Blender V5.3
gpu_shader_sequencer_widget.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
8
9namespace sequencer {
10
11struct FragOut {
13};
14
15/* Signed distance to rounded box, centered at origin.
16 * Reference: https://iquilezles.org/articles/distfunctions2d/ */
17float sdf_rounded_box(float2 pos, float2 size, float radius)
18{
19 float2 q = abs(pos) - size + radius;
20 return min(max(q.x, q.y), 0.0f) + length(max(q, 0.0f)) - radius;
21}
22
23void strip_box(float left,
24 float right,
25 float bottom,
26 float top,
27 float radius,
28 float2 pos,
29 float2 &r_pos1,
30 float2 &r_pos2,
31 float2 &r_size,
32 float2 &r_center,
33 float2 &r_pos,
34 float &r_radius)
35{
36 /* Snap to pixel grid coordinates, so that outline/border is non-fractional
37 * pixel sizes. */
38 r_pos1 = round(float2(left, bottom));
39 r_pos2 = round(float2(right, top));
40 /* Make sure strip is at least 1px wide. */
41 r_pos2.x = max(r_pos2.x, r_pos1.x + 1.0f);
42 r_size = (r_pos2 - r_pos1) * 0.5f;
43 r_center = (r_pos1 + r_pos2) * 0.5f;
44 r_pos = round(pos);
45
46 r_radius = radius;
47 if (r_radius > r_size.x) {
48 r_radius = 0.0f;
49 }
50}
51
52namespace strip {
53
54float3 color_shade(float3 rgb, float shade)
55{
56 rgb += float3(shade / 255.0f);
57 rgb = clamp(rgb, float3(0.0f), float3(1.0f));
58 return rgb;
59}
60
61/* Blends in a straight alpha `color` into premultiplied `cur` and returns premultiplied result.
62 */
64{
65 float t = color.a;
66 return cur * (1.0f - t) + float4(color.rgb * t, t);
67}
68
69struct Resources {
71 [[uniform(0)]] SeqStripDrawData (&strip_data)[GPU_SEQ_STRIP_DRAW_DATA_LEN];
72 [[uniform(1)]] SeqContextDrawData &context_data;
73
74 /* Given signed distance `d` to a shape and current premultiplied color `cur`, blends
75 * in an outline at distance between `edge1` and `edge2`.
76 * Outline color `outline_color` is in straight alpha. */
77 float4 add_outline(float d, float edge1, float edge2, float4 cur, float4 outline_color) const
78 {
79 d -= 0.5f;
80 edge1 *= context_data.pixelsize;
81 edge2 *= context_data.pixelsize;
82 float f = abs(d + (edge1 + edge2) * 0.5f) - abs(edge2 - edge1) * 0.5f + 0.5f;
83 float a = clamp(1.0f - f, 0.0f, 1.0f);
84 outline_color.a *= a;
85 return blend_color(cur, outline_color);
86 }
87};
88
93
94[[vertex]] void main_vert([[vertex_id]] const int vert_id,
95 [[instance_id]] const int inst_id,
97 [[out]] VertOut &v_out,
98 [[position]] float4 &out_pos)
99{
100 v_out.strip_id = uint(inst_id);
101 SeqStripDrawData strip = srt.strip_data[inst_id];
102 float4 rect = float4(strip.left_handle, strip.bottom, strip.right_handle, strip.top);
103 /* Expand by 1px to fit pixel grid rounding. */
104 float2 expand = float2(1.0f, 1.0f);
105 rect.xy -= expand;
106 rect.zw += expand;
107
108 float2 co;
109 if (vert_id == 0) {
110 co = rect.xw;
111 }
112 else if (vert_id == 1) {
113 co = rect.xy;
114 }
115 else if (vert_id == 2) {
116 co = rect.zw;
117 }
118 else {
119 co = rect.zy;
120 }
121
122 v_out.co_interp = co;
123 out_pos = srt.ModelViewProjectionMatrix * float4(co, 0.0f, 1.0f);
124}
125
127 [[frag_coord]] const float4 frag_co,
128 [[in]] const VertOut &v_out,
129 [[out]] FragOut &frag_out)
130{
131 float2 co = v_out.co_interp;
132
133 SeqStripDrawData strip = srt.strip_data[v_out.strip_id];
134
135 float2 pos1, pos2, size, center, pos;
136 float radius = 0.0f;
137 strip_box(strip.left_handle,
138 strip.right_handle,
139 strip.bottom,
140 strip.top,
141 srt.context_data.round_radius,
142 co,
143 pos1,
144 pos2,
145 size,
146 center,
147 pos,
148 radius);
149
150 bool border = (strip.flags & GPU_SEQ_FLAG_BORDER) != 0;
151 bool selected = (strip.flags & GPU_SEQ_FLAG_SELECTED) != 0;
152 float outline_width = selected ? 2.0f : 1.0f;
153
154 /* Distance to whole strip shape. */
155 float sdf = sdf_rounded_box(pos - center, size, radius);
156
157 /* Distance to inner part when handles are taken into account. */
158 float sdf_inner = sdf;
159 if ((strip.flags & GPU_SEQ_FLAG_ANY_HANDLE) != 0) {
160 float handle_width = strip.handle_width;
161 /* Take left/right handle from horizontal sides. */
162 if ((strip.flags & GPU_SEQ_FLAG_SELECTED_LH) != 0) {
163 pos1.x += handle_width;
164 }
165 if ((strip.flags & GPU_SEQ_FLAG_SELECTED_RH) != 0) {
166 pos2.x -= handle_width;
167 }
168 /* Reduce vertical size by outline width. */
169 pos1.y += srt.context_data.pixelsize * outline_width;
170 pos2.y -= srt.context_data.pixelsize * outline_width;
171
172 size = (pos2 - pos1) * 0.5f;
173 center = (pos1 + pos2) * 0.5f;
174 sdf_inner = sdf_rounded_box(pos - center, size, radius);
175 }
176
177 float4 col = float4(0.0f);
178
179 /* Background. */
180 if ((strip.flags & GPU_SEQ_FLAG_BACKGROUND) != 0) {
181 col = unpackUnorm4x8(strip.col_background);
182 /* Darker background for multi-image strip hold still regions. */
183 if ((strip.flags & GPU_SEQ_FLAG_SINGLE_IMAGE) == 0) {
184 if (co.x < strip.content_start || co.x > strip.content_end) {
185 col.rgb = color_shade(col.rgb, -35.0f);
186 }
187 }
188 }
189
190 /* Thumbnails background. */
191 if ((strip.flags & GPU_SEQ_FLAG_THUMBNAILS_BACKGROUND) != 0) {
192 if (co.y < strip.strip_content_top) {
193 if ((strip.flags & GPU_SEQ_FLAG_SINGLE_IMAGE) != 0 ||
194 (co.x >= strip.content_start && co.x <= strip.content_end))
195 {
196 /* Re use the color band color here. */
197 col.rgb = unpackUnorm4x8(strip.col_color_band).rgb;
198 }
199 }
200 }
201 /* Color band. */
202 else if ((strip.flags & GPU_SEQ_FLAG_COLOR_BAND) != 0) {
203 if (co.y < strip.strip_content_top) {
204 col.rgb = unpackUnorm4x8(strip.col_color_band).rgb;
205 /* Darker line to better separate the color band. */
206 if (co.y > strip.strip_content_top - 1.0f) {
207 col.rgb = color_shade(col.rgb, -20.0f);
208 }
209 }
210 }
211
212 /* Transition. */
213 if ((strip.flags & GPU_SEQ_FLAG_TRANSITION) != 0) {
214 if (co.x >= strip.content_start && co.x <= strip.content_end && co.y < strip.strip_content_top)
215 {
216 float diag_y = strip.strip_content_top - (strip.strip_content_top - strip.bottom) *
217 (co.x - strip.content_start) /
218 (strip.content_end - strip.content_start);
219 uint transition_color = co.y <= diag_y ? strip.col_transition_in : strip.col_transition_out;
220 col.rgb = unpackUnorm4x8(transition_color).rgb;
221 }
222 }
223
224 /* Previous parts were all assigning color (not blending it),
225 * make sure from now on alpha is premultiplied. */
226 col.rgb *= col.a;
227
228 /* Missing media. */
229 if ((strip.flags & GPU_SEQ_FLAG_MISSING_TITLE) != 0) {
230 if (co.y > strip.strip_content_top) {
231 col = blend_color(col, float4(112.0f / 255.0f, 0.0f, 0.0f, 230.0f / 255.0f));
232 }
233 }
234 if ((strip.flags & GPU_SEQ_FLAG_MISSING_CONTENT) != 0) {
235 if (co.y <= strip.strip_content_top) {
236 col = blend_color(col, float4(64.0f / 255.0f, 0.0f, 0.0f, 230.0f / 255.0f));
237 }
238 }
239
240 /* Locked. */
241 if ((strip.flags & GPU_SEQ_FLAG_LOCKED) != 0) {
242 if (co.y <= strip.strip_content_top) {
243 float phase = mod(frag_co.x + frag_co.y, 12.0f);
244 if (phase >= 8.0f) {
245 col = blend_color(col, float4(0.0f, 0.0f, 0.0f, 0.25f));
246 }
247 }
248 }
249
250 /* Highlight. */
251 if ((strip.flags & GPU_SEQ_FLAG_HIGHLIGHT) != 0) {
252 col = blend_color(col, float4(1.0f, 1.0f, 1.0f, 48.0f / 255.0f));
253 }
254
255 /* Handles. */
256 float4 col_outline = unpackUnorm4x8(strip.col_outline);
257 if ((strip.flags & GPU_SEQ_FLAG_ANY_HANDLE) != 0) {
258 bool left_side = pos.x < center.x;
259 uint handle_flag = left_side ? GPU_SEQ_FLAG_SELECTED_LH : GPU_SEQ_FLAG_SELECTED_RH;
260 bool selected_handle = (strip.flags & handle_flag) != 0;
261 /* Blend in handle color in between strip shape and inner handle shape. */
262 if (sdf <= 0.0f && sdf_inner >= 0.0f) {
263 float4 hcol = selected_handle ? col_outline : float4(0, 0, 0, 0.2f);
264 hcol.a *= clamp(sdf_inner, 0.0f, 1.0f);
265 col = blend_color(col, hcol);
266 }
267 /* For an unselected handle, no longer take it into account
268 * for the "inner" distance. */
269 if (!selected_handle) {
270 sdf_inner = sdf;
271 }
272 }
273
274 /* Outside of strip rounded rectangle? */
275 if (sdf > 0.0f) {
276 col = float4(0.0f);
277 }
278
279 /* Outline / border. */
280 if (border) {
281
282 if (selected) {
283 /* Selection highlight + darker inset line. */
284 col = srt.add_outline(sdf, 1.0f, 3.0f, col, col_outline);
285 /* Inset line should be inside regular border or inside the handles. */
286 float d = max(sdf_inner - 3.0f * srt.context_data.pixelsize, sdf);
287 col = srt.add_outline(d, 3.0f, 4.0f, col, float4(0, 0, 0, 0.33f));
288 }
289
290 /* Active, but not selected strips get a thin inner line. */
291 bool active_strip = (strip.flags & GPU_SEQ_FLAG_ACTIVE) != 0;
292 if (active_strip && !selected) {
293 col = srt.add_outline(sdf, 1.0f, 2.0f, col, col_outline);
294 }
295
296 /* 2px outline for all overlapping strips. */
297 bool overlaps = (strip.flags & GPU_SEQ_FLAG_OVERLAP) != 0;
298 bool clamped = (strip.flags & GPU_SEQ_FLAG_CLAMPED) != 0;
299 if (overlaps || clamped) {
300 col = srt.add_outline(sdf, 1.0f, 3.0f, col, col_outline);
301 }
302
303 /* Outer 1px outline for all strips. */
304 col = srt.add_outline(sdf, 0.0f, 1.0f, col, unpackUnorm4x8(srt.context_data.col_back));
305 }
306
307 frag_out.color = col;
308}
309} // namespace strip
310
311namespace thumbnail {
312
318
319struct Resources {
321 [[uniform(0)]] SeqStripThumbData (&thumb_data)[GPU_SEQ_STRIP_DRAW_DATA_LEN];
322 [[uniform(1)]] SeqContextDrawData &context_data;
324};
325
326[[vertex]] void main_vert([[vertex_id]] const int vert_id,
327 [[instance_id]] const int inst_id,
328 [[resource_table]] Resources &srt,
329 [[out]] VertOut &v_out,
330 [[position]] float4 &out_pos)
331{
332 v_out.thumb_id = uint(inst_id);
333 SeqStripThumbData thumb = srt.thumb_data[inst_id];
334 float4 coords = float4(thumb.x1, thumb.y1, thumb.x2, thumb.y2);
335 float4 uvs = float4(thumb.u1, thumb.v1, thumb.u2, thumb.v2);
336
337 float2 co;
338 float2 uv;
339 if (vert_id == 0) {
340 co = coords.xw;
341 uv = uvs.xw;
342 }
343 else if (vert_id == 1) {
344 co = coords.xy;
345 uv = uvs.xy;
346 }
347 else if (vert_id == 2) {
348 co = coords.zw;
349 uv = uvs.zw;
350 }
351 else {
352 co = coords.zy;
353 uv = uvs.zy;
354 }
355
356 v_out.pos_interp = co;
357 v_out.uv = uv;
358 out_pos = srt.ModelViewProjectionMatrix * float4(co, 0.0f, 1.0f);
359}
360
362 [[in]] const VertOut &v_out,
363 [[out]] FragOut &frag_out)
364{
365 SeqStripThumbData thumb = srt.thumb_data[v_out.thumb_id];
366 float2 pos1, pos2, size, center, pos;
367 float radius = 0.0f;
368 strip_box(thumb.left,
369 thumb.right,
370 thumb.bottom,
371 thumb.top,
372 srt.context_data.round_radius,
373 v_out.pos_interp,
374 pos1,
375 pos2,
376 size,
377 center,
378 pos,
379 radius);
380
381 /* Sample thumbnail texture, modulate with color. */
382 float4 col = texture(srt.image, v_out.uv) * thumb.tint_color;
383
384 /* Outside of strip rounded rectangle? */
385 float sdf = sdf_rounded_box(pos - center, size, radius);
386 if (sdf > 0.0f) {
387 col = float4(0.0f);
388 }
389
390 frag_out.color = col;
391}
392} // namespace thumbnail
393} // namespace sequencer
394
@ GPU_SEQ_FLAG_HIGHLIGHT
@ GPU_SEQ_FLAG_BACKGROUND
@ GPU_SEQ_FLAG_SINGLE_IMAGE
@ GPU_SEQ_FLAG_LOCKED
@ GPU_SEQ_FLAG_MISSING_CONTENT
@ GPU_SEQ_FLAG_SELECTED
@ GPU_SEQ_FLAG_CLAMPED
@ GPU_SEQ_FLAG_ANY_HANDLE
@ GPU_SEQ_FLAG_BORDER
@ GPU_SEQ_FLAG_THUMBNAILS_BACKGROUND
@ GPU_SEQ_FLAG_COLOR_BAND
@ GPU_SEQ_FLAG_TRANSITION
@ GPU_SEQ_FLAG_SELECTED_LH
@ GPU_SEQ_FLAG_SELECTED_RH
@ GPU_SEQ_FLAG_OVERLAP
@ GPU_SEQ_FLAG_ACTIVE
@ GPU_SEQ_FLAG_MISSING_TITLE
btScalar max
#define instance_id
#define uniform(slot)
#define position
#define flat
#define frag_coord
#define push_constant
#define resource_table
#define sampler(slot)
#define fragment
#define vertex_id
#define no_perspective
#define vertex
#define frag_color(slot)
#define round
float4 unpackUnorm4x8(uint) RET
#define abs
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
constexpr T clamp(T, U, U) RET
float length(VecOp< float, D >) RET
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
SamplerBase< float, 2 > sampler2D
PipelineGraphic gpu_shader_sequencer_strips(sequencer::strip::main_vert, sequencer::strip::main_frag)
PipelineGraphic gpu_shader_sequencer_thumbs(sequencer::thumbnail::main_vert, sequencer::thumbnail::main_frag)
MatBase< float, 4, 4 > float4x4
void main_frag(const Resources &srt, const float4 frag_co, const VertOut &v_out, FragOut &frag_out)
void main_vert(const int vert_id, const int inst_id, Resources &srt, VertOut &v_out, float4 &out_pos)
float3 color_shade(float3 rgb, float shade)
float4 blend_color(float4 cur, float4 color)
void main_vert(const int vert_id, const int inst_id, Resources &srt, VertOut &v_out, float4 &out_pos)
void main_frag(const Resources &srt, const VertOut &v_out, FragOut &frag_out)
float sdf_rounded_box(float2 pos, float2 size, float radius)
void strip_box(float left, float right, float bottom, float top, float radius, float2 pos, float2 &r_pos1, float2 &r_pos2, float2 &r_size, float2 &r_center, float2 &r_pos, float &r_radius)
#define min(a, b)
Definition sort.cc:38
float x
float y
float4 add_outline(float d, float edge1, float edge2, float4 cur, float4 outline_color) const
SeqStripDrawData(& strip_data)[GPU_SEQ_STRIP_DRAW_DATA_LEN]
SeqStripThumbData(& thumb_data)[GPU_SEQ_STRIP_DRAW_DATA_LEN]