]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_textures.c
patch from div0 that enables printf format warnings in gcc
[xonotic/darkplaces.git] / gl_textures.c
index cf4131c2e45477862048a77458d962364e6ee4f3..8ac8d6cad73cc6a8521ddf252a9621d287591ce4 100644 (file)
@@ -86,7 +86,8 @@ typedef struct gltexture_s
        int texturetype;
        // palette if the texture is TEXTYPE_PALETTE
        const unsigned int *palette;
-       // power of 2 size, after gl_picmip and gl_max_size are applied
+       // actual stored texture size after gl_picmip and gl_max_size are applied
+       // (power of 2 if gl_support_arb_texture_non_power_of_two is not supported)
        int tilewidth, tileheight, tiledepth;
        // 1 or 6 depending on texturetype
        int sides;
@@ -198,7 +199,10 @@ void R_FreeTexture(rtexture_t *rt)
                Host_Error("R_FreeTexture: texture \"%s\" not linked in pool", glt->identifier);
 
        if (glt->texnum)
-               qglDeleteTextures(1, (GLuint *)&glt->texnum);
+       {
+               CHECKGLERROR
+               qglDeleteTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
+       }
 
        if (glt->inputtexels)
                Mem_Free(glt->inputtexels);
@@ -293,6 +297,7 @@ static void GL_TextureMode_f (void)
 
        // change all the existing mipmap texture objects
        // FIXME: force renderer(/client/something?) restart instead?
+       CHECKGLERROR
        for (pool = gltexturepoolchain;pool;pool = pool->next)
        {
                for (glt = pool->gltchain;glt;glt = glt->chain)
@@ -300,14 +305,18 @@ static void GL_TextureMode_f (void)
                        // only update already uploaded images
                        if (!(glt->flags & (GLTEXF_UPLOAD | TEXF_FORCENEAREST | TEXF_FORCELINEAR)))
                        {
-                               qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);
-                               qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);
+                               qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
+                               qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
                                if (glt->flags & TEXF_MIPMAP)
-                                       qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_min);
+                               {
+                                       qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
+                               }
                                else
-                                       qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_mag);
-                               qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAG_FILTER, gl_filter_mag);
-                               qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);
+                               {
+                                       qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
+                               }
+                               qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
+                               qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
                        }
                }
        }
@@ -343,20 +352,35 @@ static void GL_Texture_CalcImageSize(int texturetype, int flags, int inwidth, in
 
        if (outwidth)
        {
-               for (width2 = 1;width2 < inwidth;width2 <<= 1);
-               for (width2 >>= picmip;width2 > maxsize;width2 >>= 1);
+               if (gl_support_arb_texture_non_power_of_two)
+                       width2 = min(inwidth >> picmip, maxsize);
+               else
+               {
+                       for (width2 = 1;width2 < inwidth;width2 <<= 1);
+                       for (width2 >>= picmip;width2 > maxsize;width2 >>= 1);
+               }
                *outwidth = max(1, width2);
        }
        if (outheight)
        {
-               for (height2 = 1;height2 < inheight;height2 <<= 1);
-               for (height2 >>= picmip;height2 > maxsize;height2 >>= 1);
+               if (gl_support_arb_texture_non_power_of_two)
+                       height2 = min(inheight >> picmip, maxsize);
+               else
+               {
+                       for (height2 = 1;height2 < inheight;height2 <<= 1);
+                       for (height2 >>= picmip;height2 > maxsize;height2 >>= 1);
+               }
                *outheight = max(1, height2);
        }
        if (outdepth)
        {
-               for (depth2 = 1;depth2 < indepth;depth2 <<= 1);
-               for (depth2 >>= picmip;depth2 > maxsize;depth2 >>= 1);
+               if (gl_support_arb_texture_non_power_of_two)
+                       depth2 = min(indepth >> picmip, maxsize);
+               else
+               {
+                       for (depth2 = 1;depth2 < indepth;depth2 <<= 1);
+                       for (depth2 >>= picmip;depth2 > maxsize;depth2 >>= 1);
+               }
                *outdepth = max(1, depth2);
        }
 }
