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.

250 lines
7.8 KiB

  1. //======= Copyright � 1996-2007, Valve Corporation, All rights reserved. ======
  2. // STATIC: "VERTEXCOLOR" "0..1"
  3. // STATIC: "CUBEMAP" "0..1"
  4. // STATIC: "HALFLAMBERT" "0..1"
  5. // STATIC: "FLASHLIGHT" "0..1"
  6. // STATIC: "SEAMLESS_BASE" "0..1"
  7. // STATIC: "SEAMLESS_DETAIL" "0..1"
  8. // STATIC: "SEPARATE_DETAIL_UVS" "0..1"
  9. // STATIC: "DECAL" "0..1" [vs30]
  10. // STATIC: "USE_STATIC_CONTROL_FLOW" "0..1" [vs20]
  11. // STATIC: "DONT_GAMMA_CONVERT_VERTEX_COLOR" "0..1"
  12. // DYNAMIC: "COMPRESSED_VERTS" "0..1"
  13. // DYNAMIC: "DYNAMIC_LIGHT" "0..1"
  14. // DYNAMIC: "STATIC_LIGHT_VERTEX" "0..1"
  15. // DYNAMIC: "STATIC_LIGHT_LIGHTMAP" "0..1"
  16. // DYNAMIC: "DOWATERFOG" "0..1"
  17. // DYNAMIC: "SKINNING" "0..1"
  18. // DYNAMIC: "LIGHTING_PREVIEW" "0..1" [PC]
  19. // DYNAMIC: "LIGHTING_PREVIEW" "0..0" [XBOX]
  20. // DYNAMIC: "MORPHING" "0..1" [vs30]
  21. // DYNAMIC: "NUM_LIGHTS" "0..2" [vs20]
  22. // If using static control flow on Direct3D, we should use the NUM_LIGHTS=0 combo
  23. // SKIP: $USE_STATIC_CONTROL_FLOW && ( $NUM_LIGHTS > 0 ) [vs20]
  24. // SKIP: ($SEPARATE_DETAIL_UVS) && ($SEAMLESS_DETAIL)
  25. // SKIP: ($DONT_GAMMA_CONVERT_VERTEX_COLOR && ( ! $VERTEXCOLOR ) )
  26. #include "common_vs_fxc.h"
  27. static const bool g_bSkinning = SKINNING ? true : false;
  28. static const int g_FogType = DOWATERFOG;
  29. static const bool g_bVertexColor = VERTEXCOLOR ? true : false;
  30. static const bool g_bCubemap = CUBEMAP ? true : false;
  31. static const bool g_bFlashlight = FLASHLIGHT ? true : false;
  32. static const bool g_bHalfLambert = HALFLAMBERT ? true : false;
  33. #if (defined( SHADER_MODEL_VS_3_0 ) && MORPHING && DECAL)
  34. static const bool g_bDecalOffset = true;
  35. #else
  36. static const bool g_bDecalOffset = false;
  37. #endif
  38. const float4 cBaseTexCoordTransform[2] : register( SHADER_SPECIFIC_CONST_0 );
  39. #if SEAMLESS_DETAIL || SEAMLESS_BASE
  40. const float cSeamlessScale : register( SHADER_SPECIFIC_CONST_2);
  41. #define SEAMLESS_SCALE cSeamlessScale.x
  42. #endif
  43. const float4 cDetailTexCoordTransform[2] : register( SHADER_SPECIFIC_CONST_4 );
  44. #if defined ( _X360 )
  45. const float4x4 g_FlashlightWorldToTexture : register( SHADER_SPECIFIC_CONST_6 ); // 6, 7, 8, 9
  46. #endif
  47. #ifdef SHADER_MODEL_VS_3_0
  48. // NOTE: cMorphTargetTextureDim.xy = target dimensions,
  49. // cMorphTargetTextureDim.z = 4tuples/morph
  50. const float3 cMorphTargetTextureDim : register( SHADER_SPECIFIC_CONST_10 );
  51. const float4 cMorphSubrect : register( SHADER_SPECIFIC_CONST_11 );
  52. sampler2D morphSampler : register( D3DVERTEXTEXTURESAMPLER0, s0 );
  53. #endif
  54. struct VS_INPUT
  55. {
  56. // This is all of the stuff that we ever use.
  57. float4 vPos : POSITION;
  58. float4 vBoneWeights : BLENDWEIGHT;
  59. float4 vBoneIndices : BLENDINDICES;
  60. float4 vNormal : NORMAL;
  61. float4 vColor : COLOR0;
  62. float3 vSpecular : COLOR1;
  63. // make these float2's and stick the [n n 0 1] in the dot math.
  64. float4 vTexCoord0 : TEXCOORD0;
  65. float4 vTexCoord1 : TEXCOORD1;
  66. float4 vTexCoord2 : TEXCOORD2;
  67. float4 vTexCoord3 : TEXCOORD3;
  68. // Position and normal/tangent deltas
  69. float3 vPosFlex : POSITION1;
  70. float3 vNormalFlex : NORMAL1;
  71. #ifdef SHADER_MODEL_VS_3_0
  72. float vVertexID : POSITION2;
  73. #endif
  74. };
  75. struct VS_OUTPUT
  76. {
  77. float4 projPos : POSITION; // Projection-space position
  78. #if !defined( _X360 )
  79. float fog : FOG;
  80. #endif
  81. #if SEAMLESS_BASE
  82. HALF3 SeamlessTexCoord : TEXCOORD0; // Base texture x/y/z (indexed by swizzle)
  83. #else
  84. HALF2 baseTexCoord : TEXCOORD0; // Base texture coordinate
  85. #endif
  86. #if SEAMLESS_DETAIL
  87. HALF3 SeamlessDetailTexCoord : TEXCOORD1; // Detail texture coordinate
  88. #else
  89. HALF2 detailTexCoord : TEXCOORD1; // Detail texture coordinate
  90. #endif
  91. float4 color : TEXCOORD2; // Vertex color (from lighting or unlit)
  92. #if CUBEMAP || _X360
  93. float3 worldVertToEyeVector : TEXCOORD3; // Necessary for cubemaps
  94. #endif
  95. float3 worldSpaceNormal : TEXCOORD4; // Necessary for cubemaps and flashlight
  96. #if defined ( _X360 ) && FLASHLIGHT
  97. float4 flashlightSpacePos : TEXCOORD5;
  98. #endif
  99. float4 vProjPos : TEXCOORD6;
  100. float4 worldPos_ProjPosZ : TEXCOORD7;
  101. float4 fogFactorW : COLOR1;
  102. #if SEAMLESS_DETAIL || SEAMLESS_BASE
  103. float3 SeamlessWeights : COLOR0; // x y z projection weights
  104. #endif
  105. };
  106. VS_OUTPUT main( const VS_INPUT v )
  107. {
  108. VS_OUTPUT o = ( VS_OUTPUT )0;
  109. bool bDynamicLight = DYNAMIC_LIGHT ? true : false;
  110. bool bStaticLight = STATIC_LIGHT_VERTEX ? true : false;
  111. bool bDoLighting = !g_bVertexColor && (bDynamicLight || bStaticLight);
  112. float4 vPosition = v.vPos;
  113. float3 vNormal = 0;
  114. if ( bDoLighting || FLASHLIGHT || SEAMLESS_BASE || SEAMLESS_DETAIL || LIGHTING_PREVIEW || g_bDecalOffset || CUBEMAP )
  115. {
  116. // The vertex only contains valid normals if they are actually needed (fetching them when absent makes D3D complain)
  117. DecompressVertex_Normal( v.vNormal, vNormal );
  118. }
  119. #if SEAMLESS_BASE || SEAMLESS_DETAIL
  120. // compute blend weights in rgb
  121. float3 NNormal=normalize( vNormal );
  122. o.SeamlessWeights.xyz = NNormal * NNormal; // sums to 1.
  123. #endif
  124. #if !defined( SHADER_MODEL_VS_3_0 ) || !MORPHING
  125. ApplyMorph( v.vPosFlex, v.vNormalFlex, vPosition.xyz, vNormal );
  126. #else
  127. ApplyMorph( morphSampler, cMorphTargetTextureDim, cMorphSubrect,
  128. v.vVertexID, v.vTexCoord2, vPosition.xyz, vNormal );
  129. #endif
  130. // Perform skinning
  131. float3 worldNormal, worldPos;
  132. SkinPositionAndNormal(
  133. g_bSkinning,
  134. vPosition, vNormal,
  135. v.vBoneWeights, v.vBoneIndices,
  136. worldPos, worldNormal );
  137. if ( !g_bVertexColor )
  138. {
  139. worldNormal = normalize( worldNormal );
  140. }
  141. #if defined( SHADER_MODEL_VS_3_0 ) && MORPHING && DECAL
  142. // Avoid z precision errors
  143. worldPos += worldNormal * 0.05f * v.vTexCoord2.z;
  144. #endif
  145. o.worldSpaceNormal = worldNormal;
  146. // Transform into projection space
  147. float4 vProjPos = mul( float4( worldPos, 1 ), cViewProj );
  148. o.projPos = vProjPos;
  149. vProjPos.z = dot( float4( worldPos, 1 ), cViewProjZ );
  150. o.vProjPos = vProjPos;
  151. o.fogFactorW.w = CalcFog( worldPos, vProjPos, g_FogType );
  152. #if !defined( _X360 )
  153. o.fog = o.fogFactorW.w;
  154. #endif
  155. o.worldPos_ProjPosZ.xyz = worldPos.xyz;
  156. o.worldPos_ProjPosZ.w = vProjPos.z;
  157. // Needed for cubemaps
  158. #if CUBEMAP
  159. o.worldVertToEyeVector.xyz = VSHADER_VECT_SCALE * (cEyePos - worldPos);
  160. #endif
  161. #if !defined (_X360) && FLASHLIGHT
  162. o.color = float4( 0.0f, 0.0f, 0.0f, 0.0f );
  163. #else
  164. if ( g_bVertexColor )
  165. {
  166. // Assume that this is unlitgeneric if you are using vertex color.
  167. o.color.rgb = ( DONT_GAMMA_CONVERT_VERTEX_COLOR ) ? v.vColor.rgb : GammaToLinear( v.vColor.rgb );
  168. o.color.a = v.vColor.a;
  169. }
  170. else
  171. {
  172. #if ( ( USE_STATIC_CONTROL_FLOW ) || defined ( SHADER_MODEL_VS_3_0 ) )
  173. {
  174. o.color.xyz = DoLighting( worldPos, worldNormal, v.vSpecular, bStaticLight, bDynamicLight, g_bHalfLambert );
  175. }
  176. #else
  177. {
  178. o.color.xyz = DoLightingUnrolled( worldPos, worldNormal, v.vSpecular, bStaticLight, bDynamicLight, g_bHalfLambert, NUM_LIGHTS );
  179. }
  180. #endif
  181. }
  182. #endif
  183. #if SEAMLESS_BASE
  184. o.SeamlessTexCoord.xyz = SEAMLESS_SCALE * v.vPos.xyz;
  185. #else
  186. // Base texture coordinates
  187. o.baseTexCoord.x = dot( v.vTexCoord0, cBaseTexCoordTransform[0] );
  188. o.baseTexCoord.y = dot( v.vTexCoord0, cBaseTexCoordTransform[1] );
  189. #endif
  190. #if SEAMLESS_DETAIL
  191. // FIXME: detail texcoord as a 2d xform doesn't make much sense here, so I just do enough so
  192. // that scale works. More smartness could allow 3d xform.
  193. o.SeamlessDetailTexCoord.xyz = (SEAMLESS_SCALE*cDetailTexCoordTransform[0].x) * v.vPos.xyz;
  194. #else
  195. // Detail texture coordinates
  196. // FIXME: This shouldn't have to be computed all the time.
  197. o.detailTexCoord.x = dot( v.vTexCoord0, cDetailTexCoordTransform[0] );
  198. o.detailTexCoord.y = dot( v.vTexCoord0, cDetailTexCoordTransform[1] );
  199. #endif
  200. #if SEPARATE_DETAIL_UVS
  201. o.detailTexCoord.xy = v.vTexCoord1.xy;
  202. #endif
  203. #if LIGHTING_PREVIEW
  204. float dot=0.5+0.5*worldNormal*float3(0.7071,0.7071,0);
  205. o.color.xyz=float3(dot,dot,dot);
  206. #endif
  207. #if defined ( _X360 ) && FLASHLIGHT
  208. o.flashlightSpacePos = mul( float4( worldPos, 1.0f ), g_FlashlightWorldToTexture );
  209. #endif
  210. return o;
  211. }