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