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