Team Fortress 2 Source Code as on 22/4/2020
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.

198 lines
6.7 KiB

  1. //========= Copyright 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. BEGIN_VS_SHADER( PortalStaticOverlay,
  14. "Help for PortalStaticOverlay shader" )
  15. BEGIN_SHADER_PARAMS
  16. SHADER_PARAM_OVERRIDE( COLOR, SHADER_PARAM_TYPE_COLOR, "{255 255 255}", "unused", SHADER_PARAM_NOT_EDITABLE )
  17. SHADER_PARAM_OVERRIDE( ALPHA, SHADER_PARAM_TYPE_FLOAT, "1.0", "unused", SHADER_PARAM_NOT_EDITABLE )
  18. SHADER_PARAM( STATICAMOUNT, SHADER_PARAM_TYPE_FLOAT, "0.0", "Amount of the static blend texture to blend into the base texture" )
  19. SHADER_PARAM( STATICBLENDTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "When adding static, this is the texture that gets blended in" )
  20. SHADER_PARAM( STATICBLENDTEXTUREFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  21. SHADER_PARAM( ALPHAMASKTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "An alpha mask for odd shaped portals" )
  22. SHADER_PARAM( ALPHAMASKTEXTUREFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  23. SHADER_PARAM( NOCOLORWRITE, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  24. END_SHADER_PARAMS
  25. SHADER_INIT_PARAMS()
  26. {
  27. SET_FLAGS( MATERIAL_VAR_TRANSLUCENT );
  28. }
  29. SHADER_FALLBACK
  30. {
  31. if( !g_pHardwareConfig->SupportsVertexAndPixelShaders() )
  32. return "PortalStaticOverlay_DX60";
  33. return 0;
  34. }
  35. SHADER_INIT
  36. {
  37. if( params[STATICBLENDTEXTURE]->IsDefined() )
  38. LoadTexture( STATICBLENDTEXTURE );
  39. if( params[ALPHAMASKTEXTURE]->IsDefined() )
  40. LoadTexture( ALPHAMASKTEXTURE );
  41. if( !params[STATICAMOUNT]->IsDefined() )
  42. params[STATICAMOUNT]->SetFloatValue( 0.0f );
  43. if( !params[STATICBLENDTEXTURE]->IsDefined() )
  44. params[STATICBLENDTEXTURE]->SetIntValue( 0 );
  45. if( !params[STATICBLENDTEXTUREFRAME]->IsDefined() )
  46. params[STATICBLENDTEXTUREFRAME]->SetIntValue( 0 );
  47. if( !params[ALPHAMASKTEXTURE]->IsDefined() )
  48. params[ALPHAMASKTEXTURE]->SetIntValue( 0 );
  49. if( !params[ALPHAMASKTEXTUREFRAME]->IsDefined() )
  50. params[ALPHAMASKTEXTUREFRAME]->SetIntValue( 0 );
  51. if( !params[NOCOLORWRITE]->IsDefined() )
  52. params[NOCOLORWRITE]->SetIntValue( 0 );
  53. }
  54. SHADER_DRAW
  55. {
  56. bool bStaticBlendTexture = params[STATICBLENDTEXTURE]->IsTexture();
  57. bool bAlphaMaskTexture = params[ALPHAMASKTEXTURE]->IsTexture(); //must support 2 texture stages to use a mask
  58. bool bIsModel = IS_FLAG_SET( MATERIAL_VAR_MODEL );
  59. bool bColorWrites = params[NOCOLORWRITE]->GetIntValue() == 0;
  60. SHADOW_STATE
  61. {
  62. SetInitialShadowState();
  63. FogToFogColor();
  64. //pShaderShadow->EnablePolyOffset( SHADER_POLYOFFSET_DECAL ); //a portal is effectively a decal on top of a wall
  65. pShaderShadow->DepthFunc( SHADER_DEPTHFUNC_NEAREROREQUAL );
  66. pShaderShadow->EnableDepthWrites( true );
  67. if( g_pHardwareConfig->GetHDRType() != HDR_TYPE_NONE )
  68. {
  69. pShaderShadow->EnableSRGBWrite( true );
  70. }
  71. pShaderShadow->EnableBlending( true );
  72. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  73. pShaderShadow->EnableAlphaTest( true );
  74. pShaderShadow->AlphaFunc( SHADER_ALPHAFUNC_GREATER, 0.0f );
  75. pShaderShadow->EnableColorWrites( bColorWrites );
  76. if( g_pHardwareConfig->GetHDRType() != HDR_TYPE_NONE )
  77. pShaderShadow->EnableSRGBWrite( true );
  78. if( bStaticBlendTexture || bAlphaMaskTexture )
  79. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  80. if( bStaticBlendTexture && bAlphaMaskTexture )
  81. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  82. int fmt = VERTEX_POSITION | VERTEX_NORMAL;
  83. int userDataSize = 0;
  84. if( bIsModel )
  85. {
  86. userDataSize = 4;
  87. }
  88. else
  89. {
  90. fmt |= VERTEX_TANGENT_S | VERTEX_TANGENT_T;
  91. }
  92. pShaderShadow->VertexShaderVertexFormat( fmt, 1, 0, userDataSize );
  93. DECLARE_STATIC_VERTEX_SHADER( portalstaticoverlay_vs20 );
  94. SET_STATIC_VERTEX_SHADER_COMBO( MODEL, bIsModel );
  95. SET_STATIC_VERTEX_SHADER( portalstaticoverlay_vs20 );
  96. // Avoid setting a pixel shader when only doing depth/stencil operations, as recommended by PIX
  97. if( bColorWrites || bAlphaMaskTexture || g_pHardwareConfig->PlatformRequiresNonNullPixelShaders() )
  98. {
  99. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  100. {
  101. DECLARE_STATIC_PIXEL_SHADER( portalstaticoverlay_ps20b );
  102. SET_STATIC_PIXEL_SHADER_COMBO( HASALPHAMASK, bAlphaMaskTexture );
  103. SET_STATIC_PIXEL_SHADER_COMBO( HASSTATICTEXTURE, bStaticBlendTexture );
  104. SET_STATIC_PIXEL_SHADER( portalstaticoverlay_ps20b );
  105. }
  106. else
  107. {
  108. DECLARE_STATIC_PIXEL_SHADER( portalstaticoverlay_ps20 );
  109. SET_STATIC_PIXEL_SHADER_COMBO( HASALPHAMASK, bAlphaMaskTexture );
  110. SET_STATIC_PIXEL_SHADER_COMBO( HASSTATICTEXTURE, bStaticBlendTexture );
  111. SET_STATIC_PIXEL_SHADER( portalstaticoverlay_ps20 );
  112. }
  113. }
  114. }
  115. DYNAMIC_STATE
  116. {
  117. pShaderAPI->SetDefaultState();
  118. float fStaticAmount = params[STATICAMOUNT]->GetFloatValue();
  119. //x is static, y is inverse static
  120. float pc0[4] = { fStaticAmount, 1.0f - fStaticAmount, 0.0f, 0.0f };
  121. pShaderAPI->SetPixelShaderConstant( 0, pc0 );
  122. if ( bStaticBlendTexture )
  123. {
  124. BindTexture( SHADER_SAMPLER0, STATICBLENDTEXTURE, STATICBLENDTEXTUREFRAME );
  125. if( bAlphaMaskTexture )
  126. BindTexture( SHADER_SAMPLER1, ALPHAMASKTEXTURE, ALPHAMASKTEXTUREFRAME );
  127. }
  128. else if( bAlphaMaskTexture )
  129. {
  130. BindTexture( SHADER_SAMPLER0, ALPHAMASKTEXTURE, ALPHAMASKTEXTUREFRAME );
  131. }
  132. pShaderAPI->SetPixelShaderFogParams( PSREG_FOG_PARAMS );
  133. float vEyePos_SpecExponent[4];
  134. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos_SpecExponent );
  135. vEyePos_SpecExponent[3] = 0.0f;
  136. pShaderAPI->SetPixelShaderConstant( PSREG_EYEPOS_SPEC_EXPONENT, vEyePos_SpecExponent, 1 );
  137. DECLARE_DYNAMIC_VERTEX_SHADER( portalstaticoverlay_vs20 );
  138. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  139. SET_DYNAMIC_VERTEX_SHADER( portalstaticoverlay_vs20 );
  140. // Avoid setting a pixel shader when only doing depth/stencil operations, as recommended by PIX
  141. if( bColorWrites || bAlphaMaskTexture || g_pHardwareConfig->PlatformRequiresNonNullPixelShaders() )
  142. {
  143. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  144. {
  145. DECLARE_DYNAMIC_PIXEL_SHADER( portalstaticoverlay_ps20b );
  146. SET_DYNAMIC_PIXEL_SHADER_COMBO( HDRENABLED, IsHDREnabled() );
  147. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  148. SET_DYNAMIC_PIXEL_SHADER( portalstaticoverlay_ps20b );
  149. }
  150. else
  151. {
  152. DECLARE_DYNAMIC_PIXEL_SHADER( portalstaticoverlay_ps20 );
  153. SET_DYNAMIC_PIXEL_SHADER_COMBO( HDRENABLED, IsHDREnabled() );
  154. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  155. SET_DYNAMIC_PIXEL_SHADER( portalstaticoverlay_ps20 );
  156. }
  157. }
  158. }
  159. Draw();
  160. }
  161. END_SHADER