9ffd20069983b39d75cb10cfa84007f90c20a310
[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
36 #include "DEG_depsgraph.h"
37 #include "DEG_depsgraph_query.h"
38
39 #include "eevee_private.h"
40 #include "GPU_texture.h"
41
42 static struct {
43         /* Motion Blur */
44         struct GPUShader *motion_blur_sh;
45 } e_data = {NULL}; /* Engine data */
46
47 extern char datatoc_effect_motion_blur_frag_glsl[];
48
49 static void eevee_motion_blur_camera_get_matrix_at_time(
50         Scene *scene,
51         ARegion *ar, RegionView3D *rv3d, View3D *v3d,
52         Object *camera,
53         float time,
54         float r_mat[4][4])
55 {
56         float obmat[4][4];
57
58         /* HACK */
59         Object cam_cpy; Camera camdata_cpy;
60         memcpy(&cam_cpy, camera, sizeof(cam_cpy));
61         memcpy(&camdata_cpy, camera->data, sizeof(camdata_cpy));
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                         /* Current matrix */
120                         eevee_motion_blur_camera_get_matrix_at_time(
121                                 scene,
122                                 ar, rv3d, v3d,
123                                 ob_camera_eval,
124                                 ctime,
125                                 effects->current_ndc_to_world);
126
127                         /* Viewport Matrix */
128                         DRW_viewport_matrix_get(persmat, DRW_MAT_PERS);
129
130                         /* Only continue if camera is not being keyed */
131                         if (DRW_state_is_image_render() ||
132                             compare_m4m4(persmat, effects->current_ndc_to_world, 0.0001f))
133                         {
134                                 /* Past matrix */
135                                 eevee_motion_blur_camera_get_matrix_at_time(
136                                         scene,
137                                         ar, rv3d, v3d,
138                                         ob_camera_eval,
139                                         ctime - delta,
140                                         effects->past_world_to_ndc);
141
142 #if 0       /* for future high quality blur */
143                                 /* Future matrix */
144                                 eevee_motion_blur_camera_get_matrix_at_time(
145                                         scene,
146                                         ar, rv3d, v3d,
147                                         ob_camera_eval,
148                                         ctime + delta,
149                                         effects->future_world_to_ndc);
150 #endif
151                                 invert_m4(effects->current_ndc_to_world);
152
153                                 effects->motion_blur_samples = scene_eval->eevee.motion_blur_samples;
154
155                                 if (!e_data.motion_blur_sh) {
156                                         eevee_create_shader_motion_blur();
157                                 }
158
159                                 return EFFECT_MOTION_BLUR | EFFECT_POST_BUFFER;
160                         }
161                 }
162         }
163
164         return 0;
165 }
166
167 void EEVEE_motion_blur_cache_init(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata)
168 {
169         EEVEE_PassList *psl = vedata->psl;
170         EEVEE_StorageList *stl = vedata->stl;
171         EEVEE_EffectsInfo *effects = stl->effects;
172         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
173
174         struct GPUBatch *quad = DRW_cache_fullscreen_quad_get();
175
176         if ((effects->enabled_effects & EFFECT_MOTION_BLUR) != 0) {
177                 psl->motion_blur = DRW_pass_create("Motion Blur", DRW_STATE_WRITE_COLOR);
178
179                 DRWShadingGroup *grp = DRW_shgroup_create(e_data.motion_blur_sh, psl->motion_blur);
180                 DRW_shgroup_uniform_int(grp, "samples", &effects->motion_blur_samples, 1);
181                 DRW_shgroup_uniform_mat4(grp, "currInvViewProjMatrix", effects->current_ndc_to_world);
182                 DRW_shgroup_uniform_mat4(grp, "pastViewProjMatrix", effects->past_world_to_ndc);
183                 DRW_shgroup_uniform_texture_ref(grp, "colorBuffer", &effects->source_buffer);
184                 DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &dtxl->depth);
185                 DRW_shgroup_call_add(grp, quad, NULL);
186         }
187 }
188
189 void EEVEE_motion_blur_draw(EEVEE_Data *vedata)
190 {
191         EEVEE_PassList *psl = vedata->psl;
192         EEVEE_TextureList *txl = vedata->txl;
193         EEVEE_FramebufferList *fbl = vedata->fbl;
194         EEVEE_StorageList *stl = vedata->stl;
195         EEVEE_EffectsInfo *effects = stl->effects;
196
197         /* Motion Blur */
198         if ((effects->enabled_effects & EFFECT_MOTION_BLUR) != 0) {
199                 GPU_framebuffer_bind(effects->target_buffer);
200                 DRW_draw_pass(psl->motion_blur);
201                 SWAP_BUFFERS();
202         }
203 }
204
205 void EEVEE_motion_blur_free(void)
206 {
207         DRW_SHADER_FREE_SAFE(e_data.motion_blur_sh);
208 }