Blender V5.3
kernel/svm/attribute.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
10#include "kernel/geom/gsplat.h"
11#include "kernel/geom/object.h"
13#include "kernel/geom/volume.h"
14
16#include "kernel/svm/util.h"
17
19
20/* Attribute Node */
21
23 ccl_private ShaderData *sd,
26{
27 *type = node.output_type;
28
30
31 if (sd->object != OBJECT_NONE) {
32 desc = find_attribute(kg, sd, node.attr);
33 if (!is_attribute_found(desc)) {
34 desc = attribute_not_found();
35 desc.type = (NodeAttributeType)node.output_type;
36 }
37 }
38 else {
39 /* background */
40 desc = attribute_not_found();
41 desc.type = (NodeAttributeType)node.output_type;
42 }
43
44 return desc;
45}
46
47/* Store attribute to the stack. Float3Type is float3 or dual3. */
48
49template<typename Float3Type>
51 ccl_private float *stack,
52 const uint out_offset,
53 const ccl_private Float3Type &f)
54{
55 using FloatType = dual_scalar_t<Float3Type>;
56 if (type == NODE_ATTR_OUTPUT_FLOAT3) {
57 stack_store(stack, out_offset, f);
58 }
59 else {
60 stack_store(stack, out_offset, FloatType(average(f)));
61 }
62}
63
64/* Core surface attribute evaluation, returning Float3Type = float3 or dual3.
65 * Fetches the attribute, applies output type conversion (float3 or scalar-as-float3),
66 * and computes derivatives when Float3Type is a dual type. */
67
68template<typename Float3Type>
69ccl_device_inline Float3Type
71 ccl_private ShaderData *sd,
72 ccl_private float *ccl_restrict stack,
74 const NodeAttributeOutputType type,
75 const AttributeDescriptor desc)
76{
77 using FloatType = dual_scalar_t<Float3Type>;
78
79#if defined(__GSPLATS__)
80 if (sd->type & PRIMITIVE_GSPLAT && node.attr == ATTR_STD_GSPLAT_RADIANCE &&
81 !is_attribute_found(desc))
82 {
83 if (type == NODE_ATTR_OUTPUT_FLOAT_ALPHA) {
84 return make_float3(FloatType(1.0f));
85 }
86 const float3 radiance = gsplat_radiance(kg, *sd);
87 return Float3Type(radiance);
88 }
89#endif
90
91 /* Spherical harmonics attribute can not be currently accessed.
92 * It is stored as PackedSphericalHarmonics that does not have a float or float3 representation.
93 */
95 const float value = (type == NODE_ATTR_OUTPUT_FLOAT_ALPHA) ? 1.0f : 0.0f;
96 return make_float3(FloatType(value));
97 }
98
99 if (sd->type == PRIMITIVE_LAMP && node.attr == ATTR_STD_UV) {
100 Float3Type uv(make_float3(1.0f - sd->u - sd->v, sd->u, 0.0f));
101 if constexpr (is_dual_v<Float3Type>) {
102 uv.dx = make_float3(-sd->du.dx - sd->dv.dx, sd->du.dx, 0.0f);
103 uv.dy = make_float3(-sd->du.dy - sd->dv.dy, sd->du.dy, 0.0f);
104 }
105 return uv;
106 }
107
108 if (node.attr == ATTR_STD_GENERATED && !is_attribute_found(desc)) {
109 Float3Type f = shading_position<Float3Type>(sd);
111 return f;
112 }
113
114 if (!is_attribute_found(desc)) {
115 if (type == NODE_ATTR_OUTPUT_FLOAT_ALPHA) {
116 return make_float3(FloatType(stack_load(stack, node.missing_alpha)));
117 }
118 return Float3Type(stack_load(stack, node.missing));
119 }
120
121 /* Surface attribute fetch with output type conversion. */
122 if (desc.type == NODE_ATTR_FLOAT) {
123 FloatType f = primitive_surface_attribute<FloatType>(kg, sd, desc);
124 if (type == NODE_ATTR_OUTPUT_FLOAT_ALPHA) {
125 return make_float3(FloatType(1.0f));
126 }
127 return make_float3(f, f, f);
128 }
129
130 if (desc.type == NODE_ATTR_FLOAT2) {
131 if constexpr (is_dual_v<Float3Type>) {
133 if (type == NODE_ATTR_OUTPUT_FLOAT) {
134 return make_float3(f.x());
135 }
136 if (type == NODE_ATTR_OUTPUT_FLOAT_ALPHA) {
137 return make_float3(FloatType(1.0f));
138 }
139 return make_float3(f);
140 }
141 else {
143 if (type == NODE_ATTR_OUTPUT_FLOAT) {
144 return make_float3(f.x);
145 }
146 if (type == NODE_ATTR_OUTPUT_FLOAT_ALPHA) {
147 return make_float3(FloatType(1.0f));
148 }
149 return make_float3(f);
150 }
151 }
152
153 if (desc.type == NODE_ATTR_FLOAT4 || desc.type == NODE_ATTR_RGBA) {
154 if constexpr (is_dual_v<Float3Type>) {
156 if (type == NODE_ATTR_OUTPUT_FLOAT) {
157 return make_float3(average(make_float3(f)));
158 }
159 if (type == NODE_ATTR_OUTPUT_FLOAT_ALPHA) {
160 return make_float3(f.w());
161 }
162 return make_float3(f);
163 }
164 else {
166 if (type == NODE_ATTR_OUTPUT_FLOAT) {
167 return make_float3(average(make_float3(f)));
168 }
169 if (type == NODE_ATTR_OUTPUT_FLOAT_ALPHA) {
170 return make_float3(f.w);
171 }
172 return make_float3(f);
173 }
174 }
175
176 if (desc.type == NODE_ATTR_QUATERNION) {
177 /* Conversion from quaternion to RGB is not well defined.
178 * Follow the same logic as what it was when quaternions were stored as float4. */
179 if constexpr (is_dual_v<Float3Type>) {
181 if (type == NODE_ATTR_OUTPUT_FLOAT) {
182 return make_float3(average(make_float3(q)));
183 }
184 if (type == NODE_ATTR_OUTPUT_FLOAT_ALPHA) {
185 /* Use make_float4, which is used for the non-dual case and swizzles components. */
186 return make_float3(
188 }
189 return make_float3(q);
190 }
191 else {
193 const float4 f = make_float4(q);
194 if (type == NODE_ATTR_OUTPUT_FLOAT) {
195 return make_float3(average(make_float3(f)));
196 }
197 if (type == NODE_ATTR_OUTPUT_FLOAT_ALPHA) {
198 return make_float3(f.w);
199 }
200 return make_float3(f);
201 }
202 }
203
204 Float3Type f = primitive_surface_attribute<Float3Type>(kg, sd, desc);
205 if (type == NODE_ATTR_OUTPUT_FLOAT) {
206 return make_float3(average(f));
207 }
208 if (type == NODE_ATTR_OUTPUT_FLOAT_ALPHA) {
209 return make_float3(FloatType(1.0f));
210 }
211 return f;
212}
213
214/* Surface attribute node. */
216 ccl_private ShaderData *sd,
217 ccl_private float *ccl_restrict stack,
219{
221 const AttributeDescriptor desc = svm_node_attr_init(kg, sd, node, &type);
222
223 float3 data = svm_node_attr_surface_eval<float3>(kg, sd, stack, node, type, desc);
224 svm_node_attr_store(type, stack, node.out_offset, data);
225}
226
227/* Evaluate surface attributes with derivatives and optional bump offset.
228 * Used for derivative tracking and bump mapping. */
229
231 ccl_private ShaderData *sd,
232 ccl_private float *ccl_restrict stack,
234{
236 const AttributeDescriptor desc = svm_node_attr_init(kg, sd, node, &type);
237
238 dual3 data = svm_node_attr_surface_eval<dual3>(kg, sd, stack, node, type, desc);
239 if (node.bump_offset == NODE_BUMP_OFFSET_DX) {
240 data.val += data.dx * node.bump_filter_width;
241 }
242 else if (node.bump_offset == NODE_BUMP_OFFSET_DY) {
243 data.val += data.dy * node.bump_filter_width;
244 }
245
246 if (node.store_derivatives) {
247 svm_node_attr_store(type, stack, node.out_offset, data);
248 }
249 else {
250 svm_node_attr_store(type, stack, node.out_offset, float3(data.val));
251 }
252}
253
254#ifdef __VOLUME__
255/* Volume attribute node. Volumes have no derivatives or bump. */
256ccl_device_noinline void svm_node_attr_volume(KernelGlobals kg,
257 ccl_private ShaderData *sd,
258 ccl_private float *ccl_restrict stack,
260{
261 kernel_assert(primitive_is_volume_attribute(sd));
262
264 const AttributeDescriptor desc = svm_node_attr_init(kg, sd, node, &type);
265 if (!is_attribute_found(desc)) {
266 if (type == NODE_ATTR_OUTPUT_FLOAT) {
267 const float3 missing = stack_load(stack, node.missing);
268 stack_store_float(stack, node.out_offset, average(missing));
269 }
270 else if (type == NODE_ATTR_OUTPUT_FLOAT3) {
271 const float3 missing = stack_load(stack, node.missing);
272 stack_store_float3(stack, node.out_offset, missing);
273 }
274 else {
275 const float f = stack_load(stack, node.missing_alpha);
276 stack_store_float(stack, node.out_offset, f);
277 }
278 return;
279 }
280
281 const bool stochastic_sample = __float_as_uint(node.bump_filter_width);
282 const float4 value = volume_attribute_float4(kg, sd, desc, stochastic_sample);
283
284 if (type == NODE_ATTR_OUTPUT_FLOAT) {
285 stack_store_float(stack, node.out_offset, volume_attribute_value<float>(value));
286 }
287 else if (type == NODE_ATTR_OUTPUT_FLOAT3) {
288 stack_store_float3(stack, node.out_offset, volume_attribute_value<float3>(value));
289 }
290 else {
291 stack_store_float(stack, node.out_offset, volume_attribute_alpha(value));
292 }
293}
294#endif
295
ccl_device_inline void stack_store_float(ccl_private float *stack, const uint a, const float f)
ccl_device_inline void stack_store_float3(ccl_private float *stack, const uint a, const float3 f)
ccl_device_inline void stack_store(ccl_private float *stack, const uint a, const float f)
ccl_device_inline Float3Type shading_position(const ccl_private ShaderData *sd)
ccl_device_inline T stack_load(const ccl_private float *stack, const uint a)
#define kernel_assert(cond)
#define ccl_restrict
#define OBJECT_NONE
#define ccl_private
#define ccl_device_inline
#define ccl_global
#define ccl_device_noinline
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define __float_as_uint(x)
float average(float2 a)
ccl_device_inline AttributeDescriptor attribute_not_found()
CCL_NAMESPACE_BEGIN ccl_device_forceinline bool is_attribute_found(const ccl_private AttributeDescriptor &desc)
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_inverse_position_transform_if_object(KernelGlobals kg, const ccl_private ShaderData *sd, ccl_private Float3Type *P)
ccl_device_noinline void svm_node_attr_surface(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *ccl_restrict stack, const ccl_global SVMNodeAttr &ccl_restrict node)
CCL_NAMESPACE_BEGIN ccl_device AttributeDescriptor svm_node_attr_init(KernelGlobals kg, ccl_private ShaderData *sd, const ccl_global SVMNodeAttr &ccl_restrict node, ccl_private NodeAttributeOutputType *type)
ccl_device_noinline void svm_node_attr_derivative(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *ccl_restrict stack, const ccl_global SVMNodeAttr &ccl_restrict node)
ccl_device_inline Float3Type svm_node_attr_surface_eval(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *ccl_restrict stack, const ccl_global SVMNodeAttr &ccl_restrict node, const NodeAttributeOutputType type, const AttributeDescriptor desc)
ccl_device_inline void svm_node_attr_store(const NodeAttributeOutputType type, ccl_private float *stack, const uint out_offset, const ccl_private Float3Type &f)
NodeAttributeType
@ NODE_ATTR_FLOAT
@ NODE_ATTR_QUATERNION
@ NODE_ATTR_RGBA
@ NODE_ATTR_FLOAT2
@ NODE_ATTR_SPHERICAL_HARMONICS_REST
@ NODE_ATTR_FLOAT4
NodeAttributeOutputType
@ NODE_ATTR_OUTPUT_FLOAT
@ NODE_ATTR_OUTPUT_FLOAT_ALPHA
@ NODE_ATTR_OUTPUT_FLOAT3
@ NODE_BUMP_OFFSET_DY
@ NODE_BUMP_OFFSET_DX
@ PRIMITIVE_LAMP
@ PRIMITIVE_GSPLAT
@ ATTR_STD_UV
@ ATTR_STD_GSPLAT_RADIANCE
@ ATTR_STD_GENERATED
CCL_NAMESPACE_BEGIN ccl_device_forceinline T primitive_surface_attribute(KernelGlobals kg, const ccl_private ShaderData *sd, const AttributeDescriptor desc)
Definition primitive.h:32
#define ccl_device
#define make_float4
NodeAttributeType type
ccl_device_inline_method dual< float > x() const
Definition types_dual.h:42
ccl_device_inline_method dual< float > w() const
Definition types_dual.h:98
T val
Definition types_dual.h:14
float x
float w
Definition sky_math.h:225
ccl_static_constexpr bool is_dual_v
Definition types_dual.h:118
dual< float4 > dual4
Definition types_dual.h:107
typename dual_scalar_type< T >::type dual_scalar_t
Definition types_dual.h:141
dual< float3 > dual3
Definition types_dual.h:106
dual< float2 > dual2
Definition types_dual.h:105