Skip to content

Commit

Permalink
VideoCommon: remove HiResTexture DDS loading, update hirestexture log…
Browse files Browse the repository at this point in the history
…ic to use custom texture data
  • Loading branch information
iwubcode committed Mar 1, 2023
1 parent 3e35255 commit 42cb3f3
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 568 deletions.
1 change: 0 additions & 1 deletion Source/Core/DolphinLib.props
Original file line number Diff line number Diff line change
Expand Up @@ -1265,7 +1265,6 @@
<ClCompile Include="VideoCommon\GraphicsModSystem\Runtime\FBInfo.cpp" />
<ClCompile Include="VideoCommon\GraphicsModSystem\Runtime\GraphicsModActionFactory.cpp" />
<ClCompile Include="VideoCommon\GraphicsModSystem\Runtime\GraphicsModManager.cpp" />
<ClCompile Include="VideoCommon\HiresTextures_DDSLoader.cpp" />
<ClCompile Include="VideoCommon\HiresTextures.cpp" />
<ClCompile Include="VideoCommon\IndexGenerator.cpp" />
<ClCompile Include="VideoCommon\LightingShaderGen.cpp" />
Expand Down
1 change: 0 additions & 1 deletion Source/Core/VideoCommon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ add_library(videocommon
GraphicsModSystem/Runtime/GraphicsModManager.h
HiresTextures.cpp
HiresTextures.h
HiresTextures_DDSLoader.cpp
IndexGenerator.cpp
IndexGenerator.h
LightingShaderGen.cpp
Expand Down
68 changes: 18 additions & 50 deletions Source/Core/VideoCommon/HiresTextures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ void HiresTexture::Prefetch()
}
if (iter != s_textureCache.end())
{
for (const Level& l : iter->second->m_levels)
for (const VideoCommon::CustomTextureData::Level& l : iter->second->m_data.m_levels)
size_sum += l.data.size();
}
}
Expand Down Expand Up @@ -246,21 +246,6 @@ std::string HiresTexture::GenBaseName(const TextureInfo& texture_info, bool dump
return "";
}

u32 HiresTexture::CalculateMipCount(u32 width, u32 height)
{
u32 mip_width = width;
u32 mip_height = height;
u32 mip_count = 1;
while (mip_width > 1 || mip_height > 1)
{
mip_width = std::max(mip_width / 2, 1u);
mip_height = std::max(mip_height / 2, 1u);
mip_count++;
}

return mip_count;
}

