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