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

eevee_motion_blur.cc « eevee_next « engines « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d9545e2e972ec8f1313f64f30fbb6444e80b9744 (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
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2021 Blender Foundation.
 */

/** \file
 * \ingroup eevee
 */

// #include "BLI_map.hh"
#include "DEG_depsgraph_query.h"

#include "eevee_instance.hh"
#include "eevee_motion_blur.hh"
// #include "eevee_sampling.hh"
// #include "eevee_shader_shared.hh"
// #include "eevee_velocity.hh"

namespace blender::eevee {

/* -------------------------------------------------------------------- */
/** \name MotionBlurModule
 *
 * \{ */

void MotionBlurModule::init()
{
  const Scene *scene = inst_.scene;

  enabled_ = (scene->eevee.flag & SCE_EEVEE_MOTION_BLUR_ENABLED) != 0;

  if (!enabled_) {
    motion_blur_fx_enabled_ = false;
    return;
  }

  /* Take into account the steps needed for fx motion blur. */
  int steps_count = max_ii(1, scene->eevee.motion_blur_steps) * 2 + 1;

  time_steps_.resize(steps_count);

  initial_frame_ = scene->r.cfra;
  initial_subframe_ = scene->r.subframe;
  frame_time_ = initial_frame_ + initial_subframe_;
  shutter_position_ = scene->eevee.motion_blur_position;
  shutter_time_ = scene->eevee.motion_blur_shutter;

  data_.depth_scale = scene->eevee.motion_blur_depth_scale;
  motion_blur_fx_enabled_ = true; /* TODO(fclem): UI option. */

  /* Viewport stops here. We only do Post-FX motion blur. */
  if (inst_.is_viewport()) {
    enabled_ = false;
    return;
  }

  /* Without this there is the possibility of the curve table not being allocated. */
  BKE_curvemapping_changed((struct CurveMapping *)&scene->r.mblur_shutter_curve, false);

  Vector<float> cdf(CM_TABLE);
  Sampling::cdf_from_curvemapping(scene->r.mblur_shutter_curve, cdf);
  Sampling::cdf_invert(cdf, time_steps_);

  for (float &time : time_steps_) {
    time = this->shutter_time_to_scene_time(time);
  }

  step_id_ = 1;

  if (motion_blur_fx_enabled_) {
    /* A bit weird but we have to sync the first 2 steps here because the step()
     * function is only called after rendering a sample. */
    inst_.velocity.step_sync(STEP_PREVIOUS, time_steps_[0]);
    inst_.velocity.step_sync(STEP_NEXT, time_steps_[2]);
  }
  inst_.set_time(time_steps_[1]);
}

/* Runs after rendering a sample. */
void MotionBlurModule::step()
{
  if (!enabled_) {
    return;
  }

  if (inst_.sampling.finished()) {
    /* Restore original frame number. This is because the render pipeline expects it. */
    RE_engine_frame_set(inst_.render, initial_frame_, initial_subframe_);
  }
  else if (inst_.sampling.do_render_sync()) {
    /* Time to change motion step. */
    BLI_assert(time_steps_.size() > step_id_ + 2);
    step_id_ += 2;

    if (motion_blur_fx_enabled_) {
      inst_.velocity.step_swap();
      inst_.velocity.step_sync(eVelocityStep::STEP_NEXT, time_steps_[step_id_ + 1]);
    }
    inst_.set_time(time_steps_[step_id_]);
  }
}

float MotionBlurModule::shutter_time_to_scene_time(float time)
{
  switch (shutter_position_) {
    case SCE_EEVEE_MB_START:
      /* No offset. */
      break;
    case SCE_EEVEE_MB_CENTER:
      time -= 0.5f;
      break;
    case SCE_EEVEE_MB_END:
      time -= 1.0;
      break;
    default:
      BLI_assert(!"Invalid motion blur position enum!");
      break;
  }
  time *= shutter_time_;
  time += frame_time_;
  return time;
}

void MotionBlurModule::sync()
{
  /* Disable motion blur in viewport when changing camera projection type.
   * Avoids really high velocities. */
  if (inst_.velocity.camera_changed_projection()) {
    motion_blur_fx_enabled_ = false;
  }

  if (!motion_blur_fx_enabled_) {
    return;
  }

  eGPUSamplerState no_filter = GPU_SAMPLER_DEFAULT;
  RenderBuffers &render_buffers = inst_.render_buffers;

  {
    /* Create max velocity tiles. */
    DRW_PASS_CREATE(tiles_flatten_ps_, DRW_STATE_NO_DRAW);
    eShaderType shader = (inst_.is_viewport()) ? MOTION_BLUR_TILE_FLATTEN_VIEWPORT :
                                                 MOTION_BLUR_TILE_FLATTEN_RENDER;
    GPUShader *sh = inst_.shaders.static_shader_get(shader);
    DRWShadingGroup *grp = DRW_shgroup_create(sh, tiles_flatten_ps_);
    inst_.velocity.bind_resources(grp);
    DRW_shgroup_uniform_block(grp, "motion_blur_buf", data_);
    DRW_shgroup_uniform_texture_ref(grp, "depth_tx", &render_buffers.depth_tx);
    DRW_shgroup_uniform_image_ref(grp, "velocity_img", &render_buffers.vector_tx);
    DRW_shgroup_uniform_image_ref(grp, "out_tiles_img", &tiles_tx_);

    DRW_shgroup_call_compute_ref(grp, dispatch_flatten_size_);
    DRW_shgroup_barrier(grp, GPU_BARRIER_SHADER_IMAGE_ACCESS | GPU_BARRIER_TEXTURE_FETCH);
  }
  {
    /* Expand max velocity tiles by spreading them in their neighborhood. */
    DRW_PASS_CREATE(tiles_dilate_ps_, DRW_STATE_NO_DRAW);
    GPUShader *sh = inst_.shaders.static_shader_get(MOTION_BLUR_TILE_DILATE);
    DRWShadingGroup *grp = DRW_shgroup_create(sh, tiles_dilate_ps_);
    DRW_shgroup_storage_block(grp, "tile_indirection_buf", tile_indirection_buf_);
    DRW_shgroup_uniform_image_ref(grp, "in_tiles_img", &tiles_tx_);

    DRW_shgroup_call_compute_ref(grp, dispatch_dilate_size_);
    DRW_shgroup_barrier(grp, GPU_BARRIER_SHADER_STORAGE);
  }
  {
    /* Do the motion blur gather algorithm. */
    DRW_PASS_CREATE(gather_ps_, DRW_STATE_NO_DRAW);
    GPUShader *sh = inst_.shaders.static_shader_get(MOTION_BLUR_GATHER);
    DRWShadingGroup *grp = DRW_shgroup_create(sh, gather_ps_);
    inst_.sampling.bind_resources(grp);
    DRW_shgroup_uniform_block(grp, "motion_blur_buf", data_);
    DRW_shgroup_storage_block(grp, "tile_indirection_buf", tile_indirection_buf_);
    DRW_shgroup_uniform_texture_ref_ex(grp, "depth_tx", &render_buffers.depth_tx, no_filter);
    DRW_shgroup_uniform_texture_ref_ex(grp, "velocity_tx", &render_buffers.vector_tx, no_filter);
    DRW_shgroup_uniform_texture_ref_ex(grp, "in_color_tx", &input_color_tx_, no_filter);
    DRW_shgroup_uniform_image_ref(grp, "in_tiles_img", &tiles_tx_);
    DRW_shgroup_uniform_image_ref(grp, "out_color_img", &output_color_tx_);

    DRW_shgroup_call_compute_ref(grp, dispatch_gather_size_);
    DRW_shgroup_barrier(grp, GPU_BARRIER_TEXTURE_FETCH);
  }
}

void MotionBlurModule::render(GPUTexture **input_tx, GPUTexture **output_tx)
{
  if (!motion_blur_fx_enabled_) {
    return;
  }

  const Texture &depth_tx = inst_.render_buffers.depth_tx;

  int2 extent = {depth_tx.width(), depth_tx.height()};
  int2 tiles_extent = math::divide_ceil(extent, int2(MOTION_BLUR_TILE_SIZE));

  if (inst_.is_viewport()) {
    float frame_delta = fabsf(inst_.velocity.step_time_delta_get(STEP_PREVIOUS, STEP_CURRENT));
    /* Avoid highly disturbing blurs, during navigation with high shutter time. */
    if (frame_delta > 0.0f && !DRW_state_is_navigating()) {
      /* Rescale motion blur intensity to be shutter time relative and avoid long streak when we
       * have frame skipping. Always try to stick to what the render frame would look like. */
      data_.motion_scale = float2(shutter_time_ / frame_delta);
    }
    else {
      /* There is no time change. Motion only comes from viewport navigation and object transform.
       * Apply motion blur as smoothing and only blur towards last frame. */
      data_.motion_scale = float2(1.0f, 0.0f);

      if (was_navigating_ != DRW_state_is_navigating()) {
        /* Special case for navigation events that only last for one frame (for instance mouse
         * scroll for zooming). For this case we have to wait for the next frame before enabling
         * the navigation motion blur. */
        was_navigating_ = DRW_state_is_navigating();
        return;
      }
    }
    was_navigating_ = DRW_state_is_navigating();

    /* Change texture swizzling to avoid complexity in gather pass shader. */
    GPU_texture_swizzle_set(inst_.render_buffers.vector_tx, "rgrg");
  }
  else {
    data_.motion_scale = float2(1.0f);
  }
  /* Second motion vector is stored inverted. */
  data_.motion_scale.y = -data_.motion_scale.y;
  data_.target_size_inv = 1.0f / float2(extent);
  data_.push_update();

  input_color_tx_ = *input_tx;
  output_color_tx_ = *output_tx;

  dispatch_flatten_size_ = int3(tiles_extent, 1);
  dispatch_dilate_size_ = int3(math::divide_ceil(tiles_extent, int2(MOTION_BLUR_GROUP_SIZE)), 1);
  dispatch_gather_size_ = int3(math::divide_ceil(extent, int2(MOTION_BLUR_GROUP_SIZE)), 1);

  DRW_stats_group_start("Motion Blur");

  tiles_tx_.acquire(tiles_extent, GPU_RGBA16F);

  GPU_storagebuf_clear_to_zero(tile_indirection_buf_);

  DRW_draw_pass(tiles_flatten_ps_);
  DRW_draw_pass(tiles_dilate_ps_);
  DRW_draw_pass(gather_ps_);

  tiles_tx_.release();

  DRW_stats_group_end();

  if (inst_.is_viewport()) {
    /* Reset swizzle since this texture might be reused in other places. */
    GPU_texture_swizzle_set(inst_.render_buffers.vector_tx, "rgba");
  }

  /* Swap buffers so that next effect has the right input. */
  *input_tx = output_color_tx_;
  *output_tx = input_color_tx_;
}

/** \} */

}  // namespace blender::eevee