]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_textures.c
fix some warnings
[divverent/darkplaces.git] / gl_textures.c
1
2 #include "quakedef.h"
3 #include "image.h"
4 #include "jpeg.h"
5 #include "image_png.h"
6
7 cvar_t gl_max_size = {CVAR_SAVE, "gl_max_size", "2048", "maximum allowed texture size, can be used to reduce video memory usage, note: this is automatically reduced to match video card capabilities (such as 256 on 3Dfx cards before Voodoo4/5)"};
8 cvar_t gl_picmip = {CVAR_SAVE, "gl_picmip", "0", "reduces resolution of textures by powers of 2, for example 1 will halve width/height, reducing texture memory usage by 75%"};
9 cvar_t r_lerpimages = {CVAR_SAVE, "r_lerpimages", "1", "bilinear filters images when scaling them up to power of 2 size (mode 1), looks better than glquake (mode 0)"};
10 cvar_t r_precachetextures = {CVAR_SAVE, "r_precachetextures", "1", "0 = never upload textures until used, 1 = upload most textures before use (exceptions: rarely used skin colormap layers), 2 = upload all textures before use (can increase texture memory usage significantly)"};
11 cvar_t gl_texture_anisotropy = {CVAR_SAVE, "gl_texture_anisotropy", "1", "anisotropic filtering quality (if supported by hardware), 1 sample (no anisotropy) and 8 sample (8 tap anisotropy) are recommended values"};
12 cvar_t gl_texturecompression = {CVAR_SAVE, "gl_texturecompression", "0", "whether to compress textures, a value of 0 disables compression (even if the individual cvars are 1), 1 enables fast (low quality) compression at startup, 2 enables slow (high quality) compression at startup"};
13 cvar_t gl_texturecompression_color = {CVAR_SAVE, "gl_texturecompression_color", "1", "whether to compress colormap (diffuse) textures"};
14 cvar_t gl_texturecompression_normal = {CVAR_SAVE, "gl_texturecompression_normal", "1", "whether to compress normalmap (normalmap) textures"};
15 cvar_t gl_texturecompression_gloss = {CVAR_SAVE, "gl_texturecompression_gloss", "1", "whether to compress glossmap (specular) textures"};
16 cvar_t gl_texturecompression_glow = {CVAR_SAVE, "gl_texturecompression_glow", "1", "whether to compress glowmap (luma) textures"};
17 cvar_t gl_texturecompression_2d = {CVAR_SAVE, "gl_texturecompression_2d", "1", "whether to compress 2d (hud/menu) textures other than the font"};
18 cvar_t gl_texturecompression_q3bsplightmaps = {CVAR_SAVE, "gl_texturecompression_q3bsplightmaps", "1", "whether to compress lightmaps in q3bsp format levels"};
19 cvar_t gl_texturecompression_q3bspdeluxemaps = {CVAR_SAVE, "gl_texturecompression_q3bspdeluxemaps", "1", "whether to compress deluxemaps in q3bsp format levels (only levels compiled with q3map2 -deluxe have these)"};
20 cvar_t gl_texturecompression_sky = {CVAR_SAVE, "gl_texturecompression_sky", "0", "whether to compress sky textures"};
21 cvar_t gl_texturecompression_lightcubemaps = {CVAR_SAVE, "gl_texturecompression_lightcubemaps", "1", "whether to compress light cubemaps (spotlights and other light projection images)"};
22
23 int             gl_filter_min = GL_LINEAR_MIPMAP_LINEAR;
24 int             gl_filter_mag = GL_LINEAR;
25
26
27 static mempool_t *texturemempool;
28
29 // note: this must not conflict with TEXF_ flags in r_textures.h
30 // cleared when a texture is uploaded
31 #define GLTEXF_UPLOAD 0x00010000
32 // bitmask for mismatch checking
33 #define GLTEXF_IMPORTANTBITS (0)
34 // set when image is uploaded and freed
35 #define GLTEXF_DESTROYED 0x00040000
36
37 typedef struct textypeinfo_s
38 {
39         int textype;
40         int inputbytesperpixel;
41         int internalbytesperpixel;
42         float glinternalbytesperpixel;
43         int glformat;
44         int glinternalformat;
45 }
46 textypeinfo_t;
47
48 static textypeinfo_t textype_palette                = {TEXTYPE_PALETTE, 1, 4, 4.0f, GL_RGBA   , 3};
49 static textypeinfo_t textype_rgb                    = {TEXTYPE_RGB    , 3, 3, 4.0f, GL_RGB    , 3};
50 static textypeinfo_t textype_rgba                   = {TEXTYPE_RGBA   , 4, 4, 4.0f, GL_RGBA   , 3};
51 static textypeinfo_t textype_palette_alpha          = {TEXTYPE_PALETTE, 1, 4, 4.0f, GL_RGBA   , 4};
52 static textypeinfo_t textype_rgba_alpha             = {TEXTYPE_RGBA   , 4, 4, 4.0f, GL_RGBA   , 4};
53 static textypeinfo_t textype_palette_compress       = {TEXTYPE_PALETTE, 1, 4, 0.5f, GL_RGBA   , GL_COMPRESSED_RGB_ARB};
54 static textypeinfo_t textype_rgb_compress           = {TEXTYPE_RGB    , 3, 3, 0.5f, GL_RGB    , GL_COMPRESSED_RGB_ARB};
55 static textypeinfo_t textype_rgba_compress          = {TEXTYPE_RGBA   , 4, 4, 0.5f, GL_RGBA   , GL_COMPRESSED_RGB_ARB};
56 static textypeinfo_t textype_palette_alpha_compress = {TEXTYPE_PALETTE, 1, 4, 1.0f, GL_RGBA   , GL_COMPRESSED_RGBA_ARB};
57 static textypeinfo_t textype_rgba_alpha_compress    = {TEXTYPE_RGBA   , 4, 4, 1.0f, GL_RGBA   , GL_COMPRESSED_RGBA_ARB};
58
59 #define GLTEXTURETYPE_1D 0
60 #define GLTEXTURETYPE_2D 1
61 #define GLTEXTURETYPE_3D 2
62 #define GLTEXTURETYPE_CUBEMAP 3
63
64 static int gltexturetypeenums[4] = {GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D, GL_TEXTURE_CUBE_MAP_ARB};
65 static int gltexturetypebindingenums[4] = {GL_TEXTURE_BINDING_1D, GL_TEXTURE_BINDING_2D, GL_TEXTURE_BINDING_3D, GL_TEXTURE_BINDING_CUBE_MAP_ARB};
66 static int gltexturetypedimensions[4] = {1, 2, 3, 2};
67 static int cubemapside[6] =
68 {
69         GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
70         GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
71         GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
72         GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
73         GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
74         GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
75 };
76
77 typedef struct gltexture_s
78 {
79         // this field is exposed to the R_GetTexture macro, for speed reasons
80         // (must be identical in rtexture_t)
81         int texnum; // GL texture slot number
82
83         // pointer to texturepool (check this to see if the texture is allocated)
84         struct gltexturepool_s *pool;
85         // pointer to next texture in texturepool chain
86         struct gltexture_s *chain;
87         // name of the texture (this might be removed someday), no duplicates
88         char identifier[MAX_QPATH + 32];
89         // original data size in *inputtexels
90         int inputwidth, inputheight, inputdepth;
91         // copy of the original texture(s) supplied to the upload function, for
92         // delayed uploads (non-precached)
93         unsigned char *inputtexels;
94         // original data size in *inputtexels
95         int inputdatasize;
96         // flags supplied to the LoadTexture function
97         // (might be altered to remove TEXF_ALPHA), and GLTEXF_ private flags
98         int flags;
99         // pointer to one of the textype_ structs
100         textypeinfo_t *textype;
101         // one of the GLTEXTURETYPE_ values
102         int texturetype;
103         // palette if the texture is TEXTYPE_PALETTE
104         const unsigned int *palette;
105         // actual stored texture size after gl_picmip and gl_max_size are applied
106         // (power of 2 if gl_support_arb_texture_non_power_of_two is not supported)
107         int tilewidth, tileheight, tiledepth;
108         // 1 or 6 depending on texturetype
109         int sides;
110         // bytes per pixel
111         int bytesperpixel;
112         // GL_RGB or GL_RGBA
113         int glformat;
114         // 3 or 4
115         int glinternalformat;
116 }
117 gltexture_t;
118
119 #define TEXTUREPOOL_SENTINEL 0xC0DEDBAD
120
121 typedef struct gltexturepool_s
122 {
123         unsigned int sentinel;
124         struct gltexture_s *gltchain;
125         struct gltexturepool_s *next;
126 }
127 gltexturepool_t;
128
129 static gltexturepool_t *gltexturepoolchain = NULL;
130
131 static unsigned char *resizebuffer = NULL, *colorconvertbuffer;
132 static int resizebuffersize = 0;
133 static unsigned char *texturebuffer;
134 static int texturebuffersize = 0;
135
136 static textypeinfo_t *R_GetTexTypeInfo(int textype, int flags)
137 {
138         if ((flags & TEXF_COMPRESS) && gl_texturecompression.integer >= 1 && gl_support_texture_compression)
139         {
140                 if (flags & TEXF_ALPHA)
141                 {
142                         switch(textype)
143                         {
144                         case TEXTYPE_PALETTE:
145                                 return &textype_palette_alpha_compress;
146                         case TEXTYPE_RGB:
147                                 Host_Error("R_GetTexTypeInfo: RGB format has no alpha, TEXF_ALPHA not allowed");
148                                 return NULL;
149                         case TEXTYPE_RGBA:
150                                 return &textype_rgba_alpha_compress;
151                         default:
152                                 Host_Error("R_GetTexTypeInfo: unknown texture format");
153                                 return NULL;
154                         }
155                 }
156                 else
157                 {
158                         switch(textype)
159                         {
160                         case TEXTYPE_PALETTE:
161                                 return &textype_palette_compress;
162                         case TEXTYPE_RGB:
163                                 return &textype_rgb_compress;
164                         case TEXTYPE_RGBA:
165                                 return &textype_rgba_compress;
166                         default:
167                                 Host_Error("R_GetTexTypeInfo: unknown texture format");
168                                 return NULL;
169                         }
170                 }
171         }
172         else
173         {
174                 if (flags & TEXF_ALPHA)
175                 {
176                         switch(textype)
177                         {
178                         case TEXTYPE_PALETTE:
179                                 return &textype_palette_alpha;
180                         case TEXTYPE_RGB:
181                                 Host_Error("R_GetTexTypeInfo: RGB format has no alpha, TEXF_ALPHA not allowed");
182                                 return NULL;
183                         case TEXTYPE_RGBA:
184                                 return &textype_rgba_alpha;
185                         default:
186                                 Host_Error("R_GetTexTypeInfo: unknown texture format");
187                                 return NULL;
188                         }
189                 }
190                 else
191                 {
192                         switch(textype)
193                         {
194                         case TEXTYPE_PALETTE:
195                                 return &textype_palette;
196                         case TEXTYPE_RGB:
197                                 return &textype_rgb;
198                         case TEXTYPE_RGBA:
199                                 return &textype_rgba;
200                         default:
201                                 Host_Error("R_GetTexTypeInfo: unknown texture format");
202                                 return NULL;
203                         }
204                 }
205         }
206         return NULL; // this line only to hush compiler warnings
207 }
208
209 static void R_UploadTexture(gltexture_t *t);
210
211 static void R_PrecacheTexture(gltexture_t *glt)
212 {
213         int precache;
214         precache = false;
215         if (glt->flags & TEXF_ALWAYSPRECACHE)
216                 precache = true;
217         else if (r_precachetextures.integer >= 2)
218                 precache = true;
219         else if (r_precachetextures.integer >= 1)
220                 if (glt->flags & TEXF_PRECACHE)
221                         precache = true;
222
223         if (precache)
224                 R_UploadTexture(glt);
225 }
226
227 int R_RealGetTexture(rtexture_t *rt)
228 {
229         if (rt)
230         {
231                 gltexture_t *glt;
232                 glt = (gltexture_t *)rt;
233                 if (glt->flags & GLTEXF_UPLOAD)
234                         R_UploadTexture(glt);
235                 return glt->texnum;
236         }
237         else
238                 return 0;
239 }
240
241 void R_FreeTexture(rtexture_t *rt)
242 {
243         gltexture_t *glt, **gltpointer;
244
245         glt = (gltexture_t *)rt;
246         if (glt == NULL)
247                 Host_Error("R_FreeTexture: texture == NULL");
248
249         for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
250         if (*gltpointer == glt)
251                 *gltpointer = glt->chain;
252         else
253                 Host_Error("R_FreeTexture: texture \"%s\" not linked in pool", glt->identifier);
254
255         if (glt->texnum)
256         {
257                 CHECKGLERROR
258                 qglDeleteTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
259         }
260
261         if (glt->inputtexels)
262                 Mem_Free(glt->inputtexels);
263         Mem_Free(glt);
264 }
265
266 rtexturepool_t *R_AllocTexturePool(void)
267 {
268         gltexturepool_t *pool;
269         if (texturemempool == NULL)
270                 return NULL;
271         pool = (gltexturepool_t *)Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
272         if (pool == NULL)
273                 return NULL;
274         pool->next = gltexturepoolchain;
275         gltexturepoolchain = pool;
276         pool->sentinel = TEXTUREPOOL_SENTINEL;
277         return (rtexturepool_t *)pool;
278 }
279
280 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
281 {
282         gltexturepool_t *pool, **poolpointer;
283         if (rtexturepool == NULL)
284                 return;
285         if (*rtexturepool == NULL)
286                 return;
287         pool = (gltexturepool_t *)(*rtexturepool);
288         *rtexturepool = NULL;
289         if (pool->sentinel != TEXTUREPOOL_SENTINEL)
290                 Host_Error("R_FreeTexturePool: pool already freed");
291         for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
292         if (*poolpointer == pool)
293                 *poolpointer = pool->next;
294         else
295                 Host_Error("R_FreeTexturePool: pool not linked");
296         while (pool->gltchain)
297                 R_FreeTexture((rtexture_t *)pool->gltchain);
298         Mem_Free(pool);
299 }
300
301
302 typedef struct glmode_s
303 {
304         char *name;
305         int minification, magnification;
306 }
307 glmode_t;
308
309 static glmode_t modes[6] =
310 {
311         {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
312         {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
313         {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
314         {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
315         {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
316         {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
317 };
318
319 static void GL_TextureMode_f (void)
320 {
321         int i;
322         GLint oldbindtexnum;
323         gltexture_t *glt;
324         gltexturepool_t *pool;
325
326         if (Cmd_Argc() == 1)
327         {
328                 for (i = 0;i < 6;i++)
329                 {
330                         if (gl_filter_min == modes[i].minification)
331                         {
332                                 Con_Printf("%s\n", modes[i].name);
333                                 return;
334                         }
335                 }
336                 Con_Print("current filter is unknown???\n");
337                 return;
338         }
339
340         for (i = 0;i < 6;i++)
341                 if (!strcasecmp (modes[i].name, Cmd_Argv(1) ) )
342                         break;
343         if (i == 6)
344         {
345                 Con_Print("bad filter name\n");
346                 return;
347         }
348
349         gl_filter_min = modes[i].minification;
350         gl_filter_mag = modes[i].magnification;
351
352         // change all the existing mipmap texture objects
353         // FIXME: force renderer(/client/something?) restart instead?
354         CHECKGLERROR
355         for (pool = gltexturepoolchain;pool;pool = pool->next)
356         {
357                 for (glt = pool->gltchain;glt;glt = glt->chain)
358                 {
359                         // only update already uploaded images
360                         if (!(glt->flags & (GLTEXF_UPLOAD | TEXF_FORCENEAREST | TEXF_FORCELINEAR)))
361                         {
362                                 qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
363                                 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
364                                 if (glt->flags & TEXF_MIPMAP)
365                                 {
366                                         qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
367                                 }
368                                 else
369                                 {
370                                         qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
371                                 }
372                                 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
373                                 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
374                         }
375                 }
376         }
377 }
378
379 static void GL_Texture_CalcImageSize(int texturetype, int flags, int inwidth, int inheight, int indepth, int *outwidth, int *outheight, int *outdepth)
380 {
381         int picmip = 0, maxsize = 0, width2 = 1, height2 = 1, depth2 = 1;
382
383         if (gl_max_size.integer > gl_max_texture_size)
384                 Cvar_SetValue("gl_max_size", gl_max_texture_size);
385
386         switch (texturetype)
387         {
388         default:
389         case GLTEXTURETYPE_1D:
390         case GLTEXTURETYPE_2D:
391                 maxsize = gl_max_texture_size;
392                 break;
393         case GLTEXTURETYPE_3D:
394                 maxsize = gl_max_3d_texture_size;
395                 break;
396         case GLTEXTURETYPE_CUBEMAP:
397                 maxsize = gl_max_cube_map_texture_size;
398                 break;
399         }
400
401         if (flags & TEXF_PICMIP)
402         {
403                 maxsize = min(maxsize, gl_max_size.integer);
404                 picmip = gl_picmip.integer;
405         }
406
407         if (outwidth)
408         {
409                 if (gl_support_arb_texture_non_power_of_two)
410                         width2 = min(inwidth >> picmip, maxsize);
411                 else
412                 {
413                         for (width2 = 1;width2 < inwidth;width2 <<= 1);
414                         for (width2 >>= picmip;width2 > maxsize;width2 >>= 1);
415                 }
416                 *outwidth = max(1, width2);
417         }
418         if (outheight)
419         {
420                 if (gl_support_arb_texture_non_power_of_two)
421                         height2 = min(inheight >> picmip, maxsize);
422                 else
423                 {
424                         for (height2 = 1;height2 < inheight;height2 <<= 1);
425                         for (height2 >>= picmip;height2 > maxsize;height2 >>= 1);
426                 }
427                 *outheight = max(1, height2);
428         }
429         if (outdepth)
430         {
431                 if (gl_support_arb_texture_non_power_of_two)
432                         depth2 = min(indepth >> picmip, maxsize);
433                 else
434                 {
435                         for (depth2 = 1;depth2 < indepth;depth2 <<= 1);
436                         for (depth2 >>= picmip;depth2 > maxsize;depth2 >>= 1);
437                 }
438                 *outdepth = max(1, depth2);
439         }
440 }
441
442
443 static int R_CalcTexelDataSize (gltexture_t *glt)
444 {
445         int width2, height2, depth2, size;
446
447         GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->inputwidth, glt->inputheight, glt->inputdepth, &width2, &height2, &depth2);
448
449         size = width2 * height2 * depth2;
450
451         if (glt->flags & TEXF_MIPMAP)
452         {
453                 while (width2 > 1 || height2 > 1 || depth2 > 1)
454                 {
455                         if (width2 > 1)
456                                 width2 >>= 1;
457                         if (height2 > 1)
458                                 height2 >>= 1;
459                         if (depth2 > 1)
460                                 depth2 >>= 1;
461                         size += width2 * height2 * depth2;
462                 }
463         }
464
465         return (int)(size * glt->textype->glinternalbytesperpixel) * glt->sides;
466 }
467
468 void R_TextureStats_Print(qboolean printeach, qboolean printpool, qboolean printtotal)
469 {
470         int glsize;
471         int isloaded;
472         int pooltotal = 0, pooltotalt = 0, pooltotalp = 0, poolloaded = 0, poolloadedt = 0, poolloadedp = 0;
473         int sumtotal = 0, sumtotalt = 0, sumtotalp = 0, sumloaded = 0, sumloadedt = 0, sumloadedp = 0;
474         gltexture_t *glt;
475         gltexturepool_t *pool;
476         if (printeach)
477                 Con_Print("glsize input loaded mip alpha name\n");
478         for (pool = gltexturepoolchain;pool;pool = pool->next)
479         {
480                 pooltotal = 0;
481                 pooltotalt = 0;
482                 pooltotalp = 0;
483                 poolloaded = 0;
484                 poolloadedt = 0;
485                 poolloadedp = 0;
486                 for (glt = pool->gltchain;glt;glt = glt->chain)
487                 {
488                         glsize = R_CalcTexelDataSize(glt);
489                         isloaded = !(glt->flags & GLTEXF_UPLOAD);
490                         pooltotal++;
491                         pooltotalt += glsize;
492                         pooltotalp += glt->inputdatasize;
493                         if (isloaded)
494                         {
495                                 poolloaded++;
496                                 poolloadedt += glsize;
497                                 poolloadedp += glt->inputdatasize;
498                         }
499                         if (printeach)
500                                 Con_Printf("%c%4i%c%c%4i%c %s %s %s %s\n", isloaded ? '[' : ' ', (glsize + 1023) / 1024, isloaded ? ']' : ' ', glt->inputtexels ? '[' : ' ', (glt->inputdatasize + 1023) / 1024, glt->inputtexels ? ']' : ' ', isloaded ? "loaded" : "      ", (glt->flags & TEXF_MIPMAP) ? "mip" : "   ", (glt->flags & TEXF_ALPHA) ? "alpha" : "     ", glt->identifier);
501                 }
502                 if (printpool)
503                         Con_Printf("texturepool %10p total: %i (%.3fMB, %.3fMB original), uploaded %i (%.3fMB, %.3fMB original), upload on demand %i (%.3fMB, %.3fMB original)\n", pool, pooltotal, pooltotalt / 1048576.0, pooltotalp / 1048576.0, poolloaded, poolloadedt / 1048576.0, poolloadedp / 1048576.0, pooltotal - poolloaded, (pooltotalt - poolloadedt) / 1048576.0, (pooltotalp - poolloadedp) / 1048576.0);
504                 sumtotal += pooltotal;
505                 sumtotalt += pooltotalt;
506                 sumtotalp += pooltotalp;
507                 sumloaded += poolloaded;
508                 sumloadedt += poolloadedt;
509                 sumloadedp += poolloadedp;
510         }
511         if (printtotal)
512                 Con_Printf("textures total: %i (%.3fMB, %.3fMB original), uploaded %i (%.3fMB, %.3fMB original), upload on demand %i (%.3fMB, %.3fMB original)\n", sumtotal, sumtotalt / 1048576.0, sumtotalp / 1048576.0, sumloaded, sumloadedt / 1048576.0, sumloadedp / 1048576.0, sumtotal - sumloaded, (sumtotalt - sumloadedt) / 1048576.0, (sumtotalp - sumloadedp) / 1048576.0);
513 }
514
515 static void R_TextureStats_f(void)
516 {
517         R_TextureStats_Print(true, true, true);
518 }
519
520 static void r_textures_start(void)
521 {
522         // LordHavoc: allow any alignment
523         CHECKGLERROR
524         qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);CHECKGLERROR
525         qglPixelStorei(GL_PACK_ALIGNMENT, 1);CHECKGLERROR
526
527         texturemempool = Mem_AllocPool("texture management", 0, NULL);
528
529         // Disable JPEG screenshots if the DLL isn't loaded
530         if (! JPEG_OpenLibrary ())
531                 Cvar_SetValueQuick (&scr_screenshot_jpeg, 0);
532         // TODO: support png screenshots?
533         PNG_OpenLibrary ();
534 }
535
536 static void r_textures_shutdown(void)
537 {
538         rtexturepool_t *temp;
539
540         JPEG_CloseLibrary ();
541
542         while(gltexturepoolchain)
543         {
544                 temp = (rtexturepool_t *) gltexturepoolchain;
545                 R_FreeTexturePool(&temp);
546         }
547
548         resizebuffersize = 0;
549         texturebuffersize = 0;
550         resizebuffer = NULL;
551         colorconvertbuffer = NULL;
552         texturebuffer = NULL;
553         Mem_FreePool(&texturemempool);
554 }
555
556 static void r_textures_newmap(void)
557 {
558 }
559
560 void R_Textures_Init (void)
561 {
562         Cmd_AddCommand("gl_texturemode", &GL_TextureMode_f, "set texture filtering mode (GL_NEAREST, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, etc)");
563         Cmd_AddCommand("r_texturestats", R_TextureStats_f, "print information about all loaded textures and some statistics");
564         Cvar_RegisterVariable (&gl_max_size);
565         Cvar_RegisterVariable (&gl_picmip);
566         Cvar_RegisterVariable (&r_lerpimages);
567         Cvar_RegisterVariable (&r_precachetextures);
568         Cvar_RegisterVariable (&gl_texture_anisotropy);
569         Cvar_RegisterVariable (&gl_texturecompression);
570         Cvar_RegisterVariable (&gl_texturecompression_color);
571         Cvar_RegisterVariable (&gl_texturecompression_normal);
572         Cvar_RegisterVariable (&gl_texturecompression_gloss);
573         Cvar_RegisterVariable (&gl_texturecompression_glow);
574         Cvar_RegisterVariable (&gl_texturecompression_2d);
575         Cvar_RegisterVariable (&gl_texturecompression_q3bsplightmaps);
576         Cvar_RegisterVariable (&gl_texturecompression_q3bspdeluxemaps);
577         Cvar_RegisterVariable (&gl_texturecompression_sky);
578         Cvar_RegisterVariable (&gl_texturecompression_lightcubemaps);
579
580         R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
581 }
582
583 void R_Textures_Frame (void)
584 {
585         static int old_aniso = 0;
586
587         // could do procedural texture animation here, if we keep track of which
588         // textures were accessed this frame...
589
590         // free the resize buffers
591         resizebuffersize = 0;
592         if (resizebuffer)
593         {
594                 Mem_Free(resizebuffer);
595                 resizebuffer = NULL;
596         }
597         if (colorconvertbuffer)
598         {
599                 Mem_Free(colorconvertbuffer);
600                 colorconvertbuffer = NULL;
601         }
602
603         if (old_aniso != gl_texture_anisotropy.integer)
604         {
605                 gltexture_t *glt;
606                 gltexturepool_t *pool;
607                 GLint oldbindtexnum;
608
609                 old_aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
610
611                 Cvar_SetValueQuick(&gl_texture_anisotropy, old_aniso);
612
613                 CHECKGLERROR
614                 for (pool = gltexturepoolchain;pool;pool = pool->next)
615                 {
616                         for (glt = pool->gltchain;glt;glt = glt->chain)
617                         {
618                                 // only update already uploaded images
619                                 if ((glt->flags & (GLTEXF_UPLOAD | TEXF_MIPMAP)) == TEXF_MIPMAP)
620                                 {
621                                         qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
622
623                                         qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
624                                         qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAX_ANISOTROPY_EXT, old_aniso);CHECKGLERROR
625
626                                         qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
627                                 }
628                         }
629                 }
630         }
631 }
632
633 void R_MakeResizeBufferBigger(int size)
634 {
635         if (resizebuffersize < size)
636         {
637                 resizebuffersize = size;
638                 if (resizebuffer)
639                         Mem_Free(resizebuffer);
640                 if (colorconvertbuffer)
641                         Mem_Free(colorconvertbuffer);
642                 resizebuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
643                 colorconvertbuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
644                 if (!resizebuffer || !colorconvertbuffer)
645                         Host_Error("R_Upload: out of memory");
646         }
647 }
648
649 static void GL_SetupTextureParameters(int flags, int texturetype)
650 {
651         int textureenum = gltexturetypeenums[texturetype];
652         int wrapmode = ((flags & TEXF_CLAMP) && gl_support_clamptoedge) ? GL_CLAMP_TO_EDGE : GL_REPEAT;
653
654         CHECKGLERROR
655
656         if (gl_support_anisotropy && (flags & TEXF_MIPMAP))
657         {
658                 int aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
659                 if (gl_texture_anisotropy.integer != aniso)
660                         Cvar_SetValueQuick(&gl_texture_anisotropy, aniso);
661                 qglTexParameteri(textureenum, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);CHECKGLERROR
662         }
663         qglTexParameteri(textureenum, GL_TEXTURE_WRAP_S, wrapmode);CHECKGLERROR
664         qglTexParameteri(textureenum, GL_TEXTURE_WRAP_T, wrapmode);CHECKGLERROR
665         if (gltexturetypedimensions[texturetype] >= 3)
666         {
667                 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_R, wrapmode);CHECKGLERROR
668         }
669
670         CHECKGLERROR
671         if (flags & TEXF_FORCENEAREST)
672         {
673                 if (flags & TEXF_MIPMAP)
674                 {
675                         qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);CHECKGLERROR
676                 }
677                 else
678                 {
679                         qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST);CHECKGLERROR
680                 }
681                 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_NEAREST);CHECKGLERROR
682         }
683         else if (flags & TEXF_FORCELINEAR)
684         {
685                 if (flags & TEXF_MIPMAP)
686                 {
687                         if (gl_filter_min == GL_NEAREST_MIPMAP_LINEAR || gl_filter_min == GL_LINEAR_MIPMAP_LINEAR)
688                         {
689                                 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);CHECKGLERROR
690                         }
691                         else
692                         {
693                                 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);CHECKGLERROR
694                         }
695                 }
696                 else
697                 {
698                         qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR);CHECKGLERROR
699                 }
700                 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_LINEAR);CHECKGLERROR
701         }
702         else
703         {
704                 if (flags & TEXF_MIPMAP)
705                 {
706                         qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
707                 }
708                 else
709                 {
710                         qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
711                 }
712                 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
713         }
714
715         CHECKGLERROR
716 }
717
718 static void R_Upload(gltexture_t *glt, unsigned char *data, int fragx, int fragy, int fragz, int fragwidth, int fragheight, int fragdepth)
719 {
720         int i, mip, width, height, depth;
721         GLint oldbindtexnum;
722         unsigned char *prevbuffer;
723         prevbuffer = data;
724
725         CHECKGLERROR
726
727         // we need to restore the texture binding after finishing the upload
728         qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
729         qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
730
731         // these are rounded up versions of the size to do better resampling
732         if (gl_support_arb_texture_non_power_of_two)
733         {
734                 width = glt->inputwidth;
735                 height = glt->inputheight;
736                 depth = glt->inputdepth;
737         }
738         else
739         {
740                 for (width  = 1;width  < glt->inputwidth ;width  <<= 1);
741                 for (height = 1;height < glt->inputheight;height <<= 1);
742                 for (depth  = 1;depth  < glt->inputdepth ;depth  <<= 1);
743         }
744
745         R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel);
746         R_MakeResizeBufferBigger(fragwidth * fragheight * fragdepth * glt->sides * glt->bytesperpixel);
747
748         if (prevbuffer == NULL)
749         {
750                 memset(resizebuffer, 0, fragwidth * fragheight * fragdepth * glt->bytesperpixel);
751                 prevbuffer = resizebuffer;
752         }
753         else if (glt->textype->textype == TEXTYPE_PALETTE)
754         {
755                 // promote paletted to RGBA, so we only have to worry about RGB and
756                 // RGBA in the rest of this code
757                 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, fragwidth * fragheight * fragdepth * glt->sides, glt->palette);
758                 prevbuffer = colorconvertbuffer;
759         }
760
761         if ((glt->flags & (TEXF_MIPMAP | TEXF_PICMIP | GLTEXF_UPLOAD)) == 0 && glt->inputwidth == glt->tilewidth && glt->inputheight == glt->tileheight && glt->inputdepth == glt->tiledepth)
762         {
763                 // update a portion of the image
764                 switch(glt->texturetype)
765                 {
766                 case GLTEXTURETYPE_1D:
767                         qglTexSubImage1D(GL_TEXTURE_1D, 0, fragx, fragwidth, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
768                         break;
769                 case GLTEXTURETYPE_2D:
770                         qglTexSubImage2D(GL_TEXTURE_2D, 0, fragx, fragy, fragwidth, fragheight, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
771                         break;
772                 case GLTEXTURETYPE_3D:
773                         qglTexSubImage3D(GL_TEXTURE_3D, 0, fragx, fragy, fragz, fragwidth, fragheight, fragdepth, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
774                         break;
775                 default:
776                         Host_Error("R_Upload: partial update of type other than 1D, 2D, or 3D");
777                         break;
778                 }
779         }
780         else
781         {
782                 if (fragx || fragy || fragz || glt->inputwidth != fragwidth || glt->inputheight != fragheight || glt->inputdepth != fragdepth)
783                         Host_Error("R_Upload: partial update not allowed on initial upload or in combination with PICMIP or MIPMAP\n");
784
785                 // upload the image for the first time
786                 glt->flags &= ~GLTEXF_UPLOAD;
787
788                 // cubemaps contain multiple images and thus get processed a bit differently
789                 if (glt->texturetype != GLTEXTURETYPE_CUBEMAP)
790                 {
791                         if (glt->inputwidth != width || glt->inputheight != height || glt->inputdepth != depth)
792                         {
793                                 Image_Resample(prevbuffer, glt->inputwidth, glt->inputheight, glt->inputdepth, resizebuffer, width, height, depth, glt->bytesperpixel, r_lerpimages.integer);
794                                 prevbuffer = resizebuffer;
795                         }
796                         // picmip/max_size
797                         while (width > glt->tilewidth || height > glt->tileheight || depth > glt->tiledepth)
798                         {
799                                 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->tilewidth, glt->tileheight, glt->tiledepth, glt->bytesperpixel);
800                                 prevbuffer = resizebuffer;
801                         }
802                 }
803                 mip = 0;
804                 if (gl_support_texture_compression)
805                 {
806                         if (gl_texturecompression.integer >= 2)
807                                 qglHint(GL_TEXTURE_COMPRESSION_HINT_ARB, GL_NICEST);
808                         else
809                                 qglHint(GL_TEXTURE_COMPRESSION_HINT_ARB, GL_FASTEST);
810                         CHECKGLERROR
811                 }
812                 switch(glt->texturetype)
813                 {
814                 case GLTEXTURETYPE_1D:
815                         qglTexImage1D(GL_TEXTURE_1D, mip++, glt->glinternalformat, width, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
816                         if (glt->flags & TEXF_MIPMAP)
817                         {
818                                 while (width > 1 || height > 1 || depth > 1)
819                                 {
820                                         Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->bytesperpixel);
821                                         prevbuffer = resizebuffer;
822                                         qglTexImage1D(GL_TEXTURE_1D, mip++, glt->glinternalformat, width, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
823                                 }
824                         }
825                         break;
826                 case GLTEXTURETYPE_2D:
827                         qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
828                         if (glt->flags & TEXF_MIPMAP)
829                         {
830                                 while (width > 1 || height > 1 || depth > 1)
831                                 {
832                                         Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->bytesperpixel);
833                                         prevbuffer = resizebuffer;
834                                         qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
835                                 }
836                         }
837                         break;
838                 case GLTEXTURETYPE_3D:
839                         qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
840                         if (glt->flags & TEXF_MIPMAP)
841                         {
842                                 while (width > 1 || height > 1 || depth > 1)
843                                 {
844                                         Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->bytesperpixel);
845                                         prevbuffer = resizebuffer;
846                                         qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
847                                 }
848                         }
849                         break;
850                 case GLTEXTURETYPE_CUBEMAP:
851                         // convert and upload each side in turn,
852                         // from a continuous block of input texels
853                         texturebuffer = prevbuffer;
854                         for (i = 0;i < 6;i++)
855                         {
856                                 prevbuffer = texturebuffer;
857                                 texturebuffer += glt->inputwidth * glt->inputheight * glt->inputdepth * glt->textype->inputbytesperpixel;
858                                 if (glt->inputwidth != width || glt->inputheight != height || glt->inputdepth != depth)
859                                 {
860                                         Image_Resample(prevbuffer, glt->inputwidth, glt->inputheight, glt->inputdepth, resizebuffer, width, height, depth, glt->bytesperpixel, r_lerpimages.integer);
861                                         prevbuffer = resizebuffer;
862                                 }
863                                 // picmip/max_size
864                                 while (width > glt->tilewidth || height > glt->tileheight || depth > glt->tiledepth)
865                                 {
866                                         Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->tilewidth, glt->tileheight, glt->tiledepth, glt->bytesperpixel);
867                                         prevbuffer = resizebuffer;
868                                 }
869                                 mip = 0;
870                                 qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
871                                 if (glt->flags & TEXF_MIPMAP)
872                                 {
873                                         while (width > 1 || height > 1 || depth > 1)
874                                         {
875                                                 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->bytesperpixel);
876                                                 prevbuffer = resizebuffer;
877                                                 qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
878                                         }
879                                 }
880                         }
881                         break;
882                 }
883                 GL_SetupTextureParameters(glt->flags, glt->texturetype);
884         }
885         qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
886 }
887
888 static void R_UploadTexture (gltexture_t *glt)
889 {
890         if (!(glt->flags & GLTEXF_UPLOAD))
891                 return;
892
893         CHECKGLERROR
894         qglGenTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
895         R_Upload(glt, glt->inputtexels, 0, 0, 0, glt->inputwidth, glt->inputheight, glt->inputdepth);
896         if (glt->inputtexels)
897         {
898                 Mem_Free(glt->inputtexels);
899                 glt->inputtexels = NULL;
900                 glt->flags |= GLTEXF_DESTROYED;
901         }
902         else if (glt->flags & GLTEXF_DESTROYED)
903                 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed.  Can not upload original image again.  Uploaded blank texture.\n", glt->identifier);
904 }
905
906 static rtexture_t *R_SetupTexture(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, int depth, int sides, int flags, int textype, int texturetype, const unsigned char *data, const unsigned int *palette)
907 {
908         int i, size;
909         gltexture_t *glt;
910         gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
911         textypeinfo_t *texinfo;
912
913         if (cls.state == ca_dedicated)
914                 return NULL;
915
916         if (texturetype == GLTEXTURETYPE_CUBEMAP && !gl_texturecubemap)
917         {
918                 Con_Printf ("R_LoadTexture: cubemap texture not supported by driver\n");
919                 return NULL;
920         }
921         if (texturetype == GLTEXTURETYPE_3D && !gl_texture3d)
922         {
923                 Con_Printf ("R_LoadTexture: 3d texture not supported by driver\n");
924                 return NULL;
925         }
926
927         texinfo = R_GetTexTypeInfo(textype, flags);
928         size = width * height * depth * sides * texinfo->inputbytesperpixel;
929         if (size < 1)
930         {
931                 Con_Printf ("R_LoadTexture: bogus texture size (%dx%dx%dx%dbppx%dsides = %d bytes)\n", width, height, depth, texinfo->inputbytesperpixel * 8, sides, size);
932                 return NULL;
933         }
934
935         // clear the alpha flag if the texture has no transparent pixels
936         switch(textype)
937         {
938         case TEXTYPE_PALETTE:
939                 if (flags & TEXF_ALPHA)
940                 {
941                         flags &= ~TEXF_ALPHA;
942                         if (data)
943                         {
944                                 for (i = 0;i < size;i++)
945                                 {
946                                         if (((unsigned char *)&palette[data[i]])[3] < 255)
947                                         {
948                                                 flags |= TEXF_ALPHA;
949                                                 break;
950                                         }
951                                 }
952                         }
953                 }
954                 break;
955         case TEXTYPE_RGB:
956                 if (flags & TEXF_ALPHA)
957                         Host_Error("R_LoadTexture: RGB has no alpha, don't specify TEXF_ALPHA");
958                 break;
959         case TEXTYPE_RGBA:
960                 if (flags & TEXF_ALPHA)
961                 {
962                         flags &= ~TEXF_ALPHA;
963                         if (data)
964                         {
965                                 for (i = 3;i < size;i += 4)
966                                 {
967                                         if (data[i] < 255)
968                                         {
969                                                 flags |= TEXF_ALPHA;
970                                                 break;
971                                         }
972                                 }
973                         }
974                 }
975                 break;
976         default:
977                 Host_Error("R_LoadTexture: unknown texture type");
978         }
979
980         glt = (gltexture_t *)Mem_Alloc(texturemempool, sizeof(gltexture_t));
981         if (identifier)
982                 strlcpy (glt->identifier, identifier, sizeof(glt->identifier));
983         glt->pool = pool;
984         glt->chain = pool->gltchain;
985         pool->gltchain = glt;
986         glt->inputwidth = width;
987         glt->inputheight = height;
988         glt->inputdepth = depth;
989         glt->flags = flags | GLTEXF_UPLOAD;
990         glt->textype = texinfo;
991         glt->texturetype = texturetype;
992         glt->inputdatasize = size;
993         glt->palette = palette;
994         glt->glinternalformat = texinfo->glinternalformat;
995         glt->glformat = texinfo->glformat;
996         glt->bytesperpixel = texinfo->internalbytesperpixel;
997         glt->sides = glt->texturetype == GLTEXTURETYPE_CUBEMAP ? 6 : 1;
998         glt->texnum = -1;
999
1000         if (data)
1001         {
1002                 glt->inputtexels = (unsigned char *)Mem_Alloc(texturemempool, size);
1003                 memcpy(glt->inputtexels, data, size);
1004         }
1005         else
1006                 glt->inputtexels = NULL;
1007
1008         GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->inputwidth, glt->inputheight, glt->inputdepth, &glt->tilewidth, &glt->tileheight, &glt->tiledepth);
1009         R_PrecacheTexture(glt);
1010
1011         return (rtexture_t *)glt;
1012 }
1013
1014 rtexture_t *R_LoadTexture1D(rtexturepool_t *rtexturepool, const char *identifier, int width, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1015 {
1016         return R_SetupTexture(rtexturepool, identifier, width, 1, 1, 1, flags, textype, GLTEXTURETYPE_1D, data, palette);
1017 }
1018
1019 rtexture_t *R_LoadTexture2D(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1020 {
1021         return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, flags, textype, GLTEXTURETYPE_2D, data, palette);
1022 }
1023
1024 rtexture_t *R_LoadTexture3D(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, int depth, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1025 {
1026         return R_SetupTexture(rtexturepool, identifier, width, height, depth, 1, flags, textype, GLTEXTURETYPE_3D, data, palette);
1027 }
1028
1029 rtexture_t *R_LoadTextureCubeMap(rtexturepool_t *rtexturepool, const char *identifier, int width, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1030 {
1031         return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, flags, textype, GLTEXTURETYPE_CUBEMAP, data, palette);
1032 }
1033
1034 int R_TextureHasAlpha(rtexture_t *rt)
1035 {
1036         return rt ? (((gltexture_t *)rt)->flags & TEXF_ALPHA) != 0 : false;
1037 }
1038
1039 int R_TextureWidth(rtexture_t *rt)
1040 {
1041         return rt ? ((gltexture_t *)rt)->inputwidth : 0;
1042 }
1043
1044 int R_TextureHeight(rtexture_t *rt)
1045 {
1046         return rt ? ((gltexture_t *)rt)->inputheight : 0;
1047 }
1048
1049 void R_UpdateTexture(rtexture_t *rt, unsigned char *data, int x, int y, int width, int height)
1050 {
1051         gltexture_t *glt;
1052         if (rt == NULL)
1053                 Host_Error("R_UpdateTexture: no texture supplied");
1054         if (data == NULL)
1055                 Host_Error("R_UpdateTexture: no data supplied");
1056         glt = (gltexture_t *)rt;
1057
1058         // we need it to be uploaded before we can update a part of it
1059         if (glt->flags & GLTEXF_UPLOAD)
1060                 R_UploadTexture(glt);
1061
1062         // update part of the texture
1063         R_Upload(glt, data, x, y, 0, width, height, 1);
1064 }
1065