Blender V5.3
workbench_effect_antialiasing.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11#include "BLI_jitter_2d.hh"
12#include "BLI_math_geom_c.hh"
13#include "BLI_smaa_textures.hh"
14
16
18 void init_samples(MutableSpan<float2> samples)
19 {
20 BLI_jitter_init(reinterpret_cast<float (*)[2]>(samples.data()), samples.size());
21
22 /* Find closest element to center */
23 int closest_index = 0;
24 float closest_squared_distance = 1.0f;
25 for (int i : samples.index_range()) {
26 const float2 sample = samples[i];
27 const float squared_dist = math::length_squared(sample);
28 if (squared_dist < closest_squared_distance) {
29 closest_squared_distance = squared_dist;
30 closest_index = i;
31 }
32 }
33
34 const float2 closest_sample = samples[closest_index];
35
36 for (float2 &sample : samples) {
37 /* Move jitter samples so that closest sample is in center */
38 sample -= closest_sample;
39 /* Avoid samples outside range (wrap around). */
40 sample = {fmodf(sample.x + 0.5f, 1.0f), fmodf(sample.y + 0.5f, 1.0f)};
41 /* Recenter the distribution[-1..1]. */
42 sample = (sample * 2.0f) - 1.0f;
43 }
44
45 /* Swap center sample to the start of the array */
46 if (closest_index != 0) {
47 std::swap(samples[0], samples[closest_index]);
48 }
49
50 /* Sort list based on farthest distance with previous. */
51 for (int i = 0; i < samples.size() - 2; i++) {
52 float squared_dist = 0.0;
53 int index = i;
54 for (int j = i + 1; j < samples.size(); j++) {
55 const float _squared_dist = math::length_squared(samples[i] - samples[j]);
56 if (_squared_dist > squared_dist) {
57 squared_dist = _squared_dist;
58 index = j;
59 }
60 }
61 std::swap(samples[i + 1], samples[index]);
62 }
63 }
64
65 public:
66 std::array<float2, 5> x5;
67 std::array<float2, 8> x8;
68 std::array<float2, 11> x11;
69 std::array<float2, 16> x16;
70 std::array<float2, 32> x32;
71
73 {
74 init_samples(x5);
75 init_samples(x8);
76 init_samples(x11);
77 init_samples(x16);
78 init_samples(x32);
79 }
80};
81
83{
84 static const TaaSamples taa_samples;
85 return taa_samples;
86}
87
88static float filter_blackman_harris(float x, const float width)
89{
90 if (x > width * 0.5f) {
91 return 0.0f;
92 }
93 x = 2.0f * M_PI * clamp_f((x / width + 0.5f), 0.0f, 1.0f);
94 return 0.35875f - 0.48829f * math::cos(x) + 0.14128f * math::cos(2.0f * x) -
95 0.01168f * math::cos(3.0f * x);
96}
97
98/* Compute weights for the 3x3 neighborhood using a 1.5px filter. */
99static void setup_taa_weights(const float2 offset, float r_weights[9], float &r_weight_sum)
100{
101 /* NOTE: If filter width is bigger than 2.0f, then we need to sample more neighborhood. */
102 const float filter_width = 2.0f;
103 r_weight_sum = 0.0f;
104 int i = 0;
105 for (int x = -1; x <= 1; x++) {
106 for (int y = -1; y <= 1; y++, i++) {
107 float2 sample_co = float2(x, y) - offset;
108 float r = len_v2(sample_co);
109 /* fclem: Is radial distance ok here? */
110 float weight = filter_blackman_harris(r, filter_width);
111 r_weight_sum += weight;
112 r_weights[i] = weight;
113 }
114 }
115}
116
118{
119 smaa_search_tx_.ensure_2d(gpu::TextureFormat::UNORM_8,
123 GPU_texture_filter_mode(smaa_search_tx_, true);
124
125 smaa_area_tx_.ensure_2d(gpu::TextureFormat::UNORM_8_8,
129 GPU_texture_filter_mode(smaa_area_tx_, true);
130}
131
132void AntiAliasingPass::init(const SceneState &scene_state)
133{
134 enabled_ = scene_state.draw_aa;
135}
136
137void AntiAliasingPass::sync(const SceneState &scene_state, SceneResources &resources)
138{
139 overlay_depth_ps_.init();
140 overlay_depth_ps_.state_set(DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS |
142 overlay_depth_ps_.state_stencil(0x00, 0xFF, uint8_t(StencilBits::OBJECT_IN_FRONT));
143 overlay_depth_ps_.shader_set(ShaderCache::get().overlay_depth.get());
144 overlay_depth_ps_.draw_procedural(GPU_PRIM_TRIS, 1, 3);
145
146 if (!enabled_) {
147 taa_accumulation_tx_.free();
148 sample0_depth_tx_.free();
149 return;
150 }
151
152 smaa_viewport_metrics_ = float4(float2(1.0f / float2(scene_state.resolution)),
153 scene_state.resolution);
154 smaa_mix_factor_ = 1.0f - clamp_f(scene_state.sample / 4.0f, 0.0f, 1.0f);
155
156 taa_accumulation_tx_.ensure_2d(gpu::TextureFormat::SFLOAT_16_16_16_16,
157 scene_state.resolution,
159 sample0_depth_tx_.ensure_2d(gpu::TextureFormat::SFLOAT_32_DEPTH_UINT_8,
160 scene_state.resolution,
162
163 taa_accumulation_ps_.init();
164 taa_accumulation_ps_.state_set(scene_state.sample == 0 ?
167 taa_accumulation_ps_.shader_set(ShaderCache::get().taa_accumulation.get());
168 taa_accumulation_ps_.bind_texture("color_buffer", &resources.color_tx);
169 taa_accumulation_ps_.push_constant("samplesWeights", weights_, 9);
170 taa_accumulation_ps_.draw_procedural(GPU_PRIM_TRIS, 1, 3);
171
172 smaa_edge_detect_ps_.init();
173 smaa_edge_detect_ps_.state_set(DRW_STATE_WRITE_COLOR);
174 smaa_edge_detect_ps_.shader_set(ShaderCache::get().smaa_edge_detect.get());
175 smaa_edge_detect_ps_.bind_texture("color_tx", &taa_accumulation_tx_);
176 smaa_edge_detect_ps_.push_constant("viewport_metrics", &smaa_viewport_metrics_, 1);
177 smaa_edge_detect_ps_.clear_color(float4(0.0f));
178 smaa_edge_detect_ps_.draw_procedural(GPU_PRIM_TRIS, 1, 3);
179
180 smaa_aa_weight_ps_.init();
181 smaa_aa_weight_ps_.state_set(DRW_STATE_WRITE_COLOR);
182 smaa_aa_weight_ps_.shader_set(ShaderCache::get().smaa_aa_weight.get());
183 smaa_aa_weight_ps_.bind_texture("edges_tx", &smaa_edge_tx_);
184 smaa_aa_weight_ps_.bind_texture("area_tx", smaa_area_tx_);
185 smaa_aa_weight_ps_.bind_texture("search_tx", smaa_search_tx_);
186 smaa_aa_weight_ps_.push_constant("viewport_metrics", &smaa_viewport_metrics_, 1);
187 smaa_aa_weight_ps_.clear_color(float4(0.0f));
188 smaa_aa_weight_ps_.draw_procedural(GPU_PRIM_TRIS, 1, 3);
189
190 smaa_resolve_ps_.init();
191 smaa_resolve_ps_.state_set(DRW_STATE_WRITE_COLOR);
192 smaa_resolve_ps_.shader_set(ShaderCache::get().smaa_resolve.get());
193 smaa_resolve_ps_.bind_texture("blend_tx", &smaa_weight_tx_);
194 smaa_resolve_ps_.bind_texture("color_tx", &taa_accumulation_tx_);
195 smaa_resolve_ps_.push_constant("viewport_metrics", &smaa_viewport_metrics_, 1);
196 smaa_resolve_ps_.push_constant("mix_factor", &smaa_mix_factor_, 1);
197 smaa_resolve_ps_.push_constant("taa_accumulated_weight", &weight_accum_, 1);
198 smaa_resolve_ps_.clear_color(float4(0.0f));
199 smaa_resolve_ps_.draw_procedural(GPU_PRIM_TRIS, 1, 3);
200}
201
202void AntiAliasingPass::setup_view(View &view, const SceneState &scene_state)
203{
204 const View &default_view = View::default_get();
205 const float4x4 &viewmat = default_view.viewmat();
206 float4x4 winmat = default_view.winmat();
207 float4x4 persmat = default_view.persmat();
208
209 if (!enabled_) {
210 view.sync(viewmat, winmat);
211 return;
212 }
213
214 const TaaSamples &taa_samples = get_taa_samples();
215
216 float2 sample_offset;
217 switch (scene_state.samples_len) {
218 default:
219 case 5:
220 sample_offset = taa_samples.x5[scene_state.sample];
221 break;
222 case 8:
223 sample_offset = taa_samples.x8[scene_state.sample];
224 break;
225 case 11:
226 sample_offset = taa_samples.x11[scene_state.sample];
227 break;
228 case 16:
229 sample_offset = taa_samples.x16[scene_state.sample];
230 break;
231 case 32:
232 sample_offset = taa_samples.x32[scene_state.sample];
233 break;
234 }
235
236 setup_taa_weights(sample_offset, weights_, weights_sum_);
237
238 window_translate_m4(winmat.ptr(),
239 persmat.ptr(),
240 sample_offset.x / scene_state.resolution.x,
241 sample_offset.y / scene_state.resolution.y);
242
243 view.sync(viewmat, winmat);
244}
245
247 Manager &manager,
248 View &view,
249 const SceneState &scene_state,
250 SceneResources &resources,
251 gpu::Texture *depth_in_front_tx)
252{
253 if (resources.depth_in_front_tx.is_valid() && scene_state.sample == 0) {
254 overlay_depth_fb_.ensure(GPU_ATTACHMENT_TEXTURE(resources.depth_tx));
255 overlay_depth_fb_.bind();
256 manager.submit(overlay_depth_ps_);
257 }
258
259 if (!enabled_) {
260 return;
261 }
262
263 const bool last_sample = scene_state.sample + 1 == scene_state.samples_len;
264
265 if (scene_state.samples_len > 1) {
266 if (scene_state.sample == 0) {
267 GPU_texture_copy(sample0_depth_tx_, resources.depth_tx);
268 if (resources.depth_in_front_tx.is_valid()) {
269 sample0_depth_in_front_tx_.ensure_2d(gpu::TextureFormat::SFLOAT_32_DEPTH_UINT_8,
270 scene_state.resolution,
272 GPU_texture_copy(sample0_depth_in_front_tx_, resources.depth_in_front_tx);
273 }
274 else {
275 sample0_depth_in_front_tx_.free();
276 }
277 }
278 else if (!draw_ctx->is_scene_render() || last_sample) {
279 /* Copy back the saved depth buffer for correct overlays. */
280 GPU_texture_copy(resources.depth_tx, sample0_depth_tx_);
281 if (sample0_depth_in_front_tx_.is_valid()) {
282 GPU_texture_copy(depth_in_front_tx, sample0_depth_in_front_tx_);
283 }
284 }
285 }
286
292 const bool taa_finished = scene_state.sample >= scene_state.samples_len;
293 if (!taa_finished) {
294 if (scene_state.sample == 0) {
295 weight_accum_ = 0;
296 }
297 /* Accumulate result to the TAA buffer. */
298 taa_accumulation_fb_.ensure(GPU_ATTACHMENT_NONE, GPU_ATTACHMENT_TEXTURE(taa_accumulation_tx_));
299 taa_accumulation_fb_.bind();
300 manager.submit(taa_accumulation_ps_, view);
301 weight_accum_ += weights_sum_;
302 }
303
305 smaa_weight_tx_.acquire_2d(scene_state.resolution,
306 gpu::TextureFormat::UNORM_8_8_8_8,
308 smaa_edge_tx_.acquire_2d(scene_state.resolution,
309 gpu::TextureFormat::UNORM_8_8,
311
312 if (!draw_ctx->is_image_render() || last_sample || taa_finished) {
313 /* After a certain point SMAA is no longer necessary. */
314 if (smaa_mix_factor_ > 0.0f) {
315 smaa_edge_fb_.ensure(GPU_ATTACHMENT_NONE, GPU_ATTACHMENT_TEXTURE(smaa_edge_tx_));
316 smaa_edge_fb_.bind();
317 manager.submit(smaa_edge_detect_ps_, view);
318
319 smaa_weight_fb_.ensure(GPU_ATTACHMENT_NONE, GPU_ATTACHMENT_TEXTURE(smaa_weight_tx_));
320 smaa_weight_fb_.bind();
321 manager.submit(smaa_aa_weight_ps_, view);
322 }
323 smaa_resolve_fb_.ensure(GPU_ATTACHMENT_NONE, GPU_ATTACHMENT_TEXTURE(resources.color_tx));
324 smaa_resolve_fb_.bind();
325 manager.submit(smaa_resolve_ps_, view);
326 }
327
328 smaa_edge_tx_.release();
329 smaa_weight_tx_.release();
330}
331
332} // namespace blender::workbench
#define M_PI
#define SEARCHTEX_HEIGHT
#define SEARCHTEX_WIDTH
#define AREATEX_HEIGHT
#define AREATEX_WIDTH
#define GPU_ATTACHMENT_TEXTURE(_texture)
#define GPU_ATTACHMENT_NONE
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr T * data() const
Definition BLI_span.hh:545
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
void submit(PassSimple &pass, View &view)
const float4x4 & winmat(int view_id=0) const
Definition draw_view.hh:148
static View & default_get()
Definition draw_view.cc:317
const float4x4 persmat(int view_id=0) const
Definition draw_view.hh:161
const float4x4 & viewmat(int view_id=0) const
Definition draw_view.hh:136
void sync(const float4x4 &view_mat, const float4x4 &win_mat, int view_id=0)
Definition draw_view.cc:27
void setup_view(View &view, const SceneState &scene_state)
void draw(const DRWContext *draw_ctx, Manager &manager, View &view, const SceneState &scene_state, SceneResources &resources, gpu::Texture *depth_in_front_tx)
void sync(const SceneState &scene_state, SceneResources &resources)
#define fmodf(x, y)
T length_squared(const VecBase< T, Size > &a)
T cos(const AngleRadianBase< T > &a)
static float filter_blackman_harris(float x, const float width)
static void setup_taa_weights(const float2 offset, float r_weights[9], float &r_weight_sum)
static const TaaSamples & get_taa_samples()
void GPU_texture_update(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
const unsigned char areaTexBytes[]
void GPU_texture_copy(gpu::Texture *dst, gpu::Texture *src)
void window_translate_m4(float winmat[4][4], float perspmat[4][4], float x, float y)
const unsigned char searchTexBytes[]
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
VecBase< float, 2 > float2
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_ATTACHMENT
unsigned char uint8_t
Definition BKE_text.h:79
void GPU_texture_filter_mode(gpu::Texture *texture, bool use_filter)
void BLI_jitter_init(float(*jitarr)[2], int num)
Definition jitter_2d.cc:129
@ DRW_STATE_BLEND_ADD_FULL
Definition draw_state.hh:55
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_WRITE_DEPTH
Definition draw_state.hh:31
@ DRW_STATE_DEPTH_ALWAYS
Definition draw_state.hh:38
@ DRW_STATE_STENCIL_EQUAL
Definition draw_state.hh:49
MINLINE float len_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
MINLINE float clamp_f(float value, float min, float max)
bool is_image_render() const
bool is_scene_render() const
const c_style_mat & ptr() const