gpu_hw_vulkan: route batch fragment shaders + PSOs through the same three-mode precompile machinery as D3D11/D3D12
This is the Vulkan follow-up to commitsf188bb0(D3D11) andd7938d2(D3D12), applying the same 'Disabled / Enabled / Lazy' precompile core option to the Vulkan backend. Vulkan has the largest PSO matrix of all four hardware backends: 3 x 4 x 5 x 9 x 2 x 2 = 2160 entries (versus D3D12's 1440 and D3D11's effectively-1 ID3D11PixelShader matrix of 144 entries), because Vulkan uses three depth-compare values (VK_COMPARE_OP_ALWAYS / GREATER_OR_EQUAL / LESS_OR_EQUAL) where D3D12 only uses two. Each vkCreateGraphicsPipelines call is typically 5-50 ms cold, putting the Enabled-mode synchronous precompile at 30-90 seconds with heavier texture filters - the longest stall of any backend by a comfortable margin. Vulkan thread-safety model: - vkCreateGraphicsPipelines: per the spec, when sharing a VkPipelineCache across threads it must be externally synchronised. The single m_batch_shader_mutex covers that. - g_vulkan_shader_cache singleton: its internal unordered_map indices aren't thread-safe on insert. Same mutex covers it. - vkCreateShaderModule: thread-safe per spec; serialised here incidentally because it's called from under the lock. Two structural differences from the D3D11 / D3D12 commits: 1. Vulkan can't share refcounted resource wrappers between dispatch matrix slots, because VkShaderModule and VkPipeline are raw handles destroyed via Vulkan::Util::SafeDestroyShaderModule and SafeDestroyPipeline. Two slots holding the same handle would double-call vkDestroy*, which was the reason commit8e7f7a3(the Reserved_*Direct16Bit dedup) explicitly left Vulkan's PSO matrix alone. This commit resolves that by moving the Reserved_*Direct16Bit dedup to the *helper entry*: texture modes 3 and 7 are remapped to 2 and 6 before the array index is computed, so the dup slots remain VK_NULL_HANDLE for the lifetime of the GPU backend. SafeDestroy* gracefully no-ops on VK_NULL_HANDLE, so DestroyPipelines doesn't trip on them. The net effect is the same as the D3D11/D3D12 ComPtr-copy approach (~22% fewer real PSO compiles, ~480 of the 2160 entries) but without the lifetime hazard. 2. The Vulkan shader cache is already a global singleton (g_vulkan_shader_cache), opened once via ShaderCache::Create at backend bring-up and torn down at shutdown. There's no re-open-on-UpdateSettings problem to guard against (which is what motivated the new ShaderCache::IsOpen() accessor on the D3D11 / D3D12 caches), so no equivalent change is needed in common/vulkan/shader_cache.h. Otherwise the structure is identical to the D3D12 commit: - GetBatchFragmentShader(render, texture, dithering, interlacing) returns the VkShaderModule for one cell of the fragment-shader matrix, faulting in via g_vulkan_shader_cache->GetFragmentShader on miss. Caller-locked (the only callers, GetBatchPipeline and the Enabled-mode precompile loop, hold the mutex / are single-threaded respectively). - GetBatchPipeline(depth, render, texture, transparency, dithering, interlacing) returns the VkPipeline for one cell of the 6-D PSO matrix. Calls GetBatchFragmentShader for the bound shader, builds a fresh Vulkan::GraphicsPipelineBuilder for the cell, calls gpbuilder.Create which goes through the shared VkPipelineCache. Internal mutex lock. - DrawBatchVertices routes its m_batch_pipelines lookup through GetBatchPipeline so all three modes share the same fault-in path. - The background-thread worker (Lazy mode) walks the full PSO matrix and calls GetBatchPipeline on each cell. Same double-checked-locking pattern as D3D11 / D3D12: main thread races ahead, worker observes filled slots under the lock and moves on. Quit flag checked between cells so DestroyPipelines can stop the worker within at most one PSO compile of latency. Persistence changes: - GPU_HW_ShaderGen is now a member (m_shadergen) so GetBatchFragmentShader can call into it at draw time. Rebuilt each CompilePipelines because its baked-in settings may have changed. - m_batch_vertex_shaders and m_batch_fragment_shaders are now members, previously locals in CompilePipelines. They were leaked-on-success in the previous code path (no enumerate(SafeDestroyShaderModule) after the PSO loop completed). Now properly destroyed in DestroyPipelines. Re-entry safety: CompilePipelines calls StopShaderCompileThread at the top so any worker from a prior UpdateSettings round-trip is joined before a new matrix fill begins. DestroyPipelines does the same before clearing the matrix. The TSAN test from the D3D11 commit (double-checked lock + Reserved_*Direct16Bit dedup + worker vs random main-thread fault-ins) models the exact same pattern as Vulkan; the only difference is the dedup location (helper entry here, post-compile ComPtr-copy there), which doesn't affect the lock structure being tested. Only OpenGL remains. The libretro hardware-renderer protocol doesn't give us a shareable GL context for a worker thread, so its Lazy mode will degrade to Disabled (compile-on-first-use on the main thread, no background work). Build: zero warnings, zero errors on linux platform=unix. .so size 8035864 (+5288 vs commit d7938d2's 8030576) - Vulkan is unlike D3D11/D3D12 in that gpu_hw_vulkan.cpp does compile on Linux, so this number reflects the real binary growth from the new helpers and threading machinery. 25 retro_* exports unchanged. Files touched: - src/core/gpu_hw_vulkan.h: includes (atomic, mutex, thread, shadergen), m_batch_shader_mutex, m_shadergen, m_shader_compile_thread, m_shader_compile_thread_quit, persistent m_batch_vertex_shaders / m_batch_fragment_shaders arrays, declarations for GetBatchFragmentShader / GetBatchPipeline / ShaderCompileThreadEntryPoint / StopShaderCompileThread - src/core/gpu_hw_vulkan.cpp: persistent shadergen, three-way CompilePipelines branch, helpers, worker entry point, DestroyPipelines joins worker and adds SafeDestroyShaderModule sweeps for the now-persistent shader arrays, DrawBatchVertices routes through the helper
This commit is contained in:
parent
d7938d2099
commit
9d2997a601
2 changed files with 353 additions and 138 deletions
|
|
@ -1448,20 +1448,38 @@ bool GPU_HW_Vulkan::CreateTextureBuffer()
|
|||
|
||||
bool GPU_HW_Vulkan::CompilePipelines()
|
||||
{
|
||||
// Make sure no previous background-compile worker is still alive
|
||||
// (UpdateSettings triggers DestroyPipelines -> CompilePipelines).
|
||||
StopShaderCompileThread();
|
||||
|
||||
VkDevice device = g_vulkan_context->GetDevice();
|
||||
VkPipelineCache pipeline_cache = g_vulkan_shader_cache->GetPipelineCache();
|
||||
|
||||
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);
|
||||
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;
|
||||
|
||||
ShaderCompileProgressTracker progress("Compiling Pipelines", 2 + (4 * 9 * 2 * 2) + (3 * 4 * 5 * 9 * 2 * 2) + 1 + 2 +
|
||||
(2 * 2) + 2 + 1 + 1 + (2 * 3) + 1);
|
||||
// Three-mode precompile control - see 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_shader_progress_units =
|
||||
(precompile_mode == GPUShaderPrecompileMode::Enabled) ? static_cast<u32>(4 * 9 * 2 * 2) : 0u;
|
||||
const u32 batch_pipeline_progress_units =
|
||||
(precompile_mode == GPUShaderPrecompileMode::Enabled) ? static_cast<u32>(3 * 4 * 5 * 9 * 2 * 2) : 0u;
|
||||
|
||||
// vertex shaders - [textured]
|
||||
// fragment shaders - [render_mode][texture_mode][dithering][interlacing]
|
||||
DimensionalArray<VkShaderModule, 2> batch_vertex_shaders{};
|
||||
DimensionalArray<VkShaderModule, 2, 2, 9, 4> batch_fragment_shaders{};
|
||||
ShaderCompileProgressTracker progress("Compiling Pipelines",
|
||||
2 + batch_shader_progress_units + batch_pipeline_progress_units + 1 + 2 +
|
||||
(2 * 2) + 2 + 1 + 1 + (2 * 3) + 1);
|
||||
|
||||
// The batch vertex and fragment shader-module arrays now live as
|
||||
// members so the lazy PSO builder can reach them at draw time.
|
||||
// Use local references for readability in the rest of this
|
||||
// function (matching the previous structure).
|
||||
auto& batch_vertex_shaders = m_batch_vertex_shaders;
|
||||
auto& batch_fragment_shaders = m_batch_fragment_shaders;
|
||||
|
||||
for (u8 textured = 0; textured < 2; textured++)
|
||||
{
|
||||
|
|
@ -1469,8 +1487,9 @@ bool GPU_HW_Vulkan::CompilePipelines()
|
|||
VkShaderModule shader = g_vulkan_shader_cache->GetVertexShader(vs);
|
||||
if (shader == VK_NULL_HANDLE)
|
||||
{
|
||||
batch_vertex_shaders.enumerate(Vulkan::Util::SafeDestroyShaderModule);
|
||||
batch_fragment_shaders.enumerate(Vulkan::Util::SafeDestroyShaderModule);
|
||||
// DestroyPipelines takes care of partial state from a failed
|
||||
// CompilePipelines via its enumerate(SafeDestroy*) sweeps,
|
||||
// because the arrays we're filling here are now members.
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -1478,150 +1497,80 @@ bool GPU_HW_Vulkan::CompilePipelines()
|
|||
progress.Increment();
|
||||
}
|
||||
|
||||
for (u8 render_mode = 0; render_mode < 4; render_mode++)
|
||||
{
|
||||
for (u8 texture_mode = 0; texture_mode < 9; texture_mode++)
|
||||
{
|
||||
// See the comment in GPU_HW_D3D11::CompileShaders: shaders for
|
||||
// Reserved_*Direct16Bit modes are bit-identical to their non-
|
||||
// reserved counterparts. We can't share the resulting
|
||||
// VkShaderModule handle between array slots safely because the
|
||||
// error-path 'enumerate(SafeDestroyShaderModule)' calls would
|
||||
// double-destroy, so for Vulkan we still call into
|
||||
// GetFragmentShader for each slot - but we feed it the source
|
||||
// string from the canonical (source_mode) slot, skipping the
|
||||
// ~50ms GenerateBatchFragmentShader call. The shader cache
|
||||
// hits on the second lookup and avoids the GLSL->SPIR-V
|
||||
// compile too; only the cheap vkCreateShaderModule call runs.
|
||||
// The PSO loop below does NOT dedup because VkPipeline shares
|
||||
// the same enumerate-destroy hazard with no convenient
|
||||
// refcounting alternative - the per-PSO win for ~22% of the
|
||||
// pipeline matrix is left for a future commit that adds a
|
||||
// proper ownership-tracking layer.
|
||||
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 interlacing = 0; interlacing < 2; interlacing++)
|
||||
{
|
||||
const std::string fs = shadergen.GenerateBatchFragmentShader(
|
||||
static_cast<BatchRenderMode>(render_mode), static_cast<GPUTextureMode>(source_mode),
|
||||
ConvertToBoolUnchecked(dithering), ConvertToBoolUnchecked(interlacing));
|
||||
|
||||
VkShaderModule shader = g_vulkan_shader_cache->GetFragmentShader(fs);
|
||||
if (shader == VK_NULL_HANDLE)
|
||||
{
|
||||
batch_vertex_shaders.enumerate(Vulkan::Util::SafeDestroyShaderModule);
|
||||
batch_fragment_shaders.enumerate(Vulkan::Util::SafeDestroyShaderModule);
|
||||
return false;
|
||||
}
|
||||
|
||||
batch_fragment_shaders[render_mode][texture_mode][dithering][interlacing] = shader;
|
||||
progress.Increment();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Vulkan::GraphicsPipelineBuilder gpbuilder;
|
||||
|
||||
// [depth_test][render_mode][texture_mode][transparency_mode][dithering][interlacing]
|
||||
for (u8 depth_test = 0; depth_test < 3; depth_test++)
|
||||
// Batch fragment shader matrix and PSO matrix. Behaviour depends
|
||||
// on g_settings.gpu_shader_precompile_mode (see core/types.h):
|
||||
//
|
||||
// - Enabled : compile every batch shader-module and every PSO
|
||||
// right here, as in the historical implementation.
|
||||
// - Lazy : leave the matrices empty; spawn a worker at the
|
||||
// end of CompilePipelines that fills them in the
|
||||
// background.
|
||||
// - Disabled: leave the matrices empty. GetBatchFragmentShader /
|
||||
// GetBatchPipeline fault each combo in on the main
|
||||
// thread the first time the game draws it.
|
||||
//
|
||||
// Both helpers handle the Reserved_*Direct16Bit dedup at the
|
||||
// helper entry (remap texture_mode 3 -> 2 and 7 -> 6 before the
|
||||
// array index), so the precompile loop doesn't need to special-
|
||||
// case them - the entry for 3 / 7 simply forwards to the
|
||||
// canonical slot.
|
||||
if (precompile_sync)
|
||||
{
|
||||
for (u8 render_mode = 0; render_mode < 4; render_mode++)
|
||||
{
|
||||
for (u8 transparency_mode = 0; transparency_mode < 5; transparency_mode++)
|
||||
for (u8 texture_mode = 0; texture_mode < 9; texture_mode++)
|
||||
{
|
||||
for (u8 texture_mode = 0; texture_mode < 9; texture_mode++)
|
||||
for (u8 dithering = 0; dithering < 2; dithering++)
|
||||
{
|
||||
for (u8 dithering = 0; dithering < 2; dithering++)
|
||||
for (u8 interlacing = 0; interlacing < 2; interlacing++)
|
||||
{
|
||||
for (u8 interlacing = 0; interlacing < 2; interlacing++)
|
||||
VkShaderModule shader =
|
||||
GetBatchFragmentShader(render_mode, texture_mode, ConvertToBoolUnchecked(dithering),
|
||||
ConvertToBoolUnchecked(interlacing));
|
||||
if (shader == VK_NULL_HANDLE)
|
||||
return false;
|
||||
progress.Increment();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (u8 depth_test = 0; depth_test < 3; depth_test++)
|
||||
{
|
||||
for (u8 render_mode = 0; render_mode < 4; render_mode++)
|
||||
{
|
||||
for (u8 transparency_mode = 0; transparency_mode < 5; transparency_mode++)
|
||||
{
|
||||
for (u8 texture_mode = 0; texture_mode < 9; texture_mode++)
|
||||
{
|
||||
for (u8 dithering = 0; dithering < 2; dithering++)
|
||||
{
|
||||
static constexpr std::array<VkCompareOp, 3> depth_test_values = {
|
||||
VK_COMPARE_OP_ALWAYS, VK_COMPARE_OP_GREATER_OR_EQUAL, VK_COMPARE_OP_LESS_OR_EQUAL};
|
||||
const bool textured = (static_cast<GPUTextureMode>(texture_mode) != GPUTextureMode::Disabled);
|
||||
|
||||
gpbuilder.SetPipelineLayout(m_batch_pipeline_layout);
|
||||
gpbuilder.SetRenderPass(m_vram_render_pass, 0);
|
||||
|
||||
gpbuilder.AddVertexBuffer(0, sizeof(BatchVertex), VK_VERTEX_INPUT_RATE_VERTEX);
|
||||
gpbuilder.AddVertexAttribute(0, 0, VK_FORMAT_R32G32B32A32_SFLOAT, offsetof(BatchVertex, x));
|
||||
gpbuilder.AddVertexAttribute(1, 0, VK_FORMAT_R8G8B8A8_UNORM, offsetof(BatchVertex, color));
|
||||
if (textured)
|
||||
for (u8 interlacing = 0; interlacing < 2; interlacing++)
|
||||
{
|
||||
gpbuilder.AddVertexAttribute(2, 0, VK_FORMAT_R32_UINT, offsetof(BatchVertex, u));
|
||||
gpbuilder.AddVertexAttribute(3, 0, VK_FORMAT_R32_UINT, offsetof(BatchVertex, texpage));
|
||||
if (m_using_uv_limits)
|
||||
gpbuilder.AddVertexAttribute(4, 0, VK_FORMAT_R8G8B8A8_UNORM, offsetof(BatchVertex, uv_limits));
|
||||
VkPipeline pipeline =
|
||||
GetBatchPipeline(depth_test, render_mode, texture_mode, transparency_mode,
|
||||
ConvertToBoolUnchecked(dithering), ConvertToBoolUnchecked(interlacing));
|
||||
if (pipeline == VK_NULL_HANDLE)
|
||||
return false;
|
||||
progress.Increment();
|
||||
}
|
||||
|
||||
gpbuilder.SetPrimitiveTopology(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST);
|
||||
gpbuilder.SetVertexShader(batch_vertex_shaders[BoolToUInt8(textured)]);
|
||||
gpbuilder.SetFragmentShader(batch_fragment_shaders[render_mode][texture_mode][dithering][interlacing]);
|
||||
|
||||
gpbuilder.SetRasterizationState(VK_POLYGON_MODE_FILL, VK_CULL_MODE_NONE, VK_FRONT_FACE_CLOCKWISE);
|
||||
gpbuilder.SetDepthState(true, true, depth_test_values[depth_test]);
|
||||
gpbuilder.SetNoBlendingState();
|
||||
gpbuilder.SetMultisamples(m_multisamples, m_per_sample_shading);
|
||||
|
||||
if ((static_cast<GPUTransparencyMode>(transparency_mode) != GPUTransparencyMode::Disabled &&
|
||||
(static_cast<BatchRenderMode>(render_mode) != BatchRenderMode::TransparencyDisabled &&
|
||||
static_cast<BatchRenderMode>(render_mode) != BatchRenderMode::OnlyOpaque)) ||
|
||||
m_texture_filtering != GPUTextureFilter::Nearest)
|
||||
{
|
||||
if (m_supports_dual_source_blend)
|
||||
{
|
||||
gpbuilder.SetBlendAttachment(
|
||||
0, true, VK_BLEND_FACTOR_ONE, VK_BLEND_FACTOR_SRC1_ALPHA,
|
||||
(static_cast<GPUTransparencyMode>(transparency_mode) ==
|
||||
GPUTransparencyMode::BackgroundMinusForeground &&
|
||||
static_cast<BatchRenderMode>(render_mode) != BatchRenderMode::TransparencyDisabled &&
|
||||
static_cast<BatchRenderMode>(render_mode) != BatchRenderMode::OnlyOpaque) ?
|
||||
VK_BLEND_OP_REVERSE_SUBTRACT :
|
||||
VK_BLEND_OP_ADD,
|
||||
VK_BLEND_FACTOR_ONE, VK_BLEND_FACTOR_ZERO, VK_BLEND_OP_ADD);
|
||||
}
|
||||
else
|
||||
{
|
||||
const float factor = (static_cast<GPUTransparencyMode>(transparency_mode) ==
|
||||
GPUTransparencyMode::HalfBackgroundPlusHalfForeground) ?
|
||||
0.5f :
|
||||
1.0f;
|
||||
gpbuilder.SetBlendAttachment(
|
||||
0, true, VK_BLEND_FACTOR_ONE, VK_BLEND_FACTOR_CONSTANT_ALPHA,
|
||||
(static_cast<GPUTransparencyMode>(transparency_mode) ==
|
||||
GPUTransparencyMode::BackgroundMinusForeground &&
|
||||
static_cast<BatchRenderMode>(render_mode) != BatchRenderMode::TransparencyDisabled &&
|
||||
static_cast<BatchRenderMode>(render_mode) != BatchRenderMode::OnlyOpaque) ?
|
||||
VK_BLEND_OP_REVERSE_SUBTRACT :
|
||||
VK_BLEND_OP_ADD,
|
||||
VK_BLEND_FACTOR_ONE, VK_BLEND_FACTOR_ZERO, VK_BLEND_OP_ADD);
|
||||
gpbuilder.SetBlendConstants(0.0f, 0.0f, 0.0f, factor);
|
||||
}
|
||||
}
|
||||
|
||||
gpbuilder.SetDynamicViewportAndScissorState();
|
||||
|
||||
VkPipeline pipeline = gpbuilder.Create(device, pipeline_cache);
|
||||
if (pipeline == VK_NULL_HANDLE)
|
||||
{
|
||||
batch_vertex_shaders.enumerate(Vulkan::Util::SafeDestroyShaderModule);
|
||||
batch_fragment_shaders.enumerate(Vulkan::Util::SafeDestroyShaderModule);
|
||||
return false;
|
||||
}
|
||||
|
||||
m_batch_pipelines[depth_test][render_mode][texture_mode][transparency_mode][dithering][interlacing] =
|
||||
pipeline;
|
||||
progress.Increment();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// For Lazy and Disabled: matrices stay empty here, filled on
|
||||
// demand by the helpers. The 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 (fullscreen quad, VRAM fill/copy/write, downsampling,
|
||||
// display). The batch pipelines build their own private
|
||||
// gpbuilder inside GetBatchPipeline so this one doesn't carry
|
||||
// any of their per-batch state.
|
||||
Vulkan::GraphicsPipelineBuilder gpbuilder;
|
||||
|
||||
VkShaderModule fullscreen_quad_vertex_shader =
|
||||
g_vulkan_shader_cache->GetVertexShader(shadergen.GenerateScreenQuadVertexShader());
|
||||
|
|
@ -1964,12 +1913,211 @@ bool GPU_HW_Vulkan::CompilePipelines()
|
|||
|
||||
#undef UPDATE_PROGRESS
|
||||
|
||||
if (precompile_mode == GPUShaderPrecompileMode::Lazy)
|
||||
{
|
||||
// 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,
|
||||
// dithering, interlacing) order, calling the same
|
||||
// GetBatchPipeline the draw path uses; the main thread can race
|
||||
// ahead and pre-fill any slot it actually needs at draw time, and
|
||||
// the worker's recheck-under-lock pattern just observes the
|
||||
// filled slot and moves on. DestroyPipelines 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_Vulkan::ShaderCompileThreadEntryPoint, this);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void GPU_HW_Vulkan::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_Vulkan::ShaderCompileThreadEntryPoint()
|
||||
{
|
||||
// Same nesting as the Enabled-mode precompile loop above. The quit
|
||||
// flag is checked between cells so DestroyPipelines can stop the
|
||||
// worker within at most one PSO compile of latency (Vulkan PSO
|
||||
// compiles can be ~50-200 ms with the heavier texture filters).
|
||||
for (u8 depth_test = 0; depth_test < 3; depth_test++)
|
||||
{
|
||||
for (u8 render_mode = 0; render_mode < 4; render_mode++)
|
||||
{
|
||||
for (u8 transparency_mode = 0; transparency_mode < 5; transparency_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;
|
||||
|
||||
GetBatchPipeline(depth_test, render_mode, texture_mode, transparency_mode,
|
||||
ConvertToBoolUnchecked(dithering), ConvertToBoolUnchecked(interlacing));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
VkShaderModule GPU_HW_Vulkan::GetBatchFragmentShader(u8 render_mode, u8 texture_mode, bool dithering, bool interlacing)
|
||||
{
|
||||
// Reserved_*Direct16Bit shader-source dedup, applied at the helper
|
||||
// entry. Slots for texture_mode 3 / 7 are never written; all
|
||||
// accesses route through the canonical slots 2 / 6. SafeDestroy*
|
||||
// on the dup slots in DestroyPipelines is a VK_NULL_HANDLE no-op.
|
||||
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
|
||||
// (GetBatchPipeline takes the lock; the Enabled-mode precompile
|
||||
// path is single-threaded).
|
||||
VkShaderModule& slot = m_batch_fragment_shaders[render_mode][lookup_mode][BoolToUInt8(dithering)][BoolToUInt8(interlacing)];
|
||||
|
||||
if (slot == VK_NULL_HANDLE)
|
||||
{
|
||||
if (!m_shadergen)
|
||||
{
|
||||
Log_ErrorPrint("GetBatchFragmentShader called before CompilePipelines constructed the shadergen");
|
||||
return VK_NULL_HANDLE;
|
||||
}
|
||||
const std::string fs = m_shadergen->GenerateBatchFragmentShader(
|
||||
static_cast<BatchRenderMode>(render_mode), static_cast<GPUTextureMode>(lookup_mode), dithering, interlacing);
|
||||
slot = g_vulkan_shader_cache->GetFragmentShader(fs);
|
||||
if (slot == VK_NULL_HANDLE)
|
||||
{
|
||||
Log_ErrorPrintf("Lazy batch fragment shader compile failed for (rm=%u, tm=%u, d=%u, i=%u)", render_mode,
|
||||
texture_mode, BoolToUInt8(dithering), BoolToUInt8(interlacing));
|
||||
return VK_NULL_HANDLE;
|
||||
}
|
||||
}
|
||||
|
||||
return slot;
|
||||
}
|
||||
|
||||
VkPipeline GPU_HW_Vulkan::GetBatchPipeline(u8 depth_test, u8 render_mode, u8 texture_mode, u8 transparency_mode,
|
||||
bool dithering, bool interlacing)
|
||||
{
|
||||
// Reserved_*Direct16Bit PSO dedup, applied at the helper entry.
|
||||
// Slots for texture_mode 3 / 7 are never written; all accesses
|
||||
// route through the canonical slots 2 / 6.
|
||||
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);
|
||||
|
||||
VkPipeline& slot = m_batch_pipelines[depth_test][render_mode][lookup_mode][transparency_mode][BoolToUInt8(dithering)]
|
||||
[BoolToUInt8(interlacing)];
|
||||
|
||||
if (slot != VK_NULL_HANDLE)
|
||||
return slot;
|
||||
|
||||
// Need the bound fragment shader first.
|
||||
VkShaderModule fs = GetBatchFragmentShader(render_mode, lookup_mode, dithering, interlacing);
|
||||
if (fs == VK_NULL_HANDLE)
|
||||
return VK_NULL_HANDLE;
|
||||
|
||||
static constexpr std::array<VkCompareOp, 3> depth_test_values = {
|
||||
VK_COMPARE_OP_ALWAYS, VK_COMPARE_OP_GREATER_OR_EQUAL, VK_COMPARE_OP_LESS_OR_EQUAL};
|
||||
const bool textured = (static_cast<GPUTextureMode>(lookup_mode) != GPUTextureMode::Disabled);
|
||||
|
||||
Vulkan::GraphicsPipelineBuilder gpbuilder;
|
||||
gpbuilder.SetPipelineLayout(m_batch_pipeline_layout);
|
||||
gpbuilder.SetRenderPass(m_vram_render_pass, 0);
|
||||
|
||||
gpbuilder.AddVertexBuffer(0, sizeof(BatchVertex), VK_VERTEX_INPUT_RATE_VERTEX);
|
||||
gpbuilder.AddVertexAttribute(0, 0, VK_FORMAT_R32G32B32A32_SFLOAT, offsetof(BatchVertex, x));
|
||||
gpbuilder.AddVertexAttribute(1, 0, VK_FORMAT_R8G8B8A8_UNORM, offsetof(BatchVertex, color));
|
||||
if (textured)
|
||||
{
|
||||
gpbuilder.AddVertexAttribute(2, 0, VK_FORMAT_R32_UINT, offsetof(BatchVertex, u));
|
||||
gpbuilder.AddVertexAttribute(3, 0, VK_FORMAT_R32_UINT, offsetof(BatchVertex, texpage));
|
||||
if (m_using_uv_limits)
|
||||
gpbuilder.AddVertexAttribute(4, 0, VK_FORMAT_R8G8B8A8_UNORM, offsetof(BatchVertex, uv_limits));
|
||||
}
|
||||
|
||||
gpbuilder.SetPrimitiveTopology(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST);
|
||||
gpbuilder.SetVertexShader(m_batch_vertex_shaders[BoolToUInt8(textured)]);
|
||||
gpbuilder.SetFragmentShader(fs);
|
||||
|
||||
gpbuilder.SetRasterizationState(VK_POLYGON_MODE_FILL, VK_CULL_MODE_NONE, VK_FRONT_FACE_CLOCKWISE);
|
||||
gpbuilder.SetDepthState(true, true, depth_test_values[depth_test]);
|
||||
gpbuilder.SetNoBlendingState();
|
||||
gpbuilder.SetMultisamples(m_multisamples, m_per_sample_shading);
|
||||
|
||||
if ((static_cast<GPUTransparencyMode>(transparency_mode) != GPUTransparencyMode::Disabled &&
|
||||
(static_cast<BatchRenderMode>(render_mode) != BatchRenderMode::TransparencyDisabled &&
|
||||
static_cast<BatchRenderMode>(render_mode) != BatchRenderMode::OnlyOpaque)) ||
|
||||
m_texture_filtering != GPUTextureFilter::Nearest)
|
||||
{
|
||||
if (m_supports_dual_source_blend)
|
||||
{
|
||||
gpbuilder.SetBlendAttachment(
|
||||
0, true, VK_BLEND_FACTOR_ONE, VK_BLEND_FACTOR_SRC1_ALPHA,
|
||||
(static_cast<GPUTransparencyMode>(transparency_mode) == GPUTransparencyMode::BackgroundMinusForeground &&
|
||||
static_cast<BatchRenderMode>(render_mode) != BatchRenderMode::TransparencyDisabled &&
|
||||
static_cast<BatchRenderMode>(render_mode) != BatchRenderMode::OnlyOpaque) ?
|
||||
VK_BLEND_OP_REVERSE_SUBTRACT :
|
||||
VK_BLEND_OP_ADD,
|
||||
VK_BLEND_FACTOR_ONE, VK_BLEND_FACTOR_ZERO, VK_BLEND_OP_ADD);
|
||||
}
|
||||
else
|
||||
{
|
||||
const float factor =
|
||||
(static_cast<GPUTransparencyMode>(transparency_mode) == GPUTransparencyMode::HalfBackgroundPlusHalfForeground) ?
|
||||
0.5f :
|
||||
1.0f;
|
||||
gpbuilder.SetBlendAttachment(
|
||||
0, true, VK_BLEND_FACTOR_ONE, VK_BLEND_FACTOR_CONSTANT_ALPHA,
|
||||
(static_cast<GPUTransparencyMode>(transparency_mode) == GPUTransparencyMode::BackgroundMinusForeground &&
|
||||
static_cast<BatchRenderMode>(render_mode) != BatchRenderMode::TransparencyDisabled &&
|
||||
static_cast<BatchRenderMode>(render_mode) != BatchRenderMode::OnlyOpaque) ?
|
||||
VK_BLEND_OP_REVERSE_SUBTRACT :
|
||||
VK_BLEND_OP_ADD,
|
||||
VK_BLEND_FACTOR_ONE, VK_BLEND_FACTOR_ZERO, VK_BLEND_OP_ADD);
|
||||
gpbuilder.SetBlendConstants(0.0f, 0.0f, 0.0f, factor);
|
||||
}
|
||||
}
|
||||
|
||||
gpbuilder.SetDynamicViewportAndScissorState();
|
||||
|
||||
VkPipeline pipeline = gpbuilder.Create(g_vulkan_context->GetDevice(), g_vulkan_shader_cache->GetPipelineCache());
|
||||
if (pipeline == VK_NULL_HANDLE)
|
||||
{
|
||||
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, BoolToUInt8(dithering), BoolToUInt8(interlacing));
|
||||
return VK_NULL_HANDLE;
|
||||
}
|
||||
|
||||
slot = pipeline;
|
||||
return slot;
|
||||
}
|
||||
|
||||
void GPU_HW_Vulkan::DestroyPipelines()
|
||||
{
|
||||
// Tear down the background-compile worker before destroying the
|
||||
// matrix - the worker writes into m_batch_fragment_shaders and
|
||||
// m_batch_pipelines, both of which we're about to enumerate-destroy.
|
||||
StopShaderCompileThread();
|
||||
m_shadergen.reset();
|
||||
|
||||
m_batch_pipelines.enumerate(Vulkan::Util::SafeDestroyPipeline);
|
||||
m_batch_fragment_shaders.enumerate(Vulkan::Util::SafeDestroyShaderModule);
|
||||
m_batch_vertex_shaders.enumerate(Vulkan::Util::SafeDestroyShaderModule);
|
||||
|
||||
m_vram_fill_pipelines.enumerate(Vulkan::Util::SafeDestroyPipeline);
|
||||
|
||||
|
|
@ -1996,11 +2144,17 @@ void GPU_HW_Vulkan::DrawBatchVertices(BatchRenderMode render_mode, u32 base_vert
|
|||
|
||||
VkCommandBuffer cmdbuf = g_vulkan_context->GetCurrentCommandBuffer();
|
||||
|
||||
// Fetch the batch PSO via the lazy helper. Behaviour matches the
|
||||
// D3D11 / D3D12 backends: Enabled mode pre-fills every slot at
|
||||
// CompilePipelines time so this is a fast mutex-protected pointer
|
||||
// load, Lazy mode either grabs the worker's result or compiles on
|
||||
// the main thread on race, Disabled compiles on every first use.
|
||||
//
|
||||
// [depth_test][render_mode][texture_mode][transparency_mode][dithering][interlacing]
|
||||
const u8 depth_test = m_batch.use_depth_buffer ? static_cast<u8>(2) : BoolToUInt8(m_batch.check_mask_before_draw);
|
||||
VkPipeline pipeline =
|
||||
m_batch_pipelines[depth_test][static_cast<u8>(render_mode)][static_cast<u8>(m_batch.texture_mode)][static_cast<u8>(
|
||||
m_batch.transparency_mode)][BoolToUInt8(m_batch.dithering)][BoolToUInt8(m_batch.interlacing)];
|
||||
GetBatchPipeline(depth_test, static_cast<u8>(render_mode), static_cast<u8>(m_batch.texture_mode),
|
||||
static_cast<u8>(m_batch.transparency_mode), m_batch.dithering, m_batch.interlacing);
|
||||
|
||||
vkCmdBindPipeline(cmdbuf, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
|
||||
vkCmdDraw(cmdbuf, num_vertices, 1, base_vertex, 0);
|
||||
|
|
|
|||
|
|
@ -5,9 +5,13 @@
|
|||
#include "common/vulkan/texture.h"
|
||||
#include "core/host_display.h"
|
||||
#include "gpu_hw.h"
|
||||
#include "gpu_hw_shadergen.h"
|
||||
#include "texture_replacements.h"
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <tuple>
|
||||
#include <libretro.h>
|
||||
|
||||
|
|
@ -146,6 +150,35 @@ private:
|
|||
bool CompilePipelines();
|
||||
void DestroyPipelines();
|
||||
|
||||
// Lazy batch-fragment-shader-module + PSO compile path. Same
|
||||
// three-mode (Disabled / Enabled / Lazy) plumbing as the D3D11
|
||||
// and D3D12 backends; see GPUShaderPrecompileMode in core/types.h
|
||||
// for the user-facing semantics.
|
||||
//
|
||||
// Unlike D3D11 / D3D12, Vulkan can't share a refcounted resource
|
||||
// wrapper between dispatch matrix slots, because both
|
||||
// VkShaderModule and VkPipeline are raw handles destroyed via
|
||||
// SafeDestroyShaderModule / SafeDestroyPipeline - two slots
|
||||
// holding the same handle would double-call vkDestroy*. So the
|
||||
// Reserved_*Direct16Bit dedup is applied at the *helper entry*
|
||||
// (texture_mode 3 -> 2 and 7 -> 6 before the array index is
|
||||
// computed) rather than by populating both slots. The dup slots
|
||||
// for texture_mode 3 / 7 remain VK_NULL_HANDLE for the lifetime
|
||||
// of the GPU backend; SafeDestroy* handles VK_NULL_HANDLE
|
||||
// gracefully so DestroyPipelines doesn't trip on them.
|
||||
VkShaderModule GetBatchFragmentShader(u8 render_mode, u8 texture_mode, bool dithering, bool interlacing);
|
||||
VkPipeline GetBatchPipeline(u8 depth_test, u8 render_mode, u8 texture_mode, u8 transparency_mode, bool dithering,
|
||||
bool interlacing);
|
||||
|
||||
// Background-thread worker for 'Lazy' mode: walks the full PSO
|
||||
// matrix and calls GetBatchPipeline on each cell. Main thread
|
||||
// can race ahead and fault in any slot it actually needs at draw
|
||||
// time; the worker observes the filled slot under the lock and
|
||||
// moves on. Quit flag checked between cells so DestroyPipelines
|
||||
// can stop the worker within at most one PSO compile of latency.
|
||||
void ShaderCompileThreadEntryPoint();
|
||||
void StopShaderCompileThread();
|
||||
|
||||
bool CreateTextureReplacementStreamBuffer();
|
||||
|
||||
bool BlitVRAMReplacementTexture(const TextureReplacementTexture* tex, u32 dst_x, u32 dst_y, u32 width, u32 height);
|
||||
|
|
@ -204,6 +237,34 @@ private:
|
|||
// [depth_test][render_mode][texture_mode][transparency_mode][dithering][interlacing]
|
||||
DimensionalArray<VkPipeline, 2, 2, 5, 9, 4, 3> m_batch_pipelines{};
|
||||
|
||||
// Persistent vertex / fragment shader modules and shadergen for
|
||||
// the lazy and background-thread compile paths. These used to be
|
||||
// locals in CompilePipelines and were leaked on success; now they
|
||||
// live for the lifetime of this GPU backend so the lazy PSO
|
||||
// builder can reach them at draw time, and DestroyPipelines tears
|
||||
// them down properly via SafeDestroyShaderModule.
|
||||
//
|
||||
// Dup slots for texture_mode 3 / 7 stay VK_NULL_HANDLE; the
|
||||
// helper-entry remap (see GetBatchFragmentShader) routes all
|
||||
// accesses through the canonical slots 2 / 6.
|
||||
//
|
||||
// m_batch_shader_mutex serialises:
|
||||
// - g_vulkan_shader_cache mutations (its unordered_map indices
|
||||
// aren't thread-safe on insert)
|
||||
// - the VkPipelineCache passed to vkCreateGraphicsPipelines
|
||||
// (Vulkan spec: must be externally synchronised when shared
|
||||
// across threads)
|
||||
// - the m_batch_fragment_shaders / m_batch_pipelines matrix
|
||||
// writes
|
||||
// Fast path: one uncontended mutex lock per DrawBatchVertices.
|
||||
std::mutex m_batch_shader_mutex;
|
||||
std::unique_ptr<GPU_HW_ShaderGen> m_shadergen;
|
||||
std::thread m_shader_compile_thread;
|
||||
std::atomic<bool> m_shader_compile_thread_quit{false};
|
||||
|
||||
DimensionalArray<VkShaderModule, 2> m_batch_vertex_shaders{}; // [textured]
|
||||
DimensionalArray<VkShaderModule, 2, 2, 9, 4> m_batch_fragment_shaders{}; // [render][texture][dither][interlace]
|
||||
|
||||
// [wrapped][interlaced]
|
||||
DimensionalArray<VkPipeline, 2, 2> m_vram_fill_pipelines{};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue