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.

317 lines
9.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "BaseVSShader.h"
  8. #include "refract_model_vs11.inc"
  9. #include "refract_world_vs11.inc"
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include "tier0/memdbgon.h"
  12. #define MAXBLUR 1
  13. DEFINE_FALLBACK_SHADER( Refract, Refract_DX80 )
  14. BEGIN_VS_SHADER( Refract_DX80,
  15. "Help for Refract_DX80" )
  16. BEGIN_SHADER_PARAMS
  17. SHADER_PARAM_OVERRIDE( COLOR, SHADER_PARAM_TYPE_COLOR, "{255 255 255}", "unused", SHADER_PARAM_NOT_EDITABLE )
  18. SHADER_PARAM_OVERRIDE( ALPHA, SHADER_PARAM_TYPE_FLOAT, "1.0", "unused", SHADER_PARAM_NOT_EDITABLE )
  19. SHADER_PARAM( REFRACTAMOUNT, SHADER_PARAM_TYPE_FLOAT, "2", "" )
  20. SHADER_PARAM( REFRACTTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "refraction tint" )
  21. SHADER_PARAM( DUDVMAP, SHADER_PARAM_TYPE_TEXTURE, "models/shadertest/shader1_dudv", "dudv bump map" )
  22. SHADER_PARAM( NORMALMAP, SHADER_PARAM_TYPE_TEXTURE, "models/shadertest/shader1_normal", "normal map" )
  23. SHADER_PARAM( BUMPFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "frame number for $bumpmap" )
  24. SHADER_PARAM( DUDVFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "frame number for $dudvmap" )
  25. SHADER_PARAM( BUMPTRANSFORM, SHADER_PARAM_TYPE_MATRIX, "center .5 .5 scale 1 1 rotate 0 translate 0 0", "$bumpmap texcoord transform" )
  26. SHADER_PARAM( TIME, SHADER_PARAM_TYPE_FLOAT, "0.0f", "" )
  27. SHADER_PARAM( BLURAMOUNT, SHADER_PARAM_TYPE_INTEGER, "1", "0, 1, or 2 for how much blur you want" )
  28. SHADER_PARAM( FADEOUTONSILHOUETTE, SHADER_PARAM_TYPE_BOOL, "1", "0 for no fade out on silhouette, 1 for fade out on sillhouette" )
  29. SHADER_PARAM( ENVMAP, SHADER_PARAM_TYPE_TEXTURE, "shadertest/shadertest_env", "envmap" )
  30. SHADER_PARAM( ENVMAPFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "envmap frame number" )
  31. SHADER_PARAM( ENVMAPTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "envmap tint" )
  32. SHADER_PARAM( ENVMAPCONTRAST, SHADER_PARAM_TYPE_FLOAT, "0.0", "contrast 0 == normal 1 == color*color" )
  33. SHADER_PARAM( ENVMAPSATURATION, SHADER_PARAM_TYPE_FLOAT, "1.0", "saturation 0 == greyscale 1 == normal" )
  34. SHADER_PARAM( REFRACTTINTTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "models/shadertest/shield", "" )
  35. SHADER_PARAM( REFRACTTINTTEXTUREFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  36. SHADER_PARAM( FRESNELREFLECTION, SHADER_PARAM_TYPE_FLOAT, "1.0", "1.0 == mirror, 0.0 == water" )
  37. SHADER_PARAM( FALLBACK, SHADER_PARAM_TYPE_STRING, "", "Name of the fallback shader" )
  38. SHADER_PARAM( FORCEREFRACT, SHADER_PARAM_TYPE_BOOL, "0", "Forces refraction on boards that have poor performance" )
  39. SHADER_PARAM( NOWRITEZ, SHADER_PARAM_TYPE_INTEGER, "0", "0 == write z, 1 = no write z" )
  40. SHADER_PARAM( MASKED, SHADER_PARAM_TYPE_BOOL, "0", "mask using dest alpha" )
  41. END_SHADER_PARAMS
  42. SHADER_INIT_PARAMS()
  43. {
  44. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  45. SET_FLAGS2( MATERIAL_VAR2_NEEDS_TANGENT_SPACES );
  46. SET_FLAGS( MATERIAL_VAR_TRANSLUCENT );
  47. if( !params[ENVMAPTINT]->IsDefined() )
  48. {
  49. params[ENVMAPTINT]->SetVecValue( 1.0f, 1.0f, 1.0f );
  50. }
  51. if( !params[ENVMAPCONTRAST]->IsDefined() )
  52. {
  53. params[ENVMAPCONTRAST]->SetFloatValue( 0.0f );
  54. }
  55. if( !params[ENVMAPSATURATION]->IsDefined() )
  56. {
  57. params[ENVMAPSATURATION]->SetFloatValue( 1.0f );
  58. }
  59. if( !params[ENVMAPFRAME]->IsDefined() )
  60. {
  61. params[ENVMAPFRAME]->SetIntValue( 0 );
  62. }
  63. if( !params[FRESNELREFLECTION]->IsDefined() )
  64. {
  65. params[FRESNELREFLECTION]->SetFloatValue( 1.0f );
  66. }
  67. if( !params[MASKED]->IsDefined() )
  68. {
  69. params[MASKED]->SetIntValue( 0 );
  70. }
  71. if( !params[BLURAMOUNT]->IsDefined() )
  72. {
  73. params[BLURAMOUNT]->SetIntValue( 0 );
  74. }
  75. if( !params[FADEOUTONSILHOUETTE]->IsDefined() )
  76. {
  77. params[FADEOUTONSILHOUETTE]->SetIntValue( 0 );
  78. }
  79. SET_FLAGS2( MATERIAL_VAR2_NEEDS_POWER_OF_TWO_FRAME_BUFFER_TEXTURE );
  80. }
  81. SHADER_FALLBACK
  82. {
  83. if ( IsPC() )
  84. {
  85. const char *pFallback = (params && params[FALLBACK]->IsDefined()) ? params[FALLBACK]->GetStringValue() : "";
  86. if (!pFallback[0])
  87. {
  88. pFallback = "Refract_DX60";
  89. }
  90. if( g_pHardwareConfig->GetDXSupportLevel() < 80 || !g_pHardwareConfig->HasProjectedBumpEnv() )
  91. return pFallback;
  92. if ( g_pHardwareConfig->PreferReducedFillrate() && (params && (params[FORCEREFRACT]->GetIntValue() == 0)) )
  93. return pFallback;
  94. }
  95. return 0;
  96. }
  97. SHADER_INIT
  98. {
  99. if (params[BASETEXTURE]->IsDefined() )
  100. {
  101. LoadTexture( BASETEXTURE );
  102. }
  103. if (params[DUDVMAP]->IsDefined() )
  104. {
  105. LoadTexture( DUDVMAP );
  106. }
  107. if (params[NORMALMAP]->IsDefined() )
  108. {
  109. LoadBumpMap( NORMALMAP );
  110. }
  111. if( params[ENVMAP]->IsDefined() )
  112. {
  113. LoadCubeMap( ENVMAP );
  114. }
  115. if( params[REFRACTTINTTEXTURE]->IsDefined() )
  116. {
  117. LoadTexture( REFRACTTINTTEXTURE );
  118. }
  119. }
  120. inline int ComputePixelShaderIndex( bool bRefractTintTexture, bool bNormalMapAlpha )
  121. {
  122. // "REFRACTTINTTEXTURE" "0..1"
  123. // "NORMALMAPALPHA" "0..1"
  124. int pshIndex = 0;
  125. if( bRefractTintTexture ) pshIndex |= 0x1;
  126. if( bNormalMapAlpha ) pshIndex |= 0x2;
  127. return pshIndex;
  128. }
  129. SHADER_DRAW
  130. {
  131. bool bIsModel = IS_FLAG_SET( MATERIAL_VAR_MODEL );
  132. bool bHasEnvmap = params[ENVMAP]->IsTexture();
  133. int blurAmount = params[BLURAMOUNT]->GetIntValue();
  134. bool bRefractTintTexture = params[REFRACTTINTTEXTURE]->IsTexture();
  135. if( blurAmount < 0 )
  136. {
  137. blurAmount = 0;
  138. }
  139. else if( blurAmount > MAXBLUR )
  140. {
  141. blurAmount = MAXBLUR;
  142. }
  143. bool bMasked = (params[MASKED]->GetIntValue() != 0);
  144. SHADOW_STATE
  145. {
  146. if ( params[NOWRITEZ]->GetIntValue() != 0 )
  147. {
  148. pShaderShadow->EnableDepthWrites( false );
  149. }
  150. // Alpha test: FIXME: shouldn't this be handled in Shader_t::SetInitialShadowState
  151. pShaderShadow->EnableAlphaTest( IS_FLAG_SET(MATERIAL_VAR_ALPHATEST) );
  152. // If envmap is not specified, the alpha channel is the translucency
  153. // (If envmap *is* specified, alpha channel is the reflection amount)
  154. bool bNormalMapAlpha = false;
  155. if ( params[NORMALMAP]->IsTexture() && !bHasEnvmap )
  156. {
  157. SetDefaultBlendingShadowState( NORMALMAP, false );
  158. if ( !bMasked && TextureIsTranslucent( NORMALMAP, false ) )
  159. {
  160. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  161. bNormalMapAlpha = true;
  162. }
  163. }
  164. // dudv map
  165. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  166. // renderable texture for refraction
  167. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  168. if( bRefractTintTexture )
  169. {
  170. // refract tint texture
  171. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  172. }
  173. int fmt = VERTEX_POSITION | VERTEX_NORMAL;
  174. int userDataSize = 0;
  175. if( bIsModel )
  176. {
  177. userDataSize = 4;
  178. }
  179. else
  180. {
  181. fmt |= VERTEX_TANGENT_S | VERTEX_TANGENT_T;
  182. }
  183. pShaderShadow->VertexShaderVertexFormat( fmt, 1, 0, userDataSize );
  184. if( bIsModel )
  185. {
  186. refract_model_vs11_Static_Index vshIndex;
  187. pShaderShadow->SetVertexShader( "Refract_model_vs11", vshIndex.GetIndex() );
  188. }
  189. else
  190. {
  191. refract_world_vs11_Static_Index vshIndex;
  192. pShaderShadow->SetVertexShader( "Refract_world_vs11", vshIndex.GetIndex() );
  193. }
  194. int pshIndex;
  195. pshIndex = ComputePixelShaderIndex( bRefractTintTexture, bNormalMapAlpha );
  196. pShaderShadow->SetPixelShader( "Refract_ps11", pshIndex );
  197. if( bMasked )
  198. {
  199. EnableAlphaBlending( SHADER_BLEND_ONE_MINUS_SRC_ALPHA, SHADER_BLEND_SRC_ALPHA );
  200. }
  201. DefaultFog();
  202. }
  203. DYNAMIC_STATE
  204. {
  205. pShaderAPI->SetDefaultState();
  206. // The dx9.0c runtime says that we shouldn't have a non-zero dimension when using vertex and pixel shaders.
  207. pShaderAPI->SetTextureTransformDimension( SHADER_TEXTURE_STAGE1, 0, true );
  208. if ( params[DUDVFRAME]->GetIntValue() == 0 )
  209. {
  210. BindTexture( SHADER_SAMPLER0, DUDVMAP, BUMPFRAME );
  211. }
  212. else
  213. {
  214. BindTexture( SHADER_SAMPLER0, DUDVMAP, DUDVFRAME );
  215. }
  216. if ( params[BASETEXTURE]->IsTexture() )
  217. {
  218. BindTexture( SHADER_SAMPLER1, BASETEXTURE, FRAME );
  219. }
  220. else
  221. {
  222. pShaderAPI->BindStandardTexture( SHADER_SAMPLER1, TEXTURE_FRAME_BUFFER_FULL_TEXTURE_0 );
  223. }
  224. if( bRefractTintTexture )
  225. {
  226. BindTexture( SHADER_SAMPLER2, REFRACTTINTTEXTURE, REFRACTTINTTEXTUREFRAME );
  227. }
  228. if ( params[NORMALMAP]->IsTexture() )
  229. {
  230. BindTexture( SHADER_SAMPLER3, NORMALMAP, BUMPFRAME );
  231. }
  232. float fRefractionAmount = params[REFRACTAMOUNT]->GetFloatValue();
  233. pShaderAPI->SetBumpEnvMatrix( SHADER_TEXTURE_STAGE1, fRefractionAmount, 0.0f, 0.0f, fRefractionAmount );
  234. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_1, BUMPTRANSFORM );
  235. // used to invert y
  236. // xboxfixme - move this into defined constants
  237. float c[4] = { 0.0f, 0.0f, 0.0f, -1.0f };
  238. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_4, c, 1 );
  239. SetPixelShaderConstant( 0, REFRACTTINT );
  240. if( bIsModel )
  241. {
  242. refract_model_vs11_Dynamic_Index vshIndex;
  243. vshIndex.SetDOWATERFOG( pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  244. vshIndex.SetSKINNING( pShaderAPI->GetCurrentNumBones() > 0 );
  245. pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  246. }
  247. else
  248. {
  249. refract_world_vs11_Dynamic_Index vshIndex;
  250. vshIndex.SetDOWATERFOG( pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  251. pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  252. }
  253. }
  254. Draw();
  255. if( bHasEnvmap )
  256. {
  257. bool bNoWriteZ = (params[NOWRITEZ]->GetIntValue() != 0);
  258. const bool bBlendSpecular = true;
  259. if( bIsModel )
  260. {
  261. DrawModelBumpedSpecularLighting( NORMALMAP, BUMPFRAME,
  262. ENVMAP, ENVMAPFRAME,
  263. ENVMAPTINT, ALPHA,
  264. ENVMAPCONTRAST, ENVMAPSATURATION,
  265. BUMPTRANSFORM,
  266. bBlendSpecular, bNoWriteZ );
  267. }
  268. else
  269. {
  270. DrawWorldBumpedSpecularLighting( NORMALMAP, ENVMAP,
  271. BUMPFRAME, ENVMAPFRAME,
  272. ENVMAPTINT, ALPHA,
  273. ENVMAPCONTRAST, ENVMAPSATURATION,
  274. BUMPTRANSFORM, FRESNELREFLECTION,
  275. bBlendSpecular, bNoWriteZ );
  276. }
  277. }
  278. }
  279. END_SHADER