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