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.

190 lines
7.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. #include "BaseVSShader.h"
  3. #include "mathlib/vmatrix.h"
  4. #include "portal_refract_helper.h"
  5. #include "convar.h"
  6. // Auto generated inc files
  7. #include "portal_refract_vs20.inc"
  8. #include "portal_refract_ps20.inc"
  9. #include "portal_refract_ps20b.inc"
  10. void InitParamsPortalRefract( CBaseVSShader *pShader, IMaterialVar** params, const char *pMaterialName, PortalRefractVars_t &info )
  11. {
  12. // Set material flags
  13. SET_FLAGS( MATERIAL_VAR_MODEL );
  14. SET_FLAGS2( MATERIAL_VAR2_NEEDS_TANGENT_SPACES );
  15. SET_FLAGS( MATERIAL_VAR_TRANSLUCENT );
  16. SET_FLAGS2( MATERIAL_VAR2_NEEDS_POWER_OF_TWO_FRAME_BUFFER_TEXTURE );
  17. // Set material parameter default values
  18. if ( ( info.m_nStage != -1 ) && ( !params[info.m_nStage]->IsDefined() ) )
  19. {
  20. params[info.m_nStage]->SetIntValue( 0 );
  21. }
  22. if ( ( info.m_nPortalOpenAmount != -1 ) && ( !params[info.m_nPortalOpenAmount]->IsDefined() ) )
  23. {
  24. params[info.m_nPortalOpenAmount]->SetFloatValue( kDefaultPortalOpenAmount );
  25. }
  26. if ( ( info.m_nPortalStatic != -1 ) && ( !params[info.m_nPortalStatic]->IsDefined() ) )
  27. {
  28. params[info.m_nPortalStatic]->SetFloatValue( kDefaultPortalStatic );
  29. }
  30. SET_PARAM_FLOAT_IF_NOT_DEFINED( info.m_nTime, 0.0f );
  31. }
  32. void InitPortalRefract( CBaseVSShader *pShader, IMaterialVar** params, PortalRefractVars_t &info )
  33. {
  34. int nStage = IS_PARAM_DEFINED( info.m_nStage ) ? params[info.m_nStage]->GetIntValue() : 0;
  35. // Load textures
  36. if ( nStage == 2 ) // Only load textures for third stage
  37. {
  38. if ( (info.m_nPortalMaskTexture != -1) && params[info.m_nPortalMaskTexture]->IsDefined() )
  39. {
  40. pShader->LoadTexture( info.m_nPortalMaskTexture );
  41. }
  42. if ( (info.m_nPortalColorTexture != -1) && params[info.m_nPortalColorTexture]->IsDefined() )
  43. {
  44. pShader->LoadTexture( info.m_nPortalColorTexture, TEXTUREFLAGS_SRGB );
  45. }
  46. }
  47. }
  48. void DrawPortalRefract( CBaseVSShader *pShader, IMaterialVar** params,
  49. IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow, PortalRefractVars_t &info )
  50. {
  51. int nStage = IS_PARAM_DEFINED( info.m_nStage ) ? params[info.m_nStage]->GetIntValue() : 0;
  52. SHADOW_STATE
  53. {
  54. // Set stream format
  55. pShaderShadow->VertexShaderVertexFormat( VERTEX_POSITION | VERTEX_NORMAL, 1, NULL, 4 );
  56. // Vertex Shader
  57. DECLARE_STATIC_VERTEX_SHADER( portal_refract_vs20 );
  58. SET_STATIC_VERTEX_SHADER_COMBO( STAGE, nStage );
  59. SET_STATIC_VERTEX_SHADER( portal_refract_vs20 );
  60. // On Leopard / 10.5.8, we can't do an sRGB read from a render target, so we must fake it in shader code
  61. bool bShaderSRGBRead = IsOSX() && !g_pHardwareConfig->CanDoSRGBReadFromRTs();
  62. // Pixel Shader
  63. if( g_pHardwareConfig->SupportsPixelShaders_2_b() || g_pHardwareConfig->ShouldAlwaysUseShaderModel2bShaders() ) // Always send OpenGL / Posix down this path
  64. {
  65. DECLARE_STATIC_PIXEL_SHADER( portal_refract_ps20b );
  66. SET_STATIC_PIXEL_SHADER_COMBO( STAGE, nStage );
  67. SET_STATIC_PIXEL_SHADER_COMBO( SHADER_SRGB_READ, bShaderSRGBRead ? 1 : 0 );
  68. SET_STATIC_PIXEL_SHADER( portal_refract_ps20b );
  69. }
  70. else
  71. {
  72. DECLARE_STATIC_PIXEL_SHADER( portal_refract_ps20 );
  73. SET_STATIC_PIXEL_SHADER_COMBO( STAGE, nStage );
  74. SET_STATIC_PIXEL_SHADER( portal_refract_ps20 );
  75. }
  76. // Textures
  77. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true ); // Refraction texture
  78. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  79. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true ); // Noise
  80. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, false ); // Not sRGB
  81. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true ); // Color
  82. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER2, true ); // sRGB
  83. pShaderShadow->EnableSRGBWrite( true ); //Stage 1 doesn't actually need srgb conversion, but it skips the software srgb solution anyway.
  84. // Enable alpha testing for all stages
  85. pShaderShadow->EnableAlphaTest( true );
  86. pShaderShadow->AlphaFunc( SHADER_ALPHAFUNC_GREATER, 0.5f );
  87. // Enable alpha blending for stage 2
  88. if ( nStage == 2 )
  89. {
  90. pShader->EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  91. pShaderShadow->AlphaFunc( SHADER_ALPHAFUNC_GREATER, 1.0f/255.0f );
  92. }
  93. // Disable z-writes for all passes
  94. pShaderShadow->EnableDepthWrites( false );
  95. // Disable alpha-writes for all passes
  96. pShaderShadow->EnableAlphaWrites( false );
  97. }
  98. DYNAMIC_STATE
  99. {
  100. // Set Vertex Shader Combos
  101. DECLARE_DYNAMIC_VERTEX_SHADER( portal_refract_vs20 );
  102. SET_DYNAMIC_VERTEX_SHADER( portal_refract_vs20 );
  103. // Set Vertex Shader Constants
  104. if ( IS_PARAM_DEFINED( info.m_nTextureTransform ) )
  105. {
  106. pShader->SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_1, info.m_nTextureTransform );
  107. }
  108. // Time % 1000
  109. float vPackedVsConst1[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  110. float flTime = IS_PARAM_DEFINED( info.m_nTime ) && params[info.m_nTime]->GetFloatValue() > 0.0f ? params[info.m_nTime]->GetFloatValue() : pShaderAPI->CurrentTime();
  111. vPackedVsConst1[0] = flTime;
  112. vPackedVsConst1[0] -= (float)( floor( vPackedVsConst1[0] / 1000.0f ) ) * 1000.0f;
  113. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, vPackedVsConst1, 1 );
  114. // Set Pixel Shader Combos
  115. if( g_pHardwareConfig->SupportsPixelShaders_2_b() || g_pHardwareConfig->ShouldAlwaysUseShaderModel2bShaders() ) // Always send OpenGL / Posix down this path
  116. {
  117. DECLARE_DYNAMIC_PIXEL_SHADER( portal_refract_ps20b );
  118. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  119. SET_DYNAMIC_PIXEL_SHADER( portal_refract_ps20b );
  120. }
  121. else
  122. {
  123. DECLARE_DYNAMIC_PIXEL_SHADER( portal_refract_ps20 );
  124. SET_DYNAMIC_PIXEL_SHADER( portal_refract_ps20 );
  125. }
  126. // Bind textures
  127. if ( nStage == 0 ) // Only bind frame buffer texture for first stage
  128. {
  129. pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, TEXTURE_FRAME_BUFFER_FULL_TEXTURE_0 ); // Refraction Map
  130. }
  131. if ( nStage == 2 ) // Only load textures for third stage
  132. {
  133. pShader->BindTexture( SHADER_SAMPLER1, info.m_nPortalMaskTexture );
  134. pShader->BindTexture( SHADER_SAMPLER2, info.m_nPortalColorTexture );
  135. }
  136. // Set Pixel Shader Constants
  137. pShaderAPI->SetPixelShaderFogParams( 6 );
  138. // Set c0-c3 to contain four rows of ViewProj matrix
  139. VMatrix mView, mProj;
  140. pShaderAPI->GetMatrix( MATERIAL_VIEW, mView.m[0] );
  141. pShaderAPI->GetMatrix( MATERIAL_PROJECTION, mProj.m[0] );
  142. VMatrix mViewProj = mView * mProj;
  143. mViewProj = mViewProj.Transpose();
  144. pShaderAPI->SetPixelShaderConstant( 0, mViewProj.m[0], 4 );
  145. // Portal open amount
  146. float vPackedConst4[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  147. vPackedConst4[0] = ( IS_PARAM_DEFINED( info.m_nPortalOpenAmount ) ? params[info.m_nPortalOpenAmount]->GetFloatValue() : kDefaultPortalOpenAmount );
  148. vPackedConst4[1] = 1.0f - ( IS_PARAM_DEFINED( info.m_nPortalStatic ) ? params[info.m_nPortalStatic]->GetFloatValue() : kDefaultPortalStatic );
  149. vPackedConst4[2] = ( IS_PARAM_DEFINED( info.m_nPortalColorScale ) ? params[info.m_nPortalColorScale]->GetFloatValue() : kDefaultPortalColorScale );
  150. //vPackedConst4[0] = 0.6f;
  151. //DevMsg( "Refract Time: %f\n", vPackedConst4[0] );
  152. pShaderAPI->SetPixelShaderConstant( 4, vPackedConst4, 1 );
  153. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_3, vPackedConst4, 1 );
  154. // Camera position
  155. float vEyePos[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  156. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos );
  157. pShaderAPI->SetPixelShaderConstant( 5, vEyePos, 1 );
  158. }
  159. pShader->Draw();
  160. }