2013-04-18 03:09:55 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <algorithm>
|
2009-07-06 02:10:26 +00:00
|
|
|
#include <cstring>
|
2014-03-12 19:33:41 +00:00
|
|
|
#include <string>
|
2009-07-06 02:10:26 +00:00
|
|
|
#include <utility>
|
2010-03-09 22:17:33 +00:00
|
|
|
#include <SOIL/SOIL.h>
|
2014-02-17 10:18:15 +00:00
|
|
|
|
|
|
|
#include "Common/CommonPaths.h"
|
|
|
|
#include "Common/FileSearch.h"
|
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
|
2014-12-22 11:53:03 +00:00
|
|
|
#include "Core/ConfigManager.h"
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2014-12-22 11:53:03 +00:00
|
|
|
#include "VideoCommon/HiresTextures.h"
|
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2014-12-22 11:53:03 +00:00
|
|
|
std::unordered_map<std::string, std::string> HiresTexture::textureMap;
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2014-12-22 11:53:03 +00:00
|
|
|
void HiresTexture::Init(const std::string& gameCode)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
2011-02-05 10:08:06 +00:00
|
|
|
textureMap.clear();
|
|
|
|
|
2009-07-06 02:10:26 +00:00
|
|
|
CFileSearch::XStringVector Directories;
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2014-03-12 19:33:41 +00:00
|
|
|
std::string szDir = StringFromFormat("%s%s", File::GetUserPath(D_HIRESTEXTURES_IDX).c_str(), gameCode.c_str());
|
|
|
|
Directories.push_back(szDir);
|
2009-07-06 02:10:26 +00:00
|
|
|
|
|
|
|
for (u32 i = 0; i < Directories.size(); i++)
|
|
|
|
{
|
|
|
|
File::FSTEntry FST_Temp;
|
2011-03-01 03:06:14 +00:00
|
|
|
File::ScanDirectoryTree(Directories[i], FST_Temp);
|
2013-10-29 05:09:01 +00:00
|
|
|
for (auto& entry : FST_Temp.children)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
2013-10-29 05:09:01 +00:00
|
|
|
if (entry.isDirectory)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
|
|
|
bool duplicate = false;
|
2014-03-12 19:33:41 +00:00
|
|
|
|
2013-10-29 05:09:01 +00:00
|
|
|
for (auto& Directory : Directories)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
2014-03-12 19:33:41 +00:00
|
|
|
if (Directory == entry.physicalName)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
|
|
|
duplicate = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-03-12 19:33:41 +00:00
|
|
|
|
2009-07-06 02:10:26 +00:00
|
|
|
if (!duplicate)
|
2014-03-12 19:33:41 +00:00
|
|
|
Directories.push_back(entry.physicalName);
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-12 19:33:41 +00:00
|
|
|
CFileSearch::XStringVector Extensions = {
|
|
|
|
"*.png",
|
|
|
|
"*.bmp",
|
|
|
|
"*.tga",
|
|
|
|
"*.dds",
|
|
|
|
"*.jpg" // Why not? Could be useful for large photo-like textures
|
|
|
|
};
|
2009-07-06 02:10:26 +00:00
|
|
|
|
|
|
|
CFileSearch FileSearch(Extensions, Directories);
|
|
|
|
const CFileSearch::XStringVector& rFilenames = FileSearch.GetFileNames();
|
2014-03-12 19:33:41 +00:00
|
|
|
|
|
|
|
const std::string code = StringFromFormat("%s_", gameCode.c_str());
|
2009-07-06 02:10:26 +00:00
|
|
|
|
|
|
|
if (rFilenames.size() > 0)
|
|
|
|
{
|
2013-10-29 05:09:01 +00:00
|
|
|
for (auto& rFilename : rFilenames)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
|
|
|
std::string FileName;
|
2014-03-09 20:14:26 +00:00
|
|
|
SplitPath(rFilename, nullptr, &FileName, nullptr);
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2014-03-12 19:33:41 +00:00
|
|
|
if (FileName.substr(0, code.length()).compare(code) == 0 && textureMap.find(FileName) == textureMap.end())
|
2013-10-29 05:09:01 +00:00
|
|
|
textureMap.insert(std::map<std::string, std::string>::value_type(FileName, rFilename));
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-22 21:33:38 +00: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)
|
2012-05-12 12:31:38 +00:00
|
|
|
{
|
2014-12-22 11:53:03 +00:00
|
|
|
u64 tex_hash = GetHashHiresTexture(texture, (int)texture_size, g_ActiveConfig.iSafeTextureCache_ColorSamples);
|
|
|
|
u64 tlut_hash = 0;
|
|
|
|
u64 hash = tex_hash;
|
|
|
|
if (tlut_size)
|
|
|
|
{
|
|
|
|
tlut_hash = GetHashHiresTexture(tlut, (int)tlut_size, g_ActiveConfig.iSafeTextureCache_ColorSamples);
|
|
|
|
hash ^= tlut_hash;
|
|
|
|
}
|
2014-12-22 21:33:38 +00:00
|
|
|
return StringFromFormat("%s_%08x_%i", SConfig::GetInstance().m_LocalCoreStartupParameter.m_strUniqueID.c_str(), (u32)hash, (u16)format);
|
|
|
|
}
|
|
|
|
|
|
|
|
HiresTexture* HiresTexture::Search(const u8* texture, size_t texture_size, const u8* tlut, size_t tlut_size, u32 width, u32 height, int format)
|
|
|
|
{
|
|
|
|
std::string base_filename = GenBaseName(texture, texture_size, tlut, tlut_size, width, height, format);
|
2012-05-12 12:31:38 +00:00
|
|
|
|
2014-12-22 11:53:03 +00:00
|
|
|
HiresTexture* ret = nullptr;
|
|
|
|
for (int level = 0;; level++)
|
|
|
|
{
|
|
|
|
std::string filename = base_filename;
|
|
|
|
if (level)
|
|
|
|
{
|
|
|
|
filename += StringFromFormat("_mip%u", level);
|
|
|
|
}
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2014-12-22 11:53:03 +00:00
|
|
|
if (textureMap.find(filename) != textureMap.end())
|
|
|
|
{
|
|
|
|
Level l;
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2014-12-22 11:53:03 +00:00
|
|
|
File::IOFile file;
|
|
|
|
file.Open(textureMap[filename], "rb");
|
|
|
|
std::vector<u8> buffer(file.GetSize());
|
|
|
|
file.ReadBytes(buffer.data(), file.GetSize());
|
2014-09-22 16:13:29 +00:00
|
|
|
|
2014-12-22 11:53:03 +00: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 16:13:29 +00:00
|
|
|
|
2014-12-22 11:53:03 +00:00
|
|
|
if (l.data == nullptr)
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "Custom texture %s failed to load", filename.c_str());
|
|
|
|
break;
|
|
|
|
}
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2014-12-22 11:53:03 +00: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-06 02:10:26 +00:00
|
|
|
|
2014-12-22 11:53:03 +00:00
|
|
|
// calculate the size of the next mipmap
|
|
|
|
width >>= 1;
|
|
|
|
height >>= 1;
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2014-12-22 11:53:03 +00:00
|
|
|
if (!ret)
|
|
|
|
ret = new HiresTexture();
|
|
|
|
ret->m_levels.push_back(l);
|
|
|
|
}
|
|
|
|
else
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
2014-12-22 11:53:03 +00:00
|
|
|
break;
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-22 11:53:03 +00:00
|
|
|
|
|
|
|
return ret;
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
|
2014-12-22 11:53:03 +00:00
|
|
|
HiresTexture::~HiresTexture()
|
|
|
|
{
|
|
|
|
for (auto& l : m_levels)
|
|
|
|
{
|
|
|
|
SOIL_free_image_data(l.data);
|
|
|
|
}
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
2014-12-22 11:53:03 +00:00
|
|
|
|