Rename any instance of scene layer or render layer in code with view layer
[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_get();
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_get();
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_get();
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                                 EEVEE_ObjectEngineData *oedata = EEVEE_object_data_get(ob);
132                                 oedata->need_update = ((ob->deg_update_flag & DEG_RUNTIME_DATA_UPDATE) != 0);
133                         }
134                 }
135         }
136         else if (ob->type == OB_LIGHTPROBE) {
137                 if ((ob->base_flag & BASE_FROMDUPLI) != 0) {
138                         /* TODO: Special case for dupli objects because we cannot save the object pointer. */
139                 }
140                 else {
141                         EEVEE_lightprobes_cache_add(sldata, ob);
142                 }
143         }
144         else if (ob->type == OB_LAMP) {
145                 if ((ob->base_flag & BASE_FROMDUPLI) != 0) {
146                         /* TODO: Special case for dupli objects because we cannot save the object pointer. */
147                 }
148                 else {
149                         EEVEE_lights_cache_add(sldata, ob);
150                 }
151         }
152 }
153
154 static void EEVEE_cache_finish(void *vedata)
155 {
156         EEVEE_ViewLayerData *sldata = EEVEE_view_layer_data_get();
157
158         EEVEE_materials_cache_finish(vedata);
159         EEVEE_lights_cache_finish(sldata);
160         EEVEE_lightprobes_cache_finish(sldata, vedata);
161 }
162
163 static void EEVEE_draw_scene(void *vedata)
164 {
165         EEVEE_PassList *psl = ((EEVEE_Data *)vedata)->psl;
166         EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
167         EEVEE_FramebufferList *fbl = ((EEVEE_Data *)vedata)->fbl;
168         EEVEE_ViewLayerData *sldata = EEVEE_view_layer_data_get();
169
170         /* Default framebuffer and texture */
171         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
172
173         /* Number of iteration: needed for all temporal effect (SSR, TAA)
174          * when using opengl render. */
175         int loop_ct = DRW_state_is_image_render() ? 4 : 1;
176
177         static float rand = 0.0f;
178
179         /* XXX temp for denoising render. TODO plug number of samples here */
180         if (DRW_state_is_image_render()) {
181                 rand += 1.0f / 16.0f;
182                 rand = rand - floorf(rand);
183
184                 /* Set jitter offset */
185                 EEVEE_update_util_texture(rand);
186         }
187         else if (((stl->effects->enabled_effects & EFFECT_TAA) != 0) && (stl->effects->taa_current_sample > 1)) {
188                 double r;
189                 BLI_halton_1D(2, 0.0, stl->effects->taa_current_sample - 1, &r);
190
191                 /* Set jitter offset */
192                 /* PERF This is killing perf ! */
193                 EEVEE_update_util_texture((float)r);
194         }
195
196         while (loop_ct--) {
197
198                 /* Refresh Probes */
199                 DRW_stats_group_start("Probes Refresh");
200                 EEVEE_lightprobes_refresh(sldata, vedata);
201                 DRW_stats_group_end();
202
203                 /* Refresh shadows */
204                 DRW_stats_group_start("Shadows");
205                 EEVEE_draw_shadows(sldata, psl);
206                 DRW_stats_group_end();
207
208                 /* Attach depth to the hdr buffer and bind it */
209                 DRW_framebuffer_texture_detach(dtxl->depth);
210                 DRW_framebuffer_texture_attach(fbl->main, dtxl->depth, 0, 0);
211                 DRW_framebuffer_bind(fbl->main);
212                 DRW_framebuffer_clear(false, true, true, NULL, 1.0f);
213
214                 if (((stl->effects->enabled_effects & EFFECT_TAA) != 0) && stl->effects->taa_current_sample > 1) {
215                         DRW_viewport_matrix_override_set(stl->effects->overide_persmat, DRW_MAT_PERS);
216                         DRW_viewport_matrix_override_set(stl->effects->overide_persinv, DRW_MAT_PERSINV);
217                         DRW_viewport_matrix_override_set(stl->effects->overide_winmat, DRW_MAT_WIN);
218                         DRW_viewport_matrix_override_set(stl->effects->overide_wininv, DRW_MAT_WININV);
219                 }
220
221                 /* Depth prepass */
222                 DRW_stats_group_start("Prepass");
223                 DRW_draw_pass(psl->depth_pass);
224                 DRW_draw_pass(psl->depth_pass_cull);
225                 DRW_stats_group_end();
226
227                 /* Create minmax texture */
228                 DRW_stats_group_start("Main MinMax buffer");
229                 EEVEE_create_minmax_buffer(vedata, dtxl->depth, -1);
230                 DRW_stats_group_end();
231
232                 EEVEE_occlusion_compute(sldata, vedata);
233                 EEVEE_volumes_compute(sldata, vedata);
234
235                 /* Shading pass */
236                 DRW_stats_group_start("Shading");
237                 DRW_draw_pass(psl->background_pass);
238                 EEVEE_draw_default_passes(psl);
239                 DRW_draw_pass(psl->material_pass);
240                 EEVEE_subsurface_data_render(sldata, vedata);
241                 DRW_stats_group_end();
242
243                 /* Effects pre-transparency */
244                 EEVEE_subsurface_compute(sldata, vedata);
245                 EEVEE_reflection_compute(sldata, vedata);
246                 EEVEE_occlusion_draw_debug(sldata, vedata);
247                 DRW_draw_pass(psl->probe_display);
248                 EEVEE_refraction_compute(sldata, vedata);
249
250                 /* Opaque refraction */
251                 DRW_stats_group_start("Opaque Refraction");
252                 DRW_draw_pass(psl->refract_depth_pass);
253                 DRW_draw_pass(psl->refract_depth_pass_cull);
254                 DRW_draw_pass(psl->refract_pass);
255                 DRW_stats_group_end();
256
257                 /* Volumetrics Resolve Opaque */
258                 EEVEE_volumes_resolve(sldata, vedata);
259
260                 /* Transparent */
261                 DRW_pass_sort_shgroup_z(psl->transparent_pass);
262                 DRW_draw_pass(psl->transparent_pass);
263
264                 /* Post Process */
265                 DRW_stats_group_start("Post FX");
266                 EEVEE_draw_effects(vedata);
267                 DRW_stats_group_end();
268
269                 if (stl->effects->taa_current_sample > 1) {
270                         DRW_viewport_matrix_override_unset(DRW_MAT_PERS);
271                         DRW_viewport_matrix_override_unset(DRW_MAT_PERSINV);
272                         DRW_viewport_matrix_override_unset(DRW_MAT_WIN);
273                         DRW_viewport_matrix_override_unset(DRW_MAT_WININV);
274                 }
275         }
276
277         EEVEE_volumes_free_smoke_textures();
278
279         stl->g_data->view_updated = false;
280 }
281
282 static void EEVEE_view_update(void *vedata)
283 {
284         EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
285         if (stl->g_data) {
286                 stl->g_data->view_updated = true;
287         }
288 }
289
290 static void EEVEE_engine_free(void)
291 {
292         EEVEE_bloom_free();
293         EEVEE_depth_of_field_free();
294         EEVEE_effects_free();
295         EEVEE_lightprobes_free();
296         EEVEE_lights_free();
297         EEVEE_materials_free();
298         EEVEE_motion_blur_free();
299         EEVEE_occlusion_free();
300         EEVEE_screen_raytrace_free();
301         EEVEE_subsurface_free();
302         EEVEE_temporal_sampling_free();
303         EEVEE_volumes_free();
304 }
305
306 static void EEVEE_layer_collection_settings_create(RenderEngine *UNUSED(engine), IDProperty *props)
307 {
308         BLI_assert(props &&
309                    props->type == IDP_GROUP &&
310                    props->subtype == IDP_GROUP_SUB_ENGINE_RENDER);
311         // BKE_collection_engine_property_add_int(props, "high_quality_sphere_lamps", false);
312         UNUSED_VARS_NDEBUG(props);
313 }
314
315 static void EEVEE_view_layer_settings_create(RenderEngine *UNUSED(engine), IDProperty *props)
316 {
317         BLI_assert(props &&
318                    props->type == IDP_GROUP &&
319                    props->subtype == IDP_GROUP_SUB_ENGINE_RENDER);
320
321         BKE_collection_engine_property_add_int(props, "gi_diffuse_bounces", 3);
322         BKE_collection_engine_property_add_int(props, "gi_cubemap_resolution", 512);
323
324         BKE_collection_engine_property_add_int(props, "taa_samples", 8);
325
326         BKE_collection_engine_property_add_bool(props, "sss_enable", false);
327         BKE_collection_engine_property_add_int(props, "sss_samples", 7);
328         BKE_collection_engine_property_add_float(props, "sss_jitter_threshold", 0.3f);
329
330         BKE_collection_engine_property_add_bool(props, "ssr_enable", false);
331         BKE_collection_engine_property_add_bool(props, "ssr_refraction", false);
332         BKE_collection_engine_property_add_bool(props, "ssr_halfres", true);
333         BKE_collection_engine_property_add_int(props, "ssr_ray_count", 1);
334         BKE_collection_engine_property_add_float(props, "ssr_quality", 0.25f);
335         BKE_collection_engine_property_add_float(props, "ssr_max_roughness", 0.5f);
336         BKE_collection_engine_property_add_float(props, "ssr_thickness", 0.2f);
337         BKE_collection_engine_property_add_float(props, "ssr_border_fade", 0.075f);
338         BKE_collection_engine_property_add_float(props, "ssr_firefly_fac", 10.0f);
339
340         BKE_collection_engine_property_add_bool(props, "volumetric_enable", false);
341         BKE_collection_engine_property_add_float(props, "volumetric_start", 0.1f);
342         BKE_collection_engine_property_add_float(props, "volumetric_end", 100.0f);
343         BKE_collection_engine_property_add_int(props, "volumetric_tile_size", 8);
344         BKE_collection_engine_property_add_int(props, "volumetric_samples", 64);
345         BKE_collection_engine_property_add_float(props, "volumetric_sample_distribution", 0.8f);
346         BKE_collection_engine_property_add_bool(props, "volumetric_lights", true);
347         BKE_collection_engine_property_add_float(props, "volumetric_light_clamp", 0.0f);
348         BKE_collection_engine_property_add_bool(props, "volumetric_shadows", false);
349         BKE_collection_engine_property_add_int(props, "volumetric_shadow_samples", 16);
350         BKE_collection_engine_property_add_bool(props, "volumetric_colored_transmittance", true);
351
352         BKE_collection_engine_property_add_bool(props, "gtao_enable", false);
353         BKE_collection_engine_property_add_bool(props, "gtao_use_bent_normals", true);
354         BKE_collection_engine_property_add_bool(props, "gtao_denoise", true);
355         BKE_collection_engine_property_add_bool(props, "gtao_bounce", true);
356         BKE_collection_engine_property_add_float(props, "gtao_distance", 0.2f);
357         BKE_collection_engine_property_add_float(props, "gtao_factor", 1.0f);
358         BKE_collection_engine_property_add_float(props, "gtao_quality", 0.25f);
359         BKE_collection_engine_property_add_int(props, "gtao_samples", 2);
360
361         BKE_collection_engine_property_add_bool(props, "dof_enable", false);
362         BKE_collection_engine_property_add_float(props, "bokeh_max_size", 100.0f);
363         BKE_collection_engine_property_add_float(props, "bokeh_threshold", 1.0f);
364
365         float default_bloom_color[3] = {1.0f, 1.0f, 1.0f};
366         BKE_collection_engine_property_add_bool(props, "bloom_enable", false);
367         BKE_collection_engine_property_add_float_array(props, "bloom_color", default_bloom_color, 3);
368         BKE_collection_engine_property_add_float(props, "bloom_threshold", 0.8f);
369         BKE_collection_engine_property_add_float(props, "bloom_knee", 0.5f);
370         BKE_collection_engine_property_add_float(props, "bloom_intensity", 0.8f);
371         BKE_collection_engine_property_add_float(props, "bloom_radius", 6.5f);
372         BKE_collection_engine_property_add_float(props, "bloom_clamp", 1.0f);
373
374         BKE_collection_engine_property_add_bool(props, "motion_blur_enable", false);
375         BKE_collection_engine_property_add_int(props, "motion_blur_samples", 8);
376         BKE_collection_engine_property_add_float(props, "motion_blur_shutter", 1.0f);
377
378         BKE_collection_engine_property_add_int(props, "shadow_method", SHADOW_ESM);
379         BKE_collection_engine_property_add_int(props, "shadow_size", 512);
380         BKE_collection_engine_property_add_bool(props, "shadow_high_bitdepth", false);
381 }
382
383 static const DrawEngineDataSize EEVEE_data_size = DRW_VIEWPORT_DATA_SIZE(EEVEE_Data);
384
385 DrawEngineType draw_engine_eevee_type = {
386         NULL, NULL,
387         N_("Eevee"),
388         &EEVEE_data_size,
389         &EEVEE_engine_init,
390         &EEVEE_engine_free,
391         &EEVEE_cache_init,
392         &EEVEE_cache_populate,
393         &EEVEE_cache_finish,
394         &EEVEE_draw_scene,
395         NULL, //&EEVEE_draw_scene
396         &EEVEE_view_update,
397 };
398
399 RenderEngineType DRW_engine_viewport_eevee_type = {
400         NULL, NULL,
401         EEVEE_ENGINE, N_("Eevee"), RE_INTERNAL | RE_USE_SHADING_NODES,
402         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
403         &EEVEE_layer_collection_settings_create, &EEVEE_view_layer_settings_create,
404         &draw_engine_eevee_type,
405         {NULL, NULL, NULL}
406 };
407
408
409 #undef EEVEE_ENGINE