mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-12-02 22:42:28 +00:00
Compare commits
2
Commits
main
...
unified-memory
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f16abbf4eb | ||
|
|
2fac6c3560 |
No files matched your search
@@ -10,6 +10,7 @@ Guest execution uses original, purpose-built test programs. Commercial Xbox game
|
||||
remains unsupported.
|
||||
|
||||
### Added
|
||||
- **Unified memory probes (research only, no shim change).** `tools/probes/unified-memory/` holds three synthetic D3D12 programs and a Wine builtin helper pair (`bx_host.dll` and an x86_64 Mach-O `bx_host.so`, built by its `build.sh`, not by `scripts/build-shims.sh`). Results for one host are in `docs/research/unified-memory-probes.md`: a heap page mapped over a section view with `mach_vm_remap` is shared by the CPU and the GPU in both directions, D3DMetal accounts heaps in 256 MiB pools, a small committed buffer costs about 1.7 KiB, and BC1 tiles of reserved textures can be taken from, and shared on, a CPU-visible heap.
|
||||
- **Ghidra first: unimplemented calls name their callers, and `bx-cli ghidra` finds the function.**
|
||||
- `runtime_veh` logs a `stub_caller` event once for each unimplemented call (signature, stub, direct caller): the stub, its caller and one more frame, as `module+offset`. The stub's own event is unchanged, so report signatures don't change.
|
||||
- `bx-cli report` lists those caller chains under each unimplemented call.
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
- [gfx10-translate](../shader-transpiler/crates/gfx10-translate/README.md) — BetterXen's GFX10.3 console shader translator: container reader, decoder with an LLVM-generated opcode table, HLSL translation ported from the Python prototype, coverage over local shaders, and how to add an instruction.
|
||||
- [Shader Corpus](../shader-transpiler/corpus/README.md) — Phase 0 of the console shader translator: BetterXen-authored kernels built locally into console containers, Microsoft ISA listings and PC DXIL, with the derived `FSPX` code-section layout (#649).
|
||||
- [D3DMetal GPU Limits](research/d3dmetal-gpu-limits.md) — Constraints a shader translator targeting D3DMetal must handle, measured by the `test_d3dmetal_gpu_limits` fixture (`WaveSize(64)` and SM 6.6 64-bit atomics not run, bounded lock emulation), plus unverified reports kept separate as leads (#453).
|
||||
- [Unified Memory Probes](research/unified-memory-probes.md) — Synthetic probes on one host: a D3D12 heap page mapped behind a title-style address is shared by CPU and GPU, D3DMetal's 256 MiB heap pools, and texture tiles taken from a CPU-visible heap (`tools/probes/unified-memory/`).
|
||||
- [Console Compute Shader Entry ABI](research/console-shader-abi.md) — Wave size, root-argument registers, descriptor forms and binding metadata observed from the NewBE backend of Microsoft's Series X|S PIX shader compiler on project-authored probes (class U: an authorized, not yet independently reproduced observation; inferred register roles).
|
||||
- [Public Technical References](research/sources.md), [Xbox GDK Notes](research/xbox-gdk.md), [RDNA 2 Notes](research/rdna2.md), and [Shader Containers](research/shader-containers.md) — Public specification references for shim and shader work.
|
||||
|
||||
|
||||
@@ -0,0 +1,239 @@
|
||||
# Unified memory probes (Milestone 0)
|
||||
|
||||
Can title memory and GPU memory be the same pages under CrossOver and D3DMetal? These are the
|
||||
results of six synthetic probes, which use no title and no GDK material, and of one measured run of
|
||||
the target build (P4).
|
||||
|
||||
- **Host:** one Apple Silicon Mac with 16 GB, macOS 27.2, CrossOver 26.3 with D3DMetal 3.0,
|
||||
Rosetta 2, run on 2026-12-02 in a bottle with the `d3dmetal` backend.
|
||||
- **Evidence class:** local implementer runs, not independently reproduced. They describe this
|
||||
host and these versions, not D3DMetal or macOS in general.
|
||||
- **Sources:** `tools/probes/unified-memory/` (`build.sh`, `p1_alias.cpp`, `p2_heap.cpp`,
|
||||
`p3_tiles.cpp`, and the helper pair `bx_host.c` / `bx_host_unix.c`).
|
||||
- **Target build:** P4 is one run of the target build; its figures are for that run only.
|
||||
|
||||
## How the probes run
|
||||
|
||||
`build.sh` builds a Wine builtin pair: `x86_64-windows/bx_host.dll` (mingw, stamped
|
||||
`Wine builtin DLL` after the DOS header) and `x86_64-unix/bx_host.so` (x86_64 Mach-O exporting
|
||||
`__wine_unix_call_funcs`). Two things are needed for Wine to load the native half:
|
||||
|
||||
- the directory holding both subdirectories is on `WINEDLLPATH` (CrossOver's `bin/wine` applies
|
||||
`--env` after it sets its own value, so the default entries must be repeated);
|
||||
- a copy of the stamped DLL lies where the Windows search finds it (beside the program). Without
|
||||
a file there the loader never looks for a builtin: the first attempt failed with error 126.
|
||||
|
||||
```sh
|
||||
tools/probes/unified-memory/build.sh
|
||||
OUT=$PWD/target/unified-memory-probes; cp $OUT/x86_64-windows/bx_host.dll $OUT/
|
||||
CX=/Applications/CrossOver.app/Contents/SharedSupport/CrossOver
|
||||
$CX/bin/wine --bottle <bottle> --env "CX_GRAPHICS_BACKEND=d3dmetal \
|
||||
WINEDLLPATH=$OUT:$CX/lib/wine/x86_64-windows:$CX/lib/wine/i386-windows:$CX/lib/wine" $OUT/p1_alias.exe
|
||||
```
|
||||
|
||||
## P1: one page, seen by the CPU at a title-style address and by the GPU
|
||||
|
||||
A 64 MiB heap (`CUSTOM`, `L0`, `WRITE_BACK`) with one placed buffer over it, mapped. A 64 KiB
|
||||
section view is made the way `xmem` makes them (placeholder, `MapViewOfFile3`, commit), then the
|
||||
native half maps a heap page over it with `mach_vm_remap` (shared, fixed, overwrite). 20 of 20
|
||||
checks passed:
|
||||
|
||||
| Check | Result |
|
||||
| --- | --- |
|
||||
| Native half reached from a mingw DLL through Wine's unix call | yes |
|
||||
| Process page size / host page size | 4 KiB / 16 KiB |
|
||||
| Heap base alignment | 16 KiB (not 64 KiB) |
|
||||
| `mach_vm_remap` over the view | success, read-write |
|
||||
| CPU write through the view, then GPU copy out | same bytes, 8 of 8 rounds, no `Map`/`Unmap` |
|
||||
| GPU copy in, then CPU read through the view | same bytes, 8 of 8 rounds |
|
||||
| `ReadFile` into the view | succeeds; bytes are in the heap page |
|
||||
| `VirtualProtect` to no access and back | honoured; bytes intact |
|
||||
| `VirtualQuery` on the view | still a committed mapped view |
|
||||
| A second view of the same heap page | shows the first view's writes |
|
||||
| Cost of one overlay | 1.4 microseconds (10,000 overlays) |
|
||||
| `UnmapViewOfFile2` preserving the placeholder | slot inaccessible again; heap page keeps its bytes |
|
||||
| Mapping the section there again | shows the section's own bytes |
|
||||
|
||||
## P2: how heaps behave and what they cost
|
||||
|
||||
- **Placement granularity is 256 bytes.** A buffer placed at a heap offset that is a multiple of
|
||||
256 (tested 64 KiB, 4 KiB and 256 bytes past a 64 KiB boundary) reads the heap's bytes at that
|
||||
offset. Offsets of 16 and 4 bytes are accepted but the buffer does not start at those bytes.
|
||||
- **Heaps come out of 256 MiB pools that are counted whole and not given back.** Ten 64 MiB heaps
|
||||
made in turn, each touched in full and used once by the GPU, moved the process footprint by
|
||||
+28, +28, +28, +285, +285, +285, +541, +541, +541, +797 MiB. Touching or using a heap changed
|
||||
nothing by itself. After releasing all ten the footprint was still +769 MiB.
|
||||
- **A small committed buffer costs about 1.7 KiB.** 50,000 committed 240-byte buffers, each mapped
|
||||
and written, added 85 MiB. Releasing them left 26 MiB.
|
||||
|
||||
Not tested: shader views (`FirstElement`) on a heap-wide buffer, which need a shader.
|
||||
|
||||
## P3: texture tiles on a CPU-visible heap
|
||||
|
||||
The device reports `TiledResourcesTier` 2 and `ResourceHeapTier` 2.
|
||||
|
||||
| Check | Result |
|
||||
| --- | --- |
|
||||
| `UpdateTileMappings` of a reserved texture onto scattered, descending tiles of the CPU-visible heap | texels land in exactly the 16 named heap tiles, visible through the heap-wide buffer |
|
||||
| Reserved buffer, 16 tiles mapped to scattered heap tiles | reads the bytes the CPU wrote into those tiles |
|
||||
| A tile unmapped again | reads zero |
|
||||
| **BC1**: how blocks lie in a 64 KiB tile | plain 8-byte blocks; each tile holds only its own 512x256 texels; order (0,0) (1,0) (0,1) (1,1) (2,0) ... (Morton, x in the low bit) |
|
||||
| **BC1**: second reserved texture on the same heap tiles | reads the same blocks |
|
||||
| **BC1**: one block written into the heap tile by the CPU | is what the texture reads there |
|
||||
| **RGBA8**: how texels lie in a tile | not plain texels (no tile held only its own texels) |
|
||||
| **RGBA8**: second reserved texture on the same heap tiles | does **not** read the same texels (all differ) |
|
||||
| **RGBA8**: CPU overwrite of one heap tile | changes that tile of the texture and no other |
|
||||
| 1024x1024 BC7, 11 mips | 256x256 tiles; mips 0 to 2 standard (16, 4, 1 tiles); 8 packed mips in 1 tile |
|
||||
| 1024x1024 BC1, 11 mips | 512x256 tiles; mips 0 to 1 standard (8, 2 tiles); 9 packed mips in 1 tile |
|
||||
|
||||
Reading of the RGBA8 rows (an inference, not tested further): this host stores such textures in an
|
||||
encoded form whose description is kept per texture, so their tiles cannot be shared or written as
|
||||
plain texels. Block-compressed textures do not have that property.
|
||||
|
||||
## P5 and P6: what unused textures cost
|
||||
|
||||
A first version of this probe (`p5_small_textures.cpp`) created textures and read the footprint
|
||||
without submitting any GPU work, and found a few KiB per texture. That was misleading: D3DMetal
|
||||
makes the pools behind its allocations resident at the next GPU submission. `p6_targets.cpp` adds
|
||||
one unrelated submission (a 16-byte buffer copy) and runs each case in a process of its own, since
|
||||
pools are kept and reused. Textures are committed, created and never used:
|
||||
|
||||
| Never-used textures | Host-reported total | Footprint at creation | After one unrelated GPU submission |
|
||||
| --- | --- | --- | --- |
|
||||
| 30 x 3840x2160 render-target BGRA8 | 971 MiB | +0 MiB | +1,291 MiB |
|
||||
| 30 x 3840x2160 unordered-access RGBA8 | 956 MiB | +0 MiB | +1,044 MiB |
|
||||
| 30 x 3840x2160 depth D32 | 971 MiB | +0 MiB | +1,300 MiB |
|
||||
| 30 x 3840x2160 plain RGBA8 | 971 MiB | +0 MiB | +1,281 MiB |
|
||||
| 10,000 x 16x4 RGBA16F | 625 MiB | +25 MiB | +282 MiB |
|
||||
| 10,000 x 128x128 BC1 | 625 MiB | +25 MiB | +281 MiB |
|
||||
| 10,000 x 64x64 BC7 | 625 MiB | +25 MiB | +281 MiB |
|
||||
|
||||
A texture that is never used still costs its size once the GPU runs anything, rounded up to whole
|
||||
256 MiB pools. Ten thousand small textures fit one pool, about 28 KiB each, less than the 64 KiB
|
||||
the host reports for each. Releasing them returned nothing in the same process.
|
||||
|
||||
## P7: GPU-written textures as reserved resources
|
||||
|
||||
`p7_reserved_targets.cpp`, one case per process:
|
||||
|
||||
| Check | Render target BGRA8 | Depth D32 | Unordered access RGBA8 |
|
||||
| --- | --- | --- | --- |
|
||||
| 30 reserved, unmapped 3840x2160 textures after an unrelated GPU submission | -13 MiB | -12 MiB | -27 MiB |
|
||||
| Reserved 1024x1024 texture created and all tiles mapped to a heap | yes | yes | yes |
|
||||
| Written and read back, 1,048,576 texels | cleared as a target: all correct | cleared as depth: all correct | copied in and out: all correct |
|
||||
|
||||
Not tested: shader writes into a reserved unordered-access texture (the probe has no compute
|
||||
shader), and drawing into the reserved render target with a pipeline.
|
||||
|
||||
## P4: measurements on the target build
|
||||
|
||||
One supervised run of the target build (Series S profile, `main` plus diagnostic counters, compute
|
||||
pipelines not translated), sampled 180 s after its intro began. The counters are in
|
||||
`shims/d3d12_xs/placed_x.cpp` (`host_memory_buffers`, `host_memory_textures`),
|
||||
`shims/xcrdapi/xcrdapi.cpp` (`dstorage_delivered`) and `shims/xmem/physical_pages.cpp`
|
||||
(`page_touches`). They are diagnostics on a local branch, not merged.
|
||||
|
||||
**Process footprint 8.6 GB:**
|
||||
|
||||
| Part | Size | What the map says |
|
||||
| --- | --- | --- |
|
||||
| Graphics, not CPU-mapped | 2.67 GB | 97 objects: textures in GPU-only storage |
|
||||
| Graphics, CPU-mapped | 2.47 GB | 1,104 regions: buffers |
|
||||
| Wine's anonymous memory | 2.67 GB dirty | 2.3 GB of it already compressed or swapped |
|
||||
| Rosetta, allocator heaps, other | about 0.8 GB | |
|
||||
|
||||
**Host resources the shim made (its own count, sizes as asked for or as the host reports):**
|
||||
|
||||
| Kind | Count | Size | Note |
|
||||
| --- | --- | --- | --- |
|
||||
| Buffers for views of placed memory | 173,566 | 873 MiB | grew from 40 MiB to 725 MiB in 120 s |
|
||||
| Buffers for placed resources | 4,203 | 761 MiB | flat after loading |
|
||||
| Buffers, all: under 16 KiB / to 1 MiB / larger | 169,060 / 8,608 / 121 | 158 / 671 / 827 MiB | |
|
||||
| GPU-written placed textures (render target, depth, unordered access) | 1,174 | 2,069 MiB | present from the menus on |
|
||||
| GPU-written committed textures | 10 | 128 MiB | |
|
||||
| Read-only textures under 64 KiB, committed | 19,077 | 1,217 MiB as reported | by P6, about 28 KiB each in whole pools: roughly 0.5 GB |
|
||||
| Reserved read-only textures: live / memory mapped | 4,105 / 31 MiB | | 33 mappings made |
|
||||
|
||||
**Page pool and DirectStorage:**
|
||||
|
||||
- The pool is one 4.17 GB Wine section file (`tmpmap-*`), with three smaller ones (311, 231 and
|
||||
194 MB). The game process had 204 MB of those mappings resident.
|
||||
- During the 205 s run the system counted 302,875 page-outs (about 4.6 GB at 16 KiB) and 45,948
|
||||
swap-outs. The run cannot say which file they went to.
|
||||
- DirectStorage delivered 2,644 MiB zlib and 297 MiB BCPack into physical pages, and 5,870 MiB to
|
||||
memory addresses (cumulative, destinations are reused).
|
||||
- First touches of 64 KiB pages: 34,429 written through DirectStorage spans; 2,638 first touched by
|
||||
the title's own code, **none of them span-written**; 60,228 first touched from other modules
|
||||
(the shims and the libraries they call), 5,665 of them span-written. Counts are cumulative over
|
||||
allocations, not live pages.
|
||||
|
||||
**Which GPU-written textures the title used** (second run, 4 minutes, `host_memory_written_use`).
|
||||
A texture counts as used once the title binds it as a target, names it in a barrier, copies or
|
||||
resolves to or from it, or a translated draw reads or writes it through a view:
|
||||
|
||||
| | Count | Size |
|
||||
| --- | --- | --- |
|
||||
| Used | 222 | 559 MiB |
|
||||
| Never used | 962 | 1,655 MiB |
|
||||
| First used in the last 30 s of the run | 10 | 6 MiB |
|
||||
|
||||
The largest never-used ones are 3840x2160 (32 to 41 MiB each). Not seen by this count: clears, and
|
||||
all but the first view of a single-range target bind; compute pipelines are not translated on this
|
||||
branch, so their views are not seen either. The never-used figure is therefore an upper bound.
|
||||
|
||||
**Pages under the buffers** (`host_memory_buffer_pages`, same run): the buffers placed in title
|
||||
memory held 1,757 MiB of copies and covered 28,752 distinct 64 KiB pages of title addresses
|
||||
(1,797 MiB); the ones made for views alone 988 MiB over 17,927 pages (1,120 MiB). Title addresses,
|
||||
not physical pages, so the page figures are upper bounds.
|
||||
|
||||
Reading (inference): the two graphics rows of the footprint match the kinds below them: GPU-only
|
||||
storage is mostly the 2.2 GB of GPU-written textures, and the CPU-mapped part is the buffer copies
|
||||
(1.66 GB of data, about 0.3 GB of per-buffer overhead at P2's 1.7 KiB, and pool rounding).
|
||||
|
||||
## What this decides
|
||||
|
||||
- The native helper and the page overlay work on this host: a page of a D3D12 heap can be the
|
||||
memory behind a title address, for the CPU, Windows calls and the GPU alike.
|
||||
- Heap memory should be asked for in few, large steps: D3DMetal accounts 256 MiB at a time and
|
||||
keeps it.
|
||||
- Packing small buffers saves about 1.7 KiB per buffer, not a memory page per buffer.
|
||||
- For block-compressed textures, tiles can be taken from chosen pages of a CPU-visible heap and
|
||||
shared between textures. For plain RGBA8 they cannot be shared.
|
||||
- On the target build, host copies of streamed textures are not where the memory is (31 MiB mapped):
|
||||
it is in GPU-written textures (about 2.2 GB) and in buffer copies (about 2.4 GB, and growing
|
||||
through buffers made for views).
|
||||
- The title's own code did not touch any page DirectStorage had written into, in this run.
|
||||
- About 1.65 GB of GPU-written textures were never used in four minutes, an unused texture costs
|
||||
its full size (P6), and reserved render-target and depth textures cost nothing until mapped and
|
||||
work once mapped (P7). Giving GPU-written textures their memory at first use, as read-only
|
||||
textures already get it, is the largest saving measured here.
|
||||
- Sharing pages for buffers saves little footprint by itself: the buffers' own pages (up to
|
||||
1.8 GB) are about as large as the copies (1.76 GB plus overhead). Its value is that the title-side
|
||||
pages stop being a second copy, the copies stop growing, and GPU writes reach the title.
|
||||
|
||||
## The same probes on Mullion (evaluation, 2026-12-02)
|
||||
|
||||
Mullion (`dttdrv/mullion`, LGPL-2.1, release `v0.5-alpha.4`) is an open-source Direct3D 12 to Metal
|
||||
layer built on DXMT. Its release was installed into a fresh bottle only, by its own installer, which
|
||||
gives the bottle a CrossOver root of its own and leaves CrossOver and other bottles unchanged. No
|
||||
licence or provenance review has been recorded yet and nothing in BetterXen depends on it. Run the
|
||||
probes with `CX_ROOT=<that root> BX_PROBE_BACKEND=dxmt`.
|
||||
|
||||
| Probe | D3DMetal 3.0 | Mullion v0.5-alpha.4 |
|
||||
| --- | --- | --- |
|
||||
| P1 page overlay with the native helper | 20 of 20 | 20 of 20 |
|
||||
| P2 heap memory | 256 MiB pools, kept after release | counted as used; a touched 64 MiB heap and ten more all returned on release |
|
||||
| P2 placed buffer offsets | any multiple of 256 bytes | multiples of 64 KiB only (`E_INVALIDARG` otherwise) |
|
||||
| P2 50,000 small committed buffers | 1.7 KiB each | 3.5 KiB each |
|
||||
| P3 tiled resources | tier 2; RGBA8 tiles cannot be shared | tier 3; RGBA8 and BC1 tiles shared, 0 failed |
|
||||
| P6 30 never-used 3840x2160 textures | +1,044 to +1,300 MiB, kept | +956 to +972 MiB, returned on release |
|
||||
| P6 10,000 never-used 128x128 BC1 | +281 MiB | +187 MiB |
|
||||
| P7 reserved GPU-written textures | cost nothing unmapped; work mapped | the same |
|
||||
| P8 `OpenExistingHeapFromFileMapping`, committed section of 1 GiB | not implemented (`E_NOTIMPL`, #473) | succeeds; +0 MiB; +1 MiB after one page is used |
|
||||
| P8 a buffer placed on that heap, against two views of the section | | GPU writes show in the views, view writes are read by the GPU with no `Map`, `ReadFile` into the view works, a page first written by the GPU shows in a view mapped later |
|
||||
| P8 the same with a reserved section (`SEC_RESERVE`) | | refused, `E_INVALIDARG` |
|
||||
| P8 `OpenExistingHeapFromAddress` on `VirtualAlloc` memory | not implemented | succeeds, both directions coherent |
|
||||
| P8 tiles of a reserved texture mapped to the imported heap | | `UpdateTileMappings` never returns (process idle) |
|
||||
|
||||
`p8_import.cpp` holds the import checks. Not run on Mullion: the conformance suite, the shims and
|
||||
the target build.
|
||||
@@ -169,6 +169,7 @@ void resolve(ID3D12Device* device, const char* key, const BxConsoleUse& use, uin
|
||||
// Held for this resolve: bx_console_forget_resource may drop the registry's reference.
|
||||
if (ok && view.resource) view.resource->AddRef();
|
||||
}
|
||||
if (ok && view.resource) bx_xbox_note_resource_use(view.resource, 4); // diagnostic; outside g_view_lock
|
||||
const D3D12_CPU_DESCRIPTOR_HANDLE host = bx_xbox_host_cpu_handle({static_cast<SIZE_T>(entry)});
|
||||
uint64_t total = 0;
|
||||
// A view whose resource the shim has released (an implicit shadow the title replaced) resolves
|
||||
|
||||
@@ -248,6 +248,7 @@ void STDMETHODCALLTYPE bx_xbox_device_create_render_target_view(void* self, ID3D
|
||||
ID3D12Resource* host_resource = bx_xbox_unwrap_as<ID3D12Resource>(resource);
|
||||
const D3D12_RENDER_TARGET_VIEW_DESC* host_desc = host.of(desc, "CreateRenderTargetView");
|
||||
bx_xbox_host<ID3D12Device>(self)->CreateRenderTargetView(host_resource, host_desc, bx_xbox_host_cpu_handle(dest));
|
||||
bx_xbox_note_target_view(bx_xbox_host_cpu_handle(dest).ptr, host_resource);
|
||||
bx_console_rtv_created(bx_xbox_host_cpu_handle(dest).ptr, view_format(host_resource, host_desc ? host_desc->Format : DXGI_FORMAT_UNKNOWN));
|
||||
}
|
||||
|
||||
@@ -259,6 +260,7 @@ void STDMETHODCALLTYPE bx_xbox_device_create_depth_stencil_view(void* self, ID3D
|
||||
ID3D12Resource* host_resource = bx_xbox_unwrap_as<ID3D12Resource>(resource);
|
||||
const D3D12_DEPTH_STENCIL_VIEW_DESC* host_desc = host.of(desc, "CreateDepthStencilView");
|
||||
bx_xbox_host<ID3D12Device>(self)->CreateDepthStencilView(host_resource, host_desc, bx_xbox_host_cpu_handle(dest));
|
||||
bx_xbox_note_target_view(bx_xbox_host_cpu_handle(dest).ptr, host_resource);
|
||||
bx_console_dsv_created(bx_xbox_host_cpu_handle(dest).ptr, view_format(host_resource, host_desc ? host_desc->Format : DXGI_FORMAT_UNKNOWN));
|
||||
}
|
||||
|
||||
|
||||
@@ -242,6 +242,9 @@ void STDMETHODCALLTYPE bx_xbox_list_om_set_render_targets(void* self, UINT count
|
||||
const UINT n = count > 8 ? 8 : count;
|
||||
for (UINT i = 0; rtvs && i < (single_range ? 1u : n); ++i) host_rtvs[i] = bx_xbox_host_cpu_handle(rtvs[i]);
|
||||
const D3D12_CPU_DESCRIPTOR_HANDLE host_dsv = dsv ? bx_xbox_host_cpu_handle(*dsv) : D3D12_CPU_DESCRIPTOR_HANDLE{};
|
||||
// Diagnostic: with a single range only the first view is known here; the others are not marked.
|
||||
for (UINT i = 0; rtvs && i < (single_range ? 1u : n); ++i) bx_xbox_note_target_bound(host_rtvs[i].ptr);
|
||||
if (dsv) bx_xbox_note_target_bound(host_dsv.ptr);
|
||||
bx_xbox_host<ID3D12GraphicsCommandList>(self)->OMSetRenderTargets(n, rtvs ? host_rtvs : nullptr, single_range,
|
||||
dsv ? &host_dsv : nullptr);
|
||||
bx_console_list_targets(bx_xbox_host<ID3D12GraphicsCommandList>(self), n, rtvs ? host_rtvs : nullptr, single_range,
|
||||
|
||||
+184
-3
@@ -25,6 +25,7 @@
|
||||
|
||||
#include "bx_log.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
@@ -51,6 +52,9 @@ struct Shadow {
|
||||
bool simultaneous = false; // texture made with D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS
|
||||
uint64_t span = 0; // implicit: the bytes of title memory the resource covers
|
||||
bool reserved = false; // texture made as a reserved resource: memory mapped at first use
|
||||
uint64_t host_bytes = 0; // diagnostic: what the host says the resource takes (0 for a reserved texture)
|
||||
unsigned char used = 0; // diagnostic: how the title has used it (bx_xbox_note_resource_use)
|
||||
uint64_t made_ms = 0, first_use_ms = 0; // diagnostic: GetTickCount64 at creation and at first use
|
||||
};
|
||||
|
||||
/*
|
||||
@@ -274,7 +278,9 @@ void for_candidates_locked(uint64_t lo, uint64_t hi, F f) {
|
||||
}
|
||||
}
|
||||
|
||||
bool remember(const Shadow& shadow) {
|
||||
bool remember(const Shadow& new_shadow) {
|
||||
Shadow shadow = new_shadow;
|
||||
shadow.made_ms = GetTickCount64();
|
||||
std::lock_guard<std::mutex> guard(g_lock);
|
||||
if (g_count == g_capacity) {
|
||||
const size_t capacity = g_capacity ? g_capacity * 2 : 64;
|
||||
@@ -397,6 +403,11 @@ HRESULT make_shadow(ID3D12Device* device, uint64_t title_va, const D3D12_RESOURC
|
||||
Shadow record{resource, title_va, desc.Width, buffer, desc, 0};
|
||||
record.simultaneous = made_simultaneous;
|
||||
record.reserved = reserved;
|
||||
if (buffer) {
|
||||
record.host_bytes = desc.Width;
|
||||
} else if (!reserved) {
|
||||
record.host_bytes = device->GetResourceAllocationInfo(0, 1, &host_desc).SizeInBytes;
|
||||
}
|
||||
if (reserved) {
|
||||
std::lock_guard<std::mutex> guard(g_memory_lock);
|
||||
g_texture_memory[resource] = TextureMemory{};
|
||||
@@ -998,8 +1009,14 @@ D3D12_GPU_VIRTUAL_ADDRESS bx_xbox_host_gpu_va(D3D12_GPU_VIRTUAL_ADDRESS title) {
|
||||
}
|
||||
|
||||
bool bx_xbox_title_memory_attach(ID3D12Resource* host, uint64_t title_va, uint64_t size, ULONGLONG attributes) {
|
||||
return remember({host, title_va, size, host->GetDesc().Dimension == D3D12_RESOURCE_DIMENSION_BUFFER, host->GetDesc(),
|
||||
attributes});
|
||||
Shadow record{host, title_va, size, host->GetDesc().Dimension == D3D12_RESOURCE_DIMENSION_BUFFER, host->GetDesc(), attributes};
|
||||
record.host_bytes = record.desc.Width;
|
||||
ID3D12Device* device = nullptr;
|
||||
if (!record.buffer && SUCCEEDED(host->GetDevice(__uuidof(ID3D12Device), reinterpret_cast<void**>(&device))) && device) {
|
||||
record.host_bytes = device->GetResourceAllocationInfo(0, 1, &record.desc).SizeInBytes;
|
||||
device->Release();
|
||||
}
|
||||
return remember(record);
|
||||
}
|
||||
|
||||
void bx_xbox_texture_memory_for_write(ID3D12Resource* host) {
|
||||
@@ -1201,9 +1218,173 @@ ID3D12Resource* bx_xbox_texture_contents(ID3D12Resource* host, BxTextureUploads*
|
||||
return host;
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
/*
|
||||
* Diagnostic (unified-memory measurements): every 10 s of submissions, what the host resources
|
||||
* this file made take, by kind. Buffers are counted at the size asked for, committed textures at
|
||||
* the size the host reports for them, reserved textures at the heap mapped behind them.
|
||||
* "committed" resources are the ones a title creates without placing them; "view" buffers are made
|
||||
* for a view of placed memory that has no resource; "bound" ones for memory bound by address.
|
||||
*/
|
||||
void report_host_memory() {
|
||||
static std::atomic<uint64_t> next{0};
|
||||
const uint64_t now = GetTickCount64();
|
||||
uint64_t due = next.load();
|
||||
if (now < due || !next.compare_exchange_strong(due, now + 10000)) return;
|
||||
struct Kind {
|
||||
uint64_t count = 0, bytes = 0;
|
||||
};
|
||||
Kind buf_placed, buf_committed, buf_view, buf_small, buf_medium, buf_large;
|
||||
Kind written_used, written_unused, used_target, used_barrier_only, used_draw, used_copy, first_use_recent;
|
||||
struct Unused {
|
||||
uint64_t bytes, width;
|
||||
UINT height;
|
||||
int format;
|
||||
UINT flags;
|
||||
};
|
||||
std::vector<Unused> unused;
|
||||
std::vector<uint64_t> buffer_pages, view_pages;
|
||||
Kind tex_reserved, tex_readonly, tex_written, tex_committed_written, tex_committed_other;
|
||||
constexpr UINT kWritten = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL |
|
||||
D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
|
||||
auto add = [](Kind& k, uint64_t bytes) {
|
||||
++k.count;
|
||||
k.bytes += bytes;
|
||||
};
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(g_lock);
|
||||
for (size_t i = 0; i < g_count; ++i) {
|
||||
const Shadow& s = g_shadows[i];
|
||||
if (s.buffer) {
|
||||
add(s.title_attributes ? buf_committed : s.implicit ? buf_view : buf_placed, s.host_bytes);
|
||||
add(s.host_bytes < 16384 ? buf_small : s.host_bytes < (1u << 20) ? buf_medium : buf_large, s.host_bytes);
|
||||
if (!s.title_attributes && s.size) { // placed in title memory: the 64 KiB pages it covers
|
||||
for (uint64_t page = s.title_va >> 16; page <= (s.title_va + s.size - 1) >> 16; ++page) {
|
||||
buffer_pages.push_back(page);
|
||||
if (s.implicit) view_pages.push_back(page);
|
||||
}
|
||||
}
|
||||
} else if (s.reserved) {
|
||||
add(tex_reserved, 0);
|
||||
} else {
|
||||
const bool written = (static_cast<UINT>(s.desc.Flags) & kWritten) != 0;
|
||||
if (written) {
|
||||
if (s.used) {
|
||||
add(written_used, s.host_bytes);
|
||||
if (s.used & 1) add(used_target, s.host_bytes);
|
||||
if (s.used == 2) add(used_barrier_only, s.host_bytes);
|
||||
if (s.used & 4) add(used_draw, s.host_bytes);
|
||||
if (s.used & 8) add(used_copy, s.host_bytes);
|
||||
if (now - s.first_use_ms < 30000) add(first_use_recent, s.host_bytes);
|
||||
} else {
|
||||
add(written_unused, s.host_bytes);
|
||||
unused.push_back({s.host_bytes, s.desc.Width, s.desc.Height, static_cast<int>(s.desc.Format), static_cast<UINT>(s.desc.Flags)});
|
||||
}
|
||||
}
|
||||
add(s.title_attributes ? (written ? tex_committed_written : tex_committed_other) : (written ? tex_written : tex_readonly),
|
||||
s.host_bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
uint64_t upload_bytes = 0, uploads = 0, retired = 0;
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(g_texture_lock);
|
||||
for (const auto& entry : g_texture_uploads) {
|
||||
if (entry.second.upload) {
|
||||
++uploads;
|
||||
upload_bytes += entry.second.upload_bytes;
|
||||
}
|
||||
}
|
||||
retired = g_retired.size();
|
||||
}
|
||||
constexpr double kMiB = 1024.0 * 1024.0;
|
||||
char detail[400];
|
||||
std::snprintf(detail, sizeof(detail),
|
||||
"host buffers MiB (count): placed %.0f (%llu), committed %.0f (%llu), for views %.0f (%llu); "
|
||||
"by size: under 16 KiB %.0f (%llu), to 1 MiB %.0f (%llu), larger %.0f (%llu)",
|
||||
buf_placed.bytes / kMiB, static_cast<unsigned long long>(buf_placed.count), buf_committed.bytes / kMiB,
|
||||
static_cast<unsigned long long>(buf_committed.count), buf_view.bytes / kMiB,
|
||||
static_cast<unsigned long long>(buf_view.count), buf_small.bytes / kMiB, static_cast<unsigned long long>(buf_small.count),
|
||||
buf_medium.bytes / kMiB, static_cast<unsigned long long>(buf_medium.count), buf_large.bytes / kMiB,
|
||||
static_cast<unsigned long long>(buf_large.count));
|
||||
bx_log_emit(BX_LOG_INFO, "d3d12_xs", "host_memory_buffers", detail);
|
||||
std::snprintf(detail, sizeof(detail),
|
||||
"host textures MiB (count): reserved, mapped now %.0f (%llu mappings made so far, %llu reserved live); placed read-only committed %.0f (%llu); "
|
||||
"placed GPU-written %.0f (%llu); title-committed GPU-written %.0f (%llu), other %.0f (%llu); "
|
||||
"upload buffers %.0f (%llu live, %llu retiring)",
|
||||
g_mapped_bytes.load() / kMiB, static_cast<unsigned long long>(g_mapped_count.load()),
|
||||
static_cast<unsigned long long>(tex_reserved.count), tex_readonly.bytes / kMiB,
|
||||
static_cast<unsigned long long>(tex_readonly.count), tex_written.bytes / kMiB,
|
||||
static_cast<unsigned long long>(tex_written.count), tex_committed_written.bytes / kMiB,
|
||||
static_cast<unsigned long long>(tex_committed_written.count), tex_committed_other.bytes / kMiB,
|
||||
static_cast<unsigned long long>(tex_committed_other.count), upload_bytes / kMiB,
|
||||
static_cast<unsigned long long>(uploads), static_cast<unsigned long long>(retired));
|
||||
bx_log_emit(BX_LOG_INFO, "d3d12_xs", "host_memory_textures", detail);
|
||||
std::snprintf(detail, sizeof(detail),
|
||||
"GPU-written textures MiB (count): used %.0f (%llu), never used %.0f (%llu); of the used: bound as a target %.0f (%llu), "
|
||||
"in a translated draw's view %.0f (%llu), copied %.0f (%llu), named in a barrier only %.0f (%llu); first used in the last 30 s %.0f (%llu)",
|
||||
written_used.bytes / kMiB, static_cast<unsigned long long>(written_used.count), written_unused.bytes / kMiB,
|
||||
static_cast<unsigned long long>(written_unused.count), used_target.bytes / kMiB,
|
||||
static_cast<unsigned long long>(used_target.count), used_draw.bytes / kMiB, static_cast<unsigned long long>(used_draw.count),
|
||||
used_copy.bytes / kMiB, static_cast<unsigned long long>(used_copy.count), used_barrier_only.bytes / kMiB,
|
||||
static_cast<unsigned long long>(used_barrier_only.count), first_use_recent.bytes / kMiB,
|
||||
static_cast<unsigned long long>(first_use_recent.count));
|
||||
bx_log_emit(BX_LOG_INFO, "d3d12_xs", "host_memory_written_use", detail);
|
||||
std::sort(unused.begin(), unused.end(), [](const Unused& a, const Unused& b) { return a.bytes > b.bytes; });
|
||||
int at = std::snprintf(detail, sizeof(detail), "largest never-used GPU-written textures (MiB WxH format flags):");
|
||||
for (size_t i = 0; i < unused.size() && i < 10 && at > 0 && at < static_cast<int>(sizeof(detail)) - 40; ++i) {
|
||||
at += std::snprintf(detail + at, sizeof(detail) - at, " %.0f %llux%u f%d 0x%x;", unused[i].bytes / kMiB,
|
||||
static_cast<unsigned long long>(unused[i].width), unused[i].height, unused[i].format, unused[i].flags);
|
||||
}
|
||||
bx_log_emit(BX_LOG_INFO, "d3d12_xs", "host_memory_written_unused", detail);
|
||||
auto distinct = [](std::vector<uint64_t>& pages) {
|
||||
std::sort(pages.begin(), pages.end());
|
||||
return static_cast<unsigned long long>(std::unique(pages.begin(), pages.end()) - pages.begin());
|
||||
};
|
||||
const unsigned long long all_pages = distinct(buffer_pages), of_views = distinct(view_pages);
|
||||
std::snprintf(detail, sizeof(detail),
|
||||
"buffers placed in title memory cover %llu distinct 64 KiB pages of title addresses (%.0f MiB); the ones made for views alone %llu (%.0f MiB)",
|
||||
all_pages, all_pages * 65536.0 / kMiB, of_views, of_views * 65536.0 / kMiB);
|
||||
bx_log_emit(BX_LOG_INFO, "d3d12_xs", "host_memory_buffer_pages", detail);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void bx_xbox_note_resource_use(ID3D12Resource* host, unsigned how) {
|
||||
if (!host) return;
|
||||
std::lock_guard<std::mutex> guard(g_lock);
|
||||
const size_t i = find_host_locked(host);
|
||||
if (i == SIZE_MAX) return;
|
||||
if (!g_shadows[i].used) g_shadows[i].first_use_ms = GetTickCount64();
|
||||
g_shadows[i].used |= static_cast<unsigned char>(how);
|
||||
}
|
||||
|
||||
namespace {
|
||||
std::mutex g_target_lock;
|
||||
std::unordered_map<uint64_t, ID3D12Resource*> g_target_views; // host descriptor -> the resource its view shows (not referenced)
|
||||
} // namespace
|
||||
|
||||
void bx_xbox_note_target_view(uint64_t handle, ID3D12Resource* host) {
|
||||
std::lock_guard<std::mutex> guard(g_target_lock);
|
||||
g_target_views[handle] = host;
|
||||
}
|
||||
|
||||
void bx_xbox_note_target_bound(uint64_t handle) {
|
||||
ID3D12Resource* host = nullptr;
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(g_target_lock);
|
||||
auto it = g_target_views.find(handle);
|
||||
if (it != g_target_views.end()) host = it->second;
|
||||
}
|
||||
// The pointer is only looked up, never dereferenced: a released resource is no longer in the table.
|
||||
bx_xbox_note_resource_use(host, 1);
|
||||
}
|
||||
|
||||
void bx_xbox_placed_note_submission() {
|
||||
++g_submissions;
|
||||
sweep_implicit();
|
||||
report_host_memory();
|
||||
}
|
||||
|
||||
void bx_xbox_placed_forget(IUnknown* host) {
|
||||
|
||||
@@ -309,6 +309,7 @@ UINT STDMETHODCALLTYPE bx_xbox_list_resource_barrier(void* self, UINT count, con
|
||||
switch (copy[i].Type) {
|
||||
case D3D12_RESOURCE_BARRIER_TYPE_TRANSITION:
|
||||
copy[i].Transition.pResource = bx_xbox_unwrap_as(copy[i].Transition.pResource);
|
||||
bx_xbox_note_resource_use(copy[i].Transition.pResource, 2);
|
||||
break;
|
||||
case D3D12_RESOURCE_BARRIER_TYPE_ALIASING:
|
||||
copy[i].Aliasing.pResourceBefore = bx_xbox_unwrap_as(copy[i].Aliasing.pResourceBefore);
|
||||
@@ -316,6 +317,7 @@ UINT STDMETHODCALLTYPE bx_xbox_list_resource_barrier(void* self, UINT count, con
|
||||
break;
|
||||
case D3D12_RESOURCE_BARRIER_TYPE_UAV:
|
||||
copy[i].UAV.pResource = bx_xbox_unwrap_as(copy[i].UAV.pResource);
|
||||
bx_xbox_note_resource_use(copy[i].UAV.pResource, 2);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -335,10 +337,12 @@ void STDMETHODCALLTYPE bx_xbox_list_copy_texture_region(void* self, const D3D12_
|
||||
d = *dst;
|
||||
d.pResource = bx_xbox_unwrap_as(d.pResource);
|
||||
bx_xbox_texture_memory_for_write(d.pResource);
|
||||
bx_xbox_note_resource_use(d.pResource, 8);
|
||||
}
|
||||
if (src) {
|
||||
s = *src;
|
||||
s.pResource = bx_xbox_unwrap_as(s.pResource);
|
||||
bx_xbox_note_resource_use(s.pResource, 8);
|
||||
}
|
||||
bx_xbox_host<ID3D12GraphicsCommandList>(self)->CopyTextureRegion(dst ? &d : nullptr, x, y, z, src ? &s : nullptr, box);
|
||||
}
|
||||
@@ -347,6 +351,8 @@ void STDMETHODCALLTYPE bx_xbox_list_copy_resource(void* self, ID3D12Resource* ds
|
||||
BX_D3D_TRACE("XboxGraphicsCommandList", "CopyResource");
|
||||
ID3D12Resource* d = bx_xbox_unwrap_as<ID3D12Resource>(dst);
|
||||
bx_xbox_texture_memory_for_write(d);
|
||||
bx_xbox_note_resource_use(d, 8);
|
||||
bx_xbox_note_resource_use(bx_xbox_unwrap_as<ID3D12Resource>(src), 8);
|
||||
bx_xbox_host<ID3D12GraphicsCommandList>(self)->CopyResource(d, bx_xbox_unwrap_as<ID3D12Resource>(src));
|
||||
}
|
||||
|
||||
@@ -355,6 +361,8 @@ void STDMETHODCALLTYPE bx_xbox_list_resolve_subresource(void* self, ID3D12Resour
|
||||
BX_D3D_TRACE("XboxGraphicsCommandList", "ResolveSubresource");
|
||||
ID3D12Resource* d = bx_xbox_unwrap_as<ID3D12Resource>(dst);
|
||||
bx_xbox_texture_memory_for_write(d);
|
||||
bx_xbox_note_resource_use(d, 8);
|
||||
bx_xbox_note_resource_use(bx_xbox_unwrap_as<ID3D12Resource>(src), 8);
|
||||
bx_xbox_host<ID3D12GraphicsCommandList>(self)->ResolveSubresource(d, dst_sub, bx_xbox_unwrap_as<ID3D12Resource>(src), src_sub,
|
||||
format);
|
||||
}
|
||||
|
||||
@@ -256,6 +256,14 @@ ID3D12Resource* bx_xbox_texture_contents(ID3D12Resource* host, BxTextureUploads*
|
||||
/* Before a title's GPU copy or resolve writes `host` (a host resource): a texture whose memory is
|
||||
mapped at first use (placed_x.cpp, #782) gets it now, so the write is kept. */
|
||||
void bx_xbox_texture_memory_for_write(ID3D12Resource* host);
|
||||
/* Diagnostic (unified-memory measurements): the title used host resource `host`. `how`: 1 bound as a
|
||||
render target or depth buffer, 2 named in a barrier, 4 read or written by a translated draw's view,
|
||||
8 source or destination of a copy or resolve. */
|
||||
void bx_xbox_note_resource_use(ID3D12Resource* host, unsigned how);
|
||||
/* Diagnostic: a render-target or depth view at host descriptor `handle` shows `host`; and that view
|
||||
was bound. */
|
||||
void bx_xbox_note_target_view(uint64_t handle, ID3D12Resource* host);
|
||||
void bx_xbox_note_target_bound(uint64_t handle);
|
||||
|
||||
/* Logs a host call that failed (`host_call_failed`), so a refusal is never silent. */
|
||||
void bx_xbox_log_host_failure(const char* iface, const char* method, HRESULT hr);
|
||||
|
||||
@@ -590,8 +590,35 @@ bool dstorage_trace_enabled() {
|
||||
return on;
|
||||
}
|
||||
|
||||
// Diagnostic (unified-memory measurements): bytes delivered, by destination and codec, logged at
|
||||
// most every 10 s.
|
||||
void count_delivery(const Request& request, HRESULT hr) {
|
||||
static std::atomic<uint64_t> pages_bcpack{0}, pages_zlib{0}, pages_plain{0}, memory_bytes{0}, resource_bytes{0}, requests{0}, next{0};
|
||||
if (FAILED(hr)) return;
|
||||
const bool packed = request.compression == compression_zlib || request.compression == compression_bcpack;
|
||||
const uint64_t out = packed ? request.output_size : request.size;
|
||||
++requests;
|
||||
if (request.resource) resource_bytes += out;
|
||||
else if (!request.page_array) memory_bytes += out;
|
||||
else if (request.compression == compression_bcpack) pages_bcpack += out;
|
||||
else if (request.compression == compression_zlib) pages_zlib += out;
|
||||
else pages_plain += out;
|
||||
const uint64_t now = GetTickCount64();
|
||||
uint64_t due = next.load();
|
||||
if (now < due || !next.compare_exchange_strong(due, now + 10000)) return;
|
||||
constexpr double kMiB = 1024.0 * 1024.0;
|
||||
char detail[240];
|
||||
std::snprintf(detail, sizeof(detail),
|
||||
"delivered MiB so far: to physical pages BCPack %.0f, zlib %.0f, uncompressed %.0f; to memory addresses %.0f; "
|
||||
"to resources %.0f; requests %llu",
|
||||
pages_bcpack.load() / kMiB, pages_zlib.load() / kMiB, pages_plain.load() / kMiB, memory_bytes.load() / kMiB,
|
||||
resource_bytes.load() / kMiB, static_cast<unsigned long long>(requests.load()));
|
||||
bx_log_emit(BX_LOG_INFO, "xcrdapi", "dstorage_delivered", detail);
|
||||
}
|
||||
|
||||
CopyResult fulfill(HANDLE file, const Request& request) {
|
||||
const CopyResult result = fulfill_request(file, request);
|
||||
count_delivery(request, result.hr);
|
||||
if (!dstorage_trace_enabled() || request.resource) return result;
|
||||
static std::atomic<UINT> traced{0};
|
||||
if (traced.fetch_add(1) >= 20000) return result;
|
||||
|
||||
@@ -23,6 +23,10 @@ struct Page {
|
||||
PageBlock* block;
|
||||
bool allocated;
|
||||
bool releasing;
|
||||
// Diagnostic (unified-memory measurements): who has touched the page since it was allocated.
|
||||
// 1: written through a span of its own (DirectStorage); 2: first touch by the title;
|
||||
// 4: by d3d12_xs; 8: by another module.
|
||||
unsigned char touched = 0;
|
||||
};
|
||||
|
||||
// One XMemAllocatePhysicalPages call: a single pagefile-backed section holding all its pages,
|
||||
@@ -388,6 +392,57 @@ View* view_at(uintptr_t address) {
|
||||
|
||||
LONG lazy_mapped = 0; // slots mapped on first touch, logged every 4096
|
||||
|
||||
// Diagnostic (unified-memory measurements): pages by who touched them. Counted under memory_lock.
|
||||
enum : unsigned char { kBySpan = 1, kByTitle = 2, kByD3D = 4, kByOther = 8 };
|
||||
struct TouchCounts {
|
||||
LONG span = 0, title = 0, d3d = 0, other = 0; // pages first touched this way
|
||||
LONG span_then_title = 0, span_then_d3d = 0, span_then_other = 0;
|
||||
} touch_counts;
|
||||
|
||||
struct ModuleRange {
|
||||
uintptr_t begin = 0, end = 0;
|
||||
bool holds(uintptr_t at) const { return at >= begin && at < end; }
|
||||
};
|
||||
ModuleRange module_range(const wchar_t* name) {
|
||||
const HMODULE module = GetModuleHandleW(name);
|
||||
if (!module) return {};
|
||||
const auto base = reinterpret_cast<uintptr_t>(module);
|
||||
const auto* dos = reinterpret_cast<const IMAGE_DOS_HEADER*>(base);
|
||||
const auto* nt = reinterpret_cast<const IMAGE_NT_HEADERS*>(base + dos->e_lfanew);
|
||||
return {base, base + nt->OptionalHeader.SizeOfImage};
|
||||
}
|
||||
|
||||
// Which module the faulting instruction lies in. Resolved before memory_lock is taken.
|
||||
unsigned char toucher_of(uintptr_t rip) {
|
||||
static ModuleRange title = module_range(nullptr);
|
||||
static ModuleRange d3d{};
|
||||
if (!d3d.end) d3d = module_range(L"d3d12_xs.dll");
|
||||
return title.holds(rip) ? kByTitle : d3d.holds(rip) ? kByD3D : kByOther;
|
||||
}
|
||||
|
||||
// Records a first touch of `page` by `who`. Called with memory_lock held.
|
||||
void note_touch(Page* page, unsigned char who) {
|
||||
if (page->touched & who) return;
|
||||
const bool by_span = (page->touched & kBySpan) != 0;
|
||||
const bool first = (page->touched & ~kBySpan) == 0;
|
||||
page->touched |= who;
|
||||
if (who == kBySpan) {
|
||||
++touch_counts.span;
|
||||
return;
|
||||
}
|
||||
if (!first) return; // counted for its first toucher only
|
||||
if (who == kByTitle) {
|
||||
++touch_counts.title;
|
||||
if (by_span) ++touch_counts.span_then_title;
|
||||
} else if (who == kByD3D) {
|
||||
++touch_counts.d3d;
|
||||
if (by_span) ++touch_counts.span_then_d3d;
|
||||
} else {
|
||||
++touch_counts.other;
|
||||
if (by_span) ++touch_counts.span_then_other;
|
||||
}
|
||||
}
|
||||
|
||||
// First touch of a recorded slot: map it and run the instruction again. Any instruction in the
|
||||
// process takes this path, the title's or a shim's. A system call writing into the slot does not
|
||||
// fault but fails, so those writers map the slot first: ReadFile through import_hooks.cpp, and
|
||||
@@ -403,6 +458,8 @@ LONG CALLBACK lazy_fault(PEXCEPTION_POINTERS ep) {
|
||||
LONG verdict = EXCEPTION_CONTINUE_SEARCH;
|
||||
LONG touched = 0;
|
||||
size_t recorded = 0;
|
||||
const unsigned char who = toucher_of(static_cast<uintptr_t>(ep->ContextRecord->Rip));
|
||||
TouchCounts counts{};
|
||||
{
|
||||
Lock lock;
|
||||
recorded = views.size();
|
||||
@@ -417,13 +474,22 @@ LONG CALLBACK lazy_fault(PEXCEPTION_POINTERS ep) {
|
||||
} else if (map_view(view)) {
|
||||
verdict = EXCEPTION_CONTINUE_EXECUTION;
|
||||
touched = InterlockedIncrement(&lazy_mapped);
|
||||
note_touch(view->page, who);
|
||||
}
|
||||
}
|
||||
counts = touch_counts;
|
||||
}
|
||||
if (touched % 4096 == 0 && touched) {
|
||||
char detail[96];
|
||||
std::snprintf(detail, sizeof(detail), "%ld slots mapped on first touch (%zu recorded)", touched, recorded);
|
||||
bx_log_emit(BX_LOG_INFO, "xmem", "lazy_map", detail);
|
||||
char who_detail[256];
|
||||
std::snprintf(who_detail, sizeof(who_detail),
|
||||
"64 KiB pages: written through spans %ld; first touched by the title %ld (%ld of them span-written), "
|
||||
"by d3d12_xs %ld (%ld span-written), by other modules %ld (%ld span-written)",
|
||||
counts.span, counts.title, counts.span_then_title, counts.d3d, counts.span_then_d3d, counts.other,
|
||||
counts.span_then_other);
|
||||
bx_log_emit(BX_LOG_INFO, "xmem", "page_touches", who_detail);
|
||||
}
|
||||
SetLastError(saved_error);
|
||||
return verdict;
|
||||
@@ -588,6 +654,7 @@ PVOID map_unreserved(SIZE_T count, const ULONG_PTR* tags) {
|
||||
SetLastError(error);
|
||||
return nullptr;
|
||||
}
|
||||
for (View* v = pending; v; v = v->next) note_touch(v->page, kBySpan);
|
||||
publish(pending);
|
||||
return base;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
#!/bin/bash
|
||||
# Builds the Milestone 0 probes into $OUT (default: target/unified-memory-probes).
|
||||
# Layout: $OUT/x86_64-windows/bx_host.dll + $OUT/x86_64-unix/bx_host.so (a Wine builtin pair), and
|
||||
# the probe programs in $OUT.
|
||||
set -euo pipefail
|
||||
HERE="$(cd "$(dirname "$0")" && pwd)"
|
||||
OUT="${OUT:-$HERE/../../../target/unified-memory-probes}"
|
||||
mkdir -p "$OUT/x86_64-windows" "$OUT/x86_64-unix"
|
||||
|
||||
# Native half: x86_64 Mach-O (the Wine process runs under Rosetta 2).
|
||||
clang -target x86_64-apple-macos13 -O2 -Wall -Wextra -dynamiclib -fvisibility=hidden \
|
||||
-o "$OUT/x86_64-unix/bx_host.so" "$HERE/bx_host_unix.c"
|
||||
|
||||
# Windows half, then the 16-byte marker Wine's server looks for right after the DOS header.
|
||||
x86_64-w64-mingw32-gcc -O2 -Wall -Wextra -shared -static-libgcc -o "$OUT/x86_64-windows/bx_host.dll" "$HERE/bx_host.c" -lntdll
|
||||
python3 - "$OUT/x86_64-windows/bx_host.dll" <<'EOF'
|
||||
import struct, sys
|
||||
path = sys.argv[1]
|
||||
data = bytearray(open(path, "rb").read())
|
||||
lfanew = struct.unpack_from("<I", data, 0x3C)[0]
|
||||
assert lfanew >= 0x40 + 32, "no room for the builtin marker before the PE header"
|
||||
data[0x40:0x60] = b"Wine builtin DLL".ljust(32, b"\0")
|
||||
open(path, "wb").write(data)
|
||||
EOF
|
||||
|
||||
for probe in "$HERE"/p*.cpp; do
|
||||
name="$(basename "$probe" .cpp)"
|
||||
x86_64-w64-mingw32-g++ -std=c++20 -O2 -Wall -Wextra -static -o "$OUT/$name.exe" "$probe"
|
||||
done
|
||||
file "$OUT/x86_64-unix/bx_host.so" | sed 's/.*: //'
|
||||
ls "$OUT"
|
||||
@@ -0,0 +1,50 @@
|
||||
/* Probe only (Milestone 0): the Windows half of bx_host. Built with mingw and stamped as a Wine
|
||||
* builtin so Wine loads bx_host.so beside it. */
|
||||
#include <windows.h>
|
||||
#include <winternl.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef NTSTATUS (WINAPI *dispatcher_t)(UINT64 handle, unsigned int code, void *args);
|
||||
NTSYSAPI void *NTAPI RtlFindExportedRoutineByName(void *module, const char *name);
|
||||
NTSYSAPI NTSTATUS NTAPI NtQueryVirtualMemory(HANDLE process, const void *address, int info_class, void *buffer, SIZE_T size, SIZE_T *returned);
|
||||
|
||||
static UINT64 g_handle;
|
||||
static dispatcher_t g_dispatch;
|
||||
static NTSTATUS g_init = (NTSTATUS)0xC0000001;
|
||||
|
||||
struct alias_args { uint64_t dst, src, size; int32_t kr; int32_t cur, max; };
|
||||
struct stats_args { uint64_t phys_footprint, resident, wired_pages, page_size, host_page_size; };
|
||||
|
||||
__declspec(dllexport) NTSTATUS bx_host_init(void)
|
||||
{
|
||||
HMODULE self = NULL;
|
||||
GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)(void *)bx_host_init, &self);
|
||||
HMODULE ntdll = GetModuleHandleW(L"ntdll.dll");
|
||||
dispatcher_t *slot = (dispatcher_t *)RtlFindExportedRoutineByName(ntdll, "__wine_unix_call_dispatcher");
|
||||
if (!slot || !*slot) return g_init = (NTSTATUS)0xC0000139; /* entry point not found */
|
||||
g_dispatch = *slot;
|
||||
/* 1000: Wine's MemoryWineUnixFuncs / MemoryWineLoadUnixLib. */
|
||||
g_init = NtQueryVirtualMemory(GetCurrentProcess(), self, 1000,&g_handle, sizeof(g_handle), NULL);
|
||||
return g_init;
|
||||
}
|
||||
|
||||
__declspec(dllexport) int bx_host_alias(void *dst, void *src, uint64_t size, int *cur, int *max)
|
||||
{
|
||||
struct alias_args a = { (uint64_t)(uintptr_t)dst, (uint64_t)(uintptr_t)src, size, -1, 0, 0 };
|
||||
if (g_init) return -2;
|
||||
if (g_dispatch(g_handle, 0, &a)) return -3;
|
||||
if (cur) *cur = a.cur;
|
||||
if (max) *max = a.max;
|
||||
return a.kr;
|
||||
}
|
||||
|
||||
__declspec(dllexport) int bx_host_stats(uint64_t *out5)
|
||||
{
|
||||
struct stats_args s = {0};
|
||||
if (g_init) return -2;
|
||||
if (g_dispatch(g_handle, 1, &s)) return -3;
|
||||
out5[0] = s.phys_footprint; out5[1] = s.resident; out5[2] = s.wired_pages; out5[3] = s.page_size; out5[4] = s.host_page_size;
|
||||
return 0;
|
||||
}
|
||||
|
||||
BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved) { (void)inst; (void)reason; (void)reserved; return TRUE; }
|
||||
@@ -0,0 +1,48 @@
|
||||
/* Probe only (Milestone 0): the native half of bx_host. x86_64 Mach-O, loaded by Wine as the
|
||||
* unix library of bx_host.dll. Uses public macOS interfaces on the process's own memory. */
|
||||
#include <mach/mach.h>
|
||||
#include <mach/mach_vm.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
|
||||
typedef int NTSTATUS;
|
||||
typedef NTSTATUS (*unixlib_entry_t)(void *args);
|
||||
|
||||
struct alias_args { uint64_t dst, src, size; int32_t kr; int32_t cur, max; };
|
||||
struct stats_args { uint64_t phys_footprint, resident, wired_pages, page_size, host_page_size; };
|
||||
|
||||
/* Maps the pages at `src` a second time at `dst` (replacing what is mapped there), shared. */
|
||||
static NTSTATUS bx_alias(void *p)
|
||||
{
|
||||
struct alias_args *a = p;
|
||||
mach_vm_address_t target = a->dst;
|
||||
vm_prot_t cur = 0, max = 0;
|
||||
a->kr = mach_vm_remap(mach_task_self(), &target, a->size, 0, VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE,
|
||||
mach_task_self(), a->src, FALSE, &cur, &max, VM_INHERIT_NONE);
|
||||
a->cur = cur;
|
||||
a->max = max;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static NTSTATUS bx_stats(void *p)
|
||||
{
|
||||
struct stats_args *s = p;
|
||||
task_vm_info_data_t info;
|
||||
mach_msg_type_number_t count = TASK_VM_INFO_COUNT;
|
||||
s->phys_footprint = s->resident = 0;
|
||||
if (task_info(mach_task_self(), TASK_VM_INFO, (task_info_t)&info, &count) == KERN_SUCCESS) {
|
||||
s->phys_footprint = info.phys_footprint;
|
||||
s->resident = info.resident_size;
|
||||
}
|
||||
vm_statistics64_data_t vm;
|
||||
mach_msg_type_number_t n = HOST_VM_INFO64_COUNT;
|
||||
s->wired_pages = 0;
|
||||
if (host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info64_t)&vm, &n) == KERN_SUCCESS) s->wired_pages = vm.wire_count;
|
||||
s->page_size = (uint64_t)getpagesize();
|
||||
vm_size_t hp = 0;
|
||||
host_page_size(mach_host_self(), &hp);
|
||||
s->host_page_size = hp;
|
||||
return 0;
|
||||
}
|
||||
|
||||
__attribute__((visibility("default"))) const unixlib_entry_t __wine_unix_call_funcs[] = { bx_alias, bx_stats };
|
||||
@@ -0,0 +1,237 @@
|
||||
// Probe P1 (Milestone 0): can pages of a D3D12 heap be mapped a second time at an address inside a
|
||||
// Wine section view, so that CPU code using that address and the GPU share one memory?
|
||||
// Standalone and synthetic: no title, no GDK material. Prints one line per check.
|
||||
#include <windows.h>
|
||||
#include <d3d12.h>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
static int g_fail = 0;
|
||||
#define CHECK(ok, ...) \
|
||||
do { \
|
||||
std::printf("%s: ", (ok) ? "PASS" : "FAIL"); \
|
||||
std::printf(__VA_ARGS__); \
|
||||
std::printf("\n"); \
|
||||
if (!(ok)) ++g_fail; \
|
||||
std::fflush(stdout); \
|
||||
} while (0)
|
||||
|
||||
using InitFn = LONG (*)(void);
|
||||
using AliasFn = int (*)(void*, void*, uint64_t, int*, int*);
|
||||
using StatsFn = int (*)(uint64_t*);
|
||||
using VirtualAlloc2Fn = PVOID(WINAPI*)(HANDLE, PVOID, SIZE_T, ULONG, ULONG, MEM_EXTENDED_PARAMETER*, ULONG);
|
||||
using MapViewOfFile3Fn = PVOID(WINAPI*)(HANDLE, HANDLE, PVOID, ULONG64, SIZE_T, ULONG, ULONG, MEM_EXTENDED_PARAMETER*, ULONG);
|
||||
using UnmapViewOfFile2Fn = BOOL(WINAPI*)(HANDLE, PVOID, ULONG);
|
||||
|
||||
static ID3D12Device* g_dev;
|
||||
static ID3D12CommandQueue* g_queue;
|
||||
static ID3D12CommandAllocator* g_alloc;
|
||||
static ID3D12GraphicsCommandList* g_list;
|
||||
static ID3D12Fence* g_fence;
|
||||
static UINT64 g_value;
|
||||
static HANDLE g_event;
|
||||
|
||||
static bool submit() {
|
||||
g_list->Close();
|
||||
ID3D12CommandList* lists[] = {g_list};
|
||||
g_queue->ExecuteCommandLists(1, lists);
|
||||
g_queue->Signal(g_fence, ++g_value);
|
||||
g_fence->SetEventOnCompletion(g_value, g_event);
|
||||
const bool done = WaitForSingleObject(g_event, 20000) == WAIT_OBJECT_0;
|
||||
g_alloc->Reset();
|
||||
g_list->Reset(g_alloc, nullptr);
|
||||
return done;
|
||||
}
|
||||
|
||||
static D3D12_RESOURCE_DESC buffer_desc(UINT64 size) {
|
||||
D3D12_RESOURCE_DESC d{};
|
||||
d.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
|
||||
d.Width = size;
|
||||
d.Height = 1;
|
||||
d.DepthOrArraySize = 1;
|
||||
d.MipLevels = 1;
|
||||
d.SampleDesc.Count = 1;
|
||||
d.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
|
||||
return d;
|
||||
}
|
||||
|
||||
static ID3D12Resource* committed(D3D12_HEAP_TYPE type, UINT64 size, D3D12_RESOURCE_STATES state) {
|
||||
D3D12_HEAP_PROPERTIES hp{};
|
||||
hp.Type = type;
|
||||
const D3D12_RESOURCE_DESC d = buffer_desc(size);
|
||||
ID3D12Resource* r = nullptr;
|
||||
g_dev->CreateCommittedResource(&hp, D3D12_HEAP_FLAG_NONE, &d, state, nullptr, __uuidof(ID3D12Resource), reinterpret_cast<void**>(&r));
|
||||
return r;
|
||||
}
|
||||
|
||||
static bool readable(const void* p) {
|
||||
uint8_t byte = 0;
|
||||
SIZE_T got = 0;
|
||||
return ReadProcessMemory(GetCurrentProcess(), p, &byte, 1, &got) && got == 1;
|
||||
}
|
||||
|
||||
int main() {
|
||||
constexpr UINT64 kHeap = 64ull << 20, kPage = 64 * 1024;
|
||||
// --- the native helper ---
|
||||
HMODULE host = LoadLibraryA("bx_host.dll");
|
||||
CHECK(host != nullptr, "bx_host.dll loaded (error %lu)", host ? 0ul : GetLastError());
|
||||
if (!host) return 1;
|
||||
auto init = reinterpret_cast<InitFn>(reinterpret_cast<void*>(GetProcAddress(host, "bx_host_init")));
|
||||
auto alias = reinterpret_cast<AliasFn>(reinterpret_cast<void*>(GetProcAddress(host, "bx_host_alias")));
|
||||
auto stats = reinterpret_cast<StatsFn>(reinterpret_cast<void*>(GetProcAddress(host, "bx_host_stats")));
|
||||
const LONG st = init ? init() : -1;
|
||||
CHECK(st == 0, "native half reached through Wine's unix call (status 0x%08lx)", static_cast<unsigned long>(st));
|
||||
if (st != 0) return 1;
|
||||
uint64_t s0[5] = {}, s1[5] = {}, s2[5] = {};
|
||||
stats(s0);
|
||||
std::printf("INFO: process page size %llu, host page size %llu, footprint %llu MiB\n", (unsigned long long)s0[3],
|
||||
(unsigned long long)s0[4], (unsigned long long)(s0[0] >> 20));
|
||||
|
||||
// --- a CPU-visible D3D12 heap with one buffer over all of it ---
|
||||
HMODULE d3d12 = LoadLibraryA("d3d12.dll");
|
||||
auto create = reinterpret_cast<PFN_D3D12_CREATE_DEVICE>(reinterpret_cast<void*>(GetProcAddress(d3d12, "D3D12CreateDevice")));
|
||||
if (!create || FAILED(create(nullptr, D3D_FEATURE_LEVEL_12_0, __uuidof(ID3D12Device), reinterpret_cast<void**>(&g_dev)))) {
|
||||
CHECK(false, "D3D12 device");
|
||||
return 1;
|
||||
}
|
||||
D3D12_COMMAND_QUEUE_DESC qd{};
|
||||
qd.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
|
||||
g_dev->CreateCommandQueue(&qd, __uuidof(ID3D12CommandQueue), reinterpret_cast<void**>(&g_queue));
|
||||
g_dev->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, __uuidof(ID3D12CommandAllocator), reinterpret_cast<void**>(&g_alloc));
|
||||
g_dev->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, g_alloc, nullptr, __uuidof(ID3D12GraphicsCommandList),
|
||||
reinterpret_cast<void**>(&g_list));
|
||||
g_dev->CreateFence(0, D3D12_FENCE_FLAG_NONE, __uuidof(ID3D12Fence), reinterpret_cast<void**>(&g_fence));
|
||||
g_event = CreateEventW(nullptr, FALSE, FALSE, nullptr);
|
||||
|
||||
D3D12_HEAP_DESC hd{};
|
||||
hd.SizeInBytes = kHeap;
|
||||
hd.Properties.Type = D3D12_HEAP_TYPE_CUSTOM;
|
||||
hd.Properties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_WRITE_BACK;
|
||||
hd.Properties.MemoryPoolPreference = D3D12_MEMORY_POOL_L0;
|
||||
ID3D12Heap* heap = nullptr;
|
||||
HRESULT hr = g_dev->CreateHeap(&hd, __uuidof(ID3D12Heap), reinterpret_cast<void**>(&heap));
|
||||
CHECK(SUCCEEDED(hr) && heap, "CPU-visible heap of 64 MiB (hr 0x%08lx)", static_cast<unsigned long>(hr));
|
||||
ID3D12Resource* whole = nullptr;
|
||||
const D3D12_RESOURCE_DESC wd = buffer_desc(kHeap);
|
||||
hr = heap ? g_dev->CreatePlacedResource(heap, 0, &wd, D3D12_RESOURCE_STATE_COMMON, nullptr, __uuidof(ID3D12Resource),
|
||||
reinterpret_cast<void**>(&whole))
|
||||
: E_FAIL;
|
||||
uint8_t* H = nullptr;
|
||||
if (whole) whole->Map(0, nullptr, reinterpret_cast<void**>(&H));
|
||||
CHECK(H != nullptr, "heap-wide buffer mapped at %p (hr 0x%08lx)", static_cast<void*>(H), static_cast<unsigned long>(hr));
|
||||
if (!H) return 1;
|
||||
CHECK((reinterpret_cast<uintptr_t>(H) & 0x3FFF) == 0, "heap base is 16 KiB aligned (low bits 0x%llx)",
|
||||
(unsigned long long)(reinterpret_cast<uintptr_t>(H) & 0xFFFF));
|
||||
stats(s1);
|
||||
|
||||
// --- a title-style slot: placeholder, section view (reserved, then committed), as xmem makes them ---
|
||||
HMODULE kb = GetModuleHandleW(L"kernelbase.dll");
|
||||
auto va2 = reinterpret_cast<VirtualAlloc2Fn>(reinterpret_cast<void*>(GetProcAddress(kb, "VirtualAlloc2")));
|
||||
auto mv3 = reinterpret_cast<MapViewOfFile3Fn>(reinterpret_cast<void*>(GetProcAddress(kb, "MapViewOfFile3")));
|
||||
auto um2 = reinterpret_cast<UnmapViewOfFile2Fn>(reinterpret_cast<void*>(GetProcAddress(kb, "UnmapViewOfFile2")));
|
||||
const HANDLE section = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE | SEC_RESERVE, 0, 1 << 20, nullptr);
|
||||
auto* base = static_cast<uint8_t*>(va2(GetCurrentProcess(), nullptr, 1 << 20, MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, PAGE_NOACCESS, nullptr, 0));
|
||||
CHECK(section && base, "section and 1 MiB placeholder at %p", static_cast<void*>(base));
|
||||
uint8_t* V0 = base;
|
||||
uint8_t* V1 = base + kPage;
|
||||
VirtualFree(V0, kPage, MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER);
|
||||
VirtualFree(V1, kPage, MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER);
|
||||
bool ok = mv3(section, GetCurrentProcess(), V0, 0, kPage, MEM_REPLACE_PLACEHOLDER, PAGE_READWRITE, nullptr, 0) == V0 &&
|
||||
VirtualAlloc(V0, kPage, MEM_COMMIT, PAGE_READWRITE) == V0;
|
||||
ok = ok && mv3(section, GetCurrentProcess(), V1, kPage, kPage, MEM_REPLACE_PLACEHOLDER, PAGE_READWRITE, nullptr, 0) == V1 &&
|
||||
VirtualAlloc(V1, kPage, MEM_COMMIT, PAGE_READWRITE) == V1;
|
||||
CHECK(ok, "two 64 KiB section views mapped over the placeholder");
|
||||
if (!ok) return 1;
|
||||
std::memcpy(V0, "SECTION0", 8);
|
||||
|
||||
// --- the overlay ---
|
||||
uint8_t* P = H + 3 * kPage; // heap page 3
|
||||
std::memcpy(P, "HEAPPAGE", 8);
|
||||
int cur = 0, max = 0;
|
||||
int kr = alias(V0, P, kPage, &cur, &max);
|
||||
CHECK(kr == 0, "mach_vm_remap of a heap page over the view (kr %d, cur prot %d, max prot %d)", kr, cur, max);
|
||||
if (kr != 0) return 1;
|
||||
CHECK(std::memcmp(V0, "HEAPPAGE", 8) == 0, "the view now shows the heap page's bytes");
|
||||
std::memcpy(V0 + 16, "VIAVIEW!", 8);
|
||||
CHECK(std::memcmp(P + 16, "VIAVIEW!", 8) == 0, "a CPU write through the view lands in the heap page");
|
||||
|
||||
// --- the GPU sees CPU writes made through the view, and the view sees GPU writes ---
|
||||
ID3D12Resource* readback = committed(D3D12_HEAP_TYPE_READBACK, 4096, D3D12_RESOURCE_STATE_COPY_DEST);
|
||||
ID3D12Resource* upload = committed(D3D12_HEAP_TYPE_UPLOAD, 4096, D3D12_RESOURCE_STATE_GENERIC_READ);
|
||||
uint32_t* rb = nullptr;
|
||||
uint32_t* up = nullptr;
|
||||
readback->Map(0, nullptr, reinterpret_cast<void**>(&rb));
|
||||
upload->Map(0, nullptr, reinterpret_cast<void**>(&up));
|
||||
bool gpu_reads = true, gpu_writes = true;
|
||||
for (uint32_t round = 0; round < 8; ++round) {
|
||||
const uint32_t v = 0xC0DE0000u + round;
|
||||
std::memcpy(V0 + 256, &v, 4);
|
||||
g_list->CopyBufferRegion(readback, 0, whole, 3 * kPage + 256, 4);
|
||||
gpu_reads = submit() && rb[0] == v && gpu_reads;
|
||||
up[0] = 0xFEED0000u + round;
|
||||
g_list->CopyBufferRegion(whole, 3 * kPage + 512, upload, 0, 4);
|
||||
uint32_t got = 0;
|
||||
const bool done = submit();
|
||||
std::memcpy(&got, V0 + 512, 4);
|
||||
gpu_writes = done && got == 0xFEED0000u + round && gpu_writes;
|
||||
}
|
||||
CHECK(gpu_reads, "GPU reads what the CPU wrote through the view (8 rounds, no Map/Unmap)");
|
||||
CHECK(gpu_writes, "the view shows what the GPU wrote (8 rounds)");
|
||||
|
||||
// --- Windows calls on the overlaid view ---
|
||||
wchar_t dir[MAX_PATH], path[MAX_PATH];
|
||||
GetTempPathW(MAX_PATH, dir);
|
||||
GetTempFileNameW(dir, L"bxp", 0, path);
|
||||
HANDLE f = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, nullptr);
|
||||
char pattern[4096];
|
||||
for (int i = 0; i < 4096; ++i) pattern[i] = static_cast<char>(i * 7 + 1);
|
||||
DWORD n = 0;
|
||||
WriteFile(f, pattern, sizeof(pattern), &n, nullptr);
|
||||
SetFilePointer(f, 0, nullptr, FILE_BEGIN);
|
||||
const BOOL rd = ReadFile(f, V0 + 8192, sizeof(pattern), &n, nullptr);
|
||||
CHECK(rd && n == sizeof(pattern) && std::memcmp(P + 8192, pattern, sizeof(pattern)) == 0,
|
||||
"ReadFile into the view succeeds and the bytes are in the heap page (ok %d, %lu bytes, error %lu)", rd, n, rd ? 0ul : GetLastError());
|
||||
CloseHandle(f);
|
||||
DWORD old = 0;
|
||||
const BOOL p1 = VirtualProtect(V0, kPage, PAGE_NOACCESS, &old);
|
||||
const bool blocked = !readable(V0);
|
||||
const BOOL p2 = VirtualProtect(V0, kPage, PAGE_READWRITE, &old);
|
||||
CHECK(p1 && blocked && p2 && std::memcmp(V0, "HEAPPAGE", 8) == 0, "VirtualProtect: no access, then read-write again with the heap bytes intact");
|
||||
MEMORY_BASIC_INFORMATION mbi{};
|
||||
VirtualQuery(V0, &mbi, sizeof(mbi));
|
||||
CHECK(mbi.State == MEM_COMMIT && mbi.Type == MEM_MAPPED, "VirtualQuery still reports a committed mapped view (state 0x%lx type 0x%lx)", mbi.State, mbi.Type);
|
||||
|
||||
// --- one heap page at two addresses ---
|
||||
kr = alias(V1, P, kPage, &cur, &max);
|
||||
std::memcpy(V0 + 1024, "TWOVIEWS", 8);
|
||||
CHECK(kr == 0 && std::memcmp(V1 + 1024, "TWOVIEWS", 8) == 0, "a second view of the same heap page shows writes made through the first");
|
||||
|
||||
// --- many overlays ---
|
||||
LARGE_INTEGER freq, t0, t1;
|
||||
QueryPerformanceFrequency(&freq);
|
||||
QueryPerformanceCounter(&t0);
|
||||
int bad = 0;
|
||||
for (int i = 0; i < 10000; ++i) bad += alias(V1, H + (static_cast<UINT64>(i) % 1000) * kPage, kPage, nullptr, nullptr) != 0;
|
||||
QueryPerformanceCounter(&t1);
|
||||
const double us = 1e6 * static_cast<double>(t1.QuadPart - t0.QuadPart) / static_cast<double>(freq.QuadPart) / 10000.0;
|
||||
CHECK(bad == 0, "10,000 overlays of one slot onto different heap pages: %.1f microseconds each", us);
|
||||
|
||||
// --- unmapping gives the slot back and leaves the heap alone ---
|
||||
const BOOL un = um2(GetCurrentProcess(), V0, MEM_PRESERVE_PLACEHOLDER);
|
||||
CHECK(un && !readable(V0) && std::memcmp(P, "HEAPPAGE", 8) == 0, "unmapping the view restores the placeholder; the heap page keeps its bytes");
|
||||
ok = mv3(section, GetCurrentProcess(), V0, 0, kPage, MEM_REPLACE_PLACEHOLDER, PAGE_READWRITE, nullptr, 0) == V0 &&
|
||||
VirtualAlloc(V0, kPage, MEM_COMMIT, PAGE_READWRITE) == V0;
|
||||
CHECK(ok && std::memcmp(V0, "SECTION0", 8) == 0, "mapping the section there again shows the section's own bytes");
|
||||
|
||||
// --- what it costs ---
|
||||
for (UINT64 i = 0; i < kHeap; i += 4096) H[i] = 1; // touch the whole heap
|
||||
g_list->CopyBufferRegion(readback, 0, whole, 0, 4);
|
||||
submit();
|
||||
stats(s2);
|
||||
std::printf("INFO: footprint MiB: start %llu, heap created %llu, heap touched and used %llu; system wired pages: %llu -> %llu -> %llu\n",
|
||||
(unsigned long long)(s0[0] >> 20), (unsigned long long)(s1[0] >> 20), (unsigned long long)(s2[0] >> 20),
|
||||
(unsigned long long)s0[2], (unsigned long long)s1[2], (unsigned long long)s2[2]);
|
||||
std::printf("RESULT: %d failed\n", g_fail);
|
||||
return g_fail ? 1 : 0;
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
// Probe P2 (Milestone 0): how D3DMetal heaps behave and what they cost.
|
||||
// Standalone and synthetic. Footprint figures are the process's phys_footprint, read through bx_host.
|
||||
#include <windows.h>
|
||||
#include <d3d12.h>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
static int g_fail = 0;
|
||||
#define CHECK(ok, ...) \
|
||||
do { \
|
||||
std::printf("%s: ", (ok) ? "PASS" : "FAIL"); \
|
||||
std::printf(__VA_ARGS__); \
|
||||
std::printf("\n"); \
|
||||
if (!(ok)) ++g_fail; \
|
||||
std::fflush(stdout); \
|
||||
} while (0)
|
||||
|
||||
using InitFn = LONG (*)(void);
|
||||
using StatsFn = int (*)(uint64_t*);
|
||||
static StatsFn g_stats;
|
||||
static ID3D12Device* g_dev;
|
||||
static ID3D12CommandQueue* g_queue;
|
||||
static ID3D12CommandAllocator* g_alloc;
|
||||
static ID3D12GraphicsCommandList* g_list;
|
||||
static ID3D12Fence* g_fence;
|
||||
static UINT64 g_value;
|
||||
static HANDLE g_event;
|
||||
|
||||
static double footprint_mib() {
|
||||
uint64_t s[5] = {};
|
||||
g_stats(s);
|
||||
return static_cast<double>(s[0]) / (1024.0 * 1024.0);
|
||||
}
|
||||
|
||||
static bool submit() {
|
||||
g_list->Close();
|
||||
ID3D12CommandList* lists[] = {g_list};
|
||||
g_queue->ExecuteCommandLists(1, lists);
|
||||
g_queue->Signal(g_fence, ++g_value);
|
||||
g_fence->SetEventOnCompletion(g_value, g_event);
|
||||
const bool done = WaitForSingleObject(g_event, 30000) == WAIT_OBJECT_0;
|
||||
g_alloc->Reset();
|
||||
g_list->Reset(g_alloc, nullptr);
|
||||
return done;
|
||||
}
|
||||
|
||||
static D3D12_RESOURCE_DESC buffer_desc(UINT64 size) {
|
||||
D3D12_RESOURCE_DESC d{};
|
||||
d.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
|
||||
d.Width = size;
|
||||
d.Height = 1;
|
||||
d.DepthOrArraySize = 1;
|
||||
d.MipLevels = 1;
|
||||
d.SampleDesc.Count = 1;
|
||||
d.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
|
||||
return d;
|
||||
}
|
||||
|
||||
static D3D12_HEAP_PROPERTIES cpu_visible() {
|
||||
D3D12_HEAP_PROPERTIES hp{};
|
||||
hp.Type = D3D12_HEAP_TYPE_CUSTOM;
|
||||
hp.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_WRITE_BACK;
|
||||
hp.MemoryPoolPreference = D3D12_MEMORY_POOL_L0;
|
||||
return hp;
|
||||
}
|
||||
|
||||
static ID3D12Heap* heap_of(UINT64 size) {
|
||||
D3D12_HEAP_DESC hd{};
|
||||
hd.SizeInBytes = size;
|
||||
hd.Properties = cpu_visible();
|
||||
ID3D12Heap* heap = nullptr;
|
||||
g_dev->CreateHeap(&hd, __uuidof(ID3D12Heap), reinterpret_cast<void**>(&heap));
|
||||
return heap;
|
||||
}
|
||||
|
||||
static ID3D12Resource* placed(ID3D12Heap* heap, UINT64 offset, UINT64 size, HRESULT* hr) {
|
||||
const D3D12_RESOURCE_DESC d = buffer_desc(size);
|
||||
ID3D12Resource* r = nullptr;
|
||||
*hr = g_dev->CreatePlacedResource(heap, offset, &d, D3D12_RESOURCE_STATE_COMMON, nullptr, __uuidof(ID3D12Resource), reinterpret_cast<void**>(&r));
|
||||
return r;
|
||||
}
|
||||
|
||||
int main() {
|
||||
HMODULE host = LoadLibraryA("bx_host.dll");
|
||||
auto init = host ? reinterpret_cast<InitFn>(reinterpret_cast<void*>(GetProcAddress(host, "bx_host_init"))) : nullptr;
|
||||
g_stats = host ? reinterpret_cast<StatsFn>(reinterpret_cast<void*>(GetProcAddress(host, "bx_host_stats"))) : nullptr;
|
||||
if (!init || init() != 0 || !g_stats) {
|
||||
CHECK(false, "bx_host");
|
||||
return 1;
|
||||
}
|
||||
HMODULE d3d12 = LoadLibraryA("d3d12.dll");
|
||||
auto create = reinterpret_cast<PFN_D3D12_CREATE_DEVICE>(reinterpret_cast<void*>(GetProcAddress(d3d12, "D3D12CreateDevice")));
|
||||
if (!create || FAILED(create(nullptr, D3D_FEATURE_LEVEL_12_0, __uuidof(ID3D12Device), reinterpret_cast<void**>(&g_dev)))) {
|
||||
CHECK(false, "D3D12 device");
|
||||
return 1;
|
||||
}
|
||||
D3D12_COMMAND_QUEUE_DESC qd{};
|
||||
qd.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
|
||||
g_dev->CreateCommandQueue(&qd, __uuidof(ID3D12CommandQueue), reinterpret_cast<void**>(&g_queue));
|
||||
g_dev->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, __uuidof(ID3D12CommandAllocator), reinterpret_cast<void**>(&g_alloc));
|
||||
g_dev->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, g_alloc, nullptr, __uuidof(ID3D12GraphicsCommandList),
|
||||
reinterpret_cast<void**>(&g_list));
|
||||
g_dev->CreateFence(0, D3D12_FENCE_FLAG_NONE, __uuidof(ID3D12Fence), reinterpret_cast<void**>(&g_fence));
|
||||
g_event = CreateEventW(nullptr, FALSE, FALSE, nullptr);
|
||||
|
||||
// A readback buffer placed in a small CPU-visible heap of its own, so the cost figures below are
|
||||
// not mixed with D3DMetal's pools for committed resources.
|
||||
ID3D12Heap* rb_heap = heap_of(1 << 20);
|
||||
HRESULT hr = S_OK;
|
||||
ID3D12Resource* readback = placed(rb_heap, 0, 1 << 20, &hr);
|
||||
uint8_t* rb = nullptr;
|
||||
if (readback) readback->Map(0, nullptr, reinterpret_cast<void**>(&rb));
|
||||
CHECK(rb != nullptr, "readback buffer placed in its own CPU-visible heap (hr 0x%08lx)", static_cast<unsigned long>(hr));
|
||||
if (!rb) return 1;
|
||||
g_list->CopyBufferRegion(readback, 4096, readback, 0, 16); // first GPU use of the device
|
||||
submit();
|
||||
const double f_base = footprint_mib();
|
||||
|
||||
// --- A. residency of a heap ---
|
||||
constexpr UINT64 kHeap = 64ull << 20, kPage = 64 * 1024;
|
||||
ID3D12Heap* heap = heap_of(kHeap);
|
||||
const double f_created = footprint_mib();
|
||||
ID3D12Resource* whole = heap ? placed(heap, 0, kHeap, &hr) : nullptr;
|
||||
uint8_t* H = nullptr;
|
||||
if (whole) whole->Map(0, nullptr, reinterpret_cast<void**>(&H));
|
||||
CHECK(H != nullptr, "64 MiB heap with a heap-wide buffer");
|
||||
if (!H) return 1;
|
||||
const double f_mapped = footprint_mib();
|
||||
std::memcpy(H + 5 * kPage, "ONEPAGE!", 8);
|
||||
g_list->CopyBufferRegion(readback, 0, whole, 5 * kPage, 8);
|
||||
const bool used = submit() && std::memcmp(rb, "ONEPAGE!", 8) == 0;
|
||||
const double f_used = footprint_mib();
|
||||
CHECK(used, "one page of the heap written and read by the GPU");
|
||||
std::printf("INFO: footprint MiB: baseline %.0f; heap created %+.0f; buffer mapped %+.0f; one page used by the GPU %+.0f\n", f_base,
|
||||
f_created - f_base, f_mapped - f_base, f_used - f_base);
|
||||
for (UINT64 i = 0; i < kHeap; i += 4096) H[i] = 1;
|
||||
g_list->CopyBufferRegion(readback, 0, whole, 7 * kPage, 8);
|
||||
submit();
|
||||
const double f_touched = footprint_mib();
|
||||
std::printf("INFO: after touching every page of the heap: %+.0f MiB over baseline\n", f_touched - f_base);
|
||||
|
||||
// --- B. buffers placed at fine offsets, overlapping the heap-wide buffer ---
|
||||
const UINT64 offsets[] = {kPage * 9, kPage * 9 + 4096, kPage * 9 + 256, kPage * 9 + 16, kPage * 9 + 4};
|
||||
for (UINT64 off : offsets) {
|
||||
char text[8];
|
||||
std::snprintf(text, sizeof(text), "o%05x", static_cast<unsigned>(off & 0xFFFFF));
|
||||
std::memcpy(H + off, text, 8);
|
||||
ID3D12Resource* small = placed(heap, off, 4096, &hr);
|
||||
bool same = false;
|
||||
if (small) {
|
||||
std::memset(rb, 0, 16);
|
||||
g_list->CopyBufferRegion(readback, 0, small, 0, 8);
|
||||
same = submit() && std::memcmp(rb, text, 8) == 0;
|
||||
small->Release();
|
||||
}
|
||||
std::printf("%s: buffer placed at heap offset 64 KiB*9 + %llu: hr 0x%08lx, its first bytes are the heap's bytes there: %s\n",
|
||||
small && same ? "PASS" : "INFO", (unsigned long long)(off - kPage * 9), static_cast<unsigned long>(hr),
|
||||
small ? (same ? "yes" : "NO") : "not created");
|
||||
}
|
||||
std::fflush(stdout);
|
||||
|
||||
// --- C. releasing the heap ---
|
||||
whole->Unmap(0, nullptr);
|
||||
whole->Release();
|
||||
heap->Release();
|
||||
g_list->CopyBufferRegion(readback, 4096, readback, 0, 16);
|
||||
submit();
|
||||
Sleep(1500);
|
||||
const double f_released = footprint_mib();
|
||||
std::printf("INFO: after releasing the heap: %+.0f MiB over baseline\n", f_released - f_base);
|
||||
|
||||
// --- C2. how the footprint moves as more heaps are made and used ---
|
||||
{
|
||||
std::vector<ID3D12Heap*> heaps;
|
||||
std::vector<ID3D12Resource*> buffers;
|
||||
const double f0 = footprint_mib();
|
||||
std::printf("INFO: footprint over this point as 64 MiB heaps are made, each touched in full and used once by the GPU:");
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
ID3D12Heap* h = heap_of(kHeap);
|
||||
ID3D12Resource* b = h ? placed(h, 0, kHeap, &hr) : nullptr;
|
||||
uint8_t* p = nullptr;
|
||||
if (b) b->Map(0, nullptr, reinterpret_cast<void**>(&p));
|
||||
if (!p) break;
|
||||
for (UINT64 k = 0; k < kHeap; k += 4096) p[k] = 1;
|
||||
g_list->CopyBufferRegion(readback, 0, b, 0, 8);
|
||||
submit();
|
||||
heaps.push_back(h);
|
||||
buffers.push_back(b);
|
||||
std::printf(" %+.0f", footprint_mib() - f0);
|
||||
}
|
||||
std::printf(" MiB\n");
|
||||
for (size_t i = 0; i < heaps.size(); ++i) {
|
||||
buffers[i]->Release();
|
||||
heaps[i]->Release();
|
||||
}
|
||||
g_list->CopyBufferRegion(readback, 4096, readback, 0, 16);
|
||||
submit();
|
||||
Sleep(1500);
|
||||
std::printf("INFO: after releasing those heaps: %+.0f MiB\n", footprint_mib() - f0);
|
||||
}
|
||||
|
||||
// --- D. what many small committed buffers cost ---
|
||||
constexpr int kCount = 50000;
|
||||
std::vector<ID3D12Resource*> small(kCount, nullptr);
|
||||
const D3D12_HEAP_PROPERTIES hp = cpu_visible();
|
||||
const D3D12_RESOURCE_DESC sd = buffer_desc(240);
|
||||
int made = 0;
|
||||
const double f_before = footprint_mib();
|
||||
for (int i = 0; i < kCount; ++i) {
|
||||
if (FAILED(g_dev->CreateCommittedResource(&hp, D3D12_HEAP_FLAG_NONE, &sd, D3D12_RESOURCE_STATE_COMMON, nullptr, __uuidof(ID3D12Resource),
|
||||
reinterpret_cast<void**>(&small[i])))) break;
|
||||
void* p = nullptr;
|
||||
if (SUCCEEDED(small[i]->Map(0, nullptr, &p)) && p) std::memset(p, 0x5A, 240);
|
||||
++made;
|
||||
}
|
||||
if (made) {
|
||||
g_list->CopyBufferRegion(readback, 0, small[made - 1], 0, 16);
|
||||
submit();
|
||||
}
|
||||
const double f_small = footprint_mib();
|
||||
std::printf("INFO: %d committed 240-byte buffers (mapped and written): %+.0f MiB, %.1f KiB each\n", made, f_small - f_before,
|
||||
made ? (f_small - f_before) * 1024.0 / made : 0.0);
|
||||
for (int i = 0; i < made; ++i) small[i]->Release();
|
||||
g_list->CopyBufferRegion(readback, 4096, readback, 0, 16);
|
||||
submit();
|
||||
Sleep(1500);
|
||||
std::printf("INFO: after releasing them: %+.0f MiB over the figure before they were made\n", footprint_mib() - f_before);
|
||||
std::printf("RESULT: %d failed\n", g_fail);
|
||||
return g_fail ? 1 : 0;
|
||||
}
|
||||
@@ -0,0 +1,411 @@
|
||||
// Probe P3 (Milestone 0): can a reserved texture take its tiles from chosen offsets of a
|
||||
// CPU-visible heap, can two textures share those tiles, and are reserved buffers available?
|
||||
// Standalone and synthetic.
|
||||
#include <windows.h>
|
||||
#include <d3d12.h>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
static int g_fail = 0;
|
||||
#define CHECK(ok, ...) \
|
||||
do { \
|
||||
std::printf("%s: ", (ok) ? "PASS" : "FAIL"); \
|
||||
std::printf(__VA_ARGS__); \
|
||||
std::printf("\n"); \
|
||||
if (!(ok)) ++g_fail; \
|
||||
std::fflush(stdout); \
|
||||
} while (0)
|
||||
|
||||
static ID3D12Device* g_dev;
|
||||
static ID3D12CommandQueue* g_queue;
|
||||
static ID3D12CommandAllocator* g_alloc;
|
||||
static ID3D12GraphicsCommandList* g_list;
|
||||
static ID3D12Fence* g_fence;
|
||||
static UINT64 g_value;
|
||||
static HANDLE g_event;
|
||||
|
||||
static bool wait_queue() {
|
||||
g_queue->Signal(g_fence, ++g_value);
|
||||
g_fence->SetEventOnCompletion(g_value, g_event);
|
||||
return WaitForSingleObject(g_event, 30000) == WAIT_OBJECT_0;
|
||||
}
|
||||
|
||||
static bool submit() {
|
||||
g_list->Close();
|
||||
ID3D12CommandList* lists[] = {g_list};
|
||||
g_queue->ExecuteCommandLists(1, lists);
|
||||
const bool done = wait_queue();
|
||||
g_alloc->Reset();
|
||||
g_list->Reset(g_alloc, nullptr);
|
||||
return done;
|
||||
}
|
||||
|
||||
static D3D12_RESOURCE_DESC buffer_desc(UINT64 size) {
|
||||
D3D12_RESOURCE_DESC d{};
|
||||
d.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
|
||||
d.Width = size;
|
||||
d.Height = 1;
|
||||
d.DepthOrArraySize = 1;
|
||||
d.MipLevels = 1;
|
||||
d.SampleDesc.Count = 1;
|
||||
d.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
|
||||
return d;
|
||||
}
|
||||
|
||||
static ID3D12Resource* committed(D3D12_HEAP_TYPE type, UINT64 size, D3D12_RESOURCE_STATES state) {
|
||||
D3D12_HEAP_PROPERTIES hp{};
|
||||
hp.Type = type;
|
||||
const D3D12_RESOURCE_DESC d = buffer_desc(size);
|
||||
ID3D12Resource* r = nullptr;
|
||||
g_dev->CreateCommittedResource(&hp, D3D12_HEAP_FLAG_NONE, &d, state, nullptr, __uuidof(ID3D12Resource), reinterpret_cast<void**>(&r));
|
||||
return r;
|
||||
}
|
||||
|
||||
constexpr UINT kSize = 512, kPitch = kSize * 4;
|
||||
constexpr UINT64 kTile = 64 * 1024, kHeap = 64ull << 20;
|
||||
|
||||
static D3D12_RESOURCE_DESC texture_desc(DXGI_FORMAT format, UINT size, UINT16 mips) {
|
||||
D3D12_RESOURCE_DESC d{};
|
||||
d.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
|
||||
d.Width = size;
|
||||
d.Height = size;
|
||||
d.DepthOrArraySize = 1;
|
||||
d.MipLevels = mips;
|
||||
d.Format = format;
|
||||
d.SampleDesc.Count = 1;
|
||||
d.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
|
||||
return d;
|
||||
}
|
||||
|
||||
static ID3D12Resource* reserved(const D3D12_RESOURCE_DESC& d, HRESULT* hr) {
|
||||
ID3D12Resource* r = nullptr;
|
||||
*hr = g_dev->CreateReservedResource(&d, D3D12_RESOURCE_STATE_COMMON, nullptr, __uuidof(ID3D12Resource), reinterpret_cast<void**>(&r));
|
||||
return r;
|
||||
}
|
||||
|
||||
// Maps tile i of mip 0 (16 tiles of 128x128 for a 512x512 RGBA8 texture) to heap tile first + step * i.
|
||||
static void map_tiles(ID3D12Resource* tex, ID3D12Heap* heap, UINT first, int step) {
|
||||
for (UINT i = 0; i < 16; ++i) {
|
||||
D3D12_TILED_RESOURCE_COORDINATE at{};
|
||||
at.X = i % 4;
|
||||
at.Y = i / 4;
|
||||
D3D12_TILE_REGION_SIZE size{};
|
||||
size.NumTiles = 1;
|
||||
D3D12_TILE_RANGE_FLAGS flags = D3D12_TILE_RANGE_FLAG_NONE;
|
||||
UINT offset = static_cast<UINT>(static_cast<int>(first) + step * static_cast<int>(i));
|
||||
UINT count = 1;
|
||||
g_queue->UpdateTileMappings(tex, 1, &at, &size, heap, 1, &flags, &offset, &count, D3D12_TILE_MAPPING_FLAG_NONE);
|
||||
}
|
||||
}
|
||||
|
||||
static uint32_t texel(UINT x, UINT y) { return 0xFF000000u | ((y & 0xFFF) << 12) | (x & 0xFFF); }
|
||||
|
||||
static void copy_out(ID3D12Resource* tex, ID3D12Resource* readback) {
|
||||
D3D12_TEXTURE_COPY_LOCATION dst{};
|
||||
dst.pResource = readback;
|
||||
dst.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
|
||||
dst.PlacedFootprint.Footprint = {DXGI_FORMAT_R8G8B8A8_UNORM, kSize, kSize, 1, kPitch};
|
||||
D3D12_TEXTURE_COPY_LOCATION src{};
|
||||
src.pResource = tex;
|
||||
src.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
|
||||
g_list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
|
||||
}
|
||||
|
||||
static unsigned wrong_texels(const uint32_t* rb) {
|
||||
unsigned wrong = 0;
|
||||
for (UINT y = 0; y < kSize; ++y)
|
||||
for (UINT x = 0; x < kSize; ++x) wrong += rb[y * kSize + x] != texel(x, y);
|
||||
return wrong;
|
||||
}
|
||||
|
||||
int main() {
|
||||
HMODULE d3d12 = LoadLibraryA("d3d12.dll");
|
||||
auto create = reinterpret_cast<PFN_D3D12_CREATE_DEVICE>(reinterpret_cast<void*>(GetProcAddress(d3d12, "D3D12CreateDevice")));
|
||||
if (!create || FAILED(create(nullptr, D3D_FEATURE_LEVEL_12_0, __uuidof(ID3D12Device), reinterpret_cast<void**>(&g_dev)))) {
|
||||
CHECK(false, "D3D12 device");
|
||||
return 1;
|
||||
}
|
||||
D3D12_FEATURE_DATA_D3D12_OPTIONS o{};
|
||||
g_dev->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS, &o, sizeof(o));
|
||||
std::printf("INFO: the device reports TiledResourcesTier %d, ResourceHeapTier %d, StandardSwizzle64KBSupported %d\n",
|
||||
static_cast<int>(o.TiledResourcesTier), static_cast<int>(o.ResourceHeapTier), o.StandardSwizzle64KBSupported);
|
||||
D3D12_COMMAND_QUEUE_DESC qd{};
|
||||
qd.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
|
||||
g_dev->CreateCommandQueue(&qd, __uuidof(ID3D12CommandQueue), reinterpret_cast<void**>(&g_queue));
|
||||
g_dev->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, __uuidof(ID3D12CommandAllocator), reinterpret_cast<void**>(&g_alloc));
|
||||
g_dev->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, g_alloc, nullptr, __uuidof(ID3D12GraphicsCommandList),
|
||||
reinterpret_cast<void**>(&g_list));
|
||||
g_dev->CreateFence(0, D3D12_FENCE_FLAG_NONE, __uuidof(ID3D12Fence), reinterpret_cast<void**>(&g_fence));
|
||||
g_event = CreateEventW(nullptr, FALSE, FALSE, nullptr);
|
||||
|
||||
// A CPU-visible heap with one buffer over all of it.
|
||||
D3D12_HEAP_DESC hd{};
|
||||
hd.SizeInBytes = kHeap;
|
||||
hd.Properties.Type = D3D12_HEAP_TYPE_CUSTOM;
|
||||
hd.Properties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_WRITE_BACK;
|
||||
hd.Properties.MemoryPoolPreference = D3D12_MEMORY_POOL_L0;
|
||||
ID3D12Heap* heap = nullptr;
|
||||
HRESULT hr = g_dev->CreateHeap(&hd, __uuidof(ID3D12Heap), reinterpret_cast<void**>(&heap));
|
||||
ID3D12Resource* whole = nullptr;
|
||||
const D3D12_RESOURCE_DESC wd = buffer_desc(kHeap);
|
||||
if (heap) g_dev->CreatePlacedResource(heap, 0, &wd, D3D12_RESOURCE_STATE_COMMON, nullptr, __uuidof(ID3D12Resource), reinterpret_cast<void**>(&whole));
|
||||
uint8_t* H = nullptr;
|
||||
if (whole) whole->Map(0, nullptr, reinterpret_cast<void**>(&H));
|
||||
CHECK(H != nullptr, "CPU-visible 64 MiB heap with a heap-wide buffer (hr 0x%08lx)", static_cast<unsigned long>(hr));
|
||||
if (!H) return 1;
|
||||
|
||||
ID3D12Resource* upload = committed(D3D12_HEAP_TYPE_UPLOAD, UINT64(kPitch) * kSize, D3D12_RESOURCE_STATE_GENERIC_READ);
|
||||
ID3D12Resource* readback = committed(D3D12_HEAP_TYPE_READBACK, UINT64(kPitch) * kSize, D3D12_RESOURCE_STATE_COPY_DEST);
|
||||
uint32_t* up = nullptr;
|
||||
uint32_t* rb = nullptr;
|
||||
upload->Map(0, nullptr, reinterpret_cast<void**>(&up));
|
||||
readback->Map(0, nullptr, reinterpret_cast<void**>(&rb));
|
||||
for (UINT y = 0; y < kSize; ++y)
|
||||
for (UINT x = 0; x < kSize; ++x) up[y * kSize + x] = texel(x, y);
|
||||
|
||||
// --- a reserved texture whose 16 tiles come from scattered, descending offsets of that heap ---
|
||||
const D3D12_RESOURCE_DESC td = texture_desc(DXGI_FORMAT_R8G8B8A8_UNORM, kSize, 1);
|
||||
ID3D12Resource* a = reserved(td, &hr);
|
||||
CHECK(a != nullptr, "reserved 512x512 RGBA8 texture (hr 0x%08lx)", static_cast<unsigned long>(hr));
|
||||
if (!a) return 1;
|
||||
UINT total = 0, count = 1;
|
||||
D3D12_PACKED_MIP_INFO packed{};
|
||||
D3D12_TILE_SHAPE shape{};
|
||||
D3D12_SUBRESOURCE_TILING tiling{};
|
||||
g_dev->GetResourceTiling(a, &total, &packed, &shape, &count, 0, &tiling);
|
||||
std::printf("INFO: tiling: %u tiles of %ux%u texels, %ux%u tiles in mip 0, %u packed mips\n", total, shape.WidthInTexels, shape.HeightInTexels,
|
||||
tiling.WidthInTiles, tiling.HeightInTiles, packed.NumPackedMips);
|
||||
constexpr UINT kFirst = 200; // tile i -> heap tile 200 - 3*i
|
||||
map_tiles(a, heap, kFirst, -3);
|
||||
wait_queue();
|
||||
D3D12_TEXTURE_COPY_LOCATION dst{};
|
||||
dst.pResource = a;
|
||||
dst.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
|
||||
D3D12_TEXTURE_COPY_LOCATION src{};
|
||||
src.pResource = upload;
|
||||
src.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
|
||||
src.PlacedFootprint.Footprint = {DXGI_FORMAT_R8G8B8A8_UNORM, kSize, kSize, 1, kPitch};
|
||||
g_list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
|
||||
copy_out(a, readback);
|
||||
bool ok = submit();
|
||||
unsigned wrong = wrong_texels(rb);
|
||||
CHECK(ok && wrong == 0, "texels uploaded into the texture read back (%u of %u wrong)", wrong, kSize * kSize);
|
||||
|
||||
// Did the texels land in the heap pages named in the mapping, where the CPU can see them?
|
||||
unsigned named_filled = 0, others_filled = 0;
|
||||
for (UINT t = 0; t < kHeap / kTile; ++t) {
|
||||
bool any = false;
|
||||
for (UINT64 i = 0; i < kTile && !any; i += 64) any = H[t * kTile + i] != 0;
|
||||
bool named = false;
|
||||
for (UINT i = 0; i < 16; ++i) named = named || t == kFirst - 3 * i;
|
||||
if (any && named) ++named_filled;
|
||||
if (any && !named) ++others_filled;
|
||||
}
|
||||
CHECK(named_filled == 16 && others_filled == 0,
|
||||
"the texels are in exactly the 16 heap tiles named by the mapping, visible to the CPU (%u named tiles filled, %u others)", named_filled,
|
||||
others_filled);
|
||||
|
||||
// Does each 64 KiB heap tile hold exactly the texels of its 128x128 tile?
|
||||
unsigned tiles_self_contained = 0;
|
||||
for (UINT i = 0; i < 16; ++i) {
|
||||
const uint32_t* words = reinterpret_cast<const uint32_t*>(H + (kFirst - 3 * i) * kTile);
|
||||
const UINT tx = i % 4, ty = i / 4;
|
||||
bool inside = true;
|
||||
for (UINT w = 0; w < kTile / 4 && inside; ++w) {
|
||||
const UINT x = words[w] & 0xFFF, y = (words[w] >> 12) & 0xFFF;
|
||||
inside = (words[w] >> 24) == 0xFF && x / 128 == tx && y / 128 == ty;
|
||||
}
|
||||
tiles_self_contained += inside;
|
||||
}
|
||||
std::printf("INFO: heap tiles holding only texels of their own 128x128 tile: %u of 16\n", tiles_self_contained);
|
||||
const uint32_t* first = reinterpret_cast<const uint32_t*>(H + kFirst * kTile);
|
||||
std::printf("INFO: texel order in a tile, as (x,y) of the first 12 texels in memory:");
|
||||
for (int w = 0; w < 12; ++w) std::printf(" (%u,%u)", first[w] & 0xFFF, (first[w] >> 12) & 0xFFF);
|
||||
std::printf("\n");
|
||||
|
||||
// --- a second texture mapped to the same heap tiles shows the same picture ---
|
||||
ID3D12Resource* b = reserved(td, &hr);
|
||||
if (b) {
|
||||
map_tiles(b, heap, kFirst, -3);
|
||||
wait_queue();
|
||||
std::memset(rb, 0, UINT64(kPitch) * kSize);
|
||||
copy_out(b, readback);
|
||||
ok = submit();
|
||||
wrong = wrong_texels(rb);
|
||||
}
|
||||
CHECK(b && ok && wrong == 0, "a second reserved texture on the same heap tiles reads the same texels (%u wrong)", b ? wrong : kSize * kSize);
|
||||
|
||||
// --- a CPU write into a heap tile changes what the texture reads ---
|
||||
std::memset(H + kFirst * kTile, 0, kTile); // tile (0,0)
|
||||
std::memset(rb, 0xAB, UINT64(kPitch) * kSize);
|
||||
copy_out(a, readback);
|
||||
ok = submit();
|
||||
unsigned zeroed = 0, changed = 0, kept = 0;
|
||||
for (UINT y = 0; y < kSize; ++y)
|
||||
for (UINT x = 0; x < kSize; ++x) {
|
||||
const bool in_tile = x < 128 && y < 128;
|
||||
if (in_tile && rb[y * kSize + x] == 0) ++zeroed;
|
||||
if (in_tile && rb[y * kSize + x] != texel(x, y)) ++changed;
|
||||
if (!in_tile && rb[y * kSize + x] == texel(x, y)) ++kept;
|
||||
}
|
||||
// RGBA8 tiles are not stored as plain texels (see the texel-order line above), so a zeroed tile
|
||||
// need not read as zero: what matters is that only that tile changes.
|
||||
CHECK(ok && changed > 0 && kept == kSize * kSize - 128 * 128,
|
||||
"overwriting one heap tile from the CPU changes that tile of the texture and no other (%u of %u texels changed, %u read zero, %u of %u others kept)",
|
||||
changed, 128 * 128, zeroed, kept, kSize * kSize - 128 * 128);
|
||||
|
||||
// --- an unmapped tile reads zero ---
|
||||
{
|
||||
D3D12_TILED_RESOURCE_COORDINATE at{};
|
||||
at.X = 3;
|
||||
at.Y = 3;
|
||||
D3D12_TILE_REGION_SIZE size{};
|
||||
size.NumTiles = 1;
|
||||
D3D12_TILE_RANGE_FLAGS flags = D3D12_TILE_RANGE_FLAG_NULL;
|
||||
UINT count1 = 1;
|
||||
g_queue->UpdateTileMappings(a, 1, &at, &size, nullptr, 1, &flags, nullptr, &count1, D3D12_TILE_MAPPING_FLAG_NONE);
|
||||
wait_queue();
|
||||
copy_out(a, readback);
|
||||
ok = submit();
|
||||
unsigned z = 0;
|
||||
for (UINT y = 384; y < 512; ++y)
|
||||
for (UINT x = 384; x < 512; ++x) z += rb[y * kSize + x] == 0;
|
||||
CHECK(ok && z == 128 * 128, "a tile unmapped again reads zero (%u of %u)", z, 128 * 128);
|
||||
}
|
||||
|
||||
// --- block-compressed textures: are the blocks stored plainly, and can two textures share them? ---
|
||||
{
|
||||
// 512x512 BC1: 128x128 blocks of 8 bytes = 128 KiB = two 512x256 tiles.
|
||||
constexpr UINT kBlocks = 128, kBcPitch = kBlocks * 8;
|
||||
const D3D12_RESOURCE_DESC bd = texture_desc(DXGI_FORMAT_BC1_UNORM, 512, 1);
|
||||
ID3D12Resource* c = reserved(bd, &hr);
|
||||
ID3D12Resource* d = reserved(bd, &hr);
|
||||
ID3D12Resource* bc_up = committed(D3D12_HEAP_TYPE_UPLOAD, UINT64(kBcPitch) * kBlocks, D3D12_RESOURCE_STATE_GENERIC_READ);
|
||||
uint64_t* blocks = nullptr;
|
||||
bc_up->Map(0, nullptr, reinterpret_cast<void**>(&blocks));
|
||||
auto block = [](UINT bx, UINT by) { return 0xB1C0000000000000ull | (static_cast<uint64_t>(by) << 16) | bx; };
|
||||
for (UINT by = 0; by < kBlocks; ++by)
|
||||
for (UINT bx = 0; bx < kBlocks; ++bx) blocks[by * kBlocks + bx] = block(bx, by);
|
||||
auto map_two = [&](ID3D12Resource* tex) {
|
||||
for (UINT i = 0; i < 2; ++i) {
|
||||
D3D12_TILED_RESOURCE_COORDINATE at{};
|
||||
at.Y = i;
|
||||
D3D12_TILE_REGION_SIZE size{};
|
||||
size.NumTiles = 1;
|
||||
D3D12_TILE_RANGE_FLAGS flags = D3D12_TILE_RANGE_FLAG_NONE;
|
||||
UINT offset = 500 + 7 * i, count1 = 1; // heap tiles 500 and 507
|
||||
g_queue->UpdateTileMappings(tex, 1, &at, &size, heap, 1, &flags, &offset, &count1, D3D12_TILE_MAPPING_FLAG_NONE);
|
||||
}
|
||||
};
|
||||
auto read_blocks = [&](ID3D12Resource* tex) {
|
||||
D3D12_TEXTURE_COPY_LOCATION out{};
|
||||
out.pResource = readback;
|
||||
out.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
|
||||
out.PlacedFootprint.Footprint = {DXGI_FORMAT_BC1_UNORM, 512, 512, 1, kBcPitch};
|
||||
D3D12_TEXTURE_COPY_LOCATION in{};
|
||||
in.pResource = tex;
|
||||
in.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
|
||||
std::memset(rb, 0xEE, UINT64(kBcPitch) * kBlocks);
|
||||
g_list->CopyTextureRegion(&out, 0, 0, 0, &in, nullptr);
|
||||
return submit();
|
||||
};
|
||||
if (c && d) {
|
||||
map_two(c);
|
||||
wait_queue();
|
||||
D3D12_TEXTURE_COPY_LOCATION to{};
|
||||
to.pResource = c;
|
||||
to.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
|
||||
D3D12_TEXTURE_COPY_LOCATION from{};
|
||||
from.pResource = bc_up;
|
||||
from.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
|
||||
from.PlacedFootprint.Footprint = {DXGI_FORMAT_BC1_UNORM, 512, 512, 1, kBcPitch};
|
||||
g_list->CopyTextureRegion(&to, 0, 0, 0, &from, nullptr);
|
||||
ok = submit() && read_blocks(c);
|
||||
const uint64_t* got = reinterpret_cast<const uint64_t*>(rb);
|
||||
unsigned bad = 0;
|
||||
for (UINT i = 0; i < kBlocks * kBlocks; ++i) bad += got[i] != block(i % kBlocks, i / kBlocks);
|
||||
CHECK(ok && bad == 0, "BC1: blocks uploaded into a reserved texture read back (%u wrong)", bad);
|
||||
// Are the heap tiles made of exactly those 8-byte blocks, each tile holding its own half?
|
||||
const uint64_t* t0 = reinterpret_cast<const uint64_t*>(H + 500 * kTile);
|
||||
const uint64_t* t1 = reinterpret_cast<const uint64_t*>(H + 507 * kTile);
|
||||
unsigned plain0 = 0, plain1 = 0;
|
||||
for (UINT w = 0; w < kTile / 8; ++w) {
|
||||
plain0 += (t0[w] >> 48) == 0xB1C0 && ((t0[w] >> 16) & 0xFFFF) < 64;
|
||||
plain1 += (t1[w] >> 48) == 0xB1C0 && ((t1[w] >> 16) & 0xFFFF) >= 64;
|
||||
}
|
||||
std::printf("INFO: BC1: heap tile words that are plain blocks of that tile's half: %u and %u of %llu\n", plain0, plain1,
|
||||
(unsigned long long)(kTile / 8));
|
||||
std::printf("INFO: BC1: block order in a tile, as (bx,by) of the first 20 blocks in memory:");
|
||||
for (int w = 0; w < 20; ++w) std::printf(" (%u,%u)", static_cast<unsigned>(t0[w] & 0xFFFF), static_cast<unsigned>((t0[w] >> 16) & 0xFFFF));
|
||||
std::printf("\n");
|
||||
// A second texture on the same heap tiles.
|
||||
map_two(d);
|
||||
wait_queue();
|
||||
ok = read_blocks(d);
|
||||
bad = 0;
|
||||
for (UINT i = 0; i < kBlocks * kBlocks; ++i) bad += got[i] != block(i % kBlocks, i / kBlocks);
|
||||
CHECK(ok && bad == 0, "BC1: a second reserved texture on the same heap tiles reads the same blocks (%u wrong)", bad);
|
||||
// A CPU write of one block, found by its value, is what the texture then reads there.
|
||||
uint64_t* w0 = reinterpret_cast<uint64_t*>(H + 500 * kTile);
|
||||
UINT at = 0;
|
||||
for (UINT w = 0; w < kTile / 8; ++w)
|
||||
if (w0[w] == block(5, 9)) at = w;
|
||||
w0[at] = 0x1122334455667788ull;
|
||||
ok = read_blocks(c);
|
||||
CHECK(ok && got[9 * kBlocks + 5] == 0x1122334455667788ull, "BC1: a block written into the heap tile by the CPU is what the texture reads there");
|
||||
} else {
|
||||
std::printf("INFO: reserved BC1 textures not created (hr 0x%08lx)\n", static_cast<unsigned long>(hr));
|
||||
}
|
||||
}
|
||||
|
||||
// --- a mip chain: which mips are packed ---
|
||||
for (DXGI_FORMAT f : {DXGI_FORMAT_BC7_UNORM, DXGI_FORMAT_BC1_UNORM}) {
|
||||
const D3D12_RESOURCE_DESC md = texture_desc(f, 1024, 11);
|
||||
ID3D12Resource* m = reserved(md, &hr);
|
||||
if (!m) {
|
||||
std::printf("INFO: reserved 1024x1024 format %d with 11 mips not created (hr 0x%08lx)\n", static_cast<int>(f), static_cast<unsigned long>(hr));
|
||||
continue;
|
||||
}
|
||||
UINT mtotal = 0, mcount = 11;
|
||||
D3D12_PACKED_MIP_INFO mp{};
|
||||
D3D12_TILE_SHAPE ms{};
|
||||
D3D12_SUBRESOURCE_TILING mt[11]{};
|
||||
g_dev->GetResourceTiling(m, &mtotal, &mp, &ms, &mcount, 0, mt);
|
||||
std::printf("INFO: 1024x1024 format %d, 11 mips: %u tiles of %ux%u texels; standard mips %u, packed mips %u in %u tiles; mip0 %ux%u tiles, mip1 %ux%u, mip2 %ux%u\n",
|
||||
static_cast<int>(f), mtotal, ms.WidthInTexels, ms.HeightInTexels, mp.NumStandardMips, mp.NumPackedMips, mp.NumTilesForPackedMips,
|
||||
mt[0].WidthInTiles, mt[0].HeightInTiles, mt[1].WidthInTiles, mt[1].HeightInTiles, mt[2].WidthInTiles, mt[2].HeightInTiles);
|
||||
m->Release();
|
||||
}
|
||||
|
||||
// --- a reserved buffer ---
|
||||
{
|
||||
D3D12_RESOURCE_DESC bd = buffer_desc(1 << 20);
|
||||
bd.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
|
||||
ID3D12Resource* rbuf = reserved(bd, &hr);
|
||||
bool same = false;
|
||||
if (rbuf) {
|
||||
// buffer tiles 0..15 -> heap tiles 300, 298, ... (descending by two)
|
||||
for (UINT i = 0; i < 16; ++i) {
|
||||
D3D12_TILED_RESOURCE_COORDINATE at{};
|
||||
at.X = i;
|
||||
D3D12_TILE_REGION_SIZE size{};
|
||||
size.NumTiles = 1;
|
||||
D3D12_TILE_RANGE_FLAGS flags = D3D12_TILE_RANGE_FLAG_NONE;
|
||||
UINT offset = 300 - 2 * i, count1 = 1;
|
||||
g_queue->UpdateTileMappings(rbuf, 1, &at, &size, heap, 1, &flags, &offset, &count1, D3D12_TILE_MAPPING_FLAG_NONE);
|
||||
std::memcpy(H + UINT64(offset) * kTile + 8, &i, 4);
|
||||
}
|
||||
wait_queue();
|
||||
same = true;
|
||||
for (UINT i = 0; i < 16; ++i) g_list->CopyBufferRegion(readback, i * 4, rbuf, UINT64(i) * kTile + 8, 4);
|
||||
same = submit();
|
||||
for (UINT i = 0; i < 16; ++i) same = same && rb[i] == i;
|
||||
}
|
||||
std::printf("%s: reserved buffer of 1 MiB: created hr 0x%08lx; its tiles read the scattered heap tiles they were mapped to: %s\n",
|
||||
rbuf && same ? "PASS" : "INFO", static_cast<unsigned long>(hr), rbuf ? (same ? "yes" : "NO") : "not created");
|
||||
}
|
||||
std::printf("RESULT: %d failed\n", g_fail);
|
||||
return g_fail ? 1 : 0;
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
// Probe P5 (Milestone 0): what many small textures cost on D3DMetal, committed versus reserved.
|
||||
// Standalone and synthetic. Footprint is the process's phys_footprint, read through bx_host.
|
||||
#include <windows.h>
|
||||
#include <d3d12.h>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <vector>
|
||||
|
||||
using InitFn = LONG (*)(void);
|
||||
using StatsFn = int (*)(uint64_t*);
|
||||
static StatsFn g_stats;
|
||||
static ID3D12Device* g_dev;
|
||||
static ID3D12CommandQueue* g_queue;
|
||||
static ID3D12Fence* g_fence;
|
||||
static UINT64 g_value;
|
||||
static HANDLE g_event;
|
||||
|
||||
static double footprint_mib() {
|
||||
uint64_t s[5] = {};
|
||||
g_stats(s);
|
||||
return static_cast<double>(s[0]) / (1024.0 * 1024.0);
|
||||
}
|
||||
|
||||
static void settle() {
|
||||
g_queue->Signal(g_fence, ++g_value);
|
||||
g_fence->SetEventOnCompletion(g_value, g_event);
|
||||
WaitForSingleObject(g_event, 30000);
|
||||
Sleep(1000);
|
||||
}
|
||||
|
||||
static D3D12_RESOURCE_DESC texture(DXGI_FORMAT format, UINT w, UINT h, bool reserved) {
|
||||
D3D12_RESOURCE_DESC d{};
|
||||
d.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
|
||||
d.Width = w;
|
||||
d.Height = h;
|
||||
d.DepthOrArraySize = 1;
|
||||
d.MipLevels = 1;
|
||||
d.Format = format;
|
||||
d.SampleDesc.Count = 1;
|
||||
d.Layout = reserved ? D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE : D3D12_TEXTURE_LAYOUT_UNKNOWN;
|
||||
return d;
|
||||
}
|
||||
|
||||
static void measure(const char* name, DXGI_FORMAT format, UINT w, UINT h, bool reserved, int count) {
|
||||
const D3D12_RESOURCE_DESC d = texture(format, w, h, reserved);
|
||||
const D3D12_RESOURCE_ALLOCATION_INFO info = g_dev->GetResourceAllocationInfo(0, 1, &d);
|
||||
D3D12_HEAP_PROPERTIES hp{};
|
||||
hp.Type = D3D12_HEAP_TYPE_DEFAULT;
|
||||
std::vector<ID3D12Resource*> made;
|
||||
made.reserve(count);
|
||||
const double before = footprint_mib();
|
||||
HRESULT hr = S_OK;
|
||||
for (int i = 0; i < count; ++i) {
|
||||
ID3D12Resource* r = nullptr;
|
||||
hr = reserved ? g_dev->CreateReservedResource(&d, D3D12_RESOURCE_STATE_COMMON, nullptr, __uuidof(ID3D12Resource), reinterpret_cast<void**>(&r))
|
||||
: g_dev->CreateCommittedResource(&hp, D3D12_HEAP_FLAG_NONE, &d, D3D12_RESOURCE_STATE_COMMON, nullptr,
|
||||
__uuidof(ID3D12Resource), reinterpret_cast<void**>(&r));
|
||||
if (FAILED(hr) || !r) break;
|
||||
made.push_back(r);
|
||||
}
|
||||
settle();
|
||||
const double after = footprint_mib();
|
||||
std::printf("INFO: %d %s %ux%u %s textures (host says %llu bytes each, last hr 0x%08lx): %+.0f MiB, %.1f KiB each\n",
|
||||
static_cast<int>(made.size()), reserved ? "reserved, unmapped" : "committed", w, h, name,
|
||||
static_cast<unsigned long long>(info.SizeInBytes), static_cast<unsigned long>(hr), after - before,
|
||||
made.empty() ? 0.0 : (after - before) * 1024.0 / static_cast<double>(made.size()));
|
||||
for (ID3D12Resource* r : made) r->Release();
|
||||
settle();
|
||||
std::printf("INFO: after releasing them: %+.0f MiB against the figure before\n", footprint_mib() - before);
|
||||
std::fflush(stdout);
|
||||
}
|
||||
|
||||
int main() {
|
||||
HMODULE host = LoadLibraryA("bx_host.dll");
|
||||
auto init = host ? reinterpret_cast<InitFn>(reinterpret_cast<void*>(GetProcAddress(host, "bx_host_init"))) : nullptr;
|
||||
g_stats = host ? reinterpret_cast<StatsFn>(reinterpret_cast<void*>(GetProcAddress(host, "bx_host_stats"))) : nullptr;
|
||||
if (!init || init() != 0 || !g_stats) {
|
||||
std::printf("FAIL: bx_host\n");
|
||||
return 1;
|
||||
}
|
||||
HMODULE d3d12 = LoadLibraryA("d3d12.dll");
|
||||
auto create = reinterpret_cast<PFN_D3D12_CREATE_DEVICE>(reinterpret_cast<void*>(GetProcAddress(d3d12, "D3D12CreateDevice")));
|
||||
if (!create || FAILED(create(nullptr, D3D_FEATURE_LEVEL_12_0, __uuidof(ID3D12Device), reinterpret_cast<void**>(&g_dev)))) {
|
||||
std::printf("FAIL: D3D12 device\n");
|
||||
return 1;
|
||||
}
|
||||
D3D12_COMMAND_QUEUE_DESC qd{};
|
||||
qd.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
|
||||
g_dev->CreateCommandQueue(&qd, __uuidof(ID3D12CommandQueue), reinterpret_cast<void**>(&g_queue));
|
||||
g_dev->CreateFence(0, D3D12_FENCE_FLAG_NONE, __uuidof(ID3D12Fence), reinterpret_cast<void**>(&g_fence));
|
||||
g_event = CreateEventW(nullptr, FALSE, FALSE, nullptr);
|
||||
settle();
|
||||
constexpr int kCount = 10000;
|
||||
measure("RGBA16F", DXGI_FORMAT_R16G16B16A16_FLOAT, 16, 4, false, kCount);
|
||||
measure("RGBA16F", DXGI_FORMAT_R16G16B16A16_FLOAT, 16, 4, true, kCount);
|
||||
measure("BC1", DXGI_FORMAT_BC1_UNORM, 128, 128, false, kCount);
|
||||
measure("BC1", DXGI_FORMAT_BC1_UNORM, 128, 128, true, kCount);
|
||||
measure("BC7", DXGI_FORMAT_BC7_UNORM, 64, 64, false, kCount);
|
||||
std::printf("RESULT: done\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
// Probe P6 (Milestone 0): what GPU-written textures cost on D3DMetal while they are never used.
|
||||
// Standalone and synthetic. Footprint is the process's phys_footprint, read through bx_host.
|
||||
#include <windows.h>
|
||||
#include <d3d12.h>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <vector>
|
||||
|
||||
using InitFn = LONG (*)(void);
|
||||
using StatsFn = int (*)(uint64_t*);
|
||||
static StatsFn g_stats;
|
||||
static ID3D12Device* g_dev;
|
||||
static ID3D12CommandQueue* g_queue;
|
||||
static ID3D12CommandAllocator* g_alloc;
|
||||
static ID3D12GraphicsCommandList* g_list;
|
||||
static ID3D12Fence* g_fence;
|
||||
static UINT64 g_value;
|
||||
static HANDLE g_event;
|
||||
static ID3D12Resource* g_a;
|
||||
static ID3D12Resource* g_b;
|
||||
|
||||
static double footprint_mib() {
|
||||
uint64_t s[5] = {};
|
||||
g_stats(s);
|
||||
return static_cast<double>(s[0]) / (1024.0 * 1024.0);
|
||||
}
|
||||
|
||||
// An unrelated GPU submission (a 16-byte buffer copy), then a pause.
|
||||
static void submit_something() {
|
||||
g_list->CopyBufferRegion(g_b, 0, g_a, 0, 16);
|
||||
g_list->Close();
|
||||
ID3D12CommandList* lists[] = {g_list};
|
||||
g_queue->ExecuteCommandLists(1, lists);
|
||||
g_queue->Signal(g_fence, ++g_value);
|
||||
g_fence->SetEventOnCompletion(g_value, g_event);
|
||||
WaitForSingleObject(g_event, 30000);
|
||||
g_alloc->Reset();
|
||||
g_list->Reset(g_alloc, nullptr);
|
||||
Sleep(1500);
|
||||
}
|
||||
|
||||
static ID3D12Resource* small_buffer(D3D12_HEAP_TYPE type, D3D12_RESOURCE_STATES state) {
|
||||
D3D12_HEAP_PROPERTIES hp{};
|
||||
hp.Type = type;
|
||||
D3D12_RESOURCE_DESC d{};
|
||||
d.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
|
||||
d.Width = 4096;
|
||||
d.Height = 1;
|
||||
d.DepthOrArraySize = 1;
|
||||
d.MipLevels = 1;
|
||||
d.SampleDesc.Count = 1;
|
||||
d.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
|
||||
ID3D12Resource* r = nullptr;
|
||||
g_dev->CreateCommittedResource(&hp, D3D12_HEAP_FLAG_NONE, &d, state, nullptr, __uuidof(ID3D12Resource), reinterpret_cast<void**>(&r));
|
||||
return r;
|
||||
}
|
||||
|
||||
static void measure(const char* name, DXGI_FORMAT format, UINT w, UINT h, D3D12_RESOURCE_FLAGS flags, int count) {
|
||||
D3D12_RESOURCE_DESC d{};
|
||||
d.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
|
||||
d.Width = w;
|
||||
d.Height = h;
|
||||
d.DepthOrArraySize = 1;
|
||||
d.MipLevels = 1;
|
||||
d.Format = format;
|
||||
d.SampleDesc.Count = 1;
|
||||
d.Flags = flags;
|
||||
const UINT64 each = g_dev->GetResourceAllocationInfo(0, 1, &d).SizeInBytes;
|
||||
D3D12_HEAP_PROPERTIES hp{};
|
||||
hp.Type = D3D12_HEAP_TYPE_DEFAULT;
|
||||
std::vector<ID3D12Resource*> made;
|
||||
const double before = footprint_mib();
|
||||
HRESULT hr = S_OK;
|
||||
for (int i = 0; i < count; ++i) {
|
||||
ID3D12Resource* r = nullptr;
|
||||
hr = g_dev->CreateCommittedResource(&hp, D3D12_HEAP_FLAG_NONE, &d, D3D12_RESOURCE_STATE_COMMON, nullptr, __uuidof(ID3D12Resource),
|
||||
reinterpret_cast<void**>(&r));
|
||||
if (FAILED(hr) || !r) break;
|
||||
made.push_back(r);
|
||||
}
|
||||
const double created = footprint_mib();
|
||||
submit_something();
|
||||
const double later = footprint_mib();
|
||||
const double nominal = static_cast<double>(each) * static_cast<double>(made.size()) / (1024.0 * 1024.0);
|
||||
std::printf("INFO: %d never-used %ux%u %s textures (host says %.0f MiB in all, last hr 0x%08lx): footprint %+.0f MiB at creation, "
|
||||
"%+.0f MiB after an unrelated GPU submission\n",
|
||||
static_cast<int>(made.size()), w, h, name, nominal, static_cast<unsigned long>(hr), created - before, later - before);
|
||||
for (ID3D12Resource* r : made) r->Release();
|
||||
submit_something();
|
||||
std::printf("INFO: after releasing them: %+.0f MiB against the figure before\n", footprint_mib() - before);
|
||||
std::fflush(stdout);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
HMODULE host = LoadLibraryA("bx_host.dll");
|
||||
auto init = host ? reinterpret_cast<InitFn>(reinterpret_cast<void*>(GetProcAddress(host, "bx_host_init"))) : nullptr;
|
||||
g_stats = host ? reinterpret_cast<StatsFn>(reinterpret_cast<void*>(GetProcAddress(host, "bx_host_stats"))) : nullptr;
|
||||
if (!init || init() != 0 || !g_stats) {
|
||||
std::printf("FAIL: bx_host\n");
|
||||
return 1;
|
||||
}
|
||||
HMODULE d3d12 = LoadLibraryA("d3d12.dll");
|
||||
auto create = reinterpret_cast<PFN_D3D12_CREATE_DEVICE>(reinterpret_cast<void*>(GetProcAddress(d3d12, "D3D12CreateDevice")));
|
||||
if (!create || FAILED(create(nullptr, D3D_FEATURE_LEVEL_12_0, __uuidof(ID3D12Device), reinterpret_cast<void**>(&g_dev)))) {
|
||||
std::printf("FAIL: D3D12 device\n");
|
||||
return 1;
|
||||
}
|
||||
D3D12_COMMAND_QUEUE_DESC qd{};
|
||||
qd.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
|
||||
g_dev->CreateCommandQueue(&qd, __uuidof(ID3D12CommandQueue), reinterpret_cast<void**>(&g_queue));
|
||||
g_dev->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, __uuidof(ID3D12CommandAllocator), reinterpret_cast<void**>(&g_alloc));
|
||||
g_dev->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, g_alloc, nullptr, __uuidof(ID3D12GraphicsCommandList),
|
||||
reinterpret_cast<void**>(&g_list));
|
||||
g_dev->CreateFence(0, D3D12_FENCE_FLAG_NONE, __uuidof(ID3D12Fence), reinterpret_cast<void**>(&g_fence));
|
||||
g_event = CreateEventW(nullptr, FALSE, FALSE, nullptr);
|
||||
g_a = small_buffer(D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_STATE_GENERIC_READ);
|
||||
g_b = small_buffer(D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_STATE_COPY_DEST);
|
||||
submit_something();
|
||||
// One case per process (argv[1]): D3DMetal keeps and reuses its pools, so cases run in one
|
||||
// process hide each other's cost.
|
||||
struct Case {
|
||||
const char* name;
|
||||
DXGI_FORMAT format;
|
||||
UINT w, h;
|
||||
D3D12_RESOURCE_FLAGS flags;
|
||||
int count;
|
||||
};
|
||||
const Case cases[] = {
|
||||
{"render-target BGRA8", DXGI_FORMAT_B8G8R8A8_UNORM, 3840, 2160, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET, 30},
|
||||
{"unordered-access RGBA8", DXGI_FORMAT_R8G8B8A8_UNORM, 3840, 2160, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, 30},
|
||||
{"depth D32", DXGI_FORMAT_D32_FLOAT, 3840, 2160, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL, 30},
|
||||
{"plain RGBA8", DXGI_FORMAT_R8G8B8A8_UNORM, 3840, 2160, D3D12_RESOURCE_FLAG_NONE, 30},
|
||||
{"plain RGBA16F", DXGI_FORMAT_R16G16B16A16_FLOAT, 16, 4, D3D12_RESOURCE_FLAG_NONE, 10000},
|
||||
{"plain BC1", DXGI_FORMAT_BC1_UNORM, 128, 128, D3D12_RESOURCE_FLAG_NONE, 10000},
|
||||
{"plain BC7", DXGI_FORMAT_BC7_UNORM, 64, 64, D3D12_RESOURCE_FLAG_NONE, 10000},
|
||||
};
|
||||
const int only = argc > 1 ? std::atoi(argv[1]) : 0;
|
||||
if (only < 0 || only >= static_cast<int>(sizeof(cases) / sizeof(cases[0]))) return 2;
|
||||
const Case& c = cases[only];
|
||||
measure(c.name, c.format, c.w, c.h, c.flags, c.count);
|
||||
std::printf("RESULT: done\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,251 @@
|
||||
// Probe P7 (Milestone 0): can GPU-written textures be reserved resources on D3DMetal, cost nothing
|
||||
// while unmapped, and work once tiles are mapped? One case per process: argv[1] = 0 render target,
|
||||
// 1 depth, 2 unordered access. Standalone and synthetic.
|
||||
#include <windows.h>
|
||||
#include <d3d12.h>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
using InitFn = LONG (*)(void);
|
||||
using StatsFn = int (*)(uint64_t*);
|
||||
static StatsFn g_stats;
|
||||
static ID3D12Device* g_dev;
|
||||
static ID3D12CommandQueue* g_queue;
|
||||
static ID3D12CommandAllocator* g_alloc;
|
||||
static ID3D12GraphicsCommandList* g_list;
|
||||
static ID3D12Fence* g_fence;
|
||||
static UINT64 g_value;
|
||||
static HANDLE g_event;
|
||||
|
||||
static double footprint_mib() {
|
||||
uint64_t s[5] = {};
|
||||
g_stats(s);
|
||||
return static_cast<double>(s[0]) / (1024.0 * 1024.0);
|
||||
}
|
||||
|
||||
static bool wait_queue() {
|
||||
g_queue->Signal(g_fence, ++g_value);
|
||||
g_fence->SetEventOnCompletion(g_value, g_event);
|
||||
return WaitForSingleObject(g_event, 30000) == WAIT_OBJECT_0;
|
||||
}
|
||||
|
||||
static bool submit() {
|
||||
g_list->Close();
|
||||
ID3D12CommandList* lists[] = {g_list};
|
||||
g_queue->ExecuteCommandLists(1, lists);
|
||||
const bool done = wait_queue();
|
||||
g_alloc->Reset();
|
||||
g_list->Reset(g_alloc, nullptr);
|
||||
return done;
|
||||
}
|
||||
|
||||
static ID3D12Resource* buffer(D3D12_HEAP_TYPE type, UINT64 size, D3D12_RESOURCE_STATES state) {
|
||||
D3D12_HEAP_PROPERTIES hp{};
|
||||
hp.Type = type;
|
||||
D3D12_RESOURCE_DESC d{};
|
||||
d.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
|
||||
d.Width = size;
|
||||
d.Height = 1;
|
||||
d.DepthOrArraySize = 1;
|
||||
d.MipLevels = 1;
|
||||
d.SampleDesc.Count = 1;
|
||||
d.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
|
||||
ID3D12Resource* r = nullptr;
|
||||
g_dev->CreateCommittedResource(&hp, D3D12_HEAP_FLAG_NONE, &d, state, nullptr, __uuidof(ID3D12Resource), reinterpret_cast<void**>(&r));
|
||||
return r;
|
||||
}
|
||||
|
||||
static D3D12_RESOURCE_DESC texture(DXGI_FORMAT format, UINT w, UINT h, D3D12_RESOURCE_FLAGS flags) {
|
||||
D3D12_RESOURCE_DESC d{};
|
||||
d.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
|
||||
d.Width = w;
|
||||
d.Height = h;
|
||||
d.DepthOrArraySize = 1;
|
||||
d.MipLevels = 1;
|
||||
d.Format = format;
|
||||
d.SampleDesc.Count = 1;
|
||||
d.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
|
||||
d.Flags = flags;
|
||||
return d;
|
||||
}
|
||||
|
||||
// One heap behind every tile of `tex`.
|
||||
static HRESULT map_all(ID3D12Resource* tex, ID3D12Heap** out) {
|
||||
UINT total = 0, count = 1;
|
||||
D3D12_PACKED_MIP_INFO packed{};
|
||||
D3D12_TILE_SHAPE shape{};
|
||||
D3D12_SUBRESOURCE_TILING tiling{};
|
||||
g_dev->GetResourceTiling(tex, &total, &packed, &shape, &count, 0, &tiling);
|
||||
if (!total) return E_FAIL;
|
||||
D3D12_HEAP_DESC hd{};
|
||||
hd.SizeInBytes = static_cast<UINT64>(total) * 65536;
|
||||
hd.Properties.Type = D3D12_HEAP_TYPE_DEFAULT;
|
||||
const HRESULT hr = g_dev->CreateHeap(&hd, __uuidof(ID3D12Heap), reinterpret_cast<void**>(out));
|
||||
if (FAILED(hr) || !*out) return FAILED(hr) ? hr : E_FAIL;
|
||||
D3D12_TILED_RESOURCE_COORDINATE at{};
|
||||
D3D12_TILE_REGION_SIZE size{};
|
||||
size.NumTiles = total;
|
||||
const D3D12_TILE_RANGE_FLAGS flags = D3D12_TILE_RANGE_FLAG_NONE;
|
||||
const UINT start = 0, range = total;
|
||||
g_queue->UpdateTileMappings(tex, 1, &at, &size, *out, 1, &flags, &start, &range, D3D12_TILE_MAPPING_FLAG_NONE);
|
||||
wait_queue();
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
HMODULE host = LoadLibraryA("bx_host.dll");
|
||||
auto init = host ? reinterpret_cast<InitFn>(reinterpret_cast<void*>(GetProcAddress(host, "bx_host_init"))) : nullptr;
|
||||
g_stats = host ? reinterpret_cast<StatsFn>(reinterpret_cast<void*>(GetProcAddress(host, "bx_host_stats"))) : nullptr;
|
||||
if (!init || init() != 0 || !g_stats) {
|
||||
std::printf("FAIL: bx_host\n");
|
||||
return 1;
|
||||
}
|
||||
HMODULE d3d12 = LoadLibraryA("d3d12.dll");
|
||||
auto create = reinterpret_cast<PFN_D3D12_CREATE_DEVICE>(reinterpret_cast<void*>(GetProcAddress(d3d12, "D3D12CreateDevice")));
|
||||
if (!create || FAILED(create(nullptr, D3D_FEATURE_LEVEL_12_0, __uuidof(ID3D12Device), reinterpret_cast<void**>(&g_dev)))) {
|
||||
std::printf("FAIL: D3D12 device\n");
|
||||
return 1;
|
||||
}
|
||||
D3D12_COMMAND_QUEUE_DESC qd{};
|
||||
qd.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
|
||||
g_dev->CreateCommandQueue(&qd, __uuidof(ID3D12CommandQueue), reinterpret_cast<void**>(&g_queue));
|
||||
g_dev->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, __uuidof(ID3D12CommandAllocator), reinterpret_cast<void**>(&g_alloc));
|
||||
g_dev->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, g_alloc, nullptr, __uuidof(ID3D12GraphicsCommandList),
|
||||
reinterpret_cast<void**>(&g_list));
|
||||
g_dev->CreateFence(0, D3D12_FENCE_FLAG_NONE, __uuidof(ID3D12Fence), reinterpret_cast<void**>(&g_fence));
|
||||
g_event = CreateEventW(nullptr, FALSE, FALSE, nullptr);
|
||||
|
||||
constexpr UINT kW = 1024, kH = 1024;
|
||||
ID3D12Resource* upload = buffer(D3D12_HEAP_TYPE_UPLOAD, UINT64(kW) * kH * 4, D3D12_RESOURCE_STATE_GENERIC_READ);
|
||||
ID3D12Resource* readback = buffer(D3D12_HEAP_TYPE_READBACK, UINT64(kW) * kH * 4, D3D12_RESOURCE_STATE_COPY_DEST);
|
||||
uint32_t* up = nullptr;
|
||||
uint32_t* rb = nullptr;
|
||||
upload->Map(0, nullptr, reinterpret_cast<void**>(&up));
|
||||
readback->Map(0, nullptr, reinterpret_cast<void**>(&rb));
|
||||
g_list->CopyBufferRegion(readback, 0, upload, 0, 16);
|
||||
submit();
|
||||
|
||||
const int which = argc > 1 ? std::atoi(argv[1]) : 0;
|
||||
const char* name = which == 0 ? "render-target BGRA8" : which == 1 ? "depth D32" : "unordered-access RGBA8";
|
||||
const DXGI_FORMAT format = which == 0 ? DXGI_FORMAT_B8G8R8A8_UNORM : which == 1 ? DXGI_FORMAT_D32_FLOAT : DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||
const D3D12_RESOURCE_FLAGS flags = which == 0 ? D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET
|
||||
: which == 1 ? D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL
|
||||
: D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
|
||||
|
||||
// --- A. thirty 3840x2160 reserved textures, never mapped: what do they cost? ---
|
||||
{
|
||||
const D3D12_RESOURCE_DESC big = texture(format, 3840, 2160, flags);
|
||||
std::vector<ID3D12Resource*> made;
|
||||
HRESULT hr = S_OK;
|
||||
const double before = footprint_mib();
|
||||
for (int i = 0; i < 30; ++i) {
|
||||
ID3D12Resource* r = nullptr;
|
||||
hr = g_dev->CreateReservedResource(&big, D3D12_RESOURCE_STATE_COMMON, nullptr, __uuidof(ID3D12Resource), reinterpret_cast<void**>(&r));
|
||||
if (FAILED(hr) || !r) break;
|
||||
made.push_back(r);
|
||||
}
|
||||
g_list->CopyBufferRegion(readback, 0, upload, 0, 16);
|
||||
submit();
|
||||
Sleep(1500);
|
||||
std::printf("INFO: %d reserved, unmapped 3840x2160 %s textures (last hr 0x%08lx): footprint %+.0f MiB after an unrelated GPU submission\n",
|
||||
static_cast<int>(made.size()), name, static_cast<unsigned long>(hr), footprint_mib() - before);
|
||||
for (ID3D12Resource* r : made) r->Release();
|
||||
}
|
||||
|
||||
// --- B. one 1024x1024 reserved texture: map it, write it the way its kind is written, read it back ---
|
||||
const D3D12_RESOURCE_DESC td = texture(format, kW, kH, flags);
|
||||
ID3D12Resource* tex = nullptr;
|
||||
HRESULT hr = g_dev->CreateReservedResource(&td, D3D12_RESOURCE_STATE_COMMON, nullptr, __uuidof(ID3D12Resource), reinterpret_cast<void**>(&tex));
|
||||
std::printf("%s: reserved 1024x1024 %s texture created (hr 0x%08lx)\n", tex ? "PASS" : "FAIL", name, static_cast<unsigned long>(hr));
|
||||
if (!tex) return 1;
|
||||
ID3D12Heap* heap = nullptr;
|
||||
hr = map_all(tex, &heap);
|
||||
std::printf("%s: all its tiles mapped to a heap (hr 0x%08lx)\n", SUCCEEDED(hr) ? "PASS" : "FAIL", static_cast<unsigned long>(hr));
|
||||
if (FAILED(hr)) return 1;
|
||||
|
||||
D3D12_RESOURCE_BARRIER b{};
|
||||
b.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
|
||||
b.Transition.pResource = tex;
|
||||
b.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
|
||||
D3D12_TEXTURE_COPY_LOCATION out{};
|
||||
out.pResource = readback;
|
||||
out.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
|
||||
D3D12_TEXTURE_COPY_LOCATION in{};
|
||||
in.pResource = tex;
|
||||
in.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
|
||||
std::memset(rb, 0xEE, UINT64(kW) * kH * 4);
|
||||
unsigned wrong = 0;
|
||||
bool ok = false;
|
||||
if (which == 0 || which == 1) {
|
||||
D3D12_DESCRIPTOR_HEAP_DESC dh{};
|
||||
dh.Type = which == 0 ? D3D12_DESCRIPTOR_HEAP_TYPE_RTV : D3D12_DESCRIPTOR_HEAP_TYPE_DSV;
|
||||
dh.NumDescriptors = 1;
|
||||
ID3D12DescriptorHeap* views = nullptr;
|
||||
g_dev->CreateDescriptorHeap(&dh, __uuidof(ID3D12DescriptorHeap), reinterpret_cast<void**>(&views));
|
||||
// The handle is returned by value; widl-style headers differ in how, so read it through the vtable-free helper.
|
||||
D3D12_CPU_DESCRIPTOR_HANDLE handle{};
|
||||
#if defined(__GNUC__)
|
||||
views->GetCPUDescriptorHandleForHeapStart(&handle);
|
||||
#else
|
||||
handle = views->GetCPUDescriptorHandleForHeapStart();
|
||||
#endif
|
||||
b.Transition.StateBefore = D3D12_RESOURCE_STATE_COMMON;
|
||||
b.Transition.StateAfter = which == 0 ? D3D12_RESOURCE_STATE_RENDER_TARGET : D3D12_RESOURCE_STATE_DEPTH_WRITE;
|
||||
g_list->ResourceBarrier(1, &b);
|
||||
if (which == 0) {
|
||||
g_dev->CreateRenderTargetView(tex, nullptr, handle);
|
||||
const float colour[4] = {1.0f, 0.5f, 0.25f, 1.0f}; // BGRA8: B=0x40 G=0x80 R=0xFF A=0xFF
|
||||
g_list->ClearRenderTargetView(handle, colour, 0, nullptr);
|
||||
} else {
|
||||
g_dev->CreateDepthStencilView(tex, nullptr, handle);
|
||||
g_list->ClearDepthStencilView(handle, D3D12_CLEAR_FLAG_DEPTH, 0.75f, 0, 0, nullptr);
|
||||
}
|
||||
b.Transition.StateBefore = b.Transition.StateAfter;
|
||||
b.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_SOURCE;
|
||||
g_list->ResourceBarrier(1, &b);
|
||||
out.PlacedFootprint.Footprint = {which == 0 ? DXGI_FORMAT_B8G8R8A8_UNORM : DXGI_FORMAT_R32_FLOAT, kW, kH, 1, kW * 4};
|
||||
g_list->CopyTextureRegion(&out, 0, 0, 0, &in, nullptr);
|
||||
ok = submit();
|
||||
float depth = 0.75f;
|
||||
uint32_t expect = 0xFFFF8040u;
|
||||
if (which == 1) std::memcpy(&expect, &depth, 4);
|
||||
for (UINT i = 0; i < kW * kH; ++i) {
|
||||
const uint32_t v = rb[i];
|
||||
// BGRA8 clear of (1, 0.5, 0.25, 1): allow the two roundings of 0.5 and 0.25.
|
||||
const bool same = which == 1 ? v == expect
|
||||
: ((v >> 24) == 0xFF && ((v >> 16) & 0xFF) == 0xFF && (((v >> 8) & 0xFF) == 0x80 || ((v >> 8) & 0xFF) == 0x7F) &&
|
||||
((v & 0xFF) == 0x40 || (v & 0xFF) == 0x3F));
|
||||
wrong += !same;
|
||||
}
|
||||
std::printf("%s: cleared as a %s and read back: %u of %u texels wrong (first 0x%08x)\n", ok && wrong == 0 ? "PASS" : "FAIL",
|
||||
which == 0 ? "render target" : "depth buffer", wrong, kW * kH, rb[0]);
|
||||
} else {
|
||||
// No compute shader here: the storage is exercised with a copy in and a copy out.
|
||||
for (UINT i = 0; i < kW * kH; ++i) up[i] = 0xFF000000u | (i * 2654435761u >> 8);
|
||||
D3D12_TEXTURE_COPY_LOCATION to{};
|
||||
to.pResource = tex;
|
||||
to.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
|
||||
D3D12_TEXTURE_COPY_LOCATION from{};
|
||||
from.pResource = upload;
|
||||
from.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
|
||||
from.PlacedFootprint.Footprint = {DXGI_FORMAT_R8G8B8A8_UNORM, kW, kH, 1, kW * 4};
|
||||
b.Transition.StateBefore = D3D12_RESOURCE_STATE_COMMON;
|
||||
b.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST;
|
||||
g_list->ResourceBarrier(1, &b);
|
||||
g_list->CopyTextureRegion(&to, 0, 0, 0, &from, nullptr);
|
||||
b.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
|
||||
b.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_SOURCE;
|
||||
g_list->ResourceBarrier(1, &b);
|
||||
out.PlacedFootprint.Footprint = {DXGI_FORMAT_R8G8B8A8_UNORM, kW, kH, 1, kW * 4};
|
||||
g_list->CopyTextureRegion(&out, 0, 0, 0, &in, nullptr);
|
||||
ok = submit();
|
||||
for (UINT i = 0; i < kW * kH; ++i) wrong += rb[i] != up[i];
|
||||
std::printf("%s: copied into and out of (shader writes not tested): %u of %u texels wrong\n", ok && wrong == 0 ? "PASS" : "FAIL", wrong,
|
||||
kW * kH);
|
||||
}
|
||||
std::printf("RESULT: done\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,267 @@
|
||||
// Probe P8: can existing memory become a D3D12 heap (OpenExistingHeapFromFileMapping / FromAddress),
|
||||
// so that a view of the mapping and the GPU see the same bytes? argv[1]: 0 = committed section,
|
||||
// 1 = reserved section (pages committed through the view, as a page pool does). Standalone and synthetic.
|
||||
#include <windows.h>
|
||||
#include <d3d12.h>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
using InitFn = LONG (*)(void);
|
||||
using StatsFn = int (*)(uint64_t*);
|
||||
static StatsFn g_stats;
|
||||
static ID3D12Device3* g_dev;
|
||||
static ID3D12CommandQueue* g_queue;
|
||||
static ID3D12CommandAllocator* g_alloc;
|
||||
static ID3D12GraphicsCommandList* g_list;
|
||||
static ID3D12Fence* g_fence;
|
||||
static UINT64 g_value;
|
||||
static HANDLE g_event;
|
||||
static int g_failed;
|
||||
|
||||
static void check(bool ok, const char* what) {
|
||||
std::printf("%s: %s\n", ok ? "PASS" : "FAIL", what);
|
||||
g_failed += !ok;
|
||||
std::fflush(stdout);
|
||||
}
|
||||
|
||||
static double footprint_mib() {
|
||||
uint64_t s[5] = {};
|
||||
g_stats(s);
|
||||
return static_cast<double>(s[0]) / (1024.0 * 1024.0);
|
||||
}
|
||||
|
||||
static bool submit() {
|
||||
g_list->Close();
|
||||
ID3D12CommandList* lists[] = {g_list};
|
||||
g_queue->ExecuteCommandLists(1, lists);
|
||||
g_queue->Signal(g_fence, ++g_value);
|
||||
g_fence->SetEventOnCompletion(g_value, g_event);
|
||||
const bool done = WaitForSingleObject(g_event, 30000) == WAIT_OBJECT_0;
|
||||
g_alloc->Reset();
|
||||
g_list->Reset(g_alloc, nullptr);
|
||||
return done;
|
||||
}
|
||||
|
||||
static D3D12_RESOURCE_DESC buffer_desc(UINT64 size, D3D12_RESOURCE_FLAGS flags) {
|
||||
D3D12_RESOURCE_DESC d{};
|
||||
d.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
|
||||
d.Width = size;
|
||||
d.Height = 1;
|
||||
d.DepthOrArraySize = 1;
|
||||
d.MipLevels = 1;
|
||||
d.SampleDesc.Count = 1;
|
||||
d.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
|
||||
d.Flags = flags;
|
||||
return d;
|
||||
}
|
||||
|
||||
static ID3D12Resource* committed(D3D12_HEAP_TYPE type, UINT64 size, D3D12_RESOURCE_STATES state) {
|
||||
D3D12_HEAP_PROPERTIES hp{};
|
||||
hp.Type = type;
|
||||
const D3D12_RESOURCE_DESC d = buffer_desc(size, D3D12_RESOURCE_FLAG_NONE);
|
||||
ID3D12Resource* r = nullptr;
|
||||
g_dev->CreateCommittedResource(&hp, D3D12_HEAP_FLAG_NONE, &d, state, nullptr, __uuidof(ID3D12Resource), reinterpret_cast<void**>(&r));
|
||||
return r;
|
||||
}
|
||||
|
||||
// A buffer placed on `heap` at `offset`; tries without and then with the cross-adapter flag.
|
||||
static ID3D12Resource* placed(ID3D12Heap* heap, UINT64 offset, UINT64 size, const char* what) {
|
||||
ID3D12Resource* r = nullptr;
|
||||
HRESULT hr = E_FAIL;
|
||||
const D3D12_RESOURCE_FLAGS tries[] = {D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_FLAG_ALLOW_CROSS_ADAPTER,
|
||||
D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS};
|
||||
for (D3D12_RESOURCE_FLAGS f : tries) {
|
||||
const D3D12_RESOURCE_DESC d = buffer_desc(size, f);
|
||||
hr = g_dev->CreatePlacedResource(heap, offset, &d, D3D12_RESOURCE_STATE_COMMON, nullptr, __uuidof(ID3D12Resource),
|
||||
reinterpret_cast<void**>(&r));
|
||||
std::printf("INFO: %s: placed buffer with flags 0x%x: hr 0x%08lx\n", what, static_cast<unsigned>(f), static_cast<unsigned long>(hr));
|
||||
if (f != D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS && r) break;
|
||||
if (f == D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS && r) { // informational only
|
||||
r->Release();
|
||||
r = nullptr;
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
// GPU -> CPU and CPU -> GPU through `cpu` (the caller's own pointer to the bytes behind `res`).
|
||||
static void coherence(ID3D12Resource* res, uint8_t* cpu, ID3D12Resource* upload, uint8_t* up, ID3D12Resource* readback, uint8_t* rb,
|
||||
const char* what) {
|
||||
constexpr UINT64 kLen = 65536;
|
||||
char line[200];
|
||||
bool gpu_to_cpu = true, cpu_to_gpu = true;
|
||||
for (int round = 0; round < 4; ++round) {
|
||||
for (UINT64 i = 0; i < kLen; ++i) up[i] = static_cast<uint8_t>(i * 7 + round * 31 + 1);
|
||||
g_list->CopyBufferRegion(res, 0, upload, 0, kLen);
|
||||
gpu_to_cpu &= submit();
|
||||
gpu_to_cpu &= std::memcmp(cpu, up, kLen) == 0;
|
||||
for (UINT64 i = 0; i < kLen; ++i) cpu[i] = static_cast<uint8_t>(i * 13 + round * 17 + 5);
|
||||
std::memset(rb, 0, kLen);
|
||||
g_list->CopyBufferRegion(readback, 0, res, 0, kLen);
|
||||
cpu_to_gpu &= submit();
|
||||
cpu_to_gpu &= std::memcmp(cpu, rb, kLen) == 0;
|
||||
}
|
||||
std::snprintf(line, sizeof line, "%s: the caller's memory shows what the GPU wrote (4 rounds)", what);
|
||||
check(gpu_to_cpu, line);
|
||||
std::snprintf(line, sizeof line, "%s: the GPU reads what the caller wrote, with no Map (4 rounds)", what);
|
||||
check(cpu_to_gpu, line);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
const bool reserve = argc > 1 && std::atoi(argv[1]) == 1;
|
||||
HMODULE host = LoadLibraryA("bx_host.dll");
|
||||
auto init = host ? reinterpret_cast<InitFn>(reinterpret_cast<void*>(GetProcAddress(host, "bx_host_init"))) : nullptr;
|
||||
g_stats = host ? reinterpret_cast<StatsFn>(reinterpret_cast<void*>(GetProcAddress(host, "bx_host_stats"))) : nullptr;
|
||||
if (!init || init() != 0 || !g_stats) {
|
||||
std::printf("FAIL: bx_host\n");
|
||||
return 1;
|
||||
}
|
||||
HMODULE d3d12 = LoadLibraryA("d3d12.dll");
|
||||
auto create = reinterpret_cast<PFN_D3D12_CREATE_DEVICE>(reinterpret_cast<void*>(GetProcAddress(d3d12, "D3D12CreateDevice")));
|
||||
if (!create || FAILED(create(nullptr, D3D_FEATURE_LEVEL_11_0, __uuidof(ID3D12Device3), reinterpret_cast<void**>(&g_dev)))) {
|
||||
std::printf("FAIL: D3D12 device (ID3D12Device3)\n");
|
||||
return 1;
|
||||
}
|
||||
D3D12_COMMAND_QUEUE_DESC qd{};
|
||||
qd.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
|
||||
g_dev->CreateCommandQueue(&qd, __uuidof(ID3D12CommandQueue), reinterpret_cast<void**>(&g_queue));
|
||||
g_dev->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, __uuidof(ID3D12CommandAllocator), reinterpret_cast<void**>(&g_alloc));
|
||||
g_dev->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, g_alloc, nullptr, __uuidof(ID3D12GraphicsCommandList),
|
||||
reinterpret_cast<void**>(&g_list));
|
||||
g_dev->CreateFence(0, D3D12_FENCE_FLAG_NONE, __uuidof(ID3D12Fence), reinterpret_cast<void**>(&g_fence));
|
||||
g_event = CreateEventW(nullptr, FALSE, FALSE, nullptr);
|
||||
ID3D12Resource* upload = committed(D3D12_HEAP_TYPE_UPLOAD, 1 << 20, D3D12_RESOURCE_STATE_GENERIC_READ);
|
||||
ID3D12Resource* readback = committed(D3D12_HEAP_TYPE_READBACK, 1 << 20, D3D12_RESOURCE_STATE_COPY_DEST);
|
||||
uint8_t* up = nullptr;
|
||||
uint8_t* rb = nullptr;
|
||||
upload->Map(0, nullptr, reinterpret_cast<void**>(&up));
|
||||
readback->Map(0, nullptr, reinterpret_cast<void**>(&rb));
|
||||
g_list->CopyBufferRegion(readback, 0, upload, 0, 16);
|
||||
submit();
|
||||
|
||||
// --- A. a 1 GiB section, opened as a heap ---
|
||||
constexpr UINT64 kSection = 1ull << 30;
|
||||
constexpr UINT64 kAt = 65536ull * 4099; // a page well inside
|
||||
std::printf("INFO: section kind: %s\n", reserve ? "reserved (SEC_RESERVE), pages committed through the view" : "committed (SEC_COMMIT)");
|
||||
HANDLE section = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE | (reserve ? SEC_RESERVE : SEC_COMMIT),
|
||||
static_cast<DWORD>(kSection >> 32), static_cast<DWORD>(kSection), nullptr);
|
||||
check(section != nullptr, "1 GiB section created");
|
||||
const double base = footprint_mib();
|
||||
ID3D12Heap* heap = nullptr;
|
||||
HRESULT hr = g_dev->OpenExistingHeapFromFileMapping(section, __uuidof(ID3D12Heap), reinterpret_cast<void**>(&heap));
|
||||
std::printf("INFO: OpenExistingHeapFromFileMapping: hr 0x%08lx\n", static_cast<unsigned long>(hr));
|
||||
check(SUCCEEDED(hr) && heap, "the section opened as a D3D12 heap");
|
||||
if (heap) {
|
||||
const D3D12_HEAP_DESC hd = heap->GetDesc();
|
||||
std::printf("INFO: heap: %llu MiB, type %d, CPU page property %d, flags 0x%x, alignment %llu\n",
|
||||
static_cast<unsigned long long>(hd.SizeInBytes >> 20), static_cast<int>(hd.Properties.Type),
|
||||
static_cast<int>(hd.Properties.CPUPageProperty), static_cast<unsigned>(hd.Flags),
|
||||
static_cast<unsigned long long>(hd.Alignment));
|
||||
std::printf("INFO: footprint after opening the heap: %+.0f MiB\n", footprint_mib() - base);
|
||||
// Two views of the same 64 KiB page, as a page pool maps one page at several addresses.
|
||||
auto* v1 = static_cast<uint8_t*>(MapViewOfFile(section, FILE_MAP_ALL_ACCESS, static_cast<DWORD>(kAt >> 32), static_cast<DWORD>(kAt), 65536));
|
||||
auto* v2 = static_cast<uint8_t*>(MapViewOfFile(section, FILE_MAP_ALL_ACCESS, static_cast<DWORD>(kAt >> 32), static_cast<DWORD>(kAt), 65536));
|
||||
bool mapped = v1 && v2;
|
||||
if (mapped && reserve) mapped = VirtualAlloc(v1, 65536, MEM_COMMIT, PAGE_READWRITE) && VirtualAlloc(v2, 65536, MEM_COMMIT, PAGE_READWRITE);
|
||||
check(mapped, "two views of one 64 KiB page of the section");
|
||||
ID3D12Resource* res = mapped ? placed(heap, kAt, 65536, "section heap") : nullptr;
|
||||
check(res != nullptr, "a buffer placed on the section heap at that page");
|
||||
if (res) {
|
||||
coherence(res, v1, upload, up, readback, rb, "section heap");
|
||||
check(std::memcmp(v1, v2, 65536) == 0, "the second view shows the same bytes");
|
||||
DWORD got = 0;
|
||||
char self[MAX_PATH] = {};
|
||||
GetModuleFileNameA(nullptr, self, MAX_PATH);
|
||||
HANDLE file = CreateFileA(self, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
|
||||
const bool read_ok = file != INVALID_HANDLE_VALUE && ReadFile(file, v1, 4096, &got, nullptr) && got == 4096;
|
||||
std::memset(rb, 0, 4096);
|
||||
g_list->CopyBufferRegion(readback, 0, res, 0, 4096);
|
||||
submit();
|
||||
check(read_ok && std::memcmp(rb, v1, 4096) == 0 && rb[0] == 'M' && rb[1] == 'Z', "ReadFile into the view is what the GPU then reads");
|
||||
std::printf("INFO: footprint after using one page of the 1 GiB heap: %+.0f MiB\n", footprint_mib() - base);
|
||||
// A page the views never touched, written only by the GPU, then seen by a later view.
|
||||
constexpr UINT64 kOther = 65536ull * 9001;
|
||||
ID3D12Resource* other = placed(heap, kOther, 65536, "section heap, untouched page");
|
||||
if (other) {
|
||||
for (UINT64 i = 0; i < 65536; ++i) up[i] = static_cast<uint8_t>(i * 3 + 9);
|
||||
g_list->CopyBufferRegion(other, 0, upload, 0, 65536);
|
||||
submit();
|
||||
auto* v3 = static_cast<uint8_t*>(
|
||||
MapViewOfFile(section, FILE_MAP_ALL_ACCESS, static_cast<DWORD>(kOther >> 32), static_cast<DWORD>(kOther), 65536));
|
||||
if (v3 && reserve) VirtualAlloc(v3, 65536, MEM_COMMIT, PAGE_READWRITE);
|
||||
check(v3 && std::memcmp(v3, up, 65536) == 0, "a page first written by the GPU shows those bytes in a view mapped afterwards");
|
||||
} else {
|
||||
check(false, "a buffer placed on an untouched page of the section heap");
|
||||
}
|
||||
}
|
||||
// A reserved BC1 texture with its tiles on the section heap.
|
||||
D3D12_RESOURCE_DESC td{};
|
||||
td.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
|
||||
td.Width = 512;
|
||||
td.Height = 512;
|
||||
td.DepthOrArraySize = 1;
|
||||
td.MipLevels = 1;
|
||||
td.Format = DXGI_FORMAT_BC1_UNORM;
|
||||
td.SampleDesc.Count = 1;
|
||||
td.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
|
||||
ID3D12Resource* tex = nullptr;
|
||||
hr = g_dev->CreateReservedResource(&td, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, __uuidof(ID3D12Resource), reinterpret_cast<void**>(&tex));
|
||||
const bool tiles = !(argc > 2 && std::strcmp(argv[2], "notiles") == 0);
|
||||
if (!tiles) {
|
||||
std::printf("INFO: tile check skipped on request\n");
|
||||
} else if (tex) {
|
||||
std::printf("INFO: reserved texture created; mapping two tiles to the section heap\n");
|
||||
std::fflush(stdout);
|
||||
constexpr UINT64 kTile = 65536ull * 12000;
|
||||
D3D12_TILED_RESOURCE_COORDINATE at{};
|
||||
D3D12_TILE_REGION_SIZE size{};
|
||||
size.NumTiles = 2; // 512x512 BC1 = 128 KiB = 2 tiles
|
||||
const D3D12_TILE_RANGE_FLAGS flags = D3D12_TILE_RANGE_FLAG_NONE;
|
||||
const UINT start = static_cast<UINT>(kTile / 65536), range = 2;
|
||||
g_queue->UpdateTileMappings(tex, 1, &at, &size, heap, 1, &flags, &start, &range, D3D12_TILE_MAPPING_FLAG_NONE);
|
||||
g_queue->Signal(g_fence, ++g_value);
|
||||
g_fence->SetEventOnCompletion(g_value, g_event);
|
||||
std::printf("INFO: tile mapping %s\n", WaitForSingleObject(g_event, 20000) == WAIT_OBJECT_0 ? "completed" : "did not complete in 20 s");
|
||||
std::fflush(stdout);
|
||||
uint32_t* words = reinterpret_cast<uint32_t*>(up);
|
||||
for (UINT i = 0; i < 128 * 128 * 2; ++i) words[i] = 0x9E3779B9u * (i + 1);
|
||||
D3D12_TEXTURE_COPY_LOCATION to{};
|
||||
to.pResource = tex;
|
||||
to.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
|
||||
D3D12_TEXTURE_COPY_LOCATION from{};
|
||||
from.pResource = upload;
|
||||
from.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
|
||||
from.PlacedFootprint.Footprint = {DXGI_FORMAT_BC1_UNORM, 512, 512, 1, 128 * 8};
|
||||
g_list->CopyTextureRegion(&to, 0, 0, 0, &from, nullptr);
|
||||
const bool done = submit();
|
||||
auto* tv = static_cast<uint8_t*>(
|
||||
MapViewOfFile(section, FILE_MAP_ALL_ACCESS, static_cast<DWORD>(kTile >> 32), static_cast<DWORD>(kTile), 131072));
|
||||
if (tv && reserve) VirtualAlloc(tv, 131072, MEM_COMMIT, PAGE_READWRITE);
|
||||
unsigned nonzero = 0;
|
||||
if (tv)
|
||||
for (UINT i = 0; i < 131072; ++i) nonzero += tv[i] != 0;
|
||||
std::printf("INFO: reserved BC1 texture with tiles on the section heap: upload %s; %u of 131072 bytes of those section pages are non-zero\n",
|
||||
done ? "done" : "timed out", nonzero);
|
||||
check(done && nonzero > 100000, "texture tiles mapped to the section heap live in the section's pages");
|
||||
} else {
|
||||
std::printf("INFO: reserved texture not created (hr 0x%08lx); tile check skipped\n", static_cast<unsigned long>(hr));
|
||||
}
|
||||
}
|
||||
|
||||
// --- B. VirtualAlloc memory, opened as a heap ---
|
||||
constexpr SIZE_T kMem = 64u << 20;
|
||||
auto* mem = static_cast<uint8_t*>(VirtualAlloc(nullptr, kMem, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE));
|
||||
ID3D12Heap* aheap = nullptr;
|
||||
hr = g_dev->OpenExistingHeapFromAddress(mem, __uuidof(ID3D12Heap), reinterpret_cast<void**>(&aheap));
|
||||
std::printf("INFO: OpenExistingHeapFromAddress: hr 0x%08lx\n", static_cast<unsigned long>(hr));
|
||||
check(SUCCEEDED(hr) && aheap, "VirtualAlloc memory opened as a D3D12 heap");
|
||||
if (aheap) {
|
||||
ID3D12Resource* res = placed(aheap, 65536 * 5, 65536, "address heap");
|
||||
check(res != nullptr, "a buffer placed on the address heap");
|
||||
if (res) coherence(res, mem + 65536 * 5, upload, up, readback, rb, "address heap");
|
||||
}
|
||||
std::printf("RESULT: %d failed\n", g_failed);
|
||||
return g_failed ? 1 : 0;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#!/bin/bash
|
||||
# Runs one built probe under CrossOver.
|
||||
# usage: run.sh <bottle> <probe name without .exe> [arguments]
|
||||
# BX_PROBE_BACKEND picks the graphics backend (default d3dmetal); CX_ROOT picks the CrossOver root
|
||||
# (a bottle with a root of its own, such as one a third-party graphics layer installed, needs it).
|
||||
set -euo pipefail
|
||||
HERE="$(cd "$(dirname "$0")" && pwd)"
|
||||
OUT="${OUT:-$HERE/../../../target/unified-memory-probes}"
|
||||
OUT="$(cd "$OUT" && pwd)"
|
||||
CX="${CX_ROOT:-/Applications/CrossOver.app/Contents/SharedSupport/CrossOver}"
|
||||
BACKEND="${BX_PROBE_BACKEND:-d3dmetal}"
|
||||
bottle="$1"; probe="$2"; shift 2
|
||||
cp "$OUT/x86_64-windows/bx_host.dll" "$OUT/bx_host.dll"
|
||||
exec env -i HOME="$HOME" USER="$USER" TMPDIR="${TMPDIR:-/tmp}" PATH=/usr/bin:/bin \
|
||||
"$CX/bin/wine" --bottle "$bottle" \
|
||||
--env "CX_GRAPHICS_BACKEND=$BACKEND WINEDLLPATH=$OUT:$CX/lib/wine/x86_64-windows:$CX/lib/wine/i386-windows:$CX/lib/wine" \
|
||||
"$OUT/$probe.exe" "$@"
|
||||
Reference in new issue
Block a user