Blender V5.3
gpu_shader_keyframe_shape.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2017-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
9
11
12struct VertIn {
16 [[attribute(3)]] float size;
17 [[attribute(4)]] uint flags;
18};
19
27
28struct FragOut {
30};
31
37
38#define line_falloff 1.0f
39#define circle_scale sqrt(2.0f / 3.1416f)
40#define square_scale sqrt(0.5f)
41#define diagonal_scale sqrt(0.5f)
42
43#define minmax_bias 0.7f
44#define minmax_scale sqrt(1.0f / (1.0f + 1.0f / minmax_bias))
45
47{
48 return float2(max(0.0f, width - line_falloff), width);
49}
50
51[[vertex]] void vert_main([[resource_table]] const Resources &srt,
52 [[in]] const VertIn &v_in,
53 [[out]] VertOut &v_out,
54 [[position]] float4 &out_pos,
55 [[point_size]] float &out_pt_size)
56{
57 out_pos = srt.ModelViewProjectionMatrix * float4(v_in.pos, 0.0f, 1.0f);
58
59 /* Align to pixel grid if the viewport size is known. */
60 if (srt.ViewportSize.x > 0) {
61 float2 scale = srt.ViewportSize * 0.5f;
62 float2 px_pos = (out_pos.xy + 1) * scale;
63 float2 adj_pos = round(px_pos - 0.5f) + 0.5f;
64 out_pos.xy = adj_pos / scale - 1;
65 }
66
67 /* Pass through parameters. */
68 v_out.finalColor = v_in.color;
69 v_out.finalOutlineColor = v_in.outlineColor;
70 v_out.finalFlags = v_in.flags;
71
72 if (!flag_test(v_in.flags,
75 {
76 v_out.finalFlags |= GPU_KEYFRAME_SHAPE_DIAMOND;
77 }
78
79 /* Size-dependent line thickness. */
80 float half_width = (0.06f + (v_in.size - 10) * 0.04f);
81 float line_width = half_width + line_falloff;
82
83 /* Outline thresholds. */
84 v_out.thresholds.xy = line_thresholds(line_width * srt.outline_scale);
85
86 /* Inner dot thresholds. */
87 v_out.thresholds.zw = line_thresholds(line_width * 1.6f);
88
89 /* Extend the primitive size by half line width on either side; odd for symmetry. */
90 float ext_radius = round(0.5f * v_in.size) + v_out.thresholds.x;
91
92 out_pt_size = ceil(ext_radius + v_out.thresholds.y) * 2 + 1;
93
94 /* Diamond radius. */
95 v_out.radii[0] = ext_radius * diagonal_scale;
96 /* Circle radius. */
97 v_out.radii[1] = ext_radius * circle_scale;
98 /* Square radius. */
99 v_out.radii[2] = round(ext_radius * square_scale);
100 /* Min/max cutout offset. */
101 v_out.radii[3] = -line_falloff;
102 /* Convert to PointCoord units. */
103 v_out.radii /= out_pt_size;
104 v_out.thresholds /= out_pt_size;
105}
106
107[[fragment]] void frag_main([[point_coord]] const float2 pt_coord,
108 [[in]] const VertOut &v_out,
109 [[out]] FragOut &frag_out)
110{
111 float2 pos = pt_coord - float2(0.5f);
112 float2 absPos = abs(pos);
113 float radius = (absPos.x + absPos.y) * diagonal_scale;
114
115 float outline_dist = -1.0f;
116
117 /* Diamond outline */
118 if (flag_test(v_out.finalFlags, GPU_KEYFRAME_SHAPE_DIAMOND)) {
119 outline_dist = max(outline_dist, radius - v_out.radii[0]);
120 }
121
122 /* Circle outline */
123 if (flag_test(v_out.finalFlags, GPU_KEYFRAME_SHAPE_CIRCLE)) {
124 radius = length(absPos);
125
126 outline_dist = max(outline_dist, radius - v_out.radii[1]);
127 }
128
129 /* Top & Bottom clamp */
130 if (flag_test(v_out.finalFlags, GPU_KEYFRAME_SHAPE_CLIPPED_VERTICAL)) {
131 outline_dist = max(outline_dist, absPos.y - v_out.radii[2]);
132 }
133
134 /* Left & Right clamp */
135 if (flag_test(v_out.finalFlags, GPU_KEYFRAME_SHAPE_CLIPPED_HORIZONTAL)) {
136 outline_dist = max(outline_dist, absPos.x - v_out.radii[2]);
137 }
138
139 float alpha = 1 - smoothstep(v_out.thresholds[0], v_out.thresholds[1], abs(outline_dist));
140
141 /* Inside the outline. */
142 if (outline_dist < 0) {
143 /* Middle dot */
144 if (flag_test(v_out.finalFlags, GPU_KEYFRAME_SHAPE_INNER_DOT)) {
145 alpha = max(alpha, 1 - smoothstep(v_out.thresholds[2], v_out.thresholds[3], length(absPos)));
146 }
147
148 /* Up and down arrow-like shading. */
149 if (flag_test(v_out.finalFlags,
151 {
152 float ypos = -1.0f;
153
154 /* Up arrow (maximum) */
155 if (flag_test(v_out.finalFlags, GPU_KEYFRAME_SHAPE_ARROW_END_MAX)) {
156 ypos = max(ypos, pos.y);
157 }
158 /* Down arrow (minimum) */
159 if (flag_test(v_out.finalFlags, GPU_KEYFRAME_SHAPE_ARROW_END_MIN)) {
160 ypos = max(ypos, -pos.y);
161 }
162
163 /* Arrow shape threshold. */
164 float minmax_dist = (ypos - v_out.radii[3]) - absPos.x * minmax_bias;
165 float minmax_step = smoothstep(
166 v_out.thresholds[0], v_out.thresholds[1], minmax_dist * minmax_scale);
167
168 /* Reduced alpha for uncertain extremes. */
169 float minmax_alpha = flag_test(v_out.finalFlags, GPU_KEYFRAME_SHAPE_ARROW_END_MIXED) ?
170 0.55f :
171 0.85f;
172
173 alpha = max(alpha, minmax_step * minmax_alpha);
174 }
175
176 frag_out.color = mix(v_out.finalColor, v_out.finalOutlineColor, alpha);
177 }
178 /* Outside the outline. */
179 else {
180 frag_out.color = float4(v_out.finalOutlineColor.rgb, v_out.finalOutlineColor.a * alpha);
181 }
182}
183} // namespace builtin::keyframe
184
@ GPU_KEYFRAME_SHAPE_INNER_DOT
@ GPU_KEYFRAME_SHAPE_CIRCLE
@ GPU_KEYFRAME_SHAPE_CLIPPED_HORIZONTAL
@ GPU_KEYFRAME_SHAPE_ARROW_END_MIXED
@ GPU_KEYFRAME_SHAPE_DIAMOND
@ GPU_KEYFRAME_SHAPE_ARROW_END_MAX
@ GPU_KEYFRAME_SHAPE_ARROW_END_MIN
@ GPU_KEYFRAME_SHAPE_CLIPPED_VERTICAL
btScalar max
#define point_size
#define position
#define flat
#define push_constant
#define resource_table
#define attribute(slot)
#define fragment
#define vertex
#define frag_color(slot)
#define point_coord
T smoothstep(T, T, T) RET
#define round
#define abs
#define ceil
float length(VecOp< float, D >) RET
PipelineGraphic gpu_shader_keyframe_shape(builtin::keyframe::vert_main, builtin::keyframe::frag_main)
#define diagonal_scale
float3x3 scale(float3x3 mat, float2 scale)
bool flag_test(uint flag, uint val)
MatBase< float, 4, 4 > float4x4
float2 line_thresholds(float width)
void vert_main(const Resources &srt, const VertIn &v_in, VertOut &v_out, float4 &out_pos, float &out_pt_size)
void frag_main(const float2 pt_coord, const VertOut &v_out, FragOut &frag_out)
#define mix
float x
float y