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