Skip to content
Open
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
16 changes: 16 additions & 0 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -2823,6 +2823,22 @@ Pipeline creation:
`LEONIDA_TRACE_PIPELINE_COMPILE=1` records each graphics and compute build's trigger, thread, elapsed time,
SPIR-V size, shader names and Vulkan application-cache feedback (1 hit, 0 miss, -1 unavailable). It also
times cache reads, merges, writes and draw/dispatch pipeline lookups that take at least 1 ms.
The `pipeline-compile-begin`, `pipeline-compile` and (for a draw) `pipeline-use` lines carry `id=` and the
`variant=` it was built or asked for. `id=` is the identity of a graphics pipeline's description, the same in every
run of the same shaders: a SHA-256 of the decoded state blocks (blend, sample mask, rasterizer, depth and stencil,
stream output), the checksum and size of each stage's DXBC container (VS, PS, DS, HS, GS, and the MS of a mesh
pipeline; an AS is hashed too, but a pipeline with one is refused before it gets an identity), the render target and
depth formats, the sample count, the input layout, strip cut and topology type (not for a mesh pipeline, whose D3D12
reading ignores them), and for a stream the back face's stencil masks and the depth bounds test. Those two are only
passed on the `ID3D12Device2::CreatePipelineState` path: a console stream prepared at bind time (`resolve_type`)
builds without them, so the same stream of the title gets another `id` there. The identity is that of what is built
(`graphics_identity`, `runtime/shims/d3d12_graphics_identity.h`). The root signature, an address and the device are
not in it. `PipelineState::create_graphics` computes it, so every graphics creation path sets it
(`CreateGraphicsPipelineState`, slot 57, and `ID3D12Device2::CreatePipelineState`, mesh pipelines included). `id=-`
is a compute pipeline. `key=` is the other name, the description the PC runtime matches a `CachedPSO` against: it
holds the root signature (its content, or its address for an object that is not ours), so it can differ between
runs, and the stream path leaves it `-`. `id=` is for telling the same pipeline apart from another across runs; it
does not change what is built or the driver's cache key.
`LEONIDA_ASYNC_PIPELINES=1` schedules cold first-use builds on the warm-up worker ahead of speculative work; how long
a submission waits for them, and what is skipped, is "Pipelines still being built" below. Strict replay builds
synchronously and waits for every build. `LEONIDA_COMPUTE_WARMUP=0` restricts the worker to pipelines requested by
Expand Down
7 changes: 7 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,13 @@ if(BUILD_TESTING)
target_compile_options(pso_stream_test PRIVATE /W4 /permissive- /utf-8)
target_link_options(pso_stream_test PRIVATE /OPT:NOREF)
add_test(NAME pso_stream COMMAND pso_stream_test)
# The identity of a graphics description (d3d12_graphics_identity.h): header only, synthetic descriptions.
add_executable(graphics_identity_test test/graphics_identity_test.cpp)
target_include_directories(graphics_identity_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/shims)
target_compile_definitions(graphics_identity_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
target_compile_options(graphics_identity_test PRIVATE /W4 /permissive- /utf-8)
target_link_options(graphics_identity_test PRIVATE /OPT:NOREF)
add_test(NAME graphics_identity COMMAND graphics_identity_test)
endif()

if(TARGET root_sig_test)
Expand Down
122 changes: 122 additions & 0 deletions runtime/shims/d3d12_graphics_identity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// The identity of a graphics pipeline description: a hash of what defines the pipeline and nothing of the run.
//
// pipeline_key (ID3D12PipelineState::GetCachedBlob, the `key=` of the pipeline-compile log line) is what the PC
// runtime matches a CachedPSO against. It names the root signature (its content, or its address for an object that is
// not ours, which can differ between runs), it is not set on the stream creation path, and it has no MS or AS.
// The identity below is for finding the same pipeline again in another run, so it takes none of those: the decoded
// state blocks, the checksum of the shader of every stage, the formats and the topology the description declares.
// The root signature, an address and a pointer are not in it. Nothing here reads a device or changes what is built.
#pragma once

#include "../src/cached_pso.h"
#include "../src/shader_library.h"

#include <d3d12.h>

#include <algorithm>
#include <bit>
#include <climits>
#include <cstdint>
#include <cstring>
#include <string>

namespace rt::d3d12 {

// The back face's stencil masks of a DEPTH_STENCIL2 subobject. D3D12_DEPTH_STENCIL_DESC has one read and one write
// mask for both faces, so the face masks travel beside the description.
struct StencilFaceMasks {
UINT8 read = D3D12_DEFAULT_STENCIL_READ_MASK;
UINT8 write = D3D12_DEFAULT_STENCIL_WRITE_MASK;
};

// A shader as the key sees it, without reading the whole program on every PSO: a DXBC container already carries a
// checksum of its contents (bytes 4..19), so that and the size stand for it. A container without one (all zero, as
// hand-built test containers have) or other bytecode is hashed with FNV-1a.
inline void key_shader(leonida::cached_pso::KeyBuilder& key, const char* stage, const D3D12_SHADER_BYTECODE& code) {
const auto* bytes = static_cast<const std::uint8_t*>(code.pShaderBytecode);
const std::size_t size = bytes ? code.BytecodeLength : 0;
key.tag(stage).u32(std::uint32_t(size)).u32(std::uint32_t(std::uint64_t(size) >> 32));
if (size >= 20 && std::memcmp(bytes, "DXBC", 4) == 0 &&
std::any_of(bytes + 4, bytes + 20, [](std::uint8_t b) { return b != 0; })) {
key.bytes(bytes + 4, 16);
return;
}
std::uint64_t h = 0xcbf29ce484222325ull;
for (std::size_t i = 0; i < size; ++i) h = (h ^ bytes[i]) * 0x100000001b3ull;
key.u32(std::uint32_t(h)).u32(std::uint32_t(h >> 32));
}

// Everything of a graphics description but its root signature, in the order pipeline_key has always hashed it.
// `input_assembly` false leaves out the input layout, the strip cut and the topology type: D3D12 ignores them for a
// mesh pipeline.
inline void key_graphics_state(leonida::cached_pso::KeyBuilder& key, const D3D12_GRAPHICS_PIPELINE_STATE_DESC& desc,
bool input_assembly) {
key_shader(key, "vs", desc.VS);
key_shader(key, "ps", desc.PS);
key_shader(key, "ds", desc.DS);
key_shader(key, "hs", desc.HS);
key_shader(key, "gs", desc.GS);
const D3D12_STREAM_OUTPUT_DESC& so = desc.StreamOutput;
key.u32(so.pSODeclaration ? so.NumEntries : 0);
for (UINT i = 0; so.pSODeclaration && i < so.NumEntries; ++i) {
const D3D12_SO_DECLARATION_ENTRY& e = so.pSODeclaration[i];
key.u32(e.Stream).text(e.SemanticName).u32(e.SemanticIndex).u32(e.StartComponent).u32(e.ComponentCount);
key.u32(e.OutputSlot);
}
key.u32(so.pBufferStrides ? so.NumStrides : 0);
for (UINT i = 0; so.pBufferStrides && i < so.NumStrides; ++i) key.u32(so.pBufferStrides[i]);
key.u32(so.RasterizedStream);
const D3D12_BLEND_DESC& blend = desc.BlendState;
key.u32(blend.AlphaToCoverageEnable).u32(blend.IndependentBlendEnable);
for (const D3D12_RENDER_TARGET_BLEND_DESC& t : blend.RenderTarget)
key.u32(t.BlendEnable).u32(t.LogicOpEnable).u32(t.SrcBlend).u32(t.DestBlend).u32(t.BlendOp).u32(t.SrcBlendAlpha)
.u32(t.DestBlendAlpha).u32(t.BlendOpAlpha).u32(t.LogicOp).u32(t.RenderTargetWriteMask);
key.u32(desc.SampleMask);
const D3D12_RASTERIZER_DESC& r = desc.RasterizerState;
std::uint32_t bias_clamp = 0, slope = 0;
std::memcpy(&bias_clamp, &r.DepthBiasClamp, 4);
std::memcpy(&slope, &r.SlopeScaledDepthBias, 4);
key.u32(r.FillMode).u32(r.CullMode).u32(r.FrontCounterClockwise).u32(std::uint32_t(r.DepthBias)).u32(bias_clamp);
key.u32(slope).u32(r.DepthClipEnable).u32(r.MultisampleEnable).u32(r.AntialiasedLineEnable);
key.u32(r.ForcedSampleCount).u32(r.ConservativeRaster);
const D3D12_DEPTH_STENCIL_DESC& d = desc.DepthStencilState;
key.u32(d.DepthEnable).u32(d.DepthWriteMask).u32(d.DepthFunc).u32(d.StencilEnable).u32(d.StencilReadMask);
key.u32(d.StencilWriteMask);
for (const D3D12_DEPTH_STENCILOP_DESC& face : {d.FrontFace, d.BackFace})
key.u32(face.StencilFailOp).u32(face.StencilDepthFailOp).u32(face.StencilPassOp).u32(face.StencilFunc);
if (input_assembly) {
const D3D12_INPUT_LAYOUT_DESC& input = desc.InputLayout;
const UINT input_count = input.pInputElementDescs
? std::min<UINT>(input.NumElements, D3D12_IA_VERTEX_INPUT_STRUCTURE_ELEMENT_COUNT)
: 0;
key.u32(input_count);
for (UINT i = 0; i < input_count; ++i) {
const D3D12_INPUT_ELEMENT_DESC& e = input.pInputElementDescs[i];
key.text(e.SemanticName).u32(e.SemanticIndex).u32(e.Format).u32(e.InputSlot).u32(e.AlignedByteOffset);
key.u32(e.InputSlotClass).u32(e.InstanceDataStepRate);
}
key.u32(desc.IBStripCutValue).u32(std::bit_cast<std::uint32_t>(desc.PrimitiveTopologyType));
}
key.u32(desc.NumRenderTargets);
for (const DXGI_FORMAT format : desc.RTVFormats) key.u32(format);
key.u32(desc.DSVFormat).u32(desc.SampleDesc.Count).u32(desc.SampleDesc.Quality).u32(desc.NodeMask);
}

// The identity of what create_graphics is given: the description, an MS or AS stage, the back face's stencil masks
// and the depth bounds test of a pipeline state stream. 64 lowercase hex digits (SHA-256).
inline std::string graphics_identity(const D3D12_GRAPHICS_PIPELINE_STATE_DESC& desc,
const D3D12_SHADER_BYTECODE* ms = nullptr,
const D3D12_SHADER_BYTECODE* as = nullptr,
const StencilFaceMasks* back_masks = nullptr, bool depth_bounds = false) {
const bool mesh = ms && ms->pShaderBytecode && ms->BytecodeLength;
leonida::cached_pso::KeyBuilder key;
key.tag("graphics identity 1");
key_graphics_state(key, desc, !mesh);
key_shader(key, "ms", ms ? *ms : D3D12_SHADER_BYTECODE{});
key_shader(key, "as", as ? *as : D3D12_SHADER_BYTECODE{});
key.u32(back_masks ? 1u : 0u).u32(back_masks ? back_masks->read : 0u).u32(back_masks ? back_masks->write : 0u);
key.u32(depth_bounds ? 1u : 0u);
return leonida::sha256_hex(key.data());
}

} // namespace rt::d3d12
24 changes: 16 additions & 8 deletions runtime/shims/d3d12_pipeline_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,18 @@ inline long long compile_wall_ms() {
std::chrono::system_clock::now().time_since_epoch()).count();
}

