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: 2 additions & 2 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -17170,9 +17170,9 @@ ctest --preset dev --no-tests=error -R "^startup_compute_dispatch_entry_" -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`.
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. The run-time sampler heap table (`smpheap`) is still refused, as "a sampler from the sampler heap". An `smpc` line is read since #3075: see [comparison samplers in a published compute manifest](experiments/startup-compute-comparison-sampler.md). 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.
`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 `smpheap` line refuses with its reason and leaves 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.

Expand Down
90 changes: 90 additions & 0 deletions docs/experiments/startup-compute-comparison-sampler.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Comparison samplers in a published compute manifest (#3075)

The stand-in reads the published translation of a slot 67 compute request from `<key>.txt`
(`load_compute_translation`, `src/startup_stubs/d3d12_compute_dispatch.inl`). It refused every `smpc` line there
with `a comparison sampler`. It now reads `smpc <i> <w0> <w1> <w2> <w3>` under the same index and whole-line
checks as `smp` and `smpl`, through `TranslatedSamplers::read_manifest_line`. `decode_statics` already turns the
line into a comparison static sampler for the draw path, and the compute root signature takes it the same way.
An `smpheap` line is still refused with `a sampler from the sampler heap`.

## What this covers

- Only `tools/startup/gfx2hlsl.py` writes `smpc` for a compute program today (`image_sample_c_lz` and
`image_sample_c` with a constant comparison sampler). Before this change the runtime refused that translation
when it loaded the manifest, so such a request could not be dispatched.
- The C++ compiler's publisher is unchanged. `format_compute_runtime_manifest` (`src/direct_compute_cache.cpp`)
returns no manifest for a comparison sampler, so `compile_dxil` publishes no compute `smpc` line.
- The in-process route (`X2W_NATIVE_SHADERS`, `translate_compute_natively`) is unchanged. It reads no manifest
lines and refuses every sampler, and every view that is not a raw buffer, before it reaches one.

## Fixture

[`tests/startup_compute_dispatch_texture_check.py`](../../tests/startup_compute_dispatch_texture_check.py) gains
a compared case. It is case A of
[`startup_gfx2hlsl_compute_check.py`](../../tests/startup_gfx2hlsl_compute_check.py) (group size 2x3x4, 3x2x2
groups, one word stored per thread) with these instructions before the store:

```
s_mov_b64 s[20:21], 0
s_mov_b64 s[22:23], 0
s_mov_b32 s20, 0x8001012
s_load_dwordx8 s[24:31], s[10:11], 0x40
v_cvt_f32_u32_e32 v7, v2
v_cvt_f32_u32_e32 v8, v0
v_mov_b32_e32 v9, 0
image_sample_c_lz v12, v[7:9], s[24:31], s[20:23] dmask:0x1 dim:SQ_RSRC_IMG_2D
v_xor_b32_e32 v5, v5, v12
```

The five instructions that are new here were not assembled by llvm-mc. The check asserts that the Python decoder
reads them as written, that the HLSL holds
`v12 = U(r1.SampleCmpLevelZero(q0, float2(F(v8), F(v9)), F(v7)));` and that the manifest ends with
`smpc 0 0x8001012 0x0 0x0 0x0` (clamp in U and V, TRUNC_COORD, point filters, LESS).

The consumer's new mode `dispatch-sampled-depth` binds a 2x1 `R32_FLOAT` texture with depths 1.0 (left) and 2.5
(right). The reference is the thread's z id (0.0 to 3.0). u is its x id: 0 reads the left texel and 1 clamps to
the right one. The CPU model XORs the bits of 1.0f into a word where `reference < texel` and leaves the word as
case A wrote it otherwise. That is 144 of the 288 written words. The 32 words past them keep the sentinel.

1. Before the translation is published, the Dispatch is refused and every word keeps the sentinel.
2. After `gfx2hlsl.py --pending`, all 320 words match the model and the debug layer reports no error.
3. Control: the same program with the manifest's word 0 changed to GREATER (`0x8004012`) matches the model of
`reference > texel` (108 words pass). The two models differ at 252 words, so the function bound is the
manifest's.

The case replaces an older control in the same file, which rewrote an `smpl` line as `smpc` and expected the
refusal.

## Command and result

```powershell
.\tools\dev.ps1
cmake --build --preset dev --target startup_stream_consumer
ctest --preset dev --no-tests=error -R '^startup_compute_dispatch_texture_(warp|hardware)$' -V
```

Measured on 27.11.2026: `main` at `e1ea515d` plus this change, Windows 11 build 26300.9974, Windows SDK
10.0.26100, DXC 1.9.2602.17, Debug build, D3D12 debug layer enabled.

| Test | Adapter, driver | Result |
|---|---|---|
| `startup_compute_dispatch_texture_hardware` | NVIDIA GeForce RTX 4090, 32.0.16.1088 | PASS (18.81 s). The compared case matched all 320 words under LESS and under GREATER, with no debug layer error. |
| `startup_compute_dispatch_texture_warp` | Microsoft Basic Render Driver, 10.0.26100.9818 | PASS (7.72 s), with the same lines. |

There was no capability skip.

Negative control: with `src/startup_stubs/d3d12_compute_dispatch.inl` put back to `e1ea515d` and the same target
rebuilt, both tests fail in the compared case, after the cases before it pass:

```
AssertionError: smpc LESS: x2w-draw compute e9f1d94c2215f572 Dispatch(3, 2, 2) refused: a comparison sampler
```

## Not measured

- A bilinear comparison filter, a border address mode, a 2D array and the other six functions. The fixture runs
point, clamp, LESS and GREATER on a 2D texture with one mip.
- A comparison sampler on the in-process route or from the C++ compiler's publisher. Both still stop before the
manifest reader, as listed above.
- Console behaviour. The comparison is the desktop sampler's. The function mapping is the draw path's
(`decode_sampler` in `src/startup_stubs/translated_sampler_bindings.h`).
10 changes: 5 additions & 5 deletions src/startup_stubs/d3d12_compute_dispatch.inl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// buffers (`srv <i> buf table <param> <byte>` and `srv <i> buf root <param>` inputs, `uav <i> table <param> <byte>`
// outputs) named by hardware buffer descriptors or 64-bit root SRV addresses, and textures
// (`srv <i> tex2d table <param> <byte>` inputs, `uav <i> tex1d/tex2d/tex2da/tex3d float table <param> <byte> channels 4` outputs) named
// by the token of a view the title created (device slots 18 and 19). `smp` lines become static samplers. Entry
// 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
// count of an output (`uav_descword3`, `uav_stride`, `uav_numrecords`). Any other manifest line makes the translation
Expand Down Expand Up @@ -170,12 +170,12 @@ const char* load_compute_translation(ComputeTranslation& tr)
}
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});
// 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";
// A static sampler, decoded as the draw path does: plain (smp), with the descriptor's LOD clamp (smpl) or a
// comparison one (smpc). Not modelled here: the run-time sampler heap table (smpheap).
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() &&
std::sscanf(line.c_str(), "smpl %u %x %x %x %x%n", &i, &x, &y, &z, &w, &used) == 5 ||
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 2D texture"
Expand Down
106 changes: 96 additions & 10 deletions tests/startup_compute_dispatch_texture_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@

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.
changed to clamp reads red 0 (the control); an `smpheap` line is refused with its reason.

