From b929505ba71dea21d6c32a5a59f2d241592b30c4 Mon Sep 17 00:00:00 2001 From: Georg Lehmann Date: Fri, 5 Mar 2021 11:24:01 +0100 Subject: [PATCH] cleanup shaders --- src/shader/cas.frag.glsl | 44 ++++++++++++++++-------------- src/shader/deband.frag.glsl | 12 ++++---- src/shader/dls.frag.glsl | 25 +++++++++-------- src/shader/lut.frag.glsl | 13 +++++---- src/shader/smaa_neighbor.vert.glsl | 23 ++++++---------- 5 files changed, 61 insertions(+), 56 deletions(-) diff --git a/src/shader/cas.frag.glsl b/src/shader/cas.frag.glsl index ee6a14df..9860bd46 100644 --- a/src/shader/cas.frag.glsl +++ b/src/shader/cas.frag.glsl @@ -23,55 +23,59 @@ layout (constant_id = 0) const float sharpness = 0.4; layout(location = 0) in vec2 textureCoord; layout(location = 0) out vec4 fragColor; +#define textureLod0Offset(img, coord, offset) textureLodOffset(img, coord, 0.0f, offset) +#define textureLod0(img, coord) textureLod(img, coord, 0.0f) + void main() { // fetch a 3x3 neighborhood around the pixel 'e', // a b c // d(e)f // g h i - float alpha = texture(img,textureCoord).w; - - vec3 a = textureOffset(img, textureCoord, ivec2(-1,-1)).xyz; - vec3 b = textureOffset(img, textureCoord, ivec2( 0,-1)).xyz; - vec3 c = textureOffset(img, textureCoord, ivec2( 1,-1)).xyz; - vec3 d = textureOffset(img, textureCoord, ivec2(-1, 0)).xyz; - vec3 e = textureOffset(img, textureCoord, ivec2( 0, 0)).xyz; - vec3 f = textureOffset(img, textureCoord, ivec2( 1, 0)).xyz; - vec3 g = textureOffset(img, textureCoord, ivec2(-1, 1)).xyz; - vec3 h = textureOffset(img, textureCoord, ivec2( 0, 1)).xyz; - vec3 i = textureOffset(img, textureCoord, ivec2( 1, 1)).xyz; - + vec4 inputColor = textureLod0(img,textureCoord); + float alpha = inputColor.a; + + vec3 a = textureLod0Offset(img, textureCoord, ivec2(-1,-1)).rgb; + vec3 b = textureLod0Offset(img, textureCoord, ivec2( 0,-1)).rgb; + vec3 c = textureLod0Offset(img, textureCoord, ivec2( 1,-1)).rgb; + vec3 d = textureLod0Offset(img, textureCoord, ivec2(-1, 0)).rgb; + vec3 e = inputColor.rgb; + vec3 f = textureLod0Offset(img, textureCoord, ivec2( 1, 0)).rgb; + vec3 g = textureLod0Offset(img, textureCoord, ivec2(-1, 1)).rgb; + vec3 h = textureLod0Offset(img, textureCoord, ivec2( 0, 1)).rgb; + vec3 i = textureLod0Offset(img, textureCoord, ivec2( 1, 1)).rgb; + // Soft min and max. // a b c b // d e f * 0.5 + d e f * 0.5 // g h i h // These are 2.0x bigger (factored out the extra multiply). - + vec3 mnRGB = min(min(min(d,e),min(f,b)),h); vec3 mnRGB2 = min(min(min(mnRGB,a),min(g,c)),i); mnRGB += mnRGB2; - + vec3 mxRGB = max(max(max(d,e),max(f,b)),h); vec3 mxRGB2 = max(max(max(mxRGB,a),max(g,c)),i); mxRGB += mxRGB2; - + // Smooth minimum distance to signal limit divided by smooth max. - + vec3 rcpMxRGB = vec3(1)/mxRGB; vec3 ampRGB = clamp((min(mnRGB,2.0-mxRGB) * rcpMxRGB),0,1); - + // Shaping amount of sharpening. ampRGB = inversesqrt(ampRGB); float peak = 8.0 - 3.0 * sharpness; vec3 wRGB = -vec3(1)/(ampRGB * peak); vec3 rcpWeightRGB = vec3(1)/(1.0 + 4.0 * wRGB); - + // 0 w 0 // Filter shape: w 1 w // 0 w 0 - + vec3 window = (b + d) + (f + h); vec3 outColor = clamp((window * wRGB + e) * rcpWeightRGB,0,1); - + fragColor = vec4(outColor,alpha); } diff --git a/src/shader/deband.frag.glsl b/src/shader/deband.frag.glsl index 8548654a..d1c62709 100644 --- a/src/shader/deband.frag.glsl +++ b/src/shader/deband.frag.glsl @@ -39,6 +39,8 @@ layout(constant_id = 8) const int iterations = 4; layout(location = 0) in vec2 texcoord; layout(location = 0) out vec4 fragColor; +#define textureLod0Offset(img, coord, offset) textureLodOffset(img, coord, 0.0f, offset) +#define textureLod0(img, coord) textureLod(img, coord, 0.0f) float rand(float x) { @@ -55,28 +57,28 @@ void analyze_pixels(vec3 ori, sampler2D tex, vec2 texcoord, vec2 _range, vec2 di // Sample at quarter-turn intervals around the source pixel // South-east - vec3 ref = texture(tex, texcoord + _range * dir).rgb; + vec3 ref = textureLod0(tex, texcoord + _range * dir).rgb; vec3 diff = abs(ori - ref); ref_max_diff = diff; ref_avg = ref; ref_mid_diff1 = ref; // North-west - ref = texture(tex, texcoord + _range * -dir).rgb; + ref = textureLod0(tex, texcoord + _range * -dir).rgb; diff = abs(ori - ref); ref_max_diff = max(ref_max_diff, diff); ref_avg += ref; ref_mid_diff1 = abs(((ref_mid_diff1 + ref) * 0.5) - ori); // North-east - ref = texture(tex, texcoord + _range * vec2(-dir.y, dir.x)).rgb; + ref = textureLod0(tex, texcoord + _range * vec2(-dir.y, dir.x)).rgb; diff = abs(ori - ref); ref_max_diff = max(ref_max_diff, diff); ref_avg += ref; ref_mid_diff2 = ref; // South-west - ref = texture(tex, texcoord + _range * vec2( dir.y, -dir.x)).rgb; + ref = textureLod0(tex, texcoord + _range * vec2( dir.y, -dir.x)).rgb; diff = abs(ori - ref); ref_max_diff = max(ref_max_diff, diff); ref_avg += ref; @@ -104,7 +106,7 @@ void main() vec3 ref_mid_diff1; // The difference between the average of SE and NW reference pixels and the original pixel vec3 ref_mid_diff2; // The difference between the average of NE and SW reference pixels and the original pixel - vec4 ori_alpha = texture(img, texcoord); // Original pixel + vec4 ori_alpha = textureLod0(img, texcoord); // Original pixel vec3 ori = ori_alpha.rgb; vec3 res; // Final pixel diff --git a/src/shader/dls.frag.glsl b/src/shader/dls.frag.glsl index 968132e0..4aee2f64 100644 --- a/src/shader/dls.frag.glsl +++ b/src/shader/dls.frag.glsl @@ -30,6 +30,9 @@ layout (constant_id = 1) const float denoise = 0.17; layout(location = 0) in vec2 textureCoord; layout(location = 0) out vec4 fragColor; +#define textureLod0Offset(img, coord, offset) textureLodOffset(img, coord, 0.0f, offset) +#define textureLod0(img, coord) textureLod(img, coord, 0.0f) + float GetLumaComponents(float r, float g, float b) { // Y from JPEG spec @@ -61,17 +64,17 @@ void main() // a (x) b // g c f - vec4 x = texture(img, textureCoord); - - vec4 a = textureOffset(img, textureCoord, ivec2(-1, 0)); - vec4 b = textureOffset(img, textureCoord, ivec2( 1, 0)); - vec4 c = textureOffset(img, textureCoord, ivec2( 0, 1)); - vec4 d = textureOffset(img, textureCoord, ivec2( 0, -1)); - - vec4 e = textureOffset(img, textureCoord, ivec2(-1, -1)); - vec4 f = textureOffset(img, textureCoord, ivec2( 1, 1)); - vec4 g = textureOffset(img, textureCoord, ivec2(-1, 1)); - vec4 h = textureOffset(img, textureCoord, ivec2( 1, -1)); + vec4 x = textureLod0(img, textureCoord); + + vec4 a = textureLod0Offset(img, textureCoord, ivec2(-1, 0)); + vec4 b = textureLod0Offset(img, textureCoord, ivec2( 1, 0)); + vec4 c = textureLod0Offset(img, textureCoord, ivec2( 0, 1)); + vec4 d = textureLod0Offset(img, textureCoord, ivec2( 0, -1)); + + vec4 e = textureLod0Offset(img, textureCoord, ivec2(-1, -1)); + vec4 f = textureLod0Offset(img, textureCoord, ivec2( 1, 1)); + vec4 g = textureLod0Offset(img, textureCoord, ivec2(-1, 1)); + vec4 h = textureLod0Offset(img, textureCoord, ivec2( 1, -1)); float lx = GetLuma(x); diff --git a/src/shader/lut.frag.glsl b/src/shader/lut.frag.glsl index 0a5d7788..2b30b458 100644 --- a/src/shader/lut.frag.glsl +++ b/src/shader/lut.frag.glsl @@ -10,21 +10,24 @@ layout(constant_id = 1) const int flipGB = 0; layout(location = 0) in vec2 textureCoord; layout(location = 0) out vec4 fragColor; +#define textureLod0Offset(img, coord, offset) textureLodOffset(img, coord, 0.0f, offset) +#define textureLod0(img, coord) textureLod(img, coord, 0.0f) + void main() { vec4 color; if(flipGB != 0) { - color = texture(img,textureCoord).rbga; + color = textureLod0(img,textureCoord).rbga; } else { - color = texture(img,textureCoord); + color = textureLod0(img,textureCoord); } - + //see https://developer.nvidia.com/gpugems/GPUGems2/gpugems2_chapter24.html vec3 scale = (vec3(lutSize) - 1.0) / vec3(lutSize); vec3 offset = 1.0 / (2.0 * vec3(lutSize)); - - fragColor = vec4(texture(lut, scale * color.rgb + offset).rgb, color.a); + + fragColor = vec4(textureLod0(lut, scale * color.rgb + offset).rgb, color.a); } diff --git a/src/shader/smaa_neighbor.vert.glsl b/src/shader/smaa_neighbor.vert.glsl index af0f493a..fe2c87e6 100644 --- a/src/shader/smaa_neighbor.vert.glsl +++ b/src/shader/smaa_neighbor.vert.glsl @@ -1,18 +1,6 @@ #version 450 #extension GL_GOOGLE_include_directive : require -vec2 positions[3] = vec2[]( - vec2(-1.0,-1.0), - vec2( 3.0,-1.0), - vec2(-1.0, 3.0) -); - -vec2 texture_coordinates[3] = vec2[]( - vec2( 0.0, 0.0), - vec2( 2.0, 0.0), - vec2( 0.0, 2.0) -); - layout(location=0) out vec2 textureCoord; layout(location=1) out vec4 offset; @@ -23,8 +11,13 @@ layout(location=1) out vec4 offset; void main() { - textureCoord = texture_coordinates[gl_VertexIndex]; + gl_Position = vec4(gl_VertexIndex == 1 ? 3.0 : -1.0, + gl_VertexIndex == 2 ? 3.0 : -1.0, + 0.0, + 1.0); + + textureCoord = vec2(gl_VertexIndex == 1 ? 2.0 : 0.0, + gl_VertexIndex == 2 ? 2.0 : 0.0); + SMAANeighborhoodBlendingVS(textureCoord, offset); - gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0); - }