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.

309 lines
12 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #include "BaseVSShader.h"
  8. #include "multiblend_dx9_helper.h"
  9. #include "..\shaderapidx9\locald3dtypes.h"
  10. #include "convar.h"
  11. #include "cpp_shader_constant_register_map.h"
  12. #include "multiblend_vs20.inc"
  13. #include "multiblend_vs30.inc"
  14. #include "multiblend_ps20.inc"
  15. #include "multiblend_ps20b.inc"
  16. #include "multiblend_ps30.inc"
  17. // NOTE: This has to be the last file included!
  18. #include "tier0/memdbgon.h"
  19. // FIXME: doesn't support fresnel!
  20. void InitParamsMultiblend_DX9( CBaseVSShader *pShader, IMaterialVar** params, const char *pMaterialName, Multiblend_DX9_Vars_t &info )
  21. {
  22. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_LIGHTMAP );
  23. }
  24. void InitMultiblend_DX9( CBaseVSShader *pShader, IMaterialVar** params, Multiblend_DX9_Vars_t &info )
  25. {
  26. if ( params[ info.m_nBaseTexture ]->IsDefined() )
  27. {
  28. pShader->LoadTexture( info.m_nBaseTexture );
  29. }
  30. if ( info.m_nSpecTexture != -1 && params[ info.m_nSpecTexture ]->IsDefined() )
  31. {
  32. pShader->LoadTexture( info.m_nSpecTexture );
  33. }
  34. if ( info.m_nBaseTexture2 != -1 && params[ info.m_nBaseTexture2 ]->IsDefined() )
  35. {
  36. pShader->LoadTexture( info.m_nBaseTexture2 );
  37. }
  38. if ( info.m_nSpecTexture2 != -1 && params[ info.m_nSpecTexture2 ]->IsDefined() )
  39. {
  40. pShader->LoadTexture( info.m_nSpecTexture2 );
  41. }
  42. if ( info.m_nBaseTexture3 != -1 && params[ info.m_nBaseTexture3 ]->IsDefined() )
  43. {
  44. pShader->LoadTexture( info.m_nBaseTexture3 );
  45. }
  46. if ( info.m_nSpecTexture3 != -1 && params[ info.m_nSpecTexture3 ]->IsDefined() )
  47. {
  48. pShader->LoadTexture( info.m_nSpecTexture3 );
  49. }
  50. if ( info.m_nBaseTexture4 != -1 && params[ info.m_nBaseTexture4 ]->IsDefined() )
  51. {
  52. pShader->LoadTexture( info.m_nBaseTexture4 );
  53. }
  54. if ( info.m_nSpecTexture4 != -1 && params[ info.m_nSpecTexture4 ]->IsDefined() )
  55. {
  56. pShader->LoadTexture( info.m_nSpecTexture4 );
  57. }
  58. }
  59. void DrawMultiblend_DX9( CBaseVSShader *pShader, IMaterialVar** params, IShaderDynamicAPI *pShaderAPI,
  60. IShaderShadow* pShaderShadow, Multiblend_DX9_Vars_t &info, VertexCompressionType_t vertexCompression )
  61. {
  62. bool bIsModel = IS_FLAG_SET( MATERIAL_VAR_MODEL );
  63. int nLightingPreviewMode = IS_FLAG2_SET( MATERIAL_VAR2_USE_GBUFFER0 ) + 2 * IS_FLAG2_SET( MATERIAL_VAR2_USE_GBUFFER1 );
  64. bool bHasSpec1 = ( info.m_nSpecTexture != -1 && params[ info.m_nSpecTexture ]->IsDefined() );
  65. bool bHasSpec2 = ( info.m_nSpecTexture2 != -1 && params[ info.m_nSpecTexture2 ]->IsDefined() );
  66. bool bHasSpec3 = ( info.m_nSpecTexture3 != -1 && params[ info.m_nSpecTexture3 ]->IsDefined() );
  67. bool bHasSpec4 = ( info.m_nSpecTexture4 != -1 && params[ info.m_nSpecTexture4 ]->IsDefined() );
  68. bool bUsingEditor = pShader->CanUseEditorMaterials(); // pShader->UsingEditor( params );
  69. bool bSRGBLightMaps = ( g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE );
  70. SHADOW_STATE
  71. {
  72. pShader->SetInitialShadowState( );
  73. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  74. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  75. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  76. pShaderShadow->EnableTexture( SHADER_SAMPLER4, true );
  77. pShaderShadow->EnableTexture( SHADER_SAMPLER5, true );
  78. pShaderShadow->EnableTexture( SHADER_SAMPLER6, true );
  79. pShaderShadow->EnableTexture( SHADER_SAMPLER7, true );
  80. pShaderShadow->EnableTexture( SHADER_SAMPLER8, true );
  81. pShaderShadow->EnableTexture( SHADER_SAMPLER9, true );
  82. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, true ); // Always SRGB read on base map 1
  83. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER2, true ); // Always SRGB read on base map 2
  84. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER3, true ); // Always SRGB read on base map 3
  85. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER4, true ); // Always SRGB read on base map 4
  86. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER6, true ); // Always SRGB read on spec map 1
  87. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER7, true ); // Always SRGB read on spec map 1
  88. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER8, true ); // Always SRGB read on spec map 1
  89. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER9, true ); // Always SRGB read on spec map 1
  90. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER5, bSRGBLightMaps );
  91. pShaderShadow->EnableSRGBWrite( true );
  92. pShaderShadow->EnableAlphaWrites( true ); // writing water fog alpha always.
  93. unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL;
  94. int nTexCoordCount = 8;
  95. static int s_TexCoordSize[]={ 2, //
  96. 2, //
  97. 0, //
  98. 4, // alpha blend
  99. 4, // vertex / blend color 0
  100. 4, // vertex / blend color 1
  101. 4, // vertex / blend color 2
  102. 4 // vertex / blend color 3
  103. };
  104. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, s_TexCoordSize, 0 );
  105. #ifndef _X360
  106. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  107. #endif
  108. {
  109. DECLARE_STATIC_VERTEX_SHADER( multiblend_vs20 );
  110. SET_STATIC_VERTEX_SHADER_COMBO( SPECULAR, !bUsingEditor );
  111. SET_STATIC_VERTEX_SHADER_COMBO( MODEL, bIsModel );
  112. SET_STATIC_VERTEX_SHADER( multiblend_vs20 );
  113. // Bind ps_2_b shader so we can get Phong terms
  114. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  115. {
  116. DECLARE_STATIC_PIXEL_SHADER( multiblend_ps20b );
  117. SET_STATIC_PIXEL_SHADER_COMBO( LIGHTING_PREVIEW, nLightingPreviewMode );
  118. SET_STATIC_PIXEL_SHADER( multiblend_ps20b );
  119. }
  120. else
  121. {
  122. DECLARE_STATIC_PIXEL_SHADER( multiblend_ps20 );
  123. SET_STATIC_PIXEL_SHADER_COMBO( LIGHTING_PREVIEW, nLightingPreviewMode );
  124. SET_STATIC_PIXEL_SHADER( multiblend_ps20 );
  125. }
  126. }
  127. #ifndef _X360
  128. else
  129. {
  130. // The vertex shader uses the vertex id stream
  131. SET_FLAGS2( MATERIAL_VAR2_USES_VERTEXID );
  132. DECLARE_STATIC_VERTEX_SHADER( multiblend_vs30 );
  133. SET_STATIC_VERTEX_SHADER_COMBO( SPECULAR, !bUsingEditor );
  134. SET_STATIC_VERTEX_SHADER_COMBO( MODEL, bIsModel );
  135. SET_STATIC_VERTEX_SHADER( multiblend_vs30 );
  136. // Bind ps_2_b shader so we can get Phong terms
  137. DECLARE_STATIC_PIXEL_SHADER( multiblend_ps30 );
  138. SET_STATIC_PIXEL_SHADER_COMBO( LIGHTING_PREVIEW, nLightingPreviewMode );
  139. SET_STATIC_PIXEL_SHADER( multiblend_ps30 );
  140. }
  141. #endif
  142. pShader->DefaultFog();
  143. float flLScale = pShaderShadow->GetLightMapScaleFactor();
  144. // Lighting constants
  145. pShader->PI_BeginCommandBuffer();
  146. pShader->PI_SetPixelShaderAmbientLightCube( PSREG_AMBIENT_CUBE );
  147. // pShader->PI_SetPixelShaderLocalLighting( PSREG_LIGHT_INFO_ARRAY );
  148. pShader->PI_SetModulationPixelShaderDynamicState_LinearScale_ScaleInW( PSREG_CONSTANT_02, flLScale );
  149. pShader->PI_EndCommandBuffer();
  150. }
  151. DYNAMIC_STATE
  152. {
  153. pShaderAPI->SetDefaultState();
  154. // Bind textures
  155. pShader->BindTexture( SHADER_SAMPLER1, TEXTURE_BINDFLAGS_SRGBREAD, info.m_nBaseTexture ); // Base Map 1
  156. pShader->BindTexture( SHADER_SAMPLER2, TEXTURE_BINDFLAGS_SRGBREAD, info.m_nBaseTexture2 ); // Base Map 2
  157. pShader->BindTexture( SHADER_SAMPLER3, TEXTURE_BINDFLAGS_SRGBREAD, info.m_nBaseTexture3 ); // Base Map 3
  158. pShader->BindTexture( SHADER_SAMPLER4, TEXTURE_BINDFLAGS_SRGBREAD, info.m_nBaseTexture4 ); // Base Map 4
  159. if ( bHasSpec1 == true )
  160. {
  161. pShader->BindTexture( SHADER_SAMPLER6, TEXTURE_BINDFLAGS_SRGBREAD, info.m_nSpecTexture ); // Spec Map 1
  162. }
  163. else
  164. {
  165. pShaderAPI->BindStandardTexture( SHADER_SAMPLER6, TEXTURE_BINDFLAGS_SRGBREAD, TEXTURE_BLACK );
  166. }
  167. if ( bHasSpec2 == true )
  168. {
  169. pShader->BindTexture( SHADER_SAMPLER7, TEXTURE_BINDFLAGS_SRGBREAD, info.m_nSpecTexture2 ); // Spec Map 2
  170. }
  171. else
  172. {
  173. pShaderAPI->BindStandardTexture( SHADER_SAMPLER7, TEXTURE_BINDFLAGS_SRGBREAD, TEXTURE_BLACK );
  174. }
  175. if ( bHasSpec3 == true )
  176. {
  177. pShader->BindTexture( SHADER_SAMPLER8, TEXTURE_BINDFLAGS_SRGBREAD, info.m_nSpecTexture3 ); // Spec Map 3
  178. }
  179. else
  180. {
  181. pShaderAPI->BindStandardTexture( SHADER_SAMPLER8, TEXTURE_BINDFLAGS_SRGBREAD, TEXTURE_BLACK );
  182. }
  183. if ( bHasSpec4 == true )
  184. {
  185. pShader->BindTexture( SHADER_SAMPLER9, TEXTURE_BINDFLAGS_SRGBREAD, info.m_nSpecTexture4 ); // Spec Map 4
  186. }
  187. else
  188. {
  189. pShaderAPI->BindStandardTexture( SHADER_SAMPLER9, TEXTURE_BINDFLAGS_SRGBREAD, TEXTURE_BLACK );
  190. }
  191. pShaderAPI->BindStandardTexture( SHADER_SAMPLER5, ( bSRGBLightMaps ) ? TEXTURE_BINDFLAGS_SRGBREAD : TEXTURE_BINDFLAGS_NONE, TEXTURE_LIGHTMAP );
  192. Vector4D vRotations( DEG2RAD( params[ info.m_nRotation ]->GetFloatValue() ), DEG2RAD( params[ info.m_nRotation2 ]->GetFloatValue() ),
  193. DEG2RAD( params[ info.m_nRotation3 ]->GetFloatValue() ), DEG2RAD( params[ info.m_nRotation4 ]->GetFloatValue() ) );
  194. pShaderAPI->SetVertexShaderConstant( 27, vRotations.Base() );
  195. Vector4D vScales( params[ info.m_nScale ]->GetFloatValue() > 0.0f ? params[ info.m_nScale ]->GetFloatValue() : 1.0f,
  196. params[ info.m_nScale2 ]->GetFloatValue() > 0.0f ? params[ info.m_nScale2 ]->GetFloatValue() : 1.0f,
  197. params[ info.m_nScale3 ]->GetFloatValue() > 0.0f ? params[ info.m_nScale3 ]->GetFloatValue() : 1.0f,
  198. params[ info.m_nScale4 ]->GetFloatValue() > 0.0f ? params[ info.m_nScale4 ]->GetFloatValue() : 1.0f );
  199. pShaderAPI->SetVertexShaderConstant( 28, vScales.Base() );
  200. Vector4D vLightDir;
  201. vLightDir.AsVector3D() = pShaderAPI->GetVectorRenderingParameter( VECTOR_RENDERPARM_GLOBAL_LIGHT_DIRECTION );
  202. vLightDir.w = pShaderAPI->GetFloatRenderingParameter( FLOAT_RENDERPARM_SPECULAR_POWER );
  203. pShaderAPI->SetVertexShaderConstant( 29, vLightDir.Base() );
  204. LightState_t lightState = {0, false, false};
  205. pShaderAPI->GetDX9LightState( &lightState );
  206. #ifndef _X360
  207. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  208. #endif
  209. {
  210. DECLARE_DYNAMIC_VERTEX_SHADER( multiblend_vs20 );
  211. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  212. SET_DYNAMIC_VERTEX_SHADER( multiblend_vs20 );
  213. // Bind ps_2_b shader so we can get Phong, rim and a cloudier refraction
  214. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  215. {
  216. DECLARE_DYNAMIC_PIXEL_SHADER( multiblend_ps20b );
  217. SET_DYNAMIC_PIXEL_SHADER( multiblend_ps20b );
  218. }
  219. else
  220. {
  221. DECLARE_DYNAMIC_PIXEL_SHADER( multiblend_ps20 );
  222. SET_DYNAMIC_PIXEL_SHADER( multiblend_ps20 );
  223. }
  224. }
  225. #ifndef _X360
  226. else
  227. {
  228. DECLARE_DYNAMIC_VERTEX_SHADER( multiblend_vs30 );
  229. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  230. SET_DYNAMIC_VERTEX_SHADER( multiblend_vs30 );
  231. DECLARE_DYNAMIC_PIXEL_SHADER( multiblend_ps30 );
  232. SET_DYNAMIC_PIXEL_SHADER( multiblend_ps30 );
  233. }
  234. #endif
  235. pShader->SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_6, info.m_nBaseTextureTransform );
  236. pShaderAPI->SetPixelShaderFogParams( PSREG_FOG_PARAMS );
  237. // Pack phong exponent in with the eye position
  238. float vEyePos_SpecExponent[4];
  239. float vSpecularTint[4] = {1, 1, 1, 1};
  240. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos_SpecExponent );
  241. // if ( (info.m_nPhongExponent != -1) && params[info.m_nPhongExponent]->IsDefined() )
  242. // vEyePos_SpecExponent[3] = params[info.m_nPhongExponent]->GetFloatValue(); // This overrides the channel in the map
  243. // else
  244. vEyePos_SpecExponent[3] = 0; // Use the alpha channel of the normal map for the exponent
  245. // If it's all zeros, there was no constant tint in the vmt
  246. if ( (vSpecularTint[0] == 0.0f) && (vSpecularTint[1] == 0.0f) && (vSpecularTint[2] == 0.0f) )
  247. {
  248. vSpecularTint[0] = 1.0f;
  249. vSpecularTint[1] = 1.0f;
  250. vSpecularTint[2] = 1.0f;
  251. }
  252. pShaderAPI->SetPixelShaderConstant( PSREG_EYEPOS_SPEC_EXPONENT, vEyePos_SpecExponent, 1 );
  253. // Set c0 and c1 to contain first two rows of ViewProj matrix
  254. VMatrix matView, matProj, matViewProj;
  255. pShaderAPI->GetMatrix( MATERIAL_VIEW, matView.m[0] );
  256. pShaderAPI->GetMatrix( MATERIAL_PROJECTION, matProj.m[0] );
  257. matViewProj = matView * matProj;
  258. pShaderAPI->SetPixelShaderConstant( 0, matViewProj.m[0], 2 );
  259. pShaderAPI->SetPixelShaderFogParams( PSREG_FOG_PARAMS );
  260. }
  261. pShader->Draw();
  262. }