diff --git a/Common/GPU/Vulkan/thin3d_vulkan.cpp b/Common/GPU/Vulkan/thin3d_vulkan.cpp index 82e9174a1c..4da47759a2 100644 --- a/Common/GPU/Vulkan/thin3d_vulkan.cpp +++ b/Common/GPU/Vulkan/thin3d_vulkan.cpp @@ -803,13 +803,15 @@ bool VKTexture::Create(VkCommandBuffer cmd, VulkanBarrierBatch *postBarriers, Vu usageBits |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT; } - VkComponentMapping r8AsAlpha[4] = { {VK_COMPONENT_SWIZZLE_ONE, VK_COMPONENT_SWIZZLE_ONE, VK_COMPONENT_SWIZZLE_ONE, VK_COMPONENT_SWIZZLE_R} }; - VkComponentMapping r8AsColor[4] = { {VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_ONE} }; + static const VkComponentMapping r8AsAlpha[4] = { {VK_COMPONENT_SWIZZLE_ONE, VK_COMPONENT_SWIZZLE_ONE, VK_COMPONENT_SWIZZLE_ONE, VK_COMPONENT_SWIZZLE_R} }; + static const VkComponentMapping r8AsColor[4] = { {VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_ONE} }; + static const VkComponentMapping r8AsPremulAlpha[4] = { {VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_R} }; VkComponentMapping *swizzle = nullptr; switch (desc.swizzle) { case TextureSwizzle::R8_AS_ALPHA: swizzle = r8AsAlpha; break; case TextureSwizzle::R8_AS_GRAYSCALE: swizzle = r8AsColor; break; + case TextureSwizzle::R8_AS_PREMUL_ALPHA: swizzle = r8AsPremulAlpha; break; case TextureSwizzle::DEFAULT: break; } diff --git a/Common/GPU/thin3d.h b/Common/GPU/thin3d.h index 33d6561e3a..3d0881421c 100644 --- a/Common/GPU/thin3d.h +++ b/Common/GPU/thin3d.h @@ -647,6 +647,7 @@ enum class TextureSwizzle { DEFAULT, R8_AS_ALPHA, R8_AS_GRAYSCALE, + R8_AS_PREMUL_ALPHA, }; struct TextureDesc { diff --git a/Common/Render/Text/draw_text.cpp b/Common/Render/Text/draw_text.cpp index 565f54bd0d..55657e04b9 100644 --- a/Common/Render/Text/draw_text.cpp +++ b/Common/Render/Text/draw_text.cpp @@ -98,7 +98,7 @@ void TextDrawer::DrawString(DrawBuffer &target, std::string_view str, float x, f desc.depth = 1; desc.mipLevels = 1; desc.tag = "TextDrawer"; - desc.swizzle = texFormat == Draw::DataFormat::R8_UNORM ? Draw::TextureSwizzle::R8_AS_ALPHA : Draw::TextureSwizzle::DEFAULT, + desc.swizzle = texFormat == Draw::DataFormat::R8_UNORM ? Draw::TextureSwizzle::R8_AS_PREMUL_ALPHA : Draw::TextureSwizzle::DEFAULT, entry->texture = draw_->CreateTexture(desc); cache_[key] = std::unique_ptr(entry); } diff --git a/Common/Render/Text/draw_text.h b/Common/Render/Text/draw_text.h index 6ebe6ef98b..c1b2d46e76 100644 --- a/Common/Render/Text/draw_text.h +++ b/Common/Render/Text/draw_text.h @@ -85,6 +85,14 @@ protected: void WrapString(std::string &out, std::string_view str, float maxWidth, int flags); + // v has the 8-bit alpha. + static u16 AlphaToPremul4444(u32 v) { + v = (v >> 4) & 0x0F; + v |= v << 4; + v |= v << 8; + return v; + } + struct CacheKey { bool operator < (const CacheKey &other) const { if (fontHash < other.fontHash) diff --git a/Common/Render/Text/draw_text_android.cpp b/Common/Render/Text/draw_text_android.cpp index 29b33a16b9..a655cc41ac 100644 --- a/Common/Render/Text/draw_text_android.cpp +++ b/Common/Render/Text/draw_text_android.cpp @@ -144,11 +144,12 @@ bool TextDrawerAndroid::DrawStringBitmap(std::vector &bitmapData, TextS for (int y = 0; y < entry.bmHeight; y++) { for (int x = 0; x < entry.bmWidth; x++) { uint32_t v = jimage[imageWidth * y + x]; - v = 0xFFF0 | ((v >> 28) & 0xF); // Grab the upper bits from the alpha channel, and put directly in the 16-bit alpha channel. - bitmapData16[entry.bmWidth * y + x] = (uint16_t)v; + // Premultiplied alpha: Put alpha in all four channels. + bitmapData16[entry.bmWidth * y + x] = AlphaToPremul4444(v >> 24); } } } else if (texFormat == Draw::DataFormat::R8_UNORM) { + // We only get here if swizzle is possible, so we can duplicate the value to all four channels. bitmapData.resize(entry.bmWidth * entry.bmHeight); for (int y = 0; y < entry.bmHeight; y++) { for (int x = 0; x < entry.bmWidth; x++) { diff --git a/Common/Render/Text/draw_text_cocoa.mm b/Common/Render/Text/draw_text_cocoa.mm index 5a8e2549e0..10c8d33ef1 100644 --- a/Common/Render/Text/draw_text_cocoa.mm +++ b/Common/Render/Text/draw_text_cocoa.mm @@ -285,8 +285,8 @@ bool TextDrawerCocoa::DrawStringBitmap(std::vector &bitmapData, TextStr uint16_t *bitmapData16 = (uint16_t *)&bitmapData[0]; for (int y = 0; y < entry.bmHeight; y++) { for (int x = 0; x < entry.bmWidth; x++) { - uint8_t bAlpha = (uint8_t)((bitmap[bmWidth * y + x] & 0xff) >> 4); - bitmapData16[entry.bmWidth * y + x] = (bAlpha) | 0xfff0; + uint8_t bAlpha = (uint8_t)(bitmap[bmWidth * y + x] & 0xff); + bitmapData16[entry.bmWidth * y + x] = AlphaToPremul4444(bAlpha); } } } else if (texFormat == Draw::DataFormat::A4R4G4B4_UNORM_PACK16) { @@ -295,8 +295,8 @@ bool TextDrawerCocoa::DrawStringBitmap(std::vector &bitmapData, TextStr uint16_t *bitmapData16 = (uint16_t *)&bitmapData[0]; for (int y = 0; y < entry.bmHeight; y++) { for (int x = 0; x < entry.bmWidth; x++) { - uint8_t bAlpha = (uint8_t)((bitmap[bmWidth * y + x] & 0xff) >> 4); - bitmapData16[entry.bmWidth * y + x] = (bAlpha << 12) | 0x0fff; + uint8_t bAlpha = (uint8_t)(bitmap[bmWidth * y + x] & 0xff); + bitmapData16[entry.bmWidth * y + x] = AlphaToPremul4444(bAlpha); } } } else if (texFormat == Draw::DataFormat::R8_UNORM) { diff --git a/Common/Render/Text/draw_text_qt.cpp b/Common/Render/Text/draw_text_qt.cpp index 21a6e831c2..14ef61a4ac 100644 --- a/Common/Render/Text/draw_text_qt.cpp +++ b/Common/Render/Text/draw_text_qt.cpp @@ -96,7 +96,7 @@ bool TextDrawerQt::DrawStringBitmap(std::vector &bitmapData, TextString uint16_t *bitmapData16 = (uint16_t *)&bitmapData[0]; for (int x = 0; x < entry.bmWidth; x++) { for (int y = 0; y < entry.bmHeight; y++) { - bitmapData16[entry.bmWidth * y + x] = 0xfff0 | (image.pixel(x, y) >> 28); + bitmapData16[entry.bmWidth * y + x] = AlphaToPremul4444(image.pixel(x, y) >> 24); } } } else if (texFormat == Draw::DataFormat::R8_UNORM) { diff --git a/Common/Render/Text/draw_text_sdl.cpp b/Common/Render/Text/draw_text_sdl.cpp index f3612ad965..997e0cee1f 100644 --- a/Common/Render/Text/draw_text_sdl.cpp +++ b/Common/Render/Text/draw_text_sdl.cpp @@ -371,8 +371,9 @@ bool TextDrawerSDL::DrawStringBitmap(std::vector &bitmapData, TextStrin for (int x = 0; x < entry.bmWidth; x++) { for (int y = 0; y < entry.bmHeight; y++) { - uint64_t index = entry.bmWidth * y + x; - bitmapData16[index] = 0xfff0 | (imageData[pitch * y + x] >> 28); + const uint64_t index = entry.bmWidth * y + x; + const u16 alpha = AlphaToPremul4444(imageData[pitch * y + x]); + bitmapData16[index] = alpha; } } } else if (texFormat == Draw::DataFormat::R8_UNORM) { diff --git a/Common/Render/Text/draw_text_uwp.cpp b/Common/Render/Text/draw_text_uwp.cpp index 457f290aca..314be60b84 100644 --- a/Common/Render/Text/draw_text_uwp.cpp +++ b/Common/Render/Text/draw_text_uwp.cpp @@ -338,8 +338,8 @@ bool TextDrawerUWP::DrawStringBitmap(std::vector &bitmapData, TextStrin uint16_t *bitmapData16 = (uint16_t *)&bitmapData[0]; for (int y = 0; y < entry.bmHeight; y++) { for (int x = 0; x < entry.bmWidth; x++) { - uint8_t bAlpha = (uint8_t)((map.bits[map.pitch * y + x * 4] & 0xff) >> 4); - bitmapData16[entry.bmWidth * y + x] = (bAlpha) | 0xfff0; + uint8_t bAlpha = (uint8_t)(map.bits[map.pitch * y + x * 4] & 0xff); + bitmapData16[entry.bmWidth * y + x] = AlphaToPremul4444(bAlpha); } } } else if (texFormat == Draw::DataFormat::A4R4G4B4_UNORM_PACK16) { @@ -348,8 +348,8 @@ bool TextDrawerUWP::DrawStringBitmap(std::vector &bitmapData, TextStrin uint16_t *bitmapData16 = (uint16_t *)&bitmapData[0]; for (int y = 0; y < entry.bmHeight; y++) { for (int x = 0; x < entry.bmWidth; x++) { - uint8_t bAlpha = (uint8_t)((map.bits[map.pitch * y + x * 4] & 0xff) >> 4); - bitmapData16[entry.bmWidth * y + x] = (bAlpha << 12) | 0x0fff; + uint8_t bAlpha = (uint8_t)(map.bits[map.pitch * y + x * 4] & 0xff); + bitmapData16[entry.bmWidth * y + x] = AlphaToPremul4444(bAlpha); } } } else if (texFormat == Draw::DataFormat::R8_UNORM) { diff --git a/Common/Render/Text/draw_text_win.cpp b/Common/Render/Text/draw_text_win.cpp index a4db97a3e4..a111deb256 100644 --- a/Common/Render/Text/draw_text_win.cpp +++ b/Common/Render/Text/draw_text_win.cpp @@ -220,8 +220,8 @@ bool TextDrawerWin32::DrawStringBitmap(std::vector &bitmapData, TextStr uint16_t *bitmapData16 = (uint16_t *)&bitmapData[0]; for (int y = 0; y < entry.bmHeight; y++) { for (int x = 0; x < entry.bmWidth; x++) { - uint8_t bAlpha = (uint8_t)((ctx_->pBitmapBits[MAX_TEXT_WIDTH * y + x] & 0xff) >> 4); - bitmapData16[entry.bmWidth * y + x] = (bAlpha) | 0xfff0; + uint8_t bAlpha = (uint8_t)(ctx_->pBitmapBits[MAX_TEXT_WIDTH * y + x] & 0xff); + bitmapData16[entry.bmWidth * y + x] = AlphaToPremul4444(bAlpha); } } } else if (texFormat == Draw::DataFormat::A4R4G4B4_UNORM_PACK16) { @@ -229,8 +229,8 @@ bool TextDrawerWin32::DrawStringBitmap(std::vector &bitmapData, TextStr uint16_t *bitmapData16 = (uint16_t *)&bitmapData[0]; for (int y = 0; y < entry.bmHeight; y++) { for (int x = 0; x < entry.bmWidth; x++) { - uint8_t bAlpha = (uint8_t)((ctx_->pBitmapBits[MAX_TEXT_WIDTH * y + x] & 0xff) >> 4); - bitmapData16[entry.bmWidth * y + x] = (bAlpha << 12) | 0x0fff; + uint8_t bAlpha = (uint8_t)(ctx_->pBitmapBits[MAX_TEXT_WIDTH * y + x] & 0xff); + bitmapData16[entry.bmWidth * y + x] = AlphaToPremul4444(bAlpha); } } } else if (texFormat == Draw::DataFormat::R8_UNORM) { diff --git a/Core/Util/PPGeDraw.cpp b/Core/Util/PPGeDraw.cpp index 0b3141ed1f..8e69aaaa63 100644 --- a/Core/Util/PPGeDraw.cpp +++ b/Core/Util/PPGeDraw.cpp @@ -1329,7 +1329,7 @@ void PPGeSetTexture(u32 dataAddr, int width, int height) WriteCmd(GE_CMD_TEXSIZE0, wp2 | (hp2 << 8)); WriteCmd(GE_CMD_TEXMAPMODE, 0 | (1 << 8)); WriteCmd(GE_CMD_TEXMODE, 0); - WriteCmd(GE_CMD_TEXFORMAT, GE_TFMT_8888); // 4444 + WriteCmd(GE_CMD_TEXFORMAT, GE_TFMT_8888); WriteCmd(GE_CMD_TEXFILTER, (1 << 8) | 1); // mag = LINEAR min = LINEAR WriteCmd(GE_CMD_TEXWRAP, (1 << 8) | 1); // clamp texture wrapping WriteCmd(GE_CMD_TEXFUNC, (0 << 16) | (1 << 8) | 0); // RGBA texture reads, modulate, no color doubling diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index 475c5ef3e1..8238ff4ee9 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -919,7 +919,7 @@ bool CreateGlobalPipelines() { } InputLayout *inputLayout = ui_draw2d.CreateInputLayout(g_draw); - BlendState *blendNormal = g_draw->CreateBlendState({ true, 0xF, BlendFactor::SRC_ALPHA, BlendFactor::ONE_MINUS_SRC_ALPHA }); + BlendState *blendNormal = g_draw->CreateBlendState({ true, 0xF, BlendFactor::ONE, BlendFactor::ONE_MINUS_SRC_ALPHA }); DepthStencilState *depth = g_draw->CreateDepthStencilState({ false, false, Comparison::LESS }); RasterState *rasterNoCull = g_draw->CreateRasterState({});