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