From 027baad73ba17f0a5d6311b7abbefbcca7f9b6a5 Mon Sep 17 00:00:00 2001 From: degasus Date: Fri, 30 May 2014 14:54:16 +0200 Subject: [PATCH] VideoCommon: use the Light struct in XF memory --- .../Core/VideoBackends/Software/TransformUnit.cpp | 6 +++--- .../Core/VideoBackends/Software/XFMemLoader.cpp | 2 +- Source/Core/VideoCommon/PixelShaderManager.cpp | 13 ++++++------- Source/Core/VideoCommon/VertexShaderManager.cpp | 15 ++++++++------- Source/Core/VideoCommon/XFMemory.h | 12 ++++-------- 5 files changed, 22 insertions(+), 26 deletions(-) diff --git a/Source/Core/VideoBackends/Software/TransformUnit.cpp b/Source/Core/VideoBackends/Software/TransformUnit.cpp index 2c4c495087..f79c57be00 100644 --- a/Source/Core/VideoBackends/Software/TransformUnit.cpp +++ b/Source/Core/VideoBackends/Software/TransformUnit.cpp @@ -210,7 +210,7 @@ inline float SafeDivide(float n, float d) void LightColor(const Vec3 &pos, const Vec3 &normal, u8 lightNum, const LitChannel &chan, Vec3 &lightCol) { - const LightPointer *light = (const LightPointer*)&xfmem.lights[0x10*lightNum]; + const LightPointer *light = (const LightPointer*)&xfmem.lights[lightNum]; if (!(chan.attnfunc & 1)) { @@ -295,7 +295,7 @@ void LightColor(const Vec3 &pos, const Vec3 &normal, u8 lightNum, const LitChann void LightAlpha(const Vec3 &pos, const Vec3 &normal, u8 lightNum, const LitChannel &chan, float &lightCol) { - const LightPointer *light = (const LightPointer*)&xfmem.lights[0x10*lightNum]; + const LightPointer *light = (const LightPointer*)&xfmem.lights[lightNum]; if (!(chan.attnfunc & 1)) { @@ -476,7 +476,7 @@ void TransformTexCoord(const InputVertexData *src, OutputVertexData *dst, bool s break; case XF_TEXGEN_EMBOSS_MAP: { - const LightPointer *light = (const LightPointer*)&xfmem.lights[0x10*texinfo.embosslightshift]; + const LightPointer *light = (const LightPointer*)&xfmem.lights[texinfo.embosslightshift]; Vec3 ldir = (light->pos - dst->mvPosition).normalized(); float d1 = ldir * dst->normal[1]; diff --git a/Source/Core/VideoBackends/Software/XFMemLoader.cpp b/Source/Core/VideoBackends/Software/XFMemLoader.cpp index d175e6b6dc..bdf8967667 100644 --- a/Source/Core/VideoBackends/Software/XFMemLoader.cpp +++ b/Source/Core/VideoBackends/Software/XFMemLoader.cpp @@ -23,7 +23,7 @@ void XFWritten(u32 transferSize, u32 baseAddress) // fix lights so invalid values don't trash the lighting computations if (baseAddress <= 0x067f && topAddress >= 0x0604) { - u32* x = xfmem.lights; + u32* x = (u32*)xfmem.lights; // go through all lights for (int light = 0; light < 8; light++) diff --git a/Source/Core/VideoCommon/PixelShaderManager.cpp b/Source/Core/VideoCommon/PixelShaderManager.cpp index 551f881c3e..bb1819eb76 100644 --- a/Source/Core/VideoCommon/PixelShaderManager.cpp +++ b/Source/Core/VideoCommon/PixelShaderManager.cpp @@ -108,16 +108,15 @@ void PixelShaderManager::SetConstants() // lights don't have a 1 to 1 mapping, the color component needs to be converted to 4 floats int istart = nLightsChanged[0] / 0x10; int iend = (nLightsChanged[1] + 15) / 0x10; - const float* xfmemptr = (const float*)&xfmem.lights[0x10 * istart]; for (int i = istart; i < iend; ++i) { - u32 color = *(const u32*)(xfmemptr + 3); - constants.plight_colors[i][0] = (color >> 24) & 0xFF; - constants.plight_colors[i][1] = (color >> 16) & 0xFF; - constants.plight_colors[i][2] = (color >> 8) & 0xFF; - constants.plight_colors[i][3] = (color) & 0xFF; - xfmemptr += 4; + const Light& light = xfmem.lights[i]; + constants.plight_colors[i][0] = light.color[3]; + constants.plight_colors[i][1] = light.color[2]; + constants.plight_colors[i][2] = light.color[1]; + constants.plight_colors[i][3] = light.color[0]; + const float* xfmemptr = light.cosatt; for (int j = 0; j < 4; ++j, xfmemptr += 3) { diff --git a/Source/Core/VideoCommon/VertexShaderManager.cpp b/Source/Core/VideoCommon/VertexShaderManager.cpp index ede4ddd9c7..d2c9358692 100644 --- a/Source/Core/VideoCommon/VertexShaderManager.cpp +++ b/Source/Core/VideoCommon/VertexShaderManager.cpp @@ -247,16 +247,17 @@ void VertexShaderManager::SetConstants() // lights don't have a 1 to 1 mapping, the color component needs to be converted to 4 floats int istart = nLightsChanged[0] / 0x10; int iend = (nLightsChanged[1] + 15) / 0x10; - const float* xfmemptr = (const float*)&xfmem.lights[0x10 * istart]; for (int i = istart; i < iend; ++i) { - u32 color = *(const u32*)(xfmemptr + 3); - constants.light_colors[i][0] = (color >> 24) & 0xFF; - constants.light_colors[i][1] = (color >> 16) & 0xFF; - constants.light_colors[i][2] = (color >> 8) & 0xFF; - constants.light_colors[i][3] = (color) & 0xFF; - xfmemptr += 4; + const Light& light = xfmem.lights[i]; + + // xfmem.light.color is packed as abgr in u8[4], so we have to swap the order + constants.light_colors[i][0] = light.color[3]; + constants.light_colors[i][1] = light.color[2]; + constants.light_colors[i][2] = light.color[1]; + constants.light_colors[i][3] = light.color[0]; + const float* xfmemptr = light.cosatt; for (int j = 0; j < 4; ++j, xfmemptr += 3) { diff --git a/Source/Core/VideoCommon/XFMemory.h b/Source/Core/VideoCommon/XFMemory.h index 07ae526bb4..f36378b1e9 100644 --- a/Source/Core/VideoCommon/XFMemory.h +++ b/Source/Core/VideoCommon/XFMemory.h @@ -194,13 +194,9 @@ union DualTexInfo struct Light { u32 useless[3]; - u32 color; // rgba - float a0; // attenuation - float a1; - float a2; - float k0; // k stuff - float k1; - float k2; + u8 color[4]; + float cosatt[3]; // cos attenuation + float distatt[3]; // dist attenuation union { @@ -241,7 +237,7 @@ struct XFMemory u32 normalMatrices[96]; // 0x0400 - 0x045f u32 unk1[160]; // 0x0460 - 0x04ff u32 postMatrices[256]; // 0x0500 - 0x05ff - u32 lights[128]; // 0x0600 - 0x067f + Light lights[8]; // 0x0600 - 0x067f u32 unk2[2432]; // 0x0680 - 0x0fff u32 error; // 0x1000 u32 diag; // 0x1001