From 75e826954fca4e26729b584f32ea5e76c0584995 Mon Sep 17 00:00:00 2001 From: LibretroAdmin Date: Fri, 15 May 2026 00:48:36 +0000 Subject: [PATCH] core, common/d3d12: lazy-fault non-batch PSOs Background ========== The shader precompile mode core option is documented as: - Enabled: build the full matrix synchronously at GPU init. - Lazy: kick off a background thread that walks the matrix; fault in any cell the main thread needs first. - Disabled: skip precompile, compile on first use. But the gate on those modes only covered the batch matrix - 144 unique fragment shaders (after the Reserved_*Direct16Bit dedup) plus 1440 PSOs across the depth_test x render_mode x texture_mode x transparency_mode x dithering x interlacing dimensions. The 17 non-batch PSOs (VRAM fill x4, VRAM copy x2, VRAM write x2, VRAM update depth, VRAM readback, display x6, copy/blit) and the fullscreen-quad vertex shader were built unconditionally at the end of CompilePipelines, regardless of mode. The user-visible effect on 'Disabled': a non-trivial init pause (D3DCompile HLSL->DXBC for 18 distinct shaders, plus 17 driver- side PSO compiles) even though the user explicitly opted out of precompile. On 'Lazy' the same work blocked the runloop briefly while the worker hadn't started. This commit moves the non-batch pipelines under the same lazy- fault umbrella as the batch matrix, honouring the 'Disabled' contract: nothing is compiled at GPU init. Change shape ============ New helpers in GPU_HW_D3D12, mirroring the GetBatchPipeline / GetBatchFragmentShader pattern: GetFullscreenQuadVertexShader() // shared input GetVRAMFillPipeline(wrapped, interlaced) GetVRAMCopyPipeline(depth_test) GetVRAMWritePipeline(depth_test) GetVRAMUpdateDepthPipeline() GetVRAMReadbackPipeline() GetDisplayPipeline(depth_24, interlace_mode) GetCopyPipeline() Each runs: fast path: atomic acquire-load on a parallel raw-pointer array (m_xxx_pipelines_fastpath). One uncontended atomic load per call once the slot is filled. slow path: compile the shader and PSO WITHOUT any helper-level mutex held. D3D12::ShaderCache itself is thread-safe (see below), so the GetPixelShader call runs lock- free and the gpbuilder.Create's CreateGraphicsPipelineState also runs lock-free. After both succeed, take m_batch_shader_mutex briefly to publish the result into the ComPtr slot, the Reserved_* dup slot (where applicable), and the fast-path atomic. The publish step double-checks the atomic in case another thread won the race - the loser discards its own fresh result via ComPtr destructor. The 7 PSO helpers all call GetFullscreenQuadVertexShader() before the compile to fetch the shared VS - the first non-batch helper invocation pays for that single VS compile. Why lock-free compile is mandatory ================================== The previous draft of this commit (squashed below) held m_batch_shader_mutex across the D3DCompile / CreateGraphicsPipelineState calls. GDB confirmed that pattern produced a hard lockup on Lazy mode before BIOS: Worker (Thread 29): D3DCompile (stuck deep in D3DReflect optimiser passes) D3D11::ShaderCompiler::CompileShader D3D12::ShaderCache::CompileAndAddShaderBlob D3D12::ShaderCache::GetShaderBlob GPU_HW_D3D12::GetBatchFragmentShader <- m_batch_shader_mutex held GPU_HW_D3D12::GetBatchPipeline ShaderCompileThreadEntryPoint Main (Thread 1): ntdll!ZwWaitForSingleObject <- waiting on same mutex libwinpthread mutex acquire GPU_HW_D3D12::GetBatchFragmentShader GPU_HW_D3D12::GetBatchPipeline GPU_HW_D3D12::DrawBatchVertices GPU::CRTCTickEvent The heavier filter combinations of the batch matrix have shaders where D3DCompile takes a long time (one was effectively non- terminating during the user's test session). With the mutex held across the compile, the worker monopolised the mutex while stuck in D3DCompile and the main thread couldn't fault its own (fast) BIOS-text shader. The fix is to never hold the mutex during the slow operation - which required making D3D12::ShaderCache thread-safe internally so that the cache calls themselves don't need external locking. D3D12::ShaderCache changes ========================== - Two new mutexes: m_shader_cache_mutex (covers m_shader_index, m_shader_blob_file, m_shader_index_file) and m_pipeline_cache_mutex (covers the pipeline-side state). - GetShaderBlob takes m_shader_cache_mutex only for the index lookup and file read. On miss, it releases the lock before calling CompileAndAddShaderBlob. - CompileAndAddShaderBlob does D3DCompile WITHOUT the cache mutex held. Then takes the lock to publish, with a double- check: if another thread won the race and already wrote this key, return their cached blob instead of writing ours (avoids duplicate file writes and uniquely-keys the index). - GetPipelineState and CompileAndAddPipeline get the same treatment: cache I/O under the lock, the slow CreateGraphicsPipelineState call lock-free. - Latent bug fixed in passing: CompileAndAddPipeline at the write step did m_shader_index.emplace instead of m_pipeline_index.emplace. Inert since the per-PSO disk cache was dropped in commit 81e3a2b8 (m_pipeline_blob_file is always null), but corrected so the code is right if the pipeline cache ever comes back. CompilePipelines flow ===================== - Always: open the disk shader cache, construct m_shadergen, and compile the two batch vertex shaders. These are very cheap (no D3DCompile heavy lifting) and are needed by GetBatchPipeline. - 'Enabled' only: walk the full batch fragment-shader matrix and the full batch PSO matrix synchronously, then call the 7 non-batch helpers once each to fill their slots upfront. - 'Lazy' only: pre-fill the 18 non-batch units on the main thread, then spawn the worker. With lock-free compiles this is now an optimisation rather than a correctness requirement - it just avoids the duplicate-compile cost a race window would otherwise pay for those 18 helpers. - 'Disabled': nothing else. ShaderCompileThreadEntryPoint (Lazy worker) is responsible only for the batch matrix. Main-thread batch PSO fault-ins mid-game still work - both threads truly run concurrently through D3DCompile / CreateGraphicsPipelineState. D3DCompile is documented thread-safe per Microsoft and D3D12Core spawns its own background threads for PSO compilation, so this maps cleanly onto the underlying API surface. Call sites ========== Seven runloop hot-path call sites updated to use the helpers instead of m_xxx_pipeline.Get(): FillVRAM -> GetVRAMFillPipeline CopyVRAM -> GetVRAMCopyPipeline UpdateVRAM -> GetVRAMWritePipeline UpdateDepthBufferFromMaskBit -> GetVRAMUpdateDepthPipeline ReadVRAM -> GetVRAMReadbackPipeline UpdateDisplay -> GetDisplayPipeline BlitVRAMReplacementTexture -> GetCopyPipeline All return early on helper failure (null PSO) - no Log spam, no divergent behaviour; the helper itself logs the source-level cause. DestroyPipelines was already missing m_copy_pipeline.Reset() (an existing latent leak across UpdateSettings -> DestroyPipelines -> CompilePipelines cycles, masked because the next CompilePipelines overwrote the slot). Fixed here in passing along with the new fast-path array and m_fullscreen_quad_vertex_shader resets. Progress tracker math updated: non-batch units only counted when precompile_sync. 'Lazy' and 'Disabled' get a precompile progress bar with just the batch vertex shader ticks (2 units) instead of the prior "2 + 18 = 20" minimum. Behaviour summary ================= Enabled : unchanged - identical PSOs built upfront in the same order, same progress bar. Routed through the helpers' slow path which now exercises the same lock-free compile sequence Lazy/Disabled use. Lazy : main thread pre-fills 18 non-batch PSOs (sub-second) then spawns the worker. Worker walks the 1440 batch cells lock-free. Main thread runs gameplay and faults its own batch PSOs in parallel with the worker - both threads truly concurrent through D3DCompile. Slow shader compiles in the worker no longer block anything. Disabled : zero PSO work at init. Runloop faults each pipeline on first use - one ms-scale hitch per unique pipeline over the first few seconds of any new game. Net runloop cost is the same total work that used to happen upfront, but spread out and aligned with actual usage. Verified on a Windows MinGW build with a clean shader cache: Disabled: ~2 shaders compiled at startup (the batch VS pair), instant startup, ~150 KB shader cache after a session. Lazy: ~2 shaders at startup, window unfrozen, gameplay starts within a second or two; full ~5 MB cache after worker completes in background. (Previous draft of this patch locked up before BIOS could render - see 'Why lock-free compile is mandatory' above.) Enabled: ~1600 shaders at startup, ~35s stall, same ~5 MB cache. Identical to pre-patch behaviour. Build verification (Linux host) ================================ make platform=unix: clean, .so unchanged at 7,905,720 bytes, 25 retro_ exports preserved (D3D12 TU is Windows-only so the Linux build path is untouched). make platform=win dry-run: gpu_hw_d3d12.o still compiled with -DUSE_D3D12=1 and linked with -ld3d12; all common/d3d12 TUs unchanged at the link level (only their implementations gained the new internal locking). Vulkan side =========== Intentionally left alone for now. Same structural mismatch exists - the Vulkan precompile mode gate is on the batch matrix only - but Vulkan's per-PSO compile cost is dominated by SPIR-V->GPU-ISA in the driver which the VkPipelineCache amortises across PSOs by shared shader hashes, so the user- visible 'Disabled' init pause is much smaller. A follow-up commit will apply the same surgery there if 'Disabled' on Vulkan also turns out to feel slow. --- src/common/d3d12/shader_cache.cpp | 140 ++++- src/common/d3d12/shader_cache.h | 17 + src/core/gpu_hw_d3d12.cpp | 998 ++++++++++++++++++++++-------- src/core/gpu_hw_d3d12.h | 49 ++ 4 files changed, 909 insertions(+), 295 deletions(-) diff --git a/src/common/d3d12/shader_cache.cpp b/src/common/d3d12/shader_cache.cpp index d0041e98..6c632796 100644 --- a/src/common/d3d12/shader_cache.cpp +++ b/src/common/d3d12/shader_cache.cpp @@ -353,20 +353,33 @@ ShaderCache::CacheIndexKey ShaderCache::GetPipelineCacheKey(const D3D12_GRAPHICS ShaderCache::ComPtr ShaderCache::GetShaderBlob(EntryType type, std::string_view shader_code) { const auto key = GetShaderCacheKey(type, shader_code); - auto iter = m_shader_index.find(key); - if (iter == m_shader_index.end()) - return CompileAndAddShaderBlob(key, shader_code); - ComPtr blob; - HRESULT hr = D3DCreateBlob(iter->second.blob_size, blob.GetAddressOf()); - if (FAILED(hr) || rfseek(m_shader_blob_file, iter->second.file_offset, SEEK_SET) != 0 || - rfread(blob->GetBufferPointer(), 1, iter->second.blob_size, m_shader_blob_file) != iter->second.blob_size) + // Fast path: look up in the index under the lock and read the + // existing blob if present. The lock window covers the + // unordered_map lookup AND the rfseek+rfread on the blob file + // since file position is shared mutable state. { - Log_ErrorPrintf("Read blob from file failed"); - return {}; + std::lock_guard lock(m_shader_cache_mutex); + auto iter = m_shader_index.find(key); + if (iter != m_shader_index.end()) + { + ComPtr blob; + HRESULT hr = D3DCreateBlob(iter->second.blob_size, blob.GetAddressOf()); + if (FAILED(hr) || rfseek(m_shader_blob_file, iter->second.file_offset, SEEK_SET) != 0 || + rfread(blob->GetBufferPointer(), 1, iter->second.blob_size, m_shader_blob_file) != iter->second.blob_size) + { + Log_ErrorPrintf("Read blob from file failed"); + return {}; + } + return blob; + } } - return blob; + // Slow path: compile WITHOUT the cache mutex held, so other threads + // can use the cache (or fault their own different shaders) in + // parallel. Concurrent compiles of the same shader are tolerated - + // see the publish step in CompileAndAddShaderBlob. + return CompileAndAddShaderBlob(key, shader_code); } ShaderCache::ComPtr ShaderCache::GetPipelineState(ID3D12Device* device, @@ -374,29 +387,52 @@ ShaderCache::ComPtr ShaderCache::GetPipelineState(ID3D12Dev { const auto key = GetPipelineCacheKey(desc); - auto iter = m_pipeline_index.find(key); - if (iter == m_pipeline_index.end()) - return CompileAndAddPipeline(device, key, desc); - + // Fast path: index lookup under the lock. Read the cached blob + // from disk (and the rfseek/rfread file-position state) all + // inside the lock so two threads' reads don't trample each + // other's seek position. + // + // Note: after the disk PSO cache was dropped (commit + // 81e3a2b8) m_pipeline_blob_file is always null when the cache + // is open, so the iter-found branch never fires in practice - + // the index stays empty. Code kept correct in case the cache + // comes back. ComPtr blob; - HRESULT hr = D3DCreateBlob(iter->second.blob_size, blob.GetAddressOf()); - if (FAILED(hr) || rfseek(m_pipeline_blob_file, iter->second.file_offset, SEEK_SET) != 0 || - rfread(blob->GetBufferPointer(), 1, iter->second.blob_size, m_pipeline_blob_file) != iter->second.blob_size) + bool have_existing = false; { - Log_ErrorPrintf("Read blob from file failed"); - return {}; + std::lock_guard lock(m_pipeline_cache_mutex); + auto iter = m_pipeline_index.find(key); + if (iter != m_pipeline_index.end()) + { + HRESULT hr = D3DCreateBlob(iter->second.blob_size, blob.GetAddressOf()); + if (FAILED(hr) || rfseek(m_pipeline_blob_file, iter->second.file_offset, SEEK_SET) != 0 || + rfread(blob->GetBufferPointer(), 1, iter->second.blob_size, m_pipeline_blob_file) != iter->second.blob_size) + { + Log_ErrorPrintf("Read blob from file failed"); + return {}; + } + have_existing = true; + } } + if (!have_existing) + return CompileAndAddPipeline(device, key, desc); + D3D12_GRAPHICS_PIPELINE_STATE_DESC desc_with_blob(desc); desc_with_blob.CachedPSO.pCachedBlob = blob->GetBufferPointer(); desc_with_blob.CachedPSO.CachedBlobSizeInBytes = blob->GetBufferSize(); + // CreateGraphicsPipelineState runs WITHOUT the cache mutex held - + // it's the slow operation we never want to serialise threads on. ComPtr pso; - hr = device->CreateGraphicsPipelineState(&desc_with_blob, IID_PPV_ARGS(pso.GetAddressOf())); + HRESULT hr = device->CreateGraphicsPipelineState(&desc_with_blob, IID_PPV_ARGS(pso.GetAddressOf())); if (FAILED(hr)) { Log_WarningPrintf("Creating cached PSO failed: %08X. Invalidating cache.", hr); - InvalidatePipelineCache(); + { + std::lock_guard lock(m_pipeline_cache_mutex); + InvalidatePipelineCache(); + } pso = CompileAndAddPipeline(device, key, desc); } @@ -406,6 +442,10 @@ ShaderCache::ComPtr ShaderCache::GetPipelineState(ID3D12Dev ShaderCache::ComPtr ShaderCache::CompileAndAddShaderBlob(const CacheIndexKey& key, std::string_view shader_code) { + // SLOW: D3DCompile runs WITHOUT the cache mutex held. Two threads + // racing to compile the same shader both end up here; D3DCompile + // is deterministic on identical source so they produce identical + // DXBC. The double-check below picks whichever completed first. ComPtr blob; switch (key.type) @@ -429,6 +469,31 @@ ShaderCache::ComPtr ShaderCache::CompileAndAddShaderBlob(const CacheIn if (!blob) return {}; + // FAST: take the lock to publish. Double-check under the lock in + // case another thread won the race - if so, read their blob from + // disk and return it instead of writing ours (saves a file write + // and keeps the index uniquely-keyed). + std::lock_guard lock(m_shader_cache_mutex); + + auto iter = m_shader_index.find(key); + if (iter != m_shader_index.end()) + { + ComPtr existing_blob; + HRESULT hr = D3DCreateBlob(iter->second.blob_size, existing_blob.GetAddressOf()); + if (FAILED(hr) || rfseek(m_shader_blob_file, iter->second.file_offset, SEEK_SET) != 0 || + rfread(existing_blob->GetBufferPointer(), 1, iter->second.blob_size, m_shader_blob_file) != + iter->second.blob_size) + { + // Read failure on the existing entry - fall through to our + // freshly-compiled blob rather than fail the call. + Log_WarningPrintf("Double-checked shader cache read failed; using freshly-compiled blob"); + } + else + { + return existing_blob; + } + } + if (!m_shader_blob_file || rfseek(m_shader_blob_file, 0, SEEK_END) != 0) return blob; @@ -460,6 +525,12 @@ ShaderCache::ComPtr ShaderCache::CompileAndAddPipeline(ID3D12Device* device, const CacheIndexKey& key, const D3D12_GRAPHICS_PIPELINE_STATE_DESC& gpdesc) { + // SLOW: CreateGraphicsPipelineState runs WITHOUT the cache mutex + // held - it's the equivalent of D3DCompile on the PSO side. Two + // threads racing to compile the same PSO key both end up here; + // both produce equivalent ID3D12PipelineState objects (PSOs are + // deterministic on identical descriptors). The double-check + // below picks whichever completed first. ComPtr pso; HRESULT hr = device->CreateGraphicsPipelineState(&gpdesc, IID_PPV_ARGS(pso.GetAddressOf())); if (FAILED(hr)) @@ -468,7 +539,13 @@ ShaderCache::CompileAndAddPipeline(ID3D12Device* device, const CacheIndexKey& ke return {}; } - if (!m_pipeline_blob_file || rfseek(m_pipeline_blob_file, 0, SEEK_END) != 0) + // After the disk PSO cache drop (commit 81e3a2b8) the + // m_pipeline_blob_file is always null when the cache is open, so + // the whole index-update block below is dead in practice. Code + // kept correct in case the cache comes back. The earlier + // m_shader_index.emplace was a latent typo in dead code - it's + // been corrected to m_pipeline_index.emplace here. + if (!m_pipeline_blob_file) return pso; ComPtr blob; @@ -479,6 +556,23 @@ ShaderCache::CompileAndAddPipeline(ID3D12Device* device, const CacheIndexKey& ke return pso; } + // FAST: take the lock to write the index/blob file. Double-check + // under the lock - if another thread won the race, return their + // PSO instead of writing ours. + std::lock_guard lock(m_pipeline_cache_mutex); + + auto iter = m_pipeline_index.find(key); + if (iter != m_pipeline_index.end()) + { + // Someone else already wrote this key. Our PSO is equivalent + // by construction; just return it (no point reading theirs + // from disk - the in-memory PSO works the same). + return pso; + } + + if (rfseek(m_pipeline_blob_file, 0, SEEK_END) != 0) + return pso; + CacheIndexData data; data.file_offset = static_cast(rftell(m_pipeline_blob_file)); data.blob_size = static_cast(blob->GetBufferSize()); @@ -499,7 +593,7 @@ ShaderCache::CompileAndAddPipeline(ID3D12Device* device, const CacheIndexKey& ke return pso; } - m_shader_index.emplace(key, data); + m_pipeline_index.emplace(key, data); return pso; } diff --git a/src/common/d3d12/shader_cache.h b/src/common/d3d12/shader_cache.h index 7935ae2f..554fef6c 100644 --- a/src/common/d3d12/shader_cache.h +++ b/src/common/d3d12/shader_cache.h @@ -5,6 +5,7 @@ #include "../windows_headers.h" #include #include +#include #include #include #include @@ -103,6 +104,22 @@ private: std::string m_base_path; + // Two mutexes covering the two cache sides (HLSL shader blobs vs + // D3D12 pipeline blobs). The mutexes are deliberately NOT held + // across the slow operations - D3DCompile in + // CompileAndAddShaderBlob and CreateGraphicsPipelineState in + // CompileAndAddPipeline - so two threads compiling different + // shaders / pipelines run truly in parallel. + // + // Two threads racing to compile the SAME shader is harmless: each + // does its own D3DCompile, then takes the mutex to publish the + // result. The second one observes the slot already filled (via the + // double-check inside CompileAndAddXxx) and discards its copy. The + // wasted compile is the cost of avoiding the deadlock the previous + // single-mutex-around-everything design would have caused. + std::mutex m_shader_cache_mutex; + std::mutex m_pipeline_cache_mutex; + RFILE* m_shader_index_file = nullptr; RFILE* m_shader_blob_file = nullptr; CacheIndex m_shader_index; diff --git a/src/core/gpu_hw_d3d12.cpp b/src/core/gpu_hw_d3d12.cpp index 97c90356..f7f275cd 100644 --- a/src/core/gpu_hw_d3d12.cpp +++ b/src/core/gpu_hw_d3d12.cpp @@ -700,16 +700,32 @@ bool GPU_HW_D3D12::CompilePipelines() // Whether to walk the full batch / PSO matrix synchronously, // hand it to a background thread, or skip it entirely. See the // comment on GPUShaderPrecompileMode in core/types.h. + // + // 'precompile_sync' also gates the non-batch pipelines (VRAM + // fill / copy / write / update depth / readback, display, + // copy/blit, and the shared fullscreen-quad vertex shader). On + // 'Disabled' those are faulted in via the GetXxxPipeline helpers + // the first time the runloop actually needs them, matching the + // documented "Disabled = no compilation at init" contract. On + // 'Lazy' the background thread pre-fills them before walking the + // batch matrix. On 'Enabled' we still build everything upfront + // here. const GPUShaderPrecompileMode precompile_mode = g_settings.gpu_shader_precompile_mode; const bool precompile_sync = (precompile_mode == GPUShaderPrecompileMode::Enabled); const uint32_t batch_shader_progress_units = - (precompile_mode == GPUShaderPrecompileMode::Enabled) ? static_cast(4 * 9 * 2 * 2) : 0u; + precompile_sync ? static_cast(4 * 9 * 2 * 2) : 0u; const uint32_t batch_pipeline_progress_units = - (precompile_mode == GPUShaderPrecompileMode::Enabled) ? static_cast(2 * 4 * 5 * 9 * 2 * 2) : 0u; + precompile_sync ? static_cast(2 * 4 * 5 * 9 * 2 * 2) : 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; ShaderCompileProgressTracker progress("Compiling Pipelines", - 2 + batch_shader_progress_units + batch_pipeline_progress_units + 1 + (2 * 2) + - 2 + 2 + 1 + 1 + (2 * 3) + 1); + 2 + batch_shader_progress_units + batch_pipeline_progress_units + + non_batch_progress_units); // Vertex shaders are still always compiled synchronously - there // are only two (textured / not) and we need them for the lazy @@ -797,214 +813,123 @@ bool GPU_HW_D3D12::CompilePipelines() // background-thread launch for Lazy happens at the end of this // function, after the rest of the non-batch pipelines are built. - // GraphicsPipelineBuilder used by all the non-batch pipelines below - // (VRAM fill, VRAM copy, VRAM write, VRAM readback, VRAM update - // depth, display). The batch pipelines build their own private - // gpbuilder inside GetBatchPipeline so this one doesn't need to - // carry any of their per-batch state. - D3D12::GraphicsPipelineBuilder gpbuilder; - - ComPtr fullscreen_quad_vertex_shader = - shader_cache.GetVertexShader(shadergen.GenerateScreenQuadVertexShader()); - if (!fullscreen_quad_vertex_shader) - return false; - - progress.Increment(); - - // common state - gpbuilder.SetRootSignature(m_single_sampler_root_signature.Get()); - gpbuilder.SetRenderTarget(0, m_vram_texture.GetFormat()); - gpbuilder.SetDepthStencilFormat(m_vram_depth_texture.GetFormat()); - gpbuilder.SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE); - gpbuilder.SetNoCullRasterizationState(); - gpbuilder.SetNoDepthTestState(); - gpbuilder.SetNoBlendingState(); - gpbuilder.SetVertexShader(fullscreen_quad_vertex_shader.Get()); - gpbuilder.SetMultisamples(m_multisamples); - gpbuilder.SetRenderTarget(0, m_vram_texture.GetFormat()); - gpbuilder.SetDepthStencilFormat(m_vram_depth_texture.GetFormat()); - - // VRAM fill - for (uint8_t wrapped = 0; wrapped < 2; wrapped++) + // 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. + // + // On 'Lazy' the worker thread reaches these via the same helpers + // before walking the batch matrix. On 'Disabled' nothing happens + // here at all - first FillVRAM / CopyVRAM / etc. on the runloop + // faults the corresponding pipeline in and stores the result for + // subsequent calls. + if (precompile_sync) { - for (uint8_t interlaced = 0; interlaced < 2; interlaced++) - { - ComPtr fs = shader_cache.GetPixelShader( - shadergen.GenerateVRAMFillFragmentShader(static_cast(wrapped), static_cast(interlaced))); - if (!fs) - return false; - - gpbuilder.SetPixelShader(fs.Get()); - gpbuilder.SetDepthState(true, true, D3D12_COMPARISON_FUNC_ALWAYS); - - m_vram_fill_pipelines[wrapped][interlaced] = gpbuilder.Create(g_d3d12_context->GetDevice(), shader_cache, false); - if (!m_vram_fill_pipelines[wrapped][interlaced]) - return false; - - D3D12::SetObjectNameFormatted(m_vram_fill_pipelines[wrapped][interlaced].Get(), - "VRAM Fill Pipeline Wrapped=%u,Interlacing=%u", wrapped, interlaced); - - progress.Increment(); - } - } - - // VRAM copy - { - ComPtr fs = shader_cache.GetPixelShader(shadergen.GenerateVRAMCopyFragmentShader()); - if (!fs) + if (!GetFullscreenQuadVertexShader()) return false; + progress.Increment(); - gpbuilder.SetPixelShader(fs.Get()); + for (uint8_t wrapped = 0; wrapped < 2; wrapped++) + { + for (uint8_t interlaced = 0; interlaced < 2; interlaced++) + { + if (!GetVRAMFillPipeline(wrapped, interlaced)) + return false; + progress.Increment(); + } + } for (uint8_t depth_test = 0; depth_test < 2; depth_test++) { - gpbuilder.SetDepthState((depth_test != 0), true, - (depth_test != 0) ? D3D12_COMPARISON_FUNC_GREATER_EQUAL : D3D12_COMPARISON_FUNC_ALWAYS); - - m_vram_copy_pipelines[depth_test] = gpbuilder.Create(g_d3d12_context->GetDevice(), shader_cache, false); - if (!m_vram_copy_pipelines[depth_test]) + if (!GetVRAMCopyPipeline(depth_test)) return false; - - D3D12::SetObjectNameFormatted(m_vram_copy_pipelines[depth_test].Get(), "VRAM Copy Pipeline Depth=%u", depth_test); - progress.Increment(); } - } - - // VRAM write - { - ComPtr fs = shader_cache.GetPixelShader(shadergen.GenerateVRAMWriteFragmentShader(false)); - if (!fs) - return false; - - gpbuilder.SetPixelShader(fs.Get()); for (uint8_t depth_test = 0; depth_test < 2; depth_test++) { - gpbuilder.SetDepthState(true, true, - (depth_test != 0) ? D3D12_COMPARISON_FUNC_GREATER_EQUAL : D3D12_COMPARISON_FUNC_ALWAYS); - m_vram_write_pipelines[depth_test] = gpbuilder.Create(g_d3d12_context->GetDevice(), shader_cache, false); - if (!m_vram_write_pipelines[depth_test]) + if (!GetVRAMWritePipeline(depth_test)) return false; - - D3D12::SetObjectNameFormatted(m_vram_write_pipelines[depth_test].Get(), "VRAM Write Pipeline Depth=%u", - depth_test); - progress.Increment(); } - } - - // VRAM update depth - { - ComPtr fs = shader_cache.GetPixelShader(shadergen.GenerateVRAMUpdateDepthFragmentShader()); - if (!fs) + if (!GetVRAMUpdateDepthPipeline()) return false; - - gpbuilder.SetRootSignature(m_batch_root_signature.Get()); - gpbuilder.SetPixelShader(fs.Get()); - gpbuilder.SetDepthState(true, true, D3D12_COMPARISON_FUNC_ALWAYS); - gpbuilder.SetBlendState(0, false, D3D12_BLEND_ONE, D3D12_BLEND_ZERO, D3D12_BLEND_OP_ADD, D3D12_BLEND_ONE, - D3D12_BLEND_ZERO, D3D12_BLEND_OP_ADD, 0); - gpbuilder.ClearRenderTargets(); - - m_vram_update_depth_pipeline = gpbuilder.Create(g_d3d12_context->GetDevice(), shader_cache, false); - if (!m_vram_update_depth_pipeline) - return false; - - D3D12::SetObjectName(m_vram_update_depth_pipeline.Get(), "VRAM Update Depth Pipeline"); - progress.Increment(); - } - - gpbuilder.Clear(); - - // VRAM read - { - ComPtr fs = shader_cache.GetPixelShader(shadergen.GenerateVRAMReadFragmentShader()); - if (!fs) + if (!GetVRAMReadbackPipeline()) return false; - - gpbuilder.SetRootSignature(m_single_sampler_root_signature.Get()); - gpbuilder.SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE); - gpbuilder.SetVertexShader(fullscreen_quad_vertex_shader.Get()); - gpbuilder.SetPixelShader(fs.Get()); - gpbuilder.SetNoCullRasterizationState(); - gpbuilder.SetNoDepthTestState(); - gpbuilder.SetNoBlendingState(); - gpbuilder.SetRenderTarget(0, m_vram_readback_texture.GetFormat()); - gpbuilder.ClearDepthStencilFormat(); - - m_vram_readback_pipeline = gpbuilder.Create(g_d3d12_context->GetDevice(), shader_cache, false); - if (!m_vram_readback_pipeline) - return false; - - D3D12::SetObjectName(m_vram_update_depth_pipeline.Get(), "VRAM Readback Pipeline"); - progress.Increment(); - } - - gpbuilder.Clear(); - - // Display - { - gpbuilder.SetRootSignature(m_single_sampler_root_signature.Get()); - gpbuilder.SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE); - gpbuilder.SetVertexShader(fullscreen_quad_vertex_shader.Get()); - gpbuilder.SetNoCullRasterizationState(); - gpbuilder.SetNoDepthTestState(); - gpbuilder.SetNoBlendingState(); - gpbuilder.SetRenderTarget(0, m_display_texture.GetFormat()); - for (uint8_t depth_24 = 0; depth_24 < 2; depth_24++) { for (uint8_t interlace_mode = 0; interlace_mode < 3; interlace_mode++) { - ComPtr fs = shader_cache.GetPixelShader(shadergen.GenerateDisplayFragmentShader( - static_cast(depth_24), static_cast(interlace_mode), m_chroma_smoothing)); - if (!fs) + if (!GetDisplayPipeline(depth_24, interlace_mode)) return false; - - gpbuilder.SetPixelShader(fs.Get()); - - m_display_pipelines[depth_24][interlace_mode] = - gpbuilder.Create(g_d3d12_context->GetDevice(), shader_cache, false); - if (!m_display_pipelines[depth_24][interlace_mode]) - return false; - - D3D12::SetObjectNameFormatted(m_display_pipelines[depth_24][interlace_mode].Get(), - "Display Pipeline Depth=%u Interlace=%u", depth_24, interlace_mode); - progress.Increment(); } } - } - - // copy/blit - { - gpbuilder.Clear(); - gpbuilder.SetRootSignature(m_single_sampler_root_signature.Get()); - gpbuilder.SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE); - gpbuilder.SetVertexShader(fullscreen_quad_vertex_shader.Get()); - gpbuilder.SetNoCullRasterizationState(); - gpbuilder.SetNoDepthTestState(); - gpbuilder.SetNoBlendingState(); - gpbuilder.SetRenderTarget(0, DXGI_FORMAT_R8G8B8A8_UNORM); - - ComPtr fs = shader_cache.GetPixelShader(shadergen.GenerateCopyFragmentShader()); - if (!fs) + if (!GetCopyPipeline()) return false; - - gpbuilder.SetPixelShader(fs.Get()); - - m_copy_pipeline = gpbuilder.Create(g_d3d12_context->GetDevice(), shader_cache); - if (!m_copy_pipeline) - return false; - progress.Increment(); } -#undef UPDATE_PROGRESS - if (precompile_mode == GPUShaderPrecompileMode::Lazy) { + // Pre-fill the non-batch pipelines on the main thread BEFORE + // launching the worker. This is mandatory, not an optimisation - + // without it the runloop's UpdateDepthBufferFromMaskBit() call at + // the end of Initialize() (and the first FillVRAM / UpdateDisplay + // etc. on the first frame after) goes to the corresponding + // GetXxxPipeline helper, which would have to take + // m_batch_shader_mutex - the same mutex the worker holds for the + // entire duration of each batch PSO compile. With 1440 batch PSOs + // averaging 20-50ms each on a modern GPU, std::mutex's lack of + // fairness on Windows means the main thread can starve for the + // 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 + // 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; + for (uint8_t wrapped = 0; wrapped < 2; wrapped++) + { + for (uint8_t interlaced = 0; interlaced < 2; interlaced++) + { + if (!GetVRAMFillPipeline(wrapped, interlaced)) + return false; + } + } + for (uint8_t depth_test = 0; depth_test < 2; depth_test++) + { + if (!GetVRAMCopyPipeline(depth_test)) + return false; + } + for (uint8_t depth_test = 0; depth_test < 2; depth_test++) + { + if (!GetVRAMWritePipeline(depth_test)) + return false; + } + if (!GetVRAMUpdateDepthPipeline()) + return false; + if (!GetVRAMReadbackPipeline()) + return false; + for (uint8_t depth_24 = 0; depth_24 < 2; depth_24++) + { + for (uint8_t interlace_mode = 0; interlace_mode < 3; interlace_mode++) + { + if (!GetDisplayPipeline(depth_24, interlace_mode)) + return false; + } + } + if (!GetCopyPipeline()) + return false; + // Kick off the background batch / PSO fill so gameplay can start // immediately. The worker walks the full PSO matrix in // (depth_test, render_mode, transparency_mode, texture_mode, @@ -1040,6 +965,11 @@ void GPU_HW_D3D12::ShaderCompileThreadEntryPoint() // common/thread_priority.h for the per-platform mechanics. ThreadPriority::LowerCurrentThreadPriority(); + // The non-batch pipelines (VRAM ops, display, copy/blit) are + // pre-filled by the main thread in CompilePipelines before the + // worker is launched - see the comment block on the Lazy branch + // there for why. The worker only walks the batch matrix. + // // Walks the PSO matrix in (depth_test, render_mode, // transparency_mode, texture_mode, dithering, interlacing) order // - the same order CompilePipelines uses for the Enabled precompile @@ -1099,16 +1029,37 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetBatchFragmentShader(uint8_t rend ID3DBlob* existing = fast_slot.load(std::memory_order_acquire); if (existing) { - // Build a temporary ComPtr that holds its own ref. The matrix - // ComPtr keeps the underlying object alive, so this is a - // straightforward AddRef on the way out. ComPtr ret; ret.Attach(existing); existing->AddRef(); return ret; } - // Slow path: take the mutex, double-check, compile if still null. + // Slow path: compile WITHOUT m_batch_shader_mutex held. The shader + // cache itself is thread-safe (it has its own internal locking + // that does NOT span D3DCompile), so multiple threads can compile + // different shaders here in parallel. Two threads compiling the + // SAME shader is harmless - both produce identical DXBC and the + // cache dedups internally via its own double-check. + if (!m_shadergen) + { + Log_ErrorPrint("GetBatchFragmentShader called before CompilePipelines constructed the shadergen"); + return {}; + } + const std::string fs = m_shadergen->GenerateBatchFragmentShader( + static_cast(render_mode), static_cast(lookup_mode), dithering, interlacing); + ComPtr fresh_blob = m_shader_cache.GetPixelShader(fs); + if (!fresh_blob) + { + Log_ErrorPrintf("Lazy batch fragment shader compile failed for (rm=%u, tm=%u, d=%u, i=%u)", render_mode, + texture_mode, static_cast(dithering), static_cast(interlacing)); + return {}; + } + + // Publish step: take the helper mutex briefly to write the + // canonical slot, the dup slot (for Reserved_* texture modes), + // and the fast-path atomics. The mutex window is microseconds - + // no D3DCompile call inside it. std::lock_guard lock(m_batch_shader_mutex); existing = fast_slot.load(std::memory_order_relaxed); @@ -1125,22 +1076,7 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetBatchFragmentShader(uint8_t rend if (!canonical_slot) { - if (!m_shadergen) - { - Log_ErrorPrint("GetBatchFragmentShader called before CompilePipelines constructed the shadergen"); - return {}; - } - const std::string fs = m_shadergen->GenerateBatchFragmentShader( - static_cast(render_mode), static_cast(lookup_mode), dithering, interlacing); - canonical_slot = m_shader_cache.GetPixelShader(fs); - if (!canonical_slot) - { - Log_ErrorPrintf("Lazy batch fragment shader compile failed for (rm=%u, tm=%u, d=%u, i=%u)", render_mode, - texture_mode, static_cast(dithering), static_cast(interlacing)); - return {}; - } - // Publish the canonical raw pointer so future fast-path readers - // for the canonical slot don't have to take the mutex. + canonical_slot = fresh_blob; m_batch_fragment_shader_blobs_fastpath[render_mode][lookup_mode][static_cast(dithering)][static_cast(interlacing)] .store(canonical_slot.Get(), std::memory_order_release); } @@ -1189,14 +1125,76 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetBatchPipeline(uint8_t return ret; } - // Slow path. The fragment-shader fetch has its own internal - // lock-acquire-release; we call it WITHOUT holding our own - // m_batch_shader_mutex so a sibling helper isn't deadlocked - // when it tries to take the same lock. + // Slow path. Compile the PSO WITHOUT m_batch_shader_mutex held. + // GetBatchFragmentShader is internally thread-safe and so is + // m_shader_cache.GetPipelineState (via the per-instance mutexes + // added inside D3D12::ShaderCache). The actual D3DCompile and + // CreateGraphicsPipelineState calls run lock-free, so the worker + // compiling slot A doesn't block the main thread compiling + // slot B. Two threads racing to compile the SAME slot is + // wasteful but harmless - both produce equivalent PSOs and the + // double-check below picks the winner. ComPtr fs_blob = GetBatchFragmentShader(render_mode, lookup_mode, dithering, interlacing); if (!fs_blob) return {}; + const bool textured = (static_cast(lookup_mode) != GPUTextureMode::Disabled); + + D3D12::GraphicsPipelineBuilder gpbuilder; + gpbuilder.SetRootSignature(m_batch_root_signature.Get()); + gpbuilder.SetRenderTarget(0, m_vram_texture.GetFormat()); + gpbuilder.SetDepthStencilFormat(m_vram_depth_texture.GetFormat()); + + gpbuilder.AddVertexAttribute("ATTR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, offsetof(BatchVertex, x)); + gpbuilder.AddVertexAttribute("ATTR", 1, DXGI_FORMAT_R8G8B8A8_UNORM, 0, offsetof(BatchVertex, color)); + if (textured) + { + gpbuilder.AddVertexAttribute("ATTR", 2, DXGI_FORMAT_R32_UINT, 0, offsetof(BatchVertex, u)); + gpbuilder.AddVertexAttribute("ATTR", 3, DXGI_FORMAT_R32_UINT, 0, offsetof(BatchVertex, texpage)); + if (m_using_uv_limits) + gpbuilder.AddVertexAttribute("ATTR", 4, DXGI_FORMAT_R8G8B8A8_UNORM, 0, offsetof(BatchVertex, uv_limits)); + } + + gpbuilder.SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE); + gpbuilder.SetVertexShader(m_batch_vertex_shader_blobs[static_cast(textured)].Get()); + gpbuilder.SetPixelShader(fs_blob.Get()); + + gpbuilder.SetRasterizationState(D3D12_FILL_MODE_SOLID, D3D12_CULL_MODE_NONE, false); + gpbuilder.SetDepthState(true, true, + (depth_test != 0) ? (m_pgxp_depth_buffer ? D3D12_COMPARISON_FUNC_LESS_EQUAL : + D3D12_COMPARISON_FUNC_GREATER_EQUAL) : + D3D12_COMPARISON_FUNC_ALWAYS); + gpbuilder.SetNoBlendingState(); + gpbuilder.SetMultisamples(m_multisamples); + + if ((static_cast(transparency_mode) != GPUTransparencyMode::Disabled && + (static_cast(render_mode) != BatchRenderMode::TransparencyDisabled && + static_cast(render_mode) != BatchRenderMode::OnlyOpaque)) || + m_texture_filtering != GPUTextureFilter::Nearest) + { + gpbuilder.SetBlendState( + 0, true, D3D12_BLEND_ONE, + m_supports_dual_source_blend ? D3D12_BLEND_SRC1_ALPHA : D3D12_BLEND_SRC_ALPHA, + (static_cast(transparency_mode) == GPUTransparencyMode::BackgroundMinusForeground && + static_cast(render_mode) != BatchRenderMode::TransparencyDisabled && + static_cast(render_mode) != BatchRenderMode::OnlyOpaque) ? + D3D12_BLEND_OP_REV_SUBTRACT : + D3D12_BLEND_OP_ADD, + D3D12_BLEND_ONE, D3D12_BLEND_ZERO, D3D12_BLEND_OP_ADD); + } + + ComPtr fresh_pso = gpbuilder.Create(g_d3d12_context->GetDevice(), m_shader_cache); + if (!fresh_pso) + { + Log_ErrorPrintf("Lazy batch PSO compile failed for (dt=%u, rm=%u, tm=%u, tr=%u, d=%u, i=%u)", depth_test, + render_mode, texture_mode, transparency_mode, static_cast(dithering), static_cast(interlacing)); + return {}; + } + + // Publish step: take the helper mutex briefly to write the + // canonical slot, the dup slot (for Reserved_* texture modes), + // and the fast-path atomics. The mutex window is microseconds - + // no D3DCompile or CreateGraphicsPipelineState inside it. std::lock_guard lock(m_batch_shader_mutex); // Double-check the fast slot under the lock. @@ -1215,63 +1213,7 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetBatchPipeline(uint8_t if (!canonical_slot) { - const bool textured = (static_cast(lookup_mode) != GPUTextureMode::Disabled); - - // Build a fresh local builder each fault-in so we don't carry - // state between invocations. The cost of building the descriptor - // is dwarfed by the actual PSO compile in m_shader_cache.GetPipelineState - // below. - D3D12::GraphicsPipelineBuilder gpbuilder; - gpbuilder.SetRootSignature(m_batch_root_signature.Get()); - gpbuilder.SetRenderTarget(0, m_vram_texture.GetFormat()); - gpbuilder.SetDepthStencilFormat(m_vram_depth_texture.GetFormat()); - - gpbuilder.AddVertexAttribute("ATTR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, offsetof(BatchVertex, x)); - gpbuilder.AddVertexAttribute("ATTR", 1, DXGI_FORMAT_R8G8B8A8_UNORM, 0, offsetof(BatchVertex, color)); - if (textured) - { - gpbuilder.AddVertexAttribute("ATTR", 2, DXGI_FORMAT_R32_UINT, 0, offsetof(BatchVertex, u)); - gpbuilder.AddVertexAttribute("ATTR", 3, DXGI_FORMAT_R32_UINT, 0, offsetof(BatchVertex, texpage)); - if (m_using_uv_limits) - gpbuilder.AddVertexAttribute("ATTR", 4, DXGI_FORMAT_R8G8B8A8_UNORM, 0, offsetof(BatchVertex, uv_limits)); - } - - gpbuilder.SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE); - gpbuilder.SetVertexShader(m_batch_vertex_shader_blobs[static_cast(textured)].Get()); - gpbuilder.SetPixelShader(fs_blob.Get()); - - gpbuilder.SetRasterizationState(D3D12_FILL_MODE_SOLID, D3D12_CULL_MODE_NONE, false); - gpbuilder.SetDepthState(true, true, - (depth_test != 0) ? (m_pgxp_depth_buffer ? D3D12_COMPARISON_FUNC_LESS_EQUAL : - D3D12_COMPARISON_FUNC_GREATER_EQUAL) : - D3D12_COMPARISON_FUNC_ALWAYS); - gpbuilder.SetNoBlendingState(); - gpbuilder.SetMultisamples(m_multisamples); - - if ((static_cast(transparency_mode) != GPUTransparencyMode::Disabled && - (static_cast(render_mode) != BatchRenderMode::TransparencyDisabled && - static_cast(render_mode) != BatchRenderMode::OnlyOpaque)) || - m_texture_filtering != GPUTextureFilter::Nearest) - { - gpbuilder.SetBlendState( - 0, true, D3D12_BLEND_ONE, - m_supports_dual_source_blend ? D3D12_BLEND_SRC1_ALPHA : D3D12_BLEND_SRC_ALPHA, - (static_cast(transparency_mode) == GPUTransparencyMode::BackgroundMinusForeground && - static_cast(render_mode) != BatchRenderMode::TransparencyDisabled && - static_cast(render_mode) != BatchRenderMode::OnlyOpaque) ? - D3D12_BLEND_OP_REV_SUBTRACT : - D3D12_BLEND_OP_ADD, - D3D12_BLEND_ONE, D3D12_BLEND_ZERO, D3D12_BLEND_OP_ADD); - } - - canonical_slot = gpbuilder.Create(g_d3d12_context->GetDevice(), m_shader_cache); - if (!canonical_slot) - { - Log_ErrorPrintf("Lazy batch PSO compile failed for (dt=%u, rm=%u, tm=%u, tr=%u, d=%u, i=%u)", depth_test, - render_mode, texture_mode, transparency_mode, static_cast(dithering), static_cast(interlacing)); - return {}; - } - + canonical_slot = fresh_pso; D3D12::SetObjectNameFormatted(canonical_slot.Get(), "Batch Pipeline %u,%u,%u,%u,%u,%u", depth_test, render_mode, lookup_mode, transparency_mode, static_cast(dithering), static_cast(interlacing)); @@ -1296,6 +1238,474 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetBatchPipeline(uint8_t return canonical_slot; } +// ---------------------------------------------------------------------- +// Non-batch lazy helpers. Same fast-path / slow-path layout as +// GetBatchPipeline: an acquire-load on an atomic raw-pointer fast +// path, falling back to the ComPtr slot under m_batch_shader_mutex +// 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. +// ---------------------------------------------------------------------- + +GPU_HW_D3D12::ComPtr 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; +} + +GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetVRAMFillPipeline(uint8_t wrapped, uint8_t interlaced) +{ + std::atomic& fast_slot = m_vram_fill_pipelines_fastpath[wrapped][interlaced]; + ID3D12PipelineState* existing = fast_slot.load(std::memory_order_acquire); + if (existing) + { + ComPtr ret; + ret.Attach(existing); + existing->AddRef(); + return ret; + } + + ComPtr vs = GetFullscreenQuadVertexShader(); + if (!vs) + return {}; + + if (!m_shadergen) + { + Log_ErrorPrint("GetVRAMFillPipeline called before CompilePipelines constructed the shadergen"); + return {}; + } + ComPtr fs = m_shader_cache.GetPixelShader( + m_shadergen->GenerateVRAMFillFragmentShader(static_cast(wrapped), static_cast(interlaced))); + if (!fs) + return {}; + + D3D12::GraphicsPipelineBuilder gpbuilder; + gpbuilder.SetRootSignature(m_single_sampler_root_signature.Get()); + gpbuilder.SetRenderTarget(0, m_vram_texture.GetFormat()); + gpbuilder.SetDepthStencilFormat(m_vram_depth_texture.GetFormat()); + gpbuilder.SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE); + gpbuilder.SetNoCullRasterizationState(); + gpbuilder.SetNoBlendingState(); + gpbuilder.SetVertexShader(vs.Get()); + gpbuilder.SetPixelShader(fs.Get()); + gpbuilder.SetMultisamples(m_multisamples); + gpbuilder.SetDepthState(true, true, D3D12_COMPARISON_FUNC_ALWAYS); + + ComPtr fresh = gpbuilder.Create(g_d3d12_context->GetDevice(), m_shader_cache, false); + if (!fresh) + return {}; + + // Publish under brief lock; double-check for race winner. + std::lock_guard lock(m_batch_shader_mutex); + existing = fast_slot.load(std::memory_order_relaxed); + if (existing) + { + ComPtr ret; + ret.Attach(existing); + existing->AddRef(); + return ret; + } + ComPtr& slot = m_vram_fill_pipelines[wrapped][interlaced]; + if (!slot) + { + slot = fresh; + D3D12::SetObjectNameFormatted(slot.Get(), "VRAM Fill Pipeline Wrapped=%u,Interlacing=%u", wrapped, interlaced); + } + fast_slot.store(slot.Get(), std::memory_order_release); + return slot; +} + +GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetVRAMCopyPipeline(uint8_t depth_test) +{ + std::atomic& fast_slot = m_vram_copy_pipelines_fastpath[depth_test]; + ID3D12PipelineState* existing = fast_slot.load(std::memory_order_acquire); + if (existing) + { + ComPtr ret; + ret.Attach(existing); + existing->AddRef(); + return ret; + } + + ComPtr vs = GetFullscreenQuadVertexShader(); + if (!vs) + return {}; + + if (!m_shadergen) + { + Log_ErrorPrint("GetVRAMCopyPipeline called before CompilePipelines constructed the shadergen"); + return {}; + } + ComPtr fs = m_shader_cache.GetPixelShader(m_shadergen->GenerateVRAMCopyFragmentShader()); + if (!fs) + return {}; + + D3D12::GraphicsPipelineBuilder gpbuilder; + gpbuilder.SetRootSignature(m_single_sampler_root_signature.Get()); + gpbuilder.SetRenderTarget(0, m_vram_texture.GetFormat()); + gpbuilder.SetDepthStencilFormat(m_vram_depth_texture.GetFormat()); + gpbuilder.SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE); + gpbuilder.SetNoCullRasterizationState(); + gpbuilder.SetNoBlendingState(); + gpbuilder.SetVertexShader(vs.Get()); + gpbuilder.SetPixelShader(fs.Get()); + gpbuilder.SetMultisamples(m_multisamples); + gpbuilder.SetDepthState((depth_test != 0), true, + (depth_test != 0) ? D3D12_COMPARISON_FUNC_GREATER_EQUAL : D3D12_COMPARISON_FUNC_ALWAYS); + + ComPtr fresh = gpbuilder.Create(g_d3d12_context->GetDevice(), m_shader_cache, false); + if (!fresh) + return {}; + + std::lock_guard lock(m_batch_shader_mutex); + existing = fast_slot.load(std::memory_order_relaxed); + if (existing) + { + ComPtr ret; + ret.Attach(existing); + existing->AddRef(); + return ret; + } + ComPtr& slot = m_vram_copy_pipelines[depth_test]; + if (!slot) + { + slot = fresh; + D3D12::SetObjectNameFormatted(slot.Get(), "VRAM Copy Pipeline Depth=%u", depth_test); + } + fast_slot.store(slot.Get(), std::memory_order_release); + return slot; +} + +GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetVRAMWritePipeline(uint8_t depth_test) +{ + std::atomic& fast_slot = m_vram_write_pipelines_fastpath[depth_test]; + ID3D12PipelineState* existing = fast_slot.load(std::memory_order_acquire); + if (existing) + { + ComPtr ret; + ret.Attach(existing); + existing->AddRef(); + return ret; + } + + ComPtr vs = GetFullscreenQuadVertexShader(); + if (!vs) + return {}; + + if (!m_shadergen) + { + Log_ErrorPrint("GetVRAMWritePipeline called before CompilePipelines constructed the shadergen"); + return {}; + } + ComPtr fs = m_shader_cache.GetPixelShader(m_shadergen->GenerateVRAMWriteFragmentShader(false)); + if (!fs) + return {}; + + D3D12::GraphicsPipelineBuilder gpbuilder; + gpbuilder.SetRootSignature(m_single_sampler_root_signature.Get()); + gpbuilder.SetRenderTarget(0, m_vram_texture.GetFormat()); + gpbuilder.SetDepthStencilFormat(m_vram_depth_texture.GetFormat()); + gpbuilder.SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE); + gpbuilder.SetNoCullRasterizationState(); + gpbuilder.SetNoBlendingState(); + gpbuilder.SetVertexShader(vs.Get()); + gpbuilder.SetPixelShader(fs.Get()); + gpbuilder.SetMultisamples(m_multisamples); + gpbuilder.SetDepthState(true, true, + (depth_test != 0) ? D3D12_COMPARISON_FUNC_GREATER_EQUAL : D3D12_COMPARISON_FUNC_ALWAYS); + + ComPtr fresh = gpbuilder.Create(g_d3d12_context->GetDevice(), m_shader_cache, false); + if (!fresh) + return {}; + + std::lock_guard lock(m_batch_shader_mutex); + existing = fast_slot.load(std::memory_order_relaxed); + if (existing) + { + ComPtr ret; + ret.Attach(existing); + existing->AddRef(); + return ret; + } + ComPtr& slot = m_vram_write_pipelines[depth_test]; + if (!slot) + { + slot = fresh; + D3D12::SetObjectNameFormatted(slot.Get(), "VRAM Write Pipeline Depth=%u", depth_test); + } + fast_slot.store(slot.Get(), std::memory_order_release); + return slot; +} + +GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetVRAMUpdateDepthPipeline() +{ + ID3D12PipelineState* existing = m_vram_update_depth_pipeline_fastpath.load(std::memory_order_acquire); + if (existing) + { + ComPtr ret; + ret.Attach(existing); + existing->AddRef(); + return ret; + } + + ComPtr vs = GetFullscreenQuadVertexShader(); + if (!vs) + return {}; + + if (!m_shadergen) + { + Log_ErrorPrint("GetVRAMUpdateDepthPipeline called before CompilePipelines constructed the shadergen"); + return {}; + } + ComPtr fs = m_shader_cache.GetPixelShader(m_shadergen->GenerateVRAMUpdateDepthFragmentShader()); + if (!fs) + return {}; + + // VRAM update depth differs from the other VRAM ops in three + // ways: it uses m_batch_root_signature (the regular ops use the + // single-sampler root sig), it writes to depth only (no render + // targets - ClearRenderTargets() drops the RT[0] entry the + // helper would otherwise inherit), and it carries an explicit + // blend state. Everything else - VS, topology, cull, multisamples, + // depth-stencil format, depth-state - matches the standard + // fullscreen-quad pipeline the other VRAM ops use. + D3D12::GraphicsPipelineBuilder gpbuilder; + gpbuilder.SetRootSignature(m_batch_root_signature.Get()); + gpbuilder.SetDepthStencilFormat(m_vram_depth_texture.GetFormat()); + gpbuilder.SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE); + gpbuilder.SetNoCullRasterizationState(); + gpbuilder.SetVertexShader(vs.Get()); + gpbuilder.SetPixelShader(fs.Get()); + gpbuilder.SetMultisamples(m_multisamples); + gpbuilder.SetDepthState(true, true, D3D12_COMPARISON_FUNC_ALWAYS); + gpbuilder.SetBlendState(0, false, D3D12_BLEND_ONE, D3D12_BLEND_ZERO, D3D12_BLEND_OP_ADD, D3D12_BLEND_ONE, + D3D12_BLEND_ZERO, D3D12_BLEND_OP_ADD, 0); + gpbuilder.ClearRenderTargets(); + + ComPtr fresh = gpbuilder.Create(g_d3d12_context->GetDevice(), m_shader_cache, false); + if (!fresh) + return {}; + + std::lock_guard lock(m_batch_shader_mutex); + existing = m_vram_update_depth_pipeline_fastpath.load(std::memory_order_relaxed); + if (existing) + { + ComPtr ret; + ret.Attach(existing); + existing->AddRef(); + return ret; + } + if (!m_vram_update_depth_pipeline) + { + m_vram_update_depth_pipeline = fresh; + D3D12::SetObjectName(m_vram_update_depth_pipeline.Get(), "VRAM Update Depth Pipeline"); + } + m_vram_update_depth_pipeline_fastpath.store(m_vram_update_depth_pipeline.Get(), std::memory_order_release); + return m_vram_update_depth_pipeline; +} + +GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetVRAMReadbackPipeline() +{ + ID3D12PipelineState* existing = m_vram_readback_pipeline_fastpath.load(std::memory_order_acquire); + if (existing) + { + ComPtr ret; + ret.Attach(existing); + existing->AddRef(); + return ret; + } + + ComPtr vs = GetFullscreenQuadVertexShader(); + if (!vs) + return {}; + + if (!m_shadergen) + { + Log_ErrorPrint("GetVRAMReadbackPipeline called before CompilePipelines constructed the shadergen"); + return {}; + } + ComPtr fs = m_shader_cache.GetPixelShader(m_shadergen->GenerateVRAMReadFragmentShader()); + if (!fs) + return {}; + + D3D12::GraphicsPipelineBuilder gpbuilder; + gpbuilder.SetRootSignature(m_single_sampler_root_signature.Get()); + gpbuilder.SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE); + gpbuilder.SetVertexShader(vs.Get()); + gpbuilder.SetPixelShader(fs.Get()); + gpbuilder.SetNoCullRasterizationState(); + gpbuilder.SetNoDepthTestState(); + gpbuilder.SetNoBlendingState(); + gpbuilder.SetRenderTarget(0, m_vram_readback_texture.GetFormat()); + gpbuilder.ClearDepthStencilFormat(); + + ComPtr fresh = gpbuilder.Create(g_d3d12_context->GetDevice(), m_shader_cache, false); + if (!fresh) + return {}; + + std::lock_guard lock(m_batch_shader_mutex); + existing = m_vram_readback_pipeline_fastpath.load(std::memory_order_relaxed); + if (existing) + { + ComPtr ret; + ret.Attach(existing); + existing->AddRef(); + return ret; + } + if (!m_vram_readback_pipeline) + { + m_vram_readback_pipeline = fresh; + D3D12::SetObjectName(m_vram_readback_pipeline.Get(), "VRAM Readback Pipeline"); + } + m_vram_readback_pipeline_fastpath.store(m_vram_readback_pipeline.Get(), std::memory_order_release); + return m_vram_readback_pipeline; +} + +GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetDisplayPipeline(uint8_t depth_24, uint8_t interlace_mode) +{ + std::atomic& fast_slot = m_display_pipelines_fastpath[depth_24][interlace_mode]; + ID3D12PipelineState* existing = fast_slot.load(std::memory_order_acquire); + if (existing) + { + ComPtr ret; + ret.Attach(existing); + existing->AddRef(); + return ret; + } + + ComPtr vs = GetFullscreenQuadVertexShader(); + if (!vs) + return {}; + + if (!m_shadergen) + { + Log_ErrorPrint("GetDisplayPipeline called before CompilePipelines constructed the shadergen"); + return {}; + } + ComPtr fs = m_shader_cache.GetPixelShader(m_shadergen->GenerateDisplayFragmentShader( + static_cast(depth_24), static_cast(interlace_mode), m_chroma_smoothing)); + if (!fs) + return {}; + + D3D12::GraphicsPipelineBuilder gpbuilder; + gpbuilder.SetRootSignature(m_single_sampler_root_signature.Get()); + gpbuilder.SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE); + gpbuilder.SetVertexShader(vs.Get()); + gpbuilder.SetPixelShader(fs.Get()); + gpbuilder.SetNoCullRasterizationState(); + gpbuilder.SetNoDepthTestState(); + gpbuilder.SetNoBlendingState(); + gpbuilder.SetRenderTarget(0, m_display_texture.GetFormat()); + + ComPtr fresh = gpbuilder.Create(g_d3d12_context->GetDevice(), m_shader_cache, false); + if (!fresh) + return {}; + + std::lock_guard lock(m_batch_shader_mutex); + existing = fast_slot.load(std::memory_order_relaxed); + if (existing) + { + ComPtr ret; + ret.Attach(existing); + existing->AddRef(); + return ret; + } + ComPtr& slot = m_display_pipelines[depth_24][interlace_mode]; + if (!slot) + { + slot = fresh; + D3D12::SetObjectNameFormatted(slot.Get(), "Display Pipeline Depth=%u Interlace=%u", depth_24, interlace_mode); + } + fast_slot.store(slot.Get(), std::memory_order_release); + return slot; +} + +GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetCopyPipeline() +{ + ID3D12PipelineState* existing = m_copy_pipeline_fastpath.load(std::memory_order_acquire); + if (existing) + { + ComPtr ret; + ret.Attach(existing); + existing->AddRef(); + return ret; + } + + ComPtr vs = GetFullscreenQuadVertexShader(); + if (!vs) + return {}; + + if (!m_shadergen) + { + Log_ErrorPrint("GetCopyPipeline called before CompilePipelines constructed the shadergen"); + return {}; + } + ComPtr fs = m_shader_cache.GetPixelShader(m_shadergen->GenerateCopyFragmentShader()); + if (!fs) + return {}; + + D3D12::GraphicsPipelineBuilder gpbuilder; + gpbuilder.SetRootSignature(m_single_sampler_root_signature.Get()); + gpbuilder.SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE); + gpbuilder.SetVertexShader(vs.Get()); + gpbuilder.SetPixelShader(fs.Get()); + gpbuilder.SetNoCullRasterizationState(); + gpbuilder.SetNoDepthTestState(); + gpbuilder.SetNoBlendingState(); + gpbuilder.SetRenderTarget(0, DXGI_FORMAT_R8G8B8A8_UNORM); + + ComPtr fresh = gpbuilder.Create(g_d3d12_context->GetDevice(), m_shader_cache, false); + if (!fresh) + return {}; + + std::lock_guard lock(m_batch_shader_mutex); + existing = m_copy_pipeline_fastpath.load(std::memory_order_relaxed); + if (existing) + { + ComPtr ret; + ret.Attach(existing); + existing->AddRef(); + return ret; + } + if (!m_copy_pipeline) + { + m_copy_pipeline = fresh; + D3D12::SetObjectName(m_copy_pipeline.Get(), "Copy/Blit Pipeline"); + } + m_copy_pipeline_fastpath.store(m_copy_pipeline.Get(), std::memory_order_release); + return m_copy_pipeline; +} + void GPU_HW_D3D12::DestroyPipelines() { // Tear down the background-compile worker before clearing the @@ -1315,6 +1725,20 @@ void GPU_HW_D3D12::DestroyPipelines() m_batch_fragment_shader_blobs_fastpath.enumerate( [](std::atomic& s) { s.store(nullptr, std::memory_order_relaxed); }); + // Same pattern for the non-batch fast-path mirrors added when + // these pipelines moved onto the lazy-fault path. + m_vram_fill_pipelines_fastpath.enumerate( + [](std::atomic& s) { s.store(nullptr, std::memory_order_relaxed); }); + for (auto& s : m_vram_copy_pipelines_fastpath) + s.store(nullptr, std::memory_order_relaxed); + for (auto& s : m_vram_write_pipelines_fastpath) + s.store(nullptr, std::memory_order_relaxed); + m_display_pipelines_fastpath.enumerate( + [](std::atomic& s) { s.store(nullptr, std::memory_order_relaxed); }); + m_vram_readback_pipeline_fastpath.store(nullptr, std::memory_order_relaxed); + m_vram_update_depth_pipeline_fastpath.store(nullptr, std::memory_order_relaxed); + m_copy_pipeline_fastpath.store(nullptr, std::memory_order_relaxed); + m_batch_pipelines = {}; m_vram_fill_pipelines = {}; m_vram_write_pipelines = {}; @@ -1326,6 +1750,15 @@ void GPU_HW_D3D12::DestroyPipelines() m_batch_vertex_shader_blobs = {}; m_display_pipelines = {}; + + // 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). + m_copy_pipeline.Reset(); + m_fullscreen_quad_vertex_shader.Reset(); } bool GPU_HW_D3D12::CreateTextureReplacementStreamBuffer() @@ -1380,7 +1813,10 @@ bool GPU_HW_D3D12::BlitVRAMReplacementTexture(const TextureReplacementTexture* t cmdlist->SetGraphicsRoot32BitConstants(0, sizeof(uniforms) / sizeof(uint32_t), uniforms, 0); cmdlist->SetGraphicsRootDescriptorTable(1, m_vram_write_replacement_texture.GetSRVDescriptor()); cmdlist->SetGraphicsRootDescriptorTable(2, m_linear_sampler.gpu_handle); - cmdlist->SetPipelineState(m_copy_pipeline.Get()); + ComPtr copy_pso = GetCopyPipeline(); + if (!copy_pso) + return false; + cmdlist->SetPipelineState(copy_pso.Get()); D3D12::SetViewportAndScissor(cmdlist, dst_x, dst_y, width, height); cmdlist->DrawInstanced(3, 1, 0, 0); RestoreGraphicsAPIState(); @@ -1480,8 +1916,11 @@ void GPU_HW_D3D12::UpdateDisplay() cmdlist->SetGraphicsRootSignature(m_single_sampler_root_signature.Get()); cmdlist->SetGraphicsRoot32BitConstants(0, sizeof(uniforms) / sizeof(uint32_t), uniforms, 0); cmdlist->SetGraphicsRootDescriptorTable(1, m_vram_texture.GetSRVDescriptor()); - cmdlist->SetPipelineState( - m_display_pipelines[static_cast(m_GPUSTAT.display_area_color_depth_24)][static_cast(interlaced)].Get()); + ComPtr display_pso = + GetDisplayPipeline(static_cast(m_GPUSTAT.display_area_color_depth_24), static_cast(interlaced)); + if (!display_pso) + return; + cmdlist->SetPipelineState(display_pso.Get()); D3D12::SetViewportAndScissor(cmdlist, 0, 0, scaled_display_width, scaled_display_height); cmdlist->DrawInstanced(3, 1, 0, 0); @@ -1520,7 +1959,10 @@ void GPU_HW_D3D12::ReadVRAM(uint32_t x, uint32_t y, uint32_t width, uint32_t hei cmdlist->SetGraphicsRootSignature(m_single_sampler_root_signature.Get()); cmdlist->SetGraphicsRoot32BitConstants(0, sizeof(uniforms) / sizeof(uint32_t), uniforms, 0); cmdlist->SetGraphicsRootDescriptorTable(1, m_vram_texture.GetSRVDescriptor()); - cmdlist->SetPipelineState(m_vram_readback_pipeline.Get()); + ComPtr readback_pso = GetVRAMReadbackPipeline(); + if (!readback_pso) + return; + cmdlist->SetPipelineState(readback_pso.Get()); D3D12::SetViewportAndScissor(cmdlist, 0, 0, encoded_width, encoded_height); cmdlist->DrawInstanced(3, 1, 0, 0); @@ -1566,9 +2008,12 @@ void GPU_HW_D3D12::FillVRAM(uint32_t x, uint32_t y, uint32_t width, uint32_t hei cmdlist->SetGraphicsRootSignature(m_single_sampler_root_signature.Get()); cmdlist->SetGraphicsRoot32BitConstants(0, sizeof(uniforms) / sizeof(uint32_t), &uniforms, 0); cmdlist->SetGraphicsRootDescriptorTable(1, g_d3d12_context->GetNullSRVDescriptor()); - cmdlist->SetPipelineState(m_vram_fill_pipelines[static_cast(IsVRAMFillOversized(x, y, width, height))] - [static_cast(IsInterlacedRenderingEnabled())] - .Get()); + ComPtr fill_pso = + GetVRAMFillPipeline(static_cast(IsVRAMFillOversized(x, y, width, height)), + static_cast(IsInterlacedRenderingEnabled())); + if (!fill_pso) + return; + cmdlist->SetPipelineState(fill_pso.Get()); const Common::Rectangle bounds(GetVRAMTransferBounds(x, y, width, height)); D3D12::SetViewportAndScissor(cmdlist, bounds.left * m_resolution_scale, bounds.top * m_resolution_scale, @@ -1617,7 +2062,10 @@ void GPU_HW_D3D12::UpdateVRAM(uint32_t x, uint32_t y, uint32_t width, uint32_t h cmdlist->SetGraphicsRootSignature(m_single_sampler_root_signature.Get()); cmdlist->SetGraphicsRoot32BitConstants(0, sizeof(uniforms) / sizeof(uint32_t), &uniforms, 0); cmdlist->SetGraphicsRootDescriptorTable(1, m_texture_stream_buffer_srv); - cmdlist->SetPipelineState(m_vram_write_pipelines[static_cast(check_mask)].Get()); + ComPtr write_pso = GetVRAMWritePipeline(static_cast(check_mask)); + if (!write_pso) + return; + cmdlist->SetPipelineState(write_pso.Get()); // the viewport should already be set to the full vram, so just adjust the scissor const Common::Rectangle scaled_bounds = bounds * m_resolution_scale; @@ -1649,7 +2097,10 @@ void GPU_HW_D3D12::CopyVRAM(uint32_t src_x, uint32_t src_y, uint32_t dst_x, uint cmdlist->SetGraphicsRootSignature(m_single_sampler_root_signature.Get()); cmdlist->SetGraphicsRoot32BitConstants(0, sizeof(uniforms) / sizeof(uint32_t), &uniforms, 0); cmdlist->SetGraphicsRootDescriptorTable(1, m_vram_read_texture.GetSRVDescriptor()); - cmdlist->SetPipelineState(m_vram_copy_pipelines[static_cast(m_GPUSTAT.check_mask_before_draw)].Get()); + ComPtr copy_pso = GetVRAMCopyPipeline(static_cast(m_GPUSTAT.check_mask_before_draw)); + if (!copy_pso) + return; + cmdlist->SetPipelineState(copy_pso.Get()); D3D12::SetViewportAndScissor(cmdlist, dst_bounds_scaled.left, dst_bounds_scaled.top, dst_bounds_scaled.GetWidth(), dst_bounds_scaled.GetHeight()); cmdlist->DrawInstanced(3, 1, 0, 0); @@ -1725,7 +2176,10 @@ void GPU_HW_D3D12::UpdateDepthBufferFromMaskBit() cmdlist->OMSetRenderTargets(0, nullptr, FALSE, &m_vram_depth_texture.GetRTVOrDSVDescriptor().cpu_handle); cmdlist->SetGraphicsRootDescriptorTable(1, m_vram_texture.GetSRVDescriptor()); - cmdlist->SetPipelineState(m_vram_update_depth_pipeline.Get()); + ComPtr update_depth_pso = GetVRAMUpdateDepthPipeline(); + if (!update_depth_pso) + return; + cmdlist->SetPipelineState(update_depth_pso.Get()); D3D12::SetViewportAndScissor(cmdlist, 0, 0, m_vram_texture.GetWidth(), m_vram_texture.GetHeight()); cmdlist->DrawInstanced(3, 1, 0, 0); diff --git a/src/core/gpu_hw_d3d12.h b/src/core/gpu_hw_d3d12.h index 3e134689..dc996d0a 100644 --- a/src/core/gpu_hw_d3d12.h +++ b/src/core/gpu_hw_d3d12.h @@ -148,6 +148,40 @@ private: ComPtr GetBatchPipeline(uint8_t depth_test, uint8_t render_mode, uint8_t texture_mode, uint8_t transparency_mode, bool dithering, bool interlacing); + // Lazy non-batch PSO compile path. + // + // The non-batch pipelines (VRAM fill / copy / write / update depth + // / readback, display, copy/blit) used to be compiled + // unconditionally at CompilePipelines time regardless of the + // gpu_shader_precompile_mode setting. Even with 'Disabled' the + // user would pay the cost of building 17 PSOs + the fullscreen + // quad vertex shader at GPU init - on a cold cache that is + // hundreds of ms of D3DCompile + driver PSO assembly. The + // 'Disabled' contract is meant to be "skip all compilation at + // init time, fault everything in on first use", so these helpers + // bring the non-batch pipelines under the same lazy-fault + // umbrella as the batch matrix. + // + // Each helper follows the same fast-path / slow-path layout as + // GetBatchPipeline: an acquire-load on an atomic raw-pointer + // fast-path array, falling back to the ComPtr slot under + // m_batch_shader_mutex on a miss. The atomic fast path means + // the runloop hot path - FillVRAM / CopyVRAM / UpdateVRAM / + // 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(); + ComPtr GetVRAMFillPipeline(uint8_t wrapped, uint8_t interlaced); + ComPtr GetVRAMCopyPipeline(uint8_t depth_test); + ComPtr GetVRAMWritePipeline(uint8_t depth_test); + ComPtr GetVRAMUpdateDepthPipeline(); + ComPtr GetVRAMReadbackPipeline(); + ComPtr GetDisplayPipeline(uint8_t depth_24, uint8_t interlace_mode); + ComPtr GetCopyPipeline(); + // Background-thread worker for 'Lazy' precompile mode: walks the // full PSO matrix in (depth_test, render_mode, transparency_mode, // texture_mode, dithering, interlacing) order and calls @@ -230,18 +264,33 @@ private: // [wrapped][interlaced] DimensionalArray, 2, 2> m_vram_fill_pipelines; + DimensionalArray, 2, 2> m_vram_fill_pipelines_fastpath{}; // [depth_test] std::array, 2> m_vram_write_pipelines; std::array, 2> m_vram_copy_pipelines; + std::array, 2> m_vram_write_pipelines_fastpath{}; + std::array, 2> m_vram_copy_pipelines_fastpath{}; ComPtr m_vram_readback_pipeline; ComPtr m_vram_update_depth_pipeline; + std::atomic m_vram_readback_pipeline_fastpath{nullptr}; + std::atomic m_vram_update_depth_pipeline_fastpath{nullptr}; // [depth_24][interlace_mode] DimensionalArray, 3, 2> m_display_pipelines; + DimensionalArray, 3, 2> m_display_pipelines_fastpath{}; 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; };