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.

802 lines
25 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Lightmap only shader
  4. //
  5. // $Header: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "BaseVSShader.h"
  9. #include "lightmappedgeneric_vs11.inc"
  10. #include "unlitgeneric_vs11.inc"
  11. #include "worldvertextransition_seamless.inc"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include "tier0/memdbgon.h"
  14. static ConVar mat_fullbright( "mat_fullbright","0", FCVAR_CHEAT );
  15. DEFINE_FALLBACK_SHADER( LightmappedGeneric, LightmappedGeneric_DX8 )
  16. BEGIN_VS_SHADER( LightmappedGeneric_DX8,
  17. "Help for LightmappedGeneric_DX8" )
  18. BEGIN_SHADER_PARAMS
  19. SHADER_PARAM( ALBEDO, SHADER_PARAM_TYPE_TEXTURE, "shadertest/BaseTexture", "albedo (Base texture with no baked lighting)" )
  20. SHADER_PARAM( SELFILLUMTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "Self-illumination tint" )
  21. SHADER_PARAM( DETAIL, SHADER_PARAM_TYPE_TEXTURE, "shadertest/detail", "detail texture" )
  22. SHADER_PARAM( DETAILSCALE, SHADER_PARAM_TYPE_FLOAT, "4", "scale of the detail texture" )
  23. SHADER_PARAM( DETAILBLENDFACTOR, SHADER_PARAM_TYPE_FLOAT, "1", "amount of detail texture to apply" )
  24. SHADER_PARAM( ENVMAP, SHADER_PARAM_TYPE_TEXTURE, "shadertest/shadertest_env", "envmap" )
  25. SHADER_PARAM( ENVMAPFRAME, SHADER_PARAM_TYPE_INTEGER, "", "" )
  26. SHADER_PARAM( ENVMAPMASK, SHADER_PARAM_TYPE_TEXTURE, "shadertest/shadertest_envmask", "envmap mask" )
  27. SHADER_PARAM( ENVMAPMASKFRAME, SHADER_PARAM_TYPE_INTEGER, "", "" )
  28. SHADER_PARAM( ENVMAPMASKSCALE, SHADER_PARAM_TYPE_FLOAT, "1", "envmap mask scale" )
  29. SHADER_PARAM( ENVMAPTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "envmap tint" )
  30. SHADER_PARAM( BUMPMAP, SHADER_PARAM_TYPE_TEXTURE, "models/shadertest/shader1_normal", "bump map" )
  31. SHADER_PARAM( BUMPFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "frame number for $bumpmap" )
  32. SHADER_PARAM( BUMPTRANSFORM, SHADER_PARAM_TYPE_MATRIX, "center .5 .5 scale 1 1 rotate 0 translate 0 0", "$bumpmap texcoord transform" )
  33. SHADER_PARAM( ENVMAPCONTRAST, SHADER_PARAM_TYPE_FLOAT, "0.0", "contrast 0 == normal 1 == color*color" )
  34. SHADER_PARAM( ENVMAPSATURATION, SHADER_PARAM_TYPE_FLOAT, "1.0", "saturation 0 == greyscale 1 == normal" )
  35. SHADER_PARAM( FRESNELREFLECTION, SHADER_PARAM_TYPE_FLOAT, "1.0", "1.0 == mirror, 0.0 == water" )
  36. SHADER_PARAM( ENVMAPOPTIONAL, SHADER_PARAM_TYPE_INTEGER, "90", "Do specular pass only on dxlevel or higher (ie.80, 81, 90)" )
  37. SHADER_PARAM( NODIFFUSEBUMPLIGHTING, SHADER_PARAM_TYPE_BOOL, "0", "0 == Use diffuse bump lighting, 1 = No diffuse bump lighting" )
  38. SHADER_PARAM( FORCEBUMP, SHADER_PARAM_TYPE_BOOL, "0", "0 == Do bumpmapping if the card says it can handle it. 1 == Always do bumpmapping." )
  39. SHADER_PARAM( ALPHATESTREFERENCE, SHADER_PARAM_TYPE_FLOAT, "0.0", "" )
  40. SHADER_PARAM( SSBUMP, SHADER_PARAM_TYPE_INTEGER, "0", "whether or not to use alternate bumpmap format with height" )
  41. SHADER_PARAM( SEAMLESS_SCALE, SHADER_PARAM_TYPE_FLOAT, "0", "Scale factor for 'seamless' texture mapping. 0 means to use ordinary mapping" )
  42. END_SHADER_PARAMS
  43. virtual bool ShouldUseBumpmapping( IMaterialVar **params )
  44. {
  45. return g_pConfig->UseBumpmapping() && params[BUMPMAP]->IsDefined();
  46. }
  47. // Set up anything that is necessary to make decisions in SHADER_FALLBACK.
  48. SHADER_INIT_PARAMS()
  49. {
  50. // FLASHLIGHTFIXME
  51. params[FLASHLIGHTTEXTURE]->SetStringValue( "effects/flashlight001" );
  52. // Write over $basetexture with $albedo if we are going to be using diffuse normal mapping.
  53. if( ShouldUseBumpmapping( params ) && params[ALBEDO]->IsDefined() &&
  54. params[BASETEXTURE]->IsDefined() &&
  55. !( params[NODIFFUSEBUMPLIGHTING]->IsDefined() && params[NODIFFUSEBUMPLIGHTING]->GetIntValue() ) )
  56. {
  57. params[BASETEXTURE]->SetStringValue( params[ALBEDO]->GetStringValue() );
  58. }
  59. if( IsUsingGraphics() && params[ENVMAP]->IsDefined() && !CanUseEditorMaterials() )
  60. {
  61. if( stricmp( params[ENVMAP]->GetStringValue(), "env_cubemap" ) == 0 )
  62. {
  63. Warning( "env_cubemap used on world geometry without rebuilding map. . ignoring: %s\n", pMaterialName );
  64. params[ENVMAP]->SetUndefined();
  65. }
  66. }
  67. if( !params[ENVMAPMASKSCALE]->IsDefined() )
  68. {
  69. params[ENVMAPMASKSCALE]->SetFloatValue( 1.0f );
  70. }
  71. if( !params[ENVMAPTINT]->IsDefined() )
  72. {
  73. params[ENVMAPTINT]->SetVecValue( 1.0f, 1.0f, 1.0f );
  74. }
  75. if( !params[SELFILLUMTINT]->IsDefined() )
  76. {
  77. params[SELFILLUMTINT]->SetVecValue( 1.0f, 1.0f, 1.0f );
  78. }
  79. if( !params[DETAILSCALE]->IsDefined() )
  80. {
  81. params[DETAILSCALE]->SetFloatValue( 4.0f );
  82. }
  83. if( !params[DETAILBLENDFACTOR]->IsDefined() )
  84. {
  85. params[DETAILBLENDFACTOR]->SetFloatValue( 1.0f );
  86. }
  87. if( !params[FRESNELREFLECTION]->IsDefined() )
  88. {
  89. params[FRESNELREFLECTION]->SetFloatValue( 1.0f );
  90. }
  91. if( !params[ENVMAPMASKFRAME]->IsDefined() )
  92. {
  93. params[ENVMAPMASKFRAME]->SetIntValue( 0 );
  94. }
  95. if( !params[ENVMAPFRAME]->IsDefined() )
  96. {
  97. params[ENVMAPFRAME]->SetIntValue( 0 );
  98. }
  99. if( !params[BUMPFRAME]->IsDefined() )
  100. {
  101. params[BUMPFRAME]->SetIntValue( 0 );
  102. }
  103. if( !params[ENVMAPCONTRAST]->IsDefined() )
  104. {
  105. params[ENVMAPCONTRAST]->SetFloatValue( 0.0f );
  106. }
  107. if( !params[ENVMAPSATURATION]->IsDefined() )
  108. {
  109. params[ENVMAPSATURATION]->SetFloatValue( 1.0f );
  110. }
  111. if( !params[ALPHATESTREFERENCE]->IsDefined() )
  112. {
  113. params[ALPHATESTREFERENCE]->SetFloatValue( 0.0f );
  114. }
  115. // No texture means no self-illum or env mask in base alpha
  116. if ( !params[BASETEXTURE]->IsDefined() )
  117. {
  118. CLEAR_FLAGS( MATERIAL_VAR_SELFILLUM );
  119. CLEAR_FLAGS( MATERIAL_VAR_BASEALPHAENVMAPMASK );
  120. }
  121. // If in decal mode, no debug override...
  122. if (IS_FLAG_SET(MATERIAL_VAR_DECAL))
  123. {
  124. SET_FLAGS( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  125. }
  126. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_LIGHTMAP );
  127. if( ShouldUseBumpmapping( params ) && (params[NODIFFUSEBUMPLIGHTING]->GetIntValue() == 0) )
  128. {
  129. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_BUMPED_LIGHTMAP );
  130. }
  131. // Get rid of the envmap if it's optional for this dx level.
  132. if( params[ENVMAPOPTIONAL]->IsDefined() && (params[ENVMAPOPTIONAL]->GetIntValue() > g_pHardwareConfig->GetDXSupportLevel()) )
  133. {
  134. params[ENVMAP]->SetUndefined();
  135. }
  136. // If mat_specular 0, then get rid of envmap
  137. if( !g_pConfig->UseSpecular() && params[ENVMAP]->IsDefined() && params[BASETEXTURE]->IsDefined() )
  138. {
  139. params[ENVMAP]->SetUndefined();
  140. }
  141. if( params[SEAMLESS_SCALE]->IsDefined() && params[SEAMLESS_SCALE]->GetFloatValue() != 0.0f )
  142. {
  143. if( params[BUMPMAP]->IsDefined() )
  144. {
  145. Warning( "Can't use $bumpmap with $seamless_scale for lightmappedgeneric_dx8. Implicitly disabling $bumpmap: %s\n", pMaterialName );
  146. params[BUMPMAP]->SetUndefined();
  147. }
  148. if( params[ENVMAP]->IsDefined() )
  149. {
  150. Warning( "Can't use $envmap with $seamless_scale for lightmappedgeneric_dx8. Implicitly disabling $envmap: %s\n", pMaterialName );
  151. params[ENVMAP]->SetUndefined();
  152. }
  153. }
  154. if ( !params[SEAMLESS_SCALE]->IsDefined() )
  155. {
  156. // zero means don't do seamless mapping.
  157. params[SEAMLESS_SCALE]->SetFloatValue( 0.0f );
  158. }
  159. // Get rid of envmap if we aren't using bumpmapping
  160. // *and* we have normalmapalphaenvmapmask *and* we don't have envmapmask elsewhere
  161. if ( params[ENVMAP]->IsDefined() && params[BUMPMAP]->IsDefined() && IS_FLAG_SET( MATERIAL_VAR_NORMALMAPALPHAENVMAPMASK ) && !ShouldUseBumpmapping( params ) )
  162. {
  163. if ( !IS_FLAG_SET(MATERIAL_VAR_BASEALPHAENVMAPMASK) && !params[ENVMAPMASK]->IsDefined() )
  164. {
  165. params[ENVMAP]->SetUndefined();
  166. }
  167. }
  168. }
  169. SHADER_FALLBACK
  170. {
  171. if ( IsPC() && g_pHardwareConfig->GetDXSupportLevel() < 80)
  172. return "LightmappedGeneric_DX6";
  173. if ( IsPC() && g_pHardwareConfig->PreferReducedFillrate() )
  174. return "LightmappedGeneric_NoBump_DX8";
  175. return 0;
  176. }
  177. SHADER_INIT
  178. {
  179. LoadTexture( FLASHLIGHTTEXTURE );
  180. if( ShouldUseBumpmapping( params ) )
  181. {
  182. LoadBumpMap( BUMPMAP );
  183. }
  184. if (params[BASETEXTURE]->IsDefined())
  185. {
  186. LoadTexture( BASETEXTURE );
  187. if (!params[BASETEXTURE]->GetTextureValue()->IsTranslucent())
  188. {
  189. CLEAR_FLAGS( MATERIAL_VAR_SELFILLUM );
  190. CLEAR_FLAGS( MATERIAL_VAR_BASEALPHAENVMAPMASK );
  191. }
  192. }
  193. if (params[DETAIL]->IsDefined())
  194. {
  195. LoadTexture( DETAIL );
  196. }
  197. // Don't alpha test if the alpha channel is used for other purposes
  198. if (IS_FLAG_SET(MATERIAL_VAR_SELFILLUM) || IS_FLAG_SET(MATERIAL_VAR_BASEALPHAENVMAPMASK) )
  199. CLEAR_FLAGS( MATERIAL_VAR_ALPHATEST );
  200. if (params[ENVMAP]->IsDefined())
  201. {
  202. if( !IS_FLAG_SET(MATERIAL_VAR_ENVMAPSPHERE) )
  203. LoadCubeMap( ENVMAP );
  204. else
  205. LoadTexture( ENVMAP );
  206. if( !g_pHardwareConfig->SupportsCubeMaps() )
  207. {
  208. SET_FLAGS( MATERIAL_VAR_ENVMAPSPHERE );
  209. }
  210. if (params[ENVMAPMASK]->IsDefined())
  211. LoadTexture( ENVMAPMASK );
  212. }
  213. if( ShouldUseBumpmapping( params ) )
  214. {
  215. SET_FLAGS2( MATERIAL_VAR2_NEEDS_TANGENT_SPACES );
  216. }
  217. }
  218. #ifndef USE_HLSL_PIXEL_SHADERS
  219. inline const char *GetPixelShaderName( IMaterialVar** params, bool bBumpedEnvMap )
  220. {
  221. static char const* s_pPixelShaders[] =
  222. {
  223. // Unmasked
  224. "LightmappedGeneric_EnvMapV2",
  225. "LightmappedGeneric_SelfIlluminatedEnvMapV2",
  226. "LightmappedGeneric_BaseAlphaMaskedEnvMapV2",
  227. "LightmappedGeneric_SelfIlluminatedEnvMapV2",
  228. // Env map mask
  229. "LightmappedGeneric_MaskedEnvMapV2",
  230. "LightmappedGeneric_SelfIlluminatedMaskedEnvMapV2",
  231. "LightmappedGeneric_MaskedEnvMapV2",
  232. "LightmappedGeneric_SelfIlluminatedMaskedEnvMapV2",
  233. };
  234. if (!params[BASETEXTURE]->IsTexture())
  235. {
  236. if (params[ENVMAP]->IsTexture() && !bBumpedEnvMap )
  237. {
  238. if (!params[ENVMAPMASK]->IsDefined() )
  239. {
  240. return "LightmappedGeneric_EnvmapNoTexture";
  241. }
  242. else
  243. {
  244. return "LightmappedGeneric_MaskedEnvmapNoTexture";
  245. }
  246. }
  247. else
  248. {
  249. return "LightmappedGeneric_NoTexture";
  250. }
  251. }
  252. else
  253. {
  254. if (params[ENVMAP]->IsTexture() && !bBumpedEnvMap )
  255. {
  256. int pshIndex = 0;
  257. if (IS_FLAG_SET(MATERIAL_VAR_SELFILLUM))
  258. pshIndex |= 0x1;
  259. if (IS_FLAG_SET(MATERIAL_VAR_BASEALPHAENVMAPMASK))
  260. pshIndex |= 0x2;
  261. if (params[ENVMAPMASK]->IsTexture())
  262. pshIndex |= 0x4;
  263. return s_pPixelShaders[pshIndex];
  264. }
  265. else
  266. {
  267. if (IS_FLAG_SET(MATERIAL_VAR_SELFILLUM))
  268. return "LightmappedGeneric_SelfIlluminated";
  269. else
  270. return "LightmappedGeneric";
  271. }
  272. }
  273. }
  274. #endif
  275. void DrawUnbumpedUsingVertexShader( IMaterialVar** params, IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow, bool bBumpedEnvMap )
  276. {
  277. bool hasEnvmap = params[ENVMAP]->IsTexture() && !bBumpedEnvMap;
  278. bool hasBaseTexture = params[BASETEXTURE]->IsTexture();
  279. bool hasVertexColor = IS_FLAG_SET( MATERIAL_VAR_VERTEXCOLOR );
  280. bool hasEnvmapCameraSpace = IS_FLAG_SET( MATERIAL_VAR_ENVMAPCAMERASPACE );
  281. bool hasEnvmapSphere = IS_FLAG_SET( MATERIAL_VAR_ENVMAPSPHERE );
  282. if ( hasEnvmap || hasBaseTexture || hasVertexColor || !bBumpedEnvMap )
  283. {
  284. SHADOW_STATE
  285. {
  286. // Alpha test
  287. pShaderShadow->EnableAlphaTest( IS_FLAG_SET(MATERIAL_VAR_ALPHATEST) );
  288. if ( params[ALPHATESTREFERENCE]->GetFloatValue() > 0.0f )
  289. {
  290. pShaderShadow->AlphaFunc( SHADER_ALPHAFUNC_GEQUAL, params[ALPHATESTREFERENCE]->GetFloatValue() );
  291. }
  292. // Base texture on stage 0
  293. if (params[BASETEXTURE]->IsTexture())
  294. {
  295. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  296. }
  297. // Lightmap on stage 1
  298. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  299. int fmt = VERTEX_POSITION;
  300. if ( hasEnvmap )
  301. {
  302. fmt |= VERTEX_NORMAL;
  303. // envmap on stage 2
  304. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  305. // envmapmask on stage 3
  306. if (params[ENVMAPMASK]->IsTexture() || IS_FLAG_SET(MATERIAL_VAR_BASEALPHAENVMAPMASK ) )
  307. {
  308. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  309. }
  310. }
  311. if (params[BASETEXTURE]->IsTexture() || bBumpedEnvMap)
  312. {
  313. SetDefaultBlendingShadowState( BASETEXTURE, true );
  314. }
  315. else
  316. {
  317. SetDefaultBlendingShadowState( ENVMAPMASK, false );
  318. }
  319. if (IS_FLAG_SET(MATERIAL_VAR_VERTEXCOLOR))
  320. {
  321. fmt |= VERTEX_COLOR;
  322. }
  323. pShaderShadow->VertexShaderVertexFormat( fmt, 2, 0, 0 );
  324. lightmappedgeneric_vs11_Static_Index vshIndex;
  325. vshIndex.SetDETAIL( false );
  326. vshIndex.SetENVMAP( hasEnvmap );
  327. vshIndex.SetENVMAPCAMERASPACE( hasEnvmap && hasEnvmapCameraSpace );
  328. vshIndex.SetENVMAPSPHERE( hasEnvmap && hasEnvmapSphere );
  329. vshIndex.SetVERTEXCOLOR( hasVertexColor );
  330. pShaderShadow->SetVertexShader( "LightmappedGeneric_vs11", vshIndex.GetIndex() );
  331. const char *pshName = GetPixelShaderName( params, bBumpedEnvMap );
  332. pShaderShadow->SetPixelShader( pshName );
  333. DefaultFog();
  334. }
  335. DYNAMIC_STATE
  336. {
  337. if (hasBaseTexture)
  338. {
  339. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  340. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, BASETEXTURETRANSFORM );
  341. }
  342. pShaderAPI->BindStandardTexture( SHADER_SAMPLER1, TEXTURE_LIGHTMAP );
  343. if ( hasEnvmap )
  344. {
  345. BindTexture( SHADER_SAMPLER2, ENVMAP, ENVMAPFRAME );
  346. if (params[ENVMAPMASK]->IsTexture() || IS_FLAG_SET(MATERIAL_VAR_BASEALPHAENVMAPMASK) )
  347. {
  348. if (params[ENVMAPMASK]->IsTexture() )
  349. BindTexture( SHADER_SAMPLER3, ENVMAPMASK, ENVMAPMASKFRAME );
  350. else
  351. BindTexture( SHADER_SAMPLER3, BASETEXTURE, FRAME );
  352. SetVertexShaderTextureScaledTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_2, BASETEXTURETRANSFORM, ENVMAPMASKSCALE );
  353. }
  354. if (IS_FLAG_SET(MATERIAL_VAR_ENVMAPSPHERE) ||
  355. IS_FLAG_SET(MATERIAL_VAR_ENVMAPCAMERASPACE))
  356. {
  357. LoadViewMatrixIntoVertexShaderConstant( VERTEX_SHADER_VIEWMODEL );
  358. }
  359. SetEnvMapTintPixelShaderDynamicState( 2, ENVMAPTINT, -1 );
  360. }
  361. if ( !hasEnvmap || hasBaseTexture || hasVertexColor )
  362. {
  363. SetModulationVertexShaderDynamicState();
  364. }
  365. EnablePixelShaderOverbright( 0, true, true );
  366. SetPixelShaderConstant( 1, SELFILLUMTINT );
  367. lightmappedgeneric_vs11_Dynamic_Index vshIndex;
  368. vshIndex.SetDOWATERFOG( pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  369. pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  370. }
  371. Draw();
  372. }
  373. if ( bBumpedEnvMap )
  374. {
  375. DrawWorldBumpedSpecularLighting(
  376. BUMPMAP, ENVMAP, BUMPFRAME, ENVMAPFRAME,
  377. ENVMAPTINT, ALPHA, ENVMAPCONTRAST, ENVMAPSATURATION,
  378. BUMPTRANSFORM, FRESNELREFLECTION,
  379. hasEnvmap || hasBaseTexture || hasVertexColor );
  380. }
  381. }
  382. void DrawDetailNoEnvmap( IMaterialVar** params, IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow, bool doSelfIllum )
  383. {
  384. SHADOW_STATE
  385. {
  386. // Alpha test
  387. pShaderShadow->EnableAlphaTest( IS_FLAG_SET(MATERIAL_VAR_ALPHATEST) );
  388. // Base texture on stage 0
  389. if (params[BASETEXTURE]->IsTexture())
  390. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  391. // Lightmap on stage 1
  392. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  393. // Detail on stage 2
  394. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  395. int fmt = VERTEX_POSITION;
  396. SetDefaultBlendingShadowState( BASETEXTURE, true );
  397. if (IS_FLAG_SET(MATERIAL_VAR_VERTEXCOLOR))
  398. fmt |= VERTEX_COLOR;
  399. pShaderShadow->VertexShaderVertexFormat( fmt, 2, 0, 0 );
  400. lightmappedgeneric_vs11_Static_Index vshIndex;
  401. vshIndex.SetDETAIL( true );
  402. vshIndex.SetENVMAP( false );
  403. vshIndex.SetENVMAPCAMERASPACE( false );
  404. vshIndex.SetENVMAPSPHERE( false );
  405. vshIndex.SetVERTEXCOLOR( IS_FLAG_SET( MATERIAL_VAR_VERTEXCOLOR ) );
  406. pShaderShadow->SetVertexShader( "LightmappedGeneric_vs11", vshIndex.GetIndex() );
  407. if (!params[BASETEXTURE]->IsTexture())
  408. {
  409. pShaderShadow->SetPixelShader("LightmappedGeneric_DetailNoTexture");
  410. }
  411. else
  412. {
  413. if (!IS_FLAG_SET(MATERIAL_VAR_SELFILLUM) || (!doSelfIllum))
  414. {
  415. pShaderShadow->SetPixelShader("LightmappedGeneric_Detail");
  416. }
  417. else
  418. {
  419. pShaderShadow->SetPixelShader("LightmappedGeneric_DetailSelfIlluminated");
  420. }
  421. }
  422. DefaultFog();
  423. }
  424. DYNAMIC_STATE
  425. {
  426. if (params[BASETEXTURE]->IsTexture())
  427. {
  428. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  429. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, BASETEXTURETRANSFORM );
  430. }
  431. pShaderAPI->BindStandardTexture( SHADER_SAMPLER1, TEXTURE_LIGHTMAP );
  432. BindTexture( SHADER_SAMPLER2, DETAIL, FRAME );
  433. SetVertexShaderTextureScaledTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_4, BASETEXTURETRANSFORM, DETAILSCALE );
  434. SetModulationVertexShaderDynamicState();
  435. EnablePixelShaderOverbright( 0, true, true );
  436. if (doSelfIllum)
  437. {
  438. SetPixelShaderConstant( 1, SELFILLUMTINT );
  439. }
  440. float c2[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
  441. c2[0] = c2[1] = c2[2] = c2[3] = params[DETAILBLENDFACTOR]->GetFloatValue();
  442. pShaderAPI->SetPixelShaderConstant( 2, c2, 1 );
  443. lightmappedgeneric_vs11_Dynamic_Index vshIndex;
  444. vshIndex.SetDOWATERFOG( pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  445. pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  446. }
  447. Draw();
  448. }
  449. inline const char *GetAdditiveEnvmapPixelShaderName( bool usingMask,
  450. bool usingBaseTexture, bool usingBaseAlphaEnvmapMask )
  451. {
  452. static char const* s_pPixelShaders[] =
  453. {
  454. "LightmappedGeneric_AddEnvmapNoTexture",
  455. "LightmappedGeneric_AddEnvmapMaskNoTexture",
  456. };
  457. if ( !usingMask && usingBaseTexture && usingBaseAlphaEnvmapMask )
  458. return "LightmappedGeneric_AddBaseAlphaMaskedEnvMap";
  459. int pshIndex = 0;
  460. if (usingMask)
  461. pshIndex |= 0x1;
  462. return s_pPixelShaders[pshIndex];
  463. }
  464. void DrawAdditiveEnvmap( IMaterialVar** params, IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow )
  465. {
  466. bool usingBaseTexture = params[BASETEXTURE]->IsTexture();
  467. bool usingMask = params[ENVMAPMASK]->IsTexture();
  468. bool usingBaseAlphaEnvmapMask = IS_FLAG_SET(MATERIAL_VAR_BASEALPHAENVMAPMASK);
  469. SHADOW_STATE
  470. {
  471. // Alpha test
  472. pShaderShadow->EnableAlphaTest( false );
  473. pShaderShadow->EnableTexture( SHADER_SAMPLER0, false );
  474. pShaderShadow->EnableTexture( SHADER_SAMPLER1, false );
  475. // envmap on stage 2
  476. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  477. // envmapmask on stage 3
  478. if (params[ENVMAPMASK]->IsTexture() || IS_FLAG_SET(MATERIAL_VAR_BASEALPHAENVMAPMASK ) )
  479. {
  480. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  481. }
  482. if (params[BASETEXTURE]->IsTexture())
  483. {
  484. SetAdditiveBlendingShadowState( BASETEXTURE, true );
  485. }
  486. else
  487. {
  488. SetAdditiveBlendingShadowState( ENVMAPMASK, false );
  489. }
  490. int fmt = VERTEX_POSITION | VERTEX_NORMAL;
  491. pShaderShadow->VertexShaderVertexFormat( fmt, 1, 0, 0 );
  492. // Compute the vertex shader index.
  493. lightmappedgeneric_vs11_Static_Index vshIndex;
  494. vshIndex.SetDETAIL( false );
  495. vshIndex.SetENVMAP( true );
  496. vshIndex.SetENVMAPCAMERASPACE( IS_FLAG_SET(MATERIAL_VAR_ENVMAPCAMERASPACE) );
  497. vshIndex.SetENVMAPSPHERE( IS_FLAG_SET(MATERIAL_VAR_ENVMAPSPHERE) );
  498. vshIndex.SetVERTEXCOLOR( false );
  499. s_pShaderShadow->SetVertexShader( "LightmappedGeneric_vs11", vshIndex.GetIndex() );
  500. const char *pshName = GetAdditiveEnvmapPixelShaderName( usingMask,
  501. usingBaseTexture, usingBaseAlphaEnvmapMask );
  502. pShaderShadow->SetPixelShader( pshName );
  503. FogToBlack();
  504. }
  505. DYNAMIC_STATE
  506. {
  507. BindTexture( SHADER_SAMPLER2, ENVMAP, ENVMAPFRAME );
  508. if (usingMask || IS_FLAG_SET(MATERIAL_VAR_BASEALPHAENVMAPMASK))
  509. {
  510. if (usingMask)
  511. BindTexture( SHADER_SAMPLER3, ENVMAPMASK, ENVMAPMASKFRAME );
  512. else
  513. BindTexture( SHADER_SAMPLER3, BASETEXTURE, FRAME );
  514. SetVertexShaderTextureScaledTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_2, BASETEXTURETRANSFORM, ENVMAPMASKSCALE );
  515. }
  516. SetPixelShaderConstant( 2, ENVMAPTINT );
  517. if (IS_FLAG_SET(MATERIAL_VAR_ENVMAPSPHERE) || IS_FLAG_SET(MATERIAL_VAR_ENVMAPCAMERASPACE))
  518. {
  519. LoadViewMatrixIntoVertexShaderConstant( VERTEX_SHADER_VIEWMODEL );
  520. }
  521. SetModulationVertexShaderDynamicState();
  522. // Compute the vertex shader index.
  523. lightmappedgeneric_vs11_Dynamic_Index vshIndex;
  524. vshIndex.SetDOWATERFOG( s_pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  525. s_pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  526. }
  527. Draw();
  528. }
  529. void DrawDetailMode1( IMaterialVar** params, IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow, bool bBumpedEnvMap )
  530. {
  531. // Mode 1 :
  532. // Pass 1 : B * L * D + Self Illum
  533. // Pass 2 : Add E * M
  534. // Draw the detail w/ no envmap
  535. DrawDetailNoEnvmap( params, pShaderAPI, pShaderShadow, true );
  536. if ( !bBumpedEnvMap )
  537. {
  538. DrawAdditiveEnvmap( params, pShaderAPI, pShaderShadow );
  539. }
  540. else
  541. {
  542. DrawWorldBumpedSpecularLighting(
  543. BUMPMAP, ENVMAP, BUMPFRAME, ENVMAPFRAME,
  544. ENVMAPTINT, ALPHA, ENVMAPCONTRAST, ENVMAPSATURATION,
  545. BUMPTRANSFORM, FRESNELREFLECTION,
  546. true );
  547. }
  548. }
  549. void DrawDetailUsingVertexShader( IMaterialVar** params, IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow, bool bBumpedEnvMap )
  550. {
  551. // We don't have enough textures; gotta do this in two passes if there's envmapping
  552. if (!params[ENVMAP]->IsTexture())
  553. {
  554. DrawDetailNoEnvmap( params, pShaderAPI, pShaderShadow, IS_FLAG_SET( MATERIAL_VAR_SELFILLUM ) );
  555. }
  556. else
  557. {
  558. if (!params[BASETEXTURE]->IsTexture())
  559. {
  560. // If there's an envmap but no base texture, ignore detail
  561. DrawUnbumpedUsingVertexShader( params, pShaderAPI, pShaderShadow, bBumpedEnvMap );
  562. }
  563. else
  564. {
  565. DrawDetailMode1( params, pShaderAPI, pShaderShadow, bBumpedEnvMap );
  566. }
  567. }
  568. }
  569. void DrawUnbumpedSeamlessUsingVertexShader( IMaterialVar** params, IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow )
  570. {
  571. // This is the seamless_scale version, which doesn't use $detail or $bumpmap
  572. SHADOW_STATE
  573. {
  574. // three copies of the base texture for seamless blending
  575. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  576. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  577. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  578. // lightmap
  579. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  580. int fmt = VERTEX_POSITION;
  581. pShaderShadow->VertexShaderVertexFormat( fmt, 2, 0, 0 );
  582. worldvertextransition_seamless_Static_Index vshIndex;
  583. pShaderShadow->SetVertexShader( "WorldVertexTransition_Seamless", vshIndex.GetIndex() );
  584. int pshIndex = 0;
  585. pShaderShadow->SetPixelShader( "WorldVertexTransition_Seamless", pshIndex );
  586. FogToFogColor();
  587. }
  588. DYNAMIC_STATE
  589. {
  590. bool bLightingOnly = mat_fullbright.GetInt() == 2 && !IS_FLAG_SET( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  591. // Texture 0..2
  592. if( bLightingOnly )
  593. {
  594. pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, TEXTURE_GREY );
  595. pShaderAPI->BindStandardTexture( SHADER_SAMPLER1, TEXTURE_GREY );
  596. pShaderAPI->BindStandardTexture( SHADER_SAMPLER2, TEXTURE_GREY );
  597. }
  598. else
  599. {
  600. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  601. BindTexture( SHADER_SAMPLER1, BASETEXTURE, FRAME );
  602. BindTexture( SHADER_SAMPLER2, BASETEXTURE, FRAME );
  603. }
  604. // Texture 3 = lightmap
  605. pShaderAPI->BindStandardTexture( SHADER_SAMPLER3, TEXTURE_LIGHTMAP );
  606. EnablePixelShaderOverbright( 0, true, true );
  607. float fSeamlessScale = params[SEAMLESS_SCALE]->GetFloatValue();
  608. float map_scale[4]= { fSeamlessScale, fSeamlessScale, fSeamlessScale, fSeamlessScale };
  609. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, map_scale );
  610. worldvertextransition_seamless_Dynamic_Index vshIndex;
  611. vshIndex.SetDOWATERFOG( pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  612. pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  613. }
  614. Draw();
  615. }
  616. SHADER_DRAW
  617. {
  618. bool hasFlashlight = UsingFlashlight( params );
  619. bool bBump = ShouldUseBumpmapping( params ) && params[BUMPMAP]->IsTexture() &&
  620. (params[NODIFFUSEBUMPLIGHTING]->GetIntValue() == 0);
  621. bool bSSBump = bBump && ( params[SSBUMP]->GetIntValue() != 0 );
  622. if( hasFlashlight )
  623. {
  624. DrawFlashlight_dx80( params, pShaderAPI, pShaderShadow, bBump, BUMPMAP, BUMPFRAME, BUMPTRANSFORM,
  625. FLASHLIGHTTEXTURE, FLASHLIGHTTEXTUREFRAME, true, false, 0, -1, -1 );
  626. }
  627. else if( bBump )
  628. {
  629. DrawWorldBumpedUsingVertexShader(
  630. BASETEXTURE, BASETEXTURETRANSFORM,
  631. BUMPMAP, BUMPFRAME, BUMPTRANSFORM, ENVMAPMASK, ENVMAPMASKFRAME, ENVMAP,
  632. ENVMAPFRAME, ENVMAPTINT, COLOR, ALPHA, ENVMAPCONTRAST, ENVMAPSATURATION, FRAME, FRESNELREFLECTION,
  633. false, -1, -1, -1, bSSBump );
  634. }
  635. else
  636. {
  637. bool bBumpedEnvMap = ShouldUseBumpmapping( params ) && params[BUMPMAP]->IsTexture() && params[ENVMAP]->IsTexture();
  638. if (!params[DETAIL]->IsTexture())
  639. {
  640. if( params[SEAMLESS_SCALE]->GetFloatValue() != 0.0f )
  641. {
  642. DrawUnbumpedSeamlessUsingVertexShader( params, pShaderAPI, pShaderShadow );
  643. }
  644. else
  645. {
  646. DrawUnbumpedUsingVertexShader( params, pShaderAPI, pShaderShadow, bBumpedEnvMap );
  647. }
  648. }
  649. else
  650. {
  651. DrawDetailUsingVertexShader( params, pShaderAPI, pShaderShadow, bBumpedEnvMap );
  652. }
  653. }
  654. }
  655. END_SHADER
  656. //-----------------------------------------------------------------------------
  657. // Version that doesn't do bumpmapping
  658. //-----------------------------------------------------------------------------
  659. BEGIN_INHERITED_SHADER( LightmappedGeneric_NoBump_DX8, LightmappedGeneric_DX8,
  660. "Help for LightmappedGeneric_NoBump_DX8" )
  661. SHADER_FALLBACK
  662. {
  663. if (g_pHardwareConfig->GetDXSupportLevel() < 80)
  664. return "LightmappedGeneric_DX6";
  665. return 0;
  666. }
  667. virtual bool ShouldUseBumpmapping( IMaterialVar **params )
  668. {
  669. if ( !g_pConfig->UseBumpmapping() )
  670. return false;
  671. if ( !params[BUMPMAP]->IsDefined() )
  672. return false;
  673. return ( params[FORCEBUMP]->GetIntValue() != 0 );
  674. }
  675. END_INHERITED_SHADER