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