Eevee: Stop depsgraph update callback form allocating unneeded memory
[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
33 #include "DNA_world_types.h"
34
35 #include "GPU_material.h"
36 #include "GPU_glew.h"
37
38 #include "eevee_engine.h"
39 #include "eevee_private.h"
40
41 #define EEVEE_ENGINE "BLENDER_EEVEE"
42
43 extern GlobalsUboStorage ts;
44
45 /* *********** FUNCTIONS *********** */
46
47 static void EEVEE_engine_init(void *ved)
48 {
49         EEVEE_Data *vedata = (EEVEE_Data *)ved;
50         EEVEE_TextureList *txl = vedata->txl;
51         EEVEE_FramebufferList *fbl = vedata->fbl;
52         EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
53         EEVEE_ViewLayerData *sldata = EEVEE_view_layer_data_ensure();
54
55         if (!stl->g_data) {
56                 /* Alloc transient pointers */
57                 stl->g_data = MEM_callocN(sizeof(*stl->g_data), __func__);
58         }
59         stl->g_data->background_alpha = 1.0f;
60         stl->g_data->valid_double_buffer = (txl->color_double_buffer != NULL);
61
62         DRWFboTexture tex = {&txl->color, DRW_TEX_RGBA_16, DRW_TEX_FILTER | DRW_TEX_MIPMAP};
63
64         const float *viewport_size = DRW_viewport_size_get();
65         DRW_framebuffer_init(&fbl->main, &draw_engine_eevee_type,
66                             (int)viewport_size[0], (int)viewport_size[1],
67                             &tex, 1);
68
69         /* EEVEE_effects_init needs to go first for TAA */
70         EEVEE_effects_init(sldata, vedata);
71
72         EEVEE_materials_init(stl);
73         EEVEE_lights_init(sldata);
74         EEVEE_lightprobes_init(sldata, vedata);
75
76         if (stl->effects->taa_current_sample > 1) {
77                 /* XXX otherwise it would break the other engines. */
78                 DRW_viewport_matrix_override_unset(DRW_MAT_PERS);
79                 DRW_viewport_matrix_override_unset(DRW_MAT_PERSINV);
80                 DRW_viewport_matrix_override_unset(DRW_MAT_WIN);
81                 DRW_viewport_matrix_override_unset(DRW_MAT_WININV);
82         }
83 }
84
85 static void EEVEE_cache_init(void *vedata)
86 {
87         EEVEE_PassList *psl = ((EEVEE_Data *)vedata)->psl;
88         EEVEE_ViewLayerData *sldata = EEVEE_view_layer_data_ensure();
89
90         EEVEE_bloom_cache_init(sldata, vedata);
91         EEVEE_depth_of_field_cache_init(sldata, vedata);
92         EEVEE_effects_cache_init(sldata, vedata);
93         EEVEE_lightprobes_cache_init(sldata, vedata);
94         EEVEE_lights_cache_init(sldata, psl);
95         EEVEE_materials_cache_init(vedata);
96         EEVEE_motion_blur_cache_init(sldata, vedata);
97         EEVEE_occlusion_cache_init(sldata, vedata);
98         EEVEE_screen_raytrace_cache_init(sldata, vedata);
99         EEVEE_subsurface_cache_init(sldata, vedata);
100         EEVEE_temporal_sampling_cache_init(sldata, vedata);
101         EEVEE_volumes_cache_init(sldata, vedata);
102 }
103
104 static void EEVEE_cache_populate(void *vedata, Object *ob)
105 {
106         EEVEE_ViewLayerData *sldata = EEVEE_view_layer_data_ensure();
107
108         const DRWContextState *draw_ctx = DRW_context_state_get();
109         const bool is_active = (ob == draw_ctx->obact);
110         if (is_active) {
111                 if (DRW_object_is_mode_shade(ob) == true) {
112                         return;
113                 }
114         }
115
116         if (ELEM(ob->type, OB_MESH)) {
117                 if (!BKE_object_is_visible(ob)) {
118                         return;
119                 }
120
121                 EEVEE_materials_cache_populate(vedata, sldata, ob);
122
123                 const bool cast_shadow = true;
124
125                 if (cast_shadow) {
126                         if ((ob->base_flag & BASE_FROMDUPLI) != 0) {
127                                 /* TODO: Special case for dupli objects because we cannot save the object pointer. */
128                         }
129                         else {
130                                 BLI_addtail(&sldata->shadow_casters, BLI_genericNodeN(ob));
131                         }
132                 }
133         }
134         else if (ob->type == OB_LIGHTPROBE) {
135                 if ((ob->base_flag & BASE_FROMDUPLI) != 0) {
136                         /* TODO: Special case for dupli objects because we cannot save the object pointer. */
137                 }
138                 else {
139                         EEVEE_lightprobes_cache_add(sldata, ob);
140                 }
141         }
142         else if (ob->type == OB_LAMP) {
143                 if ((ob->base_flag & BASE_FROMDUPLI) != 0) {
144                         /* TODO: Special case for dupli objects because we cannot save the object pointer. */
145                 }
146                 else {
147                         EEVEE_lights_cache_add(sldata, ob);
148                 }
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);
158         EEVEE_lightprobes_cache_finish(sldata, vedata);
159 }
160
161 static void EEVEE_draw_scene(void *vedata)
162 {
163         EEVEE_PassList *psl = ((EEVEE_Data *)vedata)->psl;
164         EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
165         EEVEE_FramebufferList *fbl = ((EEVEE_Data *)vedata)->fbl;
166         EEVEE_ViewLayerData *sldata = EEVEE_view_layer_data_ensure();
167
168         /* Default framebuffer and texture */
169         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
170
171         /* Number of iteration: needed for all temporal effect (SSR, TAA)
172          * when using opengl render. */
173         int loop_ct = DRW_state_is_image_render() ? 4 : 1;
174
175         static float rand = 0.0f;
176
177         /* XXX temp for denoising render. TODO plug number of samples here */
178         if (DRW_state_is_image_render()) {
179                 rand += 1.0f / 16.0f;
180                 rand = rand - floorf(rand);
181
182                 /* Set jitter offset */
183                 EEVEE_update_util_texture(rand);
184         }
185         else if (((stl->effects->enabled_effects & EFFECT_TAA) != 0) && (stl->effects->taa_current_sample > 1)) {
186                 double r;
187                 BLI_halton_1D(2, 0.0, stl->effects->taa_current_sample - 1, &r);
188
189                 /* Set jitter offset */
190                 /* PERF This is killing perf ! */
191                 EEVEE_update_util_texture((float)r);
192         }
193
194         while (loop_ct--) {
195
196                 /* Refresh Probes */
197                 DRW_stats_group_start("Probes Refresh");
198                 EEVEE_lightprobes_refresh(sldata, vedata);
199                 DRW_stats_group_end();
200
201                 /* Refresh shadows */
202                 DRW_stats_group_start("Shadows");
203                 EEVEE_draw_shadows(sldata, psl);
204                 DRW_stats_group_end();
205
206                 /* Attach depth to the hdr buffer and bind it */
207                 DRW_framebuffer_texture_detach(dtxl->depth);
208                 DRW_framebuffer_texture_attach(fbl->main, dtxl->depth, 0, 0);
209                 DRW_framebuffer_bind(fbl->main);
210                 DRW_framebuffer_clear(false, true, true, NULL, 1.0f);
211
212                 if (((stl->effects->enabled_effects & EFFECT_TAA) != 0) && stl->effects->taa_current_sample > 1) {
213                         DRW_viewport_matrix_override_set(stl->effects->overide_persmat, DRW_MAT_PERS);
214                         DRW_viewport_matrix_override_set(stl->effects->overide_persinv, DRW_MAT_PERSINV);
215                         DRW_viewport_matrix_override_set(stl->effects->overide_winmat, DRW_MAT_WIN);
216                         DRW_viewport_matrix_override_set(stl->effects->overide_wininv, DRW_MAT_WININV);
217                 }
218
219                 /* Depth prepass */
220                 DRW_stats_group_start("Prepass");
221                 DRW_draw_pass(psl->depth_pass);
222                 DRW_draw_pass(psl->depth_pass_cull);
223                 DRW_stats_group_end();
224
225                 /* Create minmax texture */
226                 DRW_stats_group_start("Main MinMax buffer");
227                 EEVEE_create_minmax_buffer(vedata, dtxl->depth, -1);
228                 DRW_stats_group_end();
229
230                 EEVEE_occlusion_compute(sldata, vedata);
231                 EEVEE_volumes_compute(sldata, vedata);
232
233                 /* Shading pass */
234                 DRW_stats_group_start("Shading");
235                 DRW_draw_pass(psl->background_pass);
236                 EEVEE_draw_default_passes(psl);
237                 DRW_draw_pass(psl->material_pass);
238                 EEVEE_subsurface_data_render(sldata, vedata);
239                 DRW_stats_group_end();
240
241                 /* Effects pre-transparency */
242                 EEVEE_subsurface_compute(sldata, vedata);
243                 EEVEE_reflection_compute(sldata, vedata);
244                 EEVEE_occlusion_draw_debug(sldata, vedata);
245                 DRW_draw_pass(psl->probe_display);
246                 EEVEE_refraction_compute(sldata, vedata);
247
248                 /* Opaque refraction */
249                 DRW_stats_group_start("Opaque Refraction");
250                 DRW_draw_pass(psl->refract_depth_pass);
251                 DRW_draw_pass(psl->refract_depth_pass_cull);
252                 DRW_draw_pass(psl->refract_pass);
253                 DRW_stats_group_end();
254
255                 /* Volumetrics Resolve Opaque */
256                 EEVEE_volumes_resolve(sldata, vedata);
257
258                 /* Transparent */
259                 DRW_pass_sort_shgroup_z(psl->transparent_pass);
260                 DRW_draw_pass(psl->transparent_pass);
261
262                 /* Post Process */
263                 DRW_stats_group_start("Post FX");
264                 EEVEE_draw_effects(vedata);
265                 DRW_stats_group_end();
266
267                 if (stl->effects->taa_current_sample > 1) {
268                         DRW_viewport_matrix_override_unset(DRW_MAT_PERS);
269                         DRW_viewport_matrix_override_unset(DRW_MAT_PERSINV);
270                         DRW_viewport_matrix_override_unset(DRW_MAT_WIN);
271                         DRW_viewport_matrix_override_unset(DRW_MAT_WININV);
272                 }
273         }
274
275         EEVEE_volumes_free_smoke_textures();
276
277         stl->g_data->view_updated = false;
278 }
279
280 static void EEVEE_view_update(void *vedata)
281 {
282         EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
283         if (stl->g_data) {
284                 stl->g_data->view_updated = true;
285         }
286 }
287
288 static void EEVEE_id_update(void *UNUSED(vedata), ID *id)
289 {
290         const ID_Type id_type = GS(id->name);
291         if (id_type == ID_OB) {
292                 Object *object = (Object *)id;
293                 EEVEE_LightProbeEngineData *ped = EEVEE_lightprobe_data_get(object);
294                 if (ped != NULL) {
295                         ped->need_full_update = true;
296                 }
297                 EEVEE_LampEngineData *led = EEVEE_lamp_data_get(object);
298                 if (led != NULL) {
299                         led->need_update = true;
300                 }
301                 EEVEE_ObjectEngineData *oedata = EEVEE_object_data_get(object);
302                 if (oedata != NULL) {
303                         oedata->need_update = true;
304                 }
305         }
306 }
307
308 static void EEVEE_engine_free(void)
309 {
310         EEVEE_bloom_free();
311         EEVEE_depth_of_field_free();
312         EEVEE_effects_free();
313         EEVEE_lightprobes_free();
314         EEVEE_lights_free();
315         EEVEE_materials_free();
316         EEVEE_motion_blur_free();
317         EEVEE_occlusion_free();
318         EEVEE_screen_raytrace_free();
319         EEVEE_subsurface_free();
320         EEVEE_temporal_sampling_free();
321         EEVEE_volumes_free();
322 }
323
324 static void EEVEE_layer_collection_settings_create(RenderEngine *UNUSED(engine), IDProperty *props)
325 {
326         BLI_assert(props &&
327                    props->type == IDP_GROUP &&
328                    props->subtype == IDP_GROUP_SUB_ENGINE_RENDER);
329         // BKE_collection_engine_property_add_int(props, "high_quality_sphere_lamps", false);
330         UNUSED_VARS_NDEBUG(props);
331 }
332
333 static void EEVEE_view_layer_settings_create(RenderEngine *UNUSED(engine), IDProperty *props)
334 {
335         BLI_assert(props &&
336                    props->type == IDP_GROUP &&
337                    props->subtype == IDP_GROUP_SUB_ENGINE_RENDER);
338
339         BKE_collection_engine_property_add_int(props, "gi_diffuse_bounces", 3);
340         BKE_collection_engine_property_add_int(props, "gi_cubemap_resolution", 512);
341
342         BKE_collection_engine_property_add_int(props, "taa_samples", 8);
343
344         BKE_collection_engine_property_add_bool(props, "sss_enable", false);
345         BKE_collection_engine_property_add_int(props, "sss_samples", 7);
346         BKE_collection_engine_property_add_float(props, "sss_jitter_threshold", 0.3f);
347         BKE_collection_engine_property_add_bool(props, "sss_separate_albedo", false);
348
349         BKE_collection_engine_property_add_bool(props, "ssr_enable", false);
350         BKE_collection_engine_property_add_bool(props, "ssr_refraction", false);
351         BKE_collection_engine_property_add_bool(props, "ssr_halfres", true);
352         BKE_collection_engine_property_add_int(props, "ssr_ray_count", 1);
353         BKE_collection_engine_property_add_float(props, "ssr_quality", 0.25f);
354         BKE_collection_engine_property_add_float(props, "ssr_max_roughness", 0.5f);
355         BKE_collection_engine_property_add_float(props, "ssr_thickness", 0.2f);
356         BKE_collection_engine_property_add_float(props, "ssr_border_fade", 0.075f);
357         BKE_collection_engine_property_add_float(props, "ssr_firefly_fac", 10.0f);
358
359         BKE_collection_engine_property_add_bool(props, "volumetric_enable", false);
360         BKE_collection_engine_property_add_float(props, "volumetric_start", 0.1f);
361         BKE_collection_engine_property_add_float(props, "volumetric_end", 100.0f);
362         BKE_collection_engine_property_add_int(props, "volumetric_tile_size", 8);
363         BKE_collection_engine_property_add_int(props, "volumetric_samples", 64);
364         BKE_collection_engine_property_add_float(props, "volumetric_sample_distribution", 0.8f);
365         BKE_collection_engine_property_add_bool(props, "volumetric_lights", true);
366         BKE_collection_engine_property_add_float(props, "volumetric_light_clamp", 0.0f);
367         BKE_collection_engine_property_add_bool(props, "volumetric_shadows", false);
368         BKE_collection_engine_property_add_int(props, "volumetric_shadow_samples", 16);
369         BKE_collection_engine_property_add_bool(props, "volumetric_colored_transmittance", true);
370
371         BKE_collection_engine_property_add_bool(props, "gtao_enable", false);
372         BKE_collection_engine_property_add_bool(props, "gtao_use_bent_normals", true);
373         BKE_collection_engine_property_add_bool(props, "gtao_denoise", true);
374         BKE_collection_engine_property_add_bool(props, "gtao_bounce", true);
375         BKE_collection_engine_property_add_float(props, "gtao_distance", 0.2f);
376         BKE_collection_engine_property_add_float(props, "gtao_factor", 1.0f);
377         BKE_collection_engine_property_add_float(props, "gtao_quality", 0.25f);
378         BKE_collection_engine_property_add_int(props, "gtao_samples", 2);
379
380         BKE_collection_engine_property_add_bool(props, "dof_enable", false);
381         BKE_collection_engine_property_add_float(props, "bokeh_max_size", 100.0f);
382         BKE_collection_engine_property_add_float(props, "bokeh_threshold", 1.0f);
383
384         float default_bloom_color[3] = {1.0f, 1.0f, 1.0f};
385         BKE_collection_engine_property_add_bool(props, "bloom_enable", false);
386         BKE_collection_engine_property_add_float_array(props, "bloom_color", default_bloom_color, 3);
387         BKE_collection_engine_property_add_float(props, "bloom_threshold", 0.8f);
388         BKE_collection_engine_property_add_float(props, "bloom_knee", 0.5f);
389         BKE_collection_engine_property_add_float(props, "bloom_intensity", 0.8f);
390         BKE_collection_engine_property_add_float(props, "bloom_radius", 6.5f);
391         BKE_collection_engine_property_add_float(props, "bloom_clamp", 1.0f);
392
393         BKE_collection_engine_property_add_bool(props, "motion_blur_enable", false);
394         BKE_collection_engine_property_add_int(props, "motion_blur_samples", 8);
395         BKE_collection_engine_property_add_float(props, "motion_blur_shutter", 1.0f);
396
397         BKE_collection_engine_property_add_int(props, "shadow_method", SHADOW_ESM);
398         BKE_collection_engine_property_add_int(props, "shadow_size", 512);
399         BKE_collection_engine_property_add_bool(props, "shadow_high_bitdepth", false);
400 }
401
402 static const DrawEngineDataSize EEVEE_data_size = DRW_VIEWPORT_DATA_SIZE(EEVEE_Data);
403
404 DrawEngineType draw_engine_eevee_type = {
405         NULL, NULL,
406         N_("Eevee"),
407         &EEVEE_data_size,
408         &EEVEE_engine_init,
409         &EEVEE_engine_free,
410         &EEVEE_cache_init,
411         &EEVEE_cache_populate,
412         &EEVEE_cache_finish,
413         &EEVEE_draw_scene,
414         NULL, //&EEVEE_draw_scene
415         &EEVEE_view_update,
416         &EEVEE_id_update,
417 };
418
419 RenderEngineType DRW_engine_viewport_eevee_type = {
420         NULL, NULL,
421         EEVEE_ENGINE, N_("Eevee"), RE_INTERNAL | RE_USE_SHADING_NODES,
422         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
423         &EEVEE_layer_collection_settings_create, &EEVEE_view_layer_settings_create,
424         &draw_engine_eevee_type,
425         {NULL, NULL, NULL}
426 };
427
428
429 #undef EEVEE_ENGINE