1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __ATTRIBUTE_H__
18 #define __ATTRIBUTE_H__
19 
20 #include "render/image.h"
21 
22 #include "kernel/kernel_types.h"
23 
24 #include "util/util_list.h"
25 #include "util/util_param.h"
26 #include "util/util_set.h"
27 #include "util/util_types.h"
28 #include "util/util_vector.h"
29 
30 CCL_NAMESPACE_BEGIN
31 
32 class Attribute;
33 class AttributeRequest;
34 class AttributeRequestSet;
35 class AttributeSet;
36 class ImageHandle;
37 class Geometry;
38 class Hair;
39 class Mesh;
40 struct Transform;
41 
42 /* Attribute
43  *
44  * Arbitrary data layers on meshes.
45  * Supported types: Float, Color, Vector, Normal, Point */
46 
47 class Attribute {
48  public:
49   ustring name;
50   AttributeStandard std;
51 
52   TypeDesc type;
53   vector<char> buffer;
54   AttributeElement element;
55   uint flags; /* enum AttributeFlag */
56 
57   Attribute(ustring name,
58             TypeDesc type,
59             AttributeElement element,
60             Geometry *geom,
61             AttributePrimitive prim);
62   Attribute(Attribute &&other) = default;
63   Attribute(const Attribute &other) = delete;
64   Attribute &operator=(const Attribute &other) = delete;
65   ~Attribute();
66 
67   void set(ustring name, TypeDesc type, AttributeElement element);
68   void resize(Geometry *geom, AttributePrimitive prim, bool reserve_only);
69   void resize(size_t num_elements);
70 
71   size_t data_sizeof() const;
72   size_t element_size(Geometry *geom, AttributePrimitive prim) const;
73   size_t buffer_size(Geometry *geom, AttributePrimitive prim) const;
74 
data()75   char *data()
76   {
77     return (buffer.size()) ? &buffer[0] : NULL;
78   }
data_float2()79   float2 *data_float2()
80   {
81     assert(data_sizeof() == sizeof(float2));
82     return (float2 *)data();
83   }
data_float3()84   float3 *data_float3()
85   {
86     assert(data_sizeof() == sizeof(float3));
87     return (float3 *)data();
88   }
data_float4()89   float4 *data_float4()
90   {
91     assert(data_sizeof() == sizeof(float4));
92     return (float4 *)data();
93   }
data_float()94   float *data_float()
95   {
96     assert(data_sizeof() == sizeof(float));
97     return (float *)data();
98   }
data_uchar4()99   uchar4 *data_uchar4()
100   {
101     assert(data_sizeof() == sizeof(uchar4));
102     return (uchar4 *)data();
103   }
data_transform()104   Transform *data_transform()
105   {
106     assert(data_sizeof() == sizeof(Transform));
107     return (Transform *)data();
108   }
109 
110   /* Attributes for voxels are images */
data_voxel()111   ImageHandle &data_voxel()
112   {
113     assert(data_sizeof() == sizeof(ImageHandle));
114     return *(ImageHandle *)data();
115   }
116 
data()117   const char *data() const
118   {
119     return (buffer.size()) ? &buffer[0] : NULL;
120   }
data_float2()121   const float2 *data_float2() const
122   {
123     assert(data_sizeof() == sizeof(float2));
124     return (const float2 *)data();
125   }
data_float3()126   const float3 *data_float3() const
127   {
128     assert(data_sizeof() == sizeof(float3));
129     return (const float3 *)data();
130   }
data_float4()131   const float4 *data_float4() const
132   {
133     assert(data_sizeof() == sizeof(float4));
134     return (const float4 *)data();
135   }
data_float()136   const float *data_float() const
137   {
138     assert(data_sizeof() == sizeof(float));
139     return (const float *)data();
140   }
data_transform()141   const Transform *data_transform() const
142   {
143     assert(data_sizeof() == sizeof(Transform));
144     return (const Transform *)data();
145   }
data_voxel()146   const ImageHandle &data_voxel() const
147   {
148     assert(data_sizeof() == sizeof(ImageHandle));
149     return *(const ImageHandle *)data();
150   }
151 
152   void zero_data(void *dst);
153   void add_with_weight(void *dst, void *src, float weight);
154 
155   void add(const float &f);
156   void add(const float2 &f);
157   void add(const float3 &f);
158   void add(const uchar4 &f);
159   void add(const Transform &tfm);
160   void add(const char *data);
161 
162   static bool same_storage(TypeDesc a, TypeDesc b);
163   static const char *standard_name(AttributeStandard std);
164   static AttributeStandard name_standard(const char *name);
165 
166   void get_uv_tiles(Geometry *geom, AttributePrimitive prim, unordered_set<int> &tiles) const;
167 };
168 
169 /* Attribute Set
170  *
171  * Set of attributes on a mesh. */
172 
173 class AttributeSet {
174  public:
175   Geometry *geometry;
176   AttributePrimitive prim;
177   list<Attribute> attributes;
178 
179   AttributeSet(Geometry *geometry, AttributePrimitive prim);
180   ~AttributeSet();
181 
182   Attribute *add(ustring name, TypeDesc type, AttributeElement element);
183   Attribute *find(ustring name) const;
184   void remove(ustring name);
185 
186   Attribute *add(AttributeStandard std, ustring name = ustring());
187   Attribute *find(AttributeStandard std) const;
188   void remove(AttributeStandard std);
189 
190   Attribute *find(AttributeRequest &req);
191 
192   void remove(Attribute *attribute);
193 
194   void resize(bool reserve_only = false);
195   void clear(bool preserve_voxel_data = false);
196 };
197 
198 /* AttributeRequest
199  *
200  * Request from a shader to use a certain attribute, so we can figure out
201  * which ones we need to export from the host app end store for the kernel.
202  * The attribute is found either by name or by standard attribute type. */
203 
204 class AttributeRequest {
205  public:
206   ustring name;
207   AttributeStandard std;
208 
209   /* temporary variables used by GeometryManager */
210   TypeDesc type, subd_type;
211   AttributeDescriptor desc, subd_desc;
212 
213   explicit AttributeRequest(ustring name_);
214   explicit AttributeRequest(AttributeStandard std);
215 };
216 
217 /* AttributeRequestSet
218  *
219  * Set of attributes requested by a shader. */
220 
221 class AttributeRequestSet {
222  public:
223   vector<AttributeRequest> requests;
224 
225   AttributeRequestSet();
226   ~AttributeRequestSet();
227 
228   void add(ustring name);
229   void add(AttributeStandard std);
230   void add(AttributeRequestSet &reqs);
231   void add_standard(ustring name);
232 
233   bool find(ustring name);
234   bool find(AttributeStandard std);
235 
236   size_t size();
237   void clear();
238 
239   bool modified(const AttributeRequestSet &other);
240 };
241 
242 CCL_NAMESPACE_END
243 
244 #endif /* __ATTRIBUTE_H__ */
245