Blender V5.3
gpu_shader_annotation.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2018-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
8
9#define GPENCIL_FLATCAP 1
10
12
13/* Get Z-depth value. */
14float get_Z_depth(float4 point)
15{
16 return (point.z / point.w);
17}
18
19/* check equality but with a small tolerance */
21{
22 float limit = 0.0001f;
23 float x = abs(p1.x - p2.x);
24 float y = abs(p1.y - p2.y);
25 float z = abs(p1.z - p2.z);
26
27 if ((x < limit) && (y < limit) && (z < limit)) {
28 return true;
29 }
30
31 return false;
32}
33
39
45
46struct Resources {
47 [[storage(0, read), frequency(GEOMETRY)]] const GreasePencilStrokeData (&gp_vert_data)[];
49
52
53 /* project 3d point to 2d on screen space */
55 {
56 return float2(vert.xy / vert.w) * gpencil_stroke_data.viewport;
57 }
58
59 GreasePencilStrokeData input_assembly(uint in_vertex_id) const
60 {
61 /* Assume no index buffer. */
62 return gp_vert_data[in_vertex_id];
63 }
64
66 {
67 float thickness_scale = 1 / gpencil_stroke_data.pixsize;
68
69 VertOut vert_out;
70 vert_out.gpu_position = ModelViewProjectionMatrix * float4(vert_in.position, 1.0f);
71 vert_out.final_color = vert_in.stroke_color;
72
73 float size = (ProjectionMatrix[3][3] == 0.0f) ?
74 (vert_in.stroke_thickness * thickness_scale / vert_out.gpu_position.z) :
75 (vert_in.stroke_thickness * thickness_scale);
76 vert_out.final_thickness = max(size * gpencil_stroke_data.objscale, 1.0f);
77 return vert_out;
78 }
79
80 void strip_emit_vertex(GeomOut &selected_vert,
81 const uint strip_index,
82 uint out_vertex_id,
83 uint out_primitive_id,
84 GeomOut geom_out) const
85 {
86 bool is_odd_primitive = (out_primitive_id & 1u) != 0u;
87 /* Maps triangle list primitives to triangle strip indices. */
88 uint out_strip_index = (is_odd_primitive ? (2u - out_vertex_id) : out_vertex_id) +
89 out_primitive_id;
90
91 if (out_strip_index == strip_index) {
92 selected_vert = geom_out;
93 }
94 }
95
96 void geometry_main(GeomOut &selected_vert,
97 VertOut geom_in[4],
98 uint out_vertex_id,
99 uint out_primitive_id) const
100 {
101 constexpr float MiterLimit = 0.75f;
102
103 float4 P0 = geom_in[0].gpu_position;
104 float4 P1 = geom_in[1].gpu_position;
105 float4 P2 = geom_in[2].gpu_position;
106 float4 P3 = geom_in[3].gpu_position;
107
108 /* Get the four vertices passed to the shader. */
109 float2 sp0 = to_screen_space(P0); /* start of previous segment */
110 float2 sp1 = to_screen_space(P1); /* end of previous segment, start of current segment */
111 float2 sp2 = to_screen_space(P2); /* end of current segment, start of next segment */
112 float2 sp3 = to_screen_space(P3); /* end of next segment */
113
114 /* Culling outside viewport. */
115 float2 area = gpencil_stroke_data.viewport * 4.0f;
116 if (sp1.x < -area.x || sp1.x > area.x) {
117 return;
118 }
119 if (sp1.y < -area.y || sp1.y > area.y) {
120 return;
121 }
122 if (sp2.x < -area.x || sp2.x > area.x) {
123 return;
124 }
125 if (sp2.y < -area.y || sp2.y > area.y) {
126 return;
127 }
128
129 /* determine the direction of each of the 3 segments (previous, current, next) */
130 float2 v0 = normalize(sp1 - sp0);
131 float2 v1 = normalize(sp2 - sp1);
132 float2 v2 = normalize(sp3 - sp2);
133
134 /* determine the normal of each of the 3 segments (previous, current, next) */
135 float2 n0 = float2(-v0.y, v0.x);
136 float2 n1 = float2(-v1.y, v1.x);
137 float2 n2 = float2(-v2.y, v2.x);
138
139 /* determine miter lines by averaging the normals of the 2 segments */
140 float2 miter_a = normalize(n0 + n1); /* miter at start of current segment */
141 float2 miter_b = normalize(n1 + n2); /* miter at end of current segment */
142
143 /* determine the length of the miter by projecting it onto normal and then inverse it */
144 float an1 = dot(miter_a, n1);
145 float bn1 = dot(miter_b, n2);
146 if (an1 == 0.0f) {
147 an1 = 1.0f;
148 }
149 if (bn1 == 0.0f) {
150 bn1 = 1.0f;
151 }
152
153 float length_a = geom_in[1].final_thickness / an1;
154 float length_b = geom_in[2].final_thickness / bn1;
155 if (length_a <= 0.0f) {
156 length_a = 0.01f;
157 }
158 if (length_b <= 0.0f) {
159 length_b = 0.01f;
160 }
161
162 GeomOut geom_out;
163
164 /* prevent excessively long miters at sharp corners */
165 if (dot(v0, v1) < -MiterLimit) {
166 miter_a = n1;
167 length_a = geom_in[1].final_thickness;
168
169 /* close the gap */
170 if (dot(v0, n1) > 0.0f) {
171 geom_out.tex_coord = float2(0.0f, 0.0f);
172 geom_out.final_color = geom_in[1].final_color;
173 geom_out.gpu_position = float4((sp1 + geom_in[1].final_thickness * n0) /
175 get_Z_depth(P1),
176 1.0f);
177 strip_emit_vertex(selected_vert, 0, out_vertex_id, out_primitive_id, geom_out);
178
179 geom_out.tex_coord = float2(0.0f, 0.0f);
180 geom_out.final_color = geom_in[1].final_color;
181 geom_out.gpu_position = float4((sp1 + geom_in[1].final_thickness * n1) /
183 get_Z_depth(P1),
184 1.0f);
185 strip_emit_vertex(selected_vert, 1, out_vertex_id, out_primitive_id, geom_out);
186
187 geom_out.tex_coord = float2(0.0f, 0.5f);
188 geom_out.final_color = geom_in[1].final_color;
189 geom_out.gpu_position = float4(sp1 / gpencil_stroke_data.viewport, get_Z_depth(P1), 1.0f);
190 strip_emit_vertex(selected_vert, 2, out_vertex_id, out_primitive_id, geom_out);
191 }
192 else {
193 geom_out.tex_coord = float2(0.0f, 1.0f);
194 geom_out.final_color = geom_in[1].final_color;
195 geom_out.gpu_position = float4((sp1 - geom_in[1].final_thickness * n1) /
197 get_Z_depth(P1),
198 1.0f);
199 strip_emit_vertex(selected_vert, 0, out_vertex_id, out_primitive_id, geom_out);
200
201 geom_out.tex_coord = float2(0.0f, 1.0f);
202 geom_out.final_color = geom_in[1].final_color;
203 geom_out.gpu_position = float4((sp1 - geom_in[1].final_thickness * n0) /
205 get_Z_depth(P1),
206 1.0f);
207 strip_emit_vertex(selected_vert, 1, out_vertex_id, out_primitive_id, geom_out);
208
209 geom_out.tex_coord = float2(0.0f, 0.5f);
210 geom_out.final_color = geom_in[1].final_color;
211 geom_out.gpu_position = float4(sp1 / gpencil_stroke_data.viewport, get_Z_depth(P1), 1.0f);
212 strip_emit_vertex(selected_vert, 2, out_vertex_id, out_primitive_id, geom_out);
213 }
214
215 /* Restart the strip. */
216 geom_out.gpu_position = float4(NAN_FLT);
217 strip_emit_vertex(selected_vert, 3, out_vertex_id, out_primitive_id, geom_out);
218 }
219
220 if (dot(v1, v2) < -MiterLimit) {
221 miter_b = n1;
222 length_b = geom_in[2].final_thickness;
223 }
224
225 /* Generate the start end-cap (alpha < 0 used as end-cap flag). */
226 float extend = gpencil_stroke_data.fill_stroke ? 2.0f : 1.0f;
228 geom_out.tex_coord = float2(1.0f, 0.5f);
229 geom_out.final_color = float4(geom_in[1].final_color.rgb, geom_in[1].final_color.a * -1.0f);
230 float2 svn1 = normalize(sp1 - sp2) * length_a * 4.0f * extend;
231 geom_out.gpu_position = float4(
232 (sp1 + svn1) / gpencil_stroke_data.viewport, get_Z_depth(P1), 1.0f);
233 strip_emit_vertex(selected_vert, 4, out_vertex_id, out_primitive_id, geom_out);
234
235 geom_out.tex_coord = float2(0.0f, 0.0f);
236 geom_out.final_color = float4(geom_in[1].final_color.rgb, geom_in[1].final_color.a * -1.0f);
237 geom_out.gpu_position = float4((sp1 - (length_a * 2.0f) * miter_a) /
239 get_Z_depth(P1),
240 1.0f);
241 strip_emit_vertex(selected_vert, 5, out_vertex_id, out_primitive_id, geom_out);
242
243 geom_out.tex_coord = float2(0.0f, 1.0f);
244 geom_out.final_color = float4(geom_in[1].final_color.rgb, geom_in[1].final_color.a * -1.0f);
245 geom_out.gpu_position = float4((sp1 + (length_a * 2.0f) * miter_a) /
247 get_Z_depth(P1),
248 1.0f);
249 strip_emit_vertex(selected_vert, 6, out_vertex_id, out_primitive_id, geom_out);
250 }
251
252 /* generate the triangle strip */
253 geom_out.tex_coord = float2(0.0f, 0.0f);
254 geom_out.final_color = geom_in[1].final_color;
255 geom_out.gpu_position = float4(
256 (sp1 + length_a * miter_a) / gpencil_stroke_data.viewport, get_Z_depth(P1), 1.0f);
257 strip_emit_vertex(selected_vert, 7, out_vertex_id, out_primitive_id, geom_out);
258
259 geom_out.tex_coord = float2(0.0f, 1.0f);
260 geom_out.final_color = geom_in[1].final_color;
261 geom_out.gpu_position = float4(
262 (sp1 - length_a * miter_a) / gpencil_stroke_data.viewport, get_Z_depth(P1), 1.0f);
263 strip_emit_vertex(selected_vert, 8, out_vertex_id, out_primitive_id, geom_out);
264
265 geom_out.tex_coord = float2(0.0f, 0.0f);
266 geom_out.final_color = geom_in[2].final_color;
267 geom_out.gpu_position = float4(
268 (sp2 + length_b * miter_b) / gpencil_stroke_data.viewport, get_Z_depth(P2), 1.0f);
269 strip_emit_vertex(selected_vert, 9, out_vertex_id, out_primitive_id, geom_out);
270
271 geom_out.tex_coord = float2(0.0f, 1.0f);
272 geom_out.final_color = geom_in[2].final_color;
273 geom_out.gpu_position = float4(
274 (sp2 - length_b * miter_b) / gpencil_stroke_data.viewport, get_Z_depth(P2), 1.0f);
275 strip_emit_vertex(selected_vert, 10, out_vertex_id, out_primitive_id, geom_out);
276
277 /* Generate the end end-cap (alpha < 0 used as end-cap flag). */
279 geom_out.tex_coord = float2(0.0f, 1.0f);
280 geom_out.final_color = float4(geom_in[2].final_color.rgb, geom_in[2].final_color.a * -1.0f);
281 geom_out.gpu_position = float4((sp2 + (length_b * 2.0f) * miter_b) /
283 get_Z_depth(P2),
284 1.0f);
285 strip_emit_vertex(selected_vert, 11, out_vertex_id, out_primitive_id, geom_out);
286
287 geom_out.tex_coord = float2(0.0f, 0.0f);
288 geom_out.final_color = float4(geom_in[2].final_color.rgb, geom_in[2].final_color.a * -1.0f);
289 geom_out.gpu_position = float4((sp2 - (length_b * 2.0f) * miter_b) /
291 get_Z_depth(P2),
292 1.0f);
293 strip_emit_vertex(selected_vert, 12, out_vertex_id, out_primitive_id, geom_out);
294
295 geom_out.tex_coord = float2(1.0f, 0.5f);
296 geom_out.final_color = float4(geom_in[2].final_color.rgb, geom_in[2].final_color.a * -1.0f);
297 float2 svn2 = normalize(sp2 - sp1) * length_b * 4.0f * extend;
298 geom_out.gpu_position = float4(
299 (sp2 + svn2) / gpencil_stroke_data.viewport, get_Z_depth(P2), 1.0f);
300 strip_emit_vertex(selected_vert, 13, out_vertex_id, out_primitive_id, geom_out);
301 }
302 }
303};
304
309
310[[vertex]] void vert_main([[resource_table]] const Resources &srt,
311 [[vertex_id]] const int vert_id,
312 [[position]] float4 &out_pos,
313 [[out]] VertInterp &v_out)
314{
315 /* Line Strip Adjacency primitive. */
316 constexpr uint input_primitive_vertex_count =
317 1u; /* We read 4 but advance 1. Assume no restart. */
318 /* Triangle list primitive (emulating triangle strip). */
319 constexpr uint output_primitive_vertex_count = 3u;
320 constexpr uint output_primitive_count = 12u;
321 constexpr uint output_invocation_count = 1u;
322 constexpr uint output_vertex_count_per_invocation = output_primitive_count *
323 output_primitive_vertex_count;
324 constexpr uint output_vertex_count_per_input_primitive = output_vertex_count_per_invocation *
325 output_invocation_count;
326
327 uint in_primitive_id = uint(vert_id) / output_vertex_count_per_input_primitive;
328 uint in_primitive_first_vertex = in_primitive_id * input_primitive_vertex_count;
329
330 uint out_vertex_id = uint(vert_id) % output_primitive_vertex_count;
331 uint out_primitive_id = (uint(vert_id) / output_primitive_vertex_count) % output_primitive_count;
332
333 GreasePencilStrokeData vert_in[4];
334 vert_in[0] = srt.input_assembly(in_primitive_first_vertex + 0u);
335 vert_in[1] = srt.input_assembly(in_primitive_first_vertex + 1u);
336 vert_in[2] = srt.input_assembly(in_primitive_first_vertex + 2u);
337 vert_in[3] = srt.input_assembly(in_primitive_first_vertex + 3u);
338
339 VertOut vert_out[4];
340 vert_out[0] = srt.vertex_main(vert_in[0]);
341 vert_out[1] = srt.vertex_main(vert_in[1]);
342 vert_out[2] = srt.vertex_main(vert_in[2]);
343 vert_out[3] = srt.vertex_main(vert_in[3]);
344
345 GeomOut out_vert;
346 /* Discard by default. */
347 out_vert.gpu_position = float4(NAN_FLT);
348 srt.geometry_main(out_vert, vert_out, out_vertex_id, out_primitive_id);
349
350 out_pos = out_vert.gpu_position;
351 v_out.mTexCoord = out_vert.tex_coord;
352 v_out.mColor = out_vert.final_color;
353}
354
355struct FragOut {
357};
358
359[[fragment]] void frag_main([[in]] const VertInterp &v_out, [[out]] FragOut &frag_out)
360{
361 constexpr float2 center = float2(0.0f, 0.5f);
362 float4 tColor = v_out.mColor;
363 /* if alpha < 0, then encap */
364 if (tColor.a < 0.0f) {
365 tColor.a = tColor.a * -1.0f;
366 float dist = length(v_out.mTexCoord - center);
367 if (dist > 0.25f) {
369 }
370 }
371 /* Solid */
372 frag_out.color = tColor;
373}
374
375} // namespace builtin::annotation
376
#define NAN_FLT
btScalar max
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
#define GPENCIL_FLATCAP
PipelineGraphic gpu_shader_annotation(builtin::annotation::vert_main, builtin::annotation::frag_main)
#define uniform(slot)
#define storage(slot, qualifiers)
#define position
#define smooth
#define push_constant
#define resource_table
#define fragment
#define vertex_id
#define frequency(freq)
#define vertex
#define frag_color(slot)
void gpu_discard_fragment()
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define abs
float length(VecOp< float, D >) RET
MatBase< float, 4, 4 > float4x4
bool is_equal(float4 p1, float4 p2)
void vert_main(const Resources &srt, const int vert_id, float4 &out_pos, VertInterp &v_out)
void frag_main(const VertInterp &v_out, FragOut &frag_out)
float get_Z_depth(float4 point)
void geometry_main(GeomOut &selected_vert, VertOut geom_in[4], uint out_vertex_id, uint out_primitive_id) const
const GreasePencilStrokeData(& gp_vert_data)[]
GreasePencilStrokeData input_assembly(uint in_vertex_id) const
VertOut vertex_main(GreasePencilStrokeData vert_in) const
float2 to_screen_space(float4 vert) const
void strip_emit_vertex(GeomOut &selected_vert, const uint strip_index, uint out_vertex_id, uint out_primitive_id, GeomOut geom_out) const
float x
float y
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225