Skip to content

Commit

Permalink
cleanup shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
DadSchoorse committed Mar 5, 2021
1 parent 111b0e8 commit b929505
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 56 deletions.
44 changes: 24 additions & 20 deletions src/shader/cas.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
12 changes: 7 additions & 5 deletions src/shader/deband.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
Expand Down Expand Up @@ -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

Expand Down
25 changes: 14 additions & 11 deletions src/shader/dls.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down
13 changes: 8 additions & 5 deletions src/shader/lut.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
23 changes: 8 additions & 15 deletions src/shader/smaa_neighbor.vert.glsl
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);

}

0 comments on commit b929505

Please sign in to comment.