Blender V5.3
gpencil_antialiasing.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_rand_c.hh"
10#include "BLI_smaa_textures.hh"
11
12#include "DNA_scene_types.h"
13#include "DRW_render.hh"
14
16
18
19void Instance::antialiasing_init()
20{
21 const float2 size_f = this->draw_ctx->viewport_size_get();
22 const int2 size(size_f[0], size_f[1]);
23 const float4 metrics = {1.0f / size[0], 1.0f / size[1], float(size[0]), float(size[1])};
24
25 if (this->simplify_antialias) {
26 /* No AA fallback. */
27 PassSimple &pass = this->smaa_resolve_ps;
28 pass.init();
30 pass.shader_set(ShaderCache::get().antialiasing[2].get());
31 pass.bind_texture("blend_tx", &this->color_tx);
32 pass.bind_texture("color_tx", &this->color_tx);
33 pass.bind_texture("reveal_tx", &this->reveal_tx);
34 pass.push_constant("do_anti_aliasing", false);
35 pass.push_constant("only_alpha", this->draw_wireframe);
36 pass.push_constant("viewport_metrics", metrics);
38 return;
39 }
40
41 if (!this->smaa_search_tx.is_valid()) {
44 gpu::TextureFormat::UNORM_8, int2(SEARCHTEX_WIDTH, SEARCHTEX_HEIGHT), usage);
46
48 gpu::TextureFormat::UNORM_8_8, int2(AREATEX_WIDTH, AREATEX_HEIGHT), usage);
50
53 }
54
55 {
57 this->smaa_edge_tx.acquire_2d(size, gpu::TextureFormat::UNORM_8_8, usage);
58 this->smaa_weight_tx.acquire_2d(size, gpu::TextureFormat::UNORM_8_8_8_8, usage);
59
62 }
63
64 SceneGpencil gpencil_settings = this->scene->grease_pencil_settings;
65 const float luma_weight = this->is_viewport ? gpencil_settings.smaa_threshold :
66 gpencil_settings.smaa_threshold_render;
67
68 {
69 /* Stage 1: Edge detection. */
70 PassSimple &pass = this->smaa_edge_ps;
71 pass.init();
73 pass.shader_set(ShaderCache::get().antialiasing[0].get());
74 pass.bind_texture("color_tx", &this->color_tx);
75 pass.bind_texture("reveal_tx", &this->reveal_tx);
76 pass.push_constant("viewport_metrics", metrics);
77 pass.push_constant("luma_weight", luma_weight);
78 pass.clear_color(float4(0.0f));
80 }
81 {
82 /* Stage 2: Blend Weight/Coord. */
83 PassSimple &pass = this->smaa_weight_ps;
84 pass.init();
86 pass.shader_set(ShaderCache::get().antialiasing[1].get());
87 pass.bind_texture("edges_tx", &this->smaa_edge_tx);
88 pass.bind_texture("area_tx", &this->smaa_area_tx);
89 pass.bind_texture("search_tx", &this->smaa_search_tx);
90 pass.push_constant("viewport_metrics", metrics);
91 pass.clear_color(float4(0.0f));
93 }
94 {
95 /* Stage 3: Resolve. */
96 PassSimple &pass = this->smaa_resolve_ps;
97 pass.init();
99 pass.shader_set(ShaderCache::get().antialiasing[2].get());
100 pass.bind_texture("blend_tx", &this->smaa_weight_tx);
101 pass.bind_texture("color_tx", &this->color_tx);
102 pass.bind_texture("reveal_tx", &this->reveal_tx);
103 pass.push_constant("do_anti_aliasing", true);
104 pass.push_constant("only_alpha", this->draw_wireframe);
105 pass.push_constant("viewport_metrics", metrics);
106 pass.draw_procedural(GPU_PRIM_TRIS, 1, 3);
107 }
108}
109
110void Instance::antialiasing_draw(Manager &manager)
111{
112 if (!this->simplify_antialias) {
114 manager.submit(this->smaa_edge_ps);
115
117 manager.submit(this->smaa_weight_ps);
118 }
119
121 manager.submit(this->smaa_resolve_ps);
122
123 if (this->need_grease_pencil_pass) {
125 GPU_framebuffer_clear(this->gpencil_pass_fb, GPU_COLOR_BIT, {0, 0, 0, 0}, 0, 0);
126 manager.submit(this->smaa_resolve_ps);
127 }
128
129 /* The engine might not support passes, so check if the combined pass actually exists before
130 * rendering grease pencil to it. */
131 const bool combined_pass_exists = DRW_viewport_pass_texture_exists(RE_PASSNAME_COMBINED);
132 if (this->need_combined_pass && combined_pass_exists) {
134 manager.submit(this->smaa_resolve_ps);
135 }
136}
137
138static float erfinv_approx(const float x)
139{
140 /* From: Approximating the `erfinv` function by Mike Giles. */
141 /* To avoid trouble at the limit, clamp input to 1-epsilon. */
142 const float a = math::min(fabsf(x), 0.99999994f);
143 float w = -logf((1.0f - a) * (1.0f + a));
144 float p;
145 if (w < 5.0f) {
146 w = w - 2.5f;
147 p = 2.81022636e-08f;
148 p = p * w + 3.43273939e-07f;
149 p = p * w + -3.5233877e-06f;
150 p = p * w + -4.39150654e-06f;
151 p = p * w + 0.00021858087f;
152 p = p * w + -0.00125372503f;
153 p = p * w + -0.00417768164f;
154 p = p * w + 0.246640727f;
155 p = p * w + 1.50140941f;
156 }
157 else {
158 w = sqrtf(w) - 3.0f;
159 p = -0.000200214257f;
160 p = p * w + 0.000100950558f;
161 p = p * w + 0.00134934322f;
162 p = p * w + -0.00367342844f;
163 p = p * w + 0.00573950773f;
164 p = p * w + -0.0076224613f;
165 p = p * w + 0.00943887047f;
166 p = p * w + 1.00167406f;
167 p = p * w + 2.83297682f;
168 }
169 return p * x;
170}
171
172float2 Instance::antialiasing_sample_get(const int sample_index, const int sample_count)
173{
174 if (sample_count < 2) {
175 return float2(0.0f);
176 }
177
178 double halton[2];
179 {
180 uint primes[2] = {2, 3};
181 double ofs[2] = {0, 0};
182 BLI_halton_2d(primes, ofs, sample_index, halton);
183 }
184 /* Uniform distribution [0..1]. */
185 const float2 rand = float2(halton[0], halton[1]);
186 /* Uniform distribution [-1..1]. */
187 const float2 rand_remap = rand * 2.0f - 1.0f;
188 /* Limit sampling region to avoid outliers. */
189 const float2 rand_adjusted = rand_remap * 0.93f;
190 /* Gaussian distribution [-1..1]. */
191 const float2 offset = float2(erfinv_approx(rand_adjusted.x), erfinv_approx(rand_adjusted.y));
192 /* Gaussian fitted to Blackman-Harris (follows EEVEE). */
193 const float sigma = 0.284f;
194 /* NOTE(fclem): Not sure where this sqrt comes from but is needed to match EEVEE. */
195 return offset * sqrt(sigma);
196}
197
198void Instance::antialiasing_accumulate(Manager &manager, const float alpha)
199{
200 BLI_assert_msg(this->render_color_tx.gpu_texture() != nullptr,
201 "This should only be called during render");
202 const int2 size = this->render_color_tx.size().xy();
203
206 accumulation_tx.ensure_2d(gpu::TextureFormat::GPENCIL_ACCUM_FORMAT, size, usage);
207
208 {
209 PassSimple &pass = this->accumulate_ps;
210 pass.init();
211 pass.state_set(DRW_STATE_WRITE_DEPTH /* There is no depth, but avoid blank state. */);
212 pass.shader_set(ShaderCache::get().accumulation.get());
213 pass.bind_image("src_img", &this->render_color_tx);
214 pass.bind_image("dst_img", &this->accumulation_tx);
215 pass.push_constant("weight_src", alpha);
216 pass.push_constant("weight_dst", 1.0f - alpha);
217 pass.draw_procedural(GPU_PRIM_TRIS, 1, 3);
218 }
219
222 manager.submit(this->accumulate_ps);
223}
224
225} // namespace blender::draw::gpencil
Random number functions.
#define SEARCHTEX_HEIGHT
#define SEARCHTEX_WIDTH
#define AREATEX_HEIGHT
#define AREATEX_WIDTH
#define RE_PASSNAME_COMBINED
#define GPU_ATTACHMENT_TEXTURE(_texture)
#define GPU_ATTACHMENT_NONE
#define BLI_assert_msg(a, msg)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
void ensure(GPUAttachment depth=GPU_ATTACHMENT_NONE, GPUAttachment color1=GPU_ATTACHMENT_NONE, GPUAttachment color2=GPU_ATTACHMENT_NONE, GPUAttachment color3=GPU_ATTACHMENT_NONE, GPUAttachment color4=GPU_ATTACHMENT_NONE, GPUAttachment color5=GPU_ATTACHMENT_NONE, GPUAttachment color6=GPU_ATTACHMENT_NONE, GPUAttachment color7=GPU_ATTACHMENT_NONE, GPUAttachment color8=GPU_ATTACHMENT_NONE)
void submit(PassSimple &pass, View &view)
bool acquire_2d(int2 extent, gpu::TextureFormat format, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, int mip_len=1)
bool ensure_2d(gpu::TextureFormat format, int2 extent, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, const float *data=nullptr, int mip_len=1)
gpu::Texture * gpu_texture()
int3 size(int miplvl=0) const
void draw_procedural(GPUPrimType primitive, uint instance_len, uint vertex_len, uint vertex_first=-1, ResourceIDRange res_id={}, uint custom_id=0)
Definition draw_pass.hh:976
void shader_set(gpu::Shader *shader)
void bind_texture(const char *name, gpu::Texture *texture, GPUSamplerState state=sampler_auto)
void clear_color(float4 color)
void bind_image(const char *name, gpu::Texture *image)
void state_set(DRWState state, int clip_plane_count=0)
void push_constant(const char *name, const float &data)
#define logf(x)
static float erfinv_approx(const float x)
detail::Pass< command::DrawCommandBuf > PassSimple
T min(const T &a, const T &b)
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
void BLI_halton_2d(const unsigned int prime[2], double offset[2], int n, double *r)
Definition rand.cc:239
void GPU_framebuffer_clear(gpu::FrameBuffer *fb, GPUFrameBufferBits buffers, const double4 clear_col, float clear_depth, unsigned int clear_stencil)
void GPU_texture_update(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
const unsigned char areaTexBytes[]
const unsigned char searchTexBytes[]
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_SHADER_WRITE
@ GPU_TEXTURE_USAGE_HOST_READ
@ GPU_TEXTURE_USAGE_ATTACHMENT
void GPU_texture_filter_mode(gpu::Texture *texture, bool use_filter)
sqrt(x)+1/max(0
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_WRITE_DEPTH
Definition draw_state.hh:31
@ DRW_STATE_BLEND_CUSTOM
Definition draw_state.hh:65
bool DRW_viewport_pass_texture_exists(const char *pass_name)
#define fabsf
#define sqrtf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
float2 viewport_size_get() const
struct SceneGpencil grease_pencil_settings
static float2 antialiasing_sample_get(int sample_index, int sample_count)
void antialiasing_accumulate(Manager &manager, float alpha)