Skip to content
Open
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
6 changes: 4 additions & 2 deletions docs/experiments/startup-compute-native-dispatch.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ cache and no offline translator, and compares all 320 output words with a CPU re
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. Refused: every other view kind (a scalar buffer load included), a sampler, more than
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 `<key>.txt`.
Expand Down Expand Up @@ -80,7 +82,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 texture, a sampler and the typed, half and indexed views are refused. This is the
- 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 (`<key>.txt` from `compile_dxil --requests`) 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
Expand Down
106 changes: 106 additions & 0 deletions docs/experiments/startup-compute-native-samplers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Constant static samplers on the in-process compute route (#3149)

With `X2W_NATIVE_SHADERS` set, the stand-in translates a slot 67 compute request with the C++ compiler at its
first Dispatch ([route](startup-compute-native-dispatch.md)). Until this change it refused every program that
samples. The compiler already resolves a sampler whose four words are constants into a `ComputeSampler`, and
`translate_compute_natively` (`src/startup_stubs/d3d12_compute_dispatch.inl`) now turns those into the static
samplers of the translated root signature, in register order.

## What is bound

- A comparison sampler (`comparison`) is bound as a comparison static sampler, as an `smpc` manifest line is.
- Any other sampler with `explicit_lod` carries the descriptor's MIN_LOD and MAX_LOD, as an `smpl` line does.
The direct compute path sets `explicit_lod` on every sampler it emits (`sample || implicit` in
`src/gfx1013_direct_compute.inl`, and the gathers through `gather_base_level`), so this route produces no
plain `smp` sampler. The conversion keeps that case, but no compute program reaches it.
- The texture of a comparison sample (`comparison_texture_srv`, 2D) is bound like a plain 2D texture input.
- A natively bound 2D texture input sets the slot's input dimension. Without it the dispatch path takes the
input for a described 1D, 2D-array or 3D view and refuses the Dispatch with "an input texture view is not 3D",
so no natively translated program could read a 2D texture.
- A failed native translation clears its samplers, so a published `<key>.txt` read next numbers its sampler
lines from 0.

The route has no refusal that names a sampler. Sampler words that are neither constants nor one entry of the
sampler heap are refused by the compiler ("nonconstant sampler") before the stand-in binds anything. A
fixed-index sampler of the directly indexed sampler heap (#3140) reaches the route as a `heap_sampler` view,
which is not bound and is refused by the view-kind message ("view kind N is not bound for a natively
translated program"). That is read from the code, not run here; binding that heap is the follow-up of #3076.
The capacity refusal now reads "more entry values, inputs or outputs than the dispatch path binds".

## Fixture

Step 5 of [`startup_compute_dispatch_native_check.py`](../../tests/startup_compute_dispatch_native_check.py),
source-built. The consumer mode `texture-volume-native-sampled` is the 3D float store of `texture-volume` plus a
2x1 R32_FLOAT texture with a 1x1 second mip, whose three texels hold different sentinel words. One program samples it four times and
stores the four results as the channels of a 3D float texture:

| Channel | Instruction | Sampler words | What it selects |
|---|---|---|---|
| 0 | `image_sample_lz` | wrap U, MAX_LOD 15.996 | mip 0, wrapped texel |
| 1 | `image_sample_l`, LOD 1.25 | mirror U, MIN_LOD 0, MAX_LOD 0 | mip 0 (the clamp), mirrored texel |
| 2 | `image_sample_l`, LOD 0 | clamp U, MIN_LOD 1, MAX_LOD 1 | mip 1 (the clamp) |
| 3 | `image_sample_c_lz` | clamp, LESS, point | 1.0 where the reference is below the texel, else 0.0 |

U is 0.3 and the reference -0.1875, each doubled once per global X. The eight threads of a row therefore read
both texels under wrap and mirror in different threads, and the comparison passes in six threads and fails in
two. All filters are point filters, so the result is exact. Every one of the 6,144 volume words is compared
with a CPU model that decodes the address mode, the LOD limits and the comparison from the sampler words.

Control: the same program with the first sampler's words loaded from the descriptor table
(`s_load_dwordx4 s[40:43], s[12:13], 0x60`) is refused with "nonconstant sampler", the Dispatch is refused with
"no translation", and every volume word keeps its sentinel.

## Command and result

```powershell
.\tools\dev.ps1
cmake --build --preset dev --target d3d12_xs_shim startup_stream_consumer compile_dxil
ctest --preset dev --no-tests=error -V -R '^(startup_compute_dispatch_native_(hardware|warp)|native_image_store_exact_width_hardware_(r|rg|rgb|array|volume|rgba|sparse)|startup_compute_dispatch_texture_hardware|startup_compute_descriptor_scc_hardware)$'
```

Commit `af6045df` on `main` at `f5a6f6ba`, Windows 11 build 26300.9974, Debug build, MSVC 14.44.35207, Windows
SDK 10.0.26100 DXC, D3D12 debug layer enabled. 11 tests selected: 10 passed, 1 capability skip, 0 failed.

| Test | Adapter, driver | Result |
|---|---|---|
| `startup_compute_dispatch_native_hardware` | NVIDIA GeForce RTX 4090, 32.0.16.1088 | PASS. The four sampled channels match the CPU model in all 6,144 volume words, the memory sampler control leaves the volume untouched, and the other steps of the check pass. No debug-layer error message. |
| `native_image_store_exact_width_hardware_{r,rg,rgb,array,volume,rgba,sparse}` | the same | PASS (7 tests). These dispatch exact-width outputs through the same binding loop. |
| `startup_compute_dispatch_texture_hardware` | the same | PASS. It shares the consumer's texture modes. |
| `startup_compute_descriptor_scc_hardware` | the same | PASS. |

Capability skip, not a pass: `startup_compute_dispatch_native_warp` exits 77 on the Microsoft Basic Render
Driver 10.0.26100.9818, which reports no 32-lane waves. Before it exits, the check confirms that the sampled
program is refused by that capability reason with the volume untouched, and that the memory sampler control is
refused with "nonconstant sampler".

Earlier revisions of this change, stacked on #3049, gave the same result for the sampled step on `main` at
`429f11d5`, `7fcde81f` and `ee3347d3`.

## Mutations

Each mutation was applied to `src/startup_stubs/d3d12_compute_dispatch.inl` at `af6045df` on its own, the
stand-in rebuilt, `startup_compute_dispatch_native_hardware` run, and the file restored.

| Mutation | Result |
|---|---|
| The file as on `main` (`f5a6f6ba`) | FAIL at the sampled program: `not translated natively: view kind 11 is not bound for a natively translated program`. The comparison texture view is refused before the sampler check is reached. |
| Only the sampler check restored; the comparison view stays accepted | FAIL at the sampled program: `not translated natively: a sampler, or more entry values, inputs or outputs than the dispatch path binds`. |
| The 2D input dimension left unset | FAIL: the program translates, then `Dispatch(2, 1, 1) refused: an input texture view is not 3D`. |
| `lod_limits` always false | FAIL: `sampled: word 2049 got=bf000080 expected=bf000000 (32 words differ)`. Channels 1 and 2 of all 16 threads read the other mip. |
| `comparison` always false | FAIL: `sampled: word 2051 got=bf000080 expected=00000000 (16 words differ)`. Channel 3 of all 16 threads holds a texel, not a comparison result. |
| Samplers bound in reverse register order | FAIL: `sampled: word 2049 got=bf000080 expected=bf000000 (56 words differ)`. |
| Restored | PASS. |

The comparison and order mutations were caught by the readback, not by a debug-layer message.

## Limits

- Constant sampler words only. A sampler read from memory, including the sampler heap, stays refused (#3076).
- One 2D texture format was sampled (R32_FLOAT), with point filters. Linear and anisotropic filters, border
colors and 3D or array textures were not run on this route.
- A plain sampler without LOD limits was not run: the direct compute path does not emit one.
- A comparison sampler is bound without the LOD limits, as on the published route. The compute path compares at
level zero and the compiler requires MIN_LOD 0, so the limits cannot change the result.
- Clearing the samplers after a failed native translation follows from the code (the manifest reader requires
sampler line `i` to be the next entry). No run exercises it.
- One adapter, in a Debug build. No start-up run used the switch.
7 changes: 5 additions & 2 deletions docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -1006,8 +1006,11 @@ against their key, translated and compiled to DXIL in the process. The compiler'

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. Anything else (a
scalar buffer load, a texture, a sampler, a typed or indexed view, an instruction the compiler does
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 `<key>.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
Expand Down
22 changes: 18 additions & 4 deletions src/startup_stubs/d3d12_compute_dispatch.inl
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ const char* load_compute_translation(ComputeTranslation& tr)
// X2W_NATIVE_SHADERS=<directory of dxcompiler.dll and dxil.dll>: 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 <key>.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.
// with root constants, raw table buffers and exact-width typed texture outputs. 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)
{
Expand All @@ -291,6 +292,7 @@ bool translate_compute_natively(ComputeTranslation& tr)
tr.uds.clear();
tr.srvs.clear();
tr.uavs.clear();
tr.samplers = {}; // a published manifest read next numbers its sampler lines from 0
tr.root.Reset();
tr.pipeline.Reset();
static LONG logged;
Expand Down Expand Up @@ -336,7 +338,10 @@ bool translate_compute_natively(ComputeTranslation& tr)
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 texture_input = view.kind == x2w::ComputeViewKind::texture_srv && view.image_dimension == 1;
// 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) &&
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;
auto& slots = output ? tr.uavs : tr.srvs;
Expand All @@ -346,6 +351,8 @@ bool translate_compute_natively(ComputeTranslation& tr)
" is not bound for a natively translated program");
slots.push_back({static_cast<int>(view.root_parameter), static_cast<int>(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;
if (x2w::accepts_cbv_window(view.kind))
Expand All @@ -359,8 +366,15 @@ bool translate_compute_natively(ComputeTranslation& tr)
D3D12_UAV_DIMENSION_TEXTURE2D;
}
}
if (!program.samplers.empty() || tr.uds.size() > 32 || tr.srvs.size() > 32 || tr.uavs.size() > 8)
throw std::runtime_error("a sampler, or more entry values, inputs or outputs than the dispatch path binds");
// 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).
for (const auto& sampler : program.samplers) {
tr.samplers.statics.push_back(sampler.words);
tr.samplers.comparison.push_back(sampler.comparison);
tr.samplers.lod_limits.push_back(sampler.explicit_lod && !sampler.comparison);
}
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))) ||
Expand Down
Loading
Loading