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.

200 lines
7.2 KiB

  1. //========== Copyright (c) Valve Corporation, All rights reserved. ==========//
  2. // STATIC: "BASETEXTURE" "0..1"
  3. // STATIC: "CUBEMAP" "0..1"
  4. // STATIC: "DIFFUSELIGHTING" "0..1"
  5. // STATIC: "NORMALMAPALPHAENVMAPMASK" "0..1"
  6. // STATIC: "HALFLAMBERT" "0..1"
  7. // STATIC: "FLASHLIGHT" "0..1"
  8. // STATIC: "TRANSLUCENT" "0..1"
  9. // DYNAMIC: "WRITEWATERFOGTODESTALPHA" "0..1"
  10. #include "common_fog_ps_fxc.h"
  11. // DYNAMIC: "WARPINGIN" "0..1"
  12. // DYNAMIC: "AMBIENT_LIGHT" "0..1"
  13. // DYNAMIC: "NUM_LIGHTS" "0..2" [ps20]
  14. // DYNAMIC: "NUM_LIGHTS" "0..4" [ps20b]
  15. // DYNAMIC: "NUM_LIGHTS" "0..4" [ps30]
  16. // We don't use other lights when doing the flashlight, so just skip that
  17. // SKIP: ( $FLASHLIGHT != 0 ) && ( $NUM_LIGHTS > 0 ) [PC]
  18. #include "common_flashlight_fxc.h"
  19. #include "common_vertexlitgeneric_dx9.h"
  20. const float4 g_EnvmapTint : register( c0 );
  21. const float4 g_DiffuseModulation : register( c1 );
  22. #if !FLASHLIGHT
  23. const float3 g_EnvmapContrast : register( c2 );
  24. const float3 g_EnvmapSaturation : register( c3 );
  25. #endif
  26. const float4 g_SelfIllumTint : register( c4 );
  27. const float3 cAmbientCube[6] : register( c5 );
  28. // 2 registers each - 6 register total
  29. PixelShaderLightInfo cLightInfo[3] : register( c13 ); // through c18
  30. const float3 g_EyePos : register( c20 );
  31. const float4 g_FogParams : register( c21 );
  32. #if FLASHLIGHT
  33. const float4 g_FlashlightAttenuationFactors : register( c22 );
  34. const float3 g_FlashlightPos : register( c23 );
  35. const float4x4 g_FlashlightWorldToTexture : register( c24 ); // through c27
  36. #else
  37. const float g_Time : register( c22 );
  38. #endif
  39. sampler BaseTextureSampler : register( s0 );
  40. sampler EnvmapSampler : register( s1 );
  41. sampler FlowMapSampler : register( s2 );
  42. sampler BumpmapSampler : register( s3 );
  43. sampler EnvmapMaskSampler : register( s4 );
  44. sampler NormalizeSampler : register( s5 );
  45. sampler SelfIllumMapSampler : register( s6 );
  46. sampler FlashlightSampler : register( s7 );
  47. #if defined( _PS3 )
  48. // Causes the Cg compiler to automatically produce _bx2 modifier on the texture load instead of producing a MAD to range expand the vector, saving one instruction.
  49. #pragma texsign BumpmapSampler
  50. #pragma texformat BumpmapSampler RGBA8
  51. #endif
  52. struct PS_INPUT
  53. {
  54. float4 baseTexCoord2_tangentSpaceVertToEyeVectorXY : TEXCOORD0;
  55. // bump mapping and a separate envmap mask texture are mutually exclusive.
  56. float4 lightAtten : TEXCOORD1;
  57. float4 worldVertToEyeVectorXYZ_tangentSpaceVertToEyeVectorZ : TEXCOORD2;
  58. float3x3 tangentSpaceTranspose : TEXCOORD3;
  59. // second row : TEXCOORD4;
  60. // third row : TEXCOORD5;
  61. float4 worldPos_projPosZ : TEXCOORD6;
  62. };
  63. float4 main( PS_INPUT i ) : COLOR
  64. {
  65. bool bBaseTexture = BASETEXTURE ? true : false;
  66. bool bCubemap = CUBEMAP ? true : false;
  67. bool bDiffuseLighting = DIFFUSELIGHTING ? true : false;
  68. bool bNormalMapAlphaEnvmapMask = NORMALMAPALPHAENVMAPMASK ? true : false;
  69. bool bHalfLambert = HALFLAMBERT ? true : false;
  70. bool bFlashlight = (FLASHLIGHT!=0) ? true : false;
  71. bool bAmbientLight = AMBIENT_LIGHT ? true : false;
  72. int nNumLights = NUM_LIGHTS;
  73. float4 baseColor = float4( 1.0f, 1.0f, 1.0f, 1.0f );
  74. if( bBaseTexture )
  75. baseColor = tex2D( BaseTextureSampler, i.baseTexCoord2_tangentSpaceVertToEyeVectorXY.xy );
  76. float specularFactor = 1.0f;
  77. float4 normalTexel = tex2D( BumpmapSampler, i.baseTexCoord2_tangentSpaceVertToEyeVectorXY.xy );
  78. float3 tangentSpaceNormal = 2.0f * normalTexel - 1.0f;
  79. if( bNormalMapAlphaEnvmapMask )
  80. specularFactor = normalTexel.a;
  81. float3 diffuseLighting = float3( 1.0f, 1.0f, 1.0f );
  82. if( bDiffuseLighting )
  83. {
  84. float3 worldSpaceNormal = mul( i.tangentSpaceTranspose, tangentSpaceNormal );
  85. worldSpaceNormal = NormalizeWithCubemap( NormalizeSampler, worldSpaceNormal );
  86. diffuseLighting = PixelShaderDoLighting( i.worldPos_projPosZ.xyz, worldSpaceNormal,
  87. float3( 0.0f, 0.0f, 0.0f ), false, bAmbientLight, i.lightAtten,
  88. cAmbientCube, NormalizeSampler, nNumLights, cLightInfo, bHalfLambert,
  89. false, NormalizeSampler );
  90. }
  91. float3 albedo = float3( 1.0f, 1.0f, 1.0f );
  92. float alpha = 1.0f;
  93. if( bBaseTexture )
  94. {
  95. albedo *= baseColor;
  96. alpha *= baseColor.a;
  97. }
  98. // If we only have specularity, assume that we want a black diffuse component, and
  99. // get alpha from the envmapmask
  100. if( !bBaseTexture && bCubemap )
  101. {
  102. diffuseLighting = float3( 0.0f, 0.0f, 0.0f );
  103. if( bNormalMapAlphaEnvmapMask )
  104. {
  105. alpha *= specularFactor;
  106. }
  107. }
  108. #if FLASHLIGHT
  109. if( bFlashlight )
  110. {
  111. float3 worldSpaceNormal = mul( i.tangentSpaceTranspose, tangentSpaceNormal );
  112. float4 flashlightSpacePosition = mul( float4( i.worldPos_projPosZ.xyz, 1.0f ), g_FlashlightWorldToTexture );
  113. diffuseLighting = DoFlashlight( g_FlashlightPos, i.worldPos_projPosZ.xyz, flashlightSpacePosition,
  114. worldSpaceNormal, g_FlashlightAttenuationFactors.xyz,
  115. g_FlashlightAttenuationFactors.w, FlashlightSampler,
  116. EnvmapMaskSampler, EnvmapMaskSampler, 0, false, float2(0, 0), true );
  117. }
  118. #endif
  119. diffuseLighting *= g_DiffuseModulation.rgb;
  120. alpha *= g_DiffuseModulation.a;
  121. float3 diffuseComponent = albedo * diffuseLighting;
  122. float4 flowmapSample = tex2D( FlowMapSampler, i.baseTexCoord2_tangentSpaceVertToEyeVectorXY.xy );
  123. #if !FLASHLIGHT
  124. flowmapSample.xy += float2( .11, .124 ) * g_Time.xx;
  125. #endif
  126. float4 selfIllumSample = tex2D( SelfIllumMapSampler, flowmapSample.xy );
  127. // float thing = ( 0.5f * ( cos( g_Time * 3 ) + 1.0f ) );
  128. // diffuseComponent.xyz += albedo * 10.0f * pow( selfIllumSample.xyz, thing );
  129. diffuseComponent.xyz += albedo * g_SelfIllumTint.xyz * selfIllumSample.xyz;
  130. float3 specularLighting = float3( 0.0f, 0.0f, 0.0f );
  131. #if !FLASHLIGHT
  132. if( bCubemap )
  133. {
  134. // If we've *only* specified a cubemap, blow off the diffuse component
  135. if ( !bBaseTexture && !bDiffuseLighting && !bFlashlight )
  136. {
  137. diffuseComponent = float3( 0.0f, 0.0f, 0.0f );
  138. }
  139. float3 worldSpaceNormal = mul( i.tangentSpaceTranspose, tangentSpaceNormal );
  140. float3 reflectVect = CalcReflectionVectorUnnormalized( worldSpaceNormal, i.worldVertToEyeVectorXYZ_tangentSpaceVertToEyeVectorZ.xyz );
  141. specularLighting = ENV_MAP_SCALE * texCUBE( EnvmapSampler, reflectVect );
  142. specularLighting *= specularFactor;
  143. specularLighting *= g_EnvmapTint;
  144. float3 specularLightingSquared = specularLighting * specularLighting;
  145. specularLighting = lerp( specularLighting, specularLightingSquared, g_EnvmapContrast );
  146. float3 greyScale = dot( specularLighting, float3( 0.299f, 0.587f, 0.114f ) );
  147. specularLighting = lerp( greyScale, specularLighting, g_EnvmapSaturation );
  148. }
  149. #endif
  150. float3 result = diffuseComponent + specularLighting;
  151. float fogFactor = CalcPixelFogFactor( PIXELFOGTYPE, g_FogParams, g_EyePos.xyz, i.worldPos_projPosZ.xyz, i.worldPos_projPosZ.w );
  152. #if WRITEWATERFOGTODESTALPHA && (PIXELFOGTYPE == PIXEL_FOG_TYPE_HEIGHT)
  153. alpha = fogFactor;
  154. #endif
  155. // result.xyz = float3( 1.0f, 1.0f, 1.0f );
  156. #if TRANSLUCENT==0
  157. # if WARPINGIN
  158. alpha = 0.0f; // write alpha where the vortigaunt is so that we can selectivly draw pixels when refracting
  159. #else
  160. alpha = 1.0f; // write alpha where the vortigaunt is so that we can selectivly draw pixels when refracting
  161. # endif
  162. #endif
  163. //FIXME: need to take dowaterfog into consideration
  164. return FinalOutput( float4( result, alpha ), fogFactor, PIXELFOGTYPE, TONEMAP_SCALE_LINEAR );
  165. }