diff --git a/data/shaders/d3d12/fullscreen_quad.vs.hlsl b/data/shaders/d3d12/fullscreen_quad.vs.hlsl new file mode 100644 index 00000000..f8703e4d --- /dev/null +++ b/data/shaders/d3d12/fullscreen_quad.vs.hlsl @@ -0,0 +1,20 @@ +// Pre-baked fullscreen-quad vertex shader for the D3D12 backend. +// +// Equivalent to ShaderGen::GenerateScreenQuadVertexShader() invoked with +// HostDisplay::RenderAPI::D3D12 in src/core/shadergen.cpp. The runtime +// path goes through D3DCompile on the generated HLSL; the pre-bake path +// embeds the DXBC output of fxc on this source. +// +// No state dependency. The body computes a fullscreen triangle from +// SV_VertexID (0, 1, 2) using the standard bit-shift trick - v_id 0 +// emits (0, 0), v_id 1 emits (2, 0), v_id 2 emits (0, 2) in tex-space, +// mapped to NDC by the * float2(2, -2) + float2(-1, 1) transform. + +void main( + in uint v_id : SV_VertexID, + out float2 v_tex0 : TEXCOORD0, + out float4 v_pos : SV_Position) +{ + v_tex0 = float2(float((v_id << 1) & 2u), float(v_id & 2u)); + v_pos = float4(v_tex0 * float2(2.0f, -2.0f) + float2(-1.0f, 1.0f), 0.0f, 1.0f); +} diff --git a/src/common/d3d12/embedded_shaders.cpp b/src/common/d3d12/embedded_shaders.cpp index 8511627d..421d7160 100644 --- a/src/common/d3d12/embedded_shaders.cpp +++ b/src/common/d3d12/embedded_shaders.cpp @@ -17,9 +17,6 @@ // includes and the namespace. namespace D3D12::EmbeddedShaders { -// (No .inc includes yet - this foundation patch lays down the storage TU. -// Subsequent patches add HLSL sources under data/shaders/d3d12/ and the -// matching generated .inc under src/common/d3d12/embedded_dxbc/, each -// included from inside this namespace block.) +#include "embedded_dxbc/fullscreen_quad_vs.inc" } // namespace D3D12::EmbeddedShaders diff --git a/src/common/d3d12/embedded_shaders.h b/src/common/d3d12/embedded_shaders.h index 7a4849f6..80a74e1a 100644 --- a/src/common/d3d12/embedded_shaders.h +++ b/src/common/d3d12/embedded_shaders.h @@ -39,9 +39,16 @@ // namespace D3D12::EmbeddedShaders { -// (No extern declarations yet - this foundation patch lays down the -// directory layout, regen script, and namespace scaffolding. Subsequent -// patches add HLSL sources, generated .inc files, and the matching -// extern declarations here.) +// Fullscreen-quad vertex shader. Emits a fullscreen triangle in NDC +// from SV_VertexID 0..2 via the standard bit-shift trick - equivalent +// to ShaderGen::GenerateScreenQuadVertexShader() in D3D12 mode. Used +// by every non-batch pipeline in the D3D12 backend (vram_fill / +// vram_copy / vram_write / vram_update_depth / vram_readback / +// display / copy). Zero state dependency, so a single pre-baked +// variant covers all call sites. +// +// Source: data/shaders/d3d12/fullscreen_quad.vs.hlsl +extern const uint8_t k_fullscreen_quad_vs[]; +extern const size_t k_fullscreen_quad_vs_size_bytes; } // namespace D3D12::EmbeddedShaders diff --git a/src/core/gpu_hw_d3d12.cpp b/src/core/gpu_hw_d3d12.cpp index 7016c863..9a186dc7 100644 --- a/src/core/gpu_hw_d3d12.cpp +++ b/src/core/gpu_hw_d3d12.cpp @@ -3,6 +3,7 @@ #include "common/d3d11/shader_compiler.h" #include "common/d3d12/context.h" #include "common/d3d12/descriptor_heap_manager.h" +#include "common/d3d12/embedded_shaders.h" #include "common/d3d12/shader_cache.h" #include "common/d3d12/util.h" #include "common/log.h" @@ -822,11 +823,13 @@ bool GPU_HW_D3D12::CompilePipelines() const uint32_t batch_pipeline_progress_units = precompile_sync ? reachable_batch_cells * 2u * 5u : 0u; // Non-batch units only counted when we build them upfront. The - // fullscreen-quad VS (1), VRAM fill (4), VRAM copy (2), VRAM - // write (2), VRAM update depth (1), VRAM readback (1), display - // (6) and copy/blit (1) sum to 18 - one tick per pipeline so the - // progress bar tracks the actual work being done. - const uint32_t non_batch_progress_units = precompile_sync ? 18u : 0u; + // VRAM fill (4), VRAM copy (2), VRAM write (2), VRAM update depth + // (1), VRAM readback (1), display (6) and copy/blit (1) sum to 17 + // - one tick per pipeline so the progress bar tracks the actual + // work being done. (Down from 18 since the fullscreen-quad VS no + // longer compiles - statically linked from + // src/common/d3d12/embedded_dxbc/ via GetFullscreenQuadVertexShader.) + const uint32_t non_batch_progress_units = precompile_sync ? 17u : 0u; ShaderCompileProgressTracker progress("Compiling Pipelines", 2 + batch_shader_progress_units + batch_pipeline_progress_units + @@ -938,12 +941,13 @@ bool GPU_HW_D3D12::CompilePipelines() // function, after the rest of the non-batch pipelines are built. // Non-batch pipelines (VRAM fill / copy / write / update depth / - // readback, display, copy/blit, fullscreen-quad VS). On 'Enabled' - // build them all upfront here through the lazy helpers so the - // helpers' GetXxx slot-fill machinery is exercised the same way - // it would be at draw time. The progress bar continues to tick - // per pipeline so the user sees the same "1 + 4 + 2 + 2 + 1 + 1 - // + 6 + 1 = 18 unit" count it always did. + // readback, display, copy/blit). On 'Enabled' build them all + // upfront here through the lazy helpers so the helpers' GetXxx + // slot-fill machinery is exercised the same way it would be at + // draw time. The progress bar ticks per pipeline so the user sees + // a "4 + 2 + 2 + 1 + 1 + 6 + 1 = 17 unit" count. (Was 18 before + // the fullscreen-quad VS moved to pre-baked DXBC, which has no + // compilation step to tick for.) // // On 'Lazy' the worker thread reaches these via the same helpers // before walking the batch matrix. On 'Disabled' nothing happens @@ -952,9 +956,11 @@ bool GPU_HW_D3D12::CompilePipelines() // subsequent calls. if (precompile_sync) { - if (!GetFullscreenQuadVertexShader()) - return false; - progress.Increment(); + // (No fullscreen-quad VS step here - it's pre-baked DXBC now, + // statically linked from src/common/d3d12/embedded_dxbc/, so + // there's nothing to compile or fault in. See + // GetFullscreenQuadVertexShader. Progress unit count below drops + // by 1 accordingly.) for (uint8_t wrapped = 0; wrapped < 2; wrapped++) { @@ -1012,15 +1018,15 @@ bool GPU_HW_D3D12::CompilePipelines() // entire worker run (30-60 seconds) waiting for a lock window // between compiles. The window appears frozen the whole time. // - // Pre-filling the 18 non-batch pipelines here costs Lazy the same + // Pre-filling the 17 non-batch pipelines here costs Lazy the same // few-hundred-ms upfront pause 'Enabled' pays for its non-batch // section, which is well under a second on the 5090 and not // perceived as a "frozen" window. The worker then only walks the // batch matrix - the main thread doesn't generally need batch // PSOs until a few frames into gameplay, by which point the // worker is past its first few cells and the wait window is brief. - if (!GetFullscreenQuadVertexShader()) - return false; + // (No fullscreen-quad VS pre-fill here either - pre-baked DXBC, + // see GetFullscreenQuadVertexShader.) for (uint8_t wrapped = 0; wrapped < 2; wrapped++) { for (uint8_t interlaced = 0; interlaced < 2; interlaced++) @@ -1410,42 +1416,23 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetBatchPipeline(GPUText // on a miss. The fast path is one uncontended atomic load per call // once the slot is filled. // -// The shared fullscreen-quad vertex shader has its own helper that -// the PSO helpers fall through to. That keeps each fault-in -// self-contained: the very first non-batch helper call from a -// 'Disabled' session pays for the VS compile, every subsequent call -// reuses it. +// The shared fullscreen-quad vertex shader is pre-baked DXBC (see +// GetFullscreenQuadVertexShader and src/common/d3d12/embedded_shaders.h), +// statically linked - no compile, no caching state, no mutex. PSO +// helpers below treat it as a free dependency. // ---------------------------------------------------------------------- -GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetFullscreenQuadVertexShader() +D3D12_SHADER_BYTECODE GPU_HW_D3D12::GetFullscreenQuadVertexShader() { - // Single-slot variant of the fast/slow path. Fast path: read the - // ComPtr under the lock (cheap - just a pointer copy + AddRef). - // Slow path: compile WITHOUT the lock held (so concurrent - // non-batch helpers can fault other pipelines while one compiles - // the shared VS), then publish under the lock with a double-check. - { - std::lock_guard lock(m_batch_shader_mutex); - if (m_fullscreen_quad_vertex_shader) - return m_fullscreen_quad_vertex_shader; - } - - if (!m_shadergen) - { - Log_ErrorPrint("GetFullscreenQuadVertexShader called before CompilePipelines constructed the shadergen"); - return {}; - } - ComPtr fresh = m_shader_cache.GetVertexShader(m_shadergen->GenerateScreenQuadVertexShader()); - if (!fresh) - { - Log_ErrorPrint("Lazy fullscreen-quad vertex shader compile failed"); - return {}; - } - - std::lock_guard lock(m_batch_shader_mutex); - if (!m_fullscreen_quad_vertex_shader) - m_fullscreen_quad_vertex_shader = fresh; - return m_fullscreen_quad_vertex_shader; + // Pre-baked DXBC blob - no compile, no caching state, no mutex. The + // blob is statically linked from src/common/d3d12/embedded_dxbc/ + // (generated offline by tools/regen_d3d12_dxbc.py from + // data/shaders/d3d12/fullscreen_quad.vs.hlsl). All callers receive + // the same const view; the storage outlives every PSO that binds it. + return D3D12_SHADER_BYTECODE{ + D3D12::EmbeddedShaders::k_fullscreen_quad_vs, + D3D12::EmbeddedShaders::k_fullscreen_quad_vs_size_bytes, + }; } GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetVRAMFillPipeline(uint8_t wrapped, uint8_t interlaced) @@ -1460,9 +1447,7 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetVRAMFillPipeline(uint return ret; } - ComPtr vs = GetFullscreenQuadVertexShader(); - if (!vs) - return {}; + const D3D12_SHADER_BYTECODE vs = GetFullscreenQuadVertexShader(); if (!m_shadergen) { @@ -1481,7 +1466,7 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetVRAMFillPipeline(uint gpbuilder.SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE); gpbuilder.SetNoCullRasterizationState(); gpbuilder.SetNoBlendingState(); - gpbuilder.SetVertexShader(vs.Get()); + gpbuilder.SetVertexShader(vs.pShaderBytecode, static_cast(vs.BytecodeLength)); gpbuilder.SetPixelShader(fs.Get()); gpbuilder.SetMultisamples(m_multisamples); gpbuilder.SetDepthState(true, true, D3D12_COMPARISON_FUNC_ALWAYS); @@ -1522,9 +1507,7 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetVRAMCopyPipeline(uint return ret; } - ComPtr vs = GetFullscreenQuadVertexShader(); - if (!vs) - return {}; + const D3D12_SHADER_BYTECODE vs = GetFullscreenQuadVertexShader(); if (!m_shadergen) { @@ -1542,7 +1525,7 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetVRAMCopyPipeline(uint gpbuilder.SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE); gpbuilder.SetNoCullRasterizationState(); gpbuilder.SetNoBlendingState(); - gpbuilder.SetVertexShader(vs.Get()); + gpbuilder.SetVertexShader(vs.pShaderBytecode, static_cast(vs.BytecodeLength)); gpbuilder.SetPixelShader(fs.Get()); gpbuilder.SetMultisamples(m_multisamples); gpbuilder.SetDepthState((depth_test != 0), true, @@ -1583,9 +1566,7 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetVRAMWritePipeline(uin return ret; } - ComPtr vs = GetFullscreenQuadVertexShader(); - if (!vs) - return {}; + const D3D12_SHADER_BYTECODE vs = GetFullscreenQuadVertexShader(); if (!m_shadergen) { @@ -1603,7 +1584,7 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetVRAMWritePipeline(uin gpbuilder.SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE); gpbuilder.SetNoCullRasterizationState(); gpbuilder.SetNoBlendingState(); - gpbuilder.SetVertexShader(vs.Get()); + gpbuilder.SetVertexShader(vs.pShaderBytecode, static_cast(vs.BytecodeLength)); gpbuilder.SetPixelShader(fs.Get()); gpbuilder.SetMultisamples(m_multisamples); gpbuilder.SetDepthState(true, true, @@ -1643,9 +1624,7 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetVRAMUpdateDepthPipeli return ret; } - ComPtr vs = GetFullscreenQuadVertexShader(); - if (!vs) - return {}; + const D3D12_SHADER_BYTECODE vs = GetFullscreenQuadVertexShader(); if (!m_shadergen) { @@ -1669,7 +1648,7 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetVRAMUpdateDepthPipeli gpbuilder.SetDepthStencilFormat(m_vram_depth_texture.GetFormat()); gpbuilder.SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE); gpbuilder.SetNoCullRasterizationState(); - gpbuilder.SetVertexShader(vs.Get()); + gpbuilder.SetVertexShader(vs.pShaderBytecode, static_cast(vs.BytecodeLength)); gpbuilder.SetPixelShader(fs.Get()); gpbuilder.SetMultisamples(m_multisamples); gpbuilder.SetDepthState(true, true, D3D12_COMPARISON_FUNC_ALWAYS); @@ -1710,9 +1689,7 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetVRAMReadbackPipeline( return ret; } - ComPtr vs = GetFullscreenQuadVertexShader(); - if (!vs) - return {}; + const D3D12_SHADER_BYTECODE vs = GetFullscreenQuadVertexShader(); if (!m_shadergen) { @@ -1726,7 +1703,7 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetVRAMReadbackPipeline( D3D12::GraphicsPipelineBuilder gpbuilder; gpbuilder.SetRootSignature(m_single_sampler_root_signature.Get()); gpbuilder.SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE); - gpbuilder.SetVertexShader(vs.Get()); + gpbuilder.SetVertexShader(vs.pShaderBytecode, static_cast(vs.BytecodeLength)); gpbuilder.SetPixelShader(fs.Get()); gpbuilder.SetNoCullRasterizationState(); gpbuilder.SetNoDepthTestState(); @@ -1768,9 +1745,7 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetDisplayPipeline(uint8 return ret; } - ComPtr vs = GetFullscreenQuadVertexShader(); - if (!vs) - return {}; + const D3D12_SHADER_BYTECODE vs = GetFullscreenQuadVertexShader(); if (!m_shadergen) { @@ -1785,7 +1760,7 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetDisplayPipeline(uint8 D3D12::GraphicsPipelineBuilder gpbuilder; gpbuilder.SetRootSignature(m_single_sampler_root_signature.Get()); gpbuilder.SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE); - gpbuilder.SetVertexShader(vs.Get()); + gpbuilder.SetVertexShader(vs.pShaderBytecode, static_cast(vs.BytecodeLength)); gpbuilder.SetPixelShader(fs.Get()); gpbuilder.SetNoCullRasterizationState(); gpbuilder.SetNoDepthTestState(); @@ -1826,9 +1801,7 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetCopyPipeline() return ret; } - ComPtr vs = GetFullscreenQuadVertexShader(); - if (!vs) - return {}; + const D3D12_SHADER_BYTECODE vs = GetFullscreenQuadVertexShader(); if (!m_shadergen) { @@ -1842,7 +1815,7 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetCopyPipeline() D3D12::GraphicsPipelineBuilder gpbuilder; gpbuilder.SetRootSignature(m_single_sampler_root_signature.Get()); gpbuilder.SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE); - gpbuilder.SetVertexShader(vs.Get()); + gpbuilder.SetVertexShader(vs.pShaderBytecode, static_cast(vs.BytecodeLength)); gpbuilder.SetPixelShader(fs.Get()); gpbuilder.SetNoCullRasterizationState(); gpbuilder.SetNoDepthTestState(); @@ -1918,12 +1891,8 @@ void GPU_HW_D3D12::DestroyPipelines() // m_copy_pipeline was previously not cleared here - latent leak // across UpdateSettings -> DestroyPipelines -> CompilePipelines - // cycles. The shared fullscreen-quad VS likewise needs to be - // dropped so the next CompilePipelines pass picks up any - // shadergen changes (e.g. the GLSL binding layout differing - // between Vulkan and Direct3D). + // cycles. m_copy_pipeline.Reset(); - m_fullscreen_quad_vertex_shader.Reset(); } void GPU_HW_D3D12::ClearDisplayPipelines() diff --git a/src/core/gpu_hw_d3d12.h b/src/core/gpu_hw_d3d12.h index 63cf4678..c1da62a4 100644 --- a/src/core/gpu_hw_d3d12.h +++ b/src/core/gpu_hw_d3d12.h @@ -179,10 +179,12 @@ private: // UpdateDisplay / etc. - takes no lock once the slot is filled. // The single-PSO ones use a bare std::atomic + ComPtr pair. // - // GetFullscreenQuadVertexShader is shared across most of the - // non-batch pipelines and is hoisted out as a separate helper so - // each PSO compile fetches it via the same lazy-fault pattern. - ComPtr GetFullscreenQuadVertexShader(); + // GetFullscreenQuadVertexShader returns the pre-baked DXBC blob for + // the screen-quad VS. No lazy compile, no caching state - the blob + // is statically linked from src/common/d3d12/embedded_dxbc/. Every + // non-batch pipeline in this backend uses it as the VS stage; see + // src/common/d3d12/embedded_shaders.h for the underlying declaration. + D3D12_SHADER_BYTECODE GetFullscreenQuadVertexShader(); ComPtr GetVRAMFillPipeline(uint8_t wrapped, uint8_t interlaced); ComPtr GetVRAMCopyPipeline(uint8_t depth_test); ComPtr GetVRAMWritePipeline(uint8_t depth_test); @@ -311,13 +313,6 @@ private: ComPtr m_copy_pipeline; std::atomic m_copy_pipeline_fastpath{nullptr}; - // Vertex shader shared across the non-batch pipelines (VRAM read, - // display, copy/blit). Compiled lazily on first use via - // GetFullscreenQuadVertexShader; serialised through - // m_batch_shader_mutex. Used to be a CompilePipelines local that - // was always built at init. - ComPtr m_fullscreen_quad_vertex_shader; - D3D12::Texture m_vram_write_replacement_texture; D3D12::StreamBuffer m_texture_replacment_stream_buffer; };