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