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