summaryrefslogtreecommitdiff
path: root/drivers/gles3/shaders
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/gles3/shaders')
-rw-r--r--drivers/gles3/shaders/canvas.glsl18
-rw-r--r--drivers/gles3/shaders/sky.glsl59
-rw-r--r--drivers/gles3/shaders/tonemap_inc.glsl117
3 files changed, 161 insertions, 33 deletions
diff --git a/drivers/gles3/shaders/canvas.glsl b/drivers/gles3/shaders/canvas.glsl
index 41d308b776..f121679833 100644
--- a/drivers/gles3/shaders/canvas.glsl
+++ b/drivers/gles3/shaders/canvas.glsl
@@ -21,6 +21,15 @@ layout(location = 10) in uvec4 bone_attrib;
layout(location = 11) in vec4 weight_attrib;
#endif
+
+// This needs to be outside clang-format so the ubo comment is in the right place
+#ifdef MATERIAL_UNIFORMS_USED
+layout(std140) uniform MaterialUniforms{ //ubo:4
+
+#MATERIAL_UNIFORMS
+
+};
+#endif
/* clang-format on */
#include "canvas_uniforms_inc.glsl"
#include "stdlib_inc.glsl"
@@ -38,15 +47,6 @@ out vec2 pixel_size_interp;
#endif
-#ifdef MATERIAL_UNIFORMS_USED
-layout(std140) uniform MaterialUniforms{
-//ubo:4
-
-#MATERIAL_UNIFORMS
-
-};
-#endif
-
#GLOBALS
void main() {
diff --git a/drivers/gles3/shaders/sky.glsl b/drivers/gles3/shaders/sky.glsl
index 0faa3eb70c..a8e5daafa1 100644
--- a/drivers/gles3/shaders/sky.glsl
+++ b/drivers/gles3/shaders/sky.glsl
@@ -26,9 +26,9 @@ out vec2 uv_interp;
void main() {
// One big triangle to cover the whole screen
- vec2 base_arr[3] = vec2[](vec2(-1.0, -2.0), vec2(-1.0, 2.0), vec2(2.0, 2.0));
+ vec2 base_arr[3] = vec2[](vec2(-1.0, -1.0), vec2(-1.0, 3.0), vec2(3.0, -1.0));
uv_interp = base_arr[gl_VertexID];
- gl_Position = vec4(uv_interp, 1.0, 1.0);
+ gl_Position = vec4(uv_interp, 0.0, 1.0);
}
/* clang-format off */
@@ -50,6 +50,8 @@ precision mediump int;
#endif
#endif
+#include "tonemap_inc.glsl"
+
in vec2 uv_interp;
/* clang-format on */
@@ -63,12 +65,7 @@ uniform sampler2D half_res; //texunit:-2
uniform sampler2D quarter_res; //texunit:-3
#endif
-layout(std140) uniform CanvasData { //ubo:0
- mat3 orientation;
- vec4 projection;
- vec4 position_multiplier;
- float time;
- float luminance_multiplier;
+layout(std140) uniform SceneData { //ubo:0
float pad1;
float pad2;
};
@@ -88,15 +85,16 @@ layout(std140) uniform DirectionalLights { //ubo:2
}
directional_lights;
+/* clang-format off */
+
#ifdef MATERIAL_UNIFORMS_USED
-layout(std140) uniform MaterialUniforms{
-//ubo:3
+layout(std140) uniform MaterialUniforms{ //ubo:3
#MATERIAL_UNIFORMS
-} material;
+};
#endif
-
+/* clang-format on */
#GLOBALS
#ifdef USE_CUBEMAP_PASS
@@ -117,6 +115,12 @@ layout(std140) uniform MaterialUniforms{
#define AT_QUARTER_RES_PASS false
#endif
+// mat4 is a waste of space, but we don't have an easy way to set a mat3 uniform for now
+uniform mat4 orientation;
+uniform vec4 projection;
+uniform vec3 position;
+uniform float time;
+
layout(location = 0) out vec4 frag_color;
void main() {
@@ -128,7 +132,7 @@ void main() {
cube_normal.z = -cube_normal.z;
cube_normal = normalize(cube_normal);
- vec2 uv = uv_interp * 0.5 + 0.5;
+ vec2 uv = gl_FragCoord.xy; // uv_interp * 0.5 + 0.5;
vec2 panorama_coords = vec2(atan(cube_normal.x, cube_normal.z), acos(cube_normal.y));
@@ -148,17 +152,17 @@ void main() {
vec3 inverted_cube_normal = cube_normal;
inverted_cube_normal.z *= -1.0;
#ifdef USES_HALF_RES_COLOR
- half_res_color = texture(samplerCube(half_res, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), inverted_cube_normal) * luminance_multiplier;
+ half_res_color = texture(samplerCube(half_res, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), inverted_cube_normal);
#endif
#ifdef USES_QUARTER_RES_COLOR
- quarter_res_color = texture(samplerCube(quarter_res, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), inverted_cube_normal) * luminance_multiplier;
+ quarter_res_color = texture(samplerCube(quarter_res, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), inverted_cube_normal);
#endif
#else
#ifdef USES_HALF_RES_COLOR
- half_res_color = textureLod(sampler2D(half_res, material_samplers[SAMPLER_LINEAR_CLAMP]), uv, 0.0) * luminance_multiplier;
+ half_res_color = textureLod(sampler2D(half_res, material_samplers[SAMPLER_LINEAR_CLAMP]), uv, 0.0);
#endif
#ifdef USES_QUARTER_RES_COLOR
- quarter_res_color = textureLod(sampler2D(quarter_res, material_samplers[SAMPLER_LINEAR_CLAMP]), uv, 0.0) * luminance_multiplier;
+ quarter_res_color = textureLod(sampler2D(quarter_res, material_samplers[SAMPLER_LINEAR_CLAMP]), uv, 0.0);
#endif
#endif
@@ -168,12 +172,19 @@ void main() {
}
- frag_color.rgb = color * position_multiplier.w / luminance_multiplier;
- frag_color.a = alpha;
+ // Tonemap before writing as we are writing to an sRGB framebuffer
+ color *= exposure;
+ color = apply_tonemapping(color, white);
+ color = linear_to_srgb(color);
- // Blending is disabled for Sky, so alpha doesn't blend
- // alpha is used for subsurface scattering so make sure it doesn't get applied to Sky
- if (!AT_CUBEMAP_PASS && !AT_HALF_RES_PASS && !AT_QUARTER_RES_PASS) {
- frag_color.a = 0.0;
- }
+#ifdef USE_BCS
+ color = apply_bcs(color, bcs);
+#endif
+
+#ifdef USE_COLOR_CORRECTION
+ color = apply_color_correction(color, color_correction);
+#endif
+
+ frag_color.rgb = color;
+ frag_color.a = alpha;
}
diff --git a/drivers/gles3/shaders/tonemap_inc.glsl b/drivers/gles3/shaders/tonemap_inc.glsl
new file mode 100644
index 0000000000..b993f5d97b
--- /dev/null
+++ b/drivers/gles3/shaders/tonemap_inc.glsl
@@ -0,0 +1,117 @@
+#ifdef USE_BCS
+uniform vec3 bcs;
+#endif
+
+#ifdef USE_COLOR_CORRECTION
+#ifdef USE_1D_LUT
+uniform sampler2D source_color_correction; //texunit:-1
+#else
+uniform sampler3D source_color_correction; //texunit:-1
+#endif
+#endif
+
+// These could be grouped into some form of SceneData UBO along with time, will have to test performance though
+uniform int tonemapper;
+uniform float exposure;
+uniform float white;
+
+vec3 apply_bcs(vec3 color, vec3 bcs) {
+ color = mix(vec3(0.0), color, bcs.x);
+ color = mix(vec3(0.5), color, bcs.y);
+ color = mix(vec3(dot(vec3(1.0), color) * 0.33333), color, bcs.z);
+
+ return color;
+}
+#ifdef USE_COLOR_CORRECTION
+#ifdef USE_1D_LUT
+vec3 apply_color_correction(vec3 color) {
+ color.r = texture(source_color_correction, vec2(color.r, 0.0f)).r;
+ color.g = texture(source_color_correction, vec2(color.g, 0.0f)).g;
+ color.b = texture(source_color_correction, vec2(color.b, 0.0f)).b;
+ return color;
+}
+#else
+vec3 apply_color_correction(vec3 color) {
+ return textureLod(source_color_correction, color, 0.0).rgb;
+}
+#endif
+#endif
+
+vec3 tonemap_filmic(vec3 color, float p_white) {
+ // exposure bias: input scale (color *= bias, white *= bias) to make the brightness consistent with other tonemappers
+ // also useful to scale the input to the range that the tonemapper is designed for (some require very high input values)
+ // has no effect on the curve's general shape or visual properties
+ const float exposure_bias = 2.0f;
+ const float A = 0.22f * exposure_bias * exposure_bias; // bias baked into constants for performance
+ const float B = 0.30f * exposure_bias;
+ const float C = 0.10f;
+ const float D = 0.20f;
+ const float E = 0.01f;
+ const float F = 0.30f;
+
+ vec3 color_tonemapped = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F;
+ float p_white_tonemapped = ((p_white * (A * p_white + C * B) + D * E) / (p_white * (A * p_white + B) + D * F)) - E / F;
+
+ return color_tonemapped / p_white_tonemapped;
+}
+
+// Adapted from https://github.com/TheRealMJP/BakingLab/blob/master/BakingLab/ACES.hlsl
+// (MIT License).
+vec3 tonemap_aces(vec3 color, float p_white) {
+ const float exposure_bias = 1.8f;
+ const float A = 0.0245786f;
+ const float B = 0.000090537f;
+ const float C = 0.983729f;
+ const float D = 0.432951f;
+ const float E = 0.238081f;
+
+ // Exposure bias baked into transform to save shader instructions. Equivalent to `color *= exposure_bias`
+ const mat3 rgb_to_rrt = mat3(
+ vec3(0.59719f * exposure_bias, 0.35458f * exposure_bias, 0.04823f * exposure_bias),
+ vec3(0.07600f * exposure_bias, 0.90834f * exposure_bias, 0.01566f * exposure_bias),
+ vec3(0.02840f * exposure_bias, 0.13383f * exposure_bias, 0.83777f * exposure_bias));
+
+ const mat3 odt_to_rgb = mat3(
+ vec3(1.60475f, -0.53108f, -0.07367f),
+ vec3(-0.10208f, 1.10813f, -0.00605f),
+ vec3(-0.00327f, -0.07276f, 1.07602f));
+
+ color *= rgb_to_rrt;
+ vec3 color_tonemapped = (color * (color + A) - B) / (color * (C * color + D) + E);
+ color_tonemapped *= odt_to_rgb;
+
+ p_white *= exposure_bias;
+ float p_white_tonemapped = (p_white * (p_white + A) - B) / (p_white * (C * p_white + D) + E);
+
+ return color_tonemapped / p_white_tonemapped;
+}
+
+vec3 tonemap_reinhard(vec3 color, float p_white) {
+ return (p_white * color + color) / (color * p_white + p_white);
+}
+
+vec3 linear_to_srgb(vec3 color) {
+ //if going to srgb, clamp from 0 to 1.
+ color = clamp(color, vec3(0.0), vec3(1.0));
+ const vec3 a = vec3(0.055f);
+ return mix((vec3(1.0f) + a) * pow(color.rgb, vec3(1.0f / 2.4f)) - a, 12.92f * color.rgb, lessThan(color.rgb, vec3(0.0031308f)));
+}
+
+#define TONEMAPPER_LINEAR 0
+#define TONEMAPPER_REINHARD 1
+#define TONEMAPPER_FILMIC 2
+#define TONEMAPPER_ACES 3
+
+vec3 apply_tonemapping(vec3 color, float p_white) { // inputs are LINEAR, always outputs clamped [0;1] color
+ // Ensure color values passed to tonemappers are positive.
+ // They can be negative in the case of negative lights, which leads to undesired behavior.
+ if (tonemapper == TONEMAPPER_LINEAR) {
+ return color;
+ } else if (tonemapper == TONEMAPPER_REINHARD) {
+ return tonemap_reinhard(max(vec3(0.0f), color), p_white);
+ } else if (tonemapper == TONEMAPPER_FILMIC) {
+ return tonemap_filmic(max(vec3(0.0f), color), p_white);
+ } else { // TONEMAPPER_ACES
+ return tonemap_aces(max(vec3(0.0f), color), p_white);
+ }
+}