4 cvar_t r_max_size = {CVAR_SAVE, "r_max_size", "2048"};
5 cvar_t r_max_scrapsize = {CVAR_SAVE, "r_max_scrapsize", "256"};
6 cvar_t r_picmip = {CVAR_SAVE, "r_picmip", "0"};
7 cvar_t r_lerpimages = {CVAR_SAVE, "r_lerpimages", "1"};
8 cvar_t r_precachetextures = {CVAR_SAVE, "r_precachetextures", "1"};
10 int gl_filter_min = GL_LINEAR_MIPMAP_LINEAR;
11 int gl_filter_mag = GL_LINEAR;
14 static mempool_t *texturemempool;
15 static mempool_t *texturedatamempool;
16 static mempool_t *textureprocessingmempool;
18 // note: this must not conflict with TEXF_ flags in r_textures.h
19 // cleared when a texture is uploaded
20 #define GLTEXF_UPLOAD 0x00010000
21 // bitmask for mismatch checking
22 #define GLTEXF_IMPORTANTBITS (0)
23 // set when image is uploaded and freed
24 #define GLTEXF_DESTROYED 0x00040000
26 // size of images which hold fragment textures, ignores picmip and max_size
27 static int block_size;
32 int inputbytesperpixel;
33 int internalbytesperpixel;
39 static textypeinfo_t textype_qpalette = {TEXTYPE_QPALETTE, 1, 4, GL_RGBA, 3};
40 static textypeinfo_t textype_rgb = {TEXTYPE_RGB , 3, 3, GL_RGB , 3};
41 static textypeinfo_t textype_rgba = {TEXTYPE_RGBA , 4, 4, GL_RGBA, 3};
42 static textypeinfo_t textype_qpalette_alpha = {TEXTYPE_QPALETTE, 1, 4, GL_RGBA, 4};
43 static textypeinfo_t textype_rgba_alpha = {TEXTYPE_RGBA , 4, 4, GL_RGBA, 4};
45 // a tiling texture (most common type)
46 #define GLIMAGETYPE_TILE 0
47 // a fragments texture (contains one or more fragment textures)
48 #define GLIMAGETYPE_FRAGMENTS 1
50 #define GLTEXTURETYPE_1D 0
51 #define GLTEXTURETYPE_2D 1
52 #define GLTEXTURETYPE_3D 2
53 #define GLTEXTURETYPE_CUBEMAP 3
55 static int cubemapside[6] =
57 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
58 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
59 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
60 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
61 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
62 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
65 // a gltextureimage can have one (or more if fragments) gltextures inside
66 typedef struct gltextureimage_s
68 struct gltextureimage_s *imagechain;
70 int type; // one of the GLIMAGETYPE_ values
71 int texturetype; // one of the GLTEXTURETYPE_ values
72 int sides; // 1 or 6 depending on texturetype
73 int texnum; // GL texture slot number
74 int width, height, depth; // 3D texture support
75 int bytesperpixel; // bytes per pixel
76 int glformat; // GL_RGB or GL_RGBA
77 int glinternalformat; // 3 or 4
79 short *blockallocation; // fragment allocation (2D only)
83 typedef struct gltexture_s
85 // this field is exposed to the R_GetTexture macro, for speed reasons
86 // (must be identical in rtexture_t)
87 int texnum; // GL texture slot number
89 // pointer to texturepool (check this to see if the texture is allocated)
90 struct gltexturepool_s *pool;
91 // pointer to next texture in texturepool chain
92 struct gltexture_s *chain;
93 // pointer into gltextureimage array
94 gltextureimage_t *image;
95 // name of the texture (this might be removed someday), no duplicates
97 // location in the image, and size
98 int x, y, z, width, height, depth;
99 // copy of the original texture(s) supplied to the upload function, for
100 // delayed uploads (non-precached)
102 // original data size in *inputtexels
104 // flags supplied to the LoadTexture function
105 // (might be altered to remove TEXF_ALPHA), and GLTEXF_ private flags
107 // pointer to one of the textype_ structs
108 textypeinfo_t *textype;
109 // one of the GLTEXTURETYPE_ values
114 #define TEXTUREPOOL_SENTINEL 0xC0DEDBAD
116 typedef struct gltexturepool_s
119 struct gltextureimage_s *imagechain;
120 struct gltexture_s *gltchain;
121 struct gltexturepool_s *next;
125 static gltexturepool_t *gltexturepoolchain = NULL;
127 static qbyte *resizebuffer = NULL, *colorconvertbuffer;
128 static int resizebuffersize = 0;
129 static qbyte *texturebuffer;
130 static int texturebuffersize = 0;
132 static int realmaxsize = 0;
134 static textypeinfo_t *R_GetTexTypeInfo(int textype, int flags)
136 if (flags & TEXF_ALPHA)
140 case TEXTYPE_QPALETTE:
141 return &textype_qpalette_alpha;
143 Host_Error("R_GetTexTypeInfo: RGB format has no alpha, TEXF_ALPHA not allowed\n");
146 return &textype_rgba_alpha;
148 Host_Error("R_GetTexTypeInfo: unknown texture format\n");
156 case TEXTYPE_QPALETTE:
157 return &textype_qpalette;
161 return &textype_rgba;
163 Host_Error("R_GetTexTypeInfo: unknown texture format\n");
169 static void R_UploadTexture(gltexture_t *t);
171 static void R_PrecacheTexture(gltexture_t *glt)
175 if (glt->flags & TEXF_ALWAYSPRECACHE)
177 else if (r_precachetextures.integer >= 2)
179 else if (r_precachetextures.integer >= 1)
180 if (glt->flags & TEXF_PRECACHE)
184 R_UploadTexture(glt);
187 int R_RealGetTexture(rtexture_t *rt)
192 glt = (gltexture_t *)rt;
193 if (glt->flags & GLTEXF_UPLOAD)
194 R_UploadTexture(glt);
195 glt->texnum = glt->image->texnum;
196 return glt->image->texnum;
202 void R_FreeTexture(rtexture_t *rt)
204 gltexture_t *glt, **gltpointer;
205 gltextureimage_t *image, **gltimagepointer;
207 glt = (gltexture_t *)rt;
209 Host_Error("R_FreeTexture: texture == NULL\n");
211 for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
212 if (*gltpointer == glt)
213 *gltpointer = glt->chain;
215 Host_Error("R_FreeTexture: texture \"%s\" not linked in pool\n", glt->identifier);
217 // note: if freeing a fragment texture, this will not make the claimed
218 // space available for new textures unless all other fragments in the
219 // image are also freed
221 image->texturecount--;
222 if (image->texturecount < 1)
224 for (gltimagepointer = &glt->pool->imagechain;*gltimagepointer && *gltimagepointer != image;gltimagepointer = &(*gltimagepointer)->imagechain);
225 if (*gltimagepointer == image)
226 *gltimagepointer = image->imagechain;
228 Host_Error("R_FreeTexture: image not linked in pool\n");
230 qglDeleteTextures(1, &image->texnum);
231 if (image->blockallocation)
232 Mem_Free(image->blockallocation);
237 Mem_Free(glt->identifier);
238 if (glt->inputtexels)
239 Mem_Free(glt->inputtexels);
244 static gltexture_t *R_FindTexture (gltexturepool_t *pool, char *identifier)
251 for (glt = pool->gltchain;glt;glt = glt->chain)
252 if (glt->identifier && !strcmp (identifier, glt->identifier))
259 rtexturepool_t *R_AllocTexturePool(void)
261 gltexturepool_t *pool;
262 if (texturemempool == NULL)
264 pool = Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
267 pool->next = gltexturepoolchain;
268 gltexturepoolchain = pool;
269 pool->sentinel = TEXTUREPOOL_SENTINEL;
270 return (rtexturepool_t *)pool;
273 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
275 gltexturepool_t *pool, **poolpointer;
276 if (rtexturepool == NULL)
278 if (*rtexturepool == NULL)
280 pool = (gltexturepool_t *)(*rtexturepool);
281 *rtexturepool = NULL;
282 if (pool->sentinel != TEXTUREPOOL_SENTINEL)
283 Host_Error("R_FreeTexturePool: pool already freed\n");
284 for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
285 if (*poolpointer == pool)
286 *poolpointer = pool->next;
288 Host_Error("R_FreeTexturePool: pool not linked\n");
289 while (pool->gltchain)
290 R_FreeTexture((rtexture_t *)pool->gltchain);
291 if (pool->imagechain)
292 Sys_Error("R_FreeTexturePool: not all images freed\n");
300 int minification, magnification;
304 static glmode_t modes[] =
306 {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
307 {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
308 {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
309 {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
310 {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
311 {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
314 extern int gl_backend_rebindtextures;
316 static void GL_TextureMode_f (void)
319 gltextureimage_t *image;
320 gltexturepool_t *pool;
324 for (i = 0;i < 6;i++)
326 if (gl_filter_min == modes[i].minification)
328 Con_Printf ("%s\n", modes[i].name);
332 Con_Printf ("current filter is unknown???\n");
336 for (i = 0;i < 6;i++)
337 if (!Q_strcasecmp (modes[i].name, Cmd_Argv(1) ) )
341 Con_Printf ("bad filter name\n");
345 gl_filter_min = modes[i].minification;
346 gl_filter_mag = modes[i].magnification;
348 // change all the existing mipmap texture objects
349 // FIXME: force renderer(/client/something?) restart instead?
350 for (pool = gltexturepoolchain;pool;pool = pool->next)
352 for (image = pool->imagechain;image;image = image->imagechain)
354 // only update already uploaded images
355 if (!(image->flags & GLTEXF_UPLOAD))
357 qglBindTexture(GL_TEXTURE_2D, image->texnum);
358 if (image->flags & TEXF_MIPMAP)
359 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
361 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
362 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
366 gl_backend_rebindtextures = true;
369 static int R_CalcTexelDataSize (gltexture_t *glt)
371 int width2, height2, depth2, size;
372 if (glt->flags & TEXF_FRAGMENT)
373 size = glt->width * glt->height * glt->depth;
376 if (r_max_size.integer > realmaxsize)
377 Cvar_SetValue("r_max_size", realmaxsize);
378 // calculate final size
379 for (width2 = 1;width2 < glt->width;width2 <<= 1);
380 for (height2 = 1;height2 < glt->height;height2 <<= 1);
381 for (depth2 = 1;depth2 < glt->depth;depth2 <<= 1);
382 for (width2 >>= r_picmip.integer;width2 > r_max_size.integer;width2 >>= 1);
383 for (height2 >>= r_picmip.integer;height2 > r_max_size.integer;height2 >>= 1);
384 for (depth2 >>= r_picmip.integer;depth2 > r_max_size.integer;depth2 >>= 1);
385 if (width2 < 1) width2 = 1;
386 if (height2 < 1) height2 = 1;
387 if (depth2 < 1) depth2 = 1;
390 if (glt->flags & TEXF_MIPMAP)
392 while (width2 > 1 || height2 > 1 || depth2 > 1)
394 size += width2 * height2 * depth2;
402 size++; // count the last 1x1 mipmap
405 size = width2 * height2 * depth2;
407 size *= glt->textype->internalbytesperpixel * glt->image->sides;
412 void R_TextureStats_PrintTotal(void)
414 int glsize, total = 0, totalt = 0, totalp = 0, loaded = 0, loadedt = 0, loadedp = 0;
416 gltexturepool_t *pool;
417 for (pool = gltexturepoolchain;pool;pool = pool->next)
419 for (glt = pool->gltchain;glt;glt = glt->chain)
421 glsize = R_CalcTexelDataSize(glt);
424 totalp += glt->inputdatasize;
425 if (!(glt->flags & GLTEXF_UPLOAD))
429 loadedp += glt->inputdatasize;
433 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);
436 static void R_TextureStats_f(void)
440 gltexturepool_t *pool;
441 Con_Printf("glsize input loaded mip alpha name\n");
442 for (pool = gltexturepoolchain;pool;pool = pool->next)
444 for (glt = pool->gltchain;glt;glt = glt->chain)
446 loaded = !(glt->flags & GLTEXF_UPLOAD);
447 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>");
449 Con_Printf("pool %10p\n", pool);
451 R_TextureStats_PrintTotal();
454 char engineversion[40];
456 static void r_textures_start(void)
458 // deal with size limits of various drivers (3dfx in particular)
459 qglGetIntegerv(GL_MAX_TEXTURE_SIZE, &realmaxsize);
461 // LordHavoc: allow any alignment
462 qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);
465 // use the largest scrap texture size we can (not sure if this is really a good idea)
466 for (block_size = 1;block_size < realmaxsize && block_size < r_max_scrapsize.integer;block_size <<= 1);
468 texturemempool = Mem_AllocPool("Texture Info");
469 texturedatamempool = Mem_AllocPool("Texture Storage (not yet uploaded)");
470 textureprocessingmempool = Mem_AllocPool("Texture Processing Buffers");
473 static void r_textures_shutdown(void)
475 rtexturepool_t *temp;
476 while(gltexturepoolchain)
478 temp = (rtexturepool_t *) gltexturepoolchain;
479 R_FreeTexturePool(&temp);
482 resizebuffersize = 0;
483 texturebuffersize = 0;
485 colorconvertbuffer = NULL;
486 texturebuffer = NULL;
487 Mem_FreePool(&texturemempool);
488 Mem_FreePool(&texturedatamempool);
489 Mem_FreePool(&textureprocessingmempool);
492 static void r_textures_newmap(void)
496 void R_Textures_Init (void)
498 Cmd_AddCommand("gl_texturemode", &GL_TextureMode_f);
499 Cmd_AddCommand("r_texturestats", R_TextureStats_f);
500 Cvar_RegisterVariable (&r_max_scrapsize);
501 Cvar_RegisterVariable (&r_max_size);
502 Cvar_RegisterVariable (&r_picmip);
503 Cvar_RegisterVariable (&r_lerpimages);
504 Cvar_RegisterVariable (&r_precachetextures);
506 R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
509 void R_Textures_Frame (void)
511 // could do procedural texture animation here, if we keep track of which
512 // textures were accessed this frame...
514 // free the resize buffers
515 resizebuffersize = 0;
518 Mem_Free(resizebuffer);
521 if (colorconvertbuffer)
523 Mem_Free(colorconvertbuffer);
524 colorconvertbuffer = NULL;
528 void R_MakeResizeBufferBigger(int size)
530 if (resizebuffersize < size)
532 resizebuffersize = size;
534 Mem_Free(resizebuffer);
535 if (colorconvertbuffer)
536 Mem_Free(colorconvertbuffer);
537 resizebuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
538 colorconvertbuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
539 if (!resizebuffer || !colorconvertbuffer)
540 Host_Error("R_Upload: out of memory\n");
544 static void R_Upload(gltexture_t *glt, qbyte *data)
546 int i, mip, width, height, depth, internalformat;
550 glt->texnum = glt->image->texnum;
551 qglBindTexture(GL_TEXTURE_2D, glt->image->texnum);
553 glt->flags &= ~GLTEXF_UPLOAD;
554 gl_backend_rebindtextures = true;
556 if (glt->flags & TEXF_FRAGMENT)
558 if (glt->image->flags & GLTEXF_UPLOAD)
560 glt->image->flags &= ~GLTEXF_UPLOAD;
561 Con_DPrintf("uploaded new fragments image\n");
562 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
563 memset(resizebuffer, 255, glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
564 switch(glt->image->texturetype)
566 case GLTEXTURETYPE_1D:
567 qglTexImage1D(GL_TEXTURE_1D, 0, glt->image->glinternalformat, glt->image->width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
569 qglTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
571 qglTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
574 case GLTEXTURETYPE_2D:
575 qglTexImage2D(GL_TEXTURE_2D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
577 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
579 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
582 case GLTEXTURETYPE_3D:
583 qglTexImage3DEXT(GL_TEXTURE_3D_EXT, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, glt->image->depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
585 qglTexParameteri(GL_TEXTURE_3D_EXT, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
587 qglTexParameteri(GL_TEXTURE_3D_EXT, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
591 Host_Error("R_Upload: fragment texture of type other than 1D, 2D, or 3D\n");
596 if (prevbuffer == NULL)
598 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
599 memset(resizebuffer, 255, glt->width * glt->height * glt->image->depth * glt->image->bytesperpixel);
600 prevbuffer = resizebuffer;
602 else if (glt->textype->textype == TEXTYPE_QPALETTE)
604 // promote paletted to RGBA, so we only have to worry about RGB and
605 // RGBA in the rest of this code
606 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
607 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height * glt->depth, d_8to24table);
608 prevbuffer = colorconvertbuffer;
611 switch(glt->image->texturetype)
613 case GLTEXTURETYPE_1D:
614 qglTexSubImage1D(GL_TEXTURE_1D, 0, glt->x, glt->width, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
617 case GLTEXTURETYPE_2D:
618 qglTexSubImage2D(GL_TEXTURE_2D, 0, glt->x, glt->y, glt->width, glt->height, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
621 case GLTEXTURETYPE_3D:
622 qglTexSubImage3DEXT(GL_TEXTURE_3D_EXT, 0, glt->x, glt->y, glt->z, glt->width, glt->height, glt->depth, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
626 Host_Error("R_Upload: fragment texture of type other than 1D, 2D, or 3D\n");
629 glt->texnum = glt->image->texnum;
633 glt->image->flags &= ~GLTEXF_UPLOAD;
635 // these are rounded up versions of the size to do better resampling
636 for (width = 1;width < glt->width ;width <<= 1);
637 for (height = 1;height < glt->height;height <<= 1);
638 for (depth = 1;depth < glt->depth ;depth <<= 1);
640 R_MakeResizeBufferBigger(width * height * depth * glt->image->bytesperpixel);
642 if (prevbuffer == NULL)
644 width = glt->image->width;
645 height = glt->image->height;
646 depth = glt->image->depth;
647 memset(resizebuffer, 255, width * height * depth * glt->image->bytesperpixel);
648 prevbuffer = resizebuffer;
652 if (glt->textype->textype == TEXTYPE_QPALETTE)
654 // promote paletted to RGBA, so we only have to worry about RGB and
655 // RGBA in the rest of this code
656 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height * glt->depth, d_8to24table);
657 prevbuffer = colorconvertbuffer;
660 if (glt->width != width || glt->height != height || glt->depth != depth)
662 Image_Resample(prevbuffer, glt->width, glt->height, glt->depth, resizebuffer, width, height, depth, glt->image->bytesperpixel, r_lerpimages.integer);
663 prevbuffer = resizebuffer;
666 // apply picmip/max_size limitations
667 while (width > glt->image->width || height > glt->image->height || depth > glt->image->depth)
669 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->image->width, glt->image->height, glt->image->depth, glt->image->bytesperpixel);
670 prevbuffer = resizebuffer;
674 // 3 and 4 are converted by the driver to it's preferred format for the current display mode
676 if (glt->flags & TEXF_ALPHA)
680 switch(glt->image->texturetype)
682 case GLTEXTURETYPE_1D:
683 qglTexImage1D(GL_TEXTURE_1D, mip++, internalformat, width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
685 if (glt->flags & TEXF_MIPMAP)
687 while (width > 1 || height > 1 || depth > 1)
689 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
690 prevbuffer = resizebuffer;
691 qglTexImage1D(GL_TEXTURE_1D, mip++, internalformat, width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
694 qglTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
696 qglTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
701 qglTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
703 qglTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
707 case GLTEXTURETYPE_2D:
708 qglTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
710 if (glt->flags & TEXF_MIPMAP)
712 while (width > 1 || height > 1 || depth > 1)
714 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
715 prevbuffer = resizebuffer;
716 qglTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
719 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
721 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
726 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
728 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
732 case GLTEXTURETYPE_3D:
733 qglTexImage3DEXT(GL_TEXTURE_3D_EXT, mip++, internalformat, width, height, depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
735 if (glt->flags & TEXF_MIPMAP)
737 while (width > 1 || height > 1 || depth > 1)
739 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
740 prevbuffer = resizebuffer;
741 qglTexImage3DEXT(GL_TEXTURE_3D_EXT, mip++, internalformat, width, height, depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
744 qglTexParameteri(GL_TEXTURE_3D_EXT, GL_TEXTURE_MIN_FILTER, gl_filter_min);
746 qglTexParameteri(GL_TEXTURE_3D_EXT, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
751 qglTexParameteri(GL_TEXTURE_3D_EXT, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
753 qglTexParameteri(GL_TEXTURE_3D_EXT, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
757 case GLTEXTURETYPE_CUBEMAP:
758 // convert and upload each side in turn,
759 // from a continuous block of input texels
760 texturebuffer = prevbuffer;
761 for (i = 0;i < 6;i++)
763 prevbuffer = texturebuffer;
764 texturebuffer += width * height * depth * glt->textype->inputbytesperpixel;
765 qglTexImage2D(cubemapside[i], mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
767 if (glt->flags & TEXF_MIPMAP)
769 while (width > 1 || height > 1 || depth > 1)
771 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
772 prevbuffer = resizebuffer;
773 qglTexImage2D(cubemapside[i], mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
776 qglTexParameteri(cubemapside[i], GL_TEXTURE_MIN_FILTER, gl_filter_min);
778 qglTexParameteri(cubemapside[i], GL_TEXTURE_MAG_FILTER, gl_filter_mag);
783 qglTexParameteri(cubemapside[i], GL_TEXTURE_MIN_FILTER, gl_filter_mag);
785 qglTexParameteri(cubemapside[i], GL_TEXTURE_MAG_FILTER, gl_filter_mag);
793 static void R_FindImageForTexture(gltexture_t *glt)
795 int i, j, best, best2, x, y, z, w, h, d;
796 textypeinfo_t *texinfo;
797 gltexturepool_t *pool;
798 gltextureimage_t *image, **imagechainpointer;
799 texinfo = glt->textype;
802 // remains -1 until uploaded
811 if (glt->flags & TEXF_FRAGMENT)
813 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain)
815 image = *imagechainpointer;
816 if (image->type != GLIMAGETYPE_FRAGMENTS)
818 if (image->texturetype != glt->texturetype)
820 if (image->glformat != texinfo->glformat || image->glinternalformat != texinfo->glinternalformat)
822 if (glt->width > image->width || glt->height > image->height)
825 // got a fragments texture, find a place in it if we can
826 for (best = image->width, i = 0;i < image->width - w;i++)
828 for (best2 = 0, j = 0;j < w;j++)
830 if (image->blockallocation[i+j] >= best)
832 if (best2 < image->blockallocation[i+j])
833 best2 = image->blockallocation[i+j];
837 // this is a valid spot
843 if (best + h > image->height)
846 for (i = 0;i < w;i++)
847 image->blockallocation[x + i] = best + h;
853 image->texturecount++;
857 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
859 Sys_Error("R_FindImageForTexture: ran out of memory\n");
860 image->type = GLIMAGETYPE_FRAGMENTS;
861 // make sure the created image is big enough for the fragment
862 for (image->width = block_size;image->width < glt->width;image->width <<= 1);
863 for (image->height = block_size;image->height < glt->height;image->height <<= 1);
864 image->blockallocation = Mem_Alloc(texturemempool, image->width * sizeof(short));
865 memset(image->blockallocation, 0, image->width * sizeof(short));
870 for (i = 0;i < w;i++)
871 image->blockallocation[x + i] = y + h;
875 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain);
877 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
879 Sys_Error("R_FindImageForTexture: ran out of memory\n");
880 image->type = GLIMAGETYPE_TILE;
881 image->blockallocation = NULL;
883 // calculate final size
884 if (r_max_size.integer > realmaxsize)
885 Cvar_SetValue("r_max_size", realmaxsize);
886 for (image->width = 1;image->width < glt->width;image->width <<= 1);
887 for (image->height = 1;image->height < glt->height;image->height <<= 1);
888 for (image->depth = 1;image->depth < glt->depth;image->depth <<= 1);
889 for (image->width >>= r_picmip.integer;image->width > r_max_size.integer;image->width >>= 1);
890 for (image->height >>= r_picmip.integer;image->height > r_max_size.integer;image->height >>= 1);
891 for (image->depth >>= r_picmip.integer;image->depth > r_max_size.integer;image->depth >>= 1);
892 if (image->width < 1) image->width = 1;
893 if (image->height < 1) image->height = 1;
894 if (image->depth < 1) image->depth = 1;
896 image->texturetype = glt->texturetype;
897 image->glinternalformat = texinfo->glinternalformat;
898 image->glformat = texinfo->glformat;
899 image->flags = (glt->flags & (TEXF_MIPMAP | TEXF_ALPHA)) | GLTEXF_UPLOAD;
900 image->bytesperpixel = texinfo->internalbytesperpixel;
901 image->sides = image->texturetype == GLTEXTURETYPE_CUBEMAP ? 6 : 1;
902 // get a texture number to use
903 qglGenTextures(1, &image->texnum);
904 *imagechainpointer = image;
905 image->texturecount++;
913 // note: R_FindImageForTexture must be called before this
914 static void R_UploadTexture (gltexture_t *glt)
916 if (!(glt->flags & GLTEXF_UPLOAD))
919 R_Upload(glt, glt->inputtexels);
920 if (glt->inputtexels)
922 Mem_Free(glt->inputtexels);
923 glt->inputtexels = NULL;
924 glt->flags |= GLTEXF_DESTROYED;
926 else if (glt->flags & GLTEXF_DESTROYED)
927 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
930 static rtexture_t *R_SetupTexture(rtexturepool_t *rtexturepool, char *identifier, int width, int height, int depth, int sides, int flags, int textype, int texturetype, qbyte *data)
934 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
935 textypeinfo_t *texinfo;
937 if (cls.state == ca_dedicated)
940 if (texturetype == GLTEXTURETYPE_CUBEMAP)
942 if (flags & TEXF_FRAGMENT)
943 Sys_Error("R_LoadTexture: fragment cubemap texture??\n");
944 if (!gl_texturecubemap)
945 Sys_Error("R_LoadTexture: cubemap texture not supported by driver\n");
947 if (texturetype == GLTEXTURETYPE_3D)
949 Sys_Error("R_LoadTexture: 3d texture not supported by driver\n");
952 glt = R_FindTexture (pool, identifier);
955 Con_Printf("R_LoadTexture: replacing existing texture %s\n", identifier);
956 R_FreeTexture((rtexture_t *)glt);
960 texinfo = R_GetTexTypeInfo(textype, flags);
961 size = width * height * depth * sides * texinfo->inputbytesperpixel;
963 // clear the alpha flag if the texture has no transparent pixels
966 case TEXTYPE_QPALETTE:
967 if (flags & TEXF_ALPHA)
969 flags &= ~TEXF_ALPHA;
972 for (i = 0;i < size;i++)
984 if (flags & TEXF_ALPHA)
985 Host_Error("R_LoadTexture: RGB has no alpha, don't specify TEXF_ALPHA\n");
988 if (flags & TEXF_ALPHA)
990 flags &= ~TEXF_ALPHA;
993 for (i = 3;i < size;i += 4)
1005 Host_Error("R_LoadTexture: unknown texture type\n");
1008 glt = Mem_Alloc(texturemempool, sizeof(gltexture_t));
1011 glt->identifier = Mem_Alloc(texturemempool, strlen(identifier)+1);
1012 strcpy (glt->identifier, identifier);
1015 glt->identifier = NULL;
1017 glt->chain = pool->gltchain;
1018 pool->gltchain = glt;
1020 glt->height = height;
1022 glt->flags = flags | GLTEXF_UPLOAD;
1023 glt->textype = texinfo;
1024 glt->texturetype = texturetype;
1025 glt->inputdatasize = size;
1029 glt->inputtexels = Mem_Alloc(texturedatamempool, size);
1030 if (glt->inputtexels == NULL)
1031 Sys_Error("R_SetupTexture: out of memory\n");
1032 memcpy(glt->inputtexels, data, size);
1035 glt->inputtexels = NULL;
1037 R_FindImageForTexture(glt);
1038 R_PrecacheTexture(glt);
1040 return (rtexture_t *)glt;
1043 rtexture_t *R_LoadTexture(rtexturepool_t *rtexturepool, char *identifier, int width, int height, qbyte *data, int textype, int flags)
1045 return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, flags, textype, GLTEXTURETYPE_2D, data);
1048 rtexture_t *R_LoadTexture1D(rtexturepool_t *rtexturepool, char *identifier, int width, qbyte *data, int textype, int flags)
1050 return R_SetupTexture(rtexturepool, identifier, width, 1, 1, 1, flags, textype, GLTEXTURETYPE_1D, data);
1053 rtexture_t *R_LoadTexture2D(rtexturepool_t *rtexturepool, char *identifier, int width, int height, qbyte *data, int textype, int flags)
1055 return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, flags, textype, GLTEXTURETYPE_2D, data);
1058 rtexture_t *R_LoadTexture3D(rtexturepool_t *rtexturepool, char *identifier, int width, int height, int depth, qbyte *data, int textype, int flags)
1060 return R_SetupTexture(rtexturepool, identifier, width, height, depth, 1, flags, textype, GLTEXTURETYPE_3D, data);
1063 rtexture_t *R_LoadTextureCubeMap(rtexturepool_t *rtexturepool, char *identifier, int width, qbyte *data, int textype, int flags)
1065 return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, flags, textype, GLTEXTURETYPE_CUBEMAP, data);
1068 int R_TextureHasAlpha(rtexture_t *rt)
1070 return rt ? (((gltexture_t *)rt)->flags & TEXF_ALPHA) != 0 : false;
1073 int R_TextureWidth(rtexture_t *rt)
1075 return rt ? ((gltexture_t *)rt)->width : 0;
1078 int R_TextureHeight(rtexture_t *rt)
1080 return rt ? ((gltexture_t *)rt)->height : 0;
1083 void R_FragmentLocation3D(rtexture_t *rt, int *x, int *y, int *z, float *fx1, float *fy1, float *fz1, float *fx2, float *fy2, float *fz2)
1086 float iwidth, iheight, idepth;
1087 if (cls.state == ca_dedicated)
1095 if (fx1 || fy1 || fx2 || fy2)
1113 Host_Error("R_FragmentLocation: no texture supplied\n");
1114 glt = (gltexture_t *)rt;
1115 if (glt->flags & TEXF_FRAGMENT)
1121 if (fx1 || fy1 || fx2 || fy2)
1123 iwidth = 1.0f / glt->image->width;
1124 iheight = 1.0f / glt->image->height;
1125 idepth = 1.0f / glt->image->depth;
1127 *fx1 = glt->x * iwidth;
1129 *fy1 = glt->y * iheight;
1131 *fz1 = glt->z * idepth;
1133 *fx2 = (glt->x + glt->width) * iwidth;
1135 *fy2 = (glt->y + glt->height) * iheight;
1137 *fz2 = (glt->z + glt->depth) * idepth;
1148 if (fx1 || fy1 || fx2 || fy2)
1166 void R_FragmentLocation(rtexture_t *rt, int *x, int *y, float *fx1, float *fy1, float *fx2, float *fy2)
1168 R_FragmentLocation3D(rt, x, y, NULL, fx1, fy1, NULL, fx2, fy2, NULL);
1171 int R_CompatibleFragmentWidth(int width, int textype, int flags)
1176 void R_UpdateTexture(rtexture_t *rt, qbyte *data)
1180 Host_Error("R_UpdateTexture: no texture supplied\n");
1182 Host_Error("R_UpdateTexture: no data supplied\n");
1183 glt = (gltexture_t *)rt;
1185 // if it has not been uploaded yet, update the data that will be used when it is
1186 if (glt->inputtexels)
1187 memcpy(glt->inputtexels, data, glt->inputdatasize);
1189 R_Upload(glt, data);