/
dds.cpp
318 lines (245 loc) · 8.3 KB
/
dds.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
Copyright (C) 2001-2006, William Joseph.
All Rights Reserved.
This file is part of GtkRadiant.
GtkRadiant is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
GtkRadiant is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GtkRadiant; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "dds.h"
#include <stdlib.h>
#include <algorithm>
#include "ifilesystem.h"
#include "iarchive.h"
#include "idatastream.h"
#include "ddslib.h"
#include "util/Noncopyable.h"
#include "RGBAImage.h"
namespace image
{
// Metadata for a single MipMap level
struct MipMapInfo
{
std::size_t width; // pixel width
std::size_t height; // pixel height
std::size_t size; // memory size used by this mipmap
std::size_t offset; // offset in _pixelData to the beginning of this mipmap
MipMapInfo() :
width(0),
height(0),
size(0),
offset(0)
{}
};
typedef std::vector<MipMapInfo> MipMapInfoList;
// Image subclass for DDS images
class DDSImage: public Image, public util::Noncopyable
{
// The actual pixels
byte* _pixelData;
// The amount of memory used by the image data
std::size_t _memSize;
// The compression format ID
GLuint _format;
MipMapInfoList _mipMapInfo;
public:
RGBAPixel* pixels;
unsigned int width, height;
// Pass the required memory size to the constructor
DDSImage(std::size_t size) :
_pixelData(NULL),
_memSize(size)
{
allocateMemory();
}
~DDSImage() {
releaseMemory();
}
void setFormat(GLuint format)
{
_format = format;
}
/**
* greebo: Declares a new mip map to be added to the internal
* structure.
*/
void addMipMap(std::size_t width,
std::size_t height,
std::size_t size,
std::size_t offset)
{
MipMapInfo info;
info.size = size;
info.width = width;
info.height = height;
info.offset = offset;
_mipMapInfo.push_back(info);
}
/**
* Allocates the memory as declared in the constructor.
* This will release any previously allocated memory.
*/
void allocateMemory()
{
// Release memory first
releaseMemory();
_pixelData = new byte[_memSize];
}
void releaseMemory() {
// Check if we have something to do at all
if (_pixelData == NULL) return;
delete[] _pixelData;
_pixelData = NULL;
}
/**
* greebo: Returns the specified mipmap pixel data.
*/
virtual byte* getMipMapPixels(std::size_t mipMapIndex) const {
assert(mipMapIndex < _mipMapInfo.size());
return _pixelData + _mipMapInfo[mipMapIndex].offset;
}
/**
* greebo: Returns the dimension of the specified mipmap.
*/
virtual std::size_t getWidth(std::size_t mipMapIndex) const {
assert(mipMapIndex < _mipMapInfo.size());
return _mipMapInfo[mipMapIndex].width;
}
virtual std::size_t getHeight(std::size_t mipMapIndex) const {
assert(mipMapIndex < _mipMapInfo.size());
return _mipMapInfo[mipMapIndex].height;
}
/* BindableTexture implementation */
TexturePtr bindTexture(const std::string& name) const
{
GLuint textureNum;
debug::assertNoGlErrors();
// Allocate a new texture number and store it into the Texture structure
glGenTextures(1, &textureNum);
glBindTexture(GL_TEXTURE_2D, textureNum);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE);
for (std::size_t i = 0; i < _mipMapInfo.size(); ++i)
{
const MipMapInfo& mipMap = _mipMapInfo[i];
glCompressedTexImage2D(
GL_TEXTURE_2D,
static_cast<GLint>(i),
_format,
static_cast<GLsizei>(mipMap.width),
static_cast<GLsizei>(mipMap.height),
0,
static_cast<GLsizei>(mipMap.size),
_pixelData + mipMap.offset
);
// Handle unsupported format error
if (glGetError() == GL_INVALID_ENUM)
{
rConsoleError() << "[DDSImage] Unable to bind texture '"
<< name << "'; unsupported texture format"
<< std::endl;
return TexturePtr();
}
debug::assertNoGlErrors();
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, static_cast<GLint>(_mipMapInfo.size() - 1));
// Un-bind the texture
glBindTexture(GL_TEXTURE_2D, 0);
// Create and return texture object
BasicTexture2DPtr texObj(new BasicTexture2D(textureNum, name));
texObj->setWidth(getWidth(0));
texObj->setHeight(getHeight(0));
debug::assertNoGlErrors();
return texObj;
}
bool isPrecompressed() const {
return true;
}
};
typedef std::shared_ptr<DDSImage> DDSImagePtr;
DDSImagePtr LoadDDSFromStream(InputStream& stream)
{
int width(0), height(0);
ddsPF_t pixelFormat;
// Load the header
typedef StreamBase::byte_type byteType;
DDSHeader header;
stream.read(reinterpret_cast<byteType*>(&header), sizeof(header));
if (DDSGetInfo(&header, &width, &height, &pixelFormat) == -1) {
return DDSImagePtr();
}
// Get the number of mipmaps from the file
std::size_t mipMapCount = (header.flags & DDSD_MIPMAPCOUNT) ? header.mipMapCount : 1;
MipMapInfoList mipMapInfo;
mipMapInfo.resize(mipMapCount);
// Calculate the total memory requirements (greebo: DXT1 has 8 bytes per block)
std::size_t blockBytes = (pixelFormat == DDS_PF_DXT1) ? 8 : 16;
std::size_t size = 0;
std::size_t offset = 0;
for (std::size_t i = 0; i < mipMapCount; ++i) {
// Create a new mipmap structure
MipMapInfo& mipMap = mipMapInfo[i];
mipMap.offset = offset;
mipMap.width = width;
mipMap.height = height;
mipMap.size = std::max( width, 4 ) / 4 * std::max( height, 4 ) / 4 * blockBytes;
// Update the offset for the next mipmap
offset += mipMap.size;
// Increase the size counter
size += mipMap.size;
// Go to the next mipmap
width = (width+1) >> 1;
height = (height+1) >> 1;
}
// Allocate a new DDS image with that size
DDSImagePtr image(new DDSImage(size));
// Set the format of this DDS image
switch (pixelFormat)
{
case DDS_PF_DXT1:
image->setFormat(GL_COMPRESSED_RGBA_S3TC_DXT1_EXT);
break;
case DDS_PF_DXT3:
image->setFormat(GL_COMPRESSED_RGBA_S3TC_DXT3_EXT);
break;
case DDS_PF_DXT5:
image->setFormat(GL_COMPRESSED_RGBA_S3TC_DXT5_EXT);
break;
default:
break;
};
// Load the mipmaps into the allocated memory
for (std::size_t i = 0; i < mipMapInfo.size(); ++i) {
const MipMapInfo& mipMap = mipMapInfo[i];
// Declare a new mipmap and store the offset
image->addMipMap(mipMap.width, mipMap.height, mipMap.size, mipMap.offset);
// Read the data into the DDSImage's memory
std::size_t bytesRead = stream.read(reinterpret_cast<byteType*>(image->getMipMapPixels(i)), mipMap.size);
assert(bytesRead == mipMap.size);
}
return image;
}
ImagePtr LoadDDS(ArchiveFile& file) {
return LoadDDSFromStream(file.getInputStream());
}
ImagePtr DDSLoader::load(ArchiveFile& file) const
{
// Pass the call to the according load function
return LoadDDS(file);
}
ImageTypeLoader::Extensions DDSLoader::getExtensions() const
{
Extensions extensions;
extensions.push_back("dds");
return extensions;
}
}