Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
TextureConverter.h
Go to the documentation of this file.
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"
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
36class 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 */
73{
74public:
76 {
83 };
84
86 {
90 };
91
93 {
97 };
98
99 enum EAlpha
100 {
105 };
106
108 {
113 };
114
115 /**
116 * Texture conversion settings.
117 */
118 struct Settings
119 {
123 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
140 };
141
142 /**
143 * Representation of <File> line from settings XML file.
144 */
145 struct Match
146 {
147 std::wstring pattern;
149 };
150
151 /**
152 * Representation of settings XML file.
153 */
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 */
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
209private:
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
Texture conversion helper class.
Definition: TextureConverter.h:73
bool IsBusy() const
The TextureConverter shouldn't utilize the CPU/Memory fully, so no conversion should be started when ...
Definition: TextureConverter.cpp:543
CTextureConverter(PIVFS vfs, bool highQuality)
Construct texture converter, for use with files in the given vfs.
Definition: TextureConverter.cpp:316
ENormalMap
Definition: TextureConverter.h:93
@ NORMAL_FALSE
Definition: TextureConverter.h:96
@ NORMAL_UNSPECIFIED
Definition: TextureConverter.h:94
@ NORMAL_TRUE
Definition: TextureConverter.h:95
EFormat
Definition: TextureConverter.h:76
@ FMT_DXT3
Definition: TextureConverter.h:79
@ FMT_RGBA
Definition: TextureConverter.h:81
@ FMT_DXT1
Definition: TextureConverter.h:78
@ FMT_ALPHA
Definition: TextureConverter.h:82
@ FMT_UNSPECIFIED
Definition: TextureConverter.h:77
@ FMT_DXT5
Definition: TextureConverter.h:80
std::queue< Future< std::unique_ptr< ConversionResult > > > m_ResultQueue
Definition: TextureConverter.h:216
~CTextureConverter()
The destructor does wait for already started tasks.
EMipmap
Definition: TextureConverter.h:86
@ MIP_TRUE
Definition: TextureConverter.h:88
@ MIP_UNSPECIFIED
Definition: TextureConverter.h:87
@ MIP_FALSE
Definition: TextureConverter.h:89
SettingsFile * LoadSettings(const VfsPath &path) const
Load a texture conversion settings XML file.
Definition: TextureConverter.cpp:133
EAlpha
Definition: TextureConverter.h:100
@ ALPHA_PLAYER
Definition: TextureConverter.h:103
@ ALPHA_UNSPECIFIED
Definition: TextureConverter.h:101
@ ALPHA_TRANSPARENCY
Definition: TextureConverter.h:104
@ ALPHA_NONE
Definition: TextureConverter.h:102
EFilter
Definition: TextureConverter.h:108
@ FILTER_UNSPECIFIED
Definition: TextureConverter.h:109
@ FILTER_BOX
Definition: TextureConverter.h:110
@ FILTER_KAISER
Definition: TextureConverter.h:112
@ FILTER_TRIANGLE
Definition: TextureConverter.h:111
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:264
bool m_HighQuality
Definition: TextureConverter.h:211
PIVFS m_VFS
Definition: TextureConverter.h:210
bool Poll(CTexturePtr &texture, VfsPath &dest, bool &ok)
Returns the result of a successful ConvertTexture call.
Definition: TextureConverter.cpp:501
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:328
MD5 hashing algorithm.
Definition: MD5.h:30
Definition: path.h:84
std::shared_ptr< CTexture > CTexturePtr
Definition: Texture.h:25
Definition: vfs_util.cpp:42
Response from the asynchronous task.
Definition: TextureConverter.cpp:112
Representation of <File> line from settings XML file.
Definition: TextureConverter.h:146
Settings settings
Definition: TextureConverter.h:148
std::wstring pattern
Definition: TextureConverter.h:147
Representation of settings XML file.
Definition: TextureConverter.h:155
std::vector< Match > patterns
Definition: TextureConverter.h:156
Texture conversion settings.
Definition: TextureConverter.h:119
EFilter filter
Definition: TextureConverter.h:136
EFormat format
Definition: TextureConverter.h:132
EAlpha alpha
Definition: TextureConverter.h:135
ENormalMap normal
Definition: TextureConverter.h:134
EMipmap mipmap
Definition: TextureConverter.h:133
float kaiserAlpha
Definition: TextureConverter.h:138
void Hash(MD5 &hash)
Append this object's state to the given hash.
Definition: TextureConverter.cpp:121
Settings()
Definition: TextureConverter.h:120
float kaiserStretch
Definition: TextureConverter.h:139
float kaiserWidth
Definition: TextureConverter.h:137
std::shared_ptr< IVFS > PIVFS
Definition: vfs.h:240