diff --git a/docs/experiments/native-compile-dxil-refusals.md b/docs/experiments/native-compile-dxil-refusals.md index 090ad0bf1..8e95579b4 100644 --- a/docs/experiments/native-compile-dxil-refusals.md +++ b/docs/experiments/native-compile-dxil-refusals.md @@ -25,8 +25,9 @@ exit: a zero group dimension, root constants above parameter 15 or more than 8 in one parameter, more than 32 user data words, a resource or sampler heap view, a view above root parameter 15, non-contiguous SRV or UAV registers, an SRV or UAV kind with no manifest line (the kind, and for - images the dimension and `array`; a texture SRV of dimension 1D, 2D, 3D or 2D array has a line, a - cube or multisampled one does not), more than 32 SRVs or 8 UAVs, a comparison sampler, and stride + images the dimension and `array`; a texture SRV of dimension 1D, 2D, 3D or 2D array has a line, as + has a cube sample of one cube (`srv N texcubearr table R B`); an `image_load` on a cube, a cube + comparison, a cube array and a multisampled one do not), more than 32 SRVs or 8 UAVs, a comparison sampler, and stride words from a view that is not one raw vector SRV. No register numbers, offsets or keys. - `--requests` still publishes `.cs.cso` and no `.txt`, still counts the request as compiled and still removes `.fail`. It prints the reason in that job's report as diff --git a/docs/experiments/startup-compute-native-dispatch.md b/docs/experiments/startup-compute-native-dispatch.md index 2af49da9b..374123703 100644 --- a/docs/experiments/startup-compute-native-dispatch.md +++ b/docs/experiments/startup-compute-native-dispatch.md @@ -86,7 +86,22 @@ ctest --preset dev --no-tests=error -V -R '^(startup_compute_dispatch(_.*)?|star 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 `address_view_fits`: the view format is the storage format or a member of its typeless family, the mip and - slice range lie inside the texture, and ResourceMinLODClamp is 0. Cube texture SRVs are not published. + slice range lie inside the texture, and ResourceMinLODClamp is 0. +- A cube sample or gather (`image_sample*` or `image_gather4*` with the cube dimension) is published as + `srv N texcubearr table R B`, because the translator declares a TextureCubeArray (DXIL resource kind 9; the layer is `face >> 3`). The title's view must be a + described TEXTURECUBE of a six-slice 2D texture: a view of another dimension (a TEXTURECUBEARRAY or TEXTURE2DARRAY + view included) is refused as "an input texture view is not cube", a missing description as "an input cube texture + has no recorded view description", and a view that does not fit as for the other dimensions. The Dispatch then + creates the view as a TEXTURECUBEARRAY of one cube (First2DArrayFace 0, NumCubes 1, the title's mips and format). + A face id of 8 or more addresses layer `face >> 3` of that one-cube view, which is not tested here; cube arrays + in the program, `image_load` on a cube (a plain `texture_srv` of the cube dimension), cube comparison samples and + descriptor arrays stay refused by name at publication. `gfx2hlsl.py` still refuses a cube in a compute program. + The fixture is `startup_compute_dispatch_cube_check.py`: 24 texels (6 faces of 2x2) sampled at face coordinates + (+-0.5, +-0.5) against a D3D face mapping written in the test. On hardware the translator's DXIL runs; WARP cannot + create its `[WaveSize(32)]` shader, so on WARP the test substitutes an HLSL equivalent for the compiled shader and + covers the manifest and the reader only. Gather on a cube is published the same way but not run by the fixture. + The TEXTURECUBEARRAY rewrite follows the declared resource kind; with it removed, WARP and hardware gave the same + words and no debug-layer message, so no run here tells the two apart. - One descriptor shape was dispatched: stride 4 and 320 records, for the input and for the output. - An indirect Dispatch of a natively translated program was not run. - A captured request starts with a 36-byte prefix of `v_nop` words before `s_inst_prefetch`. The route passes diff --git a/docs/experiments/startup-image-cube.md b/docs/experiments/startup-image-cube.md index a4f6b9d85..741e6d87e 100644 --- a/docs/experiments/startup-image-cube.md +++ b/docs/experiments/startup-image-cube.md @@ -24,7 +24,9 @@ The translator emits a helper `x2w_cube_dir(s, t, face)` that rebuilds a directi - `image_load` and `image_load_mip` on a cube (`Load is not defined on a TextureCube`), `image_sample_d` (cube derivatives are not converted), `image_get_lod`, `a16` and `d16`. -- A cube in a compute program: the compute dispatch has no `texcube` view binding. +- A cube in a compute program through `gfx2hlsl.py`: the compute dispatch has no `texcube` view binding. The + published native route (`compile_dxil --requests`) names a cube sample `srv N texcubearr table R B` and binds the + title's TEXTURECUBE view as a one-cube TEXTURECUBEARRAY (docs/experiments/startup-compute-native-dispatch.md). - A constant sampler with `DISABLE_CUBE_WRAP` (`SQ_IMG_SAMP_WORD0` bit 28, Mesa `src/amd/registers/gfx10-rsrc.json`): the guest then filters without crossing face edges, while a D3D12 cube always filters across them. With the bit clear both cross. diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 4b1a3e002..5d8aa4d45 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -1010,7 +1010,9 @@ and raw table buffers stored to, with their descriptors in 32-byte table records scalar buffer load, a texture, a sampler, 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 +refused by name. A cube sample is published as `srv N texcubearr table R B` (the program declares a +TextureCubeArray) and bound from the title's described TEXTURECUBE view of a six-slice texture, created as a +one-cube TEXTURECUBEARRAY; `image_load` on a cube, cube comparison and cube arrays 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 diff --git a/src/direct_compute_cache.cpp b/src/direct_compute_cache.cpp index 79dcc6b3c..d1715c1a1 100644 --- a/src/direct_compute_cache.cpp +++ b/src/direct_compute_cache.cpp @@ -966,6 +966,11 @@ std::optional format_compute_runtime_manifest(const NativeComputeSh : "tex2da"; srv_lines << "srv " << b.shader_register << ' ' << tex << " table " << b.root_parameter << ' ' << b.byte_offset << '\n'; + } else if (b.kind == ComputeViewKind::cube_sample_srv && b.array_count == 1 && b.image_dimension == 3) { + // The program declares a TextureCubeArray (resource kind 9); the runtime binds the title's cube view + // as a one-cube array. `texcube` keeps its draw meaning (TextureCube). + srv_lines << "srv " << b.shader_register << " texcubearr table " << b.root_parameter << ' ' + << b.byte_offset << '\n'; } else { return refuse(view_shape("SRV", b)); } diff --git a/src/startup_stubs/d3d12_compute_dispatch.inl b/src/startup_stubs/d3d12_compute_dispatch.inl index ce571a0f8..f2b9eccc2 100644 --- a/src/startup_stubs/d3d12_compute_dispatch.inl +++ b/src/startup_stubs/d3d12_compute_dispatch.inl @@ -4,7 +4,7 @@ // buffers (`srv buf table ` and `srv buf root ` inputs, `uav table ` // outputs, or `uav bufhalf table bytes ` for typed half stores) // named by hardware buffer descriptors or 64-bit root SRV addresses, and textures -// (`srv tex1d|tex2d|tex2da|tex3d table ` inputs, `uav tex1d/tex2d/tex2da/tex3d float table channels ` outputs) named +// (`srv tex1d|tex2d|tex2da|tex3d|texcubearr table ` inputs, `uav tex1d/tex2d/tex2da/tex3d float table channels ` outputs) named // by the token of a view the title created (device slots 18 and 19). `smp`, `smpl` and `smpc` lines become static samplers. Entry // values come from the compute root constants of slots 37 and 39, or from the descriptor a buffer input or output is // bound from: its stride and word 3 (`stride`, `descword3`), and word 3, the upper half of word 1 and the record @@ -191,6 +191,8 @@ const char* load_compute_translation(ComputeTranslation& tr) if (!std::strcmp(name, "tex2d")) return D3D12_SRV_DIMENSION_TEXTURE2D; if (!std::strcmp(name, "tex2da")) return D3D12_SRV_DIMENSION_TEXTURE2DARRAY; if (!std::strcmp(name, "tex3d")) return D3D12_SRV_DIMENSION_TEXTURE3D; + // The program declares a TextureCubeArray; the title's TEXTURECUBE view is bound as a one-cube array. + if (!std::strcmp(name, "texcubearr")) return D3D12_SRV_DIMENSION_TEXTURECUBE; return D3D12_SRV_DIMENSION_UNKNOWN; }; if (line.empty() || (std::sscanf(line.c_str(), "group %u %u %u%n", &x, &y, &z, &used) == 3 && whole())) continue; @@ -254,7 +256,7 @@ const char* load_compute_translation(ComputeTranslation& tr) std::sscanf(line.c_str(), "smpc %u %x %x %x %x%n", &i, &x, &y, &z, &w, &used) == 5) && whole() && i == tr.samplers.statics.size()) tr.samplers.read_manifest_line(line.c_str()); - else return line.rfind("srv", 0) == 0 ? "a resource that is not a raw table buffer or a 1D/2D/2D-array/3D texture" + else return line.rfind("srv", 0) == 0 ? "a resource that is not a raw table buffer or a 1D/2D/2D-array/3D/cube texture" : line.rfind("uav", 0) == 0 ? "an unsupported compute output view" : line.rfind("udsrc", 0) == 0 ? "an entry value that is not a root constant or a buffer descriptor word" : line.rfind("smp", 0) == 0 ? "a sampler that is not a plain static one" : "an unknown manifest line"; @@ -682,19 +684,35 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch& else if (!texture || (resource.Flags & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE)) reason = "an input texture has no desktop texture to sample"; else if (slot.srv_dimension != D3D12_SRV_DIMENSION_TEXTURE2D) { - // 1D, 2D array and 3D: only the title's own described view of that dimension, inside its texture. + // 1D, 2D array, 3D and cube: only the title's own described view of that dimension, inside its texture. const bool one = slot.srv_dimension == D3D12_SRV_DIMENSION_TEXTURE1D; const bool array = slot.srv_dimension == D3D12_SRV_DIMENSION_TEXTURE2DARRAY; + const bool cube = slot.srv_dimension == D3D12_SRV_DIMENSION_TEXTURECUBE; if (!described) reason = one ? "an input 1D texture has no recorded view description" : array ? "an input 2D array texture has no recorded view description" + : cube ? "an input cube texture has no recorded view description" : "an input 3D texture has no recorded view description"; else if (desc.ViewDimension != slot.srv_dimension) reason = one ? "an input texture view is not 1D" : array ? "an input texture view is not 2D array" - : "an input texture view is not 3D"; + : cube ? "an input texture view is not cube" : "an input texture view is not 3D"; else if (!x2w_startup::address_view_fits(resource, desc)) reason = "an input texture view does not fit its texture"; else if (x2w_startup::packed_target_format(desc.Format)) reason = "an input texture view has a packed format"; + else if (cube) { + // The program declares a TextureCubeArray (manifest `texcubearr`), and a view must match the declared + // dimension: the title's cube becomes a one-cube array of the same mips and format. The fit check + // above ran on the title's view, which only a TEXTURECUBE of exactly six slices passes. + // The two views share a union: the mips are read before the rewrite, and First2DArrayFace takes the + // place of TextureCube.ResourceMinLODClamp, which the fit check already required to be 0. + const UINT most_detailed = desc.TextureCube.MostDetailedMip, levels = desc.TextureCube.MipLevels; + desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURECUBEARRAY; + desc.TextureCubeArray.MostDetailedMip = most_detailed; + desc.TextureCubeArray.MipLevels = levels; + desc.TextureCubeArray.First2DArrayFace = 0; + desc.TextureCubeArray.NumCubes = 1; + desc.TextureCubeArray.ResourceMinLODClamp = 0.0f; + } } else if (described ? desc.ViewDimension != D3D12_SRV_DIMENSION_TEXTURE2D : resource.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D || resource.DepthOrArraySize != 1) diff --git a/tests/cmake/startup_compute_dispatch_cube.cmake b/tests/cmake/startup_compute_dispatch_cube.cmake new file mode 100644 index 000000000..4b632277f --- /dev/null +++ b/tests/cmake/startup_compute_dispatch_cube.cmake @@ -0,0 +1,17 @@ +if(TARGET compile_dxil AND EXISTS "${INTAKE_PYTHON}" + AND EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll") + add_test(NAME startup_compute_dispatch_cube_controls COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_compute_dispatch_cube_check.py" compile + "$" "${DXC_BIN_DIR}") + set_tests_properties(startup_compute_dispatch_cube_controls PROPERTIES TIMEOUT 60) +endif() +if(TARGET compile_dxil AND TARGET startup_stream_consumer AND TARGET offscreen_check AND EXISTS "${INTAKE_PYTHON}" + AND EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll") + foreach(adapter IN ITEMS warp hardware) + add_test(NAME startup_compute_dispatch_cube_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_compute_dispatch_cube_check.py" + "$" "$" "$" + "${DXC_BIN_DIR}" ${adapter}) + set_tests_properties(startup_compute_dispatch_cube_${adapter} PROPERTIES TIMEOUT 180 SKIP_RETURN_CODE 77) + endforeach() +endif() diff --git a/tests/native_compute_manifest_reasons.cpp b/tests/native_compute_manifest_reasons.cpp index 92d7147f1..3aa05abff 100644 --- a/tests/native_compute_manifest_reasons.cpp +++ b/tests/native_compute_manifest_reasons.cpp @@ -67,6 +67,19 @@ int main() { p.bindings[0].kind = K::texture_srv; p.bindings[0].array_count = 4; }}, + {"SRV kind cube_comparison_srv cube", [](auto &p) { + p.bindings[0].kind = K::cube_comparison_srv; + p.bindings[0].image_dimension = 3; + }}, + {"SRV kind cube_sample_srv cube array", [](auto &p) { + p.bindings[0].kind = K::cube_sample_srv; + p.bindings[0].image_dimension = 3; + p.bindings[0].array_count = 2; + }}, + {"SRV kind cube_sample_srv 2d", [](auto &p) { + p.bindings[0].kind = K::cube_sample_srv; + p.bindings[0].image_dimension = 1; + }}, {"UAV kind typed_buffer_uav", [](auto &p) { p.bindings[1].kind = K::typed_buffer_uav; }}, {"UAV kind texture_uav cube", [](auto &p) { p.bindings[1].kind = K::texture_uav; @@ -93,19 +106,22 @@ 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. + // A texture SRV is published under the name of its dimension: the line the runtime's reader takes. A cube sample + // names `texcubearr`: the program declares a TextureCubeArray (`texcube` stays the draw route's TextureCube). const struct { + K kind; 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"}, + {K::texture_srv, 0, "srv 0 tex1d table 1 64\n"}, + {K::texture_srv, 1, "srv 0 tex2d table 1 64\n"}, + {K::texture_srv, 2, "srv 0 tex3d table 1 64\n"}, + {K::texture_srv, 5, "srv 0 tex2da table 1 64\n"}, + {K::cube_sample_srv, 3, "srv 0 texcubearr table 1 64\n"}, }; for (const auto &c : published) { auto program = base(); - program.bindings[0].kind = K::texture_srv; + program.bindings[0].kind = c.kind; program.bindings[0].image_dimension = c.image_dimension; reason = "unset"; const auto manifest = format_compute_runtime_manifest(program, &reason); diff --git a/tests/startup_compute_dispatch_cube_check.py b/tests/startup_compute_dispatch_cube_check.py new file mode 100644 index 000000000..fb7483270 --- /dev/null +++ b/tests/startup_compute_dispatch_cube_check.py @@ -0,0 +1,254 @@ +"""A published native compute program sampling a cube texture through the stand-in's Dispatch (list slot 14). + +usage: startup_compute_dispatch_cube_check.py + startup_compute_dispatch_cube_check.py compile + +The program is synthetic gfx1013 code: 24 lanes per group, lane L sampling (image_sample_lz, dim cube, NSA form with +S = v5, T = v6, face = v4) the face L >> 2 at s = 1.25 + 0.5 (L & 1) and t = 1.25 + 0.5 ((L >> 1) & 1), that is the face +coordinates (2 s - 3, 2 t - 3) = (+-0.5, +-0.5), under an all-zero (point, wrap) sampler. Each of the 12 groups stores +its 24 results at word 24 * group + lane; the last 32 of the 320 output words keep the sentinel. The translator declares +the resource as a TextureCubeArray (DXIL kind 9), so the published manifest names the input `srv 0 texcubearr table 1 64` +and the output `uav 0 table 1 32`. The consumer (dispatch-cube) creates a 2x2 R32_FLOAT texture of six slices, texel +(x, y) of slice f = 100 + 4 f + x + 2 y, and a TEXTURECUBE view of it at table byte 64 (device slot 18). + +The expected word of a lane is the texel its face coordinates name: face L >> 2, texel x from the sign of the first +coordinate and y from the sign of the second (the D3D sc/tc convention). The direction tables here only round-trip +that choice; what checks the orientation is the GPU: the translator's cube_axes on hardware (CUBE_HLSL's directions on +WARP) must send each lane to that texel, so an s/t swap, a sign error on a minor axis or a wrong face order change words. + +Positive run: before the manifest exists the Dispatch is refused with "no translation" and the output keeps the +sentinel; after `compile_dxil --requests` all 320 words equal the reference, the debug layer reports no error or +corruption message, and exactly one Dispatch translates. Controls, each refused with one logged reason and an +untouched output: the title's view is a TEXTURE2DARRAY of the six slices (-array), the view has no description +(-undescribed), the TEXTURECUBE view is on a twelve-slice texture (-twelve), and the published line rewritten to +tex2da, tex2d (a manifest dimension the title's view is not) or texcube (a name the compute reader does not know). +Exit 77 when the adapter is not available, or, on hardware, when it has no wave32. + +The translator's DXIL is [WaveSize(32)] (a 32-lane guest wave), which WARP cannot create. On warp the published +manifest is therefore kept (compile_dxil still publishes it from the request) and the program's .cs.cso is replaced +by CUBE_HLSL below, the same program in HLSL for cs_6_0 with the face directions of direction(): that run covers the +manifest line, the reader and the debug layer on WARP, not the translator (hardware covers it). The TEXTURECUBEARRAY +rewrite of the view is not discriminated by any run here: with the rewrite removed, WARP and hardware gave the same +words and no debug-layer message. +""" +from pathlib import Path +import struct +import subprocess +import sys +from tempfile import TemporaryDirectory + +from native_dpp_mirror_check import vop2 +from native_gather_lod_check import render +from native_scalar_lds_check import scalar, sop1 +from startup_compute_dispatch_check import dispatch, lines, prepare_case +from startup_compute_translate_check import state_a, write_request +from startup_gfx2hlsl_compute_check import SENTINEL, TGID_X, TGID_Y, TGID_Z, USER7, WORDS, root_blob + +GROUP = (24, 1, 1) +STATE = state_a(GROUP, USER7 | TGID_X | TGID_Y | TGID_Z) +GROUPS = 12 # the consumer's Dispatch(3, 2, 2) +LANES = 24 +MODE = 'dispatch-cube' +INPUT_LINE = 'srv 0 texcubearr table 1 64' +OUTPUT_LINE = 'uav 0 table 1 32' +REJECTED = 'an input texture view is not cube' + + +def bits(value): + return struct.unpack('> 1 + vop2(27, 2, 129, 2), # v2 &= 1 + vop2(22, 4, 130, 0), # v4 = lane >> 2, the face + 0x7e020d01, 0x7e040d02, 0x7e080d04, # v_cvt_f32_u32 of v1, v2, v4 + vop2(8, 1, 240, 1), vop2(8, 2, 240, 2), # v1, v2 *= 0.5 + vop2(3, 5, 255, 1), bits(1.25), # v5 = S = 1.25 + v1 + vop2(3, 6, 255, 2), bits(1.25), # v6 = T = 1.25 + v2 + scalar(0x26, 8, 8, 131), # s8 = 3 * group.y + scalar(0x26, 9, 9, 134), # s9 = 6 * group.z + scalar(0, 11, 7, 8), scalar(0, 11, 11, 9), # s11 = flat group index + scalar(0x26, 11, 11, 128 + LANES), # s11 *= 24 + vop2(0x25, 8, 11, 0), # v8 = 24 * group + lane + vop2(26, 9, 130, 8), # v9 = v8 << 2 + 0xbf8cc07f, # s_waitcnt lgkmcnt(0) + 0xf09c011a, 5 | 7 << 8 | 6 << 16 | 10 << 21, 0x00000406, # image_sample_lz v7, v5, s[24:31], s[40:43] cube, T v6, face v4 + 0xbf8c3f70, # s_waitcnt vmcnt(0) + 0xe0701000, 0x80080709, # buffer_store_dword v7, v9, s[32:35], 0 offen + 0xbf810000, + ] + return struct.pack('<%dI' % len(words), *words) + + +# The program as HLSL: lane L samples face L >> 2 of the cube array at the direction of face coordinates (2 s - 3, 2 t - 3). +CUBE_HLSL = """TextureCubeArray r0 : register(t0); +RWByteAddressBuffer w0 : register(u0); +SamplerState q0 : register(s0); +[numthreads(24, 1, 1)] void main(uint3 g : SV_GroupID, uint lane : SV_GroupIndex) +{ + float sc = 2.0 * (1.25 + 0.5 * (lane & 1)) - 3.0, tc = 2.0 * (1.25 + 0.5 * ((lane >> 1) & 1)) - 3.0; + static const float3 scale[6] = {float3(0, 0, -1), float3(0, 0, 1), float3(1, 0, 0), float3(1, 0, 0), float3(1, 0, 0), + float3(-1, 0, 0)}; + static const float3 down[6] = {float3(0, -1, 0), float3(0, -1, 0), float3(0, 0, 1), float3(0, 0, -1), float3(0, -1, 0), + float3(0, -1, 0)}; + static const float3 major[6] = {float3(1, 0, 0), float3(-1, 0, 0), float3(0, 1, 0), float3(0, -1, 0), float3(0, 0, 1), + float3(0, 0, -1)}; + const uint face = lane >> 2; + const float3 direction = major[face] + sc * scale[face] + tc * down[face]; + const uint flat = g.x + 3 * g.y + 6 * g.z; + w0.Store((flat * 24 + lane) * 4, asuint(r0.SampleLevel(q0, float4(direction, 0), 0).x)); +} +""" + + +def texel_value(face, x, y): + return 100 + 4 * face + x + 2 * y + + +def project(direction): + """D3D cube mapping of a direction: (face, texel x, texel y) of a 2x2 face.""" + x, y, z = direction + axis = max(range(3), key=lambda i: abs(direction[i])) + major = direction[axis] + face = 2 * axis + (major < 0) + sc, tc = {0: (-z, -y), 1: (z, -y), 2: (x, z), 3: (x, -z), 4: (x, -y), 5: (-x, -y)}[face] + u, v = (sc / abs(major) + 1) / 2, (tc / abs(major) + 1) / 2 + return face, min(int(u * 2), 1), min(int(v * 2), 1) + + +def direction(face, sc, tc): + """The direction of face coordinates sc, tc (ma = 1): the inverse of the table in project().""" + return {0: (1, -tc, -sc), 1: (-1, -tc, sc), 2: (sc, 1, tc), 3: (sc, -1, -tc), 4: (sc, -tc, 1), 5: (-sc, -tc, -1)}[face] + + +def reference(): + values = [] + for lane in range(LANES): + s, t, face = 1.25 + 0.5 * (lane & 1), 1.25 + 0.5 * ((lane >> 1) & 1), lane >> 2 + values.append(texel_value(*project(direction(face, 2 * s - 3, 2 * t - 3)))) + assert len(set(values)) == LANES, values # a swapped axis or face must change a word + return values + + +def expected(): + values = [bits(float(v)) for v in reference()] + return [values[i % LANES] for i in range(GROUPS * LANES)] + [SENTINEL] * (WORDS - GROUPS * LANES) + + +def check_words(got, want, label): + first = next(((i, a, b) for i, (a, b) in enumerate(zip(got, want)) if a != b), None) + if first is not None: + word, actual, wanted = first + raise AssertionError(f'{label}: word {word} expected {wanted:08x}, read {actual:08x} ' + f'({sum(a != b for a, b in zip(got, want))} words differ)') + + +def hlsl_program(sdk, cache, key): + source = cache / 'cube.hlsl' + source.write_text(CUBE_HLSL) + result = subprocess.run([Path(sdk) / 'dxc.exe', '-T', 'cs_6_0', '-E', 'main', '-Fo', cache / f'{key}.cs.cso', source], + capture_output=True, text=True, timeout=60) + assert result.returncode == 0, result.stdout + result.stderr + + +def publish(compile_dxil, sdk, cache, key): + result = subprocess.run([compile_dxil, sdk, cache.parent / 'native', 'gfx1013-direct-cs@0', + '--requests', cache, cache], capture_output=True, text=True, timeout=30) + manifest = cache / f'{key}.txt' + assert result.returncode == 0 and manifest.is_file(), (result.stdout, result.stderr) + text = manifest.read_text() + assert INPUT_LINE in text.splitlines() and OUTPUT_LINE in text.splitlines(), text + assert not (cache / f'{key}.cs.hlsl').exists(), 'expected native publication' + return manifest, text + + +def check_compiler(compile_dxil, sdk): + with TemporaryDirectory() as directory: + cache = Path(directory) / 'cache' + cache.mkdir() + key = write_request(cache, program(), STATE, root=root_blob(table_range=True)) + _, text = publish(compile_dxil, sdk, cache, f'{key:016x}') + assert 'srv 0 texcube table' not in text, text + print('startup_compute_dispatch_cube compile: PASS') + + +def check(compile_dxil, consumer, sdk, work, adapter): + cache, env = prepare_case(work, adapter) + env.pop('X2W_NATIVE_SHADERS', None) + env.pop('X2W_NATIVE_SHADERS_ONLY', None) + (work / 'program').write_bytes(program()) + (work / 'state').write_bytes(STATE) + untouched, want = [SENTINEL] * WORDS, expected() + assert want != untouched + + got, result = dispatch(work, consumer, env, MODE, probe=True) + check_words(got, untouched, 'before translation') + assert len(lines(result, 'Dispatch(3, 2, 2) refused: no translation')) == 1, result.stderr[-3000:] + [request] = cache.glob('req_*.c67') + key = request.stem.removeprefix('req_') + manifest, original = publish(compile_dxil, sdk, cache, key) + if adapter == 'warp': + hlsl_program(sdk, cache, key) + + got, result = dispatch(work, consumer, env, MODE) + check_words(got, want, 'cube samples') + assert len(lines(result, 'Dispatch(3, 2, 2) translated')) == 1 and not lines(result, 'refused'), result.stderr[-3000:] + errors = [line for line in lines(result, 'x2w-d3d12-debug ') if ' sev=0 ' in line or ' sev=1 ' in line] + assert not errors, errors[0] + layers = lines(result, 'x2w-d3d12 debug layer: ') + assert layers and '0x00000000' in layers[0], ('debug layer not enabled', layers) + print(f'{adapter}: {GROUPS * LANES} cube samples (24 distinct texels) match the D3D face mapping; ' + 'debug layer: enabled, no error message', flush=True) + + def refused(label, mode, reason): + got, result = dispatch(work, consumer, env, mode) + logged = lines(result, 'refused: ') + check_words(got, untouched, label) + assert len(logged) == 1 and logged[0].endswith('refused: ' + reason), (label, logged, result.stderr[-3000:]) + assert not lines(result, 'Dispatch(3, 2, 2) translated'), label + errors = [line for line in lines(result, 'x2w-d3d12-debug ') if ' sev=0 ' in line or ' sev=1 ' in line] + assert not errors, (label, errors[0]) + + refused('2D array view', MODE + '-array', REJECTED) + refused('no description', MODE + '-undescribed', 'an input cube texture has no recorded view description') + refused('view does not fit', MODE + '-twelve', 'an input texture view does not fit its texture') + + # Mutants: the dimension is the manifest's. The title's view is a cube, so another name must be refused. + assert original.count(INPUT_LINE) == 1 + for name, reason in ( + ('tex2da', 'an input texture view is not 2D array'), + ('tex2d', 'an input texture view is not 2D'), + ('texcube', 'a resource that is not a raw table buffer or a 1D/2D/2D-array/3D/cube texture')): + manifest.write_bytes(original.replace(INPUT_LINE, INPUT_LINE.replace('texcubearr', name)).encode()) + refused('manifest ' + name, MODE, reason) + manifest.write_bytes(original.encode()) + got, result = dispatch(work, consumer, env, MODE) + check_words(got, want, 'restored manifest') + assert len(lines(result, 'Dispatch(3, 2, 2) translated')) == 1, result.stderr[-3000:] + print(f'{adapter}: a TEXTURE2DARRAY view, no description, a view on twelve slices and the line rewritten to tex2da, ' + f'tex2d or texcube are each refused with the output untouched', flush=True) + + +def main(compile_dxil, consumer, renderer, sdk, adapter): + if adapter == 'hardware': + wave = render(renderer, adapter, '--shader-wave32') + assert wave.returncode == 0, wave.stdout + wave.stderr + with TemporaryDirectory() as directory: + check(compile_dxil, consumer, sdk, Path(directory) / adapter, adapter) + print(f'startup_compute_dispatch_cube: PASS on {adapter}') + + +if __name__ == '__main__': + if len(sys.argv) == 4 and sys.argv[1] == 'compile': + check_compiler(*sys.argv[2:]) + elif len(sys.argv) == 6 and sys.argv[5] in ('warp', 'hardware'): + main(*sys.argv[1:]) + else: + sys.exit(__doc__) diff --git a/tests/startup_compute_root_srv_dispatch_check.py b/tests/startup_compute_root_srv_dispatch_check.py index 4411d2f20..3f4b08dc0 100644 --- a/tests/startup_compute_root_srv_dispatch_check.py +++ b/tests/startup_compute_root_srv_dispatch_check.py @@ -172,11 +172,11 @@ def check_single_root_srv(base, consumer, dxc, adapter): # Manifest parser refusal controls on bad `srv ... buf root ...` lines and descriptor-word udsrc on a root SRV. for bad_manifest, expected_reason in ( (manifest.replace("srv 0 buf root 2", "srv 0 buf root -1"), - "a resource that is not a raw table buffer or a 1D/2D/2D-array/3D texture"), + "a resource that is not a raw table buffer or a 1D/2D/2D-array/3D/cube texture"), (manifest.replace("srv 0 buf root 2", "srv 0 buf root 2 trailing"), - "a resource that is not a raw table buffer or a 1D/2D/2D-array/3D texture"), + "a resource that is not a raw table buffer or a 1D/2D/2D-array/3D/cube texture"), (manifest.replace("srv 0 buf root 2", "srv 1 buf root 2"), - "a resource that is not a raw table buffer or a 1D/2D/2D-array/3D texture"), + "a resource that is not a raw table buffer or a 1D/2D/2D-array/3D/cube texture"), (manifest.replace("ud 3\nudsrc 0 rootconst 0 0\nudsrc 1 rootconst 0 1\nudsrc 2 rootconst 0 2", "ud 4\nudsrc 0 rootconst 0 0\nudsrc 1 rootconst 0 1\nudsrc 2 rootconst 0 2\nudsrc 3 stride 0"), "an entry value names a descriptor that is not a buffer input or output"), diff --git a/tests/startup_stream_consumer.cpp b/tests/startup_stream_consumer.cpp index 092b6e91e..79c41b55f 100644 --- a/tests/startup_stream_consumer.cpp +++ b/tests/startup_stream_consumer.cpp @@ -397,7 +397,7 @@ static int dispatch_slot67(IDXGIAdapter* adapter, void* device, const std::strin std::memcpy(reinterpret_cast(cpu + 64), window, sizeof(window)); std::memcpy(reinterpret_cast(cpu + 32), descriptors[1], sizeof(descriptors[1])); } - if (mode == "dispatch-records32" || typed_output_offset || shared_uav) { // output at byte 32, none at byte 16 + if (mode == "dispatch-records32" || typed_output_offset || shared_uav || mode.rfind("dispatch-cube", 0) == 0) { // output at byte 32, none at byte 16 std::memset(reinterpret_cast(cpu + sizeof(descriptors[0])), 0, sizeof(descriptors[1])); std::memcpy(reinterpret_cast(cpu + 32), descriptors[1], sizeof(descriptors[1])); } @@ -585,6 +585,48 @@ static int dispatch_slot67(IDXGIAdapter* adapter, void* device, const std::strin } invoke(device, 18, word(texture), control == "undescribed" ? 0 : word(&view), cpu + 64); } + if (mode.rfind("dispatch-cube", 0) == 0) { + // dispatch-cube[-array|-undescribed|-twelve]: records of 32 bytes (the output at byte 32, as dispatch-records32); the + // sampled input is a described R32_FLOAT cube texture in the SRV at table byte 64: 2x2 texels, six slices (the cube faces, +X -X +Y -Y +Z -Z), texel (x + 2 y) of slice f holding + // 100 + 4 f + x + 2 y. Each slice is uploaded by its own copy. The view is a TEXTURECUBE of mip 0. -array views the + // same six slices as a TEXTURE2DARRAY, -undescribed creates the view without a description, -twelve makes the + // texture twelve slices deep (slices 6 to 11 hold 100 + 4 slice + x + 2 y too) under a TEXTURECUBE view. + const std::string control = mode.substr(13); + if (!control.empty() && control != "-array" && control != "-undescribed" && control != "-twelve") return 2; + constexpr DXGI_FORMAT format = DXGI_FORMAT_R32_FLOAT; + const UINT slices = control == "-twelve" ? 12 : 6; + const D3D12_RESOURCE_DESC texture_desc{D3D12_RESOURCE_DIMENSION_TEXTURE2D, 0, 2, 2, static_cast(slices), 1, format, {1, 0}}; + constexpr UINT pitch = D3D12_TEXTURE_DATA_PITCH_ALIGNMENT, slice_bytes = D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT; + const D3D12_RESOURCE_DESC upload_desc{D3D12_RESOURCE_DIMENSION_BUFFER, 0, 8192, 1, 1, 1, DXGI_FORMAT_UNKNOWN, {1, 0}, + D3D12_TEXTURE_LAYOUT_ROW_MAJOR}; + void* const texture = make_committed(device, D3D12_HEAP_TYPE_DEFAULT, texture_desc, D3D12_RESOURCE_STATE_COMMON); + void* const upload = make_committed(device, D3D12_HEAP_TYPE_UPLOAD, upload_desc, D3D12_RESOURCE_STATE_GENERIC_READ); + void* texel = nullptr; + if (!texture || !upload || FAILED(static_cast(invoke(upload, 8, 0, 0, word(&texel)))) || !texel) return 80; + std::memset(texel, 0, 8192); + for (UINT slice = 0; slice < slices; ++slice) + for (UINT y = 0; y < 2; ++y) + for (UINT x = 0; x < 2; ++x) { + const float value = static_cast(100 + 4 * slice + x + 2 * y); + std::memcpy(static_cast(texel) + slice * slice_bytes + y * pitch + x * 4, &value, sizeof(value)); + } + invoke(upload, 9, 0, 0); + for (UINT slice = 0; slice < slices; ++slice) { + D3D12_TEXTURE_COPY_LOCATION image{static_cast(texture), D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX}; + image.SubresourceIndex = slice; + D3D12_TEXTURE_COPY_LOCATION source{static_cast(upload), D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT}; + source.PlacedFootprint = {UINT64{slice} * slice_bytes, {format, 2, 2, 1, pitch}}; + invoke(list, 16, word(&image), 0, 0, 0, word(&source), 0); + } + D3D12_SHADER_RESOURCE_VIEW_DESC view{format, D3D12_SRV_DIMENSION_TEXTURECUBE, D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING}; + if (control == "-array") { + view.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2DARRAY; + view.Texture2DArray = {0, 1, 0, 6, 0, 0.0f}; + } else { + view.TextureCube = {0, 1, 0.0f}; + } + invoke(device, 18, word(texture), control == "-undescribed" ? 0 : word(&view), cpu + 64); + } if (mode == "dispatch-input-memory-view") { // The input is a buffer view (device slot 70) of memory no buffer is placed over, 16 bytes in; its token // replaces the input descriptor and the words are written after the view exists.