texturec: Support for normal maps WIP.
This commit is contained in:
parent
4a88d1cf4f
commit
4770831b11
9 changed files with 370 additions and 145 deletions
|
@ -54,7 +54,7 @@ namespace bgfx
|
|||
::free(mem);
|
||||
}
|
||||
|
||||
void imageEncodeFromRgba8(uint8_t* _dst, const uint8_t* _src, uint32_t _width, uint32_t _height, uint8_t _format)
|
||||
void imageEncodeFromRgba8(void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint8_t _format)
|
||||
{
|
||||
TextureFormat::Enum format = TextureFormat::Enum(_format);
|
||||
|
||||
|
@ -65,7 +65,7 @@ namespace bgfx
|
|||
case TextureFormat::BC3:
|
||||
case TextureFormat::BC4:
|
||||
case TextureFormat::BC5:
|
||||
squish::CompressImage(_src, _width, _height, _dst
|
||||
squish::CompressImage( (const uint8_t*)_src, _width, _height, _dst
|
||||
, format == TextureFormat::BC2 ? squish::kDxt3
|
||||
: format == TextureFormat::BC3 ? squish::kDxt5
|
||||
: format == TextureFormat::BC4 ? squish::kBc4
|
||||
|
@ -75,15 +75,15 @@ namespace bgfx
|
|||
break;
|
||||
|
||||
case TextureFormat::BC6H:
|
||||
nvtt::compressBC6H(_src, _width, _height, 4, _dst);
|
||||
nvtt::compressBC6H( (const uint8_t*)_src, _width, _height, 4, _dst);
|
||||
break;
|
||||
|
||||
case TextureFormat::BC7:
|
||||
nvtt::compressBC7(_src, _width, _height, 4, _dst);
|
||||
nvtt::compressBC7( (const uint8_t*)_src, _width, _height, 4, _dst);
|
||||
break;
|
||||
|
||||
case TextureFormat::ETC1:
|
||||
etc1_encode_image(_src, _width, _height, 4, _width*4, _dst);
|
||||
etc1_encode_image( (const uint8_t*)_src, _width, _height, 4, _width*4, (uint8_t*)_dst);
|
||||
break;
|
||||
|
||||
case TextureFormat::ETC2:
|
||||
|
@ -98,7 +98,7 @@ namespace bgfx
|
|||
RgbBitmap bmp;
|
||||
bmp.width = _width;
|
||||
bmp.height = _height;
|
||||
bmp.data = const_cast<uint8_t*>(_src);
|
||||
bmp.data = (uint8_t*)const_cast<void*>(_src);
|
||||
PvrTcEncoder::EncodeRgb4Bpp(_dst, bmp);
|
||||
bmp.data = NULL;
|
||||
}
|
||||
|
@ -113,7 +113,7 @@ namespace bgfx
|
|||
RgbaBitmap bmp;
|
||||
bmp.width = _width;
|
||||
bmp.height = _height;
|
||||
bmp.data = const_cast<uint8_t*>(_src);
|
||||
bmp.data = (uint8_t*)const_cast<void*>(_src);
|
||||
PvrTcEncoder::EncodeRgba4Bpp(_dst, bmp);
|
||||
bmp.data = NULL;
|
||||
}
|
||||
|
@ -136,6 +136,20 @@ namespace bgfx
|
|||
}
|
||||
}
|
||||
|
||||
void imageEncodeFromRgba32f(void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint8_t _format)
|
||||
{
|
||||
TextureFormat::Enum format = TextureFormat::Enum(_format);
|
||||
switch (format)
|
||||
{
|
||||
case TextureFormat::RGBA8:
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
BX_UNUSED(_dst, _src, _width, _height, _format);
|
||||
}
|
||||
|
||||
} // namespace bgfx
|
||||
|
||||
void help(const char* _error = NULL)
|
||||
|
@ -168,6 +182,7 @@ void help(const char* _error = NULL)
|
|||
" -o <file path> Output file path (file will be written in KTX format).\n"
|
||||
" -t <format> Output format type (BC1/2/3/4/5, ETC1, PVR14, etc.).\n"
|
||||
" -m, --mips Generate mip-maps.\n"
|
||||
" -n, --normalmap Input texture is normal map.\n"
|
||||
|
||||
"\n"
|
||||
"For additional information, see https://github.com/bkaradzic/bgfx\n"
|
||||
|
@ -205,7 +220,6 @@ int main(int _argc, const char* _argv[])
|
|||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
const bool mips = cmdLine.hasArg('m', "mips");
|
||||
const char* type = cmdLine.findOption('t');
|
||||
bgfx::TextureFormat::Enum format = bgfx::TextureFormat::BGRA8;
|
||||
|
||||
|
@ -220,6 +234,9 @@ int main(int _argc, const char* _argv[])
|
|||
}
|
||||
}
|
||||
|
||||
const bool mips = cmdLine.hasArg('m', "mips");
|
||||
const bool normalMap = cmdLine.hasArg('n', "normalmap");
|
||||
|
||||
uint32_t size = (uint32_t)bx::getSize(&reader);
|
||||
const bgfx::Memory* mem = bgfx::alloc(size);
|
||||
bx::read(&reader, mem->data, mem->size);
|
||||
|
@ -271,40 +288,72 @@ int main(int _argc, const char* _argv[])
|
|||
ImageMip mip;
|
||||
if (imageGetRawData(imageContainer, 0, 0, mem->data, mem->size, mip) )
|
||||
{
|
||||
uint32_t size = imageGetSize(TextureFormat::RGBA8, mip.m_width, mip.m_height);
|
||||
uint8_t* rgba = (uint8_t*)BX_ALLOC(&allocator, size);
|
||||
|
||||
imageDecodeToRgba8(rgba
|
||||
, mip.m_data
|
||||
, mip.m_width
|
||||
, mip.m_height
|
||||
, mip.m_width*mip.m_bpp/8
|
||||
, mip.m_format
|
||||
);
|
||||
|
||||
uint8_t numMips = mips
|
||||
? imageGetNumMips(format, mip.m_width, mip.m_height)
|
||||
: 1
|
||||
;
|
||||
imageContainer.m_size = imageGetSize(format, mip.m_width, mip.m_height, 0, false, numMips);
|
||||
imageContainer.m_format = format;
|
||||
output = alloc(imageContainer.m_size);
|
||||
|
||||
imageEncodeFromRgba8(output->data, rgba, mip.m_width, mip.m_height, format);
|
||||
void* temp = NULL;
|
||||
|
||||
for (uint8_t lod = 1; lod < numMips; ++lod)
|
||||
if (normalMap)
|
||||
{
|
||||
ImageMip mip1;
|
||||
imageGetRawData(imageContainer, 0, lod, output->data, output->size, mip1);
|
||||
uint8_t* data = const_cast<uint8_t*>(mip1.m_data);
|
||||
uint32_t size = imageGetSize(TextureFormat::RGBA32F, mip.m_width, mip.m_height);
|
||||
temp = BX_ALLOC(&allocator, size);
|
||||
float* rgba = (float*)temp;
|
||||
|
||||
uint32_t width = bx::uint32_max(1, mip.m_width >>lod);
|
||||
uint32_t height = bx::uint32_max(1, mip.m_height>>lod);
|
||||
imageRgba8Downsample2x2(width, height, width*4, rgba, rgba);
|
||||
imageEncodeFromRgba8(data, rgba, mip.m_width, mip.m_height, format);
|
||||
imageDecodeToRgba32f(&allocator
|
||||
, rgba
|
||||
, mip.m_data
|
||||
, mip.m_width
|
||||
, mip.m_height
|
||||
, mip.m_width*mip.m_bpp/8
|
||||
, TextureFormat::Enum(mip.m_format)
|
||||
);
|
||||
|
||||
imageContainer.m_size = imageGetSize(format, mip.m_width, mip.m_height, 0, false, numMips);
|
||||
imageContainer.m_format = format;
|
||||
output = alloc(imageContainer.m_size);
|
||||
|
||||
imageEncodeFromRgba32f(output->data, rgba, mip.m_width, mip.m_height, format);
|
||||
|
||||
for (uint8_t lod = 1; lod < numMips; ++lod)
|
||||
{
|
||||
ImageMip mip1;
|
||||
imageGetRawData(imageContainer, 0, lod, output->data, output->size, mip1);
|
||||
uint8_t* data = const_cast<uint8_t*>(mip1.m_data);
|
||||
|
||||
uint32_t width = bx::uint32_max(1, mip.m_width >>lod);
|
||||
uint32_t height = bx::uint32_max(1, mip.m_height>>lod);
|
||||
imageRgba32fDownsample2x2NormalMap(width, height, width*4, rgba, rgba);
|
||||
imageEncodeFromRgba32f(data, rgba, mip.m_width, mip.m_height, format);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
uint32_t size = imageGetSize(TextureFormat::RGBA8, mip.m_width, mip.m_height);
|
||||
temp = BX_ALLOC(&allocator, size);
|
||||
uint8_t* rgba = (uint8_t*)temp;
|
||||
|
||||
imageContainer.m_size = imageGetSize(format, mip.m_width, mip.m_height, 0, false, numMips);
|
||||
imageContainer.m_format = format;
|
||||
output = alloc(imageContainer.m_size);
|
||||
|
||||
imageEncodeFromRgba8(output->data, rgba, mip.m_width, mip.m_height, format);
|
||||
|
||||
for (uint8_t lod = 1; lod < numMips; ++lod)
|
||||
{
|
||||
ImageMip mip1;
|
||||
imageGetRawData(imageContainer, 0, lod, output->data, output->size, mip1);
|
||||
uint8_t* data = const_cast<uint8_t*>(mip1.m_data);
|
||||
|
||||
uint32_t width = bx::uint32_max(1, mip.m_width >>lod);
|
||||
uint32_t height = bx::uint32_max(1, mip.m_height>>lod);
|
||||
imageRgba8Downsample2x2(width, height, width*4, rgba, rgba);
|
||||
imageEncodeFromRgba8(data, rgba, mip.m_width, mip.m_height, format);
|
||||
}
|
||||
}
|
||||
|
||||
BX_FREE(&allocator, rgba);
|
||||
BX_FREE(&allocator, temp);
|
||||
}
|
||||
|
||||
if (NULL != output)
|
||||
|
|
Reference in a new issue