Blender V5.3
gpu_shader_widget_shadow.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::widget {
10
12 [[attribute(0)]] uint vflag;
13};
14
17 [[smooth]] float innerMask;
18};
19
23
29
31 [[in]] const VertInShadow &v_in,
32 [[out]] VertOutShadow &v_out,
33 [[position]] float4 &out_pos)
34{
35 /* NOTE(Metal): Declaring constant array in function scope to avoid increasing local shader
36 * memory pressure. */
37 const float2 cornervec[36] = {
38 float2(0.0f, 1.0f), float2(0.02f, 0.805f), float2(0.067f, 0.617f),
39 float2(0.169f, 0.45f), float2(0.293f, 0.293f), float2(0.45f, 0.169f),
40 float2(0.617f, 0.076f), float2(0.805f, 0.02f), float2(1.0f, 0.0f),
41 float2(-1.0f, 0.0f), float2(-0.805f, 0.02f), float2(-0.617f, 0.067f),
42 float2(-0.45f, 0.169f), float2(-0.293f, 0.293f), float2(-0.169f, 0.45f),
43 float2(-0.076f, 0.617f), float2(-0.02f, 0.805f), float2(0.0f, 1.0f),
44 float2(0.0f, -1.0f), float2(-0.02f, -0.805f), float2(-0.067f, -0.617f),
45 float2(-0.169f, -0.45f), float2(-0.293f, -0.293f), float2(-0.45f, -0.169f),
46 float2(-0.617f, -0.076f), float2(-0.805f, -0.02f), float2(-1.0f, 0.0f),
47 float2(1.0f, 0.0f), float2(0.805f, -0.02f), float2(0.617f, -0.067f),
48 float2(0.45f, -0.169f), float2(0.293f, -0.293f), float2(0.169f, -0.45f),
49 float2(0.076f, -0.617f), float2(0.02f, -0.805f), float2(0.0f, -1.0f)};
50
51 const float2 center_offset[4] = {
52 float2(1.0f, 1.0f), float2(-1.0f, 1.0f), float2(-1.0f, -1.0f), float2(1.0f, -1.0f)};
53
54 /* Unpack parameters. */
55 float4 recti = srt.parameters[0];
56 float4 rect = srt.parameters[1];
57 float radsi = srt.parameters[2].x;
58 float rads = srt.parameters[2].y;
59 float4 roundCorners = srt.parameters[3];
60
61 /* 2 bits for corner. */
62 /* Attention! Not the same order as in UI_interface.hh!
63 * Ordered by drawing order. */
64 constexpr uint BOTTOM_LEFT = 0u;
65 constexpr uint BOTTOM_RIGHT = 1u;
66 constexpr uint TOP_RIGHT = 2u;
67 // constexpr uint TOP_LEFT = 3u; /* Unused. */
68
69 constexpr uint CNR_FLAG_RANGE = uint((1 << 2) - 1);
70 /* 4bits for corner id */
71 constexpr uint CORNER_VEC_OFS = 2u;
72 constexpr uint CORNER_VEC_RANGE = uint((1 << 4) - 1);
73 /* is inner vert */
74 constexpr uint INNER_FLAG = uint(1 << 10);
75
76 uint cflag = v_in.vflag & CNR_FLAG_RANGE;
77 uint vofs = (v_in.vflag >> CORNER_VEC_OFS) & CORNER_VEC_RANGE;
78
79 bool is_inner = (v_in.vflag & INNER_FLAG) != 0u;
80
81 float shadow_width = rads - radsi;
82 float shadow_width_top = rect.w - recti.w;
83
84 float rad_inner = radsi * roundCorners[cflag];
85 float rad_outer = rad_inner + shadow_width;
86 float radius = (is_inner) ? rad_inner : rad_outer;
87
88 float shadow_offset = (is_inner && (cflag > BOTTOM_RIGHT)) ? (shadow_width - shadow_width_top) :
89 0.0f;
90
91 float2 c = center_offset[cflag];
92 float2 center_outer = rad_outer * c;
93 float2 center = radius * c;
94
95 /* First expand all vertices to the outer shadow border. */
96 float2 v = rad_outer * cornervec[cflag * 9u + vofs];
97
98 /* Now shrink the inner vertices onto the inner rectangle.
99 * At the top corners we keep the vertical offset to distribute a few of the vertices along the
100 * straight part of the rectangle. This allows us to get a better falloff at the top. */
101 if (is_inner && (cflag > BOTTOM_RIGHT) && (v.y < (shadow_offset - rad_outer))) {
102 v.y += shadow_width_top;
103 v.x = 0.0f;
104 }
105 else {
106 v = radius * normalize(v - (center_outer + float2(0.0f, shadow_offset))) + center;
107 }
108
109 /* Position to corner */
110 float4 rct = (is_inner) ? recti : rect;
111 if (cflag == BOTTOM_LEFT) {
112 v += rct.xz;
113 }
114 else if (cflag == BOTTOM_RIGHT) {
115 v += rct.yz;
116 }
117 else if (cflag == TOP_RIGHT) {
118 v += rct.yw;
119 }
120 else /* (cflag == TOP_LEFT) */ {
121 v += rct.xw;
122 }
123
124 float inner_shadow_strength = min((rect.w - v.y) / rad_outer + 0.1f, 1.0f);
125 v_out.shadowFalloff = (is_inner) ? inner_shadow_strength : 0.0f;
126 v_out.innerMask = (is_inner) ? 0.0f : 1.0f;
127
128 out_pos = srt.ModelViewProjectionMatrix * float4(v, 0.0f, 1.0f);
129}
130
131[[fragment]] void shadow_frag([[in]] const VertOutShadow &v_out,
132 [[out]] FragOutShadow &frag_out,
134{
135 /* Manual curve fit of the falloff curve of previous drawing method. */
136 float falloff = v_out.shadowFalloff;
137 float falloff_sqr = falloff * falloff;
138 float shadow_alpha = srt.alpha * (falloff_sqr * 0.722f + falloff * 0.277f);
139 float inner_alpha = smoothstep(0.0f, 0.05f, v_out.innerMask);
140
141 frag_out.color = float4(0.0f, 0.0f, 0.0f, inner_alpha * shadow_alpha);
142}
143
144} // namespace builtin::widget
145
#define position
#define smooth
#define push_constant
#define resource_table
#define attribute(slot)
#define fragment
#define vertex
#define frag_color(slot)
T smoothstep(T, T, T) RET
VecBase< float, D > normalize(VecOp< float, D >) RET
PipelineGraphic gpu_shader_2D_widget_shadow(builtin::widget::shadow_vert, builtin::widget::shadow_frag)
MatBase< float, 4, 4 > float4x4
void shadow_frag(const VertOutShadow &v_out, FragOutShadow &frag_out, WidgetShadow &srt)
void shadow_vert(const WidgetShadow &srt, const VertInShadow &v_in, VertOutShadow &v_out, float4 &out_pos)
#define min(a, b)
Definition sort.cc:38
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225