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