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.

259 lines
7.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Header: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "BaseVSShader.h"
  9. #include "WorldVertexAlpha.inc"
  10. #include "worldvertexalpha_ps20.inc"
  11. #include "worldvertexalpha_ps20b.inc"
  12. BEGIN_VS_SHADER( WorldVertexAlpha,
  13. "Help for WorldVertexAlpha" )
  14. BEGIN_SHADER_PARAMS
  15. END_SHADER_PARAMS
  16. SHADER_INIT_PARAMS()
  17. {
  18. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_LIGHTMAP );
  19. SET_FLAGS2( MATERIAL_VAR2_BLEND_WITH_LIGHTMAP_ALPHA );
  20. }
  21. SHADER_INIT
  22. {
  23. // Load the base texture here!
  24. LoadTexture( BASETEXTURE );
  25. }
  26. SHADER_FALLBACK
  27. {
  28. // if( g_pHardwareConfig->GetDXSupportLevel() < 90 || g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE )
  29. {
  30. return "WorldVertexAlpha_DX8";
  31. }
  32. return 0;
  33. }
  34. SHADER_DRAW
  35. {
  36. if( g_pHardwareConfig->SupportsVertexAndPixelShaders() && !UsingEditor( params ) )
  37. {
  38. if( g_pHardwareConfig->GetDXSupportLevel() < 90 )
  39. {
  40. // NOTE: This is the DX8, Non-Hammer version.
  41. SHADOW_STATE
  42. {
  43. // Base time lightmap (Need two texture stages)
  44. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  45. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  46. int fmt = VERTEX_POSITION;
  47. pShaderShadow->VertexShaderVertexFormat( fmt, 2, 0, 0 );
  48. pShaderShadow->EnableBlending( true );
  49. // Looks backwards, but this is done so that lightmap alpha = 1 when only
  50. // using 1 texture (needed for translucent displacements).
  51. pShaderShadow->BlendFunc( SHADER_BLEND_ONE_MINUS_SRC_ALPHA, SHADER_BLEND_SRC_ALPHA );
  52. worldvertexalpha_Static_Index vshIndex;
  53. pShaderShadow->SetVertexShader( "WorldVertexAlpha", vshIndex.GetIndex() );
  54. pShaderShadow->SetPixelShader( "WorldVertexAlpha" );
  55. FogToFogColor();
  56. }
  57. DYNAMIC_STATE
  58. {
  59. // Bind the base texture (Stage0) and lightmap (Stage1)
  60. BindTexture( SHADER_SAMPLER0, BASETEXTURE );
  61. pShaderAPI->BindStandardTexture( SHADER_SAMPLER1, TEXTURE_LIGHTMAP );
  62. EnablePixelShaderOverbright( 0, true, true );
  63. worldvertexalpha_Dynamic_Index vshIndex;
  64. vshIndex.SetDOWATERFOG( pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  65. pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  66. }
  67. Draw();
  68. }
  69. else
  70. {
  71. // DX 9 version with HDR support
  72. // Pass 1
  73. SHADOW_STATE
  74. {
  75. SetInitialShadowState();
  76. pShaderShadow->EnableAlphaWrites( true );
  77. // Base time lightmap (Need two texture stages)
  78. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  79. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  80. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  81. int fmt = VERTEX_POSITION;
  82. pShaderShadow->VertexShaderVertexFormat( fmt, 2, 0, 0 );
  83. pShaderShadow->EnableBlending( true );
  84. // Looks backwards, but this is done so that lightmap alpha = 1 when only
  85. // using 1 texture (needed for translucent displacements).
  86. pShaderShadow->BlendFunc( SHADER_BLEND_ONE_MINUS_SRC_ALPHA, SHADER_BLEND_SRC_ALPHA );
  87. pShaderShadow->EnableBlendingSeparateAlpha( true );
  88. pShaderShadow->BlendFuncSeparateAlpha( SHADER_BLEND_ZERO, SHADER_BLEND_SRC_ALPHA );
  89. worldvertexalpha_Static_Index vshIndex;
  90. pShaderShadow->SetVertexShader( "WorldVertexAlpha", vshIndex.GetIndex() );
  91. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  92. {
  93. DECLARE_STATIC_PIXEL_SHADER( worldvertexalpha_ps20b );
  94. SET_STATIC_PIXEL_SHADER_COMBO( PASS, 0 );
  95. SET_STATIC_PIXEL_SHADER( worldvertexalpha_ps20b );
  96. }
  97. else
  98. {
  99. DECLARE_STATIC_PIXEL_SHADER( worldvertexalpha_ps20 );
  100. SET_STATIC_PIXEL_SHADER_COMBO( PASS, 0 );
  101. SET_STATIC_PIXEL_SHADER( worldvertexalpha_ps20 );
  102. }
  103. FogToFogColor();
  104. }
  105. DYNAMIC_STATE
  106. {
  107. // Bind the base texture (Stage0) and lightmap (Stage1)
  108. BindTexture( SHADER_SAMPLER0, BASETEXTURE );
  109. pShaderAPI->BindStandardTexture( SHADER_SAMPLER1, TEXTURE_LIGHTMAP );
  110. worldvertexalpha_Dynamic_Index vshIndex;
  111. vshIndex.SetDOWATERFOG( pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  112. pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  113. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  114. {
  115. DECLARE_DYNAMIC_PIXEL_SHADER( worldvertexalpha_ps20b );
  116. SET_DYNAMIC_PIXEL_SHADER( worldvertexalpha_ps20b );
  117. }
  118. else
  119. {
  120. DECLARE_DYNAMIC_PIXEL_SHADER( worldvertexalpha_ps20 );
  121. SET_DYNAMIC_PIXEL_SHADER( worldvertexalpha_ps20 );
  122. }
  123. }
  124. Draw();
  125. // Pass 2
  126. SHADOW_STATE
  127. {
  128. SetInitialShadowState();
  129. pShaderShadow->EnableAlphaWrites( true );
  130. pShaderShadow->EnableColorWrites( false );
  131. // Base time lightmap (Need two texture stages)
  132. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  133. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  134. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  135. int fmt = VERTEX_POSITION;
  136. pShaderShadow->VertexShaderVertexFormat( fmt, 2, 0, 0 );
  137. pShaderShadow->EnableBlending( true );
  138. // Looks backwards, but this is done so that lightmap alpha = 1 when only
  139. // using 1 texture (needed for translucent displacements).
  140. pShaderShadow->BlendFunc( SHADER_BLEND_ONE_MINUS_SRC_ALPHA, SHADER_BLEND_SRC_ALPHA );
  141. pShaderShadow->EnableBlendingSeparateAlpha( true );
  142. pShaderShadow->BlendFuncSeparateAlpha( SHADER_BLEND_ONE, SHADER_BLEND_ONE );
  143. worldvertexalpha_Static_Index vshIndex;
  144. pShaderShadow->SetVertexShader( "WorldVertexAlpha", vshIndex.GetIndex() );
  145. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  146. {
  147. DECLARE_STATIC_PIXEL_SHADER( worldvertexalpha_ps20b );
  148. SET_STATIC_PIXEL_SHADER_COMBO( PASS, 1 );
  149. SET_STATIC_PIXEL_SHADER( worldvertexalpha_ps20b );
  150. }
  151. else
  152. {
  153. DECLARE_STATIC_PIXEL_SHADER( worldvertexalpha_ps20 );
  154. SET_STATIC_PIXEL_SHADER_COMBO( PASS, 1 );
  155. SET_STATIC_PIXEL_SHADER( worldvertexalpha_ps20 );
  156. }
  157. FogToFogColor();
  158. }
  159. DYNAMIC_STATE
  160. {
  161. // Bind the base texture (Stage0) and lightmap (Stage1)
  162. BindTexture( SHADER_SAMPLER0, BASETEXTURE );
  163. pShaderAPI->BindStandardTexture( SHADER_SAMPLER1, TEXTURE_LIGHTMAP );
  164. worldvertexalpha_Dynamic_Index vshIndex;
  165. vshIndex.SetDOWATERFOG( pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  166. pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  167. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  168. {
  169. DECLARE_DYNAMIC_PIXEL_SHADER( worldvertexalpha_ps20b );
  170. SET_DYNAMIC_PIXEL_SHADER( worldvertexalpha_ps20b );
  171. }
  172. else
  173. {
  174. DECLARE_DYNAMIC_PIXEL_SHADER( worldvertexalpha_ps20 );
  175. SET_DYNAMIC_PIXEL_SHADER( worldvertexalpha_ps20 );
  176. }
  177. }
  178. Draw();
  179. }
  180. }
  181. else
  182. {
  183. // NOTE: This is the DX7, Hammer version.
  184. SHADOW_STATE
  185. {
  186. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_LIGHTMAP );
  187. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  188. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  189. pShaderShadow->OverbrightValue( SHADER_TEXTURE_STAGE1, OVERBRIGHT );
  190. pShaderShadow->EnableBlending( true );
  191. // Looks backwards, but this is done so that lightmap alpha = 1 when only
  192. // using 1 texture (needed for translucent displacements).
  193. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  194. // pShaderShadow->BlendFunc( SHADER_BLEND_ONE_MINUS_SRC_ALPHA, SHADER_BLEND_SRC_ALPHA );
  195. // Use vertex color for Hammer because it puts the blending alpha in the vertices.
  196. unsigned int colorFlag = 0;
  197. if( UsingEditor( params ) )
  198. {
  199. colorFlag |= SHADER_DRAW_COLOR;
  200. }
  201. pShaderShadow->DrawFlags( colorFlag | SHADER_DRAW_POSITION | SHADER_DRAW_TEXCOORD1 |
  202. SHADER_DRAW_LIGHTMAP_TEXCOORD0 );
  203. }
  204. DYNAMIC_STATE
  205. {
  206. BindTexture( SHADER_SAMPLER1, BASETEXTURE );
  207. pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, TEXTURE_LIGHTMAP );
  208. }
  209. Draw();
  210. }
  211. }
  212. END_SHADER