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