Skip to content

Commit 8d40684

Browse files
committed
Make the test pass, add some commented-out failing ones
1 parent d65dae7 commit 8d40684

File tree

4 files changed

+40
-17
lines changed

4 files changed

+40
-17
lines changed

GPU/Common/GPUStateUtils.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,13 +522,13 @@ float ToScaledDepthFromIntegerScale(u32 useFlags, float z) {
522522
}
523523

524524
const float depthSliceFactor = DepthSliceFactor(useFlags);
525-
const double doffset = 0.5 * (depthSliceFactor - 1.0) * (1.0 / depthSliceFactor);
526525
if (useFlags & GPU_SCALE_DEPTH_FROM_24BIT_TO_16BIT) {
526+
const double doffset = 0.5 * (depthSliceFactor - 1.0) / depthSliceFactor;
527527
// Use one bit for each value, rather than 1.0 / (65535.0 * 256.0).
528528
return (float)((double)z * (1.0 / 16777215.0) + doffset);
529529
} else {
530-
const float offset = 0.5f * (depthSliceFactor - 1.0f) * (1.0f / depthSliceFactor);
531-
return z * (1.0f / depthSliceFactor) * (1.0f / 65535.0f) + offset;
530+
const float offset = 0.5f * (depthSliceFactor - 1.0f) / depthSliceFactor;
531+
return z / depthSliceFactor * (1.0f / 65535.0f) + offset;
532532
}
533533
}
534534

