changed reporting of compressed RGB textures to use 0.5 bytes per pixel
[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 }
207
208 static void R_UploadTexture(gltexture_t *t);
209
210 static void R_PrecacheTexture(gltexture_t *glt)
211 {
212         int precache;
213         precache = false;
214         if (glt->flags & TEXF_ALWAYSPRECACHE)
215                 precache = true;
216         else if (r_precachetextures.integer >= 2)
217                 precache = true;
218         else if (r_precachetextures.integer >= 1)
219                 if (glt->flags & TEXF_PRECACHE)
220                         precache = true;
221
222         if (precache)
223                 R_UploadTexture(glt);
224 }
225
226 int R_RealGetTexture(rtexture_t *rt)
227 {
228         if (rt)
229         {
230                 gltexture_t *glt;
231                 glt = (gltexture_t *)rt;
232                 if (glt->flags & GLTEXF_UPLOAD)
233                         R_UploadTexture(glt);
234                 return glt->texnum;
235         }
236         else
237                 return 0;
238 }
239
240 void R_FreeTexture(rtexture_t *rt)
241 {
242         gltexture_t *glt, **gltpointer;
243
244         glt = (gltexture_t *)rt;
245         if (glt == NULL)
246                 Host_Error("R_FreeTexture: texture == NULL");
247
248         for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
249         if (*gltpointer == glt)
250                 *gltpointer = glt->chain;
251         else
252                 Host_Error("R_FreeTexture: texture \"%s\" not linked in pool", glt->identifier);
253
254         if (glt->texnum)
255         {
256                 CHECKGLERROR
257                 qglDeleteTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
258         }
259
260         if (glt->inputtexels)
261                 Mem_Free(glt->inputtexels);
262         Mem_Free(glt);
263 }
264
265 rtexturepool_t *R_AllocTexturePool(void)
266 {
267         gltexturepool_t *pool;
268         if (texturemempool == NULL)
269                 return NULL;
270         pool = (gltexturepool_t *)Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
271         if (pool == NULL)
272                 return NULL;
273         pool->next = gltexturepoolchain;
274         gltexturepoolchain = pool;
275         pool->sentinel = TEXTUREPOOL_SENTINEL;
276         return (rtexturepool_t *)pool;
277 }
278
279 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
280 {
281         gltexturepool_t *pool, **poolpointer;
282         if (rtexturepool == NULL)
283                 return;
284         if (*rtexturepool == NULL)
285                 return;
286         pool = (gltexturepool_t *)(*rtexturepool);
287         *rtexturepool = NULL;
288         if (pool->sentinel != TEXTUREPOOL_SENTINEL)
289                 Host_Error("R_FreeTexturePool: pool already freed");
290         for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
291         if (*poolpointer == pool)
292                 *poolpointer = pool->next;
293         else
294                 Host_Error("R_FreeTexturePool: pool not linked");
295         while (pool->gltchain)
296                 R_FreeTexture((rtexture_t *)pool->gltchain);
297         Mem_Free(pool);
298 }
299
300
301 typedef struct glmode_s
302 {
303         char *name;
304         int minification, magnification;
305 }
306 glmode_t;
307
308 static glmode_t modes[6] =
309 {
310         {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
311         {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
312         {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
313         {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
314         {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
315         {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
316 };
317
318 static void GL_TextureMode_f (void)
319 {
320         int i;
321         GLint oldbindtexnum;
322         gltexture_t *glt;
323         gltexturepool_t *pool;
324
325         if (Cmd_Argc() == 1)
326         {
327                 for (i = 0;i < 6;i++)
328                 {
329                         if (gl_filter_min == modes[i].minification)
330                         {
331                                 Con_Printf("%s\n", modes[i].name);
332                                 return;
333                         }
334                 }
335                 Con_Print("current filter is unknown???\n");
336                 return;
337         }
338
339         for (i = 0;i < 6;i++)
340                 if (!strcasecmp (modes[i].name, Cmd_Argv(1) ) )
341                         break;
342         if (i == 6)
343         {
344                 Con_Print("bad filter name\n");
345                 return;
346         }
347
348         gl_filter_min = modes[i].minification;
349         gl_filter_mag = modes[i].magnification;
350
351         // change all the existing mipmap texture objects
352         // FIXME: force renderer(/client/something?) restart instead?
353         CHECKGLERROR
354         for (pool = gltexturepoolchain;pool;pool = pool->next)
355         {
356                 for (glt = pool->gltchain;glt;glt = glt->chain)
357                 {
358                         // only update already uploaded images
359                         if (!(glt->flags & (GLTEXF_UPLOAD | TEXF_FORCENEAREST | TEXF_FORCELINEAR)))
360                         {
361                                 qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
362                                 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
363                                 if (glt->flags & TEXF_MIPMAP)
364                                 {
365                                         qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
366                                 }
367                                 else
368                                 {
369                                         qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
370                                 }
371                                 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
372                                 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
373                         }
374                 }
375         }
376 }
377
378 static void GL_Texture_CalcImageSize(int texturetype, int flags, int inwidth, int inheight, int indepth, int *outwidth, int *outheight, int *outdepth)
379 {
380         int picmip = 0, maxsize = 0, width2 = 1, height2 = 1, depth2 = 1;
381
382         if (gl_max_size.integer > gl_max_texture_size)
383                 Cvar_SetValue("gl_max_size", gl_max_texture_size);
384
385         switch (texturetype)
386         {
387         default:
388         case GLTEXTURETYPE_1D:
389         case GLTEXTURETYPE_2D:
390                 maxsize = gl_max_texture_size;
391                 break;
392         case GLTEXTURETYPE_3D:
393                 maxsize = gl_max_3d_texture_size;
394                 break;
395         case GLTEXTURETYPE_CUBEMAP:
396                 maxsize = gl_max_cube_map_texture_size;
397                 break;
398         }
399
400         if (flags & TEXF_PICMIP)
401         {
402                 maxsize = min(maxsize, gl_max_size.integer);
403                 picmip = gl_picmip.integer;
404         }
405
406         if (outwidth)
407         {
408                 if (gl_support_arb_texture_non_power_of_two)
409                         width2 = min(inwidth >> picmip, maxsize);
410                 else
411                 {
412                         for (width2 = 1;width2 < inwidth;width2 <<= 1);
413                         for (width2 >>= picmip;width2 > maxsize;width2 >>= 1);
414                 }
415                 *outwidth = max(1, width2);
416         }
417         if (outheight)
418         {
419                 if (gl_support_arb_texture_non_power_of_two)
420                         height2 = min(inheight >> picmip, maxsize);
421                 else
422                 {
423                         for (height2 = 1;height2 < inheight;height2 <<= 1);
424                         for (height2 >>= picmip;height2 > maxsize;height2 >>= 1);
425                 }
426                 *outheight = max(1, height2);
427         }
428         if (outdepth)
429         {
430                 if (gl_support_arb_texture_non_power_of_two)
431                         depth2 = min(indepth >> picmip, maxsize);
432                 else
433                 {
434                         for (depth2 = 1;depth2 < indepth;depth2 <<= 1);
435                         for (depth2 >>= picmip;depth2 > maxsize;depth2 >>= 1);
436                 }
437                 *outdepth = max(1, depth2);
438         }
439 }
440
441
442 static int R_CalcTexelDataSize (gltexture_t *glt)
443 {
444         int width2, height2, depth2, size;
445
446         GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->inputwidth, glt->inputheight, glt->inputdepth, &width2, &height2, &depth2);
447
448         size = width2 * height2 * depth2;
449
450         if (glt->flags & TEXF_MIPMAP)
451         {
452                 while (width2 > 1 || height2 > 1 || depth2 > 1)
453                 {
454                         if (width2 > 1)
455                                 width2 >>= 1;
456                         if (height2 > 1)
457                                 height2 >>= 1;
458                         if (depth2 > 1)
459                                 depth2 >>= 1;
460                         size += width2 * height2 * depth2;
461                 }
462         }
463
464         return (int)(size * glt->textype->glinternalbytesperpixel) * glt->sides;
465 }
466
467 void R_TextureStats_Print(qboolean printeach, qboolean printpool, qboolean printtotal)
468 {
469         int glsize;
470         int isloaded;
471         int pooltotal = 0, pooltotalt = 0, pooltotalp = 0, poolloaded = 0, poolloadedt = 0, poolloadedp = 0;
472         int sumtotal = 0, sumtotalt = 0, sumtotalp = 0, sumloaded = 0, sumloadedt = 0, sumloadedp = 0;
473         gltexture_t *glt;
474         gltexturepool_t *pool;
475         if (printeach)
476                 Con_Print("glsize input loaded mip alpha name\n");
477         for (pool = gltexturepoolchain;pool;pool = pool->next)
478         {
479                 pooltotal = 0;
480                 pooltotalt = 0;
481                 pooltotalp = 0;
482                 poolloaded = 0;
483                 poolloadedt = 0;
484                 poolloadedp = 0;
485                 for (glt = pool->gltchain;glt;glt = glt->chain)
486                 {
487                         glsize = R_CalcTexelDataSize(glt);
488                         isloaded = !(glt->flags & GLTEXF_UPLOAD);
489                         pooltotal++;
490                         pooltotalt += glsize;
491                         pooltotalp += glt->inputdatasize;
492                         if (isloaded)
493                         {
494                                 poolloaded++;
495                                 poolloadedt += glsize;
496                                 poolloadedp += glt->inputdatasize;
497                         }
498                         if (printeach)
499                                 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);
500                 }
501                 if (printpool)
502                         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);
503                 sumtotal += pooltotal;
504                 sumtotalt += pooltotalt;
505                 sumtotalp += pooltotalp;
506                 sumloaded += poolloaded;
507                 sumloadedt += poolloadedt;
508                 sumloadedp += poolloadedp;
509         }
510         if (printtotal)
511                 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);
512 }
513
514 static void R_TextureStats_f(void)
515 {
516         R_TextureStats_Print(true, true, true);
517 }
518
519 static void r_textures_start(void)
520 {
521         // LordHavoc: allow any alignment
522         CHECKGLERROR
523         qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);CHECKGLERROR
524         qglPixelStorei(GL_PACK_ALIGNMENT, 1);CHECKGLERROR
525
526         texturemempool = Mem_AllocPool("texture management", 0, NULL);
527
528         // Disable JPEG screenshots if the DLL isn't loaded
529         if (! JPEG_OpenLibrary ())
530                 Cvar_SetValueQuick (&scr_screenshot_jpeg, 0);
531         // TODO: support png screenshots?
532         PNG_OpenLibrary ();
533 }
534
535 static void r_textures_shutdown(void)
536 {
537         rtexturepool_t *temp;
538
539         JPEG_CloseLibrary ();
540
541         while(gltexturepoolchain)
542         {
543                 temp = (rtexturepool_t *) gltexturepoolchain;
544                 R_FreeTexturePool(&temp);
545         }
546
547         resizebuffersize = 0;
548         texturebuffersize = 0;
549         resizebuffer = NULL;
550         colorconvertbuffer = NULL;
551         texturebuffer = NULL;
552         Mem_FreePool(&texturemempool);
553 }
554
555 static void r_textures_newmap(void)
556 {
557 }
558
559 void R_Textures_Init (void)
560 {
561         Cmd_AddCommand("gl_texturemode", &GL_TextureMode_f, "set texture filtering mode (GL_NEAREST, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, etc)");
562         Cmd_AddCommand("r_texturestats", R_TextureStats_f, "print information about all loaded textures and some statistics");
563         Cvar_RegisterVariable (&gl_max_size);
564         Cvar_RegisterVariable (&gl_picmip);
565         Cvar_RegisterVariable (&r_lerpimages);
566         Cvar_RegisterVariable (&r_precachetextures);
567         Cvar_RegisterVariable (&gl_texture_anisotropy);
568         Cvar_RegisterVariable (&gl_texturecompression);
569         Cvar_RegisterVariable (&gl_texturecompression_color);
570         Cvar_RegisterVariable (&gl_texturecompression_normal);
571         Cvar_RegisterVariable (&gl_texturecompression_gloss);
572         Cvar_RegisterVariable (&gl_texturecompression_glow);
573         Cvar_RegisterVariable (&gl_texturecompression_2d);
574         Cvar_RegisterVariable (&gl_texturecompression_q3bsplightmaps);
575         Cvar_RegisterVariable (&gl_texturecompression_q3bspdeluxemaps);
576         Cvar_RegisterVariable (&gl_texturecompression_sky);
577         Cvar_RegisterVariable (&gl_texturecompression_lightcubemaps);
578
579         R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
580 }
581
582 void R_Textures_Frame (void)
583 {
584         static int old_aniso = 0;
585
586         // could do procedural texture animation here, if we keep track of which
587         // textures were accessed this frame...
588
589         // free the resize buffers
590         resizebuffersize = 0;
591         if (resizebuffer)
592         {
593                 Mem_Free(resizebuffer);
594                 resizebuffer = NULL;
595         }
596         if (colorconvertbuffer)
597         {
598                 Mem_Free(colorconvertbuffer);
599                 colorconvertbuffer = NULL;
600         }
601
602         if (old_aniso != gl_texture_anisotropy.integer)
603         {
604                 gltexture_t *glt;
605                 gltexturepool_t *pool;
606                 GLint oldbindtexnum;
607
608                 old_aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
609
610                 Cvar_SetValueQuick(&gl_texture_anisotropy, old_aniso);
611
612                 CHECKGLERROR
613                 for (pool = gltexturepoolchain;pool;pool = pool->next)
614                 {
615                         for (glt = pool->gltchain;glt;glt = glt->chain)
616                         {
617                                 // only update already uploaded images
618                                 if ((glt->flags & (GLTEXF_UPLOAD | TEXF_MIPMAP)) == TEXF_MIPMAP)
619                                 {
620                                         qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
621
622                                         qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
623                                         qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAX_ANISOTROPY_EXT, old_aniso);CHECKGLERROR
624
625                                         qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
626                                 }
627                         }
628                 }
629         }
630 }
631
632 void R_MakeResizeBufferBigger(int size)
633 {
634         if (resizebuffersize < size)
635         {
636                 resizebuffersize = size;
637                 if (resizebuffer)
638                         Mem_Free(resizebuffer);
639                 if (colorconvertbuffer)
640                         Mem_Free(colorconvertbuffer);
641                 resizebuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
642                 colorconvertbuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
643                 if (!resizebuffer || !colorconvertbuffer)
644                         Host_Error("R_Upload: out of memory");
645         }
646 }
647
648 static void GL_SetupTextureParameters(int flags, int texturetype)
649 {
650         int textureenum = gltexturetypeenums[texturetype];
651         int wrapmode = ((flags & TEXF_CLAMP) && gl_support_clamptoedge) ? GL_CLAMP_TO_EDGE : GL_REPEAT;
652
653         CHECKGLERROR
654
655         if (gl_support_anisotropy && (flags & TEXF_MIPMAP))
656         {
657                 int aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
658                 if (gl_texture_anisotropy.integer != aniso)
659                         Cvar_SetValueQuick(&gl_texture_anisotropy, aniso);
660                 qglTexParameteri(textureenum, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);CHECKGLERROR
661         }
662         qglTexParameteri(textureenum, GL_TEXTURE_WRAP_S, wrapmode);CHECKGLERROR
663         qglTexParameteri(textureenum, GL_TEXTURE_WRAP_T, wrapmode);CHECKGLERROR
664         if (gltexturetypedimensions[texturetype] >= 3)
665         {
666                 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_R, wrapmode);CHECKGLERROR
667         }
668
669         CHECKGLERROR
670         if (flags & TEXF_FORCENEAREST)
671         {
672                 if (flags & TEXF_MIPMAP)
673                 {
674                         qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);CHECKGLERROR
675                 }
676                 else
677                 {
678                         qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST);CHECKGLERROR
679                 }
680                 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_NEAREST);CHECKGLERROR
681         }
682         else if (flags & TEXF_FORCELINEAR)
683         {
684                 if (flags & TEXF_MIPMAP)
685                 {
686                         if (gl_filter_min == GL_NEAREST_MIPMAP_LINEAR || gl_filter_min == GL_LINEAR_MIPMAP_LINEAR)
687                         {
688                                 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);CHECKGLERROR
689                         }
690                         else
691                         {
692                                 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);CHECKGLERROR
693                         }
694                 }
695                 else
696                 {
697                         qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR);CHECKGLERROR
698                 }
699                 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_LINEAR);CHECKGLERROR
700         }
701         else
702         {
703                 if (flags & TEXF_MIPMAP)
704                 {
705                         qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
706                 }
707                 else
708                 {
709                         qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
710                 }
711                 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
712         }
713
714         CHECKGLERROR
715 }
716
717 static void R_Upload(gltexture_t *glt, unsigned char *data, int fragx, int fragy, int fragz, int fragwidth, int fragheight, int fragdepth)
718 {
719         int i, mip, width, height, depth;
720         GLint oldbindtexnum;
721         unsigned char *prevbuffer;
722         prevbuffer = data;
723
724         CHECKGLERROR
725
726         // we need to restore the texture binding after finishing the upload
727         qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
728         qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
729
730         // these are rounded up versions of the size to do better resampling
731         if (gl_support_arb_texture_non_power_of_two)
732         {
733                 width = glt->inputwidth;
734                 height = glt->inputheight;
735                 depth = glt->inputdepth;
736         }
737         else
738         {
739                 for (width  = 1;width  < glt->inputwidth ;width  <<= 1);
740                 for (height = 1;height < glt->inputheight;height <<= 1);
741                 for (depth  = 1;depth  < glt->inputdepth ;depth  <<= 1);
742         }
743
744         R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel);
745         R_MakeResizeBufferBigger(fragwidth * fragheight * fragdepth * glt->sides * glt->bytesperpixel);
746
747         if (prevbuffer == NULL)
748         {
749                 memset(resizebuffer, 0, fragwidth * fragheight * fragdepth * glt->bytesperpixel);
750                 prevbuffer = resizebuffer;
751         }
752         else if (glt->textype->textype == TEXTYPE_PALETTE)
753         {
754                 // promote paletted to RGBA, so we only have to worry about RGB and
755                 // RGBA in the rest of this code
756                 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, fragwidth * fragheight * fragdepth * glt->sides, glt->palette);
757                 prevbuffer = colorconvertbuffer;
758         }
759
760         if ((glt->flags & (TEXF_MIPMAP | TEXF_PICMIP | GLTEXF_UPLOAD)) == 0 && glt->inputwidth == glt->tilewidth && glt->inputheight == glt->tileheight && glt->inputdepth == glt->tiledepth)
761         {
762                 // update a portion of the image
763                 switch(glt->texturetype)
764                 {
765                 case GLTEXTURETYPE_1D:
766                         qglTexSubImage1D(GL_TEXTURE_1D, 0, fragx, fragwidth, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
767                         break;
768                 case GLTEXTURETYPE_2D:
769                         qglTexSubImage2D(GL_TEXTURE_2D, 0, fragx, fragy, fragwidth, fragheight, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
770                         break;
771                 case GLTEXTURETYPE_3D:
772                         qglTexSubImage3D(GL_TEXTURE_3D, 0, fragx, fragy, fragz, fragwidth, fragheight, fragdepth, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
773                         break;
774                 default:
775                         Host_Error("R_Upload: partial update of type other than 1D, 2D, or 3D");
776                         break;
777                 }
778         }
779         else
780         {
781                 if (fragx || fragy || fragz || glt->inputwidth != fragwidth || glt->inputheight != fragheight || glt->inputdepth != fragdepth)
782                         Host_Error("R_Upload: partial update not allowed on initial upload or in combination with PICMIP or MIPMAP\n");
783
784                 // upload the image for the first time
785                 glt->flags &= ~GLTEXF_UPLOAD;
786
787                 // cubemaps contain multiple images and thus get processed a bit differently
788                 if (glt->texturetype != GLTEXTURETYPE_CUBEMAP)
789                 {
790                         if (glt->inputwidth != width || glt->inputheight != height || glt->inputdepth != depth)
791                         {
792                                 Image_Resample(prevbuffer, glt->inputwidth, glt->inputheight, glt->inputdepth, resizebuffer, width, height, depth, glt->bytesperpixel, r_lerpimages.integer);
793                                 prevbuffer = resizebuffer;
794                         }
795                         // picmip/max_size
796                         while (width > glt->tilewidth || height > glt->tileheight || depth > glt->tiledepth)
797                         {
798                                 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->tilewidth, glt->tileheight, glt->tiledepth, glt->bytesperpixel);
799                                 prevbuffer = resizebuffer;
800                         }
801                 }
802                 mip = 0;
803                 if (gl_support_texture_compression)
804                 {
805                         if (gl_texturecompression.integer >= 2)
806                                 qglHint(GL_TEXTURE_COMPRESSION_HINT_ARB, GL_NICEST);
807                         else
808                                 qglHint(GL_TEXTURE_COMPRESSION_HINT_ARB, GL_FASTEST);
809                         CHECKGLERROR
810                 }
811                 switch(glt->texturetype)
812                 {
813                 case GLTEXTURETYPE_1D:
814                         qglTexImage1D(GL_TEXTURE_1D, mip++, glt->glinternalformat, width, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
815                         if (glt->flags & TEXF_MIPMAP)
816                         {
817                                 while (width > 1 || height > 1 || depth > 1)
818                                 {
819                                         Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->bytesperpixel);
820                                         prevbuffer = resizebuffer;
821                                         qglTexImage1D(GL_TEXTURE_1D, mip++, glt->glinternalformat, width, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
822                                 }
823                         }
824                         break;
825                 case GLTEXTURETYPE_2D:
826                         qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
827                         if (glt->flags & TEXF_MIPMAP)
828                         {
829                                 while (width > 1 || height > 1 || depth > 1)
830                                 {
831                                         Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->bytesperpixel);
832                                         prevbuffer = resizebuffer;
833                                         qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
834                                 }
835                         }
836                         break;
837                 case GLTEXTURETYPE_3D:
838                         qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
839                         if (glt->flags & TEXF_MIPMAP)
840                         {
841                                 while (width > 1 || height > 1 || depth > 1)
842                                 {
843                                         Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->bytesperpixel);
844                                         prevbuffer = resizebuffer;
845                                         qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
846                                 }
847                         }
848                         break;
849                 case GLTEXTURETYPE_CUBEMAP:
850                         // convert and upload each side in turn,
851                         // from a continuous block of input texels
852                         texturebuffer = prevbuffer;
853                         for (i = 0;i < 6;i++)
854                         {
855                                 prevbuffer = texturebuffer;
856                                 texturebuffer += glt->inputwidth * glt->inputheight * glt->inputdepth * glt->textype->inputbytesperpixel;
857                                 if (glt->inputwidth != width || glt->inputheight != height || glt->inputdepth != depth)
858                                 {
859                                         Image_Resample(prevbuffer, glt->inputwidth, glt->inputheight, glt->inputdepth, resizebuffer, width, height, depth, glt->bytesperpixel, r_lerpimages.integer);
860                                         prevbuffer = resizebuffer;
861                                 }
862                                 // picmip/max_size
863                                 while (width > glt->tilewidth || height > glt->tileheight || depth > glt->tiledepth)
864                                 {
865                                         Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->tilewidth, glt->tileheight, glt->tiledepth, glt->bytesperpixel);
866                                         prevbuffer = resizebuffer;
867                                 }
868                                 mip = 0;
869                                 qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
870                                 if (glt->flags & TEXF_MIPMAP)
871                                 {
872                                         while (width > 1 || height > 1 || depth > 1)
873                                         {
874                                                 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->bytesperpixel);
875                                                 prevbuffer = resizebuffer;
876                                                 qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
877                                         }
878                                 }
879                         }
880                         break;
881                 }
882                 GL_SetupTextureParameters(glt->flags, glt->texturetype);
883         }
884         qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
885 }
886
887 static void R_UploadTexture (gltexture_t *glt)
888 {
889         if (!(glt->flags & GLTEXF_UPLOAD))
890                 return;
891
892         CHECKGLERROR
893         qglGenTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
894         R_Upload(glt, glt->inputtexels, 0, 0, 0, glt->inputwidth, glt->inputheight, glt->inputdepth);
895         if (glt->inputtexels)
896         {
897                 Mem_Free(glt->inputtexels);
898                 glt->inputtexels = NULL;
899                 glt->flags |= GLTEXF_DESTROYED;
900         }
901         else if (glt->flags & GLTEXF_DESTROYED)
902                 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed.  Can not upload original image again.  Uploaded blank texture.\n", glt->identifier);
903 }
904
905 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)
906 {
907         int i, size;
908         gltexture_t *glt;
909         gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
910         textypeinfo_t *texinfo;
911
912         if (cls.state == ca_dedicated)
913                 return NULL;
914
915         if (texturetype == GLTEXTURETYPE_CUBEMAP && !gl_texturecubemap)
916         {
917                 Con_Printf ("R_LoadTexture: cubemap texture not supported by driver\n");
918                 return NULL;
919         }
920         if (texturetype == GLTEXTURETYPE_3D && !gl_texture3d)
921         {
922                 Con_Printf ("R_LoadTexture: 3d texture not supported by driver\n");
923                 return NULL;
924         }
925
926         texinfo = R_GetTexTypeInfo(textype, flags);
927         size = width * height * depth * sides * texinfo->inputbytesperpixel;
928         if (size < 1)
929         {
930                 Con_Printf ("R_LoadTexture: bogus texture size (%dx%dx%dx%dbppx%dsides = %d bytes)\n", width, height, depth, texinfo->inputbytesperpixel * 8, sides, size);
931                 return NULL;
932         }
933
934         // clear the alpha flag if the texture has no transparent pixels
935         switch(textype)
936         {
937         case TEXTYPE_PALETTE:
938                 if (flags & TEXF_ALPHA)
939                 {
940                         flags &= ~TEXF_ALPHA;
941                         if (data)
942                         {
943                                 for (i = 0;i < size;i++)
944                                 {
945                                         if (((unsigned char *)&palette[data[i]])[3] < 255)
946                                         {
947                                                 flags |= TEXF_ALPHA;
948                                                 break;
949                                         }
950                                 }
951                         }
952                 }
953                 break;
954         case TEXTYPE_RGB:
955                 if (flags & TEXF_ALPHA)
956                         Host_Error("R_LoadTexture: RGB has no alpha, don't specify TEXF_ALPHA");
957                 break;
958         case TEXTYPE_RGBA:
959                 if (flags & TEXF_ALPHA)
960                 {
961                         flags &= ~TEXF_ALPHA;
962                         if (data)
963                         {
964                                 for (i = 3;i < size;i += 4)
965                                 {
966                                         if (data[i] < 255)
967                                         {
968                                                 flags |= TEXF_ALPHA;
969                                                 break;
970                                         }
971                                 }
972                         }
973                 }
974                 break;
975         default:
976                 Host_Error("R_LoadTexture: unknown texture type");
977         }
978
979         glt = (gltexture_t *)Mem_Alloc(texturemempool, sizeof(gltexture_t));
980         if (identifier)
981                 strlcpy (glt->identifier, identifier, sizeof(glt->identifier));
982         glt->pool = pool;
983         glt->chain = pool->gltchain;
984         pool->gltchain = glt;
985         glt->inputwidth = width;
986         glt->inputheight = height;
987         glt->inputdepth = depth;
988         glt->flags = flags | GLTEXF_UPLOAD;
989         glt->textype = texinfo;
990         glt->texturetype = texturetype;
991         glt->inputdatasize = size;
992         glt->palette = palette;
993         glt->glinternalformat = texinfo->glinternalformat;
994         glt->glformat = texinfo->glformat;
995         glt->bytesperpixel = texinfo->internalbytesperpixel;
996         glt->sides = glt->texturetype == GLTEXTURETYPE_CUBEMAP ? 6 : 1;
997         glt->texnum = -1;
998
999         if (data)
1000         {
1001                 glt->inputtexels = (unsigned char *)Mem_Alloc(texturemempool, size);
1002                 memcpy(glt->inputtexels, data, size);
1003         }
1004         else
1005                 glt->inputtexels = NULL;
1006
1007         GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->inputwidth, glt->inputheight, glt->inputdepth, &glt->tilewidth, &glt->tileheight, &glt->tiledepth);
1008         R_PrecacheTexture(glt);
1009
1010         return (rtexture_t *)glt;
1011 }
1012
1013 rtexture_t *R_LoadTexture1D(rtexturepool_t *rtexturepool, const char *identifier, int width, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1014 {
1015         return R_SetupTexture(rtexturepool, identifier, width, 1, 1, 1, flags, textype, GLTEXTURETYPE_1D, data, palette);
1016 }
1017
1018 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)
1019 {
1020         return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, flags, textype, GLTEXTURETYPE_2D, data, palette);
1021 }
1022
1023 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)
1024 {
1025         return R_SetupTexture(rtexturepool, identifier, width, height, depth, 1, flags, textype, GLTEXTURETYPE_3D, data, palette);
1026 }
1027
1028 rtexture_t *R_LoadTextureCubeMap(rtexturepool_t *rtexturepool, const char *identifier, int width, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1029 {
1030         return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, flags, textype, GLTEXTURETYPE_CUBEMAP, data, palette);
1031 }
1032
1033 int R_TextureHasAlpha(rtexture_t *rt)
1034 {
1035         return rt ? (((gltexture_t *)rt)->flags & TEXF_ALPHA) != 0 : false;
1036 }
1037
1038 int R_TextureWidth(rtexture_t *rt)
1039 {
1040         return rt ? ((gltexture_t *)rt)->inputwidth : 0;
1041 }
1042
1043 int R_TextureHeight(rtexture_t *rt)
1044 {
1045         return rt ? ((gltexture_t *)rt)->inputheight : 0;
1046 }
1047
1048 void R_UpdateTexture(rtexture_t *rt, unsigned char *data, int x, int y, int width, int height)
1049 {
1050         gltexture_t *glt;
1051         if (rt == NULL)
1052                 Host_Error("R_UpdateTexture: no texture supplied");
1053         if (data == NULL)
1054                 Host_Error("R_UpdateTexture: no data supplied");
1055         glt = (gltexture_t *)rt;
1056
1057         // we need it to be uploaded before we can update a part of it
1058         if (glt->flags & GLTEXF_UPLOAD)
1059                 R_UploadTexture(glt);
1060
1061         // update part of the texture
1062         R_Upload(glt, data, x, y, 0, width, height, 1);
1063 }
1064