GP: Improve Blur FX quality in transparency.
[blender.git] / source / blender / draw / engines / eevee / eevee_render.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_render.c
23  *  \ingroup draw_engine
24  */
25
26 /**
27  * Render functions for final render outputs.
28  */
29
30 #include "DRW_engine.h"
31 #include "DRW_render.h"
32
33 #include "DNA_node_types.h"
34 #include "DNA_object_types.h"
35
36 #include "BLI_rand.h"
37 #include "BLI_rect.h"
38
39 #include "DEG_depsgraph_query.h"
40
41 #include "GPU_framebuffer.h"
42 #include "GPU_glew.h"
43
44 #include "RE_pipeline.h"
45
46 #include "eevee_private.h"
47
48 void EEVEE_render_init(EEVEE_Data *ved, RenderEngine *engine, struct Depsgraph *depsgraph)
49 {
50         EEVEE_Data *vedata = (EEVEE_Data *)ved;
51         EEVEE_StorageList *stl = vedata->stl;
52         EEVEE_TextureList *txl = vedata->txl;
53         EEVEE_FramebufferList *fbl = vedata->fbl;
54         EEVEE_ViewLayerData *sldata = EEVEE_view_layer_data_ensure();
55         Scene *scene = DEG_get_evaluated_scene(depsgraph);
56
57         /* Init default FB and render targets:
58          * In render mode the default framebuffer is not generated
59          * because there is no viewport. So we need to manually create it or
60          * not use it. For code clarity we just allocate it make use of it. */
61         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
62         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
63
64         /* TODO 32 bit depth */
65         DRW_texture_ensure_fullscreen_2D(&dtxl->depth, GPU_DEPTH24_STENCIL8, 0);
66         DRW_texture_ensure_fullscreen_2D(&txl->color, GPU_RGBA32F, DRW_TEX_FILTER | DRW_TEX_MIPMAP);
67
68         GPU_framebuffer_ensure_config(&dfbl->default_fb, {
69                 GPU_ATTACHMENT_TEXTURE(dtxl->depth),
70                 GPU_ATTACHMENT_TEXTURE(txl->color)
71         });
72         GPU_framebuffer_ensure_config(&fbl->main_fb, {
73                 GPU_ATTACHMENT_TEXTURE(dtxl->depth),
74                 GPU_ATTACHMENT_TEXTURE(txl->color)
75         });
76         GPU_framebuffer_ensure_config(&fbl->main_color_fb, {
77                 GPU_ATTACHMENT_NONE,
78                 GPU_ATTACHMENT_TEXTURE(txl->color)
79         });
80
81         /* Alloc transient data. */
82         if (!stl->g_data) {
83                 stl->g_data = MEM_callocN(sizeof(*stl->g_data), __func__);
84         }
85         EEVEE_PrivateData *g_data = stl->g_data;
86         g_data->background_alpha = DRW_state_draw_background() ? 1.0f : 0.0f;
87         g_data->valid_double_buffer = 0;
88
89         /* Alloc common ubo data. */
90         if (sldata->common_ubo == NULL) {
91                 sldata->common_ubo = DRW_uniformbuffer_create(sizeof(sldata->common_data), &sldata->common_data);
92         }
93         if (sldata->clip_ubo == NULL) {
94                 sldata->clip_ubo = DRW_uniformbuffer_create(sizeof(sldata->clip_data), &sldata->clip_data);
95         }
96
97         /* Set the pers & view matrix. */
98         /* TODO(sergey): Shall render hold pointer to an evaluated camera instead? */
99         struct Object *ob_camera_eval = DEG_get_evaluated_object(depsgraph, RE_GetCamera(engine->re));
100         float frame = BKE_scene_frame_get(scene);
101         RE_GetCameraWindow(engine->re, ob_camera_eval, frame, g_data->winmat);
102         RE_GetCameraModelMatrix(engine->re, ob_camera_eval, g_data->viewinv);
103
104         invert_m4_m4(g_data->viewmat, g_data->viewinv);
105         mul_m4_m4m4(g_data->persmat, g_data->winmat, g_data->viewmat);
106         invert_m4_m4(g_data->persinv, g_data->persmat);
107         invert_m4_m4(g_data->wininv, g_data->winmat);
108
109         DRW_viewport_matrix_override_set(g_data->persmat, DRW_MAT_PERS);
110         DRW_viewport_matrix_override_set(g_data->persinv, DRW_MAT_PERSINV);
111         DRW_viewport_matrix_override_set(g_data->winmat, DRW_MAT_WIN);
112         DRW_viewport_matrix_override_set(g_data->wininv, DRW_MAT_WININV);
113         DRW_viewport_matrix_override_set(g_data->viewmat, DRW_MAT_VIEW);
114         DRW_viewport_matrix_override_set(g_data->viewinv, DRW_MAT_VIEWINV);
115
116         /* EEVEE_effects_init needs to go first for TAA */
117         EEVEE_effects_init(sldata, vedata, ob_camera_eval);
118         EEVEE_materials_init(sldata, stl, fbl);
119         EEVEE_lights_init(sldata);
120         EEVEE_lightprobes_init(sldata, vedata);
121
122         /* INIT CACHE */
123         EEVEE_bloom_cache_init(sldata, vedata);
124         EEVEE_depth_of_field_cache_init(sldata, vedata);
125         EEVEE_effects_cache_init(sldata, vedata);
126         EEVEE_lightprobes_cache_init(sldata, vedata);
127         EEVEE_lights_cache_init(sldata, vedata);
128         EEVEE_materials_cache_init(sldata, vedata);
129         EEVEE_motion_blur_cache_init(sldata, vedata);
130         EEVEE_occlusion_cache_init(sldata, vedata);
131         EEVEE_screen_raytrace_cache_init(sldata, vedata);
132         EEVEE_subsurface_cache_init(sldata, vedata);
133         EEVEE_temporal_sampling_cache_init(sldata, vedata);
134         EEVEE_volumes_cache_init(sldata, vedata);
135 }
136
137 /* Used by light cache. in this case engine is NULL. */
138 void EEVEE_render_cache(
139         void *vedata, struct Object *ob,
140         struct RenderEngine *engine, struct Depsgraph *UNUSED(depsgraph))
141 {
142         EEVEE_ViewLayerData *sldata = EEVEE_view_layer_data_ensure();
143         EEVEE_LightProbesInfo *pinfo = sldata->probes;
144         bool cast_shadow = false;
145
146         if (pinfo->vis_data.collection) {
147                 /* Used for rendering probe with visibility groups. */
148                 bool ob_vis = BKE_collection_has_object_recursive(pinfo->vis_data.collection, ob);
149                 ob_vis = (pinfo->vis_data.invert) ? !ob_vis : ob_vis;
150
151                 if (!ob_vis) {
152                         return;
153                 }
154         }
155
156         if (engine) {
157                 char info[42];
158                 BLI_snprintf(info, sizeof(info), "Syncing %s", ob->id.name + 2);
159                 RE_engine_update_stats(engine, NULL, info);
160         }
161
162         if (ob->base_flag & BASE_VISIBLE) {
163                 EEVEE_hair_cache_populate(vedata, sldata, ob, &cast_shadow);
164         }
165
166         if (DRW_object_is_visible_in_active_context(ob)) {
167                 if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL)) {
168                         EEVEE_materials_cache_populate(vedata, sldata, ob, &cast_shadow);
169                 }
170                 else if (ob->type == OB_LIGHTPROBE) {
171                         EEVEE_lightprobes_cache_add(sldata, vedata, ob);
172                 }
173                 else if (ob->type == OB_LAMP) {
174                         EEVEE_lights_cache_add(sldata, ob);
175                 }
176         }
177
178         if (cast_shadow) {
179                 EEVEE_lights_cache_shcaster_object_add(sldata, ob);
180         }
181 }
182
183 static void eevee_render_result_combined(
184         RenderLayer *rl, const char *viewname, const rcti *rect,
185         EEVEE_Data *vedata, EEVEE_ViewLayerData *UNUSED(sldata))
186 {
187         RenderPass *rp = RE_pass_find_by_name(rl, RE_PASSNAME_COMBINED, viewname);
188
189         GPU_framebuffer_bind(vedata->stl->effects->final_fb);
190         GPU_framebuffer_read_color(vedata->stl->effects->final_fb,
191                                    rect->xmin, rect->ymin,
192                                    BLI_rcti_size_x(rect), BLI_rcti_size_y(rect),
193                                    4, 0, rp->rect);
194
195         /* Premult alpha */
196         int pixels_len = BLI_rcti_size_x(rect) * BLI_rcti_size_y(rect);
197         for (int i = 0; i < pixels_len * 4; i += 4) {
198                 mul_v3_fl(rp->rect + i, rp->rect[i + 3]);
199         }
200 }
201
202 static void eevee_render_result_subsurface(
203         RenderLayer *rl, const char *viewname, const rcti *rect,
204         EEVEE_Data *vedata, EEVEE_ViewLayerData *UNUSED(sldata), int render_samples)
205 {
206         const DRWContextState *draw_ctx = DRW_context_state_get();
207         ViewLayer *view_layer = draw_ctx->view_layer;
208
209         if (vedata->fbl->sss_accum_fb == NULL) {
210                 /* SSS is not enabled. */
211                 return;
212         }
213
214         if ((view_layer->passflag & SCE_PASS_SUBSURFACE_COLOR) != 0) {
215                 RenderPass *rp = RE_pass_find_by_name(rl, RE_PASSNAME_SUBSURFACE_COLOR, viewname);
216
217                 GPU_framebuffer_bind(vedata->fbl->sss_accum_fb);
218                 GPU_framebuffer_read_color(vedata->fbl->sss_accum_fb,
219                                            rect->xmin, rect->ymin,
220                                            BLI_rcti_size_x(rect), BLI_rcti_size_y(rect),
221                                            3, 1, rp->rect);
222
223                 /* This is the accumulated color. Divide by the number of samples. */
224                 for (int i = 0; i < rp->rectx * rp->recty * 3; i++) {
225                         rp->rect[i] /= (float)render_samples;
226                 }
227         }
228
229         if ((view_layer->passflag & SCE_PASS_SUBSURFACE_DIRECT) != 0) {
230                 RenderPass *rp = RE_pass_find_by_name(rl, RE_PASSNAME_SUBSURFACE_DIRECT, viewname);
231
232                 GPU_framebuffer_bind(vedata->fbl->sss_accum_fb);
233                 GPU_framebuffer_read_color(vedata->fbl->sss_accum_fb,
234                                            rect->xmin, rect->ymin,
235                                            BLI_rcti_size_x(rect), BLI_rcti_size_y(rect),
236                                            3, 0, rp->rect);
237
238                 /* This is the accumulated color. Divide by the number of samples. */
239                 for (int i = 0; i < rp->rectx * rp->recty * 3; i++) {
240                         rp->rect[i] /= (float)render_samples;
241                 }
242         }
243
244         if ((view_layer->passflag & SCE_PASS_SUBSURFACE_INDIRECT) != 0) {
245                 /* Do nothing as all the lighting is in the direct pass.
246                  * TODO : Separate Direct from indirect lighting. */
247         }
248 }
249
250 static void eevee_render_result_normal(
251         RenderLayer *rl, const char *viewname, const rcti *rect,
252         EEVEE_Data *vedata, EEVEE_ViewLayerData *UNUSED(sldata))
253 {
254         const DRWContextState *draw_ctx = DRW_context_state_get();
255         ViewLayer *view_layer = draw_ctx->view_layer;
256         EEVEE_StorageList *stl = vedata->stl;
257         EEVEE_PrivateData *g_data = stl->g_data;
258
259         /* Only read the center texel. */
260         if (stl->effects->taa_current_sample > 1)
261                 return;
262
263         if ((view_layer->passflag & SCE_PASS_NORMAL) != 0) {
264                 RenderPass *rp = RE_pass_find_by_name(rl, RE_PASSNAME_NORMAL, viewname);
265
266                 GPU_framebuffer_bind(vedata->fbl->main_fb);
267                 GPU_framebuffer_read_color(vedata->fbl->main_fb,
268                                            rect->xmin, rect->ymin,
269                                            BLI_rcti_size_x(rect), BLI_rcti_size_y(rect),
270                                            3, 1, rp->rect);
271
272                 /* Convert Eevee encoded normals to Blender normals. */
273                 for (int i = 0; i < rp->rectx * rp->recty * 3; i += 3) {
274                         if (rp->rect[i] == 0.0f && rp->rect[i + 1] == 0.0f) {
275                                 /* If normal is not correct then do not produce NANs.  */
276                                 continue;
277                         }
278
279                         float fenc[2];
280                         fenc[0] = rp->rect[i + 0] * 4.0f - 2.0f;
281                         fenc[1] = rp->rect[i + 1] * 4.0f - 2.0f;
282
283                         float f = dot_v2v2(fenc, fenc);
284                         float g = sqrtf(1.0f - f / 4.0f);
285
286                         rp->rect[i + 0] = fenc[0] * g;
287                         rp->rect[i + 1] = fenc[1] * g;
288                         rp->rect[i + 2] = 1.0f - f / 2.0f;
289
290                         mul_mat3_m4_v3(g_data->viewinv, &rp->rect[i]);
291                 }
292         }
293 }
294
295 static void eevee_render_result_z(
296         RenderLayer *rl, const char *viewname, const rcti *rect,
297         EEVEE_Data *vedata, EEVEE_ViewLayerData *sldata)
298 {
299         const DRWContextState *draw_ctx = DRW_context_state_get();
300         ViewLayer *view_layer = draw_ctx->view_layer;
301         EEVEE_CommonUniformBuffer *common_data = &sldata->common_data;
302         EEVEE_StorageList *stl = vedata->stl;
303         EEVEE_PrivateData *g_data = stl->g_data;
304
305         /* Only read the center texel. */
306         if (stl->effects->taa_current_sample > 1)
307                 return;
308
309         if ((view_layer->passflag & SCE_PASS_Z) != 0) {
310                 RenderPass *rp = RE_pass_find_by_name(rl, RE_PASSNAME_Z, viewname);
311
312                 GPU_framebuffer_bind(vedata->fbl->main_fb);
313                 GPU_framebuffer_read_depth(vedata->fbl->main_fb,
314                                            rect->xmin, rect->ymin,
315                                            BLI_rcti_size_x(rect), BLI_rcti_size_y(rect),
316                                            rp->rect);
317
318                 bool is_persp = DRW_viewport_is_persp_get();
319
320                 /* Convert ogl depth [0..1] to view Z [near..far] */
321                 for (int i = 0; i < rp->rectx * rp->recty; ++i) {
322                         if (rp->rect[i] == 1.0f ) {
323                                 rp->rect[i] = 1e10f; /* Background */
324                         }
325                         else {
326                                 if (is_persp) {
327                                         rp->rect[i] = rp->rect[i] * 2.0f - 1.0f;
328                                         rp->rect[i] = g_data->winmat[3][2] / (rp->rect[i] + g_data->winmat[2][2]);
329                                 }
330                                 else {
331                                         rp->rect[i] = -common_data->view_vecs[0][2] + rp->rect[i] * -common_data->view_vecs[1][2];
332                                 }
333                         }
334                 }
335         }
336 }
337
338 static void eevee_render_result_mist(
339         RenderLayer *rl, const char *viewname, const rcti *rect,
340         EEVEE_Data *vedata, EEVEE_ViewLayerData *UNUSED(sldata), int render_samples)
341 {
342         const DRWContextState *draw_ctx = DRW_context_state_get();
343         ViewLayer *view_layer = draw_ctx->view_layer;
344
345         if ((view_layer->passflag & SCE_PASS_MIST) != 0) {
346                 RenderPass *rp = RE_pass_find_by_name(rl, RE_PASSNAME_MIST, viewname);
347
348                 GPU_framebuffer_bind(vedata->fbl->mist_accum_fb);
349                 GPU_framebuffer_read_color(vedata->fbl->mist_accum_fb,
350                                            rect->xmin, rect->ymin,
351                                            BLI_rcti_size_x(rect), BLI_rcti_size_y(rect),
352                                            1, 0, rp->rect);
353
354                 /* This is the accumulated color. Divide by the number of samples. */
355                 for (int i = 0; i < rp->rectx * rp->recty; i++) {
356                         rp->rect[i] /= (float)render_samples;
357                 }
358         }
359 }
360
361 static void eevee_render_result_occlusion(
362         RenderLayer *rl, const char *viewname, const rcti *rect,
363         EEVEE_Data *vedata, EEVEE_ViewLayerData *UNUSED(sldata), int render_samples)
364 {
365         const DRWContextState *draw_ctx = DRW_context_state_get();
366         ViewLayer *view_layer = draw_ctx->view_layer;
367
368         if (vedata->fbl->ao_accum_fb == NULL) {
369                 /* AO is not enabled. */
370                 return;
371         }
372
373         if ((view_layer->passflag & SCE_PASS_AO) != 0) {
374                 RenderPass *rp = RE_pass_find_by_name(rl, RE_PASSNAME_AO, viewname);
375
376                 GPU_framebuffer_bind(vedata->fbl->ao_accum_fb);
377                 GPU_framebuffer_read_color(vedata->fbl->ao_accum_fb,
378                                            rect->xmin, rect->ymin,
379                                            BLI_rcti_size_x(rect), BLI_rcti_size_y(rect),
380                                            3, 0, rp->rect);
381
382                 /* This is the accumulated color. Divide by the number of samples. */
383                 for (int i = 0; i < rp->rectx * rp->recty * 3; i += 3) {
384                         rp->rect[i] = rp->rect[i + 1] = rp->rect[i + 2] = min_ff(1.0f, rp->rect[i] / (float)render_samples);
385                 }
386         }
387 }
388
389 static void eevee_render_draw_background(EEVEE_Data *vedata)
390 {
391         EEVEE_FramebufferList *fbl = vedata->fbl;
392         EEVEE_StorageList *stl = vedata->stl;
393         EEVEE_PassList *psl = vedata->psl;
394
395         /* Prevent background to write to data buffers.
396          * NOTE : This also make sure the textures are bound
397          *        to the right double buffer. */
398         GPU_framebuffer_ensure_config(&fbl->main_fb, {
399                 GPU_ATTACHMENT_LEAVE,
400                 GPU_ATTACHMENT_LEAVE,
401                 GPU_ATTACHMENT_NONE,
402                 GPU_ATTACHMENT_NONE,
403                 GPU_ATTACHMENT_NONE,
404                 GPU_ATTACHMENT_NONE
405         });
406         GPU_framebuffer_bind(fbl->main_fb);
407
408         DRW_draw_pass(psl->background_pass);
409
410         GPU_framebuffer_ensure_config(&fbl->main_fb, {
411                 GPU_ATTACHMENT_LEAVE,
412                 GPU_ATTACHMENT_LEAVE,
413                 GPU_ATTACHMENT_TEXTURE(stl->effects->ssr_normal_input),
414                 GPU_ATTACHMENT_TEXTURE(stl->effects->ssr_specrough_input),
415                 GPU_ATTACHMENT_TEXTURE(stl->effects->sss_data),
416                 GPU_ATTACHMENT_TEXTURE(stl->effects->sss_albedo)
417         });
418         GPU_framebuffer_bind(fbl->main_fb);
419 }
420
421 void EEVEE_render_draw(EEVEE_Data *vedata, RenderEngine *engine, RenderLayer *rl, const rcti *rect)
422 {
423         const DRWContextState *draw_ctx = DRW_context_state_get();
424         const Scene *scene_eval = DEG_get_evaluated_scene(draw_ctx->depsgraph);
425         ViewLayer *view_layer = draw_ctx->view_layer;
426         const char *viewname = RE_GetActiveRenderView(engine->re);
427         EEVEE_PassList *psl = vedata->psl;
428         EEVEE_StorageList *stl = vedata->stl;
429         EEVEE_FramebufferList *fbl = vedata->fbl;
430         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
431         EEVEE_ViewLayerData *sldata = EEVEE_view_layer_data_ensure();
432         EEVEE_PrivateData *g_data = stl->g_data;
433
434         /* FINISH CACHE */
435         EEVEE_materials_cache_finish(vedata);
436         EEVEE_lights_cache_finish(sldata);
437         EEVEE_lightprobes_cache_finish(sldata, vedata);
438
439         /* Sort transparents before the loop. */
440         DRW_pass_sort_shgroup_z(psl->transparent_pass);
441
442         /* Push instances attribs to the GPU. */
443         DRW_render_instance_buffer_finish();
444
445         /* Need to be called after DRW_render_instance_buffer_finish() */
446         /* Also we weed to have a correct fbo bound for DRW_hair_update */
447         GPU_framebuffer_bind(fbl->main_fb);
448         DRW_hair_update();
449
450         if ((view_layer->passflag & (SCE_PASS_SUBSURFACE_COLOR |
451                                      SCE_PASS_SUBSURFACE_DIRECT |
452                                      SCE_PASS_SUBSURFACE_INDIRECT)) != 0)
453         {
454                 EEVEE_subsurface_output_init(sldata, vedata);
455         }
456
457         if ((view_layer->passflag & SCE_PASS_MIST) != 0) {
458                 EEVEE_mist_output_init(sldata, vedata);
459         }
460
461         if ((view_layer->passflag & SCE_PASS_AO) != 0) {
462                 EEVEE_occlusion_output_init(sldata, vedata);
463         }
464
465         uint tot_sample = scene_eval->eevee.taa_render_samples;
466         uint render_samples = 0;
467
468         if (RE_engine_test_break(engine)) {
469                 return;
470         }
471
472         while (render_samples < tot_sample && !RE_engine_test_break(engine)) {
473                 float clear_col[4] = {0.0f, 0.0f, 0.0f, 0.0f};
474                 float clear_depth = 1.0f;
475                 uint clear_stencil = 0x00;
476                 uint primes[3] = {2, 3, 7};
477                 double offset[3] = {0.0, 0.0, 0.0};
478                 double r[3];
479
480                 /* Restore winmat before jittering again. */
481                 copy_m4_m4(stl->effects->overide_winmat, g_data->winmat);
482                 /* Copy previous persmat to UBO data */
483                 copy_m4_m4(sldata->common_data.prev_persmat, stl->effects->prev_persmat);
484
485                 BLI_halton_3D(primes, offset, stl->effects->taa_current_sample, r);
486                 EEVEE_update_noise(psl, fbl, r);
487                 EEVEE_temporal_sampling_matrices_calc(stl->effects, g_data->viewmat, g_data->persmat, r);
488                 EEVEE_volumes_set_jitter(sldata, stl->effects->taa_current_sample - 1);
489                 EEVEE_materials_init(sldata, stl, fbl);
490
491                 /* Set matrices. */
492                 DRW_viewport_matrix_override_set(stl->effects->overide_persmat, DRW_MAT_PERS);
493                 DRW_viewport_matrix_override_set(stl->effects->overide_persinv, DRW_MAT_PERSINV);
494                 DRW_viewport_matrix_override_set(stl->effects->overide_winmat, DRW_MAT_WIN);
495                 DRW_viewport_matrix_override_set(stl->effects->overide_wininv, DRW_MAT_WININV);
496                 DRW_viewport_matrix_override_set(g_data->viewmat, DRW_MAT_VIEW);
497                 DRW_viewport_matrix_override_set(g_data->viewinv, DRW_MAT_VIEWINV);
498
499                 /* Refresh Probes */
500                 RE_engine_update_stats(engine, NULL, "Updating Probes");
501                 EEVEE_lightprobes_refresh(sldata, vedata);
502                 EEVEE_lightprobes_refresh_planar(sldata, vedata);
503                 DRW_uniformbuffer_update(sldata->common_ubo, &sldata->common_data);
504
505                 char info[42];
506                 BLI_snprintf(info, sizeof(info), "Rendering %u / %u samples", render_samples + 1, tot_sample);
507                 RE_engine_update_stats(engine, NULL, info);
508
509                 /* Refresh Shadows */
510                 EEVEE_draw_shadows(sldata, psl);
511
512                 GPU_framebuffer_bind(fbl->main_fb);
513                 GPU_framebuffer_clear_color_depth_stencil(fbl->main_fb, clear_col, clear_depth, clear_stencil);
514                 /* Depth prepass */
515                 DRW_draw_pass(psl->depth_pass);
516                 DRW_draw_pass(psl->depth_pass_cull);
517                 /* Create minmax texture */
518                 EEVEE_create_minmax_buffer(vedata, dtxl->depth, -1);
519                 EEVEE_occlusion_compute(sldata, vedata, dtxl->depth, -1);
520                 EEVEE_volumes_compute(sldata, vedata);
521                 /* Shading pass */
522                 eevee_render_draw_background(vedata);
523                 GPU_framebuffer_bind(fbl->main_fb);
524                 EEVEE_draw_default_passes(psl);
525                 DRW_draw_pass(psl->material_pass);
526                 EEVEE_subsurface_data_render(sldata, vedata);
527                 /* Effects pre-transparency */
528                 EEVEE_subsurface_compute(sldata, vedata);
529                 EEVEE_reflection_compute(sldata, vedata);
530                 EEVEE_refraction_compute(sldata, vedata);
531                 /* Opaque refraction */
532                 DRW_draw_pass(psl->refract_depth_pass);
533                 DRW_draw_pass(psl->refract_depth_pass_cull);
534                 DRW_draw_pass(psl->refract_pass);
535                 /* Subsurface output */
536                 EEVEE_subsurface_output_accumulate(sldata, vedata);
537                 /* Occlusion output */
538                 EEVEE_occlusion_output_accumulate(sldata, vedata);
539                 /* Result NORMAL */
540                 eevee_render_result_normal(rl, viewname, rect, vedata, sldata);
541                 /* Volumetrics Resolve Opaque */
542                 EEVEE_volumes_resolve(sldata, vedata);
543                 /* Mist output */
544                 EEVEE_mist_output_accumulate(sldata, vedata);
545                 /* Transparent */
546                 DRW_draw_pass(psl->transparent_pass);
547                 /* Result Z */
548                 eevee_render_result_z(rl, viewname, rect, vedata, sldata);
549                 /* Post Process */
550                 EEVEE_draw_effects(sldata, vedata);
551
552                 /* XXX Seems to fix TDR issue with NVidia drivers on linux. */
553                 glFinish();
554
555                 RE_engine_update_progress(engine, (float)(render_samples++) / (float)tot_sample);
556         }
557
558         eevee_render_result_combined(rl, viewname, rect, vedata, sldata);
559         eevee_render_result_subsurface(rl, viewname, rect, vedata, sldata, render_samples);
560         eevee_render_result_mist(rl, viewname, rect, vedata, sldata, render_samples);
561         eevee_render_result_occlusion(rl, viewname, rect, vedata, sldata, render_samples);
562 }
563
564 void EEVEE_render_update_passes(RenderEngine *engine, Scene *scene, ViewLayer *view_layer)
565 {
566         int type;
567
568         RE_engine_register_pass(engine, scene, view_layer, RE_PASSNAME_COMBINED, 4, "RGBA", SOCK_RGBA);
569
570 #define CHECK_PASS(name, channels, chanid) \
571         if (view_layer->passflag & (SCE_PASS_ ## name)) { \
572                 if (channels == 4) type = SOCK_RGBA; \
573                 else if (channels == 3) type = SOCK_VECTOR; \
574                 else type = SOCK_FLOAT; \
575                 RE_engine_register_pass(engine, scene, view_layer, RE_PASSNAME_ ## name, channels, chanid, type); \
576         }
577
578         CHECK_PASS(Z,           1, "Z");
579         CHECK_PASS(MIST,        1, "Z");
580         CHECK_PASS(NORMAL,      3, "XYZ");
581         CHECK_PASS(AO,          3, "RGB");
582         CHECK_PASS(SUBSURFACE_COLOR,     3, "RGB");
583         CHECK_PASS(SUBSURFACE_DIRECT,    3, "RGB");
584
585 #undef CHECK_PASS
586 }