@@ -442,8 +466,9 @@ static void R_TextureStats_f(void)
 static void r_textures_start(void)
 {
        // LordHavoc: allow any alignment
-       qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);
        CHECKGLERROR
+       qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);CHECKGLERROR
+       qglPixelStorei(GL_PACK_ALIGNMENT, 1);CHECKGLERROR
 
        texturemempool = Mem_AllocPool("texture management", 0, NULL);
 
@@ -521,6 +546,7 @@ void R_Textures_Frame (void)
 
                Cvar_SetValueQuick(&gl_texture_anisotropy, old_aniso);
 
+               CHECKGLERROR
                for (pool = gltexturepoolchain;pool;pool = pool->next)
                {
                        for (glt = pool->gltchain;glt;glt = glt->chain)
@@ -528,12 +554,12 @@ void R_Textures_Frame (void)
                                // only update already uploaded images
                                if ((glt->flags & (GLTEXF_UPLOAD | TEXF_MIPMAP)) == TEXF_MIPMAP)
                                {
-                                       qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);
+                                       qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
 
-                                       qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);
+                                       qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
                                        qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAX_ANISOTROPY_EXT, old_aniso);CHECKGLERROR
 
-                                       qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);
+                                       qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
                                }
                        }
                }
@@ -635,14 +661,22 @@ static void R_Upload(gltexture_t *glt, unsigned char *data, int fragx, int fragy
        CHECKGLERROR
 
        // we need to restore the texture binding after finishing the upload
-       qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);
-       qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);
-       CHECKGLERROR
+       qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
+       qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
 
        // these are rounded up versions of the size to do better resampling
-       for (width  = 1;width  < glt->inputwidth ;width  <<= 1);
-       for (height = 1;height < glt->inputheight;height <<= 1);
-       for (depth  = 1;depth  < glt->inputdepth ;depth  <<= 1);
+       if (gl_support_arb_texture_non_power_of_two)
+       {
+               width = glt->inputwidth;
+               height = glt->inputheight;
+               depth = glt->inputdepth;
+       }
+       else
+       {
+               for (width  = 1;width  < glt->inputwidth ;width  <<= 1);
+               for (height = 1;height < glt->inputheight;height <<= 1);
+               for (depth  = 1;depth  < glt->inputdepth ;depth  <<= 1);
+       }
 
        R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel);
        R_MakeResizeBufferBigger(fragwidth * fragheight * fragdepth * glt->sides * glt->bytesperpixel);
