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.

224 lines
6.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================
  7. #include "BaseVSShader.h"
  8. #include "commandbuilder.h"
  9. #include "vr_distort_hud_ps20.inc"
  10. #include "vr_distort_hud_ps20b.inc"
  11. #include "vr_distort_hud_vs20.inc"
  12. #include "vr_distort_hud_ps30.inc"
  13. #include "vr_distort_hud_vs30.inc"
  14. #include "../materialsystem_global.h"
  15. // memdbgon must be the last include file in a .cpp file!!!
  16. #include "tier0/memdbgon.h"
  17. class CVRDistortTexture_DX9_Context : public CBasePerMaterialContextData
  18. {
  19. public:
  20. uint8 *m_pStaticCmds;
  21. CCommandBufferBuilder< CFixedCommandStorageBuffer< 1000 > > m_SemiStaticCmdsOut;
  22. void ResetStaticCmds( void )
  23. {
  24. if ( m_pStaticCmds )
  25. {
  26. delete[] m_pStaticCmds;
  27. m_pStaticCmds = NULL;
  28. }
  29. }
  30. CVRDistortTexture_DX9_Context( void )
  31. {
  32. m_pStaticCmds = NULL;
  33. }
  34. ~CVRDistortTexture_DX9_Context( void )
  35. {
  36. ResetStaticCmds();
  37. }
  38. };
  39. BEGIN_VS_SHADER( vr_distort_hud, "Help for hud warp" )
  40. BEGIN_SHADER_PARAMS
  41. SHADER_PARAM( BASETEXTURE, SHADER_PARAM_TYPE_TEXTURE, "_rt_gui", "" )
  42. SHADER_PARAM( DISTORTMAP, SHADER_PARAM_TYPE_TEXTURE, "vr_distort_map_left", "" )
  43. SHADER_PARAM( DISTORTBOUNDS, SHADER_PARAM_TYPE_VEC4, "[ 0 0 1 1 ]", "" )
  44. SHADER_PARAM( HUDTRANSLUCENT, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  45. SHADER_PARAM( HUDUNDISTORT, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  46. END_SHADER_PARAMS
  47. SHADER_INIT_PARAMS()
  48. {
  49. }
  50. SHADER_FALLBACK
  51. {
  52. return 0;
  53. }
  54. SHADER_INIT
  55. {
  56. LoadTexture( BASETEXTURE, TEXTUREFLAGS_SRGB );
  57. LoadTexture( DISTORTMAP, TEXTUREFLAGS_NOMIP | TEXTUREFLAGS_NOLOD | TEXTUREFLAGS_NODEBUGOVERRIDE | TEXTUREFLAGS_SINGLECOPY |
  58. TEXTUREFLAGS_CLAMPS | TEXTUREFLAGS_CLAMPT );
  59. }
  60. SHADER_DRAW
  61. {
  62. CVRDistortTexture_DX9_Context *pContextData = reinterpret_cast< CVRDistortTexture_DX9_Context *> ( *pContextDataPtr );
  63. bool bNeedRegenStaticCmds = ( !pContextData ) || pShaderShadow;
  64. if ( !pContextData ) // make sure allocated
  65. {
  66. pContextData = new CVRDistortTexture_DX9_Context;
  67. *pContextDataPtr = pContextData;
  68. }
  69. if ( pShaderShadow || bNeedRegenStaticCmds )
  70. {
  71. pContextData->ResetStaticCmds();
  72. CCommandBufferBuilder< CFixedCommandStorageBuffer< 5000 > > staticCmdsBuf;
  73. staticCmdsBuf.BindTexture( this, SHADER_SAMPLER0, BASETEXTURE, -1 );
  74. staticCmdsBuf.BindTexture( this, SHADER_SAMPLER1, DISTORTMAP, -1 );
  75. staticCmdsBuf.End();
  76. // now, copy buf
  77. pContextData->m_pStaticCmds = new uint8[ staticCmdsBuf.Size() ];
  78. memcpy( pContextData->m_pStaticCmds, staticCmdsBuf.Base(), staticCmdsBuf.Size() );
  79. }
  80. if ( pShaderAPI && pContextData->m_bMaterialVarsChanged )
  81. {
  82. // need to regenerate the semistatic cmds
  83. pContextData->m_SemiStaticCmdsOut.Reset();
  84. pContextData->m_bMaterialVarsChanged = false;
  85. pContextData->m_SemiStaticCmdsOut.SetAmbientCubeDynamicStateVertexShader();
  86. pContextData->m_SemiStaticCmdsOut.End();
  87. }
  88. SHADOW_STATE
  89. {
  90. SetInitialShadowState( );
  91. pShaderShadow->EnableDepthWrites( false );
  92. pShaderShadow->EnableDepthTest( false );
  93. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  94. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  95. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  96. pShaderShadow->EnableSRGBWrite( true );
  97. pShaderShadow->EnableAlphaWrites( false );
  98. pShaderShadow->AlphaFunc( SHADER_ALPHAFUNC_GREATER, 0.0f );
  99. if ( IS_FLAG_SET( MATERIAL_VAR_TRANSLUCENT ) )
  100. {
  101. pShaderShadow->EnableAlphaTest( true );
  102. pShaderShadow->EnableBlending( true );
  103. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  104. }
  105. else
  106. {
  107. pShaderShadow->EnableAlphaTest( false );
  108. pShaderShadow->EnableBlending( false );
  109. pShaderShadow->BlendFunc( SHADER_BLEND_ONE, SHADER_BLEND_ZERO );
  110. }
  111. DefaultFog();
  112. int nFormat = 0;
  113. nFormat |= VERTEX_POSITION;
  114. pShaderShadow->VertexShaderVertexFormat( nFormat, 2, 0, 0 );
  115. if ( !g_pHardwareConfig->SupportsShaderModel_3_0() )
  116. {
  117. DECLARE_STATIC_VERTEX_SHADER( vr_distort_hud_vs20 );
  118. SET_STATIC_VERTEX_SHADER( vr_distort_hud_vs20 );
  119. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  120. {
  121. DECLARE_STATIC_PIXEL_SHADER( vr_distort_hud_ps20b );
  122. SET_STATIC_PIXEL_SHADER( vr_distort_hud_ps20b );
  123. }
  124. else
  125. {
  126. DECLARE_STATIC_PIXEL_SHADER( vr_distort_hud_ps20 );
  127. SET_STATIC_PIXEL_SHADER( vr_distort_hud_ps20 );
  128. }
  129. }
  130. else
  131. {
  132. DECLARE_STATIC_VERTEX_SHADER( vr_distort_hud_vs30 );
  133. SET_STATIC_VERTEX_SHADER( vr_distort_hud_vs30 );
  134. DECLARE_STATIC_PIXEL_SHADER( vr_distort_hud_ps30 );
  135. SET_STATIC_PIXEL_SHADER( vr_distort_hud_ps30 );
  136. }
  137. }
  138. DYNAMIC_STATE
  139. {
  140. CCommandBufferBuilder< CFixedCommandStorageBuffer< 1000 > > DynamicCmdsOut;
  141. DynamicCmdsOut.Call( pContextData->m_pStaticCmds );
  142. DynamicCmdsOut.Call( pContextData->m_SemiStaticCmdsOut.Base() );
  143. pShaderAPI->SetDefaultState();
  144. SetPixelShaderConstant( 0, DISTORTBOUNDS );
  145. SetPixelShaderConstant( 1, HUDTRANSLUCENT );
  146. int hudUndistortEnabled = ( params[ HUDUNDISTORT ]->GetIntValue() == 0 ) ? 0 : 1;
  147. if ( !g_pHardwareConfig->SupportsShaderModel_3_0() )
  148. {
  149. DECLARE_DYNAMIC_VERTEX_SHADER( vr_distort_hud_vs20 );
  150. SET_DYNAMIC_VERTEX_SHADER( vr_distort_hud_vs20 );
  151. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  152. {
  153. DECLARE_DYNAMIC_PIXEL_SHADER( vr_distort_hud_ps20b );
  154. SET_DYNAMIC_PIXEL_SHADER_COMBO( CMBO_HUDUNDISTORT, hudUndistortEnabled );
  155. SET_DYNAMIC_PIXEL_SHADER( vr_distort_hud_ps20b );
  156. }
  157. else
  158. {
  159. DECLARE_DYNAMIC_PIXEL_SHADER( vr_distort_hud_ps20 );
  160. SET_DYNAMIC_PIXEL_SHADER_COMBO( CMBO_HUDUNDISTORT, hudUndistortEnabled );
  161. SET_DYNAMIC_PIXEL_SHADER( vr_distort_hud_ps20 );
  162. }
  163. }
  164. else
  165. {
  166. DECLARE_DYNAMIC_VERTEX_SHADER( vr_distort_hud_vs30 );
  167. SET_DYNAMIC_VERTEX_SHADER( vr_distort_hud_vs30 );
  168. DECLARE_DYNAMIC_PIXEL_SHADER( vr_distort_hud_ps30 );
  169. SET_DYNAMIC_PIXEL_SHADER_COMBO( CMBO_HUDUNDISTORT, hudUndistortEnabled );
  170. SET_DYNAMIC_PIXEL_SHADER( vr_distort_hud_ps30 );
  171. }
  172. DynamicCmdsOut.End();
  173. pShaderAPI->ExecuteCommandBuffer( DynamicCmdsOut.Base() );
  174. }
  175. Draw();
  176. }
  177. END_SHADER