X-Git-Url: https://de.git.xonotic.org/?a=blobdiff_plain;f=gl_textures.c;h=10cba22dd64283763d771ab38a33bf646648671e;hb=80dbf4c394ba53ff5380961115999266f4f4c78b;hp=8961ffe861ef13f922a2774aa899c3935ed9a691;hpb=76d7f3a54dabd285f3b084b1a13dc184b9fd799f;p=xonotic%2Fdarkplaces.git diff --git a/gl_textures.c b/gl_textures.c index 8961ffe8..10cba22d 100644 --- a/gl_textures.c +++ b/gl_textures.c @@ -1,3 +1,4 @@ + #include "quakedef.h" cvar_t r_max_size = {CVAR_SAVE, "r_max_size", "2048"}; @@ -6,7 +7,7 @@ cvar_t r_picmip = {CVAR_SAVE, "r_picmip", "0"}; cvar_t r_lerpimages = {CVAR_SAVE, "r_lerpimages", "1"}; cvar_t r_precachetextures = {CVAR_SAVE, "r_precachetextures", "1"}; -int gl_filter_min = GL_LINEAR_MIPMAP_LINEAR; //NEAREST; +int gl_filter_min = GL_LINEAR_MIPMAP_LINEAR; int gl_filter_mag = GL_LINEAR; @@ -17,15 +18,12 @@ static mempool_t *textureprocessingmempool; // note: this must not conflict with TEXF_ flags in r_textures.h // cleared when a texture is uploaded #define GLTEXF_UPLOAD 0x00010000 -// texture generated by code, also causes permanent GLTEXF_UPLOAD effect -#define GLTEXF_PROCEDURAL 0x00020000 // bitmask for mismatch checking -#define GLTEXF_IMPORTANTBITS (GLTEXF_PROCEDURAL) +#define GLTEXF_IMPORTANTBITS (0) // set when image is uploaded and freed #define GLTEXF_DESTROYED 0x00040000 // size of images which hold fragment textures, ignores picmip and max_size -//#define BLOCK_SIZE 256 static int block_size; // really this number only governs gltexnuminuse @@ -75,6 +73,10 @@ gltextureimage_t; typedef struct gltexture_s { + // this field is exposed to the R_GetTexture macro, for speed reasons + // (must be identical in rtexture_t) + int texnum; // GL texture slot number + // pointer to texturepool (check this to see if the texture is allocated) struct gltexturepool_s *pool; // pointer to next texture in texturepool chain @@ -89,17 +91,9 @@ typedef struct gltexture_s qbyte *inputtexels; // to identify cache mismatchs (this might be removed someday) int crc; - // flags supplied to the LoadTexture/ProceduralTexture functions + // flags supplied to the LoadTexture function // (might be altered to remove TEXF_ALPHA), and GLTEXF_ private flags int flags; - // procedural texture generation function, called once per frame if the texture is used - int (*generate)(qbyte *buffer, int width, int height, void *parameterdata, int parameterdatasize); - // data provided to generate, persistent from call to call - qbyte *proceduraldata; - // size of data - int proceduraldatasize; - // used only to avoid updating the texture more than once per frame - int proceduralframecount; // pointer to one of the textype_ structs textypeinfo_t *textype; } @@ -178,34 +172,28 @@ static void R_PrecacheTexture(gltexture_t *glt) R_UploadTexture(glt); } -int R_GetTexture(rtexture_t *rt) +int R_RealGetTexture(rtexture_t *rt) { - gltexture_t *glt; - if (!rt) - return 0; - glt = (gltexture_t *)rt; - if (glt->flags & (GLTEXF_UPLOAD | GLTEXF_PROCEDURAL)) + if (rt) { - if (glt->flags & GLTEXF_PROCEDURAL) - { - if (glt->proceduralframecount != r_framecount) - { - glt->proceduralframecount = r_framecount; - R_UploadTexture(glt); - } - } - else + gltexture_t *glt; + glt = (gltexture_t *)rt; + if (glt->flags & GLTEXF_UPLOAD) R_UploadTexture(glt); + glt->texnum = glt->image->texnum; + return glt->image->texnum; } - return glt->image->texnum; + else + return 0; } -static void R_FreeTexture(gltexture_t *glt) +void R_FreeTexture(rtexture_t *rt) { - gltexture_t **gltpointer; + gltexture_t *glt, **gltpointer; gltextureimage_t *image, **gltimagepointer; GLuint texnum; + glt = (gltexture_t *)rt; if (glt == NULL) Host_Error("R_FreeTexture: texture == NULL\n"); @@ -231,7 +219,7 @@ static void R_FreeTexture(gltexture_t *glt) { texnum = image->texnum; gltexnuminuse[image->texnum] = 0; - glDeleteTextures(1, &texnum); + qglDeleteTextures(1, &texnum); } if (image->blockallocation) Mem_Free(image->blockallocation); @@ -242,8 +230,6 @@ static void R_FreeTexture(gltexture_t *glt) Mem_Free(glt->identifier); if (glt->inputtexels) Mem_Free(glt->inputtexels); - if (glt->proceduraldata) - Mem_Free(glt->proceduraldata); Mem_Free(glt); } @@ -269,7 +255,6 @@ rtexturepool_t *R_AllocTexturePool(void) pool = Mem_Alloc(texturemempool, sizeof(gltexturepool_t)); if (pool == NULL) return NULL; - //memset(pool, 0, sizeof(gltexturepool_t)); pool->next = gltexturepoolchain; gltexturepoolchain = pool; pool->sentinel = TEXTUREPOOL_SENTINEL; @@ -293,7 +278,7 @@ void R_FreeTexturePool(rtexturepool_t **rtexturepool) else Host_Error("R_FreeTexturePool: pool not linked\n"); while (pool->gltchain) - R_FreeTexture(pool->gltchain); + R_FreeTexture((rtexture_t *)pool->gltchain); if (pool->imagechain) Sys_Error("R_FreeTexturePool: not all images freed\n"); Mem_Free(pool); @@ -447,10 +432,7 @@ static void R_TextureStats_f(void) for (glt = pool->gltchain;glt;glt = glt->chain) { loaded = !(glt->flags & GLTEXF_UPLOAD); - if (glt->flags & GLTEXF_PROCEDURAL) - Con_Printf("%c%4i%c %4i PROC %s %s %s %s\n" , loaded ? '[' : ' ', (R_CalcTexelDataSize(glt) + 1023) / 1024, loaded ? ']' : ' ', (glt->width * glt->height * glt->textype->inputbytesperpixel + 1023) / 1024, loaded ? "loaded" : " ", (glt->flags & TEXF_MIPMAP) ? "mip" : " ", (glt->flags & TEXF_ALPHA) ? "alpha" : " ", glt->identifier ? glt->identifier : ""); - else - Con_Printf("%c%4i%c%c%4i%c %04X %s %s %s %s\n", loaded ? '[' : ' ', (R_CalcTexelDataSize(glt) + 1023) / 1024, loaded ? ']' : ' ', glt->inputtexels ? '[' : ' ', (glt->width * glt->height * glt->textype->inputbytesperpixel + 1023) / 1024, glt->inputtexels ? ']' : ' ', glt->crc, loaded ? "loaded" : " ", (glt->flags & TEXF_MIPMAP) ? "mip" : " ", (glt->flags & TEXF_ALPHA) ? "alpha" : " ", glt->identifier ? glt->identifier : ""); + Con_Printf("%c%4i%c%c%4i%c %04X %s %s %s %s\n", loaded ? '[' : ' ', (R_CalcTexelDataSize(glt) + 1023) / 1024, loaded ? ']' : ' ', glt->inputtexels ? '[' : ' ', (glt->width * glt->height * glt->textype->inputbytesperpixel + 1023) / 1024, glt->inputtexels ? ']' : ' ', glt->crc, loaded ? "loaded" : " ", (glt->flags & TEXF_MIPMAP) ? "mip" : " ", (glt->flags & TEXF_ALPHA) ? "alpha" : " ", glt->identifier ? glt->identifier : ""); } Con_Printf("pool %10p\n", pool); } @@ -472,7 +454,6 @@ static void r_textures_start(void) texturedatamempool = Mem_AllocPool("Texture Storage (not yet uploaded)"); textureprocessingmempool = Mem_AllocPool("Texture Processing Buffers"); gltexnuminuse = Mem_Alloc(texturemempool, MAX_GLTEXTURES); - //memset(gltexnuminuse, 0, MAX_GLTEXTURES); } static void r_textures_shutdown(void) @@ -484,12 +465,6 @@ static void r_textures_shutdown(void) R_FreeTexturePool(&temp); } - /* - if (resizebuffer) Mem_Free(resizebuffer);resizebuffer = NULL; - if (colorconvertbuffer) Mem_Free(colorconvertbuffer);colorconvertbuffer = NULL; - if (texturebuffer) Mem_Free(texturebuffer);texturebuffer = NULL; - if (gltexnuminuse) Mem_Free(gltexnuminuse);gltexnuminuse = NULL; - */ resizebuffersize = 0; texturebuffersize = 0; resizebuffer = NULL; @@ -575,6 +550,7 @@ static void R_Upload(gltexture_t *glt, qbyte *data) qglTexSubImage2D(GL_TEXTURE_2D, 0, glt->x, glt->y, glt->width, glt->height, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer); CHECKGLERROR + glt->texnum = glt->image->texnum; return; } @@ -659,6 +635,7 @@ static void R_Upload(gltexture_t *glt, qbyte *data) qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag); CHECKGLERROR } + glt->texnum = glt->image->texnum; } static void R_FindImageForTexture(gltexture_t *glt) @@ -670,6 +647,9 @@ static void R_FindImageForTexture(gltexture_t *glt) texinfo = glt->textype; pool = glt->pool; + // remains -1 until uploaded + glt->texnum = -1; + x = 0; y = 0; w = glt->width; @@ -683,10 +663,11 @@ static void R_FindImageForTexture(gltexture_t *glt) continue; if (image->glformat != texinfo->glformat || image->glinternalformat != texinfo->glinternalformat) continue; + if (glt->width > image->width || glt->height > image->height) + continue; // got a fragments texture, find a place in it if we can - best = block_size; - for (best = block_size, i = 0;i < block_size - w;i += texinfo->align) + for (best = image->width, i = 0;i < image->width - w;i += texinfo->align) { for (best2 = 0, j = 0;j < w;j++) { @@ -703,7 +684,7 @@ static void R_FindImageForTexture(gltexture_t *glt) } } - if (best + h > block_size) + if (best + h > image->height) continue; for (i = 0;i < w;i++) @@ -719,12 +700,12 @@ static void R_FindImageForTexture(gltexture_t *glt) image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t)); if (image == NULL) Sys_Error("R_FindImageForTexture: ran out of memory\n"); - //memset(image, 0, sizeof(*image)); image->type = GLIMAGETYPE_FRAGMENTS; - image->width = block_size; - image->height = block_size; - image->blockallocation = Mem_Alloc(texturemempool, block_size * sizeof(short)); - memset(image->blockallocation, 0, block_size * sizeof(short)); + // make sure the created image is big enough for the fragment + for (image->width = block_size;image->width < glt->width;image->width <<= 1); + for (image->height = block_size;image->height < glt->height;image->height <<= 1); + image->blockallocation = Mem_Alloc(texturemempool, image->width * sizeof(short)); + memset(image->blockallocation, 0, image->width * sizeof(short)); x = 0; y = 0; @@ -738,7 +719,6 @@ static void R_FindImageForTexture(gltexture_t *glt) image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t)); if (image == NULL) Sys_Error("R_FindImageForTexture: ran out of memory\n"); - //memset(image, 0, sizeof(*image)); image->type = GLIMAGETYPE_TILE; image->blockallocation = NULL; @@ -774,43 +754,24 @@ static void R_FindImageForTexture(gltexture_t *glt) // note: R_FindImageForTexture must be called before this static void R_UploadTexture (gltexture_t *glt) { - if (!(glt->flags & (GLTEXF_UPLOAD | GLTEXF_PROCEDURAL))) + if (!(glt->flags & GLTEXF_UPLOAD)) return; - if (glt->flags & GLTEXF_PROCEDURAL) - { - if (glt->generate) - { - if (texturebuffersize < glt->width * glt->height * glt->textype->inputbytesperpixel) - { - if (texturebuffer) - Mem_Free(texturebuffer); - texturebuffersize = glt->width * glt->height * glt->textype->inputbytesperpixel; - texturebuffer = Mem_Alloc(textureprocessingmempool, texturebuffersize); - } - - glt->generate(texturebuffer, glt->width, glt->height, (void *)glt->proceduraldata, glt->proceduraldatasize); - } - } - else + R_Upload(glt, glt->inputtexels); + if (glt->inputtexels) { - R_Upload(glt, glt->inputtexels); - if (glt->inputtexels) - { - Mem_Free(glt->inputtexels); - glt->inputtexels = NULL; - glt->flags |= GLTEXF_DESTROYED; - } - else if (glt->flags & GLTEXF_DESTROYED) - Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier); + Mem_Free(glt->inputtexels); + glt->inputtexels = NULL; + glt->flags |= GLTEXF_DESTROYED; } + else if (glt->flags & GLTEXF_DESTROYED) + Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier); } -static gltexture_t *R_SetupTexture(gltexturepool_t *pool, char *identifier, int crc, int width, int height, int flags, textypeinfo_t *texinfo, qbyte *data, int (*generate)(qbyte *buffer, int width, int height, void *proceduraldata, int proceduraldatasize), void *proceduraldata, int proceduraldatasize) +static gltexture_t *R_SetupTexture(gltexturepool_t *pool, char *identifier, int crc, int width, int height, int flags, textypeinfo_t *texinfo, qbyte *data) { gltexture_t *glt; glt = Mem_Alloc(texturemempool, sizeof(gltexture_t)); - //memset(glt, 0, sizeof(gltexture_t)); if (identifier) { glt->identifier = Mem_Alloc(texturemempool, strlen(identifier)+1); @@ -837,17 +798,6 @@ static gltexture_t *R_SetupTexture(gltexturepool_t *pool, char *identifier, int else glt->inputtexels = NULL; - glt->generate = generate; - glt->proceduraldatasize = proceduraldatasize; - if (proceduraldatasize) - { - glt->proceduraldata = Mem_Alloc(texturemempool, proceduraldatasize); - if (glt->proceduraldata == NULL) - Sys_Error("R_SetupTexture: out of memory\n"); - } - else - glt->proceduraldata = NULL; - R_FindImageForTexture(glt); R_PrecacheTexture(glt); @@ -872,17 +822,9 @@ rtexture_t *R_LoadTexture (rtexturepool_t *rtexturepool, char *identifier, int w texinfo = R_GetTexTypeInfo(textype, flags); - // data can be NULL -// if (data == NULL) -// Host_Error("R_LoadTexture: \"%s\" has no data\n", identifier); - if (flags & TEXF_FRAGMENT) - { - if (width > block_size || height > block_size) - Host_Error("R_LoadTexture: fragment too big, must be no more than %dx%d\n", block_size, block_size); if ((width * texinfo->internalbytesperpixel) & 3) Host_Error("R_LoadTexture: incompatible width for fragment"); - } // clear the alpha flag if the texture has no transparent pixels switch(textype) @@ -938,47 +880,10 @@ rtexture_t *R_LoadTexture (rtexturepool_t *rtexturepool, char *identifier, int w return (rtexture_t *)glt; // exact match, use existing } Con_Printf("R_LoadTexture: cache mismatch on %s, replacing old texture\n", identifier); - R_FreeTexture(glt); - } - - return (rtexture_t *)R_SetupTexture(pool, identifier, crc, width, height, flags | GLTEXF_UPLOAD, texinfo, data, NULL, NULL, 0); -} - -rtexture_t *R_ProceduralTexture (rtexturepool_t *rtexturepool, char *identifier, int width, int height, int textype, int flags, int (*generate)(qbyte *buffer, int width, int height, void *proceduraldata, int proceduraldatasize), void *proceduraldata, int proceduraldatasize) -{ - gltexture_t *glt; - gltexturepool_t *pool = (gltexturepool_t *)rtexturepool; - textypeinfo_t *texinfo; - - if (cls.state == ca_dedicated) - return NULL; - - texinfo = R_GetTexTypeInfo(textype, flags); - - // no function is supported, for odd uses -// if (generate == NULL) -// Host_Error("R_ProceduralTexture: \"%s\" has no generate function\n", identifier); - if (flags & TEXF_FRAGMENT) - { - if (width > block_size || height > block_size) - Host_Error("R_ProceduralTexture: fragment too big, must be no more than %dx%d\n", block_size, block_size); - if ((width * texinfo->internalbytesperpixel) & 3) - Host_Error("R_ProceduralTexture: incompatible width for fragment"); + R_FreeTexture((rtexture_t *)glt); } - // see if the texture is already present - if (identifier && (glt = R_FindTexture(pool, identifier))) - { - if (width == glt->width && height == glt->height && texinfo == glt->textype && ((flags ^ glt->flags) & TEXF_IMPORTANTBITS) == 0 && ((flags ^ glt->flags) & GLTEXF_IMPORTANTBITS) == 0) - { - Con_Printf("R_LoadTexture: exact match with existing texture %s\n", identifier); - return (rtexture_t *)glt; // exact match, use existing - } - Con_Printf("R_LoadTexture: cache mismatch, replacing old texture\n"); - R_FreeTexture(glt); - } - - return (rtexture_t *)R_SetupTexture(pool, identifier, 0, width, height, flags | GLTEXF_PROCEDURAL | GLTEXF_UPLOAD, texinfo, NULL, generate, proceduraldata, proceduraldatasize); + return (rtexture_t *)R_SetupTexture(pool, identifier, crc, width, height, flags | GLTEXF_UPLOAD, texinfo, data); } int R_TextureHasAlpha(rtexture_t *rt) @@ -1086,19 +991,7 @@ void R_UpdateTexture(rtexture_t *rt, qbyte *data) if (data == NULL) Host_Error("R_UpdateTexture: no data supplied\n"); glt = (gltexture_t *)rt; - /* - if (!(glt->flags & GLTEXF_PROCEDURAL)) - { - if (glt->inputtexels == NULL) - { - glt->inputtexels = Mem_Alloc(texturedatamempool, glt->width * glt->height * glt->textype->inputbytesperpixel); - if (glt->inputtexels == NULL) - Host_Error("R_UpdateTexture: ran out of memory\n"); - } - memcpy(glt->inputtexels, data, glt->width * glt->height * glt->textype->inputbytesperpixel); - } - R_Upload(glt, data); - */ + // if it has not been uploaded yet, update the data that will be used when it is if (glt->inputtexels) memcpy(glt->inputtexels, data, glt->width * glt->height * glt->textype->inputbytesperpixel);