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.

226 lines
7.5 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "BaseVSShader.h"
  8. #include "convar.h"
  9. #include "portalstaticoverlay_vs20.inc"
  10. #include "portalstaticoverlay_ps20.inc"
  11. #include "portalstaticoverlay_ps20b.inc"
  12. #include "cpp_shader_constant_register_map.h"
  13. // NOTE: This has to be the last file included!
  14. #include "tier0/memdbgon.h"
  15. BEGIN_VS_SHADER( PortalStaticOverlay,
  16. "Help for PortalStaticOverlay shader" )
  17. BEGIN_SHADER_PARAMS
  18. SHADER_PARAM_OVERRIDE( COLOR, SHADER_PARAM_TYPE_COLOR, "{255 255 255}", "unused", SHADER_PARAM_NOT_EDITABLE )
  19. SHADER_PARAM_OVERRIDE( ALPHA, SHADER_PARAM_TYPE_FLOAT, "1.0", "unused", SHADER_PARAM_NOT_EDITABLE )
  20. SHADER_PARAM( STATICAMOUNT, SHADER_PARAM_TYPE_FLOAT, "0.0", "Amount of the static blend texture to blend into the base texture" )
  21. SHADER_PARAM( STATICBLENDTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "When adding static, this is the texture that gets blended in" )
  22. SHADER_PARAM( STATICBLENDTEXTUREFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  23. SHADER_PARAM( ALPHAMASKTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "An alpha mask for odd shaped portals" )
  24. SHADER_PARAM( ALPHAMASKTEXTUREFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  25. SHADER_PARAM( NOCOLORWRITE, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  26. SHADER_PARAM( GHOSTOVERLAY, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  27. END_SHADER_PARAMS
  28. SHADER_INIT_PARAMS()
  29. {
  30. SET_FLAGS( MATERIAL_VAR_TRANSLUCENT );
  31. }
  32. SHADER_FALLBACK
  33. {
  34. return 0;
  35. }
  36. SHADER_INIT
  37. {
  38. if( params[STATICBLENDTEXTURE]->IsDefined() )
  39. LoadTexture( STATICBLENDTEXTURE, TEXTUREFLAGS_SRGB | ANISOTROPIC_OVERRIDE );
  40. if( params[ALPHAMASKTEXTURE]->IsDefined() )
  41. LoadTexture( ALPHAMASKTEXTURE, ANISOTROPIC_OVERRIDE );
  42. if( !params[STATICAMOUNT]->IsDefined() )
  43. params[STATICAMOUNT]->SetFloatValue( 0.0f );
  44. if( !params[STATICBLENDTEXTURE]->IsDefined() )
  45. params[STATICBLENDTEXTURE]->SetIntValue( 0 );
  46. if( !params[STATICBLENDTEXTUREFRAME]->IsDefined() )
  47. params[STATICBLENDTEXTUREFRAME]->SetIntValue( 0 );
  48. if( !params[ALPHAMASKTEXTURE]->IsDefined() )
  49. params[ALPHAMASKTEXTURE]->SetIntValue( 0 );
  50. if( !params[ALPHAMASKTEXTUREFRAME]->IsDefined() )
  51. params[ALPHAMASKTEXTUREFRAME]->SetIntValue( 0 );
  52. if( !params[NOCOLORWRITE]->IsDefined() )
  53. params[NOCOLORWRITE]->SetIntValue( 0 );
  54. if( !params[GHOSTOVERLAY]->IsDefined() )
  55. params[GHOSTOVERLAY]->SetIntValue( 0 );
  56. }
  57. SHADER_DRAW
  58. {
  59. bool bStaticBlendTexture = params[STATICBLENDTEXTURE]->IsTexture();
  60. bool bAlphaMaskTexture = params[ALPHAMASKTEXTURE]->IsTexture(); //must support 2 texture stages to use a mask
  61. bool bIsModel = IS_FLAG_SET( MATERIAL_VAR_MODEL );
  62. bool bColorWrites = params[NOCOLORWRITE]->GetIntValue() == 0;
  63. bool bGhostOverlay = params[GHOSTOVERLAY]->GetIntValue() != 0;
  64. SHADOW_STATE
  65. {
  66. SetInitialShadowState();
  67. FogToFogColor();
  68. //pShaderShadow->EnablePolyOffset( SHADER_POLYOFFSET_DECAL ); //a portal is effectively a decal on top of a wall
  69. pShaderShadow->DepthFunc( SHADER_DEPTHFUNC_NEAREROREQUAL );
  70. pShaderShadow->EnableDepthWrites( !bGhostOverlay );
  71. pShaderShadow->EnableBlending( true );
  72. if ( bGhostOverlay )
  73. {
  74. // Custom blend state to ensure we can see the ghost portals on bright foreground surfaces
  75. pShaderShadow->BlendFunc( SHADER_BLEND_ONE, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  76. // Do a reverse z-test so we only render hidden pixels
  77. pShaderShadow->DepthFunc( SHADER_DEPTHFUNC_FARTHER );
  78. pShaderShadow->EnableDepthTest( true );
  79. pShaderShadow->EnableDepthWrites( false );
  80. }
  81. else
  82. {
  83. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  84. }
  85. if ( !bGhostOverlay )
  86. {
  87. pShaderShadow->EnableAlphaTest( true );
  88. pShaderShadow->AlphaFunc( SHADER_ALPHAFUNC_GREATER, 0.0f );
  89. }
  90. pShaderShadow->EnableColorWrites( bColorWrites );
  91. if( bStaticBlendTexture || bAlphaMaskTexture )
  92. {
  93. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  94. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, bStaticBlendTexture && !IsX360() );
  95. }
  96. if( bStaticBlendTexture && bAlphaMaskTexture )
  97. {
  98. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  99. }
  100. if( g_pHardwareConfig->GetHDRType() != HDR_TYPE_NONE )
  101. {
  102. pShaderShadow->EnableSRGBWrite( true );
  103. }
  104. int fmt = VERTEX_POSITION | VERTEX_NORMAL;
  105. if ( bGhostOverlay )
  106. {
  107. fmt |= VERTEX_COLOR;
  108. }
  109. int userDataSize = 0;
  110. if( bIsModel )
  111. {
  112. userDataSize = 4;
  113. }
  114. else
  115. {
  116. fmt |= VERTEX_TANGENT_S | VERTEX_TANGENT_T;
  117. }
  118. pShaderShadow->VertexShaderVertexFormat( fmt, 1, NULL, userDataSize );
  119. DECLARE_STATIC_VERTEX_SHADER( portalstaticoverlay_vs20 );
  120. SET_STATIC_VERTEX_SHADER_COMBO( MODEL, bIsModel );
  121. SET_STATIC_VERTEX_SHADER_COMBO( PORTALGHOSTOVERLAY, params[GHOSTOVERLAY]->GetIntValue() );
  122. SET_STATIC_VERTEX_SHADER( portalstaticoverlay_vs20 );
  123. // Avoid setting a pixel shader when only doing depth/stencil operations, as recommended by PIX
  124. if( bColorWrites || bAlphaMaskTexture || g_pHardwareConfig->PlatformRequiresNonNullPixelShaders() )
  125. {
  126. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  127. {
  128. DECLARE_STATIC_PIXEL_SHADER( portalstaticoverlay_ps20b );
  129. SET_STATIC_PIXEL_SHADER_COMBO( HASALPHAMASK, bAlphaMaskTexture );
  130. SET_STATIC_PIXEL_SHADER_COMBO( HASSTATICTEXTURE, bStaticBlendTexture );
  131. SET_STATIC_PIXEL_SHADER_COMBO( PORTALGHOSTOVERLAY, bGhostOverlay );
  132. SET_STATIC_PIXEL_SHADER( portalstaticoverlay_ps20b );
  133. }
  134. else
  135. {
  136. DECLARE_STATIC_PIXEL_SHADER( portalstaticoverlay_ps20 );
  137. SET_STATIC_PIXEL_SHADER_COMBO( HASALPHAMASK, bAlphaMaskTexture );
  138. SET_STATIC_PIXEL_SHADER_COMBO( HASSTATICTEXTURE, bStaticBlendTexture );
  139. SET_STATIC_PIXEL_SHADER_COMBO( PORTALGHOSTOVERLAY, bGhostOverlay );
  140. SET_STATIC_PIXEL_SHADER( portalstaticoverlay_ps20 );
  141. }
  142. }
  143. }
  144. DYNAMIC_STATE
  145. {
  146. pShaderAPI->SetDefaultState();
  147. float fStaticAmount = params[STATICAMOUNT]->GetFloatValue();
  148. //x is static, y is inverse static
  149. float pc0[4] = { fStaticAmount, 1.0f - fStaticAmount, 0.0f, 0.0f };
  150. pShaderAPI->SetPixelShaderConstant( 0, pc0 );
  151. if ( bStaticBlendTexture )
  152. {
  153. BindTexture( SHADER_SAMPLER0, !IsX360() ? TEXTURE_BINDFLAGS_SRGBREAD : TEXTURE_BINDFLAGS_NONE, STATICBLENDTEXTURE, STATICBLENDTEXTUREFRAME );
  154. if( bAlphaMaskTexture )
  155. BindTexture( SHADER_SAMPLER1, TEXTURE_BINDFLAGS_NONE, ALPHAMASKTEXTURE, ALPHAMASKTEXTUREFRAME );
  156. }
  157. else if( bAlphaMaskTexture )
  158. {
  159. BindTexture( SHADER_SAMPLER0, TEXTURE_BINDFLAGS_NONE, ALPHAMASKTEXTURE, ALPHAMASKTEXTUREFRAME );
  160. }
  161. pShaderAPI->SetPixelShaderFogParams( PSREG_FOG_PARAMS );
  162. float vEyePos_SpecExponent[4];
  163. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos_SpecExponent );
  164. vEyePos_SpecExponent[3] = 0.0f;
  165. pShaderAPI->SetPixelShaderConstant( PSREG_EYEPOS_SPEC_EXPONENT, vEyePos_SpecExponent, 1 );
  166. DECLARE_DYNAMIC_VERTEX_SHADER( portalstaticoverlay_vs20 );
  167. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  168. SET_DYNAMIC_VERTEX_SHADER( portalstaticoverlay_vs20 );
  169. // Avoid setting a pixel shader when only doing depth/stencil operations, as recommended by PIX
  170. if( bColorWrites || bAlphaMaskTexture || g_pHardwareConfig->PlatformRequiresNonNullPixelShaders() )
  171. {
  172. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  173. {
  174. DECLARE_DYNAMIC_PIXEL_SHADER( portalstaticoverlay_ps20b );
  175. SET_DYNAMIC_PIXEL_SHADER( portalstaticoverlay_ps20b );
  176. }
  177. else
  178. {
  179. DECLARE_DYNAMIC_PIXEL_SHADER( portalstaticoverlay_ps20 );
  180. SET_DYNAMIC_PIXEL_SHADER( portalstaticoverlay_ps20 );
  181. }
  182. }
  183. }
  184. Draw();
  185. }
  186. END_SHADER