Debian Codegraph @

servers/rendering/renderer_rd/shaders/effects/bokeh_dof.glsl

raw
1 #[compute]
2
3 #version 450
4
5 #VERSION_DEFINES
6
7 #define BLOCK_SIZE 8
8
9 layout(local_size_x = BLOCK_SIZE, local_size_y = BLOCK_SIZE, local_size_z = 1) in;
10
11 #ifdef MODE_GEN_BLUR_SIZE
12 layout(rgba16f, set = 0, binding = 0) uniform restrict image2D color_image;
13 layout(set = 1, binding = 0) uniform sampler2D source_depth;
14 #endif
15
16 #if defined(MODE_BOKEH_BOX) || defined(MODE_BOKEH_HEXAGONAL) || defined(MODE_BOKEH_CIRCULAR)
17 layout(set = 1, binding = 0) uniform sampler2D color_texture;
18 layout(rgba16f, set = 0, binding = 0) uniform restrict writeonly image2D bokeh_image;
19 #endif
20
21 #ifdef MODE_COMPOSITE_BOKEH
22 layout(rgba16f, set = 0, binding = 0) uniform restrict image2D color_image;
23 layout(set = 1, binding = 0) uniform sampler2D source_bokeh;
24 #endif
25
26 // based on https://www.shadertoy.com/view/Xd3GDl
27
28 #include "bokeh_dof_inc.glsl"
29
30 #ifdef MODE_GEN_BLUR_SIZE
31
32 float get_depth_at_pos(vec2 uv) {
33 float depth = textureLod(source_depth, uv, 0.0).x * 2.0 - 1.0;
34 if (params.orthogonal) {
35 depth = -(depth * (params.z_far - params.z_near) - (params.z_far + params.z_near)) / 2.0;
36 } else {
37 depth = 2.0 * params.z_near * params.z_far / (params.z_far + params.z_near + depth * (params.z_far - params.z_near));
38 }
39 return depth;
40 }
41
42 float get_blur_size(float depth) {
43 if (params.blur_near_active && depth < params.blur_near_begin) {
44 if (params.use_physical_near) {
45 // Physically-based.
46 float d = abs(params.blur_near_begin - depth);
47 return -(d / (params.blur_near_begin - d)) * params.blur_size_near - DEPTH_GAP; // Near blur is negative.
48 } else {
49 // Non-physically-based.
50 return -(1.0 - smoothstep(params.blur_near_end, params.blur_near_begin, depth)) * params.blur_size - DEPTH_GAP; // Near blur is negative.
51 }
52 }
53
54 if (params.blur_far_active && depth > params.blur_far_begin) {
55 if (params.use_physical_far) {
56 // Physically-based.
57 float d = abs(params.blur_far_begin - depth);
58 return (d / (params.blur_far_begin + d)) * params.blur_size_far + DEPTH_GAP;
59 } else {
60 // Non-physically-based.
61 return smoothstep(params.blur_far_begin, params.blur_far_end, depth) * params.blur_size + DEPTH_GAP;
62 }
63 }
64
65 return 0.0;
66 }
67
68 #endif
69
70 #if defined(MODE_BOKEH_BOX) || defined(MODE_BOKEH_HEXAGONAL)
71
72 vec4 weighted_filter_dir(vec2 dir, vec2 uv, vec2 pixel_size) {
73 dir *= pixel_size;
74 vec4 color = texture(color_texture, uv);
75
76 vec4 accum = color;
77 float total = 1.0;
78
79 float blur_scale = params.blur_size / float(params.blur_steps);
80
81 if (params.use_jitter) {
82 uv += dir * (hash12n(uv + params.jitter_seed) - 0.5);
83 }
84
85 for (int i = -params.blur_steps; i <= params.blur_steps; i++) {
86 if (i == 0) {
87 continue;
88 }
89 float radius = float(i) * blur_scale;
90 vec2 suv = uv + dir * radius;
91 radius = abs(radius);
92
93 vec4 sample_color = texture(color_texture, suv);
94 float limit;
95
96 if (sample_color.a < color.a) {
97 limit = abs(sample_color.a);
98 } else {
99 limit = abs(color.a);
100 }
101
102 limit -= DEPTH_GAP;
103
104 float m = smoothstep(radius - 0.5, radius + 0.5, limit);
105
106 accum += mix(color, sample_color, m);
107
108 total += 1.0;
109 }
110
111 return accum / total;
112 }
113
114 #endif
115
116 void main() {
117 ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
118
119 if (any(greaterThan(pos, params.size))) { //too large, do nothing
120 return;
121 }
122
123 vec2 pixel_size = 1.0 / vec2(params.size);
124 vec2 uv = vec2(pos) / vec2(params.size);
125
126 #ifdef MODE_GEN_BLUR_SIZE
127 uv += pixel_size * 0.5;
128 //precompute size in alpha channel
129 float depth = get_depth_at_pos(uv);
130 float size = get_blur_size(depth);
131
132 vec4 color = imageLoad(color_image, pos);
133 color.a = size;
134 imageStore(color_image, pos, color);
135 #endif
136
137 #ifdef MODE_BOKEH_BOX
138
139 //pixel_size*=0.5; //resolution is doubled
140 if (params.second_pass || !params.half_size) {
141 uv += pixel_size * 0.5; //half pixel to read centers
142 } else {
143 uv += pixel_size * 0.25; //half pixel to read centers from full res
144 }
145
146 vec2 dir = (params.second_pass ? vec2(0.0, 1.0) : vec2(1.0, 0.0));
147
148 vec4 color = weighted_filter_dir(dir, uv, pixel_size);
149
150 imageStore(bokeh_image, pos, color);
151
152 #endif
153
154 #ifdef MODE_BOKEH_HEXAGONAL
155
156 //pixel_size*=0.5; //resolution is doubled
157 if (params.second_pass || !params.half_size) {
158 uv += pixel_size * 0.5; //half pixel to read centers
159 } else {
160 uv += pixel_size * 0.25; //half pixel to read centers from full res
161 }
162
163 vec2 dir = (params.second_pass ? normalize(vec2(1.0, 0.577350269189626)) : vec2(0.0, 1.0));
164
165 vec4 color = weighted_filter_dir(dir, uv, pixel_size);
166
167 if (params.second_pass) {
168 dir = normalize(vec2(-1.0, 0.577350269189626));
169
170 vec4 color2 = weighted_filter_dir(dir, uv, pixel_size);
171
172 color.rgb = min(color.rgb, color2.rgb);
173 color.a = (color.a + color2.a) * 0.5;
174 }
175
176 imageStore(bokeh_image, pos, color);
177
178 #endif
179
180 #ifdef MODE_BOKEH_CIRCULAR
181
182 if (params.half_size) {
183 pixel_size *= 0.5; //resolution is doubled
184 }
185
186 uv += pixel_size * 0.5; //half pixel to read centers
187
188 vec4 color = texture(color_texture, uv);
189 float initial_blur = color.a;
190 float accum = 1.0;
191 float radius = params.blur_scale;
192
193 for (float ang = 0.0; radius < params.blur_size; ang += GOLDEN_ANGLE) {
194 vec2 suv = uv + vec2(cos(ang), sin(ang)) * pixel_size * radius;
195 vec4 sample_color = texture(color_texture, suv);
196 float sample_size = abs(sample_color.a);
197 if (sample_color.a > initial_blur) {
198 sample_size = clamp(sample_size, 0.0, abs(initial_blur) * 2.0);
199 }
200
201 float m = smoothstep(radius - 0.5, radius + 0.5, sample_size);
202 color += mix(color / accum, sample_color, m);
203 accum += 1.0;
204 radius += params.blur_scale / radius;
205 }
206
207 color /= accum;
208
209 imageStore(bokeh_image, pos, color);
210 #endif
211
212 #ifdef MODE_COMPOSITE_BOKEH
213
214 uv += pixel_size * 0.5;
215 vec4 color = imageLoad(color_image, pos);
216 vec4 bokeh = texture(source_bokeh, uv);
217
218 float mix_amount;
219 if (bokeh.a < color.a) {
220 mix_amount = min(1.0, max(0.0, max(abs(color.a), abs(bokeh.a)) - DEPTH_GAP));
221 } else {
222 mix_amount = min(1.0, max(0.0, abs(color.a) - DEPTH_GAP));
223 }
224
225 color.rgb = mix(color.rgb, bokeh.rgb, mix_amount); //blend between hires and lowres
226
227 color.a = 0; //reset alpha
228 imageStore(color_image, pos, color);
229 #endif
230 }