DRW: Fix crash caused by hair transform feedback
[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 void EEVEE_render_cache(
138         void *vedata, struct Object *ob,
139         struct RenderEngine *engine, struct Depsgraph *UNUSED(depsgraph))
140 {
141         EEVEE_ViewLayerData *sldata = EEVEE_view_layer_data_ensure();
142
143         char info[42];
144         BLI_snprintf(info, sizeof(info), "Syncing %s", ob->id.name + 2);
145         RE_engine_update_stats(engine, NULL, info);
146         bool cast_shadow = false;
147
148         if (ob->base_flag & BASE_VISIBLED) {
149                 EEVEE_hair_cache_populate(vedata, sldata, ob, &cast_shadow);
150         }
151
152         if (DRW_check_object_visible_within_active_context(ob)) {
153                 if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL)) {
154                         EEVEE_materials_cache_populate(vedata, sldata, ob, &cast_shadow);
155                 }
156                 else if (ob->type == OB_LIGHTPROBE) {
157                         EEVEE_lightprobes_cache_add(sldata, ob);
158                 }
159                 else if (ob->type == OB_LAMP) {
160                         EEVEE_lights_cache_add(sldata, ob);
161                 }
162         }
163
164         if (cast_shadow) {
165                 EEVEE_lights_cache_shcaster_object_add(sldata, ob);
166         }
167 }
168
169 static void eevee_render_result_combined(
170         RenderLayer *rl, const char *viewname, const rcti *rect,
171         EEVEE_Data *vedata, EEVEE_ViewLayerData *UNUSED(sldata))
172 {
173         RenderPass *rp = RE_pass_find_by_name(rl, RE_PASSNAME_COMBINED, viewname);
174
175         GPU_framebuffer_bind(vedata->stl->effects->final_fb);
176         GPU_framebuffer_read_color(vedata->stl->effects->final_fb,
177                                    rect->xmin, rect->ymin,
178                                    BLI_rcti_size_x(rect), BLI_rcti_size_y(rect),
179                                    4, 0, rp->rect);
180 }
181
182 static void eevee_render_result_subsurface(
183         RenderLayer *rl, const char *viewname, const rcti *rect,
184         EEVEE_Data *vedata, EEVEE_ViewLayerData *UNUSED(sldata), int render_samples)
185 {
186         const DRWContextState *draw_ctx = DRW_context_state_get();
187         ViewLayer *view_layer = draw_ctx->view_layer;
188
189         if (vedata->fbl->sss_accum_fb == NULL) {
190                 /* SSS is not enabled. */
191                 return;
192         }
193
194         if ((view_layer->passflag & SCE_PASS_SUBSURFACE_COLOR) != 0) {
195                 RenderPass *rp = RE_pass_find_by_name(rl, RE_PASSNAME_SUBSURFACE_COLOR, viewname);
196
197                 GPU_framebuffer_bind(vedata->fbl->sss_accum_fb);
198                 GPU_framebuffer_read_color(vedata->fbl->sss_accum_fb,
199                                            rect->xmin, rect->ymin,
200                                            BLI_rcti_size_x(rect), BLI_rcti_size_y(rect),
201                                            3, 1, rp->rect);
202
203                 /* This is the accumulated color. Divide by the number of samples. */
204                 for (int i = 0; i < rp->rectx * rp->recty * 3; i++) {
205                         rp->rect[i] /= (float)render_samples;
206                 }
207         }
208
209         if ((view_layer->passflag & SCE_PASS_SUBSURFACE_DIRECT) != 0) {
210                 RenderPass *rp = RE_pass_find_by_name(rl, RE_PASSNAME_SUBSURFACE_DIRECT, viewname);
211
212                 GPU_framebuffer_bind(vedata->fbl->sss_accum_fb);
213                 GPU_framebuffer_read_color(vedata->fbl->sss_accum_fb,
214                                            rect->xmin, rect->ymin,
215                                            BLI_rcti_size_x(rect), BLI_rcti_size_y(rect),
216                                            3, 0, rp->rect);
217
218                 /* This is the accumulated color. Divide by the number of samples. */
219                 for (int i = 0; i < rp->rectx * rp->recty * 3; i++) {
220                         rp->rect[i] /= (float)render_samples;
221                 }
222         }
223
224         if ((view_layer->passflag & SCE_PASS_SUBSURFACE_INDIRECT) != 0) {
225                 /* Do nothing as all the lighting is in the direct pass.
226                  * TODO : Separate Direct from indirect lighting. */
227         }
228 }
229
230 static void eevee_render_result_normal(
231         RenderLayer *rl, const char *viewname, const rcti *rect,
232         EEVEE_Data *vedata, EEVEE_ViewLayerData *UNUSED(sldata))
233 {
234         const DRWContextState *draw_ctx = DRW_context_state_get();
235         ViewLayer *view_layer = draw_ctx->view_layer;
236         EEVEE_StorageList *stl = vedata->stl;
237         EEVEE_PrivateData *g_data = stl->g_data;
238
239         /* Only read the center texel. */
240         if (stl->effects->taa_current_sample > 1)
241                 return;
242
243         if ((view_layer->passflag & SCE_PASS_NORMAL) != 0) {
244                 RenderPass *rp = RE_pass_find_by_name(rl, RE_PASSNAME_NORMAL, viewname);
245
246                 GPU_framebuffer_bind(vedata->fbl->main_fb);
247                 GPU_framebuffer_read_color(vedata->fbl->main_fb,
248                                            rect->xmin, rect->ymin,
249                                            BLI_rcti_size_x(rect), BLI_rcti_size_y(rect),
250                                            3, 1, rp->rect);
251
252                 /* Convert Eevee encoded normals to Blender normals. */
253                 for (int i = 0; i < rp->rectx * rp->recty * 3; i += 3) {
254                         if (rp->rect[i] == 0.0f && rp->rect[i + 1] == 0.0f) {
255                                 /* If normal is not correct then do not produce NANs.  */
256                                 continue;
257                         }
258
259                         float fenc[2];
260                         fenc[0] = rp->rect[i + 0] * 4.0f - 2.0f;
261                         fenc[1] = rp->rect[i + 1] * 4.0f - 2.0f;
262
263                         float f = dot_v2v2(fenc, fenc);
264                         float g = sqrtf(1.0f - f / 4.0f);
265
266                         rp->rect[i + 0] = fenc[0] * g;
267                         rp->rect[i + 1] = fenc[1] * g;
268                         rp->rect[i + 2] = 1.0f - f / 2.0f;
269
270                         mul_mat3_m4_v3(g_data->viewinv, &rp->rect[i]);
271                 }
272         }
273 }
274
275 static void eevee_render_result_z(
276         RenderLayer *rl, const char *viewname, const rcti *rect,
277         EEVEE_Data *vedata, EEVEE_ViewLayerData *sldata)
278 {
279         const DRWContextState *draw_ctx = DRW_context_state_get();
280         ViewLayer *view_layer = draw_ctx->view_layer;
281         EEVEE_CommonUniformBuffer *common_data = &sldata->common_data;
282         EEVEE_StorageList *stl = vedata->stl;
283         EEVEE_PrivateData *g_data = stl->g_data;
284
285         /* Only read the center texel. */
286         if (stl->effects->taa_current_sample > 1)
287                 return;
288
289         if ((view_layer->passflag & SCE_PASS_Z) != 0) {
290                 RenderPass *rp = RE_pass_find_by_name(rl, RE_PASSNAME_Z, viewname);
291
292                 GPU_framebuffer_bind(vedata->fbl->main_fb);
293                 GPU_framebuffer_read_depth(vedata->fbl->main_fb,
294                                            rect->xmin, rect->ymin,
295                                            BLI_rcti_size_x(rect), BLI_rcti_size_y(rect),
296                                            rp->rect);
297
298                 bool is_persp = DRW_viewport_is_persp_get();
299
300                 /* Convert ogl depth [0..1] to view Z [near..far] */
301                 for (int i = 0; i < rp->rectx * rp->recty; ++i) {
302                         if (rp->rect[i] == 1.0f ) {
303                                 rp->rect[i] = 1e10f; /* Background */
304                         }
305                         else {
306                                 if (is_persp) {
307                                         rp->rect[i] = rp->rect[i] * 2.0f - 1.0f;
308                                         rp->rect[i] = g_data->winmat[3][2] / (rp->rect[i] + g_data->winmat[2][2]);
309                                 }
310                                 else {
311                                         rp->rect[i] = -common_data->view_vecs[0][2] + rp->rect[i] * -common_data->view_vecs[1][2];
312                                 }
313                         }
314                 }
315         }
316 }
317
318 static void eevee_render_result_mist(
319         RenderLayer *rl, const char *viewname, const rcti *rect,
320         EEVEE_Data *vedata, EEVEE_ViewLayerData *UNUSED(sldata), int render_samples)
321 {
322         const DRWContextState *draw_ctx = DRW_context_state_get();
323         ViewLayer *view_layer = draw_ctx->view_layer;
324
325         if ((view_layer->passflag & SCE_PASS_MIST) != 0) {
326                 RenderPass *rp = RE_pass_find_by_name(rl, RE_PASSNAME_MIST, viewname);
327
328                 GPU_framebuffer_bind(vedata->fbl->mist_accum_fb);
329                 GPU_framebuffer_read_color(vedata->fbl->mist_accum_fb,
330                                            rect->xmin, rect->ymin,
331                                            BLI_rcti_size_x(rect), BLI_rcti_size_y(rect),
332                                            1, 0, rp->rect);
333
334                 /* This is the accumulated color. Divide by the number of samples. */
335                 for (int i = 0; i < rp->rectx * rp->recty; i++) {
336                         rp->rect[i] /= (float)render_samples;
337                 }
338         }
339 }
340
341 static void eevee_render_result_occlusion(
342         RenderLayer *rl, const char *viewname, const rcti *rect,
343         EEVEE_Data *vedata, EEVEE_ViewLayerData *UNUSED(sldata), int render_samples)
344 {
345         const DRWContextState *draw_ctx = DRW_context_state_get();
346         ViewLayer *view_layer = draw_ctx->view_layer;
347
348         if (vedata->fbl->ao_accum_fb == NULL) {
349                 /* AO is not enabled. */
350                 return;
351         }
352
353         if ((view_layer->passflag & SCE_PASS_AO) != 0) {
354                 RenderPass *rp = RE_pass_find_by_name(rl, RE_PASSNAME_AO, viewname);
355
356                 GPU_framebuffer_bind(vedata->fbl->ao_accum_fb);
357                 GPU_framebuffer_read_color(vedata->fbl->ao_accum_fb,
358                                            rect->xmin, rect->ymin,
359                                            BLI_rcti_size_x(rect), BLI_rcti_size_y(rect),
360                                            3, 0, rp->rect);
361
362                 /* This is the accumulated color. Divide by the number of samples. */
363                 for (int i = 0; i < rp->rectx * rp->recty * 3; i += 3) {
364                         rp->rect[i] = rp->rect[i + 1] = rp->rect[i + 2] = min_ff(1.0f, rp->rect[i] / (float)render_samples);
365                 }
366         }
367 }
368
369 static void eevee_render_draw_background(EEVEE_Data *vedata)
370 {
371         EEVEE_FramebufferList *fbl = vedata->fbl;
372         EEVEE_StorageList *stl = vedata->stl;
373         EEVEE_PassList *psl = vedata->psl;
374
375         /* Prevent background to write to data buffers.
376          * NOTE : This also make sure the textures are bound
377          *        to the right double buffer. */
378         GPU_framebuffer_ensure_config(&fbl->main_fb, {
379                 GPU_ATTACHMENT_LEAVE,
380                 GPU_ATTACHMENT_LEAVE,
381                 GPU_ATTACHMENT_NONE,
382                 GPU_ATTACHMENT_NONE,
383                 GPU_ATTACHMENT_NONE,
384                 GPU_ATTACHMENT_NONE
385         });
386         GPU_framebuffer_bind(fbl->main_fb);
387
388         DRW_draw_pass(psl->background_pass);
389
390         GPU_framebuffer_ensure_config(&fbl->main_fb, {
391                 GPU_ATTACHMENT_LEAVE,
392                 GPU_ATTACHMENT_LEAVE,
393                 GPU_ATTACHMENT_TEXTURE(stl->effects->ssr_normal_input),
394                 GPU_ATTACHMENT_TEXTURE(stl->effects->ssr_specrough_input),
395                 GPU_ATTACHMENT_TEXTURE(stl->effects->sss_data),
396                 GPU_ATTACHMENT_TEXTURE(stl->effects->sss_albedo)
397         });
398         GPU_framebuffer_bind(fbl->main_fb);
399 }
400
401 void EEVEE_render_draw(EEVEE_Data *vedata, RenderEngine *engine, RenderLayer *rl, const rcti *rect)
402 {
403         const DRWContextState *draw_ctx = DRW_context_state_get();
404         const Scene *scene_eval = DEG_get_evaluated_scene(draw_ctx->depsgraph);
405         ViewLayer *view_layer = draw_ctx->view_layer;
406         const char *viewname = RE_GetActiveRenderView(engine->re);
407         EEVEE_PassList *psl = vedata->psl;
408         EEVEE_StorageList *stl = vedata->stl;
409         EEVEE_FramebufferList *fbl = vedata->fbl;
410         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
411         EEVEE_ViewLayerData *sldata = EEVEE_view_layer_data_ensure();
412         EEVEE_PrivateData *g_data = stl->g_data;
413
414         /* FINISH CACHE */
415         EEVEE_materials_cache_finish(vedata);
416         EEVEE_lights_cache_finish(sldata);
417         EEVEE_lightprobes_cache_finish(sldata, vedata);
418
419         /* Sort transparents before the loop. */
420         DRW_pass_sort_shgroup_z(psl->transparent_pass);
421
422         /* Push instances attribs to the GPU. */
423         DRW_render_instance_buffer_finish();
424
425         /* Need to be called after DRW_render_instance_buffer_finish() */
426         /* Also we weed to have a correct fbo bound for DRW_hair_update */
427         GPU_framebuffer_bind(fbl->main_fb);
428         DRW_hair_update();
429
430         if ((view_layer->passflag & (SCE_PASS_SUBSURFACE_COLOR |
431                                      SCE_PASS_SUBSURFACE_DIRECT |
432                                      SCE_PASS_SUBSURFACE_INDIRECT)) != 0)
433         {
434                 EEVEE_subsurface_output_init(sldata, vedata);
435         }
436
437         if ((view_layer->passflag & SCE_PASS_MIST) != 0) {
438                 EEVEE_mist_output_init(sldata, vedata);
439         }
440
441         if ((view_layer->passflag & SCE_PASS_AO) != 0) {
442                 EEVEE_occlusion_output_init(sldata, vedata);
443         }
444
445         uint tot_sample = scene_eval->eevee.taa_render_samples;
446         uint render_samples = 0;
447
448         if (RE_engine_test_break(engine)) {
449                 return;
450         }
451
452         while (render_samples < tot_sample && !RE_engine_test_break(engine)) {
453                 float clear_col[4] = {0.0f, 0.0f, 0.0f, 0.0f};
454                 float clear_depth = 1.0f;
455                 uint clear_stencil = 0xFF;
456                 uint primes[3] = {2, 3, 7};
457                 double offset[3] = {0.0, 0.0, 0.0};
458                 double r[3];
459
460                 /* Restore winmat before jittering again. */
461                 copy_m4_m4(stl->effects->overide_winmat, g_data->winmat);
462                 /* Copy previous persmat to UBO data */
463                 copy_m4_m4(sldata->common_data.prev_persmat, stl->effects->prev_persmat);
464
465                 BLI_halton_3D(primes, offset, stl->effects->taa_current_sample, r);
466                 EEVEE_update_noise(psl, fbl, r);
467                 EEVEE_temporal_sampling_matrices_calc(stl->effects, g_data->viewmat, g_data->persmat, r);
468                 EEVEE_volumes_set_jitter(sldata, stl->effects->taa_current_sample - 1);
469                 EEVEE_materials_init(sldata, stl, fbl);
470
471                 /* Set matrices. */
472                 DRW_viewport_matrix_override_set(stl->effects->overide_persmat, DRW_MAT_PERS);
473                 DRW_viewport_matrix_override_set(stl->effects->overide_persinv, DRW_MAT_PERSINV);
474                 DRW_viewport_matrix_override_set(stl->effects->overide_winmat, DRW_MAT_WIN);
475                 DRW_viewport_matrix_override_set(stl->effects->overide_wininv, DRW_MAT_WININV);
476                 DRW_viewport_matrix_override_set(g_data->viewmat, DRW_MAT_VIEW);
477                 DRW_viewport_matrix_override_set(g_data->viewinv, DRW_MAT_VIEWINV);
478
479                 /* Refresh Probes */
480                 while (EEVEE_lightprobes_all_probes_ready(sldata, vedata) == false) {
481                         RE_engine_update_stats(engine, NULL, "Updating Probes");
482                         EEVEE_lightprobes_refresh(sldata, vedata);
483                         /* Refreshing probes can take some times, allow exit. */
484                         if (RE_engine_test_break(engine)) {
485                                 return;
486                         }
487                 }
488                 EEVEE_lightprobes_refresh_planar(sldata, vedata);
489                 DRW_uniformbuffer_update(sldata->common_ubo, &sldata->common_data);
490
491                 char info[42];
492                 BLI_snprintf(info, sizeof(info), "Rendering %u / %u samples", render_samples + 1, tot_sample);
493                 RE_engine_update_stats(engine, NULL, info);
494
495                 /* Refresh Shadows */
496                 EEVEE_draw_shadows(sldata, psl);
497
498                 GPU_framebuffer_bind(fbl->main_fb);
499                 GPU_framebuffer_clear_color_depth_stencil(fbl->main_fb, clear_col, clear_depth, clear_stencil);
500                 /* Depth prepass */
501                 DRW_draw_pass(psl->depth_pass);
502                 DRW_draw_pass(psl->depth_pass_cull);
503                 /* Create minmax texture */
504                 EEVEE_create_minmax_buffer(vedata, dtxl->depth, -1);
505                 EEVEE_occlusion_compute(sldata, vedata, dtxl->depth, -1);
506                 EEVEE_volumes_compute(sldata, vedata);
507                 /* Shading pass */
508                 eevee_render_draw_background(vedata);
509                 GPU_framebuffer_bind(fbl->main_fb);
510                 EEVEE_draw_default_passes(psl);
511                 DRW_draw_pass(psl->material_pass);
512                 EEVEE_subsurface_data_render(sldata, vedata);
513                 /* Effects pre-transparency */
514                 EEVEE_subsurface_compute(sldata, vedata);
515                 EEVEE_reflection_compute(sldata, vedata);
516                 EEVEE_refraction_compute(sldata, vedata);
517                 /* Opaque refraction */
518                 DRW_draw_pass(psl->refract_depth_pass);
519                 DRW_draw_pass(psl->refract_depth_pass_cull);
520                 DRW_draw_pass(psl->refract_pass);
521                 /* Subsurface output */
522                 EEVEE_subsurface_output_accumulate(sldata, vedata);
523                 /* Occlusion output */
524                 EEVEE_occlusion_output_accumulate(sldata, vedata);
525                 /* Result NORMAL */
526                 eevee_render_result_normal(rl, viewname, rect, vedata, sldata);
527                 /* Volumetrics Resolve Opaque */
528                 EEVEE_volumes_resolve(sldata, vedata);
529                 /* Mist output */
530                 EEVEE_mist_output_accumulate(sldata, vedata);
531                 /* Transparent */
532                 DRW_draw_pass(psl->transparent_pass);
533                 /* Result Z */
534                 eevee_render_result_z(rl, viewname, rect, vedata, sldata);
535                 /* Post Process */
536                 EEVEE_draw_effects(sldata, vedata);
537
538                 RE_engine_update_progress(engine, (float)(render_samples++) / (float)tot_sample);
539         }
540
541         eevee_render_result_combined(rl, viewname, rect, vedata, sldata);
542         eevee_render_result_subsurface(rl, viewname, rect, vedata, sldata, render_samples);
543         eevee_render_result_mist(rl, viewname, rect, vedata, sldata, render_samples);
544         eevee_render_result_occlusion(rl, viewname, rect, vedata, sldata, render_samples);
545 }
546
547 void EEVEE_render_update_passes(RenderEngine *engine, Scene *scene, ViewLayer *view_layer)
548 {
549         int type;
550
551         RE_engine_register_pass(engine, scene, view_layer, RE_PASSNAME_COMBINED, 4, "RGBA", SOCK_RGBA);
552
553 #define CHECK_PASS(name, channels, chanid) \
554         if (view_layer->passflag & (SCE_PASS_ ## name)) { \
555                 if (channels == 4) type = SOCK_RGBA; \
556                 else if (channels == 3) type = SOCK_VECTOR; \
557                 else type = SOCK_FLOAT; \
558                 RE_engine_register_pass(engine, scene, view_layer, RE_PASSNAME_ ## name, channels, chanid, type); \
559         }
560
561         CHECK_PASS(Z,           1, "Z");
562         CHECK_PASS(MIST,        1, "Z");
563         CHECK_PASS(NORMAL,      3, "XYZ");
564         CHECK_PASS(AO,          3, "RGB");
565         CHECK_PASS(SUBSURFACE_COLOR,     3, "RGB");
566         CHECK_PASS(SUBSURFACE_DIRECT,    3, "RGB");
567
568 #undef CHECK_PASS
569 }