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.

515 lines
21 KiB

  1. //===== Copyright (c) 1996-2008, Valve Corporation, All rights reserved. ======//
  2. #include "BaseVSShader.h"
  3. #include "mathlib/vmatrix.h"
  4. #include "eye_refract_helper.h"
  5. #include "cpp_shader_constant_register_map.h"
  6. #include "eye_refract_vs20.inc"
  7. #include "eye_refract_ps20.inc"
  8. #include "eye_refract_ps20b.inc"
  9. #if !defined( _X360 ) && !defined( _PS3 )
  10. #include "eye_refract_vs30.inc"
  11. #include "eye_refract_ps30.inc"
  12. #endif
  13. #include "convar.h"
  14. // NOTE: This has to be the last file included!
  15. #include "tier0/memdbgon.h"
  16. static ConVar r_lightwarpidentity( "r_lightwarpidentity","0", FCVAR_CHEAT );
  17. static ConVar mat_displacementmap( "mat_displacementmap", "1", FCVAR_CHEAT );
  18. void InitParams_Eyes_Refract( CBaseVSShader *pShader, IMaterialVar** params, const char *pMaterialName, Eye_Refract_Vars_t &info )
  19. {
  20. params[FLASHLIGHTTEXTURE]->SetStringValue( GetFlashlightTextureFilename() );
  21. // Set material flags
  22. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  23. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_VERTEX_LIT );
  24. // Set material parameter default values
  25. if ( ( info.m_nDilation >= 0 ) && ( !params[info.m_nDilation]->IsDefined() ) )
  26. {
  27. params[info.m_nDilation]->SetFloatValue( kDefaultDilation );
  28. }
  29. if ( ( info.m_nGlossiness >= 0 ) && ( !params[info.m_nGlossiness]->IsDefined() ) )
  30. {
  31. params[info.m_nGlossiness]->SetFloatValue( kDefaultGlossiness );
  32. }
  33. if ( ( info.m_nSphereTexKillCombo >= 0 ) && ( !params[info.m_nSphereTexKillCombo]->IsDefined() ) )
  34. {
  35. params[info.m_nSphereTexKillCombo]->SetIntValue( kDefaultSphereTexKillCombo );
  36. }
  37. if ( ( info.m_nRaytraceSphere >= 0 ) && ( !params[info.m_nRaytraceSphere]->IsDefined() ) )
  38. {
  39. params[info.m_nRaytraceSphere]->SetIntValue( kDefaultRaytraceSphere );
  40. }
  41. if ( ( info.m_nAmbientOcclColor >= 0 ) && ( !params[info.m_nAmbientOcclColor]->IsDefined() ) )
  42. {
  43. params[info.m_nAmbientOcclColor]->SetVecValue( kDefaultAmbientOcclColor, 4 );
  44. }
  45. if ( ( info.m_nEyeballRadius >= 0 ) && ( !params[info.m_nEyeballRadius]->IsDefined() ) )
  46. {
  47. params[info.m_nEyeballRadius]->SetFloatValue( kDefaultEyeballRadius );
  48. }
  49. if ( ( info.m_nParallaxStrength >= 0 ) && ( !params[info.m_nParallaxStrength]->IsDefined() ) )
  50. {
  51. params[info.m_nParallaxStrength]->SetFloatValue( kDefaultParallaxStrength );
  52. }
  53. if ( ( info.m_nCorneaBumpStrength >= 0 ) && ( !params[info.m_nCorneaBumpStrength]->IsDefined() ) )
  54. {
  55. params[info.m_nCorneaBumpStrength]->SetFloatValue( kDefaultCorneaBumpStrength );
  56. }
  57. if ( ( info.m_nAmbientOcclusion >= 0 ) && ( !params[info.m_nAmbientOcclusion]->IsDefined() ) )
  58. {
  59. params[info.m_nAmbientOcclusion]->SetFloatValue( 0.0f );
  60. }
  61. }
  62. void Init_Eyes_Refract( CBaseVSShader *pShader, IMaterialVar** params, Eye_Refract_Vars_t &info )
  63. {
  64. pShader->LoadTexture( info.m_nCorneaTexture ); // SHADER_SAMPLER0 (this is a normal, hence not sRGB)
  65. pShader->LoadTexture( info.m_nIris, TEXTUREFLAGS_SRGB ); // SHADER_SAMPLER1
  66. pShader->LoadCubeMap( info.m_nEnvmap, TEXTUREFLAGS_SRGB ); // SHADER_SAMPLER2
  67. pShader->LoadTexture( info.m_nAmbientOcclTexture, TEXTUREFLAGS_SRGB ); // SHADER_SAMPLER3
  68. if ( IS_PARAM_DEFINED( info.m_nDiffuseWarpTexture ) )
  69. {
  70. pShader->LoadTexture( info.m_nDiffuseWarpTexture ); // SHADER_SAMPLER4
  71. }
  72. pShader->LoadTexture( FLASHLIGHTTEXTURE, TEXTUREFLAGS_SRGB ); // SHADER_SAMPLER5
  73. }
  74. void Draw_Eyes_Refract_Internal( CBaseVSShader *pShader, IMaterialVar** params, IShaderDynamicAPI *pShaderAPI,
  75. IShaderShadow* pShaderShadow, bool bDrawFlashlightAdditivePass, Eye_Refract_Vars_t &info, VertexCompressionType_t vertexCompression )
  76. {
  77. bool bDiffuseWarp = IS_PARAM_DEFINED( info.m_nDiffuseWarpTexture );
  78. SHADOW_STATE
  79. {
  80. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_VERTEX_LIT );
  81. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true ); // Cornea normal
  82. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true ); // Iris
  83. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true ); // Cube reflection
  84. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true ); // Ambient occlusion
  85. // Set stream format (note that this shader supports compression)
  86. unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL | VERTEX_FORMAT_COMPRESSED;
  87. int nTexCoordCount = 1;
  88. int userDataSize = 0;
  89. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, NULL, userDataSize );
  90. if ( bDiffuseWarp )
  91. {
  92. pShaderShadow->EnableTexture( SHADER_SAMPLER4, true ); // Light warp
  93. }
  94. #if !defined( PLATFORM_X360 )
  95. bool bWorldNormal = ( ENABLE_FIXED_LIGHTING_OUTPUTNORMAL_AND_DEPTH == ( IS_FLAG2_SET( MATERIAL_VAR2_USE_GBUFFER0 ) + 2 * IS_FLAG2_SET( MATERIAL_VAR2_USE_GBUFFER1 )));
  96. #endif
  97. ShadowFilterMode_t nShadowFilterMode = SHADOWFILTERMODE_DEFAULT;
  98. if ( bDrawFlashlightAdditivePass == true )
  99. {
  100. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  101. {
  102. nShadowFilterMode = g_pHardwareConfig->GetShadowFilterMode( false /* bForceLowQuality */, g_pHardwareConfig->HasFastVertexTextures() && !IsPlatformX360() && !IsPlatformPS3() ); // Based upon vendor and device dependent formats
  103. }
  104. pShaderShadow->EnableDepthWrites( false );
  105. pShaderShadow->EnableAlphaWrites( false );
  106. pShader->EnableAlphaBlending( SHADER_BLEND_ONE, SHADER_BLEND_ONE ); // Additive blending
  107. pShaderShadow->EnableTexture( SHADER_SAMPLER5, true ); // Flashlight cookie
  108. }
  109. else
  110. {
  111. pShaderShadow->EnableAlphaWrites( true );
  112. }
  113. #if !defined( _X360 ) && !defined( _PS3 )
  114. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  115. #endif
  116. {
  117. DECLARE_STATIC_VERTEX_SHADER( eye_refract_vs20 );
  118. SET_STATIC_VERTEX_SHADER_COMBO( HALFLAMBERT, IS_FLAG_SET( MATERIAL_VAR_HALFLAMBERT ) );
  119. SET_STATIC_VERTEX_SHADER_COMBO( FLASHLIGHT, bDrawFlashlightAdditivePass ? 1 : 0 );
  120. SET_STATIC_VERTEX_SHADER_COMBO( LIGHTWARPTEXTURE, bDiffuseWarp ? 1 : 0 );
  121. SET_STATIC_VERTEX_SHADER_COMBO( WORLD_NORMAL, 0 );
  122. SET_STATIC_VERTEX_SHADER( eye_refract_vs20 );
  123. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  124. {
  125. bool bSphereTexKillCombo = IS_PARAM_DEFINED( info.m_nSphereTexKillCombo ) ? ( params[info.m_nSphereTexKillCombo]->GetIntValue() ? true : false ) : ( kDefaultSphereTexKillCombo ? true : false );
  126. bool bRayTraceSphere = IS_PARAM_DEFINED( info.m_nRaytraceSphere ) ? ( params[info.m_nRaytraceSphere]->GetIntValue() ? true : false ) : ( kDefaultRaytraceSphere ? true : false );
  127. DECLARE_STATIC_PIXEL_SHADER( eye_refract_ps20b );
  128. SET_STATIC_PIXEL_SHADER_COMBO( SPHERETEXKILLCOMBO, bSphereTexKillCombo ? 1 : 0 );
  129. SET_STATIC_PIXEL_SHADER_COMBO( RAYTRACESPHERE, bRayTraceSphere ? 1 : 0 );
  130. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHT, bDrawFlashlightAdditivePass ? 1 : 0 );
  131. SET_STATIC_PIXEL_SHADER_COMBO( LIGHTWARPTEXTURE, bDiffuseWarp ? 1 : 0 );
  132. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHTDEPTHFILTERMODE, nShadowFilterMode );
  133. SET_STATIC_PIXEL_SHADER_COMBO( WORLD_NORMAL, 0 );
  134. SET_STATIC_PIXEL_SHADER( eye_refract_ps20b );
  135. if ( bDrawFlashlightAdditivePass == true )
  136. {
  137. pShaderShadow->EnableTexture( SHADER_SAMPLER6, true ); // Shadow depth map
  138. //pShaderShadow->SetShadowDepthFiltering( SHADER_SAMPLER6 );
  139. pShaderShadow->EnableTexture( SHADER_SAMPLER7, true ); // Noise map
  140. }
  141. }
  142. else
  143. {
  144. DECLARE_STATIC_PIXEL_SHADER( eye_refract_ps20 );
  145. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHT, bDrawFlashlightAdditivePass ? 1 : 0 );
  146. SET_STATIC_PIXEL_SHADER_COMBO( LIGHTWARPTEXTURE, bDiffuseWarp ? 1 : 0 );
  147. SET_STATIC_PIXEL_SHADER_COMBO( WORLD_NORMAL, 0 );
  148. SET_STATIC_PIXEL_SHADER( eye_refract_ps20 );
  149. }
  150. }
  151. #if !defined( _X360 ) && !defined( _PS3 )
  152. else
  153. {
  154. pShaderShadow->EnableTexture( SHADER_SAMPLER8, true ); // Screen space ambient occlusion
  155. // The vertex shader uses the vertex id stream
  156. SET_FLAGS2( MATERIAL_VAR2_USES_VERTEXID );
  157. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_TESSELLATION );
  158. DECLARE_STATIC_VERTEX_SHADER( eye_refract_vs30 );
  159. SET_STATIC_VERTEX_SHADER_COMBO( HALFLAMBERT, IS_FLAG_SET( MATERIAL_VAR_HALFLAMBERT ) );
  160. SET_STATIC_VERTEX_SHADER_COMBO( FLASHLIGHT, bDrawFlashlightAdditivePass ? 1 : 0 );
  161. SET_STATIC_VERTEX_SHADER_COMBO( LIGHTWARPTEXTURE, bDiffuseWarp ? 1 : 0 );
  162. SET_STATIC_VERTEX_SHADER_COMBO( WORLD_NORMAL, bWorldNormal );
  163. SET_STATIC_VERTEX_SHADER( eye_refract_vs30 );
  164. bool bSphereTexKillCombo = IS_PARAM_DEFINED( info.m_nSphereTexKillCombo ) ? ( params[info.m_nSphereTexKillCombo]->GetIntValue() ? true : false ) : ( kDefaultSphereTexKillCombo ? true : false );
  165. bool bRayTraceSphere = IS_PARAM_DEFINED( info.m_nRaytraceSphere ) ? ( params[info.m_nRaytraceSphere]->GetIntValue() ? true : false ) : ( kDefaultRaytraceSphere ? true : false );
  166. DECLARE_STATIC_PIXEL_SHADER( eye_refract_ps30 );
  167. SET_STATIC_PIXEL_SHADER_COMBO( SPHERETEXKILLCOMBO, bSphereTexKillCombo ? 1 : 0 );
  168. SET_STATIC_PIXEL_SHADER_COMBO( RAYTRACESPHERE, bRayTraceSphere ? 1 : 0 );
  169. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHT, bDrawFlashlightAdditivePass ? 1 : 0 );
  170. SET_STATIC_PIXEL_SHADER_COMBO( LIGHTWARPTEXTURE, bDiffuseWarp ? 1 : 0 );
  171. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHTDEPTHFILTERMODE, nShadowFilterMode );
  172. SET_STATIC_PIXEL_SHADER_COMBO( WORLD_NORMAL, bWorldNormal );
  173. SET_STATIC_PIXEL_SHADER( eye_refract_ps30 );
  174. if ( bDrawFlashlightAdditivePass == true )
  175. {
  176. pShaderShadow->EnableTexture( SHADER_SAMPLER6, true ); // Shadow depth map
  177. pShaderShadow->EnableTexture( SHADER_SAMPLER7, true ); // Noise map
  178. }
  179. }
  180. #endif
  181. // On DX9, get the gamma read and write correct
  182. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, true ); // Iris
  183. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER2, true ); // Cube map reflection
  184. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER3, true ); // Ambient occlusion
  185. pShaderShadow->EnableSRGBWrite( true );
  186. if ( bDrawFlashlightAdditivePass == true )
  187. {
  188. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER5, true ); // Flashlight cookie
  189. }
  190. // Fog
  191. if ( bDrawFlashlightAdditivePass == true )
  192. {
  193. pShader->FogToBlack();
  194. }
  195. else
  196. {
  197. pShader->FogToFogColor();
  198. }
  199. // Per-instance state
  200. pShader->PI_BeginCommandBuffer();
  201. if ( !bDrawFlashlightAdditivePass )
  202. {
  203. pShader->PI_SetPixelShaderLocalLighting( PSREG_LIGHT_INFO_ARRAY );
  204. }
  205. pShader->PI_SetVertexShaderAmbientLightCube();
  206. pShader->PI_SetPixelShaderAmbientLightCubeLuminance( 10 );
  207. pShader->PI_EndCommandBuffer();
  208. }
  209. DYNAMIC_STATE
  210. {
  211. VMatrix worldToTexture;
  212. ITexture *pFlashlightDepthTexture = NULL;
  213. FlashlightState_t flashlightState;
  214. bool bFlashlightShadows = false;
  215. if ( bDrawFlashlightAdditivePass == true )
  216. {
  217. flashlightState = pShaderAPI->GetFlashlightStateEx( worldToTexture, &pFlashlightDepthTexture );
  218. bFlashlightShadows = flashlightState.m_bEnableShadows;
  219. }
  220. bool bSinglePassFlashlight = false;
  221. pShader->BindTexture( SHADER_SAMPLER0, TEXTURE_BINDFLAGS_NONE, info.m_nCorneaTexture ); // Cornea normal
  222. pShader->BindTexture( SHADER_SAMPLER1, TEXTURE_BINDFLAGS_SRGBREAD, info.m_nIris, info.m_nIrisFrame );
  223. pShader->BindTexture( SHADER_SAMPLER2, TEXTURE_BINDFLAGS_SRGBREAD, info.m_nEnvmap );
  224. pShader->BindTexture( SHADER_SAMPLER3, TEXTURE_BINDFLAGS_SRGBREAD, info.m_nAmbientOcclTexture );
  225. if ( bDiffuseWarp )
  226. {
  227. if ( r_lightwarpidentity.GetBool() )
  228. {
  229. pShaderAPI->BindStandardTexture( SHADER_SAMPLER4, TEXTURE_BINDFLAGS_NONE, TEXTURE_IDENTITY_LIGHTWARP );
  230. }
  231. else
  232. {
  233. pShader->BindTexture( SHADER_SAMPLER4, TEXTURE_BINDFLAGS_NONE, info.m_nDiffuseWarpTexture );
  234. }
  235. }
  236. // On PC, we sample from ambient occlusion texture
  237. if ( IsPC() && g_pHardwareConfig->HasFastVertexTextures() )
  238. {
  239. ITexture *pAOTexture = pShaderAPI->GetTextureRenderingParameter( TEXTURE_RENDERPARM_AMBIENT_OCCLUSION );
  240. if ( pAOTexture )
  241. {
  242. pShader->BindTexture( SHADER_SAMPLER8, TEXTURE_BINDFLAGS_NONE, pAOTexture );
  243. }
  244. else
  245. {
  246. pShaderAPI->BindStandardTexture( SHADER_SAMPLER8, TEXTURE_BINDFLAGS_NONE, TEXTURE_WHITE );
  247. }
  248. }
  249. if ( bDrawFlashlightAdditivePass == true )
  250. pShader->BindTexture( SHADER_SAMPLER5, TEXTURE_BINDFLAGS_SRGBREAD, flashlightState.m_pSpotlightTexture, flashlightState.m_nSpotlightTextureFrame );
  251. pShader->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, info.m_nEyeOrigin );
  252. pShader->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_2, info.m_nIrisU );
  253. pShader->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_3, info.m_nIrisV );
  254. if ( bDrawFlashlightAdditivePass == true )
  255. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_4, flashlightState.m_vecLightOrigin.Base(), 1 );
  256. LightState_t lightState = { 0, false, false };
  257. if ( bDrawFlashlightAdditivePass == false )
  258. {
  259. pShaderAPI->GetDX9LightState( &lightState );
  260. }
  261. int nFixedLightingMode = pShaderAPI->GetIntRenderingParameter( INT_RENDERPARM_ENABLE_FIXED_LIGHTING );
  262. #if !defined( _X360 ) && !defined( _PS3 )
  263. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  264. #endif
  265. {
  266. DECLARE_DYNAMIC_VERTEX_SHADER( eye_refract_vs20 );
  267. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  268. SET_DYNAMIC_VERTEX_SHADER_COMBO( DYNAMIC_LIGHT, lightState.HasDynamicLight() );
  269. SET_DYNAMIC_VERTEX_SHADER_COMBO( NUM_LIGHTS, lightState.m_nNumLights );
  270. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  271. SET_DYNAMIC_VERTEX_SHADER_COMBO( TESSELLATION, 0 );
  272. SET_DYNAMIC_VERTEX_SHADER( eye_refract_vs20 );
  273. }
  274. #if !defined( _X360 ) && !defined( _PS3 )
  275. else
  276. {
  277. pShader->SetHWMorphVertexShaderState( VERTEX_SHADER_SHADER_SPECIFIC_CONST_10, VERTEX_SHADER_SHADER_SPECIFIC_CONST_11, SHADER_VERTEXTEXTURE_SAMPLER0 );
  278. if ( nFixedLightingMode == ENABLE_FIXED_LIGHTING_OUTPUTNORMAL_AND_DEPTH )
  279. {
  280. float vEyeDir[4];
  281. pShaderAPI->GetWorldSpaceCameraDirection( vEyeDir );
  282. float flFarZ = pShaderAPI->GetFarZ();
  283. vEyeDir[0] /= flFarZ; // Divide by farZ for SSAO algorithm
  284. vEyeDir[1] /= flFarZ;
  285. vEyeDir[2] /= flFarZ;
  286. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_5, vEyeDir );
  287. }
  288. TessellationMode_t nTessellationMode = pShaderAPI->GetTessellationMode();
  289. if ( nTessellationMode != TESSELLATION_MODE_DISABLED )
  290. {
  291. pShaderAPI->BindStandardVertexTexture( SHADER_VERTEXTEXTURE_SAMPLER1, TEXTURE_SUBDIVISION_PATCHES );
  292. bool bHasDisplacement = false; // TODO
  293. float vSubDDimensions[4] = { 1.0f/pShaderAPI->GetSubDHeight(), bHasDisplacement && mat_displacementmap.GetBool() ? 1.0f : 0.0f, 0.0f, 0.0f };
  294. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_1, vSubDDimensions );
  295. }
  296. DECLARE_DYNAMIC_VERTEX_SHADER( eye_refract_vs30 );
  297. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  298. SET_DYNAMIC_VERTEX_SHADER_COMBO( DYNAMIC_LIGHT, lightState.HasDynamicLight() );
  299. SET_DYNAMIC_VERTEX_SHADER_COMBO( NUM_LIGHTS, lightState.m_nNumLights );
  300. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  301. SET_DYNAMIC_VERTEX_SHADER_COMBO( TESSELLATION, nTessellationMode );
  302. SET_DYNAMIC_VERTEX_SHADER( eye_refract_vs30 );
  303. }
  304. #endif
  305. // Special constant for DX9 eyes: { Dilation, Glossiness, x, x };
  306. float vPSConst[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  307. vPSConst[0] = IS_PARAM_DEFINED( info.m_nDilation ) ? params[info.m_nDilation]->GetFloatValue() : kDefaultDilation;
  308. vPSConst[1] = IS_PARAM_DEFINED( info.m_nGlossiness ) ? params[info.m_nGlossiness]->GetFloatValue() : kDefaultGlossiness;
  309. vPSConst[2] = 0.0f; // NOT USED
  310. vPSConst[3] = IS_PARAM_DEFINED( info.m_nCorneaBumpStrength ) ? params[info.m_nCorneaBumpStrength]->GetFloatValue() : kDefaultCorneaBumpStrength;
  311. pShaderAPI->SetPixelShaderConstant( 0, vPSConst, 1 );
  312. pShaderAPI->SetPixelShaderConstant( 1, IS_PARAM_DEFINED( info.m_nEyeOrigin ) ? params[info.m_nEyeOrigin]->GetVecValue() : kDefaultEyeOrigin, 1 );
  313. pShaderAPI->SetPixelShaderConstant( 2, IS_PARAM_DEFINED( info.m_nIrisU ) ? params[info.m_nIrisU]->GetVecValue() : kDefaultIrisU, 1 );
  314. pShaderAPI->SetPixelShaderConstant( 3, IS_PARAM_DEFINED( info.m_nIrisV ) ? params[info.m_nIrisV]->GetVecValue() : kDefaultIrisV, 1 );
  315. float vEyePos[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  316. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos );
  317. pShaderAPI->SetPixelShaderConstant( 4, vEyePos, 1 );
  318. float vAmbientOcclusion[4] = { 0.33f, 0.33f, 0.33f, 0.0f };
  319. if ( IS_PARAM_DEFINED( info.m_nAmbientOcclColor ) )
  320. {
  321. params[info.m_nAmbientOcclColor]->GetVecValue( vAmbientOcclusion, 3 );
  322. }
  323. vAmbientOcclusion[3] = IS_PARAM_DEFINED( info.m_nAmbientOcclusion ) ? params[info.m_nAmbientOcclusion]->GetFloatValue() : 0.0f;
  324. float vPackedConst6[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
  325. //vPackedConst6[0] Unused
  326. vPackedConst6[1] = IS_PARAM_DEFINED( info.m_nEyeballRadius ) ? params[info.m_nEyeballRadius]->GetFloatValue() : kDefaultEyeballRadius;
  327. //vPackedConst6[2] = IS_PARAM_DEFINED( info.m_nRaytraceSphere ) ? params[info.m_nRaytraceSphere]->GetFloatValue() : kDefaultRaytraceSphere;
  328. vPackedConst6[3] = IS_PARAM_DEFINED( info.m_nParallaxStrength ) ? params[info.m_nParallaxStrength]->GetFloatValue() : kDefaultParallaxStrength;
  329. pShaderAPI->SetPixelShaderConstant( 6, vPackedConst6, 1 );
  330. if ( bDrawFlashlightAdditivePass == true )
  331. {
  332. SetFlashLightColorFromState( flashlightState, pShaderAPI, bSinglePassFlashlight );
  333. if ( pFlashlightDepthTexture && g_pConfig->ShadowDepthTexture() && flashlightState.m_bEnableShadows )
  334. {
  335. pShader->BindTexture( SHADER_SAMPLER6, TEXTURE_BINDFLAGS_SHADOWDEPTH, pFlashlightDepthTexture, 0 );
  336. pShaderAPI->BindStandardTexture( SHADER_SAMPLER7, TEXTURE_BINDFLAGS_NONE, TEXTURE_SHADOW_NOISE_2D );
  337. }
  338. }
  339. if ( nFixedLightingMode == ENABLE_FIXED_LIGHTING_OUTPUTNORMAL_AND_DEPTH )
  340. {
  341. float vEyeDir[4];
  342. pShaderAPI->GetWorldSpaceCameraDirection( vEyeDir );
  343. float flFarZ = pShaderAPI->GetFarZ();
  344. vEyeDir[0] /= flFarZ; // Divide by farZ for SSAO algorithm
  345. vEyeDir[1] /= flFarZ;
  346. vEyeDir[2] /= flFarZ;
  347. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_5, vEyeDir );
  348. }
  349. // Flashlight tax
  350. #if !defined( _X360 ) && !defined( _PS3 )
  351. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  352. #endif
  353. {
  354. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  355. {
  356. DECLARE_DYNAMIC_PIXEL_SHADER( eye_refract_ps20b );
  357. SET_DYNAMIC_PIXEL_SHADER_COMBO( NUM_LIGHTS, lightState.m_nNumLights );
  358. SET_DYNAMIC_PIXEL_SHADER_COMBO( FLASHLIGHTSHADOWS, bFlashlightShadows );
  359. SET_DYNAMIC_PIXEL_SHADER( eye_refract_ps20b );
  360. }
  361. else // ps.2.0
  362. {
  363. DECLARE_DYNAMIC_PIXEL_SHADER( eye_refract_ps20 );
  364. SET_DYNAMIC_PIXEL_SHADER_COMBO( NUM_LIGHTS, lightState.m_nNumLights );
  365. SET_DYNAMIC_PIXEL_SHADER( eye_refract_ps20 );
  366. }
  367. }
  368. #if !defined( _X360 ) && !defined( _PS3 )
  369. else
  370. {
  371. DECLARE_DYNAMIC_PIXEL_SHADER( eye_refract_ps30 );
  372. SET_DYNAMIC_PIXEL_SHADER_COMBO( NUM_LIGHTS, nFixedLightingMode ? 0 : lightState.m_nNumLights );
  373. SET_DYNAMIC_PIXEL_SHADER_COMBO( FLASHLIGHTSHADOWS, nFixedLightingMode ? false : bFlashlightShadows );
  374. SET_DYNAMIC_PIXEL_SHADER_COMBO( UBERLIGHT, flashlightState.m_bUberlight );
  375. SET_DYNAMIC_PIXEL_SHADER( eye_refract_ps30 );
  376. // Set constant to enable translation of VPOS to render target coordinates in ps_3_0
  377. pShaderAPI->SetScreenSizeForVPOS();
  378. SetupUberlightFromState( pShaderAPI, flashlightState );
  379. }
  380. #endif
  381. pShaderAPI->SetPixelShaderFogParams( PSREG_FOG_PARAMS );
  382. if ( bDrawFlashlightAdditivePass == true )
  383. {
  384. float atten[4], pos[4], tweaks[4];
  385. atten[0] = flashlightState.m_fConstantAtten; // Set the flashlight attenuation factors
  386. atten[1] = flashlightState.m_fLinearAtten;
  387. atten[2] = flashlightState.m_fQuadraticAtten;
  388. atten[3] = flashlightState.m_FarZAtten;
  389. pShaderAPI->SetPixelShaderConstant( 7, atten, 1 );
  390. pos[0] = flashlightState.m_vecLightOrigin[0]; // Set the flashlight origin
  391. pos[1] = flashlightState.m_vecLightOrigin[1];
  392. pos[2] = flashlightState.m_vecLightOrigin[2];
  393. pShaderAPI->SetPixelShaderConstant( 8, pos, 1 );
  394. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_6, worldToTexture.Base(), 4 );
  395. // Tweaks associated with a given flashlight
  396. tweaks[0] = ShadowFilterFromState( flashlightState );
  397. tweaks[1] = ShadowAttenFromState( flashlightState );
  398. pShader->HashShadow2DJitter( flashlightState.m_flShadowJitterSeed, &tweaks[2], &tweaks[3] );
  399. pShaderAPI->SetPixelShaderConstant( 9, tweaks, 1 );
  400. // Dimensions of screen, used for screen-space noise map sampling
  401. float vScreenScale[4] = {1280.0f / 32.0f, 720.0f / 32.0f, 0, 0};
  402. int nWidth, nHeight;
  403. pShaderAPI->GetBackBufferDimensions( nWidth, nHeight );
  404. int nTexWidth, nTexHeight;
  405. pShaderAPI->GetStandardTextureDimensions( &nTexWidth, &nTexHeight, TEXTURE_SHADOW_NOISE_2D );
  406. vScreenScale[0] = (float) nWidth / nTexWidth;
  407. vScreenScale[1] = (float) nHeight / nTexHeight;
  408. vScreenScale[2] = 1.0f / flashlightState.m_flShadowMapResolution;
  409. vScreenScale[3] = 2.0f / flashlightState.m_flShadowMapResolution;
  410. pShaderAPI->SetPixelShaderConstant( PSREG_FLASHLIGHT_SCREEN_SCALE, vScreenScale, 1 );
  411. vAmbientOcclusion[3] *= flashlightState.m_flAmbientOcclusion;
  412. }
  413. vAmbientOcclusion[3] = MIN( MAX( vAmbientOcclusion[3], 0.0f ), 1.0f );
  414. pShaderAPI->SetPixelShaderConstant( 5, vAmbientOcclusion, 1 );
  415. }
  416. pShader->Draw();
  417. }
  418. void Draw_Eyes_Refract( CBaseVSShader *pShader, IMaterialVar** params, IShaderDynamicAPI *pShaderAPI,
  419. IShaderShadow* pShaderShadow, Eye_Refract_Vars_t &info, VertexCompressionType_t vertexCompression )
  420. {
  421. bool bHasFlashlight = pShader->UsingFlashlight( params );
  422. if ( bHasFlashlight && ( IsX360() || IsPS3() ) )
  423. {
  424. Draw_Eyes_Refract_Internal( pShader, params, pShaderAPI, pShaderShadow, false, info, vertexCompression );
  425. if ( pShaderShadow )
  426. {
  427. pShader->SetInitialShadowState( );
  428. }
  429. }
  430. Draw_Eyes_Refract_Internal( pShader, params, pShaderAPI, pShaderShadow, bHasFlashlight, info, vertexCompression );
  431. }