Blender V5.3
overlay_antialiasing.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019-2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
12
13#pragma once
14
15#include "gpu_shader_compat.hh"
17#include "overlay_common.bsl.hh"
19
21
22/* Per-pixel line data, unpacked as its perpendicular direction, and distance to origin. */
23struct Line {
25 float dist;
26 float dist_raw;
27
31 static Line zero()
32 {
33 return {.dir = float2(0.0f), .dist = 0.0f, .dist_raw = 0.0f};
34 }
35
36 static Line unpack(float2 data)
37 {
38 /* If the data indicates a blocked pixel, store 0.0f instead of 1.0f,
39 * specifically for the small line fix at the bottom.. */
40 float dist_raw = data.y == 1.0f ? 0.0f : data.y;
41
42 Line line = {.dist_raw = dist_raw};
43 unpack_line_data(data, line.dir, line.dist);
44 return line;
45 }
46
47 bool is_valid() const
48 {
49 /* If 0.0: likely no write was performed to this pixel.
50 * If 1.0: a line overlay wrote to this pixel indicating it does its own AA. */
51 return dist_raw != 0.0f && dist_raw != 1.0f;
52 }
53
58 float coverage(float kernel_size, bool do_smooth_lines) const
59 {
60 if (do_smooth_lines) {
61 return smoothstep(LINE_SMOOTH_END, LINE_SMOOTH_START, abs(dist) - kernel_size);
62 }
63 return step(-0.5f, kernel_size - abs(dist));
64 }
65
71 {
72 bool is_dir_horizontal = abs(dir.x) > abs(dir.y);
73 bool is_ofs_horizontal = offset.x != 0;
74
75 if (!is_valid() || is_ofs_horizontal != is_dir_horizontal) {
76 /* No line. */
77 dist = 1e10f;
78 return;
79 }
80
81 /* Add projection of the line direction onto a unit vector. */
82 dist = dist + dot(float2(offset), -dir);
83 }
84};
85
86struct TexelData {
88 float depth;
90
91 static TexelData zero()
92 {
93 return {.color = float4(0.0f), .depth = 1.0f, .line = Line::zero()};
94 }
95};
96
97struct Resources {
101
104
106 {
107 int2 texel_actual = texel + offset;
108 TexelData data = {
109 .color = texelFetch(color_tx, texel_actual, 0),
110 .depth = texelFetch(depth_tx, texel_actual, 0).r,
111 .line = Line::unpack(texelFetch(line_tx, texel_actual, 0).rg),
112 };
113 if (any(notEqual(offset, int2(0)))) {
114 data.line.offset_to_neighbor(offset);
115 }
116 return data;
117 }
118};
119
124TexelData furthest_texel(TexelData center, TexelData neighbors[4], bool do_background_fetch)
125{
126 if (!do_background_fetch) {
127 return TexelData::zero();
128 }
129
130 TexelData furthest = center;
131 for (int i = 0; i < 4; i++) [[unroll]] {
132 if (neighbors[i].depth > furthest.depth) {
133 furthest = neighbors[i];
134 }
135 }
136 return furthest;
137}
138
143 const TexelData background,
144 float kernel_size,
145 bool do_smooth_lines)
146{
147 if (!target.line.is_valid()) {
148 return;
149 }
150
151 float coverage = target.line.coverage(kernel_size, do_smooth_lines);
152 target.color = mix(background.color, target.color, coverage);
153}
154
160 TexelData neighbor,
161 TexelData background,
162 float kernel_size,
163 bool do_smooth_lines)
164{
165 if (!neighbor.line.is_valid()) {
166 return;
167 }
168
169 float coverage = neighbor.line.coverage(kernel_size, do_smooth_lines);
170 if (coverage == 0.0f) {
171 return;
172 }
173
174 /* Blend neighbor over background based on their respective line coverages. */
175 neighbor.color = mix(background.color, neighbor.color, coverage);
176
177 /* Select over/under for alpha blending, based on relative depth. */
178 bool target_over_neighbor = target.depth < neighbor.depth;
179 float4 over = target_over_neighbor ? target.color : neighbor.color;
180 float4 under = target_over_neighbor ? neighbor.color : target.color;
181
182 if (background.color.a == 0.0) {
183 /* Without background, we do additive alpha on the current frame-buffer. */
184 target.color = over + under * (1.0f - over.a);
185 }
186 else {
187 /* With background, we have to avoid pre-multiplied alpha. */
188 under.a *= (1.0f - over.a);
189 target.color = (over * over.a + under * under.a) / (over.a + under.a);
190 }
191
192 if (!target_over_neighbor) {
193 target.depth = neighbor.depth;
194 }
195}
196
197[[vertex]] void vert_main([[vertex_id]] const int &vert_id, [[position]] float4 &vert)
198{
199 fullscreen_vertex(vert_id, vert);
200}
201
202struct FragOut {
204};
205
207 [[resource_table]] Resources &srt,
208 [[resource_table]] const Uniform &uni,
209 [[out]] FragOut &frag)
210{
211 const int2 texel = int2(frag_coord.xy);
212
213 /* Fetch center pixel. */
214 TexelData center = srt.fetch_texel(texel, int2(0));
215
216 /* Guard; check if no expansion or AA should be applied. */
217 if (!srt.do_smooth_lines && center.line.dist <= 1.0f) {
218 frag.color = center.color;
219 return;
220 }
221
222 /* Fetch a cross of neighboring pixels. */
223 TexelData neighbors[] = {srt.fetch_texel(texel, int2(1, 0)),
224 srt.fetch_texel(texel, int2(-1, 0)),
225 srt.fetch_texel(texel, int2(0, 1)),
226 srt.fetch_texel(texel, int2(0, -1))};
227
228 /* Find the furthest non-line texel among neighbors. */
229 TexelData background = furthest_texel(center, neighbors, srt.do_background_fetch);
230
231 /* Store until after blend, does the center pixel have alpha? */
232 const bool original_center_has_alpha = center.color.a < 1.0f;
233
234 /* Blend center color over background. */
235 const float kernel_size = uni.uniform_buf.sizes.pixel * 0.5f - 0.5f;
236 center_kernel(center, background, kernel_size, srt.do_smooth_lines);
237 /* We don't order fragments; instead blending neighbors alpha-over/alpha-under based on
238 * a tracked depth for each neighbor, using the center pixel as starting reference. */
239 neighbor_kernel(center, neighbors[0], background, kernel_size, srt.do_smooth_lines);
240 neighbor_kernel(center, neighbors[1], background, kernel_size, srt.do_smooth_lines);
241 neighbor_kernel(center, neighbors[2], background, kernel_size, srt.do_smooth_lines);
242 neighbor_kernel(center, neighbors[3], background, kernel_size, srt.do_smooth_lines);
243
244#if 1
245 /* Fix aliasing issue with really dense meshes and 1 pixel sized lines. */
246 if (!original_center_has_alpha && center.line.is_valid() && kernel_size < 0.45f) {
247 float4 lines_raw = float4(neighbors[0].line.dist_raw,
248 neighbors[1].line.dist_raw,
249 neighbors[2].line.dist_raw,
250 neighbors[3].line.dist_raw);
251 float blend = dot(float4(0.25f), step(1e-2f, lines_raw));
252
253 /* Only do blend if there are more than 2 neighbors, to avoid losing too much AA. */
254 blend = clamp(blend * 2.0f - 1.0f, 0.0f, 1.0f);
255 center.color = mix(center.color, center.color / center.color.a, blend);
256 }
257#endif
258
259 frag.color = center.color;
260}
261
263
264} // namespace overlay::antialiasing
#define position
#define frag_coord
#define push_constant
#define resource_table
#define sampler(slot)
#define fragment
#define vertex_id
#define unroll
#define vertex
#define frag_color(slot)
VecBase< bool, D > notEqual(VecOp< T, D >, VecOp< T, D >) RET
T smoothstep(T, T, T) RET
bool any(VecOp< bool, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define abs
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
constexpr T clamp(T, U, U) RET
TEX_TEMPLATE DataVec texelFetch(T, IntCoord, int) RET
SamplerBase< float, 2, false, false, false, true > sampler2DDepth
SamplerBase< float, 2 > sampler2D
void fullscreen_vertex(int vertex_id, float4 &out_position)
TexelData furthest_texel(TexelData center, TexelData neighbors[4], bool do_background_fetch)
void frag_main(const float4 &frag_coord, Resources &srt, const Uniform &uni, FragOut &frag)
PipelineGraphic pipeline(vert_main, frag_main)
void vert_main(const int &vert_id, float4 &vert)
void neighbor_kernel(TexelData &target, TexelData neighbor, TexelData background, float kernel_size, bool do_smooth_lines)
void center_kernel(TexelData &target, const TexelData background, float kernel_size, bool do_smooth_lines)
void unpack_line_data(float2 data, float2 &perp, float &dist)
#define LINE_SMOOTH_START
#define LINE_SMOOTH_END
#define mix
float x
float y
float coverage(float kernel_size, bool do_smooth_lines) const
TexelData fetch_texel(int2 texel, int2 offset)