diff options
Diffstat (limited to 'drivers/gles2/shaders')
-rw-r--r-- | drivers/gles2/shaders/SCsub | 4 | ||||
-rw-r--r-- | drivers/gles2/shaders/canvas.glsl | 31 | ||||
-rw-r--r-- | drivers/gles2/shaders/canvas_shadow.glsl | 9 | ||||
-rw-r--r-- | drivers/gles2/shaders/copy.glsl | 4 | ||||
-rw-r--r-- | drivers/gles2/shaders/cube_to_dp.glsl | 5 | ||||
-rw-r--r-- | drivers/gles2/shaders/effect_blur.glsl | 244 | ||||
-rw-r--r-- | drivers/gles2/shaders/lens_distorted.glsl | 7 | ||||
-rw-r--r-- | drivers/gles2/shaders/scene.glsl | 19 | ||||
-rw-r--r-- | drivers/gles2/shaders/stdlib.glsl | 383 | ||||
-rw-r--r-- | drivers/gles2/shaders/tonemap.glsl | 296 |
10 files changed, 699 insertions, 303 deletions
diff --git a/drivers/gles2/shaders/SCsub b/drivers/gles2/shaders/SCsub index 085c43319c..d7ae0243e6 100644 --- a/drivers/gles2/shaders/SCsub +++ b/drivers/gles2/shaders/SCsub @@ -12,12 +12,12 @@ if 'GLES2_GLSL' in env['BUILDERS']: env.GLES2_GLSL('cube_to_dp.glsl'); # env.GLES2_GLSL('blend_shape.glsl'); # env.GLES2_GLSL('screen_space_reflection.glsl'); -# env.GLES2_GLSL('effect_blur.glsl'); + env.GLES2_GLSL('effect_blur.glsl'); # env.GLES2_GLSL('subsurf_scattering.glsl'); # env.GLES2_GLSL('ssao.glsl'); # env.GLES2_GLSL('ssao_minify.glsl'); # env.GLES2_GLSL('ssao_blur.glsl'); # env.GLES2_GLSL('exposure.glsl'); -# env.GLES2_GLSL('tonemap.glsl'); + env.GLES2_GLSL('tonemap.glsl'); # env.GLES2_GLSL('particles.glsl'); env.GLES2_GLSL('lens_distorted.glsl'); diff --git a/drivers/gles2/shaders/canvas.glsl b/drivers/gles2/shaders/canvas.glsl index 0818942b0a..08548ded17 100644 --- a/drivers/gles2/shaders/canvas.glsl +++ b/drivers/gles2/shaders/canvas.glsl @@ -258,6 +258,8 @@ precision mediump int; #endif #endif +#include "stdlib.glsl" + uniform sampler2D color_texture; // texunit:-1 /* clang-format on */ uniform highp vec2 color_texpixel_size; @@ -329,6 +331,7 @@ void light_compute( inout vec4 light_color, vec2 light_uv, inout vec4 shadow_color, + inout vec2 shadow_vec, vec3 normal, vec2 uv, #if defined(SCREEN_UV_USED) @@ -405,6 +408,7 @@ FRAGMENT_SHADER_CODE #ifdef USE_LIGHTING vec2 light_vec = transformed_light_uv; + vec2 shadow_vec = transformed_light_uv; if (normal_used) { normal.xy = mat2(local_rot.xy, local_rot.zw) * normal.xy; @@ -432,6 +436,7 @@ FRAGMENT_SHADER_CODE real_light_color, light_uv, real_light_shadow_color, + shadow_vec, normal, uv, #if defined(SCREEN_UV_USED) @@ -450,11 +455,18 @@ FRAGMENT_SHADER_CODE color *= light; #ifdef USE_SHADOWS - // Reset light_vec to compute shadows, the shadow map is created from the light origin, so it only - // makes sense to compute shadows from there. - light_vec = light_uv_interp.zw; - float angle_to_light = -atan(light_vec.x, light_vec.y); +#ifdef SHADOW_VEC_USED + mat3 inverse_light_matrix = mat3(light_matrix); + inverse_light_matrix[0] = normalize(inverse_light_matrix[0]); + inverse_light_matrix[1] = normalize(inverse_light_matrix[1]); + inverse_light_matrix[2] = normalize(inverse_light_matrix[2]); + shadow_vec = (inverse_light_matrix * vec3(shadow_vec, 0.0)).xy; +#else + shadow_vec = light_uv_interp.zw; +#endif + + float angle_to_light = -atan(shadow_vec.x, shadow_vec.y); float PI = 3.14159265358979323846264; /*int i = int(mod(floor((angle_to_light+7.0*PI/6.0)/(4.0*PI/6.0))+1.0, 3.0)); // +1 pq os indices estao em ordem 2,0,1 nos arrays float ang*/ @@ -465,18 +477,18 @@ FRAGMENT_SHADER_CODE vec2 point; float sh; if (abs_angle < 45.0 * PI / 180.0) { - point = light_vec; + point = shadow_vec; sh = 0.0 + (1.0 / 8.0); } else if (abs_angle > 135.0 * PI / 180.0) { - point = -light_vec; + point = -shadow_vec; sh = 0.5 + (1.0 / 8.0); } else if (angle_to_light > 0.0) { - point = vec2(light_vec.y, -light_vec.x); + point = vec2(shadow_vec.y, -shadow_vec.x); sh = 0.25 + (1.0 / 8.0); } else { - point = vec2(-light_vec.y, light_vec.x); + point = vec2(-shadow_vec.y, shadow_vec.x); sh = 0.75 + (1.0 / 8.0); } @@ -489,8 +501,7 @@ FRAGMENT_SHADER_CODE highp float shadow_attenuation = 0.0; #ifdef USE_RGBA_SHADOWS - -#define SHADOW_DEPTH(m_tex, m_uv) dot(texture2D((m_tex), (m_uv)), vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0)) +#define SHADOW_DEPTH(m_tex, m_uv) dot(texture2D((m_tex), (m_uv)), vec4(1.0 / (255.0 * 255.0 * 255.0), 1.0 / (255.0 * 255.0), 1.0 / 255.0, 1.0)) #else diff --git a/drivers/gles2/shaders/canvas_shadow.glsl b/drivers/gles2/shaders/canvas_shadow.glsl index 01b2c59325..7a5ba4f571 100644 --- a/drivers/gles2/shaders/canvas_shadow.glsl +++ b/drivers/gles2/shaders/canvas_shadow.glsl @@ -34,9 +34,14 @@ void main() { #define mediump #define highp #else +#if defined(USE_HIGHP_PRECISION) +precision highp float; +precision highp int; +#else precision mediump float; precision mediump int; #endif +#endif varying highp vec4 position_interp; /* clang-format on */ @@ -47,8 +52,8 @@ void main() { #ifdef USE_RGBA_SHADOWS - highp vec4 comp = fract(depth * vec4(256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0)); - comp -= comp.xxyz * vec4(0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0); + highp vec4 comp = fract(depth * vec4(255.0 * 255.0 * 255.0, 255.0 * 255.0, 255.0, 1.0)); + comp -= comp.xxyz * vec4(0.0, 1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0); gl_FragColor = comp; #else diff --git a/drivers/gles2/shaders/copy.glsl b/drivers/gles2/shaders/copy.glsl index 195db7c45f..aa967115da 100644 --- a/drivers/gles2/shaders/copy.glsl +++ b/drivers/gles2/shaders/copy.glsl @@ -144,11 +144,11 @@ void main() { #elif defined(USE_ASYM_PANO) // When an asymmetrical projection matrix is used (applicable for stereoscopic rendering i.e. VR) we need to do this calculation per fragment to get a perspective correct result. - // Note that we're ignoring the x-offset for IPD, with Z sufficiently in the distance it becomes neglectible, as a result we could probably just set cube_normal.z to -1. + // Asymmetrical projection means the center of projection is no longer in the center of the screen but shifted. // The Matrix[2][0] (= asym_proj.x) and Matrix[2][1] (= asym_proj.z) values are what provide the right shift in the image. vec3 cube_normal; - cube_normal.z = -1000000.0; + cube_normal.z = -1.0; cube_normal.x = (cube_normal.z * (-uv_interp.x - asym_proj.x)) / asym_proj.y; cube_normal.y = (cube_normal.z * (-uv_interp.y - asym_proj.z)) / asym_proj.a; cube_normal = mat3(sky_transform) * mat3(pano_transform) * cube_normal; diff --git a/drivers/gles2/shaders/cube_to_dp.glsl b/drivers/gles2/shaders/cube_to_dp.glsl index cb4b3f6dec..769908c3b4 100644 --- a/drivers/gles2/shaders/cube_to_dp.glsl +++ b/drivers/gles2/shaders/cube_to_dp.glsl @@ -30,9 +30,14 @@ void main() { #define mediump #define highp #else +#if defined(USE_HIGHP_PRECISION) +precision highp float; +precision highp int; +#else precision mediump float; precision mediump int; #endif +#endif uniform highp samplerCube source_cube; //texunit:0 /* clang-format on */ diff --git a/drivers/gles2/shaders/effect_blur.glsl b/drivers/gles2/shaders/effect_blur.glsl index df79e89931..b28d78a6ca 100644 --- a/drivers/gles2/shaders/effect_blur.glsl +++ b/drivers/gles2/shaders/effect_blur.glsl @@ -1,11 +1,20 @@ /* clang-format off */ [vertex] -layout(location = 0) in highp vec4 vertex_attrib; +#ifdef USE_GLES_OVER_GL +#define lowp +#define mediump +#define highp +#else +precision highp float; +precision highp int; +#endif + +attribute vec2 vertex_attrib; // attrib:0 /* clang-format on */ -layout(location = 4) in vec2 uv_in; +attribute vec2 uv_in; // attrib:4 -out vec2 uv_interp; +varying vec2 uv_interp; #ifdef USE_BLUR_SECTION @@ -16,7 +25,7 @@ uniform vec4 blur_section; void main() { uv_interp = uv_in; - gl_Position = vertex_attrib; + gl_Position = vec4(vertex_attrib, 0.0, 1.0); #ifdef USE_BLUR_SECTION uv_interp = blur_section.xy + uv_interp * blur_section.zw; @@ -27,29 +36,46 @@ void main() { /* clang-format off */ [fragment] -#if !defined(GLES_OVER_GL) +// texture2DLodEXT and textureCubeLodEXT are fragment shader specific. +// Do not copy these defines in the vertex section. +#ifndef USE_GLES_OVER_GL +#ifdef GL_EXT_shader_texture_lod +#extension GL_EXT_shader_texture_lod : enable +#define texture2DLod(img, coord, lod) texture2DLodEXT(img, coord, lod) +#define textureCubeLod(img, coord, lod) textureCubeLodEXT(img, coord, lod) +#endif +#endif // !USE_GLES_OVER_GL + +#ifdef GL_ARB_shader_texture_lod +#extension GL_ARB_shader_texture_lod : enable +#endif + +#if !defined(GL_EXT_shader_texture_lod) && !defined(GL_ARB_shader_texture_lod) +#define texture2DLod(img, coord, lod) texture2D(img, coord, lod) +#define textureCubeLod(img, coord, lod) textureCube(img, coord, lod) +#endif + +#ifdef USE_GLES_OVER_GL +#define lowp +#define mediump +#define highp +#else +#if defined(USE_HIGHP_PRECISION) +precision highp float; +precision highp int; +#else precision mediump float; +precision mediump int; +#endif #endif -in vec2 uv_interp; +varying vec2 uv_interp; /* clang-format on */ uniform sampler2D source_color; //texunit:0 -#ifdef SSAO_MERGE -uniform sampler2D source_ssao; //texunit:1 -#endif - uniform float lod; uniform vec2 pixel_size; -layout(location = 0) out vec4 frag_color; - -#ifdef SSAO_MERGE - -uniform vec4 ssao_color; - -#endif - #if defined(GLOW_GAUSSIAN_HORIZONTAL) || defined(GLOW_GAUSSIAN_VERTICAL) uniform float glow_strength; @@ -58,6 +84,7 @@ uniform float glow_strength; #if defined(DOF_FAR_BLUR) || defined(DOF_NEAR_BLUR) +#ifdef USE_GLES_OVER_GL #ifdef DOF_QUALITY_LOW const int dof_kernel_size = 5; const int dof_kernel_from = 2; @@ -76,6 +103,7 @@ const int dof_kernel_size = 21; const int dof_kernel_from = 10; const float dof_kernel[21] = float[](0.028174, 0.032676, 0.037311, 0.041944, 0.046421, 0.050582, 0.054261, 0.057307, 0.059587, 0.060998, 0.061476, 0.060998, 0.059587, 0.057307, 0.054261, 0.050582, 0.046421, 0.041944, 0.037311, 0.032676, 0.028174); #endif +#endif uniform sampler2D dof_source_depth; //texunit:1 uniform float dof_begin; @@ -83,24 +111,11 @@ uniform float dof_end; uniform vec2 dof_dir; uniform float dof_radius; -#ifdef DOF_NEAR_BLUR_MERGE - -uniform sampler2D source_dof_original; //texunit:2 -#endif - #endif #ifdef GLOW_FIRST_PASS -uniform float exposure; -uniform float white; - -#ifdef GLOW_USE_AUTO_EXPOSURE - -uniform highp sampler2D source_auto_exposure; //texunit:1 -uniform highp float auto_exposure_grey; - -#endif +uniform highp float luminance_cap; uniform float glow_bloom; uniform float glow_hdr_threshold; @@ -113,60 +128,95 @@ uniform float camera_z_near; void main() { -#ifdef GAUSSIAN_HORIZONTAL +#ifdef GLOW_GAUSSIAN_HORIZONTAL vec2 pix_size = pixel_size; pix_size *= 0.5; //reading from larger buffer, so use more samples - // sigma 2 - vec4 color = textureLod(source_color, uv_interp + vec2(0.0, 0.0) * pix_size, lod) * 0.214607; - color += textureLod(source_color, uv_interp + vec2(1.0, 0.0) * pix_size, lod) * 0.189879; - color += textureLod(source_color, uv_interp + vec2(2.0, 0.0) * pix_size, lod) * 0.131514; - color += textureLod(source_color, uv_interp + vec2(3.0, 0.0) * pix_size, lod) * 0.071303; - color += textureLod(source_color, uv_interp + vec2(-1.0, 0.0) * pix_size, lod) * 0.189879; - color += textureLod(source_color, uv_interp + vec2(-2.0, 0.0) * pix_size, lod) * 0.131514; - color += textureLod(source_color, uv_interp + vec2(-3.0, 0.0) * pix_size, lod) * 0.071303; - frag_color = color; + vec4 color = texture2DLod(source_color, uv_interp + vec2(0.0, 0.0) * pix_size, lod) * 0.174938; + color += texture2DLod(source_color, uv_interp + vec2(1.0, 0.0) * pix_size, lod) * 0.165569; + color += texture2DLod(source_color, uv_interp + vec2(2.0, 0.0) * pix_size, lod) * 0.140367; + color += texture2DLod(source_color, uv_interp + vec2(3.0, 0.0) * pix_size, lod) * 0.106595; + color += texture2DLod(source_color, uv_interp + vec2(-1.0, 0.0) * pix_size, lod) * 0.165569; + color += texture2DLod(source_color, uv_interp + vec2(-2.0, 0.0) * pix_size, lod) * 0.140367; + color += texture2DLod(source_color, uv_interp + vec2(-3.0, 0.0) * pix_size, lod) * 0.106595; + color *= glow_strength; + gl_FragColor = color; #endif -#ifdef GAUSSIAN_VERTICAL - vec4 color = textureLod(source_color, uv_interp + vec2(0.0, 0.0) * pixel_size, lod) * 0.38774; - color += textureLod(source_color, uv_interp + vec2(0.0, 1.0) * pixel_size, lod) * 0.24477; - color += textureLod(source_color, uv_interp + vec2(0.0, 2.0) * pixel_size, lod) * 0.06136; - color += textureLod(source_color, uv_interp + vec2(0.0, -1.0) * pixel_size, lod) * 0.24477; - color += textureLod(source_color, uv_interp + vec2(0.0, -2.0) * pixel_size, lod) * 0.06136; - frag_color = color; +#ifdef GLOW_GAUSSIAN_VERTICAL + vec4 color = texture2DLod(source_color, uv_interp + vec2(0.0, 0.0) * pixel_size, lod) * 0.288713; + color += texture2DLod(source_color, uv_interp + vec2(0.0, 1.0) * pixel_size, lod) * 0.233062; + color += texture2DLod(source_color, uv_interp + vec2(0.0, 2.0) * pixel_size, lod) * 0.122581; + color += texture2DLod(source_color, uv_interp + vec2(0.0, -1.0) * pixel_size, lod) * 0.233062; + color += texture2DLod(source_color, uv_interp + vec2(0.0, -2.0) * pixel_size, lod) * 0.122581; + color *= glow_strength; + gl_FragColor = color; #endif - //glow uses larger sigma for a more rounded blur effect +#ifndef USE_GLES_OVER_GL +#if defined(DOF_FAR_BLUR) || defined(DOF_NEAR_BLUR) -#ifdef GLOW_GAUSSIAN_HORIZONTAL - vec2 pix_size = pixel_size; - pix_size *= 0.5; //reading from larger buffer, so use more samples - vec4 color = textureLod(source_color, uv_interp + vec2(0.0, 0.0) * pix_size, lod) * 0.174938; - color += textureLod(source_color, uv_interp + vec2(1.0, 0.0) * pix_size, lod) * 0.165569; - color += textureLod(source_color, uv_interp + vec2(2.0, 0.0) * pix_size, lod) * 0.140367; - color += textureLod(source_color, uv_interp + vec2(3.0, 0.0) * pix_size, lod) * 0.106595; - color += textureLod(source_color, uv_interp + vec2(-1.0, 0.0) * pix_size, lod) * 0.165569; - color += textureLod(source_color, uv_interp + vec2(-2.0, 0.0) * pix_size, lod) * 0.140367; - color += textureLod(source_color, uv_interp + vec2(-3.0, 0.0) * pix_size, lod) * 0.106595; - color *= glow_strength; - frag_color = color; +#ifdef DOF_QUALITY_LOW + const int dof_kernel_size = 5; + const int dof_kernel_from = 2; + float dof_kernel[5]; + dof_kernel[0] = 0.153388; + dof_kernel[1] = 0.221461; + dof_kernel[2] = 0.250301; + dof_kernel[3] = 0.221461; + dof_kernel[4] = 0.153388; #endif -#ifdef GLOW_GAUSSIAN_VERTICAL - vec4 color = textureLod(source_color, uv_interp + vec2(0.0, 0.0) * pixel_size, lod) * 0.288713; - color += textureLod(source_color, uv_interp + vec2(0.0, 1.0) * pixel_size, lod) * 0.233062; - color += textureLod(source_color, uv_interp + vec2(0.0, 2.0) * pixel_size, lod) * 0.122581; - color += textureLod(source_color, uv_interp + vec2(0.0, -1.0) * pixel_size, lod) * 0.233062; - color += textureLod(source_color, uv_interp + vec2(0.0, -2.0) * pixel_size, lod) * 0.122581; - color *= glow_strength; - frag_color = color; +#ifdef DOF_QUALITY_MEDIUM + const int dof_kernel_size = 11; + const int dof_kernel_from = 5; + float dof_kernel[11]; + dof_kernel[0] = 0.055037; + dof_kernel[1] = 0.072806; + dof_kernel[2] = 0.090506; + dof_kernel[3] = 0.105726; + dof_kernel[4] = 0.116061; + dof_kernel[5] = 0.119726; + dof_kernel[6] = 0.116061; + dof_kernel[7] = 0.105726; + dof_kernel[8] = 0.090506; + dof_kernel[9] = 0.072806; + dof_kernel[10] = 0.055037; #endif +#ifdef DOF_QUALITY_HIGH + const int dof_kernel_size = 21; + const int dof_kernel_from = 10; + float dof_kernel[21]; + dof_kernel[0] = 0.028174; + dof_kernel[1] = 0.032676; + dof_kernel[2] = 0.037311; + dof_kernel[3] = 0.041944; + dof_kernel[4] = 0.046421; + dof_kernel[5] = 0.050582; + dof_kernel[6] = 0.054261; + dof_kernel[7] = 0.057307; + dof_kernel[8] = 0.059587; + dof_kernel[9] = 0.060998; + dof_kernel[10] = 0.061476; + dof_kernel[11] = 0.060998; + dof_kernel[12] = 0.059587; + dof_kernel[13] = 0.057307; + dof_kernel[14] = 0.054261; + dof_kernel[15] = 0.050582; + dof_kernel[16] = 0.046421; + dof_kernel[17] = 0.041944; + dof_kernel[18] = 0.037311; + dof_kernel[19] = 0.032676; + dof_kernel[20] = 0.028174; +#endif +#endif +#endif //!USE_GLES_OVER_GL + #ifdef DOF_FAR_BLUR vec4 color_accum = vec4(0.0); - float depth = textureLod(dof_source_depth, uv_interp, 0.0).r; + float depth = texture2DLod(dof_source_depth, uv_interp, 0.0).r; depth = depth * 2.0 - 1.0; #ifdef USE_ORTHOGONAL_PROJECTION depth = ((depth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0; @@ -184,17 +234,17 @@ void main() { float tap_k = dof_kernel[i]; - float tap_depth = texture(dof_source_depth, tap_uv, 0.0).r; + float tap_depth = texture2D(dof_source_depth, tap_uv, 0.0).r; tap_depth = tap_depth * 2.0 - 1.0; #ifdef USE_ORTHOGONAL_PROJECTION tap_depth = ((tap_depth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0; #else tap_depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - tap_depth * (camera_z_far - camera_z_near)); #endif - float tap_amount = mix(smoothstep(dof_begin, dof_end, tap_depth), 1.0, int_ofs == 0); + float tap_amount = int_ofs == 0 ? 1.0 : smoothstep(dof_begin, dof_end, tap_depth); tap_amount *= tap_amount * tap_amount; //prevent undesired glow effect - vec4 tap_color = textureLod(source_color, tap_uv, 0.0) * tap_k; + vec4 tap_color = texture2DLod(source_color, tap_uv, 0.0) * tap_k; k_accum += tap_k * tap_amount; color_accum += tap_color * tap_amount; @@ -204,7 +254,7 @@ void main() { color_accum /= k_accum; } - frag_color = color_accum; ///k_accum; + gl_FragColor = color_accum; ///k_accum; #endif @@ -212,19 +262,19 @@ void main() { vec4 color_accum = vec4(0.0); - float max_accum = 0; + float max_accum = 0.0; for (int i = 0; i < dof_kernel_size; i++) { int int_ofs = i - dof_kernel_from; vec2 tap_uv = uv_interp + dof_dir * float(int_ofs) * dof_radius; - float ofs_influence = max(0.0, 1.0 - float(abs(int_ofs)) / float(dof_kernel_from)); + float ofs_influence = max(0.0, 1.0 - abs(float(int_ofs)) / float(dof_kernel_from)); float tap_k = dof_kernel[i]; - vec4 tap_color = textureLod(source_color, tap_uv, 0.0); + vec4 tap_color = texture2DLod(source_color, tap_uv, 0.0); - float tap_depth = texture(dof_source_depth, tap_uv, 0.0).r; + float tap_depth = texture2D(dof_source_depth, tap_uv, 0.0).r; tap_depth = tap_depth * 2.0 - 1.0; #ifdef USE_ORTHOGONAL_PROJECTION tap_depth = ((tap_depth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0; @@ -247,46 +297,16 @@ void main() { color_accum.a = max(color_accum.a, sqrt(max_accum)); -#ifdef DOF_NEAR_BLUR_MERGE - - vec4 original = textureLod(source_dof_original, uv_interp, 0.0); - color_accum = mix(original, color_accum, color_accum.a); - -#endif - -#ifndef DOF_NEAR_FIRST_TAP - //color_accum=vec4(vec3(color_accum.a),1.0); -#endif - frag_color = color_accum; + gl_FragColor = color_accum; #endif #ifdef GLOW_FIRST_PASS -#ifdef GLOW_USE_AUTO_EXPOSURE - - frag_color /= texelFetch(source_auto_exposure, ivec2(0, 0), 0).r / auto_exposure_grey; -#endif - frag_color *= exposure; - - float luminance = max(frag_color.r, max(frag_color.g, frag_color.b)); + float luminance = max(gl_FragColor.r, max(gl_FragColor.g, gl_FragColor.b)); float feedback = max(smoothstep(glow_hdr_threshold, glow_hdr_threshold + glow_hdr_scale, luminance), glow_bloom); - frag_color *= feedback; - -#endif - -#ifdef SIMPLE_COPY - vec4 color = textureLod(source_color, uv_interp, 0.0); - frag_color = color; -#endif - -#ifdef SSAO_MERGE - - vec4 color = textureLod(source_color, uv_interp, 0.0); - float ssao = textureLod(source_ssao, uv_interp, 0.0).r; - - frag_color = vec4(mix(color.rgb, color.rgb * mix(ssao_color.rgb, vec3(1.0), ssao), color.a), 1.0); + gl_FragColor = min(gl_FragColor * feedback, vec4(luminance_cap)); #endif } diff --git a/drivers/gles2/shaders/lens_distorted.glsl b/drivers/gles2/shaders/lens_distorted.glsl index 81898a75a5..f4ff80ba9a 100644 --- a/drivers/gles2/shaders/lens_distorted.glsl +++ b/drivers/gles2/shaders/lens_distorted.glsl @@ -34,8 +34,13 @@ void main() { #define mediump #define highp #else -precision mediump float; +#if defined(USE_HIGHP_PRECISION) +precision highp float; precision highp int; +#else +precision mediump float; +precision mediump int; +#endif #endif uniform sampler2D source; //texunit:0 diff --git a/drivers/gles2/shaders/scene.glsl b/drivers/gles2/shaders/scene.glsl index b7f8ec3ce9..57c2d886b3 100644 --- a/drivers/gles2/shaders/scene.glsl +++ b/drivers/gles2/shaders/scene.glsl @@ -10,7 +10,9 @@ precision highp float; precision highp int; #endif +/* clang-format on */ #include "stdlib.glsl" +/* clang-format off */ #define SHADER_IS_SRGB true @@ -59,10 +61,6 @@ uniform ivec2 skeleton_texture_size; #endif -uniform highp mat4 skeleton_transform; -uniform highp mat4 skeleton_transform_inverse; -uniform bool skeleton_in_world_coords; - #endif #ifdef USE_INSTANCING @@ -408,12 +406,7 @@ void main() { #endif - if (skeleton_in_world_coords) { - bone_transform = skeleton_transform * (bone_transform * skeleton_transform_inverse); - world_matrix = bone_transform * world_matrix; - } else { - world_matrix = world_matrix * bone_transform; - } + world_matrix = world_matrix * bone_transform; #endif @@ -1365,7 +1358,7 @@ LIGHT_SHADER_CODE #ifdef USE_RGBA_SHADOWS -#define SHADOW_DEPTH(m_val) dot(m_val, vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0)) +#define SHADOW_DEPTH(m_val) dot(m_val, vec4(1.0 / (255.0 * 255.0 * 255.0), 1.0 / (255.0 * 255.0), 1.0 / 255.0, 1.0)) #else @@ -2207,8 +2200,8 @@ FRAGMENT_SHADER_CODE #ifdef USE_RGBA_SHADOWS highp float depth = ((position_interp.z / position_interp.w) + 1.0) * 0.5 + 0.0; // bias - highp vec4 comp = fract(depth * vec4(256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0)); - comp -= comp.xxyz * vec4(0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0); + highp vec4 comp = fract(depth * vec4(255.0 * 255.0 * 255.0, 255.0 * 255.0, 255.0, 1.0)); + comp -= comp.xxyz * vec4(0.0, 1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0); gl_FragColor = comp; #endif diff --git a/drivers/gles2/shaders/stdlib.glsl b/drivers/gles2/shaders/stdlib.glsl index 3674d70c9f..9c74418743 100644 --- a/drivers/gles2/shaders/stdlib.glsl +++ b/drivers/gles2/shaders/stdlib.glsl @@ -36,12 +36,385 @@ highp vec4 texel2DFetch(highp sampler2D tex, ivec2 size, ivec2 coord) { return texture2DLod(tex, vec2(x_coord, y_coord), 0.0); } +#if defined(SINH_USED) + +highp float sinh(highp float x) { + return 0.5 * (exp(x) - exp(-x)); +} + +highp vec2 sinh(highp vec2 x) { + return 0.5 * vec2(exp(x.x) - exp(-x.x), exp(x.y) - exp(-x.y)); +} + +highp vec3 sinh(highp vec3 x) { + return 0.5 * vec3(exp(x.x) - exp(-x.x), exp(x.y) - exp(-x.y), exp(x.z) - exp(-x.z)); +} + +highp vec4 sinh(highp vec4 x) { + return 0.5 * vec4(exp(x.x) - exp(-x.x), exp(x.y) - exp(-x.y), exp(x.z) - exp(-x.z), exp(x.w) - exp(-x.w)); +} + +#endif + +#if defined(COSH_USED) + +highp float cosh(highp float x) { + return 0.5 * (exp(x) + exp(-x)); +} + +highp vec2 cosh(highp vec2 x) { + return 0.5 * vec2(exp(x.x) + exp(-x.x), exp(x.y) + exp(-x.y)); +} + +highp vec3 cosh(highp vec3 x) { + return 0.5 * vec3(exp(x.x) + exp(-x.x), exp(x.y) + exp(-x.y), exp(x.z) + exp(-x.z)); +} + +highp vec4 cosh(highp vec4 x) { + return 0.5 * vec4(exp(x.x) + exp(-x.x), exp(x.y) + exp(-x.y), exp(x.z) + exp(-x.z), exp(x.w) + exp(-x.w)); +} + +#endif + +#if defined(TANH_USED) + +highp float tanh(highp float x) { + highp float exp2x = exp(2.0 * x); + return (exp2x - 1.0) / (exp2x + 1.0); +} + +highp vec2 tanh(highp vec2 x) { + highp float exp2x = exp(2.0 * x.x); + highp float exp2y = exp(2.0 * x.y); + return vec2((exp2x - 1.0) / (exp2x + 1.0), (exp2y - 1.0) / (exp2y + 1.0)); +} + +highp vec3 tanh(highp vec3 x) { + highp float exp2x = exp(2.0 * x.x); + highp float exp2y = exp(2.0 * x.y); + highp float exp2z = exp(2.0 * x.z); + return vec3((exp2x - 1.0) / (exp2x + 1.0), (exp2y - 1.0) / (exp2y + 1.0), (exp2z - 1.0) / (exp2z + 1.0)); +} + +highp vec4 tanh(highp vec4 x) { + highp float exp2x = exp(2.0 * x.x); + highp float exp2y = exp(2.0 * x.y); + highp float exp2z = exp(2.0 * x.z); + highp float exp2w = exp(2.0 * x.w); + return vec4((exp2x - 1.0) / (exp2x + 1.0), (exp2y - 1.0) / (exp2y + 1.0), (exp2z - 1.0) / (exp2z + 1.0), (exp2w - 1.0) / (exp2w + 1.0)); +} + +#endif + +#if defined(ASINH_USED) + +highp float asinh(highp float x) { + return sign(x) * log(abs(x) + sqrt(1.0 + x * x)); +} + +highp vec2 asinh(highp vec2 x) { + return vec2(sign(x.x) * log(abs(x.x) + sqrt(1.0 + x.x * x.x)), sign(x.y) * log(abs(x.y) + sqrt(1.0 + x.y * x.y))); +} + +highp vec3 asinh(highp vec3 x) { + return vec3(sign(x.x) * log(abs(x.x) + sqrt(1.0 + x.x * x.x)), sign(x.y) * log(abs(x.y) + sqrt(1.0 + x.y * x.y)), sign(x.z) * log(abs(x.z) + sqrt(1.0 + x.z * x.z))); +} + +highp vec4 asinh(highp vec4 x) { + return vec4(sign(x.x) * log(abs(x.x) + sqrt(1.0 + x.x * x.x)), sign(x.y) * log(abs(x.y) + sqrt(1.0 + x.y * x.y)), sign(x.z) * log(abs(x.z) + sqrt(1.0 + x.z * x.z)), sign(x.w) * log(abs(x.w) + sqrt(1.0 + x.w * x.w))); +} + +#endif + +#if defined(ACOSH_USED) + +highp float acosh(highp float x) { + return log(x + sqrt(x * x - 1.0)); +} + +highp vec2 acosh(highp vec2 x) { + return vec2(log(x.x + sqrt(x.x * x.x - 1.0)), log(x.y + sqrt(x.y * x.y - 1.0))); +} + +highp vec3 acosh(highp vec3 x) { + return vec3(log(x.x + sqrt(x.x * x.x - 1.0)), log(x.y + sqrt(x.y * x.y - 1.0)), log(x.z + sqrt(x.z * x.z - 1.0))); +} + +highp vec4 acosh(highp vec4 x) { + return vec4(log(x.x + sqrt(x.x * x.x - 1.0)), log(x.y + sqrt(x.y * x.y - 1.0)), log(x.z + sqrt(x.z * x.z - 1.0)), log(x.w + sqrt(x.w * x.w - 1.0))); +} + +#endif + +#if defined(ATANH_USED) + +highp float atanh(highp float x) { + return 0.5 * log((1.0 + x) / (1.0 - x)); +} + +highp vec2 atanh(highp vec2 x) { + return 0.5 * vec2(log((1.0 + x.x) / (1.0 - x.x)), log((1.0 + x.y) / (1.0 - x.y))); +} + +highp vec3 atanh(highp vec3 x) { + return 0.5 * vec3(log((1.0 + x.x) / (1.0 - x.x)), log((1.0 + x.y) / (1.0 - x.y)), log((1.0 + x.z) / (1.0 - x.z))); +} + +highp vec4 atanh(highp vec4 x) { + return 0.5 * vec4(log((1.0 + x.x) / (1.0 - x.x)), log((1.0 + x.y) / (1.0 - x.y)), log((1.0 + x.z) / (1.0 - x.z)), log((1.0 + x.w) / (1.0 - x.w))); +} + +#endif + +#if defined(ROUND_USED) + +highp float round(highp float x) { + return floor(x + 0.5); +} + +highp vec2 round(highp vec2 x) { + return floor(x + vec2(0.5)); +} + +highp vec3 round(highp vec3 x) { + return floor(x + vec3(0.5)); +} + +highp vec4 round(highp vec4 x) { + return floor(x + vec4(0.5)); +} + +#endif + +#if defined(ROUND_EVEN_USED) + +highp float roundEven(highp float x) { + highp float t = x + 0.5; + highp float f = floor(t); + highp float r; + if (t == f) { + if (x > 0) + r = f - mod(f, 2); + else + r = f + mod(f, 2); + } else + r = f; + return r; +} + +highp vec2 roundEven(highp vec2 x) { + return vec2(roundEven(x.x), roundEven(x.y)); +} + +highp vec3 roundEven(highp vec3 x) { + return vec3(roundEven(x.x), roundEven(x.y), roundEven(x.z)); +} + +highp vec4 roundEven(highp vec4 x) { + return vec4(roundEven(x.x), roundEven(x.y), roundEven(x.z), roundEven(x.w)); +} + +#endif + +#if defined(IS_INF_USED) + +bool isinf(highp float x) { + return (2 * x == x) && (x != 0); +} + +bvec2 isinf(highp vec2 x) { + return bvec2((2 * x.x == x.x) && (x.x != 0), (2 * x.y == x.y) && (x.y != 0)); +} + +bvec3 isinf(highp vec3 x) { + return bvec3((2 * x.x == x.x) && (x.x != 0), (2 * x.y == x.y) && (x.y != 0), (2 * x.z == x.z) && (x.z != 0)); +} + +bvec4 isinf(highp vec4 x) { + return bvec4((2 * x.x == x.x) && (x.x != 0), (2 * x.y == x.y) && (x.y != 0), (2 * x.z == x.z) && (x.z != 0), (2 * x.w == x.w) && (x.w != 0)); +} + +#endif + +#if defined(IS_NAN_USED) + +bool isnan(highp float x) { + return x != x; +} + +bvec2 isnan(highp vec2 x) { + return bvec2(x.x != x.x, x.y != x.y); +} + +bvec3 isnan(highp vec3 x) { + return bvec3(x.x != x.x, x.y != x.y, x.z != x.z); +} + +bvec4 isnan(highp vec4 x) { + return bvec4(x.x != x.x, x.y != x.y, x.z != x.z, x.w != x.w); +} + +#endif + +#if defined(TRUNC_USED) + +highp float trunc(highp float x) { + return x < 0 ? -floor(-x) : floor(x); +} + +highp vec2 trunc(highp vec2 x) { + return vec2(x.x < 0 ? -floor(-x.x) : floor(x.x), x.y < 0 ? -floor(-x.y) : floor(x.y)); +} + +highp vec3 trunc(highp vec3 x) { + return vec3(x.x < 0 ? -floor(-x.x) : floor(x.x), x.y < 0 ? -floor(-x.y) : floor(x.y), x.z < 0 ? -floor(-x.z) : floor(x.z)); +} + +highp vec4 trunc(highp vec4 x) { + return vec4(x.x < 0 ? -floor(-x.x) : floor(x.x), x.y < 0 ? -floor(-x.y) : floor(x.y), x.z < 0 ? -floor(-x.z) : floor(x.z), x.w < 0 ? -floor(-x.w) : floor(x.w)); +} + +#endif + +#if defined(DETERMINANT_USED) + +highp float determinant(highp mat2 m) { + return m[0].x * m[1].y - m[1].x * m[0].y; +} + +highp float determinant(highp mat3 m) { + return m[0].x * (m[1].y * m[2].z - m[2].y * m[1].z) - m[1].x * (m[0].y * m[2].z - m[2].y * m[0].z) + m[2].x * (m[0].y * m[1].z - m[1].y * m[0].z); +} + +highp float determinant(highp mat4 m) { + highp float s00 = m[2].z * m[3].w - m[3].z * m[2].w; + highp float s01 = m[2].y * m[3].w - m[3].y * m[2].w; + highp float s02 = m[2].y * m[3].z - m[3].y * m[2].z; + highp float s03 = m[2].x * m[3].w - m[3].x * m[2].w; + highp float s04 = m[2].x * m[3].z - m[3].x * m[2].z; + highp float s05 = m[2].x * m[3].y - m[3].x * m[2].y; + highp vec4 c = vec4((m[1].y * s00 - m[1].z * s01 + m[1].w * s02), -(m[1].x * s00 - m[1].z * s03 + m[1].w * s04), (m[1].x * s01 - m[1].y * s03 + m[1].w * s05), -(m[1].x * s02 - m[1].y * s04 + m[1].z * s05)); + return m[0].x * c.x + m[0].y * c.y + m[0].z * c.z + m[0].w * c.w; +} + +#endif + +#if defined(INVERSE_USED) + +highp mat2 inverse(highp mat2 m) { + highp float d = 1.0 / (m[0].x * m[1].y - m[1].x * m[0].y); + return mat2( + vec2(m[1].y * d, -m[0].y * d), + vec2(-m[1].x * d, m[0].x * d)); +} + +highp mat3 inverse(highp mat3 m) { + highp float d = 1.0 / (m[0].x * (m[1].y * m[2].z - m[2].y * m[1].z) - m[1].x * (m[0].y * m[2].z - m[2].y * m[0].z) + m[2].x * (m[0].y * m[1].z - m[1].y * m[0].z)); + return mat3( + vec3((m[1].y * m[2].z - m[2].y * m[1].z), -(m[1].x * m[2].z - m[2].x * m[1].z), (m[1].x * m[2].y - m[2].x * m[1].y)) * d, + vec3(-(m[0].y * m[2].z - m[2].y * m[0].z), (m[0].x * m[2].z - m[2].x * m[0].z), -(m[0].x * m[2].y - m[2].x * m[0].y)) * d, + vec3((m[0].y * m[1].z - m[1].y * m[0].z), -(m[0].x * m[1].z - m[1].x * m[0].z), (m[0].x * m[1].y - m[1].x * m[0].y)) * d); +} + +highp mat4 inverse(highp mat4 m) { + highp float c00 = m[2].z * m[3].w - m[3].z * m[2].w; + highp float c02 = m[1].z * m[3].w - m[3].z * m[1].w; + highp float c03 = m[1].z * m[2].w - m[2].z * m[1].w; + + highp float c04 = m[2].y * m[3].w - m[3].y * m[2].w; + highp float c06 = m[1].y * m[3].w - m[3].y * m[1].w; + highp float c07 = m[1].y * m[2].w - m[2].y * m[1].w; + + highp float c08 = m[2].y * m[3].z - m[3].y * m[2].z; + highp float c10 = m[1].y * m[3].z - m[3].y * m[1].z; + highp float c11 = m[1].y * m[2].z - m[2].y * m[1].z; + + highp float c12 = m[2].x * m[3].w - m[3].x * m[2].w; + highp float c14 = m[1].x * m[3].w - m[3].x * m[1].w; + highp float c15 = m[1].x * m[2].w - m[2].x * m[1].w; + + highp float c16 = m[2].x * m[3].z - m[3].x * m[2].z; + highp float c18 = m[1].x * m[3].z - m[3].x * m[1].z; + highp float c19 = m[1].x * m[2].z - m[2].x * m[1].z; + + highp float c20 = m[2].x * m[3].y - m[3].x * m[2].y; + highp float c22 = m[1].x * m[3].y - m[3].x * m[1].y; + highp float c23 = m[1].x * m[2].y - m[2].x * m[1].y; + + vec4 f0 = vec4(c00, c00, c02, c03); + vec4 f1 = vec4(c04, c04, c06, c07); + vec4 f2 = vec4(c08, c08, c10, c11); + vec4 f3 = vec4(c12, c12, c14, c15); + vec4 f4 = vec4(c16, c16, c18, c19); + vec4 f5 = vec4(c20, c20, c22, c23); + + vec4 v0 = vec4(m[1].x, m[0].x, m[0].x, m[0].x); + vec4 v1 = vec4(m[1].y, m[0].y, m[0].y, m[0].y); + vec4 v2 = vec4(m[1].z, m[0].z, m[0].z, m[0].z); + vec4 v3 = vec4(m[1].w, m[0].w, m[0].w, m[0].w); + + vec4 inv0 = vec4(v1 * f0 - v2 * f1 + v3 * f2); + vec4 inv1 = vec4(v0 * f0 - v2 * f3 + v3 * f4); + vec4 inv2 = vec4(v0 * f1 - v1 * f3 + v3 * f5); + vec4 inv3 = vec4(v0 * f2 - v1 * f4 + v2 * f5); + + vec4 sa = vec4(+1, -1, +1, -1); + vec4 sb = vec4(-1, +1, -1, +1); + + mat4 inv = mat4(inv0 * sa, inv1 * sb, inv2 * sa, inv3 * sb); + + vec4 r0 = vec4(inv[0].x, inv[1].x, inv[2].x, inv[3].x); + vec4 d0 = vec4(m[0] * r0); + + highp float d1 = (d0.x + d0.y) + (d0.z + d0.w); + highp float d = 1.0 / d1; + + return inv * d; +} + +#endif + #ifndef USE_GLES_OVER_GL -highp mat4 transpose(highp mat4 src) { + +#if defined(TRANSPOSE_USED) + +highp mat2 transpose(highp mat2 m) { + return mat2( + vec2(m[0].x, m[1].x), + vec2(m[0].y, m[1].y)); +} + +highp mat3 transpose(highp mat3 m) { + return mat3( + vec3(m[0].x, m[1].x, m[2].x), + vec3(m[0].y, m[1].y, m[2].y), + vec3(m[0].z, m[1].z, m[2].z)); +} + +#endif + +highp mat4 transpose(highp mat4 m) { return mat4( - vec4(src[0].x, src[1].x, src[2].x, src[3].x), - vec4(src[0].y, src[1].y, src[2].y, src[3].y), - vec4(src[0].z, src[1].z, src[2].z, src[3].z), - vec4(src[0].w, src[1].w, src[2].w, src[3].w)); + vec4(m[0].x, m[1].x, m[2].x, m[3].x), + vec4(m[0].y, m[1].y, m[2].y, m[3].y), + vec4(m[0].z, m[1].z, m[2].z, m[3].z), + vec4(m[0].w, m[1].w, m[2].w, m[3].w)); } + +#if defined(OUTER_PRODUCT_USED) + +highp mat2 outerProduct(highp vec2 c, highp vec2 r) { + return mat2(c * r.x, c * r.y); +} + +highp mat3 outerProduct(highp vec3 c, highp vec3 r) { + return mat3(c * r.x, c * r.y, c * r.z); +} + +highp mat4 outerProduct(highp vec4 c, highp vec4 r) { + return mat4(c * r.x, c * r.y, c * r.z, c * r.w); +} + +#endif + #endif diff --git a/drivers/gles2/shaders/tonemap.glsl b/drivers/gles2/shaders/tonemap.glsl index eae3b5a1ca..585d821626 100644 --- a/drivers/gles2/shaders/tonemap.glsl +++ b/drivers/gles2/shaders/tonemap.glsl @@ -1,66 +1,103 @@ /* clang-format off */ [vertex] -layout(location = 0) in highp vec4 vertex_attrib; +#ifdef USE_GLES_OVER_GL +#define lowp +#define mediump +#define highp +#else +precision highp float; +precision highp int; +#endif + +attribute vec2 vertex_attrib; // attrib:0 /* clang-format on */ -layout(location = 4) in vec2 uv_in; +attribute vec2 uv_in; // attrib:4 -out vec2 uv_interp; +varying vec2 uv_interp; void main() { + gl_Position = vec4(vertex_attrib, 0.0, 1.0); - gl_Position = vertex_attrib; uv_interp = uv_in; -#ifdef V_FLIP - uv_interp.y = 1.0 - uv_interp.y; -#endif } /* clang-format off */ [fragment] -#if !defined(GLES_OVER_GL) -precision mediump float; + +// texture2DLodEXT and textureCubeLodEXT are fragment shader specific. +// Do not copy these defines in the vertex section. +#ifndef USE_GLES_OVER_GL +#ifdef GL_EXT_shader_texture_lod +#extension GL_EXT_shader_texture_lod : enable +#define texture2DLod(img, coord, lod) texture2DLodEXT(img, coord, lod) +#define textureCubeLod(img, coord, lod) textureCubeLodEXT(img, coord, lod) #endif +#endif // !USE_GLES_OVER_GL -in vec2 uv_interp; -/* clang-format on */ +#ifdef GL_ARB_shader_texture_lod +#extension GL_ARB_shader_texture_lod : enable +#endif -uniform highp sampler2D source; //texunit:0 +#if !defined(GL_EXT_shader_texture_lod) && !defined(GL_ARB_shader_texture_lod) +#define texture2DLod(img, coord, lod) texture2D(img, coord, lod) +#define textureCubeLod(img, coord, lod) textureCube(img, coord, lod) +#endif -uniform float exposure; -uniform float white; +// Allows the use of bitshift operators for bicubic upscale +#ifdef GL_EXT_gpu_shader4 +#extension GL_EXT_gpu_shader4 : enable +#endif -#ifdef USE_AUTO_EXPOSURE +#ifdef USE_GLES_OVER_GL +#define lowp +#define mediump +#define highp +#else +#if defined(USE_HIGHP_PRECISION) +precision highp float; +precision highp int; +#else +precision mediump float; +precision mediump int; +#endif +#endif -uniform highp sampler2D source_auto_exposure; //texunit:1 -uniform highp float auto_exposure_grey; +#include "stdlib.glsl" -#endif +varying vec2 uv_interp; +/* clang-format on */ -#if defined(USE_GLOW_LEVEL1) || defined(USE_GLOW_LEVEL2) || defined(USE_GLOW_LEVEL3) || defined(USE_GLOW_LEVEL4) || defined(USE_GLOW_LEVEL5) || defined(USE_GLOW_LEVEL6) || defined(USE_GLOW_LEVEL7) +uniform highp sampler2D source; //texunit:0 -uniform highp sampler2D source_glow; //texunit:2 +#if defined(USE_GLOW_LEVEL1) || defined(USE_GLOW_LEVEL2) || defined(USE_GLOW_LEVEL3) || defined(USE_GLOW_LEVEL4) || defined(USE_GLOW_LEVEL5) || defined(USE_GLOW_LEVEL6) || defined(USE_GLOW_LEVEL7) +#define USING_GLOW // only use glow when at least one glow level is selected + +#ifdef USE_MULTI_TEXTURE_GLOW +uniform highp sampler2D source_glow1; //texunit:1 +uniform highp sampler2D source_glow2; //texunit:2 +uniform highp sampler2D source_glow3; //texunit:3 +uniform highp sampler2D source_glow4; //texunit:4 +uniform highp sampler2D source_glow5; //texunit:5 +uniform highp sampler2D source_glow6; //texunit:6 +uniform highp sampler2D source_glow7; //texunit:7 +#else +uniform highp sampler2D source_glow; //texunit:1 +#endif uniform highp float glow_intensity; - #endif #ifdef USE_BCS - uniform vec3 bcs; - #endif #ifdef USE_COLOR_CORRECTION - -uniform sampler2D color_correction; //texunit:3 - +uniform sampler2D color_correction; //texunit:2 #endif -layout(location = 0) out vec4 frag_color; - +#ifdef GL_EXT_gpu_shader4 #ifdef USE_GLOW_FILTER_BICUBIC - // w0, w1, w2, and w3 are the four cubic B-spline basis functions float w0(float a) { return (1.0 / 6.0) * (a * (a * (-a + 3.0) - 3.0) + 1.0); @@ -101,8 +138,10 @@ uniform ivec2 glow_texture_size; vec4 texture2D_bicubic(sampler2D tex, vec2 uv, int p_lod) { float lod = float(p_lod); vec2 tex_size = vec2(glow_texture_size >> p_lod); - vec2 pixel_size = 1.0 / tex_size; - uv = uv * tex_size + 0.5; + vec2 pixel_size = vec2(1.0) / tex_size; + + uv = uv * tex_size + vec2(0.5); + vec2 iuv = floor(uv); vec2 fuv = fract(uv); @@ -113,73 +152,97 @@ vec4 texture2D_bicubic(sampler2D tex, vec2 uv, int p_lod) { float h0y = h0(fuv.y); float h1y = h1(fuv.y); - vec2 p0 = (vec2(iuv.x + h0x, iuv.y + h0y) - 0.5) * pixel_size; - vec2 p1 = (vec2(iuv.x + h1x, iuv.y + h0y) - 0.5) * pixel_size; - vec2 p2 = (vec2(iuv.x + h0x, iuv.y + h1y) - 0.5) * pixel_size; - vec2 p3 = (vec2(iuv.x + h1x, iuv.y + h1y) - 0.5) * pixel_size; + vec2 p0 = (vec2(iuv.x + h0x, iuv.y + h0y) - vec2(0.5)) * pixel_size; + vec2 p1 = (vec2(iuv.x + h1x, iuv.y + h0y) - vec2(0.5)) * pixel_size; + vec2 p2 = (vec2(iuv.x + h0x, iuv.y + h1y) - vec2(0.5)) * pixel_size; + vec2 p3 = (vec2(iuv.x + h1x, iuv.y + h1y) - vec2(0.5)) * pixel_size; - return (g0(fuv.y) * (g0x * textureLod(tex, p0, lod) + g1x * textureLod(tex, p1, lod))) + - (g1(fuv.y) * (g0x * textureLod(tex, p2, lod) + g1x * textureLod(tex, p3, lod))); + return (g0(fuv.y) * (g0x * texture2DLod(tex, p0, lod) + g1x * texture2DLod(tex, p1, lod))) + + (g1(fuv.y) * (g0x * texture2DLod(tex, p2, lod) + g1x * texture2DLod(tex, p3, lod))); } #define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) texture2D_bicubic(m_tex, m_uv, m_lod) +#else //!USE_GLOW_FILTER_BICUBIC +#define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) texture2DLod(m_tex, m_uv, float(m_lod)) +#endif //USE_GLOW_FILTER_BICUBIC -#else +#else //!GL_EXT_gpu_shader4 +#define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) texture2DLod(m_tex, m_uv, float(m_lod)) +#endif //GL_EXT_gpu_shader4 -#define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) textureLod(m_tex, m_uv, float(m_lod)) +vec3 apply_glow(vec3 color, vec3 glow) { // apply glow using the selected blending mode +#ifdef USE_GLOW_REPLACE + color = glow; +#endif +#ifdef USE_GLOW_SCREEN + color = max((color + glow) - (color * glow), vec3(0.0)); #endif -vec3 tonemap_filmic(vec3 color, float white) { +#ifdef USE_GLOW_SOFTLIGHT + glow = glow * vec3(0.5) + vec3(0.5); - float A = 0.15; - float B = 0.50; - float C = 0.10; - float D = 0.20; - float E = 0.02; - float F = 0.30; - float W = 11.2; + color.r = (glow.r <= 0.5) ? (color.r - (1.0 - 2.0 * glow.r) * color.r * (1.0 - color.r)) : (((glow.r > 0.5) && (color.r <= 0.25)) ? (color.r + (2.0 * glow.r - 1.0) * (4.0 * color.r * (4.0 * color.r + 1.0) * (color.r - 1.0) + 7.0 * color.r)) : (color.r + (2.0 * glow.r - 1.0) * (sqrt(color.r) - color.r))); + color.g = (glow.g <= 0.5) ? (color.g - (1.0 - 2.0 * glow.g) * color.g * (1.0 - color.g)) : (((glow.g > 0.5) && (color.g <= 0.25)) ? (color.g + (2.0 * glow.g - 1.0) * (4.0 * color.g * (4.0 * color.g + 1.0) * (color.g - 1.0) + 7.0 * color.g)) : (color.g + (2.0 * glow.g - 1.0) * (sqrt(color.g) - color.g))); + color.b = (glow.b <= 0.5) ? (color.b - (1.0 - 2.0 * glow.b) * color.b * (1.0 - color.b)) : (((glow.b > 0.5) && (color.b <= 0.25)) ? (color.b + (2.0 * glow.b - 1.0) * (4.0 * color.b * (4.0 * color.b + 1.0) * (color.b - 1.0) + 7.0 * color.b)) : (color.b + (2.0 * glow.b - 1.0) * (sqrt(color.b) - color.b))); +#endif - vec3 coltn = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F; - float whitetn = ((white * (A * white + C * B) + D * E) / (white * (A * white + B) + D * F)) - E / F; +#if !defined(USE_GLOW_SCREEN) && !defined(USE_GLOW_SOFTLIGHT) && !defined(USE_GLOW_REPLACE) // no other selected -> additive + color += glow; +#endif - return coltn / whitetn; + return color; } -vec3 tonemap_aces(vec3 color) { - float a = 2.51f; - float b = 0.03f; - float c = 2.43f; - float d = 0.59f; - float e = 0.14f; - return color = clamp((color * (a * color + b)) / (color * (c * color + d) + e), vec3(0.0), vec3(1.0)); +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; } -vec3 tonemap_reindhart(vec3 color, float white) { +vec3 apply_color_correction(vec3 color, sampler2D correction_tex) { + color.r = texture2D(correction_tex, vec2(color.r, 0.0)).r; + color.g = texture2D(correction_tex, vec2(color.g, 0.0)).g; + color.b = texture2D(correction_tex, vec2(color.b, 0.0)).b; - return (color * (1.0 + (color / (white)))) / (1.0 + color); + return color; } void main() { + vec3 color = texture2DLod(source, uv_interp, 0.0).rgb; - vec4 color = textureLod(source, uv_interp, 0.0); + // Glow -#ifdef USE_AUTO_EXPOSURE - - color /= texelFetch(source_auto_exposure, ivec2(0, 0), 0).r / auto_exposure_grey; +#ifdef USING_GLOW + vec3 glow = vec3(0.0); +#ifdef USE_MULTI_TEXTURE_GLOW +#ifdef USE_GLOW_LEVEL1 + glow += GLOW_TEXTURE_SAMPLE(source_glow1, uv_interp, 0).rgb; +#ifdef USE_GLOW_LEVEL2 + glow += GLOW_TEXTURE_SAMPLE(source_glow2, uv_interp, 0).rgb; +#ifdef USE_GLOW_LEVEL3 + glow += GLOW_TEXTURE_SAMPLE(source_glow3, uv_interp, 0).rgb; +#ifdef USE_GLOW_LEVEL4 + glow += GLOW_TEXTURE_SAMPLE(source_glow4, uv_interp, 0).rgb; +#ifdef USE_GLOW_LEVEL5 + glow += GLOW_TEXTURE_SAMPLE(source_glow5, uv_interp, 0).rgb; +#ifdef USE_GLOW_LEVEL6 + glow += GLOW_TEXTURE_SAMPLE(source_glow6, uv_interp, 0).rgb; +#ifdef USE_GLOW_LEVEL7 + glow += GLOW_TEXTURE_SAMPLE(source_glow7, uv_interp, 0).rgb; +#endif +#endif +#endif +#endif +#endif #endif - - color *= exposure; - -#if defined(USE_GLOW_LEVEL1) || defined(USE_GLOW_LEVEL2) || defined(USE_GLOW_LEVEL3) || defined(USE_GLOW_LEVEL4) || defined(USE_GLOW_LEVEL5) || defined(USE_GLOW_LEVEL6) || defined(USE_GLOW_LEVEL7) -#define USING_GLOW #endif -#if defined(USING_GLOW) - vec3 glow = vec3(0.0); +#else #ifdef USE_GLOW_LEVEL1 - glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 1).rgb; #endif @@ -206,100 +269,21 @@ void main() { #ifdef USE_GLOW_LEVEL7 glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 7).rgb; #endif +#endif //USE_MULTI_TEXTURE_GLOW glow *= glow_intensity; - -#endif - -#ifdef USE_REINDHART_TONEMAPPER - - color.rgb = tonemap_reindhart(color.rgb, white); - -#if defined(USING_GLOW) - glow = tonemap_reindhart(glow, white); -#endif - + color = apply_glow(color, glow); #endif -#ifdef USE_FILMIC_TONEMAPPER - - color.rgb = tonemap_filmic(color.rgb, white); - -#if defined(USING_GLOW) - glow = tonemap_filmic(glow, white); -#endif - -#endif - -#ifdef USE_ACES_TONEMAPPER - - color.rgb = tonemap_aces(color.rgb); - -#if defined(USING_GLOW) - glow = tonemap_aces(glow); -#endif - -#endif - - //regular Linear -> SRGB conversion - vec3 a = vec3(0.055); - color.rgb = mix((vec3(1.0) + a) * pow(color.rgb, vec3(1.0 / 2.4)) - a, 12.92 * color.rgb, lessThan(color.rgb, vec3(0.0031308))); - -#if defined(USING_GLOW) - glow = mix((vec3(1.0) + a) * pow(glow, vec3(1.0 / 2.4)) - a, 12.92 * glow, lessThan(glow, vec3(0.0031308))); -#endif - - //glow needs to be added in SRGB space (together with image space effects) - - color.rgb = clamp(color.rgb, 0.0, 1.0); - -#if defined(USING_GLOW) - glow = clamp(glow, 0.0, 1.0); -#endif - -#ifdef USE_GLOW_REPLACE - - color.rgb = glow; - -#endif - -#ifdef USE_GLOW_SCREEN - - color.rgb = max((color.rgb + glow) - (color.rgb * glow), vec3(0.0)); - -#endif - -#ifdef USE_GLOW_SOFTLIGHT - - { - - glow = (glow * 0.5) + 0.5; - color.r = (glow.r <= 0.5) ? (color.r - (1.0 - 2.0 * glow.r) * color.r * (1.0 - color.r)) : (((glow.r > 0.5) && (color.r <= 0.25)) ? (color.r + (2.0 * glow.r - 1.0) * (4.0 * color.r * (4.0 * color.r + 1.0) * (color.r - 1.0) + 7.0 * color.r)) : (color.r + (2.0 * glow.r - 1.0) * (sqrt(color.r) - color.r))); - color.g = (glow.g <= 0.5) ? (color.g - (1.0 - 2.0 * glow.g) * color.g * (1.0 - color.g)) : (((glow.g > 0.5) && (color.g <= 0.25)) ? (color.g + (2.0 * glow.g - 1.0) * (4.0 * color.g * (4.0 * color.g + 1.0) * (color.g - 1.0) + 7.0 * color.g)) : (color.g + (2.0 * glow.g - 1.0) * (sqrt(color.g) - color.g))); - color.b = (glow.b <= 0.5) ? (color.b - (1.0 - 2.0 * glow.b) * color.b * (1.0 - color.b)) : (((glow.b > 0.5) && (color.b <= 0.25)) ? (color.b + (2.0 * glow.b - 1.0) * (4.0 * color.b * (4.0 * color.b + 1.0) * (color.b - 1.0) + 7.0 * color.b)) : (color.b + (2.0 * glow.b - 1.0) * (sqrt(color.b) - color.b))); - } - -#endif - -#if defined(USING_GLOW) && !defined(USE_GLOW_SCREEN) && !defined(USE_GLOW_SOFTLIGHT) && !defined(USE_GLOW_REPLACE) - //additive - color.rgb += glow; -#endif + // Additional effects #ifdef USE_BCS - - color.rgb = mix(vec3(0.0), color.rgb, bcs.x); - color.rgb = mix(vec3(0.5), color.rgb, bcs.y); - color.rgb = mix(vec3(dot(vec3(1.0), color.rgb) * 0.33333), color.rgb, bcs.z); - + color = apply_bcs(color, bcs); #endif #ifdef USE_COLOR_CORRECTION - - color.r = texture(color_correction, vec2(color.r, 0.0)).r; - color.g = texture(color_correction, vec2(color.g, 0.0)).g; - color.b = texture(color_correction, vec2(color.b, 0.0)).b; + color = apply_color_correction(color, color_correction); #endif - frag_color = vec4(color.rgb, 1.0); + gl_FragColor = vec4(color, 1.0); } |