diff --git a/src/core/gpu_hw_d3d11.cpp b/src/core/gpu_hw_d3d11.cpp index 5912b579..fe47cfc2 100644 --- a/src/core/gpu_hw_d3d11.cpp +++ b/src/core/gpu_hw_d3d11.cpp @@ -989,10 +989,37 @@ bool GPU_HW_D3D11::CompileShaders() { for (u8 texture_mode = 0; texture_mode < 9; texture_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(GPUTextureMode::Reserved_Direct16Bit)) ? 2u : + (texture_mode == static_cast(GPUTextureMode::Reserved_RawDirect16Bit)) ? 6u : + texture_mode; + for (u8 dithering = 0; dithering < 2; dithering++) { for (u8 interlacing = 0; interlacing < 2; interlacing++) { + if (source_mode != texture_mode) + { + m_batch_pixel_shaders[render_mode][texture_mode][dithering][interlacing] = + m_batch_pixel_shaders[render_mode][source_mode][dithering][interlacing]; + progress.Increment(); + continue; + } + const std::string ps = shadergen.GenerateBatchFragmentShader( static_cast(render_mode), static_cast(texture_mode), ConvertToBoolUnchecked(dithering), ConvertToBoolUnchecked(interlacing)); diff --git a/src/core/gpu_hw_d3d12.cpp b/src/core/gpu_hw_d3d12.cpp index 64e2a745..f2201e3a 100644 --- a/src/core/gpu_hw_d3d12.cpp +++ b/src/core/gpu_hw_d3d12.cpp @@ -434,10 +434,27 @@ bool GPU_HW_D3D12::CompilePipelines() { for (u8 texture_mode = 0; texture_mode < 9; texture_mode++) { + // See the equivalent comment in GPU_HW_D3D11::CompileShaders: + // Reserved_*Direct16Bit produce sources identical to the + // non-reserved variants after macro expansion. Skip the + // regeneration and the cache-key MD5 by copying the compiled + // blob pointer directly. + const u8 source_mode = (texture_mode == static_cast(GPUTextureMode::Reserved_Direct16Bit)) ? 2u : + (texture_mode == static_cast(GPUTextureMode::Reserved_RawDirect16Bit)) ? 6u : + texture_mode; + for (u8 dithering = 0; dithering < 2; dithering++) { for (u8 interlacing = 0; interlacing < 2; interlacing++) { + if (source_mode != texture_mode) + { + batch_fragment_shaders[render_mode][texture_mode][dithering][interlacing] = + batch_fragment_shaders[render_mode][source_mode][dithering][interlacing]; + progress.Increment(); + continue; + } + const std::string fs = shadergen.GenerateBatchFragmentShader( static_cast(render_mode), static_cast(texture_mode), ConvertToBoolUnchecked(dithering), ConvertToBoolUnchecked(interlacing)); @@ -463,10 +480,30 @@ bool GPU_HW_D3D12::CompilePipelines() { for (u8 texture_mode = 0; texture_mode < 9; texture_mode++) { + // See the comment on the shader-compile loop above: the + // Reserved_*Direct16Bit shaders are bit-identical to their + // non-reserved variants. Since the PSO inputs (bound shader, + // vertex attributes, render-target format, blend / depth / + // raster state) don't depend on texture_mode except through + // the bound shader, the resulting PSO is also identical. + // Copy the ComPtr to inherit the reference instead of + // building a duplicate PSO. + const u8 source_mode = (texture_mode == static_cast(GPUTextureMode::Reserved_Direct16Bit)) ? 2u : + (texture_mode == static_cast(GPUTextureMode::Reserved_RawDirect16Bit)) ? 6u : + texture_mode; + for (u8 dithering = 0; dithering < 2; dithering++) { for (u8 interlacing = 0; interlacing < 2; interlacing++) { + if (source_mode != texture_mode) + { + m_batch_pipelines[depth_test][render_mode][texture_mode][transparency_mode][dithering][interlacing] = + m_batch_pipelines[depth_test][render_mode][source_mode][transparency_mode][dithering][interlacing]; + progress.Increment(); + continue; + } + const bool textured = (static_cast(texture_mode) != GPUTextureMode::Disabled); gpbuilder.SetRootSignature(m_batch_root_signature.Get()); diff --git a/src/core/gpu_hw_opengl.cpp b/src/core/gpu_hw_opengl.cpp index d5cea33a..5483e64f 100644 --- a/src/core/gpu_hw_opengl.cpp +++ b/src/core/gpu_hw_opengl.cpp @@ -1082,14 +1082,33 @@ bool GPU_HW_OpenGL::CompilePrograms() { for (u32 texture_mode = 0; texture_mode < 9; texture_mode++) { + // Reserved_Direct16Bit (3) and Reserved_RawDirect16Bit (7) produce + // shaders identical to Direct16Bit (2) and RawDirect16Bit (6) - see + // GPU_HW_D3D11::CompileShaders for the full explanation. For + // OpenGL we can't simply copy the resulting GL::Program object + // because it's move-only (it owns a GLuint program ID that the + // destructor will glDeleteProgram), so we have to re-link the + // program. We can, however, skip the ~50ms shader-source + // regeneration in GenerateBatchFragmentShader by re-using the + // already-built source string from the previous (source_mode) + // iteration. The shader cache will hit on the second GetProgram + // call and reload the binary instead of recompiling. + const u32 source_mode = (texture_mode == static_cast(GPUTextureMode::Reserved_Direct16Bit)) ? 2u : + (texture_mode == static_cast(GPUTextureMode::Reserved_RawDirect16Bit)) ? 6u : + texture_mode; + for (u8 dithering = 0; dithering < 2; dithering++) { for (u8 interlacing = 0; interlacing < 2; interlacing++) { const bool textured = (static_cast(texture_mode) != GPUTextureMode::Disabled); const std::string batch_vs = shadergen.GenerateBatchVertexShader(textured); + // If we're at a Reserved_* texture_mode, build a fragment + // shader source for the equivalent non-reserved mode. The + // post-MD5 cache dedup will then short-circuit the GL + // compile/link work too. const std::string fs = shadergen.GenerateBatchFragmentShader( - static_cast(render_mode), static_cast(texture_mode), + static_cast(render_mode), static_cast(source_mode), ConvertToBoolUnchecked(dithering), ConvertToBoolUnchecked(interlacing)); const auto link_callback = [this, textured, use_binding_layout](GL::Program& prog) { diff --git a/src/core/gpu_hw_vulkan.cpp b/src/core/gpu_hw_vulkan.cpp index bdd480a5..ca7ef499 100644 --- a/src/core/gpu_hw_vulkan.cpp +++ b/src/core/gpu_hw_vulkan.cpp @@ -1482,12 +1482,32 @@ bool GPU_HW_Vulkan::CompilePipelines() { 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(GPUTextureMode::Reserved_Direct16Bit)) ? 2u : + (texture_mode == static_cast(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(render_mode), static_cast(texture_mode), + static_cast(render_mode), static_cast(source_mode), ConvertToBoolUnchecked(dithering), ConvertToBoolUnchecked(interlacing)); VkShaderModule shader = g_vulkan_shader_cache->GetFragmentShader(fs);