Blender  V3.3
scene/attribute.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #ifndef __ATTRIBUTE_H__
5 #define __ATTRIBUTE_H__
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 
19 class Attribute;
20 class AttributeRequest;
22 class AttributeSet;
23 class ImageHandle;
24 class Geometry;
25 class Hair;
26 class Mesh;
27 struct 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 
37 enum 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 
44 class Attribute {
45  public:
46  ustring name;
48 
49  TypeDesc type;
52  uint flags; /* enum AttributeFlag */
53 
54  bool modified;
55 
56  Attribute(ustring name,
57  TypeDesc type,
59  Geometry *geom,
60  AttributePrimitive prim);
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, TypeDesc type, AttributeElement element);
67  void resize(Geometry *geom, AttributePrimitive prim, bool reserve_only);
68  void resize(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.size()) ? &buffer[0] : NULL;
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.size()) ? &buffer[0] : NULL;
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  }
140  const Transform *data_transform() const
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  void add_with_weight(void *dst, void *src, float weight);
153 
154  void add(const float &f);
155  void add(const float2 &f);
156  void add(const float3 &f);
157  void add(const uchar4 &f);
158  void add(const Transform &tfm);
159  void add(const char *data);
160 
161  void set_data_from(Attribute &&other);
162 
163  static bool same_storage(TypeDesc a, TypeDesc b);
164  static const char *standard_name(AttributeStandard std);
165  static AttributeStandard name_standard(const char *name);
166 
167  static AttrKernelDataType kernel_type(const Attribute &attr);
168 
169  void get_uv_tiles(Geometry *geom, AttributePrimitive prim, unordered_set<int> &tiles) const;
170 };
171 
172 /* Attribute Set
173  *
174  * Set of attributes on a mesh. */
175 
177  uint32_t modified_flag;
178 
179  public:
182  list<Attribute> attributes;
183 
185  AttributeSet(AttributeSet &&) = default;
186  ~AttributeSet();
187 
188  Attribute *add(ustring name, TypeDesc type, AttributeElement element);
189  Attribute *find(ustring name) const;
190  void remove(ustring name);
191 
192  Attribute *add(AttributeStandard std, ustring name = ustring());
195 
197  Attribute *find_matching(const Attribute &other);
198 
199  void remove(Attribute *attribute);
200 
201  void remove(list<Attribute>::iterator it);
202 
203  void resize(bool reserve_only = false);
204  void clear(bool preserve_voxel_data = false);
205 
206  /* Update the attributes in this AttributeSet with the ones from the new set,
207  * and remove any attribute not found on the new set from this. */
208  void update(AttributeSet &&new_attributes);
209 
210  /* Return whether the attributes of the given kernel_type are modified, where "modified" means
211  * that some attributes of the given type were added or removed from this AttributeSet. This does
212  * not mean that the data of the remaining attributes in this AttributeSet were also modified. To
213  * check this, use Attribute.modified. */
214  bool modified(AttrKernelDataType kernel_type) const;
215 
216  void clear_modified();
217 
218  private:
219  /* Set the relevant modified flag for the attribute. Only attributes that are stored in device
220  * arrays will be considered for tagging this AttributeSet as modified. */
221  void tag_modified(const Attribute &attr);
222 };
223 
224 /* AttributeRequest
225  *
226  * Request from a shader to use a certain attribute, so we can figure out
227  * which ones we need to export from the host app end store for the kernel.
228  * The attribute is found either by name or by standard attribute type. */
229 
231  public:
232  ustring name;
234 
235  /* temporary variables used by GeometryManager */
236  TypeDesc type, subd_type;
238 
239  explicit AttributeRequest(ustring name_);
241 };
242 
243 /* AttributeRequestSet
244  *
245  * Set of attributes requested by a shader. */
246 
248  public:
250 
253 
254  void add(ustring name);
255  void add(AttributeStandard std);
256  void add(AttributeRequestSet &reqs);
257  void add_standard(ustring name);
258 
259  bool find(ustring name);
260  bool find(AttributeStandard std);
261 
262  size_t size();
263  void clear();
264 
265  bool modified(const AttributeRequestSet &other);
266 };
267 
269 
270 #endif /* __ATTRIBUTE_H__ */
unsigned int uint
Definition: BLI_sys_types.h:67
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
float float4[4]
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value Normal Generate a perturbed normal from an RGB normal map image Typically used for faking highly detailed surfaces Generate an OSL shader from a file or text data block Image Sample an image file as a texture Sky Generate a procedural sky texture Noise Generate fractal Perlin noise Wave Generate procedural bands or rings with noise Voronoi Generate Worley noise based on the distance to random points Typically used to generate textures such as or biological cells Brick Generate a procedural texture producing bricks Texture Retrieve multiple types of texture coordinates nTypically used as inputs for texture nodes Vector Convert a or normal between and object coordinate space Combine Create a color from its and value channels Color Retrieve a color attribute
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 subd_desc
AttributeDescriptor desc
AttributeStandard std
Geometry * geometry
Attribute * add(ustring name, TypeDesc type, AttributeElement element)
void update(AttributeSet &&new_attributes)
AttributeSet(AttributeSet &&)=default
list< Attribute > attributes
Attribute * find(ustring name) const
Attribute * find_matching(const Attribute &other)
bool modified(AttrKernelDataType kernel_type) const
void remove(ustring name)
AttributeSet(Geometry *geometry, AttributePrimitive prim)
void resize(bool reserve_only=false)
void clear(bool preserve_voxel_data=false)
AttributePrimitive prim
ImageHandle & data_voxel()
AttributeElement element
const ImageHandle & data_voxel() const
static bool same_storage(TypeDesc a, TypeDesc b)
void add_with_weight(void *dst, void *src, float weight)
static AttrKernelDataType kernel_type(const Attribute &attr)
Attribute & operator=(const Attribute &other)=delete
const float4 * data_float4() const
float * data_float()
uchar4 * data_uchar4()
void set(ustring name, TypeDesc type, AttributeElement element)
const float2 * data_float2() const
static AttributeStandard name_standard(const char *name)
Attribute(const Attribute &other)=delete
Transform * data_transform()
TypeDesc type
const float3 * data_float3() const
void resize(Geometry *geom, AttributePrimitive prim, bool reserve_only)
size_t element_size(Geometry *geom, AttributePrimitive prim) const
float4 * data_float4()
void get_uv_tiles(Geometry *geom, AttributePrimitive prim, unordered_set< int > &tiles) const
char * data()
vector< char > buffer
void set_data_from(Attribute &&other)
void zero_data(void *dst)
Attribute(ustring name, TypeDesc type, AttributeElement element, Geometry *geom, AttributePrimitive prim)
static const char * standard_name(AttributeStandard std)
Attribute(Attribute &&other)=default
size_t buffer_size(Geometry *geom, AttributePrimitive prim) const
AttributeStandard std
ustring name
const Transform * data_transform() const
size_t data_sizeof() const
const float * data_float() const
void add(const float &f)
float3 * data_float3()
const char * data() const
float2 * data_float2()
Definition: hair.h:13
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
SyclQueue void void * src
ccl_gpu_kernel_postfix ccl_global KernelWorkTile * tiles
AttributeStandard
Definition: kernel/types.h:612
AttributeElement
Definition: kernel/types.h:597
AttributePrimitive
Definition: kernel/types.h:590
static unsigned a[3]
Definition: RandGen.cpp:78
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
AttrKernelDataType
@ FLOAT4
@ NUM
@ FLOAT2
@ UCHAR4
@ FLOAT3
@ FLOAT
unsigned int uint32_t
Definition: stdint.h:80