Counter Strike : Global Offensive Source Code
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.

473 lines
14 KiB

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