1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
#define BIT_RANGE(x) uint((1 << x) - 1)
/* 2 bits for corner */
/* Attention! Not the same order as in UI_interface.h!
* Ordered by drawing order. */
#define BOTTOM_LEFT 0u
#define BOTTOM_RIGHT 1u
#define TOP_RIGHT 2u
#define TOP_LEFT 3u
#define CNR_FLAG_RANGE BIT_RANGE(2)
/* 4bits for corner id */
#define CORNER_VEC_OFS 2u
#define CORNER_VEC_RANGE BIT_RANGE(4)
const vec2 cornervec[9] = vec2[9](vec2(0.0, 1.0),
vec2(0.02, 0.805),
vec2(0.067, 0.617),
vec2(0.169, 0.45),
vec2(0.293, 0.293),
vec2(0.45, 0.169),
vec2(0.617, 0.076),
vec2(0.805, 0.02),
vec2(1.0, 0.0));
/* 4bits for jitter id */
#define JIT_OFS 6u
#define JIT_RANGE BIT_RANGE(4)
const vec2 jit[9] = vec2[9](vec2(0.468813, -0.481430),
vec2(-0.155755, -0.352820),
vec2(0.219306, -0.238501),
vec2(-0.393286, -0.110949),
vec2(-0.024699, 0.013908),
vec2(0.343805, 0.147431),
vec2(-0.272855, 0.269918),
vec2(0.095909, 0.388710),
vec2(0.0, 0.0));
/* 2bits for other flags */
#define INNER_FLAG uint(1 << 10) /* is inner vert */
#define EMBOSS_FLAG uint(1 << 11) /* is emboss vert */
/* 2bits for color */
#define COLOR_OFS 12u
#define COLOR_RANGE BIT_RANGE(2)
#define COLOR_INNER 0u
#define COLOR_EDGE 1u
#define COLOR_EMBOSS 2u
/* 2bits for trias type */
#define TRIA_FLAG uint(1 << 14) /* is tria vert */
#define TRIA_FIRST INNER_FLAG /* is first tria (reuse INNER_FLAG) */
/* We can reuse the CORNER_* bits for tria */
#define TRIA_VEC_RANGE BIT_RANGE(6)
/* Some GPUs have performanse issues with this array being const (Doesn't fit in the registers?).
* To resolve this issue, store the array as a uniform buffer.
* (The array is still stored in the registry, but indexing is done in the uniform buffer.) */
uniform vec2 triavec[43] = vec2[43](
/* ROUNDBOX_TRIA_ARROWS */
vec2(-0.170000, 0.400000),
vec2(-0.050000, 0.520000),
vec2(0.250000, 0.000000),
vec2(0.470000, -0.000000),
vec2(-0.170000, -0.400000),
vec2(-0.050000, -0.520000),
vec2(0.170000, 0.400000),
vec2(0.050000, 0.520000),
vec2(-0.250000, 0.000000),
vec2(-0.470000, -0.000000),
vec2(0.170000, -0.400000),
vec2(0.050000, -0.520000),
/* ROUNDBOX_TRIA_SCROLL - circle tria (triangle strip) */
vec2(0.000000, 1.000000),
vec2(0.382684, 0.923879),
vec2(-0.382683, 0.923880),
vec2(0.707107, 0.707107),
vec2(-0.707107, 0.707107),
vec2(0.923879, 0.382684),
vec2(-0.923879, 0.382684),
vec2(1.000000, 0.000000),
vec2(-1.000000, 0.000000),
vec2(0.923879, -0.382684),
vec2(-0.923879, -0.382684),
vec2(0.707107, -0.707107),
vec2(-0.707107, -0.707107),
vec2(0.382684, -0.923879),
vec2(-0.382683, -0.923880),
vec2(0.000000, -1.000000),
/* ROUNDBOX_TRIA_MENU - menu arrows */
vec2(-0.51, 0.07),
vec2(-0.4, 0.18),
vec2(-0.05, -0.39),
vec2(-0.05, -0.17),
vec2(0.41, 0.07),
vec2(0.3, 0.18),
/* ROUNDBOX_TRIA_CHECK - check mark */
vec2(-0.67000, 0.020000),
vec2(-0.500000, 0.190000),
vec2(-0.130000, -0.520000),
vec2(-0.130000, -0.170000),
vec2(0.720000, 0.430000),
vec2(0.530000, 0.590000),
/* ROUNDBOX_TRIA_HOLD_ACTION_ARROW - hold action arrows */
#define OX (-0.32)
#define OY (0.1)
#define SC (0.35 * 2)
// vec2(-0.5 + SC, 1.0 + OY), vec2( 0.5, 1.0 + OY), vec2( 0.5, 0.0 + OY + SC),
vec2((0.5 - SC) + OX, 1.0 + OY),
vec2(-0.5 + OX, 1.0 + OY),
vec2(-0.5 + OX, SC + OY)
#undef OX
#undef OY
#undef SC
);
uniform mat4 ModelViewProjectionMatrix;
#define MAX_PARAM 11
#ifdef USE_INSTANCE
# define MAX_INSTANCE 6
uniform vec4 parameters[MAX_PARAM * MAX_INSTANCE];
#else
uniform vec4 parameters[MAX_PARAM];
#endif
/* gl_InstanceID is 0 if not drawing instances. */
#define recti parameters[gl_InstanceID * MAX_PARAM + 0]
#define rect parameters[gl_InstanceID * MAX_PARAM + 1]
#define radsi parameters[gl_InstanceID * MAX_PARAM + 2].x
#define rads parameters[gl_InstanceID * MAX_PARAM + 2].y
#define faci parameters[gl_InstanceID * MAX_PARAM + 2].zw
#define roundCorners parameters[gl_InstanceID * MAX_PARAM + 3]
#define colorInner1 parameters[gl_InstanceID * MAX_PARAM + 4]
#define colorInner2 parameters[gl_InstanceID * MAX_PARAM + 5]
#define colorEdge parameters[gl_InstanceID * MAX_PARAM + 6]
#define colorEmboss parameters[gl_InstanceID * MAX_PARAM + 7]
#define colorTria parameters[gl_InstanceID * MAX_PARAM + 8]
#define tria1Center parameters[gl_InstanceID * MAX_PARAM + 9].xy
#define tria2Center parameters[gl_InstanceID * MAX_PARAM + 9].zw
#define tria1Size parameters[gl_InstanceID * MAX_PARAM + 10].x
#define tria2Size parameters[gl_InstanceID * MAX_PARAM + 10].y
#define shadeDir parameters[gl_InstanceID * MAX_PARAM + 10].z
#define alphaDiscard parameters[gl_InstanceID * MAX_PARAM + 10].w
/* We encode alpha check and discard factor together. */
#define doAlphaCheck (alphaDiscard < 0.0)
#define discardFactor abs(alphaDiscard)
in uint vflag;
noperspective out vec4 finalColor;
noperspective out float butCo;
flat out float discardFac;
vec2 do_widget(void)
{
uint cflag = vflag & CNR_FLAG_RANGE;
uint vofs = (vflag >> CORNER_VEC_OFS) & CORNER_VEC_RANGE;
bool is_inner = (vflag & INNER_FLAG) != 0u;
vec2 v = cornervec[vofs];
/* Scale by corner radius */
v *= roundCorners[cflag] * ((is_inner) ? radsi : rads);
/* Flip in the right direction and osition to corner */
vec4 rct = (is_inner) ? recti : rect;
if (cflag == BOTTOM_LEFT) {
v += rct.xz;
}
else if (cflag == BOTTOM_RIGHT) {
v = vec2(-v.y, v.x);
v += rct.yz;
}
else if (cflag == TOP_RIGHT) {
v = -v;
v += rct.yw;
}
else /* (cflag == TOP_LEFT) */ {
v = vec2(v.y, -v.x);
v += rct.xw;
}
vec2 uv = faci * (v - recti.xz);
/* compute uv and color gradient */
uint color_id = (vflag >> COLOR_OFS) & COLOR_RANGE;
if (color_id == COLOR_INNER) {
float fac = clamp((shadeDir > 0.0) ? uv.y : uv.x, 0.0, 1.0);
if (doAlphaCheck) {
finalColor = colorInner1;
butCo = uv.x;
}
else {
finalColor = mix(colorInner2, colorInner1, fac);
butCo = -abs(uv.x);
}
}
else if (color_id == COLOR_EDGE) {
finalColor = colorEdge;
butCo = -abs(uv.x);
}
else /* (color_id == COLOR_EMBOSS) */ {
finalColor = colorEmboss;
butCo = -abs(uv.x);
}
bool is_emboss = (vflag & EMBOSS_FLAG) != 0u;
v.y -= (is_emboss) ? 1.0f : 0.0;
return v;
}
vec2 do_tria()
{
uint vofs = vflag & TRIA_VEC_RANGE;
vec2 v = triavec[vofs];
finalColor = colorTria;
butCo = -1.0;
bool is_tria_first = (vflag & TRIA_FIRST) != 0u;
if (is_tria_first) {
v = v * tria1Size + tria1Center;
}
else {
v = v * tria2Size + tria2Center;
}
return v;
}
void main()
{
discardFac = discardFactor;
bool is_tria = (vflag & TRIA_FLAG) != 0u;
vec2 v = (is_tria) ? do_tria() : do_widget();
/* Antialiasing offset */
v += jit[(vflag >> JIT_OFS) & JIT_RANGE];
gl_Position = ModelViewProjectionMatrix * vec4(v, 0.0, 1.0);
}
|