Compared input: case A with a level-zero comparison sample (image_sample_c_lz) under a constant point, clamp, LESS
sampler that the translator writes as `smpc`, and a 2x1 R32_FLOAT texture of depths 1.0 and 2.5
(dispatch-sampled-depth). The reference is the thread's z id and u its x id, so the 288 written words hold both
outcomes; all 320 words equal a CPU model of the comparison. The same program with the manifest's function changed
to GREATER matches the GREATER model (the control).

The two-mips mode writes two different patterns through separate UAVs of the same texture, then swaps the views.
Exit 77 when the adapter is not available.
Expand Down Expand Up @@ -373,22 +379,101 @@ def reds(got):
layer = clean(result)

# Control: the same program under the manifest's sampler changed to clamp in U, V and W reads red 0, which the
# check above refuses; a comparison sampler and a sampler heap table stay refused, each with its reason.
# check above refuses; a sampler heap table stays refused with its reason.
manifest = cache / f'{key:016x}.txt'
original = manifest.read_text()
assert original.count(line) == 1, original
manifest.write_bytes(original.replace(line, line.replace('smpl 0 0x0 ', 'smpl 0 0x92 ')).encode())
got, result = run(directory, consumer, env, 'dispatch-sampled-2x1', layout)
assert len(lines(result, 'Dispatch(3, 2, 2) translated')) == 1 and reds(got) == {0.0}, result.stderr[-3000:]
for new, reason in ((line.replace('smpl', 'smpc'), 'a comparison sampler'),
(line + '\nsmpheap 2048 1 fallback 2047', 'a sampler from the sampler heap')):
manifest.write_bytes(original.replace(line, new).encode())
got, result = run(directory, consumer, env, 'dispatch-sampled-2x1', layout)
refused = lines(result, 'refused: ')
assert got == [C.SENTINEL] * C.WORDS and len(refused) == 1 and refused[0].endswith('refused: ' + reason), (
reason, refused, result.stderr[-3000:])
manifest.write_bytes(original.replace(line, line + '\nsmpheap 2048 1 fallback 2047').encode())
got, result = run(directory, consumer, env, 'dispatch-sampled-2x1', layout)
refused = lines(result, 'refused: ')
assert got == [C.SENTINEL] * C.WORDS and len(refused) == 1 and refused[0].endswith(
'refused: a sampler from the sampler heap'), (refused, result.stderr[-3000:])
print(f'filtered input: an smpl wrap and linear sampler reads red 0.5 at the left edge of a 2x1 texture; the same '
f'program with a clamp sampler reads 0; comparison and heap samplers refused; debug layer: {layer}')
f'program with a clamp sampler reads 0; a heap sampler is refused; debug layer: {layer}')


