Blender V5.3
kernel/geom/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#include "kernel/types.h"
10
11#include "util/color.h"
12
14
15/* Attributes
16 *
17 * We support an arbitrary number of attributes on various mesh elements.
18 * On vertices, triangles, curve keys, curves, meshes and volume grids.
19 * Most of the code for attribute reading is in the primitive files.
20 *
21 * Lookup of attributes is different between OSL and SVM, as OSL is ustring
22 * based while for SVM we use integer ids. */
23
28
34
35/* Find attribute based on ID */
36
38{
39 return kernel_data_fetch(objects, object).attribute_map_offset;
40}
41
43 ccl_private uint &attr_offset,
44 const uint64_t id)
45{
46 /* For SVM, find attribute by unique id. */
47 AttributeMap attr_map = attributes_map[attr_offset];
48
49 while (attr_map.id != id) {
50 if (UNLIKELY(attr_map.id == ATTR_STD_NONE)) {
51 if (UNLIKELY(attr_map.element == 0)) {
52 return false;
53 }
54 /* Chain jump to a different part of the table. */
55 attr_offset = attr_map.offset;
56 }
57 else {
58 attr_offset += ATTR_PRIM_TYPES;
59 }
60 attr_map = attributes_map[attr_offset];
61 }
62
63 return true;
64}
65
67 uint attr_offset,
68 const int prim,
69 const uint64_t id)
70{
71 if (!find_attr_offset(attributes_map, attr_offset, id)) {
72 return attribute_not_found();
73 }
74
75 const AttributeMap attr_map = attributes_map[attr_offset];
76
78 desc.element = (AttributeElement)attr_map.element;
79
80 if (prim == PRIM_NONE &&
82 {
83 return attribute_not_found();
84 }
85
86 /* return result */
87 desc.offset = (attr_map.element == ATTR_ELEMENT_NONE) ? (int)ATTR_STD_NOT_FOUND :
88 attr_map.offset;
89 desc.type = (NodeAttributeType)attr_map.type;
90
91 return desc;
92}
93
95 const int object,
96 const int prim,
97 const uint64_t id)
98{
99 if (object == OBJECT_NONE) {
100 return attribute_not_found();
101 }
102
103 return find_attribute(
104 &kernel_data_fetch(attributes_map, 0), object_attribute_map_offset(kg, object), prim, id);
105}
106
108 const ccl_private ShaderData *sd,
109 const uint64_t id)
110{
111 return find_attribute(kg, sd->object, sd->prim, id);
112}
113
114/* Templated functions to read from the attribute data */
115template<typename T>
117
119 AttributeElement /*element*/,
120 int offset)
121{
122 return kernel_data_fetch(attributes_float, offset);
123}
124
126 AttributeElement /*element*/,
127 int offset)
128{
129 return kernel_data_fetch(attributes_float2, offset);
130}
131
133 AttributeElement element,
134 int offset)
135{
136 if (element & ATTR_ELEMENT_IS_NORMAL) {
137 const packed_normal normal = kernel_data_fetch(attributes_normal, offset);
138 return normal.decode();
139 }
140 return kernel_data_fetch(attributes_float3, offset);
141}
142
144 AttributeElement element,
145 int offset)
146{
147 if (element & ATTR_ELEMENT_IS_BYTE) {
148 const float4 rec709 = color_srgb_to_linear_v4(
149 color_uchar4_to_float4(kernel_data_fetch(attributes_uchar4, offset)));
150 return make_float4(rec709_to_rgb(kg, make_float3(rec709)), rec709.w);
151 }
152 return kernel_data_fetch(attributes_float4, offset);
153}
154
156{
157 const packed_normal normal = kernel_data_fetch(attributes_normal, offset);
158 return normal.decode();
159}
160
162 const int offset,
163 const int i0,
164 const int i1,
165 const int i2,
167{
168#ifndef __KERNEL_GPU__
169 float4 nx, ny, nz;
170 const int4 packed_values = make_int4(kernel_data_fetch(attributes_normal, offset + i0).value,
171 kernel_data_fetch(attributes_normal, offset + i1).value,
172 kernel_data_fetch(attributes_normal, offset + i2).value,
173 0);
174 packed_normal_decode_simd(packed_values, nx, ny, nz);
175 N[0] = make_float3(nx.x, ny.x, nz.x);
176 N[1] = make_float3(nx.y, ny.y, nz.y);
177 N[2] = make_float3(nx.z, ny.z, nz.z);
178#else
179 N[0] = attribute_data_fetch_normal(kg, offset + i0);
180 N[1] = attribute_data_fetch_normal(kg, offset + i1);
181 N[2] = attribute_data_fetch_normal(kg, offset + i2);
182#endif
183}
184
186 const int offset,
187 const int i0,
188 const int i1,
189 const int i2,
190 const float u,
191 const float v)
192{
193#ifndef __KERNEL_GPU__
194 float4 nx, ny, nz;
195 const int4 packed_values = make_int4(kernel_data_fetch(attributes_normal, offset + i0).value,
196 kernel_data_fetch(attributes_normal, offset + i1).value,
197 kernel_data_fetch(attributes_normal, offset + i2).value,
198 0);
199 packed_normal_decode_simd(packed_values, nx, ny, nz);
200
201 const float4 weights = make_float4(1.0f - u - v, u, v, 0.0f);
202 return make_float3(dot(nx, weights), dot(ny, weights), dot(nz, weights));
203#else
204 const float3 n0 = attribute_data_fetch_normal(kg, offset + i0);
205 const float3 n1 = attribute_data_fetch_normal(kg, offset + i1);
206 const float3 n2 = attribute_data_fetch_normal(kg, offset + i2);
207 return (1.0f - u - v) * n0 + u * n1 + v * n2;
208#endif
209}
210
211#ifdef __KERNEL_METAL__
212template<typename U, typename V> using attribute_data_type_is_same = metal::is_same<U, V>;
213#else
214template<typename U, typename V> using attribute_data_type_is_same = std::is_same<U, V>;
215#endif
216
217template<typename T>
219 const AttributeElement element,
220 const int offset,
221 const int i0,
222 const int i1,
223 const int i2,
224 ccl_private T f[3])
225{
227 if (element & ATTR_ELEMENT_IS_NORMAL) {
228 attribute_data_fetch_normals(kg, offset, i0, i1, i2, f);
229 }
230 else {
231 f[0] = kernel_data_fetch(attributes_float3, offset + i0);
232 f[1] = kernel_data_fetch(attributes_float3, offset + i1);
233 f[2] = kernel_data_fetch(attributes_float3, offset + i2);
234 }
235 }
236 else {
237 f[0] = attribute_data_fetch<T>(kg, element, offset + i0);
238 f[1] = attribute_data_fetch<T>(kg, element, offset + i1);
239 f[2] = attribute_data_fetch<T>(kg, element, offset + i2);
240 }
241}
242
244
245 AttributeElement /*element*/,
246 int offset)
247{
248 Transform tfm;
249
250 tfm.x = kernel_data_fetch(attributes_float4, offset + 0);
251 tfm.y = kernel_data_fetch(attributes_float4, offset + 1);
252 tfm.z = kernel_data_fetch(attributes_float4, offset + 2);
253
254 return tfm;
255}
256
258 AttributeElement /*element*/,
259 const int offset)
260{
261 return kernel_data_fetch(attributes_quaternion, offset);
262}
263
264/* Transform matrix attribute on meshes */
265
270
#define UNLIKELY(x)
unsigned long long int uint64_t
ccl_device_inline float4 color_uchar4_to_float4(const uchar4 c)
Definition color.h:45
ccl_device float4 color_srgb_to_linear_v4(const float4 c)
Definition color.h:335
#define ccl_device_forceinline
#define kernel_data_fetch(name, index)
#define PRIM_NONE
#define OBJECT_NONE
#define ccl_private
#define ccl_device_inline
#define ccl_device_template_spec
#define ccl_global
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
ccl_device_forceinline int4 make_int4(const int x, const int y, const int z, const int w)
float dot(VecOp< float, D >, VecOp< float, D >) RET
ccl_device_inline uint object_attribute_map_offset(KernelGlobals kg, const int object)
ccl_device_inline AttributeDescriptor attribute_not_found()
ccl_device_inline void attribute_data_fetch_normals(KernelGlobals kg, const int offset, const int i0, const int i1, const int i2, ccl_private float3 N[3])
ccl_device_inline void attribute_data_fetch_3(KernelGlobals kg, const AttributeElement element, const int offset, const int i0, const int i1, const int i2, ccl_private T f[3])
CCL_NAMESPACE_BEGIN ccl_device_forceinline bool is_attribute_found(const ccl_private AttributeDescriptor &desc)
std::is_same< U, V > attribute_data_type_is_same
ccl_device Transform primitive_attribute_matrix(KernelGlobals kg, const AttributeDescriptor desc)
ccl_device_inline T attribute_data_fetch(KernelGlobals kg, AttributeElement element, int offset)
ccl_device_inline float3 attribute_data_interpolate_normals(KernelGlobals kg, const int offset, const int i0, const int i1, const int i2, const float u, const float v)
ccl_device_inline float3 attribute_data_fetch_normal(KernelGlobals kg, 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 bool find_attr_offset(const ccl_global AttributeMap *attributes_map, ccl_private uint &attr_offset, const uint64_t id)
NodeAttributeType
AttributeElement
@ ATTR_ELEMENT_NONE
@ ATTR_ELEMENT_VOXEL
@ ATTR_ELEMENT_OBJECT
@ ATTR_ELEMENT_IS_BYTE
@ ATTR_ELEMENT_IS_NORMAL
@ ATTR_ELEMENT_MESH
@ ATTR_PRIM_TYPES
@ ATTR_STD_NOT_FOUND
@ ATTR_STD_NONE
ccl_device float3 rec709_to_rgb(KernelGlobals kg, const float3 rec709)
#define N
#define T
#define ccl_device
#define make_float4
AttributeElement element
NodeAttributeType type
uint16_t element
float4 y
Definition transform.h:53
float4 x
Definition transform.h:53
float4 z
Definition transform.h:53
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225
ccl_device_inline_method float3 decode() const
ccl_device_inline void packed_normal_decode_simd(const int4 value, ccl_private float4 &nx, ccl_private float4 &ny, ccl_private float4 &nz)