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