attribute.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #ifndef __ATTRIBUTE_H__
  17. #define __ATTRIBUTE_H__
  18. #include "kernel/kernel_types.h"
  19. #include "util/util_list.h"
  20. #include "util/util_param.h"
  21. #include "util/util_types.h"
  22. #include "util/util_vector.h"
  23. CCL_NAMESPACE_BEGIN
  24. class Attribute;
  25. class AttributeRequest;
  26. class AttributeRequestSet;
  27. class AttributeSet;
  28. class ImageManager;
  29. class Mesh;
  30. struct Transform;
  31. /* Attributes for voxels are images */
  32. struct VoxelAttribute {
  33. ImageManager *manager;
  34. int slot;
  35. };
  36. /* Attribute
  37. *
  38. * Arbitrary data layers on meshes.
  39. * Supported types: Float, Color, Vector, Normal, Point */
  40. class Attribute {
  41. public:
  42. ustring name;
  43. AttributeStandard std;
  44. TypeDesc type;
  45. vector<char> buffer;
  46. AttributeElement element;
  47. uint flags; /* enum AttributeFlag */
  48. Attribute()
  49. {
  50. }
  51. ~Attribute();
  52. void set(ustring name, TypeDesc type, AttributeElement element);
  53. void resize(Mesh *mesh, AttributePrimitive prim, bool reserve_only);
  54. void resize(size_t num_elements);
  55. size_t data_sizeof() const;
  56. size_t element_size(Mesh *mesh, AttributePrimitive prim) const;
  57. size_t buffer_size(Mesh *mesh, AttributePrimitive prim) const;
  58. char *data()
  59. {
  60. return (buffer.size()) ? &buffer[0] : NULL;
  61. }
  62. float2 *data_float2()
  63. {
  64. assert(data_sizeof() == sizeof(float2));
  65. return (float2 *)data();
  66. }
  67. float3 *data_float3()
  68. {
  69. assert(data_sizeof() == sizeof(float3));
  70. return (float3 *)data();
  71. }
  72. float4 *data_float4()
  73. {
  74. assert(data_sizeof() == sizeof(float4));
  75. return (float4 *)data();
  76. }
  77. float *data_float()
  78. {
  79. assert(data_sizeof() == sizeof(float));
  80. return (float *)data();
  81. }
  82. uchar4 *data_uchar4()
  83. {
  84. assert(data_sizeof() == sizeof(uchar4));
  85. return (uchar4 *)data();
  86. }
  87. Transform *data_transform()
  88. {
  89. assert(data_sizeof() == sizeof(Transform));
  90. return (Transform *)data();
  91. }
  92. VoxelAttribute *data_voxel()
  93. {
  94. assert(data_sizeof() == sizeof(VoxelAttribute));
  95. return (VoxelAttribute *)data();
  96. }
  97. const char *data() const
  98. {
  99. return (buffer.size()) ? &buffer[0] : NULL;
  100. }
  101. const float2 *data_float2() const
  102. {
  103. assert(data_sizeof() == sizeof(float2));
  104. return (const float2 *)data();
  105. }
  106. const float3 *data_float3() const
  107. {
  108. assert(data_sizeof() == sizeof(float3));
  109. return (const float3 *)data();
  110. }
  111. const float4 *data_float4() const
  112. {
  113. assert(data_sizeof() == sizeof(float4));
  114. return (const float4 *)data();
  115. }
  116. const float *data_float() const
  117. {
  118. assert(data_sizeof() == sizeof(float));
  119. return (const float *)data();
  120. }
  121. const Transform *data_transform() const
  122. {
  123. assert(data_sizeof() == sizeof(Transform));
  124. return (const Transform *)data();
  125. }
  126. const VoxelAttribute *data_voxel() const
  127. {
  128. assert(data_sizeof() == sizeof(VoxelAttribute));
  129. return (const VoxelAttribute *)data();
  130. }
  131. void zero_data(void *dst);
  132. void add_with_weight(void *dst, void *src, float weight);
  133. void add(const float &f);
  134. void add(const float2 &f);
  135. void add(const float3 &f);
  136. void add(const uchar4 &f);
  137. void add(const Transform &f);
  138. void add(const VoxelAttribute &f);
  139. void add(const char *data);
  140. static bool same_storage(TypeDesc a, TypeDesc b);
  141. static const char *standard_name(AttributeStandard std);
  142. static AttributeStandard name_standard(const char *name);
  143. };
  144. /* Attribute Set
  145. *
  146. * Set of attributes on a mesh. */
  147. class AttributeSet {
  148. public:
  149. Mesh *triangle_mesh;
  150. Mesh *curve_mesh;
  151. Mesh *subd_mesh;
  152. list<Attribute> attributes;
  153. AttributeSet();
  154. ~AttributeSet();
  155. Attribute *add(ustring name, TypeDesc type, AttributeElement element);
  156. Attribute *find(ustring name) const;
  157. void remove(ustring name);
  158. Attribute *add(AttributeStandard std, ustring name = ustring());
  159. Attribute *find(AttributeStandard std) const;
  160. void remove(AttributeStandard std);
  161. Attribute *find(AttributeRequest &req);
  162. void remove(Attribute *attribute);
  163. void resize(bool reserve_only = false);
  164. void clear(bool preserve_voxel_data = false);
  165. };
  166. /* AttributeRequest
  167. *
  168. * Request from a shader to use a certain attribute, so we can figure out
  169. * which ones we need to export from the host app end store for the kernel.
  170. * The attribute is found either by name or by standard attribute type. */
  171. class AttributeRequest {
  172. public:
  173. ustring name;
  174. AttributeStandard std;
  175. /* temporary variables used by MeshManager */
  176. TypeDesc triangle_type, curve_type, subd_type;
  177. AttributeDescriptor triangle_desc, curve_desc, subd_desc;
  178. explicit AttributeRequest(ustring name_);
  179. explicit AttributeRequest(AttributeStandard std);
  180. };
  181. /* AttributeRequestSet
  182. *
  183. * Set of attributes requested by a shader. */
  184. class AttributeRequestSet {
  185. public:
  186. vector<AttributeRequest> requests;
  187. AttributeRequestSet();
  188. ~AttributeRequestSet();
  189. void add(ustring name);
  190. void add(AttributeStandard std);
  191. void add(AttributeRequestSet &reqs);
  192. void add_standard(ustring name);
  193. bool find(ustring name);
  194. bool find(AttributeStandard std);
  195. size_t size();
  196. void clear();
  197. bool modified(const AttributeRequestSet &other);
  198. };
  199. CCL_NAMESPACE_END
  200. #endif /* __ATTRIBUTE_H__ */