texturec: Added basic normalmap mipmap filter.

This commit is contained in:
Branimir Karadžić 2016-01-06 18:15:05 -08:00
parent 59f8e06094
commit 6c25d56551
2 changed files with 74 additions and 53 deletions

View file

@ -469,28 +469,29 @@ namespace bgfx
return; return;
} }
float* dst = (float*)_dst; const uint8_t* src = (const uint8_t*)_src;
const float* src = (const float*)_src; uint8_t* dst = (uint8_t*)_dst;
for (uint32_t yy = 0, ystep = _pitch*2; yy < dstheight; ++yy, src += ystep) for (uint32_t yy = 0, ystep = _pitch*2; yy < dstheight; ++yy, src += ystep)
{ {
const float* rgba = src; const float* rgba0 = (const float*)&src[0];
for (uint32_t xx = 0; xx < dstwidth; ++xx, rgba += 8, dst += 4) const float* rgba1 = (const float*)&src[_pitch];
for (uint32_t xx = 0; xx < dstwidth; ++xx, rgba0 += 8, rgba1 += 8, dst += 16)
{ {
float xyz[3]; float xyz[3];
xyz[0] = rgba[ 0]; xyz[0] = rgba0[0];
xyz[1] = rgba[ 1]; xyz[1] = rgba0[1];
xyz[2] = rgba[ 2]; xyz[2] = rgba0[2];
xyz[0] += rgba[ 4]; xyz[0] += rgba0[4];
xyz[1] += rgba[ 5]; xyz[1] += rgba0[5];
xyz[2] += rgba[ 6]; xyz[2] += rgba0[6];
xyz[0] += rgba[_pitch+0]; xyz[0] += rgba1[0];
xyz[1] += rgba[_pitch+1]; xyz[1] += rgba1[1];
xyz[2] += rgba[_pitch+2]; xyz[2] += rgba1[2];
xyz[0] += rgba[_pitch+4]; xyz[0] += rgba1[4];
xyz[1] += rgba[_pitch+5]; xyz[1] += rgba1[5];
xyz[2] += rgba[_pitch+6]; xyz[2] += rgba1[6];
bx::vec3Norm(dst, xyz); bx::vec3Norm( (float*)dst, xyz);
} }
} }
} }
@ -2496,36 +2497,39 @@ namespace bgfx
void imageDecodeToRgba32f(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _pitch, TextureFormat::Enum _format) void imageDecodeToRgba32f(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _pitch, TextureFormat::Enum _format)
{ {
const uint8_t* src = (const uint8_t*)_src; const uint8_t* src = (const uint8_t*)_src;
float* dst = (float*)_dst; uint8_t* dst = (uint8_t*)_dst;
uint32_t width = _width/4;
uint32_t height = _height/4;
switch (_format) switch (_format)
{ {
case TextureFormat::BC5: case TextureFormat::BC5:
for (uint32_t yy = 0; yy < height; ++yy)
{ {
for (uint32_t xx = 0; xx < width; ++xx) uint32_t width = _width/4;
uint32_t height = _height/4;
for (uint32_t yy = 0; yy < height; ++yy)
{ {
uint8_t temp[16*4]; for (uint32_t xx = 0; xx < width; ++xx)
decodeBlockDxt45A(temp+1, src);
src += 8;
decodeBlockDxt45A(temp+2, src);
src += 8;
for (uint32_t ii = 0; ii < 16; ++ii)
{ {
float nx = temp[ii*4+2]*2.0f/255.0f - 1.0f; uint8_t temp[16*4];
float ny = temp[ii*4+1]*2.0f/255.0f - 1.0f;
float nz = sqrtf(1.0f - nx*nx - ny*ny);
float* block = &dst[( (yy + ii/4)*_pitch+xx*4+ii%4)*16]; decodeBlockDxt45A(temp+1, src);
block[0] = nx; src += 8;
block[1] = ny; decodeBlockDxt45A(temp+2, src);
block[2] = nz; src += 8;
block[3] = 0.0f;
for (uint32_t ii = 0; ii < 16; ++ii)
{
float nx = temp[ii*4+2]*2.0f/255.0f - 1.0f;
float ny = temp[ii*4+1]*2.0f/255.0f - 1.0f;
float nz = sqrtf(1.0f - nx*nx - ny*ny);
const uint32_t offset = (yy + ii/4)*_pitch + (xx*4 + ii%4)*16;
float* block = (float*)&dst[offset];
block[0] = nx;
block[1] = ny;
block[2] = nz;
block[3] = 0.0f;
}
} }
} }
} }

View file

@ -331,6 +331,19 @@ int main(int _argc, const char* _argv[])
, mip.m_format , mip.m_format
); );
for (uint32_t yy = 0; yy < mip.m_height; ++yy)
{
for (uint32_t xx = 0; xx < mip.m_width; ++xx)
{
const uint32_t offset = (yy*mip.m_width + xx) * 4;
float* inout = &rgba[offset];
inout[0] = inout[0] * 2.0f/255.0f - 1.0f;
inout[1] = inout[1] * 2.0f/255.0f - 1.0f;
inout[2] = inout[2] * 2.0f/255.0f - 1.0f;
inout[3] = inout[3] * 2.0f/255.0f - 1.0f;
}
}
imageContainer.m_numMips = numMips; imageContainer.m_numMips = numMips;
imageContainer.m_size = imageGetSize(format, mip.m_width, mip.m_height, 0, false, numMips); imageContainer.m_size = imageGetSize(format, mip.m_width, mip.m_height, 0, false, numMips);
imageContainer.m_format = format; imageContainer.m_format = format;
@ -340,14 +353,12 @@ int main(int _argc, const char* _argv[])
for (uint8_t lod = 1; lod < numMips; ++lod) for (uint8_t lod = 1; lod < numMips; ++lod)
{ {
ImageMip mip1; imageRgba32fDownsample2x2NormalMap(mip.m_width, mip.m_height, mip.m_width*16, rgba, rgba);
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); ImageMip dstMip;
uint32_t height = bx::uint32_max(1, mip.m_height>>lod); imageGetRawData(imageContainer, 0, lod, output->data, output->size, dstMip);
imageRgba32fDownsample2x2NormalMap(width, height, width*16, rgba, rgba); uint8_t* data = const_cast<uint8_t*>(dstMip.m_data);
imageEncodeFromRgba32f(data, rgba, width, height, format); imageEncodeFromRgba32f(data, rgba, dstMip.m_width, dstMip.m_height, format);
} }
} }
else else
@ -356,6 +367,14 @@ int main(int _argc, const char* _argv[])
temp = BX_ALLOC(&allocator, size); temp = BX_ALLOC(&allocator, size);
uint8_t* rgba = (uint8_t*)temp; uint8_t* rgba = (uint8_t*)temp;
imageDecodeToRgba8(rgba
, mip.m_data
, mip.m_width
, mip.m_height
, mip.m_width*mip.m_bpp/8
, mip.m_format
);
imageContainer.m_numMips = numMips; imageContainer.m_numMips = numMips;
imageContainer.m_size = imageGetSize(format, mip.m_width, mip.m_height, 0, false, numMips); imageContainer.m_size = imageGetSize(format, mip.m_width, mip.m_height, 0, false, numMips);
imageContainer.m_format = format; imageContainer.m_format = format;
@ -365,14 +384,12 @@ int main(int _argc, const char* _argv[])
for (uint8_t lod = 1; lod < numMips; ++lod) for (uint8_t lod = 1; lod < numMips; ++lod)
{ {
ImageMip mip1; imageRgba8Downsample2x2(mip.m_width, mip.m_height, mip.m_width*4, rgba, rgba);
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); ImageMip dstMip;
uint32_t height = bx::uint32_max(1, mip.m_height>>lod); imageGetRawData(imageContainer, 0, lod, output->data, output->size, dstMip);
imageRgba8Downsample2x2(width, height, width*4, rgba, rgba); uint8_t* data = const_cast<uint8_t*>(dstMip.m_data);
imageEncodeFromRgba8(data, rgba, width, height, format); imageEncodeFromRgba8(data, rgba, dstMip.m_width, dstMip.m_height, format);
} }
} }