Blender V5.3
gpu_shader_area_borders.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2018-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
8
9namespace builtin {
10
11namespace area_borders {
12
13struct Resources {
15
18 /* Amount of pixels the border can cover. Scales rounded corner radius. */
20 /* Width of the border relative to the scale. Also affects rounded corner radius. */
23};
24
25struct VertIn {
27};
28
29struct VertOut {
31};
32
33struct FragOut {
35};
36
37[[vertex]] void main_vert([[resource_table]] const Resources &srt,
38 [[in]] const VertIn &v_in,
39 [[out]] VertOut &v_out,
40 [[vertex_id]] const int vert_id,
41 [[position]] float4 &out_pos)
42{
43 int corner_id = (vert_id / srt.cornerLen) % 4;
44 bool inner = all(lessThan(abs(v_in.pos), float2(1.0f)));
45
46 /* Scale the inner part of the border.
47 * Add a sub pixel offset to the outer part to make sure we don't miss a pixel row/column. */
48 float2 final_pos = v_in.pos * ((inner) ? (1.0f - srt.width) : 1.05f);
49
50 v_out.uv = final_pos;
51 /* Rescale to the corner size and position the corner. */
52 if (corner_id == 0) {
53 /* top right */
54 final_pos = (final_pos - float2(1.0f, 1.0f)) * srt.scale + srt.rect.yw;
55 }
56 else if (corner_id == 1) {
57 /* top left */
58 final_pos = (final_pos - float2(-1.0f, 1.0f)) * srt.scale + srt.rect.xw;
59 }
60 else if (corner_id == 2) {
61 /* bottom left */
62 final_pos = (final_pos - float2(-1.0f, -1.0f)) * srt.scale + srt.rect.xz;
63 }
64 else {
65 /* bottom right */
66 final_pos = (final_pos - float2(1.0f, -1.0f)) * srt.scale + srt.rect.yz;
67 }
68
69 out_pos = (srt.ModelViewProjectionMatrix * float4(final_pos, 0.0f, 1.0f));
70}
71
73 [[in]] const VertOut &v_out,
74 [[out]] FragOut &frag_out)
75{
76 /* Should be 1.0f but minimize the AA on the edges. */
77 float dist = (length(v_out.uv) - (0.98f - srt.width)) * srt.scale;
78
79 frag_out.color = srt.color;
80 frag_out.color.a *= smoothstep(-0.09f, 1.09f, dist);
81}
82
83} // namespace area_borders
84
85namespace window_borders {
86
87struct Resources {
89
90 /* The rectangle to round the corners of, as (`xmin`, `xmax`, `ymin`, `ymax`). */
93 /* The radius of each corner in pixels, counter-clockwise from the top right. A radius of zero
94 * leaves that corner square. */
96};
97
98struct VertOut {
99 /* Position relative to the center of the corner's arc, in pixels, mirrored so that the corner of
100 * the rectangle is always in the positive quadrant. */
102 [[flat]] float arc_radius;
103};
104
105struct FragOut {
107};
108
113[[vertex]] void main_vert([[resource_table]] const Resources &srt,
114 [[out]] VertOut &v_out,
115 [[instance_index]] const int inst_index,
116 [[vertex_id]] const int vert_id,
117 [[position]] float4 &out_pos)
118{
119 /* One quad per corner, counter-clockwise from the top right. */
120 int corner_id = inst_index;
121 float radius = srt.radii[corner_id];
122
123 float2 corner_sign = float2((corner_id == 0 || corner_id == 3) ? 1.0f : -1.0f,
124 (corner_id == 0 || corner_id == 1) ? 1.0f : -1.0f);
125 float2 corner_co = float2((corner_sign.x > 0.0f) ? srt.rect.y : srt.rect.x,
126 (corner_sign.y > 0.0f) ? srt.rect.w : srt.rect.z);
127
128 /* Distance inside of the corner, spanning the square bounding the arc, expanded by a pixel. */
129 float2 quad_co = float2(float(vert_id / 2), float(vert_id % 2));
130 float2 inset = float2(-1.0f) + (quad_co * (radius + 1.0f));
131
132 v_out.arc_co = float2(radius) - inset;
133 v_out.arc_radius = radius;
134
135 float2 final_pos = corner_co - (corner_sign * inset);
136 out_pos = srt.ModelViewProjectionMatrix * float4(final_pos, 0.0f, 1.0f);
137}
138
140 [[in]] const VertOut &v_out,
141 [[out]] FragOut &frag_out)
142{
143 /* Distance to the arc, negative inside of the rounded rectangle. */
144 float dist = length(v_out.arc_co) - v_out.arc_radius;
145
146 frag_out.color = srt.color;
147 /* A one pixel wide linear ramp centered on the arc. */
148 frag_out.color.a *= clamp(dist + 0.5f, 0.0f, 1.0f);
149}
150
151} // namespace window_borders
152
153} // namespace builtin
154
PipelineGraphic gpu_shader_2D_rounded_corner_mask(builtin::window_borders::main_vert, builtin::window_borders::main_frag)
PipelineGraphic gpu_shader_2D_area_borders(builtin::area_borders::main_vert, builtin::area_borders::main_frag)
#define instance_index
#define position
#define flat
#define smooth
#define push_constant
#define resource_table
#define attribute(slot)
#define fragment
#define vertex_id
#define vertex
#define frag_color(slot)
T smoothstep(T, T, T) RET
#define abs
bool all(VecOp< bool, D >) RET
VecBase< bool, D > lessThan(VecOp< T, D >, VecOp< T, D >) RET
constexpr T clamp(T, U, U) RET
float length(VecOp< float, D >) RET
MatBase< float, 4, 4 > float4x4
void main_vert(const Resources &srt, const VertIn &v_in, VertOut &v_out, const int vert_id, float4 &out_pos)
void main_frag(const Resources &srt, const VertOut &v_out, FragOut &frag_out)
void main_frag(const Resources &srt, const VertOut &v_out, FragOut &frag_out)
void main_vert(const Resources &srt, VertOut &v_out, const int inst_index, const int vert_id, float4 &out_pos)
float x
float y