Blender V5.3
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
18#include "BKE_material.hh"
19#include "DNA_light_types.h"
20#include "DNA_material_types.h"
22
24
25static Geometry::Type determine_geom_type(BObjectInfo &b_ob_info, bool use_particle_hair)
26{
27 if (b_ob_info.object_data->id_type() == blender::ID_LA) {
28 blender::Light &b_light = *blender::id_cast<blender::Light *>(b_ob_info.object_data);
29 switch (b_light.type) {
34 case blender::LA_SUN:
38 default:
39 /* Should be handled in `sync_background_light()`. */
40 assert(false);
42 }
43 }
44
45 if (b_ob_info.object_data->id_type() == blender::ID_CV || use_particle_hair) {
46 return Geometry::HAIR;
47 }
48
49 if (b_ob_info.object_data->id_type() == blender::ID_PT) {
51 }
52
53 if (b_ob_info.object_data->id_type() == blender::ID_VO ||
54 (b_ob_info.object_data ==
55 object_get_data(*b_ob_info.real_object, b_ob_info.use_adaptive_subdivision) &&
56 object_fluid_gas_domain_find(*b_ob_info.real_object)))
57 {
58 return Geometry::VOLUME;
59 }
60
61 return Geometry::MESH;
62}
63
64static Shader *get_default_shader(const Scene *scene, const blender::Object &b_ob)
65{
66 if (b_ob.type == blender::OB_VOLUME) {
67 return scene->default_volume;
68 }
69
70 if (b_ob.type == blender::OB_POINTCLOUD &&
71 blender::id_cast<const blender::PointCloud *>(b_ob.data)->type ==
73 {
74 return scene->default_gsplat;
75 }
76
77 return scene->default_surface;
78}
79
80array<Node *> BlenderSync::find_used_shaders(blender::Object &b_ob)
81{
82 array<Node *> used_shaders;
83
84 if (b_ob.type == blender::OB_LAMP) {
85 find_shader(static_cast<blender::ID *>(b_ob.data), used_shaders, scene->default_light);
86 return used_shaders;
87 }
88
89 blender::Material *material_override = view_layer.material_override;
90 Shader *default_shader = get_default_shader(scene, b_ob);
91
92 for (const int i : blender::IndexRange(BKE_object_material_count_eval(&b_ob))) {
93 if (material_override) {
94 find_shader(&material_override->id, used_shaders, default_shader);
95 }
96 else {
97 blender::Material *b_material = BKE_object_material_get(&b_ob, i + 1);
98 find_shader(reinterpret_cast<blender::ID *>(b_material), used_shaders, default_shader);
99 }
100 }
101
102 if (used_shaders.size() == 0) {
103 if (material_override) {
104 find_shader(&material_override->id, used_shaders, default_shader);
105 }
106 else {
107 used_shaders.push_back_slow(default_shader);
108 }
109 }
110
111 return used_shaders;
112}
113
114Geometry *BlenderSync::sync_geometry(BObjectInfo &b_ob_info,
115 bool object_updated,
116 bool use_particle_hair,
117 TaskPool *task_pool)
118{
119 /* Test if we can instance or if the object is modified. */
120 const Geometry::Type geom_type = determine_geom_type(b_ob_info, use_particle_hair);
121 blender::ID *const b_key_id = (b_ob_info.is_real_object_data() &&
122 BKE_object_is_modified(*b_ob_info.real_object)) ?
123 &b_ob_info.real_object->id :
124 b_ob_info.object_data;
125 const GeometryKey key(b_key_id, geom_type);
126
127 /* Find shader indices. */
128 array<Node *> used_shaders = find_used_shaders(*b_ob_info.iter_object);
129
130 /* Ensure we only sync instanced geometry once. */
131 Geometry *geom = geometry_map.find(key);
132 if (geom) {
133 if (geometry_synced.contains(geom)) {
134 return geom;
135 }
136 }
137
138 /* Test if we need to sync. */
139 bool sync = true;
140 if (geom == nullptr) {
141 /* Add new geometry if it did not exist yet. */
142 if (geom_type == Geometry::POINT_LIGHT) {
143 geom = scene->create_light_node<PointLight>();
144 }
145 else if (geom_type == Geometry::SPOT_LIGHT) {
146 geom = scene->create_light_node<SpotLight>();
147 }
148 else if (geom_type == Geometry::SUN_LIGHT) {
149 geom = scene->create_light_node<SunLight>();
150 }
151 else if (geom_type == Geometry::AREA_LIGHT) {
152 geom = scene->create_light_node<AreaLight>();
153 }
154 else if (geom_type == Geometry::HAIR) {
155 geom = scene->create_node<Hair>();
156 }
157 else if (geom_type == Geometry::VOLUME) {
158 geom = scene->create_node<Volume>();
159 }
160 else if (geom_type == Geometry::POINTCLOUD) {
161 geom = scene->create_node<PointCloud>();
162 }
163 else {
164 assert(geom_type == Geometry::MESH);
165 geom = scene->create_node<Mesh>();
166 }
167 geometry_map.add(key, geom);
168 }
169 else {
170 /* Test if we need to update existing geometry. */
171 sync = geometry_map.update(geom, b_key_id);
172 }
173
174 if (!sync) {
175 /* If transform was applied to geometry, need full update. */
176 if (object_updated && geom->transform_applied) {
177 ;
178 }
179 /* Test if shaders changed, these can be object level so geometry
180 * does not get tagged for recalc. */
181 else if (geom->get_used_shaders() != used_shaders) {
182 ;
183 }
184 else {
185 /* Even if not tagged for recalc, we may need to sync anyway
186 * because the shader needs different geometry attributes. */
187 bool attribute_recalc = false;
188
189 for (Node *node : geom->get_used_shaders()) {
190 Shader *shader = static_cast<Shader *>(node);
191 if (shader->need_update_geometry()) {
192 attribute_recalc = true;
193 }
194 }
195
196 if (!attribute_recalc) {
197 return geom;
198 }
199 }
200 }
201
202 geometry_synced.insert(geom);
203
204 geom->name = ustring(BKE_id_name(*b_ob_info.object_data));
205
206 /* Store the shaders immediately for the object attribute code. */
207 geom->set_used_shaders(used_shaders);
208
209 auto sync_func = [this, geom_type, b_ob_info, geom]() mutable {
210 if (progress.get_cancel()) {
211 return;
212 }
213
214 progress.set_sync_status("Synchronizing object", BKE_id_name(b_ob_info.real_object->id));
215
216 if (geom->is_light()) {
217 Light *light = static_cast<Light *>(geom);
218 sync_light(b_ob_info, light);
219 }
220 else if (geom_type == Geometry::HAIR) {
221 Hair *hair = static_cast<Hair *>(geom);
222 sync_hair(b_ob_info, hair);
223 }
224 else if (geom_type == Geometry::VOLUME) {
225 Volume *volume = static_cast<Volume *>(geom);
226 sync_volume(b_ob_info, volume);
227 }
228 else if (geom_type == Geometry::POINTCLOUD) {
229 PointCloud *pointcloud = static_cast<PointCloud *>(geom);
230 sync_pointcloud(pointcloud, b_ob_info);
231 }
232 else {
233 Mesh *mesh = static_cast<Mesh *>(geom);
234 sync_mesh(b_ob_info, mesh);
235 }
236 };
237
238 /* Defer the actual geometry sync to the task_pool for multithreading */
239 if (task_pool) {
240 task_pool->push(sync_func);
241 }
242 else {
243 sync_func();
244 }
245
246 return geom;
247}
248
249void BlenderSync::sync_geometry_motion(BObjectInfo &b_ob_info,
250 Object *object,
251 const float motion_time,
252 bool use_particle_hair,
253 TaskPool *task_pool)
254{
255 /* Ensure we only sync instanced geometry once. */
256 Geometry *geom = object->get_geometry();
257
258 if (geometry_motion_synced.contains(geom) || geometry_motion_attribute_synced.contains(geom)) {
259 return;
260 }
261
262 geometry_motion_synced.insert(geom);
263
264 /* Ensure we only motion sync geometry that also had geometry synced, to avoid
265 * unnecessary work and to ensure that its attributes were clear. */
266 if (!geometry_synced.contains(geom)) {
267 return;
268 }
269
270 /* Nothing to do for lights. */
271 if (geom->is_light()) {
272 return;
273 }
274
275 /* If the geometry already has motion blur from a velocity attribute, don't
276 * set the geometry motion steps again.
277 *
278 * Otherwise, setting geometry motion steps is done here to avoid concurrency issues.
279 * - It can't be done earlier in sync_object_motion_init because sync_geometry
280 * runs in parallel, and has_motion_blur would check attributes while
281 * sync_geometry is potentially creating the attribute from velocity.
282 * - It needs to happen before the parallel motion sync that happens right after
283 * this, because that can create the attribute from neighboring frames.
284 * Copying the motion steps from the object here solves this. */
285 if (!geom->has_motion_blur()) {
286 geom->set_motion_steps(object->get_motion().size());
287 }
288
289 /* Find time matching motion step required by geometry. */
290 const int motion_step = geom->motion_step(motion_time);
291 if (motion_step < 0) {
292 return;
293 }
294
295 auto sync_func = [this, b_ob_info, use_particle_hair, motion_step, geom]() mutable {
296 if (progress.get_cancel()) {
297 return;
298 }
299
300 if (b_ob_info.object_data->id_type() == blender::ID_CV || use_particle_hair) {
301 Hair *hair = static_cast<Hair *>(geom);
302 sync_hair_motion(b_ob_info, hair, motion_step);
303 }
304 else if (b_ob_info.object_data->id_type() == blender::ID_VO ||
305 object_fluid_gas_domain_find(*b_ob_info.real_object))
306 {
307 /* No volume motion blur support yet. */
308 }
309 else if (b_ob_info.object_data->id_type() == blender::ID_PT) {
310 PointCloud *pointcloud = static_cast<PointCloud *>(geom);
311 sync_pointcloud_motion(pointcloud, b_ob_info, motion_step);
312 }
313 else {
314 Mesh *mesh = static_cast<Mesh *>(geom);
315 sync_mesh_motion(b_ob_info, mesh, motion_step);
316 }
317 };
318
319 /* Defer the actual geometry sync to the task_pool for multithreading */
320 if (task_pool) {
321 task_pool->push(sync_func);
322 }
323 else {
324 sync_func();
325 }
326}
327
General operations, lookup, etc. for materials.
static Shader * get_default_shader(const Scene *scene, const blender::Object &b_ob)
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
Definition scene.h:133
Shader * default_volume
Definition scene.h:179
Shader * default_surface
Definition scene.h:178
Shader * default_gsplat
Definition scene.h:183
bool need_update_geometry() const
void push(TaskRunFunction &&task)
Definition task.cpp:21
size_t size() const
void push_back_slow(const T &t)
#define CCL_NAMESPACE_END
#define assert(assertion)
int BKE_object_material_count_eval(const Object *ob)
Material * BKE_object_material_get(Object *ob, short act)
const char * BKE_id_name(const ID &id)
blender::Object * iter_object
bool is_real_object_data() const
blender::ID * object_data
blender::Object * real_object
ustring name
Definition graph/node.h:178