Pyrogenesis  trunk
TextureConverter.h
Go to the documentation of this file.
1 /* Copyright (C) 2021 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 "lib/file/vfs/vfs.h"
22 
23 #include "TextureManager.h"
24 
25 #include <condition_variable>
26 #include <deque>
27 #include <mutex>
28 #include <thread>
29 
30 class MD5;
31 
32 /**
33  * Texture conversion helper class.
34  * Provides an asynchronous API to convert input image files into compressed DDS,
35  * given various conversion settings.
36  * (The (potentially very slow) compression is a performed in a background thread,
37  * so the game can remain responsive).
38  * Also provides an API to load conversion settings from XML files.
39  *
40  * XML files are of the form:
41  * @code
42  * <Textures>
43  * <File pattern="*" format="dxt5" mipmap="false" alpha="transparency"/>
44  * <File pattern="button_wood.*" format="rgba"/>
45  * </Entity>
46  * @endcode
47  *
48  * 'pattern' is a wildcard expression, matching on filenames.
49  * All other attributes are optional. Later elements override attributes from
50  * earlier elements.
51  *
52  * 'format' is 'dxt1', 'dxt3', 'dxt5' or 'rgba'.
53  *
54  * 'mipmap' is 'true' or 'false'.
55  *
56  * 'normal' is 'true' or 'false'.
57  *
58  * 'alpha' is 'transparency' or 'player' (it determines whether the color value of
59  * 0-alpha pixels is significant or not).
60  *
61  * 'filter' is 'box', 'triangle' or 'kaiser'.
62  *
63  * 'kaiserwidth', 'kaiseralpha', 'kaiserstretch' are floats
64  * (see http://code.google.com/p/nvidia-texture-tools/wiki/ApiDocumentation#Mipmap_Generation)
65  */
67 {
68 public:
69  enum EFormat
70  {
77  };
78 
79  enum EMipmap
80  {
84  };
85 
87  {
91  };
92 
93  enum EAlpha
94  {
99  };
100 
101  enum EFilter
102  {
107  };
108 
109  /**
110  * Texture conversion settings.
111  */
112  struct Settings
113  {
117  kaiserWidth(-1.f), kaiserAlpha(-1.f), kaiserStretch(-1.f)
118  {
119  }
120 
121  /**
122  * Append this object's state to the given hash.
123  */
124  void Hash(MD5& hash);
125 
131  float kaiserWidth;
132  float kaiserAlpha;
134  };
135 
136  /**
137  * Representation of <File> line from settings XML file.
138  */
139  struct Match
140  {
141  std::wstring pattern;
143  };
144 
145  /**
146  * Representation of settings XML file.
147  */
149  {
150  std::vector<Match> patterns;
151  };
152 
153  /**
154  * Construct texture converter, for use with files in the given vfs.
155  */
156  CTextureConverter(PIVFS vfs, bool highQuality);
157 
158  /**
159  * Destroy texture converter and wait to shut down worker thread.
160  * This might take a long time (maybe seconds) if the worker is busy
161  * processing a texture.
162  */
164 
165  /**
166  * Load a texture conversion settings XML file.
167  * Returns NULL on failure.
168  */
169  SettingsFile* LoadSettings(const VfsPath& path) const;
170 
171  /**
172  * Match a sequence of settings files against a given texture filename,
173  * and return the resulting settings.
174  * Later entries in settingsFiles override earlier entries.
175  */
176  Settings ComputeSettings(const std::wstring& filename, const std::vector<SettingsFile*>& settingsFiles) const;
177 
178  /**
179  * Begin converting a texture, using the given settings.
180  * This will load src and return false on failure.
181  * Otherwise it will return true and start an asynchronous conversion request,
182  * whose result will be returned from Poll() (with the texture and dest passed
183  * into this function).
184  */
185  bool ConvertTexture(const CTexturePtr& texture, const VfsPath& src, const VfsPath& dest, const Settings& settings);
186 
187  /**
188  * Returns the result of a successful ConvertTexture call.
189  * If no result is available yet, returns false.
190  * Otherwise, if the conversion succeeded, it sets ok to true and sets
191  * texture and dest to the corresponding values passed into ConvertTexture(),
192  * then returns true.
193  * If the conversion failed, it sets ok to false and doesn't touch the other arguments,
194  * then returns true.
195  */
196  bool Poll(CTexturePtr& texture, VfsPath& dest, bool& ok);
197 
198  /**
199  * Returns whether there is currently a queued request from ConvertTexture().
200  * (Note this may return false while the worker thread is still converting the last texture.)
201  */
202  bool IsBusy();
203 
204 private:
205  static void RunThread(CTextureConverter* data);
206 
209 
210 #if CONFIG2_NVTT
211  std::thread m_WorkerThread;
212  std::mutex m_WorkerMutex;
213  std::condition_variable m_WorkerCV;
214 #endif // CONFIG2_NVTT
215 
216  struct ConversionRequest;
218 
219  std::deque<std::shared_ptr<ConversionRequest>> m_RequestQueue; // protected by m_WorkerMutex
220  std::deque<std::shared_ptr<ConversionResult>> m_ResultQueue; // protected by m_WorkerMutex
221  bool m_Shutdown; // protected by m_WorkerMutex
222 };
223 
224 #endif // INCLUDED_TEXTURECONVERTER
Definition: TextureConverter.h:75
Definition: TextureConverter.h:96
bool Poll(CTexturePtr &texture, VfsPath &dest, bool &ok)
Returns the result of a successful ConvertTexture call.
Definition: TextureConverter.cpp:497
std::wstring pattern
Definition: TextureConverter.h:141
Definition: TextureConverter.h:97
EFilter
Definition: TextureConverter.h:101
Definition: TextureConverter.h:106
Definition: TextureConverter.h:104
Definition: TextureConverter.h:76
Definition: TextureConverter.h:83
PIVFS m_VFS
Definition: TextureConverter.h:207
float kaiserStretch
Definition: TextureConverter.h:133
Texture conversion settings.
Definition: TextureConverter.h:112
std::deque< std::shared_ptr< ConversionRequest > > m_RequestQueue
Definition: TextureConverter.h:217
bool m_HighQuality
Definition: TextureConverter.h:208
std::vector< Match > patterns
Definition: TextureConverter.h:150
void Hash(MD5 &hash)
Append this object&#39;s state to the given hash.
Definition: TextureConverter.cpp:101
Settings()
Definition: TextureConverter.h:114
std::shared_ptr< IVFS > PIVFS
Definition: vfs.h:220
Definition: TextureConverter.h:90
Settings settings
Definition: TextureConverter.h:142
Definition: TextureConverter.h:103
EFormat format
Definition: TextureConverter.h:126
SettingsFile * LoadSettings(const VfsPath &path) const
Load a texture conversion settings XML file.
Definition: TextureConverter.cpp:113
Definition: TextureConverter.h:88
Representation of <File> line from settings XML file.
Definition: TextureConverter.h:139
Definition: TextureConverter.h:74
MD5 hashing algorithm.
Definition: MD5.h:27
Definition: path.h:79
Definition: TextureConverter.h:73
Definition: TextureConverter.h:95
ENormalMap
Definition: TextureConverter.h:86
static void RunThread(CTextureConverter *data)
Definition: TextureConverter.cpp:558
Definition: TextureConverter.h:81
Representation of settings XML file.
Definition: TextureConverter.h:148
float kaiserAlpha
Definition: TextureConverter.h:132
bool IsBusy()
Returns whether there is currently a queued request from ConvertTexture().
Definition: TextureConverter.cpp:548
EFilter filter
Definition: TextureConverter.h:130
Definition: TextureConverter.h:89
EFormat
Definition: TextureConverter.h:69
float kaiserWidth
Definition: TextureConverter.h:131
Settings ComputeSettings(const std::wstring &filename, const std::vector< SettingsFile *> &settingsFiles) const
Match a sequence of settings files against a given texture filename, and return the resulting setting...
Definition: TextureConverter.cpp:243
EAlpha
Definition: TextureConverter.h:93
Definition: TextureConverter.h:105
Texture conversion helper class.
Definition: TextureConverter.h:66
std::deque< std::shared_ptr< ConversionResult > > m_ResultQueue
Definition: TextureConverter.h:220
bool m_Shutdown
Definition: TextureConverter.h:221
Result from worker thread.
Definition: TextureConverter.cpp:91
~CTextureConverter()
Destroy texture converter and wait to shut down worker thread.
Definition: TextureConverter.cpp:310
EAlpha alpha
Definition: TextureConverter.h:129
EMipmap mipmap
Definition: TextureConverter.h:127
Definition: vfs_util.cpp:39
ENormalMap normal
Definition: TextureConverter.h:128
Definition: TextureConverter.h:72
Definition: TextureConverter.h:82
std::shared_ptr< CTexture > CTexturePtr
Definition: Texture.h:22
Request for worker thread to process.
Definition: TextureConverter.cpp:79
Definition: TextureConverter.h:98
bool ConvertTexture(const CTexturePtr &texture, const VfsPath &src, const VfsPath &dest, const Settings &settings)
Begin converting a texture, using the given settings.
Definition: TextureConverter.cpp:335
Definition: TextureConverter.h:71
CTextureConverter(PIVFS vfs, bool highQuality)
Construct texture converter, for use with files in the given vfs.
Definition: TextureConverter.cpp:295
EMipmap
Definition: TextureConverter.h:79