aboutsummaryrefslogtreecommitdiff
path: root/res/shaders
diff options
context:
space:
mode:
authorNicolas James <Eele1Ephe7uZahRie@tutanota.com>2025-02-12 18:05:18 +1100
committerNicolas James <Eele1Ephe7uZahRie@tutanota.com>2025-02-12 18:05:18 +1100
commit1cc08c51eb4b0f95c30c0a98ad1fc5ad3459b2df (patch)
tree222dfcd07a1e40716127a347bbfd7119ce3d0984 /res/shaders
initial commit
Diffstat (limited to 'res/shaders')
-rw-r--r--res/shaders/face.fs15
-rw-r--r--res/shaders/face.vs12
-rw-r--r--res/shaders/framebuffer.fs100
-rw-r--r--res/shaders/framebuffer.vs9
-rw-r--r--res/shaders/line.fs55
-rw-r--r--res/shaders/line.vs8
-rw-r--r--res/shaders/model.fs9
-rw-r--r--res/shaders/model.vs13
-rw-r--r--res/shaders/postprocess.fs105
-rw-r--r--res/shaders/rectangle.fs9
-rw-r--r--res/shaders/rectangle.vs8
-rw-r--r--res/shaders/text.fs13
-rw-r--r--res/shaders/text.vs14
13 files changed, 370 insertions, 0 deletions
diff --git a/res/shaders/face.fs b/res/shaders/face.fs
new file mode 100644
index 0000000..d82949e
--- /dev/null
+++ b/res/shaders/face.fs
@@ -0,0 +1,15 @@
+#version 420 core
+
+in vec3 _f_texture;
+
+layout (binding = 1) uniform sampler2DArray _u_texture;
+
+out vec4 _o_colour;
+
+void main() {
+ vec4 texture = texture(_u_texture, _f_texture);
+ if (texture.a < 0.60f) {
+ discard;
+ }
+ _o_colour = texture;
+}
diff --git a/res/shaders/face.vs b/res/shaders/face.vs
new file mode 100644
index 0000000..a939dbd
--- /dev/null
+++ b/res/shaders/face.vs
@@ -0,0 +1,12 @@
+#version 420 core
+layout (location = 0) in vec3 _l_position;
+layout (location = 1) in vec3 _l_texture;
+
+out vec3 _f_texture;
+
+uniform mat4 _u_matrix;
+
+void main() {
+ _f_texture = _l_texture;
+ gl_Position = _u_matrix * vec4(_l_position, 1.0);
+}
diff --git a/res/shaders/framebuffer.fs b/res/shaders/framebuffer.fs
new file mode 100644
index 0000000..b644980
--- /dev/null
+++ b/res/shaders/framebuffer.fs
@@ -0,0 +1,100 @@
+#version 420 core
+
+in vec2 _f_texture;
+
+layout(binding = 2) uniform sampler2D main_colour;
+layout(binding = 3) uniform sampler2D main_depth;
+layout(binding = 4) uniform sampler2D water_colour;
+layout(binding = 5) uniform sampler2D water_depth;
+
+layout(std140, binding = 0) uniform _u_globals {
+ mat4 proj;
+ mat4 view;
+ vec4 frustum[6];
+ float znear;
+ float zfar;
+ float xfov;
+ float yfov;
+ int time;
+ float xwindow;
+ float ywindow;
+};
+
+uniform bool _u_is_underwater;
+
+const vec3 fog_colour = vec3(0.5f, 0.7f, 0.9f);
+const vec3 underwater_fog_colour = vec3(0.137f, 0.275f, 0.694f);
+
+out vec4 _o_colour;
+
+vec4 texture_sample(const sampler2D sampler, const vec2 coord) {
+ return texture(sampler, coord);
+}
+
+float depth_sample(const sampler2D sampler, const vec2 coord) {
+ return texture(sampler, coord).r;
+}
+
+float linearise_depth(const float depth) {
+ const float z = depth * 2.0f - 1.0f; // normalised device coordinates
+ const float linear_depth =
+ (2.0 * znear * zfar) / (zfar + znear - (z * (zfar - znear)));
+ // Usually we would just return linear depth, but this is incorrect! We need
+ // to account for the additional distance of the frustum away from the
+ // center of the screen. Just some trigonomety
+
+ const float xsigma = tan(xfov * 0.5) * zfar * _f_texture.x;
+ const float ysigma = tan(yfov * 0.5) * zfar * _f_texture.y;
+ const float dist = sqrt(xsigma * xsigma + ysigma * ysigma) / zfar;
+
+ const float o_dist = linear_depth * dist;
+ return sqrt(o_dist * o_dist + linear_depth * linear_depth);
+}
+
+float get_fog_intensity(const float distance) {
+ return clamp(pow((1.0f / (zfar * 0.6f)) * distance, 3.0f), 0.0f, 1.0f);
+}
+
+float get_water_intensity(const float distance) {
+ return 1.0f - exp(0.5 * distance);
+}
+
+void main() {
+ // We recycle the coordinates of our position into our texture coordinates.
+ const vec2 coords =
+ vec2((_f_texture.x + 1.0f) / 2.0f, (_f_texture.y + 1.0f) / 2.0f);
+
+ const float main_depth = depth_sample(main_depth, coords);
+ const float main_distance = linearise_depth(main_depth);
+ const float water_depth = depth_sample(water_depth, coords);
+ const float water_distance = linearise_depth(water_depth);
+
+ _o_colour = texture_sample(main_colour, coords);
+ if (!_u_is_underwater) {
+ if (water_depth < main_depth) {
+ _o_colour =
+ vec4(mix(vec3(_o_colour.rgb),
+ texture_sample(water_colour, coords).rgb,
+ get_water_intensity(water_distance - main_distance)),
+ 1.0f);
+ }
+ _o_colour =
+ vec4(mix(vec3(_o_colour.rgb), fog_colour,
+ get_fog_intensity(min(main_distance, water_distance))),
+ 1.0f);
+ } else {
+ _o_colour = vec4(mix(vec3(_o_colour.rgb), fog_colour,
+ get_fog_intensity(main_distance)),
+ 1.0f);
+ if (water_depth < main_depth) {
+ _o_colour = vec4(mix(vec3(_o_colour.rgb),
+ texture_sample(water_colour, coords).rgb,
+ get_water_intensity(-water_distance)),
+ 1.0f);
+ } else {
+ _o_colour = vec4(mix(vec3(_o_colour.rgb), underwater_fog_colour,
+ get_water_intensity(-main_distance)),
+ 1.0f);
+ }
+ }
+}
diff --git a/res/shaders/framebuffer.vs b/res/shaders/framebuffer.vs
new file mode 100644
index 0000000..88579b6
--- /dev/null
+++ b/res/shaders/framebuffer.vs
@@ -0,0 +1,9 @@
+#version 420 core
+layout (location = 0) in vec2 _l_position;
+
+out vec2 _f_texture;
+
+void main() {
+ _f_texture = _l_position;
+ gl_Position = vec4(_l_position.xy, 0.0f, 1.0f);
+}
diff --git a/res/shaders/line.fs b/res/shaders/line.fs
new file mode 100644
index 0000000..04cba86
--- /dev/null
+++ b/res/shaders/line.fs
@@ -0,0 +1,55 @@
+#version 420 core
+
+uniform vec4 _u_colour;
+
+layout(std140, binding = 0) uniform _u_globals {
+ mat4 proj;
+ mat4 view;
+ vec4 frustum[6];
+ float znear;
+ float zfar;
+ float xfov;
+ float yfov;
+ int time;
+ float xwindow;
+ float ywindow;
+};
+layout(binding = 2) uniform sampler2D main_colour;
+layout(binding = 3) uniform sampler2D main_depth;
+
+out vec4 _o_colour;
+
+vec4 texture_sample(const sampler2D sampler, const vec2 coord) {
+ return texture(sampler, coord);
+}
+
+float depth_sample(const sampler2D sampler, const vec2 coord) {
+ return texture(sampler, coord).r;
+}
+
+void main() {
+ const vec2 coords = vec2(gl_FragCoord.x / xwindow, gl_FragCoord.y / ywindow);
+
+ const float cur_depth = depth_sample(main_depth, coords);
+ gl_FragDepth = gl_FragCoord.z * 0.9999f;
+ if (gl_FragDepth > cur_depth) {
+ discard;
+ }
+
+ _o_colour = vec4(0.0f, 0.0f, 0.0f, 1.0f);
+
+ // This is for dynamically choosing a higher contrast colour, but it's not
+ // that nice of an effect so it's commented out.
+ /*
+ const vec2 coords = vec2(gl_FragCoord.x / xwindow, gl_FragCoord.y / ywindow);
+ const float cur_depth = depth_sample(main_depth, coords);
+ gl_FragDepth = gl_FragCoord.z * 0.9999f;
+ if (gl_FragDepth > cur_depth) {
+ discard;
+ }
+
+ const vec4 cur_tex = texture_sample(main_colour, coords);
+ const float clr = (1.0f - (cur_tex.r + cur_tex.g + cur_tex.b) / 3.0f) > 0.5f ? 1.0f : 0.0f;
+ _o_colour = vec4(clr, clr, clr, 1.0f);
+ */
+}
diff --git a/res/shaders/line.vs b/res/shaders/line.vs
new file mode 100644
index 0000000..7ad4775
--- /dev/null
+++ b/res/shaders/line.vs
@@ -0,0 +1,8 @@
+#version 420 core
+layout (location = 0) in vec3 _l_position;
+
+uniform mat4 _u_matrix;
+
+void main() {
+ gl_Position = _u_matrix * vec4(_l_position, 1.0f);
+}
diff --git a/res/shaders/model.fs b/res/shaders/model.fs
new file mode 100644
index 0000000..9244ff6
--- /dev/null
+++ b/res/shaders/model.fs
@@ -0,0 +1,9 @@
+#version 420 core
+
+in vec2 _f_texture;
+
+out vec4 _o_colour;
+
+void main() {
+ _o_colour = vec4(0.4, 0.4, 0.4, 1.0);
+}
diff --git a/res/shaders/model.vs b/res/shaders/model.vs
new file mode 100644
index 0000000..47b104d
--- /dev/null
+++ b/res/shaders/model.vs
@@ -0,0 +1,13 @@
+#version 420 core
+layout (location = 0) in vec3 _l_position;
+layout (location = 1) in vec3 _l_normals;
+layout (location = 2) in vec2 _l_texture;
+
+uniform mat4 _u_matrix;
+
+out vec2 _f_texture;
+
+void main() {
+ _f_texture = _l_texture;
+ gl_Position = _u_matrix * vec4(_l_position, 1.0);
+}
diff --git a/res/shaders/postprocess.fs b/res/shaders/postprocess.fs
new file mode 100644
index 0000000..af09c2c
--- /dev/null
+++ b/res/shaders/postprocess.fs
@@ -0,0 +1,105 @@
+#version 420 core
+
+in vec2 _f_texture;
+
+layout(binding = 2) uniform sampler2D main_colour;
+
+layout(std140, binding = 0) uniform _u_globals {
+ mat4 proj;
+ mat4 view;
+ vec4 frustum[6];
+ float znear;
+ float zfar;
+ float xfov;
+ float yfov;
+ uint time;
+};
+
+const float u_lumaThreshold = 0.125f;
+const float u_mulReduce = 1.0f / 8.0f;
+const float u_minReduce = 1.0f / 128.0f;
+const float u_maxSpan = 8.0f;
+
+out vec4 _o_colour;
+
+void main() {
+ const vec2 coords =
+ vec2((_f_texture.x + 1.0f) / 2.0f, (_f_texture.y + 1.0f) / 2.0f);
+
+ vec3 rgbM = texture(main_colour, coords).rgb;
+ vec2 size = textureSize(main_colour, 0);
+ vec2 u_texelStep = vec2(1.0f / size.x, 1.0f / size.y);
+
+ // Sampling neighbour texels. Offsets are adapted to OpenGL texture coordinates.
+ vec3 rgbNW = textureOffset(main_colour, coords, ivec2(-1, 1)).rgb;
+ vec3 rgbNE = textureOffset(main_colour, coords, ivec2(1, 1)).rgb;
+ vec3 rgbSW = textureOffset(main_colour, coords, ivec2(-1, -1)).rgb;
+ vec3 rgbSE = textureOffset(main_colour, coords, ivec2(1, -1)).rgb;
+
+ // see http://en.wikipedia.org/wiki/Grayscale
+ const vec3 toLuma = vec3(0.299, 0.587, 0.114);
+
+ // Convert from RGB to luma.
+ float lumaNW = dot(rgbNW, toLuma);
+ float lumaNE = dot(rgbNE, toLuma);
+ float lumaSW = dot(rgbSW, toLuma);
+ float lumaSE = dot(rgbSE, toLuma);
+ float lumaM = dot(rgbM, toLuma);
+
+ // Gather minimum and maximum luma.
+ float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
+ float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
+
+ // If contrast is lower than a maximum threshold ...
+ if (lumaMax - lumaMin <= lumaMax * u_lumaThreshold)
+ {
+ // ... do no AA and return.
+ _o_colour = vec4(rgbM, 1.0);
+
+ return;
+ }
+
+ // Sampling is done along the gradient.
+ vec2 samplingDirection;
+ samplingDirection.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
+ samplingDirection.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));
+
+ // Sampling step distance depends on the luma: The brighter the sampled texels, the smaller the final sampling step direction.
+ // This results, that brighter areas are less blurred/more sharper than dark areas.
+ float samplingDirectionReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * 0.25 * u_mulReduce, u_minReduce);
+
+ // Factor for norming the sampling direction plus adding the brightness influence.
+ float minSamplingDirectionFactor = 1.0 / (min(abs(samplingDirection.x), abs(samplingDirection.y)) + samplingDirectionReduce);
+
+ // Calculate final sampling direction vector by reducing, clamping to a range and finally adapting to the texture size.
+ samplingDirection = clamp(samplingDirection * minSamplingDirectionFactor, vec2(-u_maxSpan), vec2(u_maxSpan)) * u_texelStep;
+
+ // Inner samples on the tab.
+ vec3 rgbSampleNeg = texture(main_colour, coords + samplingDirection * (1.0/3.0 - 0.5)).rgb;
+ vec3 rgbSamplePos = texture(main_colour, coords + samplingDirection * (2.0/3.0 - 0.5)).rgb;
+
+ vec3 rgbTwoTab = (rgbSamplePos + rgbSampleNeg) * 0.5;
+
+ // Outer samples on the tab.
+ vec3 rgbSampleNegOuter = texture(main_colour, coords + samplingDirection * (0.0/3.0 - 0.5)).rgb;
+ vec3 rgbSamplePosOuter = texture(main_colour, coords + samplingDirection * (3.0/3.0 - 0.5)).rgb;
+
+ vec3 rgbFourTab = (rgbSamplePosOuter + rgbSampleNegOuter) * 0.25 + rgbTwoTab * 0.5;
+
+ // Calculate luma for checking against the minimum and maximum value.
+ float lumaFourTab = dot(rgbFourTab, toLuma);
+
+ // Are outer samples of the tab beyond the edge ...
+ if (lumaFourTab < lumaMin || lumaFourTab > lumaMax)
+ {
+ // ... yes, so use only two samples.
+ _o_colour = vec4(rgbTwoTab, 1.0);
+ }
+ else
+ {
+ // ... no, so use four samples.
+ _o_colour = vec4(rgbFourTab, 1.0);
+ }
+
+ //_o_colour.r = 1.0f;
+}
diff --git a/res/shaders/rectangle.fs b/res/shaders/rectangle.fs
new file mode 100644
index 0000000..97ca400
--- /dev/null
+++ b/res/shaders/rectangle.fs
@@ -0,0 +1,9 @@
+#version 330 core
+
+uniform vec4 _u_colour;
+
+out vec4 _o_colour;
+
+void main() {
+ _o_colour = _u_colour;
+}
diff --git a/res/shaders/rectangle.vs b/res/shaders/rectangle.vs
new file mode 100644
index 0000000..6adb73d
--- /dev/null
+++ b/res/shaders/rectangle.vs
@@ -0,0 +1,8 @@
+#version 330 core
+layout (location = 0) in vec2 _l_position;
+
+uniform mat4 _u_matrix;
+
+void main() {
+ gl_Position = _u_matrix * vec4(_l_position, 0.0f, 1.0f);
+}
diff --git a/res/shaders/text.fs b/res/shaders/text.fs
new file mode 100644
index 0000000..0408f2c
--- /dev/null
+++ b/res/shaders/text.fs
@@ -0,0 +1,13 @@
+#version 420 core
+
+in vec2 _f_texture;
+
+layout (binding = 0) uniform sampler2D text;
+uniform vec4 _u_colour;
+
+out vec4 _o_colour;
+
+void main() {
+ float alpha = texture(text, _f_texture).r;
+ _o_colour = vec4(_u_colour.rgb, alpha * _u_colour.a);
+}
diff --git a/res/shaders/text.vs b/res/shaders/text.vs
new file mode 100644
index 0000000..19ae8cb
--- /dev/null
+++ b/res/shaders/text.vs
@@ -0,0 +1,14 @@
+#version 330 core
+
+layout (location = 0) in vec2 vertex;
+layout (location = 1) in vec2 texture;
+
+// <vec2 pos, vec2 tex>
+out vec2 _f_texture;
+
+uniform mat4 _u_matrix;
+
+void main() {
+ _f_texture = texture;
+ gl_Position = _u_matrix * vec4(vertex.xy, 0.0, 1.0);
+}