Welcome to mirror list, hosted at ThFree Co, Russian Federation.

eevee_velocity.cc « eevee « engines « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6d37aea3713a18425753f871776670d905df8ca0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2021 Blender Foundation.
 */

/** \file
 * \ingroup eevee
 *
 * The velocity pass outputs motion vectors to use for either
 * temporal re-projection or motion blur.
 *
 * It is the module that tracks the objects between frames updates.
 */

#include "BKE_duplilist.h"
#include "BKE_object.h"
#include "BLI_map.hh"
#include "DEG_depsgraph_query.h"
#include "DNA_rigidbody_types.h"

#include "eevee_instance.hh"
#include "eevee_renderpasses.hh"
#include "eevee_shader.hh"
#include "eevee_shader_shared.hh"
#include "eevee_velocity.hh"
#include "eevee_wrapper.hh"

namespace blender::eevee {

/* -------------------------------------------------------------------- */
/** \name VelocityModule
 *
 * \{ */

void VelocityModule::init(void)
{
  if (inst_.is_viewport()) {
    /* For viewport we sync when object is evaluated and we swap at init time.
     * Use next step to store the current position. This one will become the previous step after
     * next swapping. */
    step_ = STEP_NEXT;
    step_swap();
  }

  if (inst_.render && (inst_.render_passes.vector != nullptr)) {
    /* No motion blur and the vector pass was requested. Do the step sync here. */
    const Scene *scene = inst_.scene;
    float initial_time = scene->r.cfra + scene->r.subframe;
    step_sync(STEP_PREVIOUS, initial_time - 1.0f);
    step_sync(STEP_NEXT, initial_time + 1.0f);
    inst_.set_time(initial_time);
  }
}

static void step_object_sync_render(void *velocity,
                                    Object *ob,
                                    RenderEngine *UNUSED(engine),
                                    Depsgraph *UNUSED(depsgraph))
{
  ObjectKey object_key(ob);
  reinterpret_cast<VelocityModule *>(velocity)->step_object_sync(ob, object_key);
}

void VelocityModule::step_sync(eStep step, float time)
{
  inst_.set_time(time);
  step_ = step;
  step_camera_sync();
  DRW_render_object_iter(this, inst_.render, inst_.depsgraph, step_object_sync_render);
}

void VelocityModule::step_camera_sync()
{
  if (!inst_.is_viewport()) {
    inst_.camera.sync();
  }

  if (step_ == STEP_NEXT) {
    camera_step.next = inst_.camera.data_get();
  }
  else if (step_ == STEP_PREVIOUS) {
    camera_step.prev = inst_.camera.data_get();
  }
}

/* Gather motion data from all objects in the scene. */
void VelocityModule::step_object_sync(Object *ob, ObjectKey &object_key)
{
  if (!object_has_velocity(ob) && !object_is_deform(ob)) {
    return;
  }

  auto add_cb = [&]() {
    inst_.sampling.reset();
    return new VelocityObjectBuf();
  };
  auto data = objects_steps.lookup_or_add_cb(object_key, add_cb);

  if (step_ == STEP_NEXT) {
    data->next_object_mat = ob->obmat;
  }
  else if (step_ == STEP_PREVIOUS) {
    data->prev_object_mat = ob->obmat;
  }
}

/* Moves next frame data to previous frame data. Nullify next frame data. */
void VelocityModule::step_swap(void)
{
  for (VelocityObjectBuf *data : objects_steps.values()) {
    data->prev_object_mat = data->next_object_mat;
    /* Important: This let us known if object is missing from the next time step. */
    zero_m4(data->next_object_mat.ptr());
  }
  camera_step.prev = static_cast<CameraData>(camera_step.next);
}

void VelocityModule::begin_sync(void)
{
  if (inst_.is_viewport()) {
    step_camera_sync();
  }
}

/* This is the end of the current frame sync. Not the step_sync. */
void VelocityModule::end_sync(void)
{
  Vector<ObjectKey, 1> deleted_keys;

  for (auto item : objects_steps.items()) {
    /* Detect object deletion. Only do this on viewport as STEP_NEXT means current step. */
    if (inst_.is_viewport() && is_zero_m4(item.value->next_object_mat.ptr())) {
      deleted_keys.append(item.key);
      delete item.value;
    }
    else {
      item.value->push_update();
    }
  }

  if (deleted_keys.size() > 0) {
    inst_.sampling.reset();
  }

  for (auto key : deleted_keys) {
    objects_steps.remove(key);
  }

  camera_step.prev.push_update();
  camera_step.next.push_update();
}

bool VelocityModule::object_has_velocity(const Object *ob)
{
#if 0
    RigidBodyOb *rbo = ob->rigidbody_object;
    /* Active rigidbody objects only, as only those are affected by sim. */
    const bool has_rigidbody = (rbo && (rbo->type == RBO_TYPE_ACTIVE));
    /* For now we assume dupli objects are moving. */
    const bool is_dupli = (ob->base_flag & BASE_FROM_DUPLI) != 0;
    const bool object_moves = is_dupli || has_rigidbody || BKE_object_moves_in_time(ob, true);
#else
  UNUSED_VARS(ob);
  /* BKE_object_moves_in_time does not work in some cases.
   * Better detect non moving object after evaluation. */
  const bool object_moves = true;
#endif
  return object_moves;
}

bool VelocityModule::object_is_deform(const Object *ob)
{
  RigidBodyOb *rbo = ob->rigidbody_object;
  /* Active rigidbody objects only, as only those are affected by sim. */
  const bool has_rigidbody = (rbo && (rbo->type == RBO_TYPE_ACTIVE));
  const bool is_deform = BKE_object_is_deform_modified(inst_.scene, (Object *)ob) ||
                         (has_rigidbody && (rbo->flag & RBO_FLAG_USE_DEFORM) != 0);

  return is_deform;
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name VelocityPass
 *
 * Draws velocity data from VelocityModule module to a framebuffer / texture.
 * \{ */

void VelocityPass::sync(void)
{
  VelocityModule &velocity = inst_.velocity;
  {
    /* Outputs camera motion vector. */
    /* TODO(fclem) Ideally, we should run this only where the motion vectors were not written.
     * But without imageLoadStore, we cannot do that without another buffer. */
    DRWState state = DRW_STATE_WRITE_COLOR;
    DRW_PASS_CREATE(camera_ps_, state);
    GPUShader *sh = inst_.shaders.static_shader_get(VELOCITY_CAMERA);
    DRWShadingGroup *grp = DRW_shgroup_create(sh, camera_ps_);
    DRW_shgroup_uniform_texture_ref(grp, "depth_tx", &input_depth_tx_);
    DRW_shgroup_uniform_block(grp, "cam_prev", velocity.camera_step.prev);
    DRW_shgroup_uniform_block(grp, "cam_next", velocity.camera_step.next);
    DRW_shgroup_uniform_block(grp, "cam_curr", inst_.camera.ubo_get());
    DRW_shgroup_call_procedural_triangles(grp, nullptr, 1);
  }
  {
    /* Animated objects are rendered and output the correct motion vector. */
    DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL;
    DRW_PASS_CREATE(object_ps_, state);
    {
      GPUShader *sh = inst_.shaders.static_shader_get(VELOCITY_MESH);
      DRWShadingGroup *grp = mesh_grp_ = DRW_shgroup_create(sh, object_ps_);
      DRW_shgroup_uniform_block(grp, "cam_prev", velocity.camera_step.prev);
      DRW_shgroup_uniform_block(grp, "cam_next", velocity.camera_step.next);
      DRW_shgroup_uniform_block(grp, "cam_curr", inst_.camera.ubo_get());
    }
  }
}

void VelocityPass::mesh_add(Object *ob, ObjectHandle &handle)
{
  if (inst_.is_viewport()) {
    /* FIXME(fclem) As we are using original objects pointers, there is a chance the previous
     * object key matches a totally different object if the scene was changed by user or python
     * callback. In this case, we cannot correctly match objects between updates.
     * What this means is that there will be incorrect motion vectors for these objects.
     * We live with that until we have a correct way of identifying new objects. */
    if (handle.recalc & ID_RECALC_TRANSFORM) {
      inst_.velocity.step_object_sync(ob, handle.object_key);
    }
  }

  VelocityObjectBuf **data_ptr = inst_.velocity.objects_steps.lookup_ptr(handle.object_key);

  if (data_ptr == nullptr) {
    return;
  }

  VelocityObjectBuf *data = *data_ptr;

  GPUBatch *geom = DRW_cache_object_surface_get(ob);
  if (geom == nullptr) {
    return;
  }

  if (!inst_.is_viewport()) {
    /* Fill missing matrices if the object was hidden in previous or next frame. */
    if (is_zero_m4(data->prev_object_mat.ptr())) {
      copy_m4_m4(data->prev_object_mat.ptr(), ob->obmat);
    }

    /* Avoid drawing object that has no motions since object_moves is always true. */
    if (/* !mb_geom->use_deform && */ /* Object deformation can happen without transform.  */
        equals_m4m4(data->prev_object_mat.ptr(), ob->obmat)) {
      return;
    }
  }
  else {
    /* Fill missing matrices if the object was hidden in previous or next frame. */
    if (is_zero_m4(data->prev_object_mat.ptr())) {
      data->prev_object_mat = ob->obmat;
    }
    if (is_zero_m4(data->next_object_mat.ptr())) {
      data->next_object_mat = ob->obmat;
    }

    // if (mb_geom->use_deform) {
    //   /* Keep to modify later (after init). */
    //   mb_geom->batch = geom;
    // }

    /* Avoid drawing object that has no motions since object_moves is always true. */
    if (/* !mb_geom->use_deform && */ /* Object deformation can happen without transform.  */
        equals_m4m4(data->prev_object_mat.ptr(), ob->obmat) &&
        equals_m4m4(data->next_object_mat.ptr(), ob->obmat)) {
      return;
    }
  }

  /* TODO(fclem) Use the same layout as modelBlock from draw so we can reuse the same offset
   * and avoid the overhead of 1 shading group and one UBO per object. */
  DRWShadingGroup *grp = DRW_shgroup_create_sub(mesh_grp_);
  DRW_shgroup_uniform_block(grp, "velocity", *data);
  DRW_shgroup_call(grp, geom, ob);
}

void VelocityPass::render_objects(void)
{
  DRW_draw_pass(object_ps_);
}

void VelocityPass::resolve_camera_motion(GPUTexture *depth_tx)
{
  input_depth_tx_ = depth_tx;
  DRW_draw_pass(camera_ps_);
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name VelocityPass
 *
 * Draws velocity data from VelocityModule module to a framebuffer / texture.
 * \{ */

void Velocity::sync(int extent[2])
{
  /* HACK: View name should be unique and static.
   * With this, we can reuse the same texture across views. */
  DrawEngineType *owner = (DrawEngineType *)view_name_.c_str();

  /* TODO(fclem) Only allocate if needed. RG16F when only doing reprojection. */
  velocity_camera_tx_ = DRW_texture_pool_query_2d(UNPACK2(extent), GPU_RGBA16F, owner);
  /* TODO(fclem) Only allocate if needed. RG16F when only doing motion blur post fx in
   * panoramic camera. */
  velocity_view_tx_ = DRW_texture_pool_query_2d(UNPACK2(extent), GPU_RGBA16F, owner);

  velocity_only_fb_.ensure(GPU_ATTACHMENT_NONE,
                           GPU_ATTACHMENT_TEXTURE(velocity_camera_tx_),
                           GPU_ATTACHMENT_TEXTURE(velocity_view_tx_));
}

void Velocity::render(GPUTexture *depth_tx)
{
  DRW_stats_group_start("Velocity");

  GPU_framebuffer_bind(velocity_only_fb_);
  inst_.shading_passes.velocity.resolve_camera_motion(depth_tx);

  velocity_fb_.ensure(GPU_ATTACHMENT_TEXTURE(depth_tx),
                      GPU_ATTACHMENT_TEXTURE(velocity_camera_tx_),
                      GPU_ATTACHMENT_TEXTURE(velocity_view_tx_));

  GPU_framebuffer_bind(velocity_fb_);
  inst_.shading_passes.velocity.render_objects();

  DRW_stats_group_end();
}

/** \} */

}  // namespace blender::eevee