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.

251 lines
6.6 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 "warp_ps20.inc"
  10. #include "warp_ps20b.inc"
  11. #include "warp_vs20.inc"
  12. #include "warp_ps30.inc"
  13. #include "warp_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 CWarp_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. CWarp_DX9_Context( void )
  31. {
  32. m_pStaticCmds = NULL;
  33. }
  34. ~CWarp_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( warp, "Help for warp" )
  41. BEGIN_SHADER_PARAMS
  42. SHADER_PARAM( BASETEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "" )
  43. END_SHADER_PARAMS
  44. SHADER_INIT_PARAMS()
  45. {
  46. }
  47. SHADER_FALLBACK
  48. {
  49. return 0;
  50. }
  51. SHADER_INIT
  52. {
  53. LoadTexture( BASETEXTURE, TEXTUREFLAGS_SRGB );
  54. }
  55. SHADER_DRAW
  56. {
  57. CWarp_DX9_Context *pContextData = reinterpret_cast< CWarp_DX9_Context *> ( *pContextDataPtr );
  58. bool bNeedRegenStaticCmds = ( !pContextData ) || pShaderShadow;
  59. if ( !pContextData ) // make sure allocated
  60. {
  61. pContextData = new CWarp_DX9_Context;
  62. *pContextDataPtr = pContextData;
  63. }
  64. if ( pShaderShadow || bNeedRegenStaticCmds )
  65. {
  66. pContextData->ResetStaticCmds();
  67. CCommandBufferBuilder< CFixedCommandStorageBuffer< 5000 > > staticCmdsBuf;
  68. staticCmdsBuf.BindTexture( this, SHADER_SAMPLER0, BASETEXTURE, -1 );
  69. staticCmdsBuf.End();
  70. // now, copy buf
  71. pContextData->m_pStaticCmds = new uint8[ staticCmdsBuf.Size() ];
  72. memcpy( pContextData->m_pStaticCmds, staticCmdsBuf.Base(), staticCmdsBuf.Size() );
  73. }
  74. if ( pShaderAPI && pContextData->m_bMaterialVarsChanged )
  75. {
  76. // need to regenerate the semistatic cmds
  77. pContextData->m_SemiStaticCmdsOut.Reset();
  78. pContextData->m_bMaterialVarsChanged = false;
  79. pContextData->m_SemiStaticCmdsOut.SetAmbientCubeDynamicStateVertexShader();
  80. pContextData->m_SemiStaticCmdsOut.End();
  81. }
  82. SHADOW_STATE
  83. {
  84. SetInitialShadowState( );
  85. pShaderShadow->EnableDepthWrites( false );
  86. pShaderShadow->EnableDepthTest( false );
  87. pShaderShadow->EnableBlending( false );
  88. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  89. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  90. pShaderShadow->EnableSRGBWrite( true );
  91. pShaderShadow->EnableAlphaWrites( false );
  92. pShaderShadow->EnableAlphaTest( false );
  93. DefaultFog();
  94. int nFormat = 0;
  95. nFormat |= VERTEX_POSITION;
  96. pShaderShadow->VertexShaderVertexFormat( nFormat, 2, 0, 0 );
  97. if ( !g_pHardwareConfig->SupportsShaderModel_3_0() )
  98. {
  99. DECLARE_STATIC_VERTEX_SHADER( warp_vs20 );
  100. SET_STATIC_VERTEX_SHADER( warp_vs20 );
  101. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  102. {
  103. DECLARE_STATIC_PIXEL_SHADER( warp_ps20b );
  104. SET_STATIC_PIXEL_SHADER( warp_ps20b );
  105. }
  106. else
  107. {
  108. DECLARE_STATIC_PIXEL_SHADER( warp_ps20 );
  109. SET_STATIC_PIXEL_SHADER( warp_ps20 );
  110. }
  111. }
  112. else
  113. {
  114. DECLARE_STATIC_VERTEX_SHADER( warp_vs30 );
  115. SET_STATIC_VERTEX_SHADER( warp_vs30 );
  116. DECLARE_STATIC_PIXEL_SHADER( warp_ps30 );
  117. SET_STATIC_PIXEL_SHADER( warp_ps30 );
  118. }
  119. }
  120. DYNAMIC_STATE
  121. {
  122. CCommandBufferBuilder< CFixedCommandStorageBuffer< 1000 > > DynamicCmdsOut;
  123. DynamicCmdsOut.Call( pContextData->m_pStaticCmds );
  124. DynamicCmdsOut.Call( pContextData->m_SemiStaticCmdsOut.Base() );
  125. pShaderAPI->SetDefaultState();
  126. Vector4D vParms;
  127. Vector vTemp;
  128. // GetVectorRenderingParameter only return a Vec3, not a Vec4, so I need to repack these :-(
  129. vTemp = pShaderAPI->GetVectorRenderingParameter ( VECTOR_RENDERPARM_HMDWARP_GROW_OUTIN );
  130. vParms.x = vTemp.x;
  131. vParms.y = vTemp.y;
  132. vTemp = pShaderAPI->GetVectorRenderingParameter ( VECTOR_RENDERPARM_HMDWARP_GROW_ABOVEBELOW );
  133. vParms.z = vTemp.x;
  134. vParms.w = vTemp.y;
  135. pShaderAPI->SetPixelShaderConstant( 0, vParms.Base() );
  136. vTemp = pShaderAPI->GetVectorRenderingParameter ( VECTOR_RENDERPARM_HMDWARP_LEFT_CENTRE );
  137. vParms.x = vTemp.x;
  138. vParms.y = vTemp.y;
  139. vTemp = pShaderAPI->GetVectorRenderingParameter ( VECTOR_RENDERPARM_HMDWARP_RIGHT_CENTRE );
  140. vParms.z = vTemp.x;
  141. vParms.w = vTemp.y;
  142. pShaderAPI->SetPixelShaderConstant( 1, vParms.Base() );
  143. vTemp = pShaderAPI->GetVectorRenderingParameter ( VECTOR_RENDERPARM_HMDWARP_LEFT_COEFF012 );
  144. vParms.x = vTemp.x;
  145. vParms.y = vTemp.y;
  146. vParms.z = vTemp.z;
  147. vTemp = pShaderAPI->GetVectorRenderingParameter ( VECTOR_RENDERPARM_HMDWARP_LEFT_COEFF34_RED_OFFSET );
  148. vParms.w = vTemp.z; // Red offset
  149. // Shader doesn't support a 4th & 5th coefficient (yet?).
  150. pShaderAPI->SetPixelShaderConstant( 2, vParms.Base() );
  151. vTemp = pShaderAPI->GetVectorRenderingParameter ( VECTOR_RENDERPARM_HMDWARP_RIGHT_COEFF012 );
  152. vParms.x = vTemp.x;
  153. vParms.y = vTemp.y;
  154. vParms.z = vTemp.z;
  155. vTemp = pShaderAPI->GetVectorRenderingParameter ( VECTOR_RENDERPARM_HMDWARP_RIGHT_COEFF34_BLUE_OFFSET );
  156. vParms.w = vTemp.z; // Blue offset
  157. // Shader doesn't support a 4th & 5th coefficient (yet?).
  158. pShaderAPI->SetPixelShaderConstant( 3, vParms.Base() );
  159. vTemp = pShaderAPI->GetVectorRenderingParameter ( VECTOR_RENDERPARM_HMDWARP_ASPECT );
  160. vParms.x = vTemp.x;
  161. vParms.y = vTemp.y;
  162. vParms.z = vTemp.z;
  163. vParms.w = 0.0f;
  164. pShaderAPI->SetPixelShaderConstant( 4, vParms.Base() );
  165. int nDistortType = pShaderAPI->GetIntRenderingParameter( INT_RENDERPARM_DISTORTION_TYPE );
  166. if ( !g_pHardwareConfig->SupportsShaderModel_3_0() )
  167. {
  168. DECLARE_DYNAMIC_VERTEX_SHADER( warp_vs20 );
  169. SET_DYNAMIC_VERTEX_SHADER( warp_vs20 );
  170. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  171. {
  172. DECLARE_DYNAMIC_PIXEL_SHADER( warp_ps20b );
  173. SET_DYNAMIC_PIXEL_SHADER_COMBO( DISTORT_TYPE, nDistortType );
  174. SET_DYNAMIC_PIXEL_SHADER( warp_ps20b );
  175. }
  176. else
  177. {
  178. DECLARE_DYNAMIC_PIXEL_SHADER( warp_ps20 );
  179. SET_DYNAMIC_PIXEL_SHADER_COMBO( DISTORT_TYPE, nDistortType );
  180. SET_DYNAMIC_PIXEL_SHADER( warp_ps20 );
  181. }
  182. }
  183. else
  184. {
  185. DECLARE_DYNAMIC_VERTEX_SHADER( warp_vs30 );
  186. SET_DYNAMIC_VERTEX_SHADER( warp_vs30 );
  187. DECLARE_DYNAMIC_PIXEL_SHADER( warp_ps30 );
  188. SET_DYNAMIC_PIXEL_SHADER_COMBO( DISTORT_TYPE, nDistortType );
  189. SET_DYNAMIC_PIXEL_SHADER( warp_ps30 );
  190. }
  191. DynamicCmdsOut.End();
  192. pShaderAPI->ExecuteCommandBuffer( DynamicCmdsOut.Base() );
  193. }
  194. Draw();
  195. }
  196. END_SHADER