6 cvar_t r_max_size = {CVAR_SAVE, "r_max_size", "2048"};
7 cvar_t r_max_scrapsize = {CVAR_SAVE, "r_max_scrapsize", "256"};
8 cvar_t r_picmip = {CVAR_SAVE, "r_picmip", "0"};
9 cvar_t r_lerpimages = {CVAR_SAVE, "r_lerpimages", "1"};
10 cvar_t r_precachetextures = {CVAR_SAVE, "r_precachetextures", "1"};
11 cvar_t gl_texture_anisotropy = {CVAR_SAVE, "gl_texture_anisotropy", "0"};
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};
48 // a tiling texture (most common type)
49 #define GLIMAGETYPE_TILE 0
50 // a fragments texture (contains one or more fragment textures)
51 #define GLIMAGETYPE_FRAGMENTS 1
53 #define GLTEXTURETYPE_1D 0
54 #define GLTEXTURETYPE_2D 1
55 #define GLTEXTURETYPE_3D 2
56 #define GLTEXTURETYPE_CUBEMAP 3
58 static int gltexturetypeenums[4] = {GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D, GL_TEXTURE_CUBE_MAP_ARB};
59 static int gltexturetypebindingenums[4] = {GL_TEXTURE_BINDING_1D, GL_TEXTURE_BINDING_2D, GL_TEXTURE_BINDING_3D, GL_TEXTURE_BINDING_CUBE_MAP_ARB};
60 static int gltexturetypedimensions[4] = {1, 2, 3, 2};
61 static int cubemapside[6] =
63 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
64 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
65 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
66 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
67 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
68 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
71 // a gltextureimage can have one (or more if fragments) gltextures inside
72 typedef struct gltextureimage_s
74 struct gltextureimage_s *imagechain;
76 int type; // one of the GLIMAGETYPE_ values
77 int texturetype; // one of the GLTEXTURETYPE_ values
78 int sides; // 1 or 6 depending on texturetype
79 int texnum; // GL texture slot number
80 int width, height, depth; // 3D texture support
81 int bytesperpixel; // bytes per pixel
82 int glformat; // GL_RGB or GL_RGBA
83 int glinternalformat; // 3 or 4
85 short *blockallocation; // fragment allocation (2D only)
89 typedef struct gltexture_s
91 // this field is exposed to the R_GetTexture macro, for speed reasons
92 // (must be identical in rtexture_t)
93 int texnum; // GL texture slot number
95 // pointer to texturepool (check this to see if the texture is allocated)
96 struct gltexturepool_s *pool;
97 // pointer to next texture in texturepool chain
98 struct gltexture_s *chain;
99 // pointer into gltextureimage array
100 gltextureimage_t *image;
101 // name of the texture (this might be removed someday), no duplicates
103 // location in the image, and size
104 int x, y, z, width, height, depth;
105 // copy of the original texture(s) supplied to the upload function, for
106 // delayed uploads (non-precached)
108 // original data size in *inputtexels
110 // flags supplied to the LoadTexture function
111 // (might be altered to remove TEXF_ALPHA), and GLTEXF_ private flags
113 // pointer to one of the textype_ structs
114 textypeinfo_t *textype;
115 // one of the GLTEXTURETYPE_ values
117 // palette if the texture is TEXTYPE_PALETTE
118 const unsigned int *palette;
122 #define TEXTUREPOOL_SENTINEL 0xC0DEDBAD
124 typedef struct gltexturepool_s
126 unsigned int sentinel;
127 struct gltextureimage_s *imagechain;
128 struct gltexture_s *gltchain;
129 struct gltexturepool_s *next;
133 static gltexturepool_t *gltexturepoolchain = NULL;
135 static qbyte *resizebuffer = NULL, *colorconvertbuffer;
136 static int resizebuffersize = 0;
137 static qbyte *texturebuffer;
138 static int texturebuffersize = 0;
140 static int realmaxsize = 0;
142 static textypeinfo_t *R_GetTexTypeInfo(int textype, int flags)
144 if (flags & TEXF_ALPHA)
148 case TEXTYPE_PALETTE:
149 return &textype_palette_alpha;
151 Host_Error("R_GetTexTypeInfo: RGB format has no alpha, TEXF_ALPHA not allowed\n");
154 return &textype_rgba_alpha;
156 Host_Error("R_GetTexTypeInfo: unknown texture format\n");
164 case TEXTYPE_PALETTE:
165 return &textype_palette;
169 return &textype_rgba;
171 Host_Error("R_GetTexTypeInfo: unknown texture format\n");
177 static void R_UploadTexture(gltexture_t *t);
179 static void R_PrecacheTexture(gltexture_t *glt)
183 if (glt->flags & TEXF_ALWAYSPRECACHE)
185 else if (r_precachetextures.integer >= 2)
187 else if (r_precachetextures.integer >= 1)
188 if (glt->flags & TEXF_PRECACHE)
192 R_UploadTexture(glt);
195 int R_RealGetTexture(rtexture_t *rt)
200 glt = (gltexture_t *)rt;
201 if (glt->flags & GLTEXF_UPLOAD)
202 R_UploadTexture(glt);
203 glt->texnum = glt->image->texnum;
204 return glt->image->texnum;
210 void R_FreeTexture(rtexture_t *rt)
212 gltexture_t *glt, **gltpointer;
213 gltextureimage_t *image, **gltimagepointer;
215 glt = (gltexture_t *)rt;
217 Host_Error("R_FreeTexture: texture == NULL\n");
219 for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
220 if (*gltpointer == glt)
221 *gltpointer = glt->chain;
223 Host_Error("R_FreeTexture: texture \"%s\" not linked in pool\n", glt->identifier);
225 // note: if freeing a fragment texture, this will not make the claimed
226 // space available for new textures unless all other fragments in the
227 // image are also freed
231 image->texturecount--;
232 if (image->texturecount < 1)
234 for (gltimagepointer = &glt->pool->imagechain;*gltimagepointer && *gltimagepointer != image;gltimagepointer = &(*gltimagepointer)->imagechain);
235 if (*gltimagepointer == image)
236 *gltimagepointer = image->imagechain;
238 Host_Error("R_FreeTexture: image not linked in pool\n");
240 qglDeleteTextures(1, &image->texnum);
241 if (image->blockallocation)
242 Mem_Free(image->blockallocation);
248 Mem_Free(glt->identifier);
249 if (glt->inputtexels)
250 Mem_Free(glt->inputtexels);
255 static gltexture_t *R_FindTexture (gltexturepool_t *pool, char *identifier)
262 for (glt = pool->gltchain;glt;glt = glt->chain)
263 if (glt->identifier && !strcmp (identifier, glt->identifier))
270 rtexturepool_t *R_AllocTexturePool(void)
272 gltexturepool_t *pool;
273 if (texturemempool == NULL)
275 pool = Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
278 pool->next = gltexturepoolchain;
279 gltexturepoolchain = pool;
280 pool->sentinel = TEXTUREPOOL_SENTINEL;
281 return (rtexturepool_t *)pool;
284 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
286 gltexturepool_t *pool, **poolpointer;
287 if (rtexturepool == NULL)
289 if (*rtexturepool == NULL)
291 pool = (gltexturepool_t *)(*rtexturepool);
292 *rtexturepool = NULL;
293 if (pool->sentinel != TEXTUREPOOL_SENTINEL)
294 Host_Error("R_FreeTexturePool: pool already freed\n");
295 for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
296 if (*poolpointer == pool)
297 *poolpointer = pool->next;
299 Host_Error("R_FreeTexturePool: pool not linked\n");
300 while (pool->gltchain)
301 R_FreeTexture((rtexture_t *)pool->gltchain);
302 if (pool->imagechain)
303 Sys_Error("R_FreeTexturePool: not all images freed\n");
311 int minification, magnification;
315 static glmode_t modes[] =
317 {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
318 {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
319 {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
320 {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
321 {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
322 {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
325 static void GL_TextureMode_f (void)
329 gltextureimage_t *image;
330 gltexturepool_t *pool;
334 for (i = 0;i < 6;i++)
336 if (gl_filter_min == modes[i].minification)
338 Con_Printf ("%s\n", modes[i].name);
342 Con_Printf ("current filter is unknown???\n");
346 for (i = 0;i < 6;i++)
347 if (!strcasecmp (modes[i].name, Cmd_Argv(1) ) )
351 Con_Printf ("bad filter name\n");
355 gl_filter_min = modes[i].minification;
356 gl_filter_mag = modes[i].magnification;
358 // change all the existing mipmap texture objects
359 // FIXME: force renderer(/client/something?) restart instead?
360 for (pool = gltexturepoolchain;pool;pool = pool->next)
362 for (image = pool->imagechain;image;image = image->imagechain)
364 // only update already uploaded images
365 if (!(image->flags & GLTEXF_UPLOAD))
367 qglGetIntegerv(gltexturetypebindingenums[image->texturetype], &oldbindtexnum);
368 qglBindTexture(gltexturetypeenums[image->texturetype], image->texnum);
369 if (image->flags & TEXF_MIPMAP)
370 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_min);
372 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_mag);
373 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MAG_FILTER, gl_filter_mag);
374 qglBindTexture(gltexturetypeenums[image->texturetype], oldbindtexnum);
380 static int R_CalcTexelDataSize (gltexture_t *glt)
382 int width2, height2, depth2, size;
383 if (glt->flags & TEXF_FRAGMENT)
384 size = glt->width * glt->height * glt->depth;
387 if (r_max_size.integer > realmaxsize)
388 Cvar_SetValue("r_max_size", realmaxsize);
389 // calculate final size
390 for (width2 = 1;width2 < glt->width;width2 <<= 1);
391 for (height2 = 1;height2 < glt->height;height2 <<= 1);
392 for (depth2 = 1;depth2 < glt->depth;depth2 <<= 1);
393 for (width2 >>= r_picmip.integer;width2 > r_max_size.integer;width2 >>= 1);
394 for (height2 >>= r_picmip.integer;height2 > r_max_size.integer;height2 >>= 1);
395 for (depth2 >>= r_picmip.integer;depth2 > r_max_size.integer;depth2 >>= 1);
396 if (width2 < 1) width2 = 1;
397 if (height2 < 1) height2 = 1;
398 if (depth2 < 1) depth2 = 1;
401 if (glt->flags & TEXF_MIPMAP)
403 while (width2 > 1 || height2 > 1 || depth2 > 1)
405 size += width2 * height2 * depth2;
413 size++; // count the last 1x1 mipmap
416 size = width2 * height2 * depth2;
418 size *= glt->textype->internalbytesperpixel * glt->image->sides;
423 void R_TextureStats_PrintTotal(void)
425 int glsize, total = 0, totalt = 0, totalp = 0, loaded = 0, loadedt = 0, loadedp = 0;
427 gltexturepool_t *pool;
428 for (pool = gltexturepoolchain;pool;pool = pool->next)
430 for (glt = pool->gltchain;glt;glt = glt->chain)
432 glsize = R_CalcTexelDataSize(glt);
435 totalp += glt->inputdatasize;
436 if (!(glt->flags & GLTEXF_UPLOAD))
440 loadedp += glt->inputdatasize;
444 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);
447 static void R_TextureStats_f(void)
451 gltexturepool_t *pool;
452 Con_Printf("glsize input loaded mip alpha name\n");
453 for (pool = gltexturepoolchain;pool;pool = pool->next)
455 for (glt = pool->gltchain;glt;glt = glt->chain)
457 loaded = !(glt->flags & GLTEXF_UPLOAD);
458 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>");
460 Con_Printf("pool %10p\n", pool);
462 R_TextureStats_PrintTotal();
465 char engineversion[40];
467 static void r_textures_start(void)
469 // deal with size limits of various drivers (3dfx in particular)
470 qglGetIntegerv(GL_MAX_TEXTURE_SIZE, &realmaxsize);
472 // LordHavoc: allow any alignment
473 qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);
476 // use the largest scrap texture size we can (not sure if this is really a good idea)
477 for (block_size = 1;block_size < realmaxsize && block_size < r_max_scrapsize.integer;block_size <<= 1);
479 texturemempool = Mem_AllocPool("Texture Info");
480 texturedatamempool = Mem_AllocPool("Texture Storage (not yet uploaded)");
481 textureprocessingmempool = Mem_AllocPool("Texture Processing Buffers");
483 // Disable JPEG screenshots if the DLL isn't loaded
484 if (! JPEG_OpenLibrary ())
485 Cvar_SetValueQuick (&scr_screenshot_jpeg, 0);
488 static void r_textures_shutdown(void)
490 rtexturepool_t *temp;
492 JPEG_CloseLibrary ();
494 while(gltexturepoolchain)
496 temp = (rtexturepool_t *) gltexturepoolchain;
497 R_FreeTexturePool(&temp);
500 resizebuffersize = 0;
501 texturebuffersize = 0;
503 colorconvertbuffer = NULL;
504 texturebuffer = NULL;
505 Mem_FreePool(&texturemempool);
506 Mem_FreePool(&texturedatamempool);
507 Mem_FreePool(&textureprocessingmempool);
510 static void r_textures_newmap(void)
514 void R_Textures_Init (void)
516 Cmd_AddCommand("gl_texturemode", &GL_TextureMode_f);
517 Cmd_AddCommand("r_texturestats", R_TextureStats_f);
518 Cvar_RegisterVariable (&r_max_scrapsize);
519 Cvar_RegisterVariable (&r_max_size);
520 Cvar_RegisterVariable (&r_picmip);
521 Cvar_RegisterVariable (&r_lerpimages);
522 Cvar_RegisterVariable (&r_precachetextures);
523 Cvar_RegisterVariable (&gl_texture_anisotropy);
525 R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
528 void R_Textures_Frame (void)
530 // could do procedural texture animation here, if we keep track of which
531 // textures were accessed this frame...
533 // free the resize buffers
534 resizebuffersize = 0;
537 Mem_Free(resizebuffer);
540 if (colorconvertbuffer)
542 Mem_Free(colorconvertbuffer);
543 colorconvertbuffer = NULL;
547 void R_MakeResizeBufferBigger(int size)
549 if (resizebuffersize < size)
551 resizebuffersize = size;
553 Mem_Free(resizebuffer);
554 if (colorconvertbuffer)
555 Mem_Free(colorconvertbuffer);
556 resizebuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
557 colorconvertbuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
558 if (!resizebuffer || !colorconvertbuffer)
559 Host_Error("R_Upload: out of memory\n");
563 static void GL_SetupTextureParameters(int flags, int texturetype)
565 int textureenum = gltexturetypeenums[texturetype];
566 int wrapmode = ((flags & TEXF_CLAMP) && gl_support_clamptoedge) ? GL_CLAMP_TO_EDGE : GL_REPEAT;
570 if (gl_support_anisotropy)
571 qglTexParameterf(textureenum, GL_TEXTURE_MAX_ANISOTROPY_EXT, gl_texture_anisotropy.value);
572 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_S, wrapmode);
573 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_T, wrapmode);
574 if (gltexturetypedimensions[texturetype] >= 3)
575 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_R, wrapmode);
577 if (flags & TEXF_FORCENEAREST)
579 if (flags & TEXF_MIPMAP)
580 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
582 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
583 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
585 else if (flags & TEXF_FORCELINEAR)
587 if (flags & TEXF_MIPMAP)
589 if (gl_filter_min == GL_NEAREST_MIPMAP_LINEAR || gl_filter_min == GL_LINEAR_MIPMAP_LINEAR)
590 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
592 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
595 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
596 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
600 if (flags & TEXF_MIPMAP)
601 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_min);
603 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
604 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
610 static void R_Upload(gltexture_t *glt, qbyte *data)
612 int i, mip, width, height, depth;
619 glt->texnum = glt->image->texnum;
620 // we need to restore the texture binding after finishing the upload
621 qglGetIntegerv(gltexturetypebindingenums[glt->image->texturetype], &oldbindtexnum);
622 qglBindTexture(gltexturetypeenums[glt->image->texturetype], glt->image->texnum);
624 glt->flags &= ~GLTEXF_UPLOAD;
626 if (glt->flags & TEXF_FRAGMENT)
628 if (glt->image->flags & GLTEXF_UPLOAD)
630 glt->image->flags &= ~GLTEXF_UPLOAD;
631 Con_DPrintf("uploaded new fragments image\n");
632 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
633 memset(resizebuffer, 255, glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
634 switch(glt->image->texturetype)
636 case GLTEXTURETYPE_1D:
637 qglTexImage1D(GL_TEXTURE_1D, 0, glt->image->glinternalformat, glt->image->width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
640 case GLTEXTURETYPE_2D:
641 qglTexImage2D(GL_TEXTURE_2D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
644 case GLTEXTURETYPE_3D:
645 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);
649 Host_Error("R_Upload: fragment texture of type other than 1D, 2D, or 3D\n");
652 GL_SetupTextureParameters(glt->image->flags, glt->image->texturetype);
655 if (prevbuffer == NULL)
657 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
658 memset(resizebuffer, 255, glt->width * glt->height * glt->image->depth * glt->image->bytesperpixel);
659 prevbuffer = resizebuffer;
661 else if (glt->textype->textype == TEXTYPE_PALETTE)
663 // promote paletted to RGBA, so we only have to worry about RGB and
664 // RGBA in the rest of this code
665 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->sides * glt->image->bytesperpixel);
666 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height * glt->depth, glt->palette);
667 prevbuffer = colorconvertbuffer;
670 switch(glt->image->texturetype)
672 case GLTEXTURETYPE_1D:
673 qglTexSubImage1D(GL_TEXTURE_1D, 0, glt->x, glt->width, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
676 case GLTEXTURETYPE_2D:
677 qglTexSubImage2D(GL_TEXTURE_2D, 0, glt->x, glt->y, glt->width, glt->height, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
680 case GLTEXTURETYPE_3D:
681 qglTexSubImage3D(GL_TEXTURE_3D, 0, glt->x, glt->y, glt->z, glt->width, glt->height, glt->depth, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
685 Host_Error("R_Upload: fragment texture of type other than 1D, 2D, or 3D\n");
691 glt->image->flags &= ~GLTEXF_UPLOAD;
693 // these are rounded up versions of the size to do better resampling
694 for (width = 1;width < glt->width ;width <<= 1);
695 for (height = 1;height < glt->height;height <<= 1);
696 for (depth = 1;depth < glt->depth ;depth <<= 1);
698 R_MakeResizeBufferBigger(width * height * depth * glt->image->sides * glt->image->bytesperpixel);
700 if (prevbuffer == NULL)
702 width = glt->image->width;
703 height = glt->image->height;
704 depth = glt->image->depth;
705 memset(resizebuffer, 255, width * height * depth * glt->image->bytesperpixel);
706 prevbuffer = resizebuffer;
710 if (glt->textype->textype == TEXTYPE_PALETTE)
712 // promote paletted to RGBA, so we only have to worry about RGB and
713 // RGBA in the rest of this code
714 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height * glt->depth * glt->image->sides, glt->palette);
715 prevbuffer = colorconvertbuffer;
719 // cubemaps contain multiple images and thus get processed a bit differently
720 if (glt->image->texturetype != GLTEXTURETYPE_CUBEMAP)
722 if (glt->width != width || glt->height != height || glt->depth != depth)
724 Image_Resample(prevbuffer, glt->width, glt->height, glt->depth, resizebuffer, width, height, depth, glt->image->bytesperpixel, r_lerpimages.integer);
725 prevbuffer = resizebuffer;
728 while (width > glt->image->width || height > glt->image->height || depth > glt->image->depth)
730 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->image->width, glt->image->height, glt->image->depth, glt->image->bytesperpixel);
731 prevbuffer = resizebuffer;
735 switch(glt->image->texturetype)
737 case GLTEXTURETYPE_1D:
738 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->image->glinternalformat, width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
740 if (glt->flags & TEXF_MIPMAP)
742 while (width > 1 || height > 1 || depth > 1)
744 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
745 prevbuffer = resizebuffer;
746 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->image->glinternalformat, width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
751 case GLTEXTURETYPE_2D:
752 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
754 if (glt->flags & TEXF_MIPMAP)
756 while (width > 1 || height > 1 || depth > 1)
758 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
759 prevbuffer = resizebuffer;
760 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
765 case GLTEXTURETYPE_3D:
766 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->image->glinternalformat, width, height, depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
768 if (glt->flags & TEXF_MIPMAP)
770 while (width > 1 || height > 1 || depth > 1)
772 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
773 prevbuffer = resizebuffer;
774 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->image->glinternalformat, width, height, depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
779 case GLTEXTURETYPE_CUBEMAP:
780 // convert and upload each side in turn,
781 // from a continuous block of input texels
782 texturebuffer = prevbuffer;
783 for (i = 0;i < 6;i++)
785 prevbuffer = texturebuffer;
786 texturebuffer += width * height * depth * glt->textype->inputbytesperpixel;
787 if (glt->width != width || glt->height != height || glt->depth != depth)
789 Image_Resample(prevbuffer, glt->width, glt->height, glt->depth, resizebuffer, width, height, depth, glt->image->bytesperpixel, r_lerpimages.integer);
790 prevbuffer = resizebuffer;
793 while (width > glt->image->width || height > glt->image->height || depth > glt->image->depth)
795 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->image->width, glt->image->height, glt->image->depth, glt->image->bytesperpixel);
796 prevbuffer = resizebuffer;
799 qglTexImage2D(cubemapside[i], mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
801 if (glt->flags & TEXF_MIPMAP)
803 while (width > 1 || height > 1 || depth > 1)
805 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
806 prevbuffer = resizebuffer;
807 qglTexImage2D(cubemapside[i], mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
814 GL_SetupTextureParameters(glt->image->flags, glt->image->texturetype);
816 qglBindTexture(gltexturetypeenums[glt->image->texturetype], oldbindtexnum);
819 static void R_FindImageForTexture(gltexture_t *glt)
821 int i, j, best, best2, x, y, z, w, h, d;
822 textypeinfo_t *texinfo;
823 gltexturepool_t *pool;
824 gltextureimage_t *image, **imagechainpointer;
825 texinfo = glt->textype;
828 // remains -1 until uploaded
837 if (glt->flags & TEXF_FRAGMENT)
839 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain)
841 image = *imagechainpointer;
842 if (image->type != GLIMAGETYPE_FRAGMENTS)
844 if (image->texturetype != glt->texturetype)
846 if ((image->flags ^ glt->flags) & (TEXF_MIPMAP | TEXF_ALPHA | TEXF_CLAMP))
848 if (image->glformat != texinfo->glformat || image->glinternalformat != texinfo->glinternalformat)
850 if (glt->width > image->width || glt->height > image->height || glt->depth > image->depth)
853 // got a fragments texture, find a place in it if we can
854 for (best = image->width, i = 0;i < image->width - w;i++)
856 for (best2 = 0, j = 0;j < w;j++)
858 if (image->blockallocation[i+j] >= best)
860 if (best2 < image->blockallocation[i+j])
861 best2 = image->blockallocation[i+j];
865 // this is a valid spot
871 if (best + h > image->height)
874 for (i = 0;i < w;i++)
875 image->blockallocation[x + i] = best + h;
881 image->texturecount++;
885 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
887 Sys_Error("R_FindImageForTexture: ran out of memory\n");
888 image->type = GLIMAGETYPE_FRAGMENTS;
889 // make sure the created image is big enough for the fragment
890 for (image->width = block_size;image->width < glt->width;image->width <<= 1);
892 if (gltexturetypedimensions[glt->texturetype] >= 2)
893 for (image->height = block_size;image->height < glt->height;image->height <<= 1);
895 if (gltexturetypedimensions[glt->texturetype] >= 3)
896 for (image->depth = block_size;image->depth < glt->depth;image->depth <<= 1);
897 image->blockallocation = Mem_Alloc(texturemempool, image->width * sizeof(short));
898 memset(image->blockallocation, 0, image->width * sizeof(short));
903 for (i = 0;i < w;i++)
904 image->blockallocation[x + i] = y + h;
908 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain);
910 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
912 Sys_Error("R_FindImageForTexture: ran out of memory\n");
913 image->type = GLIMAGETYPE_TILE;
914 image->blockallocation = NULL;
916 // calculate final size
917 if (r_max_size.integer > realmaxsize)
918 Cvar_SetValue("r_max_size", realmaxsize);
919 for (image->width = 1;image->width < glt->width;image->width <<= 1);
920 for (image->height = 1;image->height < glt->height;image->height <<= 1);
921 for (image->depth = 1;image->depth < glt->depth;image->depth <<= 1);
922 for (image->width >>= r_picmip.integer;image->width > r_max_size.integer;image->width >>= 1);
923 for (image->height >>= r_picmip.integer;image->height > r_max_size.integer;image->height >>= 1);
924 for (image->depth >>= r_picmip.integer;image->depth > r_max_size.integer;image->depth >>= 1);
925 if (image->width < 1) image->width = 1;
926 if (image->height < 1) image->height = 1;
927 if (image->depth < 1) image->depth = 1;
929 image->texturetype = glt->texturetype;
930 image->glinternalformat = texinfo->glinternalformat;
931 image->glformat = texinfo->glformat;
932 image->flags = (glt->flags & (TEXF_MIPMAP | TEXF_ALPHA | TEXF_CLAMP)) | GLTEXF_UPLOAD;
933 image->bytesperpixel = texinfo->internalbytesperpixel;
934 image->sides = image->texturetype == GLTEXTURETYPE_CUBEMAP ? 6 : 1;
935 // get a texture number to use
936 qglGenTextures(1, &image->texnum);
937 *imagechainpointer = image;
938 image->texturecount++;
946 // note: R_FindImageForTexture must be called before this
947 static void R_UploadTexture (gltexture_t *glt)
949 if (!(glt->flags & GLTEXF_UPLOAD))
952 R_Upload(glt, glt->inputtexels);
953 if (glt->inputtexels)
955 Mem_Free(glt->inputtexels);
956 glt->inputtexels = NULL;
957 glt->flags |= GLTEXF_DESTROYED;
959 else if (glt->flags & GLTEXF_DESTROYED)
960 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
963 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)
967 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
968 textypeinfo_t *texinfo;
970 if (cls.state == ca_dedicated)
973 if (flags & TEXF_FRAGMENT && texturetype != GLTEXTURETYPE_2D)
974 Sys_Error("R_LoadTexture: only 2D fragment textures implemented\n");
975 if (texturetype == GLTEXTURETYPE_CUBEMAP && !gl_texturecubemap)
976 Sys_Error("R_LoadTexture: cubemap texture not supported by driver\n");
977 if (texturetype == GLTEXTURETYPE_3D && !gl_texture3d)
978 Sys_Error("R_LoadTexture: 3d texture not supported by driver\n");
981 glt = R_FindTexture (pool, identifier);
984 Con_Printf("R_LoadTexture: replacing existing texture %s\n", identifier);
985 R_FreeTexture((rtexture_t *)glt);
989 texinfo = R_GetTexTypeInfo(textype, flags);
990 size = width * height * depth * sides * texinfo->inputbytesperpixel;
992 Sys_Error("R_LoadTexture: bogus texture size (%dx%dx%dx%dbppx%dsides = %d bytes)\n", width, height, depth, texinfo->inputbytesperpixel * 8, sides);
994 // clear the alpha flag if the texture has no transparent pixels
997 case TEXTYPE_PALETTE:
998 if (flags & TEXF_ALPHA)
1000 flags &= ~TEXF_ALPHA;
1003 for (i = 0;i < size;i++)
1005 if (((qbyte *)&palette[data[i]])[3] < 255)
1007 flags |= TEXF_ALPHA;
1015 if (flags & TEXF_ALPHA)
1016 Host_Error("R_LoadTexture: RGB has no alpha, don't specify TEXF_ALPHA\n");
1019 if (flags & TEXF_ALPHA)
1021 flags &= ~TEXF_ALPHA;
1024 for (i = 3;i < size;i += 4)
1028 flags |= TEXF_ALPHA;
1036 Host_Error("R_LoadTexture: unknown texture type\n");
1039 glt = Mem_Alloc(texturemempool, sizeof(gltexture_t));
1042 glt->identifier = Mem_Alloc(texturemempool, strlen(identifier)+1);
1043 strcpy (glt->identifier, identifier);
1046 glt->identifier = NULL;
1048 glt->chain = pool->gltchain;
1049 pool->gltchain = glt;
1051 glt->height = height;
1053 glt->flags = flags | GLTEXF_UPLOAD;
1054 glt->textype = texinfo;
1055 glt->texturetype = texturetype;
1056 glt->inputdatasize = size;
1057 glt->palette = palette;
1061 glt->inputtexels = Mem_Alloc(texturedatamempool, size);
1062 if (glt->inputtexels == NULL)
1063 Sys_Error("R_LoadTexture: out of memory\n");
1064 memcpy(glt->inputtexels, data, size);
1067 glt->inputtexels = NULL;
1069 R_FindImageForTexture(glt);
1070 R_PrecacheTexture(glt);
1072 return (rtexture_t *)glt;
1075 rtexture_t *R_LoadTexture1D(rtexturepool_t *rtexturepool, const char *identifier, int width, const qbyte *data, int textype, int flags, const unsigned int *palette)
1077 return R_SetupTexture(rtexturepool, identifier, width, 1, 1, 1, flags, textype, GLTEXTURETYPE_1D, data, palette);
1080 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)
1082 return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, flags, textype, GLTEXTURETYPE_2D, data, palette);
1085 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)
1087 return R_SetupTexture(rtexturepool, identifier, width, height, depth, 1, flags, textype, GLTEXTURETYPE_3D, data, palette);
1090 rtexture_t *R_LoadTextureCubeMap(rtexturepool_t *rtexturepool, const char *identifier, int width, const qbyte *data, int textype, int flags, const unsigned int *palette)
1092 return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, flags, textype, GLTEXTURETYPE_CUBEMAP, data, palette);
1095 int R_TextureHasAlpha(rtexture_t *rt)
1097 return rt ? (((gltexture_t *)rt)->flags & TEXF_ALPHA) != 0 : false;
1100 int R_TextureWidth(rtexture_t *rt)
1102 return rt ? ((gltexture_t *)rt)->width : 0;
1105 int R_TextureHeight(rtexture_t *rt)
1107 return rt ? ((gltexture_t *)rt)->height : 0;
1110 void R_FragmentLocation3D(rtexture_t *rt, int *x, int *y, int *z, float *fx1, float *fy1, float *fz1, float *fx2, float *fy2, float *fz2)
1113 float iwidth, iheight, idepth;
1114 if (cls.state == ca_dedicated)
1122 if (fx1 || fy1 || fx2 || fy2)
1140 Host_Error("R_FragmentLocation: no texture supplied\n");
1141 glt = (gltexture_t *)rt;
1142 if (glt->flags & TEXF_FRAGMENT)
1148 if (fx1 || fy1 || fx2 || fy2)
1150 iwidth = 1.0f / glt->image->width;
1151 iheight = 1.0f / glt->image->height;
1152 idepth = 1.0f / glt->image->depth;
1154 *fx1 = glt->x * iwidth;
1156 *fy1 = glt->y * iheight;
1158 *fz1 = glt->z * idepth;
1160 *fx2 = (glt->x + glt->width) * iwidth;
1162 *fy2 = (glt->y + glt->height) * iheight;
1164 *fz2 = (glt->z + glt->depth) * idepth;
1175 if (fx1 || fy1 || fx2 || fy2)
1193 void R_FragmentLocation(rtexture_t *rt, int *x, int *y, float *fx1, float *fy1, float *fx2, float *fy2)
1195 R_FragmentLocation3D(rt, x, y, NULL, fx1, fy1, NULL, fx2, fy2, NULL);
1198 int R_CompatibleFragmentWidth(int width, int textype, int flags)
1203 void R_UpdateTexture(rtexture_t *rt, qbyte *data)
1207 Host_Error("R_UpdateTexture: no texture supplied\n");
1209 Host_Error("R_UpdateTexture: no data supplied\n");
1210 glt = (gltexture_t *)rt;
1212 // if it has not been uploaded yet, update the data that will be used when it is
1213 if (glt->inputtexels)
1214 memcpy(glt->inputtexels, data, glt->inputdatasize);
1216 R_Upload(glt, data);