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