From 58aeeaf1559ec9f0a2d16509a120ca98a9db6c8a Mon Sep 17 00:00:00 2001 From: Sandy Date: Mon, 22 Aug 2022 20:36:50 -0400 Subject: [PATCH] texture format info: Invert R and B in RGB5A1 (#2876) d3d9: Revert 76db2ed38d273bb6a06cb314651b4857b13c525a to force software conversion and use the format that does exist d3d11: Force software conversion because the format doesn't exist gl: Revert b36aa71403033de6213be2a102649816a1a9b10d to swizzle R and B mtl: Swizzle R and B --- src/renderer_d3d11.cpp | 36 ++++++++++++++++++++++++++++++++---- src/renderer_d3d9.cpp | 8 ++++++++ src/renderer_gl.cpp | 2 +- src/renderer_mtl.mm | 2 +- 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/renderer_d3d11.cpp b/src/renderer_d3d11.cpp index 794c369c2..7a5fd0e27 100644 --- a/src/renderer_d3d11.cpp +++ b/src/renderer_d3d11.cpp @@ -7,6 +7,7 @@ #if BGFX_CONFIG_RENDERER_DIRECT3D11 # include "renderer_d3d11.h" +# include namespace bgfx { namespace d3d11 { @@ -4468,6 +4469,7 @@ namespace bgfx { namespace d3d11 , swizzle ? " (swizzle BGRA8 -> RGBA8)" : "" ); + uint8_t* temp = NULL; for (uint16_t side = 0; side < numSides; ++side) { for (uint8_t lod = 0, num = ti.numMips; lod < num; ++lod) @@ -4480,7 +4482,7 @@ namespace bgfx { namespace d3d11 if (convert) { uint32_t srcpitch = mip.m_width*bpp/8; - uint8_t* temp = (uint8_t*)BX_ALLOC(g_allocator, srcpitch*mip.m_height); + temp = (uint8_t*)BX_ALLOC(g_allocator, srcpitch*mip.m_height); bimg::imageDecodeToBgra8(g_allocator, temp, mip.m_data, mip.m_width, mip.m_height, srcpitch, mip.m_format); srd[kk].pSysMem = temp; @@ -4494,6 +4496,15 @@ namespace bgfx { namespace d3d11 else { srd[kk].SysMemPitch = mip.m_width*mip.m_bpp/8; + + switch (m_textureFormat) + { + 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; + } } srd[kk].SysMemSlicePitch = mip.m_height*srd[kk].SysMemPitch; @@ -4718,8 +4729,7 @@ namespace bgfx { namespace d3d11 DX_CHECK(s_renderD3D11->m_device->CreateUnorderedAccessView(m_ptr, NULL, &m_uav) ); } - if (convert - && 0 != kk) + if (temp != NULL) { kk = 0; for (uint16_t side = 0; side < numSides; ++side) @@ -4794,9 +4804,11 @@ namespace bgfx { namespace d3d11 box.back = 1; } + const bimg::ImageBlockInfo & blockInfo = bimg::getBlockInfo(bimg::TextureFormat::Enum(m_textureFormat) ); + const uint16_t blockHeight = blockInfo.blockHeight; + const uint16_t bpp = blockInfo.bitsPerPixel; const uint32_t subres = _mip + ( (layer + _side) * m_numMips); const bool depth = bimg::isDepth(bimg::TextureFormat::Enum(m_textureFormat) ); - const uint32_t bpp = bimg::getBitsPerPixel(bimg::TextureFormat::Enum(m_textureFormat) ); uint32_t rectpitch = _rect.m_width*bpp/8; if (bimg::isCompressed(bimg::TextureFormat::Enum(m_textureFormat))) { @@ -4822,6 +4834,22 @@ namespace bgfx { namespace d3d11 box.bottom = bx::max(1u, m_height >> _mip); } + { + uint8_t* src = data; + for (uint32_t yy = 0, height = _rect.m_height; yy < height; yy += blockHeight) + { + switch (m_textureFormat) + { + 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; + } + } + deviceCtx->UpdateSubresource( m_ptr , subres diff --git a/src/renderer_d3d9.cpp b/src/renderer_d3d9.cpp index 742014a7c..3a5cccfa2 100644 --- a/src/renderer_d3d9.cpp +++ b/src/renderer_d3d9.cpp @@ -3037,6 +3037,10 @@ 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); + break; + case TextureFormat::RGBA4: bimg::imageConvert(bits, 16, bx::packBgra4, mip.m_data, bx::unpackRgba4, size); break; @@ -3098,6 +3102,10 @@ namespace bgfx { namespace d3d9 { switch (m_textureFormat) { + case TextureFormat::RGB5A1: + bimg::imageConvert(dst, 16, bx::packBgr5a1, src, bx::unpackRgb5a1, rectpitch); + break; + case TextureFormat::RGBA4: bimg::imageConvert(dst, 16, bx::packBgra4, src, bx::unpackRgba4, rectpitch); break; diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index dc0cf4858..15b91d00b 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -289,7 +289,7 @@ namespace bgfx { namespace gl { 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_RGBA4, GL_ZERO, GL_BGRA, GL_BGRA, GL_UNSIGNED_SHORT_4_4_4_4_REV, false, { $_, $_, $_, $_ } }, // RGBA4 - { GL_RGB5_A1, GL_ZERO, GL_BGRA, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, false, { $_, $_, $_, $_ } }, // RGB5A1 + { 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 { GL_R11F_G11F_B10F, GL_ZERO, GL_RGB, GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV, false, { $_, $_, $_, $_ } }, // RG11B10F { GL_ZERO, GL_ZERO, GL_ZERO, GL_ZERO, GL_ZERO, false, { $_, $_, $_, $_ } }, // UnknownDepth diff --git a/src/renderer_mtl.mm b/src/renderer_mtl.mm index c7898d18a..cb402b517 100644 --- a/src/renderer_mtl.mm +++ b/src/renderer_mtl.mm @@ -306,7 +306,7 @@ namespace bgfx { namespace mtl { MTLPixelFormatRGBA32Float, MTLPixelFormatInvalid, MTLReadWriteTextureTier2, { $R, $G, $B, $A }, true }, // RGBA32F { MTLPixelFormat(40/*B5G6R5Unorm*/), MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $R, $G, $B, $A }, true }, // R5G6B5 { MTLPixelFormat(42/*ABGR4Unorm*/), MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $R, $G, $B, $A }, true }, // RGBA4 - { MTLPixelFormat(41/*A1BGR5Unorm*/), MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $R, $G, $B, $A }, true }, // RGB5A1 + { MTLPixelFormatA1BGR5Unorm, MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $B, $G, $R, $A }, true }, // RGB5A1 { MTLPixelFormatRGB10A2Unorm, MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $R, $G, $B, $A }, true }, // RGB10A2 { MTLPixelFormatRG11B10Float, MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $R, $G, $B, $A }, true }, // RG11B10F { MTLPixelFormatInvalid, MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $R, $G, $B, $A }, false }, // UnknownDepth