mesh.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 __MESH_H__
  17. #define __MESH_H__
  18. #include "graph/node.h"
  19. #include "render/attribute.h"
  20. #include "render/shader.h"
  21. #include "util/util_array.h"
  22. #include "util/util_boundbox.h"
  23. #include "util/util_list.h"
  24. #include "util/util_map.h"
  25. #include "util/util_param.h"
  26. #include "util/util_transform.h"
  27. #include "util/util_types.h"
  28. #include "util/util_vector.h"
  29. CCL_NAMESPACE_BEGIN
  30. class Attribute;
  31. class BVH;
  32. class Device;
  33. class DeviceScene;
  34. class Mesh;
  35. class Progress;
  36. class RenderStats;
  37. class Scene;
  38. class SceneParams;
  39. class AttributeRequest;
  40. struct SubdParams;
  41. class DiagSplit;
  42. struct PackedPatchTable;
  43. /* Mesh */
  44. class Mesh : public Node {
  45. public:
  46. NODE_DECLARE
  47. /* Mesh Triangle */
  48. struct Triangle {
  49. int v[3];
  50. void bounds_grow(const float3 *verts, BoundBox &bounds) const;
  51. void motion_verts(const float3 *verts,
  52. const float3 *vert_steps,
  53. size_t num_verts,
  54. size_t num_steps,
  55. float time,
  56. float3 r_verts[3]) const;
  57. void verts_for_step(const float3 *verts,
  58. const float3 *vert_steps,
  59. size_t num_verts,
  60. size_t num_steps,
  61. size_t step,
  62. float3 r_verts[3]) const;
  63. float3 compute_normal(const float3 *verts) const;
  64. bool valid(const float3 *verts) const;
  65. };
  66. Triangle get_triangle(size_t i) const
  67. {
  68. Triangle tri = {{triangles[i * 3 + 0], triangles[i * 3 + 1], triangles[i * 3 + 2]}};
  69. return tri;
  70. }
  71. size_t num_triangles() const
  72. {
  73. return triangles.size() / 3;
  74. }
  75. /* Mesh Curve */
  76. struct Curve {
  77. int first_key;
  78. int num_keys;
  79. int num_segments()
  80. {
  81. return num_keys - 1;
  82. }
  83. void bounds_grow(const int k,
  84. const float3 *curve_keys,
  85. const float *curve_radius,
  86. BoundBox &bounds) const;
  87. void bounds_grow(float4 keys[4], BoundBox &bounds) const;
  88. void bounds_grow(const int k,
  89. const float3 *curve_keys,
  90. const float *curve_radius,
  91. const Transform &aligned_space,
  92. BoundBox &bounds) const;
  93. void motion_keys(const float3 *curve_keys,
  94. const float *curve_radius,
  95. const float3 *key_steps,
  96. size_t num_curve_keys,
  97. size_t num_steps,
  98. float time,
  99. size_t k0,
  100. size_t k1,
  101. float4 r_keys[2]) const;
  102. void cardinal_motion_keys(const float3 *curve_keys,
  103. const float *curve_radius,
  104. const float3 *key_steps,
  105. size_t num_curve_keys,
  106. size_t num_steps,
  107. float time,
  108. size_t k0,
  109. size_t k1,
  110. size_t k2,
  111. size_t k3,
  112. float4 r_keys[4]) const;
  113. void keys_for_step(const float3 *curve_keys,
  114. const float *curve_radius,
  115. const float3 *key_steps,
  116. size_t num_curve_keys,
  117. size_t num_steps,
  118. size_t step,
  119. size_t k0,
  120. size_t k1,
  121. float4 r_keys[2]) const;
  122. void cardinal_keys_for_step(const float3 *curve_keys,
  123. const float *curve_radius,
  124. const float3 *key_steps,
  125. size_t num_curve_keys,
  126. size_t num_steps,
  127. size_t step,
  128. size_t k0,
  129. size_t k1,
  130. size_t k2,
  131. size_t k3,
  132. float4 r_keys[4]) const;
  133. };
  134. Curve get_curve(size_t i) const
  135. {
  136. int first = curve_first_key[i];
  137. int next_first = (i + 1 < curve_first_key.size()) ? curve_first_key[i + 1] : curve_keys.size();
  138. Curve curve = {first, next_first - first};
  139. return curve;
  140. }
  141. size_t num_curves() const
  142. {
  143. return curve_first_key.size();
  144. }
  145. /* Mesh SubdFace */
  146. struct SubdFace {
  147. int start_corner;
  148. int num_corners;
  149. int shader;
  150. bool smooth;
  151. int ptex_offset;
  152. bool is_quad()
  153. {
  154. return num_corners == 4;
  155. }
  156. float3 normal(const Mesh *mesh) const;
  157. int num_ptex_faces() const
  158. {
  159. return num_corners == 4 ? 1 : num_corners;
  160. }
  161. };
  162. struct SubdEdgeCrease {
  163. int v[2];
  164. float crease;
  165. };
  166. enum SubdivisionType {
  167. SUBDIVISION_NONE,
  168. SUBDIVISION_LINEAR,
  169. SUBDIVISION_CATMULL_CLARK,
  170. };
  171. SubdivisionType subdivision_type;
  172. /* Mesh Data */
  173. enum GeometryFlags {
  174. GEOMETRY_NONE = 0,
  175. GEOMETRY_TRIANGLES = (1 << 0),
  176. GEOMETRY_CURVES = (1 << 1),
  177. };
  178. int geometry_flags; /* used to distinguish meshes with no verts
  179. and meshed for which geometry is not created */
  180. array<int> triangles;
  181. array<float3> verts;
  182. array<int> shader;
  183. array<bool> smooth;
  184. /* used for storing patch info for subd triangles, only allocated if there are patches */
  185. array<int> triangle_patch; /* must be < 0 for non subd triangles */
  186. array<float2> vert_patch_uv;
  187. float volume_isovalue;
  188. bool has_volume; /* Set in the device_update_flags(). */
  189. bool has_surface_bssrdf; /* Set in the device_update_flags(). */
  190. array<float3> curve_keys;
  191. array<float> curve_radius;
  192. array<int> curve_first_key;
  193. array<int> curve_shader;
  194. array<SubdFace> subd_faces;
  195. array<int> subd_face_corners;
  196. int num_ngons;
  197. array<SubdEdgeCrease> subd_creases;
  198. SubdParams *subd_params;
  199. vector<Shader *> used_shaders;
  200. AttributeSet attributes;
  201. AttributeSet curve_attributes;
  202. AttributeSet subd_attributes;
  203. BoundBox bounds;
  204. bool transform_applied;
  205. bool transform_negative_scaled;
  206. Transform transform_normal;
  207. PackedPatchTable *patch_table;
  208. uint motion_steps;
  209. bool use_motion_blur;
  210. /* Update Flags */
  211. bool need_update;
  212. bool need_update_rebuild;
  213. /* BVH */
  214. BVH *bvh;
  215. size_t tri_offset;
  216. size_t vert_offset;
  217. size_t curve_offset;
  218. size_t curvekey_offset;
  219. size_t patch_offset;
  220. size_t patch_table_offset;
  221. size_t face_offset;
  222. size_t corner_offset;
  223. size_t attr_map_offset;
  224. size_t num_subd_verts;
  225. /* Functions */
  226. Mesh();
  227. ~Mesh();
  228. void resize_mesh(int numverts, int numfaces);
  229. void reserve_mesh(int numverts, int numfaces);
  230. void resize_curves(int numcurves, int numkeys);
  231. void reserve_curves(int numcurves, int numkeys);
  232. void resize_subd_faces(int numfaces, int num_ngons, int numcorners);
  233. void reserve_subd_faces(int numfaces, int num_ngons, int numcorners);
  234. void clear(bool preserve_voxel_data = false);
  235. void add_vertex(float3 P);
  236. void add_vertex_slow(float3 P);
  237. void add_triangle(int v0, int v1, int v2, int shader, bool smooth);
  238. void add_curve_key(float3 loc, float radius);
  239. void add_curve(int first_key, int shader);
  240. void add_subd_face(int *corners, int num_corners, int shader_, bool smooth_);
  241. void compute_bounds();
  242. void add_face_normals();
  243. void add_vertex_normals();
  244. void add_undisplaced();
  245. void pack_shaders(Scene *scene, uint *shader);
  246. void pack_normals(float4 *vnormal);
  247. void pack_verts(const vector<uint> &tri_prim_index,
  248. uint4 *tri_vindex,
  249. uint *tri_patch,
  250. float2 *tri_patch_uv,
  251. size_t vert_offset,
  252. size_t tri_offset);
  253. void pack_curves(Scene *scene, float4 *curve_key_co, float4 *curve_data, size_t curvekey_offset);
  254. void pack_patches(uint *patch_data, uint vert_offset, uint face_offset, uint corner_offset);
  255. void compute_bvh(Device *device,
  256. DeviceScene *dscene,
  257. SceneParams *params,
  258. Progress *progress,
  259. int n,
  260. int total);
  261. bool need_attribute(Scene *scene, AttributeStandard std);
  262. bool need_attribute(Scene *scene, ustring name);
  263. void tag_update(Scene *scene, bool rebuild);
  264. bool has_motion_blur() const;
  265. bool has_true_displacement() const;
  266. /* Convert between normalized -1..1 motion time and index
  267. * in the VERTEX_MOTION attribute. */
  268. float motion_time(int step) const;
  269. int motion_step(float time) const;
  270. /* Check whether the mesh should have own BVH built separately. Briefly,
  271. * own BVH is needed for mesh, if:
  272. *
  273. * - It is instanced multiple times, so each instance object should share the
  274. * same BVH tree.
  275. * - Special ray intersection is needed, for example to limit subsurface rays
  276. * to only the mesh itself.
  277. */
  278. bool need_build_bvh() const;
  279. /* Check if the mesh should be treated as instanced. */
  280. bool is_instanced() const;
  281. void tessellate(DiagSplit *split);
  282. };
  283. /* Mesh Manager */
  284. class MeshManager {
  285. public:
  286. bool need_update;
  287. bool need_flags_update;
  288. MeshManager();
  289. ~MeshManager();
  290. bool displace(Device *device, DeviceScene *dscene, Scene *scene, Mesh *mesh, Progress &progress);
  291. /* attributes */
  292. void update_osl_attributes(Device *device,
  293. Scene *scene,
  294. vector<AttributeRequestSet> &mesh_attributes);
  295. void update_svm_attributes(Device *device,
  296. DeviceScene *dscene,
  297. Scene *scene,
  298. vector<AttributeRequestSet> &mesh_attributes);
  299. void device_update_preprocess(Device *device, Scene *scene, Progress &progress);
  300. void device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress);
  301. void device_free(Device *device, DeviceScene *dscene);
  302. void tag_update(Scene *scene);
  303. void create_volume_mesh(Scene *scene, Mesh *mesh, Progress &progress);
  304. void collect_statistics(const Scene *scene, RenderStats *stats);
  305. protected:
  306. /* Calculate verts/triangles/curves offsets in global arrays. */
  307. void mesh_calc_offset(Scene *scene);
  308. void device_update_object(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress);
  309. void device_update_mesh(Device *device,
  310. DeviceScene *dscene,
  311. Scene *scene,
  312. bool for_displacement,
  313. Progress &progress);
  314. void device_update_attributes(Device *device,
  315. DeviceScene *dscene,
  316. Scene *scene,
  317. Progress &progress);
  318. void device_update_bvh(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress);
  319. void device_update_displacement_images(Device *device, Scene *scene, Progress &progress);
  320. void device_update_volume_images(Device *device, Scene *scene, Progress &progress);
  321. };
  322. CCL_NAMESPACE_END
  323. #endif /* __MESH_H__ */