mirror of
https://github.com/scratchfoundation/bgfx.git
synced 2024-11-28 10:35:43 -05:00
Added missing pack/unpack for texture formats.
This commit is contained in:
parent
4ae57c2ef6
commit
ddec926231
3 changed files with 271 additions and 105 deletions
208
src/image.cpp
208
src/image.cpp
|
@ -1115,6 +1115,63 @@ namespace bgfx
|
|||
memcpy(_dst, _src, 8);
|
||||
}
|
||||
|
||||
template<int32_t MantissaBits, int32_t ExpBits>
|
||||
void encodeRgbE(float* _dst, const float* _src)
|
||||
{
|
||||
const int32_t expMax = (1<<ExpBits) - 1;
|
||||
const int32_t expBias = (1<<(ExpBits - 1) ) - 1;
|
||||
const float sharedExpMax = float( (1 << MantissaBits) - 1) / (1 << MantissaBits) * (1 << (expMax-expBias));
|
||||
|
||||
const float rr = bx::fclamp(_src[0], 0.0f, sharedExpMax);
|
||||
const float gg = bx::fclamp(_src[1], 0.0f, sharedExpMax);
|
||||
const float bb = bx::fclamp(_src[2], 0.0f, sharedExpMax);
|
||||
const float max = bx::fmax3(rr, gg, bb);
|
||||
const float expShared = bx::fmax(-expBias-1, bx::ffloor(bx::flog2(max) ) ) + 1 + expBias;
|
||||
const float invExp = 1.0f/bx::fpow(2.0f, expShared - expBias - MantissaBits);
|
||||
|
||||
_dst[0] = rr * invExp;
|
||||
_dst[1] = gg * invExp;
|
||||
_dst[2] = bb * invExp;
|
||||
_dst[3] = expShared;
|
||||
}
|
||||
|
||||
template<int32_t MantissaBits, int32_t ExpBits>
|
||||
void decodeRgbE(float* _dst, const float* _src)
|
||||
{
|
||||
const int32_t expBias = (1<<(ExpBits - 1) ) - 1;
|
||||
const float exp = bx::fpow(2.0f, _src[3]-float(expBias-MantissaBits) );
|
||||
_dst[0] = _src[0] * exp;
|
||||
_dst[1] = _src[1] * exp;
|
||||
_dst[2] = _src[2] * exp;
|
||||
}
|
||||
|
||||
// RGB9E5F
|
||||
void packRgb9E5F(void* _dst, const float* _src)
|
||||
{
|
||||
float tmp[4];
|
||||
encodeRgbE<9, 5>(tmp, _src);
|
||||
|
||||
*( (uint32_t*)_dst) = 0
|
||||
| (toUnorm(tmp[0], 511.0f) )
|
||||
| (toUnorm(tmp[1], 511.0f)<< 9)
|
||||
| (toUnorm(tmp[2], 511.0f)<<18)
|
||||
| (uint32_t(tmp[3]) <<27)
|
||||
;
|
||||
}
|
||||
|
||||
void unpackRgb9E5F(float* _dst, const void* _src)
|
||||
{
|
||||
uint32_t packed = *( (const uint32_t*)_src);
|
||||
|
||||
float tmp[4];
|
||||
tmp[0] = float( ( (packed ) & 0x1ff) ) / 511.0f;
|
||||
tmp[1] = float( ( (packed>> 9) & 0x1ff) ) / 511.0f;
|
||||
tmp[2] = float( ( (packed>>18) & 0x1ff) ) / 511.0f;
|
||||
tmp[3] = float( ( (packed>>27) & 0x1f) );
|
||||
|
||||
decodeRgbE<9, 5>(_dst, tmp);
|
||||
}
|
||||
|
||||
// RGBA32I
|
||||
void packRgba32I(void* _dst, const float* _src)
|
||||
{
|
||||
|
@ -1148,6 +1205,102 @@ namespace bgfx
|
|||
memcpy(_dst, _src, 16);
|
||||
}
|
||||
|
||||
// R5G6B5
|
||||
void packR5G6B5(void* _dst, const float* _src)
|
||||
{
|
||||
*( (uint16_t*)_dst) = 0
|
||||
| uint16_t(toUnorm(_src[0], 31.0f) )
|
||||
| uint16_t(toUnorm(_src[1], 63.0f)<< 5)
|
||||
| uint16_t(toUnorm(_src[2], 31.0f)<<11)
|
||||
;
|
||||
}
|
||||
|
||||
void unpackR5G6B5(float* _dst, const void* _src)
|
||||
{
|
||||
uint16_t packed = *( (const uint16_t*)_src);
|
||||
_dst[0] = float( ( (packed ) & 0x1f) ) / 31.0f;
|
||||
_dst[1] = float( ( (packed>> 5) & 0x3f) ) / 63.0f;
|
||||
_dst[2] = float( ( (packed>>11) & 0x1f) ) / 31.0f;
|
||||
}
|
||||
|
||||
// RGBA4
|
||||
void packRgba4(void* _dst, const float* _src)
|
||||
{
|
||||
*( (uint16_t*)_dst) = 0
|
||||
| uint16_t(toUnorm(_src[0], 15.0f) )
|
||||
| uint16_t(toUnorm(_src[1], 15.0f)<< 4)
|
||||
| uint16_t(toUnorm(_src[2], 15.0f)<< 8)
|
||||
| uint16_t(toUnorm(_src[3], 15.0f)<<12)
|
||||
;
|
||||
}
|
||||
|
||||
void unpackRgba4(float* _dst, const void* _src)
|
||||
{
|
||||
uint16_t packed = *( (const uint16_t*)_src);
|
||||
_dst[0] = float( ( (packed ) & 0x7) ) / 15.0f;
|
||||
_dst[1] = float( ( (packed>> 4) & 0x7) ) / 15.0f;
|
||||
_dst[2] = float( ( (packed>> 8) & 0x7) ) / 15.0f;
|
||||
_dst[3] = float( ( (packed>>12) & 0x7) ) / 15.0f;
|
||||
}
|
||||
|
||||
// RGB5A1
|
||||
void packRgb5a1(void* _dst, const float* _src)
|
||||
{
|
||||
*( (uint16_t*)_dst) = 0
|
||||
| uint16_t(toUnorm(_src[0], 31.0f) )
|
||||
| uint16_t(toUnorm(_src[1], 31.0f)<< 5)
|
||||
| uint16_t(toUnorm(_src[2], 31.0f)<<10)
|
||||
| uint16_t(toUnorm(_src[3], 1.0f)<<15)
|
||||
;
|
||||
}
|
||||
|
||||
void unpackRgb5a1(float* _dst, const void* _src)
|
||||
{
|
||||
uint16_t packed = *( (const uint16_t*)_src);
|
||||
_dst[0] = float( ( (packed ) & 0x1f) ) / 31.0f;
|
||||
_dst[1] = float( ( (packed>> 5) & 0x1f) ) / 31.0f;
|
||||
_dst[2] = float( ( (packed>>10) & 0x1f) ) / 31.0f;
|
||||
_dst[3] = float( ( (packed>>14) & 0x1) );
|
||||
}
|
||||
|
||||
// RGB10A2
|
||||
void packRgb10A2(void* _dst, const float* _src)
|
||||
{
|
||||
*( (uint32_t*)_dst) = 0
|
||||
| (toUnorm(_src[0], 1023.0f) )
|
||||
| (toUnorm(_src[1], 1023.0f)<<10)
|
||||
| (toUnorm(_src[2], 1023.0f)<<20)
|
||||
| (toUnorm(_src[3], 3.0f)<<30)
|
||||
;
|
||||
}
|
||||
|
||||
void unpackRgb10A2(float* _dst, const void* _src)
|
||||
{
|
||||
uint32_t packed = *( (const uint32_t*)_src);
|
||||
_dst[0] = float( ( (packed ) & 0x3ff) ) / 1023.0f;
|
||||
_dst[1] = float( ( (packed>>10) & 0x3ff) ) / 1023.0f;
|
||||
_dst[2] = float( ( (packed>>20) & 0x3ff) ) / 1023.0f;
|
||||
_dst[3] = float( ( (packed>>30) & 0x3) ) / 3.0f;
|
||||
}
|
||||
|
||||
// R11G11B10F
|
||||
void packR11G11B10F(void* _dst, const float* _src)
|
||||
{
|
||||
*( (uint32_t*)_dst) = 0
|
||||
| ( (bx::halfFromFloat(_src[0])>> 4) & 0x7ff)
|
||||
| ( (bx::halfFromFloat(_src[0])<< 7) & 0x3ff800)
|
||||
| ( (bx::halfFromFloat(_src[0])<<17) & 0xffc00000)
|
||||
;
|
||||
}
|
||||
|
||||
void unpackR11G11B10F(float* _dst, const void* _src)
|
||||
{
|
||||
uint32_t packed = *( (const uint32_t*)_src);
|
||||
_dst[0] = bx::halfToFloat( (packed<< 4) & 0x7ff0);
|
||||
_dst[1] = bx::halfToFloat( (packed>> 7) & 0x7ff0);
|
||||
_dst[2] = bx::halfToFloat( (packed>>17) & 0x7fe0);
|
||||
}
|
||||
|
||||
typedef void (*PackFn)(void*, const float*);
|
||||
typedef void (*UnpackFn)(float*, const void*);
|
||||
|
||||
|
@ -1203,7 +1356,7 @@ namespace bgfx
|
|||
{ packRg32I, unpackRg32I }, // RG32I
|
||||
{ packRg32U, unpackRg32U }, // RG32U
|
||||
{ packRg32F, unpackRg32F }, // RG32F
|
||||
{ NULL, NULL }, // RGB9E5F
|
||||
{ packRgb9E5F, unpackRgb9E5F }, // RGB9E5F
|
||||
{ packBgra8, unpackBgra8 }, // BGRA8
|
||||
{ packRgba8, unpackRgba8 }, // RGBA8
|
||||
{ packRgba8I, unpackRgba8I }, // RGBA8I
|
||||
|
@ -1217,11 +1370,11 @@ namespace bgfx
|
|||
{ packRgba32I, unpackRgba32I }, // RGBA32I
|
||||
{ packRgba32U, unpackRgba32U }, // RGBA32U
|
||||
{ packRgba32F, unpackRgba32F }, // RGBA32F
|
||||
{ NULL, NULL }, // R5G6B5
|
||||
{ NULL, NULL }, // RGBA4
|
||||
{ NULL, NULL }, // RGB5A1
|
||||
{ NULL, NULL }, // RGB10A2
|
||||
{ NULL, NULL }, // R11G11B10F
|
||||
{ packR5G6B5, unpackR5G6B5 }, // R5G6B5
|
||||
{ packRgba4, unpackRgba4 }, // RGBA4
|
||||
{ packRgb5a1, unpackRgb5a1 }, // RGB5A1
|
||||
{ packRgb10A2, unpackRgb10A2 }, // RGB10A2
|
||||
{ packR11G11B10F, unpackR11G11B10F }, // R11G11B10F
|
||||
{ NULL, NULL }, // UnknownDepth
|
||||
{ NULL, NULL }, // D16
|
||||
{ NULL, NULL }, // D24
|
||||
|
@ -1234,8 +1387,16 @@ namespace bgfx
|
|||
};
|
||||
BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_packUnpack) );
|
||||
|
||||
void imageConvert(void* _dst, TextureFormat::Enum _dstFormat, const void* _src, TextureFormat::Enum _srcFormat, uint32_t _width, uint32_t _height)
|
||||
bool imageConvert(void* _dst, TextureFormat::Enum _dstFormat, const void* _src, TextureFormat::Enum _srcFormat, uint32_t _width, uint32_t _height)
|
||||
{
|
||||
UnpackFn unpack = s_packUnpack[_srcFormat].unpack;
|
||||
PackFn pack = s_packUnpack[_dstFormat].pack;
|
||||
if (NULL == pack
|
||||
|| NULL == unpack)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const uint8_t* src = (uint8_t*)_src;
|
||||
uint8_t* dst = (uint8_t*)_dst;
|
||||
|
||||
|
@ -1244,9 +1405,6 @@ namespace bgfx
|
|||
const uint32_t srcPitch = _width * srcBpp / 8;
|
||||
const uint32_t dstPitch = _width * dstBpp / 8;
|
||||
|
||||
UnpackFn unpack = s_packUnpack[_srcFormat].unpack;
|
||||
PackFn pack = s_packUnpack[_dstFormat].pack;
|
||||
|
||||
for (uint32_t yy = 0; yy < _height; ++yy, src += srcPitch, dst += dstPitch)
|
||||
{
|
||||
for (uint32_t xx = 0; xx < _width; ++xx)
|
||||
|
@ -1256,6 +1414,8 @@ namespace bgfx
|
|||
pack(&dst[xx*srcBpp/8], rgba);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t bitRangeConvert(uint32_t _in, uint32_t _from, uint32_t _to)
|
||||
|
@ -2042,6 +2202,34 @@ namespace bgfx
|
|||
}
|
||||
}
|
||||
|
||||
const Memory* imageAlloc(ImageContainer& _imageContainer, TextureFormat::Enum _format, uint16_t _width, uint16_t _height, uint16_t _depth, bool _cubeMap, bool _mips)
|
||||
{
|
||||
const uint8_t numMips = _mips ? imageGetNumMips(_format, _width, _height) : 1;
|
||||
uint32_t size = imageGetSize(_format, _width, _height, 0, false, numMips);
|
||||
const Memory* image = alloc(size);
|
||||
|
||||
_imageContainer.m_data = image->data;
|
||||
_imageContainer.m_format = _format;
|
||||
_imageContainer.m_size = image->size;
|
||||
_imageContainer.m_offset = 0;
|
||||
_imageContainer.m_width = _width;
|
||||
_imageContainer.m_height = _height;
|
||||
_imageContainer.m_depth = _depth;
|
||||
_imageContainer.m_numMips = numMips;
|
||||
_imageContainer.m_hasAlpha = false;
|
||||
_imageContainer.m_cubeMap = _cubeMap;
|
||||
_imageContainer.m_ktx = false;
|
||||
_imageContainer.m_ktxLE = false;
|
||||
_imageContainer.m_srgb = false;
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
void imageFree(const Memory* _memory)
|
||||
{
|
||||
release(_memory);
|
||||
}
|
||||
|
||||
// DDS
|
||||
#define DDS_MAGIC BX_MAKEFOURCC('D', 'D', 'S', ' ')
|
||||
#define DDS_HEADER_SIZE 124
|
||||
|
|
|
@ -118,7 +118,13 @@ namespace bgfx
|
|||
void imageCopy(uint32_t _width, uint32_t _height, uint32_t _bpp, uint32_t _pitch, const void* _src, void* _dst);
|
||||
|
||||
///
|
||||
void imageConvert(void* _dst, TextureFormat::Enum _dstFormat, const void* _src, TextureFormat::Enum _srcFormat, uint32_t _width, uint32_t _height);
|
||||
bool imageConvert(void* _dst, TextureFormat::Enum _dstFormat, const void* _src, TextureFormat::Enum _srcFormat, uint32_t _width, uint32_t _height);
|
||||
|
||||
///
|
||||
const Memory* imageAlloc(ImageContainer& _imageContainer, TextureFormat::Enum _format, uint16_t _width, uint16_t _height, uint16_t _depth = 0, bool _cubeMap = false, bool _mips = false);
|
||||
|
||||
///
|
||||
void imageFree(const Memory* _memory);
|
||||
|
||||
///
|
||||
void imageWriteTga(bx::WriterI* _writer, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, bool _grayscale, bool _yflip);
|
||||
|
|
|
@ -210,34 +210,6 @@ namespace bgfx
|
|||
}
|
||||
}
|
||||
|
||||
const Memory* imageAlloc(ImageContainer& _imageContainer, TextureFormat::Enum _format, uint16_t _width, uint16_t _height, uint16_t _depth = 0, bool _cubeMap = false, bool _mips = false)
|
||||
{
|
||||
const uint8_t numMips = _mips ? imageGetNumMips(_format, _width, _height) : 1;
|
||||
uint32_t size = imageGetSize(_format, _width, _height, 0, false, numMips);
|
||||
const Memory* image = alloc(size);
|
||||
|
||||
_imageContainer.m_data = image->data;
|
||||
_imageContainer.m_format = _format;
|
||||
_imageContainer.m_size = image->size;
|
||||
_imageContainer.m_offset = 0;
|
||||
_imageContainer.m_width = _width;
|
||||
_imageContainer.m_height = _height;
|
||||
_imageContainer.m_depth = _depth;
|
||||
_imageContainer.m_numMips = numMips;
|
||||
_imageContainer.m_hasAlpha = false;
|
||||
_imageContainer.m_cubeMap = _cubeMap;
|
||||
_imageContainer.m_ktx = false;
|
||||
_imageContainer.m_ktxLE = false;
|
||||
_imageContainer.m_srgb = false;
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
void imageFree(const Memory* _memory)
|
||||
{
|
||||
release(_memory);
|
||||
}
|
||||
|
||||
} // namespace bgfx
|
||||
|
||||
void help(const char* _error = NULL)
|
||||
|
|
Loading…
Reference in a new issue