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.

317 lines
11 KiB

  1. //===== Copyright � 1996-2007, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $Header: $
  6. // $NoKeywords: $
  7. //===========================================================================//
  8. #include "BaseVSShader.h"
  9. #include "cloak_blended_pass_helper.h"
  10. #include "cpp_shader_constant_register_map.h"
  11. #include "unlittwotexture_vs20.inc"
  12. #include "unlittwotexture_ps20.inc"
  13. #include "unlittwotexture_ps20b.inc"
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include "tier0/memdbgon.h"
  16. DEFINE_FALLBACK_SHADER( UnlitTwoTexture, UnlitTwoTexture_DX9 )
  17. BEGIN_VS_SHADER( UnlitTwoTexture_DX9, "Help for UnlitTwoTexture_DX9" )
  18. BEGIN_SHADER_PARAMS
  19. SHADER_PARAM( TEXTURE2, SHADER_PARAM_TYPE_TEXTURE, "shadertest/BaseTexture", "second texture" )
  20. SHADER_PARAM( FRAME2, SHADER_PARAM_TYPE_INTEGER, "0", "frame number for $texture2" )
  21. SHADER_PARAM( TEXTURE2TRANSFORM, SHADER_PARAM_TYPE_MATRIX, "center .5 .5 scale 1 1 rotate 0 translate 0 0", "$texture2 texcoord transform" )
  22. // CStrike adaptive crosshair mode
  23. SHADER_PARAM( CROSSHAIRMODE, SHADER_PARAM_TYPE_BOOL, "0", "Crosshair mode" )
  24. SHADER_PARAM( CROSSHAIRCOLORTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "" )
  25. SHADER_PARAM( CROSSHAIRCOLORADAPT, SHADER_PARAM_TYPE_FLOAT, "0.4", "" )
  26. // Cloak Pass
  27. SHADER_PARAM( CLOAKPASSENABLED, SHADER_PARAM_TYPE_BOOL, "0", "Enables cloak render in a second pass" )
  28. SHADER_PARAM( CLOAKFACTOR, SHADER_PARAM_TYPE_FLOAT, "0.0", "" )
  29. SHADER_PARAM( CLOAKCOLORTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "Cloak color tint" )
  30. SHADER_PARAM( REFRACTAMOUNT, SHADER_PARAM_TYPE_FLOAT, "2", "" )
  31. END_SHADER_PARAMS
  32. SHADER_FALLBACK
  33. {
  34. return 0;
  35. }
  36. // Cloak Pass
  37. void SetupVarsCloakBlendedPass( CloakBlendedPassVars_t &info )
  38. {
  39. info.m_nCloakFactor = CLOAKFACTOR;
  40. info.m_nCloakColorTint = CLOAKCOLORTINT;
  41. info.m_nRefractAmount = REFRACTAMOUNT;
  42. }
  43. bool NeedsPowerOfTwoFrameBufferTexture( IMaterialVar **params, bool bCheckSpecificToThisFrame ) const
  44. {
  45. if ( params[CROSSHAIRMODE]->GetFloatValue() > 0.0f )
  46. return true;
  47. if ( params[CLOAKPASSENABLED]->GetIntValue() ) // If material supports cloaking
  48. {
  49. if ( bCheckSpecificToThisFrame == false ) // For setting model flag at load time
  50. return true;
  51. else if ( ( params[CLOAKFACTOR]->GetFloatValue() > 0.0f ) && ( params[CLOAKFACTOR]->GetFloatValue() < 1.0f ) ) // Per-frame check
  52. return true;
  53. // else, not cloaking this frame, so check flag2 in case the base material still needs it
  54. }
  55. // Check flag2 if not drawing cloak pass
  56. return IS_FLAG2_SET( MATERIAL_VAR2_NEEDS_POWER_OF_TWO_FRAME_BUFFER_TEXTURE );
  57. }
  58. bool IsTranslucent( IMaterialVar **params ) const
  59. {
  60. if ( params[CLOAKPASSENABLED]->GetIntValue() ) // If material supports cloaking
  61. {
  62. if ( ( params[CLOAKFACTOR]->GetFloatValue() > 0.0f ) && ( params[CLOAKFACTOR]->GetFloatValue() < 1.0f ) ) // Per-frame check
  63. return true;
  64. // else, not cloaking this frame, so check flag in case the base material still needs it
  65. }
  66. // Check flag if not drawing cloak pass
  67. return IS_FLAG_SET( MATERIAL_VAR_TRANSLUCENT );
  68. }
  69. SHADER_INIT_PARAMS()
  70. {
  71. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  72. if ( !params[CROSSHAIRMODE]->IsDefined() )
  73. {
  74. params[CROSSHAIRMODE]->SetIntValue(0);
  75. }
  76. if (!params[CROSSHAIRCOLORADAPT]->IsDefined())
  77. {
  78. params[CROSSHAIRCOLORADAPT]->SetFloatValue(0.4f);
  79. }
  80. if (!params[CROSSHAIRCOLORTINT]->IsDefined())
  81. {
  82. params[CROSSHAIRCOLORTINT]->SetVecValue(1,1,1);
  83. }
  84. // Cloak Pass
  85. if ( !params[CLOAKPASSENABLED]->IsDefined() )
  86. {
  87. params[CLOAKPASSENABLED]->SetIntValue( 0 );
  88. }
  89. else if ( params[CLOAKPASSENABLED]->GetIntValue() )
  90. {
  91. CloakBlendedPassVars_t info;
  92. SetupVarsCloakBlendedPass( info );
  93. InitParamsCloakBlendedPass( this, params, pMaterialName, info );
  94. }
  95. }
  96. SHADER_INIT
  97. {
  98. if (params[BASETEXTURE]->IsDefined())
  99. LoadTexture( BASETEXTURE, TEXTUREFLAGS_SRGB | ANISOTROPIC_OVERRIDE );
  100. if (params[TEXTURE2]->IsDefined())
  101. LoadTexture( TEXTURE2, TEXTUREFLAGS_SRGB | ANISOTROPIC_OVERRIDE );
  102. // Cloak Pass
  103. if ( params[CLOAKPASSENABLED]->GetIntValue() )
  104. {
  105. CloakBlendedPassVars_t info;
  106. SetupVarsCloakBlendedPass( info );
  107. InitCloakBlendedPass( this, params, info );
  108. }
  109. }
  110. SHADER_DRAW
  111. {
  112. // Skip the standard rendering if cloak pass is fully opaque
  113. bool bDrawStandardPass = true;
  114. if ( params[CLOAKPASSENABLED]->GetIntValue() && ( pShaderShadow == NULL ) ) // && not snapshotting
  115. {
  116. CloakBlendedPassVars_t info;
  117. SetupVarsCloakBlendedPass( info );
  118. if ( CloakBlendedPassIsFullyOpaque( params, info ) )
  119. {
  120. bDrawStandardPass = false;
  121. }
  122. }
  123. // Skip flashlight pass for unlit stuff
  124. bool bNewFlashlightPath = IsX360() || IsPS3();
  125. if ( bDrawStandardPass && ( pShaderShadow == NULL ) && ( pShaderAPI != NULL ) &&
  126. !bNewFlashlightPath && ( pShaderAPI->InFlashlightMode() ) ) // not snapshotting && flashlight pass)
  127. {
  128. bDrawStandardPass = false;
  129. }
  130. // Standard rendering pass
  131. if ( bDrawStandardPass )
  132. {
  133. BlendType_t nBlendType = EvaluateBlendRequirements( BASETEXTURE, true );
  134. bool bFullyOpaque = (nBlendType != BT_BLENDADD) && (nBlendType != BT_BLEND) && !IS_FLAG_SET(MATERIAL_VAR_ALPHATEST); //dest alpha is free for special use
  135. SHADOW_STATE
  136. {
  137. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  138. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  139. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  140. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, true );
  141. s_pShaderShadow->EnableSRGBWrite( true );
  142. // Either we've got a constant modulation or we've got a texture alpha on either texture
  143. if ( IsAlphaModulating() || IS_FLAG_SET( MATERIAL_VAR_TRANSLUCENT ) || TextureIsTranslucent( BASETEXTURE, true ) || TextureIsTranslucent( TEXTURE2, true ) )
  144. {
  145. if ( IS_FLAG_SET(MATERIAL_VAR_ADDITIVE) )
  146. {
  147. EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE );
  148. }
  149. else
  150. {
  151. EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  152. }
  153. }
  154. else
  155. {
  156. if ( IS_FLAG_SET(MATERIAL_VAR_ADDITIVE) )
  157. {
  158. EnableAlphaBlending( SHADER_BLEND_ONE, SHADER_BLEND_ONE );
  159. }
  160. else
  161. {
  162. DisableAlphaBlending( );
  163. }
  164. }
  165. // Set stream format (note that this shader supports compression)
  166. unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL | VERTEX_FORMAT_COMPRESSED;
  167. int nTexCoordCount = 1;
  168. int userDataSize = 0;
  169. if (IS_FLAG_SET( MATERIAL_VAR_VERTEXCOLOR ))
  170. {
  171. flags |= VERTEX_COLOR;
  172. }
  173. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, NULL, userDataSize );
  174. // If this is set, blend with the alpha channels of the textures and modulation color
  175. bool bTranslucent = IsAlphaModulating() || IS_FLAG_SET( MATERIAL_VAR_TRANSLUCENT ) || TextureIsTranslucent( BASETEXTURE, true ) || TextureIsTranslucent( TEXTURE2, true );
  176. bool bCrosshairMode = ( params[CROSSHAIRMODE]->IsDefined() && params[CROSSHAIRMODE]->GetIntValue() > 0 );
  177. int nLightingPreviewMode = IS_FLAG2_SET( MATERIAL_VAR2_USE_GBUFFER0 ) + 2 * IS_FLAG2_SET( MATERIAL_VAR2_USE_GBUFFER1 );
  178. DECLARE_STATIC_VERTEX_SHADER( unlittwotexture_vs20 );
  179. SET_STATIC_VERTEX_SHADER( unlittwotexture_vs20 );
  180. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  181. {
  182. DECLARE_STATIC_PIXEL_SHADER( unlittwotexture_ps20b );
  183. SET_STATIC_PIXEL_SHADER_COMBO( TRANSLUCENT, bTranslucent );
  184. SET_STATIC_PIXEL_SHADER_COMBO( LIGHTING_PREVIEW, nLightingPreviewMode );
  185. SET_STATIC_PIXEL_SHADER_COMBO( CROSSHAIR_MODE, bCrosshairMode );
  186. SET_STATIC_PIXEL_SHADER( unlittwotexture_ps20b );
  187. }
  188. else
  189. {
  190. DECLARE_STATIC_PIXEL_SHADER( unlittwotexture_ps20 );
  191. SET_STATIC_PIXEL_SHADER_COMBO( TRANSLUCENT, bTranslucent );
  192. SET_STATIC_PIXEL_SHADER_COMBO( LIGHTING_PREVIEW, nLightingPreviewMode );
  193. SET_STATIC_PIXEL_SHADER_COMBO( CROSSHAIR_MODE, bCrosshairMode );
  194. SET_STATIC_PIXEL_SHADER( unlittwotexture_ps20 );
  195. }
  196. DefaultFog();
  197. pShaderShadow->EnableAlphaWrites( bFullyOpaque );
  198. PI_BeginCommandBuffer();
  199. PI_SetModulationPixelShaderDynamicState_LinearColorSpace( 1 );
  200. PI_EndCommandBuffer();
  201. }
  202. DYNAMIC_STATE
  203. {
  204. BindTexture( SHADER_SAMPLER0, TEXTURE_BINDFLAGS_SRGBREAD, BASETEXTURE, FRAME );
  205. BindTexture( SHADER_SAMPLER1, TEXTURE_BINDFLAGS_SRGBREAD, TEXTURE2, FRAME2 );
  206. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, BASETEXTURETRANSFORM );
  207. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_2, TEXTURE2TRANSFORM );
  208. pShaderAPI->SetPixelShaderFogParams( PSREG_FOG_PARAMS );
  209. float vEyePos_SpecExponent[4];
  210. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos_SpecExponent );
  211. vEyePos_SpecExponent[3] = 0.0f;
  212. pShaderAPI->SetPixelShaderConstant( PSREG_EYEPOS_SPEC_EXPONENT, vEyePos_SpecExponent, 1 );
  213. if ( params[CROSSHAIRMODE]->IsDefined() && params[CROSSHAIRMODE]->GetIntValue() > 0 )
  214. {
  215. float fvConst4[4] = { params[CROSSHAIRCOLORTINT]->GetVecValue()[0],
  216. params[CROSSHAIRCOLORTINT]->GetVecValue()[1],
  217. params[CROSSHAIRCOLORTINT]->GetVecValue()[2],
  218. params[CROSSHAIRCOLORADAPT]->GetFloatValue() };
  219. pShaderAPI->SetPixelShaderConstant(4, fvConst4, 1);
  220. }
  221. int numBones = pShaderAPI->GetCurrentNumBones();
  222. bool bWorldNormal = pShaderAPI->GetIntRenderingParameter( INT_RENDERPARM_ENABLE_FIXED_LIGHTING ) == ENABLE_FIXED_LIGHTING_OUTPUTNORMAL_AND_DEPTH;
  223. if ( IsPC() && bWorldNormal )
  224. {
  225. float vEyeDir[4];
  226. pShaderAPI->GetWorldSpaceCameraDirection( vEyeDir );
  227. float flFarZ = pShaderAPI->GetFarZ();
  228. vEyeDir[0] /= flFarZ; // Divide by farZ for SSAO algorithm
  229. vEyeDir[1] /= flFarZ;
  230. vEyeDir[2] /= flFarZ;
  231. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_4, vEyeDir );
  232. }
  233. DECLARE_DYNAMIC_VERTEX_SHADER( unlittwotexture_vs20 );
  234. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, numBones > 0 );
  235. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  236. SET_DYNAMIC_VERTEX_SHADER_COMBO( WORLD_NORMAL, bWorldNormal );
  237. SET_DYNAMIC_VERTEX_SHADER( unlittwotexture_vs20 );
  238. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  239. {
  240. DECLARE_DYNAMIC_PIXEL_SHADER( unlittwotexture_ps20b );
  241. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITE_DEPTH_TO_DESTALPHA, bFullyOpaque && pShaderAPI->ShouldWriteDepthToDestAlpha() );
  242. SET_DYNAMIC_PIXEL_SHADER( unlittwotexture_ps20b );
  243. }
  244. else
  245. {
  246. DECLARE_DYNAMIC_PIXEL_SHADER( unlittwotexture_ps20 );
  247. SET_DYNAMIC_PIXEL_SHADER( unlittwotexture_ps20 );
  248. }
  249. }
  250. Draw();
  251. }
  252. else
  253. {
  254. // Skip this pass!
  255. Draw( false );
  256. }
  257. // Cloak Pass
  258. if ( params[CLOAKPASSENABLED]->GetIntValue() )
  259. {
  260. // If ( snapshotting ) or ( we need to draw this frame )
  261. if ( ( pShaderShadow != NULL ) || ( ( params[CLOAKFACTOR]->GetFloatValue() > 0.0f ) && ( params[CLOAKFACTOR]->GetFloatValue() < 1.0f ) ) )
  262. {
  263. CloakBlendedPassVars_t info;
  264. SetupVarsCloakBlendedPass( info );
  265. DrawCloakBlendedPass( this, params, pShaderAPI, pShaderShadow, info, vertexCompression );
  266. }
  267. else // We're not snapshotting and we don't need to draw this frame
  268. {
  269. // Skip this pass!
  270. Draw( false );
  271. }
  272. }
  273. }
  274. END_SHADER