From f81f34545cee9bebdba2500a808c0f76a1b53911 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Mon, 9 Feb 2015 11:24:59 -0800 Subject: [PATCH] Added cubemap to texture info. Fixed issue #255. --- examples/08-update/update.cpp | 8 ++++---- include/bgfx.c99.h | 3 ++- include/bgfx.h | 3 ++- src/bgfx.cpp | 36 ++++++++++++++++++----------------- src/bgfx_p.h | 8 +++++--- 5 files changed, 32 insertions(+), 26 deletions(-) diff --git a/examples/08-update/update.cpp b/examples/08-update/update.cpp index e151f9ba9..0ed14fa3f 100644 --- a/examples/08-update/update.cpp +++ b/examples/08-update/update.cpp @@ -84,7 +84,7 @@ static const uint16_t s_cubeIndices[36] = 9, 10, 11, 12, 14, 13, // 6 - 14, 15, 13, + 14, 15, 13, 16, 18, 17, // 8 18, 19, 17, @@ -96,7 +96,7 @@ static const uint16_t s_cubeIndices[36] = static void updateTextureCubeRectBgra8(bgfx::TextureHandle _handle, uint8_t _side, uint32_t _x, uint32_t _y, uint32_t _width, uint32_t _height, uint8_t _r, uint8_t _g, uint8_t _b, uint8_t _a = 0xff) { bgfx::TextureInfo ti; - bgfx::calcTextureSize(ti, _width, _height, 1, 1, bgfx::TextureFormat::BGRA8); + bgfx::calcTextureSize(ti, _width, _height, 1, 1, false, bgfx::TextureFormat::BGRA8); const bgfx::Memory* mem = bgfx::alloc(ti.storageSize); uint8_t* data = (uint8_t*)mem->data; @@ -285,7 +285,7 @@ int _main_(int /*_argc*/, char** /*_argv*/) float at[3] = { 0.0f, 0.0f, 0.0f }; float eye[3] = { 0.0f, 0.0f, -5.0f }; - + float view[16]; float proj[16]; bx::mtxLookAt(view, eye, at); @@ -394,7 +394,7 @@ int _main_(int /*_argc*/, char** /*_argv*/) bgfx::submit(1); } - // Advance to next frame. Rendering thread will be kicked to + // Advance to next frame. Rendering thread will be kicked to // process submitted rendering primitives. bgfx::frame(); } diff --git a/include/bgfx.c99.h b/include/bgfx.c99.h index afc1c44ed..df1930e5f 100644 --- a/include/bgfx.c99.h +++ b/include/bgfx.c99.h @@ -265,6 +265,7 @@ typedef struct bgfx_texture_info uint16_t depth; uint8_t numMips; uint8_t bitsPerPixel; + bool cubeMap; } bgfx_texture_info_t; @@ -870,7 +871,7 @@ BGFX_C_API void bgfx_destroy_program(bgfx_program_handle_t _handle); /** * Calculate amount of memory required for texture. */ -BGFX_C_API void bgfx_calc_texture_size(bgfx_texture_info_t* _info, uint16_t _width, uint16_t _height, uint16_t _depth, uint8_t _numMips, bgfx_texture_format_t _format); +BGFX_C_API void bgfx_calc_texture_size(bgfx_texture_info_t* _info, uint16_t _width, uint16_t _height, uint16_t _depth, bool _cubeMap, uint8_t _numMips, bgfx_texture_format_t _format); /** * Create texture from memory buffer. diff --git a/include/bgfx.h b/include/bgfx.h index c81a51e03..31bf19a6f 100644 --- a/include/bgfx.h +++ b/include/bgfx.h @@ -360,6 +360,7 @@ namespace bgfx uint16_t depth; //!< Texture depth. uint8_t numMips; //!< Number of MIP maps. uint8_t bitsPerPixel; //!< Format bits per pixel. + bool cubeMap; //!< Texture is cubemap. }; /// @@ -817,7 +818,7 @@ namespace bgfx void destroyProgram(ProgramHandle _handle); /// Calculate amount of memory required for texture. - void calcTextureSize(TextureInfo& _info, uint16_t _width, uint16_t _height, uint16_t _depth, uint8_t _numMips, TextureFormat::Enum _format); + void calcTextureSize(TextureInfo& _info, uint16_t _width, uint16_t _height, uint16_t _depth, bool _cubeMap, uint8_t _numMips, TextureFormat::Enum _format); /// Create texture from memory buffer. /// diff --git a/src/bgfx.cpp b/src/bgfx.cpp index d068b5aaf..35320e1a4 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -2309,7 +2309,7 @@ again: s_ctx->destroyProgram(_handle); } - void calcTextureSize(TextureInfo& _info, uint16_t _width, uint16_t _height, uint16_t _depth, uint8_t _numMips, TextureFormat::Enum _format) + void calcTextureSize(TextureInfo& _info, uint16_t _width, uint16_t _height, uint16_t _depth, bool _cubeMap, uint8_t _numMips, TextureFormat::Enum _format) { const ImageBlockInfo& blockInfo = getBlockInfo(_format); const uint8_t bpp = blockInfo.bitsPerPixel; @@ -2326,7 +2326,8 @@ again: uint32_t width = _width; uint32_t height = _height; uint32_t depth = _depth; - uint32_t size = 0; + uint32_t sides = _cubeMap ? 6 : 1; + uint32_t size = 0; for (uint32_t lod = 0; lod < _numMips; ++lod) { @@ -2334,7 +2335,7 @@ again: height = bx::uint32_max(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight); depth = bx::uint32_max(1, depth); - size += width*height*depth*bpp/8; + size += width*height*depth*bpp/8 * sides; width >>= 1; height >>= 1; @@ -2346,6 +2347,7 @@ again: _info.height = _height; _info.depth = _depth; _info.numMips = _numMips; + _info.cubeMap = _cubeMap; _info.storageSize = size; _info.bitsPerPixel = bpp; } @@ -2367,7 +2369,7 @@ again: && NULL != _mem) { TextureInfo ti; - calcTextureSize(ti, _width, _height, 1, _numMips, _format); + calcTextureSize(ti, _width, _height, 1, false, _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 @@ -2408,7 +2410,7 @@ again: && NULL != _mem) { TextureInfo ti; - calcTextureSize(ti, _width, _height, _depth, _numMips, _format); + calcTextureSize(ti, _width, _height, _depth, false, _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 @@ -2448,10 +2450,10 @@ again: && NULL != _mem) { TextureInfo ti; - calcTextureSize(ti, _size, _size, 1, _numMips, _format); - BX_CHECK(ti.storageSize*6 == _mem->size + calcTextureSize(ti, _size, _size, 1, true, _numMips, _format); + BX_CHECK(ti.storageSize == _mem->size , "createTextureCube: Texture storage size doesn't match passed memory size (storage size: %d, memory size: %d)" - , ti.storageSize*6 + , ti.storageSize , _mem->size ); } @@ -2464,15 +2466,15 @@ again: bx::write(&writer, magic); TextureCreate tc; - tc.m_flags = _flags; - tc.m_width = _size; - tc.m_height = _size; - tc.m_sides = 6; - tc.m_depth = 0; + tc.m_flags = _flags; + tc.m_width = _size; + tc.m_height = _size; + tc.m_sides = 6; + tc.m_depth = 0; tc.m_numMips = _numMips; - tc.m_format = uint8_t(_format); + tc.m_format = uint8_t(_format); tc.m_cubeMap = true; - tc.m_mem = _mem; + tc.m_mem = _mem; bx::write(&writer, tc); return s_ctx->createTexture(mem, _flags, 0, NULL); @@ -3199,10 +3201,10 @@ BGFX_C_API void bgfx_destroy_program(bgfx_program_handle_t _handle) bgfx::destroyProgram(handle.cpp); } -BGFX_C_API void bgfx_calc_texture_size(bgfx_texture_info_t* _info, uint16_t _width, uint16_t _height, uint16_t _depth, uint8_t _numMips, bgfx_texture_format_t _format) +BGFX_C_API void bgfx_calc_texture_size(bgfx_texture_info_t* _info, uint16_t _width, uint16_t _height, uint16_t _depth, bool _cubeMap, uint8_t _numMips, bgfx_texture_format_t _format) { bgfx::TextureInfo& info = *(bgfx::TextureInfo*)_info; - bgfx::calcTextureSize(info, _width, _height, _depth, _numMips, bgfx::TextureFormat::Enum(_format) ); + bgfx::calcTextureSize(info, _width, _height, _depth, _cubeMap, _numMips, bgfx::TextureFormat::Enum(_format) ); } BGFX_C_API bgfx_texture_handle_t bgfx_create_texture(const bgfx_memory_t* _mem, uint32_t _flags, uint8_t _skip, bgfx_texture_info_t* _info) diff --git a/src/bgfx_p.h b/src/bgfx_p.h index 8589acb35..e158b6021 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -2681,6 +2681,7 @@ namespace bgfx , (uint16_t)imageContainer.m_width , (uint16_t)imageContainer.m_height , (uint16_t)imageContainer.m_depth + , imageContainer.m_cubeMap , imageContainer.m_numMips , TextureFormat::Enum(imageContainer.m_format) ); @@ -2689,11 +2690,12 @@ namespace bgfx { _info->format = TextureFormat::Unknown; _info->storageSize = 0; - _info->width = 0; - _info->height = 0; - _info->depth = 0; + _info->width = 0; + _info->height = 0; + _info->depth = 0; _info->numMips = 0; _info->bitsPerPixel = 0; + _info->cubeMap = false; } }