From cba2374abd35e77055bff817c282d8975be48b9d Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Mon, 3 Jan 2022 22:56:26 -0800 Subject: [PATCH] softgpu: Separate calculation of S/T. We could probably reuse, but we're not right now and it complicates the logic. --- GPU/Software/Lighting.cpp | 36 +++++++++++++++------------------- GPU/Software/Lighting.h | 3 ++- GPU/Software/TransformUnit.cpp | 5 ++++- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/GPU/Software/Lighting.cpp b/GPU/Software/Lighting.cpp index d4d7a020ee..35a1dde62a 100644 --- a/GPU/Software/Lighting.cpp +++ b/GPU/Software/Lighting.cpp @@ -43,6 +43,22 @@ static inline float pspLightPow(float v, float e) { return v; } +static inline float GenerateLightCoord(VertexData &vertex, int light) { + // TODO: Should specular lighting should affect this, too? Doesn't in GLES. + Vec3 L = GetLightVec(gstate.lpos, light); + // In other words, L.Length2() == 0.0f means Dot({0, 0, 1}, worldnormal). + float diffuse_factor = Dot(L.NormalizedOr001(cpu_info.bSSE4_1), vertex.worldnormal); + + return (diffuse_factor + 1.0f) / 2.0f; +} + +void GenerateLightST(VertexData &vertex) { + // Always calculate texture coords from lighting results if environment mapping is active + // This should be done even if lighting is disabled altogether. + vertex.texturecoords.s() = GenerateLightCoord(vertex, gstate.getUVLS0()); + vertex.texturecoords.t() = GenerateLightCoord(vertex, gstate.getUVLS1()); +} + void Process(VertexData& vertex, bool hasColor) { const int materialupdate = gstate.materialupdate & (hasColor ? 7 : 0); @@ -53,26 +69,6 @@ void Process(VertexData& vertex, bool hasColor) { Vec3 final_color = mec + mac * Vec3::FromRGB(gstate.getAmbientRGBA()); Vec3 specular_color(0.0f, 0.0f, 0.0f); - for (unsigned int light = 0; light < 4; ++light) { - // Always calculate texture coords from lighting results if environment mapping is active - // TODO: Should specular lighting should affect this, too? Doesn't in GLES. - // This should be done even if lighting is disabled altogether. - if (gstate.getUVGenMode() == GE_TEXMAP_ENVIRONMENT_MAP) { - Vec3 L = GetLightVec(gstate.lpos, light); - // In other words, L.Length2() == 0.0f means Dot({0, 0, 1}, worldnormal). - float diffuse_factor = Dot(L.NormalizedOr001(cpu_info.bSSE4_1), vertex.worldnormal); - - if (gstate.getUVLS0() == (int)light) - vertex.texturecoords.s() = (diffuse_factor + 1.f) / 2.f; - - if (gstate.getUVLS1() == (int)light) - vertex.texturecoords.t() = (diffuse_factor + 1.f) / 2.f; - } - } - - if (!gstate.isLightingEnabled()) - return; - for (unsigned int light = 0; light < 4; ++light) { if (!gstate.isLightChanEnabled(light)) continue; diff --git a/GPU/Software/Lighting.h b/GPU/Software/Lighting.h index 9e08eacade..e5915e6aac 100644 --- a/GPU/Software/Lighting.h +++ b/GPU/Software/Lighting.h @@ -21,6 +21,7 @@ namespace Lighting { +void GenerateLightST(VertexData &vertex); void Process(VertexData& vertex, bool hasColor); -} \ No newline at end of file +} diff --git a/GPU/Software/TransformUnit.cpp b/GPU/Software/TransformUnit.cpp index bdecb90372..b15bb0115c 100644 --- a/GPU/Software/TransformUnit.cpp +++ b/GPU/Software/TransformUnit.cpp @@ -277,10 +277,13 @@ VertexData TransformUnit::ReadVertex(VertexReader &vreader, bool &outside_range_ Vec3 stq = tgen * source + Vec3(gstate.tgenMatrix[9], gstate.tgenMatrix[10], gstate.tgenMatrix[11]); float z_recip = 1.0f / stq.z; vertex.texturecoords = Vec2f(stq.x * z_recip, stq.y * z_recip); + } else if (gstate.getUVGenMode() == GE_TEXMAP_ENVIRONMENT_MAP) { + Lighting::GenerateLightST(vertex); } PROFILE_THIS_SCOPE("light"); - Lighting::Process(vertex, vreader.hasColor0()); + if (gstate.isLightingEnabled()) + Lighting::Process(vertex, vreader.hasColor0()); } else { vertex.screenpos.x = (int)(pos[0] * 16) + gstate.getOffsetX16(); vertex.screenpos.y = (int)(pos[1] * 16) + gstate.getOffsetY16();