GPU/Common/GPUStateUtils.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ void UpdateCachedViewportState(const ViewportAndScissor &vpAndScissor);
9393
class DepthScaleFactors {
9494
public:
9595
// This should only be used from GetDepthScaleFactors.
96-
DepthScaleFactors(float offset, float scale) : offset_(offset), scale_(scale) {}
96+
DepthScaleFactors(double offset, double scale) : offset_(offset), scale_(scale) {}
9797

9898
// Decodes a value from a depth buffer to a value of range 0..65536
9999
float DecodeToU16(float z) const {
@@ -106,13 +106,13 @@ class DepthScaleFactors {
106106
return (z_u16 / scale_) + offset_;
107107
}
108108

109-
float Offset() const { return offset_; }
110-
float ScaleU16() const { return scale_; }
109+
float Offset() const { return (float)offset_; }
110+
float ScaleU16() const { return (float)scale_; }
111111
// float Scale() const { return scale_ / 65535.0f; }
112112

113113
private:
114-
float offset_;
115-
float scale_;
114+
double offset_;
115+
double scale_;
116116
};
117117

118118
DepthScaleFactors GetDepthScaleFactors(u32 useFlags);

unittest/UnitTest.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include "ppsspp_config.h"
3131

32+
#include <algorithm>
3233
#include <cstdio>
3334
#include <cstdlib>
3435
#include <cmath>
@@ -801,33 +802,40 @@ static bool TestDepthMath() {
801802
// 0
802803
// GPU_USE_ACCURATE_DEPTH
803804
// GPU_USE_ACCURATE_DEPTH | GPU_SCALE_DEPTH_FROM_24BIT_TO_16BIT
804-
// What about GPU_USE_DEPTH_CLAMP? It basically overrides GPU_USE_ACCURATE_DEPTH?
805+
//
806+
// TODO: What about GPU_USE_DEPTH_CLAMP? It basically overrides GPU_USE_ACCURATE_DEPTH?
805807

806808
// These are in normalized space.
807-
static const float testValues[] = { 0.0f * 65535.0f, 0.1f * 65535.0f, 0.9f * 65535.0f, 1.0f * 65535.0f };
809+
static const volatile float testValues[] = { 0.0f, 0.1f, 0.5f, 0.9f, 1.0f };
808810

809811
static const u32 useFlagsArray[] = {
810812
0,
811813
GPU_USE_ACCURATE_DEPTH,
812814
GPU_USE_ACCURATE_DEPTH | GPU_SCALE_DEPTH_FROM_24BIT_TO_16BIT,
815+
// GPU_USE_DEPTH_CLAMP | GPU_USE_ACCURATE_DEPTH, fails
816+
// GPU_USE_DEPTH_CLAMP | GPU_USE_ACCURATE_DEPTH | GPU_SCALE_DEPTH_FROM_24BIT_TO_16BIT, fails
813817
};
814-
static const float expectedScale[] = { 65535.0f, 262140.0f, -1.0f };
815-
static const float expectedOffset[] = { 0.0f, 0.375f, -2.0f };
818+
static const float expectedScale[] = { 65535.0f, 262140.0f, 16776960.0f };
819+
static const float expectedOffset[] = { 0.0f, 0.375f, 0.498047f };
820+
821+
EXPECT_REL_EQ_FLOAT(100000.0f, 100001.0f, 0.00001f);
816822

817823
for (int j = 0; j < ARRAY_SIZE(useFlagsArray); j++) {
818824
u32 useFlags = useFlagsArray[j];
819825
printf("j: %d useflags: %d\n", j, useFlags);
820826
DepthScaleFactors factors = GetDepthScaleFactors(useFlags);
821827

822828
EXPECT_EQ_FLOAT(factors.ScaleU16(), expectedScale[j]);
823-
EXPECT_EQ_FLOAT(factors.Offset(), expectedOffset[j]);
829+
EXPECT_REL_EQ_FLOAT(factors.Offset(), expectedOffset[j], 0.00001f);
824830
EXPECT_EQ_FLOAT(factors.ScaleU16(), DepthSliceFactor(useFlags) * 65535.0f);
825831

826832
for (int i = 0; i < ARRAY_SIZE(testValues); i++) {
827-
float encoded = factors.EncodeFromU16(testValues[i]);
833+
float testValue = testValues[i] * 65535.0f;
834+
835+
float encoded = factors.EncodeFromU16(testValue);
828836
float decodedU16 = factors.DecodeToU16(encoded);
829-
EXPECT_EQ_FLOAT(decodedU16, testValues[i]);
830-
EXPECT_EQ_FLOAT(encoded, ToScaledDepthFromIntegerScale(useFlags, testValues[i]));
837+
EXPECT_REL_EQ_FLOAT(decodedU16, testValue, 0.0001f);
838+
EXPECT_REL_EQ_FLOAT(encoded, ToScaledDepthFromIntegerScale(useFlags, testValue), 0.00001f);
831839
}
832840
}
833841

unittest/UnitTest.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
#pragma once
22

3+
#include <cmath>
4+
#include <algorithm>
5+
6+
inline bool rel_equal(float a, float b, float precision) {
7+
float diff = fabsf(a - b);
8+
if (diff == 0.0f) {
9+
return true;
10+
}
11+
float range = std::max(fabsf(a), fabsf(b));
12+
float quot = diff / range;
13+
return quot < precision;
14+
}
15+
16+
317
#define EXPECT_TRUE(a) if (!(a)) { printf("%s:%i: Test Fail\n", __FUNCTION__, __LINE__); return false; }
418
#define EXPECT_FALSE(a) if ((a)) { printf("%s:%i: Test Fail\n", __FUNCTION__, __LINE__); return false; }
519
#define EXPECT_EQ_INT(a, b) if ((a) != (b)) { printf("%s:%i: Test Fail\n%d\nvs\n%d\n", __FUNCTION__, __LINE__, (int)(a), (int)(b)); return false; }
620
#define EXPECT_EQ_HEX(a, b) if ((a) != (b)) { printf("%s:%i: Test Fail\n%x\nvs\n%x\n", __FUNCTION__, __LINE__, a, b); return false; }
7-
#define EXPECT_EQ_FLOAT(a, b) if ((a) != (b)) { printf("%s:%i: Test Fail\n%f\nvs\n%f\n", __FUNCTION__, __LINE__, a, b); return false; }
21+
#define EXPECT_EQ_FLOAT(a, b) if ((a) != (b)) { printf("%s:%i: Test Fail\n%0.7f\nvs\n%0.7f\n", __FUNCTION__, __LINE__, a, b); return false; }
822
#define EXPECT_APPROX_EQ_FLOAT(a, b) if (fabsf((a)-(b))>0.00001f) { printf("%s:%i: Test Fail\n%f\nvs\n%f\n", __FUNCTION__, __LINE__, a, b); /*return false;*/ }
23+
#define EXPECT_REL_EQ_FLOAT(a, b, precision) if (!rel_equal(a, b, precision)) { printf("%s:%i: Test Fail\n%0.7f\nvs\n%0.7f\n", __FUNCTION__, __LINE__, a, b); /*return false;*/ }
924
#define EXPECT_EQ_STR(a, b) if (a != b) { printf("%s: Test Fail\n%s\nvs\n%s\n", __FUNCTION__, a.c_str(), b.c_str()); return false; }
1025

1126
#define RET(a) if (!(a)) { return false; }

0 commit comments

Comments
 (0)