Skip to content

Commit

Permalink
Merge pull request #17749 from hrydgard/ktx2-d3d11-fix
Browse files Browse the repository at this point in the history
In D3D11, force block compressed textures to have dimensions divisible by 4
  • Loading branch information
hrydgard authored Jul 20, 2023
2 parents 2985faa + ace2170 commit 52d30c0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions GPU/Common/ReplacedTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,10 @@ ReplacedTexture::LoadLevelResult ReplacedTexture::LoadLevelData(VFSFileReference
bool bc = Draw::DataFormatIsBlockCompressed(*pixelFormat, &blockSize);
_dbg_assert_(bc || *pixelFormat == Draw::DataFormat::R8G8B8A8_UNORM);

if (bc && (level.w & 3) != 0 || (level.h & 3) != 0) {
WARN_LOG(G3D, "Block compressed replacement texture '%s' not divisible by 4x4 (%dx%d). In D3D11 (only!) we will have to expand (potentially causing glitches).", filename.c_str(), level.w, level.h);
}

data_.resize(numMips);

basist::ktx2_transcoder_state transcodeState; // Each thread needs one of these.
Expand Down Expand Up @@ -546,6 +550,7 @@ ReplacedTexture::LoadLevelResult ReplacedTexture::LoadLevelData(VFSFileReference
}
transcoder.clear();
vfs_->CloseFile(openFile);

return LoadLevelResult::DONE; // don't read more levels
} else if (imageType == ReplacedImageType::DDS) {
// TODO: Do better with alphaStatus, it's possible.
Expand All @@ -562,6 +567,10 @@ ReplacedTexture::LoadLevelResult ReplacedTexture::LoadLevelData(VFSFileReference
bool bc = Draw::DataFormatIsBlockCompressed(*pixelFormat, &blockSize);
_dbg_assert_(bc);

if (bc && (level.w & 3) != 0 || (level.h & 3) != 0) {
WARN_LOG(G3D, "Block compressed replacement texture '%s' not divisible by 4x4 (%dx%d). In D3D11 (only!) we will have to expand (potentially causing glitches).", filename.c_str(), level.w, level.h);
}

data_.resize(numMips);

// A DDS File can contain multiple mipmaps.
Expand All @@ -583,6 +592,7 @@ ReplacedTexture::LoadLevelResult ReplacedTexture::LoadLevelData(VFSFileReference
level.fileRef = nullptr; // We only provide a fileref on level 0 if we have mipmaps.
}
vfs_->CloseFile(openFile);

return LoadLevelResult::DONE; // don't read more levels

} else if (imageType == ReplacedImageType::ZIM) {
Expand Down
3 changes: 3 additions & 0 deletions GPU/Common/TextureReplacer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ bool TextureReplacer::LoadIniValues(IniFile &ini, VFSBackend *dir, bool isOverri
}
}

auto gr = GetI18NCategory(I18NCat::GRAPHICS);

g_OSD.Show(OSDType::MESSAGE_SUCCESS, gr->T("Texture replacement pack activated"));
return true;
}

Expand Down
8 changes: 8 additions & 0 deletions GPU/D3D11/TextureCacheD3D11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,14 @@ void TextureCacheD3D11::BuildTexture(TexCacheEntry *const entry) {
if (th > 16384)
th = 16384;

// NOTE: For block-compressed textures, we'll force the size up to the closest 4x4. This is due to an
// unfortunate restriction in D3D11 (and early D3D12). We'll warn about it in the log to give texture pack
// authors notice to fix it.
if (plan.doReplace && Draw::DataFormatIsBlockCompressed(plan.replaced->Format(), nullptr)) {
tw = (tw + 3) & ~3;
th = (th + 3) & ~3;
}

if (plan.depth == 1) {
// We don't yet have mip generation, so clamp the number of levels to the ones we can load directly.
levels = std::min(plan.levelsToCreate, plan.levelsToLoad);
Expand Down
1 change: 1 addition & 0 deletions assets/lang/en_US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ Stereo rendering = Stereo rendering
Stretch = Stretch
Texture Filter = Texture filtering
Texture Filtering = Texture filtering
Texture replacement pack activated = Texture replacement pack activated
Texture Scaling = Texture scaling
Texture Shader = Texture shader
Turn off Hardware Tessellation - unsupported = Turn off "hardware tessellation": unsupported
Expand Down

0 comments on commit 52d30c0

Please sign in to comment.