diff --git a/examples/lanes/README.md b/examples/lanes/README.md index c71fb870..0f966b84 100644 --- a/examples/lanes/README.md +++ b/examples/lanes/README.md @@ -124,9 +124,9 @@ The `raster-samplers` scenario draws with the same vertex program and a third ha The `lane-spill-interp` scenario draws the same fullscreen triangle with a third hand-written pixel program under an empty root. It writes a lane-spill area in v128, then runs an f16 interpolator whose attribute selector is encoded like v128. The selector is not a register read, so every texel must hold the program's constant. Under a refused translation the pipeline becomes a stand-in and every texel keeps its clear value. -The `mesh-samplers` scenario samples the same way through a mesh pipeline. Its pixel program declares `s0` and `s1`, which the title's root has as point-filtered static samplers, so neither register can stand in for the literal state and the program does not translate against that root. Its mesh program is a console container with FSSM records only: they name the root constant `b501` where the root declares `b500`, so the provider builds the mesh stage a root of its own. The provider has to add the literal state to that root as a private static sampler and translate the pixel program against it, so every texel reads 1.5. Without that it falls back to the pixel program's translation without a root, whose private sampler the pipeline's root lacks, and the pipeline becomes a stand-in. The mesh program's desktop translation (`mesh_samplers.hlsl`, a fullscreen triangle) is compiled into the prepared cache under its digest before launch. +The `mesh-samplers` scenario samples the same way through a mesh pipeline. Its pixel program declares `s0` and `s1`, which the title's root has as point-filtered static samplers, so neither register can stand in for the literal state and the program does not translate against that root. Its synthetic console mesh program names the root constant `b501` where the title root declares `b500`, so the provider builds the mesh stage a root of its own. It emits guarded half-to-float positions for a 67-vertex NGG program. The provider has to add the literal state to the reflected root as a private static sampler and translate both stages against it, so every texel reads 1.5. The verification gate matches the loaded mesh and pixel digests against fresh shader-worker receipts, forbids rootless translations and stand-ins, and does not prepare a desktop mesh shader. The separate translated-mesh GPU control tests 3, 67 and 128 vertices, including clipped draws. -These roots are made while the game runs, so the pixel programs cannot be prepared before launch. `verify` runs the three scenarios with the host worker, requires the provider to report the reflected root (for the vertex pipelines) or the mesh pipeline and its descriptor slots, and fails if a pixel or vertex program was translated without a root. +These roots are made while the game runs, so the pixel programs cannot be prepared before launch. `verify` runs the five scenarios with the host worker, requires the provider to report the reflected root (for the vertex pipelines) or the mesh pipeline and its descriptor slots, and fails if a pixel or vertex program was translated without a root. ```sh python tuxbox.py verify --only lanes-raster-tables --output build/lanes-raster-tables.json diff --git a/examples/lanes/raster_fixture.py b/examples/lanes/raster_fixture.py index 172ea095..2047218c 100644 --- a/examples/lanes/raster_fixture.py +++ b/examples/lanes/raster_fixture.py @@ -89,7 +89,28 @@ MESH_USER = ([0, 0, 1, 501, 0x200],) MESH_USED = ([2, 0, 501, 0, 0x2_0004],) MESH_RESOURCES = ([2, 0, 501, 501],) -MESH_CODE = (0xBF810000,) # s_endpgm: the desktop replacement runs instead +# Synthetic RDNA2 NGG, 67 vertices, a guarded FP16-to-FP32 position export. +# Assembled from tools.check_translated_mesh_gpu.program(67, half=True). +MESH_CODE = ( + 0xd7650000, 0x020100c1, 0x7e000505, 0x9381ff03, 0x00040018, 0xd7660000, + 0x020200c1, 0x900a8700, 0xd7460001, 0x04010c01, 0xbe9403ff, 0x00000043, + 0x7e2802ff, 0x80000000, 0x7e3602ff, 0x04210440, 0x7d840280, 0x02283714, + 0x7d8402ff, 0x00000042, 0xd5010015, 0x01a9fef3, 0x40400000, 0x7d8402ff, + 0x00000041, 0xd5010016, 0x01a9fef3, 0x40400000, 0x7e2e02f0, 0x7e3002f2, + 0xbeac047e, 0x7da80214, 0xbf880002, 0x7e0e1515, 0x7e0e1707, 0xbefe042c, + 0xbf068014, 0x8580807e, 0xb5940080, 0x8582807e, 0x88800000, 0x88820202, + 0x88800002, 0xd501000a, 0x00010280, 0x7e00050a, 0x87000081, 0xbf068100, + 0xbe80047e, 0x8582807e, 0xd501000b, 0x00090214, 0x8984c102, 0x7e0c050b, + 0x7da40280, 0xbf88000d, 0x8f078c06, 0x8aea027e, 0x88060607, 0xbefc0306, + 0xbf900009, 0xbf8c0000, 0xbf870006, 0xbf8a0000, 0x7e147081, 0xf80008c0, + 0x00000000, 0xf8000941, 0x0000000a, 0xbf8cff0f, 0xbefe0400, 0xbf8a0000, + 0x7d880214, 0x8780046a, 0xbe862400, 0xbf880012, 0xd548000c, 0x02291514, + 0xd548000d, 0x02292914, 0x361c28ff, 0x000003ff, 0x7d861814, 0xd4c30002, + 0x00021a14, 0xd4c30000, 0x00021c14, 0x8882026a, 0x88800200, 0xd501000f, + 0x0001ff14, 0x80000000, 0xf8000941, 0x0000000f, 0xbf8cff0f, 0xbefe0406, + 0x7d880214, 0x8780046a, 0xbe802400, 0xbf880002, 0xf80008cf, 0x18171607, + 0xbf810000, +) ABSENT = 0xDEADBEEFDEADBEEF ROWS_AT, CODE_AT = 0x240, 0x280 @@ -183,10 +204,14 @@ def mesh_sampler_pixel(): def mesh(): - """The records-only console mesh program (shader model 6.5, stage 13).""" + """Synthetic NGG mesh program with guarded FP16 position (stage 13).""" + info = bytearray(validation(13, MESH_RESOURCES)) + info[31] = 2 + struct.pack_into("> 16, 13) self.assertEqual(mesh[b"PSV0"][28], 13) + self.assertEqual(mesh[b"PSV0"][31], 2) + self.assertEqual(struct.unpack_from(" +#include +#include +using frame_abi::check; +using frame_abi::ComPtr; + +using Stream = frame_abi::MeshPipelineStream; + +static ComPtr buffer(ID3D12Device* d, D3D12_HEAP_TYPE type) { + D3D12_HEAP_PROPERTIES heap{}; + heap.Type = type; + heap.CreationNodeMask = heap.VisibleNodeMask = 1; + D3D12_RESOURCE_DESC desc{}; + desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; + desc.Width = 8 * 8 * 4; + desc.Height = desc.DepthOrArraySize = desc.MipLevels = desc.SampleDesc.Count = 1; + desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; + if (type == D3D12_HEAP_TYPE_DEFAULT) + desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; + ComPtr result; + check(d->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &desc, + type == D3D12_HEAP_TYPE_UPLOAD ? D3D12_RESOURCE_STATE_GENERIC_READ + : D3D12_RESOURCE_STATE_COPY_DEST, + nullptr, IID_PPV_ARGS(&result)), + "create buffer"); + return result; +} + +static std::vector load(const char* path) { + std::ifstream stream(path, std::ios::binary | std::ios::ate); + const auto size = stream.tellg(); + if (size <= 0 || size > 16 * 1024 * 1024) + throw std::runtime_error("invalid shader file"); + std::vector data(static_cast(size)); + stream.seekg(0); + if (!stream.read(data.data(), size)) + throw std::runtime_error("shader read"); + return data; +} + +static void run(const char* name, bool draw, bool clipped, FILE* file) { + const auto mesh = load((std::string(name) + ".cso").c_str()); + const auto pixel = load("pixel.cso"); + ComPtr provider; + check(D3D12CreateDevice(nullptr, D3D_FEATURE_LEVEL_12_0, IID_PPV_ARGS(&provider)), "desktop device"); + D3D12_ROOT_PARAMETER parameter{}; + parameter.ParameterType = D3D12_ROOT_PARAMETER_TYPE_UAV; + D3D12_ROOT_SIGNATURE_DESC desc{1, ¶meter, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_NONE}; + ComPtr blob; + check(D3D12SerializeRootSignature(&desc, D3D_ROOT_SIGNATURE_VERSION_1, &blob, nullptr), "serialize root"); + ComPtr root; + check(provider->CreateRootSignature(0, blob->GetBufferPointer(), blob->GetBufferSize(), IID_PPV_ARGS(&root)), + "create root"); + ComPtr owner; + check(root->GetDevice(IID_PPV_ARGS(&owner)), "get owning device"); + ComPtr factory; + ComPtr adapter; + DXGI_ADAPTER_DESC1 gpu{}; + check(CreateDXGIFactory1(IID_PPV_ARGS(&factory)), "DXGI factory"); + check(factory->EnumAdapterByLuid(owner->GetAdapterLuid(), IID_PPV_ARGS(&adapter)), "DXGI adapter"); + check(adapter->GetDesc1(&gpu), "GPU description"); + std::fprintf(file, "%s_%s_adapter=%04x:%04x\n", name, clipped ? "clipped" : "full", gpu.VendorId, gpu.DeviceId); + Stream stream; + stream.root.payload = root.Get(); + stream.ms.payload = {mesh.data(), mesh.size()}; + stream.ps.payload = {pixel.data(), pixel.size()}; + stream.raster.payload.FillMode = D3D12_FILL_MODE_SOLID; + stream.raster.payload.CullMode = D3D12_CULL_MODE_NONE; + stream.raster.payload.DepthClipEnable = TRUE; + stream.ms.tag = D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_MS; + stream.as.tag = D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_AS; + D3D12_PIPELINE_STATE_STREAM_DESC pipelineDesc{offsetof(Stream, extension), &stream}; + ComPtr pso; + check(owner->CreatePipelineState(&pipelineDesc, IID_PPV_ARGS(&pso)), "desktop mesh pipeline"); + ComPtr queue; + ComPtr allocator; + ComPtr list; + D3D12_COMMAND_QUEUE_DESC q{}; + check(owner->CreateCommandQueue(&q, IID_PPV_ARGS(&queue)), "queue"); + check(owner->CreateCommandAllocator(q.Type, IID_PPV_ARGS(&allocator)), "allocator"); + check(owner->CreateCommandList(0, q.Type, allocator.Get(), pso.Get(), IID_PPV_ARGS(&list)), "list6"); + auto output = buffer(owner.Get(), D3D12_HEAP_TYPE_DEFAULT); + auto readback = buffer(owner.Get(), D3D12_HEAP_TYPE_READBACK); + auto upload = buffer(owner.Get(), D3D12_HEAP_TYPE_UPLOAD); + constexpr UINT seed = 0x13572468; + void* mapped{}; + D3D12_RANGE none{}; + check(upload->Map(0, &none, &mapped), "map seed"); + for (UINT i = 0; i < 64; ++i) static_cast(mapped)[i] = seed; + upload->Unmap(0, nullptr); + list->CopyResource(output.Get(), upload.Get()); + D3D12_RESOURCE_BARRIER before{}; + before.Transition = {output.Get(), 0, D3D12_RESOURCE_STATE_COPY_DEST, + D3D12_RESOURCE_STATE_UNORDERED_ACCESS}; + list->ResourceBarrier(1, &before); + list->SetGraphicsRootSignature(root.Get()); + list->SetGraphicsRootUnorderedAccessView(0, output->GetGPUVirtualAddress()); + D3D12_VIEWPORT viewport{0, 0, 8, 8, 0, 1}; + D3D12_RECT scissor = clipped ? D3D12_RECT{2, 1, 6, 5} : D3D12_RECT{0, 0, 8, 8}; + list->RSSetViewports(1, &viewport); + list->RSSetScissorRects(1, &scissor); + list->DispatchMesh(1, 1, 1); + D3D12_RESOURCE_BARRIER barrier{}; + barrier.Transition = {output.Get(), 0, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, + D3D12_RESOURCE_STATE_COPY_SOURCE}; + list->ResourceBarrier(1, &barrier); + list->CopyResource(readback.Get(), output.Get()); + check(list->Close(), "close"); + ID3D12CommandList* lists[] = {list.Get()}; + queue->ExecuteCommandLists(1, lists); + ComPtr fence; + check(owner->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence)), "fence"); + check(queue->Signal(fence.Get(), 1), "signal"); + HANDLE event = CreateEventW(nullptr, FALSE, FALSE, nullptr); + if (!event) + throw std::runtime_error("event"); + HRESULT hr = fence->SetEventOnCompletion(1, event); + DWORD wait = SUCCEEDED(hr) ? WaitForSingleObject(event, 15000) : WAIT_FAILED; + CloseHandle(event); + check(hr, "signal event"); + if (wait != WAIT_OBJECT_0 || fence->GetCompletedValue() != 1) + throw std::runtime_error("GPU fence"); + void* data{}; + D3D12_RANGE range{0, 256}; + check(readback->Map(0, &range, &data), "readback"); + UINT matched = 0; + for (UINT i = 0; i < 64; ++i) { + const UINT x = i % 8, y = i / 8; + const bool covered = draw && (!clipped || (x >= 2 && x < 6 && y >= 1 && y < 5)); + const UINT expected = covered ? 0x2468ace0 : seed; + matched += static_cast(data)[i] == expected; + } + readback->Unmap(0, &none); + std::fprintf(file, "%s_%s=%u\n", name, clipped ? "clipped" : "full", matched); + std::fflush(file); + if (matched != 64) + throw std::runtime_error("mesh/pixel readback mismatch"); +} + +int main() { + FILE* file{}; + if (fopen_s(&file, "result.txt", "wb")) return 2; + int exit = 0; + try { + for (const char* name : {"plain-3", "swizzle-3", "plain-67", "swizzle-67", + "plain-128", "swizzle-128", "plain-0", "swizzle-0", + "plain-129", "swizzle-129", "bad-index", "half-3", "half-67", "half-128"}) { + const std::string id(name); + const bool draw = id != "bad-index" && id.find("-0") == std::string::npos + && id.find("-129") == std::string::npos; + run(name, draw, false, file); + run(name, draw, true, file); + } + } catch (const std::exception& e) { + std::fprintf(file, "error=%s\n", e.what()); + exit = 1; + } + return fclose(file) ? 2 : exit; +} diff --git a/tools/reference/mesh/translated_shared_count.s b/tools/reference/mesh/translated_shared_count.s new file mode 100644 index 00000000..0435d29d --- /dev/null +++ b/tools/reference/mesh/translated_shared_count.s @@ -0,0 +1,73 @@ +// Synthetic gfx1030 NGG allocation with shared output count. +// Branch immediates are in dwords; only lane 0 emits a live triangle. +v_mbcnt_lo_u32_b32 v0, -1, 0 +v_readfirstlane_b32 s0, v5 +s_bfe_u32 s1, s3, 0x00040018 +v_mbcnt_hi_u32_b32 v0, -1, v0 +s_lshr_b32 s10, s0, 7 +v_lshl_add_u32 v1, s1, 6, v0 +s_mov_b32 s20, 3 +v_mov_b32 v20, 0x80000000 +v_mov_b32 v27, 0x00200400 +v_cmp_eq_u32 vcc, 0, v1 +v_cndmask_b32 v20, v20, v27, vcc +v_cmp_eq_u32 vcc, 2, v1 +v_cndmask_b32 v21, -1.0, 3.0, vcc +v_cmp_eq_u32 vcc, 1, v1 +v_cndmask_b32 v22, -1.0, 3.0, vcc +v_mov_b32 v23, 0.5 +v_mov_b32 v24, 1.0 +s_cmp_eq_u32 s20, 0 +s_cselect_b64 s[0:1], exec, 0 +s_cmpk_gt_u32 s20, 128 +s_cselect_b64 s[2:3], exec, 0 +s_or_b64 s[0:1], s[0:1], s[0:1] +s_or_b64 s[2:3], s[2:3], s[2:3] +s_or_b64 s[0:1], s[2:3], s[0:1] +v_cndmask_b32_e64 v10, 0, 1, s[0:1] +v_readfirstlane_b32 s0, v10 +s_and_b32 s0, 1, s0 +s_cmp_eq_u32 s0, 1 +s_mov_b64 s[0:1], exec +s_cselect_b64 s[2:3], exec, 0 +v_cndmask_b32_e64 v11, s20, 1, s[2:3] +s_xor_b64 s[4:5], s[2:3], -1 +v_readfirstlane_b32 s6, v11 +v_cmpx_eq_u32 0, v1 +s_cbranch_execz 13 +s_lshl_b32 s7, s6, 12 +s_andn2_b64 vcc, exec, s[2:3] +s_or_b32 s6, s7, s6 +s_mov_b32 m0, s6 +s_sendmsg sendmsg(MSG_GS_ALLOC_REQ) +s_waitcnt 0 +s_cbranch_vccnz 6 +s_barrier +v_bfrev_b32 v10, 1 +exp pos0 off, off, off, off done +exp prim v10, off, off, off done +s_waitcnt 0xff0f +s_mov_b64 exec, s[0:1] +s_barrier +v_cmp_gt_u32 vcc, s20, v1 +s_and_b64 s[0:1], vcc, s[4:5] +s_and_saveexec_b64 s[6:7], s[0:1] +s_cbranch_execz 18 +v_bfe_u32 v12, v20, 10, 10 +v_bfe_u32 v13, v20, 20, 10 +v_and_b32 v14, 0x3ff, v20 +v_cmp_le_u32 vcc, s20, v12 +v_cmp_le_u32_e64 s[2:3], s20, v13 +v_cmp_le_u32_e64 s[0:1], s20, v14 +s_or_b64 s[2:3], vcc, s[2:3] +s_or_b64 s[0:1], s[0:1], s[2:3] +v_cndmask_b32_e64 v15, v20, 0x80000000, s[0:1] +exp prim v15, off, off, off done +s_waitcnt 0xff0f +s_mov_b64 exec, s[6:7] +v_cmp_gt_u32 vcc, s20, v1 +s_and_b64 s[0:1], vcc, s[4:5] +s_and_saveexec_b64 s[0:1], s[0:1] +s_cbranch_execz 2 +exp pos0 v21,v22,v23,v24 done +s_endpgm