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