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.

196 lines
7.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. #include "BaseVSShader.h"
  3. #include "mathlib/vmatrix.h"
  4. #include "aftershock_helper.h"
  5. #include "convar.h"
  6. // Auto generated inc files
  7. #include "aftershock_vs20.inc"
  8. #include "aftershock_ps20.inc"
  9. #include "aftershock_ps20b.inc"
  10. void InitParamsAftershock( CBaseVSShader *pShader, IMaterialVar** params, const char *pMaterialName, AftershockVars_t &info )
  11. {
  12. // Set material flags
  13. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  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_nRefractAmount != -1 ) && ( !params[info.m_nRefractAmount]->IsDefined() ) )
  19. {
  20. params[info.m_nRefractAmount]->SetFloatValue( kDefaultRefractAmount );
  21. }
  22. if ( ( info.m_nColorTint != -1 ) && ( !params[info.m_nColorTint]->IsDefined() ) )
  23. {
  24. params[info.m_nColorTint]->SetVecValue( kDefaultColorTint[0], kDefaultColorTint[1], kDefaultColorTint[2], kDefaultColorTint[3] );
  25. }
  26. if( (info.m_nBumpFrame != -1 ) && !params[info.m_nBumpFrame]->IsDefined() )
  27. {
  28. params[info.m_nBumpFrame]->SetIntValue( 0 );
  29. }
  30. if ( ( info.m_nSilhouetteThickness != -1 ) && ( !params[info.m_nSilhouetteThickness]->IsDefined() ) )
  31. {
  32. params[info.m_nSilhouetteThickness]->SetFloatValue( kDefaultSilhouetteThickness );
  33. }
  34. if ( ( info.m_nSilhouetteColor != -1 ) && ( !params[info.m_nSilhouetteColor]->IsDefined() ) )
  35. {
  36. params[info.m_nSilhouetteColor]->SetVecValue( kDefaultSilhouetteColor[0], kDefaultSilhouetteColor[1], kDefaultSilhouetteColor[2], kDefaultSilhouetteColor[3] );
  37. }
  38. if ( ( info.m_nGroundMin != -1 ) && ( !params[info.m_nGroundMin]->IsDefined() ) )
  39. {
  40. params[info.m_nGroundMin]->SetFloatValue( kDefaultGroundMin );
  41. }
  42. if ( ( info.m_nGroundMax != -1 ) && ( !params[info.m_nGroundMax]->IsDefined() ) )
  43. {
  44. params[info.m_nGroundMax]->SetFloatValue( kDefaultGroundMax );
  45. }
  46. if ( ( info.m_nBlurAmount != -1 ) && ( !params[info.m_nBlurAmount]->IsDefined() ) )
  47. {
  48. params[info.m_nBlurAmount]->SetFloatValue( kDefaultBlurAmount );
  49. }
  50. SET_PARAM_FLOAT_IF_NOT_DEFINED( info.m_nTime, 0.0f );
  51. }
  52. void InitAftershock( CBaseVSShader *pShader, IMaterialVar** params, AftershockVars_t &info )
  53. {
  54. // Load textures
  55. if ( (info.m_nBumpmap != -1) && params[info.m_nBumpmap]->IsDefined() )
  56. {
  57. pShader->LoadTexture( info.m_nBumpmap );
  58. }
  59. }
  60. void DrawAftershock( CBaseVSShader *pShader, IMaterialVar** params, IShaderDynamicAPI *pShaderAPI,
  61. IShaderShadow* pShaderShadow, AftershockVars_t &info, VertexCompressionType_t vertexCompression )
  62. {
  63. bool bBumpMapping = ( info.m_nBumpmap == -1 ) || !params[info.m_nBumpmap]->IsTexture() ? 0 : 1;
  64. SHADOW_STATE
  65. {
  66. // Set stream format (note that this shader supports compression)
  67. unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL | VERTEX_FORMAT_COMPRESSED;
  68. int nTexCoordCount = 1;
  69. int userDataSize = 0;
  70. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, NULL, userDataSize );
  71. // Vertex Shader
  72. DECLARE_STATIC_VERTEX_SHADER( aftershock_vs20 );
  73. SET_STATIC_VERTEX_SHADER( aftershock_vs20 );
  74. // Pixel Shader
  75. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  76. {
  77. DECLARE_STATIC_PIXEL_SHADER( aftershock_ps20b );
  78. SET_STATIC_PIXEL_SHADER( aftershock_ps20b );
  79. }
  80. else
  81. {
  82. DECLARE_STATIC_PIXEL_SHADER( aftershock_ps20 );
  83. SET_STATIC_PIXEL_SHADER( aftershock_ps20 );
  84. }
  85. // Textures
  86. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true ); // Refraction texture
  87. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  88. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true ); // Bump
  89. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, false ); // Not sRGB
  90. pShaderShadow->EnableSRGBWrite( true );
  91. // Blending
  92. pShader->EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  93. pShaderShadow->EnableAlphaWrites( false );
  94. // !!! We need to turn this back on because EnableAlphaBlending() above disables it!
  95. //pShaderShadow->EnableDepthWrites( true );
  96. }
  97. DYNAMIC_STATE
  98. {
  99. // Set Vertex Shader Combos
  100. DECLARE_DYNAMIC_VERTEX_SHADER( aftershock_vs20 );
  101. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  102. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  103. SET_DYNAMIC_VERTEX_SHADER( aftershock_vs20 );
  104. // Set Vertex Shader Constants
  105. if ( info.m_nBumpTransform != -1 )
  106. {
  107. pShader->SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_1, info.m_nBumpTransform );
  108. }
  109. // Time % 1000
  110. float vPackedVsConst1[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  111. float flTime = IS_PARAM_DEFINED( info.m_nTime ) && params[info.m_nTime]->GetFloatValue() > 0.0f ? params[info.m_nTime]->GetFloatValue() : pShaderAPI->CurrentTime();
  112. vPackedVsConst1[0] = flTime;
  113. vPackedVsConst1[0] -= (float)( (int)( vPackedVsConst1[0] / 1000.0f ) ) * 1000.0f;
  114. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, vPackedVsConst1, 1 );
  115. // Set Pixel Shader Combos
  116. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  117. {
  118. DECLARE_DYNAMIC_PIXEL_SHADER( aftershock_ps20b );
  119. SET_DYNAMIC_PIXEL_SHADER( aftershock_ps20b );
  120. }
  121. else
  122. {
  123. DECLARE_DYNAMIC_PIXEL_SHADER( aftershock_ps20 );
  124. SET_DYNAMIC_PIXEL_SHADER( aftershock_ps20 );
  125. }
  126. // Bind textures
  127. pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, TEXTURE_FRAME_BUFFER_FULL_TEXTURE_0 ); // Refraction Map
  128. if ( bBumpMapping )
  129. {
  130. pShader->BindTexture( SHADER_SAMPLER1, info.m_nBumpmap, info.m_nBumpFrame );
  131. }
  132. // Set Pixel Shader Constants
  133. float vEyePos[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  134. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos );
  135. pShaderAPI->SetPixelShaderConstant( 5, vEyePos, 1 );
  136. float vPackedConst1[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  137. vPackedConst1[0] = IS_PARAM_DEFINED( info.m_nBlurAmount ) ? params[info.m_nBlurAmount]->GetFloatValue() : kDefaultBlurAmount;
  138. vPackedConst1[1] = IS_PARAM_DEFINED( info.m_nRefractAmount ) ? params[info.m_nRefractAmount]->GetFloatValue() : kDefaultRefractAmount;
  139. vPackedConst1[3] = vPackedVsConst1[0]; // Time
  140. pShaderAPI->SetPixelShaderConstant( 6, vPackedConst1, 1 );
  141. // Refract color tint
  142. pShaderAPI->SetPixelShaderConstant( 7, IS_PARAM_DEFINED( info.m_nColorTint ) ? params[info.m_nColorTint]->GetVecValue() : kDefaultColorTint, 1 );
  143. // Silhouette values
  144. float vPackedConst8[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  145. vPackedConst8[0] = IS_PARAM_DEFINED( info.m_nSilhouetteColor ) ? params[info.m_nSilhouetteColor]->GetVecValue()[0] : kDefaultSilhouetteColor[0];
  146. vPackedConst8[1] = IS_PARAM_DEFINED( info.m_nSilhouetteColor ) ? params[info.m_nSilhouetteColor]->GetVecValue()[1] : kDefaultSilhouetteColor[1];
  147. vPackedConst8[2] = IS_PARAM_DEFINED( info.m_nSilhouetteColor ) ? params[info.m_nSilhouetteColor]->GetVecValue()[2] : kDefaultSilhouetteColor[2];
  148. vPackedConst8[3] = IS_PARAM_DEFINED( info.m_nSilhouetteThickness ) ? params[info.m_nSilhouetteThickness]->GetFloatValue() : kDefaultSilhouetteThickness;
  149. pShaderAPI->SetPixelShaderConstant( 8, vPackedConst8, 1 );
  150. // Ground min/max
  151. float vPackedConst9[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  152. vPackedConst9[0] = IS_PARAM_DEFINED( info.m_nGroundMin ) ? params[info.m_nGroundMin]->GetFloatValue() : kDefaultGroundMin;
  153. vPackedConst9[1] = IS_PARAM_DEFINED( info.m_nGroundMax ) ? params[info.m_nGroundMax]->GetFloatValue() : kDefaultGroundMax;
  154. pShaderAPI->SetPixelShaderConstant( 9, vPackedConst9, 1 );
  155. // Set c0 and c1 to contain first two rows of ViewProj matrix
  156. VMatrix mView, mProj;
  157. pShaderAPI->GetMatrix( MATERIAL_VIEW, mView.m[0] );
  158. pShaderAPI->GetMatrix( MATERIAL_PROJECTION, mProj.m[0] );
  159. VMatrix mViewProj = mView * mProj;
  160. mViewProj = mViewProj.Transpose3x3();
  161. pShaderAPI->SetPixelShaderConstant( 0, mViewProj.m[0], 2 );
  162. }
  163. pShader->Draw();
  164. }