]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - gl_textures.c
added cl_screen.c/h (eventually most 2D stuff should be moved here)
[xonotic/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                 width2 = 1;while (width2 < glt->width) width2 <<= 1;
376                 height2 = 1;while (height2 < glt->height) height2 <<= 1;
377                 width2 >>= (int) r_picmip.integer;
378                 height2 >>= (int) r_picmip.integer;
379                 while (width2 > (int) r_max_size.integer) width2 >>= 1;
380                 while (height2 > (int) r_max_size.integer) height2 >>= 1;
381                 if (width2 < 1) width2 = 1;
382                 if (height2 < 1) height2 = 1;
383
384                 size = 0;
385                 if (glt->flags & TEXF_MIPMAP)
386                 {
387                         while (width2 > 1 || height2 > 1)
388                         {
389                                 size += width2 * height2;
390                                 if (width2 > 1)
391                                         width2 >>= 1;
392                                 if (height2 > 1)
393                                         height2 >>= 1;
394                         }
395                         size++; // count the last 1x1 mipmap
396                 }
397                 else
398                         size = width2*height2;
399         }
400         size *= glt->textype->internalbytesperpixel;
401
402         return size;
403 }
404
405 void R_TextureStats_PrintTotal(void)
406 {
407         int glsize, inputsize, total = 0, totalt = 0, totalp = 0, loaded = 0, loadedt = 0, loadedp = 0;
408         gltexture_t *glt;
409         gltexturepool_t *pool;
410         for (pool = gltexturepoolchain;pool;pool = pool->next)
411         {
412                 for (glt = pool->gltchain;glt;glt = glt->chain)
413                 {
414                         glsize = R_CalcTexelDataSize(glt);
415                         inputsize = glt->width * glt->height * glt->textype->inputbytesperpixel;
416
417                         total++;
418                         totalt += glsize;
419                         totalp += inputsize;
420                         if (!(glt->flags & GLTEXF_UPLOAD))
421                         {
422                                 loaded++;
423                                 loadedt += glsize;
424                                 loadedp += inputsize;
425                         }
426                 }
427         }
428         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);
429 }
430
431 static void R_TextureStats_f(void)
432 {
433         int loaded;
434         gltexture_t *glt;
435         gltexturepool_t *pool;
436         Con_Printf("glsize input crc  loaded mip alpha name\n");
437         for (pool = gltexturepoolchain;pool;pool = pool->next)
438         {
439                 for (glt = pool->gltchain;glt;glt = glt->chain)
440                 {
441                         loaded = !(glt->flags & GLTEXF_UPLOAD);
442                         if (glt->flags & GLTEXF_PROCEDURAL)
443                                 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>");
444                         else
445                                 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>");
446                 }
447                 Con_Printf("pool %10p\n", pool);
448         }
449         R_TextureStats_PrintTotal();
450 }
451
452 char engineversion[40];
453
454 static void r_textures_start(void)
455 {
456         // deal with size limits of various drivers (3dfx in particular)
457         glGetIntegerv(GL_MAX_TEXTURE_SIZE, &realmaxsize);
458         CHECKGLERROR
459
460         // use the largest scrap texture size we can (not sure if this is really a good idea)
461         for (block_size = 1;block_size < realmaxsize && block_size < r_max_scrapsize.integer;block_size <<= 1);
462
463         texturemempool = Mem_AllocPool("Textures");
464         gltexnuminuse = Mem_Alloc(texturemempool, MAX_GLTEXTURES);
465         //memset(gltexnuminuse, 0, MAX_GLTEXTURES);
466 }
467
468 static void r_textures_shutdown(void)
469 {
470         rtexturepool_t *temp;
471         while(gltexturepoolchain)
472         {
473                 temp = (rtexturepool_t *) gltexturepoolchain;
474                 R_FreeTexturePool(&temp);
475         }
476
477         /*
478         if (resizebuffer) Mem_Free(resizebuffer);resizebuffer = NULL;
479         if (colorconvertbuffer) Mem_Free(colorconvertbuffer);colorconvertbuffer = NULL;
480         if (resamplerow1) Mem_Free(resamplerow1);resamplerow1 = NULL;
481         if (resamplerow2) Mem_Free(resamplerow2);resamplerow2 = NULL;
482         if (texturebuffer) Mem_Free(texturebuffer);texturebuffer = NULL;
483         if (gltexnuminuse) Mem_Free(gltexnuminuse);gltexnuminuse = NULL;
484         */
485         resizebuffersize = 0;
486         resamplerowsize = 0;
487         texturebuffersize = 0;
488         resizebuffer = NULL;
489         colorconvertbuffer = NULL;
490         resamplerow1 = NULL;
491         resamplerow2 = NULL;
492         texturebuffer = NULL;
493         gltexnuminuse = NULL;
494         Mem_FreePool(&texturemempool);
495 }
496
497 static void r_textures_newmap(void)
498 {
499 }
500
501 void R_Textures_Init (void)
502 {
503         Cmd_AddCommand ("gl_texturemode", &GL_TextureMode_f);
504         Cmd_AddCommand("r_texturestats", R_TextureStats_f);
505         Cvar_RegisterVariable (&r_max_scrapsize);
506         Cvar_RegisterVariable (&r_max_size);
507         Cvar_RegisterVariable (&r_picmip);
508         Cvar_RegisterVariable (&r_lerpimages);
509         Cvar_RegisterVariable (&r_precachetextures);
510         gltexnuminuse = NULL;
511
512         R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
513 }
514
515 static void R_ResampleTextureLerpLine (byte *in, byte *out, int inwidth, int outwidth, int bytesperpixel)
516 {
517         int             j, xi, oldx = 0, f, fstep, endx, lerp;
518         fstep = (int) (inwidth*65536.0f/outwidth);
519         endx = (inwidth-1);
520         if (bytesperpixel == 4)
521         {
522                 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
523                 {
524                         xi = f >> 16;
525                         if (xi != oldx)
526                         {
527                                 in += (xi - oldx) * 4;
528                                 oldx = xi;
529                         }
530                         if (xi < endx)
531                         {
532                                 lerp = f & 0xFFFF;
533                                 *out++ = (byte) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
534                                 *out++ = (byte) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
535                                 *out++ = (byte) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
536                                 *out++ = (byte) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
537                         }
538                         else // last pixel of the line has no pixel to lerp to
539                         {
540                                 *out++ = in[0];
541                                 *out++ = in[1];
542                                 *out++ = in[2];
543                                 *out++ = in[3];
544                         }
545                 }
546         }
547         else if (bytesperpixel == 3)
548         {
549                 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
550                 {
551                         xi = f >> 16;
552                         if (xi != oldx)
553                         {
554                                 in += (xi - oldx) * 3;
555                                 oldx = xi;
556                         }
557                         if (xi < endx)
558                         {
559                                 lerp = f & 0xFFFF;
560                                 *out++ = (byte) ((((in[3] - in[0]) * lerp) >> 16) + in[0]);
561                                 *out++ = (byte) ((((in[4] - in[1]) * lerp) >> 16) + in[1]);
562                                 *out++ = (byte) ((((in[5] - in[2]) * lerp) >> 16) + in[2]);
563                         }
564                         else // last pixel of the line has no pixel to lerp to
565                         {
566                                 *out++ = in[0];
567                                 *out++ = in[1];
568                                 *out++ = in[2];
569                         }
570                 }
571         }
572         else
573                 Sys_Error("R_ResampleTextureLerpLine: unsupported bytesperpixel %i\n", bytesperpixel);
574 }
575
576 /*
577 ================
578 R_ResampleTexture
579 ================
580 */
581 static void R_ResampleTexture (void *indata, int inwidth, int inheight, void *outdata,  int outwidth, int outheight, int bytesperpixel)
582 {
583         if (resamplerowsize < outwidth*4)
584         {
585                 if (resamplerow1)
586                         Mem_Free(resamplerow1);
587                 if (resamplerow2)
588                         Mem_Free(resamplerow2);
589                 resamplerowsize = outwidth*4;
590                 resamplerow1 = Mem_Alloc(texturemempool, resamplerowsize);
591                 resamplerow2 = Mem_Alloc(texturemempool, resamplerowsize);
592         }
593 #define row1 resamplerow1
594 #define row2 resamplerow2
595         if (bytesperpixel == 4)
596         {
597                 if (r_lerpimages.integer)
598                 {
599                         int             i, j, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
600                         byte    *inrow, *out;
601                         out = outdata;
602                         fstep = (int) (inheight*65536.0f/outheight);
603
604                         inrow = indata;
605                         oldy = 0;
606                         R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
607                         R_ResampleTextureLerpLine (inrow + inwidth4, row2, inwidth, outwidth, bytesperpixel);
608                         for (i = 0, f = 0;i < outheight;i++,f += fstep)
609                         {
610                                 yi = f >> 16;
611                                 if (yi < endy)
612                                 {
613                                         lerp = f & 0xFFFF;
614                                         if (yi != oldy)
615                                         {
616                                                 inrow = (byte *)indata + inwidth4*yi;
617                                                 if (yi == oldy+1)
618                                                         memcpy(row1, row2, outwidth4);
619                                                 else
620                                                         R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
621                                                 R_ResampleTextureLerpLine (inrow + inwidth4, row2, inwidth, outwidth, bytesperpixel);
622                                                 oldy = yi;
623                                         }
624                                         j = outwidth - 4;
625                                         while(j >= 0)
626                                         {
627 #define LERPBYTE(i) out[i] = (byte) ((((row2[i] - row1[i]) * lerp) >> 16) + row1[i])
628                                                 LERPBYTE( 0);
629                                                 LERPBYTE( 1);
630                                                 LERPBYTE( 2);
631                                                 LERPBYTE( 3);
632                                                 LERPBYTE( 4);
633                                                 LERPBYTE( 5);
634                                                 LERPBYTE( 6);
635                                                 LERPBYTE( 7);
636                                                 LERPBYTE( 8);
637                                                 LERPBYTE( 9);
638                                                 LERPBYTE(10);
639                                                 LERPBYTE(11);
640                                                 LERPBYTE(12);
641                                                 LERPBYTE(13);
642                                                 LERPBYTE(14);
643                                                 LERPBYTE(15);
644                                                 out += 16;
645                                                 row1 += 16;
646                                                 row2 += 16;
647                                                 j -= 4;
648                                         }
649                                         if (j & 2)
650                                         {
651                                                 LERPBYTE( 0);
652                                                 LERPBYTE( 1);
653                                                 LERPBYTE( 2);
654                                                 LERPBYTE( 3);
655                                                 LERPBYTE( 4);
656                                                 LERPBYTE( 5);
657                                                 LERPBYTE( 6);
658                                                 LERPBYTE( 7);
659                                                 out += 8;
660                                                 row1 += 8;
661                                                 row2 += 8;
662                                         }
663                                         if (j & 1)
664                                         {
665                                                 LERPBYTE( 0);
666                                                 LERPBYTE( 1);
667                                                 LERPBYTE( 2);
668                                                 LERPBYTE( 3);
669                                                 out += 4;
670                                                 row1 += 4;
671                                                 row2 += 4;
672                                         }
673                                         row1 -= outwidth4;
674                                         row2 -= outwidth4;
675                                 }
676                                 else
677                                 {
678                                         if (yi != oldy)
679                                         {
680                                                 inrow = (byte *)indata + inwidth4*yi;
681                                                 if (yi == oldy+1)
682                                                         memcpy(row1, row2, outwidth4);
683                                                 else
684                                                         R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
685                                                 oldy = yi;
686                                         }
687                                         memcpy(out, row1, outwidth4);
688                                 }
689                         }
690                 }
691                 else
692                 {
693                         int i, j;
694                         unsigned frac, fracstep;
695                         // relies on int being 4 bytes
696                         int *inrow, *out;
697                         out = outdata;
698
699                         fracstep = inwidth*0x10000/outwidth;
700                         for (i = 0;i < outheight;i++)
701                         {
702                                 inrow = (int *)indata + inwidth*(i*inheight/outheight);
703                                 frac = fracstep >> 1;
704                                 j = outwidth - 4;
705                                 while (j >= 0)
706                                 {
707                                         out[0] = inrow[frac >> 16];frac += fracstep;
708                                         out[1] = inrow[frac >> 16];frac += fracstep;
709                                         out[2] = inrow[frac >> 16];frac += fracstep;
710                                         out[3] = inrow[frac >> 16];frac += fracstep;
711                                         out += 4;
712                                         j -= 4;
713                                 }
714                                 if (j & 2)
715                                 {
716                                         out[0] = inrow[frac >> 16];frac += fracstep;
717                                         out[1] = inrow[frac >> 16];frac += fracstep;
718                                         out += 2;
719                                 }
720                                 if (j & 1)
721                                 {
722                                         out[0] = inrow[frac >> 16];frac += fracstep;
723                                         out += 1;
724                                 }
725                         }
726                 }
727         }
728         else if (bytesperpixel == 3)
729         {
730                 if (r_lerpimages.integer)
731                 {
732                         int             i, j, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth3 = inwidth * 3, outwidth3 = outwidth * 3;
733                         byte    *inrow, *out;
734                         out = outdata;
735                         fstep = (int) (inheight*65536.0f/outheight);
736
737                         inrow = indata;
738                         oldy = 0;
739                         R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
740                         R_ResampleTextureLerpLine (inrow + inwidth3, row2, inwidth, outwidth, bytesperpixel);
741                         for (i = 0, f = 0;i < outheight;i++,f += fstep)
742                         {
743                                 yi = f >> 16;
744                                 if (yi < endy)
745                                 {
746                                         lerp = f & 0xFFFF;
747                                         if (yi != oldy)
748                                         {
749                                                 inrow = (byte *)indata + inwidth3*yi;
750                                                 if (yi == oldy+1)
751                                                         memcpy(row1, row2, outwidth3);
752                                                 else
753                                                         R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
754                                                 R_ResampleTextureLerpLine (inrow + inwidth3, row2, inwidth, outwidth, bytesperpixel);
755                                                 oldy = yi;
756                                         }
757                                         j = outwidth - 4;
758                                         while(j >= 0)
759                                         {
760 #define LERPBYTE(i) out[i] = (byte) ((((row2[i] - row1[i]) * lerp) >> 16) + row1[i])
761                                                 LERPBYTE( 0);
762                                                 LERPBYTE( 1);
763                                                 LERPBYTE( 2);
764                                                 LERPBYTE( 3);
765                                                 LERPBYTE( 4);
766                                                 LERPBYTE( 5);
767                                                 LERPBYTE( 6);
768                                                 LERPBYTE( 7);
769                                                 LERPBYTE( 8);
770                                                 LERPBYTE( 9);
771                                                 LERPBYTE(10);
772                                                 LERPBYTE(11);
773                                                 out += 12;
774                                                 row1 += 12;
775                                                 row2 += 12;
776                                                 j -= 4;
777                                         }
778                                         if (j & 2)
779                                         {
780                                                 LERPBYTE( 0);
781                                                 LERPBYTE( 1);
782                                                 LERPBYTE( 2);
783                                                 LERPBYTE( 3);
784                                                 LERPBYTE( 4);
785                                                 LERPBYTE( 5);
786                                                 out += 6;
787                                                 row1 += 6;
788                                                 row2 += 6;
789                                         }
790                                         if (j & 1)
791                                         {
792                                                 LERPBYTE( 0);
793                                                 LERPBYTE( 1);
794                                                 LERPBYTE( 2);
795                                                 out += 3;
796                                                 row1 += 3;
797                                                 row2 += 3;
798                                         }
799                                         row1 -= outwidth3;
800                                         row2 -= outwidth3;
801                                 }
802                                 else
803                                 {
804                                         if (yi != oldy)
805                                         {
806                                                 inrow = (byte *)indata + inwidth3*yi;
807                                                 if (yi == oldy+1)
808                                                         memcpy(row1, row2, outwidth3);
809                                                 else
810                                                         R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
811                                                 oldy = yi;
812                                         }
813                                         memcpy(out, row1, outwidth3);
814                                 }
815                         }
816                 }
817                 else
818                 {
819                         int i, j, f, inwidth3 = inwidth * 3;
820                         unsigned frac, fracstep;
821                         byte *inrow, *out;
822                         out = outdata;
823
824                         fracstep = inwidth*0x10000/outwidth;
825                         for (i = 0;i < outheight;i++)
826                         {
827                                 inrow = (byte *)indata + inwidth3*(i*inheight/outheight);
828                                 frac = fracstep >> 1;
829                                 j = outwidth - 4;
830                                 while (j >= 0)
831                                 {
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                                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
835                                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
836                                         j -= 4;
837                                 }
838                                 if (j & 2)
839                                 {
840                                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
841                                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
842                                         out += 2;
843                                 }
844                                 if (j & 1)
845                                 {
846                                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
847                                         out += 1;
848                                 }
849                         }
850                 }
851         }
852         else
853                 Sys_Error("R_ResampleTexture: unsupported bytesperpixel %i\n", bytesperpixel);
854 #undef row1
855 #undef row2
856 }
857
858 // in can be the same as out
859 static void R_MipReduce(byte *in, byte *out, int *width, int *height, int destwidth, int destheight, int bytesperpixel)
860 {
861         int x, y, nextrow;
862         nextrow = *width * bytesperpixel;
863         if (*width > destwidth)
864         {
865                 *width >>= 1;
866                 if (*height > destheight)
867                 {
868                         // reduce both
869                         *height >>= 1;
870                         if (bytesperpixel == 4)
871                         {
872                                 for (y = 0;y < *height;y++)
873                                 {
874                                         for (x = 0;x < *width;x++)
875                                         {
876                                                 out[0] = (byte) ((in[0] + in[4] + in[nextrow  ] + in[nextrow+4]) >> 2);
877                                                 out[1] = (byte) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
878                                                 out[2] = (byte) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
879                                                 out[3] = (byte) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
880                                                 out += 4;
881                                                 in += 8;
882                                         }
883                                         in += nextrow; // skip a line
884                                 }
885                         }
886                         else if (bytesperpixel == 3)
887                         {
888                                 for (y = 0;y < *height;y++)
889                                 {
890                                         for (x = 0;x < *width;x++)
891                                         {
892                                                 out[0] = (byte) ((in[0] + in[3] + in[nextrow  ] + in[nextrow+3]) >> 2);
893                                                 out[1] = (byte) ((in[1] + in[4] + in[nextrow+1] + in[nextrow+4]) >> 2);
894                                                 out[2] = (byte) ((in[2] + in[5] + in[nextrow+2] + in[nextrow+5]) >> 2);
895                                                 out += 3;
896                                                 in += 6;
897                                         }
898                                         in += nextrow; // skip a line
899                                 }
900                         }
901                         else
902                                 Sys_Error("R_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
903                 }
904                 else
905                 {
906                         // reduce width
907                         if (bytesperpixel == 4)
908                         {
909                                 for (y = 0;y < *height;y++)
910                                 {
911                                         for (x = 0;x < *width;x++)
912                                         {
913                                                 out[0] = (byte) ((in[0] + in[4]) >> 1);
914                                                 out[1] = (byte) ((in[1] + in[5]) >> 1);
915                                                 out[2] = (byte) ((in[2] + in[6]) >> 1);
916                                                 out[3] = (byte) ((in[3] + in[7]) >> 1);
917                                                 out += 4;
918                                                 in += 8;
919                                         }
920                                 }
921                         }
922                         else if (bytesperpixel == 3)
923                         {
924                                 for (y = 0;y < *height;y++)
925                                 {
926                                         for (x = 0;x < *width;x++)
927                                         {
928                                                 out[0] = (byte) ((in[0] + in[3]) >> 1);
929                                                 out[1] = (byte) ((in[1] + in[4]) >> 1);
930                                                 out[2] = (byte) ((in[2] + in[5]) >> 1);
931                                                 out += 3;
932                                                 in += 6;
933                                         }
934                                 }
935                         }
936                         else
937                                 Sys_Error("R_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
938                 }
939         }
940         else
941         {
942                 if (*height > destheight)
943                 {
944                         // reduce height
945                         *height >>= 1;
946                         if (bytesperpixel == 4)
947                         {
948                                 for (y = 0;y < *height;y++)
949                                 {
950                                         for (x = 0;x < *width;x++)
951                                         {
952                                                 out[0] = (byte) ((in[0] + in[nextrow  ]) >> 1);
953                                                 out[1] = (byte) ((in[1] + in[nextrow+1]) >> 1);
954                                                 out[2] = (byte) ((in[2] + in[nextrow+2]) >> 1);
955                                                 out[3] = (byte) ((in[3] + in[nextrow+3]) >> 1);
956                                                 out += 4;
957                                                 in += 4;
958                                         }
959                                         in += nextrow; // skip a line
960                                 }
961                         }
962                         else if (bytesperpixel == 3)
963                         {
964                                 for (y = 0;y < *height;y++)
965                                 {
966                                         for (x = 0;x < *width;x++)
967                                         {
968                                                 out[0] = (byte) ((in[0] + in[nextrow  ]) >> 1);
969                                                 out[1] = (byte) ((in[1] + in[nextrow+1]) >> 1);
970                                                 out[2] = (byte) ((in[2] + in[nextrow+2]) >> 1);
971                                                 out += 3;
972                                                 in += 3;
973                                         }
974                                         in += nextrow; // skip a line
975                                 }
976                         }
977                         else
978                                 Sys_Error("R_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
979                 }
980                 else
981                         Sys_Error("R_MipReduce: desired size already achieved\n");
982         }
983 }
984
985 static void R_Upload(gltexture_t *glt, byte *data)
986 {
987         int mip, width, height, internalformat;
988         byte *prevbuffer;
989         prevbuffer = data;
990
991         glBindTexture(GL_TEXTURE_2D, glt->image->texnum);
992         CHECKGLERROR
993
994         glt->flags &= ~GLTEXF_UPLOAD;
995
996         if (glt->flags & TEXF_FRAGMENT)
997         {
998                 if (resizebuffersize < glt->image->width * glt->image->height * glt->image->bytesperpixel)
999                 {
1000                         resizebuffersize = glt->image->width * glt->image->height * glt->image->bytesperpixel;
1001                         if (resizebuffer)
1002                                 Mem_Free(resizebuffer);
1003                         if (colorconvertbuffer)
1004                                 Mem_Free(colorconvertbuffer);
1005                         resizebuffer = Mem_Alloc(texturemempool, resizebuffersize);
1006                         colorconvertbuffer = Mem_Alloc(texturemempool, resizebuffersize);
1007                         if (!resizebuffer || !colorconvertbuffer)
1008                                 Host_Error("R_Upload: out of memory\n");
1009                 }
1010
1011                 if (glt->image->flags & GLTEXF_UPLOAD)
1012                 {
1013                         glt->image->flags &= ~GLTEXF_UPLOAD;
1014                         memset(resizebuffer, 255, glt->image->width * glt->image->height * glt->image->bytesperpixel);
1015                         glTexImage2D (GL_TEXTURE_2D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
1016                         CHECKGLERROR
1017                         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
1018                         CHECKGLERROR
1019                         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
1020                         CHECKGLERROR
1021                 }
1022
1023                 if (prevbuffer == NULL)
1024                 {
1025                         memset(resizebuffer, 255, glt->width * glt->height * glt->image->bytesperpixel);
1026                         prevbuffer = resizebuffer;
1027                 }
1028                 else if (glt->textype->textype == TEXTYPE_QPALETTE)
1029                 {
1030                         // promote paletted to RGBA, so we only have to worry about RGB and
1031                         // RGBA in the rest of this code
1032                         Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height, d_8to24table);
1033                         prevbuffer = colorconvertbuffer;
1034                 }
1035
1036                 glTexSubImage2D(GL_TEXTURE_2D, 0, glt->x, glt->y, glt->width, glt->height, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
1037                 CHECKGLERROR
1038                 return;
1039         }
1040
1041         glt->image->flags &= ~GLTEXF_UPLOAD;
1042
1043         // these are rounded up versions of the size to do better resampling
1044         width = 1;while(width < glt->width) width *= 2;
1045         height = 1;while(height < glt->height) height *= 2;
1046
1047         if (resizebuffersize < width * height * glt->image->bytesperpixel)
1048         {
1049                 resizebuffersize = width * height * glt->image->bytesperpixel;
1050                 if (resizebuffer)
1051                         Mem_Free(resizebuffer);
1052                 if (colorconvertbuffer)
1053                         Mem_Free(colorconvertbuffer);
1054                 resizebuffer = Mem_Alloc(texturemempool, resizebuffersize);
1055                 colorconvertbuffer = Mem_Alloc(texturemempool, resizebuffersize);
1056                 if (!resizebuffer || !colorconvertbuffer)
1057                         Host_Error("R_Upload: out of memory\n");
1058         }
1059
1060         if (prevbuffer == NULL)
1061         {
1062                 width = glt->image->width;
1063                 height = glt->image->height;
1064                 memset(resizebuffer, 255, width * height * glt->image->bytesperpixel);
1065                 prevbuffer = resizebuffer;
1066         }
1067         else
1068         {
1069                 if (glt->textype->textype == TEXTYPE_QPALETTE)
1070                 {
1071                         // promote paletted to RGBA, so we only have to worry about RGB and
1072                         // RGBA in the rest of this code
1073                         Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height, d_8to24table);
1074                         prevbuffer = colorconvertbuffer;
1075                 }
1076
1077                 if (glt->width != width || glt->height != height)
1078                 {
1079                         R_ResampleTexture(prevbuffer, glt->width, glt->height, resizebuffer, width, height, glt->image->bytesperpixel);
1080                         prevbuffer = resizebuffer;
1081                 }
1082
1083                 // apply picmip/max_size limitations
1084                 while (width > glt->image->width || height > glt->image->height)
1085                 {
1086                         R_MipReduce(prevbuffer, resizebuffer, &width, &height, glt->image->width, glt->image->height, glt->image->bytesperpixel);
1087                         prevbuffer = resizebuffer;
1088                 }
1089         }
1090
1091         // 3 and 4 are converted by the driver to it's preferred format for the current display mode
1092         internalformat = 3;
1093         if (glt->flags & TEXF_ALPHA)
1094                 internalformat = 4;
1095
1096         mip = 0;
1097         glTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
1098         CHECKGLERROR
1099         if (glt->flags & TEXF_MIPMAP)
1100         {
1101                 while (width > 1 || height > 1)
1102                 {
1103                         R_MipReduce(prevbuffer, resizebuffer, &width, &height, 1, 1, glt->image->bytesperpixel);
1104                         prevbuffer = resizebuffer;
1105
1106                         glTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
1107                         CHECKGLERROR
1108                 }
1109
1110                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
1111                 CHECKGLERROR
1112                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
1113                 CHECKGLERROR
1114         }
1115         else
1116         {
1117                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
1118                 CHECKGLERROR
1119                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
1120                 CHECKGLERROR
1121         }
1122 }
1123
1124 static void R_FindImageForTexture(gltexture_t *glt)
1125 {
1126         int i, j, best, best2, x, y, w, h;
1127         textypeinfo_t *texinfo;
1128         gltexturepool_t *pool;
1129         gltextureimage_t *image, **imagechainpointer;
1130         texinfo = glt->textype;
1131         pool = glt->pool;
1132
1133         x = 0;
1134         y = 0;
1135         w = glt->width;
1136         h = glt->height;
1137         if (glt->flags & TEXF_FRAGMENT)
1138         {
1139                 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain)
1140                 {
1141                         image = *imagechainpointer;
1142                         if (image->type != GLIMAGETYPE_FRAGMENTS)
1143                                 continue;
1144                         if (image->glformat != texinfo->glformat || image->glinternalformat != texinfo->glinternalformat)
1145                                 continue;
1146
1147                         // got a fragments texture, find a place in it if we can
1148                         best = block_size;
1149                         for (best = block_size, i = 0;i < block_size - w;i += texinfo->align)
1150                         {
1151                                 for (best2 = 0, j = 0;j < w;j++)
1152                                 {
1153                                         if (image->blockallocation[i+j] >= best)
1154                                                 break;
1155                                         if (best2 < image->blockallocation[i+j])
1156                                                 best2 = image->blockallocation[i+j];
1157                                 }
1158                                 if (j == w)
1159                                 {
1160                                         // this is a valid spot
1161                                         x = i;
1162                                         y = best = best2;
1163                                 }
1164                         }
1165
1166                         if (best + h > block_size)
1167                                 continue;
1168
1169                         for (i = 0;i < w;i++)
1170                                 image->blockallocation[x + i] = best + h;
1171
1172                         glt->x = x;
1173                         glt->y = y;
1174                         glt->image = image;
1175                         image->texturecount++;
1176                         return;
1177                 }
1178
1179                 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
1180                 if (image == NULL)
1181                         Sys_Error("R_FindImageForTexture: ran out of memory\n");
1182                 //memset(image, 0, sizeof(*image));
1183                 image->type = GLIMAGETYPE_FRAGMENTS;
1184                 image->width = block_size;
1185                 image->height = block_size;
1186                 image->blockallocation = Mem_Alloc(texturemempool, block_size * sizeof(short));
1187                 memset(image->blockallocation, 0, block_size * sizeof(short));
1188
1189                 x = 0;
1190                 y = 0;
1191                 for (i = 0;i < w;i++)
1192                         image->blockallocation[x + i] = y + h;
1193         }
1194         else
1195         {
1196                 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain);
1197
1198                 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
1199                 if (image == NULL)
1200                         Sys_Error("R_FindImageForTexture: ran out of memory\n");
1201                 //memset(image, 0, sizeof(*image));
1202                 image->type = GLIMAGETYPE_TILE;
1203                 image->blockallocation = NULL;
1204
1205                 // calculate final size
1206                 if (r_max_size.integer > realmaxsize)
1207                         Cvar_SetValue("r_max_size", realmaxsize);
1208                 image->width = 1;while (image->width < glt->width) image->width <<= 1;
1209                 image->height = 1;while (image->height < glt->height) image->height <<= 1;
1210                 image->width >>= r_picmip.integer;while (image->width > r_max_size.integer) image->width >>= 1;
1211                 image->height >>= r_picmip.integer;while (image->height > r_max_size.integer) image->height >>= 1;
1212                 if (image->width < 1) image->width = 1;
1213                 if (image->height < 1) image->height = 1;
1214         }
1215         image->glinternalformat = texinfo->glinternalformat;
1216         image->glformat = texinfo->glformat;
1217         image->flags = (glt->flags & (TEXF_MIPMAP | TEXF_ALPHA)) | GLTEXF_UPLOAD;
1218         image->bytesperpixel = texinfo->internalbytesperpixel;
1219         for (i = 1;i < MAX_GLTEXTURES;i++)
1220                 if (!gltexnuminuse[i])
1221                         break;
1222         if (i < MAX_GLTEXTURES)
1223                 gltexnuminuse[image->texnum = i] = true;
1224         else
1225                 Sys_Error("R_FindImageForTexture: ran out of GL textures\n");
1226         *imagechainpointer = image;
1227         image->texturecount++;
1228
1229         glt->x = x;
1230         glt->y = y;
1231         glt->image = image;
1232 }
1233
1234 // note: R_FindImageForTexture must be called before this
1235 static void R_UploadTexture (gltexture_t *glt)
1236 {
1237         if (!(glt->flags & (GLTEXF_UPLOAD | GLTEXF_PROCEDURAL)))
1238                 return;
1239
1240         if (glt->flags & GLTEXF_PROCEDURAL)
1241         {
1242                 if (glt->generate)
1243                 {
1244                         if (texturebuffersize < glt->width * glt->height * glt->textype->inputbytesperpixel)
1245                         {
1246                                 if (texturebuffer)
1247                                         Mem_Free(texturebuffer);
1248                                 texturebuffersize = glt->width * glt->height * glt->textype->inputbytesperpixel;
1249                                 texturebuffer = Mem_Alloc(texturemempool, texturebuffersize);
1250                         }
1251
1252                         glt->generate(texturebuffer, glt->width, glt->height, (void *)glt->proceduraldata, glt->proceduraldatasize);
1253                 }
1254         }
1255         else
1256         {
1257                 R_Upload(glt, glt->inputtexels);
1258                 if (glt->inputtexels)
1259                 {
1260                         Mem_Free(glt->inputtexels);
1261                         glt->inputtexels = NULL;
1262                         glt->flags |= GLTEXF_DESTROYED;
1263                 }
1264                 else if (glt->flags & GLTEXF_DESTROYED)
1265                         Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed.  Can not upload original image again.  Uploaded blank texture.\n", glt->identifier);
1266         }
1267 }
1268
1269 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)
1270 {
1271         gltexture_t *glt;
1272         glt = Mem_Alloc(texturemempool, sizeof(gltexture_t));
1273         //memset(glt, 0, sizeof(gltexture_t));
1274         if (identifier)
1275         {
1276                 glt->identifier = Mem_Alloc(texturemempool, strlen(identifier)+1);
1277                 strcpy (glt->identifier, identifier);
1278         }
1279         else
1280                 glt->identifier = NULL;
1281         glt->pool = pool;
1282         glt->chain = pool->gltchain;
1283         pool->gltchain = glt;
1284         glt->crc = crc;
1285         glt->width = width;
1286         glt->height = height;
1287         glt->flags = flags;
1288         glt->textype = texinfo;
1289
1290         if (data)
1291         {
1292                 glt->inputtexels = Mem_Alloc(texturemempool, glt->width * glt->height * texinfo->inputbytesperpixel);
1293                 if (glt->inputtexels == NULL)
1294                         Sys_Error("R_SetupTexture: out of memory\n");
1295                 memcpy(glt->inputtexels, data, glt->width * glt->height * texinfo->inputbytesperpixel);
1296         }
1297         else
1298                 glt->inputtexels = NULL;
1299
1300         glt->generate = generate;
1301         glt->proceduraldatasize = proceduraldatasize;
1302         if (proceduraldatasize)
1303         {
1304                 glt->proceduraldata = Mem_Alloc(texturemempool, proceduraldatasize);
1305                 if (glt->proceduraldata == NULL)
1306                         Sys_Error("R_SetupTexture: out of memory\n");
1307         }
1308         else
1309                 glt->proceduraldata = NULL;
1310
1311         R_FindImageForTexture(glt);
1312         R_PrecacheTexture(glt);
1313
1314         return glt;
1315 }
1316
1317 /*
1318 ================
1319 R_LoadTexture
1320 ================
1321 */
1322 rtexture_t *R_LoadTexture (rtexturepool_t *rtexturepool, char *identifier, int width, int height, byte *data, int textype, int flags)
1323 {
1324         int i;
1325         gltexture_t *glt;
1326         gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
1327         textypeinfo_t *texinfo;
1328         unsigned short crc;
1329
1330         if (cls.state == ca_dedicated)
1331                 return NULL;
1332
1333         texinfo = R_GetTexTypeInfo(textype, flags);
1334
1335         // data can be NULL
1336 //      if (data == NULL)
1337 //              Host_Error("R_LoadTexture: \"%s\" has no data\n", identifier);
1338
1339         if (flags & TEXF_FRAGMENT)
1340         {
1341                 if (width > block_size || height > block_size)
1342                         Host_Error("R_LoadTexture: fragment too big, must be no more than %dx%d\n", block_size, block_size);
1343                 if ((width * texinfo->internalbytesperpixel) & 3)
1344                         Host_Error("R_LoadTexture: incompatible width for fragment");
1345         }
1346
1347         // clear the alpha flag if the texture has no transparent pixels
1348         switch(textype)
1349         {
1350         case TEXTYPE_QPALETTE:
1351                 if (flags & TEXF_ALPHA)
1352                 {
1353                         flags &= ~TEXF_ALPHA;
1354                         for (i = 0;i < width * height;i++)
1355                         {
1356                                 if (data[i] == 255)
1357                                 {
1358                                         flags |= TEXF_ALPHA;
1359                                         break;
1360                                 }
1361                         }
1362                 }
1363                 break;
1364         case TEXTYPE_RGB:
1365                 if (flags & TEXF_ALPHA)
1366                         Host_Error("R_LoadTexture: RGB has no alpha, don't specify TEXF_ALPHA\n");
1367                 break;
1368         case TEXTYPE_RGBA:
1369                 if (flags & TEXF_ALPHA)
1370                 {
1371                         flags &= ~TEXF_ALPHA;
1372                         for (i = 0;i < width * height;i++)
1373                         {
1374                                 if (data[i * 4 + 3] < 255)
1375                                 {
1376                                         flags |= TEXF_ALPHA;
1377                                         break;
1378                                 }
1379                         }
1380                 }
1381                 break;
1382         default:
1383                 Host_Error("R_LoadTexture: unknown texture type\n");
1384         }
1385
1386         // LordHavoc: do a CRC to confirm the data really is the same as previous occurances.
1387         if (data == NULL)
1388                 crc = 0;
1389         else
1390                 crc = CRC_Block(data, width*height*texinfo->inputbytesperpixel);
1391
1392         // see if the texture is already present
1393         if (identifier && (glt = R_FindTexture(pool, identifier)))
1394         {
1395                 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)
1396                         return (rtexture_t *)glt; // exact match, use existing
1397                 Con_Printf("R_LoadTexture: cache mismatch on %s, replacing old texture\n", identifier);
1398                 R_FreeTexture(glt);
1399         }
1400
1401         return (rtexture_t *)R_SetupTexture(pool, identifier, crc, width, height, flags | GLTEXF_UPLOAD, texinfo, data, NULL, NULL, 0);
1402 }
1403
1404 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)
1405 {
1406         gltexture_t             *glt;
1407         gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
1408         textypeinfo_t   *texinfo;
1409
1410         if (cls.state == ca_dedicated)
1411                 return NULL;
1412
1413         texinfo = R_GetTexTypeInfo(textype, flags);
1414
1415         // no function is supported, for odd uses
1416 //      if (generate == NULL)
1417 //              Host_Error("R_ProceduralTexture: \"%s\" has no generate function\n", identifier);
1418         if (flags & TEXF_FRAGMENT)
1419         {
1420                 if (width > block_size || height > block_size)
1421                         Host_Error("R_ProceduralTexture: fragment too big, must be no more than %dx%d\n", block_size, block_size);
1422                 if ((width * texinfo->internalbytesperpixel) & 3)
1423                         Host_Error("R_ProceduralTexture: incompatible width for fragment");
1424         }
1425
1426         // see if the texture is already present
1427         if (identifier && (glt = R_FindTexture(pool, identifier)))
1428         {
1429                 if (width == glt->width && height == glt->height && texinfo == glt->textype && ((flags ^ glt->flags) & TEXF_IMPORTANTBITS) == 0 && ((flags ^ glt->flags) & GLTEXF_IMPORTANTBITS) == 0)
1430                         return (rtexture_t *)glt; // exact match, use existing
1431                 Con_DPrintf("R_LoadTexture: cache mismatch, replacing old texture\n");
1432                 R_FreeTexture(glt);
1433         }
1434
1435         return (rtexture_t *)R_SetupTexture(pool, identifier, 0, width, height, flags | GLTEXF_PROCEDURAL | GLTEXF_UPLOAD, texinfo, NULL, generate, proceduraldata, proceduraldatasize);
1436 }
1437
1438 int R_TextureHasAlpha(rtexture_t *rt)
1439 {
1440         gltexture_t *glt;
1441         if (!rt)
1442                 return false;
1443         glt = (gltexture_t *)rt;
1444         return (glt->flags & TEXF_ALPHA) != 0;
1445 }
1446
1447 int R_TextureWidth(rtexture_t *rt)
1448 {
1449         if (!rt)
1450                 return false;
1451         return ((gltexture_t *)rt)->width;
1452 }
1453
1454 int R_TextureHeight(rtexture_t *rt)
1455 {
1456         if (!rt)
1457                 return false;
1458         return ((gltexture_t *)rt)->height;
1459 }
1460
1461 void R_GetFragmentLocation(rtexture_t *rt, int *x, int *y, float *fx1, float *fy1, float *fx2, float *fy2)
1462 {
1463         gltexture_t *glt;
1464         float iwidth, iheight;
1465         if (cls.state == ca_dedicated)
1466         {
1467                 if (x)
1468                         *x = 0;
1469                 if (y)
1470                         *y = 0;
1471                 if (fx1 || fy1 || fx2 || fy2)
1472                 {
1473                         if (fx1)
1474                                 *fx1 = 0;
1475                         if (fy1)
1476                                 *fy1 = 0;
1477                         if (fx2)
1478                                 *fx2 = 1;
1479                         if (fy2)
1480                                 *fy2 = 1;
1481                 }
1482                 return;
1483         }
1484         if (!rt)
1485                 Host_Error("R_GetFragmentLocation: no texture supplied\n");
1486         glt = (gltexture_t *)rt;
1487         if (glt->flags & TEXF_FRAGMENT)
1488         {
1489                 if (x)
1490                         *x = glt->x;
1491                 if (y)
1492                         *y = glt->y;
1493                 if (fx1 || fy1 || fx2 || fy2)
1494                 {
1495                         iwidth = 1.0f / glt->image->width;
1496                         iheight = 1.0f / glt->image->height;
1497                         if (fx1)
1498                                 *fx1 = glt->x * iwidth;
1499                         if (fy1)
1500                                 *fy1 = glt->y * iheight;
1501                         if (fx2)
1502                                 *fx2 = (glt->x + glt->width) * iwidth;
1503                         if (fy2)
1504                                 *fy2 = (glt->y + glt->height) * iheight;
1505                 }
1506         }
1507         else
1508         {
1509                 if (x)
1510                         *x = 0;
1511                 if (y)
1512                         *y = 0;
1513                 if (fx1 || fy1 || fx2 || fy2)
1514                 {
1515                         if (fx1)
1516                                 *fx1 = 0;
1517                         if (fy1)
1518                                 *fy1 = 0;
1519                         if (fx2)
1520                                 *fx2 = 1;
1521                         if (fy2)
1522                                 *fy2 = 1;
1523                 }
1524         }
1525 }
1526
1527 int R_CompatibleFragmentWidth(int width, int textype, int flags)
1528 {
1529         textypeinfo_t *texinfo = R_GetTexTypeInfo(textype, flags);
1530         while ((width * texinfo->internalbytesperpixel) & 3)
1531                 width++;
1532         return width;
1533 }
1534
1535 void R_UpdateTexture(rtexture_t *rt, byte *data)
1536 {
1537         gltexture_t *glt;
1538         if (rt == NULL)
1539                 Host_Error("R_UpdateTexture: no texture supplied\n");
1540         if (data == NULL)
1541                 Host_Error("R_UpdateTexture: no data supplied\n");
1542         glt = (gltexture_t *)rt;
1543         if (!(glt->flags & GLTEXF_PROCEDURAL))
1544         {
1545                 if (glt->inputtexels == NULL)
1546                 {
1547                         glt->inputtexels = Mem_Alloc(texturemempool, glt->width * glt->height * glt->textype->inputbytesperpixel);
1548                         if (glt->inputtexels == NULL)
1549                                 Host_Error("R_UpdateTexture: ran out of memory\n");
1550                 }
1551                 memcpy(glt->inputtexels, data, glt->width * glt->height * glt->textype->inputbytesperpixel);
1552         }
1553         R_Upload(glt, data);
1554 }
1555