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.

148 lines
5.0 KiB

  1. //========== Copyright (c) Valve Corporation, All rights reserved. ==========//
  2. // STATIC: "FLATTEN_STATIC_CONTROL_FLOW" "0..1" [vs20] [PC]
  3. // STATIC: "FLATTEN_STATIC_CONTROL_FLOW" "0..0" [CONSOLE]
  4. // DYNAMIC: "COMPRESSED_VERTS" "0..1"
  5. // DYNAMIC: "SKINNING" "0..1"
  6. // DYNAMIC: "NUM_LIGHTS" "0..2" [vs20] [PC]
  7. // DYNAMIC: "NUM_LIGHTS" "0..0" [vs20] [CONSOLE]
  8. // If using static control flow on Direct3D, we should use the NUM_LIGHTS=0 combo
  9. // SKIP: ( $FLATTEN_STATIC_CONTROL_FLOW == 0 ) && ( $NUM_LIGHTS > 0 ) [vs20] [PC]
  10. // Includes
  11. #include "common_vs_fxc.h"
  12. // Globals
  13. const float g_flUVScale : register( SHADER_SPECIFIC_CONST_0 );
  14. const float3 g_vUVProjOffset : register( SHADER_SPECIFIC_CONST_1 );
  15. const float3 g_vBBMin : register( SHADER_SPECIFIC_CONST_2 );
  16. const float3 g_vBBMax : register( SHADER_SPECIFIC_CONST_3 );
  17. // Structs
  18. struct VS_INPUT
  19. {
  20. float4 vPos : POSITION; // Position
  21. float4 vNormal : NORMAL; // Normal
  22. float4 vBoneWeights : BLENDWEIGHT; // Skin weights
  23. float4 vBoneIndices : BLENDINDICES; // Skin indices
  24. float4 vClosestSurfaceDir : TEXCOORD0;
  25. float4 vTangent : TANGENT;
  26. };
  27. struct VS_OUTPUT
  28. {
  29. float4 vProjPosition : POSITION; // Projection-space position
  30. float4 vWorldNormal : TEXCOORD0; // w is proj. z coord (for depth stuff)
  31. float4 vClosestSurfaceDir : TEXCOORD1;
  32. float4 vWorldPos : TEXCOORD2; // w is proj. w coord
  33. float4 vUV0 : TEXCOORD3;
  34. float4 vUV1 : TEXCOORD4;
  35. float4 vLightAtten : TEXCOORD5;
  36. float3 vAmbient : TEXCOORD6;
  37. };
  38. float3 CubeUV( float3 vWorldPosition, float3 vWorldNormal, float3 vUVSpacePos )
  39. {
  40. // Generate texcoords for a cubemap wrapped around the ice sculpture's bounding box
  41. float3 vDir = normalize( lerp( normalize( vUVSpacePos.xyz ), vWorldNormal.xyz, 0.5 ) );
  42. float3 tMins = ( g_vBBMin - vWorldPosition ) / vDir;
  43. float3 tMaxs = ( g_vBBMax - vWorldPosition ) / vDir;
  44. tMaxs = max( tMaxs, tMins ); // weed out negative t's per axis
  45. float t = min( tMaxs.x, min( tMaxs.y, tMaxs.z ) );
  46. float3 vIntersectionPos = vWorldPosition + t * vDir;
  47. // transform into unit cube
  48. float3 vCubeUV = vIntersectionPos;
  49. vCubeUV -= 0.5*( g_vBBMax + g_vBBMin );
  50. vCubeUV /= 0.5*( g_vBBMax - g_vBBMin );
  51. vCubeUV = normalize( vCubeUV );
  52. return vCubeUV;
  53. }
  54. // Main
  55. VS_OUTPUT main( const VS_INPUT i )
  56. {
  57. VS_OUTPUT o;
  58. float4 vObjPosition = i.vPos;
  59. float4 vObjTangent = i.vTangent;
  60. float3 vObjNormal;
  61. DecompressVertex_Normal( i.vNormal, vObjNormal );
  62. // Transform the position and normal
  63. float3 vWorldPosition = { 0.0f, 0.0f, 0.0f };
  64. float3 vWorldNormal = { 0.0f, 0.0f, 0.0f };
  65. float3 vWorldTangent = { 0.0f, 0.0f, 0.0f };
  66. float3 vWorldBinormal = { 0.0f, 0.0f, 0.0f };
  67. SkinPositionNormalAndTangentSpace( SKINNING ? true : false,
  68. vObjPosition, vObjNormal.xyz, vObjTangent.xyzw,
  69. i.vBoneWeights, i.vBoneIndices,
  70. vWorldPosition, vWorldNormal, vWorldTangent, vWorldBinormal );
  71. vWorldNormal.xyz = normalize( vWorldNormal.xyz );
  72. o.vWorldNormal.xyz = vWorldNormal.xyz;
  73. o.vWorldPos.xyz = vWorldPosition.xyz;
  74. // Transform into projection space
  75. float4 vProjPosition = mul( float4( vWorldPosition, 1.0f ), cViewProj );
  76. o.vProjPosition = vProjPosition;
  77. o.vWorldNormal.w = vProjPosition.z;
  78. o.vWorldPos.w = vProjPosition.w;
  79. #ifdef _PS3
  80. // Account for OpenGL's flipped y coordinate and expanded z range [-1,1] instead of [0,1]
  81. o.vProjPosition.y = -o.vProjPosition.y;
  82. o.vProjPosition.z = 2.0f * o.vProjPosition.z - o.vProjPosition.w;
  83. #endif // _PS3
  84. // Seamless texturing UV projections
  85. float3 vUVSpacePos = vWorldPosition.xyz - g_vUVProjOffset.xyz;
  86. o.vUV0.xy = g_flUVScale * vUVSpacePos.yz;
  87. o.vUV0.wz = g_flUVScale * vUVSpacePos.xz;
  88. o.vUV1.xy = g_flUVScale * vUVSpacePos.xy;
  89. // Cubemap UV gen
  90. //o.vCubeUVW.xyz = CubeUV( vWorldPosition, vWorldNormal, vUVSpacePos );
  91. // Screen-space texcoord
  92. float2 vScreenPos = float2( 0.5, -0.5 ) * vProjPosition.xy + 0.5 * vProjPosition.w;
  93. o.vUV1.wz = vScreenPos;
  94. // Dynamic light attenuation factors
  95. #if ( defined( SHADER_MODEL_VS_2_0 ) && FLATTEN_STATIC_CONTROL_FLOW )
  96. {
  97. o.vLightAtten.xyzw = float4(0,0,0,0);
  98. #if ( NUM_LIGHTS > 0 )
  99. o.vLightAtten.x = GetVertexAttenForLight( vWorldPosition, 0, false );
  100. #endif
  101. #if ( NUM_LIGHTS > 1 )
  102. o.vLightAtten.y = GetVertexAttenForLight( vWorldPosition, 1, false );
  103. #endif
  104. #if ( NUM_LIGHTS > 2 )
  105. o.vLightAtten.z = GetVertexAttenForLight( vWorldPosition, 2, false );
  106. #endif
  107. #if ( NUM_LIGHTS > 3 )
  108. o.vLightAtten.w = GetVertexAttenForLight( vWorldPosition, 3, false );
  109. #endif
  110. }
  111. #else
  112. {
  113. o.vLightAtten.x = GetVertexAttenForLight( vWorldPosition, 0 );
  114. o.vLightAtten.y = GetVertexAttenForLight( vWorldPosition, 1 );
  115. o.vLightAtten.z = GetVertexAttenForLight( vWorldPosition, 2 );
  116. o.vLightAtten.w = GetVertexAttenForLight( vWorldPosition, 3 );
  117. }
  118. #endif
  119. // Ambient light
  120. o.vAmbient = AmbientLight( vWorldNormal );
  121. // Closest surface's direction and distance, for contact shadowing
  122. o.vClosestSurfaceDir.xyz = i.vClosestSurfaceDir.xyz;
  123. o.vClosestSurfaceDir.w = length( i.vClosestSurfaceDir.xyz );
  124. return o;
  125. }