Blender V5.0
blender/geometry.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 "scene/curves.h"
6#include "scene/hair.h"
7#include "scene/light.h"
8#include "scene/mesh.h"
9#include "scene/object.h"
10#include "scene/pointcloud.h"
11#include "scene/volume.h"
12
13#include "blender/sync.h"
14#include "blender/util.h"
15
16#include "util/task.h"
17
19
20static Geometry::Type determine_geom_type(BObjectInfo &b_ob_info, bool use_particle_hair)
21{
22 if (b_ob_info.object_data.is_a(&RNA_Light)) {
23 return Geometry::LIGHT;
24 }
25
26 if (b_ob_info.object_data.is_a(&RNA_Curves) || use_particle_hair) {
27 return Geometry::HAIR;
28 }
29
30 if (b_ob_info.object_data.is_a(&RNA_PointCloud)) {
32 }
33
34 if (b_ob_info.object_data.is_a(&RNA_Volume) ||
35 (b_ob_info.object_data ==
38 {
39 return Geometry::VOLUME;
40 }
41
42 return Geometry::MESH;
43}
44
45array<Node *> BlenderSync::find_used_shaders(BL::Object &b_ob)
46{
47 array<Node *> used_shaders;
48
49 if (b_ob.type() == BL::Object::type_LIGHT) {
50 find_shader(b_ob.data(), used_shaders, scene->default_light);
51 return used_shaders;
52 }
53
54 BL::Material material_override = view_layer.material_override;
55 Shader *default_shader = (b_ob.type() == BL::Object::type_VOLUME) ? scene->default_volume :
56 scene->default_surface;
57
58 for (BL::MaterialSlot &b_slot : b_ob.material_slots) {
59 if (material_override) {
60 find_shader(material_override, used_shaders, default_shader);
61 }
62 else {
63 BL::ID b_material(b_slot.material());
64 find_shader(b_material, used_shaders, default_shader);
65 }
66 }
67
68 if (used_shaders.size() == 0) {
69 if (material_override) {
70 find_shader(material_override, used_shaders, default_shader);
71 }
72 else {
73 used_shaders.push_back_slow(default_shader);
74 }
75 }
76
77 return used_shaders;
78}
79
80Geometry *BlenderSync::sync_geometry(BObjectInfo &b_ob_info,
81 bool object_updated,
82 bool use_particle_hair,
84{
85 /* Test if we can instance or if the object is modified. */
86 const Geometry::Type geom_type = determine_geom_type(b_ob_info, use_particle_hair);
87 BL::ID const b_key_id = (b_ob_info.is_real_object_data() &&
88 BKE_object_is_modified(b_ob_info.real_object)) ?
89 b_ob_info.real_object :
90 b_ob_info.object_data;
91 const GeometryKey key(b_key_id.ptr.data, geom_type);
92
93 /* Find shader indices. */
94 array<Node *> used_shaders = find_used_shaders(b_ob_info.iter_object);
95
96 /* Ensure we only sync instanced geometry once. */
97 Geometry *geom = geometry_map.find(key);
98 if (geom) {
99 if (geometry_synced.find(geom) != geometry_synced.end()) {
100 return geom;
101 }
102 }
103
104 /* Test if we need to sync. */
105 bool sync = true;
106 if (geom == nullptr) {
107 /* Add new geometry if it did not exist yet. */
108 if (geom_type == Geometry::LIGHT) {
109 geom = scene->create_node<Light>();
110 }
111 else if (geom_type == Geometry::HAIR) {
112 geom = scene->create_node<Hair>();
113 }
114 else if (geom_type == Geometry::VOLUME) {
115 geom = scene->create_node<Volume>();
116 }
117 else if (geom_type == Geometry::POINTCLOUD) {
118 geom = scene->create_node<PointCloud>();
119 }
120 else {
121 geom = scene->create_node<Mesh>();
122 }
123 geometry_map.add(key, geom);
124 }
125 else {
126 /* Test if we need to update existing geometry. */
127 sync = geometry_map.update(geom, b_key_id);
128 }
129
130 if (!sync) {
131 /* If transform was applied to geometry, need full update. */
132 if (object_updated && geom->transform_applied) {
133 ;
134 }
135 /* Test if shaders changed, these can be object level so geometry
136 * does not get tagged for recalc. */
137 else if (geom->get_used_shaders() != used_shaders) {
138 ;
139 }
140 else {
141 /* Even if not tagged for recalc, we may need to sync anyway
142 * because the shader needs different geometry attributes. */
143 bool attribute_recalc = false;
144
145 for (Node *node : geom->get_used_shaders()) {
146 Shader *shader = static_cast<Shader *>(node);
147 if (shader->need_update_geometry()) {
148 attribute_recalc = true;
149 }
150 }
151
152 if (!attribute_recalc) {
153 return geom;
154 }
155 }
156 }
157
158 geometry_synced.insert(geom);
159
160 geom->name = ustring(b_ob_info.object_data.name().c_str());
161
162 /* Store the shaders immediately for the object attribute code. */
163 geom->set_used_shaders(used_shaders);
164
165 auto sync_func = [this, geom_type, b_ob_info, geom]() mutable {
166 if (progress.get_cancel()) {
167 return;
168 }
169
170 progress.set_sync_status("Synchronizing object", b_ob_info.real_object.name());
171
172 if (geom_type == Geometry::LIGHT) {
173 Light *light = static_cast<Light *>(geom);
174 sync_light(b_ob_info, light);
175 }
176 else if (geom_type == Geometry::HAIR) {
177 Hair *hair = static_cast<Hair *>(geom);
178 sync_hair(b_ob_info, hair);
179 }
180 else if (geom_type == Geometry::VOLUME) {
181 Volume *volume = static_cast<Volume *>(geom);
182 sync_volume(b_ob_info, volume);
183 }
184 else if (geom_type == Geometry::POINTCLOUD) {
185 PointCloud *pointcloud = static_cast<PointCloud *>(geom);
186 sync_pointcloud(pointcloud, b_ob_info);
187 }
188 else {
189 Mesh *mesh = static_cast<Mesh *>(geom);
190 sync_mesh(b_ob_info, mesh);
191 }
192 };
193
194 /* Defer the actual geometry sync to the task_pool for multithreading */
195 if (task_pool) {
197 }
198 else {
199 sync_func();
200 }
201
202 return geom;
203}
204
205void BlenderSync::sync_geometry_motion(BObjectInfo &b_ob_info,
206 Object *object,
207 const float motion_time,
208 bool use_particle_hair,
210{
211 /* Ensure we only sync instanced geometry once. */
212 Geometry *geom = object->get_geometry();
213
214 if (geometry_motion_synced.find(geom) != geometry_motion_synced.end() ||
215 geometry_motion_attribute_synced.find(geom) != geometry_motion_attribute_synced.end())
216 {
217 return;
218 }
219
220 geometry_motion_synced.insert(geom);
221
222 /* Ensure we only motion sync geometry that also had geometry synced, to avoid
223 * unnecessary work and to ensure that its attributes were clear. */
224 if (geometry_synced.find(geom) == geometry_synced.end()) {
225 return;
226 }
227
228 /* Nothing to do for lights. */
229 if (geom->is_light()) {
230 return;
231 }
232
233 /* If the geometry already has motion blur from a velocity attribute, don't
234 * set the geometry motion steps again.
235 *
236 * Otherwise, setting geometry motion steps is done here to avoid concurrency issues.
237 * - It can't be done earlier in sync_object_motion_init because sync_geometry
238 * runs in parallel, and has_motion_blur would check attributes while
239 * sync_geometry is potentially creating the attribute from velocity.
240 * - It needs to happen before the parallel motion sync that happens right after
241 * this, because that can create the attribute from neighboring frames.
242 * Copying the motion steps from the object here solves this. */
243 if (!geom->has_motion_blur()) {
244 geom->set_motion_steps(object->get_motion().size());
245 }
246
247 /* Find time matching motion step required by geometry. */
248 const int motion_step = geom->motion_step(motion_time);
249 if (motion_step < 0) {
250 return;
251 }
252
253 auto sync_func = [this, b_ob_info, use_particle_hair, motion_step, geom]() mutable {
254 if (progress.get_cancel()) {
255 return;
256 }
257
258 if (b_ob_info.object_data.is_a(&RNA_Curves) || use_particle_hair) {
259 Hair *hair = static_cast<Hair *>(geom);
260 sync_hair_motion(b_ob_info, hair, motion_step);
261 }
262 else if (b_ob_info.object_data.is_a(&RNA_Volume) ||
264 {
265 /* No volume motion blur support yet. */
266 }
267 else if (b_ob_info.object_data.is_a(&RNA_PointCloud)) {
268 PointCloud *pointcloud = static_cast<PointCloud *>(geom);
269 sync_pointcloud_motion(pointcloud, b_ob_info, motion_step);
270 }
271 else {
272 Mesh *mesh = static_cast<Mesh *>(geom);
273 sync_mesh_motion(b_ob_info, mesh, motion_step);
274 }
275 };
276
277 /* Defer the actual geometry sync to the task_pool for multithreading */
278 if (task_pool) {
280 }
281 else {
282 sync_func();
283 }
284}
285
struct Light Light
struct Mesh Mesh
struct PointCloud PointCloud
struct Volume Volume
static CCL_NAMESPACE_BEGIN Geometry::Type determine_geom_type(BObjectInfo &b_ob_info, bool use_particle_hair)
int motion_step(const float time) const
bool transform_applied
bool is_light() const
virtual bool has_motion_blur() const
bool need_update_geometry() const
size_t size() const
void push_back_slow(const T &t)
static BL::FluidDomainSettings object_fluid_gas_domain_find(BL::Object &b_ob)
static CCL_NAMESPACE_BEGIN BL::ID object_get_data(const BL::Object &b_ob, const bool use_adaptive_subdivision)
#define CCL_NAMESPACE_END
TaskPool * task_pool
static PyObject * sync_func(PyObject *, PyObject *args)
Definition python.cpp:396
BL::Object real_object
BL::Object iter_object
bool is_real_object_data() const
ustring name
Definition graph/node.h:177
void push(TaskRunFunction &&task)
Definition task.cpp:21