6 cvar_t gl_max_size = {CVAR_SAVE, "gl_max_size", "2048"};
7 cvar_t gl_max_scrapsize = {CVAR_SAVE, "gl_max_scrapsize", "256"};
8 cvar_t gl_picmip = {CVAR_SAVE, "gl_picmip", "0"};
9 cvar_t r_lerpimages = {CVAR_SAVE, "r_lerpimages", "1"};
10 cvar_t r_precachetextures = {CVAR_SAVE, "r_precachetextures", "1"};
11 cvar_t gl_texture_anisotropy = {CVAR_SAVE, "gl_texture_anisotropy", "1"};
13 int gl_filter_min = GL_LINEAR_MIPMAP_LINEAR;
14 int gl_filter_mag = GL_LINEAR;
17 static mempool_t *texturemempool;
19 // note: this must not conflict with TEXF_ flags in r_textures.h
20 // cleared when a texture is uploaded
21 #define GLTEXF_UPLOAD 0x00010000
22 // bitmask for mismatch checking
23 #define GLTEXF_IMPORTANTBITS (0)
24 // set when image is uploaded and freed
25 #define GLTEXF_DESTROYED 0x00040000
27 // size of images which hold fragment textures, ignores picmip and max_size
28 static int block_size;
30 typedef struct textypeinfo_s
33 int inputbytesperpixel;
34 int internalbytesperpixel;
40 static textypeinfo_t textype_palette = {TEXTYPE_PALETTE, 1, 4, GL_RGBA , 3};
41 static textypeinfo_t textype_rgb = {TEXTYPE_RGB , 3, 3, GL_RGB , 3};
42 static textypeinfo_t textype_rgba = {TEXTYPE_RGBA , 4, 4, GL_RGBA , 3};
43 static textypeinfo_t textype_palette_alpha = {TEXTYPE_PALETTE, 1, 4, GL_RGBA , 4};
44 static textypeinfo_t textype_rgba_alpha = {TEXTYPE_RGBA , 4, 4, GL_RGBA , 4};
45 static textypeinfo_t textype_dsdt = {TEXTYPE_DSDT , 2, 2, GL_DSDT_NV, GL_DSDT8_NV};
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 return &textype_dsdt;
172 Host_Error("R_GetTexTypeInfo: unknown texture format\n");
178 static void R_UploadTexture(gltexture_t *t);
180 static void R_PrecacheTexture(gltexture_t *glt)
184 if (glt->flags & TEXF_ALWAYSPRECACHE)
186 else if (r_precachetextures.integer >= 2)
188 else if (r_precachetextures.integer >= 1)
189 if (glt->flags & TEXF_PRECACHE)
193 R_UploadTexture(glt);
196 int R_RealGetTexture(rtexture_t *rt)
201 glt = (gltexture_t *)rt;
202 if (glt->flags & GLTEXF_UPLOAD)
203 R_UploadTexture(glt);
204 glt->texnum = glt->image->texnum;
205 return glt->image->texnum;
211 void R_FreeTexture(rtexture_t *rt)
213 gltexture_t *glt, **gltpointer;
214 gltextureimage_t *image, **gltimagepointer;
216 glt = (gltexture_t *)rt;
218 Host_Error("R_FreeTexture: texture == NULL\n");
220 for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
221 if (*gltpointer == glt)
222 *gltpointer = glt->chain;
224 Host_Error("R_FreeTexture: texture \"%s\" not linked in pool\n", glt->identifier);
226 // note: if freeing a fragment texture, this will not make the claimed
227 // space available for new textures unless all other fragments in the
228 // image are also freed
232 image->texturecount--;
233 if (image->texturecount < 1)
235 for (gltimagepointer = &glt->pool->imagechain;*gltimagepointer && *gltimagepointer != image;gltimagepointer = &(*gltimagepointer)->imagechain);
236 if (*gltimagepointer == image)
237 *gltimagepointer = image->imagechain;
239 Host_Error("R_FreeTexture: image not linked in pool\n");
241 qglDeleteTextures(1, (GLuint *)&image->texnum);
242 if (image->blockallocation)
243 Mem_Free(image->blockallocation);
248 if (glt->inputtexels)
249 Mem_Free(glt->inputtexels);
253 rtexturepool_t *R_AllocTexturePool(void)
255 gltexturepool_t *pool;
256 if (texturemempool == NULL)
258 pool = (gltexturepool_t *)Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
261 pool->next = gltexturepoolchain;
262 gltexturepoolchain = pool;
263 pool->sentinel = TEXTUREPOOL_SENTINEL;
264 return (rtexturepool_t *)pool;
267 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
269 gltexturepool_t *pool, **poolpointer;
270 if (rtexturepool == NULL)
272 if (*rtexturepool == NULL)
274 pool = (gltexturepool_t *)(*rtexturepool);
275 *rtexturepool = NULL;
276 if (pool->sentinel != TEXTUREPOOL_SENTINEL)
277 Host_Error("R_FreeTexturePool: pool already freed\n");
278 for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
279 if (*poolpointer == pool)
280 *poolpointer = pool->next;
282 Host_Error("R_FreeTexturePool: pool not linked\n");
283 while (pool->gltchain)
284 R_FreeTexture((rtexture_t *)pool->gltchain);
285 if (pool->imagechain)
286 Con_Printf("R_FreeTexturePool: not all images freed\n");
291 typedef struct glmode_s
294 int minification, magnification;
298 static glmode_t modes[] =
300 {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
301 {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
302 {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
303 {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
304 {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
305 {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
308 static void GL_TextureMode_f (void)
312 gltextureimage_t *image;
313 gltexturepool_t *pool;
317 for (i = 0;i < 6;i++)
319 if (gl_filter_min == modes[i].minification)
321 Con_Printf("%s\n", modes[i].name);
325 Con_Print("current filter is unknown???\n");
329 for (i = 0;i < 6;i++)
330 if (!strcasecmp (modes[i].name, Cmd_Argv(1) ) )
334 Con_Print("bad filter name\n");
338 gl_filter_min = modes[i].minification;
339 gl_filter_mag = modes[i].magnification;
341 // change all the existing mipmap texture objects
342 // FIXME: force renderer(/client/something?) restart instead?
343 for (pool = gltexturepoolchain;pool;pool = pool->next)
345 for (image = pool->imagechain;image;image = image->imagechain)
347 // only update already uploaded images
348 if (!(image->flags & GLTEXF_UPLOAD) && !(image->flags & (TEXF_FORCENEAREST | TEXF_FORCELINEAR)))
350 qglGetIntegerv(gltexturetypebindingenums[image->texturetype], &oldbindtexnum);
351 qglBindTexture(gltexturetypeenums[image->texturetype], image->texnum);
352 if (image->flags & TEXF_MIPMAP)
353 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_min);
355 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_mag);
356 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MAG_FILTER, gl_filter_mag);
357 qglBindTexture(gltexturetypeenums[image->texturetype], oldbindtexnum);
363 static int R_CalcTexelDataSize (gltexture_t *glt)
365 int width2, height2, depth2, size, picmip;
366 if (glt->flags & TEXF_FRAGMENT)
367 size = glt->width * glt->height * glt->depth;
371 if (glt->flags & TEXF_PICMIP)
372 picmip = gl_picmip.integer;
373 if (gl_max_size.integer > realmaxsize)
374 Cvar_SetValue("gl_max_size", realmaxsize);
375 // calculate final size
376 for (width2 = 1;width2 < glt->width;width2 <<= 1);
377 for (height2 = 1;height2 < glt->height;height2 <<= 1);
378 for (depth2 = 1;depth2 < glt->depth;depth2 <<= 1);
379 for (width2 >>= picmip;width2 > gl_max_size.integer;width2 >>= 1);
380 for (height2 >>= picmip;height2 > gl_max_size.integer;height2 >>= 1);
381 for (depth2 >>= picmip;depth2 > gl_max_size.integer;depth2 >>= 1);
382 if (width2 < 1) width2 = 1;
383 if (height2 < 1) height2 = 1;
384 if (depth2 < 1) depth2 = 1;
387 if (glt->flags & TEXF_MIPMAP)
389 while (width2 > 1 || height2 > 1 || depth2 > 1)
391 size += width2 * height2 * depth2;
399 size++; // count the last 1x1 mipmap
402 size = width2 * height2 * depth2;
404 size *= glt->textype->internalbytesperpixel * glt->image->sides;
409 void R_TextureStats_Print(qboolean printeach, qboolean printpool, qboolean printtotal)
413 int pooltotal = 0, pooltotalt = 0, pooltotalp = 0, poolloaded = 0, poolloadedt = 0, poolloadedp = 0;
414 int sumtotal = 0, sumtotalt = 0, sumtotalp = 0, sumloaded = 0, sumloadedt = 0, sumloadedp = 0;
416 gltexturepool_t *pool;
418 Con_Print("glsize input loaded mip alpha name\n");
419 for (pool = gltexturepoolchain;pool;pool = pool->next)
427 for (glt = pool->gltchain;glt;glt = glt->chain)
429 glsize = R_CalcTexelDataSize(glt);
430 isloaded = !(glt->flags & GLTEXF_UPLOAD);
432 pooltotalt += glsize;
433 pooltotalp += glt->inputdatasize;
437 poolloadedt += glsize;
438 poolloadedp += glt->inputdatasize;
441 Con_Printf("%c%4i%c%c%4i%c %s %s %s %s\n", isloaded ? '[' : ' ', (glsize + 1023) / 1024, isloaded ? ']' : ' ', glt->inputtexels ? '[' : ' ', (glt->inputdatasize + 1023) / 1024, glt->inputtexels ? ']' : ' ', isloaded ? "loaded" : " ", (glt->flags & TEXF_MIPMAP) ? "mip" : " ", (glt->flags & TEXF_ALPHA) ? "alpha" : " ", glt->identifier);
444 Con_Printf("texturepool %10p total: %i (%.3fMB, %.3fMB original), uploaded %i (%.3fMB, %.3fMB original), upload on demand %i (%.3fMB, %.3fMB original)\n", pool, pooltotal, pooltotalt / 1048576.0, pooltotalp / 1048576.0, poolloaded, poolloadedt / 1048576.0, poolloadedp / 1048576.0, pooltotal - poolloaded, (pooltotalt - poolloadedt) / 1048576.0, (pooltotalp - poolloadedp) / 1048576.0);
445 sumtotal += pooltotal;
446 sumtotalt += pooltotalt;
447 sumtotalp += pooltotalp;
448 sumloaded += poolloaded;
449 sumloadedt += poolloadedt;
450 sumloadedp += poolloadedp;
453 Con_Printf("textures total: %i (%.3fMB, %.3fMB original), uploaded %i (%.3fMB, %.3fMB original), upload on demand %i (%.3fMB, %.3fMB original)\n", sumtotal, sumtotalt / 1048576.0, sumtotalp / 1048576.0, sumloaded, sumloadedt / 1048576.0, sumloadedp / 1048576.0, sumtotal - sumloaded, (sumtotalt - sumloadedt) / 1048576.0, (sumtotalp - sumloadedp) / 1048576.0);
456 static void R_TextureStats_f(void)
458 R_TextureStats_Print(true, true, true);
461 static void r_textures_start(void)
463 // deal with size limits of various drivers (3dfx in particular)
464 qglGetIntegerv(GL_MAX_TEXTURE_SIZE, &realmaxsize);
466 // LordHavoc: allow any alignment
467 qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);
470 // use the largest scrap texture size we can (not sure if this is really a good idea)
471 for (block_size = 1;block_size < realmaxsize && block_size < gl_max_scrapsize.integer;block_size <<= 1);
473 texturemempool = Mem_AllocPool("texture management", 0, NULL);
475 // Disable JPEG screenshots if the DLL isn't loaded
476 if (! JPEG_OpenLibrary ())
477 Cvar_SetValueQuick (&scr_screenshot_jpeg, 0);
480 static void r_textures_shutdown(void)
482 rtexturepool_t *temp;
484 JPEG_CloseLibrary ();
486 while(gltexturepoolchain)
488 temp = (rtexturepool_t *) gltexturepoolchain;
489 R_FreeTexturePool(&temp);
492 resizebuffersize = 0;
493 texturebuffersize = 0;
495 colorconvertbuffer = NULL;
496 texturebuffer = NULL;
497 Mem_FreePool(&texturemempool);
500 static void r_textures_newmap(void)
504 void R_Textures_Init (void)
506 Cmd_AddCommand("gl_texturemode", &GL_TextureMode_f);
507 Cmd_AddCommand("r_texturestats", R_TextureStats_f);
508 Cvar_RegisterVariable (&gl_max_scrapsize);
509 Cvar_RegisterVariable (&gl_max_size);
510 Cvar_RegisterVariable (&gl_picmip);
511 Cvar_RegisterVariable (&r_lerpimages);
512 Cvar_RegisterVariable (&r_precachetextures);
513 Cvar_RegisterVariable (&gl_texture_anisotropy);
515 R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
518 void R_Textures_Frame (void)
520 static int old_aniso = 0;
522 // could do procedural texture animation here, if we keep track of which
523 // textures were accessed this frame...
525 // free the resize buffers
526 resizebuffersize = 0;
529 Mem_Free(resizebuffer);
532 if (colorconvertbuffer)
534 Mem_Free(colorconvertbuffer);
535 colorconvertbuffer = NULL;
538 if (old_aniso != gl_texture_anisotropy.integer)
540 gltextureimage_t *image;
541 gltexturepool_t *pool;
544 old_aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
546 Cvar_SetValueQuick(&gl_texture_anisotropy, old_aniso);
548 for (pool = gltexturepoolchain;pool;pool = pool->next)
550 for (image = pool->imagechain;image;image = image->imagechain)
552 // only update already uploaded images
553 if (!(image->flags & GLTEXF_UPLOAD) && (image->flags & TEXF_MIPMAP))
555 qglGetIntegerv(gltexturetypebindingenums[image->texturetype], &oldbindtexnum);
557 qglBindTexture(gltexturetypeenums[image->texturetype], image->texnum);
558 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MAX_ANISOTROPY_EXT, old_aniso);CHECKGLERROR
560 qglBindTexture(gltexturetypeenums[image->texturetype], oldbindtexnum);
567 void R_MakeResizeBufferBigger(int size)
569 if (resizebuffersize < size)
571 resizebuffersize = size;
573 Mem_Free(resizebuffer);
574 if (colorconvertbuffer)
575 Mem_Free(colorconvertbuffer);
576 resizebuffer = (qbyte *)Mem_Alloc(texturemempool, resizebuffersize);
577 colorconvertbuffer = (qbyte *)Mem_Alloc(texturemempool, resizebuffersize);
578 if (!resizebuffer || !colorconvertbuffer)
579 Host_Error("R_Upload: out of memory\n");
583 static void GL_SetupTextureParameters(int flags, int texturetype)
585 int textureenum = gltexturetypeenums[texturetype];
586 int wrapmode = ((flags & TEXF_CLAMP) && gl_support_clamptoedge) ? GL_CLAMP_TO_EDGE : GL_REPEAT;
590 if (gl_support_anisotropy && (flags & TEXF_MIPMAP))
592 int aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
593 if (gl_texture_anisotropy.integer != aniso)
594 Cvar_SetValueQuick(&gl_texture_anisotropy, aniso);
595 qglTexParameteri(textureenum, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);CHECKGLERROR
597 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_S, wrapmode);CHECKGLERROR
598 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_T, wrapmode);CHECKGLERROR
599 if (gltexturetypedimensions[texturetype] >= 3)
601 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_R, wrapmode);CHECKGLERROR
605 if (flags & TEXF_FORCENEAREST)
607 if (flags & TEXF_MIPMAP)
609 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);CHECKGLERROR
613 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST);CHECKGLERROR
615 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_NEAREST);CHECKGLERROR
617 else if (flags & TEXF_FORCELINEAR)
619 if (flags & TEXF_MIPMAP)
621 if (gl_filter_min == GL_NEAREST_MIPMAP_LINEAR || gl_filter_min == GL_LINEAR_MIPMAP_LINEAR)
623 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);CHECKGLERROR
627 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);CHECKGLERROR
632 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR);CHECKGLERROR
634 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_LINEAR);CHECKGLERROR
638 if (flags & TEXF_MIPMAP)
640 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
644 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
646 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
652 static void R_Upload(gltexture_t *glt, qbyte *data)
654 int i, mip, width, height, depth;
661 glt->texnum = glt->image->texnum;
662 // we need to restore the texture binding after finishing the upload
663 qglGetIntegerv(gltexturetypebindingenums[glt->image->texturetype], &oldbindtexnum);
664 qglBindTexture(gltexturetypeenums[glt->image->texturetype], glt->image->texnum);
666 glt->flags &= ~GLTEXF_UPLOAD;
668 if (glt->flags & TEXF_FRAGMENT)
670 if (glt->image->flags & GLTEXF_UPLOAD)
672 glt->image->flags &= ~GLTEXF_UPLOAD;
673 Con_DPrint("uploaded new fragments image\n");
674 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
675 memset(resizebuffer, 255, glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
676 switch(glt->image->texturetype)
678 case GLTEXTURETYPE_1D:
679 qglTexImage1D(GL_TEXTURE_1D, 0, glt->image->glinternalformat, glt->image->width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
682 case GLTEXTURETYPE_2D:
683 qglTexImage2D(GL_TEXTURE_2D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
686 case GLTEXTURETYPE_3D:
687 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);
691 Host_Error("R_Upload: fragment texture of type other than 1D, 2D, or 3D\n");
694 GL_SetupTextureParameters(glt->image->flags, glt->image->texturetype);
697 if (prevbuffer == NULL)
699 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
700 memset(resizebuffer, 0, glt->width * glt->height * glt->image->depth * glt->image->bytesperpixel);
701 prevbuffer = resizebuffer;
703 else if (glt->textype->textype == TEXTYPE_PALETTE)
705 // promote paletted to RGBA, so we only have to worry about RGB and
706 // RGBA in the rest of this code
707 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->sides * glt->image->bytesperpixel);
708 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height * glt->depth, glt->palette);
709 prevbuffer = colorconvertbuffer;
712 switch(glt->image->texturetype)
714 case GLTEXTURETYPE_1D:
715 qglTexSubImage1D(GL_TEXTURE_1D, 0, glt->x, glt->width, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
718 case GLTEXTURETYPE_2D:
719 qglTexSubImage2D(GL_TEXTURE_2D, 0, glt->x, glt->y, glt->width, glt->height, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
722 case GLTEXTURETYPE_3D:
723 qglTexSubImage3D(GL_TEXTURE_3D, 0, glt->x, glt->y, glt->z, glt->width, glt->height, glt->depth, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
727 Host_Error("R_Upload: fragment texture of type other than 1D, 2D, or 3D\n");
733 glt->image->flags &= ~GLTEXF_UPLOAD;
735 // these are rounded up versions of the size to do better resampling
736 for (width = 1;width < glt->width ;width <<= 1);
737 for (height = 1;height < glt->height;height <<= 1);
738 for (depth = 1;depth < glt->depth ;depth <<= 1);
740 R_MakeResizeBufferBigger(width * height * depth * glt->image->sides * glt->image->bytesperpixel);
742 if (prevbuffer == NULL)
744 width = glt->image->width;
745 height = glt->image->height;
746 depth = glt->image->depth;
747 memset(resizebuffer, 0, width * height * depth * glt->image->bytesperpixel);
748 prevbuffer = resizebuffer;
752 if (glt->textype->textype == TEXTYPE_PALETTE)
754 // promote paletted to RGBA, so we only have to worry about RGB and
755 // RGBA in the rest of this code
756 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height * glt->depth * glt->image->sides, glt->palette);
757 prevbuffer = colorconvertbuffer;
761 // cubemaps contain multiple images and thus get processed a bit differently
762 if (glt->image->texturetype != GLTEXTURETYPE_CUBEMAP)
764 if (glt->width != width || glt->height != height || glt->depth != depth)
766 Image_Resample(prevbuffer, glt->width, glt->height, glt->depth, resizebuffer, width, height, depth, glt->image->bytesperpixel, r_lerpimages.integer);
767 prevbuffer = resizebuffer;
770 while (width > glt->image->width || height > glt->image->height || depth > glt->image->depth)
772 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->image->width, glt->image->height, glt->image->depth, glt->image->bytesperpixel);
773 prevbuffer = resizebuffer;
777 switch(glt->image->texturetype)
779 case GLTEXTURETYPE_1D:
780 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->image->glinternalformat, width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
782 if (glt->flags & TEXF_MIPMAP)
784 while (width > 1 || height > 1 || depth > 1)
786 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
787 prevbuffer = resizebuffer;
788 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->image->glinternalformat, width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
793 case GLTEXTURETYPE_2D:
794 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
796 if (glt->flags & TEXF_MIPMAP)
798 while (width > 1 || height > 1 || depth > 1)
800 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
801 prevbuffer = resizebuffer;
802 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
807 case GLTEXTURETYPE_3D:
808 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->image->glinternalformat, width, height, depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
810 if (glt->flags & TEXF_MIPMAP)
812 while (width > 1 || height > 1 || depth > 1)
814 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
815 prevbuffer = resizebuffer;
816 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->image->glinternalformat, width, height, depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
821 case GLTEXTURETYPE_CUBEMAP:
822 // convert and upload each side in turn,
823 // from a continuous block of input texels
824 texturebuffer = prevbuffer;
825 for (i = 0;i < 6;i++)
827 prevbuffer = texturebuffer;
828 texturebuffer += glt->width * glt->height * glt->depth * glt->textype->inputbytesperpixel;
829 if (glt->width != width || glt->height != height || glt->depth != depth)
831 Image_Resample(prevbuffer, glt->width, glt->height, glt->depth, resizebuffer, width, height, depth, glt->image->bytesperpixel, r_lerpimages.integer);
832 prevbuffer = resizebuffer;
835 while (width > glt->image->width || height > glt->image->height || depth > glt->image->depth)
837 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->image->width, glt->image->height, glt->image->depth, glt->image->bytesperpixel);
838 prevbuffer = resizebuffer;
841 qglTexImage2D(cubemapside[i], mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
843 if (glt->flags & TEXF_MIPMAP)
845 while (width > 1 || height > 1 || depth > 1)
847 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
848 prevbuffer = resizebuffer;
849 qglTexImage2D(cubemapside[i], mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
856 GL_SetupTextureParameters(glt->image->flags, glt->image->texturetype);
858 qglBindTexture(gltexturetypeenums[glt->image->texturetype], oldbindtexnum);
861 static void R_FindImageForTexture(gltexture_t *glt)
863 int i, j, best, best2, x, y, z, w, h, d, picmip;
864 textypeinfo_t *texinfo;
865 gltexturepool_t *pool;
866 gltextureimage_t *image, **imagechainpointer;
867 texinfo = glt->textype;
870 // remains -1 until uploaded
879 if (glt->flags & TEXF_FRAGMENT)
881 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain)
883 image = *imagechainpointer;
884 if (image->type != GLIMAGETYPE_FRAGMENTS)
886 if (image->texturetype != glt->texturetype)
888 if ((image->flags ^ glt->flags) & (TEXF_MIPMAP | TEXF_ALPHA | TEXF_CLAMP | TEXF_FORCENEAREST | TEXF_FORCELINEAR))
890 if (image->glformat != texinfo->glformat || image->glinternalformat != texinfo->glinternalformat)
892 if (glt->width > image->width || glt->height > image->height || glt->depth > image->depth)
895 // got a fragments texture, find a place in it if we can
896 for (best = image->width, i = 0;i < image->width - w;i++)
898 for (best2 = 0, j = 0;j < w;j++)
900 if (image->blockallocation[i+j] >= best)
902 if (best2 < image->blockallocation[i+j])
903 best2 = image->blockallocation[i+j];
907 // this is a valid spot
913 if (best + h > image->height)
916 for (i = 0;i < w;i++)
917 image->blockallocation[x + i] = best + h;
923 image->texturecount++;
927 image = (gltextureimage_t *)Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
930 Con_Printf ("R_FindImageForTexture: ran out of memory\n");
933 image->type = GLIMAGETYPE_FRAGMENTS;
934 // make sure the created image is big enough for the fragment
935 for (image->width = block_size;image->width < glt->width;image->width <<= 1);
937 if (gltexturetypedimensions[glt->texturetype] >= 2)
938 for (image->height = block_size;image->height < glt->height;image->height <<= 1);
940 if (gltexturetypedimensions[glt->texturetype] >= 3)
941 for (image->depth = block_size;image->depth < glt->depth;image->depth <<= 1);
942 image->blockallocation = (short int *)Mem_Alloc(texturemempool, image->width * sizeof(short));
943 memset(image->blockallocation, 0, image->width * sizeof(short));
948 for (i = 0;i < w;i++)
949 image->blockallocation[x + i] = y + h;
953 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain);
955 image = (gltextureimage_t *)Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
958 Con_Printf ("R_FindImageForTexture: ran out of memory\n");
961 image->type = GLIMAGETYPE_TILE;
962 image->blockallocation = NULL;
965 if (glt->flags & TEXF_PICMIP)
966 picmip = gl_picmip.integer;
967 // calculate final size
968 if (gl_max_size.integer > realmaxsize)
969 Cvar_SetValue("gl_max_size", realmaxsize);
970 for (image->width = 1;image->width < glt->width;image->width <<= 1);
971 for (image->height = 1;image->height < glt->height;image->height <<= 1);
972 for (image->depth = 1;image->depth < glt->depth;image->depth <<= 1);
973 for (image->width >>= picmip;image->width > gl_max_size.integer;image->width >>= 1);
974 for (image->height >>= picmip;image->height > gl_max_size.integer;image->height >>= 1);
975 for (image->depth >>= picmip;image->depth > gl_max_size.integer;image->depth >>= 1);
976 if (image->width < 1) image->width = 1;
977 if (image->height < 1) image->height = 1;
978 if (image->depth < 1) image->depth = 1;
980 image->texturetype = glt->texturetype;
981 image->glinternalformat = texinfo->glinternalformat;
982 image->glformat = texinfo->glformat;
983 image->flags = (glt->flags & (TEXF_MIPMAP | TEXF_ALPHA | TEXF_CLAMP | TEXF_PICMIP | TEXF_FORCENEAREST | TEXF_FORCELINEAR)) | GLTEXF_UPLOAD;
984 image->bytesperpixel = texinfo->internalbytesperpixel;
985 image->sides = image->texturetype == GLTEXTURETYPE_CUBEMAP ? 6 : 1;
986 // get a texture number to use
987 qglGenTextures(1, (GLuint *)&image->texnum);
988 *imagechainpointer = image;
989 image->texturecount++;
997 // note: R_FindImageForTexture must be called before this
998 static void R_UploadTexture (gltexture_t *glt)
1000 if (!(glt->flags & GLTEXF_UPLOAD))
1003 R_Upload(glt, glt->inputtexels);
1004 if (glt->inputtexels)
1006 Mem_Free(glt->inputtexels);
1007 glt->inputtexels = NULL;
1008 glt->flags |= GLTEXF_DESTROYED;
1010 else if (glt->flags & GLTEXF_DESTROYED)
1011 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
1014 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)
1018 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
1019 textypeinfo_t *texinfo;
1021 if (cls.state == ca_dedicated)
1024 if (flags & TEXF_FRAGMENT && texturetype != GLTEXTURETYPE_2D)
1026 Con_Printf ("R_LoadTexture: only 2D fragment textures implemented\n");
1029 if (texturetype == GLTEXTURETYPE_CUBEMAP && !gl_texturecubemap)
1031 Con_Printf ("R_LoadTexture: cubemap texture not supported by driver\n");
1034 if (texturetype == GLTEXTURETYPE_3D && !gl_texture3d)
1036 Con_Printf ("R_LoadTexture: 3d texture not supported by driver\n");
1040 texinfo = R_GetTexTypeInfo(textype, flags);
1041 size = width * height * depth * sides * texinfo->inputbytesperpixel;
1044 Con_Printf ("R_LoadTexture: bogus texture size (%dx%dx%dx%dbppx%dsides = %d bytes)\n", width, height, depth, texinfo->inputbytesperpixel * 8, sides);
1048 // clear the alpha flag if the texture has no transparent pixels
1051 case TEXTYPE_PALETTE:
1052 if (flags & TEXF_ALPHA)
1054 flags &= ~TEXF_ALPHA;
1057 for (i = 0;i < size;i++)
1059 if (((qbyte *)&palette[data[i]])[3] < 255)
1061 flags |= TEXF_ALPHA;
1069 if (flags & TEXF_ALPHA)
1070 Host_Error("R_LoadTexture: RGB has no alpha, don't specify TEXF_ALPHA\n");
1073 if (flags & TEXF_ALPHA)
1075 flags &= ~TEXF_ALPHA;
1078 for (i = 3;i < size;i += 4)
1082 flags |= TEXF_ALPHA;
1092 Host_Error("R_LoadTexture: unknown texture type\n");
1095 glt = (gltexture_t *)Mem_Alloc(texturemempool, sizeof(gltexture_t));
1097 strlcpy (glt->identifier, identifier, sizeof(glt->identifier));
1099 glt->chain = pool->gltchain;
1100 pool->gltchain = glt;
1102 glt->height = height;
1104 glt->flags = flags | GLTEXF_UPLOAD;
1105 glt->textype = texinfo;
1106 glt->texturetype = texturetype;
1107 glt->inputdatasize = size;
1108 glt->palette = palette;
1112 glt->inputtexels = (qbyte *)Mem_Alloc(texturemempool, size);
1113 if (glt->inputtexels == NULL)
1114 Con_Printf ("R_LoadTexture: out of memory\n");
1116 memcpy(glt->inputtexels, data, size);
1119 glt->inputtexels = NULL;
1121 R_FindImageForTexture(glt);
1122 R_PrecacheTexture(glt);
1124 return (rtexture_t *)glt;
1127 rtexture_t *R_LoadTexture1D(rtexturepool_t *rtexturepool, const char *identifier, int width, const qbyte *data, int textype, int flags, const unsigned int *palette)
1129 return R_SetupTexture(rtexturepool, identifier, width, 1, 1, 1, flags, textype, GLTEXTURETYPE_1D, data, palette);
1132 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)
1134 return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, flags, textype, GLTEXTURETYPE_2D, data, palette);
1137 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)
1139 return R_SetupTexture(rtexturepool, identifier, width, height, depth, 1, flags, textype, GLTEXTURETYPE_3D, data, palette);
1142 rtexture_t *R_LoadTextureCubeMap(rtexturepool_t *rtexturepool, const char *identifier, int width, const qbyte *data, int textype, int flags, const unsigned int *palette)
1144 return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, flags, textype, GLTEXTURETYPE_CUBEMAP, data, palette);
1147 int R_TextureHasAlpha(rtexture_t *rt)
1149 return rt ? (((gltexture_t *)rt)->flags & TEXF_ALPHA) != 0 : false;
1152 int R_TextureWidth(rtexture_t *rt)
1154 return rt ? ((gltexture_t *)rt)->width : 0;
1157 int R_TextureHeight(rtexture_t *rt)
1159 return rt ? ((gltexture_t *)rt)->height : 0;
1162 void R_FragmentLocation3D(rtexture_t *rt, int *x, int *y, int *z, float *fx1, float *fy1, float *fz1, float *fx2, float *fy2, float *fz2)
1165 float iwidth, iheight, idepth;
1166 if (cls.state == ca_dedicated)
1174 if (fx1 || fy1 || fx2 || fy2)
1192 Host_Error("R_FragmentLocation: no texture supplied\n");
1193 glt = (gltexture_t *)rt;
1194 if (glt->flags & TEXF_FRAGMENT)
1200 if (fx1 || fy1 || fx2 || fy2)
1202 iwidth = 1.0f / glt->image->width;
1203 iheight = 1.0f / glt->image->height;
1204 idepth = 1.0f / glt->image->depth;
1206 *fx1 = glt->x * iwidth;
1208 *fy1 = glt->y * iheight;
1210 *fz1 = glt->z * idepth;
1212 *fx2 = (glt->x + glt->width) * iwidth;
1214 *fy2 = (glt->y + glt->height) * iheight;
1216 *fz2 = (glt->z + glt->depth) * idepth;
1227 if (fx1 || fy1 || fx2 || fy2)
1245 void R_FragmentLocation(rtexture_t *rt, int *x, int *y, float *fx1, float *fy1, float *fx2, float *fy2)
1247 R_FragmentLocation3D(rt, x, y, NULL, fx1, fy1, NULL, fx2, fy2, NULL);
1250 int R_CompatibleFragmentWidth(int width, int textype, int flags)
1255 void R_UpdateTexture(rtexture_t *rt, qbyte *data)
1259 Host_Error("R_UpdateTexture: no texture supplied\n");
1261 Host_Error("R_UpdateTexture: no data supplied\n");
1262 glt = (gltexture_t *)rt;
1264 // if it has not been uploaded yet, update the data that will be used when it is
1265 if (glt->inputtexels)
1266 memcpy(glt->inputtexels, data, glt->inputdatasize);
1268 R_Upload(glt, data);