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