gpu_hw_d3d11: introduce three-mode shader precompile core option
Switching the texture filter from Nearest to JINC2 / xBR / Bilinear
in the D3D11 backend currently freezes RetroArch for multiple
seconds: GPU_HW_D3D11::UpdateSettings -> DestroyShaders ->
CompileShaders rebuilds the entire batch fragment shader matrix
(4 render modes x 9 texture modes x 2 dithering x 2 interlacing =
144 permutations, 112 of them unique) synchronously on the runloop
thread. With high-optimisation HLSL compiles at 50-200 ms per
shader the total can run 10-20 seconds; the previously-shipped
RETRO_MESSAGE_TYPE_PROGRESS notification is unreadable against
the RetroArch menu UI during the stall, so users perceive a hang.
This adds a 'swanstation_GPU_ShaderPrecompile' core option with
three values, defaulting to 'Lazy', and threads its behaviour
through the D3D11 backend (other backends follow in subsequent
commits):
- Disabled: CompileShaders skips the batch fragment shader matrix
entirely. Each combination compiles on the main thread the
first time the game dispatches a draw using it, then is
cached. Lowest startup latency; trades the single multi-second
stall for a small number of first-use hitches scattered
through early gameplay. Useful on low-end hardware and for
users who don't intend to use the heavier filters.
- Enabled: the historical behaviour. CompileShaders walks the
full matrix synchronously at GPU init. RetroArch is blocked
the entire time. After that, no per-draw hitches. Useful for
benchmarking and for users who want a single up-front cost.
- Lazy (default): CompileShaders spawns a background thread
that walks the matrix and fills it slot-by-slot. The main
thread starts gameplay immediately. If the game asks for a
combo the worker hasn't reached yet, the main thread compiles
it itself (same hitch as Disabled for that one combo);
otherwise the slot is already populated and the draw proceeds
at full speed. After the worker finishes, behaviour is
identical to Enabled.
A new GetBatchPixelShader(render_mode, texture_mode, dithering,
interlacing) helper centralises the lazy-compile path. It:
- Applies the Reserved_Direct16Bit (3) -> Direct16Bit (2) and
Reserved_RawDirect16Bit (7) -> RawDirect16Bit (6) dedup
discovered in commit 8e7f7a3, by routing the lookup through a
canonical slot and ComPtr-copying the result into the
reserved-variant slot the first time it's accessed.
- Takes m_batch_shader_mutex around both the cache mutation
(D3D11::ShaderCache::m_index is an unordered_map and is not
thread-safe on insert) and the m_batch_pixel_shaders array
write. The fast path is one uncontended mutex lock per
DrawBatchVertices call; on a modern std::mutex impl that's
roughly 20 ns of overhead.
- Returns a non-owning ID3D11PixelShader* (consumer side is
free-threaded, so PSSetShader from the runloop is safe even
while the worker thread is still publishing future entries).
DrawBatchVertices routes its m_batch_pixel_shaders lookup through
GetBatchPixelShader so all three precompile modes use the same
fault-in path; the difference is purely whether anyone called
GetBatchPixelShader before the draw did.
D3D11::ShaderCache and GPU_HW_ShaderGen lifetimes change to match
the GPU backend's lifetime instead of being locals in
CompileShaders. The shader cache now lives in m_shader_cache; the
shader generator lives in m_shadergen (a unique_ptr because the
class is non-default-constructible). On UpdateSettings round-trips
through DestroyShaders/CompileShaders, the shader cache instance
persists (Open is guarded by a new D3D11::ShaderCache::IsOpen()
accessor), so the in-memory cache index isn't re-read from disk
each time. The shadergen is rebuilt because its baked-in settings
may have changed.
The background-thread worker walks the matrix in
(render, texture, dithering, interlacing) order, calling
GetBatchPixelShader on each slot. The double-checked-locking
pattern means the main thread can race ahead and pre-fill any
slot it needs at draw time; the worker just observes the filled
slot under the lock and skips its own compile.
DestroyShaders signals m_shader_compile_thread_quit before
joining the worker so teardown latency is bounded by one
in-flight shader compile (up to ~200 ms in pathological cases).
The worker checks the flag between every cell. Re-entry safety:
CompileShaders calls StopShaderCompileThread at the top so any
worker from a prior UpdateSettings round-trip is joined before a
new matrix fill begins.
A standalone TSAN test exercising the same concurrency model
(double-checked lock + dedup + race against random main-thread
fault-ins) confirmed: exactly 112 unique compiles, 32 dedup'd
slots, no data races, no double-init, clean early-quit termination,
clean 5-cycle teardown/recreate.
For 'Lazy' mode the ShaderCompileProgressTracker is given a total
of 0 for the batch matrix dimension (the worker doesn't update it),
so the progress widget shipped in commit 1089822 only reports the
~12 non-batch shaders during init - a sub-second blip rather than
the previous multi-second crawl. The total wall-clock cost is the
same; it's just moved off the runloop.
The other three backends (D3D12, OpenGL, Vulkan) still take the
historical Enabled-style path regardless of the user's choice,
because the core option doesn't reach their CompileShaders yet.
Follow-up commits will extend this. OpenGL is harder because the
GL context can't be shared with a worker thread under the libretro
hardware-renderer protocol - the Lazy option will degrade to
Disabled there (compile-on-first-use on the main thread, no
background work).
Build: zero warnings, zero errors on linux platform=unix (which
doesn't compile gpu_hw_d3d11.cpp - guarded ifeq IS_WINDOWS=1).
gpu_hw_d3d11.cpp and common/d3d11/shader_cache.cpp both
cross-compile cleanly with x86_64-w64-mingw32-g++. .so size
8030576 (+2336 vs master); the growth is GetBatchPixelShader,
the worker entry point, and the thread/mutex member machinery.
25 retro_* exports unchanged.
Files touched:
- src/core/types.h: new GPUShaderPrecompileMode enum
- src/core/settings.{h,cpp}: gpu_shader_precompile_mode field,
Parse/Get helpers, Load wiring
- src/libretro/libretro_core_options.h: swanstation_GPU_ShaderPrecompile
- src/common/d3d11/shader_cache.h: IsOpen() accessor
- src/core/gpu_hw_d3d11.{h,cpp}: persistent shader cache and
shadergen members, GetBatchPixelShader, worker thread
machinery, three-way CompileShaders branch, DrawBatchVertices
routes through the helper, DestroyShaders joins the worker.
This commit is contained in:
parent
8e7f7a30cb
commit
f188bb0ee2
7 changed files with 322 additions and 50 deletions
|
|
@ -24,6 +24,13 @@ public:
|
|||
|
||||
void Open(std::string_view base_path, D3D_FEATURE_LEVEL feature_level, u32 version, bool debug);
|
||||
|
||||
// Returns whether Open() has already been called successfully on
|
||||
// this instance. Used by the persistent (lazy-compile) GPU backend
|
||||
// to avoid re-reading the same on-disk index on UpdateSettings
|
||||
// round-trips through DestroyShaders/CompileShaders, which would
|
||||
// both leak file handles and double-count m_index entries.
|
||||
bool IsOpen() const { return m_index_file != nullptr; }
|
||||
|
||||
ComPtr<ID3DBlob> GetShaderBlob(ShaderCompiler::Type type, std::string_view shader_code);
|
||||
|
||||
ComPtr<ID3D11VertexShader> GetVertexShader(ID3D11Device* device, std::string_view shader_code);
|
||||
|
|
|
|||
|
|
@ -928,16 +928,48 @@ void GPU_HW_D3D11::DestroyStateObjects()
|
|||
|
||||
bool GPU_HW_D3D11::CompileShaders()
|
||||
{
|
||||
D3D11::ShaderCache shader_cache;
|
||||
shader_cache.Open(g_host_interface->GetShaderCacheBasePath(), m_device->GetFeatureLevel(), SHADER_CACHE_VERSION,
|
||||
false);
|
||||
// Make sure no previous background-compile worker is still alive
|
||||
// from a prior CompileShaders call (UpdateSettings triggers
|
||||
// DestroyShaders -> CompileShaders, and the worker from the
|
||||
// previous incarnation has to be joined before we start a new one
|
||||
// so it doesn't keep writing into the matrix the new run is about
|
||||
// to fill in).
|
||||
StopShaderCompileThread();
|
||||
|
||||
GPU_HW_ShaderGen shadergen(m_host_display->GetRenderAPI(), m_resolution_scale, m_multisamples, m_per_sample_shading,
|
||||
m_true_color, m_scaled_dithering, m_texture_filtering, m_using_uv_limits,
|
||||
m_pgxp_depth_buffer, m_disable_color_perspective, m_supports_dual_source_blend);
|
||||
// Open the disk-backed shader cache the first time we get here; on
|
||||
// subsequent calls (UpdateSettings -> DestroyShaders ->
|
||||
// CompileShaders) the cache instance still holds the index from
|
||||
// last time and the underlying disk file hasn't moved, so we don't
|
||||
// want to re-read it. Re-opening would double-count every entry in
|
||||
// m_index and leak the previous RFILE* handles, since
|
||||
// ShaderCache::Open has no guard of its own.
|
||||
if (!m_shader_cache.IsOpen())
|
||||
{
|
||||
m_shader_cache.Open(g_host_interface->GetShaderCacheBasePath(), m_device->GetFeatureLevel(), SHADER_CACHE_VERSION,
|
||||
false);
|
||||
}
|
||||
// Convenience local reference so the rest of this function reads
|
||||
// the same way it always has. The lazy-compile helpers below also
|
||||
// touch m_shader_cache via the member.
|
||||
D3D11::ShaderCache& shader_cache = m_shader_cache;
|
||||
|
||||
m_shadergen = std::make_unique<GPU_HW_ShaderGen>(
|
||||
m_host_display->GetRenderAPI(), m_resolution_scale, m_multisamples, m_per_sample_shading, m_true_color,
|
||||
m_scaled_dithering, m_texture_filtering, m_using_uv_limits, m_pgxp_depth_buffer, m_disable_color_perspective,
|
||||
m_supports_dual_source_blend);
|
||||
GPU_HW_ShaderGen& shadergen = *m_shadergen;
|
||||
|
||||
// Whether to walk the full batch-fragment-shader matrix
|
||||
// synchronously from this thread, hand it to a background thread,
|
||||
// or skip it entirely. See the comment on GPUShaderPrecompileMode
|
||||
// in core/types.h.
|
||||
const GPUShaderPrecompileMode precompile_mode = g_settings.gpu_shader_precompile_mode;
|
||||
const bool precompile_sync = (precompile_mode == GPUShaderPrecompileMode::Enabled);
|
||||
const u32 batch_progress_units =
|
||||
(precompile_mode == GPUShaderPrecompileMode::Enabled) ? static_cast<u32>(4 * 9 * 2 * 2) : 0u;
|
||||
|
||||
ShaderCompileProgressTracker progress("Compiling Shaders",
|
||||
1 + 1 + 2 + (4 * 9 * 2 * 2) + 1 + (2 * 2) + 4 + (2 * 3) + 1);
|
||||
1 + 1 + 2 + batch_progress_units + 1 + (2 * 2) + 4 + (2 * 3) + 1);
|
||||
|
||||
// input layout
|
||||
{
|
||||
|
|
@ -985,55 +1017,48 @@ bool GPU_HW_D3D11::CompileShaders()
|
|||
progress.Increment();
|
||||
}
|
||||
|
||||
for (u8 render_mode = 0; render_mode < 4; render_mode++)
|
||||
// Batch fragment shader matrix. Behaviour depends on
|
||||
// g_settings.gpu_shader_precompile_mode:
|
||||
//
|
||||
// - Enabled : compile every slot right here, like before. Old
|
||||
// behaviour preserved.
|
||||
// - Lazy : leave the matrix empty for now; spawn a worker at
|
||||
// the end of CompileShaders that fills it in
|
||||
// background.
|
||||
// - Disabled: leave the matrix empty. GetBatchPixelShader will
|
||||
// fault each combo in synchronously the first time
|
||||
// the game dispatches a draw using it.
|
||||
//
|
||||
// GetBatchPixelShader handles the Reserved_*Direct16Bit dedup
|
||||
// internally, so the precompile loop here doesn't need to special-
|
||||
// case those - it'll call GetBatchPixelShader and the second
|
||||
// duplicate will be a cheap cache hit / pointer copy.
|
||||
if (precompile_sync)
|
||||
{
|
||||
for (u8 texture_mode = 0; texture_mode < 9; texture_mode++)
|
||||
for (u8 render_mode = 0; render_mode < 4; render_mode++)
|
||||
{
|
||||
// Reserved_Direct16Bit (3) and Reserved_RawDirect16Bit (7) produce
|
||||
// shaders that are byte-for-byte identical to Direct16Bit (2) and
|
||||
// RawDirect16Bit (6) after macro expansion: GPU_HW_ShaderGen's
|
||||
// GenerateBatchFragmentShader masks the RawTextureBit out of
|
||||
// texture_mode and only consults the result via the PALETTE,
|
||||
// PALETTE_4_BIT, and PALETTE_8_BIT macros - all three of which
|
||||
// evaluate to false for any of {Direct16Bit, Reserved_Direct16Bit,
|
||||
// RawDirect16Bit, Reserved_RawDirect16Bit}. The disk-backed
|
||||
// shader cache already deduplicates at the MD5 level, but
|
||||
// reaching that fast-path still requires (a) calling
|
||||
// GenerateBatchFragmentShader, which builds ~3000 lines of HLSL
|
||||
// every iteration, and (b) MD5-hashing that output to compute
|
||||
// the cache key. Short-circuit at the dispatch matrix instead
|
||||
// so the slots for 3 and 7 inherit the compiled object pointer
|
||||
// directly from 2 and 6, eliminating those two cost centres.
|
||||
const u8 source_mode = (texture_mode == static_cast<u8>(GPUTextureMode::Reserved_Direct16Bit)) ? 2u :
|
||||
(texture_mode == static_cast<u8>(GPUTextureMode::Reserved_RawDirect16Bit)) ? 6u :
|
||||
texture_mode;
|
||||
|
||||
for (u8 dithering = 0; dithering < 2; dithering++)
|
||||
for (u8 texture_mode = 0; texture_mode < 9; texture_mode++)
|
||||
{
|
||||
for (u8 interlacing = 0; interlacing < 2; interlacing++)
|
||||
for (u8 dithering = 0; dithering < 2; dithering++)
|
||||
{
|
||||
if (source_mode != texture_mode)
|
||||
for (u8 interlacing = 0; interlacing < 2; interlacing++)
|
||||
{
|
||||
m_batch_pixel_shaders[render_mode][texture_mode][dithering][interlacing] =
|
||||
m_batch_pixel_shaders[render_mode][source_mode][dithering][interlacing];
|
||||
ID3D11PixelShader* shader = GetBatchPixelShader(render_mode, texture_mode,
|
||||
ConvertToBoolUnchecked(dithering),
|
||||
ConvertToBoolUnchecked(interlacing));
|
||||
if (!shader)
|
||||
return false;
|
||||
|
||||
progress.Increment();
|
||||
continue;
|
||||
}
|
||||
|
||||
const std::string ps = shadergen.GenerateBatchFragmentShader(
|
||||
static_cast<BatchRenderMode>(render_mode), static_cast<GPUTextureMode>(texture_mode),
|
||||
ConvertToBoolUnchecked(dithering), ConvertToBoolUnchecked(interlacing));
|
||||
|
||||
m_batch_pixel_shaders[render_mode][texture_mode][dithering][interlacing] =
|
||||
shader_cache.GetPixelShader(m_device.Get(), ps);
|
||||
if (!m_batch_pixel_shaders[render_mode][texture_mode][dithering][interlacing])
|
||||
return false;
|
||||
|
||||
progress.Increment();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// For Lazy and Disabled: m_batch_pixel_shaders stays empty here;
|
||||
// each slot fills in on first use via GetBatchPixelShader. The
|
||||
// background-thread launch for Lazy happens after the rest of the
|
||||
// non-batch shaders are built, near the end of this function.
|
||||
|
||||
m_copy_pixel_shader = shader_cache.GetPixelShader(m_device.Get(), shadergen.GenerateCopyFragmentShader());
|
||||
if (!m_copy_pixel_shader)
|
||||
|
|
@ -1125,11 +1150,117 @@ bool GPU_HW_D3D11::CompileShaders()
|
|||
|
||||
#undef UPDATE_PROGRESS
|
||||
|
||||
if (precompile_mode == GPUShaderPrecompileMode::Lazy)
|
||||
{
|
||||
// Kick off the background-thread batch-fragment-shader fill so
|
||||
// gameplay can start while the rest of the matrix compiles. The
|
||||
// worker just walks the (render, texture, dither, interlace)
|
||||
// matrix in order, calling the same GetBatchPixelShader helper
|
||||
// the draw path uses, so any slot the game touches in the
|
||||
// meantime is just skipped here (the recheck under the mutex
|
||||
// sees it's already filled). DestroyShaders signals
|
||||
// m_shader_compile_thread_quit and joins.
|
||||
m_shader_compile_thread_quit.store(false, std::memory_order_relaxed);
|
||||
m_shader_compile_thread = std::thread(&GPU_HW_D3D11::ShaderCompileThreadEntryPoint, this);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void GPU_HW_D3D11::StopShaderCompileThread()
|
||||
{
|
||||
if (!m_shader_compile_thread.joinable())
|
||||
return;
|
||||
|
||||
m_shader_compile_thread_quit.store(true, std::memory_order_relaxed);
|
||||
m_shader_compile_thread.join();
|
||||
m_shader_compile_thread_quit.store(false, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
void GPU_HW_D3D11::ShaderCompileThreadEntryPoint()
|
||||
{
|
||||
// Walk the matrix in (render, texture, dither, interlace) order and
|
||||
// call GetBatchPixelShader on each cell. Each call takes
|
||||
// m_batch_shader_mutex internally; we don't hold a lock across the
|
||||
// loop so the main thread can still race ahead and pre-fill any
|
||||
// slots it actually needs at draw time without waiting for the
|
||||
// worker to reach them. The quit flag is checked between cells so
|
||||
// DestroyShaders can bring the worker down within at most one
|
||||
// shader-compile worth of latency.
|
||||
for (u8 render_mode = 0; render_mode < 4; render_mode++)
|
||||
{
|
||||
for (u8 texture_mode = 0; texture_mode < 9; texture_mode++)
|
||||
{
|
||||
for (u8 dithering = 0; dithering < 2; dithering++)
|
||||
{
|
||||
for (u8 interlacing = 0; interlacing < 2; interlacing++)
|
||||
{
|
||||
if (m_shader_compile_thread_quit.load(std::memory_order_relaxed))
|
||||
return;
|
||||
|
||||
GetBatchPixelShader(render_mode, texture_mode, ConvertToBoolUnchecked(dithering),
|
||||
ConvertToBoolUnchecked(interlacing));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ID3D11PixelShader* GPU_HW_D3D11::GetBatchPixelShader(u8 render_mode, u8 texture_mode, bool dithering, bool interlacing)
|
||||
{
|
||||
// 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).
|
||||
const u8 lookup_mode = (texture_mode == static_cast<u8>(GPUTextureMode::Reserved_Direct16Bit)) ? 2u :
|
||||
(texture_mode == static_cast<u8>(GPUTextureMode::Reserved_RawDirect16Bit)) ? 6u :
|
||||
texture_mode;
|
||||
|
||||
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.
|
||||
ComPtr<ID3D11PixelShader>& canonical_slot =
|
||||
m_batch_pixel_shaders[render_mode][lookup_mode][BoolToUInt8(dithering)][BoolToUInt8(interlacing)];
|
||||
|
||||
if (!canonical_slot)
|
||||
{
|
||||
if (!m_shadergen)
|
||||
{
|
||||
Log_ErrorPrint("GetBatchPixelShader called before CompileShaders constructed the shadergen");
|
||||
return nullptr;
|
||||
}
|
||||
const std::string ps = m_shadergen->GenerateBatchFragmentShader(
|
||||
static_cast<BatchRenderMode>(render_mode), static_cast<GPUTextureMode>(lookup_mode), dithering, interlacing);
|
||||
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));
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
if (lookup_mode != texture_mode)
|
||||
{
|
||||
ComPtr<ID3D11PixelShader>& dup_slot =
|
||||
m_batch_pixel_shaders[render_mode][texture_mode][BoolToUInt8(dithering)][BoolToUInt8(interlacing)];
|
||||
if (!dup_slot)
|
||||
dup_slot = canonical_slot;
|
||||
}
|
||||
|
||||
return canonical_slot.Get();
|
||||
}
|
||||
|
||||
void GPU_HW_D3D11::DestroyShaders()
|
||||
{
|
||||
// Tear the background compile thread down before clearing the
|
||||
// matrix - otherwise the worker would be writing into ComPtrs we're
|
||||
// about to default-construct.
|
||||
StopShaderCompileThread();
|
||||
m_shadergen.reset();
|
||||
|
||||
m_downsample_composite_pixel_shader.Reset();
|
||||
m_downsample_blur_pass_pixel_shader.Reset();
|
||||
m_downsample_mid_pass_pixel_shader.Reset();
|
||||
|
|
@ -1255,10 +1386,18 @@ void GPU_HW_D3D11::DrawBatchVertices(BatchRenderMode render_mode, u32 base_verte
|
|||
|
||||
m_context->VSSetShader(m_batch_vertex_shaders[BoolToUInt8(textured)].Get(), nullptr, 0);
|
||||
|
||||
m_context->PSSetShader(m_batch_pixel_shaders[static_cast<u8>(render_mode)][static_cast<u8>(m_batch.texture_mode)]
|
||||
[BoolToUInt8(m_batch.dithering)][BoolToUInt8(m_batch.interlacing)]
|
||||
.Get(),
|
||||
nullptr, 0);
|
||||
// Fetch the batch pixel shader via the lazy helper. In 'Enabled'
|
||||
// precompile mode every slot was already filled at CompileShaders
|
||||
// time so this is a fast mutex-protected pointer load. In 'Lazy'
|
||||
// mode this either gets the already-compiled shader (background
|
||||
// thread reached it first) or compiles it now on the main thread
|
||||
// (game raced ahead of the worker). In 'Disabled' mode it always
|
||||
// compiles on miss. The mutex guards both the cache and the
|
||||
// matrix; cost is ~20 ns uncontended per modern std::mutex impl.
|
||||
ID3D11PixelShader* batch_pixel_shader =
|
||||
GetBatchPixelShader(static_cast<u8>(render_mode), static_cast<u8>(m_batch.texture_mode), m_batch.dithering,
|
||||
m_batch.interlacing);
|
||||
m_context->PSSetShader(batch_pixel_shader, nullptr, 0);
|
||||
|
||||
const GPUTransparencyMode transparency_mode =
|
||||
(render_mode == BatchRenderMode::OnlyOpaque) ? GPUTransparencyMode::Disabled : m_batch.transparency_mode;
|
||||
|
|
|
|||
|
|
@ -4,11 +4,15 @@
|
|||
#include "common/d3d11/stream_buffer.h"
|
||||
#include "common/d3d11/texture.h"
|
||||
#include "gpu_hw.h"
|
||||
#include "gpu_hw_shadergen.h"
|
||||
#include "host_display.h"
|
||||
#include "texture_replacements.h"
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <d3d11.h>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <tuple>
|
||||
#include <wrl/client.h>
|
||||
#include <libretro.h>
|
||||
|
|
@ -135,6 +139,29 @@ 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.
|
||||
ID3D11PixelShader* GetBatchPixelShader(u8 render_mode, u8 texture_mode, bool dithering, bool interlacing);
|
||||
|
||||
// Background-thread worker for 'Lazy' mode: walks the entire
|
||||
// (render_mode, texture_mode, dithering, interlacing) matrix and
|
||||
// calls GetBatchPixelShader on each slot. The main thread can
|
||||
// race ahead and pre-populate any slots it actually needs; the
|
||||
// worker just skips already-filled slots.
|
||||
void ShaderCompileThreadEntryPoint();
|
||||
void StopShaderCompileThread();
|
||||
|
||||
void SetViewport(u32 x, u32 y, u32 width, u32 height);
|
||||
void SetScissor(u32 x, u32 y, u32 width, u32 height);
|
||||
void SetViewportAndScissor(u32 x, u32 y, u32 width, u32 height);
|
||||
|
|
@ -189,6 +216,23 @@ private:
|
|||
std::array<std::array<std::array<std::array<ComPtr<ID3D11PixelShader>, 2>, 2>, 9>, 4>
|
||||
m_batch_pixel_shaders; // [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.
|
||||
std::mutex m_batch_shader_mutex;
|
||||
D3D11::ShaderCache m_shader_cache;
|
||||
std::unique_ptr<GPU_HW_ShaderGen> m_shadergen;
|
||||
std::thread m_shader_compile_thread;
|
||||
std::atomic<bool> m_shader_compile_thread_quit{false};
|
||||
|
||||
ComPtr<ID3D11VertexShader> m_screen_quad_vertex_shader;
|
||||
ComPtr<ID3D11VertexShader> m_uv_quad_vertex_shader;
|
||||
ComPtr<ID3D11PixelShader> m_copy_pixel_shader;
|
||||
|
|
|
|||
|
|
@ -171,6 +171,10 @@ void Settings::Load(LibretroSettingsInterface& si)
|
|||
ParseDownsampleModeName(
|
||||
si.GetStringValue("GPU", "DownsampleMode", GetDownsampleModeName(DEFAULT_GPU_DOWNSAMPLE_MODE)).c_str())
|
||||
.value_or(DEFAULT_GPU_DOWNSAMPLE_MODE);
|
||||
gpu_shader_precompile_mode =
|
||||
ParseShaderPrecompileMode(
|
||||
si.GetStringValue("GPU", "ShaderPrecompile", GetShaderPrecompileModeName(GPUShaderPrecompileMode::Lazy)).c_str())
|
||||
.value_or(GPUShaderPrecompileMode::Lazy);
|
||||
gpu_disable_interlacing = si.GetBoolValue("GPU", "DisableInterlacing", true);
|
||||
gpu_force_ntsc_timings = si.GetBoolValue("GPU", "ForceNTSCTimings", false);
|
||||
gpu_widescreen_hack = si.GetBoolValue("GPU", "WidescreenHack", false);
|
||||
|
|
@ -477,6 +481,27 @@ const char* Settings::GetDownsampleModeName(GPUDownsampleMode mode)
|
|||
return s_downsample_mode_names[static_cast<int>(mode)];
|
||||
}
|
||||
|
||||
static constexpr auto s_shader_precompile_mode_names = make_array("Disabled", "Enabled", "Lazy");
|
||||
|
||||
std::optional<GPUShaderPrecompileMode> Settings::ParseShaderPrecompileMode(const char* str)
|
||||
{
|
||||
int index = 0;
|
||||
for (const char* name : s_shader_precompile_mode_names)
|
||||
{
|
||||
if (StringUtil::Strcasecmp(name, str) == 0)
|
||||
return static_cast<GPUShaderPrecompileMode>(index);
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const char* Settings::GetShaderPrecompileModeName(GPUShaderPrecompileMode mode)
|
||||
{
|
||||
return s_shader_precompile_mode_names[static_cast<int>(mode)];
|
||||
}
|
||||
|
||||
static std::array<const char*, 3> s_display_crop_mode_names = {{"None", "Overscan", "Borders"}};
|
||||
static std::array<const char*, 3> s_display_crop_mode_display_names = {
|
||||
{TRANSLATABLE("DisplayCropMode", "None"), TRANSLATABLE("DisplayCropMode", "Only Overscan Area"),
|
||||
|
|
|
|||
|
|
@ -74,6 +74,7 @@ struct Settings
|
|||
bool gpu_scaled_dithering = false;
|
||||
GPUTextureFilter gpu_texture_filter = GPUTextureFilter::Nearest;
|
||||
GPUDownsampleMode gpu_downsample_mode = GPUDownsampleMode::Disabled;
|
||||
GPUShaderPrecompileMode gpu_shader_precompile_mode = GPUShaderPrecompileMode::Lazy;
|
||||
bool gpu_disable_interlacing = true;
|
||||
bool gpu_force_ntsc_timings = false;
|
||||
bool gpu_widescreen_hack = false;
|
||||
|
|
@ -222,6 +223,9 @@ struct Settings
|
|||
static const char* GetDownsampleModeName(GPUDownsampleMode mode);
|
||||
static const char* GetDownsampleModeDisplayName(GPUDownsampleMode mode);
|
||||
|
||||
static std::optional<GPUShaderPrecompileMode> ParseShaderPrecompileMode(const char* str);
|
||||
static const char* GetShaderPrecompileModeName(GPUShaderPrecompileMode mode);
|
||||
|
||||
static std::optional<DisplayCropMode> ParseDisplayCropMode(const char* str);
|
||||
static const char* GetDisplayCropModeName(DisplayCropMode crop_mode);
|
||||
static const char* GetDisplayCropModeDisplayName(DisplayCropMode crop_mode);
|
||||
|
|
|
|||
|
|
@ -84,6 +84,40 @@ enum class GPUDownsampleMode : u8
|
|||
Count
|
||||
};
|
||||
|
||||
// Controls when batch fragment shaders / PSOs are built. There are
|
||||
// 144 batch fragment shader permutations on D3D11 / OpenGL and up to
|
||||
// 2160 PSO permutations on Vulkan, but most games only ever dispatch a
|
||||
// small subset of them at runtime. The historical behaviour was to
|
||||
// compile the entire matrix at GPU init - which is what 'Enabled' does
|
||||
// - and that's where the multi-second 'libretro looks hung' stall on
|
||||
// texture-filter changes comes from.
|
||||
//
|
||||
// - Disabled: no precompile at GPU init. Each batch shader / PSO is
|
||||
// compiled on the main thread the first time the game actually
|
||||
// dispatches a draw using that combination, then cached. The user
|
||||
// trades the single multi-second stall for a series of small
|
||||
// first-use hitches spread across early gameplay. Lowest startup
|
||||
// latency; useful on low-end hardware and for users who don't use
|
||||
// the heavier filters (JINC2/xBR/Bilinear).
|
||||
// - Enabled: the historical behaviour. CompileShaders walks the full
|
||||
// matrix synchronously at GPU init. RetroArch is blocked the whole
|
||||
// time. After that there are no per-draw hitches at all. Useful
|
||||
// for benchmarking and for users who want a single up-front cost.
|
||||
// - Lazy: compile on a background thread while gameplay starts
|
||||
// immediately. Each combo the game actually needs is faulted in
|
||||
// on the main thread if the background thread hasn't reached it
|
||||
// yet (worst case: same hitch as Disabled for that one combo),
|
||||
// otherwise the entry is just picked up. After the background
|
||||
// thread finishes, behaviour is identical to Enabled.
|
||||
// This is the default.
|
||||
enum class GPUShaderPrecompileMode : u8
|
||||
{
|
||||
Disabled,
|
||||
Enabled,
|
||||
Lazy,
|
||||
Count
|
||||
};
|
||||
|
||||
enum class DisplayCropMode : u8
|
||||
{
|
||||
None,
|
||||
|
|
|
|||
|
|
@ -701,6 +701,25 @@ struct retro_core_option_v2_definition option_defs_us[] = {
|
|||
{NULL, NULL},
|
||||
},
|
||||
"Disabled"},
|
||||
{"swanstation_GPU_ShaderPrecompile",
|
||||
"Shader Precompilation",
|
||||
NULL,
|
||||
"Controls when the hardware renderer's batch fragment shaders are compiled. "
|
||||
"'Lazy' (default) compiles them on a background thread while gameplay starts immediately; "
|
||||
"the previous frontend-blocking behaviour is preserved as 'Enabled'. "
|
||||
"'Disabled' skips precompilation entirely and compiles each shader on the main thread "
|
||||
"the first time the game actually dispatches a draw using it - lowest startup latency, "
|
||||
"but brief stutters can occur during early gameplay when new shader permutations are "
|
||||
"encountered. Changing the texture filter recompiles the matrix and pays this cost again.",
|
||||
NULL,
|
||||
"display",
|
||||
{
|
||||
{"Lazy", "Lazy (background thread, default)"},
|
||||
{"Enabled", "Enabled (block until done, like old behaviour)"},
|
||||
{"Disabled", "Disabled (compile on first use)"},
|
||||
{NULL, NULL},
|
||||
},
|
||||
"Lazy"},
|
||||
{"swanstation_Display_ShowOSDMessages",
|
||||
"Display OSD Messages",
|
||||
NULL,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue