Blender V4.5
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"
9
10#include "util/color.h"
11
13
14/* Attributes
15 *
16 * We support an arbitrary number of attributes on various mesh elements.
17 * On vertices, triangles, curve keys, curves, meshes and volume grids.
18 * Most of the code for attribute reading is in the primitive files.
19 *
20 * Lookup of attributes is different between OSL and SVM, as OSL is ustring
21 * based while for SVM we use integer ids. */
22
28
29/* Find attribute based on ID */
30
32{
33 return kernel_data_fetch(objects, object).attribute_map_offset;
34}
35
37 const int object,
38 const int prim,
39 const uint64_t id)
40{
41 if (object == OBJECT_NONE) {
42 return attribute_not_found();
43 }
44
45 /* for SVM, find attribute by unique id */
46 uint attr_offset = object_attribute_map_offset(kg, object);
47 AttributeMap attr_map = kernel_data_fetch(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 attribute_not_found();
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 = kernel_data_fetch(attributes_map, attr_offset);
61 }
62
64 desc.element = (AttributeElement)attr_map.element;
65
66 if (prim == PRIM_NONE && desc.element != ATTR_ELEMENT_MESH &&
68 {
69 return attribute_not_found();
70 }
71
72 /* return result */
73 desc.offset = (attr_map.element == ATTR_ELEMENT_NONE) ? (int)ATTR_STD_NOT_FOUND :
74 attr_map.offset;
75 desc.type = (NodeAttributeType)attr_map.type;
76
77 return desc;
78}
79
81 const ccl_private ShaderData *sd,
82 const uint64_t id)
83{
84 return find_attribute(kg, sd->object, sd->prim, id);
85}
86
87/* Templated functions to read from the attribute data */
88template<typename T> ccl_device_inline T attribute_data_fetch(KernelGlobals kg, int offset);
89
91{
92 return kernel_data_fetch(attributes_float, offset);
93}
94
96{
97 return kernel_data_fetch(attributes_float2, offset);
98}
99
101{
102 return kernel_data_fetch(attributes_float3, offset);
103}
104
106{
107 return kernel_data_fetch(attributes_float4, offset);
108}
109
111{
112 return kernel_data_fetch(attributes_uchar4, offset);
113}
114
115/* ATTR_ELEMENT_CORNER_BYTE is stored as uchar4, but has to be converted to float4.
116 * We don't support it for float/float2/float3. */
117template<typename T>
123
129
131{
132 Transform tfm;
133
134 tfm.x = kernel_data_fetch(attributes_float4, offset + 0);
135 tfm.y = kernel_data_fetch(attributes_float4, offset + 1);
136 tfm.z = kernel_data_fetch(attributes_float4, offset + 2);
137
138 return tfm;
139}
140
141/* Transform matrix attribute on meshes */
142
147
unsigned int uint
#define UNLIKELY(x)
unsigned long long int uint64_t
ccl_device_inline float4 color_uchar4_to_float4(const uchar4 c)
Definition color.h:57
ccl_device float4 color_srgb_to_linear_v4(const float4 c)
Definition color.h:347
#define kernel_assert(cond)
#define kernel_data_fetch(name, index)
#define PRIM_NONE
#define ccl_device
#define OBJECT_NONE
#define ccl_private
const ThreadKernelGlobalsCPU * KernelGlobals
#define ccl_device_inline
#define ccl_device_template_spec
#define CCL_NAMESPACE_END
VecBase< float, 4 > float4
ccl_device_inline uint object_attribute_map_offset(KernelGlobals kg, const int object)
CCL_NAMESPACE_BEGIN ccl_device_inline AttributeDescriptor attribute_not_found()
ccl_device Transform primitive_attribute_matrix(KernelGlobals kg, const AttributeDescriptor desc)
ccl_device_inline AttributeDescriptor find_attribute(KernelGlobals kg, const int object, const int prim, const uint64_t id)
ccl_device_inline T attribute_data_fetch(KernelGlobals kg, int offset)
ccl_device_inline T attribute_data_fetch_bytecolor(KernelGlobals kg, int offset)
NodeAttributeType
@ ATTR_STD_NOT_FOUND
@ ATTR_STD_NONE
AttributeElement
@ ATTR_ELEMENT_NONE
@ ATTR_ELEMENT_VOXEL
@ ATTR_ELEMENT_OBJECT
@ ATTR_ELEMENT_MESH
@ ATTR_PRIM_TYPES
ccl_device_inline T make_zero()
#define T
AttributeElement element
NodeAttributeType type
uint16_t element
float4 y
Definition transform.h:23
float4 x
Definition transform.h:23
float4 z
Definition transform.h:23