Skip to content

Commit

Permalink
Support DXT1/3/5 (BC1,2,3) in D3D9
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Mar 12, 2023
1 parent f2673c8 commit b44e879
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 35 deletions.
1 change: 1 addition & 0 deletions Common/GPU/D3D9/thin3d_d3d9.cpp
Expand Up @@ -1592,6 +1592,7 @@ uint32_t D3D9Context::GetDataFormatSupport(DataFormat fmt) const {
case DataFormat::BC1_RGBA_UNORM_BLOCK:
case DataFormat::BC2_UNORM_BLOCK:
case DataFormat::BC3_UNORM_BLOCK:
// DXT1, DXT3, DXT5.
return FMT_TEXTURE;
default:
return 0;
Expand Down
8 changes: 4 additions & 4 deletions GPU/Common/ReplacedTexture.cpp
Expand Up @@ -297,6 +297,10 @@ bool ReplacedTexture::LoadLevelData(ReplacedTextureLevel &level, int mipLevel, D
level.w = header.dwWidth;
level.h = header.dwHeight;
numMips = header.dwMipMapCount;

if (numMips > 1) {
WARN_LOG(G3D, "DDS file contains more than one mip level. Ignoring for now.");
}
} else if (imageType == ReplacedImageType::ZIM) {
uint32_t ignore = 0;
struct ZimHeader {
Expand Down Expand Up @@ -326,10 +330,6 @@ bool ReplacedTexture::LoadLevelData(ReplacedTextureLevel &level, int mipLevel, D
ERROR_LOG(G3D, "Could not load texture replacement info: %s - unsupported format %s", level.file.ToVisualString().c_str(), magic.c_str());
}

if (numMips > 1) {
WARN_LOG(G3D, "File contains more than one mip level. Ignoring for now.");
}

// Already populated from cache. TODO: Move this above the first read, and take level.w/h from the cache.
if (!out.empty()) {
*pixelFormat = levelData_->fmt;
Expand Down
32 changes: 13 additions & 19 deletions GPU/D3D11/TextureCacheD3D11.cpp
Expand Up @@ -58,17 +58,11 @@ static const D3D11_INPUT_ELEMENT_DESC g_QuadVertexElements[] = {

static Draw::DataFormat FromD3D11Format(u32 fmt) {
switch (fmt) {
case DXGI_FORMAT_B4G4R4A4_UNORM:
return Draw::DataFormat::A4R4G4B4_UNORM_PACK16;
case DXGI_FORMAT_B5G5R5A1_UNORM:
return Draw::DataFormat::A1R5G5B5_UNORM_PACK16;
case DXGI_FORMAT_B5G6R5_UNORM:
return Draw::DataFormat::R5G6B5_UNORM_PACK16;
case DXGI_FORMAT_R8_UNORM:
return Draw::DataFormat::R8_UNORM;
case DXGI_FORMAT_B8G8R8A8_UNORM:
default:
return Draw::DataFormat::R8G8B8A8_UNORM;
case DXGI_FORMAT_B4G4R4A4_UNORM: return Draw::DataFormat::A4R4G4B4_UNORM_PACK16;
case DXGI_FORMAT_B5G5R5A1_UNORM: return Draw::DataFormat::A1R5G5B5_UNORM_PACK16;
case DXGI_FORMAT_B5G6R5_UNORM: return Draw::DataFormat::R5G6B5_UNORM_PACK16;
case DXGI_FORMAT_R8_UNORM: return Draw::DataFormat::R8_UNORM;
case DXGI_FORMAT_B8G8R8A8_UNORM: default: return Draw::DataFormat::R8G8B8A8_UNORM;
}
}

Expand Down Expand Up @@ -281,14 +275,6 @@ void TextureCacheD3D11::BuildTexture(TexCacheEntry *const entry) {
ID3D11Resource *texture = DxTex(entry);
_assert_(texture == nullptr);

int tw;
int th;
plan.GetMipSize(0, &tw, &th);
if (tw > 16384)
tw = 16384;
if (th > 16384)
th = 16384;

// The PSP only supports 8 mip levels, but we support 12 in the texture replacer (4k textures down to 1).
D3D11_SUBRESOURCE_DATA subresData[12]{};

Expand Down Expand Up @@ -355,6 +341,14 @@ void TextureCacheD3D11::BuildTexture(TexCacheEntry *const entry) {
LoadTextureLevel(*entry, data, stride, plan, srcLevel, texFmt, TexDecodeFlags{});
}

int tw;
int th;
plan.GetMipSize(0, &tw, &th);
if (tw > 16384)
tw = 16384;
if (th > 16384)
th = 16384;

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
22 changes: 10 additions & 12 deletions GPU/Directx9/TextureCacheDX9.cpp
Expand Up @@ -40,23 +40,21 @@

Draw::DataFormat FromD3D9Format(u32 fmt) {
switch (fmt) {
case D3DFMT_A4R4G4B4:
return Draw::DataFormat::B4G4R4A4_UNORM_PACK16;
case D3DFMT_A1R5G5B5:
return Draw::DataFormat::A1R5G5B5_UNORM_PACK16;
case D3DFMT_R5G6B5:
return Draw::DataFormat::R5G6B5_UNORM_PACK16;
case D3DFMT_A8:
return Draw::DataFormat::R8_UNORM;
case D3DFMT_A8R8G8B8:
default:
return Draw::DataFormat::R8G8B8A8_UNORM;
case D3DFMT_A4R4G4B4: return Draw::DataFormat::B4G4R4A4_UNORM_PACK16;
case D3DFMT_A1R5G5B5: return Draw::DataFormat::A1R5G5B5_UNORM_PACK16;
case D3DFMT_R5G6B5: return Draw::DataFormat::R5G6B5_UNORM_PACK16;
case D3DFMT_A8: return Draw::DataFormat::R8_UNORM;
case D3DFMT_A8R8G8B8: default: return Draw::DataFormat::R8G8B8A8_UNORM;
}
}

D3DFORMAT ToD3D9Format(Draw::DataFormat fmt) {
switch (fmt) {
case Draw::DataFormat::R8G8B8A8_UNORM: default: return D3DFMT_A8R8G8B8;
case Draw::DataFormat::BC1_RGBA_UNORM_BLOCK: return D3DFMT_DXT1;
case Draw::DataFormat::BC2_UNORM_BLOCK: return D3DFMT_DXT3;
case Draw::DataFormat::BC3_UNORM_BLOCK: return D3DFMT_DXT5;
case Draw::DataFormat::R8G8B8A8_UNORM: return D3DFMT_A8R8G8B8;
default: _dbg_assert_(false); return D3DFMT_A8R8G8B8;
}
}

Expand Down

0 comments on commit b44e879

Please sign in to comment.