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.

604 lines
22 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 "convar.h"
  10. #if !defined( _X360 ) && !defined( _PS3 )
  11. #include "lightmappedgeneric_vs30.inc"
  12. #include "worldtwotextureblend_ps30.inc"
  13. #endif
  14. #include "lightmappedgeneric_vs20.inc"
  15. #include "worldtwotextureblend_ps20.inc"
  16. #include "worldtwotextureblend_ps20b.inc"
  17. #include "shaderapifast.h"
  18. // memdbgon must be the last include file in a .cpp file!!!
  19. #include "tier0/memdbgon.h"
  20. #if defined( CSTRIKE15 ) && defined( _X360 )
  21. static ConVar r_shader_srgbread( "r_shader_srgbread", "1", 0, "1 = use shader srgb texture reads, 0 = use HW" );
  22. #else
  23. static ConVar r_shader_srgbread( "r_shader_srgbread", "0", 0, "1 = use shader srgb texture reads, 0 = use HW" );
  24. #endif
  25. // FIXME: Need to make a dx9 version so that "CENTROID" works.
  26. BEGIN_VS_SHADER( WorldTwoTextureBlend,
  27. "Help for WorldTwoTextureBlend" )
  28. BEGIN_SHADER_PARAMS
  29. SHADER_PARAM_OVERRIDE( BASETEXTURE, SHADER_PARAM_TYPE_TEXTURE, "shadertest/WorldTwoTextureBlend", "iris texture", 0 )
  30. SHADER_PARAM( ALBEDO, SHADER_PARAM_TYPE_TEXTURE, "shadertest/WorldTwoTextureBlend", "albedo (Base texture with no baked lighting)" )
  31. SHADER_PARAM( SELFILLUMTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "Self-illumination tint" )
  32. SHADER_PARAM( DETAIL, SHADER_PARAM_TYPE_TEXTURE, "shadertest/WorldTwoTextureBlend_detail", "detail texture" )
  33. SHADER_PARAM( DETAILFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "frame number for $detail" )
  34. SHADER_PARAM( DETAILSCALE, SHADER_PARAM_TYPE_FLOAT, "1.0", "scale of the detail texture" )
  35. SHADER_PARAM( DETAIL_ALPHA_MASK_BASE_TEXTURE, SHADER_PARAM_TYPE_BOOL, "0",
  36. "If this is 1, then when detail alpha=0, no base texture is blended and when "
  37. "detail alpha=1, you get detail*base*lightmap" )
  38. SHADER_PARAM( BUMPMAP, SHADER_PARAM_TYPE_TEXTURE, "models/shadertest/shader1_normal", "bump map" )
  39. SHADER_PARAM( BUMPFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "frame number for $bumpmap" )
  40. SHADER_PARAM( BUMPTRANSFORM, SHADER_PARAM_TYPE_MATRIX, "center .5 .5 scale 1 1 rotate 0 translate 0 0", "$bumpmap texcoord transform" )
  41. SHADER_PARAM( NODIFFUSEBUMPLIGHTING, SHADER_PARAM_TYPE_INTEGER, "0", "0 == Use diffuse bump lighting, 1 = No diffuse bump lighting" )
  42. SHADER_PARAM( SEAMLESS_SCALE, SHADER_PARAM_TYPE_FLOAT, "0", "Scale factor for 'seamless' texture mapping. 0 means to use ordinary mapping" )
  43. SHADER_PARAM( SHADERSRGBREAD360, SHADER_PARAM_TYPE_BOOL, "0", "Simulate srgb read in shader code")
  44. END_SHADER_PARAMS
  45. SHADER_FALLBACK
  46. {
  47. return 0;
  48. }
  49. SHADER_INIT_PARAMS()
  50. {
  51. if( !params[DETAIL_ALPHA_MASK_BASE_TEXTURE]->IsDefined() )
  52. {
  53. params[DETAIL_ALPHA_MASK_BASE_TEXTURE]->SetIntValue( 0 );
  54. }
  55. params[FLASHLIGHTTEXTURE]->SetStringValue( GetFlashlightTextureFilename() );
  56. // Write over $basetexture with $albedo if we are going to be using diffuse normal mapping.
  57. if( g_pConfig->UseBumpmapping() && params[BUMPMAP]->IsDefined() && params[ALBEDO]->IsDefined() &&
  58. params[BASETEXTURE]->IsDefined() &&
  59. !( params[NODIFFUSEBUMPLIGHTING]->IsDefined() && params[NODIFFUSEBUMPLIGHTING]->GetIntValue() ) )
  60. {
  61. params[BASETEXTURE]->SetStringValue( params[ALBEDO]->GetStringValue() );
  62. }
  63. if( !params[NODIFFUSEBUMPLIGHTING]->IsDefined() )
  64. {
  65. params[NODIFFUSEBUMPLIGHTING]->SetIntValue( 0 );
  66. }
  67. if( !params[SELFILLUMTINT]->IsDefined() )
  68. {
  69. params[SELFILLUMTINT]->SetVecValue( 1.0f, 1.0f, 1.0f );
  70. }
  71. if( !params[DETAILSCALE]->IsDefined() )
  72. {
  73. params[DETAILSCALE]->SetFloatValue( 4.0f );
  74. }
  75. if( !params[BUMPFRAME]->IsDefined() )
  76. {
  77. params[BUMPFRAME]->SetIntValue( 0 );
  78. }
  79. if( !params[DETAILFRAME]->IsDefined() )
  80. {
  81. params[DETAILFRAME]->SetIntValue( 0 );
  82. }
  83. // No texture means no self-illum or env mask in base alpha
  84. if ( !params[BASETEXTURE]->IsDefined() )
  85. {
  86. CLEAR_FLAGS( MATERIAL_VAR_SELFILLUM );
  87. CLEAR_FLAGS( MATERIAL_VAR_BASEALPHAENVMAPMASK );
  88. }
  89. // If in decal mode, no debug override...
  90. if (IS_FLAG_SET(MATERIAL_VAR_DECAL))
  91. {
  92. SET_FLAGS( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  93. }
  94. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_LIGHTMAP );
  95. if( g_pConfig->UseBumpmapping() && params[BUMPMAP]->IsDefined() && (params[NODIFFUSEBUMPLIGHTING]->GetIntValue() == 0) )
  96. {
  97. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_BUMPED_LIGHTMAP );
  98. }
  99. // srgb read 360
  100. InitIntParam( SHADERSRGBREAD360, params, 0 );
  101. }
  102. SHADER_INIT
  103. {
  104. if( g_pConfig->UseBumpmapping() && params[BUMPMAP]->IsDefined() )
  105. {
  106. LoadBumpMap( BUMPMAP );
  107. }
  108. if (params[BASETEXTURE]->IsDefined())
  109. {
  110. LoadTexture( BASETEXTURE, TEXTUREFLAGS_SRGB );
  111. if (!params[BASETEXTURE]->GetTextureValue()->IsTranslucent())
  112. {
  113. CLEAR_FLAGS( MATERIAL_VAR_SELFILLUM );
  114. CLEAR_FLAGS( MATERIAL_VAR_BASEALPHAENVMAPMASK );
  115. }
  116. }
  117. if (params[DETAIL]->IsDefined())
  118. {
  119. LoadTexture( DETAIL );
  120. }
  121. LoadTexture( FLASHLIGHTTEXTURE, TEXTUREFLAGS_SRGB );
  122. // Don't alpha test if the alpha channel is used for other purposes
  123. if (IS_FLAG_SET(MATERIAL_VAR_SELFILLUM) || IS_FLAG_SET(MATERIAL_VAR_BASEALPHAENVMAPMASK) )
  124. {
  125. CLEAR_FLAGS( MATERIAL_VAR_ALPHATEST );
  126. }
  127. // We always need this because of the flashlight.
  128. SET_FLAGS2( MATERIAL_VAR2_NEEDS_TANGENT_SPACES );
  129. }
  130. void DrawPass( IMaterialVar** params, IShaderDynamicAPI *pShaderAPI,
  131. IShaderShadow* pShaderShadow, bool hasFlashlight, VertexCompressionType_t vertexCompression )
  132. {
  133. bool bSinglePassFlashlight = false;
  134. bool hasBump = params[BUMPMAP]->IsTexture();
  135. bool hasBaseTexture = params[BASETEXTURE]->IsTexture();
  136. bool hasDetailTexture = /*!hasBump && */params[DETAIL]->IsTexture();
  137. bool hasVertexColor = IS_FLAG_SET( MATERIAL_VAR_VERTEXCOLOR ) != 0;
  138. bool bHasDetailAlpha = params[DETAIL_ALPHA_MASK_BASE_TEXTURE]->GetIntValue() != 0;
  139. bool bIsAlphaTested = IS_FLAG_SET( MATERIAL_VAR_ALPHATEST ) != 0;
  140. BlendType_t nBlendType = EvaluateBlendRequirements( BASETEXTURE, true );
  141. bool bFullyOpaque = (nBlendType != BT_BLENDADD) && (nBlendType != BT_BLEND) && !IS_FLAG_SET(MATERIAL_VAR_ALPHATEST); //dest alpha is free for special use
  142. bool bSeamlessMapping = params[SEAMLESS_SCALE]->GetFloatValue() != 0.0;
  143. #if defined( CSTRIKE15 )
  144. bool bShaderSrgbRead = ( IsX360() && r_shader_srgbread.GetBool() );
  145. #else
  146. bool bShaderSrgbRead = ( IsX360() && IS_PARAM_DEFINED( SHADERSRGBREAD360 ) && params[SHADERSRGBREAD360]->GetIntValue() );
  147. #endif
  148. SHADOW_STATE
  149. {
  150. ShadowFilterMode_t nShadowFilterMode = SHADOWFILTERMODE_DEFAULT;
  151. // Alpha test: FIXME: shouldn't this be handled in Shader_t::SetInitialShadowState
  152. pShaderShadow->EnableAlphaTest( bIsAlphaTested );
  153. if( hasFlashlight )
  154. {
  155. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  156. {
  157. nShadowFilterMode = g_pHardwareConfig->GetShadowFilterMode( false /* bForceLowQuality */, g_pHardwareConfig->HasFastVertexTextures() && !IsPlatformX360() && !IsPlatformPS3() /* bPS30 */ ); // Based upon vendor and device dependent formats
  158. }
  159. SetAdditiveBlendingShadowState( BASETEXTURE, true );
  160. pShaderShadow->EnableDepthWrites( false );
  161. // Be sure not to write to dest alpha
  162. pShaderShadow->EnableAlphaWrites( false );
  163. }
  164. else
  165. {
  166. SetDefaultBlendingShadowState( BASETEXTURE, true );
  167. }
  168. unsigned int flags = VERTEX_POSITION;
  169. if( hasBaseTexture )
  170. {
  171. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  172. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, !bShaderSrgbRead );
  173. }
  174. // if( hasLightmap )
  175. {
  176. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  177. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE );
  178. }
  179. if( hasFlashlight )
  180. {
  181. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  182. pShaderShadow->EnableTexture( SHADER_SAMPLER7, true );
  183. //pShaderShadow->SetShadowDepthFiltering( SHADER_SAMPLER7 );
  184. flags |= VERTEX_TANGENT_S | VERTEX_TANGENT_T | VERTEX_NORMAL;
  185. }
  186. if( hasDetailTexture )
  187. {
  188. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  189. }
  190. if( hasBump )
  191. {
  192. pShaderShadow->EnableTexture( SHADER_SAMPLER4, true );
  193. }
  194. if( hasVertexColor )
  195. {
  196. flags |= VERTEX_COLOR;
  197. }
  198. // Normalizing cube map
  199. pShaderShadow->EnableTexture( SHADER_SAMPLER6, true );
  200. // texcoord0 : base texcoord
  201. // texcoord1 : lightmap texcoord
  202. // texcoord2 : lightmap texcoord offset
  203. int numTexCoords = 2;
  204. if( hasBump )
  205. {
  206. numTexCoords = 3;
  207. }
  208. pShaderShadow->VertexShaderVertexFormat( flags, numTexCoords, 0, 0 );
  209. // Pre-cache pixel shaders
  210. bool hasSelfIllum = IS_FLAG_SET( MATERIAL_VAR_SELFILLUM );
  211. pShaderShadow->EnableSRGBWrite( true );
  212. int nLightingPreviewMode = 0;
  213. #if 0
  214. int nLightingPreviewMode = IS_FLAG2_SET( MATERIAL_VAR2_USE_GBUFFER0 ) + 2 * IS_FLAG2_SET( MATERIAL_VAR2_USE_GBUFFER1 );
  215. #endif
  216. #if !defined( _X360 ) && !defined( _PS3 )
  217. if ( g_pHardwareConfig->HasFastVertexTextures() )
  218. {
  219. DECLARE_STATIC_VERTEX_SHADER( lightmappedgeneric_vs30 );
  220. SET_STATIC_VERTEX_SHADER_COMBO( ENVMAP_MASK, false );
  221. SET_STATIC_VERTEX_SHADER_COMBO( BUMPMASK, false );
  222. SET_STATIC_VERTEX_SHADER_COMBO( TANGENTSPACE, hasFlashlight );
  223. SET_STATIC_VERTEX_SHADER_COMBO( BUMPMAP, hasBump );
  224. SET_STATIC_VERTEX_SHADER_COMBO( VERTEXCOLOR, hasVertexColor );
  225. SET_STATIC_VERTEX_SHADER_COMBO( VERTEXALPHATEXBLENDFACTOR, false );
  226. SET_STATIC_VERTEX_SHADER_COMBO( SEAMLESS, bSeamlessMapping ); //( bumpmap_variant == 2 )?1:0);
  227. SET_STATIC_VERTEX_SHADER_COMBO( DETAILTEXTURE, hasDetailTexture );
  228. SET_STATIC_VERTEX_SHADER_COMBO( SELFILLUM, hasSelfIllum );
  229. SET_STATIC_VERTEX_SHADER_COMBO( FANCY_BLENDING, false );
  230. SET_STATIC_VERTEX_SHADER_COMBO( LIGHTING_PREVIEW, nLightingPreviewMode != 0 );
  231. SET_STATIC_VERTEX_SHADER_COMBO( PAINT, 0 );
  232. SET_STATIC_VERTEX_SHADER_COMBO( ADDBUMPMAPS, 0 );
  233. SET_STATIC_VERTEX_SHADER( lightmappedgeneric_vs30 );
  234. }
  235. else
  236. #endif
  237. {
  238. DECLARE_STATIC_VERTEX_SHADER( lightmappedgeneric_vs20 );
  239. SET_STATIC_VERTEX_SHADER_COMBO( ENVMAP_MASK, false );
  240. SET_STATIC_VERTEX_SHADER_COMBO( BUMPMASK, false );
  241. SET_STATIC_VERTEX_SHADER_COMBO( TANGENTSPACE, hasFlashlight );
  242. SET_STATIC_VERTEX_SHADER_COMBO( BUMPMAP, hasBump );
  243. SET_STATIC_VERTEX_SHADER_COMBO( VERTEXCOLOR, hasVertexColor );
  244. SET_STATIC_VERTEX_SHADER_COMBO( VERTEXALPHATEXBLENDFACTOR, false );
  245. SET_STATIC_VERTEX_SHADER_COMBO( SEAMLESS, bSeamlessMapping ); //( bumpmap_variant == 2 )?1:0);
  246. SET_STATIC_VERTEX_SHADER_COMBO( DETAILTEXTURE, hasDetailTexture );
  247. SET_STATIC_VERTEX_SHADER_COMBO( SELFILLUM, hasSelfIllum );
  248. SET_STATIC_VERTEX_SHADER_COMBO( FANCY_BLENDING, false );
  249. SET_STATIC_VERTEX_SHADER_COMBO( LIGHTING_PREVIEW, nLightingPreviewMode != 0 );
  250. SET_STATIC_VERTEX_SHADER_COMBO( PAINT, 0 );
  251. SET_STATIC_VERTEX_SHADER_COMBO( ADDBUMPMAPS, 0 );
  252. #if defined( _X360 ) || defined( _PS3 )
  253. SET_STATIC_VERTEX_SHADER_COMBO( FLASHLIGHT, hasFlashlight );
  254. #endif
  255. SET_STATIC_VERTEX_SHADER( lightmappedgeneric_vs20 );
  256. }
  257. #if !defined( _X360 ) && !defined( _PS3 )
  258. if ( g_pHardwareConfig->HasFastVertexTextures() )
  259. {
  260. DECLARE_STATIC_PIXEL_SHADER( worldtwotextureblend_ps30 );
  261. SET_STATIC_PIXEL_SHADER_COMBO( DETAILTEXTURE, hasDetailTexture );
  262. SET_STATIC_PIXEL_SHADER_COMBO( BUMPMAP, hasBump );
  263. SET_STATIC_PIXEL_SHADER_COMBO( VERTEXCOLOR, hasVertexColor );
  264. SET_STATIC_PIXEL_SHADER_COMBO( SELFILLUM, hasSelfIllum );
  265. SET_STATIC_PIXEL_SHADER_COMBO( DETAIL_ALPHA_MASK_BASE_TEXTURE, bHasDetailAlpha );
  266. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHT, hasFlashlight );
  267. SET_STATIC_PIXEL_SHADER_COMBO( SEAMLESS, bSeamlessMapping );
  268. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHTDEPTHFILTERMODE, nShadowFilterMode );
  269. SET_STATIC_PIXEL_SHADER_COMBO( SHADER_SRGB_READ, bShaderSrgbRead );
  270. SET_STATIC_PIXEL_SHADER( worldtwotextureblend_ps30 );
  271. }
  272. else
  273. #endif
  274. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  275. {
  276. DECLARE_STATIC_PIXEL_SHADER( worldtwotextureblend_ps20b );
  277. SET_STATIC_PIXEL_SHADER_COMBO( DETAILTEXTURE, hasDetailTexture );
  278. SET_STATIC_PIXEL_SHADER_COMBO( BUMPMAP, hasBump );
  279. SET_STATIC_PIXEL_SHADER_COMBO( VERTEXCOLOR, hasVertexColor );
  280. SET_STATIC_PIXEL_SHADER_COMBO( SELFILLUM, hasSelfIllum );
  281. SET_STATIC_PIXEL_SHADER_COMBO( DETAIL_ALPHA_MASK_BASE_TEXTURE, bHasDetailAlpha );
  282. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHT, hasFlashlight );
  283. SET_STATIC_PIXEL_SHADER_COMBO( SEAMLESS, bSeamlessMapping );
  284. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHTDEPTHFILTERMODE, nShadowFilterMode );
  285. SET_STATIC_PIXEL_SHADER_COMBO( SHADER_SRGB_READ, bShaderSrgbRead );
  286. SET_STATIC_PIXEL_SHADER( worldtwotextureblend_ps20b );
  287. }
  288. else
  289. {
  290. DECLARE_STATIC_PIXEL_SHADER( worldtwotextureblend_ps20 );
  291. SET_STATIC_PIXEL_SHADER_COMBO( DETAILTEXTURE, hasDetailTexture );
  292. SET_STATIC_PIXEL_SHADER_COMBO( BUMPMAP, hasBump );
  293. SET_STATIC_PIXEL_SHADER_COMBO( VERTEXCOLOR, hasVertexColor );
  294. SET_STATIC_PIXEL_SHADER_COMBO( SELFILLUM, hasSelfIllum );
  295. SET_STATIC_PIXEL_SHADER_COMBO( DETAIL_ALPHA_MASK_BASE_TEXTURE, bHasDetailAlpha );
  296. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHT, hasFlashlight );
  297. SET_STATIC_PIXEL_SHADER_COMBO( SEAMLESS, bSeamlessMapping );
  298. SET_STATIC_PIXEL_SHADER_COMBO( SHADER_SRGB_READ, bShaderSrgbRead );
  299. SET_STATIC_PIXEL_SHADER( worldtwotextureblend_ps20 );
  300. }
  301. // HACK HACK HACK - enable alpha writes all the time so that we have them for
  302. // underwater stuff.
  303. // But only do it if we're not using the alpha already for translucency
  304. pShaderShadow->EnableAlphaWrites( bFullyOpaque );
  305. if( hasFlashlight )
  306. {
  307. FogToBlack();
  308. }
  309. else
  310. {
  311. DefaultFog();
  312. }
  313. PI_BeginCommandBuffer();
  314. PI_SetModulationVertexShaderDynamicState( );
  315. PI_EndCommandBuffer();
  316. }
  317. DYNAMIC_STATE
  318. {
  319. if( hasBaseTexture )
  320. {
  321. BindTexture( SHADER_SAMPLER0, SRGBReadMask( !bShaderSrgbRead ), BASETEXTURE, FRAME );
  322. }
  323. else
  324. {
  325. ShaderApiFast( pShaderAPI )->BindStandardTexture( SHADER_SAMPLER0, SRGBReadMask( !bShaderSrgbRead ), TEXTURE_WHITE );
  326. }
  327. // if( hasLightmap )
  328. {
  329. ShaderApiFast( pShaderAPI )->BindStandardTexture( SHADER_SAMPLER1, ( g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE ) ? TEXTURE_BINDFLAGS_SRGBREAD : TEXTURE_BINDFLAGS_NONE, TEXTURE_LIGHTMAP );
  330. }
  331. bool bFlashlightShadows = false;
  332. bool bUberlight = false;
  333. if( hasFlashlight )
  334. {
  335. VMatrix worldToTexture;
  336. ITexture *pFlashlightDepthTexture;
  337. FlashlightState_t state = ShaderApiFast( pShaderAPI )->GetFlashlightStateEx( worldToTexture, &pFlashlightDepthTexture );
  338. bFlashlightShadows = state.m_bEnableShadows;
  339. bUberlight = state.m_bUberlight;
  340. SetFlashLightColorFromState( state, pShaderAPI, bSinglePassFlashlight );
  341. BindTexture( SHADER_SAMPLER2, TEXTURE_BINDFLAGS_NONE, state.m_pSpotlightTexture, state.m_nSpotlightTextureFrame );
  342. if( pFlashlightDepthTexture && g_pConfig->ShadowDepthTexture() )
  343. {
  344. BindTexture( SHADER_SAMPLER7, TEXTURE_BINDFLAGS_SHADOWDEPTH, pFlashlightDepthTexture );
  345. }
  346. }
  347. if( hasDetailTexture )
  348. {
  349. BindTexture( SHADER_SAMPLER3, TEXTURE_BINDFLAGS_NONE, DETAIL, DETAILFRAME );
  350. }
  351. if( hasBump )
  352. {
  353. if( !g_pConfig->m_bFastNoBump )
  354. {
  355. BindTexture( SHADER_SAMPLER4, TEXTURE_BINDFLAGS_NONE, BUMPMAP, BUMPFRAME );
  356. }
  357. else
  358. {
  359. ShaderApiFast( pShaderAPI )->BindStandardTexture( SHADER_SAMPLER4, TEXTURE_BINDFLAGS_NONE, TEXTURE_NORMALMAP_FLAT );
  360. }
  361. }
  362. ShaderApiFast( pShaderAPI )->BindStandardTexture( SHADER_SAMPLER6, TEXTURE_BINDFLAGS_NONE, TEXTURE_NORMALIZATION_CUBEMAP_SIGNED );
  363. // If we don't have a texture transform, we don't have
  364. // to set vertex shader constants or run vertex shader instructions
  365. // for the texture transform.
  366. bool bHasTextureTransform =
  367. !( params[BASETEXTURETRANSFORM]->MatrixIsIdentity() &&
  368. params[BUMPTRANSFORM]->MatrixIsIdentity() );
  369. bool bVertexShaderFastPath = !bHasTextureTransform;
  370. if( params[DETAIL]->IsTexture() )
  371. {
  372. bVertexShaderFastPath = false;
  373. }
  374. if( ShaderApiFast( pShaderAPI )->GetIntRenderingParameter(INT_RENDERPARM_ENABLE_FIXED_LIGHTING) != 0 )
  375. {
  376. bVertexShaderFastPath = false;
  377. }
  378. if( !bVertexShaderFastPath )
  379. {
  380. if ( !bSeamlessMapping )
  381. {
  382. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, BASETEXTURETRANSFORM );
  383. }
  384. if( hasBump && !bHasDetailAlpha )
  385. {
  386. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_2, BUMPTRANSFORM );
  387. Assert( !hasDetailTexture );
  388. }
  389. }
  390. MaterialFogMode_t fogType = ShaderApiFast( pShaderAPI )->GetSceneFogMode();
  391. if ( IsPC() )
  392. {
  393. bool bWorldNormal = ShaderApiFast( pShaderAPI )->GetIntRenderingParameter( INT_RENDERPARM_ENABLE_FIXED_LIGHTING ) == ENABLE_FIXED_LIGHTING_OUTPUTNORMAL_AND_DEPTH;
  394. if ( bWorldNormal )
  395. {
  396. float vEyeDir[4];
  397. ShaderApiFast( pShaderAPI )->GetWorldSpaceCameraDirection( vEyeDir );
  398. float flFarZ = ShaderApiFast( pShaderAPI )->GetFarZ();
  399. vEyeDir[0] /= flFarZ; // Divide by farZ for SSAO algorithm
  400. vEyeDir[1] /= flFarZ;
  401. vEyeDir[2] /= flFarZ;
  402. ShaderApiFast( pShaderAPI )->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_12, vEyeDir );
  403. }
  404. }
  405. #if !defined( _X360 ) && !defined( _PS3 )
  406. if (g_pHardwareConfig->HasFastVertexTextures() )
  407. {
  408. DECLARE_DYNAMIC_VERTEX_SHADER( lightmappedgeneric_vs30 );
  409. SET_DYNAMIC_VERTEX_SHADER_COMBO( FASTPATH, bVertexShaderFastPath );
  410. SET_DYNAMIC_VERTEX_SHADER( lightmappedgeneric_vs30 );
  411. }
  412. else
  413. #endif
  414. {
  415. DECLARE_DYNAMIC_VERTEX_SHADER( lightmappedgeneric_vs20 );
  416. SET_DYNAMIC_VERTEX_SHADER_COMBO( FASTPATH, bVertexShaderFastPath );
  417. SET_DYNAMIC_VERTEX_SHADER( lightmappedgeneric_vs20 );
  418. }
  419. bool bWriteDepthToAlpha;
  420. bool bWriteWaterFogToAlpha;
  421. if( bFullyOpaque )
  422. {
  423. bWriteDepthToAlpha = ShaderApiFast( pShaderAPI )->ShouldWriteDepthToDestAlpha();
  424. bWriteWaterFogToAlpha = (fogType == MATERIAL_FOG_LINEAR_BELOW_FOG_Z);
  425. AssertMsg( !(bWriteDepthToAlpha && bWriteWaterFogToAlpha), "Can't write two values to alpha at the same time." );
  426. }
  427. else
  428. {
  429. //can't write a special value to dest alpha if we're actually using as-intended alpha
  430. bWriteDepthToAlpha = false;
  431. bWriteWaterFogToAlpha = false;
  432. }
  433. #if !defined( _X360 ) && !defined( _PS3 )
  434. if ( g_pHardwareConfig->HasFastVertexTextures() )
  435. {
  436. DECLARE_DYNAMIC_PIXEL_SHADER( worldtwotextureblend_ps30 );
  437. // Don't write fog to alpha if we're using translucency
  438. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITEWATERFOGTODESTALPHA, bWriteWaterFogToAlpha );
  439. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITE_DEPTH_TO_DESTALPHA, bWriteDepthToAlpha );
  440. SET_DYNAMIC_PIXEL_SHADER_COMBO( FLASHLIGHTSHADOWS, bFlashlightShadows );
  441. SET_DYNAMIC_PIXEL_SHADER_COMBO( UBERLIGHT, bUberlight );
  442. SET_DYNAMIC_PIXEL_SHADER( worldtwotextureblend_ps30 );
  443. }
  444. else
  445. #endif
  446. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  447. {
  448. DECLARE_DYNAMIC_PIXEL_SHADER( worldtwotextureblend_ps20b );
  449. // Don't write fog to alpha if we're using translucency
  450. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITEWATERFOGTODESTALPHA, bWriteWaterFogToAlpha );
  451. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITE_DEPTH_TO_DESTALPHA, bWriteDepthToAlpha );
  452. SET_DYNAMIC_PIXEL_SHADER_COMBO( FLASHLIGHTSHADOWS, bFlashlightShadows );
  453. SET_DYNAMIC_PIXEL_SHADER( worldtwotextureblend_ps20b );
  454. }
  455. else
  456. {
  457. DECLARE_DYNAMIC_PIXEL_SHADER( worldtwotextureblend_ps20 );
  458. // Don't write fog to alpha if we're using translucency
  459. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITEWATERFOGTODESTALPHA, (fogType == MATERIAL_FOG_LINEAR_BELOW_FOG_Z) &&
  460. (nBlendType != BT_BLENDADD) && (nBlendType != BT_BLEND) && !bIsAlphaTested );
  461. SET_DYNAMIC_PIXEL_SHADER( worldtwotextureblend_ps20 );
  462. }
  463. // always set the transform for detail textures since I'm assuming that you'll
  464. // always have a detailscale.
  465. if( hasDetailTexture )
  466. {
  467. SetVertexShaderTextureScaledTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_2, BASETEXTURETRANSFORM, DETAILSCALE );
  468. Assert( !( hasBump && !bHasDetailAlpha ) );
  469. }
  470. SetPixelShaderConstantGammaToLinear( 7, SELFILLUMTINT );
  471. float eyePos[4];
  472. ShaderApiFast( pShaderAPI )->GetWorldSpaceCameraPosition( eyePos );
  473. ShaderApiFast( pShaderAPI )->SetPixelShaderConstant( 10, eyePos, 1 );
  474. ShaderApiFast( pShaderAPI )->SetPixelShaderFogParams( 11 );
  475. if ( bSeamlessMapping )
  476. {
  477. float map_scale[4]={ params[SEAMLESS_SCALE]->GetFloatValue(),0,0,0};
  478. ShaderApiFast( pShaderAPI )->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, map_scale );
  479. }
  480. if( hasFlashlight )
  481. {
  482. VMatrix worldToTexture;
  483. const FlashlightState_t &flashlightState = ShaderApiFast( pShaderAPI )->GetFlashlightState( worldToTexture );
  484. // Set the flashlight attenuation factors
  485. float atten[4];
  486. atten[0] = flashlightState.m_fConstantAtten;
  487. atten[1] = flashlightState.m_fLinearAtten;
  488. atten[2] = flashlightState.m_fQuadraticAtten;
  489. atten[3] = flashlightState.m_FarZAtten;
  490. ShaderApiFast( pShaderAPI )->SetPixelShaderConstant( 20, atten, 1 );
  491. // Set the flashlight origin
  492. float pos[4];
  493. pos[0] = flashlightState.m_vecLightOrigin[0];
  494. pos[1] = flashlightState.m_vecLightOrigin[1];
  495. pos[2] = flashlightState.m_vecLightOrigin[2];
  496. pos[3] = flashlightState.m_FarZ; // didn't have this in main. . probably need this?
  497. ShaderApiFast( pShaderAPI )->SetPixelShaderConstant( 15, pos, 1 );
  498. ShaderApiFast( pShaderAPI )->SetPixelShaderConstant( 16, worldToTexture.Base(), 4 );
  499. if ( IsPC() && g_pHardwareConfig->HasFastVertexTextures() )
  500. {
  501. SetupUberlightFromState( pShaderAPI, flashlightState );
  502. }
  503. }
  504. }
  505. Draw();
  506. }
  507. SHADER_DRAW
  508. {
  509. bool bHasFlashlight = UsingFlashlight( params );
  510. if ( bHasFlashlight && ( IsX360() || IsPS3() ) )
  511. {
  512. DrawPass( params, pShaderAPI, pShaderShadow, false, vertexCompression );
  513. SHADOW_STATE
  514. {
  515. SetInitialShadowState( );
  516. }
  517. }
  518. DrawPass( params, pShaderAPI, pShaderShadow, bHasFlashlight, vertexCompression );
  519. }
  520. END_SHADER