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.

313 lines
11 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "BaseVSShader.h"
  8. #include "screenspaceeffect_vs20.inc"
  9. DEFINE_FALLBACK_SHADER( screenspace_general, screenspace_general_dx9 )
  10. BEGIN_VS_SHADER_FLAGS( screenspace_general_dx9, "Help for screenspace_general", SHADER_NOT_EDITABLE )
  11. BEGIN_SHADER_PARAMS
  12. SHADER_PARAM( C0_X,SHADER_PARAM_TYPE_FLOAT,"0","")
  13. SHADER_PARAM( C0_Y,SHADER_PARAM_TYPE_FLOAT,"0","")
  14. SHADER_PARAM( C0_Z,SHADER_PARAM_TYPE_FLOAT,"0","")
  15. SHADER_PARAM( C0_W,SHADER_PARAM_TYPE_FLOAT,"0","")
  16. SHADER_PARAM( C1_X,SHADER_PARAM_TYPE_FLOAT,"0","")
  17. SHADER_PARAM( C1_Y,SHADER_PARAM_TYPE_FLOAT,"0","")
  18. SHADER_PARAM( C1_Z,SHADER_PARAM_TYPE_FLOAT,"0","")
  19. SHADER_PARAM( C1_W,SHADER_PARAM_TYPE_FLOAT,"0","")
  20. SHADER_PARAM( C2_X,SHADER_PARAM_TYPE_FLOAT,"0","")
  21. SHADER_PARAM( C2_Y,SHADER_PARAM_TYPE_FLOAT,"0","")
  22. SHADER_PARAM( C2_Z,SHADER_PARAM_TYPE_FLOAT,"0","")
  23. SHADER_PARAM( C2_W,SHADER_PARAM_TYPE_FLOAT,"0","")
  24. SHADER_PARAM( C3_X,SHADER_PARAM_TYPE_FLOAT,"0","")
  25. SHADER_PARAM( C3_Y,SHADER_PARAM_TYPE_FLOAT,"0","")
  26. SHADER_PARAM( C3_Z,SHADER_PARAM_TYPE_FLOAT,"0","")
  27. SHADER_PARAM( C3_W,SHADER_PARAM_TYPE_FLOAT,"0","")
  28. SHADER_PARAM( PIXSHADER, SHADER_PARAM_TYPE_STRING, "", "Name of the pixel shader to use" )
  29. SHADER_PARAM( DISABLE_COLOR_WRITES,SHADER_PARAM_TYPE_INTEGER,"0","")
  30. SHADER_PARAM( ALPHATESTED,SHADER_PARAM_TYPE_FLOAT,"0","")
  31. SHADER_PARAM( ALPHA_BLEND_COLOR_OVERLAY, SHADER_PARAM_TYPE_INTEGER, "0", "")
  32. SHADER_PARAM( ALPHA_BLEND, SHADER_PARAM_TYPE_INTEGER, "0", "")
  33. SHADER_PARAM( TEXTURE1, SHADER_PARAM_TYPE_TEXTURE, "", "" )
  34. SHADER_PARAM( TEXTURE2, SHADER_PARAM_TYPE_TEXTURE, "", "" )
  35. SHADER_PARAM( TEXTURE3, SHADER_PARAM_TYPE_TEXTURE, "", "" )
  36. SHADER_PARAM( LINEARREAD_BASETEXTURE, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  37. SHADER_PARAM( LINEARREAD_TEXTURE1, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  38. SHADER_PARAM( LINEARREAD_TEXTURE2, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  39. SHADER_PARAM( LINEARREAD_TEXTURE3, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  40. SHADER_PARAM( LINEARWRITE,SHADER_PARAM_TYPE_INTEGER,"0","")
  41. SHADER_PARAM( X360APPCHOOSER, SHADER_PARAM_TYPE_INTEGER, "0", "Needed for movies in 360 launcher" )
  42. SHADER_PARAM( COPYALPHA, SHADER_PARAM_TYPE_INTEGER, "0", "")
  43. END_SHADER_PARAMS
  44. SHADER_INIT
  45. {
  46. if ( params[BASETEXTURE]->IsDefined() )
  47. {
  48. #ifdef POSIX
  49. ImageFormat fmt = params[BASETEXTURE]->GetTextureValue()->GetImageFormat();
  50. bool bSRGB;
  51. if ( ( fmt == IMAGE_FORMAT_RGBA16161616F ) || ( fmt == IMAGE_FORMAT_RGBA16161616 ) )
  52. bSRGB = false;
  53. else
  54. bSRGB = !params[LINEARREAD_BASETEXTURE]->IsDefined() || !params[LINEARREAD_BASETEXTURE]->GetIntValue();
  55. LoadTexture( BASETEXTURE, bSRGB ? TEXTUREFLAGS_SRGB : 0 );
  56. #else
  57. LoadTexture( BASETEXTURE );
  58. #endif // POSIX
  59. }
  60. if ( params[TEXTURE1]->IsDefined() )
  61. {
  62. #ifdef POSIX
  63. ImageFormat fmt = params[TEXTURE1]->GetTextureValue()->GetImageFormat();
  64. bool bSRGB;
  65. if ( ( fmt == IMAGE_FORMAT_RGBA16161616F ) || ( fmt == IMAGE_FORMAT_RGBA16161616 ) )
  66. bSRGB = false;
  67. else
  68. bSRGB = !params[LINEARREAD_TEXTURE1]->IsDefined() || !params[LINEARREAD_TEXTURE1]->GetIntValue();
  69. LoadTexture( TEXTURE1, bSRGB ? TEXTUREFLAGS_SRGB : 0 );
  70. #else
  71. LoadTexture( TEXTURE1 );
  72. #endif // POSIX
  73. }
  74. if ( params[TEXTURE2]->IsDefined() )
  75. {
  76. #ifdef POSIX
  77. ImageFormat fmt = params[TEXTURE2]->GetTextureValue()->GetImageFormat();
  78. bool bSRGB;
  79. if ( ( fmt == IMAGE_FORMAT_RGBA16161616F ) || ( fmt == IMAGE_FORMAT_RGBA16161616 ) )
  80. bSRGB = false;
  81. else
  82. bSRGB = !params[LINEARREAD_TEXTURE2]->IsDefined() || !params[LINEARREAD_TEXTURE2]->GetIntValue();
  83. LoadTexture( TEXTURE2, bSRGB ? TEXTUREFLAGS_SRGB : 0 );
  84. #else
  85. LoadTexture( TEXTURE2 );
  86. #endif // POSIX
  87. }
  88. if ( params[TEXTURE3]->IsDefined() )
  89. {
  90. #ifdef POSIX
  91. ImageFormat fmt = params[TEXTURE3]->GetTextureValue()->GetImageFormat();
  92. bool bSRGB;
  93. if ( ( fmt == IMAGE_FORMAT_RGBA16161616F ) || ( fmt == IMAGE_FORMAT_RGBA16161616 ) )
  94. bSRGB = false;
  95. else
  96. bSRGB = !params[LINEARREAD_TEXTURE3]->IsDefined() || !params[LINEARREAD_TEXTURE3]->GetIntValue();
  97. LoadTexture( TEXTURE3, bSRGB ? TEXTUREFLAGS_SRGB : 0 );
  98. #else
  99. LoadTexture( TEXTURE3 );
  100. #endif // POSIX
  101. }
  102. }
  103. SHADER_FALLBACK
  104. {
  105. if ( g_pHardwareConfig->GetDXSupportLevel() < 90 )
  106. {
  107. return "screenspace_general_dx8";
  108. }
  109. return 0;
  110. }
  111. SHADER_DRAW
  112. {
  113. SHADOW_STATE
  114. {
  115. pShaderShadow->EnableDepthWrites( false );
  116. if (params[BASETEXTURE]->IsDefined())
  117. {
  118. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  119. ITexture *txtr=params[BASETEXTURE]->GetTextureValue();
  120. ImageFormat fmt=txtr->GetImageFormat();
  121. if ((fmt==IMAGE_FORMAT_RGBA16161616F) || (fmt==IMAGE_FORMAT_RGBA16161616))
  122. pShaderShadow->EnableSRGBRead(SHADER_SAMPLER0,false);
  123. else
  124. pShaderShadow->EnableSRGBRead(SHADER_SAMPLER0, !params[LINEARREAD_BASETEXTURE]->IsDefined() || !params[LINEARREAD_BASETEXTURE]->GetIntValue() );
  125. }
  126. if (params[TEXTURE1]->IsDefined())
  127. {
  128. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  129. ITexture *txtr=params[TEXTURE1]->GetTextureValue();
  130. ImageFormat fmt=txtr->GetImageFormat();
  131. if ((fmt==IMAGE_FORMAT_RGBA16161616F) || (fmt==IMAGE_FORMAT_RGBA16161616))
  132. pShaderShadow->EnableSRGBRead(SHADER_SAMPLER1,false);
  133. else
  134. pShaderShadow->EnableSRGBRead(SHADER_SAMPLER1, !params[LINEARREAD_TEXTURE1]->IsDefined() || !params[LINEARREAD_TEXTURE1]->GetIntValue() );
  135. }
  136. if (params[TEXTURE2]->IsDefined())
  137. {
  138. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  139. ITexture *txtr=params[TEXTURE2]->GetTextureValue();
  140. ImageFormat fmt=txtr->GetImageFormat();
  141. if ((fmt==IMAGE_FORMAT_RGBA16161616F) || (fmt==IMAGE_FORMAT_RGBA16161616))
  142. pShaderShadow->EnableSRGBRead(SHADER_SAMPLER2,false);
  143. else
  144. pShaderShadow->EnableSRGBRead(SHADER_SAMPLER2, !params[LINEARREAD_TEXTURE2]->IsDefined() || !params[LINEARREAD_TEXTURE2]->GetIntValue() );
  145. }
  146. if (params[TEXTURE3]->IsDefined())
  147. {
  148. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  149. ITexture *txtr=params[TEXTURE3]->GetTextureValue();
  150. ImageFormat fmt=txtr->GetImageFormat();
  151. if ((fmt==IMAGE_FORMAT_RGBA16161616F) || (fmt==IMAGE_FORMAT_RGBA16161616))
  152. pShaderShadow->EnableSRGBRead(SHADER_SAMPLER3,false);
  153. else
  154. pShaderShadow->EnableSRGBRead(SHADER_SAMPLER3, !params[LINEARREAD_TEXTURE3]->IsDefined() || !params[LINEARREAD_TEXTURE3]->GetIntValue() );
  155. }
  156. int fmt = VERTEX_POSITION;
  157. if ( IS_PARAM_DEFINED( X360APPCHOOSER ) && ( params[X360APPCHOOSER]->GetIntValue() ) )
  158. {
  159. fmt |= VERTEX_COLOR;
  160. EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  161. }
  162. pShaderShadow->VertexShaderVertexFormat( fmt, 1, 0, 0 );
  163. // maybe convert from linear to gamma on write.
  164. bool srgb_write=true;
  165. if (params[LINEARWRITE]->GetFloatValue())
  166. srgb_write=false;
  167. pShaderShadow->EnableSRGBWrite( srgb_write );
  168. // Pre-cache shaders
  169. DECLARE_STATIC_VERTEX_SHADER( screenspaceeffect_vs20 );
  170. SET_STATIC_VERTEX_SHADER_COMBO( X360APPCHOOSER, IS_PARAM_DEFINED( X360APPCHOOSER ) ? params[X360APPCHOOSER]->GetIntValue() : 0 );
  171. vsh_forgot_to_set_static_X360APPCHOOSER = 0; // This is a dirty workaround to the shortcut [= 0] in the fxc
  172. SET_STATIC_VERTEX_SHADER( screenspaceeffect_vs20 );
  173. if (params[DISABLE_COLOR_WRITES]->GetIntValue())
  174. {
  175. pShaderShadow->EnableColorWrites(false);
  176. }
  177. // if (params[ALPHATESTED]->GetFloatValue())
  178. {
  179. pShaderShadow->EnableAlphaTest(true);
  180. pShaderShadow->AlphaFunc(SHADER_ALPHAFUNC_GREATER,0.0);
  181. }
  182. if ( IS_FLAG_SET(MATERIAL_VAR_ADDITIVE) )
  183. {
  184. EnableAlphaBlending( SHADER_BLEND_ONE, SHADER_BLEND_ONE );
  185. }
  186. if ( params[ ALPHA_BLEND_COLOR_OVERLAY ]->GetIntValue() )
  187. {
  188. // Used for adding L4D-style halos
  189. EnableAlphaBlending( SHADER_BLEND_ONE, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  190. }
  191. if ( params[ ALPHA_BLEND ]->GetIntValue() )
  192. {
  193. // Used for adding L4D-style halos
  194. EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  195. }
  196. if( params[ COPYALPHA ]->GetIntValue() )
  197. {
  198. pShaderShadow->EnableBlending( false );
  199. pShaderShadow->AlphaFunc( SHADER_ALPHAFUNC_ALWAYS, 0.0f );
  200. }
  201. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  202. {
  203. const char *szPixelShader = params[PIXSHADER]->GetStringValue();
  204. size_t iLength = Q_strlen( szPixelShader );
  205. if( (iLength > 5) && (Q_stricmp( &szPixelShader[iLength - 5], "_ps20" ) == 0) ) //detect if it's trying to load a ps20 shader
  206. {
  207. //replace it with the ps20b shader
  208. char *szNewName = (char *)stackalloc( sizeof( char ) * (iLength + 2) );
  209. memcpy( szNewName, szPixelShader, sizeof( char ) * iLength );
  210. szNewName[iLength] = 'b';
  211. szNewName[iLength + 1] = '\0';
  212. pShaderShadow->SetPixelShader( szNewName, 0 );
  213. }
  214. else
  215. {
  216. pShaderShadow->SetPixelShader( params[PIXSHADER]->GetStringValue(), 0 );
  217. }
  218. }
  219. else
  220. {
  221. pShaderShadow->SetPixelShader( params[PIXSHADER]->GetStringValue(), 0 );
  222. }
  223. }
  224. DYNAMIC_STATE
  225. {
  226. // Using c4-c7 to store the pixel sizes of each texture
  227. if (params[BASETEXTURE]->IsDefined())
  228. {
  229. BindTexture( SHADER_SAMPLER0, BASETEXTURE, -1 );
  230. ITexture *pTarget = params[ BASETEXTURE ]->GetTextureValue();
  231. float vPixelSize[4] = { 1.0f / pTarget->GetActualWidth(), 1.0f / pTarget->GetActualHeight(), 0.0f, 0.0f };
  232. pShaderAPI->SetPixelShaderConstant( 4, vPixelSize, 1 );
  233. }
  234. if (params[TEXTURE1]->IsDefined())
  235. {
  236. BindTexture( SHADER_SAMPLER1, TEXTURE1, -1 );
  237. ITexture *pTarget = params[ TEXTURE1 ]->GetTextureValue();
  238. float vPixelSize[4] = { 1.0f / pTarget->GetActualWidth(), 1.0f / pTarget->GetActualHeight(), 0.0f, 0.0f };
  239. pShaderAPI->SetPixelShaderConstant( 5, vPixelSize, 1 );
  240. }
  241. if (params[TEXTURE2]->IsDefined())
  242. {
  243. BindTexture( SHADER_SAMPLER2, TEXTURE2, -1 );
  244. ITexture *pTarget = params[ TEXTURE2 ]->GetTextureValue();
  245. float vPixelSize[4] = { 1.0f / pTarget->GetActualWidth(), 1.0f / pTarget->GetActualHeight(), 0.0f, 0.0f };
  246. pShaderAPI->SetPixelShaderConstant( 6, vPixelSize, 1 );
  247. }
  248. if (params[TEXTURE3]->IsDefined())
  249. {
  250. BindTexture( SHADER_SAMPLER3, TEXTURE3, -1 );
  251. ITexture *pTarget = params[ TEXTURE3 ]->GetTextureValue();
  252. float vPixelSize[4] = { 1.0f / pTarget->GetActualWidth(), 1.0f / pTarget->GetActualHeight(), 0.0f, 0.0f };
  253. pShaderAPI->SetPixelShaderConstant( 7, vPixelSize, 1 );
  254. }
  255. float c0[] = {
  256. params[C0_X]->GetFloatValue(),
  257. params[C0_Y]->GetFloatValue(),
  258. params[C0_Z]->GetFloatValue(),
  259. params[C0_W]->GetFloatValue(),
  260. params[C1_X]->GetFloatValue(),
  261. params[C1_Y]->GetFloatValue(),
  262. params[C1_Z]->GetFloatValue(),
  263. params[C1_W]->GetFloatValue(),
  264. params[C2_X]->GetFloatValue(),
  265. params[C2_Y]->GetFloatValue(),
  266. params[C2_Z]->GetFloatValue(),
  267. params[C2_W]->GetFloatValue(),
  268. params[C3_X]->GetFloatValue(),
  269. params[C3_Y]->GetFloatValue(),
  270. params[C3_Z]->GetFloatValue(),
  271. params[C3_W]->GetFloatValue()
  272. };
  273. // c0-c3
  274. pShaderAPI->SetPixelShaderConstant( 0, c0, ARRAYSIZE(c0)/4 );
  275. float eyePos[4];
  276. pShaderAPI->GetWorldSpaceCameraPosition( eyePos );
  277. pShaderAPI->SetPixelShaderConstant( 10, eyePos, 1 );
  278. pShaderAPI->SetVertexShaderIndex( 0 );
  279. pShaderAPI->SetPixelShaderIndex( 0 );
  280. DECLARE_DYNAMIC_VERTEX_SHADER( screenspaceeffect_vs20 );
  281. SET_DYNAMIC_VERTEX_SHADER( screenspaceeffect_vs20 );
  282. }
  283. Draw();
  284. }
  285. END_SHADER