@@ -666,16 +700,13 @@ static void R_Upload(gltexture_t *glt, unsigned char *data, int fragx, int fragy
                switch(glt->texturetype)
                {
                case GLTEXTURETYPE_1D:
-                       qglTexSubImage1D(GL_TEXTURE_1D, 0, fragx, fragwidth, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);
-                       CHECKGLERROR
+                       qglTexSubImage1D(GL_TEXTURE_1D, 0, fragx, fragwidth, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
                        break;
                case GLTEXTURETYPE_2D:
-                       qglTexSubImage2D(GL_TEXTURE_2D, 0, fragx, fragy, fragwidth, fragheight, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);
-                       CHECKGLERROR
+                       qglTexSubImage2D(GL_TEXTURE_2D, 0, fragx, fragy, fragwidth, fragheight, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
                        break;
                case GLTEXTURETYPE_3D:
-                       qglTexSubImage3D(GL_TEXTURE_3D, 0, fragx, fragy, fragz, fragwidth, fragheight, fragdepth, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);
-                       CHECKGLERROR
+                       qglTexSubImage3D(GL_TEXTURE_3D, 0, fragx, fragy, fragz, fragwidth, fragheight, fragdepth, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
                        break;
                default:
                        Host_Error("R_Upload: partial update of type other than 1D, 2D, or 3D");
@@ -709,44 +740,38 @@ static void R_Upload(gltexture_t *glt, unsigned char *data, int fragx, int fragy
                switch(glt->texturetype)
                {
                case GLTEXTURETYPE_1D:
-                       qglTexImage1D(GL_TEXTURE_1D, mip++, glt->glinternalformat, width, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);
-                       CHECKGLERROR
+                       qglTexImage1D(GL_TEXTURE_1D, mip++, glt->glinternalformat, width, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
                        if (glt->flags & TEXF_MIPMAP)
                        {
                                while (width > 1 || height > 1 || depth > 1)
                                {
                                        Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->bytesperpixel);
                                        prevbuffer = resizebuffer;
-                                       qglTexImage1D(GL_TEXTURE_1D, mip++, glt->glinternalformat, width, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);
-                                       CHECKGLERROR
+                                       qglTexImage1D(GL_TEXTURE_1D, mip++, glt->glinternalformat, width, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
                                }
                        }
                        break;
                case GLTEXTURETYPE_2D:
-                       qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);
-                       CHECKGLERROR
+                       qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
                        if (glt->flags & TEXF_MIPMAP)
                        {
                                while (width > 1 || height > 1 || depth > 1)
                                {
                                        Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->bytesperpixel);
                                        prevbuffer = resizebuffer;
-                                       qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);
-                                       CHECKGLERROR
+                                       qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
                                }
                        }
                        break;
                case GLTEXTURETYPE_3D:
-                       qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);
-                       CHECKGLERROR
+                       qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
                        if (glt->flags & TEXF_MIPMAP)
                        {
                                while (width > 1 || height > 1 || depth > 1)
                                {
                                        Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->bytesperpixel);
                                        prevbuffer = resizebuffer;
-                                       qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);
-                                       CHECKGLERROR
+                                       qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
                                }
                        }
                        break;
@@ -770,16 +795,14 @@ static void R_Upload(gltexture_t *glt, unsigned char *data, int fragx, int fragy
                                        prevbuffer = resizebuffer;
                                }
                                mip = 0;
-                               qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);
-                               CHECKGLERROR
+                               qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
                                if (glt->flags & TEXF_MIPMAP)
                                {
                                        while (width > 1 || height > 1 || depth > 1)
                                        {
                                                Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->bytesperpixel);
                                                prevbuffer = resizebuffer;
-                                               qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);
-                                               CHECKGLERROR
+                                               qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
                                        }
                                }
                        }
@@ -787,7 +810,7 @@ static void R_Upload(gltexture_t *glt, unsigned char *data, int fragx, int fragy
                }
                GL_SetupTextureParameters(glt->flags, glt->texturetype);
        }
-       qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);
+       qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
 }
 
 static void R_UploadTexture (gltexture_t *glt)
@@ -795,7 +818,8 @@ static void R_UploadTexture (gltexture_t *glt)
        if (!(glt->flags & GLTEXF_UPLOAD))
                return;
 
-       qglGenTextures(1, (GLuint *)&glt->texnum);
+       CHECKGLERROR
+       qglGenTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
        R_Upload(glt, glt->inputtexels, 0, 0, 0, glt->inputwidth, glt->inputheight, glt->inputdepth);
        if (glt->inputtexels)
        {
@@ -832,7 +856,7 @@ static rtexture_t *R_SetupTexture(rtexturepool_t *rtexturepool, const char *iden
        size = width * height * depth * sides * texinfo->inputbytesperpixel;
        if (size < 1)
        {
-               Con_Printf ("R_LoadTexture: bogus texture size (%dx%dx%dx%dbppx%dsides = %d bytes)\n", width, height, depth, texinfo->inputbytesperpixel * 8, sides);
+               Con_Printf ("R_LoadTexture: bogus texture size (%dx%dx%dx%dbppx%dsides = %d bytes)\n", width, height, depth, texinfo->inputbytesperpixel * 8, sides, size);
                return NULL;
        }