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.

159 lines
4.9 KiB

  1. //========== Copyright (c) Valve Corporation, All rights reserved. ==========//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. // STATIC: "MODEL" "0..1"
  7. // STATIC: "FLATTEN_STATIC_CONTROL_FLOW" "0..1" [vs20] [PC]
  8. // STATIC: "FLATTEN_STATIC_CONTROL_FLOW" "0..0" [CONSOLE]
  9. // DYNAMIC: "COMPRESSED_VERTS" "0..1"
  10. #include "common_fog_vs_fxc.h"
  11. // DYNAMIC: "SKINNING" "0..1"
  12. // DYNAMIC: "MORPHING" "0..0" [ = false ]
  13. // DYNAMIC: "NUM_LIGHTS" "0..2" [vs20] [PC]
  14. // DYNAMIC: "NUM_LIGHTS" "0..0" [vs20] [CONSOLE]
  15. // If using static control flow on Direct3D, we should use the NUM_LIGHTS=0 combo
  16. // SKIP: ( $FLATTEN_STATIC_CONTROL_FLOW == 0 )&& ( $NUM_LIGHTS > 0 ) [vs20] [PC]
  17. #include "common_vs_fxc.h"
  18. static const bool g_bSkinning = SKINNING ? true : false;
  19. static const int g_FogType = DOWATERFOG;
  20. #ifdef SHADER_MODEL_VS_3_0
  21. // NOTE: cMorphTargetTextureDim.xy = target dimensions,
  22. // cMorphTargetTextureDim.z = 4tuples/morph
  23. const float3 cMorphTargetTextureDim : register( SHADER_SPECIFIC_CONST_6 );
  24. const float4 cMorphSubrect : register( SHADER_SPECIFIC_CONST_7 );
  25. sampler2D morphSampler : register( s0 );
  26. #endif
  27. struct VS_INPUT
  28. {
  29. float4 vPos : POSITION;
  30. float4 vBoneWeights : BLENDWEIGHT;
  31. float4 vBoneIndices : BLENDINDICES;
  32. float4 vNormal : NORMAL;
  33. float4 vBaseTexCoord : TEXCOORD0;
  34. float4 vUserData : TANGENT;
  35. // Position and normal/tangent deltas
  36. float3 vPosFlex : POSITION1;
  37. float3 vNormalFlex : NORMAL1;
  38. #ifdef SHADER_MODEL_VS_3_0
  39. float vVertexID : POSITION2;
  40. #endif
  41. };
  42. struct VS_OUTPUT
  43. {
  44. float4 vSetupProjPos : POSITION;
  45. #if !defined( _X360 ) && !defined( SHADER_MODEL_VS_3_0 )
  46. float fog : FOG;
  47. #endif
  48. float2 vBaseTexCoord : TEXCOORD0;
  49. float3x3 tangentSpaceTranspose : TEXCOORD1;
  50. // second row : TEXCOORD2;
  51. // third row : TEXCOORD3;
  52. float3 worldPos : TEXCOORD4;
  53. float3 projPos : TEXCOORD5;
  54. float4 lightAtten : TEXCOORD6;
  55. };
  56. VS_OUTPUT main( const VS_INPUT v )
  57. {
  58. VS_OUTPUT o = ( VS_OUTPUT )0;
  59. float4 vPosition = v.vPos;
  60. float3 vNormal;
  61. float4 vTangent;
  62. DecompressVertex_NormalTangent( v.vNormal, v.vUserData, vNormal, vTangent );
  63. #if !defined( SHADER_MODEL_VS_3_0 ) || !MORPHING
  64. ApplyMorph( v.vPosFlex, v.vNormalFlex, vPosition.xyz, vNormal, vTangent.xyz );
  65. #else
  66. ApplyMorph( morphSampler, cMorphTargetTextureDim, cMorphSubrect, v.vVertexID, float3( 0, 0, 0 ),
  67. vPosition.xyz, vNormal, vTangent.xyz );
  68. #endif
  69. // Perform skinning
  70. float3 worldNormal, worldPos, worldTangentS, worldTangentT;
  71. SkinPositionNormalAndTangentSpace( g_bSkinning, vPosition, vNormal, vTangent,
  72. v.vBoneWeights, v.vBoneIndices, worldPos,
  73. worldNormal, worldTangentS, worldTangentT );
  74. // Always normalize since flex path is controlled by runtime
  75. // constant not a shader combo and will always generate the normalization
  76. worldNormal = normalize( worldNormal );
  77. worldTangentS = normalize( worldTangentS );
  78. worldTangentT = normalize( worldTangentT );
  79. // Projected position
  80. float4 vProjPos = mul( float4( worldPos, 1 ), cViewProj );
  81. // Map projected position to the refraction texture
  82. float2 vRefractPos;
  83. vRefractPos.x = vProjPos.x;
  84. vRefractPos.y = -vProjPos.y; // invert Y
  85. vRefractPos = (vRefractPos + vProjPos.w) * 0.5f;
  86. #ifdef _PS3
  87. // Account for OpenGL's flipped y coordinate and expanded z range [-1,1] instead of [0,1]
  88. vProjPos.y = -vProjPos.y;
  89. vProjPos.z = 2.0f * vProjPos.z - vProjPos.w;
  90. #endif // _PS3
  91. o.vSetupProjPos = vProjPos;
  92. #if defined ( SHADER_MODEL_VS_2_0 ) && ( FLATTEN_STATIC_CONTROL_FLOW )
  93. o.lightAtten = float4(0,0,0,0);
  94. #if ( NUM_LIGHTS > 0 )
  95. o.lightAtten.x = GetVertexAttenForLight( worldPos, 0, false );
  96. #endif
  97. #if ( NUM_LIGHTS > 1 )
  98. o.lightAtten.y = GetVertexAttenForLight( worldPos, 1, false );
  99. #endif
  100. #if ( NUM_LIGHTS > 2 )
  101. o.lightAtten.z = GetVertexAttenForLight( worldPos, 2, false );
  102. #endif
  103. #if ( NUM_LIGHTS > 3 )
  104. o.lightAtten.w = GetVertexAttenForLight( worldPos, 3, false );
  105. #endif
  106. #else
  107. // Scalar light attenuation
  108. o.lightAtten.x = GetVertexAttenForLight( worldPos, 0, true );
  109. o.lightAtten.y = GetVertexAttenForLight( worldPos, 1, true );
  110. o.lightAtten.z = GetVertexAttenForLight( worldPos, 2, true );
  111. o.lightAtten.w = GetVertexAttenForLight( worldPos, 3, true );
  112. #endif
  113. // Compute fog based on the position
  114. float3 vWorldPos = mul4x3( v.vPos, cModel[0] );
  115. #if !defined( _X360 ) && !defined( SHADER_MODEL_VS_3_0 )
  116. o.fog = CalcFixedFunctionFog( vWorldPos, g_FogType );
  117. #endif
  118. // World position
  119. o.worldPos = worldPos;
  120. // Refract position
  121. o.projPos = float3(vRefractPos.x, vRefractPos.y, vProjPos.w);
  122. // Tranform bump coordinates
  123. o.vBaseTexCoord = v.vBaseTexCoord.xy;
  124. // Tangent space transform
  125. o.tangentSpaceTranspose[0] = float3( worldTangentS.x, worldTangentT.x, worldNormal.x );
  126. o.tangentSpaceTranspose[1] = float3( worldTangentS.y, worldTangentT.y, worldNormal.y );
  127. o.tangentSpaceTranspose[2] = float3( worldTangentS.z, worldTangentT.z, worldNormal.z );
  128. return o;
  129. }