Skip to content

Commit

Permalink
glsl 1.20 💪
Browse files Browse the repository at this point in the history
  • Loading branch information
cgytrus committed Sep 12, 2023
1 parent c96e6ee commit 45a8e99
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
31 changes: 15 additions & 16 deletions resources/shaders/pp-frag.glsl
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
#version 150
#version 120

in vec2 TexCoords;

out vec4 FragColor;
varying vec2 TexCoords;

uniform sampler2D screen;
uniform vec2 screenSize;
uniform bool fast;
uniform bool first;
uniform float radius;

void main() {
float scaledRadius = radius * textureSize(screen, 0).y * 0.5;
vec2 texOffset = 1.0 / textureSize(screen, 0); // gets size of single texel
float scaledRadius = radius * screenSize.y * 0.5;
vec2 texOffset = 1.0 / screenSize; // gets size of single texel

vec3 result = texture(screen, TexCoords.st).rgb;
vec3 result = texture2D(screen, TexCoords.st).rgb;
if (fast) {
scaledRadius *= radius * 10.0 / ((radius * 10.0 + 1.0) * (radius * 10.0 + 1.0) - 1.0);
float weight = 1.0;
Expand All @@ -22,16 +21,16 @@ void main() {
for (int i = 1; float(i) < scaledRadius; i++) {
weight -= 1.0 / scaledRadius;
weightSum += weight * 2.0;
result += texture(screen, TexCoords.st + vec2(texOffset.x * i, 0.0)).rgb * weight;
result += texture(screen, TexCoords.st - vec2(texOffset.x * i, 0.0)).rgb * weight;
result += texture2D(screen, TexCoords.st + vec2(texOffset.x * i, 0.0)).rgb * weight;
result += texture2D(screen, TexCoords.st - vec2(texOffset.x * i, 0.0)).rgb * weight;
}
}
else {
for (int i = 1; float(i) < scaledRadius; i++) {
weight -= 1.0 / scaledRadius;
weightSum += weight * 2.0;
result += texture(screen, TexCoords.st + vec2(0.0, texOffset.y * i)).rgb * weight;
result += texture(screen, TexCoords.st - vec2(0.0, texOffset.y * i)).rgb * weight;
result += texture2D(screen, TexCoords.st + vec2(0.0, texOffset.y * i)).rgb * weight;
result += texture2D(screen, TexCoords.st - vec2(0.0, texOffset.y * i)).rgb * weight;
}
}
result /= weightSum;
Expand All @@ -44,20 +43,20 @@ void main() {
for (int i = 1; float(i) <= ceil(scaledRadius); i++) {
float weight = firstWeight * exp(-i * i / (2.0 * scaledRadius));
weightSum += weight * 2;
result += texture(screen, TexCoords.st + vec2(texOffset.x * i, 0.0)).rgb * weight;
result += texture(screen, TexCoords.st - vec2(texOffset.x * i, 0.0)).rgb * weight;
result += texture2D(screen, TexCoords.st + vec2(texOffset.x * i, 0.0)).rgb * weight;
result += texture2D(screen, TexCoords.st - vec2(texOffset.x * i, 0.0)).rgb * weight;
}
}
else {
for (int i = 1; float(i) <= ceil(scaledRadius); i++) {
float weight = firstWeight * exp(-i * i / (2.0 * scaledRadius));
weightSum += weight * 2;
result += texture(screen, TexCoords.st + vec2(0.0, texOffset.y * i)).rgb * weight;
result += texture(screen, TexCoords.st - vec2(0.0, texOffset.y * i)).rgb * weight;
result += texture2D(screen, TexCoords.st + vec2(0.0, texOffset.y * i)).rgb * weight;
result += texture2D(screen, TexCoords.st - vec2(0.0, texOffset.y * i)).rgb * weight;
}
}
result /= weightSum;
}

FragColor = vec4(result, 1.0);
gl_FragColor = vec4(result, 1.0);
}
11 changes: 6 additions & 5 deletions resources/shaders/pp-vert.glsl
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#version 150
#extension GL_ARB_explicit_attrib_location : enable
#version 120
#extension GL_ARB_explicit_attrib_location : require
#extension GL_ARB_separate_shader_objects : require

in layout(location = 0) vec2 aPosition;
in layout(location = 1) vec2 aTexCoords;
attribute layout(location = 0) vec2 aPosition;
attribute layout(location = 1) vec2 aTexCoords;

out vec2 TexCoords;
varying vec2 TexCoords;

void main() {
gl_Position = vec4(aPosition.x, aPosition.y, 0.0, 1.0);
Expand Down
3 changes: 2 additions & 1 deletion src/pp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ void setupPostProcess() {
return;
}
log::info("{}", res.unwrap());
glUniform1i(glGetUniformLocation(ppShader.program, "screen"), 0);
glProgramUniform1i(ppShader.program, glGetUniformLocation(ppShader.program, "screen"), 0);
glProgramUniform2f(ppShader.program, glGetUniformLocation(ppShader.program, "screenSize"), size.width, size.height);
ppShaderFast = glGetUniformLocation(ppShader.program, "fast");
ppShaderFirst = glGetUniformLocation(ppShader.program, "first");
ppShaderRadius = glGetUniformLocation(ppShader.program, "radius");
Expand Down

0 comments on commit 45a8e99

Please sign in to comment.