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