core, common: route resolution_scale, true_color, scaled_dithering through batch cbuffer

Three per-session settings used to be baked into the HLSL / GLSL
source string at shadergen time:

  - RESOLUTION_SCALE: emitted as 'CONSTANT uint RESOLUTION_SCALE =
    N u;' at the top of every batch shader. VRAM_SIZE / RCP_VRAM_SIZE
    derived from it inline. Every distinct resolution scale produced
    a distinct source string -> distinct DXBC / GLSL blob.
  - TRUE_COLOR: DefineMacro'd as a #define, guarded around #if /
    #if ! / #else / #endif blocks in ApplyDithering, the textured
    colour-truncation path, the untextured non-DITHERING fallback,
    and the final colour premultiply. Two distinct source strings
    per shader cell (true_color on / off).
  - DITHERING_SCALED: DefineMacro'd as a #define around the dither
    fragment-coordinate selector in ApplyDithering. Two distinct
    source strings (scaled_dithering on / off).

Net result: each batch fragment shader cell could produce up to
~5 (typical resolution scales) x 2 x 2 = 20 distinct DXBC blobs
across a session's settings space, every one of them requiring a
fresh D3DCompile invocation on first encounter. On D3D12 with
shader precompilation Enabled and a cold disk cache, the
combinatorial explosion is exactly the 'roughly one minute initial
startup' pain.

This patch moves the three settings out of the source string and
into the per-batch uniform buffer. The HLSL / GLSL produced by
GenerateBatchFragmentShader / GenerateBatchVertexShader becomes
INVARIANT under those three settings - a session-state toggle is
a single 4-byte cbuffer write picked up on the next FlushRender,
with no shader recompile, no PSO rebuild, no batch matrix flush.

Mechanically:

  - BatchUBOData (gpu_hw.h) grows by 16 bytes:
      uint32_t u_resolution_scale;
      uint32_t u_true_color;       // bool packed as uint32_t
      uint32_t u_scaled_dithering; // same
      uint32_t u_pad0;             // pad to 16-byte cbuffer row
    The new fields are written from m_resolution_scale /
    m_true_color / m_scaled_dithering in two places: GPU_HW::
    Initialize after the member values are set, and
    GPU_HW::UpdateHWSettings right after the same members are
    updated. Both sites set m_batch_ubo_dirty = true so the next
    FlushRender uploads.

  - WriteCommonFunctions (gpu_hw_shadergen.cpp) gains a
    'batch_uniform_buffer' parameter (default false). When true it
    skips the file-scope 'CONSTANT uint RESOLUTION_SCALE = ...',
    'CONSTANT uint2 VRAM_SIZE = ...', and 'CONSTANT float2
    RCP_VRAM_SIZE = ...' emissions; the cbuffer + macro alias path
    supplies them instead. Non-batch shaders (VRAM fill / copy /
    write / read / update-depth, display, downsample) continue
    using the file-scope CONSTANT path - they're a handful of
    shaders with a much smaller combinatorial space and don't
    benefit from the cbuffer indirection.

  - WriteBatchUniformBuffer adds the three new cbuffer fields and
    a u_pad0 entry, then emits three preprocessor aliases:
      #define RESOLUTION_SCALE u_resolution_scale
      #define VRAM_SIZE (uint2(VRAM_WIDTH u, VRAM_HEIGHT u) *
                         u_resolution_scale)
      #define RCP_VRAM_SIZE (float2(1.0, 1.0) / float2(VRAM_SIZE))
    Every existing reference in the shader body keeps working as-is
    via macro substitution. VRAM_WIDTH / VRAM_HEIGHT stay
    compile-time literals - they are fixed PSX geometry (1024 x
    512), not session state.

  - GenerateBatchVertexShader / GenerateBatchFragmentShader call
    WriteBatchUniformBuffer BEFORE WriteCommonFunctions. The
    ordering matters: fixYCoord (defined inside
    WriteCommonFunctions) references RCP_VRAM_SIZE and VRAM_SIZE,
    which after the cbuffer-aware refactor are macros expanding to
    expressions involving u_resolution_scale. u_resolution_scale
    must be declared (cbuffer emitted) before the macros are
    expanded in fixYCoord's body, otherwise the HLSL / GLSL
    single-pass parser sees an undeclared identifier.

  - GenerateBatchFragmentShader drops the DefineMacro calls for
    TRUE_COLOR and DITHERING_SCALED, and the four #if (!)TRUE_COLOR
    / #if DITHERING_SCALED preprocessor blocks become runtime
    'if (u_true_color != 0u)' / 'if (u_scaled_dithering != 0u)'
    branches. These are uniform-control-flow branches on
    cbuffer-uniform values - free on every modern GPU. The inner
    RAW_TEXTURE / DITHERING / etc. #ifs remain compile-time
    because those dimensions are still structural in the batch
    matrix.

  - Shader cache file format versions bumped on D3D11 (2 -> 3),
    D3D12 (1 -> 2), and OpenGL (3 -> 4). Existing on-disk caches
    were keyed by HLSL / GLSL source hashes that included the
    inlined RESOLUTION_SCALE / TRUE_COLOR / DITHERING_SCALED
    literals; after this patch the source no longer contains those
    literals, so the hashes change. Bumping the file version
    forces a clean rebuild of the cache file on first launch
    rather than leaving stale entries to grow the file
    indefinitely.

