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.

244 lines
8.9 KiB

  1. //========== Copyright (c) Valve Corporation, All rights reserved. ==========//
  2. // STATIC: "HALFLAMBERT" "0..1"
  3. // STATIC: "FLASHLIGHT" "0..1"
  4. // STATIC: "LIGHTWARPTEXTURE" "0..1"
  5. // STATIC: "WORLD_NORMAL" "0..0" [vs20] [PC]
  6. // STATIC: "WORLD_NORMAL" "0..1" [vs30] [PC]
  7. // STATIC: "WORLD_NORMAL" "0..0" [CONSOLE]
  8. // DYNAMIC: "COMPRESSED_VERTS" "0..1"
  9. // DYNAMIC: "SKINNING" "0..1"
  10. #include "common_fog_vs_fxc.h"
  11. // DYNAMIC: "DYNAMIC_LIGHT" "0..1"
  12. // DYNAMIC: "NUM_LIGHTS" "0..4"
  13. // DYNAMIC: "TESSELLATION" "0..0" [vs30] [PC]
  14. // DYNAMIC: "TESSELLATION" "0..0" [CONSOLE]
  15. // DYNAMIC: "TESSELLATION" "0..0" [vs20] [PC]
  16. // DYNAMIC: "MORPHING" "0..0" [ = false ]
  17. // SKIP: ( $MORPHING || $SKINNING || $COMPRESSED_VERTS ) && $TESSELLATION
  18. #include "vortwarp_vs20_helper.h"
  19. static const bool g_bSkinning = SKINNING ? true : false;
  20. static const int g_iFogType = DOWATERFOG;
  21. static const bool g_bHalfLambert = HALFLAMBERT ? true : false;
  22. const float3 g_cEyeOrigin : register( SHADER_SPECIFIC_CONST_0 );
  23. const float4 g_vIrisProjectionU : register( SHADER_SPECIFIC_CONST_2 );
  24. const float4 g_vIrisProjectionV : register( SHADER_SPECIFIC_CONST_3 );
  25. const float4 g_vFlashlightPosition : register( SHADER_SPECIFIC_CONST_4 );
  26. #if ( WORLD_NORMAL )
  27. const float4 g_vEyeVector : register( SHADER_SPECIFIC_CONST_5 );
  28. #endif
  29. const float4x4 g_vFlashlightMatrix : register( SHADER_SPECIFIC_CONST_6 ); // 6 & 7 & 8 & 9
  30. #ifdef SHADER_MODEL_VS_3_0
  31. // NOTE: cMorphTargetTextureDim.xy = target dimensions,
  32. // cMorphTargetTextureDim.z = 4tuples/morph
  33. const float3 cMorphTargetTextureDim : register( SHADER_SPECIFIC_CONST_10 );
  34. const float4 cMorphSubrect : register( SHADER_SPECIFIC_CONST_11 );
  35. sampler2D morphSampler : register( s0 );
  36. #endif
  37. #if TESSELLATION
  38. #include "tessellation_vs_fxc.h"
  39. const float4 g_SubDControls : register( SHADER_SPECIFIC_CONST_1 );
  40. sampler2D BezierSampler : register( s1 );
  41. sampler2D DispSampler : register( s2 );
  42. // VS_INPUT defined in header
  43. #else // no TESSELLATION
  44. struct VS_INPUT
  45. {
  46. float4 vPos : POSITION; // Position
  47. float4 vBoneWeights : BLENDWEIGHT; // Skin weights
  48. float4 vBoneIndices : BLENDINDICES; // Skin indices
  49. float4 vTexCoord0 : TEXCOORD0; // Base (sclera) texture coordinates
  50. // Position deltas
  51. float3 vPosFlex : POSITION1;
  52. #ifdef SHADER_MODEL_VS_3_0
  53. float vVertexID : POSITION2;
  54. #endif
  55. };
  56. #endif // TESSELLATION
  57. struct VS_OUTPUT
  58. {
  59. float4 projPos : POSITION; // Projection-space position
  60. #if !defined( _X360 ) && !defined( SHADER_MODEL_VS_3_0 )
  61. float fog : FOG; // Fixed-function fog factor
  62. #endif
  63. float4 vAmbientOcclUv_fallbackCorneaUv : TEXCOORD0; // Base texture coordinate
  64. float4 cVertexLight : TEXCOORD1; // Vertex-lit color (Note: w is used for flashlight pass)
  65. float4 vTangentViewVector : TEXCOORD2; // Tangent view vector (Note: w is used for flashlight pass)
  66. float4 vWorldPosition_ProjPosZ : TEXCOORD3;
  67. float3 vWorldNormal : TEXCOORD4; // World-space normal
  68. float3 vWorldTangent : TEXCOORD5; // World-space tangent
  69. float4 vLightFalloffCosine01 : TEXCOORD6; // Light falloff and cosine terms for first two local lights
  70. float4 vLightFalloffCosine23 : TEXCOORD7; // Light falloff and cosine terms for next two local lights
  71. float3 vWorldBinormal : COLOR0; // World-space normal
  72. };
  73. VS_OUTPUT main( const VS_INPUT v )
  74. {
  75. VS_OUTPUT o;
  76. bool bDynamicLight = DYNAMIC_LIGHT ? true : false;
  77. float2 baseTexCoords;
  78. float3 vWorldPosition;
  79. #if ( TESSELLATION )
  80. {
  81. float3 dummyNormal;
  82. float2 patchCoords;
  83. EvaluateSubdivisionSurface( v, g_SubDControls.x, g_SubDControls.y, g_SubDControls.z, BezierSampler, DispSampler,
  84. dummyNormal, vWorldPosition, baseTexCoords, patchCoords );
  85. }
  86. #else // not tessellating
  87. {
  88. float4 vPosition = v.vPos;
  89. baseTexCoords = v.vTexCoord0.xy;
  90. #if ( !defined( SHADER_MODEL_VS_3_0 ) || !MORPHING )
  91. {
  92. ApplyMorph( v.vPosFlex, vPosition.xyz );
  93. }
  94. #else
  95. {
  96. ApplyMorph( morphSampler, cMorphTargetTextureDim, cMorphSubrect, v.vVertexID, float3( 0, 0, 0 ), vPosition.xyz );
  97. }
  98. #endif
  99. // Transform the position
  100. SkinPosition( g_bSkinning, vPosition, v.vBoneWeights, v.vBoneIndices, vWorldPosition );
  101. }
  102. #endif // TESSELLATION
  103. // Note: I'm relying on the iris projection vector math not changing or this will break
  104. float3 vEyeSocketUpVector = normalize( -g_vIrisProjectionV.xyz );
  105. float3 vEyeSocketLeftVector = normalize( -g_vIrisProjectionU.xyz );
  106. o.vWorldPosition_ProjPosZ.xyz = vWorldPosition.xyz;
  107. // Transform into projection space
  108. float4 vProjPos = mul( float4( vWorldPosition, 1.0f ), cViewProj );
  109. #if defined( _X360 )
  110. {
  111. // Without the isolate keyword here the output position for the flashlight pass is slightly behind the position of the main pass
  112. // and the flashlight pass will be z-culled.
  113. [isolate] o.projPos = vProjPos;
  114. }
  115. #else
  116. {
  117. o.projPos = vProjPos;
  118. }
  119. #endif
  120. #ifdef _PS3
  121. // Account for OpenGL's flipped y coordinate and expanded z range [-1,1] instead of [0,1]
  122. o.projPos.y = -o.projPos.y;
  123. o.projPos.z = 2.0f * o.projPos.z - o.projPos.w;
  124. #endif // _PS3
  125. #if ( WORLD_NORMAL )
  126. o.vWorldPosition_ProjPosZ.w = dot( g_vEyeVector, vWorldPosition.xyz - cEyePos.xyz ); // Linear depth
  127. #else
  128. o.vWorldPosition_ProjPosZ.w = vProjPos.z;
  129. #endif
  130. #if !defined( _X360 ) && !defined( SHADER_MODEL_VS_3_0 )
  131. o.fog = CalcFixedFunctionFog( vWorldPosition, g_iFogType );
  132. #endif
  133. // Normal = (Pos - Eye origin)
  134. float3 vWorldNormal = normalize( vWorldPosition.xyz - g_cEyeOrigin.xyz );
  135. o.vWorldNormal.xyz = vWorldNormal.xyz;
  136. // Tangent & binormal
  137. float3 vWorldTangent = normalize( cross( vEyeSocketUpVector.xyz, vWorldNormal.xyz ) );
  138. o.vWorldTangent.xyz = vWorldTangent.xyz;
  139. float3 vWorldBinormal = normalize( cross( vWorldNormal.xyz, vWorldTangent.xyz ) );
  140. o.vWorldBinormal.xyz = vWorldBinormal.xyz * 0.5f + 0.5f;
  141. float3 vWorldViewVector = normalize (vWorldPosition.xyz - cEyePos.xyz);
  142. o.vTangentViewVector.xyz = Vec3WorldToTangentNormalized (vWorldViewVector.xyz, vWorldNormal.xyz, vWorldTangent.xyz, vWorldBinormal.xyz);
  143. // AV - I think this will effectively make the eyeball less rounded left to right to help vertex lighting quality
  144. // AV - Note: This probably won't look good if put on an exposed eyeball
  145. //float vNormalDotSideVec = -dot( vWorldNormal, g_vEyeballUp ) * 0.5f;
  146. float vNormalDotSideVec = -dot( vWorldNormal, vEyeSocketLeftVector) * 0.5f;
  147. float3 vBentWorldNormal = normalize(vNormalDotSideVec * vEyeSocketLeftVector + vWorldNormal);
  148. // Compute vertex lighting
  149. o.cVertexLight.a = 0.0f; //Only used for flashlight pass
  150. o.cVertexLight.rgb = DoLightingUnrolled( vWorldPosition, vBentWorldNormal, float3(0.0f, 0.0f, 0.0f), false, bDynamicLight, g_bHalfLambert, NUM_LIGHTS );
  151. // Only interpolate ambient light for TF NPR lighting
  152. bool bDoDiffuseWarp = LIGHTWARPTEXTURE ? true : false;
  153. if ( bDoDiffuseWarp )
  154. {
  155. if( bDynamicLight )
  156. {
  157. o.cVertexLight.rgb = AmbientLight( vBentWorldNormal.xyz );
  158. }
  159. else
  160. {
  161. o.cVertexLight.rgb = float3( 0.0f, 0.0f, 0.0f );
  162. }
  163. }
  164. // Light falloff for first two local lights
  165. o.vLightFalloffCosine01.x = VertexAttenInternal( vWorldPosition.xyz, 0 );
  166. o.vLightFalloffCosine01.y = VertexAttenInternal( vWorldPosition.xyz, 1 );
  167. o.vLightFalloffCosine01.z = CosineTermInternal( vWorldPosition.xyz, vWorldNormal.xyz, 0, g_bHalfLambert );
  168. o.vLightFalloffCosine01.w = CosineTermInternal( vWorldPosition.xyz, vWorldNormal.xyz, 1, g_bHalfLambert );
  169. // Light falloff for next two local lights
  170. o.vLightFalloffCosine23.x = VertexAttenInternal( vWorldPosition.xyz, 2 );
  171. o.vLightFalloffCosine23.y = VertexAttenInternal( vWorldPosition.xyz, 3 );
  172. o.vLightFalloffCosine23.z = CosineTermInternal( vWorldPosition.xyz, vWorldNormal.xyz, 2, g_bHalfLambert );
  173. o.vLightFalloffCosine23.w = CosineTermInternal( vWorldPosition.xyz, vWorldNormal.xyz, 3, g_bHalfLambert );
  174. // Texture coordinates set by artists for ambient occlusion
  175. o.vAmbientOcclUv_fallbackCorneaUv.xy = baseTexCoords;
  176. // Cornea uv for ps.2.0 fallback
  177. float2 vCorneaUv; // Note: Cornea texture is a cropped version of the iris texture
  178. vCorneaUv.x = dot( g_vIrisProjectionU, float4( vWorldPosition, 1.0f ) );
  179. vCorneaUv.y = dot( g_vIrisProjectionV, float4( vWorldPosition, 1.0f ) );
  180. float2 vSphereUv = ( vCorneaUv.xy * 0.5f ) + 0.25f;
  181. o.vAmbientOcclUv_fallbackCorneaUv.wz = vCorneaUv.xy; // Note: wz unpacks faster than zw in ps.2.0!
  182. // Step on the vertex light interpolator for the flashlight tex coords
  183. bool bFlashlight = ( FLASHLIGHT != 0 ) ? true : false;
  184. o.vTangentViewVector.w = 0.0f;
  185. if ( bFlashlight )
  186. {
  187. o.cVertexLight = mul( float4( vWorldPosition.xyz, 1.0f ), g_vFlashlightMatrix );
  188. o.vTangentViewVector.w = saturate( dot( vBentWorldNormal.xyz, normalize ( g_vFlashlightPosition.xyz - vWorldPosition.xyz ) ) ); // Flashlight N.L with modified normal
  189. // Half lambert version
  190. //o.cVertexLight.z = dot( vBentWorldNormal.xyz, normalize ( g_vFlashlightPosition.xyz - vWorldPosition.xyz ) ); // Flashlight N.L with modified normal
  191. //o.cVertexLight.z = ( o.cVertexLight.z * 0.5f ) + 0.5f;
  192. //o.cVertexLight.z *= o.cVertexLight.z;
  193. }
  194. return o;
  195. }