summaryrefslogtreecommitdiff
path: root/Source/Core/VideoBackends/Software/TransformUnit.cpp
diff options
context:
space:
mode:
authormagumagu <magumagu9@gmail.com>2014-05-11 12:53:02 -0700
committermagumagu <magumagu9@gmail.com>2014-05-11 12:53:02 -0700
commit9e4eeb3b9b012265d0f9865aeaa3fecbf200d7fc (patch)
tree1f971acc4ee1c8df17d9e54b36d32ce9996582f6 /Source/Core/VideoBackends/Software/TransformUnit.cpp
parent36720e6822a8049c34ff1bfe4ea24e47f85847ca (diff)
Video backends: fix rounding in lighting computation.
For whatever reason, the hardware doesn't do a full divide by 255, but instead uses an approximation with shifting, similar to the way it is done in TEV.
Diffstat (limited to 'Source/Core/VideoBackends/Software/TransformUnit.cpp')
-rw-r--r--Source/Core/VideoBackends/Software/TransformUnit.cpp24
1 files changed, 14 insertions, 10 deletions
diff --git a/Source/Core/VideoBackends/Software/TransformUnit.cpp b/Source/Core/VideoBackends/Software/TransformUnit.cpp
index f0722ce797..fb8af08e11 100644
--- a/Source/Core/VideoBackends/Software/TransformUnit.cpp
+++ b/Source/Core/VideoBackends/Software/TransformUnit.cpp
@@ -5,6 +5,8 @@
#include <cmath>
#include "Common/Common.h"
+#include "Common/MathUtil.h"
+
#include "VideoBackends/Software/BPMemLoader.h"
#include "VideoBackends/Software/CPMemLoader.h"
#include "VideoBackends/Software/NativeVertexFormat.h"
@@ -200,11 +202,6 @@ inline void AddScaledIntegerColor(const u8 *src, float scale, Vec3 &dst)
dst.z += src[3] * scale;
}
-inline float Clamp(float val, float a, float b)
-{
- return val<a?a:val>b?b:val;
-}
-
inline float SafeDivide(float n, float d)
{
return (d==0) ? (n>0?1:0) : n/d;
@@ -414,10 +411,15 @@ void TransformColor(const InputVertexData *src, OutputVertexData *dst)
LightColor(dst->mvPosition, dst->normal[0], i, colorchan, lightCol);
}
- float inv = 1.0f / 255.0f;
- chancolor[1] = (u8)(matcolor[1] * Clamp(lightCol.x * inv, 0.0f, 1.0f));
- chancolor[2] = (u8)(matcolor[2] * Clamp(lightCol.y * inv, 0.0f, 1.0f));
- chancolor[3] = (u8)(matcolor[3] * Clamp(lightCol.z * inv, 0.0f, 1.0f));
+ int light_x = int(lightCol.x);
+ int light_y = int(lightCol.y);
+ int light_z = int(lightCol.z);
+ MathUtil::Clamp(&light_x, 0, 255);
+ MathUtil::Clamp(&light_y, 0, 255);
+ MathUtil::Clamp(&light_z, 0, 255);
+ chancolor[1] = (matcolor[1] * (light_x + (light_x >> 7))) >> 8;
+ chancolor[2] = (matcolor[2] * (light_y + (light_y >> 7))) >> 8;
+ chancolor[3] = (matcolor[3] * (light_z + (light_z >> 7))) >> 8;
}
else
{
@@ -446,7 +448,9 @@ void TransformColor(const InputVertexData *src, OutputVertexData *dst)
LightAlpha(dst->mvPosition, dst->normal[0], i, alphachan, lightCol);
}
- chancolor[0] = (u8)(matcolor[0] * Clamp(lightCol / 255.0f, 0.0f, 1.0f));
+ int light_a = int(lightCol);
+ MathUtil::Clamp(&light_a, 0, 255);
+ chancolor[0] = (matcolor[0] * (light_a + (light_a >> 7))) >> 8;
}
else
{