diff --git a/CMakeLists.txt b/CMakeLists.txt
index 25505d597c..9056de3389 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -571,6 +571,8 @@ add_library(Common STATIC
Common/Data/Format/JSONReader.cpp
Common/Data/Format/JSONWriter.h
Common/Data/Format/JSONWriter.cpp
+ Common/Data/Format/DDSLoad.cpp
+ Common/Data/Format/DDSLoad.h
Common/Data/Format/PNGLoad.cpp
Common/Data/Format/PNGLoad.h
Common/Data/Format/ZIMLoad.cpp
diff --git a/Common/Common.vcxproj b/Common/Common.vcxproj
index 8557f133d3..90d8d0d872 100644
--- a/Common/Common.vcxproj
+++ b/Common/Common.vcxproj
@@ -405,6 +405,7 @@
+
@@ -844,6 +845,7 @@
+
diff --git a/Common/Common.vcxproj.filters b/Common/Common.vcxproj.filters
index 0f40093153..053702cd0a 100644
--- a/Common/Common.vcxproj.filters
+++ b/Common/Common.vcxproj.filters
@@ -470,6 +470,9 @@
File\VFS
+
+ Data\Format
+
@@ -890,6 +893,9 @@
File\VFS
+
+ Data\Format
+
diff --git a/Common/Data/Format/DDSLoad.cpp b/Common/Data/Format/DDSLoad.cpp
new file mode 100644
index 0000000000..1d3bdb4d1d
--- /dev/null
+++ b/Common/Data/Format/DDSLoad.cpp
@@ -0,0 +1,5 @@
+#include "Common/Data/Format/DDSLoad.h"
+
+bool DetectDDSParams(const DDSHeader *header, DDSLoadInfo *info) {
+ return false;
+}
diff --git a/Common/Data/Format/DDSLoad.h b/Common/Data/Format/DDSLoad.h
new file mode 100644
index 0000000000..9862854b95
--- /dev/null
+++ b/Common/Data/Format/DDSLoad.h
@@ -0,0 +1,69 @@
+#pragma once
+
+#include
+
+// DDSPixelFormat.dwFlags bits
+enum {
+ DDPF_ALPHAPIXELS = 1, // Texture contains alpha data; dwRGBAlphaBitMask contains valid data.
+ DDPF_ALPHA = 2, // Used in some older DDS files for alpha channel only uncompressed data(dwRGBBitCount contains the alpha channel bitcount; dwABitMask contains valid data)
+ DDPF_FOURCC = 4, // Texture contains compressed RGB data; dwFourCC contains valid data. 0x4
+ DDPF_RGB = 8, // Texture contains uncompressed RGB data; dwRGBBitCount and the RGB masks(dwRBitMask, dwGBitMask, dwBBitMask) contain valid data. 0x40
+ DDPF_YUV = 16, //Used in some older DDS files for YUV uncompressed data(dwRGBBitCount contains the YUV bit count; dwRBitMask contains the Y mask, dwGBitMask contains the U mask, dwBBitMask contains the V mask)
+ DDPF_LUMINANCE = 32, // Used in some older DDS files for single channel color uncompressed data (dwRGBBitCount contains the luminance channel bit count; dwRBitMask contains the channel mask). Can be combined with DDPF_ALPHAPIXELS for a two channel DDS file.
+};
+
+// dwCaps members
+enum {
+ DDSCAPS_COMPLEX = 8,
+ DDSCAPS_MIPMAP = 0x400000,
+ DDSCAPS_TEXTURE = 0x1000, // Required
+};
+
+// Not using any D3D headers here, this is cross platform and minimal.
+// Boiled down from the public documentation.
+struct DDSPixelFormat {
+ uint32_t dwSize; // must be 32
+ uint32_t dwFlags;
+ uint32_t dwFourCC;
+ uint32_t dwRGBBitCount;
+ uint32_t dwRBitMask;
+ uint32_t dwGBitMask;
+ uint32_t dwBBitMask;
+ uint32_t dwABitMask;
+};
+
+struct DDSHeader {
+ uint32_t dwMagic; // Magic is not technically part of the header struct but convenient to have here when reading the files.
+ uint32_t dwSize; // must be 124
+ uint32_t dwFlags;
+ uint32_t dwHeight;
+ uint32_t dwWidth;
+ uint32_t dwPitchOrLinearSize; // The pitch or number of bytes per scan line in an uncompressed texture; the total number of bytes in the top level texture for a compressed texture
+ uint32_t dwDepth; // we'll always use 1 here
+ uint32_t dwMipMapCount;
+ uint32_t dwReserved1[11];
+ DDSPixelFormat ddspf;
+ uint32_t dwCaps;
+ uint32_t dwCaps2; // nothing we care about
+ uint32_t dwCaps3; // unused
+ uint32_t dwCaps4; // unused
+ uint32_t dwReserved2;
+};
+
+// DDS header extension to handle resource arrays, DXGI pixel formats that don't map to the legacy Microsoft DirectDraw pixel format structures, and additional metadata.
+struct DDSHeaderDXT10 {
+ uint32_t dxgiFormat;
+ uint32_t resourceDimension; // 1d = 2, 2d = 3, 3d = 4. very intuitive
+ uint32_t miscFlag;
+ uint32_t arraySize; // we only support 1 here
+ uint32_t miscFlags2; // sets alpha interpretation, let's not bother
+};
+
+// Simple DDS parser, suitable for texture replacement packs.
+// Doesn't actually load, only does some logic to fill out DDSLoadInfo so the caller can then
+// do the actual load with a simple series of memcpys or whatever is appropriate.
+struct DDSLoadInfo {
+ uint32_t bytesToCopy;
+};
+
+bool DetectDDSParams(const DDSHeader *header, DDSLoadInfo *info);
diff --git a/Common/GPU/D3D11/thin3d_d3d11.cpp b/Common/GPU/D3D11/thin3d_d3d11.cpp
index 120f8db5ea..58d5a59397 100644
--- a/Common/GPU/D3D11/thin3d_d3d11.cpp
+++ b/Common/GPU/D3D11/thin3d_d3d11.cpp
@@ -491,7 +491,12 @@ static DXGI_FORMAT dataFormatToD3D11(DataFormat format) {
case DataFormat::D16: return DXGI_FORMAT_D16_UNORM;
case DataFormat::D32F: return DXGI_FORMAT_D32_FLOAT;
case DataFormat::D32F_S8: return DXGI_FORMAT_D32_FLOAT_S8X24_UINT;
- case DataFormat::ETC1:
+ case DataFormat::BC1_RGBA_UNORM_BLOCK: return DXGI_FORMAT_BC1_UNORM;
+ case DataFormat::BC2_UNORM_BLOCK: return DXGI_FORMAT_BC2_UNORM;
+ case DataFormat::BC3_UNORM_BLOCK: return DXGI_FORMAT_BC3_UNORM;
+ case DataFormat::BC4_UNORM_BLOCK: return DXGI_FORMAT_BC4_UNORM;
+ case DataFormat::BC5_UNORM_BLOCK: return DXGI_FORMAT_BC5_UNORM;
+ case DataFormat::BC7_UNORM_BLOCK: return DXGI_FORMAT_BC7_UNORM;
default:
return DXGI_FORMAT_UNKNOWN;
}
diff --git a/Common/GPU/D3D9/thin3d_d3d9.cpp b/Common/GPU/D3D9/thin3d_d3d9.cpp
index b130aae436..2a90648abc 100644
--- a/Common/GPU/D3D9/thin3d_d3d9.cpp
+++ b/Common/GPU/D3D9/thin3d_d3d9.cpp
@@ -125,6 +125,9 @@ D3DFORMAT FormatToD3DFMT(DataFormat fmt) {
case DataFormat::A1R5G5B5_UNORM_PACK16: return D3DFMT_A1R5G5B5;
case DataFormat::D24_S8: return D3DFMT_D24S8;
case DataFormat::D16: return D3DFMT_D16;
+ case DataFormat::BC1_RGBA_UNORM_BLOCK: return D3DFMT_DXT1;
+ case DataFormat::BC2_UNORM_BLOCK: return D3DFMT_DXT3; // DXT3 is indeed BC2.
+ case DataFormat::BC3_UNORM_BLOCK: return D3DFMT_DXT5; // DXT5 is indeed BC3
default: return D3DFMT_UNKNOWN;
}
}
diff --git a/Common/GPU/DataFormat.h b/Common/GPU/DataFormat.h
index 32463d3a16..6f5cd2bc2e 100644
--- a/Common/GPU/DataFormat.h
+++ b/Common/GPU/DataFormat.h
@@ -46,22 +46,20 @@ enum class DataFormat : uint8_t {
// Block compression formats.
// These are modern names for DXT and friends, now patent free.
// https://msdn.microsoft.com/en-us/library/bb694531.aspx
- BC1_RGBA_UNORM_BLOCK,
- BC1_RGBA_SRGB_BLOCK,
- BC2_UNORM_BLOCK, // 4-bit straight alpha + DXT1 color. Usually not worth using
- BC2_SRGB_BLOCK,
- BC3_UNORM_BLOCK, // 3-bit alpha with 2 ref values (+ magic) + DXT1 color
- BC3_SRGB_BLOCK,
- BC4_UNORM_BLOCK, // 1-channel, same storage as BC3 alpha
- BC4_SNORM_BLOCK,
- BC5_UNORM_BLOCK, // 2-channel RG, each has same storage as BC3 alpha
- BC5_SNORM_BLOCK,
- BC6H_UFLOAT_BLOCK, // TODO
- BC6H_SFLOAT_BLOCK,
- BC7_UNORM_BLOCK, // Highly advanced, very expensive to compress, very good quality.
- BC7_SRGB_BLOCK,
+ BC1_RGBA_UNORM_BLOCK, // 64 bits per 4x4 block. Used by Basis, along with ETC2_R8G8B8_UNORM_BLOCK.
+ BC2_UNORM_BLOCK, // 4-bit straight alpha + DXT1 color. 128 bits per block. Usually not worth using
+ BC3_UNORM_BLOCK, // 3-bit alpha with 2 ref values (+ magic) + DXT1 color. 128 bits per block.
+ BC4_UNORM_BLOCK, // 1-channel, same storage as BC3 alpha. 64 bits per block.
+ BC5_UNORM_BLOCK, // 2-channel RG, each has same storage as BC3 alpha. 128 bits per block.
+ BC7_UNORM_BLOCK, // Highly advanced RGBA, very expensive to compress, very good quality. 128 bits per block.
- ETC1,
+ // Ericsson texture compression.
+ ETC2_R8G8B8_UNORM_BLOCK, // Color-only, 64 bits per 4x4 block.
+ ETC2_R8G8B8A1_UNORM_BLOCK, // Color + alpha, 128 bits per 4x4 block.
+ ETC2_R8G8B8A8_UNORM_BLOCK, // Color + alpha, 128 bits per 4x4 block.
+
+ // This is the one ASTC format used by UASTC / basis Universal.
+ ASTC_4x4_UNORM_BLOCK,
S8,
D16,
@@ -76,6 +74,7 @@ bool DataFormatIsDepthStencil(DataFormat fmt);
inline bool DataFormatIsColor(DataFormat fmt) {
return !DataFormatIsDepthStencil(fmt);
}
+bool DataFormatIsBlockCompressed(DataFormat fmt, int *blockSize);
// Limited format support for now.
const char *DataFormatToString(DataFormat fmt);
diff --git a/Common/GPU/OpenGL/DataFormatGL.cpp b/Common/GPU/OpenGL/DataFormatGL.cpp
index 1b2af548d8..c53190bae2 100644
--- a/Common/GPU/OpenGL/DataFormatGL.cpp
+++ b/Common/GPU/OpenGL/DataFormatGL.cpp
@@ -86,6 +86,50 @@ bool Thin3DFormatToGLFormatAndType(DataFormat fmt, GLuint &internalFormat, GLuin
alignment = 16;
break;
+#if PPSSPP_PLATFORM(WINDOWS)
+ case DataFormat::BC1_RGBA_UNORM_BLOCK:
+ internalFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
+ format = GL_RGB;
+ type = GL_FLOAT;
+ alignment = 8;
+ break;
+
+ case DataFormat::BC7_UNORM_BLOCK:
+ internalFormat = GL_COMPRESSED_RGBA_BPTC_UNORM;
+ format = GL_RGBA;
+ type = GL_FLOAT;
+ alignment = 16;
+ break;
+#endif
+
+ case DataFormat::ETC2_R8G8B8_UNORM_BLOCK:
+ internalFormat = GL_COMPRESSED_RGB8_ETC2;
+ format = GL_RGB;
+ type = GL_FLOAT;
+ alignment = 8;
+ break;
+
+ case DataFormat::ETC2_R8G8B8A1_UNORM_BLOCK:
+ internalFormat = GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
+ format = GL_RGBA;
+ type = GL_FLOAT;
+ alignment = 16;
+ break;
+
+ case DataFormat::ETC2_R8G8B8A8_UNORM_BLOCK:
+ internalFormat = GL_COMPRESSED_RGBA8_ETC2_EAC;
+ format = GL_RGBA;
+ type = GL_FLOAT;
+ alignment = 16;
+ break;
+
+ case DataFormat::ASTC_4x4_UNORM_BLOCK:
+ internalFormat = GL_COMPRESSED_RGBA_ASTC_4x4_KHR;
+ format = GL_RGBA;
+ type = GL_FLOAT;
+ alignment = 16;
+ break;
+
default:
return false;
}
diff --git a/Common/GPU/Vulkan/thin3d_vulkan.cpp b/Common/GPU/Vulkan/thin3d_vulkan.cpp
index 2d58bebdc8..ce76c90af2 100644
--- a/Common/GPU/Vulkan/thin3d_vulkan.cpp
+++ b/Common/GPU/Vulkan/thin3d_vulkan.cpp
@@ -560,6 +560,7 @@ private:
uint8_t stencilCompareMask_ = 0xFF;
};
+// Bits per pixel, not bytes.
static int GetBpp(VkFormat format) {
switch (format) {
case VK_FORMAT_R8G8B8A8_UNORM:
@@ -582,6 +583,21 @@ static int GetBpp(VkFormat format) {
return 32;
case VK_FORMAT_D16_UNORM:
return 16;
+ case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK:
+ return 4;
+ case VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK:
+ case VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK:
+ return 8;
+ case VK_FORMAT_ASTC_4x4_UNORM_BLOCK:
+ return 8;
+ case VK_FORMAT_BC1_RGBA_UNORM_BLOCK:
+ return 4;
+ case VK_FORMAT_BC2_UNORM_BLOCK:
+ case VK_FORMAT_BC3_UNORM_BLOCK:
+ case VK_FORMAT_BC4_UNORM_BLOCK:
+ case VK_FORMAT_BC5_UNORM_BLOCK:
+ case VK_FORMAT_BC7_UNORM_BLOCK:
+ return 8;
default:
return 0;
}
@@ -625,13 +641,15 @@ static VkFormat DataFormatToVulkan(DataFormat format) {
case DataFormat::BC2_UNORM_BLOCK: return VK_FORMAT_BC2_UNORM_BLOCK;
case DataFormat::BC3_UNORM_BLOCK: return VK_FORMAT_BC3_UNORM_BLOCK;
case DataFormat::BC4_UNORM_BLOCK: return VK_FORMAT_BC4_UNORM_BLOCK;
- case DataFormat::BC4_SNORM_BLOCK: return VK_FORMAT_BC4_SNORM_BLOCK;
case DataFormat::BC5_UNORM_BLOCK: return VK_FORMAT_BC5_UNORM_BLOCK;
- case DataFormat::BC5_SNORM_BLOCK: return VK_FORMAT_BC5_SNORM_BLOCK;
- case DataFormat::BC6H_SFLOAT_BLOCK: return VK_FORMAT_BC6H_SFLOAT_BLOCK;
- case DataFormat::BC6H_UFLOAT_BLOCK: return VK_FORMAT_BC6H_UFLOAT_BLOCK;
case DataFormat::BC7_UNORM_BLOCK: return VK_FORMAT_BC7_UNORM_BLOCK;
- case DataFormat::BC7_SRGB_BLOCK: return VK_FORMAT_BC7_SRGB_BLOCK;
+
+ case DataFormat::ETC2_R8G8B8A1_UNORM_BLOCK: return VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK;
+ case DataFormat::ETC2_R8G8B8A8_UNORM_BLOCK: return VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK;
+ case DataFormat::ETC2_R8G8B8_UNORM_BLOCK: return VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK;
+
+ case DataFormat::ASTC_4x4_UNORM_BLOCK: return VK_FORMAT_ASTC_4x4_UNORM_BLOCK;
+
default:
return VK_FORMAT_UNDEFINED;
}
diff --git a/Common/GPU/thin3d.cpp b/Common/GPU/thin3d.cpp
index 83726b7391..f5dc4b452e 100644
--- a/Common/GPU/thin3d.cpp
+++ b/Common/GPU/thin3d.cpp
@@ -93,6 +93,31 @@ bool DataFormatIsDepthStencil(DataFormat fmt) {
}
}
+// We don't bother listing the formats that are irrelevant for PPSSPP, like BC6 (HDR format)
+// or weird-shaped ASTC formats. We only support 4x4 block size formats for now.
+// If you pass in a blockSize parameter, it receives byte count that a 4x4 block takes in this format.
+bool DataFormatIsBlockCompressed(DataFormat fmt, int *blockSize) {
+ switch (fmt) {
+ case DataFormat::BC1_RGBA_UNORM_BLOCK:
+ case DataFormat::BC4_UNORM_BLOCK:
+ case DataFormat::ETC2_R8G8B8_UNORM_BLOCK:
+ if (blockSize) *blockSize = 8; // 64 bits
+ return true;
+ case DataFormat::BC2_UNORM_BLOCK:
+ case DataFormat::BC3_UNORM_BLOCK:
+ case DataFormat::BC5_UNORM_BLOCK:
+ case DataFormat::BC7_UNORM_BLOCK:
+ case DataFormat::ETC2_R8G8B8A1_UNORM_BLOCK:
+ case DataFormat::ETC2_R8G8B8A8_UNORM_BLOCK:
+ case DataFormat::ASTC_4x4_UNORM_BLOCK:
+ if (blockSize) *blockSize = 16; // 128 bits
+ return true;
+ default:
+ if (blockSize) *blockSize = 0;
+ return false;
+ }
+}
+
RefCountedObject::~RefCountedObject() {
_dbg_assert_(refcount_ == 0xDEDEDE);
}
diff --git a/GPU/Common/ReplacedTexture.cpp b/GPU/Common/ReplacedTexture.cpp
index ac5e54f599..692a03465f 100644
--- a/GPU/Common/ReplacedTexture.cpp
+++ b/GPU/Common/ReplacedTexture.cpp
@@ -23,6 +23,7 @@
#include "GPU/Common/TextureReplacer.h"
#include "Common/Data/Format/IniFile.h"
+#include "Common/Data/Format/DDSLoad.h"
#include "Common/Data/Format/ZIMLoad.h"
#include "Common/Data/Format/PNGLoad.h"
#include "Common/Thread/ParallelLoop.h"
@@ -31,6 +32,35 @@
#include "Common/Log.h"
#include "Common/TimeUtil.h"
+#define MK_FOURCC(str) (str[0] | ((uint8_t)str[1] << 8) | ((uint8_t)str[2] << 16) | ((uint8_t)str[3] << 24))
+
+static ReplacedImageType IdentifyMagic(const uint8_t magic[4]) {
+ if (strncmp((const char *)magic, "ZIMG", 4) == 0)
+ return ReplacedImageType::ZIM;
+ if (magic[0] == 0x89 && strncmp((const char *)&magic[1], "PNG", 3) == 0)
+ return ReplacedImageType::PNG;
+ if (strncmp((const char *)magic, "DDS ", 4) == 0)
+ return ReplacedImageType::DDS;
+ return ReplacedImageType::INVALID;
+}
+
+static ReplacedImageType Identify(VFSBackend *vfs, VFSOpenFile *openFile, std::string *outMagic) {
+ uint8_t magic[4];
+ if (vfs->Read(openFile, magic, 4) != 4) {
+ *outMagic = "FAIL";
+ return ReplacedImageType::INVALID;
+ }
+ // Turn the signature into a readable string that we can display in an error message.
+ *outMagic = std::string((const char *)magic, 4);
+ for (int i = 0; i < outMagic->size(); i++) {
+ if ((s8)(*outMagic)[i] < 32) {
+ (*outMagic)[i] = '_';
+ }
+ }
+ vfs->Rewind(openFile);
+ return IdentifyMagic(magic);
+}
+
class ReplacedTextureTask : public Task {
public:
ReplacedTextureTask(VFSBackend *vfs, ReplacedTexture &tex, LimitedWaitable *w) : vfs_(vfs), tex_(tex), waitable_(w) {}
@@ -149,7 +179,16 @@ void ReplacedTexture::Prepare(VFSBackend *vfs) {
level.fileRef = fileRef;
- if (LoadLevelData(level, i)) {
+ Draw::DataFormat pixelFormat;
+ if (LoadLevelData(level, i, &pixelFormat)) {
+ if (i == 0) {
+ fmt = pixelFormat;
+ } else {
+ if (fmt != pixelFormat) {
+ ERROR_LOG(G3D, "Replacement mipmap %d doesn't have the same pixel format as mipmap 0. Stopping.", i);
+ break;
+ }
+ }
levels_.push_back(level);
} else {
// Otherwise, we're done loading mips (bad PNG or bad size, either way.)
@@ -167,15 +206,26 @@ void ReplacedTexture::Prepare(VFSBackend *vfs) {
return;
}
+ levelData_->fmt = fmt;
SetState(ReplacementState::ACTIVE);
if (threadWaitable_)
threadWaitable_->Notify();
}
-bool ReplacedTexture::LoadLevelData(ReplacedTextureLevel &level, int mipLevel) {
+inline uint32_t RoundUpTo4(uint32_t value) {
+ return (value + 3) & ~3;
+}
+
+bool ReplacedTexture::LoadLevelData(ReplacedTextureLevel &level, int mipLevel, Draw::DataFormat *pixelFormat) {
bool good = false;
+ if (levelData_->data.size() <= mipLevel) {
+ levelData_->data.resize(mipLevel + 1);
+ }
+
+ std::vector &out = levelData_->data[mipLevel];
+
size_t fileSize;
VFSOpenFile *openFile = vfs_->OpenFileForRead(level.fileRef, &fileSize);
if (!openFile) {
@@ -185,7 +235,63 @@ bool ReplacedTexture::LoadLevelData(ReplacedTextureLevel &level, int mipLevel) {
std::string magic;
ReplacedImageType imageType = Identify(vfs_, openFile, &magic);
- if (imageType == ReplacedImageType::ZIM) {
+ int ddsBytesToRead = 0; // Used by the DDS reader only.
+ bool ddsDX10 = false;
+ int numMips = 1;
+
+ if (imageType == ReplacedImageType::DDS) {
+ DDSHeader header;
+ DDSHeaderDXT10 header10{};
+ good = vfs_->Read(openFile, &header, sizeof(header)) == sizeof(header);
+
+ *pixelFormat = Draw::DataFormat::UNDEFINED;
+ u32 format;
+ if (good && (header.ddspf.dwFlags & DDPF_FOURCC)) {
+ char *fcc = (char *)&header.ddspf.dwFourCC;
+ INFO_LOG(G3D, "DDS fourcc: %c%c%c%c", fcc[0], fcc[1], fcc[2], fcc[3]);
+ if (header.ddspf.dwFourCC == MK_FOURCC("DX10")) {
+ ddsDX10 = true;
+ good = good && vfs_->Read(openFile, &header10, sizeof(header10)) == sizeof(header10);
+ format = header10.dxgiFormat;
+ switch (format) {
+ case 98: // DXGI_FORMAT_BC7_UNORM:
+ case 99: // DXGI_FORMAT_BC7_UNORM_SRGB:
+ ddsBytesToRead = RoundUpTo4(header.dwWidth) * RoundUpTo4(header.dwHeight); // 1 byte per pixel so this should be right.
+ *pixelFormat = Draw::DataFormat::BC7_UNORM_BLOCK;
+ break;
+ default:
+ ERROR_LOG(G3D, "DXGI pixel format %d not supported.", header10.dxgiFormat);
+ good = false;
+ }
+ } else {
+ ddsBytesToRead = header.dwPitchOrLinearSize;
+ format = header.ddspf.dwFourCC;
+ // OK, there are a number of possible formats we might have ended up with. We choose just a few
+ // to support for now.
+ switch (format) {
+ case MK_FOURCC("DXT1"):
+ *pixelFormat = Draw::DataFormat::BC1_RGBA_UNORM_BLOCK;
+ break;
+ case MK_FOURCC("DXT3"):
+ *pixelFormat = Draw::DataFormat::BC2_UNORM_BLOCK;
+ break;
+ case MK_FOURCC("DXT5"):
+ *pixelFormat = Draw::DataFormat::BC3_UNORM_BLOCK;
+ break;
+ default:
+ ERROR_LOG(G3D, "DDS pixel format not supported.");
+ good = false;
+ }
+ }
+ } else if (good) {
+ ERROR_LOG(G3D, "DDS non-fourCC format not supported.");
+ good = false;
+ }
+
+ level.w = header.dwWidth;
+ level.h = header.dwHeight;
+ numMips = header.dwMipMapCount;
+ } else if (imageType == ReplacedImageType::ZIM) {
uint32_t ignore = 0;
struct ZimHeader {
uint32_t magic;
@@ -196,7 +302,8 @@ bool ReplacedTexture::LoadLevelData(ReplacedTextureLevel &level, int mipLevel) {
good = vfs_->Read(openFile, &header, sizeof(header)) == sizeof(header);
level.w = header.w;
level.h = header.h;
- good = (header.flags & ZIM_FORMAT_MASK) == ZIM_RGBA8888;
+ good = good && (header.flags & ZIM_FORMAT_MASK) == ZIM_RGBA8888;
+ *pixelFormat = Draw::DataFormat::R8G8B8A8_UNORM;
} else if (imageType == ReplacedImageType::PNG) {
PNGHeaderPeek headerPeek;
good = vfs_->Read(openFile, &headerPeek, sizeof(headerPeek)) == sizeof(headerPeek);
@@ -208,10 +315,21 @@ bool ReplacedTexture::LoadLevelData(ReplacedTextureLevel &level, int mipLevel) {
ERROR_LOG(G3D, "Could not get PNG dimensions: %s (zip)", level.file.ToVisualString().c_str());
good = false;
}
+ *pixelFormat = Draw::DataFormat::R8G8B8A8_UNORM;
} else {
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;
+ return true;
+ }
+
// Is this really the right place to do it?
level.w = (level.w * desc_->w) / desc_->newW;
level.h = (level.h * desc_->h) / desc_->newH;
@@ -229,24 +347,26 @@ bool ReplacedTexture::LoadLevelData(ReplacedTextureLevel &level, int mipLevel) {
return false;
}
- if (levelData_->data.size() <= mipLevel) {
- levelData_->data.resize(mipLevel + 1);
- }
-
- std::vector &out = levelData_->data[mipLevel];
-
- // Already populated from cache.
- if (!out.empty()) {
- return true;
- }
-
auto cleanup = [&] {
vfs_->CloseFile(openFile);
};
vfs_->Rewind(openFile);
- if (imageType == ReplacedImageType::ZIM) {
+ if (imageType == ReplacedImageType::DDS) {
+ DDSHeader header;
+ DDSHeaderDXT10 header10{};
+ vfs_->Read(openFile, &header, sizeof(header));
+ if (ddsDX10) {
+ vfs_->Read(openFile, &header10, sizeof(header10));
+ }
+ // For compressed formats (we don't support uncompressed DDS files yet), this is supposed to be the linear size.
+ out.resize(ddsBytesToRead);
+ size_t read_bytes = vfs_->Read(openFile, &out[0], ddsBytesToRead);
+ if (read_bytes != ddsBytesToRead) {
+ WARN_LOG(G3D, "DDS: Expected %d bytes, got %d", ddsBytesToRead, read_bytes);
+ }
+ } else if (imageType == ReplacedImageType::ZIM) {
std::unique_ptr zim(new uint8_t[fileSize]);
if (!zim) {
ERROR_LOG(G3D, "Failed to allocate memory for texture replacement");
@@ -334,6 +454,8 @@ bool ReplacedTexture::LoadLevelData(ReplacedTextureLevel &level, int mipLevel) {
alphaStatus_ = ReplacedTextureAlpha(res);
}
}
+ } else {
+ WARN_LOG(G3D, "Don't know how to load this image type! %d", (int)imageType);
}
cleanup();
@@ -390,21 +512,28 @@ bool ReplacedTexture::CopyLevelTo(int level, void *out, int rowPitch) {
return false;
}
- if (rowPitch < info.w * 4) {
- ERROR_LOG(G3D, "Replacement rowPitch=%d, but w=%d (level=%d)", rowPitch, info.w * 4, level);
- return false;
- }
- _assert_msg_(data.size() == info.w * info.h * 4, "Data has wrong size");
+ if (fmt == Draw::DataFormat::R8G8B8A8_UNORM) {
+ if (rowPitch < info.w * 4) {
+ ERROR_LOG(G3D, "Replacement rowPitch=%d, but w=%d (level=%d)", rowPitch, info.w * 4, level);
+ return false;
+ }
- if (rowPitch == info.w * 4) {
- ParallelMemcpy(&g_threadManager, out, &data[0], info.w * 4 * info.h);
+ _assert_msg_(data.size() == info.w * info.h * 4, "Data has wrong size");
+
+ if (rowPitch == info.w * 4) {
+ ParallelMemcpy(&g_threadManager, out, &data[0], info.w * 4 * info.h);
+ } else {
+ const int MIN_LINES_PER_THREAD = 4;
+ ParallelRangeLoop(&g_threadManager, [&](int l, int h) {
+ for (int y = l; y < h; ++y) {
+ memcpy((uint8_t *)out + rowPitch * y, &data[0] + info.w * 4 * y, info.w * 4);
+ }
+ }, 0, info.h, MIN_LINES_PER_THREAD);
+ }
} else {
- const int MIN_LINES_PER_THREAD = 4;
- ParallelRangeLoop(&g_threadManager, [&](int l, int h) {
- for (int y = l; y < h; ++y) {
- memcpy((uint8_t *)out + rowPitch * y, &data[0] + info.w * 4 * y, info.w * 4);
- }
- }, 0, info.h, MIN_LINES_PER_THREAD);
+ // TODO: Add sanity checks here for other formats?
+ // Just gonna do a memcpy, slightly scared of the parallel ones.
+ memcpy(out, data.data(), data.size());
}
return true;
diff --git a/GPU/Common/ReplacedTexture.h b/GPU/Common/ReplacedTexture.h
index 56868d5689..264525b0c3 100644
--- a/GPU/Common/ReplacedTexture.h
+++ b/GPU/Common/ReplacedTexture.h
@@ -44,6 +44,7 @@ enum class ReplacedTextureHash {
enum class ReplacedImageType {
PNG,
ZIM,
+ DDS,
INVALID,
};
@@ -60,8 +61,6 @@ struct ReplacedTextureLevel {
VFSFileReference *fileRef = nullptr;
};
-ReplacedImageType Identify(VFSBackend *vfs, VFSOpenFile *openFile, std::string *outMagic);
-
enum class ReplacementState : uint32_t {
UNINITIALIZED,
POPULATED, // We located the texture files but have not started the thread.
@@ -88,6 +87,13 @@ struct ReplacementDesc {
ReplacedLevelsCache *cache;
};
+struct ReplacedLevelsCache {
+ Draw::DataFormat fmt = Draw::DataFormat::UNDEFINED;
+ std::mutex lock;
+ std::vector> data;
+ double lastUsed = 0.0;
+};
+
// These aren't actually all replaced, they can also represent a placeholder for a not-found
// replacement (state_ == NOT_FOUND).
struct ReplacedTexture {
@@ -112,6 +118,11 @@ struct ReplacedTexture {
*h = levels_[level].h;
}
+ int GetLevelDataSize(int level) const {
+ _dbg_assert_(State() == ReplacementState::ACTIVE);
+ return (int)levelData_->data[level].size();
+ }
+
int NumLevels() const {
_dbg_assert_(State() == ReplacementState::ACTIVE);
return (int)levels_.size();
@@ -134,7 +145,7 @@ struct ReplacedTexture {
private:
void Prepare(VFSBackend *vfs);
- bool LoadLevelData(ReplacedTextureLevel &info, int level);
+ bool LoadLevelData(ReplacedTextureLevel &info, int level, Draw::DataFormat *pixelFormat);
void PurgeIfOlder(double t);
std::vector levels_;
diff --git a/GPU/Common/TextureCacheCommon.cpp b/GPU/Common/TextureCacheCommon.cpp
index d852267f5c..dbda83389d 100644
--- a/GPU/Common/TextureCacheCommon.cpp
+++ b/GPU/Common/TextureCacheCommon.cpp
@@ -1550,6 +1550,8 @@ ReplacedTexture *TextureCacheCommon::FindReplacement(TexCacheEntry *entry, int &
// Make sure we keep polling.
entry->status |= TexCacheEntry::STATUS_TO_REPLACE;
break;
+ default:
+ break;
}
replacementTimeThisFrame_ += time_now_d() - replaceStart;
return replaced;
diff --git a/GPU/Common/TextureReplacer.cpp b/GPU/Common/TextureReplacer.cpp
index 0a3889c062..6c94b76f73 100644
--- a/GPU/Common/TextureReplacer.cpp
+++ b/GPU/Common/TextureReplacer.cpp
@@ -54,31 +54,6 @@ static const std::string NEW_TEXTURE_DIR = "new/";
static const int VERSION = 1;
static const double MAX_CACHE_SIZE = 4.0;
-static inline ReplacedImageType IdentifyMagic(const uint8_t magic[4]) {
- if (strncmp((const char *)magic, "ZIMG", 4) == 0)
- return ReplacedImageType::ZIM;
- if (magic[0] == 0x89 && strncmp((const char *)&magic[1], "PNG", 3) == 0)
- return ReplacedImageType::PNG;
- return ReplacedImageType::INVALID;
-}
-
-ReplacedImageType Identify(VFSBackend *vfs, VFSOpenFile *openFile, std::string *outMagic) {
- uint8_t magic[4];
- if (vfs->Read(openFile, magic, 4) != 4) {
- *outMagic = "FAIL";
- return ReplacedImageType::INVALID;
- }
- // Turn the signature into a readable string that we can display in an error message.
- *outMagic = std::string((const char *)magic, 4);
- for (int i = 0; i < outMagic->size(); i++) {
- if ((s8)(*outMagic)[i] < 32) {
- (*outMagic)[i] = '_';
- }
- }
- vfs->Rewind(openFile);
- return IdentifyMagic(magic);
-}
-
TextureReplacer::TextureReplacer(Draw::DrawContext *draw) {
// TODO: Check draw for supported texture formats.
}
diff --git a/GPU/Common/TextureReplacer.h b/GPU/Common/TextureReplacer.h
index ed0df878c9..4766351b2a 100644
--- a/GPU/Common/TextureReplacer.h
+++ b/GPU/Common/TextureReplacer.h
@@ -48,12 +48,6 @@ struct SavedTextureCacheData {
double lastTimeSaved = 0.0;
};
-struct ReplacedLevelsCache {
- std::mutex lock;
- std::vector> data;
- double lastUsed = 0.0;
-};
-
struct ReplacementCacheKey {
u64 cachekey;
u32 hash;
diff --git a/GPU/D3D11/TextureCacheD3D11.cpp b/GPU/D3D11/TextureCacheD3D11.cpp
index 90d1927898..1d2973ae48 100644
--- a/GPU/D3D11/TextureCacheD3D11.cpp
+++ b/GPU/D3D11/TextureCacheD3D11.cpp
@@ -56,7 +56,7 @@ static const D3D11_INPUT_ELEMENT_DESC g_QuadVertexElements[] = {
// NOTE: In the D3D backends, we flip R and B in the shaders, so while these look wrong, they're OK.
-Draw::DataFormat FromD3D11Format(u32 fmt) {
+static Draw::DataFormat FromD3D11Format(u32 fmt) {
switch (fmt) {
case DXGI_FORMAT_B4G4R4A4_UNORM:
return Draw::DataFormat::A4R4G4B4_UNORM_PACK16;
@@ -72,9 +72,16 @@ Draw::DataFormat FromD3D11Format(u32 fmt) {
}
}
-DXGI_FORMAT ToDXGIFormat(Draw::DataFormat fmt) {
+static DXGI_FORMAT ToDXGIFormat(Draw::DataFormat fmt) {
switch (fmt) {
- case Draw::DataFormat::R8G8B8A8_UNORM: default: return DXGI_FORMAT_B8G8R8A8_UNORM;
+ case Draw::DataFormat::BC1_RGBA_UNORM_BLOCK: return DXGI_FORMAT_BC1_UNORM;
+ case Draw::DataFormat::BC2_UNORM_BLOCK: return DXGI_FORMAT_BC2_UNORM;
+ case Draw::DataFormat::BC3_UNORM_BLOCK: return DXGI_FORMAT_BC3_UNORM;
+ case Draw::DataFormat::BC4_UNORM_BLOCK: return DXGI_FORMAT_BC4_UNORM;
+ case Draw::DataFormat::BC5_UNORM_BLOCK: return DXGI_FORMAT_BC5_UNORM;
+ case Draw::DataFormat::BC7_UNORM_BLOCK: return DXGI_FORMAT_BC7_UNORM;
+ case Draw::DataFormat::R8G8B8A8_UNORM: return DXGI_FORMAT_B8G8R8A8_UNORM;
+ default: _dbg_assert_(false); return DXGI_FORMAT_UNKNOWN;
}
}
diff --git a/GPU/Vulkan/TextureCacheVulkan.cpp b/GPU/Vulkan/TextureCacheVulkan.cpp
index 2ec829f511..20a8345c84 100644
--- a/GPU/Vulkan/TextureCacheVulkan.cpp
+++ b/GPU/Vulkan/TextureCacheVulkan.cpp
@@ -405,7 +405,14 @@ static Draw::DataFormat FromVulkanFormat(VkFormat fmt) {
static VkFormat ToVulkanFormat(Draw::DataFormat fmt) {
switch (fmt) {
- case Draw::DataFormat::R8G8B8A8_UNORM: default: return VULKAN_8888_FORMAT;
+ case Draw::DataFormat::BC1_RGBA_UNORM_BLOCK: return VK_FORMAT_BC1_RGBA_UNORM_BLOCK;
+ case Draw::DataFormat::BC2_UNORM_BLOCK: return VK_FORMAT_BC2_UNORM_BLOCK;
+ case Draw::DataFormat::BC3_UNORM_BLOCK: return VK_FORMAT_BC3_UNORM_BLOCK;
+ case Draw::DataFormat::BC4_UNORM_BLOCK: return VK_FORMAT_BC4_UNORM_BLOCK;
+ case Draw::DataFormat::BC5_UNORM_BLOCK: return VK_FORMAT_BC5_UNORM_BLOCK;
+ case Draw::DataFormat::BC7_UNORM_BLOCK: return VK_FORMAT_BC7_UNORM_BLOCK;
+ case Draw::DataFormat::R8G8B8A8_UNORM: return VULKAN_8888_FORMAT;
+ default: _assert_msg_(false, "Bad texture pixel format"); return VULKAN_8888_FORMAT;
}
}
@@ -442,8 +449,15 @@ void TextureCacheVulkan::BuildTexture(TexCacheEntry *const entry) {
// Any texture scaling is gonna move away from the original 16-bit format, if any.
VkFormat actualFmt = plan.scaleFactor > 1 ? VULKAN_8888_FORMAT : dstFmt;
+ bool bcFormat = false;
+ int bcAlign = 0;
if (plan.replaceValid) {
- actualFmt = ToVulkanFormat(plan.replaced->Format());
+ Draw::DataFormat fmt = plan.replaced->Format();
+ bcFormat = Draw::DataFormatIsBlockCompressed(fmt, &bcAlign);
+ actualFmt = ToVulkanFormat(fmt);
+ if (actualFmt != VULKAN_8888_FORMAT) {
+ actualFmt = actualFmt;
+ }
}
bool computeUpload = false;
@@ -570,6 +584,12 @@ void TextureCacheVulkan::BuildTexture(TexCacheEntry *const entry) {
bool dataScaled = true;
if (plan.replaceValid) {
+ int bufferRowLength = byteStride;
+ if (bcFormat) {
+ // For block compressed formats, we just set the upload size to the data size..
+ uploadSize = plan.replaced->GetLevelDataSize(plan.baseLevelSrc + i);
+ bufferRowLength = mipWidth;
+ }
// Directly load the replaced image.
data = pushBuffer->PushAligned(uploadSize, &bufferOffset, &texBuf, pushAlignment);
double replaceStart = time_now_d();
diff --git a/UWP/CommonUWP/CommonUWP.vcxproj b/UWP/CommonUWP/CommonUWP.vcxproj
index 6160d923d6..b6c7e04111 100644
--- a/UWP/CommonUWP/CommonUWP.vcxproj
+++ b/UWP/CommonUWP/CommonUWP.vcxproj
@@ -272,6 +272,7 @@
+
@@ -418,6 +419,7 @@
+
diff --git a/UWP/CommonUWP/CommonUWP.vcxproj.filters b/UWP/CommonUWP/CommonUWP.vcxproj.filters
index 40b8f80da4..63bccb6fcf 100644
--- a/UWP/CommonUWP/CommonUWP.vcxproj.filters
+++ b/UWP/CommonUWP/CommonUWP.vcxproj.filters
@@ -420,6 +420,9 @@
UI
+
+ Data\Format
+
@@ -778,6 +781,9 @@
UI
+
+ Data\Format
+
diff --git a/android/jni/Android.mk b/android/jni/Android.mk
index 57b695f3e5..6ff34b212f 100644
--- a/android/jni/Android.mk
+++ b/android/jni/Android.mk
@@ -163,6 +163,8 @@ EXEC_AND_LIB_FILES := \
$(SRC)/Common/Data/Format/IniFile.cpp \
$(SRC)/Common/Data/Format/JSONReader.cpp \
$(SRC)/Common/Data/Format/JSONWriter.cpp \
+ $(SRC)/Common/Data/Format/DDSLoad.cpp \
+ $(SRC)/Common/Data/Format/DDSLoad.h \
$(SRC)/Common/Data/Format/PNGLoad.cpp \
$(SRC)/Common/Data/Format/PNGLoad.h \
$(SRC)/Common/Data/Format/ZIMLoad.cpp \
diff --git a/libretro/Makefile.common b/libretro/Makefile.common
index 67149d94d4..3908a3bd08 100644
--- a/libretro/Makefile.common
+++ b/libretro/Makefile.common
@@ -261,6 +261,7 @@ SOURCES_CXX += \
$(COMMONDIR)/Data/Format/IniFile.cpp \
$(COMMONDIR)/Data/Format/JSONReader.cpp \
$(COMMONDIR)/Data/Format/JSONWriter.cpp \
+ $(COMMONDIR)/Data/Format/DDSLoad.cpp \
$(COMMONDIR)/Data/Format/PNGLoad.cpp \
$(COMMONDIR)/Data/Format/ZIMLoad.cpp \
$(COMMONDIR)/Data/Format/ZIMSave.cpp \