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
8 changes: 7 additions & 1 deletion docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -11009,7 +11009,13 @@ ctest --preset release --no-tests=error -R '^cpu_aot_vex_parity$' -V

## Startup root signature changes

`tests/startup_root_reset.cpp` and `tests/startup_root_reset_check.py` bind a 1D texture through a descriptor table (list slot 36) and check that setting a different root signature (slot 34) drops the table, that the same one keeps it, and that six out-of-range `SetGraphicsRoot32BitConstants` calls log four lines and leave the table alone. Details are in [startup-runtime.md](startup-runtime.md#root-signature-changes-and-dropped-root-constants). Limits: synthetic command-stream fixture; the `*_hardware` target skips when no hardware D3D12 adapter is present.
`tests/startup_root_reset.cpp` and `tests/startup_root_reset_check.py` bind a 1D texture through a descriptor table
(list slot 36) and a root constant through slot 40. The pixel shader puts the constant in the blue channel. The
fixture checks that a different root signature (slot 34) clears both inputs, the same signature preserves them,
and six out-of-range constant writes log four lines and preserve both bindings. Before clearing cached constants,
the switched case reads back blue 173 on a pixel where the fixture expects zero. Details are in
[startup-runtime.md](startup-runtime.md#root-signature-changes-and-dropped-root-constants). The synthetic hardware
case returns 77 without a physical D3D12 adapter.

```powershell
cmake --build --preset dev --target startup_stream_consumer
Expand Down
10 changes: 6 additions & 4 deletions docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,15 @@ data. The comparison uses the kept bytes because the ring is write-combined memo

## Root signature changes and dropped root constants

Command-list slot 34 (`SetGraphicsRootSignature`) leaves no descriptor table bound when the signature differs from the
current one, as it already clears the root addresses; setting the same signature again keeps the tables. Slot 40
Command-list slot 34 (`SetGraphicsRootSignature`) clears the translated draw's cached descriptor tables, root
addresses and root constants when the signature changes. Setting the same signature again preserves these arguments.
An unset constant reads as zero in the translated draw. Slot 40
(`SetGraphicsRoot32BitConstants`) keeps 16 parameters of 8 words. A call with a parameter of 16 or more or an offset of
8 or more is dropped, and one whose count passes the eighth word is truncated; each logs one line
(`root constants dropped` or `root constants truncated`, with the parameter, offset and count) for the first four such
calls in the process. `startup_root_reset_{warp,hardware}` draws a 1D texture through a bound table with no change, the same
signature, a switched signature and six out-of-range calls.
calls in the process. `startup_root_reset_{warp,hardware}` draws a 1D texture through a bound table and writes a
root constant into the blue channel. It checks the original bindings, the same signature, a switched signature and
six out-of-range calls. The switched case clears both inputs to the shader.

## Null bindings in translated draws

Expand Down
9 changes: 5 additions & 4 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1778,12 +1778,13 @@ void __stdcall list_graphics_root(Wrapper* self, void* root)
{
Wrapper* w = as_wrapper(root);
Pass& pass = pass_of(self);
// A different root signature leaves no descriptor table bound; the same one keeps them.
if (pass.root != (w ? root_info(w) : nullptr))
for (auto& table : pass.view_tables) table = {};
const auto* params = w ? root_info(w) : nullptr;
if (pass.root != params)
// A different root signature invalidates the title's cached root arguments.
if (pass.root != params) {
for (auto& table : pass.view_tables) table = {};
std::fill_n(pass.root_address, 16, 0);
std::memset(pass.constants, 0, sizeof(pass.constants));
}
pass.root = params;
pass.srv_bound = 0;
const bool real = w && w->own().kind == root_signature_kind && w->own().real;
Expand Down
12 changes: 7 additions & 5 deletions tests/startup_root_reset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ namespace {
template<class T> StreamWord word_of(T* value) { return reinterpret_cast<std::uintptr_t>(value); }
}

// A descriptor table is bound with SetGraphicsRootDescriptorTable (list slot 36) under the stream root signature, then
// A descriptor table and a root constant are set under the stream root signature, then
// X2W_TEST_ROOT_RESET names what the title does before the draw:
// bound nothing: the table stays
// same_root SetGraphicsRootSignature with the same signature: the table stays
// switch_root a different signature and back: the table is gone
// out_of_range six SetGraphicsRoot32BitConstants calls that reach no stored word, in four ways; the table stays
// bound nothing: both bindings stay
// same_root SetGraphicsRootSignature with the same signature: both bindings stay
// switch_root a different signature and back: both bindings are invalidated
// out_of_range six out-of-range SetGraphicsRoot32BitConstants calls; both bindings stay
int draw_root_reset(void* guest, void* list)
{
char mode[16]{};
Expand Down Expand Up @@ -47,6 +47,8 @@ int draw_root_reset(void* guest, void* list)
invoke(list, 34, root); // SetGraphicsRootSignature
invoke(list, 32, 1, word_of(&heap));
invoke(list, 36, 0, invoke(heap, 10));
const std::uint32_t blue = 173;
invoke(list, 40, 1, 1, word_of(&blue), 0);
if (!std::strcmp(mode, "same_root")) {
invoke(list, 34, root);
} else if (!std::strcmp(mode, "switch_root")) {
Expand Down
18 changes: 9 additions & 9 deletions tests/startup_root_reset_check.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""A different root signature drops the bound descriptor tables, the same one keeps them, and root constants that
reach no stored word are logged (list slots 34, 36 and 40)."""
"""Check root table and constant lifetime through translated rendering (list slots 34, 36 and 40)."""
from pathlib import Path
import sys
import tempfile
Expand All @@ -8,22 +7,23 @@

HLSL = (
"Texture1D<float4> tex : register(t0);\n"
"cbuffer Entry : register(b0) { uint4 entry; };\n"
"float4 main(float4 p : SV_Position) : SV_Target {\n"
" float4 value = tex.Load(int2(uint(p.x) % 4, 0));\n"
" return float4(value.r, value.a, 0, 1);\n"
" return float4(value.r, value.a, float(entry.w) / 255.0, 1);\n"
"}\n"
)
CLEAR = bytes((0, 0, 255, 255))
TEXELS = bytes(b for x in range(4) for b in (10 + 20 * x, 200 - 30 * x, 0, 255))
NO_TABLE = bytes((0, 0, 0, 255)) * 4
TEXELS = bytes(b for x in range(4) for b in (10 + 20 * x, 200 - 30 * x, 173, 255))
NO_BINDINGS = bytes((0, 0, 0, 255)) * 4


def run(directory, consumer, backend, mode):
folder = directory / mode
folder.mkdir()
log = folder / "trap.log"
pixels = draw_texture(folder, consumer, backend, HLSL, {"X2W_TEST_ROOT_RESET": mode, "X2W_TRAP_LOG": str(log)},
"tex1d")
binding="srv 0 tex1d table 0 0\nudsrc 3 rootconst 1 0")
lines = [line for line in log.read_text(errors="replace").splitlines() if "root constants" in line] if log.exists() else []
return pixels, lines

Expand All @@ -32,9 +32,9 @@ def check(directory, consumer, backend):
# The left half of the 8x4 target is drawn, the right half keeps the clear colour.
cases = (
("bound", TEXELS, 0),
("same_root", TEXELS, 0), # the same root signature again keeps the table
("switch_root", NO_TABLE, 0), # another root signature drops it: the shader reads zeros
("out_of_range", TEXELS, 4), # six calls reach no stored word; the first four are logged
("same_root", TEXELS, 0),
("switch_root", NO_BINDINGS, 0),
("out_of_range", TEXELS, 4),
)
for mode, drawn, logged in cases:
pixels, lines = run(directory, consumer, backend, mode)
Expand Down
12 changes: 10 additions & 2 deletions tests/startup_stream_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,21 @@ int prepare_stream_pipeline(const char* vs_path, const char* ps_path, const char

D3D12_ROOT_SIGNATURE_DESC root_desc{};
const D3D12_DESCRIPTOR_RANGE srv{D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, 0, 0, 0};
D3D12_ROOT_PARAMETER table{};
D3D12_ROOT_PARAMETER textured_parameters[2]{};
if (textured) {
D3D12_ROOT_PARAMETER& table = textured_parameters[0];
table.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
table.DescriptorTable = {1, &srv};
table.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
root_desc.NumParameters = 1;
root_desc.pParameters = &table;
root_desc.pParameters = textured_parameters;
if (GetEnvironmentVariableW(L"X2W_TEST_ROOT_RESET", nullptr, 0)) {
D3D12_ROOT_PARAMETER& constant = textured_parameters[1];
constant.ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;
constant.Constants = {0, 0, 1};
constant.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
root_desc.NumParameters = 2;
}
}
// X2W_TEST_ROOT_CBV=<mode> declares root parameter 0 as a constant buffer view (startup_root_cbv.cpp);
// "wrong_type" declares a shader resource view there instead.
Expand Down
Loading