diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 50dd51614b..0da5fb01eb 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2799,7 +2799,10 @@ Pipeline creation: appended to `pipeline_order.log` beside `pipeline_cache.session.bin` (so `LEONIDA_PIPELINE_CACHE_DIR` moves it): `P *`, with the times in milliseconds since the process started. The key is the SHA-256 of the title's own description of the PSO - (`GetCachedBlob`'s key; a mesh pipeline's MS is folded in), not of the SPIR-V, so a translator change keeps it. A + (`GetCachedBlob`'s key; a mesh pipeline's MS is folded in, and so are a stream's depth-bounds flag and back-face + stencil masks unlike the front's, which the plain description does not hold: RT-STATE-PIPELINE-KEY-DS2; a log + written before that names such a pipeline by the plain key, which then finds nothing, while every other key is + unchanged), not of the SPIR-V, so a translator change keeps it. A build that ends after its line was written adds `B *`; a session starts with `S 1 `. A compute pipeline's `P` line is followed by `E *`: the name hash is 16 hex digits of FNV-1a 64 over the name of diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 882026e0ca..916ad66ee8 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -3070,6 +3070,14 @@ if(TARGET Vulkan::Headers) add_test(NAME d3d12_descriptor COMMAND d3d12_descriptor_test) set_tests_properties(d3d12_descriptor PROPERTIES SKIP_RETURN_CODE 77) + # The cache and order keys of a pipeline state stream: depth bounds and back-face stencil masks are in them + # (RT-STATE-PIPELINE-KEY-DS2). Parses only, no device. + add_executable(d3d12_pipeline_stream_key_test test/d3d12_pipeline_stream_key_test.cpp) + target_link_libraries(d3d12_pipeline_stream_key_test PRIVATE gta6rt_d3d12_com) + target_compile_definitions(d3d12_pipeline_stream_key_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) + target_compile_options(d3d12_pipeline_stream_key_test PRIVATE /W4 /permissive- /utf-8) + add_test(NAME d3d12_pipeline_stream_key COMMAND d3d12_pipeline_stream_key_test) + # Console descriptor heap T# binding rules and per-reason refusal tracking (RT-UNBOUND-TSHARP-REASONS). add_executable(d3d12_unbound_tsharp_rules_test test/d3d12_unbound_tsharp_rules_test.cpp) target_link_libraries(d3d12_unbound_tsharp_rules_test PRIVATE gta6rt_d3d12_com) diff --git a/runtime/shims/d3d12_com.h b/runtime/shims/d3d12_com.h index 3bad0b7671..669b16d319 100644 --- a/runtime/shims/d3d12_com.h +++ b/runtime/shims/d3d12_com.h @@ -441,6 +441,11 @@ struct UnboundTsharpStats { std::uint64_t total_refusals = 0; }; +// For tests: the cache key (GetCachedBlob) and the pipeline_order.log key that ID3D12Device2::CreatePipelineState +// gives a stream's pipeline, without creating it. Fails as parsing the stream does. +HRESULT pipeline_stream_keys_for_test(const D3D12_PIPELINE_STATE_STREAM_DESC& desc, std::string& cache_key, + std::string& order_key); + // Returns statistics on unbound T# descriptor refusals across console descriptor heaps. UnboundTsharpStats unbound_tsharp_stats(); // Resets unbound T# refusal statistics (for testing). diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 19632c0ba2..0835022708 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -10905,11 +10905,29 @@ struct PipelineStream { bool depth_bounds = false; // DEPTH_STENCIL1/2 DepthBoundsTestEnable (not in the plain graphics desc) }; +// A stream's graphics key: pipeline_key of its graphics description, with what the stream adds beyond that +// description folded in, since create_graphics builds from it too: DEPTH_STENCIL1/2's depth-bounds flag, and +// DEPTH_STENCIL2's back-face stencil masks where they differ from the front face's (without them the back face takes +// the front's, so equal ones build the same pipeline). Two streams that differ only there would otherwise share the +// key (kevincommits, RT-STATE-AUDIT-PIPELINE item 5). A stream without either keeps the key it had before, the one +// CreateGraphicsPipelineState gives the same description. +leonida::cached_pso::Key stream_graphics_key(const PipelineStream& stream) { + auto key = pipeline_key(stream.graphics); + const D3D12_DEPTH_STENCIL_DESC& d = stream.graphics.DepthStencilState; + const bool back = stream.back_masks && (stream.back_stencil.read != d.StencilReadMask || + stream.back_stencil.write != d.StencilWriteMask); + if (!stream.depth_bounds && !back) return key; + leonida::cached_pso::KeyBuilder extra; + extra.tag("graphics stream").text(key.c_str()).u32(stream.depth_bounds).u32(back); + if (back) extra.u32(stream.back_stencil.read).u32(stream.back_stencil.write); + return leonida::sha256_hex(extra.data()); +} + // What pipeline_order.log names a stream's pipeline by: the cache key, with the mesh shader's hash folded in (the // graphics key does not cover the MS, so two mesh pipelines that differ only there would share it). leonida::cached_pso::Key order_key(const PipelineStream& stream) { if (stream.has_cs) return pipeline_key(stream.compute); - auto key = pipeline_key(stream.graphics); + auto key = stream_graphics_key(stream); if (stream.ms.BytecodeLength) { key += leonida::sha256_hex({static_cast(stream.ms.pShaderBytecode), stream.ms.BytecodeLength}); key = leonida::sha256_hex({reinterpret_cast(key.data()), key.size()}); @@ -15697,7 +15715,7 @@ class Device final if(!record)return E_INVALIDARG; auto* pipeline=new PipelineState(state_); pipeline->attach_device(identity()); - pipeline->set_cache_key(stream.has_cs?pipeline_key(stream.compute):pipeline_key(stream.graphics)); + pipeline->set_cache_key(stream.has_cs?pipeline_key(stream.compute):stream_graphics_key(stream)); if(state_.order_log||state_.prewarm_plan) pipeline->set_order_key(order_key(stream)); if(!rt::d3d12_xs::attach_pso_record(pipeline,std::move(record))){pipeline->Release();return E_OUTOFMEMORY;} pipeline->set_pending_stream(stream.has_cs); @@ -17937,6 +17955,9 @@ class Device final if (FAILED(parsed)) return finish(parsed); auto* pipeline = new PipelineState(state_); pipeline->attach_device(identity()); + // The same cache key the description paths and a console stream (prepare_console_stream) give: GetCachedBlob + // and a later CachedPSO check name this stream's state, its depth bounds and back-face stencil masks with it. + pipeline->set_cache_key(stream.has_cs ? pipeline_key(stream.compute) : stream_graphics_key(stream)); if (state_.order_log || state_.prewarm_plan) pipeline->set_order_key(order_key(stream)); const HRESULT hr = stream.has_cs ? pipeline->create_compute(stream.compute) : pipeline->create_graphics(stream.graphics, &stream.ms, &stream.as, @@ -23350,6 +23371,16 @@ HRESULT create_console_descriptor_heap(ID3D12Device* device, const D3D12_DESCRIP return d->create_console_descriptor_heap(desc, console_address, console_size, riid, out); } +HRESULT pipeline_stream_keys_for_test(const D3D12_PIPELINE_STATE_STREAM_DESC& desc, std::string& cache_key, + std::string& order) { + PipelineStream stream; + const HRESULT hr = parse_pipeline_stream(desc, stream); + if (FAILED(hr)) return hr; + cache_key = stream.has_cs ? pipeline_key(stream.compute) : stream_graphics_key(stream); + order = order_key(stream); + return S_OK; +} + UnboundTsharpStats unbound_tsharp_stats() { return unbound_tsharp_tracker().stats(); } diff --git a/runtime/test/d3d12_compute_test.cpp b/runtime/test/d3d12_compute_test.cpp index 05591a780a..1d2506a202 100644 --- a/runtime/test/d3d12_compute_test.cpp +++ b/runtime/test/d3d12_compute_test.cpp @@ -439,6 +439,45 @@ int main() { EXPECT(attempt(resealed(blob_bytes, 4)) == E_INVALIDARG); // version } + // ID3D12Device2::CreatePipelineState gives a stream's PSO the same cache key as the description path: the same + // root signature and CS make the same GetCachedBlob bytes, and the blob is accepted back (#2477's review: the + // public stream path left the key empty). + { + struct alignas(void*) RootSubobject { + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE type; + ID3D12RootSignature* value; + }; + struct alignas(void*) ShaderSubobject { + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE type; + D3D12_SHADER_BYTECODE value; + }; + struct { + RootSubobject root; + ShaderSubobject cs; + } stream{{D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_ROOT_SIGNATURE, pso_desc.pRootSignature}, + {D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_CS, pso_desc.CS}}; + const D3D12_PIPELINE_STATE_STREAM_DESC stream_desc{sizeof(stream), &stream}; + Ref device2; + EXPECT(SUCCEEDED(inner->QueryInterface(IID_PPV_ARGS(&device2.p))) && device2.p); + Ref from_desc, from_stream; + EXPECT(SUCCEEDED(device->CreateComputePipelineState(&pso_desc, IID_PPV_ARGS(&from_desc.p)))); + if (device2.p) EXPECT(SUCCEEDED(device2->CreatePipelineState(&stream_desc, IID_PPV_ARGS(&from_stream.p)))); + Ref desc_blob, stream_blob; + if (from_desc.p && from_stream.p) { + EXPECT(from_desc->GetCachedBlob(&desc_blob.p) == S_OK && desc_blob.p); + EXPECT(from_stream->GetCachedBlob(&stream_blob.p) == S_OK && stream_blob.p); + } + if (desc_blob.p && stream_blob.p) { + EXPECT(desc_blob->GetBufferSize() == stream_blob->GetBufferSize() && + std::memcmp(desc_blob->GetBufferPointer(), stream_blob->GetBufferPointer(), + desc_blob->GetBufferSize()) == 0); + D3D12_COMPUTE_PIPELINE_STATE_DESC d = pso_desc; + d.CachedPSO = {stream_blob->GetBufferPointer(), stream_blob->GetBufferSize()}; + Ref made; + EXPECT(device->CreateComputePipelineState(&d, IID_PPV_ARGS(&made.p)) == S_OK); + } + } + // A device whose maxPushConstantsSize is the Vulkan minimum (128 bytes) cannot hold the 144-byte block, so // its PSOs are refused instead of creating a pipeline layout the spec forbids (#224's fallback flag). { diff --git a/runtime/test/d3d12_pipeline_stream_key_test.cpp b/runtime/test/d3d12_pipeline_stream_key_test.cpp new file mode 100644 index 0000000000..d0c2c1e35d --- /dev/null +++ b/runtime/test/d3d12_pipeline_stream_key_test.cpp @@ -0,0 +1,136 @@ +// The keys ID3D12Device2::CreatePipelineState gives a stream's pipeline (shims/d3d12_vulkan_device.cpp): the cache key +// (GetCachedBlob) and the pipeline_order.log key. A stream carries state the plain graphics description does not: +// DEPTH_STENCIL1/2's depth-bounds flag and DEPTH_STENCIL2's back-face stencil masks. Two streams that differ only there +// build different pipelines, so they must not share a key (RT-STATE-PIPELINE-KEY-DS2, item 5 of kevincommits' +// RT-STATE-AUDIT-PIPELINE finding). A stream without that state keeps the key it had before. Parses only: no device, +// no GPU, no game data. +#include "../shims/d3d12_com.h" + +#include +#include +#include +#include +#include +#include + +namespace { + +int failures = 0; +#define EXPECT(cond) \ + do { \ + if (!(cond)) { \ + std::fprintf(stderr, "%s:%d: EXPECT(%s) failed\n", __FILE__, __LINE__, #cond); \ + ++failures; \ + } \ + } while (0) + +// A pipeline state stream, laid out as the D3D12 helpers do: each subobject's type, its payload at the payload's +// alignment, the next subobject at pointer alignment. +class Stream { +public: + template + Stream& add(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE type, const T& value) { + place(&type, sizeof(type), alignof(void*)); + place(&value, sizeof(T), alignof(T)); + bytes_.resize(round_up(bytes_.size(), alignof(void*))); + return *this; + } + D3D12_PIPELINE_STATE_STREAM_DESC desc() { return {bytes_.size(), bytes_.data()}; } + +private: + static std::size_t round_up(std::size_t v, std::size_t a) { return (v + a - 1) / a * a; } + void place(const void* p, std::size_t n, std::size_t align) { + const std::size_t at = round_up(bytes_.size(), align); + bytes_.resize(at + n); + std::memcpy(bytes_.data() + at, p, n); + } + std::vector bytes_; // new[]'s alignment covers every payload's +}; + +const std::uint8_t kVs[16] = {'n', 'o', 't', ' ', 'a', ' ', 'r', 'e', 'a', 'l', ' ', 'v', 's', 0, 1, 2}; + +struct Keys { + std::string cache, order; + bool operator==(const Keys& o) const { return cache == o.cache && order == o.order; } +}; + +// The cache and order keys of a stream holding a VS, one render target, a depth-stencil format and the given +// depth-stencil subobject. +template +Keys keys(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE type, const T& depth_stencil) { + Stream s; + s.add(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_VS, D3D12_SHADER_BYTECODE{kVs, sizeof(kVs)}); + D3D12_RT_FORMAT_ARRAY formats{}; + formats.NumRenderTargets = 1; + formats.RTFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM; + s.add(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RENDER_TARGET_FORMATS, formats); + s.add(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL_FORMAT, DXGI_FORMAT_D32_FLOAT_S8X24_UINT); + s.add(type, depth_stencil); + Keys k; + const HRESULT hr = rt::d3d12::pipeline_stream_keys_for_test(s.desc(), k.cache, k.order); + EXPECT(SUCCEEDED(hr)); + EXPECT(!k.cache.empty() && !k.order.empty()); + return k; +} + +D3D12_DEPTH_STENCILOP_DESC face_op() { + return {D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_INCR, D3D12_STENCIL_OP_REPLACE, D3D12_COMPARISON_FUNC_EQUAL}; +} +D3D12_DEPTH_STENCILOP_DESC1 face_op1(UINT8 read, UINT8 write) { + const D3D12_DEPTH_STENCILOP_DESC f = face_op(); + return {f.StencilFailOp, f.StencilDepthFailOp, f.StencilPassOp, f.StencilFunc, read, write}; +} +D3D12_DEPTH_STENCIL_DESC plain() { + return {TRUE, D3D12_DEPTH_WRITE_MASK_ALL, D3D12_COMPARISON_FUNC_LESS, TRUE, 0x0f, 0xf0, face_op(), face_op()}; +} +D3D12_DEPTH_STENCIL_DESC1 with_bounds(BOOL bounds) { + const D3D12_DEPTH_STENCIL_DESC d = plain(); + return {d.DepthEnable, d.DepthWriteMask, d.DepthFunc, d.StencilEnable, d.StencilReadMask, d.StencilWriteMask, + d.FrontFace, d.BackFace, bounds}; +} +D3D12_DEPTH_STENCIL_DESC2 with_back(UINT8 back_read, UINT8 back_write, BOOL bounds = FALSE) { + const D3D12_DEPTH_STENCIL_DESC d = plain(); + return {d.DepthEnable, d.DepthWriteMask, d.DepthFunc, d.StencilEnable, face_op1(d.StencilReadMask, d.StencilWriteMask), + face_op1(back_read, back_write), bounds}; +} + +} // namespace + +int main() { + const Keys base = keys(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL, plain()); + + // Depth bounds: DEPTH_STENCIL1 with the test off is the plain description, key included; on, another pipeline. + const Keys bounds_off = keys(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL1, with_bounds(FALSE)); + const Keys bounds_on = keys(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL1, with_bounds(TRUE)); + EXPECT(bounds_off == base); + EXPECT(bounds_on.cache != bounds_off.cache); + EXPECT(bounds_on.order != bounds_off.order); + std::printf("depth bounds: on and off get different cache and order keys; off keeps the plain key\n"); + + // Back-face stencil masks: DEPTH_STENCIL2 with the back face's masks equal to the front's builds the plain + // pipeline (without DEPTH_STENCIL2 the back face takes the front's), so it keeps the plain key; other masks, and + // two different sets of them, get keys of their own. + const Keys back_same = keys(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL2, with_back(0x0f, 0xf0)); + const Keys back_read = keys(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL2, with_back(0x3c, 0xf0)); + const Keys back_write = keys(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL2, with_back(0x0f, 0x01)); + EXPECT(back_same == base); + EXPECT(back_read.cache != base.cache && back_read.order != base.order); + EXPECT(back_write.cache != base.cache && back_write.order != base.order); + EXPECT(back_read.cache != back_write.cache && back_read.order != back_write.order); + std::printf("back-face stencil masks: masks unlike the front's get keys of their own; equal ones keep the plain key\n"); + + // Both at once differ from either alone. + const Keys both = keys(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL2, with_back(0x3c, 0xf0, TRUE)); + const Keys bounds_only2 = keys(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL2, with_back(0x0f, 0xf0, TRUE)); + EXPECT(bounds_only2 == bounds_on); + EXPECT(both.cache != bounds_on.cache && both.cache != back_read.cache); + EXPECT(both.order != bounds_on.order && both.order != back_read.order); + std::printf("depth bounds with back-face masks: a key unlike either alone\n"); + + if (failures) { + std::fprintf(stderr, "d3d12_pipeline_stream_key_test: %d failure(s)\n", failures); + return 1; + } + std::printf("d3d12_pipeline_stream_key_test: all passed\n"); + return 0; +}