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.

384 lines
14 KiB

  1. #ifndef COMMON_VERTEXLITGENERIC_DX9_H_
  2. #define COMMON_VERTEXLITGENERIC_DX9_H_
  3. #include "common_ps_fxc.h"
  4. // We store four light colors and positions in an
  5. // array of three of these structures like so:
  6. //
  7. // x y z w
  8. // +------+------+------+------+
  9. // | L0.rgb | |
  10. // +------+------+------+ |
  11. // | L0.pos | L3 |
  12. // +------+------+------+ rgb |
  13. // | L1.rgb | |
  14. // +------+------+------+------+
  15. // | L1.pos | |
  16. // +------+------+------+ |
  17. // | L2.rgb | L3 |
  18. // +------+------+------+ pos |
  19. // | L2.pos | |
  20. // +------+------+------+------+
  21. //
  22. struct PixelShaderLightInfo
  23. {
  24. float4 color;
  25. float4 pos;
  26. };
  27. #define cOverbright 2.0f
  28. #define cOOOverbright 0.5f
  29. #define LIGHTTYPE_NONE 0
  30. #define LIGHTTYPE_SPOT 1
  31. #define LIGHTTYPE_POINT 2
  32. #define LIGHTTYPE_DIRECTIONAL 3
  33. // Better suited to Pixel shader models, 11 instructions in pixel shader
  34. float3 PixelShaderAmbientLight( const float3 worldNormal, const float3 cAmbientCube[6] )
  35. {
  36. float3 linearColor, nSquared = worldNormal * worldNormal;
  37. float3 isNegative = ( worldNormal < 0.0 );
  38. float3 isPositive = 1-isNegative;
  39. isNegative *= nSquared;
  40. isPositive *= nSquared;
  41. linearColor = isPositive.x * cAmbientCube[0] + isNegative.x * cAmbientCube[1] +
  42. isPositive.y * cAmbientCube[2] + isNegative.y * cAmbientCube[3] +
  43. isPositive.z * cAmbientCube[4] + isNegative.z * cAmbientCube[5];
  44. return linearColor;
  45. }
  46. // Better suited to Vertex shader models
  47. // Six VS instructions due to use of constant indexing (slt, mova, mul, mul, mad, mad)
  48. float3 VertexShaderAmbientLight( const float3 worldNormal, const float3 cAmbientCube[6] )
  49. {
  50. float3 nSquared = worldNormal * worldNormal;
  51. int3 isNegative = ( worldNormal < 0.0 );
  52. float3 linearColor;
  53. linearColor = nSquared.x * cAmbientCube[isNegative.x] +
  54. nSquared.y * cAmbientCube[isNegative.y+2] +
  55. nSquared.z * cAmbientCube[isNegative.z+4];
  56. return linearColor;
  57. }
  58. float3 AmbientLight( const float3 worldNormal, const float3 cAmbientCube[6] )
  59. {
  60. #if defined( SHADER_MODEL_VS_1_0 ) || defined( SHADER_MODEL_VS_1_1 ) || defined( SHADER_MODEL_VS_2_0 ) || defined( SHADER_MODEL_VS_3_0 )
  61. return VertexShaderAmbientLight( worldNormal, cAmbientCube );
  62. #else
  63. return PixelShaderAmbientLight( worldNormal, cAmbientCube );
  64. #endif
  65. }
  66. //-----------------------------------------------------------------------------
  67. // Purpose: Compute scalar diffuse term
  68. //-----------------------------------------------------------------------------
  69. float3 DiffuseTerm(const bool bHalfLambert, const float3 worldNormal, const float3 lightDir,
  70. const bool bDoLightingWarp, in sampler lightWarpSampler )
  71. {
  72. float fResult;
  73. float NDotL = dot( worldNormal, lightDir ); // Unsaturated dot (-1 to 1 range)
  74. if ( bHalfLambert )
  75. {
  76. fResult = saturate(NDotL * 0.5 + 0.5); // Scale and bias to 0 to 1 range
  77. if ( !bDoLightingWarp )
  78. {
  79. fResult *= fResult; // Square
  80. }
  81. }
  82. else
  83. {
  84. fResult = saturate( NDotL ); // Saturate pure Lambertian term
  85. fResult = SoftenCosineTerm( fResult ); // For CS:GO
  86. }
  87. float3 fOut = float3( fResult, fResult, fResult );
  88. if ( bDoLightingWarp )
  89. {
  90. fOut = tex1D( lightWarpSampler, fResult ).xyz;
  91. }
  92. return fOut;
  93. }
  94. float3 PixelShaderDoGeneralDiffuseLight( const float fAtten, const float3 worldPos, const float3 worldNormal,
  95. in samplerCUBE NormalizeSampler,
  96. const float3 vPosition, const float3 vColor, const bool bHalfLambert,
  97. const bool bDoLightingWarp, in sampler lightWarpSampler )
  98. {
  99. #if (defined(SHADER_MODEL_PS_2_B) || defined(SHADER_MODEL_PS_3_0))
  100. float3 lightDir = normalize( vPosition - worldPos );
  101. #else
  102. float3 lightDir = NormalizeWithCubemap( NormalizeSampler, vPosition - worldPos );
  103. #endif
  104. return vColor * fAtten * DiffuseTerm( bHalfLambert, worldNormal, lightDir,
  105. bDoLightingWarp, lightWarpSampler );
  106. }
  107. float3 PixelShaderGetLightVector( const float3 worldPos, PixelShaderLightInfo cLightInfo[3], int nLightIndex )
  108. {
  109. if ( nLightIndex == 3 )
  110. {
  111. // Unpack light 3 from w components...
  112. float3 vLight3Pos = float3( cLightInfo[1].pos.w, cLightInfo[2].color.w, cLightInfo[2].pos.w );
  113. return normalize( vLight3Pos - worldPos );
  114. }
  115. else
  116. {
  117. return normalize( cLightInfo[nLightIndex].pos.xyz - worldPos );
  118. }
  119. }
  120. float3 PixelShaderGetLightColor( PixelShaderLightInfo cLightInfo[3], int nLightIndex )
  121. {
  122. if ( nLightIndex == 3 )
  123. {
  124. // Unpack light 3 from w components...
  125. return float3( cLightInfo[0].color.w, cLightInfo[0].pos.w, cLightInfo[1].color.w );
  126. }
  127. else
  128. {
  129. return cLightInfo[nLightIndex].color.rgb;
  130. }
  131. }
  132. void SpecularAndRimTerms( const float3 vWorldNormal, const float3 vLightDir, const float fSpecularExponent, const float3 vEyeDir,
  133. const bool bDoSpecularWarp, in sampler specularWarpSampler, const float fFresnel,
  134. const float3 color, const bool bDoRimLighting, const float fRimExponent,
  135. // Outputs
  136. out float3 specularLighting, out float3 rimLighting )
  137. {
  138. float3 vHalfAngle = normalize( vEyeDir.xyz + vLightDir.xyz );
  139. float flNDotH = saturate( dot( vWorldNormal.xyz, vHalfAngle.xyz ) );
  140. specularLighting = pow( flNDotH, fSpecularExponent ); // Raise to specular exponent
  141. // Optionally warp as function of scalar specular and fresnel
  142. if ( bDoSpecularWarp )
  143. {
  144. specularLighting *= tex2D( specularWarpSampler, float2(specularLighting.x, fFresnel) ).rgb; // Sample at { (N.H)^k, fresnel }
  145. }
  146. specularLighting *= pow( saturate( dot( vWorldNormal, vLightDir ) ), 0.5 ); // Mask with N.L raised to a power
  147. specularLighting *= color; // Modulate with light color
  148. // Optionally do rim lighting
  149. rimLighting = float3( 0.0, 0.0, 0.0 );
  150. if ( bDoRimLighting )
  151. {
  152. rimLighting = pow( flNDotH, fRimExponent ); // Raise to rim exponent
  153. rimLighting *= saturate(dot( vWorldNormal, vLightDir )); // Mask with N.L
  154. rimLighting *= color; // Modulate with light color
  155. }
  156. }
  157. // Traditional fresnel term approximation
  158. float Fresnel( const float3 vNormal, const float3 vEyeDir )
  159. {
  160. float fresnel = 1-saturate( dot( vNormal, vEyeDir ) ); // 1-(N.V) for Fresnel term
  161. return fresnel * fresnel; // Square for a more subtle look
  162. }
  163. // Traditional fresnel term approximation which uses 4th power (square twice)
  164. float Fresnel4( const float3 vNormal, const float3 vEyeDir )
  165. {
  166. float fresnel = 1-saturate( dot( vNormal, vEyeDir ) ); // 1-(N.V) for Fresnel term
  167. fresnel = fresnel * fresnel; // Square
  168. return fresnel * fresnel; // Square again for a more subtle look
  169. }
  170. //
  171. // Custom Fresnel with low, mid and high parameters defining a piecewise continuous function
  172. // with traditional fresnel (0 to 1 range) as input. The 0 to 0.5 range blends between
  173. // low and mid while the 0.5 to 1 range blends between mid and high
  174. //
  175. // |
  176. // | . M . . . H
  177. // | .
  178. // L
  179. // |
  180. // +----------------
  181. // 0 1
  182. //
  183. float Fresnel( const float3 vNormal, const float3 vEyeDir, float3 vRanges )
  184. {
  185. float result, f = Fresnel( vNormal, vEyeDir ); // Traditional Fresnel
  186. if ( f > 0.5f )
  187. result = lerp( vRanges.y, vRanges.z, (2*f)-1 ); // Blend between mid and high values
  188. else
  189. result = lerp( vRanges.x, vRanges.y, 2*f ); // Blend between low and mid values
  190. return result;
  191. }
  192. void PixelShaderDoSpecularLight( const float3 vWorldPos, const float3 vWorldNormal, const float fSpecularExponent, const float3 vEyeDir,
  193. const float fAtten, const float3 vLightColor, const float3 vLightDir,
  194. const bool bDoSpecularWarp, in sampler specularWarpSampler, float fFresnel,
  195. const bool bDoRimLighting, const float fRimExponent,
  196. // Outputs
  197. out float3 specularLighting, out float3 rimLighting )
  198. {
  199. // Compute Specular and rim terms
  200. SpecularAndRimTerms( vWorldNormal, vLightDir, fSpecularExponent, vEyeDir,
  201. bDoSpecularWarp, specularWarpSampler, fFresnel, vLightColor * fAtten,
  202. bDoRimLighting, fRimExponent, specularLighting, rimLighting );
  203. }
  204. float3 PixelShaderDoLightingLinear( const float3 worldPos, const float3 worldNormal,
  205. const float3 staticLightingColor, const bool bStaticLight,
  206. const bool bAmbientLight, const float4 lightAtten, const float3 cAmbientCube[6],
  207. in samplerCUBE NormalizeSampler, const int nNumLights, PixelShaderLightInfo cLightInfo[3], const bool bHalfLambert,
  208. const bool bDoLightingWarp, in sampler lightWarpSampler, float flDirectShadow )
  209. {
  210. float3 linearColor = 0.0f;
  211. if ( bStaticLight )
  212. {
  213. // The static lighting comes in in gamma space and has also been premultiplied by $cOOOverbright
  214. // need to get it into
  215. // linear space so that we can do adds.
  216. linearColor += GammaToLinear( staticLightingColor * cOverbright );
  217. }
  218. if ( bAmbientLight )
  219. {
  220. linearColor += AmbientLight( worldNormal, cAmbientCube );
  221. }
  222. if ( nNumLights > 0 )
  223. {
  224. // First local light will always be forced to a directional light in CS:GO (see CanonicalizeMaterialLightingState() in shaderapidx8.cpp) - it may be completely black.
  225. linearColor += PixelShaderDoGeneralDiffuseLight( lightAtten.x, worldPos, worldNormal, NormalizeSampler,
  226. cLightInfo[0].pos.xyz, cLightInfo[0].color.rgb, bHalfLambert,
  227. bDoLightingWarp, lightWarpSampler ) * flDirectShadow;
  228. if ( nNumLights > 1 )
  229. {
  230. linearColor += PixelShaderDoGeneralDiffuseLight( lightAtten.y, worldPos, worldNormal, NormalizeSampler,
  231. cLightInfo[1].pos.xyz, cLightInfo[1].color.rgb, bHalfLambert,
  232. bDoLightingWarp, lightWarpSampler );
  233. if ( nNumLights > 2 )
  234. {
  235. linearColor += PixelShaderDoGeneralDiffuseLight( lightAtten.z, worldPos, worldNormal, NormalizeSampler,
  236. cLightInfo[2].pos.xyz, cLightInfo[2].color.rgb, bHalfLambert,
  237. bDoLightingWarp, lightWarpSampler );
  238. if ( nNumLights > 3 )
  239. {
  240. // Unpack the 4th light's data from tight constant packing
  241. float3 vLight3Color = float3( cLightInfo[0].color.w, cLightInfo[0].pos.w, cLightInfo[1].color.w );
  242. float3 vLight3Pos = float3( cLightInfo[1].pos.w, cLightInfo[2].color.w, cLightInfo[2].pos.w );
  243. linearColor += PixelShaderDoGeneralDiffuseLight( lightAtten.w, worldPos, worldNormal, NormalizeSampler,
  244. vLight3Pos, vLight3Color, bHalfLambert,
  245. bDoLightingWarp, lightWarpSampler );
  246. }
  247. }
  248. }
  249. }
  250. return linearColor;
  251. }
  252. void PixelShaderDoSpecularLighting( const float3 worldPos, const float3 worldNormal, const float fSpecularExponent, const float3 vEyeDir,
  253. const float4 lightAtten, const int nNumLights, PixelShaderLightInfo cLightInfo[3],
  254. const bool bDoSpecularWarp, in sampler specularWarpSampler, float fFresnel,
  255. const bool bDoRimLighting, const float fRimExponent, const float flDirectShadow,
  256. // Outputs
  257. out float3 specularLighting, out float3 rimLighting )
  258. {
  259. specularLighting = rimLighting = float3( 0.0f, 0.0f, 0.0f );
  260. float3 localSpecularTerm, localRimTerm;
  261. if( nNumLights > 0 )
  262. {
  263. // First local light will always be forced to a directional light in CS:GO (see CanonicalizeMaterialLightingState() in shaderapidx8.cpp) - it may be completely black.
  264. PixelShaderDoSpecularLight( worldPos, worldNormal, fSpecularExponent, vEyeDir,
  265. lightAtten.x, PixelShaderGetLightColor( cLightInfo, 0 ),
  266. PixelShaderGetLightVector( worldPos, cLightInfo, 0 ),
  267. bDoSpecularWarp, specularWarpSampler, fFresnel,
  268. bDoRimLighting, fRimExponent,
  269. localSpecularTerm, localRimTerm );
  270. specularLighting += localSpecularTerm * flDirectShadow; // Accumulate specular and rim terms
  271. rimLighting += localRimTerm * flDirectShadow;
  272. }
  273. if( nNumLights > 1 )
  274. {
  275. PixelShaderDoSpecularLight( worldPos, worldNormal, fSpecularExponent, vEyeDir,
  276. lightAtten.y, PixelShaderGetLightColor( cLightInfo, 1 ),
  277. PixelShaderGetLightVector( worldPos, cLightInfo, 1 ),
  278. bDoSpecularWarp, specularWarpSampler, fFresnel,
  279. bDoRimLighting, fRimExponent,
  280. localSpecularTerm, localRimTerm );
  281. specularLighting += localSpecularTerm; // Accumulate specular and rim terms
  282. rimLighting += localRimTerm;
  283. }
  284. if( nNumLights > 2 )
  285. {
  286. PixelShaderDoSpecularLight( worldPos, worldNormal, fSpecularExponent, vEyeDir,
  287. lightAtten.z, PixelShaderGetLightColor( cLightInfo, 2 ),
  288. PixelShaderGetLightVector( worldPos, cLightInfo, 2 ),
  289. bDoSpecularWarp, specularWarpSampler, fFresnel,
  290. bDoRimLighting, fRimExponent,
  291. localSpecularTerm, localRimTerm );
  292. specularLighting += localSpecularTerm; // Accumulate specular and rim terms
  293. rimLighting += localRimTerm;
  294. }
  295. if( nNumLights > 3 )
  296. {
  297. PixelShaderDoSpecularLight( worldPos, worldNormal, fSpecularExponent, vEyeDir,
  298. lightAtten.w, PixelShaderGetLightColor( cLightInfo, 3 ),
  299. PixelShaderGetLightVector( worldPos, cLightInfo, 3 ),
  300. bDoSpecularWarp, specularWarpSampler, fFresnel,
  301. bDoRimLighting, fRimExponent,
  302. localSpecularTerm, localRimTerm );
  303. specularLighting += localSpecularTerm; // Accumulate specular and rim terms
  304. rimLighting += localRimTerm;
  305. }
  306. }
  307. float3 PixelShaderDoRimLighting( const float3 worldNormal, const float3 vEyeDir, const float3 cAmbientCube[6], float fFresnel )
  308. {
  309. float3 vReflect = reflect( -vEyeDir, worldNormal ); // Reflect view through normal
  310. return fFresnel * PixelShaderAmbientLight( vEyeDir, cAmbientCube );
  311. }
  312. // Called directly by newer shaders or through the following wrapper for older shaders
  313. float3 PixelShaderDoLighting( const float3 worldPos, const float3 worldNormal,
  314. const float3 staticLightingColor, const bool bStaticLight,
  315. const bool bAmbientLight, const float4 lightAtten, const float3 cAmbientCube[6],
  316. in samplerCUBE NormalizeSampler, const int nNumLights, PixelShaderLightInfo cLightInfo[3],
  317. const bool bHalfLambert, const bool bDoLightingWarp, in sampler lightWarpSampler, const float flDirectShadow = 1.0f )
  318. {
  319. float3 linearColor = PixelShaderDoLightingLinear( worldPos, worldNormal, staticLightingColor,
  320. bStaticLight, bAmbientLight, lightAtten,
  321. cAmbientCube, NormalizeSampler, nNumLights, cLightInfo, bHalfLambert,
  322. bDoLightingWarp, lightWarpSampler, flDirectShadow );
  323. // go ahead and clamp to the linear space equivalent of overbright 2 so that we match everything else.
  324. // linearColor = HuePreservingColorClamp( linearColor, pow( 2.0f, 2.2 ) );
  325. return linearColor;
  326. }
  327. #endif //#ifndef COMMON_VERTEXLITGENERIC_DX9_H_