19 int closest_index = 0;
20 float closest_squared_distance = 1.0f;
21 for (
int i :
samples.index_range()) {
24 if (squared_dist < closest_squared_distance) {
25 closest_squared_distance = squared_dist;
42 if (closest_index != 0) {
47 for (
int i = 0; i <
samples.size() - 2; i++) {
48 float squared_dist = 0.0;
50 for (
int j = i + 1; j <
samples.size(); j++) {
52 if (_squared_dist > squared_dist) {
53 squared_dist = _squared_dist;
62 std::array<float2, 5>
x5;
63 std::array<float2, 8>
x8;
64 std::array<float2, 11>
x11;
65 std::array<float2, 16>
x16;
66 std::array<float2, 32>
x32;
86 if (
x > width * 0.5f) {
98 const float filter_width = 2.0f;
101 for (
int x = -1;
x <= 1;
x++) {
102 for (
int y = -1;
y <= 1;
y++, i++) {
104 float r =
len_v2(sample_co);
107 r_weight_sum += weight;
108 r_weights[i] = weight;
127 enabled_ = scene_state.
draw_aa;
132 overlay_depth_ps_.
init();
140 taa_accumulation_tx_.
free();
141 sample0_depth_tx_.
free();
147 smaa_mix_factor_ = 1.0f -
clamp_f(scene_state.
sample / 4.0f, 0.0f, 1.0f);
156 taa_accumulation_ps_.
init();
162 taa_accumulation_ps_.
push_constant(
"samplesWeights", weights_, 9);
165 smaa_edge_detect_ps_.
init();
168 smaa_edge_detect_ps_.
bind_texture(
"color_tx", &taa_accumulation_tx_);
169 smaa_edge_detect_ps_.
push_constant(
"viewport_metrics", &smaa_viewport_metrics_, 1);
173 smaa_aa_weight_ps_.
init();
176 smaa_aa_weight_ps_.
bind_texture(
"edges_tx", &smaa_edge_tx_);
177 smaa_aa_weight_ps_.
bind_texture(
"area_tx", smaa_area_tx_);
178 smaa_aa_weight_ps_.
bind_texture(
"search_tx", smaa_search_tx_);
179 smaa_aa_weight_ps_.
push_constant(
"viewport_metrics", &smaa_viewport_metrics_, 1);
183 smaa_resolve_ps_.
init();
186 smaa_resolve_ps_.
bind_texture(
"blend_tx", &smaa_weight_tx_);
187 smaa_resolve_ps_.
bind_texture(
"color_tx", &taa_accumulation_tx_);
188 smaa_resolve_ps_.
push_constant(
"viewport_metrics", &smaa_viewport_metrics_, 1);
189 smaa_resolve_ps_.
push_constant(
"mix_factor", &smaa_mix_factor_, 1);
190 smaa_resolve_ps_.
push_constant(
"taa_accumulated_weight", &weight_accum_, 1);
203 view.sync(viewmat, winmat);
213 sample_offset = taa_samples.
x5[scene_state.
sample];
216 sample_offset = taa_samples.
x8[scene_state.
sample];
219 sample_offset = taa_samples.
x11[scene_state.
sample];
222 sample_offset = taa_samples.
x16[scene_state.
sample];
225 sample_offset = taa_samples.
x32[scene_state.
sample];
236 view.sync(viewmat, winmat);
244 GPUTexture *depth_in_front_tx)
248 overlay_depth_fb_.
bind();
249 manager.
submit(overlay_depth_ps_);
259 if (scene_state.
sample == 0) {
267 sample0_depth_in_front_tx_.
free();
273 if (sample0_depth_in_front_tx_.
is_valid()) {
286 if (scene_state.
sample == 0) {
291 taa_accumulation_fb_.
bind();
293 weight_accum_ += weights_sum_;
306 if (smaa_mix_factor_ > 0.0f) {
308 smaa_edge_fb_.
bind();
312 smaa_weight_fb_.
bind();
316 smaa_resolve_fb_.
bind();
void BLI_jitter_init(float(*jitarr)[2], int num)
MINLINE float clamp_f(float value, float min, float max)
void window_translate_m4(float winmat[4][4], float perspmat[4][4], float x, float y)
MINLINE float len_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
const unsigned char searchTexBytes[]
const unsigned char areaTexBytes[]
#define GPU_ATTACHMENT_TEXTURE(_texture)
#define GPU_ATTACHMENT_NONE
void GPU_texture_copy(GPUTexture *dst, GPUTexture *src)
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_ATTACHMENT
void GPU_texture_filter_mode(GPUTexture *texture, bool use_filter)
void GPU_texture_update(GPUTexture *texture, eGPUDataFormat data_format, const void *data)
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)
void acquire(int2 extent, eGPUTextureFormat format, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL)
bool ensure_2d(eGPUTextureFormat format, int2 extent, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, const float *data=nullptr, int mip_len=1)
const float4x4 & winmat(int view_id=0) const
const float4x4 persmat(int view_id=0) const
const float4x4 & viewmat(int view_id=0) const
static View & default_get()
void bind_texture(const char *name, GPUTexture *texture, GPUSamplerState state=sampler_auto)
void clear_color(float4 color)
void state_set(DRWState state, int clip_plane_count=0)
void state_stencil(uint8_t write_mask, uint8_t reference, uint8_t compare_mask)
void push_constant(const char *name, const float &data)
void draw_procedural(GPUPrimType primitive, uint instance_len, uint vertex_len, uint vertex_first=-1, ResourceHandleRange handle={0}, uint custom_id=0)
void shader_set(GPUShader *shader)
void setup_view(View &view, const SceneState &scene_state)
void init(const SceneState &scene_state)
void draw(const DRWContext *draw_ctx, Manager &manager, View &view, const SceneState &scene_state, SceneResources &resources, GPUTexture *depth_in_front_tx)
void sync(const SceneState &scene_state, SceneResources &resources)
StaticShader smaa_aa_weight
StaticShader taa_accumulation
StaticShader smaa_resolve
StaticShader smaa_edge_detect
static ShaderCache & get()
std::array< float2, 16 > x16
std::array< float2, 8 > x8
std::array< float2, 5 > x5
std::array< float2, 11 > x11
std::array< float2, 32 > x32
@ DRW_STATE_STENCIL_EQUAL
@ DRW_STATE_BLEND_ADD_FULL
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()
VecBase< float, 4 > float4
VecBase< float, 2 > float2
bool is_image_render() const
bool is_scene_render() const
const c_style_mat & ptr() const
TextureRef depth_in_front_tx
Vector< SampleData< T > > samples