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 switch(glt->image->texturetype)
553 case GLTEXTURETYPE_1D:
554 qglBindTexture(GL_TEXTURE_1D, glt->image->texnum);
557 case GLTEXTURETYPE_2D:
558 qglBindTexture(GL_TEXTURE_2D, glt->image->texnum);
561 case GLTEXTURETYPE_3D:
562 qglBindTexture(GL_TEXTURE_3D, glt->image->texnum);
565 case GLTEXTURETYPE_CUBEMAP:
566 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, glt->image->texnum);
570 glt->flags &= ~GLTEXF_UPLOAD;
571 gl_backend_rebindtextures = true;
573 if (glt->flags & TEXF_FRAGMENT)
575 if (glt->image->flags & GLTEXF_UPLOAD)
577 glt->image->flags &= ~GLTEXF_UPLOAD;
578 Con_DPrintf("uploaded new fragments image\n");
579 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
580 memset(resizebuffer, 255, glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
581 switch(glt->image->texturetype)
583 case GLTEXTURETYPE_1D:
584 qglTexImage1D(GL_TEXTURE_1D, 0, glt->image->glinternalformat, glt->image->width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
586 qglTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
588 qglTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
591 case GLTEXTURETYPE_2D:
592 qglTexImage2D(GL_TEXTURE_2D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
594 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
596 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
599 case GLTEXTURETYPE_3D:
600 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);
602 qglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
604 qglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
608 Host_Error("R_Upload: fragment texture of type other than 1D, 2D, or 3D\n");
613 if (prevbuffer == NULL)
615 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
616 memset(resizebuffer, 255, glt->width * glt->height * glt->image->depth * glt->image->bytesperpixel);
617 prevbuffer = resizebuffer;
619 else if (glt->textype->textype == TEXTYPE_QPALETTE)
621 // promote paletted to RGBA, so we only have to worry about RGB and
622 // RGBA in the rest of this code
623 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
624 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height * glt->depth, d_8to24table);
625 prevbuffer = colorconvertbuffer;
628 switch(glt->image->texturetype)
630 case GLTEXTURETYPE_1D:
631 qglTexSubImage1D(GL_TEXTURE_1D, 0, glt->x, glt->width, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
634 case GLTEXTURETYPE_2D:
635 qglTexSubImage2D(GL_TEXTURE_2D, 0, glt->x, glt->y, glt->width, glt->height, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
638 case GLTEXTURETYPE_3D:
639 qglTexSubImage3D(GL_TEXTURE_3D, 0, glt->x, glt->y, glt->z, glt->width, glt->height, glt->depth, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
643 Host_Error("R_Upload: fragment texture of type other than 1D, 2D, or 3D\n");
646 glt->texnum = glt->image->texnum;
650 glt->image->flags &= ~GLTEXF_UPLOAD;
652 // these are rounded up versions of the size to do better resampling
653 for (width = 1;width < glt->width ;width <<= 1);
654 for (height = 1;height < glt->height;height <<= 1);
655 for (depth = 1;depth < glt->depth ;depth <<= 1);
657 R_MakeResizeBufferBigger(width * height * depth * glt->image->bytesperpixel);
659 if (prevbuffer == NULL)
661 width = glt->image->width;
662 height = glt->image->height;
663 depth = glt->image->depth;
664 memset(resizebuffer, 255, width * height * depth * glt->image->bytesperpixel);
665 prevbuffer = resizebuffer;
669 if (glt->textype->textype == TEXTYPE_QPALETTE)
671 // promote paletted to RGBA, so we only have to worry about RGB and
672 // RGBA in the rest of this code
673 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height * glt->depth, d_8to24table);
674 prevbuffer = colorconvertbuffer;
677 if (glt->width != width || glt->height != height || glt->depth != depth)
679 Image_Resample(prevbuffer, glt->width, glt->height, glt->depth, resizebuffer, width, height, depth, glt->image->bytesperpixel, r_lerpimages.integer);
680 prevbuffer = resizebuffer;
683 // apply picmip/max_size limitations
684 while (width > glt->image->width || height > glt->image->height || depth > glt->image->depth)
686 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->image->width, glt->image->height, glt->image->depth, glt->image->bytesperpixel);
687 prevbuffer = resizebuffer;
691 // 3 and 4 are converted by the driver to it's preferred format for the current display mode
693 if (glt->flags & TEXF_ALPHA)
697 switch(glt->image->texturetype)
699 case GLTEXTURETYPE_1D:
700 qglTexImage1D(GL_TEXTURE_1D, mip++, internalformat, width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
702 if (glt->flags & TEXF_MIPMAP)
704 while (width > 1 || height > 1 || depth > 1)
706 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
707 prevbuffer = resizebuffer;
708 qglTexImage1D(GL_TEXTURE_1D, mip++, internalformat, width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
711 qglTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
713 qglTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
718 qglTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
720 qglTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
724 case GLTEXTURETYPE_2D:
725 qglTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
727 if (glt->flags & TEXF_MIPMAP)
729 while (width > 1 || height > 1 || depth > 1)
731 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
732 prevbuffer = resizebuffer;
733 qglTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
736 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
738 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
743 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
745 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
749 case GLTEXTURETYPE_3D:
750 qglTexImage3D(GL_TEXTURE_3D, mip++, internalformat, width, height, depth, 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 qglTexImage3D(GL_TEXTURE_3D, mip++, internalformat, width, height, depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
761 qglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
763 qglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
768 qglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
770 qglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
774 case GLTEXTURETYPE_CUBEMAP:
775 // convert and upload each side in turn,
776 // from a continuous block of input texels
777 texturebuffer = prevbuffer;
778 for (i = 0;i < 6;i++)
780 prevbuffer = texturebuffer;
781 texturebuffer += width * height * depth * glt->textype->inputbytesperpixel;
782 qglTexImage2D(cubemapside[i], mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
784 if (glt->flags & TEXF_MIPMAP)
786 while (width > 1 || height > 1 || depth > 1)
788 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
789 prevbuffer = resizebuffer;
790 qglTexImage2D(cubemapside[i], mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
793 qglTexParameteri(cubemapside[i], GL_TEXTURE_MIN_FILTER, gl_filter_min);
795 qglTexParameteri(cubemapside[i], GL_TEXTURE_MAG_FILTER, gl_filter_mag);
800 qglTexParameteri(cubemapside[i], GL_TEXTURE_MIN_FILTER, gl_filter_mag);
802 qglTexParameteri(cubemapside[i], GL_TEXTURE_MAG_FILTER, gl_filter_mag);
810 static void R_FindImageForTexture(gltexture_t *glt)
812 int i, j, best, best2, x, y, z, w, h, d;
813 textypeinfo_t *texinfo;
814 gltexturepool_t *pool;
815 gltextureimage_t *image, **imagechainpointer;
816 texinfo = glt->textype;
819 // remains -1 until uploaded
828 if (glt->flags & TEXF_FRAGMENT)
830 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain)
832 image = *imagechainpointer;
833 if (image->type != GLIMAGETYPE_FRAGMENTS)
835 if (image->texturetype != glt->texturetype)
837 if (image->glformat != texinfo->glformat || image->glinternalformat != texinfo->glinternalformat)
839 if (glt->width > image->width || glt->height > image->height)
842 // got a fragments texture, find a place in it if we can
843 for (best = image->width, i = 0;i < image->width - w;i++)
845 for (best2 = 0, j = 0;j < w;j++)
847 if (image->blockallocation[i+j] >= best)
849 if (best2 < image->blockallocation[i+j])
850 best2 = image->blockallocation[i+j];
854 // this is a valid spot
860 if (best + h > image->height)
863 for (i = 0;i < w;i++)
864 image->blockallocation[x + i] = best + h;
870 image->texturecount++;
874 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
876 Sys_Error("R_FindImageForTexture: ran out of memory\n");
877 image->type = GLIMAGETYPE_FRAGMENTS;
878 // make sure the created image is big enough for the fragment
879 for (image->width = block_size;image->width < glt->width;image->width <<= 1);
881 if (glt->texturetype != GLTEXTURETYPE_1D)
882 for (image->height = block_size;image->height < glt->height;image->height <<= 1);
884 if (glt->texturetype == GLTEXTURETYPE_3D)
885 for (image->depth = block_size;image->depth < glt->depth;image->depth <<= 1);
886 image->blockallocation = Mem_Alloc(texturemempool, image->width * sizeof(short));
887 memset(image->blockallocation, 0, image->width * sizeof(short));
892 for (i = 0;i < w;i++)
893 image->blockallocation[x + i] = y + h;
897 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain);
899 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
901 Sys_Error("R_FindImageForTexture: ran out of memory\n");
902 image->type = GLIMAGETYPE_TILE;
903 image->blockallocation = NULL;
905 // calculate final size
906 if (r_max_size.integer > realmaxsize)
907 Cvar_SetValue("r_max_size", realmaxsize);
908 for (image->width = 1;image->width < glt->width;image->width <<= 1);
909 for (image->height = 1;image->height < glt->height;image->height <<= 1);
910 for (image->depth = 1;image->depth < glt->depth;image->depth <<= 1);
911 for (image->width >>= r_picmip.integer;image->width > r_max_size.integer;image->width >>= 1);
912 for (image->height >>= r_picmip.integer;image->height > r_max_size.integer;image->height >>= 1);
913 for (image->depth >>= r_picmip.integer;image->depth > r_max_size.integer;image->depth >>= 1);
914 if (image->width < 1) image->width = 1;
915 if (image->height < 1) image->height = 1;
916 if (image->depth < 1) image->depth = 1;
918 image->texturetype = glt->texturetype;
919 image->glinternalformat = texinfo->glinternalformat;
920 image->glformat = texinfo->glformat;
921 image->flags = (glt->flags & (TEXF_MIPMAP | TEXF_ALPHA)) | GLTEXF_UPLOAD;
922 image->bytesperpixel = texinfo->internalbytesperpixel;
923 image->sides = image->texturetype == GLTEXTURETYPE_CUBEMAP ? 6 : 1;
924 // get a texture number to use
925 qglGenTextures(1, &image->texnum);
926 *imagechainpointer = image;
927 image->texturecount++;
935 // note: R_FindImageForTexture must be called before this
936 static void R_UploadTexture (gltexture_t *glt)
938 if (!(glt->flags & GLTEXF_UPLOAD))
941 R_Upload(glt, glt->inputtexels);
942 if (glt->inputtexels)
944 Mem_Free(glt->inputtexels);
945 glt->inputtexels = NULL;
946 glt->flags |= GLTEXF_DESTROYED;
948 else if (glt->flags & GLTEXF_DESTROYED)
949 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
952 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)
956 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
957 textypeinfo_t *texinfo;
959 if (cls.state == ca_dedicated)
962 if (flags & TEXF_FRAGMENT && texturetype != GLTEXTURETYPE_2D)
963 Sys_Error("R_LoadTexture: only 2D fragment textures implemented\n");
964 if (texturetype == GLTEXTURETYPE_CUBEMAP && !gl_texturecubemap)
965 Sys_Error("R_LoadTexture: cubemap texture not supported by driver\n");
966 if (texturetype == GLTEXTURETYPE_3D && !gl_texture3d)
967 Sys_Error("R_LoadTexture: 3d texture not supported by driver\n");
970 glt = R_FindTexture (pool, identifier);
973 Con_Printf("R_LoadTexture: replacing existing texture %s\n", identifier);
974 R_FreeTexture((rtexture_t *)glt);
978 texinfo = R_GetTexTypeInfo(textype, flags);
979 size = width * height * depth * sides * texinfo->inputbytesperpixel;
981 // clear the alpha flag if the texture has no transparent pixels
984 case TEXTYPE_QPALETTE:
985 if (flags & TEXF_ALPHA)
987 flags &= ~TEXF_ALPHA;
990 for (i = 0;i < size;i++)
1002 if (flags & TEXF_ALPHA)
1003 Host_Error("R_LoadTexture: RGB has no alpha, don't specify TEXF_ALPHA\n");
1006 if (flags & TEXF_ALPHA)
1008 flags &= ~TEXF_ALPHA;
1011 for (i = 3;i < size;i += 4)
1015 flags |= TEXF_ALPHA;
1023 Host_Error("R_LoadTexture: unknown texture type\n");
1026 glt = Mem_Alloc(texturemempool, sizeof(gltexture_t));
1029 glt->identifier = Mem_Alloc(texturemempool, strlen(identifier)+1);
1030 strcpy (glt->identifier, identifier);
1033 glt->identifier = NULL;
1035 glt->chain = pool->gltchain;
1036 pool->gltchain = glt;
1038 glt->height = height;
1040 glt->flags = flags | GLTEXF_UPLOAD;
1041 glt->textype = texinfo;
1042 glt->texturetype = texturetype;
1043 glt->inputdatasize = size;
1047 glt->inputtexels = Mem_Alloc(texturedatamempool, size);
1048 if (glt->inputtexels == NULL)
1049 Sys_Error("R_SetupTexture: out of memory\n");
1050 memcpy(glt->inputtexels, data, size);
1053 glt->inputtexels = NULL;
1055 R_FindImageForTexture(glt);
1056 R_PrecacheTexture(glt);
1058 return (rtexture_t *)glt;
1061 rtexture_t *R_LoadTexture(rtexturepool_t *rtexturepool, char *identifier, int width, int height, qbyte *data, int textype, int flags)
1063 return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, flags, textype, GLTEXTURETYPE_2D, data);
1066 rtexture_t *R_LoadTexture1D(rtexturepool_t *rtexturepool, char *identifier, int width, qbyte *data, int textype, int flags)
1068 return R_SetupTexture(rtexturepool, identifier, width, 1, 1, 1, flags, textype, GLTEXTURETYPE_1D, data);
1071 rtexture_t *R_LoadTexture2D(rtexturepool_t *rtexturepool, char *identifier, int width, int height, qbyte *data, int textype, int flags)
1073 return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, flags, textype, GLTEXTURETYPE_2D, data);
1076 rtexture_t *R_LoadTexture3D(rtexturepool_t *rtexturepool, char *identifier, int width, int height, int depth, qbyte *data, int textype, int flags)
1078 return R_SetupTexture(rtexturepool, identifier, width, height, depth, 1, flags, textype, GLTEXTURETYPE_3D, data);
1081 rtexture_t *R_LoadTextureCubeMap(rtexturepool_t *rtexturepool, char *identifier, int width, qbyte *data, int textype, int flags)
1083 return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, flags, textype, GLTEXTURETYPE_CUBEMAP, data);
1086 int R_TextureHasAlpha(rtexture_t *rt)
1088 return rt ? (((gltexture_t *)rt)->flags & TEXF_ALPHA) != 0 : false;
1091 int R_TextureWidth(rtexture_t *rt)
1093 return rt ? ((gltexture_t *)rt)->width : 0;
1096 int R_TextureHeight(rtexture_t *rt)
1098 return rt ? ((gltexture_t *)rt)->height : 0;
1101 void R_FragmentLocation3D(rtexture_t *rt, int *x, int *y, int *z, float *fx1, float *fy1, float *fz1, float *fx2, float *fy2, float *fz2)
1104 float iwidth, iheight, idepth;
1105 if (cls.state == ca_dedicated)
1113 if (fx1 || fy1 || fx2 || fy2)
1131 Host_Error("R_FragmentLocation: no texture supplied\n");
1132 glt = (gltexture_t *)rt;
1133 if (glt->flags & TEXF_FRAGMENT)
1139 if (fx1 || fy1 || fx2 || fy2)
1141 iwidth = 1.0f / glt->image->width;
1142 iheight = 1.0f / glt->image->height;
1143 idepth = 1.0f / glt->image->depth;
1145 *fx1 = glt->x * iwidth;
1147 *fy1 = glt->y * iheight;
1149 *fz1 = glt->z * idepth;
1151 *fx2 = (glt->x + glt->width) * iwidth;
1153 *fy2 = (glt->y + glt->height) * iheight;
1155 *fz2 = (glt->z + glt->depth) * idepth;
1166 if (fx1 || fy1 || fx2 || fy2)
1184 void R_FragmentLocation(rtexture_t *rt, int *x, int *y, float *fx1, float *fy1, float *fx2, float *fy2)
1186 R_FragmentLocation3D(rt, x, y, NULL, fx1, fy1, NULL, fx2, fy2, NULL);
1189 int R_CompatibleFragmentWidth(int width, int textype, int flags)
1194 void R_UpdateTexture(rtexture_t *rt, qbyte *data)
1198 Host_Error("R_UpdateTexture: no texture supplied\n");
1200 Host_Error("R_UpdateTexture: no data supplied\n");
1201 glt = (gltexture_t *)rt;
1203 // if it has not been uploaded yet, update the data that will be used when it is
1204 if (glt->inputtexels)
1205 memcpy(glt->inputtexels, data, glt->inputdatasize);
1207 R_Upload(glt, data);