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.

140 lines
4.7 KiB

  1. //========== Copyright (c) Valve Corporation, All rights reserved. ==========//
  2. // STATIC: "QUALITY_MODE" "0..2" [ps30]
  3. // STATIC: "QUALITY_MODE" "0..1" [ps20b]
  4. // STATIC: "QUALITY_MODE" "0..0" [ps20]
  5. #include "common_ps_fxc.h"
  6. #include "shader_constant_register_map.h"
  7. // SAMPLERS
  8. sampler BaseSampler : register( s0 );
  9. //sampler NoiseSampler : register( s1 );
  10. //const float4 g_fvConstRegister0 : register( c0 );
  11. //#define g_vTimeUVOffset g_fvConstRegister0.xy
  12. struct PS_INPUT
  13. {
  14. float2 vBaseUV : TEXCOORD0;
  15. };
  16. float UnpackFloat2(float2 packed)
  17. {
  18. return packed.x + ( packed.y * (1.0f / 256.0f) );
  19. }
  20. float4_color_return_type main( PS_INPUT i ) : COLOR
  21. {
  22. //sample basetexture
  23. float4 vColorCenter = tex2D( BaseSampler, i.vBaseUV.xy );
  24. //float2 vSamplePerturb = (tex2D( NoiseSampler, g_vTimeUVOffset + i.vBaseUV.xy * 32.0f ).xy * 2.0f) - 1.0f;
  25. float3 vCenterPos = float3( 0, 0, UnpackFloat2( vColorCenter.ba ) );
  26. //clip( vCenterPos.z - 0.001f );
  27. float3 vCenterNormal = normalize( float3( ((vColorCenter.xy * 2.0f) - 1.0f), 1 ) );
  28. float flSSAO = 1.0f;
  29. // experimenting with various poisson discs
  30. //float2 offsets[16] = {
  31. // float2( -0.94201624, -0.39906216 ),
  32. // float2( 0.94558609, -0.76890725 ),
  33. // float2( -0.094184101, -0.92938870 ),
  34. // float2( 0.34495938, 0.29387760 ),
  35. // float2( -0.91588581, 0.45771432 ),
  36. // float2( -0.81544232, -0.87912464 ),
  37. // float2( -0.38277543, 0.27676845 ),
  38. // float2( 0.97484398, 0.75648379 ),
  39. // float2( 0.44323325, -0.97511554 ),
  40. // float2( 0.53742981, -0.47373420 ),
  41. // float2( -0.26496911, -0.41893023 ),
  42. // float2( 0.79197514, 0.19090188 ),
  43. // float2( -0.24188840, 0.99706507 ),
  44. // float2( -0.81409955, 0.91437590 ),
  45. // float2( 0.19984126, 0.78641367 ),
  46. // float2( 0.14383161, -0.14100790 )
  47. // };
  48. //for ( int k = 0; k < 16; k++ )
  49. #if QUALITY_MODE == 2
  50. float2 offsets[16] = {
  51. float2( 0.5301946, 0.5073427 ),
  52. float2( 0.0684771, 0.4128032 ),
  53. float2( 0.5126259, 0.0365609 ),
  54. float2( 0.0101744, 0.8379333 ),
  55. float2( 0.1560884, -0.1649500 ),
  56. float2( 0.9068975, -0.0846123 ),
  57. float2( 0.6797046, -0.5736184 ),
  58. float2( -0.4168866, 0.8836493 ),
  59. float2( -0.5666743, 0.3535654 ),
  60. float2( -0.0424080, -0.6649325 ),
  61. float2( 0.3658477, -0.8909833 ),
  62. float2( -0.4150895, -0.2524949 ),
  63. float2( -0.9276293, 0.0097100 ),
  64. float2( 0.4289346, 0.9020033 ),
  65. float2( -0.6507930, -0.6037632 ),
  66. float2( -0.2034065, 0.1169684 )
  67. };
  68. for ( int k = 0; k < 16; k++ )
  69. #elif QUALITY_MODE == 1
  70. float2 offsets[9] = {
  71. float2( 0.7420793, 0.6406463 ),
  72. float2( 0.1241058, 0.5777215 ),
  73. float2( 0.8185505, -0.0552139 ),
  74. float2( 0.0845212, -0.1639908 ),
  75. float2( 0.1128483, -0.8857714 ),
  76. float2( -0.4971790, 0.6808146 ),
  77. float2( -0.5255098, -0.2983963 ),
  78. float2( 0.6586789, -0.7424613 ),
  79. float2( -0.9488768, 0.1668307 )
  80. };
  81. for ( int k = 0; k < 9; k++ )
  82. #else
  83. float2 offsets[4] = {
  84. float2( 0.6443077, 0.0112282 ),
  85. float2( -0.3698092, -0.6966258 ),
  86. float2( 0.0570701, 0.7935340 ),
  87. float2( -0.6399019, 0.3894303 )
  88. };
  89. for ( int k = 0; k < 4; k++ )
  90. #endif
  91. {
  92. #if QUALITY_MODE == 2
  93. float4 vAOBuffer = tex2D( BaseSampler, i.vBaseUV.xy + offsets[k] * 0.001f );
  94. #elif QUALITY_MODE == 1
  95. float4 vAOBuffer = tex2D( BaseSampler, i.vBaseUV.xy + offsets[k] * 0.0015f );
  96. #else
  97. float4 vAOBuffer = tex2D( BaseSampler, i.vBaseUV.xy + offsets[k] * 0.002f );
  98. #endif
  99. float flSampleDepth = UnpackFloat2( vAOBuffer.ba );
  100. float flDepthDiff = (flSampleDepth - vCenterPos.z);
  101. #if QUALITY_MODE > 0
  102. float3 vSampleNormal = normalize( float3( ((vAOBuffer.xy * 2.0f) - 1.0f), 1 ) );
  103. float3 vSamplePos = float3( offsets[k], flSampleDepth );
  104. float3 vHSV = ( vCenterPos - vSamplePos ); //HeightSpaceVector
  105. float3 vHSVn = normalize( vHSV );
  106. float flSampleAOContrib = abs( dot( vSampleNormal, vHSVn ) - dot( vCenterNormal, vHSVn ) );
  107. flSSAO -= flSampleAOContrib * step( abs(flDepthDiff), 0.015f ) * (length( vHSV ) * 0.1f);
  108. #else
  109. flSSAO -= 20.0f * flDepthDiff * step( abs(flDepthDiff), 0.015f );
  110. #endif
  111. }
  112. return float4( flSSAO.rrr, 1 );
  113. }