diff --git a/CMakeLists.txt b/CMakeLists.txt index a43c3ae223..467b16b0b3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4967,6 +4967,12 @@ if(EXISTS "${INTAKE_PYTHON}") add_test(NAME startup_compute_dispatch_${adapter} COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_compute_dispatch_check.py" "$" "${DXC_EXECUTABLE}" ${adapter}) + if(TARGET compile_dxil AND EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll") + add_test(NAME startup_compute_dispatch_texture_native_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_compute_dispatch_texture_check.py" "$" + "${DXC_EXECUTABLE}" ${adapter} "$" "${DXC_BIN_DIR}") + set_tests_properties(startup_compute_dispatch_texture_native_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77) + endif() add_test(NAME startup_compute_dispatch_texture_${adapter} COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_compute_dispatch_texture_check.py" "$" "${DXC_EXECUTABLE}" ${adapter}) diff --git a/docs/experiments/startup-compute-native-dispatch.md b/docs/experiments/startup-compute-native-dispatch.md index c35093e7e0..e854c8b154 100644 --- a/docs/experiments/startup-compute-native-dispatch.md +++ b/docs/experiments/startup-compute-native-dispatch.md @@ -14,13 +14,15 @@ cache and no offline translator, and compares all 320 output words with a CPU re 3. The entry values and views fill the same `ComputeTranslation` a published `.txt` fills. The existing dispatch path then resolves the table descriptors, sets the resource states and records the Dispatch. -Accepted: root constants, a raw table buffer loaded per lane and a raw table buffer stored to. These are the -bindings the fixture runs; a later step samples a 2D texture through constant samplers -([note](startup-compute-native-samplers.md)). Refused: every other view kind (a scalar buffer load included), -a sampler read from memory, more than -32 entry values or inputs or 8 outputs, an adapter without 32-lane waves, and whatever the frontend refuses. The -stand-in logs the first 16 refusals of a process with their reason. A refused key is left to a published -translation. The in-process translation is tried once per key and before a published `.txt`. +Accepted bindings include root constants, scalar and per-lane raw table buffer inputs, raw buffer outputs, +supported half-buffer outputs, 2D texture inputs, four-channel float and packed half4 texture outputs +(1D, 2D, 2D-array and 3D), and exact-width float texture outputs (2D, 2D-array and 3D). +The native route samples 2D textures through constant samplers, including comparison samplers +([note](startup-compute-native-samplers.md)). The stand-in refuses samplers read from memory, indexed views, +other unsupported view shapes, more than 32 entry values or inputs or 8 outputs, and adapters that lack wave32 +or report a shader model below 6.6. It logs the first 16 refusals of a process +with their reason. A refused key is left to a published translation. The in-process translation is tried +once per key and before a published `.txt`. `NativeRecording` and `NativeDispatchStates` are not used. The dispatch path already allocates the descriptors and tracks the states of these buffers. @@ -46,7 +48,7 @@ decoder, and the consumer's `dispatch-records32` mode writes the table with the 5. Order: the first program runs again with its published translation in the cache. The log still names the native translation, and all 320 words match. -Without 32-lane waves, step 1 instead checks that the native translation is refused by that name with every word +Without wave32 or shader model 6.6, step 1 instead checks the named capability refusal with every word untouched. Step 2 is left out, steps 3 and 4 run, step 5 runs the first program through its published translation, and the check exits 77. @@ -82,8 +84,7 @@ ctest --preset dev --no-tests=error -V -R '^(startup_compute_dispatch(_.*)?|star ## Limits - Compute only. Graphics pipelines are not translated in-process. -- A scalar buffer load, a sampler read from memory and the typed, half and indexed views are refused. This is the - in-process route. The published-manifest route (`.txt` from `compile_dxil --requests`) binds a +- The published-manifest route (`.txt` from `compile_dxil --requests`) also binds a title's 1D, 2D, 2D-array and 3D texture SRV (`srv N tex1d|tex2d|tex2da|tex3d table R B`). For 1D, 2D-array and 3D the title's view must be described, have the manifest's dimension and fit its texture, otherwise the Dispatch is refused (on a dimension mismatch the draw path binds a null view instead). "Fits" is diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 73ad397ac2..369c8f47ed 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -1008,19 +1008,19 @@ pipelines, and the cache can start empty: the request files the stand-in wrote a against their key, translated and compiled to DXIL in the process. The compiler's own cache goes to `\native`. Nothing is published as `.txt`. -The existing dispatch path records the result, and a program is accepted only with the bindings a -fixture has run through it: root constants, raw table buffers loaded per lane (`buffer_load_dword`) -and raw table buffers stored to, with their descriptors in 32-byte table records. A 2D texture can be -sampled through samplers whose four words are constants: they become static samplers with the -descriptor's MIN_LOD and MAX_LOD, or comparison samplers -([fixture and results](experiments/startup-compute-native-samplers.md)). Anything else (a -scalar buffer load, a sampler read from memory, a typed or indexed view, an instruction the compiler does -not lower) is refused. Programs published as `.txt` also bind 1D, 2D, 2D-array and 3D texture SRVs -(`srv N tex1d|tex2d|tex2da|tex3d table R B`) from the title's described view of that dimension, or are -refused by name; cube texture SRVs are not published. The first 16 refusals of a process are logged as +The dispatch path binds root constants and views in 32-byte table records. Buffer views include +scalar SRVs, per-lane raw SRVs, raw UAVs and supported half-buffer UAVs. Texture views include 2D +SRVs, four-channel float and packed half4 UAVs (1D, 2D, 2D-array and 3D), and exact-width float UAVs +(2D, 2D-array and 3D). The in-process path binds samplers whose four words are constants as static +samplers with the descriptor's MIN_LOD and MAX_LOD, or as comparison samplers +([fixture and results](experiments/startup-compute-native-samplers.md)). It refuses samplers read from +memory, indexed views, and unsupported view shapes or instructions. Programs published as `.txt` also bind root buffer SRVs and 1D, 2D, +2D-array and 3D texture SRVs (`srv N tex1d|tex2d|tex2da|tex3d table R B`) from the title's described view +of that dimension, or are refused by name. Cube texture SRVs are not published. The first 16 +refusals of a process are logged as `compute pipeline not translated natively: `. A refused key is left to the -published translations, so `-Translate` can run beside it for those pipelines. A direct module declares -wave size 32, so an adapter without 32-lane waves refuses every program by that name. The in-process +published translations, so `-Translate` can run beside it for those pipelines. A direct module requires +shader model 6.6 and wave32. The in-process translation is tried once per key and before a published `.txt`. It covers compute only; graphics pipelines still need a published translation. @@ -1034,6 +1034,15 @@ pick up a rebuilt output (by the code path; no test covers the reload). Compute Check with `ctest --preset dev -R '^startup_compute_dispatch_native_(warp|hardware)$' -V` ([`tests/startup_compute_dispatch_native_check.py`](../tests/startup_compute_dispatch_native_check.py); [route, controls and results](experiments/startup-compute-native-dispatch.md)). +The #2958/#2993 fixture reads back a 3D float store fed by a scalar buffer SRV and a 2D texture SRV. +The consumer creates the views with the adapter's descriptor increment and copies their tokens +into a packed guest table when the increment exceeds 32 bytes. A zero-size W range must translate +natively, be refused at Dispatch, and leave all 6,144 volume words untouched. +Targeted in-process readbacks also check a mip-one 1D float store, a 2D-array float store into +relative slice 1 (physical slice 2), and a packed D16 store into an RGBA16_FLOAT texture with +round-to-nearest-even input conversion. Each case checks every output value, including sentinels +outside the store region. On adapters without wave32 or shader model 6.6 the cases check the named refusal +and untouched output before the test returns 77. ### Compute pipeline requests @@ -4086,3 +4095,21 @@ before any desktop copy, leaving a sentinel-filled readback buffer unchanged. On list refuses boxes with an unaligned right or bottom edge, copies the aligned 4x4 block, and reads back all eight block bytes with the next byte unchanged. Reproduce the portable and wrapped-list checks with `ctest --preset dev --no-tests=error -R '^startup_list_copy_texture' -V`. + +### Offline compute manifests for 3D and 1D UAVs + +For offline compute requests, the C++ formatter emits `uav N tex3d float table P OFF channels 4` and +`uav N tex1d float table P OFF channels 4`. The stand-in validates UAV view +dimensions and mip levels, including the W-slice range within the selected 3D mip. + +The native offline fixture compiles 1D, 3D and 3D NSA programs, checks the published DXIL and +manifest, and compares every mip-one word after Dispatch. It also checks that mismatched views, +an invalid 3D W range and an invalid 1D mip leave the output unchanged. An adapter without +wave32 or shader model 6.6 can verify the native manifests before returning a capability skip for Dispatch. +After a published PSO creation failure, the runtime keeps PSO creation as the reason and appends +the adapter observation when available: `(adapter has no 32-lane waves)`, `(adapter wave capability query failed)`, or +`(adapter shader model below 6.6)`. A published manifest carries no wave size. The native fixture uses the +reported wave32 and shader model limits for its capability skips. The separate texture dispatch +fixture exercises the existing portable Python-published path. + +`ctest --preset dev --no-tests=error -R '^startup_compute_dispatch_texture_(native_)?(warp|hardware)$' -V` diff --git a/src/direct_compute_cache.cpp b/src/direct_compute_cache.cpp index b9a06c8246..3ec2177cff 100644 --- a/src/direct_compute_cache.cpp +++ b/src/direct_compute_cache.cpp @@ -996,9 +996,16 @@ std::optional format_compute_runtime_manifest(const NativeComputeSh uav_lines << "uav " << b.shader_register << ' ' << dimension << " float table " << b.root_parameter << ' ' << b.byte_offset << " channels " << channels << '\n'; } else if ((b.kind == ComputeViewKind::texture_uav || b.kind == ComputeViewKind::half4_texture_uav) && - (b.image_dimension == 1 || b.image_dimension == 5 || - (b.kind == ComputeViewKind::texture_uav && b.image_dimension == 2)) && b.array_count == 1) { - uav_lines << "uav " << b.shader_register << (b.image_dimension == 5 ? " tex2da" : b.image_dimension == 2 ? " tex3d" : " tex2d") + b.array_count == 1) { + const char *tex; + switch (b.image_dimension) { + case 0: tex = "tex1d"; break; + case 1: tex = "tex2d"; break; + case 2: tex = "tex3d"; break; + case 5: tex = "tex2da"; break; + default: return refuse(view_shape("UAV", b)); + } + uav_lines << "uav " << b.shader_register << ' ' << tex << " float table " << b.root_parameter << ' ' << b.byte_offset << " channels 4\n"; } else { return refuse(view_shape("UAV", b)); diff --git a/src/startup_stubs/d3d12_compute_dispatch.inl b/src/startup_stubs/d3d12_compute_dispatch.inl index 6df05afb76..06929c7577 100644 --- a/src/startup_stubs/d3d12_compute_dispatch.inl +++ b/src/startup_stubs/d3d12_compute_dispatch.inl @@ -106,6 +106,22 @@ std::unordered_map& compute_translations() { static std LONG compute_dispatches_translated, compute_dispatches_refused, compute_dispatches_untranslated, compute_dispatches_unmodelled; LONG compute_inputs_gpu_copied, compute_inputs_snapshot, compute_inputs_snapshot_kib; // inputs by path: this list's GPU copy, a title snapshot +// Whether the adapter runs 32-lane waves: 1 yes, 0 no, -1 the capability query failed. +int adapter_32_lane_waves() +{ + D3D12_FEATURE_DATA_D3D12_OPTIONS1 waves{}; + if (FAILED(device_of(the_device)->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS1, &waves, sizeof(waves)))) + return -1; + return waves.WaveOps && waves.WaveLaneCountMin <= 32 && waves.WaveLaneCountMax >= 32; +} + +bool adapter_reports_shader_model_below_6_6() +{ + D3D12_FEATURE_DATA_SHADER_MODEL model{D3D_SHADER_MODEL_6_6}; + return SUCCEEDED(device_of(the_device)->CheckFeatureSupport(D3D12_FEATURE_SHADER_MODEL, &model, sizeof(model))) && + model.HighestShaderModel < D3D_SHADER_MODEL_6_6; +} + // The root signature and pipeline of a translation whose entry values and views are filled in. Returns why it is // unavailable, or null when it is ready. const char* create_compute_pipeline(ComputeTranslation& tr, const void* code, std::size_t bytes) @@ -276,7 +292,17 @@ const char* load_compute_translation(ComputeTranslation& tr) } x2w_startup::ComPtr code; if (!cache_path(path, L"", tr.key, L".cs.cso") || FAILED(D3DReadFileToBlob(path, &code))) return "no compiled program"; - if (const char* failure = create_compute_pipeline(tr, code->GetBufferPointer(), code->GetBufferSize())) return failure; + if (const char* failure = create_compute_pipeline(tr, code->GetBufferPointer(), code->GetBufferSize())) { + // The manifest does not declare a wave size. Report the adapter capability alongside the PSO failure. + if (!std::strcmp(failure, "pipeline creation failed")) { + const int waves = adapter_32_lane_waves(); + if (waves < 0) return "pipeline creation failed (adapter wave capability query failed)"; + if (!waves) return "pipeline creation failed (adapter has no 32-lane waves)"; + if (adapter_reports_shader_model_below_6_6()) + return "pipeline creation failed (adapter shader model below 6.6)"; + } + return failure; + } log("x2w-draw compute pipeline %016llx translated: %zu entry values, %zu inputs, %zu outputs", tr.key, tr.uds.size(), tr.srvs.size(), tr.uavs.size()); return nullptr; @@ -284,9 +310,8 @@ const char* load_compute_translation(ComputeTranslation& tr) // X2W_NATIVE_SHADERS=: the first Dispatch of a slot 67 request translates // it in this process with the C++ compiler (src/gfx1013_compute_native.cpp), before a published .txt is read -// and with no offline translator running. The dispatch path below binds the result, so a program is accepted only -// with root constants, raw table buffers and exact-width typed texture outputs. The constant sampler words the -// compiler resolved become static samplers. +// and with no offline translator running. The dispatch path below binds root constants and supported table views. +// The constant sampler words the compiler resolved become static samplers. // A refusal is logged and leaves the key to the published translations. draw_lock held. Returns whether tr is ready. bool translate_compute_natively(ComputeTranslation& tr) { @@ -342,38 +367,46 @@ bool translate_compute_natively(ComputeTranslation& tr) std::sort(program.bindings.begin(), program.bindings.end(), [](const auto& a, const auto& b) { return a.shader_register < b.shader_register; }); for (const auto& view : program.bindings) { + using Kind = x2w::ComputeViewKind; const unsigned channels = x2w::exact_texture_uav_channels(view.kind); - const bool packed_x = view.kind == x2w::ComputeViewKind::packed_d16_x_uav; - const bool half_typed = packed_x || view.kind == x2w::ComputeViewKind::half_buffer_uav; + const bool four_channel = view.kind == Kind::texture_uav || view.kind == Kind::half4_texture_uav; + const bool texture_output = four_channel || channels; + const bool packed_x = view.kind == Kind::packed_d16_x_uav; + const bool half_typed = packed_x || view.kind == Kind::half_buffer_uav; // A comparison sample reads its 2D texture through the same kind of view as a plain one. - const bool texture_input = (view.kind == x2w::ComputeViewKind::texture_srv || - view.kind == x2w::ComputeViewKind::comparison_texture_srv) && + const bool texture_input = (view.kind == Kind::texture_srv || view.kind == Kind::comparison_texture_srv) && view.image_dimension == 1; - const bool volume = view.kind == x2w::ComputeViewKind::texture_uav && view.image_dimension == 2; - const bool output = view.kind == x2w::ComputeViewKind::raw_buffer_uav || half_typed || volume || channels; + const bool output = texture_output || view.kind == Kind::raw_buffer_uav || half_typed; + auto dimension = D3D12_UAV_DIMENSION_UNKNOWN; + if (texture_output) { + switch (view.image_dimension) { + case 0: if (four_channel) dimension = D3D12_UAV_DIMENSION_TEXTURE1D; break; + case 1: dimension = D3D12_UAV_DIMENSION_TEXTURE2D; break; + case 2: dimension = D3D12_UAV_DIMENSION_TEXTURE3D; break; + case 5: dimension = D3D12_UAV_DIMENSION_TEXTURE2DARRAY; break; + } + } auto& slots = output ? tr.uavs : tr.srvs; - if ((!output && !texture_input && view.kind != x2w::ComputeViewKind::raw_vector_srv && !x2w::accepts_cbv_window(view.kind)) || view.array_count != 1 || + const bool supported = view.kind == Kind::raw_buffer_uav || half_typed || view.kind == Kind::raw_vector_srv || + x2w::accepts_cbv_window(view.kind) || texture_input || + dimension != D3D12_UAV_DIMENSION_UNKNOWN; + if (!supported || view.array_count != 1 || view.root_parameter >= 16 || view.shader_register != slots.size()) throw std::runtime_error("view kind " + std::to_string(static_cast(view.kind)) + " is not bound for a natively translated program"); - slots.push_back({static_cast(view.root_parameter), static_cast(view.byte_offset), volume || texture_input}); - if (volume) slots.back().uav_dimension = D3D12_UAV_DIMENSION_TEXTURE3D; - // The dispatch path takes an input of any other dimension for a described 1D, 2D-array or 3D view. - if (texture_input) slots.back().srv_dimension = D3D12_SRV_DIMENSION_TEXTURE2D; - slots.back().half_typed_bytes = half_typed ? view.required_bytes : 0; - slots.back().packed_d16_x = packed_x; + slots.push_back({static_cast(view.root_parameter), static_cast(view.byte_offset), + texture_input || texture_output}); + auto& slot = slots.back(); + slot.uav_dimension = dimension; + if (texture_input) slot.srv_dimension = D3D12_SRV_DIMENSION_TEXTURE2D; // the only input dimension bound here + if (channels) + slot.channels = channels; + slot.half_typed_bytes = half_typed ? view.required_bytes : 0; + slot.packed_d16_x = packed_x; // An MTBUF byte window needs identity channel order, as on the published route (identity_dst_sel). - slots.back().requires_identity_dst_sel = view.requires_identity_dst_sel; + slot.requires_identity_dst_sel = view.requires_identity_dst_sel; if (x2w::accepts_cbv_window(view.kind)) - slots.back().scalar_window = view.required_bytes; - if (channels) { - auto& slot = slots.back(); - slot.texture = true; - slot.channels = channels; - slot.uav_dimension = view.image_dimension == 2 ? D3D12_UAV_DIMENSION_TEXTURE3D : - view.image_dimension == 5 ? D3D12_UAV_DIMENSION_TEXTURE2DARRAY : - D3D12_UAV_DIMENSION_TEXTURE2D; - } + slot.scalar_window = view.required_bytes; } // Static samplers in register order, flagged as the manifest writer names them: smpc, else smpl, else smp. // The compiler has already refused sampler words that are not constants (a sampler read from memory). @@ -384,11 +417,11 @@ bool translate_compute_natively(ComputeTranslation& tr) } if (tr.uds.size() > 32 || tr.srvs.size() > 32 || tr.uavs.size() > 8) throw std::runtime_error("more entry values, inputs or outputs than the dispatch path binds"); - // A direct module declares wave size 32 (src/dxil_module.h): without it the pipeline cannot be created. - D3D12_FEATURE_DATA_D3D12_OPTIONS1 waves{}; - if (FAILED(device_of(the_device)->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS1, &waves, sizeof(waves))) || - !waves.WaveOps || waves.WaveLaneCountMin > 32 || waves.WaveLaneCountMax < 32) + // Direct modules use shader model 6.6 with WaveSize(32). + if (adapter_32_lane_waves() != 1) throw std::runtime_error("the adapter has no 32-lane waves"); + if (adapter_reports_shader_model_below_6_6()) + throw std::runtime_error("the adapter reports shader model below 6.6"); // Never released: a static destructor would release DXC after its module may have detached. static x2w::ShaderDxilCompiler* const compiler = new x2w::ShaderDxilCompiler(x2w::ShaderDxilCompiler::runtime_worker(sdk)); diff --git a/tests/native_compute_manifest_reasons.cpp b/tests/native_compute_manifest_reasons.cpp index 92d7147f14..fdaf29a82e 100644 --- a/tests/native_compute_manifest_reasons.cpp +++ b/tests/native_compute_manifest_reasons.cpp @@ -93,24 +93,31 @@ int main() { ok ? "" : manifest ? "a manifest" : reason.c_str()); failures += !ok; } - // A texture SRV is published under the name of its dimension: the line the runtime's reader takes. + // Texture bindings are published under the dimension names the runtime's reader takes. const struct { + unsigned binding; unsigned image_dimension; const char *line; } published[] = { - {0, "srv 0 tex1d table 1 64\n"}, - {1, "srv 0 tex2d table 1 64\n"}, - {2, "srv 0 tex3d table 1 64\n"}, - {5, "srv 0 tex2da table 1 64\n"}, + {0, 0, "srv 0 tex1d table 1 64\n"}, + {0, 1, "srv 0 tex2d table 1 64\n"}, + {0, 2, "srv 0 tex3d table 1 64\n"}, + {0, 5, "srv 0 tex2da table 1 64\n"}, + {1, 0, "uav 0 tex1d float table 1 32 channels 4\n"}, + {1, 1, "uav 0 tex2d float table 1 32 channels 4\n"}, + {1, 2, "uav 0 tex3d float table 1 32 channels 4\n"}, + {1, 5, "uav 0 tex2da float table 1 32 channels 4\n"}, }; for (const auto &c : published) { auto program = base(); - program.bindings[0].kind = K::texture_srv; - program.bindings[0].image_dimension = c.image_dimension; + auto &binding = program.bindings[c.binding]; + binding.kind = c.binding == 0 ? K::texture_srv : K::texture_uav; + binding.image_dimension = c.image_dimension; reason = "unset"; const auto manifest = format_compute_runtime_manifest(program, &reason); + const char *buffer_line = c.binding == 0 ? "srv 0 buf table 1 64\n" : "uav 0 table 1 32\n"; std::string wanted = expected; - wanted.replace(wanted.find("srv 0 buf table 1 64\n"), std::strlen("srv 0 buf table 1 64\n"), c.line); + wanted.replace(wanted.find(buffer_line), std::strlen(buffer_line), c.line); const bool ok = manifest && *manifest == wanted && reason == "unset"; std::printf("%s publishes %s", ok ? "ok " : "FAIL", c.line); if (!ok) diff --git a/tests/startup_compute_dispatch_native_check.py b/tests/startup_compute_dispatch_native_check.py index 19842660f1..5bd50f3539 100644 --- a/tests/startup_compute_dispatch_native_check.py +++ b/tests/startup_compute_dispatch_native_check.py @@ -25,12 +25,13 @@ comparison results. Every volume word is compared with a CPU model of the sampler words. Control: with the first sampler's words loaded from memory (the table) instead, the C++ compiler refuses the program and the volume keeps its words. +6. Table views: a scalar buffer read and a 2D image load feed a 3D image store. A sample through a table-loaded + sampler and a zero-size UAV W range each leave the volume untouched. +7. Texture outputs: 1D float, 2D-array float, and 2D packed D16 stores match CPU references. -A direct compute module declares wave size 32. On an adapter without 32-lane waves, steps 1 and 5 instead check that -the native translation is refused by that name with the output untouched (the memory sampler control runs as -written), steps 2 and 3 run as written, step 4 runs the -first program through its published translation, and the check exits 77. Exit 77 also when the adapter is not -available. +A direct compute module requires shader model 6.6 and wave32. Without these, steps 1 and 5 through 7 check that the +native translation is refused by that name with the output untouched. The memory sampler controls and steps 2 and 3 +run as written, step 4 uses the published translation, and the check exits 77. Exit 77 also when the adapter is unavailable. """ import os from pathlib import Path @@ -42,6 +43,7 @@ import startup_gfx2hlsl_compute_check as C import startup_compute_dispatch_texture_check as T import startup_compute_image_store_check as A +import startup_compute_image_store_d16_check as D import startup_gfx2hlsl_image_store_3d1d_check as V from startup_compute_dispatch_check import dispatch, lines, prepare_case from startup_compute_translate_check import state_a @@ -50,7 +52,6 @@ LOAD_X8 = bytes.fromhex('05030cf4' '000000fa') LOADS_X4 = bytes.fromhex('050308f4' '000000fa' '050408f4' '200000fa') RECORD_LOADS = ['s_load_dwordx4 s[12:15], s[10:11], 0x0', 's_load_dwordx4 s[16:19], s[10:11], 0x20'] -NO_WAVES = 'not translated natively: the adapter has no 32-lane waves' # Step 5. Sampler words [address, LOD limits, filter, border] in register order: U address mode in bits 2:0 over # clamp V, MIN_LOD and MAX_LOD as 4.8 fixed point in bits 11:0 and 23:12 of word 1, point filters. WRAP, MIRROR, CLAMP, POINT_MIP = 0, 1, 2, 1 << 26 @@ -76,6 +77,12 @@ ] MEMORY_SAMPLER = 's_load_dwordx4 s[40:43], s[12:13], 0x60' MEMORY_SAMPLER_REFUSAL = 'nonconstant sampler' +CAPABILITY_REFUSALS = ( + 'not translated natively: the adapter has no 32-lane waves', + 'not translated natively: the adapter reports shader model below 6.6', +) +TEXTURE_DISPATCH = 'Dispatch(2, 1, 1)' +ZERO_W_REFUSAL = 'an output 3D texture W range is outside the mip' def mismatch(got, expected, result): @@ -107,7 +114,7 @@ def scalar_window_case(consumer, dxc_dir, adapter): source.write_bytes(struct.pack(f'<{C.WORDS}I', *data)) expected = [value] * 288 + [C.SENTINEL] * 32 got, result = dispatch(directory, consumer, env, 'dispatch-scalar-window') - if lines(result, NO_WAVES): + if any(lines(result, reason) for reason in CAPABILITY_REFUSALS): assert got == [C.SENTINEL] * C.WORDS return assert not mismatch(got, expected, result), mismatch(got, expected, result) @@ -161,18 +168,10 @@ def texel(sampler, lod): return texel(0, 0), texel(1, 1.25), texel(2, 0), 0x3f800000 if passed else 0 -def sampled_views(directory, consumer, dxc_dir, adapter, no_waves): +def sampled_views(directory, consumer, dxc_dir, adapter, capability_refusal): cache, env = prepare_case(directory, adapter) env['X2W_NATIVE_SHADERS'] = dxc_dir - # The guest table: two SRV records (the texture is record 1), then one UAV in record 2. - blob = bytearray(C.root_blob(3, table_range=True)[:-24]) - blob.extend(struct.pack('<12I', 0, 2, 0, 0, 0, 0, 1, 1, 0, 0, 0, 2)) - payload = 44 # DXBC header, part offset, and RTS0 part header - struct.pack_into(' [two-mips] +usage: startup_compute_dispatch_texture_check.py [two-mips | compile_dxil SDK] Texture output: the consumer creates the image store program of startup_compute_image_store_d16_check.py as a device slot 67 pipeline, fills a 32x8 RGBA16_FLOAT texture with sentinel halves, creates an unordered access view of @@ -14,6 +14,7 @@ with one logged reason, every half keeps its sentinel. A fifth run binds the same program under a manifest edited to declare no entry values. The 1D and 3D float texture cases check mip-one readback, a mismatched view, and an invalid 3D W range. +With compile_dxil, run only the 1D and 3D cases using native offline manifests and bytecode. Texture input: case A of startup_gfx2hlsl_compute_check.py with a level-zero sample of a 2D texture XORed into the stored word (a static sampler, a texture view at table byte 64). The consumer adds a 1x1 RGBA8 texture whose red @@ -234,11 +235,17 @@ def store_two_mips(directory, consumer, dxc, env): print(f'two mip outputs: {len(got)} halves match; swapping views changes {changed} halves; debug layer: {layer}') -def store_3d1d(directory, consumer, dxc, env, form): +def store_3d1d(directory, consumer, dxc, env, form, compiler=None, sdk=None): cache, base = prepare(directory) env = dict(base, **env) - (directory / 'compute_root').write_bytes(C.root_blob(3, table_range=True)) - env['X2W_TEST_COMPUTE_ROOT'] = str(directory / 'compute_root') + env.pop('X2W_NATIVE_SHADERS', None) + root = directory / 'compute_root' + root_blob = C.root_blob(3, table_range=True) + if compiler is not None: + # Use an explicit UAV(u0) range at guest offset 2 for the native frontend. + root_blob = root_blob[:-24] + struct.pack('<6I', 1, 1, 0, 0, 0, 2) + root.write_bytes(root_blob) + env['X2W_TEST_COMPUTE_ROOT'] = str(root) (directory / 'program').write_bytes(V.program(form)) (directory / 'data').write_bytes(DATA) (directory / 'state').write_bytes(state_a(V.group(form), V.A.RSRC2)) @@ -257,11 +264,26 @@ def store_3d1d(directory, consumer, dxc, env, form): got, result = run(directory, consumer, env, mode, layout) assert got == initial and len(lines(result, 'Dispatch(2, 1, 1) refused: no translation')) == 1 key = int(lines(result, 'x2w-draw compute request')[0].split()[3], 16) - translate(cache, dxc, key) + if compiler is None: + translate(cache, dxc, key) + else: + compiled = subprocess.run([compiler, sdk, directory / 'frontend', 'gfx1013-direct-cs@0', '--requests', + cache, cache], capture_output=True, text=True) + assert compiled.returncode == 0 and 'requests=1 compiled=1 refused=0' in compiled.stdout, ( + compiled.stdout, compiled.stderr) + assert (cache / f'{key:016x}.cs.cso').read_bytes().startswith(b'DXBC') manifest = (cache / f'{key:016x}.txt').read_text().splitlines() dim = '3d' if volume else '1d' assert f'uav 0 tex{dim} float table 1 64 channels 4' in manifest, manifest got, result = run(directory, consumer, env, mode, layout) + if compiler is not None: + for capability in ('adapter has no 32-lane waves', 'adapter shader model below 6.6'): + reason = f'pipeline creation failed ({capability})' + if lines(result, f'not translated: {reason}'): + assert got == initial and len(lines(result, f'Dispatch(2, 1, 1) refused: {reason}')) == 1, ( + result.stderr[-3000:]) + print(f'{form}: native DXIL and manifest verified; dispatch skipped ({capability})') + return False assert len(lines(result, 'Dispatch(2, 1, 1) translated')) == 1, result.stderr[-3000:] assert got == expected, (form, sum(a != b for a, b in zip(got, expected, strict=True))) clean(result) @@ -280,6 +302,7 @@ def store_3d1d(directory, consumer, dxc, env, form): assert len(refusal) == 1 and refusal[0].endswith(reason), result.stderr[-3000:] assert got == untouched and not lines(result, 'Dispatch(2, 1, 1) translated') print(f'{form}: {len(expected)} mip-one words match; incompatible views refused with sentinels intact') + return True # v_xor_b32 v5, v5, v12: the assembled `v_xor_b32 v5, v5, v6` of case A with its second source register changed @@ -554,14 +577,20 @@ def uniform_image_load_mip(directory, consumer, dxc, env): f'x=0 and mip=1 controls each differ at {written} words; debug layer: {layer}') -def main(consumer, dxc, adapter, mode=None): +def main(consumer, dxc, adapter, *, compiler=None, sdk=None, two_mips=False): env = dict(X2W_D3D12_LIST_NOOP='1', X2W_D3D12_DEBUG='1') # guest lists; debug-layer messages go to the log if adapter == 'hardware': env['X2W_TEST_DISPATCH_HARDWARE'] = '1' with tempfile.TemporaryDirectory() as temporary: directory = Path(temporary) - if mode == 'two-mips': + if two_mips: store_two_mips(directory / 'mips', consumer, dxc, env) + elif compiler is not None: + dispatched = [store_3d1d(directory / form, consumer, dxc, env, form, compiler, sdk) + for form in V.FORMS] + if not any(dispatched): + return 77 + assert all(dispatched), dispatched else: store(directory / 'store', consumer, dxc, env) for form in V.FORMS: @@ -574,6 +603,11 @@ def main(consumer, dxc, adapter, mode=None): if __name__ == '__main__': - if len(sys.argv) not in (4, 5) or sys.argv[3] not in ('warp', 'hardware') or sys.argv[4:] not in ([], ['two-mips']): + if len(sys.argv) < 4 or sys.argv[3] not in ('warp', 'hardware'): sys.exit(__doc__) - main(*sys.argv[1:]) + consumer, dxc, adapter, *options = sys.argv[1:] + if options in ([], ['two-mips']): + sys.exit(main(consumer, dxc, adapter, two_mips=bool(options))) + if len(options) == 2 and options[0] != 'two-mips': + sys.exit(main(consumer, dxc, adapter, compiler=options[0], sdk=options[1])) + sys.exit(__doc__) diff --git a/tests/startup_stream_consumer.cpp b/tests/startup_stream_consumer.cpp index 769513639d..b1ba2951bd 100644 --- a/tests/startup_stream_consumer.cpp +++ b/tests/startup_stream_consumer.cpp @@ -808,7 +808,9 @@ static int texture_slot67(IDXGIAdapter* adapter, void* device, const std::string // texture-volume-native-sampled: the volume store, plus a 2x1 R32_FLOAT texture (a format a comparison sample // reads) with a 1x1 second mip, whose shader resource view token is copied to table byte 32. const bool sampled = mode == "texture-volume-native-sampled"; - const bool volume = sampled || mode == "texture-volume" || mode == "texture-volume-bad-w" || + const bool zero_w = mode == "texture-volume-native-zero-w"; + const bool native_inputs = mode == "texture-volume-native-inputs" || zero_w; + const bool volume = sampled || native_inputs || mode == "texture-volume" || mode == "texture-volume-bad-w" || mode == "texture-exact-volume"; const bool line = mode == "texture-line" || mode == "texture-line-bad-mip"; const UINT first_mip = volume || line ? 1 : 0; @@ -889,10 +891,15 @@ static int texture_slot67(IDXGIAdapter* adapter, void* device, const std::string for (UINT i = 0; i < bytes / 2; ++i) static_cast(memory)[i] = static_cast(0x5000 + i); } invoke(upload, 9, 0, 0); + constexpr StreamWord record_bytes = 32; + const StreamWord increment = invoke(device, 15, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); + const UINT descriptor_count = dual ? 5 : 4; + // On wider heaps, reserve a separate region for the shader's packed guest table. + const UINT table_slot = increment == record_bytes ? 0 : descriptor_count; const struct { D3D12_DESCRIPTOR_HEAP_DESC desc; UINT64 backing, bytes; - } heap_desc{{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, dual ? 5u : 4u, D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE}, 0, 0}; + } heap_desc{{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, table_slot + descriptor_count, D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE}, 0, 0}; void *heap = nullptr, *queue = nullptr, *allocator = nullptr, *list = nullptr, *fence = nullptr; const D3D12_COMMAND_QUEUE_DESC queue_desc{}; const UINT list_desc[]{D3D12_COMMAND_LIST_TYPE_DIRECT, 2, 1, 0}; @@ -902,12 +909,37 @@ static int texture_slot67(IDXGIAdapter* adapter, void* device, const std::string FAILED(static_cast(invoke(device, 73, word(list_desc), word(allocator), 0, word(&__uuidof(ID3D12GraphicsCommandList)), word(&list)))) || FAILED(static_cast(invoke(device, 36, 0, 0, word(&__uuidof(ID3D12Fence)), word(&fence))))) return 72; const StreamWord cpu = invoke(heap, 9), gpu = invoke(heap, 10); - if (!cpu || !gpu) return 73; + if (!cpu || !gpu || increment < record_bytes) return 73; + const StreamWord table_cpu = cpu + table_slot * increment; + const StreamWord uav_handle = cpu + 2 * increment; + void* input_texture = nullptr; + if (native_inputs) { + // A hardware buffer descriptor in record 0 and a 2D SRV token in record 1. + auto* input = static_cast(VirtualAlloc(nullptr, 0x10000, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); + if (!input) return 70; + input[0] = 0x3f200000u; // 0.625f, not a value the store fixture writes + void* placed = nullptr; + const D3D12_RESOURCE_DESC input_buffer{D3D12_RESOURCE_DIMENSION_BUFFER, 0, sizeof(UINT), 1, 1, 1, + DXGI_FORMAT_UNKNOWN, {1, 0}, D3D12_TEXTURE_LAYOUT_ROW_MAJOR}; + if (FAILED(static_cast(invoke(device, 133, word(input), word(&input_buffer), D3D12_RESOURCE_STATE_COMMON, + 0, word(&__uuidof(ID3D12Resource)), word(&placed))))) return 71; + const auto address = reinterpret_cast(input); + const UINT descriptor[4] = {static_cast(address), static_cast(address >> 32) | (4u << 16), 1, 0}; + std::memcpy(reinterpret_cast(table_cpu), descriptor, sizeof(descriptor)); + const D3D12_RESOURCE_DESC input_desc{D3D12_RESOURCE_DIMENSION_TEXTURE2D, 0, 1, 1, 1, 1, format, {1, 0}}; + input_texture = make(D3D12_HEAP_TYPE_DEFAULT, input_desc, D3D12_RESOURCE_STATE_COMMON); + if (!input_texture) return 71; + D3D12_SHADER_RESOURCE_VIEW_DESC input_view{format, D3D12_SRV_DIMENSION_TEXTURE2D, + D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING}; + input_view.Texture2D.MipLevels = 1; + invoke(device, 18, word(input_texture), word(&input_view), cpu + 1 * increment); + } D3D12_UNORDERED_ACCESS_VIEW_DESC view{format, D3D12_UAV_DIMENSION_TEXTURE2D}; if (volume) { view.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE3D; // At mip 1 the depth is six. The bad count fits only if the binder uses the base mip's depth. view.Texture3D = {first_mip, 1, mode == "texture-volume-bad-w" ? layers : ~0u}; + if (zero_w) view.Texture3D.WSize = 0; } else if (line) { view.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE1D; view.Texture1D.MipSlice = mode == "texture-line-bad-mip" ? first_mip + 1 : first_mip; @@ -917,33 +949,32 @@ static int texture_slot67(IDXGIAdapter* adapter, void* device, const std::string } else { view.Texture2D.MipSlice = swapped ? 1 : 0; } - if (mode != "texture-store-copied") invoke(device, 19, word(texture), 0, word(&view), cpu + 64); + if (mode != "texture-store-copied") invoke(device, 19, word(texture), 0, word(&view), uav_handle); if (mode == "texture-store-copied") { // The view is created at another handle and its token copied into this slot by memcpy, as a title does; a // refused creation at this slot then keeps that copied token. invoke(device, 19, word(texture), 0, word(&view), cpu); - std::memcpy(reinterpret_cast(cpu + 64), reinterpret_cast(cpu), 32); + std::memcpy(reinterpret_cast(uav_handle), reinterpret_cast(cpu), record_bytes); D3D12_RESOURCE_DESC plain = texture_desc; plain.Flags = D3D12_RESOURCE_FLAG_NONE; void* const other = make(D3D12_HEAP_TYPE_DEFAULT, plain, D3D12_RESOURCE_STATE_COMMON); if (!other) return 79; - invoke(device, 19, word(other), 0, word(&view), cpu + 64); + invoke(device, 19, word(other), 0, word(&view), uav_handle); } else if (mode == "texture-store-occupied") { // A second creation at the same handle, over a texture without unordered access, is refused and leaves the first view. D3D12_RESOURCE_DESC plain = texture_desc; plain.Flags = D3D12_RESOURCE_FLAG_NONE; void* const other = make(D3D12_HEAP_TYPE_DEFAULT, plain, D3D12_RESOURCE_STATE_COMMON); if (!other) return 79; - invoke(device, 19, word(other), 0, word(&view), cpu + 64); + invoke(device, 19, word(other), 0, word(&view), uav_handle); } if (dual) { view.Texture2D.MipSlice = swapped ? 0 : 1; - invoke(device, 19, word(texture), 0, word(&view), cpu + 128); + invoke(device, 19, word(texture), 0, word(&view), cpu + 4 * increment); } D3D12_SHADER_RESOURCE_VIEW_DESC read{format, D3D12_SRV_DIMENSION_TEXTURE2D, D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING}; read.Texture2D.MipLevels = 1; if (mode == "texture-store-self") invoke(device, 18, word(texture), word(&read), cpu); - void* input_texture = nullptr; if (sampled) { const D3D12_RESOURCE_DESC input_desc{D3D12_RESOURCE_DIMENSION_TEXTURE2D, 0, 2, 1, 1, 2, DXGI_FORMAT_R32_FLOAT, {1, 0}}; input_texture = make(D3D12_HEAP_TYPE_DEFAULT, input_desc, D3D12_RESOURCE_STATE_COMMON); @@ -951,9 +982,19 @@ static int texture_slot67(IDXGIAdapter* adapter, void* device, const std::string D3D12_SHADER_RESOURCE_VIEW_DESC input_view{DXGI_FORMAT_R32_FLOAT, D3D12_SRV_DIMENSION_TEXTURE2D, D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING}; input_view.Texture2D.MipLevels = 2; - // Created at handle 0 and copied to table byte 32, which is not a handle where the increment is above 32. - invoke(device, 18, word(input_texture), word(&input_view), cpu); - std::memcpy(reinterpret_cast(cpu + 32), reinterpret_cast(cpu), 32); + invoke(device, 18, word(input_texture), word(&input_view), cpu + increment); + } + if (table_slot) { + // Creation writes 32-byte tokens at the stride-aligned CPU handles. The translated + // Dispatch reads them from packed 32-byte records in the bound guest table. + const auto copy_token = [&](UINT slot) { + std::memcpy(reinterpret_cast(table_cpu + slot * record_bytes), + reinterpret_cast(cpu + slot * increment), record_bytes); + }; + if (native_inputs || sampled) copy_token(1); + if (mode == "texture-store-self") copy_token(0); + copy_token(2); + if (dual) copy_token(4); } const auto transfer = [&](void* buffer, bool to_texture) { for (UINT layer = 0; layer < subresources; ++layer) @@ -969,20 +1010,22 @@ static int texture_slot67(IDXGIAdapter* adapter, void* device, const std::string if (FAILED(static_cast(invoke(list, 139, word(allocator))))) return 74; invoke(list, 32, 1, word(&heap)); // Mip 0 takes the first two sentinel words of the upload, mip 1 the word at byte 512 (word 128). - for (UINT mip = 0; input_texture && mip < 2; ++mip) { + for (UINT mip = 0; input_texture && mip < (sampled ? 2u : 1u); ++mip) { D3D12_TEXTURE_COPY_LOCATION input_image{static_cast(input_texture), D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX}; input_image.SubresourceIndex = mip; D3D12_TEXTURE_COPY_LOCATION input_source{static_cast(upload), D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT}; - input_source.PlacedFootprint = {mip * 512u, {DXGI_FORMAT_R32_FLOAT, 2u >> mip, 1, 1, pitch}}; + input_source.PlacedFootprint = sampled + ? D3D12_PLACED_SUBRESOURCE_FOOTPRINT{mip * 512u, {DXGI_FORMAT_R32_FLOAT, 2u >> mip, 1, 1, pitch}} + : D3D12_PLACED_SUBRESOURCE_FOOTPRINT{0, {format, 1, 1, 1, pitch}}; invoke(list, 16, word(&input_image), 0, 0, 0, word(&input_source), 0); } transfer(upload, true); invoke(list, 25, word(slot67_pipeline)); invoke(list, 33, word(slot67_root)); invoke(list, 39, 0, 3, word(constants), 0); - invoke(list, 35, 1, gpu); + invoke(list, 35, 1, gpu + table_slot * increment); if (mode != "texture-store-none") invoke(list, 14, 2, 1, 1); transfer(readback, false); if (FAILED(static_cast(invoke(list, 9)))) return 75;