Blender V5.3
workbench_fx_dof.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#pragma once
12
13#include "draw_view.bsl.hh"
17
18namespace workbench::dof {
19
20[[vertex]]
21void vert([[vertex_id]] const int vert_id, [[position]] float4 &position)
22{
24}
25
26#define MAX_COC_SIZE 100.0f
27
28float2 dof_encode_coc(float near, float far)
29{
30 return float2(near, far) / MAX_COC_SIZE;
31}
33{
34 return max(cocs.x, cocs.y) * MAX_COC_SIZE;
35}
37{
38 return ((cocs.x > cocs.y) ? cocs.x : -cocs.y) * MAX_COC_SIZE;
39}
40
41float4 sum_weighted(float4 val0, float4 val1, float4 val2, float4 val3, float4 weights)
42{
43 return (val0 * weights[0] + val1 * weights[1] + val2 * weights[2] + val3 * weights[3]) *
44 safe_rcp(weights[0] + weights[1] + weights[2] + weights[3]);
45}
46
52
53 /* divide by sensor size to get the normalized size */
54 template<typename T> T calculate_coc(T z_depth)
55 {
56 float aperture_size = dof_params.x;
57 float distance = dof_params.y;
58 float invsensor_size = dof_params.z;
59 return aperture_size * (distance / z_depth - 1.0f) * invsensor_size;
60 }
61};
62
63template float DepthOfField::calculate_coc<float>(float);
65
66/*
67 * NOTE: Keep the sampler bind points consistent between the steps.
68 *
69 * [[sampler(0)]] sampler2D input_coc_tx;
70 * [[sampler(1)]] sampler2D scene_color_tx;
71 * [[sampler(2)]] sampler2D scene_depth_tx;
72 * [[sampler(3)]] sampler2D half_res_color_tx;
73 * [[sampler(4)]] sampler2D blur_tx;
74 * [[sampler(5)]] sampler2D noise_tx;
75 */
76
81
86
91
93 [[resource_table]] Prepare &srt,
94 [[resource_table]] draw::View &views,
95 [[out]] PrepareOut &out,
96 [[frag_coord]] const float4 frag_co)
97{
98 int4 texel = int4(frag_co.xyxy) * 2 + int4(0, 0, 1, 1);
99
100 float4 color1 = texelFetch(srt.scene_color_tx, texel.xy, 0);
101 float4 color2 = texelFetch(srt.scene_color_tx, texel.zw, 0);
102 float4 color3 = texelFetch(srt.scene_color_tx, texel.zy, 0);
103 float4 color4 = texelFetch(srt.scene_color_tx, texel.xw, 0);
104
105 float4 depths;
106 depths.x = texelFetch(srt.scene_depth_tx, texel.xy, 0).x;
107 depths.y = texelFetch(srt.scene_depth_tx, texel.zw, 0).x;
108 depths.z = texelFetch(srt.scene_depth_tx, texel.zy, 0).x;
109 depths.w = texelFetch(srt.scene_depth_tx, texel.xw, 0).x;
110
111 ViewMatrices view = views.get(0);
112
113 float4 zdepths = float4(view.depth_screen_to_view(depths.x),
114 view.depth_screen_to_view(depths.y),
115 view.depth_screen_to_view(depths.z),
116 view.depth_screen_to_view(depths.w));
117 float4 cocs_near = dof.calculate_coc(zdepths);
118 float4 cocs_far = -cocs_near;
119
120 float coc_near = max(reduce_max(cocs_near), 0.0f);
121 float coc_far = max(reduce_max(cocs_far), 0.0f);
122
123 /* now we need to write the near-far fields premultiplied by the coc
124 * also use bilateral weighting by each coc values to avoid bleeding. */
125 float4 near_weights = step(0.0f, cocs_near) *
126 clamp(1.0f - abs(coc_near - cocs_near), 0.0f, 1.0f);
127 float4 far_weights = step(0.0f, cocs_far) * clamp(1.0f - abs(coc_far - cocs_far), 0.0f, 1.0f);
128
129 /* now write output to weighted buffers. */
130 /* Take far plane pixels in priority. */
131 float4 w = any(notEqual(far_weights, float4(0.0f))) ? far_weights : near_weights;
132 out.half_res_color = sum_weighted(color1, color2, color3, color4, w);
133 out.half_res_color = clamp(out.half_res_color, 0.0f, 3.0f);
134
135 out.normalized_coc = dof_encode_coc(coc_near, coc_far);
136}
137
142
147
152
154 [[out]] DownSampleOut &out,
155 [[frag_coord]] const float4 frag_co)
156{
157 float4 texel = float4(frag_co.xyxy) * 2.0f + float4(0.0f, 0.0f, 1.0f, 1.0f);
158 texel = (texel - 0.5f) / float4(textureSize(srt.scene_color_tx, 0).xyxy);
159
160 /* Using texelFetch can bypass the mip range setting on some platform.
161 * Using texture LOD fixes this issue. Note that we need to disable filtering to get the right
162 * texel values. */
163 float4 color1 = textureLod(srt.scene_color_tx, texel.xy, 0.0f);
164 float4 color2 = textureLod(srt.scene_color_tx, texel.zw, 0.0f);
165 float4 color3 = textureLod(srt.scene_color_tx, texel.zy, 0.0f);
166 float4 color4 = textureLod(srt.scene_color_tx, texel.xw, 0.0f);
167
168 float2 cocs1 = textureLod(srt.input_coc_tx, texel.xy, 0.0f).rg;
169 float2 cocs2 = textureLod(srt.input_coc_tx, texel.zw, 0.0f).rg;
170 float2 cocs3 = textureLod(srt.input_coc_tx, texel.zy, 0.0f).rg;
171 float2 cocs4 = textureLod(srt.input_coc_tx, texel.xw, 0.0f).rg;
172
173 float4 cocs_near = float4(cocs1.r, cocs2.r, cocs3.r, cocs4.r) * MAX_COC_SIZE;
174 float4 cocs_far = float4(cocs1.g, cocs2.g, cocs3.g, cocs4.g) * MAX_COC_SIZE;
175
176 float coc_near = reduce_max(cocs_near);
177 float coc_far = reduce_max(cocs_far);
178
179 /* Now we need to write the near-far fields pre-multiplied by the COC
180 * also use bilateral weighting by each COC values to avoid bleeding. */
181 float4 near_weights = step(0.0f, cocs_near) *
182 clamp(1.0f - abs(coc_near - cocs_near), 0.0f, 1.0f);
183 float4 far_weights = step(0.0f, cocs_far) * clamp(1.0f - abs(coc_far - cocs_far), 0.0f, 1.0f);
184
185 /* now write output to weighted buffers. */
186 float4 w = any(notEqual(far_weights, float4(0.0f))) ? far_weights : near_weights;
187 out.color = sum_weighted(color1, color2, color3, color4, w);
188
189 out.cocs = dof_encode_coc(coc_near, coc_far);
190}
191
197
198static constexpr int dof_sample_count = 49;
199
200struct Blur1 {
205
206 float2 get_random_vector(float2 frag_co, float offset) const
207 {
208 /* Interleaved gradient noise by Jorge Jimenez
209 * http://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare */
210 float ign = fract(offset +
211 52.9829189f * fract(0.06711056f * frag_co.x + 0.00583715f * frag_co.y));
212 float bn = texelFetch(noise_tx, int2(frag_co.xy) % 64, 0).a;
213 float ang = M_PI * 2.0f * fract(bn + offset);
214 return float2(cos(ang), sin(ang)) * sqrt(ign);
215 }
216};
217
218struct Blur1Out {
220};
221
223 [[resource_table]] Blur1 &srt,
224 [[out]] Blur1Out &out,
225 [[frag_coord]] const float4 frag_co)
226{
227 float2 uv = frag_co.xy * dof.inverted_viewport_size * 2.0f;
228
229 float2 size = float2(textureSize(srt.half_res_color_tx, 0).xy);
230 int2 texel = int2(uv * size);
231
232 float4 color = float4(0.0f);
233 float tot = 0.0f;
234
235 float coc = dof_decode_coc(texelFetch(srt.input_coc_tx, texel, 0).rg);
236 float max_radius = coc;
237 float2 noise = srt.get_random_vector(frag_co.xy, dof.noise_offset) * 0.2f *
238 clamp(max_radius * 0.2f - 4.0f, 0.0f, 1.0f);
239 for (int i = 0; i < dof_sample_count; i++) {
240 float2 tc = uv + (noise + srt.samples[i].xy) * dof.inverted_viewport_size * max_radius;
241
242 /* decode_signed_coc return biggest coc. */
243 coc = abs(dof_decode_signed_coc(texture(srt.input_coc_tx, tc).rg));
244
245 float lod = log2(clamp((coc + min(coc, max_radius)) * 0.5f - 21.0f, 0.0f, 16.0f) * 0.25f);
246 float4 samp = textureLod(srt.half_res_color_tx, tc, lod);
247
248 float radius = srt.samples[i].z * max_radius;
249 float weight = abs(coc) * smoothstep(radius - 0.5f, radius + 0.5f, abs(coc));
250
251 color += samp * weight;
252 tot += weight;
253 }
254
255 if (tot > 0.0f) {
256 out.color = color / tot;
257 }
258 else {
259 out.color = textureLod(srt.half_res_color_tx, uv, 0.0f);
260 }
261}
262
272
277
278struct Blur2Out {
280};
281
283{
284 float4 temp = a;
285 a = min(a, b);
286 b = max(temp, b);
287}
288
290{
291 s2(a, b);
292 s2(a, c);
293}
294
296{
297 s2(b, c);
298 s2(a, c);
299}
300
302{
303 mx3(a, b, c);
304 s2(a, b); /* 3 exchanges */
305}
306
308{
309 s2(a, b);
310 s2(c, d);
311 s2(a, c);
312 s2(b, d); /* 4 exchanges */
313}
314
316{
317 s2(a, b);
318 s2(c, d);
319 mn3(a, c, e);
320 mx3(b, d, e); /* 6 exchanges */
321}
322
323[[force_inline]] void mnmx6(float4 &a, float4 &b, float4 &c, float4 &d, float4 &e, float4 &f)
324{
325 s2(a, d);
326 s2(b, e);
327 s2(c, f);
328 mn3(a, b, c);
329 mx3(d, e, f); /* 7 exchanges */
330}
331
333 [[out]] Blur2Out &out,
334 [[frag_coord]] const float4 frag_co)
335{
336 /* Half Res pass */
337 float2 pixel_size = 1.0f / float2(textureSize(srt.blur_tx, 0).xy);
338 float2 uv = frag_co.xy * pixel_size.xy;
339 float coc = dof_decode_coc(texture(srt.input_coc_tx, uv).rg);
340 /* Only use this filter if coc is > 9.0f
341 * since this filter is not weighted by CoC
342 * and can bleed a bit. */
343 float rad = clamp(coc - 9.0f, 0.0f, 1.0f);
344
345 float4 v[9];
346
347 /* Add the pixels which make up our window to the pixel array. */
348 for (int dX = -1; dX <= 1; dX++) [[unroll]] {
349 for (int dY = -1; dY <= 1; dY++) [[unroll]] {
350 float2 offset = float2(float(dX), float(dY));
351 /* If a pixel in the window is located at (x+dX, y+dY), put it at index (dX + R)(2R + 1) +
352 * (dY + R) of the pixel array. This will fill the pixel array, with the top left pixel of
353 * the window at pixel[0] and the bottom right pixel of the window at pixel[N-1]. */
354 v[(dX + 1) * 3 + (dY + 1)] = texture(srt.blur_tx, uv + offset * pixel_size * rad);
355 }
356 }
357
358 /* Starting with a subset of size 6, remove the min and max each time */
359 mnmx6(v[0], v[1], v[2], v[3], v[4], v[5]);
360 mnmx5(v[1], v[2], v[3], v[4], v[6]);
361 mnmx4(v[2], v[3], v[4], v[7]);
362 mnmx3(v[3], v[4], v[8]);
363 out.color = v[4];
364}
365
369
374
379
381 [[resource_table]] Resolve &srt,
382 [[resource_table]] draw::View &views,
383 [[out]] ResolveOut &out,
384 [[frag_coord]] const float4 frag_co)
385{
386 /* Full-screen pass. */
387 float2 pixel_size = 0.5f / float2(textureSize(srt.half_res_color_tx, 0).xy);
388 float2 uv = frag_co.xy * pixel_size;
389
390 ViewMatrices view = views.get(0);
391
392 /* TODO: MAKE SURE TO ALIGN SAMPLE POSITION TO AVOID OFFSET IN THE BOKEH. */
393 float depth = texelFetch(srt.scene_depth_tx, int2(frag_co.xy), 0).r;
394 float zdepth = view.depth_screen_to_view(depth);
395 float coc = dof.calculate_coc(zdepth);
396
397 float blend = smoothstep(1.0f, 3.0f, abs(coc));
398 out.color_add = texture(srt.half_res_color_tx, uv) * blend;
399 out.color_mul = float4(1.0f - blend);
400}
401
402} // namespace workbench::dof
403
#define M_PI
btScalar max
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define uniform(slot)
#define position
#define index(i)
#define frag_coord
#define push_constant
#define resource_table
#define sampler(slot)
#define fragment
#define vertex_id
#define unroll
#define force_inline
#define vertex
#define frag_color(slot)
VecBase< bool, D > notEqual(VecOp< T, D >, VecOp< T, D >) RET
T smoothstep(T, T, T) RET
#define sin
T fract(T) RET
bool any(VecOp< bool, D >) RET
#define cos
#define log2
#define abs
float distance(float, float) RET
#define sqrt
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
constexpr T clamp(T, U, U) RET
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
TEX_TEMPLATE DataVec texelFetch(T, IntCoord, int) RET
TEX_TEMPLATE DataVec textureLod(T, FltCoord, float) RET
SamplerBase< float, 2 > sampler2D
TEX_TEMPLATE SizeVec textureSize(T, int) RET
void fullscreen_vertex(int vertex_id, float4 &out_position)
float noise(float p)
float safe_rcp(float a)
float reduce_max(float2 a)
#define T
void vert(const int vert_id, float4 &position)
static constexpr int dof_sample_count
float2 dof_encode_coc(float near, float far)
float4 sum_weighted(float4 val0, float4 val1, float4 val2, float4 val3, float4 weights)
float dof_decode_signed_coc(float2 cocs)
void mnmx3(float4 &a, float4 &b, float4 &c)
void mn3(float4 &a, float4 &b, float4 &c)
void downsample(DownSample &srt, DownSampleOut &out, const float4 frag_co)
float dof_decode_coc(float2 cocs)
void blur2(Blur2 &srt, Blur2Out &out, const float4 frag_co)
void mnmx6(float4 &a, float4 &b, float4 &c, float4 &d, float4 &e, float4 &f)
void resolve(DepthOfField &dof, Resolve &srt, draw::View &views, ResolveOut &out, const float4 frag_co)
void mnmx4(float4 &a, float4 &b, float4 &c, float4 &d)
void mnmx5(float4 &a, float4 &b, float4 &c, float4 &d, float4 &e)
void s2(float4 &a, float4 &b)
void blur1(DepthOfField &dof, Blur1 &srt, Blur1Out &out, const float4 frag_co)
void prepare(DepthOfField &dof, Prepare &srt, draw::View &views, PrepareOut &out, const float4 frag_co)
void mx3(float4 &a, float4 &b, float4 &c)
#define min(a, b)
Definition sort.cc:38
float depth_screen_to_view(float ss_depth) const
float x
float y
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225
float4 samples[dof_sample_count]
float2 get_random_vector(float2 frag_co, float offset) const
PipelineGraphic workbench_effect_dof_blur2(workbench::dof::vert, workbench::dof::blur2)
PipelineGraphic workbench_effect_dof_blur1(workbench::dof::vert, workbench::dof::blur1)
#define MAX_COC_SIZE
PipelineGraphic workbench_effect_dof_resolve(workbench::dof::vert, workbench::dof::resolve)
PipelineGraphic workbench_effect_dof_prepare(workbench::dof::vert, workbench::dof::prepare)
PipelineGraphic workbench_effect_dof_downsample(workbench::dof::vert, workbench::dof::downsample)