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.

280 lines
9.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Header: $
  6. // $NoKeywords: $
  7. //===========================================================================//
  8. #include "BaseVSShader.h"
  9. #include "cloak_blended_pass_helper.h"
  10. #include "cpp_shader_constant_register_map.h"
  11. #include "unlittwotexture_vs20.inc"
  12. #include "unlittwotexture_ps20.inc"
  13. #include "unlittwotexture_ps20b.inc"
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include "tier0/memdbgon.h"
  16. DEFINE_FALLBACK_SHADER( UnlitTwoTexture, UnlitTwoTexture_DX9 )
  17. extern ConVar r_flashlight_version2;
  18. BEGIN_VS_SHADER( UnlitTwoTexture_DX9, "Help for UnlitTwoTexture_DX9" )
  19. BEGIN_SHADER_PARAMS
  20. SHADER_PARAM( TEXTURE2, SHADER_PARAM_TYPE_TEXTURE, "shadertest/BaseTexture", "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. // Cloak Pass
  24. SHADER_PARAM( CLOAKPASSENABLED, SHADER_PARAM_TYPE_BOOL, "0", "Enables cloak render in a second pass" )
  25. SHADER_PARAM( CLOAKFACTOR, SHADER_PARAM_TYPE_FLOAT, "0.0", "" )
  26. SHADER_PARAM( CLOAKCOLORTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "Cloak color tint" )
  27. SHADER_PARAM( REFRACTAMOUNT, SHADER_PARAM_TYPE_FLOAT, "2", "" )
  28. END_SHADER_PARAMS
  29. SHADER_FALLBACK
  30. {
  31. if ( g_pHardwareConfig->GetDXSupportLevel() < 90)
  32. {
  33. return "UnlitTwoTexture_DX8";
  34. }
  35. return 0;
  36. }
  37. // Cloak Pass
  38. void SetupVarsCloakBlendedPass( CloakBlendedPassVars_t &info )
  39. {
  40. info.m_nCloakFactor = CLOAKFACTOR;
  41. info.m_nCloakColorTint = CLOAKCOLORTINT;
  42. info.m_nRefractAmount = REFRACTAMOUNT;
  43. }
  44. bool NeedsPowerOfTwoFrameBufferTexture( IMaterialVar **params, bool bCheckSpecificToThisFrame ) const
  45. {
  46. if ( params[CLOAKPASSENABLED]->GetIntValue() ) // If material supports cloaking
  47. {
  48. if ( bCheckSpecificToThisFrame == false ) // For setting model flag at load time
  49. return true;
  50. else if ( ( params[CLOAKFACTOR]->GetFloatValue() > 0.0f ) && ( params[CLOAKFACTOR]->GetFloatValue() < 1.0f ) ) // Per-frame check
  51. return true;
  52. // else, not cloaking this frame, so check flag2 in case the base material still needs it
  53. }
  54. // Check flag2 if not drawing cloak pass
  55. return IS_FLAG2_SET( MATERIAL_VAR2_NEEDS_POWER_OF_TWO_FRAME_BUFFER_TEXTURE );
  56. }
  57. bool IsTranslucent( IMaterialVar **params ) const
  58. {
  59. if ( params[CLOAKPASSENABLED]->GetIntValue() ) // If material supports cloaking
  60. {
  61. if ( ( params[CLOAKFACTOR]->GetFloatValue() > 0.0f ) && ( params[CLOAKFACTOR]->GetFloatValue() < 1.0f ) ) // Per-frame check
  62. return true;
  63. // else, not cloaking this frame, so check flag in case the base material still needs it
  64. }
  65. // Check flag if not drawing cloak pass
  66. return IS_FLAG_SET( MATERIAL_VAR_TRANSLUCENT );
  67. }
  68. SHADER_INIT_PARAMS()
  69. {
  70. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  71. // Cloak Pass
  72. if ( !params[CLOAKPASSENABLED]->IsDefined() )
  73. {
  74. params[CLOAKPASSENABLED]->SetIntValue( 0 );
  75. }
  76. else if ( params[CLOAKPASSENABLED]->GetIntValue() )
  77. {
  78. CloakBlendedPassVars_t info;
  79. SetupVarsCloakBlendedPass( info );
  80. InitParamsCloakBlendedPass( this, params, pMaterialName, info );
  81. }
  82. }
  83. SHADER_INIT
  84. {
  85. if (params[BASETEXTURE]->IsDefined())
  86. LoadTexture( BASETEXTURE, TEXTUREFLAGS_SRGB );
  87. if (params[TEXTURE2]->IsDefined())
  88. LoadTexture( TEXTURE2, TEXTUREFLAGS_SRGB );
  89. // Cloak Pass
  90. if ( params[CLOAKPASSENABLED]->GetIntValue() )
  91. {
  92. CloakBlendedPassVars_t info;
  93. SetupVarsCloakBlendedPass( info );
  94. InitCloakBlendedPass( this, params, info );
  95. }
  96. }
  97. SHADER_DRAW
  98. {
  99. // Skip the standard rendering if cloak pass is fully opaque
  100. bool bDrawStandardPass = true;
  101. if ( params[CLOAKPASSENABLED]->GetIntValue() && ( pShaderShadow == NULL ) ) // && not snapshotting
  102. {
  103. CloakBlendedPassVars_t info;
  104. SetupVarsCloakBlendedPass( info );
  105. if ( CloakBlendedPassIsFullyOpaque( params, info ) )
  106. {
  107. bDrawStandardPass = false;
  108. }
  109. }
  110. // Skip flashlight pass for unlit stuff
  111. bool bNewFlashlightPath = IsX360() || ( r_flashlight_version2.GetInt() != 0 );
  112. if ( bDrawStandardPass && ( pShaderShadow == NULL ) && ( pShaderAPI != NULL ) &&
  113. !bNewFlashlightPath && ( pShaderAPI->InFlashlightMode() ) ) // not snapshotting && flashlight pass)
  114. {
  115. bDrawStandardPass = false;
  116. }
  117. // Standard rendering pass
  118. if ( bDrawStandardPass )
  119. {
  120. BlendType_t nBlendType = EvaluateBlendRequirements( BASETEXTURE, true );
  121. bool bFullyOpaque = (nBlendType != BT_BLENDADD) && (nBlendType != BT_BLEND) && !IS_FLAG_SET(MATERIAL_VAR_ALPHATEST); //dest alpha is free for special use
  122. SHADOW_STATE
  123. {
  124. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  125. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  126. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  127. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, true );
  128. s_pShaderShadow->EnableSRGBWrite( true );
  129. // Either we've got a constant modulation
  130. bool isTranslucent = IsAlphaModulating();
  131. // Or we've got a texture alpha on either texture
  132. isTranslucent = isTranslucent || TextureIsTranslucent( BASETEXTURE, true ) ||
  133. TextureIsTranslucent( TEXTURE2, true );
  134. if ( isTranslucent )
  135. {
  136. if ( IS_FLAG_SET(MATERIAL_VAR_ADDITIVE) )
  137. {
  138. EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE );
  139. }
  140. else
  141. {
  142. EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  143. }
  144. }
  145. else
  146. {
  147. if ( IS_FLAG_SET(MATERIAL_VAR_ADDITIVE) )
  148. {
  149. EnableAlphaBlending( SHADER_BLEND_ONE, SHADER_BLEND_ONE );
  150. }
  151. else
  152. {
  153. DisableAlphaBlending( );
  154. }
  155. }
  156. // Set stream format (note that this shader supports compression)
  157. unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL | VERTEX_FORMAT_COMPRESSED;
  158. int nTexCoordCount = 1;
  159. int userDataSize = 0;
  160. if (IS_FLAG_SET( MATERIAL_VAR_VERTEXCOLOR ))
  161. {
  162. flags |= VERTEX_COLOR;
  163. }
  164. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, NULL, userDataSize );
  165. DECLARE_STATIC_VERTEX_SHADER( unlittwotexture_vs20 );
  166. SET_STATIC_VERTEX_SHADER( unlittwotexture_vs20 );
  167. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  168. {
  169. DECLARE_STATIC_PIXEL_SHADER( unlittwotexture_ps20b );
  170. SET_STATIC_PIXEL_SHADER( unlittwotexture_ps20b );
  171. }
  172. else
  173. {
  174. DECLARE_STATIC_PIXEL_SHADER( unlittwotexture_ps20 );
  175. SET_STATIC_PIXEL_SHADER( unlittwotexture_ps20 );
  176. }
  177. DefaultFog();
  178. pShaderShadow->EnableAlphaWrites( bFullyOpaque );
  179. }
  180. DYNAMIC_STATE
  181. {
  182. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  183. BindTexture( SHADER_SAMPLER1, TEXTURE2, FRAME2 );
  184. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, BASETEXTURETRANSFORM );
  185. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_2, TEXTURE2TRANSFORM );
  186. SetModulationPixelShaderDynamicState_LinearColorSpace( 1 );
  187. pShaderAPI->SetPixelShaderFogParams( PSREG_FOG_PARAMS );
  188. float vEyePos_SpecExponent[4];
  189. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos_SpecExponent );
  190. vEyePos_SpecExponent[3] = 0.0f;
  191. pShaderAPI->SetPixelShaderConstant( PSREG_EYEPOS_SPEC_EXPONENT, vEyePos_SpecExponent, 1 );
  192. MaterialFogMode_t fogType = pShaderAPI->GetSceneFogMode();
  193. int fogIndex = ( fogType == MATERIAL_FOG_LINEAR_BELOW_FOG_Z ) ? 1 : 0;
  194. int numBones = pShaderAPI->GetCurrentNumBones();
  195. DECLARE_DYNAMIC_VERTEX_SHADER( unlittwotexture_vs20 );
  196. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, numBones > 0 );
  197. SET_DYNAMIC_VERTEX_SHADER_COMBO( DOWATERFOG, fogIndex );
  198. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  199. SET_DYNAMIC_VERTEX_SHADER( unlittwotexture_vs20 );
  200. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  201. {
  202. DECLARE_DYNAMIC_PIXEL_SHADER( unlittwotexture_ps20b );
  203. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  204. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITE_DEPTH_TO_DESTALPHA, bFullyOpaque && pShaderAPI->ShouldWriteDepthToDestAlpha() );
  205. SET_DYNAMIC_PIXEL_SHADER_COMBO( LIGHTING_PREVIEW,
  206. pShaderAPI->GetIntRenderingParameter(INT_RENDERPARM_ENABLE_FIXED_LIGHTING) );
  207. SET_DYNAMIC_PIXEL_SHADER( unlittwotexture_ps20b );
  208. }
  209. else
  210. {
  211. DECLARE_DYNAMIC_PIXEL_SHADER( unlittwotexture_ps20 );
  212. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  213. SET_DYNAMIC_PIXEL_SHADER_COMBO( LIGHTING_PREVIEW,
  214. pShaderAPI->GetIntRenderingParameter(INT_RENDERPARM_ENABLE_FIXED_LIGHTING) );
  215. SET_DYNAMIC_PIXEL_SHADER( unlittwotexture_ps20 );
  216. }
  217. }
  218. Draw();
  219. }
  220. else
  221. {
  222. // Skip this pass!
  223. Draw( false );
  224. }
  225. // Cloak Pass
  226. if ( params[CLOAKPASSENABLED]->GetIntValue() )
  227. {
  228. // If ( snapshotting ) or ( we need to draw this frame )
  229. if ( ( pShaderShadow != NULL ) || ( ( params[CLOAKFACTOR]->GetFloatValue() > 0.0f ) && ( params[CLOAKFACTOR]->GetFloatValue() < 1.0f ) ) )
  230. {
  231. CloakBlendedPassVars_t info;
  232. SetupVarsCloakBlendedPass( info );
  233. DrawCloakBlendedPass( this, params, pShaderAPI, pShaderShadow, info, vertexCompression );
  234. }
  235. else // We're not snapshotting and we don't need to draw this frame
  236. {
  237. // Skip this pass!
  238. Draw( false );
  239. }
  240. }
  241. }
  242. END_SHADER