Skip to content

Commit e47ab9d

Browse files
committed
KTX ASTC Support
I removed some vestiges of ASTC support in DDS, and fixed a few math bugs regarding pixel sizes and compressed size for astc. Also, we don't try to invert ASTC KTX textures becaue they're fed to opengl correctly as long as the origin is set accurately.
1 parent 43faf6f commit e47ab9d

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

src/osg/Image.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -806,15 +806,15 @@ unsigned int Image::computePixelSizeInBits(GLenum format,GLenum type)
806806
{
807807
osg::Vec3i footprint = computeBlockFootprint(format);
808808
unsigned int pixelsPerBlock = footprint.x() * footprint.y();
809-
unsigned int bitsPerBlock = computeBlockSize(format, 0);//16 x 8 = 128
809+
unsigned int bitsPerBlock = computeBlockSize(format, 0) * 8; // Convert bytes to bits
810810
unsigned int bitsPerPixel = bitsPerBlock / pixelsPerBlock;
811811
if (bitsPerBlock == bitsPerPixel * pixelsPerBlock) {
812-
OSG_WARN << "Image::computePixelSizeInBits(format,type) : bits per pixel (" << bitsPerPixel << ") is not an integer for GL_KHR_texture_compression_astc_hdr sizes other than 4x4 and 8x8." << std::endl;
812+
// Integer division worked perfectly
813813
return bitsPerPixel;
814814
} else {
815815
OSG_WARN << "Image::computePixelSizeInBits(format,type) : bits per pixel (" << bitsPerBlock << "/" << pixelsPerBlock << ") is not an integer for GL_KHR_texture_compression_astc_hdr size" << footprint.x() << "x" << footprint.y() << "." << std::endl;
816+
return 0;
816817
}
817-
return 0;
818818
}
819819
default: break;
820820
}
@@ -1827,6 +1827,8 @@ void Image::flipVertical()
18271827

