Blender V5.0
geometry_bvh.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "bvh/bvh.h"
6#include "bvh/bvh2.h"
7#include "bvh/params.h"
8
9#include "device/device.h"
10
11#include "scene/attribute.h"
12#include "scene/camera.h"
13#include "scene/geometry.h"
14#include "scene/light.h"
15#include "scene/mesh.h"
16#include "scene/object.h"
17#include "scene/scene.h"
18#include "scene/shader.h"
19#include "scene/shader_nodes.h"
20
21#include "util/log.h"
22#include "util/progress.h"
23
25
27 DeviceScene *dscene,
29 Progress *progress,
30 const size_t n,
31 const size_t total)
32{
33 if (progress->get_cancel()) {
34 return;
35 }
36
38
39 const BVHLayout bvh_layout = BVHParams::best_bvh_layout(
40 params->bvh_layout, device->get_bvh_layout_mask(dscene->data.kernel_features));
41 if (need_build_bvh(bvh_layout)) {
42 string msg = "Updating Geometry BVH ";
43 if (name.empty()) {
44 msg += string_printf("%u/%u", (uint)(n + 1), (uint)total);
45 }
46 else {
47 msg += string_printf("%s %u/%u", name.c_str(), (uint)(n + 1), (uint)total);
48 }
49
50 Object object;
51
52 /* Ensure all visibility bits are set at the geometry level BVH. In
53 * the object level BVH is where actual visibility is tested. */
54 object.set_is_shadow_catcher(true);
55 object.set_visibility(~0);
56
57 object.set_geometry(this);
58
59 vector<Geometry *> geometry;
60 geometry.push_back(this);
61 vector<Object *> objects;
62 objects.push_back(&object);
63
64 if (bvh && !need_update_rebuild && params->bvh_type == BVH_TYPE_DYNAMIC) {
65 progress->set_status(msg, "Refitting BVH");
66
67 bvh->replace_geometry(geometry, objects);
68
69 device->build_bvh(bvh.get(), *progress, true);
70 }
71 else {
72 progress->set_status(msg, "Building BVH");
73
74 BVHParams bparams;
75 bparams.use_spatial_split = params->use_bvh_spatial_split;
76 bparams.use_compact_structure = params->use_bvh_compact_structure;
77 bparams.bvh_layout = bvh_layout;
78 bparams.use_unaligned_nodes = dscene->data.bvh.have_curves &&
79 params->use_bvh_unaligned_nodes;
80 bparams.num_motion_triangle_steps = params->num_bvh_time_steps;
81 bparams.num_motion_curve_steps = params->num_bvh_time_steps;
82 bparams.num_motion_point_steps = params->num_bvh_time_steps;
83 bparams.bvh_type = params->bvh_type;
84 bparams.curve_subdivisions = params->curve_subdivisions();
85
86 bvh = BVH::create(bparams, geometry, objects, device);
87 MEM_GUARDED_CALL(progress, device->build_bvh, bvh.get(), *progress, false);
88 }
89 }
90
91 need_update_rebuild = false;
93}
94
96 DeviceScene *dscene,
97 Scene *scene,
98 Progress &progress)
99{
100 /* bvh build */
101 progress.set_status("Updating Scene BVH", "Building");
102
103 BVHParams bparams;
104 bparams.top_level = true;
106 scene->params.bvh_layout, device->get_bvh_layout_mask(dscene->data.kernel_features));
108 bparams.use_unaligned_nodes = dscene->data.bvh.have_curves &&
113 bparams.bvh_type = scene->params.bvh_type;
115
116 LOG_INFO << "Using " << bvh_layout_name(bparams.bvh_layout) << " layout.";
117
118 const bool can_refit = scene->bvh != nullptr && scene->params.bvh_type == BVH_TYPE_DYNAMIC &&
121
122 BVH *bvh = scene->bvh.get();
123 if (bvh == nullptr) {
124 scene->bvh = BVH::create(bparams, scene->geometry, scene->objects, device);
125 bvh = scene->bvh.get();
126 }
127
128 device->build_bvh(bvh, progress, can_refit);
129
130 if (progress.get_cancel()) {
131 return;
132 }
133
134 const bool has_bvh2_layout = (bparams.bvh_layout == BVH_LAYOUT_BVH2);
135
136 PackedBVH pack;
137 if (has_bvh2_layout) {
138 pack = std::move(static_cast<BVH2 *>(bvh)->pack);
139 }
140 else {
141 pack.root_index = -1;
142 }
143
144 /* copy to device */
145 progress.set_status("Updating Scene BVH", "Copying BVH to device");
146
147 /* When using BVH2, we always have to copy/update the data as its layout is dependent on the
148 * BVH's leaf nodes which may be different when the objects or vertices move. */
149
150 if (pack.nodes.size()) {
151 dscene->bvh_nodes.steal_data(pack.nodes);
152 dscene->bvh_nodes.copy_to_device();
153 }
154 if (pack.leaf_nodes.size()) {
157 }
158 if (pack.object_node.size()) {
159 dscene->object_node.steal_data(pack.object_node);
160 dscene->object_node.copy_to_device();
161 }
162 if (pack.prim_type.size()) {
163 dscene->prim_type.steal_data(pack.prim_type);
164 dscene->prim_type.copy_to_device();
165 }
166 if (pack.prim_visibility.size()) {
169 }
170 if (pack.prim_index.size()) {
171 dscene->prim_index.steal_data(pack.prim_index);
172 dscene->prim_index.copy_to_device();
173 }
174 if (pack.prim_object.size()) {
175 dscene->prim_object.steal_data(pack.prim_object);
176 dscene->prim_object.copy_to_device();
177 }
178 if (pack.prim_time.size()) {
179 dscene->prim_time.steal_data(pack.prim_time);
180 dscene->prim_time.copy_to_device();
181 }
182
183 dscene->data.bvh.root = pack.root_index;
184 dscene->data.bvh.use_bvh_steps = (scene->params.num_bvh_time_steps != 0);
185 dscene->data.bvh.curve_subdivisions = scene->params.curve_subdivisions();
186
187#ifdef WITH_EMBREE
188 /* The scene handle is set in 'CPUDevice::const_copy_to' and 'OptiXDevice::const_copy_to' */
189 dscene->data.device_bvh = nullptr;
190#else
191 dscene->data.device_bvh = 0;
192#endif
193}
194
unsigned int uint
CCL_NAMESPACE_BEGIN const char * bvh_layout_name(BVHLayout layout)
Definition bvh.cpp:32
Definition bvh2.h:38
bool use_spatial_split
Definition params.h:60
int num_motion_triangle_steps
Definition params.h:100
static BVHLayout best_bvh_layout(BVHLayout requested_layout, BVHLayoutMask supported_layouts)
Definition bvh.cpp:65
BVHLayout bvh_layout
Definition params.h:83
bool use_compact_structure
Definition params.h:91
bool use_unaligned_nodes
Definition params.h:88
int curve_subdivisions
Definition params.h:108
int num_motion_point_steps
Definition params.h:102
bool top_level
Definition params.h:80
int bvh_type
Definition params.h:105
int num_motion_curve_steps
Definition params.h:101
Definition bvh/bvh.h:67
static unique_ptr< BVH > create(const BVHParams &params, const vector< Geometry * > &geometry, const vector< Object * > &objects, Device *device)
Definition bvh.cpp:97
device_vector< float2 > prim_time
Definition devicescene.h:24
device_vector< uint > prim_visibility
Definition devicescene.h:21
device_vector< int > object_node
Definition devicescene.h:19
device_vector< int > prim_object
Definition devicescene.h:23
device_vector< int4 > bvh_leaf_nodes
Definition devicescene.h:18
KernelData data
Definition devicescene.h:94
device_vector< int > prim_type
Definition devicescene.h:20
device_vector< int4 > bvh_nodes
Definition devicescene.h:17
device_vector< int > prim_index
Definition devicescene.h:22
virtual void build_bvh(BVH *bvh, Progress &progress, bool refit)
virtual BVHLayoutMask get_bvh_layout_mask(const uint kernel_features) const =0
void device_update_bvh(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
virtual void compute_bounds()=0
bool need_update_bvh_for_offset
bool need_build_bvh(BVHLayout layout) const
void compute_bvh(Device *device, DeviceScene *dscene, SceneParams *params, Progress *progress, const size_t n, size_t total)
unique_ptr< BVH > bvh
bool need_update_rebuild
bool get_cancel() const
Definition progress.h:77
void set_status(const string &status_, const string &substatus_="")
Definition progress.h:248
BVHLayout bvh_layout
Definition scene.h:65
bool use_bvh_unaligned_nodes
Definition scene.h:70
bool use_bvh_spatial_split
Definition scene.h:68
int curve_subdivisions()
Definition scene.h:105
int num_bvh_time_steps
Definition scene.h:71
BVHType bvh_type
Definition scene.h:67
size_t size() const
void steal_data(array< T > &from)
#define CCL_NAMESPACE_END
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
@ BVH_LAYOUT_OPTIX
@ BVH_LAYOUT_METAL
@ BVH_LAYOUT_BVH2
#define LOG_INFO
Definition log.h:106
#define MEM_GUARDED_CALL(progress, func,...)
KernelBVHLayout BVHLayout
Definition params.h:22
@ BVH_TYPE_DYNAMIC
Definition params.h:33
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:23
ustring name
Definition graph/node.h:177
array< int > prim_index
Definition bvh/bvh.h:50
array< int > prim_type
Definition bvh/bvh.h:45
array< int4 > nodes
Definition bvh/bvh.h:39
array< uint > prim_visibility
Definition bvh/bvh.h:47
array< float2 > prim_time
Definition bvh/bvh.h:54
array< int4 > leaf_nodes
Definition bvh/bvh.h:41
array< int > prim_object
Definition bvh/bvh.h:52
int root_index
Definition bvh/bvh.h:57
array< int > object_node
Definition bvh/bvh.h:43
SceneParams params
Definition scene.h:168
unique_ptr_vector< Geometry > geometry
Definition scene.h:140
unique_ptr_vector< Object > objects
Definition scene.h:141
unique_ptr< BVH > bvh
Definition scene.h:123