From 79f5f23b7905ee52ce75edb421cc83bbd77d0f56 Mon Sep 17 00:00:00 2001 From: bkaradzic Date: Sun, 6 Jan 2013 12:39:18 -0800 Subject: [PATCH] Cleanup of texture swizzle and BGRA format handling. --- src/bgfx.cpp | 36 +++++++++++++++++++++++++++++++++++ src/renderer_gl.cpp | 46 +++++++++++++++++++++++++++++++-------------- 2 files changed, 68 insertions(+), 14 deletions(-) diff --git a/src/bgfx.cpp b/src/bgfx.cpp index 0ddac848..f6dbe081 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -960,6 +960,18 @@ namespace bgfx TextureHandle createTexture2D(uint16_t _width, uint16_t _height, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags, const Memory* _mem) { BGFX_CHECK_MAIN_THREAD(); + + if (NULL != _mem) + { + TextureInfo ti; + calcTextureSize(ti, _width, _height, 1, _numMips, _format); + BX_CHECK(ti.storageSize == _mem->size + , "createTexture2D: Texture storage size doesn't match passed memory size (storage size: %d, memory size: %d)" + , ti.storageSize + , _mem->size + ); + } + uint32_t size = sizeof(uint32_t)+sizeof(TextureCreate); const Memory* mem = alloc(size); @@ -984,6 +996,18 @@ namespace bgfx TextureHandle createTexture3D(uint16_t _width, uint16_t _height, uint16_t _depth, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags, const Memory* _mem) { BGFX_CHECK_MAIN_THREAD(); + + if (NULL != _mem) + { + TextureInfo ti; + calcTextureSize(ti, _width, _height, _depth, _numMips, _format); + BX_CHECK(ti.storageSize == _mem->size + , "createTexture3D: Texture storage size doesn't match passed memory size (storage size: %d, memory size: %d)" + , ti.storageSize + , _mem->size + ); + } + uint32_t size = sizeof(uint32_t)+sizeof(TextureCreate); const Memory* mem = alloc(size); @@ -1008,6 +1032,18 @@ namespace bgfx TextureHandle createTextureCube(uint16_t _sides, uint16_t _width, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags, const Memory* _mem) { BGFX_CHECK_MAIN_THREAD(); + + if (NULL != _mem) + { + TextureInfo ti; + calcTextureSize(ti, _width, _width, 1, _numMips, _format); + BX_CHECK(ti.storageSize*_sides == _mem->size + , "createTextureCube: Texture storage size doesn't match passed memory size (storage size: %d, memory size: %d)" + , ti.storageSize*_sides + , _mem->size + ); + } + uint32_t size = sizeof(uint32_t)+sizeof(TextureCreate); const Memory* mem = alloc(size); diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index 0636e90c..36b29b76 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -78,9 +78,7 @@ namespace bgfx static Extension s_extension[Extension::Count] = { { "GL_EXT_texture_filter_anisotropic", false, true }, - // Nvidia BGRA on Linux bug: - // https://groups.google.com/a/chromium.org/forum/?fromgroups#!topic/chromium-reviews/yFfbUdyeUCQ - { "GL_EXT_texture_format_BGRA8888", false, !BX_PLATFORM_LINUX }, + { "GL_EXT_texture_format_BGRA8888", false, true }, { "GL_EXT_texture_compression_s3tc", false, true }, { "GL_EXT_texture_compression_dxt1", false, true }, { "GL_CHROMIUM_texture_compression_dxt3", false, true }, @@ -190,6 +188,7 @@ namespace bgfx , m_maxAnisotropy(0.0f) , m_dxtSupport(false) , m_programBinarySupport(false) + , m_textureSwizzleSupport(false) , m_flip(false) , m_postSwapBuffers(NULL) , m_hash( (BX_PLATFORM_WINDOWS<<1) | BX_ARCH_64BIT) @@ -714,6 +713,7 @@ namespace bgfx float m_maxAnisotropy; bool m_dxtSupport; bool m_programBinarySupport; + bool m_textureSwizzleSupport; bool m_flip; PostSwapBuffersFn m_postSwapBuffers; @@ -935,7 +935,7 @@ namespace bgfx uint8_t m_bpp; }; - static const TextureFormatInfo s_textureFormat[TextureFormat::Count] = + static TextureFormatInfo s_textureFormat[TextureFormat::Count] = { { GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_ZERO, 4 }, { GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_ZERO, 4 }, @@ -1434,20 +1434,11 @@ namespace bgfx || TextureFormat::Unknown < dds.m_type) { bool decompress = TextureFormat::Unknown > dds.m_type; - - if (GL_RGBA == internalFmt - || decompress) - { - internalFmt = GL_RGBA; - m_fmt = s_extension[Extension::EXT_texture_format_BGRA8888].m_supported ? GL_BGRA_EXT : GL_RGBA; - - } - bool swizzle = GL_RGBA == m_fmt; #if BGFX_CONFIG_RENDERER_OPENGL if (swizzle - && (s_extension[Extension::ARB_texture_swizzle].m_supported || s_extension[Extension::EXT_texture_swizzle].m_supported) ) + && s_renderCtx.m_textureSwizzleSupport) { swizzle = false; GLint swizzleMask[] = { GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA }; @@ -1590,6 +1581,17 @@ namespace bgfx uint32_t bpp = tfi.m_bpp; uint8_t* data = NULL != tc.m_mem ? tc.m_mem->data : NULL; uint32_t min = m_compressed ? 4 : 1; + bool swizzle = GL_RGBA == m_fmt; + +#if BGFX_CONFIG_RENDERER_OPENGL + if (swizzle + && s_renderCtx.m_textureSwizzleSupport) + { + swizzle = false; + GLint swizzleMask[] = { GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA }; + GL_CHECK(glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask) ); + } +#endif // BGFX_CONFIG_RENDERER_OPENGL for (uint8_t side = 0, numSides = tc.m_cubeMap ? 6 : 1; side < numSides; ++side) { @@ -1619,6 +1621,11 @@ namespace bgfx } else { + if (swizzle) + { + rgbaToBgra(data, width, height); + } + texImage(target+side , lod , internalFmt @@ -2195,10 +2202,21 @@ namespace bgfx || s_extension[Extension::OES_get_program_binary].m_supported ; + s_renderCtx.m_textureSwizzleSupport = false + || s_extension[Extension::ARB_texture_swizzle].m_supported + || s_extension[Extension::EXT_texture_swizzle].m_supported + ; + if (s_extension[Extension::EXT_texture_filter_anisotropic].m_supported) { glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &s_renderCtx.m_maxAnisotropy); } + + if (s_extension[Extension::EXT_texture_format_BGRA8888].m_supported) + { + s_textureFormat[TextureFormat::BGRX8].m_fmt = GL_BGRA_EXT; + s_textureFormat[TextureFormat::BGRA8].m_fmt = GL_BGRA_EXT; + } } }