]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_textures.c
make csqc render entities persistent (except the temp ones created by a
[xonotic/darkplaces.git] / gl_textures.c
index a42595adbf76a408313b8c273b9f13de2867c985..bc6494e8735c57cfb4563d19fa0544cb5bc227a9 100644 (file)
@@ -60,7 +60,8 @@ static textypeinfo_t textype_bgra                   = {TEXTYPE_BGRA   , 4, 4, 4.
 static textypeinfo_t textype_bgra_alpha             = {TEXTYPE_BGRA   , 4, 4, 4.0f, GL_BGRA   , 4, GL_UNSIGNED_BYTE};
 static textypeinfo_t textype_bgra_compress          = {TEXTYPE_BGRA   , 4, 4, 0.5f, GL_BGRA   , GL_COMPRESSED_RGB_ARB, GL_UNSIGNED_BYTE};
 static textypeinfo_t textype_bgra_alpha_compress    = {TEXTYPE_BGRA   , 4, 4, 1.0f, GL_BGRA   , GL_COMPRESSED_RGBA_ARB, GL_UNSIGNED_BYTE};
-static textypeinfo_t textype_shadowmap              = {TEXTYPE_SHADOWMAP,4,4, 4.0f, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT24_ARB, GL_UNSIGNED_INT};
+static textypeinfo_t textype_shadowmap16            = {TEXTYPE_SHADOWMAP,2,2, 2.0f, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT16_ARB, GL_UNSIGNED_SHORT};
+static textypeinfo_t textype_shadowmap24            = {TEXTYPE_SHADOWMAP,4,4, 4.0f, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT24_ARB, GL_UNSIGNED_INT};
 
 typedef enum gltexturetype_e
 {
@@ -217,7 +218,7 @@ static textypeinfo_t *R_GetTexTypeInfo(textype_t textype, int flags)
                        case TEXTYPE_BGRA:
                                return &textype_bgra;
                        case TEXTYPE_SHADOWMAP:
-                               return &textype_shadowmap;
+                               return (flags & TEXF_LOWPRECISION) ? &textype_shadowmap16 : &textype_shadowmap24;
                        default:
                                Host_Error("R_GetTexTypeInfo: unknown texture format");
                                return NULL;
@@ -264,41 +265,6 @@ static void R_UpdateDynamicTexture(gltexture_t *glt) {
        }
 }
 
-static void R_UploadTexture(gltexture_t *t);
-
-static void R_PrecacheTexture(gltexture_t *glt)
-{
-       int precache;
-       precache = false;
-       if (glt->flags & TEXF_ALWAYSPRECACHE)
-               precache = true;
-       else if (r_precachetextures.integer >= 2)
-               precache = true;
-       else if (r_precachetextures.integer >= 1)
-               if (glt->flags & TEXF_PRECACHE)
-                       precache = true;
-
-       if (precache)
-               R_UploadTexture(glt);
-}
-
-int R_RealGetTexture(rtexture_t *rt)
-{
-       if (rt)
-       {
-               gltexture_t *glt;
-               glt = (gltexture_t *)rt;
-               if (glt->flags & GLTEXF_DYNAMIC)
-                       R_UpdateDynamicTexture(glt);
-               if (glt->flags & GLTEXF_UPLOAD)
-                       R_UploadTexture(glt);
-
-               return glt->texnum;
-       }
-       else
-               return 0;
-}
-
 void R_PurgeTexture(rtexture_t *rt)
 {
        if(rt && !(((gltexture_t*) rt)->flags & TEXF_PERSISTENT)) {
@@ -714,7 +680,7 @@ void R_MakeResizeBufferBigger(int size)
        }
 }
 
-static void GL_SetupTextureParameters(int flags, int texturetype)
+static void GL_SetupTextureParameters(int flags, textype_t textype, int texturetype)
 {
        int textureenum = gltexturetypeenums[texturetype];
        int wrapmode = ((flags & TEXF_CLAMP) && gl_support_clamptoedge) ? GL_CLAMP_TO_EDGE : GL_REPEAT;
@@ -780,17 +746,21 @@ static void GL_SetupTextureParameters(int flags, int texturetype)
                qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
        }
 
-       if (texturetype == TEXTYPE_SHADOWMAP)
+       if (textype == TEXTYPE_SHADOWMAP)
        {
-#if 1
-               qglTexParameteri(textureenum, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);CHECKGLERROR
-               qglTexParameteri(textureenum, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);CHECKGLERROR
-               qglTexParameteri(textureenum, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);CHECKGLERROR
-#else
-               qglTexParameteri(textureenum, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);CHECKGLERROR
-               qglTexParameteri(textureenum, GL_TEXTURE_COMPARE_FUNC_ARB, GL_ALWAYS);CHECKGLERROR
-               qglTexParameteri(textureenum, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);CHECKGLERROR
-#endif
+               if (gl_support_arb_shadow)
+               {
+                       if (flags & TEXF_COMPARE)
+                       {
+                               qglTexParameteri(textureenum, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);CHECKGLERROR
+                       }
+                       else
+                       {
+                               qglTexParameteri(textureenum, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);CHECKGLERROR
+                       }
+                       qglTexParameteri(textureenum, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);CHECKGLERROR
+               }
+               qglTexParameteri(textureenum, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);CHECKGLERROR
        }
 
        CHECKGLERROR
@@ -810,7 +780,7 @@ static void R_Upload(gltexture_t *glt, const unsigned char *data, int fragx, int
        qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
 
        // these are rounded up versions of the size to do better resampling
-       if (gl_support_arb_texture_non_power_of_two)
+       if (gl_support_arb_texture_non_power_of_two || glt->texturetype == GLTEXTURETYPE_RECTANGLE)
        {
                width = glt->inputwidth;
                height = glt->inputheight;
@@ -963,27 +933,38 @@ static void R_Upload(gltexture_t *glt, const unsigned char *data, int fragx, int
                        qglTexImage2D(GL_TEXTURE_RECTANGLE_ARB, mip++, glt->glinternalformat, width, height, 0, glt->glformat, glt->gltype, NULL);CHECKGLERROR
                        break;
                }
-               GL_SetupTextureParameters(glt->flags, glt->texturetype);
+               GL_SetupTextureParameters(glt->flags, glt->textype->textype, glt->texturetype);
        }
        qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
 }
 
-static void R_UploadTexture (gltexture_t *glt)
+int R_RealGetTexture(rtexture_t *rt)
 {
-       if (!(glt->flags & GLTEXF_UPLOAD))
-               return;
-
-       CHECKGLERROR
-       qglGenTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
-       R_Upload(glt, glt->inputtexels, 0, 0, 0, glt->inputwidth, glt->inputheight, glt->inputdepth);
-       if (glt->inputtexels)
+       if (rt)
        {
-               Mem_Free(glt->inputtexels);
-               glt->inputtexels = NULL;
-               glt->flags |= GLTEXF_DESTROYED;
+               gltexture_t *glt;
+               glt = (gltexture_t *)rt;
+               if (glt->flags & GLTEXF_DYNAMIC)
+                       R_UpdateDynamicTexture(glt);
+               if (glt->flags & GLTEXF_UPLOAD)
+               {
+                       CHECKGLERROR
+                       qglGenTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
+                       R_Upload(glt, glt->inputtexels, 0, 0, 0, glt->inputwidth, glt->inputheight, glt->inputdepth);
+                       if (glt->inputtexels)
+                       {
+                               Mem_Free(glt->inputtexels);
+                               glt->inputtexels = NULL;
+                               glt->flags |= GLTEXF_DESTROYED;
+                       }
+                       else if (glt->flags & GLTEXF_DESTROYED)
+                               Con_Printf("R_GetTexture: Texture %s already uploaded and destroyed.  Can not upload original image again.  Uploaded blank texture.\n", glt->identifier);
+               }
+
+               return glt->texnum;
        }
-       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);
+       else
+               return 0;
 }
 
 static rtexture_t *R_SetupTexture(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, int depth, int sides, int flags, textype_t textype, int texturetype, const unsigned char *data, const unsigned int *palette)
@@ -992,6 +973,7 @@ static rtexture_t *R_SetupTexture(rtexturepool_t *rtexturepool, const char *iden
        gltexture_t *glt;
        gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
        textypeinfo_t *texinfo;
+       int precache;
 
        if (cls.state == ca_dedicated)
                return NULL;
@@ -1089,16 +1071,45 @@ static rtexture_t *R_SetupTexture(rtexturepool_t *rtexturepool, const char *iden
        glt->updatecallback = NULL;
        glt->updatacallback_data = NULL;
 
-       if (data)
+       GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->inputwidth, glt->inputheight, glt->inputdepth, &glt->tilewidth, &glt->tileheight, &glt->tiledepth);
+
+       precache = false;
+       if (glt->flags & TEXF_ALWAYSPRECACHE)
+               precache = true;
+       else if (r_precachetextures.integer >= 2)
+               precache = true;
+       else if (r_precachetextures.integer >= 1)
+               if (glt->flags & TEXF_PRECACHE)
+                       precache = true;
+
+       if (precache)
        {
-               glt->inputtexels = (unsigned char *)Mem_Alloc(texturemempool, size);
-               memcpy(glt->inputtexels, data, size);
+               // immediate upload (most common case)
+               // data may be NULL (blank texture for dynamic rendering)
+               CHECKGLERROR
+               qglGenTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
+               R_Upload(glt, data, 0, 0, 0, glt->inputwidth, glt->inputheight, glt->inputdepth);
+       }
+       else if (data)
+       {
+               // deferred texture upload (menu graphics)
+               // optimize first if possible
+               if ((textype == TEXTYPE_BGRA || textype == TEXTYPE_RGBA) && glt->inputwidth * glt->inputheight * glt->inputdepth > glt->tilewidth * glt->tileheight * glt->tiledepth)
+               {
+                       glt->inputtexels = (unsigned char *)Mem_Alloc(texturemempool, glt->tilewidth*glt->tileheight*glt->tiledepth*glt->sides*glt->bytesperpixel);
+                       Image_Resample32(data, glt->inputwidth, glt->inputheight, glt->inputdepth, glt->inputtexels, glt->tilewidth, glt->tileheight, glt->tiledepth, r_lerpimages.integer);
+                       // change texture size accordingly
+                       glt->inputwidth = glt->tilewidth;
+                       glt->inputheight = glt->tileheight;
+                       glt->inputdepth = glt->tiledepth;
+                       GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->inputwidth, glt->inputheight, glt->inputdepth, &glt->tilewidth, &glt->tileheight, &glt->tiledepth);
+               }
+               else
+               {
+                       glt->inputtexels = (unsigned char *)Mem_Alloc(texturemempool, size);
+                       memcpy(glt->inputtexels, data, size);
+               }
        }
-       else
-               glt->inputtexels = NULL;
-
-       GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->inputwidth, glt->inputheight, glt->inputdepth, &glt->tilewidth, &glt->tileheight, &glt->tiledepth);
-       R_PrecacheTexture(glt);
 
        // texture converting and uploading can take a while, so make sure we're sending keepalives
        CL_KeepaliveMessage(false);
