doxygen: add newline after \file
[blender.git] / source / blender / draw / engines / eevee / eevee_engine.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 #include "DRW_render.h"
24
25 #include "BLI_rand.h"
26
27 #include "BKE_object.h"
28 #include "BKE_global.h" /* for G.debug_value */
29
30 #include "DNA_world_types.h"
31
32 #include "eevee_private.h"
33
34 #define EEVEE_ENGINE "BLENDER_EEVEE"
35
36 /* *********** FUNCTIONS *********** */
37
38 static void eevee_engine_init(void *ved)
39 {
40         EEVEE_Data *vedata = (EEVEE_Data *)ved;
41         EEVEE_TextureList *txl = vedata->txl;
42         EEVEE_FramebufferList *fbl = vedata->fbl;
43         EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
44         EEVEE_ViewLayerData *sldata = EEVEE_view_layer_data_ensure();
45         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
46
47         const DRWContextState *draw_ctx = DRW_context_state_get();
48         View3D *v3d = draw_ctx->v3d;
49         RegionView3D *rv3d = draw_ctx->rv3d;
50         Object *camera = (rv3d->persp == RV3D_CAMOB) ? v3d->camera : NULL;
51
52         if (!stl->g_data) {
53                 /* Alloc transient pointers */
54                 stl->g_data = MEM_callocN(sizeof(*stl->g_data), __func__);
55         }
56         stl->g_data->use_color_render_settings = USE_SCENE_LIGHT(v3d) || !LOOK_DEV_STUDIO_LIGHT_ENABLED(v3d);
57         stl->g_data->background_alpha = DRW_state_draw_background() ? 1.0f : 0.0f;
58         stl->g_data->valid_double_buffer = (txl->color_double_buffer != NULL);
59         stl->g_data->valid_taa_history = (txl->taa_history != NULL);
60
61         /* Main Buffer */
62         DRW_texture_ensure_fullscreen_2D(&txl->color, GPU_RGBA16F, DRW_TEX_FILTER | DRW_TEX_MIPMAP);
63
64         GPU_framebuffer_ensure_config(&fbl->main_fb, {
65                 GPU_ATTACHMENT_TEXTURE(dtxl->depth),
66                 GPU_ATTACHMENT_TEXTURE(txl->color),
67                 GPU_ATTACHMENT_LEAVE,
68                 GPU_ATTACHMENT_LEAVE,
69                 GPU_ATTACHMENT_LEAVE,
70                 GPU_ATTACHMENT_LEAVE
71         });
72
73         GPU_framebuffer_ensure_config(&fbl->main_color_fb, {
74                 GPU_ATTACHMENT_NONE,
75                 GPU_ATTACHMENT_TEXTURE(txl->color)
76         });
77
78         if (sldata->common_ubo == NULL) {
79                 sldata->common_ubo = DRW_uniformbuffer_create(sizeof(sldata->common_data), &sldata->common_data);
80         }
81         if (sldata->clip_ubo == NULL) {
82                 sldata->clip_ubo = DRW_uniformbuffer_create(sizeof(sldata->clip_data), &sldata->clip_data);
83         }
84
85         /* EEVEE_effects_init needs to go first for TAA */
86         EEVEE_effects_init(sldata, vedata, camera, false);
87         EEVEE_materials_init(sldata, stl, fbl);
88         EEVEE_lights_init(sldata);
89         EEVEE_lightprobes_init(sldata, vedata);
90
91         if ((stl->effects->taa_current_sample > 1) && !DRW_state_is_image_render()) {
92                 /* XXX otherwise it would break the other engines. */
93                 DRW_viewport_matrix_override_unset_all();
94         }
95 }
96
97 static void eevee_cache_init(void *vedata)
98 {
99         EEVEE_ViewLayerData *sldata = EEVEE_view_layer_data_ensure();
100
101         EEVEE_bloom_cache_init(sldata, vedata);
102         EEVEE_depth_of_field_cache_init(sldata, vedata);
103         EEVEE_effects_cache_init(sldata, vedata);
104         EEVEE_lightprobes_cache_init(sldata, vedata);
105         EEVEE_lights_cache_init(sldata, vedata);
106         EEVEE_materials_cache_init(sldata, vedata);
107         EEVEE_motion_blur_cache_init(sldata, vedata);
108         EEVEE_occlusion_cache_init(sldata, vedata);
109         EEVEE_screen_raytrace_cache_init(sldata, vedata);
110         EEVEE_subsurface_cache_init(sldata, vedata);
111         EEVEE_temporal_sampling_cache_init(sldata, vedata);
112         EEVEE_volumes_cache_init(sldata, vedata);
113 }
114
115 void EEVEE_cache_populate(void *vedata, Object *ob)
116 {
117         EEVEE_ViewLayerData *sldata = EEVEE_view_layer_data_ensure();
118
119         const DRWContextState *draw_ctx = DRW_context_state_get();
120         const int ob_visibility = DRW_object_visibility_in_active_context(ob);
121         bool cast_shadow = false;
122
123         if (ob_visibility & OB_VISIBLE_PARTICLES) {
124                 EEVEE_hair_cache_populate(vedata, sldata, ob, &cast_shadow);
125         }
126
127         if (DRW_object_is_renderable(ob) && (ob_visibility & OB_VISIBLE_SELF)) {
128                 if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL)) {
129                         EEVEE_materials_cache_populate(vedata, sldata, ob, &cast_shadow);
130                 }
131                 else if (!USE_SCENE_LIGHT(draw_ctx->v3d)) {
132                         /* do not add any scene light sources to the cache */
133                 }
134                 else if (ob->type == OB_LIGHTPROBE) {
135                         if ((ob->base_flag & BASE_FROM_DUPLI) != 0) {
136                                 /* TODO: Special case for dupli objects because we cannot save the object pointer. */
137                         }
138                         else {
139                                 EEVEE_lightprobes_cache_add(sldata, vedata, ob);
140                         }
141                 }
142                 else if (ob->type == OB_LAMP) {
143                         EEVEE_lights_cache_add(sldata, ob);
144                 }
145         }
146
147         if (cast_shadow) {
148                 EEVEE_lights_cache_shcaster_object_add(sldata, ob);
149         }
150 }
151
152 static void eevee_cache_finish(void *vedata)
153 {
154         EEVEE_ViewLayerData *sldata = EEVEE_view_layer_data_ensure();
155
156         EEVEE_materials_cache_finish(vedata);
157         EEVEE_lights_cache_finish(sldata, vedata);
158         EEVEE_lightprobes_cache_finish(sldata, vedata);
159 }
160
161 /* As renders in an HDR offscreen buffer, we need draw everything once
162  * during the background pass. This way the other drawing callback between
163  * the background and the scene pass are visible.
164  * Note: we could break it up in two passes using some depth test
165  * to reduce the fillrate */
166 static void eevee_draw_background(void *vedata)
167 {
168         EEVEE_PassList *psl = ((EEVEE_Data *)vedata)->psl;
169         EEVEE_TextureList *txl = ((EEVEE_Data *)vedata)->txl;
170         EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
171         EEVEE_FramebufferList *fbl = ((EEVEE_Data *)vedata)->fbl;
172         EEVEE_EffectsInfo *effects = stl->effects;
173         EEVEE_ViewLayerData *sldata = EEVEE_view_layer_data_ensure();
174
175         /* Default framebuffer and texture */
176         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
177         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
178
179         /* Sort transparents before the loop. */
180         DRW_pass_sort_shgroup_z(psl->transparent_pass);
181
182         /* Number of iteration: needed for all temporal effect (SSR, volumetrics)
183          * when using opengl render. */
184         int loop_len = (DRW_state_is_image_render() &&
185                        (stl->effects->enabled_effects & (EFFECT_VOLUMETRIC | EFFECT_SSR)) != 0) ? 4 : 1;
186
187         while (loop_len--) {
188                 float clear_col[4] = {0.0f, 0.0f, 0.0f, 0.0f};
189                 float clear_depth = 1.0f;
190                 uint clear_stencil = 0x0;
191                 uint primes[3] = {2, 3, 7};
192                 double offset[3] = {0.0, 0.0, 0.0};
193                 double r[3];
194
195                 bool taa_use_reprojection = (stl->effects->enabled_effects & EFFECT_TAA_REPROJECT) != 0;
196
197                 if (DRW_state_is_image_render() ||
198                     taa_use_reprojection ||
199                     ((stl->effects->enabled_effects & EFFECT_TAA) != 0))
200                 {
201                         int samp = taa_use_reprojection
202                                     ? stl->effects->taa_reproject_sample + 1
203                                     : stl->effects->taa_current_sample;
204                         BLI_halton_3D(primes, offset, samp, r);
205                         EEVEE_update_noise(psl, fbl, r);
206                         EEVEE_volumes_set_jitter(sldata, samp - 1);
207                         EEVEE_materials_init(sldata, stl, fbl);
208                 }
209                 /* Copy previous persmat to UBO data */
210                 copy_m4_m4(sldata->common_data.prev_persmat, stl->effects->prev_persmat);
211
212                 if (((stl->effects->enabled_effects & EFFECT_TAA) != 0) &&
213                     (stl->effects->taa_current_sample > 1) &&
214                     !DRW_state_is_image_render() &&
215                     !taa_use_reprojection)
216                 {
217                         DRW_viewport_matrix_override_set(stl->effects->overide_persmat, DRW_MAT_PERS);
218                         DRW_viewport_matrix_override_set(stl->effects->overide_persinv, DRW_MAT_PERSINV);
219                         DRW_viewport_matrix_override_set(stl->effects->overide_winmat, DRW_MAT_WIN);
220                         DRW_viewport_matrix_override_set(stl->effects->overide_wininv, DRW_MAT_WININV);
221                 }
222
223                 /* Refresh Probes */
224                 DRW_stats_group_start("Probes Refresh");
225                 EEVEE_lightprobes_refresh(sldata, vedata);
226                 /* Probes refresh can have reset the current sample. */
227                 if (stl->effects->taa_current_sample == 1) {
228                         DRW_viewport_matrix_override_unset_all();
229                 }
230                 EEVEE_lightprobes_refresh_planar(sldata, vedata);
231                 DRW_stats_group_end();
232
233                 /* Refresh shadows */
234                 DRW_stats_group_start("Shadows");
235                 EEVEE_draw_shadows(sldata, vedata);
236                 DRW_stats_group_end();
237
238                 /* Set ray type. */
239                 sldata->common_data.ray_type = EEVEE_RAY_CAMERA;
240                 sldata->common_data.ray_depth = 0.0f;
241                 DRW_uniformbuffer_update(sldata->common_ubo, &sldata->common_data);
242
243                 GPU_framebuffer_bind(fbl->main_fb);
244                 eGPUFrameBufferBits clear_bits = GPU_DEPTH_BIT;
245                 clear_bits |= (DRW_state_draw_background()) ? 0 : GPU_COLOR_BIT;
246                 clear_bits |= ((stl->effects->enabled_effects & EFFECT_SSS) != 0) ? GPU_STENCIL_BIT : 0;
247                 GPU_framebuffer_clear(fbl->main_fb, clear_bits, clear_col, clear_depth, clear_stencil);
248
249                 /* Depth prepass */
250                 DRW_stats_group_start("Prepass");
251                 DRW_draw_pass(psl->depth_pass);
252                 DRW_draw_pass(psl->depth_pass_cull);
253                 DRW_stats_group_end();
254
255                 /* Create minmax texture */
256                 DRW_stats_group_start("Main MinMax buffer");
257                 EEVEE_create_minmax_buffer(vedata, dtxl->depth, -1);
258                 DRW_stats_group_end();
259
260                 EEVEE_occlusion_compute(sldata, vedata, dtxl->depth, -1);
261                 EEVEE_volumes_compute(sldata, vedata);
262
263                 /* Shading pass */
264                 DRW_stats_group_start("Shading");
265                 if (DRW_state_draw_background()) {
266                         DRW_draw_pass(psl->background_pass);
267                 }
268                 EEVEE_draw_default_passes(psl);
269                 DRW_draw_pass(psl->material_pass);
270                 EEVEE_subsurface_data_render(sldata, vedata);
271                 DRW_stats_group_end();
272
273                 /* Effects pre-transparency */
274                 EEVEE_subsurface_compute(sldata, vedata);
275                 EEVEE_reflection_compute(sldata, vedata);
276                 EEVEE_occlusion_draw_debug(sldata, vedata);
277                 if (psl->probe_display) {
278                         DRW_draw_pass(psl->probe_display);
279                 }
280                 EEVEE_refraction_compute(sldata, vedata);
281
282                 /* Opaque refraction */
283                 DRW_stats_group_start("Opaque Refraction");
284                 DRW_draw_pass(psl->refract_depth_pass);
285                 DRW_draw_pass(psl->refract_depth_pass_cull);
286                 DRW_draw_pass(psl->refract_pass);
287                 DRW_stats_group_end();
288
289                 /* Volumetrics Resolve Opaque */
290                 EEVEE_volumes_resolve(sldata, vedata);
291
292                 /* Transparent */
293                 DRW_draw_pass(psl->transparent_pass);
294
295                 /* Post Process */
296                 DRW_stats_group_start("Post FX");
297                 EEVEE_draw_effects(sldata, vedata);
298                 DRW_stats_group_end();
299
300                 if ((stl->effects->taa_current_sample > 1) && !DRW_state_is_image_render()) {
301                         DRW_viewport_matrix_override_unset_all();
302                 }
303         }
304
305         /* LookDev */
306         EEVEE_lookdev_draw_background(vedata);
307         /* END */
308
309
310         /* Tonemapping and transfer result to default framebuffer. */
311         bool use_render_settings = stl->g_data->use_color_render_settings;
312
313         GPU_framebuffer_bind(dfbl->default_fb);
314         DRW_transform_to_display(stl->effects->final_tx, true, use_render_settings);
315
316         /* Debug : Output buffer to view. */
317         switch (G.debug_value) {
318                 case 1:
319                         if (txl->maxzbuffer) DRW_transform_to_display(txl->maxzbuffer, false, false);
320                         break;
321                 case 2:
322                         if (effects->ssr_pdf_output) DRW_transform_to_display(effects->ssr_pdf_output, false, false);
323                         break;
324                 case 3:
325                         if (effects->ssr_normal_input) DRW_transform_to_display(effects->ssr_normal_input, false, false);
326                         break;
327                 case 4:
328                         if (effects->ssr_specrough_input) DRW_transform_to_display(effects->ssr_specrough_input, false, false);
329                         break;
330                 case 5:
331                         if (txl->color_double_buffer) DRW_transform_to_display(txl->color_double_buffer, false, false);
332                         break;
333                 case 6:
334                         if (effects->gtao_horizons_debug) DRW_transform_to_display(effects->gtao_horizons_debug, false, false);
335                         break;
336                 case 7:
337                         if (effects->gtao_horizons) DRW_transform_to_display(effects->gtao_horizons, false, false);
338                         break;
339                 case 8:
340                         if (effects->sss_data) DRW_transform_to_display(effects->sss_data, false, false);
341                         break;
342                 case 9:
343                         if (effects->velocity_tx) DRW_transform_to_display(effects->velocity_tx, false, false);
344                         break;
345                 default:
346                         break;
347         }
348
349         EEVEE_volumes_free_smoke_textures();
350
351         stl->g_data->view_updated = false;
352 }
353
354 static void eevee_view_update(void *vedata)
355 {
356         EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
357         if (stl->g_data) {
358                 stl->g_data->view_updated = true;
359         }
360 }
361
362 static void eevee_id_object_update(void *UNUSED(vedata), Object *object)
363 {
364         EEVEE_LightProbeEngineData *ped = EEVEE_lightprobe_data_get(object);
365         if (ped != NULL && ped->dd.recalc != 0) {
366                 ped->need_update = (ped->dd.recalc & (ID_RECALC_TRANSFORM | ID_RECALC_COPY_ON_WRITE)) != 0;
367                 ped->dd.recalc = 0;
368         }
369         EEVEE_LampEngineData *led = EEVEE_lamp_data_get(object);
370         if (led != NULL && led->dd.recalc != 0) {
371                 led->need_update = true;
372                 led->dd.recalc = 0;
373         }
374         EEVEE_ObjectEngineData *oedata = EEVEE_object_data_get(object);
375         if (oedata != NULL && oedata->dd.recalc != 0) {
376                 oedata->need_update = true;
377                 oedata->dd.recalc = 0;
378         }
379 }
380
381 static void eevee_id_world_update(void *vedata, World *wo)
382 {
383         EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
384         LightCache *lcache = stl->g_data->light_cache;
385
386         EEVEE_WorldEngineData *wedata = EEVEE_world_data_ensure(wo);
387
388         if (wedata != NULL && wedata->dd.recalc != 0) {
389                 if ((lcache->flag & (LIGHTCACHE_BAKED | LIGHTCACHE_BAKING)) == 0) {
390                         lcache->flag |= LIGHTCACHE_UPDATE_WORLD;
391                 }
392                 wedata->dd.recalc = 0;
393         }
394 }
395
396 static void eevee_id_update(void *vedata, ID *id)
397 {
398         /* Handle updates based on ID type. */
399         switch (GS(id->name)) {
400                 case ID_WO:
401                         eevee_id_world_update(vedata, (World *)id);
402                         break;
403                 case ID_OB:
404                         eevee_id_object_update(vedata, (Object *)id);
405                         break;
406                 default:
407                         /* pass */
408                         break;
409         }
410 }
411
412 static void eevee_render_to_image(void *vedata, RenderEngine *engine, struct RenderLayer *render_layer, const rcti *rect)
413 {
414         const DRWContextState *draw_ctx = DRW_context_state_get();
415         EEVEE_render_init(vedata, engine, draw_ctx->depsgraph);
416
417         DRW_render_object_iter(vedata, engine, draw_ctx->depsgraph, EEVEE_render_cache);
418
419         /* Actually do the rendering. */
420         EEVEE_render_draw(vedata, engine, render_layer, rect);
421 }
422
423 static void eevee_engine_free(void)
424 {
425         EEVEE_shaders_free();
426         EEVEE_bloom_free();
427         EEVEE_depth_of_field_free();
428         EEVEE_effects_free();
429         EEVEE_lightprobes_free();
430         EEVEE_lights_free();
431         EEVEE_materials_free();
432         EEVEE_mist_free();
433         EEVEE_motion_blur_free();
434         EEVEE_occlusion_free();
435         EEVEE_screen_raytrace_free();
436         EEVEE_subsurface_free();
437         EEVEE_volumes_free();
438 }
439
440 static const DrawEngineDataSize eevee_data_size = DRW_VIEWPORT_DATA_SIZE(EEVEE_Data);
441
442 DrawEngineType draw_engine_eevee_type = {
443         NULL, NULL,
444         N_("Eevee"),
445         &eevee_data_size,
446         &eevee_engine_init,
447         &eevee_engine_free,
448         &eevee_cache_init,
449         &EEVEE_cache_populate,
450         &eevee_cache_finish,
451         &eevee_draw_background,
452         NULL, /* Everything is drawn in the background pass (see comment on function) */
453         &eevee_view_update,
454         &eevee_id_update,
455         &eevee_render_to_image,
456 };
457
458 RenderEngineType DRW_engine_viewport_eevee_type = {
459         NULL, NULL,
460         EEVEE_ENGINE, N_("Eevee"), RE_INTERNAL | RE_USE_SHADING_NODES | RE_USE_PREVIEW,
461         NULL, &DRW_render_to_image, NULL, NULL, NULL, NULL,
462         &EEVEE_render_update_passes,
463         &draw_engine_eevee_type,
464         {NULL, NULL, NULL},
465 };
466
467
468 #undef EEVEE_ENGINE