Blender V5.0
scene/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 "scene/image.h"
8
9#include "kernel/types.h"
10
11#include "util/list.h"
12#include "util/param.h"
13#include "util/set.h"
14#include "util/types.h"
15#include "util/vector.h"
16
18
19class Attribute;
22class AttributeSet;
23class ImageHandle;
24class Geometry;
25class Hair;
26class Mesh;
27struct Transform;
28
29/* AttrKernelDataType.
30 *
31 * The data type of the device arrays storing the attribute's data. Those data types are different
32 * than the ones for attributes as some attribute types are stored in the same array, e.g. Point,
33 * Vector, and Transform are all stored as float3 in the kernel.
34 *
35 * The values of this enumeration are also used as flags to detect changes in AttributeSet. */
36
37enum AttrKernelDataType { FLOAT = 0, FLOAT2 = 1, FLOAT3 = 2, FLOAT4 = 3, UCHAR4 = 4, NUM = 5 };
38
39/* Attribute
40 *
41 * Arbitrary data layers on meshes.
42 * Supported types: Float, Color, Vector, Normal, Point */
43
44class Attribute {
45 public:
46 ustring name;
48
49 TypeDesc type;
52 uint flags; /* enum AttributeFlag */
53
55
56 Attribute(ustring name,
57 const TypeDesc type,
59 Geometry *geom,
61 Attribute(Attribute &&other) = default;
62 Attribute(const Attribute &other) = delete;
63 Attribute &operator=(const Attribute &other) = delete;
64 ~Attribute();
65
66 void set(ustring name, const TypeDesc type, AttributeElement element);
67 void resize(Geometry *geom, AttributePrimitive prim, bool reserve_only);
68 void resize(const size_t num_elements);
69
70 size_t data_sizeof() const;
71 size_t element_size(Geometry *geom, AttributePrimitive prim) const;
72 size_t buffer_size(Geometry *geom, AttributePrimitive prim) const;
73
74 char *data()
75 {
76 return (!buffer.empty()) ? buffer.data() : nullptr;
77 }
79 {
80 assert(data_sizeof() == sizeof(float2));
81 return (float2 *)data();
82 }
84 {
85 assert(data_sizeof() == sizeof(float3));
86 return (float3 *)data();
87 }
89 {
90 assert(data_sizeof() == sizeof(float4));
91 return (float4 *)data();
92 }
93 float *data_float()
94 {
95 assert(data_sizeof() == sizeof(float));
96 return (float *)data();
97 }
99 {
100 assert(data_sizeof() == sizeof(uchar4));
101 return (uchar4 *)data();
102 }
104 {
105 assert(data_sizeof() == sizeof(Transform));
106 return (Transform *)data();
107 }
108
109 /* Attributes for voxels are images */
111 {
112 assert(data_sizeof() == sizeof(ImageHandle));
113 return *(ImageHandle *)data();
114 }
115
116 const char *data() const
117 {
118 return (!buffer.empty()) ? buffer.data() : nullptr;
119 }
120 const float2 *data_float2() const
121 {
122 assert(data_sizeof() == sizeof(float2));
123 return (const float2 *)data();
124 }
125 const float3 *data_float3() const
126 {
127 assert(data_sizeof() == sizeof(float3));
128 return (const float3 *)data();
129 }
130 const float4 *data_float4() const
131 {
132 assert(data_sizeof() == sizeof(float4));
133 return (const float4 *)data();
134 }
135 const float *data_float() const
136 {
137 assert(data_sizeof() == sizeof(float));
138 return (const float *)data();
139 }
141 {
142 assert(data_sizeof() == sizeof(Transform));
143 return (const Transform *)data();
144 }
145 const ImageHandle &data_voxel() const
146 {
147 assert(data_sizeof() == sizeof(ImageHandle));
148 return *(const ImageHandle *)data();
149 }
150
151 void zero_data(void *dst);
152
153 void add(const float &f);
154 void add(const float2 &f);
155 void add(const float3 &f);
156 void add(const uchar4 &f);
157 void add(const Transform &f);
158 void add(const char *data);
159
160 void set_data_from(Attribute &&other);
161
162 static bool same_storage(const TypeDesc a, const TypeDesc b);
163 static const char *standard_name(AttributeStandard std);
164 static AttributeStandard name_standard(const char *name);
165
166 static AttrKernelDataType kernel_type(const Attribute &attr);
167
168 void get_uv_tiles(Geometry *geom, AttributePrimitive prim, unordered_set<int> &tiles) const;
169};
170
171/* Attribute Set
172 *
173 * Set of attributes on a mesh. */
174
176 uint32_t modified_flag;
177
178 public:
181 list<Attribute> attributes;
182
186
187 Attribute *add(ustring name, const TypeDesc type, AttributeElement element);
188 Attribute *find(ustring name) const;
189 void remove(ustring name);
190
191 Attribute *add(AttributeStandard std, ustring name = ustring());
192 Attribute *find(AttributeStandard std) const;
193 void remove(AttributeStandard std);
194
195 Attribute &copy(const Attribute &attr);
196
198 Attribute *find_matching(const Attribute &other);
199
200 void remove(Attribute *attribute);
201
202 void remove(list<Attribute>::iterator it);
203
204 void resize(bool reserve_only = false);
205 void clear(bool preserve_voxel_data = false);
206
207 /* Update the attributes in this AttributeSet with the ones from the new set,
208 * and remove any attribute not found on the new set from this. */
209 void update(AttributeSet &&new_attributes);
210
211 /* Return whether the attributes of the given kernel_type are modified, where "modified" means
212 * that some attributes of the given type were added or removed from this AttributeSet. This does
213 * not mean that the data of the remaining attributes in this AttributeSet were also modified. To
214 * check this, use Attribute.modified. */
215 bool modified(AttrKernelDataType kernel_type) const;
216
217 void clear_modified();
218
219 private:
220 /* Set the relevant modified flag for the attribute. Only attributes that are stored in device
221 * arrays will be considered for tagging this AttributeSet as modified. */
222 void tag_modified(const Attribute &attr);
223};
224
225/* AttributeRequest
226 *
227 * Request from a shader to use a certain attribute, so we can figure out
228 * which ones we need to export from the host app end store for the kernel.
229 * The attribute is found either by name or by standard attribute type. */
230
232 public:
233 ustring name;
235
236 /* temporary variables used by GeometryManager */
237 TypeDesc type;
239
240 explicit AttributeRequest(ustring name_);
242};
243
244/* AttributeRequestSet
245 *
246 * Set of attributes requested by a shader. */
247
249 public:
251
254
255 void add(ustring name);
256 void add(AttributeStandard std);
257 void add(AttributeRequestSet &reqs);
258 void add_standard(ustring name);
259
260 bool find(ustring name);
261 bool find(AttributeStandard std);
262
263 size_t size();
264 void clear();
265
266 bool modified(const AttributeRequestSet &other);
267};
268
unsigned int uint
BMesh const char void * data
ATTR_WARN_UNUSED_RESULT const void * element
vector< AttributeRequest > requests
bool find(ustring name)
void add(ustring name)
bool modified(const AttributeRequestSet &other)
void add_standard(ustring name)
AttributeRequest(ustring name_)
AttributeDescriptor desc
AttributeStandard std
Geometry * geometry
void update(AttributeSet &&new_attributes)
AttributeSet(AttributeSet &&)=default
Attribute & copy(const Attribute &attr)
list< Attribute > attributes
Attribute * find(ustring name) const
Attribute * find_matching(const Attribute &other)
bool modified(AttrKernelDataType kernel_type) const
Attribute * add(ustring name, const TypeDesc type, AttributeElement element)
void remove(ustring name)
AttributeSet(Geometry *geometry, AttributePrimitive prim)
void resize(bool reserve_only=false)
void clear(bool preserve_voxel_data=false)
AttributePrimitive prim
Definition hair.h:13
nullptr FLOAT
#define CCL_NAMESPACE_END
#define assert(assertion)
ccl_gpu_kernel_postfix ccl_global KernelWorkTile * tiles
AttributeStandard
AttributeElement
AttributePrimitive
const char * name
AttrKernelDataType
@ FLOAT4
@ NUM
@ FLOAT2
@ UCHAR4
@ FLOAT3
const char * name
AttributeElement element
Attribute(ustring name, const TypeDesc type, AttributeElement element, Geometry *geom, AttributePrimitive prim)
uchar4 * data_uchar4()
void set(ustring name, const TypeDesc type, AttributeElement element)
static AttrKernelDataType kernel_type(const Attribute &attr)
char * data()
static AttributeStandard name_standard(const char *name)
const ImageHandle & data_voxel() const
Attribute(const Attribute &other)=delete
TypeDesc type
const float4 * data_float4() const
void resize(Geometry *geom, AttributePrimitive prim, bool reserve_only)
size_t element_size(Geometry *geom, AttributePrimitive prim) const
void get_uv_tiles(Geometry *geom, AttributePrimitive prim, unordered_set< int > &tiles) const
const float3 * data_float3() const
vector< char > buffer
const float2 * data_float2() const
void set_data_from(Attribute &&other)
const float * data_float() const
Attribute & operator=(const Attribute &other)=delete
void zero_data(void *dst)
static const char * standard_name(AttributeStandard std)
Attribute(Attribute &&other)=default
const Transform * data_transform() const
float * data_float()
const char * data() const
size_t buffer_size(Geometry *geom, AttributePrimitive prim) const
AttributeStandard std
ImageHandle & data_voxel()
size_t data_sizeof() const
static bool same_storage(const TypeDesc a, const TypeDesc b)
float3 * data_float3()
float4 * data_float4()
Transform * data_transform()
void add(const float &f)
float2 * data_float2()