| 1 |
/* clang-format off */ |
| 2 |
#[vertex] |
| 3 |
|
| 4 |
#version 450 |
| 5 |
|
| 6 |
#VERSION_DEFINES |
| 7 |
|
| 8 |
#include "bokeh_dof_inc.glsl" |
| 9 |
|
| 10 |
layout(location = 0) out vec2 uv_interp; |
| 11 |
/* clang-format on */ |
| 12 |
|
| 13 |
void main() { |
| 14 |
// old code, ARM driver bug on Mali-GXXx GPUs and Vulkan API 1.3.xxx |
| 15 |
// https://github.com/godotengine/godot/pull/92817#issuecomment-2168625982 |
| 16 |
//vec2 base_arr[3] = vec2[](vec2(-1.0, -1.0), vec2(-1.0, 3.0), vec2(3.0, -1.0)); |
| 17 |
//gl_Position = vec4(base_arr[gl_VertexIndex], 0.0, 1.0); |
| 18 |
//uv_interp = clamp(gl_Position.xy, vec2(0.0, 0.0), vec2(1.0, 1.0)) * 2.0; // saturate(x) * 2.0 |
| 19 |
|
| 20 |
vec2 vertex_base; |
| 21 |
if (gl_VertexIndex == 0) { |
| 22 |
vertex_base = vec2(-1.0, -1.0); |
| 23 |
} else if (gl_VertexIndex == 1) { |
| 24 |
vertex_base = vec2(-1.0, 3.0); |
| 25 |
} else { |
| 26 |
vertex_base = vec2(3.0, -1.0); |
| 27 |
} |
| 28 |
gl_Position = vec4(vertex_base, 0.0, 1.0); |
| 29 |
uv_interp = clamp(vertex_base, vec2(0.0, 0.0), vec2(1.0, 1.0)) * 2.0; // saturate(x) * 2.0 |
| 30 |
} |
| 31 |
|
| 32 |
/* clang-format off */ |
| 33 |
#[fragment] |
| 34 |
|
| 35 |
#version 450 |
| 36 |
|
| 37 |
#VERSION_DEFINES |
| 38 |
|
| 39 |
#include "bokeh_dof_inc.glsl" |
| 40 |
|
| 41 |
layout(location = 0) in vec2 uv_interp; |
| 42 |
/* clang-format on */ |
| 43 |
|
| 44 |
#ifdef MODE_GEN_BLUR_SIZE |
| 45 |
layout(location = 0) out float weight; |
| 46 |
|
| 47 |
layout(set = 0, binding = 0) uniform sampler2D source_depth; |
| 48 |
#else |
| 49 |
layout(location = 0) out vec4 frag_color; |
| 50 |
#ifdef OUTPUT_WEIGHT |
| 51 |
layout(location = 1) out float weight; |
| 52 |
#endif |
| 53 |
|
| 54 |
layout(set = 0, binding = 0) uniform sampler2D source_color; |
| 55 |
layout(set = 1, binding = 0) uniform sampler2D source_weight; |
| 56 |
#ifdef MODE_COMPOSITE_BOKEH |
| 57 |
layout(set = 2, binding = 0) uniform sampler2D original_weight; |
| 58 |
#endif |
| 59 |
#endif |
| 60 |
|
| 61 |
//DOF |
| 62 |
// Bokeh single pass implementation based on https://tuxedolabs.blogspot.com/2018/05/bokeh-depth-of-field-in-single-pass.html |
| 63 |
|
| 64 |
#ifdef MODE_GEN_BLUR_SIZE |
| 65 |
|
| 66 |
float get_depth_at_pos(vec2 uv) { |
| 67 |
float depth = textureLod(source_depth, uv, 0.0).x * 2.0 - 1.0; |
| 68 |
if (params.orthogonal) { |
| 69 |
depth = -(depth * (params.z_far - params.z_near) - (params.z_far + params.z_near)) / 2.0; |
| 70 |
} else { |
| 71 |
depth = 2.0 * params.z_near * params.z_far / (params.z_far + params.z_near + depth * (params.z_far - params.z_near)); |
| 72 |
} |
| 73 |
return depth; |
| 74 |
} |
| 75 |
|
| 76 |
float get_blur_size(float depth) { |
| 77 |
if (params.blur_near_active && depth < params.blur_near_begin) { |
| 78 |
if (params.use_physical_near) { |
| 79 |
// Physically-based. |
| 80 |
float d = abs(params.blur_near_begin - depth); |
| 81 |
return -(d / (params.blur_near_begin - d)) * params.blur_size_near - DEPTH_GAP; // Near blur is negative. |
| 82 |
} else { |
| 83 |
// Non-physically-based. |
| 84 |
return -(1.0 - smoothstep(params.blur_near_end, params.blur_near_begin, depth)) * params.blur_size - DEPTH_GAP; // Near blur is negative. |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
if (params.blur_far_active && depth > params.blur_far_begin) { |
| 89 |
if (params.use_physical_far) { |
| 90 |
// Physically-based. |
| 91 |
float d = abs(params.blur_far_begin - depth); |
| 92 |
return (d / (params.blur_far_begin + d)) * params.blur_size_far + DEPTH_GAP; |
| 93 |
} else { |
| 94 |
// Non-physically-based. |
| 95 |
return smoothstep(params.blur_far_begin, params.blur_far_end, depth) * params.blur_size + DEPTH_GAP; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
return 0.0; |
| 100 |
} |
| 101 |
|
| 102 |
#endif |
| 103 |
|
| 104 |
#if defined(MODE_BOKEH_BOX) || defined(MODE_BOKEH_HEXAGONAL) |
| 105 |
|
| 106 |
vec4 weighted_filter_dir(vec2 dir, vec2 uv, vec2 pixel_size) { |
| 107 |
dir *= pixel_size; |
| 108 |
vec4 color = texture(source_color, uv); |
| 109 |
color.a = texture(source_weight, uv).r; |
| 110 |
|
| 111 |
vec4 accum = color; |
| 112 |
float total = 1.0; |
| 113 |
|
| 114 |
float blur_scale = params.blur_size / float(params.blur_steps); |
| 115 |
|
| 116 |
if (params.use_jitter) { |
| 117 |
uv += dir * (hash12n(uv + params.jitter_seed) - 0.5); |
| 118 |
} |
| 119 |
|
| 120 |
for (int i = -params.blur_steps; i <= params.blur_steps; i++) { |
| 121 |
if (i == 0) { |
| 122 |
continue; |
| 123 |
} |
| 124 |
float radius = float(i) * blur_scale; |
| 125 |
vec2 suv = uv + dir * radius; |
| 126 |
radius = abs(radius); |
| 127 |
|
| 128 |
vec4 sample_color = texture(source_color, suv); |
| 129 |
sample_color.a = texture(source_weight, suv).r; |
| 130 |
float limit; |
| 131 |
|
| 132 |
if (sample_color.a < color.a) { |
| 133 |
limit = abs(sample_color.a); |
| 134 |
} else { |
| 135 |
limit = abs(color.a); |
| 136 |
} |
| 137 |
|
| 138 |
limit -= DEPTH_GAP; |
| 139 |
|
| 140 |
float m = smoothstep(radius - 0.5, radius + 0.5, limit); |
| 141 |
|
| 142 |
accum += mix(color, sample_color, m); |
| 143 |
|
| 144 |
total += 1.0; |
| 145 |
} |
| 146 |
|
| 147 |
return accum / total; |
| 148 |
} |
| 149 |
|
| 150 |
#endif |
| 151 |
|
| 152 |
void main() { |
| 153 |
vec2 pixel_size = 1.0 / vec2(params.size); |
| 154 |
vec2 uv = uv_interp; |
| 155 |
|
| 156 |
#ifdef MODE_GEN_BLUR_SIZE |
| 157 |
uv += pixel_size * 0.5; |
| 158 |
float center_depth = get_depth_at_pos(uv); |
| 159 |
weight = get_blur_size(center_depth); |
| 160 |
#endif |
| 161 |
|
| 162 |
#ifdef MODE_BOKEH_BOX |
| 163 |
//pixel_size*=0.5; //resolution is doubled |
| 164 |
if (params.second_pass || !params.half_size) { |
| 165 |
uv += pixel_size * 0.5; //half pixel to read centers |
| 166 |
} else { |
| 167 |
uv += pixel_size * 0.25; //half pixel to read centers from full res |
| 168 |
} |
| 169 |
|
| 170 |
float alpha = texture(source_color, uv).a; // retain this |
| 171 |
vec2 dir = (params.second_pass ? vec2(0.0, 1.0) : vec2(1.0, 0.0)); |
| 172 |
|
| 173 |
vec4 color = weighted_filter_dir(dir, uv, pixel_size); |
| 174 |
|
| 175 |
frag_color = color; |
| 176 |
frag_color.a = alpha; // attempt to retain this in case we have a transparent background, ignored if half_size |
| 177 |
#ifdef OUTPUT_WEIGHT |
| 178 |
weight = color.a; |
| 179 |
#endif |
| 180 |
|
| 181 |
#endif |
| 182 |
|
| 183 |
#ifdef MODE_BOKEH_HEXAGONAL |
| 184 |
|
| 185 |
//pixel_size*=0.5; //resolution is doubled |
| 186 |
if (params.second_pass || !params.half_size) { |
| 187 |
uv += pixel_size * 0.5; //half pixel to read centers |
| 188 |
} else { |
| 189 |
uv += pixel_size * 0.25; //half pixel to read centers from full res |
| 190 |
} |
| 191 |
|
| 192 |
float alpha = texture(source_color, uv).a; // retain this |
| 193 |
|
| 194 |
vec2 dir = (params.second_pass ? normalize(vec2(1.0, 0.577350269189626)) : vec2(0.0, 1.0)); |
| 195 |
|
| 196 |
vec4 color = weighted_filter_dir(dir, uv, pixel_size); |
| 197 |
|
| 198 |
if (params.second_pass) { |
| 199 |
dir = normalize(vec2(-1.0, 0.577350269189626)); |
| 200 |
|
| 201 |
vec4 color2 = weighted_filter_dir(dir, uv, pixel_size); |
| 202 |
|
| 203 |
color.rgb = min(color.rgb, color2.rgb); |
| 204 |
color.a = (color.a + color2.a) * 0.5; |
| 205 |
} |
| 206 |
|
| 207 |
frag_color = color; |
| 208 |
frag_color.a = alpha; // attempt to retain this in case we have a transparent background, ignored if half_size |
| 209 |
#ifdef OUTPUT_WEIGHT |
| 210 |
weight = color.a; |
| 211 |
#endif |
| 212 |
|
| 213 |
#endif |
| 214 |
|
| 215 |
#ifdef MODE_BOKEH_CIRCULAR |
| 216 |
if (params.half_size) { |
| 217 |
pixel_size *= 0.5; //resolution is doubled |
| 218 |
} |
| 219 |
|
| 220 |
uv += pixel_size * 0.5; //half pixel to read centers |
| 221 |
|
| 222 |
vec4 color = texture(source_color, uv); |
| 223 |
float alpha = color.a; // retain this |
| 224 |
color.a = texture(source_weight, uv).r; |
| 225 |
|
| 226 |
vec4 color_accum = color; |
| 227 |
float accum = 1.0; |
| 228 |
|
| 229 |
float radius = params.blur_scale; |
| 230 |
for (float ang = 0.0; radius < params.blur_size; ang += GOLDEN_ANGLE) { |
| 231 |
vec2 uv_adj = uv + vec2(cos(ang), sin(ang)) * pixel_size * radius; |
| 232 |
|
| 233 |
vec4 sample_color = texture(source_color, uv_adj); |
| 234 |
sample_color.a = texture(source_weight, uv_adj).r; |
| 235 |
|
| 236 |
float limit = abs(sample_color.a); |
| 237 |
if (sample_color.a > color.a) { |
| 238 |
limit = clamp(limit, 0.0, abs(color.a) * 2.0); |
| 239 |
} |
| 240 |
|
| 241 |
limit -= DEPTH_GAP; |
| 242 |
|
| 243 |
float m = smoothstep(radius - 0.5, radius + 0.5, limit); |
| 244 |
color_accum += mix(color_accum / accum, sample_color, m); |
| 245 |
accum += 1.0; |
| 246 |
|
| 247 |
radius += params.blur_scale / radius; |
| 248 |
} |
| 249 |
|
| 250 |
color_accum = color_accum / accum; |
| 251 |
|
| 252 |
frag_color.rgb = color_accum.rgb; |
| 253 |
frag_color.a = alpha; // attempt to retain this in case we have a transparent background, ignored if half_size |
| 254 |
#ifdef OUTPUT_WEIGHT |
| 255 |
weight = color_accum.a; |
| 256 |
#endif |
| 257 |
|
| 258 |
#endif |
| 259 |
|
| 260 |
#ifdef MODE_COMPOSITE_BOKEH |
| 261 |
frag_color.rgb = texture(source_color, uv).rgb; |
| 262 |
|
| 263 |
float center_weight = texture(source_weight, uv).r; |
| 264 |
float sample_weight = texture(original_weight, uv).r; |
| 265 |
|
| 266 |
float mix_amount; |
| 267 |
if (sample_weight < center_weight) { |
| 268 |
mix_amount = min(1.0, max(0.0, max(abs(center_weight), abs(sample_weight)) - DEPTH_GAP)); |
| 269 |
} else { |
| 270 |
mix_amount = min(1.0, max(0.0, abs(center_weight) - DEPTH_GAP)); |
| 271 |
} |
| 272 |
|
| 273 |
// let alpha blending take care of mixing |
| 274 |
frag_color.a = mix_amount; |
| 275 |
#endif |
| 276 |
} |