2020-05-18 08:56:22 +00:00
|
|
|
#[compute]
|
2019-09-07 01:51:27 +00:00
|
|
|
|
|
|
|
#version 450
|
|
|
|
|
2021-04-13 20:01:43 +00:00
|
|
|
#VERSION_DEFINES
|
2019-09-07 01:51:27 +00:00
|
|
|
|
2020-04-11 17:43:12 +00:00
|
|
|
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
|
2019-09-07 01:51:27 +00:00
|
|
|
|
2020-04-11 17:43:12 +00:00
|
|
|
#define FLAG_HORIZONTAL (1 << 0)
|
|
|
|
#define FLAG_USE_BLUR_SECTION (1 << 1)
|
|
|
|
#define FLAG_USE_ORTHOGONAL_PROJECTION (1 << 2)
|
|
|
|
#define FLAG_DOF_NEAR_FIRST_TAP (1 << 3)
|
|
|
|
#define FLAG_GLOW_FIRST_PASS (1 << 4)
|
|
|
|
#define FLAG_FLIP_Y (1 << 5)
|
|
|
|
#define FLAG_FORCE_LUMINANCE (1 << 6)
|
|
|
|
#define FLAG_COPY_ALL_SOURCE (1 << 7)
|
2022-12-13 07:56:47 +00:00
|
|
|
#define FLAG_ALPHA_TO_ONE (1 << 8)
|
2020-04-11 17:43:12 +00:00
|
|
|
|
2022-02-11 17:40:24 +00:00
|
|
|
layout(push_constant, std430) uniform Params {
|
2020-04-11 17:43:12 +00:00
|
|
|
ivec4 section;
|
|
|
|
ivec2 target;
|
|
|
|
uint flags;
|
|
|
|
uint pad;
|
|
|
|
// Glow.
|
|
|
|
float glow_strength;
|
|
|
|
float glow_bloom;
|
|
|
|
float glow_hdr_threshold;
|
|
|
|
float glow_hdr_scale;
|
|
|
|
|
|
|
|
float glow_exposure;
|
|
|
|
float glow_white;
|
|
|
|
float glow_luminance_cap;
|
2022-07-31 23:20:24 +00:00
|
|
|
float glow_auto_exposure_scale;
|
2020-04-11 17:43:12 +00:00
|
|
|
// DOF.
|
|
|
|
float camera_z_far;
|
|
|
|
float camera_z_near;
|
2020-04-14 03:05:21 +00:00
|
|
|
uint pad2[2];
|
2020-10-28 18:34:27 +00:00
|
|
|
|
|
|
|
vec4 set_color;
|
2020-04-11 17:43:12 +00:00
|
|
|
}
|
|
|
|
params;
|
2019-09-07 01:51:27 +00:00
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
#ifdef MODE_CUBEMAP_ARRAY_TO_PANORAMA
|
|
|
|
layout(set = 0, binding = 0) uniform samplerCubeArray source_color;
|
|
|
|
#elif defined(MODE_CUBEMAP_TO_PANORAMA)
|
|
|
|
layout(set = 0, binding = 0) uniform samplerCube source_color;
|
2020-10-28 18:34:27 +00:00
|
|
|
#elif !defined(MODE_SET_COLOR)
|
2020-04-11 17:43:12 +00:00
|
|
|
layout(set = 0, binding = 0) uniform sampler2D source_color;
|
2020-05-01 12:34:23 +00:00
|
|
|
#endif
|
2019-09-07 01:51:27 +00:00
|
|
|
|
2020-04-11 17:43:12 +00:00
|
|
|
#ifdef GLOW_USE_AUTO_EXPOSURE
|
|
|
|
layout(set = 1, binding = 0) uniform sampler2D source_auto_exposure;
|
|
|
|
#endif
|
2019-09-07 01:51:27 +00:00
|
|
|
|
2020-04-11 17:43:12 +00:00
|
|
|
#if defined(MODE_LINEARIZE_DEPTH_COPY) || defined(MODE_SIMPLE_COPY_DEPTH)
|
|
|
|
layout(r32f, set = 3, binding = 0) uniform restrict writeonly image2D dest_buffer;
|
|
|
|
#elif defined(DST_IMAGE_8BIT)
|
|
|
|
layout(rgba8, set = 3, binding = 0) uniform restrict writeonly image2D dest_buffer;
|
|
|
|
#else
|
2023-08-03 12:10:01 +00:00
|
|
|
layout(rgba16f, set = 3, binding = 0) uniform restrict writeonly image2D dest_buffer;
|
2020-04-11 17:43:12 +00:00
|
|
|
#endif
|
2019-09-07 01:51:27 +00:00
|
|
|
|
2022-02-05 23:03:39 +00:00
|
|
|
#ifdef MODE_GAUSSIAN_BLUR
|
2020-09-19 22:02:32 +00:00
|
|
|
shared vec4 local_cache[256];
|
|
|
|
shared vec4 temp_cache[128];
|
|
|
|
#endif
|
|
|
|
|
2020-04-11 17:43:12 +00:00
|
|
|
void main() {
|
|
|
|
// Pixel being shaded
|
|
|
|
ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
|
2020-09-19 22:02:32 +00:00
|
|
|
|
2022-02-05 23:03:39 +00:00
|
|
|
#ifndef MODE_GAUSSIAN_BLUR // Gaussian blur needs the extra threads
|
2020-05-01 12:34:23 +00:00
|
|
|
if (any(greaterThanEqual(pos, params.section.zw))) { //too large, do nothing
|
2020-04-11 17:43:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-09-19 22:02:32 +00:00
|
|
|
#endif
|
2019-09-07 01:51:27 +00:00
|
|
|
|
2020-04-11 17:43:12 +00:00
|
|
|
#ifdef MODE_MIPMAP
|
2019-09-07 01:51:27 +00:00
|
|
|
|
2020-04-11 17:43:12 +00:00
|
|
|
ivec2 base_pos = (pos + params.section.xy) << 1;
|
|
|
|
vec4 color = texelFetch(source_color, base_pos, 0);
|
|
|
|
color += texelFetch(source_color, base_pos + ivec2(0, 1), 0);
|
|
|
|
color += texelFetch(source_color, base_pos + ivec2(1, 0), 0);
|
|
|
|
color += texelFetch(source_color, base_pos + ivec2(1, 1), 0);
|
|
|
|
color /= 4.0;
|
2021-08-03 07:07:32 +00:00
|
|
|
color = mix(color, vec4(100.0, 100.0, 100.0, 1.0), isinf(color));
|
|
|
|
color = mix(color, vec4(100.0, 100.0, 100.0, 1.0), isnan(color));
|
2019-09-07 01:51:27 +00:00
|
|
|
|
2020-04-11 17:43:12 +00:00
|
|
|
imageStore(dest_buffer, pos + params.target, color);
|
|
|
|
#endif
|
2019-09-07 01:51:27 +00:00
|
|
|
|
2020-04-11 17:43:12 +00:00
|
|
|
#ifdef MODE_GAUSSIAN_BLUR
|
|
|
|
|
2020-09-19 22:02:32 +00:00
|
|
|
// First pass copy texture into 16x16 local memory for every 8x8 thread block
|
2022-12-12 22:10:06 +00:00
|
|
|
vec2 quad_center_uv = clamp(vec2(params.section.xy + gl_GlobalInvocationID.xy + gl_LocalInvocationID.xy - 3.5) / params.section.zw, vec2(0.5 / params.section.zw), vec2(1.0 - 1.5 / params.section.zw));
|
2020-09-19 22:02:32 +00:00
|
|
|
uint dest_index = gl_LocalInvocationID.x * 2 + gl_LocalInvocationID.y * 2 * 16;
|
2019-09-07 01:51:27 +00:00
|
|
|
|
2022-12-13 07:56:47 +00:00
|
|
|
local_cache[dest_index] = textureLod(source_color, quad_center_uv, 0);
|
|
|
|
local_cache[dest_index + 1] = textureLod(source_color, quad_center_uv + vec2(1.0 / params.section.z, 0.0), 0);
|
|
|
|
local_cache[dest_index + 16] = textureLod(source_color, quad_center_uv + vec2(0.0, 1.0 / params.section.w), 0);
|
|
|
|
local_cache[dest_index + 16 + 1] = textureLod(source_color, quad_center_uv + vec2(1.0 / params.section.zw), 0);
|
|
|
|
|
2022-02-05 23:03:39 +00:00
|
|
|
#ifdef MODE_GLOW
|
2021-10-30 04:41:57 +00:00
|
|
|
if (bool(params.flags & FLAG_GLOW_FIRST_PASS)) {
|
|
|
|
// Tonemap initial samples to reduce weight of fireflies: https://graphicrants.blogspot.com/2013/12/tone-mapping.html
|
2023-09-26 07:17:11 +00:00
|
|
|
vec3 tonemap_col = vec3(0.299, 0.587, 0.114) / max(params.glow_luminance_cap, 6.0);
|
|
|
|
local_cache[dest_index] /= 1.0 + dot(local_cache[dest_index].rgb, tonemap_col);
|
|
|
|
local_cache[dest_index + 1] /= 1.0 + dot(local_cache[dest_index + 1].rgb, tonemap_col);
|
|
|
|
local_cache[dest_index + 16] /= 1.0 + dot(local_cache[dest_index + 16].rgb, tonemap_col);
|
|
|
|
local_cache[dest_index + 16 + 1] /= 1.0 + dot(local_cache[dest_index + 16 + 1].rgb, tonemap_col);
|
2021-10-30 04:41:57 +00:00
|
|
|
}
|
2023-09-26 07:17:11 +00:00
|
|
|
const float kernel[5] = { 0.2024, 0.1790, 0.1240, 0.0672, 0.0285 };
|
2022-02-05 23:03:39 +00:00
|
|
|
#else
|
|
|
|
// Simpler blur uses SIGMA2 for the gaussian kernel for a stronger effect.
|
|
|
|
const float kernel[4] = { 0.214607, 0.189879, 0.131514, 0.071303 };
|
|
|
|
#endif
|
2020-09-19 22:02:32 +00:00
|
|
|
memoryBarrierShared();
|
|
|
|
barrier();
|
|
|
|
|
|
|
|
// Horizontal pass. Needs to copy into 8x16 chunk of local memory so vertical pass has full resolution
|
|
|
|
uint read_index = gl_LocalInvocationID.x + gl_LocalInvocationID.y * 32 + 4;
|
|
|
|
vec4 color_top = vec4(0.0);
|
2022-02-05 23:03:39 +00:00
|
|
|
color_top += local_cache[read_index] * kernel[0];
|
|
|
|
color_top += local_cache[read_index + 1] * kernel[1];
|
|
|
|
color_top += local_cache[read_index + 2] * kernel[2];
|
|
|
|
color_top += local_cache[read_index + 3] * kernel[3];
|
|
|
|
color_top += local_cache[read_index - 1] * kernel[1];
|
|
|
|
color_top += local_cache[read_index - 2] * kernel[2];
|
|
|
|
color_top += local_cache[read_index - 3] * kernel[3];
|
2023-09-26 07:17:11 +00:00
|
|
|
#ifdef MODE_GLOW
|
|
|
|
color_top += local_cache[read_index + 4] * kernel[4];
|
|
|
|
color_top += local_cache[read_index - 4] * kernel[4];
|
|
|
|
#endif // MODE_GLOW
|
2020-09-19 22:02:32 +00:00
|
|
|
|
|
|
|
vec4 color_bottom = vec4(0.0);
|
2022-02-05 23:03:39 +00:00
|
|
|
color_bottom += local_cache[read_index + 16] * kernel[0];
|
|
|
|
color_bottom += local_cache[read_index + 1 + 16] * kernel[1];
|
|
|
|
color_bottom += local_cache[read_index + 2 + 16] * kernel[2];
|
|
|
|
color_bottom += local_cache[read_index + 3 + 16] * kernel[3];
|
|
|
|
color_bottom += local_cache[read_index - 1 + 16] * kernel[1];
|
|
|
|
color_bottom += local_cache[read_index - 2 + 16] * kernel[2];
|
|
|
|
color_bottom += local_cache[read_index - 3 + 16] * kernel[3];
|
2023-09-26 07:17:11 +00:00
|
|
|
#ifdef MODE_GLOW
|
|
|
|
color_bottom += local_cache[read_index + 4 + 16] * kernel[4];
|
|
|
|
color_bottom += local_cache[read_index - 4 + 16] * kernel[4];
|
|
|
|
#endif // MODE_GLOW
|
2020-09-19 22:02:32 +00:00
|
|
|
|
|
|
|
// rotate samples to take advantage of cache coherency
|
|
|
|
uint write_index = gl_LocalInvocationID.y * 2 + gl_LocalInvocationID.x * 16;
|
|
|
|
|
|
|
|
temp_cache[write_index] = color_top;
|
|
|
|
temp_cache[write_index + 1] = color_bottom;
|
|
|
|
|
|
|
|
memoryBarrierShared();
|
|
|
|
barrier();
|
|
|
|
|
2022-02-05 23:03:39 +00:00
|
|
|
// If destination outside of texture, can stop doing work now
|
|
|
|
if (any(greaterThanEqual(pos, params.section.zw))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-19 22:02:32 +00:00
|
|
|
// Vertical pass
|
|
|
|
uint index = gl_LocalInvocationID.y + gl_LocalInvocationID.x * 16 + 4;
|
2020-04-11 17:43:12 +00:00
|
|
|
vec4 color = vec4(0.0);
|
2019-09-07 01:51:27 +00:00
|
|
|
|
2022-02-05 23:03:39 +00:00
|
|
|
color += temp_cache[index] * kernel[0];
|
|
|
|
color += temp_cache[index + 1] * kernel[1];
|
|
|
|
color += temp_cache[index + 2] * kernel[2];
|
|
|
|
color += temp_cache[index + 3] * kernel[3];
|
|
|
|
color += temp_cache[index - 1] * kernel[1];
|
|
|
|
color += temp_cache[index - 2] * kernel[2];
|
|
|
|
color += temp_cache[index - 3] * kernel[3];
|
2023-09-26 07:17:11 +00:00
|
|
|
#ifdef MODE_GLOW
|
|
|
|
color += temp_cache[index + 4] * kernel[4];
|
|
|
|
color += temp_cache[index - 4] * kernel[4];
|
|
|
|
#endif // MODE_GLOW
|
2019-09-07 01:51:27 +00:00
|
|
|
|
2022-02-05 23:03:39 +00:00
|
|
|
#ifdef MODE_GLOW
|
2021-10-30 04:41:57 +00:00
|
|
|
if (bool(params.flags & FLAG_GLOW_FIRST_PASS)) {
|
|
|
|
// Undo tonemap to restore range: https://graphicrants.blogspot.com/2013/12/tone-mapping.html
|
2023-09-26 07:17:11 +00:00
|
|
|
color /= 1.0 - dot(color.rgb, vec3(0.299, 0.587, 0.114) / max(params.glow_luminance_cap, 6.0));
|
2021-10-30 04:41:57 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 22:02:32 +00:00
|
|
|
color *= params.glow_strength;
|
2020-04-11 17:43:12 +00:00
|
|
|
|
|
|
|
if (bool(params.flags & FLAG_GLOW_FIRST_PASS)) {
|
|
|
|
#ifdef GLOW_USE_AUTO_EXPOSURE
|
|
|
|
|
2022-07-31 23:20:24 +00:00
|
|
|
color /= texelFetch(source_auto_exposure, ivec2(0, 0), 0).r / params.glow_auto_exposure_scale;
|
2020-04-11 17:43:12 +00:00
|
|
|
#endif
|
|
|
|
color *= params.glow_exposure;
|
|
|
|
|
2022-04-29 07:10:54 +00:00
|
|
|
float luminance = max(color.r, max(color.g, color.b));
|
2020-04-11 17:43:12 +00:00
|
|
|
float feedback = max(smoothstep(params.glow_hdr_threshold, params.glow_hdr_threshold + params.glow_hdr_scale, luminance), params.glow_bloom);
|
|
|
|
|
|
|
|
color = min(color * feedback, vec4(params.glow_luminance_cap));
|
|
|
|
}
|
2022-11-10 20:07:54 +00:00
|
|
|
#endif // MODE_GLOW
|
2020-04-11 17:43:12 +00:00
|
|
|
imageStore(dest_buffer, pos + params.target, color);
|
|
|
|
|
2022-11-10 20:07:54 +00:00
|
|
|
#endif // MODE_GAUSSIAN_BLUR
|
2020-04-11 17:43:12 +00:00
|
|
|
|
|
|
|
#ifdef MODE_SIMPLE_COPY
|
|
|
|
|
|
|
|
vec4 color;
|
|
|
|
if (bool(params.flags & FLAG_COPY_ALL_SOURCE)) {
|
|
|
|
vec2 uv = vec2(pos) / vec2(params.section.zw);
|
|
|
|
if (bool(params.flags & FLAG_FLIP_Y)) {
|
|
|
|
uv.y = 1.0 - uv.y;
|
|
|
|
}
|
|
|
|
color = textureLod(source_color, uv, 0.0);
|
|
|
|
|
2019-09-07 01:51:27 +00:00
|
|
|
} else {
|
2020-04-11 17:43:12 +00:00
|
|
|
color = texelFetch(source_color, pos + params.section.xy, 0);
|
|
|
|
|
|
|
|
if (bool(params.flags & FLAG_FLIP_Y)) {
|
|
|
|
pos.y = params.section.w - pos.y - 1;
|
|
|
|
}
|
2020-10-28 18:34:27 +00:00
|
|
|
}
|
2020-04-11 17:43:12 +00:00
|
|
|
|
2020-10-28 18:34:27 +00:00
|
|
|
if (bool(params.flags & FLAG_FORCE_LUMINANCE)) {
|
|
|
|
color.rgb = vec3(max(max(color.r, color.g), color.b));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bool(params.flags & FLAG_ALPHA_TO_ONE)) {
|
|
|
|
color.a = 1.0;
|
2019-09-07 01:51:27 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 18:34:27 +00:00
|
|
|
imageStore(dest_buffer, pos + params.target, color);
|
|
|
|
|
2022-11-10 20:07:54 +00:00
|
|
|
#endif // MODE_SIMPLE_COPY
|
2020-04-11 17:43:12 +00:00
|
|
|
|
|
|
|
#ifdef MODE_SIMPLE_COPY_DEPTH
|
|
|
|
|
|
|
|
vec4 color = texelFetch(source_color, pos + params.section.xy, 0);
|
|
|
|
|
|
|
|
if (bool(params.flags & FLAG_FLIP_Y)) {
|
|
|
|
pos.y = params.section.w - pos.y - 1;
|
|
|
|
}
|
2019-09-07 01:51:27 +00:00
|
|
|
|
2020-04-11 17:43:12 +00:00
|
|
|
imageStore(dest_buffer, pos + params.target, vec4(color.r));
|
|
|
|
|
2022-11-10 20:07:54 +00:00
|
|
|
#endif // MODE_SIMPLE_COPY_DEPTH
|
2020-04-11 17:43:12 +00:00
|
|
|
|
|
|
|
#ifdef MODE_LINEARIZE_DEPTH_COPY
|
|
|
|
|
|
|
|
float depth = texelFetch(source_color, pos + params.section.xy, 0).r;
|
|
|
|
depth = depth * 2.0 - 1.0;
|
|
|
|
depth = 2.0 * params.camera_z_near * params.camera_z_far / (params.camera_z_far + params.camera_z_near - depth * (params.camera_z_far - params.camera_z_near));
|
|
|
|
vec4 color = vec4(depth / params.camera_z_far);
|
|
|
|
|
|
|
|
if (bool(params.flags & FLAG_FLIP_Y)) {
|
|
|
|
pos.y = params.section.w - pos.y - 1;
|
|
|
|
}
|
2019-09-07 01:51:27 +00:00
|
|
|
|
2020-04-11 17:43:12 +00:00
|
|
|
imageStore(dest_buffer, pos + params.target, color);
|
2022-11-10 20:07:54 +00:00
|
|
|
#endif // MODE_LINEARIZE_DEPTH_COPY
|
2020-05-01 12:34:23 +00:00
|
|
|
|
|
|
|
#if defined(MODE_CUBEMAP_TO_PANORAMA) || defined(MODE_CUBEMAP_ARRAY_TO_PANORAMA)
|
|
|
|
|
|
|
|
const float PI = 3.14159265359;
|
|
|
|
vec2 uv = vec2(pos) / vec2(params.section.zw);
|
2021-12-23 23:47:42 +00:00
|
|
|
if (bool(params.flags & FLAG_FLIP_Y)) {
|
|
|
|
uv.y = 1.0 - uv.y;
|
|
|
|
}
|
2020-05-01 12:34:23 +00:00
|
|
|
float phi = uv.x * 2.0 * PI;
|
|
|
|
float theta = uv.y * PI;
|
|
|
|
|
|
|
|
vec3 normal;
|
|
|
|
normal.x = sin(phi) * sin(theta) * -1.0;
|
|
|
|
normal.y = cos(theta);
|
|
|
|
normal.z = cos(phi) * sin(theta) * -1.0;
|
|
|
|
|
|
|
|
#ifdef MODE_CUBEMAP_TO_PANORAMA
|
|
|
|
vec4 color = textureLod(source_color, normal, params.camera_z_far); //the biggest the lod the least the acne
|
|
|
|
#else
|
|
|
|
vec4 color = textureLod(source_color, vec4(normal, params.camera_z_far), 0.0); //the biggest the lod the least the acne
|
|
|
|
#endif
|
|
|
|
imageStore(dest_buffer, pos + params.target, color);
|
2022-11-10 20:07:54 +00:00
|
|
|
#endif // defined(MODE_CUBEMAP_TO_PANORAMA) || defined(MODE_CUBEMAP_ARRAY_TO_PANORAMA)
|
2020-10-28 18:34:27 +00:00
|
|
|
|
|
|
|
#ifdef MODE_SET_COLOR
|
|
|
|
imageStore(dest_buffer, pos + params.target, params.set_color);
|
|
|
|
#endif
|
2019-09-07 01:51:27 +00:00
|
|
|
}
|