@@ -1126,24 +1137,31 @@ rtexture_t *R_LoadTextureCubeMap(rtexturepool_t *rtexturepool, const char *ident
        return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, flags, textype, GLTEXTURETYPE_CUBEMAP, data, palette);
 }
 
-rtexture_t *R_LoadTextureShadowMapRectangle(rtexturepool_t *rtexturepool, const char *identifier, int width, int height)
+static int R_ShadowMapTextureFlags(int precision, qboolean filter)
 {
-       return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, TEXF_ALWAYSPRECACHE | TEXF_FORCENEAREST | TEXF_CLAMP, TEXTYPE_SHADOWMAP, GLTEXTURETYPE_RECTANGLE, NULL, NULL);
+       int flags = TEXF_ALWAYSPRECACHE | TEXF_CLAMP;
+       if (filter)
+               flags |= TEXF_FORCELINEAR | TEXF_COMPARE;
+       else
+               flags |= TEXF_FORCENEAREST;
+       if (precision <= 16)
+               flags |= TEXF_LOWPRECISION;
+       return flags;
 }
 
-rtexture_t *R_LoadTextureShadowMapCube(rtexturepool_t *rtexturepool, const char *identifier, int width)
+rtexture_t *R_LoadTextureShadowMapRectangle(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, int precision, qboolean filter)
 {
-       return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, TEXF_ALWAYSPRECACHE | TEXF_FORCELINEAR | TEXF_CLAMP, TEXTYPE_SHADOWMAP, GLTEXTURETYPE_CUBEMAP, NULL, NULL);
+       return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, R_ShadowMapTextureFlags(precision, filter), TEXTYPE_SHADOWMAP, GLTEXTURETYPE_RECTANGLE, NULL, NULL);
 }
 
