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