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.

807 lines
27 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Header: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "BaseVSShader.h"
  9. #include "vertexlitgeneric_vs11.inc"
  10. #include "vertexlitgeneric_selfillumonly.inc"
  11. #include "emissive_scroll_blended_pass_helper.h"
  12. #include "flesh_interior_blended_pass_helper.h"
  13. #include "cloak_blended_pass_helper.h"
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include "tier0/memdbgon.h"
  16. DEFINE_FALLBACK_SHADER( VertexLitGeneric, VertexLitGeneric_DX8 )
  17. DEFINE_FALLBACK_SHADER( Skin_DX9, VertexLitGeneric_DX8 )
  18. BEGIN_VS_SHADER( VertexLitGeneric_DX8,
  19. "Help for VertexLitGeneric" )
  20. BEGIN_SHADER_PARAMS
  21. SHADER_PARAM( SELFILLUMTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "Self-illumination tint" )
  22. SHADER_PARAM( DETAIL, SHADER_PARAM_TYPE_TEXTURE, "shadertest/detail", "detail texture" )
  23. SHADER_PARAM( DETAILSCALE, SHADER_PARAM_TYPE_FLOAT, "4", "scale of the detail texture" )
  24. SHADER_PARAM( DETAILFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "frame number for $detail" )
  25. SHADER_PARAM( ENVMAP, SHADER_PARAM_TYPE_TEXTURE, "shadertest/shadertest_env", "envmap" )
  26. SHADER_PARAM( ENVMAPFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "envmap frame number" )
  27. SHADER_PARAM( ENVMAPMASK, SHADER_PARAM_TYPE_TEXTURE, "shadertest/shadertest_envmask", "envmap mask" )
  28. SHADER_PARAM( ENVMAPMASKFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  29. SHADER_PARAM( ENVMAPMASKSCALE, SHADER_PARAM_TYPE_FLOAT, "1", "envmap mask scale" )
  30. SHADER_PARAM( ENVMAPTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "envmap tint" )
  31. SHADER_PARAM( BUMPMAP, SHADER_PARAM_TYPE_TEXTURE, "models/shadertest/shader1_normal", "bump map" )
  32. SHADER_PARAM( BUMPFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "frame number for $bumpmap" )
  33. SHADER_PARAM( BUMPTRANSFORM, SHADER_PARAM_TYPE_MATRIX, "center .5 .5 scale 1 1 rotate 0 translate 0 0", "$bumpmap texcoord transform" )
  34. SHADER_PARAM( ENVMAPCONTRAST, SHADER_PARAM_TYPE_FLOAT, "0.0", "contrast 0 == normal 1 == color*color" )
  35. SHADER_PARAM( ENVMAPSATURATION, SHADER_PARAM_TYPE_FLOAT, "1.0", "saturation 0 == greyscale 1 == normal" )
  36. SHADER_PARAM( ENVMAPOPTIONAL, SHADER_PARAM_TYPE_BOOL, "0", "Make the envmap only apply to dx9 and higher hardware" )
  37. SHADER_PARAM( FORCEBUMP, SHADER_PARAM_TYPE_BOOL, "0", "0 == Do bumpmapping if the card says it can handle it. 1 == Always do bumpmapping." )
  38. SHADER_PARAM( ALPHATESTREFERENCE, SHADER_PARAM_TYPE_FLOAT, "0.0", "" )
  39. SHADER_PARAM( DETAILBLENDMODE, SHADER_PARAM_TYPE_INTEGER, "0", "mode for combining detail texture with base. 0=normal, 1= additive, 2=alpha blend detail over base, 3=crossfade" )
  40. SHADER_PARAM( DETAILBLENDFACTOR, SHADER_PARAM_TYPE_FLOAT, "1", "blend amount for detail texture." )
  41. // Emissive Scroll Pass
  42. SHADER_PARAM( EMISSIVEBLENDENABLED, SHADER_PARAM_TYPE_BOOL, "0", "Enable emissive blend pass" )
  43. SHADER_PARAM( EMISSIVEBLENDBASETEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "self-illumination map" )
  44. SHADER_PARAM( EMISSIVEBLENDSCROLLVECTOR, SHADER_PARAM_TYPE_VEC2, "[0.11 0.124]", "Emissive scroll vec" )
  45. SHADER_PARAM( EMISSIVEBLENDSTRENGTH, SHADER_PARAM_TYPE_FLOAT, "1.0", "Emissive blend strength" )
  46. SHADER_PARAM( EMISSIVEBLENDTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "self-illumination map" )
  47. SHADER_PARAM( EMISSIVEBLENDTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "Self-illumination tint" )
  48. SHADER_PARAM( TIME, SHADER_PARAM_TYPE_FLOAT, "0.0", "Needs CurrentTime Proxy" )
  49. // Cloak Pass
  50. SHADER_PARAM( CLOAKPASSENABLED, SHADER_PARAM_TYPE_BOOL, "0", "Enables cloak render in a second pass" )
  51. SHADER_PARAM( CLOAKFACTOR, SHADER_PARAM_TYPE_FLOAT, "0.0", "" )
  52. SHADER_PARAM( CLOAKCOLORTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "Cloak color tint" )
  53. SHADER_PARAM( REFRACTAMOUNT, SHADER_PARAM_TYPE_FLOAT, "2", "" )
  54. // Flesh Interior Pass
  55. SHADER_PARAM( FLESHINTERIORENABLED, SHADER_PARAM_TYPE_BOOL, "0", "Enable Flesh interior blend pass" )
  56. SHADER_PARAM( FLESHINTERIORTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "Flesh color texture" )
  57. SHADER_PARAM( FLESHINTERIORNOISETEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "Flesh noise texture" )
  58. SHADER_PARAM( FLESHBORDERTEXTURE1D, SHADER_PARAM_TYPE_TEXTURE, "", "Flesh border 1D texture" )
  59. SHADER_PARAM( FLESHNORMALTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "Flesh normal texture" )
  60. SHADER_PARAM( FLESHSUBSURFACETEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "Flesh subsurface texture" )
  61. SHADER_PARAM( FLESHCUBETEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "Flesh cubemap texture" )
  62. SHADER_PARAM( FLESHBORDERNOISESCALE, SHADER_PARAM_TYPE_FLOAT, "1.5", "Flesh Noise UV scalar for border" )
  63. SHADER_PARAM( FLESHDEBUGFORCEFLESHON, SHADER_PARAM_TYPE_BOOL, "0", "Flesh Debug full flesh" )
  64. SHADER_PARAM( FLESHEFFECTCENTERRADIUS1, SHADER_PARAM_TYPE_VEC4, "[0 0 0 0.001]", "Flesh effect center and radius" )
  65. SHADER_PARAM( FLESHEFFECTCENTERRADIUS2, SHADER_PARAM_TYPE_VEC4, "[0 0 0 0.001]", "Flesh effect center and radius" )
  66. SHADER_PARAM( FLESHEFFECTCENTERRADIUS3, SHADER_PARAM_TYPE_VEC4, "[0 0 0 0.001]", "Flesh effect center and radius" )
  67. SHADER_PARAM( FLESHEFFECTCENTERRADIUS4, SHADER_PARAM_TYPE_VEC4, "[0 0 0 0.001]", "Flesh effect center and radius" )
  68. SHADER_PARAM( FLESHSUBSURFACETINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "Subsurface Color" )
  69. SHADER_PARAM( FLESHBORDERWIDTH, SHADER_PARAM_TYPE_FLOAT, "0.3", "Flesh border" )
  70. SHADER_PARAM( FLESHBORDERSOFTNESS, SHADER_PARAM_TYPE_FLOAT, "0.42", "Flesh border softness (> 0.0 && <= 0.5)" )
  71. SHADER_PARAM( FLESHBORDERTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "Flesh border Color" )
  72. SHADER_PARAM( FLESHGLOBALOPACITY, SHADER_PARAM_TYPE_FLOAT, "1.0", "Flesh global opacity" )
  73. SHADER_PARAM( FLESHGLOSSBRIGHTNESS, SHADER_PARAM_TYPE_FLOAT, "0.66", "Flesh gloss brightness" )
  74. SHADER_PARAM( FLESHSCROLLSPEED, SHADER_PARAM_TYPE_FLOAT, "1.0", "Flesh scroll speed" )
  75. // Color Replacement Pass
  76. SHADER_PARAM( BLENDTINTBYBASEALPHA, SHADER_PARAM_TYPE_BOOL, "0", "Use the base alpha to blend in the $color modulation")
  77. SHADER_PARAM( BLENDTINTCOLOROVERBASE, SHADER_PARAM_TYPE_FLOAT, "0", "blend between tint acting as a multiplication versus a replace" )
  78. END_SHADER_PARAMS
  79. // Cloak Pass
  80. void SetupVarsCloakBlendedPass( CloakBlendedPassVars_t &info )
  81. {
  82. info.m_nCloakFactor = CLOAKFACTOR;
  83. info.m_nCloakColorTint = CLOAKCOLORTINT;
  84. info.m_nRefractAmount = REFRACTAMOUNT;
  85. // Delete these lines if not bump mapping!
  86. info.m_nBumpmap = BUMPMAP;
  87. info.m_nBumpFrame = BUMPFRAME;
  88. info.m_nBumpTransform = BUMPTRANSFORM;
  89. }
  90. bool NeedsPowerOfTwoFrameBufferTexture( IMaterialVar **params, bool bCheckSpecificToThisFrame ) const
  91. {
  92. if ( params[CLOAKPASSENABLED]->GetIntValue() ) // If material supports cloaking
  93. {
  94. if ( bCheckSpecificToThisFrame == false ) // For setting model flag at load time
  95. return true;
  96. else if ( ( params[CLOAKFACTOR]->GetFloatValue() > 0.0f ) && ( params[CLOAKFACTOR]->GetFloatValue() < 1.0f ) ) // Per-frame check
  97. return true;
  98. // else, not cloaking this frame, so check flag2 in case the base material still needs it
  99. }
  100. // Check flag2 if not drawing cloak pass
  101. return IS_FLAG2_SET( MATERIAL_VAR2_NEEDS_POWER_OF_TWO_FRAME_BUFFER_TEXTURE );
  102. }
  103. bool IsTranslucent( IMaterialVar **params ) const
  104. {
  105. if ( params[CLOAKPASSENABLED]->GetIntValue() ) // If material supports cloaking
  106. {
  107. if ( ( params[CLOAKFACTOR]->GetFloatValue() > 0.0f ) && ( params[CLOAKFACTOR]->GetFloatValue() < 1.0f ) ) // Per-frame check
  108. return true;
  109. // else, not cloaking this frame, so check flag in case the base material still needs it
  110. }
  111. // Check flag if not drawing cloak pass
  112. return IS_FLAG_SET( MATERIAL_VAR_TRANSLUCENT );
  113. }
  114. // Emissive Scroll Pass
  115. void SetupVarsEmissiveScrollBlendedPass( EmissiveScrollBlendedPassVars_t &info )
  116. {
  117. info.m_nBlendStrength = EMISSIVEBLENDSTRENGTH;
  118. info.m_nBaseTexture = EMISSIVEBLENDBASETEXTURE;
  119. info.m_nFlowTexture = -1; // Not used in DX8
  120. info.m_nEmissiveTexture = EMISSIVEBLENDTEXTURE;
  121. info.m_nEmissiveTint = EMISSIVEBLENDTINT;
  122. info.m_nEmissiveScrollVector = EMISSIVEBLENDSCROLLVECTOR;
  123. info.m_nTime = TIME;
  124. }
  125. // Flesh Interior Pass
  126. void SetupVarsFleshInteriorBlendedPass( FleshInteriorBlendedPassVars_t &info )
  127. {
  128. info.m_nFleshTexture = FLESHINTERIORTEXTURE;
  129. info.m_nFleshNoiseTexture = FLESHINTERIORNOISETEXTURE;
  130. info.m_nFleshBorderTexture1D = FLESHBORDERTEXTURE1D;
  131. info.m_nFleshNormalTexture = FLESHNORMALTEXTURE;
  132. info.m_nFleshSubsurfaceTexture = FLESHSUBSURFACETEXTURE;
  133. info.m_nFleshCubeTexture = FLESHCUBETEXTURE;
  134. info.m_nflBorderNoiseScale = FLESHBORDERNOISESCALE;
  135. info.m_nflDebugForceFleshOn = FLESHDEBUGFORCEFLESHON;
  136. info.m_nvEffectCenterRadius1 = FLESHEFFECTCENTERRADIUS1;
  137. info.m_nvEffectCenterRadius2 = FLESHEFFECTCENTERRADIUS2;
  138. info.m_nvEffectCenterRadius3 = FLESHEFFECTCENTERRADIUS3;
  139. info.m_nvEffectCenterRadius4 = FLESHEFFECTCENTERRADIUS4;
  140. info.m_ncSubsurfaceTint = FLESHSUBSURFACETINT;
  141. info.m_nflBorderWidth = FLESHBORDERWIDTH;
  142. info.m_nflBorderSoftness = FLESHBORDERSOFTNESS;
  143. info.m_ncBorderTint = FLESHBORDERTINT;
  144. info.m_nflGlobalOpacity = FLESHGLOBALOPACITY;
  145. info.m_nflGlossBrightness = FLESHGLOSSBRIGHTNESS;
  146. info.m_nflScrollSpeed = FLESHSCROLLSPEED;
  147. info.m_nTime = TIME;
  148. }
  149. SHADER_INIT_PARAMS()
  150. {
  151. // FLASHLIGHTFIXME
  152. params[FLASHLIGHTTEXTURE]->SetStringValue( "effects/flashlight001" );
  153. // We don't want no stinking bump mapping on models in dx8.
  154. // Wait a minute! We want specular bump. .need to make that work by itself.
  155. // params[BUMPMAP]->SetUndefined();
  156. // if( IS_FLAG_SET( MATERIAL_VAR_NORMALMAPALPHAENVMAPMASK ) )
  157. // {
  158. // CLEAR_FLAGS( MATERIAL_VAR_NORMALMAPALPHAENVMAPMASK );
  159. // params[ENVMAP]->SetUndefined();
  160. // }
  161. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  162. if( !params[ENVMAPMASKSCALE]->IsDefined() )
  163. params[ENVMAPMASKSCALE]->SetFloatValue( 1.0f );
  164. if( !params[ENVMAPMASKFRAME]->IsDefined() )
  165. params[ENVMAPMASKFRAME]->SetIntValue( 0 );
  166. if( !params[ENVMAPTINT]->IsDefined() )
  167. params[ENVMAPTINT]->SetVecValue( 1.0f, 1.0f, 1.0f );
  168. if( !params[SELFILLUMTINT]->IsDefined() )
  169. params[SELFILLUMTINT]->SetVecValue( 1.0f, 1.0f, 1.0f );
  170. if( !params[DETAILSCALE]->IsDefined() )
  171. params[DETAILSCALE]->SetFloatValue( 4.0f );
  172. if( !params[DETAILBLENDFACTOR]->IsDefined() )
  173. params[DETAILBLENDFACTOR]->SetFloatValue( 1.0f );
  174. if( !params[DETAILBLENDMODE]->IsDefined() )
  175. params[DETAILBLENDMODE]->SetFloatValue( 0 );
  176. if( !params[ENVMAPCONTRAST]->IsDefined() )
  177. params[ENVMAPCONTRAST]->SetFloatValue( 0.0f );
  178. if( !params[ENVMAPSATURATION]->IsDefined() )
  179. params[ENVMAPSATURATION]->SetFloatValue( 1.0f );
  180. if( !params[ENVMAPFRAME]->IsDefined() )
  181. params[ENVMAPFRAME]->SetIntValue( 0 );
  182. if( !params[BUMPFRAME]->IsDefined() )
  183. params[BUMPFRAME]->SetIntValue( 0 );
  184. if( !params[ALPHATESTREFERENCE]->IsDefined() )
  185. params[ALPHATESTREFERENCE]->SetFloatValue( 0.0f );
  186. // No texture means no self-illum or env mask in base alpha
  187. if ( !params[BASETEXTURE]->IsDefined() )
  188. {
  189. CLEAR_FLAGS( MATERIAL_VAR_SELFILLUM );
  190. CLEAR_FLAGS( MATERIAL_VAR_BASEALPHAENVMAPMASK );
  191. }
  192. // If in decal mode, no debug override...
  193. if (IS_FLAG_SET(MATERIAL_VAR_DECAL))
  194. {
  195. SET_FLAGS( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  196. }
  197. if( g_pConfig->UseBumpmapping() && params[BUMPMAP]->IsDefined() )
  198. {
  199. SET_FLAGS2( MATERIAL_VAR2_NEEDS_TANGENT_SPACES );
  200. }
  201. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_VERTEX_LIT );
  202. // Get rid of the envmap if it's optional for this dx level.
  203. if( params[ENVMAPOPTIONAL]->IsDefined() && params[ENVMAPOPTIONAL]->GetIntValue() )
  204. {
  205. params[ENVMAP]->SetUndefined();
  206. }
  207. // If mat_specular 0, then get rid of envmap
  208. if( !g_pConfig->UseSpecular() && params[ENVMAP]->IsDefined() && params[BASETEXTURE]->IsDefined() )
  209. {
  210. params[ENVMAP]->SetUndefined();
  211. }
  212. // If a bumpmap is defined but an envmap isn't, then ignore the bumpmap.
  213. // It was meant to be used with diffuse
  214. if ( !params[ENVMAP]->IsDefined() )
  215. {
  216. params[BUMPMAP]->SetUndefined();
  217. }
  218. // Cloak Pass
  219. if ( !params[CLOAKPASSENABLED]->IsDefined() )
  220. {
  221. params[CLOAKPASSENABLED]->SetIntValue( 0 );
  222. }
  223. else if ( params[CLOAKPASSENABLED]->GetIntValue() )
  224. {
  225. CloakBlendedPassVars_t info;
  226. SetupVarsCloakBlendedPass( info );
  227. InitParamsCloakBlendedPass( this, params, pMaterialName, info );
  228. }
  229. // Emissive Scroll Pass
  230. if ( !params[EMISSIVEBLENDENABLED]->IsDefined() )
  231. {
  232. params[EMISSIVEBLENDENABLED]->SetIntValue( 0 );
  233. }
  234. else if ( params[EMISSIVEBLENDENABLED]->GetIntValue() )
  235. {
  236. EmissiveScrollBlendedPassVars_t info;
  237. SetupVarsEmissiveScrollBlendedPass( info );
  238. InitParamsEmissiveScrollBlendedPass( this, params, pMaterialName, info );
  239. }
  240. // Flesh Interior Pass
  241. if ( !params[FLESHINTERIORENABLED]->IsDefined() )
  242. {
  243. params[FLESHINTERIORENABLED]->SetIntValue( 0 );
  244. }
  245. else if ( params[FLESHINTERIORENABLED]->GetIntValue() )
  246. {
  247. FleshInteriorBlendedPassVars_t info;
  248. SetupVarsFleshInteriorBlendedPass( info );
  249. InitParamsFleshInteriorBlendedPass( this, params, pMaterialName, info );
  250. }
  251. // Color Replacement Pass
  252. if ( !params[BLENDTINTBYBASEALPHA]->IsDefined() )
  253. {
  254. params[BLENDTINTBYBASEALPHA]->SetIntValue(0);
  255. }
  256. if ( !params[BLENDTINTCOLOROVERBASE]->IsDefined() )
  257. {
  258. params[BLENDTINTCOLOROVERBASE]->SetFloatValue(0);
  259. }
  260. }
  261. SHADER_FALLBACK
  262. {
  263. if ( IsPC() )
  264. {
  265. if ( g_pHardwareConfig->GetDXSupportLevel() < 70)
  266. return "VertexLitGeneric_DX6";
  267. if ( g_pHardwareConfig->GetDXSupportLevel() < 80)
  268. return "VertexLitGeneric_DX7";
  269. if ( g_pHardwareConfig->PreferReducedFillrate() )
  270. return "VertexLitGeneric_NoBump_DX8";
  271. }
  272. return 0;
  273. }
  274. SHADER_INIT
  275. {
  276. LoadTexture( FLASHLIGHTTEXTURE );
  277. if (params[BASETEXTURE]->IsDefined())
  278. {
  279. LoadTexture( BASETEXTURE );
  280. if (!params[BASETEXTURE]->GetTextureValue()->IsTranslucent())
  281. {
  282. CLEAR_FLAGS( MATERIAL_VAR_SELFILLUM );
  283. CLEAR_FLAGS( MATERIAL_VAR_BASEALPHAENVMAPMASK );
  284. }
  285. }
  286. if (params[DETAIL]->IsDefined())
  287. {
  288. LoadTexture( DETAIL );
  289. }
  290. if (g_pConfig->UseBumpmapping() && params[BUMPMAP]->IsDefined())
  291. {
  292. LoadBumpMap( BUMPMAP );
  293. }
  294. // Don't alpha test if the alpha channel is used for other purposes
  295. if (IS_FLAG_SET(MATERIAL_VAR_SELFILLUM) || IS_FLAG_SET(MATERIAL_VAR_BASEALPHAENVMAPMASK) )
  296. {
  297. CLEAR_FLAGS( MATERIAL_VAR_ALPHATEST );
  298. }
  299. if (params[ENVMAP]->IsDefined())
  300. {
  301. if( !IS_FLAG_SET(MATERIAL_VAR_ENVMAPSPHERE) )
  302. {
  303. LoadCubeMap( ENVMAP );
  304. }
  305. else
  306. {
  307. LoadTexture( ENVMAP );
  308. }
  309. if( !g_pHardwareConfig->SupportsCubeMaps() )
  310. {
  311. SET_FLAGS( MATERIAL_VAR_ENVMAPSPHERE );
  312. }
  313. if (params[ENVMAPMASK]->IsDefined())
  314. {
  315. LoadTexture( ENVMAPMASK );
  316. }
  317. }
  318. // Cloak Pass
  319. if ( params[CLOAKPASSENABLED]->GetIntValue() )
  320. {
  321. CloakBlendedPassVars_t info;
  322. SetupVarsCloakBlendedPass( info );
  323. InitCloakBlendedPass( this, params, info );
  324. }
  325. // Emissive Scroll Pass
  326. if ( params[EMISSIVEBLENDENABLED]->GetIntValue() )
  327. {
  328. EmissiveScrollBlendedPassVars_t info;
  329. SetupVarsEmissiveScrollBlendedPass( info );
  330. InitEmissiveScrollBlendedPass( this, params, info );
  331. }
  332. // Flesh Interior Pass
  333. if ( params[FLESHINTERIORENABLED]->GetIntValue() )
  334. {
  335. FleshInteriorBlendedPassVars_t info;
  336. SetupVarsFleshInteriorBlendedPass( info );
  337. InitFleshInteriorBlendedPass( this, params, info );
  338. }
  339. }
  340. inline const char *GetUnbumpedPixelShaderName( IMaterialVar** params, bool bSkipEnvmap )
  341. {
  342. static char const* s_pPixelShaders[] =
  343. {
  344. "VertexLitGeneric_EnvmapV2",
  345. "VertexLitGeneric_SelfIlluminatedEnvmapV2",
  346. "VertexLitGeneric_BaseAlphaMaskedEnvmapV2",
  347. "VertexLitGeneric_SelfIlluminatedEnvmapV2",
  348. // Env map mask
  349. "VertexLitGeneric_MaskedEnvmapV2",
  350. "VertexLitGeneric_SelfIlluminatedMaskedEnvmapV2",
  351. "VertexLitGeneric_MaskedEnvmapV2",
  352. "VertexLitGeneric_SelfIlluminatedMaskedEnvmapV2",
  353. // Detail
  354. "VertexLitGeneric_DetailEnvmapV2",
  355. "VertexLitGeneric_DetailSelfIlluminatedEnvmapV2",
  356. "VertexLitGeneric_DetailBaseAlphaMaskedEnvmapV2",
  357. "VertexLitGeneric_DetailSelfIlluminatedEnvmapV2",
  358. // Env map mask
  359. "VertexLitGeneric_DetailMaskedEnvmapV2",
  360. "VertexLitGeneric_DetailSelfIlluminatedMaskedEnvmapV2",
  361. "VertexLitGeneric_DetailMaskedEnvmapV2",
  362. "VertexLitGeneric_DetailSelfIlluminatedMaskedEnvmapV2",
  363. };
  364. if ( !params[BASETEXTURE]->IsTexture() )
  365. {
  366. if (params[ENVMAP]->IsTexture() && !bSkipEnvmap )
  367. {
  368. if (!params[ENVMAPMASK]->IsTexture())
  369. {
  370. return "VertexLitGeneric_EnvmapNoTexture";
  371. }
  372. else
  373. {
  374. return "VertexLitGeneric_MaskedEnvmapNoTexture";
  375. }
  376. }
  377. else
  378. {
  379. if ( params[DETAIL]->IsTexture() )
  380. {
  381. return "VertexLitGeneric_DetailNoTexture";
  382. }
  383. else
  384. {
  385. return "VertexLitGeneric_NoTexture";
  386. }
  387. }
  388. }
  389. else
  390. {
  391. if ( params[BLENDTINTBYBASEALPHA]->GetIntValue() )
  392. {
  393. return "VertexLitGeneric_BlendTint";
  394. }
  395. else if ( params[ENVMAP]->IsTexture() && !bSkipEnvmap )
  396. {
  397. int pshIndex = 0;
  398. if (IS_FLAG_SET(MATERIAL_VAR_SELFILLUM))
  399. pshIndex |= 0x1;
  400. if (IS_FLAG_SET(MATERIAL_VAR_BASEALPHAENVMAPMASK))
  401. pshIndex |= 0x2;
  402. if (params[ENVMAPMASK]->IsTexture())
  403. pshIndex |= 0x4;
  404. if (params[DETAIL]->IsTexture())
  405. pshIndex |= 0x8;
  406. return s_pPixelShaders[pshIndex];
  407. }
  408. else
  409. {
  410. if (IS_FLAG_SET(MATERIAL_VAR_SELFILLUM))
  411. {
  412. if ( params[DETAIL]->IsTexture() )
  413. return "VertexLitGeneric_DetailSelfIlluminated";
  414. else
  415. return "VertexLitGeneric_SelfIlluminated";
  416. }
  417. else if ( params[DETAIL]->IsTexture() )
  418. {
  419. switch( params[DETAILBLENDMODE]->GetIntValue() )
  420. {
  421. case 0:
  422. return "VertexLitGeneric_Detail";
  423. case 1: // additive modes
  424. return "VertexLitGeneric_Detail_Additive";
  425. case 5:
  426. case 6:
  427. return "VertexLitGeneric_Detail_Additive_selfillum";
  428. break;
  429. default:
  430. return "VertexLitGeneric_Detail_LerpBase";
  431. }
  432. }
  433. else
  434. {
  435. return "VertexLitGeneric";
  436. }
  437. }
  438. }
  439. }
  440. void DrawUnbumpedUsingVertexShader( IMaterialVar** params, IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow, bool bSkipEnvmap )
  441. {
  442. SHADOW_STATE
  443. {
  444. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  445. pShaderShadow->EnableAlphaTest( IS_FLAG_SET(MATERIAL_VAR_ALPHATEST) );
  446. if ( params[ALPHATESTREFERENCE]->GetFloatValue() > 0.0f )
  447. {
  448. pShaderShadow->AlphaFunc( SHADER_ALPHAFUNC_GEQUAL, params[ALPHATESTREFERENCE]->GetFloatValue() );
  449. }
  450. int fmt = VERTEX_POSITION | VERTEX_NORMAL;
  451. // FIXME: We could enable this, but we'd never get it working on dx7 or lower
  452. // FIXME: This isn't going to work until we make more vertex shaders that
  453. // pass the vertex color and alpha values through.
  454. #if 0
  455. if ( IS_FLAG_SET( MATERIAL_VAR_VERTEXCOLOR ) || IS_FLAG_SET( MATERIAL_VAR_VERTEXALPHA ) )
  456. fmt |= VERTEX_COLOR;
  457. #endif
  458. if (params[ENVMAP]->IsTexture() && !bSkipEnvmap )
  459. {
  460. // envmap on stage 1
  461. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  462. // envmapmask on stage 2
  463. if (params[ENVMAPMASK]->IsTexture() || IS_FLAG_SET(MATERIAL_VAR_BASEALPHAENVMAPMASK ) )
  464. {
  465. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  466. }
  467. }
  468. if (params[BASETEXTURE]->IsTexture())
  469. {
  470. SetDefaultBlendingShadowState( BASETEXTURE, true );
  471. }
  472. else
  473. {
  474. SetDefaultBlendingShadowState( ENVMAPMASK, false );
  475. }
  476. if ( params[DETAIL]->IsTexture())
  477. {
  478. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  479. }
  480. pShaderShadow->VertexShaderVertexFormat( fmt, 1, 0, 0 );
  481. // Set up the vertex shader index.
  482. vertexlitgeneric_vs11_Static_Index vshIndex;
  483. vshIndex.SetHALF_LAMBERT( IS_FLAG_SET( MATERIAL_VAR_HALFLAMBERT ) );
  484. //vshIndex.SetDETAIL( params[DETAIL]->IsTexture() );
  485. if( params[ENVMAP]->IsTexture() && !bSkipEnvmap )
  486. {
  487. vshIndex.SetENVMAP( true );
  488. vshIndex.SetENVMAPCAMERASPACE( IS_FLAG_SET(MATERIAL_VAR_ENVMAPCAMERASPACE) );
  489. if( IS_FLAG_SET(MATERIAL_VAR_ENVMAPCAMERASPACE) )
  490. {
  491. vshIndex.SetENVMAPSPHERE( false );
  492. }
  493. else
  494. {
  495. vshIndex.SetENVMAPSPHERE( IS_FLAG_SET( MATERIAL_VAR_ENVMAPSPHERE ) );
  496. }
  497. }
  498. else
  499. {
  500. vshIndex.SetENVMAP( false );
  501. vshIndex.SetENVMAPCAMERASPACE( false );
  502. vshIndex.SetENVMAPSPHERE( false );
  503. }
  504. pShaderShadow->SetVertexShader( "vertexlitgeneric_vs11", vshIndex.GetIndex() );
  505. const char *pshName = GetUnbumpedPixelShaderName( params, bSkipEnvmap );
  506. pShaderShadow->SetPixelShader( pshName );
  507. DefaultFog();
  508. pShaderShadow->EnableAlphaWrites( true );
  509. }
  510. DYNAMIC_STATE
  511. {
  512. if (params[BASETEXTURE]->IsTexture())
  513. {
  514. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  515. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, BASETEXTURETRANSFORM );
  516. }
  517. // if (params[ENVMAP]->IsTexture())
  518. if (params[ENVMAP]->IsTexture() && !bSkipEnvmap )
  519. {
  520. BindTexture( SHADER_SAMPLER1, ENVMAP, ENVMAPFRAME );
  521. if (params[ENVMAPMASK]->IsTexture() || IS_FLAG_SET(MATERIAL_VAR_BASEALPHAENVMAPMASK) )
  522. {
  523. if (params[ENVMAPMASK]->IsTexture() )
  524. BindTexture( SHADER_SAMPLER2, ENVMAPMASK, ENVMAPMASKFRAME );
  525. else
  526. BindTexture( SHADER_SAMPLER2, BASETEXTURE, FRAME );
  527. SetVertexShaderTextureScaledTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_2, BASETEXTURETRANSFORM, ENVMAPMASKSCALE );
  528. }
  529. if (IS_FLAG_SET(MATERIAL_VAR_ENVMAPSPHERE) ||
  530. IS_FLAG_SET(MATERIAL_VAR_ENVMAPCAMERASPACE))
  531. {
  532. LoadViewMatrixIntoVertexShaderConstant( VERTEX_SHADER_VIEWMODEL );
  533. }
  534. SetEnvMapTintPixelShaderDynamicState( 2, ENVMAPTINT, -1 );
  535. }
  536. if ( params[DETAIL]->IsTexture())
  537. {
  538. BindTexture( SHADER_SAMPLER3, DETAIL, DETAILFRAME );
  539. SetVertexShaderTextureScaledTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_4, BASETEXTURETRANSFORM, DETAILSCALE );
  540. }
  541. SetAmbientCubeDynamicStateVertexShader();
  542. SetModulationPixelShaderDynamicState( 3 );
  543. EnablePixelShaderOverbright( 0, true, true );
  544. SetPixelShaderConstant( 1, SELFILLUMTINT );
  545. if ( params[DETAIL]->IsTexture() && ( ! IS_FLAG_SET( MATERIAL_VAR_SELFILLUM ) ) )
  546. {
  547. float c1[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
  548. c1[0] = c1[1] = c1[2] = c1[3] = params[DETAILBLENDFACTOR]->GetFloatValue();
  549. pShaderAPI->SetPixelShaderConstant( 1, c1, 1 );
  550. }
  551. if ( params[BLENDTINTBYBASEALPHA]->GetIntValue() )
  552. {
  553. float c1[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
  554. c1[0] = c1[1] = c1[2] = c1[3] = params[BLENDTINTCOLOROVERBASE]->GetFloatValue();
  555. pShaderAPI->SetPixelShaderConstant( 1, c1 );
  556. }
  557. vertexlitgeneric_vs11_Dynamic_Index vshIndex;
  558. vshIndex.SetDOWATERFOG( pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  559. vshIndex.SetSKINNING( pShaderAPI->GetCurrentNumBones() > 0 );
  560. vshIndex.SetLIGHT_COMBO( pShaderAPI->GetCurrentLightCombo() );
  561. pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  562. if( pShaderAPI->GetIntRenderingParameter( INT_RENDERPARM_WRITE_DEPTH_TO_DESTALPHA ) )
  563. {
  564. pShaderAPI->SetPixelShaderIndex( 0 );
  565. }
  566. else
  567. {
  568. // write 255 to alpha if we aren't told to write depth to dest alpha (We don't ever actually write depth to dest alpha in dx8)
  569. pShaderAPI->SetPixelShaderIndex( 1 );
  570. float c4[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
  571. pShaderAPI->SetPixelShaderConstant( 4, c4, 1 );
  572. }
  573. }
  574. Draw();
  575. }
  576. SHADER_DRAW
  577. {
  578. // Skip the standard rendering if cloak pass is fully opaque
  579. bool bDrawStandardPass = true;
  580. if ( false/*disabled! no effect*/ && params[CLOAKPASSENABLED]->GetIntValue() && ( pShaderShadow == NULL ) ) // && not snapshotting
  581. {
  582. CloakBlendedPassVars_t info;
  583. SetupVarsCloakBlendedPass( info );
  584. if ( CloakBlendedPassIsFullyOpaque( params, info ) )
  585. {
  586. // There is some strangeness in DX8 when trying to skip the main pass, so leave this alone for now
  587. //bDrawStandardPass = false;
  588. }
  589. }
  590. // Standard rendering pass
  591. if ( bDrawStandardPass )
  592. {
  593. // FLASHLIGHTFIXME: need to make these the same.
  594. bool hasFlashlight = UsingFlashlight( params );
  595. bool bBump = g_pConfig->UseBumpmapping() && params[BUMPMAP]->IsTexture();
  596. if( hasFlashlight )
  597. {
  598. DrawFlashlight_dx80( params, pShaderAPI, pShaderShadow, bBump, BUMPMAP, BUMPFRAME, BUMPTRANSFORM,
  599. FLASHLIGHTTEXTURE, FLASHLIGHTTEXTUREFRAME, false, false, 0, -1, -1 );
  600. }
  601. else if( bBump )
  602. {
  603. bool bSkipEnvmap = true;
  604. DrawUnbumpedUsingVertexShader( params, pShaderAPI, pShaderShadow, bSkipEnvmap );
  605. // specular pass
  606. bool bBlendSpecular = true;
  607. if( params[ENVMAP]->IsTexture() )
  608. {
  609. DrawModelBumpedSpecularLighting( BUMPMAP, BUMPFRAME, ENVMAP, ENVMAPFRAME,
  610. ENVMAPTINT, ALPHA, ENVMAPCONTRAST, ENVMAPSATURATION, BUMPTRANSFORM, bBlendSpecular );
  611. }
  612. }
  613. else
  614. {
  615. bool bSkipEnvmap = false;
  616. DrawUnbumpedUsingVertexShader( params, pShaderAPI, pShaderShadow, bSkipEnvmap );
  617. }
  618. }
  619. else
  620. {
  621. // Skip this pass!
  622. Draw( false );
  623. }
  624. // Cloak Pass
  625. if ( params[CLOAKPASSENABLED]->GetIntValue() )
  626. {
  627. // If ( snapshotting ) or ( we need to draw this frame )
  628. if ( ( pShaderShadow != NULL ) || ( ( params[CLOAKFACTOR]->GetFloatValue() > 0.0f ) && ( params[CLOAKFACTOR]->GetFloatValue() < 1.0f ) ) )
  629. {
  630. CloakBlendedPassVars_t info;
  631. SetupVarsCloakBlendedPass( info );
  632. DrawCloakBlendedPass( this, params, pShaderAPI, pShaderShadow, info, vertexCompression );
  633. }
  634. else // We're not snapshotting and we don't need to draw this frame
  635. {
  636. // Skip this pass!
  637. Draw( false );
  638. }
  639. }
  640. // Emissive Scroll Pass
  641. if ( params[EMISSIVEBLENDENABLED]->GetIntValue() )
  642. {
  643. // If ( snapshotting ) or ( we need to draw this frame )
  644. if ( ( pShaderShadow != NULL ) || ( params[EMISSIVEBLENDSTRENGTH]->GetFloatValue() > 0.0f ) )
  645. {
  646. EmissiveScrollBlendedPassVars_t info;
  647. SetupVarsEmissiveScrollBlendedPass( info );
  648. DrawEmissiveScrollBlendedPass( this, params, pShaderAPI, pShaderShadow, info, vertexCompression );
  649. }
  650. else // We're not snapshotting and we don't need to draw this frame
  651. {
  652. // Skip this pass!
  653. Draw( false );
  654. }
  655. }
  656. // Flesh Interior Pass
  657. if ( params[FLESHINTERIORENABLED]->GetIntValue() )
  658. {
  659. // If ( snapshotting ) or ( we need to draw this frame )
  660. if ( ( pShaderShadow != NULL ) || ( true ) )
  661. {
  662. FleshInteriorBlendedPassVars_t info;
  663. SetupVarsFleshInteriorBlendedPass( info );
  664. DrawFleshInteriorBlendedPass( this, params, pShaderAPI, pShaderShadow, info, vertexCompression );
  665. }
  666. else // We're not snapshotting and we don't need to draw this frame
  667. {
  668. // Skip this pass!
  669. Draw( false );
  670. }
  671. }
  672. }
  673. END_SHADER
  674. //-----------------------------------------------------------------------------
  675. // Version that doesn't do bumpmapping
  676. //-----------------------------------------------------------------------------
  677. BEGIN_INHERITED_SHADER( VertexLitGeneric_NoBump_DX8, VertexLitGeneric_DX8,
  678. "Help for VertexLitGeneric_NoBump_DX8" )
  679. SHADER_FALLBACK
  680. {
  681. if (g_pConfig->bSoftwareLighting)
  682. return "VertexLitGeneric_DX6";
  683. if (!g_pHardwareConfig->SupportsVertexAndPixelShaders())
  684. return "VertexLitGeneric_DX7";
  685. return 0;
  686. }
  687. virtual bool ShouldUseBumpmapping( IMaterialVar **params )
  688. {
  689. if ( !g_pConfig->UseBumpmapping() )
  690. return false;
  691. if ( !params[BUMPMAP]->IsDefined() )
  692. return false;
  693. return ( params[FORCEBUMP]->GetIntValue() != 0 );
  694. }
  695. END_INHERITED_SHADER