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