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