LCOV - code coverage report
Current view: top level - source/graphics - TextureConverter.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 1 6 16.7 %
Date: 2026-12-03 01:19:39 Functions: 0 0 -

          Line data    Source code
       1             : /* Copyright (C) 2025 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             : #ifndef INCLUDED_TEXTURECONVERTER
      19             : #define INCLUDED_TEXTURECONVERTER
      20             : 
      21             : #include "graphics/Texture.h"
      22             : #include "lib/config2.h"
      23             : #include "lib/file/vfs/vfs.h"
      24             : #include "lib/file/vfs/vfs_path.h"
      25             : 
      26             : #include <string>
      27             : #include <vector>
      28             : 
      29             : #if CONFIG2_NVTT
      30             : #include "ps/Future.h"
      31             : 
      32             : #include <memory>
      33             : #include <queue>
      34             : #endif
      35             : 
      36             : class MD5;
      37             : 
      38             : /**
      39             :  * Texture conversion helper class.
      40             :  * Provides an asynchronous API to convert input image files into compressed DDS,
      41             :  * given various conversion settings.
      42             :  * (The (potentially very slow) compression is a performed asynchronously, so
      43             :  * the game can remain responsive).
      44             :  * Also provides an API to load conversion settings from XML files.
      45             :  *
      46             :  * XML files are of the form:
      47             :  * @code
      48             :  * <Textures>
      49             :  *   <File pattern="*" format="dxt5" mipmap="false" alpha="transparency"/>
      50             :  *   <File pattern="button_wood.*" format="rgba"/>
      51             :  * </Entity>
      52             :  * @endcode
      53             :  *
      54             :  * 'pattern' is a wildcard expression, matching on filenames.
      55             :  * All other attributes are optional. Later elements override attributes from
      56             :  * earlier elements.
      57             :  *
      58             :  * 'format' is 'dxt1', 'dxt3', 'dxt5' or 'rgba'.
      59             :  *
      60             :  * 'mipmap' is 'true' or 'false'.
      61             :  *
      62             :  * 'normal' is 'true' or 'false'.
      63             :  *
      64             :  * 'alpha' is 'transparency' or 'player' (it determines whether the color value of
      65             :  * 0-alpha pixels is significant or not).
      66             :  *
      67             :  * 'filter' is 'box', 'triangle' or 'kaiser'.
      68             :  *
      69             :  * 'kaiserwidth', 'kaiseralpha', 'kaiserstretch' are floats
      70             :  * (see http://code.google.com/p/nvidia-texture-tools/wiki/ApiDocumentation#Mipmap_Generation)
      71             :  */
      72             : class CTextureConverter
      73             : {
      74             : public:
      75             :     enum EFormat
      76             :     {
      77             :         FMT_UNSPECIFIED,
      78             :         FMT_DXT1,
      79             :         FMT_DXT3,
      80             :         FMT_DXT5,
      81             :         FMT_RGBA,
      82             :         FMT_ALPHA
      83             :     };
      84             : 
      85             :     enum EMipmap
      86             :     {
      87             :         MIP_UNSPECIFIED,
      88             :         MIP_TRUE,
      89             :         MIP_FALSE
      90             :     };
      91             : 
      92             :     enum ENormalMap
      93             :     {
      94             :         NORMAL_UNSPECIFIED,
      95             :         NORMAL_TRUE,
      96             :         NORMAL_FALSE
      97             :     };
      98             : 
      99             :     enum EAlpha
     100             :     {
     101             :         ALPHA_UNSPECIFIED,
     102             :         ALPHA_NONE,
     103             :         ALPHA_PLAYER,
     104             :         ALPHA_TRANSPARENCY
     105             :     };
     106             : 
     107             :     enum EFilter
     108             :     {
     109             :         FILTER_UNSPECIFIED,
     110             :         FILTER_BOX,
     111             :         FILTER_TRIANGLE,
     112             :         FILTER_KAISER
     113             :     };
     114             : 
     115             :     /**
     116             :      * Texture conversion settings.
     117             :      */
     118             :     struct Settings
     119             :     {
     120          18 :         Settings() :
     121           0 :             format(FMT_UNSPECIFIED), mipmap(MIP_UNSPECIFIED), normal(NORMAL_UNSPECIFIED),
     122           0 :             alpha(ALPHA_UNSPECIFIED), filter(FILTER_UNSPECIFIED),
     123           0 :             kaiserWidth(-1.f), kaiserAlpha(-1.f), kaiserStretch(-1.f)
     124             :         {
     125             :         }
     126             : 
     127             :         /**
     128             :          * Append this object's state to the given hash.
     129             :          */
     130             :         void Hash(MD5& hash);
     131             : 
     132             :         EFormat format;
     133             :         EMipmap mipmap;
     134             :         ENormalMap normal;
     135             :         EAlpha alpha;
     136             :         EFilter filter;
     137             :         float kaiserWidth;
     138             :         float kaiserAlpha;
     139             :         float kaiserStretch;
     140             :     };
     141             : 
     142             :     /**
     143             :      * Representation of \<File\> line from settings XML file.
     144             :      */
     145           0 :     struct Match
     146             :     {
     147             :         std::wstring pattern;
     148             :         Settings settings;
     149             :     };
     150             : 
     151             :     /**
     152             :      * Representation of settings XML file.
     153             :      */
     154           0 :     struct SettingsFile
     155             :     {
     156             :         std::vector<Match> patterns;
     157             :     };
     158             : 
     159             :     /**
     160             :      * Construct texture converter, for use with files in the given vfs.
     161             :      */
     162             :     CTextureConverter(PIVFS vfs, bool highQuality);
     163             : 
     164             :     /**
     165             :      * The destructor does wait for already started tasks. This might take a
     166             :      * long time (maybe seconds).
     167             :      */
     168             :     ~CTextureConverter();
     169             : 
     170             :     /**
     171             :      * Load a texture conversion settings XML file.
     172             :      * Returns NULL on failure.
     173             :      */
     174             :     SettingsFile* LoadSettings(const VfsPath& path) const;
     175             : 
     176             :     /**
     177             :      * Match a sequence of settings files against a given texture filename,
     178             :      * and return the resulting settings.
     179             :      * Later entries in settingsFiles override earlier entries.
     180             :      */
     181             :     Settings ComputeSettings(const std::wstring& filename, const std::vector<SettingsFile*>& settingsFiles) const;
     182             : 
     183             :     /**
     184             :      * Begin converting a texture, using the given settings.
     185             :      * This will load src and return false on failure.
     186             :      * Otherwise it will return true and start an asynchronous conversion request,
     187             :      * whose result will be returned from Poll() (with the texture and dest passed
     188             :      * into this function).
     189             :      */
     190             :     bool ConvertTexture(const CTexturePtr& texture, const VfsPath& src, const VfsPath& dest, const Settings& settings);
     191             : 
     192             :     /**
     193             :      * Returns the result of a successful ConvertTexture call.
     194             :      * If no result is available yet, returns false.
     195             :      * Otherwise, if the conversion succeeded, it sets ok to true and sets
     196             :      * texture and dest to the corresponding values passed into ConvertTexture(),
     197             :      * then returns true.
     198             :      * If the conversion failed, it sets ok to false and doesn't touch the other arguments,
     199             :      * then returns true.
     200             :      */
     201             :     bool Poll(CTexturePtr& texture, VfsPath& dest, bool& ok);
     202             : 
     203             :     /**
     204             :      * The TextureConverter shouldn't utilize the CPU/Memory fully, so no
     205             :      * conversion should be started when a call to @p IsBusy returns true.
     206             :      */
     207             :     bool IsBusy() const;
     208             : 
     209             : private:
     210             :     PIVFS m_VFS;
     211             :     bool m_HighQuality;
     212             : 
     213             : #if CONFIG2_NVTT
     214             :     struct ConversionResult;
     215             : 
     216             :     std::queue<Future<std::unique_ptr<ConversionResult>>> m_ResultQueue;
     217             : #endif
     218             : };
     219             : 
     220             : #endif // INCLUDED_TEXTURECONVERTER

Generated by: LCOV version 1.16