LCOV - code coverage report
Current view: top level - source/graphics - TextureManager.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 267 535 49.9 %
Date: 2026-12-03 01:19:39 Functions: 25 55 45.5 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "TextureManager.h"
      21             : 
      22             : #include "graphics/Color.h"
      23             : #include "graphics/SColor.h"
      24             : #include "graphics/TextureConverter.h"
      25             : #include "lib/debug.h"
      26             : #include "lib/hash.h"
      27             : #include "lib/path.h"
      28             : #include "lib/status.h"
      29             : #include "lib/tex/tex.h"
      30             : #include "maths/MD5.h"
      31             : #include "maths/MathUtil.h"
      32             : #include "ps/CLogger.h"
      33             : #include "ps/CStr.h"
      34             : #include "ps/CacheLoader.h"
      35             : #include "ps/ConfigDB.h"
      36             : #include "ps/Filesystem.h"
      37             : #include "ps/Profiler2.h"
      38             : #include "ps/Util.h"
      39             : #include "renderer/backend/IDevice.h"
      40             : #include "renderer/backend/IDeviceCommandContext.h"
      41             : #include "renderer/backend/ITexture.h"
      42             : 
      43             : #include <algorithm>
      44             : #include <array>
      45             : #include <bit>
      46             : #include <boost/iterator/iterator_facade.hpp>
      47             : #include <chrono>
      48             : #include <filesystem>
      49             : #include <iterator>
      50             : #include <set>
      51             : #include <sstream>
      52             : #include <string>
      53             : #include <thread>
      54             : #include <unordered_map>
      55             : #include <unordered_set>
      56             : #include <utility>
      57             : #include <vector>
      58             : 
      59             : namespace
      60             : {
      61             : 
      62           6 : Renderer::Backend::Format ChooseFormatAndTransformTextureDataIfNeeded(
      63             :     Renderer::Backend::IDevice* device, Tex& textureData, const bool hasS3TC, const bool sRGB)
      64             : {
      65           6 :     const bool alpha = (textureData.m_Flags & TEX_ALPHA) != 0;
      66           6 :     const bool grey = (textureData.m_Flags & TEX_GREY) != 0;
      67           6 :     const size_t dxt = textureData.m_Flags & TEX_DXT;
      68             : 
      69             :     // Some backends don't support BGR as an internal format (like GLES).
      70             :     // TODO: add a check that the format is internally supported.
      71           6 :     if ((textureData.m_Flags & TEX_BGR) != 0)
      72             :     {
      73           0 :         LOGWARNING("Using slow path to convert BGR texture.");
      74           0 :         textureData.transform_to(textureData.m_Flags & ~TEX_BGR);
      75             :     }
      76             : 
      77           6 :     if (dxt)
      78             :     {
      79           6 :         if (hasS3TC)
      80             :         {
      81           6 :             switch (dxt)
      82             :             {
      83           0 :             case DXT1A:
      84           0 :                 return sRGB ? Renderer::Backend::Format::BC1_RGBA_SRGB : Renderer::Backend::Format::BC1_RGBA_UNORM;
      85           6 :             case 1:
      86          12 :                 return sRGB ? Renderer::Backend::Format::BC1_RGB_SRGB : Renderer::Backend::Format::BC1_RGB_UNORM;
      87           0 :             case 3:
      88           0 :                 return sRGB ? Renderer::Backend::Format::BC2_SRGB : Renderer::Backend::Format::BC2_UNORM;
      89           0 :             case 5:
      90           0 :                 return sRGB ? Renderer::Backend::Format::BC3_SRGB : Renderer::Backend::Format::BC3_UNORM;
      91           0 :             default:
      92           0 :                 LOGERROR("Unknown DXT compression.");
      93           0 :                 return Renderer::Backend::Format::UNDEFINED;
      94             :             }
      95             :         }
      96             :         else
      97           0 :             textureData.transform_to(textureData.m_Flags & ~TEX_DXT);
      98             :     }
      99             : 
     100           0 :     switch (textureData.m_Bpp)
     101             :     {
     102           0 :     case 8:
     103           0 :         ENSURE(grey);
     104           0 :         ENSURE(!sRGB);
     105             :         return Renderer::Backend::Format::L8_UNORM;
     106           0 :     case 24:
     107           0 :         ENSURE(grey);
     108           0 :         ENSURE(!alpha);
     109           0 :         ENSURE(!sRGB);
     110           0 :         if (device->IsTextureFormatSupported(Renderer::Backend::Format::R8G8B8_UNORM))
     111             :             return Renderer::Backend::Format::R8G8B8_UNORM;
     112             :         else
     113             :         {
     114           0 :             LOGWARNING("Using slow path to convert unsupported RGB texture to RGBA.");
     115           0 :             textureData.transform_to(textureData.m_Flags & TEX_ALPHA);
     116           0 :             return Renderer::Backend::Format::R8G8B8A8_UNORM;
     117             :         }
     118           0 :     case 32:
     119           0 :         ENSURE(alpha);
     120           0 :         return sRGB ? Renderer::Backend::Format::R8G8B8A8_SRGB : Renderer::Backend::Format::R8G8B8A8_UNORM;
     121           0 :     default:
     122           0 :         LOGERROR("Unsupported BPP: {}", textureData.m_Bpp);
     123             :     }
     124             : 
     125           0 :     return Renderer::Backend::Format::UNDEFINED;
     126             : }
     127             : 
     128             : } // anonymous namespace
     129             : 
     130         169 : class CPredefinedTexture
     131             : {
     132             : public:
     133           0 :     const CTexturePtr& GetTexture()
     134             :     {
     135           0 :         return m_Texture;
     136             :     }
     137             : 
     138          78 :     void CreateTexture(
     139             :         std::unique_ptr<Renderer::Backend::ITexture> backendTexture,
     140             :         CTextureManagerImpl* textureManager)
     141             :     {
     142          78 :         Renderer::Backend::ITexture* fallback = backendTexture.get();
     143         156 :         CTextureProperties props(VfsPath{});
     144         156 :         m_Texture = CTexturePtr(new CTexture(
     145         156 :             std::move(backendTexture), fallback, props, textureManager));
     146          78 :         m_Texture->m_State = CTexture::UPLOADED;
     147          78 :         m_Texture->m_Self = m_Texture;
     148          78 :     }
     149             : 
     150             : private:
     151             :     CTexturePtr m_Texture;
     152             : };
     153             : 
     154          65 : class CSingleColorTexture final : public CPredefinedTexture
     155             : {
     156             : public:
     157          52 :     CSingleColorTexture(const CColor& color, Renderer::Backend::IDevice* device,
     158             :         CTextureManagerImpl* textureManager)
     159          52 :         : m_Color(color)
     160             :     {
     161          52 :         std::stringstream textureName;
     162          52 :         textureName << "SingleColorTexture (";
     163          52 :         textureName << "R:" << m_Color.r << ", ";
     164          52 :         textureName << "G:" << m_Color.g << ", ";
     165          52 :         textureName << "B:" << m_Color.b << ", ";
     166          52 :         textureName << "A:" << m_Color.a << ")";
     167             : 
     168          52 :         std::unique_ptr<Renderer::Backend::ITexture> backendTexture =
     169          52 :             device->CreateTexture2D(
     170          52 :                 textureName.str().c_str(),
     171             :                 Renderer::Backend::ITexture::Usage::TRANSFER_DST |
     172             :                     Renderer::Backend::ITexture::Usage::SAMPLED,
     173             :                 Renderer::Backend::Format::R8G8B8A8_UNORM,
     174          52 :                 1, 1, Renderer::Backend::Sampler::MakeDefaultSampler(
     175             :                     Renderer::Backend::Sampler::Filter::LINEAR,
     176          52 :                     Renderer::Backend::Sampler::AddressMode::REPEAT));
     177         104 :         CreateTexture(std::move(backendTexture), textureManager);
     178          52 :     }
     179             : 
     180           0 :     void Upload(Renderer::Backend::IDeviceCommandContext* deviceCommandContext)
     181             :     {
     182           0 :         if (!GetTexture() || !GetTexture()->GetBackendTexture())
     183           0 :             return;
     184             : 
     185           0 :         const SColor4ub color32 = m_Color.AsSColor4ub();
     186             :         // Construct 1x1 32-bit texture
     187           0 :         const std::uint8_t data[4] =
     188             :         {
     189             :             color32.R,
     190             :             color32.G,
     191             :             color32.B,
     192             :             color32.A
     193           0 :         };
     194           0 :         deviceCommandContext->UploadTexture(GetTexture()->GetBackendTexture(),
     195             :             Renderer::Backend::Format::R8G8B8A8_UNORM, data, std::size(data));
     196             :     }
     197             : 
     198             : private:
     199             :     CColor m_Color;
     200             : };
     201             : 
     202          26 : class CSingleColorTextureCube final : public CPredefinedTexture
     203             : {
     204             : public:
     205          13 :     CSingleColorTextureCube(const CColor& color, Renderer::Backend::IDevice* device,
     206             :         CTextureManagerImpl* textureManager)
     207          13 :         : m_Color(color)
     208             :     {
     209          13 :         std::stringstream textureName;
     210          13 :         textureName << "SingleColorTextureCube (";
     211          13 :         textureName << "R:" << m_Color.r << ", ";
     212          13 :         textureName << "G:" << m_Color.g << ", ";
     213          13 :         textureName << "B:" << m_Color.b << ", ";
     214          13 :         textureName << "A:" << m_Color.a << ")";
     215             : 
     216          13 :         std::unique_ptr<Renderer::Backend::ITexture> backendTexture =
     217          13 :             device->CreateTexture(
     218          13 :                 textureName.str().c_str(), Renderer::Backend::ITexture::Type::TEXTURE_CUBE,
     219             :                 Renderer::Backend::ITexture::Usage::TRANSFER_DST |
     220             :                     Renderer::Backend::ITexture::Usage::SAMPLED,
     221             :                 Renderer::Backend::Format::R8G8B8A8_UNORM,
     222          13 :                 1, 1, Renderer::Backend::Sampler::MakeDefaultSampler(
     223             :                     Renderer::Backend::Sampler::Filter::LINEAR,
     224          13 :                     Renderer::Backend::Sampler::AddressMode::CLAMP_TO_EDGE), 1, 1);
     225          26 :         CreateTexture(std::move(backendTexture), textureManager);
     226          13 :     }
     227             : 
     228           0 :     void Upload(Renderer::Backend::IDeviceCommandContext* deviceCommandContext)
     229             :     {
     230           0 :         if (!GetTexture() || !GetTexture()->GetBackendTexture())
     231           0 :             return;
     232             : 
     233           0 :         const SColor4ub color32 = m_Color.AsSColor4ub();
     234             :         // Construct 1x1 32-bit texture
     235           0 :         const std::uint8_t data[4] =
     236             :         {
     237             :             color32.R,
     238             :             color32.G,
     239             :             color32.B,
     240             :             color32.A
     241           0 :         };
     242             : 
     243           0 :         for (size_t face = 0; face < 6; ++face)
     244             :         {
     245           0 :             deviceCommandContext->UploadTexture(
     246           0 :                 GetTexture()->GetBackendTexture(), Renderer::Backend::Format::R8G8B8A8_UNORM,
     247             :                 data, std::size(data), 0, face);
     248             :         }
     249             :     }
     250             : 
     251             : private:
     252             :     CColor m_Color;
     253             : };
     254             : 
     255          26 : class CGradientTexture final : public CPredefinedTexture
     256             : {
     257             : public:
     258             :     static const uint32_t WIDTH = 256;
     259             :     static const uint32_t NUMBER_OF_LEVELS = 9;
     260             : 
     261          13 :     CGradientTexture(const CColor& colorFrom, const CColor& colorTo,
     262             :         Renderer::Backend::IDevice* device, CTextureManagerImpl* textureManager)
     263          13 :         : m_ColorFrom(colorFrom), m_ColorTo(colorTo)
     264             :     {
     265          13 :         std::stringstream textureName;
     266          13 :         textureName << "GradientTexture";
     267          13 :         textureName << " From (";
     268          13 :         textureName << "R:" << m_ColorFrom.r << ", ";
     269          13 :         textureName << "G:" << m_ColorFrom.g << ", ";
     270          13 :         textureName << "B:" << m_ColorFrom.b << ", ";
     271          13 :         textureName << "A:" << m_ColorFrom.a << ")";
     272          13 :         textureName << " To (";
     273          13 :         textureName << "R:" << m_ColorTo.r << ", ";
     274          13 :         textureName << "G:" << m_ColorTo.g << ", ";
     275          13 :         textureName << "B:" << m_ColorTo.b << ", ";
     276          13 :         textureName << "A:" << m_ColorTo.a << ")";
     277             : 
     278          13 :         std::unique_ptr<Renderer::Backend::ITexture> backendTexture =
     279          13 :             device->CreateTexture2D(
     280          13 :                 textureName.str().c_str(),
     281             :                 Renderer::Backend::ITexture::Usage::TRANSFER_DST |
     282             :                     Renderer::Backend::ITexture::Usage::SAMPLED,
     283             :                 Renderer::Backend::Format::R8G8B8A8_UNORM,
     284          13 :                 WIDTH, 1, Renderer::Backend::Sampler::MakeDefaultSampler(
     285             :                     Renderer::Backend::Sampler::Filter::LINEAR,
     286             :                     Renderer::Backend::Sampler::AddressMode::CLAMP_TO_EDGE),
     287          13 :                 NUMBER_OF_LEVELS);
     288          26 :         CreateTexture(std::move(backendTexture), textureManager);
     289          13 :     }
     290             : 
     291           0 :     void Upload(Renderer::Backend::IDeviceCommandContext* deviceCommandContext)
     292             :     {
     293           0 :         if (!GetTexture() || !GetTexture()->GetBackendTexture())
     294           0 :             return;
     295             : 
     296             :         std::array<std::array<std::uint8_t, 4>, WIDTH> data;
     297           0 :         for (uint32_t x = 0; x < WIDTH; ++x)
     298             :         {
     299           0 :             const float t = static_cast<float>(x) / (WIDTH - 1);
     300           0 :             const CColor color(
     301           0 :                 Interpolate(m_ColorFrom.r, m_ColorTo.r, t),
     302           0 :                 Interpolate(m_ColorFrom.g, m_ColorTo.g, t),
     303           0 :                 Interpolate(m_ColorFrom.b, m_ColorTo.b, t),
     304           0 :                 Interpolate(m_ColorFrom.a, m_ColorTo.a, t));
     305           0 :             const SColor4ub color32 = color.AsSColor4ub();
     306           0 :             data[x][0] = color32.R;
     307           0 :             data[x][1] = color32.G;
     308           0 :             data[x][2] = color32.B;
     309           0 :             data[x][3] = color32.A;
     310             :         }
     311           0 :         for (uint32_t level = 0; level < NUMBER_OF_LEVELS; ++level)
     312             :         {
     313           0 :             deviceCommandContext->UploadTexture(GetTexture()->GetBackendTexture(),
     314           0 :                 Renderer::Backend::Format::R8G8B8A8_UNORM, data.data(), (WIDTH >> level) * data[0].size(), level);
     315             :             // Prepare data for the next level.
     316           0 :             const uint32_t nextLevelWidth = (WIDTH >> (level + 1));
     317           0 :             if (nextLevelWidth > 0)
     318             :             {
     319           0 :                 for (uint32_t x = 0; x < nextLevelWidth; ++x)
     320           0 :                     data[x] = data[(x << 1)];
     321             :                 // Border values should be the same.
     322           0 :                 data[nextLevelWidth - 1] = data[(WIDTH >> level) - 1];
     323             :             }
     324             :         }
     325             :     }
     326             : 
     327             : private:
     328             :     CColor m_ColorFrom, m_ColorTo;
     329             : };
     330             : 
     331             : struct TPhash
     332             : {
     333          10 :     std::size_t operator()(const CTextureProperties& textureProperties) const
     334             :     {
     335          10 :         std::size_t seed = 0;
     336          10 :         hash_combine(seed, m_PathHash(textureProperties.m_Path));
     337          10 :         hash_combine(seed, textureProperties.m_AddressModeU);
     338          10 :         hash_combine(seed, textureProperties.m_AddressModeV);
     339          10 :         hash_combine(seed, textureProperties.m_AnisotropicFilterEnabled);
     340          10 :         hash_combine(seed, textureProperties.m_FormatOverride);
     341          10 :         hash_combine(seed, textureProperties.m_IgnoreQuality);
     342          10 :         hash_combine(seed, textureProperties.m_SRGB);
     343          10 :         return seed;
     344             :     }
     345             : 
     346          10 :     std::size_t operator()(const CTexturePtr& texture) const
     347             :     {
     348          10 :         return this->operator()(texture->m_Properties);
     349             :     }
     350             : 
     351             : private:
     352             :     std::hash<Path> m_PathHash;
     353             : };
     354             : 
     355             : struct TPequal_to
     356             : {
     357           1 :     bool operator()(const CTextureProperties& lhs, const CTextureProperties& rhs) const
     358             :     {
     359           1 :         return
     360           1 :             lhs.m_Path == rhs.m_Path &&
     361           1 :             lhs.m_AddressModeU == rhs.m_AddressModeU &&
     362             :             lhs.m_AddressModeV == rhs.m_AddressModeV &&
     363           1 :             lhs.m_AnisotropicFilterEnabled == rhs.m_AnisotropicFilterEnabled &&
     364             :             lhs.m_FormatOverride == rhs.m_FormatOverride &&
     365           2 :             lhs.m_IgnoreQuality == rhs.m_IgnoreQuality &&
     366           1 :             lhs.m_SRGB == rhs.m_SRGB;
     367             :     }
     368             : 
     369           1 :     bool operator()(const CTexturePtr& lhs, const CTexturePtr& rhs) const
     370             :     {
     371           1 :         return this->operator()(lhs->m_Properties, rhs->m_Properties);
     372             :     }
     373             : };
     374             : 
     375             : class CTextureManagerImpl
     376             : {
     377             :     friend class CTexture;
     378             : public:
     379          13 :     CTextureManagerImpl(PIVFS vfs, bool highQuality, Renderer::Backend::IDevice* device) :
     380          39 :         m_VFS(vfs), m_CacheLoader(vfs, L".dds"), m_Device(device),
     381          13 :         m_TextureConverter(vfs, highQuality),
     382          13 :         m_DefaultTexture(CColor(0.25f, 0.25f, 0.25f, 1.0f), device, this),
     383          13 :         m_ErrorTexture(CColor(1.0f, 0.0f, 1.0f, 1.0f), device, this),
     384          13 :         m_WhiteTexture(CColor(1.0f, 1.0f, 1.0f, 1.0f), device, this),
     385          13 :         m_TransparentTexture(CColor(0.0f, 0.0f, 0.0f, 0.0f), device, this),
     386          13 :         m_AlphaGradientTexture(
     387          13 :             CColor(1.0f, 1.0f, 1.0f, 0.0f), CColor(1.0f, 1.0f, 1.0f, 1.0f), device, this),
     388          13 :         m_BlackTextureCube(CColor(0.0f, 0.0f, 0.0f, 1.0f), device, this)
     389             :     {
     390             :         // Allow hotloading of textures
     391          13 :         RegisterFileReloadFunc(ReloadChangedFileCB, this);
     392             : 
     393          26 :         m_HasS3TC =
     394          26 :             m_Device->IsTextureFormatSupported(Renderer::Backend::Format::BC1_RGB_UNORM) &&
     395          26 :             m_Device->IsTextureFormatSupported(Renderer::Backend::Format::BC1_RGBA_UNORM) &&
     396          39 :             m_Device->IsTextureFormatSupported(Renderer::Backend::Format::BC2_UNORM) &&
     397          13 :             m_Device->IsTextureFormatSupported(Renderer::Backend::Format::BC3_UNORM);
     398          13 :     }
     399             : 
     400          13 :     ~CTextureManagerImpl()
     401             :     {
     402          13 :         UnregisterFileReloadFunc(ReloadChangedFileCB, this);
     403          91 :     }
     404             : 
     405           0 :     const CTexturePtr& GetErrorTexture()
     406             :     {
     407           0 :         return m_ErrorTexture.GetTexture();
     408             :     }
     409             : 
     410           0 :     const CTexturePtr& GetWhiteTexture()
     411             :     {
     412           0 :         return m_WhiteTexture.GetTexture();
     413             :     }
     414             : 
     415           0 :     const CTexturePtr& GetTransparentTexture()
     416             :     {
     417           0 :         return m_TransparentTexture.GetTexture();
     418             :     }
     419             : 
     420           0 :     const CTexturePtr& GetAlphaGradientTexture()
     421             :     {
     422           0 :         return m_AlphaGradientTexture.GetTexture();
     423             :     }
     424             : 
     425           0 :     const CTexturePtr& GetBlackTextureCube()
     426             :     {
     427           0 :         return m_BlackTextureCube.GetTexture();
     428             :     }
     429             : 
     430             :     /**
     431             :      * See CTextureManager::CreateTexture
     432             :      */
     433           7 :     CTexturePtr CreateTexture(const CTextureProperties& props)
     434             :     {
     435             :         // Construct a new default texture with the given properties to use as the search key
     436           7 :         CTexturePtr texture(new CTexture(
     437          21 :             nullptr, m_DefaultTexture.GetTexture()->GetBackendTexture(), props, this));
     438             : 
     439             :         // Try to find an existing texture with the given properties
     440           7 :         TextureCache::iterator it = m_TextureCache.find(texture);
     441           7 :         if (it != m_TextureCache.end())
     442           1 :             return *it;
     443             : 
     444             :         // Can't find an existing texture - finish setting up this new texture
     445           6 :         texture->m_Self = texture;
     446           6 :         m_TextureCache.insert(texture);
     447           6 :         m_HotloadFiles[props.m_Path].insert(texture);
     448             : 
     449           7 :         return texture;
     450           7 :     }
     451             : 
     452           5 :     CTexturePtr WrapBackendTexture(
     453             :         std::unique_ptr<Renderer::Backend::ITexture> backendTexture)
     454             :     {
     455           5 :         ENSURE(backendTexture);
     456           5 :         Renderer::Backend::ITexture* fallback = backendTexture.get();
     457             : 
     458          10 :         CTextureProperties props(VfsPath{});
     459           5 :         CTexturePtr texture(new CTexture(
     460          10 :             std::move(backendTexture), fallback, props, this));
     461           5 :         texture->m_State = CTexture::UPLOADED;
     462           5 :         texture->m_Self = texture;
     463           5 :         return texture;
     464             :     }
     465             : 
     466             :     /**
     467             :      * Load the given file into the texture object and upload it to OpenGL.
     468             :      * Assumes the file already exists.
     469             :      */
     470           6 :     void LoadTexture(const CTexturePtr& texture, const VfsPath& path)
     471             :     {
     472           6 :         PROFILE2("load texture");
     473           6 :         PROFILE2_ATTR("name: %ls", path.string().c_str());
     474             : 
     475           6 :         std::shared_ptr<std::uint8_t> fileData;
     476           6 :         size_t fileSize;
     477           6 :         const Status loadStatus = m_VFS->LoadFile(path, fileData, fileSize);
     478           6 :         if (loadStatus != INFO::OK)
     479             :         {
     480           0 :             LOGERROR("Texture failed to load; \"{}\" {}",
     481             :                 texture->m_Properties.m_Path.string8(), GetStatusAsString(loadStatus).c_str());
     482           0 :             texture->ResetBackendTexture(
     483           0 :                 nullptr, m_ErrorTexture.GetTexture()->GetBackendTexture());
     484           0 :             texture->m_TextureData.reset();
     485           0 :             return;
     486             :         }
     487             : 
     488           6 :         texture->m_TextureData = std::make_unique<Tex>();
     489           6 :         Tex& textureData = *texture->m_TextureData;
     490           6 :         const Status decodeStatus = textureData.decode(fileData, fileSize);
     491           6 :         if (decodeStatus != INFO::OK)
     492             :         {
     493           0 :             LOGERROR("Texture failed to decode; \"{}\" {}",
     494             :                 texture->m_Properties.m_Path.string8(), GetStatusAsString(decodeStatus));
     495           0 :             texture->ResetBackendTexture(
     496           0 :                 nullptr, m_ErrorTexture.GetTexture()->GetBackendTexture());
     497           0 :             texture->m_TextureData.reset();
     498           0 :             return;
     499             :         }
     500             : 
     501           6 :         if (!std::has_single_bit(textureData.m_Width) || !std::has_single_bit(textureData.m_Height))
     502             :         {
     503           0 :             LOGERROR("Texture should have width and height be power of two; \"{}\" {}x{}",
     504             :                 texture->m_Properties.m_Path.string8(), textureData.m_Width, textureData.m_Height);
     505           0 :             texture->ResetBackendTexture(
     506           0 :                 nullptr, m_ErrorTexture.GetTexture()->GetBackendTexture());
     507           0 :             texture->m_TextureData.reset();
     508           0 :             return;
     509             :         }
     510             : 
     511             :         // Initialise base color from the texture
     512           6 :         texture->m_BaseColor = textureData.get_average_color();
     513             : 
     514           6 :         Renderer::Backend::Format format = Renderer::Backend::Format::UNDEFINED;
     515           6 :         if (texture->m_Properties.m_FormatOverride != Renderer::Backend::Format::UNDEFINED)
     516             :         {
     517           0 :             format = texture->m_Properties.m_FormatOverride;
     518             :             // TODO: it'd be good to remove the override hack and provide information
     519             :             // via XML.
     520           0 :             ENSURE((textureData.m_Flags & TEX_DXT) == 0);
     521           0 :             if (format == Renderer::Backend::Format::A8_UNORM)
     522             :             {
     523           0 :                 ENSURE(textureData.m_Bpp == 8 && (textureData.m_Flags & TEX_GREY));
     524             :             }
     525           0 :             else if (format == Renderer::Backend::Format::R8G8B8A8_UNORM)
     526             :             {
     527           0 :                 ENSURE(textureData.m_Bpp == 32 && (textureData.m_Flags & TEX_ALPHA));
     528             :             }
     529             :             else
     530           0 :                 debug_warn("Unsupported format override.");
     531             :         }
     532             :         else
     533             :         {
     534           6 :             format = ChooseFormatAndTransformTextureDataIfNeeded(
     535           6 :                 m_Device, textureData, m_HasS3TC, texture->m_Properties.m_SRGB);
     536             :         }
     537             : 
     538           6 :         if (format == Renderer::Backend::Format::UNDEFINED)
     539             :         {
     540           0 :             LOGERROR("Texture failed to choose format; \"{}\"", texture->m_Properties.m_Path.string8());
     541           0 :             texture->ResetBackendTexture(
     542           0 :                 nullptr, m_ErrorTexture.GetTexture()->GetBackendTexture());
     543           0 :             texture->m_TextureData.reset();
     544           0 :             return;
     545             :         }
     546             : 
     547           6 :         const uint32_t width = texture->m_TextureData->m_Width;
     548           6 :         const uint32_t height = texture->m_TextureData->m_Height ;
     549           6 :         const uint32_t MIPLevelCount = texture->m_TextureData->GetMIPLevels().size();
     550           6 :         texture->m_BaseLevelOffset = 0;
     551             : 
     552           6 :         Renderer::Backend::Sampler::Desc defaultSamplerDesc =
     553           6 :             Renderer::Backend::Sampler::MakeDefaultSampler(
     554             :                 Renderer::Backend::Sampler::Filter::LINEAR,
     555             :                 Renderer::Backend::Sampler::AddressMode::REPEAT);
     556             : 
     557           6 :         defaultSamplerDesc.addressModeU = texture->m_Properties.m_AddressModeU;
     558           6 :         defaultSamplerDesc.addressModeV = texture->m_Properties.m_AddressModeV;
     559           6 :         if (texture->m_Properties.m_AnisotropicFilterEnabled && m_Device->GetCapabilities().anisotropicFiltering)
     560             :         {
     561           0 :             const int maxAnisotropy{g_ConfigDB.Get("textures.maxanisotropy", 1)};
     562           0 :             const int allowedValues[] = {2, 4, 8, 16};
     563           0 :             if (std::find(std::begin(allowedValues), std::end(allowedValues), maxAnisotropy) !=
     564           0 :                 std::end(allowedValues))
     565             :             {
     566           0 :                 defaultSamplerDesc.anisotropyEnabled = true;
     567           0 :                 defaultSamplerDesc.maxAnisotropy = maxAnisotropy;
     568             :             }
     569             :         }
     570             : 
     571           6 :         if (!texture->m_Properties.m_IgnoreQuality)
     572             :         {
     573           6 :             const int quality{g_ConfigDB.Get("textures.quality", 2)};
     574           6 :             if (quality == 1)
     575             :             {
     576           0 :                 if (MIPLevelCount > 1 && std::min(width, height) > 8)
     577           0 :                     texture->m_BaseLevelOffset += 1;
     578             :             }
     579           6 :             else if (quality == 0)
     580             :             {
     581           0 :                 if (MIPLevelCount > 2 && std::min(width, height) > 16)
     582           0 :                     texture->m_BaseLevelOffset += 2;
     583           0 :                 while (std::min(width >> texture->m_BaseLevelOffset, height >> texture->m_BaseLevelOffset) > 256 &&
     584           0 :                     MIPLevelCount > texture->m_BaseLevelOffset + 1)
     585             :                 {
     586           0 :                     texture->m_BaseLevelOffset += 1;
     587             :                 }
     588           0 :                 defaultSamplerDesc.mipFilter = Renderer::Backend::Sampler::Filter::NEAREST;
     589           0 :                 defaultSamplerDesc.anisotropyEnabled = false;
     590             :             }
     591             :         }
     592             : 
     593          12 :         texture->m_BackendTexture = m_Device->CreateTexture2D(
     594           6 :             texture->m_Properties.m_Path.string8().c_str(),
     595             :             Renderer::Backend::ITexture::Usage::TRANSFER_DST |
     596             :                 Renderer::Backend::ITexture::Usage::SAMPLED,
     597           6 :             format, (width >> texture->m_BaseLevelOffset), (height >> texture->m_BaseLevelOffset),
     598          12 :             defaultSamplerDesc, MIPLevelCount - texture->m_BaseLevelOffset);
     599           6 :     }
     600             : 
     601             :     /**
     602             :      * Set up some parameters for the loose cache filename code.
     603             :      */
     604          11 :     void PrepareCacheKey(const CTexturePtr& texture, MD5& hash, std::uint32_t& version)
     605             :     {
     606             :         // Hash the settings, so we won't use an old loose cache file if the
     607             :         // settings have changed
     608          11 :         CTextureConverter::Settings settings = GetConverterSettings(texture);
     609          11 :         settings.Hash(hash);
     610             : 
     611             :         // Arbitrary version number - change this if we update the code and
     612             :         // need to invalidate old users' caches
     613          11 :         version = 1;
     614             :     }
     615             : 
     616             :     /**
     617             :      * Attempts to load a cached version of a texture.
     618             :      * If the texture is loaded (or there was an error), returns true.
     619             :      * Otherwise, returns false to indicate the caller should generate the cached version.
     620             :      */
     621           6 :     bool TryLoadingCached(const CTexturePtr& texture)
     622             :     {
     623           6 :         MD5 hash;
     624           6 :         std::uint32_t version;
     625           6 :         PrepareCacheKey(texture, hash, version);
     626             : 
     627           6 :         VfsPath loadPath;
     628           6 :         Status ret = m_CacheLoader.TryLoadingCached(texture->m_Properties.m_Path, hash, version, loadPath);
     629             : 
     630           6 :         if (ret == INFO::OK)
     631             :         {
     632             :             // Found a cached texture - load it
     633           1 :             LoadTexture(texture, loadPath);
     634             :             return true;
     635             :         }
     636           5 :         else if (ret == INFO::SKIPPED)
     637             :         {
     638             :             // No cached version was found - we'll need to create it
     639             :             return false;
     640             :         }
     641             :         else
     642             :         {
     643           0 :             ENSURE(ret < 0);
     644             : 
     645             :             // No source file or archive cache was found, so we can't load the
     646             :             // real texture at all - return the error texture instead
     647           0 :             LOGERROR("CCacheLoader failed to find archived or source file for: \"{}\"",
     648             :                 texture->m_Properties.m_Path.string8());
     649           0 :             texture->ResetBackendTexture(
     650           0 :                 nullptr, m_ErrorTexture.GetTexture()->GetBackendTexture());
     651           0 :             return true;
     652             :         }
     653           6 :     }
     654             : 
     655             :     /**
     656             :      * Initiates an asynchronous conversion process, from the texture's
     657             :      * source file to the corresponding loose cache file.
     658             :      */
     659           5 :     void ConvertTexture(const CTexturePtr& texture)
     660             :     {
     661           5 :         const VfsPath sourcePath = texture->m_Properties.m_Path;
     662             : 
     663           5 :         PROFILE2("convert texture");
     664           5 :         PROFILE2_ATTR("name: %ls", sourcePath.string().c_str());
     665             : 
     666           5 :         MD5 hash;
     667           5 :         std::uint32_t version;
     668           5 :         PrepareCacheKey(texture, hash, version);
     669           5 :         const VfsPath looseCachePath = m_CacheLoader.LooseCachePath(sourcePath, hash, version);
     670             : 
     671           5 :         CTextureConverter::Settings settings = GetConverterSettings(texture);
     672             : 
     673           5 :         m_TextureConverter.ConvertTexture(texture, sourcePath, looseCachePath, settings);
     674          10 :     }
     675             : 
     676           0 :     bool TextureExists(const VfsPath& path) const
     677             :     {
     678           0 :         return m_VFS->GetFileInfo(m_CacheLoader.ArchiveCachePath(path), 0) == INFO::OK ||
     679           0 :                m_VFS->GetFileInfo(path, 0) == INFO::OK;
     680             :     }
     681             : 
     682           0 :     bool GenerateCachedTexture(const VfsPath& sourcePath, VfsPath& archiveCachePath)
     683             :     {
     684           0 :         archiveCachePath = m_CacheLoader.ArchiveCachePath(sourcePath);
     685             : 
     686           0 :         CTextureProperties textureProps(sourcePath);
     687           0 :         CTexturePtr texture = CreateTexture(textureProps);
     688           0 :         CTextureConverter::Settings settings = GetConverterSettings(texture);
     689             : 
     690           0 :         if (!m_TextureConverter.ConvertTexture(texture, sourcePath, VfsPath("cache") / archiveCachePath, settings))
     691             :             return false;
     692             : 
     693           0 :         while (true)
     694             :         {
     695           0 :             CTexturePtr textureOut;
     696           0 :             VfsPath dest;
     697           0 :             bool ok;
     698           0 :             if (m_TextureConverter.Poll(textureOut, dest, ok))
     699           0 :                 return ok;
     700             : 
     701           0 :             std::this_thread::sleep_for(std::chrono::microseconds(1));
     702           0 :         }
     703           0 :     }
     704             : 
     705           0 :     VfsPath GetCachedPath(const VfsPath& path) const
     706             :     {
     707           0 :         return m_CacheLoader.ArchiveCachePath(path);
     708             :     }
     709             : 
     710          11 :     bool MakeProgress()
     711             :     {
     712             :         // Process any completed conversion tasks
     713          11 :         {
     714          11 :             CTexturePtr texture;
     715          11 :             VfsPath dest;
     716          11 :             bool ok;
     717          11 :             if (m_TextureConverter.Poll(texture, dest, ok))
     718             :             {
     719           5 :                 if (ok)
     720             :                 {
     721           5 :                     LoadTexture(texture, dest);
     722             :                 }
     723             :                 else
     724             :                 {
     725           0 :                     LOGERROR("Texture failed to convert: \"{}\"",
     726             :                         texture->m_Properties.m_Path.string8());
     727           0 :                     texture->ResetBackendTexture(
     728           0 :                         nullptr, m_ErrorTexture.GetTexture()->GetBackendTexture());
     729             :                 }
     730           5 :                 texture->m_State = CTexture::LOADED;
     731           5 :                 return true;
     732             :             }
     733          16 :         }
     734             : 
     735           6 :         if (m_TextureCache.empty())
     736             :             return false;
     737             : 
     738           6 :         const auto getPriority = [busy = m_TextureConverter.IsBusy()](const CTexturePtr& tex)
     739             :         {
     740             :             // We'll only push new conversion requests if it's not already busy
     741             : 
     742             :             // Look for all high-priority textures needing conversion.
     743          15 :             if (!busy && tex->m_State == CTexture::HIGH_NEEDS_CONVERTING)
     744             :                 return 3;
     745           9 :             if (tex->m_State == CTexture::PREFETCH_NEEDS_LOADING)
     746             :                 return 2;
     747           9 :             if (!busy && tex->m_State == CTexture::PREFETCH_NEEDS_CONVERTING)
     748             :                 return 1;
     749             :             return 0;
     750           6 :         };
     751             : 
     752           6 :         CTexturePtr highPrioritytexture = std::ranges::max(m_TextureCache, {}, getPriority);
     753             : 
     754           6 :         switch (highPrioritytexture->m_State)
     755             :         {
     756           5 :         case CTexture::HIGH_NEEDS_CONVERTING:
     757             :             // Start converting this texture
     758           5 :             highPrioritytexture->m_State = CTexture::HIGH_IS_CONVERTING;
     759           5 :             ConvertTexture(highPrioritytexture);
     760             :             return true;
     761             : 
     762           0 :         case CTexture::PREFETCH_NEEDS_LOADING:
     763             :             // Try loading prefetched textures from their cache
     764           0 :             highPrioritytexture->m_State = TryLoadingCached(highPrioritytexture) ? CTexture::LOADED :
     765             :                 CTexture::PREFETCH_NEEDS_CONVERTING;
     766           0 :             return true;
     767             : 
     768           0 :         case CTexture::PREFETCH_NEEDS_CONVERTING:
     769             :             // If we've got nothing better to do, then start converting prefetched textures.
     770           0 :             highPrioritytexture->m_State = CTexture::PREFETCH_IS_CONVERTING;
     771           0 :             ConvertTexture(highPrioritytexture);
     772             :             return true;
     773             : 
     774             :         default:
     775             :             return false;
     776             :         }
     777          11 :     }
     778             : 
     779           0 :     bool MakeUploadProgress(
     780             :         Renderer::Backend::IDeviceCommandContext* deviceCommandContext)
     781             :     {
     782           0 :         if (!m_PredefinedTexturesUploaded)
     783             :         {
     784           0 :             m_DefaultTexture.Upload(deviceCommandContext);
     785           0 :             m_ErrorTexture.Upload(deviceCommandContext);
     786           0 :             m_WhiteTexture.Upload(deviceCommandContext);
     787           0 :             m_TransparentTexture.Upload(deviceCommandContext);
     788           0 :             m_AlphaGradientTexture.Upload(deviceCommandContext);
     789           0 :             m_BlackTextureCube.Upload(deviceCommandContext);
     790           0 :             m_PredefinedTexturesUploaded = true;
     791           0 :             return true;
     792             :         }
     793             :         return false;
     794             :     }
     795             : 
     796             :     /**
     797             :      * Compute the conversion settings that apply to a given texture, by combining
     798             :      * the textures.xml files from its directory and all parent directories
     799             :      * (up to the VFS root).
     800             :      */
     801          16 :     CTextureConverter::Settings GetConverterSettings(const CTexturePtr& texture)
     802             :     {
     803          16 :         std::filesystem::path srcPath = texture->m_Properties.m_Path.string();
     804             : 
     805          16 :         std::vector<CTextureConverter::SettingsFile*> files;
     806          16 :         VfsPath p;
     807         256 :         for (std::filesystem::path::iterator it = srcPath.begin(); it != srcPath.end(); ++it)
     808             :         {
     809          64 :             VfsPath settingsPath = p / "textures.xml";
     810          64 :             m_HotloadFiles[settingsPath].insert(texture);
     811          64 :             CTextureConverter::SettingsFile* f = GetSettingsFile(settingsPath);
     812          64 :             if (f)
     813           0 :                 files.push_back(f);
     814         272 :             p = p / it->wstring();
     815          64 :         }
     816          64 :         return m_TextureConverter.ComputeSettings(srcPath.filename().wstring(), files);
     817          16 :     }
     818             : 
     819             :     /**
     820             :      * Return the (cached) settings file with the given filename,
     821             :      * or NULL if it doesn't exist.
     822             :      */
     823          64 :     CTextureConverter::SettingsFile* GetSettingsFile(const VfsPath& path)
     824             :     {
     825          64 :         SettingsFilesMap::iterator it = m_SettingsFiles.find(path);
     826          64 :         if (it != m_SettingsFiles.end())
     827          52 :             return it->second.get();
     828             : 
     829          12 :         if (m_VFS->GetFileInfo(path, NULL) >= 0)
     830             :         {
     831           0 :             std::shared_ptr<CTextureConverter::SettingsFile> settings(m_TextureConverter.LoadSettings(path));
     832           0 :             m_SettingsFiles.insert(std::make_pair(path, settings));
     833           0 :             return settings.get();
     834           0 :         }
     835             :         else
     836             :         {
     837          12 :             m_SettingsFiles.insert(std::make_pair(path, std::shared_ptr<CTextureConverter::SettingsFile>()));
     838          12 :             return NULL;
     839             :         }
     840             :     }
     841             : 
     842           0 :     static Status ReloadChangedFileCB(void* param, const VfsPath& path)
     843             :     {
     844           0 :         return static_cast<CTextureManagerImpl*>(param)->ReloadChangedFile(path);
     845             :     }
     846             : 
     847           0 :     Status ReloadChangedFile(const VfsPath& path)
     848             :     {
     849             :         // Uncache settings file, if this is one
     850           0 :         m_SettingsFiles.erase(path);
     851             : 
     852             :         // Find all textures using this file
     853           0 :         HotloadFilesMap::iterator files = m_HotloadFiles.find(path);
     854           0 :         if (files != m_HotloadFiles.end())
     855             :         {
     856             :             // Flag all textures using this file as needing reloading
     857           0 :             for (std::set<std::weak_ptr<CTexture>>::iterator it = files->second.begin(); it != files->second.end(); ++it)
     858             :             {
     859           0 :                 if (std::shared_ptr<CTexture> texture = it->lock())
     860             :                 {
     861           0 :                     texture->m_State = CTexture::UNLOADED;
     862           0 :                     texture->ResetBackendTexture(
     863           0 :                         nullptr, m_DefaultTexture.GetTexture()->GetBackendTexture());
     864           0 :                     texture->m_TextureData.reset();
     865           0 :                 }
     866             :             }
     867             :         }
     868             : 
     869           0 :         return INFO::OK;
     870             :     }
     871             : 
     872           0 :     void ReloadAllTextures()
     873             :     {
     874           0 :         for (const CTexturePtr& texture : m_TextureCache)
     875             :         {
     876           0 :             texture->m_State = CTexture::UNLOADED;
     877           0 :             texture->ResetBackendTexture(
     878           0 :                 nullptr, m_DefaultTexture.GetTexture()->GetBackendTexture());
     879           0 :             texture->m_TextureData.reset();
     880             :         }
     881           0 :     }
     882             : 
     883           0 :     size_t GetBytesUploaded() const
     884             :     {
     885           0 :         size_t size = 0;
     886           0 :         for (TextureCache::const_iterator it = m_TextureCache.begin(); it != m_TextureCache.end(); ++it)
     887           0 :             size += (*it)->GetUploadedSize();
     888           0 :         return size;
     889             :     }
     890             : 
     891           0 :     void OnQualityChanged()
     892             :     {
     893           0 :         ReloadAllTextures();
     894             :     }
     895             : 
     896             : private:
     897             :     PIVFS m_VFS;
     898             :     CCacheLoader m_CacheLoader;
     899             :     Renderer::Backend::IDevice* m_Device = nullptr;
     900             :     CTextureConverter m_TextureConverter;
     901             : 
     902             :     CSingleColorTexture m_DefaultTexture;
     903             :     CSingleColorTexture m_ErrorTexture;
     904             :     CSingleColorTexture m_WhiteTexture;
     905             :     CSingleColorTexture m_TransparentTexture;
     906             :     CGradientTexture m_AlphaGradientTexture;
     907             :     CSingleColorTextureCube m_BlackTextureCube;
     908             :     bool m_PredefinedTexturesUploaded = false;
     909             : 
     910             :     // Cache of all loaded textures
     911             :     using TextureCache =
     912             :         std::unordered_set<CTexturePtr, TPhash, TPequal_to>;
     913             :     TextureCache m_TextureCache;
     914             :     // TODO: we ought to expire unused textures from the cache eventually
     915             : 
     916             :     // Store the set of textures that need to be reloaded when the given file
     917             :     // (a source file or settings.xml) is modified
     918             :     using HotloadFilesMap =
     919             :         std::unordered_map<VfsPath, std::set<std::weak_ptr<CTexture>, std::owner_less<std::weak_ptr<CTexture>>>>;
     920             :     HotloadFilesMap m_HotloadFiles;
     921             : 
     922             :     // Cache for the conversion settings files
     923             :     using SettingsFilesMap =
     924             :         std::unordered_map<VfsPath, std::shared_ptr<CTextureConverter::SettingsFile>>;
     925             :     SettingsFilesMap m_SettingsFiles;
     926             : 
     927             :     bool m_HasS3TC = false;
     928             : };
     929             : 
     930          90 : CTexture::CTexture(
     931             :     std::unique_ptr<Renderer::Backend::ITexture> texture,
     932             :     Renderer::Backend::ITexture* fallback,
     933          90 :     const CTextureProperties& props, CTextureManagerImpl* textureManager) :
     934          90 :     m_BackendTexture(std::move(texture)), m_FallbackBackendTexture(fallback),
     935          90 :     m_BaseColor(0), m_State(UNLOADED), m_Properties(props),
     936          90 :     m_TextureManager(textureManager)
     937             : {
     938          90 : }
     939             : 
     940         268 : CTexture::~CTexture() = default;
     941             : 
     942           0 : void CTexture::UploadBackendTextureIfNeeded(
     943             :     Renderer::Backend::IDeviceCommandContext* deviceCommandContext)
     944             : {
     945           0 :     if (IsUploaded())
     946             :         return;
     947             : 
     948           0 :     if (!IsLoaded())
     949           0 :         TryLoad();
     950             : 
     951           0 :     if (!IsLoaded())
     952             :         return;
     953           0 :     else if (!m_TextureData || !m_BackendTexture)
     954             :     {
     955           0 :         ResetBackendTexture(nullptr, m_TextureManager->GetErrorTexture()->GetBackendTexture());
     956           0 :         m_State = UPLOADED;
     957           0 :         return;
     958             :     }
     959             : 
     960           0 :     m_UploadedSize = 0;
     961           0 :     for (uint32_t textureDataLevel = m_BaseLevelOffset, level = 0; textureDataLevel < m_TextureData->GetMIPLevels().size(); ++textureDataLevel)
     962             :     {
     963           0 :         const Tex::MIPLevel& levelData = m_TextureData->GetMIPLevels()[textureDataLevel];
     964           0 :         deviceCommandContext->UploadTexture(m_BackendTexture.get(), m_BackendTexture->GetFormat(),
     965           0 :             levelData.data, levelData.dataSize, level++);
     966           0 :         m_UploadedSize += levelData.dataSize;
     967             :     }
     968           0 :     m_TextureData.reset();
     969             : 
     970           0 :     m_State = UPLOADED;
     971             : }
     972             : 
     973           7 : Renderer::Backend::ITexture* CTexture::GetBackendTexture()
     974             : {
     975           7 :     return m_BackendTexture && IsUploaded() ? m_BackendTexture.get() : m_FallbackBackendTexture;
     976             : }
     977             : 
     978           0 : const Renderer::Backend::ITexture* CTexture::GetBackendTexture() const
     979             : {
     980           0 :     return m_BackendTexture && IsUploaded() ? m_BackendTexture.get() : m_FallbackBackendTexture;
     981             : }
     982             : 
     983           6 : bool CTexture::TryLoad()
     984             : {
     985             :     // If we haven't started loading, then try loading, and if that fails then request conversion.
     986             :     // If we have already tried prefetch loading, and it failed, bump the conversion request to HIGH priority.
     987           6 :     if (m_State == UNLOADED || m_State == PREFETCH_NEEDS_LOADING || m_State == PREFETCH_NEEDS_CONVERTING)
     988             :     {
     989           6 :         if (std::shared_ptr<CTexture> self = m_Self.lock())
     990             :         {
     991           6 :             if (m_State != PREFETCH_NEEDS_CONVERTING && m_TextureManager->TryLoadingCached(self))
     992           1 :                 m_State = LOADED;
     993             :             else
     994           5 :                 m_State = HIGH_NEEDS_CONVERTING;
     995           6 :         }
     996             :     }
     997             : 
     998           7 :     return IsLoaded() || IsUploaded();
     999             : }
    1000             : 
    1001           0 : void CTexture::Prefetch()
    1002             : {
    1003           0 :     if (m_State == UNLOADED)
    1004             :     {
    1005           0 :         if (std::shared_ptr<CTexture> self = m_Self.lock())
    1006             :         {
    1007           0 :             m_State = PREFETCH_NEEDS_LOADING;
    1008           0 :         }
    1009             :     }
    1010           0 : }
    1011             : 
    1012           0 : void CTexture::ResetBackendTexture(
    1013             :     std::unique_ptr<Renderer::Backend::ITexture> backendTexture,
    1014             :     Renderer::Backend::ITexture* fallbackBackendTexture)
    1015             : {
    1016           0 :     m_BackendTexture = std::move(backendTexture);
    1017           0 :     m_FallbackBackendTexture = fallbackBackendTexture;
    1018           0 : }
    1019             : 
    1020           0 : size_t CTexture::GetWidth() const
    1021             : {
    1022           0 :     return GetBackendTexture()->GetWidth();
    1023             : }
    1024             : 
    1025           0 : size_t CTexture::GetHeight() const
    1026             : {
    1027           0 :     return GetBackendTexture()->GetHeight();
    1028             : }
    1029             : 
    1030           0 : bool CTexture::HasAlpha() const
    1031             : {
    1032           0 :     const Renderer::Backend::Format format = GetBackendTexture()->GetFormat();
    1033           0 :     return
    1034           0 :         format == Renderer::Backend::Format::A8_UNORM ||
    1035           0 :         format == Renderer::Backend::Format::R8G8B8A8_UNORM ||
    1036           0 :         format == Renderer::Backend::Format::BC1_RGBA_UNORM ||
    1037           0 :         format == Renderer::Backend::Format::BC2_UNORM ||
    1038           0 :         format == Renderer::Backend::Format::BC3_UNORM;
    1039             : }
    1040             : 
    1041           0 : std::uint32_t CTexture::GetBaseColor() const
    1042             : {
    1043           0 :     return m_BaseColor;
    1044             : }
    1045             : 
    1046           0 : size_t CTexture::GetUploadedSize() const
    1047             : {
    1048           0 :     return m_UploadedSize;
    1049             : }
    1050             : 
    1051             : // CTextureManager: forward all calls to impl:
    1052             : 
    1053          13 : CTextureManager::CTextureManager(PIVFS vfs, bool highQuality, Renderer::Backend::IDevice* device) :
    1054          13 :     m(new CTextureManagerImpl(vfs, highQuality, device))
    1055             : {
    1056          13 : }
    1057             : 
    1058          13 : CTextureManager::~CTextureManager()
    1059             : {
    1060          13 :     delete m;
    1061          13 : }
    1062             : 
    1063           7 : CTexturePtr CTextureManager::CreateTexture(const CTextureProperties& props)
    1064             : {
    1065           7 :     return m->CreateTexture(props);
    1066             : }
    1067             : 
    1068           5 : CTexturePtr CTextureManager::WrapBackendTexture(
    1069             :     std::unique_ptr<Renderer::Backend::ITexture> backendTexture)
    1070             : {
    1071          10 :     return m->WrapBackendTexture(std::move(backendTexture));
    1072             : }
    1073             : 
    1074           0 : bool CTextureManager::TextureExists(const VfsPath& path) const
    1075             : {
    1076           0 :     return m->TextureExists(path);
    1077             : }
    1078             : 
    1079           0 : const CTexturePtr& CTextureManager::GetErrorTexture()
    1080             : {
    1081           0 :     return m->GetErrorTexture();
    1082             : }
    1083             : 
    1084           0 : const CTexturePtr& CTextureManager::GetWhiteTexture()
    1085             : {
    1086           0 :     return m->GetWhiteTexture();
    1087             : }
    1088             : 
    1089           0 : const CTexturePtr& CTextureManager::GetTransparentTexture()
    1090             : {
    1091           0 :     return m->GetTransparentTexture();
    1092             : }
    1093             : 
    1094           0 : const CTexturePtr& CTextureManager::GetAlphaGradientTexture()
    1095             : {
    1096           0 :     return m->GetAlphaGradientTexture();
    1097             : }
    1098             : 
    1099           0 : const CTexturePtr& CTextureManager::GetBlackTextureCube()
    1100             : {
    1101           0 :     return m->GetBlackTextureCube();
    1102             : }
    1103             : 
    1104          11 : bool CTextureManager::MakeProgress()
    1105             : {
    1106          11 :     return m->MakeProgress();
    1107             : }
    1108             : 
    1109           0 : bool CTextureManager::MakeUploadProgress(
    1110             :     Renderer::Backend::IDeviceCommandContext* deviceCommandContext)
    1111             : {
    1112           0 :     return m->MakeUploadProgress(deviceCommandContext);
    1113             : }
    1114             : 
    1115           0 : bool CTextureManager::GenerateCachedTexture(const VfsPath& path, VfsPath& outputPath)
    1116             : {
    1117           0 :     return m->GenerateCachedTexture(path, outputPath);
    1118             : }
    1119             : 
    1120           0 : VfsPath CTextureManager::GetCachedPath(const VfsPath& path) const
    1121             : {
    1122           0 :     return m->GetCachedPath(path);
    1123             : }
    1124             : 
    1125           0 : size_t CTextureManager::GetBytesUploaded() const
    1126             : {
    1127           0 :     return m->GetBytesUploaded();
    1128             : }
    1129             : 
    1130           0 : void CTextureManager::OnQualityChanged()
    1131             : {
    1132           0 :     m->OnQualityChanged();
    1133           0 : }

Generated by: LCOV version 1.16