made gl_max_size cvar only affect TEXF_PICMIP textures, this prevents it from breakin...
[divverent/darkplaces.git] / gl_textures.c
1
2 #include "quakedef.h"
3 #include "image.h"
4 #include "jpeg.h"
5
6 cvar_t  gl_max_size = {CVAR_SAVE, "gl_max_size", "2048"};
7 cvar_t  gl_max_scrapsize = {CVAR_SAVE, "gl_max_scrapsize", "256"};
8 cvar_t  gl_picmip = {CVAR_SAVE, "gl_picmip", "0"};
9 cvar_t  r_lerpimages = {CVAR_SAVE, "r_lerpimages", "1"};
10 cvar_t  r_precachetextures = {CVAR_SAVE, "r_precachetextures", "1"};
11 cvar_t  gl_texture_anisotropy = {CVAR_SAVE, "gl_texture_anisotropy", "1"};
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 // size of images which hold fragment textures, ignores picmip and max_size
28 static int block_size;
29
30 typedef struct textypeinfo_s
31 {
32         int textype;
33         int inputbytesperpixel;
34         int internalbytesperpixel;
35         int glformat;
36         int glinternalformat;
37 }
38 textypeinfo_t;
39
40 static textypeinfo_t textype_palette       = {TEXTYPE_PALETTE, 1, 4, GL_RGBA   , 3};
41 static textypeinfo_t textype_rgb           = {TEXTYPE_RGB    , 3, 3, GL_RGB    , 3};
42 static textypeinfo_t textype_rgba          = {TEXTYPE_RGBA   , 4, 4, GL_RGBA   , 3};
43 static textypeinfo_t textype_palette_alpha = {TEXTYPE_PALETTE, 1, 4, GL_RGBA   , 4};
44 static textypeinfo_t textype_rgba_alpha    = {TEXTYPE_RGBA   , 4, 4, GL_RGBA   , 4};
45 static textypeinfo_t textype_dsdt          = {TEXTYPE_DSDT   , 2, 2, GL_DSDT_NV, GL_DSDT8_NV};
46
47 // a tiling texture (most common type)
48 #define GLIMAGETYPE_TILE 0
49 // a fragments texture (contains one or more fragment textures)
50 #define GLIMAGETYPE_FRAGMENTS 1
51
52 #define GLTEXTURETYPE_1D 0
53 #define GLTEXTURETYPE_2D 1
54 #define GLTEXTURETYPE_3D 2
55 #define GLTEXTURETYPE_CUBEMAP 3
56
57 static int gltexturetypeenums[4] = {GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D, GL_TEXTURE_CUBE_MAP_ARB};
58 static int gltexturetypebindingenums[4] = {GL_TEXTURE_BINDING_1D, GL_TEXTURE_BINDING_2D, GL_TEXTURE_BINDING_3D, GL_TEXTURE_BINDING_CUBE_MAP_ARB};
59 static int gltexturetypedimensions[4] = {1, 2, 3, 2};
60 static int cubemapside[6] =
61 {
62         GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
63         GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
64         GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
65         GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
66         GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
67         GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
68 };
69
70 // a gltextureimage can have one (or more if fragments) gltextures inside
71 typedef struct gltextureimage_s
72 {
73         struct gltextureimage_s *imagechain;
74         int texturecount;
75         int type; // one of the GLIMAGETYPE_ values
76         int texturetype; // one of the GLTEXTURETYPE_ values
77         int sides; // 1 or 6 depending on texturetype
78         int texnum; // GL texture slot number
79         int width, height, depth; // 3D texture support
80         int bytesperpixel; // bytes per pixel
81         int glformat; // GL_RGB or GL_RGBA
82         int glinternalformat; // 3 or 4
83         int flags;
84         short *blockallocation; // fragment allocation (2D only)
85 }
86 gltextureimage_t;
87
88 typedef struct gltexture_s
89 {
90         // this field is exposed to the R_GetTexture macro, for speed reasons
91         // (must be identical in rtexture_t)
92         int texnum; // GL texture slot number
93
94         // pointer to texturepool (check this to see if the texture is allocated)
95         struct gltexturepool_s *pool;
96         // pointer to next texture in texturepool chain
97         struct gltexture_s *chain;
98         // pointer into gltextureimage array
99         gltextureimage_t *image;
100         // name of the texture (this might be removed someday), no duplicates
101         char identifier[32];
102         // location in the image, and size
103         int x, y, z, width, height, depth;
104         // copy of the original texture(s) supplied to the upload function, for
105         // delayed uploads (non-precached)
106         unsigned char *inputtexels;
107         // original data size in *inputtexels
108         int inputdatasize;
109         // flags supplied to the LoadTexture function
110         // (might be altered to remove TEXF_ALPHA), and GLTEXF_ private flags
111         int flags;
112         // pointer to one of the textype_ structs
113         textypeinfo_t *textype;
114         // one of the GLTEXTURETYPE_ values
115         int texturetype;
116         // palette if the texture is TEXTYPE_PALETTE
117         const unsigned int *palette;
118 }
119 gltexture_t;
120
121 #define TEXTUREPOOL_SENTINEL 0xC0DEDBAD
122
123 typedef struct gltexturepool_s
124 {
125         unsigned int sentinel;
126         struct gltextureimage_s *imagechain;
127         struct gltexture_s *gltchain;
128         struct gltexturepool_s *next;
129 }
130 gltexturepool_t;
131
132 static gltexturepool_t *gltexturepoolchain = NULL;
133
134 static unsigned char *resizebuffer = NULL, *colorconvertbuffer;
135 static int resizebuffersize = 0;
136 static unsigned char *texturebuffer;
137 static int texturebuffersize = 0;
138
139 static textypeinfo_t *R_GetTexTypeInfo(int textype, int flags)
140 {
141         if ((flags & (TEXF_PICMIP | TEXF_FRAGMENT)) == (TEXF_PICMIP | TEXF_FRAGMENT))
142         {
143                 Host_Error("R_GetTexTypeInfo: TEXF_PICMIP can not be used with TEXF_FRAGMENT");
144                 return NULL;
145         }
146         if (flags & TEXF_ALPHA)
147         {
148                 switch(textype)
149                 {
150                 case TEXTYPE_PALETTE:
151                         return &textype_palette_alpha;
152                 case TEXTYPE_RGB:
153                         Host_Error("R_GetTexTypeInfo: RGB format has no alpha, TEXF_ALPHA not allowed");
154                         return NULL;
155                 case TEXTYPE_RGBA:
156                         return &textype_rgba_alpha;
157                 default:
158                         Host_Error("R_GetTexTypeInfo: unknown texture format");
159                         return NULL;
160                 }
161         }
162         else
163         {
164                 switch(textype)
165                 {
166                 case TEXTYPE_PALETTE:
167                         return &textype_palette;
168                 case TEXTYPE_RGB:
169                         return &textype_rgb;
170                 case TEXTYPE_RGBA:
171                         return &textype_rgba;
172                 case TEXTYPE_DSDT:
173                         return &textype_dsdt;
174                 default:
175                         Host_Error("R_GetTexTypeInfo: unknown texture format");
176                         return NULL;
177                 }
178         }
179 }
180
181 static void R_UploadTexture(gltexture_t *t);
182
183 static void R_PrecacheTexture(gltexture_t *glt)
184 {
185         int precache;
186         precache = false;
187         if (glt->flags & TEXF_ALWAYSPRECACHE)
188                 precache = true;
189         else if (r_precachetextures.integer >= 2)
190                 precache = true;
191         else if (r_precachetextures.integer >= 1)
192                 if (glt->flags & TEXF_PRECACHE)
193                         precache = true;
194
195         if (precache)
196                 R_UploadTexture(glt);
197 }
198
199 int R_RealGetTexture(rtexture_t *rt)
200 {
201         if (rt)
202         {
203                 gltexture_t *glt;
204                 glt = (gltexture_t *)rt;
205                 if (glt->flags & GLTEXF_UPLOAD)
206                         R_UploadTexture(glt);
207                 glt->texnum = glt->image->texnum;
208                 return glt->image->texnum;
209         }
210         else
211                 return 0;
212 }
213
214 void R_FreeTexture(rtexture_t *rt)
215 {
216         gltexture_t *glt, **gltpointer;
217         gltextureimage_t *image, **gltimagepointer;
218
219         glt = (gltexture_t *)rt;
220         if (glt == NULL)
221                 Host_Error("R_FreeTexture: texture == NULL");
222
223         for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
224         if (*gltpointer == glt)
225                 *gltpointer = glt->chain;
226         else
227                 Host_Error("R_FreeTexture: texture \"%s\" not linked in pool", glt->identifier);
228
229         // note: if freeing a fragment texture, this will not make the claimed
230         // space available for new textures unless all other fragments in the
231         // image are also freed
232         if (glt->image)
233         {
234                 image = glt->image;
235                 image->texturecount--;
236                 if (image->texturecount < 1)
237                 {
238                         for (gltimagepointer = &glt->pool->imagechain;*gltimagepointer && *gltimagepointer != image;gltimagepointer = &(*gltimagepointer)->imagechain);
239                         if (*gltimagepointer == image)
240                                 *gltimagepointer = image->imagechain;
241                         else
242                                 Host_Error("R_FreeTexture: image not linked in pool");
243                         if (image->texnum)
244                                 qglDeleteTextures(1, (GLuint *)&image->texnum);
245                         if (image->blockallocation)
246                                 Mem_Free(image->blockallocation);
247                         Mem_Free(image);
248                 }
249         }
250
251         if (glt->inputtexels)
252                 Mem_Free(glt->inputtexels);
253         Mem_Free(glt);
254 }
255
256 rtexturepool_t *R_AllocTexturePool(void)
257 {
258         gltexturepool_t *pool;
259         if (texturemempool == NULL)
260                 return NULL;
261         pool = (gltexturepool_t *)Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
262         if (pool == NULL)
263                 return NULL;
264         pool->next = gltexturepoolchain;
265         gltexturepoolchain = pool;
266         pool->sentinel = TEXTUREPOOL_SENTINEL;
267         return (rtexturepool_t *)pool;
268 }
269
270 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
271 {
272         gltexturepool_t *pool, **poolpointer;
273         if (rtexturepool == NULL)
274                 return;
275         if (*rtexturepool == NULL)
276                 return;
277         pool = (gltexturepool_t *)(*rtexturepool);
278         *rtexturepool = NULL;
279         if (pool->sentinel != TEXTUREPOOL_SENTINEL)
280                 Host_Error("R_FreeTexturePool: pool already freed");
281         for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
282         if (*poolpointer == pool)
283                 *poolpointer = pool->next;
284         else
285                 Host_Error("R_FreeTexturePool: pool not linked");
286         while (pool->gltchain)
287                 R_FreeTexture((rtexture_t *)pool->gltchain);
288         if (pool->imagechain)
289                 Con_Printf("R_FreeTexturePool: not all images freed\n");
290         Mem_Free(pool);
291 }
292
293
294 typedef struct glmode_s
295 {
296         char *name;
297         int minification, magnification;
298 }
299 glmode_t;
300
301 static glmode_t modes[] =
302 {
303         {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
304         {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
305         {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
306         {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
307         {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
308         {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
309 };
310
311 static void GL_TextureMode_f (void)
312 {
313         int i;
314         GLint oldbindtexnum;
315         gltextureimage_t *image;
316         gltexturepool_t *pool;
317
318         if (Cmd_Argc() == 1)
319         {
320                 for (i = 0;i < 6;i++)
321                 {
322                         if (gl_filter_min == modes[i].minification)
323                         {
324                                 Con_Printf("%s\n", modes[i].name);
325                                 return;
326                         }
327                 }
328                 Con_Print("current filter is unknown???\n");
329                 return;
330         }
331
332         for (i = 0;i < 6;i++)
333                 if (!strcasecmp (modes[i].name, Cmd_Argv(1) ) )
334                         break;
335         if (i == 6)
336         {
337                 Con_Print("bad filter name\n");
338                 return;
339         }
340
341         gl_filter_min = modes[i].minification;
342         gl_filter_mag = modes[i].magnification;
343
344         // change all the existing mipmap texture objects
345         // FIXME: force renderer(/client/something?) restart instead?
346         for (pool = gltexturepoolchain;pool;pool = pool->next)
347         {
348                 for (image = pool->imagechain;image;image = image->imagechain)
349                 {
350                         // only update already uploaded images
351                         if (!(image->flags & GLTEXF_UPLOAD) && !(image->flags & (TEXF_FORCENEAREST | TEXF_FORCELINEAR)))
352                         {
353                                 qglGetIntegerv(gltexturetypebindingenums[image->texturetype], &oldbindtexnum);
354                                 qglBindTexture(gltexturetypeenums[image->texturetype], image->texnum);
355                                 if (image->flags & TEXF_MIPMAP)
356                                         qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_min);
357                                 else
358                                         qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_mag);
359                                 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MAG_FILTER, gl_filter_mag);
360                                 qglBindTexture(gltexturetypeenums[image->texturetype], oldbindtexnum);
361                         }
362                 }
363         }
364 }
365
366 static void GL_Texture_CalcImageSize(int texturetype, int flags, int inwidth, int inheight, int indepth, int *outwidth, int *outheight, int *outdepth)
367 {
368         int picmip = 0, maxsize = 0, width2 = 1, height2 = 1, depth2 = 1;
369
370         if (gl_max_size.integer > gl_max_texture_size)
371                 Cvar_SetValue("gl_max_size", gl_max_texture_size);
372
373         switch (texturetype)
374         {
375         default:
376         case GLTEXTURETYPE_1D:
377         case GLTEXTURETYPE_2D:
378                 maxsize = gl_max_texture_size;
379                 break;
380         case GLTEXTURETYPE_3D:
381                 maxsize = gl_max_3d_texture_size;
382                 break;
383         case GLTEXTURETYPE_CUBEMAP:
384                 maxsize = gl_max_cube_map_texture_size;
385                 break;
386         }
387
388         if (flags & TEXF_PICMIP)
389         {
390                 maxsize = min(maxsize, gl_max_size.integer);
391                 picmip = gl_picmip.integer;
392         }
393
394         if (outwidth)
395         {
396                 for (width2 = 1;width2 < inwidth;width2 <<= 1);
397                 for (width2 >>= picmip;width2 > maxsize;width2 >>= 1);
398                 *outwidth = max(1, width2);
399         }
400         if (outheight)
401         {
402                 for (height2 = 1;height2 < inheight;height2 <<= 1);
403                 for (height2 >>= picmip;height2 > maxsize;height2 >>= 1);
404                 *outheight = max(1, height2);
405         }
406         if (outdepth)
407         {
408                 for (depth2 = 1;depth2 < indepth;depth2 <<= 1);
409                 for (depth2 >>= picmip;depth2 > maxsize;depth2 >>= 1);
410                 *outdepth = max(1, depth2);
411         }
412 }
413
414
415 static int R_CalcTexelDataSize (gltexture_t *glt)
416 {
417         int width2, height2, depth2, size;
418
419         if (glt->flags & TEXF_FRAGMENT)
420                 return glt->width * glt->height * glt->depth * glt->textype->internalbytesperpixel * glt->image->sides;
421
422         GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->width, glt->height, glt->depth, &width2, &height2, &depth2);
423
424         size = width2 * height2 * depth2;
425
426         if (glt->flags & TEXF_MIPMAP)
427         {
428                 while (width2 > 1 || height2 > 1 || depth2 > 1)
429                 {
430                         if (width2 > 1)
431                                 width2 >>= 1;
432                         if (height2 > 1)
433                                 height2 >>= 1;
434                         if (depth2 > 1)
435                                 depth2 >>= 1;
436                         size += width2 * height2 * depth2;
437                 }
438         }
439
440         return size * glt->textype->internalbytesperpixel * glt->image->sides;
441 }
442
443 void R_TextureStats_Print(qboolean printeach, qboolean printpool, qboolean printtotal)
444 {
445         int glsize;
446         int isloaded;
447         int pooltotal = 0, pooltotalt = 0, pooltotalp = 0, poolloaded = 0, poolloadedt = 0, poolloadedp = 0;
448         int sumtotal = 0, sumtotalt = 0, sumtotalp = 0, sumloaded = 0, sumloadedt = 0, sumloadedp = 0;
449         gltexture_t *glt;
450         gltexturepool_t *pool;
451         if (printeach)
452                 Con_Print("glsize input loaded mip alpha name\n");
453         for (pool = gltexturepoolchain;pool;pool = pool->next)
454         {
455                 pooltotal = 0;
456                 pooltotalt = 0;
457                 pooltotalp = 0;
458                 poolloaded = 0;
459                 poolloadedt = 0;
460                 poolloadedp = 0;
461                 for (glt = pool->gltchain;glt;glt = glt->chain)
462                 {
463                         glsize = R_CalcTexelDataSize(glt);
464                         isloaded = !(glt->flags & GLTEXF_UPLOAD);
465                         pooltotal++;
466                         pooltotalt += glsize;
467                         pooltotalp += glt->inputdatasize;
468                         if (isloaded)
469                         {
470                                 poolloaded++;
471                                 poolloadedt += glsize;
472                                 poolloadedp += glt->inputdatasize;
473                         }
474                         if (printeach)
475                                 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);
476                 }
477                 if (printpool)
478                         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);
479                 sumtotal += pooltotal;
480                 sumtotalt += pooltotalt;
481                 sumtotalp += pooltotalp;
482                 sumloaded += poolloaded;
483                 sumloadedt += poolloadedt;
484                 sumloadedp += poolloadedp;
485         }
486         if (printtotal)
487                 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);
488 }
489
490 static void R_TextureStats_f(void)
491 {
492         R_TextureStats_Print(true, true, true);
493 }
494
495 static void r_textures_start(void)
496 {
497         // LordHavoc: allow any alignment
498         qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);
499         CHECKGLERROR
500
501         // use the largest scrap texture size we can (not sure if this is really a good idea)
502         for (block_size = 1;block_size * 2 <= gl_max_texture_size && block_size * 2 <= gl_max_scrapsize.integer;block_size *= 2);
503
504         texturemempool = Mem_AllocPool("texture management", 0, NULL);
505
506         // Disable JPEG screenshots if the DLL isn't loaded
507         if (! JPEG_OpenLibrary ())
508                 Cvar_SetValueQuick (&scr_screenshot_jpeg, 0);
509 }
510
511 static void r_textures_shutdown(void)
512 {
513         rtexturepool_t *temp;
514
515         JPEG_CloseLibrary ();
516
517         while(gltexturepoolchain)
518         {
519                 temp = (rtexturepool_t *) gltexturepoolchain;
520                 R_FreeTexturePool(&temp);
521         }
522
523         resizebuffersize = 0;
524         texturebuffersize = 0;
525         resizebuffer = NULL;
526         colorconvertbuffer = NULL;
527         texturebuffer = NULL;
528         Mem_FreePool(&texturemempool);
529 }
530
531 static void r_textures_newmap(void)
532 {
533 }
534
535 void R_Textures_Init (void)
536 {
537         Cmd_AddCommand("gl_texturemode", &GL_TextureMode_f);
538         Cmd_AddCommand("r_texturestats", R_TextureStats_f);
539         Cvar_RegisterVariable (&gl_max_scrapsize);
540         Cvar_RegisterVariable (&gl_max_size);
541         Cvar_RegisterVariable (&gl_picmip);
542         Cvar_RegisterVariable (&r_lerpimages);
543         Cvar_RegisterVariable (&r_precachetextures);
544         Cvar_RegisterVariable (&gl_texture_anisotropy);
545
546         R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
547 }
548
549 void R_Textures_Frame (void)
550 {
551         static int old_aniso = 0;
552
553         // could do procedural texture animation here, if we keep track of which
554         // textures were accessed this frame...
555
556         // free the resize buffers
557         resizebuffersize = 0;
558         if (resizebuffer)
559         {
560                 Mem_Free(resizebuffer);
561                 resizebuffer = NULL;
562         }
563         if (colorconvertbuffer)
564         {
565                 Mem_Free(colorconvertbuffer);
566                 colorconvertbuffer = NULL;
567         }
568
569         if (old_aniso != gl_texture_anisotropy.integer)
570         {
571                 gltextureimage_t *image;
572                 gltexturepool_t *pool;
573                 GLint oldbindtexnum;
574
575                 old_aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
576
577                 Cvar_SetValueQuick(&gl_texture_anisotropy, old_aniso);
578
579                 for (pool = gltexturepoolchain;pool;pool = pool->next)
580                 {
581                         for (image = pool->imagechain;image;image = image->imagechain)
582                         {
583                                 // only update already uploaded images
584                                 if (!(image->flags & GLTEXF_UPLOAD) && (image->flags & TEXF_MIPMAP))
585                                 {
586                                         qglGetIntegerv(gltexturetypebindingenums[image->texturetype], &oldbindtexnum);
587
588                                         qglBindTexture(gltexturetypeenums[image->texturetype], image->texnum);
589                                         qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MAX_ANISOTROPY_EXT, old_aniso);CHECKGLERROR
590
591                                         qglBindTexture(gltexturetypeenums[image->texturetype], oldbindtexnum);
592                                 }
593                         }
594                 }
595         }
596 }
597
598 void R_MakeResizeBufferBigger(int size)
599 {
600         if (resizebuffersize < size)
601         {
602                 resizebuffersize = size;
603                 if (resizebuffer)
604                         Mem_Free(resizebuffer);
605                 if (colorconvertbuffer)
606                         Mem_Free(colorconvertbuffer);
607                 resizebuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
608                 colorconvertbuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
609                 if (!resizebuffer || !colorconvertbuffer)
610                         Host_Error("R_Upload: out of memory");
611         }
612 }
613
614 static void GL_SetupTextureParameters(int flags, int texturetype)
615 {
616         int textureenum = gltexturetypeenums[texturetype];
617         int wrapmode = ((flags & TEXF_CLAMP) && gl_support_clamptoedge) ? GL_CLAMP_TO_EDGE : GL_REPEAT;
618
619         CHECKGLERROR
620
621         if (gl_support_anisotropy && (flags & TEXF_MIPMAP))
622         {
623                 int aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
624                 if (gl_texture_anisotropy.integer != aniso)
625                         Cvar_SetValueQuick(&gl_texture_anisotropy, aniso);
626                 qglTexParameteri(textureenum, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);CHECKGLERROR
627         }
628         qglTexParameteri(textureenum, GL_TEXTURE_WRAP_S, wrapmode);CHECKGLERROR
629         qglTexParameteri(textureenum, GL_TEXTURE_WRAP_T, wrapmode);CHECKGLERROR
630         if (gltexturetypedimensions[texturetype] >= 3)
631         {
632                 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_R, wrapmode);CHECKGLERROR
633         }
634
635         CHECKGLERROR
636         if (flags & TEXF_FORCENEAREST)
637         {
638                 if (flags & TEXF_MIPMAP)
639                 {
640                         qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);CHECKGLERROR
641                 }
642                 else
643                 {
644                         qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST);CHECKGLERROR
645                 }
646                 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_NEAREST);CHECKGLERROR
647         }
648         else if (flags & TEXF_FORCELINEAR)
649         {
650                 if (flags & TEXF_MIPMAP)
651                 {
652                         if (gl_filter_min == GL_NEAREST_MIPMAP_LINEAR || gl_filter_min == GL_LINEAR_MIPMAP_LINEAR)
653                         {
654                                 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);CHECKGLERROR
655                         }
656                         else
657                         {
658                                 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);CHECKGLERROR
659                         }
660                 }
661                 else
662                 {
663                         qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR);CHECKGLERROR
664                 }
665                 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_LINEAR);CHECKGLERROR
666         }
667         else
668         {
669                 if (flags & TEXF_MIPMAP)
670                 {
671                         qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
672                 }
673                 else
674                 {
675                         qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
676                 }
677                 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
678         }
679
680         CHECKGLERROR
681 }
682
683 static void R_Upload(gltexture_t *glt, unsigned char *data)
684 {
685         int i, mip, width, height, depth;
686         GLint oldbindtexnum;
687         unsigned char *prevbuffer;
688         prevbuffer = data;
689
690         CHECKGLERROR
691
692         glt->texnum = glt->image->texnum;
693         // we need to restore the texture binding after finishing the upload
694         qglGetIntegerv(gltexturetypebindingenums[glt->image->texturetype], &oldbindtexnum);
695         qglBindTexture(gltexturetypeenums[glt->image->texturetype], glt->image->texnum);
696         CHECKGLERROR
697         glt->flags &= ~GLTEXF_UPLOAD;
698
699         if (glt->flags & TEXF_FRAGMENT)
700         {
701                 if (glt->image->flags & GLTEXF_UPLOAD)
702                 {
703                         glt->image->flags &= ~GLTEXF_UPLOAD;
704                         Con_DPrint("uploaded new fragments image\n");
705                         R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
706                         memset(resizebuffer, 255, glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
707                         switch(glt->image->texturetype)
708                         {
709                         case GLTEXTURETYPE_1D:
710                                 qglTexImage1D(GL_TEXTURE_1D, 0, glt->image->glinternalformat, glt->image->width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
711                                 CHECKGLERROR
712                                 break;
713                         case GLTEXTURETYPE_2D:
714                                 qglTexImage2D(GL_TEXTURE_2D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
715                                 CHECKGLERROR
716                                 break;
717                         case GLTEXTURETYPE_3D:
718                                 qglTexImage3D(GL_TEXTURE_3D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, glt->image->depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
719                                 CHECKGLERROR
720                                 break;
721                         default:
722                                 Host_Error("R_Upload: fragment texture of type other than 1D, 2D, or 3D");
723                                 break;
724                         }
725                         GL_SetupTextureParameters(glt->image->flags, glt->image->texturetype);
726                 }
727
728                 if (prevbuffer == NULL)
729                 {
730                         R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
731                         memset(resizebuffer, 0, glt->width * glt->height * glt->image->depth * glt->image->bytesperpixel);
732                         prevbuffer = resizebuffer;
733                 }
734                 else if (glt->textype->textype == TEXTYPE_PALETTE)
735                 {
736                         // promote paletted to RGBA, so we only have to worry about RGB and
737                         // RGBA in the rest of this code
738                         R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->sides * glt->image->bytesperpixel);
739                         Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height * glt->depth, glt->palette);
740                         prevbuffer = colorconvertbuffer;
741                 }
742
743                 switch(glt->image->texturetype)
744                 {
745                 case GLTEXTURETYPE_1D:
746                         qglTexSubImage1D(GL_TEXTURE_1D, 0, glt->x, glt->width, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
747                         CHECKGLERROR
748                         break;
749                 case GLTEXTURETYPE_2D:
750                         qglTexSubImage2D(GL_TEXTURE_2D, 0, glt->x, glt->y, glt->width, glt->height, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
751                         CHECKGLERROR
752                         break;
753                 case GLTEXTURETYPE_3D:
754                         qglTexSubImage3D(GL_TEXTURE_3D, 0, glt->x, glt->y, glt->z, glt->width, glt->height, glt->depth, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
755                         CHECKGLERROR
756                         break;
757                 default:
758                         Host_Error("R_Upload: fragment texture of type other than 1D, 2D, or 3D");
759                         break;
760                 }
761         }
762         else
763         {
764                 glt->image->flags &= ~GLTEXF_UPLOAD;
765
766                 // these are rounded up versions of the size to do better resampling
767                 for (width  = 1;width  < glt->width ;width  <<= 1);
768                 for (height = 1;height < glt->height;height <<= 1);
769                 for (depth  = 1;depth  < glt->depth ;depth  <<= 1);
770
771                 R_MakeResizeBufferBigger(width * height * depth * glt->image->sides * glt->image->bytesperpixel);
772
773                 if (prevbuffer == NULL)
774                 {
775                         width = glt->image->width;
776                         height = glt->image->height;
777                         depth = glt->image->depth;
778                         memset(resizebuffer, 0, width * height * depth * glt->image->bytesperpixel);
779                         prevbuffer = resizebuffer;
780                 }
781                 else
782                 {
783                         if (glt->textype->textype == TEXTYPE_PALETTE)
784                         {
785                                 // promote paletted to RGBA, so we only have to worry about RGB and
786                                 // RGBA in the rest of this code
787                                 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height * glt->depth * glt->image->sides, glt->palette);
788                                 prevbuffer = colorconvertbuffer;
789                         }
790                 }
791
792                 // cubemaps contain multiple images and thus get processed a bit differently
793                 if (glt->image->texturetype != GLTEXTURETYPE_CUBEMAP)
794                 {
795                         if (glt->width != width || glt->height != height || glt->depth != depth)
796                         {
797                                 Image_Resample(prevbuffer, glt->width, glt->height, glt->depth, resizebuffer, width, height, depth, glt->image->bytesperpixel, r_lerpimages.integer);
798                                 prevbuffer = resizebuffer;
799                         }
800                         // picmip/max_size
801                         while (width > glt->image->width || height > glt->image->height || depth > glt->image->depth)
802                         {
803                                 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->image->width, glt->image->height, glt->image->depth, glt->image->bytesperpixel);
804                                 prevbuffer = resizebuffer;
805                         }
806                 }
807                 mip = 0;
808                 switch(glt->image->texturetype)
809                 {
810                 case GLTEXTURETYPE_1D:
811                         qglTexImage1D(GL_TEXTURE_1D, mip++, glt->image->glinternalformat, width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
812                         CHECKGLERROR
813                         if (glt->flags & TEXF_MIPMAP)
814                         {
815                                 while (width > 1 || height > 1 || depth > 1)
816                                 {
817                                         Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
818                                         prevbuffer = resizebuffer;
819                                         qglTexImage1D(GL_TEXTURE_1D, mip++, glt->image->glinternalformat, width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
820                                         CHECKGLERROR
821                                 }
822                         }
823                         break;
824                 case GLTEXTURETYPE_2D:
825                         qglTexImage2D(GL_TEXTURE_2D, mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
826                         CHECKGLERROR
827                         if (glt->flags & TEXF_MIPMAP)
828                         {
829                                 while (width > 1 || height > 1 || depth > 1)
830                                 {
831                                         Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
832                                         prevbuffer = resizebuffer;
833                                         qglTexImage2D(GL_TEXTURE_2D, mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
834                                         CHECKGLERROR
835                                 }
836                         }
837                         break;
838                 case GLTEXTURETYPE_3D:
839                         qglTexImage3D(GL_TEXTURE_3D, mip++, glt->image->glinternalformat, width, height, depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
840                         CHECKGLERROR
841                         if (glt->flags & TEXF_MIPMAP)
842                         {
843                                 while (width > 1 || height > 1 || depth > 1)
844                                 {
845                                         Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
846                                         prevbuffer = resizebuffer;
847                                         qglTexImage3D(GL_TEXTURE_3D, mip++, glt->image->glinternalformat, width, height, depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
848                                         CHECKGLERROR
849                                 }
850                         }
851                         break;
852                 case GLTEXTURETYPE_CUBEMAP:
853                         // convert and upload each side in turn,
854                         // from a continuous block of input texels
855                         texturebuffer = prevbuffer;
856                         for (i = 0;i < 6;i++)
857                         {
858                                 prevbuffer = texturebuffer;
859                                 texturebuffer += glt->width * glt->height * glt->depth * glt->textype->inputbytesperpixel;
860                                 if (glt->width != width || glt->height != height || glt->depth != depth)
861                                 {
862                                         Image_Resample(prevbuffer, glt->width, glt->height, glt->depth, resizebuffer, width, height, depth, glt->image->bytesperpixel, r_lerpimages.integer);
863                                         prevbuffer = resizebuffer;
864                                 }
865                                 // picmip/max_size
866                                 while (width > glt->image->width || height > glt->image->height || depth > glt->image->depth)
867                                 {
868                                         Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->image->width, glt->image->height, glt->image->depth, glt->image->bytesperpixel);
869                                         prevbuffer = resizebuffer;
870                                 }
871                                 mip = 0;
872                                 qglTexImage2D(cubemapside[i], mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
873                                 CHECKGLERROR
874                                 if (glt->flags & TEXF_MIPMAP)
875                                 {
876                                         while (width > 1 || height > 1 || depth > 1)
877                                         {
878                                                 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
879                                                 prevbuffer = resizebuffer;
880                                                 qglTexImage2D(cubemapside[i], mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
881                                                 CHECKGLERROR
882                                         }
883                                 }
884                         }
885                         break;
886                 }
887                 GL_SetupTextureParameters(glt->image->flags, glt->image->texturetype);
888         }
889         qglBindTexture(gltexturetypeenums[glt->image->texturetype], oldbindtexnum);
890 }
891
892 static void R_FindImageForTexture(gltexture_t *glt)
893 {
894         int i, j, best, best2, x, y, z, w, h, d;
895         textypeinfo_t *texinfo;
896         gltexturepool_t *pool;
897         gltextureimage_t *image, **imagechainpointer;
898         texinfo = glt->textype;
899         pool = glt->pool;
900
901         // remains -1 until uploaded
902         glt->texnum = -1;
903
904         x = 0;
905         y = 0;
906         z = 0;
907         w = glt->width;
908         h = glt->height;
909         d = glt->depth;
910         if (glt->flags & TEXF_FRAGMENT)
911         {
912                 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain)
913                 {
914                         image = *imagechainpointer;
915                         if (image->type != GLIMAGETYPE_FRAGMENTS)
916                                 continue;
917                         if (image->texturetype != glt->texturetype)
918                                 continue;
919                         if ((image->flags ^ glt->flags) & (TEXF_MIPMAP | TEXF_ALPHA | TEXF_CLAMP | TEXF_FORCENEAREST | TEXF_FORCELINEAR))
920                                 continue;
921                         if (image->glformat != texinfo->glformat || image->glinternalformat != texinfo->glinternalformat)
922                                 continue;
923                         if (glt->width > image->width || glt->height > image->height || glt->depth > image->depth)
924                                 continue;
925
926                         // got a fragments texture, find a place in it if we can
927                         for (best = image->width, i = 0;i < image->width - w;i++)
928                         {
929                                 for (best2 = 0, j = 0;j < w;j++)
930                                 {
931                                         if (image->blockallocation[i+j] >= best)
932                                                 break;
933                                         if (best2 < image->blockallocation[i+j])
934                                                 best2 = image->blockallocation[i+j];
935                                 }
936                                 if (j == w)
937                                 {
938                                         // this is a valid spot
939                                         x = i;
940                                         y = best = best2;
941                                 }
942                         }
943
944                         if (best + h > image->height)
945                                 continue;
946
947                         for (i = 0;i < w;i++)
948                                 image->blockallocation[x + i] = best + h;
949
950                         glt->x = x;
951                         glt->y = y;
952                         glt->z = 0;
953                         glt->image = image;
954                         image->texturecount++;
955                         return;
956                 }
957
958                 image = (gltextureimage_t *)Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
959                 if (image == NULL)
960                 {
961                         Con_Printf ("R_FindImageForTexture: ran out of memory\n");
962                         return;
963                 }
964                 image->type = GLIMAGETYPE_FRAGMENTS;
965                 // make sure the created image is big enough for the fragment
966                 for (image->width = block_size;image->width < glt->width;image->width <<= 1);
967                 image->height = 1;
968                 if (gltexturetypedimensions[glt->texturetype] >= 2)
969                         for (image->height = block_size;image->height < glt->height;image->height <<= 1);
970                 image->depth = 1;
971                 if (gltexturetypedimensions[glt->texturetype] >= 3)
972                         for (image->depth = block_size;image->depth < glt->depth;image->depth <<= 1);
973                 image->blockallocation = (short int *)Mem_Alloc(texturemempool, image->width * sizeof(short));
974                 memset(image->blockallocation, 0, image->width * sizeof(short));
975
976                 x = 0;
977                 y = 0;
978                 z = 0;
979                 for (i = 0;i < w;i++)
980                         image->blockallocation[x + i] = y + h;
981         }
982         else
983         {
984                 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain);
985
986                 image = (gltextureimage_t *)Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
987                 if (image == NULL)
988                 {
989                         Con_Printf ("R_FindImageForTexture: ran out of memory\n");
990                         return;
991                 }
992                 image->type = GLIMAGETYPE_TILE;
993                 image->blockallocation = NULL;
994
995                 GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->width, glt->height, glt->depth, &image->width, &image->height, &image->depth);
996         }
997         image->texturetype = glt->texturetype;
998         image->glinternalformat = texinfo->glinternalformat;
999         image->glformat = texinfo->glformat;
1000         image->flags = (glt->flags & (TEXF_MIPMAP | TEXF_ALPHA | TEXF_CLAMP | TEXF_PICMIP | TEXF_FORCENEAREST | TEXF_FORCELINEAR)) | GLTEXF_UPLOAD;
1001         image->bytesperpixel = texinfo->internalbytesperpixel;
1002         image->sides = image->texturetype == GLTEXTURETYPE_CUBEMAP ? 6 : 1;
1003         // get a texture number to use
1004         qglGenTextures(1, (GLuint *)&image->texnum);
1005         *imagechainpointer = image;
1006         image->texturecount++;
1007
1008         glt->x = x;
1009         glt->y = y;
1010         glt->z = z;
1011         glt->image = image;
1012 }
1013
1014 // note: R_FindImageForTexture must be called before this
1015 static void R_UploadTexture (gltexture_t *glt)
1016 {
1017         if (!(glt->flags & GLTEXF_UPLOAD))
1018                 return;
1019
1020         R_Upload(glt, glt->inputtexels);
1021         if (glt->inputtexels)
1022         {
1023                 Mem_Free(glt->inputtexels);
1024                 glt->inputtexels = NULL;
1025                 glt->flags |= GLTEXF_DESTROYED;
1026         }
1027         else if (glt->flags & GLTEXF_DESTROYED)
1028                 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed.  Can not upload original image again.  Uploaded blank texture.\n", glt->identifier);
1029 }
1030
1031 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)
1032 {
1033         int i, size;
1034         gltexture_t *glt;
1035         gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
1036         textypeinfo_t *texinfo;
1037
1038         if (cls.state == ca_dedicated)
1039                 return NULL;
1040
1041         if (flags & TEXF_FRAGMENT && texturetype != GLTEXTURETYPE_2D)
1042         {
1043                 Con_Printf ("R_LoadTexture: only 2D fragment textures implemented\n");
1044                 return NULL;
1045         }
1046         if (texturetype == GLTEXTURETYPE_CUBEMAP && !gl_texturecubemap)
1047         {
1048                 Con_Printf ("R_LoadTexture: cubemap texture not supported by driver\n");
1049                 return NULL;
1050         }
1051         if (texturetype == GLTEXTURETYPE_3D && !gl_texture3d)
1052         {
1053                 Con_Printf ("R_LoadTexture: 3d texture not supported by driver\n");
1054                 return NULL;
1055         }
1056
1057         texinfo = R_GetTexTypeInfo(textype, flags);
1058         size = width * height * depth * sides * texinfo->inputbytesperpixel;
1059         if (size < 1)
1060         {
1061                 Con_Printf ("R_LoadTexture: bogus texture size (%dx%dx%dx%dbppx%dsides = %d bytes)\n", width, height, depth, texinfo->inputbytesperpixel * 8, sides);
1062                 return NULL;
1063         }
1064
1065         // clear the alpha flag if the texture has no transparent pixels
1066         switch(textype)
1067         {
1068         case TEXTYPE_PALETTE:
1069                 if (flags & TEXF_ALPHA)
1070                 {
1071                         flags &= ~TEXF_ALPHA;
1072                         if (data)
1073                         {
1074                                 for (i = 0;i < size;i++)
1075                                 {
1076                                         if (((unsigned char *)&palette[data[i]])[3] < 255)
1077                                         {
1078                                                 flags |= TEXF_ALPHA;
1079                                                 break;
1080                                         }
1081                                 }
1082                         }
1083                 }
1084                 break;
1085         case TEXTYPE_RGB:
1086                 if (flags & TEXF_ALPHA)
1087                         Host_Error("R_LoadTexture: RGB has no alpha, don't specify TEXF_ALPHA");
1088                 break;
1089         case TEXTYPE_RGBA:
1090                 if (flags & TEXF_ALPHA)
1091                 {
1092                         flags &= ~TEXF_ALPHA;
1093                         if (data)
1094                         {
1095                                 for (i = 3;i < size;i += 4)
1096                                 {
1097                                         if (data[i] < 255)
1098                                         {
1099                                                 flags |= TEXF_ALPHA;
1100                                                 break;
1101                                         }
1102                                 }
1103                         }
1104                 }
1105                 break;
1106         case TEXTYPE_DSDT:
1107                 break;
1108         default:
1109                 Host_Error("R_LoadTexture: unknown texture type");
1110         }
1111
1112         glt = (gltexture_t *)Mem_Alloc(texturemempool, sizeof(gltexture_t));
1113         if (identifier)
1114                 strlcpy (glt->identifier, identifier, sizeof(glt->identifier));
1115         glt->pool = pool;
1116         glt->chain = pool->gltchain;
1117         pool->gltchain = glt;
1118         glt->width = width;
1119         glt->height = height;
1120         glt->depth = depth;
1121         glt->flags = flags | GLTEXF_UPLOAD;
1122         glt->textype = texinfo;
1123         glt->texturetype = texturetype;
1124         glt->inputdatasize = size;
1125         glt->palette = palette;
1126
1127         if (data)
1128         {
1129                 glt->inputtexels = (unsigned char *)Mem_Alloc(texturemempool, size);
1130                 if (glt->inputtexels == NULL)
1131                         Con_Printf ("R_LoadTexture: out of memory\n");
1132                 else
1133                         memcpy(glt->inputtexels, data, size);
1134         }
1135         else
1136                 glt->inputtexels = NULL;
1137
1138         R_FindImageForTexture(glt);
1139         R_PrecacheTexture(glt);
1140
1141         return (rtexture_t *)glt;
1142 }
1143
1144 rtexture_t *R_LoadTexture1D(rtexturepool_t *rtexturepool, const char *identifier, int width, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1145 {
1146         return R_SetupTexture(rtexturepool, identifier, width, 1, 1, 1, flags, textype, GLTEXTURETYPE_1D, data, palette);
1147 }
1148
1149 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)
1150 {
1151         return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, flags, textype, GLTEXTURETYPE_2D, data, palette);
1152 }
1153
1154 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)
1155 {
1156         return R_SetupTexture(rtexturepool, identifier, width, height, depth, 1, flags, textype, GLTEXTURETYPE_3D, data, palette);
1157 }
1158
1159 rtexture_t *R_LoadTextureCubeMap(rtexturepool_t *rtexturepool, const char *identifier, int width, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1160 {
1161         return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, flags, textype, GLTEXTURETYPE_CUBEMAP, data, palette);
1162 }
1163
1164 int R_TextureHasAlpha(rtexture_t *rt)
1165 {
1166         return rt ? (((gltexture_t *)rt)->flags & TEXF_ALPHA) != 0 : false;
1167 }
1168
1169 int R_TextureWidth(rtexture_t *rt)
1170 {
1171         return rt ? ((gltexture_t *)rt)->width : 0;
1172 }
1173
1174 int R_TextureHeight(rtexture_t *rt)
1175 {
1176         return rt ? ((gltexture_t *)rt)->height : 0;
1177 }
1178
1179 void R_FragmentLocation3D(rtexture_t *rt, int *x, int *y, int *z, float *fx1, float *fy1, float *fz1, float *fx2, float *fy2, float *fz2)
1180 {
1181         gltexture_t *glt;
1182         float iwidth, iheight, idepth;
1183         if (cls.state == ca_dedicated)
1184         {
1185                 if (x)
1186                         *x = 0;
1187                 if (y)
1188                         *y = 0;
1189                 if (z)
1190                         *z = 0;
1191                 if (fx1 || fy1 || fx2 || fy2)
1192                 {
1193                         if (fx1)
1194                                 *fx1 = 0;
1195                         if (fy1)
1196                                 *fy1 = 0;
1197                         if (fz1)
1198                                 *fz1 = 0;
1199                         if (fx2)
1200                                 *fx2 = 1;
1201                         if (fy2)
1202                                 *fy2 = 1;
1203                         if (fz2)
1204                                 *fz2 = 1;
1205                 }
1206                 return;
1207         }
1208         if (!rt)
1209                 Host_Error("R_FragmentLocation: no texture supplied");
1210         glt = (gltexture_t *)rt;
1211         if (glt->flags & TEXF_FRAGMENT)
1212         {
1213                 if (x)
1214                         *x = glt->x;
1215                 if (y)
1216                         *y = glt->y;
1217                 if (fx1 || fy1 || fx2 || fy2)
1218                 {
1219                         iwidth = 1.0f / glt->image->width;
1220                         iheight = 1.0f / glt->image->height;
1221                         idepth = 1.0f / glt->image->depth;
1222                         if (fx1)
1223                                 *fx1 = glt->x * iwidth;
1224                         if (fy1)
1225                                 *fy1 = glt->y * iheight;
1226                         if (fz1)
1227                                 *fz1 = glt->z * idepth;
1228                         if (fx2)
1229                                 *fx2 = (glt->x + glt->width) * iwidth;
1230                         if (fy2)
1231                                 *fy2 = (glt->y + glt->height) * iheight;
1232                         if (fz2)
1233                                 *fz2 = (glt->z + glt->depth) * idepth;
1234                 }
1235         }
1236         else
1237         {
1238                 if (x)
1239                         *x = 0;
1240                 if (y)
1241                         *y = 0;
1242                 if (z)
1243                         *z = 0;
1244                 if (fx1 || fy1 || fx2 || fy2)
1245                 {
1246                         if (fx1)
1247                                 *fx1 = 0;
1248                         if (fy1)
1249                                 *fy1 = 0;
1250                         if (fz1)
1251                                 *fz1 = 0;
1252                         if (fx2)
1253                                 *fx2 = 1;
1254                         if (fy2)
1255                                 *fy2 = 1;
1256                         if (fz2)
1257                                 *fz2 = 1;
1258                 }
1259         }
1260 }
1261
1262 void R_FragmentLocation(rtexture_t *rt, int *x, int *y, float *fx1, float *fy1, float *fx2, float *fy2)
1263 {
1264         R_FragmentLocation3D(rt, x, y, NULL, fx1, fy1, NULL, fx2, fy2, NULL);
1265 }
1266
1267 int R_CompatibleFragmentWidth(int width, int textype, int flags)
1268 {
1269         return width;
1270 }
1271
1272 void R_UpdateTexture(rtexture_t *rt, unsigned char *data)
1273 {
1274         gltexture_t *glt;
1275         if (rt == NULL)
1276                 Host_Error("R_UpdateTexture: no texture supplied");
1277         if (data == NULL)
1278                 Host_Error("R_UpdateTexture: no data supplied");
1279         glt = (gltexture_t *)rt;
1280
1281         // if it has not been uploaded yet, update the data that will be used when it is
1282         if (glt->inputtexels)
1283                 memcpy(glt->inputtexels, data, glt->inputdatasize);
1284         else
1285                 R_Upload(glt, data);
1286 }
1287