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.

237 lines
7.1 KiB

  1. //===================== Copyright (c) Valve Corporation. All Rights Reserved. ======================
  2. // STATIC: "ENVMAP_MASK" "0..1"
  3. // STATIC: "TANGENTSPACE" "0..1"
  4. // STATIC: "BUMPMAP" "0..1"
  5. // STATIC: "DETAILTEXTURE" "0..1"
  6. // STATIC: "VERTEXCOLOR" "0..1"
  7. // STATIC: "SEAMLESS" "0..1"
  8. // diffuse bump map is always true when bumpmapping is enabled, so just set it to 1
  9. #define DIFFUSEBUMPMAP 1
  10. // STATIC: "FLASHLIGHT" "0..1" [CONSOLE]
  11. // STATIC: "SELFILLUM" "0..1"
  12. // STATIC: "LIGHTING_PREVIEW" "0..3" [PC]
  13. // STATIC: "LIGHTING_PREVIEW" "0..0" [CONSOLE]
  14. // DYNAMIC: "FASTPATH" "0..1"
  15. // SKIP: $LIGHTING_PREVIEW && $FLASHLIGHT
  16. #include "common_fog_vs_supportsvertexfog_fxc.h"
  17. #include "common_vs_fxc.h"
  18. #include "common_4wayblend_fxc.h"
  19. static const int g_FogType = DOWATERFOG;
  20. static const bool g_UseSeparateEnvmapMask = ENVMAP_MASK;
  21. static const bool g_bTangentSpace = TANGENTSPACE;
  22. static const bool g_bBumpmap = BUMPMAP;
  23. static const bool g_bBumpmapDiffuseLighting = DIFFUSEBUMPMAP;
  24. static const bool g_bVertexColor = VERTEXCOLOR;
  25. #if SEAMLESS
  26. const float4 SeamlessScale : register( SHADER_SPECIFIC_CONST_0 );
  27. #define SEAMLESS_SCALE (SeamlessScale.x)
  28. #else
  29. const float4 cBaseTexCoordTransform[2] : register( SHADER_SPECIFIC_CONST_0 );
  30. const float4 cDetailOrBumpTexCoordTransform[2] : register( SHADER_SPECIFIC_CONST_2 );
  31. #endif
  32. // This should be identity if we are bump mapping, otherwise we'll screw up the lightmapTexCoordOffset.
  33. const float4 cEnvmapMaskTexCoordTransform[2] : register( SHADER_SPECIFIC_CONST_4 );
  34. #if FLASHLIGHT
  35. const float4x4 g_FlashlightWorldToTexture : register( SHADER_SPECIFIC_CONST_6 );
  36. #endif
  37. #if DETAILTEXTURE && BUMPMAP && !SELFILLUM
  38. const float4 cBumpTexCoordTransform[2] : register( SHADER_SPECIFIC_CONST_10 ); // not contiguous with the rest!
  39. #endif
  40. #if ( LIGHTING_PREVIEW == 3 )
  41. const float4 g_vEyeVector : register( SHADER_SPECIFIC_CONST_12 );
  42. #endif
  43. struct VS_INPUT
  44. {
  45. float3 vPos : POSITION;
  46. float4 vNormal : NORMAL;
  47. float2 vBaseTexCoord : TEXCOORD0;
  48. float2 vLightmapTexCoord : TEXCOORD1;
  49. float2 vLightmapTexCoordOffset : TEXCOORD2;
  50. float3 vTangentS : TANGENT;
  51. float3 vTangentT : BINORMAL;
  52. float4 vColor : COLOR0;
  53. float4 vVertexBlends : COLOR1;
  54. };
  55. VS_OUTPUT main( const VS_INPUT v )
  56. {
  57. VS_OUTPUT o = ( VS_OUTPUT )0;
  58. float3 vObjNormal;
  59. DecompressVertex_Normal( v.vNormal, vObjNormal );
  60. float4 projPos;
  61. float3 worldPos;
  62. worldPos = mul4x3( float4( v.vPos, 1 ), cModel[0] );
  63. projPos = mul( float4( worldPos, 1 ), cViewProj );
  64. o.projPos = projPos;
  65. #ifdef _PS3
  66. // Account for OpenGL's flipped y coordinate and expanded z range [-1,1] instead of [0,1]
  67. o.projPos.y = -o.projPos.y;
  68. o.projPos.z = 2.0f * o.projPos.z - o.projPos.w;
  69. #endif // _PS3
  70. o.worldPos_projPosZ.w = projPos.z;
  71. o.worldPos_projPosZ.xyz = worldPos;
  72. #if ( LIGHTING_PREVIEW == 3 )
  73. o.worldPos_projPosZ.w = dot( g_vEyeVector, worldPos.xyz - cEyePos.xyz ); // Linear depth
  74. #endif
  75. float3 worldNormal = mul3x3( vObjNormal, ( float3x3 )cModel[0] );
  76. #if TANGENTSPACE || LIGHTING_PREVIEW || defined( _X360 ) || defined( _PS3 )
  77. float3 worldTangentS = mul3x3( v.vTangentS, ( const float3x3 )cModel[0] );
  78. float3 worldTangentT = mul3x3( v.vTangentT, ( const float3x3 )cModel[0] );
  79. #if SEAMLESS && BUMPMAP && ( defined( _X360 ) || defined( _PS3 ) )
  80. float3 n = normalize( worldNormal );
  81. float3 n2 = n * n; // sums to 1.
  82. o.tangentSpaceTranspose[0] = normalize( float3( n2.y + n2.z, 0.0f, n2.x ) );
  83. o.tangentSpaceTranspose[1] = normalize( float3( 0.0f, n2.x + n2.z, n2.y ) );
  84. o.tangentSpaceTranspose[2] = worldNormal;
  85. #else
  86. o.tangentSpaceTranspose[0] = worldTangentS;
  87. o.tangentSpaceTranspose[1] = worldTangentT;
  88. o.tangentSpaceTranspose[2] = worldNormal;
  89. #endif
  90. #endif
  91. float3 worldVertToEyeVector = VSHADER_VECT_SCALE * (cEyePos - worldPos);
  92. #if ( SEAMLESS )
  93. {
  94. // we need to fill in the texture coordinate projections
  95. o.SeamlessTexCoord_fogFactorW.xyz = SEAMLESS_SCALE * worldPos;
  96. }
  97. #else
  98. {
  99. if ( FASTPATH )
  100. {
  101. o.baseTexCoord_fogFactorZ.xy = v.vBaseTexCoord;
  102. }
  103. else
  104. {
  105. o.baseTexCoord_fogFactorZ.x = dot( v.vBaseTexCoord.xy, cBaseTexCoordTransform[0].xy ) + cBaseTexCoordTransform[0].w;
  106. o.baseTexCoord_fogFactorZ.y = dot( v.vBaseTexCoord.xy, cBaseTexCoordTransform[1].xy ) + cBaseTexCoordTransform[1].w;
  107. }
  108. // calculate detailorbumptexcoord
  109. if ( FASTPATH )
  110. {
  111. o.DETAILORBUMPCOORDS = v.vBaseTexCoord.xy;
  112. }
  113. else
  114. {
  115. o.DETAILORBUMPCOORDS = float2( dot( v.vBaseTexCoord.xy, cDetailOrBumpTexCoordTransform[0].xy ) + cDetailOrBumpTexCoordTransform[0].w,
  116. dot( v.vBaseTexCoord.xy, cDetailOrBumpTexCoordTransform[1].xy ) + cDetailOrBumpTexCoordTransform[1].w );
  117. }
  118. }
  119. #endif
  120. if ( FASTPATH )
  121. {
  122. o.lightmapTexCoord3.zw = v.vBaseTexCoord;
  123. #if DETAILTEXTURE && BUMPMAP && !SELFILLUM
  124. o.BUMPCOORDS = v.vBaseTexCoord;
  125. #endif
  126. }
  127. else
  128. {
  129. #if DETAILTEXTURE && BUMPMAP && !SELFILLUM
  130. o.BUMPCOORDS = float2( dot( v.vBaseTexCoord.xy, cBumpTexCoordTransform[0].xy ) + cBumpTexCoordTransform[0].w,
  131. dot( v.vBaseTexCoord.xy, cBumpTexCoordTransform[1].xy ) + cBumpTexCoordTransform[1].w );
  132. #endif
  133. }
  134. // compute lightmap coordinates
  135. if( g_bBumpmap && g_bBumpmapDiffuseLighting )
  136. {
  137. o.lightmapTexCoord1And2.xy = v.vLightmapTexCoord + v.vLightmapTexCoordOffset;
  138. float2 lightmapTexCoord2 = o.lightmapTexCoord1And2.xy + v.vLightmapTexCoordOffset;
  139. float2 lightmapTexCoord3 = lightmapTexCoord2 + v.vLightmapTexCoordOffset;
  140. // reversed component order
  141. o.lightmapTexCoord1And2.w = lightmapTexCoord2.x;
  142. o.lightmapTexCoord1And2.z = lightmapTexCoord2.y;
  143. o.lightmapTexCoord3.xy = lightmapTexCoord3;
  144. }
  145. else
  146. {
  147. o.lightmapTexCoord1And2.xy = v.vLightmapTexCoord;
  148. }
  149. if( g_UseSeparateEnvmapMask )
  150. {
  151. // reversed component order
  152. #if FASTPATH
  153. o.ENVMAPMASKCOORDS = v.vBaseTexCoord.xy;
  154. #else
  155. o.ENVMAPMASKCOORDS = float2( dot( v.vBaseTexCoord.xy, cEnvmapMaskTexCoordTransform[0].xy ) + cEnvmapMaskTexCoordTransform[0].w,
  156. dot( v.vBaseTexCoord.xy, cEnvmapMaskTexCoordTransform[1].xy ) + cEnvmapMaskTexCoordTransform[1].w );
  157. #endif
  158. }
  159. #if ( HARDWAREFOGBLEND )
  160. {
  161. o.fog = CalcFixedFunctionFog( worldPos, g_FogType );
  162. }
  163. #endif
  164. #if ( !DOPIXELFOG && !HARDWAREFOGBLEND )
  165. {
  166. #if ( SEAMLESS )
  167. {
  168. o.SeamlessTexCoord_fogFactorW.w = CalcNonFixedFunctionFog( worldPos, g_FogType );
  169. }
  170. #else
  171. {
  172. o.baseTexCoord_fogFactorZ.z = CalcNonFixedFunctionFog( worldPos, g_FogType );
  173. }
  174. #endif
  175. }
  176. #endif
  177. if ( !g_bVertexColor )
  178. {
  179. o.vertexColor = float4( 1.0f, 1.0f, 1.0f, cModulationColor.a );
  180. }
  181. else
  182. {
  183. o.vertexColor = v.vColor;
  184. o.vertexColor.a *= cModulationColor.a;
  185. }
  186. #if SEAMLESS
  187. // compute belnd weights in rgb
  188. float3 vNormal=normalize( worldNormal );
  189. o.vertexColor.xyz = vNormal * vNormal; // sums to 1.
  190. #endif
  191. // On 360/PS3, we have extra iterators and can fold the flashlight into this shader
  192. #if ( defined( _X360 ) || defined( _PS3 ) ) && FLASHLIGHT
  193. o.flashlightSpacePos = TransformFlashlightWorldToTexture( worldPos, g_FlashlightWorldToTexture );
  194. o.vProjPos = projPos;
  195. #endif
  196. o.vertexBlend = v.vVertexBlends;
  197. return o;
  198. }