From: havoc Date: Fri, 17 Sep 2010 14:19:14 +0000 (+0000) Subject: fix some bugs in the HLSL shader, mostly to do with r_shadow_deferred X-Git-Tag: xonotic-v0.1.0preview~230^2~13 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=30d2b84076b90be73d1838f1c396edcfd9989c69;p=xonotic%2Fdarkplaces.git fix some bugs in the HLSL shader, mostly to do with r_shadow_deferred change texture unit for shadowmaps to 15 rather than 11 because this fixes a conflict between r_shadow_deferred 1 and r_shadows 2 git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@10471 d7cf8633-e32d-0410-b094-e92efae38249 --- diff --git a/gl_rmain.c b/gl_rmain.c index 94438fc0..9f8ab240 100644 --- a/gl_rmain.c +++ b/gl_rmain.c @@ -1833,9 +1833,9 @@ const char *builtincgshaderstring = ")\n" "{\n" "// float3 temp = float3(Depth,Depth*(65536.0/255.0),Depth*(16777216.0/255.0));\n" -" float3 temp = float3(Depth,Depth*256.0,Depth*65536.0);\n" +" float4 temp = float4(Depth,Depth*256.0,Depth*65536.0,0.0);\n" " temp.yz -= floor(temp.yz);\n" -" gl_FragColor = float4(temp,0);\n" +" gl_FragColor = temp;\n" "// gl_FragColor = float4(Depth,0,0,0);\n" "}\n" "#endif\n" @@ -2468,7 +2468,7 @@ const char *builtincgshaderstring = "#endif\n" "out float3 VectorS : TEXCOORD5, // direction of S texcoord (sometimes crudely called tangent)\n" "out float3 VectorT : TEXCOORD6, // direction of T texcoord (sometimes crudely called binormal)\n" -"out float3 VectorR : TEXCOORD7 // direction of R texcoord (surface normal)\n" +"out float4 VectorR : TEXCOORD7 // direction of R texcoord (surface normal), Depth value\n" ")\n" "{\n" " TexCoordBoth = mul(TexMatrix, gl_MultiTexCoord0);\n" @@ -2491,8 +2491,9 @@ const char *builtincgshaderstring = "\n" " VectorS = mul(ModelViewMatrix, float4(gl_MultiTexCoord1.xyz, 0)).xyz;\n" " VectorT = mul(ModelViewMatrix, float4(gl_MultiTexCoord2.xyz, 0)).xyz;\n" -" VectorR = mul(ModelViewMatrix, float4(gl_MultiTexCoord3.xyz, 0)).xyz;\n" +" VectorR.xyz = mul(ModelViewMatrix, float4(gl_MultiTexCoord3.xyz, 0)).xyz;\n" " gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n" +" VectorR.w = gl_Position.z;\n" "}\n" "#endif // VERTEX_SHADER\n" "\n" @@ -2503,7 +2504,7 @@ const char *builtincgshaderstring = "float3 EyeVector : TEXCOORD2,\n" "float3 VectorS : TEXCOORD5, // direction of S texcoord (sometimes crudely called tangent)\n" "float3 VectorT : TEXCOORD6, // direction of T texcoord (sometimes crudely called binormal)\n" -"float3 VectorR : TEXCOORD7, // direction of R texcoord (surface normal)\n" +"float4 VectorR : TEXCOORD7, // direction of R texcoord (surface normal), Depth value\n" "uniform sampler Texture_Normal : register(s0),\n" "#ifdef USEALPHAKILL\n" "uniform sampler Texture_Color : register(s1),\n" @@ -2517,7 +2518,12 @@ const char *builtincgshaderstring = "uniform float OffsetMapping_Scale : register(c24),\n" "#endif\n" "uniform half SpecularPower : register(c36),\n" +"#ifdef HLSL\n" +"out float4 gl_FragData0 : COLOR0,\n" +"out float4 gl_FragData1 : COLOR1\n" +"#else\n" "out float4 gl_FragColor : COLOR\n" +"#endif\n" ")\n" "{\n" " float2 TexCoord = TexCoordBoth.xy;\n" @@ -2547,7 +2553,16 @@ const char *builtincgshaderstring = " float a = tex2D(Texture_Gloss, TexCoord).a;\n" "#endif\n" "\n" +"#ifdef HLSL\n" +" gl_FragData0 = float4(normalize(surfacenormal.x * VectorS + surfacenormal.y * VectorT + surfacenormal.z * VectorR.xyz) * 0.5 + float3(0.5, 0.5, 0.5), a);\n" +" float Depth = VectorR.w / 256.0;\n" +" float4 depthcolor = float4(Depth,Depth*65536.0/255.0,Depth*16777216.0/255.0,0.0);\n" +"// float4 depthcolor = float4(Depth,Depth*256.0,Depth*65536.0,0.0);\n" +" depthcolor.yz -= floor(depthcolor.yz);\n" +" gl_FragData1 = depthcolor;\n" +"#else\n" " gl_FragColor = float4(normalize(surfacenormal.x * VectorS + surfacenormal.y * VectorT + surfacenormal.z * VectorR) * 0.5 + float3(0.5, 0.5, 0.5), a);\n" +"#endif\n" "}\n" "#endif // FRAGMENT_SHADER\n" "#else // !MODE_DEFERREDGEOMETRY\n" @@ -2600,9 +2615,9 @@ const char *builtincgshaderstring = "\n" "#ifdef USESHADOWMAP2D\n" "# ifdef USESHADOWSAMPLER\n" -"uniform sampler Texture_ShadowMap2D : register(s11),\n" +"uniform sampler Texture_ShadowMap2D : register(s15),\n" "# else\n" -"uniform sampler Texture_ShadowMap2D : register(s11),\n" +"uniform sampler Texture_ShadowMap2D : register(s15),\n" "# endif\n" "#endif\n" "\n" @@ -2623,7 +2638,11 @@ const char *builtincgshaderstring = " float2 ScreenTexCoord = Pixel * PixelToScreenTexCoord;\n" " //ScreenTexCoord.y = ScreenTexCoord.y * -1 + 1; // Cg is opposite?\n" " float3 position;\n" +"#ifdef HLSL\n" +" position.z = texDepth2D(Texture_ScreenDepth, ScreenTexCoord) * 256.0;\n" +"#else\n" " position.z = ScreenToDepth.y / (texDepth2D(Texture_ScreenDepth, ScreenTexCoord) + ScreenToDepth.x);\n" +"#endif\n" " position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z);\n" " // decode viewspace pixel normal\n" " half4 normalmap = half4(tex2D(Texture_ScreenNormalMap, ScreenTexCoord));\n" @@ -2643,19 +2662,19 @@ const char *builtincgshaderstring = " // calculate directional shading\n" " float3 eyevector = position * -1.0;\n" "# ifdef USEEXACTSPECULARMATH\n" -" half specular = pow(half(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), SpecularPower * normalmap.a);\n" +" half specular = half(pow(half(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), SpecularPower * normalmap.a));\n" "# else\n" " half3 specularnormal = half3(normalize(lightnormal + half3(normalize(eyevector))));\n" -" half specular = pow(half(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower * normalmap.a);\n" +" half specular = half(pow(half(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower * normalmap.a));\n" "# endif\n" "#endif\n" "\n" "#if defined(USESHADOWMAP2D)\n" -" fade *= ShadowMapCompare(CubeVector, Texture_ShadowMap2D, ShadowMap_Parameters, ShadowMap_TextureScale\n" +" fade *= half(ShadowMapCompare(CubeVector, Texture_ShadowMap2D, ShadowMap_Parameters, ShadowMap_TextureScale\n" "#ifdef USESHADOWMAPVSDCT\n" ", Texture_CubeProjection\n" "#endif\n" -" );\n" +" ));\n" "#endif\n" "\n" "#ifdef USEDIFFUSE\n" @@ -2904,6 +2923,8 @@ const char *builtincgshaderstring = "uniform sampler Texture_ScreenNormalMap : register(s14),\n" "#endif\n" "#ifdef USEDEFERREDLIGHTMAP\n" +"uniform sampler Texture_ScreenDepth : register(s13),\n" +"uniform sampler Texture_ScreenNormalMap : register(s14),\n" "uniform sampler Texture_ScreenDiffuse : register(s11),\n" "uniform sampler Texture_ScreenSpecular : register(s12),\n" "#endif\n" @@ -2963,9 +2984,9 @@ const char *builtincgshaderstring = "\n" "#ifdef USESHADOWMAP2D\n" "# ifdef USESHADOWSAMPLER\n" -"uniform sampler Texture_ShadowMap2D : register(s11),\n" +"uniform sampler Texture_ShadowMap2D : register(s15),\n" "# else\n" -"uniform sampler Texture_ShadowMap2D : register(s11),\n" +"uniform sampler Texture_ShadowMap2D : register(s15),\n" "# endif\n" "#endif\n" "\n" @@ -3168,13 +3189,15 @@ const char *builtincgshaderstring = "#endif\n" "\n" "#ifdef USESHADOWMAPORTHO\n" -" color.rgb *= ShadowMapCompare(ShadowMapTC, Texture_ShadowMap2D, ShadowMap_Parameters, ShadowMap_TextureScale);\n" +" color.rgb *= half(ShadowMapCompare(ShadowMapTC, Texture_ShadowMap2D, ShadowMap_Parameters, ShadowMap_TextureScale));\n" "#endif\n" "\n" "#ifdef USEDEFERREDLIGHTMAP\n" " float2 ScreenTexCoord = Pixel * PixelToScreenTexCoord;\n" " color.rgb += diffusetex * half3(tex2D(Texture_ScreenDiffuse, ScreenTexCoord).rgb) * DeferredMod_Diffuse;\n" " color.rgb += glosstex.rgb * half3(tex2D(Texture_ScreenSpecular, ScreenTexCoord).rgb) * DeferredMod_Specular;\n" +"// color.rgb = half3(tex2D(Texture_ScreenDepth, ScreenTexCoord).rgb);\n" +"// color.r = half(texDepth2D(Texture_ScreenDepth, ScreenTexCoord)) * 1.0;\n" "#endif\n" "\n" "#ifdef USEGLOW\n" @@ -3738,7 +3761,7 @@ static void R_GLSL_CompilePermutation(r_glsl_permutation_t *p, unsigned int mode if (p->loc_Texture_Cube >= 0) qglUniform1iARB(p->loc_Texture_Cube , GL20TU_CUBE); if (p->loc_Texture_Refraction >= 0) qglUniform1iARB(p->loc_Texture_Refraction , GL20TU_REFRACTION); if (p->loc_Texture_Reflection >= 0) qglUniform1iARB(p->loc_Texture_Reflection , GL20TU_REFLECTION); - if (p->loc_Texture_ShadowMap2D >= 0) qglUniform1iARB(p->loc_Texture_ShadowMap2D , permutation & SHADERPERMUTATION_SHADOWMAPORTHO ? GL20TU_SHADOWMAPORTHO2D : GL20TU_SHADOWMAP2D); + if (p->loc_Texture_ShadowMap2D >= 0) qglUniform1iARB(p->loc_Texture_ShadowMap2D , GL20TU_SHADOWMAP2D); if (p->loc_Texture_CubeProjection >= 0) qglUniform1iARB(p->loc_Texture_CubeProjection , GL20TU_CUBEPROJECTION); if (p->loc_Texture_ScreenDepth >= 0) qglUniform1iARB(p->loc_Texture_ScreenDepth , GL20TU_SCREENDEPTH); if (p->loc_Texture_ScreenNormalMap >= 0) qglUniform1iARB(p->loc_Texture_ScreenNormalMap, GL20TU_SCREENNORMALMAP); @@ -4485,7 +4508,7 @@ static void R_HLSL_CacheShader(r_hlsl_permutation_t *p, const char *cachename, c vsbin = (DWORD *)FS_LoadFile(va("%s.vsbin", cachename), r_main_mempool, true, &vsbinsize); psbin = (DWORD *)FS_LoadFile(va("%s.psbin", cachename), r_main_mempool, true, &psbinsize); } - if (debugshader || (!vsbin && vertstring) || (!psbin && fragstring)) + if ((!vsbin && vertstring) || (!psbin && fragstring)) { const char* dllnames_d3dx9 [] = { @@ -4578,14 +4601,11 @@ static void R_HLSL_CacheShader(r_hlsl_permutation_t *p, const char *cachename, c else Con_Printf("Unable to compile shader - D3DXCompileShader function not found\n"); } - if (vsbin) + if (vsbin && psbin) { vsresult = IDirect3DDevice9_CreateVertexShader(vid_d3d9dev, vsbin, &p->vertexshader); if (FAILED(vsresult)) Con_Printf("HLSL CreateVertexShader failed for %s (hresult = %8x)\n", cachename, vsresult); - } - if (psbin) - { psresult = IDirect3DDevice9_CreatePixelShader(vid_d3d9dev, psbin, &p->pixelshader); if (FAILED(psresult)) Con_Printf("HLSL CreatePixelShader failed for %s (hresult = %8x)\n", cachename, psresult); @@ -5066,6 +5086,7 @@ extern int r_shadow_prepass_width; extern int r_shadow_prepass_height; extern rtexture_t *r_shadow_prepassgeometrydepthtexture; extern rtexture_t *r_shadow_prepassgeometrynormalmaptexture; +extern rtexture_t *r_shadow_prepassgeometrydepthcolortexture; extern rtexture_t *r_shadow_prepasslightingdiffusetexture; extern rtexture_t *r_shadow_prepasslightingspeculartexture; extern cvar_t gl_mesh_separatearrays; @@ -5636,7 +5657,7 @@ void R_SetupShader_Surface(const vec3_t lightcolorbase, qboolean modellighting, if (permutation & SHADERPERMUTATION_DEFERREDLIGHTMAP ) R_Mesh_TexBind(GL20TU_SCREENSPECULAR , r_shadow_prepasslightingspeculartexture ); if (rsurface.rtlight || (r_shadow_usingshadowmaportho && !(rsurface.ent_flags & RENDER_NOSELFSHADOW))) { - R_Mesh_TexBind((permutation & SHADERPERMUTATION_SHADOWMAPORTHO) ? GL20TU_SHADOWMAPORTHO2D : GL20TU_SHADOWMAP2D, (permutation & SHADERPERMUTATION_SHADOWSAMPLER) ? r_shadow_shadowmap2dtexture : r_shadow_shadowmap2dcolortexture); + R_Mesh_TexBind(GL20TU_SHADOWMAP2D, r_shadow_shadowmap2dcolortexture); if (rsurface.rtlight) { if (permutation & SHADERPERMUTATION_CUBEFILTER ) R_Mesh_TexBind(GL20TU_CUBE , rsurface.rtlight->currentcubemap ); @@ -5792,7 +5813,7 @@ void R_SetupShader_Surface(const vec3_t lightcolorbase, qboolean modellighting, if (r_glsl_permutation->loc_Texture_ScreenSpecular >= 0) R_Mesh_TexBind(GL20TU_SCREENSPECULAR , r_shadow_prepasslightingspeculartexture ); if (rsurface.rtlight || (r_shadow_usingshadowmaportho && !(rsurface.ent_flags & RENDER_NOSELFSHADOW))) { - if (r_glsl_permutation->loc_Texture_ShadowMap2D >= 0) R_Mesh_TexBind(r_shadow_usingshadowmaportho ? GL20TU_SHADOWMAPORTHO2D : GL20TU_SHADOWMAP2D, r_shadow_shadowmap2dtexture ); + if (r_glsl_permutation->loc_Texture_ShadowMap2D >= 0) R_Mesh_TexBind(GL20TU_SHADOWMAP2D, r_shadow_shadowmap2dtexture ); if (rsurface.rtlight) { if (r_glsl_permutation->loc_Texture_Cube >= 0) R_Mesh_TexBind(GL20TU_CUBE , rsurface.rtlight->currentcubemap ); @@ -6041,10 +6062,10 @@ void R_SetupShader_DeferredLight(const rtlight_t *rtlight) hlslPSSetParameter2f(D3DPSREGISTER_PixelToScreenTexCoord, 1.0f/vid.width, 1.0/vid.height); R_Mesh_TexBind(GL20TU_ATTENUATION , r_shadow_attenuationgradienttexture ); - R_Mesh_TexBind(GL20TU_SCREENDEPTH , r_shadow_prepassgeometrydepthtexture ); + R_Mesh_TexBind(GL20TU_SCREENDEPTH , r_shadow_prepassgeometrydepthcolortexture ); R_Mesh_TexBind(GL20TU_SCREENNORMALMAP , r_shadow_prepassgeometrynormalmaptexture ); R_Mesh_TexBind(GL20TU_CUBE , rsurface.rtlight->currentcubemap ); - R_Mesh_TexBind(GL20TU_SHADOWMAP2D , r_shadow_shadowmap2dtexture ); + R_Mesh_TexBind(GL20TU_SHADOWMAP2D , r_shadow_shadowmap2dcolortexture ); R_Mesh_TexBind(GL20TU_CUBEPROJECTION , r_shadow_shadowmapvsdcttexture ); #endif break; diff --git a/r_shadow.c b/r_shadow.c index 1fc6410b..45fe5c9f 100644 --- a/r_shadow.c +++ b/r_shadow.c @@ -254,6 +254,7 @@ GLuint r_shadow_prepasslightingfbo; int r_shadow_prepass_width; int r_shadow_prepass_height; rtexture_t *r_shadow_prepassgeometrydepthtexture; +rtexture_t *r_shadow_prepassgeometrydepthcolortexture; rtexture_t *r_shadow_prepassgeometrynormalmaptexture; rtexture_t *r_shadow_prepasslightingdiffusetexture; rtexture_t *r_shadow_prepasslightingspeculartexture; @@ -3851,6 +3852,10 @@ static void R_Shadow_FreeDeferred(void) R_FreeTexture(r_shadow_prepassgeometrydepthtexture); r_shadow_prepassgeometrydepthtexture = NULL; + if (r_shadow_prepassgeometrydepthcolortexture) + R_FreeTexture(r_shadow_prepassgeometrydepthcolortexture); + r_shadow_prepassgeometrydepthcolortexture = NULL; + if (r_shadow_prepassgeometrynormalmaptexture) R_FreeTexture(r_shadow_prepassgeometrynormalmaptexture); r_shadow_prepassgeometrynormalmaptexture = NULL; @@ -3882,9 +3887,9 @@ void R_Shadow_DrawPrepass(void) GL_BlendFunc(GL_ONE, GL_ZERO); GL_Color(1,1,1,1); GL_DepthTest(true); - R_Mesh_SetRenderTargets(r_shadow_prepassgeometryfbo, r_shadow_prepassgeometrydepthtexture, r_shadow_prepassgeometrynormalmaptexture, NULL, NULL, NULL); + R_Mesh_SetRenderTargets(r_shadow_prepassgeometryfbo, r_shadow_prepassgeometrydepthtexture, r_shadow_prepassgeometrynormalmaptexture, r_shadow_prepassgeometrydepthcolortexture, NULL, NULL); Vector4Set(clearcolor, 0.5f,0.5f,0.5f,1.0f); - GL_Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, clearcolor, 1.0f, 0); + GL_Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, clearcolor, 1.0f, 0); if (r_timereport_active) R_TimeReport("prepasscleargeom"); @@ -3992,13 +3997,21 @@ void R_Shadow_PrepareLights(void) r_shadow_prepass_width = vid.width; r_shadow_prepass_height = vid.height; r_shadow_prepassgeometrydepthtexture = R_LoadTextureShadowMap2D(r_shadow_texturepool, "prepassgeometrydepthmap", vid.width, vid.height, 24, false); + switch (vid.renderpath) + { + case RENDERPATH_D3D9: + r_shadow_prepassgeometrydepthcolortexture = R_LoadTexture2D(r_shadow_texturepool, "prepassgeometrydepthcolormap", vid.width, vid.height, NULL, TEXTYPE_COLORBUFFER, TEXF_RENDERTARGET | TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCENEAREST, -1, NULL); + break; + default: + break; + } r_shadow_prepassgeometrynormalmaptexture = R_LoadTexture2D(r_shadow_texturepool, "prepassgeometrynormalmap", vid.width, vid.height, NULL, TEXTYPE_COLORBUFFER, TEXF_RENDERTARGET | TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCENEAREST, -1, NULL); r_shadow_prepasslightingdiffusetexture = R_LoadTexture2D(r_shadow_texturepool, "prepasslightingdiffuse", vid.width, vid.height, NULL, TEXTYPE_COLORBUFFER, TEXF_RENDERTARGET | TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCENEAREST, -1, NULL); r_shadow_prepasslightingspeculartexture = R_LoadTexture2D(r_shadow_texturepool, "prepasslightingspecular", vid.width, vid.height, NULL, TEXTYPE_COLORBUFFER, TEXF_RENDERTARGET | TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCENEAREST, -1, NULL); // set up the geometry pass fbo (depth + normalmap) r_shadow_prepassgeometryfbo = R_Mesh_CreateFramebufferObject(r_shadow_prepassgeometrydepthtexture, r_shadow_prepassgeometrynormalmaptexture, NULL, NULL, NULL); - R_Mesh_SetRenderTargets(r_shadow_prepassgeometryfbo, r_shadow_prepassgeometrydepthtexture, r_shadow_prepassgeometrynormalmaptexture, NULL, NULL, NULL); + R_Mesh_SetRenderTargets(r_shadow_prepassgeometryfbo, r_shadow_prepassgeometrydepthtexture, r_shadow_prepassgeometrynormalmaptexture, r_shadow_prepassgeometrydepthcolortexture, NULL, NULL); // render depth into one texture and normalmap into the other if (qglDrawBuffersARB) { diff --git a/render.h b/render.h index 00a93ed9..7a5f8466 100644 --- a/render.h +++ b/render.h @@ -500,11 +500,8 @@ typedef enum gl20_texunit_e // conflicts with lightmap/deluxemap GL20TU_ATTENUATION = 9, GL20TU_CUBE = 10, - GL20TU_SHADOWMAP2D = 11, + GL20TU_SHADOWMAP2D = 15, GL20TU_CUBEPROJECTION = 12, - // orthographic-projection shadowmapping - GL20TU_SHADOWMAPORTHORECT = 15, - GL20TU_SHADOWMAPORTHO2D = 15, // rtlight prepass data (screenspace depth and normalmap) GL20TU_SCREENDEPTH = 13, GL20TU_SCREENNORMALMAP = 14,