-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathr2_R_lights.cpp
230 lines (207 loc) · 6.5 KB
/
r2_R_lights.cpp
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
#include "stdafx.h"
IC bool pred_area (light* _1, light* _2)
{
u32 a0 = _1->X.S.size;
u32 a1 = _2->X.S.size;
return a0>a1; // reverse -> descending
}
void CRender::render_lights (light_Package& LP)
{
//////////////////////////////////////////////////////////////////////////
// Refactor order based on ability to pack shadow-maps
// 1. calculate area + sort in descending order
// const u16 smap_unassigned = u16(-1);
{
xr_vector<light*>& source = LP.v_shadowed;
for (u32 it=0; it<source.size(); it++)
{
light* L = source[it];
if (!L->vis.visible) {
source.erase (source.begin()+it);
it--;
} else {
LR.compute_xf_spot (L);
}
}
}
// 2. refactor - infact we could go from the backside and sort in ascending order
{
xr_vector<light*>& source = LP.v_shadowed;
xr_vector<light*> refactored ;
refactored.reserve (source.size());
u32 total = source.size();
for (u16 smap_ID=0; refactored.size()!=total; smap_ID++)
{
LP_smap_pool.initialize (RImplementation.o.smapsize);
std::sort (source.begin(),source.end(),pred_area);
for (u32 test=0; test<source.size(); test++)
{
light* L = source[test];
SMAP_Rect R;
if (LP_smap_pool.push(R,L->X.S.size)) {
// OK
L->X.S.posX = R.min.x;
L->X.S.posY = R.min.y;
L->vis.smap_ID = smap_ID;
refactored.push_back(L);
source.erase (source.begin()+test);
test --;
}
}
}
// save (lights are popped from back)
std::reverse (refactored.begin(),refactored.end());
LP.v_shadowed = refactored;
}
//////////////////////////////////////////////////////////////////////////
// sort lights by importance???
// while (has_any_lights_that_cast_shadows) {
// if (has_point_shadowed) -> generate point shadowmap
// if (has_spot_shadowed) -> generate spot shadowmap
// switch-to-accumulator
// if (has_point_unshadowed) -> accum point unshadowed
// if (has_spot_unshadowed) -> accum spot unshadowed
// if (was_point_shadowed) -> accum point shadowed
// if (was_spot_shadowed) -> accum spot shadowed
// }
// if (left_some_lights_that_doesn't cast shadows)
// accumulate them
HOM.Disable ();
while (LP.v_shadowed.size() )
{
// if (has_spot_shadowed)
xr_vector<light*> L_spot_s;
stats.s_used ++;
// generate spot shadowmap
Target->phase_smap_spot_clear ();
xr_vector<light*>& source = LP.v_shadowed;
light* L = source.back () ;
u16 sid = L->vis.smap_ID ;
while (true)
{
if (source.empty()) break;
L = source.back ();
if (L->vis.smap_ID!=sid) break;
source.pop_back ();
Lights_LastFrame.push_back (L);
// render
phase = PHASE_SMAP;
if (RImplementation.o.Tshadows) r_pmask (true,true );
else r_pmask (true,false );
L->svis.begin ();
r_dsgraph_render_subspace (L->spatial.sector, L->X.S.combine, L->position, TRUE);
bool bNormal = mapNormalPasses[0][0].size() || mapMatrixPasses[0][0].size();
bool bSpecial = mapNormalPasses[1][0].size() || mapMatrixPasses[1][0].size() || mapSorted.size();
if ( bNormal || bSpecial) {
stats.s_merged ++;
L_spot_s.push_back (L);
Target->phase_smap_spot (L);
RCache.set_xform_world (Fidentity);
RCache.set_xform_view (L->X.S.view);
RCache.set_xform_project (L->X.S.project);
r_dsgraph_render_graph (0);
L->X.S.transluent = FALSE;
if (bSpecial) {
L->X.S.transluent = TRUE;
Target->phase_smap_spot_tsh (L);
r_dsgraph_render_graph (1); // normal level, secondary priority
r_dsgraph_render_sorted ( ); // strict-sorted geoms
}
} else {
stats.s_finalclip ++;
}
L->svis.end ();
r_pmask (true,false);
}
// switch-to-accumulator
Target->phase_accumulator ();
HOM.Disable ();
// if (has_point_unshadowed) -> accum point unshadowed
if (!LP.v_point.empty()) {
light* L = LP.v_point.back (); LP.v_point.pop_back ();
if (L->vis.visible) {
Target->accum_point (L);
render_indirect (L);
}
}
// if (has_spot_unshadowed) -> accum spot unshadowed
if (!LP.v_spot.empty()) {
light* L = LP.v_spot.back (); LP.v_spot.pop_back ();
if (L->vis.visible) {
LR.compute_xf_spot (L);
Target->accum_spot (L);
render_indirect (L);
}
}
// if (was_spot_shadowed) -> accum spot shadowed
if (!L_spot_s.empty())
{
for (u32 it=0; it<L_spot_s.size(); it++)
{
Target->accum_spot (L_spot_s[it]);
render_indirect (L_spot_s[it]);
}
if (RImplementation.o.advancedpp && ps_r2_ls_flags.is(R2FLAG_VOLUMETRIC_LIGHTS))
for (u32 it=0; it<L_spot_s.size(); it++)
Target->accum_volumetric(L_spot_s[it]);
L_spot_s.clear ();
}
}
// Point lighting (unshadowed, if left)
if (!LP.v_point.empty()) {
xr_vector<light*>& Lvec = LP.v_point;
for (u32 pid=0; pid<Lvec.size(); pid++) {
if (Lvec[pid]->vis.visible) {
render_indirect (Lvec[pid]);
Target->accum_point (Lvec[pid]);
}
}
Lvec.clear ();
}
// Spot lighting (unshadowed, if left)
if (!LP.v_spot.empty()) {
xr_vector<light*>& Lvec = LP.v_spot;
for (u32 pid=0; pid<Lvec.size(); pid++) {
if (Lvec[pid]->vis.visible) {
LR.compute_xf_spot (Lvec[pid]);
render_indirect (Lvec[pid]);
Target->accum_spot (Lvec[pid]);
}
}
Lvec.clear ();
}
}
void CRender::render_indirect (light* L)
{
if (!ps_r2_ls_flags.test(R2FLAG_GI)) return;
light LIGEN;
LIGEN.set_type (IRender_Light::REFLECTED);
LIGEN.set_shadow (false);
LIGEN.set_cone (PI_DIV_2*2.f);
xr_vector<light_indirect>& Lvec = L->indirect;
if (Lvec.empty()) return;
float LE = L->color.intensity ();
for (u32 it=0; it<Lvec.size(); it++) {
light_indirect& LI = Lvec[it];
// energy and color
float LIE = LE*LI.E;
if (LIE < ps_r2_GI_clip) continue;
Fvector T; T.set(L->color.r,L->color.g,L->color.b).mul(LI.E);
LIGEN.set_color (T.x,T.y,T.z);
// geometric
Fvector L_up,L_right; L_up.set (0,1,0); if (_abs(L_up.dotproduct(LI.D))>.99f) L_up.set(0,0,1);
L_right.crossproduct (L_up,LI.D).normalize ();
LIGEN.spatial.sector = LI.S;
LIGEN.set_position (LI.P);
LIGEN.set_rotation (LI.D,L_right);
// range
// dist^2 / range^2 = A - has infinity number of solutions
// approximate energy by linear fallof Emax / (1 + x) = Emin
float Emax = LIE;
float Emin = 1.f / 255.f;
float x = (Emax - Emin)/Emin;
if (x < 0.1f) continue;
LIGEN.set_range (x);
Target->accum_reflected (&LIGEN);
}
}