Vulkan is unaffected at runtime. The Vulkan backend uses the
pre-baked SPIR-V templates under data/shaders/vulkan/*.glsl,
which handle RESOLUTION_SCALE / TRUE_COLOR / DITHERING_SCALED via
VkSpecializationInfo spec constants (IDs 0, 106, 105). Those
templates are independent of GPU_HW_ShaderGen; the runtime path
patches the spec consts at vkCreateGraphicsPipelines time and the
result is the same architectural shape this patch gives D3D11 /
D3D12 / OpenGL via the cbuffer.

Smoke checks:
  - Tree-wide grep confirms no DefineMacro for TRUE_COLOR or
    DITHERING_SCALED survives in gpu_hw_shadergen.cpp; only one
    explanatory comment references them by name.
  - u_resolution_scale / u_true_color / u_scaled_dithering are
    referenced in gpu_hw_shadergen.cpp (the cbuffer declaration
    plus the runtime branch sites), gpu_hw.cpp (the two write
    paths in Initialize and UpdateHWSettings), and gpu_hw.h
    (the BatchUBOData struct definition).

Test priority:
  - D3D12, shader cache nuked. Boot a game at default settings
    (resolution scale 4x, true_color off, scaled_dithering on,
    no MSAA, Nearest). Initial precompile_sync runs - it still
    has to compile ~120 fragment shader sources once, but they
    are now invariant under the three migrated settings.
    Subsequent toggles of true_color or scaled_dithering should
    no longer trigger any D3DCompile.
  - Toggle resolution scale (1x -> 4x -> 2x -> ...). The HLSL
    source is identical across all scale values; the cache
    should hit every time after the first compile pass. The
    framebuffer is still rebuilt (textures resize), and PSOs are
    still rebuilt (driver bytecode-to-ISA pass), but no fresh
    D3DCompile invocations.
  - Toggle true_color on and off mid-session. Visually identical
    behaviour to before this patch (24-bit vs 16-bit framebuffer
    look). No precompile pause.
  - Toggle scaled_dithering on and off mid-session. Visually
    identical dithering pattern behaviour. No precompile pause.
  - Same battery on D3D11 and OpenGL backends.
  - Vulkan: no behavioural change expected (still uses pre-baked
    SPIR-V via spec consts). Confirm filter / true_color /
    scaled_dithering toggles continue to be instant on revisit
    via the dim cache from earlier work.

This is patch 1 of the D3D12 startup work. Patch 2 will add the
pre-baked DXBC infrastructure (regen_d3d12_dxbc.py-style offline
tool plus .inc tables) that pre-baked SPIR-V did for Vulkan in
the glslang elimination series. With the source invariance from
this patch, the pre-bake size is bounded to the structural
matrix (~120 blobs for the default-user MSAA=1 / no-per_sample /
no-color-perspective case) rather than the full per-session
explosion.
This commit is contained in:
LibretroAdmin 2026-05-18 02:57:30 +00:00 • committed by U-DESKTOP-SPFP6AQ\twistedtechre
commit 7b575a3f87
7 changed files with 118 additions and 38 deletions

View file

@ -38,7 +38,7 @@ public:
ComPtr<ID3D11PixelShader> GetPixelShader(ID3D11Device* device, std::string_view shader_code);
private:
static constexpr uint32_t FILE_VERSION = 2;
static constexpr uint32_t FILE_VERSION = 3;
struct CacheIndexKey
{

View file

@ -62,7 +62,7 @@ public:
ComPtr<ID3D12PipelineState> GetPipelineState(ID3D12Device* device, const D3D12_GRAPHICS_PIPELINE_STATE_DESC& desc);
private:
static constexpr uint32_t FILE_VERSION = 1;
static constexpr uint32_t FILE_VERSION = 2;
struct CacheIndexKey
{

View file

@ -35,7 +35,7 @@ public:
const std::string_view fragment_shader, const PreLinkCallback& callback = {});
private:
static constexpr uint32_t FILE_VERSION = 3;
static constexpr uint32_t FILE_VERSION = 4;
struct CacheIndexKey
{

View file

@ -89,6 +89,13 @@ bool GPU_HW::Initialize(HostDisplay* host_display)
m_pgxp_depth_buffer = g_settings.UsingPGXPDepthBuffer();
// Seed the per-session cbuffer fields. m_batch_ubo_dirty defaults
// to true so the first FlushRender uploads these; subsequent
// sessions update them in UpdateHWSettings below.
m_batch_ubo_data.u_resolution_scale = m_resolution_scale;
m_batch_ubo_data.u_true_color = m_true_color ? 1u : 0u;
m_batch_ubo_data.u_scaled_dithering = m_scaled_dithering ? 1u : 0u;
UpdateSoftwareRenderer(false);
return true;
@ -206,6 +213,19 @@ void GPU_HW::UpdateHWSettings(bool* framebuffer_changed, bool* shaders_changed,
m_disable_color_perspective = disable_color_perspective;
m_shader_precompile_mode = g_settings.gpu_shader_precompile_mode;
// Push per-session cbuffer fields into the batch UBO. These three
// settings used to drive shader recompiles on every flip; they now
// ride the existing per-session UBO upload that already runs on
// m_batch_ubo_dirty. Setting the flag here ensures the next
// FlushRender picks up the new values before any draw uses them.
// Toggling these settings is effectively free at the shader cache
// level on D3D11 / D3D12 / OpenGL backends, and on Vulkan it
// reduces redundant spec const churn.
m_batch_ubo_data.u_resolution_scale = m_resolution_scale;
m_batch_ubo_data.u_true_color = m_true_color ? 1u : 0u;
m_batch_ubo_data.u_scaled_dithering = m_scaled_dithering ? 1u : 0u;
m_batch_ubo_dirty = true;
if (!m_supports_dual_source_blend && TextureFilterRequiresDualSourceBlend(m_texture_filtering))
m_texture_filtering = GPUTextureFilter::Nearest;

View file

@ -116,6 +116,20 @@ protected:
float u_dst_alpha_factor;
uint32_t u_interlaced_displayed_field;
uint32_t u_set_mask_while_drawing;
// Per-session fields. These used to be baked into the HLSL/GLSL
// source at shadergen time via DefineMacro / CONSTANT-literal
// emission, which forced a fresh shader compile for every value
// (every resolution scale, every true_color flip, every
// scaled_dithering flip). Routing them through the batch
// uniform buffer instead means a toggle is a single 4-byte
// cbuffer write picked up on the next FlushRender - no DXBC
// recompile, no PSO rebuild, no batch matrix flush. Mirrors
// what Vulkan spec consts do for the same settings on the
// pre-baked SPIR-V path.
uint32_t u_resolution_scale;
uint32_t u_true_color;
uint32_t u_scaled_dithering;
uint32_t u_pad0; // pad to 16-byte cbuffer row
};
struct VRAMFillUBOData

View file

@ -15,13 +15,26 @@ GPU_HW_ShaderGen::GPU_HW_ShaderGen(HostDisplay::RenderAPI render_api, uint32_t r
GPU_HW_ShaderGen::~GPU_HW_ShaderGen() = default;
void GPU_HW_ShaderGen::WriteCommonFunctions(std::stringstream& ss)
void GPU_HW_ShaderGen::WriteCommonFunctions(std::stringstream& ss, bool batch_uniform_buffer)
{
DefineMacro(ss, "MULTISAMPLING", UsingMSAA());
ss << "CONSTANT uint RESOLUTION_SCALE = " << m_resolution_scale << "u;\n";
ss << "CONSTANT uint2 VRAM_SIZE = uint2(" << VRAM_WIDTH << ", " << VRAM_HEIGHT << ") * RESOLUTION_SCALE;\n";
ss << "CONSTANT float2 RCP_VRAM_SIZE = float2(1.0, 1.0) / float2(VRAM_SIZE);\n";
// RESOLUTION_SCALE / VRAM_SIZE / RCP_VRAM_SIZE are emitted either
// as file-scope literals (the legacy path - used for non-batch
// shaders that don't bind the batch UBO) or are derived from
// u_resolution_scale inside the batch UBO (the per-session path).
// The batch path lets the same DXBC / GLSL serve every resolution
// scale without recompile; the legacy path keeps the literal
// baking that non-batch shaders rely on. WriteBatchUniformBuffer
// emits the #defines for the batch path; we just skip the
// file-scope CONSTANT block here when it's going to.
if (!batch_uniform_buffer)
{
ss << "CONSTANT uint RESOLUTION_SCALE = " << m_resolution_scale << "u;\n";
ss << "CONSTANT uint2 VRAM_SIZE = uint2(" << VRAM_WIDTH << ", " << VRAM_HEIGHT << ") * RESOLUTION_SCALE;\n";
ss << "CONSTANT float2 RCP_VRAM_SIZE = float2(1.0, 1.0) / float2(VRAM_SIZE);\n";
}
ss << "CONSTANT uint MULTISAMPLES = " << m_multisamples << "u;\n";
ss << "CONSTANT bool PER_SAMPLE_SHADING = " << (m_per_sample_shading ? "true" : "false") << ";\n";
ss << R"(
@ -70,8 +83,19 @@ void GPU_HW_ShaderGen::WriteBatchUniformBuffer(std::stringstream& ss)
DeclareUniformBuffer(ss,
{"uint2 u_texture_window_and", "uint2 u_texture_window_or", "float u_src_alpha_factor",
"float u_dst_alpha_factor", "uint u_interlaced_displayed_field",
"bool u_set_mask_while_drawing"},
"bool u_set_mask_while_drawing", "uint u_resolution_scale", "uint u_true_color",
"uint u_scaled_dithering", "uint u_pad0"},
false);
// Alias the historical compile-time constants to their cbuffer-
// backed equivalents. Every existing reference in the shader body
// (RESOLUTION_SCALE, VRAM_SIZE, RCP_VRAM_SIZE) keeps working as-is;
// the preprocessor just substitutes them with arithmetic over
// u_resolution_scale. VRAM_WIDTH / VRAM_HEIGHT stay compile-time
// literals because they are fixed PSX geometry, not session state.
ss << "#define RESOLUTION_SCALE u_resolution_scale\n";
ss << "#define VRAM_SIZE (uint2(" << VRAM_WIDTH << "u, " << VRAM_HEIGHT << "u) * u_resolution_scale)\n";
ss << "#define RCP_VRAM_SIZE (float2(1.0, 1.0) / float2(VRAM_SIZE))\n";
}
std::string GPU_HW_ShaderGen::GenerateBatchVertexShader(bool textured)
@ -82,8 +106,14 @@ std::string GPU_HW_ShaderGen::GenerateBatchVertexShader(bool textured)
DefineMacro(ss, "UV_LIMITS", m_uv_limits);
DefineMacro(ss, "PGXP_DEPTH", m_pgxp_depth);
WriteCommonFunctions(ss);
// Order matters: WriteBatchUniformBuffer must emit before
// WriteCommonFunctions so the #define aliases for RESOLUTION_SCALE
// / VRAM_SIZE / RCP_VRAM_SIZE are in scope when fixYCoord (defined
// in WriteCommonFunctions) references them, and so that the
// resulting u_resolution_scale references resolve against an
// already-declared cbuffer member.
WriteBatchUniformBuffer(ss);
WriteCommonFunctions(ss, true);
ss << R"(
@ -687,16 +717,25 @@ std::string GPU_HW_ShaderGen::GenerateBatchFragmentShader(GPU_HW::BatchRenderMod
DefineMacro(ss, "PALETTE_8_BIT", actual_texture_mode == GPUTextureMode::Palette8Bit);
DefineMacro(ss, "RAW_TEXTURE", raw_texture);
DefineMacro(ss, "DITHERING", dithering);
DefineMacro(ss, "DITHERING_SCALED", m_scaled_dithering);
// DITHERING_SCALED and TRUE_COLOR used to live as compile-time
// #defines, baking a fresh shader compile for every flip of either.
// They are now routed through the batch UBO (u_scaled_dithering,
// u_true_color), checked at runtime via uniform-control-flow
// branches in the FS body. Toggling either is a single cbuffer
// write picked up on the next FlushRender, with no DXBC recompile
// and no PSO rebuild.
DefineMacro(ss, "INTERLACING", interlacing);
DefineMacro(ss, "TRUE_COLOR", m_true_color);
DefineMacro(ss, "TEXTURE_FILTERING", m_texture_filter != GPUTextureFilter::Nearest);
DefineMacro(ss, "UV_LIMITS", m_uv_limits);
DefineMacro(ss, "USE_DUAL_SOURCE", use_dual_source);
DefineMacro(ss, "PGXP_DEPTH", m_pgxp_depth);
WriteCommonFunctions(ss);
// Same ordering rule as in GenerateBatchVertexShader: cbuffer
// declaration before helpers, so RESOLUTION_SCALE / VRAM_SIZE /
// RCP_VRAM_SIZE are macro-aliased to u_resolution_scale before
// fixYCoord references them.
WriteBatchUniformBuffer(ss);
WriteCommonFunctions(ss, true);
DeclareTexture(ss, "samp0", 0);
if (m_glsl)
@ -717,18 +756,17 @@ std::string GPU_HW_ShaderGen::GenerateBatchFragmentShader(GPU_HW::BatchRenderMod
ss << R"(
uint3 ApplyDithering(uint2 coord, uint3 icol)
{
#if DITHERING_SCALED
uint2 fc = coord & uint2(3u, 3u);
#else
uint2 fc = (coord / uint2(RESOLUTION_SCALE, RESOLUTION_SCALE)) & uint2(3u, 3u);
#endif
uint2 fc;
if (u_scaled_dithering != 0u)
fc = coord & uint2(3u, 3u);
else
fc = (coord / uint2(RESOLUTION_SCALE, RESOLUTION_SCALE)) & uint2(3u, 3u);
int offset = s_dither_values[fc.y * 4u + fc.x];
#if !TRUE_COLOR
return uint3(clamp((int3(icol) + int3(offset, offset, offset)) >> 3, 0, 31));
#else
if (u_true_color != 0u)
return uint3(clamp(int3(icol) + int3(offset, offset, offset), 0, 255));
#endif
else
return uint3(clamp((int3(icol) + int3(offset, offset, offset)) >> 3, 0, 31));
}
#if TEXTURED
@ -874,17 +912,11 @@ float4 SampleFromVRAM(uint4 texpage, float2 coords)
semitransparent = (texcol.a >= 0.5);
// If not using true color, truncate the framebuffer colors to 5-bit.
#if !TRUE_COLOR
icolor = uint3(texcol.rgb * float3(255.0, 255.0, 255.0)) >> 3;
#if !RAW_TEXTURE
icolor = (icolor * vertcol) >> 4;
#if DITHERING
icolor = ApplyDithering(uint2(v_pos.xy), icolor);
#else
icolor = min(icolor >> 3, uint3(31u, 31u, 31u));
#endif
#endif
#else
// Runtime branch on u_true_color (was a compile-time #if). The
// inner RAW_TEXTURE / DITHERING #ifs stay compile-time because
// those are still structural in the shader matrix.
if (u_true_color != 0u)
{
icolor = uint3(texcol.rgb * float3(255.0, 255.0, 255.0));
#if !RAW_TEXTURE
icolor = (icolor * vertcol) >> 7;
@ -894,7 +926,19 @@ float4 SampleFromVRAM(uint4 texpage, float2 coords)
icolor = min(icolor, uint3(255u, 255u, 255u));
#endif
#endif
#endif
}
else
{
icolor = uint3(texcol.rgb * float3(255.0, 255.0, 255.0)) >> 3;
#if !RAW_TEXTURE
icolor = (icolor * vertcol) >> 4;
#if DITHERING
icolor = ApplyDithering(uint2(v_pos.xy), icolor);
#else
icolor = min(icolor >> 3, uint3(31u, 31u, 31u));
#endif
#endif
}
// Compute output alpha (mask bit)
oalpha = float(u_set_mask_while_drawing ? 1 : int(semitransparent));
@ -907,9 +951,8 @@ float4 SampleFromVRAM(uint4 texpage, float2 coords)
#if DITHERING
icolor = ApplyDithering(uint2(v_pos.xy), icolor);
#else
#if !TRUE_COLOR
if (u_true_color == 0u)
icolor >>= 3;
#endif
#endif
// However, the mask bit is cleared if set mask bit is false.
@ -923,14 +966,17 @@ float4 SampleFromVRAM(uint4 texpage, float2 coords)
#endif
float3 color;
#if !TRUE_COLOR
if (u_true_color != 0u)
{
// True color is actually simpler here since we want to preserve the precision.
color = (float3(icolor) * premultiply_alpha) / float3(255.0, 255.0, 255.0);
}
else
{
// We want to apply the alpha before the truncation to 16-bit, otherwise we'll be passing a 32-bit precision color
// into the blend unit, which can cause a small amount of error to accumulate.
color = floor(float3(icolor) * premultiply_alpha) / float3(31.0, 31.0, 31.0);
#else
// True color is actually simpler here since we want to preserve the precision.
color = (float3(icolor) * premultiply_alpha) / float3(255.0, 255.0, 255.0);
#endif
}
#if TRANSPARENCY && TEXTURED
// Apply semitransparency. If not a semitransparent texel, destination alpha is ignored.

View file

@ -30,7 +30,7 @@ private:
ALWAYS_INLINE bool UsingMSAA() const { return m_multisamples > 1; }
ALWAYS_INLINE bool UsingPerSampleShading() const { return m_multisamples > 1 && m_per_sample_shading; }
void WriteCommonFunctions(std::stringstream& ss);
void WriteCommonFunctions(std::stringstream& ss, bool batch_uniform_buffer = false);
void WriteBatchUniformBuffer(std::stringstream& ss);
void WriteBatchTextureFilter(std::stringstream& ss, GPUTextureFilter texture_filter);