diff options
| author | UjinT34 <41110182+UjinT34@users.noreply.github.com> | 2026-05-08 00:28:34 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-05-07 22:28:34 +0100 |
| commit | 39af3901b389f792a55176041dec9211c82ed1c9 (patch) | |
| tree | cfe9a9b84e56d2553934aa1600ffe5789aaa667b /src/render/shaders | |
| parent | c18186a519d188a3ae8caf9947a5420a25590b82 (diff) | |
renderer: more FP16 fixes (#14070)
* fix fp16 blur with invalidation
* restore tonemap code
* fix fade outs
* add sdr compat for unaware surfaces
* use a better work space, not linear, only for sdr
* fix hl tf -> wp tf
* cache workbuffer descriptions
* linear not normalised WIP
* clear offload fb
* CM math
* fix includes
* use precalculated cm for shadows
* inner glow cm
* quad cm
* fix unmodified copy
* codegen
* cache color CM
* mark blur dirty when CM/DS changes
* avoid icc workbuffer
* fp16 blur finish hack
* fix output hdr luminances
* use srgb for mirroring/sharing with fp16
* defaults people
---------
Co-authored-by: Vaxry <vaxry@vaxry.net>
Diffstat (limited to 'src/render/shaders')
| -rw-r--r-- | src/render/shaders/glsl/blur1.glsl | 8 | ||||
| -rw-r--r-- | src/render/shaders/glsl/blurFinish.glsl | 6 | ||||
| -rw-r--r-- | src/render/shaders/glsl/cm_helpers.glsl | 11 | ||||
| -rw-r--r-- | src/render/shaders/glsl/constants.h | 1 | ||||
| -rw-r--r-- | src/render/shaders/glsl/inner_glow.frag | 41 | ||||
| -rw-r--r-- | src/render/shaders/glsl/inner_glow.glsl | 55 | ||||
| -rw-r--r-- | src/render/shaders/glsl/quad.frag | 7 | ||||
| -rw-r--r-- | src/render/shaders/glsl/shadow.frag | 34 | ||||
| -rw-r--r-- | src/render/shaders/glsl/shadow.glsl | 58 | ||||
| -rw-r--r-- | src/render/shaders/glsl/tonemap.glsl | 25 |
10 files changed, 63 insertions, 183 deletions
diff --git a/src/render/shaders/glsl/blur1.glsl b/src/render/shaders/glsl/blur1.glsl index 86a37d88..8d9e39ae 100644 --- a/src/render/shaders/glsl/blur1.glsl +++ b/src/render/shaders/glsl/blur1.glsl @@ -104,10 +104,10 @@ vec4 blur1(vec2 v_texcoord, sampler2D tex, float radius, vec2 halfpixel, int pas // That garbage maps to 0.0-1.0 range with UINT8 buffer and doesn't have any significant impact on the end result. // FP16 garbage maps to -65,504 - 65,504 and defines the end result. Clamp it here to 0.0 - 1.0 to get the same quality outcome as with UINT8. // Rerendering an undamaged area to get some insignificant color accuracy increase on blur edges isn't worth it. - sum += clamp(texture(tex, uv - halfpixel.xy * radius), 0.0, 1.0); - sum += clamp(texture(tex, uv + halfpixel.xy * radius), 0.0, 1.0); - sum += clamp(texture(tex, uv + vec2(halfpixel.x, -halfpixel.y) * radius), 0.0, 1.0); - sum += clamp(texture(tex, uv - vec2(halfpixel.x, -halfpixel.y) * radius), 0.0, 1.0); + sum += texture(tex, uv - halfpixel.xy * radius); + sum += texture(tex, uv + halfpixel.xy * radius); + sum += texture(tex, uv + vec2(halfpixel.x, -halfpixel.y) * radius); + sum += texture(tex, uv - vec2(halfpixel.x, -halfpixel.y) * radius); vec4 color = sum / 8.0; diff --git a/src/render/shaders/glsl/blurFinish.glsl b/src/render/shaders/glsl/blurFinish.glsl index e7e1f1c6..bdfe2bc8 100644 --- a/src/render/shaders/glsl/blurFinish.glsl +++ b/src/render/shaders/glsl/blurFinish.glsl @@ -30,7 +30,11 @@ vec4 blurFinish(vec4 pixColor, vec2 v_texcoord, float noise, float brightness pixColor.rgb *= min(1.0, brightness); #if USE_CM - pixColor = doColorManagement(pixColor, sourceTF, targetTF, convertMatrix, srcTFRange, dstTFRange); + if (targetTF == CM_TRANSFER_FUNCTION_EXT_LINEAR) { + pixColor = doColorManagement(pixColor, sourceTF, targetTF, convertMatrix, srcTFRange, vec2(0.0, 203.0)); // FIXME wtf + } else { + pixColor = doColorManagement(pixColor, sourceTF, targetTF, convertMatrix, srcTFRange, dstTFRange); + } #endif return pixColor; diff --git a/src/render/shaders/glsl/cm_helpers.glsl b/src/render/shaders/glsl/cm_helpers.glsl index 0d8c68e9..ae4b63ae 100644 --- a/src/render/shaders/glsl/cm_helpers.glsl +++ b/src/render/shaders/glsl/cm_helpers.glsl @@ -127,6 +127,7 @@ vec3 tfST240(vec3 color) { vec3 toLinearRGB(vec3 color, int tf) { switch (tf) { + case CM_TRANSFER_FUNCTION_LINEAR: return color; case CM_TRANSFER_FUNCTION_EXT_LINEAR: return color; case CM_TRANSFER_FUNCTION_ST2084_PQ: return tfInvPQ(color); case CM_TRANSFER_FUNCTION_GAMMA22: return pow(max(color, vec3(0.0)), vec3(2.2)); @@ -179,7 +180,7 @@ vec3 fromLinearRGB(vec3 color, int tf) { } vec4 fromLinear(vec4 color, int tf) { - if (tf == CM_TRANSFER_FUNCTION_EXT_LINEAR) + if (tf == CM_TRANSFER_FUNCTION_EXT_LINEAR || tf == CM_TRANSFER_FUNCTION_LINEAR) return color; color.rgb /= max(color.a, 0.001); @@ -189,6 +190,9 @@ vec4 fromLinear(vec4 color, int tf) { } vec4 fromLinearNit(vec4 color, int tf, vec2 range) { + if (tf == CM_TRANSFER_FUNCTION_LINEAR) + return color; + color.rgb = (color.rgb - range[0] * color.a) / (range[1] - range[0]); // @gulafaran color.rgb /= max(color.a, 0.001); color.rgb = fromLinearRGB(color.rgb, tf); @@ -231,7 +235,8 @@ vec4 pixColor.rgb *= pixColor.a; #else pixColor.rgb = convertMatrix * pixColor.rgb; - pixColor = toNit(pixColor, srcTFRange); + if (srcTF != CM_TRANSFER_FUNCTION_LINEAR) + pixColor = toNit(pixColor, srcTFRange); pixColor.rgb *= pixColor.a; #if USE_TONEMAP pixColor = tonemap(pixColor, dstxyz, maxLuminance, dstMaxLuminance, dstRefLuminance, srcRefLuminance); @@ -258,4 +263,4 @@ vec4 #endif } -#endif
\ No newline at end of file +#endif diff --git a/src/render/shaders/glsl/constants.h b/src/render/shaders/glsl/constants.h index bbab5284..db8b2cc1 100644 --- a/src/render/shaders/glsl/constants.h +++ b/src/render/shaders/glsl/constants.h @@ -1,6 +1,7 @@ #ifndef CONSTANTS_H #define CONSTANTS_H //enum eTransferFunction +#define CM_TRANSFER_FUNCTION_LINEAR 0 // not normalised #define CM_TRANSFER_FUNCTION_BT1886 1 #define CM_TRANSFER_FUNCTION_GAMMA22 2 #define CM_TRANSFER_FUNCTION_GAMMA28 3 diff --git a/src/render/shaders/glsl/inner_glow.frag b/src/render/shaders/glsl/inner_glow.frag index 2fc405c0..63369e0a 100644 --- a/src/render/shaders/glsl/inner_glow.frag +++ b/src/render/shaders/glsl/inner_glow.frag @@ -8,10 +8,7 @@ precision highp float; in vec4 v_color; in vec2 v_texcoord; -uniform int sourceTF; // eTransferFunction -uniform int targetTF; // eTransferFunction -uniform mat3 targetPrimariesXYZ; - +uniform vec4 colorSRGB; uniform vec2 topLeft; uniform vec2 bottomRight; uniform vec2 fullSize; @@ -20,38 +17,22 @@ uniform float roundingPower; uniform float range; uniform float shadowPower; -#if USE_CM -#include "cm_helpers.glsl" -#include "CM.glsl" -#endif - #include "inner_glow.glsl" layout(location = 0) out vec4 fragColor; +#if USE_MIRROR +layout(location = 1) out vec4 mirrorColor; +#endif void main() { vec4 pixColor = v_color; - - fragColor = getInnerGlow(pixColor, v_texcoord, radius, roundingPower, topLeft, fullSize, range, shadowPower, bottomRight -#if USE_CM - , - sourceTF, targetTF, convertMatrix, srcTFRange, dstTFRange -#if USE_ICC - , - iccLut3D, iccLutSize +#if USE_MIRROR + vec4[2] pixColors = #else -#if USE_TONEMAP || USE_SDR_MOD - , - targetPrimariesXYZ -#endif -#if USE_TONEMAP - , - maxLuminance, dstMaxLuminance, dstRefLuminance, srcRefLuminance -#endif -#if USE_SDR_MOD - , - sdrSaturation, sdrBrightnessMultiplier -#endif + fragColor = #endif + getInnerGlow(pixColor, colorSRGB, v_texcoord, radius, roundingPower, topLeft, fullSize, range, shadowPower, bottomRight); +#if USE_MIRROR + fragColor = pixColors[0]; + mirrorColor = pixColors[1]; #endif - ); } diff --git a/src/render/shaders/glsl/inner_glow.glsl b/src/render/shaders/glsl/inner_glow.glsl index b0d55b19..307c3648 100644 --- a/src/render/shaders/glsl/inner_glow.glsl +++ b/src/render/shaders/glsl/inner_glow.glsl @@ -5,8 +5,6 @@ #ifndef INNER_GLOW_GLSL #define INNER_GLOW_GLSL -#include "cm_helpers.glsl" - float innerGlowAlpha(float distFromEdge, float range, float glowPower) { if (distFromEdge >= range) return 0.0; @@ -26,29 +24,7 @@ float innerGlowSmin(float a, float b, float k) { return min(a, b) - h * h * h * k * (1.0 / 6.0); } -vec4 getInnerGlow(vec4 pixColor, vec2 v_texcoord, float radius, float roundingPower, vec2 topLeft, vec2 fullSize, float range, float glowPower, vec2 bottomRight -#if USE_CM - , - int sourceTF, int targetTF, mat3 convertMatrix, vec2 srcTFRange, vec2 dstTFRange -#if USE_ICC - , - highp sampler3D iccLut3D, float iccLutSize -#else -#if USE_TONEMAP || USE_SDR_MOD - , - mat3 targetPrimariesXYZ -#endif -#if USE_TONEMAP - , - float maxLuminance, float dstMaxLuminance, float dstRefLuminance, float srcRefLuminance -#endif -#if USE_SDR_MOD - , - float sdrSaturation, float sdrBrightnessMultiplier -#endif -#endif -#endif -) { +vec4 getInnerGlow(vec4 pixColor, vec4 colorSRGB, vec2 v_texcoord, float radius, float roundingPower, vec2 topLeft, vec2 fullSize, float range, float glowPower, vec2 bottomRight) { vec2 pixCoord = fullSize * v_texcoord; // clip to the rounded rectangle shape using actual SDF @@ -79,28 +55,15 @@ vec4 getInnerGlow(vec4 pixColor, vec2 v_texcoord, float radius, float roundingPo // premultiply pixColor.rgb *= pixColor[3]; -#if USE_CM - pixColor = doColorManagement(pixColor, sourceTF, targetTF, convertMatrix, srcTFRange, dstTFRange -#if USE_ICC - , - iccLut3D, iccLutSize +#if USE_MIRROR + vec4[2] pixColors; + pixColors[0] = pixColor; + pixColors[1] = colorSRGB; + pixColors[1].a = pixColor.a; + pixColors[1].rgb *= pixColors[1].a; + return pixColors; #else -#if USE_TONEMAP || USE_SDR_MOD - , - targetPrimariesXYZ -#endif -#if USE_TONEMAP - , - maxLuminance, dstMaxLuminance, dstRefLuminance, srcRefLuminance -#endif -#if USE_SDR_MOD - , - sdrSaturation, sdrBrightnessMultiplier -#endif -#endif - ); -#endif - return pixColor; +#endif } #endif diff --git a/src/render/shaders/glsl/quad.frag b/src/render/shaders/glsl/quad.frag index dc1945af..46d42a8c 100644 --- a/src/render/shaders/glsl/quad.frag +++ b/src/render/shaders/glsl/quad.frag @@ -7,6 +7,7 @@ precision highp float; in vec4 v_color; +uniform vec4 colorSRGB; #if USE_ROUNDING uniform float radius; uniform float roundingPower; @@ -28,6 +29,10 @@ void main() { fragColor = pixColor; #if USE_MIRROR - mirrorColor = fragColor; +#if USE_ROUNDING + mirrorColor = rounding(colorSRGB, radius, roundingPower, topLeft, fullSize); +#else + mirrorColor = colorSRGB; +#endif #endif } diff --git a/src/render/shaders/glsl/shadow.frag b/src/render/shaders/glsl/shadow.frag index e91e9a14..eb6f3174 100644 --- a/src/render/shaders/glsl/shadow.frag +++ b/src/render/shaders/glsl/shadow.frag @@ -8,10 +8,7 @@ precision highp float; in vec4 v_color; in vec2 v_texcoord; -uniform int sourceTF; // eTransferFunction -uniform int targetTF; // eTransferFunction -uniform mat3 targetPrimariesXYZ; - +uniform vec4 colorSRGB; uniform vec2 topLeft; uniform vec2 bottomRight; uniform vec2 windowTopLeft; @@ -23,11 +20,6 @@ uniform float range; uniform float shadowPower; uniform float thick; -#if USE_CM -#include "cm_helpers.glsl" -#include "CM.glsl" -#endif - #include "shadow.glsl" layout(location = 0) out vec4 fragColor; @@ -41,29 +33,7 @@ void main() { #else fragColor = #endif - getShadow(pixColor, v_texcoord, radius, roundingPower, topLeft, fullSize, range, shadowPower, bottomRight, windowTopLeft, windowBottomRight, thick -#if USE_CM - , - sourceTF, targetTF, convertMatrix, srcTFRange, dstTFRange -#if USE_ICC - , - iccLut3D, iccLutSize -#else -#if USE_TONEMAP || USE_SDR_MOD - , - targetPrimariesXYZ -#endif -#if USE_TONEMAP - , - maxLuminance, dstMaxLuminance, dstRefLuminance, srcRefLuminance -#endif -#if USE_SDR_MOD - , - sdrSaturation, sdrBrightnessMultiplier -#endif -#endif -#endif - ); + getShadow(pixColor, colorSRGB, v_texcoord, radius, roundingPower, topLeft, fullSize, range, shadowPower, bottomRight, windowTopLeft, windowBottomRight, thick); #if USE_MIRROR fragColor = pixColors[0]; mirrorColor = pixColors[1]; diff --git a/src/render/shaders/glsl/shadow.glsl b/src/render/shaders/glsl/shadow.glsl index fdfa697c..ba68bc49 100644 --- a/src/render/shaders/glsl/shadow.glsl +++ b/src/render/shaders/glsl/shadow.glsl @@ -5,7 +5,6 @@ #ifndef SHADOW_GLSL #define SHADOW_GLSL -#include "cm_helpers.glsl" #include "rounding.glsl" float pixAlphaRoundedDistance(float distanceToCorner, float radius, float range, float shadowPower) { @@ -54,30 +53,8 @@ vec4[2] #else vec4 #endif - getShadow(vec4 pixColor, vec2 v_texcoord, float borderRadius, float roundingPower, vec2 topLeft, vec2 fullSize, float range, float shadowPower, vec2 bottomRight, - vec2 windowTopLeft, vec2 windowBottomRight, float windowRadius -#if USE_CM - , - int sourceTF, int targetTF, mat3 convertMatrix, vec2 srcTFRange, vec2 dstTFRange -#if USE_ICC - , - highp sampler3D iccLut3D, float iccLutSize -#else -#if USE_TONEMAP || USE_SDR_MOD - , - mat3 targetPrimariesXYZ -#endif -#if USE_TONEMAP - , - float maxLuminance, float dstMaxLuminance, float dstRefLuminance, float srcRefLuminance -#endif -#if USE_SDR_MOD - , - float sdrSaturation, float sdrBrightnessMultiplier -#endif -#endif -#endif - ) { + getShadow(vec4 pixColor, vec4 colorSRGB, vec2 v_texcoord, float borderRadius, float roundingPower, vec2 topLeft, vec2 fullSize, float range, float shadowPower, vec2 bottomRight, + vec2 windowTopLeft, vec2 windowBottomRight, float windowRadius) { float radius = range + borderRadius; float originalAlpha = pixColor[3]; @@ -144,33 +121,12 @@ vec4 // premultiply pixColor.rgb *= pixColor[3]; -#if USE_CM -#if USE_MIRROR - vec4[2] pixColors = -#else - pixColor = -#endif - doColorManagement(pixColor, sourceTF, targetTF, convertMatrix, srcTFRange, dstTFRange -#if USE_ICC - , - iccLut3D, iccLutSize -#else -#if USE_TONEMAP || USE_SDR_MOD - , - targetPrimariesXYZ -#endif -#if USE_TONEMAP - , - maxLuminance, dstMaxLuminance, dstRefLuminance, srcRefLuminance -#endif -#if USE_SDR_MOD - , - sdrSaturation, sdrBrightnessMultiplier -#endif -#endif - ); -#endif #if USE_MIRROR + vec4[2] pixColors; + pixColors[0] = pixColor; + pixColors[1] = colorSRGB; + pixColors[1].a = pixColor.a; + pixColors[1].rgb *= pixColors[1].a; return pixColors; #else return pixColor; diff --git a/src/render/shaders/glsl/tonemap.glsl b/src/render/shaders/glsl/tonemap.glsl index dcce45a6..a2db7e6e 100644 --- a/src/render/shaders/glsl/tonemap.glsl +++ b/src/render/shaders/glsl/tonemap.glsl @@ -50,21 +50,16 @@ vec4 tonemap(vec4 color, mat3 dstXYZ, float maxLuminance, float dstMaxLuminance, float E = pow(clamp(ICtCp[0], 0.0, 1.0), PQ_INV_M2); float luminance = pow((max(E - PQ_C1, 0.0)) / (PQ_C2 - PQ_C3 * E), PQ_INV_M1) * HDR_MAX_LUMINANCE; - float linearPart = min(luminance, dstRefLuminance); - float luminanceAboveRef = max(luminance - dstRefLuminance, 0.0); - float maxExcessLuminance = max(maxLuminance - dstRefLuminance, 1.0); - float shoulder = log((luminanceAboveRef / maxExcessLuminance + 1.0) * (M_E - 1.0)); - float mappedHigh = shoulder * (dstMaxLuminance - dstRefLuminance); - float newLum = clamp(linearPart + mappedHigh, 0.0, dstMaxLuminance); + float luminanceRatio = max(luminance / dstRefLuminance, 0.0); + float srcScale = maxLuminance / dstRefLuminance; + float dstScale = dstMaxLuminance / dstRefLuminance; + float v = (dstScale * (1.0 + srcScale) - srcScale) / pow(srcScale, 2.0); + float newLuminance = (luminanceRatio * (1.0 + luminanceRatio * v) / (1.0 + luminanceRatio)) * dstRefLuminance; - // scale src to dst reference - float refScale = dstRefLuminance / srcRefLuminance; + E = pow(clamp(newLuminance / HDR_MAX_LUMINANCE, 0.0, 1.0), PQ_M1); + ICtCp[0] = pow((PQ_C1 + PQ_C2 * E) / (1.0 + PQ_C3 * E), PQ_M2); - // kind of works but doesn't use newLum at all - return vec4(fromLMS * toLinear(vec4(ICtCpPQInv * ICtCp, 1.0), CM_TRANSFER_FUNCTION_ST2084_PQ).rgb * HDR_MAX_LUMINANCE * refScale, color[3]); - // breaks with overriden monitor luminances. might be caused by incorrect imput values - // @gulafaran - // vec3 outRGB = fromLMS * toLinear(vec4(ICtCpPQInv * ICtCp, 1.0), CM_TRANSFER_FUNCTION_ST2084_PQ).rgb; - // outRGB *= (newLum / max(luminance, 0.0001)); // actually apply the tone mapping - // return vec4(clamp(outRGB * HDR_MAX_LUMINANCE * refScale, 0.0, dstMaxLuminance), color[3]); + color = vec4(fromLMS * toLinear(vec4(ICtCpPQInv * ICtCp, 1.0), CM_TRANSFER_FUNCTION_ST2084_PQ).rgb * HDR_MAX_LUMINANCE, color[3]); + + return clamp(color, 0.0, dstMaxLuminance); } |
