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.

138 lines
5.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. #include "BaseVSShader.h"
  3. #include "mathlib/vmatrix.h"
  4. #include "volume_clouds_helper.h"
  5. #include "convar.h"
  6. // Auto generated inc files
  7. #include "volume_clouds_vs20.inc"
  8. #include "volume_clouds_ps20.inc"
  9. #include "volume_clouds_ps20b.inc"
  10. void InitParamsVolumeClouds( CBaseVSShader *pShader, IMaterialVar** params, const char *pMaterialName, VolumeCloudsVars_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_PARAM_FLOAT_IF_NOT_DEFINED( info.m_nTime, 0.0f );
  17. // Set material parameter default values
  18. SET_PARAM_FLOAT_IF_NOT_DEFINED( info.m_nRefractAmount, kDefaultRefractAmount );
  19. }
  20. void InitVolumeClouds( CBaseVSShader *pShader, IMaterialVar** params, VolumeCloudsVars_t &info )
  21. {
  22. // Load textures
  23. if ( (info.m_nTexture1 != -1) && params[info.m_nTexture1]->IsDefined() )
  24. {
  25. pShader->LoadTexture( info.m_nTexture1, TEXTUREFLAGS_SRGB );
  26. }
  27. if ( (info.m_nTexture2 != -1) && params[info.m_nTexture2]->IsDefined() )
  28. {
  29. pShader->LoadTexture( info.m_nTexture2, TEXTUREFLAGS_SRGB );
  30. }
  31. if ( (info.m_nTexture3 != -1) && params[info.m_nTexture3]->IsDefined() )
  32. {
  33. pShader->LoadTexture( info.m_nTexture3, TEXTUREFLAGS_SRGB );
  34. }
  35. }
  36. void DrawVolumeClouds( CBaseVSShader *pShader, IMaterialVar** params, IShaderDynamicAPI *pShaderAPI,
  37. IShaderShadow* pShaderShadow, VolumeCloudsVars_t &info, VertexCompressionType_t vertexCompression )
  38. {
  39. SHADOW_STATE
  40. {
  41. // Set stream format (note that this shader supports compression)
  42. unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL | VERTEX_FORMAT_COMPRESSED;
  43. int nTexCoordCount = 1;
  44. int userDataSize = 0;
  45. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, NULL, userDataSize );
  46. // Vertex Shader
  47. DECLARE_STATIC_VERTEX_SHADER( volume_clouds_vs20 );
  48. SET_STATIC_VERTEX_SHADER( volume_clouds_vs20 );
  49. // Pixel Shader
  50. if( g_pHardwareConfig->SupportsPixelShaders_2_b() && !IsOpenGL() ) // Always send POSIX down the 20 path (rg - why?)
  51. {
  52. DECLARE_STATIC_PIXEL_SHADER( volume_clouds_ps20b );
  53. SET_STATIC_PIXEL_SHADER( volume_clouds_ps20b );
  54. }
  55. else
  56. {
  57. DECLARE_STATIC_PIXEL_SHADER( volume_clouds_ps20 );
  58. SET_STATIC_PIXEL_SHADER( volume_clouds_ps20 );
  59. }
  60. // Textures
  61. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  62. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  63. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  64. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, true );
  65. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  66. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER2, true );
  67. pShaderShadow->EnableSRGBWrite( true );
  68. // Blending
  69. pShader->EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  70. pShaderShadow->EnableAlphaWrites( false );
  71. // !!! We need to turn this back on because EnableAlphaBlending() above disables it!
  72. //pShaderShadow->EnableDepthWrites( true );
  73. }
  74. DYNAMIC_STATE
  75. {
  76. // Set Vertex Shader Combos
  77. DECLARE_DYNAMIC_VERTEX_SHADER( volume_clouds_vs20 );
  78. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  79. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  80. SET_DYNAMIC_VERTEX_SHADER( volume_clouds_vs20 );
  81. // Set Vertex Shader Constants
  82. // Time
  83. float vPackedVsConst1[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  84. float flTime = IS_PARAM_DEFINED( info.m_nTime ) && params[info.m_nTime]->GetFloatValue() > 0.0f ? params[info.m_nTime]->GetFloatValue() : pShaderAPI->CurrentTime();
  85. float flRotateSpeed = 0.065f;
  86. vPackedVsConst1[0] = flTime * flRotateSpeed * 1.0f;
  87. vPackedVsConst1[1] = flTime * flRotateSpeed * 2.0f;
  88. vPackedVsConst1[2] = flTime * flRotateSpeed * 4.0f;
  89. vPackedVsConst1[0] -= (float)( (int)( vPackedVsConst1[0] / ( 2.0f * 3.14159f ) ) ) * 2.0f * 3.14159f;
  90. vPackedVsConst1[1] -= (float)( (int)( vPackedVsConst1[1] / ( 2.0f * 3.14159f ) ) ) * 2.0f * 3.14159f;
  91. vPackedVsConst1[2] -= (float)( (int)( vPackedVsConst1[2] / ( 2.0f * 3.14159f ) ) ) * 2.0f * 3.14159f;
  92. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, vPackedVsConst1, 1 );
  93. // Set Pixel Shader Combos
  94. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() && !IsOpenGL() ) // Always send POSIX down the 20 path (rg - why?)
  95. {
  96. DECLARE_DYNAMIC_PIXEL_SHADER( volume_clouds_ps20b );
  97. SET_DYNAMIC_PIXEL_SHADER( volume_clouds_ps20b );
  98. }
  99. else
  100. {
  101. DECLARE_DYNAMIC_PIXEL_SHADER( volume_clouds_ps20 );
  102. SET_DYNAMIC_PIXEL_SHADER( volume_clouds_ps20 );
  103. }
  104. // Bind textures
  105. pShader->BindTexture( SHADER_SAMPLER0, info.m_nTexture1 );
  106. pShader->BindTexture( SHADER_SAMPLER1, info.m_nTexture2 );
  107. pShader->BindTexture( SHADER_SAMPLER2, info.m_nTexture3 );
  108. // Set Pixel Shader Constants
  109. float vEyePos[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  110. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos );
  111. pShaderAPI->SetPixelShaderConstant( 5, vEyePos, 1 );
  112. float vPackedConst6[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  113. vPackedConst6[0] = IS_PARAM_DEFINED( info.m_nRefractAmount ) ? params[info.m_nRefractAmount]->GetFloatValue() : kDefaultRefractAmount;
  114. vPackedConst6[1] = vPackedVsConst1[0]; // Time % 1000
  115. pShaderAPI->SetPixelShaderConstant( 6, vPackedConst6, 1 );
  116. }
  117. pShader->Draw();
  118. }