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