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.

379 lines
11 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. // Implementation of the sprite shader
  8. //=============================================================================//
  9. #include "BaseVSShader.h"
  10. #include <string.h>
  11. #include "const.h"
  12. #include "sprite_vs11.inc"
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. // WARNING! Change these in engine/SpriteGn.h if you change them here!
  16. #define SPR_VP_PARALLEL_UPRIGHT 0
  17. #define SPR_FACING_UPRIGHT 1
  18. #define SPR_VP_PARALLEL 2
  19. #define SPR_ORIENTED 3
  20. #define SPR_VP_PARALLEL_ORIENTED 4
  21. DEFINE_FALLBACK_SHADER( Sprite, Sprite_DX8 )
  22. BEGIN_VS_SHADER( Sprite_DX8,
  23. "Help for Sprite_DX8" )
  24. BEGIN_SHADER_PARAMS
  25. SHADER_PARAM( SPRITEORIGIN, SHADER_PARAM_TYPE_VEC3, "[0 0 0]", "sprite origin" )
  26. SHADER_PARAM( SPRITEORIENTATION, SHADER_PARAM_TYPE_INTEGER, "0", "sprite orientation" )
  27. SHADER_PARAM( SPRITERENDERMODE, SHADER_PARAM_TYPE_INTEGER, "0", "sprite rendermode" )
  28. SHADER_PARAM( IGNOREVERTEXCOLORS, SHADER_PARAM_TYPE_BOOL, "1", "ignore vertex colors" )
  29. END_SHADER_PARAMS
  30. SHADER_FALLBACK
  31. {
  32. if ( IsPC() && g_pHardwareConfig->GetDXSupportLevel() < 80 )
  33. return "Sprite_DX6";
  34. return 0;
  35. }
  36. SHADER_INIT_PARAMS()
  37. {
  38. // FIXME: This can share code with sprite.cpp
  39. // FIXME: Not sure if this is the best solution, but it's a very]
  40. // easy one. When graphics aren't enabled, we oftentimes need to get
  41. // at the parameters of a shader. Therefore, we must set the default
  42. // values in a separate phase from when we load resources.
  43. if (!params[ALPHA]->IsDefined())
  44. params[ ALPHA ]->SetFloatValue( 1.0f );
  45. SET_FLAGS( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  46. SET_FLAGS( MATERIAL_VAR_VERTEXCOLOR );
  47. SET_FLAGS( MATERIAL_VAR_VERTEXALPHA );
  48. // translate from a string orientation to an enumeration
  49. if (params[SPRITEORIENTATION]->IsDefined())
  50. {
  51. const char *orientationString = params[SPRITEORIENTATION]->GetStringValue();
  52. if( stricmp( orientationString, "parallel_upright" ) == 0 )
  53. {
  54. params[SPRITEORIENTATION]->SetIntValue( SPR_VP_PARALLEL_UPRIGHT );
  55. }
  56. else if( stricmp( orientationString, "facing_upright" ) == 0 )
  57. {
  58. params[SPRITEORIENTATION]->SetIntValue( SPR_FACING_UPRIGHT );
  59. }
  60. else if( stricmp( orientationString, "vp_parallel" ) == 0 )
  61. {
  62. params[SPRITEORIENTATION]->SetIntValue( SPR_VP_PARALLEL );
  63. }
  64. else if( stricmp( orientationString, "oriented" ) == 0 )
  65. {
  66. params[SPRITEORIENTATION]->SetIntValue( SPR_ORIENTED );
  67. }
  68. else if( stricmp( orientationString, "vp_parallel_oriented" ) == 0 )
  69. {
  70. params[SPRITEORIENTATION]->SetIntValue( SPR_VP_PARALLEL_ORIENTED );
  71. }
  72. else
  73. {
  74. Warning( "error with $spriteOrientation\n" );
  75. params[SPRITEORIENTATION]->SetIntValue( SPR_VP_PARALLEL_UPRIGHT );
  76. }
  77. }
  78. else
  79. {
  80. // default case
  81. params[SPRITEORIENTATION]->SetIntValue( SPR_VP_PARALLEL_UPRIGHT );
  82. }
  83. }
  84. SHADER_INIT
  85. {
  86. LoadTexture( BASETEXTURE );
  87. }
  88. #define SHADER_USE_VERTEX_COLOR 1
  89. #define SHADER_USE_CONSTANT_COLOR 2
  90. void SetSpriteCommonShadowState( unsigned int shaderFlags )
  91. {
  92. s_pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  93. unsigned int flags = VERTEX_POSITION;
  94. if( shaderFlags & SHADER_USE_VERTEX_COLOR )
  95. {
  96. flags |= VERTEX_COLOR;
  97. }
  98. s_pShaderShadow->VertexShaderVertexFormat( flags, 1, 0, 0 );
  99. sprite_vs11_Static_Index vshIndex;
  100. bool vertexColor = ( shaderFlags & SHADER_USE_VERTEX_COLOR ) ? true : false;
  101. vshIndex.SetVERTEXCOLOR( vertexColor );
  102. s_pShaderShadow->SetVertexShader( "sprite_vs11", vshIndex.GetIndex() );
  103. // "VERTEXCOLOR" "0..1"
  104. // "CONSTANTCOLOR" "0..1"
  105. int pshIndex = 0;
  106. if ( shaderFlags & SHADER_USE_VERTEX_COLOR ) pshIndex |= 0x1;
  107. if ( shaderFlags & SHADER_USE_CONSTANT_COLOR ) pshIndex |= 0x2;
  108. s_pShaderShadow->SetPixelShader( "sprite_ps11", pshIndex );
  109. }
  110. void SetSpriteCommonDynamicState( unsigned int shaderFlags )
  111. {
  112. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  113. MaterialFogMode_t fogType = s_pShaderAPI->GetSceneFogMode();
  114. int fogIndex = ( fogType == MATERIAL_FOG_LINEAR_BELOW_FOG_Z ) ? 1 : 0;
  115. sprite_vs11_Dynamic_Index vshIndex;
  116. vshIndex.SetSKINNING( 0 );
  117. vshIndex.SetDOWATERFOG( fogIndex );
  118. s_pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  119. s_pShaderAPI->SetPixelShaderIndex( 0 );
  120. if ( shaderFlags & SHADER_USE_CONSTANT_COLOR )
  121. {
  122. SetPixelShaderConstant( 0, COLOR, ALPHA );
  123. }
  124. float color[4] = { 1.0, 1.0, 1.0, 1.0 };
  125. s_pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_MODULATION_COLOR, color );
  126. // identity base texture transorm
  127. float ident[2][4] = {
  128. { 1.0f, 0.0f, 0.0f, 0.0f },
  129. { 0.0f, 1.0f, 0.0f, 0.0f }
  130. };
  131. s_pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, &ident[0][0], 2 );
  132. }
  133. SHADER_DRAW
  134. {
  135. SHADOW_STATE
  136. {
  137. pShaderShadow->EnableCulling( false );
  138. }
  139. switch( params[SPRITERENDERMODE]->GetIntValue() )
  140. {
  141. case kRenderNormal:
  142. SHADOW_STATE
  143. {
  144. FogToFogColor();
  145. SetSpriteCommonShadowState( 0 );
  146. }
  147. DYNAMIC_STATE
  148. {
  149. SetSpriteCommonDynamicState( 0 );
  150. }
  151. Draw();
  152. break;
  153. case kRenderTransColor:
  154. case kRenderTransTexture:
  155. SHADOW_STATE
  156. {
  157. pShaderShadow->EnableDepthWrites( false );
  158. pShaderShadow->EnableBlending( true );
  159. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  160. FogToFogColor();
  161. SetSpriteCommonShadowState( SHADER_USE_VERTEX_COLOR );
  162. }
  163. DYNAMIC_STATE
  164. {
  165. SetSpriteCommonDynamicState( SHADER_USE_VERTEX_COLOR );
  166. }
  167. Draw();
  168. break;
  169. case kRenderGlow:
  170. case kRenderWorldGlow:
  171. SHADOW_STATE
  172. {
  173. pShaderShadow->EnableDepthWrites( false );
  174. pShaderShadow->DepthFunc( SHADER_DEPTHFUNC_ALWAYS );
  175. pShaderShadow->EnableBlending( true );
  176. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE );
  177. FogToBlack();
  178. SetSpriteCommonShadowState( SHADER_USE_VERTEX_COLOR );
  179. }
  180. DYNAMIC_STATE
  181. {
  182. SetSpriteCommonDynamicState( SHADER_USE_VERTEX_COLOR );
  183. }
  184. Draw();
  185. break;
  186. case kRenderTransAlpha:
  187. // untested cut and past from kRenderTransAlphaAdd . . same as first pass of that.
  188. SHADOW_STATE
  189. {
  190. pShaderShadow->EnableDepthWrites( false );
  191. pShaderShadow->EnableBlending( true );
  192. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  193. FogToFogColor();
  194. SetSpriteCommonShadowState( SHADER_USE_VERTEX_COLOR );
  195. }
  196. DYNAMIC_STATE
  197. {
  198. SetSpriteCommonDynamicState( SHADER_USE_VERTEX_COLOR );
  199. }
  200. Draw();
  201. break;
  202. case kRenderTransAlphaAdd:
  203. SHADOW_STATE
  204. {
  205. pShaderShadow->EnableDepthWrites( false );
  206. pShaderShadow->EnableBlending( true );
  207. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  208. FogToFogColor();
  209. SetSpriteCommonShadowState( SHADER_USE_VERTEX_COLOR );
  210. }
  211. DYNAMIC_STATE
  212. {
  213. SetSpriteCommonDynamicState( SHADER_USE_VERTEX_COLOR );
  214. }
  215. Draw();
  216. SHADOW_STATE
  217. {
  218. SetInitialShadowState();
  219. pShaderShadow->EnableDepthWrites( false );
  220. pShaderShadow->EnableBlending( true );
  221. pShaderShadow->BlendFunc( SHADER_BLEND_ONE_MINUS_SRC_ALPHA, SHADER_BLEND_ONE );
  222. FogToBlack();
  223. SetSpriteCommonShadowState( SHADER_USE_VERTEX_COLOR );
  224. }
  225. DYNAMIC_STATE
  226. {
  227. SetSpriteCommonDynamicState( SHADER_USE_VERTEX_COLOR );
  228. }
  229. Draw();
  230. break;
  231. case kRenderTransAdd:
  232. {
  233. unsigned int flags = SHADER_USE_CONSTANT_COLOR;
  234. if( !params[ IGNOREVERTEXCOLORS ]->GetIntValue() )
  235. {
  236. flags |= SHADER_USE_VERTEX_COLOR;
  237. }
  238. SHADOW_STATE
  239. {
  240. pShaderShadow->EnableDepthWrites( false );
  241. pShaderShadow->EnableBlending( true );
  242. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE );
  243. FogToBlack();
  244. SetSpriteCommonShadowState( flags );
  245. }
  246. DYNAMIC_STATE
  247. {
  248. SetSpriteCommonDynamicState( flags );
  249. }
  250. }
  251. Draw();
  252. break;
  253. case kRenderTransAddFrameBlend:
  254. {
  255. float flFrame = params[FRAME]->GetFloatValue();
  256. float flFade = params[ALPHA]->GetFloatValue();
  257. unsigned int flags = SHADER_USE_CONSTANT_COLOR;
  258. if( !params[ IGNOREVERTEXCOLORS ]->GetIntValue() )
  259. {
  260. flags |= SHADER_USE_VERTEX_COLOR;
  261. }
  262. SHADOW_STATE
  263. {
  264. pShaderShadow->EnableDepthWrites( false );
  265. pShaderShadow->EnableBlending( true );
  266. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE );
  267. FogToBlack();
  268. SetSpriteCommonShadowState( flags );
  269. }
  270. DYNAMIC_STATE
  271. {
  272. float frameBlendAlpha = 1.0f - ( flFrame - ( int )flFrame );
  273. ITexture *pTexture = params[BASETEXTURE]->GetTextureValue();
  274. BindTexture( SHADER_SAMPLER0, pTexture, ( int )flFrame );
  275. MaterialFogMode_t fogType = s_pShaderAPI->GetSceneFogMode();
  276. int fogIndex = ( fogType == MATERIAL_FOG_LINEAR_BELOW_FOG_Z ) ? 1 : 0;
  277. sprite_vs11_Dynamic_Index vshIndex;
  278. vshIndex.SetSKINNING( 0 );
  279. vshIndex.SetDOWATERFOG( fogIndex );
  280. s_pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  281. float color[4] = { 1.0, 1.0, 1.0, 1.0 };
  282. s_pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_MODULATION_COLOR, color );
  283. s_pShaderAPI->SetPixelShaderIndex( 0 );
  284. color[0] = color[1] = color[2] = flFade * frameBlendAlpha;
  285. color[3] = 1.0f;
  286. s_pShaderAPI->SetPixelShaderConstant( 0, color );
  287. // identity base texture transorm
  288. float ident[2][4] = {
  289. { 1.0f, 0.0f, 0.0f, 0.0f },
  290. { 0.0f, 1.0f, 0.0f, 0.0f }
  291. };
  292. s_pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, &ident[0][0], 2 );
  293. }
  294. Draw();
  295. SHADOW_STATE
  296. {
  297. FogToBlack();
  298. SetSpriteCommonShadowState( flags );
  299. }
  300. DYNAMIC_STATE
  301. {
  302. float frameBlendAlpha = ( flFrame - ( int )flFrame );
  303. ITexture *pTexture = params[BASETEXTURE]->GetTextureValue();
  304. int numAnimationFrames = pTexture->GetNumAnimationFrames();
  305. BindTexture( SHADER_SAMPLER0, pTexture, ( ( int )flFrame + 1 ) % numAnimationFrames );
  306. MaterialFogMode_t fogType = s_pShaderAPI->GetSceneFogMode();
  307. int fogIndex = ( fogType == MATERIAL_FOG_LINEAR_BELOW_FOG_Z ) ? 1 : 0;
  308. sprite_vs11_Dynamic_Index vshIndex;
  309. vshIndex.SetSKINNING( 0 );
  310. vshIndex.SetDOWATERFOG( fogIndex );
  311. s_pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  312. float color[4] = { 1.0, 1.0, 1.0, 1.0 };
  313. s_pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_MODULATION_COLOR, color );
  314. s_pShaderAPI->SetPixelShaderIndex( 0 );
  315. color[0] = color[1] = color[2] = flFade * frameBlendAlpha;
  316. color[3] = 1.0f;
  317. s_pShaderAPI->SetPixelShaderConstant( 0, color );
  318. // identity base texture transorm
  319. float ident[2][4] = {
  320. { 1.0f, 0.0f, 0.0f, 0.0f },
  321. { 0.0f, 1.0f, 0.0f, 0.0f }
  322. };
  323. s_pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, &ident[0][0], 2 );
  324. }
  325. Draw();
  326. }
  327. break;
  328. default:
  329. ShaderWarning( "shader Sprite: Unknown sprite render mode\n" );
  330. break;
  331. }
  332. }
  333. END_SHADER