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.

316 lines
11 KiB

  1. //========== Copyright (c) Valve Corporation, All rights reserved. ==========//
  2. // STATIC: "SFM" "0..0" [vs20] [PC]
  3. // STATIC: "SFM" "0..1" [vs30] [PC]
  4. // STATIC: "SFM" "0..0" [CONSOLE]
  5. // STATIC: "DECAL" "0..1" [vs30]
  6. // STATIC: "WORLD_NORMAL" "0..0" [vs20] [PC]
  7. // STATIC: "WORLD_NORMAL" "0..1" [vs30] [PC]
  8. // STATIC: "WORLD_NORMAL" "0..0" [CONSOLE]
  9. // STATIC: "FLATTEN_STATIC_CONTROL_FLOW" "0..1" [vs20] [PC]
  10. // STATIC: "FLATTEN_STATIC_CONTROL_FLOW" "0..0" [CONSOLE]
  11. // STATIC: "CASCADED_SHADOW_MAPPING" "0..1" [vs20] [PC]
  12. // STATIC: "CASCADED_SHADOW_MAPPING" "0..0" [vs30] [PC]
  13. // DYNAMIC: "CSM_VIEWMODELQUALITY" "0..1" [vs20] [PC]
  14. // DYNAMIC: "CSM_VIEWMODELQUALITY" "0..0" [vs30] [PC]
  15. // DYNAMIC: "COMPRESSED_VERTS" "0..1"
  16. // DYNAMIC: "SKINNING" "0..1"
  17. // DYNAMIC: "MORPHING" "0..0" [ = false ]
  18. // DYNAMIC: "TESSELLATION" "0..0" [vs30] [PC]
  19. // DYNAMIC: "TESSELLATION" "0..0" [vs20] [PC]
  20. // DYNAMIC: "TESSELLATION" "0..0" [CONSOLE]
  21. // DYNAMIC: "NUM_LIGHTS" "0..2" [vs20] [PC]
  22. // DYNAMIC: "NUM_LIGHTS" "0..0" [vs20] [CONSOLE]
  23. // DYNAMIC: "STATICLIGHT3" "0..1"
  24. // SKIP: ( $SFM == 0 ) && ( $MORPHING )
  25. // SKIP: ( $SFM == 0 ) && ( $TESSELLATION )
  26. // If using static control flow on Direct3D, we should use the NUM_LIGHTS=0 combo
  27. // SKIP: ( $FLATTEN_STATIC_CONTROL_FLOW == 0 ) && ( $NUM_LIGHTS > 0 ) [vs20] [PC]
  28. // SKIP: ( $MORPHING || $SKINNING || $COMPRESSED_VERTS ) && $TESSELLATION
  29. #include "common_fog_vs_supportsvertexfog_fxc.h"
  30. #include "common_vs_fxc.h"
  31. static const bool g_bSkinning = SKINNING ? true : false;
  32. static const int g_FogType = DOWATERFOG;
  33. const float4 cBaseTexCoordTransform[2] : register( SHADER_SPECIFIC_CONST_0 );
  34. const float4 cBumpTexCoordTransform[2] : register( SHADER_SPECIFIC_CONST_2 );
  35. const float4 cDetailTexCoordTransform[2] : register( SHADER_SPECIFIC_CONST_4 );
  36. #if ( MORPHING )
  37. // NOTE: cMorphTargetTextureDim.xy = target dimensions,
  38. // cMorphTargetTextureDim.z = 4tuples/morph
  39. const float3 cMorphTargetTextureDim : register( SHADER_SPECIFIC_CONST_6 );
  40. const float4 cMorphSubrect : register( SHADER_SPECIFIC_CONST_7 );
  41. sampler2D morphSampler : register( s0 );
  42. #endif
  43. const float4 g_vEyeVector : register( SHADER_SPECIFIC_CONST_8 );
  44. const float2 g_teamIdMinMax : register( SHADER_SPECIFIC_CONST_10 );
  45. #if ( CASCADED_SHADOW_MAPPING )
  46. const float4x4 g_matWorldToShadowTexMatrices[2] : register( SHADER_SPECIFIC_CONST_CSM_0 );
  47. const float4 g_matWorldToShadowTexMatrix0_z : register( SHADER_SPECIFIC_CONST_CSM_1 );
  48. #endif
  49. #if TESSELLATION
  50. // VS_INPUT defined in header
  51. #include "tessellation_vs_fxc.h"
  52. const float4 g_SubDControls : register( SHADER_SPECIFIC_CONST_9 );
  53. sampler2D BezierSampler : register( s1 );
  54. sampler2D DispSampler : register( s2 );
  55. #else // no TESSELLATION
  56. struct VS_INPUT
  57. {
  58. // This is all of the stuff that we ever use.
  59. float4 vPos : POSITION;
  60. float4 vBoneWeights : BLENDWEIGHT;
  61. float4 vBoneIndices : BLENDINDICES;
  62. float4 vNormal : NORMAL;
  63. float4 vColor : COLOR0;
  64. float4 vStaticLight : COLOR1;
  65. #if STATICLIGHT3
  66. float4 vStaticLight2 : COLOR2;
  67. float4 vStaticLight3 : COLOR3;
  68. #endif
  69. // make these float2's and stick the [n n 0 1] in the dot math.
  70. float4 vTexCoord0 : TEXCOORD0;
  71. float4 vTexCoord1 : TEXCOORD1;
  72. float4 vTexCoord2 : TEXCOORD2;
  73. float4 vTexCoord3 : TEXCOORD3;
  74. float3 vTangentS : TANGENT;
  75. float3 vTangentT : BINORMAL;
  76. float4 vUserData : TANGENT;
  77. // Position and normal/tangent deltas
  78. float4 vPosFlex : POSITION1;
  79. float4 vNormalFlex : NORMAL1;
  80. #if ( SFM )
  81. float vVertexID : POSITION2;
  82. #endif
  83. };
  84. #endif // TESSELLATION
  85. struct VS_OUTPUT
  86. {
  87. // Stuff that isn't seen by the pixel shader
  88. float4 projPos : POSITION;
  89. // Stuff that is seen by the pixel shader
  90. float4 baseTexCoord_baseTexCoord2 : TEXCOORD0; // Base texture coordinates in .xy, 2nd (decal) uv set in .zw
  91. float4 bumpTexCoord_detailTexCoord : TEXCOORD1;
  92. float4 vWorldNormal_flTeamIdFade : TEXCOORD2;
  93. float4 vWorldTangent : TEXCOORD3;
  94. float4 vWorldPos_vertexFogFactor : TEXCOORD4;
  95. float4 vProjPos_fWrinkleWeight : TEXCOORD5;
  96. #if defined( SHADER_MODEL_VS_3_0 )
  97. float4 staticLight0 : TEXCOORD6; // STATICLIGHT3 output basis 0 in .xyz, sun amount (if applicable) in .w
  98. float4 staticLight1 : TEXCOORD7; // STATICLIGHT3 output basis 1 in .xyz
  99. float4 staticLight2 : TEXCOORD8; // STATICLIGHT3 output basis 2 in .xyz
  100. float4 lightAtten : TEXCOORD9; // light attenuation for 4 lights
  101. #else
  102. float4 csmXform0or1_csmXform2 : TEXCOORD6;
  103. float4 staticLight_csmXform0z : TEXCOORD7; // STATICLIGHT3 average rgb in .xyz, csm lightToWorldxformcascade0.z in .w
  104. float4 lightAtten : COLOR0; // light attenuation for lights 0 and 1 in .xy (lower precision for sm2_b/OSX), sun amount (if applicable) in .w
  105. #endif
  106. };
  107. float RemapValClamped( float val, float A, float B, float C, float D)
  108. {
  109. float cVal = (val - A) / (B - A);
  110. cVal = saturate( cVal );
  111. return C + (D - C) * cVal;
  112. }
  113. //-----------------------------------------------------------------------------
  114. // Main shader entry point
  115. //-----------------------------------------------------------------------------
  116. VS_OUTPUT main( const VS_INPUT v )
  117. {
  118. VS_OUTPUT o = ( VS_OUTPUT )0;
  119. float4 vPosition, vTangent, vTexCoords;
  120. float3 vNormal, worldPos, worldNormal, worldTangentS, worldTangentT;
  121. #if TESSELLATION
  122. {
  123. float flBiTangentSign;
  124. EvaluateSubdivisionSurface( v, g_SubDControls.x, g_SubDControls.y, g_SubDControls.z, BezierSampler, DispSampler,
  125. worldNormal, worldPos, worldTangentS, worldTangentT, flBiTangentSign,
  126. o.vProjPos_fWrinkleWeight.w, o.baseTexCoord_baseTexCoord2.xy, o.bumpTexCoord_detailTexCoord.zw );
  127. }
  128. #else // not tessellating
  129. {
  130. vPosition = v.vPos;
  131. DecompressVertex_NormalTangent( v.vNormal, v.vUserData, vNormal, vTangent );
  132. #if ( !MORPHING )
  133. {
  134. ApplyMorph( v.vPosFlex, v.vNormalFlex.xyz, vPosition.xyz, vNormal, vTangent.xyz, o.vProjPos_fWrinkleWeight.w );
  135. }
  136. #else
  137. {
  138. ApplyMorph( morphSampler, cMorphTargetTextureDim, cMorphSubrect, v.vVertexID, v.vTexCoord2,
  139. vPosition.xyz, vNormal, vTangent.xyz, o.vProjPos_fWrinkleWeight.w );
  140. }
  141. #endif
  142. // Perform skinning
  143. SkinPositionNormalAndTangentSpace( g_bSkinning, vPosition, vNormal, vTangent,
  144. v.vBoneWeights, v.vBoneIndices, worldPos,
  145. worldNormal, worldTangentS, worldTangentT );
  146. // Always normalize since flex path can denormalize
  147. worldNormal = normalize( worldNormal );
  148. worldTangentS = normalize( worldTangentS );
  149. worldTangentT = normalize( worldTangentT );
  150. #if ( MORPHING && DECAL && !WORLD_NORMAL )
  151. {
  152. // Avoid z precision errors
  153. worldPos += worldNormal * 0.05f * v.vTexCoord2.z;
  154. }
  155. #endif
  156. #if ( STATICLIGHT3 )
  157. {
  158. // for phong, to start with we'll try only baking indirect light from vrad - so that we don't lose the ability to acquire phong highlights
  159. // from map lights (including the sun/csm casting light) - direct lights will still come through as dynamic lights!
  160. #if defined( SHADER_MODEL_VS_3_0 )
  161. // Adjust lighting data (static lighting in linear space * cOverbright)
  162. o.staticLight0.rgb = GammaToLinear( v.vStaticLight.rgb * cOverbright );
  163. o.staticLight1.rgb = GammaToLinear( v.vStaticLight2.rgb * cOverbright );
  164. o.staticLight2.rgb = GammaToLinear( v.vStaticLight3.rgb * cOverbright );
  165. o.staticLight0.a = v.vStaticLight.a;
  166. o.staticLight1.a = v.vStaticLight2.a;
  167. o.staticLight2.a = v.vStaticLight3.a;
  168. #else
  169. // take average of STATICLIGHT3 data for shader model 2b (OSX)
  170. // Adjust lighting data (static lighting in linear space * cOverbright)
  171. o.staticLight_csmXform0z.rgb = GammaToLinear( v.vStaticLight.rgb * cOverbright );
  172. o.staticLight_csmXform0z.rgb += GammaToLinear( v.vStaticLight2.rgb * cOverbright );
  173. o.staticLight_csmXform0z.rgb += GammaToLinear( v.vStaticLight3.rgb * cOverbright );
  174. o.staticLight_csmXform0z.rgb *= 0.3333f;
  175. o.lightAtten.w = ( v.vStaticLight.a + v.vStaticLight2.a + v.vStaticLight3.a ) * 0.3333f;
  176. #endif
  177. }
  178. #endif
  179. vTexCoords = v.vTexCoord0;
  180. // Base texture coordinate transform
  181. o.baseTexCoord_baseTexCoord2.x = dot( vTexCoords, cBaseTexCoordTransform[0] );
  182. o.baseTexCoord_baseTexCoord2.y = dot( vTexCoords, cBaseTexCoordTransform[1] );
  183. // 2nd uv set, pass through for now
  184. o.baseTexCoord_baseTexCoord2.zw = v.vTexCoord1.xy;
  185. // bump texture coordinate transform - temp
  186. o.bumpTexCoord_detailTexCoord.x = dot( v.vTexCoord0, cBumpTexCoordTransform[0] );
  187. o.bumpTexCoord_detailTexCoord.y = dot( v.vTexCoord0, cBumpTexCoordTransform[1] );
  188. // Detail texture coordinate transform
  189. o.bumpTexCoord_detailTexCoord.z = dot( v.vTexCoord0, cDetailTexCoordTransform[0] );
  190. o.bumpTexCoord_detailTexCoord.w = dot( v.vTexCoord0, cDetailTexCoordTransform[1] );
  191. }
  192. #endif
  193. #if ( !DOPIXELFOG )
  194. {
  195. o.vWorldPos_vertexFogFactor.w = CalcNonFixedFunctionFog( worldPos, g_FogType );
  196. }
  197. #endif
  198. // Needed for water fog alpha and diffuse lighting
  199. // FIXME: we shouldn't have to compute this all the time.
  200. o.vWorldPos_vertexFogFactor.xyz = worldPos;
  201. // Compute lighting attenuations
  202. #if ( defined ( SHADER_MODEL_VS_2_0 ) && FLATTEN_STATIC_CONTROL_FLOW )
  203. {
  204. #if ( NUM_LIGHTS > 0 )
  205. o.lightAtten.x = GetVertexAttenForLight( worldPos.xyz, 0, false );
  206. #endif
  207. #if ( NUM_LIGHTS > 1 )
  208. o.lightAtten.y = GetVertexAttenForLight( worldPos.xyz, 1, false );
  209. #endif
  210. }
  211. #else
  212. {
  213. o.lightAtten.x = GetVertexAttenForLight( worldPos, 0 );
  214. o.lightAtten.y = GetVertexAttenForLight( worldPos, 1 );
  215. #if defined( SHADER_MODEL_VS_3_0 )
  216. o.lightAtten.z = GetVertexAttenForLight( worldPos, 2 );
  217. o.lightAtten.w = GetVertexAttenForLight( worldPos, 3 );
  218. #endif
  219. }
  220. #endif
  221. // Tangent space transform
  222. o.vWorldNormal_flTeamIdFade.xyz = worldNormal.xyz;
  223. o.vWorldTangent = float4( worldTangentS.xyz, vTangent.w ); // Propagate binormal sign in world tangent.w
  224. // Transform into projection space
  225. o.projPos = mul( float4( worldPos, 1 ), cViewProj );
  226. #ifdef _PS3
  227. // Account for OpenGL's flipped y coordinate and expanded z range [-1,1] instead of [0,1]
  228. o.projPos.y = -o.projPos.y;
  229. o.projPos.z = 2.0f * o.projPos.z - o.projPos.w;
  230. #endif // _PS3
  231. o.vProjPos_fWrinkleWeight.xyz = o.projPos.xyz;
  232. #if ( WORLD_NORMAL )
  233. {
  234. o.vProjPos_fWrinkleWeight.z = dot( g_vEyeVector, worldPos.xyz - cEyePos.xyz ); // Linear depth
  235. }
  236. #endif
  237. // Don't have to worry about hardware blend here since we never do that with phong.
  238. #if ( !DOPIXELFOG )
  239. {
  240. o.vWorldPos_vertexFogFactor.w = CalcNonFixedFunctionFog( worldPos, g_FogType );
  241. }
  242. #endif
  243. o.vWorldNormal_flTeamIdFade.w = RemapValClamped( distance( cEyePos, worldPos.xyz ), g_teamIdMinMax.x, g_teamIdMinMax.y, 0.0, 1.0 );
  244. #if( CASCADED_SHADOW_MAPPING )
  245. {
  246. float4 worldPos = float4( o.vWorldPos_vertexFogFactor.xyz, 1.0f );
  247. #if CSM_VIEWMODELQUALITY
  248. o.csmXform0or1_csmXform2.xy = mul( worldPos, g_matWorldToShadowTexMatrices[0] ).xy;
  249. #else
  250. o.csmXform0or1_csmXform2.xy = mul( worldPos, g_matWorldToShadowTexMatrices[0] ).xy;
  251. o.csmXform0or1_csmXform2.zw = mul( worldPos, g_matWorldToShadowTexMatrices[1] ).xy;
  252. #endif
  253. o.staticLight_csmXform0z.w = mul( worldPos, g_matWorldToShadowTexMatrix0_z );
  254. }
  255. #endif
  256. return o;
  257. }