diff options
Diffstat (limited to 'servers/visual/rasterizer_rd/shaders')
-rw-r--r-- | servers/visual/rasterizer_rd/shaders/SCsub | 3 | ||||
-rw-r--r-- | servers/visual/rasterizer_rd/shaders/blur.glsl | 32 | ||||
-rw-r--r-- | servers/visual/rasterizer_rd/shaders/luminance_reduce.glsl | 76 | ||||
-rw-r--r-- | servers/visual/rasterizer_rd/shaders/tonemap.glsl | 24 |
4 files changed, 116 insertions, 19 deletions
diff --git a/servers/visual/rasterizer_rd/shaders/SCsub b/servers/visual/rasterizer_rd/shaders/SCsub index ff83b46a9b..7d5e88b0ab 100644 --- a/servers/visual/rasterizer_rd/shaders/SCsub +++ b/servers/visual/rasterizer_rd/shaders/SCsub @@ -14,5 +14,6 @@ if 'RD_GLSL' in env['BUILDERS']: env.RD_GLSL('giprobe.glsl'); env.RD_GLSL('giprobe_debug.glsl'); env.RD_GLSL('giprobe_sdf.glsl'); - + env.RD_GLSL('luminance_reduce.glsl'); + diff --git a/servers/visual/rasterizer_rd/shaders/blur.glsl b/servers/visual/rasterizer_rd/shaders/blur.glsl index 3f67638a45..ae4cedec67 100644 --- a/servers/visual/rasterizer_rd/shaders/blur.glsl +++ b/servers/visual/rasterizer_rd/shaders/blur.glsl @@ -125,31 +125,43 @@ void main() { //Glow uses larger sigma 1 for a more rounded blur effect +#define GLOW_ADD( m_ofs, m_mult )\ + {\ + vec2 ofs = uv_interp + m_ofs * pix_size;\ + vec4 c = texture(source_color, ofs ) * m_mult; \ + if (any(lessThan(ofs,vec2(0.0))) || any(greaterThan(ofs,vec2(1.0)))) {\ + c*=0.0;\ + }\ + color+=c;\ + } + if (bool(blur.flags & FLAG_HORIZONTAL)) { vec2 pix_size = blur.pixel_size; pix_size *= 0.5; //reading from larger buffer, so use more samples vec4 color = texture(source_color, uv_interp + vec2(0.0, 0.0) * pix_size) * 0.174938; - color += texture(source_color, uv_interp + vec2(1.0, 0.0) * pix_size) * 0.165569; - color += texture(source_color, uv_interp + vec2(2.0, 0.0) * pix_size) * 0.140367; - color += texture(source_color, uv_interp + vec2(3.0, 0.0) * pix_size) * 0.106595; - color += texture(source_color, uv_interp + vec2(-1.0, 0.0) * pix_size) * 0.165569; - color += texture(source_color, uv_interp + vec2(-2.0, 0.0) * pix_size) * 0.140367; - color += texture(source_color, uv_interp + vec2(-3.0, 0.0) * pix_size) * 0.106595; + GLOW_ADD(vec2(1.0, 0.0),0.165569); + GLOW_ADD(vec2(2.0, 0.0),0.140367); + GLOW_ADD(vec2(3.0, 0.0), 0.106595); + GLOW_ADD(vec2(-1.0, 0.0),0.165569); + GLOW_ADD(vec2(-2.0, 0.0),0.140367); + GLOW_ADD(vec2(-3.0, 0.0),0.106595); color *= blur.glow_strength; frag_color = color; } else { vec2 pix_size = blur.pixel_size; vec4 color = texture(source_color, uv_interp + vec2(0.0, 0.0) * pix_size) * 0.288713; - color += texture(source_color, uv_interp + vec2(0.0, 1.0) * pix_size) * 0.233062; - color += texture(source_color, uv_interp + vec2(0.0, 2.0) * pix_size) * 0.122581; - color += texture(source_color, uv_interp + vec2(0.0, -1.0) * pix_size) * 0.233062; - color += texture(source_color, uv_interp + vec2(0.0, -2.0) * pix_size) * 0.122581; + GLOW_ADD(vec2(0.0, 1.0),0.233062); + GLOW_ADD(vec2(0.0, 2.0),0.122581); + GLOW_ADD(vec2(0.0, -1.0),0.233062); + GLOW_ADD(vec2(0.0, -2.0),0.122581); color *= blur.glow_strength; frag_color = color; } +#undef GLOW_ADD + if (bool(blur.flags & FLAG_GLOW_FIRST_PASS)) { #ifdef GLOW_USE_AUTO_EXPOSURE diff --git a/servers/visual/rasterizer_rd/shaders/luminance_reduce.glsl b/servers/visual/rasterizer_rd/shaders/luminance_reduce.glsl new file mode 100644 index 0000000000..157ea91a36 --- /dev/null +++ b/servers/visual/rasterizer_rd/shaders/luminance_reduce.glsl @@ -0,0 +1,76 @@ +/* clang-format off */ +[compute] +/* clang-format on */ +#version 450 + +VERSION_DEFINES + +#define BLOCK_SIZE 8 + +layout(local_size_x = BLOCK_SIZE, local_size_y = BLOCK_SIZE, local_size_z = 1) in; + +shared float tmp_data[BLOCK_SIZE*BLOCK_SIZE]; + + +#ifdef READ_TEXTURE + +//use for main texture +layout(set = 0, binding = 1) uniform texture2D source_texture; +layout(set = 0, binding = 2) uniform sampler source_sampler; + +#else + +//use for intermediate textures +layout(r32f, set = 0, binding = 1) uniform restrict readonly image2D source_luminance; + +#endif + +layout(r32f, set = 0, binding = 3) uniform restrict writeonly image2D dest_luminance; + +layout(push_constant, binding = 0, std430) uniform Params { + ivec2 source_size; +} params; + +void main() { + + + uint t = gl_LocalInvocationID.y * BLOCK_SIZE + gl_LocalInvocationID.x; + ivec2 pos = ivec2(gl_GlobalInvocationID.xy); + + if (any(lessThan(pos,params.source_size))) { + +#ifdef READ_TEXTURE + vec3 v = texelFetch(sampler2D(source_texture,source_sampler),pos).rgb; + avg += max(v.r,max(v.g,v.b)); + tmp_data[t] = 0.0; +#else + tmp_data[t] = imageLoad(source_luminance, pos); +#endif + } else { + tmp_data[t] = 0.0; + } + + groupMemoryBarrier(); + barrier(); + + uint size = (BLOCK_SIZE * BLOCK_SIZE)>>1; + + do { + if (t<size) { + tmp_data[t]+=tmp_data[t+size]; + } + groupMemoryBarrier(); + barrier(); + + size>>=1; + + } while(size>1); + + if (t==0) { + //compute rect size + ivec2 rect_size = max(params.source_size - pos,ivec2(BLOCK_SIZE)); + float avg = tmp_data[0] / float(rect_size.x*rect_size.y); + pos/=ivec2(BLOCK_SIZE); + imageStore(dest_luminance, pos, vec4(avg)); + } +} diff --git a/servers/visual/rasterizer_rd/shaders/tonemap.glsl b/servers/visual/rasterizer_rd/shaders/tonemap.glsl index 477e0d1055..ee6ced0a55 100644 --- a/servers/visual/rasterizer_rd/shaders/tonemap.glsl +++ b/servers/visual/rasterizer_rd/shaders/tonemap.glsl @@ -187,31 +187,31 @@ vec3 gather_glow(sampler2D tex, vec2 uv) { // sample all selected glow levels vec3 glow = vec3(0.0f); if (bool(params.glow_level_flags & (1 << 0))) { - glow += GLOW_TEXTURE_SAMPLE(tex, uv, 1).rgb; + glow += GLOW_TEXTURE_SAMPLE(tex, uv, 0).rgb; } if (bool(params.glow_level_flags & (1 << 1))) { - glow += GLOW_TEXTURE_SAMPLE(tex, uv, 2).rgb; + glow += GLOW_TEXTURE_SAMPLE(tex, uv, 1).rgb; } if (bool(params.glow_level_flags & (1 << 2))) { - glow += GLOW_TEXTURE_SAMPLE(tex, uv, 3).rgb; + glow += GLOW_TEXTURE_SAMPLE(tex, uv, 2).rgb; } if (bool(params.glow_level_flags & (1 << 3))) { - glow += GLOW_TEXTURE_SAMPLE(tex, uv, 4).rgb; + glow += GLOW_TEXTURE_SAMPLE(tex, uv, 3).rgb; } if (bool(params.glow_level_flags & (1 << 4))) { - glow += GLOW_TEXTURE_SAMPLE(tex, uv, 5).rgb; + glow += GLOW_TEXTURE_SAMPLE(tex, uv, 4).rgb; } if (bool(params.glow_level_flags & (1 << 5))) { - glow += GLOW_TEXTURE_SAMPLE(tex, uv, 6).rgb; + glow += GLOW_TEXTURE_SAMPLE(tex, uv, 5).rgb; } if (bool(params.glow_level_flags & (1 << 6))) { - glow += GLOW_TEXTURE_SAMPLE(tex, uv, 7).rgb; + glow += GLOW_TEXTURE_SAMPLE(tex, uv, 6).rgb; } return glow; @@ -221,6 +221,7 @@ vec3 gather_glow(sampler2D tex, vec2 uv) { // sample all selected glow levels #define GLOW_MODE_SCREEN 1 #define GLOW_MODE_SOFTLIGHT 2 #define GLOW_MODE_REPLACE 3 +#define GLOW_MODE_MIX 4 vec3 apply_glow(vec3 color, vec3 glow) { // apply glow using the selected blending mode if (params.glow_mode == GLOW_MODE_ADD) { @@ -266,13 +267,20 @@ void main() { // Early Tonemap & SRGB Conversion + if (params.use_glow && params.glow_mode == GLOW_MODE_MIX) { + + vec3 glow = gather_glow(source_glow, uv_interp); + color.rgb = mix(color.rgb,glow,params.glow_intensity); + + } + color = apply_tonemapping(color, params.white); color = linear_to_srgb(color); // regular linear -> SRGB conversion // Glow - if (params.use_glow) { + if (params.use_glow && params.glow_mode != GLOW_MODE_MIX) { vec3 glow = gather_glow(source_glow, uv_interp) * params.glow_intensity; |