Blender V5.3
draw_curves.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
8
9#pragma once
10
11#include "draw_shader_shared.hh"
15
22
23namespace draw::curves {
24
25struct Segment {
26 /* Index of this segment. Used to load indirection buffer. */
27 uint id;
28 /* Vertex index inside this segment. */
29 uint v_idx;
30 /* Restart triangle strip if true. Only for cylinder topology. */
32};
33
34/* Result of interpreting the indirection buffer.
35 * The indirection buffer maps drawn segments back to their curves and curve segments.
36 * This is needed for attribute loading. */
38 /* Can be equal to INT_MAX with ribbon draw type. */
40 /* Segment ID starting at 0 at curve start. */
42 /* Restart triangle strip if true. Only for ribbon topology. */
44 /* Does these vertices correspond to the last point of a cyclic curve (duplicate of start). */
46};
47
48struct Point {
49 /* Position of the evaluated curve point (not the shape / cylinder point). */
51 /* Tangent vector going from the root to the tip of the curve. */
53
54 float radius;
55 /* Lateral/Azimuthal offset from the center of the curve's width. Range [-1..1]. */
57
61};
62
63struct ShapePoint {
64 /* Curve tangent space. */
68 /* Position on the curve shape. */
70 /* Shading normal at the position on the curve shape. */
72};
73
74} // namespace draw::curves
75
76namespace draw {
77
78struct Curves {
81
83
84 /* Indirection buffer indexing. */
86 {
87 const bool is_cylinder = drw_curves.half_cylinder_face_count > 1u;
88 curves::Segment segment;
90 segment.v_idx = vertex_id % drw_curves.vertex_per_segment;
91 segment.is_end_of_segment = is_cylinder &&
92 (segment.v_idx == drw_curves.vertex_per_segment - 1);
93 if (is_cylinder && !segment.is_end_of_segment && (segment.id & 1u) == 0u) {
94 /* The topology is not actually restarted and the winding order changes (because we skip an
95 * odd number of triangles). So we have to manually reverse the winding so that is stays
96 * consistent.
97 */
98 segment.v_idx ^= 1u;
99 }
100 return segment;
101 }
102
104 {
106 ind.is_cyclic_point = false;
107 ind.is_end_of_curve = false;
108 ind.curve_id = 0;
109
110 constexpr int cyclic_endpoint_pivot = INT_MAX / 2;
111 constexpr int end_of_curve = INT_MAX;
112
113 int indirection_value = texelFetch(curves_indirection_buf, int(segment.id)).r;
114 if (indirection_value == end_of_curve) {
115 ind.curve_segment = 0;
116 ind.is_end_of_curve = true;
117 }
118 else if (indirection_value >= 0) {
119 /* This is start of curve. The indirection value is the curve ID. */
120 ind.curve_segment = 0;
121 ind.curve_id = indirection_value;
122 }
123 else if (indirection_value <= -cyclic_endpoint_pivot) {
124 /* This is the last segment of a cyclic curve. The indirection value is the offset to the
125 * start of the curve offset by cyclic_endpoint_pivot. */
126 ind.curve_segment = -indirection_value - cyclic_endpoint_pivot;
127 ind.is_cyclic_point = true;
128 }
129 else {
130 /* This is a normal segment. The indirection value is the offset to the start of the curve.
131 */
132 ind.curve_segment = -indirection_value;
133 }
134
135 if (ind.curve_segment != 0) {
136 ind.curve_id = texelFetch(curves_indirection_buf, int(segment.id) - ind.curve_segment).r;
137 }
138
139 const bool is_cylinder = drw_curves.half_cylinder_face_count > 1u;
140 if (is_cylinder) {
141 bool is_end_of_segment = (segment.v_idx & 1u) != 0u;
142 ind.curve_segment += int(is_end_of_segment);
143 /* Only the end of the segment is to be considered the cyclic point. */
144 if (!is_end_of_segment) {
145 ind.is_cyclic_point = false;
146 }
147 }
148 return ind;
149 }
150
151 int point_id_get(curves::Segment segment, curves::Indirection indirection) const
152 {
153 const bool is_cylinder = drw_curves.half_cylinder_face_count > 1u;
154 if (is_cylinder) {
155 return int(segment.id) + indirection.curve_id + int(segment.v_idx & 1u);
156 }
157 return int(segment.id) - indirection.curve_id;
158 }
159
161 {
162 float offset;
163 const bool is_strand = drw_curves.half_cylinder_face_count == 0u;
164 const bool is_cylinder = drw_curves.half_cylinder_face_count > 1u;
165 if (is_strand) {
166 offset = 0.5f;
167 }
168 else if (is_cylinder) {
169 offset = float(segment.v_idx >> 1) / float(drw_curves.half_cylinder_face_count);
170 }
171 else {
172 offset = float(segment.v_idx);
173 }
174 return offset * 2.0f - 1.0f;
175 }
176
178 {
179 return texelFetch(curves_pos_rad_buf, int(point_id));
180 }
181
182 float3 point_position_get(uint point_id) const
183 {
184 return texelFetch(curves_pos_rad_buf, int(point_id)).rgb;
185 }
186
187 /* Return data about the curve point. */
189 {
191 curves::Indirection indirection = indirection_get(segment);
192
193 curves::Point pt;
194 pt.point_id = point_id_get(segment, indirection);
195 pt.curve_id = indirection.curve_id;
196 pt.curve_segment = indirection.curve_segment;
197
199
200 bool restart_strip = indirection.is_end_of_curve || segment.is_end_of_segment;
201 pt.P = (restart_strip) ? float3(NAN_FLT) : pos_rad.xyz;
202 pt.radius = pos_rad.w;
204
205 if (pt.curve_segment == 0) {
206 /* Hair root. */
207 pt.T = point_position_get(uint(pt.point_id + 1)) - pt.P;
208 }
209 else if (indirection.is_cyclic_point) {
210 /* Cyclic end point must match start point. */
211 pt.T = point_position_get(uint(pt.point_id - pt.curve_segment + 1)) - pt.P;
212 }
213 else {
214 pt.T = pt.P - point_position_get(uint(pt.point_id - 1));
215 }
216 return pt;
217 }
218
226
233 {
234 const bool is_strand = drw_curves.half_cylinder_face_count == 0u;
235
236 curves::ShapePoint shape;
237 /* Shading tangent is inverted because of legacy reason. */
238 /* TODO(fclem): Change user code. */
239 shape.curve_T = -pt.T;
240 shape.curve_B = normalize(cross(pt.T, V));
241 shape.curve_N = cross(shape.curve_B, pt.T);
242 /* Point in curve azimuthal space. */
244 shape.N = shape.curve_B * lP.x + shape.curve_N * lP.y;
245 shape.P = pt.P + shape.N * (is_strand ? 0.0f : pt.radius);
246 return shape;
247 }
248
249 float3 get_curve_root_pos(const int point_id, const int curve_segment) const
250 {
251 int curve_start = point_id - curve_segment;
252 return texelFetch(curves_pos_rad_buf, curve_start).xyz;
253 }
254};
255
256} // namespace draw
257
258namespace draw::curves {
259
260float get_customdata_float(const int curve_id, const samplerBuffer cd_buf)
261{
262 return texelFetch(cd_buf, curve_id).x;
263}
264
265float2 get_customdata_vec2(const int curve_id, const samplerBuffer cd_buf)
266{
267 return texelFetch(cd_buf, curve_id).xy;
268}
269
270float3 get_customdata_vec3(const int curve_id, const samplerBuffer cd_buf)
271{
272 return texelFetch(cd_buf, curve_id).xyz;
273}
274
275float4 get_customdata_vec4(const int curve_id, const samplerBuffer cd_buf)
276{
277 return texelFetch(cd_buf, curve_id).xyzw;
278}
279
280} // namespace draw::curves
#define NAN_FLT
#define M_SQRT1_3
#define DRW_OBJ_DATA_INFO_UBO_SLOT
#define INT_MAX
#define uniform(slot)
#define sampler(slot)
#define vertex_id
#define frequency(freq)
VecBase< float, D > normalize(VecOp< float, D >) RET
#define abs
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
float length(VecOp< float, D >) RET
TEX_TEMPLATE DataVec texelFetch(T, IntCoord, int) RET
SamplerBase< float, 1 > samplerBuffer
SamplerBase< int, 1 > isamplerBuffer
float sin_from_cos(float c)
float3 to_scale(float3x3 mat)
float3 transform_direction(float3x3 mat, float3 direction)
float3 transform_point(float3x3 mat, float3 point)
MatBase< float, 4, 4 > float4x4
float3 get_customdata_vec3(const int curve_id, const samplerBuffer cd_buf)
float2 get_customdata_vec2(const int curve_id, const samplerBuffer cd_buf)
float4 get_customdata_vec4(const int curve_id, const samplerBuffer cd_buf)
float get_customdata_float(const int curve_id, const samplerBuffer cd_buf)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
float3 get_curve_root_pos(const int point_id, const int curve_segment) const
curves::Segment segment_get(uint vertex_id) const
curves::Point point_get(uint vertex_id) const
curves::Point object_to_world(curves::Point pt, float4x4 object_to_world) const
CurvesInfos & drw_curves
int point_id_get(curves::Segment segment, curves::Indirection indirection) const
float3 point_position_get(uint point_id) const
curves::ShapePoint shape_point_get(const curves::Point pt, const float3 V) const
curves::Indirection indirection_get(curves::Segment segment) const
samplerBuffer curves_pos_rad_buf
float azimuthal_offset_get(curves::Segment segment) const
float4 point_position_and_radius_get(uint point_id) const
isamplerBuffer curves_indirection_buf
float x
float y
float w
Definition sky_math.h:225
CCL_NAMESPACE_BEGIN struct Window V