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.

221 lines
6.7 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Header: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "BaseVSShader.h"
  9. #include "shadow_ps20.inc"
  10. #include "shadow_ps20b.inc"
  11. #include "shadow_vs20.inc"
  12. // NOTE: This has to be the last file included!
  13. #include "tier0/memdbgon.h"
  14. BEGIN_VS_SHADER_FLAGS( Shadow, "Help for Shadow", SHADER_NOT_EDITABLE )
  15. BEGIN_SHADER_PARAMS
  16. SHADER_PARAM( MAXFALLOFFAMOUNT, SHADER_PARAM_TYPE_FLOAT, "240", "" )
  17. SHADER_PARAM( DEFERREDSHADOWS, SHADER_PARAM_TYPE_BOOL, "0", "" )
  18. SHADER_PARAM( ZFAILENABLE, SHADER_PARAM_TYPE_BOOL, "0", "" )
  19. SHADER_PARAM( BLOBBYSHADOWS, SHADER_PARAM_TYPE_BOOL, "0", "" )
  20. SHADER_PARAM( DEPTHTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "" )
  21. END_SHADER_PARAMS
  22. SHADER_INIT_PARAMS()
  23. {
  24. /*
  25. The alpha blending state either must be:
  26. Src Color * Dst Color + Dst Color * 0
  27. (src color = C*A + 1-A)
  28. or
  29. // Can't be this, doesn't work with fog
  30. Src Color * Dst Color + Dst Color * (1-Src Alpha)
  31. (src color = C * A, Src Alpha = A)
  32. */
  33. INIT_FLOAT_PARM( MAXFALLOFFAMOUNT, 240.0f );
  34. if ( !params[DEFERREDSHADOWS]->IsDefined() )
  35. {
  36. params[DEFERREDSHADOWS]->SetIntValue( 0 );
  37. }
  38. if ( !params[ZFAILENABLE]->IsDefined() )
  39. {
  40. params[ZFAILENABLE]->SetIntValue( 0 );
  41. }
  42. if ( !params[BLOBBYSHADOWS]->IsDefined() )
  43. {
  44. params[BLOBBYSHADOWS]->SetIntValue( 0 );
  45. }
  46. }
  47. SHADER_FALLBACK
  48. {
  49. return 0;
  50. }
  51. SHADER_INIT
  52. {
  53. LoadTexture( BASETEXTURE, TEXTUREFLAGS_SRGB );
  54. }
  55. SHADER_DRAW
  56. {
  57. SHADOW_STATE
  58. {
  59. bool bDeferredShadows = ( params[DEFERREDSHADOWS]->GetIntValue() != 0 );
  60. bool bBlobbyShadows = ( params[BLOBBYSHADOWS]->GetIntValue() != 0 );
  61. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  62. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  63. if ( bDeferredShadows )
  64. {
  65. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  66. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, false );
  67. }
  68. if ( !bDeferredShadows )
  69. {
  70. // NOTE: This is deliberately this way round (instead of "DST_COLOR, ZERO"),
  71. // since these two permutations produce *different* values on 360!!
  72. // This was causing undue darkening of the framebuffer by these
  73. // shadows, which was highly noticeable in very dark areas:
  74. EnableAlphaBlending( SHADER_BLEND_ZERO, SHADER_BLEND_SRC_COLOR );
  75. unsigned int flags = VERTEX_POSITION | VERTEX_COLOR;
  76. int numTexCoords = 2;
  77. int texCoordDims[2] = { 3, 3 };
  78. pShaderShadow->VertexShaderVertexFormat( flags, numTexCoords, texCoordDims, 0 );
  79. }
  80. else
  81. {
  82. EnableAlphaBlending( SHADER_BLEND_ZERO, SHADER_BLEND_SRC_COLOR );
  83. /*
  84. EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  85. pShaderShadow->EnableBlending( true );
  86. pShaderShadow->EnableBlendingSeparateAlpha( true );
  87. pShaderShadow->BlendFuncSeparateAlpha( SHADER_BLEND_ONE, SHADER_BLEND_ONE );
  88. pShaderShadow->BlendOpSeparateAlpha( SHADER_BLEND_OP_MIN );
  89. */
  90. unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL;
  91. int numTexCoords = 7;
  92. int texCoordDims[7] = { 3, 4, 4, 4, 4, 4, 3 };
  93. pShaderShadow->VertexShaderVertexFormat( flags, numTexCoords, texCoordDims, 0 );
  94. }
  95. DECLARE_STATIC_VERTEX_SHADER( shadow_vs20 );
  96. SET_STATIC_VERTEX_SHADER_COMBO( DEFERRED_SHADOWS, bDeferredShadows );
  97. SET_STATIC_VERTEX_SHADER( shadow_vs20 );
  98. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  99. {
  100. DECLARE_STATIC_PIXEL_SHADER( shadow_ps20b );
  101. SET_STATIC_PIXEL_SHADER_COMBO( DEFERRED_SHADOWS, bDeferredShadows );
  102. SET_STATIC_PIXEL_SHADER_COMBO( BLOBBY_SHADOWS, bBlobbyShadows );
  103. SET_STATIC_PIXEL_SHADER( shadow_ps20b );
  104. }
  105. else
  106. {
  107. DECLARE_STATIC_PIXEL_SHADER( shadow_ps20 );
  108. SET_STATIC_PIXEL_SHADER_COMBO( DEFERRED_SHADOWS, bDeferredShadows );
  109. SET_STATIC_PIXEL_SHADER_COMBO( BLOBBY_SHADOWS, bBlobbyShadows );
  110. SET_STATIC_PIXEL_SHADER( shadow_ps20 );
  111. }
  112. pShaderShadow->EnableSRGBWrite( true );
  113. if ( bDeferredShadows )
  114. {
  115. pShaderShadow->DepthFunc( params[ZFAILENABLE]->GetIntValue() ? SHADER_DEPTHFUNC_FARTHER : SHADER_DEPTHFUNC_NEAREROREQUAL );
  116. pShaderShadow->EnableDepthWrites( false );
  117. pShaderShadow->EnableColorWrites( true );
  118. pShaderShadow->EnableAlphaWrites( true );
  119. //pShaderShadow->PolyMode( SHADER_POLYMODEFACE_FRONT_AND_BACK, SHADER_POLYMODE_LINE );
  120. }
  121. // We need to fog to *white* regardless of overbrighting...
  122. FogToWhite();
  123. }
  124. DYNAMIC_STATE
  125. {
  126. bool bDeferredShadows = ( params[DEFERREDSHADOWS]->GetIntValue() != 0 );
  127. BindTexture( SHADER_SAMPLER0, TEXTURE_BINDFLAGS_SRGBREAD, BASETEXTURE, FRAME );
  128. if ( bDeferredShadows )
  129. {
  130. BindTexture( SHADER_SAMPLER1, TEXTURE_BINDFLAGS_NONE, DEPTHTEXTURE );
  131. //pShaderAPI->BindStandardTexture( SHADER_SAMPLER1, TEXTURE_FRAME_BUFFER_FULL_DEPTH );
  132. }
  133. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, BASETEXTURETRANSFORM );
  134. SetPixelShaderConstantGammaToLinear( 1, COLOR ); // shadow color
  135. // Get texture dimensions...
  136. int nWidth = 16;
  137. int nHeight = 16;
  138. ITexture *pTexture = params[BASETEXTURE]->GetTextureValue();
  139. if (pTexture)
  140. {
  141. nWidth = pTexture->GetActualWidth();
  142. nHeight = pTexture->GetActualHeight();
  143. }
  144. Vector4D vecJitter( 1.0 / nWidth, 1.0 / nHeight, 0.0, 0.0 );
  145. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_2, vecJitter.Base() );
  146. vecJitter.y *= -1.0f;
  147. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_3, vecJitter.Base() );
  148. DECLARE_DYNAMIC_VERTEX_SHADER( shadow_vs20 );
  149. SET_DYNAMIC_VERTEX_SHADER( shadow_vs20 );
  150. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  151. {
  152. DECLARE_DYNAMIC_PIXEL_SHADER( shadow_ps20b );
  153. SET_DYNAMIC_PIXEL_SHADER( shadow_ps20b );
  154. }
  155. else
  156. {
  157. DECLARE_DYNAMIC_PIXEL_SHADER( shadow_ps20 );
  158. SET_DYNAMIC_PIXEL_SHADER( shadow_ps20 );
  159. }
  160. float eyePos[4];
  161. pShaderAPI->GetWorldSpaceCameraPosition( eyePos );
  162. pShaderAPI->SetPixelShaderConstant( 2, eyePos, 1 );
  163. pShaderAPI->SetPixelShaderFogParams( 3 );
  164. float fConst[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  165. fConst[0] = IS_PARAM_DEFINED( MAXFALLOFFAMOUNT ) ? params[MAXFALLOFFAMOUNT]->GetFloatValue() : 240.0f;
  166. fConst[0] /= 255.0f;
  167. if ( bDeferredShadows )
  168. {
  169. pTexture = params[DEPTHTEXTURE]->GetTextureValue();
  170. if (pTexture)
  171. {
  172. nWidth = pTexture->GetActualWidth();
  173. nHeight = pTexture->GetActualHeight();
  174. }
  175. //pShaderAPI->GetBackBufferDimensions( nWidth, nHeight );
  176. fConst[1] = 1.0f / float( nWidth );
  177. fConst[2] = 1.0f / float( nHeight );
  178. }
  179. pShaderAPI->SetPixelShaderConstant( 4, fConst );
  180. }
  181. Draw( );
  182. }
  183. END_SHADER