-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathglddstest.c
290 lines (236 loc) · 8.62 KB
/
glddstest.c
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
/**
* MojoDDS; tools for dealing with DDS files.
*
* Please see the file LICENSE.txt in the source's root directory.
*/
#define _GNU_SOURCE
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL.h>
#include <GL/glew.h>
#include "mojodds.h"
// example how to use mojodds with OpenGL
// this program doesn't actually draw anything
// use apitrace, vogl or similar tool to see what it does
static void pumpGLErrors(const char *format, ...) {
va_list args;
va_start(args, format);
char *tempStr = NULL;
vasprintf(&tempStr, format, args);
va_end(args);
GLenum err;
while ((err = glGetError()) != GL_NO_ERROR) {
switch (err) {
case GL_INVALID_ENUM:
printf("GL error GL_INVALID_ENUM in \"%s\"\n", tempStr);
break;
case GL_INVALID_VALUE:
printf("GL error GL_INVALID_VALUE in \"%s\"\n", tempStr);
break;
default:
printf("Unknown GL error %04x in \"%s\"\n", err, tempStr);
break;
}
}
free(tempStr);
}
static int glddstest(const char *filename) {
printf("%s\n", filename);
if (GLEW_GREMEDY_string_marker) {
glStringMarkerGREMEDY(0, filename);
}
FILE *f = fopen(filename, "rb");
if (!f) {
printf("Error opening %s: %s (%d)\n", filename, strerror(errno), errno);
return 1;
}
fseek(f, 0, SEEK_END);
long size = ftell(f);
fseek(f, 0, SEEK_SET);
char *contents = malloc(size);
size_t readbytes = fread(contents, 1, size, f);
if (readbytes != size) {
printf("Only got %u of %ld bytes: %s\n", (unsigned int) readbytes, size, strerror(errno));
free(contents);
fclose(f);
return 2;
}
fclose(f);
int isDDS = MOJODDS_isDDS(contents, size);
printf("isDDS: %d\n", isDDS);
if (isDDS) {
const void *tex = NULL;
unsigned long texlen = 0;
unsigned int glfmt = 0, w = 0, h = 0, miplevels = 0;
unsigned int cubemapfacelen = 0;
MOJODDS_textureType textureType = 0;
int retval = MOJODDS_getTexture(contents, size, &tex, &texlen, &glfmt, &w, &h, &miplevels, &cubemapfacelen, &textureType);
if (!retval) {
printf("MOJODDS_getTexture failed\n");
free(contents);
return 3;
}
GLint maxTexSize = 0;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTexSize);
if (w > maxTexSize || h > maxTexSize) {
printf("Texture too large: %ux%u vs %d\n", w, h, maxTexSize);
return 4;
}
bool isCompressed = true;
GLenum internalFormat = glfmt;
if (glfmt == GL_BGRA || glfmt == GL_BGR || glfmt == GL_LUMINANCE_ALPHA) {
isCompressed = false;
if (glfmt == GL_BGR) {
internalFormat = GL_RGB8;
} else {
internalFormat = GL_RGBA8;
}
}
GLuint texId = 0;
// we leak this but don't care
glGenTextures(1, &texId);
switch (textureType) {
case MOJODDS_TEXTURE_2D:
glBindTexture(GL_TEXTURE_2D, texId);
for (unsigned int miplevel = 0; miplevel < miplevels; miplevel++) {
const void *miptex = NULL;
unsigned long miptexlen = 0;
unsigned int mipW = 0, mipH = 0;
retval = MOJODDS_getMipMapTexture(miplevel, glfmt, tex, w, h, &miptex, &miptexlen, &mipW, &mipH);
if (!retval) {
printf("MOJODDS_getMipMapTexture(%u) error: %d\n", miplevel, retval);
continue;
}
if (isCompressed) {
glCompressedTexImage2D(GL_TEXTURE_2D, miplevel, glfmt, mipW, mipH, 0, miptexlen, miptex);
pumpGLErrors("glCompressedTexImage2D %u 0x%04x %ux%u %u", miplevel, glfmt, mipW, mipH, miptexlen);
} else {
glTexImage2D(GL_TEXTURE_2D, miplevel, internalFormat, mipW, mipH, 0, glfmt, GL_UNSIGNED_BYTE, miptex);
pumpGLErrors("glTexImage2D %u 0x%04x %ux%u 0x%04x", miplevel, internalFormat, mipW, mipH, glfmt);
}
}
// and now the same with ARB_texture_storage if it's available
if (GLEW_ARB_texture_storage) {
glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_2D, texId);
glTexStorage2D(GL_TEXTURE_2D, miplevels, internalFormat, w, h);
pumpGLErrors("glTexStorage2D %u 0x%04x %ux%u", miplevels, internalFormat, w, h);
for (unsigned int miplevel = 0; miplevel < miplevels; miplevel++) {
const void *miptex = NULL;
unsigned long miptexlen = 0;
unsigned int mipW = 0, mipH = 0;
retval = MOJODDS_getMipMapTexture(miplevel, glfmt, tex, w, h, &miptex, &miptexlen, &mipW, &mipH);
if (!retval) {
printf("MOJODDS_getMipMapTexture(%u) error: %d\n", miplevel, retval);
continue;
}
if (isCompressed) {
glCompressedTexSubImage2D(GL_TEXTURE_2D, miplevel, 0, 0, mipW, mipH, glfmt, miptexlen, miptex);
pumpGLErrors("glCompressedTexSubImage2D %u %ux%u 0x%04x %u", miplevel, mipW, mipH, glfmt, miptexlen);
} else {
glTexSubImage2D(GL_TEXTURE_2D, miplevel, 0, 0, mipW, mipH, glfmt, GL_UNSIGNED_BYTE, miptex);
pumpGLErrors("glTexSubImage2D %u %ux%u 0x%04x", miplevel, mipW, mipH, glfmt);
}
}
}
glBindTexture(GL_TEXTURE_2D, 0);
break;
case MOJODDS_TEXTURE_CUBE:
glBindTexture(GL_TEXTURE_CUBE_MAP, texId);
for (MOJODDS_cubeFace cubeFace = MOJODDS_CUBEFACE_POSITIVE_X; cubeFace <= MOJODDS_CUBEFACE_NEGATIVE_Z; cubeFace++) {
for (unsigned int miplevel = 0; miplevel < miplevels; miplevel++) {
const void *miptex = NULL;
unsigned long miptexlen = 0;
unsigned int mipW = 0, mipH = 0;
retval = MOJODDS_getCubeFace(cubeFace, miplevel, glfmt, tex, cubemapfacelen, w, h, &miptex, &miptexlen, &mipW, &mipH);
if (!retval) {
printf("MOJODDS_getCubeFace(%d, %u) error: %d\n", cubeFace, miplevel, retval);
continue;
}
if (isCompressed) {
glCompressedTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + cubeFace, miplevel, glfmt, mipW, mipH, 0, miptexlen, miptex);
pumpGLErrors("glCompressedTexImage2D %u 0x%04x %ux%u %u", miplevel, glfmt, mipW, mipH, miptexlen);
} else {
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + cubeFace, miplevel, internalFormat, mipW, mipH, 0, glfmt, GL_UNSIGNED_BYTE, miptex);
pumpGLErrors("glTexImage2D %u 0x%04x %ux%u 0x%04x", miplevel, internalFormat, mipW, mipH, glfmt);
}
}
}
// and now the same with ARB_texture_storage if it's available
if (GLEW_ARB_texture_storage) {
glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_CUBE_MAP, texId);
glTexStorage2D(GL_TEXTURE_CUBE_MAP, miplevels, internalFormat, w, h);
pumpGLErrors("glTexStorage2D %u 0x%04x %ux%u", miplevels, internalFormat, w, h);
for (MOJODDS_cubeFace cubeFace = MOJODDS_CUBEFACE_POSITIVE_X; cubeFace <= MOJODDS_CUBEFACE_NEGATIVE_Z; cubeFace++) {
for (unsigned int miplevel = 0; miplevel < miplevels; miplevel++) {
const void *miptex = NULL;
unsigned long miptexlen = 0;
unsigned int mipW = 0, mipH = 0;
retval = MOJODDS_getCubeFace(cubeFace, miplevel, glfmt, tex, cubemapfacelen, w, h, &miptex, &miptexlen, &mipW, &mipH);
if (!retval) {
printf("MOJODDS_getCubeFace(%d, %u) error: %d\n", cubeFace, miplevel, retval);
continue;
}
if (isCompressed) {
glCompressedTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + cubeFace, miplevel, 0, 0, mipW, mipH, glfmt, miptexlen, miptex);
pumpGLErrors("glCompressedTexSubImage2D %u %ux%u 0x%04x %u", miplevel, mipW, mipH, glfmt, miptexlen);
} else {
glTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + cubeFace, miplevel, 0, 0, mipW, mipH, glfmt, GL_UNSIGNED_BYTE, miptex);
pumpGLErrors("glTexSubImage2D %u %ux%u 0x%04x", miplevel, mipW, mipH, glfmt);
}
}
}
}
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
break;
case MOJODDS_TEXTURE_VOLUME:
// TODO: do something with the data
break;
}
}
free(contents);
return 0;
}
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: %s DDS-file ...\n", argv[0]);
return 0;
}
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window = SDL_CreateWindow("OpenGL DDS test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 0, 0, SDL_WINDOW_OPENGL);
if (window == NULL) {
printf("Could not create window: %s\n", SDL_GetError());
return 3;
}
SDL_GLContext context = SDL_GL_CreateContext(window);
if (context == NULL) {
printf("Could not create GL context: %s\n", SDL_GetError());
SDL_DestroyWindow(window);
return 4;
}
// clear and swap to get better traces
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SDL_GL_SwapWindow(window);
glewInit();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SDL_GL_SwapWindow(window);
// glewInit might raise errors if we got an OpenGL 3.0 context
// ignore them
// in this case glGetError is simpler than fooling around with callbacks
while (glGetError() != GL_NO_ERROR) { }
for (int i = 1; i < argc; i++) {
glddstest(argv[i]);
// clear and swap to make trace easier to parse
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SDL_GL_SwapWindow(window);
}
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
return 0;
}