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.

149 lines
4.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: A shader that builds the shadow using render-to-texture
  4. //
  5. // $Header: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "BaseVSShader.h"
  9. #include "mathlib/vmatrix.h"
  10. #include "unlitgeneric_vs20.inc"
  11. #include "shadowbuildtexture_ps20.inc"
  12. #include "shadowbuildtexture_ps20b.inc"
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. DEFINE_FALLBACK_SHADER( ShadowBuild, ShadowBuild_DX9 )
  16. BEGIN_VS_SHADER_FLAGS( ShadowBuild_DX9, "Help for ShadowBuild", SHADER_NOT_EDITABLE )
  17. BEGIN_SHADER_PARAMS
  18. SHADER_PARAM( TRANSLUCENT_MATERIAL, SHADER_PARAM_TYPE_MATERIAL, "", "Points to a material to grab translucency from" )
  19. END_SHADER_PARAMS
  20. SHADER_INIT_PARAMS()
  21. {
  22. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  23. SET_FLAGS( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  24. }
  25. SHADER_FALLBACK
  26. {
  27. if ( !g_pHardwareConfig->SupportsVertexAndPixelShaders() )
  28. return "ShadowBuild_DX6";
  29. if ( g_pHardwareConfig->GetDXSupportLevel() < 90 )
  30. return "ShadowBuild_DX8";
  31. return 0;
  32. }
  33. SHADER_INIT
  34. {
  35. if ( params[BASETEXTURE]->IsDefined() )
  36. {
  37. LoadTexture( BASETEXTURE, TEXTUREFLAGS_SRGB );
  38. }
  39. }
  40. SHADER_DRAW
  41. {
  42. SHADOW_STATE
  43. {
  44. // Add the alphas into the frame buffer
  45. EnableAlphaBlending( SHADER_BLEND_ONE, SHADER_BLEND_ONE );
  46. // base texture. We just use this for alpha, but enable SRGB read to make everything consistent.
  47. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  48. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  49. pShaderShadow->EnableSRGBWrite( true );
  50. pShaderShadow->EnableAlphaWrites( true );
  51. pShaderShadow->EnableDepthWrites( false );
  52. pShaderShadow->DepthFunc( SHADER_DEPTHFUNC_ALWAYS );
  53. // Specify vertex format (note that this shader supports compression)
  54. unsigned int flags = VERTEX_POSITION | VERTEX_FORMAT_COMPRESSED;
  55. unsigned int nTexCoordCount = 1;
  56. unsigned int userDataSize = 0;
  57. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, NULL, userDataSize );
  58. DECLARE_STATIC_VERTEX_SHADER( unlitgeneric_vs20 );
  59. SET_STATIC_VERTEX_SHADER_COMBO( VERTEXCOLOR, 0 );
  60. SET_STATIC_VERTEX_SHADER( unlitgeneric_vs20 );
  61. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  62. {
  63. DECLARE_STATIC_PIXEL_SHADER( shadowbuildtexture_ps20b );
  64. SET_STATIC_PIXEL_SHADER( shadowbuildtexture_ps20b );
  65. }
  66. else
  67. {
  68. DECLARE_STATIC_PIXEL_SHADER( shadowbuildtexture_ps20 );
  69. SET_STATIC_PIXEL_SHADER( shadowbuildtexture_ps20 );
  70. }
  71. }
  72. DYNAMIC_STATE
  73. {
  74. SetModulationVertexShaderDynamicState();
  75. // Snack important parameters from the original material
  76. // FIXME: What about alpha modulation? Need a solution for that
  77. ITexture *pTexture = NULL;
  78. IMaterialVar **ppTranslucentParams = NULL;
  79. if (params[TRANSLUCENT_MATERIAL]->IsDefined())
  80. {
  81. IMaterial *pMaterial = params[TRANSLUCENT_MATERIAL]->GetMaterialValue();
  82. if (pMaterial)
  83. {
  84. ppTranslucentParams = pMaterial->GetShaderParams();
  85. if ( ppTranslucentParams[BASETEXTURE]->IsTexture() )
  86. {
  87. pTexture = ppTranslucentParams[BASETEXTURE]->GetTextureValue();
  88. }
  89. }
  90. }
  91. if (pTexture)
  92. {
  93. BindTexture( SHADER_SAMPLER0, pTexture, ppTranslucentParams[FRAME]->GetIntValue() );
  94. Vector4D transformation[2];
  95. const VMatrix &mat = ppTranslucentParams[BASETEXTURETRANSFORM]->GetMatrixValue();
  96. transformation[0].Init( mat[0][0], mat[0][1], mat[0][2], mat[0][3] );
  97. transformation[1].Init( mat[1][0], mat[1][1], mat[1][2], mat[1][3] );
  98. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, transformation[0].Base(), 2 );
  99. }
  100. else
  101. {
  102. pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, TEXTURE_LIGHTMAP_FULLBRIGHT );
  103. }
  104. float vVertexColor[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  105. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_6, vVertexColor, 1 );
  106. // Compute the vertex shader index.
  107. DECLARE_DYNAMIC_VERTEX_SHADER( unlitgeneric_vs20 );
  108. SET_DYNAMIC_VERTEX_SHADER_COMBO( DOWATERFOG, pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  109. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  110. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  111. SET_DYNAMIC_VERTEX_SHADER( unlitgeneric_vs20 );
  112. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  113. {
  114. DECLARE_DYNAMIC_PIXEL_SHADER( shadowbuildtexture_ps20b );
  115. SET_DYNAMIC_PIXEL_SHADER( shadowbuildtexture_ps20b );
  116. }
  117. else
  118. {
  119. DECLARE_DYNAMIC_PIXEL_SHADER( shadowbuildtexture_ps20 );
  120. SET_DYNAMIC_PIXEL_SHADER( shadowbuildtexture_ps20 );
  121. }
  122. }
  123. Draw( );
  124. }
  125. END_SHADER