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.

118 lines
3.6 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_vs11.inc"
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. DEFINE_FALLBACK_SHADER( ShadowBuild, ShadowBuild_DX8 )
  14. BEGIN_VS_SHADER_FLAGS( ShadowBuild_DX8, "Help for ShadowBuild", SHADER_NOT_EDITABLE )
  15. BEGIN_SHADER_PARAMS
  16. SHADER_PARAM( TRANSLUCENT_MATERIAL, SHADER_PARAM_TYPE_MATERIAL, "", "Points to a material to grab translucency from" )
  17. END_SHADER_PARAMS
  18. SHADER_INIT_PARAMS()
  19. {
  20. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  21. SET_FLAGS( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  22. }
  23. SHADER_FALLBACK
  24. {
  25. if ( !g_pHardwareConfig->SupportsVertexAndPixelShaders() )
  26. return "ShadowBuild_DX6";
  27. return 0;
  28. }
  29. SHADER_INIT
  30. {
  31. if (params[BASETEXTURE]->IsDefined())
  32. {
  33. LoadTexture(BASETEXTURE);
  34. }
  35. }
  36. SHADER_DRAW
  37. {
  38. SHADOW_STATE
  39. {
  40. // Add the alphas into the frame buffer
  41. EnableAlphaBlending( SHADER_BLEND_ONE, SHADER_BLEND_ONE );
  42. // base texture. We just use this for alpha, but enable SRGB read to make everything consistent.
  43. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  44. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  45. pShaderShadow->EnableSRGBWrite( true );
  46. pShaderShadow->EnableAlphaWrites( true );
  47. pShaderShadow->EnableDepthWrites( false );
  48. pShaderShadow->DepthFunc( SHADER_DEPTHFUNC_ALWAYS );
  49. pShaderShadow->VertexShaderVertexFormat( VERTEX_POSITION, 1, 0, 0 );
  50. unlitgeneric_vs11_Static_Index vshIndex;
  51. vshIndex.SetDETAIL( false );
  52. vshIndex.SetENVMAP( false );
  53. vshIndex.SetENVMAPCAMERASPACE( false );
  54. vshIndex.SetENVMAPSPHERE( false );
  55. vshIndex.SetVERTEXCOLOR( false );
  56. vshIndex.SetSEPARATEDETAILUVS( false );
  57. pShaderShadow->SetVertexShader( "UnlitGeneric_vs11", vshIndex.GetIndex() );
  58. pShaderShadow->SetPixelShader( "ShadowBuildTexture" );
  59. }
  60. DYNAMIC_STATE
  61. {
  62. SetModulationVertexShaderDynamicState();
  63. // Snack important parameters from the original material
  64. // FIXME: What about alpha modulation? Need a solution for that
  65. ITexture *pTexture = NULL;
  66. IMaterialVar **ppTranslucentParams = NULL;
  67. if (params[TRANSLUCENT_MATERIAL]->IsDefined())
  68. {
  69. IMaterial *pMaterial = params[TRANSLUCENT_MATERIAL]->GetMaterialValue();
  70. if (pMaterial)
  71. {
  72. ppTranslucentParams = pMaterial->GetShaderParams();
  73. if ( ppTranslucentParams[BASETEXTURE]->IsTexture() )
  74. {
  75. pTexture = ppTranslucentParams[BASETEXTURE]->GetTextureValue();
  76. }
  77. }
  78. }
  79. if (pTexture)
  80. {
  81. BindTexture( SHADER_SAMPLER0, pTexture, ppTranslucentParams[FRAME]->GetIntValue() );
  82. Vector4D transformation[2];
  83. const VMatrix &mat = ppTranslucentParams[BASETEXTURETRANSFORM]->GetMatrixValue();
  84. transformation[0].Init( mat[0][0], mat[0][1], mat[0][2], mat[0][3] );
  85. transformation[1].Init( mat[1][0], mat[1][1], mat[1][2], mat[1][3] );
  86. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, transformation[0].Base(), 2 );
  87. }
  88. else
  89. {
  90. pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, TEXTURE_LIGHTMAP_FULLBRIGHT );
  91. }
  92. // Compute the vertex shader index.
  93. unlitgeneric_vs11_Dynamic_Index vshIndex;
  94. vshIndex.SetDOWATERFOG( pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  95. vshIndex.SetSKINNING( pShaderAPI->GetCurrentNumBones() > 0 );
  96. pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  97. }
  98. Draw( );
  99. }
  100. END_SHADER