18281828
const bool dxtc(dxtc_tool::isDXTC(_pixelFormat));
18291829
const bool rgtc(dxtc_tool::isRGTC(_pixelFormat));
1830+
const bool astc(isCompressed() && (_pixelFormat >= GL_COMPRESSED_RGBA_ASTC_4x4_KHR && _pixelFormat <= GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR));
1831+
18301832
if (_mipmapData.empty())
18311833
{
18321834
// no mipmaps,
@@ -1840,9 +1842,10 @@ void Image::flipVertical()
18401842
OSG_NOTICE << "Notice Image::flipVertical(): Vertical flip do not succeed" << std::endl;
18411843
}
18421844
}
1843-
else
1845+
// ASTC textures are stored in KTX format with native OpenGL orientation - skip flipping
1846+
else if (!astc)
18441847
{
1845-
if (isCompressed()) OSG_NOTICE << "Notice Image::flipVertical(): image is compressed but normal v-flip is used" << std::endl;
1848+
if (isCompressed()) OSG_NOTICE << "Notice Image::flipVertical(): file=" << _fileName << " image is compressed but normal v-flip is used" << std::endl;
18461849
// its not a compressed image, so implement flip oursleves.
18471850
unsigned char* top = data(0,0,r);
18481851
unsigned char* bottom = top + (_t-1)*rowStep;
@@ -1887,7 +1890,8 @@ void Image::flipVertical()
18871890
OSG_NOTICE << "Notice Image::flipVertical(): Vertical flip did not succeed" << std::endl;
18881891
}
18891892
}
1890-
else
1893+
// ASTC textures are stored in KTX format with native OpenGL orientation - skip flipping
1894+
else if (!astc)
18911895
{
18921896
// it's not a compressed image, so implement flip ourselves.
18931897
unsigned int mipRowSize = computeRowWidthInBytes(s, _pixelFormat, _dataType, _packing);

src/osg/Texture.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,85 +1995,85 @@ void Texture::getCompressedSize(GLenum internalFormat, GLint width, GLint height
19951995
else if (internalFormat == GL_COMPRESSED_RGBA_ASTC_4x4_KHR || internalFormat == GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR)
19961996
{
19971997
blockSize = 16;
1998-
size = ceil(width/4.0)*ceil(height/4.0)*blockSize;
1998+
size = ((width+3)/4)*((height+3)/4)*depth*blockSize;
19991999
return;
20002000
}
20012001
else if (internalFormat == GL_COMPRESSED_RGBA_ASTC_5x4_KHR || internalFormat == GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR)
20022002
{
20032003
blockSize = 16;
2004-
size = ceil(width/5.0)*ceil(height/4.0)*blockSize;
2004+
size = ((width+4)/5)*((height+3)/4)*depth*blockSize;
20052005
return;
20062006
}
20072007
else if (internalFormat == GL_COMPRESSED_RGBA_ASTC_5x5_KHR || internalFormat == GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR)
20082008
{
20092009
blockSize = 16;
2010-
size = ceil(width/5.0)*ceil(height/5.0)*blockSize;
2010+
size = ((width+4)/5)*((height+4)/5)*depth*blockSize;
20112011
return;
20122012
}
20132013
else if (internalFormat == GL_COMPRESSED_RGBA_ASTC_6x5_KHR || internalFormat == GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR)
20142014
{
20152015
blockSize = 16;
2016-
size = ceil(width/6.0)*ceil(height/5.0)*blockSize;
2016+
size = ((width+5)/6)*((height+4)/5)*depth*blockSize;
20172017
return;
20182018
}
20192019
else if (internalFormat == GL_COMPRESSED_RGBA_ASTC_6x6_KHR || internalFormat == GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR)
20202020
{
20212021
blockSize = 16;
2022-
size = ceil(width/6.0)*ceil(height/6.0)*blockSize;
2022+
size = ((width+5)/6)*((height+5)/6)*depth*blockSize;
20232023
return;
20242024
}
20252025
else if (internalFormat == GL_COMPRESSED_RGBA_ASTC_8x5_KHR || internalFormat == GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR)
20262026
{
20272027
blockSize = 16;
2028-
size = ceil(width/8.0)*ceil(height/5.0)*blockSize;
2028+
size = ((width+7)/8)*((height+4)/5)*depth*blockSize;
20292029
return;
20302030
}
20312031
else if (internalFormat == GL_COMPRESSED_RGBA_ASTC_8x6_KHR || internalFormat == GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR)
20322032
{
20332033
blockSize = 16;
2034-
size = ceil(width/8.0)*ceil(height/6.0)*blockSize;
2034+
size = ((width+7)/8)*((height+5)/6)*depth*blockSize;
20352035
return;
20362036
}
20372037
else if (internalFormat == GL_COMPRESSED_RGBA_ASTC_8x8_KHR || internalFormat == GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR)
20382038
{
20392039
blockSize = 16;
2040-
size = ceil(width/8.0)*ceil(height/8.0)*blockSize;
2040+
size = ((width+7)/8)*((height+7)/8)*depth*blockSize;
20412041
return;
20422042
}
20432043
else if (internalFormat == GL_COMPRESSED_RGBA_ASTC_10x5_KHR || internalFormat == GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR)
20442044
{
20452045
blockSize = 16;
2046-
size = ceil(width/10.0)*ceil(height/5.0)*blockSize;
2046+
size = ((width+9)/10)*((height+4)/5)*depth*blockSize;
20472047
return;
20482048
}
20492049
else if (internalFormat == GL_COMPRESSED_RGBA_ASTC_10x6_KHR || internalFormat == GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR)
20502050
{
20512051
blockSize = 16;
2052-
size = ceil(width/10.0)*ceil(height/6.0)*blockSize;
2052+
size = ((width+9)/10)*((height+5)/6)*depth*blockSize;
20532053
return;
20542054
}
20552055
else if (internalFormat == GL_COMPRESSED_RGBA_ASTC_10x8_KHR || internalFormat == GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR)
20562056
{
20572057
blockSize = 16;
2058-
size = ceil(width/10.0)*ceil(height/8.0)*blockSize;
2058+
size = ((width+9)/10)*((height+7)/8)*depth*blockSize;
20592059
return;
20602060
}
20612061
else if (internalFormat == GL_COMPRESSED_RGBA_ASTC_10x10_KHR || internalFormat == GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR)
20622062
{
20632063
blockSize = 16;
2064-
size = ceil(width/10.0)*ceil(height/10.0)*blockSize;
2064+
size = ((width+9)/10)*((height+9)/10)*depth*blockSize;
20652065
return;
20662066
}
20672067
else if (internalFormat == GL_COMPRESSED_RGBA_ASTC_12x10_KHR || internalFormat == GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR)
20682068
{
20692069
blockSize = 16;
2070-
size = ceil(width/12.0)*ceil(height/10.0)*blockSize;
2070+
size = ((width+11)/12)*((height+9)/10)*depth*blockSize;
20712071
return;
20722072
}
20732073
else if (internalFormat == GL_COMPRESSED_RGBA_ASTC_12x12_KHR || internalFormat == GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR)
20742074
{
20752075
blockSize = 16;
2076-
size = ceil(width/12.0)*ceil(height/12.0)*blockSize;
2076+
size = ((width+11)/12)*((height+11)/12)*depth*blockSize;
20772077
return;
20782078
}
20792079
else

src/osgPlugins/ktx/ReaderWriterKTX.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ osgDB::ReaderWriter::ReadResult ReaderWriterKTX::readKTXStream(std::istream& fin
210210
header.glInternalFormat, header.glFormat,
211211
header.glType, totalImageData, osg::Image::USE_NEW_DELETE);
212212

213+
// KTX textures are stored in their original coordinate system (TOP_LEFT)
214+
image->setOrigin(osg::Image::TOP_LEFT);
215+
213216
if (header.numberOfMipmapLevels > 1)
214217
image->setMipmapLevels(mipmapData);
215218

0 commit comments

Comments
 (0)