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