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