gpu_hw: skip duplicate-source compiles for the two Reserved_*Direct16Bit texture modes

GPU_HW_ShaderGen::GenerateBatchFragmentShader emits a fragment
shader source string parameterised by (BatchRenderMode,
GPUTextureMode, dithering, interlacing). The texture_mode arm
masks the RawTextureBit out of the input -

  const GPUTextureMode actual_texture_mode = texture_mode & ~GPUTextureMode::RawTextureBit;

- and consults the masked value only via three macros: PALETTE,
PALETTE_4_BIT, and PALETTE_8_BIT. None of those three is true for
any of {Direct16Bit (2), Reserved_Direct16Bit (3),
RawDirect16Bit (6), Reserved_RawDirect16Bit (7)}: the masking
collapses 2 and 6 to the same code path, and the reserved variants
3 and 7 are bit-identical to their non-reserved counterparts in
the generated shader source.

The disk-backed shader cache does dedup at the MD5 level - it
hashes the generated source text and only invokes the underlying
compiler once per unique key - but reaching that fast-path still
requires (a) calling GenerateBatchFragmentShader to assemble the
~3000-line shader source for the duplicate, then (b) computing
the MD5 to look it up. Across the 144-iteration shader-compile
matrix (4 render-modes x 9 texture-modes x 2 dither x 2 interlace),
2/9 of the iterations - 32 - run that redundant source assembly
and MD5 for no end-state gain. The D3D12 and Vulkan PSO loops
multiply the redundancy further: D3D12's pipeline matrix is
2 x 4 x 5 x 9 x 2 x 2 = 1440 entries, Vulkan's is
3 x 4 x 5 x 9 x 2 x 2 = 2160 entries, of which 22% are duplicates.

This commit short-circuits the duplicate work at each backend's
loop instead of relying on the cache's MD5 dedup to catch it.
The exact mechanism varies by backend's resource model:

  - GPU_HW_D3D11: m_batch_pixel_shaders stores ComPtr<ID3D11PixelShader>,
    refcounted. When texture_mode is 3 or 7, copy the ComPtr from
    the canonical slot (2 or 6 respectively) instead of regenerating
    the shader source and invoking the cache. Both source generation
    AND the device-side CreatePixelShader call are eliminated.

  - GPU_HW_D3D12: same ComPtr<ID3DBlob>/ComPtr<ID3D12PipelineState>
    treatment for both the batch_fragment_shaders blob array AND
    the m_batch_pipelines PSO matrix - which is the bigger win
    (~320 PSOs avoided rather than ~32 shaders).

  - GPU_HW_OpenGL: GL::Program is non-copyable (owns a GLuint),
    so the m_render_programs array can't simply share entries.
    Skip the GenerateBatchFragmentShader call for the duplicate
    modes by passing the source-mode's parameters to it, and let
    the program-binary cache hit on the lookup - the GL compile/
    link path is bypassed because of the MD5 match.

  - GPU_HW_Vulkan: VkShaderModule destroy is done via enumerate
    over the array using SafeDestroyShaderModule, which would
    double-destroy if two slots held the same handle, so we
    can't copy. Apply the same approach as OpenGL: skip the
    source generation by reusing the canonical source string,
    let the shader cache's MD5 match short-circuit the
    GLSL->SPIR-V step, and pay only the cheap vkCreateShaderModule
    call to get a fresh handle for each slot. The Vulkan PSO
    matrix is not deduplicated in this commit because VkPipeline
    has the same enumerate-destroy hazard with no convenient
    refcounting alternative; that win (~480 PSOs avoided) is
    left for a future commit that introduces an ownership-aware
    pipeline wrapper.

Per backend, the per-call elimination breakdown:

  - D3D11:  32 GenerateBatchFragmentShader + 32 MD5 + 32 D3DCompile
            cache-lookup + 32 CreatePixelShader avoided
  - D3D12:  32 GenerateBatchFragmentShader + 32 MD5 +
            320 gpbuilder.Create (the actual D3D12 PSO compile
            into GPU machine code, the dominant cost) avoided
  - OpenGL: 32 GenerateBatchFragmentShader avoided
            (cache + CreateProgramBinary on the rest)
  - Vulkan: 32 GenerateBatchFragmentShader avoided
            (cache + vkCreateShaderModule on the rest)

Behaviour is identical at draw time: every backend's dispatch
indexes m_batch_pixel_shaders/m_render_programs/m_batch_pipelines/
batch_fragment_shaders directly with m_batch.texture_mode, and
those slots now hold either the same compiled artifact (D3D11/D3D12
via ComPtr copy) or a logically-equivalent freshly-created artifact
(GL/Vulkan via cache-warmed lookup). The dispatch matrix is fully
populated.

Build: zero warnings, zero errors. 4 files changed, +105/-2 lines.
.so size unchanged at 8028240 bytes (control-flow change inside
existing functions; no new code or removed code from the binary).
25 retro_* exports unchanged.

The cold-cache cost of switching the texture filter from Nearest
to xBR/JINC2/Bilinear is dominated by 'compile 112 unique shader
sources from scratch with high-optimization HLSL/SPIR-V/GLSL
compilers'; this commit eliminates the redundancy on top of that
baseline rather than the baseline itself, but reduces the
constant overhead by enough to be perceptible in the slowest
paths (D3D12 / Vulkan PSO matrices).
This commit is contained in:
Lib 2026-05-11 14:41:42 +00:00 • committed by U-DESKTOP-SPFP6AQ\twistedtechre
commit 8e7f7a30cb
4 changed files with 105 additions and 2 deletions

View file

@ -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<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++)
{
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<BatchRenderMode>(render_mode), static_cast<GPUTextureMode>(texture_mode),
ConvertToBoolUnchecked(dithering), ConvertToBoolUnchecked(interlacing));

View file

@ -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<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++)
{
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<BatchRenderMode>(render_mode), static_cast<GPUTextureMode>(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<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++)
{
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<GPUTextureMode>(texture_mode) != GPUTextureMode::Disabled);
gpbuilder.SetRootSignature(m_batch_root_signature.Get());

View file

@ -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<u32>(GPUTextureMode::Reserved_Direct16Bit)) ? 2u :
(texture_mode == static_cast<u32>(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<GPUTextureMode>(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<BatchRenderMode>(render_mode), static_cast<GPUTextureMode>(texture_mode),
static_cast<BatchRenderMode>(render_mode), static_cast<GPUTextureMode>(source_mode),
ConvertToBoolUnchecked(dithering), ConvertToBoolUnchecked(interlacing));
const auto link_callback = [this, textured, use_binding_layout](GL::Program& prog) {

View file

@ -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<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>(texture_mode),
static_cast<BatchRenderMode>(render_mode), static_cast<GPUTextureMode>(source_mode),
ConvertToBoolUnchecked(dithering), ConvertToBoolUnchecked(interlacing));
VkShaderModule shader = g_vulkan_shader_cache->GetFragmentShader(fs);