std::shared_ptr<HiresTexture> HiresTexture::Search(const TextureInfo& texture_info)
{
const std::string base_filename = GenBaseName(texture_info);
Expand Down Expand Up @@ -298,10 +283,10 @@ std::unique_ptr<HiresTexture> HiresTexture::Load(const std::string& base_filenam
std::unique_ptr<HiresTexture> ret = std::unique_ptr<HiresTexture>(new HiresTexture());
const DiskTexture& first_mip_file = filename_iter->second;
ret->m_has_arbitrary_mipmaps = first_mip_file.has_arbitrary_mipmaps;
LoadDDSTexture(ret.get(), first_mip_file.path);
VideoCommon::LoadDDSTexture(&ret->m_data, first_mip_file.path);

// Load remaining mip levels, or from the start if it's not a DDS texture.
for (u32 mip_level = static_cast<u32>(ret->m_levels.size());; mip_level++)
for (u32 mip_level = static_cast<u32>(ret->m_data.m_levels.size());; mip_level++)
{
std::string filename = base_filename;
if (mip_level != 0)
Expand All @@ -313,30 +298,25 @@ std::unique_ptr<HiresTexture> HiresTexture::Load(const std::string& base_filenam

// Try loading DDS textures first, that way we maintain compression of DXT formats.
// TODO: Reduce the number of open() calls here. We could use one fd.
Level level;
if (!LoadDDSTexture(level, filename_iter->second.path, mip_level))
VideoCommon::CustomTextureData::Level level;
if (!LoadDDSTexture(&level, filename_iter->second.path, mip_level))
{
File::IOFile file;
file.Open(filename_iter->second.path, "rb");
std::vector<u8> buffer(file.GetSize());
file.ReadBytes(buffer.data(), file.GetSize());

if (!LoadTexture(level, buffer))
if (!LoadPNGTexture(&level, filename_iter->second.path))
{
ERROR_LOG_FMT(VIDEO, "Custom texture {} failed to load", filename);
break;
}
}

ret->m_levels.push_back(std::move(level));
ret->m_data.m_levels.push_back(std::move(level));
}

// If we failed to load any mip levels, we can't use this texture at all.
if (ret->m_levels.empty())
if (ret->m_data.m_levels.empty())
return nullptr;

// Verify that the aspect ratio of the texture hasn't changed, as this could have side-effects.
const Level& first_mip = ret->m_levels[0];
const VideoCommon::CustomTextureData::Level& first_mip = ret->m_data.m_levels[0];
if (first_mip.width * height != first_mip.height * width)
{
ERROR_LOG_FMT(VIDEO,
Expand All @@ -357,14 +337,14 @@ std::unique_ptr<HiresTexture> HiresTexture::Load(const std::string& base_filenam
// Verify that each mip level is the correct size (divide by 2 each time).
u32 current_mip_width = first_mip.width;
u32 current_mip_height = first_mip.height;
for (u32 mip_level = 1; mip_level < static_cast<u32>(ret->m_levels.size()); mip_level++)
for (u32 mip_level = 1; mip_level < static_cast<u32>(ret->m_data.m_levels.size()); mip_level++)
{
if (current_mip_width != 1 || current_mip_height != 1)
{
current_mip_width = std::max(current_mip_width / 2, 1u);
current_mip_height = std::max(current_mip_height / 2, 1u);

const Level& level = ret->m_levels[mip_level];
const VideoCommon::CustomTextureData::Level& level = ret->m_data.m_levels[mip_level];
if (current_mip_width == level.width && current_mip_height == level.height)
continue;

Expand All @@ -381,13 +361,15 @@ std::unique_ptr<HiresTexture> HiresTexture::Load(const std::string& base_filenam
}

// Drop this mip level and any others after it.
while (ret->m_levels.size() > mip_level)
ret->m_levels.pop_back();
while (ret->m_data.m_levels.size() > mip_level)
ret->m_data.m_levels.pop_back();
}

// All levels have to have the same format.
if (std::any_of(ret->m_levels.begin(), ret->m_levels.end(),
[&ret](const Level& l) { return l.format != ret->m_levels[0].format; }))
if (std::any_of(ret->m_data.m_levels.begin(), ret->m_data.m_levels.end(),
[&ret](const VideoCommon::CustomTextureData::Level& l) {
return l.format != ret->m_data.m_levels[0].format;
}))
{
ERROR_LOG_FMT(VIDEO, "Custom texture {} has inconsistent formats across mip levels.",
first_mip_file.path);
Expand All @@ -398,20 +380,6 @@ std::unique_ptr<HiresTexture> HiresTexture::Load(const std::string& base_filenam
return ret;
}

bool HiresTexture::LoadTexture(Level& level, const std::vector<u8>& buffer)
{
if (!Common::LoadPNG(buffer, &level.data, &level.width, &level.height))
return false;

if (level.data.empty())
return false;

// Loaded PNG images are converted to RGBA.
level.format = AbstractTextureFormat::RGBA8;
level.row_length = level.width;
return true;
}

std::set<std::string> GetTextureDirectoriesWithGameId(const std::string& root_directory,
const std::string& game_id)
{
Expand Down Expand Up @@ -464,7 +432,7 @@ HiresTexture::~HiresTexture()

AbstractTextureFormat HiresTexture::GetFormat() const
{
return m_levels.at(0).format;
return m_data.m_levels.at(0).format;
}

bool HiresTexture::HasArbitraryMipmaps() const
Expand Down
19 changes: 5 additions & 14 deletions Source/Core/VideoCommon/HiresTextures.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <vector>

#include "Common/CommonTypes.h"
#include "VideoCommon/GraphicsModSystem/Runtime/CustomTextureData.h"
#include "VideoCommon/TextureConfig.h"
#include "VideoCommon/TextureInfo.h"

Expand All @@ -29,31 +30,21 @@ class HiresTexture

static std::string GenBaseName(const TextureInfo& texture_info, bool dump = false);

static u32 CalculateMipCount(u32 width, u32 height);

~HiresTexture();

AbstractTextureFormat GetFormat() const;
bool HasArbitraryMipmaps() const;

struct Level
{
std::vector<u8> data;
AbstractTextureFormat format = AbstractTextureFormat::RGBA8;
u32 width = 0;
u32 height = 0;
u32 row_length = 0;
};
std::vector<Level> m_levels;
VideoCommon::CustomTextureData& GetData() { return m_data; }
const VideoCommon::CustomTextureData& GetData() const { return m_data; }

private:
static std::unique_ptr<HiresTexture> Load(const std::string& base_filename, u32 width,
u32 height);
static bool LoadDDSTexture(HiresTexture* tex, const std::string& filename);
static bool LoadDDSTexture(Level& level, const std::string& filename, u32 mip_level);
static bool LoadTexture(Level& level, const std::vector<u8>& buffer);
static void Prefetch();

HiresTexture() = default;

VideoCommon::CustomTextureData m_data;
bool m_has_arbitrary_mipmaps = false;
};

0 comments on commit 42cb3f3

Please sign in to comment.