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