Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
TextureCache: Fix D3D backends crashing when a game uses multiple 1x1…
…-sized LODs.
  • Loading branch information
neobrain committed Feb 18, 2013
1 parent d0ea94a commit 19ab5bf
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 3 deletions.
11 changes: 8 additions & 3 deletions Source/Core/VideoCommon/Src/TextureCacheBase.cpp
Expand Up @@ -318,7 +318,7 @@ static TextureCache::TCacheEntryBase* ReturnEntry(unsigned int stage, TextureCac

TextureCache::TCacheEntryBase* TextureCache::Load(unsigned int const stage,
u32 const address, unsigned int width, unsigned int height, int const texformat,
unsigned int const tlutaddr, int const tlutfmt, bool const use_mipmaps, unsigned int const maxlevel, bool const from_tmem)
unsigned int const tlutaddr, int const tlutfmt, bool const use_mipmaps, unsigned int maxlevel, bool const from_tmem)
{
if (0 == address)
return NULL;
Expand All @@ -345,7 +345,7 @@ TextureCache::TCacheEntryBase* TextureCache::Load(unsigned int const stage,
full_format = texformat | (tlutfmt << 16);

const u32 texture_size = TexDecoder_GetTextureSizeInBytes(expandedWidth, expandedHeight, texformat);

const u8* src_data;
if (from_tmem)
src_data = &texMem[bpmem.tex[stage / 4].texImage1[stage % 4].tmem_even * TMEM_LINE_SIZE];
Expand All @@ -372,6 +372,11 @@ TextureCache::TCacheEntryBase* TextureCache::Load(unsigned int const stage,
tex_hash ^= tlut_hash;
}

// D3D doesn't like when the specified mipmap count would require more than one 1x1-sized LOD in the mipmap chain
// e.g. 64x64 with 7 LODs would have the mipmap chain 64x64,32x32,16x16,8x8,4x4,2x2,1x1,1x1, so we limit the mipmap count to 6 there
while (g_ActiveConfig.backend_info.bUseMinimalMipCount && max(expandedWidth, expandedHeight) >> maxlevel == 0)
--maxlevel;

TCacheEntryBase *entry = textures[texID];
if (entry)
{
Expand Down Expand Up @@ -456,7 +461,7 @@ TextureCache::TCacheEntryBase* TextureCache::Load(unsigned int const stage,
const bool using_custom_lods = using_custom_texture && CheckForCustomTextureLODs(tex_hash, texformat, texLevels);
// Only load native mips if their dimensions fit to our virtual texture dimensions
const bool use_native_mips = use_mipmaps && !using_custom_lods && (width == nativeW && height == nativeH);
texLevels = (use_native_mips || using_custom_lods) ? texLevels : 1;
texLevels = (use_native_mips || using_custom_lods) ? texLevels : 1; // TODO: Should be forced to 1 for non-pow2 textures (e.g. efb copies with automatically adjusted IR)

// create the entry/texture
if (NULL == entry)
Expand Down
1 change: 1 addition & 0 deletions Source/Core/VideoCommon/Src/VideoConfig.cpp
Expand Up @@ -42,6 +42,7 @@ VideoConfig::VideoConfig()
// disable all features by default
backend_info.APIType = API_NONE;
backend_info.bUseRGBATextures = false;
backend_info.bUseMinimalMipCount = false;
backend_info.bSupports3DVision = false;
}

Expand Down
1 change: 1 addition & 0 deletions Source/Core/VideoCommon/Src/VideoConfig.h
Expand Up @@ -157,6 +157,7 @@ struct VideoConfig
std::vector<std::string> PPShaders; // post-processing shaders

bool bUseRGBATextures; // used for D3D11 in TextureCache
bool bUseMinimalMipCount;
bool bSupports3DVision;
bool bSupportsDualSourceBlend; // only supported by D3D11 and OpenGL
bool bSupportsFormatReinterpretation;
Expand Down
1 change: 1 addition & 0 deletions Source/Plugins/Plugin_VideoDX11/Src/main.cpp
Expand Up @@ -90,6 +90,7 @@ void InitBackendInfo()

g_Config.backend_info.APIType = API_D3D11;
g_Config.backend_info.bUseRGBATextures = true; // the GX formats barely match any D3D11 formats
g_Config.backend_info.bUseMinimalMipCount = true;
g_Config.backend_info.bSupports3DVision = false;
g_Config.backend_info.bSupportsDualSourceBlend = true;
g_Config.backend_info.bSupportsFormatReinterpretation = true;
Expand Down
1 change: 1 addition & 0 deletions Source/Plugins/Plugin_VideoDX9/Src/main.cpp
Expand Up @@ -93,6 +93,7 @@ void InitBackendInfo()
const int maxConstants = (shaderModel < 3) ? 32 : ((shaderModel < 4) ? 224 : 65536);
g_Config.backend_info.APIType = shaderModel < 3 ? API_D3D9_SM20 :API_D3D9_SM30;
g_Config.backend_info.bUseRGBATextures = false;
g_Config.backend_info.bUseMinimalMipCount = true;
g_Config.backend_info.bSupports3DVision = true;
g_Config.backend_info.bSupportsDualSourceBlend = false;
g_Config.backend_info.bSupportsFormatReinterpretation = true;
Expand Down
1 change: 1 addition & 0 deletions Source/Plugins/Plugin_VideoOGL/Src/main.cpp
Expand Up @@ -130,6 +130,7 @@ void InitBackendInfo()
{
g_Config.backend_info.APIType = API_OPENGL;
g_Config.backend_info.bUseRGBATextures = false;
g_Config.backend_info.bUseMinimalMipCount = false;
g_Config.backend_info.bSupports3DVision = false;
g_Config.backend_info.bSupportsDualSourceBlend = false; // supported, but broken
g_Config.backend_info.bSupportsFormatReinterpretation = false;
Expand Down

0 comments on commit 19ab5bf

Please sign in to comment.