diff --git a/src/core/gpu_hw_d3d11.cpp b/src/core/gpu_hw_d3d11.cpp index 89340ff1..4030f576 100644 --- a/src/core/gpu_hw_d3d11.cpp +++ b/src/core/gpu_hw_d3d11.cpp @@ -1211,16 +1211,37 @@ ID3D11PixelShader* GPU_HW_D3D11::GetBatchPixelShader(u8 render_mode, u8 texture_ // Apply the Reserved_*Direct16Bit dedup at the matrix level. The // shader source for texture_mode 3 / 7 is byte-for-byte identical // to 2 / 6 after macro expansion; storing the same ComPtr in both - // slots is safe (refcounted). + // slots is safe (refcounted), and storing the same raw pointer in + // both atomic fast-path slots is safe because the ComPtr keeps the + // shader alive for the lifetime of the GPU backend. const u8 lookup_mode = (texture_mode == static_cast(GPUTextureMode::Reserved_Direct16Bit)) ? 2u : (texture_mode == static_cast(GPUTextureMode::Reserved_RawDirect16Bit)) ? 6u : texture_mode; + // Fast path: lock-free atomic acquire-load of the caller's slot. + // If it's filled (the worker reached it first, or an earlier + // main-thread fault-in did), we're done with no mutex / kernel + // call / contention against the worker. + std::atomic& fast_slot = + m_batch_pixel_shader_fastpath[render_mode][texture_mode][BoolToUInt8(dithering)][BoolToUInt8(interlacing)]; + ID3D11PixelShader* existing = fast_slot.load(std::memory_order_acquire); + if (existing) + return existing; + + // Slow path. Take the mutex; double-check the fast slot in case + // someone else just published while we were waiting; otherwise + // compile via m_shader_cache.GetPixelShader (mutex-protected + // because the cache's m_index unordered_map isn't thread-safe on + // insert), populate the canonical ComPtr slot, propagate to the + // dup slot if the request was for a Reserved_* texture mode, and + // publish the raw pointer back out through fast_slot via + // memory_order_release. std::lock_guard lock(m_batch_shader_mutex); - // If the dedup-redirected slot is already filled, propagate the - // ComPtr into the caller's slot (if it asked for a Reserved_ mode) - // and return the underlying pointer. + existing = fast_slot.load(std::memory_order_relaxed); + if (existing) + return existing; + ComPtr& canonical_slot = m_batch_pixel_shaders[render_mode][lookup_mode][BoolToUInt8(dithering)][BoolToUInt8(interlacing)]; @@ -1236,10 +1257,14 @@ ID3D11PixelShader* GPU_HW_D3D11::GetBatchPixelShader(u8 render_mode, u8 texture_ canonical_slot = m_shader_cache.GetPixelShader(m_device.Get(), ps); if (!canonical_slot) { - Log_ErrorPrintf("Lazy batch pixel shader compile failed for (rm=%u, tm=%u, d=%u, i=%u)", - render_mode, texture_mode, BoolToUInt8(dithering), BoolToUInt8(interlacing)); + Log_ErrorPrintf("Lazy batch pixel shader compile failed for (rm=%u, tm=%u, d=%u, i=%u)", render_mode, + texture_mode, BoolToUInt8(dithering), BoolToUInt8(interlacing)); return nullptr; } + // Publish the canonical raw pointer so future fast-path readers + // for the canonical slot don't have to take the mutex. + m_batch_pixel_shader_fastpath[render_mode][lookup_mode][BoolToUInt8(dithering)][BoolToUInt8(interlacing)].store( + canonical_slot.Get(), std::memory_order_release); } if (lookup_mode != texture_mode) @@ -1250,6 +1275,10 @@ ID3D11PixelShader* GPU_HW_D3D11::GetBatchPixelShader(u8 render_mode, u8 texture_ dup_slot = canonical_slot; } + // Publish the caller's slot. For the canonical case this is a + // redundant store relative to the one above; for the dup case + // this is what makes the dup slot fast-path-reachable. + fast_slot.store(canonical_slot.Get(), std::memory_order_release); return canonical_slot.Get(); } @@ -1274,6 +1303,20 @@ void GPU_HW_D3D11::DestroyShaders() m_copy_pixel_shader.Reset(); m_uv_quad_vertex_shader.Reset(); m_screen_quad_vertex_shader.Reset(); + + // Clear the atomic fast-path array BEFORE dropping the ComPtr + // ownership so a hypothetical concurrent reader couldn't see a + // raw pointer pointing to a just-freed shader. By this point the + // worker is stopped (StopShaderCompileThread above) and the + // runloop isn't drawing (UpdateSettings is the only call site that + // goes through DestroyShaders, and it's on the runloop thread + // itself), so memory_order_relaxed is sufficient. + for (auto& a : m_batch_pixel_shader_fastpath) + for (auto& b : a) + for (auto& c : b) + for (auto& slot : c) + slot.store(nullptr, std::memory_order_relaxed); + m_batch_pixel_shaders = {}; m_batch_vertex_shaders = {}; m_batch_input_layout.Reset(); diff --git a/src/core/gpu_hw_d3d11.h b/src/core/gpu_hw_d3d11.h index acd4b11a..150cfab9 100644 --- a/src/core/gpu_hw_d3d11.h +++ b/src/core/gpu_hw_d3d11.h @@ -140,18 +140,31 @@ private: bool CompileShaders(); void DestroyShaders(); - // Lazy batch-fragment-shader compile path. The shader array starts - // all-null; GetBatchPixelShader fills a slot on demand (the first - // time the game dispatches a draw using that combination) and - // returns a non-owning pointer to the underlying ID3D11PixelShader. - // It also handles the texture-mode dedup (3->2 and 7->6) inline so - // the duplicate slots never hit the cache. Synchronization between - // the main thread and the background precompile worker (when the - // 'Lazy' precompile mode is selected) is via m_batch_shader_mutex - // around the cache + array access; ID3D11PixelShader* itself is - // free-threaded for the consumer side (PSSetShader). The fast path - // is one mutex lock per DrawBatchVertices call, which on a modern - // libc is ~20 ns uncontended. + // Lazy batch-fragment-shader compile path. The shader matrix + // starts all-null; GetBatchPixelShader fills a slot on demand + // (the first time the game dispatches a draw using that + // combination) and returns a non-owning pointer to the underlying + // ID3D11PixelShader. It also handles the texture-mode dedup + // (3->2 and 7->6) inline so the duplicate slots share their + // canonical ComPtr. + // + // Two-level synchronisation: + // - FAST path (slot already filled): a single atomic + // acquire-load from m_batch_pixel_shader_fastpath. No mutex, + // no kernel call, no serialisation against the background + // precompile worker. This is what DrawBatchVertices hits in + // steady state. + // - SLOW path (slot null after the atomic load): take + // m_batch_shader_mutex, double-check the slot, compile via + // m_shader_cache.GetPixelShader (which mutates its + // unordered_map index and so requires external synchronisation), + // publish the ComPtr into m_batch_pixel_shaders, and then + // atomic-store the raw pointer into m_batch_pixel_shader_fastpath + // so future fast-path readers see it. + // + // ID3D11PixelShader* itself is free-threaded for the consumer + // side (PSSetShader), so DrawBatchVertices can use the raw + // pointer returned here without further locking. ID3D11PixelShader* GetBatchPixelShader(u8 render_mode, u8 texture_mode, bool dithering, bool interlacing); // Background-thread worker for 'Lazy' mode: walks the entire @@ -213,20 +226,49 @@ private: std::array, 5> m_batch_blend_states; // [transparency_mode] ComPtr m_batch_input_layout; std::array, 2> m_batch_vertex_shaders; // [textured] + + // Batch pixel shader matrix. The ComPtr array owns the + // reference; the parallel atomic-raw-pointer array exists so + // DrawBatchVertices can sample a slot without taking any lock. + // + // m_batch_pixel_shaders holds the COM ownership and is only + // written under m_batch_shader_mutex (in GetBatchPixelShader's + // slow path). The shader stays alive for the lifetime of this + // GPU backend (until DestroyShaders walks the ComPtr array), so + // the raw pointer published into m_batch_pixel_shader_fastpath + // is valid until then. + // + // The Reserved_*Direct16Bit dedup at the matrix level copies + // the ComPtr (sharing one shader across two slots) and copies + // the raw pointer too. SafeDestroy is via ComPtr-reset on the + // owning array; the atomic array is just a view. + // + // Without this split the fast path took the same mutex the + // background precompile worker holds during 50-200 ms HLSL + // compiles, so concurrent main-thread draws would stall behind + // the worker for one whole shader compile per draw. With the + // split, DrawBatchVertices is lock-free on cache-hit and the + // worker can compile in the background unmolested. std::array, 2>, 2>, 9>, 4> m_batch_pixel_shaders; // [render_mode][texture_mode][dithering][interlacing] + std::array, 2>, 2>, 9>, 4> + m_batch_pixel_shader_fastpath{}; // [render_mode][texture_mode][dithering][interlacing] // Persistent shader cache and shadergen instances for the lazy // / background-thread compile path. Both used to be locals in // CompileShaders(); now they live for the lifetime of this GPU // backend so GetBatchPixelShader can call into them at draw time. - // m_batch_shader_mutex serialises access to both the cache (its - // internal m_index unordered_map is not thread-safe on insert) and - // the m_batch_pixel_shaders array. The shadergen is stateless once - // constructed - all its parameters are baked in at construction - - // so reads under the mutex are safe; we re-construct it in - // CompileShaders whenever settings change and the matrix needs - // re-filling. + // m_batch_shader_mutex serialises the SLOW path of + // GetBatchPixelShader (cache mutation, ComPtr-array write, and + // the atomic-raw-pointer publish). The FAST path - + // DrawBatchVertices looking up an already-filled slot - reads + // m_batch_pixel_shader_fastpath with an atomic acquire-load and + // does not take this mutex; that's what keeps the runloop + // running while the background precompile worker is compiling + // other cells. The shadergen is stateless once constructed - all + // its parameters are baked in at construction - so reads under + // the mutex are safe; we re-construct it in CompileShaders + // whenever settings change and the matrix needs re-filling. std::mutex m_batch_shader_mutex; D3D11::ShaderCache m_shader_cache; std::unique_ptr m_shadergen; diff --git a/src/core/gpu_hw_d3d12.cpp b/src/core/gpu_hw_d3d12.cpp index 9cee993f..a00ec5f3 100644 --- a/src/core/gpu_hw_d3d12.cpp +++ b/src/core/gpu_hw_d3d12.cpp @@ -811,14 +811,43 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetBatchFragmentShader(u8 render_mo // matrix level. Two slots end up holding the same ComPtr; ID3DBlob // is refcounted so sharing the reference across slots is safe and // the destructor on DestroyPipelines just drops the refcount twice. + // The parallel _fastpath atomic array gets the same raw pointer + // in both slots, which is safe because the ComPtr keeps the blob + // alive for the lifetime of the GPU backend. const u8 lookup_mode = (texture_mode == static_cast(GPUTextureMode::Reserved_Direct16Bit)) ? 2u : (texture_mode == static_cast(GPUTextureMode::Reserved_RawDirect16Bit)) ? 6u : texture_mode; - // Caller is responsible for holding m_batch_shader_mutex - this - // helper is only reached from GetBatchPipeline (which takes the - // lock) or from CompilePipelines's Enabled-mode loop which is - // single-threaded. + // Fast path: lock-free acquire-load. Both the worker (via + // GetBatchPipeline -> here) and the main thread (same) can read + // a slot concurrently without serialising against each other or + // each other's slow-path compiles. + std::atomic& fast_slot = + m_batch_fragment_shader_blobs_fastpath[render_mode][texture_mode][BoolToUInt8(dithering)][BoolToUInt8(interlacing)]; + 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. + 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& canonical_slot = m_batch_fragment_shader_blobs[render_mode][lookup_mode][BoolToUInt8(dithering)][BoolToUInt8(interlacing)]; @@ -838,6 +867,10 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetBatchFragmentShader(u8 render_mo texture_mode, BoolToUInt8(dithering), BoolToUInt8(interlacing)); return {}; } + // Publish the canonical raw pointer so future fast-path readers + // for the canonical slot don't have to take the mutex. + m_batch_fragment_shader_blobs_fastpath[render_mode][lookup_mode][BoolToUInt8(dithering)][BoolToUInt8(interlacing)] + .store(canonical_slot.Get(), std::memory_order_release); } if (lookup_mode != texture_mode) @@ -848,6 +881,8 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetBatchFragmentShader(u8 render_mo dup_slot = canonical_slot; } + // Publish the caller's slot. + fast_slot.store(canonical_slot.Get(), std::memory_order_release); return canonical_slot; } @@ -859,24 +894,55 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetBatchPipeline(u8 dept // dependent input to the PSO is the bound fragment shader (which // is itself dedup'd in GetBatchFragmentShader), the resulting PSO // for the Reserved_* modes is bit-identical to the canonical mode - // and we can share the ComPtr across both slots. + // and we can share the ComPtr across both slots. The atomic + // _fastpath array gets the same raw pointer in both slots, kept + // alive by the ComPtr for the lifetime of the GPU backend. const u8 lookup_mode = (texture_mode == static_cast(GPUTextureMode::Reserved_Direct16Bit)) ? 2u : (texture_mode == static_cast(GPUTextureMode::Reserved_RawDirect16Bit)) ? 6u : texture_mode; + // Fast path: lock-free acquire-load. DrawBatchVertices hits this + // once a slot is filled (either by the precompile worker or by + // an earlier main-thread fault-in). No mutex, no contention + // against the worker. + std::atomic& fast_slot = + m_batch_pipelines_fastpath[depth_test][render_mode][texture_mode][transparency_mode][BoolToUInt8(dithering)] + [BoolToUInt8(interlacing)]; + ID3D12PipelineState* existing = fast_slot.load(std::memory_order_acquire); + if (existing) + { + ComPtr ret; + ret.Attach(existing); + existing->AddRef(); + 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. + ComPtr fs_blob = GetBatchFragmentShader(render_mode, lookup_mode, dithering, interlacing); + if (!fs_blob) + return {}; + std::lock_guard lock(m_batch_shader_mutex); + // Double-check the fast slot under the lock. + existing = fast_slot.load(std::memory_order_relaxed); + if (existing) + { + ComPtr ret; + ret.Attach(existing); + existing->AddRef(); + return ret; + } + ComPtr& canonical_slot = m_batch_pipelines[depth_test][render_mode][lookup_mode][transparency_mode][BoolToUInt8(dithering)] [BoolToUInt8(interlacing)]; if (!canonical_slot) { - // Need the bound fragment shader first. - ComPtr fs_blob = GetBatchFragmentShader(render_mode, lookup_mode, dithering, interlacing); - if (!fs_blob) - return {}; - const bool textured = (static_cast(lookup_mode) != GPUTextureMode::Disabled); // Build a fresh local builder each fault-in so we don't carry @@ -936,6 +1002,12 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetBatchPipeline(u8 dept D3D12::SetObjectNameFormatted(canonical_slot.Get(), "Batch Pipeline %u,%u,%u,%u,%u,%u", depth_test, render_mode, lookup_mode, transparency_mode, BoolToUInt8(dithering), BoolToUInt8(interlacing)); + + // Publish the canonical raw pointer for future fast-path + // readers of the canonical slot. + m_batch_pipelines_fastpath[depth_test][render_mode][lookup_mode][transparency_mode][BoolToUInt8(dithering)] + [BoolToUInt8(interlacing)] + .store(canonical_slot.Get(), std::memory_order_release); } if (lookup_mode != texture_mode) @@ -947,6 +1019,8 @@ GPU_HW_D3D12::ComPtr GPU_HW_D3D12::GetBatchPipeline(u8 dept dup_slot = canonical_slot; } + // Publish the caller's slot. + fast_slot.store(canonical_slot.Get(), std::memory_order_release); return canonical_slot; } @@ -958,6 +1032,17 @@ void GPU_HW_D3D12::DestroyPipelines() StopShaderCompileThread(); m_shadergen.reset(); + // Clear the atomic fast-path views BEFORE dropping the ComPtr + // ownership so a hypothetical concurrent reader couldn't see a + // raw pointer pointing to a just-freed object. By this point the + // worker is stopped and the runloop isn't drawing (UpdateSettings + // is the only call site that runs DestroyPipelines, and it's on + // the runloop thread), so memory_order_relaxed is sufficient. + m_batch_pipelines_fastpath.enumerate( + [](std::atomic& s) { s.store(nullptr, std::memory_order_relaxed); }); + m_batch_fragment_shader_blobs_fastpath.enumerate( + [](std::atomic& s) { s.store(nullptr, std::memory_order_relaxed); }); + m_batch_pipelines = {}; m_vram_fill_pipelines = {}; m_vram_write_pipelines = {}; diff --git a/src/core/gpu_hw_d3d12.h b/src/core/gpu_hw_d3d12.h index 208b747e..518e918f 100644 --- a/src/core/gpu_hw_d3d12.h +++ b/src/core/gpu_hw_d3d12.h @@ -126,20 +126,29 @@ private: u32 m_current_uniform_buffer_offset = 0; + // Batch PSO matrix. The ComPtr array owns the reference; the + // parallel atomic-raw-pointer array exists so DrawBatchVertices + // can sample a slot without taking any lock. Same split as the + // D3D11 batch pixel shader matrix: m_batch_pipelines holds the + // COM ownership and is only written under m_batch_shader_mutex + // (in GetBatchPipeline's slow path); m_batch_pipelines_fastpath + // is the atomic view the runloop reads on the fast path. + // // [depth_test][render_mode][texture_mode][transparency_mode][dithering][interlacing] DimensionalArray, 2, 2, 5, 9, 4, 2> m_batch_pipelines; + DimensionalArray, 2, 2, 5, 9, 4, 2> m_batch_pipelines_fastpath{}; - // Persistent shader-cache + shadergen instances for the lazy and - // background-thread compile paths. Both used to be locals in - // CompilePipelines(); now they live for the lifetime of this GPU - // backend so the lazy helpers can call into them at draw time. - // The mutex serialises access to the cache (its index maps are - // not thread-safe on insert), to m_batch_vertex_shader_blobs / - // m_batch_fragment_shader_blobs (the shader-bytecode caches used - // both at precompile and at lazy PSO build time), and to - // m_batch_pipelines itself. m_shadergen is functionally - // stateless once constructed but we still pin its lifetime here - // because GetBatchFragmentShader calls into it under the lock. + // m_batch_shader_mutex serialises the SLOW path of the lazy + // helpers (cache mutation, ComPtr-array write, atomic-raw-pointer + // publish). The FAST path - DrawBatchVertices looking up an + // already-filled PSO slot, or GetBatchPipeline looking up an + // already-compiled fragment shader - reads the corresponding + // _fastpath atomic array with an acquire-load and does not take + // this mutex. That decoupling is what keeps the runloop running + // while the background precompile worker is compiling other + // cells. m_shadergen is functionally stateless once constructed + // but we still pin its lifetime here because the helpers' slow + // path calls into it under the lock. std::mutex m_batch_shader_mutex; D3D12::ShaderCache m_shader_cache; std::unique_ptr m_shadergen; @@ -156,8 +165,13 @@ private: // (render, texture, dithering, interlacing) only - the // depth/transparency dimensions of m_batch_pipelines don't enter // the fragment shader source. + // + // Same fast/slow-path split as m_batch_pipelines above: + // m_batch_fragment_shader_blobs owns the ComPtr; the parallel + // _fastpath array is the lock-free view. DimensionalArray, 2> m_batch_vertex_shader_blobs; // [textured] DimensionalArray, 2, 2, 9, 4> m_batch_fragment_shader_blobs; // [render][texture][dither][interlace] + DimensionalArray, 2, 2, 9, 4> m_batch_fragment_shader_blobs_fastpath{}; // [wrapped][interlaced] DimensionalArray, 2, 2> m_vram_fill_pipelines;