Blender V5.3
primitive.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/* Primitive Utilities
6 *
7 * Generic functions to look up mesh, curve and volume primitive attributes for
8 * shading and render passes. */
9
10#pragma once
11
12#include "kernel/globals.h"
13
15
17#include "kernel/geom/curve.h"
18#include "kernel/geom/object.h"
19#include "kernel/geom/point.h"
21#include "kernel/geom/volume.h"
22
24
25/* Surface Attributes
26 *
27 * Read geometry attributes for surface shading. This is distinct from volume
28 * attributes for performance, mainly for GPU performance to avoid bringing in
29 * heavy volume interpolation code. */
30
31template<typename T>
33 const ccl_private ShaderData *sd,
34 const AttributeDescriptor desc)
35{
36 using BaseT = dual_base_t<T>;
37
39 return T(attribute_data_fetch<BaseT>(kg, desc.element, desc.offset));
40 }
41
42 if (sd->type & PRIMITIVE_TRIANGLE) {
43 return triangle_attribute<T>(kg, sd, desc);
44 }
45#ifdef __HAIR__
46 if (sd->type & PRIMITIVE_CURVE) {
47 return curve_attribute<T>(kg, sd, desc);
48 }
49#endif
50#ifdef __POINTCLOUD__
51 else if (sd->type & PRIMITIVE_ANY_POINT) {
52 return point_attribute<T>(kg, sd, desc);
53 }
54#endif
55 else {
56 return make_zero<T>();
57 }
58}
59
60/* Set sd->N to the undisplaced normal. For smooth shading, use the stored undisplaced
61 * normal attribute. For flat shading, compute the geometric face normal from undisplaced
62 * triangle positions. */
64 ccl_private ShaderData *sd,
65 const int position_undisplaced_offset)
66{
67 float3 N;
68
69 if (sd->shader & SHADER_SMOOTH_NORMAL) {
71 if (!is_attribute_found(ndesc)) {
72 return;
73 }
75 }
76 else {
77 N = triangle_face_normal_undisplaced(kg, sd, position_undisplaced_offset);
78 }
79
80 object_normal_transform(kg, sd, &N);
81 sd->N = (sd->runtime_flag & SR_BACKFACING) ? -N : N;
82}
83
84#ifdef __VOLUME__
85/* Volume Attributes
86 *
87 * Read geometry attributes for volume shading. This is distinct from surface
88 * attributes for performance, mainly for GPU performance to avoid bringing in
89 * heavy volume interpolation code. */
90
91ccl_device_forceinline bool primitive_is_volume_attribute(const ccl_private ShaderData *sd)
92{
93 return sd->type == PRIMITIVE_VOLUME;
94}
95
96template<typename T>
97ccl_device_inline T primitive_volume_attribute(KernelGlobals kg,
98 ccl_private ShaderData *sd,
99 const AttributeDescriptor desc,
100 const bool stochastic)
101{
102 if (primitive_is_volume_attribute(sd)) {
103 return volume_attribute_value<T>(volume_attribute_float4(kg, sd, desc, stochastic));
104 }
105 return make_zero<T>();
106}
107#endif
108
109/* Default UV coordinate */
110
112{
113 const AttributeDescriptor desc = find_attribute(kg, sd, ATTR_STD_UV);
114
115 if (!is_attribute_found(desc)) {
116 return make_float3(0.0f, 0.0f, 0.0f);
117 }
118
119 const float2 uv = primitive_surface_attribute<float2>(kg, sd, desc);
120 return make_float3(uv.x, uv.y, 1.0f);
121}
122
123/* PTEX coordinates. */
124
126 ccl_private ShaderData *sd,
128 ccl_private int *face_id)
129{
130 /* storing ptex data as attributes is not memory efficient but simple for tests */
131 const AttributeDescriptor desc_face_id = find_attribute(kg, sd, ATTR_STD_PTEX_FACE_ID);
132 const AttributeDescriptor desc_uv = find_attribute(kg, sd, ATTR_STD_PTEX_UV);
133
134 if (!is_attribute_found(desc_face_id) || !is_attribute_found(desc_uv)) {
135 return false;
136 }
137
138 const float3 uv3 = primitive_surface_attribute<float3>(kg, sd, desc_uv);
139 const float face_id_f = primitive_surface_attribute<float>(kg, sd, desc_face_id);
140
141 *uv = make_float2(uv3.x, uv3.y);
142 *face_id = (int)face_id_f;
143
144 return true;
145}
146
147/* Surface tangent */
148
149template<typename Float3Type>
151{
152#if defined(__HAIR__) || defined(__POINTCLOUD__)
153 if (sd->type & (PRIMITIVE_CURVE | PRIMITIVE_ANY_POINT)) {
154# ifdef __DPDU__
155 return Float3Type(normalize(sd->dPdu));
156 }
157# else
158 return make_zero<Float3Type>();
159# endif
160#endif
161
162 /* try to create spherical tangent from generated coordinates */
164
165 if (is_attribute_found(desc)) {
166 if constexpr (is_dual_v<Float3Type>) {
167 dual3 data = primitive_surface_attribute<dual3>(kg, sd, desc);
168 data = make_float3(-(data.y() - 0.5f), (data.x() - 0.5f), dual1());
169 object_normal_transform(kg, sd, &data);
170 return cross(sd->N, normalize(cross(data, sd->N)));
171 }
172 else {
173 float3 data = primitive_surface_attribute<float3>(kg, sd, desc);
174 data = make_float3(-(data.y - 0.5f), (data.x - 0.5f), 0.0f);
175 object_normal_transform(kg, sd, &data);
176 return cross(sd->N, normalize(cross(data, sd->N)));
177 }
178 }
179 /* otherwise use surface derivatives */
180#ifdef __DPDU__
181 return Float3Type(normalize(sd->dPdu));
182#else
183 return make_zero<Float3Type>();
184#endif
185}
186
187/* Motion vector common */
188
190 const ccl_private ShaderData *sd,
191 const int offset)
192{
193#if defined(__HAIR__)
194 if (sd->type & PRIMITIVE_CURVE) {
195 const KernelCurve curve = kernel_data_fetch(curves, sd->prim);
196 const int k0 = curve.first_key + PRIMITIVE_UNPACK_SEGMENT(sd->type);
197 const int k1 = k0 + 1;
198 const float4 f0 = kernel_data_fetch(curve_keys, offset + k0);
199 const float4 f1 = kernel_data_fetch(curve_keys, offset + k1);
200 return make_float3(mix(f0, f1, sd->u));
201 }
202#endif
203#if defined(__POINTCLOUD__)
204 if (sd->type & PRIMITIVE_ANY_POINT) {
205 return make_float3(kernel_data_fetch(points, offset + sd->prim));
206 }
207#endif
208 const uint3 tri_vindex = kernel_data_fetch(tri_vindex, sd->prim);
209 const float3 v0 = kernel_data_fetch(tri_verts, offset + tri_vindex.x);
210 const float3 v1 = kernel_data_fetch(tri_verts, offset + tri_vindex.y);
211 const float3 v2 = kernel_data_fetch(tri_verts, offset + tri_vindex.z);
212 return triangle_interpolate(sd->u, sd->v, v0, v1, v2);
213}
214
216 const ccl_private ShaderData *sd,
217 ccl_private float3 *motion_center,
218 ccl_private float3 *motion_pre,
219 ccl_private float3 *motion_post)
220{
221#if defined(__HAIR__) || defined(__POINTCLOUD__)
222 const bool is_curve_or_point = sd->type & (PRIMITIVE_CURVE | PRIMITIVE_ANY_POINT);
223 if (is_curve_or_point) {
224 *motion_center = make_float3(0.0f, 0.0f, 0.0f);
225
226 if (sd->type & PRIMITIVE_CURVE) {
227# if defined(__HAIR__)
228 *motion_center = curve_motion_center_location(kg, sd);
229# endif
230 }
231 else if (sd->type & PRIMITIVE_ANY_POINT) {
232# if defined(__POINTCLOUD__)
233 *motion_center = point_motion_center_location(kg, sd);
234# endif
235 }
236
237 if (!(sd->object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
238 object_position_transform(kg, sd, motion_center);
239 }
240 }
241 else
242#endif
243 {
244 *motion_center = sd->P;
245 }
246
247 *motion_pre = *motion_center;
248 *motion_post = *motion_center;
249
250 /* deformation motion */
251 const ccl_global KernelObject &kobject = kernel_data_fetch(objects, sd->object);
252 const int pos_offset = kobject.position_offset;
253 const int numverts = kobject.numverts;
254 const int num_motion_steps = kobject.num_geom_steps;
255
256 if (sd->object_flag & SD_OBJECT_HAS_VERTEX_MOTION) {
257 /* Motion steps are stored after the center position in the dedicated position arrays. */
258 int offset = pos_offset + numverts;
259 *motion_pre = primitive_motion_position(kg, sd, offset);
260 if (num_motion_steps > 2) {
261 offset += numverts;
262 *motion_post = primitive_motion_position(kg, sd, offset);
263 }
264 else {
265 object_inverse_position_transform(kg, sd, motion_post);
266 }
267 }
268
269 /* object motion. note that depending on the mesh having motion vectors, this
270 * transformation was set match the world/object space of motion_pre/post */
271 Transform tfm;
272
274 *motion_pre = transform_point(&tfm, *motion_pre);
275
277 *motion_post = transform_point(&tfm, *motion_post);
278}
279
280/* Motion vector for motion pass */
281
283 const ccl_private ShaderData *sd)
284{
285 float3 motion_center, motion_pre, motion_post;
286 primitive_motion_data_without_camera(kg, sd, &motion_center, &motion_pre, &motion_post);
287
288 return camera_motion_vector(kg, motion_center, motion_pre, motion_post);
289}
290
291/* Motion vector for denoising backward motion pass */
292
295{
296 Transform tfm;
297 float3 motion_center, motion_pre, motion_post;
298 primitive_motion_data_without_camera(kg, sd, &motion_center, &motion_pre, &motion_post);
299
300 /* Get camera-space vectors for linear depth delta. */
301 tfm = kernel_data.cam.worldtocamera;
302 float3 motion_center_cam = transform_point(&tfm, motion_center);
303 tfm = kernel_data.cam.motion_pass_pre;
304 float3 motion_pre_cam = transform_point(&tfm, motion_pre);
305
306 float4 motion = camera_motion_vector(kg, motion_center, motion_pre, motion_post);
307
308 float linear_depth_delta_pre = motion_pre_cam.z - motion_center_cam.z;
309
310 return make_float3(motion.x, motion.y, linear_depth_delta_pre);
311}
312
313/* Motion vector for reflections */
314
316 const float3 reflector_N,
317 const float3 P)
318{
319 /* See Ray Tracing Gems chapter 32.4.3 Reflection Motion Vectors */
320
321 float3 reflector_to_o = P - reflector_P;
322 float3 N = dot(reflector_N, reflector_to_o) < 0.0f ? -reflector_N : reflector_N;
323 float3 T, B;
324 make_orthonormals(N, &T, &B);
325
326 float3 P_o = to_local(reflector_to_o, T, B, N);
327
328 float z_o = max(P_o.z, 1e-5f);
329 float x_o = P_o.x;
330 float y_o = P_o.y;
331 const float inverse_f = 0.0f;
332 float z_i = 1.0f / (inverse_f - 1.0f / z_o);
333 float x_i = -(z_i / z_o) * x_o;
334 float y_i = -(z_i / z_o) * y_o;
335
336 float3 P_i = reflector_P + to_global(make_float3(x_i, y_i, z_i), T, B, N);
337 return P_i;
338}
339
341 const float3 reflector_P,
342 const float3 reflector_N,
343 const ccl_private ShaderData *sd)
344{
345 float3 motion_center, motion_pre, motion_post;
346 primitive_motion_data_without_camera(kg, sd, &motion_center, &motion_pre, &motion_post);
347
348 /* TODO: This does not handle cases where the reflector is moving. */
349 motion_center = project_reflection(reflector_P, reflector_N, motion_center);
350 motion_pre = project_reflection(reflector_P, reflector_N, motion_pre);
351 motion_post = project_reflection(reflector_P, reflector_N, motion_post);
352
353 return camera_motion_vector(kg, motion_center, motion_pre, motion_post);
354}
355
btScalar max
#define kernel_data
#define ccl_device_forceinline
#define kernel_data_fetch(name, index)
#define PRIMITIVE_UNPACK_SEGMENT(type)
#define ccl_private
#define ccl_device_inline
#define ccl_global
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
ccl_device_inline float3 triangle_face_normal_undisplaced(KernelGlobals kg, ccl_private const ShaderData *sd, const int position_attr_offset)
CCL_NAMESPACE_BEGIN ccl_device_inline T triangle_interpolate(const float u, const float v, const T f0, const T f1, const T f2)
ccl_device T triangle_attribute(KernelGlobals kg, const ccl_private ShaderData *sd, const AttributeDescriptor desc)
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
float3 transform_point(float3x3 mat, float3 point)
VecT safe_normalize(VecT vector)
ccl_device_forceinline float4 camera_motion_vector(KernelGlobals kg, float3 motion_center, float3 motion_pre, float3 motion_post)
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_normal_transform(KernelGlobals kg, const ccl_private ShaderData *sd, ccl_private T *N)
ccl_device_inline Transform object_fetch_motion_pass_transform(KernelGlobals kg, const int object, enum ObjectVectorTransform type)
ccl_device_inline void object_inverse_position_transform(KernelGlobals kg, const ccl_private ShaderData *sd, ccl_private T *P)
ccl_device_inline void object_position_transform(KernelGlobals kg, const ccl_private ShaderData *sd, ccl_private T *P)
@ OBJECT_PASS_MOTION_PRE
@ OBJECT_PASS_MOTION_POST
@ SR_BACKFACING
@ PRIMITIVE_VOLUME
@ PRIMITIVE_CURVE
@ PRIMITIVE_ANY_POINT
@ PRIMITIVE_TRIANGLE
@ SD_OBJECT_TRANSFORM_APPLIED
@ SD_OBJECT_HAS_VERTEX_MOTION
@ SHADER_SMOOTH_NORMAL
@ ATTR_ELEMENT_OBJECT
@ ATTR_ELEMENT_MESH
@ ATTR_STD_UV
@ ATTR_STD_PTEX_FACE_ID
@ ATTR_STD_NORMAL_UNDISPLACED
@ ATTR_STD_PTEX_UV
@ ATTR_STD_GENERATED
ccl_device_inline T make_zero()
Definition math_dual.h:20
ccl_device_inline void make_orthonormals(const float3 N, ccl_private float3 *a, ccl_private float3 *b)
#define N
#define T
#define B
ccl_device_forceinline float3 primitive_motion_vector_backward_depth_delta(KernelGlobals kg, const ccl_private ShaderData *sd)
Definition primitive.h:294
ccl_device_forceinline float3 primitive_uv(KernelGlobals kg, const ccl_private ShaderData *sd)
Definition primitive.h:111
ccl_device_forceinline float4 primitive_motion_vector(KernelGlobals kg, const ccl_private ShaderData *sd)
Definition primitive.h:282
ccl_device_forceinline float3 project_reflection(const float3 reflector_P, const float3 reflector_N, const float3 P)
Definition primitive.h:315
ccl_device void primitive_normal_set_undisplaced(KernelGlobals kg, ccl_private ShaderData *sd, const int position_undisplaced_offset)
Definition primitive.h:63
ccl_device Float3Type primitive_tangent(KernelGlobals kg, ccl_private ShaderData *sd)
Definition primitive.h:150
ccl_device_forceinline float4 primitive_motion_vector_reflection(KernelGlobals kg, const float3 reflector_P, const float3 reflector_N, const ccl_private ShaderData *sd)
Definition primitive.h:340
CCL_NAMESPACE_BEGIN ccl_device_forceinline T primitive_surface_attribute(KernelGlobals kg, const ccl_private ShaderData *sd, const AttributeDescriptor desc)
Definition primitive.h:32
ccl_device_inline float3 primitive_motion_position(KernelGlobals kg, const ccl_private ShaderData *sd, const int offset)
Definition primitive.h:189
ccl_device bool primitive_ptex(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float2 *uv, ccl_private int *face_id)
Definition primitive.h:125
ccl_device_forceinline void primitive_motion_data_without_camera(KernelGlobals kg, const ccl_private ShaderData *sd, ccl_private float3 *motion_center, ccl_private float3 *motion_pre, ccl_private float3 *motion_post)
Definition primitive.h:215
#define mix
#define ccl_device
#define make_float2
AttributeElement element
float x
float y
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 x
Definition sky_math.h:225
uint y
Definition types_uint3.h:13
uint z
Definition types_uint3.h:13
uint x
Definition types_uint3.h:13
ccl_device_inline float2 to_local(const T p, const T X, const T Y)
Definition transform.h:33
CCL_NAMESPACE_BEGIN ccl_device_inline T to_global(const float2 p, const T X, const T Y)
Definition transform.h:21
ccl_static_constexpr bool is_dual_v
Definition types_dual.h:118
dual< float3 > dual3
Definition types_dual.h:106
typename dual_base_type< T >::type dual_base_t
Definition types_dual.h:128
dual< float > dual1
Definition types_dual.h:104