diff --git a/docs/experiments.md b/docs/experiments.md index e69fadc8fb..a4fe9a4e32 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -16650,6 +16650,14 @@ cmake --build --preset dev --target startup_stream_consumer ctest --preset dev --no-tests=error -R "^startup_compute_dispatch_" -V ``` +### Static samplers with a LOD clamp + +The translator writes a constant sampler of `image_sample`, `_l` or `_lz` whose mip filter is not 0 as an `smpl` line (#2482): a static sampler whose MinLOD and MaxLOD come from the descriptor's word 1. The compute Dispatch now reads `smpl` lines as the draw path does, through the same `TranslatedSamplers::read_manifest_line` and `create_translated_root`; the only difference for compute is that the root signature is set with `SetComputeRootSignature`, and static samplers need nothing else. A comparison sampler (`smpc`) and the run-time sampler heap table (`smpheap`) are still refused, as "a comparison sampler" and "a sampler from the sampler heap". Binding the heap table for compute would need the draw path's sampler snapshot (`snapshot_samplers`, `create_sampler_snapshot_heap`) taken from the compute root's sampler heap pointer, the sampler heap set with `SetDescriptorHeaps` and its table set with `SetComputeRootDescriptorTable`. + +`tests/startup_compute_dispatch_texture_check.py` adds a filtered case: the texture input program with a wrap, linear sampler (`smpl 0 0x0 0xc00000 0x18500000 0x0`) reads a 2x1 RGBA8 texture whose left texel has red 0 and right texel red 1.0, at the left edge. Only a linear filter that wraps reads 0.5 there. On WARP and on a physical adapter, every thread read red 0.5, and the debug layer reported no error. The control is the same program with the manifest's sampler changed to clamp: it reads red 0, which the check refuses. The `smpc` and `smpheap` lines refuse with their reasons and leave the output untouched. Without the new `smpl` reading, the Dispatch refuses with "a sampler that is not a plain static one" and the case fails. The texture has one mip, so the LOD clamp itself is not exercised here. + +In a local census, 7 compute programs (about 135 thousand Dispatch calls) had an `smpl` line as their first refusal, each with one or two `smpl` lines and no other sampler form. Read against the classifier, all 7 manifests now load; their next refusal, if any, comes from the bindings at Dispatch and has not been measured. + ### An input named by a resource heap record A compute root signature that indexes the resource heap directly (root flag 0x400) passes the heap pointer in one SGPR after the root parameters. `gfx2hlsl.py` lowers one form of a load through that pointer: `s_lshl_b32 R, , 5`, then, with no write of `R` and no branch target in between, `s_load_dwordx4 D, s[H:H+1], R` with a constant zero in `s[H+1]` and no `offset:` modifier. `D` is then a buffer descriptor, and the manifest names it `srv buf heap rootconst `. Slot 32 now records the title memory of the list's view heap on every call; at Dispatch the stand-in reads the 16-byte descriptor at byte `word << 5` (32-bit arithmetic, as the program computes it) of that memory and binds the buffer through the path a `table` record takes. A record outside the heap refuses the dispatch (`a heap record is outside the list's view heap`) and binds nothing. diff --git a/src/startup_stubs/d3d12_compute_dispatch.inl b/src/startup_stubs/d3d12_compute_dispatch.inl index 618ad9976a..af6daff194 100644 --- a/src/startup_stubs/d3d12_compute_dispatch.inl +++ b/src/startup_stubs/d3d12_compute_dispatch.inl @@ -112,7 +112,12 @@ const char* load_compute_translation(ComputeTranslation& tr) tr.uavs.push_back({a, b, true}); else if (std::sscanf(line.c_str(), "srv %u tex2d table %d %d%n", &i, &a, &b, &used) == 3 && whole() && i == tr.srvs.size()) tr.srvs.push_back({a, b, true}); - else if (std::sscanf(line.c_str(), "smp %u %x %x %x %x%n", &i, &x, &y, &z, &w, &used) == 5 && whole() && + // A static sampler, plain (smp) or with the descriptor's LOD clamp (smpl), decoded as the draw path does. + // Not modelled here: comparison samplers (smpc) and the run-time sampler heap table (smpheap). + else if (line.rfind("smpc", 0) == 0) return "a comparison sampler"; + else if (line.rfind("smpheap", 0) == 0) return "a sampler from the sampler heap"; + else if ((std::sscanf(line.c_str(), "smp %u %x %x %x %x%n", &i, &x, &y, &z, &w, &used) == 5 || + std::sscanf(line.c_str(), "smpl %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 2D texture" diff --git a/tests/startup_compute_dispatch_texture_check.py b/tests/startup_compute_dispatch_texture_check.py index d317d036ac..fd09c48dec 100644 --- a/tests/startup_compute_dispatch_texture_check.py +++ b/tests/startup_compute_dispatch_texture_check.py @@ -19,6 +19,10 @@ is 1.0 and a shader resource view of it (device slot 18). After translation all 320 words equal the case's expectation XOR the bits of 1.0f; dispatch-sampled-unbound (no view created) is refused with one logged reason and every word keeps the sentinel. + +Filtered input: the texture input case with a constant wrap, linear sampler that the translator writes as `smpl` +and a 2x1 texture (dispatch-sampled-2x1): red 0.5 at the left edge. The same program with the manifest's sampler +changed to clamp reads red 0 (the control); `smpc` and `smpheap` lines are refused with their reasons. Exit 77 when the adapter is not available. """ import os @@ -199,6 +203,66 @@ def sample(directory, consumer, dxc, env): f'view; debug layer: {layer}') +# The texture input case with a constant sampler instead of the zero one: s21 MAX_LOD 12, s22 linear min, mag and mip +# filters, s20 wrap in U, V and W. A mip filter makes the translator write it as `smpl` (its LOD clamp carried over). +WRAP_LINEAR = (0x0, 0xc00000, 0x18500000, 0x0) +LOD_SAMPLED = SAMPLED[:2] + ('s_mov_b32 s21, 0xc00000', 's_mov_b32 s22, 0x18500000') + SAMPLED[2:] + + +def filtered(directory, consumer, dxc, env): + """The sampler bound is the manifest's: a 2x1 texture (red 0, 1.0) read at the left edge (the coordinates are the + bits of the thread ids, 0 or a denormal) gives red 0.5 only through a linear filter that wraps; clamp gives 0.""" + cache, base = prepare(directory) + env = dict(base, **env) + (directory / 'compute_root').write_bytes(C.root_blob(table_range=True)) + env['X2W_TEST_COMPUTE_ROOT'] = str(directory / 'compute_root') + _, _, rsrc2, group, _ = C.CASES['A'] + C.SPLICE.update({XOR: '05190a3a', LOD_SAMPLED[2]: 'ff0395be0000c000', LOD_SAMPLED[3]: 'ff0396be00005018'}) + code = C.splice(*LOD_SAMPLED) + line = 'smpl 0 %s' % ' '.join('0x%x' % w for w in WRAP_LINEAR) + assert C.translate('A', code=code).manifest().endswith(line + '\n'), C.translate('A', code=code).manifest() + (directory / 'program').write_bytes(code) + (directory / 'data').write_bytes(DATA) + (directory / 'state').write_bytes(state_a(group, rsrc2)) + written = 24 * 3 * 2 * 2 + plain = C.expected('A') + layout = '<%dI' % C.WORDS + + def reds(got): + assert got[written:] == plain[written:], 'a word past the threads changed' + return {struct.unpack('(invoke(list, 139, word(allocator))))) return 74; bind(); if (mode.rfind("dispatch-sampled", 0) == 0) { - const D3D12_RESOURCE_DESC texture_desc{D3D12_RESOURCE_DIMENSION_TEXTURE2D, 0, 1, 1, 1, 1, DXGI_FORMAT_R8G8B8A8_UNORM, {1, 0}}; + const UINT width = mode == "dispatch-sampled-2x1" ? 2 : 1; + const D3D12_RESOURCE_DESC texture_desc{D3D12_RESOURCE_DIMENSION_TEXTURE2D, 0, width, 1, 1, 1, DXGI_FORMAT_R8G8B8A8_UNORM, {1, 0}}; const D3D12_RESOURCE_DESC upload_desc{D3D12_RESOURCE_DIMENSION_BUFFER, 0, 256, 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; - const unsigned char rgba[4] = {255, 64, 128, 32}; // the program reads red: 1.0f + // The program reads red: 1.0f. The 2x1 texture's left texel has red 0, so only a linear filter that wraps + // reads 0.5 at the left edge (point or clamp read 0). + unsigned char rgba[8] = {255, 64, 128, 32, 255, 64, 128, 32}; + if (width == 2) rgba[0] = 0; std::memcpy(texel, rgba, sizeof(rgba)); invoke(upload, 9, 0, 0); D3D12_TEXTURE_COPY_LOCATION image{static_cast(texture), D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX}; D3D12_TEXTURE_COPY_LOCATION source{static_cast(upload), D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT}; - source.PlacedFootprint = {0, {DXGI_FORMAT_R8G8B8A8_UNORM, 1, 1, 1, 256}}; + source.PlacedFootprint = {0, {DXGI_FORMAT_R8G8B8A8_UNORM, width, 1, 1, 256}}; invoke(list, 16, word(&image), 0, 0, 0, word(&source), 0); D3D12_SHADER_RESOURCE_VIEW_DESC view{DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_SRV_DIMENSION_TEXTURE2D, D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING}; view.Texture2D.MipLevels = 1;