GP: Improve Blur FX quality in transparency.
[blender.git] / source / blender / draw / engines / eevee / eevee_effects.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_effects.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
32 #include "BLI_string_utils.h"
33
34 #include "eevee_private.h"
35 #include "GPU_texture.h"
36 #include "GPU_extensions.h"
37
38 static struct {
39         /* Downsample Depth */
40         struct GPUShader *minz_downlevel_sh;
41         struct GPUShader *maxz_downlevel_sh;
42         struct GPUShader *minz_downdepth_sh;
43         struct GPUShader *maxz_downdepth_sh;
44         struct GPUShader *minz_downdepth_layer_sh;
45         struct GPUShader *maxz_downdepth_layer_sh;
46         struct GPUShader *maxz_copydepth_layer_sh;
47         struct GPUShader *minz_copydepth_sh;
48         struct GPUShader *maxz_copydepth_sh;
49
50         /* Simple Downsample */
51         struct GPUShader *downsample_sh;
52         struct GPUShader *downsample_cube_sh;
53
54         /* Velocity Resolve */
55         struct GPUShader *velocity_resolve_sh;
56
57         /* Theses are just references, not actually allocated */
58         struct GPUTexture *depth_src;
59         struct GPUTexture *color_src;
60
61         int depth_src_layer;
62         float cube_texel_size;
63 } e_data = {NULL}; /* Engine data */
64
65 extern char datatoc_common_uniforms_lib_glsl[];
66 extern char datatoc_common_view_lib_glsl[];
67 extern char datatoc_bsdf_common_lib_glsl[];
68 extern char datatoc_effect_velocity_resolve_frag_glsl[];
69 extern char datatoc_effect_minmaxz_frag_glsl[];
70 extern char datatoc_effect_downsample_frag_glsl[];
71 extern char datatoc_effect_downsample_cube_frag_glsl[];
72 extern char datatoc_lightprobe_vert_glsl[];
73 extern char datatoc_lightprobe_geom_glsl[];
74
75 static void eevee_create_shader_downsample(void)
76 {
77         char *frag_str = BLI_string_joinN(
78             datatoc_common_uniforms_lib_glsl,
79             datatoc_common_view_lib_glsl,
80             datatoc_bsdf_common_lib_glsl,
81             datatoc_effect_velocity_resolve_frag_glsl);
82
83         e_data.velocity_resolve_sh = DRW_shader_create_fullscreen(frag_str, NULL);
84
85         MEM_freeN(frag_str);
86
87         e_data.downsample_sh = DRW_shader_create_fullscreen(datatoc_effect_downsample_frag_glsl, NULL);
88         e_data.downsample_cube_sh = DRW_shader_create(
89                 datatoc_lightprobe_vert_glsl,
90                 datatoc_lightprobe_geom_glsl,
91                 datatoc_effect_downsample_cube_frag_glsl, NULL);
92
93         e_data.minz_downlevel_sh = DRW_shader_create_fullscreen(
94                 datatoc_effect_minmaxz_frag_glsl,
95                 "#define MIN_PASS\n");
96         e_data.maxz_downlevel_sh = DRW_shader_create_fullscreen(
97                 datatoc_effect_minmaxz_frag_glsl,
98                 "#define MAX_PASS\n");
99         e_data.minz_downdepth_sh = DRW_shader_create_fullscreen(
100                 datatoc_effect_minmaxz_frag_glsl,
101                 "#define MIN_PASS\n");
102         e_data.maxz_downdepth_sh = DRW_shader_create_fullscreen(
103                 datatoc_effect_minmaxz_frag_glsl,
104                 "#define MAX_PASS\n");
105         e_data.minz_downdepth_layer_sh = DRW_shader_create_fullscreen(
106                 datatoc_effect_minmaxz_frag_glsl,
107                 "#define MIN_PASS\n"
108                 "#define LAYERED\n");
109         e_data.maxz_downdepth_layer_sh = DRW_shader_create_fullscreen(
110                 datatoc_effect_minmaxz_frag_glsl,
111                 "#define MAX_PASS\n"
112                 "#define LAYERED\n");
113         e_data.maxz_copydepth_layer_sh = DRW_shader_create_fullscreen(
114                 datatoc_effect_minmaxz_frag_glsl,
115                 "#define MAX_PASS\n"
116                 "#define COPY_DEPTH\n"
117                 "#define LAYERED\n");
118         e_data.minz_copydepth_sh = DRW_shader_create_fullscreen(
119                 datatoc_effect_minmaxz_frag_glsl,
120                 "#define MIN_PASS\n"
121                 "#define COPY_DEPTH\n");
122         e_data.maxz_copydepth_sh = DRW_shader_create_fullscreen(
123                 datatoc_effect_minmaxz_frag_glsl,
124                 "#define MAX_PASS\n"
125                 "#define COPY_DEPTH\n");
126 }
127
128 #define SETUP_BUFFER(tex, fb, fb_color) { \
129         DRW_texture_ensure_fullscreen_2D(&tex, GPU_RGBA16F, DRW_TEX_FILTER | DRW_TEX_MIPMAP); \
130         GPU_framebuffer_ensure_config(&fb, { \
131                 GPU_ATTACHMENT_TEXTURE(dtxl->depth), \
132                 GPU_ATTACHMENT_TEXTURE(tex), \
133         }); \
134         GPU_framebuffer_ensure_config(&fb_color, { \
135                 GPU_ATTACHMENT_NONE, \
136                 GPU_ATTACHMENT_TEXTURE(tex), \
137         }); \
138 }
139
140 #define CLEANUP_BUFFER(tex, fb, fb_color) { \
141         /* Cleanup to release memory */ \
142         DRW_TEXTURE_FREE_SAFE(tex); \
143         GPU_FRAMEBUFFER_FREE_SAFE(fb); \
144         GPU_FRAMEBUFFER_FREE_SAFE(fb_color); \
145 }
146
147 void EEVEE_effects_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata, Object *camera)
148 {
149         EEVEE_CommonUniformBuffer *common_data = &sldata->common_data;
150         EEVEE_StorageList *stl = vedata->stl;
151         EEVEE_FramebufferList *fbl = vedata->fbl;
152         EEVEE_TextureList *txl = vedata->txl;
153         EEVEE_EffectsInfo *effects;
154         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
155         const DRWContextState *draw_ctx = DRW_context_state_get();
156         ViewLayer *view_layer = draw_ctx->view_layer;
157
158         const float *viewport_size = DRW_viewport_size_get();
159         int size_fs[2] = {(int)viewport_size[0], (int)viewport_size[1]};
160
161         /* Shaders */
162         if (!e_data.downsample_sh) {
163                 eevee_create_shader_downsample();
164         }
165
166         if (!stl->effects) {
167                 stl->effects = MEM_callocN(sizeof(EEVEE_EffectsInfo), "EEVEE_EffectsInfo");
168         }
169
170         effects = stl->effects;
171
172         effects->enabled_effects = 0;
173         effects->enabled_effects |= (G.debug_value == 9) ? EFFECT_VELOCITY_BUFFER : 0;
174         effects->enabled_effects |= EEVEE_motion_blur_init(sldata, vedata, camera);
175         effects->enabled_effects |= EEVEE_bloom_init(sldata, vedata);
176         effects->enabled_effects |= EEVEE_depth_of_field_init(sldata, vedata, camera);
177         effects->enabled_effects |= EEVEE_temporal_sampling_init(sldata, vedata);
178         effects->enabled_effects |= EEVEE_occlusion_init(sldata, vedata);
179         effects->enabled_effects |= EEVEE_subsurface_init(sldata, vedata);
180         effects->enabled_effects |= EEVEE_screen_raytrace_init(sldata, vedata);
181         effects->enabled_effects |= EEVEE_volumes_init(sldata, vedata);
182
183         /* Force normal buffer creation. */
184         if (DRW_state_is_image_render() &&
185             (view_layer->passflag & SCE_PASS_NORMAL) != 0)
186         {
187                 effects->enabled_effects |= EFFECT_NORMAL_BUFFER;
188         }
189
190         /**
191          * Ping Pong buffer
192          */
193         if ((effects->enabled_effects & EFFECT_POST_BUFFER) != 0) {
194                 SETUP_BUFFER(txl->color_post, fbl->effect_fb, fbl->effect_color_fb);
195         }
196         else {
197                 CLEANUP_BUFFER(txl->color_post, fbl->effect_fb, fbl->effect_color_fb);
198         }
199
200         /**
201          * MinMax Pyramid
202          */
203         const bool half_res_hiz = true;
204         int size[2], div;
205         common_data->hiz_mip_offset = (half_res_hiz) ? 1 : 0;
206         div = (half_res_hiz) ? 2 : 1;
207         size[0] = max_ii(size_fs[0] / div, 1);
208         size[1] = max_ii(size_fs[1] / div, 1);
209
210         if (GPU_type_matches(GPU_DEVICE_INTEL, GPU_OS_ANY, GPU_DRIVER_ANY)) {
211                 /* Intel gpu seems to have problem rendering to only depth format */
212                 DRW_texture_ensure_2D(&txl->maxzbuffer, size[0], size[1], GPU_R32F, DRW_TEX_MIPMAP);
213         }
214         else {
215                 DRW_texture_ensure_2D(&txl->maxzbuffer, size[0], size[1], GPU_DEPTH_COMPONENT24, DRW_TEX_MIPMAP);
216         }
217
218         if (fbl->downsample_fb == NULL) {
219                 fbl->downsample_fb = GPU_framebuffer_create();
220         }
221
222         /**
223          * Compute Mipmap texel alignment.
224          */
225         for (int i = 0; i < 10; ++i) {
226                 int mip_size[2];
227                 GPU_texture_get_mipmap_size(txl->color, i, mip_size);
228                 common_data->mip_ratio[i][0] = viewport_size[0] / (mip_size[0] * powf(2.0f, i));
229                 common_data->mip_ratio[i][1] = viewport_size[1] / (mip_size[1] * powf(2.0f, i));
230         }
231
232         /**
233          * Normal buffer for deferred passes.
234          */
235         if ((effects->enabled_effects & EFFECT_NORMAL_BUFFER) != 0)     {
236                 effects->ssr_normal_input = DRW_texture_pool_query_2D(size_fs[0], size_fs[1], GPU_RG16,
237                                                                       &draw_engine_eevee_type);
238
239                 GPU_framebuffer_texture_attach(fbl->main_fb, effects->ssr_normal_input, 1, 0);
240         }
241         else {
242                 effects->ssr_normal_input = NULL;
243         }
244
245         /**
246          * Motion vector buffer for correct TAA / motion blur.
247          */
248         if ((effects->enabled_effects & EFFECT_VELOCITY_BUFFER) != 0) {
249                 effects->velocity_tx = DRW_texture_pool_query_2D(size_fs[0], size_fs[1], GPU_RG16,
250                                                                  &draw_engine_eevee_type);
251
252                 /* TODO output objects velocity during the mainpass. */
253                 // GPU_framebuffer_texture_attach(fbl->main_fb, effects->velocity_tx, 1, 0);
254
255                 GPU_framebuffer_ensure_config(&fbl->velocity_resolve_fb, {
256                         GPU_ATTACHMENT_NONE,
257                         GPU_ATTACHMENT_TEXTURE(effects->velocity_tx)
258                 });
259         }
260         else {
261                 effects->velocity_tx = NULL;
262         }
263
264         /**
265          * Setup depth double buffer.
266          */
267         if ((effects->enabled_effects & EFFECT_DEPTH_DOUBLE_BUFFER) != 0) {
268                 DRW_texture_ensure_fullscreen_2D(&txl->depth_double_buffer, GPU_DEPTH24_STENCIL8, 0);
269
270                 GPU_framebuffer_ensure_config(&fbl->double_buffer_depth_fb, {
271                         GPU_ATTACHMENT_TEXTURE(txl->depth_double_buffer)
272                 });
273         }
274         else {
275                 /* Cleanup to release memory */
276                 DRW_TEXTURE_FREE_SAFE(txl->depth_double_buffer);
277                 GPU_FRAMEBUFFER_FREE_SAFE(fbl->double_buffer_depth_fb);
278         }
279
280         /**
281          * Setup double buffer so we can access last frame as it was before post processes.
282          */
283         if ((effects->enabled_effects & EFFECT_DOUBLE_BUFFER) != 0) {
284                 SETUP_BUFFER(txl->color_double_buffer, fbl->double_buffer_fb, fbl->double_buffer_color_fb);
285         }
286         else {
287                 CLEANUP_BUFFER(txl->color_double_buffer, fbl->double_buffer_fb, fbl->double_buffer_color_fb);
288         }
289
290         if ((effects->enabled_effects & (EFFECT_TAA | EFFECT_TAA_REPROJECT)) != 0) {
291                 SETUP_BUFFER(txl->taa_history, fbl->taa_history_fb, fbl->taa_history_color_fb);
292         }
293         else {
294                 CLEANUP_BUFFER(txl->taa_history, fbl->taa_history_fb, fbl->taa_history_color_fb);
295         }
296 }
297
298 void EEVEE_effects_cache_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
299 {
300         EEVEE_PassList *psl = vedata->psl;
301         EEVEE_TextureList *txl = vedata->txl;
302         EEVEE_StorageList *stl = vedata->stl;
303         EEVEE_EffectsInfo *effects = stl->effects;
304         int downsample_write = DRW_STATE_WRITE_DEPTH;
305
306         /* Intel gpu seems to have problem rendering to only depth format.
307          * Use color texture instead. */
308         if (GPU_type_matches(GPU_DEVICE_INTEL, GPU_OS_ANY, GPU_DRIVER_ANY)) {
309                 downsample_write = DRW_STATE_WRITE_COLOR;
310         }
311
312         struct GPUBatch *quad = DRW_cache_fullscreen_quad_get();
313
314         {
315                 psl->color_downsample_ps = DRW_pass_create(
316                         "Downsample", DRW_STATE_WRITE_COLOR);
317                 DRWShadingGroup *grp = DRW_shgroup_create(e_data.downsample_sh, psl->color_downsample_ps);
318                 DRW_shgroup_uniform_texture_ref(grp, "source", &e_data.color_src);
319                 DRW_shgroup_uniform_float(grp, "fireflyFactor", &sldata->common_data.ssr_firefly_fac, 1);
320                 DRW_shgroup_call_add(grp, quad, NULL);
321         }
322
323         {
324                 static int zero = 0;
325                 static uint six = 6;
326                 psl->color_downsample_cube_ps = DRW_pass_create(
327                         "Downsample Cube", DRW_STATE_WRITE_COLOR);
328                 DRWShadingGroup *grp = DRW_shgroup_create(e_data.downsample_cube_sh, psl->color_downsample_cube_ps);
329                 DRW_shgroup_uniform_texture_ref(grp, "source", &e_data.color_src);
330                 DRW_shgroup_uniform_float(grp, "texelSize", &e_data.cube_texel_size, 1);
331                 DRW_shgroup_uniform_int(grp, "Layer", &zero, 1);
332                 DRW_shgroup_call_instances_add(grp, quad, NULL, &six);
333         }
334
335         {
336                 /* Perform min/max downsample */
337                 DRWShadingGroup *grp;
338
339                 psl->maxz_downlevel_ps = DRW_pass_create(
340                         "HiZ Max Down Level", downsample_write | DRW_STATE_DEPTH_ALWAYS);
341                 grp = DRW_shgroup_create(e_data.maxz_downlevel_sh, psl->maxz_downlevel_ps);
342                 DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &txl->maxzbuffer);
343                 DRW_shgroup_call_add(grp, quad, NULL);
344
345                 /* Copy depth buffer to halfres top level of HiZ */
346
347                 psl->maxz_downdepth_ps = DRW_pass_create(
348                         "HiZ Max Copy Depth Halfres", downsample_write | DRW_STATE_DEPTH_ALWAYS);
349                 grp = DRW_shgroup_create(e_data.maxz_downdepth_sh, psl->maxz_downdepth_ps);
350                 DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &e_data.depth_src);
351                 DRW_shgroup_call_add(grp, quad, NULL);
352
353                 psl->maxz_downdepth_layer_ps = DRW_pass_create(
354                         "HiZ Max Copy DepthLayer Halfres", downsample_write | DRW_STATE_DEPTH_ALWAYS);
355                 grp = DRW_shgroup_create(e_data.maxz_downdepth_layer_sh, psl->maxz_downdepth_layer_ps);
356                 DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &e_data.depth_src);
357                 DRW_shgroup_uniform_int(grp, "depthLayer", &e_data.depth_src_layer, 1);
358                 DRW_shgroup_call_add(grp, quad, NULL);
359
360                 psl->maxz_copydepth_ps = DRW_pass_create(
361                         "HiZ Max Copy Depth Fullres", downsample_write | DRW_STATE_DEPTH_ALWAYS);
362                 grp = DRW_shgroup_create(e_data.maxz_copydepth_sh, psl->maxz_copydepth_ps);
363                 DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &e_data.depth_src);
364                 DRW_shgroup_call_add(grp, quad, NULL);
365
366                 psl->maxz_copydepth_layer_ps = DRW_pass_create(
367                         "HiZ Max Copy DepthLayer Halfres", downsample_write | DRW_STATE_DEPTH_ALWAYS);
368                 grp = DRW_shgroup_create(e_data.maxz_copydepth_layer_sh, psl->maxz_copydepth_layer_ps);
369                 DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &e_data.depth_src);
370                 DRW_shgroup_uniform_int(grp, "depthLayer", &e_data.depth_src_layer, 1);
371                 DRW_shgroup_call_add(grp, quad, NULL);
372         }
373
374         if ((effects->enabled_effects & EFFECT_VELOCITY_BUFFER) != 0) {
375                 /* This pass compute camera motions to the non moving objects. */
376                 psl->velocity_resolve = DRW_pass_create(
377                         "Velocity Resolve", DRW_STATE_WRITE_COLOR);
378                 DRWShadingGroup *grp = DRW_shgroup_create(e_data.velocity_resolve_sh, psl->velocity_resolve);
379                 DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &e_data.depth_src);
380                 DRW_shgroup_uniform_block(grp, "common_block", sldata->common_ubo);
381                 DRW_shgroup_uniform_mat4(grp, "currPersinv", effects->velocity_curr_persinv);
382                 DRW_shgroup_uniform_mat4(grp, "pastPersmat", effects->velocity_past_persmat);
383                 DRW_shgroup_call_add(grp, quad, NULL);
384         }
385 }
386
387 #if 0 /* Not required for now */
388 static void min_downsample_cb(void *vedata, int UNUSED(level))
389 {
390         EEVEE_PassList *psl = ((EEVEE_Data *)vedata)->psl;
391         DRW_draw_pass(psl->minz_downlevel_ps);
392 }
393 #endif
394
395 static void max_downsample_cb(void *vedata, int UNUSED(level))
396 {
397         EEVEE_PassList *psl = ((EEVEE_Data *)vedata)->psl;
398         DRW_draw_pass(psl->maxz_downlevel_ps);
399 }
400
401 static void simple_downsample_cb(void *vedata, int UNUSED(level))
402 {
403         EEVEE_PassList *psl = ((EEVEE_Data *)vedata)->psl;
404         DRW_draw_pass(psl->color_downsample_ps);
405 }
406
407 static void simple_downsample_cube_cb(void *vedata, int level)
408 {
409         EEVEE_PassList *psl = ((EEVEE_Data *)vedata)->psl;
410         e_data.cube_texel_size = (float)(1 << level) / (float)GPU_texture_width(e_data.color_src);
411         DRW_draw_pass(psl->color_downsample_cube_ps);
412 }
413
414 void EEVEE_create_minmax_buffer(EEVEE_Data *vedata, GPUTexture *depth_src, int layer)
415 {
416         EEVEE_PassList *psl = vedata->psl;
417         EEVEE_FramebufferList *fbl = vedata->fbl;
418         EEVEE_TextureList *txl = vedata->txl;
419
420         e_data.depth_src = depth_src;
421         e_data.depth_src_layer = layer;
422
423 #if 0 /* Not required for now */
424         DRW_stats_group_start("Min buffer");
425         /* Copy depth buffer to min texture top level */
426         GPU_framebuffer_texture_attach(fbl->downsample_fb, stl->g_data->minzbuffer, 0, 0);
427         GPU_framebuffer_bind(fbl->downsample_fb);
428         if (layer >= 0) {
429                 DRW_draw_pass(psl->minz_downdepth_layer_ps);
430         }
431         else {
432                 DRW_draw_pass(psl->minz_downdepth_ps);
433         }
434         GPU_framebuffer_texture_detach(stl->g_data->minzbuffer);
435
436         /* Create lower levels */
437         GPU_framebuffer_recursive_downsample(fbl->downsample_fb, stl->g_data->minzbuffer, 8, &min_downsample_cb, vedata);
438         DRW_stats_group_end();
439 #endif
440         int minmax_size[3], depth_size[3];
441         GPU_texture_get_mipmap_size(depth_src, 0, depth_size);
442         GPU_texture_get_mipmap_size(txl->maxzbuffer, 0, minmax_size);
443         bool is_full_res_minmaxz = (minmax_size[0] == depth_size[0] && minmax_size[1] == depth_size[1]);
444
445         DRW_stats_group_start("Max buffer");
446         /* Copy depth buffer to max texture top level */
447         GPU_framebuffer_texture_attach(fbl->downsample_fb, txl->maxzbuffer, 0, 0);
448         GPU_framebuffer_bind(fbl->downsample_fb);
449         if (layer >= 0) {
450                 if (is_full_res_minmaxz) {
451                         DRW_draw_pass(psl->maxz_copydepth_layer_ps);
452                 }
453                 else {
454                         DRW_draw_pass(psl->maxz_downdepth_layer_ps);
455                 }
456         }
457         else {
458                 if (is_full_res_minmaxz) {
459                         DRW_draw_pass(psl->maxz_copydepth_ps);
460                 }
461                 else {
462                         DRW_draw_pass(psl->maxz_downdepth_ps);
463                 }
464         }
465
466         /* Create lower levels */
467         GPU_framebuffer_recursive_downsample(fbl->downsample_fb, 8, &max_downsample_cb, vedata);
468         GPU_framebuffer_texture_detach(fbl->downsample_fb, txl->maxzbuffer);
469         DRW_stats_group_end();
470
471         /* Restore */
472         GPU_framebuffer_bind(fbl->main_fb);
473 }
474
475 /**
476  * Simple downsampling algorithm. Reconstruct mip chain up to mip level.
477  **/
478 void EEVEE_downsample_buffer(EEVEE_Data *vedata, GPUTexture *texture_src, int level)
479 {
480         EEVEE_FramebufferList *fbl = vedata->fbl;
481         e_data.color_src = texture_src;
482
483         /* Create lower levels */
484         DRW_stats_group_start("Downsample buffer");
485         GPU_framebuffer_texture_attach(fbl->downsample_fb, texture_src, 0, 0);
486         GPU_framebuffer_recursive_downsample(fbl->downsample_fb, level, &simple_downsample_cb, vedata);
487         GPU_framebuffer_texture_detach(fbl->downsample_fb, texture_src);
488         DRW_stats_group_end();
489 }
490
491 /**
492  * Simple downsampling algorithm for cubemap. Reconstruct mip chain up to mip level.
493  **/
494 void EEVEE_downsample_cube_buffer(EEVEE_Data *vedata, GPUTexture *texture_src, int level)
495 {
496         EEVEE_FramebufferList *fbl = vedata->fbl;
497         e_data.color_src = texture_src;
498
499         /* Create lower levels */
500         DRW_stats_group_start("Downsample Cube buffer");
501         GPU_framebuffer_texture_attach(fbl->downsample_fb, texture_src, 0, 0);
502         GPU_framebuffer_recursive_downsample(fbl->downsample_fb, level, &simple_downsample_cube_cb, vedata);
503         GPU_framebuffer_texture_detach(fbl->downsample_fb, texture_src);
504         DRW_stats_group_end();
505 }
506
507 void EEVEE_draw_effects(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata)
508 {
509         EEVEE_PassList *psl = vedata->psl;
510         EEVEE_TextureList *txl = vedata->txl;
511         EEVEE_FramebufferList *fbl = vedata->fbl;
512         EEVEE_StorageList *stl = vedata->stl;
513         EEVEE_EffectsInfo *effects = stl->effects;
514
515         /* First resolve the velocity. */
516         if ((effects->enabled_effects & EFFECT_VELOCITY_BUFFER) != 0) {
517                 DRW_viewport_matrix_get(effects->velocity_curr_persinv, DRW_MAT_PERSINV);
518
519                 GPU_framebuffer_bind(fbl->velocity_resolve_fb);
520                 DRW_draw_pass(psl->velocity_resolve);
521         }
522         DRW_viewport_matrix_get(effects->velocity_past_persmat, DRW_MAT_PERS);
523
524         /* only once per frame after the first post process */
525         effects->swap_double_buffer = ((effects->enabled_effects & EFFECT_DOUBLE_BUFFER) != 0);
526
527         /* Init pointers */
528         effects->source_buffer = txl->color; /* latest updated texture */
529         effects->target_buffer = fbl->effect_color_fb; /* next target to render to */
530
531         /* Post process stack (order matters) */
532         EEVEE_motion_blur_draw(vedata);
533         EEVEE_depth_of_field_draw(vedata);
534         EEVEE_temporal_sampling_draw(vedata);
535         EEVEE_bloom_draw(vedata);
536
537         /* Save the final texture and framebuffer for final transformation or read. */
538         effects->final_tx = effects->source_buffer;
539         effects->final_fb = (effects->target_buffer != fbl->main_color_fb) ? fbl->main_fb : fbl->effect_fb;
540         if ((effects->enabled_effects & EFFECT_TAA) &&
541             (effects->source_buffer == txl->taa_history))
542         {
543                 effects->final_fb = fbl->taa_history_fb;
544         }
545
546         /* If no post processes is enabled, buffers are still not swapped, do it now. */
547         SWAP_DOUBLE_BUFFERS();
548
549         if (!stl->g_data->valid_double_buffer &&
550             ((effects->enabled_effects & EFFECT_DOUBLE_BUFFER) != 0) &&
551             (DRW_state_is_image_render() == false))
552         {
553                 /* If history buffer is not valid request another frame.
554                  * This fix black reflections on area resize. */
555                 DRW_viewport_request_redraw();
556         }
557
558         /* Record pers matrix for the next frame. */
559         DRW_viewport_matrix_get(stl->effects->prev_persmat, DRW_MAT_PERS);
560
561         /* Update double buffer status if render mode. */
562         if (DRW_state_is_image_render()) {
563                 stl->g_data->valid_double_buffer = (txl->color_double_buffer != NULL);
564                 stl->g_data->valid_taa_history = (txl->taa_history != NULL);
565         }
566 }
567
568 void EEVEE_effects_free(void)
569 {
570         DRW_SHADER_FREE_SAFE(e_data.velocity_resolve_sh);
571
572         DRW_SHADER_FREE_SAFE(e_data.downsample_sh);
573         DRW_SHADER_FREE_SAFE(e_data.downsample_cube_sh);
574
575         DRW_SHADER_FREE_SAFE(e_data.minz_downlevel_sh);
576         DRW_SHADER_FREE_SAFE(e_data.maxz_downlevel_sh);
577         DRW_SHADER_FREE_SAFE(e_data.minz_downdepth_sh);
578         DRW_SHADER_FREE_SAFE(e_data.maxz_downdepth_sh);
579         DRW_SHADER_FREE_SAFE(e_data.minz_downdepth_layer_sh);
580         DRW_SHADER_FREE_SAFE(e_data.maxz_downdepth_layer_sh);
581         DRW_SHADER_FREE_SAFE(e_data.maxz_copydepth_layer_sh);
582         DRW_SHADER_FREE_SAFE(e_data.minz_copydepth_sh);
583         DRW_SHADER_FREE_SAFE(e_data.maxz_copydepth_sh);
584 }