Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -14052,9 +14052,9 @@ cmake --build --preset dev --target offscreen_check d3d12_xs_shim
ctest --preset dev --no-tests=error -R '^startup_image_sample_a16_(controls|warp|hardware)$' -V
```

## Startup translator: level-zero D16 image loads
## Startup translator: D16 image loads

For level-zero `image_load` with `d16` on 2D (including `a16` and NSA), 3D and 2D-array resources, `tools/startup/gfx2hlsl.py` narrows each enabled `dmask` component of the `Load` result to IEEE binary16 and packs the halves pairwise into `ceil(popcount(dmask) / 2)` consecutive VGPRs (low half first, high half second). HLSL `f32tof16` truncates toward zero on WARP (lowering 31,740 of the 65,536 `DXGI_FORMAT_R16_UNORM` depth values by 1 ULP relative to RDNA hardware), while narrowing an intermediate binary32 `Load` value still double-rounds `R16_UNORM` at `0xffaf` (`0x3bfe` instead of `0x3bfd`) and `0xffef` (`0x3c00` instead of `0x3bff`). `gfx2hlsl.py` therefore emits both an integer binary32-to-binary16 round-to-nearest-even helper (`pack16_rne`, quieting NaNs while preserving sign) and an exact `UNORM16` helper (`pack16_unorm16`, recovering `u = uint(saturate(v) * 65535.0 + 0.5)` and rounding `u / 65535` directly to binary16), selected per SRV via `udsrc <ud> d16unorm16 <index>`, and writes `d16srv <index> exact` to `<key>.txt`. Before drawing, `src/startup_stubs/d3d12_draw.inl` (`src/startup_stubs/d16_image_format.h`) validates the bound view's effective `DXGI_FORMAT` against the 14 independently GPU-verified exact formats (five 32-bit float views, six 8-bit `UNORM` views, and three 16-bit `UNORM` views), setting `d16unorm16` to `1` for `R16*_UNORM` and `0` for the exact binary32/`UNORM8` formats while refusing integer (`UINT`/`SINT`), typeless, depth-stencil-only, `SNORM16`, `R24_UNORM`, `SRGB`, block-compressed, packed-target stand-in, console extension, null-resource and unbound views. When `popcount(dmask)` is odd (`1` or `3`), bits `31:16` of the final destination VGPR are hardware-undefined on RDNA 2; `gfx2hlsl.py` tracks those registers across expressions and basic blocks and refuses whole-word reads unless consumed through a low-16-bit operation (`v_cvt_f32_f16`, `v_cvt_f32_ubyte0`/`1`, `SDWA` `BYTE_0`/`BYTE_1`/`WORD_0`, `v_and_b32` with constant mask `<= 0xffff`, `v_cmp_class_f16`/`v_cmpx_class_f16`, or `v_fma_mix*` half operands). Pipelines without `image_load` `d16` omit all helpers and `d16srv` lines; `image_load_mip` with `d16`, sampler MIMG forms with `d16`, 1D and cube dimensions, 3D or 2D-array `a16`, `tfe`, `lwe`, `r128`, set reserved MIMG bits, an empty `dmask`, and mismatched register counts remain refused.
For `image_load` and `image_load_mip` with `d16` on 2D (including NSA, and `a16` on level-zero `image_load`), 3D and 2D-array resources, `tools/startup/gfx2hlsl.py` narrows each enabled `dmask` component of the `Load` result to IEEE binary16 and packs the halves pairwise into `ceil(popcount(dmask) / 2)` consecutive VGPRs (low half first, high half second). HLSL `f32tof16` truncates toward zero on WARP (lowering 31,740 of the 65,536 `DXGI_FORMAT_R16_UNORM` depth values by 1 ULP relative to RDNA hardware), while narrowing an intermediate binary32 `Load` value still double-rounds `R16_UNORM` at `0xffaf` (`0x3bfe` instead of `0x3bfd`) and `0xffef` (`0x3c00` instead of `0x3bff`). `gfx2hlsl.py` therefore emits both an integer binary32-to-binary16 round-to-nearest-even helper (`pack16_rne`, quieting NaNs while preserving sign) and an exact `UNORM16` helper (`pack16_unorm16`, recovering `u = uint(saturate(v) * 65535.0 + 0.5)` and rounding `u / 65535` directly to binary16), selected per SRV via `udsrc <ud> d16unorm16 <index>`, and writes `d16srv <index> exact` to `<key>.txt`. Before drawing, `src/startup_stubs/d3d12_draw.inl` (`src/startup_stubs/d16_image_format.h`) validates the bound view's effective `DXGI_FORMAT` against the 14 independently GPU-verified exact formats (five 32-bit float views, six 8-bit `UNORM` views, and three 16-bit `UNORM` views), setting `d16unorm16` to `1` for `R16*_UNORM` and `0` for the exact binary32/`UNORM8` formats while refusing integer (`UINT`/`SINT`), typeless, depth-stencil-only, `SNORM16`, `R24_UNORM`, `SRGB`, block-compressed, packed-target stand-in, console extension, null-resource and unbound views. When `popcount(dmask)` is odd (`1` or `3`), bits `31:16` of the final destination VGPR are hardware-undefined on RDNA 2; `gfx2hlsl.py` tracks those registers across expressions and basic blocks and refuses whole-word reads unless consumed through a low-16-bit operation (`v_cvt_f32_f16`, `v_cvt_f32_ubyte0`/`1`, `SDWA` `BYTE_0`/`BYTE_1`/`WORD_0`, `v_and_b32` with constant mask `<= 0xffff`, `v_cmp_class_f16`/`v_cmpx_class_f16`, or `v_fma_mix*` half operands). Pipelines without D16 image loads omit all helpers and `d16srv` lines; sampler MIMG forms with `d16`, 1D and cube dimensions, `image_load_mip` or 3D / 2D-array `a16`, `tfe`, `lwe`, `r128`, set reserved MIMG bits, an empty `dmask`, and mismatched register counts remain refused.

Verified on commit base `009153b7` on Windows 11 26H2 (build `26300.9974`) across Microsoft Basic Render Driver (`1414:008c`, driver `10.0.26100.9818`) and AMD Radeon RX 9070 XT (`1002:7550`, driver `32.0.32029.2010`), D3D12 debug layer enabled (`debug_errors=0 debug_warnings=0`): exhaustive GPU probes across all 65,536 `DXGI_FORMAT_R16_UNORM` texels (DXC `cs_6_2` and FXC `cs_5_0`) and all 256 `DXGI_FORMAT_R8_UNORM` texels match RDNA 2 hardware `d16` loads with 0 mismatches (`65,536 / 65,536` and `256 / 256`, including `0xffaf -> 0x3bfd` and `0xffef -> 0x3bff`). In `offscreen_check --startup-image-load-d16` (DXC `ps_6_0` and FXC `ps_5_1`), fixtures A and B match all 128 output words (32 pixels) on `8x2` `R16_UNORM` and `8x2` `R32G32B32A32_FLOAT` textures, remain invariant when `0xdead0000` is injected into bits `31:16` of the odd-`dmask` registers, and reject eight mutated HLSL controls (truncation toward zero, binary32 `double_round` on `0xffaf`/`0xffef`, unpacked 32-bit `U(t.x)`, swapped low/high halves, dropped `<< 16` shift, dropped `a16` coordinate unpack, ignored NSA register list, and unmasked odd-`dmask` upper half). Through `d3d12_xs.dll` (`startup_d16_srv_{warp,hardware}`), `R16_UNORM` (`0xffef -> 0x3bff`), `R32_FLOAT` (`0x3f7ff000 -> 0x3c00`) and `R16_TYPELESS` viewed as `R16_UNORM` (`0xaaaa -> 0x3955`) dispatch the exact per-SRV mode on one PSO while `R16_UINT`, `R16_SNORM`, null-resource and unwritten views are refused and malformed `d16srv` manifests are rejected.

Expand Down
2 changes: 1 addition & 1 deletion tests/desktop_gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void check_startup_image_3d(ID3D12Device* device, const char* vertex_path, const
bool one_dimensional = false, bool two_dimensional = false,
UINT first_mip = 0, UINT view_levels = 0);
void check_startup_image_load_d16(ID3D12Device* device, const char* vertex_path, const char* pixel_path,
bool depth16, std::uint32_t words[64]);
bool depth16, std::uint32_t words[64], bool array = false);
int check_startup_image_store(ID3D12Device* device, const char* vertex_path, const char* pixel_path, UINT x, UINT y,
const std::uint32_t expected[4]);
void check_startup_flat_param(ID3D12Device* device, const char* vertex_path, const char* pixel_path,
Expand Down
16 changes: 9 additions & 7 deletions tests/offscreen_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1331,21 +1331,23 @@ OperationResult run_startup_image_2d_view(const OperationContext& ctx)
return {OutcomeKind::pass, nullptr};
}

// --startup-image-load-d16 <r16_unorm|rgba32f> <vs.cso> <ps.cso> <expected.bin>: draws an 8x2 full-screen
// pair to an R32G32B32A32_UINT target against an 8x2 R16_UNORM or R32G32B32A32_FLOAT source texture and
// compares all 64 little-endian words with expected.bin.
// --startup-image-load-d16 <r16_unorm|rgba32f|r16_unorm_array|rgba32f_array> <vs.cso> <ps.cso> <expected.bin>:
// draws an 8x2 full-screen pair to an R32G32B32A32_UINT target against a two-mip (8x2 + 4x1) 2D or two-slice
// 2D-array R16_UNORM / R32G32B32A32_FLOAT source texture and compares all 64 little-endian words with expected.bin.
OperationResult run_startup_image_load_d16(const OperationContext& ctx)
{
const bool depth16 = std::strcmp(ctx.argv[3], "r16_unorm") == 0;
if (!depth16 && std::strcmp(ctx.argv[3], "rgba32f") != 0)
return {OutcomeKind::fail, "image_load d16 source mode must be r16_unorm or rgba32f"};
const bool depth16 = std::strcmp(ctx.argv[3], "r16_unorm") == 0 || std::strcmp(ctx.argv[3], "r16_unorm_array") == 0;
const bool rgba32f = std::strcmp(ctx.argv[3], "rgba32f") == 0 || std::strcmp(ctx.argv[3], "rgba32f_array") == 0;
const bool array = std::strcmp(ctx.argv[3], "r16_unorm_array") == 0 || std::strcmp(ctx.argv[3], "rgba32f_array") == 0;
if (!depth16 && !rgba32f)
return {OutcomeKind::fail, "image_load d16 source mode must be r16_unorm[_array] or rgba32f[_array]"};
std::uint32_t expected[64]{};
std::ifstream file(ctx.argv[6], std::ios::binary);
file.read(reinterpret_cast<char*>(expected), sizeof(expected));
if (!file || file.peek() != std::char_traits<char>::eof())
return {OutcomeKind::fail, "expected file does not hold 64 words"};
std::uint32_t actual[64]{};
check_startup_image_load_d16(ctx.device, ctx.argv[4], ctx.argv[5], depth16, actual);
check_startup_image_load_d16(ctx.device, ctx.argv[4], ctx.argv[5], depth16, actual, array);
unsigned differing = 0;
for (unsigned i = 0; i < 16; ++i) {
const std::uint32_t* want = &expected[i * 4];
Expand Down
14 changes: 12 additions & 2 deletions tests/startup_image_2d_array_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,18 @@ def mip_controls():
accepted[1] |= 0x10 # unorm
assert 't = %s;' % MIP_CALL in translate(mip_words(1, 0, bytes(accepted))).hlsl_ps()

# Refused: packed 16-bit data or addresses and fault-status returns need their own lowering.
for flag, byte, mask in (('d16', 7, 0x80), ('a16', 7, 0x40), ('tfe', 2, 0x01)):
# Packed 16-bit data narrows the four channels into v[0:1] and marks SRV 0 for exact D16 validation.
d16 = bytearray(MIP_IMAGE)
d16[7] |= 0x80
d16_pipe = translate(mip_words(1, 0, bytes(d16)))
d16_ps = d16_pipe.hlsl_ps()
assert 't = %s;' % MIP_CALL in d16_ps, d16_ps
assert 'v0 = pack16_d16(t.x, ud4) | (pack16_d16(t.y, ud4) << 16);' in d16_ps, d16_ps
assert 'v1 = pack16_d16(t.z, ud4) | (pack16_d16(t.w, ud4) << 16);' in d16_ps, d16_ps
assert 'd16srv 0 exact' in d16_pipe.manifest().splitlines(), d16_pipe.manifest()

# Refused: packed addresses and fault-status returns need their own lowering.
for flag, byte, mask in (('a16', 7, 0x40), ('tfe', 2, 0x01)):
refused = bytearray(MIP_IMAGE)
refused[byte] |= mask
try:
Expand Down
68 changes: 49 additions & 19 deletions tests/startup_image_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,19 +215,24 @@ void check_startup_image_3d(ID3D12Device* device, const char* vertex_path, const
z, color[0], color[1], color[2], color[3]);
}

// Draws an 8x2 R32G32B32A32_UINT target against an 8x2 2D source texture with no static samplers:
// Draws an 8x2 R32G32B32A32_UINT target against a two-mip (8x2 mip 0, 4x1 mip 1) 2D or two-slice 2D-array
// source texture with no static samplers:
// depth16 == true: DXGI_FORMAT_R16_UNORM (16-bit depth SRV format) covering zero, binary16 subnormals,
// exact fractions and values where IEEE round-to-nearest-even differs from RTZ;
// exact fractions, double-rounding discriminators (0xffaf, 0xffef) and RNE-vs-RTZ codes;
// depth16 == false: DXGI_FORMAT_R32G32B32A32_FLOAT covering four distinct channels per texel, halfway ties
// to even, 1-ULP midpoint neighbours, subnormals, signed zero, overflow and quiet NaNs.
void check_startup_image_load_d16(ID3D12Device* device, const char* vertex_path, const char* pixel_path,
bool depth16, std::uint32_t words[64])
bool depth16, std::uint32_t words[64], bool array)
{
constexpr UINT width = 8, height = 2;
constexpr UINT width = 8, height = 2, d16_mips = 2;
static constexpr std::uint16_t kDepthTexels[width * height] = {
0x0000, 0x0001, 0x0003, 0x1234, 0x2aad, 0x4008, 0x5555, 0x8000,
0x8010, 0xa018, 0xc018, 0xe00c, 0xffaf, 0xffef, 0xfffe, 0xffff,
};
static constexpr std::uint16_t kDepthMip1[2][width / 2] = {
{0xffaf, 0xffef, 0x0003, 0x8010},
{0xffef, 0xffaf, 0x5555, 0xa018},
};
static constexpr std::uint32_t kFloatTexels[width * height][4] = {
{0x3f801000u, 0x3f803000u, 0x3f801001u, 0x3f7fffffu},
{0x387fe000u, 0x387fdfffu, 0x33800000u, 0x33000000u},
Expand All @@ -246,6 +251,12 @@ void check_startup_image_load_d16(ID3D12Device* device, const char* vertex_path,
{0x3fc00000u, 0xc0000000u, 0x40200000u, 0xc0400000u},
{0x477fe000u, 0xc77fe000u, 0x38800000u, 0xb8800000u},
};
static constexpr std::uint32_t kFloatMip1[width / 2][4] = {
{0x3f803000u, 0x387fe000u, 0xbf803000u, 0x477fefffu},
{0x33800000u, 0x33000001u, 0x80000000u, 0x7fc00001u},
{0x44006000u, 0xc4006000u, 0x39003000u, 0xb9003000u},
{0x477ff000u, 0xc77ff000u, 0x3e200000u, 0xbe200000u},
};

const auto vs = read_blob(vertex_path);
const auto ps = read_blob(pixel_path);
Expand All @@ -271,29 +282,46 @@ void check_startup_image_load_d16(ID3D12Device* device, const char* vertex_path,
ComPtr<ID3D12PipelineState> pipeline;
check(device->CreateGraphicsPipelineState(&pd, IID_PPV_ARGS(&pipeline)), "Create image_load d16 PSO");

const UINT slices = array ? 2u : 1u;
const UINT subresources = slices * d16_mips;
D3D12_RESOURCE_DESC td{};
td.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
td.Width = width;
td.Height = height;
td.DepthOrArraySize = 1;
td.MipLevels = 1;
td.DepthOrArraySize = static_cast<UINT16>(slices);
td.MipLevels = d16_mips;
td.Format = depth16 ? DXGI_FORMAT_R16_UNORM : DXGI_FORMAT_R32G32B32A32_FLOAT;
td.SampleDesc.Count = 1;
const auto texture = resource(device, td, D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_STATE_COPY_DEST);

D3D12_PLACED_SUBRESOURCE_FOOTPRINT src_footprint{};
D3D12_PLACED_SUBRESOURCE_FOOTPRINT src_footprints[4]{};
UINT64 upload_size = 0;
device->GetCopyableFootprints(&td, 0, 1, 0, &src_footprint, nullptr, nullptr, &upload_size);
device->GetCopyableFootprints(&td, 0, subresources, 0, src_footprints, nullptr, nullptr, &upload_size);
const auto upload = make_buffer(device, upload_size, D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_STATE_GENERIC_READ);
void* mapped = nullptr;
const D3D12_RANGE none{0, 0};
check(upload->Map(0, &none, &mapped), "Map image_load d16 texels");
auto* const dst_bytes = static_cast<std::uint8_t*>(mapped) + src_footprint.Offset;
const UINT row_bytes = depth16 ? width * sizeof(std::uint16_t) : width * 4 * sizeof(std::uint32_t);
for (UINT y = 0; y < height; ++y) {
const void* row_src = depth16 ? static_cast<const void*>(&kDepthTexels[y * width])
: static_cast<const void*>(&kFloatTexels[y * width][0]);
std::memcpy(dst_bytes + y * src_footprint.Footprint.RowPitch, row_src, row_bytes);
const UINT texel_bytes = depth16 ? sizeof(std::uint16_t) : 4 * sizeof(std::uint32_t);
for (UINT s = 0; s < slices; ++s) {
for (UINT m = 0; m < d16_mips; ++m) {
const UINT sub = m + s * d16_mips;
const UINT mw = width >> m, mh = height >> m;
auto* const sub_bytes = static_cast<std::uint8_t*>(mapped) + src_footprints[sub].Offset;
for (UINT y = 0; y < mh; ++y) {
for (UINT x = 0; x < mw; ++x) {
auto* const cell = sub_bytes + y * src_footprints[sub].Footprint.RowPitch + x * texel_bytes;
if (m == 0) {
const UINT idx = s == 0 ? (y * width + x) : (width * height - 1u - (y * width + x));
if (depth16) std::memcpy(cell, &kDepthTexels[idx], texel_bytes);
else std::memcpy(cell, &kFloatTexels[idx][0], texel_bytes);
} else {
const UINT idx = s == 0 ? x : ((width / 2 - 1u) - x);
if (depth16) std::memcpy(cell, &kDepthMip1[s][x], texel_bytes);
else std::memcpy(cell, &kFloatMip1[idx][0], texel_bytes);
}
}
}
}
}
upload->Unmap(0, nullptr);

Expand Down Expand Up @@ -323,11 +351,13 @@ void check_startup_image_load_d16(ID3D12Device* device, const char* vertex_path,

ComputeCommands commands(device, pipeline.Get(), D3D12_COMMAND_LIST_TYPE_DIRECT);
auto* list = commands.list.Get();
D3D12_TEXTURE_COPY_LOCATION to{texture.Get(), D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX};
to.SubresourceIndex = 0;
D3D12_TEXTURE_COPY_LOCATION from{upload.Get(), D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT};
from.PlacedFootprint = src_footprint;
list->CopyTextureRegion(&to, 0, 0, 0, &from, nullptr);
for (UINT sub = 0; sub < subresources; ++sub) {
D3D12_TEXTURE_COPY_LOCATION to{texture.Get(), D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX};
to.SubresourceIndex = sub;
D3D12_TEXTURE_COPY_LOCATION from{upload.Get(), D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT};
from.PlacedFootprint = src_footprints[sub];
list->CopyTextureRegion(&to, 0, 0, 0, &from, nullptr);
}
transition(list, texture.Get(), D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
list->SetGraphicsRootSignature(root.Get());
ID3D12DescriptorHeap* heaps[]{views.Get()};
Expand Down
Loading
Loading