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.

302 lines
10 KiB

  1. //========= Copyright � 1996-2005, 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. // NOTE: This has to be the last file included!
  14. #include "tier0/memdbgon.h"
  15. DEFINE_FALLBACK_SHADER( Portal, Portal_DX90 )
  16. BEGIN_VS_SHADER( Portal_DX90,
  17. "Help for Portal shader" )
  18. BEGIN_SHADER_PARAMS
  19. SHADER_PARAM_OVERRIDE( COLOR, SHADER_PARAM_TYPE_COLOR, "{255 255 255}", "unused", SHADER_PARAM_NOT_EDITABLE )
  20. SHADER_PARAM_OVERRIDE( ALPHA, SHADER_PARAM_TYPE_FLOAT, "1.0", "unused", SHADER_PARAM_NOT_EDITABLE )
  21. SHADER_PARAM( STATICAMOUNT, SHADER_PARAM_TYPE_FLOAT, "0.0", "Amount of the static blend texture to blend into the base texture" )
  22. SHADER_PARAM( STATICBLENDTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "When adding static, this is the texture that gets blended in" )
  23. SHADER_PARAM( STATICBLENDTEXTUREFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  24. SHADER_PARAM( ALPHAMASKTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "An alpha mask for odd shaped portals" )
  25. SHADER_PARAM( ALPHAMASKTEXTUREFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  26. SHADER_PARAM( RENDERFIXZ, SHADER_PARAM_TYPE_INTEGER, "0", "Special depth handling, intended for rendering bug workarounds for extremely close polygons" )
  27. SHADER_PARAM( USEALTERNATEVIEWMATRIX, SHADER_PARAM_TYPE_INTEGER, "1", "Use the alternate view matrix instead of the current view matrix" )
  28. SHADER_PARAM( ALTERNATEVIEWMATRIX, SHADER_PARAM_TYPE_MATRIX, "0", "The alternate view matrix to use when $usealternateviewmatrix is enabled" )
  29. END_SHADER_PARAMS
  30. SHADER_INIT_PARAMS()
  31. {
  32. SET_FLAGS( MATERIAL_VAR_TRANSLUCENT );
  33. if( !params[BASETEXTURE]->IsDefined() )
  34. {
  35. SET_FLAGS2( MATERIAL_VAR2_NEEDS_POWER_OF_TWO_FRAME_BUFFER_TEXTURE );
  36. }
  37. }
  38. SHADER_FALLBACK
  39. {
  40. return 0;
  41. }
  42. SHADER_INIT
  43. {
  44. if ( params[BASETEXTURE]->IsDefined() )
  45. {
  46. if ( IsGameConsole() )
  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 = 2;
  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. // Sampler for nvidia's stereo hackery
  136. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  137. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER3, false );
  138. DECLARE_STATIC_VERTEX_SHADER( portal_vs20 );
  139. SET_STATIC_VERTEX_SHADER_COMBO( HASALPHAMASK, bAlphaMaskTexture );
  140. SET_STATIC_VERTEX_SHADER_COMBO( HASSTATICTEXTURE, bStaticBlendTexture );
  141. SET_STATIC_VERTEX_SHADER_COMBO( USEALTERNATEVIEW, (params[USEALTERNATEVIEWMATRIX]->GetIntValue() != 0) );
  142. SET_STATIC_VERTEX_SHADER( portal_vs20 );
  143. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  144. {
  145. DECLARE_STATIC_PIXEL_SHADER( portal_ps20b );
  146. SET_STATIC_PIXEL_SHADER_COMBO( HASALPHAMASK, bAlphaMaskTexture );
  147. SET_STATIC_PIXEL_SHADER_COMBO( HASSTATICTEXTURE, bStaticBlendTexture );
  148. SET_STATIC_PIXEL_SHADER( portal_ps20b );
  149. }
  150. else
  151. {
  152. DECLARE_STATIC_PIXEL_SHADER( portal_ps20 );
  153. SET_STATIC_PIXEL_SHADER_COMBO( HASALPHAMASK, bAlphaMaskTexture );
  154. SET_STATIC_PIXEL_SHADER_COMBO( HASSTATICTEXTURE, bStaticBlendTexture );
  155. SET_STATIC_PIXEL_SHADER( portal_ps20 );
  156. }
  157. }
  158. DYNAMIC_STATE
  159. {
  160. pShaderAPI->SetDefaultState();
  161. //x is static, y is inverse static
  162. float pc0[4] = { fStaticAmount, 1.0f - fStaticAmount, 0.0f, 0.0f };
  163. pShaderAPI->SetPixelShaderConstant( 0, pc0 );
  164. pShaderAPI->SetPixelShaderFogParams( PSREG_FOG_PARAMS );
  165. float vEyePos_SpecExponent[4];
  166. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos_SpecExponent );
  167. vEyePos_SpecExponent[3] = 0.0f;
  168. pShaderAPI->SetPixelShaderConstant( PSREG_EYEPOS_SPEC_EXPONENT, vEyePos_SpecExponent, 1 );
  169. if ( params[BASETEXTURE]->IsTexture() )
  170. BindTexture( SHADER_SAMPLER0, TEXTURE_BINDFLAGS_SRGBREAD, BASETEXTURE, FRAME );
  171. else
  172. pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, TEXTURE_BINDFLAGS_SRGBREAD, 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, TEXTURE_BINDFLAGS_NONE, ALPHAMASKTEXTURE, ALPHAMASKTEXTUREFRAME );
  178. if ( bUsingStaticTexture )
  179. BindTexture( SHADER_SAMPLER2, TEXTURE_BINDFLAGS_NONE, STATICBLENDTEXTURE, STATICBLENDTEXTUREFRAME );
  180. }
  181. else
  182. {
  183. if ( bUsingStaticTexture )
  184. BindTexture( SHADER_SAMPLER1, TEXTURE_BINDFLAGS_NONE, STATICBLENDTEXTURE, STATICBLENDTEXTUREFRAME );
  185. }
  186. if ( params[USEALTERNATEVIEWMATRIX]->GetIntValue() != 0 )
  187. {
  188. const VMatrix &matCustomView = params[ALTERNATEVIEWMATRIX]->GetMatrixValue();
  189. VMatrix matProj;
  190. pShaderAPI->GetMatrix( MATERIAL_PROJECTION, matProj.Base() );
  191. MatrixTranspose( matProj, matProj );
  192. VMatrix matFinal;
  193. MatrixMultiply( matProj, matCustomView, matFinal );
  194. #ifdef _PS3
  195. // PS3's Cg likes things in row-major rather than column-major
  196. MatrixTranspose( matFinal, matFinal );
  197. #endif // _PS3
  198. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, matFinal.Base(), 4 );
  199. }
  200. // Get viewport and render target dimensions and set shader constant to do a 2D mad
  201. {
  202. int nViewportX, nViewportY, nViewportWidth, nViewportHeight;
  203. pShaderAPI->GetCurrentViewport( nViewportX, nViewportY, nViewportWidth, nViewportHeight );
  204. int nRtWidth, nRtHeight;
  205. pShaderAPI->GetCurrentRenderTargetDimensions( nRtWidth, nRtHeight );
  206. float vViewportMad[4];
  207. vViewportMad[0] = ( float )nViewportWidth / ( float )nRtWidth;
  208. vViewportMad[1] = ( float )nViewportHeight / ( float )nRtHeight;
  209. vViewportMad[2] = ( float )nViewportX / ( float )nRtWidth;
  210. vViewportMad[3] = ( float )nViewportY / ( float )nRtHeight;
  211. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_4, vViewportMad, 1 );
  212. }
  213. int nPortalRecursionDepth = ShaderApiFast( pShaderAPI )->GetIntRenderingParameter( INT_RENDERPARM_PORTAL_RECURSION_DEPTH );
  214. float vPackedConst4[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  215. if( g_pHardwareConfig->UseFastClipping() )
  216. {
  217. vPackedConst4[0] = ( nPortalRecursionDepth > 0 ) ? 1.0f : 0.0f;
  218. }
  219. else
  220. {
  221. vPackedConst4[0] = ( nPortalRecursionDepth > 1 )? 1.0f : 0.0f;
  222. }
  223. ShaderApiFast( pShaderAPI )->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_5, vPackedConst4, 1 );
  224. bool bNvidiaStereoActiveThisFrame = pShaderAPI->IsStereoActiveThisFrame();
  225. if ( bNvidiaStereoActiveThisFrame )
  226. {
  227. pShaderAPI->BindStandardTexture( SHADER_SAMPLER3, TEXTURE_BINDFLAGS_NONE, TEXTURE_STEREO_PARAM_MAP );
  228. }
  229. DECLARE_DYNAMIC_VERTEX_SHADER( portal_vs20 );
  230. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  231. SET_DYNAMIC_VERTEX_SHADER_COMBO( ADDSTATIC, bHasStatic );
  232. SET_DYNAMIC_VERTEX_SHADER( portal_vs20 );
  233. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  234. {
  235. DECLARE_DYNAMIC_PIXEL_SHADER( portal_ps20b );
  236. SET_DYNAMIC_PIXEL_SHADER_COMBO( ADDSTATIC, bHasStatic );
  237. SET_DYNAMIC_PIXEL_SHADER_COMBO( D_NVIDIA_STEREO, bNvidiaStereoActiveThisFrame );
  238. SET_DYNAMIC_PIXEL_SHADER( portal_ps20b );
  239. }
  240. else
  241. {
  242. DECLARE_DYNAMIC_PIXEL_SHADER( portal_ps20 );
  243. SET_DYNAMIC_PIXEL_SHADER_COMBO( ADDSTATIC, bHasStatic );
  244. SET_DYNAMIC_PIXEL_SHADER( portal_ps20 );
  245. }
  246. }
  247. Draw();
  248. }
  249. END_SHADER