1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
|
/*
* Copyright 2016, Blender Foundation.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contributor(s): Blender Institute
*
*/
/** \file eevee_lights.c
* \ingroup DNA
*/
#include "DRW_render.h"
#include "eevee_engine.h"
#include "eevee_private.h"
typedef struct EEVEE_LightData {
short light_id, shadow_id;
} EEVEE_LightData;
typedef struct EEVEE_ShadowCubeData {
short light_id, shadow_id;
float viewprojmat[6][4][4];
} EEVEE_ShadowCubeData;
typedef struct EEVEE_ShadowMapData {
short light_id, shadow_id;
float viewprojmat[4][4]; /* World->Lamp->NDC : used for rendering the shadow map. */
} EEVEE_ShadowMapData;
typedef struct EEVEE_ShadowCascadeData {
short light_id, shadow_id;
float viewprojmat[MAX_CASCADE_NUM][4][4]; /* World->Lamp->NDC : used for rendering the shadow map. */
} EEVEE_ShadowCascadeData;
/* *********** FUNCTIONS *********** */
void EEVEE_lights_init(EEVEE_StorageList *stl)
{
const unsigned int shadow_ubo_size = sizeof(EEVEE_ShadowCube) * MAX_SHADOW_CUBE +
sizeof(EEVEE_ShadowMap) * MAX_SHADOW_MAP +
sizeof(EEVEE_ShadowCascade) * MAX_SHADOW_CASCADE;
if (!stl->lamps) {
stl->lamps = MEM_callocN(sizeof(EEVEE_LampsInfo), "EEVEE_LampsInfo");
stl->light_ubo = DRW_uniformbuffer_create(sizeof(EEVEE_Light) * MAX_LIGHT, NULL);
stl->shadow_ubo = DRW_uniformbuffer_create(shadow_ubo_size, NULL);
stl->shadow_render_ubo = DRW_uniformbuffer_create(sizeof(EEVEE_ShadowRender), NULL);
}
}
void EEVEE_lights_cache_init(EEVEE_StorageList *stl)
{
EEVEE_LampsInfo *linfo = stl->lamps;
linfo->num_light = linfo->num_cube = linfo->num_map = linfo->num_cascade = 0;
memset(linfo->light_ref, 0, sizeof(linfo->light_ref));
memset(linfo->shadow_cube_ref, 0, sizeof(linfo->shadow_cube_ref));
memset(linfo->shadow_map_ref, 0, sizeof(linfo->shadow_map_ref));
memset(linfo->shadow_cascade_ref, 0, sizeof(linfo->shadow_cascade_ref));
}
void EEVEE_lights_cache_add(EEVEE_StorageList *stl, Object *ob)
{
EEVEE_LampsInfo *linfo = stl->lamps;
/* Step 1 find all lamps in the scene and setup them */
if (linfo->num_light > MAX_LIGHT) {
printf("Too much lamps in the scene !!!\n");
linfo->num_light = MAX_LIGHT;
}
else {
Lamp *la = (Lamp *)ob->data;
EEVEE_LampEngineData *led = (EEVEE_LampEngineData *)DRW_lamp_engine_data_get(ob, &DRW_engine_viewport_eevee_type);
DRW_lamp_engine_data_free((void *)led);
#if 0 /* TODO Waiting for notified refresh. only on scene change. Else too much perf cost. */
if (la->mode & (LA_SHAD_BUF | LA_SHAD_RAY)) {
if (la->type == LA_SUN && linfo->num_cascade < MAX_SHADOW_CASCADE) {
led->sto = MEM_mallocN(sizeof(EEVEE_ShadowCascadeData), "EEVEE_ShadowCascadeData");
((EEVEE_ShadowCascadeData *)led->sto)->shadow_id = linfo->num_cascade;
linfo->shadow_cascade_ref[linfo->num_cascade] = ob;
linfo->num_cascade++;
}
else if ((la->type == LA_SPOT || la->type == LA_LOCAL || la->type == LA_AREA)
&& linfo->num_cube < MAX_SHADOW_CUBE) {
led->sto = MEM_mallocN(sizeof(EEVEE_ShadowCubeData), "EEVEE_ShadowCubeData");
((EEVEE_ShadowCubeData *)led->sto)->shadow_id = linfo->num_cube;
linfo->shadow_cube_ref[linfo->num_cube] = ob;
linfo->num_cube++;
}
}
#else
UNUSED_VARS(la);
#endif
if (!led->sto) {
led->sto = MEM_mallocN(sizeof(EEVEE_LightData), "EEVEE_LightData");
((EEVEE_LightData *)led->sto)->shadow_id = -1;
}
((EEVEE_LightData *)led->sto)->light_id = linfo->num_light;
linfo->light_ref[linfo->num_light] = ob;
linfo->num_light++;
}
}
void EEVEE_lights_cache_finish(EEVEE_StorageList *stl, EEVEE_TextureList *txl, EEVEE_FramebufferList *fbl)
{
EEVEE_LampsInfo *linfo = stl->lamps;
/* Step 4 Update Lamp UBOs */
EEVEE_lights_update(stl);
/* Step 5 Setup enough layers */
/* Free textures if number mismatch */
if (linfo->num_cube != linfo->cache_num_cube) {
DRW_TEXTURE_FREE_SAFE(txl->shadow_depth_cube_pool);
linfo->cache_num_cube = linfo->num_cube;
}
if (linfo->num_map != linfo->cache_num_map) {
DRW_TEXTURE_FREE_SAFE(txl->shadow_depth_map_pool);
linfo->cache_num_map = linfo->num_map;
}
if (linfo->num_cascade != linfo->cache_num_cascade) {
DRW_TEXTURE_FREE_SAFE(txl->shadow_depth_cascade_pool);
linfo->cache_num_cascade = linfo->num_cascade;
}
/* Initialize Textures Arrays first so DRW_framebuffer_init just bind them */
if (!txl->shadow_depth_cube_pool) {
txl->shadow_depth_cube_pool = DRW_texture_create_2D_array(
512, 512, MAX2(1, linfo->num_cube * 6), DRW_TEX_DEPTH_24,
DRW_TEX_FILTER | DRW_TEX_COMPARE, NULL);
if (fbl->shadow_cube_fb) {
DRW_framebuffer_texture_attach(fbl->shadow_cube_fb, txl->shadow_depth_cube_pool, 0, 0);
}
}
if (!txl->shadow_depth_map_pool) {
txl->shadow_depth_map_pool = DRW_texture_create_2D_array(
512, 512, MAX2(1, linfo->num_map), DRW_TEX_DEPTH_24,
DRW_TEX_FILTER | DRW_TEX_COMPARE, NULL);
if (fbl->shadow_map_fb) {
DRW_framebuffer_texture_attach(fbl->shadow_map_fb, txl->shadow_depth_map_pool, 0, 0);
}
}
if (!txl->shadow_depth_cascade_pool) {
txl->shadow_depth_cascade_pool = DRW_texture_create_2D_array(
512, 512, MAX2(1, linfo->num_cascade * MAX_CASCADE_NUM), DRW_TEX_DEPTH_24,
DRW_TEX_FILTER | DRW_TEX_COMPARE, NULL);
if (fbl->shadow_cascade_fb) {
DRW_framebuffer_texture_attach(fbl->shadow_cascade_fb, txl->shadow_depth_map_pool, 0, 0);
}
}
DRWFboTexture tex_cube = {&txl->shadow_depth_cube_pool, DRW_BUF_DEPTH_24, DRW_TEX_FILTER | DRW_TEX_COMPARE};
DRW_framebuffer_init(&fbl->shadow_cube_fb, 512, 512, &tex_cube, 1);
DRWFboTexture tex_map = {&txl->shadow_depth_map_pool, DRW_BUF_DEPTH_24, DRW_TEX_FILTER | DRW_TEX_COMPARE};
DRW_framebuffer_init(&fbl->shadow_map_fb, 512, 512, &tex_map, 1);
DRWFboTexture tex_cascade = {&txl->shadow_depth_cascade_pool, DRW_BUF_DEPTH_24, DRW_TEX_FILTER | DRW_TEX_COMPARE};
DRW_framebuffer_init(&fbl->shadow_cascade_fb, 512, 512, &tex_cascade, 1);
}
/* Update buffer with lamp data */
static void eevee_light_setup(Object *ob, EEVEE_LampsInfo *linfo, EEVEE_LampEngineData *led)
{
/* TODO only update if data changes */
EEVEE_LightData *evld = led->sto;
EEVEE_Light *evli = linfo->light_data + evld->light_id;
Lamp *la = (Lamp *)ob->data;
float mat[4][4], scale[3], power;
/* Position */
copy_v3_v3(evli->position, ob->obmat[3]);
/* Color */
evli->color[0] = la->r * la->energy;
evli->color[1] = la->g * la->energy;
evli->color[2] = la->b * la->energy;
/* Influence Radius */
evli->dist = la->dist;
/* Vectors */
normalize_m4_m4_ex(mat, ob->obmat, scale);
copy_v3_v3(evli->forwardvec, mat[2]);
normalize_v3(evli->forwardvec);
negate_v3(evli->forwardvec);
copy_v3_v3(evli->rightvec, mat[0]);
normalize_v3(evli->rightvec);
copy_v3_v3(evli->upvec, mat[1]);
normalize_v3(evli->upvec);
/* Spot size & blend */
if (la->type == LA_SPOT) {
evli->sizex = scale[0] / scale[2];
evli->sizey = scale[1] / scale[2];
evli->spotsize = cosf(la->spotsize * 0.5f);
evli->spotblend = (1.0f - evli->spotsize) * la->spotblend;
evli->radius = MAX2(0.001f, la->area_size);
}
else if (la->type == LA_AREA) {
evli->sizex = MAX2(0.0001f, la->area_size * scale[0] * 0.5f);
if (la->area_shape == LA_AREA_RECT) {
evli->sizey = MAX2(0.0001f, la->area_sizey * scale[1] * 0.5f);
}
else {
evli->sizey = MAX2(0.0001f, la->area_size * scale[1] * 0.5f);
}
}
else {
evli->radius = MAX2(0.001f, la->area_size);
}
/* Make illumination power constant */
if (la->type == LA_AREA) {
power = 1.0f / (evli->sizex * evli->sizey * 4.0f * M_PI) /* 1/(w*h*Pi) */
* 80.0f; /* XXX : Empirical, Fit cycles power */
}
else if (la->type == LA_SPOT || la->type == LA_LOCAL) {
power = 1.0f / (4.0f * evli->radius * evli->radius * M_PI * M_PI) /* 1/(4*r²*Pi²) */
* M_PI * M_PI * M_PI * 10.0; /* XXX : Empirical, Fit cycles power */
/* for point lights (a.k.a radius == 0.0) */
// power = M_PI * M_PI * 0.78; /* XXX : Empirical, Fit cycles power */
}
else {
power = 1.0f;
}
mul_v3_fl(evli->color, power);
/* Lamp Type */
evli->lamptype = (float)la->type;
/* No shadow by default */
evli->shadowid = -1.0f;
}
static void eevee_shadow_cube_setup(Object *ob, EEVEE_LampsInfo *linfo, EEVEE_LampEngineData *led)
{
float projmat[4][4];
EEVEE_ShadowCubeData *evsmp = (EEVEE_ShadowCubeData *)led->sto;
EEVEE_Light *evli = linfo->light_data + evsmp->light_id;
EEVEE_ShadowCube *evsh = linfo->shadow_cube_data + evsmp->shadow_id;
Lamp *la = (Lamp *)ob->data;
perspective_m4(projmat, -la->clipsta, la->clipsta, -la->clipsta, la->clipsta, la->clipsta, la->clipend);
for (int i = 0; i < 6; ++i) {
float tmp[4][4];
unit_m4(tmp);
negate_v3_v3(tmp[3], ob->obmat[3]);
mul_m4_m4m4(tmp, cubefacemat[i], tmp);
mul_m4_m4m4(evsmp->viewprojmat[i], projmat, tmp);
}
evsh->bias = 0.05f * la->bias;
evsh->near = la->clipsta;
evsh->far = la->clipend;
evli->shadowid = (float)(evsmp->shadow_id);
}
static void eevee_shadow_map_setup(Object *ob, EEVEE_LampsInfo *linfo, EEVEE_LampEngineData *led)
{
float viewmat[4][4], projmat[4][4];
EEVEE_ShadowMapData *evsmp = (EEVEE_ShadowMapData *)led->sto;
EEVEE_Light *evli = linfo->light_data + evsmp->light_id;
EEVEE_ShadowMap *evsh = linfo->shadow_map_data + evsmp->shadow_id;
Lamp *la = (Lamp *)ob->data;
invert_m4_m4(viewmat, ob->obmat);
normalize_v3(viewmat[0]);
normalize_v3(viewmat[1]);
normalize_v3(viewmat[2]);
float wsize = la->shadow_frustum_size;
orthographic_m4(projmat, -wsize, wsize, -wsize, wsize, la->clipsta, la->clipend);
mul_m4_m4m4(evsmp->viewprojmat, projmat, viewmat);
mul_m4_m4m4(evsh->shadowmat, texcomat, evsmp->viewprojmat);
evsh->bias = 0.005f * la->bias;
evli->shadowid = (float)(MAX_SHADOW_CUBE + evsmp->shadow_id);
}
#define LERP(t, a, b) ((a) + (t) * ((b) - (a)))
static void frustum_min_bounding_sphere(const float corners[8][4], float r_center[3], float *r_radius)
{
#if 0 /* Simple solution but waist too much space. */
float minvec[3], maxvec[3];
/* compute the bounding box */
INIT_MINMAX(minvec, maxvec);
for (int i = 0; i < 8; ++i) {
minmax_v3v3_v3(minvec, maxvec, corners[i]);
}
/* compute the bounding sphere of this box */
r_radius = len_v3v3(minvec, maxvec) * 0.5f;
add_v3_v3v3(r_center, minvec, maxvec);
mul_v3_fl(r_center, 0.5f);
#else
/* Make the bouding sphere always centered on the front diagonal */
add_v3_v3v3(r_center, corners[4], corners[7]);
mul_v3_fl(r_center, 0.5f);
*r_radius = len_v3v3(corners[0], r_center);
/* Search the largest distance between the sphere center
* and the front plane corners. */
for (int i = 0; i < 4; ++i) {
float rad = len_v3v3(corners[4+i], r_center);
if (rad > *r_radius) {
*r_radius = rad;
}
}
#endif
}
static void eevee_shadow_cascade_setup(Object *ob, EEVEE_LampsInfo *linfo, EEVEE_LampEngineData *led)
{
/* Camera Matrices */
float persmat[4][4], persinv[4][4];
float viewprojmat[4][4], projinv[4][4];
float near, far;
float near_v[4] = {0.0f, 0.0f, -1.0f, 1.0f};
float far_v[4] = {0.0f, 0.0f, 1.0f, 1.0f};
bool is_persp = DRW_viewport_is_persp_get();
DRW_viewport_matrix_get(persmat, DRW_MAT_PERS);
invert_m4_m4(persinv, persmat);
/* FIXME : Get near / far from Draw manager? */
DRW_viewport_matrix_get(viewprojmat, DRW_MAT_WIN);
invert_m4_m4(projinv, viewprojmat);
mul_m4_v4(projinv, near_v);
mul_m4_v4(projinv, far_v);
near = near_v[2];
far = far_v[2]; /* TODO: Should be a shadow parameter */
if (is_persp) {
near /= near_v[3];
far /= far_v[3];
}
/* Lamps Matrices */
float viewmat[4][4], projmat[4][4];
int cascade_ct = MAX_CASCADE_NUM;
float shadow_res = 512.0f; /* TODO parameter */
EEVEE_ShadowCascadeData *evscp = (EEVEE_ShadowCascadeData *)led->sto;
EEVEE_Light *evli = linfo->light_data + evscp->light_id;
EEVEE_ShadowCascade *evsh = linfo->shadow_cascade_data + evscp->shadow_id;
Lamp *la = (Lamp *)ob->data;
/* The technique consists into splitting
* the view frustum into several sub-frustum
* that are individually receiving one shadow map */
/* init near/far */
for (int c = 0; c < MAX_CASCADE_NUM; ++c) {
evsh->split[c] = far;
}
/* Compute split planes */
float splits_ndc[MAX_CASCADE_NUM + 1];
splits_ndc[0] = -1.0f;
splits_ndc[cascade_ct] = 1.0f;
for (int c = 1; c < cascade_ct; ++c) {
const float lambda = 0.8f; /* TODO : Parameter */
/* View Space */
float linear_split = LERP(((float)(c) / (float)cascade_ct), near, far);
float exp_split = near * powf(far / near, (float)(c) / (float)cascade_ct);
if (is_persp) {
evsh->split[c-1] = LERP(lambda, linear_split, exp_split);
}
else {
evsh->split[c-1] = linear_split;
}
/* NDC Space */
float p[4] = {1.0f, 1.0f, evsh->split[c-1], 1.0f};
mul_m4_v4(viewprojmat, p);
splits_ndc[c] = p[2];
if (is_persp) {
splits_ndc[c] /= p[3];
}
}
/* For each cascade */
for (int c = 0; c < cascade_ct; ++c) {
/* Given 8 frustrum corners */
float corners[8][4] = {
/* Near Cap */
{-1.0f, -1.0f, splits_ndc[c], 1.0f},
{ 1.0f, -1.0f, splits_ndc[c], 1.0f},
{-1.0f, 1.0f, splits_ndc[c], 1.0f},
{ 1.0f, 1.0f, splits_ndc[c], 1.0f},
/* Far Cap */
{-1.0f, -1.0f, splits_ndc[c+1], 1.0f},
{ 1.0f, -1.0f, splits_ndc[c+1], 1.0f},
{-1.0f, 1.0f, splits_ndc[c+1], 1.0f},
{ 1.0f, 1.0f, splits_ndc[c+1], 1.0f}
};
/* Transform them into world space */
for (int i = 0; i < 8; ++i) {
mul_m4_v4(persinv, corners[i]);
mul_v3_fl(corners[i], 1.0f / corners[i][3]);
corners[i][3] = 1.0f;
}
/* Project them into light space */
invert_m4_m4(viewmat, ob->obmat);
normalize_v3(viewmat[0]);
normalize_v3(viewmat[1]);
normalize_v3(viewmat[2]);
for (int i = 0; i < 8; ++i) {
mul_m4_v4(viewmat, corners[i]);
}
float center[3], radius;
frustum_min_bounding_sphere(corners, center, &radius);
/* Snap projection center to nearest texel to cancel shimering. */
float shadow_origin[2], shadow_texco[2];
mul_v2_v2fl(shadow_origin, center, shadow_res / (2.0f * radius)); /* Light to texture space. */
/* Find the nearest texel. */
shadow_texco[0] = round(shadow_origin[0]);
shadow_texco[1] = round(shadow_origin[1]);
/* Compute offset. */
sub_v2_v2(shadow_texco, shadow_origin);
mul_v2_fl(shadow_texco, (2.0f * radius) / shadow_res); /* Texture to light space. */
/* Apply offset. */
add_v2_v2(center, shadow_texco);
/* Expand the projection to cover frustum range */
orthographic_m4(projmat,
center[0] - radius,
center[0] + radius,
center[1] - radius,
center[1] + radius,
la->clipsta, la->clipend);
mul_m4_m4m4(evscp->viewprojmat[c], projmat, viewmat);
mul_m4_m4m4(evsh->shadowmat[c], texcomat, evscp->viewprojmat[c]);
/* TODO modify bias depending on the cascade radius */
evsh->bias[c] = 0.005f * la->bias;
}
evli->shadowid = (float)(MAX_SHADOW_CUBE + MAX_SHADOW_MAP + evscp->shadow_id);
}
void EEVEE_lights_update(EEVEE_StorageList *stl)
{
EEVEE_LampsInfo *linfo = stl->lamps;
Object *ob;
int i;
for (i = 0; (ob = linfo->light_ref[i]) && (i < MAX_LIGHT); i++) {
EEVEE_LampEngineData *led = (EEVEE_LampEngineData *)DRW_lamp_engine_data_get(ob, &DRW_engine_viewport_eevee_type);
eevee_light_setup(ob, linfo, led);
}
for (i = 0; (ob = linfo->shadow_cube_ref[i]) && (i < MAX_SHADOW_CUBE); i++) {
EEVEE_LampEngineData *led = (EEVEE_LampEngineData *)DRW_lamp_engine_data_get(ob, &DRW_engine_viewport_eevee_type);
eevee_shadow_cube_setup(ob, linfo, led);
}
for (i = 0; (ob = linfo->shadow_map_ref[i]) && (i < MAX_SHADOW_MAP); i++) {
EEVEE_LampEngineData *led = (EEVEE_LampEngineData *)DRW_lamp_engine_data_get(ob, &DRW_engine_viewport_eevee_type);
eevee_shadow_map_setup(ob, linfo, led);
}
for (i = 0; (ob = linfo->shadow_cascade_ref[i]) && (i < MAX_SHADOW_CASCADE); i++) {
EEVEE_LampEngineData *led = (EEVEE_LampEngineData *)DRW_lamp_engine_data_get(ob, &DRW_engine_viewport_eevee_type);
eevee_shadow_cascade_setup(ob, linfo, led);
}
DRW_uniformbuffer_update(stl->light_ubo, &linfo->light_data);
DRW_uniformbuffer_update(stl->shadow_ubo, &linfo->shadow_cube_data); /* Update all data at once */
}
/* this refresh lamps shadow buffers */
void EEVEE_draw_shadows(EEVEE_Data *vedata)
{
EEVEE_PassList *psl = vedata->psl;
EEVEE_StorageList *stl = vedata->stl;
EEVEE_FramebufferList *fbl = vedata->fbl;
EEVEE_LampsInfo *linfo = stl->lamps;
Object *ob;
int i;
/* Cube Shadow Maps */
/* For old hardware support, we render each face of the shadow map
* onto 6 layer of a big 2D texture array and sample manualy the right layer
* in the fragment shader. */
DRW_framebuffer_bind(fbl->shadow_cube_fb);
DRW_framebuffer_clear(false, true, false, NULL, 1.0);
/* Render each shadow to one layer of the array */
for (i = 0; (ob = linfo->shadow_cube_ref[i]) && (i < MAX_SHADOW_CUBE); i++) {
EEVEE_LampEngineData *led = (EEVEE_LampEngineData *)DRW_lamp_engine_data_get(ob, &DRW_engine_viewport_eevee_type);
EEVEE_ShadowCubeData *evscd = (EEVEE_ShadowCubeData *)led->sto;
EEVEE_ShadowRender *srd = &linfo->shadow_render_data;
srd->layer = i;
for (int j = 0; j < 6; ++j) {
copy_m4_m4(srd->shadowmat[j], evscd->viewprojmat[j]);
}
DRW_uniformbuffer_update(stl->shadow_render_ubo, &linfo->shadow_render_data);
DRW_draw_pass(psl->shadow_cube_pass);
}
#if 0
/* Standard Shadow Maps */
DRW_framebuffer_bind(fbl->shadow_map_fb);
DRW_framebuffer_clear(false, true, false, NULL, 1.0);
/* Render each shadow to one layer of the array */
for (i = 0; (ob = linfo->shadow_map_ref[i]) && (i < MAX_SHADOW_MAP); i++) {
EEVEE_LampEngineData *led = (EEVEE_LampEngineData *)DRW_lamp_engine_data_get(ob, &DRW_engine_viewport_eevee_type);
EEVEE_ShadowMapData *evsmd = (EEVEE_ShadowMapData *)led->sto;
linfo->layer = i;
copy_m4_m4(linfo->shadowmat, evsmd->viewprojmat);
DRW_draw_pass(vedata->psl->shadow_pass);
}
#endif
/* Cascaded Shadow Maps */
DRW_framebuffer_bind(fbl->shadow_cascade_fb);
DRW_framebuffer_clear(false, true, false, NULL, 1.0);
/* Render each shadow to one layer of the array */
for (i = 0; (ob = linfo->shadow_cascade_ref[i]) && (i < MAX_SHADOW_CASCADE); i++) {
EEVEE_LampEngineData *led = (EEVEE_LampEngineData *)DRW_lamp_engine_data_get(ob, &DRW_engine_viewport_eevee_type);
EEVEE_ShadowCascadeData *evscd = (EEVEE_ShadowCascadeData *)led->sto;
EEVEE_ShadowRender *srd = &linfo->shadow_render_data;
srd->layer = i;
for (int j = 0; j < MAX_CASCADE_NUM; ++j) {
copy_m4_m4(srd->shadowmat[j], evscd->viewprojmat[j]);
}
DRW_uniformbuffer_update(stl->shadow_render_ubo, &linfo->shadow_render_data);
DRW_draw_pass(psl->shadow_cascade_pass);
}
}
|