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.

298 lines
10 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "BaseVSShader.h"
  8. #include "refract_dx9_helper.h"
  9. #include "convar.h"
  10. #include "Refract_vs20.inc"
  11. #include "Refract_ps20.inc"
  12. #include "Refract_ps20b.inc"
  13. #include "cpp_shader_constant_register_map.h"
  14. #define MAXBLUR 1
  15. // FIXME: doesn't support fresnel!
  16. void InitParamsRefract_DX9( CBaseVSShader *pShader, IMaterialVar** params, const char *pMaterialName, Refract_DX9_Vars_t &info )
  17. {
  18. SET_FLAGS2( MATERIAL_VAR2_NEEDS_TANGENT_SPACES );
  19. SET_FLAGS( MATERIAL_VAR_TRANSLUCENT );
  20. if( !params[info.m_nEnvmapTint]->IsDefined() )
  21. {
  22. params[info.m_nEnvmapTint]->SetVecValue( 1.0f, 1.0f, 1.0f );
  23. }
  24. if( !params[info.m_nEnvmapContrast]->IsDefined() )
  25. {
  26. params[info.m_nEnvmapContrast]->SetFloatValue( 0.0f );
  27. }
  28. if( !params[info.m_nEnvmapSaturation]->IsDefined() )
  29. {
  30. params[info.m_nEnvmapSaturation]->SetFloatValue( 1.0f );
  31. }
  32. if( !params[info.m_nEnvmapFrame]->IsDefined() )
  33. {
  34. params[info.m_nEnvmapFrame]->SetIntValue( 0 );
  35. }
  36. if( !params[info.m_nFresnelReflection]->IsDefined() )
  37. {
  38. params[info.m_nFresnelReflection]->SetFloatValue( 1.0f );
  39. }
  40. if( !params[info.m_nMasked]->IsDefined() )
  41. {
  42. params[info.m_nMasked]->SetIntValue( 0 );
  43. }
  44. if( !params[info.m_nBlurAmount]->IsDefined() )
  45. {
  46. params[info.m_nBlurAmount]->SetIntValue( 0 );
  47. }
  48. if( !params[info.m_nFadeOutOnSilhouette]->IsDefined() )
  49. {
  50. params[info.m_nFadeOutOnSilhouette]->SetIntValue( 0 );
  51. }
  52. if( !params[info.m_nForceAlphaWrite]->IsDefined() )
  53. {
  54. params[info.m_nForceAlphaWrite]->SetIntValue( 0 );
  55. }
  56. SET_FLAGS2( MATERIAL_VAR2_NEEDS_POWER_OF_TWO_FRAME_BUFFER_TEXTURE );
  57. }
  58. void InitRefract_DX9( CBaseVSShader *pShader, IMaterialVar** params, Refract_DX9_Vars_t &info )
  59. {
  60. if (params[info.m_nBaseTexture]->IsDefined() )
  61. {
  62. pShader->LoadTexture( info.m_nBaseTexture, TEXTUREFLAGS_SRGB );
  63. }
  64. if (params[info.m_nNormalMap]->IsDefined() )
  65. {
  66. pShader->LoadBumpMap( info.m_nNormalMap );
  67. }
  68. if (params[info.m_nNormalMap2]->IsDefined() )
  69. {
  70. pShader->LoadBumpMap( info.m_nNormalMap2 );
  71. }
  72. if( params[info.m_nEnvmap]->IsDefined() )
  73. {
  74. pShader->LoadCubeMap( info.m_nEnvmap, TEXTUREFLAGS_SRGB );
  75. }
  76. if( params[info.m_nRefractTintTexture]->IsDefined() )
  77. {
  78. pShader->LoadTexture( info.m_nRefractTintTexture, TEXTUREFLAGS_SRGB );
  79. }
  80. }
  81. void DrawRefract_DX9( CBaseVSShader *pShader, IMaterialVar** params, IShaderDynamicAPI *pShaderAPI,
  82. IShaderShadow* pShaderShadow, Refract_DX9_Vars_t &info, VertexCompressionType_t vertexCompression )
  83. {
  84. bool bIsModel = IS_FLAG_SET( MATERIAL_VAR_MODEL );
  85. bool bHasEnvmap = params[info.m_nEnvmap]->IsTexture();
  86. bool bRefractTintTexture = params[info.m_nRefractTintTexture]->IsTexture();
  87. bool bFadeOutOnSilhouette = params[info.m_nFadeOutOnSilhouette]->GetIntValue() != 0;
  88. int blurAmount = params[info.m_nBlurAmount]->GetIntValue();
  89. bool bMasked = (params[info.m_nMasked]->GetIntValue() != 0);
  90. bool bSecondaryNormal = ( ( info.m_nNormalMap2 != -1 ) && ( params[info.m_nNormalMap2]->IsTexture() ) );
  91. bool bColorModulate = ( ( info.m_nVertexColorModulate != -1 ) && ( params[info.m_nVertexColorModulate]->GetIntValue() ) );
  92. bool bWriteZ = params[info.m_nNoWriteZ]->GetIntValue() == 0;
  93. if( blurAmount < 0 )
  94. {
  95. blurAmount = 0;
  96. }
  97. else if( blurAmount > MAXBLUR )
  98. {
  99. blurAmount = MAXBLUR;
  100. }
  101. BlendType_t nBlendType = pShader->EvaluateBlendRequirements( BASETEXTURE, true );
  102. bool bFullyOpaque = (nBlendType != BT_BLENDADD) && (nBlendType != BT_BLEND) && !IS_FLAG_SET(MATERIAL_VAR_ALPHATEST); //dest alpha is free for special use
  103. bFullyOpaque &= !bMasked;
  104. bool bTranslucentNormal = pShader->TextureIsTranslucent( info.m_nNormalMap, false );
  105. bFullyOpaque &= (! bTranslucentNormal );
  106. SHADOW_STATE
  107. {
  108. pShader->SetInitialShadowState( );
  109. pShaderShadow->EnableDepthWrites( bWriteZ );
  110. // Alpha test: FIXME: shouldn't this be handled in Shader_t::SetInitialShadowState
  111. pShaderShadow->EnableAlphaTest( IS_FLAG_SET(MATERIAL_VAR_ALPHATEST) );
  112. // If envmap is not specified, the alpha channel is the translucency
  113. // (If envmap *is* specified, alpha channel is the reflection amount)
  114. if ( params[info.m_nNormalMap]->IsTexture() && !bHasEnvmap )
  115. {
  116. pShader->SetDefaultBlendingShadowState( info.m_nNormalMap, false );
  117. }
  118. // source render target that contains the image that we are warping.
  119. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  120. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER2, true );
  121. // normal map
  122. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  123. if ( bSecondaryNormal )
  124. {
  125. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  126. }
  127. if( bHasEnvmap )
  128. {
  129. // envmap
  130. pShaderShadow->EnableTexture( SHADER_SAMPLER4, true );
  131. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER4, true );
  132. }
  133. if( bRefractTintTexture )
  134. {
  135. // refract tint texture
  136. pShaderShadow->EnableTexture( SHADER_SAMPLER5, true );
  137. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER5, true );
  138. }
  139. pShaderShadow->EnableSRGBWrite( true );
  140. unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL;
  141. int userDataSize = 0;
  142. int nTexCoordCount = 1;
  143. if( bIsModel )
  144. {
  145. userDataSize = 4;
  146. }
  147. else
  148. {
  149. flags |= VERTEX_TANGENT_S | VERTEX_TANGENT_T;
  150. }
  151. if ( bColorModulate )
  152. {
  153. flags |= VERTEX_COLOR;
  154. }
  155. // This shader supports compressed vertices, so OR in that flag:
  156. flags |= VERTEX_FORMAT_COMPRESSED;
  157. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, NULL, userDataSize );
  158. DECLARE_STATIC_VERTEX_SHADER( refract_vs20 );
  159. SET_STATIC_VERTEX_SHADER_COMBO( MODEL, bIsModel );
  160. SET_STATIC_VERTEX_SHADER_COMBO( COLORMODULATE, bColorModulate );
  161. SET_STATIC_VERTEX_SHADER( refract_vs20 );
  162. // We have to do this in the shader on R500 or Leopard
  163. bool bShaderSRGBConvert = IsOSX() && ( g_pHardwareConfig->FakeSRGBWrite() || !g_pHardwareConfig->CanDoSRGBReadFromRTs() );
  164. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() || g_pHardwareConfig->ShouldAlwaysUseShaderModel2bShaders() ) // always send OpenGL down the ps2b path
  165. {
  166. DECLARE_STATIC_PIXEL_SHADER( refract_ps20b );
  167. SET_STATIC_PIXEL_SHADER_COMBO( BLUR, blurAmount );
  168. SET_STATIC_PIXEL_SHADER_COMBO( FADEOUTONSILHOUETTE, bFadeOutOnSilhouette );
  169. SET_STATIC_PIXEL_SHADER_COMBO( CUBEMAP, bHasEnvmap );
  170. SET_STATIC_PIXEL_SHADER_COMBO( REFRACTTINTTEXTURE, bRefractTintTexture );
  171. SET_STATIC_PIXEL_SHADER_COMBO( MASKED, bMasked );
  172. SET_STATIC_PIXEL_SHADER_COMBO( COLORMODULATE, bColorModulate );
  173. SET_STATIC_PIXEL_SHADER_COMBO( SECONDARY_NORMAL, bSecondaryNormal );
  174. SET_STATIC_PIXEL_SHADER_COMBO( NORMAL_DECODE_MODE, (int) NORMAL_DECODE_NONE );
  175. SET_STATIC_PIXEL_SHADER_COMBO( SHADER_SRGB_READ, bShaderSRGBConvert );
  176. SET_STATIC_PIXEL_SHADER( refract_ps20b );
  177. }
  178. else
  179. {
  180. DECLARE_STATIC_PIXEL_SHADER( refract_ps20 );
  181. SET_STATIC_PIXEL_SHADER_COMBO( BLUR, blurAmount );
  182. SET_STATIC_PIXEL_SHADER_COMBO( FADEOUTONSILHOUETTE, bFadeOutOnSilhouette );
  183. SET_STATIC_PIXEL_SHADER_COMBO( CUBEMAP, bHasEnvmap );
  184. SET_STATIC_PIXEL_SHADER_COMBO( REFRACTTINTTEXTURE, bRefractTintTexture );
  185. SET_STATIC_PIXEL_SHADER_COMBO( MASKED, bMasked );
  186. SET_STATIC_PIXEL_SHADER_COMBO( COLORMODULATE, bColorModulate );
  187. SET_STATIC_PIXEL_SHADER_COMBO( SECONDARY_NORMAL, bSecondaryNormal );
  188. SET_STATIC_PIXEL_SHADER_COMBO( NORMAL_DECODE_MODE, (int) NORMAL_DECODE_NONE );
  189. SET_STATIC_PIXEL_SHADER( refract_ps20 );
  190. }
  191. pShader->DefaultFog();
  192. if( bMasked )
  193. {
  194. pShader->EnableAlphaBlending( SHADER_BLEND_ONE_MINUS_SRC_ALPHA, SHADER_BLEND_SRC_ALPHA );
  195. }
  196. bool bAlphaWrites = bFullyOpaque || ( params[ info.m_nForceAlphaWrite ]->GetIntValue() != 0 );
  197. pShaderShadow->EnableAlphaWrites( bAlphaWrites );
  198. }
  199. DYNAMIC_STATE
  200. {
  201. pShaderAPI->SetDefaultState();
  202. if ( params[info.m_nBaseTexture]->IsTexture() )
  203. {
  204. pShader->BindTexture( SHADER_SAMPLER2, info.m_nBaseTexture, info.m_nFrame );
  205. }
  206. else
  207. {
  208. pShaderAPI->BindStandardTexture( SHADER_SAMPLER2, TEXTURE_FRAME_BUFFER_FULL_TEXTURE_0 );
  209. }
  210. pShader->BindTexture( SHADER_SAMPLER3, info.m_nNormalMap, info.m_nBumpFrame );
  211. if ( bSecondaryNormal )
  212. {
  213. pShader->BindTexture( SHADER_SAMPLER1, info.m_nNormalMap2, info.m_nBumpFrame2 );
  214. }
  215. if( bHasEnvmap )
  216. {
  217. pShader->BindTexture( SHADER_SAMPLER4, info.m_nEnvmap, info.m_nEnvmapFrame );
  218. }
  219. if( bRefractTintTexture )
  220. {
  221. pShader->BindTexture( SHADER_SAMPLER5, info.m_nRefractTintTexture, info.m_nRefractTintTextureFrame );
  222. }
  223. DECLARE_DYNAMIC_VERTEX_SHADER( refract_vs20 );
  224. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  225. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  226. SET_DYNAMIC_VERTEX_SHADER( refract_vs20 );
  227. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() || g_pHardwareConfig->ShouldAlwaysUseShaderModel2bShaders() ) // always send Posix down the ps2b path
  228. {
  229. DECLARE_DYNAMIC_PIXEL_SHADER( refract_ps20b );
  230. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  231. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITE_DEPTH_TO_DESTALPHA, bWriteZ && bFullyOpaque && pShaderAPI->ShouldWriteDepthToDestAlpha() );
  232. SET_DYNAMIC_PIXEL_SHADER( refract_ps20b );
  233. }
  234. else
  235. {
  236. DECLARE_DYNAMIC_PIXEL_SHADER( refract_ps20 );
  237. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  238. SET_DYNAMIC_PIXEL_SHADER( refract_ps20 );
  239. }
  240. pShader->SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_1, info.m_nBumpTransform ); // 1 & 2
  241. pShader->SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_3, info.m_nBumpTransform2 ); // 3 & 4
  242. pShaderAPI->SetPixelShaderFogParams( PSREG_FOG_PARAMS );
  243. float vEyePos_SpecExponent[4];
  244. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos_SpecExponent );
  245. vEyePos_SpecExponent[3] = 0.0f;
  246. pShaderAPI->SetPixelShaderConstant( PSREG_EYEPOS_SPEC_EXPONENT, vEyePos_SpecExponent, 1 );
  247. pShader->SetPixelShaderConstantGammaToLinear( 0, info.m_nEnvmapTint );
  248. pShader->SetPixelShaderConstantGammaToLinear( 1, info.m_nRefractTint );
  249. pShader->SetPixelShaderConstant( 2, info.m_nEnvmapContrast );
  250. pShader->SetPixelShaderConstant( 3, info.m_nEnvmapSaturation );
  251. float c5[4] = { params[info.m_nRefractAmount]->GetFloatValue(),
  252. params[info.m_nRefractAmount]->GetFloatValue(), 0.0f, 0.0f };
  253. // Time % 1000
  254. c5[3] = pShaderAPI->CurrentTime();
  255. c5[3] -= (float)( (int)( c5[3] / 1000.0f ) ) * 1000.0f;
  256. pShaderAPI->SetPixelShaderConstant( 5, c5, 1 );
  257. float cVs3[4] = { c5[3], 0.0f, 0.0f, 0.0f };
  258. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_5, cVs3, 1 );
  259. }
  260. pShader->Draw();
  261. }