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