Formalize render object id contract

This commit is contained in:
2026-04-10 01:57:15 +08:00
parent 4debbbea1f
commit b5ba985831
12 changed files with 282 additions and 102 deletions

View File

@@ -2,28 +2,48 @@
#include <XCEngine/Rendering/Picking/ObjectIdCodec.h>
#include <limits>
using namespace XCEngine::Rendering;
TEST(ObjectIdEncoding_Test, EncodesOnlyLow32BitsIntoPickingId) {
constexpr uint64_t objectId = 0x123456789ABCDEF0ull;
TEST(ObjectIdEncoding_Test, ConvertsValidRuntimeObjectIdLosslessly) {
constexpr uint64_t objectId = 0x00ABCDEFu;
RenderObjectId renderObjectId = kInvalidRenderObjectId;
EXPECT_FALSE(CanEncodeObjectIdWithoutLoss(objectId));
EXPECT_EQ(EncodeObjectIdToEncodedId(objectId), 0x9ABCDEF0u);
EXPECT_EQ(EncodeObjectIdToUInt32(objectId), 0x9ABCDEF0u);
EXPECT_TRUE(CanConvertRuntimeObjectIdToRenderObjectId(objectId));
EXPECT_TRUE(TryConvertRuntimeObjectIdToRenderObjectId(objectId, renderObjectId));
EXPECT_EQ(renderObjectId, 0x00ABCDEFu);
EXPECT_EQ(ConvertRenderObjectIdToRuntimeObjectId(renderObjectId), objectId);
}
TEST(ObjectIdEncoding_Test, DecodesColorBackToWidenedRuntimeId) {
TEST(ObjectIdEncoding_Test, RejectsInvalidAndOverflowRuntimeObjectIds) {
RenderObjectId renderObjectId = 123u;
EXPECT_FALSE(CanConvertRuntimeObjectIdToRenderObjectId(0u));
EXPECT_FALSE(TryConvertRuntimeObjectIdToRenderObjectId(0u, renderObjectId));
EXPECT_EQ(renderObjectId, kInvalidRenderObjectId);
const uint64_t overflowObjectId =
static_cast<uint64_t>(std::numeric_limits<RenderObjectId>::max()) + 1ull;
renderObjectId = 456u;
EXPECT_FALSE(CanConvertRuntimeObjectIdToRenderObjectId(overflowObjectId));
EXPECT_FALSE(TryConvertRuntimeObjectIdToRenderObjectId(overflowObjectId, renderObjectId));
EXPECT_EQ(renderObjectId, kInvalidRenderObjectId);
}
TEST(ObjectIdEncoding_Test, DecodesColorBackToRenderObjectId) {
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);
EXPECT_EQ(DecodeRenderObjectIdFromColor(r, g, b, a), 0x78563412u);
EXPECT_EQ(ConvertRenderObjectIdToRuntimeObjectId(DecodeRenderObjectIdFromColor(r, g, b, a)), 0x78563412ull);
}
TEST(ObjectIdEncoding_Test, EncodesColorChannelsFromLow32Bits) {
const auto encodedColor = EncodeObjectIdToColor(0x78563412ull);
TEST(ObjectIdEncoding_Test, EncodesColorChannelsFromRenderObjectId) {
const auto encodedColor = EncodeRenderObjectIdToColor(0x78563412u);
constexpr float kInv255 = 1.0f / 255.0f;
EXPECT_FLOAT_EQ(encodedColor.x, 0x12u * kInv255);