Blender  V2.93
geometry.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2020 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 __GEOMETRY_H__
18 #define __GEOMETRY_H__
19 
20 #include "graph/node.h"
21 
22 #include "bvh/bvh_params.h"
23 
24 #include "render/attribute.h"
25 
26 #include "util/util_boundbox.h"
27 #include "util/util_set.h"
28 #include "util/util_transform.h"
29 #include "util/util_types.h"
30 #include "util/util_vector.h"
31 
33 
34 class BVH;
35 class Device;
36 class DeviceScene;
37 class Mesh;
38 class Progress;
39 class RenderStats;
40 class Scene;
41 class SceneParams;
42 class Shader;
43 class Volume;
44 struct PackedBVH;
45 
46 /* Flags used to determine which geometry data need to be packed. */
48  PACK_NONE = 0u,
49 
50  /* Pack the geometry information (e.g. triangle or curve keys indices). */
51  PACK_GEOMETRY = (1u << 0),
52 
53  /* Pack the vertice, for Meshes and Volumes' bouding meshes. */
54  PACK_VERTICES = (1u << 1),
55 
56  /* Pack the visibility flags for each triangle or curve. */
57  PACK_VISIBILITY = (1u << 2),
58 
60 };
61 
62 PackFlags operator|=(PackFlags &pack_flags, uint32_t value);
63 
64 /* Geometry
65  *
66  * Base class for geometric types like Mesh and Hair. */
67 
68 class Geometry : public Node {
69  public:
71 
72  enum Type {
76  };
77 
79 
80  /* Attributes */
82 
83  /* Shaders */
85 
86  /* Transform */
91 
92  /* Motion Blur */
93  NODE_SOCKET_API(uint, motion_steps)
94  NODE_SOCKET_API(bool, use_motion_blur)
95 
96  /* Maximum number of motion steps supported (due to Embree). */
97  static const uint MAX_MOTION_STEPS = 129;
98 
99  /* BVH */
102  size_t prim_offset;
104 
105  /* Shader Properties */
106  bool has_volume; /* Set in the device_update_flags(). */
107  bool has_surface_bssrdf; /* Set in the device_update_flags(). */
108 
109  /* Update Flags */
112 
113  /* Index into scene->geometry (only valid during update) */
114  size_t index;
115 
116  /* Constructor/Destructor */
117  explicit Geometry(const NodeType *node_type, const Type type);
118  virtual ~Geometry();
119 
120  /* Geometry */
121  virtual void clear(bool preserve_shaders = false);
122  virtual void compute_bounds() = 0;
123  virtual void apply_transform(const Transform &tfm, const bool apply_to_motion) = 0;
124 
125  /* Attribute Requests */
127  bool need_attribute(Scene *scene, ustring name);
128 
130 
131  /* UDIM */
132  virtual void get_uv_tiles(ustring map, unordered_set<int> &tiles) = 0;
133 
134  /* Convert between normalized -1..1 motion time and index in the
135  * VERTEX_MOTION attribute. */
136  float motion_time(int step) const;
137  int motion_step(float time) const;
138 
139  /* BVH */
140  void compute_bvh(Device *device,
141  DeviceScene *dscene,
143  Progress *progress,
144  int n,
145  int total);
146 
147  virtual void pack_primitives(PackedBVH *pack,
148  int object,
149  uint visibility,
150  PackFlags pack_flags) = 0;
151 
152  /* Check whether the geometry should have own BVH built separately. Briefly,
153  * own BVH is needed for geometry, if:
154  *
155  * - It is instanced multiple times, so each instance object should share the
156  * same BVH tree.
157  * - Special ray intersection is needed, for example to limit subsurface rays
158  * to only the geometry itself.
159  * - The BVH layout requires the top level to only contain instances.
160  */
161  bool need_build_bvh(BVHLayout layout) const;
162 
163  /* Test if the geometry should be treated as instanced. */
164  bool is_instanced() const;
165 
166  bool has_true_displacement() const;
167  bool has_motion_blur() const;
168  bool has_voxel_attributes() const;
169 
170  bool is_mesh() const
171  {
172  return geometry_type == MESH;
173  }
174 
175  bool is_hair() const
176  {
177  return geometry_type == HAIR;
178  }
179 
180  bool is_volume() const
181  {
182  return geometry_type == VOLUME;
183  }
184 
185  /* Updates */
186  void tag_update(Scene *scene, bool rebuild);
187 
188  void tag_bvh_update(bool rebuild);
189 };
190 
191 /* Geometry Manager */
192 
194  uint32_t update_flags;
195 
196  public:
197  enum : uint32_t {
198  UV_PASS_NEEDED = (1 << 0),
199  MOTION_PASS_NEEDED = (1 << 1),
200  GEOMETRY_MODIFIED = (1 << 2),
201  OBJECT_MANAGER = (1 << 3),
202  MESH_ADDED = (1 << 4),
203  MESH_REMOVED = (1 << 5),
204  HAIR_ADDED = (1 << 6),
205  HAIR_REMOVED = (1 << 7),
206 
209 
212 
213  TRANSFORM_MODIFIED = (1 << 10),
214 
215  VISIBILITY_MODIFIED = (1 << 11),
216 
217  /* tag everything in the manager for an update */
218  UPDATE_ALL = ~0u,
219 
221  };
222 
223  /* Update Flags */
225 
226  /* Constructor/Destructor */
227  GeometryManager();
229 
230  /* Device Updates */
231  void device_update_preprocess(Device *device, Scene *scene, Progress &progress);
232  void device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress);
233  void device_free(Device *device, DeviceScene *dscene, bool force_free);
234 
235  /* Updates */
236  void tag_update(Scene *scene, uint32_t flag);
237 
238  bool need_update() const;
239 
240  /* Statistics */
241  void collect_statistics(const Scene *scene, RenderStats *stats);
242 
243  protected:
244  bool displace(Device *device, DeviceScene *dscene, Scene *scene, Mesh *mesh, Progress &progress);
245 
246  void create_volume_mesh(Volume *volume, Progress &progress);
247 
248  /* Attributes */
249  void update_osl_attributes(Device *device,
250  Scene *scene,
251  vector<AttributeRequestSet> &geom_attributes);
252  void update_svm_attributes(Device *device,
253  DeviceScene *dscene,
254  Scene *scene,
255  vector<AttributeRequestSet> &geom_attributes,
256  vector<AttributeRequestSet> &object_attributes);
257 
258  /* Compute verts/triangles/curves offsets in global arrays. */
259  void mesh_calc_offset(Scene *scene, BVHLayout bvh_layout);
260 
261  void device_update_object(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress);
262 
263  void device_update_mesh(Device *device,
264  DeviceScene *dscene,
265  Scene *scene,
266  bool for_displacement,
267  Progress &progress);
268 
269  void device_update_attributes(Device *device,
270  DeviceScene *dscene,
271  Scene *scene,
272  Progress &progress);
273 
274  void device_update_bvh(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress);
275 
276  void device_update_displacement_images(Device *device, Scene *scene, Progress &progress);
277 
278  void device_update_volume_images(Device *device, Scene *scene, Progress &progress);
279 
280  private:
281  static void update_attribute_element_offset(Geometry *geom,
282  device_vector<float> &attr_float,
283  size_t &attr_float_offset,
284  device_vector<float2> &attr_float2,
285  size_t &attr_float2_offset,
286  device_vector<float4> &attr_float3,
287  size_t &attr_float3_offset,
288  device_vector<uchar4> &attr_uchar4,
289  size_t &attr_uchar4_offset,
290  Attribute *mattr,
291  AttributePrimitive prim,
292  TypeDesc &type,
293  AttributeDescriptor &desc);
294 };
295 
297 
298 #endif /* __GEOMETRY_H__ */
unsigned int uint
Definition: BLI_sys_types.h:83
_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
CCL_NAMESPACE_BEGIN typedef KernelBVHLayout BVHLayout
Definition: bvh_params.h:32
Definition: bvh/bvh.h:80
Definition: device.h:293
void device_update_displacement_images(Device *device, Scene *scene, Progress &progress)
Definition: geometry.cpp:1675
void update_osl_attributes(Device *device, Scene *scene, vector< AttributeRequestSet > &geom_attributes)
Definition: geometry.cpp:311
void device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
Definition: geometry.cpp:1749
void device_update_mesh(Device *device, DeviceScene *dscene, Scene *scene, bool for_displacement, Progress &progress)
Definition: geometry.cpp:1025
void mesh_calc_offset(Scene *scene, BVHLayout bvh_layout)
Definition: geometry.cpp:956
bool displace(Device *device, DeviceScene *dscene, Scene *scene, Mesh *mesh, Progress &progress)
void device_update_attributes(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
Definition: geometry.cpp:715
@ SHADER_DISPLACEMENT_MODIFIED
Definition: geometry.h:208
@ SHADER_ATTRIBUTE_MODIFIED
Definition: geometry.h:207
void device_free(Device *device, DeviceScene *dscene, bool force_free)
Definition: geometry.cpp:2085
void device_update_object(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
void tag_update(Scene *scene, uint32_t flag)
Definition: geometry.cpp:2127
void create_volume_mesh(Volume *volume, Progress &progress)
Definition: volume.cpp:502
void device_update_bvh(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
Definition: geometry.cpp:1220
void collect_statistics(const Scene *scene, RenderStats *stats)
Definition: geometry.cpp:2142
void device_update_preprocess(Device *device, Scene *scene, Progress &progress)
Definition: geometry.cpp:1451
void device_update_volume_images(Device *device, Scene *scene, Progress &progress)
Definition: geometry.cpp:1713
void update_svm_attributes(Device *device, DeviceScene *dscene, Scene *scene, vector< AttributeRequestSet > &geom_attributes, vector< AttributeRequestSet > &object_attributes)
Definition: geometry.cpp:470
bool need_flags_update
Definition: geometry.h:224
bool need_update() const
Definition: geometry.cpp:2137
Transform transform_normal
Definition: geometry.h:90
void compute_bvh(Device *device, DeviceScene *dscene, SceneParams *params, Progress *progress, int n, int total)
Definition: geometry.cpp:199
float motion_time(int step) const
Definition: geometry.cpp:145
Type geometry_type
Definition: geometry.h:78
virtual void compute_bounds()=0
@ MESH
Definition: geometry.h:73
@ VOLUME
Definition: geometry.h:75
@ HAIR
Definition: geometry.h:74
BoundBox bounds
Definition: geometry.h:87
virtual void pack_primitives(PackedBVH *pack, int object, uint visibility, PackFlags pack_flags)=0
bool need_update_bvh_for_offset
Definition: geometry.h:111
bool transform_applied
Definition: geometry.h:88
static const uint MAX_MOTION_STEPS
Definition: geometry.h:97
size_t optix_prim_offset
Definition: geometry.h:103
BVH * bvh
Definition: geometry.h:100
bool has_volume
Definition: geometry.h:106
bool has_voxel_attributes() const
Definition: geometry.cpp:260
bool has_true_displacement() const
Definition: geometry.cpp:187
bool need_build_bvh(BVHLayout layout) const
Definition: geometry.cpp:171
size_t index
Definition: geometry.h:114
bool is_volume() const
Definition: geometry.h:180
int motion_step(float time) const
Definition: geometry.cpp:150
bool need_attribute(Scene *scene, AttributeStandard std)
Definition: geometry.cpp:102
bool is_hair() const
Definition: geometry.h:175
bool has_surface_bssrdf
Definition: geometry.h:107
bool is_instanced() const
Definition: geometry.cpp:177
AttributeRequestSet needed_attributes()
Definition: geometry.cpp:133
size_t attr_map_offset
Definition: geometry.h:101
size_t prim_offset
Definition: geometry.h:102
bool has_motion_blur() const
Definition: geometry.cpp:255
void tag_update(Scene *scene, bool rebuild)
Definition: geometry.cpp:271
virtual void get_uv_tiles(ustring map, unordered_set< int > &tiles)=0
bool need_update_rebuild
Definition: geometry.h:110
AttributeSet attributes
Definition: geometry.h:81
bool is_mesh() const
Definition: geometry.h:170
bool transform_negative_scaled
Definition: geometry.h:89
virtual void apply_transform(const Transform &tfm, const bool apply_to_motion)=0
void tag_bvh_update(bool rebuild)
Definition: geometry.cpp:290
virtual void clear(bool preserve_shaders=false)
Definition: geometry.cpp:91
Definition: shader.h:80
double time
Scene scene
PackFlags operator|=(PackFlags &pack_flags, uint32_t value)
Definition: geometry.cpp:49
PackFlags
Definition: geometry.h:47
@ PACK_ALL
Definition: geometry.h:59
@ PACK_NONE
Definition: geometry.h:48
@ PACK_VERTICES
Definition: geometry.h:54
@ PACK_GEOMETRY
Definition: geometry.h:51
@ PACK_VISIBILITY
Definition: geometry.h:57
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
#define CCL_NAMESPACE_END
AttributeStandard
Definition: kernel_types.h:744
AttributePrimitive
Definition: kernel_types.h:722
#define NODE_SOCKET_API_ARRAY(type_, name)
Definition: node.h:71
#define NODE_SOCKET_API(type_, name)
Definition: node.h:63
#define NODE_ABSTRACT_DECLARE
Definition: node_type.h:166
unsigned int uint32_t
Definition: stdint.h:83
Definition: node.h:98
const NodeType * type
Definition: node.h:175
ustring name
Definition: node.h:174