diff --git a/docs/experiments/native-fixed-mesh57.md b/docs/experiments/native-fixed-mesh57.md index 008b6b5243..dcb0755f22 100644 --- a/docs/experiments/native-fixed-mesh57.md +++ b/docs/experiments/native-fixed-mesh57.md @@ -6,6 +6,8 @@ For an independently supplied raw pixel stage, `compile_dxil SDK CACHE gfx1013-r The source-built fixture creates a fixed pipeline with guest MS and PS instructions. The stand-in translates both to DXIL using its C++ shader library, creates a desktop mesh PSO and submits a guest command-list slot 61 indirect command. A GPU compute pass supplies the dispatch arguments. The tested interface uses an empty guest root, one to eight single-sample color attachments, explicit rasterizer, blend, depth and stencil state, and an MS with no resource bindings. The PS exports a constant to color target zero. +For offline `gfx1013-raw-ms` and `--refusals`, add `guest_root` to the `.mslayout` to derive descriptor-table, root-constant, root-SRV and directly indexed resource-heap SGPRs from a serialized root signature, read as version 1.1, in the request's sibling `.root` file. A root that denies mesh shader access is refused. The walk starts at s16 and mirrors the register advancement rule in `read_raw_graphics`. Every parameter consumes its register slots in signature order. Only supported parameters visible to all stages or mesh shaders are mapped. Root CBVs and UAVs consume two slots each and remain unmapped. Explicit `table`, `constants`, `root_address` and `resource_heap` lines are mutually exclusive with `guest_root`. The caller still supplies the mesh dimensions, topology, group stride, wave size and any packed-parameter or heap-read validation options. Runtime preparation continues to use the reviewed profile. + From the x64 Windows build environment: ```powershell diff --git a/src/gfx1013_compute_native.cpp b/src/gfx1013_compute_native.cpp index 5ad51d3491..9be8723846 100644 --- a/src/gfx1013_compute_native.cpp +++ b/src/gfx1013_compute_native.cpp @@ -2135,6 +2135,45 @@ static bool raw_center_inputs(unsigned enable_mask) { constexpr unsigned position_inputs = (0xfu << 8) | (1u << 15); return (enable_mask & ~position_inputs) == 2; } +template +static void walk_raw_graphics_root(const D3D12_ROOT_SIGNATURE_DESC1 &layout, + D3D12_SHADER_VISIBILITY visibility, AddInput add_input) { + const bool mesh = visibility == D3D12_SHADER_VISIBILITY_MESH; + unsigned sgpr = mesh ? 16 : 8; + for (unsigned j=0; j106-sgpr || j>=16) refuse(0,"raw graphics user data exceeds register range"); + if (visible && (!mesh || table || constants || p.ParameterType==D3D12_ROOT_PARAMETER_TYPE_SRV)) + add_input(table ? 3u : constants ? 0u : 1u, count, sgpr, j); + sgpr+=count; + } + if (layout.Flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) { + if (sgpr >= 106) refuse(0, "resource heap outside graphics user data"); + add_input(5, 1, sgpr++, 0); + } + if (!mesh && (layout.Flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED)) { + if (sgpr >= 106) refuse(0, "sampler heap outside graphics user data"); + add_input(6, 1, sgpr, 0); + } +} + +x2w::MeshRootMapping x2w::derive_mesh_root_mapping(const D3D12_ROOT_SIGNATURE_DESC1 &root) { + if (root.Flags & D3D12_ROOT_SIGNATURE_FLAG_DENY_MESH_SHADER_ROOT_ACCESS) + throw UnsupportedComputeShader("guest root denies mesh shader root access"); + MeshRootMapping mapping; + walk_raw_graphics_root(root, D3D12_SHADER_VISIBILITY_MESH, + [&](unsigned type, unsigned count, unsigned sgpr, unsigned root_parameter) { + if (type == 5) mapping.resource_heap_sgpr = sgpr; + else mapping.inputs.push_back({sgpr, root_parameter, type == 0 ? count : 0, type == 1}); + }); + return mapping; +} + static GuestShaderStage read_raw_graphics(std::string_view bytes, const D3D12_ROOT_SIGNATURE_DESC1 &layout, unsigned enable_mask, unsigned address_mask, bool mesh_pixel = false) { if (bytes.empty() || bytes.size() % 4 || bytes.size() > (1u << 20)) @@ -2218,26 +2257,10 @@ static GuestShaderStage read_raw_graphics(std::string_view bytes, const D3D12_RO } } // Same fixed-descriptor user data layout as the legacy graphics path. - unsigned sgpr = 8; - for (unsigned j=0; j106-sgpr || j>=16) refuse(0,"raw graphics user data exceeds register range"); - if (p.ShaderVisibility==D3D12_SHADER_VISIBILITY_ALL || - p.ShaderVisibility==(position ? D3D12_SHADER_VISIBILITY_VERTEX : D3D12_SHADER_VISIBILITY_PIXEL)) - stage.inputs.push_back({table ? 3u : constants ? 0u : 1u,0,count,0,uint8_t(sgpr),uint8_t(j)}); - sgpr+=count; - } - if (layout.Flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) { - if (sgpr >= 106) refuse(0, "resource heap outside graphics user data"); - stage.inputs.push_back({5,0,1,0,uint8_t(sgpr++),0}); - } - if (layout.Flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) { - if (sgpr >= 106) refuse(0, "sampler heap outside graphics user data"); - stage.inputs.push_back({6,0,1,0,uint8_t(sgpr),0}); - } + walk_raw_graphics_root(layout, position ? D3D12_SHADER_VISIBILITY_VERTEX : D3D12_SHADER_VISIBILITY_PIXEL, + [&](unsigned type, unsigned count, unsigned sgpr, unsigned root_parameter) { + stage.inputs.push_back({type,0,count,0,uint8_t(sgpr),uint8_t(root_parameter)}); + }); return stage; } diff --git a/src/gfx1013_compute_native.h b/src/gfx1013_compute_native.h index c844b6bf59..136b2fc754 100644 --- a/src/gfx1013_compute_native.h +++ b/src/gfx1013_compute_native.h @@ -208,6 +208,11 @@ struct MeshRootInput { unsigned sgpr, root_parameter, constant_count; // Zero words denotes a table unless root_address is set. bool root_address{}; }; +struct MeshRootMapping { + std::vector inputs; + std::optional resource_heap_sgpr; +}; +MeshRootMapping derive_mesh_root_mapping(const D3D12_ROOT_SIGNATURE_DESC1 &root); struct MeshStageLayout { unsigned threads, max_vertices, max_primitives, topology, group_stride; bool wave32; diff --git a/tests/native_compile_dxil_refusals_check.py b/tests/native_compile_dxil_refusals_check.py index 7389e56c6c..7185e27032 100644 --- a/tests/native_compile_dxil_refusals_check.py +++ b/tests/native_compile_dxil_refusals_check.py @@ -55,14 +55,14 @@ def put_mesh_request(directory, words, pixel, root, *, layout=None, wrong_key=Fa def check_mesh_report(parsed, report, translated, refusals, caller_layout=None): mesh = parsed["mesh57"] - assert mesh["examined"] == 4, mesh - assert mesh["no_refusal"] == translated and mesh["refused"] == 4 - translated, mesh + assert mesh["examined"] == 7, mesh + assert mesh["no_refusal"] == translated and mesh["refused"] == 7 - translated, mesh assert {row["reason"]: row["count"] for row in mesh["reasons"]} == refusals, mesh mesh_text = report.split("Mesh requests (device slot 57)\n", 1)[1].splitlines() assert mesh_text == [ - "Mesh requests examined: 4", + "Mesh requests examined: 7", f"No refusal recorded (not compiled): {translated}", - f"Refused: {4 - translated}", + f"Refused: {7 - translated}", "Count\tReason", *(f"{row['count']}\t{row['reason']}" for row in mesh["reasons"]), ], mesh_text @@ -186,6 +186,15 @@ def main(argv: list[str]) -> int: layout = "mesh1 128 3 1 2 128 64\n" m_ok = put_mesh_request(req_dir, triangle(), ps_words(4), g_root, layout=layout) m_root = put_mesh_request(req_dir, [0xF4080308, 0xFA000B20] + triangle(), b"", g_root, layout=layout) + # Load a descriptor from the table at s16 and export the fetched word. + mesh_with_root = triangle() + mesh_with_root[7] = 0x7E020C0C # v_cvt_f32_u32 v1,s12 + table_load = [0xBE980310, 0xBE990380, 0xF408080C, 0xFA000000, 0xF4200310, 0xFA000004] + put_mesh_request(req_dir, table_load + mesh_with_root, b"", g_root, layout=layout + "guest_root\n") + # An explicit root mapping line before guest_root is refused. + put_mesh_request(req_dir, table_load + mesh_with_root, ps_words(4), g_root, layout=layout + "constants 21 0 1\nguest_root\n") + # A second guest_root line is refused. + put_mesh_request(req_dir, [0xF4080308, 0xFA000B20] + triangle(), ps_words(4), g_root, layout=layout + "guest_root\nguest_root\n") m_no_layout = put_mesh_request(req_dir, triangle(), b"", g_root) m_bad_key = put_mesh_request(req_dir, triangle(), ps_words(4), g_root, layout=layout, wrong_key=True) caller_layout = work / "caller mesh.mslayout" @@ -228,13 +237,15 @@ def main(argv: list[str]) -> int: assert r1.stdout == r2.stdout, (r1.stdout, r2.stdout) parsed = json.loads(r1.stdout) mesh_refusals = { + "guest_root excludes explicit root mapping lines": 1, + "duplicate guest_root": 1, "mesh scalar memory needs a verified guest root mapping: s_load_dwordx4": 1, "mesh request has no entry layout": 1, "mesh request key does not match its files": 1, } - check_mesh_report(parsed, r_text.stdout, 1, mesh_refusals) + check_mesh_report(parsed, r_text.stdout, 2, mesh_refusals) del mesh_refusals["mesh request has no entry layout"] - check_mesh_report(json.loads(r_layout.stdout), r_layout_text.stdout, 2, mesh_refusals, caller_layout) + check_mesh_report(json.loads(r_layout.stdout), r_layout_text.stdout, 3, mesh_refusals, caller_layout) assert parsed["graphics"]["examined"] == 6, parsed assert parsed["graphics"]["no_refusal"] == 1, parsed assert parsed["graphics"]["refused"] == 5, parsed diff --git a/tests/native_direct_mesh_check.py b/tests/native_direct_mesh_check.py index 25f0b85d5e..79d9f082e8 100644 --- a/tests/native_direct_mesh_check.py +++ b/tests/native_direct_mesh_check.py @@ -137,8 +137,10 @@ def run(name, words, expected=None): # Runtime root constants select an actual scalar diamond. Each path # defines s20, consumed by a vertex export after the join. rs = root / 'fixture.hlsl' + root_signature = raw.with_suffix('.root') rs.write_text('#define RS "RootConstants(num32BitConstants=1,b0),DescriptorTable(SRV(t0))"') - subprocess.run([sdk/'dxc.exe','-T','rootsig_1_1','-E','RS','-Fo',raw.with_suffix('.root'),rs], check=True) + subprocess.run([sdk/'dxc.exe','-T','rootsig_1_1','-E','RS','-Fo',root_signature,rs], check=True) + branch_root = root_signature.read_bytes() layout.write_text('mesh1 128 3 1 2 128 64\nconstants 21 0 1\ntable 16 1') controlled = program.copy() controlled[7] = 0x7e020c14 # read joined s20 @@ -298,6 +300,85 @@ def lane_skip(name, body): 0x7e020282] # v_mov_b32_e32 v1, 2 lane_skip('nested-exec-restore', nested_exec) + + # Read the table and root SRV, use the first constant to select a + # heap descriptor, and include the second constant in a vertex output. + mapped = [ + 0xbe980310, 0xbe990380, # table s16 -> s24:s25 + 0xf408080c, 0xfa000000, # s_load_dwordx4 s[32:35], s[24:25], 0 + 0xf4200310, 0xfa000004, # s_buffer_load_dword s12, s[32:35], 4 + 0xbe9a0311, 0xbe9b0312, # root SRV s17:s18 -> s26:s27 + 0xbe9c03ff, 128, 0xbe9d03ff, 0x31016fac, + 0xf420034d, 0xfa000004, # s_buffer_load_dword s13, s[26:29], 4 + 0x880c0d0c, 0x880c140c, # s12 |= s13 | s20 + 0xbe9e0315, 0xbe9f0380, # heap s21 -> s30:s31 + 0x8716ff13, 31, 0x8f168516, # s22 = (s19 & 31) * 32 + 0xf408080f, 0x2c000000, # heap descriptor into s32:s35 + 0x7e020280, 0xe0002000, 0x80080a01, # formatted uint load into v10 + ] + mapped_export = exported.copy() + mapped_export[8] = 0x7e040c0c # v_cvt_f32_u32 v2,s12 + mapped += mapped_export + + table = 'DescriptorTable(SRV(t0), visibility=SHADER_VISIBILITY_MESH)' + srv = 'SRV(t1)' + constants = 'RootConstants(num32BitConstants=2,b0)' + + def write_mesh_root(*params, root_flags='CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED'): + rs.write_text('#define RS "' + ','.join((f'RootFlags({root_flags})', *params)) + '"') + subprocess.run([sdk/'dxc.exe', '-T', 'rootsig_1_1', '-E', 'RS', + '-Fo', root_signature, rs], check=True) + + base_layout = 'mesh1 128 3 1 2 128 64\nvalidate_uint_heap_reads\n' + explicit = 'table 16 0\nroot_address 17 1\nconstants 19 2 2\nresource_heap 21\n' + write_mesh_root(table, srv, constants) + layout.write_text(base_layout + explicit) + text = run('explicit-mesh-root', mapped) + for marker in ('cbufferLoadLegacy', 'rawBufferLoad', 'createHandleFromHeap'): + assert marker in text, marker + layout.write_text(base_layout + 'guest_root\n') + run('derived-mesh-root', mapped) + assert (root/'derived-mesh-root.dxil').read_bytes() == (root/'explicit-mesh-root.dxil').read_bytes() + + write_mesh_root(table, srv, constants, + root_flags='CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED | DENY_MESH_SHADER_ROOT_ACCESS') + run('deny-mesh-root-access', mapped, 'guest root denies mesh shader root access') + + write_mesh_root(srv, constants) + run('shifted-mesh-root', mapped, 'verified guest root mapping') + write_mesh_root('DescriptorTable(SRV(t0), visibility=SHADER_VISIBILITY_PIXEL)', srv, constants) + run('pixel-only-mesh-root', mapped, 'verified guest root mapping') + write_mesh_root('DescriptorTable(SRV(t2), visibility=SHADER_VISIBILITY_PIXEL)', table, srv, constants) + # The pixel-only table occupies s16, so the mesh table starts at s17. + shifted_table_load = [0xbe980311] + mapped[1:6] + root_export = program.copy() + root_export[7] = 0x7e020c0c # export the value in s12 + assert 'rawBufferLoad' in run('skip-pixel-mesh-root', shifted_table_load + root_export) + run('unmapped-pixel-mesh-root', mapped[:6] + root_export, + 'native compute +0x8: mesh scalar memory needs a verified guest root mapping: s_load_dwordx4') + + # Each root descriptor occupies s17:s18 between the table and root SRV. + srv_export = program.copy() + srv_export[7] = 0x7e020c0d # export the value in s13 + for name, descriptor in (('cbv', 'CBV(b1)'), ('uav', 'UAV(u0)')): + write_mesh_root(table, descriptor, srv, constants) + shifted_srv_load = [0xbe9a0313, 0xbe9b0314] + mapped[8:14] + text = run(f'shifted-mesh-root-{name}', shifted_srv_load + srv_export) + assert 'rawBufferLoad' in text, text + # Use the same load with the reserved, unmapped CBV/UAV pair. + run(f'unmapped-mesh-root-{name}', mapped[6:14] + srv_export, + 'native compute +0x18: unproven resource descriptor s26') + + write_mesh_root(table, srv, constants) + layout.write_text(base_layout + 'guest_root\nguest_root\n') + run('duplicate-mesh-root', mapped, 'duplicate guest_root') + for i, line in enumerate(explicit.splitlines()): + for j, mapping in enumerate((f'guest_root\n{line}', f'{line}\nguest_root')): + layout.write_text(base_layout + mapping) + run(f'conflicting-mesh-root-{i}-{j}', mapped, 'guest_root excludes explicit root mapping lines') + + root_signature.write_bytes(branch_root) + layout.write_text('mesh1 128 3 1 2 128 64\nconstants 21 0 1') # Three incoming scalar edges share one join. Distinct reaching # values of s20 must all be linked to the post-join mesh position. nested = [0xbf0c8015,0xbf840006,0xbf0c8115,0xbf840002, diff --git a/tools/startup/compile_dxil.cpp b/tools/startup/compile_dxil.cpp index 8722ddd14d..658b89f045 100644 --- a/tools/startup/compile_dxil.cpp +++ b/tools/startup/compile_dxil.cpp @@ -103,9 +103,17 @@ static void compile_source(x2w::ShaderDxilCompiler& compiler,const std::string& publish_atomic(destination, dxil.data(), dxil.size()); } -static x2w::MeshStageLayout read_mesh_layout(const fs::path& path) { +struct RawMeshLayout { + x2w::MeshStageLayout entry{}; + bool guest_root{}; + + bool needs_root() const { return guest_root || !entry.root_inputs.empty(); } +}; + +static RawMeshLayout read_mesh_layout(const fs::path& path) { std::ifstream meta(path); - x2w::MeshStageLayout layout{}; + RawMeshLayout raw; + auto& layout = raw.entry; std::string version, extra; unsigned wave=0; if (!(meta>>version>>layout.threads>>layout.max_vertices>>layout.max_primitives>>layout.topology>> @@ -121,6 +129,16 @@ static x2w::MeshStageLayout read_mesh_layout(const fs::path& path) { layout.validate_uint_heap_reads=true; continue; } + if (extra=="guest_root") { + if (raw.guest_root) + throw std::runtime_error("duplicate guest_root"); + if (!layout.root_inputs.empty() || layout.resource_heap_sgpr) + throw std::runtime_error("guest_root excludes explicit root mapping lines"); + raw.guest_root=true; + continue; + } + if (raw.guest_root && (extra=="table" || extra=="constants" || extra=="root_address" || extra=="resource_heap")) + throw std::runtime_error("guest_root excludes explicit root mapping lines"); if (extra=="resource_heap") { unsigned sgpr; if (layout.resource_heap_sgpr || !(meta>>sgpr)) throw std::runtime_error("invalid mesh resource heap"); @@ -134,18 +152,24 @@ static x2w::MeshStageLayout read_mesh_layout(const fs::path& path) { throw std::runtime_error("invalid mesh entry root mapping"); layout.root_inputs.push_back(input); } - return layout; + return raw; } static x2w::NativeComputeShader translate_raw_mesh(const std::string& source, - const x2w::MeshStageLayout& layout, std::string_view root) { - if (layout.root_inputs.empty()) + RawMeshLayout raw, std::string_view root) { + auto& layout = raw.entry; + if (!raw.needs_root()) return x2w::translate_mesh_program(source, layout); Microsoft::WRL::ComPtr signature; const D3D12_VERSIONED_ROOT_SIGNATURE_DESC *desc=nullptr; if (FAILED(D3D12CreateVersionedRootSignatureDeserializer(root.data(),root.size(),IID_PPV_ARGS(&signature))) || FAILED(signature->GetRootSignatureDescAtVersion(D3D_ROOT_SIGNATURE_VERSION_1_1,&desc))) throw std::runtime_error("invalid raw mesh root signature"); + if (raw.guest_root) { + auto mapping = x2w::derive_mesh_root_mapping(desc->Desc_1_1); + layout.root_inputs = std::move(mapping.inputs); + layout.resource_heap_sgpr = mapping.resource_heap_sgpr; + } return x2w::translate_mesh_program(source,layout,desc->Desc_1_1); } @@ -232,10 +256,10 @@ static std::optional compile_one(x2w::ShaderDxilCompiler& compiler, }; if(profile!=L"gfx1013-raw-ms") source=mesh_ir(x2w::translate_mesh_container(source,profile==L"gfx1013-container-ms-uint-heap")); else { - const auto layout=read_mesh_layout(fs::path(input_path).replace_extension(L".mslayout")); - const auto root=layout.root_inputs.empty() ? std::string{} : - x2w::read_shader_container_bytes(fs::path(input_path).replace_extension(L".root")); - source=mesh_ir(translate_raw_mesh(source,layout,root)); + auto layout=read_mesh_layout(fs::path(input_path).replace_extension(L".mslayout")); + const auto root=layout.needs_root() ? + x2w::read_shader_container_bytes(fs::path(input_path).replace_extension(L".root")) : std::string{}; + source=mesh_ir(translate_raw_mesh(source,std::move(layout),root)); } profile=L"ms_6_6"; } else if (mesh_pixel) {