Blender V5.3
curve.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include "kernel/globals.h"
8
11#include "kernel/geom/object.h"
12
14
15/* Curve Primitive
16 *
17 * Curve primitive for rendering hair and fur. These can be render as flat
18 * ribbons or curves with actual thickness. The curve can also be rendered as
19 * line segments rather than curves for better performance.
20 */
21
22#ifdef __HAIR__
23
24/* Partial derivative of f w.r.t. x, namely ∂f/∂x
25 * f is a function of u (along the curve)
26 * f(u) = f0 * (1 - u) + f1 * u,
27 * The partial derivative in x is
28 * ∂f/∂x = ∂f/∂u * ∂u/∂x
29 * = (f1 - f0) * du.dx. */
30template<typename T>
31ccl_device_inline T curve_attribute_dfdx(const ccl_private differential &du,
32 const ccl_private T &f0,
33 const ccl_private T &f1)
34{
35 return du.dx * (f1 - f0);
36}
37
38/* Partial derivative of f w.r.t. in x, namely ∂f/∂y, similarly computed as ∂f/∂x above. */
39template<typename T>
40ccl_device_inline T curve_attribute_dfdy(const ccl_private differential &du,
41 const ccl_private T &f0,
42 const ccl_private T &f1)
43{
44 return du.dy * (f1 - f0);
45}
46
47/* Read attributes on various curve elements. T is the return type, which can be a plain type
48 * or a dual type to include derivatives. */
49
50template<typename T>
51ccl_device T curve_attribute(KernelGlobals kg,
52 const ccl_private ShaderData *sd,
53 const AttributeDescriptor desc)
54{
55 using BaseT = dual_base_t<T>;
56
58 const KernelCurve curve = kernel_data_fetch(curves, sd->prim);
59 const int k0 = curve.first_key + PRIMITIVE_UNPACK_SEGMENT(sd->type);
60 const int k1 = k0 + 1;
61
62 const BaseT f0 = attribute_data_fetch<BaseT>(kg, desc.element, desc.offset + k0);
63 const BaseT f1 = attribute_data_fetch<BaseT>(kg, desc.element, desc.offset + k1);
64
65 if constexpr (is_dual_v<T>) {
66 T result;
67 result.val = mix(f0, f1, sd->u);
68# ifdef __RAY_DIFFERENTIALS__
69 result.dx = curve_attribute_dfdx(sd->du, f0, f1);
70 result.dy = curve_attribute_dfdy(sd->du, f0, f1);
71# endif
72 return result;
73 }
74 else {
75 return mix(f0, f1, sd->u);
76 }
77 }
78
79 /* idea: we can't derive any useful differentials here, but for tiled
80 * mipmap image caching it would be useful to avoid reading the highest
81 * detail level always. maybe a derivative based on the hair density
82 * could be computed somehow? */
83
84 if (desc.element & ATTR_ELEMENT_CURVE) {
85 return T(attribute_data_fetch<BaseT>(kg, desc.element, desc.offset + sd->prim));
86 }
87 return make_zero<T>();
88}
89
90/* Curve thickness */
91
92ccl_device float curve_thickness(KernelGlobals kg, const ccl_private ShaderData *sd)
93{
94 if (!(sd->type & PRIMITIVE_CURVE)) {
95 return 0.0f;
96 }
97
98 const KernelCurve curve = kernel_data_fetch(curves, sd->prim);
99 const int k0 = curve.first_key + PRIMITIVE_UNPACK_SEGMENT(sd->type);
100 const int k1 = k0 + 1;
101
102 float4 P_curve[2];
103
104# ifdef __OBJECT_MOTION__
105 if (sd->type & PRIMITIVE_MOTION) {
106 motion_curve_keys_linear(kg, sd->object, sd->time, k0, k1, P_curve);
107 }
108 else
109# endif
110 {
111 const int position_offset = kernel_data_fetch(objects, sd->object).position_offset;
112 P_curve[0] = kernel_data_fetch(curve_keys, position_offset + k0);
113 P_curve[1] = kernel_data_fetch(curve_keys, position_offset + k1);
114 }
115
116 float r = 2.0f * ((P_curve[1].w - P_curve[0].w) * sd->u + P_curve[0].w);
117
118 if (sd->object_flag & SD_OBJECT_TRANSFORM_APPLIED) {
119 return r;
120 }
121
122 const float normalized_r = r * (1.0f / M_SQRT3_F);
123 float3 dir = make_float3(normalized_r, normalized_r, normalized_r);
124 object_dir_transform(kg, sd, &dir);
125 return len(dir);
126}
127
128/* Curve random */
129
130ccl_device float curve_random(KernelGlobals kg, const ccl_private ShaderData *sd)
131{
132 if (sd->type & PRIMITIVE_CURVE) {
134 return is_attribute_found(desc) ? curve_attribute<float>(kg, sd, desc) : 0.0f;
135 }
136 return 0.0f;
137}
138
139/* Curve location for motion pass, linear interpolation between keys and
140 * ignoring radius because we do the same for the motion keys */
141
142ccl_device float3 curve_motion_center_location(KernelGlobals kg, const ccl_private ShaderData *sd)
143{
144 const KernelCurve curve = kernel_data_fetch(curves, sd->prim);
145 const int k0 = curve.first_key + PRIMITIVE_UNPACK_SEGMENT(sd->type);
146 const int k1 = k0 + 1;
147
148 float4 P_curve[2];
149
150 const int position_offset = kernel_data_fetch(objects, sd->object).position_offset;
151 P_curve[0] = kernel_data_fetch(curve_keys, position_offset + k0);
152 P_curve[1] = kernel_data_fetch(curve_keys, position_offset + k1);
153
154 return make_float3(P_curve[1]) * sd->u + make_float3(P_curve[0]) * (1.0f - sd->u);
155}
156
157/* Curve tangent normal */
158
159ccl_device float3 curve_tangent_normal(const ccl_private ShaderData *sd)
160{
161 float3 tgN = make_float3(0.0f, 0.0f, 0.0f);
162
163 if (sd->type & PRIMITIVE_CURVE) {
164
165 tgN = -(-sd->wi - sd->dPdu * (dot(sd->dPdu, -sd->wi) / len_squared(sd->dPdu)));
166 tgN = normalize(tgN);
167
168 /* need to find suitable scaled gd for corrected normal */
169# if 0
170 tgN = normalize(tgN - gd * sd->dPdu);
171# endif
172 }
173
174 return tgN;
175}
176
177/* Curve bounds utility function */
178
180 ccl_private float *upper,
181 ccl_private float *extremta,
182 ccl_private float *extrema,
183 ccl_private float *extremtb,
184 ccl_private float *extremb,
185 float p0,
186 float p1,
187 float p2,
188 float p3)
189{
190 float halfdiscroot = (p2 * p2 - 3 * p3 * p1);
191 float ta = -1.0f;
192 float tb = -1.0f;
193
194 *extremta = -1.0f;
195 *extremtb = -1.0f;
196 *upper = p0;
197 *lower = (p0 + p1) + (p2 + p3);
198 *extrema = *upper;
199 *extremb = *lower;
200
201 if (*lower >= *upper) {
202 *upper = *lower;
203 *lower = p0;
204 }
205
206 if (halfdiscroot >= 0) {
207 const float inv3p3 = (1.0f / 3.0f) / p3;
208 halfdiscroot = sqrtf(halfdiscroot);
209 ta = (-p2 - halfdiscroot) * inv3p3;
210 tb = (-p2 + halfdiscroot) * inv3p3;
211 }
212
213 float t2;
214 float t3;
215
216 if (ta > 0.0f && ta < 1.0f) {
217 t2 = ta * ta;
218 t3 = t2 * ta;
219 *extremta = ta;
220 *extrema = p3 * t3 + p2 * t2 + p1 * ta + p0;
221
222 *upper = fmaxf(*extrema, *upper);
223 *lower = fminf(*extrema, *lower);
224 }
225
226 if (tb > 0.0f && tb < 1.0f) {
227 t2 = tb * tb;
228 t3 = t2 * tb;
229 *extremtb = tb;
230 *extremb = p3 * t3 + p2 * t2 + p1 * tb + p0;
231
232 *upper = fmaxf(*extremb, *upper);
233 *lower = fminf(*extremb, *lower);
234 }
235}
236
237#endif /* __HAIR__ */
238
#define kernel_data_fetch(name, index)
#define PRIMITIVE_UNPACK_SEGMENT(type)
#define ccl_private
#define ccl_device_inline
#define M_SQRT3_F
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
CCL_NAMESPACE_BEGIN ccl_device_forceinline bool is_attribute_found(const ccl_private AttributeDescriptor &desc)
ccl_device_inline T attribute_data_fetch(KernelGlobals kg, AttributeElement element, int offset)
ccl_device_inline AttributeDescriptor find_attribute(const ccl_global AttributeMap *attributes_map, uint attr_offset, const int prim, const uint64_t id)
ccl_device_inline void object_dir_transform(KernelGlobals kg, const ccl_private ShaderData *sd, ccl_private float3 *D)
@ PRIMITIVE_MOTION
@ PRIMITIVE_CURVE
@ SD_OBJECT_TRANSFORM_APPLIED
@ ATTR_ELEMENT_CURVE_KEY
@ ATTR_ELEMENT_CURVE
@ ATTR_STD_CURVE_RANDOM
ccl_device_inline T make_zero()
Definition math_dual.h:20
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
ccl_device_inline dual1 len_squared(const ccl_private dual< T > &a)
Definition math_dual.h:273
#define T
#define mix
#define sqrtf
#define ccl_device
#define fmaxf
#define fminf
CCL_NAMESPACE_BEGIN void curvebounds(float *lower, float *upper, float3 *p, const int dim)
AttributeElement element
float w
Definition sky_math.h:225
ccl_static_constexpr bool is_dual_v
Definition types_dual.h:118
typename dual_base_type< T >::type dual_base_t
Definition types_dual.h:128