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