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

eevee_motion_blur.c « eevee « engines « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 612695b849b965143849c4476b611e6b123574a3 (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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Copyright 2016, Blender Foundation.
 */

/** \file
 * \ingroup draw_engine
 *
 * Gather all screen space effects technique such as Bloom, Motion Blur, DoF, SSAO, SSR, ...
 */

#include "DRW_render.h"

#include "BLI_rand.h"

#include "BKE_animsys.h"
#include "BKE_camera.h"
#include "BKE_object.h"
#include "BKE_screen.h"

#include "DNA_anim_types.h"
#include "DNA_camera_types.h"
#include "DNA_mesh_types.h"
#include "DNA_screen_types.h"

#include "ED_screen.h"

#include "DEG_depsgraph.h"
#include "DEG_depsgraph_query.h"

#include "GPU_batch.h"
#include "GPU_texture.h"
#include "eevee_private.h"

static struct {
  /* Motion Blur */
  struct GPUShader *motion_blur_sh;
  struct GPUShader *motion_blur_object_sh;
  struct GPUShader *velocity_tiles_sh;
  struct GPUShader *velocity_tiles_expand_sh;
} e_data = {NULL}; /* Engine data */

extern char datatoc_effect_velocity_tile_frag_glsl[];
extern char datatoc_effect_motion_blur_frag_glsl[];
extern char datatoc_object_motion_frag_glsl[];
extern char datatoc_object_motion_vert_glsl[];
extern char datatoc_common_view_lib_glsl[];

static void eevee_create_shader_motion_blur(void)
{
  e_data.motion_blur_sh = DRW_shader_create_fullscreen(datatoc_effect_motion_blur_frag_glsl, NULL);
  e_data.motion_blur_object_sh = DRW_shader_create_with_lib(datatoc_object_motion_vert_glsl,
                                                            NULL,
                                                            datatoc_object_motion_frag_glsl,
                                                            datatoc_common_view_lib_glsl,
                                                            NULL);
  e_data.velocity_tiles_sh = DRW_shader_create_fullscreen(datatoc_effect_velocity_tile_frag_glsl,
                                                          "#define TILE_GATHER\n");
  e_data.velocity_tiles_expand_sh = DRW_shader_create_fullscreen(
      datatoc_effect_velocity_tile_frag_glsl, "#define TILE_EXPANSION\n");
}

#if 0
static void eevee_motion_blur_past_persmat_get(const CameraParams *past_params,
                                               const CameraParams *current_params,
                                               const RegionView3D *rv3d,
                                               const ARegion *region,
                                               const float (*world_to_view)[4],
                                               float (*r_world_to_ndc)[4])
{
  CameraParams params = *past_params;
  params.offsetx = current_params->offsetx;
  params.offsety = current_params->offsety;
  params.zoom = current_params->zoom;

  float zoom = BKE_screen_view3d_zoom_to_fac(rv3d->camzoom);
  params.shiftx *= zoom;
  params.shifty *= zoom;

  BKE_camera_params_compute_viewplane(&params, region->winx, region->winy, 1.0f, 1.0f);
  BKE_camera_params_compute_matrix(&params);

  mul_m4_m4m4(r_world_to_ndc, params.winmat, world_to_view);
}
#endif

int EEVEE_motion_blur_init(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata, Object *camera)
{
  EEVEE_StorageList *stl = vedata->stl;
  EEVEE_FramebufferList *fbl = vedata->fbl;
  EEVEE_EffectsInfo *effects = stl->effects;

  const DRWContextState *draw_ctx = DRW_context_state_get();
  Scene *scene = draw_ctx->scene;

  /* Viewport not supported for now. */
  if (!DRW_state_is_scene_render()) {
    return 0;
  }

  if (scene->eevee.flag & SCE_EEVEE_MOTION_BLUR_ENABLED) {
    if (!e_data.motion_blur_sh) {
      eevee_create_shader_motion_blur();
    }

    if (DRW_state_is_scene_render()) {
      int mb_step = effects->motion_blur_step;
      DRW_view_viewmat_get(NULL, effects->motion_blur.camera[mb_step].viewmat, false);
      DRW_view_persmat_get(NULL, effects->motion_blur.camera[mb_step].persmat, false);
      DRW_view_persmat_get(NULL, effects->motion_blur.camera[mb_step].persinv, true);
    }

    if (camera != NULL) {
      Camera *cam = camera->data;
      effects->motion_blur_near_far[0] = cam->clip_start;
      effects->motion_blur_near_far[1] = cam->clip_end;
    }
    else {
      /* Not supported yet. */
      BLI_assert(0);
    }

#if 0 /* For when we can do viewport motion blur. */
    /* Update Motion Blur Matrices */
    if (camera && (camera->type == OB_CAMERA) && (camera->data != NULL)) {
      if (effects->current_time != ctime) {
        copy_m4_m4(effects->past_world_to_ndc, effects->current_world_to_ndc);
        copy_m4_m4(effects->past_world_to_view, effects->current_world_to_view);
        effects->past_time = effects->current_time;
        effects->past_cam_params = effects->current_cam_params;
      }
      DRW_view_viewmat_get(NULL, effects->current_world_to_view, false);
      DRW_view_persmat_get(NULL, effects->current_world_to_ndc, false);
      DRW_view_persmat_get(NULL, effects->current_ndc_to_world, true);

      if (draw_ctx->v3d) {
        CameraParams params;
        /* Save object params for next frame. */
        BKE_camera_params_init(&effects->current_cam_params);
        BKE_camera_params_from_object(&effects->current_cam_params, camera);
        /* Compute v3d params to apply on last frame object params. */
        BKE_camera_params_init(&params);
        BKE_camera_params_from_view3d(&params, draw_ctx->depsgraph, draw_ctx->v3d, draw_ctx->rv3d);

        eevee_motion_blur_past_persmat_get(&effects->past_cam_params,
                                           &params,
                                           draw_ctx->rv3d,
                                           draw_ctx->region,
                                           effects->past_world_to_view,
                                           effects->past_world_to_ndc);
      }

      effects->current_time = ctime;

      if (effects->cam_params_init == false) {
        /* Disable motion blur if not initialized. */
        copy_m4_m4(effects->past_world_to_ndc, effects->current_world_to_ndc);
        copy_m4_m4(effects->past_world_to_view, effects->current_world_to_view);
        effects->past_time = effects->current_time;
        effects->past_cam_params = effects->current_cam_params;
        effects->cam_params_init = true;
      }
    }
    else {
      /* Make no camera motion blur by using the same matrix for previous and current transform. */
      DRW_view_persmat_get(NULL, effects->past_world_to_ndc, false);
      DRW_view_persmat_get(NULL, effects->current_world_to_ndc, false);
      DRW_view_persmat_get(NULL, effects->current_ndc_to_world, true);
      effects->past_time = effects->current_time = ctime;
      effects->cam_params_init = false;
    }
#endif

    effects->motion_blur_max = 32;
    const float *fs_size = DRW_viewport_size_get();
    int tx_size[2] = {1 + ((int)fs_size[0] / effects->motion_blur_max),
                      1 + ((int)fs_size[1] / effects->motion_blur_max)};

    effects->velocity_tiles_x_tx = DRW_texture_pool_query_2d(
        tx_size[0], fs_size[1], GPU_RGBA16, &draw_engine_eevee_type);
    GPU_framebuffer_ensure_config(&fbl->velocity_tiles_x_fb,
                                  {
                                      GPU_ATTACHMENT_NONE,
                                      GPU_ATTACHMENT_TEXTURE(effects->velocity_tiles_x_tx),
                                  });

    effects->velocity_tiles_tx = DRW_texture_pool_query_2d(
        tx_size[0], tx_size[1], GPU_RGBA16, &draw_engine_eevee_type);
    GPU_framebuffer_ensure_config(&fbl->velocity_tiles_fb,
                                  {
                                      GPU_ATTACHMENT_NONE,
                                      GPU_ATTACHMENT_TEXTURE(effects->velocity_tiles_tx),
                                  });

    effects->velocity_tiles_expand_tx = DRW_texture_pool_query_2d(
        tx_size[0], tx_size[1], GPU_RGBA16, &draw_engine_eevee_type);
    GPU_framebuffer_ensure_config(&fbl->velocity_tiles_expand_fb,
                                  {
                                      GPU_ATTACHMENT_NONE,
                                      GPU_ATTACHMENT_TEXTURE(effects->velocity_tiles_expand_tx),
                                  });

    return EFFECT_MOTION_BLUR | EFFECT_POST_BUFFER | EFFECT_VELOCITY_BUFFER;
  }
  return 0;
}

void EEVEE_motion_blur_step_set(EEVEE_Data *vedata, int step)
{
  BLI_assert(step < 3);
  /* Meh, code duplication. Could be avoided if render init would not contain cache init. */
  if (vedata->stl->effects == NULL) {
    vedata->stl->effects = MEM_callocN(sizeof(*vedata->stl->effects), __func__);
  }
  vedata->stl->effects->motion_blur_step = step;
}

void EEVEE_motion_blur_cache_init(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata)
{
  EEVEE_PassList *psl = vedata->psl;
  EEVEE_StorageList *stl = vedata->stl;
  EEVEE_EffectsInfo *effects = stl->effects;
  EEVEE_MotionBlurData *mb_data = &effects->motion_blur;
  DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
  const DRWContextState *draw_ctx = DRW_context_state_get();
  Scene *scene = draw_ctx->scene;

  if ((effects->enabled_effects & EFFECT_MOTION_BLUR) != 0) {
    DRWShadingGroup *grp;
    {
      DRW_PASS_CREATE(psl->velocity_tiles_x, DRW_STATE_WRITE_COLOR);
      DRW_PASS_CREATE(psl->velocity_tiles, DRW_STATE_WRITE_COLOR);
      DRW_PASS_CREATE(psl->velocity_tiles_expand, DRW_STATE_WRITE_COLOR);
      eGPUSamplerState state = 0;

      /* Create max velocity tiles in 2 passes. One for X and one for Y */
      GPUShader *sh = e_data.velocity_tiles_sh;
      grp = DRW_shgroup_create(sh, psl->velocity_tiles_x);
      DRW_shgroup_uniform_texture_ex(grp, "velocityBuffer", effects->velocity_tx, state);
      DRW_shgroup_uniform_int_copy(grp, "maxBlurRadius", effects->motion_blur_max);
      DRW_shgroup_uniform_vec2(grp, "viewportSize", DRW_viewport_size_get(), 1);
      DRW_shgroup_uniform_vec2(grp, "viewportSizeInv", DRW_viewport_invert_size_get(), 1);
      DRW_shgroup_uniform_ivec2_copy(grp, "gatherStep", (int[2]){1, 0});
      DRW_shgroup_call_procedural_triangles(grp, NULL, 1);

      grp = DRW_shgroup_create(sh, psl->velocity_tiles);
      DRW_shgroup_uniform_texture_ex(grp, "velocityBuffer", effects->velocity_tiles_x_tx, state);
      DRW_shgroup_uniform_ivec2_copy(grp, "gatherStep", (int[2]){0, 1});
      DRW_shgroup_call_procedural_triangles(grp, NULL, 1);

      /* Expand max tiles by keeping the max tile in each tile neighborhood. */
      GPUShader *sh_expand = e_data.velocity_tiles_expand_sh;
      grp = DRW_shgroup_create(sh_expand, psl->velocity_tiles_expand);
      DRW_shgroup_uniform_texture_ex(grp, "velocityBuffer", effects->velocity_tiles_tx, state);
      DRW_shgroup_uniform_vec2(grp, "viewportSize", DRW_viewport_size_get(), 1);
      DRW_shgroup_uniform_vec2(grp, "viewportSizeInv", DRW_viewport_invert_size_get(), 1);
      DRW_shgroup_call_procedural_triangles(grp, NULL, 1);
    }
    {
      DRW_PASS_CREATE(psl->motion_blur, DRW_STATE_WRITE_COLOR);
      eGPUSamplerState state = 0;

      grp = DRW_shgroup_create(e_data.motion_blur_sh, psl->motion_blur);
      DRW_shgroup_uniform_int_copy(grp, "samples", scene->eevee.motion_blur_samples);
      DRW_shgroup_uniform_float(grp, "sampleOffset", &effects->motion_blur_sample_offset, 1);
      DRW_shgroup_uniform_texture(grp, "utilTex", EEVEE_materials_get_util_tex());
      DRW_shgroup_uniform_texture_ref_ex(grp, "colorBuffer", &effects->source_buffer, state);
      DRW_shgroup_uniform_texture_ref_ex(grp, "depthBuffer", &dtxl->depth, state);
      DRW_shgroup_uniform_texture_ref_ex(grp, "velocityBuffer", &effects->velocity_tx, state);
      DRW_shgroup_uniform_texture_ref_ex(
          grp, "tileMaxBuffer", &effects->velocity_tiles_expand_tx, state);
      DRW_shgroup_uniform_int_copy(grp, "maxBlurRadius", effects->motion_blur_max);
      DRW_shgroup_uniform_vec2(grp, "nearFar", effects->motion_blur_near_far, 1);
      DRW_shgroup_uniform_bool_copy(grp, "isPerspective", DRW_view_is_persp_get(NULL));
      DRW_shgroup_uniform_vec2(grp, "viewportSize", DRW_viewport_size_get(), 1);
      DRW_shgroup_uniform_vec2(grp, "viewportSizeInv", DRW_viewport_invert_size_get(), 1);
      DRW_shgroup_call_procedural_triangles(grp, NULL, 1);
    }
    {
      DRW_PASS_CREATE(psl->velocity_object, DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL);

      grp = DRW_shgroup_create(e_data.motion_blur_object_sh, psl->velocity_object);
      DRW_shgroup_uniform_mat4(grp, "prevViewProjMatrix", mb_data->camera[MB_PREV].persmat);
      DRW_shgroup_uniform_mat4(grp, "currViewProjMatrix", mb_data->camera[MB_CURR].persmat);
      DRW_shgroup_uniform_mat4(grp, "nextViewProjMatrix", mb_data->camera[MB_NEXT].persmat);
    }

    EEVEE_motion_blur_data_init(mb_data);
  }
  else {
    psl->motion_blur = NULL;
    psl->velocity_object = NULL;
  }
}

void EEVEE_motion_blur_cache_populate(EEVEE_ViewLayerData *UNUSED(sldata),
                                      EEVEE_Data *vedata,
                                      Object *ob)
{
  EEVEE_PassList *psl = vedata->psl;
  EEVEE_StorageList *stl = vedata->stl;
  EEVEE_EffectsInfo *effects = stl->effects;
  DRWShadingGroup *grp = NULL;

  /* TODO(fclem) Also detect if object has any motion. */
  if (!DRW_state_is_scene_render() || psl->velocity_object == NULL) {
    return;
  }

  EEVEE_ObjectMotionData *mb_data = EEVEE_motion_blur_object_data_get(&effects->motion_blur, ob);

  if (mb_data) {
    int mb_step = effects->motion_blur_step;
    /* Store transform  */
    copy_m4_m4(mb_data->obmat[mb_step], ob->obmat);

    EEVEE_GeometryMotionData *mb_geom = EEVEE_motion_blur_geometry_data_get(&effects->motion_blur,
                                                                            ob);

    if (mb_step == MB_CURR) {
      GPUBatch *batch = DRW_cache_object_surface_get(ob);
      if (batch == NULL || mb_geom->vbo[0] == NULL) {
        return;
      }

      grp = DRW_shgroup_create(e_data.motion_blur_object_sh, psl->velocity_object);
      DRW_shgroup_uniform_mat4(grp, "prevModelMatrix", mb_data->obmat[MB_PREV]);
      DRW_shgroup_uniform_mat4(grp, "currModelMatrix", mb_data->obmat[MB_CURR]);
      DRW_shgroup_uniform_mat4(grp, "nextModelMatrix", mb_data->obmat[MB_NEXT]);
      DRW_shgroup_uniform_bool(grp, "useDeform", &mb_geom->use_deform, 1);

      DRW_shgroup_call(grp, batch, ob);
      /* Keep to modify later (after init). */
      mb_geom->batch = batch;
    }
    else {
      /* Store vertex position buffer. */
      mb_geom->vbo[mb_step] = DRW_cache_object_pos_vertbuf_get(ob);
      /* TODO(fclem) only limit deform motion blur to object that needs it. */
      mb_geom->use_deform = (mb_geom->vbo[mb_step] != NULL);
    }
  }
}

void EEVEE_motion_blur_cache_finish(EEVEE_Data *vedata)
{
  EEVEE_StorageList *stl = vedata->stl;
  EEVEE_EffectsInfo *effects = stl->effects;
  GHashIterator ghi;

  if ((effects->enabled_effects & EFFECT_MOTION_BLUR) == 0) {
    return;
  }

  for (BLI_ghashIterator_init(&ghi, effects->motion_blur.geom);
       BLI_ghashIterator_done(&ghi) == false;
       BLI_ghashIterator_step(&ghi)) {
    EEVEE_GeometryMotionData *mb_geom = BLI_ghashIterator_getValue(&ghi);

    int mb_step = effects->motion_blur_step;

    if (!mb_geom->use_deform) {
      continue;
    }

    if (mb_step == MB_CURR) {
      /* Modify batch to have data from adjacent frames. */
      GPUBatch *batch = mb_geom->batch;
      for (int i = 0; i < MB_CURR; i++) {
        GPUVertBuf *vbo = mb_geom->vbo[i];
        if (vbo && batch) {
          if (vbo->vertex_len != batch->verts[0]->vertex_len) {
            /* Vertex count mismatch, disable deform motion blur. */
            mb_geom->use_deform = false;
            GPU_VERTBUF_DISCARD_SAFE(mb_geom->vbo[MB_PREV]);
            GPU_VERTBUF_DISCARD_SAFE(mb_geom->vbo[MB_NEXT]);
            break;
          }
          /* Modify the batch to include the previous position. */
          GPU_batch_vertbuf_add_ex(batch, vbo, true);
          /* TODO(fclem) keep the vbo around for next (sub)frames. */
          /* Only do once. */
          mb_geom->vbo[i] = NULL;
        }
      }
    }
    else {
      GPUVertBuf *vbo = mb_geom->vbo[mb_step];
      /* If this assert fails, it means that different EEVEE_GeometryMotionDatas
       * has been used for each motion blur step.  */
      BLI_assert(vbo);
      if (vbo) {
        /* Use the vbo to perform the copy on the GPU. */
        GPU_vertbuf_use(vbo);
        /* Perform a copy to avoid loosing it after RE_engine_frame_set(). */
        mb_geom->vbo[mb_step] = vbo = GPU_vertbuf_duplicate(vbo);
        /* Find and replace "pos" attrib name. */
        int attrib_id = GPU_vertformat_attr_id_get(&vbo->format, "pos");
        GPU_vertformat_attr_rename(&vbo->format, attrib_id, (mb_step == MB_PREV) ? "prv" : "nxt");
      }
    }
  }
}

void EEVEE_motion_blur_draw(EEVEE_Data *vedata)
{
  EEVEE_PassList *psl = vedata->psl;
  EEVEE_TextureList *txl = vedata->txl;
  EEVEE_FramebufferList *fbl = vedata->fbl;
  EEVEE_StorageList *stl = vedata->stl;
  EEVEE_EffectsInfo *effects = stl->effects;

  /* Motion Blur */
  if ((effects->enabled_effects & EFFECT_MOTION_BLUR) != 0) {
    int sample = DRW_state_is_image_render() ? effects->taa_render_sample :
                                               effects->taa_current_sample;
    double r;
    BLI_halton_1d(2, 0.0, sample - 1, &r);
    effects->motion_blur_sample_offset = r;

    GPU_framebuffer_bind(fbl->velocity_tiles_x_fb);
    DRW_draw_pass(psl->velocity_tiles_x);

    GPU_framebuffer_bind(fbl->velocity_tiles_fb);
    DRW_draw_pass(psl->velocity_tiles);

    GPU_framebuffer_bind(fbl->velocity_tiles_expand_fb);
    DRW_draw_pass(psl->velocity_tiles_expand);

    GPU_framebuffer_bind(effects->target_buffer);
    DRW_draw_pass(psl->motion_blur);
    SWAP_BUFFERS();
  }
}

void EEVEE_motion_blur_free(void)
{
  DRW_SHADER_FREE_SAFE(e_data.motion_blur_sh);
  DRW_SHADER_FREE_SAFE(e_data.motion_blur_object_sh);
  DRW_SHADER_FREE_SAFE(e_data.velocity_tiles_sh);
  DRW_SHADER_FREE_SAFE(e_data.velocity_tiles_expand_sh);
}