Blender V5.3
gpu_shader_widget_base.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2018-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
8
10
11/* TODO(fclem): Share with C code. */
12static constexpr int MAX_PARAM = 12;
13static constexpr int MAX_INSTANCE = 6;
14
15namespace builtin::widget {
16
28
31};
32
33struct [[host_shared]] Widget {
36
37 float radsi;
38 float rads;
40
47
50
53 float shade_dir;
55
56 float tria_type;
57 float _pad0;
58 float _pad1;
59 float _pad2;
60
61 /* We encode alpha check and discard factor together. */
62 bool do_alpha_check() const
63 {
64 return alpha_discard < 0.0f;
65 }
66 float discard_factor() const
67 {
68 return abs(alpha_discard);
69 }
70
71 void do_widget(int vert_id, float2 &pos, VertOut &v_out)
72 {
73 /* Offset to avoid losing pixels (mimics conservative rasterization). */
74 const float2 ofs = float2(0.5f, -0.5f);
75 v_out.line_width = abs(rect.x - recti.x);
76 float2 emboss_ofs = float2(0.0f, -v_out.line_width);
77
78 switch (vert_id) {
79 default:
80 case 0: {
81 pos = rect.xz + emboss_ofs + ofs.yy;
82 break;
83 }
84 case 1: {
85 pos = rect.xw + ofs.yx;
86 break;
87 }
88 case 2: {
89 pos = rect.yz + emboss_ofs + ofs.xy;
90 break;
91 }
92 case 3: {
93 pos = rect.yw + ofs.xx;
94 break;
95 }
96 }
97
98 v_out.uv_interp = pos - rect.xz;
99 v_out.out_rect_size = rect.yw - rect.xz;
101
102 float2 uv = v_out.uv_interp / v_out.out_rect_size;
103 float fac = clamp((shade_dir > 0.0f) ? uv.y : uv.x, 0.0f, 1.0f);
104 /* Note innerColor is premultiplied inside the fragment shader. */
105 if (do_alpha_check()) {
107 v_out.but_co = uv.x;
108 }
109 else {
111 v_out.but_co = -abs(uv.x);
112 }
113
114 /* We need premultiplied color for transparency. */
115 v_out.border_color = color_edge * float4(color_edge.aaa, 1.0f);
116 v_out.emboss_color = color_emboss * float4(color_emboss.aaa, 1.0f);
117 }
118
119 void do_tria(int vert_id, float2 &pos, VertOut &v_out)
120 {
121 int vidx = vert_id % 4;
122 bool tria2 = vert_id > 7;
123
124 pos = float2(0.0f);
125 float size = (tria2) ? -tria2_size : tria1_size;
126 float2 center = (tria2) ? tria2_center : tria1_center;
127
128 float2 arrow_pos[] = {
129 float2(0.0f, 0.6f), float2(0.6f, 0.0f), float2(-0.6f, 0.0f), float2(0.0f, -0.6f)};
130 /* Rotated uv space by 45deg and mirrored. */
131 float2 arrow_uvs[] = {
132 float2(0.0f, 0.85f), float2(0.85f, 0.85f), float2(0.0f, 0.0f), float2(0.0f, 0.85f)};
133
134 float2 point_pos[] = {
135 float2(-1.0f, -1.0f), float2(-1.0f, 1.0f), float2(1.0f, -1.0f), float2(1.0f, 1.0f)};
136 float2 point_uvs[] = {
137 float2(0.0f, 0.0f), float2(0.0f, 1.0f), float2(1.0f, 0.0f), float2(1.0f, 1.0f)};
138
139 /* We reuse the SDF round-box rendering of widget to render the tria shapes.
140 * This means we do clever tricks to position the rectangle the way we want using
141 * the 2 triangles uvs. */
142 if (tria_type == 0.0f) {
143 /* ROUNDBOX_TRIA_NONE */
144 v_out.out_rect_size = v_out.uv_interp = pos = float2(0);
145 v_out.out_round_corners = float4(0.01f);
146 }
147 else if (tria_type == 1.0f) {
148 /* ROUNDBOX_TRIA_ARROWS */
149 pos = arrow_pos[vidx];
150 v_out.uv_interp = arrow_uvs[vidx];
151 v_out.uv_interp -= float2(0.05f, 0.63f); /* Translate */
152 v_out.out_rect_size = float2(0.74f, 0.17f);
153 v_out.out_round_corners = float4(0.08f);
154 }
155 else if (tria_type == 2.0f) {
156 /* ROUNDBOX_TRIA_SCROLL */
157 pos = point_pos[vidx];
158 v_out.uv_interp = point_uvs[vidx];
159 v_out.out_rect_size = float2(1.0f);
160 v_out.out_round_corners = float4(0.5f);
161 }
162 else if (tria_type == 3.0f) {
163 /* ROUNDBOX_TRIA_MENU */
164 pos = tria2 ? float2(0.0f) : arrow_pos[vidx]; /* Solo tria */
165 pos = float2(pos.y, -pos.x); /* Rotate */
166 pos += float2(-0.05f, 0.0f); /* Translate */
167 size *= 0.8f; /* Scale */
168 v_out.uv_interp = arrow_uvs[vidx];
169 v_out.uv_interp -= float2(0.05f, 0.63f); /* Translate */
170 v_out.out_rect_size = float2(0.74f, 0.17f);
171 v_out.out_round_corners = float4(0.01f);
172 }
173 else if (tria_type == 4.0f) {
174 /* ROUNDBOX_TRIA_CHECK */
175 /* A bit more hacky: We use the two triangles joined together to render
176 * both sides of the check-mark with different length. */
177 pos = arrow_pos[min(vidx, 2)]; /* Only keep 1 triangle. */
178 pos.y = tria2 ? -pos.y : pos.y; /* Mirror along X */
179 pos = pos.x * float2(0.0872f, -0.996f) +
180 pos.y * float2(0.996f, 0.0872f); /* Rotate (85deg) */
181 pos += float2(-0.1f, 0.2f); /* Translate */
182 center = tria1_center;
183 size = tria1_size * 1.7f; /* Scale */
184 v_out.uv_interp = arrow_uvs[vidx];
185 v_out.uv_interp -= tria2 ? float2(0.4f, 0.65f) : float2(0.08f, 0.65f); /* Translate */
186 v_out.out_rect_size = float2(0.74f, 0.14f);
187 v_out.out_round_corners = float4(0.01f);
188 }
189 else if (tria_type == 5.0f) {
190 /* ROUNDBOX_TRIA_HOLD_ACTION_ARROW */
191 /* We use a single triangle to cut the round rect in half.
192 * The edge will not be Anti-aliased. */
193 pos = tria2 ? float2(0.0f) : arrow_pos[min(vidx, 2)]; /* Only keep 1 triangle. */
194 pos = pos.x * float2(0.707f, 0.707f) + pos.y * float2(-0.707f, 0.707f); /* Rotate (45deg)
195 */
196 pos += float2(-1.7f, 2.4f); /* Translate (hard-coded, might want to remove). */
197 size *= 0.4f; /* Scale */
198 v_out.uv_interp = arrow_uvs[vidx];
199 v_out.uv_interp -= float2(0.05f, 0.05f); /* Translate */
200 v_out.out_rect_size = float2(0.75f);
201 v_out.out_round_corners = float4(0.01f);
202 }
203 else if (tria_type == 6.0f) {
204 /* ROUNDBOX_TRIA_DASH */
205 pos = point_pos[vidx];
206 v_out.uv_interp = point_uvs[vidx];
207 v_out.uv_interp -= float2(0.2f, 0.45f); /* Translate */
208 v_out.out_rect_size = float2(0.6f, 0.1f);
209 v_out.out_round_corners = float4(0.01f);
210 }
211
212 v_out.uv_interp *= abs(size);
213 v_out.out_rect_size *= abs(size);
214 v_out.out_round_corners *= abs(size);
215
216 pos = pos * size + center;
217
218 v_out.inner_color = color_tria * float4(color_tria.aaa, 1.0f);
219
220 v_out.line_width = 0.0f;
221 v_out.border_color = float4(0.0f);
222 v_out.emboss_color = float4(0.0f);
223
224 v_out.but_co = -2.0f;
225 }
226};
227
232
233struct Resources {
235
238
240 /* WORKAROUND: We cannot use structs with push constants, so we push a float4 array and
241 * reinterpret using a union. */
244
247 {
248 /* Hopefully, all of these move instructions are optimized out. */
249 WidgetRaw raw;
250 if (instanced) [[static_branch]] {
251 for (int i = 0; i < 12; i++) [[unroll]] {
252 raw.data[i] = parameters_inst[index * MAX_PARAM + i];
253 }
254 }
255 else {
256 for (int i = 0; i < 12; i++) [[unroll]] {
257 raw.data[i] = parameters[i];
258 }
259 }
260
261 /* Equivalent of reinterpret_cast. */
263 widget.raw = raw;
264 return widget.data;
265 }
266
268 {
269 float size = checkerColorAndSize.z;
270 float2 phase = mod(frag_co.xy, size * 2.0f);
271
272 if ((phase.x > size && phase.y < size) || (phase.x < size && phase.y > size)) {
273 return float4(checkerColorAndSize.xxx, 1.0f);
274 }
275 return float4(checkerColorAndSize.yyy, 1.0f);
276 }
277};
278
279[[vertex]] void vert([[vertex_id]] const int vert_id,
280 [[instance_id]] const int inst_id,
281 [[resource_table]] Resources &srt,
282 [[out]] VertOut &v_out,
284{
285 Widget widget = srt.get_widget(inst_id);
286
287 bool is_tria = (vert_id > 3);
288 float2 pos;
289 if (is_tria) {
290 widget.do_tria(vert_id, pos, v_out);
291 }
292 else {
293 widget.do_widget(vert_id, pos, v_out);
294 }
295
296 v_out.discard_fac = widget.discard_factor();
297
298 position = srt.ModelViewProjectionMatrix * float4(pos, 0.0f, 1.0f);
299}
300
301struct FragOut {
303};
304
306 [[in]] const VertOut &v_out,
307 [[out]] FragOut &frag_out,
308 [[frag_coord]] const float4 frag_co,
309 [[resource_table]] Resources &srt)
310{
311 if (min(1.0f, -v_out.but_co) > v_out.discard_fac) {
313 }
314
315 float2 uv = v_out.uv_interp;
316
317 bool upper_half = uv.y > v_out.out_rect_size.y * 0.5f;
318 bool right_half = uv.x > v_out.out_rect_size.x * 0.5f;
319 float corner_rad;
320
321 /* Correct aspect ratio for 2D views not using uniform scaling.
322 * uv is already in pixel space so a uniform scale should give us a ratio of 1. */
323 float ratio = (v_out.but_co != -2.0f) ? abs(gpu_dfdy(uv.y) / gpu_dfdx(uv.x)) : 1.0f;
324 float2 uv_sdf = uv;
325 uv_sdf.x *= ratio;
326
327 if (right_half) {
328 uv_sdf.x = v_out.out_rect_size.x * ratio - uv_sdf.x;
329 }
330 if (upper_half) {
331 uv_sdf.y = v_out.out_rect_size.y - uv_sdf.y;
332 corner_rad = right_half ? v_out.out_round_corners.z : v_out.out_round_corners.w;
333 }
334 else {
335 corner_rad = right_half ? v_out.out_round_corners.y : v_out.out_round_corners.x;
336 }
337
338 /* Fade emboss at the border. */
339 float emboss_size = upper_half ? 0.0f : min(1.0f, uv_sdf.x / (corner_rad * ratio));
340
341 /* Signed distance field from the corner (in pixel).
342 * inner_sdf is sharp and outer_sdf is rounded. */
343 uv_sdf -= corner_rad;
344 float inner_sdf = max(0.0f, min(uv_sdf.x, uv_sdf.y));
345 float outer_sdf = -length(min(uv_sdf, 0.0f));
346 float sdf = inner_sdf + outer_sdf + corner_rad;
347
348 /* Clamp line width to be at least 1px wide. This can happen if the projection matrix
349 * has been scaled (i.e: Node editor)... */
350 float line_width = (v_out.line_width > 0.0f) ? max(gpu_fwidth(uv.y), v_out.line_width) : 0.0f;
351
352 constexpr float aa_radius = 0.5f;
353 float3 masks;
354 masks.x = smoothstep(-aa_radius, aa_radius, sdf);
355 masks.y = smoothstep(-aa_radius, aa_radius, sdf - line_width);
356 masks.z = smoothstep(-aa_radius, aa_radius, sdf + line_width * emboss_size);
357
358 /* Compose masks together to avoid having too much alpha. */
359 masks.zx = max(float2(0.0f), masks.zx - masks.xy);
360
361 if (v_out.but_co > 0.0f) {
362 /* Alpha checker widget. */
363 if (v_out.but_co > 0.5f) {
364 float4 checker = srt.do_checkerboard(frag_co.xy);
365 frag_out.color = mix(checker, v_out.inner_color, v_out.inner_color.a);
366 }
367 else {
368 /* Set alpha to 1.0f. */
369 frag_out.color = v_out.inner_color;
370 }
371 frag_out.color.a = 1.0f;
372 }
373 else {
374 /* Pre-multiply here. */
375 frag_out.color = v_out.inner_color * float4(v_out.inner_color.aaa, 1.0f);
376 }
377 frag_out.color *= masks.y;
378 frag_out.color += masks.x * v_out.border_color;
379 frag_out.color += masks.z * v_out.emboss_color;
380
381 /* Un-pre-multiply because the blend equation is already doing the multiplication. */
382 if (frag_out.color.a > 0.0f) {
383 frag_out.color.rgb /= frag_out.color.a;
384 }
385
386 frag_out.color = colorspace.rec709_srgb_to_output_space(frag_out.color);
387}
388
389} // namespace builtin::widget
390
393 builtin::widget::Resources{.instanced = false});
396 builtin::widget::Resources{.instanced = true});
#define host_shared
btScalar max
#define instance_id
#define position
#define index(i)
#define flat
#define frag_coord
#define push_constant
#define compilation_constant
#define resource_table
#define static_branch
#define legacy_info
#define fragment
#define vertex_id
#define unroll
#define no_perspective
#define condition(cond)
#define vertex
#define frag_color(slot)
T smoothstep(T, T, T) RET
void gpu_discard_fragment()
T gpu_dfdx(T) RET
T gpu_fwidth(T) RET
#define abs
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
T gpu_dfdy(T) RET
constexpr T clamp(T, U, U) RET
float length(VecOp< float, D >) RET
PipelineGraphic gpu_shader_2D_widget_base(builtin::widget::vert, builtin::widget::frag, builtin::widget::Resources{.instanced=false})
static constexpr int MAX_PARAM
PipelineGraphic gpu_shader_2D_widget_base_inst(builtin::widget::vert, builtin::widget::frag, builtin::widget::Resources{.instanced=true})
static constexpr int MAX_INSTANCE
MatBase< float, 4, 4 > float4x4
void frag(const ColorSpace &colorspace, const VertOut &v_out, FragOut &frag_out, const float4 frag_co, Resources &srt)
void vert(const int vert_id, const int inst_id, Resources &srt, VertOut &v_out, float4 &position)
#define mix
#define min(a, b)
Definition sort.cc:38
const float4 parameters_inst[MAX_PARAM *MAX_INSTANCE]
void do_tria(int vert_id, float2 &pos, VertOut &v_out)
void do_widget(int vert_id, float2 &pos, VertOut &v_out)
float x
float y
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
float x
Definition sky_math.h:225