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