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
11 changes: 3 additions & 8 deletions runtime/src/root_signature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ PackResult pack(const RootSignature& sig) {
out.error = "unknown root parameter type";
return out;
}
if (p.type == ParamType::Constants && p.num_values == 0) {
out.error = "root constant parameter needs at least 1 dword";
return out;
}
// A constant parameter of 0 values is valid (the host serializer warns, CreateRootSignature answers S_OK):
// it takes no dwords, so it gets no provenance entry and the parameters after it pack as if it were absent.
const uint32_t dwords = p.type == ParamType::Table ? 2
: p.type == ParamType::Descriptor ? 2
: p.num_values;
Expand Down Expand Up @@ -71,15 +69,12 @@ StagePackResult pack_stage(const RootSignature& sig, Stage stage) {
out.error = "unknown root parameter type";
return out;
}
if (p.type == ParamType::Constants && p.num_values == 0) {
out.error = "root constant parameter needs at least 1 dword";
return out;
}
cost += param_cost(p.type, p.num_values);
if (cost > kMaxD3d12Cost) {
out.error = "root signature exceeds the 64-dword D3D12 cost limit";
return out;
}
// 0 for a constant parameter of 0 values, which then shares its first SGPR with the next parameter.
const uint64_t dwords = stage_dwords(p.type, p.num_values);
if (!spilling && next + dwords <= kMaxUserSgprs) {
first.push_back(static_cast<uint32_t>(next));
Expand Down
8 changes: 5 additions & 3 deletions runtime/src/root_signature.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ constexpr uint32_t kMaxD3d12Cost = 64;
// Packs root parameters in order. Static samplers consume no dwords.
// Fails (ok == false) when the signature needs more than 32 user SGPRs
// (spill layout unknown: refuse, do not guess) or exceeds the 64-dword
// D3D12 cost limit, or holds more than 64 root parameters.
// D3D12 cost limit, or holds more than 64 root parameters. A constant
// parameter of 0 values takes no dwords, as on the host.
[[nodiscard]] PackResult pack(const RootSignature& sig);

// The console layout of root arguments in a stage's user SGPRs (docs/d3d12_vulkan.md section 3.5, D3D-448).
Expand Down Expand Up @@ -183,8 +184,9 @@ struct StagePackResult {
};

// Lays the parameters out for `stage` in the console layout. Fails like pack() on more than 64 parameters, an
// unknown parameter type, a constant parameter of 0 values, or the 64-dword cost limit. Compute spills any trailing
// parameters from the first one that does not fit whole into spill_dword; non-compute stages refuse any spill.
// unknown parameter type or the 64-dword cost limit. Compute spills any trailing parameters from the first one
// that does not fit whole into spill_dword; non-compute stages refuse any spill. A constant parameter of 0 values
// takes no SGPRs and no spill dwords.
[[nodiscard]] StagePackResult pack_stage(const RootSignature& sig, Stage stage);

// D3D12 cost of one parameter against the 64-dword limit.
Expand Down
65 changes: 64 additions & 1 deletion runtime/test/root_signature_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,39 @@ int main() {
check(!r.ok && r.error.find("64-dword") != std::string::npos,
"65 constants refused by cost limit");
}
// 64 constants are within the cost limit, as on the host: pack() and the graphics stages refuse them only for
// the spill past the user SGPRs, and that is what the error says; compute spills them whole.
{
RootSignature sig;
sig.params = {consts(64)};
const PackResult r = pack(sig);
check(!r.ok && r.error.find("64-dword") == std::string::npos && r.error.find("spill") != std::string::npos,
"64 constants pass the cost limit (refused only as a spill)");
const StagePackResult p = pack_stage(sig, Stage::Pixel);
check(!p.ok && p.error.find("64-dword") == std::string::npos && p.error.find("spill") != std::string::npos,
"pack_stage: 64 constants pass the cost limit for a pixel shader (refused only as a spill)");
const StagePackResult c = pack_stage(sig, Stage::Compute);
check(c.ok && c.d3d12_cost == 64 && c.spill_dword == std::vector<uint32_t>({0}) && c.spill_dwords == 64,
"pack_stage: 64 constants pass the cost limit for compute, spilled whole");
sig.params = {consts(32), consts(0), consts(32)};
check(pack(sig).error.find("spill") != std::string::npos, "32 + 0 + 32 constants cost 64, not more");
}
// A constant parameter of 0 values is valid on the host (the serializer warns, CreateRootSignature answers
// S_OK). It takes no dwords and no cost: the parameters around it pack as if it were absent.
{
RootSignature sig;
sig.params = {consts(0), consts(2), consts(0), view(), consts(0)};
const PackResult r = pack(sig);
check(r.ok && r.dword_count == 2 + 2 && r.d3d12_cost == 2 + 2, "zero-value constants pack to no dwords");
check(r.provenance.size() == 4 && r.provenance[0].param_index == 1 && r.provenance[1].param_index == 1 &&
r.provenance[2].param_index == 3 && r.provenance[2].sub_index == 0 &&
r.provenance[3].param_index == 3 && r.provenance[3].sub_index == 1,
"zero-value constants leave no provenance; their neighbours keep their dwords");
RootSignature alone;
alone.params = {consts(0)};
const PackResult a = pack(alone);
check(a.ok && a.dword_count == 0 && a.d3d12_cost == 0, "a lone zero-value constant parameter packs");
}
// A count that wraps a 32-bit sum (1 + 0xFFFFFFFF) is still refused, not packed past dwords[32].
{
rt::rootsig::RootSignature sig;
Expand Down Expand Up @@ -188,6 +221,19 @@ int main() {
const PackResult r = pack(d.sig);
check(r.ok && r.dword_count == 3 + 2, "parsed RTS0 packs to 5 dwords");
}
// The same blob with 0 values in its constant parameter: parsed and packed, the CBV taking the first dwords.
{
std::vector<uint8_t> payload = build_payload();
payload[56] = 0; // the constants' value count (payload at 48: register, space, count)
const DecodeResult d = parse_rts0(payload.data(), payload.size());
check(d.ok && d.sig.params.size() == 2 && d.sig.params[0].num_values == 0,
"RTS0 with a zero-value constant parameter parses");
const PackResult r = pack(d.sig);
check(r.ok && r.dword_count == 2 && r.provenance[0].param_index == 1, "zero-value RTS0 packs the CBV at 0");
const StagePackResult c = pack_stage(d.sig, Stage::Compute);
check(c.ok && c.first_sgpr.size() == 2 && c.first_sgpr[0] == 4 && c.first_sgpr[1] == 4 && c.end_sgpr == 6,
"pack_stage: zero-value RTS0 puts the CBV at s4");
}

// RTS0 controls: unknown version, wild offsets and truncation fail loudly.
{
Expand Down Expand Up @@ -541,9 +587,26 @@ int main() {
bad_param.type = static_cast<ParamType>(0xffffffffu);
invalid_sig.params = {bad_param};
check(!pack_stage(invalid_sig, Stage::Compute).ok, "pack_stage: invalid parameter enum rejected");
// Zero-value constants are accepted (the host takes them) and take no SGPRs: the next parameter starts where
// they would have.
RootSignature zero;
zero.params = {consts(0)};
check(!pack_stage(zero, Stage::Compute).ok, "pack_stage: zero-value constants rejected");
const StagePackResult lone = pack_stage(zero, Stage::Compute);
check(lone.ok && lone.first_sgpr.size() == 1 && lone.first_sgpr[0] == 4 && lone.end_sgpr == 4 &&
lone.spill_dwords == 0,
"pack_stage: zero-value constants accepted");
zero.params = {consts(1), consts(0), table(), consts(0), consts(26)};
const StagePackResult mixed = pack_stage(zero, Stage::Compute);
check(mixed.ok && mixed.first_sgpr.size() == 5 && mixed.first_sgpr[0] == 4 && mixed.first_sgpr[1] == 5 &&
mixed.first_sgpr[2] == 5 && mixed.first_sgpr[3] == 6 && mixed.first_sgpr[4] == 6 &&
mixed.end_sgpr == 32 && mixed.spill_dwords == 0 && mixed.d3d12_cost == 1 + 1 + 26,
"pack_stage: zero-value constants between others fill compute exactly, offsets unchanged");
// In the compute spill array a zero-value constant takes no dwords either: the next parameter shares its offset.
zero.params = {consts(28), table(), consts(0), consts(1)};
const StagePackResult spilled = pack_stage(zero, Stage::Compute);
check(spilled.ok && spilled.end_sgpr == 32 &&
spilled.spill_dword == std::vector<uint32_t>({kNoSpill, 0, 1, 1}) && spilled.spill_dwords == 2,
"pack_stage: zero-value constants in the compute spill array take no dwords");
RootSignature costly;
costly.params = {consts(32), consts(33)};
const StagePackResult costly_r = pack_stage(costly, Stage::Compute);
Expand Down
16 changes: 15 additions & 1 deletion tests/test_root_sig_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def test_cli_refuses_boolean_root_constant_count(tmp_path):
[sys.executable, "-I", TOOL, str(index_path), "--rootsig", str(sig_path)],
capture_output=True, text=True)
assert result.returncode == 1
assert "root signature refused: param 0: constants need an integer count >= 1" in result.stdout
assert "root signature refused: param 0: constants need an integer count >= 0" in result.stdout


def test_reconcile_compatible():
Expand Down Expand Up @@ -345,6 +345,20 @@ def test_console_layout_stage_bases():
assert dwords == [] and "no console base" in error


def test_zero_value_constants_take_no_dwords():
# As runtime/src/root_signature.cpp: a constant parameter of 0 values is accepted and packs to nothing.
sig = {"params": [{"type": "constants", "count": 0}, {"type": "cbv"},
{"type": "constants", "count": 0}, {"type": "table"}]}
dwords, error = rsc.pack_layout(sig)
assert error == "" and [(d["param"], d["sub"]) for d in dwords] == [(1, 0), (1, 1), (3, 0), (3, 1)]
dwords, error = rsc.pack_layout(sig, "cs")
assert error == "" and [(d["param"], d["sub"]) for d in dwords[4:]] == [(1, 0), (1, 1), (3, 0)]
dwords, error = rsc.pack_layout({"params": [{"type": "constants", "count": 0}]}, "cs")
assert error == "" and len(dwords) == 4
dwords, error = rsc.pack_layout({"params": [{"type": "constants", "count": -1}]})
assert dwords == [] and "count >= 0" in error


def test_console_layout_spill_refused():
sig = {"version": "1.1", "flags": 0, "params": [{"type": "table"} for _ in range(17)]}
assert rsc.pack_layout(sig, "ms")[1] != "" # 17 one-dword tables past s31
Expand Down
11 changes: 7 additions & 4 deletions tools/root_sig_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import sys

# Packing order mirrors runtime/src/root_signature.{h,cpp}: constants take
# NumValues dwords, root descriptors and tables take 2 (VA lo/hi), static
# samplers take none. With a stage (--model console) it mirrors pack_stage():
# NumValues dwords (none for 0 values), root descriptors and tables take 2
# (VA lo/hi), static samplers take none. With a stage (--model console) it mirrors pack_stage():
# from the stage's base, a table takes ONE dword (its 32-bit VA), and the
# SGPRs below the base are the driver's (D3D-448).
STAGE_BASE = {"ps": 8, "vs": 8, "gs": 8, "cs": 4, "ms": 16}
Expand Down Expand Up @@ -66,8 +66,11 @@ def pack_layout(sig, stage=None):
ptype = p.get("type", "")
if ptype == "constants":
count = p.get("count", 0)
if type(count) is not int or count < 1:
return [], f"param {index}: constants need an integer count >= 1"
# A count of 0 is valid, as in rt::rootsig::pack and pack_stage: the host serializer warns and
# CreateRootSignature answers S_OK. Such a parameter takes no dwords and its neighbours pack as if it
# were absent.
if type(count) is not int or count < 0:
return [], f"param {index}: constants need an integer count >= 0"
width, cost_add = count, count
elif ptype in ("cbv", "srv", "uav"):
width, cost_add = 2, 2
Expand Down
Loading