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.

361 lines
14 KiB

  1. //========= Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "BaseVSShader.h"
  8. #include "screenspaceeffect_vs20.inc"
  9. // NOTE: This has to be the last file included!
  10. #include "tier0/memdbgon.h"
  11. static void GetAdjustedShaderName( char *pOutputBuffer, char const *pShader20Name )
  12. {
  13. Q_strcpy( pOutputBuffer, pShader20Name );
  14. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  15. {
  16. size_t iLength = Q_strlen( pOutputBuffer );
  17. if( ( iLength > 4 ) &&
  18. (
  19. ( Q_stricmp( pOutputBuffer + iLength - 5, "_ps20" ) == 0 ) ||
  20. ( Q_stricmp( pOutputBuffer + iLength - 5, "_vs20" ) == 0 )
  21. )
  22. )
  23. {
  24. strcpy( pOutputBuffer + iLength - 2, "20b" );
  25. }
  26. }
  27. }
  28. DEFINE_FALLBACK_SHADER( screenspace_general, screenspace_general_dx9 )
  29. BEGIN_VS_SHADER_FLAGS( screenspace_general_dx9, "Help for screenspace_general", SHADER_NOT_EDITABLE )
  30. BEGIN_SHADER_PARAMS
  31. SHADER_PARAM( C0_X,SHADER_PARAM_TYPE_FLOAT,"0","")
  32. SHADER_PARAM( C0_Y,SHADER_PARAM_TYPE_FLOAT,"0","")
  33. SHADER_PARAM( C0_Z,SHADER_PARAM_TYPE_FLOAT,"0","")
  34. SHADER_PARAM( C0_W,SHADER_PARAM_TYPE_FLOAT,"0","")
  35. SHADER_PARAM( C1_X,SHADER_PARAM_TYPE_FLOAT,"0","")
  36. SHADER_PARAM( C1_Y,SHADER_PARAM_TYPE_FLOAT,"0","")
  37. SHADER_PARAM( C1_Z,SHADER_PARAM_TYPE_FLOAT,"0","")
  38. SHADER_PARAM( C1_W,SHADER_PARAM_TYPE_FLOAT,"0","")
  39. SHADER_PARAM( C2_X,SHADER_PARAM_TYPE_FLOAT,"0","")
  40. SHADER_PARAM( C2_Y,SHADER_PARAM_TYPE_FLOAT,"0","")
  41. SHADER_PARAM( C2_Z,SHADER_PARAM_TYPE_FLOAT,"0","")
  42. SHADER_PARAM( C2_W,SHADER_PARAM_TYPE_FLOAT,"0","")
  43. SHADER_PARAM( C3_X,SHADER_PARAM_TYPE_FLOAT,"0","")
  44. SHADER_PARAM( C3_Y,SHADER_PARAM_TYPE_FLOAT,"0","")
  45. SHADER_PARAM( C3_Z,SHADER_PARAM_TYPE_FLOAT,"0","")
  46. SHADER_PARAM( C3_W,SHADER_PARAM_TYPE_FLOAT,"0","")
  47. SHADER_PARAM( C4_X,SHADER_PARAM_TYPE_FLOAT,"0","")
  48. SHADER_PARAM( C4_Y,SHADER_PARAM_TYPE_FLOAT,"0","")
  49. SHADER_PARAM( C4_Z,SHADER_PARAM_TYPE_FLOAT,"0","")
  50. SHADER_PARAM( C4_W,SHADER_PARAM_TYPE_FLOAT,"0","")
  51. SHADER_PARAM( PIXSHADER, SHADER_PARAM_TYPE_STRING, "", "Name of the pixel shader to use" )
  52. SHADER_PARAM( VERTEXSHADER, SHADER_PARAM_TYPE_STRING, "", "Name of the vertex shader to use" )
  53. SHADER_PARAM( DISABLE_COLOR_WRITES,SHADER_PARAM_TYPE_INTEGER,"0","")
  54. SHADER_PARAM( ALPHATESTED,SHADER_PARAM_TYPE_FLOAT,"0","")
  55. SHADER_PARAM( ALPHA_BLEND_COLOR_OVERLAY, SHADER_PARAM_TYPE_INTEGER, "0", "")
  56. SHADER_PARAM( ALPHA_BLEND, SHADER_PARAM_TYPE_INTEGER, "0", "")
  57. SHADER_PARAM( TEXTURE1, SHADER_PARAM_TYPE_TEXTURE, "", "" )
  58. SHADER_PARAM( TEXTURE2, SHADER_PARAM_TYPE_TEXTURE, "", "" )
  59. SHADER_PARAM( TEXTURE3, SHADER_PARAM_TYPE_TEXTURE, "", "" )
  60. SHADER_PARAM( LINEARREAD_BASETEXTURE, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  61. SHADER_PARAM( LINEARREAD_TEXTURE1, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  62. SHADER_PARAM( LINEARREAD_TEXTURE2, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  63. SHADER_PARAM( LINEARREAD_TEXTURE3, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  64. SHADER_PARAM( LINEARWRITE,SHADER_PARAM_TYPE_INTEGER,"0","")
  65. SHADER_PARAM( VERTEXTRANSFORM,SHADER_PARAM_TYPE_INTEGER, "0", "verts are in world space" )
  66. SHADER_PARAM( ALPHABLEND,SHADER_PARAM_TYPE_INTEGER, "0", "whether or not to enable alpha blend" )
  67. SHADER_PARAM( MULTIPLYCOLOR, SHADER_PARAM_TYPE_INTEGER, "0", "whether or not to multiply src and dest color" )
  68. SHADER_PARAM( WRITEALPHA,SHADER_PARAM_TYPE_INTEGER, "0", "whether or not to enable alpha write" )
  69. SHADER_PARAM( WRITEDEPTH,SHADER_PARAM_TYPE_INTEGER, "0", "whether or not to enable depth write" )
  70. SHADER_PARAM( TCSIZE0, SHADER_PARAM_TYPE_INTEGER, "2", "Number of components in texture coord0" )
  71. SHADER_PARAM( TCSIZE1, SHADER_PARAM_TYPE_INTEGER, "0", "Number of components in texture coord1" )
  72. SHADER_PARAM( TCSIZE2, SHADER_PARAM_TYPE_INTEGER, "0", "Number of components in texture coord2" )
  73. SHADER_PARAM( TCSIZE3, SHADER_PARAM_TYPE_INTEGER, "0", "Number of components in texture coord3" )
  74. SHADER_PARAM( TCSIZE4, SHADER_PARAM_TYPE_INTEGER, "0", "Number of components in texture coord4" )
  75. SHADER_PARAM( TCSIZE5, SHADER_PARAM_TYPE_INTEGER, "0", "Number of components in texture coord5" )
  76. SHADER_PARAM( TCSIZE6, SHADER_PARAM_TYPE_INTEGER, "0", "Number of components in texture coord6" )
  77. SHADER_PARAM( TCSIZE7, SHADER_PARAM_TYPE_INTEGER, "0", "Number of components in texture coord7" )
  78. SHADER_PARAM( POINTSAMPLE_BASETEXTURE, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  79. SHADER_PARAM( POINTSAMPLE_TEXTURE1, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  80. SHADER_PARAM( POINTSAMPLE_TEXTURE2, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  81. SHADER_PARAM( POINTSAMPLE_TEXTURE3, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  82. SHADER_PARAM( CULL, SHADER_PARAM_TYPE_INTEGER, "0", "Culling control - 0 = nocull, 1 = do cull" )
  83. SHADER_PARAM( DEPTHTEST, SHADER_PARAM_TYPE_INTEGER, "0", "Enable Depthtest" )
  84. SHADER_PARAM( COPYALPHA, SHADER_PARAM_TYPE_INTEGER, "0", "")
  85. END_SHADER_PARAMS
  86. SHADER_INIT_PARAMS()
  87. {
  88. if ( ! params[TCSIZE0]->IsDefined() )
  89. params[TCSIZE0]->SetIntValue( 2 );
  90. }
  91. SHADER_INIT
  92. {
  93. if ( params[BASETEXTURE]->IsDefined() )
  94. {
  95. #if defined( PLATFORM_POSIX ) && !defined( _PS3 )
  96. ImageFormat fmt = params[BASETEXTURE]->GetTextureValue()->GetImageFormat();
  97. bool bSRGB;
  98. if ( ( fmt == IMAGE_FORMAT_RGBA16161616F ) || ( fmt == IMAGE_FORMAT_RGBA16161616 ) )
  99. bSRGB = false;
  100. else
  101. bSRGB = !params[LINEARREAD_BASETEXTURE]->IsDefined() || !params[LINEARREAD_BASETEXTURE]->GetIntValue();
  102. LoadTexture( BASETEXTURE, bSRGB ? TEXTUREFLAGS_SRGB : 0 );
  103. #else
  104. LoadTexture( BASETEXTURE );
  105. #endif // PLATFORM_POSIX
  106. }
  107. if ( params[TEXTURE1]->IsDefined() )
  108. {
  109. #if defined( PLATFORM_POSIX ) && !defined( _PS3 )
  110. ImageFormat fmt = params[TEXTURE1]->GetTextureValue()->GetImageFormat();
  111. bool bSRGB;
  112. if ( ( fmt == IMAGE_FORMAT_RGBA16161616F ) || ( fmt == IMAGE_FORMAT_RGBA16161616 ) )
  113. bSRGB = false;
  114. else
  115. bSRGB = !params[LINEARREAD_TEXTURE1]->IsDefined() || !params[LINEARREAD_TEXTURE1]->GetIntValue();
  116. LoadTexture( TEXTURE1, bSRGB ? TEXTUREFLAGS_SRGB : 0 );
  117. #else
  118. LoadTexture( TEXTURE1 );
  119. #endif // PLATFORM_POSIX
  120. }
  121. if ( params[TEXTURE2]->IsDefined() )
  122. {
  123. #if defined( PLATFORM_POSIX ) && !defined( _PS3 )
  124. ImageFormat fmt = params[TEXTURE2]->GetTextureValue()->GetImageFormat();
  125. bool bSRGB;
  126. if ( ( fmt == IMAGE_FORMAT_RGBA16161616F ) || ( fmt == IMAGE_FORMAT_RGBA16161616 ) )
  127. bSRGB = false;
  128. else
  129. bSRGB = !params[LINEARREAD_TEXTURE2]->IsDefined() || !params[LINEARREAD_TEXTURE2]->GetIntValue();
  130. LoadTexture( TEXTURE2, bSRGB ? TEXTUREFLAGS_SRGB : 0 );
  131. #else
  132. LoadTexture( TEXTURE2 );
  133. #endif // PLATFORM_POSIX
  134. }
  135. if ( params[TEXTURE3]->IsDefined() )
  136. {
  137. #if defined( PLATFORM_POSIX ) && !defined( _PS3 )
  138. ImageFormat fmt = params[TEXTURE3]->GetTextureValue()->GetImageFormat();
  139. bool bSRGB;
  140. if ( ( fmt == IMAGE_FORMAT_RGBA16161616F ) || ( fmt == IMAGE_FORMAT_RGBA16161616 ) )
  141. bSRGB = false;
  142. else
  143. bSRGB = !params[LINEARREAD_TEXTURE3]->IsDefined() || !params[LINEARREAD_TEXTURE3]->GetIntValue();
  144. LoadTexture( TEXTURE3, bSRGB ? TEXTUREFLAGS_SRGB : 0 );
  145. #else
  146. LoadTexture( TEXTURE3 );
  147. #endif // PLATFORM_POSIX
  148. }
  149. }
  150. SHADER_FALLBACK
  151. {
  152. return 0;
  153. }
  154. SHADER_DRAW
  155. {
  156. bool bCustomVertexShader = params[VERTEXSHADER]->IsDefined();
  157. SHADOW_STATE
  158. {
  159. pShaderShadow->EnableDepthWrites( params[WRITEDEPTH]->GetIntValue() != 0 );
  160. if ( params[WRITEDEPTH]->GetIntValue() != 0 )
  161. {
  162. pShaderShadow->EnableDepthTest( true );
  163. pShaderShadow->DepthFunc( SHADER_DEPTHFUNC_ALWAYS );
  164. }
  165. pShaderShadow->EnableAlphaWrites( params[WRITEALPHA]->GetIntValue() != 0 );
  166. pShaderShadow->EnableDepthTest( params[DEPTHTEST]->GetIntValue() != 0 );
  167. pShaderShadow->EnableCulling( params[CULL]->GetIntValue() != 0 );
  168. if (params[BASETEXTURE]->IsDefined())
  169. {
  170. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  171. pShaderShadow->EnableSRGBRead(SHADER_SAMPLER0, !params[LINEARREAD_BASETEXTURE]->IsDefined() || !params[LINEARREAD_BASETEXTURE]->GetIntValue() );
  172. }
  173. if (params[TEXTURE1]->IsDefined())
  174. {
  175. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  176. pShaderShadow->EnableSRGBRead(SHADER_SAMPLER1, !params[LINEARREAD_TEXTURE1]->IsDefined() || !params[LINEARREAD_TEXTURE1]->GetIntValue() );
  177. }
  178. if (params[TEXTURE2]->IsDefined())
  179. {
  180. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  181. pShaderShadow->EnableSRGBRead(SHADER_SAMPLER2, !params[LINEARREAD_TEXTURE2]->IsDefined() || !params[LINEARREAD_TEXTURE2]->GetIntValue() );
  182. }
  183. if (params[TEXTURE3]->IsDefined())
  184. {
  185. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  186. pShaderShadow->EnableSRGBRead(SHADER_SAMPLER3,false);
  187. pShaderShadow->EnableSRGBRead(SHADER_SAMPLER3, !params[LINEARREAD_TEXTURE3]->IsDefined() || !params[LINEARREAD_TEXTURE3]->GetIntValue() );
  188. }
  189. int fmt = VERTEX_POSITION;
  190. const bool bHasVertexColor = IS_FLAG_SET( MATERIAL_VAR_VERTEXCOLOR );
  191. if ( bHasVertexColor )
  192. {
  193. fmt |= VERTEX_COLOR;
  194. }
  195. int nTexCoordSize[8];
  196. int nNumTexCoords = 0;
  197. static int s_tcSizeIds[]={ TCSIZE0, TCSIZE1, TCSIZE2, TCSIZE3, TCSIZE4, TCSIZE5, TCSIZE6, TCSIZE7 };
  198. int nCtr = 0;
  199. while( nCtr < 8 && ( params[s_tcSizeIds[nCtr]]->GetIntValue() ) )
  200. {
  201. nNumTexCoords++;
  202. nTexCoordSize[nCtr] = params[s_tcSizeIds[nCtr]]->GetIntValue();
  203. nCtr++;
  204. }
  205. pShaderShadow->VertexShaderVertexFormat( fmt, nNumTexCoords, nTexCoordSize, 0 );
  206. if ( IS_FLAG_SET(MATERIAL_VAR_ADDITIVE) )
  207. {
  208. EnableAlphaBlending( SHADER_BLEND_ONE, SHADER_BLEND_ONE );
  209. }
  210. else if ( params[MULTIPLYCOLOR]->GetIntValue() )
  211. {
  212. EnableAlphaBlending( SHADER_BLEND_ZERO, SHADER_BLEND_SRC_COLOR );
  213. }
  214. else if ( params[ALPHABLEND]->GetIntValue() )
  215. {
  216. EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  217. }
  218. else
  219. {
  220. pShaderShadow->EnableBlending( false );
  221. }
  222. // maybe convert from linear to gamma on write.
  223. bool srgb_write=true;
  224. if (params[LINEARWRITE]->GetFloatValue())
  225. srgb_write=false;
  226. pShaderShadow->EnableSRGBWrite( srgb_write );
  227. char szShaderNameBuf[256];
  228. if ( bCustomVertexShader )
  229. {
  230. GetAdjustedShaderName( szShaderNameBuf, params[VERTEXSHADER]->GetStringValue() );
  231. pShaderShadow->SetVertexShader( params[VERTEXSHADER]->GetStringValue(), 0 ); //szShaderNameBuf, 0 );
  232. }
  233. else
  234. {
  235. // Pre-cache shaders
  236. DECLARE_STATIC_VERTEX_SHADER( screenspaceeffect_vs20 );
  237. SET_STATIC_VERTEX_SHADER_COMBO_HAS_DEFAULT( VERTEXCOLOR, bHasVertexColor );
  238. SET_STATIC_VERTEX_SHADER_COMBO_HAS_DEFAULT( TRANSFORMVERTS, params[VERTEXTRANSFORM]->GetIntValue() );
  239. SET_STATIC_VERTEX_SHADER( screenspaceeffect_vs20 );
  240. }
  241. if (params[DISABLE_COLOR_WRITES]->GetIntValue())
  242. {
  243. pShaderShadow->EnableColorWrites(false);
  244. }
  245. // if (params[ALPHATESTED]->GetFloatValue())
  246. {
  247. pShaderShadow->EnableAlphaTest(true);
  248. pShaderShadow->AlphaFunc(SHADER_ALPHAFUNC_GREATER,0.0);
  249. }
  250. GetAdjustedShaderName( szShaderNameBuf, params[PIXSHADER]->GetStringValue() );
  251. pShaderShadow->SetPixelShader( szShaderNameBuf, 0 );
  252. if ( params[ ALPHA_BLEND_COLOR_OVERLAY ]->GetIntValue() )
  253. {
  254. // Used for adding L4D halos
  255. EnableAlphaBlending( SHADER_BLEND_ONE, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  256. }
  257. if ( params[ ALPHA_BLEND ]->GetIntValue() )
  258. {
  259. // Used for adding L4D halos
  260. EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  261. }
  262. if( params[ COPYALPHA ]->GetIntValue() )
  263. {
  264. pShaderShadow->EnableBlending( false );
  265. pShaderShadow->AlphaFunc( SHADER_ALPHAFUNC_ALWAYS, 0.0f );
  266. }
  267. }
  268. DYNAMIC_STATE
  269. {
  270. if (params[BASETEXTURE]->IsDefined())
  271. {
  272. BindTexture( SHADER_SAMPLER0, SRGBReadMask( !params[LINEARREAD_BASETEXTURE]->IsDefined() || !params[LINEARREAD_BASETEXTURE]->GetIntValue() ), BASETEXTURE );
  273. if ( params[POINTSAMPLE_BASETEXTURE]->GetIntValue() )
  274. pShaderAPI->SetTextureFilterMode( SHADER_SAMPLER0, TFILTER_MODE_POINTSAMPLED );
  275. }
  276. if (params[TEXTURE1]->IsDefined())
  277. {
  278. BindTexture( SHADER_SAMPLER1, SRGBReadMask( !params[LINEARREAD_TEXTURE1]->IsDefined() || !params[LINEARREAD_TEXTURE1]->GetIntValue() ), TEXTURE1, -1 );
  279. if ( params[POINTSAMPLE_TEXTURE1]->GetIntValue() )
  280. pShaderAPI->SetTextureFilterMode( SHADER_SAMPLER1, TFILTER_MODE_POINTSAMPLED );
  281. }
  282. if (params[TEXTURE2]->IsDefined())
  283. {
  284. BindTexture( SHADER_SAMPLER2, SRGBReadMask( !params[LINEARREAD_TEXTURE2]->IsDefined() || !params[LINEARREAD_TEXTURE2]->GetIntValue() ), TEXTURE2, -1 );
  285. if ( params[POINTSAMPLE_TEXTURE2]->GetIntValue() )
  286. pShaderAPI->SetTextureFilterMode( SHADER_SAMPLER2, TFILTER_MODE_POINTSAMPLED );
  287. }
  288. if (params[TEXTURE3]->IsDefined())
  289. {
  290. BindTexture( SHADER_SAMPLER3, SRGBReadMask( !params[LINEARREAD_TEXTURE3]->IsDefined() || !params[LINEARREAD_TEXTURE3]->GetIntValue() ), TEXTURE3, -1 );
  291. if ( params[POINTSAMPLE_TEXTURE3]->GetIntValue() )
  292. pShaderAPI->SetTextureFilterMode( SHADER_SAMPLER3, TFILTER_MODE_POINTSAMPLED );
  293. }
  294. float c0[]={
  295. params[C0_X]->GetFloatValue(),
  296. params[C0_Y]->GetFloatValue(),
  297. params[C0_Z]->GetFloatValue(),
  298. params[C0_W]->GetFloatValue(),
  299. params[C1_X]->GetFloatValue(),
  300. params[C1_Y]->GetFloatValue(),
  301. params[C1_Z]->GetFloatValue(),
  302. params[C1_W]->GetFloatValue(),
  303. params[C2_X]->GetFloatValue(),
  304. params[C2_Y]->GetFloatValue(),
  305. params[C2_Z]->GetFloatValue(),
  306. params[C2_W]->GetFloatValue(),
  307. params[C3_X]->GetFloatValue(),
  308. params[C3_Y]->GetFloatValue(),
  309. params[C3_Z]->GetFloatValue(),
  310. params[C3_W]->GetFloatValue(),
  311. params[C4_X]->GetFloatValue(),
  312. params[C4_Y]->GetFloatValue(),
  313. params[C4_Z]->GetFloatValue(),
  314. params[C4_W]->GetFloatValue()
  315. };
  316. pShaderAPI->SetPixelShaderConstant( 0, c0, ARRAYSIZE(c0)/4 );
  317. float eyePos[4];
  318. pShaderAPI->GetWorldSpaceCameraPosition( eyePos );
  319. pShaderAPI->SetPixelShaderConstant( 10, eyePos, 1 );
  320. pShaderAPI->SetVertexShaderIndex( 0 );
  321. pShaderAPI->SetPixelShaderIndex( 0 );
  322. if ( ! bCustomVertexShader )
  323. {
  324. DECLARE_DYNAMIC_VERTEX_SHADER( screenspaceeffect_vs20 );
  325. SET_DYNAMIC_VERTEX_SHADER( screenspaceeffect_vs20 );
  326. }
  327. }
  328. Draw();
  329. }
  330. END_SHADER