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.

197 lines
6.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "BaseVSShader.h"
  7. #include "unlittwotexture_vs20.inc"
  8. #include "monitorscreen_ps20.inc"
  9. #include "monitorscreen_ps20b.inc"
  10. #include "cpp_shader_constant_register_map.h"
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. DEFINE_FALLBACK_SHADER( MonitorScreen, MonitorScreen_DX9 )
  14. BEGIN_VS_SHADER( MonitorScreen_DX9,
  15. "This is a shader that does a contrast/saturation version of base times lightmap." )
  16. BEGIN_SHADER_PARAMS
  17. SHADER_PARAM( CONTRAST, SHADER_PARAM_TYPE_FLOAT, "0.0", "contrast 0 == normal 1 == color*color" )
  18. SHADER_PARAM( SATURATION, SHADER_PARAM_TYPE_FLOAT, "1.0", "saturation 0 == greyscale 1 == normal" )
  19. SHADER_PARAM( TINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "monitor tint" )
  20. SHADER_PARAM( TEXTURE2, SHADER_PARAM_TYPE_TEXTURE, "shadertest/lightmappedtexture", "second texture" )
  21. SHADER_PARAM( FRAME2, SHADER_PARAM_TYPE_INTEGER, "0", "frame number for $texture2" )
  22. SHADER_PARAM( TEXTURE2TRANSFORM, SHADER_PARAM_TYPE_MATRIX, "center .5 .5 scale 1 1 rotate 0 translate 0 0", "$texture2 texcoord transform" )
  23. END_SHADER_PARAMS
  24. // Set up anything that is necessary to make decisions in SHADER_FALLBACK.
  25. SHADER_INIT_PARAMS()
  26. {
  27. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  28. if( !params[CONTRAST]->IsDefined() )
  29. {
  30. params[CONTRAST]->SetFloatValue( 0.0f );
  31. }
  32. if( !params[SATURATION]->IsDefined() )
  33. {
  34. params[SATURATION]->SetFloatValue( 1.0f );
  35. }
  36. if( !params[TINT]->IsDefined() )
  37. {
  38. params[TINT]->SetVecValue( 1.0f, 1.0f, 1.0f );
  39. }
  40. if (!IS_FLAG_DEFINED( MATERIAL_VAR_MODEL ))
  41. {
  42. CLEAR_FLAGS( MATERIAL_VAR_MODEL );
  43. }
  44. }
  45. SHADER_FALLBACK
  46. {
  47. if( params && !params[BASETEXTURE]->IsDefined() )
  48. {
  49. if( IS_FLAG_DEFINED( MATERIAL_VAR_MODEL ) )
  50. {
  51. return "VertexLitGeneric_DX6";
  52. }
  53. else
  54. {
  55. return "LightmappedGeneric_DX6";
  56. }
  57. }
  58. if ( !(g_pHardwareConfig->SupportsPixelShaders_2_0() && g_pHardwareConfig->SupportsVertexShaders_2_0()) ||
  59. (g_pHardwareConfig->GetDXSupportLevel() < 90) )
  60. {
  61. return "MonitorScreen_DX8";
  62. }
  63. return 0;
  64. }
  65. SHADER_INIT
  66. {
  67. if (params[BASETEXTURE]->IsDefined())
  68. {
  69. LoadTexture( BASETEXTURE, TEXTUREFLAGS_SRGB );
  70. }
  71. if (params[TEXTURE2]->IsDefined())
  72. {
  73. LoadTexture( TEXTURE2, TEXTUREFLAGS_SRGB );
  74. }
  75. }
  76. SHADER_DRAW
  77. {
  78. bool bHasTexture2 = params[TEXTURE2]->IsTexture();
  79. BlendType_t nBlendType = EvaluateBlendRequirements( BASETEXTURE, true );
  80. bool bFullyOpaque = (nBlendType != BT_BLENDADD) && (nBlendType != BT_BLEND) && !IS_FLAG_SET(MATERIAL_VAR_ALPHATEST); //dest alpha is free for special use
  81. SHADOW_STATE
  82. {
  83. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  84. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  85. if ( bHasTexture2 )
  86. {
  87. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  88. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, true );
  89. }
  90. pShaderShadow->EnableSRGBWrite( true );
  91. // Either we've got a constant modulation
  92. bool isTranslucent = IsAlphaModulating();
  93. // Or we've got a texture alpha on either texture
  94. isTranslucent = isTranslucent || TextureIsTranslucent( BASETEXTURE, true ) ||
  95. TextureIsTranslucent( TEXTURE2, true );
  96. if ( isTranslucent )
  97. {
  98. if ( IS_FLAG_SET(MATERIAL_VAR_ADDITIVE) )
  99. EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE );
  100. else
  101. EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  102. }
  103. else
  104. {
  105. if ( IS_FLAG_SET(MATERIAL_VAR_ADDITIVE) )
  106. EnableAlphaBlending( SHADER_BLEND_ONE, SHADER_BLEND_ONE );
  107. else
  108. DisableAlphaBlending( );
  109. }
  110. // Set stream format (note that this shader supports compression)
  111. unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL | VERTEX_FORMAT_COMPRESSED;
  112. int nTexCoordCount = 1;
  113. int userDataSize = 0;
  114. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, NULL, userDataSize );
  115. DECLARE_STATIC_VERTEX_SHADER( unlittwotexture_vs20 );
  116. SET_STATIC_VERTEX_SHADER( unlittwotexture_vs20 );
  117. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  118. {
  119. DECLARE_STATIC_PIXEL_SHADER( monitorscreen_ps20b );
  120. SET_STATIC_PIXEL_SHADER_COMBO( TEXTURE2, (bHasTexture2)?(1):(0) );
  121. SET_STATIC_PIXEL_SHADER( monitorscreen_ps20b );
  122. }
  123. else
  124. {
  125. DECLARE_STATIC_PIXEL_SHADER( monitorscreen_ps20 );
  126. SET_STATIC_PIXEL_SHADER_COMBO( TEXTURE2, (bHasTexture2)?(1):(0) );
  127. SET_STATIC_PIXEL_SHADER( monitorscreen_ps20 );
  128. }
  129. DefaultFog();
  130. pShaderShadow->EnableAlphaWrites( bFullyOpaque );
  131. }
  132. DYNAMIC_STATE
  133. {
  134. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  135. if( bHasTexture2 )
  136. {
  137. BindTexture( SHADER_SAMPLER1, TEXTURE2, FRAME2 );
  138. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_2, TEXTURE2TRANSFORM );
  139. }
  140. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, BASETEXTURETRANSFORM );
  141. SetPixelShaderConstant( 1, CONTRAST );
  142. SetPixelShaderConstant( 2, SATURATION );
  143. SetPixelShaderConstant( 3, TINT );
  144. SetModulationVertexShaderDynamicState();
  145. pShaderAPI->SetPixelShaderFogParams( PSREG_FOG_PARAMS );
  146. float vEyePos_SpecExponent[4];
  147. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos_SpecExponent );
  148. vEyePos_SpecExponent[3] = 0.0f;
  149. pShaderAPI->SetPixelShaderConstant( PSREG_EYEPOS_SPEC_EXPONENT, vEyePos_SpecExponent, 1 );
  150. DECLARE_DYNAMIC_VERTEX_SHADER( unlittwotexture_vs20 );
  151. SET_DYNAMIC_VERTEX_SHADER_COMBO( DOWATERFOG, pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  152. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  153. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  154. SET_DYNAMIC_VERTEX_SHADER( unlittwotexture_vs20 );
  155. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  156. {
  157. DECLARE_DYNAMIC_PIXEL_SHADER( monitorscreen_ps20b );
  158. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  159. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITE_DEPTH_TO_DESTALPHA, bFullyOpaque && pShaderAPI->ShouldWriteDepthToDestAlpha() );
  160. SET_DYNAMIC_PIXEL_SHADER( monitorscreen_ps20b );
  161. }
  162. else
  163. {
  164. DECLARE_DYNAMIC_PIXEL_SHADER( monitorscreen_ps20 );
  165. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  166. SET_DYNAMIC_PIXEL_SHADER( monitorscreen_ps20 );
  167. }
  168. }
  169. Draw();
  170. }
  171. END_SHADER