6 cvar_t r_max_size = {CVAR_SAVE, "r_max_size", "2048"};
7 cvar_t r_max_scrapsize = {CVAR_SAVE, "r_max_scrapsize", "256"};
8 cvar_t r_picmip = {CVAR_SAVE, "r_picmip", "0"};
9 cvar_t r_lerpimages = {CVAR_SAVE, "r_lerpimages", "1"};
10 cvar_t r_precachetextures = {CVAR_SAVE, "r_precachetextures", "1"};
12 int gl_filter_min = GL_LINEAR_MIPMAP_LINEAR;
13 int gl_filter_mag = GL_LINEAR;
16 static mempool_t *texturemempool;
17 static mempool_t *texturedatamempool;
18 static mempool_t *textureprocessingmempool;
20 // note: this must not conflict with TEXF_ flags in r_textures.h
21 // cleared when a texture is uploaded
22 #define GLTEXF_UPLOAD 0x00010000
23 // bitmask for mismatch checking
24 #define GLTEXF_IMPORTANTBITS (0)
25 // set when image is uploaded and freed
26 #define GLTEXF_DESTROYED 0x00040000
28 // size of images which hold fragment textures, ignores picmip and max_size
29 static int block_size;
34 int inputbytesperpixel;
35 int internalbytesperpixel;
41 static textypeinfo_t textype_palette = {TEXTYPE_PALETTE, 1, 4, GL_RGBA, 3};
42 static textypeinfo_t textype_rgb = {TEXTYPE_RGB , 3, 3, GL_RGB , 3};
43 static textypeinfo_t textype_rgba = {TEXTYPE_RGBA , 4, 4, GL_RGBA, 3};
44 static textypeinfo_t textype_palette_alpha = {TEXTYPE_PALETTE, 1, 4, GL_RGBA, 4};
45 static textypeinfo_t textype_rgba_alpha = {TEXTYPE_RGBA , 4, 4, GL_RGBA, 4};
47 // a tiling texture (most common type)
48 #define GLIMAGETYPE_TILE 0
49 // a fragments texture (contains one or more fragment textures)
50 #define GLIMAGETYPE_FRAGMENTS 1
52 #define GLTEXTURETYPE_1D 0
53 #define GLTEXTURETYPE_2D 1
54 #define GLTEXTURETYPE_3D 2
55 #define GLTEXTURETYPE_CUBEMAP 3
57 static int gltexturetypeenums[4] = {GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D, GL_TEXTURE_CUBE_MAP_ARB};
58 static int gltexturetypebindingenums[4] = {GL_TEXTURE_BINDING_1D, GL_TEXTURE_BINDING_2D, GL_TEXTURE_BINDING_3D, GL_TEXTURE_BINDING_CUBE_MAP_ARB};
59 static int gltexturetypedimensions[4] = {1, 2, 3, 2};
60 static int cubemapside[6] =
62 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
63 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
64 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
65 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
66 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
67 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
70 // a gltextureimage can have one (or more if fragments) gltextures inside
71 typedef struct gltextureimage_s
73 struct gltextureimage_s *imagechain;
75 int type; // one of the GLIMAGETYPE_ values
76 int texturetype; // one of the GLTEXTURETYPE_ values
77 int sides; // 1 or 6 depending on texturetype
78 int texnum; // GL texture slot number
79 int width, height, depth; // 3D texture support
80 int bytesperpixel; // bytes per pixel
81 int glformat; // GL_RGB or GL_RGBA
82 int glinternalformat; // 3 or 4
84 short *blockallocation; // fragment allocation (2D only)
88 typedef struct gltexture_s
90 // this field is exposed to the R_GetTexture macro, for speed reasons
91 // (must be identical in rtexture_t)
92 int texnum; // GL texture slot number
94 // pointer to texturepool (check this to see if the texture is allocated)
95 struct gltexturepool_s *pool;
96 // pointer to next texture in texturepool chain
97 struct gltexture_s *chain;
98 // pointer into gltextureimage array
99 gltextureimage_t *image;
100 // name of the texture (this might be removed someday), no duplicates
102 // location in the image, and size
103 int x, y, z, width, height, depth;
104 // copy of the original texture(s) supplied to the upload function, for
105 // delayed uploads (non-precached)
107 // original data size in *inputtexels
109 // flags supplied to the LoadTexture function
110 // (might be altered to remove TEXF_ALPHA), and GLTEXF_ private flags
112 // pointer to one of the textype_ structs
113 textypeinfo_t *textype;
114 // one of the GLTEXTURETYPE_ values
116 // palette if the texture is TEXTYPE_PALETTE
117 const unsigned int *palette;
121 #define TEXTUREPOOL_SENTINEL 0xC0DEDBAD
123 typedef struct gltexturepool_s
125 unsigned int sentinel;
126 struct gltextureimage_s *imagechain;
127 struct gltexture_s *gltchain;
128 struct gltexturepool_s *next;
132 static gltexturepool_t *gltexturepoolchain = NULL;
134 static qbyte *resizebuffer = NULL, *colorconvertbuffer;
135 static int resizebuffersize = 0;
136 static qbyte *texturebuffer;
137 static int texturebuffersize = 0;
139 static int realmaxsize = 0;
141 static textypeinfo_t *R_GetTexTypeInfo(int textype, int flags)
143 if (flags & TEXF_ALPHA)
147 case TEXTYPE_PALETTE:
148 return &textype_palette_alpha;
150 Host_Error("R_GetTexTypeInfo: RGB format has no alpha, TEXF_ALPHA not allowed\n");
153 return &textype_rgba_alpha;
155 Host_Error("R_GetTexTypeInfo: unknown texture format\n");
163 case TEXTYPE_PALETTE:
164 return &textype_palette;
168 return &textype_rgba;
170 Host_Error("R_GetTexTypeInfo: unknown texture format\n");
176 static void R_UploadTexture(gltexture_t *t);
178 static void R_PrecacheTexture(gltexture_t *glt)
182 if (glt->flags & TEXF_ALWAYSPRECACHE)
184 else if (r_precachetextures.integer >= 2)
186 else if (r_precachetextures.integer >= 1)
187 if (glt->flags & TEXF_PRECACHE)
191 R_UploadTexture(glt);
194 int R_RealGetTexture(rtexture_t *rt)
199 glt = (gltexture_t *)rt;
200 if (glt->flags & GLTEXF_UPLOAD)
201 R_UploadTexture(glt);
202 glt->texnum = glt->image->texnum;
203 return glt->image->texnum;
209 void R_FreeTexture(rtexture_t *rt)
211 gltexture_t *glt, **gltpointer;
212 gltextureimage_t *image, **gltimagepointer;
214 glt = (gltexture_t *)rt;
216 Host_Error("R_FreeTexture: texture == NULL\n");
218 for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
219 if (*gltpointer == glt)
220 *gltpointer = glt->chain;
222 Host_Error("R_FreeTexture: texture \"%s\" not linked in pool\n", glt->identifier);
224 // note: if freeing a fragment texture, this will not make the claimed
225 // space available for new textures unless all other fragments in the
226 // image are also freed
230 image->texturecount--;
231 if (image->texturecount < 1)
233 for (gltimagepointer = &glt->pool->imagechain;*gltimagepointer && *gltimagepointer != image;gltimagepointer = &(*gltimagepointer)->imagechain);
234 if (*gltimagepointer == image)
235 *gltimagepointer = image->imagechain;
237 Host_Error("R_FreeTexture: image not linked in pool\n");
239 qglDeleteTextures(1, &image->texnum);
240 if (image->blockallocation)
241 Mem_Free(image->blockallocation);
247 Mem_Free(glt->identifier);
248 if (glt->inputtexels)
249 Mem_Free(glt->inputtexels);
254 static gltexture_t *R_FindTexture (gltexturepool_t *pool, char *identifier)
261 for (glt = pool->gltchain;glt;glt = glt->chain)
262 if (glt->identifier && !strcmp (identifier, glt->identifier))
269 rtexturepool_t *R_AllocTexturePool(void)
271 gltexturepool_t *pool;
272 if (texturemempool == NULL)
274 pool = Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
277 pool->next = gltexturepoolchain;
278 gltexturepoolchain = pool;
279 pool->sentinel = TEXTUREPOOL_SENTINEL;
280 return (rtexturepool_t *)pool;
283 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
285 gltexturepool_t *pool, **poolpointer;
286 if (rtexturepool == NULL)
288 if (*rtexturepool == NULL)
290 pool = (gltexturepool_t *)(*rtexturepool);
291 *rtexturepool = NULL;
292 if (pool->sentinel != TEXTUREPOOL_SENTINEL)
293 Host_Error("R_FreeTexturePool: pool already freed\n");
294 for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
295 if (*poolpointer == pool)
296 *poolpointer = pool->next;
298 Host_Error("R_FreeTexturePool: pool not linked\n");
299 while (pool->gltchain)
300 R_FreeTexture((rtexture_t *)pool->gltchain);
301 if (pool->imagechain)
302 Sys_Error("R_FreeTexturePool: not all images freed\n");
310 int minification, magnification;
314 static glmode_t modes[] =
316 {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
317 {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
318 {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
319 {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
320 {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
321 {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
324 static void GL_TextureMode_f (void)
328 gltextureimage_t *image;
329 gltexturepool_t *pool;
333 for (i = 0;i < 6;i++)
335 if (gl_filter_min == modes[i].minification)
337 Con_Printf ("%s\n", modes[i].name);
341 Con_Printf ("current filter is unknown???\n");
345 for (i = 0;i < 6;i++)
346 if (!strcasecmp (modes[i].name, Cmd_Argv(1) ) )
350 Con_Printf ("bad filter name\n");
354 gl_filter_min = modes[i].minification;
355 gl_filter_mag = modes[i].magnification;
357 // change all the existing mipmap texture objects
358 // FIXME: force renderer(/client/something?) restart instead?
359 for (pool = gltexturepoolchain;pool;pool = pool->next)
361 for (image = pool->imagechain;image;image = image->imagechain)
363 // only update already uploaded images
364 if (!(image->flags & GLTEXF_UPLOAD))
366 qglGetIntegerv(gltexturetypebindingenums[image->texturetype], &oldbindtexnum);
367 qglBindTexture(gltexturetypeenums[image->texturetype], image->texnum);
368 if (image->flags & TEXF_MIPMAP)
369 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_min);
371 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_mag);
372 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MAG_FILTER, gl_filter_mag);
373 qglBindTexture(gltexturetypeenums[image->texturetype], oldbindtexnum);
379 static int R_CalcTexelDataSize (gltexture_t *glt)
381 int width2, height2, depth2, size;
382 if (glt->flags & TEXF_FRAGMENT)
383 size = glt->width * glt->height * glt->depth;
386 if (r_max_size.integer > realmaxsize)
387 Cvar_SetValue("r_max_size", realmaxsize);
388 // calculate final size
389 for (width2 = 1;width2 < glt->width;width2 <<= 1);
390 for (height2 = 1;height2 < glt->height;height2 <<= 1);
391 for (depth2 = 1;depth2 < glt->depth;depth2 <<= 1);
392 for (width2 >>= r_picmip.integer;width2 > r_max_size.integer;width2 >>= 1);
393 for (height2 >>= r_picmip.integer;height2 > r_max_size.integer;height2 >>= 1);
394 for (depth2 >>= r_picmip.integer;depth2 > r_max_size.integer;depth2 >>= 1);
395 if (width2 < 1) width2 = 1;
396 if (height2 < 1) height2 = 1;
397 if (depth2 < 1) depth2 = 1;
400 if (glt->flags & TEXF_MIPMAP)
402 while (width2 > 1 || height2 > 1 || depth2 > 1)
404 size += width2 * height2 * depth2;
412 size++; // count the last 1x1 mipmap
415 size = width2 * height2 * depth2;
417 size *= glt->textype->internalbytesperpixel * glt->image->sides;
422 void R_TextureStats_PrintTotal(void)
424 int glsize, total = 0, totalt = 0, totalp = 0, loaded = 0, loadedt = 0, loadedp = 0;
426 gltexturepool_t *pool;
427 for (pool = gltexturepoolchain;pool;pool = pool->next)
429 for (glt = pool->gltchain;glt;glt = glt->chain)
431 glsize = R_CalcTexelDataSize(glt);
434 totalp += glt->inputdatasize;
435 if (!(glt->flags & GLTEXF_UPLOAD))
439 loadedp += glt->inputdatasize;
443 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);
446 static void R_TextureStats_f(void)
450 gltexturepool_t *pool;
451 Con_Printf("glsize input loaded mip alpha name\n");
452 for (pool = gltexturepoolchain;pool;pool = pool->next)
454 for (glt = pool->gltchain;glt;glt = glt->chain)
456 loaded = !(glt->flags & GLTEXF_UPLOAD);
457 Con_Printf("%c%4i%c%c%4i%c %s %s %s %s\n", loaded ? '[' : ' ', (R_CalcTexelDataSize(glt) + 1023) / 1024, loaded ? ']' : ' ', glt->inputtexels ? '[' : ' ', (glt->inputdatasize + 1023) / 1024, glt->inputtexels ? ']' : ' ', loaded ? "loaded" : " ", (glt->flags & TEXF_MIPMAP) ? "mip" : " ", (glt->flags & TEXF_ALPHA) ? "alpha" : " ", glt->identifier ? glt->identifier : "<unnamed>");
459 Con_Printf("pool %10p\n", pool);
461 R_TextureStats_PrintTotal();
464 char engineversion[40];
466 static void r_textures_start(void)
468 // deal with size limits of various drivers (3dfx in particular)
469 qglGetIntegerv(GL_MAX_TEXTURE_SIZE, &realmaxsize);
471 // LordHavoc: allow any alignment
472 qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);
475 // use the largest scrap texture size we can (not sure if this is really a good idea)
476 for (block_size = 1;block_size < realmaxsize && block_size < r_max_scrapsize.integer;block_size <<= 1);
478 texturemempool = Mem_AllocPool("Texture Info");
479 texturedatamempool = Mem_AllocPool("Texture Storage (not yet uploaded)");
480 textureprocessingmempool = Mem_AllocPool("Texture Processing Buffers");
482 // Disable JPEG screenshots if the DLL isn't loaded
483 if (! JPEG_OpenLibrary ())
484 Cvar_SetValueQuick (&scr_screenshot_jpeg, 0);
487 static void r_textures_shutdown(void)
489 rtexturepool_t *temp;
491 JPEG_CloseLibrary ();
493 while(gltexturepoolchain)
495 temp = (rtexturepool_t *) gltexturepoolchain;
496 R_FreeTexturePool(&temp);
499 resizebuffersize = 0;
500 texturebuffersize = 0;
502 colorconvertbuffer = NULL;
503 texturebuffer = NULL;
504 Mem_FreePool(&texturemempool);
505 Mem_FreePool(&texturedatamempool);
506 Mem_FreePool(&textureprocessingmempool);
509 static void r_textures_newmap(void)
513 void R_Textures_Init (void)
515 Cmd_AddCommand("gl_texturemode", &GL_TextureMode_f);
516 Cmd_AddCommand("r_texturestats", R_TextureStats_f);
517 Cvar_RegisterVariable (&r_max_scrapsize);
518 Cvar_RegisterVariable (&r_max_size);
519 Cvar_RegisterVariable (&r_picmip);
520 Cvar_RegisterVariable (&r_lerpimages);
521 Cvar_RegisterVariable (&r_precachetextures);
523 R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
526 void R_Textures_Frame (void)
528 // could do procedural texture animation here, if we keep track of which
529 // textures were accessed this frame...
531 // free the resize buffers
532 resizebuffersize = 0;
535 Mem_Free(resizebuffer);
538 if (colorconvertbuffer)
540 Mem_Free(colorconvertbuffer);
541 colorconvertbuffer = NULL;
545 void R_MakeResizeBufferBigger(int size)
547 if (resizebuffersize < size)
549 resizebuffersize = size;
551 Mem_Free(resizebuffer);
552 if (colorconvertbuffer)
553 Mem_Free(colorconvertbuffer);
554 resizebuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
555 colorconvertbuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
556 if (!resizebuffer || !colorconvertbuffer)
557 Host_Error("R_Upload: out of memory\n");
561 static void GL_SetupTextureParameters(int flags, int texturetype)
563 int textureenum = gltexturetypeenums[texturetype];
564 int wrapmode = ((flags & TEXF_CLAMP) && gl_support_clamptoedge) ? GL_CLAMP_TO_EDGE : GL_REPEAT;
568 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_S, wrapmode);
569 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_T, wrapmode);
570 if (gltexturetypedimensions[texturetype] >= 3)
571 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_R, wrapmode);
573 if (flags & TEXF_FORCENEAREST)
575 if (flags & TEXF_MIPMAP)
576 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
578 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
579 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
581 else if (flags & TEXF_FORCELINEAR)
583 if (flags & TEXF_MIPMAP)
585 if (gl_filter_min == GL_NEAREST_MIPMAP_LINEAR || gl_filter_min == GL_LINEAR_MIPMAP_LINEAR)
586 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
588 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
591 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
592 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
596 if (flags & TEXF_MIPMAP)
597 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_min);
599 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
600 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
606 static void R_Upload(gltexture_t *glt, qbyte *data)
608 int i, mip, width, height, depth;
615 glt->texnum = glt->image->texnum;
616 // we need to restore the texture binding after finishing the upload
617 qglGetIntegerv(gltexturetypebindingenums[glt->image->texturetype], &oldbindtexnum);
618 qglBindTexture(gltexturetypeenums[glt->image->texturetype], glt->image->texnum);
620 glt->flags &= ~GLTEXF_UPLOAD;
622 if (glt->flags & TEXF_FRAGMENT)
624 if (glt->image->flags & GLTEXF_UPLOAD)
626 glt->image->flags &= ~GLTEXF_UPLOAD;
627 Con_DPrintf("uploaded new fragments image\n");
628 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
629 memset(resizebuffer, 255, glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
630 switch(glt->image->texturetype)
632 case GLTEXTURETYPE_1D:
633 qglTexImage1D(GL_TEXTURE_1D, 0, glt->image->glinternalformat, glt->image->width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
636 case GLTEXTURETYPE_2D:
637 qglTexImage2D(GL_TEXTURE_2D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
640 case GLTEXTURETYPE_3D:
641 qglTexImage3D(GL_TEXTURE_3D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, glt->image->depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
645 Host_Error("R_Upload: fragment texture of type other than 1D, 2D, or 3D\n");
648 GL_SetupTextureParameters(glt->image->flags, glt->image->texturetype);
651 if (prevbuffer == NULL)
653 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
654 memset(resizebuffer, 255, glt->width * glt->height * glt->image->depth * glt->image->bytesperpixel);
655 prevbuffer = resizebuffer;
657 else if (glt->textype->textype == TEXTYPE_PALETTE)
659 // promote paletted to RGBA, so we only have to worry about RGB and
660 // RGBA in the rest of this code
661 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->sides * glt->image->bytesperpixel);
662 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height * glt->depth, glt->palette);
663 prevbuffer = colorconvertbuffer;
666 switch(glt->image->texturetype)
668 case GLTEXTURETYPE_1D:
669 qglTexSubImage1D(GL_TEXTURE_1D, 0, glt->x, glt->width, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
672 case GLTEXTURETYPE_2D:
673 qglTexSubImage2D(GL_TEXTURE_2D, 0, glt->x, glt->y, glt->width, glt->height, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
676 case GLTEXTURETYPE_3D:
677 qglTexSubImage3D(GL_TEXTURE_3D, 0, glt->x, glt->y, glt->z, glt->width, glt->height, glt->depth, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
681 Host_Error("R_Upload: fragment texture of type other than 1D, 2D, or 3D\n");
687 glt->image->flags &= ~GLTEXF_UPLOAD;
689 // these are rounded up versions of the size to do better resampling
690 for (width = 1;width < glt->width ;width <<= 1);
691 for (height = 1;height < glt->height;height <<= 1);
692 for (depth = 1;depth < glt->depth ;depth <<= 1);
694 R_MakeResizeBufferBigger(width * height * depth * glt->image->sides * glt->image->bytesperpixel);
696 if (prevbuffer == NULL)
698 width = glt->image->width;
699 height = glt->image->height;
700 depth = glt->image->depth;
701 memset(resizebuffer, 255, width * height * depth * glt->image->bytesperpixel);
702 prevbuffer = resizebuffer;
706 if (glt->textype->textype == TEXTYPE_PALETTE)
708 // promote paletted to RGBA, so we only have to worry about RGB and
709 // RGBA in the rest of this code
710 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height * glt->depth * glt->image->sides, glt->palette);
711 prevbuffer = colorconvertbuffer;
715 // cubemaps contain multiple images and thus get processed a bit differently
716 if (glt->image->texturetype != GLTEXTURETYPE_CUBEMAP)
718 if (glt->width != width || glt->height != height || glt->depth != depth)
720 Image_Resample(prevbuffer, glt->width, glt->height, glt->depth, resizebuffer, width, height, depth, glt->image->bytesperpixel, r_lerpimages.integer);
721 prevbuffer = resizebuffer;
724 while (width > glt->image->width || height > glt->image->height || depth > glt->image->depth)
726 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->image->width, glt->image->height, glt->image->depth, glt->image->bytesperpixel);
727 prevbuffer = resizebuffer;
731 switch(glt->image->texturetype)
733 case GLTEXTURETYPE_1D:
734 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->image->glinternalformat, width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
736 if (glt->flags & TEXF_MIPMAP)
738 while (width > 1 || height > 1 || depth > 1)
740 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
741 prevbuffer = resizebuffer;
742 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->image->glinternalformat, width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
747 case GLTEXTURETYPE_2D:
748 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
750 if (glt->flags & TEXF_MIPMAP)
752 while (width > 1 || height > 1 || depth > 1)
754 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
755 prevbuffer = resizebuffer;
756 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
761 case GLTEXTURETYPE_3D:
762 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->image->glinternalformat, width, height, depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
764 if (glt->flags & TEXF_MIPMAP)
766 while (width > 1 || height > 1 || depth > 1)
768 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
769 prevbuffer = resizebuffer;
770 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->image->glinternalformat, width, height, depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
775 case GLTEXTURETYPE_CUBEMAP:
776 // convert and upload each side in turn,
777 // from a continuous block of input texels
778 texturebuffer = prevbuffer;
779 for (i = 0;i < 6;i++)
781 prevbuffer = texturebuffer;
782 texturebuffer += width * height * depth * glt->textype->inputbytesperpixel;
783 if (glt->width != width || glt->height != height || glt->depth != depth)
785 Image_Resample(prevbuffer, glt->width, glt->height, glt->depth, resizebuffer, width, height, depth, glt->image->bytesperpixel, r_lerpimages.integer);
786 prevbuffer = resizebuffer;
789 while (width > glt->image->width || height > glt->image->height || depth > glt->image->depth)
791 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->image->width, glt->image->height, glt->image->depth, glt->image->bytesperpixel);
792 prevbuffer = resizebuffer;
795 qglTexImage2D(cubemapside[i], mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
797 if (glt->flags & TEXF_MIPMAP)
799 while (width > 1 || height > 1 || depth > 1)
801 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
802 prevbuffer = resizebuffer;
803 qglTexImage2D(cubemapside[i], mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
810 GL_SetupTextureParameters(glt->image->flags, glt->image->texturetype);
812 qglBindTexture(gltexturetypeenums[glt->image->texturetype], oldbindtexnum);
815 static void R_FindImageForTexture(gltexture_t *glt)
817 int i, j, best, best2, x, y, z, w, h, d;
818 textypeinfo_t *texinfo;
819 gltexturepool_t *pool;
820 gltextureimage_t *image, **imagechainpointer;
821 texinfo = glt->textype;
824 // remains -1 until uploaded
833 if (glt->flags & TEXF_FRAGMENT)
835 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain)
837 image = *imagechainpointer;
838 if (image->type != GLIMAGETYPE_FRAGMENTS)
840 if (image->texturetype != glt->texturetype)
842 if ((image->flags ^ glt->flags) & (TEXF_MIPMAP | TEXF_ALPHA | TEXF_CLAMP))
844 if (image->glformat != texinfo->glformat || image->glinternalformat != texinfo->glinternalformat)
846 if (glt->width > image->width || glt->height > image->height || glt->depth > image->depth)
849 // got a fragments texture, find a place in it if we can
850 for (best = image->width, i = 0;i < image->width - w;i++)
852 for (best2 = 0, j = 0;j < w;j++)
854 if (image->blockallocation[i+j] >= best)
856 if (best2 < image->blockallocation[i+j])
857 best2 = image->blockallocation[i+j];
861 // this is a valid spot
867 if (best + h > image->height)
870 for (i = 0;i < w;i++)
871 image->blockallocation[x + i] = best + h;
877 image->texturecount++;
881 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
883 Sys_Error("R_FindImageForTexture: ran out of memory\n");
884 image->type = GLIMAGETYPE_FRAGMENTS;
885 // make sure the created image is big enough for the fragment
886 for (image->width = block_size;image->width < glt->width;image->width <<= 1);
888 if (gltexturetypedimensions[glt->texturetype] >= 2)
889 for (image->height = block_size;image->height < glt->height;image->height <<= 1);
891 if (gltexturetypedimensions[glt->texturetype] >= 3)
892 for (image->depth = block_size;image->depth < glt->depth;image->depth <<= 1);
893 image->blockallocation = Mem_Alloc(texturemempool, image->width * sizeof(short));
894 memset(image->blockallocation, 0, image->width * sizeof(short));
899 for (i = 0;i < w;i++)
900 image->blockallocation[x + i] = y + h;
904 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain);
906 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
908 Sys_Error("R_FindImageForTexture: ran out of memory\n");
909 image->type = GLIMAGETYPE_TILE;
910 image->blockallocation = NULL;
912 // calculate final size
913 if (r_max_size.integer > realmaxsize)
914 Cvar_SetValue("r_max_size", realmaxsize);
915 for (image->width = 1;image->width < glt->width;image->width <<= 1);
916 for (image->height = 1;image->height < glt->height;image->height <<= 1);
917 for (image->depth = 1;image->depth < glt->depth;image->depth <<= 1);
918 for (image->width >>= r_picmip.integer;image->width > r_max_size.integer;image->width >>= 1);
919 for (image->height >>= r_picmip.integer;image->height > r_max_size.integer;image->height >>= 1);
920 for (image->depth >>= r_picmip.integer;image->depth > r_max_size.integer;image->depth >>= 1);
921 if (image->width < 1) image->width = 1;
922 if (image->height < 1) image->height = 1;
923 if (image->depth < 1) image->depth = 1;
925 image->texturetype = glt->texturetype;
926 image->glinternalformat = texinfo->glinternalformat;
927 image->glformat = texinfo->glformat;
928 image->flags = (glt->flags & (TEXF_MIPMAP | TEXF_ALPHA | TEXF_CLAMP)) | GLTEXF_UPLOAD;
929 image->bytesperpixel = texinfo->internalbytesperpixel;
930 image->sides = image->texturetype == GLTEXTURETYPE_CUBEMAP ? 6 : 1;
931 // get a texture number to use
932 qglGenTextures(1, &image->texnum);
933 *imagechainpointer = image;
934 image->texturecount++;
942 // note: R_FindImageForTexture must be called before this
943 static void R_UploadTexture (gltexture_t *glt)
945 if (!(glt->flags & GLTEXF_UPLOAD))
948 R_Upload(glt, glt->inputtexels);
949 if (glt->inputtexels)
951 Mem_Free(glt->inputtexels);
952 glt->inputtexels = NULL;
953 glt->flags |= GLTEXF_DESTROYED;
955 else if (glt->flags & GLTEXF_DESTROYED)
956 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
959 static rtexture_t *R_SetupTexture(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, int depth, int sides, int flags, int textype, int texturetype, const qbyte *data, const unsigned int *palette)
963 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
964 textypeinfo_t *texinfo;
966 if (cls.state == ca_dedicated)
969 if (flags & TEXF_FRAGMENT && texturetype != GLTEXTURETYPE_2D)
970 Sys_Error("R_LoadTexture: only 2D fragment textures implemented\n");
971 if (texturetype == GLTEXTURETYPE_CUBEMAP && !gl_texturecubemap)
972 Sys_Error("R_LoadTexture: cubemap texture not supported by driver\n");
973 if (texturetype == GLTEXTURETYPE_3D && !gl_texture3d)
974 Sys_Error("R_LoadTexture: 3d texture not supported by driver\n");
977 glt = R_FindTexture (pool, identifier);
980 Con_Printf("R_LoadTexture: replacing existing texture %s\n", identifier);
981 R_FreeTexture((rtexture_t *)glt);
985 texinfo = R_GetTexTypeInfo(textype, flags);
986 size = width * height * depth * sides * texinfo->inputbytesperpixel;
988 Sys_Error("R_LoadTexture: bogus texture size (%dx%dx%dx%dbppx%dsides = %d bytes)\n", width, height, depth, texinfo->inputbytesperpixel * 8, sides);
990 // clear the alpha flag if the texture has no transparent pixels
993 case TEXTYPE_PALETTE:
994 if (flags & TEXF_ALPHA)
996 flags &= ~TEXF_ALPHA;
999 for (i = 0;i < size;i++)
1001 if (((qbyte *)&palette[data[i]])[3] < 255)
1003 flags |= TEXF_ALPHA;
1011 if (flags & TEXF_ALPHA)
1012 Host_Error("R_LoadTexture: RGB has no alpha, don't specify TEXF_ALPHA\n");
1015 if (flags & TEXF_ALPHA)
1017 flags &= ~TEXF_ALPHA;
1020 for (i = 3;i < size;i += 4)
1024 flags |= TEXF_ALPHA;
1032 Host_Error("R_LoadTexture: unknown texture type\n");
1035 glt = Mem_Alloc(texturemempool, sizeof(gltexture_t));
1038 glt->identifier = Mem_Alloc(texturemempool, strlen(identifier)+1);
1039 strcpy (glt->identifier, identifier);
1042 glt->identifier = NULL;
1044 glt->chain = pool->gltchain;
1045 pool->gltchain = glt;
1047 glt->height = height;
1049 glt->flags = flags | GLTEXF_UPLOAD;
1050 glt->textype = texinfo;
1051 glt->texturetype = texturetype;
1052 glt->inputdatasize = size;
1053 glt->palette = palette;
1057 glt->inputtexels = Mem_Alloc(texturedatamempool, size);
1058 if (glt->inputtexels == NULL)
1059 Sys_Error("R_LoadTexture: out of memory\n");
1060 memcpy(glt->inputtexels, data, size);
1063 glt->inputtexels = NULL;
1065 R_FindImageForTexture(glt);
1066 R_PrecacheTexture(glt);
1068 return (rtexture_t *)glt;
1071 rtexture_t *R_LoadTexture1D(rtexturepool_t *rtexturepool, const char *identifier, int width, const qbyte *data, int textype, int flags, const unsigned int *palette)
1073 return R_SetupTexture(rtexturepool, identifier, width, 1, 1, 1, flags, textype, GLTEXTURETYPE_1D, data, palette);
1076 rtexture_t *R_LoadTexture2D(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, const qbyte *data, int textype, int flags, const unsigned int *palette)
1078 return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, flags, textype, GLTEXTURETYPE_2D, data, palette);
1081 rtexture_t *R_LoadTexture3D(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, int depth, const qbyte *data, int textype, int flags, const unsigned int *palette)
1083 return R_SetupTexture(rtexturepool, identifier, width, height, depth, 1, flags, textype, GLTEXTURETYPE_3D, data, palette);
1086 rtexture_t *R_LoadTextureCubeMap(rtexturepool_t *rtexturepool, const char *identifier, int width, const qbyte *data, int textype, int flags, const unsigned int *palette)
1088 return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, flags, textype, GLTEXTURETYPE_CUBEMAP, data, palette);
1091 int R_TextureHasAlpha(rtexture_t *rt)
1093 return rt ? (((gltexture_t *)rt)->flags & TEXF_ALPHA) != 0 : false;
1096 int R_TextureWidth(rtexture_t *rt)
1098 return rt ? ((gltexture_t *)rt)->width : 0;
1101 int R_TextureHeight(rtexture_t *rt)
1103 return rt ? ((gltexture_t *)rt)->height : 0;
1106 void R_FragmentLocation3D(rtexture_t *rt, int *x, int *y, int *z, float *fx1, float *fy1, float *fz1, float *fx2, float *fy2, float *fz2)
1109 float iwidth, iheight, idepth;
1110 if (cls.state == ca_dedicated)
1118 if (fx1 || fy1 || fx2 || fy2)
1136 Host_Error("R_FragmentLocation: no texture supplied\n");
1137 glt = (gltexture_t *)rt;
1138 if (glt->flags & TEXF_FRAGMENT)
1144 if (fx1 || fy1 || fx2 || fy2)
1146 iwidth = 1.0f / glt->image->width;
1147 iheight = 1.0f / glt->image->height;
1148 idepth = 1.0f / glt->image->depth;
1150 *fx1 = glt->x * iwidth;
1152 *fy1 = glt->y * iheight;
1154 *fz1 = glt->z * idepth;
1156 *fx2 = (glt->x + glt->width) * iwidth;
1158 *fy2 = (glt->y + glt->height) * iheight;
1160 *fz2 = (glt->z + glt->depth) * idepth;
1171 if (fx1 || fy1 || fx2 || fy2)
1189 void R_FragmentLocation(rtexture_t *rt, int *x, int *y, float *fx1, float *fy1, float *fx2, float *fy2)
1191 R_FragmentLocation3D(rt, x, y, NULL, fx1, fy1, NULL, fx2, fy2, NULL);
1194 int R_CompatibleFragmentWidth(int width, int textype, int flags)
1199 void R_UpdateTexture(rtexture_t *rt, qbyte *data)
1203 Host_Error("R_UpdateTexture: no texture supplied\n");
1205 Host_Error("R_UpdateTexture: no data supplied\n");
1206 glt = (gltexture_t *)rt;
1208 // if it has not been uploaded yet, update the data that will be used when it is
1209 if (glt->inputtexels)
1210 memcpy(glt->inputtexels, data, glt->inputdatasize);
1212 R_Upload(glt, data);