Object Mode: Add to EvaluationContext & DRWContextState
[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         EvaluationContext eval_ctx;
63         float obmat[4][4];
64
65         /* HACK */
66         Object cam_cpy; Camera camdata_cpy;
67         memcpy(&cam_cpy, camera, sizeof(cam_cpy));
68         memcpy(&camdata_cpy, camera->data, sizeof(camdata_cpy));
69         cam_cpy.data = &camdata_cpy;
70
71         /* NOTE: Mode corresponds to old usage of eval_ctx from viewport (which was
72          * actually coming from bmain). It was always DAG_EVAL_VIEWPORT. For F12
73          * render this should be DAG_EVAL_RENDER, but the whole hack is to be
74          * reconsidered first anyway.
75          */
76         const DRWContextState *draw_ctx = DRW_context_state_get();
77         DEG_evaluation_context_init_from_scene(
78                 &eval_ctx,
79                 scene,
80                 draw_ctx->view_layer,
81                 draw_ctx->engine_type,
82                 draw_ctx->object_mode,
83                 DAG_EVAL_VIEWPORT);
84         eval_ctx.ctime = time;
85
86         /* Past matrix */
87         /* FIXME : This is a temporal solution that does not take care of parent animations */
88         /* Recalc Anim manualy */
89         BKE_animsys_evaluate_animdata(scene, &cam_cpy.id, cam_cpy.adt, time, ADT_RECALC_ALL);
90         BKE_animsys_evaluate_animdata(scene, &camdata_cpy.id, camdata_cpy.adt, time, ADT_RECALC_ALL);
91         BKE_object_where_is_calc_time(&eval_ctx, scene, &cam_cpy, time);
92
93         /* Compute winmat */
94         CameraParams params;
95         BKE_camera_params_init(&params);
96
97         if (v3d != NULL) {
98                 BKE_camera_params_from_view3d(&params, draw_ctx->depsgraph, v3d, rv3d);
99                 BKE_camera_params_compute_viewplane(&params, ar->winx, ar->winy, 1.0f, 1.0f);
100         }
101         else {
102                 BKE_camera_params_from_object(&params, &cam_cpy);
103                 BKE_camera_params_compute_viewplane(&params, scene->r.xsch, scene->r.ysch, scene->r.xasp, scene->r.yasp);
104         }
105
106         BKE_camera_params_compute_matrix(&params);
107
108         /* FIXME Should be done per view (MULTIVIEW) */
109         normalize_m4_m4(obmat, cam_cpy.obmat);
110         invert_m4(obmat);
111         mul_m4_m4m4(r_mat, params.winmat, obmat);
112 }
113
114 static void eevee_create_shader_motion_blur(void)
115 {
116         e_data.motion_blur_sh = DRW_shader_create_fullscreen(datatoc_effect_motion_blur_frag_glsl, NULL);
117 }
118
119 int EEVEE_motion_blur_init(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata, Object *camera)
120 {
121         EEVEE_StorageList *stl = vedata->stl;
122         EEVEE_EffectsInfo *effects = stl->effects;
123
124         const DRWContextState *draw_ctx = DRW_context_state_get();
125         ViewLayer *view_layer = draw_ctx->view_layer;
126         Scene *scene = draw_ctx->scene;
127         View3D *v3d = draw_ctx->v3d;
128         RegionView3D *rv3d = draw_ctx->rv3d;
129         ARegion *ar = draw_ctx->ar;
130         IDProperty *props = BKE_view_layer_engine_evaluated_get(view_layer,
131                                                                 COLLECTION_MODE_NONE,
132                                                                 RE_engine_id_BLENDER_EEVEE);
133
134         if (BKE_collection_engine_property_value_get_bool(props, "motion_blur_enable")) {
135                 /* Update Motion Blur Matrices */
136                 if (camera) {
137                         float persmat[4][4];
138                         float ctime = BKE_scene_frame_get(scene);
139                         float delta = BKE_collection_engine_property_value_get_float(props, "motion_blur_shutter");
140                         Object *camera_object = DEG_get_evaluated_object(draw_ctx->depsgraph, camera);
141
142                         /* Current matrix */
143                         eevee_motion_blur_camera_get_matrix_at_time(scene,
144                                                                     ar, rv3d, v3d,
145                                                                     camera_object,
146                                                                     ctime,
147                                                                     effects->current_ndc_to_world);
148
149                         /* Viewport Matrix */
150                         DRW_viewport_matrix_get(persmat, DRW_MAT_PERS);
151
152                         /* Only continue if camera is not being keyed */
153                         if (DRW_state_is_image_render() ||
154                             compare_m4m4(persmat, effects->current_ndc_to_world, 0.0001f))
155                         {
156                                 /* Past matrix */
157                                 eevee_motion_blur_camera_get_matrix_at_time(scene,
158                                                                             ar, rv3d, v3d,
159                                                                             camera_object,
160                                                                             ctime - delta,
161                                                                             effects->past_world_to_ndc);
162
163 #if 0       /* for future high quality blur */
164                                 /* Future matrix */
165                                 eevee_motion_blur_camera_get_matrix_at_time(scene,
166                                                                             ar, rv3d, v3d,
167                                                                             camera_object,
168                                                                             ctime + delta,
169                                                                             effects->future_world_to_ndc);
170 #endif
171                                 invert_m4(effects->current_ndc_to_world);
172
173                                 effects->motion_blur_samples = BKE_collection_engine_property_value_get_int(props,
174                                                                                                             "motion_blur_samples");
175
176                                 if (!e_data.motion_blur_sh) {
177                                         eevee_create_shader_motion_blur();
178                                 }
179
180                                 return EFFECT_MOTION_BLUR | EFFECT_POST_BUFFER;
181                         }
182                 }
183         }
184
185         return 0;
186 }
187
188 void EEVEE_motion_blur_cache_init(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata)
189 {
190         EEVEE_PassList *psl = vedata->psl;
191         EEVEE_StorageList *stl = vedata->stl;
192         EEVEE_EffectsInfo *effects = stl->effects;
193         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
194
195         struct Gwn_Batch *quad = DRW_cache_fullscreen_quad_get();
196
197         if ((effects->enabled_effects & EFFECT_MOTION_BLUR) != 0) {
198                 psl->motion_blur = DRW_pass_create("Motion Blur", DRW_STATE_WRITE_COLOR);
199
200                 DRWShadingGroup *grp = DRW_shgroup_create(e_data.motion_blur_sh, psl->motion_blur);
201                 DRW_shgroup_uniform_int(grp, "samples", &effects->motion_blur_samples, 1);
202                 DRW_shgroup_uniform_mat4(grp, "currInvViewProjMatrix", (float *)effects->current_ndc_to_world);
203                 DRW_shgroup_uniform_mat4(grp, "pastViewProjMatrix", (float *)effects->past_world_to_ndc);
204                 DRW_shgroup_uniform_buffer(grp, "colorBuffer", &effects->source_buffer);
205                 DRW_shgroup_uniform_buffer(grp, "depthBuffer", &dtxl->depth);
206                 DRW_shgroup_call_add(grp, quad, NULL);
207         }
208 }
209
210 void EEVEE_motion_blur_draw(EEVEE_Data *vedata)
211 {
212         EEVEE_PassList *psl = vedata->psl;
213         EEVEE_TextureList *txl = vedata->txl;
214         EEVEE_FramebufferList *fbl = vedata->fbl;
215         EEVEE_StorageList *stl = vedata->stl;
216         EEVEE_EffectsInfo *effects = stl->effects;
217
218         /* Motion Blur */
219         if ((effects->enabled_effects & EFFECT_MOTION_BLUR) != 0) {
220                 DRW_framebuffer_bind(effects->target_buffer);
221                 DRW_draw_pass(psl->motion_blur);
222                 SWAP_BUFFERS();
223         }
224 }
225
226 void EEVEE_motion_blur_free(void)
227 {
228         DRW_SHADER_FREE_SAFE(e_data.motion_blur_sh);
229 }