Skip to content

Commit

Permalink
Reimplement get_offset to account for mipmaps with sizes that are not…
Browse files Browse the repository at this point in the history
… aligned to the format's block size. Fixes #6. (#7)
  • Loading branch information
DarioSamo authored Aug 7, 2024
1 parent 06f1d3f commit 21ca0c4
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions ddspp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1005,32 +1005,38 @@ namespace ddspp

if (desc.type == Texture3D)
{
for (unsigned int m = 0; m < mip; ++m)
for (unsigned int m = 0; m <= mip; ++m)
{
unsigned long long mipSize = mip0Size >> 2 * m;
offset += mipSize * desc.numMips;
unsigned int mipWidth = (desc.width >> m) > 1 ? (desc.width >> m) : 1;
unsigned int mipHeight = (desc.height >> m) > 1 ? (desc.height >> m) : 1;
unsigned int mipBlocksWidth = (mipWidth + desc.blockWidth - 1) / desc.blockWidth;
unsigned int mipBlocksHeight = (mipHeight + desc.blockHeight - 1) / desc.blockHeight;
unsigned long long mipSize = mipBlocksWidth * mipBlocksHeight * desc.bitsPerPixelOrBlock;
offset += mipSize * ((m == mip) ? slice : desc.numMips);
}

unsigned long long lastMip = mip0Size >> 2 * mip;

offset += lastMip * slice;
}
else
{
unsigned long long mipChainSize = 0;

for (unsigned int m = 0; m < desc.numMips; ++m)
{
unsigned long long mipSize = mip0Size >> 2 * m; // Divide by 2 in width and height
mipChainSize += mipSize > desc.bitsPerPixelOrBlock ? mipSize : desc.bitsPerPixelOrBlock;
unsigned int mipWidth = (desc.width >> m) > 1 ? (desc.width >> m) : 1;
unsigned int mipHeight = (desc.height >> m) > 1 ? (desc.height >> m) : 1;
unsigned int mipBlocksWidth = (mipWidth + desc.blockWidth - 1) / desc.blockWidth;
unsigned int mipBlocksHeight = (mipHeight + desc.blockHeight - 1) / desc.blockHeight;
mipChainSize += mipBlocksWidth * mipBlocksHeight * desc.bitsPerPixelOrBlock;
}

offset += mipChainSize * slice;

for (unsigned int m = 0; m < mip; ++m)
{
unsigned long long mipSize = mip0Size >> 2 * m; // Divide by 2 in width and height
offset += mipSize > desc.bitsPerPixelOrBlock ? mipSize : desc.bitsPerPixelOrBlock;
unsigned int mipWidth = (desc.width >> m) > 1 ? (desc.width >> m) : 1;
unsigned int mipHeight = (desc.height >> m) > 1 ? (desc.height >> m) : 1;
unsigned int mipBlocksWidth = (mipWidth + desc.blockWidth - 1) / desc.blockWidth;
unsigned int mipBlocksHeight = (mipHeight + desc.blockHeight - 1) / desc.blockHeight;
offset += mipBlocksWidth * mipBlocksHeight * desc.bitsPerPixelOrBlock;
}
}

Expand Down

0 comments on commit 21ca0c4

Please sign in to comment.