Blender V5.3
eevee_depth_of_field_filter.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
7#include "eevee_defines.hh"
9
17
20 float weight;
21};
22
23struct Resources {
26
29
30 /* -------------------------------------------------------------------- */
33
34 [[shared]] float4 color_cache[DOF_FILTER_GROUP_SIZE + 2][DOF_FILTER_GROUP_SIZE + 2];
35 [[shared]] float weight_cache[DOF_FILTER_GROUP_SIZE + 2][DOF_FILTER_GROUP_SIZE + 2];
36
37 void init(uint3 global_id, uint3 local_id)
38 {
57
58 constexpr uint cache_size = uint(DOF_FILTER_GROUP_SIZE + 2);
59
60 int2 texel = int2(global_id.xy) - 1;
61 if (all(lessThan(local_id.xy, uint2(cache_size / 2u)))) {
62 for (int y = 0; y < 2; y++) {
63 for (int x = 0; x < 2; x++) {
64 int2 offset = int2(x, y) * int2(cache_size / 2u);
65 int2 cache_texel = int2(local_id.xy) + offset;
66 int2 load_texel = clamp(texel + offset, int2(0), textureSize(color_tx, 0) - 1);
67
68 color_cache[cache_texel.y][cache_texel.x] = texelFetch(color_tx, load_texel, 0);
69 weight_cache[cache_texel.y][cache_texel.x] = texelFetch(weight_tx, load_texel, 0).r;
70 }
71 }
72 }
73 barrier();
74 }
75
76 FilterSample fetch_sample(int x, int y) const
77 {
78 return {color_cache[y][x], weight_cache[y][x]};
79 }
80
82};
83
84/* -------------------------------------------------------------------- */
92
94{
95 return {min(a.color, b.color), min(a.weight, b.weight)};
96}
97
99{
100 return {max(a.color, b.color), max(a.weight, b.weight)};
101}
102
104{
105 return {min(a.color, min(c.color, b.color)), min(a.weight, min(c.weight, b.weight))};
106}
107
109{
110 return {max(a.color, max(c.color, b.color)), max(a.weight, max(c.weight, b.weight))};
111}
112
114{
115 /* From diagram, with nodes numbered from top to bottom. */
116 FilterSample l1 = filter_min(s2, s3);
117 FilterSample h1 = filter_max(s2, s3);
118 FilterSample h2 = filter_max(s1, l1);
119 FilterSample l3 = filter_min(h2, h1);
120 return l3;
121}
122
128
130{
131 /* From diagram, with nodes numbered from top to bottom. */
132 FilterSample h1 = filter_max(s2, s3);
133 FilterSample l1 = filter_min(s2, s3);
134
135 FilterSample h2 = filter_max(s1, l1);
136 FilterSample l2 = filter_min(s1, l1);
137
138 FilterSample h3 = filter_max(h2, h1);
139 FilterSample l3 = filter_min(h2, h1);
140
141 FilterLmhResult result;
142 result.low = l2;
143 result.median = l3;
144 result.high = h3;
145
146 return result;
147}
148
150
151[[compute, local_size(DOF_FILTER_GROUP_SIZE, DOF_FILTER_GROUP_SIZE)]]
153 [[global_invocation_id]] const uint3 global_id,
154 [[local_invocation_id]] const uint3 local_id)
155{
161
162 srt.init(global_id, local_id);
163
164 int2 texel = int2(local_id.xy);
165
166 FilterLmhResult rows[3];
167 for (int y = 0; y < 3; y++) [[unroll]] {
168 rows[y] = filter_lmh(srt.fetch_sample(texel[0] + 0, texel[1] + y),
169 srt.fetch_sample(texel[0] + 1, texel[1] + y),
170 srt.fetch_sample(texel[0] + 2, texel[1] + y));
171 }
172 /* Left nodes. */
173 FilterSample high = filter_max(rows[0].low, rows[1].low, rows[2].low);
174 /* Right nodes. */
175 FilterSample low = filter_min(rows[0].high, rows[1].high, rows[2].high);
176 /* Center nodes. */
177 FilterSample median = filter_median(rows[0].median, rows[1].median, rows[2].median);
178 /* Last bottom nodes. */
179 median = filter_median(low, median, high);
180
181 int2 out_texel = int2(global_id.xy);
182 imageStore(srt.out_color_img, out_texel, median.color);
183 imageStore(srt.out_weight_img, out_texel, float4(median.weight));
184}
185
186} // namespace eevee::dof::filter_pass
187
btScalar max
PipelineCompute eevee_depth_of_field_filter(eevee::dof::filter_pass::comp_main)
@ SFLOAT_16_16_16_16
#define shared
#define image(slot, qualifiers, format)
#define resource_table
#define sampler(slot)
#define global_invocation_id
#define local_invocation_id
#define unroll
#define compute
#define local_size(...)
bool all(VecOp< bool, D >) RET
VecBase< bool, D > lessThan(VecOp< T, D >, VecOp< T, D >) RET
void barrier()
constexpr T clamp(T, U, U) RET
ImageBase< float, 2 > image2D
IMG_TEMPLATE void imageStore(T &, IntCoord, DataVec)
TEX_TEMPLATE DataVec texelFetch(T, IntCoord, int) RET
SamplerBase< float, 2 > sampler2D
TEX_TEMPLATE SizeVec textureSize(T, int) RET
FilterSample filter_min(FilterSample a, FilterSample b)
FilterSample filter_max(FilterSample a, FilterSample b)
FilterSample filter_median(FilterSample s1, FilterSample s2, FilterSample s3)
FilterLmhResult filter_lmh(FilterSample s1, FilterSample s2, FilterSample s3)
void comp_main(Resources &srt, const uint3 global_id, const uint3 local_id)
#define min(a, b)
Definition sort.cc:38
void init(uint3 global_id, uint3 local_id)
float weight_cache[DOF_FILTER_GROUP_SIZE+2][DOF_FILTER_GROUP_SIZE+2]
float4 color_cache[DOF_FILTER_GROUP_SIZE+2][DOF_FILTER_GROUP_SIZE+2]