From e64fc28a7fb3bb0dd43524dbcd8ee43bc0909ea3 Mon Sep 17 00:00:00 2001 From: LibretroAdmin Date: Mon, 18 May 2026 19:35:10 +0000 Subject: [PATCH] core: route RESOLUTION_SCALE through display_ps cbuffer Last shader in the non-batch RESOLUTION_SCALE arc. Same refactor as e56d4d4 / 9d2b49d / 2980961, applied to display_ps - the fragment shader that composites the upscaled VRAM texture into the host display framebuffer every frame. display_ps body uses RESOLUTION_SCALE / VRAM_SIZE in two distinct spots driven by the DEPTH_24BIT preprocessor variant: * 24-bit colour path (FMV playback, true-colour framebuffer games): SampleVRAM24 helper multiplies native VRAM coords by RESOLUTION_SCALE to map into the upscaled VRAM texture: LoadVRAM(int2((vram_coords % clamp_size) * RESOLUTION_SCALE)) Hit twice per output pixel (left + right 16-bit packed texel pair). * 16-bit colour path (common case, every game's standard framebuffer present): VRAM_SIZE used for upscaled-coord modulo wrap: LoadVRAM(int2((icoords + u_vram_offset) % VRAM_SIZE)) Both expand to the same #define aliases as u_resolution_scale- derived arithmetic after this commit. Per-pixel cost difference vs the literal-baked version is negligible - the multiplication with a cbuffer-loaded uint operates at the same throughput as with a constant, and the modulo wrap was always a real ALU op not a folded constant. Subtle but important: this commit pushes m_resolution_scale to u_resolution_scale, NOT the local `resolution_scale` variable in each backend's UpdateDisplay scope. The local is const uint32_t resolution_scale = m_GPUSTAT.display_area_color_depth_24 ? 1 : m_resolution_scale; which forces 1 in 24-bit mode to keep u_vram_offset / u_crop_left in NATIVE coords (the 24-bit display path runs the viewport at native size, not upscaled - see scaled_display_width = display_width * resolution_scale at the call site). The shader's RESOLUTION_SCALE macro has always been the session m_resolution_scale because SampleVRAM24's `vram_coords * RESOLUTION_SCALE` needs to map native VRAM coords into the upscaled VRAM texture regardless of how the display target itself is sized. Pushing the local resolution_scale (=1 in 24-bit) would break SampleVRAM24's upscale mapping and produce wrong 24-bit colour output (would sample VRAM at native coords instead of upscaled ones). Files: * gpu_hw_shadergen.cpp: GenerateDisplayFragmentShader reordered so DeclareUniformBuffer runs before WriteCommonFunctions(ss, true). u_resolution_scale + u_pad0 appended to the cbuffer member list (after u_field_offset). WriteCBufferResolutionScaleAliases call inserted between cbuffer and WriteCommonFunctions. * gpu_hw_d3d11.cpp / gpu_hw_d3d12.cpp / gpu_hw_vulkan.cpp / gpu_hw_opengl.cpp: each backend's UpdateDisplay constructs an inline uniforms[] array at its present site. All four grow from uniforms[4] (u_vram_offset.xy + u_crop_left + u_field_offset) to uniforms[6] (adding u_resolution_scale + u_pad0). D3D11 / D3D12 / Vulkan share the same form; OpenGL keeps its scaled_flipped_vram_offset_y for lower-left origin handling and gets m_resolution_scale + u_pad0 appended in parallel. Push-constants ceiling check: 6 DWORDs = 24 bytes, well under MAX_PUSH_CONSTANTS_SIZE = 64 on both D3D12 and Vulkan. Variant axis dropped: display_ps had depth_24bit (2) x interlace_mode (3) x smooth_chroma (2) x MSAA (M) x m_resolution_scale (N) variants. After this commit it's 2 x 3 x 2 x M = 12 x M variants, with the scale dimension fully gone. Pre-bake for display_ps becomes viable in a follow-up - 12 blobs (or 12 x M with MSAA still as a variant axis), which is manageable. Multi-variant pre-bake exercises TEMPLATE_VARIANTS in regen_d3d12_dxbc.py for the first time. Shader-cache invalidation: display_ps's source string changes on every backend. First boot after this lands re-compiles all 12 (depth_24 x interlace x smooth_chroma) variants once for each MSAA count the user has enabled; subsequent boots hit the regenerated cache entries. Test: - Build under each backend succeeds. - Render a normal 16-bit framebuffer game (any normal title) at every resolution scale (1x, 2x, 4x, 8x). The frame composite uses the 16-bit path's `% VRAM_SIZE` for wrap- around handling - if u_resolution_scale weren't populated correctly, the modulo would be off by a factor and the presented image would either be the wrong region of VRAM or wrap onto itself with visible tearing. Visible immediately. - Render a 24-bit framebuffer source (FMV playback in any game with pre-rendered videos - the bookend Sony intro, FF7 cutscenes, MGS pre-render scenes, etc.) at every resolution scale. The SampleVRAM24 path's `vram_coords * RESOLUTION_SCALE` is the heaviest user; if it weren't populated, FMVs would show garbled pixels sourced from the wrong upscaled VRAM location. - Toggle resolution scale at runtime (1x -> 2x -> 4x -> 1x). Each switch triggers an UpdateDisplay; if the cbuffer field didn't track the change, the display would freeze on the previous scale's coords or render at wrong size. This concludes the non-batch RESOLUTION_SCALE refactor arc. After this lands, every non-batch shader that referenced RESOLUTION_SCALE or VRAM_SIZE in its body now reads u_resolution_scale from its own cbuffer, identical pattern to what 7b575a3 did for batch shaders. The only remaining literal-RESOLUTION_SCALE consumers are the adaptive_downsample_* shader family, which is a separate concern - those shaders take a u_rcp_resolution cbuffer field already, and they use RCP_VRAM_SIZE differently (for mip-level sampling derivatives, not just coord scaling), so they'd need a slightly different refactor pattern. Next on the pre-bake side: with the scale axis removed from vram_copy / vram_write / vram_read / display, each one becomes a candidate for offline DXBC pre-bake. Smallest first: vram_update_depth (2 MSAA variants, body has no RESOLUTION_SCALE reference so doesn't need this refactor at all) is the natural TEMPLATE_VARIANTS exercise. --- src/core/gpu_hw_d3d11.cpp | 11 +++++++++-- src/core/gpu_hw_d3d12.cpp | 11 +++++++++-- src/core/gpu_hw_opengl.cpp | 11 +++++++++-- src/core/gpu_hw_shadergen.cpp | 24 ++++++++++++++++++++++-- src/core/gpu_hw_vulkan.cpp | 11 +++++++++-- 5 files changed, 58 insertions(+), 10 deletions(-) diff --git a/src/core/gpu_hw_d3d11.cpp b/src/core/gpu_hw_d3d11.cpp index d28fa949..10d96810 100644 --- a/src/core/gpu_hw_d3d11.cpp +++ b/src/core/gpu_hw_d3d11.cpp @@ -1773,8 +1773,15 @@ void GPU_HW_D3D11::UpdateDisplay() const uint32_t reinterpret_field_offset = (interlaced != InterlacedRenderMode::None) ? GetInterlacedDisplayField() : 0; const uint32_t reinterpret_start_x = m_crtc_state.regs.X * resolution_scale; const uint32_t reinterpret_crop_left = (m_crtc_state.display_vram_left - m_crtc_state.regs.X) * resolution_scale; - const uint32_t uniforms[4] = {reinterpret_start_x, scaled_vram_offset_y + reinterpret_field_offset, - reinterpret_crop_left, reinterpret_field_offset}; + // 6 DWORDs to match the post-RESOLUTION_SCALE-refactor display_ps + // cbuffer (u_vram_offset.xy, u_crop_left, u_field_offset, + // u_resolution_scale, u_pad0). m_resolution_scale is pushed, + // NOT the local resolution_scale (which is forced to 1 in + // 24-bit mode for coord scaling) - the shader's RESOLUTION_SCALE + // macro has always been the session m_resolution_scale. + const uint32_t uniforms[6] = {reinterpret_start_x, scaled_vram_offset_y + reinterpret_field_offset, + reinterpret_crop_left, reinterpret_field_offset, + m_resolution_scale, 0u /* u_pad0 */}; ID3D11PixelShader* display_pixel_shader = m_display_pixel_shaders[static_cast(m_GPUSTAT.display_area_color_depth_24)][static_cast(interlaced)].Get(); diff --git a/src/core/gpu_hw_d3d12.cpp b/src/core/gpu_hw_d3d12.cpp index 01d6fd08..4a137876 100644 --- a/src/core/gpu_hw_d3d12.cpp +++ b/src/core/gpu_hw_d3d12.cpp @@ -2067,8 +2067,15 @@ void GPU_HW_D3D12::UpdateDisplay() const uint32_t reinterpret_field_offset = (interlaced != InterlacedRenderMode::None) ? GetInterlacedDisplayField() : 0; const uint32_t reinterpret_start_x = m_crtc_state.regs.X * resolution_scale; const uint32_t reinterpret_crop_left = (m_crtc_state.display_vram_left - m_crtc_state.regs.X) * resolution_scale; - const uint32_t uniforms[4] = {reinterpret_start_x, scaled_vram_offset_y + reinterpret_field_offset, - reinterpret_crop_left, reinterpret_field_offset}; + // 6 DWORDs to match the post-RESOLUTION_SCALE-refactor display_ps + // cbuffer (u_vram_offset.xy, u_crop_left, u_field_offset, + // u_resolution_scale, u_pad0). m_resolution_scale is pushed, + // NOT the local resolution_scale (which is forced to 1 in + // 24-bit mode for coord scaling) - the shader's RESOLUTION_SCALE + // macro has always been the session m_resolution_scale. + const uint32_t uniforms[6] = {reinterpret_start_x, scaled_vram_offset_y + reinterpret_field_offset, + reinterpret_crop_left, reinterpret_field_offset, + m_resolution_scale, 0u /* u_pad0 */}; ID3D12GraphicsCommandList* cmdlist = g_d3d12_context->GetCommandList(); m_display_texture.TransitionToState(D3D12_RESOURCE_STATE_RENDER_TARGET); diff --git a/src/core/gpu_hw_opengl.cpp b/src/core/gpu_hw_opengl.cpp index 4c00c97d..ef6a1976 100644 --- a/src/core/gpu_hw_opengl.cpp +++ b/src/core/gpu_hw_opengl.cpp @@ -1756,8 +1756,15 @@ void GPU_HW_OpenGL::UpdateDisplay() reinterpret_field_offset - (scaled_display_height >> height_div2); const uint32_t reinterpret_start_x = m_crtc_state.regs.X * resolution_scale; const uint32_t reinterpret_crop_left = (m_crtc_state.display_vram_left - m_crtc_state.regs.X) * resolution_scale; - const uint32_t uniforms[4] = {reinterpret_start_x, scaled_flipped_vram_offset_y, reinterpret_crop_left, - reinterpret_field_offset}; + // 6 DWORDs to match the post-RESOLUTION_SCALE-refactor display_ps + // cbuffer (u_vram_offset.xy, u_crop_left, u_field_offset, + // u_resolution_scale, u_pad0). m_resolution_scale is pushed, + // NOT the local resolution_scale (forced to 1 in 24-bit mode) + // since the shader's RESOLUTION_SCALE has always been the + // session m_resolution_scale. scaled_flipped_vram_offset_y + // preserves OpenGL's lower-left origin y-flip. + const uint32_t uniforms[6] = {reinterpret_start_x, scaled_flipped_vram_offset_y, reinterpret_crop_left, + reinterpret_field_offset, m_resolution_scale, 0u /* u_pad0 */}; UploadUniformBuffer(uniforms, sizeof(uniforms)); m_batch_ubo_dirty = true; diff --git a/src/core/gpu_hw_shadergen.cpp b/src/core/gpu_hw_shadergen.cpp index 0c48a0dd..a4ba82d8 100644 --- a/src/core/gpu_hw_shadergen.cpp +++ b/src/core/gpu_hw_shadergen.cpp @@ -1070,8 +1070,28 @@ std::string GPU_HW_ShaderGen::GenerateDisplayFragmentShader(bool depth_24bit, DefineMacro(ss, "INTERLEAVED", interlace_mode == GPU_HW::InterlacedRenderMode::InterleavedFields); DefineMacro(ss, "SMOOTH_CHROMA", smooth_chroma); - WriteCommonFunctions(ss); - DeclareUniformBuffer(ss, {"uint2 u_vram_offset", "uint u_crop_left", "uint u_field_offset"}, true); + // u_resolution_scale appended; u_pad0 keeps the cbuffer 16-byte- + // aligned. The 4 callers (D3D11 / D3D12 / OpenGL / Vulkan + // GPU_HW_*::UpdateDisplay) must push uniforms in this exact + // order: u_vram_offset.x, u_vram_offset.y, u_crop_left, + // u_field_offset, u_resolution_scale, u_pad0. + DeclareUniformBuffer(ss, + {"uint2 u_vram_offset", "uint u_crop_left", "uint u_field_offset", "uint u_resolution_scale", + "uint u_pad0"}, + true); + + // Route RESOLUTION_SCALE / VRAM_SIZE / RCP_VRAM_SIZE through the + // cbuffer above. Same pattern as e56d4d4 / 9d2b49d / 2980961. + // display_ps body uses RESOLUTION_SCALE in SampleVRAM24 (24-bit + // colour mode coord scaling) and VRAM_SIZE in the 16-bit path's + // modulo wrap. The 24-bit path is hit during FMV playback and any + // game that uses true-colour mode for the framebuffer; the 16-bit + // path is the common case (every game's standard framebuffer + // present). Both expand to the same #define aliases as + // u_resolution_scale-derived arithmetic. + WriteCBufferResolutionScaleAliases(ss); + WriteCommonFunctions(ss, true); + DeclareTexture(ss, "samp0", 0, UsingMSAA()); ss << R"( diff --git a/src/core/gpu_hw_vulkan.cpp b/src/core/gpu_hw_vulkan.cpp index ab11d5fb..7df82154 100644 --- a/src/core/gpu_hw_vulkan.cpp +++ b/src/core/gpu_hw_vulkan.cpp @@ -3128,8 +3128,15 @@ void GPU_HW_Vulkan::UpdateDisplay() const uint32_t reinterpret_field_offset = (interlaced != InterlacedRenderMode::None) ? GetInterlacedDisplayField() : 0; const uint32_t reinterpret_start_x = m_crtc_state.regs.X * resolution_scale; const uint32_t reinterpret_crop_left = (m_crtc_state.display_vram_left - m_crtc_state.regs.X) * resolution_scale; - const uint32_t uniforms[4] = {reinterpret_start_x, scaled_vram_offset_y + reinterpret_field_offset, - reinterpret_crop_left, reinterpret_field_offset}; + // 6 DWORDs to match the post-RESOLUTION_SCALE-refactor display_ps + // cbuffer (u_vram_offset.xy, u_crop_left, u_field_offset, + // u_resolution_scale, u_pad0). m_resolution_scale is pushed, + // NOT the local resolution_scale (which is forced to 1 in + // 24-bit mode for coord scaling) - the shader's RESOLUTION_SCALE + // macro has always been the session m_resolution_scale. + const uint32_t uniforms[6] = {reinterpret_start_x, scaled_vram_offset_y + reinterpret_field_offset, + reinterpret_crop_left, reinterpret_field_offset, + m_resolution_scale, 0u /* u_pad0 */}; m_display_texture.TransitionToLayout(cmdbuf, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); m_vram_texture.TransitionToLayout(cmdbuf, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);