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.

302 lines
9.6 KiB

  1. //========== Copyright (c) Valve Corporation, All rights reserved. ==========//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. // vertexshaders that pair with this pixel shader:
  7. // lightmappedgeneric_flashlight_vs20
  8. // vertexlitgeneric_flashlight_vs20
  9. // STATIC: "SFM" "0..0" [ps20] [ps20b] [PC]
  10. // STATIC: "SFM" "0..1" [ps30] [PC]
  11. // STATIC: "SFM" "0..0" [CONSOLE]
  12. // STATIC: "NORMALMAP" "0..2"
  13. // STATIC: "NORMALMAP2" "0..1"
  14. // STATIC: "WORLDVERTEXTRANSITION" "0..1"
  15. // STATIC: "SEAMLESS" "0..1"
  16. // STATIC: "DETAILTEXTURE" "0..1"
  17. // STATIC: "DETAIL_BLEND_MODE" "0..11"
  18. // STATIC: "FLASHLIGHTDEPTHFILTERMODE" "0..3" [ps20b] [PC]
  19. // STATIC: "FLASHLIGHTDEPTHFILTERMODE" "0..2" [ps30] [PC]
  20. // STATIC: "FLASHLIGHTDEPTHFILTERMODE" "0..0" [ps20b] [CONSOLE]
  21. // DYNAMIC: "FLASHLIGHTSHADOWS" "0..1" [ps20b] [ps30]
  22. // DYNAMIC: "UBERLIGHT" "0..1" [ps30] [PC]
  23. // SKIP: ( $SFM == 0 ) && ( $UBERLIGHT == 1 )
  24. // SKIP: !$WORLDVERTEXTRANSITION && $NORMALMAP2
  25. // SKIP: !$NORMALMAP && $NORMALMAP2
  26. // SKIP: !$DETAILTEXTURE && ( $DETAIL_BLEND_MODE != 0 )
  27. #include "common_fog_ps_supportsvertexfog_fxc.h"
  28. #include "shader_constant_register_map.h"
  29. #include "common_flashlight_fxc.h"
  30. #include "common_lightmappedgeneric_fxc.h"
  31. const float4 g_DetailConstants : register( c0 );
  32. const float3 g_DiffuseModulation : register( PSREG_DIFFUSE_MODULATION );
  33. const float4 g_vShadowTweaks : register( PSREG_ENVMAP_TINT__SHADOW_TWEAKS );
  34. const float4 g_FogParams : register( PSREG_FOG_PARAMS );
  35. const float4 g_EyePos : register( PSREG_EYEPOS_SPEC_EXPONENT );
  36. const float4 g_FlashlightAttenuation : register( PSREG_FLASHLIGHT_ATTENUATION );
  37. #if !defined( SHADER_MODEL_PS_2_0 )
  38. const float4 g_FlashlightPos : register( PSREG_FLASHLIGHT_POSITION_RIM_BOOST );
  39. #else
  40. // Lock flashlight pos to eye pos in ps_2_0
  41. #define g_FlashlightPos g_EyePos
  42. #endif
  43. #if ( UBERLIGHT )
  44. const float3 g_vSmoothEdge0 : register( PSREG_UBERLIGHT_SMOOTH_EDGE_0 );
  45. const float3 g_vSmoothEdge1 : register( PSREG_UBERLIGHT_SMOOTH_EDGE_1 );
  46. const float3 g_vSmoothOneOverWidth : register( PSREG_UBERLIGHT_SMOOTH_EDGE_OOW );
  47. const float4 g_vShearRound : register( PSREG_UBERLIGHT_SHEAR_ROUND );
  48. const float4 g_aAbB : register( PSREG_UBERLIGHT_AABB );
  49. const float4x4 g_FlashlightWorldToLight : register( PSREG_UBERLIGHT_WORLD_TO_LIGHT );
  50. #endif
  51. sampler SpotSampler : register( s0 );
  52. sampler BaseTextureSampler : register( s1 );
  53. samplerCUBE NormalizingCubemapSampler : register( s2 );
  54. // use a normalizing cube map here if we aren't normal mapping
  55. sampler BumpMapSampler : register( s3 );
  56. sampler BaseTextureSampler2 : register( s4 );
  57. #ifdef WORLDVERTEXTRANSITION
  58. sampler NormalMap2Sampler : register( s6 );
  59. #endif
  60. #if DETAILTEXTURE
  61. sampler DetailSampler : register( s8 );
  62. #endif
  63. #if FLASHLIGHTSHADOWS && ( defined( SHADER_MODEL_PS_2_B ) || defined( SHADER_MODEL_PS_3_0 ) )
  64. sampler RandomRotationSampler : register( s5 ); // Random rotation sampler
  65. sampler FlashlightDepthSampler : register( s7 );
  66. #endif
  67. struct PS_INPUT
  68. {
  69. float4 spotTexCoord : TEXCOORD0;
  70. #if SEAMLESS
  71. float3 SeamlessTexCoord : TEXCOORD1;
  72. #else
  73. float2 baseTexCoord : TEXCOORD1;
  74. #endif
  75. #if NORMALMAP
  76. float3 tangentPosToLightVector : TEXCOORD2;
  77. float2 normalMapTexCoord : TEXCOORD3;
  78. #else
  79. float3 worldPosToLightVector : TEXCOORD2;
  80. float3 normal : TEXCOORD3;
  81. #endif
  82. float2 detailCoords : TEXCOORD4;
  83. float4 worldPos_worldTransition : TEXCOORD5;
  84. #if HARDWAREFOGBLEND || DOPIXELFOG
  85. float3 projPos_fogFactorW : TEXCOORD6;
  86. #else
  87. float4 projPos_fogFactorW : TEXCOORD6;
  88. #endif
  89. float4 vNormalSqr : COLOR1;
  90. };
  91. float4 SampleNormal( sampler s, PS_INPUT i )
  92. {
  93. #if SEAMLESS
  94. float4 szy=tex2D( s, i.SeamlessTexCoord.zy );
  95. float4 sxz=tex2D( s, i.SeamlessTexCoord.xz );
  96. float4 syx=tex2D( s, i.SeamlessTexCoord.xy );
  97. return i.vNormalSqr.r*szy + i.vNormalSqr.g*sxz + i.vNormalSqr.b*syx;
  98. #else
  99. #if NORMALMAP
  100. return tex2D( s, i.normalMapTexCoord.xy);
  101. #else
  102. return float4(0,0,1,1);
  103. #endif
  104. #endif
  105. }
  106. float4_color_return_type main( PS_INPUT i ) : COLOR
  107. {
  108. bool bBase2 = WORLDVERTEXTRANSITION ? true : false;
  109. bool bBump = (NORMALMAP != 0) ? true : false;
  110. // Do spot stuff early since we can bail out
  111. float3 spotColor = float3(0,0,0);
  112. float3 vProjCoords = i.spotTexCoord.xyz / i.spotTexCoord.w;
  113. #if ( defined( _X360 ) )
  114. float3 ltz = vProjCoords.xyz < float3( 0.0f, 0.0f, 0.0f );
  115. float3 gto = vProjCoords.xyz > float3( 1.0f, 1.0f, 1.0f );
  116. [branch]
  117. if ( dot(ltz + gto, float3(1,1,1)) > 0 )
  118. {
  119. clip (-1);
  120. return float4(0,0,0,0);
  121. }
  122. else
  123. {
  124. spotColor = tex2D( SpotSampler, vProjCoords );
  125. [branch]
  126. if ( dot(spotColor.xyz, float3(1,1,1)) <= 0 )
  127. {
  128. clip(-1);
  129. return float4(0,0,0,0);
  130. }
  131. else
  132. {
  133. #else
  134. spotColor = tex2D( SpotSampler, vProjCoords ).rgb;
  135. #if ( defined(SHADER_MODEL_PS_2_B) || defined(SHADER_MODEL_PS_3_0) )
  136. spotColor *= i.spotTexCoord.www > float3(0,0,0); // Catch back projection (PC-only at the moment)
  137. #endif
  138. #endif
  139. HALF4 baseColor = 0.0f;
  140. HALF4 baseColor2 = 0.0f;
  141. HALF4 vNormal = float4(0, 0, 1, 1);
  142. float3 baseTexCoords = float3(0,0,0);
  143. float3 normalTexCoords = float3( 0, 0, 0 );
  144. #if SEAMLESS
  145. baseTexCoords = i.SeamlessTexCoord.xyz;
  146. normalTexCoords = baseTexCoords;
  147. #else
  148. baseTexCoords.xy = i.baseTexCoord.xy;
  149. #if ( NORMALMAP != 0 )
  150. normalTexCoords.xy = i.normalMapTexCoord.xy;
  151. #else
  152. normalTexCoords = baseTexCoords;
  153. #endif
  154. #endif
  155. GetBaseTextureAndNormal( BaseTextureSampler, BaseTextureSampler2, BumpMapSampler, bBase2, bBump,
  156. baseTexCoords.xyz, baseTexCoords.xyz, normalTexCoords.xy, i.vNormalSqr.xyz, baseColor.rgba, baseColor2.rgba, vNormal.xyzw );
  157. #if DETAILTEXTURE
  158. float4 detailColor = float4( g_DetailConstants.xyz, 1.0f ) * tex2D( DetailSampler, i.detailCoords );
  159. #endif
  160. #if WORLDVERTEXTRANSITION
  161. float lerpAlpha = 1-i.worldPos_worldTransition.a;
  162. #endif
  163. #if ( NORMALMAP == 0 )
  164. vNormal.xyz = normalize( i.normal.xyz );
  165. #endif
  166. #if ( NORMALMAP == 1 )
  167. vNormal.xyz = vNormal.xyz * 2.0f - 1.0f; // signed
  168. # if NORMALMAP2
  169. float3 normal2 = SampleNormal( NormalMap2Sampler, i ).xyz * 2.0f - 1.0f;
  170. vNormal.xyz = lerp( normal2, vNormal.xyz, lerpAlpha );
  171. # endif
  172. #endif
  173. // ssbump
  174. #if ( NORMALMAP == 2 )
  175. # if NORMALMAP2
  176. float3 normal2 = SampleNormal( NormalMap2Sampler, i ).xyz;
  177. vNormal.xyz = lerp( normal2, vNormal.xyz, lerpAlpha );
  178. # endif
  179. #if ( DETAILTEXTURE && ( DETAIL_BLEND_MODE == TCOMBINE_SSBUMP_BUMP ) )
  180. vNormal.xyz *= lerp( float3( 1, 1, 1 ), 2.0 * detailColor.xyz, baseColor.a );
  181. baseColor.a = 1;
  182. #endif
  183. #else
  184. // Normalize normal after all of the lerps above (including the tri/bilinear texel fetches)
  185. vNormal.xyz = normalize( vNormal.xyz );
  186. #endif
  187. spotColor.rgb *= cFlashlightColor.rgb;
  188. // Compute per-pixel distance attenuation
  189. float3 delta = g_FlashlightPos.xyz - i.worldPos_worldTransition.xyz;
  190. float distSquared = dot( delta, delta );
  191. float dist = sqrt( distSquared );
  192. float farZAtten = g_FlashlightAttenuation.w;
  193. float endFalloffFactor = RemapValClamped( dist, farZAtten, 0.6f * farZAtten, 0.0f, 1.0f );
  194. float flAtten = saturate(endFalloffFactor * dot( g_FlashlightAttenuation.xyz, float3( 1.0f, 1.0f/dist, 1.0f/distSquared ) ) );
  195. #if FLASHLIGHTSHADOWS && ( defined( SHADER_MODEL_PS_2_B ) || defined( SHADER_MODEL_PS_3_0 ) )
  196. float flShadow = DoFlashlightShadow( FlashlightDepthSampler, RandomRotationSampler, vProjCoords, i.projPos_fogFactorW.xy / i.projPos_fogFactorW.z, FLASHLIGHTDEPTHFILTERMODE, g_vShadowTweaks );
  197. float flAttenuated = lerp( flShadow, 1.0f, g_vShadowTweaks.y ); // Blend between fully attenuated and not attenuated
  198. flShadow = saturate(lerp( flAttenuated, flShadow, flAtten )); // Blend between shadow and above, according to light attenuation
  199. spotColor *= flShadow;
  200. #endif
  201. #if ( UBERLIGHT )
  202. // Transform from world space position to light space position
  203. float3 flashlightSpacePosition = mul( float4( i.worldPos_worldTransition.xyz, 1.0f ), g_FlashlightWorldToLight ).yzx;
  204. spotColor *= uberlight( flashlightSpacePosition, g_vSmoothEdge0, g_vSmoothEdge1,
  205. g_vSmoothOneOverWidth, g_vShearRound.xy, g_aAbB, g_vShearRound.zw );
  206. #endif
  207. #if WORLDVERTEXTRANSITION
  208. baseColor.xyz = lerp( baseColor2.xyz, baseColor.xyz, lerpAlpha );
  209. #endif
  210. #if DETAILTEXTURE
  211. float4 vBase = TextureCombine( baseColor, detailColor, DETAIL_BLEND_MODE, g_DetailConstants.w );
  212. baseColor.xyz = vBase.xyz;
  213. #endif
  214. #if NORMALMAP == 0
  215. float3 worldPosToLightVector = texCUBE( NormalizingCubemapSampler, i.worldPosToLightVector ).rgb * 2.0f - 1.0f;
  216. float nDotL = dot( worldPosToLightVector, vNormal.xyz );
  217. #endif
  218. #if NORMALMAP == 1
  219. // flashlightfixme: wrap this!
  220. float3 tangentPosToLightVector = texCUBE( NormalizingCubemapSampler, i.tangentPosToLightVector ).rgb * 2.0f - 1.0f;
  221. float nDotL = dot( tangentPosToLightVector, vNormal.xyz );
  222. #endif
  223. #if NORMALMAP == 2
  224. float3 tangentPosToLightVector = normalize( i.tangentPosToLightVector );
  225. float nDotL =
  226. vNormal.x*dot( tangentPosToLightVector, bumpBasis[0]) +
  227. vNormal.y*dot( tangentPosToLightVector, bumpBasis[1]) +
  228. vNormal.z*dot( tangentPosToLightVector, bumpBasis[2]);
  229. #endif
  230. float3 outColor;
  231. outColor = g_DiffuseModulation * spotColor * baseColor.xyz * saturate( nDotL );
  232. outColor *= flAtten;
  233. float flVertexFogFactor = 0.0f;
  234. #if ( !HARDWAREFOGBLEND && !DOPIXELFOG )
  235. {
  236. flVertexFogFactor = i.projPos_fogFactorW.w;
  237. }
  238. #endif
  239. float fogFactor = CalcPixelFogFactorSupportsVertexFog( PIXELFOGTYPE, g_FogParams, g_EyePos.xyz, i.worldPos_worldTransition.xyz, i.projPos_fogFactorW.z, flVertexFogFactor );
  240. return FinalOutput( float4(outColor, baseColor.a), fogFactor, PIXELFOGTYPE, TONEMAP_SCALE_LINEAR );
  241. // so we can jump over all of the above
  242. #if ( defined( _X360 ) )
  243. }
  244. }
  245. #endif
  246. }