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;
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, &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 = 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 Sys_Error("R_FreeTexturePool: not all images freed\n");
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 char engineversion[40];
463 static void r_textures_start(void)
465 // deal with size limits of various drivers (3dfx in particular)
466 qglGetIntegerv(GL_MAX_TEXTURE_SIZE, &realmaxsize);
468 // LordHavoc: allow any alignment
469 qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);
472 // use the largest scrap texture size we can (not sure if this is really a good idea)
473 for (block_size = 1;block_size < realmaxsize && block_size < gl_max_scrapsize.integer;block_size <<= 1);
475 texturemempool = Mem_AllocPool("texture management", 0, NULL);
477 // Disable JPEG screenshots if the DLL isn't loaded
478 if (! JPEG_OpenLibrary ())
479 Cvar_SetValueQuick (&scr_screenshot_jpeg, 0);
482 static void r_textures_shutdown(void)
484 rtexturepool_t *temp;
486 JPEG_CloseLibrary ();
488 while(gltexturepoolchain)
490 temp = (rtexturepool_t *) gltexturepoolchain;
491 R_FreeTexturePool(&temp);
494 resizebuffersize = 0;
495 texturebuffersize = 0;
497 colorconvertbuffer = NULL;
498 texturebuffer = NULL;
499 Mem_FreePool(&texturemempool);
502 static void r_textures_newmap(void)
506 void R_Textures_Init (void)
508 Cmd_AddCommand("gl_texturemode", &GL_TextureMode_f);
509 Cmd_AddCommand("r_texturestats", R_TextureStats_f);
510 Cvar_RegisterVariable (&gl_max_scrapsize);
511 Cvar_RegisterVariable (&gl_max_size);
512 Cvar_RegisterVariable (&gl_picmip);
513 Cvar_RegisterVariable (&r_lerpimages);
514 Cvar_RegisterVariable (&r_precachetextures);
515 Cvar_RegisterVariable (&gl_texture_anisotropy);
517 R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
520 void R_Textures_Frame (void)
522 static int old_aniso = 0;
524 // could do procedural texture animation here, if we keep track of which
525 // textures were accessed this frame...
527 // free the resize buffers
528 resizebuffersize = 0;
531 Mem_Free(resizebuffer);
534 if (colorconvertbuffer)
536 Mem_Free(colorconvertbuffer);
537 colorconvertbuffer = NULL;
540 if (old_aniso != gl_texture_anisotropy.integer)
542 gltextureimage_t *image;
543 gltexturepool_t *pool;
546 old_aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
548 Cvar_SetValueQuick(&gl_texture_anisotropy, old_aniso);
550 for (pool = gltexturepoolchain;pool;pool = pool->next)
552 for (image = pool->imagechain;image;image = image->imagechain)
554 // only update already uploaded images
555 if (!(image->flags & GLTEXF_UPLOAD) && (image->flags & TEXF_MIPMAP))
557 qglGetIntegerv(gltexturetypebindingenums[image->texturetype], &oldbindtexnum);
559 qglBindTexture(gltexturetypeenums[image->texturetype], image->texnum);
560 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MAX_ANISOTROPY_EXT, old_aniso);CHECKGLERROR
562 qglBindTexture(gltexturetypeenums[image->texturetype], oldbindtexnum);
569 void R_MakeResizeBufferBigger(int size)
571 if (resizebuffersize < size)
573 resizebuffersize = size;
575 Mem_Free(resizebuffer);
576 if (colorconvertbuffer)
577 Mem_Free(colorconvertbuffer);
578 resizebuffer = Mem_Alloc(texturemempool, resizebuffersize);
579 colorconvertbuffer = Mem_Alloc(texturemempool, resizebuffersize);
580 if (!resizebuffer || !colorconvertbuffer)
581 Host_Error("R_Upload: out of memory\n");
585 static void GL_SetupTextureParameters(int flags, int texturetype)
587 int textureenum = gltexturetypeenums[texturetype];
588 int wrapmode = ((flags & TEXF_CLAMP) && gl_support_clamptoedge) ? GL_CLAMP_TO_EDGE : GL_REPEAT;
592 if (gl_support_anisotropy && (flags & TEXF_MIPMAP))
594 int aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
595 if (gl_texture_anisotropy.integer != aniso)
596 Cvar_SetValueQuick(&gl_texture_anisotropy, aniso);
597 qglTexParameteri(textureenum, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);CHECKGLERROR
599 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_S, wrapmode);CHECKGLERROR
600 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_T, wrapmode);CHECKGLERROR
601 if (gltexturetypedimensions[texturetype] >= 3)
603 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_R, wrapmode);CHECKGLERROR
607 if (flags & TEXF_FORCENEAREST)
609 if (flags & TEXF_MIPMAP)
611 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);CHECKGLERROR
615 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST);CHECKGLERROR
617 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_NEAREST);CHECKGLERROR
619 else if (flags & TEXF_FORCELINEAR)
621 if (flags & TEXF_MIPMAP)
623 if (gl_filter_min == GL_NEAREST_MIPMAP_LINEAR || gl_filter_min == GL_LINEAR_MIPMAP_LINEAR)
625 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);CHECKGLERROR
629 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);CHECKGLERROR
634 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR);CHECKGLERROR
636 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_LINEAR);CHECKGLERROR
640 if (flags & TEXF_MIPMAP)
642 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
646 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
648 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
654 static void R_Upload(gltexture_t *glt, qbyte *data)
656 int i, mip, width, height, depth;
663 glt->texnum = glt->image->texnum;
664 // we need to restore the texture binding after finishing the upload
665 qglGetIntegerv(gltexturetypebindingenums[glt->image->texturetype], &oldbindtexnum);
666 qglBindTexture(gltexturetypeenums[glt->image->texturetype], glt->image->texnum);
668 glt->flags &= ~GLTEXF_UPLOAD;
670 if (glt->flags & TEXF_FRAGMENT)
672 if (glt->image->flags & GLTEXF_UPLOAD)
674 glt->image->flags &= ~GLTEXF_UPLOAD;
675 Con_DPrint("uploaded new fragments image\n");
676 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
677 memset(resizebuffer, 255, glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
678 switch(glt->image->texturetype)
680 case GLTEXTURETYPE_1D:
681 qglTexImage1D(GL_TEXTURE_1D, 0, glt->image->glinternalformat, glt->image->width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
684 case GLTEXTURETYPE_2D:
685 qglTexImage2D(GL_TEXTURE_2D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
688 case GLTEXTURETYPE_3D:
689 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);
693 Host_Error("R_Upload: fragment texture of type other than 1D, 2D, or 3D\n");
696 GL_SetupTextureParameters(glt->image->flags, glt->image->texturetype);
699 if (prevbuffer == NULL)
701 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
702 memset(resizebuffer, 0, glt->width * glt->height * glt->image->depth * glt->image->bytesperpixel);
703 prevbuffer = resizebuffer;
705 else if (glt->textype->textype == TEXTYPE_PALETTE)
707 // promote paletted to RGBA, so we only have to worry about RGB and
708 // RGBA in the rest of this code
709 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->sides * glt->image->bytesperpixel);
710 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height * glt->depth, glt->palette);
711 prevbuffer = colorconvertbuffer;
714 switch(glt->image->texturetype)
716 case GLTEXTURETYPE_1D:
717 qglTexSubImage1D(GL_TEXTURE_1D, 0, glt->x, glt->width, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
720 case GLTEXTURETYPE_2D:
721 qglTexSubImage2D(GL_TEXTURE_2D, 0, glt->x, glt->y, glt->width, glt->height, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
724 case GLTEXTURETYPE_3D:
725 qglTexSubImage3D(GL_TEXTURE_3D, 0, glt->x, glt->y, glt->z, glt->width, glt->height, glt->depth, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
729 Host_Error("R_Upload: fragment texture of type other than 1D, 2D, or 3D\n");
735 glt->image->flags &= ~GLTEXF_UPLOAD;
737 // these are rounded up versions of the size to do better resampling
738 for (width = 1;width < glt->width ;width <<= 1);
739 for (height = 1;height < glt->height;height <<= 1);
740 for (depth = 1;depth < glt->depth ;depth <<= 1);
742 R_MakeResizeBufferBigger(width * height * depth * glt->image->sides * glt->image->bytesperpixel);
744 if (prevbuffer == NULL)
746 width = glt->image->width;
747 height = glt->image->height;
748 depth = glt->image->depth;
749 memset(resizebuffer, 0, width * height * depth * glt->image->bytesperpixel);
750 prevbuffer = resizebuffer;
754 if (glt->textype->textype == TEXTYPE_PALETTE)
756 // promote paletted to RGBA, so we only have to worry about RGB and
757 // RGBA in the rest of this code
758 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height * glt->depth * glt->image->sides, glt->palette);
759 prevbuffer = colorconvertbuffer;
763 // cubemaps contain multiple images and thus get processed a bit differently
764 if (glt->image->texturetype != GLTEXTURETYPE_CUBEMAP)
766 if (glt->width != width || glt->height != height || glt->depth != depth)
768 Image_Resample(prevbuffer, glt->width, glt->height, glt->depth, resizebuffer, width, height, depth, glt->image->bytesperpixel, r_lerpimages.integer);
769 prevbuffer = resizebuffer;
772 while (width > glt->image->width || height > glt->image->height || depth > glt->image->depth)
774 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->image->width, glt->image->height, glt->image->depth, glt->image->bytesperpixel);
775 prevbuffer = resizebuffer;
779 switch(glt->image->texturetype)
781 case GLTEXTURETYPE_1D:
782 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->image->glinternalformat, width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
784 if (glt->flags & TEXF_MIPMAP)
786 while (width > 1 || height > 1 || depth > 1)
788 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
789 prevbuffer = resizebuffer;
790 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->image->glinternalformat, width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
795 case GLTEXTURETYPE_2D:
796 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
798 if (glt->flags & TEXF_MIPMAP)
800 while (width > 1 || height > 1 || depth > 1)
802 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
803 prevbuffer = resizebuffer;
804 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
809 case GLTEXTURETYPE_3D:
810 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->image->glinternalformat, width, height, depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
812 if (glt->flags & TEXF_MIPMAP)
814 while (width > 1 || height > 1 || depth > 1)
816 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
817 prevbuffer = resizebuffer;
818 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->image->glinternalformat, width, height, depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
823 case GLTEXTURETYPE_CUBEMAP:
824 // convert and upload each side in turn,
825 // from a continuous block of input texels
826 texturebuffer = prevbuffer;
827 for (i = 0;i < 6;i++)
829 prevbuffer = texturebuffer;
830 texturebuffer += glt->width * glt->height * glt->depth * glt->textype->inputbytesperpixel;
831 if (glt->width != width || glt->height != height || glt->depth != depth)
833 Image_Resample(prevbuffer, glt->width, glt->height, glt->depth, resizebuffer, width, height, depth, glt->image->bytesperpixel, r_lerpimages.integer);
834 prevbuffer = resizebuffer;
837 while (width > glt->image->width || height > glt->image->height || depth > glt->image->depth)
839 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->image->width, glt->image->height, glt->image->depth, glt->image->bytesperpixel);
840 prevbuffer = resizebuffer;
843 qglTexImage2D(cubemapside[i], mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
845 if (glt->flags & TEXF_MIPMAP)
847 while (width > 1 || height > 1 || depth > 1)
849 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
850 prevbuffer = resizebuffer;
851 qglTexImage2D(cubemapside[i], mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
858 GL_SetupTextureParameters(glt->image->flags, glt->image->texturetype);
860 qglBindTexture(gltexturetypeenums[glt->image->texturetype], oldbindtexnum);
863 static void R_FindImageForTexture(gltexture_t *glt)
865 int i, j, best, best2, x, y, z, w, h, d, picmip;
866 textypeinfo_t *texinfo;
867 gltexturepool_t *pool;
868 gltextureimage_t *image, **imagechainpointer;
869 texinfo = glt->textype;
872 // remains -1 until uploaded
881 if (glt->flags & TEXF_FRAGMENT)
883 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain)
885 image = *imagechainpointer;
886 if (image->type != GLIMAGETYPE_FRAGMENTS)
888 if (image->texturetype != glt->texturetype)
890 if ((image->flags ^ glt->flags) & (TEXF_MIPMAP | TEXF_ALPHA | TEXF_CLAMP | TEXF_FORCENEAREST | TEXF_FORCELINEAR))
892 if (image->glformat != texinfo->glformat || image->glinternalformat != texinfo->glinternalformat)
894 if (glt->width > image->width || glt->height > image->height || glt->depth > image->depth)
897 // got a fragments texture, find a place in it if we can
898 for (best = image->width, i = 0;i < image->width - w;i++)
900 for (best2 = 0, j = 0;j < w;j++)
902 if (image->blockallocation[i+j] >= best)
904 if (best2 < image->blockallocation[i+j])
905 best2 = image->blockallocation[i+j];
909 // this is a valid spot
915 if (best + h > image->height)
918 for (i = 0;i < w;i++)
919 image->blockallocation[x + i] = best + h;
925 image->texturecount++;
929 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
931 Sys_Error("R_FindImageForTexture: ran out of memory\n");
932 image->type = GLIMAGETYPE_FRAGMENTS;
933 // make sure the created image is big enough for the fragment
934 for (image->width = block_size;image->width < glt->width;image->width <<= 1);
936 if (gltexturetypedimensions[glt->texturetype] >= 2)
937 for (image->height = block_size;image->height < glt->height;image->height <<= 1);
939 if (gltexturetypedimensions[glt->texturetype] >= 3)
940 for (image->depth = block_size;image->depth < glt->depth;image->depth <<= 1);
941 image->blockallocation = Mem_Alloc(texturemempool, image->width * sizeof(short));
942 memset(image->blockallocation, 0, image->width * sizeof(short));
947 for (i = 0;i < w;i++)
948 image->blockallocation[x + i] = y + h;
952 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain);
954 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
956 Sys_Error("R_FindImageForTexture: ran out of memory\n");
957 image->type = GLIMAGETYPE_TILE;
958 image->blockallocation = NULL;
961 if (glt->flags & TEXF_PICMIP)
962 picmip = gl_picmip.integer;
963 // calculate final size
964 if (gl_max_size.integer > realmaxsize)
965 Cvar_SetValue("gl_max_size", realmaxsize);
966 for (image->width = 1;image->width < glt->width;image->width <<= 1);
967 for (image->height = 1;image->height < glt->height;image->height <<= 1);
968 for (image->depth = 1;image->depth < glt->depth;image->depth <<= 1);
969 for (image->width >>= picmip;image->width > gl_max_size.integer;image->width >>= 1);
970 for (image->height >>= picmip;image->height > gl_max_size.integer;image->height >>= 1);
971 for (image->depth >>= picmip;image->depth > gl_max_size.integer;image->depth >>= 1);
972 if (image->width < 1) image->width = 1;
973 if (image->height < 1) image->height = 1;
974 if (image->depth < 1) image->depth = 1;
976 image->texturetype = glt->texturetype;
977 image->glinternalformat = texinfo->glinternalformat;
978 image->glformat = texinfo->glformat;
979 image->flags = (glt->flags & (TEXF_MIPMAP | TEXF_ALPHA | TEXF_CLAMP | TEXF_PICMIP | TEXF_FORCENEAREST | TEXF_FORCELINEAR)) | GLTEXF_UPLOAD;
980 image->bytesperpixel = texinfo->internalbytesperpixel;
981 image->sides = image->texturetype == GLTEXTURETYPE_CUBEMAP ? 6 : 1;
982 // get a texture number to use
983 qglGenTextures(1, &image->texnum);
984 *imagechainpointer = image;
985 image->texturecount++;
993 // note: R_FindImageForTexture must be called before this
994 static void R_UploadTexture (gltexture_t *glt)
996 if (!(glt->flags & GLTEXF_UPLOAD))
999 R_Upload(glt, glt->inputtexels);
1000 if (glt->inputtexels)
1002 Mem_Free(glt->inputtexels);
1003 glt->inputtexels = NULL;
1004 glt->flags |= GLTEXF_DESTROYED;
1006 else if (glt->flags & GLTEXF_DESTROYED)
1007 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
1010 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)
1014 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
1015 textypeinfo_t *texinfo;
1017 if (cls.state == ca_dedicated)
1020 if (flags & TEXF_FRAGMENT && texturetype != GLTEXTURETYPE_2D)
1021 Sys_Error("R_LoadTexture: only 2D fragment textures implemented\n");
1022 if (texturetype == GLTEXTURETYPE_CUBEMAP && !gl_texturecubemap)
1023 Sys_Error("R_LoadTexture: cubemap texture not supported by driver\n");
1024 if (texturetype == GLTEXTURETYPE_3D && !gl_texture3d)
1025 Sys_Error("R_LoadTexture: 3d texture not supported by driver\n");
1027 texinfo = R_GetTexTypeInfo(textype, flags);
1028 size = width * height * depth * sides * texinfo->inputbytesperpixel;
1030 Sys_Error("R_LoadTexture: bogus texture size (%dx%dx%dx%dbppx%dsides = %d bytes)\n", width, height, depth, texinfo->inputbytesperpixel * 8, sides);
1032 // clear the alpha flag if the texture has no transparent pixels
1035 case TEXTYPE_PALETTE:
1036 if (flags & TEXF_ALPHA)
1038 flags &= ~TEXF_ALPHA;
1041 for (i = 0;i < size;i++)
1043 if (((qbyte *)&palette[data[i]])[3] < 255)
1045 flags |= TEXF_ALPHA;
1053 if (flags & TEXF_ALPHA)
1054 Host_Error("R_LoadTexture: RGB has no alpha, don't specify TEXF_ALPHA\n");
1057 if (flags & TEXF_ALPHA)
1059 flags &= ~TEXF_ALPHA;
1062 for (i = 3;i < size;i += 4)
1066 flags |= TEXF_ALPHA;
1076 Host_Error("R_LoadTexture: unknown texture type\n");
1079 glt = Mem_Alloc(texturemempool, sizeof(gltexture_t));
1081 strlcpy (glt->identifier, identifier, sizeof(glt->identifier));
1083 glt->chain = pool->gltchain;
1084 pool->gltchain = glt;
1086 glt->height = height;
1088 glt->flags = flags | GLTEXF_UPLOAD;
1089 glt->textype = texinfo;
1090 glt->texturetype = texturetype;
1091 glt->inputdatasize = size;
1092 glt->palette = palette;
1096 glt->inputtexels = Mem_Alloc(texturemempool, size);
1097 if (glt->inputtexels == NULL)
1098 Sys_Error("R_LoadTexture: out of memory\n");
1099 memcpy(glt->inputtexels, data, size);
1102 glt->inputtexels = NULL;
1104 R_FindImageForTexture(glt);
1105 R_PrecacheTexture(glt);
1107 return (rtexture_t *)glt;
1110 rtexture_t *R_LoadTexture1D(rtexturepool_t *rtexturepool, const char *identifier, int width, const qbyte *data, int textype, int flags, const unsigned int *palette)
1112 return R_SetupTexture(rtexturepool, identifier, width, 1, 1, 1, flags, textype, GLTEXTURETYPE_1D, data, palette);
1115 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)
1117 return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, flags, textype, GLTEXTURETYPE_2D, data, palette);
1120 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)
1122 return R_SetupTexture(rtexturepool, identifier, width, height, depth, 1, flags, textype, GLTEXTURETYPE_3D, data, palette);
1125 rtexture_t *R_LoadTextureCubeMap(rtexturepool_t *rtexturepool, const char *identifier, int width, const qbyte *data, int textype, int flags, const unsigned int *palette)
1127 return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, flags, textype, GLTEXTURETYPE_CUBEMAP, data, palette);
1130 int R_TextureHasAlpha(rtexture_t *rt)
1132 return rt ? (((gltexture_t *)rt)->flags & TEXF_ALPHA) != 0 : false;
1135 int R_TextureWidth(rtexture_t *rt)
1137 return rt ? ((gltexture_t *)rt)->width : 0;
1140 int R_TextureHeight(rtexture_t *rt)
1142 return rt ? ((gltexture_t *)rt)->height : 0;
1145 void R_FragmentLocation3D(rtexture_t *rt, int *x, int *y, int *z, float *fx1, float *fy1, float *fz1, float *fx2, float *fy2, float *fz2)
1148 float iwidth, iheight, idepth;
1149 if (cls.state == ca_dedicated)
1157 if (fx1 || fy1 || fx2 || fy2)
1175 Host_Error("R_FragmentLocation: no texture supplied\n");
1176 glt = (gltexture_t *)rt;
1177 if (glt->flags & TEXF_FRAGMENT)
1183 if (fx1 || fy1 || fx2 || fy2)
1185 iwidth = 1.0f / glt->image->width;
1186 iheight = 1.0f / glt->image->height;
1187 idepth = 1.0f / glt->image->depth;
1189 *fx1 = glt->x * iwidth;
1191 *fy1 = glt->y * iheight;
1193 *fz1 = glt->z * idepth;
1195 *fx2 = (glt->x + glt->width) * iwidth;
1197 *fy2 = (glt->y + glt->height) * iheight;
1199 *fz2 = (glt->z + glt->depth) * idepth;
1210 if (fx1 || fy1 || fx2 || fy2)
1228 void R_FragmentLocation(rtexture_t *rt, int *x, int *y, float *fx1, float *fy1, float *fx2, float *fy2)
1230 R_FragmentLocation3D(rt, x, y, NULL, fx1, fy1, NULL, fx2, fy2, NULL);
1233 int R_CompatibleFragmentWidth(int width, int textype, int flags)
1238 void R_UpdateTexture(rtexture_t *rt, qbyte *data)
1242 Host_Error("R_UpdateTexture: no texture supplied\n");
1244 Host_Error("R_UpdateTexture: no data supplied\n");
1245 glt = (gltexture_t *)rt;
1247 // if it has not been uploaded yet, update the data that will be used when it is
1248 if (glt->inputtexels)
1249 memcpy(glt->inputtexels, data, glt->inputdatasize);
1251 R_Upload(glt, data);