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