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.

213 lines
5.7 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_texture_ps20.inc"
  10. #include "vr_distort_texture_ps20b.inc"
  11. #include "vr_distort_texture_vs20.inc"
  12. #include "vr_distort_texture_ps30.inc"
  13. #include "vr_distort_texture_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. static const float kAllZeros[ 4 ] = { 0.0f, 0.0f, 0.0f, 0.0f };
  40. BEGIN_VS_SHADER( vr_distort_texture, "Help for warp" )
  41. BEGIN_SHADER_PARAMS
  42. SHADER_PARAM( BASETEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "" )
  43. SHADER_PARAM( DISTORTMAP, SHADER_PARAM_TYPE_TEXTURE, "vr_distort_map", "" )
  44. SHADER_PARAM( USERENDERTARGET, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  45. END_SHADER_PARAMS
  46. SHADER_INIT_PARAMS()
  47. {
  48. }
  49. SHADER_FALLBACK
  50. {
  51. return 0;
  52. }
  53. SHADER_INIT
  54. {
  55. LoadTexture( BASETEXTURE, TEXTUREFLAGS_SRGB );
  56. LoadTexture( DISTORTMAP, TEXTUREFLAGS_NOMIP | TEXTUREFLAGS_NOLOD | TEXTUREFLAGS_NODEBUGOVERRIDE |
  57. TEXTUREFLAGS_SINGLECOPY | TEXTUREFLAGS_CLAMPS | TEXTUREFLAGS_CLAMPT );
  58. }
  59. SHADER_DRAW
  60. {
  61. CVRDistortTexture_DX9_Context *pContextData = reinterpret_cast< CVRDistortTexture_DX9_Context *> ( *pContextDataPtr );
  62. bool bNeedRegenStaticCmds = ( !pContextData ) || pShaderShadow;
  63. if ( !pContextData ) // make sure allocated
  64. {
  65. pContextData = new CVRDistortTexture_DX9_Context;
  66. *pContextDataPtr = pContextData;
  67. }
  68. if ( pShaderShadow || bNeedRegenStaticCmds )
  69. {
  70. pContextData->ResetStaticCmds();
  71. CCommandBufferBuilder< CFixedCommandStorageBuffer< 5000 > > staticCmdsBuf;
  72. staticCmdsBuf.BindTexture( this, SHADER_SAMPLER0, BASETEXTURE, -1 );
  73. staticCmdsBuf.BindTexture( this, SHADER_SAMPLER1, DISTORTMAP, -1 );
  74. staticCmdsBuf.End();
  75. // now, copy buf
  76. pContextData->m_pStaticCmds = new uint8[ staticCmdsBuf.Size() ];
  77. memcpy( pContextData->m_pStaticCmds, staticCmdsBuf.Base(), staticCmdsBuf.Size() );
  78. }
  79. if ( pShaderAPI && pContextData->m_bMaterialVarsChanged )
  80. {
  81. // need to regenerate the semistatic cmds
  82. pContextData->m_SemiStaticCmdsOut.Reset();
  83. pContextData->m_bMaterialVarsChanged = false;
  84. pContextData->m_SemiStaticCmdsOut.SetAmbientCubeDynamicStateVertexShader();
  85. pContextData->m_SemiStaticCmdsOut.End();
  86. }
  87. SHADOW_STATE
  88. {
  89. SetInitialShadowState( );
  90. pShaderShadow->EnableDepthWrites( false );
  91. pShaderShadow->EnableDepthTest( false );
  92. pShaderShadow->EnableBlending( 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->EnableAlphaTest( false );
  99. DefaultFog();
  100. int nFormat = 0;
  101. nFormat |= VERTEX_POSITION;
  102. pShaderShadow->VertexShaderVertexFormat( nFormat, 2, 0, 0 );
  103. if ( !g_pHardwareConfig->SupportsShaderModel_3_0() )
  104. {
  105. DECLARE_STATIC_VERTEX_SHADER( vr_distort_texture_vs20 );
  106. SET_STATIC_VERTEX_SHADER( vr_distort_texture_vs20 );
  107. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  108. {
  109. DECLARE_STATIC_PIXEL_SHADER( vr_distort_texture_ps20b );
  110. SET_STATIC_PIXEL_SHADER( vr_distort_texture_ps20b );
  111. }
  112. else
  113. {
  114. DECLARE_STATIC_PIXEL_SHADER( vr_distort_texture_ps20 );
  115. SET_STATIC_PIXEL_SHADER( vr_distort_texture_ps20 );
  116. }
  117. }
  118. else
  119. {
  120. DECLARE_STATIC_VERTEX_SHADER( vr_distort_texture_vs30 );
  121. SET_STATIC_VERTEX_SHADER( vr_distort_texture_vs30 );
  122. DECLARE_STATIC_PIXEL_SHADER( vr_distort_texture_ps30 );
  123. SET_STATIC_PIXEL_SHADER( vr_distort_texture_ps30 );
  124. }
  125. }
  126. DYNAMIC_STATE
  127. {
  128. CCommandBufferBuilder< CFixedCommandStorageBuffer< 1000 > > DynamicCmdsOut;
  129. DynamicCmdsOut.Call( pContextData->m_pStaticCmds );
  130. DynamicCmdsOut.Call( pContextData->m_SemiStaticCmdsOut.Base() );
  131. pShaderAPI->SetDefaultState();
  132. int useRenderTarget = ( params[ USERENDERTARGET ]->GetIntValue() == 0 ) ? 0 : 1;
  133. if ( !g_pHardwareConfig->SupportsShaderModel_3_0() )
  134. {
  135. DECLARE_DYNAMIC_VERTEX_SHADER( vr_distort_texture_vs20 );
  136. SET_DYNAMIC_VERTEX_SHADER( vr_distort_texture_vs20 );
  137. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  138. {
  139. DECLARE_DYNAMIC_PIXEL_SHADER( vr_distort_texture_ps20b );
  140. SET_DYNAMIC_PIXEL_SHADER_COMBO( CMBO_USERENDERTARGET, useRenderTarget );
  141. SET_DYNAMIC_PIXEL_SHADER( vr_distort_texture_ps20b );
  142. }
  143. else
  144. {
  145. DECLARE_DYNAMIC_PIXEL_SHADER( vr_distort_texture_ps20 );
  146. SET_DYNAMIC_PIXEL_SHADER_COMBO( CMBO_USERENDERTARGET, useRenderTarget );
  147. SET_DYNAMIC_PIXEL_SHADER( vr_distort_texture_ps20 );
  148. }
  149. }
  150. else
  151. {
  152. DECLARE_DYNAMIC_VERTEX_SHADER( vr_distort_texture_vs30 );
  153. SET_DYNAMIC_VERTEX_SHADER( vr_distort_texture_vs30 );
  154. DECLARE_DYNAMIC_PIXEL_SHADER( vr_distort_texture_ps30 );
  155. SET_DYNAMIC_PIXEL_SHADER_COMBO( CMBO_USERENDERTARGET, useRenderTarget );
  156. SET_DYNAMIC_PIXEL_SHADER( vr_distort_texture_ps30 );
  157. }
  158. DynamicCmdsOut.End();
  159. pShaderAPI->ExecuteCommandBuffer( DynamicCmdsOut.Base() );
  160. }
  161. Draw();
  162. }
  163. END_SHADER