Blender V5.3
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 <type_traits>
8
9#include "scene/image.h"
10
11#include "kernel/types.h"
12
14#include "util/list.h"
15#include "util/param.h"
16#include "util/set.h"
17#include "util/types.h"
18#include "util/vector.h"
19
21
22class Attribute;
25class AttributeSet;
26class ImageHandle;
27class Geometry;
28class Hair;
29class Mesh;
30struct Transform;
31
32/* AttrKernelDataType.
33 *
34 * The data type of the device arrays storing the attribute's data. Those data types are different
35 * than the ones for attributes as some attribute types are stored in the same array, e.g. Point,
36 * Vector, and Transform are all stored as float3 in the kernel.
37 *
38 * The values of this enumeration are also used as flags to detect changes in AttributeSet. */
39
41 FLOAT = 0,
42 FLOAT2 = 1,
43 FLOAT3 = 2,
44 FLOAT4 = 3,
45 UCHAR4 = 4,
46 NORMAL = 5,
49 NUM = 8
50};
51
52/* Attribute
53 *
54 * Arbitrary data layers on meshes.
55 * Supported types: Float, Color, Vector, Normal, Point, Quaternion, Spherical Harmonics. */
56
57class Attribute {
58 public:
59 /* Storage buffer for one motion step, or a single buffer if no motion. */
60 struct Buffer {
61 /* Optionally used to share ownership of #data, see implicit_sharing.h.
62 * If this is null, #data is wholly owned by this Attribute. */
63 const void *data = nullptr;
65 };
66
67 ustring name;
69
70 TypeDesc type;
71 /* Center motion step. */
73 /* Other motion steps. Empty when the attribute has no motion. */
75 int size = 0;
77 uint flags; /* enum AttributeFlag */
78
80
81 Attribute(ustring name,
82 const TypeDesc type,
84 Geometry *geom,
86 Attribute(ustring name,
87 const TypeDesc type,
89 const void *data,
90 int size,
91 ImplicitSharingInfo sharing_info);
92 Attribute(Attribute &&other);
93 Attribute &operator=(Attribute &&other) = delete;
94 Attribute(const Attribute &other) = delete;
95 Attribute &operator=(const Attribute &other) = delete;
96 ~Attribute();
97
98 void set(ustring name, const TypeDesc type, AttributeElement element);
99 void resize(Geometry *geom, AttributePrimitive prim);
100 void resize(const size_t num_elements);
101
102 /* Allocate or free motion steps, matching the geometry's motion_steps. */
103 void add_motion(const Geometry *geom);
104 void remove_motion();
105 bool has_motion() const
106 {
107 return !motion.empty();
108 }
109 /* Number of motion steps stored, including the center step. */
111 {
112 return has_motion() ? int(motion.size()) + 1 : 1;
113 }
114
115 /* Move motion steps from another attribute into this one. */
116 void take_motion_from(Attribute &other);
117
118 /* Replace a single motion step's data with a buffer shared with another owner,
119 * using implicit sharing. Motion steps are 1-indexed (step 0 is the center). */
121 const void *data,
122 int new_size,
123 ImplicitSharingInfo sharing_info);
124
125 size_t data_sizeof() const;
127 size_t buffer_size(Geometry *geom, AttributePrimitive prim) const;
128
129 /* Typed data access. */
130 template<typename T = void> const T *data(const int step = 0) const
131 {
132 if constexpr (!std::is_same_v<T, void>) {
133 assert(data_sizeof() == sizeof(T));
134 }
135 if (step == 0 || !has_motion()) {
136 return static_cast<const T *>(center.data);
137 }
138 assert(step >= 1 && step <= int(motion.size()));
139 return static_cast<const T *>(motion[step - 1].data);
140 }
141 template<typename T = void> T *data_for_write(const int step = 0)
142 {
143 if constexpr (!std::is_same_v<T, void>) {
144 assert(data_sizeof() == sizeof(T));
145 }
146 return reinterpret_cast<T *>(data_for_write_buffer(step));
147 }
148
149 /* Motion steps come in two orderings:
150 * - data() indexes the attribute step, with the center step at index 0.
151 * - data_at_time_step() indexes the time-ordered step, with the center step
152 * in the middle.
153 *
154 * The number of time steps must be passed in, as it may be lower than the
155 * stored motion steps, for example when BVH building without motion blur
156 * but there are motion steps for a motion render pass. */
157 int time_step_to_attr_step(const int step, const int num_time_steps) const
158 {
159 const int center = (num_time_steps - 1) / 2;
160 return (step == center) ? 0 : (step < center) ? step + 1 : step;
161 }
162 template<typename T = void>
163 const T *data_at_time_step(const int step, const int num_time_steps) const
164 {
165 return data<T>(time_step_to_attr_step(step, num_time_steps));
166 }
167
168 /* Attributes for voxels are 3D NanoVDB images. */
169 const ImageHandle &data_voxel() const
170 {
171 return *data<ImageHandle>();
172 }
177
178 private:
179 char *data_for_write_buffer(const int step);
180
181 public:
182 void zero_data(void *dst);
183
184 void set_data_from(Attribute &&other);
185
186 void free_data();
187
188 static bool same_storage(const TypeDesc a, const TypeDesc b);
189 static const char *standard_name(AttributeStandard std);
190 static AttributeStandard name_standard(ustring name);
192
193 static ustring osl_name(AttributeStandard std);
194 static ustring osl_name(ustring name);
195
196 bool matches_standard(const Geometry *geometry, AttributeStandard std) const;
197
198 static AttrKernelDataType kernel_type(const Attribute &attr);
199
200 void get_uv_tiles(Geometry *geom, AttributePrimitive prim, unordered_set<int> &tiles) const;
201};
202
203/* Attribute Set
204 *
205 * Set of attributes on a mesh. */
206
208 uint32_t modified_flag;
209
210 public:
213 list<Attribute> attributes;
214
218
219 Attribute *add(ustring name, const TypeDesc type, AttributeElement element);
220 Attribute *add_shared(ustring name,
221 const TypeDesc type,
222 AttributeElement element,
223 const void *data,
224 int size,
225 ImplicitSharingInfo sharing_info);
226 Attribute *add_from(Attribute &&other);
227 Attribute *find(ustring name) const;
228 void remove(ustring name);
229
230 Attribute *add(AttributeStandard std, ustring name = ustring());
232 ustring name,
233 const void *data,
234 int size,
235 ImplicitSharingInfo sharing_info);
236 Attribute *find(AttributeStandard std) const;
237 void remove(AttributeStandard std);
238
239 Attribute &copy(const Attribute &attr);
240
242 Attribute *find_matching(const Attribute &other);
243
245
246 void remove(list<Attribute>::iterator it);
247
248 void resize();
249 void clear(bool preserve_voxel_data = false);
250
251 /* Update the attributes in this AttributeSet with the ones from the new set,
252 * and remove any attribute not found on the new set from this. */
253 void update(AttributeSet &&new_attributes);
254
255 /* Return whether the attributes of the given kernel_type are modified, where "modified" means
256 * that some attributes of the given type were added or removed from this AttributeSet. This does
257 * not mean that the data of the remaining attributes in this AttributeSet were also modified. To
258 * check this, use Attribute.modified. */
259 bool modified(AttrKernelDataType kernel_type) const;
260
261 void clear_modified();
262
263 private:
264 /* Set the relevant modified flag for the attribute. Only attributes that are stored in device
265 * arrays will be considered for tagging this AttributeSet as modified. */
266 void tag_modified(const Attribute &attr);
267};
268
269/* AttributeRequest
270 *
271 * Request from a shader to use a certain attribute, so we can figure out
272 * which ones we need to export from the host app end store for the kernel.
273 *
274 * The attribute is found by name, by standard attribute type, or by both. */
275
277 public:
278 ustring name;
280
281 /* temporary variables used by GeometryManager */
282 TypeDesc type;
284
285 explicit AttributeRequest(ustring name_);
286 explicit AttributeRequest(AttributeStandard std_);
287 AttributeRequest(ustring name_, AttributeStandard std_);
288};
289
290/* AttributeRequestSet
291 *
292 * Set of attributes requested by a shader. */
293
295 public:
297
300
301 /* Request the attribute with this specific name or standard. */
302 void add(ustring name);
303 void add(AttributeStandard std);
304
305 /* Request the attribute with this name, or the standard attribute that this
306 * is the name of. This is used by e.g. the Attribute node, which accepts
307 * both even if this sometimes ambiguous. If both exist, the name has priority. */
308 void add_name_or_standard(ustring name);
309
310 void add(const AttributeRequestSet &reqs);
311
312 bool find(ustring name) const;
313 bool find(AttributeStandard std) const;
314
315 size_t size() const;
316 void clear();
317
318 bool modified(const AttributeRequestSet &other) const;
319};
320
vector< AttributeRequest > requests
void add(ustring name)
size_t size() const
bool find(ustring name) const
void add_name_or_standard(ustring name)
bool modified(const AttributeRequestSet &other) const
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)
void clear_modified()
AttributeSet(Geometry *geometry, AttributePrimitive prim)
void clear(bool preserve_voxel_data=false)
Attribute * add_shared(ustring name, const TypeDesc type, AttributeElement element, const void *data, int size, ImplicitSharingInfo sharing_info)
AttributePrimitive prim
Attribute * add_from(Attribute &&other)
static AttributeStandard name_standard(ustring name)
ImageHandle & data_voxel_for_write()
AttributeElement element
Attribute(ustring name, const TypeDesc type, AttributeElement element, Geometry *geom, AttributePrimitive prim)
Definition attribute.cpp:20
vector< Buffer > motion
void take_motion_from(Attribute &other)
const T * data(const int step=0) const
void set(ustring name, const TypeDesc type, AttributeElement element)
void resize(Geometry *geom, AttributePrimitive prim)
static AttrKernelDataType kernel_type(const Attribute &attr)
T * data_for_write(const int step=0)
void set_motion_step_shared(int step, const void *data, int new_size, ImplicitSharingInfo sharing_info)
static size_t element_size(Geometry *geom, AttributeElement element, AttributePrimitive prim)
const ImageHandle & data_voxel() const
Attribute(const Attribute &other)=delete
static ustring osl_name(AttributeStandard std)
bool has_motion() const
TypeDesc type
void free_data()
static AttributeStandard name_volume_standard(ustring name)
Attribute & operator=(Attribute &&other)=delete
void add_motion(const Geometry *geom)
void get_uv_tiles(Geometry *geom, AttributePrimitive prim, unordered_set< int > &tiles) const
void remove_motion()
void set_data_from(Attribute &&other)
Attribute & operator=(const Attribute &other)=delete
void zero_data(void *dst)
static const char * standard_name(AttributeStandard std)
size_t buffer_size(Geometry *geom, AttributePrimitive prim) const
AttributeStandard std
int num_motion_steps() const
size_t data_sizeof() const
static bool same_storage(const TypeDesc a, const TypeDesc b)
bool matches_standard(const Geometry *geometry, AttributeStandard std) const
const T * data_at_time_step(const int step, const int num_time_steps) const
int time_step_to_attr_step(const int step, const int num_time_steps) const
Definition hair.h:13
nullptr FLOAT
#define CCL_NAMESPACE_END
#define assert(assertion)
#define attribute(slot)
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
const void * ImplicitSharingInfo
ccl_gpu_kernel_postfix ccl_global KernelWorkTile * tiles
AttributeElement
AttributePrimitive
AttributeStandard
#define T
AttrKernelDataType
ImplicitSharingInfo sharing_info