Eevee: Refactor Shadow System
[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 "BKE_object.h"
29
30 #include "eevee_engine.h"
31 #include "eevee_private.h"
32
33 /* Theses are the structs stored inside Objects.
34  * It works with even if the object is in multiple layers
35  * because we don't get the same "Object *" for each layer. */
36 typedef struct EEVEE_LightData {
37         short light_id, shadow_id;
38 } EEVEE_LightData;
39
40 typedef struct EEVEE_ShadowCubeData {
41         short light_id, shadow_id;
42         float viewprojmat[6][4][4];
43 } EEVEE_ShadowCubeData;
44
45 typedef struct EEVEE_ShadowCascadeData {
46         short light_id, shadow_id;
47         float viewprojmat[MAX_CASCADE_NUM][4][4]; /* World->Lamp->NDC : used for rendering the shadow map. */
48 } EEVEE_ShadowCascadeData;
49
50 typedef struct ShadowCaster {
51         struct ShadowCaster *next, *prev;
52         void *ob;
53         bool prune;
54 } ShadowCaster;
55
56 static struct {
57         struct GPUShader *shadow_sh;
58         struct GPUShader *shadow_store_cube_sh;
59         struct GPUShader *shadow_store_cascade_sh;
60 } e_data = {NULL}; /* Engine data */
61
62 extern char datatoc_shadow_vert_glsl[];
63 extern char datatoc_shadow_geom_glsl[];
64 extern char datatoc_shadow_frag_glsl[];
65 extern char datatoc_shadow_store_frag_glsl[];
66
67 /* *********** FUNCTIONS *********** */
68
69 void EEVEE_lights_init(EEVEE_SceneLayerData *sldata)
70 {
71         const unsigned int shadow_ubo_size = sizeof(EEVEE_ShadowCube) * MAX_SHADOW_CUBE +
72                                              sizeof(EEVEE_ShadowCascade) * MAX_SHADOW_CASCADE;
73
74         const DRWContextState *draw_ctx = DRW_context_state_get();
75         SceneLayer *scene_layer = draw_ctx->scene_layer;
76         IDProperty *props = BKE_scene_layer_engine_evaluated_get(scene_layer, COLLECTION_MODE_NONE, RE_engine_id_BLENDER_EEVEE);
77
78         if (!e_data.shadow_sh) {
79                 e_data.shadow_sh = DRW_shader_create(
80                         datatoc_shadow_vert_glsl, datatoc_shadow_geom_glsl, datatoc_shadow_frag_glsl, NULL);
81
82                 e_data.shadow_store_cube_sh = DRW_shader_create_fullscreen(datatoc_shadow_store_frag_glsl, NULL);
83                 e_data.shadow_store_cascade_sh = DRW_shader_create_fullscreen(datatoc_shadow_store_frag_glsl, "#define CSM");
84         }
85
86         if (!sldata->lamps) {
87                 sldata->lamps              = MEM_callocN(sizeof(EEVEE_LampsInfo), "EEVEE_LampsInfo");
88                 sldata->light_ubo          = DRW_uniformbuffer_create(sizeof(EEVEE_Light) * MAX_LIGHT, NULL);
89                 sldata->shadow_ubo         = DRW_uniformbuffer_create(shadow_ubo_size, NULL);
90                 sldata->shadow_render_ubo  = DRW_uniformbuffer_create(sizeof(EEVEE_ShadowRender), NULL);
91         }
92
93         int sh_method = BKE_collection_engine_property_value_get_int(props, "shadow_method");
94         int sh_size = BKE_collection_engine_property_value_get_int(props, "shadow_size");
95         UNUSED_VARS(sh_method);
96
97         EEVEE_LampsInfo *linfo = sldata->lamps;
98         if (linfo->shadow_size != sh_size) {
99                 BLI_assert((sh_size > 0) && (sh_size <= 8192));
100                 DRW_TEXTURE_FREE_SAFE(sldata->shadow_pool);
101                 DRW_TEXTURE_FREE_SAFE(sldata->shadow_cascade_target);
102
103                 linfo->shadow_size = sh_size;
104                 linfo->shadow_render_data.stored_texel_size = 1.0 / (float)linfo->shadow_size;
105
106                 /* Compute adequate size for the cubemap render target.
107                  * The 3.0f factor is here to make sure there is no under sampling between
108                  * the octahedron mapping and the cubemap. */
109                 int new_cube_target_size = (int)ceil(sqrt((float)(sh_size * sh_size) / 6.0f) * 3.0f);
110
111                 CLAMP(new_cube_target_size, 1, 4096);
112
113                 if (linfo->shadow_cube_target_size != new_cube_target_size) {
114                         linfo->shadow_cube_target_size = new_cube_target_size;
115                         DRW_TEXTURE_FREE_SAFE(sldata->shadow_cube_target);
116                         linfo->shadow_render_data.cube_texel_size = 1.0 / (float)linfo->shadow_cube_target_size;
117                 }
118         }
119 }
120
121 void EEVEE_lights_cache_init(EEVEE_SceneLayerData *sldata, EEVEE_PassList *psl)
122 {
123         EEVEE_LampsInfo *linfo = sldata->lamps;
124
125         linfo->num_light = linfo->num_cube = linfo->num_cascade = linfo->num_shadow = 0;
126         memset(linfo->light_ref, 0, sizeof(linfo->light_ref));
127         memset(linfo->shadow_cube_ref, 0, sizeof(linfo->shadow_cube_ref));
128         memset(linfo->shadow_cascade_ref, 0, sizeof(linfo->shadow_cascade_ref));
129
130         {
131                 psl->shadow_cube_store_pass = DRW_pass_create("Shadow Storage Pass", DRW_STATE_WRITE_COLOR);
132
133                 DRWShadingGroup *grp = DRW_shgroup_create(e_data.shadow_store_cube_sh, psl->shadow_cube_store_pass);
134                 DRW_shgroup_uniform_buffer(grp, "shadowTexture", &sldata->shadow_cube_target);
135                 DRW_shgroup_uniform_block(grp, "shadow_render_block", sldata->shadow_render_ubo);
136                 DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
137         }
138
139         {
140                 psl->shadow_cascade_store_pass = DRW_pass_create("Shadow Cascade Storage Pass", DRW_STATE_WRITE_COLOR);
141
142                 DRWShadingGroup *grp = DRW_shgroup_create(e_data.shadow_store_cascade_sh, psl->shadow_cascade_store_pass);
143                 DRW_shgroup_uniform_buffer(grp, "shadowTexture", &sldata->shadow_cascade_target);
144                 DRW_shgroup_uniform_block(grp, "shadow_render_block", sldata->shadow_render_ubo);
145                 DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
146         }
147
148         {
149                 psl->shadow_cube_pass = DRW_pass_create("Shadow Cube Pass", DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
150         }
151
152         {
153                 psl->shadow_cascade_pass = DRW_pass_create("Shadow Cascade Pass", DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
154         }
155
156         /* Reset shadow casters list */
157         BLI_freelistN(&sldata->shadow_casters);
158 }
159
160 void EEVEE_lights_cache_add(EEVEE_SceneLayerData *sldata, Object *ob)
161 {
162         EEVEE_LampsInfo *linfo = sldata->lamps;
163
164         /* Step 1 find all lamps in the scene and setup them */
165         if (linfo->num_light > MAX_LIGHT) {
166                 printf("Too much lamps in the scene !!!\n");
167                 linfo->num_light = MAX_LIGHT;
168         }
169         else {
170                 Lamp *la = (Lamp *)ob->data;
171                 EEVEE_LampEngineData *led = EEVEE_lamp_data_get(ob);
172
173                 if ((ob->deg_update_flag & DEG_RUNTIME_DATA_UPDATE) != 0) {
174                         led->need_update = true;
175                 }
176
177                 MEM_SAFE_FREE(led->storage);
178
179 #if 1 /* TODO Waiting for notified refresh. only on scene change. Else too much perf cost. */
180                 if (la->mode & (LA_SHAD_BUF | LA_SHAD_RAY)) {
181                         if (la->type == LA_SUN && linfo->num_cascade < MAX_SHADOW_CASCADE) {
182                                 led->storage = MEM_mallocN(sizeof(EEVEE_ShadowCascadeData), "EEVEE_ShadowCascadeData");
183                                 ((EEVEE_ShadowCascadeData *)led->storage)->shadow_id = linfo->num_shadow;
184                                 linfo->shadow_cascade_ref[linfo->num_cascade] = ob;
185                                 linfo->num_cascade++;
186                                 linfo->num_shadow += MAX_CASCADE_NUM;
187                         }
188                         else if ((la->type == LA_SPOT || la->type == LA_LOCAL || la->type == LA_AREA)
189                                   && linfo->num_cube < MAX_SHADOW_CUBE) {
190                                 led->storage = MEM_mallocN(sizeof(EEVEE_ShadowCubeData), "EEVEE_ShadowCubeData");
191                                 ((EEVEE_ShadowCubeData *)led->storage)->shadow_id = linfo->num_shadow;
192                                 linfo->shadow_cube_ref[linfo->num_cube] = ob;
193                                 linfo->num_cube++;
194                                 linfo->num_shadow += 1;
195                         }
196
197                 }
198 #else
199                 UNUSED_VARS(la);
200 #endif
201                 /* Default light without shadows */
202                 if (!led->storage) {
203                         led->storage = MEM_mallocN(sizeof(EEVEE_LightData), "EEVEE_LightData");
204                         ((EEVEE_LightData *)led->storage)->shadow_id = -1;
205                 }
206
207                 ((EEVEE_LightData *)led->storage)->light_id = linfo->num_light;
208                 linfo->light_ref[linfo->num_light] = ob;
209                 linfo->num_light++;
210         }
211 }
212
213 /* Add a shadow caster to the shadowpasses */
214 void EEVEE_lights_cache_shcaster_add(EEVEE_SceneLayerData *sldata, EEVEE_PassList *psl, struct Gwn_Batch *geom, float (*obmat)[4])
215 {
216         DRWShadingGroup *grp = DRW_shgroup_instance_create(e_data.shadow_sh, psl->shadow_cube_pass, geom);
217         DRW_shgroup_uniform_block(grp, "shadow_render_block", sldata->shadow_render_ubo);
218         DRW_shgroup_uniform_mat4(grp, "ShadowModelMatrix", (float *)obmat);
219
220         for (int i = 0; i < 6; ++i)
221                 DRW_shgroup_call_dynamic_add_empty(grp);
222
223         grp = DRW_shgroup_instance_create(e_data.shadow_sh, psl->shadow_cascade_pass, geom);
224         DRW_shgroup_uniform_block(grp, "shadow_render_block", sldata->shadow_render_ubo);
225         DRW_shgroup_uniform_mat4(grp, "ShadowModelMatrix", (float *)obmat);
226
227         for (int i = 0; i < MAX_CASCADE_NUM; ++i)
228                 DRW_shgroup_call_dynamic_add_empty(grp);
229 }
230
231 void EEVEE_lights_cache_shcaster_material_add(
232         EEVEE_SceneLayerData *sldata, EEVEE_PassList *psl, struct GPUMaterial *gpumat, struct Gwn_Batch *geom, float (*obmat)[4], float *alpha_threshold)
233 {
234         DRWShadingGroup *grp = DRW_shgroup_material_instance_create(gpumat, psl->shadow_cube_pass, geom);
235
236         if (grp == NULL) return;
237
238         DRW_shgroup_uniform_block(grp, "shadow_render_block", sldata->shadow_render_ubo);
239         DRW_shgroup_uniform_mat4(grp, "ShadowModelMatrix", (float *)obmat);
240
241         if (alpha_threshold != NULL)
242                 DRW_shgroup_uniform_float(grp, "alphaThreshold", alpha_threshold, 1);
243
244         for (int i = 0; i < 6; ++i)
245                 DRW_shgroup_call_dynamic_add_empty(grp);
246
247         grp = DRW_shgroup_material_instance_create(gpumat, psl->shadow_cascade_pass, geom);
248         DRW_shgroup_uniform_block(grp, "shadow_render_block", sldata->shadow_render_ubo);
249         DRW_shgroup_uniform_mat4(grp, "ShadowModelMatrix", (float *)obmat);
250
251         if (alpha_threshold != NULL)
252                 DRW_shgroup_uniform_float(grp, "alphaThreshold", alpha_threshold, 1);
253
254         for (int i = 0; i < MAX_CASCADE_NUM; ++i)
255                 DRW_shgroup_call_dynamic_add_empty(grp);
256 }
257
258 void EEVEE_lights_cache_finish(EEVEE_SceneLayerData *sldata)
259 {
260         EEVEE_LampsInfo *linfo = sldata->lamps;
261         DRWTextureFormat shadow_pool_format;
262
263         /* Setup enough layers. */
264         /* Free textures if number mismatch. */
265         if ((linfo->num_shadow != linfo->cache_num_shadow) ||
266                 (linfo->num_cube != linfo->cache_num_cube) ||
267             (linfo->num_cascade != linfo->cache_num_cascade))
268         {
269                 DRW_TEXTURE_FREE_SAFE(sldata->shadow_pool);
270                 linfo->cache_num_cube = linfo->num_cube;
271                 linfo->cache_num_cascade = linfo->num_cascade;
272                 linfo->cache_num_shadow = linfo->num_shadow;
273                 linfo->update_flag |= LIGHT_UPDATE_SHADOW_CUBE;
274         }
275
276         /* TODO Variance Shadow Map */
277         shadow_pool_format = DRW_TEX_R_32;
278
279         if (!sldata->shadow_cube_target) {
280                 /* TODO render everything on the same 2d render target using clip planes and no Geom Shader. */
281                 /* Cubemaps */
282                 sldata->shadow_cube_target = DRW_texture_create_cube(linfo->shadow_cube_target_size, DRW_TEX_DEPTH_24, 0, NULL);
283         }
284
285         if (!sldata->shadow_cascade_target) {
286                 /* CSM */
287                 sldata->shadow_cascade_target = DRW_texture_create_2D_array(
288                         linfo->shadow_size, linfo->shadow_size, MAX_CASCADE_NUM, DRW_TEX_DEPTH_24, 0, NULL);
289         }
290
291         /* Initialize Textures Array first so DRW_framebuffer_init just bind them. */
292         if (!sldata->shadow_pool) {
293                 /* All shadows fit in this array */
294                 sldata->shadow_pool = DRW_texture_create_2D_array(
295                         linfo->shadow_size, linfo->shadow_size, max_ff(1, linfo->num_cube + linfo->num_cascade),
296                         shadow_pool_format, DRW_TEX_FILTER, NULL);
297         }
298
299         /* Render FB */
300         DRWFboTexture tex_cascade = {&sldata->shadow_cube_target, DRW_TEX_DEPTH_24, 0};
301         DRW_framebuffer_init(&sldata->shadow_target_fb, &draw_engine_eevee_type,
302                              linfo->shadow_size, linfo->shadow_size,
303                              &tex_cascade, 1);
304
305         /* Storage FB */
306         DRWFboTexture tex_pool = {&sldata->shadow_pool, shadow_pool_format, DRW_TEX_FILTER};
307         DRW_framebuffer_init(&sldata->shadow_store_fb, &draw_engine_eevee_type,
308                              linfo->shadow_size, linfo->shadow_size,
309                              &tex_pool, 1);
310
311         /* Restore */
312         DRW_framebuffer_texture_detach(sldata->shadow_cube_target);
313
314         /* Update Lamps UBOs. */
315         EEVEE_lights_update(sldata);
316 }
317
318 /* Update buffer with lamp data */
319 static void eevee_light_setup(Object *ob, EEVEE_LampsInfo *linfo, EEVEE_LampEngineData *led)
320 {
321         /* TODO only update if data changes */
322         EEVEE_LightData *evld = led->storage;
323         EEVEE_Light *evli = linfo->light_data + evld->light_id;
324         Lamp *la = (Lamp *)ob->data;
325         float mat[4][4], scale[3], power;
326
327         /* Position */
328         copy_v3_v3(evli->position, ob->obmat[3]);
329
330         /* Color */
331         copy_v3_v3(evli->color, &la->r);
332
333         /* Influence Radius */
334         evli->dist = la->dist;
335
336         /* Vectors */
337         normalize_m4_m4_ex(mat, ob->obmat, scale);
338         copy_v3_v3(evli->forwardvec, mat[2]);
339         normalize_v3(evli->forwardvec);
340         negate_v3(evli->forwardvec);
341
342         copy_v3_v3(evli->rightvec, mat[0]);
343         normalize_v3(evli->rightvec);
344
345         copy_v3_v3(evli->upvec, mat[1]);
346         normalize_v3(evli->upvec);
347
348         /* Spot size & blend */
349         if (la->type == LA_SPOT) {
350                 evli->sizex = scale[0] / scale[2];
351                 evli->sizey = scale[1] / scale[2];
352                 evli->spotsize = cosf(la->spotsize * 0.5f);
353                 evli->spotblend = (1.0f - evli->spotsize) * la->spotblend;
354                 evli->radius = max_ff(0.001f, la->area_size);
355         }
356         else if (la->type == LA_AREA) {
357                 evli->sizex = max_ff(0.0001f, la->area_size * scale[0] * 0.5f);
358                 if (la->area_shape == LA_AREA_RECT) {
359                         evli->sizey = max_ff(0.0001f, la->area_sizey * scale[1] * 0.5f);
360                 }
361                 else {
362                         evli->sizey = max_ff(0.0001f, la->area_size * scale[1] * 0.5f);
363                 }
364         }
365         else {
366                 evli->radius = max_ff(0.001f, la->area_size);
367         }
368
369         /* Make illumination power constant */
370         if (la->type == LA_AREA) {
371                 power = 1.0f / (evli->sizex * evli->sizey * 4.0f * M_PI) /* 1/(w*h*Pi) */
372                         * 80.0f; /* XXX : Empirical, Fit cycles power */
373         }
374         else if (la->type == LA_SPOT || la->type == LA_LOCAL) {
375                 power = 1.0f / (4.0f * evli->radius * evli->radius * M_PI * M_PI) /* 1/(4*r²*Pi²) */
376                         * M_PI * M_PI * M_PI * 10.0; /* XXX : Empirical, Fit cycles power */
377
378                 /* for point lights (a.k.a radius == 0.0) */
379                 // power = M_PI * M_PI * 0.78; /* XXX : Empirical, Fit cycles power */
380         }
381         else {
382                 power = 1.0f;
383         }
384         mul_v3_fl(evli->color, power * la->energy);
385
386         /* Lamp Type */
387         evli->lamptype = (float)la->type;
388
389         /* No shadow by default */
390         evli->shadowid = -1.0f;
391 }
392
393 static void eevee_shadow_cube_setup(Object *ob, EEVEE_LampsInfo *linfo, EEVEE_LampEngineData *led)
394 {
395         float projmat[4][4];
396
397         EEVEE_ShadowCubeData *evsmp = (EEVEE_ShadowCubeData *)led->storage;
398         EEVEE_Light *evli = linfo->light_data + evsmp->light_id;
399         EEVEE_ShadowCube *evsh = linfo->shadow_cube_data + evsmp->shadow_id;
400         Lamp *la = (Lamp *)ob->data;
401
402         perspective_m4(projmat, -la->clipsta, la->clipsta, -la->clipsta, la->clipsta, la->clipsta, la->clipend);
403
404         for (int i = 0; i < 6; ++i) {
405                 float tmp[4][4];
406                 unit_m4(tmp);
407                 negate_v3_v3(tmp[3], ob->obmat[3]);
408                 mul_m4_m4m4(tmp, cubefacemat[i], tmp);
409                 mul_m4_m4m4(evsmp->viewprojmat[i], projmat, tmp);
410         }
411
412         evsh->bias = 0.05f * la->bias;
413         evsh->near = la->clipsta;
414         evsh->far = la->clipend;
415         evsh->exp = la->bleedexp;
416
417         evli->shadowid = (float)(evsmp->shadow_id);
418 }
419
420 #define LERP(t, a, b) ((a) + (t) * ((b) - (a)))
421
422 static void frustum_min_bounding_sphere(const float corners[8][4], float r_center[3], float *r_radius)
423 {
424 #if 0 /* Simple solution but waist too much space. */
425         float minvec[3], maxvec[3];
426
427         /* compute the bounding box */
428         INIT_MINMAX(minvec, maxvec);
429         for (int i = 0; i < 8; ++i)     {
430                 minmax_v3v3_v3(minvec, maxvec, corners[i]);
431         }
432
433         /* compute the bounding sphere of this box */
434         r_radius = len_v3v3(minvec, maxvec) * 0.5f;
435         add_v3_v3v3(r_center, minvec, maxvec);
436         mul_v3_fl(r_center, 0.5f);
437 #else
438         /* Make the bouding sphere always centered on the front diagonal */
439         add_v3_v3v3(r_center, corners[4], corners[7]);
440         mul_v3_fl(r_center, 0.5f);
441         *r_radius = len_v3v3(corners[0], r_center);
442
443         /* Search the largest distance between the sphere center
444          * and the front plane corners. */
445         for (int i = 0; i < 4; ++i) {
446                 float rad = len_v3v3(corners[4+i], r_center);
447                 if (rad > *r_radius) {
448                         *r_radius = rad;
449                 }
450         }
451 #endif
452 }
453
454 static void eevee_shadow_cascade_setup(Object *ob, EEVEE_LampsInfo *linfo, EEVEE_LampEngineData *led)
455 {
456         /* Camera Matrices */
457         float persmat[4][4], persinv[4][4];
458         float viewprojmat[4][4], projinv[4][4];
459         float near, far;
460         float near_v[4] = {0.0f, 0.0f, -1.0f, 1.0f};
461         float far_v[4] = {0.0f, 0.0f,  1.0f, 1.0f};
462         bool is_persp = DRW_viewport_is_persp_get();
463         DRW_viewport_matrix_get(persmat, DRW_MAT_PERS);
464         invert_m4_m4(persinv, persmat);
465         /* FIXME : Get near / far from Draw manager? */
466         DRW_viewport_matrix_get(viewprojmat, DRW_MAT_WIN);
467         invert_m4_m4(projinv, viewprojmat);
468         mul_m4_v4(projinv, near_v);
469         mul_m4_v4(projinv, far_v);
470         near = near_v[2];
471         far = far_v[2]; /* TODO: Should be a shadow parameter */
472         if (is_persp) {
473                 near /= near_v[3];
474                 far /= far_v[3];
475         }
476
477         /* Lamps Matrices */
478         float viewmat[4][4], projmat[4][4];
479         int cascade_ct = MAX_CASCADE_NUM;
480         float shadow_res = 512.0f; /* TODO parameter */
481
482         EEVEE_ShadowCascadeData *evscp = (EEVEE_ShadowCascadeData *)led->storage;
483         EEVEE_Light *evli = linfo->light_data + evscp->light_id;
484         EEVEE_ShadowCascade *evsh = linfo->shadow_cascade_data + evscp->shadow_id;
485         Lamp *la = (Lamp *)ob->data;
486
487         /* The technique consists into splitting
488          * the view frustum into several sub-frustum
489          * that are individually receiving one shadow map */
490
491         /* init near/far */
492         for (int c = 0; c < MAX_CASCADE_NUM; ++c) {
493                 evsh->split[c] = far;
494         }
495
496         /* Compute split planes */
497         float splits_ndc[MAX_CASCADE_NUM + 1];
498         splits_ndc[0] = -1.0f;
499         splits_ndc[cascade_ct] = 1.0f;
500         for (int c = 1; c < cascade_ct; ++c) {
501                 const float lambda = 0.8f; /* TODO : Parameter */
502
503                 /* View Space */
504                 float linear_split = LERP(((float)(c) / (float)cascade_ct), near, far);
505                 float exp_split = near * powf(far / near, (float)(c) / (float)cascade_ct);
506
507                 if (is_persp) {
508                         evsh->split[c-1] = LERP(lambda, linear_split, exp_split);
509                 }
510                 else {
511                         evsh->split[c-1] = linear_split;
512                 }
513
514                 /* NDC Space */
515                 float p[4] = {1.0f, 1.0f, evsh->split[c-1], 1.0f};
516                 mul_m4_v4(viewprojmat, p);
517                 splits_ndc[c] = p[2];
518
519                 if (is_persp) {
520                         splits_ndc[c] /= p[3];
521                 }
522         }
523
524         /* For each cascade */
525         for (int c = 0; c < cascade_ct; ++c) {
526                 /* Given 8 frustrum corners */
527                 float corners[8][4] = {
528                         /* Near Cap */
529                         {-1.0f, -1.0f, splits_ndc[c], 1.0f},
530                         { 1.0f, -1.0f, splits_ndc[c], 1.0f},
531                         {-1.0f,  1.0f, splits_ndc[c], 1.0f},
532                         { 1.0f,  1.0f, splits_ndc[c], 1.0f},
533                         /* Far Cap */
534                         {-1.0f, -1.0f, splits_ndc[c+1], 1.0f},
535                         { 1.0f, -1.0f, splits_ndc[c+1], 1.0f},
536                         {-1.0f,  1.0f, splits_ndc[c+1], 1.0f},
537                         { 1.0f,  1.0f, splits_ndc[c+1], 1.0f}
538                 };
539
540                 /* Transform them into world space */
541                 for (int i = 0; i < 8; ++i)     {
542                         mul_m4_v4(persinv, corners[i]);
543                         mul_v3_fl(corners[i], 1.0f / corners[i][3]);
544                         corners[i][3] = 1.0f;
545                 }
546
547                 /* Project them into light space */
548                 invert_m4_m4(viewmat, ob->obmat);
549                 normalize_v3(viewmat[0]);
550                 normalize_v3(viewmat[1]);
551                 normalize_v3(viewmat[2]);
552
553                 for (int i = 0; i < 8; ++i)     {
554                         mul_m4_v4(viewmat, corners[i]);
555                 }
556
557                 float center[3], radius;
558                 frustum_min_bounding_sphere(corners, center, &radius);
559
560                 /* Snap projection center to nearest texel to cancel shimering. */
561                 float shadow_origin[2], shadow_texco[2];
562                 mul_v2_v2fl(shadow_origin, center, shadow_res / (2.0f * radius)); /* Light to texture space. */
563
564                 /* Find the nearest texel. */
565                 shadow_texco[0] = round(shadow_origin[0]);
566                 shadow_texco[1] = round(shadow_origin[1]);
567
568                 /* Compute offset. */
569                 sub_v2_v2(shadow_texco, shadow_origin);
570                 mul_v2_fl(shadow_texco, (2.0f * radius) / shadow_res); /* Texture to light space. */
571
572                 /* Apply offset. */
573                 add_v2_v2(center, shadow_texco);
574
575                 /* Expand the projection to cover frustum range */
576                 orthographic_m4(projmat,
577                                 center[0] - radius,
578                                 center[0] + radius,
579                                 center[1] - radius,
580                                 center[1] + radius,
581                                 la->clipsta, la->clipend);
582
583                 mul_m4_m4m4(evscp->viewprojmat[c], projmat, viewmat);
584                 mul_m4_m4m4(evsh->shadowmat[c], texcomat, evscp->viewprojmat[c]);
585
586                 /* TODO modify bias depending on the cascade radius */
587                 evsh->bias = 0.005f * la->bias;
588         }
589
590         evli->shadowid = (float)(MAX_SHADOW_CUBE + evscp->shadow_id);
591 }
592
593 /* Used for checking if object is inside the shadow volume. */
594 static bool cube_bbox_intersect(const float cube_center[3], float cube_half_dim, const BoundBox *bb, float (*obmat)[4])
595 {
596         float min[3], max[4], tmp[4][4];
597         unit_m4(tmp);
598         translate_m4(tmp, -cube_center[0], -cube_center[1], -cube_center[2]);
599         mul_m4_m4m4(tmp, tmp, obmat);
600
601         /* Just simple AABB intersection test in world space. */
602         INIT_MINMAX(min, max);
603         for (int i = 0; i < 8; ++i) {
604                 float vec[3];
605                 copy_v3_v3(vec, bb->vec[i]);
606                 mul_m4_v3(tmp, vec);
607                 minmax_v3v3_v3(min, max, vec);
608         }
609
610     if (MAX3(max[0], max[1], max[2]) < -cube_half_dim) return false;
611     if (MIN3(min[0], min[1], min[2]) >  cube_half_dim) return false;
612
613         return true;
614 }
615
616 static ShadowCaster *search_object_in_list(ListBase *list, Object *ob)
617 {
618         for (ShadowCaster *ldata = list->first; ldata; ldata = ldata->next) {
619                 if (ldata->ob == ob)
620                         return ldata;
621         }
622
623         return NULL;
624 }
625
626 static void delete_pruned_shadowcaster(EEVEE_LampEngineData *led)
627 {
628         ShadowCaster *next;
629         for (ShadowCaster *ldata = led->shadow_caster_list.first; ldata; ldata = next) {
630                 next = ldata->next;
631                 if (ldata->prune == true) {
632                         led->need_update = true;
633                         BLI_freelinkN(&led->shadow_caster_list, ldata);
634                 }
635         }
636 }
637
638 static void light_tag_shadow_update(Object *lamp, Object *ob)
639 {
640         Lamp *la = lamp->data;
641         EEVEE_LampEngineData *led = EEVEE_lamp_data_get(lamp);
642
643         bool is_inside_range = cube_bbox_intersect(lamp->obmat[3], la->clipend, BKE_object_boundbox_get(ob), ob->obmat);
644         ShadowCaster *ldata = search_object_in_list(&led->shadow_caster_list, ob);
645
646         if (is_inside_range) {
647                 if (ldata == NULL) {
648                         /* Object was not a shadow caster previously but is now. Add it. */
649                         ldata = MEM_callocN(sizeof(ShadowCaster), "ShadowCaster");
650                         ldata->ob = ob;
651                         BLI_addtail(&led->shadow_caster_list, ldata);
652                         led->need_update = true;
653                 }
654                 else {
655                         EEVEE_ObjectEngineData *oedata = EEVEE_object_data_get(ob);
656                         if (oedata->need_update) {
657                                 led->need_update = true;
658                         }
659                 }
660                 ldata->prune = false;
661         }
662         else if (ldata != NULL) {
663                 /* Object was a shadow caster previously and is not anymore. Remove it. */
664                 led->need_update = true;
665                 BLI_freelinkN(&led->shadow_caster_list, ldata);
666         }
667 }
668
669 static void eevee_lights_shcaster_updated(EEVEE_SceneLayerData *sldata, Object *ob)
670 {
671         Object *lamp;
672         EEVEE_LampsInfo *linfo = sldata->lamps;
673
674         /* Iterate over all shadow casting lamps to see if
675          * each of them needs update because of this object */
676         for (int i = 0; (lamp = linfo->shadow_cube_ref[i]) && (i < MAX_SHADOW_CUBE); i++) {
677                 light_tag_shadow_update(lamp, ob);
678         }
679 }
680
681 void EEVEE_lights_update(EEVEE_SceneLayerData *sldata)
682 {
683         EEVEE_LampsInfo *linfo = sldata->lamps;
684         Object *ob;
685         int i;
686
687         /* Prune shadow casters to remove if object does not exists anymore (unprune them if object exists) */
688         Object *lamp;
689         for (i = 0; (lamp = linfo->shadow_cube_ref[i]) && (i < MAX_SHADOW_CUBE); i++) {
690                 EEVEE_LampEngineData *led = EEVEE_lamp_data_get(lamp);
691
692                 if ((linfo->update_flag & LIGHT_UPDATE_SHADOW_CUBE) != 0) {
693                         led->need_update = true;
694                 }
695
696                 for (ShadowCaster *ldata = led->shadow_caster_list.first; ldata; ldata = ldata->next) {
697                         ldata->prune = true;
698                 }
699         }
700
701         for (LinkData *ldata = sldata->shadow_casters.first; ldata; ldata = ldata->next) {
702                 eevee_lights_shcaster_updated(sldata, ldata->data);
703         }
704
705         for (i = 0; (ob = linfo->light_ref[i]) && (i < MAX_LIGHT); i++) {
706                 EEVEE_LampEngineData *led = EEVEE_lamp_data_get(ob);
707                 eevee_light_setup(ob, linfo, led);
708         }
709
710         for (i = 0; (ob = linfo->shadow_cube_ref[i]) && (i < MAX_SHADOW_CUBE); i++) {
711                 EEVEE_LampEngineData *led = EEVEE_lamp_data_get(ob);
712                 eevee_shadow_cube_setup(ob, linfo, led);
713                 delete_pruned_shadowcaster(led);
714         }
715
716         for (i = 0; (ob = linfo->shadow_cascade_ref[i]) && (i < MAX_SHADOW_CASCADE); i++) {
717                 EEVEE_LampEngineData *led = EEVEE_lamp_data_get(ob);
718                 eevee_shadow_cascade_setup(ob, linfo, led);
719         }
720
721         DRW_uniformbuffer_update(sldata->light_ubo, &linfo->light_data);
722         DRW_uniformbuffer_update(sldata->shadow_ubo, &linfo->shadow_cube_data); /* Update all data at once */
723 }
724
725 /* this refresh lamps shadow buffers */
726 void EEVEE_draw_shadows(EEVEE_SceneLayerData *sldata, EEVEE_PassList *psl)
727 {
728         EEVEE_LampsInfo *linfo = sldata->lamps;
729         Object *ob;
730         int i;
731         float clear_col[4] = {FLT_MAX};
732
733         /* Cube Shadow Maps */
734         /* Render each shadow to one layer of the array */
735         for (i = 0; (ob = linfo->shadow_cube_ref[i]) && (i < MAX_SHADOW_CUBE); i++) {
736                 EEVEE_LampEngineData *led = EEVEE_lamp_data_get(ob);
737                 Lamp *la = (Lamp *)ob->data;
738
739                 if (led->need_update) {
740                         EEVEE_ShadowCubeData *evscd = (EEVEE_ShadowCubeData *)led->storage;
741                         EEVEE_ShadowRender *srd = &linfo->shadow_render_data;
742
743                         srd->shadow_samples_ct = 32.0f;
744                         srd->shadow_inv_samples_ct = 1.0f / srd->shadow_samples_ct;
745                         srd->clip_near = la->clipsta;
746                         srd->clip_far = la->clipend;
747                         copy_v3_v3(srd->position, ob->obmat[3]);
748                         for (int j = 0; j < 6; j++) {
749                                 float tmp[4][4];
750
751                                 unit_m4(tmp);
752                                 negate_v3_v3(tmp[3], ob->obmat[3]);
753                                 mul_m4_m4m4(srd->viewmat[j], cubefacemat[j], tmp);
754
755                                 copy_m4_m4(srd->shadowmat[j], evscd->viewprojmat[j]);
756                         }
757                         DRW_uniformbuffer_update(sldata->shadow_render_ubo, srd);
758
759                         DRW_framebuffer_texture_attach(sldata->shadow_target_fb, sldata->shadow_cube_target, 0, 0);
760                         DRW_framebuffer_bind(sldata->shadow_target_fb);
761                         DRW_framebuffer_clear(true, true, false, clear_col, 1.0f);
762                         /* Render shadow cube */
763                         DRW_draw_pass(psl->shadow_cube_pass);
764
765                         /* Push it to shadowmap array */
766                         DRW_framebuffer_texture_layer_attach(sldata->shadow_store_fb, sldata->shadow_pool, 0, i, 0);
767                         DRW_framebuffer_bind(sldata->shadow_store_fb);
768                         DRW_draw_pass(psl->shadow_cube_store_pass);
769
770                         led->need_update = false;
771                 }
772         }
773         linfo->update_flag &= ~LIGHT_UPDATE_SHADOW_CUBE;
774
775         DRW_framebuffer_texture_detach(sldata->shadow_cube_target);
776
777         /* Cascaded Shadow Maps */
778 //      DRW_framebuffer_bind(fbl->shadow_cascade_fb);
779 //      DRW_framebuffer_clear(false, true, false, NULL, 1.0);
780
781 //      /* Render each shadow to one layer of the array */
782 //      for (i = 0; (ob = linfo->shadow_cascade_ref[i]) && (i < MAX_SHADOW_CASCADE); i++) {
783 //              EEVEE_LampEngineData *led = EEVEE_lamp_data_get(ob);
784 //              EEVEE_ShadowCascadeData *evscd = (EEVEE_ShadowCascadeData *)led->storage;
785 //              EEVEE_ShadowRender *srd = &linfo->shadow_render_data;
786
787 //              srd->layer = i;
788 //              for (int j = 0; j < MAX_CASCADE_NUM; ++j) {
789 //                      copy_m4_m4(srd->shadowmat[j], evscd->viewprojmat[j]);
790 //              }
791 //              DRW_uniformbuffer_update(sldata->shadow_render_ubo, &linfo->shadow_render_data);
792
793 //              DRW_draw_pass(psl->shadow_cascade_pass);
794 //      }
795 }
796
797 void EEVEE_lights_free(void)
798 {
799         DRW_SHADER_FREE_SAFE(e_data.shadow_sh);
800         DRW_SHADER_FREE_SAFE(e_data.shadow_store_cube_sh);
801         DRW_SHADER_FREE_SAFE(e_data.shadow_store_cascade_sh);
802 }