Skip to content

Commit

Permalink
Cleaned up shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
adriengivry committed Nov 26, 2024
1 parent 5731308 commit 7b9a111
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 88 deletions.
22 changes: 12 additions & 10 deletions Resources/Engine/Shaders/Blit.ovfx
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
#shader vertex
#version 430 core

layout(location = 0) in vec2 inPos;
layout(location = 1) in vec2 inUV;
layout(location = 0) in vec2 geo_Pos;
layout(location = 1) in vec2 geo_TexCoords;

out vec2 fragUV;
out vec2 TexCoords;

void main() {
fragUV = inUV;
gl_Position = vec4(inPos, 0.0, 1.0);
void main()
{
TexCoords = geo_TexCoords;
gl_Position = vec4(geo_Pos, 0.0, 1.0);
}

#shader fragment
#version 430 core

in vec2 fragUV;
out vec4 fragColor; // Output color
in vec2 TexCoords;
out vec4 FRAGMENT_COLOR;

uniform sampler2D _InputTexture;

void main() {
fragColor = texture(_InputTexture, fragUV);
void main()
{
FRAGMENT_COLOR = texture(_InputTexture, TexCoords);
}
32 changes: 17 additions & 15 deletions Resources/Engine/Shaders/PostProcess/Bloom.ovfx
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
#shader vertex
#version 430 core

layout(location = 0) in vec2 inPos;
layout(location = 1) in vec2 inUV;
layout(location = 0) in vec2 geo_Pos;
layout(location = 1) in vec2 geo_TexCoords;

out vec2 fragUV;
out vec2 TexCoords;

void main() {
fragUV = inUV;
gl_Position = vec4(inPos, 0.0, 1.0);
void main()
{
TexCoords = geo_TexCoords;
gl_Position = vec4(geo_Pos, 0.0, 1.0);
}

#shader fragment
#version 430 core

in vec2 fragUV;
out vec4 fragColor; // Output color
in vec2 TexCoords;
out vec4 FRAGMENT_COLOR;

uniform sampler2D _InputTexture; // Original scene texture
uniform sampler2D _BloomTexture; // Blurred bloom texture
uniform float _BloomIntensity; // Strength of bloom effect
uniform sampler2D _InputTexture;
uniform sampler2D _BloomTexture;
uniform float _BloomIntensity;

void main() {
vec3 sceneColor = texture(_InputTexture, fragUV).rgb;
vec3 bloomColor = texture(_BloomTexture, fragUV).rgb;
fragColor = vec4(sceneColor + _BloomIntensity * bloomColor, 1.0);
void main()
{
const vec3 sceneColor = texture(_InputTexture, TexCoords).rgb;
const vec3 bloomColor = texture(_BloomTexture, TexCoords).rgb;
FRAGMENT_COLOR = vec4(sceneColor + _BloomIntensity * bloomColor, 1.0);
}
43 changes: 21 additions & 22 deletions Resources/Engine/Shaders/PostProcess/Blur.ovfx
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
#shader vertex
#version 430 core

layout(location = 0) in vec2 inPos;
layout(location = 1) in vec2 inUV;
layout(location = 0) in vec2 geo_Pos;
layout(location = 1) in vec2 geo_TexCoords;

out vec2 fragUV;
out vec2 TexCoords;

void main() {
fragUV = inUV;
gl_Position = vec4(inPos, 0.0, 1.0);
void main()
{
TexCoords = geo_TexCoords;
gl_Position = vec4(geo_Pos, 0.0, 1.0);
}

#shader fragment
#version 430 core

in vec2 fragUV;
out vec4 fragColor; // Output color
in vec2 TexCoords;
out vec4 FRAGMENT_COLOR;

uniform sampler2D _InputTexture; // Input texture
uniform bool _Horizontal; // Blur direction: horizontal or vertical
uniform sampler2D _InputTexture;
uniform bool _Horizontal;
uniform float _BlurSize;
uniform int _KernelSize;

uniform float _BlurSize; // Controls the spread of the blur
uniform int _KernelSize; // Number of samples in the blur kernel

void main() {
vec2 texelSize = 1.0 / textureSize(_InputTexture, 0); // Size of one texel in UV coordinates
vec2 direction = _Horizontal ? vec2(texelSize.x, 0.0) : vec2(0.0, texelSize.y);
void main()
{
const vec2 texelSize = 1.0 / textureSize(_InputTexture, 0);
const vec2 direction = _Horizontal ? vec2(texelSize.x, 0.0) : vec2(0.0, texelSize.y);

vec4 color = vec4(0.0);
float totalWeight = 0.0;

// Loop through the kernel
for (int i = -_KernelSize; i <= _KernelSize; i++) {
for (int i = -_KernelSize; i <= _KernelSize; i++)
{
float weight = exp(-0.5 * (i * i) / (_BlurSize * _BlurSize));
vec2 offset = float(i) * direction;
color += texture(_InputTexture, fragUV + offset) * weight;
color += texture(_InputTexture, TexCoords + offset) * weight;
totalWeight += weight;
}

// Normalize the result
fragColor = color / totalWeight;
FRAGMENT_COLOR = color / totalWeight;
}

28 changes: 15 additions & 13 deletions Resources/Engine/Shaders/PostProcess/Brightness.ovfx
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
#shader vertex
#version 430 core

layout(location = 0) in vec2 inPos;
layout(location = 1) in vec2 inUV;
layout(location = 0) in vec2 geo_Pos;
layout(location = 1) in vec2 geo_TexCoords;

out vec2 fragUV;
out vec2 TexCoords;

void main() {
fragUV = inUV;
gl_Position = vec4(inPos, 0.0, 1.0);
void main()
{
TexCoords = geo_TexCoords;
gl_Position = vec4(geo_Pos, 0.0, 1.0);
}

#shader fragment
#version 430 core

in vec2 fragUV;
out vec4 fragColor; // Output color
in vec2 TexCoords;
out vec4 FRAGMENT_COLOR;

uniform sampler2D _InputTexture; // Input scene texture
uniform float _Threshold; // Brightness threshold
uniform sampler2D _InputTexture;
uniform float _Threshold;

float luminance(vec3 color)
{
return dot(color, vec3(0.2126, 0.7152, 0.0722));
}

void main() {
vec3 color = texture(_InputTexture, fragUV).rgb;
void main()
{
const vec3 color = texture(_InputTexture, TexCoords).rgb;
float brightness = luminance(color);
brightness = max(0.0, brightness - _Threshold);
fragColor = vec4(color * sign(brightness), 1.0);
FRAGMENT_COLOR = vec4(color * sign(brightness), 1.0);
}
31 changes: 18 additions & 13 deletions Resources/Engine/Shaders/PostProcess/FXAA.ovfx
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
#shader vertex
#version 430 core

layout(location = 0) in vec2 inPos;
layout(location = 1) in vec2 inUV;
layout(location = 0) in vec2 geo_Pos;
layout(location = 1) in vec2 geo_TexCoords;

out vec2 fragUV;
out vec2 TexCoords;

void main() {
fragUV = inUV;
gl_Position = vec4(inPos, 0.0, 1.0);
void main()
{
TexCoords = geo_TexCoords;
gl_Position = vec4(geo_Pos, 0.0, 1.0);
}

#shader fragment
#version 430 core

in vec2 fragUV;
out vec4 fragColor;
in vec2 TexCoords;
out vec4 FRAGMENT_COLOR;

uniform sampler2D _InputTexture;

// FXAA function
vec3 applyFXAA(sampler2D tex, vec2 uv) {
vec3 applyFXAA(sampler2D tex, vec2 uv)
{
vec2 resolution = vec2(textureSize(tex, 0));
vec2 pixelSize = 1.0 / resolution;

Expand All @@ -43,7 +44,9 @@ vec3 applyFXAA(sampler2D tex, vec2 uv) {
float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
float lumaRange = lumaMax - lumaMin;

if (lumaRange < 0.1) { // Threshold for edge detection
// Threshold for edge detection
if (lumaRange < 0.1)
{
return rgbM; // No significant edge, return original
}

Expand All @@ -62,6 +65,7 @@ vec3 applyFXAA(sampler2D tex, vec2 uv) {
texture(tex, uv + dir * (1.0 / 3.0 - 0.5)).rgb +
texture(tex, uv + dir * (2.0 / 3.0 - 0.5)).rgb
);

vec3 rgbB = rgbA * 0.5 + 0.25 * (
texture(tex, uv + dir * -0.5).rgb +
texture(tex, uv + dir * 0.5).rgb
Expand All @@ -72,6 +76,7 @@ vec3 applyFXAA(sampler2D tex, vec2 uv) {
return (lumaB < lumaMin || lumaB > lumaMax) ? rgbA : rgbB;
}

void main() {
fragColor = vec4(applyFXAA(_InputTexture, fragUV), 1.0);
void main()
{
FRAGMENT_COLOR = vec4(applyFXAA(_InputTexture, TexCoords), 1.0);
}
30 changes: 15 additions & 15 deletions Resources/Engine/Shaders/PostProcess/Tonemapping.ovfx
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#shader vertex
#version 430 core

layout(location = 0) in vec2 inPos;
layout(location = 1) in vec2 inUV;
layout(location = 0) in vec2 geo_Pos;
layout(location = 1) in vec2 geo_TexCoords;

out vec2 fragUV;
out vec2 TexCoords;

void main() {
fragUV = inUV;
gl_Position = vec4(inPos, 0.0, 1.0);
TexCoords = geo_TexCoords;
gl_Position = vec4(geo_Pos, 0.0, 1.0);
}

#shader fragment
#version 430 core

in vec2 fragUV;
out vec4 fragColor;
in vec2 TexCoords;
out vec4 FRAGMENT_COLOR;

uniform sampler2D _InputTexture;
uniform float _Exposure;
Expand Down Expand Up @@ -67,12 +67,12 @@ vec3 reinhard_jodie(vec3 v)

vec3 uncharted2_tonemap_partial(vec3 x)
{
float A = 0.15f;
float B = 0.50f;
float C = 0.10f;
float D = 0.20f;
float E = 0.02f;
float F = 0.30f;
const float A = 0.15f;
const float B = 0.50f;
const float C = 0.10f;
const float D = 0.20f;
const float E = 0.02f;
const float F = 0.30f;
return ((x*(A*x+C*B)+D*E)/(x*(A*x+B)+D*F))-E/F;
}

Expand All @@ -98,7 +98,7 @@ vec3 ACESFilm(vec3 x)

void main()
{
vec3 color = texture(_InputTexture, fragUV).rgb * _Exposure;
vec3 color = texture(_InputTexture, TexCoords).rgb * _Exposure;

switch(_Mode)
{
Expand All @@ -115,5 +115,5 @@ void main()
color = applyGammaCorrection(color);
}

fragColor = vec4(color, 1.0);
FRAGMENT_COLOR = vec4(color, 1.0);
}

0 comments on commit 7b9a111

Please sign in to comment.