From 244e89f39cc6d7199d8ab5dd126eac2d9f95b062 Mon Sep 17 00:00:00 2001 From: Jan Trencansky Date: Wed, 25 Nov 2026 16:40:50 -0500 Subject: [PATCH] root_sig_contract: accept root constants of 0 values, as the runtime packer does tools/root_sig_contract.py says its packing mirrors runtime/src/root_signature.{h,cpp}, but it still refused a constant parameter with count 0, so after this branch it rejected a signature the runtime accepts. Accept count >= 0; a zero count adds no dwords and its neighbours pack as if it were absent, in both the s0 and the console layout. Raised in review. Co-Authored-By: Claude Opus 5.5 --- tests/test_root_sig_contract.py | 16 +++++++++++++++- tools/root_sig_contract.py | 11 +++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/tests/test_root_sig_contract.py b/tests/test_root_sig_contract.py index 19a02e353..a2230e43a 100644 --- a/tests/test_root_sig_contract.py +++ b/tests/test_root_sig_contract.py @@ -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(): @@ -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 diff --git a/tools/root_sig_contract.py b/tools/root_sig_contract.py index cda9213df..30cbe2f70 100644 --- a/tools/root_sig_contract.py +++ b/tools/root_sig_contract.py @@ -21,8 +21,8 @@ import json 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} @@ -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