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.

307 lines
10 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "BaseVSShader.h"
  8. #include "portal_vs11.inc"
  9. #include "portal_ps11.inc"
  10. #include "convar.h"
  11. DEFINE_FALLBACK_SHADER( Portal, Portal_DX80 )
  12. BEGIN_VS_SHADER( Portal_DX80,
  13. "Help for Portal shader" )
  14. BEGIN_SHADER_PARAMS
  15. SHADER_PARAM_OVERRIDE( COLOR, SHADER_PARAM_TYPE_COLOR, "{255 255 255}", "unused", SHADER_PARAM_NOT_EDITABLE )
  16. SHADER_PARAM_OVERRIDE( ALPHA, SHADER_PARAM_TYPE_FLOAT, "1.0", "unused", SHADER_PARAM_NOT_EDITABLE )
  17. SHADER_PARAM( STATICAMOUNT, SHADER_PARAM_TYPE_FLOAT, "0.0", "Amount of the static blend texture to blend into the base texture" )
  18. SHADER_PARAM( STATICBLENDTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "When adding static, this is the texture that gets blended in" )
  19. SHADER_PARAM( STATICBLENDTEXTUREFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  20. SHADER_PARAM( ALPHAMASKTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "An alpha mask for odd shaped portals" )
  21. SHADER_PARAM( ALPHAMASKTEXTUREFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  22. SHADER_PARAM( RENDERFIXZ, SHADER_PARAM_TYPE_INTEGER, "0", "Special depth handling, intended for rendering bug workarounds for extremely close polygons" )
  23. SHADER_PARAM( USEALTERNATEVIEWMATRIX, SHADER_PARAM_TYPE_INTEGER, "1", "Use the alternate view matrix instead of the current view matrix" )
  24. SHADER_PARAM( ALTERNATEVIEWMATRIX, SHADER_PARAM_TYPE_MATRIX, "0", "The alternate view matrix to use when $usealternateviewmatrix is enabled" )
  25. END_SHADER_PARAMS
  26. SHADER_INIT_PARAMS()
  27. {
  28. SET_FLAGS( MATERIAL_VAR_TRANSLUCENT );
  29. if( !params[BASETEXTURE]->IsDefined() )
  30. {
  31. SET_FLAGS2( MATERIAL_VAR2_NEEDS_POWER_OF_TWO_FRAME_BUFFER_TEXTURE );
  32. }
  33. }
  34. SHADER_FALLBACK
  35. {
  36. if( !g_pHardwareConfig->SupportsVertexAndPixelShaders() )
  37. return "Portal_DX60";
  38. return 0;
  39. }
  40. int iMaxTextureStages;
  41. SHADER_INIT
  42. {
  43. iMaxTextureStages = min( 3, g_pHardwareConfig->GetSamplerCount() );
  44. if (params[BASETEXTURE]->IsDefined() )
  45. LoadTexture( BASETEXTURE );
  46. if( params[STATICBLENDTEXTURE]->IsDefined() )
  47. LoadTexture( STATICBLENDTEXTURE );
  48. if( (iMaxTextureStages > 1) && params[ALPHAMASKTEXTURE]->IsDefined() )
  49. LoadTexture( ALPHAMASKTEXTURE );
  50. if( !params[STATICAMOUNT]->IsDefined() )
  51. params[STATICAMOUNT]->SetFloatValue( 0.0f );
  52. if( !params[STATICAMOUNT]->IsDefined() )
  53. params[STATICAMOUNT]->SetFloatValue( 0.0f );
  54. if( !params[STATICBLENDTEXTURE]->IsDefined() )
  55. params[STATICBLENDTEXTURE]->SetIntValue( 0 );
  56. if( !params[STATICBLENDTEXTUREFRAME]->IsDefined() )
  57. params[STATICBLENDTEXTUREFRAME]->SetIntValue( 0 );
  58. if( !params[ALPHAMASKTEXTURE]->IsDefined() )
  59. params[ALPHAMASKTEXTURE]->SetIntValue( 0 );
  60. if( !params[ALPHAMASKTEXTUREFRAME]->IsDefined() )
  61. params[ALPHAMASKTEXTUREFRAME]->SetIntValue( 0 );
  62. if( !params[RENDERFIXZ]->IsDefined() )
  63. params[RENDERFIXZ]->SetIntValue( 0 );
  64. if ( !params[USEALTERNATEVIEWMATRIX]->IsDefined() )
  65. params[USEALTERNATEVIEWMATRIX]->SetIntValue( 0 );
  66. if ( !params[ALTERNATEVIEWMATRIX]->IsDefined() )
  67. {
  68. VMatrix matIdentity;
  69. matIdentity.Identity();
  70. params[ALTERNATEVIEWMATRIX]->SetMatrixValue( matIdentity );
  71. }
  72. }
  73. SHADER_DRAW
  74. {
  75. bool bStaticBlendTexture = params[STATICBLENDTEXTURE]->IsTexture();
  76. bool bAlphaMaskTexture = ((iMaxTextureStages > 1) && (params[ALPHAMASKTEXTURE]->IsTexture()))?(1):(0); //must support at least 2 texture stages to use any kind of mask
  77. float fStaticAmount = params[STATICAMOUNT]->GetFloatValue();
  78. SHADOW_STATE
  79. {
  80. SetInitialShadowState();
  81. FogToFogColor();
  82. if( params[RENDERFIXZ]->GetIntValue() == 0 )
  83. {
  84. //pShaderShadow->EnablePolyOffset( SHADER_POLYOFFSET_DECAL ); //a portal is effectively a decal on top of a wall
  85. pShaderShadow->DepthFunc( SHADER_DEPTHFUNC_NEAREROREQUAL );
  86. }
  87. else
  88. {
  89. pShaderShadow->EnablePolyOffset( SHADER_POLYOFFSET_DISABLE );
  90. pShaderShadow->DepthFunc( SHADER_DEPTHFUNC_ALWAYS );
  91. pShaderShadow->EnableDepthTest( false );
  92. pShaderShadow->EnableDepthWrites( false );
  93. }
  94. pShaderShadow->EnableAlphaTest( true );
  95. if( ((iMaxTextureStages < 3) && bStaticBlendTexture) || bAlphaMaskTexture ) //in multipass modes, we need alpha to mix portal static, other
  96. {
  97. pShaderShadow->EnableBlending( true );
  98. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  99. }
  100. else
  101. {
  102. pShaderShadow->EnableBlending( false );
  103. }
  104. int fmt = VERTEX_POSITION | VERTEX_NORMAL;
  105. int userDataSize = 0;
  106. int iTexCoords = 1;
  107. if( IS_FLAG_SET( MATERIAL_VAR_MODEL ) )
  108. {
  109. userDataSize = 4;
  110. }
  111. else
  112. {
  113. fmt |= VERTEX_TANGENT_S | VERTEX_TANGENT_T;
  114. }
  115. pShaderShadow->VertexShaderVertexFormat( fmt, iTexCoords, NULL, userDataSize );
  116. DECLARE_STATIC_VERTEX_SHADER( portal_vs11 );
  117. SET_STATIC_VERTEX_SHADER_COMBO( MAXTEXTURESTAGES, (iMaxTextureStages - 1) );
  118. SET_STATIC_VERTEX_SHADER_COMBO( HASALPHAMASK, bAlphaMaskTexture );
  119. SET_STATIC_VERTEX_SHADER_COMBO( HASSTATICTEXTURE, bStaticBlendTexture );
  120. SET_STATIC_VERTEX_SHADER_COMBO( USEALTERNATEVIEW, (params[USEALTERNATEVIEWMATRIX]->GetIntValue() != 0) );
  121. SET_STATIC_VERTEX_SHADER( portal_vs11 );
  122. DECLARE_STATIC_PIXEL_SHADER( portal_ps11 );
  123. SET_STATIC_PIXEL_SHADER_COMBO( MAXTEXTURESTAGES, (iMaxTextureStages - 1) );
  124. SET_STATIC_PIXEL_SHADER_COMBO( HASALPHAMASK, bAlphaMaskTexture );
  125. SET_STATIC_PIXEL_SHADER_COMBO( HASSTATICTEXTURE, bStaticBlendTexture );
  126. SET_STATIC_PIXEL_SHADER( portal_ps11 );
  127. }
  128. DYNAMIC_STATE
  129. {
  130. pShaderAPI->SetDefaultState();
  131. //x is static, y is inverse static
  132. float pc0[4] = { fStaticAmount, 1.0f - fStaticAmount, 0.0f, 0.0f };
  133. pShaderAPI->SetPixelShaderConstant( 0, pc0 );
  134. if ( params[USEALTERNATEVIEWMATRIX]->GetIntValue() != 0 )
  135. {
  136. const VMatrix &matCustomView = params[ALTERNATEVIEWMATRIX]->GetMatrixValue();
  137. VMatrix matProj;
  138. pShaderAPI->GetMatrix( MATERIAL_PROJECTION, matProj.Base() );
  139. MatrixTranspose( matProj, matProj );
  140. VMatrix matFinal;
  141. MatrixMultiply( matProj, matCustomView, matFinal );
  142. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, matFinal.Base(), 4 );
  143. }
  144. }
  145. if( iMaxTextureStages == 3 )
  146. {
  147. SinglePass( pShaderShadow, pShaderAPI, params, fStaticAmount, bStaticBlendTexture, bAlphaMaskTexture );
  148. }
  149. else
  150. {
  151. MultiPass_CutoutPass( pShaderShadow, pShaderAPI, params, bAlphaMaskTexture );
  152. if( bStaticBlendTexture ) //if we don't have a static texture, the cutout pass will mix in gray for any static amount
  153. MultiPass_StaticPass( pShaderShadow, pShaderAPI, params, bAlphaMaskTexture );
  154. }
  155. }
  156. void SinglePass( IShaderShadow *pShaderShadow, IShaderDynamicAPI *pShaderAPI, IMaterialVar **params, float fStaticAmount, bool bStaticBlendTexture, bool bAlphaMaskTexture )
  157. {
  158. SHADOW_STATE
  159. {
  160. // source render target that contains the image that we are warping.
  161. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  162. if( bStaticBlendTexture || bAlphaMaskTexture )
  163. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  164. if( bStaticBlendTexture && bAlphaMaskTexture )
  165. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  166. }
  167. DYNAMIC_STATE
  168. {
  169. if ( params[BASETEXTURE]->IsTexture() )
  170. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  171. else
  172. pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, TEXTURE_FRAME_BUFFER_FULL_TEXTURE_0 );
  173. bool bHasStatic = (fStaticAmount > 0.0f);
  174. bool bUsingStaticTexture = (bStaticBlendTexture && bHasStatic);
  175. if ( bAlphaMaskTexture )
  176. {
  177. BindTexture( SHADER_SAMPLER1, ALPHAMASKTEXTURE, ALPHAMASKTEXTUREFRAME );
  178. if ( bUsingStaticTexture )
  179. BindTexture( SHADER_SAMPLER2, STATICBLENDTEXTURE, STATICBLENDTEXTUREFRAME );
  180. }
  181. else
  182. {
  183. if ( bUsingStaticTexture )
  184. BindTexture( SHADER_SAMPLER1, STATICBLENDTEXTURE, STATICBLENDTEXTUREFRAME );
  185. }
  186. int iHasStatic = bHasStatic?1:0;
  187. DECLARE_DYNAMIC_VERTEX_SHADER( portal_vs11 );
  188. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  189. SET_DYNAMIC_VERTEX_SHADER_COMBO( ADDSTATIC, iHasStatic );
  190. SET_DYNAMIC_VERTEX_SHADER( portal_vs11 );
  191. DECLARE_DYNAMIC_PIXEL_SHADER( portal_ps11 );
  192. SET_DYNAMIC_PIXEL_SHADER_COMBO( ADDSTATIC, iHasStatic );
  193. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  194. SET_DYNAMIC_PIXEL_SHADER( portal_ps11 );
  195. }
  196. Draw();
  197. }
  198. void MultiPass_CutoutPass( IShaderShadow *pShaderShadow, IShaderDynamicAPI *pShaderAPI, IMaterialVar **params, bool bAlphaMaskTexture )
  199. {
  200. //the basic portal effect
  201. SHADOW_STATE
  202. {
  203. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  204. if( bAlphaMaskTexture )
  205. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  206. }
  207. DYNAMIC_STATE
  208. {
  209. if ( params[BASETEXTURE]->IsTexture() )
  210. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  211. else
  212. pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, TEXTURE_FRAME_BUFFER_FULL_TEXTURE_0 );
  213. if( bAlphaMaskTexture )
  214. BindTexture( SHADER_SAMPLER1, ALPHAMASKTEXTURE, ALPHAMASKTEXTUREFRAME );
  215. DECLARE_DYNAMIC_VERTEX_SHADER( portal_vs11 );
  216. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  217. SET_DYNAMIC_VERTEX_SHADER_COMBO( ADDSTATIC, 0 );
  218. SET_DYNAMIC_VERTEX_SHADER( portal_vs11 );
  219. DECLARE_DYNAMIC_PIXEL_SHADER( portal_ps11 );
  220. SET_DYNAMIC_PIXEL_SHADER_COMBO( ADDSTATIC, 0 );
  221. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  222. SET_DYNAMIC_PIXEL_SHADER( portal_ps11 );
  223. }
  224. Draw();
  225. }
  226. void MultiPass_StaticPass( IShaderShadow *pShaderShadow, IShaderDynamicAPI *pShaderAPI, IMaterialVar **params, bool bAlphaMaskTexture )
  227. {
  228. SHADOW_STATE
  229. {
  230. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  231. if( bAlphaMaskTexture )
  232. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  233. }
  234. DYNAMIC_STATE
  235. {
  236. BindTexture( SHADER_SAMPLER0, STATICBLENDTEXTURE, STATICBLENDTEXTUREFRAME );
  237. if( bAlphaMaskTexture )
  238. BindTexture( SHADER_SAMPLER1, ALPHAMASKTEXTURE, ALPHAMASKTEXTUREFRAME );
  239. DECLARE_DYNAMIC_VERTEX_SHADER( portal_vs11 );
  240. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  241. SET_DYNAMIC_VERTEX_SHADER_COMBO( ADDSTATIC, 1 );
  242. SET_DYNAMIC_VERTEX_SHADER( portal_vs11 );
  243. DECLARE_DYNAMIC_PIXEL_SHADER( portal_ps11 );
  244. SET_DYNAMIC_PIXEL_SHADER_COMBO( ADDSTATIC, 1 );
  245. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  246. SET_DYNAMIC_PIXEL_SHADER( portal_ps11 );
  247. }
  248. Draw();
  249. }
  250. END_SHADER