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