template<class Build> VkPipeline replay_pipeline(const char* trigger, Build&& get) {
// `description_id` is the graphics_identity of the pipeline being used ("-" for none, as for a compute pipeline) and
// `variant` the variant asked for.
template<class Build> VkPipeline replay_pipeline(const char* trigger, Build&& get, const std::string& description_id = {},
std::uint32_t variant = 0) {
if (!trace_pipeline_compile()) return get();
const auto start = compile_us();
auto handle = get();
auto elapsed = compile_us() - start;
if (elapsed >= 1000)
std::fprintf(stderr, "[rt] pipeline-use trigger=%s tid=%lu us=%lld ready=%d end_ms=%lld\n",
trigger, GetCurrentThreadId(), elapsed, int(handle != VK_NULL_HANDLE), compile_wall_ms());
std::fprintf(stderr, "[rt] pipeline-use trigger=%s tid=%lu us=%lld ready=%d end_ms=%lld id=%s variant=%u\n",
trigger, GetCurrentThreadId(), elapsed, int(handle != VK_NULL_HANDLE), compile_wall_ms(),
description_id.empty() ? "-" : description_id.c_str(), variant);
return handle;
}

Expand All @@ -130,15 +134,18 @@ struct CompileTrace {
nullptr, &feedback, 0, nullptr};
long long started = 0;
const char* key = "-";
const char* id = "-"; // graphics_identity of a graphics pipeline, "-" for a compute pipeline
bool linked = false; // chain is in a create info's pNext chain

template<class Info> void begin(Info& info, bool supported, const char* trigger, const char* shaders,
std::uint32_t variant, std::size_t bytes, const std::string& description_key) {
std::uint32_t variant, std::size_t bytes, const std::string& description_key,
const std::string& description_id = {}) {
if (!trace_pipeline_compile()) return;
if (!description_key.empty()) key = description_key.c_str();
if (!description_id.empty()) id = description_id.c_str();
if (supported) request_feedback(info);
std::fprintf(stderr, "[rt] pipeline-compile-begin trigger=%s tid=%lu bytes=%zu variant=%u begin_ms=%lld key=%s shaders=%s\n",
trigger, GetCurrentThreadId(), bytes, variant, compile_wall_ms(), key, shaders);
std::fprintf(stderr, "[rt] pipeline-compile-begin trigger=%s tid=%lu bytes=%zu variant=%u begin_ms=%lld key=%s id=%s shaders=%s\n",
trigger, GetCurrentThreadId(), bytes, variant, compile_wall_ms(), key, id, shaders);
started = compile_us();
}

Expand All @@ -163,8 +170,9 @@ struct CompileTrace {
const auto ended = compile_us();
const int hit = this->hit();
std::fprintf(stderr, "[rt] pipeline-compile trigger=%s tid=%lu start_us=%lld us=%lld hit=%d bytes=%zu "
"variant=%u result=%d end_ms=%lld key=%s shaders=%s\n", trigger, GetCurrentThreadId(), started,
ended - started, hit, bytes, variant, int(result), compile_wall_ms(), key, shaders);
"variant=%u result=%d end_ms=%lld key=%s id=%s shaders=%s\n", trigger, GetCurrentThreadId(),
started, ended - started, hit, bytes, variant, int(result), compile_wall_ms(), key, id,
shaders);
}
};

Expand Down
Loading
Loading