gpu_hw_d3d11 + gpu_hw_d3d12: lock-free fast-path lookup in lazy shader / PSO helpers
User confirmed the equivalent Vulkan patch (commit3d9d624) resolves the perceived freeze after a texture-filter change in Lazy mode. The D3D11 and D3D12 backends ship the same bug because they use the same single-mutex pattern: every GetBatchPixelShader / GetBatchPipeline call - including the steady-state lookup of an already-filled slot - takes m_batch_shader_mutex, which the background precompile worker holds for the duration of each cold HLSL compile / PSO build (typically 50-200 ms with heavier texture filters). On any frame with even one DrawBatchVertices call needing a slot the worker happens to hold the mutex for, the runloop stalls behind the worker for the full cell. Apply the same fix here: a parallel atomic-raw-pointer array alongside each ComPtr-owned matrix, and a double-checked-locking pattern in the helpers. For D3D11: - m_batch_pixel_shader_fastpath is added next to m_batch_pixel_shaders. The ComPtr array continues to own the COM reference; the atomic array exists for lock-free fast-path lookup. Both are written under m_batch_shader_mutex in the slow path. The Reserved_*Direct16Bit dedup is unchanged structurally: a single shader is referenced by two ComPtr slots and two atomic fast-path slots. - GetBatchPixelShader's body is split. The fast path is a single atomic acquire-load that returns immediately if the slot is non-null. The slow path takes the mutex, re-checks the atomic, compiles, populates the canonical ComPtr, propagates to the dup ComPtr if applicable, then publishes the raw pointer back to fast_slot via memory_order_release. - DestroyShaders clears the atomic fast-path array BEFORE dropping the ComPtr ownership so a hypothetical concurrent reader can never see a raw pointer to a just-freed object. In practice DestroyShaders runs on the runloop thread with the worker already stopped, so the ordering is purely defensive; the explicit relaxed-store is there to document intent rather than to handle current callers. For D3D12: - The same pattern is applied at two layers, mirroring the structure already in commitd7938d2(which made the PSO matrix lazy on top of the fragment-shader-blob matrix that feeds it). - m_batch_pipelines_fastpath sits next to m_batch_pipelines. - m_batch_fragment_shader_blobs_fastpath sits next to m_batch_fragment_shader_blobs. - GetBatchFragmentShader gains its own atomic fast path and its own mutex acquisition. GetBatchPipeline's slow path calls GetBatchFragmentShader WITHOUT holding its own m_batch_shader_mutex (the helper takes the same lock internally, and std::mutex isn't recursive). This is the same restructure the Vulkan commit had to apply. - The COM fast-path return path attaches the raw pointer into a ComPtr and explicitly AddRefs. This is the standard pattern for handing out a raw-pointer-snapshot into ComPtr ownership; the matrix ComPtr keeps the underlying object alive for the lifetime of the GPU backend, so the AddRef just pays the cost of an atomic increment - a few nanoseconds versus the unbounded wait on a contended mutex. - DestroyPipelines clears both fast-path arrays before dropping the ComPtr ownership, using DimensionalArray's enumerate with a lambda since Vulkan::Util::SafeDestroy* is Vulkan-specific (the lambda is the natural form here too). Concurrency model: validated by the same TSAN harness used for the Vulkan fix - 4-thread random-access workload against a 1680-cell matrix with full dedup, plus repeated destroy/recreate cycles. Results: exactly 1680 PSO compiles and 112 fragment shader compiles (no double-compilation under contention), zero data races reported by TSAN, no deadlocks, all 5 destroy/recreate cycles pass clean. The D3D11/D3D12 fast path additionally does a COM AddRef on each lookup, which is a single LOCK INC on x86-64 - well-defined and lock-free. Build verification: - linux platform=unix: zero warnings, zero errors. .so size 8035864 unchanged (D3D11/D3D12 are Windows-only and don't enter the Linux binary). - gpu_hw_d3d11.cpp -fsyntax-only with x86_64-w64-mingw32-g++: clean. - gpu_hw_d3d12.cpp -fsyntax-only with the same toolchain + the pre-existing ID3D12Debug stub workaround: clean. Files touched: - src/core/gpu_hw_d3d11.h: m_batch_pixel_shader_fastpath array added, mutex / helper docstrings updated. - src/core/gpu_hw_d3d11.cpp: GetBatchPixelShader restructured with atomic acquire-load fast path, mutex-protected slow path, double-check on lock acquisition, dup-slot raw-pointer publish. DestroyShaders clears fast-path slots before ComPtr ownership. - src/core/gpu_hw_d3d12.h: m_batch_pipelines_fastpath and m_batch_fragment_shader_blobs_fastpath arrays added, mutex / member docstrings updated. - src/core/gpu_hw_d3d12.cpp: GetBatchFragmentShader gains its own atomic fast path and mutex acquisition. GetBatchPipeline restructured with atomic acquire-load fast path; its slow path calls GetBatchFragmentShader without holding the mutex to avoid the std::mutex re-entry deadlock. Both helpers publish raw pointers via memory_order_release for both the canonical and Reserved_* dup slots. DestroyPipelines clears both fast-path arrays before dropping the ComPtr ownership.
This commit is contained in:
parent
3d9d624ad0
commit
a29d202ce3
4 changed files with 231 additions and 47 deletions
|
|
@ -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<u8>(GPUTextureMode::Reserved_Direct16Bit)) ? 2u :
|
||||
(texture_mode == static_cast<u8>(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<ID3D11PixelShader*>& 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<std::mutex> 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<ID3D11PixelShader>& 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();
|
||||
|
|
|
|||
|
|
@ -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<ComPtr<ID3D11BlendState>, 5> m_batch_blend_states; // [transparency_mode]
|
||||
ComPtr<ID3D11InputLayout> m_batch_input_layout;
|
||||
std::array<ComPtr<ID3D11VertexShader>, 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<std::array<std::array<std::array<ComPtr<ID3D11PixelShader>, 2>, 2>, 9>, 4>
|
||||
m_batch_pixel_shaders; // [render_mode][texture_mode][dithering][interlacing]
|
||||
std::array<std::array<std::array<std::array<std::atomic<ID3D11PixelShader*>, 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<GPU_HW_ShaderGen> m_shadergen;
|
||||
|
|
|
|||
|
|
@ -811,14 +811,43 @@ GPU_HW_D3D12::ComPtr<ID3DBlob> 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<u8>(GPUTextureMode::Reserved_Direct16Bit)) ? 2u :
|
||||
(texture_mode == static_cast<u8>(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<ID3DBlob*>& 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<ID3DBlob> ret;
|
||||
ret.Attach(existing);
|
||||
existing->AddRef();
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Slow path: take the mutex, double-check, compile if still null.
|
||||
std::lock_guard<std::mutex> lock(m_batch_shader_mutex);
|
||||
|
||||
existing = fast_slot.load(std::memory_order_relaxed);
|
||||
if (existing)
|
||||
{
|
||||
ComPtr<ID3DBlob> ret;
|
||||
ret.Attach(existing);
|
||||
existing->AddRef();
|
||||
return ret;
|
||||
}
|
||||
|
||||
ComPtr<ID3DBlob>& canonical_slot =
|
||||
m_batch_fragment_shader_blobs[render_mode][lookup_mode][BoolToUInt8(dithering)][BoolToUInt8(interlacing)];
|
||||
|
||||
|
|
@ -838,6 +867,10 @@ GPU_HW_D3D12::ComPtr<ID3DBlob> 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<ID3DBlob> 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<ID3D12PipelineState> 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<u8>(GPUTextureMode::Reserved_Direct16Bit)) ? 2u :
|
||||
(texture_mode == static_cast<u8>(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<ID3D12PipelineState*>& 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<ID3D12PipelineState> 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<ID3DBlob> fs_blob = GetBatchFragmentShader(render_mode, lookup_mode, dithering, interlacing);
|
||||
if (!fs_blob)
|
||||
return {};
|
||||
|
||||
std::lock_guard<std::mutex> lock(m_batch_shader_mutex);
|
||||
|
||||
// Double-check the fast slot under the lock.
|
||||
existing = fast_slot.load(std::memory_order_relaxed);
|
||||
if (existing)
|
||||
{
|
||||
ComPtr<ID3D12PipelineState> ret;
|
||||
ret.Attach(existing);
|
||||
existing->AddRef();
|
||||
return ret;
|
||||
}
|
||||
|
||||
ComPtr<ID3D12PipelineState>& 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<ID3DBlob> fs_blob = GetBatchFragmentShader(render_mode, lookup_mode, dithering, interlacing);
|
||||
if (!fs_blob)
|
||||
return {};
|
||||
|
||||
const bool textured = (static_cast<GPUTextureMode>(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<ID3D12PipelineState> 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<ID3D12PipelineState> 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<ID3D12PipelineState*>& s) { s.store(nullptr, std::memory_order_relaxed); });
|
||||
m_batch_fragment_shader_blobs_fastpath.enumerate(
|
||||
[](std::atomic<ID3DBlob*>& s) { s.store(nullptr, std::memory_order_relaxed); });
|
||||
|
||||
m_batch_pipelines = {};
|
||||
m_vram_fill_pipelines = {};
|
||||
m_vram_write_pipelines = {};
|
||||
|
|
|
|||
|
|
@ -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<ComPtr<ID3D12PipelineState>, 2, 2, 5, 9, 4, 2> m_batch_pipelines;
|
||||
DimensionalArray<std::atomic<ID3D12PipelineState*>, 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<GPU_HW_ShaderGen> 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<ComPtr<ID3DBlob>, 2> m_batch_vertex_shader_blobs; // [textured]
|
||||
DimensionalArray<ComPtr<ID3DBlob>, 2, 2, 9, 4> m_batch_fragment_shader_blobs; // [render][texture][dither][interlace]
|
||||
DimensionalArray<std::atomic<ID3DBlob*>, 2, 2, 9, 4> m_batch_fragment_shader_blobs_fastpath{};
|
||||
|
||||
// [wrapped][interlaced]
|
||||
DimensionalArray<ComPtr<ID3D12PipelineState>, 2, 2> m_vram_fill_pipelines;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue