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.

152 lines
5.1 KiB

  1. // ------------------------------------------------------------------------------
  2. // $cLight0Pos = world space light position
  3. // $SHADER_SPECIFIC_CONST_1 = spotlight projection
  4. // $SHADER_SPECIFIC_CONST_2 = spotlight projection
  5. // $SHADER_SPECIFIC_CONST_3 = spotlight projection
  6. // $SHADER_SPECIFIC_CONST_4 = spotlight projection
  7. // $SHADER_SPECIFIC_CONST_5 = far z
  8. // $SHADER_SPECIFIC_CONST_6 = eyeball origin
  9. // $SHADER_SPECIFIC_CONST_7 = eyeball up * 0.5
  10. // $SHADER_SPECIFIC_CONST_8 = iris projection U
  11. // $SHADER_SPECIFIC_CONST_9 = iris projection V
  12. // ------------------------------------------------------------------------------
  13. // DYNAMIC: "COMPRESSED_VERTS" "0..1"
  14. // DYNAMIC: "SKINNING" "0..1"
  15. #include "common_fog_vs_fxc.h"
  16. // DYNAMIC: "MORPHING" "0..0" [ = false ]
  17. #include "common_vs_fxc.h"
  18. static const bool g_bSkinning = SKINNING ? true : false;
  19. static const int g_FogType = DOWATERFOG;
  20. const float4 cLightPosition : register( SHADER_SPECIFIC_CONST_0 );
  21. const float4 cSpotlightProj1 : register( SHADER_SPECIFIC_CONST_1 );
  22. const float4 cSpotlightProj2 : register( SHADER_SPECIFIC_CONST_2 );
  23. const float4 cSpotlightProj3 : register( SHADER_SPECIFIC_CONST_3 );
  24. const float4 cSpotlightProj4 : register( SHADER_SPECIFIC_CONST_4 );
  25. const float4 cFlashlighAtten : register( SHADER_SPECIFIC_CONST_5 ); // const, linear, quadratic & farZ
  26. const float4 cIrisProjectionU : register( SHADER_SPECIFIC_CONST_8 );
  27. const float4 cIrisProjectionV : register( SHADER_SPECIFIC_CONST_9 );
  28. #ifdef SHADER_MODEL_VS_3_0
  29. // NOTE: cMorphTargetTextureDim.xy = target dimensions,
  30. // cMorphTargetTextureDim.z = 4tuples/morph
  31. const float3 cMorphTargetTextureDim : register( SHADER_SPECIFIC_CONST_10 );
  32. const float4 cMorphSubrect : register( SHADER_SPECIFIC_CONST_11 );
  33. sampler2D morphSampler : register( s0 );
  34. #endif
  35. struct VS_INPUT
  36. {
  37. float4 vPos : POSITION; // Position
  38. float4 vBoneWeights : BLENDWEIGHT; // Skin weights
  39. float4 vBoneIndices : BLENDINDICES; // Skin indices
  40. float4 vNormal : NORMAL;
  41. float4 vTexCoord0 : TEXCOORD0; // Base (sclera) texture coordinates
  42. // Position and normal/tangent deltas
  43. float3 vPosFlex : POSITION1;
  44. float3 vNormalFlex : NORMAL1;
  45. #ifdef SHADER_MODEL_VS_3_0
  46. float vVertexID : POSITION2;
  47. #endif
  48. };
  49. struct VS_OUTPUT
  50. {
  51. float4 projPos : POSITION; // Projection-space position
  52. #if !defined( _X360 ) && !defined( SHADER_MODEL_VS_3_0 )
  53. float fog : FOG; // Fixed-function fog factor
  54. #endif
  55. float4 spotTexCoord : TEXCOORD0; // Spotlight texture coordinates
  56. float2 baseTexCoord : TEXCOORD1; // Base texture coordinates
  57. float2 irisTexCoord : TEXCOORD3; // Iris texture coordinates
  58. float3 vertAtten : TEXCOORD4; // vertex attenuation
  59. float3 worldPos : TEXCOORD5;
  60. float3 projPosXYZ : TEXCOORD7;
  61. };
  62. float RemapValClamped_01( float val, float A, float B )
  63. {
  64. float cVal = (val - A) / (B - A);
  65. cVal = saturate( cVal );
  66. return cVal;
  67. }
  68. VS_OUTPUT main( const VS_INPUT v )
  69. {
  70. VS_OUTPUT o = ( VS_OUTPUT )0;
  71. float4 vPosition = v.vPos;
  72. float3 vNormal;
  73. DecompressVertex_Normal( v.vNormal, vNormal );
  74. #if !defined( SHADER_MODEL_VS_3_0 ) || !MORPHING
  75. ApplyMorph( v.vPosFlex, v.vNormalFlex, vPosition.xyz, vNormal );
  76. #else
  77. ApplyMorph( morphSampler, cMorphTargetTextureDim, cMorphSubrect, v.vVertexID, float3( 0, 0, 0 ), vPosition.xyz, vNormal );
  78. #endif
  79. // Perform skinning
  80. float3 worldNormal, worldPos;
  81. SkinPositionAndNormal(
  82. g_bSkinning,
  83. vPosition, vNormal,
  84. v.vBoneWeights, v.vBoneIndices,
  85. worldPos, worldNormal );
  86. worldNormal = normalize( worldNormal );
  87. // Transform into projection space
  88. float4 projPos = mul( float4( worldPos, 1 ), cViewProj );
  89. o.projPos = projPos;
  90. #ifdef _PS3
  91. // Account for OpenGL's flipped y coordinate and expanded z range [-1,1] instead of [0,1]
  92. o.projPos.y = -o.projPos.y;
  93. o.projPos.z = 2.0f * o.projPos.z - o.projPos.w;
  94. #endif // _PS3
  95. o.projPosXYZ = projPos.xyz;
  96. o.worldPos = worldPos.xyz;
  97. #if !defined( _X360 ) && !defined( SHADER_MODEL_VS_3_0 )
  98. o.fog = CalcFixedFunctionFog( worldPos, g_FogType );
  99. #endif
  100. // Base texture coordinates
  101. o.baseTexCoord = v.vTexCoord0;
  102. // Spotlight texture coordinates
  103. o.spotTexCoord.x = dot( cSpotlightProj1, float4(worldPos, 1) );
  104. o.spotTexCoord.y = dot( cSpotlightProj2, float4(worldPos, 1) );
  105. o.spotTexCoord.z = dot( cSpotlightProj3, float4(worldPos, 1) );
  106. o.spotTexCoord.w = dot( cSpotlightProj4, float4(worldPos, 1) );
  107. // Compute vector to light
  108. float3 vWorldPosToLightVector = cLightPosition.xyz - worldPos;
  109. float3 vDistAtten = float3(1, 1, 1);
  110. vDistAtten.z = dot( vWorldPosToLightVector, vWorldPosToLightVector ); // distsquared
  111. vDistAtten.y = rsqrt( vDistAtten.z ); // 1 / dist
  112. float flDist = vDistAtten.z * vDistAtten.y; // dist
  113. vDistAtten.z = 1.0f / vDistAtten.z; // 1 / distsquared
  114. float fFarZ = cFlashlighAtten.w;
  115. float endFalloffFactor = RemapValClamped_01( flDist, fFarZ, 0.6 * fFarZ );
  116. o.vertAtten.xyz = endFalloffFactor * dot( vDistAtten, cFlashlighAtten.xyz );
  117. o.vertAtten *= dot( normalize( vWorldPosToLightVector ), worldNormal );
  118. o.irisTexCoord.x = dot( cIrisProjectionU, float4(worldPos, 1) );
  119. o.irisTexCoord.y = dot( cIrisProjectionV, float4(worldPos, 1) );
  120. return o;
  121. }