2013-04-17 21:09:55 -06:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2009-07-05 20:10:26 -06:00
|
|
|
|
2014-02-17 03:18:15 -07:00
|
|
|
#include <algorithm>
|
2015-01-21 15:06:18 -07:00
|
|
|
#include <cinttypes>
|
2009-07-05 20:10:26 -06:00
|
|
|
#include <cstring>
|
2014-03-12 13:33:41 -06:00
|
|
|
#include <string>
|
2009-07-05 20:10:26 -06:00
|
|
|
#include <utility>
|
2015-01-21 13:45:14 -07:00
|
|
|
#include <xxhash.h>
|
2010-03-09 15:17:33 -07:00
|
|
|
#include <SOIL/SOIL.h>
|
2014-02-17 03:18:15 -07:00
|
|
|
|
|
|
|
#include "Common/CommonPaths.h"
|
|
|
|
#include "Common/FileSearch.h"
|
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
|
2014-12-22 04:53:03 -07:00
|
|
|
#include "Core/ConfigManager.h"
|
2009-07-05 20:10:26 -06:00
|
|
|
|
2014-12-22 04:53:03 -07:00
|
|
|
#include "VideoCommon/HiresTextures.h"
|
2015-01-14 15:19:10 -07:00
|
|
|
#include "VideoCommon/OnScreenDisplay.h"
|
2014-12-22 04:53:03 -07:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2009-07-05 20:10:26 -06:00
|
|
|
|
2015-01-14 13:53:05 -07:00
|
|
|
static std::unordered_map<std::string, std::string> s_textureMap;
|
|
|
|
static bool s_check_native_format;
|
|
|
|
static bool s_check_new_format;
|
|
|
|
|
|
|
|
static const std::string s_format_prefix = "tex1_";
|
|
|
|
|
2014-12-22 04:53:03 -07:00
|
|
|
void HiresTexture::Init(const std::string& gameCode)
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
2015-01-14 13:53:05 -07:00
|
|
|
s_textureMap.clear();
|
|
|
|
s_check_native_format = false;
|
|
|
|
s_check_new_format = false;
|
2011-02-05 03:08:06 -07:00
|
|
|
|
2009-07-05 20:10:26 -06:00
|
|
|
CFileSearch::XStringVector Directories;
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
std::string szDir = StringFromFormat("%s%s", File::GetUserPath(D_HIRESTEXTURES_IDX).c_str(), gameCode.c_str());
|
|
|
|
Directories.push_back(szDir);
|
2009-07-05 20:10:26 -06:00
|
|
|
|
|
|
|
for (u32 i = 0; i < Directories.size(); i++)
|
|
|
|
{
|
|
|
|
File::FSTEntry FST_Temp;
|
2011-02-28 20:06:14 -07:00
|
|
|
File::ScanDirectoryTree(Directories[i], FST_Temp);
|
2013-10-28 23:09:01 -06:00
|
|
|
for (auto& entry : FST_Temp.children)
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
2013-10-28 23:09:01 -06:00
|
|
|
if (entry.isDirectory)
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
|
|
|
bool duplicate = false;
|
2014-03-12 13:33:41 -06:00
|
|
|
|
2013-10-28 23:09:01 -06:00
|
|
|
for (auto& Directory : Directories)
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
2014-03-12 13:33:41 -06:00
|
|
|
if (Directory == entry.physicalName)
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
|
|
|
duplicate = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-03-12 13:33:41 -06:00
|
|
|
|
2009-07-05 20:10:26 -06:00
|
|
|
if (!duplicate)
|
2014-03-12 13:33:41 -06:00
|
|
|
Directories.push_back(entry.physicalName);
|
2009-07-05 20:10:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
CFileSearch::XStringVector Extensions = {
|
|
|
|
"*.png",
|
|
|
|
"*.bmp",
|
|
|
|
"*.tga",
|
|
|
|
"*.dds",
|
|
|
|
"*.jpg" // Why not? Could be useful for large photo-like textures
|
|
|
|
};
|
2009-07-05 20:10:26 -06:00
|
|
|
|
|
|
|
CFileSearch FileSearch(Extensions, Directories);
|
|
|
|
const CFileSearch::XStringVector& rFilenames = FileSearch.GetFileNames();
|
2014-03-12 13:33:41 -06:00
|
|
|
|
|
|
|
const std::string code = StringFromFormat("%s_", gameCode.c_str());
|
2015-01-14 13:53:05 -07:00
|
|
|
const std::string code2 = "";
|
2009-07-05 20:10:26 -06:00
|
|
|
|
2015-01-14 13:53:05 -07:00
|
|
|
for (auto& rFilename : rFilenames)
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
2015-01-14 13:53:05 -07:00
|
|
|
std::string FileName;
|
|
|
|
SplitPath(rFilename, nullptr, &FileName, nullptr);
|
|
|
|
|
|
|
|
if (FileName.substr(0, code.length()) == code)
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
2015-01-14 13:53:05 -07:00
|
|
|
s_textureMap[FileName] = rFilename;
|
|
|
|
s_check_native_format = true;
|
|
|
|
}
|
2009-07-05 20:10:26 -06:00
|
|
|
|
2015-01-14 13:53:05 -07:00
|
|
|
if (FileName.substr(0, s_format_prefix.length()) == s_format_prefix)
|
|
|
|
{
|
|
|
|
s_textureMap[FileName] = rFilename;
|
|
|
|
s_check_new_format = true;
|
2009-07-05 20:10:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-15 13:33:22 -07:00
|
|
|
std::string HiresTexture::GenBaseName(const u8* texture, size_t texture_size, const u8* tlut, size_t tlut_size, u32 width, u32 height, int format, bool has_mipmaps, bool dump)
|
2012-05-12 06:31:38 -06:00
|
|
|
{
|
2015-01-14 15:19:10 -07:00
|
|
|
std::string name = "";
|
|
|
|
bool convert = false;
|
2015-01-14 13:53:05 -07:00
|
|
|
if (!dump && s_check_native_format)
|
2015-01-13 16:14:53 -07:00
|
|
|
{
|
2015-01-14 13:53:05 -07:00
|
|
|
// try to load the old format first
|
|
|
|
u64 tex_hash = GetHashHiresTexture(texture, (int)texture_size, g_ActiveConfig.iSafeTextureCache_ColorSamples);
|
|
|
|
u64 tlut_hash = tlut_size ? GetHashHiresTexture(tlut, (int)tlut_size, g_ActiveConfig.iSafeTextureCache_ColorSamples) : 0;
|
2015-01-14 15:19:10 -07:00
|
|
|
name = StringFromFormat("%s_%08x_%i", SConfig::GetInstance().m_LocalCoreStartupParameter.m_strUniqueID.c_str(), (u32)(tex_hash ^ tlut_hash), (u16)format);
|
2015-01-14 13:53:05 -07:00
|
|
|
if (s_textureMap.find(name) != s_textureMap.end())
|
2015-01-14 15:19:10 -07:00
|
|
|
{
|
|
|
|
if (g_ActiveConfig.bConvertHiresTextures)
|
|
|
|
convert = true;
|
|
|
|
else
|
|
|
|
return name;
|
|
|
|
}
|
2015-01-13 16:14:53 -07:00
|
|
|
}
|
2015-01-14 13:53:05 -07:00
|
|
|
|
2015-01-14 15:19:10 -07:00
|
|
|
if (dump || s_check_new_format || convert)
|
2015-01-13 16:14:53 -07:00
|
|
|
{
|
2015-01-14 13:53:05 -07:00
|
|
|
// checking for min/max on paletted textures
|
|
|
|
u32 min = 0xffff;
|
|
|
|
u32 max = 0;
|
|
|
|
switch(tlut_size)
|
|
|
|
{
|
|
|
|
case 0: break;
|
|
|
|
case 16 * 2:
|
|
|
|
for (size_t i = 0; i < texture_size; i++)
|
|
|
|
{
|
|
|
|
min = std::min<u32>(min, texture[i] & 0xf);
|
|
|
|
min = std::min<u32>(min, texture[i] >> 4);
|
|
|
|
max = std::max<u32>(max, texture[i] & 0xf);
|
|
|
|
max = std::max<u32>(max, texture[i] >> 4);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 256 * 2:
|
|
|
|
for (size_t i = 0; i < texture_size; i++)
|
|
|
|
{
|
|
|
|
min = std::min<u32>(min, texture[i]);
|
|
|
|
max = std::max<u32>(max, texture[i]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 16384 * 2:
|
|
|
|
for (size_t i = 0; i < texture_size/2; i++)
|
|
|
|
{
|
|
|
|
min = std::min<u32>(min, Common::swap16(((u16*)texture)[i]) & 0x3fff);
|
|
|
|
max = std::max<u32>(max, Common::swap16(((u16*)texture)[i]) & 0x3fff);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (tlut_size > 0)
|
|
|
|
{
|
|
|
|
tlut_size = 2 * (max + 1 - min);
|
|
|
|
tlut += 2 * min;
|
|
|
|
}
|
|
|
|
|
2015-01-21 13:45:14 -07:00
|
|
|
u64 tex_hash = XXH64(texture, texture_size, 0);
|
|
|
|
u64 tlut_hash = tlut_size ? XXH64(tlut, tlut_size, 0) : 0;
|
2015-01-13 16:14:53 -07:00
|
|
|
|
2015-01-21 15:06:18 -07:00
|
|
|
std::string basename = s_format_prefix + StringFromFormat("%dx%d%s_%016" PRIx64, width, height, has_mipmaps ? "_m" : "", tex_hash);
|
|
|
|
std::string tlutname = tlut_size ? StringFromFormat("_%016" PRIx64, tlut_hash) : "";
|
2015-01-14 13:53:05 -07:00
|
|
|
std::string formatname = StringFromFormat("_%d", format);
|
2015-01-14 15:19:10 -07:00
|
|
|
std::string fullname = basename + tlutname + formatname;
|
|
|
|
|
|
|
|
for (int level = 0; level < 10 && convert; level++)
|
|
|
|
{
|
|
|
|
std::string oldname = name;
|
|
|
|
if (level)
|
|
|
|
oldname += StringFromFormat("_mip%d", level);
|
|
|
|
|
|
|
|
// skip not existing levels
|
|
|
|
if (s_textureMap.find(oldname) == s_textureMap.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (int i = 0;; i++)
|
|
|
|
{
|
|
|
|
// for hash collisions, padd with an integer
|
|
|
|
std::string newname = fullname;
|
|
|
|
if (level)
|
|
|
|
newname += StringFromFormat("_mip%d", level);
|
|
|
|
if (i)
|
|
|
|
newname += StringFromFormat(".%d", i);
|
|
|
|
|
|
|
|
// new texture
|
|
|
|
if (s_textureMap.find(newname) == s_textureMap.end())
|
|
|
|
{
|
|
|
|
std::string src = s_textureMap[oldname];
|
|
|
|
size_t postfix = src.find_last_of('.');
|
|
|
|
std::string dst = src.substr(0, postfix - oldname.length()) + newname + src.substr(postfix, src.length() - postfix);
|
|
|
|
if (File::Rename(src, dst))
|
|
|
|
{
|
|
|
|
s_textureMap.erase(oldname);
|
|
|
|
s_textureMap[newname] = dst;
|
|
|
|
s_check_new_format = true;
|
|
|
|
OSD::AddMessage(StringFromFormat("Rename custom texture %s to %s", oldname.c_str(), newname.c_str()), 5000);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "rename failed");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// dst fail already exist, compare content
|
|
|
|
std::string a, b;
|
|
|
|
File::ReadFileToString(s_textureMap[oldname], a);
|
|
|
|
File::ReadFileToString(s_textureMap[newname], b);
|
|
|
|
|
|
|
|
if (a == b && a != "")
|
|
|
|
{
|
|
|
|
// equal, so remove
|
|
|
|
if (File::Delete(s_textureMap[oldname]))
|
|
|
|
{
|
|
|
|
s_textureMap.erase(oldname);
|
|
|
|
OSD::AddMessage(StringFromFormat("Delete double old custom texture %s", oldname.c_str()), 5000);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "delete failed");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// else continue in this loop with the next higher padding variable
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-14 13:25:33 -07:00
|
|
|
|
2015-01-14 13:53:05 -07:00
|
|
|
// try to match a wildcard template
|
|
|
|
if (!dump && s_textureMap.find(basename + "_*" + formatname) != s_textureMap.end())
|
|
|
|
return basename + "_*" + formatname;
|
2015-01-14 13:25:33 -07:00
|
|
|
|
2015-01-14 13:53:05 -07:00
|
|
|
// else generate the complete texture
|
2015-01-14 15:19:10 -07:00
|
|
|
if (dump || s_textureMap.find(fullname) != s_textureMap.end())
|
|
|
|
return fullname;
|
2015-01-14 13:53:05 -07:00
|
|
|
}
|
2015-01-14 13:25:33 -07:00
|
|
|
|
2015-01-14 15:19:10 -07:00
|
|
|
return name;
|
2014-12-22 14:33:38 -07:00
|
|
|
}
|
|
|
|
|
2015-01-15 13:33:22 -07:00
|
|
|
HiresTexture* HiresTexture::Search(const u8* texture, size_t texture_size, const u8* tlut, size_t tlut_size, u32 width, u32 height, int format, bool has_mipmaps)
|
2014-12-22 14:33:38 -07:00
|
|
|
{
|
2015-01-15 13:33:22 -07:00
|
|
|
std::string base_filename = GenBaseName(texture, texture_size, tlut, tlut_size, width, height, format, has_mipmaps);
|
2012-05-12 06:31:38 -06:00
|
|
|
|
2014-12-22 04:53:03 -07:00
|
|
|
HiresTexture* ret = nullptr;
|
|
|
|
for (int level = 0;; level++)
|
|
|
|
{
|
|
|
|
std::string filename = base_filename;
|
|
|
|
if (level)
|
|
|
|
{
|
|
|
|
filename += StringFromFormat("_mip%u", level);
|
|
|
|
}
|
2009-07-05 20:10:26 -06:00
|
|
|
|
2015-01-14 13:53:05 -07:00
|
|
|
if (s_textureMap.find(filename) != s_textureMap.end())
|
2014-12-22 04:53:03 -07:00
|
|
|
{
|
|
|
|
Level l;
|
2009-07-05 20:10:26 -06:00
|
|
|
|
2014-12-22 04:53:03 -07:00
|
|
|
File::IOFile file;
|
2015-01-14 13:53:05 -07:00
|
|
|
file.Open(s_textureMap[filename], "rb");
|
2014-12-22 04:53:03 -07:00
|
|
|
std::vector<u8> buffer(file.GetSize());
|
|
|
|
file.ReadBytes(buffer.data(), file.GetSize());
|
2014-09-22 10:13:29 -06:00
|
|
|
|
2014-12-22 04:53:03 -07:00
|
|
|
int channels;
|
|
|
|
l.data = SOIL_load_image_from_memory(buffer.data(), (int)buffer.size(), (int*)&l.width, (int*)&l.height, &channels, SOIL_LOAD_RGBA);
|
|
|
|
l.data_size = (size_t)l.width * l.height * 4;
|
2014-09-22 10:13:29 -06:00
|
|
|
|
2014-12-22 04:53:03 -07:00
|
|
|
if (l.data == nullptr)
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "Custom texture %s failed to load", filename.c_str());
|
|
|
|
break;
|
|
|
|
}
|
2009-07-05 20:10:26 -06:00
|
|
|
|
2014-12-22 04:53:03 -07:00
|
|
|
if (!level)
|
|
|
|
{
|
|
|
|
if (l.width * height != l.height * width)
|
|
|
|
ERROR_LOG(VIDEO, "Invalid custom texture size %dx%d for texture %s. The aspect differs from the native size %dx%d.",
|
|
|
|
l.width, l.height, filename.c_str(), width, height);
|
|
|
|
if (l.width % width || l.height % height)
|
|
|
|
WARN_LOG(VIDEO, "Invalid custom texture size %dx%d for texture %s. Please use an integer upscaling factor based on the native size %dx%d.",
|
|
|
|
l.width, l.height, filename.c_str(), width, height);
|
|
|
|
width = l.width;
|
|
|
|
height = l.height;
|
|
|
|
}
|
|
|
|
else if (width != l.width || height != l.height)
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "Invalid custom texture size %dx%d for texture %s. This mipmap layer _must_ be %dx%d.",
|
|
|
|
l.width, l.height, filename.c_str(), width, height);
|
|
|
|
SOIL_free_image_data(l.data);
|
|
|
|
break;
|
|
|
|
}
|
2009-07-05 20:10:26 -06:00
|
|
|
|
2014-12-22 04:53:03 -07:00
|
|
|
// calculate the size of the next mipmap
|
|
|
|
width >>= 1;
|
|
|
|
height >>= 1;
|
2009-07-05 20:10:26 -06:00
|
|
|
|
2014-12-22 04:53:03 -07:00
|
|
|
if (!ret)
|
|
|
|
ret = new HiresTexture();
|
|
|
|
ret->m_levels.push_back(l);
|
|
|
|
}
|
|
|
|
else
|
2009-07-05 20:10:26 -06:00
|
|
|
{
|
2014-12-22 04:53:03 -07:00
|
|
|
break;
|
2009-07-05 20:10:26 -06:00
|
|
|
}
|
|
|
|
}
|
2014-12-22 04:53:03 -07:00
|
|
|
|
|
|
|
return ret;
|
2009-07-05 20:10:26 -06:00
|
|
|
}
|
|
|
|
|
2014-12-22 04:53:03 -07:00
|
|
|
HiresTexture::~HiresTexture()
|
|
|
|
{
|
|
|
|
for (auto& l : m_levels)
|
|
|
|
{
|
|
|
|
SOIL_free_image_data(l.data);
|
|
|
|
}
|
2009-07-05 20:10:26 -06:00
|
|
|
}
|
2014-12-22 04:53:03 -07:00
|
|
|
|