Eevee: Add Lamp Specular multiplier.
[blender.git] / source / blender / draw / engines / eevee / eevee_lights.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_lights.c
23  *  \ingroup DNA
24  */
25
26 #include "DRW_render.h"
27
28 #include "BLI_dynstr.h"
29 #include "BLI_rect.h"
30
31 #include "BKE_object.h"
32
33 #include "eevee_engine.h"
34 #include "eevee_private.h"
35
36 #define SHADOW_CASTER_ALLOC_CHUNK 16
37
38 static struct {
39         struct GPUShader *shadow_sh;
40         struct GPUShader *shadow_store_cube_sh[SHADOW_METHOD_MAX];
41         struct GPUShader *shadow_store_cascade_sh[SHADOW_METHOD_MAX];
42         struct GPUShader *shadow_copy_cube_sh[SHADOW_METHOD_MAX];
43         struct GPUShader *shadow_copy_cascade_sh[SHADOW_METHOD_MAX];
44 } e_data = {NULL}; /* Engine data */
45
46 extern char datatoc_shadow_vert_glsl[];
47 extern char datatoc_shadow_frag_glsl[];
48 extern char datatoc_shadow_store_frag_glsl[];
49 extern char datatoc_shadow_copy_frag_glsl[];
50 extern char datatoc_concentric_samples_lib_glsl[];
51
52 /* Prototype */
53 static void eevee_light_setup(Object *ob, EEVEE_Light *evli);
54
55 /* *********** LIGHT BITS *********** */
56 static void lightbits_set_single(EEVEE_LightBits *bitf, unsigned int idx, bool val)
57 {
58         if (val) {
59                 bitf->fields[idx / 8] |=  (1 << (idx % 8));
60         }
61         else {
62                 bitf->fields[idx / 8] &= ~(1 << (idx % 8));
63         }
64 }
65
66 static void lightbits_set_all(EEVEE_LightBits *bitf, bool val)
67 {
68         memset(bitf, (val) ? 0xFF : 0x00, sizeof(EEVEE_LightBits));
69 }
70
71 static void lightbits_or(EEVEE_LightBits *r, const EEVEE_LightBits *v)
72 {
73         for (int i = 0; i < MAX_LIGHTBITS_FIELDS; ++i) {
74                 r->fields[i] |= v->fields[i];
75         }
76 }
77
78 static bool lightbits_get(const EEVEE_LightBits *r, unsigned int idx)
79 {
80         return r->fields[idx / 8] & (1 << (idx % 8));
81 }
82
83 static void lightbits_convert(EEVEE_LightBits *r, const EEVEE_LightBits *bitf, const int *light_bit_conv_table, unsigned int table_length)
84 {
85         for (int i = 0; i < table_length; ++i) {
86                 if (lightbits_get(bitf, i) != 0) {
87                         if (light_bit_conv_table[i] >= 0) {
88                                 r->fields[i / 8] |= (1 << (i % 8));
89                         }
90                 }
91         }
92 }
93
94 /* *********** FUNCTIONS *********** */
95
96 void EEVEE_lights_init(EEVEE_ViewLayerData *sldata)
97 {
98         const unsigned int shadow_ubo_size = sizeof(EEVEE_Shadow) * MAX_SHADOW +
99                                              sizeof(EEVEE_ShadowCube) * MAX_SHADOW_CUBE +
100                                              sizeof(EEVEE_ShadowCascade) * MAX_SHADOW_CASCADE;
101
102         const DRWContextState *draw_ctx = DRW_context_state_get();
103         ViewLayer *view_layer = draw_ctx->view_layer;
104         IDProperty *props = BKE_view_layer_engine_evaluated_get(view_layer, COLLECTION_MODE_NONE, RE_engine_id_BLENDER_EEVEE);
105
106         if (!e_data.shadow_sh) {
107                 e_data.shadow_sh = DRW_shader_create(
108                         datatoc_shadow_vert_glsl, NULL, datatoc_shadow_frag_glsl, NULL);
109         }
110
111         if (!sldata->lamps) {
112                 sldata->lamps              = MEM_callocN(sizeof(EEVEE_LampsInfo), "EEVEE_LampsInfo");
113                 sldata->light_ubo          = DRW_uniformbuffer_create(sizeof(EEVEE_Light) * MAX_LIGHT, NULL);
114                 sldata->shadow_ubo         = DRW_uniformbuffer_create(shadow_ubo_size, NULL);
115                 sldata->shadow_render_ubo  = DRW_uniformbuffer_create(sizeof(EEVEE_ShadowRender), NULL);
116
117                 for (int i = 0; i < 2; ++i) {
118                         sldata->shcasters_buffers[i].shadow_casters = MEM_callocN(sizeof(EEVEE_ShadowCaster) * SHADOW_CASTER_ALLOC_CHUNK, "EEVEE_ShadowCaster buf");
119                         sldata->shcasters_buffers[i].flags = MEM_callocN(sizeof(sldata->shcasters_buffers[0].flags) * SHADOW_CASTER_ALLOC_CHUNK, "EEVEE_shcast_buffer flags buf");
120                         sldata->shcasters_buffers[i].alloc_count = SHADOW_CASTER_ALLOC_CHUNK;
121                         sldata->shcasters_buffers[i].count = 0;
122                 }
123
124                 sldata->lamps->shcaster_frontbuffer = &sldata->shcasters_buffers[0];
125                 sldata->lamps->shcaster_backbuffer = &sldata->shcasters_buffers[1];
126         }
127
128         /* Flip buffers */
129         SWAP(EEVEE_ShadowCasterBuffer *, sldata->lamps->shcaster_frontbuffer, sldata->lamps->shcaster_backbuffer);
130
131         int sh_method = BKE_collection_engine_property_value_get_int(props, "shadow_method");
132         int sh_size = BKE_collection_engine_property_value_get_int(props, "shadow_size");
133         int sh_high_bitdepth = BKE_collection_engine_property_value_get_int(props, "shadow_high_bitdepth");
134
135         EEVEE_LampsInfo *linfo = sldata->lamps;
136         if ((linfo->shadow_size != sh_size) ||
137             (linfo->shadow_method != sh_method) ||
138             (linfo->shadow_high_bitdepth != sh_high_bitdepth))
139         {
140                 BLI_assert((sh_size > 0) && (sh_size <= 8192));
141                 DRW_TEXTURE_FREE_SAFE(sldata->shadow_pool);
142                 DRW_TEXTURE_FREE_SAFE(sldata->shadow_cube_target);
143                 DRW_TEXTURE_FREE_SAFE(sldata->shadow_cascade_target);
144                 DRW_TEXTURE_FREE_SAFE(sldata->shadow_cube_blur);
145                 DRW_TEXTURE_FREE_SAFE(sldata->shadow_cascade_blur);
146
147                 linfo->shadow_high_bitdepth = sh_high_bitdepth;
148                 linfo->shadow_method = sh_method;
149                 linfo->shadow_size = sh_size;
150                 linfo->shadow_render_data.stored_texel_size = 1.0 / (float)linfo->shadow_size;
151
152                 /* Compute adequate size for the cubemap render target.
153                  * The 3.0f factor is here to make sure there is no under sampling between
154                  * the octahedron mapping and the cubemap. */
155                 int new_cube_target_size = (int)ceil(sqrt((float)(sh_size * sh_size) / 6.0f) * 3.0f);
156
157                 CLAMP(new_cube_target_size, 1, 4096);
158
159                 linfo->shadow_cube_target_size = new_cube_target_size;
160                 linfo->shadow_render_data.cube_texel_size = 1.0 / (float)linfo->shadow_cube_target_size;
161         }
162
163         /* only compile the ones needed. reduce startup time. */
164         if ((sh_method == SHADOW_ESM) && !e_data.shadow_store_cube_sh[SHADOW_ESM]) {
165                 DynStr *ds_frag = BLI_dynstr_new();
166                 BLI_dynstr_append(ds_frag, datatoc_concentric_samples_lib_glsl);
167                 BLI_dynstr_append(ds_frag, datatoc_shadow_store_frag_glsl);
168                 char *store_shadow_shader_str = BLI_dynstr_get_cstring(ds_frag);
169                 BLI_dynstr_free(ds_frag);
170
171                 e_data.shadow_store_cube_sh[SHADOW_ESM] = DRW_shader_create_fullscreen(
172                         store_shadow_shader_str,
173                         "#define ESM\n");
174                 e_data.shadow_store_cascade_sh[SHADOW_ESM] = DRW_shader_create_fullscreen(
175                         store_shadow_shader_str,
176                         "#define ESM\n"
177                         "#define CSM\n");
178                 MEM_freeN(store_shadow_shader_str);
179
180                 e_data.shadow_copy_cube_sh[SHADOW_ESM] = DRW_shader_create_fullscreen(
181                         datatoc_shadow_copy_frag_glsl,
182                         "#define ESM\n"
183                         "#define COPY\n");
184                 e_data.shadow_copy_cascade_sh[SHADOW_ESM] = DRW_shader_create_fullscreen(
185                         datatoc_shadow_copy_frag_glsl,
186                         "#define ESM\n"
187                         "#define COPY\n"
188                         "#define CSM\n");
189         }
190         else if ((sh_method == SHADOW_VSM) && !e_data.shadow_store_cube_sh[SHADOW_VSM]) {
191                 DynStr *ds_frag = BLI_dynstr_new();
192                 BLI_dynstr_append(ds_frag, datatoc_concentric_samples_lib_glsl);
193                 BLI_dynstr_append(ds_frag, datatoc_shadow_store_frag_glsl);
194                 char *store_shadow_shader_str = BLI_dynstr_get_cstring(ds_frag);
195                 BLI_dynstr_free(ds_frag);
196
197                 e_data.shadow_store_cube_sh[SHADOW_VSM] = DRW_shader_create_fullscreen(
198                         store_shadow_shader_str,
199                         "#define VSM\n");
200                 e_data.shadow_store_cascade_sh[SHADOW_VSM] = DRW_shader_create_fullscreen(
201                         store_shadow_shader_str,
202                         "#define VSM\n"
203                         "#define CSM\n");
204                 MEM_freeN(store_shadow_shader_str);
205
206                 e_data.shadow_copy_cube_sh[SHADOW_VSM] = DRW_shader_create_fullscreen(
207                         datatoc_shadow_copy_frag_glsl,
208                         "#define VSM\n"
209                         "#define COPY\n");
210                 e_data.shadow_copy_cascade_sh[SHADOW_VSM] = DRW_shader_create_fullscreen(
211                         datatoc_shadow_copy_frag_glsl,
212                         "#define VSM\n"
213                         "#define COPY\n"
214                         "#define CSM\n");
215         }
216 }
217
218 void EEVEE_lights_cache_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
219 {
220         EEVEE_LampsInfo *linfo = sldata->lamps;
221         EEVEE_StorageList *stl = vedata->stl;
222         EEVEE_PassList *psl = vedata->psl;
223
224         linfo->shcaster_frontbuffer->count = 0;
225         linfo->num_light = 0;
226         linfo->num_layer = 0;
227         linfo->gpu_cube_ct = linfo->gpu_cascade_ct = linfo->gpu_shadow_ct = 0;
228         linfo->cpu_cube_ct = linfo->cpu_cascade_ct = 0;
229         memset(linfo->light_ref, 0, sizeof(linfo->light_ref));
230         memset(linfo->shadow_cube_ref, 0, sizeof(linfo->shadow_cube_ref));
231         memset(linfo->shadow_cascade_ref, 0, sizeof(linfo->shadow_cascade_ref));
232         memset(linfo->new_shadow_id, -1, sizeof(linfo->new_shadow_id));
233
234         /* Shadow Casters: Reset flags. */
235         memset(linfo->shcaster_backbuffer->flags, (char)SHADOW_CASTER_PRUNED, linfo->shcaster_backbuffer->alloc_count);
236         memset(linfo->shcaster_frontbuffer->flags, 0x00, linfo->shcaster_frontbuffer->alloc_count);
237
238         {
239                 psl->shadow_cube_store_pass = DRW_pass_create("Shadow Storage Pass", DRW_STATE_WRITE_COLOR);
240
241                 DRWShadingGroup *grp = DRW_shgroup_create(
242                         e_data.shadow_store_cube_sh[linfo->shadow_method], psl->shadow_cube_store_pass);
243                 DRW_shgroup_uniform_texture_ref(grp, "shadowTexture", &sldata->shadow_cube_blur);
244                 DRW_shgroup_uniform_block(grp, "shadow_render_block", sldata->shadow_render_ubo);
245                 DRW_shgroup_uniform_float(grp, "shadowFilterSize", &linfo->filter_size, 1);
246                 DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
247         }
248
249         {
250                 psl->shadow_cascade_store_pass = DRW_pass_create("Shadow Cascade Storage Pass", DRW_STATE_WRITE_COLOR);
251
252                 DRWShadingGroup *grp = DRW_shgroup_create(
253                         e_data.shadow_store_cascade_sh[linfo->shadow_method], psl->shadow_cascade_store_pass);
254                 DRW_shgroup_uniform_texture_ref(grp, "shadowTexture", &sldata->shadow_cascade_blur);
255                 DRW_shgroup_uniform_block(grp, "shadow_render_block", sldata->shadow_render_ubo);
256                 DRW_shgroup_uniform_int(grp, "cascadeId", &linfo->current_shadow_cascade, 1);
257                 DRW_shgroup_uniform_float(grp, "shadowFilterSize", &linfo->filter_size, 1);
258                 DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
259         }
260
261         {
262                 psl->shadow_cube_copy_pass = DRW_pass_create("Shadow Copy Pass", DRW_STATE_WRITE_COLOR);
263
264                 DRWShadingGroup *grp = DRW_shgroup_create(
265                         e_data.shadow_copy_cube_sh[linfo->shadow_method], psl->shadow_cube_copy_pass);
266                 DRW_shgroup_uniform_texture_ref(grp, "shadowTexture", &sldata->shadow_cube_target);
267                 DRW_shgroup_uniform_block(grp, "shadow_render_block", sldata->shadow_render_ubo);
268                 DRW_shgroup_uniform_float(grp, "shadowFilterSize", &linfo->filter_size, 1);
269                 DRW_shgroup_uniform_int(grp, "faceId", &linfo->current_shadow_face, 1);
270                 DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
271         }
272
273         {
274                 psl->shadow_cascade_copy_pass = DRW_pass_create("Shadow Cascade Copy Pass", DRW_STATE_WRITE_COLOR);
275
276                 DRWShadingGroup *grp = DRW_shgroup_create(
277                         e_data.shadow_copy_cascade_sh[linfo->shadow_method], psl->shadow_cascade_copy_pass);
278                 DRW_shgroup_uniform_texture_ref(grp, "shadowTexture", &sldata->shadow_cascade_target);
279                 DRW_shgroup_uniform_block(grp, "shadow_render_block", sldata->shadow_render_ubo);
280                 DRW_shgroup_uniform_float(grp, "shadowFilterSize", &linfo->filter_size, 1);
281                 DRW_shgroup_uniform_int(grp, "cascadeId", &linfo->current_shadow_cascade, 1);
282                 DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
283         }
284
285         {
286                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS;
287                 psl->shadow_pass = DRW_pass_create("Shadow Pass", state);
288
289                 stl->g_data->shadow_shgrp = DRW_shgroup_create(e_data.shadow_sh, psl->shadow_pass);
290         }
291 }
292
293 void EEVEE_lights_cache_add(EEVEE_ViewLayerData *sldata, Object *ob)
294 {
295         EEVEE_LampsInfo *linfo = sldata->lamps;
296
297         /* Step 1 find all lamps in the scene and setup them */
298         if (linfo->num_light >= MAX_LIGHT) {
299                 printf("Too many lamps in the scene !!!\n");
300         }
301         else {
302                 Lamp *la = (Lamp *)ob->data;
303                 EEVEE_Light *evli = linfo->light_data + linfo->num_light;
304                 eevee_light_setup(ob, evli);
305
306                 /* We do not support shadowmaps for dupli lamps. */
307                 if ((ob->base_flag & BASE_FROMDUPLI) != 0) {
308                         linfo->num_light++;
309                         return;
310                 }
311
312                 EEVEE_LampEngineData *led = EEVEE_lamp_data_ensure(ob);
313
314                 /* Save previous shadow id. */
315                 int prev_cube_sh_id = led->prev_cube_shadow_id;
316
317                 /* Default light without shadows */
318                 led->data.ld.shadow_id = -1;
319                 led->prev_cube_shadow_id = -1;
320
321                 if (la->mode & LA_SHADOW) {
322                         if (la->type == LA_SUN) {
323                                 int sh_nbr = 1; /* TODO : MSM */
324                                 int cascade_nbr = MAX_CASCADE_NUM; /* TODO : Custom cascade number */
325
326                                 if ((linfo->gpu_cascade_ct + sh_nbr) <= MAX_SHADOW_CASCADE) {
327                                         /* Save Light object. */
328                                         linfo->shadow_cascade_ref[linfo->cpu_cascade_ct] = ob;
329
330                                         /* Store indices. */
331                                         EEVEE_ShadowCascadeData *data = &led->data.scad;
332                                         data->shadow_id = linfo->gpu_shadow_ct;
333                                         data->cascade_id = linfo->gpu_cascade_ct;
334                                         data->layer_id = linfo->num_layer;
335
336                                         /* Increment indices. */
337                                         linfo->gpu_shadow_ct += 1;
338                                         linfo->gpu_cascade_ct += sh_nbr;
339                                         linfo->num_layer += sh_nbr * cascade_nbr;
340
341                                         linfo->cpu_cascade_ct += 1;
342                                 }
343                         }
344                         else if (la->type == LA_SPOT || la->type == LA_LOCAL || la->type == LA_AREA) {
345                                 int sh_nbr = 1; /* TODO : MSM */
346
347                                 if ((linfo->gpu_cube_ct + sh_nbr) <= MAX_SHADOW_CUBE) {
348                                         /* Save Light object. */
349                                         linfo->shadow_cube_ref[linfo->cpu_cube_ct] = ob;
350
351                                         /* For light update tracking. */
352                                         if ((prev_cube_sh_id >= 0) &&
353                                             (prev_cube_sh_id < linfo->shcaster_backbuffer->count))
354                                         {
355                                                 linfo->new_shadow_id[prev_cube_sh_id] = linfo->cpu_cube_ct;
356                                         }
357                                         led->prev_cube_shadow_id = linfo->cpu_cube_ct;
358
359                                         /* Saving lamp bounds for later. */
360                                         BLI_assert(linfo->cpu_cube_ct >= 0 && linfo->cpu_cube_ct < MAX_LIGHT);
361                                         copy_v3_v3(linfo->shadow_bounds[linfo->cpu_cube_ct].center, ob->obmat[3]);
362                                         linfo->shadow_bounds[linfo->cpu_cube_ct].radius = la->clipend;
363
364                                         EEVEE_ShadowCubeData *data = &led->data.scd;
365                                         /* Store indices. */
366                                         data->shadow_id = linfo->gpu_shadow_ct;
367                                         data->cube_id = linfo->gpu_cube_ct;
368                                         data->layer_id = linfo->num_layer;
369
370                                         /* Increment indices. */
371                                         linfo->gpu_shadow_ct += 1;
372                                         linfo->gpu_cube_ct += sh_nbr;
373                                         linfo->num_layer += sh_nbr;
374
375                                         linfo->cpu_cube_ct += 1;
376                                 }
377                         }
378                 }
379
380                 led->data.ld.light_id = linfo->num_light;
381                 linfo->light_ref[linfo->num_light] = ob;
382                 linfo->num_light++;
383         }
384 }
385
386 /* Add a shadow caster to the shadowpasses */
387 void EEVEE_lights_cache_shcaster_add(
388         EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_StorageList *stl, struct Gwn_Batch *geom, Object *ob)
389 {
390         DRW_shgroup_call_object_add(
391                 stl->g_data->shadow_shgrp,
392                 geom, ob);
393 }
394
395 void EEVEE_lights_cache_shcaster_material_add(
396         EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_PassList *psl, struct GPUMaterial *gpumat,
397         struct Gwn_Batch *geom, struct Object *ob, float *alpha_threshold)
398 {
399         /* TODO / PERF : reuse the same shading group for objects with the same material */
400         DRWShadingGroup *grp = DRW_shgroup_material_create(gpumat, psl->shadow_pass);
401
402         if (grp == NULL) return;
403
404         if (alpha_threshold != NULL)
405                 DRW_shgroup_uniform_float(grp, "alphaThreshold", alpha_threshold, 1);
406
407         DRW_shgroup_call_object_add(grp, geom, ob);
408 }
409
410 /* Make that object update shadow casting lamps inside its influence bounding box. */
411 void EEVEE_lights_cache_shcaster_object_add(EEVEE_ViewLayerData *sldata, Object *ob)
412 {
413         if ((ob->base_flag & BASE_FROMDUPLI) != 0) {
414                 /* TODO: Special case for dupli objects because we cannot save the object pointer. */
415                 return;
416         }
417
418         EEVEE_ObjectEngineData *oedata = EEVEE_object_data_ensure(ob);
419         EEVEE_LampsInfo *linfo = sldata->lamps;
420         EEVEE_ShadowCasterBuffer *backbuffer = linfo->shcaster_backbuffer;
421         EEVEE_ShadowCasterBuffer *frontbuffer = linfo->shcaster_frontbuffer;
422         int past_id = oedata->shadow_caster_id;
423
424         /* Update flags in backbuffer. */
425         if (past_id > -1 && past_id < backbuffer->count) {
426                 backbuffer->flags[past_id] &= ~SHADOW_CASTER_PRUNED;
427
428                 if (oedata->need_update) {
429                         backbuffer->flags[past_id] |= SHADOW_CASTER_UPDATED;
430                 }
431         }
432
433         /* Update id. */
434         oedata->shadow_caster_id = frontbuffer->count++;
435
436         /* Make sure shadow_casters is big enough. */
437         if (oedata->shadow_caster_id >= frontbuffer->alloc_count) {
438                 frontbuffer->alloc_count += SHADOW_CASTER_ALLOC_CHUNK;
439                 frontbuffer->shadow_casters = MEM_reallocN(frontbuffer->shadow_casters, sizeof(EEVEE_ShadowCaster) * frontbuffer->alloc_count);
440                 frontbuffer->flags = MEM_reallocN(frontbuffer->flags, sizeof(EEVEE_ShadowCaster) * frontbuffer->alloc_count);
441         }
442
443         EEVEE_ShadowCaster *shcaster = frontbuffer->shadow_casters + oedata->shadow_caster_id;
444
445         if (oedata->need_update) {
446                 frontbuffer->flags[oedata->shadow_caster_id] = SHADOW_CASTER_UPDATED;
447         }
448
449         /* Update World AABB in frontbuffer. */
450         BoundBox *bb = BKE_object_boundbox_get(ob);
451         float min[3], max[3];
452         INIT_MINMAX(min, max);
453         for (int i = 0; i < 8; ++i) {
454                 float vec[3];
455                 copy_v3_v3(vec, bb->vec[i]);
456                 mul_m4_v3(ob->obmat, vec);
457                 minmax_v3v3_v3(min, max, vec);
458         }
459
460         EEVEE_BoundBox *aabb = &shcaster->bbox;
461         add_v3_v3v3(aabb->center, min, max);
462         mul_v3_fl(aabb->center, 0.5f);
463         sub_v3_v3v3(aabb->halfdim, aabb->center, max);
464
465         aabb->halfdim[0] = fabsf(aabb->halfdim[0]);
466         aabb->halfdim[1] = fabsf(aabb->halfdim[1]);
467         aabb->halfdim[2] = fabsf(aabb->halfdim[2]);
468
469         oedata->need_update = false;
470 }
471
472 void EEVEE_lights_cache_finish(EEVEE_ViewLayerData *sldata)
473 {
474         EEVEE_LampsInfo *linfo = sldata->lamps;
475         GPUTextureFormat shadow_pool_format = GPU_R32F;
476
477         sldata->common_data.la_num_light = linfo->num_light;
478
479         /* Setup enough layers. */
480         /* Free textures if number mismatch. */
481         if (linfo->num_layer != linfo->cache_num_layer) {
482                 DRW_TEXTURE_FREE_SAFE(sldata->shadow_pool);
483                 linfo->cache_num_layer = linfo->num_layer;
484                 linfo->update_flag |= LIGHT_UPDATE_SHADOW_CUBE;
485         }
486
487         switch (linfo->shadow_method) {
488                 case SHADOW_ESM: shadow_pool_format = ((linfo->shadow_high_bitdepth) ? GPU_R32F : GPU_R16F); break;
489                 case SHADOW_VSM: shadow_pool_format = ((linfo->shadow_high_bitdepth) ? GPU_RG32F : GPU_RG16F); break;
490                 default:
491                         BLI_assert(!"Incorrect Shadow Method");
492                         break;
493         }
494
495         if (!sldata->shadow_cube_target) {
496                 /* TODO render everything on the same 2d render target using clip planes and no Geom Shader. */
497                 /* Cubemaps */
498                 sldata->shadow_cube_target = DRW_texture_create_cube(
499                         linfo->shadow_cube_target_size, GPU_DEPTH_COMPONENT24, 0, NULL);
500                 sldata->shadow_cube_blur = DRW_texture_create_cube(
501                         linfo->shadow_cube_target_size, shadow_pool_format, DRW_TEX_FILTER, NULL);
502         }
503
504         if (!sldata->shadow_cascade_target) {
505                 /* CSM */
506                 sldata->shadow_cascade_target = DRW_texture_create_2D_array(
507                         linfo->shadow_size, linfo->shadow_size, MAX_CASCADE_NUM, GPU_DEPTH_COMPONENT24, 0, NULL);
508                 sldata->shadow_cascade_blur = DRW_texture_create_2D_array(
509                         linfo->shadow_size, linfo->shadow_size, MAX_CASCADE_NUM, shadow_pool_format, DRW_TEX_FILTER, NULL);
510         }
511
512         if (!sldata->shadow_pool) {
513                 /* All shadows fit in this array */
514                 sldata->shadow_pool = DRW_texture_create_2D_array(
515                         linfo->shadow_size, linfo->shadow_size, max_ff(1, linfo->num_layer),
516                         shadow_pool_format, DRW_TEX_FILTER, NULL);
517         }
518
519         /* Render FB */
520         GPU_framebuffer_ensure_config(&sldata->shadow_cube_target_fb, {
521                 GPU_ATTACHMENT_TEXTURE(sldata->shadow_cube_target)
522         });
523         GPU_framebuffer_ensure_config(&sldata->shadow_cascade_target_fb, {
524                 GPU_ATTACHMENT_TEXTURE(sldata->shadow_cascade_target)
525         });
526
527         /* Storage FB */
528         GPU_framebuffer_ensure_config(&sldata->shadow_store_fb, {
529                 GPU_ATTACHMENT_NONE,
530                 GPU_ATTACHMENT_TEXTURE(sldata->shadow_pool)
531         });
532
533         /* Update Lamps UBOs. */
534         EEVEE_lights_update(sldata);
535 }
536
537 /* Update buffer with lamp data */
538 static void eevee_light_setup(Object *ob, EEVEE_Light *evli)
539 {
540         Lamp *la = (Lamp *)ob->data;
541         float mat[4][4], scale[3], power;
542
543         /* Position */
544         copy_v3_v3(evli->position, ob->obmat[3]);
545
546         /* Color */
547         copy_v3_v3(evli->color, &la->r);
548
549         evli->spec = la->spec_fac;
550
551         /* Influence Radius */
552         evli->dist = la->dist;
553
554         /* Vectors */
555         normalize_m4_m4_ex(mat, ob->obmat, scale);
556         copy_v3_v3(evli->forwardvec, mat[2]);
557         normalize_v3(evli->forwardvec);
558         negate_v3(evli->forwardvec);
559
560         copy_v3_v3(evli->rightvec, mat[0]);
561         normalize_v3(evli->rightvec);
562
563         copy_v3_v3(evli->upvec, mat[1]);
564         normalize_v3(evli->upvec);
565
566         /* Spot size & blend */
567         if (la->type == LA_SPOT) {
568                 evli->sizex = scale[0] / scale[2];
569                 evli->sizey = scale[1] / scale[2];
570                 evli->spotsize = cosf(la->spotsize * 0.5f);
571                 evli->spotblend = (1.0f - evli->spotsize) * la->spotblend;
572                 evli->radius = max_ff(0.001f, la->area_size);
573         }
574         else if (la->type == LA_AREA) {
575                 evli->sizex = max_ff(0.0001f, la->area_size * scale[0] * 0.5f);
576                 if (la->area_shape == LA_AREA_RECT) {
577                         evli->sizey = max_ff(0.0001f, la->area_sizey * scale[1] * 0.5f);
578                 }
579                 else {
580                         evli->sizey = max_ff(0.0001f, la->area_size * scale[1] * 0.5f);
581                 }
582         }
583         else {
584                 evli->radius = max_ff(0.001f, la->area_size);
585         }
586
587         /* Make illumination power constant */
588         if (la->type == LA_AREA) {
589                 power = 1.0f / (evli->sizex * evli->sizey * 4.0f * M_PI) * /* 1/(w*h*Pi) */
590                         80.0f; /* XXX : Empirical, Fit cycles power */
591         }
592         else if (la->type == LA_SPOT || la->type == LA_LOCAL) {
593                 power = 1.0f / (4.0f * evli->radius * evli->radius * M_PI * M_PI) * /* 1/(4*r²*Pi²) */
594                         M_PI * M_PI * 10.0; /* XXX : Empirical, Fit cycles power */
595
596                 /* for point lights (a.k.a radius == 0.0) */
597                 // power = M_PI * M_PI * 0.78; /* XXX : Empirical, Fit cycles power */
598         }
599         else {
600                 power = 1.0f / (4.0f * evli->radius * evli->radius * M_PI * M_PI) * /* 1/(r²*Pi) */
601                         12.5f; /* XXX : Empirical, Fit cycles power */
602         }
603         mul_v3_fl(evli->color, power * la->energy);
604
605         /* Lamp Type */
606         evli->lamptype = (float)la->type;
607
608         /* No shadow by default */
609         evli->shadowid = -1.0f;
610 }
611
612 static void eevee_shadow_cube_setup(Object *ob, EEVEE_LampsInfo *linfo, EEVEE_LampEngineData *led)
613 {
614         EEVEE_ShadowCubeData *sh_data = &led->data.scd;
615         EEVEE_Light *evli = linfo->light_data + sh_data->light_id;
616         EEVEE_Shadow *ubo_data = linfo->shadow_data + sh_data->shadow_id;
617         EEVEE_ShadowCube *cube_data = linfo->shadow_cube_data + sh_data->cube_id;
618         Lamp *la = (Lamp *)ob->data;
619
620         int sh_nbr = 1; /* TODO: MSM */
621
622         for (int i = 0; i < sh_nbr; ++i) {
623                 /* TODO : choose MSM sample point here. */
624                 copy_v3_v3(cube_data->position, ob->obmat[3]);
625         }
626
627         ubo_data->bias = 0.05f * la->bias;
628         ubo_data->near = la->clipsta;
629         ubo_data->far = la->clipend;
630         ubo_data->exp = (linfo->shadow_method == SHADOW_VSM) ? la->bleedbias : la->bleedexp;
631
632         evli->shadowid = (float)(sh_data->shadow_id);
633         ubo_data->shadow_start = (float)(sh_data->layer_id);
634         ubo_data->data_start = (float)(sh_data->cube_id);
635         ubo_data->multi_shadow_count = (float)(sh_nbr);
636         ubo_data->shadow_blur = la->soft * 0.02f; /* Used by translucence shadowmap blur */
637
638         ubo_data->contact_dist = (la->mode & LA_SHAD_CONTACT) ? la->contact_dist : 0.0f;
639         ubo_data->contact_bias = 0.05f * la->contact_bias;
640         ubo_data->contact_spread = la->contact_spread;
641         ubo_data->contact_thickness = la->contact_thickness;
642 }
643
644 #define LERP(t, a, b) ((a) + (t) * ((b) - (a)))
645
646 static double round_to_digits(double value, int digits)
647 {
648         double factor = pow(10.0, digits - ceil(log10(fabs(value))));
649         return round(value * factor) / factor;
650 }
651
652 static void frustum_min_bounding_sphere(const float corners[8][3], float r_center[3], float *r_radius)
653 {
654 #if 0 /* Simple solution but waste too much space. */
655         float minvec[3], maxvec[3];
656
657         /* compute the bounding box */
658         INIT_MINMAX(minvec, maxvec);
659         for (int i = 0; i < 8; ++i) {
660                 minmax_v3v3_v3(minvec, maxvec, corners[i]);
661         }
662
663         /* compute the bounding sphere of this box */
664         r_radius = len_v3v3(minvec, maxvec) * 0.5f;
665         add_v3_v3v3(r_center, minvec, maxvec);
666         mul_v3_fl(r_center, 0.5f);
667 #else
668         /* Find averaged center. */
669         zero_v3(r_center);
670         for (int i = 0; i < 8; ++i) {
671                 add_v3_v3(r_center, corners[i]);
672         }
673         mul_v3_fl(r_center, 1.0f / 8.0f);
674
675         /* Search the largest distance from the sphere center. */
676         *r_radius = 0.0f;
677         for (int i = 0; i < 8; ++i) {
678                 float rad = len_squared_v3v3(corners[i], r_center);
679                 if (rad > *r_radius) {
680                         *r_radius = rad;
681                 }
682         }
683
684         /* TODO try to reduce the radius further by moving the center.
685          * Remember we need a __stable__ solution! */
686
687         /* Try to reduce float imprecision leading to shimmering. */
688         *r_radius = (float)round_to_digits(sqrtf(*r_radius), 3);
689 #endif
690 }
691
692 static void eevee_shadow_cascade_setup(Object *ob, EEVEE_LampsInfo *linfo, EEVEE_LampEngineData *led)
693 {
694         Lamp *la = (Lamp *)ob->data;
695
696         /* Camera Matrices */
697         float persmat[4][4], persinv[4][4];
698         float viewprojmat[4][4], projinv[4][4];
699         float view_near, view_far;
700         float near_v[4] = {0.0f, 0.0f, -1.0f, 1.0f};
701         float far_v[4] = {0.0f, 0.0f,  1.0f, 1.0f};
702         bool is_persp = DRW_viewport_is_persp_get();
703         DRW_viewport_matrix_get(persmat, DRW_MAT_PERS);
704         invert_m4_m4(persinv, persmat);
705         /* FIXME : Get near / far from Draw manager? */
706         DRW_viewport_matrix_get(viewprojmat, DRW_MAT_WIN);
707         invert_m4_m4(projinv, viewprojmat);
708         mul_m4_v4(projinv, near_v);
709         mul_m4_v4(projinv, far_v);
710         view_near = near_v[2];
711         view_far = far_v[2]; /* TODO: Should be a shadow parameter */
712         if (is_persp) {
713                 view_near /= near_v[3];
714                 view_far /= far_v[3];
715         }
716
717         /* Lamps Matrices */
718         int sh_nbr = 1; /* TODO : MSM */
719         int cascade_nbr = la->cascade_count;
720
721         EEVEE_ShadowCascadeData *sh_data = &led->data.scad;
722         EEVEE_Light *evli = linfo->light_data + sh_data->light_id;
723         EEVEE_Shadow *ubo_data = linfo->shadow_data + sh_data->shadow_id;
724         EEVEE_ShadowCascade *cascade_data = linfo->shadow_cascade_data + sh_data->cascade_id;
725
726         /* obmat = Object Space > World Space */
727         /* viewmat = World Space > View Space */
728         float (*viewmat)[4] = sh_data->viewmat;
729         invert_m4_m4(viewmat, ob->obmat);
730         normalize_m4(viewmat);
731         invert_m4_m4(sh_data->viewinv, viewmat);
732
733         /* The technique consists into splitting
734          * the view frustum into several sub-frustum
735          * that are individually receiving one shadow map */
736
737         float csm_start, csm_end;
738
739         if (is_persp) {
740                 csm_start = view_near;
741                 csm_end = max_ff(view_far, -la->cascade_max_dist);
742                 /* Avoid artifacts */
743                 csm_end = min_ff(view_near, csm_end);
744         }
745         else {
746                 csm_start = -view_far;
747                 csm_end = view_far;
748         }
749
750         /* init near/far */
751         for (int c = 0; c < MAX_CASCADE_NUM; ++c) {
752                 cascade_data->split_start[c] = csm_end;
753                 cascade_data->split_end[c] = csm_end;
754         }
755
756         /* Compute split planes */
757         float splits_start_ndc[MAX_CASCADE_NUM];
758         float splits_end_ndc[MAX_CASCADE_NUM];
759
760         {
761                 /* Nearest plane */
762                 float p[4] = {1.0f, 1.0f, csm_start, 1.0f};
763                 /* TODO: we don't need full m4 multiply here */
764                 mul_m4_v4(viewprojmat, p);
765                 splits_start_ndc[0] = p[2];
766                 if (is_persp) {
767                         splits_start_ndc[0] /= p[3];
768                 }
769         }
770
771         {
772                 /* Farthest plane */
773                 float p[4] = {1.0f, 1.0f, csm_end, 1.0f};
774                 /* TODO: we don't need full m4 multiply here */
775                 mul_m4_v4(viewprojmat, p);
776                 splits_end_ndc[cascade_nbr - 1] = p[2];
777                 if (is_persp) {
778                         splits_end_ndc[cascade_nbr - 1] /= p[3];
779                 }
780         }
781
782         cascade_data->split_start[0] = csm_start;
783         cascade_data->split_end[cascade_nbr - 1] = csm_end;
784
785         for (int c = 1; c < cascade_nbr; ++c) {
786                 /* View Space */
787                 float linear_split = LERP(((float)(c) / (float)cascade_nbr), csm_start, csm_end);
788                 float exp_split = csm_start * powf(csm_end / csm_start, (float)(c) / (float)cascade_nbr);
789
790                 if (is_persp) {
791                         cascade_data->split_start[c] = LERP(la->cascade_exponent, linear_split, exp_split);
792                 }
793                 else {
794                         cascade_data->split_start[c] = linear_split;
795                 }
796                 cascade_data->split_end[c - 1] = cascade_data->split_start[c];
797
798                 /* Add some overlap for smooth transition */
799                 cascade_data->split_start[c] = LERP(la->cascade_fade, cascade_data->split_end[c - 1],
800                                                     (c > 1) ? cascade_data->split_end[c - 2] : cascade_data->split_start[0]);
801
802                 /* NDC Space */
803                 {
804                         float p[4] = {1.0f, 1.0f, cascade_data->split_start[c], 1.0f};
805                         /* TODO: we don't need full m4 multiply here */
806                         mul_m4_v4(viewprojmat, p);
807                         splits_start_ndc[c] = p[2];
808
809                         if (is_persp) {
810                                 splits_start_ndc[c] /= p[3];
811                         }
812                 }
813
814                 {
815                         float p[4] = {1.0f, 1.0f, cascade_data->split_end[c - 1], 1.0f};
816                         /* TODO: we don't need full m4 multiply here */
817                         mul_m4_v4(viewprojmat, p);
818                         splits_end_ndc[c - 1] = p[2];
819
820                         if (is_persp) {
821                                 splits_end_ndc[c - 1] /= p[3];
822                         }
823                 }
824         }
825
826         /* Set last cascade split fade distance into the first split_start. */
827         float prev_split = (cascade_nbr > 1) ? cascade_data->split_end[cascade_nbr - 2] : cascade_data->split_start[0];
828         cascade_data->split_start[0] = LERP(la->cascade_fade, cascade_data->split_end[cascade_nbr - 1], prev_split);
829
830         /* For each cascade */
831         for (int c = 0; c < cascade_nbr; ++c) {
832                 float (*projmat)[4] = sh_data->projmat[c];
833                 /* Given 8 frustum corners */
834                 float corners[8][3] = {
835                         /* Near Cap */
836                         {-1.0f, -1.0f, splits_start_ndc[c]},
837                         { 1.0f, -1.0f, splits_start_ndc[c]},
838                         {-1.0f,  1.0f, splits_start_ndc[c]},
839                         { 1.0f,  1.0f, splits_start_ndc[c]},
840                         /* Far Cap */
841                         {-1.0f, -1.0f, splits_end_ndc[c]},
842                         { 1.0f, -1.0f, splits_end_ndc[c]},
843                         {-1.0f,  1.0f, splits_end_ndc[c]},
844                         { 1.0f,  1.0f, splits_end_ndc[c]}
845                 };
846
847                 /* Transform them into world space */
848                 for (int i = 0; i < 8; ++i) {
849                         mul_project_m4_v3(persinv, corners[i]);
850                 }
851
852                 float center[3];
853                 frustum_min_bounding_sphere(corners, center, &(sh_data->radius[c]));
854
855                 /* Project into lightspace */
856                 mul_mat3_m4_v3(viewmat, center);
857
858                 /* Snap projection center to nearest texel to cancel shimmering. */
859                 float shadow_origin[2], shadow_texco[2];
860                 /* Light to texture space. */
861                 mul_v2_v2fl(shadow_origin, center, linfo->shadow_size / (2.0f * sh_data->radius[c]));
862
863                 /* Find the nearest texel. */
864                 shadow_texco[0] = roundf(shadow_origin[0]);
865                 shadow_texco[1] = roundf(shadow_origin[1]);
866
867                 /* Compute offset. */
868                 sub_v2_v2(shadow_texco, shadow_origin);
869                 mul_v2_fl(shadow_texco, (2.0f * sh_data->radius[c]) / linfo->shadow_size); /* Texture to light space. */
870
871                 /* Apply offset. */
872                 add_v2_v2(center, shadow_texco);
873
874                 /* Expand the projection to cover frustum range */
875                 rctf rect_cascade;
876                 BLI_rctf_init_pt_radius(&rect_cascade, center, sh_data->radius[c]);
877                 orthographic_m4(projmat,
878                                 rect_cascade.xmin, rect_cascade.xmax,
879                                 rect_cascade.ymin, rect_cascade.ymax,
880                                 la->clipsta, la->clipend);
881
882                 mul_m4_m4m4(sh_data->viewprojmat[c], projmat, viewmat);
883                 mul_m4_m4m4(cascade_data->shadowmat[c], texcomat, sh_data->viewprojmat[c]);
884         }
885
886         ubo_data->bias = 0.05f * la->bias;
887         ubo_data->near = la->clipsta;
888         ubo_data->far = la->clipend;
889         ubo_data->exp = (linfo->shadow_method == SHADOW_VSM) ? la->bleedbias : la->bleedexp;
890
891         evli->shadowid = (float)(sh_data->shadow_id);
892         ubo_data->shadow_start = (float)(sh_data->layer_id);
893         ubo_data->data_start = (float)(sh_data->cascade_id);
894         ubo_data->multi_shadow_count = (float)(sh_nbr);
895         ubo_data->shadow_blur = la->soft * 0.02f; /* Used by translucence shadowmap blur */
896
897         ubo_data->contact_dist = (la->mode & LA_SHAD_CONTACT) ? la->contact_dist : 0.0f;
898         ubo_data->contact_bias = 0.05f * la->contact_bias;
899         ubo_data->contact_spread = la->contact_spread;
900         ubo_data->contact_thickness = la->contact_thickness;
901 }
902
903 /* Used for checking if object is inside the shadow volume. */
904 static bool sphere_bbox_intersect(const EEVEE_BoundSphere *bs, const EEVEE_BoundBox *bb)
905 {
906         /* We are testing using a rougher AABB vs AABB test instead of full AABB vs Sphere. */
907         /* TODO test speed with AABB vs Sphere. */
908         bool x = fabsf(bb->center[0] - bs->center[0]) <= (bb->halfdim[0] + bs->radius);
909         bool y = fabsf(bb->center[1] - bs->center[1]) <= (bb->halfdim[1] + bs->radius);
910         bool z = fabsf(bb->center[2] - bs->center[2]) <= (bb->halfdim[2] + bs->radius);
911
912         return x && y && z;
913 }
914
915 void EEVEE_lights_update(EEVEE_ViewLayerData *sldata)
916 {
917         EEVEE_LampsInfo *linfo = sldata->lamps;
918         Object *ob;
919         int i;
920         char *flag;
921         EEVEE_ShadowCaster *shcaster;
922         EEVEE_BoundSphere *bsphere;
923         EEVEE_ShadowCasterBuffer *frontbuffer = linfo->shcaster_frontbuffer;
924         EEVEE_ShadowCasterBuffer *backbuffer = linfo->shcaster_backbuffer;
925
926         EEVEE_LightBits update_bits = {{0}};
927         if ((linfo->update_flag & LIGHT_UPDATE_SHADOW_CUBE) != 0) {
928                 /* Update all lights. */
929                 lightbits_set_all(&update_bits, true);
930         }
931         else {
932                 /* Search for deleted shadow casters and if shcaster WAS in shadow radius. */
933                 /* No need to run this if we already update all lamps. */
934                 EEVEE_LightBits past_bits = {{0}};
935                 EEVEE_LightBits curr_bits = {{0}};
936                 shcaster = backbuffer->shadow_casters;
937                 flag = backbuffer->flags;
938                 for (i = 0; i < backbuffer->count; ++i, ++flag, ++shcaster) {
939                         /* If the shadowcaster has been deleted or updated. */
940                         if (*flag != 0) {
941                                 /* Add the lamps that were intersecting with its BBox. */
942                                 lightbits_or(&past_bits, &shcaster->bits);
943                         }
944                 }
945                 /* Convert old bits to new bits and add result to final update bits. */
946                 /* NOTE: This might be overkill since all lights are tagged to refresh if
947                  * the light count changes. */
948                 lightbits_convert(&curr_bits, &past_bits, linfo->new_shadow_id, MAX_LIGHT);
949                 lightbits_or(&update_bits, &curr_bits);
950         }
951
952         /* Search for updates in current shadow casters. */
953         shcaster = frontbuffer->shadow_casters;
954         flag = frontbuffer->flags;
955         for (i = 0; i < frontbuffer->count; i++, flag++, shcaster++) {
956                 /* Run intersection checks to fill the bitfields. */
957                 bsphere = linfo->shadow_bounds;
958                 for (int j = 0; j < linfo->cpu_cube_ct; j++, bsphere++) {
959                         bool iter = sphere_bbox_intersect(bsphere, &shcaster->bbox);
960                         lightbits_set_single(&shcaster->bits, j, iter);
961                 }
962                 /* Only add to final bits if objects has been updated. */
963                 if (*flag != 0) {
964                         lightbits_or(&update_bits, &shcaster->bits);
965                 }
966         }
967
968         /* Setup shadow cube in UBO and tag for update if necessary. */
969         for (i = 0; (i < MAX_SHADOW_CUBE) && (ob = linfo->shadow_cube_ref[i]); i++) {
970                 EEVEE_LampEngineData *led = EEVEE_lamp_data_ensure(ob);
971
972                 eevee_shadow_cube_setup(ob, linfo, led);
973                 if (lightbits_get(&update_bits, i) != 0) {
974                         led->need_update = true;
975                 }
976         }
977
978         /* Resize shcasters buffers if too big. */
979         if (frontbuffer->alloc_count - frontbuffer->count > SHADOW_CASTER_ALLOC_CHUNK) {
980                 frontbuffer->alloc_count  = (frontbuffer->count / SHADOW_CASTER_ALLOC_CHUNK) * SHADOW_CASTER_ALLOC_CHUNK;
981                 frontbuffer->alloc_count += (frontbuffer->count % SHADOW_CASTER_ALLOC_CHUNK != 0) ? SHADOW_CASTER_ALLOC_CHUNK : 0;
982                 frontbuffer->shadow_casters = MEM_reallocN(frontbuffer->shadow_casters, sizeof(EEVEE_ShadowCaster) * frontbuffer->alloc_count);
983                 frontbuffer->flags = MEM_reallocN(frontbuffer->flags, sizeof(EEVEE_ShadowCaster) * frontbuffer->alloc_count);
984         }
985 }
986
987 /* this refresh lamps shadow buffers */
988 void EEVEE_draw_shadows(EEVEE_ViewLayerData *sldata, EEVEE_PassList *psl)
989 {
990         EEVEE_LampsInfo *linfo = sldata->lamps;
991         Object *ob;
992         int i;
993
994         DRWMatrixState saved_mats;
995
996         /* We need to save the Matrices before overidding them */
997         DRW_viewport_matrix_get_all(&saved_mats);
998
999         /* Cube Shadow Maps */
1000         DRW_stats_group_start("Cube Shadow Maps");
1001         /* Render each shadow to one layer of the array */
1002         for (i = 0; (ob = linfo->shadow_cube_ref[i]) && (i < MAX_SHADOW_CUBE); i++) {
1003                 EEVEE_LampEngineData *led = EEVEE_lamp_data_ensure(ob);
1004                 Lamp *la = (Lamp *)ob->data;
1005
1006                 if (!led->need_update) {
1007                         continue;
1008                 }
1009
1010                 DRWMatrixState render_mats;
1011                 float (*winmat)[4] = render_mats.mat[DRW_MAT_WIN];
1012                 float (*viewmat)[4] = render_mats.mat[DRW_MAT_VIEW];
1013                 float (*persmat)[4] = render_mats.mat[DRW_MAT_PERS];
1014
1015                 EEVEE_ShadowRender *srd = &linfo->shadow_render_data;
1016                 EEVEE_ShadowCubeData *evscd = &led->data.scd;
1017
1018                 perspective_m4(winmat, -la->clipsta, la->clipsta, -la->clipsta, la->clipsta, la->clipsta, la->clipend);
1019
1020                 srd->clip_near = la->clipsta;
1021                 srd->clip_far = la->clipend;
1022                 copy_v3_v3(srd->position, ob->obmat[3]);
1023
1024                 DRW_uniformbuffer_update(sldata->shadow_render_ubo, srd);
1025
1026                 /* Render shadow cube */
1027                 /* Render 6 faces separatly: seems to be faster for the general case.
1028                  * The only time it's more beneficial is when the CPU culling overhead
1029                  * outweight the instancing overhead. which is rarelly the case. */
1030                 for (int j = 0; j < 6; j++) {
1031                         /* TODO optimize */
1032                         float tmp[4][4];
1033                         unit_m4(tmp);
1034                         negate_v3_v3(tmp[3], srd->position);
1035                         mul_m4_m4m4(viewmat, cubefacemat[j], tmp);
1036                         mul_m4_m4m4(persmat, winmat, viewmat);
1037                         invert_m4_m4(render_mats.mat[DRW_MAT_WININV], winmat);
1038                         invert_m4_m4(render_mats.mat[DRW_MAT_VIEWINV], viewmat);
1039                         invert_m4_m4(render_mats.mat[DRW_MAT_PERSINV], persmat);
1040
1041                         DRW_viewport_matrix_override_set_all(&render_mats);
1042
1043                         GPU_framebuffer_texture_cubeface_attach(sldata->shadow_cube_target_fb,
1044                                                                 sldata->shadow_cube_target, 0, j, 0);
1045                         GPU_framebuffer_bind(sldata->shadow_cube_target_fb);
1046                         GPU_framebuffer_clear_depth(sldata->shadow_cube_target_fb, 1.0f);
1047                         DRW_draw_pass(psl->shadow_pass);
1048                 }
1049
1050                 /* 0.001f is arbitrary, but it should be relatively small so that filter size is not too big. */
1051                 float filter_texture_size = la->soft * 0.001f;
1052                 float filter_pixel_size = ceil(filter_texture_size / linfo->shadow_render_data.cube_texel_size);
1053                 linfo->filter_size = linfo->shadow_render_data.cube_texel_size * ((filter_pixel_size > 1.0f) ? 1.5f : 0.0f);
1054
1055                 /* TODO: OPTI: Filter all faces in one/two draw call */
1056                 for (linfo->current_shadow_face = 0;
1057                      linfo->current_shadow_face < 6;
1058                      linfo->current_shadow_face++)
1059                 {
1060                         /* Copy using a small 3x3 box filter */
1061                         GPU_framebuffer_texture_cubeface_attach(sldata->shadow_store_fb, sldata->shadow_cube_blur, 0,
1062                                                                 linfo->current_shadow_face, 0);
1063                         GPU_framebuffer_bind(sldata->shadow_store_fb);
1064                         DRW_draw_pass(psl->shadow_cube_copy_pass);
1065                 }
1066
1067                 /* Push it to shadowmap array */
1068
1069                 /* Adjust constants if concentric samples change. */
1070                 const float max_filter_size = 7.5f;
1071                 const float previous_box_filter_size = 9.0f; /* Dunno why but that works. */
1072                 const int max_sample = 256;
1073
1074                 if (filter_pixel_size > 2.0f) {
1075                         linfo->filter_size = linfo->shadow_render_data.cube_texel_size * max_filter_size * previous_box_filter_size;
1076                         filter_pixel_size = max_ff(0.0f, filter_pixel_size - 3.0f);
1077                         /* Compute number of concentric samples. Depends directly on filter size. */
1078                         float pix_size_sqr = filter_pixel_size * filter_pixel_size;
1079                         srd->shadow_samples_ct = min_ii(max_sample, 4 + 8 * (int)filter_pixel_size + 4 * (int)(pix_size_sqr));
1080                 }
1081                 else {
1082                         linfo->filter_size = 0.0f;
1083                         srd->shadow_samples_ct = 4;
1084                 }
1085                 srd->shadow_inv_samples_ct = 1.0f / (float)srd->shadow_samples_ct;
1086                 DRW_uniformbuffer_update(sldata->shadow_render_ubo, srd);
1087
1088                 GPU_framebuffer_texture_layer_attach(sldata->shadow_store_fb, sldata->shadow_pool, 0, evscd->layer_id, 0);
1089                 GPU_framebuffer_bind(sldata->shadow_store_fb);
1090                 DRW_draw_pass(psl->shadow_cube_store_pass);
1091
1092                 led->need_update = false;
1093         }
1094         linfo->update_flag &= ~LIGHT_UPDATE_SHADOW_CUBE;
1095         DRW_stats_group_end();
1096
1097         DRW_viewport_matrix_override_set_all(&saved_mats);
1098
1099         /* Cascaded Shadow Maps */
1100         DRW_stats_group_start("Cascaded Shadow Maps");
1101         for (i = 0; (ob = linfo->shadow_cascade_ref[i]) && (i < MAX_SHADOW_CASCADE); i++) {
1102                 EEVEE_LampEngineData *led = EEVEE_lamp_data_ensure(ob);
1103                 Lamp *la = (Lamp *)ob->data;
1104
1105                 EEVEE_ShadowCascadeData *evscd = &led->data.scad;
1106                 EEVEE_ShadowRender *srd = &linfo->shadow_render_data;
1107
1108                 DRWMatrixState render_mats;
1109                 float (*winmat)[4] = render_mats.mat[DRW_MAT_WIN];
1110                 float (*viewmat)[4] = render_mats.mat[DRW_MAT_VIEW];
1111                 float (*persmat)[4] = render_mats.mat[DRW_MAT_PERS];
1112
1113                 eevee_shadow_cascade_setup(ob, linfo, led);
1114
1115                 srd->clip_near = la->clipsta;
1116                 srd->clip_far = la->clipend;
1117
1118                 DRW_uniformbuffer_update(sldata->shadow_render_ubo, &linfo->shadow_render_data);
1119
1120                 copy_m4_m4(viewmat, evscd->viewmat);
1121                 invert_m4_m4(render_mats.mat[DRW_MAT_VIEWINV], viewmat);
1122
1123                 /* Render shadow cascades */
1124                 /* Render cascade separatly: seems to be faster for the general case.
1125                  * The only time it's more beneficial is when the CPU culling overhead
1126                  * outweight the instancing overhead. which is rarelly the case. */
1127                 for (int j = 0; j < la->cascade_count; j++) {
1128                         copy_m4_m4(winmat, evscd->projmat[j]);
1129                         copy_m4_m4(persmat, evscd->viewprojmat[j]);
1130                         invert_m4_m4(render_mats.mat[DRW_MAT_WININV], winmat);
1131                         invert_m4_m4(render_mats.mat[DRW_MAT_PERSINV], persmat);
1132
1133                         DRW_viewport_matrix_override_set_all(&render_mats);
1134
1135                         GPU_framebuffer_texture_layer_attach(sldata->shadow_cascade_target_fb,
1136                                                              sldata->shadow_cascade_target, 0, j, 0);
1137                         GPU_framebuffer_bind(sldata->shadow_cascade_target_fb);
1138                         GPU_framebuffer_clear_depth(sldata->shadow_cascade_target_fb, 1.0f);
1139                         DRW_draw_pass(psl->shadow_pass);
1140                 }
1141
1142                 /* TODO: OPTI: Filter all cascade in one/two draw call */
1143                 for (linfo->current_shadow_cascade = 0;
1144                      linfo->current_shadow_cascade < la->cascade_count;
1145                      ++linfo->current_shadow_cascade)
1146                 {
1147                         /* 0.01f factor to convert to percentage */
1148                         float filter_texture_size = la->soft * 0.01f / evscd->radius[linfo->current_shadow_cascade];
1149                         float filter_pixel_size = ceil(linfo->shadow_size * filter_texture_size);
1150
1151                         /* Copy using a small 3x3 box filter */
1152                         linfo->filter_size = linfo->shadow_render_data.stored_texel_size * ((filter_pixel_size > 1.0f) ? 1.0f : 0.0f);
1153                         GPU_framebuffer_texture_layer_attach(
1154                                 sldata->shadow_store_fb, sldata->shadow_cascade_blur, 0, linfo->current_shadow_cascade, 0);
1155                         GPU_framebuffer_bind(sldata->shadow_store_fb);
1156                         DRW_draw_pass(psl->shadow_cascade_copy_pass);
1157
1158                         /* Push it to shadowmap array and blur more */
1159
1160                         /* Adjust constants if concentric samples change. */
1161                         const float max_filter_size = 7.5f;
1162                         const float previous_box_filter_size = 3.2f; /* Arbitrary: less banding */
1163                         const int max_sample = 256;
1164
1165                         if (filter_pixel_size > 2.0f) {
1166                                 linfo->filter_size = linfo->shadow_render_data.stored_texel_size * max_filter_size * previous_box_filter_size;
1167                                 filter_pixel_size = max_ff(0.0f, filter_pixel_size - 3.0f);
1168                                 /* Compute number of concentric samples. Depends directly on filter size. */
1169                                 float pix_size_sqr = filter_pixel_size * filter_pixel_size;
1170                                 srd->shadow_samples_ct = min_ii(max_sample, 4 + 8 * (int)filter_pixel_size + 4 * (int)(pix_size_sqr));
1171                         }
1172                         else {
1173                                 linfo->filter_size = 0.0f;
1174                                 srd->shadow_samples_ct = 4;
1175                         }
1176                         srd->shadow_inv_samples_ct = 1.0f / (float)srd->shadow_samples_ct;
1177                         DRW_uniformbuffer_update(sldata->shadow_render_ubo, &linfo->shadow_render_data);
1178
1179                         int layer = evscd->layer_id + linfo->current_shadow_cascade;
1180                         GPU_framebuffer_texture_layer_attach(sldata->shadow_store_fb, sldata->shadow_pool, 0, layer, 0);
1181                         GPU_framebuffer_bind(sldata->shadow_store_fb);
1182                         DRW_draw_pass(psl->shadow_cascade_store_pass);
1183                 }
1184         }
1185
1186         DRW_stats_group_end();
1187
1188         DRW_viewport_matrix_override_set_all(&saved_mats);
1189
1190         DRW_uniformbuffer_update(sldata->light_ubo, &linfo->light_data);
1191         DRW_uniformbuffer_update(sldata->shadow_ubo, &linfo->shadow_data); /* Update all data at once */
1192 }
1193
1194 void EEVEE_lights_free(void)
1195 {
1196         DRW_SHADER_FREE_SAFE(e_data.shadow_sh);
1197         for (int i = 0; i < SHADOW_METHOD_MAX; ++i) {
1198                 DRW_SHADER_FREE_SAFE(e_data.shadow_store_cube_sh[i]);
1199                 DRW_SHADER_FREE_SAFE(e_data.shadow_store_cascade_sh[i]);
1200                 DRW_SHADER_FREE_SAFE(e_data.shadow_copy_cube_sh[i]);
1201                 DRW_SHADER_FREE_SAFE(e_data.shadow_copy_cascade_sh[i]);
1202         }
1203 }