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: 4 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ cmake --build --preset dev --target startup_stream_consumer d3d12_xs_shim compil
ctest --preset dev --no-tests=error -V -R '^startup_native_graphics_draw_(hardware|warp)$'
```

## Implicit derivatives in direct compute

[Group shapes, lane mapping and readback fixtures](experiments/native-compute-implicit-derivatives.md)

## Slot 67 wave size in direct compute

State block A bit 20 of dword 4 selects the guest wave size.
Expand Down
34 changes: 34 additions & 0 deletions docs/experiments/native-compute-implicit-derivatives.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Implicit derivatives in direct compute

Groups with a multiple of four threads can use implicit derivatives when
they pass the existing wave guards: a group of more than 32 threads must be
a multiple of 32, and a wave64 group a multiple of 64. The DXIL module uses
`numthreads(X*Y*Z,1,1)` and validator version 1.8 only when it emits
compute derivatives. Guest local X, Y and Z are reconstructed from the
linear guest index.

A guest quad is four consecutive guest lanes. The emitter reconstructs
the linear guest index as host wave rank * 32 + host lane index. The
SM 6.6 derivatives spec section "1D quads" guarantees the matching position
within each quad. The full reconstruction also assumes that host lane
order follows thread order within a wave, including across quads.

A 6-thread wave32 group has an incomplete quad and refuses with
"implicit derivatives require a pixel stage". A 36-thread wave32 group
has complete quads and fails the earlier wave guard with "partial final
wave in multi-wave group". Implicit Gather with a constant sampler requires
that sampler to be fixed to mip zero (LOD range word zero).

```powershell
cmake --build --preset dev --target compile_dxil offscreen_check
ctest --preset dev --no-tests=error -j 1 -R '^native_compute_implicit_lod_' -V
```

On an NVIDIA GeForce RTX 5080 (Windows 11, Release), the hardware fixture
matches its CPU reference for 4-, 12-, 32- and 96-thread wave32 groups,
64- and 128-thread wave64 groups, and the multidimensional (8,4,1) and
(8,4,2) groups. Its other readbacks cover bias selecting mip 2, point depth
comparison at mip 1, both LOD query outputs, and the four taps of mip-zero
Gather. Bias and Gather each use the same CPU reference for their implicit
and explicit-LOD shaders. Inbox WARP reports the capability skip
"requires SM 6.6, WaveOps, and a 32-lane wave".
5 changes: 4 additions & 1 deletion src/dxil_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ class DxilModule {
<< "b, %dx.types.ResourceProperties { i32 14, i32 0 })\n";
return h;
}
// DXIL validator 1.8 accepts implicit-derivative operations in compute; every other
// module keeps validator 1.6.
unsigned validator_minor = 6;
Text finish_compute(unsigned x, unsigned y, unsigned z, const Text &stage = "cs") {
std::ostringstream out;
out << "target datalayout = "
Expand Down Expand Up @@ -293,7 +296,7 @@ class DxilModule {
"readnone\n"
"declare %dx.types.Handle "
"@dx.op.annotateHandle(i32,%dx.types.Handle,%dx.types.ResourceProperties) nounwind readnone\n";
out << "!llvm.ident = !{" << ident << "}\n!dx.version = !{" << version << "}\n!dx.valver = !{" << version
out << "!llvm.ident = !{" << ident << "}\n!dx.version = !{" << version << "}\n!dx.valver = !{" << node("i32 1, i32 " + std::to_string(validator_minor))
<< "}\n!dx.shaderModel = !{" << model << "}\n";
if (res != "null")
out << "!dx.resources = !{" << res << "}\n";
Expand Down
18 changes: 15 additions & 3 deletions src/gfx1013_direct_compute.inl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ struct DirectCompute {
std::map<std::pair<unsigned,unsigned>,string> descriptor_strides;
unsigned label_number = 0;
bool pixel_stage = false;
bool complete_compute_quads = false;
bool compute_derivatives = false;
// Straight-line graphics stages keep exact SSA definitions; a missing
// entry value is resolved through the container's stage signature.
std::function<string(unsigned)> graphics_input;
Expand Down Expand Up @@ -201,6 +203,7 @@ struct DirectCompute {
}
void initialize(unsigned user, unsigned flags, unsigned x, unsigned y, unsigned z) {
unsigned threads = x * y * z;
complete_compute_quads = threads % 4 == 0;
lane = ir.invoke("i32", "waveGetLaneIndex", 111, {}, "nounwind readnone");
lane_bit = ir.binary("shl", "1", lane);
write(126, ballot("true"));
Expand Down Expand Up @@ -828,8 +831,11 @@ struct DirectCompute {
const bool compare_gather = i.op == 79;
const bool compare = i.op == 40 || i.op == 47 || compare_gather;
const bool implicit = i.op == 32 || i.op == 40 || bias || query || i.op == 64;
if (implicit && !pixel_stage)
refuse(i.pc, "implicit derivatives require a pixel stage");
if (implicit && !pixel_stage) {
if (!complete_compute_quads)
refuse(i.pc, "implicit derivatives require a pixel stage");
compute_derivatives = true;
}
const bool gather = i.op == 64 || i.op == 71 || compare_gather;
const bool gather_lod = i.op == 68;
const bool sample = (!gather && implicit) || (compare && !gather) || gradient || i.op == 39 || i.op == 36,
Expand Down Expand Up @@ -3883,7 +3889,13 @@ struct DirectCompute {
const unsigned rank_words = x * y * z > 64 ? x * y * z : 0;
if ((uses_lds ? lds_words : 0) + rank_words + (uses_exchange ? exchange_words : 0) > 8192)
refuse(entry, "combined shared storage exceeds 32 KiB");
r.dxil_ir = ir.finish_compute(x, y, z);
// SM 6.6 forms consecutive linear quads for 1D groups. In 2D it forms
// spatial 2x2 quads, which can cross the guest's consecutive lane quads.
// initialize() still reconstructs guest IDs using the original dimensions.
// The wave64 split occurs at lane 32, a quad boundary.
if (compute_derivatives)
ir.validator_minor = 8;
r.dxil_ir = compute_derivatives ? ir.finish_compute(x * y * z, 1, 1) : ir.finish_compute(x, y, z);
return std::move(r);
}
};
15 changes: 15 additions & 0 deletions tests/cmake/native_compute_implicit_lod.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
if(TARGET compile_dxil AND EXISTS "${INTAKE_PYTHON}" AND
EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll")
set(implicit_lod_check "${CMAKE_SOURCE_DIR}/tests/native_compute_implicit_lod_check.py")
add_test(NAME native_compute_implicit_lod_controls COMMAND "${INTAKE_PYTHON}" -B
"${implicit_lod_check}" "$<TARGET_FILE:compile_dxil>" "${DXC_BIN_DIR}")
set_tests_properties(native_compute_implicit_lod_controls PROPERTIES TIMEOUT 330)
if(TARGET offscreen_check)
foreach(adapter IN ITEMS warp hardware)
add_test(NAME native_compute_implicit_lod_${adapter} COMMAND "${INTAKE_PYTHON}" -B
"${implicit_lod_check}" "$<TARGET_FILE:compile_dxil>" "${DXC_BIN_DIR}"
"$<TARGET_FILE:offscreen_check>" ${adapter})
set_tests_properties(native_compute_implicit_lod_${adapter} PROPERTIES TIMEOUT 540 SKIP_RETURN_CODE 77)
endforeach()
endif()
endif()
41 changes: 36 additions & 5 deletions tests/compute_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ constexpr UINT uavs = 13, srvs = 21;
// The image case header packs the dispatch count and a format/capability selector. The payload
// carries raw texels, one word each for RGBA8 and scalar R32, or four for RGBA32.
constexpr UINT texture_rgba8_flag = 1u << 24;
// Read-only image case selectors. The dispatch dimensions each fit in three bits.
constexpr UINT texture_mip_zero_sampler_flag = 1u << 22;
constexpr UINT texture_comparison_flag = 1u << 23;
constexpr UINT texture_atomic_uint_flag = 1u << 25;
constexpr UINT texture_atomic_sint_minmax_flag = 1u << 26;
constexpr UINT texture_atomic_uint_minmax_flag = 1u << 27;
Expand Down Expand Up @@ -460,8 +463,13 @@ void check_compute_texture_file(ID3D12Device* device, const char* path, const ch
const bool rgba8 = (store.groups & texture_rgba8_flag) != 0;
const bool rgba32_float = (store.groups & texture_rgba32_float_flag) != 0;
const bool view_mip1 = (store.groups & texture_view_mip1_flag) != 0;
store.groups &= ~(texture_rgba8_flag | texture_rgba32_float_flag | texture_view_mip1_flag);
const UINT base_words = texture_width * texture_height * (rgba8 ? 1 : texture_channels);
const bool mip_zero = (store.groups & texture_mip_zero_sampler_flag) != 0;
const bool comparison = (store.groups & texture_comparison_flag) != 0;
store.groups &= ~(texture_rgba8_flag | texture_rgba32_float_flag | texture_view_mip1_flag |
texture_mip_zero_sampler_flag | texture_comparison_flag);
const auto format = comparison ? DXGI_FORMAT_R32_FLOAT : rgba8 ? DXGI_FORMAT_R8G8B8A8_UINT
: rgba32_float ? DXGI_FORMAT_R32G32B32A32_FLOAT : DXGI_FORMAT_R32G32B32A32_UINT;
const UINT base_words = texture_width * texture_height * texture_words_per_texel(format);
std::vector<UINT> texels(base_words);
source.read(reinterpret_cast<char*>(texels.data()), static_cast<std::streamsize>(texels.size() * sizeof(UINT)));
// Older cases stop after the base image. A mip case appends levels one and two.
Expand All @@ -472,11 +480,34 @@ void check_compute_texture_file(ID3D12Device* device, const char* path, const ch
}
if (!source || source.peek() != std::char_traits<char>::eof() || store.groups >> 24 || !store.axis(0) ||
store.axis(0) > 5 || store.axis(1) > 5 || store.axis(2) > 5 || (rgba8 && rgba32_float) ||
(comparison && (rgba8 || rgba32_float || mip_zero)) ||
(view_mip1 && texels.size() == base_words))
throw std::runtime_error("Invalid compute image load reference");
const auto format = rgba8 ? DXGI_FORMAT_R8G8B8A8_UINT
: rgba32_float ? DXGI_FORMAT_R32G32B32A32_FLOAT : DXGI_FORMAT_R32G32B32A32_UINT;
const auto program = create_compute_program(device, shader, {compute_tables, sizeof(compute_tables)});
D3D12_SHADER_BYTECODE root{compute_tables, sizeof(compute_tables)};
ComPtr<ID3DBlob> case_root;
if (comparison || mip_zero) {
ComPtr<ID3D12VersionedRootSignatureDeserializer> embedded;
check(D3D12CreateVersionedRootSignatureDeserializer(root.pShaderBytecode, root.BytecodeLength,
IID_PPV_ARGS(&embedded)), "Read embedded compute table signature");
const D3D12_VERSIONED_ROOT_SIGNATURE_DESC* original = nullptr;
check(embedded->GetRootSignatureDescAtVersion(D3D_ROOT_SIGNATURE_VERSION_1_1, &original),
"Get v1.1 compute table signature");
auto desc = *original;
if (desc.Desc_1_1.NumStaticSamplers != 1)
throw std::runtime_error("Expected one static sampler in the compute table signature");
auto sampler = desc.Desc_1_1.pStaticSamplers[0];
if (comparison) {
sampler.Filter = D3D12_FILTER_COMPARISON_MIN_MAG_MIP_POINT;
sampler.ComparisonFunc = D3D12_COMPARISON_FUNC_LESS;
} else {
sampler.Filter = D3D12_FILTER_MIN_MAG_LINEAR_MIP_POINT;
sampler.MaxLOD = 0;
}
desc.Desc_1_1.pStaticSamplers = &sampler;
check(D3D12SerializeVersionedRootSignature(&desc, &case_root, nullptr), "Serialize compute case signature");
root = {case_root->GetBufferPointer(), case_root->GetBufferSize()};
}
const auto program = create_compute_program(device, shader, root);
run_compute(device, true, program, false, &store, &texels, format,
nullptr, false, false, view_mip1 ? 1 : 0);
}
Expand Down
Loading
Loading