From 536c4cdbf5589ac9ee51b393e2f602fa7005c719 Mon Sep 17 00:00:00 2001 From: Sandy Date: Tue, 23 Aug 2022 09:46:56 -0400 Subject: [PATCH] Texture format info: Invert R and B in R5G6B5 (#2886) d3d9: Do software conversion to take advantage of B5G6R5. d3d11: Do software conversion to take advantage of B5G6R5. gl: Set internal representation to swap R and B. metal: Swizzle R and B and remove software conversion. --- src/renderer_d3d11.cpp | 18 ++++++++++++++---- src/renderer_d3d9.cpp | 16 ++++++++++++---- src/renderer_gl.cpp | 2 +- src/renderer_mtl.mm | 5 ++--- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/renderer_d3d11.cpp b/src/renderer_d3d11.cpp index 9d6f56475..02782d53f 100644 --- a/src/renderer_d3d11.cpp +++ b/src/renderer_d3d11.cpp @@ -4499,9 +4499,9 @@ namespace bgfx { namespace d3d11 switch (m_textureFormat) { - case TextureFormat::RGB5A1: + case TextureFormat::R5G6B5: temp = (uint8_t*)BX_ALLOC(g_allocator, srd[kk].SysMemPitch*mip.m_height); - bimg::imageConvert(temp, 16, bx::packBgr5a1, mip.m_data, bx::unpackRgb5a1, srd[kk].SysMemPitch*mip.m_height); + bimg::imageConvert(temp, 16, bx::packB5G6R5, mip.m_data, bx::unpackR5G6B5, srd[kk].SysMemPitch*mip.m_height); srd[kk].pSysMem = temp; break; case TextureFormat::RGBA4: @@ -4509,6 +4509,11 @@ namespace bgfx { namespace d3d11 bimg::imageConvert(temp, 16, bx::packBgra4, mip.m_data, bx::unpackRgba4, srd[kk].SysMemPitch*mip.m_height); srd[kk].pSysMem = temp; break; + case TextureFormat::RGB5A1: + temp = (uint8_t*)BX_ALLOC(g_allocator, srd[kk].SysMemPitch*mip.m_height); + bimg::imageConvert(temp, 16, bx::packBgr5a1, mip.m_data, bx::unpackRgb5a1, srd[kk].SysMemPitch*mip.m_height); + srd[kk].pSysMem = temp; + break; } } @@ -4844,9 +4849,9 @@ namespace bgfx { namespace d3d11 { switch (m_textureFormat) { - case TextureFormat::RGB5A1: + case TextureFormat::R5G6B5: temp = (uint8_t*)BX_ALLOC(g_allocator, rectpitch); - bimg::imageConvert(temp, 16, bx::packBgr5a1, src, bx::unpackRgb5a1, rectpitch); + bimg::imageConvert(temp, 16, bx::packB5G6R5, src, bx::unpackR5G6B5, rectpitch); data = temp; break; case TextureFormat::RGBA4: @@ -4854,6 +4859,11 @@ namespace bgfx { namespace d3d11 bimg::imageConvert(temp, 16, bx::packBgra4, src, bx::unpackRgba4, rectpitch); data = temp; break; + case TextureFormat::RGB5A1: + temp = (uint8_t*)BX_ALLOC(g_allocator, rectpitch); + bimg::imageConvert(temp, 16, bx::packBgr5a1, src, bx::unpackRgb5a1, rectpitch); + data = temp; + break; } src += srcpitch; } diff --git a/src/renderer_d3d9.cpp b/src/renderer_d3d9.cpp index 3a5cccfa2..7980b2b4e 100644 --- a/src/renderer_d3d9.cpp +++ b/src/renderer_d3d9.cpp @@ -3037,14 +3037,18 @@ namespace bgfx { namespace d3d9 uint32_t size = useMipSize ? mip.m_size : mipSize; switch (m_textureFormat) { - case TextureFormat::RGB5A1: - bimg::imageConvert(bits, 16, bx::packBgr5a1, mip.m_data, bx::unpackRgb5a1, size); + case TextureFormat::R5G6B5: + bimg::imageConvert(bits, 16, bx::packB5G6R5, mip.m_data, bx::unpackR5G6B5, size); break; case TextureFormat::RGBA4: bimg::imageConvert(bits, 16, bx::packBgra4, mip.m_data, bx::unpackRgba4, size); break; + case TextureFormat::RGB5A1: + bimg::imageConvert(bits, 16, bx::packBgr5a1, mip.m_data, bx::unpackRgb5a1, size); + break; + default: bx::memCopy(bits, mip.m_data, size); break; @@ -3102,14 +3106,18 @@ namespace bgfx { namespace d3d9 { switch (m_textureFormat) { - case TextureFormat::RGB5A1: - bimg::imageConvert(dst, 16, bx::packBgr5a1, src, bx::unpackRgb5a1, rectpitch); + case TextureFormat::R5G6B5: + bimg::imageConvert(dst, 16, bx::packB5G6R5, src, bx::unpackR5G6B5, rectpitch); break; case TextureFormat::RGBA4: bimg::imageConvert(dst, 16, bx::packBgra4, src, bx::unpackRgba4, rectpitch); break; + case TextureFormat::RGB5A1: + bimg::imageConvert(dst, 16, bx::packBgr5a1, src, bx::unpackRgb5a1, rectpitch); + break; + default: bx::memCopy(dst, src, rectpitch); break; diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index b999ef0e4..64960fe8e 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -287,7 +287,7 @@ namespace bgfx { namespace gl { GL_RGBA32I, GL_ZERO, RGBA_INTEGER, GL_RGBA_INTEGER, GL_INT, false, { $_, $_, $_, $_ } }, // RGBA32I { GL_RGBA32UI, GL_ZERO, RGBA_INTEGER, GL_RGBA_INTEGER, GL_UNSIGNED_INT, false, { $_, $_, $_, $_ } }, // RGBA32U { GL_RGBA32F, GL_ZERO, GL_RGBA, GL_RGBA, GL_FLOAT, false, { $_, $_, $_, $_ } }, // RGBA32F - { GL_RGB565, GL_ZERO, GL_RGB, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, false, { $_, $_, $_, $_ } }, // R5G6B5 + { GL_RGB565, GL_ZERO, GL_BGR, GL_BGR, GL_UNSIGNED_SHORT_5_6_5, false, { $_, $_, $_, $_ } }, // R5G6B5 { GL_RGBA4, GL_ZERO, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4_REV, false, { $_, $_, $_, $_ } }, // RGBA4 { GL_RGB5_A1, GL_ZERO, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, false, { $_, $_, $_, $_ } }, // RGB5A1 { GL_RGB10_A2, GL_ZERO, GL_RGBA, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, false, { $_, $_, $_, $_ } }, // RGB10A2 diff --git a/src/renderer_mtl.mm b/src/renderer_mtl.mm index bd5252b59..e6981a9c5 100644 --- a/src/renderer_mtl.mm +++ b/src/renderer_mtl.mm @@ -304,7 +304,7 @@ namespace bgfx { namespace mtl { MTLPixelFormatRGBA32Sint, MTLPixelFormatInvalid, MTLReadWriteTextureTier2, { $R, $G, $B, $A }, true }, // RGBA32I { MTLPixelFormatRGBA32Uint, MTLPixelFormatInvalid, MTLReadWriteTextureTier2, { $R, $G, $B, $A }, true }, // RGBA32U { MTLPixelFormatRGBA32Float, MTLPixelFormatInvalid, MTLReadWriteTextureTier2, { $R, $G, $B, $A }, true }, // RGBA32F - { MTLPixelFormat(40/*B5G6R5Unorm*/), MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $R, $G, $B, $A }, true }, // R5G6B5 + { MTLPixelFormatB5G6R5Unorm, MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $B, $G, $R, $A }, true }, // R5G6B5 { MTLPixelFormatABGR4Unorm, MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $A, $B, $G, $R }, true }, // RGBA4 { MTLPixelFormatBGR5A1Unorm, MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $B, $G, $R, $A }, true }, // RGB5A1 { MTLPixelFormatRGB10A2Unorm, MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $R, $G, $B, $A }, true }, // RGB10A2 @@ -763,8 +763,7 @@ BX_STATIC_ASSERT(BX_COUNTOF(s_accessNames) == Access::Count, "Invalid s_accessNa g_caps.formats[TextureFormat::PTC12 ] = g_caps.formats[TextureFormat::PTC14 ] = g_caps.formats[TextureFormat::PTC12A] = - g_caps.formats[TextureFormat::PTC14A] = - g_caps.formats[TextureFormat::R5G6B5] = BGFX_CAPS_FORMAT_TEXTURE_NONE; + g_caps.formats[TextureFormat::PTC14A] = BGFX_CAPS_FORMAT_TEXTURE_NONE; g_caps.formats[TextureFormat::RGB9E5F] &= ~(BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER | BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER_MSAA); }