2024-06-25 21:03:01 +02:00
|
|
|
/* Copyright (C) 2024 Wildfire Games.
|
2023-12-03 01:30:12 +01:00
|
|
|
* This file is part of 0 A.D.
|
2010-09-10 23:02:10 +02:00
|
|
|
*
|
2023-12-03 01:30:12 +01:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2010-09-10 23:02:10 +02:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2023-12-03 01:30:12 +01:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2010-09-10 23:02:10 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2023-12-03 01:30:12 +01:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2010-09-10 23:02:10 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include "TextureConverter.h"
|
|
|
|
|
|
|
|
#include "lib/allocators/shared_ptr.h"
|
2022-04-11 18:28:41 +02:00
|
|
|
#include "lib/bits.h"
|
|
|
|
#include "lib/regex.h"
|
2010-09-10 23:02:10 +02:00
|
|
|
#include "lib/tex/tex.h"
|
2022-04-11 18:28:41 +02:00
|
|
|
#include "lib/timer.h"
|
2010-09-10 23:02:10 +02:00
|
|
|
#include "maths/MD5.h"
|
|
|
|
#include "ps/CLogger.h"
|
|
|
|
#include "ps/CStr.h"
|
2011-11-04 02:35:50 +01:00
|
|
|
#include "ps/Profiler2.h"
|
2023-12-17 10:43:54 +01:00
|
|
|
#include "ps/TaskManager.h"
|
2022-04-12 19:39:05 +02:00
|
|
|
#include "ps/Util.h"
|
2010-09-10 23:02:10 +02:00
|
|
|
#include "ps/XML/Xeromyces.h"
|
|
|
|
|
2012-02-15 14:58:58 +01:00
|
|
|
#if CONFIG2_NVTT
|
|
|
|
|
2010-09-10 23:02:10 +02:00
|
|
|
#include "nvtt/nvtt.h"
|
|
|
|
|
2020-08-18 19:30:41 +02:00
|
|
|
// We assume NVTT is recent enough to support the alpha flag in the DXT1a format. If users try to use an
|
|
|
|
// old version of NVTT, the game will crash when trying to decode dds files generated by NVTT.
|
|
|
|
//
|
|
|
|
// The support was added upstream in https://github.com/castano/nvidia-texture-tools/commit/782a127071895f538c1ae49925a6e15687e3c966
|
|
|
|
// so, in theory, 2.0.7 and newer should be enough, but all 2.0.x releases define NVTT_VERSION as 200, so
|
|
|
|
// we can't distinguish them. NVTT_VERSION is 201 in all development versions of the 2.0.x era, so we also
|
|
|
|
// have to exclude that value.
|
|
|
|
#if !defined NVTT_VERSION || NVTT_VERSION == 200 || NVTT_VERSION == 201
|
|
|
|
#error Please use NVTT 2.1.0 or newer. \
|
|
|
|
If your system does not provide it, you should use the bundled version by NOT passing --with-system-nvtt to premake.
|
|
|
|
#endif
|
|
|
|
|
2023-12-17 10:43:54 +01:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
// Completely arbitrary constant - there is some main-thread cost to loading textures and the textures
|
|
|
|
// use a lot of memory, so probably should not be too high.
|
|
|
|
// Note that some results in the result queue may already be ready.
|
|
|
|
constexpr size_t MAX_QUEUE_SIZE_FOR_OPTIMAL_UTILIZATION{12};
|
|
|
|
|
2010-09-10 23:02:10 +02:00
|
|
|
/**
|
|
|
|
* Output handler to collect NVTT's output into a simplistic buffer.
|
|
|
|
*/
|
|
|
|
struct BufferOutputHandler : public nvtt::OutputHandler
|
|
|
|
{
|
|
|
|
std::vector<u8> buffer;
|
|
|
|
|
|
|
|
virtual void beginImage(int UNUSED(size), int UNUSED(width), int UNUSED(height), int UNUSED(depth), int UNUSED(face), int UNUSED(miplevel))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool writeData(const void* data, int size)
|
|
|
|
{
|
|
|
|
size_t off = buffer.size();
|
|
|
|
buffer.resize(off + size);
|
|
|
|
memcpy(&buffer[off], data, size);
|
|
|
|
return true;
|
|
|
|
}
|
2019-12-29 12:21:33 +01:00
|
|
|
|
|
|
|
virtual void endImage()
|
|
|
|
{
|
|
|
|
}
|
2010-09-10 23:02:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2023-12-17 10:43:54 +01:00
|
|
|
* Arguments to the asynchronous task.
|
2010-09-10 23:02:10 +02:00
|
|
|
*/
|
2023-12-17 10:43:54 +01:00
|
|
|
struct ConversionRequest
|
2010-09-10 23:02:10 +02:00
|
|
|
{
|
|
|
|
VfsPath dest;
|
|
|
|
CTexturePtr texture;
|
|
|
|
nvtt::InputOptions inputOptions;
|
|
|
|
nvtt::CompressionOptions compressionOptions;
|
|
|
|
nvtt::OutputOptions outputOptions;
|
|
|
|
};
|
|
|
|
|
2023-12-17 10:43:54 +01:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2010-09-10 23:02:10 +02:00
|
|
|
/**
|
2023-12-17 10:43:54 +01:00
|
|
|
* Response from the asynchronous task.
|
2010-09-10 23:02:10 +02:00
|
|
|
*/
|
|
|
|
struct CTextureConverter::ConversionResult
|
|
|
|
{
|
|
|
|
VfsPath dest;
|
|
|
|
CTexturePtr texture;
|
|
|
|
BufferOutputHandler output;
|
|
|
|
bool ret; // true if the conversion succeeded
|
|
|
|
};
|
|
|
|
|
2012-02-15 14:58:58 +01:00
|
|
|
#endif // CONFIG2_NVTT
|
|
|
|
|
2010-09-10 23:02:10 +02:00
|
|
|
void CTextureConverter::Settings::Hash(MD5& hash)
|
|
|
|
{
|
|
|
|
hash.Update((const u8*)&format, sizeof(format));
|
|
|
|
hash.Update((const u8*)&mipmap, sizeof(mipmap));
|
|
|
|
hash.Update((const u8*)&normal, sizeof(normal));
|
|
|
|
hash.Update((const u8*)&alpha, sizeof(alpha));
|
|
|
|
hash.Update((const u8*)&filter, sizeof(filter));
|
|
|
|
hash.Update((const u8*)&kaiserWidth, sizeof(kaiserWidth));
|
|
|
|
hash.Update((const u8*)&kaiserAlpha, sizeof(kaiserAlpha));
|
|
|
|
hash.Update((const u8*)&kaiserStretch, sizeof(kaiserStretch));
|
|
|
|
}
|
|
|
|
|
|
|
|
CTextureConverter::SettingsFile* CTextureConverter::LoadSettings(const VfsPath& path) const
|
|
|
|
{
|
|
|
|
CXeromyces XeroFile;
|
2015-06-07 23:56:52 +02:00
|
|
|
if (XeroFile.Load(m_VFS, path, "texture") != PSRETURN_OK)
|
2010-09-10 23:02:10 +02:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Define all the elements used in the XML file
|
|
|
|
#define EL(x) int el_##x = XeroFile.GetElementID(#x)
|
|
|
|
#define AT(x) int at_##x = XeroFile.GetAttributeID(#x)
|
|
|
|
EL(textures);
|
|
|
|
EL(file);
|
|
|
|
AT(pattern);
|
|
|
|
AT(format);
|
|
|
|
AT(mipmap);
|
|
|
|
AT(normal);
|
|
|
|
AT(alpha);
|
|
|
|
AT(filter);
|
|
|
|
AT(kaiserwidth);
|
|
|
|
AT(kaiseralpha);
|
|
|
|
AT(kaiserstretch);
|
|
|
|
#undef AT
|
|
|
|
#undef EL
|
|
|
|
|
|
|
|
XMBElement root = XeroFile.GetRoot();
|
|
|
|
|
|
|
|
if (root.GetNodeName() != el_textures)
|
|
|
|
{
|
2015-01-22 21:36:24 +01:00
|
|
|
LOGERROR("Invalid texture settings file \"%s\" (unrecognised root element)", path.string8());
|
2010-09-10 23:02:10 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-02-14 00:53:40 +01:00
|
|
|
std::unique_ptr<SettingsFile> settings = std::make_unique<SettingsFile>();
|
2010-09-10 23:02:10 +02:00
|
|
|
|
|
|
|
XERO_ITER_EL(root, child)
|
|
|
|
{
|
|
|
|
if (child.GetNodeName() == el_file)
|
|
|
|
{
|
|
|
|
Match p;
|
|
|
|
|
|
|
|
XERO_ITER_ATTR(child, attr)
|
|
|
|
{
|
|
|
|
if (attr.Name == at_pattern)
|
|
|
|
{
|
2011-02-17 21:08:20 +01:00
|
|
|
p.pattern = attr.Value.FromUTF8();
|
2010-09-10 23:02:10 +02:00
|
|
|
}
|
|
|
|
else if (attr.Name == at_format)
|
|
|
|
{
|
|
|
|
CStr v(attr.Value);
|
|
|
|
if (v == "dxt1")
|
|
|
|
p.settings.format = FMT_DXT1;
|
|
|
|
else if (v == "dxt3")
|
|
|
|
p.settings.format = FMT_DXT3;
|
|
|
|
else if (v == "dxt5")
|
|
|
|
p.settings.format = FMT_DXT5;
|
|
|
|
else if (v == "rgba")
|
|
|
|
p.settings.format = FMT_RGBA;
|
2013-10-18 17:36:31 +02:00
|
|
|
else if (v == "alpha")
|
|
|
|
p.settings.format = FMT_ALPHA;
|
2010-09-10 23:02:10 +02:00
|
|
|
else
|
2015-01-22 21:36:24 +01:00
|
|
|
LOGERROR("Invalid attribute value <file format='%s'>", v.c_str());
|
2010-09-10 23:02:10 +02:00
|
|
|
}
|
|
|
|
else if (attr.Name == at_mipmap)
|
|
|
|
{
|
|
|
|
CStr v(attr.Value);
|
|
|
|
if (v == "true")
|
|
|
|
p.settings.mipmap = MIP_TRUE;
|
|
|
|
else if (v == "false")
|
|
|
|
p.settings.mipmap = MIP_FALSE;
|
|
|
|
else
|
2015-01-22 21:36:24 +01:00
|
|
|
LOGERROR("Invalid attribute value <file mipmap='%s'>", v.c_str());
|
2010-09-10 23:02:10 +02:00
|
|
|
}
|
|
|
|
else if (attr.Name == at_normal)
|
|
|
|
{
|
|
|
|
CStr v(attr.Value);
|
|
|
|
if (v == "true")
|
|
|
|
p.settings.normal = NORMAL_TRUE;
|
|
|
|
else if (v == "false")
|
|
|
|
p.settings.normal = NORMAL_FALSE;
|
|
|
|
else
|
2015-01-22 21:36:24 +01:00
|
|
|
LOGERROR("Invalid attribute value <file normal='%s'>", v.c_str());
|
2010-09-10 23:02:10 +02:00
|
|
|
}
|
|
|
|
else if (attr.Name == at_alpha)
|
|
|
|
{
|
|
|
|
CStr v(attr.Value);
|
|
|
|
if (v == "none")
|
|
|
|
p.settings.alpha = ALPHA_NONE;
|
|
|
|
else if (v == "player")
|
|
|
|
p.settings.alpha = ALPHA_PLAYER;
|
|
|
|
else if (v == "transparency")
|
|
|
|
p.settings.alpha = ALPHA_TRANSPARENCY;
|
|
|
|
else
|
2015-01-22 21:36:24 +01:00
|
|
|
LOGERROR("Invalid attribute value <file alpha='%s'>", v.c_str());
|
2010-09-10 23:02:10 +02:00
|
|
|
}
|
|
|
|
else if (attr.Name == at_filter)
|
|
|
|
{
|
|
|
|
CStr v(attr.Value);
|
|
|
|
if (v == "box")
|
|
|
|
p.settings.filter = FILTER_BOX;
|
|
|
|
else if (v == "triangle")
|
|
|
|
p.settings.filter = FILTER_TRIANGLE;
|
|
|
|
else if (v == "kaiser")
|
|
|
|
p.settings.filter = FILTER_KAISER;
|
|
|
|
else
|
2015-01-22 21:36:24 +01:00
|
|
|
LOGERROR("Invalid attribute value <file filter='%s'>", v.c_str());
|
2010-09-10 23:02:10 +02:00
|
|
|
}
|
|
|
|
else if (attr.Name == at_kaiserwidth)
|
|
|
|
{
|
|
|
|
p.settings.kaiserWidth = CStr(attr.Value).ToFloat();
|
|
|
|
}
|
|
|
|
else if (attr.Name == at_kaiseralpha)
|
|
|
|
{
|
|
|
|
p.settings.kaiserAlpha = CStr(attr.Value).ToFloat();
|
|
|
|
}
|
|
|
|
else if (attr.Name == at_kaiserstretch)
|
|
|
|
{
|
|
|
|
p.settings.kaiserStretch = CStr(attr.Value).ToFloat();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
XMB Improvements, parse JS into XMB, make strings more efficient.
XMB format is bumped to 4, invalidating all cached files. The
differences are:
- element/attribute names are stored after the elements themselves, and
not before. This allows writing XMB data in one pass instead of two.
- names themselves becomes offsets (instead of arbitrary integers),
making getting the string from the int name much more efficient.
XMBFile is renamed to XMBData to clarify that it does not, in fact,
refer to a file on disk.
XMBData::GetElementString is also changed to return a const char*, thus
not creating an std::string. A string_view version is added where
convenient.
The XML->XMB and JS->XMB conversion functions and the corresponding
storage are moved to `ps/XMB`, since that format doesn't particularly
relate to XML. CXeromyces becomes lighter and more focused as a result.
The XML->XMB conversion also benefits from the above streamlining.
Note that in a few cases, string_view gets printed to CLogger via
data(), which is generally not legal, but we know that the strings are
null-terminated here. Our libfmt (version 4) doesn't support
string_view, that would be v5.
Differential Revision: https://code.wildfiregames.com/D3909
This was SVN commit r25375.
2021-05-04 15:02:34 +02:00
|
|
|
LOGERROR("Invalid attribute name <file %s='...'>", XeroFile.GetAttributeString(attr.Name));
|
2010-09-10 23:02:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
settings->patterns.push_back(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return settings.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
CTextureConverter::Settings CTextureConverter::ComputeSettings(const std::wstring& filename, const std::vector<SettingsFile*>& settingsFiles) const
|
|
|
|
{
|
|
|
|
// Set sensible defaults
|
|
|
|
Settings settings;
|
|
|
|
settings.format = FMT_DXT1;
|
|
|
|
settings.mipmap = MIP_TRUE;
|
|
|
|
settings.normal = NORMAL_FALSE;
|
|
|
|
settings.alpha = ALPHA_NONE;
|
|
|
|
settings.filter = FILTER_BOX;
|
|
|
|
settings.kaiserWidth = 3.f;
|
|
|
|
settings.kaiserAlpha = 4.f;
|
|
|
|
settings.kaiserStretch = 1.f;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < settingsFiles.size(); ++i)
|
|
|
|
{
|
|
|
|
for (size_t j = 0; j < settingsFiles[i]->patterns.size(); ++j)
|
|
|
|
{
|
|
|
|
Match p = settingsFiles[i]->patterns[j];
|
|
|
|
|
|
|
|
// Check that the pattern matches the texture file
|
|
|
|
if (!match_wildcard(filename.c_str(), p.pattern.c_str()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (p.settings.format != FMT_UNSPECIFIED)
|
|
|
|
settings.format = p.settings.format;
|
|
|
|
|
|
|
|
if (p.settings.mipmap != MIP_UNSPECIFIED)
|
|
|
|
settings.mipmap = p.settings.mipmap;
|
|
|
|
|
|
|
|
if (p.settings.normal != NORMAL_UNSPECIFIED)
|
|
|
|
settings.normal = p.settings.normal;
|
|
|
|
|
|
|
|
if (p.settings.alpha != ALPHA_UNSPECIFIED)
|
|
|
|
settings.alpha = p.settings.alpha;
|
|
|
|
|
|
|
|
if (p.settings.filter != FILTER_UNSPECIFIED)
|
|
|
|
settings.filter = p.settings.filter;
|
|
|
|
|
|
|
|
if (p.settings.kaiserWidth != -1.f)
|
|
|
|
settings.kaiserWidth = p.settings.kaiserWidth;
|
|
|
|
|
|
|
|
if (p.settings.kaiserAlpha != -1.f)
|
|
|
|
settings.kaiserAlpha = p.settings.kaiserAlpha;
|
|
|
|
|
|
|
|
if (p.settings.kaiserStretch != -1.f)
|
|
|
|
settings.kaiserStretch = p.settings.kaiserStretch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
|
2010-09-20 18:34:09 +02:00
|
|
|
CTextureConverter::CTextureConverter(PIVFS vfs, bool highQuality) :
|
2023-12-17 10:43:54 +01:00
|
|
|
m_VFS(vfs), m_HighQuality(highQuality)
|
2010-09-10 23:02:10 +02:00
|
|
|
{
|
2021-06-10 17:42:38 +02:00
|
|
|
#if CONFIG2_NVTT
|
2010-09-10 23:02:10 +02:00
|
|
|
// Verify that we are running with at least the version we were compiled with,
|
|
|
|
// to avoid bugs caused by ABI changes
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(nvtt::version() >= NVTT_VERSION);
|
2021-06-10 17:42:38 +02:00
|
|
|
#endif // CONFIG2_NVTT
|
2010-09-10 23:02:10 +02:00
|
|
|
}
|
|
|
|
|
2024-06-25 21:03:01 +02:00
|
|
|
CTextureConverter::~CTextureConverter() = default;
|
2010-09-10 23:02:10 +02:00
|
|
|
|
|
|
|
bool CTextureConverter::ConvertTexture(const CTexturePtr& texture, const VfsPath& src, const VfsPath& dest, const Settings& settings)
|
|
|
|
{
|
2021-05-22 21:21:33 +02:00
|
|
|
std::shared_ptr<u8> file;
|
2010-09-10 23:02:10 +02:00
|
|
|
size_t fileSize;
|
|
|
|
if (m_VFS->LoadFile(src, file, fileSize) < 0)
|
|
|
|
{
|
2015-01-22 21:36:24 +01:00
|
|
|
LOGERROR("Failed to load texture \"%s\"", src.string8());
|
2010-09-10 23:02:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Tex tex;
|
2022-04-12 19:39:05 +02:00
|
|
|
const Status decodeStatus = tex.decode(file, fileSize);
|
|
|
|
if (decodeStatus != INFO::OK)
|
2010-09-10 23:02:10 +02:00
|
|
|
{
|
2022-04-12 19:39:05 +02:00
|
|
|
LOGERROR("Failed to decode texture \"%s\" %s", src.string8(), GetStatusAsString(decodeStatus).c_str());
|
2010-09-10 23:02:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-11 18:28:41 +02:00
|
|
|
if (!is_pow2(tex.m_Width) || !is_pow2(tex.m_Height))
|
|
|
|
{
|
|
|
|
LOGERROR("Texture to convert \"%s\" should have width and height be power of two: %zux%zu",
|
|
|
|
src.string8(), tex.m_Width, tex.m_Height);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-10 23:02:10 +02:00
|
|
|
// Check whether there's any alpha channel
|
2014-03-13 03:37:05 +01:00
|
|
|
bool hasAlpha = ((tex.m_Flags & TEX_ALPHA) != 0);
|
2010-09-10 23:02:10 +02:00
|
|
|
|
2013-10-18 17:36:31 +02:00
|
|
|
if (settings.format == FMT_ALPHA)
|
2012-08-30 21:24:24 +02:00
|
|
|
{
|
2013-10-18 17:36:31 +02:00
|
|
|
// Convert to uncompressed 8-bit with no mipmaps
|
2014-03-13 03:37:05 +01:00
|
|
|
if (tex.transform_to((tex.m_Flags | TEX_GREY) & ~(TEX_DXT | TEX_MIPMAPS | TEX_ALPHA)) < 0)
|
2013-10-18 17:36:31 +02:00
|
|
|
{
|
2015-01-22 21:36:24 +01:00
|
|
|
LOGERROR("Failed to transform texture \"%s\"", src.string8());
|
2013-10-18 17:36:31 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-08-30 21:24:24 +02:00
|
|
|
}
|
2013-10-18 17:36:31 +02:00
|
|
|
else
|
2010-09-10 23:02:10 +02:00
|
|
|
{
|
2013-10-18 17:36:31 +02:00
|
|
|
// Convert to uncompressed BGRA with no mipmaps
|
2014-03-13 03:37:05 +01:00
|
|
|
if (tex.transform_to((tex.m_Flags | TEX_BGR | TEX_ALPHA) & ~(TEX_DXT | TEX_MIPMAPS)) < 0)
|
2013-10-18 17:36:31 +02:00
|
|
|
{
|
2015-01-22 21:36:24 +01:00
|
|
|
LOGERROR("Failed to transform texture \"%s\"", src.string8());
|
2013-10-18 17:36:31 +02:00
|
|
|
return false;
|
|
|
|
}
|
2010-09-10 23:02:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the texture has all alpha=255, so we can automatically
|
|
|
|
// switch from DXT3/DXT5 to DXT1 with no loss
|
|
|
|
if (hasAlpha)
|
|
|
|
{
|
|
|
|
hasAlpha = false;
|
2014-03-13 03:37:05 +01:00
|
|
|
u8* data = tex.get_data();
|
|
|
|
for (size_t i = 0; i < tex.m_Width * tex.m_Height; ++i)
|
2010-09-10 23:02:10 +02:00
|
|
|
{
|
|
|
|
if (data[i*4+3] != 0xFF)
|
|
|
|
{
|
|
|
|
hasAlpha = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-15 14:58:58 +01:00
|
|
|
#if CONFIG2_NVTT
|
|
|
|
|
2023-12-17 10:43:54 +01:00
|
|
|
std::unique_ptr<ConversionRequest> request = std::make_unique<ConversionRequest>();
|
2010-09-10 23:02:10 +02:00
|
|
|
request->dest = dest;
|
|
|
|
request->texture = texture;
|
|
|
|
|
|
|
|
// Apply the chosen settings:
|
|
|
|
|
|
|
|
request->inputOptions.setMipmapGeneration(settings.mipmap == MIP_TRUE);
|
|
|
|
|
|
|
|
if (settings.alpha == ALPHA_TRANSPARENCY)
|
|
|
|
request->inputOptions.setAlphaMode(nvtt::AlphaMode_Transparency);
|
|
|
|
else
|
|
|
|
request->inputOptions.setAlphaMode(nvtt::AlphaMode_None);
|
|
|
|
|
|
|
|
if (settings.format == FMT_RGBA)
|
|
|
|
{
|
|
|
|
request->compressionOptions.setFormat(nvtt::Format_RGBA);
|
|
|
|
// Change the default component order (see tex_dds.cpp decode_pf)
|
|
|
|
request->compressionOptions.setPixelFormat(32, 0xFF, 0xFF00, 0xFF0000, 0xFF000000u);
|
|
|
|
}
|
2013-10-18 17:36:31 +02:00
|
|
|
else if (settings.format == FMT_ALPHA)
|
|
|
|
{
|
|
|
|
request->compressionOptions.setFormat(nvtt::Format_RGBA);
|
|
|
|
request->compressionOptions.setPixelFormat(8, 0x00, 0x00, 0x00, 0xFF);
|
|
|
|
}
|
2010-09-10 23:02:10 +02:00
|
|
|
else if (!hasAlpha)
|
|
|
|
{
|
|
|
|
// if no alpha channel then there's no point using DXT3 or DXT5
|
|
|
|
request->compressionOptions.setFormat(nvtt::Format_DXT1);
|
|
|
|
}
|
|
|
|
else if (settings.format == FMT_DXT1)
|
|
|
|
{
|
|
|
|
request->compressionOptions.setFormat(nvtt::Format_DXT1a);
|
|
|
|
}
|
|
|
|
else if (settings.format == FMT_DXT3)
|
|
|
|
{
|
|
|
|
request->compressionOptions.setFormat(nvtt::Format_DXT3);
|
|
|
|
}
|
|
|
|
else if (settings.format == FMT_DXT5)
|
|
|
|
{
|
|
|
|
request->compressionOptions.setFormat(nvtt::Format_DXT5);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settings.filter == FILTER_BOX)
|
|
|
|
request->inputOptions.setMipmapFilter(nvtt::MipmapFilter_Box);
|
|
|
|
else if (settings.filter == FILTER_TRIANGLE)
|
|
|
|
request->inputOptions.setMipmapFilter(nvtt::MipmapFilter_Triangle);
|
|
|
|
else if (settings.filter == FILTER_KAISER)
|
|
|
|
request->inputOptions.setMipmapFilter(nvtt::MipmapFilter_Kaiser);
|
|
|
|
|
|
|
|
if (settings.normal == NORMAL_TRUE)
|
|
|
|
request->inputOptions.setNormalMap(true);
|
|
|
|
|
|
|
|
request->inputOptions.setKaiserParameters(settings.kaiserWidth, settings.kaiserAlpha, settings.kaiserStretch);
|
|
|
|
|
|
|
|
request->inputOptions.setWrapMode(nvtt::WrapMode_Mirror); // TODO: should this be configurable?
|
|
|
|
|
2010-09-20 18:34:09 +02:00
|
|
|
request->compressionOptions.setQuality(m_HighQuality ? nvtt::Quality_Production : nvtt::Quality_Fastest);
|
2010-09-10 23:02:10 +02:00
|
|
|
|
|
|
|
// TODO: normal maps, gamma, etc
|
|
|
|
|
|
|
|
// Load the texture data
|
2014-03-13 03:37:05 +01:00
|
|
|
request->inputOptions.setTextureLayout(nvtt::TextureType_2D, tex.m_Width, tex.m_Height);
|
|
|
|
if (tex.m_Bpp == 32)
|
2013-10-18 17:36:31 +02:00
|
|
|
{
|
2014-03-13 03:37:05 +01:00
|
|
|
request->inputOptions.setMipmapData(tex.get_data(), tex.m_Width, tex.m_Height);
|
2013-10-18 17:36:31 +02:00
|
|
|
}
|
|
|
|
else // bpp == 8
|
|
|
|
{
|
|
|
|
// NVTT requires 32-bit input data, so convert
|
2014-03-13 03:37:05 +01:00
|
|
|
const u8* input = tex.get_data();
|
|
|
|
u8* rgba = new u8[tex.m_Width * tex.m_Height * 4];
|
2013-10-18 17:36:31 +02:00
|
|
|
u8* p = rgba;
|
2014-03-13 03:37:05 +01:00
|
|
|
for (size_t i = 0; i < tex.m_Width * tex.m_Height; i++)
|
2013-10-18 17:36:31 +02:00
|
|
|
{
|
|
|
|
p[0] = p[1] = p[2] = p[3] = *input++;
|
|
|
|
p += 4;
|
|
|
|
}
|
2014-03-13 03:37:05 +01:00
|
|
|
request->inputOptions.setMipmapData(rgba, tex.m_Width, tex.m_Height);
|
2013-10-18 17:36:31 +02:00
|
|
|
delete[] rgba;
|
|
|
|
}
|
2010-09-10 23:02:10 +02:00
|
|
|
|
2023-12-17 10:43:54 +01:00
|
|
|
m_ResultQueue.push(Threading::TaskManager::Instance().PushTask([request = std::move(request)]
|
|
|
|
{
|
|
|
|
PROFILE2("compress");
|
|
|
|
// Set up the result object
|
|
|
|
std::unique_ptr<ConversionResult> result = std::make_unique<ConversionResult>();
|
|
|
|
result->dest = request->dest;
|
|
|
|
result->texture = request->texture;
|
|
|
|
|
|
|
|
request->outputOptions.setOutputHandler(&result->output);
|
2010-09-10 23:02:10 +02:00
|
|
|
|
2023-12-17 10:43:54 +01:00
|
|
|
// Perform the compression
|
|
|
|
nvtt::Compressor compressor;
|
|
|
|
result->ret = compressor.process(request->inputOptions, request->compressionOptions,
|
|
|
|
request->outputOptions);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}, Threading::TaskPriority::LOW));
|
2010-09-10 23:02:10 +02:00
|
|
|
|
|
|
|
return true;
|
2012-02-15 14:58:58 +01:00
|
|
|
|
2021-06-10 17:42:38 +02:00
|
|
|
#else // CONFIG2_NVTT
|
2015-01-22 21:36:24 +01:00
|
|
|
LOGERROR("Failed to convert texture \"%s\" (NVTT not available)", src.string8());
|
2012-02-15 14:58:58 +01:00
|
|
|
return false;
|
2021-06-10 17:42:38 +02:00
|
|
|
#endif // !CONFIG2_NVTT
|
2010-09-10 23:02:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CTextureConverter::Poll(CTexturePtr& texture, VfsPath& dest, bool& ok)
|
|
|
|
{
|
2012-02-15 14:58:58 +01:00
|
|
|
#if CONFIG2_NVTT
|
2023-12-17 10:43:54 +01:00
|
|
|
if (m_ResultQueue.empty() || !m_ResultQueue.front().IsReady())
|
2010-09-10 23:02:10 +02:00
|
|
|
{
|
|
|
|
// no work to do
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-12-17 10:43:54 +01:00
|
|
|
std::unique_ptr<ConversionResult> result = m_ResultQueue.front().Get();
|
|
|
|
m_ResultQueue.pop();
|
|
|
|
|
2010-09-10 23:02:10 +02:00
|
|
|
if (!result->ret)
|
|
|
|
{
|
|
|
|
// conversion had failed
|
|
|
|
ok = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move output into a correctly-aligned buffer
|
|
|
|
size_t size = result->output.buffer.size();
|
2021-05-22 21:21:33 +02:00
|
|
|
std::shared_ptr<u8> file;
|
2011-04-29 21:10:34 +02:00
|
|
|
AllocateAligned(file, size, maxSectorSize);
|
2010-09-10 23:02:10 +02:00
|
|
|
memcpy(file.get(), &result->output.buffer[0], size);
|
|
|
|
if (m_VFS->CreateFile(result->dest, file, size) < 0)
|
|
|
|
{
|
|
|
|
// error writing file
|
|
|
|
ok = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Succeeded in converting texture
|
|
|
|
texture = result->texture;
|
|
|
|
dest = result->dest;
|
|
|
|
ok = true;
|
|
|
|
return true;
|
2012-02-15 14:58:58 +01:00
|
|
|
|
2021-06-10 17:42:38 +02:00
|
|
|
#else // CONFIG2_NVTT
|
2012-02-15 14:58:58 +01:00
|
|
|
return false;
|
2021-06-10 17:42:38 +02:00
|
|
|
#endif // !CONFIG2_NVTT
|
2010-09-10 23:02:10 +02:00
|
|
|
}
|
|
|
|
|
2023-12-17 10:43:54 +01:00
|
|
|
bool CTextureConverter::IsBusy() const
|
2010-09-10 23:02:10 +02:00
|
|
|
{
|
2021-06-10 17:42:38 +02:00
|
|
|
#if CONFIG2_NVTT
|
2023-12-17 10:43:54 +01:00
|
|
|
return m_ResultQueue.size() >= MAX_QUEUE_SIZE_FOR_OPTIMAL_UTILIZATION;
|
2021-06-10 17:42:38 +02:00
|
|
|
#else // CONFIG2_NVTT
|
|
|
|
return false;
|
|
|
|
#endif // !CONFIG2_NVTT
|
2010-09-10 23:02:10 +02:00
|
|
|
}
|