# The texture input case with a comparison sample: s20 clamp in U and V, TRUNC_COORD and LESS (point filters, LOD
# range 0). The reference is v7 = float(thread id z), u is v8 = float(thread id x) and v is v9 = 0. The five new
# instructions are not assembled by llvm-mc here; the decoder is checked to read them as written.
POINT_LESS = 2 | 2 << 3 | 1 << 27 | 1 << 12
POINT_GREATER = POINT_LESS ^ 1 << 12 | 4 << 12
COMPARED = SAMPLED[:2] + ('s_mov_b32 s20, 0x%x' % POINT_LESS, SAMPLED[2], 'v_cvt_f32_u32_e32 v7, v2',
'v_cvt_f32_u32_e32 v8, v0', 'v_mov_b32_e32 v9, 0',
'image_sample_c_lz v12, v[7:9], s[24:31], s[20:23] dmask:0x1 dim:SQ_RSRC_IMG_2D',
'v_xor_b32_e32 v5, v5, v12')


def compared(directory, consumer, dxc, env):
"""The sampler bound is the manifest's `smpc` one: a 2x1 R32_FLOAT texture (1.0, 2.5) is compared at u = 0 (the
left texel) or u = 1 (clamped to the right one) with the references 0.0 to 3.0. A word is XORed with the bits of
1.0f where the CPU model of the comparison passes and is left as case A wrote it where the model fails."""
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({COMPARED[2]: 'ff0394be' + struct.pack('<I', POINT_LESS).hex(), COMPARED[4]: '020d0e7e',
COMPARED[5]: '000d107e', COMPARED[6]: '8002127e', COMPARED[7]: '0801bcf0070ca600',
COMPARED[8]: '05190a3a'})
code = C.splice(*COMPARED)
decoded = [i[3] for i in G.decode(code) if i[0] >= C.STORE_A][:len(COMPARED)]
assert decoded == list(COMPARED), decoded
pipeline = C.translate('A', code=code)
line = 'smpc 0 0x%x 0x0 0x0 0x0' % POINT_LESS
assert 'v12 = U(r1.SampleCmpLevelZero(q0, float2(F(v8), F(v9)), F(v7)));' in pipeline.hlsl_cs() and \
pipeline.manifest().endswith('srv 1 tex2d table 1 64\nuav 0 table 1 16\n' + line + '\n'), pipeline.manifest()
(directory / 'program').write_bytes(code)
(directory / 'data').write_bytes(DATA)
(directory / 'state').write_bytes(state_a(group, rsrc2))
one = struct.unpack('<I', struct.pack('<f', 1.0))[0]
written = 24 * 3 * 2 * 2
plain = C.expected('A')
layout = '<%dI' % C.WORDS
depths = (1.0, 2.5) # the consumer's texels, left and right

