Blender V5.3
gpu_shader_polyline.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
7#include "gpu_shader_attribute_load_lib.glsl"
10
12
13static constexpr float SMOOTH_WIDTH = 1.0f;
14
15/* Using vertex pulling from a storage buffer. */
20
26
33
36};
37
44
45struct Resources {
47
52
55
57
58 [[storage(GPU_SSBO_POLYLINE_POS_BUF_SLOT, read), frequency(GEOMETRY)]] const float (&pos)[];
60 /* Number of component per vertex. */
62 /* True if attribute is in int format and we need to fetch as int (not normalized). */
64
66 frequency(GEOMETRY)]] [[condition(!use_color_uniform)]] const float (&color)[];
70
72
74
75 VertIn pull_vertex_data(uint in_vertex_id) const
76 {
78 uint v_i = index.load(in_vertex_id);
79 uint ofs = uint(gpu_vert_stride_count_offset.z);
80
81 VertIn vert_in;
82 vert_in.ls_P = float3(0.0f, 0.0f, 0.0f);
83 /* Need to support 1, 2 and 3 dimensional input (sigh). */
84 vert_in.ls_P.x = pos[gpu_attr_load_index(v_i, gpu_attr_0) + 0 + ofs];
85 if (gpu_attr_0_len >= 2) {
86 vert_in.ls_P.y = pos[gpu_attr_load_index(v_i, gpu_attr_0) + 1 + ofs];
87 }
88 if (gpu_attr_0_len >= 3) {
89 vert_in.ls_P.z = pos[gpu_attr_load_index(v_i, gpu_attr_0) + 2 + ofs];
90 }
91
93 vert_in.ls_P = float3(floatBitsToInt(vert_in.ls_P));
94 }
95
96 vert_in.final_color = float4(0.0f, 0.0f, 0.0f, 1.0f);
98 /* Need to support 1, 2, 3 and 4 dimensional input (sigh). */
99 vert_in.final_color.x = color[gpu_attr_load_index(v_i, gpu_attr_1) + 0 + ofs];
102 }
103 else {
104 if (gpu_attr_1_len >= 2) {
105 vert_in.final_color.y = color[gpu_attr_load_index(v_i, gpu_attr_1) + 1 + ofs];
106 }
107 if (gpu_attr_1_len >= 3) {
108 vert_in.final_color.z = color[gpu_attr_load_index(v_i, gpu_attr_1) + 2 + ofs];
109 }
110 if (gpu_attr_1_len >= 4) {
111 vert_in.final_color.w = color[gpu_attr_load_index(v_i, gpu_attr_1) + 3 + ofs];
112 }
113 }
114 }
115 return vert_in;
116 }
117
119 {
120 VertOut vert_out;
121 vert_out.gpu_position = ModelViewProjectionMatrix * float4(vert_in.ls_P, 1.0f);
122 vert_out.final_color = vert_in.final_color;
123 vert_out.clip = dot(ModelMatrix * float4(vert_in.ls_P, 1.0f), ClipPlane);
124 return vert_out;
125 }
126
127 /* Clips point to near clip plane before perspective divide. */
129 {
130 if (p.z < -p.w) {
131 /* Just solves p + (q - p) * A; for A when p.z / p.w = -1.0f. */
132 float denom = q.z - p.z + q.w - p.w;
133 if (denom == 0.0f) {
134 /* No solution. */
135 return p;
136 }
137 float A = (-p.z - p.w) / denom;
138 p = p + (q - p) * A;
139 }
140 return p;
141 }
142
143 void strip_emit_vertex(GeomOut &selected_vert,
144 const uint strip_index,
145 uint out_vertex_id,
146 uint out_primitive_id,
147 GeomOut geom_out) const
148 {
149 bool is_odd_primitive = (out_primitive_id & 1u) != 0u;
150 /* Maps triangle list primitives to triangle strip indices. */
151 uint out_strip_index = (is_odd_primitive ? (2u - out_vertex_id) : out_vertex_id) +
152 out_primitive_id;
153
154 if (out_strip_index == strip_index) {
155 selected_vert = geom_out;
156 }
157 }
158
159 /* Runs once per line vertex. Emit 2 vertices. */
160 void do_line_vertex(GeomOut &selected_vert,
161 const uint i,
162 uint out_vertex_id,
163 uint out_primitive_id,
164 VertOut geom_in[2],
166 float2 ofs) const
167 {
168 GeomOut geom_out;
170 [[resource_table]] const UniformColor &uni = uniform_col;
171 geom_out.final_color = uni.color;
172 }
174 /* WATCH: Assuming last provoking vertex. */
175 geom_out.final_color = geom_in[1].final_color;
176 }
178 geom_out.final_color = geom_in[i].final_color;
179 }
180
181 geom_out.clip = geom_in[i].clip;
182
183 geom_out.smoothline = (lineWidth + SMOOTH_WIDTH * float(lineSmooth)) * 0.5f;
184 geom_out.gpu_position = position;
185 geom_out.gpu_position.xy += ofs * position.w;
186 strip_emit_vertex(selected_vert, i * 2u + 0u, out_vertex_id, out_primitive_id, geom_out);
187
188 geom_out.smoothline = -(lineWidth + SMOOTH_WIDTH * float(lineSmooth)) * 0.5f;
189 geom_out.gpu_position = position;
190 geom_out.gpu_position.xy -= ofs * position.w;
191 strip_emit_vertex(selected_vert, i * 2u + 1u, out_vertex_id, out_primitive_id, geom_out);
192 }
193
194 void geometry_main(GeomOut &selected_vert,
195 VertOut geom_in[2],
196 uint out_vertex_id,
197 uint out_primitive_id,
198 uint /*out_invocation_id*/) const
199 {
200 float4 p0 = clip_line_point_homogeneous_space(geom_in[0].gpu_position,
201 geom_in[1].gpu_position);
202 float4 p1 = clip_line_point_homogeneous_space(geom_in[1].gpu_position,
203 geom_in[0].gpu_position);
204 float2 e = normalize(((p1.xy / p1.w) - (p0.xy / p0.w)) * viewportSize.xy);
205
206#if 0 /* Hard turn when line direction changes quadrant. */
207 e = abs(e);
208 float2 ofs = (e.x > e.y) ? float2(0.0f, 1.0f / e.x) : float2(1.0f / e.y, 0.0f);
209#else /* Use perpendicular direction. */
210 float2 ofs = float2(-e.y, e.x);
211#endif
212 ofs /= viewportSize.xy;
214
215 do_line_vertex(selected_vert, 0u, out_vertex_id, out_primitive_id, geom_in, p0, ofs);
216 do_line_vertex(selected_vert, 1u, out_vertex_id, out_primitive_id, geom_in, p1, ofs);
217 }
218};
219
225
226[[vertex]] void vert_main([[resource_table]] const Resources &srt,
227 [[vertex_id]] const int vert_id,
228 [[position]] float4 &out_pos,
229 [[out]] VertInterp &v_out)
230{
231 /* Line list primitive. */
232 uint input_primitive_vertex_count = uint(srt.gpu_vert_stride_count_offset.x);
233 /* Triangle list primitive (emulating triangle strip). */
234 constexpr uint output_primitive_vertex_count = 3u;
235 constexpr uint output_primitive_count = 2u;
236 constexpr uint output_invocation_count = 1u;
237 constexpr uint output_vertex_count_per_invocation = output_primitive_count *
238 output_primitive_vertex_count;
239 constexpr uint output_vertex_count_per_input_primitive = output_vertex_count_per_invocation *
240 output_invocation_count;
241
242 uint in_primitive_id = uint(vert_id) / output_vertex_count_per_input_primitive;
243 uint in_primitive_first_vertex = in_primitive_id * input_primitive_vertex_count;
244
245 uint out_vertex_id = uint(vert_id) % output_primitive_vertex_count;
246 uint out_primitive_id = (uint(vert_id) / output_primitive_vertex_count) % output_primitive_count;
247 uint out_invocation_id = (uint(vert_id) / output_vertex_count_per_invocation) %
248 output_invocation_count;
249 /* Used to wrap around for the line loop case. */
250 uint input_total_vertex_count = uint(srt.gpu_vert_stride_count_offset.y);
251
252 VertIn vert_in[2];
253 vert_in[0] = srt.pull_vertex_data(in_primitive_first_vertex + 0u);
254 vert_in[1] = srt.pull_vertex_data((in_primitive_first_vertex + 1u) % input_total_vertex_count);
255
256 VertOut vert_out[2];
257 vert_out[0] = srt.vertex_main(vert_in[0]);
258 vert_out[1] = srt.vertex_main(vert_in[1]);
259
260 GeomOut out_vert;
261 /* Discard by default. */
262 out_vert.gpu_position = float4(NAN_FLT);
263 srt.geometry_main(out_vert, vert_out, out_vertex_id, out_primitive_id, out_invocation_id);
264
265 out_pos = out_vert.gpu_position;
266 v_out.final_color = out_vert.final_color;
267 v_out.smoothline = out_vert.smoothline;
268 v_out.clip = out_vert.clip;
269}
270
271struct FragOut {
273};
274
277 [[in]] const VertInterp &v_out,
278 [[out]] FragOut &frag_out)
279{
280 if (srt.consts.use_clipping) [[static_branch]] {
281 if (v_out.clip < 0.0f) {
283 }
284 }
285 frag_out.color = v_out.final_color;
286 if (srt.lineSmooth) {
287 frag_out.color.a *= clamp(
288 (srt.lineWidth + SMOOTH_WIDTH) * 0.5f - abs(v_out.smoothline), 0.0f, 1.0f);
289 }
290 frag_out.color = colorspace.rec709_srgb_to_output_space(frag_out.color);
291}
292
293} // namespace builtin::polyline
294
298 .use_clipping = false,
299 .use_color_uniform = false,
300 .use_color_flat = true,
301 .use_color_smooth = false,
302 });
306 .use_clipping = false,
307 .use_color_uniform = false,
308 .use_color_flat = false,
309 .use_color_smooth = true,
310 });
314 .use_clipping = false,
315 .use_color_uniform = true,
316 .use_color_flat = false,
317 .use_color_smooth = false,
318 });
322 .use_clipping = true,
323 .use_color_uniform = true,
324 .use_color_flat = false,
325 .use_color_smooth = false,
326 });
#define NAN_FLT
#define A
uint gpu_attr_load_index(uint vertex_index, int2 stride_and_offset)
#define GPU_SSBO_POLYLINE_POS_BUF_SLOT
#define GPU_SSBO_POLYLINE_COL_BUF_SLOT
#define storage(slot, qualifiers)
#define position
#define index(i)
#define smooth
#define push_constant
#define compilation_constant
#define resource_table
#define static_branch
#define fragment
#define vertex_id
#define frequency(freq)
#define no_perspective
#define condition(cond)
#define vertex
#define frag_color(slot)
void gpu_discard_fragment()
VecBase< int, D > floatBitsToInt(VecOp< float, D >) RET
float4 unpackUnorm4x8(uint) RET
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define abs
VecBase< uint, D > floatBitsToUint(VecOp< float, D >) RET
constexpr T clamp(T, U, U) RET
PipelineGraphic gpu_shader_polyline_uniform_color_clipped(builtin::polyline::vert_main, builtin::polyline::frag_main, builtin::polyline::Constants{ .use_clipping=true,.use_color_uniform=true,.use_color_flat=false,.use_color_smooth=false, })
PipelineGraphic gpu_shader_polyline_uniform_color(builtin::polyline::vert_main, builtin::polyline::frag_main, builtin::polyline::Constants{ .use_clipping=false,.use_color_uniform=true,.use_color_flat=false,.use_color_smooth=false, })
PipelineGraphic gpu_shader_polyline_smooth_color(builtin::polyline::vert_main, builtin::polyline::frag_main, builtin::polyline::Constants{ .use_clipping=false,.use_color_uniform=false,.use_color_flat=false,.use_color_smooth=true, })
PipelineGraphic gpu_shader_polyline_flat_color(builtin::polyline::vert_main, builtin::polyline::frag_main, builtin::polyline::Constants{ .use_clipping=false,.use_color_uniform=false,.use_color_flat=true,.use_color_smooth=false, })
MatBase< float, 4, 4 > float4x4
void frag_main(const Resources &srt, const ColorSpace &colorspace, const VertInterp &v_out, FragOut &frag_out)
static constexpr float SMOOTH_WIDTH
void vert_main(const Resources &srt, const int vert_id, float4 &out_pos, VertInterp &v_out)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
void geometry_main(GeomOut &selected_vert, VertOut geom_in[2], uint out_vertex_id, uint out_primitive_id, uint) const
void do_line_vertex(GeomOut &selected_vert, const uint i, uint out_vertex_id, uint out_primitive_id, VertOut geom_in[2], float4 position, float2 ofs) const
void strip_emit_vertex(GeomOut &selected_vert, const uint strip_index, uint out_vertex_id, uint out_primitive_id, GeomOut geom_out) const
VertIn pull_vertex_data(uint in_vertex_id) const
VertOut vertex_main(VertIn vert_in) const
float4 clip_line_point_homogeneous_space(float4 p, float4 q) const
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
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