]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_textures.c
52c0da5907456bc625e7672d5daddb5f9bf64ac4
[divverent/darkplaces.git] / gl_textures.c
1 #include "quakedef.h"
2
3 cvar_t  r_max_size = {0, "r_max_size", "2048"};
4 cvar_t  r_max_scrapsize = {0, "r_max_scrapsize", "1024"};
5 cvar_t  r_picmip = {0, "r_picmip", "0"};
6 cvar_t  r_lerpimages = {CVAR_SAVE, "r_lerpimages", "1"};
7 cvar_t  r_precachetextures = {CVAR_SAVE, "r_precachetextures", "1"};
8
9 int             gl_filter_min = GL_LINEAR_MIPMAP_LINEAR; //NEAREST;
10 int             gl_filter_mag = GL_LINEAR;
11
12
13 static mempool_t *texturemempool;
14
15 // note: this must not conflict with TEXF_ flags in r_textures.h
16 // cleared when a texture is uploaded
17 #define GLTEXF_UPLOAD 0x00010000
18 // texture generated by code, also causes permanent GLTEXF_UPLOAD effect
19 #define GLTEXF_PROCEDURAL 0x00020000
20 // bitmask for mismatch checking
21 #define GLTEXF_IMPORTANTBITS (GLTEXF_PROCEDURAL)
22 // set when image is uploaded and freed
23 #define GLTEXF_DESTROYED 0x00040000
24
25 // size of images which hold fragment textures, ignores picmip and max_size
26 //#define BLOCK_SIZE 256
27 static int block_size;
28
29 // really this number only governs gltexnuminuse
30 #define MAX_GLTEXTURES 65536
31
32 // since there is only one set of GL texture numbers, we have to track them
33 // globally, everything else is per texture pool
34 static byte *gltexnuminuse;
35
36 typedef struct
37 {
38         int textype;
39         int inputbytesperpixel;
40         int internalbytesperpixel;
41         int glformat;
42         int glinternalformat;
43         int align;
44 }
45 textypeinfo_t;
46
47 static textypeinfo_t textype_qpalette       = {TEXTYPE_QPALETTE, 1, 4, GL_RGBA, 3, 1};
48 static textypeinfo_t textype_rgb            = {TEXTYPE_RGB     , 3, 3, GL_RGB , 3, 3};
49 static textypeinfo_t textype_rgba           = {TEXTYPE_RGBA    , 4, 4, GL_RGBA, 3, 1};
50 static textypeinfo_t textype_qpalette_alpha = {TEXTYPE_QPALETTE, 1, 4, GL_RGBA, 4, 1};
51 static textypeinfo_t textype_rgba_alpha     = {TEXTYPE_RGBA    , 4, 4, GL_RGBA, 4, 1};
52
53 // a tiling texture (most common type)
54 #define GLIMAGETYPE_TILE 0
55 // a fragments texture (contains one or more fragment textures)
56 #define GLIMAGETYPE_FRAGMENTS 1
57
58 // a gltextureimage can have one (or more if fragments) gltextures inside
59 typedef struct gltextureimage_s
60 {
61         struct gltextureimage_s *imagechain;
62         int texturecount;
63         int type; // one of the GLIMAGETYPE_ values
64         int texnum; // GL texture slot number
65         int width, height;
66         int bytesperpixel; // bytes per pixel
67         int glformat; // GL_RGB or GL_RGBA
68         int glinternalformat; // 3 or 4
69         int flags;
70         short *blockallocation; // fragment allocation
71 }
72 gltextureimage_t;
73
74 typedef struct gltexture_s
75 {
76         // pointer to texturepool (check this to see if the texture is allocated)
77         struct gltexturepool_s *pool;
78         // pointer to next texture in texturepool chain
79         struct gltexture_s *chain;
80         // pointer into gltextureimage array
81         gltextureimage_t *image;
82         // name of the texture (this might be removed someday), no duplicates
83         char *identifier;
84         // location in the image, and size
85         int x, y, width, height;
86         // copy of the original texture supplied to the upload function, for re-uploading or deferred uploads (non-precached)
87         byte *inputtexels;
88         // to identify cache mismatchs (this might be removed someday)
89         int crc;
90         // flags supplied to the LoadTexture/ProceduralTexture functions
91         // (might be altered to remove TEXF_ALPHA), and GLTEXF_ private flags
92         int flags;
93         // procedural texture generation function, called once per frame if the texture is used
94         int (*generate)(byte *buffer, int width, int height, void *parameterdata, int parameterdatasize);
95         // data provided to generate, persistent from call to call
96         byte *proceduraldata;
97         // size of data
98         int proceduraldatasize;
99         // used only to avoid updating the texture more than once per frame
100         int proceduralframecount;
101         // pointer to one of the textype_ structs
102         textypeinfo_t *textype;
103 }
104 gltexture_t;
105
106 #define TEXTUREPOOL_SENTINEL 0xC0DEDBAD
107
108 typedef struct gltexturepool_s
109 {
110         int sentinel;
111         struct gltextureimage_s *imagechain;
112         struct gltexture_s *gltchain;
113         struct gltexturepool_s *next;
114 }
115 gltexturepool_t;
116
117 static gltexturepool_t *gltexturepoolchain = NULL;
118
119 static byte *resamplerow1 = NULL, *resamplerow2 = NULL;
120 static int resamplerowsize = 0;
121 static byte *resizebuffer = NULL, *colorconvertbuffer;
122 static int resizebuffersize = 0;
123 static byte *texturebuffer;
124 static int texturebuffersize = 0;
125
126 static int realmaxsize = 0;
127
128 static textypeinfo_t *R_GetTexTypeInfo(int textype, int flags)
129 {
130         if (flags & TEXF_ALPHA)
131         {
132                 switch(textype)
133                 {
134                 case TEXTYPE_QPALETTE:
135                         return &textype_qpalette_alpha;
136                 case TEXTYPE_RGB:
137                         Host_Error("R_GetTexTypeInfo: RGB format has no alpha, TEXF_ALPHA not allowed\n");
138                         return NULL;
139                 case TEXTYPE_RGBA:
140                         return &textype_rgba_alpha;
141                 default:
142                         Host_Error("R_GetTexTypeInfo: unknown texture format\n");
143                         return NULL;
144                 }
145         }
146         else
147         {
148                 switch(textype)
149                 {
150                 case TEXTYPE_QPALETTE:
151                         return &textype_qpalette;
152                 case TEXTYPE_RGB:
153                         return &textype_rgb;
154                 case TEXTYPE_RGBA:
155                         return &textype_rgba;
156                 default:
157                         Host_Error("R_GetTexTypeInfo: unknown texture format\n");
158                         return NULL;
159                 }
160         }
161 }
162
163 static void R_UploadTexture(gltexture_t *t);
164
165 static void R_PrecacheTexture(gltexture_t *glt)
166 {
167         int precache;
168         precache = false;
169         if (glt->flags & TEXF_ALWAYSPRECACHE)
170                 precache = true;
171         else if (r_precachetextures.integer >= 2)
172                 precache = true;
173         else if (r_precachetextures.integer >= 1)
174                 if (glt->flags & TEXF_PRECACHE)
175                         precache = true;
176
177         if (precache)
178                 R_UploadTexture(glt);
179 }
180
181 int R_GetTexture(rtexture_t *rt)
182 {
183         gltexture_t *glt;
184         if (!rt)
185                 return 0;
186         glt = (gltexture_t *)rt;
187         if (glt->flags & (GLTEXF_UPLOAD | GLTEXF_PROCEDURAL))
188         {
189                 if (glt->flags & GLTEXF_PROCEDURAL)
190                 {
191                         if (glt->proceduralframecount != r_framecount)
192                         {
193                                 glt->proceduralframecount = r_framecount;
194                                 R_UploadTexture(glt);
195                         }
196                 }
197                 else
198                         R_UploadTexture(glt);
199         }
200         return glt->image->texnum;
201 }
202
203 static void R_FreeTexture(gltexture_t *glt)
204 {
205         gltexture_t **gltpointer;
206         gltextureimage_t *image, **gltimagepointer;
207         GLuint texnum;
208
209         for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
210         if (*gltpointer == glt)
211                 *gltpointer = glt->chain;
212         else
213                 Host_Error("R_FreeTexture: texture not linked in pool\n");
214
215         // note: if freeing a fragment texture, this will not make the claimed
216         // space available for new textures unless all other fragments in the
217         // image are also freed
218         image = glt->image;
219         image->texturecount--;
220         if (image->texturecount < 1)
221         {
222                 for (gltimagepointer = &glt->pool->imagechain;*gltimagepointer && *gltimagepointer != image;gltimagepointer = &(*gltimagepointer)->imagechain);
223                 if (*gltimagepointer == image)
224                         *gltimagepointer = image->imagechain;
225                 else
226                         Host_Error("R_FreeTexture: image not linked in pool\n");
227                 if (image->texnum)
228                 {
229                         texnum = image->texnum;
230                         gltexnuminuse[image->texnum] = 0;
231                         glDeleteTextures(1, &texnum);
232                 }
233                 if (image->blockallocation)
234                         Mem_Free(image->blockallocation);
235                 Mem_Free(image);
236         }
237
238         if (glt->identifier)
239                 Mem_Free(glt->identifier);
240         if (glt->inputtexels)
241                 Mem_Free(glt->inputtexels);
242         if (glt->proceduraldata)
243                 Mem_Free(glt->proceduraldata);
244         Mem_Free(glt);
245 }
246
247 static gltexture_t *R_FindTexture (gltexturepool_t *pool, char *identifier)
248 {
249         gltexture_t     *glt;
250
251         if (!identifier)
252                 return NULL;
253
254         for (glt = pool->gltchain;glt;glt = glt->chain)
255                 if (glt->identifier && !strcmp (identifier, glt->identifier))
256                         return glt;
257
258         return NULL;
259 }
260
261 rtexturepool_t *R_AllocTexturePool(void)
262 {
263         gltexturepool_t *pool;
264         pool = Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
265         if (pool == NULL)
266                 return NULL;
267         //memset(pool, 0, sizeof(gltexturepool_t));
268         pool->next = gltexturepoolchain;
269         gltexturepoolchain = pool;
270         pool->sentinel = TEXTUREPOOL_SENTINEL;
271         return (rtexturepool_t *)pool;
272 }
273
274 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
275 {
276         gltexturepool_t *pool, **poolpointer;
277         if (rtexturepool == NULL)
278                 return;
279         if (*rtexturepool == NULL)
280                 return;
281         pool = (gltexturepool_t *)(*rtexturepool);
282         *rtexturepool = NULL;
283         if (pool->sentinel != TEXTUREPOOL_SENTINEL)
284                 Host_Error("R_FreeTexturePool: pool already freed\n");
285         for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
286         if (*poolpointer == pool)
287                 *poolpointer = pool->next;
288         else
289                 Host_Error("R_FreeTexturePool: pool not linked\n");
290         while (pool->gltchain)
291                 R_FreeTexture(pool->gltchain);
292         Mem_Free(pool);
293 }
294
295
296 typedef struct
297 {
298         char *name;
299         int minification, magnification;
300 }
301 glmode_t;
302
303 static glmode_t modes[] =
304 {
305         {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
306         {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
307         {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
308         {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
309         {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
310         {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
311 };
312
313 static void GL_TextureMode_f (void)
314 {
315         int i;
316         gltextureimage_t *image;
317         gltexturepool_t *pool;
318
319         if (Cmd_Argc() == 1)
320         {
321                 for (i = 0;i < 6;i++)
322                 {
323                         if (gl_filter_min == modes[i].minification)
324                         {
325                                 Con_Printf ("%s\n", modes[i].name);
326                                 return;
327                         }
328                 }
329                 Con_Printf ("current filter is unknown???\n");
330                 return;
331         }
332
333         for (i = 0;i < 6;i++)
334                 if (!Q_strcasecmp (modes[i].name, Cmd_Argv(1) ) )
335                         break;
336         if (i == 6)
337         {
338                 Con_Printf ("bad filter name\n");
339                 return;
340         }
341
342         gl_filter_min = modes[i].minification;
343         gl_filter_mag = modes[i].magnification;
344
345         // change all the existing mipmap texture objects
346         // FIXME: force renderer(/client/something?) restart instead?
347         for (pool = gltexturepoolchain;pool;pool = pool->next)
348         {
349                 for (image = pool->imagechain;image;image = image->imagechain)
350                 {
351                         // only update already uploaded images
352                         if (!(image->flags & GLTEXF_UPLOAD))
353                         {
354                                 glBindTexture(GL_TEXTURE_2D, image->texnum);
355                                 if (image->flags & TEXF_MIPMAP)
356                                         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
357                                 else
358                                         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
359                                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
360                         }
361                 }
362         }
363 }
364
365 static int R_CalcTexelDataSize (gltexture_t *glt)
366 {
367         int width2, height2, size;
368         if (glt->flags & TEXF_FRAGMENT)
369                 size = glt->width * glt->height;
370         else
371         {
372                 if (r_max_size.integer > realmaxsize)
373                         Cvar_SetValue("r_max_size", realmaxsize);
374                 // calculate final size
375                 for (width2 = 1;width2 < glt->width;width2 <<= 1);
376                 for (height2 = 1;height2 < glt->height;height2 <<= 1);
377                 for (width2 >>= r_picmip.integer;width2 > r_max_size.integer;width2 >>= 1);
378                 for (height2 >>= r_picmip.integer;height2 > r_max_size.integer;height2 >>= 1);
379                 if (width2 < 1) width2 = 1;
380                 if (height2 < 1) height2 = 1;
381
382                 size = 0;
383                 if (glt->flags & TEXF_MIPMAP)
384                 {
385                         while (width2 > 1 || height2 > 1)
386                         {
387                                 size += width2 * height2;
388                                 if (width2 > 1)
389                                         width2 >>= 1;
390                                 if (height2 > 1)
391                                         height2 >>= 1;
392                         }
393                         size++; // count the last 1x1 mipmap
394                 }
395                 else
396                         size = width2*height2;
397         }
398         size *= glt->textype->internalbytesperpixel;
399
400         return size;
401 }
402
403 void R_TextureStats_PrintTotal(void)
404 {
405         int glsize, inputsize, total = 0, totalt = 0, totalp = 0, loaded = 0, loadedt = 0, loadedp = 0;
406         gltexture_t *glt;
407         gltexturepool_t *pool;
408         for (pool = gltexturepoolchain;pool;pool = pool->next)
409         {
410                 for (glt = pool->gltchain;glt;glt = glt->chain)
411                 {
412                         glsize = R_CalcTexelDataSize(glt);
413                         inputsize = glt->width * glt->height * glt->textype->inputbytesperpixel;
414
415                         total++;
416                         totalt += glsize;
417                         totalp += inputsize;
418                         if (!(glt->flags & GLTEXF_UPLOAD))
419                         {
420                                 loaded++;
421                                 loadedt += glsize;
422                                 loadedp += inputsize;
423                         }
424                 }
425         }
426         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);
427 }
428
429 static void R_TextureStats_f(void)
430 {
431         int loaded;
432         gltexture_t *glt;
433         gltexturepool_t *pool;
434         Con_Printf("glsize input crc  loaded mip alpha name\n");
435         for (pool = gltexturepoolchain;pool;pool = pool->next)
436         {
437                 for (glt = pool->gltchain;glt;glt = glt->chain)
438                 {
439                         loaded = !(glt->flags & GLTEXF_UPLOAD);
440                         if (glt->flags & GLTEXF_PROCEDURAL)
441                                 Con_Printf("%c%4i%c %4i  PROC %s %s %s %s\n"  , loaded ? '[' : ' ', (R_CalcTexelDataSize(glt) + 1023) / 1024, loaded ? ']' : ' ',                               (glt->width * glt->height * glt->textype->inputbytesperpixel + 1023) / 1024,                                         loaded ? "loaded" : "      ", (glt->flags & TEXF_MIPMAP) ? "mip" : "   ", (glt->flags & TEXF_ALPHA) ? "alpha" : "     ", glt->identifier ? glt->identifier : "<unnamed>");
442                         else
443                                 Con_Printf("%c%4i%c%c%4i%c %04X %s %s %s %s\n", loaded ? '[' : ' ', (R_CalcTexelDataSize(glt) + 1023) / 1024, loaded ? ']' : ' ', glt->inputtexels ? '[' : ' ', (glt->width * glt->height * glt->textype->inputbytesperpixel + 1023) / 1024, glt->inputtexels ? ']' : ' ', glt->crc, loaded ? "loaded" : "      ", (glt->flags & TEXF_MIPMAP) ? "mip" : "   ", (glt->flags & TEXF_ALPHA) ? "alpha" : "     ", glt->identifier ? glt->identifier : "<unnamed>");
444                 }
445                 Con_Printf("pool %10p\n", pool);
446         }
447         R_TextureStats_PrintTotal();
448 }
449
450 char engineversion[40];
451
452 static void r_textures_start(void)
453 {
454         // deal with size limits of various drivers (3dfx in particular)
455         glGetIntegerv(GL_MAX_TEXTURE_SIZE, &realmaxsize);
456         CHECKGLERROR
457
458         // use the largest scrap texture size we can (not sure if this is really a good idea)
459         for (block_size = 1;block_size < realmaxsize && block_size < r_max_scrapsize.integer;block_size <<= 1);
460
461         texturemempool = Mem_AllocPool("Textures");
462         gltexnuminuse = Mem_Alloc(texturemempool, MAX_GLTEXTURES);
463         //memset(gltexnuminuse, 0, MAX_GLTEXTURES);
464 }
465
466 static void r_textures_shutdown(void)
467 {
468         rtexturepool_t *temp;
469         while(gltexturepoolchain)
470         {
471                 temp = (rtexturepool_t *) gltexturepoolchain;
472                 R_FreeTexturePool(&temp);
473         }
474
475         /*
476         if (resizebuffer) Mem_Free(resizebuffer);resizebuffer = NULL;
477         if (colorconvertbuffer) Mem_Free(colorconvertbuffer);colorconvertbuffer = NULL;
478         if (resamplerow1) Mem_Free(resamplerow1);resamplerow1 = NULL;
479         if (resamplerow2) Mem_Free(resamplerow2);resamplerow2 = NULL;
480         if (texturebuffer) Mem_Free(texturebuffer);texturebuffer = NULL;
481         if (gltexnuminuse) Mem_Free(gltexnuminuse);gltexnuminuse = NULL;
482         */
483         resizebuffersize = 0;
484         resamplerowsize = 0;
485         texturebuffersize = 0;
486         resizebuffer = NULL;
487         colorconvertbuffer = NULL;
488         resamplerow1 = NULL;
489         resamplerow2 = NULL;
490         texturebuffer = NULL;
491         gltexnuminuse = NULL;
492         Mem_FreePool(&texturemempool);
493 }
494
495 static void r_textures_newmap(void)
496 {
497 }
498
499 void R_Textures_Init (void)
500 {
501         Cmd_AddCommand ("gl_texturemode", &GL_TextureMode_f);
502         Cmd_AddCommand("r_texturestats", R_TextureStats_f);
503         Cvar_RegisterVariable (&r_max_scrapsize);
504         Cvar_RegisterVariable (&r_max_size);
505         Cvar_RegisterVariable (&r_picmip);
506         Cvar_RegisterVariable (&r_lerpimages);
507         Cvar_RegisterVariable (&r_precachetextures);
508         gltexnuminuse = NULL;
509
510         R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
511 }
512
513 static void R_ResampleTextureLerpLine (byte *in, byte *out, int inwidth, int outwidth, int bytesperpixel)
514 {
515         int             j, xi, oldx = 0, f, fstep, endx, lerp;
516         fstep = (int) (inwidth*65536.0f/outwidth);
517         endx = (inwidth-1);
518         if (bytesperpixel == 4)
519         {
520                 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
521                 {
522                         xi = f >> 16;
523                         if (xi != oldx)
524                         {
525                                 in += (xi - oldx) * 4;
526                                 oldx = xi;
527                         }
528                         if (xi < endx)
529                         {
530                                 lerp = f & 0xFFFF;
531                                 *out++ = (byte) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
532                                 *out++ = (byte) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
533                                 *out++ = (byte) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
534                                 *out++ = (byte) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
535                         }
536                         else // last pixel of the line has no pixel to lerp to
537                         {
538                                 *out++ = in[0];
539                                 *out++ = in[1];
540                                 *out++ = in[2];
541                                 *out++ = in[3];
542                         }
543                 }
544         }
545         else if (bytesperpixel == 3)
546         {
547                 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
548                 {
549                         xi = f >> 16;
550                         if (xi != oldx)
551                         {
552                                 in += (xi - oldx) * 3;
553                                 oldx = xi;
554                         }
555                         if (xi < endx)
556                         {
557                                 lerp = f & 0xFFFF;
558                                 *out++ = (byte) ((((in[3] - in[0]) * lerp) >> 16) + in[0]);
559                                 *out++ = (byte) ((((in[4] - in[1]) * lerp) >> 16) + in[1]);
560                                 *out++ = (byte) ((((in[5] - in[2]) * lerp) >> 16) + in[2]);
561                         }
562                         else // last pixel of the line has no pixel to lerp to
563                         {
564                                 *out++ = in[0];
565                                 *out++ = in[1];
566                                 *out++ = in[2];
567                         }
568                 }
569         }
570         else
571                 Sys_Error("R_ResampleTextureLerpLine: unsupported bytesperpixel %i\n", bytesperpixel);
572 }
573
574 /*
575 ================
576 R_ResampleTexture
577 ================
578 */
579 static void R_ResampleTexture (void *indata, int inwidth, int inheight, void *outdata,  int outwidth, int outheight, int bytesperpixel)
580 {
581         if (resamplerowsize < outwidth*4)
582         {
583                 if (resamplerow1)
584                         Mem_Free(resamplerow1);
585                 if (resamplerow2)
586                         Mem_Free(resamplerow2);
587                 resamplerowsize = outwidth*4;
588                 resamplerow1 = Mem_Alloc(texturemempool, resamplerowsize);
589                 resamplerow2 = Mem_Alloc(texturemempool, resamplerowsize);
590         }
591 #define row1 resamplerow1
592 #define row2 resamplerow2
593         if (bytesperpixel == 4)
594         {
595                 if (r_lerpimages.integer)
596                 {
597                         int             i, j, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
598                         byte    *inrow, *out;
599                         out = outdata;
600                         fstep = (int) (inheight*65536.0f/outheight);
601
602                         inrow = indata;
603                         oldy = 0;
604                         R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
605                         R_ResampleTextureLerpLine (inrow + inwidth4, row2, inwidth, outwidth, bytesperpixel);
606                         for (i = 0, f = 0;i < outheight;i++,f += fstep)
607                         {
608                                 yi = f >> 16;
609                                 if (yi < endy)
610                                 {
611                                         lerp = f & 0xFFFF;
612                                         if (yi != oldy)
613                                         {
614                                                 inrow = (byte *)indata + inwidth4*yi;
615                                                 if (yi == oldy+1)
616                                                         memcpy(row1, row2, outwidth4);
617                                                 else
618                                                         R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
619                                                 R_ResampleTextureLerpLine (inrow + inwidth4, row2, inwidth, outwidth, bytesperpixel);
620                                                 oldy = yi;
621                                         }
622                                         j = outwidth - 4;
623                                         while(j >= 0)
624                                         {
625 #define LERPBYTE(i) out[i] = (byte) ((((row2[i] - row1[i]) * lerp) >> 16) + row1[i])
626                                                 LERPBYTE( 0);
627                                                 LERPBYTE( 1);
628                                                 LERPBYTE( 2);
629                                                 LERPBYTE( 3);
630                                                 LERPBYTE( 4);
631                                                 LERPBYTE( 5);
632                                                 LERPBYTE( 6);
633                                                 LERPBYTE( 7);
634                                                 LERPBYTE( 8);
635                                                 LERPBYTE( 9);
636                                                 LERPBYTE(10);
637                                                 LERPBYTE(11);
638                                                 LERPBYTE(12);
639                                                 LERPBYTE(13);
640                                                 LERPBYTE(14);
641                                                 LERPBYTE(15);
642                                                 out += 16;
643                                                 row1 += 16;
644                                                 row2 += 16;
645                                                 j -= 4;
646                                         }
647                                         if (j & 2)
648                                         {
649                                                 LERPBYTE( 0);
650                                                 LERPBYTE( 1);
651                                                 LERPBYTE( 2);
652                                                 LERPBYTE( 3);
653                                                 LERPBYTE( 4);
654                                                 LERPBYTE( 5);
655                                                 LERPBYTE( 6);
656                                                 LERPBYTE( 7);
657                                                 out += 8;
658                                                 row1 += 8;
659                                                 row2 += 8;
660                                         }
661                                         if (j & 1)
662                                         {
663                                                 LERPBYTE( 0);
664                                                 LERPBYTE( 1);
665                                                 LERPBYTE( 2);
666                                                 LERPBYTE( 3);
667                                                 out += 4;
668                                                 row1 += 4;
669                                                 row2 += 4;
670                                         }
671                                         row1 -= outwidth4;
672                                         row2 -= outwidth4;
673                                 }
674                                 else
675                                 {
676                                         if (yi != oldy)
677                                         {
678                                                 inrow = (byte *)indata + inwidth4*yi;
679                                                 if (yi == oldy+1)
680                                                         memcpy(row1, row2, outwidth4);
681                                                 else
682                                                         R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
683                                                 oldy = yi;
684                                         }
685                                         memcpy(out, row1, outwidth4);
686                                 }
687                         }
688                 }
689                 else
690                 {
691                         int i, j;
692                         unsigned frac, fracstep;
693                         // relies on int being 4 bytes
694                         int *inrow, *out;
695                         out = outdata;
696
697                         fracstep = inwidth*0x10000/outwidth;
698                         for (i = 0;i < outheight;i++)
699                         {
700                                 inrow = (int *)indata + inwidth*(i*inheight/outheight);
701                                 frac = fracstep >> 1;
702                                 j = outwidth - 4;
703                                 while (j >= 0)
704                                 {
705                                         out[0] = inrow[frac >> 16];frac += fracstep;
706                                         out[1] = inrow[frac >> 16];frac += fracstep;
707                                         out[2] = inrow[frac >> 16];frac += fracstep;
708                                         out[3] = inrow[frac >> 16];frac += fracstep;
709                                         out += 4;
710                                         j -= 4;
711                                 }
712                                 if (j & 2)
713                                 {
714                                         out[0] = inrow[frac >> 16];frac += fracstep;
715                                         out[1] = inrow[frac >> 16];frac += fracstep;
716                                         out += 2;
717                                 }
718                                 if (j & 1)
719                                 {
720                                         out[0] = inrow[frac >> 16];frac += fracstep;
721                                         out += 1;
722                                 }
723                         }
724                 }
725         }
726         else if (bytesperpixel == 3)
727         {
728                 if (r_lerpimages.integer)
729                 {
730                         int             i, j, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth3 = inwidth * 3, outwidth3 = outwidth * 3;
731                         byte    *inrow, *out;
732                         out = outdata;
733                         fstep = (int) (inheight*65536.0f/outheight);
734
735                         inrow = indata;
736                         oldy = 0;
737                         R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
738                         R_ResampleTextureLerpLine (inrow + inwidth3, row2, inwidth, outwidth, bytesperpixel);
739                         for (i = 0, f = 0;i < outheight;i++,f += fstep)
740                         {
741                                 yi = f >> 16;
742                                 if (yi < endy)
743                                 {
744                                         lerp = f & 0xFFFF;
745                                         if (yi != oldy)
746                                         {
747                                                 inrow = (byte *)indata + inwidth3*yi;
748                                                 if (yi == oldy+1)
749                                                         memcpy(row1, row2, outwidth3);
750                                                 else
751                                                         R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
752                                                 R_ResampleTextureLerpLine (inrow + inwidth3, row2, inwidth, outwidth, bytesperpixel);
753                                                 oldy = yi;
754                                         }
755                                         j = outwidth - 4;
756                                         while(j >= 0)
757                                         {
758 #define LERPBYTE(i) out[i] = (byte) ((((row2[i] - row1[i]) * lerp) >> 16) + row1[i])
759                                                 LERPBYTE( 0);
760                                                 LERPBYTE( 1);
761                                                 LERPBYTE( 2);
762                                                 LERPBYTE( 3);
763                                                 LERPBYTE( 4);
764                                                 LERPBYTE( 5);
765                                                 LERPBYTE( 6);
766                                                 LERPBYTE( 7);
767                                                 LERPBYTE( 8);
768                                                 LERPBYTE( 9);
769                                                 LERPBYTE(10);
770                                                 LERPBYTE(11);
771                                                 out += 12;
772                                                 row1 += 12;
773                                                 row2 += 12;
774                                                 j -= 4;
775                                         }
776                                         if (j & 2)
777                                         {
778                                                 LERPBYTE( 0);
779                                                 LERPBYTE( 1);
780                                                 LERPBYTE( 2);
781                                                 LERPBYTE( 3);
782                                                 LERPBYTE( 4);
783                                                 LERPBYTE( 5);
784                                                 out += 6;
785                                                 row1 += 6;
786                                                 row2 += 6;
787                                         }
788                                         if (j & 1)
789                                         {
790                                                 LERPBYTE( 0);
791                                                 LERPBYTE( 1);
792                                                 LERPBYTE( 2);
793                                                 out += 3;
794                                                 row1 += 3;
795                                                 row2 += 3;
796                                         }
797                                         row1 -= outwidth3;
798                                         row2 -= outwidth3;
799                                 }
800                                 else
801                                 {
802                                         if (yi != oldy)
803                                         {
804                                                 inrow = (byte *)indata + inwidth3*yi;
805                                                 if (yi == oldy+1)
806                                                         memcpy(row1, row2, outwidth3);
807                                                 else
808                                                         R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
809                                                 oldy = yi;
810                                         }
811                                         memcpy(out, row1, outwidth3);
812                                 }
813                         }
814                 }
815                 else
816                 {
817                         int i, j, f, inwidth3 = inwidth * 3;
818                         unsigned frac, fracstep;
819                         byte *inrow, *out;
820                         out = outdata;
821
822                         fracstep = inwidth*0x10000/outwidth;
823                         for (i = 0;i < outheight;i++)
824                         {
825                                 inrow = (byte *)indata + inwidth3*(i*inheight/outheight);
826                                 frac = fracstep >> 1;
827                                 j = outwidth - 4;
828                                 while (j >= 0)
829                                 {
830                                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
831                                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
832                                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
833                                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
834                                         j -= 4;
835                                 }
836                                 if (j & 2)
837                                 {
838                                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
839                                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
840                                         out += 2;
841                                 }
842                                 if (j & 1)
843                                 {
844                                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
845                                         out += 1;
846                                 }
847                         }
848                 }
849         }
850         else
851                 Sys_Error("R_ResampleTexture: unsupported bytesperpixel %i\n", bytesperpixel);
852 #undef row1
853 #undef row2
854 }
855
856 // in can be the same as out
857 static void R_MipReduce(byte *in, byte *out, int *width, int *height, int destwidth, int destheight, int bytesperpixel)
858 {
859         int x, y, nextrow;
860         nextrow = *width * bytesperpixel;
861         if (*width > destwidth)
862         {
863                 *width >>= 1;
864                 if (*height > destheight)
865                 {
866                         // reduce both
867                         *height >>= 1;
868                         if (bytesperpixel == 4)
869                         {
870                                 for (y = 0;y < *height;y++)
871                                 {
872                                         for (x = 0;x < *width;x++)
873                                         {
874                                                 out[0] = (byte) ((in[0] + in[4] + in[nextrow  ] + in[nextrow+4]) >> 2);
875                                                 out[1] = (byte) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
876                                                 out[2] = (byte) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
877                                                 out[3] = (byte) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
878                                                 out += 4;
879                                                 in += 8;
880                                         }
881                                         in += nextrow; // skip a line
882                                 }
883                         }
884                         else if (bytesperpixel == 3)
885                         {
886                                 for (y = 0;y < *height;y++)
887                                 {
888                                         for (x = 0;x < *width;x++)
889                                         {
890                                                 out[0] = (byte) ((in[0] + in[3] + in[nextrow  ] + in[nextrow+3]) >> 2);
891                                                 out[1] = (byte) ((in[1] + in[4] + in[nextrow+1] + in[nextrow+4]) >> 2);
892                                                 out[2] = (byte) ((in[2] + in[5] + in[nextrow+2] + in[nextrow+5]) >> 2);
893                                                 out += 3;
894                                                 in += 6;
895                                         }
896                                         in += nextrow; // skip a line
897                                 }
898                         }
899                         else
900                                 Sys_Error("R_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
901                 }
902                 else
903                 {
904                         // reduce width
905                         if (bytesperpixel == 4)
906                         {
907                                 for (y = 0;y < *height;y++)
908                                 {
909                                         for (x = 0;x < *width;x++)
910                                         {
911                                                 out[0] = (byte) ((in[0] + in[4]) >> 1);
912                                                 out[1] = (byte) ((in[1] + in[5]) >> 1);
913                                                 out[2] = (byte) ((in[2] + in[6]) >> 1);
914                                                 out[3] = (byte) ((in[3] + in[7]) >> 1);
915                                                 out += 4;
916                                                 in += 8;
917                                         }
918                                 }
919                         }
920                         else if (bytesperpixel == 3)
921                         {
922                                 for (y = 0;y < *height;y++)
923                                 {
924                                         for (x = 0;x < *width;x++)
925                                         {
926                                                 out[0] = (byte) ((in[0] + in[3]) >> 1);
927                                                 out[1] = (byte) ((in[1] + in[4]) >> 1);
928                                                 out[2] = (byte) ((in[2] + in[5]) >> 1);
929                                                 out += 3;
930                                                 in += 6;
931                                         }
932                                 }
933                         }
934                         else
935                                 Sys_Error("R_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
936                 }
937         }
938         else
939         {
940                 if (*height > destheight)
941                 {
942                         // reduce height
943                         *height >>= 1;
944                         if (bytesperpixel == 4)
945                         {
946                                 for (y = 0;y < *height;y++)
947                                 {
948                                         for (x = 0;x < *width;x++)
949                                         {
950                                                 out[0] = (byte) ((in[0] + in[nextrow  ]) >> 1);
951                                                 out[1] = (byte) ((in[1] + in[nextrow+1]) >> 1);
952                                                 out[2] = (byte) ((in[2] + in[nextrow+2]) >> 1);
953                                                 out[3] = (byte) ((in[3] + in[nextrow+3]) >> 1);
954                                                 out += 4;
955                                                 in += 4;
956                                         }
957                                         in += nextrow; // skip a line
958                                 }
959                         }
960                         else if (bytesperpixel == 3)
961                         {
962                                 for (y = 0;y < *height;y++)
963                                 {
964                                         for (x = 0;x < *width;x++)
965                                         {
966                                                 out[0] = (byte) ((in[0] + in[nextrow  ]) >> 1);
967                                                 out[1] = (byte) ((in[1] + in[nextrow+1]) >> 1);
968                                                 out[2] = (byte) ((in[2] + in[nextrow+2]) >> 1);
969                                                 out += 3;
970                                                 in += 3;
971                                         }
972                                         in += nextrow; // skip a line
973                                 }
974                         }
975                         else
976                                 Sys_Error("R_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
977                 }
978                 else
979                         Sys_Error("R_MipReduce: desired size already achieved\n");
980         }
981 }
982
983 static void R_Upload(gltexture_t *glt, byte *data)
984 {
985         int mip, width, height, internalformat;
986         byte *prevbuffer;
987         prevbuffer = data;
988
989         glBindTexture(GL_TEXTURE_2D, glt->image->texnum);
990         CHECKGLERROR
991
992         glt->flags &= ~GLTEXF_UPLOAD;
993
994         if (glt->flags & TEXF_FRAGMENT)
995         {
996                 if (resizebuffersize < glt->image->width * glt->image->height * glt->image->bytesperpixel)
997                 {
998                         resizebuffersize = glt->image->width * glt->image->height * glt->image->bytesperpixel;
999                         if (resizebuffer)
1000                                 Mem_Free(resizebuffer);
1001                         if (colorconvertbuffer)
1002                                 Mem_Free(colorconvertbuffer);
1003                         resizebuffer = Mem_Alloc(texturemempool, resizebuffersize);
1004                         colorconvertbuffer = Mem_Alloc(texturemempool, resizebuffersize);
1005                         if (!resizebuffer || !colorconvertbuffer)
1006                                 Host_Error("R_Upload: out of memory\n");
1007                 }
1008
1009                 if (glt->image->flags & GLTEXF_UPLOAD)
1010                 {
1011                         glt->image->flags &= ~GLTEXF_UPLOAD;
1012                         memset(resizebuffer, 255, glt->image->width * glt->image->height * glt->image->bytesperpixel);
1013                         glTexImage2D (GL_TEXTURE_2D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
1014                         CHECKGLERROR
1015                         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
1016                         CHECKGLERROR
1017                         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
1018                         CHECKGLERROR
1019                 }
1020
1021                 if (prevbuffer == NULL)
1022                 {
1023                         memset(resizebuffer, 255, glt->width * glt->height * glt->image->bytesperpixel);
1024                         prevbuffer = resizebuffer;
1025                 }
1026                 else if (glt->textype->textype == TEXTYPE_QPALETTE)
1027                 {
1028                         // promote paletted to RGBA, so we only have to worry about RGB and
1029                         // RGBA in the rest of this code
1030                         Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height, d_8to24table);
1031                         prevbuffer = colorconvertbuffer;
1032                 }
1033
1034                 glTexSubImage2D(GL_TEXTURE_2D, 0, glt->x, glt->y, glt->width, glt->height, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
1035                 CHECKGLERROR
1036                 return;
1037         }
1038
1039         glt->image->flags &= ~GLTEXF_UPLOAD;
1040
1041         // these are rounded up versions of the size to do better resampling
1042         for (width = 1;width < glt->width;width <<= 1);
1043         for (height = 1;height < glt->height;height <<= 1);
1044
1045         if (resizebuffersize < width * height * glt->image->bytesperpixel)
1046         {
1047                 resizebuffersize = width * height * glt->image->bytesperpixel;
1048                 if (resizebuffer)
1049                         Mem_Free(resizebuffer);
1050                 if (colorconvertbuffer)
1051                         Mem_Free(colorconvertbuffer);
1052                 resizebuffer = Mem_Alloc(texturemempool, resizebuffersize);
1053                 colorconvertbuffer = Mem_Alloc(texturemempool, resizebuffersize);
1054                 if (!resizebuffer || !colorconvertbuffer)
1055                         Host_Error("R_Upload: out of memory\n");
1056         }
1057
1058         if (prevbuffer == NULL)
1059         {
1060                 width = glt->image->width;
1061                 height = glt->image->height;
1062                 memset(resizebuffer, 255, width * height * glt->image->bytesperpixel);
1063                 prevbuffer = resizebuffer;
1064         }
1065         else
1066         {
1067                 if (glt->textype->textype == TEXTYPE_QPALETTE)
1068                 {
1069                         // promote paletted to RGBA, so we only have to worry about RGB and
1070                         // RGBA in the rest of this code
1071                         Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height, d_8to24table);
1072                         prevbuffer = colorconvertbuffer;
1073                 }
1074
1075                 if (glt->width != width || glt->height != height)
1076                 {
1077                         R_ResampleTexture(prevbuffer, glt->width, glt->height, resizebuffer, width, height, glt->image->bytesperpixel);
1078                         prevbuffer = resizebuffer;
1079                 }
1080
1081                 // apply picmip/max_size limitations
1082                 while (width > glt->image->width || height > glt->image->height)
1083                 {
1084                         R_MipReduce(prevbuffer, resizebuffer, &width, &height, glt->image->width, glt->image->height, glt->image->bytesperpixel);
1085                         prevbuffer = resizebuffer;
1086                 }
1087         }
1088
1089         // 3 and 4 are converted by the driver to it's preferred format for the current display mode
1090         internalformat = 3;
1091         if (glt->flags & TEXF_ALPHA)
1092                 internalformat = 4;
1093
1094         mip = 0;
1095         glTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
1096         CHECKGLERROR
1097         if (glt->flags & TEXF_MIPMAP)
1098         {
1099                 while (width > 1 || height > 1)
1100                 {
1101                         R_MipReduce(prevbuffer, resizebuffer, &width, &height, 1, 1, glt->image->bytesperpixel);
1102                         prevbuffer = resizebuffer;
1103
1104                         glTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
1105                         CHECKGLERROR
1106                 }
1107
1108                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
1109                 CHECKGLERROR
1110                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
1111                 CHECKGLERROR
1112         }
1113         else
1114         {
1115                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
1116                 CHECKGLERROR
1117                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
1118                 CHECKGLERROR
1119         }
1120 }
1121
1122 static void R_FindImageForTexture(gltexture_t *glt)
1123 {
1124         int i, j, best, best2, x, y, w, h;
1125         textypeinfo_t *texinfo;
1126         gltexturepool_t *pool;
1127         gltextureimage_t *image, **imagechainpointer;
1128         texinfo = glt->textype;
1129         pool = glt->pool;
1130
1131         x = 0;
1132         y = 0;
1133         w = glt->width;
1134         h = glt->height;
1135         if (glt->flags & TEXF_FRAGMENT)
1136         {
1137                 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain)
1138                 {
1139                         image = *imagechainpointer;
1140                         if (image->type != GLIMAGETYPE_FRAGMENTS)
1141                                 continue;
1142                         if (image->glformat != texinfo->glformat || image->glinternalformat != texinfo->glinternalformat)
1143                                 continue;
1144
1145                         // got a fragments texture, find a place in it if we can
1146                         best = block_size;
1147                         for (best = block_size, i = 0;i < block_size - w;i += texinfo->align)
1148                         {
1149                                 for (best2 = 0, j = 0;j < w;j++)
1150                                 {
1151                                         if (image->blockallocation[i+j] >= best)
1152                                                 break;
1153                                         if (best2 < image->blockallocation[i+j])
1154                                                 best2 = image->blockallocation[i+j];
1155                                 }
1156                                 if (j == w)
1157                                 {
1158                                         // this is a valid spot
1159                                         x = i;
1160                                         y = best = best2;
1161                                 }
1162                         }
1163
1164                         if (best + h > block_size)
1165                                 continue;
1166
1167                         for (i = 0;i < w;i++)
1168                                 image->blockallocation[x + i] = best + h;
1169
1170                         glt->x = x;
1171                         glt->y = y;
1172                         glt->image = image;
1173                         image->texturecount++;
1174                         return;
1175                 }
1176
1177                 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
1178                 if (image == NULL)
1179                         Sys_Error("R_FindImageForTexture: ran out of memory\n");
1180                 //memset(image, 0, sizeof(*image));
1181                 image->type = GLIMAGETYPE_FRAGMENTS;
1182                 image->width = block_size;
1183                 image->height = block_size;
1184                 image->blockallocation = Mem_Alloc(texturemempool, block_size * sizeof(short));
1185                 memset(image->blockallocation, 0, block_size * sizeof(short));
1186
1187                 x = 0;
1188                 y = 0;
1189                 for (i = 0;i < w;i++)
1190                         image->blockallocation[x + i] = y + h;
1191         }
1192         else
1193         {
1194                 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain);
1195
1196                 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
1197                 if (image == NULL)
1198                         Sys_Error("R_FindImageForTexture: ran out of memory\n");
1199                 //memset(image, 0, sizeof(*image));
1200                 image->type = GLIMAGETYPE_TILE;
1201                 image->blockallocation = NULL;
1202
1203                 // calculate final size
1204                 if (r_max_size.integer > realmaxsize)
1205                         Cvar_SetValue("r_max_size", realmaxsize);
1206                 for (image->width = 1;image->width < glt->width;image->width <<= 1);
1207                 for (image->height = 1;image->height < glt->height;image->height <<= 1);
1208                 for (image->width >>= r_picmip.integer;image->width > r_max_size.integer;image->width >>= 1);
1209                 for (image->height >>= r_picmip.integer;image->height > r_max_size.integer;image->height >>= 1);
1210                 if (image->width < 1) image->width = 1;
1211                 if (image->height < 1) image->height = 1;
1212         }
1213         image->glinternalformat = texinfo->glinternalformat;
1214         image->glformat = texinfo->glformat;
1215         image->flags = (glt->flags & (TEXF_MIPMAP | TEXF_ALPHA)) | GLTEXF_UPLOAD;
1216         image->bytesperpixel = texinfo->internalbytesperpixel;
1217         for (i = 1;i < MAX_GLTEXTURES;i++)
1218                 if (!gltexnuminuse[i])
1219                         break;
1220         if (i < MAX_GLTEXTURES)
1221                 gltexnuminuse[image->texnum = i] = true;
1222         else
1223                 Sys_Error("R_FindImageForTexture: ran out of GL textures\n");
1224         *imagechainpointer = image;
1225         image->texturecount++;
1226
1227         glt->x = x;
1228         glt->y = y;
1229         glt->image = image;
1230 }
1231
1232 // note: R_FindImageForTexture must be called before this
1233 static void R_UploadTexture (gltexture_t *glt)
1234 {
1235         if (!(glt->flags & (GLTEXF_UPLOAD | GLTEXF_PROCEDURAL)))
1236                 return;
1237
1238         if (glt->flags & GLTEXF_PROCEDURAL)
1239         {
1240                 if (glt->generate)
1241                 {
1242                         if (texturebuffersize < glt->width * glt->height * glt->textype->inputbytesperpixel)
1243                         {
1244                                 if (texturebuffer)
1245                                         Mem_Free(texturebuffer);
1246                                 texturebuffersize = glt->width * glt->height * glt->textype->inputbytesperpixel;
1247                                 texturebuffer = Mem_Alloc(texturemempool, texturebuffersize);
1248                         }
1249
1250                         glt->generate(texturebuffer, glt->width, glt->height, (void *)glt->proceduraldata, glt->proceduraldatasize);
1251                 }
1252         }
1253         else
1254         {
1255                 R_Upload(glt, glt->inputtexels);
1256                 if (glt->inputtexels)
1257                 {
1258                         Mem_Free(glt->inputtexels);
1259                         glt->inputtexels = NULL;
1260                         glt->flags |= GLTEXF_DESTROYED;
1261                 }
1262                 else if (glt->flags & GLTEXF_DESTROYED)
1263                         Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed.  Can not upload original image again.  Uploaded blank texture.\n", glt->identifier);
1264         }
1265 }
1266
1267 static gltexture_t *R_SetupTexture(gltexturepool_t *pool, char *identifier, int crc, int width, int height, int flags, textypeinfo_t *texinfo, byte *data, int (*generate)(byte *buffer, int width, int height, void *proceduraldata, int proceduraldatasize), void *proceduraldata, int proceduraldatasize)
1268 {
1269         gltexture_t *glt;
1270         glt = Mem_Alloc(texturemempool, sizeof(gltexture_t));
1271         //memset(glt, 0, sizeof(gltexture_t));
1272         if (identifier)
1273         {
1274                 glt->identifier = Mem_Alloc(texturemempool, strlen(identifier)+1);
1275                 strcpy (glt->identifier, identifier);
1276         }
1277         else
1278                 glt->identifier = NULL;
1279         glt->pool = pool;
1280         glt->chain = pool->gltchain;
1281         pool->gltchain = glt;
1282         glt->crc = crc;
1283         glt->width = width;
1284         glt->height = height;
1285         glt->flags = flags;
1286         glt->textype = texinfo;
1287
1288         if (data)
1289         {
1290                 glt->inputtexels = Mem_Alloc(texturemempool, glt->width * glt->height * texinfo->inputbytesperpixel);
1291                 if (glt->inputtexels == NULL)
1292                         Sys_Error("R_SetupTexture: out of memory\n");
1293                 memcpy(glt->inputtexels, data, glt->width * glt->height * texinfo->inputbytesperpixel);
1294         }
1295         else
1296                 glt->inputtexels = NULL;
1297
1298         glt->generate = generate;
1299         glt->proceduraldatasize = proceduraldatasize;
1300         if (proceduraldatasize)
1301         {
1302                 glt->proceduraldata = Mem_Alloc(texturemempool, proceduraldatasize);
1303                 if (glt->proceduraldata == NULL)
1304                         Sys_Error("R_SetupTexture: out of memory\n");
1305         }
1306         else
1307                 glt->proceduraldata = NULL;
1308
1309         R_FindImageForTexture(glt);
1310         R_PrecacheTexture(glt);
1311
1312         return glt;
1313 }
1314
1315 /*
1316 ================
1317 R_LoadTexture
1318 ================
1319 */
1320 rtexture_t *R_LoadTexture (rtexturepool_t *rtexturepool, char *identifier, int width, int height, byte *data, int textype, int flags)
1321 {
1322         int i;
1323         gltexture_t *glt;
1324         gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
1325         textypeinfo_t *texinfo;
1326         unsigned short crc;
1327
1328         if (cls.state == ca_dedicated)
1329                 return NULL;
1330
1331         texinfo = R_GetTexTypeInfo(textype, flags);
1332
1333         // data can be NULL
1334 //      if (data == NULL)
1335 //              Host_Error("R_LoadTexture: \"%s\" has no data\n", identifier);
1336
1337         if (flags & TEXF_FRAGMENT)
1338         {
1339                 if (width > block_size || height > block_size)
1340                         Host_Error("R_LoadTexture: fragment too big, must be no more than %dx%d\n", block_size, block_size);
1341                 if ((width * texinfo->internalbytesperpixel) & 3)
1342                         Host_Error("R_LoadTexture: incompatible width for fragment");
1343         }
1344
1345         // clear the alpha flag if the texture has no transparent pixels
1346         switch(textype)
1347         {
1348         case TEXTYPE_QPALETTE:
1349                 if (flags & TEXF_ALPHA)
1350                 {
1351                         flags &= ~TEXF_ALPHA;
1352                         for (i = 0;i < width * height;i++)
1353                         {
1354                                 if (data[i] == 255)
1355                                 {
1356                                         flags |= TEXF_ALPHA;
1357                                         break;
1358                                 }
1359                         }
1360                 }
1361                 break;
1362         case TEXTYPE_RGB:
1363                 if (flags & TEXF_ALPHA)
1364                         Host_Error("R_LoadTexture: RGB has no alpha, don't specify TEXF_ALPHA\n");
1365                 break;
1366         case TEXTYPE_RGBA:
1367                 if (flags & TEXF_ALPHA)
1368                 {
1369                         flags &= ~TEXF_ALPHA;
1370                         for (i = 0;i < width * height;i++)
1371                         {
1372                                 if (data[i * 4 + 3] < 255)
1373                                 {
1374                                         flags |= TEXF_ALPHA;
1375                                         break;
1376                                 }
1377                         }
1378                 }
1379                 break;
1380         default:
1381                 Host_Error("R_LoadTexture: unknown texture type\n");
1382         }
1383
1384         // LordHavoc: do a CRC to confirm the data really is the same as previous occurances.
1385         if (data == NULL)
1386                 crc = 0;
1387         else
1388                 crc = CRC_Block(data, width*height*texinfo->inputbytesperpixel);
1389
1390         // see if the texture is already present
1391         if (identifier && (glt = R_FindTexture(pool, identifier)))
1392         {
1393                 if (crc == glt->crc && width == glt->width && height == glt->height && texinfo == glt->textype && ((flags ^ glt->flags) & TEXF_IMPORTANTBITS) == 0 && ((flags ^ glt->flags) & GLTEXF_IMPORTANTBITS) == 0)
1394                         return (rtexture_t *)glt; // exact match, use existing
1395                 Con_Printf("R_LoadTexture: cache mismatch on %s, replacing old texture\n", identifier);
1396                 R_FreeTexture(glt);
1397         }
1398
1399         return (rtexture_t *)R_SetupTexture(pool, identifier, crc, width, height, flags | GLTEXF_UPLOAD, texinfo, data, NULL, NULL, 0);
1400 }
1401
1402 rtexture_t *R_ProceduralTexture (rtexturepool_t *rtexturepool, char *identifier, int width, int height, int textype, int flags, int (*generate)(byte *buffer, int width, int height, void *proceduraldata, int proceduraldatasize), void *proceduraldata, int proceduraldatasize)
1403 {
1404         gltexture_t             *glt;
1405         gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
1406         textypeinfo_t   *texinfo;
1407
1408         if (cls.state == ca_dedicated)
1409                 return NULL;
1410
1411         texinfo = R_GetTexTypeInfo(textype, flags);
1412
1413         // no function is supported, for odd uses
1414 //      if (generate == NULL)
1415 //              Host_Error("R_ProceduralTexture: \"%s\" has no generate function\n", identifier);
1416         if (flags & TEXF_FRAGMENT)
1417         {
1418                 if (width > block_size || height > block_size)
1419                         Host_Error("R_ProceduralTexture: fragment too big, must be no more than %dx%d\n", block_size, block_size);
1420                 if ((width * texinfo->internalbytesperpixel) & 3)
1421                         Host_Error("R_ProceduralTexture: incompatible width for fragment");
1422         }
1423
1424         // see if the texture is already present
1425         if (identifier && (glt = R_FindTexture(pool, identifier)))
1426         {
1427                 if (width == glt->width && height == glt->height && texinfo == glt->textype && ((flags ^ glt->flags) & TEXF_IMPORTANTBITS) == 0 && ((flags ^ glt->flags) & GLTEXF_IMPORTANTBITS) == 0)
1428                         return (rtexture_t *)glt; // exact match, use existing
1429                 Con_DPrintf("R_LoadTexture: cache mismatch, replacing old texture\n");
1430                 R_FreeTexture(glt);
1431         }
1432
1433         return (rtexture_t *)R_SetupTexture(pool, identifier, 0, width, height, flags | GLTEXF_PROCEDURAL | GLTEXF_UPLOAD, texinfo, NULL, generate, proceduraldata, proceduraldatasize);
1434 }
1435
1436 int R_TextureHasAlpha(rtexture_t *rt)
1437 {
1438         gltexture_t *glt;
1439         if (!rt)
1440                 return false;
1441         glt = (gltexture_t *)rt;
1442         return (glt->flags & TEXF_ALPHA) != 0;
1443 }
1444
1445 int R_TextureWidth(rtexture_t *rt)
1446 {
1447         if (!rt)
1448                 return false;
1449         return ((gltexture_t *)rt)->width;
1450 }
1451
1452 int R_TextureHeight(rtexture_t *rt)
1453 {
1454         if (!rt)
1455                 return false;
1456         return ((gltexture_t *)rt)->height;
1457 }
1458
1459 void R_FragmentLocation(rtexture_t *rt, int *x, int *y, float *fx1, float *fy1, float *fx2, float *fy2)
1460 {
1461         gltexture_t *glt;
1462         float iwidth, iheight;
1463         if (cls.state == ca_dedicated)
1464         {
1465                 if (x)
1466                         *x = 0;
1467                 if (y)
1468                         *y = 0;
1469                 if (fx1 || fy1 || fx2 || fy2)
1470                 {
1471                         if (fx1)
1472                                 *fx1 = 0;
1473                         if (fy1)
1474                                 *fy1 = 0;
1475                         if (fx2)
1476                                 *fx2 = 1;
1477                         if (fy2)
1478                                 *fy2 = 1;
1479                 }
1480                 return;
1481         }
1482         if (!rt)
1483                 Host_Error("R_FragmentLocation: no texture supplied\n");
1484         glt = (gltexture_t *)rt;
1485         if (glt->flags & TEXF_FRAGMENT)
1486         {
1487                 if (x)
1488                         *x = glt->x;
1489                 if (y)
1490                         *y = glt->y;
1491                 if (fx1 || fy1 || fx2 || fy2)
1492                 {
1493                         iwidth = 1.0f / glt->image->width;
1494                         iheight = 1.0f / glt->image->height;
1495                         if (fx1)
1496                                 *fx1 = glt->x * iwidth;
1497                         if (fy1)
1498                                 *fy1 = glt->y * iheight;
1499                         if (fx2)
1500                                 *fx2 = (glt->x + glt->width) * iwidth;
1501                         if (fy2)
1502                                 *fy2 = (glt->y + glt->height) * iheight;
1503                 }
1504         }
1505         else
1506         {
1507                 if (x)
1508                         *x = 0;
1509                 if (y)
1510                         *y = 0;
1511                 if (fx1 || fy1 || fx2 || fy2)
1512                 {
1513                         if (fx1)
1514                                 *fx1 = 0;
1515                         if (fy1)
1516                                 *fy1 = 0;
1517                         if (fx2)
1518                                 *fx2 = 1;
1519                         if (fy2)
1520                                 *fy2 = 1;
1521                 }
1522         }
1523 }
1524
1525 int R_CompatibleFragmentWidth(int width, int textype, int flags)
1526 {
1527         textypeinfo_t *texinfo = R_GetTexTypeInfo(textype, flags);
1528         while ((width * texinfo->internalbytesperpixel) & 3)
1529                 width++;
1530         return width;
1531 }
1532
1533 void R_UpdateTexture(rtexture_t *rt, byte *data)
1534 {
1535         gltexture_t *glt;
1536         if (rt == NULL)
1537                 Host_Error("R_UpdateTexture: no texture supplied\n");
1538         if (data == NULL)
1539                 Host_Error("R_UpdateTexture: no data supplied\n");
1540         glt = (gltexture_t *)rt;
1541         if (!(glt->flags & GLTEXF_PROCEDURAL))
1542         {
1543                 if (glt->inputtexels == NULL)
1544                 {
1545                         glt->inputtexels = Mem_Alloc(texturemempool, glt->width * glt->height * glt->textype->inputbytesperpixel);
1546                         if (glt->inputtexels == NULL)
1547                                 Host_Error("R_UpdateTexture: ran out of memory\n");
1548                 }
1549                 memcpy(glt->inputtexels, data, glt->width * glt->height * glt->textype->inputbytesperpixel);
1550         }
1551         R_Upload(glt, data);
1552 }
1553