1 /* Copyright (C) 2012 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #ifndef INCLUDED_TEXTURECONVERTER
19 #define INCLUDED_TEXTURECONVERTER
21 #include "lib/file/vfs/vfs.h"
22 #include "lib/posix/posix_pthread.h"
23 #include "lib/external_libraries/libsdl.h"
25 #include "TextureManager.h"
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.
37 * XML files are of the form:
40 * <File pattern="*" format="dxt5" mipmap="false" alpha="transparency"/>
41 * <File pattern="button_wood.*" format="rgba"/>
45 * 'pattern' is a wildcard expression, matching on filenames.
46 * All other attributes are optional. Later elements override attributes from
49 * 'format' is 'dxt1', 'dxt3', 'dxt5' or 'rgba'.
51 * 'mipmap' is 'true' or 'false'.
53 * 'normal' is 'true' or 'false'.
55 * 'alpha' is 'transparency' or 'player' (it determines whether the color value of
56 * 0-alpha pixels is significant or not).
58 * 'filter' is 'box', 'triangle' or 'kaiser'.
60 * 'kaiserwidth', 'kaiseralpha', 'kaiserstretch' are floats
61 * (see http://code.google.com/p/nvidia-texture-tools/wiki/ApiDocumentation#Mipmap_Generation)
63 class CTextureConverter
107 * Texture conversion settings.
112 format(FMT_UNSPECIFIED
), mipmap(MIP_UNSPECIFIED
), normal(NORMAL_UNSPECIFIED
),
113 alpha(ALPHA_UNSPECIFIED
), filter(FILTER_UNSPECIFIED
),
114 kaiserWidth(-1.f
), kaiserAlpha(-1.f
), kaiserStretch(-1.f
)
119 * Append this object's state to the given hash.
121 void Hash(MD5
& hash
);
134 * Representation of \<File\> line from settings XML file.
138 std::wstring pattern
;
143 * Representation of settings XML file.
147 std::vector
<Match
> patterns
;
151 * Construct texture converter, for use with files in the given vfs.
153 CTextureConverter(PIVFS vfs
, bool highQuality
);
156 * Destroy texture converter and wait to shut down worker thread.
157 * This might take a long time (maybe seconds) if the worker is busy
158 * processing a texture.
160 ~CTextureConverter();
163 * Load a texture conversion settings XML file.
164 * Returns NULL on failure.
166 SettingsFile
* LoadSettings(const VfsPath
& path
) const;
169 * Match a sequence of settings files against a given texture filename,
170 * and return the resulting settings.
171 * Later entries in settingsFiles override earlier entries.
173 Settings
ComputeSettings(const std::wstring
& filename
, const std::vector
<SettingsFile
*>& settingsFiles
) const;
176 * Begin converting a texture, using the given settings.
177 * This will load src and return false on failure.
178 * Otherwise it will return true and start an asynchronous conversion request,
179 * whose result will be returned from Poll() (with the texture and dest passed
180 * into this function).
182 bool ConvertTexture(const CTexturePtr
& texture
, const VfsPath
& src
, const VfsPath
& dest
, const Settings
& settings
);
185 * Returns the result of a successful ConvertTexture call.
186 * If no result is available yet, returns false.
187 * Otherwise, if the conversion succeeded, it sets ok to true and sets
188 * texture and dest to the corresponding values passed into ConvertTexture(),
190 * If the conversion failed, it sets ok to false and doesn't touch the other arguments,
193 bool Poll(CTexturePtr
& texture
, VfsPath
& dest
, bool& ok
);
196 * Returns whether there is currently a queued request from ConvertTexture().
197 * (Note this may return false while the worker thread is still converting the last texture.)
202 static void* RunThread(void* data
);
207 pthread_t m_WorkerThread
;
208 pthread_mutex_t m_WorkerMutex
;
209 SDL_sem
* m_WorkerSem
;
211 struct ConversionRequest
;
212 struct ConversionResult
;
214 std::deque
<shared_ptr
<ConversionRequest
> > m_RequestQueue
; // protected by m_WorkerMutex
215 std::deque
<shared_ptr
<ConversionResult
> > m_ResultQueue
; // protected by m_WorkerMutex
216 bool m_Shutdown
; // protected by m_WorkerMutex
219 #endif // INCLUDED_TEXTURECONVERTER