From 13b62e43b4a3050bb95008a40e8f40ddf59c5797 Mon Sep 17 00:00:00 2001 From: eihrul Date: Thu, 1 Oct 2009 17:30:00 +0000 Subject: [PATCH] avoid unnecessary texture offset scaling in shadowmap lookups git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@9273 d7cf8633-e32d-0410-b094-e92efae38249 --- gl_rmain.c | 69 +++++++++++++++++++++++++----------------------------- r_shadow.c | 4 +--- 2 files changed, 33 insertions(+), 40 deletions(-) diff --git a/gl_rmain.c b/gl_rmain.c index 2d101390..9c9b8e4a 100644 --- a/gl_rmain.c +++ b/gl_rmain.c @@ -460,12 +460,9 @@ static const char *builtinshaderstring = "# endif\n" "# ifdef GL_ARB_texture_gather\n" "# extension GL_ARB_texture_gather : enable\n" -"# define USETEXTUREGATHER\n" "# else\n" "# ifdef GL_AMD_texture_texture4\n" "# extension GL_AMD_texture_texture4 : enable\n" -"# define USETEXTUREGATHER\n" -"# define textureGather texture4\n" "# endif\n" "# endif\n" "#endif\n" @@ -910,7 +907,7 @@ static const char *builtinshaderstring = "#endif // USEOFFSETMAPPING\n" "\n" "#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE)\n" -"uniform vec4 ShadowMap_TextureScale;\n" +"uniform vec2 ShadowMap_TextureScale;\n" "uniform vec4 ShadowMap_Parameters;\n" "#endif\n" "\n" @@ -956,8 +953,8 @@ static const char *builtinshaderstring = " vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma;\n" " stc.xy += offset * ShadowMap_Parameters.y;\n" " stc.z += ShadowMap_Parameters.z;\n" -"# ifndef USESHADOWMAPRECT\n" -" stc.xy *= ShadowMap_TextureScale.xy;\n" +"# if showshadowmap\n" +" stc.xy *= ShadowMap_TextureScale;\n" "# endif\n" " return stc;\n" "# else\n" @@ -966,8 +963,8 @@ static const char *builtinshaderstring = " vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma;\n" " stc.xy += proj.zw * ShadowMap_Parameters.y;\n" " stc.z += ShadowMap_Parameters.z;\n" -"# ifndef USESHADOWMAPRECT\n" -" stc.xy *= ShadowMap_TextureScale.xy;\n" +"# if showshadowmap\n" +" stc.xy *= ShadowMap_TextureScale;\n" "# endif\n" " return stc;\n" "# endif\n" @@ -1035,35 +1032,38 @@ static const char *builtinshaderstring = "\n" "# ifdef USESHADOWSAMPLER\n" "# ifdef USESHADOWMAPPCF\n" -"# ifdef GL_EXT_gpu_shader4\n" -"# define texval(x, y) shadow2DOffset(Texture_ShadowMap2D, shadowmaptc, ivec2(x, y)).r\n" -"# else\n" -"# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy + vec2(x, y)*ShadowMap_TextureScale.xy, shadowmaptc.z)).r \n" -"# endif\n" +"# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r \n" +" vec2 center = shadowmaptc.xy*ShadowMap_TextureScale;\n" " f = dot(vec4(0.25), vec4(texval(-0.4, 1.0), texval(-1.0, -0.4), texval(0.4, -1.0), texval(1.0, 0.4)));\n" "# else\n" -" f = shadow2D(Texture_ShadowMap2D, shadowmaptc).r;\n" +" f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r;\n" "# endif\n" "# else\n" "# ifdef USESHADOWMAPPCF\n" -"# ifdef USETEXTUREGATHER\n" -" vec2 center = shadowmaptc.xy*ShadowMap_TextureScale.zw - 0.5, offset = fract(center);\n" -" vec4 group1 = step(shadowmaptc.z, textureGather(Texture_ShadowMap2D, (center + vec2(-1.0, -1.0))*ShadowMap_TextureScale.xy));\n" -" vec4 group2 = step(shadowmaptc.z, textureGather(Texture_ShadowMap2D, (center + vec2( 1.0, -1.0))*ShadowMap_TextureScale.xy));\n" -" vec4 group3 = step(shadowmaptc.z, textureGather(Texture_ShadowMap2D, (center + vec2(-1.0, 1.0))*ShadowMap_TextureScale.xy));\n" -" vec4 group4 = step(shadowmaptc.z, textureGather(Texture_ShadowMap2D, (center + vec2( 1.0, 1.0))*ShadowMap_TextureScale.xy));\n" +"# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4)\n" +"# ifdef GL_ARB_texture_gather\n" +"# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y))\n" +"# else\n" +"# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale)\n" +"# endif\n" +" vec2 center = shadowmaptc.xy - 0.5, offset = fract(center);\n" +" center *= ShadowMap_TextureScale;\n" +" vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0));\n" +" vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0));\n" +" vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0));\n" +" vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0));\n" " vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) +\n" " mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y);\n" " f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));\n" "# else\n" -"# if USESHADOWMAPPCF > 1\n" -"# ifdef GL_EXT_gpu_shader4\n" -" vec2 center = shadowmaptc.xy - 0.5*ShadowMap_TextureScale.xy, offset = fract(center*ShadowMap_TextureScale.zw);\n" -"# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r\n" -"# else\n" -" vec2 center = shadowmaptc.xy*ShadowMap_TextureScale.zw - 0.5, offset = fract(center);\n" -"# define texval(x, y) texture2D(Texture_ShadowMap2D, (center + vec2(x, y))*ShadowMap_TextureScale.xy).r \n" -"# endif\n" +"# ifdef GL_EXT_gpu_shader4\n" +"# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r\n" +"# else\n" +"# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r \n" +"# endif\n" +"# if USESHADOWMAPPCF > 1\n" +" vec2 center = shadowmaptc.xy - 0.5, offset = fract(center);\n" +" center *= ShadowMap_TextureScale;\n" " vec4 row1 = step(shadowmaptc.z, vec4(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0), texval( 2.0, -1.0)));\n" " vec4 row2 = step(shadowmaptc.z, vec4(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0), texval( 2.0, 0.0)));\n" " vec4 row3 = step(shadowmaptc.z, vec4(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0), texval( 2.0, 1.0)));\n" @@ -1071,12 +1071,7 @@ static const char *builtinshaderstring = " vec4 cols = row2 + row3 + mix(row1, row4, offset.y);\n" " f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));\n" "# else\n" -"# ifdef GL_EXT_gpu_shader4\n" -"# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, shadowmaptc.xy, ivec2(x, y)).r\n" -"# else\n" -"# define texval(x, y) texture2D(Texture_ShadowMap2D, shadowmaptc.xy + vec2(x, y)*ShadowMap_TextureScale.xy).r \n" -"# endif\n" -" vec2 offset = fract(shadowmaptc.xy*ShadowMap_TextureScale.zw);\n" +" vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy);\n" " vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0)));\n" " vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0)));\n" " vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0)));\n" @@ -1085,7 +1080,7 @@ static const char *builtinshaderstring = "# endif\n" "# endif\n" "# else\n" -" f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy).r);\n" +" f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r);\n" "# endif\n" "# endif\n" " return f;\n" @@ -2012,7 +2007,7 @@ extern rtexture_t *r_shadow_attenuation3dtexture; extern qboolean r_shadow_usingshadowmaprect; extern qboolean r_shadow_usingshadowmapcube; extern qboolean r_shadow_usingshadowmap2d; -extern float r_shadow_shadowmap_texturescale[4]; +extern float r_shadow_shadowmap_texturescale[2]; extern float r_shadow_shadowmap_parameters[4]; extern qboolean r_shadow_shadowmapvsdct; extern qboolean r_shadow_shadowmapsampler; @@ -2197,7 +2192,7 @@ void R_SetupSurfaceShader(const vec3_t lightcolorbase, qboolean modellighting, f // additive passes are only darkened by fog, not tinted if (r_glsl_permutation->loc_FogColor >= 0) qglUniform3fARB(r_glsl_permutation->loc_FogColor, 0, 0, 0); - if (r_glsl_permutation->loc_ShadowMap_TextureScale >= 0) qglUniform4fARB(r_glsl_permutation->loc_ShadowMap_TextureScale, r_shadow_shadowmap_texturescale[0], r_shadow_shadowmap_texturescale[1], r_shadow_shadowmap_texturescale[2], r_shadow_shadowmap_texturescale[3]); + if (r_glsl_permutation->loc_ShadowMap_TextureScale >= 0) qglUniform2fARB(r_glsl_permutation->loc_ShadowMap_TextureScale, r_shadow_shadowmap_texturescale[0], r_shadow_shadowmap_texturescale[1]); if (r_glsl_permutation->loc_ShadowMap_Parameters >= 0) qglUniform4fARB(r_glsl_permutation->loc_ShadowMap_Parameters, r_shadow_shadowmap_parameters[0], r_shadow_shadowmap_parameters[1], r_shadow_shadowmap_parameters[2], r_shadow_shadowmap_parameters[3]); } else diff --git a/r_shadow.c b/r_shadow.c index a6f99ec0..7a3cf131 100644 --- a/r_shadow.c +++ b/r_shadow.c @@ -171,7 +171,7 @@ r_shadow_rendermode_t r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_N qboolean r_shadow_usingshadowmaprect; qboolean r_shadow_usingshadowmap2d; qboolean r_shadow_usingshadowmapcube; -float r_shadow_shadowmap_texturescale[4]; +float r_shadow_shadowmap_texturescale[2]; float r_shadow_shadowmap_parameters[4]; int r_shadow_drawbuffer; int r_shadow_readbuffer; @@ -1697,8 +1697,6 @@ void R_Shadow_RenderMode_ShadowMap(int side, qboolean clear, int size) r_shadow_shadowmap_parameters[1] = 1.0f; r_shadow_rendermode = R_SHADOW_RENDERMODE_SHADOWMAPCUBESIDE; } - r_shadow_shadowmap_texturescale[2] = 1.0f / r_shadow_shadowmap_texturescale[0]; - r_shadow_shadowmap_texturescale[3] = 1.0f / r_shadow_shadowmap_texturescale[1]; CHECKGLERROR R_SetViewport(&viewport); GL_PolygonOffset(0, 0); -- 2.39.2