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
3 changes: 3 additions & 0 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ jobs:
(["tests/startup_image_2d_array_check.py", "binary", "llvm-mc-15"], {}),
(["tests/startup_image_sample_a16_check.py", "binary", "llvm-mc-15"], {}),
(["tests/startup_image_sample_a16_check.py", "decode", "libLLVM-15.so.1"], {}),
(["tests/startup_image_load_d16_check.py", "binary", "llvm-mc-15"], {}),
(["tests/startup_image_load_d16_check.py", "decode", "libLLVM-15.so.1"], {}),
(["tests/startup_flat_param_check.py", "binary", "llvm-mc-15"], {}),
(["tests/startup_gfx2hlsl_bit_alu_check.py", "binary", "llvm-mc-15"], {}),
(["tests/startup_gfx2hlsl_mad_check.py", "binary", "llvm-mc-15"], {}),
Expand Down Expand Up @@ -203,6 +205,7 @@ jobs:
tests/startup_gfx2hlsl_operand_count_check.py \
tests/startup_image_2d_array_check.py \
tests/startup_image_sample_a16_check.py \
tests/startup_image_load_d16_check.py \
tests/startup_host_cpu_check.py \
tests/startup_live_telemetry_check.py \
tests/startup_gfx2hlsl_cvt_alu_check.py \
Expand Down
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ python3 -m venv out/pe-env
./out/pe-env/bin/python -B tests/startup_gfx2hlsl_swap_check.py
./out/pe-env/bin/python -B tests/startup_image_2d_array_check.py
./out/pe-env/bin/python -B tests/startup_image_sample_a16_check.py
./out/pe-env/bin/python -B tests/startup_image_load_d16_check.py
./out/pe-env/bin/python -B tests/startup_host_cpu_check.py
./out/pe-env/bin/python -B tests/startup_live_telemetry_check.py
./out/pe-env/bin/python -B tests/startup_gfx2hlsl_capture_check.py
Expand Down
20 changes: 20 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,13 @@ target_compile_features(startup_export_write_mask_check PRIVATE cxx_std_17)
add_test(NAME startup_export_write_mask COMMAND startup_export_write_mask_check)
add_test(NAME startup_export_write_mask_shared_blend_control COMMAND startup_export_write_mask_check --mutate shared-blend)
set_tests_properties(startup_export_write_mask_shared_blend_control PROPERTIES WILL_FAIL TRUE)
# Bound SRV format classification and 'd16srv' manifest parsing for D16 image_load (#2023).
add_executable(startup_d16_image_format_check tests/startup_d16_image_format_check.cpp)
target_include_directories(startup_d16_image_format_check PRIVATE src)
target_compile_features(startup_d16_image_format_check PRIVATE cxx_std_17)
add_test(NAME startup_d16_image_format COMMAND startup_d16_image_format_check)
add_test(NAME startup_d16_image_format_accept_uint_control COMMAND startup_d16_image_format_check --mutate accept-uint)
set_tests_properties(startup_d16_image_format_accept_uint_control PROPERTIES WILL_FAIL TRUE)
# Composite provenance (#1628): one VS and two pixel shaders from one HLSL file.
set(startup_composite_provenance_headers "")
foreach(stage IN ITEMS "vs_6_0;vs_main;vs" "ps_6_0;ps_fill;fill" "ps_6_0;ps_composite;composite")
Expand Down Expand Up @@ -2585,6 +2592,15 @@ if(EXISTS "${INTAKE_PYTHON}")
"$<TARGET_FILE:offscreen_check>" ${adapter} $<$<BOOL:${STARTUP_FXC}>:${STARTUP_FXC}>)
set_tests_properties(startup_image_sample_a16_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77)
endforeach()
# gfx2hlsl level-zero D16 image_load narrowing and pairwise packing (#2023).
add_test(NAME startup_image_load_d16_controls COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/startup_image_load_d16_check.py")
foreach(adapter IN ITEMS warp hardware)
add_test(NAME startup_image_load_d16_${adapter} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/startup_image_load_d16_check.py" draw "${DXC_EXECUTABLE}"
"$<TARGET_FILE:offscreen_check>" ${adapter} $<$<BOOL:${STARTUP_FXC}>:${STARTUP_FXC}>)
set_tests_properties(startup_image_load_d16_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77)
endforeach()
# gfx2hlsl flat parameters keep the exported words (#1259): translation controls, then a desktop
# draw of the translated pair with FXC 5_1 (the --pending path) and DXC 6_0.
add_test(NAME startup_flat_param_controls COMMAND "${INTAKE_PYTHON}" -B
Expand Down Expand Up @@ -2807,6 +2823,10 @@ if(EXISTS "${INTAKE_PYTHON}")
"${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_address_buffer_srv_check.py"
"$<TARGET_FILE:startup_stream_consumer>" "${backend}")
set_tests_properties(startup_address_buffer_srv_${backend} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77)
add_test(NAME startup_d16_srv_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}"
"${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_d16_srv_check.py"
"$<TARGET_FILE:startup_stream_consumer>" "${backend}")
set_tests_properties(startup_d16_srv_${backend} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77)
add_test(NAME startup_frame_copy_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}"
"${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_frame_copy_check.py"
"$<TARGET_FILE:startup_stream_consumer>" ${backend})
Expand Down
13 changes: 12 additions & 1 deletion docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -11960,7 +11960,7 @@ For plain 2D `image_load`, the startup translator reads `a16` coordinates from t
halves of one VGPR and emits `Texture2D.Load` at mip zero. The synthetic case in
`python -B tests/startup_image_load_mip_check.py` packs x=1 and y=2 into v0 and assigns 63 to v1, which the
old lowering used as y. Full-width loads still accept cache-policy flags. Plain loads refuse an empty
`dmask`, `r128`, `lwe`, `d16`, `tfe` and reserved bits. The fixture also checks that 3D `a16` and a
`dmask`, `r128`, `lwe`, `tfe` and reserved bits. The fixture also checks that 3D `a16` and a
patched 2D `a16` instruction carrying an unexpected NSA extension are refused, and that the runtime
and offline cache keys use the same revision. The check verifies translation on the CPU.

