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