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.

490 lines
15 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "BaseVSShader.h"
  9. #include <string.h>
  10. #include "const.h"
  11. #include "cpp_shader_constant_register_map.h"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include "tier0/memdbgon.h"
  14. #include "sprite_vs20.inc"
  15. #include "sprite_ps20.inc"
  16. #include "sprite_ps20b.inc"
  17. // WARNING! Change these in engine/SpriteGn.h if you change them here!
  18. #define SPR_VP_PARALLEL_UPRIGHT 0
  19. #define SPR_FACING_UPRIGHT 1
  20. #define SPR_VP_PARALLEL 2
  21. #define SPR_ORIENTED 3
  22. #define SPR_VP_PARALLEL_ORIENTED 4
  23. DEFINE_FALLBACK_SHADER( Sprite, Sprite_DX9 )
  24. BEGIN_VS_SHADER( Sprite_DX9,
  25. "Help for Sprite_DX9" )
  26. BEGIN_SHADER_PARAMS
  27. SHADER_PARAM( SPRITEORIGIN, SHADER_PARAM_TYPE_VEC3, "[0 0 0]", "sprite origin" )
  28. SHADER_PARAM( SPRITEORIENTATION, SHADER_PARAM_TYPE_INTEGER, "0", "sprite orientation" )
  29. SHADER_PARAM( SPRITERENDERMODE, SHADER_PARAM_TYPE_INTEGER, "0", "sprite rendermode" )
  30. SHADER_PARAM( IGNOREVERTEXCOLORS, SHADER_PARAM_TYPE_BOOL, "1", "ignore vertex colors" )
  31. SHADER_PARAM( NOSRGB, SHADER_PARAM_TYPE_BOOL, "0", "do not operate in srgb space" )
  32. SHADER_PARAM( HDRCOLORSCALE, SHADER_PARAM_TYPE_FLOAT, "1.0", "hdr color scale" )
  33. END_SHADER_PARAMS
  34. SHADER_FALLBACK
  35. {
  36. if (g_pHardwareConfig->GetDXSupportLevel() < 90)
  37. return "Sprite_DX8";
  38. return 0;
  39. }
  40. SHADER_INIT_PARAMS()
  41. {
  42. // FIXME: This can share code with sprite.cpp
  43. if (!params[ALPHA]->IsDefined())
  44. {
  45. params[ALPHA]->SetFloatValue( 1.0f );
  46. }
  47. if (!params[HDRCOLORSCALE]->IsDefined())
  48. {
  49. params[HDRCOLORSCALE]->SetFloatValue( 1.0f );
  50. }
  51. if ( !params[NOSRGB]->IsDefined() )
  52. {
  53. // Disable sRGB reads and writes by default
  54. params[NOSRGB]->SetIntValue( 1 );
  55. }
  56. SET_FLAGS( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  57. SET_FLAGS( MATERIAL_VAR_VERTEXCOLOR );
  58. SET_FLAGS( MATERIAL_VAR_VERTEXALPHA );
  59. // translate from a string orientation to an enumeration
  60. if (params[SPRITEORIENTATION]->IsDefined())
  61. {
  62. const char *orientationString = params[SPRITEORIENTATION]->GetStringValue();
  63. if( stricmp( orientationString, "parallel_upright" ) == 0 )
  64. {
  65. params[SPRITEORIENTATION]->SetIntValue( SPR_VP_PARALLEL_UPRIGHT );
  66. }
  67. else if( stricmp( orientationString, "facing_upright" ) == 0 )
  68. {
  69. params[SPRITEORIENTATION]->SetIntValue( SPR_FACING_UPRIGHT );
  70. }
  71. else if( stricmp( orientationString, "vp_parallel" ) == 0 )
  72. {
  73. params[SPRITEORIENTATION]->SetIntValue( SPR_VP_PARALLEL );
  74. }
  75. else if( stricmp( orientationString, "oriented" ) == 0 )
  76. {
  77. params[SPRITEORIENTATION]->SetIntValue( SPR_ORIENTED );
  78. }
  79. else if( stricmp( orientationString, "vp_parallel_oriented" ) == 0 )
  80. {
  81. params[SPRITEORIENTATION]->SetIntValue( SPR_VP_PARALLEL_ORIENTED );
  82. }
  83. else
  84. {
  85. Warning( "error with $spriteOrientation\n" );
  86. params[SPRITEORIENTATION]->SetIntValue( SPR_VP_PARALLEL_UPRIGHT );
  87. }
  88. }
  89. else
  90. {
  91. // default case
  92. params[SPRITEORIENTATION]->SetIntValue( SPR_VP_PARALLEL_UPRIGHT );
  93. }
  94. }
  95. SHADER_INIT
  96. {
  97. bool bSRGB = s_ppParams[NOSRGB]->GetIntValue() == 0;
  98. LoadTexture( BASETEXTURE, bSRGB ? TEXTUREFLAGS_SRGB : 0 );
  99. }
  100. #define SHADER_USE_VERTEX_COLOR 1
  101. #define SHADER_USE_CONSTANT_COLOR 2
  102. void SetSpriteCommonShadowState( unsigned int shaderFlags )
  103. {
  104. IShaderShadow *pShaderShadow = s_pShaderShadow;
  105. s_pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  106. bool bSRGB = s_ppParams[NOSRGB]->GetIntValue() == 0;
  107. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, bSRGB );
  108. // Only enabling this on OSX() - it causes GL mode's light glow sprites to be much darker vs. D3D9 under Linux/Win GL.
  109. bool bSRGBOutputAdapter = ( IsOSX() && !g_pHardwareConfig->FakeSRGBWrite() ) && !bSRGB;
  110. unsigned int flags = VERTEX_POSITION;
  111. if( shaderFlags & SHADER_USE_VERTEX_COLOR )
  112. {
  113. flags |= VERTEX_COLOR;
  114. }
  115. int numTexCoords = 1;
  116. s_pShaderShadow->VertexShaderVertexFormat( flags, numTexCoords, 0, 0 );
  117. DECLARE_STATIC_VERTEX_SHADER( sprite_vs20 );
  118. SET_STATIC_VERTEX_SHADER_COMBO( VERTEXCOLOR, ( shaderFlags & SHADER_USE_VERTEX_COLOR ) ? true : false );
  119. SET_STATIC_VERTEX_SHADER_COMBO( SRGB, bSRGB );
  120. SET_STATIC_VERTEX_SHADER( sprite_vs20 );
  121. if( g_pHardwareConfig->SupportsPixelShaders_2_b() || g_pHardwareConfig->ShouldAlwaysUseShaderModel2bShaders() ) // Always send GL down this path
  122. {
  123. DECLARE_STATIC_PIXEL_SHADER( sprite_ps20b );
  124. SET_STATIC_PIXEL_SHADER_COMBO( VERTEXCOLOR, ( shaderFlags & SHADER_USE_VERTEX_COLOR ) ? true : false );
  125. SET_STATIC_PIXEL_SHADER_COMBO( CONSTANTCOLOR, ( shaderFlags & SHADER_USE_CONSTANT_COLOR ) ? true : false );
  126. SET_STATIC_PIXEL_SHADER_COMBO( HDRTYPE, g_pHardwareConfig->GetHDRType() );
  127. SET_STATIC_PIXEL_SHADER_COMBO( SRGB, bSRGB );
  128. SET_STATIC_PIXEL_SHADER_COMBO( SRGB_OUTPUT_ADAPTER, bSRGBOutputAdapter );
  129. SET_STATIC_PIXEL_SHADER( sprite_ps20b );
  130. }
  131. else
  132. {
  133. DECLARE_STATIC_PIXEL_SHADER( sprite_ps20 );
  134. SET_STATIC_PIXEL_SHADER_COMBO( VERTEXCOLOR, ( shaderFlags & SHADER_USE_VERTEX_COLOR ) ? true : false );
  135. SET_STATIC_PIXEL_SHADER_COMBO( CONSTANTCOLOR, ( shaderFlags & SHADER_USE_CONSTANT_COLOR ) ? true : false );
  136. SET_STATIC_PIXEL_SHADER_COMBO( HDRTYPE, g_pHardwareConfig->GetHDRType() );
  137. SET_STATIC_PIXEL_SHADER_COMBO( SRGB, bSRGB );
  138. SET_STATIC_PIXEL_SHADER( sprite_ps20 );
  139. }
  140. // OSX always has to sRGB write (don't do this on Linux/Win GL - it causes glow sprites to be way too dark)
  141. s_pShaderShadow->EnableSRGBWrite( bSRGB || ( IsOSX() && !g_pHardwareConfig->FakeSRGBWrite() ) );
  142. }
  143. void SetSpriteCommonDynamicState( unsigned int shaderFlags )
  144. {
  145. IShaderDynamicAPI *pShaderAPI = s_pShaderAPI;
  146. bool bSRGB = s_ppParams[NOSRGB]->GetIntValue() == 0;
  147. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  148. MaterialFogMode_t fogType = s_pShaderAPI->GetSceneFogMode();
  149. int fogIndex = ( fogType == MATERIAL_FOG_LINEAR_BELOW_FOG_Z ) ? 1 : 0;
  150. DECLARE_DYNAMIC_VERTEX_SHADER( sprite_vs20 );
  151. SET_DYNAMIC_VERTEX_SHADER_COMBO( DOWATERFOG, fogIndex );
  152. SET_DYNAMIC_VERTEX_SHADER( sprite_vs20 );
  153. if( g_pHardwareConfig->SupportsPixelShaders_2_b() || g_pHardwareConfig->ShouldAlwaysUseShaderModel2bShaders() ) // Always send GL down this path
  154. {
  155. DECLARE_DYNAMIC_PIXEL_SHADER( sprite_ps20b );
  156. SET_DYNAMIC_PIXEL_SHADER_COMBO( HDRENABLED, IsHDREnabled() );
  157. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  158. SET_DYNAMIC_PIXEL_SHADER( sprite_ps20b );
  159. }
  160. else
  161. {
  162. DECLARE_DYNAMIC_PIXEL_SHADER( sprite_ps20 );
  163. SET_DYNAMIC_PIXEL_SHADER_COMBO( HDRENABLED, IsHDREnabled() );
  164. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  165. SET_DYNAMIC_PIXEL_SHADER( sprite_ps20 );
  166. }
  167. pShaderAPI->SetPixelShaderFogParams( PSREG_FOG_PARAMS );
  168. float vEyePos_SpecExponent[4];
  169. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos_SpecExponent );
  170. vEyePos_SpecExponent[3] = 0.0f;
  171. pShaderAPI->SetPixelShaderConstant( PSREG_EYEPOS_SPEC_EXPONENT, vEyePos_SpecExponent, 1 );
  172. if( shaderFlags & SHADER_USE_CONSTANT_COLOR )
  173. {
  174. if ( bSRGB )
  175. SetPixelShaderConstantGammaToLinear( 0, COLOR, ALPHA );
  176. else
  177. SetPixelShaderConstant( 0, COLOR, ALPHA );
  178. }
  179. if( IsHDREnabled() )
  180. {
  181. if ( bSRGB )
  182. SetPixelShaderConstantGammaToLinear( 1, HDRCOLORSCALE );
  183. else
  184. SetPixelShaderConstant( 1, HDRCOLORSCALE );
  185. }
  186. }
  187. SHADER_DRAW
  188. {
  189. bool bSRGB = params[NOSRGB]->GetIntValue() == 0;
  190. SHADOW_STATE
  191. {
  192. pShaderShadow->EnableCulling( false );
  193. }
  194. switch( params[SPRITERENDERMODE]->GetIntValue() )
  195. {
  196. case kRenderNormal:
  197. SHADOW_STATE
  198. {
  199. FogToFogColor();
  200. SetSpriteCommonShadowState( 0 );
  201. }
  202. DYNAMIC_STATE
  203. {
  204. SetSpriteCommonDynamicState( 0 );
  205. }
  206. Draw();
  207. break;
  208. case kRenderTransColor:
  209. case kRenderTransTexture:
  210. SHADOW_STATE
  211. {
  212. pShaderShadow->EnableDepthWrites( false );
  213. pShaderShadow->EnableBlending( true );
  214. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  215. FogToFogColor();
  216. SetSpriteCommonShadowState( SHADER_USE_VERTEX_COLOR );
  217. }
  218. DYNAMIC_STATE
  219. {
  220. SetSpriteCommonDynamicState( SHADER_USE_VERTEX_COLOR );
  221. }
  222. Draw();
  223. break;
  224. case kRenderGlow:
  225. case kRenderWorldGlow:
  226. SHADOW_STATE
  227. {
  228. pShaderShadow->EnableDepthWrites( false );
  229. pShaderShadow->EnableDepthTest( false );
  230. pShaderShadow->EnableBlending( true );
  231. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE );
  232. FogToBlack();
  233. SetSpriteCommonShadowState( SHADER_USE_VERTEX_COLOR );
  234. }
  235. DYNAMIC_STATE
  236. {
  237. SetSpriteCommonDynamicState( SHADER_USE_VERTEX_COLOR );
  238. }
  239. Draw();
  240. break;
  241. case kRenderTransAlpha:
  242. // untested cut and past from kRenderTransAlphaAdd . . same as first pass of that.
  243. SHADOW_STATE
  244. {
  245. pShaderShadow->EnableDepthWrites( false );
  246. pShaderShadow->EnableBlending( true );
  247. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  248. FogToFogColor();
  249. SetSpriteCommonShadowState( SHADER_USE_VERTEX_COLOR );
  250. }
  251. DYNAMIC_STATE
  252. {
  253. SetSpriteCommonDynamicState( SHADER_USE_VERTEX_COLOR );
  254. }
  255. Draw();
  256. break;
  257. case kRenderTransAlphaAdd:
  258. SHADOW_STATE
  259. {
  260. pShaderShadow->EnableDepthWrites( false );
  261. pShaderShadow->EnableBlending( true );
  262. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  263. FogToFogColor();
  264. SetSpriteCommonShadowState( SHADER_USE_VERTEX_COLOR );
  265. }
  266. DYNAMIC_STATE
  267. {
  268. SetSpriteCommonDynamicState( SHADER_USE_VERTEX_COLOR );
  269. }
  270. Draw();
  271. SHADOW_STATE
  272. {
  273. SetInitialShadowState();
  274. pShaderShadow->EnableDepthWrites( false );
  275. pShaderShadow->EnableBlending( true );
  276. pShaderShadow->BlendFunc( SHADER_BLEND_ONE_MINUS_SRC_ALPHA, SHADER_BLEND_ONE );
  277. FogToBlack();
  278. SetSpriteCommonShadowState( SHADER_USE_VERTEX_COLOR );
  279. }
  280. DYNAMIC_STATE
  281. {
  282. SetSpriteCommonDynamicState( SHADER_USE_VERTEX_COLOR );
  283. }
  284. Draw();
  285. break;
  286. case kRenderTransAdd:
  287. {
  288. unsigned int flags = SHADER_USE_CONSTANT_COLOR;
  289. if( !params[ IGNOREVERTEXCOLORS ]->GetIntValue() )
  290. {
  291. flags |= SHADER_USE_VERTEX_COLOR;
  292. }
  293. SHADOW_STATE
  294. {
  295. pShaderShadow->EnableDepthWrites( false );
  296. pShaderShadow->EnableBlending( true );
  297. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE );
  298. FogToBlack();
  299. SetSpriteCommonShadowState( flags );
  300. }
  301. DYNAMIC_STATE
  302. {
  303. SetSpriteCommonDynamicState( flags );
  304. }
  305. }
  306. Draw();
  307. break;
  308. case kRenderTransAddFrameBlend:
  309. {
  310. float flFrame = params[FRAME]->GetFloatValue();
  311. float flFade = params[ALPHA]->GetFloatValue();
  312. unsigned int flags = SHADER_USE_CONSTANT_COLOR;
  313. if( !params[ IGNOREVERTEXCOLORS ]->GetIntValue() )
  314. {
  315. flags |= SHADER_USE_VERTEX_COLOR;
  316. }
  317. SHADOW_STATE
  318. {
  319. pShaderShadow->EnableDepthWrites( false );
  320. pShaderShadow->EnableBlending( true );
  321. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE );
  322. FogToBlack();
  323. SetSpriteCommonShadowState( flags );
  324. }
  325. DYNAMIC_STATE
  326. {
  327. float frameBlendAlpha = 1.0f - ( flFrame - ( int )flFrame );
  328. ITexture *pTexture = params[BASETEXTURE]->GetTextureValue();
  329. BindTexture( SHADER_SAMPLER0, pTexture, ( int )flFrame );
  330. MaterialFogMode_t fogType = s_pShaderAPI->GetSceneFogMode();
  331. int fogIndex = ( fogType == MATERIAL_FOG_LINEAR_BELOW_FOG_Z ) ? 1 : 0;
  332. DECLARE_DYNAMIC_VERTEX_SHADER( sprite_vs20 );
  333. SET_DYNAMIC_VERTEX_SHADER_COMBO( DOWATERFOG, fogIndex );
  334. SET_DYNAMIC_VERTEX_SHADER( sprite_vs20 );
  335. if( g_pHardwareConfig->SupportsPixelShaders_2_b() || g_pHardwareConfig->ShouldAlwaysUseShaderModel2bShaders() ) // Always send GL down this path
  336. {
  337. DECLARE_DYNAMIC_PIXEL_SHADER( sprite_ps20b );
  338. SET_DYNAMIC_PIXEL_SHADER_COMBO( HDRENABLED, IsHDREnabled() );
  339. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  340. SET_DYNAMIC_PIXEL_SHADER( sprite_ps20b );
  341. }
  342. else
  343. {
  344. DECLARE_DYNAMIC_PIXEL_SHADER( sprite_ps20 );
  345. SET_DYNAMIC_PIXEL_SHADER_COMBO( HDRENABLED, IsHDREnabled() );
  346. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  347. SET_DYNAMIC_PIXEL_SHADER( sprite_ps20 );
  348. }
  349. pShaderAPI->SetPixelShaderFogParams( PSREG_FOG_PARAMS );
  350. float vEyePos_SpecExponent[4];
  351. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos_SpecExponent );
  352. vEyePos_SpecExponent[3] = 0.0f;
  353. pShaderAPI->SetPixelShaderConstant( PSREG_EYEPOS_SPEC_EXPONENT, vEyePos_SpecExponent, 1 );
  354. float color[4];
  355. if ( bSRGB )
  356. color[0] = color[1] = color[2] = GammaToLinear( flFade * frameBlendAlpha );
  357. else
  358. color[0] = color[1] = color[2] = flFade * frameBlendAlpha;
  359. color[3] = 1.0f;
  360. s_pShaderAPI->SetPixelShaderConstant( 0, color );
  361. if( IsHDREnabled() )
  362. {
  363. if ( bSRGB )
  364. SetPixelShaderConstantGammaToLinear( 1, HDRCOLORSCALE );
  365. else
  366. SetPixelShaderConstant( 1, HDRCOLORSCALE );
  367. }
  368. }
  369. Draw();
  370. SHADOW_STATE
  371. {
  372. FogToBlack();
  373. SetSpriteCommonShadowState( flags );
  374. }
  375. DYNAMIC_STATE
  376. {
  377. float frameBlendAlpha = ( flFrame - ( int )flFrame );
  378. ITexture *pTexture = params[BASETEXTURE]->GetTextureValue();
  379. int numAnimationFrames = pTexture->GetNumAnimationFrames();
  380. BindTexture( SHADER_SAMPLER0, pTexture, ( ( int )flFrame + 1 ) % numAnimationFrames );
  381. MaterialFogMode_t fogType = s_pShaderAPI->GetSceneFogMode();
  382. int fogIndex = ( fogType == MATERIAL_FOG_LINEAR_BELOW_FOG_Z ) ? 1 : 0;
  383. DECLARE_DYNAMIC_VERTEX_SHADER( sprite_vs20 );
  384. SET_DYNAMIC_VERTEX_SHADER_COMBO( DOWATERFOG, fogIndex );
  385. SET_DYNAMIC_VERTEX_SHADER( sprite_vs20 );
  386. if( g_pHardwareConfig->SupportsPixelShaders_2_b() || g_pHardwareConfig->ShouldAlwaysUseShaderModel2bShaders() ) // Always send GL down this path
  387. {
  388. DECLARE_DYNAMIC_PIXEL_SHADER( sprite_ps20b );
  389. SET_DYNAMIC_PIXEL_SHADER_COMBO( HDRENABLED, IsHDREnabled() );
  390. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  391. SET_DYNAMIC_PIXEL_SHADER( sprite_ps20b );
  392. }
  393. else
  394. {
  395. DECLARE_DYNAMIC_PIXEL_SHADER( sprite_ps20 );
  396. SET_DYNAMIC_PIXEL_SHADER_COMBO( HDRENABLED, IsHDREnabled() );
  397. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  398. SET_DYNAMIC_PIXEL_SHADER( sprite_ps20 );
  399. }
  400. pShaderAPI->SetPixelShaderFogParams( PSREG_FOG_PARAMS );
  401. float vEyePos_SpecExponent[4];
  402. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos_SpecExponent );
  403. vEyePos_SpecExponent[3] = 0.0f;
  404. pShaderAPI->SetPixelShaderConstant( PSREG_EYEPOS_SPEC_EXPONENT, vEyePos_SpecExponent, 1 );
  405. float color[4];
  406. if ( bSRGB )
  407. color[0] = color[1] = color[2] = GammaToLinear( flFade * frameBlendAlpha );
  408. else
  409. color[0] = color[1] = color[2] = flFade * frameBlendAlpha;
  410. color[3] = 1.0f;
  411. s_pShaderAPI->SetPixelShaderConstant( 0, color );
  412. if( IsHDREnabled() )
  413. {
  414. if ( bSRGB )
  415. SetPixelShaderConstantGammaToLinear( 1, HDRCOLORSCALE );
  416. else
  417. SetPixelShaderConstant( 1, HDRCOLORSCALE );
  418. }
  419. }
  420. Draw();
  421. }
  422. break;
  423. default:
  424. ShaderWarning( "shader Sprite: Unknown sprite render mode\n" );
  425. break;
  426. }
  427. }
  428. END_SHADER