Formalize object id encoding contract

This commit is contained in:
2026-04-05 19:10:31 +08:00
parent 081773b8c9
commit dabd4dd8b2
3 changed files with 68 additions and 13 deletions

View File

@@ -0,0 +1,33 @@
#include <gtest/gtest.h>
#include <XCEngine/Rendering/ObjectIdEncoding.h>
using namespace XCEngine::Rendering;
TEST(ObjectIdEncoding_Test, EncodesOnlyLow32BitsIntoPickingId) {
constexpr uint64_t objectId = 0x123456789ABCDEF0ull;
EXPECT_FALSE(CanEncodeObjectIdWithoutLoss(objectId));
EXPECT_EQ(EncodeObjectIdToEncodedId(objectId), 0x9ABCDEF0u);
EXPECT_EQ(EncodeObjectIdToUInt32(objectId), 0x9ABCDEF0u);
}
TEST(ObjectIdEncoding_Test, DecodesColorBackToWidenedRuntimeId) {
constexpr uint8_t r = 0x12u;
constexpr uint8_t g = 0x34u;
constexpr uint8_t b = 0x56u;
constexpr uint8_t a = 0x78u;
EXPECT_EQ(DecodeEncodedObjectIdFromColor(r, g, b, a), 0x78563412u);
EXPECT_EQ(DecodeObjectIdFromColor(r, g, b, a), 0x78563412ull);
}
TEST(ObjectIdEncoding_Test, EncodesColorChannelsFromLow32Bits) {
const auto encodedColor = EncodeObjectIdToColor(0x78563412ull);
constexpr float kInv255 = 1.0f / 255.0f;
EXPECT_FLOAT_EQ(encodedColor.x, 0x12u * kInv255);
EXPECT_FLOAT_EQ(encodedColor.y, 0x34u * kInv255);
EXPECT_FLOAT_EQ(encodedColor.z, 0x56u * kInv255);
EXPECT_FLOAT_EQ(encodedColor.w, 0x78u * kInv255);
}