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.

260 lines
8.7 KiB

  1. //========== Copyright (c) Valve Corporation, All rights reserved. ==========//
  2. // paired with lightmappedgeneric_vs##
  3. // STATIC: "DETAILTEXTURE" "0..1"
  4. // STATIC: "BUMPMAP" "0..1"
  5. // STATIC: "VERTEXCOLOR" "0..1"
  6. // STATIC: "SELFILLUM" "0..1"
  7. // STATIC: "DETAIL_ALPHA_MASK_BASE_TEXTURE" "0..1"
  8. // STATIC: "FLASHLIGHT" "0..1"
  9. // STATIC: "SEAMLESS" "0..1"
  10. // STATIC: "FLASHLIGHTDEPTHFILTERMODE" "0..3" [ps20b] [PC]
  11. // STATIC: "FLASHLIGHTDEPTHFILTERMODE" "0..2" [ps30] [PC]
  12. // STATIC: "FLASHLIGHTDEPTHFILTERMODE" "0..0" [ps20b] [CONSOLE]
  13. // STATIC: "SHADER_SRGB_READ" "0..1" [XBOX]
  14. // STATIC: "SHADER_SRGB_READ" "0..0" [PC]
  15. // STATIC: "SHADER_SRGB_READ" "0..0" [SONYPS3]
  16. // diffuse bump map is always true when bumpmapping is enabled, so just set it to 1
  17. #define DIFFUSEBUMPMAP 1
  18. // DYNAMIC: "WRITEWATERFOGTODESTALPHA" "0..1"
  19. #include "common_fog_ps_fxc.h"
  20. // DYNAMIC: "WRITE_DEPTH_TO_DESTALPHA" "0..1" [ps20b] [ps30] [PC]
  21. // DYNAMIC: "WRITE_DEPTH_TO_DESTALPHA" "0..0" [ps20b] [CONSOLE]
  22. // DYNAMIC: "FLASHLIGHTSHADOWS" "0..1" [ps20b] [ps30]
  23. // DYNAMIC: "UBERLIGHT" "0..1" [ps30] [PC]
  24. // SKIP: $DETAILTEXTURE && ( $BUMPMAP && !$DETAIL_ALPHA_MASK_BASE_TEXTURE )
  25. // SKIP: $VERTEXCOLOR && $BUMPMAP
  26. // SKIP: $FLASHLIGHT && $SELFILLUM
  27. // SKIP: $FLASHLIGHT && $DETAIL_ALPHA_MASK_BASE_TEXTURE
  28. // SKIP: $FLASHLIGHT && $BUMPMAP
  29. // We don't care about flashlight depth unless the flashlight is on
  30. // SKIP: ( $FLASHLIGHT == 0 ) && ( $FLASHLIGHTSHADOWS == 1 ) [ps20b] [ps30]
  31. // We don't care about uberlight unless the flashlight is on
  32. // SKIP: ( $FLASHLIGHT == 0 ) && ( $UBERLIGHT == 1 ) [ps30]
  33. #if defined( SHADER_MODEL_PS_2_0 )
  34. # define WRITE_DEPTH_TO_DESTALPHA 0
  35. #endif
  36. #define HDRTYPE HDR_TYPE_NONE
  37. #include "shader_constant_register_map.h"
  38. #include "common_flashlight_fxc.h"
  39. #include "common_ps_fxc.h"
  40. #define PIXELSHADER
  41. #include "common_lightmappedgeneric_fxc.h"
  42. const float4 g_SelfIllumTint : register( c7 );
  43. static const float g_OverbrightFactor = 2.0f;
  44. const float3 g_EyePos : register( c10 );
  45. const float4 g_FogParams : register( c11 );
  46. const float3 g_FlashlightPos : register( c15 );
  47. // flashlightfixme: Move this math into the vertex shader.
  48. const float4x4 g_FlashlightWorldToTexture : register( c16 );
  49. const float4 g_FlashlightAttenuationFactors : register( c20 );
  50. #if UBERLIGHT && defined( SHADER_MODEL_PS_3_0 )
  51. const float3 g_vSmoothEdge0 : register( PSREG_UBERLIGHT_SMOOTH_EDGE_0 ); // ps_3_0 and up (over 32 registers)
  52. const float3 g_vSmoothEdge1 : register( PSREG_UBERLIGHT_SMOOTH_EDGE_1 );
  53. const float3 g_vSmoothOneOverWidth : register( PSREG_UBERLIGHT_SMOOTH_EDGE_OOW );
  54. const float4 g_vShearRound : register( PSREG_UBERLIGHT_SHEAR_ROUND );
  55. const float4 g_aAbB : register( PSREG_UBERLIGHT_AABB );
  56. const float4x4 g_FlashlightWorldToLight : register( PSREG_UBERLIGHT_WORLD_TO_LIGHT );
  57. #endif
  58. sampler BaseTextureSampler : register( s0 );
  59. sampler LightmapSampler : register( s1 );
  60. sampler FlashlightSampler : register( s2 );
  61. sampler DetailSampler : register( s3 );
  62. sampler BumpmapSampler : register( s4 );
  63. sampler NormalizeSampler : register( s6 );
  64. sampler ShadowDepthSampler : register( s7 );
  65. #if defined(_PS3)
  66. // Needed for optimal shadow filter code generation on PS3.
  67. #pragma texformat ShadowDepthSampler DEPTH_COMPONENT24
  68. #endif
  69. #if (BUMPMAP == 1) && defined( _PS3 )
  70. // 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.
  71. #pragma texsign BumpmapSampler
  72. #pragma texformat BumpmapSampler RGBA8
  73. #endif
  74. //PS_INPUT defined in common_lightmappedgeneric_fxc.h"
  75. float4_color_return_type main( PS_INPUT i ) : COLOR
  76. {
  77. bool bDetailTexture = DETAILTEXTURE ? true : false;
  78. bool bBumpmap = BUMPMAP ? true : false;
  79. bool bDiffuseBumpmap = DIFFUSEBUMPMAP ? true : false;
  80. bool bVertexColor = VERTEXCOLOR ? true : false;
  81. bool bSelfIllum = SELFILLUM ? true : false;
  82. bool bDetailAlphaMaskBaseTexture = DETAIL_ALPHA_MASK_BASE_TEXTURE ? true : false;
  83. bool bFlashlight = FLASHLIGHT ? true : false;
  84. float2 vBaseTexCoord;
  85. #if ( SEAMLESS )
  86. {
  87. vBaseTexCoord = i.SeamlessTexCoord.xy;
  88. }
  89. #else
  90. {
  91. vBaseTexCoord = i.BASETEXCOORD;
  92. }
  93. #endif
  94. float3 worldPos = i.worldPos_projPosZ.xyz;
  95. float3 lightmapColor1 = float3( 1.0f, 1.0f, 1.0f );
  96. float3 lightmapColor2 = float3( 1.0f, 1.0f, 1.0f );
  97. float3 lightmapColor3 = float3( 1.0f, 1.0f, 1.0f );
  98. if( bBumpmap && bDiffuseBumpmap )
  99. {
  100. float2 bumpCoord1;
  101. float2 bumpCoord2;
  102. float2 bumpCoord3;
  103. ComputeBumpedLightmapCoordinates( i.lightmapTexCoord1And2, i.lightmapTexCoord3_bumpTexCoord.xy,
  104. bumpCoord1, bumpCoord2, bumpCoord3 );
  105. float4 lightmapSample1 = tex2D( LightmapSampler, bumpCoord1 );
  106. lightmapColor1 = lightmapSample1.rgb;
  107. lightmapColor2 = tex2D( LightmapSampler, bumpCoord2 ).rgb;
  108. lightmapColor3 = tex2D( LightmapSampler, bumpCoord3 ).rgb;
  109. }
  110. else
  111. {
  112. if( !bFlashlight )
  113. {
  114. float2 bumpCoord1 = ComputeLightmapCoordinates( i.lightmapTexCoord1And2, i.lightmapTexCoord3_bumpTexCoord.xy );
  115. float4 lightmapSample1 = tex2D( LightmapSampler, bumpCoord1 );
  116. lightmapColor1 = lightmapSample1.rgb;
  117. }
  118. }
  119. float4 detailColor = float4( 1.0f, 1.0f, 1.0f, 1.0f );
  120. if( bDetailTexture )
  121. {
  122. detailColor = tex2D( DetailSampler, i.DETAILCOORD );
  123. }
  124. float4 baseColor = float4( 1.0f, 1.0f, 1.0f, 1.0f );
  125. baseColor = tex2Dsrgb( BaseTextureSampler, vBaseTexCoord );
  126. if ( bDetailAlphaMaskBaseTexture )
  127. {
  128. // This is what WorldTwoTextureBlend_DX6 does.
  129. baseColor.rgb = saturate( saturate( baseColor.rgb * 2 ) * detailColor.a + (1 - detailColor.a) );
  130. baseColor.rgb *= detailColor.rgb;
  131. }
  132. else
  133. {
  134. baseColor.rgb = lerp( baseColor.rgb, detailColor.rgb, detailColor.a );
  135. }
  136. float3 normal = float3( 0.0f, 0.0f, 1.0f );
  137. if( bBumpmap )
  138. {
  139. float3 normalTexel;
  140. normalTexel = tex2D( BumpmapSampler, i.BUMPCOORD ).xyz;
  141. normal = 2.0f * normalTexel - 1.0f;
  142. }
  143. float3 albedo = float3( 1.0f, 1.0f, 1.0f );
  144. float alpha = 1.0f;
  145. albedo *= baseColor.rgb;
  146. if( !bSelfIllum )
  147. {
  148. alpha *= baseColor.a;
  149. }
  150. // The vertex color contains the modulation color + vertex color combined
  151. albedo *= i.vertexColor.rgb;
  152. alpha *= i.vertexColor.a; // not sure about this one
  153. float3 diffuseLighting;
  154. if( bFlashlight )
  155. {
  156. float3 worldSpaceNormal;
  157. if ( bBumpmap )
  158. {
  159. worldSpaceNormal = mul( normal, float3x3( i.tangentSpaceTranspose0_vertexBlendX.xyz, i.tangentSpaceTranspose1_bumpTexCoord2u.xyz, i.tangentSpaceTranspose2_bumpTexCoord2v.xyz ) );
  160. }
  161. else
  162. {
  163. worldSpaceNormal = i.tangentSpaceTranspose2_bumpTexCoord2v.xyz;
  164. }
  165. int nShadowSampleLevel = 0;
  166. bool bDoShadows = false;
  167. // On ps_2_b, we can do shadow mapping
  168. #if ( FLASHLIGHTSHADOWS && (defined(SHADER_MODEL_PS_2_B) || defined(SHADER_MODEL_PS_3_0) ) )
  169. nShadowSampleLevel = FLASHLIGHTDEPTHFILTERMODE;
  170. bDoShadows = true;
  171. #endif
  172. float4 flashlightSpacePosition = TransformFlashlightWorldToTexture( worldPos, g_FlashlightWorldToTexture );
  173. diffuseLighting = DoFlashlight( g_FlashlightPos, worldPos, flashlightSpacePosition,
  174. worldSpaceNormal, g_FlashlightAttenuationFactors.xyz,
  175. g_FlashlightAttenuationFactors.w, FlashlightSampler, ShadowDepthSampler, NormalizeSampler,
  176. nShadowSampleLevel, bDoShadows, float2(0, 0), false );
  177. #if UBERLIGHT && defined( SHADER_MODEL_PS_3_0 )
  178. float4 uberLightPosition = mul( float4( worldPos, 1.0f ), g_FlashlightWorldToLight ).yzxw;
  179. diffuseLighting *= uberlight( uberLightPosition, g_vSmoothEdge0, g_vSmoothEdge1,
  180. g_vSmoothOneOverWidth, g_vShearRound.xy, g_aAbB, g_vShearRound.zw );
  181. #endif
  182. }
  183. else
  184. {
  185. if( bBumpmap && bDiffuseBumpmap )
  186. {
  187. float dot1 = saturate( dot( normal, bumpBasis[0] ) );
  188. float dot2 = saturate( dot( normal, bumpBasis[1] ) );
  189. float dot3 = saturate( dot( normal, bumpBasis[2] ) );
  190. float sum = dot1 + dot2 + dot3;
  191. diffuseLighting = dot1 * lightmapColor1 +
  192. dot2 * lightmapColor2 +
  193. dot3 * lightmapColor3;
  194. diffuseLighting *= 1.0f / sum;
  195. }
  196. else
  197. {
  198. diffuseLighting = lightmapColor1;
  199. }
  200. // Only scale here since the flashlight will already be scaled properly
  201. diffuseLighting *= g_OverbrightFactor;
  202. }
  203. float3 diffuseComponent = albedo * diffuseLighting;
  204. if( bSelfIllum )
  205. {
  206. float3 selfIllumComponent = g_SelfIllumTint.rgb * albedo;
  207. diffuseComponent = lerp( diffuseComponent, selfIllumComponent, baseColor.a );
  208. }
  209. float3 specularLighting = float3( 0.0f, 0.0f, 0.0f );
  210. float3 result = diffuseComponent + specularLighting;
  211. float fogFactor = CalcPixelFogFactor( PIXELFOGTYPE, g_FogParams, g_EyePos.xyz, worldPos, i.worldPos_projPosZ.w );
  212. #if WRITEWATERFOGTODESTALPHA && (PIXELFOGTYPE == PIXEL_FOG_TYPE_HEIGHT)
  213. alpha = fogFactor;
  214. #endif
  215. return FinalOutput( float4( result, alpha ), fogFactor, PIXELFOGTYPE, TONEMAP_SCALE_LINEAR, (WRITE_DEPTH_TO_DESTALPHA != 0), i.worldPos_projPosZ.w );
  216. }