]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_textures.c
removed R_Mesh_Draw_NativeOnly (it was not used)
[xonotic/darkplaces.git] / gl_textures.c
index 7c1d44c3abe1e771dbe16dbad93f94c4b98343ed..0a7f2a1d1e3f72d5ff913be2e4da35abc7329bab 100644 (file)
@@ -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;
 
 
@@ -25,7 +26,6 @@ static mempool_t *textureprocessingmempool;
 #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
@@ -200,12 +200,13 @@ int R_GetTexture(rtexture_t *rt)
        return glt->image->texnum;
 }
 
-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 +232,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);
@@ -269,7 +270,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 +293,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);
@@ -360,12 +360,12 @@ static void GL_TextureMode_f (void)
                        // only update already uploaded images
                        if (!(image->flags & GLTEXF_UPLOAD))
                        {
-                               glBindTexture(GL_TEXTURE_2D, image->texnum);
+                               qglBindTexture(GL_TEXTURE_2D, image->texnum);
                                if (image->flags & TEXF_MIPMAP)
-                                       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
+                                       qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
                                else
-                                       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
-                               glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
+                                       qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
+                               qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
                        }
                }
        }
@@ -462,7 +462,7 @@ char engineversion[40];
 static void r_textures_start(void)
 {
        // deal with size limits of various drivers (3dfx in particular)
-       glGetIntegerv(GL_MAX_TEXTURE_SIZE, &realmaxsize);
+       qglGetIntegerv(GL_MAX_TEXTURE_SIZE, &realmaxsize);
        CHECKGLERROR
 
        // use the largest scrap texture size we can (not sure if this is really a good idea)
@@ -472,7 +472,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 +483,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;
@@ -525,7 +518,7 @@ static void R_Upload(gltexture_t *glt, qbyte *data)
        qbyte *prevbuffer;
        prevbuffer = data;
 
-       glBindTexture(GL_TEXTURE_2D, glt->image->texnum);
+       qglBindTexture(GL_TEXTURE_2D, glt->image->texnum);
        CHECKGLERROR
 
        gl_backend_rebindtextures = true;
@@ -552,11 +545,11 @@ static void R_Upload(gltexture_t *glt, qbyte *data)
                        Con_DPrintf("uploaded new fragments image\n");
                        glt->image->flags &= ~GLTEXF_UPLOAD;
                        memset(resizebuffer, 255, glt->image->width * glt->image->height * glt->image->bytesperpixel);
-                       glTexImage2D (GL_TEXTURE_2D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
+                       qglTexImage2D (GL_TEXTURE_2D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
                        CHECKGLERROR
-                       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
+                       qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
                        CHECKGLERROR
-                       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
+                       qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
                        CHECKGLERROR
                }
 
@@ -573,7 +566,7 @@ static void R_Upload(gltexture_t *glt, qbyte *data)
                        prevbuffer = colorconvertbuffer;
                }
 
-               glTexSubImage2D(GL_TEXTURE_2D, 0, glt->x, glt->y, glt->width, glt->height, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
+               qglTexSubImage2D(GL_TEXTURE_2D, 0, glt->x, glt->y, glt->width, glt->height, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
                CHECKGLERROR
                return;
        }
@@ -634,7 +627,7 @@ static void R_Upload(gltexture_t *glt, qbyte *data)
                internalformat = 4;
 
        mip = 0;
-       glTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
+       qglTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
        CHECKGLERROR
        if (glt->flags & TEXF_MIPMAP)
        {
@@ -643,20 +636,20 @@ static void R_Upload(gltexture_t *glt, qbyte *data)
                        Image_MipReduce(prevbuffer, resizebuffer, &width, &height, 1, 1, glt->image->bytesperpixel);
                        prevbuffer = resizebuffer;
 
-                       glTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
+                       qglTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
                        CHECKGLERROR
                }
 
-               glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
+               qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
                CHECKGLERROR
-               glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
+               qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
                CHECKGLERROR
        }
        else
        {
-               glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
+               qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
                CHECKGLERROR
-               glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
+               qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
                CHECKGLERROR
        }
 }
@@ -683,10 +676,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 +697,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 +713,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 +732,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;
 
@@ -810,7 +803,6 @@ static gltexture_t *R_SetupTexture(gltexturepool_t *pool, char *identifier, int
 {
        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);
@@ -872,17 +864,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,7 +922,7 @@ 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);
+               R_FreeTexture((rtexture_t *)glt);
        }
 
        return (rtexture_t *)R_SetupTexture(pool, identifier, crc, width, height, flags | GLTEXF_UPLOAD, texinfo, data, NULL, NULL, 0);
@@ -955,16 +939,9 @@ rtexture_t *R_ProceduralTexture (rtexturepool_t *rtexturepool, char *identifier,
 
        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");
-       }
 
        // see if the texture is already present
        if (identifier && (glt = R_FindTexture(pool, identifier)))
@@ -975,7 +952,7 @@ rtexture_t *R_ProceduralTexture (rtexturepool_t *rtexturepool, char *identifier,
                        return (rtexture_t *)glt; // exact match, use existing
                }
                Con_Printf("R_LoadTexture: cache mismatch, replacing old texture\n");
-               R_FreeTexture(glt);
+               R_FreeTexture((rtexture_t *)glt);
        }
 
        return (rtexture_t *)R_SetupTexture(pool, identifier, 0, width, height, flags | GLTEXF_PROCEDURAL | GLTEXF_UPLOAD, texinfo, NULL, generate, proceduraldata, proceduraldatasize);
@@ -1086,19 +1063,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);