Blender V5.3
eevee_velocity.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
13
14#include "BKE_duplilist.hh"
15#include "BKE_object.hh"
16#include "BLI_map.hh"
18#include "DNA_modifier_types.h"
19#include "DNA_particle_types.h"
20#include "DNA_rigidbody_types.h"
21
22#include "DRW_engine.hh"
23#include "GPU_capabilities.hh"
24#include "draw_cache.hh"
25#include "draw_cache_impl.hh"
26
27#include "eevee_instance.hh"
28// #include "eevee_renderpasses.hh"
29#include "eevee_shader.hh"
30#include "eevee_velocity.hh"
32
33#include "draw_common.hh"
34
35namespace blender::eevee {
36
37/* -------------------------------------------------------------------- */
41
43{
44 if (!inst_.is_viewport() && !inst_.is_baking() &&
45 (inst_.film.enabled_passes_get() & EEVEE_RENDER_PASS_VECTOR) &&
46 !inst_.motion_blur.postfx_enabled())
47 {
48 /* No motion blur and the vector pass was requested. Do the steps sync here. */
49 const Scene *scene = inst_.scene;
50 float initial_time = scene->r.cfra + scene->r.subframe;
51 step_sync(STEP_PREVIOUS, initial_time - 1.0f);
52 step_sync(STEP_NEXT, initial_time + 1.0f);
53 /* Let the main sync loop handle the current step. */
54 inst_.set_time(initial_time);
55 step_ = STEP_CURRENT;
56 }
57
58 /* For viewport, only previous motion is supported.
59 * Still bind previous step to avoid undefined behavior. */
60 next_step_ = (inst_.is_viewport() || inst_.is_baking()) ? STEP_PREVIOUS : STEP_NEXT;
61}
62
63/* Similar to Instance::object_sync, but only syncs velocity. */
64static void step_object_sync_render(Instance &inst, ObjectRef &ob_ref)
65{
66 Object *ob = ob_ref.object;
67
68 const bool is_velocity_type = ELEM(ob->type, OB_CURVES, OB_MESH, OB_POINTCLOUD);
69 const int ob_visibility = DRW_object_visibility_in_active_context(ob);
70 const bool partsys_is_visible = (ob_visibility & OB_VISIBLE_PARTICLES) != 0 &&
71 (ob->type == OB_MESH);
72 const bool object_is_visible = DRW_object_is_renderable(ob) &&
73 (ob_visibility & OB_VISIBLE_SELF) != 0;
74
75 if (!is_velocity_type || (!partsys_is_visible && !object_is_visible)) {
76 return;
77 }
78
79 /* NOTE: Dummy resource handle since this won't be used for drawing. */
80 ResourceHandleRange resource_handle = {};
81
82 if (partsys_is_visible) {
83 auto sync_hair = [&](const HairParticleInfo &info) {
84 ObjectHandle ob_handle(ob_ref, resource_handle, info.recalc_flags, info.sub_key);
85 inst.velocity.step_object_sync(ob_handle, &info);
86 };
87 foreach_hair_particle(inst, ob_ref, sync_hair);
88 };
89
90 if (object_is_visible) {
91 ObjectHandle ob_handle = inst.sync.sync_object(ob_ref, resource_handle);
92 inst.velocity.step_object_sync(ob_handle);
93 }
94}
95
97{
98 inst_.set_time(time);
99 step_ = step;
100 object_steps_usage[step_] = 0;
102
104 inst_.render, inst_.depsgraph, [&](draw::ObjectRef &ob_ref, RenderEngine *, Depsgraph *) {
105 step_object_sync_render(inst_, ob_ref);
106 });
107
109}
110
112{
113 inst_.camera.sync();
114 *camera_steps[step_] = inst_.camera.data_get();
115 step_time[step_] = inst_.scene->r.cfra + inst_.scene->r.subframe;
116 /* Fix undefined camera steps when rendering is starting. */
117 if ((step_ == STEP_CURRENT) && (camera_steps[STEP_PREVIOUS]->initialized == false)) {
118 *camera_steps[STEP_PREVIOUS] = *static_cast<CameraData *>(camera_steps[step_]);
119 camera_steps[STEP_PREVIOUS]->initialized = true;
121 }
122}
123
125 HairParticleInfo const *hair_particle /*=nullptr*/)
126{
127 bool has_motion = object_has_velocity(ob_handle.object) ||
128 (ob_handle.recalc & ID_RECALC_TRANSFORM);
129 /* NOTE: Fragile. This will only work with 1 frame of lag since we can't record every geometry
130 * just in case there might be an update the next frame. */
131 bool has_deform = object_is_deform(ob_handle.object) || (ob_handle.recalc & ID_RECALC_GEOMETRY);
132
133 if (!has_motion && !has_deform) {
134 return false;
135 }
136
137 /* While VelocityObjectData is unique for each object/instance, multiple VelocityObjectDatas
138 * can point to the same offset in VelocityGeometryData, since geometry is stored local space. */
139 uint64_t velocity_id = hair_particle ? uint64_t(&hair_particle->psys) :
140 uint64_t(ob_handle.object->data);
141
142 /* Geometry motion. */
143 if (has_deform) {
144 auto add_cb = [&]() {
146 if (hair_particle) {
148 inst_.scene, ob_handle.object, &hair_particle->psys, &hair_particle->md);
149 return data;
150 }
151 switch (ob_handle.object->type) {
152 case OB_CURVES:
153 data.pos_buf = draw::curves_pos_buffer_get(ob_handle.object);
154 break;
155 case OB_POINTCLOUD:
157 break;
158 case OB_MESH:
159 data.pos_buf = DRW_cache_mesh_surface_get(ob_handle.object);
160 break;
161 default:
162 break;
163 }
164 return data;
165 };
166
167 const VelocityGeometryData &data = geometry_map.lookup_or_add_cb(velocity_id, add_cb);
168 has_deform = data.has_data();
169 }
170
171 bool any_instance_has_motion = false;
172 for (int i : IndexRange(ob_handle.instances_count())) {
173 /* Object motion. */
174 /* FIXME(fclem) As we are using original objects pointers, there is a chance the previous
175 * object key matches a totally different object if the scene was changed by user or python
176 * callback. In this case, we cannot correctly match objects between updates.
177 * What this means is that there will be incorrect motion vectors for these objects.
178 * We live with that until we have a correct way of identifying new objects. */
179 VelocityObjectData &vel = velocity_map.lookup_or_add_default(ObjectKey(ob_handle, i));
180 vel.obj.ofs[step_] = object_steps_usage[step_]++;
181 if (ob_handle.res_handle.is_valid()) {
182 vel.obj.resource_index = ob_handle.res_handle.sub_handle(i).index();
183 }
184 else {
185 /* Called from `step_object_sync_render`. */
186 vel.obj.resource_index = 0;
187 }
188 vel.id = velocity_id;
189 object_steps[step_]->get_or_resize(vel.obj.ofs[step_]) = ob_handle.object_to_world(i);
190 if (step_ == STEP_CURRENT) {
191 /* Replace invalid steps. Can happen if object was hidden in one of those steps. */
192 if (vel.obj.ofs[STEP_PREVIOUS] == -1) {
194 object_steps[STEP_PREVIOUS]->get_or_resize(
195 vel.obj.ofs[STEP_PREVIOUS]) = ob_handle.object_to_world(i);
196 }
197 if (vel.obj.ofs[STEP_NEXT] == -1) {
198 if (inst_.is_viewport()) {
199 /* Just set it to 0. motion.next is not meant to be valid in the viewport. */
200 vel.obj.ofs[STEP_NEXT] = 0;
201 }
202 else {
204 object_steps[STEP_NEXT]->get_or_resize(
205 vel.obj.ofs[STEP_NEXT]) = ob_handle.object_to_world(i);
206 }
207 }
208 }
209
210 /* Avoid drawing object that has no motions but were tagged as such. */
211 if (step_ == STEP_CURRENT && has_motion == true && has_deform == false) {
212 const float4x4 &obmat_curr = (*object_steps[STEP_CURRENT])[vel.obj.ofs[STEP_CURRENT]];
213 const float4x4 &obmat_prev = (*object_steps[STEP_PREVIOUS])[vel.obj.ofs[STEP_PREVIOUS]];
214 if (inst_.is_viewport()) {
215 any_instance_has_motion |= obmat_curr != obmat_prev;
216 }
217 else {
218 const float4x4 &obmat_next = (*object_steps[STEP_NEXT])[vel.obj.ofs[STEP_NEXT]];
219 any_instance_has_motion |= (obmat_curr != obmat_prev) || (obmat_curr != obmat_next);
220 }
221 }
222
223#if 0
224 if (!has_motion && !has_deform) {
225 printf("Detected no motion on %s (%d)\n", ob_handle.object->id.name, i);
226 }
227 if (has_deform) {
228 printf("Geometry Motion on %s (%d)\n", ob_handle.object->id.name, i);
229 }
230 if (has_motion) {
231 printf("Object Motion on %s (%d)\n", ob_handle.object->id.name, i);
232 }
233#endif
234 }
235
236 return has_deform || any_instance_has_motion;
237}
238
240{
241 uint dst_ofs = 0;
242 for (VelocityGeometryData &geom : geometry_map.values()) {
243 gpu::VertBuf *pos_buf = geom.pos_buf_get();
244 if (!pos_buf) {
245 continue;
246 }
247 uint src_len = GPU_vertbuf_get_vertex_len(pos_buf);
248 geom.len = src_len;
249 geom.ofs = dst_ofs;
250 dst_ofs += src_len;
251 }
252
253 /* Fallback when velocity buffer could not be allocated. */
254 if (GPU_max_storage_buffer_size() < dst_ofs * sizeof(float) * 4) {
255 inst_.info_append_i18n(
256 "Error: Could not allocate velocity geometry buffer. Only object and camera velocities "
257 "will be used.");
258 for (VelocityObjectData &vel : velocity_map.values()) {
259 vel.geo.len[step_] = -1;
260 vel.geo.ofs[step_] = -1;
261 vel.id = 0;
262 }
263 return;
264 }
265 geometry_steps[step_]->resize(max_ii(16, dst_ofs));
266
268
269 PassSimple copy_ps("Velocity Copy Pass");
270 copy_ps.init();
272 copy_ps.shader_set(inst_.shaders.static_shader_get(VERTEX_COPY));
273 copy_ps.bind_ssbo("out_buf", *geometry_steps[step_]);
274
275 for (VelocityGeometryData &geom : geometry_map.values()) {
276 gpu::VertBuf *pos_buf = geom.pos_buf_get();
277 if (!pos_buf || geom.len == 0) {
278 continue;
279 }
280 const GPUVertFormat *format = GPU_vertbuf_get_format(pos_buf);
281 if (format->stride == 16) {
283 pos_buf,
284 geom.ofs * sizeof(float4),
285 0,
286 geom.len * sizeof(float4));
287 }
288 else {
289 BLI_assert(format->stride % 4 == 0);
290 copy_ps.bind_ssbo("in_buf", pos_buf);
291 copy_ps.push_constant("start_offset", geom.ofs);
292 copy_ps.push_constant("vertex_stride", int(format->stride / 4));
293 copy_ps.push_constant("vertex_count", geom.len);
294 uint group_len_x = divide_ceil_u(geom.len, VERTEX_COPY_GROUP_SIZE);
295 uint verts_per_thread = divide_ceil_u(group_len_x, GPU_max_work_group_count(0));
296 copy_ps.dispatch(int3(group_len_x / verts_per_thread, 1, 1));
297 }
298 }
299
301 inst_.manager->submit(copy_ps);
302
304
305 /* Copy back the #VelocityGeometryIndex into #VelocityObjectData which are
306 * indexed using persistent keys (unlike geometries which are indexed by volatile ID). */
307 for (VelocityObjectData &vel : velocity_map.values()) {
308 const VelocityGeometryData &geom = geometry_map.lookup_default(vel.id, VelocityGeometryData());
309 vel.geo.len[step_] = geom.len;
310 vel.geo.ofs[step_] = geom.ofs;
311 /* Avoid reuse. */
312 vel.id = 0;
313 }
314
315 geometry_map.clear();
316}
317
319{
320 auto swap_steps = [&](eVelocityStep step_a, eVelocityStep step_b) {
321 std::swap(object_steps[step_a], object_steps[step_b]);
322 std::swap(geometry_steps[step_a], geometry_steps[step_b]);
323 std::swap(camera_steps[step_a], camera_steps[step_b]);
324 std::swap(step_time[step_a], step_time[step_b]);
325 std::swap(object_steps_usage[step_a], object_steps_usage[step_b]);
326
327 for (VelocityObjectData &vel : velocity_map.values()) {
328 vel.obj.ofs[step_a] = vel.obj.ofs[step_b];
329 vel.obj.ofs[step_b] = uint(-1);
330 vel.geo.ofs[step_a] = vel.geo.ofs[step_b];
331 vel.geo.len[step_a] = vel.geo.len[step_b];
332 vel.geo.ofs[step_b] = uint(-1);
333 vel.geo.len[step_b] = uint(-1);
334 }
335 };
336
337 if (inst_.is_viewport()) {
339 /* For viewport we only use the last rendered redraw as previous frame.
340 * We swap current with previous step at the end of a redraw.
341 * We do not support motion blur as it is rendered to avoid conflicting motions
342 * for temporal reprojection. */
344 }
345 else {
346 /* Render case: The STEP_CURRENT is left untouched. */
348 }
349}
350
352{
353 step_ = STEP_CURRENT;
355 object_steps_usage[step_] = 0;
356
357 /* STEP_NEXT is not used for viewport. (See #131134) */
358 BLI_assert(!inst_.is_viewport() || object_steps_usage[STEP_NEXT] == 0);
359}
360
362{
363 Vector<ObjectKey, 0> deleted_obj;
364
365 uint32_t max_resource_index_ = 0u;
366
368 if (item.value.obj.resource_index == uint32_t(-1)) {
369 deleted_obj.append(item.key);
370 }
371 else {
372 max_resource_index_ = max_uu(max_resource_index_, item.value.obj.resource_index);
373 }
374 }
375
376 for (auto &key : deleted_obj) {
377 velocity_map.remove(key);
378 }
379
380 indirection_buf.resize(ceil_to_multiple_u(max_resource_index_ + 1, 128));
381
382 /* Avoid uploading more data to the GPU as well as an extra level of
383 * indirection on the GPU by copying back offsets the to VelocityIndex. */
384 for (VelocityObjectData &vel : velocity_map.values()) {
385 /* Disable deform if vertex count mismatch. */
386 if (inst_.is_viewport()) {
387 /* Current geometry step will be copied at the end of the frame.
388 * Thus vel.geo.len[STEP_CURRENT] is not yet valid and the current length is manually
389 * retrieved. */
390 gpu::VertBuf *pos_buf =
391 geometry_map.lookup_default(vel.id, VelocityGeometryData()).pos_buf_get();
392 vel.geo.do_deform = pos_buf != nullptr &&
393 (vel.geo.len[STEP_PREVIOUS] == GPU_vertbuf_get_vertex_len(pos_buf));
394 }
395 else {
396 vel.geo.do_deform = (vel.geo.len[STEP_CURRENT] != 0) &&
397 (vel.geo.len[STEP_CURRENT] == vel.geo.len[STEP_PREVIOUS]) &&
398 (vel.geo.len[STEP_CURRENT] == vel.geo.len[STEP_NEXT]);
399 }
400 indirection_buf[vel.obj.resource_index] = vel;
401 /* Reset for next sync. */
402 vel.obj.resource_index = uint(-1);
403 }
404
405 object_steps[STEP_PREVIOUS]->push_update();
406 object_steps[STEP_NEXT]->push_update();
407 camera_steps[STEP_PREVIOUS]->push_update();
408 camera_steps[STEP_CURRENT]->push_update();
409 camera_steps[STEP_NEXT]->push_update();
411}
412
413bool VelocityModule::object_has_velocity(const Object *ob)
414{
415#if 0
416 RigidBodyOb *rbo = ob->rigidbody_object;
417 /* Active rigidbody objects only, as only those are affected by sim. */
418 const bool has_rigidbody = (rbo && (rbo->type == RBO_TYPE_ACTIVE));
419 /* For now we assume dupli objects are moving. */
420 const bool is_dupli = (ob->base_flag & BASE_FROM_DUPLI) != 0;
421 const bool object_moves = is_dupli || has_rigidbody || BKE_object_moves_in_time(ob, true);
422#else
423 UNUSED_VARS(ob);
424 /* BKE_object_moves_in_time does not work in some cases.
425 * Better detect non moving object after evaluation. */
426 const bool object_moves = true;
427#endif
428 return object_moves;
429}
430
431bool VelocityModule::object_is_deform(const Object *ob)
432{
433 RigidBodyOb *rbo = ob->rigidbody_object;
434 /* Active rigidbody objects only, as only those are affected by sim. */
435 const bool has_rigidbody = (rbo && (rbo->type == RBO_TYPE_ACTIVE));
436 const bool is_deform = BKE_object_is_deform_modified(inst_.scene, const_cast<Object *>(ob)) ||
437 (has_rigidbody && (rbo->flag & RBO_FLAG_USE_DEFORM) != 0);
438
439 return is_deform;
440}
441
443{
444 /* Only valid after sync. */
445 if (inst_.is_viewport()) {
446 /* Viewport has no next step. */
448 }
451}
452
454{
455 /* Only valid after sync. */
456 if (inst_.is_viewport()) {
457 return camera_steps[STEP_PREVIOUS]->type != camera_steps[STEP_CURRENT]->type;
458 }
459 /* Cannot happen in render mode since we set the type during the init phase. */
460 return false;
461}
462
464{
465 return step_time[end] - step_time[start];
466}
467
469
470} // namespace blender::eevee
General operations, lookup, etc. for blender objects.
Types and defines for representing Rigid Body entities.
#define UNUSED_VARS(...)
#define ELEM(...)
constexpr IndexRange()=default
void append(const T &value)
float4x4 object_to_world(int instance_index) const
ResourceHandle sub_handle(int index) const
void shader_set(gpu::Shader *shader)
void dispatch(int group_len)
void state_set(DRWState state, int clip_plane_count=0)
void barrier(GPUBarrier type)
void push_constant(const char *name, const float &data)
void bind_ssbo(const char *name, gpu::StorageBuf *buffer)
A running instance of the engine.
ObjectHandle sync_object(const ObjectRef &ob_ref, const ResourceHandleRange &res_handle, uint sub_key=0)
Definition eevee_sync.cc:32
float step_time_delta_get(eVelocityStep start, eVelocityStep end) const
Map< ObjectKey, VelocityObjectData > velocity_map
std::array< CameraDataBuf *, 3 > camera_steps
bool step_object_sync(const ObjectHandle &ob_handle, HairParticleInfo const *hair_particle=nullptr)
std::array< VelocityObjectBuf *, 3 > object_steps
std::array< VelocityGeometryBuf *, 3 > geometry_steps
void step_sync(eVelocityStep step, float time)
Map< uint64_t, VelocityGeometryData > geometry_map
#define VERTEX_COPY_GROUP_SIZE
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
detail::Pass< command::DrawCommandBuf > PassSimple
gpu::VertBuf * curves_pos_buffer_get(Object *object)
gpu::VertBuf * hair_pos_buffer_get(Scene *scene, Object *object, ParticleSystem *psys, ModifierData *md)
Definition draw_hair.cc:36
gpu::Batch * DRW_cache_mesh_surface_get(Object *ob)
gpu::VertBuf * DRW_pointcloud_position_and_radius_buffer_get(Object *ob)
static void step_object_sync_render(Instance &inst, ObjectRef &ob_ref)
void foreach_hair_particle(Instance &inst, ObjectRef &ob_ref, HairHandleCallback callback)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
unsigned int uint
Definition BKE_text.h:55
bool BKE_object_moves_in_time(const Object *object, bool recurse_parent)
Definition object.cc:4968
bool DRW_object_is_renderable(const Object *ob)
MINLINE uint max_uu(uint a, uint b)
MINLINE int max_ii(int a, int b)
const GPUVertFormat * GPU_vertbuf_get_format(const gpu::VertBuf *verts)
MatBase< float, 4, 4 > float4x4
int DRW_object_visibility_in_active_context(const Object *ob)
void DRW_render_object_iter(RenderEngine *engine, Depsgraph *depsgraph, std::function< void(draw::ObjectRef &, RenderEngine *, Depsgraph *)> callback)
VecBase< float, 4 > float4
int GPU_max_work_group_count(int index)
unsigned int uint32_t
Definition BKE_text.h:81
VecBase< int32_t, 3 > int3
BMesh const char void * data
size_t GPU_max_storage_buffer_size()
int BKE_object_is_deform_modified(Scene *scene, Object *ob)
Definition object.cc:5063
@ GPU_BARRIER_SHADER_STORAGE
Definition GPU_state.hh:51
@ DRW_STATE_NO_DRAW
Definition draw_state.hh:29
void DRW_submission_end()
void GPU_storagebuf_copy_sub_from_vertbuf(gpu::StorageBuf *ssbo, gpu::VertBuf *src, uint dst_offset, uint src_offset, uint copy_size)
Copy a part of a vertex buffer to a storage buffer.
@ EEVEE_RENDER_PASS_VECTOR
@ ID_RECALC_TRANSFORM
Definition DNA_ID.h:1054
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
MINLINE uint divide_ceil_u(uint a, uint b)
void DRW_submission_start()
uint GPU_vertbuf_get_vertex_len(const gpu::VertBuf *verts)
MINLINE uint ceil_to_multiple_u(uint a, uint b)
@ OB_VISIBLE_PARTICLES
@ OB_VISIBLE_SELF
char name[258]
Definition DNA_ID.h:433
struct RigidBodyOb * rigidbody_object
struct RenderData r
const ResourceHandleRange res_handle
Definition eevee_sync.hh:39
struct VelocityObjectIndex obj