2020-05-18 10:56:22 +02:00
|
|
|
#[compute]
|
2020-02-11 14:01:43 +01:00
|
|
|
|
2020-01-13 19:37:24 +01:00
|
|
|
#version 450
|
|
|
|
|
2021-04-13 22:01:43 +02:00
|
|
|
#VERSION_DEFINES
|
2020-01-13 19:37:24 +01:00
|
|
|
|
|
|
|
#define BLOCK_SIZE 8
|
|
|
|
|
|
|
|
layout(local_size_x = BLOCK_SIZE, local_size_y = BLOCK_SIZE, local_size_z = 1) in;
|
|
|
|
|
|
|
|
#ifdef MODE_GEN_BLUR_SIZE
|
|
|
|
layout(rgba16f, set = 0, binding = 0) uniform restrict image2D color_image;
|
|
|
|
layout(set = 1, binding = 0) uniform sampler2D source_depth;
|
|
|
|
#endif
|
|
|
|
|
2020-01-16 01:23:21 +01:00
|
|
|
#if defined(MODE_BOKEH_BOX) || defined(MODE_BOKEH_HEXAGONAL) || defined(MODE_BOKEH_CIRCULAR)
|
|
|
|
layout(set = 1, binding = 0) uniform sampler2D color_texture;
|
2020-01-13 19:37:24 +01:00
|
|
|
layout(rgba16f, set = 0, binding = 0) uniform restrict writeonly image2D bokeh_image;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MODE_COMPOSITE_BOKEH
|
|
|
|
layout(rgba16f, set = 0, binding = 0) uniform restrict image2D color_image;
|
|
|
|
layout(set = 1, binding = 0) uniform sampler2D source_bokeh;
|
|
|
|
#endif
|
|
|
|
|
2020-01-16 01:23:21 +01:00
|
|
|
// based on https://www.shadertoy.com/view/Xd3GDl
|
2020-01-13 19:37:24 +01:00
|
|
|
|
2021-07-29 09:43:17 +02:00
|
|
|
#include "bokeh_dof_inc.glsl"
|
2020-01-16 01:23:21 +01:00
|
|
|
|
|
|
|
#ifdef MODE_GEN_BLUR_SIZE
|
2020-01-13 19:37:24 +01:00
|
|
|
|
|
|
|
float get_depth_at_pos(vec2 uv) {
|
2022-08-13 00:39:05 +02:00
|
|
|
float depth = textureLod(source_depth, uv, 0.0).x * 2.0 - 1.0;
|
2020-01-13 19:37:24 +01:00
|
|
|
if (params.orthogonal) {
|
|
|
|
depth = ((depth + (params.z_far + params.z_near) / (params.z_far - params.z_near)) * (params.z_far - params.z_near)) / 2.0;
|
|
|
|
} else {
|
|
|
|
depth = 2.0 * params.z_near * params.z_far / (params.z_far + params.z_near - depth * (params.z_far - params.z_near));
|
|
|
|
}
|
|
|
|
return depth;
|
|
|
|
}
|
|
|
|
|
|
|
|
float get_blur_size(float depth) {
|
|
|
|
if (params.blur_near_active && depth < params.blur_near_begin) {
|
2020-02-11 14:01:43 +01:00
|
|
|
return -(1.0 - smoothstep(params.blur_near_end, params.blur_near_begin, depth)) * params.blur_size - DEPTH_GAP; //near blur is negative
|
2020-01-13 19:37:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (params.blur_far_active && depth > params.blur_far_begin) {
|
2020-02-11 14:01:43 +01:00
|
|
|
return smoothstep(params.blur_far_begin, params.blur_far_end, depth) * params.blur_size + DEPTH_GAP;
|
2020-01-13 19:37:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-01-16 01:23:21 +01:00
|
|
|
#if defined(MODE_BOKEH_BOX) || defined(MODE_BOKEH_HEXAGONAL)
|
|
|
|
|
|
|
|
vec4 weighted_filter_dir(vec2 dir, vec2 uv, vec2 pixel_size) {
|
|
|
|
dir *= pixel_size;
|
2020-02-11 14:01:43 +01:00
|
|
|
vec4 color = texture(color_texture, uv);
|
2020-01-16 01:23:21 +01:00
|
|
|
|
|
|
|
vec4 accum = color;
|
|
|
|
float total = 1.0;
|
|
|
|
|
2020-02-11 14:01:43 +01:00
|
|
|
float blur_scale = params.blur_size / float(params.blur_steps);
|
2020-01-16 01:23:21 +01:00
|
|
|
|
|
|
|
if (params.use_jitter) {
|
2020-02-11 14:01:43 +01:00
|
|
|
uv += dir * (hash12n(uv + params.jitter_seed) - 0.5);
|
2020-01-16 01:23:21 +01:00
|
|
|
}
|
|
|
|
|
2020-02-11 14:01:43 +01:00
|
|
|
for (int i = -params.blur_steps; i <= params.blur_steps; i++) {
|
|
|
|
if (i == 0) {
|
2020-01-16 01:23:21 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
float radius = float(i) * blur_scale;
|
|
|
|
vec2 suv = uv + dir * radius;
|
|
|
|
radius = abs(radius);
|
|
|
|
|
|
|
|
vec4 sample_color = texture(color_texture, suv);
|
|
|
|
float limit;
|
|
|
|
|
|
|
|
if (sample_color.a < color.a) {
|
|
|
|
limit = abs(sample_color.a);
|
|
|
|
} else {
|
|
|
|
limit = abs(color.a);
|
|
|
|
}
|
|
|
|
|
|
|
|
limit -= DEPTH_GAP;
|
|
|
|
|
2020-02-11 14:01:43 +01:00
|
|
|
float m = smoothstep(radius - 0.5, radius + 0.5, limit);
|
2020-01-16 01:23:21 +01:00
|
|
|
|
2020-02-11 14:01:43 +01:00
|
|
|
accum += mix(color, sample_color, m);
|
2020-01-16 01:23:21 +01:00
|
|
|
|
|
|
|
total += 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return accum / total;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-01-13 19:37:24 +01:00
|
|
|
void main() {
|
|
|
|
ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
|
|
|
|
|
2020-02-11 14:01:43 +01:00
|
|
|
if (any(greaterThan(pos, params.size))) { //too large, do nothing
|
2020-01-13 19:37:24 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-11 14:01:43 +01:00
|
|
|
vec2 pixel_size = 1.0 / vec2(params.size);
|
2020-01-13 19:37:24 +01:00
|
|
|
vec2 uv = vec2(pos) / vec2(params.size);
|
|
|
|
|
|
|
|
#ifdef MODE_GEN_BLUR_SIZE
|
2020-02-11 14:01:43 +01:00
|
|
|
uv += pixel_size * 0.5;
|
2020-01-13 19:37:24 +01:00
|
|
|
//precompute size in alpha channel
|
|
|
|
float depth = get_depth_at_pos(uv);
|
|
|
|
float size = get_blur_size(depth);
|
|
|
|
|
2020-02-11 14:01:43 +01:00
|
|
|
vec4 color = imageLoad(color_image, pos);
|
2020-01-13 19:37:24 +01:00
|
|
|
color.a = size;
|
2020-02-11 14:01:43 +01:00
|
|
|
imageStore(color_image, pos, color);
|
2020-01-13 19:37:24 +01:00
|
|
|
#endif
|
|
|
|
|
2020-01-16 01:23:21 +01:00
|
|
|
#ifdef MODE_BOKEH_BOX
|
2020-01-13 19:37:24 +01:00
|
|
|
|
2020-01-16 01:23:21 +01:00
|
|
|
//pixel_size*=0.5; //resolution is doubled
|
|
|
|
if (params.second_pass || !params.half_size) {
|
2020-02-11 14:01:43 +01:00
|
|
|
uv += pixel_size * 0.5; //half pixel to read centers
|
2020-01-16 01:23:21 +01:00
|
|
|
} else {
|
2020-02-11 14:01:43 +01:00
|
|
|
uv += pixel_size * 0.25; //half pixel to read centers from full res
|
2020-01-16 01:23:21 +01:00
|
|
|
}
|
|
|
|
|
2020-02-11 14:01:43 +01:00
|
|
|
vec2 dir = (params.second_pass ? vec2(0.0, 1.0) : vec2(1.0, 0.0));
|
2020-01-16 01:23:21 +01:00
|
|
|
|
2020-02-11 14:01:43 +01:00
|
|
|
vec4 color = weighted_filter_dir(dir, uv, pixel_size);
|
2020-01-16 01:23:21 +01:00
|
|
|
|
2020-02-11 14:01:43 +01:00
|
|
|
imageStore(bokeh_image, pos, color);
|
2020-01-16 01:23:21 +01:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MODE_BOKEH_HEXAGONAL
|
|
|
|
|
|
|
|
//pixel_size*=0.5; //resolution is doubled
|
|
|
|
if (params.second_pass || !params.half_size) {
|
2020-02-11 14:01:43 +01:00
|
|
|
uv += pixel_size * 0.5; //half pixel to read centers
|
2020-01-16 01:23:21 +01:00
|
|
|
} else {
|
2020-02-11 14:01:43 +01:00
|
|
|
uv += pixel_size * 0.25; //half pixel to read centers from full res
|
2020-01-16 01:23:21 +01:00
|
|
|
}
|
|
|
|
|
2020-02-11 14:01:43 +01:00
|
|
|
vec2 dir = (params.second_pass ? normalize(vec2(1.0, 0.577350269189626)) : vec2(0.0, 1.0));
|
2020-01-16 01:23:21 +01:00
|
|
|
|
2020-02-11 14:01:43 +01:00
|
|
|
vec4 color = weighted_filter_dir(dir, uv, pixel_size);
|
2020-01-16 01:23:21 +01:00
|
|
|
|
|
|
|
if (params.second_pass) {
|
2020-02-11 14:01:43 +01:00
|
|
|
dir = normalize(vec2(-1.0, 0.577350269189626));
|
2020-01-16 01:23:21 +01:00
|
|
|
|
2020-02-11 14:01:43 +01:00
|
|
|
vec4 color2 = weighted_filter_dir(dir, uv, pixel_size);
|
2020-01-16 01:23:21 +01:00
|
|
|
|
2020-02-11 14:01:43 +01:00
|
|
|
color.rgb = min(color.rgb, color2.rgb);
|
2020-01-16 01:23:21 +01:00
|
|
|
color.a = (color.a + color2.a) * 0.5;
|
|
|
|
}
|
|
|
|
|
2020-02-11 14:01:43 +01:00
|
|
|
imageStore(bokeh_image, pos, color);
|
2020-01-16 01:23:21 +01:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MODE_BOKEH_CIRCULAR
|
|
|
|
|
|
|
|
if (params.half_size) {
|
2020-02-11 14:01:43 +01:00
|
|
|
pixel_size *= 0.5; //resolution is doubled
|
2020-01-16 01:23:21 +01:00
|
|
|
}
|
2020-01-13 22:36:12 +01:00
|
|
|
|
2020-02-11 14:01:43 +01:00
|
|
|
uv += pixel_size * 0.5; //half pixel to read centers
|
2020-01-13 22:36:12 +01:00
|
|
|
|
2020-02-11 14:01:43 +01:00
|
|
|
vec4 color = texture(color_texture, uv);
|
2020-01-13 19:37:24 +01:00
|
|
|
float accum = 1.0;
|
|
|
|
float radius = params.blur_scale;
|
|
|
|
|
|
|
|
for (float ang = 0.0; radius < params.blur_size; ang += GOLDEN_ANGLE) {
|
|
|
|
vec2 suv = uv + vec2(cos(ang), sin(ang)) * pixel_size * radius;
|
|
|
|
vec4 sample_color = texture(color_texture, suv);
|
2020-01-16 01:23:21 +01:00
|
|
|
float sample_size = abs(sample_color.a);
|
|
|
|
if (sample_color.a > color.a) {
|
2020-02-11 14:01:43 +01:00
|
|
|
sample_size = clamp(sample_size, 0.0, abs(color.a) * 2.0);
|
2020-01-13 19:37:24 +01:00
|
|
|
}
|
|
|
|
|
2020-02-11 14:01:43 +01:00
|
|
|
float m = smoothstep(radius - 0.5, radius + 0.5, sample_size);
|
|
|
|
color += mix(color / accum, sample_color, m);
|
2020-01-13 19:37:24 +01:00
|
|
|
accum += 1.0;
|
2020-02-11 14:01:43 +01:00
|
|
|
radius += params.blur_scale / radius;
|
2020-01-13 19:37:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
color /= accum;
|
|
|
|
|
2020-02-11 14:01:43 +01:00
|
|
|
imageStore(bokeh_image, pos, color);
|
2020-01-13 19:37:24 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MODE_COMPOSITE_BOKEH
|
|
|
|
|
2020-02-11 14:01:43 +01:00
|
|
|
uv += pixel_size * 0.5;
|
|
|
|
vec4 color = imageLoad(color_image, pos);
|
|
|
|
vec4 bokeh = texture(source_bokeh, uv);
|
2020-01-13 22:36:12 +01:00
|
|
|
|
2020-01-16 01:23:21 +01:00
|
|
|
float mix_amount;
|
|
|
|
if (bokeh.a < color.a) {
|
2020-02-11 14:01:43 +01:00
|
|
|
mix_amount = min(1.0, max(0.0, max(abs(color.a), abs(bokeh.a)) - DEPTH_GAP));
|
2020-01-16 01:23:21 +01:00
|
|
|
} else {
|
2020-02-11 14:01:43 +01:00
|
|
|
mix_amount = min(1.0, max(0.0, abs(color.a) - DEPTH_GAP));
|
2020-01-16 01:23:21 +01:00
|
|
|
}
|
|
|
|
|
2020-02-11 14:01:43 +01:00
|
|
|
color.rgb = mix(color.rgb, bokeh.rgb, mix_amount); //blend between hires and lowres
|
2020-01-13 22:36:12 +01:00
|
|
|
|
2020-02-11 14:01:43 +01:00
|
|
|
color.a = 0; //reset alpha
|
|
|
|
imageStore(color_image, pos, color);
|
2020-01-13 19:37:24 +01:00
|
|
|
#endif
|
|
|
|
}
|