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.

341 lines
13 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #include "BaseVSShader.h"
  9. #include "example_model_dx9_helper.h"
  10. #include "convar.h"
  11. #include "cpp_shader_constant_register_map.h"
  12. #include "example_model_vs20.inc"
  13. #include "example_model_ps20b.inc"
  14. #include "commandbuilder.h"
  15. // memdbgon must be the last include file in a .cpp file!!!
  16. #include "tier0/memdbgon.h"
  17. static ConVar mat_fullbright( "mat_fullbright", "0", FCVAR_CHEAT );
  18. static ConVar r_lightwarpidentity( "r_lightwarpidentity", "0", FCVAR_CHEAT );
  19. static ConVar r_rimlight( "r_rimlight", "1", FCVAR_NONE );
  20. // Textures may be bound to the following samplers:
  21. // SHADER_SAMPLER0 Base (Albedo) / Gloss in alpha
  22. // SHADER_SAMPLER4 Flashlight Shadow Depth Map
  23. // SHADER_SAMPLER5 Normalization cube map
  24. // SHADER_SAMPLER6 Flashlight Cookie
  25. //-----------------------------------------------------------------------------
  26. // Initialize shader parameters
  27. //-----------------------------------------------------------------------------
  28. void InitParamsExampleModel_DX9( CBaseVSShader *pShader, IMaterialVar** params, const char *pMaterialName, ExampleModel_DX9_Vars_t &info )
  29. {
  30. // FLASHLIGHTFIXME: Do ShaderAPI::BindFlashlightTexture
  31. Assert( info.m_nFlashlightTexture >= 0 );
  32. if ( g_pHardwareConfig->SupportsBorderColor() )
  33. {
  34. params[FLASHLIGHTTEXTURE]->SetStringValue( "effects/flashlight_border" );
  35. }
  36. else
  37. {
  38. params[FLASHLIGHTTEXTURE]->SetStringValue( "effects/flashlight001" );
  39. }
  40. // This shader can be used with hw skinning
  41. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  42. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_VERTEX_LIT );
  43. }
  44. //-----------------------------------------------------------------------------
  45. // Initialize shader
  46. //-----------------------------------------------------------------------------
  47. void InitExampleModel_DX9( CBaseVSShader *pShader, IMaterialVar** params, ExampleModel_DX9_Vars_t &info )
  48. {
  49. Assert( info.m_nFlashlightTexture >= 0 );
  50. pShader->LoadTexture( info.m_nFlashlightTexture, TEXTUREFLAGS_SRGB );
  51. bool bIsBaseTextureTranslucent = false;
  52. if ( params[info.m_nBaseTexture]->IsDefined() )
  53. {
  54. pShader->LoadTexture( info.m_nBaseTexture, TEXTUREFLAGS_SRGB );
  55. if ( params[info.m_nBaseTexture]->GetTextureValue()->IsTranslucent() )
  56. {
  57. bIsBaseTextureTranslucent = true;
  58. }
  59. }
  60. }
  61. class CExampleModel_DX9_Context : public CBasePerMaterialContextData
  62. {
  63. public:
  64. CCommandBufferBuilder< CFixedCommandStorageBuffer< 800 > > m_SemiStaticCmdsOut;
  65. bool m_bFastPath;
  66. };
  67. //-----------------------------------------------------------------------------
  68. // Draws the shader
  69. //-----------------------------------------------------------------------------
  70. void DrawExampleModel_DX9_Internal( CBaseVSShader *pShader, IMaterialVar** params, IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow,
  71. bool bHasFlashlight, ExampleModel_DX9_Vars_t &info, VertexCompressionType_t vertexCompression,
  72. CBasePerMaterialContextData **pContextDataPtr )
  73. {
  74. bool bHasBaseTexture = (info.m_nBaseTexture != -1) && params[info.m_nBaseTexture]->IsTexture();
  75. bool bIsAlphaTested = IS_FLAG_SET( MATERIAL_VAR_ALPHATEST ) != 0;
  76. BlendType_t nBlendType= pShader->EvaluateBlendRequirements( info.m_nBaseTexture, true );
  77. bool bFullyOpaque = ( nBlendType != BT_BLENDADD ) && ( nBlendType != BT_BLEND ) && !bIsAlphaTested && !bHasFlashlight;
  78. CExampleModel_DX9_Context *pContextData = reinterpret_cast< CExampleModel_DX9_Context *> ( *pContextDataPtr );
  79. if ( !pContextData )
  80. {
  81. pContextData = new CExampleModel_DX9_Context;
  82. *pContextDataPtr = pContextData;
  83. }
  84. if( pShader->IsSnapshotting() )
  85. {
  86. pShaderShadow->EnableAlphaTest( bIsAlphaTested );
  87. if( info.m_nAlphaTestReference != -1 && params[info.m_nAlphaTestReference]->GetFloatValue() > 0.0f )
  88. {
  89. pShaderShadow->AlphaFunc( SHADER_ALPHAFUNC_GEQUAL, params[info.m_nAlphaTestReference]->GetFloatValue() );
  90. }
  91. int nShadowFilterMode = 0;
  92. if( bHasFlashlight )
  93. {
  94. if (params[info.m_nBaseTexture]->IsTexture())
  95. {
  96. pShader->SetAdditiveBlendingShadowState( info.m_nBaseTexture, true );
  97. }
  98. if( bIsAlphaTested )
  99. {
  100. // disable alpha test and use the zfunc zequals since alpha isn't guaranteed to
  101. // be the same on both the regular pass and the flashlight pass.
  102. pShaderShadow->EnableAlphaTest( false );
  103. pShaderShadow->DepthFunc( SHADER_DEPTHFUNC_EQUAL );
  104. }
  105. pShaderShadow->EnableBlending( true );
  106. pShaderShadow->EnableDepthWrites( false );
  107. // Be sure not to write to dest alpha
  108. pShaderShadow->EnableAlphaWrites( false );
  109. nShadowFilterMode = g_pHardwareConfig->GetShadowFilterMode(); // Based upon vendor and device dependent formats
  110. }
  111. else // not flashlight pass
  112. {
  113. if (params[info.m_nBaseTexture]->IsTexture())
  114. {
  115. pShader->SetDefaultBlendingShadowState( info.m_nBaseTexture, true );
  116. }
  117. }
  118. unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL;
  119. int userDataSize = 0;
  120. // Always enable...will bind white if nothing specified...
  121. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true ); // Base (albedo) map
  122. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  123. if( bHasFlashlight )
  124. {
  125. pShaderShadow->EnableTexture( SHADER_SAMPLER4, true ); // Shadow depth map
  126. pShaderShadow->SetShadowDepthFiltering( SHADER_SAMPLER4 );
  127. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER4, false );
  128. pShaderShadow->EnableTexture( SHADER_SAMPLER5, true ); // Noise map
  129. pShaderShadow->EnableTexture( SHADER_SAMPLER6, true ); // Flashlight cookie
  130. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER6, true );
  131. userDataSize = 4; // tangent S
  132. }
  133. // Always enable, since flat normal will be bound
  134. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true ); // Normal map
  135. userDataSize = 4; // tangent S
  136. pShaderShadow->EnableTexture( SHADER_SAMPLER5, true ); // Normalizing cube map
  137. pShaderShadow->EnableSRGBWrite( true );
  138. // texcoord0 : base texcoord, texcoord2 : decal hw morph delta
  139. int pTexCoordDim[3] = { 2, 0, 3 };
  140. int nTexCoordCount = 1;
  141. // This shader supports compressed vertices, so OR in that flag:
  142. flags |= VERTEX_FORMAT_COMPRESSED;
  143. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, pTexCoordDim, userDataSize );
  144. DECLARE_STATIC_VERTEX_SHADER( example_model_vs20 );
  145. SET_STATIC_VERTEX_SHADER( example_model_vs20 );
  146. // Assume we're only going to get in here if we support 2b
  147. DECLARE_STATIC_PIXEL_SHADER( example_model_ps20b );
  148. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHT, bHasFlashlight );
  149. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHTDEPTHFILTERMODE, nShadowFilterMode );
  150. SET_STATIC_PIXEL_SHADER_COMBO( CONVERT_TO_SRGB, 0 );
  151. SET_STATIC_PIXEL_SHADER( example_model_ps20b );
  152. if( bHasFlashlight )
  153. {
  154. pShader->FogToBlack();
  155. }
  156. else
  157. {
  158. pShader->DefaultFog();
  159. }
  160. // HACK HACK HACK - enable alpha writes all the time so that we have them for underwater stuff
  161. pShaderShadow->EnableAlphaWrites( bFullyOpaque );
  162. }
  163. else // not snapshotting -- begin dynamic state
  164. {
  165. bool bLightingOnly = mat_fullbright.GetInt() == 2 && !IS_FLAG_SET( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  166. if( bHasBaseTexture )
  167. {
  168. pShader->BindTexture( SHADER_SAMPLER0, info.m_nBaseTexture, info.m_nBaseTextureFrame );
  169. }
  170. else
  171. {
  172. pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, TEXTURE_WHITE );
  173. }
  174. LightState_t lightState = { 0, false, false };
  175. bool bFlashlightShadows = false;
  176. if( bHasFlashlight )
  177. {
  178. Assert( info.m_nFlashlightTexture >= 0 && info.m_nFlashlightTextureFrame >= 0 );
  179. pShader->BindTexture( SHADER_SAMPLER6, info.m_nFlashlightTexture, info.m_nFlashlightTextureFrame );
  180. VMatrix worldToTexture;
  181. ITexture *pFlashlightDepthTexture;
  182. FlashlightState_t state = pShaderAPI->GetFlashlightStateEx( worldToTexture, &pFlashlightDepthTexture );
  183. bFlashlightShadows = state.m_bEnableShadows && ( pFlashlightDepthTexture != NULL );
  184. SetFlashLightColorFromState( state, pShaderAPI, PSREG_FLASHLIGHT_COLOR );
  185. if( pFlashlightDepthTexture && g_pConfig->ShadowDepthTexture() && state.m_bEnableShadows )
  186. {
  187. pShader->BindTexture( SHADER_SAMPLER4, pFlashlightDepthTexture, 0 );
  188. pShaderAPI->BindStandardTexture( SHADER_SAMPLER5, TEXTURE_SHADOW_NOISE_2D );
  189. }
  190. }
  191. else // no flashlight
  192. {
  193. pShaderAPI->GetDX9LightState( &lightState );
  194. }
  195. MaterialFogMode_t fogType = pShaderAPI->GetSceneFogMode();
  196. int fogIndex = ( fogType == MATERIAL_FOG_LINEAR_BELOW_FOG_Z ) ? 1 : 0;
  197. int numBones = pShaderAPI->GetCurrentNumBones();
  198. bool bWriteDepthToAlpha = false;
  199. bool bWriteWaterFogToAlpha = false;
  200. if( bFullyOpaque )
  201. {
  202. bWriteDepthToAlpha = pShaderAPI->ShouldWriteDepthToDestAlpha();
  203. bWriteWaterFogToAlpha = (fogType == MATERIAL_FOG_LINEAR_BELOW_FOG_Z);
  204. AssertMsg( !(bWriteDepthToAlpha && bWriteWaterFogToAlpha), "Can't write two values to alpha at the same time." );
  205. }
  206. DECLARE_DYNAMIC_VERTEX_SHADER( example_model_vs20 );
  207. SET_DYNAMIC_VERTEX_SHADER_COMBO( DOWATERFOG, fogIndex );
  208. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, numBones > 0 );
  209. SET_DYNAMIC_VERTEX_SHADER_COMBO( LIGHTING_PREVIEW, pShaderAPI->GetIntRenderingParameter(INT_RENDERPARM_ENABLE_FIXED_LIGHTING)!=0);
  210. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  211. SET_DYNAMIC_VERTEX_SHADER_COMBO( NUM_LIGHTS, lightState.m_nNumLights );
  212. SET_DYNAMIC_VERTEX_SHADER( example_model_vs20 );
  213. DECLARE_DYNAMIC_PIXEL_SHADER( example_model_ps20b );
  214. SET_DYNAMIC_PIXEL_SHADER_COMBO( NUM_LIGHTS, lightState.m_nNumLights );
  215. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITEWATERFOGTODESTALPHA, bWriteWaterFogToAlpha );
  216. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITE_DEPTH_TO_DESTALPHA, bWriteDepthToAlpha );
  217. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  218. SET_DYNAMIC_PIXEL_SHADER_COMBO( FLASHLIGHTSHADOWS, bFlashlightShadows );
  219. SET_DYNAMIC_PIXEL_SHADER( example_model_ps20b );
  220. pShader->SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, info.m_nBaseTextureTransform );
  221. pShader->SetModulationPixelShaderDynamicState_LinearColorSpace( 1 );
  222. pShader->SetAmbientCubeDynamicStateVertexShader();
  223. if( !bHasFlashlight )
  224. {
  225. pShaderAPI->BindStandardTexture( SHADER_SAMPLER5, TEXTURE_NORMALIZATION_CUBEMAP_SIGNED );
  226. }
  227. pShaderAPI->SetPixelShaderStateAmbientLightCube( PSREG_AMBIENT_CUBE, !lightState.m_bAmbientLight ); // Force to black if not bAmbientLight
  228. pShaderAPI->CommitPixelShaderLighting( PSREG_LIGHT_INFO_ARRAY );
  229. // handle mat_fullbright 2 (diffuse lighting only)
  230. if( bLightingOnly )
  231. {
  232. pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, TEXTURE_GREY );
  233. }
  234. pShaderAPI->SetPixelShaderFogParams( PSREG_FOG_PARAMS );
  235. if( bHasFlashlight )
  236. {
  237. VMatrix worldToTexture;
  238. float atten[4], pos[4], tweaks[4];
  239. const FlashlightState_t &flashlightState = pShaderAPI->GetFlashlightState( worldToTexture );
  240. SetFlashLightColorFromState( flashlightState, pShaderAPI, PSREG_FLASHLIGHT_COLOR );
  241. pShader->BindTexture( SHADER_SAMPLER6, flashlightState.m_pSpotlightTexture, flashlightState.m_nSpotlightTextureFrame );
  242. atten[0] = flashlightState.m_fConstantAtten; // Set the flashlight attenuation factors
  243. atten[1] = flashlightState.m_fLinearAtten;
  244. atten[2] = flashlightState.m_fQuadraticAtten;
  245. atten[3] = flashlightState.m_FarZ;
  246. pShaderAPI->SetPixelShaderConstant( PSREG_FLASHLIGHT_ATTENUATION, atten, 1 );
  247. pos[0] = flashlightState.m_vecLightOrigin[0]; // Set the flashlight origin
  248. pos[1] = flashlightState.m_vecLightOrigin[1];
  249. pos[2] = flashlightState.m_vecLightOrigin[2];
  250. pShaderAPI->SetPixelShaderConstant( PSREG_FLASHLIGHT_POSITION_RIM_BOOST, pos, 1 );
  251. pShaderAPI->SetPixelShaderConstant( PSREG_FLASHLIGHT_TO_WORLD_TEXTURE, worldToTexture.Base(), 4 );
  252. // Tweaks associated with a given flashlight
  253. tweaks[0] = ShadowFilterFromState( flashlightState );
  254. tweaks[1] = ShadowAttenFromState( flashlightState );
  255. pShader->HashShadow2DJitter( flashlightState.m_flShadowJitterSeed, &tweaks[2], &tweaks[3] );
  256. pShaderAPI->SetPixelShaderConstant( PSREG_ENVMAP_TINT__SHADOW_TWEAKS, tweaks, 1 );
  257. // Dimensions of screen, used for screen-space noise map sampling
  258. float vScreenScale[4] = {1280.0f / 32.0f, 720.0f / 32.0f, 0, 0};
  259. int nWidth, nHeight;
  260. pShaderAPI->GetBackBufferDimensions( nWidth, nHeight );
  261. vScreenScale[0] = (float) nWidth / 32.0f;
  262. vScreenScale[1] = (float) nHeight / 32.0f;
  263. pShaderAPI->SetPixelShaderConstant( PSREG_FLASHLIGHT_SCREEN_SCALE, vScreenScale, 1 );
  264. }
  265. }
  266. pShader->Draw();
  267. }
  268. //-----------------------------------------------------------------------------
  269. // Draws the shader
  270. //-----------------------------------------------------------------------------
  271. void DrawExampleModel_DX9( CBaseVSShader *pShader, IMaterialVar** params, IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow,
  272. ExampleModel_DX9_Vars_t &info, VertexCompressionType_t vertexCompression, CBasePerMaterialContextData **pContextDataPtr )
  273. {
  274. bool bHasFlashlight = pShader->UsingFlashlight( params );
  275. if ( bHasFlashlight )
  276. {
  277. DrawExampleModel_DX9_Internal( pShader, params, pShaderAPI, pShaderShadow, false, info, vertexCompression, pContextDataPtr++ );
  278. if ( pShaderShadow )
  279. {
  280. pShader->SetInitialShadowState( );
  281. }
  282. }
  283. DrawExampleModel_DX9_Internal( pShader, params, pShaderAPI, pShaderShadow, bHasFlashlight, info, vertexCompression, pContextDataPtr );
  284. }