Added support for LATC/3DC texture compression.

This commit is contained in:
bkaradzic 2013-01-20 12:47:29 -08:00
parent 6eed7bdccb
commit c26f8f4718
15 changed files with 154 additions and 74 deletions

View file

@ -70,7 +70,8 @@ void main()
normalize(v_normal)
);
vec3 normal = normalize(texture2D(u_texNormal, v_texcoord0).xyz * 2.0 - 1.0);
vec3 normal = texture2D(u_texNormal, v_texcoord0).xy * 2.0 - 1.0;
normal.z = sqrt(1.0 - dot(normal.xy, normal.xy) );
vec3 view = -normalize(v_view);
vec3 lightColor;

Binary file not shown.

Before

Width:  |  Height:  |  Size: 222 KiB

View file

@ -296,9 +296,11 @@ namespace bgfx
{
enum Enum
{
Dxt1,
Dxt3,
Dxt5,
BC1, // DXT1
BC2, // DXT3
BC3, // DXT5
BC4, // LATC1/ATI1
BC5, // LATC2/ATI2
Unknown,
L8,
BGRX8,

View file

@ -909,6 +909,8 @@ namespace bgfx
4, // Dxt1
4, // Dxt3
4, // Dxt5
4, // BC4
4, // BC5
0, // Unknown
8, // L8
32, // BGRX8

View file

@ -18,6 +18,8 @@ namespace bgfx
#define DDS_DXT3 BX_MAKEFOURCC('D', 'X', 'T', '3')
#define DDS_DXT4 BX_MAKEFOURCC('D', 'X', 'T', '4')
#define DDS_DXT5 BX_MAKEFOURCC('D', 'X', 'T', '5')
#define DDS_ATI1 BX_MAKEFOURCC('A', 'T', 'I', '1')
#define DDS_ATI2 BX_MAKEFOURCC('A', 'T', 'I', '2')
#define D3DFMT_A16B16G16R16F 113
@ -165,7 +167,7 @@ void decodeBlockDxt1(uint8_t _dst[16*4], const uint8_t _src[8])
void decodeBlockDxt23A(uint8_t _dst[16*4], const uint8_t _src[8])
{
for (uint32_t ii = 3, next = 0; ii < 16*4; ii += 4, next += 4)
for (uint32_t ii = 0, next = 0; ii < 16*4; ii += 4, next += 4)
{
uint32_t c0 = (_src[next>>3] >> (next&7) ) & 0xf;
_dst[ii] = bitRangeConvert(c0, 4, 8);
@ -197,20 +199,18 @@ void decodeBlockDxt45A(uint8_t _dst[16*4], const uint8_t _src[8])
alpha[7] = 255;
}
for (uint32_t ii = 3, next = 8*2; ii < 16*4; ii += 4, ++next)
uint32_t idx0 = _src[2];
uint32_t idx1 = _src[5];
idx0 |= uint32_t(_src[3])<<8;
idx1 |= uint32_t(_src[6])<<8;
idx0 |= uint32_t(_src[4])<<16;
idx1 |= uint32_t(_src[7])<<16;
for (uint32_t ii = 0; ii < 8*4; ii += 4)
{
uint32_t bit = (_src[next>>3] >> (next&7) ) & 1;
uint32_t idx = bit;
++next;
bit = (_src[next>>3] >> (next&7) ) & 1;
idx += bit << 1;
++next;
bit = (_src[next>>3] >> (next&7) ) & 1;
idx += bit << 2;
_dst[ii] = alpha[idx & 7];
_dst[ii] = alpha[idx0&7];
_dst[ii+32] = alpha[idx1&7];
idx0 >>= 3;
idx1 >>= 3;
}
}
@ -233,7 +233,7 @@ void Mip::decode(uint8_t* _dst)
switch (m_type)
{
case TextureFormat::Dxt1:
case TextureFormat::BC1:
for (uint32_t yy = 0; yy < height; ++yy)
{
for (uint32_t xx = 0; xx < width; ++xx)
@ -242,46 +242,91 @@ void Mip::decode(uint8_t* _dst)
src += 8;
uint8_t* dst = &_dst[(yy*pitch+xx*4)*4];
memcpy(dst, temp, 16);
memcpy(&dst[pitch], &temp[16], 16);
memcpy(&dst[0*pitch], &temp[ 0], 16);
memcpy(&dst[1*pitch], &temp[16], 16);
memcpy(&dst[2*pitch], &temp[32], 16);
memcpy(&dst[3*pitch], &temp[48], 16);
}
}
break;
case TextureFormat::Dxt3:
case TextureFormat::BC2:
for (uint32_t yy = 0; yy < height; ++yy)
{
for (uint32_t xx = 0; xx < width; ++xx)
{
decodeBlockDxt23A(temp, src);
decodeBlockDxt23A(temp+3, src);
src += 8;
decodeBlockDxt(temp, src);
src += 8;
uint8_t* dst = &_dst[(yy*pitch+xx*4)*4];
memcpy(dst, temp, 16);
memcpy(&dst[pitch], &temp[16], 16);
memcpy(&dst[0*pitch], &temp[ 0], 16);
memcpy(&dst[1*pitch], &temp[16], 16);
memcpy(&dst[2*pitch], &temp[32], 16);
memcpy(&dst[3*pitch], &temp[48], 16);
}
}
break;
case TextureFormat::Dxt5:
case TextureFormat::BC3:
for (uint32_t yy = 0; yy < height; ++yy)
{
for (uint32_t xx = 0; xx < width; ++xx)
{
decodeBlockDxt45A(temp+3, src);
src += 8;
decodeBlockDxt(temp, src);
src += 8;
uint8_t* dst = &_dst[(yy*pitch+xx*4)*4];
memcpy(&dst[0*pitch], &temp[ 0], 16);
memcpy(&dst[1*pitch], &temp[16], 16);
memcpy(&dst[2*pitch], &temp[32], 16);
memcpy(&dst[3*pitch], &temp[48], 16);
}
}
break;
case TextureFormat::BC4:
for (uint32_t yy = 0; yy < height; ++yy)
{
for (uint32_t xx = 0; xx < width; ++xx)
{
decodeBlockDxt45A(temp, src);
src += 8;
decodeBlockDxt(temp, src);
src += 8;
uint8_t* dst = &_dst[(yy*pitch+xx*4)*4];
memcpy(dst, temp, 16);
memcpy(&dst[pitch], &temp[16], 16);
memcpy(&dst[0*pitch], &temp[ 0], 16);
memcpy(&dst[1*pitch], &temp[16], 16);
memcpy(&dst[2*pitch], &temp[32], 16);
memcpy(&dst[3*pitch], &temp[48], 16);
}
}
break;
case TextureFormat::BC5:
for (uint32_t yy = 0; yy < height; ++yy)
{
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;
float ny = temp[ii*4+1]*2.0f/255.0f - 1.0f;
float nz = sqrtf(1.0f - nx*nx - ny*ny);
temp[ii*4+0] = uint8_t( (nz + 1.0f)*255.0f/2.0f);
temp[ii*4+3] = 0;
}
uint8_t* dst = &_dst[(yy*pitch+xx*4)*4];
memcpy(&dst[0*pitch], &temp[ 0], 16);
memcpy(&dst[1*pitch], &temp[16], 16);
memcpy(&dst[2*pitch], &temp[32], 16);
memcpy(&dst[3*pitch], &temp[48], 16);
}
@ -417,21 +462,33 @@ bool parseDds(Dds& _dds, const Memory* _mem)
switch (fourcc)
{
case DDS_DXT1:
type = TextureFormat::Dxt1;
type = TextureFormat::BC1;
blockSize = 8;
bpp = 4;
break;
case DDS_DXT2:
case DDS_DXT3:
type = TextureFormat::Dxt3;
type = TextureFormat::BC2;
blockSize = 16;
bpp = 4;
break;
case DDS_DXT4:
case DDS_DXT5:
type = TextureFormat::Dxt5;
type = TextureFormat::BC3;
blockSize = 16;
bpp = 4;
break;
case DDS_ATI1:
type = TextureFormat::BC4;
blockSize = 16;
bpp = 4;
break;
case DDS_ATI2:
type = TextureFormat::BC5;
blockSize = 16;
bpp = 4;
break;

View file

@ -152,6 +152,8 @@ namespace bgfx
{ DXGI_FORMAT_BC1_UNORM, 4 },
{ DXGI_FORMAT_BC2_UNORM, 4 },
{ DXGI_FORMAT_BC3_UNORM, 4 },
{ DXGI_FORMAT_BC4_UNORM, 4 },
{ DXGI_FORMAT_BC5_UNORM, 4 },
{ DXGI_FORMAT_UNKNOWN, 0 },
{ DXGI_FORMAT_R8_UNORM, 8 },
{ DXGI_FORMAT_B8G8R8A8_UNORM, 32 },

View file

@ -167,6 +167,8 @@ namespace bgfx
{ D3DFMT_DXT1, 4 },
{ D3DFMT_DXT3, 4 },
{ D3DFMT_DXT5, 4 },
{ D3DFMT_ATI1, 4 },
{ D3DFMT_ATI2, 4 },
{ D3DFMT_UNKNOWN, 0 },
{ D3DFMT_L8, 8 },
{ D3DFMT_X8R8G8B8, 32 },
@ -357,7 +359,7 @@ namespace bgfx
if (m_amd
&& s_extendedFormats[ExtendedFormat::Inst].m_supported)
{
// ATi only
// AMD only
m_device->SetRenderState(D3DRS_POINTSIZE, D3DFMT_INST);
}
@ -1360,23 +1362,27 @@ namespace bgfx
m_format = dds.m_type;
const TextureFormatInfo& tfi = s_textureFormat[dds.m_type];
bool decompress = false;
bool decompress = false
|| (TextureFormat::BC4 == dds.m_type && !s_extendedFormats[ExtendedFormat::Ati1].m_supported)
|| (TextureFormat::BC5 == dds.m_type && !s_extendedFormats[ExtendedFormat::Ati2].m_supported)
;
D3DFORMAT format = decompress ? D3DFMT_A8R8G8B8 : tfi.m_fmt;
uint8_t bpp = decompress ? 32 : tfi.m_bpp;
if (dds.m_cubeMap)
{
createCubeTexture(dds.m_width, dds.m_numMips, tfi.m_fmt);
createCubeTexture(dds.m_width, dds.m_numMips, format);
}
else if (dds.m_depth > 1)
{
createVolumeTexture(dds.m_width, dds.m_height, dds.m_depth, dds.m_numMips, tfi.m_fmt);
createVolumeTexture(dds.m_width, dds.m_height, dds.m_depth, dds.m_numMips, format);
}
else
{
createTexture(dds.m_width, dds.m_height, dds.m_numMips, tfi.m_fmt);
createTexture(dds.m_width, dds.m_height, dds.m_numMips, format);
}
uint8_t bpp = tfi.m_bpp;
if (decompress
|| TextureFormat::Unknown < dds.m_type)
{

View file

@ -22,6 +22,7 @@ namespace bgfx
EXT_texture_compression_dxt1,
CHROMIUM_texture_compression_dxt3,
CHROMIUM_texture_compression_dxt5,
EXT_texture_compression_latc,
ARB_texture_float,
OES_texture_float,
OES_texture_float_linear,
@ -70,6 +71,7 @@ namespace bgfx
{ "GL_EXT_texture_compression_dxt1", false, true },
{ "GL_CHROMIUM_texture_compression_dxt3", false, true },
{ "GL_CHROMIUM_texture_compression_dxt5", false, true },
{ "GL_EXT_texture_compression_latc", false, true },
{ "GL_ARB_texture_float", false, true },
{ "GL_OES_texture_float", false, true },
{ "GL_OES_texture_float_linear", false, true },
@ -152,7 +154,6 @@ namespace bgfx
: m_capture(NULL)
, m_captureSize(0)
, m_maxAnisotropy(0.0f)
, m_dxtSupport(false)
, m_vaoSupport(false)
, m_programBinarySupport(false)
, m_textureSwizzleSupport(false)
@ -328,7 +329,6 @@ namespace bgfx
void* m_capture;
uint32_t m_captureSize;
float m_maxAnisotropy;
bool m_dxtSupport;
bool m_vaoSupport;
bool m_programBinarySupport;
bool m_textureSwizzleSupport;
@ -521,26 +521,29 @@ namespace bgfx
GLenum m_fmt;
GLenum m_type;
uint8_t m_bpp;
bool m_supported;
};
static TextureFormatInfo s_textureFormat[TextureFormat::Count] =
{
{ GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_ZERO, 4 },
{ GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_ZERO, 4 },
{ GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_ZERO, 4 },
{ GL_ZERO, GL_ZERO, GL_ZERO, 0 },
{ GL_LUMINANCE, GL_LUMINANCE, GL_UNSIGNED_BYTE, 8 },
{ GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, 32 },
{ GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, 32 },
{ GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_ZERO, 4, false },
{ GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_ZERO, 4, false },
{ GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_ZERO, 4, false },
{ GL_COMPRESSED_LUMINANCE_LATC1_EXT, GL_COMPRESSED_LUMINANCE_LATC1_EXT, GL_ZERO, 4, false },
{ GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, GL_ZERO, 4, false },
{ GL_ZERO, GL_ZERO, GL_ZERO, 0, true },
{ GL_LUMINANCE, GL_LUMINANCE, GL_UNSIGNED_BYTE, 8, true },
{ GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, 32, true },
{ GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, 32, true },
#if BGFX_CONFIG_RENDERER_OPENGL
{ GL_RGBA16, GL_RGBA, GL_HALF_FLOAT, 64 },
{ GL_RGBA16, GL_RGBA, GL_HALF_FLOAT, 64, true },
#else
{ GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, 64 },
{ GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, 64, true },
#endif // BGFX_CONFIG_RENDERER_OPENGL
{ GL_RGB565, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 16 },
{ GL_RGBA4, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, 16 },
{ GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, 16 },
{ GL_RGB10_A2, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, 32 },
{ GL_RGB565, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 16, true },
{ GL_RGBA4, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, 16, true },
{ GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, 16, true },
{ GL_RGB10_A2, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, 32, true },
};
const char* glslTypeName(GLuint _type)
@ -1019,7 +1022,7 @@ namespace bgfx
target = GL_TEXTURE_CUBE_MAP_POSITIVE_X;
}
if (!s_renderCtx.m_dxtSupport
if (!tfi.m_supported
|| TextureFormat::Unknown < dds.m_type)
{
bool decompress = TextureFormat::Unknown > dds.m_type;
@ -1790,15 +1793,14 @@ namespace bgfx
}
}
s_renderCtx.m_dxtSupport = true
&& s_extension[Extension::EXT_texture_compression_dxt1].m_supported
&& s_extension[Extension::CHROMIUM_texture_compression_dxt3].m_supported
&& s_extension[Extension::CHROMIUM_texture_compression_dxt5].m_supported
;
bool dxtSupported = s_extension[Extension::EXT_texture_compression_s3tc].m_supported;
s_textureFormat[TextureFormat::BC1].m_supported = dxtSupported || s_extension[Extension::EXT_texture_compression_dxt1].m_supported;
s_textureFormat[TextureFormat::BC2].m_supported = dxtSupported || s_extension[Extension::CHROMIUM_texture_compression_dxt3].m_supported;
s_textureFormat[TextureFormat::BC3].m_supported = dxtSupported || s_extension[Extension::CHROMIUM_texture_compression_dxt5].m_supported;
s_renderCtx.m_dxtSupport |=
s_extension[Extension::EXT_texture_compression_s3tc].m_supported
;
bool latcSupported = s_extension[Extension::EXT_texture_compression_latc].m_supported;
s_textureFormat[TextureFormat::BC4].m_supported = latcSupported;
s_textureFormat[TextureFormat::BC5].m_supported = latcSupported;
s_renderCtx.m_vaoSupport = !!BGFX_CONFIG_RENDERER_OPENGLES3
|| s_extension[Extension::ARB_vertex_array_object].m_supported

View file

@ -123,6 +123,14 @@
# define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
# endif // GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
# ifndef GL_COMPRESSED_LUMINANCE_LATC1_EXT
# define GL_COMPRESSED_LUMINANCE_LATC1_EXT 0x8C70
# endif // GL_COMPRESSED_LUMINANCE_LATC1_EXT
# ifndef GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT
# define GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT 0x8C72
# endif // GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT
# ifndef GL_TRANSLATED_SHADER_SOURCE_LENGTH_ANGLE
# define GL_TRANSLATED_SHADER_SOURCE_LENGTH_ANGLE 0x93A0
# endif // GL_TRANSLATED_SHADER_SOURCE_LENGTH_ANGLE