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.

222 lines
6.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Header: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "BaseVSShader.h"
  9. #include "unlittwotexture.inc"
  10. #include "cloak_blended_pass_helper.h"
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. DEFINE_FALLBACK_SHADER( UnlitTwoTexture, UnlitTwoTexture_DX8 )
  14. BEGIN_VS_SHADER( UnlitTwoTexture_DX8, "Help for UnlitTwoTexture" )
  15. BEGIN_SHADER_PARAMS
  16. SHADER_PARAM( TEXTURE2, SHADER_PARAM_TYPE_TEXTURE, "shadertest/BaseTexture", "second texture" )
  17. SHADER_PARAM( FRAME2, SHADER_PARAM_TYPE_INTEGER, "0", "frame number for $texture2" )
  18. SHADER_PARAM( TEXTURE2TRANSFORM, SHADER_PARAM_TYPE_MATRIX, "center .5 .5 scale 1 1 rotate 0 translate 0 0", "$texture2 texcoord transform" )
  19. // Cloak Pass
  20. SHADER_PARAM( CLOAKPASSENABLED, SHADER_PARAM_TYPE_BOOL, "0", "Enables cloak render in a second pass" )
  21. SHADER_PARAM( CLOAKFACTOR, SHADER_PARAM_TYPE_FLOAT, "0.0", "" )
  22. SHADER_PARAM( CLOAKCOLORTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "Cloak color tint" )
  23. SHADER_PARAM( REFRACTAMOUNT, SHADER_PARAM_TYPE_FLOAT, "2", "" )
  24. END_SHADER_PARAMS
  25. SHADER_FALLBACK
  26. {
  27. if ( !g_pHardwareConfig->SupportsVertexAndPixelShaders())
  28. {
  29. return "UnlitTwoTexture_DX6";
  30. }
  31. return 0;
  32. }
  33. // Cloak Pass
  34. void SetupVarsCloakBlendedPass( CloakBlendedPassVars_t &info )
  35. {
  36. info.m_nCloakFactor = CLOAKFACTOR;
  37. info.m_nCloakColorTint = CLOAKCOLORTINT;
  38. info.m_nRefractAmount = REFRACTAMOUNT;
  39. }
  40. bool NeedsPowerOfTwoFrameBufferTexture( IMaterialVar **params, bool bCheckSpecificToThisFrame ) const
  41. {
  42. if ( params[CLOAKPASSENABLED]->GetIntValue() ) // If material supports cloaking
  43. {
  44. if ( bCheckSpecificToThisFrame == false ) // For setting model flag at load time
  45. return true;
  46. else if ( ( params[CLOAKFACTOR]->GetFloatValue() > 0.0f ) && ( params[CLOAKFACTOR]->GetFloatValue() < 1.0f ) ) // Per-frame check
  47. return true;
  48. // else, not cloaking this frame, so check flag2 in case the base material still needs it
  49. }
  50. // Check flag2 if not drawing cloak pass
  51. return IS_FLAG2_SET( MATERIAL_VAR2_NEEDS_POWER_OF_TWO_FRAME_BUFFER_TEXTURE );
  52. }
  53. bool IsTranslucent( IMaterialVar **params ) const
  54. {
  55. if ( params[CLOAKPASSENABLED]->GetIntValue() ) // If material supports cloaking
  56. {
  57. if ( ( params[CLOAKFACTOR]->GetFloatValue() > 0.0f ) && ( params[CLOAKFACTOR]->GetFloatValue() < 1.0f ) ) // Per-frame check
  58. return true;
  59. // else, not cloaking this frame, so check flag in case the base material still needs it
  60. }
  61. // Check flag if not drawing cloak pass
  62. return IS_FLAG_SET( MATERIAL_VAR_TRANSLUCENT );
  63. }
  64. SHADER_INIT_PARAMS()
  65. {
  66. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  67. // Cloak Pass
  68. if ( !params[CLOAKPASSENABLED]->IsDefined() )
  69. {
  70. params[CLOAKPASSENABLED]->SetIntValue( 0 );
  71. }
  72. else if ( params[CLOAKPASSENABLED]->GetIntValue() )
  73. {
  74. CloakBlendedPassVars_t info;
  75. SetupVarsCloakBlendedPass( info );
  76. InitParamsCloakBlendedPass( this, params, pMaterialName, info );
  77. }
  78. }
  79. SHADER_INIT
  80. {
  81. if (params[BASETEXTURE]->IsDefined())
  82. LoadTexture( BASETEXTURE );
  83. if (params[TEXTURE2]->IsDefined())
  84. LoadTexture( TEXTURE2 );
  85. // Cloak Pass
  86. if ( params[CLOAKPASSENABLED]->GetIntValue() )
  87. {
  88. CloakBlendedPassVars_t info;
  89. SetupVarsCloakBlendedPass( info );
  90. InitCloakBlendedPass( this, params, info );
  91. }
  92. }
  93. SHADER_DRAW
  94. {
  95. // Skip the standard rendering if cloak pass is fully opaque
  96. bool bDrawStandardPass = true;
  97. if ( params[CLOAKPASSENABLED]->GetIntValue() && ( pShaderShadow == NULL ) ) // && not snapshotting
  98. {
  99. CloakBlendedPassVars_t info;
  100. SetupVarsCloakBlendedPass( info );
  101. if ( CloakBlendedPassIsFullyOpaque( params, info ) )
  102. {
  103. // There is some strangeness in DX8 when trying to skip the main pass, so leave this alone for now
  104. //bDrawStandardPass = false;
  105. }
  106. }
  107. // Standard rendering pass
  108. if ( bDrawStandardPass )
  109. {
  110. SHADOW_STATE
  111. {
  112. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  113. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  114. // Either we've got a constant modulation
  115. bool isTranslucent = IsAlphaModulating();
  116. // Or we've got a texture alpha on either texture
  117. isTranslucent = isTranslucent || TextureIsTranslucent( BASETEXTURE, true ) ||
  118. TextureIsTranslucent( TEXTURE2, true );
  119. if ( isTranslucent )
  120. {
  121. if ( IS_FLAG_SET(MATERIAL_VAR_ADDITIVE) )
  122. {
  123. EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE );
  124. }
  125. else
  126. {
  127. EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  128. }
  129. }
  130. else
  131. {
  132. if ( IS_FLAG_SET(MATERIAL_VAR_ADDITIVE) )
  133. {
  134. EnableAlphaBlending( SHADER_BLEND_ONE, SHADER_BLEND_ONE );
  135. }
  136. else
  137. {
  138. DisableAlphaBlending( );
  139. }
  140. }
  141. int fmt = VERTEX_POSITION | VERTEX_NORMAL;
  142. if (IS_FLAG_SET( MATERIAL_VAR_VERTEXCOLOR ))
  143. {
  144. fmt |= VERTEX_COLOR;
  145. }
  146. pShaderShadow->VertexShaderVertexFormat( fmt, 1, 0, 0 );
  147. unlittwotexture_Static_Index vshIndex;
  148. pShaderShadow->SetVertexShader( "UnlitTwoTexture", vshIndex.GetIndex() );
  149. pShaderShadow->SetPixelShader( "UnlitTwoTexture" );
  150. DefaultFog();
  151. }
  152. DYNAMIC_STATE
  153. {
  154. if ( pShaderAPI->InFlashlightMode() ) // Not snapshotting && flashlight pass
  155. {
  156. Draw( false );
  157. return;
  158. }
  159. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  160. BindTexture( SHADER_SAMPLER1, TEXTURE2, FRAME2 );
  161. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, BASETEXTURETRANSFORM );
  162. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_2, TEXTURE2TRANSFORM );
  163. SetModulationVertexShaderDynamicState();
  164. unlittwotexture_Dynamic_Index vshIndex;
  165. vshIndex.SetDOWATERFOG( pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  166. vshIndex.SetSKINNING( pShaderAPI->GetCurrentNumBones() > 0 );
  167. pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  168. }
  169. Draw();
  170. }
  171. else
  172. {
  173. // Skip this pass!
  174. Draw( false );
  175. }
  176. // Cloak Pass
  177. if ( params[CLOAKPASSENABLED]->GetIntValue() )
  178. {
  179. // If ( snapshotting ) or ( we need to draw this frame )
  180. if ( ( pShaderShadow != NULL ) || ( ( params[CLOAKFACTOR]->GetFloatValue() > 0.0f ) && ( params[CLOAKFACTOR]->GetFloatValue() < 1.0f ) ) )
  181. {
  182. CloakBlendedPassVars_t info;
  183. SetupVarsCloakBlendedPass( info );
  184. DrawCloakBlendedPass( this, params, pShaderAPI, pShaderShadow, info, vertexCompression );
  185. }
  186. else // We're not snapshotting and we don't need to draw this frame
  187. {
  188. // Skip this pass!
  189. Draw( false );
  190. }
  191. }
  192. }
  193. END_SHADER