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.

160 lines
4.6 KiB

  1. //========== Copyright (c) Valve Corporation, All rights reserved. ==========//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. // STATIC: "MODEL" "0..1"
  7. // STATIC: "COLORMODULATE" "0..1"
  8. // DYNAMIC: "COMPRESSED_VERTS" "0..1"
  9. // DYNAMIC: "SKINNING" "0..1"
  10. #include "common_vs_fxc.h"
  11. static const bool g_bSkinning = SKINNING ? true : false;
  12. static const bool g_bModel = MODEL ? true : false;
  13. const float4 cBumpTexCoordTransform[4] : register( SHADER_SPECIFIC_CONST_1 );
  14. const float g_flTime : register( SHADER_SPECIFIC_CONST_5 );
  15. const float4 g_vViewportMad : register( SHADER_SPECIFIC_CONST_6 );
  16. struct VS_INPUT
  17. {
  18. float4 vPos : POSITION;
  19. float4 vBoneWeights : BLENDWEIGHT;
  20. float4 vBoneIndices : BLENDINDICES;
  21. float4 vNormal : NORMAL;
  22. float4 vBaseTexCoord : TEXCOORD0;
  23. #if !MODEL
  24. float3 vTangentS : TANGENT;
  25. float3 vTangentT : BINORMAL0;
  26. #else
  27. float4 vUserData : TANGENT;
  28. #endif
  29. #if COLORMODULATE
  30. float4 vColor : COLOR0;
  31. #endif
  32. };
  33. struct VS_OUTPUT
  34. {
  35. float4 vProjPos_POSITION : POSITION;
  36. #if !defined( _X360 ) && !defined( SHADER_MODEL_VS_3_0 )
  37. float fog : FOG;
  38. #endif
  39. float4 vBumpTexCoord : TEXCOORD0;
  40. float3 vTangentEyeVect : TEXCOORD1;
  41. float3 vWorldNormal : TEXCOORD2;
  42. float3 vWorldTangent : TEXCOORD3;
  43. float3 vWorldBinormal : TEXCOORD4;
  44. float3 vRefractXYW : TEXCOORD5;
  45. float3 vWorldViewVector : TEXCOORD6;
  46. #if COLORMODULATE
  47. float4 vColor : COLOR0;
  48. #endif
  49. float4 worldPos_projPosZ : TEXCOORD7; // Necessary for pixel fog
  50. };
  51. VS_OUTPUT main( const VS_INPUT v )
  52. {
  53. VS_OUTPUT o = ( VS_OUTPUT )0;
  54. #if COLORMODULATE
  55. o.vColor = v.vColor;
  56. #endif
  57. float3 worldNormal, worldPos, worldTangentS, worldTangentT;
  58. float3 vObjNormal;
  59. #if MODEL
  60. float4 vObjTangent;
  61. DecompressVertex_NormalTangent( v.vNormal, v.vUserData, vObjNormal, vObjTangent );
  62. SkinPositionNormalAndTangentSpace(
  63. g_bSkinning,
  64. v.vPos, vObjNormal, vObjTangent,
  65. v.vBoneWeights, v.vBoneIndices,
  66. worldPos, worldNormal, worldTangentS, worldTangentT );
  67. #else
  68. DecompressVertex_Normal( v.vNormal, vObjNormal );
  69. worldPos = mul4x3( v.vPos, cModel[0] );
  70. worldTangentS = mul3x3( v.vTangentS, ( const float3x3 )cModel[0] );
  71. worldTangentT = mul3x3( v.vTangentT, ( const float3x3 )cModel[0] );
  72. worldNormal = mul3x3( vObjNormal, ( float3x3 )cModel[0] );
  73. #endif
  74. // Normalize vectors
  75. worldNormal.xyz = normalize( worldNormal.xyz );
  76. worldTangentS.xyz = normalize( worldTangentS.xyz );
  77. worldTangentT.xyz = normalize( worldTangentT.xyz );
  78. // World normal
  79. o.vWorldNormal.xyz = normalize( worldNormal.xyz );
  80. // Projected position
  81. float4 vProjPos = mul( float4( worldPos, 1 ), cViewProj );
  82. o.vProjPos_POSITION = vProjPos;
  83. o.worldPos_projPosZ = float4( worldPos.xyz, vProjPos.z );
  84. //o.projNormal.xyz = mul( worldNormal, cViewProj );
  85. #ifdef _PS3
  86. // Account for OpenGL's flipped y coordinate and expanded z range [-1,1] instead of [0,1]
  87. o.vProjPos_POSITION.y = -o.vProjPos_POSITION.y;
  88. o.vProjPos_POSITION.z = 2.0f * o.vProjPos_POSITION.z - o.vProjPos_POSITION.w;
  89. #endif // _PS3
  90. // Map projected position to the refraction texture
  91. float2 vRefractPos;
  92. vRefractPos.x = vProjPos.x;
  93. vRefractPos.y = -vProjPos.y; // invert Y
  94. vRefractPos = (vRefractPos + vProjPos.w) * 0.5f;
  95. // Adjust for current viewport
  96. #if defined( _X360 ) || defined( _PS3 )
  97. {
  98. vRefractPos.xy = ( ( ( vRefractPos.xy / vProjPos.w ) * g_vViewportMad.xy ) + g_vViewportMad.zw ) * vProjPos.w;
  99. }
  100. #endif
  101. // Refraction transform
  102. o.vRefractXYW = float3(vRefractPos.x, vRefractPos.y, vProjPos.w);
  103. // Compute fog based on the position
  104. float3 vWorldPos = mul4x3( v.vPos, cModel[0] );
  105. #if !defined( _X360 ) && !defined( SHADER_MODEL_VS_3_0 )
  106. o.fog = CalcFixedFunctionFog( vWorldPos, FOGTYPE_RANGE );
  107. #endif
  108. // Eye vector
  109. float3 vWorldEyeVect = normalize( cEyePos - vWorldPos );
  110. o.vWorldViewVector.xyz = -vWorldEyeVect.xyz;
  111. // Transform to the tangent space
  112. o.vTangentEyeVect.x = dot( vWorldEyeVect, worldTangentS );
  113. o.vTangentEyeVect.y = dot( vWorldEyeVect, worldTangentT );
  114. o.vTangentEyeVect.z = dot( vWorldEyeVect, worldNormal );
  115. // Tranform bump coordinates
  116. o.vBumpTexCoord.x = dot( v.vBaseTexCoord, cBumpTexCoordTransform[0] );
  117. o.vBumpTexCoord.y = dot( v.vBaseTexCoord, cBumpTexCoordTransform[1] );
  118. // Tranform bump coordinates (note wz, not zw)
  119. o.vBumpTexCoord.w = dot( v.vBaseTexCoord, cBumpTexCoordTransform[2] );
  120. o.vBumpTexCoord.z = dot( v.vBaseTexCoord, cBumpTexCoordTransform[3] );
  121. // Tangent space transform
  122. o.vWorldNormal.xyz = worldNormal.xyz;
  123. o.vWorldTangent.xyz = worldTangentS.xyz;
  124. o.vWorldBinormal.xyz = worldTangentT.xyz;
  125. return o;
  126. }