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.

293 lines
10 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..2"
  6. // STATIC: "VERTEXCOLOR" "0..1"
  7. // STATIC: "VERTEXALPHATEXBLENDFACTOR" "0..1"
  8. // STATIC: "SEAMLESS" "0..1"
  9. // disable seamless & bumpmask (does not appear to be used)
  10. // STATIC: "BUMPMASK" "0..0"
  11. // diffuse bump map is always true when bumpmapping is enabled, so just set it to 1
  12. #define DIFFUSEBUMPMAP 1
  13. // STATIC: "FLASHLIGHT" "0..1" [CONSOLE]
  14. // STATIC: "FANCY_BLENDING" "0..1"
  15. // STATIC: "SELFILLUM" "0..1"
  16. // STATIC: "LIGHTING_PREVIEW" "0..1" [PC]
  17. // STATIC: "LIGHTING_PREVIEW" "0..0" [CONSOLE]
  18. // STATIC: "PAINT" "0..1"
  19. // STATIC: "ADDBUMPMAPS" "0..1"
  20. // DYNAMIC: "FASTPATH" "0..1"
  21. // SKIP: $BUMPMASK && $ADDBUMPMAPS
  22. // SKIP: !$BUMPMAP && $ADDBUMPMAPS
  23. // This should not be a combo since I'm a moron with the tangent space and the flashlight.
  24. // SKIP: $BUMPMASK && $SEAMLESS
  25. // SKIP: $LIGHTING_PREVIEW && $FLASHLIGHT
  26. #include "common_fog_vs_supportsvertexfog_fxc.h"
  27. #include "common_vs_fxc.h"
  28. #include "common_lightmappedgeneric_fxc.h"
  29. static const int g_FogType = DOWATERFOG;
  30. static const bool g_UseSeparateEnvmapMask = ENVMAP_MASK;
  31. static const bool g_bTangentSpace = TANGENTSPACE;
  32. static const bool g_bBumpmap = BUMPMAP;
  33. static const bool g_bBumpmapDiffuseLighting = DIFFUSEBUMPMAP;
  34. static const bool g_bVertexColor = VERTEXCOLOR;
  35. static const bool g_bVertexAlphaTexBlendFactor = VERTEXALPHATEXBLENDFACTOR;
  36. static const bool g_BumpMask = BUMPMASK;
  37. #if SEAMLESS
  38. const float4 SeamlessScale : register( SHADER_SPECIFIC_CONST_0 );
  39. #define SEAMLESS_SCALE (SeamlessScale.x)
  40. #else
  41. const float4 cBaseTexCoordTransform[2] : register( SHADER_SPECIFIC_CONST_0 );
  42. const float4 cDetailTexCoordTransform[2] : register( SHADER_SPECIFIC_CONST_2 );
  43. #endif
  44. #if FLASHLIGHT
  45. const float4x4 g_FlashlightWorldToTexture : register( SHADER_SPECIFIC_CONST_6 );
  46. #endif
  47. const float4 cBumpTexCoordTransform[2] : register( SHADER_SPECIFIC_CONST_10 );
  48. #if ( ( LIGHTING_PREVIEW == 3 ) || PAINT )
  49. const float4 g_vEyeVector : register( SHADER_SPECIFIC_CONST_12 );
  50. #endif
  51. #if ( DETAILTEXTURE == 2 )
  52. #define SHADER_SPECIFIC_CONST_13 c217 // TODO: add this to common_vs_fxc.h and fix up case (pain) of vcs filenames when all the other shaders are forced to compile as a result
  53. const float4 cDetailTexCoordTransform2[2] : register( SHADER_SPECIFIC_CONST_13 ); // not contiguous with the rest!
  54. #endif
  55. #if ( SHADER_MODEL_VS_3_0 )
  56. #define SHADER_SPECIFIC_CONST_14 c219 // TODO: add this to common_vs_fxc.h and fix up case (pain) of vcs filenames when all the other shaders are forced to compile as a result
  57. #define SHADER_SPECIFIC_CONST_15 c221
  58. #define SHADER_SPECIFIC_CONST_16 c223
  59. const float4 cBaseTexCoordTransform2[2] : register( SHADER_SPECIFIC_CONST_14 );
  60. const float4 cBumpTexCoordTransform2[2] : register( SHADER_SPECIFIC_CONST_15 );
  61. const float4 cBlendModulateTexCoordTransform[2] : register( SHADER_SPECIFIC_CONST_16 );
  62. #endif
  63. struct VS_INPUT
  64. {
  65. float3 vPos : POSITION;
  66. float4 vNormal : NORMAL;
  67. float2 vBaseTexCoord : TEXCOORD0;
  68. float2 vLightmapTexCoord : TEXCOORD1;
  69. float2 vLightmapTexCoordOffset : TEXCOORD2;
  70. float3 vTangentS : TANGENT;
  71. float3 vTangentT : BINORMAL;
  72. float4 vColor : COLOR0;
  73. };
  74. VS_OUTPUT main( const VS_INPUT v )
  75. {
  76. VS_OUTPUT o = ( VS_OUTPUT )0;
  77. float3 vObjNormal;
  78. DecompressVertex_Normal( v.vNormal, vObjNormal );
  79. float4 projPos;
  80. float3 worldPos;
  81. worldPos = mul4x3( float4( v.vPos, 1 ), cModel[0] );
  82. projPos = mul( float4( worldPos, 1 ), cViewProj );
  83. o.projPos = projPos;
  84. #ifdef _PS3
  85. // Account for OpenGL's flipped y coordinate and expanded z range [-1,1] instead of [0,1]
  86. o.projPos.y = -o.projPos.y;
  87. o.projPos.z = 2.0f * o.projPos.z - o.projPos.w;
  88. #endif // _PS3
  89. o.worldPos_projPosZ.w = projPos.z;
  90. o.worldPos_projPosZ.xyz = worldPos;
  91. #if ( LIGHTING_PREVIEW == 3 )
  92. o.worldPos_projPosZ.w = dot( g_vEyeVector, worldPos.xyz - cEyePos.xyz ); // Linear depth
  93. #endif
  94. float3 worldNormal = mul3x3( vObjNormal, ( float3x3 )cModel[0] );
  95. #if TANGENTSPACE || PAINT || LIGHTING_PREVIEW || defined( _X360 ) || defined( _PS3 )
  96. float3 worldTangentS = mul3x3( v.vTangentS, ( const float3x3 )cModel[0] );
  97. float3 worldTangentT = mul3x3( v.vTangentT, ( const float3x3 )cModel[0] );
  98. #if SEAMLESS && BUMPMAP && ( defined( _X360 ) || defined( _PS3 ) )
  99. float3 n = normalize( worldNormal );
  100. float3 n2 = n * n; // sums to 1.
  101. o.tangentSpaceTranspose0_vertexBlendX.xyz = normalize( float3( n2.y + n2.z, 0.0f, n2.x ) );
  102. o.tangentSpaceTranspose1_bumpTexCoord2u.xyz = normalize( float3( 0.0f, n2.x + n2.z, n2.y ) );
  103. o.tangentSpaceTranspose2_bumpTexCoord2v.xyz = worldNormal;
  104. #else
  105. o.tangentSpaceTranspose0_vertexBlendX.xyz = worldTangentS;
  106. o.tangentSpaceTranspose1_bumpTexCoord2u.xyz = worldTangentT;
  107. o.tangentSpaceTranspose2_bumpTexCoord2v.xyz = worldNormal;
  108. #endif
  109. #if 0 //PAINT //should try to do this in the vs but the lack of tesselation on world geo makes the interpolation yucky
  110. float3x3 matCameraFaceUVs;
  111. float3 fvViewDirection = normalize( g_vEyeVector.xyz - worldPos.xyz );
  112. float3 fvTangentViewDirection;
  113. fvTangentViewDirection.x = dot( fvViewDirection, worldTangentS );
  114. fvTangentViewDirection.y = dot( fvViewDirection, worldTangentT );
  115. fvTangentViewDirection.z = dot( fvViewDirection, worldNormal );
  116. matCameraFaceUVs[2] = fvTangentViewDirection; // new normal
  117. matCameraFaceUVs[1] = float3( 0.0f, 1.0f, 0.0f ); // tangent
  118. matCameraFaceUVs[0] = normalize( cross( matCameraFaceUVs[1].xyz, matCameraFaceUVs[2].xyz ) ); //binormal
  119. matCameraFaceUVs[1] = normalize( cross( matCameraFaceUVs[2].xyz, matCameraFaceUVs[0].xyz ) ); //re-square tangent
  120. o.detailOrBumpAndEnvmapMaskTexCoord = float4( matCameraFaceUVs[0].xy, matCameraFaceUVs[1].xy );
  121. #endif
  122. #else
  123. // Surface normal (required for Phong in absence of tangentspace)
  124. o.tangentSpaceTranspose2_bumpTexCoord2v.xyz = worldNormal;
  125. #endif
  126. float3 worldVertToEyeVector = VSHADER_VECT_SCALE * (cEyePos - worldPos);
  127. #if ( SEAMLESS )
  128. {
  129. // we need to fill in the texture coordinate projections
  130. o.SeamlessTexCoord.xyz = SEAMLESS_SCALE * worldPos;
  131. }
  132. #else
  133. {
  134. if ( FASTPATH )
  135. {
  136. o.BASETEXCOORD = v.vBaseTexCoord;
  137. #if defined( SHADER_MODEL_VS_3_0 )
  138. o.BASETEXCOORD2 = v.vBaseTexCoord;
  139. #endif
  140. }
  141. else
  142. {
  143. o.BASETEXCOORD = float2( dot( v.vBaseTexCoord.xy, cBaseTexCoordTransform[0].xy ) + cBaseTexCoordTransform[0].w,
  144. dot( v.vBaseTexCoord.xy, cBaseTexCoordTransform[1].xy ) + cBaseTexCoordTransform[1].w );
  145. #if defined( SHADER_MODEL_VS_3_0 )
  146. o.BASETEXCOORD2 = float2( dot( v.vBaseTexCoord.xy, cBaseTexCoordTransform2[0].xy ) + cBaseTexCoordTransform2[0].w,
  147. dot( v.vBaseTexCoord.xy, cBaseTexCoordTransform2[1].xy ) + cBaseTexCoordTransform2[1].w );
  148. #endif
  149. }
  150. }
  151. #endif
  152. if ( FASTPATH )
  153. {
  154. #if BUMPMAP && !SELFILLUM && !PAINT
  155. o.BUMPCOORD = v.vBaseTexCoord;
  156. o.BUMPCOORD2U = v.vBaseTexCoord.x;
  157. o.BUMPCOORD2V = v.vBaseTexCoord.y;
  158. #endif
  159. #if ( DETAILTEXTURE )
  160. o.DETAILCOORD = v.vBaseTexCoord;
  161. #if ( DETAILTEXTURE == 2 ) && defined( SHADER_MODEL_VS_3_0 )
  162. o.DETAILCOORD2 = v.vBaseTexCoord;
  163. #endif
  164. #endif
  165. #if FANCY_BLENDING && !PAINT && !SEAMLESS
  166. o.BLENDMODULATECOORD = v.vBaseTexCoord;
  167. #endif
  168. }
  169. else
  170. {
  171. #if BUMPMAP && !SELFILLUM && !PAINT
  172. o.BUMPCOORD = float2( dot( v.vBaseTexCoord.xy, cBumpTexCoordTransform[0].xy ) + cBumpTexCoordTransform[0].w,
  173. dot( v.vBaseTexCoord.xy, cBumpTexCoordTransform[1].xy ) + cBumpTexCoordTransform[1].w );
  174. #if defined( SHADER_MODEL_VS_3_0 )
  175. o.BUMPCOORD2U = dot( v.vBaseTexCoord.xy, cBumpTexCoordTransform2[0].xy ) + cBumpTexCoordTransform2[0].w;
  176. o.BUMPCOORD2V = dot( v.vBaseTexCoord.xy, cBumpTexCoordTransform2[1].xy ) + cBumpTexCoordTransform2[1].w;
  177. #endif
  178. #endif
  179. #if FANCY_BLENDING && !PAINT && defined( SHADER_MODEL_VS_3_0 ) && ( SEAMLESS == 0 )
  180. o.BLENDMODULATECOORD = float2( dot( v.vBaseTexCoord.xy, cBlendModulateTexCoordTransform[0].xy ) + cBlendModulateTexCoordTransform[0].w,
  181. dot( v.vBaseTexCoord.xy, cBlendModulateTexCoordTransform[1].xy ) + cBlendModulateTexCoordTransform[1].w );
  182. #endif
  183. #if ( DETAILTEXTURE ) && ( SEAMLESS == 0 )
  184. o.DETAILCOORD = float2( dot( v.vBaseTexCoord.xy, cDetailTexCoordTransform[0].xy ) + cDetailTexCoordTransform[0].w,
  185. dot( v.vBaseTexCoord.xy, cDetailTexCoordTransform[1].xy ) + cDetailTexCoordTransform[1].w );
  186. #if ( DETAILTEXTURE == 2 ) && defined( SHADER_MODEL_VS_3_0 )
  187. o.DETAILCOORD2 = float2( dot( v.vBaseTexCoord.xy, cDetailTexCoordTransform2[0].xy ) + cDetailTexCoordTransform2[0].w,
  188. dot( v.vBaseTexCoord.xy, cDetailTexCoordTransform2[1].xy ) + cDetailTexCoordTransform2[1].w );
  189. #endif
  190. #endif
  191. }
  192. // compute lightmap coordinates
  193. if( g_bBumpmap && g_bBumpmapDiffuseLighting )
  194. {
  195. o.lightmapTexCoord1And2.xy = v.vLightmapTexCoord + v.vLightmapTexCoordOffset;
  196. float2 lightmapTexCoord2 = o.lightmapTexCoord1And2.xy + v.vLightmapTexCoordOffset;
  197. float2 lightmapTexCoord3 = lightmapTexCoord2 + v.vLightmapTexCoordOffset;
  198. // reversed component order
  199. o.lightmapTexCoord1And2.w = lightmapTexCoord2.x;
  200. o.lightmapTexCoord1And2.z = lightmapTexCoord2.y;
  201. o.lightmapTexCoord3_bumpTexCoord.xy = lightmapTexCoord3;
  202. }
  203. else
  204. {
  205. o.lightmapTexCoord1And2.xy = v.vLightmapTexCoord;
  206. }
  207. #if !ADDBUMPMAPS
  208. if( g_UseSeparateEnvmapMask || g_BumpMask )
  209. #endif
  210. {
  211. // transform(s) performed in PS, pass in untransformed base coords
  212. o.ENVMAPMASKCOORD = v.vBaseTexCoord.xy;
  213. }
  214. if ( !g_bVertexColor )
  215. {
  216. o.vertexColor = float4( 1.0f, 1.0f, 1.0f, cModulationColor.a );
  217. }
  218. else
  219. {
  220. if ( g_bVertexAlphaTexBlendFactor )
  221. {
  222. o.vertexColor.rgb = v.vColor.rgb;
  223. o.vertexColor.a = cModulationColor.a;
  224. }
  225. else
  226. {
  227. o.vertexColor = v.vColor;
  228. o.vertexColor.a *= cModulationColor.a;
  229. }
  230. }
  231. #if SEAMLESS
  232. // compute belnd weights in rgb
  233. float3 vNormal=normalize( worldNormal );
  234. o.vertexColor.xyz = vNormal * vNormal; // sums to 1.
  235. #endif
  236. // On 360/PS3, we have extra iterators and can fold the flashlight into this shader
  237. #if ( defined( _X360 ) || defined( _PS3 ) ) && FLASHLIGHT
  238. o.flashlightSpacePos = TransformFlashlightWorldToTexture( worldPos, g_FlashlightWorldToTexture );
  239. o.vProjPos = projPos;
  240. #endif
  241. if ( g_bVertexAlphaTexBlendFactor )
  242. {
  243. o.tangentSpaceTranspose0_vertexBlendX.w = v.vColor.a;
  244. }
  245. return o;
  246. }