Skip to content

Commit

Permalink
Fix DXT/DDS texture bug for OpenGL
Browse files Browse the repository at this point in the history
  • Loading branch information
mogemimi committed Oct 4, 2019
1 parent e02438c commit 39493f0
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions src/RenderSystem.GL4/Texture2DGL4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,32 +127,48 @@ void SetPixelDataTexture2DCompressedGL4(
|| format == SurfaceFormat::BlockComp3_UNorm);

auto const internalFormat = ToInternalFormatGL4(format);
auto const bytesPerBlock = SurfaceFormatHelper::ToBytesPerBlock(format);
std::size_t startOffset = 0;

GLsizei mipMapPixelWidth = pixelWidth;
GLsizei mipMapPixelHeight = pixelHeight;
const auto blockSize = [&format]() -> GLint {
switch (format) {
case SurfaceFormat::BlockComp1_UNorm:
return 8;
case SurfaceFormat::BlockComp2_UNorm:
return 16;
case SurfaceFormat::BlockComp3_UNorm:
return 16;
default:
break;
}
return 8;
}();

for (GLint mipmapLevel = 0; mipmapLevel < levelCount; ++mipmapLevel) {
POMDOG_ASSERT(mipMapPixelWidth > 0);
POMDOG_ASSERT(mipMapPixelHeight > 0);
std::size_t startOffset = 0;

GLsizei const strideBytesPerMipmap = MipmapImageDataBytes(
mipMapPixelWidth, mipMapPixelHeight, bytesPerBlock);
GLsizei mipmapWidth = pixelWidth;
GLsizei mipmapHeight = pixelHeight;

glCompressedTexSubImage2D(GL_TEXTURE_2D,
mipmapLevel, 0, 0,
mipMapPixelWidth,
mipMapPixelHeight,
for (GLint mipmapLevel = 0; mipmapLevel < levelCount; ++mipmapLevel) {
POMDOG_ASSERT(mipmapWidth > 0);
POMDOG_ASSERT(mipmapHeight > 0);

const GLsizei strideBytesPerMipmap = ((mipmapWidth + 3) / 4) * ((mipmapHeight + 3) / 4) * blockSize;

glCompressedTexSubImage2D(
GL_TEXTURE_2D,
mipmapLevel,
0,
0,
mipmapWidth,
mipmapHeight,
internalFormat,
strideBytesPerMipmap,
reinterpret_cast<const std::uint8_t*>(pixelData) + startOffset);
POMDOG_CHECK_ERROR_GL4("glCompressedTexSubImage2D");

startOffset += strideBytesPerMipmap;

mipMapPixelWidth = std::max((mipMapPixelWidth >> 1), 1);
mipMapPixelHeight = std::max((mipMapPixelHeight >> 1), 1);
mipmapWidth = std::max((mipmapWidth >> 1), 1);
mipmapHeight = std::max((mipmapHeight >> 1), 1);
}
}

Expand Down

0 comments on commit 39493f0

Please sign in to comment.