CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/Software/Lighting.cpp
Views: 1401
1
// Copyright (c) 2013- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include "ppsspp_config.h"
19
#include <cmath>
20
#include "Common/Common.h"
21
#include "Common/CPUDetect.h"
22
#include "GPU/GPUState.h"
23
#include "GPU/Software/Lighting.h"
24
25
namespace Lighting {
26
27
static inline Vec3f GetLightVec(const u32 lparams[12], int light) {
28
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
29
__m128i values = _mm_loadu_si128((__m128i *)&lparams[3 * light]);
30
__m128i from24 = _mm_slli_epi32(values, 8);
31
return _mm_castsi128_ps(from24);
32
#elif PPSSPP_ARCH(ARM64_NEON)
33
uint32x4_t values = vld1q_u32((uint32_t *)&lparams[3 * light]);
34
uint32x4_t from24 = vshlq_n_u32(values, 8);
35
return vreinterpretq_f32_u32(from24);
36
#else
37
return Vec3<float>(getFloat24(lparams[3 * light]), getFloat24(lparams[3 * light + 1]), getFloat24(lparams[3 * light + 2]));
38
#endif
39
}
40
41
static inline float pspLightPow(float v, float e) {
42
if (e <= 0.0f) {
43
return 1.0f;
44
}
45
if (v > 0.0f) {
46
return pow(v, e);
47
}
48
// Negative stays negative, so let's just return the original.
49
return v;
50
}
51
52
static inline Vec4<int> LightColorFactor(const Vec4<int> &expanded, const Vec4<int> &ones) {
53
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
54
return _mm_add_epi32(_mm_slli_epi32(expanded.ivec, 1), ones.ivec);
55
#elif PPSSPP_ARCH(ARM64_NEON)
56
return vaddq_s32(vshlq_n_s32(expanded.ivec, 1), ones.ivec);
57
#else
58
return expanded * 2 + ones;
59
#endif
60
}
61
62
static inline Vec4<int> LightColorFactor(uint32_t c, const Vec4<int> &ones) {
63
return LightColorFactor(Vec4<int>::FromRGBA(c), ones);
64
}
65
66
static inline bool IsLargerThanHalf(const Vec4<int> &v) {
67
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
68
__m128i add23 = _mm_add_epi32(v.ivec, _mm_shuffle_epi32(v.ivec, _MM_SHUFFLE(3, 2, 3, 2)));
69
__m128i add1 = _mm_add_epi32(add23, _mm_shuffle_epi32(add23, _MM_SHUFFLE(1, 1, 1, 1)));
70
return _mm_cvtsi128_si32(add1) > 4;
71
#elif PPSSPP_ARCH(ARM64_NEON)
72
int32x2_t add02 = vpmax_s32(vget_low_s32(v.ivec), vget_high_s32(v.ivec));
73
int32x2_t add1 = vpmax_s32(add02, add02);
74
return vget_lane_s32(add1, 0) > 4;
75
#else
76
bool larger = false;
77
for (int i = 0; i < 3; ++i)
78
larger = v[i] > 1;
79
return larger;
80
#endif
81
}
82
83
void ComputeState(State *state, bool hasColor0) {
84
const Vec4<int> ones = Vec4<int>::AssignToAll(1);
85
86
bool anyAmbient = false;
87
bool anyDiffuse = false;
88
bool anySpecular = false;
89
bool anyNonDirectional = false;
90
for (int light = 0; light < 4; ++light) {
91
auto &lstate = state->lights[light];
92
lstate.enabled = gstate.isLightChanEnabled(light);
93
if (!lstate.enabled)
94
continue;
95
96
lstate.poweredDiffuse = gstate.isUsingPoweredDiffuseLight(light);
97
lstate.specular = gstate.isUsingSpecularLight(light);
98
99
lstate.ambientColorFactor = LightColorFactor(gstate.getLightAmbientColor(light), ones);
100
lstate.ambient = IsLargerThanHalf(lstate.ambientColorFactor);
101
anyAmbient = anyAmbient || lstate.ambient;
102
103
lstate.diffuseColorFactor = LightColorFactor(gstate.getDiffuseColor(light), ones);
104
lstate.diffuse = IsLargerThanHalf(lstate.diffuseColorFactor);
105
anyDiffuse = anyDiffuse || lstate.diffuse;
106
107
if (lstate.specular) {
108
lstate.specularColorFactor = LightColorFactor(gstate.getSpecularColor(light), ones);
109
lstate.specular = IsLargerThanHalf(lstate.specularColorFactor);
110
anySpecular = anySpecular || lstate.specular;
111
}
112
113
// Doesn't actually need to be on if nothing will affect it.
114
if (!lstate.specular && !lstate.ambient && !lstate.diffuse) {
115
lstate.enabled = false;
116
continue;
117
}
118
119
lstate.pos = GetLightVec(gstate.lpos, light);
120
lstate.directional = gstate.isDirectionalLight(light);
121
if (lstate.directional) {
122
lstate.pos.NormalizeOr001();
123
} else {
124
lstate.att = GetLightVec(gstate.latt, light);
125
anyNonDirectional = true;
126
}
127
128
lstate.spot = gstate.isSpotLight(light);
129
if (lstate.spot) {
130
lstate.spotDir = GetLightVec(gstate.ldir, light);
131
lstate.spotDir.Normalize();
132
lstate.spotCutoff = getFloat24(gstate.lcutoff[light]);
133
if (std::isnan(lstate.spotCutoff) && std::signbit(lstate.spotCutoff))
134
lstate.spotCutoff = 0.0f;
135
136
lstate.spotExp = getFloat24(gstate.lconv[light]);
137
if (lstate.spotExp <= 0.0f)
138
lstate.spotExp = 0.0f;
139
else if (std::isnan(lstate.spotExp))
140
lstate.spotExp = std::signbit(lstate.spotExp) ? 0.0f : INFINITY;
141
}
142
}
143
144
const int materialupdate = gstate.materialupdate & (hasColor0 ? 7 : 0);
145
state->colorForAmbient = (materialupdate & 1) != 0;
146
state->colorForDiffuse = (materialupdate & 2) != 0;
147
state->colorForSpecular = (materialupdate & 4) != 0;
148
149
if (!state->colorForAmbient) {
150
state->material.ambientColorFactor = LightColorFactor(gstate.getMaterialAmbientRGBA(), ones);
151
if (!IsLargerThanHalf(state->material.ambientColorFactor) && anyAmbient) {
152
for (int i = 0; i < 4; ++i)
153
state->lights[i].ambient = false;
154
}
155
}
156
157
if (anyDiffuse && !state->colorForDiffuse) {
158
state->material.diffuseColorFactor = LightColorFactor(gstate.getMaterialDiffuse(), ones);
159
if (!IsLargerThanHalf(state->material.diffuseColorFactor)) {
160
anyDiffuse = false;
161
for (int i = 0; i < 4; ++i)
162
state->lights[i].diffuse = false;
163
}
164
}
165
166
if (anySpecular && !state->colorForSpecular) {
167
state->material.specularColorFactor = LightColorFactor(gstate.getMaterialSpecular(), ones);
168
if (!IsLargerThanHalf(state->material.specularColorFactor)) {
169
anySpecular = false;
170
for (int i = 0; i < 4; ++i)
171
state->lights[i].specular = false;
172
}
173
}
174
175
if (anyDiffuse || anySpecular) {
176
state->specularExp = gstate.getMaterialSpecularCoef();
177
if (state->specularExp <= 0.0f)
178
state->specularExp = 0.0f;
179
else if (std::isnan(state->specularExp))
180
state->specularExp = std::signbit(state->specularExp) ? 0.0f : INFINITY;
181
}
182
183
state->baseAmbientColorFactor = LightColorFactor(gstate.getAmbientRGBA(), ones);
184
state->setColor1 = gstate.isUsingSecondaryColor() && anySpecular;
185
state->addColor1 = !gstate.isUsingSecondaryColor() && anySpecular;
186
state->usesWorldPos = anyNonDirectional;
187
state->usesWorldNormal = gstate.getUVGenMode() == GE_TEXMAP_ENVIRONMENT_MAP || anyDiffuse || anySpecular;
188
}
189
190
static inline float GenerateLightCoord(VertexData &vertex, const WorldCoords &worldnormal, int light) {
191
// TODO: Should specular lighting should affect this, too? Doesn't in GLES.
192
Vec3<float> L = GetLightVec(gstate.lpos, light);
193
// In other words, L.Length2() == 0.0f means Dot({0, 0, 1}, worldnormal).
194
float diffuse_factor = Dot(L.NormalizedOr001(cpu_info.bSSE4_1), worldnormal);
195
196
return (diffuse_factor + 1.0f) / 2.0f;
197
}
198
199
void GenerateLightST(VertexData &vertex, const WorldCoords &worldnormal) {
200
// Always calculate texture coords from lighting results if environment mapping is active
201
// This should be done even if lighting is disabled altogether.
202
vertex.texturecoords.s() = GenerateLightCoord(vertex, worldnormal, gstate.getUVLS0());
203
vertex.texturecoords.t() = GenerateLightCoord(vertex, worldnormal, gstate.getUVLS1());
204
}
205
206
#if defined(_M_SSE)
207
#if defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER)
208
[[gnu::target("sse4.1")]]
209
#endif
210
static inline int LightCeilSSE4(float f) {
211
__m128 v = _mm_set_ss(f);
212
// This isn't terribly fast, but seems to be better than calling ceilf().
213
return _mm_cvt_ss2si(_mm_ceil_ss(v, v));
214
}
215
216
#if defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER)
217
[[gnu::target("sse4.1")]]
218
#endif
219
static inline __m128i LightColorScaleBy512SSE4(__m128i factor, __m128i color, __m128i scale) {
220
// We can use 16-bit multiply here (faster than 32-bit multiply) since our top bits are zero.
221
__m128i result18 = _mm_madd_epi16(factor, color);
222
// But now with 18 bits, we need a full multiply.
223
__m128i multiplied = _mm_mullo_epi32(result18, scale);
224
return _mm_srai_epi32(multiplied, 10 + 9);
225
}
226
#endif
227
228
template <bool useSSE4>
229
static inline int LightCeil(float f) {
230
#if defined(_M_SSE)
231
if (useSSE4)
232
return LightCeilSSE4(f);
233
#elif PPSSPP_ARCH(ARM64_NEON)
234
return vcvtps_s32_f32(f);
235
#endif
236
return (int)ceilf(f);
237
}
238
239
template <bool useSSE4>
240
static Vec4<int> LightColorScaleBy512(const Vec4<int> &factor, const Vec4<int> &color, int scale) {
241
// We multiply s9 * s9 * s9, resulting in s27, then shift off 19 to get 8-bit.
242
// The reason all factors are s9 is to account for rounding.
243
// Also note that all values are positive, so can be treated as unsigned.
244
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
245
if (useSSE4)
246
return LightColorScaleBy512SSE4(factor.ivec, color.ivec, _mm_set1_epi32(scale));
247
#elif PPSSPP_ARCH(ARM64_NEON)
248
int32x4_t multiplied = vmulq_n_s32(vmulq_s32(factor.ivec, color.ivec), scale);
249
return vshrq_n_s32(multiplied, 10 + 9);
250
#endif
251
return (factor * color * scale) >> (10 + 9);
252
}
253
254
static inline void LightColorSum(Vec4<int> &sum, const Vec4<int> &src) {
255
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
256
sum.ivec = _mm_add_epi32(sum.ivec, src.ivec);
257
#elif PPSSPP_ARCH(ARM64_NEON)
258
sum.ivec = vaddq_s32(sum.ivec, src.ivec);
259
#else
260
sum += src;
261
#endif
262
}
263
264
static inline float Dot33(const Vec3f &a, const Vec3f &b) {
265
#if defined(_M_SSE)
266
__m128 v = _mm_mul_ps(SAFE_M128(a.vec), SAFE_M128(b.vec)); // [X, Y, Z, W]
267
__m128 shuf = _mm_shuffle_ps(v, v, _MM_SHUFFLE(3, 2, 0, 1)); // [Y, X, Z, W]
268
__m128 sums = _mm_add_ps(v, shuf); // [X + Y, X + Y, Z + Z, W + W]
269
shuf = _mm_movehl_ps(shuf, shuf); // [Z, W, Z, W]
270
return _mm_cvtss_f32(_mm_add_ss(sums, shuf)); // X + Y + Z
271
#elif PPSSPP_ARCH(ARM64_NEON)
272
float32x4_t multipled = vsetq_lane_f32(0.0f, vmulq_f32(a.vec, b.vec), 3);
273
float32x2_t add1 = vget_low_f32(vpaddq_f32(multipled, multipled));
274
float32x2_t add2 = vpadd_f32(add1, add1);
275
return vget_lane_f32(add2, 0);
276
#else
277
return Dot(a, b);
278
#endif
279
}
280
281
template <bool useSSE4>
282
static void ProcessSIMD(VertexData &vertex, const WorldCoords &worldpos, const WorldCoords &worldnormal, const State &state) {
283
// Lighting blending rounds using the half offset method (like alpha blend.)
284
Vec4<int> colorFactor;
285
if (state.colorForAmbient || state.colorForDiffuse || state.colorForSpecular) {
286
const Vec4<int> ones = Vec4<int>::AssignToAll(1);
287
colorFactor = LightColorFactor(vertex.color0, ones);
288
}
289
290
Vec4<int> mec = Vec4<int>::FromRGBA(gstate.getMaterialEmissive());
291
292
Vec4<int> mac = state.colorForAmbient ? colorFactor : state.material.ambientColorFactor;
293
Vec4<int> ambient = (mac * state.baseAmbientColorFactor) >> 10;
294
295
Vec4<int> final_color = mec + ambient;
296
Vec4<int> specular_color = Vec4<int>::AssignToAll(0);
297
298
for (unsigned int light = 0; light < 4; ++light) {
299
const auto &lstate = state.lights[light];
300
if (!lstate.enabled)
301
continue;
302
303
// L = vector from vertex to light source
304
// TODO: Should transfer the light positions to world/view space for these calculations?
305
Vec3<float> L = lstate.pos;
306
float attspot = 1.0f;
307
if (!lstate.directional) {
308
L -= worldpos;
309
// TODO: Should this normalize (0, 0, 0) to (0, 0, 1)?
310
float d = L.NormalizeOr001();
311
312
float att = 1.0f / Dot33(lstate.att, Vec3f(1.0f, d, d * d));
313
if (!(att > 0.0f))
314
att = 0.0f;
315
else if (att > 1.0f)
316
att = 1.0f;
317
attspot = att;
318
}
319
320
if (lstate.spot) {
321
float rawSpot = Dot33(lstate.spotDir, L);
322
if (std::isnan(rawSpot))
323
rawSpot = std::signbit(rawSpot) ? 0.0f : 1.0f;
324
325
float spot = 1.0f;
326
if (rawSpot >= lstate.spotCutoff) {
327
spot = pspLightPow(rawSpot, lstate.spotExp);
328
if (std::isnan(spot))
329
spot = 0.0f;
330
} else {
331
spot = 0.0f;
332
}
333
334
attspot *= spot;
335
}
336
337
// ambient lighting
338
if (lstate.ambient) {
339
int attspot512 = (int)LightCeil<useSSE4>(256 * 2 * attspot + 1);
340
if (attspot512 > 512)
341
attspot512 = 512;
342
Vec4<int> lambient = LightColorScaleBy512<useSSE4>(lstate.ambientColorFactor, mac, attspot512);
343
LightColorSum(final_color, lambient);
344
}
345
346
// diffuse lighting
347
float diffuse_factor;
348
if (lstate.diffuse || lstate.specular) {
349
diffuse_factor = Dot33(L, worldnormal);
350
if (lstate.poweredDiffuse) {
351
diffuse_factor = pspLightPow(diffuse_factor, state.specularExp);
352
}
353
}
354
355
if (lstate.diffuse && diffuse_factor > 0.0f) {
356
int diffuse_attspot = (int)LightCeil<useSSE4>(256 * 2 * attspot * diffuse_factor + 1);
357
if (diffuse_attspot > 512)
358
diffuse_attspot = 512;
359
Vec4<int> mdc = state.colorForDiffuse ? colorFactor : state.material.diffuseColorFactor;
360
Vec4<int> ldiffuse = LightColorScaleBy512<useSSE4>(lstate.diffuseColorFactor, mdc, diffuse_attspot);
361
LightColorSum(final_color, ldiffuse);
362
}
363
364
if (lstate.specular && diffuse_factor >= 0.0f) {
365
Vec3<float> H = L + Vec3<float>(0.f, 0.f, 1.f);
366
367
float specular_factor = Dot33(H.NormalizedOr001(useSSE4), worldnormal);
368
specular_factor = pspLightPow(specular_factor, state.specularExp);
369
370
if (specular_factor > 0.0f) {
371
int specular_attspot = (int)LightCeil<useSSE4>(256 * 2 * attspot * specular_factor + 1);
372
if (specular_attspot > 512)
373
specular_attspot = 512;
374
375
Vec4<int> msc = state.colorForSpecular ? colorFactor : state.material.specularColorFactor;
376
Vec4<int> lspecular = LightColorScaleBy512<useSSE4>(lstate.specularColorFactor, msc, specular_attspot);
377
LightColorSum(specular_color, lspecular);
378
}
379
}
380
}
381
382
// Note: these are all naturally clamped by ToRGBA/toRGB.
383
if (state.setColor1) {
384
vertex.color0 = final_color.ToRGBA();
385
vertex.color1 = specular_color.rgb().ToRGB();
386
} else if (state.addColor1) {
387
vertex.color0 = (final_color + specular_color).ToRGBA();
388
} else {
389
vertex.color0 = final_color.ToRGBA();
390
}
391
}
392
393
void Process(VertexData &vertex, const WorldCoords &worldpos, const WorldCoords &worldnormal, const State &state) {
394
#ifdef _M_SSE
395
if (cpu_info.bSSE4_1) {
396
ProcessSIMD<true>(vertex, worldpos, worldnormal, state);
397
return;
398
}
399
#endif
400
ProcessSIMD<false>(vertex, worldpos, worldnormal, state);
401
}
402
403
} // namespace
404
405