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;
18 static mempool_t *texturedatamempool;
19 static mempool_t *textureprocessingmempool;
21 // note: this must not conflict with TEXF_ flags in r_textures.h
22 // cleared when a texture is uploaded
23 #define GLTEXF_UPLOAD 0x00010000
24 // bitmask for mismatch checking
25 #define GLTEXF_IMPORTANTBITS (0)
26 // set when image is uploaded and freed
27 #define GLTEXF_DESTROYED 0x00040000
29 // size of images which hold fragment textures, ignores picmip and max_size
30 static int block_size;
35 int inputbytesperpixel;
36 int internalbytesperpixel;
42 static textypeinfo_t textype_palette = {TEXTYPE_PALETTE, 1, 4, GL_RGBA , 3};
43 static textypeinfo_t textype_rgb = {TEXTYPE_RGB , 3, 3, GL_RGB , 3};
44 static textypeinfo_t textype_rgba = {TEXTYPE_RGBA , 4, 4, GL_RGBA , 3};
45 static textypeinfo_t textype_palette_alpha = {TEXTYPE_PALETTE, 1, 4, GL_RGBA , 4};
46 static textypeinfo_t textype_rgba_alpha = {TEXTYPE_RGBA , 4, 4, GL_RGBA , 4};
47 static textypeinfo_t textype_dsdt = {TEXTYPE_DSDT , 2, 2, GL_DSDT_NV, GL_DSDT8_NV};
49 // a tiling texture (most common type)
50 #define GLIMAGETYPE_TILE 0
51 // a fragments texture (contains one or more fragment textures)
52 #define GLIMAGETYPE_FRAGMENTS 1
54 #define GLTEXTURETYPE_1D 0
55 #define GLTEXTURETYPE_2D 1
56 #define GLTEXTURETYPE_3D 2
57 #define GLTEXTURETYPE_CUBEMAP 3
59 static int gltexturetypeenums[4] = {GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D, GL_TEXTURE_CUBE_MAP_ARB};
60 static int gltexturetypebindingenums[4] = {GL_TEXTURE_BINDING_1D, GL_TEXTURE_BINDING_2D, GL_TEXTURE_BINDING_3D, GL_TEXTURE_BINDING_CUBE_MAP_ARB};
61 static int gltexturetypedimensions[4] = {1, 2, 3, 2};
62 static int cubemapside[6] =
64 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
65 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
66 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
67 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
68 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
69 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
72 // a gltextureimage can have one (or more if fragments) gltextures inside
73 typedef struct gltextureimage_s
75 struct gltextureimage_s *imagechain;
77 int type; // one of the GLIMAGETYPE_ values
78 int texturetype; // one of the GLTEXTURETYPE_ values
79 int sides; // 1 or 6 depending on texturetype
80 int texnum; // GL texture slot number
81 int width, height, depth; // 3D texture support
82 int bytesperpixel; // bytes per pixel
83 int glformat; // GL_RGB or GL_RGBA
84 int glinternalformat; // 3 or 4
86 short *blockallocation; // fragment allocation (2D only)
90 typedef struct gltexture_s
92 // this field is exposed to the R_GetTexture macro, for speed reasons
93 // (must be identical in rtexture_t)
94 int texnum; // GL texture slot number
96 // pointer to texturepool (check this to see if the texture is allocated)
97 struct gltexturepool_s *pool;
98 // pointer to next texture in texturepool chain
99 struct gltexture_s *chain;
100 // pointer into gltextureimage array
101 gltextureimage_t *image;
102 // name of the texture (this might be removed someday), no duplicates
104 // location in the image, and size
105 int x, y, z, width, height, depth;
106 // copy of the original texture(s) supplied to the upload function, for
107 // delayed uploads (non-precached)
109 // original data size in *inputtexels
111 // flags supplied to the LoadTexture function
112 // (might be altered to remove TEXF_ALPHA), and GLTEXF_ private flags
114 // pointer to one of the textype_ structs
115 textypeinfo_t *textype;
116 // one of the GLTEXTURETYPE_ values
118 // palette if the texture is TEXTYPE_PALETTE
119 const unsigned int *palette;
123 #define TEXTUREPOOL_SENTINEL 0xC0DEDBAD
125 typedef struct gltexturepool_s
127 unsigned int sentinel;
128 struct gltextureimage_s *imagechain;
129 struct gltexture_s *gltchain;
130 struct gltexturepool_s *next;
134 static gltexturepool_t *gltexturepoolchain = NULL;
136 static qbyte *resizebuffer = NULL, *colorconvertbuffer;
137 static int resizebuffersize = 0;
138 static qbyte *texturebuffer;
139 static int texturebuffersize = 0;
141 static int realmaxsize = 0;
143 static textypeinfo_t *R_GetTexTypeInfo(int textype, int flags)
145 if (flags & TEXF_ALPHA)
149 case TEXTYPE_PALETTE:
150 return &textype_palette_alpha;
152 Host_Error("R_GetTexTypeInfo: RGB format has no alpha, TEXF_ALPHA not allowed\n");
155 return &textype_rgba_alpha;
157 Host_Error("R_GetTexTypeInfo: unknown texture format\n");
165 case TEXTYPE_PALETTE:
166 return &textype_palette;
170 return &textype_rgba;
172 return &textype_dsdt;
174 Host_Error("R_GetTexTypeInfo: unknown texture format\n");
180 static void R_UploadTexture(gltexture_t *t);
182 static void R_PrecacheTexture(gltexture_t *glt)
186 if (glt->flags & TEXF_ALWAYSPRECACHE)
188 else if (r_precachetextures.integer >= 2)
190 else if (r_precachetextures.integer >= 1)
191 if (glt->flags & TEXF_PRECACHE)
195 R_UploadTexture(glt);
198 int R_RealGetTexture(rtexture_t *rt)
203 glt = (gltexture_t *)rt;
204 if (glt->flags & GLTEXF_UPLOAD)
205 R_UploadTexture(glt);
206 glt->texnum = glt->image->texnum;
207 return glt->image->texnum;
213 void R_FreeTexture(rtexture_t *rt)
215 gltexture_t *glt, **gltpointer;
216 gltextureimage_t *image, **gltimagepointer;
218 glt = (gltexture_t *)rt;
220 Host_Error("R_FreeTexture: texture == NULL\n");
222 for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
223 if (*gltpointer == glt)
224 *gltpointer = glt->chain;
226 Host_Error("R_FreeTexture: texture \"%s\" not linked in pool\n", glt->identifier);
228 // note: if freeing a fragment texture, this will not make the claimed
229 // space available for new textures unless all other fragments in the
230 // image are also freed
234 image->texturecount--;
235 if (image->texturecount < 1)
237 for (gltimagepointer = &glt->pool->imagechain;*gltimagepointer && *gltimagepointer != image;gltimagepointer = &(*gltimagepointer)->imagechain);
238 if (*gltimagepointer == image)
239 *gltimagepointer = image->imagechain;
241 Host_Error("R_FreeTexture: image not linked in pool\n");
243 qglDeleteTextures(1, &image->texnum);
244 if (image->blockallocation)
245 Mem_Free(image->blockallocation);
251 Mem_Free(glt->identifier);
252 if (glt->inputtexels)
253 Mem_Free(glt->inputtexels);
258 static gltexture_t *R_FindTexture (gltexturepool_t *pool, char *identifier)
265 for (glt = pool->gltchain;glt;glt = glt->chain)
266 if (glt->identifier && !strcmp (identifier, glt->identifier))
273 rtexturepool_t *R_AllocTexturePool(void)
275 gltexturepool_t *pool;
276 if (texturemempool == NULL)
278 pool = Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
281 pool->next = gltexturepoolchain;
282 gltexturepoolchain = pool;
283 pool->sentinel = TEXTUREPOOL_SENTINEL;
284 return (rtexturepool_t *)pool;
287 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
289 gltexturepool_t *pool, **poolpointer;
290 if (rtexturepool == NULL)
292 if (*rtexturepool == NULL)
294 pool = (gltexturepool_t *)(*rtexturepool);
295 *rtexturepool = NULL;
296 if (pool->sentinel != TEXTUREPOOL_SENTINEL)
297 Host_Error("R_FreeTexturePool: pool already freed\n");
298 for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
299 if (*poolpointer == pool)
300 *poolpointer = pool->next;
302 Host_Error("R_FreeTexturePool: pool not linked\n");
303 while (pool->gltchain)
304 R_FreeTexture((rtexture_t *)pool->gltchain);
305 if (pool->imagechain)
306 Sys_Error("R_FreeTexturePool: not all images freed\n");
314 int minification, magnification;
318 static glmode_t modes[] =
320 {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
321 {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
322 {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
323 {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
324 {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
325 {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
328 static void GL_TextureMode_f (void)
332 gltextureimage_t *image;
333 gltexturepool_t *pool;
337 for (i = 0;i < 6;i++)
339 if (gl_filter_min == modes[i].minification)
341 Con_Printf("%s\n", modes[i].name);
345 Con_Print("current filter is unknown???\n");
349 for (i = 0;i < 6;i++)
350 if (!strcasecmp (modes[i].name, Cmd_Argv(1) ) )
354 Con_Print("bad filter name\n");
358 gl_filter_min = modes[i].minification;
359 gl_filter_mag = modes[i].magnification;
361 // change all the existing mipmap texture objects
362 // FIXME: force renderer(/client/something?) restart instead?
363 for (pool = gltexturepoolchain;pool;pool = pool->next)
365 for (image = pool->imagechain;image;image = image->imagechain)
367 // only update already uploaded images
368 if (!(image->flags & GLTEXF_UPLOAD) && !(image->flags & (TEXF_FORCENEAREST | TEXF_FORCELINEAR)))
370 qglGetIntegerv(gltexturetypebindingenums[image->texturetype], &oldbindtexnum);
371 qglBindTexture(gltexturetypeenums[image->texturetype], image->texnum);
372 if (image->flags & TEXF_MIPMAP)
373 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_min);
375 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_mag);
376 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MAG_FILTER, gl_filter_mag);
377 qglBindTexture(gltexturetypeenums[image->texturetype], oldbindtexnum);
383 static int R_CalcTexelDataSize (gltexture_t *glt)
385 int width2, height2, depth2, size, picmip;
386 if (glt->flags & TEXF_FRAGMENT)
387 size = glt->width * glt->height * glt->depth;
391 if (glt->flags & TEXF_PICMIP)
392 picmip = gl_picmip.integer;
393 if (gl_max_size.integer > realmaxsize)
394 Cvar_SetValue("gl_max_size", realmaxsize);
395 // calculate final size
396 for (width2 = 1;width2 < glt->width;width2 <<= 1);
397 for (height2 = 1;height2 < glt->height;height2 <<= 1);
398 for (depth2 = 1;depth2 < glt->depth;depth2 <<= 1);
399 for (width2 >>= picmip;width2 > gl_max_size.integer;width2 >>= 1);
400 for (height2 >>= picmip;height2 > gl_max_size.integer;height2 >>= 1);
401 for (depth2 >>= picmip;depth2 > gl_max_size.integer;depth2 >>= 1);
402 if (width2 < 1) width2 = 1;
403 if (height2 < 1) height2 = 1;
404 if (depth2 < 1) depth2 = 1;
407 if (glt->flags & TEXF_MIPMAP)
409 while (width2 > 1 || height2 > 1 || depth2 > 1)
411 size += width2 * height2 * depth2;
419 size++; // count the last 1x1 mipmap
422 size = width2 * height2 * depth2;
424 size *= glt->textype->internalbytesperpixel * glt->image->sides;
429 void R_TextureStats_PrintTotal(void)
431 int glsize, total = 0, totalt = 0, totalp = 0, loaded = 0, loadedt = 0, loadedp = 0;
433 gltexturepool_t *pool;
434 for (pool = gltexturepoolchain;pool;pool = pool->next)
436 for (glt = pool->gltchain;glt;glt = glt->chain)
438 glsize = R_CalcTexelDataSize(glt);
441 totalp += glt->inputdatasize;
442 if (!(glt->flags & GLTEXF_UPLOAD))
446 loadedp += glt->inputdatasize;
450 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);
453 static void R_TextureStats_f(void)
457 gltexturepool_t *pool;
458 Con_Print("glsize input loaded mip alpha name\n");
459 for (pool = gltexturepoolchain;pool;pool = pool->next)
461 for (glt = pool->gltchain;glt;glt = glt->chain)
463 loaded = !(glt->flags & GLTEXF_UPLOAD);
464 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>");
466 Con_Printf("pool %10p\n", pool);
468 R_TextureStats_PrintTotal();
471 char engineversion[40];
473 static void r_textures_start(void)
475 // deal with size limits of various drivers (3dfx in particular)
476 qglGetIntegerv(GL_MAX_TEXTURE_SIZE, &realmaxsize);
478 // LordHavoc: allow any alignment
479 qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);
482 // use the largest scrap texture size we can (not sure if this is really a good idea)
483 for (block_size = 1;block_size < realmaxsize && block_size < gl_max_scrapsize.integer;block_size <<= 1);
485 texturemempool = Mem_AllocPool("Texture Info", 0, NULL);
486 texturedatamempool = Mem_AllocPool("Texture Storage (not yet uploaded)", 0, NULL);
487 textureprocessingmempool = Mem_AllocPool("Texture Processing Buffers", 0, NULL);
489 // Disable JPEG screenshots if the DLL isn't loaded
490 if (! JPEG_OpenLibrary ())
491 Cvar_SetValueQuick (&scr_screenshot_jpeg, 0);
494 static void r_textures_shutdown(void)
496 rtexturepool_t *temp;
498 JPEG_CloseLibrary ();
500 while(gltexturepoolchain)
502 temp = (rtexturepool_t *) gltexturepoolchain;
503 R_FreeTexturePool(&temp);
506 resizebuffersize = 0;
507 texturebuffersize = 0;
509 colorconvertbuffer = NULL;
510 texturebuffer = NULL;
511 Mem_FreePool(&texturemempool);
512 Mem_FreePool(&texturedatamempool);
513 Mem_FreePool(&textureprocessingmempool);
516 static void r_textures_newmap(void)
520 void R_Textures_Init (void)
522 Cmd_AddCommand("gl_texturemode", &GL_TextureMode_f);
523 Cmd_AddCommand("r_texturestats", R_TextureStats_f);
524 Cvar_RegisterVariable (&gl_max_scrapsize);
525 Cvar_RegisterVariable (&gl_max_size);
526 Cvar_RegisterVariable (&gl_picmip);
527 Cvar_RegisterVariable (&r_lerpimages);
528 Cvar_RegisterVariable (&r_precachetextures);
529 Cvar_RegisterVariable (&gl_texture_anisotropy);
531 R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
534 void R_Textures_Frame (void)
536 static int old_aniso = 0;
538 // could do procedural texture animation here, if we keep track of which
539 // textures were accessed this frame...
541 // free the resize buffers
542 resizebuffersize = 0;
545 Mem_Free(resizebuffer);
548 if (colorconvertbuffer)
550 Mem_Free(colorconvertbuffer);
551 colorconvertbuffer = NULL;
554 if (old_aniso != gl_texture_anisotropy.integer)
556 gltextureimage_t *image;
557 gltexturepool_t *pool;
560 old_aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
562 Cvar_SetValueQuick(&gl_texture_anisotropy, old_aniso);
564 for (pool = gltexturepoolchain;pool;pool = pool->next)
566 for (image = pool->imagechain;image;image = image->imagechain)
568 // only update already uploaded images
569 if (!(image->flags & GLTEXF_UPLOAD))
571 qglGetIntegerv(gltexturetypebindingenums[image->texturetype], &oldbindtexnum);
573 qglBindTexture(gltexturetypeenums[image->texturetype], image->texnum);
574 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MAX_ANISOTROPY_EXT, old_aniso);CHECKGLERROR
576 qglBindTexture(gltexturetypeenums[image->texturetype], oldbindtexnum);
583 void R_MakeResizeBufferBigger(int size)
585 if (resizebuffersize < size)
587 resizebuffersize = size;
589 Mem_Free(resizebuffer);
590 if (colorconvertbuffer)
591 Mem_Free(colorconvertbuffer);
592 resizebuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
593 colorconvertbuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
594 if (!resizebuffer || !colorconvertbuffer)
595 Host_Error("R_Upload: out of memory\n");
599 static void GL_SetupTextureParameters(int flags, int texturetype)
601 int textureenum = gltexturetypeenums[texturetype];
602 int wrapmode = ((flags & TEXF_CLAMP) && gl_support_clamptoedge) ? GL_CLAMP_TO_EDGE : GL_REPEAT;
606 if (gl_support_anisotropy)
608 int aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
609 if (gl_texture_anisotropy.integer != aniso)
610 Cvar_SetValueQuick(&gl_texture_anisotropy, aniso);
611 qglTexParameteri(textureenum, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);CHECKGLERROR
613 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_S, wrapmode);CHECKGLERROR
614 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_T, wrapmode);CHECKGLERROR
615 if (gltexturetypedimensions[texturetype] >= 3)
617 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_R, wrapmode);CHECKGLERROR
621 if (flags & TEXF_FORCENEAREST)
623 if (flags & TEXF_MIPMAP)
625 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);CHECKGLERROR
629 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST);CHECKGLERROR
631 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_NEAREST);CHECKGLERROR
633 else if (flags & TEXF_FORCELINEAR)
635 if (flags & TEXF_MIPMAP)
637 if (gl_filter_min == GL_NEAREST_MIPMAP_LINEAR || gl_filter_min == GL_LINEAR_MIPMAP_LINEAR)
639 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);CHECKGLERROR
643 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);CHECKGLERROR
648 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR);CHECKGLERROR
650 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_LINEAR);CHECKGLERROR
654 if (flags & TEXF_MIPMAP)
656 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
660 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
662 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
668 static void R_Upload(gltexture_t *glt, qbyte *data)
670 int i, mip, width, height, depth;
677 glt->texnum = glt->image->texnum;
678 // we need to restore the texture binding after finishing the upload
679 qglGetIntegerv(gltexturetypebindingenums[glt->image->texturetype], &oldbindtexnum);
680 qglBindTexture(gltexturetypeenums[glt->image->texturetype], glt->image->texnum);
682 glt->flags &= ~GLTEXF_UPLOAD;
684 if (glt->flags & TEXF_FRAGMENT)
686 if (glt->image->flags & GLTEXF_UPLOAD)
688 glt->image->flags &= ~GLTEXF_UPLOAD;
689 Con_DPrint("uploaded new fragments image\n");
690 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
691 memset(resizebuffer, 255, glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
692 switch(glt->image->texturetype)
694 case GLTEXTURETYPE_1D:
695 qglTexImage1D(GL_TEXTURE_1D, 0, glt->image->glinternalformat, glt->image->width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
698 case GLTEXTURETYPE_2D:
699 qglTexImage2D(GL_TEXTURE_2D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
702 case GLTEXTURETYPE_3D:
703 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);
707 Host_Error("R_Upload: fragment texture of type other than 1D, 2D, or 3D\n");
710 GL_SetupTextureParameters(glt->image->flags, glt->image->texturetype);
713 if (prevbuffer == NULL)
715 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
716 memset(resizebuffer, 255, glt->width * glt->height * glt->image->depth * glt->image->bytesperpixel);
717 prevbuffer = resizebuffer;
719 else if (glt->textype->textype == TEXTYPE_PALETTE)
721 // promote paletted to RGBA, so we only have to worry about RGB and
722 // RGBA in the rest of this code
723 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->sides * glt->image->bytesperpixel);
724 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height * glt->depth, glt->palette);
725 prevbuffer = colorconvertbuffer;
728 switch(glt->image->texturetype)
730 case GLTEXTURETYPE_1D:
731 qglTexSubImage1D(GL_TEXTURE_1D, 0, glt->x, glt->width, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
734 case GLTEXTURETYPE_2D:
735 qglTexSubImage2D(GL_TEXTURE_2D, 0, glt->x, glt->y, glt->width, glt->height, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
738 case GLTEXTURETYPE_3D:
739 qglTexSubImage3D(GL_TEXTURE_3D, 0, glt->x, glt->y, glt->z, glt->width, glt->height, glt->depth, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
743 Host_Error("R_Upload: fragment texture of type other than 1D, 2D, or 3D\n");
749 glt->image->flags &= ~GLTEXF_UPLOAD;
751 // these are rounded up versions of the size to do better resampling
752 for (width = 1;width < glt->width ;width <<= 1);
753 for (height = 1;height < glt->height;height <<= 1);
754 for (depth = 1;depth < glt->depth ;depth <<= 1);
756 R_MakeResizeBufferBigger(width * height * depth * glt->image->sides * glt->image->bytesperpixel);
758 if (prevbuffer == NULL)
760 width = glt->image->width;
761 height = glt->image->height;
762 depth = glt->image->depth;
763 memset(resizebuffer, 255, width * height * depth * glt->image->bytesperpixel);
764 prevbuffer = resizebuffer;
768 if (glt->textype->textype == TEXTYPE_PALETTE)
770 // promote paletted to RGBA, so we only have to worry about RGB and
771 // RGBA in the rest of this code
772 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height * glt->depth * glt->image->sides, glt->palette);
773 prevbuffer = colorconvertbuffer;
777 // cubemaps contain multiple images and thus get processed a bit differently
778 if (glt->image->texturetype != GLTEXTURETYPE_CUBEMAP)
780 if (glt->width != width || glt->height != height || glt->depth != depth)
782 Image_Resample(prevbuffer, glt->width, glt->height, glt->depth, resizebuffer, width, height, depth, glt->image->bytesperpixel, r_lerpimages.integer);
783 prevbuffer = resizebuffer;
786 while (width > glt->image->width || height > glt->image->height || depth > glt->image->depth)
788 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->image->width, glt->image->height, glt->image->depth, glt->image->bytesperpixel);
789 prevbuffer = resizebuffer;
793 switch(glt->image->texturetype)
795 case GLTEXTURETYPE_1D:
796 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->image->glinternalformat, width, 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 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->image->glinternalformat, width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
809 case GLTEXTURETYPE_2D:
810 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->image->glinternalformat, width, height, 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 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
823 case GLTEXTURETYPE_3D:
824 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->image->glinternalformat, width, height, depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
826 if (glt->flags & TEXF_MIPMAP)
828 while (width > 1 || height > 1 || depth > 1)
830 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
831 prevbuffer = resizebuffer;
832 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->image->glinternalformat, width, height, depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
837 case GLTEXTURETYPE_CUBEMAP:
838 // convert and upload each side in turn,
839 // from a continuous block of input texels
840 texturebuffer = prevbuffer;
841 for (i = 0;i < 6;i++)
843 prevbuffer = texturebuffer;
844 texturebuffer += glt->width * glt->height * glt->depth * glt->textype->inputbytesperpixel;
845 if (glt->width != width || glt->height != height || glt->depth != depth)
847 Image_Resample(prevbuffer, glt->width, glt->height, glt->depth, resizebuffer, width, height, depth, glt->image->bytesperpixel, r_lerpimages.integer);
848 prevbuffer = resizebuffer;
851 while (width > glt->image->width || height > glt->image->height || depth > glt->image->depth)
853 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->image->width, glt->image->height, glt->image->depth, glt->image->bytesperpixel);
854 prevbuffer = resizebuffer;
857 qglTexImage2D(cubemapside[i], mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
859 if (glt->flags & TEXF_MIPMAP)
861 while (width > 1 || height > 1 || depth > 1)
863 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
864 prevbuffer = resizebuffer;
865 qglTexImage2D(cubemapside[i], mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
872 GL_SetupTextureParameters(glt->image->flags, glt->image->texturetype);
874 qglBindTexture(gltexturetypeenums[glt->image->texturetype], oldbindtexnum);
877 static void R_FindImageForTexture(gltexture_t *glt)
879 int i, j, best, best2, x, y, z, w, h, d, picmip;
880 textypeinfo_t *texinfo;
881 gltexturepool_t *pool;
882 gltextureimage_t *image, **imagechainpointer;
883 texinfo = glt->textype;
886 // remains -1 until uploaded
895 if (glt->flags & TEXF_FRAGMENT)
897 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain)
899 image = *imagechainpointer;
900 if (image->type != GLIMAGETYPE_FRAGMENTS)
902 if (image->texturetype != glt->texturetype)
904 if ((image->flags ^ glt->flags) & (TEXF_MIPMAP | TEXF_ALPHA | TEXF_CLAMP))
906 if (image->glformat != texinfo->glformat || image->glinternalformat != texinfo->glinternalformat)
908 if (glt->width > image->width || glt->height > image->height || glt->depth > image->depth)
911 // got a fragments texture, find a place in it if we can
912 for (best = image->width, i = 0;i < image->width - w;i++)
914 for (best2 = 0, j = 0;j < w;j++)
916 if (image->blockallocation[i+j] >= best)
918 if (best2 < image->blockallocation[i+j])
919 best2 = image->blockallocation[i+j];
923 // this is a valid spot
929 if (best + h > image->height)
932 for (i = 0;i < w;i++)
933 image->blockallocation[x + i] = best + h;
939 image->texturecount++;
943 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
945 Sys_Error("R_FindImageForTexture: ran out of memory\n");
946 image->type = GLIMAGETYPE_FRAGMENTS;
947 // make sure the created image is big enough for the fragment
948 for (image->width = block_size;image->width < glt->width;image->width <<= 1);
950 if (gltexturetypedimensions[glt->texturetype] >= 2)
951 for (image->height = block_size;image->height < glt->height;image->height <<= 1);
953 if (gltexturetypedimensions[glt->texturetype] >= 3)
954 for (image->depth = block_size;image->depth < glt->depth;image->depth <<= 1);
955 image->blockallocation = Mem_Alloc(texturemempool, image->width * sizeof(short));
956 memset(image->blockallocation, 0, image->width * sizeof(short));
961 for (i = 0;i < w;i++)
962 image->blockallocation[x + i] = y + h;
966 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain);
968 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
970 Sys_Error("R_FindImageForTexture: ran out of memory\n");
971 image->type = GLIMAGETYPE_TILE;
972 image->blockallocation = NULL;
975 if (glt->flags & TEXF_PICMIP)
976 picmip = gl_picmip.integer;
977 // calculate final size
978 if (gl_max_size.integer > realmaxsize)
979 Cvar_SetValue("gl_max_size", realmaxsize);
980 for (image->width = 1;image->width < glt->width;image->width <<= 1);
981 for (image->height = 1;image->height < glt->height;image->height <<= 1);
982 for (image->depth = 1;image->depth < glt->depth;image->depth <<= 1);
983 for (image->width >>= picmip;image->width > gl_max_size.integer;image->width >>= 1);
984 for (image->height >>= picmip;image->height > gl_max_size.integer;image->height >>= 1);
985 for (image->depth >>= picmip;image->depth > gl_max_size.integer;image->depth >>= 1);
986 if (image->width < 1) image->width = 1;
987 if (image->height < 1) image->height = 1;
988 if (image->depth < 1) image->depth = 1;
990 image->texturetype = glt->texturetype;
991 image->glinternalformat = texinfo->glinternalformat;
992 image->glformat = texinfo->glformat;
993 image->flags = (glt->flags & (TEXF_MIPMAP | TEXF_ALPHA | TEXF_CLAMP | TEXF_PICMIP)) | GLTEXF_UPLOAD;
994 image->bytesperpixel = texinfo->internalbytesperpixel;
995 image->sides = image->texturetype == GLTEXTURETYPE_CUBEMAP ? 6 : 1;
996 // get a texture number to use
997 qglGenTextures(1, &image->texnum);
998 *imagechainpointer = image;
999 image->texturecount++;
1007 // note: R_FindImageForTexture must be called before this
1008 static void R_UploadTexture (gltexture_t *glt)
1010 if (!(glt->flags & GLTEXF_UPLOAD))
1013 R_Upload(glt, glt->inputtexels);
1014 if (glt->inputtexels)
1016 Mem_Free(glt->inputtexels);
1017 glt->inputtexels = NULL;
1018 glt->flags |= GLTEXF_DESTROYED;
1020 else if (glt->flags & GLTEXF_DESTROYED)
1021 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
1024 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)
1028 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
1029 textypeinfo_t *texinfo;
1031 if (cls.state == ca_dedicated)
1034 if (flags & TEXF_FRAGMENT && texturetype != GLTEXTURETYPE_2D)
1035 Sys_Error("R_LoadTexture: only 2D fragment textures implemented\n");
1036 if (texturetype == GLTEXTURETYPE_CUBEMAP && !gl_texturecubemap)
1037 Sys_Error("R_LoadTexture: cubemap texture not supported by driver\n");
1038 if (texturetype == GLTEXTURETYPE_3D && !gl_texture3d)
1039 Sys_Error("R_LoadTexture: 3d texture not supported by driver\n");
1042 glt = R_FindTexture (pool, identifier);
1045 Con_Printf("R_LoadTexture: replacing existing texture %s\n", identifier);
1046 R_FreeTexture((rtexture_t *)glt);
1050 texinfo = R_GetTexTypeInfo(textype, flags);
1051 size = width * height * depth * sides * texinfo->inputbytesperpixel;
1053 Sys_Error("R_LoadTexture: bogus texture size (%dx%dx%dx%dbppx%dsides = %d bytes)\n", width, height, depth, texinfo->inputbytesperpixel * 8, sides);
1055 // clear the alpha flag if the texture has no transparent pixels
1058 case TEXTYPE_PALETTE:
1059 if (flags & TEXF_ALPHA)
1061 flags &= ~TEXF_ALPHA;
1064 for (i = 0;i < size;i++)
1066 if (((qbyte *)&palette[data[i]])[3] < 255)
1068 flags |= TEXF_ALPHA;
1076 if (flags & TEXF_ALPHA)
1077 Host_Error("R_LoadTexture: RGB has no alpha, don't specify TEXF_ALPHA\n");
1080 if (flags & TEXF_ALPHA)
1082 flags &= ~TEXF_ALPHA;
1085 for (i = 3;i < size;i += 4)
1089 flags |= TEXF_ALPHA;
1099 Host_Error("R_LoadTexture: unknown texture type\n");
1102 glt = Mem_Alloc(texturemempool, sizeof(gltexture_t));
1105 glt->identifier = Mem_Alloc(texturemempool, strlen(identifier)+1);
1106 strcpy (glt->identifier, identifier);
1109 glt->identifier = NULL;
1111 glt->chain = pool->gltchain;
1112 pool->gltchain = glt;
1114 glt->height = height;
1116 glt->flags = flags | GLTEXF_UPLOAD;
1117 glt->textype = texinfo;
1118 glt->texturetype = texturetype;
1119 glt->inputdatasize = size;
1120 glt->palette = palette;
1124 glt->inputtexels = Mem_Alloc(texturedatamempool, size);
1125 if (glt->inputtexels == NULL)
1126 Sys_Error("R_LoadTexture: out of memory\n");
1127 memcpy(glt->inputtexels, data, size);
1130 glt->inputtexels = NULL;
1132 R_FindImageForTexture(glt);
1133 R_PrecacheTexture(glt);
1135 return (rtexture_t *)glt;
1138 rtexture_t *R_LoadTexture1D(rtexturepool_t *rtexturepool, const char *identifier, int width, const qbyte *data, int textype, int flags, const unsigned int *palette)
1140 return R_SetupTexture(rtexturepool, identifier, width, 1, 1, 1, flags, textype, GLTEXTURETYPE_1D, data, palette);
1143 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)
1145 return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, flags, textype, GLTEXTURETYPE_2D, data, palette);
1148 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)
1150 return R_SetupTexture(rtexturepool, identifier, width, height, depth, 1, flags, textype, GLTEXTURETYPE_3D, data, palette);
1153 rtexture_t *R_LoadTextureCubeMap(rtexturepool_t *rtexturepool, const char *identifier, int width, const qbyte *data, int textype, int flags, const unsigned int *palette)
1155 return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, flags, textype, GLTEXTURETYPE_CUBEMAP, data, palette);
1158 int R_TextureHasAlpha(rtexture_t *rt)
1160 return rt ? (((gltexture_t *)rt)->flags & TEXF_ALPHA) != 0 : false;
1163 int R_TextureWidth(rtexture_t *rt)
1165 return rt ? ((gltexture_t *)rt)->width : 0;
1168 int R_TextureHeight(rtexture_t *rt)
1170 return rt ? ((gltexture_t *)rt)->height : 0;
1173 void R_FragmentLocation3D(rtexture_t *rt, int *x, int *y, int *z, float *fx1, float *fy1, float *fz1, float *fx2, float *fy2, float *fz2)
1176 float iwidth, iheight, idepth;
1177 if (cls.state == ca_dedicated)
1185 if (fx1 || fy1 || fx2 || fy2)
1203 Host_Error("R_FragmentLocation: no texture supplied\n");
1204 glt = (gltexture_t *)rt;
1205 if (glt->flags & TEXF_FRAGMENT)
1211 if (fx1 || fy1 || fx2 || fy2)
1213 iwidth = 1.0f / glt->image->width;
1214 iheight = 1.0f / glt->image->height;
1215 idepth = 1.0f / glt->image->depth;
1217 *fx1 = glt->x * iwidth;
1219 *fy1 = glt->y * iheight;
1221 *fz1 = glt->z * idepth;
1223 *fx2 = (glt->x + glt->width) * iwidth;
1225 *fy2 = (glt->y + glt->height) * iheight;
1227 *fz2 = (glt->z + glt->depth) * idepth;
1238 if (fx1 || fy1 || fx2 || fy2)
1256 void R_FragmentLocation(rtexture_t *rt, int *x, int *y, float *fx1, float *fy1, float *fx2, float *fy2)
1258 R_FragmentLocation3D(rt, x, y, NULL, fx1, fy1, NULL, fx2, fy2, NULL);
1261 int R_CompatibleFragmentWidth(int width, int textype, int flags)
1266 void R_UpdateTexture(rtexture_t *rt, qbyte *data)
1270 Host_Error("R_UpdateTexture: no texture supplied\n");
1272 Host_Error("R_UpdateTexture: no data supplied\n");
1273 glt = (gltexture_t *)rt;
1275 // if it has not been uploaded yet, update the data that will be used when it is
1276 if (glt->inputtexels)
1277 memcpy(glt->inputtexels, data, glt->inputdatasize);
1279 R_Upload(glt, data);