Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
7.1 KiB

  1. //========== Copyright (c) Valve Corporation, All rights reserved. ==========//
  2. // STATIC: "DEFERRED_SHADOWS" "0..1" [CONSOLE]
  3. // STATIC: "DEFERRED_SHADOWS" "0..0" [PC]
  4. // STATIC: "BLOBBY_SHADOWS" "0..1"
  5. #include "common_fog_ps_fxc.h"
  6. #define HDRTYPE HDR_TYPE_NONE
  7. #include "common_ps_fxc.h"
  8. const float4 g_ShadowColor : register( c1 );
  9. const float3 g_EyePos : register( c2 );
  10. const float4 g_FogParams : register( c3 );
  11. const float4 g_DeferredParams : register( c4 );
  12. #define g_MaxFalloffAmount g_DeferredParams.x
  13. #define g_vInvScreenSize g_DeferredParams.yz
  14. const float4x4 g_mInvViewProj : register( c5 );
  15. sampler ShadowSampler : register( s0 );
  16. sampler sDepth : register( s1 );
  17. #if DEFERRED_SHADOWS == 0
  18. //////////////////////////////////
  19. // NORMAL SHADOW SHADER
  20. //////////////////////////////////
  21. struct PS_INPUT
  22. {
  23. float4 worldPos_projPosZ : TEXCOORD0;
  24. float4 vFalloffParams : TEXCOORD1_centroid;
  25. float3 texCoord0_shadowAlpha : TEXCOORD2_centroid;
  26. #if !defined( _X360 )
  27. float4 texCoord1_2 : TEXCOORD3_centroid;
  28. float4 texCoord3_4 : TEXCOORD4;
  29. #endif
  30. };
  31. float4_color_return_type main( PS_INPUT i ) : COLOR
  32. {
  33. float shadowCoverage;
  34. #if BLOBBY_SHADOWS == 1
  35. {
  36. shadowCoverage = tex2D( ShadowSampler, i.texCoord0_shadowAlpha.xy ).a;
  37. }
  38. #elif !defined( _X360 )
  39. {
  40. float samples0;
  41. float4 samples1_4;
  42. samples0 = tex2D( ShadowSampler, i.texCoord0_shadowAlpha.xy ).a;
  43. samples1_4.x = tex2D( ShadowSampler, i.texCoord1_2.xy ).a;
  44. samples1_4.y = tex2D( ShadowSampler, i.texCoord1_2.wz ).a;
  45. samples1_4.z = tex2D( ShadowSampler, i.texCoord3_4.xy ).a;
  46. samples1_4.w = tex2D( ShadowSampler, i.texCoord3_4.wz ).a;
  47. // Interpolate between a bunch of jittered shadow samples.
  48. shadowCoverage = samples0 * 0.2 + dot( samples1_4, float4( 0.2, 0.2, 0.2, 0.2 ) );
  49. }
  50. #else
  51. {
  52. float samples0 = tex2D( ShadowSampler, i.texCoord0_shadowAlpha.xy ).a;
  53. float2 texCoord = i.texCoord0_shadowAlpha.xy;
  54. float4 samples1_4;
  55. asm {
  56. tfetch2D samples1_4.w___, texCoord.xy, ShadowSampler, OffsetX = -1.0, OffsetY = -1.0
  57. tfetch2D samples1_4._w__, texCoord.xy, ShadowSampler, OffsetX = 1.0, OffsetY = -1.0
  58. tfetch2D samples1_4.__w_, texCoord.xy, ShadowSampler, OffsetX = -1.0, OffsetY = 1.0
  59. tfetch2D samples1_4.___w, texCoord.xy, ShadowSampler, OffsetX = 1.0, OffsetY = 1.0
  60. };
  61. // Interpolate between a bunch of jittered shadow samples.
  62. shadowCoverage = samples0 * 0.2 + dot( samples1_4, float4( 0.2,0.2,0.2,0.2 ) );
  63. }
  64. #endif
  65. // compute "vertex" alpha
  66. // NOTE: 0 means black, non-zero adds towards white...
  67. float fVertAlpha = saturate( i.vFalloffParams.w * i.vFalloffParams.y + i.vFalloffParams.x ); // could pull the mad into the VS
  68. fVertAlpha = saturate( i.vFalloffParams.z + fVertAlpha * g_MaxFalloffAmount );
  69. //fVertAlpha = i.texCoord0_shadowAlpha.z;
  70. // To accomplish shadow fading, subtract vertex alpha from texture alpha
  71. shadowCoverage = saturate( shadowCoverage - fVertAlpha );
  72. // Blend between white and the constant color...
  73. // return lerp( 1.0-shadowCoverage, 1.0, g_ShadowColor );
  74. // this is equivalent, and saves an instruction
  75. float4 result = shadowCoverage*g_ShadowColor - shadowCoverage;
  76. result = 1.0 + result;
  77. float alpha = 1.0f;
  78. float fogFactor = CalcPixelFogFactor( PIXELFOGTYPE, g_FogParams, g_EyePos.xyz, i.worldPos_projPosZ.xyz, i.worldPos_projPosZ.w );
  79. // Apply fog here to compensate for our srcColor*dstColor alpha blend into already fogged pixels
  80. result.rgb = 1.0f - ( ( 1.0f - result.rgb ) * pow( ( 1.0f - fogFactor ), 4.0f ) );
  81. // Call FinalOutput without fog!
  82. return FinalOutput( float4( result.rgb, alpha ), fogFactor, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
  83. }
  84. #else // DEFERRED_SHADOWS == 1
  85. //////////////////////////////////
  86. // X360 DEFERRED SHADOW SHADER
  87. //////////////////////////////////
  88. struct PS_INPUT
  89. {
  90. float4 vTexCoordBiasScale : TEXCOORD0;
  91. float4 vProjToTex0 : TEXCOORD1_centroid;
  92. float4 vProjToTex1 : TEXCOORD2_centroid;
  93. float4 vProjToTex2 : TEXCOORD3_centroid;
  94. float4 vProjToTex3 : TEXCOORD4;
  95. float3 vFalloffParams : TEXCOORD5;
  96. #ifdef _PS3
  97. float4 vPos : TEXCOORD6;
  98. #else
  99. float2 vPos : VPOS;
  100. #endif
  101. };
  102. float4_color_return_type main( PS_INPUT i ) : COLOR
  103. {
  104. float4 vPosCS;
  105. vPosCS.xy = ( i.vPos.xy+float2(0.5, 0.5) ) * g_vInvScreenSize;
  106. vPosCS.z = 1.0 - tex2D( sDepth, vPosCS.xy );
  107. vPosCS.w = 1.0;
  108. vPosCS.xy = 2.0 * vPosCS.xy - 1.0;
  109. vPosCS.y = -vPosCS.y;
  110. //return float4((1-vPosCS.z).xxx, 1);
  111. float4 vPosTS;// = mul( vPosCS, g_mScreenToTexture );
  112. // Clip space to shadow texture space transform
  113. vPosTS.x = dot( vPosCS, i.vProjToTex0 );
  114. vPosTS.y = dot( vPosCS, i.vProjToTex1 );
  115. vPosTS.z = dot( vPosCS, i.vProjToTex2 );
  116. vPosTS.w = dot( vPosCS, i.vProjToTex3 );
  117. vPosTS /= vPosTS.w;
  118. // check if pixel is within shadow frustum, and early-out if it's not
  119. float2 vClamped = saturate( vPosTS.xy );
  120. vClamped -= vPosTS.xy;
  121. clip( 0.0001 - dot( vClamped, vClamped ) );
  122. // extract normal in texture space
  123. float3 vNormalTS = cross( ddx(vPosTS.xyz), ddy(vPosTS.xyz) );
  124. #if BLOBBY_SHADOWS == 0
  125. float2 vTexCoord = i.vTexCoordBiasScale.xy + i.vTexCoordBiasScale.zw * vPosTS.xy;
  126. #else
  127. float2 vTexCoord = vPosTS.xy;
  128. #endif
  129. float shadowCoverage = 0.0f;
  130. #if !defined( _X360 ) || ( BLOBBY_SHADOWS == 1 )
  131. {
  132. shadowCoverage = tex2D( ShadowSampler, vTexCoord ).a;
  133. }
  134. #else
  135. {
  136. float samples0 = tex2D( ShadowSampler, vTexCoord ).a;
  137. float4 samples1_4;
  138. asm {
  139. tfetch2D samples1_4.w___, vTexCoord.xy, ShadowSampler, OffsetX = -1.0, OffsetY = -1.0
  140. tfetch2D samples1_4._w__, vTexCoord.xy, ShadowSampler, OffsetX = 1.0, OffsetY = -1.0
  141. tfetch2D samples1_4.__w_, vTexCoord.xy, ShadowSampler, OffsetX = -1.0, OffsetY = 1.0
  142. tfetch2D samples1_4.___w, vTexCoord.xy, ShadowSampler, OffsetX = 1.0, OffsetY = 1.0
  143. };
  144. // Interpolate between a bunch of jittered shadow samples.
  145. shadowCoverage = samples0 * 0.2 + dot( samples1_4, float4( 0.2,0.2,0.2,0.2 ) );
  146. }
  147. #endif
  148. // compute "vertex" alpha
  149. // NOTE: 0 means black, non-zero adds towards white...
  150. float fVertAlpha = saturate( vPosTS.z * i.vFalloffParams.y + i.vFalloffParams.x ); // could pull the mad into the VS
  151. fVertAlpha = saturate( i.vFalloffParams.z + fVertAlpha * g_MaxFalloffAmount );
  152. // To accomplish shadow fading, subtract vertex alpha from texture alpha
  153. shadowCoverage = saturate( shadowCoverage - fVertAlpha );
  154. // mask out shadows on geometry facing away from the shadow direction
  155. shadowCoverage *= saturate(sign(vNormalTS.z));
  156. // TODO: Add fog
  157. // Blend between white and the constant color...
  158. // return lerp( 1.0-shadowCoverage, 1.0, g_ShadowColor );
  159. // this is equivalent, and saves an instruction
  160. float4 result = shadowCoverage*g_ShadowColor - shadowCoverage;
  161. result = 1.0 + result;
  162. float alpha = 1.0f;
  163. // TODO: Add support for fog
  164. float fogFactor = 0;
  165. /*
  166. // Apply fog here to compensate for our srcColor*dstColor alpha blend into already fogged pixels
  167. result.rgb = 1.0f - ( ( 1.0f - result.rgb ) * pow( ( 1.0f - fogFactor ), 4.0f ) );
  168. */
  169. //return float4( g_shadowColor.rgb, shadowCoverage );
  170. // Call FinalOutput without fog!
  171. return FinalOutput( float4( result.rgb, alpha ), fogFactor, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
  172. }
  173. #endif