Fix a crash when creating a texture with more mip-map levels than D3D9 allows

This commit is contained in:
Luke Usher 2018-10-28 22:29:41 +00:00
parent 97c0975bb4
commit 2e056347e9
1 changed files with 22 additions and 0 deletions

View File

@ -4941,6 +4941,28 @@ void CreateHostResource(XTL::X_D3DResource *pResource, DWORD D3DUsage, int iText
// Interpret Width/Height/BPP
CxbxGetPixelContainerMeasures(pPixelContainer, 0, &dwWidth, &dwHeight, &dwDepth, &dwRowPitch, &dwSlicePitch);
// Each mip-map level is 1/2 the size of the previous level
// D3D9 forbids creation of a texture with more mip-map levels than it is divisible
// EG: A 256x256 texture cannot have more than 8 levels, since that would create a texture smaller than 1x1
// Because of this, we need to cap dwMipMapLevels when required
if (dwMipMapLevels > 0) {
// Calculate how many mip-map levels it takes to get to a texture of 1 pixels in either dimension
UINT highestMipMapLevel = 0;
UINT width = dwWidth; UINT height = dwHeight;
while (width > 1 && height > 1) {
width /= 2;
height /= 2;
highestMipMapLevel++;
}
// If the desired mip-map level was higher than the maximum possible, cap it
// Test case: Shin Megami Tensei: Nine
if (dwMipMapLevels > highestMipMapLevel) {
LOG_TEST_CASE("Too many mip-map levels");
dwMipMapLevels = highestMipMapLevel;
}
}
if (dwDepth != 1) {
LOG_TEST_CASE("CreateHostResource : Depth != 1");
}