From 4d35a3105eccc752c2a930be7daa4cb7655553b3 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 6 Aug 2017 18:14:51 -0400 Subject: [PATCH 1/2] TransformUnit: Make LightColor()'s chan parameter const Also marks references/pointers const where applicable --- Source/Core/VideoBackends/Software/TransformUnit.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Source/Core/VideoBackends/Software/TransformUnit.cpp b/Source/Core/VideoBackends/Software/TransformUnit.cpp index 8fff7ded27..aa8e265584 100644 --- a/Source/Core/VideoBackends/Software/TransformUnit.cpp +++ b/Source/Core/VideoBackends/Software/TransformUnit.cpp @@ -265,7 +265,7 @@ static float CalculateLightAttn(const LightPointer* light, Vec3* _ldir, const Ve return attn; } -static void LightColor(const Vec3& pos, const Vec3& normal, u8 lightNum, LitChannel& chan, +static void LightColor(const Vec3& pos, const Vec3& normal, u8 lightNum, const LitChannel& chan, Vec3& lightCol) { const LightPointer* light = (const LightPointer*)&xfmem.lights[lightNum]; @@ -326,7 +326,7 @@ void TransformColor(const InputVertexData* src, OutputVertexData* dst) u8 chancolor[4]; // color - LitChannel& colorchan = xfmem.color[chan]; + const LitChannel& colorchan = xfmem.color[chan]; if (colorchan.matsource) *(u32*)matcolor = *(u32*)src->color[chan]; // vertex else @@ -344,7 +344,7 @@ void TransformColor(const InputVertexData* src, OutputVertexData* dst) } else { - u8* ambColor = (u8*)&xfmem.ambColor[chan]; + const u8* ambColor = reinterpret_cast(&xfmem.ambColor[chan]); lightCol.x = ambColor[1]; lightCol.y = ambColor[2]; lightCol.z = ambColor[3]; @@ -370,7 +370,7 @@ void TransformColor(const InputVertexData* src, OutputVertexData* dst) } // alpha - LitChannel& alphachan = xfmem.alpha[chan]; + const LitChannel& alphachan = xfmem.alpha[chan]; if (alphachan.matsource) matcolor[0] = src->color[chan][0]; // vertex else From 745f92b4e586b544fb82a1d2084cd2d57227bb64 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 6 Aug 2017 19:01:08 -0400 Subject: [PATCH 2/2] TransformUnit: Get rid of pointer casting in TransformColor() where applicable The casts to u32* are technically undefined behavior. The u8* cast is left, as char/unsigned char is exempted from this rule to allow for bvtewise inspection of objects (and this is what s8/u8 are typedefs of on platforms we support). --- .../VideoBackends/Software/TransformUnit.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/Source/Core/VideoBackends/Software/TransformUnit.cpp b/Source/Core/VideoBackends/Software/TransformUnit.cpp index aa8e265584..0b1d28c27b 100644 --- a/Source/Core/VideoBackends/Software/TransformUnit.cpp +++ b/Source/Core/VideoBackends/Software/TransformUnit.cpp @@ -5,7 +5,9 @@ #include "VideoBackends/Software/TransformUnit.h" #include +#include #include +#include #include "Common/Assert.h" #include "Common/CommonTypes.h" @@ -322,15 +324,15 @@ void TransformColor(const InputVertexData* src, OutputVertexData* dst) for (u32 chan = 0; chan < xfmem.numChan.numColorChans; chan++) { // abgr - u8 matcolor[4]; - u8 chancolor[4]; + std::array matcolor; + std::array chancolor; // color const LitChannel& colorchan = xfmem.color[chan]; if (colorchan.matsource) - *(u32*)matcolor = *(u32*)src->color[chan]; // vertex + std::memcpy(matcolor.data(), src->color[chan], sizeof(u32)); // vertex else - *(u32*)matcolor = xfmem.matColor[chan]; + std::memcpy(matcolor.data(), &xfmem.matColor[chan], sizeof(u32)); if (colorchan.enablelighting) { @@ -366,7 +368,7 @@ void TransformColor(const InputVertexData* src, OutputVertexData* dst) } else { - *(u32*)chancolor = *(u32*)matcolor; + chancolor = matcolor; } // alpha @@ -382,7 +384,7 @@ void TransformColor(const InputVertexData* src, OutputVertexData* dst) if (alphachan.ambsource) lightCol = src->color[chan][0]; // vertex else - lightCol = (float)(xfmem.ambColor[chan] & 0xff); + lightCol = static_cast(xfmem.ambColor[chan] & 0xff); u8 mask = alphachan.GetFullLightMask(); for (int i = 0; i < 8; ++i) @@ -400,7 +402,8 @@ void TransformColor(const InputVertexData* src, OutputVertexData* dst) } // abgr -> rgba - *(u32*)dst->color[chan] = Common::swap32(*(u32*)chancolor); + const u32 rgba_color = Common::swap32(chancolor.data()); + std::memcpy(dst->color[chan], &rgba_color, sizeof(u32)); } }