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