texturec: Fixed conversion of unaligned image.

This commit is contained in:
Branimir Karadžić 2016-03-18 17:46:10 -07:00
parent a076c1a03b
commit d45d3c62f8
2 changed files with 29 additions and 20 deletions

View file

@ -2364,6 +2364,16 @@ namespace bgfx
const Memory* imageAlloc(ImageContainer& _imageContainer, TextureFormat::Enum _format, uint16_t _width, uint16_t _height, uint16_t _depth, bool _cubeMap, bool _generateMips)
{
const ImageBlockInfo& blockInfo = getBlockInfo(_format);
const uint16_t blockWidth = blockInfo.blockWidth;
const uint16_t blockHeight = blockInfo.blockHeight;
const uint16_t minBlockX = blockInfo.minBlockX;
const uint16_t minBlockY = blockInfo.minBlockY;
_width = bx::uint16_max(blockWidth * minBlockX, ( (_width + blockWidth - 1) / blockWidth)*blockWidth);
_height = bx::uint16_max(blockHeight * minBlockY, ( (_height + blockHeight - 1) / blockHeight)*blockHeight);
_depth = bx::uint16_max(1, _depth);
const uint8_t numMips = _generateMips ? imageGetNumMips(_format, _width, _height) : 1;
uint32_t size = imageGetSize(_format, _width, _height, 0, false, numMips);
const Memory* image = alloc(size);

View file

@ -472,7 +472,12 @@ int main(int _argc, const char* _argv[])
if (normalMap)
{
uint32_t size = imageGetSize(TextureFormat::RGBA32F, mip.m_width, mip.m_height);
output = imageAlloc(imageContainer, format, mip.m_width, mip.m_height, 0, false, mips);
ImageMip dstMip;
imageGetRawData(imageContainer, 0, 0, NULL, 0, dstMip);
uint32_t size = imageGetSize(TextureFormat::RGBA32F, dstMip.m_width, dstMip.m_height);
temp = BX_ALLOC(&allocator, size);
float* rgba = (float*)temp;
float* rgbaDst = (float*)BX_ALLOC(&allocator, size);
@ -502,19 +507,13 @@ int main(int _argc, const char* _argv[])
}
}
output = imageAlloc(imageContainer, format, mip.m_width, mip.m_height, 0, false, mips);
imageRgba32f11to01(rgbaDst, mip.m_width, mip.m_height, mip.m_width*16, rgba);
imageEncodeFromRgba32f(&allocator, output->data, rgbaDst, mip.m_width, mip.m_height, format);
imageRgba32f11to01(rgbaDst, dstMip.m_width, dstMip.m_height, dstMip.m_width*16, rgba);
imageEncodeFromRgba32f(&allocator, output->data, rgbaDst, dstMip.m_width, dstMip.m_height, format);
for (uint8_t lod = 1; lod < numMips; ++lod)
{
imageRgba32fDownsample2x2NormalMap(mip.m_width, mip.m_height, mip.m_width*16, rgba, rgba);
imageRgba32f11to01(rgbaDst, mip.m_width, mip.m_height, mip.m_width*16, rgba);
mip.m_width = bx::uint32_max(1, mip.m_width >> 1);
mip.m_height = bx::uint32_max(1, mip.m_height >> 1);
ImageMip dstMip;
imageRgba32fDownsample2x2NormalMap(dstMip.m_width, dstMip.m_height, dstMip.m_width*16, rgba, rgba);
imageRgba32f11to01(rgbaDst, dstMip.m_width, dstMip.m_height, dstMip.m_width*16, rgba);
imageGetRawData(imageContainer, 0, lod, output->data, output->size, dstMip);
uint8_t* data = const_cast<uint8_t*>(dstMip.m_data);
imageEncodeFromRgba32f(&allocator, data, rgbaDst, dstMip.m_width, dstMip.m_height, format);
@ -524,8 +523,14 @@ int main(int _argc, const char* _argv[])
}
else
{
uint32_t size = imageGetSize(TextureFormat::RGBA8, mip.m_width, mip.m_height);
output = imageAlloc(imageContainer, format, mip.m_width, mip.m_height, 0, false, mips);
ImageMip dstMip;
imageGetRawData(imageContainer, 0, 0, NULL, 0, dstMip);
uint32_t size = imageGetSize(TextureFormat::RGBA8, dstMip.m_width, dstMip.m_height);
temp = BX_ALLOC(&allocator, size);
memset(temp, 0, size);
uint8_t* rgba = (uint8_t*)temp;
imageDecodeToRgba8(rgba
@ -536,17 +541,11 @@ int main(int _argc, const char* _argv[])
, mip.m_format
);
output = imageAlloc(imageContainer, format, mip.m_width, mip.m_height, 0, false, mips);
imageEncodeFromRgba8(output->data, rgba, mip.m_width, mip.m_height, format);
imageEncodeFromRgba8(output->data, rgba, dstMip.m_width, dstMip.m_height, format);
for (uint8_t lod = 1; lod < numMips; ++lod)
{
imageRgba8Downsample2x2(mip.m_width, mip.m_height, mip.m_width*4, rgba, rgba);
mip.m_width = bx::uint32_max(1, mip.m_width >> 1);
mip.m_height = bx::uint32_max(1, mip.m_height >> 1);
ImageMip dstMip;
imageRgba8Downsample2x2(dstMip.m_width, dstMip.m_height, dstMip.m_width*4, rgba, rgba);
imageGetRawData(imageContainer, 0, lod, output->data, output->size, dstMip);
uint8_t* data = const_cast<uint8_t*>(dstMip.m_data);
imageEncodeFromRgba8(data, rgba, dstMip.m_width, dstMip.m_height, format);