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.

335 lines
12 KiB

  1. //===== Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #include "BaseVSShader.h"
  8. #include "cloak_dx9_helper.h"
  9. #include "../shaderapidx9/locald3dtypes.h"
  10. #include "convar.h"
  11. #include "cpp_shader_constant_register_map.h"
  12. #include "cloak_vs20.inc"
  13. #include "cloak_ps20.inc"
  14. #include "cloak_ps20b.inc"
  15. #if !defined( _X360 ) && !defined( _PS3 )
  16. #include "cloak_vs30.inc"
  17. #include "cloak_ps30.inc"
  18. #endif
  19. // NOTE: This has to be the last file included!
  20. #include "tier0/memdbgon.h"
  21. static ConVar r_lightwarpidentity( "r_lightwarpidentity", "0", FCVAR_CHEAT );
  22. // FIXME: doesn't support fresnel!
  23. void InitParamsCloak_DX9( CBaseVSShader *pShader, IMaterialVar** params, const char *pMaterialName, Cloak_DX9_Vars_t &info )
  24. {
  25. SET_FLAGS2( MATERIAL_VAR2_NEEDS_TANGENT_SPACES );
  26. SET_FLAGS( MATERIAL_VAR_TRANSLUCENT );
  27. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_VERTEX_LIT );
  28. if( !params[info.m_nFresnelReflection]->IsDefined() )
  29. {
  30. params[info.m_nFresnelReflection]->SetFloatValue( 1.0f );
  31. }
  32. if( !params[info.m_nMasked]->IsDefined() )
  33. {
  34. params[info.m_nMasked]->SetIntValue( 0 );
  35. }
  36. SET_FLAGS2( MATERIAL_VAR2_NEEDS_POWER_OF_TWO_FRAME_BUFFER_TEXTURE );
  37. }
  38. void InitCloak_DX9( CBaseVSShader *pShader, IMaterialVar** params, Cloak_DX9_Vars_t &info )
  39. {
  40. if (params[info.m_nBaseTexture]->IsDefined() )
  41. {
  42. pShader->LoadTexture( info.m_nBaseTexture, TEXTUREFLAGS_SRGB );
  43. }
  44. if (params[info.m_nNormalMap]->IsDefined() )
  45. {
  46. pShader->LoadBumpMap( info.m_nNormalMap );
  47. }
  48. if ( (info.m_nDiffuseWarpTexture != -1) && params[info.m_nDiffuseWarpTexture]->IsDefined() )
  49. {
  50. pShader->LoadTexture( info.m_nDiffuseWarpTexture );
  51. }
  52. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_VERTEX_LIT );
  53. }
  54. void DrawCloak_DX9( CBaseVSShader *pShader, IMaterialVar** params, IShaderDynamicAPI *pShaderAPI,
  55. IShaderShadow* pShaderShadow, Cloak_DX9_Vars_t &info, VertexCompressionType_t vertexCompression )
  56. {
  57. bool bIsModel = IS_FLAG_SET( MATERIAL_VAR_MODEL );
  58. bool bMasked = (params[info.m_nMasked]->GetIntValue() != 0);
  59. bool hasDiffuseWarp = (info.m_nDiffuseWarpTexture != -1) && params[info.m_nDiffuseWarpTexture]->IsTexture();
  60. bool hasPhongExponentTexture = (info.m_nPhongExponentTexture != -1) && params[info.m_nPhongExponentTexture]->IsTexture();
  61. bool hasPhongTintMap = hasPhongExponentTexture && (info.m_nPhongAlbedoTint != -1) && ( params[info.m_nPhongAlbedoTint]->GetIntValue() != 0 );
  62. bool bHasRimLight = (info.m_nRimLight != -1) && ( params[info.m_nRimLight]->GetIntValue() != 0 );
  63. bool bHasRimMaskMap = hasPhongExponentTexture && bHasRimLight && (info.m_nRimMask != -1) && ( params[info.m_nRimMask]->GetIntValue() != 0 );
  64. SHADOW_STATE
  65. {
  66. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_VERTEX_LIT );
  67. pShader->SetInitialShadowState( );
  68. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true ); // Always SRGB read on base map
  69. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER2, true ); // Refraction map sampler...
  70. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  71. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  72. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  73. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  74. pShaderShadow->EnableTexture( SHADER_SAMPLER5, true );
  75. pShaderShadow->EnableSRGBWrite( true );
  76. unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL;
  77. int nTexCoordCount = 1;
  78. int userDataSize = 0;
  79. if( bIsModel )
  80. {
  81. userDataSize = 4;
  82. }
  83. else
  84. {
  85. flags |= VERTEX_TANGENT_S | VERTEX_TANGENT_T;
  86. }
  87. // This shader supports compressed vertices, so OR in that flag:
  88. flags |= VERTEX_FORMAT_COMPRESSED;
  89. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, NULL, userDataSize );
  90. #if !defined( _X360 ) && !defined( _PS3 )
  91. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  92. #endif
  93. {
  94. bool bFlattenStaticControlFlow = !g_pHardwareConfig->SupportsStaticControlFlow();
  95. DECLARE_STATIC_VERTEX_SHADER( cloak_vs20 );
  96. SET_STATIC_VERTEX_SHADER_COMBO( MODEL, bIsModel );
  97. SET_STATIC_VERTEX_SHADER_COMBO( FLATTEN_STATIC_CONTROL_FLOW, bFlattenStaticControlFlow );
  98. SET_STATIC_VERTEX_SHADER( cloak_vs20 );
  99. // Bind ps_2_b shader so we can get Phong terms
  100. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  101. {
  102. DECLARE_STATIC_PIXEL_SHADER( cloak_ps20b );
  103. SET_STATIC_PIXEL_SHADER_COMBO( LIGHTWARPTEXTURE, hasDiffuseWarp );
  104. SET_STATIC_PIXEL_SHADER( cloak_ps20b );
  105. }
  106. else
  107. {
  108. DECLARE_STATIC_PIXEL_SHADER( cloak_ps20 );
  109. SET_STATIC_PIXEL_SHADER_COMBO( LIGHTWARPTEXTURE, hasDiffuseWarp );
  110. SET_STATIC_PIXEL_SHADER( cloak_ps20 );
  111. }
  112. }
  113. #if !defined( _X360 ) && !defined( _PS3 )
  114. else
  115. {
  116. // The vertex shader uses the vertex id stream
  117. SET_FLAGS2( MATERIAL_VAR2_USES_VERTEXID );
  118. DECLARE_STATIC_VERTEX_SHADER( cloak_vs30 );
  119. SET_STATIC_VERTEX_SHADER_COMBO( MODEL, bIsModel );
  120. SET_STATIC_VERTEX_SHADER( cloak_vs30 );
  121. // Bind ps_2_b shader so we can get Phong terms
  122. DECLARE_STATIC_PIXEL_SHADER( cloak_ps30 );
  123. SET_STATIC_PIXEL_SHADER_COMBO( LIGHTWARPTEXTURE, hasDiffuseWarp );
  124. SET_STATIC_PIXEL_SHADER( cloak_ps30 );
  125. }
  126. #endif
  127. pShader->DefaultFog();
  128. if( bMasked )
  129. {
  130. pShader->EnableAlphaBlending( SHADER_BLEND_ONE_MINUS_SRC_ALPHA, SHADER_BLEND_SRC_ALPHA );
  131. }
  132. // Lighting constants
  133. pShader->PI_BeginCommandBuffer();
  134. pShader->PI_SetPixelShaderAmbientLightCube( PSREG_AMBIENT_CUBE );
  135. pShader->PI_SetPixelShaderLocalLighting( PSREG_LIGHT_INFO_ARRAY );
  136. pShader->PI_EndCommandBuffer();
  137. }
  138. DYNAMIC_STATE
  139. {
  140. pShaderAPI->SetDefaultState();
  141. // Bind textures
  142. pShader->BindTexture( SHADER_SAMPLER0, TEXTURE_BINDFLAGS_SRGBREAD, info.m_nBaseTexture, 0 ); // Base Map
  143. pShaderAPI->BindStandardTexture( SHADER_SAMPLER2, TEXTURE_BINDFLAGS_NONE, TEXTURE_FRAME_BUFFER_FULL_TEXTURE_0 ); // Refraction Map
  144. pShader->BindTexture( SHADER_SAMPLER3, TEXTURE_BINDFLAGS_NONE, info.m_nNormalMap, info.m_nBumpFrame ); // Normal Map
  145. pShaderAPI->BindStandardTexture( SHADER_SAMPLER5, TEXTURE_BINDFLAGS_NONE, TEXTURE_NORMALIZATION_CUBEMAP_SIGNED ); // Normalization cube map
  146. if ( hasDiffuseWarp )
  147. {
  148. if ( r_lightwarpidentity.GetBool() )
  149. {
  150. pShaderAPI->BindStandardTexture( SHADER_SAMPLER1, TEXTURE_BINDFLAGS_NONE, TEXTURE_IDENTITY_LIGHTWARP );
  151. }
  152. else
  153. {
  154. pShader->BindTexture( SHADER_SAMPLER1, TEXTURE_BINDFLAGS_NONE, info.m_nDiffuseWarpTexture ); // Light warp texture
  155. }
  156. }
  157. MaterialFogMode_t fogType = pShaderAPI->GetSceneFogMode();
  158. LightState_t lightState = {0, false, false};
  159. pShaderAPI->GetDX9LightState( &lightState );
  160. #if !defined( _X360 ) && !defined( _PS3 )
  161. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  162. #endif
  163. {
  164. bool bUseStaticControlFlow = g_pHardwareConfig->SupportsStaticControlFlow();
  165. DECLARE_DYNAMIC_VERTEX_SHADER( cloak_vs20 );
  166. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  167. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  168. SET_DYNAMIC_VERTEX_SHADER_COMBO( NUM_LIGHTS, bUseStaticControlFlow ? 0 : lightState.m_nNumLights );
  169. SET_DYNAMIC_VERTEX_SHADER( cloak_vs20 );
  170. // Bind ps_2_b shader so we can get Phong, rim and a cloudier refraction
  171. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  172. {
  173. DECLARE_DYNAMIC_PIXEL_SHADER( cloak_ps20b );
  174. SET_DYNAMIC_PIXEL_SHADER_COMBO( NUM_LIGHTS, lightState.m_nNumLights );
  175. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITEWATERFOGTODESTALPHA, fogType == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  176. SET_DYNAMIC_PIXEL_SHADER( cloak_ps20b );
  177. }
  178. else
  179. {
  180. // JasonM Hack
  181. //
  182. // In general, cloaking on ps_2_0 needs re-working for multipass...yuck...
  183. //
  184. int nPS20NumLights = MAX( lightState.m_nNumLights, 1 );
  185. DECLARE_DYNAMIC_PIXEL_SHADER( cloak_ps20 );
  186. SET_DYNAMIC_PIXEL_SHADER_COMBO( NUM_LIGHTS, nPS20NumLights );
  187. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITEWATERFOGTODESTALPHA, fogType == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  188. SET_DYNAMIC_PIXEL_SHADER( cloak_ps20 );
  189. }
  190. }
  191. #if !defined( _X360 ) && !defined( _PS3 )
  192. else
  193. {
  194. pShader->SetHWMorphVertexShaderState( VERTEX_SHADER_SHADER_SPECIFIC_CONST_6, VERTEX_SHADER_SHADER_SPECIFIC_CONST_7, SHADER_VERTEXTEXTURE_SAMPLER0 );
  195. DECLARE_DYNAMIC_VERTEX_SHADER( cloak_vs30 );
  196. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  197. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  198. SET_DYNAMIC_VERTEX_SHADER( cloak_vs30 );
  199. DECLARE_DYNAMIC_PIXEL_SHADER( cloak_ps30 );
  200. SET_DYNAMIC_PIXEL_SHADER_COMBO( NUM_LIGHTS, lightState.m_nNumLights );
  201. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITEWATERFOGTODESTALPHA, fogType == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  202. SET_DYNAMIC_PIXEL_SHADER( cloak_ps30 );
  203. }
  204. #endif
  205. pShader->SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_1, info.m_nBumpTransform );
  206. if( g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE )
  207. {
  208. pShader->SetPixelShaderConstant( 27, info.m_nRefractTint );
  209. }
  210. else
  211. {
  212. pShader->SetPixelShaderConstantGammaToLinear( 27, info.m_nRefractTint );
  213. }
  214. pShaderAPI->SetPixelShaderFogParams( PSREG_FOG_PARAMS );
  215. // Pack phong exponent in with the eye position
  216. float vEyePos_SpecExponent[4], vFresnelRanges_SpecBoost[4] = {0, 0.5, 1, 1};
  217. float vSpecularTint[4] = {1, 1, 1, 1}, vRimBoost[4] = {1, 1, 1, 1};
  218. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos_SpecExponent );
  219. if ( (info.m_nPhongExponent != -1) && params[info.m_nPhongExponent]->IsDefined() )
  220. vEyePos_SpecExponent[3] = params[info.m_nPhongExponent]->GetFloatValue(); // This overrides the channel in the map
  221. else
  222. vEyePos_SpecExponent[3] = 0; // Use the alpha channel of the normal map for the exponent
  223. if ( (info.m_nPhongTint != -1 ) && params[info.m_nPhongTint]->IsDefined() ) // Get the tint parameter
  224. params[info.m_nPhongTint]->GetVecValue(vSpecularTint, 4);
  225. // Get the rim light power (goes in w of Phong tint)
  226. if ( bHasRimLight && (info.m_nRimLightPower != -1) && params[info.m_nRimLightPower]->IsDefined() )
  227. {
  228. vSpecularTint[3] = params[info.m_nRimLightPower]->GetFloatValue();
  229. vSpecularTint[3] = MAX(vSpecularTint[3], 1.0f); // Make sure this is at least 1
  230. }
  231. // Get the rim boost power (goes in w of flashlight position)
  232. if ( bHasRimLight && (info.m_nRimLightBoost != -1) && params[info.m_nRimLightBoost]->IsDefined() )
  233. {
  234. vRimBoost[3] = params[info.m_nRimLightBoost]->GetFloatValue();
  235. }
  236. // Rim mask...if this is true, use alpha channel of spec exponent texture to mask the rim term
  237. if ( bHasRimMaskMap )
  238. {
  239. float vRimMaskControl[4] = {0, 0, 0, 0}; // Only x is relevant in shader code
  240. vRimMaskControl[0] = params[info.m_nRimMask]->GetFloatValue();
  241. pShaderAPI->SetPixelShaderConstant( PSREG_FLASHLIGHT_ATTENUATION, vRimMaskControl, 1 );
  242. }
  243. // If it's all zeros, there was no constant tint in the vmt
  244. if ( (vSpecularTint[0] == 0.0f) && (vSpecularTint[1] == 0.0f) && (vSpecularTint[2] == 0.0f) )
  245. {
  246. if ( hasPhongTintMap ) // If we have a map to use, tell the shader
  247. {
  248. vSpecularTint[0] = -1;
  249. }
  250. else // Otherwise, just tint with white
  251. {
  252. vSpecularTint[0] = 1.0f;
  253. vSpecularTint[1] = 1.0f;
  254. vSpecularTint[2] = 1.0f;
  255. }
  256. }
  257. if ( (info.m_nPhongFresnelRanges != -1 ) && params[info.m_nPhongFresnelRanges]->IsDefined() )
  258. params[info.m_nPhongFresnelRanges]->GetVecValue( vFresnelRanges_SpecBoost, 3 ); // Grab optional fresnel range parameters
  259. if ( ( info.m_nPhongBoost != -1 ) &&params[info.m_nPhongBoost]->IsDefined() ) // Grab optional phong boost param
  260. vFresnelRanges_SpecBoost[3] = params[info.m_nPhongBoost]->GetFloatValue();
  261. else
  262. vFresnelRanges_SpecBoost[3] = 1.0f;
  263. pShaderAPI->SetPixelShaderConstant( PSREG_EYEPOS_SPEC_EXPONENT, vEyePos_SpecExponent, 1 );
  264. pShaderAPI->SetPixelShaderConstant( PSREG_FRESNEL_SPEC_PARAMS, vFresnelRanges_SpecBoost, 1 );
  265. pShaderAPI->SetPixelShaderConstant( PSREG_SPEC_RIM_PARAMS, vSpecularTint, 1 );
  266. pShaderAPI->SetPixelShaderConstant( PSREG_FLASHLIGHT_POSITION_RIM_BOOST, vRimBoost, 1 ); // Rim boost in w on non-flashlight pass
  267. pShaderAPI->SetPixelShaderFogParams( PSREG_FOG_PARAMS );
  268. // Set c0 and c1 to contain first two rows of ViewProj matrix
  269. VMatrix matView, matProj, matViewProj;
  270. pShaderAPI->GetMatrix( MATERIAL_VIEW, matView.m[0] );
  271. pShaderAPI->GetMatrix( MATERIAL_PROJECTION, matProj.m[0] );
  272. matViewProj = matView * matProj;
  273. pShaderAPI->SetPixelShaderConstant( 0, matViewProj.m[0], 2 );
  274. // Cloaking control constants
  275. float vCloakControls[4] = { params[info.m_nRefractAmount]->GetFloatValue(), params[info.m_nCloakFactor]->GetFloatValue(), 0.0f, 0.0f };
  276. pShaderAPI->SetPixelShaderConstant( 3, vCloakControls, 1 );
  277. }
  278. pShader->Draw();
  279. }