Fix T58610: EEVEE: camera motion blur renders only one viewport sample
[blender.git] / source / blender / draw / engines / eevee / eevee_motion_blur.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * Copyright 2016, Blender Foundation.
17  */
18
19 /** \file
20  * \ingroup draw_engine
21  *
22  * Gather all screen space effects technique such as Bloom, Motion Blur, DoF, SSAO, SSR, ...
23  */
24
25 #include "DRW_render.h"
26
27 #include "BKE_camera.h"
28 #include "BKE_object.h"
29 #include "BKE_animsys.h"
30
31 #include "DNA_anim_types.h"
32 #include "DNA_camera_types.h"
33 #include "DNA_screen_types.h"
34
35 #include "ED_screen.h"
36
37 #include "DEG_depsgraph.h"
38 #include "DEG_depsgraph_query.h"
39
40 #include "eevee_private.h"
41 #include "GPU_texture.h"
42
43 static struct {
44         /* Motion Blur */
45         struct GPUShader *motion_blur_sh;
46 } e_data = {NULL}; /* Engine data */
47
48 extern char datatoc_effect_motion_blur_frag_glsl[];
49
50 static void eevee_motion_blur_camera_get_matrix_at_time(
51         Scene *scene,
52         ARegion *ar, RegionView3D *rv3d, View3D *v3d,
53         Object *camera,
54         float time,
55         float r_mat[4][4])
56 {
57         float obmat[4][4];
58
59         /* HACK */
60         Object cam_cpy = *camera;
61         Camera camdata_cpy = *(Camera *)(camera->data);
62         cam_cpy.data = &camdata_cpy;
63
64         const DRWContextState *draw_ctx = DRW_context_state_get();
65
66         /* Past matrix */
67         /* FIXME : This is a temporal solution that does not take care of parent animations */
68         /* Recalc Anim manually */
69         BKE_animsys_evaluate_animdata(draw_ctx->depsgraph, scene, &camdata_cpy.id, camdata_cpy.adt, time, ADT_RECALC_ALL);
70         BKE_object_where_is_calc_time(draw_ctx->depsgraph, scene, &cam_cpy, time);
71
72         /* Compute winmat */
73         CameraParams params;
74         BKE_camera_params_init(&params);
75
76         if (v3d != NULL) {
77                 BKE_camera_params_from_view3d(&params, draw_ctx->depsgraph, v3d, rv3d);
78                 BKE_camera_params_compute_viewplane(&params, ar->winx, ar->winy, 1.0f, 1.0f);
79         }
80         else {
81                 BKE_camera_params_from_object(&params, &cam_cpy);
82                 BKE_camera_params_compute_viewplane(&params, scene->r.xsch, scene->r.ysch, scene->r.xasp, scene->r.yasp);
83         }
84
85         BKE_camera_params_compute_matrix(&params);
86
87         /* FIXME Should be done per view (MULTIVIEW) */
88         normalize_m4_m4(obmat, cam_cpy.obmat);
89         invert_m4(obmat);
90         mul_m4_m4m4(r_mat, params.winmat, obmat);
91 }
92
93 static void eevee_create_shader_motion_blur(void)
94 {
95         e_data.motion_blur_sh = DRW_shader_create_fullscreen(datatoc_effect_motion_blur_frag_glsl, NULL);
96 }
97
98 int EEVEE_motion_blur_init(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata, Object *camera)
99 {
100         EEVEE_StorageList *stl = vedata->stl;
101         EEVEE_EffectsInfo *effects = stl->effects;
102
103         const DRWContextState *draw_ctx = DRW_context_state_get();
104         const Scene *scene_eval = DEG_get_evaluated_scene(draw_ctx->depsgraph);
105         Scene *scene = draw_ctx->scene;
106
107         View3D *v3d = draw_ctx->v3d;
108         RegionView3D *rv3d = draw_ctx->rv3d;
109         ARegion *ar = draw_ctx->ar;
110
111         if (scene_eval->eevee.flag & SCE_EEVEE_MOTION_BLUR_ENABLED) {
112                 /* Update Motion Blur Matrices */
113                 if (camera) {
114                         float persmat[4][4];
115                         float ctime = DEG_get_ctime(draw_ctx->depsgraph);
116                         float delta = scene_eval->eevee.motion_blur_shutter;
117                         Object *ob_camera_eval = DEG_get_evaluated_object(draw_ctx->depsgraph, camera);
118
119                         /* Viewport Matrix */
120                         /* Note: This does not have TAA jitter applied. */
121                         DRW_viewport_matrix_get(persmat, DRW_MAT_PERS);
122
123                         bool view_is_valid = (stl->g_data->view_updated == false);
124
125                         if (draw_ctx->evil_C != NULL) {
126                                 struct wmWindowManager *wm = CTX_wm_manager(draw_ctx->evil_C);
127                                 view_is_valid = view_is_valid && (ED_screen_animation_no_scrub(wm) == NULL);
128                         }
129
130                         /* The view is jittered by the oglrenderer. So avoid testing in this case. */
131                         if (!DRW_state_is_image_render()) {
132                                 view_is_valid = view_is_valid && compare_m4m4(persmat, effects->prev_drw_persmat, FLT_MIN);
133                                 /* WATCH: assume TAA init code runs last. */
134                                 if (scene_eval->eevee.taa_samples == 1) {
135                                         /* Only if TAA is disabled. If not, TAA will update prev_drw_persmat itself. */
136                                         copy_m4_m4(effects->prev_drw_persmat, persmat);
137                                 }
138                         }
139
140                         effects->motion_blur_mat_cached = view_is_valid && !DRW_state_is_image_render();
141
142                         /* Current matrix */
143                         if (effects->motion_blur_mat_cached == false) {
144                                 eevee_motion_blur_camera_get_matrix_at_time(
145                                         scene,
146                                         ar, rv3d, v3d,
147                                         ob_camera_eval,
148                                         ctime,
149                                         effects->current_world_to_ndc);
150                         }
151
152                         /* Only continue if camera is not being keyed */
153                         if (DRW_state_is_image_render() ||
154                             compare_m4m4(persmat, effects->current_world_to_ndc, 0.0001f))
155                         {
156                                 /* Past matrix */
157                                 if (effects->motion_blur_mat_cached == false) {
158                                         eevee_motion_blur_camera_get_matrix_at_time(
159                                                 scene,
160                                                 ar, rv3d, v3d,
161                                                 ob_camera_eval,
162                                                 ctime - delta,
163                                                 effects->past_world_to_ndc);
164
165 #if 0       /* for future high quality blur */
166                                         /* Future matrix */
167                                         eevee_motion_blur_camera_get_matrix_at_time(
168                                                 scene,
169                                                 ar, rv3d, v3d,
170                                                 ob_camera_eval,
171                                                 ctime + delta,
172                                                 effects->future_world_to_ndc);
173 #endif
174                                         invert_m4_m4(effects->current_ndc_to_world, effects->current_world_to_ndc);
175                                 }
176
177                                 effects->motion_blur_mat_cached = true;
178                                 effects->motion_blur_samples = scene_eval->eevee.motion_blur_samples;
179
180                                 if (!e_data.motion_blur_sh) {
181                                         eevee_create_shader_motion_blur();
182                                 }
183
184                                 return EFFECT_MOTION_BLUR | EFFECT_POST_BUFFER;
185                         }
186                 }
187         }
188
189         return 0;
190 }
191
192 void EEVEE_motion_blur_cache_init(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata)
193 {
194         EEVEE_PassList *psl = vedata->psl;
195         EEVEE_StorageList *stl = vedata->stl;
196         EEVEE_EffectsInfo *effects = stl->effects;
197         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
198
199         struct GPUBatch *quad = DRW_cache_fullscreen_quad_get();
200
201         if ((effects->enabled_effects & EFFECT_MOTION_BLUR) != 0) {
202                 psl->motion_blur = DRW_pass_create("Motion Blur", DRW_STATE_WRITE_COLOR);
203
204                 DRWShadingGroup *grp = DRW_shgroup_create(e_data.motion_blur_sh, psl->motion_blur);
205                 DRW_shgroup_uniform_int(grp, "samples", &effects->motion_blur_samples, 1);
206                 DRW_shgroup_uniform_mat4(grp, "currInvViewProjMatrix", effects->current_ndc_to_world);
207                 DRW_shgroup_uniform_mat4(grp, "pastViewProjMatrix", effects->past_world_to_ndc);
208                 DRW_shgroup_uniform_texture_ref(grp, "colorBuffer", &effects->source_buffer);
209                 DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &dtxl->depth);
210                 DRW_shgroup_call_add(grp, quad, NULL);
211         }
212 }
213
214 void EEVEE_motion_blur_draw(EEVEE_Data *vedata)
215 {
216         EEVEE_PassList *psl = vedata->psl;
217         EEVEE_TextureList *txl = vedata->txl;
218         EEVEE_FramebufferList *fbl = vedata->fbl;
219         EEVEE_StorageList *stl = vedata->stl;
220         EEVEE_EffectsInfo *effects = stl->effects;
221
222         /* Motion Blur */
223         if ((effects->enabled_effects & EFFECT_MOTION_BLUR) != 0) {
224                 GPU_framebuffer_bind(effects->target_buffer);
225                 DRW_draw_pass(psl->motion_blur);
226                 SWAP_BUFFERS();
227         }
228 }
229
230 void EEVEE_motion_blur_free(void)
231 {
232         DRW_SHADER_FREE_SAFE(e_data.motion_blur_sh);
233 }