-rtexture_t *R_LoadTextureShadowMap2D(rtexturepool_t *rtexturepool, const char *identifier, int width, int height)
+rtexture_t *R_LoadTextureShadowMap2D(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, int precision, qboolean filter)
 {
-       return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, TEXF_ALWAYSPRECACHE | TEXF_FORCENEAREST | TEXF_CLAMP, TEXTYPE_SHADOWMAP, GLTEXTURETYPE_2D, NULL, NULL);
+       return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, R_ShadowMapTextureFlags(precision, filter), TEXTYPE_SHADOWMAP, GLTEXTURETYPE_2D, NULL, NULL);
 }
 
-rtexture_t *R_LoadTextureCubeProjection(rtexturepool_t *rtexturepool, const char *identifier)
+rtexture_t *R_LoadTextureShadowMapCube(rtexturepool_t *rtexturepool, const char *identifier, int width, int precision, qboolean filter)
 {
-       return R_SetupTexture(rtexturepool, identifier, 2, 2, 1, 6, TEXF_ALWAYSPRECACHE | TEXF_FORCELINEAR | TEXF_CLAMP, TEXTYPE_BGRA, GLTEXTURETYPE_CUBEMAP, NULL, NULL);
+    return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, R_ShadowMapTextureFlags(precision, filter), TEXTYPE_SHADOWMAP, GLTEXTURETYPE_CUBEMAP, NULL, NULL);
 }
 
 int R_TextureHasAlpha(rtexture_t *rt)
@@ -1168,13 +1186,12 @@ void R_UpdateTexture(rtexture_t *rt, const unsigned char *data, int x, int y, in
                Host_Error("R_UpdateTexture: no texture supplied");
        if (data == NULL)
                Host_Error("R_UpdateTexture: no data supplied");
-       glt = (gltexture_t *)rt;
 
        // we need it to be uploaded before we can update a part of it
-       if (glt->flags & GLTEXF_UPLOAD)
-               R_UploadTexture(glt);
+       R_RealGetTexture(rt);
 
        // update part of the texture
+       glt = (gltexture_t *)rt;
        R_Upload(glt, data, x, y, 0, width, height, 1);
 }