def model(passes):
# Word i is thread (x, y, z) of a 2x3x4 group: i % 24 == x + 2 * y + 6 * z. Reference z, texel x.
assert group == (2, 3, 4)
return [word ^ (one if passes(float(i % 24 // 6), depths[i % 2]) else 0)
for i, word in enumerate(plain[:written])] + plain[written:]

def match(got, want, result, what):
refused = lines(result, 'refused: ')
assert not refused, f'{what}: {refused[0]}'
wrong = [i for i in range(C.WORDS) if got[i] != want[i]]
assert not wrong, '%s: word=%d got=%08x expected=%08x (%d words differ)\n%s' % (
what, wrong[0], got[wrong[0]], want[wrong[0]], len(wrong), result.stderr[-3000:])
assert len(lines(result, 'Dispatch(3, 2, 2) translated')) == 1, result.stderr[-3000:]

less, greater = model(lambda ref, texel: ref < texel), model(lambda ref, texel: ref > texel)
passing = [sum(a != b for a, b in zip(m, plain)) for m in (less, greater)]
differ = sum(a != b for a, b in zip(less, greater))
assert passing == [144, 108] and differ == 252, (passing, differ)

got, result = run(directory, consumer, env, 'dispatch-sampled-depth', layout)
assert got == [C.SENTINEL] * C.WORDS, 'a Dispatch without a translation wrote the output'
key = int(lines(result, 'x2w-draw compute request')[0].split()[3], 16)
translate(cache, dxc, key)

got, result = run(directory, consumer, env, 'dispatch-sampled-depth', layout)
match(got, less, result, 'smpc LESS')
layer = clean(result)

# Control: the function is the manifest's. Under GREATER the same program passes where the reference is above
# the texel, a result the check above refuses at 252 words.
manifest = cache / f'{key:016x}.txt'
original = manifest.read_text()
assert original.count(line) == 1, original
manifest.write_bytes(original.replace(line, 'smpc 0 0x%x 0x0 0x0 0x0' % POINT_GREATER).encode())
got, result = run(directory, consumer, env, 'dispatch-sampled-depth', layout)
match(got, greater, result, 'smpc GREATER')
clean(result)
print(f'compared input: an smpc point, clamp, LESS sampler passes at 144 of the {written} written words and all '
f'{C.WORDS} words match the CPU model; the same program under GREATER matches its model (108 pass, 252 '
f'words differ from LESS); debug layer: {layer}')


def uniform_image_load_mip(directory, consumer, dxc, env):
Expand Down Expand Up @@ -483,6 +568,7 @@ def main(consumer, dxc, adapter, mode=None):
store_3d1d(directory / form, consumer, dxc, env, form)
sample(directory / 'sample', consumer, dxc, env)
filtered(directory / 'filtered', consumer, dxc, env)
compared(directory / 'compared', consumer, dxc, env)
uniform_image_load_mip(directory / 'uniform_image_load_mip', consumer, dxc, env)
print(f'startup_compute_dispatch_texture: PASS on {adapter}')

Expand Down
Loading
Loading