5 cvar_t r_max_size = {CVAR_SAVE, "r_max_size", "2048"};
6 cvar_t r_max_scrapsize = {CVAR_SAVE, "r_max_scrapsize", "256"};
7 cvar_t r_picmip = {CVAR_SAVE, "r_picmip", "0"};
8 cvar_t r_lerpimages = {CVAR_SAVE, "r_lerpimages", "1"};
9 cvar_t r_precachetextures = {CVAR_SAVE, "r_precachetextures", "1"};
11 int gl_filter_min = GL_LINEAR_MIPMAP_LINEAR;
12 int gl_filter_mag = GL_LINEAR;
15 static mempool_t *texturemempool;
16 static mempool_t *texturedatamempool;
17 static mempool_t *textureprocessingmempool;
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};
46 // a tiling texture (most common type)
47 #define GLIMAGETYPE_TILE 0
48 // a fragments texture (contains one or more fragment textures)
49 #define GLIMAGETYPE_FRAGMENTS 1
51 #define GLTEXTURETYPE_1D 0
52 #define GLTEXTURETYPE_2D 1
53 #define GLTEXTURETYPE_3D 2
54 #define GLTEXTURETYPE_CUBEMAP 3
56 static int gltexturetypeenums[4] = {GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D, GL_TEXTURE_CUBE_MAP_ARB};
57 static int gltexturetypedimensions[4] = {1, 2, 3, 2};
58 static int cubemapside[6] =
60 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
61 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
62 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
63 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
64 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
65 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
68 // a gltextureimage can have one (or more if fragments) gltextures inside
69 typedef struct gltextureimage_s
71 struct gltextureimage_s *imagechain;
73 int type; // one of the GLIMAGETYPE_ values
74 int texturetype; // one of the GLTEXTURETYPE_ values
75 int sides; // 1 or 6 depending on texturetype
76 int texnum; // GL texture slot number
77 int width, height, depth; // 3D texture support
78 int bytesperpixel; // bytes per pixel
79 int glformat; // GL_RGB or GL_RGBA
80 int glinternalformat; // 3 or 4
82 short *blockallocation; // fragment allocation (2D only)
86 typedef struct gltexture_s
88 // this field is exposed to the R_GetTexture macro, for speed reasons
89 // (must be identical in rtexture_t)
90 int texnum; // GL texture slot number
92 // pointer to texturepool (check this to see if the texture is allocated)
93 struct gltexturepool_s *pool;
94 // pointer to next texture in texturepool chain
95 struct gltexture_s *chain;
96 // pointer into gltextureimage array
97 gltextureimage_t *image;
98 // name of the texture (this might be removed someday), no duplicates
100 // location in the image, and size
101 int x, y, z, width, height, depth;
102 // copy of the original texture(s) supplied to the upload function, for
103 // delayed uploads (non-precached)
105 // original data size in *inputtexels
107 // flags supplied to the LoadTexture function
108 // (might be altered to remove TEXF_ALPHA), and GLTEXF_ private flags
110 // pointer to one of the textype_ structs
111 textypeinfo_t *textype;
112 // one of the GLTEXTURETYPE_ values
114 // palette if the texture is TEXTYPE_PALETTE
115 const unsigned int *palette;
119 #define TEXTUREPOOL_SENTINEL 0xC0DEDBAD
121 typedef struct gltexturepool_s
124 struct gltextureimage_s *imagechain;
125 struct gltexture_s *gltchain;
126 struct gltexturepool_s *next;
130 static gltexturepool_t *gltexturepoolchain = NULL;
132 static qbyte *resizebuffer = NULL, *colorconvertbuffer;
133 static int resizebuffersize = 0;
134 static qbyte *texturebuffer;
135 static int texturebuffersize = 0;
137 static int realmaxsize = 0;
139 static textypeinfo_t *R_GetTexTypeInfo(int textype, int flags)
141 if (flags & TEXF_ALPHA)
145 case TEXTYPE_PALETTE:
146 return &textype_palette_alpha;
148 Host_Error("R_GetTexTypeInfo: RGB format has no alpha, TEXF_ALPHA not allowed\n");
151 return &textype_rgba_alpha;
153 Host_Error("R_GetTexTypeInfo: unknown texture format\n");
161 case TEXTYPE_PALETTE:
162 return &textype_palette;
166 return &textype_rgba;
168 Host_Error("R_GetTexTypeInfo: unknown texture format\n");
174 static void R_UploadTexture(gltexture_t *t);
176 static void R_PrecacheTexture(gltexture_t *glt)
180 if (glt->flags & TEXF_ALWAYSPRECACHE)
182 else if (r_precachetextures.integer >= 2)
184 else if (r_precachetextures.integer >= 1)
185 if (glt->flags & TEXF_PRECACHE)
189 R_UploadTexture(glt);
192 int R_RealGetTexture(rtexture_t *rt)
197 glt = (gltexture_t *)rt;
198 if (glt->flags & GLTEXF_UPLOAD)
199 R_UploadTexture(glt);
200 glt->texnum = glt->image->texnum;
201 return glt->image->texnum;
207 void R_FreeTexture(rtexture_t *rt)
209 gltexture_t *glt, **gltpointer;
210 gltextureimage_t *image, **gltimagepointer;
212 glt = (gltexture_t *)rt;
214 Host_Error("R_FreeTexture: texture == NULL\n");
216 for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
217 if (*gltpointer == glt)
218 *gltpointer = glt->chain;
220 Host_Error("R_FreeTexture: texture \"%s\" not linked in pool\n", glt->identifier);
222 // note: if freeing a fragment texture, this will not make the claimed
223 // space available for new textures unless all other fragments in the
224 // image are also freed
226 image->texturecount--;
227 if (image->texturecount < 1)
229 for (gltimagepointer = &glt->pool->imagechain;*gltimagepointer && *gltimagepointer != image;gltimagepointer = &(*gltimagepointer)->imagechain);
230 if (*gltimagepointer == image)
231 *gltimagepointer = image->imagechain;
233 Host_Error("R_FreeTexture: image not linked in pool\n");
235 qglDeleteTextures(1, &image->texnum);
236 if (image->blockallocation)
237 Mem_Free(image->blockallocation);
242 Mem_Free(glt->identifier);
243 if (glt->inputtexels)
244 Mem_Free(glt->inputtexels);
249 static gltexture_t *R_FindTexture (gltexturepool_t *pool, char *identifier)
256 for (glt = pool->gltchain;glt;glt = glt->chain)
257 if (glt->identifier && !strcmp (identifier, glt->identifier))
264 rtexturepool_t *R_AllocTexturePool(void)
266 gltexturepool_t *pool;
267 if (texturemempool == NULL)
269 pool = Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
272 pool->next = gltexturepoolchain;
273 gltexturepoolchain = pool;
274 pool->sentinel = TEXTUREPOOL_SENTINEL;
275 return (rtexturepool_t *)pool;
278 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
280 gltexturepool_t *pool, **poolpointer;
281 if (rtexturepool == NULL)
283 if (*rtexturepool == NULL)
285 pool = (gltexturepool_t *)(*rtexturepool);
286 *rtexturepool = NULL;
287 if (pool->sentinel != TEXTUREPOOL_SENTINEL)
288 Host_Error("R_FreeTexturePool: pool already freed\n");
289 for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
290 if (*poolpointer == pool)
291 *poolpointer = pool->next;
293 Host_Error("R_FreeTexturePool: pool not linked\n");
294 while (pool->gltchain)
295 R_FreeTexture((rtexture_t *)pool->gltchain);
296 if (pool->imagechain)
297 Sys_Error("R_FreeTexturePool: not all images freed\n");
305 int minification, magnification;
309 static glmode_t modes[] =
311 {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
312 {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
313 {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
314 {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
315 {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
316 {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
319 extern int gl_backend_rebindtextures;
321 static void GL_TextureMode_f (void)
324 gltextureimage_t *image;
325 gltexturepool_t *pool;
329 for (i = 0;i < 6;i++)
331 if (gl_filter_min == modes[i].minification)
333 Con_Printf ("%s\n", modes[i].name);
337 Con_Printf ("current filter is unknown???\n");
341 for (i = 0;i < 6;i++)
342 if (!Q_strcasecmp (modes[i].name, Cmd_Argv(1) ) )
346 Con_Printf ("bad filter name\n");
350 gl_filter_min = modes[i].minification;
351 gl_filter_mag = modes[i].magnification;
353 // change all the existing mipmap texture objects
354 // FIXME: force renderer(/client/something?) restart instead?
355 for (pool = gltexturepoolchain;pool;pool = pool->next)
357 for (image = pool->imagechain;image;image = image->imagechain)
359 // only update already uploaded images
360 if (!(image->flags & GLTEXF_UPLOAD))
362 qglBindTexture(GL_TEXTURE_2D, image->texnum);
363 if (image->flags & TEXF_MIPMAP)
364 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
366 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
367 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
371 gl_backend_rebindtextures = true;
374 static int R_CalcTexelDataSize (gltexture_t *glt)
376 int width2, height2, depth2, size;
377 if (glt->flags & TEXF_FRAGMENT)
378 size = glt->width * glt->height * glt->depth;
381 if (r_max_size.integer > realmaxsize)
382 Cvar_SetValue("r_max_size", realmaxsize);
383 // calculate final size
384 for (width2 = 1;width2 < glt->width;width2 <<= 1);
385 for (height2 = 1;height2 < glt->height;height2 <<= 1);
386 for (depth2 = 1;depth2 < glt->depth;depth2 <<= 1);
387 for (width2 >>= r_picmip.integer;width2 > r_max_size.integer;width2 >>= 1);
388 for (height2 >>= r_picmip.integer;height2 > r_max_size.integer;height2 >>= 1);
389 for (depth2 >>= r_picmip.integer;depth2 > r_max_size.integer;depth2 >>= 1);
390 if (width2 < 1) width2 = 1;
391 if (height2 < 1) height2 = 1;
392 if (depth2 < 1) depth2 = 1;
395 if (glt->flags & TEXF_MIPMAP)
397 while (width2 > 1 || height2 > 1 || depth2 > 1)
399 size += width2 * height2 * depth2;
407 size++; // count the last 1x1 mipmap
410 size = width2 * height2 * depth2;
412 size *= glt->textype->internalbytesperpixel * glt->image->sides;
417 void R_TextureStats_PrintTotal(void)
419 int glsize, total = 0, totalt = 0, totalp = 0, loaded = 0, loadedt = 0, loadedp = 0;
421 gltexturepool_t *pool;
422 for (pool = gltexturepoolchain;pool;pool = pool->next)
424 for (glt = pool->gltchain;glt;glt = glt->chain)
426 glsize = R_CalcTexelDataSize(glt);
429 totalp += glt->inputdatasize;
430 if (!(glt->flags & GLTEXF_UPLOAD))
434 loadedp += glt->inputdatasize;
438 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);
441 static void R_TextureStats_f(void)
445 gltexturepool_t *pool;
446 Con_Printf("glsize input loaded mip alpha name\n");
447 for (pool = gltexturepoolchain;pool;pool = pool->next)
449 for (glt = pool->gltchain;glt;glt = glt->chain)
451 loaded = !(glt->flags & GLTEXF_UPLOAD);
452 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>");
454 Con_Printf("pool %10p\n", pool);
456 R_TextureStats_PrintTotal();
459 char engineversion[40];
461 static void r_textures_start(void)
463 // deal with size limits of various drivers (3dfx in particular)
464 qglGetIntegerv(GL_MAX_TEXTURE_SIZE, &realmaxsize);
466 // LordHavoc: allow any alignment
467 qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);
470 // use the largest scrap texture size we can (not sure if this is really a good idea)
471 for (block_size = 1;block_size < realmaxsize && block_size < r_max_scrapsize.integer;block_size <<= 1);
473 texturemempool = Mem_AllocPool("Texture Info");
474 texturedatamempool = Mem_AllocPool("Texture Storage (not yet uploaded)");
475 textureprocessingmempool = Mem_AllocPool("Texture Processing Buffers");
478 static void r_textures_shutdown(void)
480 rtexturepool_t *temp;
481 while(gltexturepoolchain)
483 temp = (rtexturepool_t *) gltexturepoolchain;
484 R_FreeTexturePool(&temp);
487 resizebuffersize = 0;
488 texturebuffersize = 0;
490 colorconvertbuffer = NULL;
491 texturebuffer = NULL;
492 Mem_FreePool(&texturemempool);
493 Mem_FreePool(&texturedatamempool);
494 Mem_FreePool(&textureprocessingmempool);
497 static void r_textures_newmap(void)
501 void R_Textures_Init (void)
503 Cmd_AddCommand("gl_texturemode", &GL_TextureMode_f);
504 Cmd_AddCommand("r_texturestats", R_TextureStats_f);
505 Cvar_RegisterVariable (&r_max_scrapsize);
506 Cvar_RegisterVariable (&r_max_size);
507 Cvar_RegisterVariable (&r_picmip);
508 Cvar_RegisterVariable (&r_lerpimages);
509 Cvar_RegisterVariable (&r_precachetextures);
511 R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
514 void R_Textures_Frame (void)
516 // could do procedural texture animation here, if we keep track of which
517 // textures were accessed this frame...
519 // free the resize buffers
520 resizebuffersize = 0;
523 Mem_Free(resizebuffer);
526 if (colorconvertbuffer)
528 Mem_Free(colorconvertbuffer);
529 colorconvertbuffer = NULL;
533 void R_MakeResizeBufferBigger(int size)
535 if (resizebuffersize < size)
537 resizebuffersize = size;
539 Mem_Free(resizebuffer);
540 if (colorconvertbuffer)
541 Mem_Free(colorconvertbuffer);
542 resizebuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
543 colorconvertbuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
544 if (!resizebuffer || !colorconvertbuffer)
545 Host_Error("R_Upload: out of memory\n");
549 static void GL_SetupTextureParameters(int flags, int texturetype)
551 int textureenum = gltexturetypeenums[texturetype];
552 int wrapmode = (flags & TEXF_CLAMP) ? GL_CLAMP : GL_REPEAT;
556 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_S, wrapmode);
557 if (gltexturetypedimensions[texturetype] >= 2)
558 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_T, wrapmode);
559 if (gltexturetypedimensions[texturetype] >= 3)
560 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_R, wrapmode);
562 if (flags & TEXF_MIPMAP)
563 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_min);
565 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
566 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
571 static void R_Upload(gltexture_t *glt, qbyte *data)
573 int i, mip, width, height, depth, internalformat;
579 glt->texnum = glt->image->texnum;
580 qglBindTexture(gltexturetypeenums[glt->image->texturetype], glt->image->texnum);
582 glt->flags &= ~GLTEXF_UPLOAD;
583 gl_backend_rebindtextures = true;
585 if (glt->flags & TEXF_FRAGMENT)
587 if (glt->image->flags & GLTEXF_UPLOAD)
589 glt->image->flags &= ~GLTEXF_UPLOAD;
590 Con_DPrintf("uploaded new fragments image\n");
591 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
592 memset(resizebuffer, 255, glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
593 switch(glt->image->texturetype)
595 case GLTEXTURETYPE_1D:
596 qglTexImage1D(GL_TEXTURE_1D, 0, glt->image->glinternalformat, glt->image->width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
599 case GLTEXTURETYPE_2D:
600 qglTexImage2D(GL_TEXTURE_2D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
603 case GLTEXTURETYPE_3D:
604 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);
608 Host_Error("R_Upload: fragment texture of type other than 1D, 2D, or 3D\n");
611 GL_SetupTextureParameters(glt->image->flags, glt->image->texturetype);
614 if (prevbuffer == NULL)
616 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
617 memset(resizebuffer, 255, glt->width * glt->height * glt->image->depth * glt->image->bytesperpixel);
618 prevbuffer = resizebuffer;
620 else if (glt->textype->textype == TEXTYPE_PALETTE)
622 // promote paletted to RGBA, so we only have to worry about RGB and
623 // RGBA in the rest of this code
624 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
625 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height * glt->depth, glt->palette);
626 prevbuffer = colorconvertbuffer;
629 switch(glt->image->texturetype)
631 case GLTEXTURETYPE_1D:
632 qglTexSubImage1D(GL_TEXTURE_1D, 0, glt->x, glt->width, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
635 case GLTEXTURETYPE_2D:
636 qglTexSubImage2D(GL_TEXTURE_2D, 0, glt->x, glt->y, glt->width, glt->height, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
639 case GLTEXTURETYPE_3D:
640 qglTexSubImage3D(GL_TEXTURE_3D, 0, glt->x, glt->y, glt->z, glt->width, glt->height, glt->depth, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
644 Host_Error("R_Upload: fragment texture of type other than 1D, 2D, or 3D\n");
647 glt->texnum = glt->image->texnum;
651 glt->image->flags &= ~GLTEXF_UPLOAD;
653 // these are rounded up versions of the size to do better resampling
654 for (width = 1;width < glt->width ;width <<= 1);
655 for (height = 1;height < glt->height;height <<= 1);
656 for (depth = 1;depth < glt->depth ;depth <<= 1);
658 R_MakeResizeBufferBigger(width * height * depth * glt->image->bytesperpixel);
660 if (prevbuffer == NULL)
662 width = glt->image->width;
663 height = glt->image->height;
664 depth = glt->image->depth;
665 memset(resizebuffer, 255, width * height * depth * glt->image->bytesperpixel);
666 prevbuffer = resizebuffer;
670 if (glt->textype->textype == TEXTYPE_PALETTE)
672 // promote paletted to RGBA, so we only have to worry about RGB and
673 // RGBA in the rest of this code
674 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height * glt->depth, glt->palette);
675 prevbuffer = colorconvertbuffer;
678 if (glt->width != width || glt->height != height || glt->depth != depth)
680 Image_Resample(prevbuffer, glt->width, glt->height, glt->depth, resizebuffer, width, height, depth, glt->image->bytesperpixel, r_lerpimages.integer);
681 prevbuffer = resizebuffer;
684 // apply picmip/max_size limitations
685 while (width > glt->image->width || height > glt->image->height || depth > glt->image->depth)
687 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->image->width, glt->image->height, glt->image->depth, glt->image->bytesperpixel);
688 prevbuffer = resizebuffer;
692 // 3 and 4 are converted by the driver to it's preferred format for the current display mode
694 if (glt->flags & TEXF_ALPHA)
698 switch(glt->image->texturetype)
700 case GLTEXTURETYPE_1D:
701 qglTexImage1D(GL_TEXTURE_1D, mip++, internalformat, width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
703 if (glt->flags & TEXF_MIPMAP)
705 while (width > 1 || height > 1 || depth > 1)
707 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
708 prevbuffer = resizebuffer;
709 qglTexImage1D(GL_TEXTURE_1D, mip++, internalformat, width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
714 case GLTEXTURETYPE_2D:
715 qglTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
717 if (glt->flags & TEXF_MIPMAP)
719 while (width > 1 || height > 1 || depth > 1)
721 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
722 prevbuffer = resizebuffer;
723 qglTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
728 case GLTEXTURETYPE_3D:
729 qglTexImage3D(GL_TEXTURE_3D, mip++, internalformat, width, height, depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
731 if (glt->flags & TEXF_MIPMAP)
733 while (width > 1 || height > 1 || depth > 1)
735 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
736 prevbuffer = resizebuffer;
737 qglTexImage3D(GL_TEXTURE_3D, mip++, internalformat, width, height, depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
742 case GLTEXTURETYPE_CUBEMAP:
743 // convert and upload each side in turn,
744 // from a continuous block of input texels
745 texturebuffer = prevbuffer;
746 for (i = 0;i < 6;i++)
748 prevbuffer = texturebuffer;
749 texturebuffer += width * height * depth * glt->textype->inputbytesperpixel;
750 qglTexImage2D(cubemapside[i], mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
752 if (glt->flags & TEXF_MIPMAP)
754 while (width > 1 || height > 1 || depth > 1)
756 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
757 prevbuffer = resizebuffer;
758 qglTexImage2D(cubemapside[i], mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
765 GL_SetupTextureParameters(glt->image->flags, glt->image->texturetype);
768 static void R_FindImageForTexture(gltexture_t *glt)
770 int i, j, best, best2, x, y, z, w, h, d;
771 textypeinfo_t *texinfo;
772 gltexturepool_t *pool;
773 gltextureimage_t *image, **imagechainpointer;
774 texinfo = glt->textype;
777 // remains -1 until uploaded
786 if (glt->flags & TEXF_FRAGMENT)
788 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain)
790 image = *imagechainpointer;
791 if (image->type != GLIMAGETYPE_FRAGMENTS)
793 if (image->texturetype != glt->texturetype)
795 if ((image->flags ^ glt->flags) & (TEXF_MIPMAP | TEXF_ALPHA | TEXF_CLAMP))
797 if (image->glformat != texinfo->glformat || image->glinternalformat != texinfo->glinternalformat)
799 if (glt->width > image->width || glt->height > image->height || glt->depth > image->depth)
802 // got a fragments texture, find a place in it if we can
803 for (best = image->width, i = 0;i < image->width - w;i++)
805 for (best2 = 0, j = 0;j < w;j++)
807 if (image->blockallocation[i+j] >= best)
809 if (best2 < image->blockallocation[i+j])
810 best2 = image->blockallocation[i+j];
814 // this is a valid spot
820 if (best + h > image->height)
823 for (i = 0;i < w;i++)
824 image->blockallocation[x + i] = best + h;
830 image->texturecount++;
834 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
836 Sys_Error("R_FindImageForTexture: ran out of memory\n");
837 image->type = GLIMAGETYPE_FRAGMENTS;
838 // make sure the created image is big enough for the fragment
839 for (image->width = block_size;image->width < glt->width;image->width <<= 1);
841 if (gltexturetypedimensions[glt->texturetype] >= 2)
842 for (image->height = block_size;image->height < glt->height;image->height <<= 1);
844 if (gltexturetypedimensions[glt->texturetype] >= 3)
845 for (image->depth = block_size;image->depth < glt->depth;image->depth <<= 1);
846 image->blockallocation = Mem_Alloc(texturemempool, image->width * sizeof(short));
847 memset(image->blockallocation, 0, image->width * sizeof(short));
852 for (i = 0;i < w;i++)
853 image->blockallocation[x + i] = y + h;
857 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain);
859 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
861 Sys_Error("R_FindImageForTexture: ran out of memory\n");
862 image->type = GLIMAGETYPE_TILE;
863 image->blockallocation = NULL;
865 // calculate final size
866 if (r_max_size.integer > realmaxsize)
867 Cvar_SetValue("r_max_size", realmaxsize);
868 for (image->width = 1;image->width < glt->width;image->width <<= 1);
869 for (image->height = 1;image->height < glt->height;image->height <<= 1);
870 for (image->depth = 1;image->depth < glt->depth;image->depth <<= 1);
871 for (image->width >>= r_picmip.integer;image->width > r_max_size.integer;image->width >>= 1);
872 for (image->height >>= r_picmip.integer;image->height > r_max_size.integer;image->height >>= 1);
873 for (image->depth >>= r_picmip.integer;image->depth > r_max_size.integer;image->depth >>= 1);
874 if (image->width < 1) image->width = 1;
875 if (image->height < 1) image->height = 1;
876 if (image->depth < 1) image->depth = 1;
878 image->texturetype = glt->texturetype;
879 image->glinternalformat = texinfo->glinternalformat;
880 image->glformat = texinfo->glformat;
881 image->flags = (glt->flags & (TEXF_MIPMAP | TEXF_ALPHA | TEXF_CLAMP)) | GLTEXF_UPLOAD;
882 image->bytesperpixel = texinfo->internalbytesperpixel;
883 image->sides = image->texturetype == GLTEXTURETYPE_CUBEMAP ? 6 : 1;
884 // get a texture number to use
885 qglGenTextures(1, &image->texnum);
886 *imagechainpointer = image;
887 image->texturecount++;
895 // note: R_FindImageForTexture must be called before this
896 static void R_UploadTexture (gltexture_t *glt)
898 if (!(glt->flags & GLTEXF_UPLOAD))
901 R_Upload(glt, glt->inputtexels);
902 if (glt->inputtexels)
904 Mem_Free(glt->inputtexels);
905 glt->inputtexels = NULL;
906 glt->flags |= GLTEXF_DESTROYED;
908 else if (glt->flags & GLTEXF_DESTROYED)
909 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
912 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)
916 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
917 textypeinfo_t *texinfo;
919 if (cls.state == ca_dedicated)
922 if (flags & TEXF_FRAGMENT && texturetype != GLTEXTURETYPE_2D)
923 Sys_Error("R_LoadTexture: only 2D fragment textures implemented\n");
924 if (texturetype == GLTEXTURETYPE_CUBEMAP && !gl_texturecubemap)
925 Sys_Error("R_LoadTexture: cubemap texture not supported by driver\n");
926 if (texturetype == GLTEXTURETYPE_3D && !gl_texture3d)
927 Sys_Error("R_LoadTexture: 3d texture not supported by driver\n");
930 glt = R_FindTexture (pool, identifier);
933 Con_Printf("R_LoadTexture: replacing existing texture %s\n", identifier);
934 R_FreeTexture((rtexture_t *)glt);
938 texinfo = R_GetTexTypeInfo(textype, flags);
939 size = width * height * depth * sides * texinfo->inputbytesperpixel;
941 // clear the alpha flag if the texture has no transparent pixels
944 case TEXTYPE_PALETTE:
945 if (flags & TEXF_ALPHA)
947 flags &= ~TEXF_ALPHA;
950 for (i = 0;i < size;i++)
952 if (((qbyte *)&palette[data[i]])[3] == 255)
962 if (flags & TEXF_ALPHA)
963 Host_Error("R_LoadTexture: RGB has no alpha, don't specify TEXF_ALPHA\n");
966 if (flags & TEXF_ALPHA)
968 flags &= ~TEXF_ALPHA;
971 for (i = 3;i < size;i += 4)
983 Host_Error("R_LoadTexture: unknown texture type\n");
986 glt = Mem_Alloc(texturemempool, sizeof(gltexture_t));
989 glt->identifier = Mem_Alloc(texturemempool, strlen(identifier)+1);
990 strcpy (glt->identifier, identifier);
993 glt->identifier = NULL;
995 glt->chain = pool->gltchain;
996 pool->gltchain = glt;
998 glt->height = height;
1000 glt->flags = flags | GLTEXF_UPLOAD;
1001 glt->textype = texinfo;
1002 glt->texturetype = texturetype;
1003 glt->inputdatasize = size;
1004 glt->palette = palette;
1008 glt->inputtexels = Mem_Alloc(texturedatamempool, size);
1009 if (glt->inputtexels == NULL)
1010 Sys_Error("R_SetupTexture: out of memory\n");
1011 memcpy(glt->inputtexels, data, size);
1014 glt->inputtexels = NULL;
1016 R_FindImageForTexture(glt);
1017 R_PrecacheTexture(glt);
1019 return (rtexture_t *)glt;
1022 rtexture_t *R_LoadTexture1D(rtexturepool_t *rtexturepool, const char *identifier, int width, const qbyte *data, int textype, int flags, const unsigned int *palette)
1024 return R_SetupTexture(rtexturepool, identifier, width, 1, 1, 1, flags, textype, GLTEXTURETYPE_1D, data, palette);
1027 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)
1029 return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, flags, textype, GLTEXTURETYPE_2D, data, palette);
1032 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)
1034 return R_SetupTexture(rtexturepool, identifier, width, height, depth, 1, flags, textype, GLTEXTURETYPE_3D, data, palette);
1037 rtexture_t *R_LoadTextureCubeMap(rtexturepool_t *rtexturepool, const char *identifier, int width, const qbyte *data, int textype, int flags, const unsigned int *palette)
1039 return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, flags, textype, GLTEXTURETYPE_CUBEMAP, data, palette);
1042 int R_TextureHasAlpha(rtexture_t *rt)
1044 return rt ? (((gltexture_t *)rt)->flags & TEXF_ALPHA) != 0 : false;
1047 int R_TextureWidth(rtexture_t *rt)
1049 return rt ? ((gltexture_t *)rt)->width : 0;
1052 int R_TextureHeight(rtexture_t *rt)
1054 return rt ? ((gltexture_t *)rt)->height : 0;
1057 void R_FragmentLocation3D(rtexture_t *rt, int *x, int *y, int *z, float *fx1, float *fy1, float *fz1, float *fx2, float *fy2, float *fz2)
1060 float iwidth, iheight, idepth;
1061 if (cls.state == ca_dedicated)
1069 if (fx1 || fy1 || fx2 || fy2)
1087 Host_Error("R_FragmentLocation: no texture supplied\n");
1088 glt = (gltexture_t *)rt;
1089 if (glt->flags & TEXF_FRAGMENT)
1095 if (fx1 || fy1 || fx2 || fy2)
1097 iwidth = 1.0f / glt->image->width;
1098 iheight = 1.0f / glt->image->height;
1099 idepth = 1.0f / glt->image->depth;
1101 *fx1 = glt->x * iwidth;
1103 *fy1 = glt->y * iheight;
1105 *fz1 = glt->z * idepth;
1107 *fx2 = (glt->x + glt->width) * iwidth;
1109 *fy2 = (glt->y + glt->height) * iheight;
1111 *fz2 = (glt->z + glt->depth) * idepth;
1122 if (fx1 || fy1 || fx2 || fy2)
1140 void R_FragmentLocation(rtexture_t *rt, int *x, int *y, float *fx1, float *fy1, float *fx2, float *fy2)
1142 R_FragmentLocation3D(rt, x, y, NULL, fx1, fy1, NULL, fx2, fy2, NULL);
1145 int R_CompatibleFragmentWidth(int width, int textype, int flags)
1150 void R_UpdateTexture(rtexture_t *rt, qbyte *data)
1154 Host_Error("R_UpdateTexture: no texture supplied\n");
1156 Host_Error("R_UpdateTexture: no data supplied\n");
1157 glt = (gltexture_t *)rt;
1159 // if it has not been uploaded yet, update the data that will be used when it is
1160 if (glt->inputtexels)
1161 memcpy(glt->inputtexels, data, glt->inputdatasize);
1163 R_Upload(glt, data);