Blender V5.0
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
29
30/* Find attribute based on ID */
31
33{
34 return kernel_data_fetch(objects, object).attribute_map_offset;
35}
36
38 const int object,
39 const int prim,
40 const uint64_t id)
41{
42 if (object == OBJECT_NONE) {
43 return attribute_not_found();
44 }
45
46 /* for SVM, find attribute by unique id */
47 uint attr_offset = object_attribute_map_offset(kg, object);
48 AttributeMap attr_map = kernel_data_fetch(attributes_map, attr_offset);
49
50 while (attr_map.id != id) {
51 if (UNLIKELY(attr_map.id == ATTR_STD_NONE)) {
52 if (UNLIKELY(attr_map.element == 0)) {
53 return attribute_not_found();
54 }
55 /* Chain jump to a different part of the table. */
56 attr_offset = attr_map.offset;
57 }
58 else {
59 attr_offset += ATTR_PRIM_TYPES;
60 }
61 attr_map = kernel_data_fetch(attributes_map, attr_offset);
62 }
63
65 desc.element = (AttributeElement)attr_map.element;
66
67 if (prim == PRIM_NONE && desc.element != ATTR_ELEMENT_MESH &&
69 {
70 return attribute_not_found();
71 }
72
73 /* return result */
74 desc.offset = (attr_map.element == ATTR_ELEMENT_NONE) ? (int)ATTR_STD_NOT_FOUND :
75 attr_map.offset;
76 desc.type = (NodeAttributeType)attr_map.type;
77
78 return desc;
79}
80
82 const ccl_private ShaderData *sd,
83 const uint64_t id)
84{
85 return find_attribute(kg, sd->object, sd->prim, id);
86}
87
88/* Templated functions to read from the attribute data */
89template<typename T> ccl_device_inline T attribute_data_fetch(KernelGlobals kg, int offset);
90
92{
93 return kernel_data_fetch(attributes_float, offset);
94}
95
97{
98 return kernel_data_fetch(attributes_float2, offset);
99}
100
102{
103 return kernel_data_fetch(attributes_float3, offset);
104}
105
107{
108 return kernel_data_fetch(attributes_float4, offset);
109}
110
112{
113 return kernel_data_fetch(attributes_uchar4, offset);
114}
115
116/* ATTR_ELEMENT_CORNER_BYTE is stored as uchar4, but has to be converted to float4.
117 * We don't support it for float/float2/float3. */
118template<typename T>
120{
121 kernel_assert(false);
122 return make_zero<T>();
123}
124
126{
127 const float4 rec709 = color_srgb_to_linear_v4(
128 color_uchar4_to_float4(kernel_data_fetch(attributes_uchar4, offset)));
129 return make_float4(rec709_to_rgb(kg, make_float3(rec709)), rec709.w);
130}
131
133{
134 Transform tfm;
135
136 tfm.x = kernel_data_fetch(attributes_float4, offset + 0);
137 tfm.y = kernel_data_fetch(attributes_float4, offset + 1);
138 tfm.z = kernel_data_fetch(attributes_float4, offset + 2);
139
140 return tfm;
141}
142
143/* Transform matrix attribute on meshes */
144
149
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 OBJECT_NONE
#define ccl_private
const ThreadKernelGlobalsCPU * KernelGlobals
#define ccl_device_inline
#define ccl_device_template_spec
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
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_bytecolor(KernelGlobals, int)
ccl_device_inline T attribute_data_fetch(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 float3 rec709_to_rgb(KernelGlobals kg, const float3 rec709)
ccl_device_inline T make_zero()
Definition math_dual.h:17
#define T
#define ccl_device
#define make_float4
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
float w
Definition sky_math.h:225