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.

262 lines
8.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "BaseVSShader.h"
  8. #include "portal_vs20.inc"
  9. #include "portal_ps20.inc"
  10. #include "portal_ps20b.inc"
  11. #include "convar.h"
  12. #include "cpp_shader_constant_register_map.h"
  13. DEFINE_FALLBACK_SHADER( Portal, Portal_DX90 )
  14. BEGIN_VS_SHADER( Portal_DX90,
  15. "Help for Portal shader" )
  16. BEGIN_SHADER_PARAMS
  17. SHADER_PARAM_OVERRIDE( COLOR, SHADER_PARAM_TYPE_COLOR, "{255 255 255}", "unused", SHADER_PARAM_NOT_EDITABLE )
  18. SHADER_PARAM_OVERRIDE( ALPHA, SHADER_PARAM_TYPE_FLOAT, "1.0", "unused", SHADER_PARAM_NOT_EDITABLE )
  19. SHADER_PARAM( STATICAMOUNT, SHADER_PARAM_TYPE_FLOAT, "0.0", "Amount of the static blend texture to blend into the base texture" )
  20. SHADER_PARAM( STATICBLENDTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "When adding static, this is the texture that gets blended in" )
  21. SHADER_PARAM( STATICBLENDTEXTUREFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  22. SHADER_PARAM( ALPHAMASKTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "An alpha mask for odd shaped portals" )
  23. SHADER_PARAM( ALPHAMASKTEXTUREFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  24. SHADER_PARAM( RENDERFIXZ, SHADER_PARAM_TYPE_INTEGER, "0", "Special depth handling, intended for rendering bug workarounds for extremely close polygons" )
  25. SHADER_PARAM( USEALTERNATEVIEWMATRIX, SHADER_PARAM_TYPE_INTEGER, "1", "Use the alternate view matrix instead of the current view matrix" )
  26. SHADER_PARAM( ALTERNATEVIEWMATRIX, SHADER_PARAM_TYPE_MATRIX, "0", "The alternate view matrix to use when $usealternateviewmatrix is enabled" )
  27. END_SHADER_PARAMS
  28. SHADER_INIT_PARAMS()
  29. {
  30. SET_FLAGS( MATERIAL_VAR_TRANSLUCENT );
  31. if( !params[BASETEXTURE]->IsDefined() )
  32. {
  33. SET_FLAGS2( MATERIAL_VAR2_NEEDS_POWER_OF_TWO_FRAME_BUFFER_TEXTURE );
  34. }
  35. }
  36. SHADER_FALLBACK
  37. {
  38. if ( g_pHardwareConfig->GetDXSupportLevel() < 90 )
  39. return "Portal_DX80";
  40. return 0;
  41. }
  42. SHADER_INIT
  43. {
  44. if ( params[BASETEXTURE]->IsDefined() )
  45. {
  46. if ( IsX360() )
  47. {
  48. // prevent unused rt access
  49. IMaterialVar* pNameVar = params[BASETEXTURE];
  50. const char *pStringValue = pNameVar->GetStringValue();
  51. if ( !V_stricmp( pStringValue, "_rt_portal1" ) || !V_stricmp( pStringValue, "_rt_portal2" ) )
  52. {
  53. pNameVar->SetStringValue( "white" );
  54. }
  55. }
  56. LoadTexture( BASETEXTURE, TEXTUREFLAGS_SRGB );
  57. }
  58. if ( params[STATICBLENDTEXTURE]->IsDefined() )
  59. LoadTexture( STATICBLENDTEXTURE );
  60. if ( params[ALPHAMASKTEXTURE]->IsDefined() )
  61. LoadTexture( ALPHAMASKTEXTURE );
  62. if ( !params[STATICAMOUNT]->IsDefined() )
  63. params[STATICAMOUNT]->SetFloatValue( 0.0f );
  64. if ( !params[STATICAMOUNT]->IsDefined() )
  65. params[STATICAMOUNT]->SetFloatValue( 0.0f );
  66. if ( !params[STATICBLENDTEXTURE]->IsDefined() )
  67. params[STATICBLENDTEXTURE]->SetIntValue( 0 );
  68. if ( !params[STATICBLENDTEXTUREFRAME]->IsDefined() )
  69. params[STATICBLENDTEXTUREFRAME]->SetIntValue( 0 );
  70. if ( !params[ALPHAMASKTEXTURE]->IsDefined() )
  71. params[ALPHAMASKTEXTURE]->SetIntValue( 0 );
  72. if ( !params[ALPHAMASKTEXTUREFRAME]->IsDefined() )
  73. params[ALPHAMASKTEXTUREFRAME]->SetIntValue( 0 );
  74. if ( !params[RENDERFIXZ]->IsDefined() )
  75. params[RENDERFIXZ]->SetIntValue( 0 );
  76. if ( !params[USEALTERNATEVIEWMATRIX]->IsDefined() )
  77. params[USEALTERNATEVIEWMATRIX]->SetIntValue( 0 );
  78. if ( !params[ALTERNATEVIEWMATRIX]->IsDefined() )
  79. {
  80. VMatrix matIdentity;
  81. matIdentity.Identity();
  82. params[ALTERNATEVIEWMATRIX]->SetMatrixValue( matIdentity );
  83. }
  84. }
  85. SHADER_DRAW
  86. {
  87. bool bStaticBlendTexture = params[STATICBLENDTEXTURE]->IsTexture();
  88. bool bAlphaMaskTexture = ( params[ALPHAMASKTEXTURE]->IsTexture()? 1 : 0 );
  89. float fStaticAmount = params[STATICAMOUNT]->GetFloatValue();
  90. SHADOW_STATE
  91. {
  92. SetInitialShadowState();
  93. FogToFogColor();
  94. if( params[RENDERFIXZ]->GetIntValue() == 0 )
  95. {
  96. //pShaderShadow->EnablePolyOffset( SHADER_POLYOFFSET_DECAL ); //a portal is effectively a decal on top of a wall
  97. pShaderShadow->DepthFunc( SHADER_DEPTHFUNC_NEAREROREQUAL );
  98. }
  99. else
  100. {
  101. pShaderShadow->EnablePolyOffset( SHADER_POLYOFFSET_DISABLE );
  102. pShaderShadow->DepthFunc( SHADER_DEPTHFUNC_ALWAYS );
  103. pShaderShadow->EnableDepthTest( false );
  104. pShaderShadow->EnableDepthWrites( false );
  105. }
  106. pShaderShadow->EnableAlphaTest( true );
  107. if( bAlphaMaskTexture )
  108. {
  109. pShaderShadow->EnableBlending( true );
  110. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  111. }
  112. else
  113. {
  114. pShaderShadow->EnableBlending( false );
  115. }
  116. pShaderShadow->EnableSRGBWrite( true );
  117. int fmt = VERTEX_POSITION | VERTEX_NORMAL;
  118. int userDataSize = 0;
  119. int iTexCoords = 1;
  120. if( IS_FLAG_SET( MATERIAL_VAR_MODEL ) )
  121. {
  122. userDataSize = 4;
  123. }
  124. else
  125. {
  126. fmt |= VERTEX_TANGENT_S | VERTEX_TANGENT_T;
  127. }
  128. pShaderShadow->VertexShaderVertexFormat( fmt, iTexCoords, NULL, userDataSize );
  129. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  130. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  131. if( bStaticBlendTexture || bAlphaMaskTexture )
  132. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  133. if( bStaticBlendTexture && bAlphaMaskTexture )
  134. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  135. DECLARE_STATIC_VERTEX_SHADER( portal_vs20 );
  136. SET_STATIC_VERTEX_SHADER_COMBO( HASALPHAMASK, bAlphaMaskTexture );
  137. SET_STATIC_VERTEX_SHADER_COMBO( HASSTATICTEXTURE, bStaticBlendTexture );
  138. SET_STATIC_VERTEX_SHADER_COMBO( USEALTERNATEVIEW, (params[USEALTERNATEVIEWMATRIX]->GetIntValue() != 0) );
  139. SET_STATIC_VERTEX_SHADER( portal_vs20 );
  140. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  141. {
  142. DECLARE_STATIC_PIXEL_SHADER( portal_ps20b );
  143. SET_STATIC_PIXEL_SHADER_COMBO( HASALPHAMASK, bAlphaMaskTexture );
  144. SET_STATIC_PIXEL_SHADER_COMBO( HASSTATICTEXTURE, bStaticBlendTexture );
  145. SET_STATIC_PIXEL_SHADER( portal_ps20b );
  146. }
  147. else
  148. {
  149. DECLARE_STATIC_PIXEL_SHADER( portal_ps20 );
  150. SET_STATIC_PIXEL_SHADER_COMBO( HASALPHAMASK, bAlphaMaskTexture );
  151. SET_STATIC_PIXEL_SHADER_COMBO( HASSTATICTEXTURE, bStaticBlendTexture );
  152. SET_STATIC_PIXEL_SHADER( portal_ps20 );
  153. }
  154. }
  155. DYNAMIC_STATE
  156. {
  157. pShaderAPI->SetDefaultState();
  158. //x is static, y is inverse static
  159. float pc0[4] = { fStaticAmount, 1.0f - fStaticAmount, 0.0f, 0.0f };
  160. pShaderAPI->SetPixelShaderConstant( 0, pc0 );
  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. if ( params[BASETEXTURE]->IsTexture() )
  167. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  168. else
  169. pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, TEXTURE_FRAME_BUFFER_FULL_TEXTURE_0 );
  170. bool bHasStatic = (fStaticAmount > 0.0f);
  171. bool bUsingStaticTexture = (bStaticBlendTexture && bHasStatic);
  172. if ( bAlphaMaskTexture )
  173. {
  174. BindTexture( SHADER_SAMPLER1, ALPHAMASKTEXTURE, ALPHAMASKTEXTUREFRAME );
  175. if ( bUsingStaticTexture )
  176. BindTexture( SHADER_SAMPLER2, STATICBLENDTEXTURE, STATICBLENDTEXTUREFRAME );
  177. }
  178. else
  179. {
  180. if ( bUsingStaticTexture )
  181. BindTexture( SHADER_SAMPLER1, STATICBLENDTEXTURE, STATICBLENDTEXTUREFRAME );
  182. }
  183. if ( params[USEALTERNATEVIEWMATRIX]->GetIntValue() != 0 )
  184. {
  185. const VMatrix &matCustomView = params[ALTERNATEVIEWMATRIX]->GetMatrixValue();
  186. VMatrix matProj;
  187. pShaderAPI->GetMatrix( MATERIAL_PROJECTION, matProj.Base() );
  188. MatrixTranspose( matProj, matProj );
  189. VMatrix matFinal;
  190. MatrixMultiply( matProj, matCustomView, matFinal );
  191. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, matFinal.Base(), 4 );
  192. }
  193. DECLARE_DYNAMIC_VERTEX_SHADER( portal_vs20 );
  194. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  195. SET_DYNAMIC_VERTEX_SHADER_COMBO( ADDSTATIC, bHasStatic );
  196. SET_DYNAMIC_VERTEX_SHADER( portal_vs20 );
  197. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  198. {
  199. DECLARE_DYNAMIC_PIXEL_SHADER( portal_ps20b );
  200. SET_DYNAMIC_PIXEL_SHADER_COMBO( ADDSTATIC, bHasStatic );
  201. SET_DYNAMIC_PIXEL_SHADER_COMBO( HDRENABLED, IsHDREnabled() );
  202. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  203. SET_DYNAMIC_PIXEL_SHADER( portal_ps20b );
  204. }
  205. else
  206. {
  207. DECLARE_DYNAMIC_PIXEL_SHADER( portal_ps20 );
  208. SET_DYNAMIC_PIXEL_SHADER_COMBO( ADDSTATIC, bHasStatic );
  209. SET_DYNAMIC_PIXEL_SHADER_COMBO( HDRENABLED, IsHDREnabled() );
  210. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  211. SET_DYNAMIC_PIXEL_SHADER( portal_ps20 );
  212. }
  213. }
  214. Draw();
  215. }
  216. END_SHADER