Team Fortress 2 Source Code as on 22/4/2020
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.

174 lines
6.1 KiB

  1. //======= Copyright (c) 1996-2007, Valve Corporation, All rights reserved. ======
  2. // STATIC: "HALFLAMBERT" "0..1"
  3. // STATIC: "USE_STATIC_CONTROL_FLOW" "0..1" [vs20]
  4. // DYNAMIC: "COMPRESSED_VERTS" "0..1"
  5. // DYNAMIC: "DOWATERFOG" "0..1"
  6. // DYNAMIC: "SKINNING" "0..1"
  7. // DYNAMIC: "MORPHING" "0..1" [vs30]
  8. // DYNAMIC: "NUM_LIGHTS" "0..2" [vs20]
  9. // If using static control flow on Direct3D, we should use the NUM_LIGHTS=0 combo
  10. // SKIP: $USE_STATIC_CONTROL_FLOW && ( $NUM_LIGHTS > 0 ) [vs20]
  11. // SKIP: $DOWATERFOG
  12. #include "vortwarp_vs20_helper.h"
  13. static const bool g_bSkinning = SKINNING ? true : false;
  14. static const int g_FogType = DOWATERFOG;
  15. const float4 cBaseTexCoordTransform[2] : register( SHADER_SPECIFIC_CONST_0 );
  16. const float4 const4 : register( SHADER_SPECIFIC_CONST_4 );
  17. #define g_Time const4.w
  18. #define modelOrigin const4.xyz
  19. #ifdef SHADER_MODEL_VS_3_0
  20. // NOTE: cMorphTargetTextureDim.xy = target dimensions,
  21. // cMorphTargetTextureDim.z = 4tuples/morph
  22. const float3 cMorphTargetTextureDim : register( SHADER_SPECIFIC_CONST_6 );
  23. const float4 cMorphSubrect : register( SHADER_SPECIFIC_CONST_7 );
  24. sampler2D morphSampler : register( D3DVERTEXTEXTURESAMPLER0, s0 );
  25. #endif
  26. //-----------------------------------------------------------------------------
  27. // Input vertex format
  28. //-----------------------------------------------------------------------------
  29. struct VS_INPUT
  30. {
  31. // This is all of the stuff that we ever use.
  32. float4 vPos : POSITION;
  33. float4 vBoneWeights : BLENDWEIGHT;
  34. float4 vBoneIndices : BLENDINDICES;
  35. float4 vNormal : NORMAL;
  36. float4 vColor : COLOR0;
  37. float3 vSpecular : COLOR1;
  38. // make these float2's and stick the [n n 0 1] in the dot math.
  39. float4 vTexCoord0 : TEXCOORD0;
  40. float4 vTexCoord1 : TEXCOORD1;
  41. float4 vTexCoord2 : TEXCOORD2;
  42. float4 vTexCoord3 : TEXCOORD3;
  43. float3 vTangentS : TANGENT;
  44. float3 vTangentT : BINORMAL;
  45. float4 vUserData : TANGENT;
  46. // Position and normal/tangent deltas
  47. float3 vPosFlex : POSITION1;
  48. float3 vNormalFlex : NORMAL1;
  49. #ifdef SHADER_MODEL_VS_3_0
  50. float vVertexID : POSITION2;
  51. #endif
  52. };
  53. //-----------------------------------------------------------------------------
  54. // Output vertex format
  55. //-----------------------------------------------------------------------------
  56. struct VS_OUTPUT
  57. {
  58. float4 projPos : POSITION;
  59. #if !defined( _X360 )
  60. float fog : FOG;
  61. #endif
  62. HALF4 baseTexCoord2_tangentSpaceVertToEyeVectorXY : TEXCOORD0;
  63. // bump mapping and a separate envmap mask texture are mutually exclusive.
  64. float4 lightAtten : TEXCOORD1;
  65. float4 worldVertToEyeVectorXYZ_tangentSpaceVertToEyeVectorZ : TEXCOORD2;
  66. float3x3 tangentSpaceTranspose : TEXCOORD3;
  67. // second row : TEXCOORD4;
  68. // third row : TEXCOORD5;
  69. float4 worldPos_projPosZ : TEXCOORD6;
  70. float4 fogFactorW : COLOR1;
  71. };
  72. //-----------------------------------------------------------------------------
  73. // Main shader entry point
  74. //-----------------------------------------------------------------------------
  75. VS_OUTPUT main( const VS_INPUT v )
  76. {
  77. VS_OUTPUT o = ( VS_OUTPUT )0;
  78. float4 vPosition = v.vPos;
  79. float3 vNormal;
  80. float4 vTangent;
  81. DecompressVertex_NormalTangent( v.vNormal, v.vUserData, vNormal, vTangent );
  82. #if !defined( SHADER_MODEL_VS_3_0 ) || !MORPHING
  83. ApplyMorph( v.vPosFlex, v.vNormalFlex, vPosition.xyz, vNormal, vTangent.xyz );
  84. #else
  85. ApplyMorph( morphSampler, cMorphTargetTextureDim, cMorphSubrect, v.vVertexID, float3( 0, 0, 0 ),
  86. vPosition.xyz, vNormal, vTangent.xyz );
  87. #endif
  88. // Perform skinning
  89. float3 worldNormal, worldPos, worldTangentS, worldTangentT;
  90. SkinPositionNormalAndTangentSpace(
  91. g_bSkinning,
  92. vPosition, vNormal, vTangent,
  93. v.vBoneWeights, v.vBoneIndices,
  94. worldPos, worldNormal, worldTangentS, worldTangentT );
  95. WorldSpaceVertexProcess( g_Time, modelOrigin, worldPos, worldNormal, worldTangentS, worldTangentT );
  96. // Always normalize since flex path is controlled by runtime
  97. // constant not a shader combo and will always generate the normalization
  98. worldNormal = normalize( worldNormal );
  99. worldTangentS = normalize( worldTangentS );
  100. worldTangentT = normalize( worldTangentT );
  101. // Transform into projection space
  102. float4 projPos = mul( float4( worldPos, 1 ), cViewProj );
  103. o.projPos = projPos;
  104. projPos.z = mul( float4( worldPos, 1 ), cViewProjZ );
  105. o.fogFactorW = CalcFog( worldPos, projPos, g_FogType );
  106. #if !defined( _X360 )
  107. o.fog = o.fogFactorW;
  108. #endif
  109. // Needed for water fog alpha and diffuse lighting
  110. // FIXME: we shouldn't have to compute this all the time.
  111. o.worldPos_projPosZ = float4( worldPos, projPos.z );
  112. // Needed for cubemapping + parallax mapping
  113. // FIXME: We shouldn't have to compute this all the time.
  114. o.worldVertToEyeVectorXYZ_tangentSpaceVertToEyeVectorZ.xyz = VSHADER_VECT_SCALE * (cEyePos - worldPos);
  115. #if defined ( SHADER_MODEL_VS_2_0 ) && ( !USE_STATIC_CONTROL_FLOW )
  116. o.lightAtten = float4(0,0,0,0);
  117. #if ( NUM_LIGHTS > 0 )
  118. o.lightAtten.x = GetVertexAttenForLight( worldPos, 0, false );
  119. #endif
  120. #if ( NUM_LIGHTS > 1 )
  121. o.lightAtten.y = GetVertexAttenForLight( worldPos, 1, false );
  122. #endif
  123. #if ( NUM_LIGHTS > 2 )
  124. o.lightAtten.z = GetVertexAttenForLight( worldPos, 2, false );
  125. #endif
  126. #if ( NUM_LIGHTS > 3 )
  127. o.lightAtten.w = GetVertexAttenForLight( worldPos, 3, false );
  128. #endif
  129. #else
  130. // Scalar light attenuation
  131. o.lightAtten.x = GetVertexAttenForLight( worldPos, 0, true );
  132. o.lightAtten.y = GetVertexAttenForLight( worldPos, 1, true );
  133. o.lightAtten.z = GetVertexAttenForLight( worldPos, 2, true );
  134. o.lightAtten.w = GetVertexAttenForLight( worldPos, 3, true );
  135. #endif
  136. // Base texture coordinate transform
  137. o.baseTexCoord2_tangentSpaceVertToEyeVectorXY.x = dot( v.vTexCoord0, cBaseTexCoordTransform[0] );
  138. o.baseTexCoord2_tangentSpaceVertToEyeVectorXY.y = dot( v.vTexCoord0, cBaseTexCoordTransform[1] );
  139. // Tangent space transform
  140. o.tangentSpaceTranspose[0] = float3( worldTangentS.x, worldTangentT.x, worldNormal.x );
  141. o.tangentSpaceTranspose[1] = float3( worldTangentS.y, worldTangentT.y, worldNormal.y );
  142. o.tangentSpaceTranspose[2] = float3( worldTangentS.z, worldTangentT.z, worldNormal.z );
  143. return o;
  144. }