Move EEVEE properties into scene
[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         const Scene *scene_eval = DEG_get_evaluated_scene(draw_ctx->depsgraph);
112         Scene *scene = draw_ctx->scene;
113
114         View3D *v3d = draw_ctx->v3d;
115         RegionView3D *rv3d = draw_ctx->rv3d;
116         ARegion *ar = draw_ctx->ar;
117
118         if (scene_eval->eevee.flag & SCE_EEVEE_MOTION_BLUR_ENABLED) {
119                 /* Update Motion Blur Matrices */
120                 if (camera) {
121                         float persmat[4][4];
122                         float ctime = BKE_scene_frame_get(scene_eval);
123                         float delta = scene_eval->eevee.motion_blur_shutter;
124                         Object *camera_object = DEG_get_evaluated_object(draw_ctx->depsgraph, camera);
125
126                         /* Current matrix */
127                         eevee_motion_blur_camera_get_matrix_at_time(scene,
128                                                                     ar, rv3d, v3d,
129                                                                     camera_object,
130                                                                     ctime,
131                                                                     effects->current_ndc_to_world);
132
133                         /* Viewport Matrix */
134                         DRW_viewport_matrix_get(persmat, DRW_MAT_PERS);
135
136                         /* Only continue if camera is not being keyed */
137                         if (DRW_state_is_image_render() ||
138                             compare_m4m4(persmat, effects->current_ndc_to_world, 0.0001f))
139                         {
140                                 /* Past matrix */
141                                 eevee_motion_blur_camera_get_matrix_at_time(scene,
142                                                                             ar, rv3d, v3d,
143                                                                             camera_object,
144                                                                             ctime - delta,
145                                                                             effects->past_world_to_ndc);
146
147 #if 0       /* for future high quality blur */
148                                 /* Future matrix */
149                                 eevee_motion_blur_camera_get_matrix_at_time(scene,
150                                                                             ar, rv3d, v3d,
151                                                                             camera_object,
152                                                                             ctime + delta,
153                                                                             effects->future_world_to_ndc);
154 #endif
155                                 invert_m4(effects->current_ndc_to_world);
156
157                                 effects->motion_blur_samples = scene_eval->eevee.motion_blur_samples;
158
159                                 if (!e_data.motion_blur_sh) {
160                                         eevee_create_shader_motion_blur();
161                                 }
162
163                                 return EFFECT_MOTION_BLUR | EFFECT_POST_BUFFER;
164                         }
165                 }
166         }
167
168         return 0;
169 }
170
171 void EEVEE_motion_blur_cache_init(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata)
172 {
173         EEVEE_PassList *psl = vedata->psl;
174         EEVEE_StorageList *stl = vedata->stl;
175         EEVEE_EffectsInfo *effects = stl->effects;
176         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
177
178         struct Gwn_Batch *quad = DRW_cache_fullscreen_quad_get();
179
180         if ((effects->enabled_effects & EFFECT_MOTION_BLUR) != 0) {
181                 psl->motion_blur = DRW_pass_create("Motion Blur", DRW_STATE_WRITE_COLOR);
182
183                 DRWShadingGroup *grp = DRW_shgroup_create(e_data.motion_blur_sh, psl->motion_blur);
184                 DRW_shgroup_uniform_int(grp, "samples", &effects->motion_blur_samples, 1);
185                 DRW_shgroup_uniform_mat4(grp, "currInvViewProjMatrix", effects->current_ndc_to_world);
186                 DRW_shgroup_uniform_mat4(grp, "pastViewProjMatrix", effects->past_world_to_ndc);
187                 DRW_shgroup_uniform_texture_ref(grp, "colorBuffer", &effects->source_buffer);
188                 DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &dtxl->depth);
189                 DRW_shgroup_call_add(grp, quad, NULL);
190         }
191 }
192
193 void EEVEE_motion_blur_draw(EEVEE_Data *vedata)
194 {
195         EEVEE_PassList *psl = vedata->psl;
196         EEVEE_TextureList *txl = vedata->txl;
197         EEVEE_FramebufferList *fbl = vedata->fbl;
198         EEVEE_StorageList *stl = vedata->stl;
199         EEVEE_EffectsInfo *effects = stl->effects;
200
201         /* Motion Blur */
202         if ((effects->enabled_effects & EFFECT_MOTION_BLUR) != 0) {
203                 GPU_framebuffer_bind(effects->target_buffer);
204                 DRW_draw_pass(psl->motion_blur);
205                 SWAP_BUFFERS();
206         }
207 }
208
209 void EEVEE_motion_blur_free(void)
210 {
211         DRW_SHADER_FREE_SAFE(e_data.motion_blur_sh);
212 }