TsundereStation/data/shaders/vulkan/adaptive_downsample_mip.frag.glsl

81 lines
2.8 KiB
Text
Raw Normal View History

common/vulkan, core: spec-constant infrastructure + adaptive downsample mip/blur pre-bake Patch 3 in the glslang-elimination series. Introduces the specialization- constant plumbing that subsequent patches need, and applies it to two adaptive-downsample shaders that demonstrate both the no-spec-const and the with-spec-const paths through the new infrastructure. Infrastructure additions: - Vulkan::SpecConstants class in builders.{h,cpp}. A small builder that owns a VkSpecializationMapEntry array and a 4-byte-per-slot payload buffer, exposing AddBool/AddUInt/AddInt/AddFloat by constant_id. The consumer typically declares a SpecConstants on the same stack frame as the GraphicsPipelineBuilder, fills it, and passes GetInfo() to SetVertexShader / SetFragmentShader. GetInfo() returns nullptr if no entries have been added (equivalent to no specialization info), so the same code path can serve both spec-const and non-spec-const shaders without conditionals at the call site. Up to MAX_ENTRIES (16) constants per stage; AddBool stores the value as a 4-byte word (1 for true, 0 for false) per the Vulkan spec-constant ABI. Larger types (vec4, double) intentionally not supported - no current shader needs them; can be added later if the batch shaders end up needing wider values. - GraphicsPipelineBuilder::SetVertexShader and ::SetFragmentShader now take an optional 'const VkSpecializationInfo*' that defaults to nullptr. All 36 existing call sites continue to compile unchanged via the default. SetShaderStage is also extended; in addition to threading spec_info through it now explicitly clears pNext, flags, and pSpecializationInfo on every call. The builder is sometimes reused across pipelines without an intervening Clear() (e.g. the display / cursor pair in LibretroVulkanHostDisplay::CreateResources), so a stale spec_info pointer or flags bit could previously leak from one pipeline build into the next. Latent issue pre-existing this patch; fixed defensively because the new code path exercises it. Spec-constant ID allocation convention (documented in the SpecConstants class header and adhered to project-wide): 0-99 common knobs (RESOLUTION_SCALE, MULTISAMPLES, PER_SAMPLE_SHADING, PGXP_DEPTH) - to be plumbed by patch 4. 100+ shader-specific; e.g. FIRST_PASS for the adaptive-downsample mip FS in this patch. Shaders converted by this patch: 1. AdaptiveDownsampleBlurFragmentShader (no spec constants). One SPIR-V blob, replaces GetDownsampleBlurPassPipeline's runtime glslang call. Establishes that the embedded path works alongside the spec-constant path - the call site is identical to a patch-1 or patch-2 conversion. 2. AdaptiveDownsampleMipFragmentShader. The 'first_pass' bool that previously produced two distinct GLSL source strings (and so two cached SPIR-V blobs) is now a single 'bool FIRST_PASS' spec constant at constant_id=100. ONE SPIR-V blob serves both GetDownsampleFirstPassPipeline (Adaptive branch) with FIRST_PASS=true, and GetDownsampleMidPassPipeline with FIRST_PASS=false. The branch is preserved as a runtime 'if'; the driver's SPIR-V optimizer drops the dead arm at vkCreateGraphics Pipelines time because the bool is a compile-time constant in that context. spirv-dis on the blob confirms 'OpDecorate %FIRST_PASS SpecId 100' and 'OpSpecConstantFalse %bool' are emitted as expected. Other changes in gpu_hw_vulkan.cpp: - GetDownsampleFirstPassPipeline: m_shadergen null-check moved from function entry into the Box branch (which still uses GenerateBoxSampleDownsampleFragmentShader at runtime). The Adaptive branch no longer needs the check - pre-baked SPIR-V doesn't depend on shadergen. - GetDownsampleMidPassPipeline and GetDownsampleBlurPassPipeline: m_shadergen null-checks removed entirely - both are Adaptive-only (early-return on non-Adaptive) and now fully pre-baked. Cumulative state after this patch: - 7 of ~25-30 Bucket A variants converted (1 in patch 1, 4 in patch 2, 2 in this patch; the mip FS is one source but two specializations). - Spec-constant infrastructure ready for patches 4+ to plumb RESOLUTION_SCALE / MULTISAMPLES / PGXP_DEPTH into the remaining utility shaders. - glslang and the runtime ShaderGen path still in the tree, still used by every Vulkan path not yet converted (batch shaders, remaining utility shaders, Box downsample, all GL/D3D backends). Smoke tests in the sandbox: - Both new .inc files reconstruct to SPIR-V that passes spirv-val --target-env vulkan1.0. - spirv-dis on the mip FS confirms the OpDecorate SpecId 100 + OpSpecConstantFalse pair. - g++ -fsyntax-only on both modified TUs (builders.cpp, embedded_shaders.cpp) is clean; the only warnings are two pre-existing unused-parameter warnings in SamplerBuilder::Create and BufferViewBuilder::Create, unchanged by this patch. Smoke tests at user's end: - Cold-boot with the shader cache nuked and the downsample mode set to Adaptive. All three Adaptive pipelines must succeed; the visual output should be byte-identical to the pre-patch baseline (same algorithm, same texture binds, just specialized SPIR-V instead of hand-rolled GLSL). - Same cold-boot test with Box mode is also useful - it exercises that the Box branch in GetDownsampleFirstPassPipeline still works via the runtime-glslang path (regression check on the m_shadergen null-check relocation).
2026-05-17 18:36:06 +00:00
// Pre-baked Vulkan source for
// GPU_HW_ShaderGen::GenerateAdaptiveDownsampleMipFragmentShader(first_pass).
//
// Vulkan-only port. Originally "mipmap_energy.glsl" from parallel-rsx.
// Measures local "energy" (variance) across a 2x2 footprint and writes a
// scalar bias used downstream to choose mip levels.
//
// The 'first_pass' parameter that previously produced two distinct GLSL
// source strings (and thus two cached SPIR-V blobs) is now a single bool
// specialization constant at id=100. One SPIR-V blob serves both
// GetDownsampleFirstPassPipeline (Adaptive branch) and
// GetDownsampleMidPassPipeline; each binds a different
// VkSpecializationInfo at vkCreateGraphicsPipelines time.
//
// Spec-constant ID allocation (project-wide convention):
// 0-99 : common knobs (RESOLUTION_SCALE, MULTISAMPLES, ...) - not used here
// 100+ : shader-specific. FIRST_PASS lives at 100.
#version 450 core
layout(constant_id = 100) const bool FIRST_PASS = false;
layout(set = 0, binding = 0) uniform sampler2D samp0;
layout(push_constant) uniform PushConstants {
vec2 u_uv_min;
vec2 u_uv_max;
vec2 u_rcp_resolution;
};
layout(location = 0) in VertexData {
vec2 v_tex0;
};
layout(location = 0) out vec4 o_col0;
// Two overloads of get_bias - the rgb form is used by the first pass (input
// is the raw downsampled color so we have no prior alpha bias to carry),
// the rgba form is used by subsequent passes which feed back the bias in
// the alpha channel.
vec4 get_bias(vec3 c00, vec3 c01, vec3 c10, vec3 c11)
{
vec3 avg = 0.25 * (c00 + c01 + c10 + c11);
float s00 = dot(c00 - avg, c00 - avg);
float s01 = dot(c01 - avg, c01 - avg);
float s10 = dot(c10 - avg, c10 - avg);
float s11 = dot(c11 - avg, c11 - avg);
return vec4(avg, 1.0 - log2(1000.0 * (s00 + s01 + s10 + s11) + 1.0));
}
vec4 get_bias(vec4 c00, vec4 c01, vec4 c10, vec4 c11)
{
float avg = 0.25 * (c00.a + c01.a + c10.a + c11.a);
vec4 bias = get_bias(c00.rgb, c01.rgb, c10.rgb, c11.rgb);
bias.a *= avg;
return bias;
}
void main()
{
vec2 uv = v_tex0 - (u_rcp_resolution * 0.25);
// The spec-constant bool turns the runtime 'if' into a compile-time
// selection at pipeline-creation; the dead branch is dropped by the
// driver's optimizer.
if (FIRST_PASS)
{
vec3 c00 = textureOffset(samp0, uv, ivec2(0, 0)).rgb;
vec3 c01 = textureOffset(samp0, uv, ivec2(0, 1)).rgb;
vec3 c10 = textureOffset(samp0, uv, ivec2(1, 0)).rgb;
vec3 c11 = textureOffset(samp0, uv, ivec2(1, 1)).rgb;
o_col0 = get_bias(c00, c01, c10, c11);
}
else
{
vec4 c00 = textureOffset(samp0, uv, ivec2(0, 0));
vec4 c01 = textureOffset(samp0, uv, ivec2(0, 1));
vec4 c10 = textureOffset(samp0, uv, ivec2(1, 0));
vec4 c11 = textureOffset(samp0, uv, ivec2(1, 1));
o_col0 = get_bias(c00, c01, c10, c11);
}
}