From 247f36102b75f5bb273ef9163b9c86a6dda52ec2 Mon Sep 17 00:00:00 2001 From: Phillip Stephens Date: Mon, 14 Apr 2025 18:36:21 -0700 Subject: [PATCH] Fix RGB5A3 alpha, we want to swap before assigning the color or we stomp on alpha --- Runtime/Graphics/CTexture.cpp | 41 ++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/Runtime/Graphics/CTexture.cpp b/Runtime/Graphics/CTexture.cpp index ee7dcd3bb..0e17a9515 100644 --- a/Runtime/Graphics/CTexture.cpp +++ b/Runtime/Graphics/CTexture.cpp @@ -258,22 +258,22 @@ void CTexture::MangleMipmap(u32 mip) { return; } - const uint colors[4] = { - 0x000000FF, - 0x0000FF00, - 0x00FF0000, - 0x0000FFFF, -}; + constexpr uint colors[4] = { + 0x000000FF, + 0x0000FF00, + 0x00FF0000, + 0x0000FFFF, + }; const uint color = colors[(mip - 1) & 3]; - ushort rgb565Color = ((color >> 3) & 0x001F) | // B - ((color >> 5) & 0x07E0) | // G - ((color >> 8) & 0xF800); // R - ushort rgb555Color = ((color >> 3) & 0x001F) | // B - ((color >> 6) & 0x03E0) | // G - ((color >> 9) & 0x7C00); // R - ushort rgb4Color = ((color >> 4) & 0x000F) | // B - ((color >> 8) & 0x00F0) | // G - ((color >> 12) & 0x0F00); // R + const ushort rgb565Color = ((color >> 3) & 0x001F) | // B + ((color >> 5) & 0x07E0) | // G + ((color >> 8) & 0xF800); // R + const ushort rgb555Color = ((color >> 3) & 0x001F) | // B + ((color >> 6) & 0x03E0) | // G + ((color >> 9) & 0x7C00); // R + const ushort rgb4Color = ((color >> 4) & 0x000F) | // B + ((color >> 8) & 0x00F0) | // G + ((color >> 12) & 0x0F00); // R int width = GetWidth(); int height = GetHeight(); @@ -287,7 +287,7 @@ void CTexture::MangleMipmap(u32 mip) { switch (GetTexelFormat()) { case ETexelFormat::RGB565: { - ushort* ptr = reinterpret_cast< ushort* >(x44_aramToken_x4_buff.get());//mARAMToken.GetMRAMSafe()); + const auto ptr = reinterpret_cast(x44_aramToken_x4_buff.get()); // mARAMToken.GetMRAMSafe()); for (int i = 0; i < width * height; ++i) { ptr[i + offset] = rgb565Color; CBasics::Swap2Bytes(reinterpret_cast(&ptr[i + offset])); @@ -295,7 +295,7 @@ void CTexture::MangleMipmap(u32 mip) { break; } case ETexelFormat::CMPR: { - ushort* ptr = reinterpret_cast< ushort* >(x44_aramToken_x4_buff.get()) + offset / 4; + auto ptr = reinterpret_cast(x44_aramToken_x4_buff.get()) + offset / 4; for (int i = 0; i < width * height / 16; ++i, ptr += 4) { ptr[0] = rgb565Color; CBasics::Swap2Bytes(reinterpret_cast(&ptr[0])); @@ -307,10 +307,11 @@ void CTexture::MangleMipmap(u32 mip) { break; } case ETexelFormat::RGB5A3: { - ushort* ptr = reinterpret_cast< ushort* >(x44_aramToken_x4_buff.get()); + const auto ptr = reinterpret_cast(x44_aramToken_x4_buff.get()); for (int i = 0; i < width * height; ++i) { ushort& val = ptr[i + offset]; - if (val & 0x8000) { + CBasics::Swap2Bytes(reinterpret_cast(&val)); + if ((val & 0x8000) != 0) { val = rgb555Color | 0x8000; } else { val = (val & 0xF000) | rgb4Color; @@ -346,7 +347,7 @@ u32 CTexture::TexelFormatBitsPerPixel(ETexelFormat fmt) { } } -bool CTexture::sMangleMips = false; +bool CTexture::sMangleMips = true; u32 CTexture::sCurrentFrameCount = 0; u32 CTexture::sTotalAllocatedMemory = 0;