Expand All @@ -11975,6 +11975,17 @@ 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

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.

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.

```powershell
cmake --build --preset dev --target offscreen_check startup_d16_image_format_check startup_stream_consumer
ctest --preset dev --no-tests=error -R '^startup_(image_load_d16_(controls|warp|hardware)|d16_image_format.*|d16_srv_(warp|hardware))$' -V
```

## Startup translator: pixel depth export

The startup translator emits `SV_Depth` for an uncompressed `exp mrtz vN, off, off, off` in a straight-line pixel shader that still uses its entry execution mask. Color exports in the same shader keep their `SV_Target` outputs. Additional MRTZ channels, packed exports, multiple depth exports, branches and changes to the execution mask before the depth export are refused.
Expand Down
107 changes: 107 additions & 0 deletions src/startup_stubs/d16_image_format.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Experimental (docs/startup-runtime.md): bound SRV format validation and conversion mode selection
// for translated shaders that execute level-zero MIMG image_load with the d16 modifier (#2023, item D).
//
// gfx2hlsl writes 'd16srv <index> exact' and an entry constant 'udsrc <ud> d16unorm16 <index>' when SRV
// <index> is read by image_load ... d16. Only formats independently verified exact against RDNA 2 D16
// image_load are accepted:
// - D16SrvMode::float_exact (d16unorm16 = 0): 32-bit float views (including R32_FLOAT_X8X24_TYPELESS)
// and 8-bit UNORM views (256/256 exact under pack16_rne(Texture*<float4>.Load)).
// - D16SrvMode::unorm16 (d16unorm16 = 1): 16-bit UNORM views (65,536/65,536 exact when recovering the
// 16-bit code from Load and rounding u / 65535 directly to binary16, avoiding binary32 double-rounding
// at 0xffaf and 0xffef).
// Integer (UINT/SINT), typeless, depth-stencil-only, SNORM16, SRGB, block-compressed, packed-target
// stand-in, console extension and unknown/unbound views are refused.
#pragma once

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <d3d12.h>

#include <cstddef>
#include <cstdint>
#include <string_view>

namespace x2w_startup {

enum class D16SrvMode : std::uint8_t {
refused = 0,
float_exact = 1,
unorm16 = 2,
};

inline constexpr D16SrvMode d16_srv_mode(DXGI_FORMAT format)
{
switch (format) {
case DXGI_FORMAT_R32G32B32A32_FLOAT:
case DXGI_FORMAT_R32G32B32_FLOAT:
case DXGI_FORMAT_R32G32_FLOAT:
case DXGI_FORMAT_R32_FLOAT:
case DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS:
case DXGI_FORMAT_R8G8B8A8_UNORM:
case DXGI_FORMAT_R8G8_UNORM:
case DXGI_FORMAT_R8_UNORM:
case DXGI_FORMAT_A8_UNORM:
case DXGI_FORMAT_B8G8R8A8_UNORM:
case DXGI_FORMAT_B8G8R8X8_UNORM:
return D16SrvMode::float_exact;
case DXGI_FORMAT_R16G16B16A16_UNORM:
case DXGI_FORMAT_R16G16_UNORM:
case DXGI_FORMAT_R16_UNORM:
return D16SrvMode::unorm16;
default:
return D16SrvMode::refused;
}
}

inline constexpr bool d16_exact_srv_format(DXGI_FORMAT format)
{
return d16_srv_mode(format) != D16SrvMode::refused;
}

struct D16ImageSrvs {
std::uint32_t exact_mask = 0;

// Parses 'd16srv <index> exact' for 0 <= index < 32 without leading zeroes or duplicates.
bool read_manifest_line(std::string_view line)
{
constexpr std::string_view prefix = "d16srv ";
constexpr std::string_view suffix = " exact";
if (line.size() <= prefix.size() + suffix.size() ||
line.substr(0, prefix.size()) != prefix ||
line.substr(line.size() - suffix.size()) != suffix)
return false;
const std::string_view digits = line.substr(prefix.size(), line.size() - prefix.size() - suffix.size());
if (digits.empty() || digits.size() > 2 || (digits.size() == 2 && digits[0] == '0'))
return false;
unsigned index = 0;
for (char c : digits) {
if (c < '0' || c > '9') return false;
index = index * 10 + static_cast<unsigned>(c - '0');
}
if (index >= 32 || (exact_mask & (std::uint32_t{1} << index))) return false;
exact_mask |= std::uint32_t{1} << index;
return true;
}

bool any() const { return exact_mask != 0; }

bool requires_exact(unsigned index) const
{
return index < 32 && (exact_mask & (std::uint32_t{1} << index)) != 0;
}

template <class SrvVec>
bool valid_for_srvs(const SrvVec& srvs) const
{
if (!exact_mask) return true;
if (srvs.size() < 32 && (exact_mask >> srvs.size()) != 0) return false;
for (std::size_t i = 0; i < srvs.size() && i < 32; ++i) {
if (requires_exact(static_cast<unsigned>(i)) && (srvs[i].kind < 1 || srvs[i].kind > 4))
return false;
}
return true;
}
};

} // namespace x2w_startup
Loading
Loading