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