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.

287 lines
9.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #include "BaseVSShader.h"
  8. #include "mathlib/vmatrix.h"
  9. #include "common_hlsl_cpp_consts.h" // hack hack hack!
  10. #include "lightmappedreflective_vs20.inc"
  11. #include "lightmappedreflective_ps20.inc"
  12. #include "lightmappedreflective_ps20b.inc"
  13. DEFINE_FALLBACK_SHADER( LightmappedReflective, LightmappedReflective_DX90 )
  14. BEGIN_VS_SHADER( LightmappedReflective_DX90, "Help for Lightmapped Reflective" )
  15. BEGIN_SHADER_PARAMS
  16. SHADER_PARAM( REFRACTTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "_rt_WaterRefraction", "" )
  17. SHADER_PARAM( REFLECTTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "_rt_WaterReflection", "" )
  18. SHADER_PARAM( REFRACTAMOUNT, SHADER_PARAM_TYPE_FLOAT, "0", "" )
  19. SHADER_PARAM( REFRACTTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "refraction tint" )
  20. SHADER_PARAM( REFLECTAMOUNT, SHADER_PARAM_TYPE_FLOAT, "0.8", "" )
  21. SHADER_PARAM( REFLECTTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "reflection tint" )
  22. SHADER_PARAM( NORMALMAP, SHADER_PARAM_TYPE_TEXTURE, "dev/water_normal", "normal map" )
  23. SHADER_PARAM( BUMPFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "frame number for $bumpmap" )
  24. SHADER_PARAM( BUMPTRANSFORM, SHADER_PARAM_TYPE_MATRIX, "center .5 .5 scale 1 1 rotate 0 translate 0 0", "$bumpmap texcoord transform" )
  25. SHADER_PARAM( ENVMAPMASK, SHADER_PARAM_TYPE_TEXTURE, "shadertest/shadertest_envmask", "envmap mask" )
  26. SHADER_PARAM( ENVMAPMASKFRAME, SHADER_PARAM_TYPE_INTEGER, "", "" )
  27. SHADER_PARAM( FRESNELPOWER, SHADER_PARAM_TYPE_FLOAT, "5", "" )
  28. SHADER_PARAM( MAXREFLECTIVITY, SHADER_PARAM_TYPE_FLOAT, "1", "" )
  29. SHADER_PARAM( MINREFLECTIVITY, SHADER_PARAM_TYPE_FLOAT, "0", "" )
  30. END_SHADER_PARAMS
  31. SHADER_INIT_PARAMS()
  32. {
  33. if ( !params[FRESNELPOWER]->IsDefined() )
  34. {
  35. params[FRESNELPOWER]->SetFloatValue( 5.0f );
  36. }
  37. if ( !params[MAXREFLECTIVITY]->IsDefined() )
  38. {
  39. params[MAXREFLECTIVITY]->SetFloatValue( 1.0f );
  40. }
  41. SET_FLAGS2( MATERIAL_VAR2_NEEDS_TANGENT_SPACES );
  42. if ( params[BASETEXTURE]->IsDefined() )
  43. {
  44. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_LIGHTMAP );
  45. if( g_pConfig->UseBumpmapping() && params[NORMALMAP]->IsDefined() )
  46. {
  47. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_BUMPED_LIGHTMAP );
  48. }
  49. }
  50. }
  51. SHADER_FALLBACK
  52. {
  53. // FIXME: Create dx8 level fallback if we use this feature out of the SFM
  54. return 0;
  55. }
  56. SHADER_INIT
  57. {
  58. if( params[REFRACTTEXTURE]->IsDefined() )
  59. {
  60. LoadTexture( REFRACTTEXTURE, g_pHardwareConfig->GetHDRType() == HDR_TYPE_INTEGER || IsOSX() ? TEXTUREFLAGS_SRGB : 0 );
  61. }
  62. if( params[REFLECTTEXTURE]->IsDefined() )
  63. {
  64. LoadTexture( REFLECTTEXTURE, g_pHardwareConfig->GetHDRType() == HDR_TYPE_INTEGER || IsOSX() ? TEXTUREFLAGS_SRGB : 0 );
  65. }
  66. if ( params[NORMALMAP]->IsDefined() )
  67. {
  68. LoadBumpMap( NORMALMAP );
  69. }
  70. if( params[BASETEXTURE]->IsDefined() )
  71. {
  72. LoadTexture( BASETEXTURE, TEXTUREFLAGS_SRGB );
  73. if( params[ENVMAPMASK]->IsDefined() )
  74. {
  75. LoadTexture( ENVMAPMASK );
  76. }
  77. }
  78. else
  79. {
  80. params[ENVMAPMASK]->SetUndefined();
  81. }
  82. }
  83. inline void DrawReflectionRefraction( IMaterialVar **params, IShaderShadow* pShaderShadow,
  84. IShaderDynamicAPI* pShaderAPI, bool bReflection, bool bRefraction )
  85. {
  86. BlendType_t nBlendType = EvaluateBlendRequirements( BASETEXTURE, true );
  87. bool bFullyOpaque = (nBlendType != BT_BLENDADD) && (nBlendType != BT_BLEND) && !IS_FLAG_SET(MATERIAL_VAR_ALPHATEST); //dest alpha is free for special use
  88. SHADOW_STATE
  89. {
  90. SetInitialShadowState( );
  91. if( bRefraction )
  92. {
  93. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true ); // Refract
  94. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, g_pHardwareConfig->GetHDRType() == HDR_TYPE_INTEGER || IsOSX() );
  95. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true ); // Base
  96. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, true );
  97. }
  98. if( bReflection )
  99. {
  100. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true ); // Reflect
  101. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER2, g_pHardwareConfig->GetHDRType() == HDR_TYPE_INTEGER || IsOSX() );
  102. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true ); // Lightmap
  103. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER3, g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE );
  104. }
  105. if( params[BASETEXTURE]->IsTexture() )
  106. {
  107. // BASETEXTURE
  108. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  109. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, true );
  110. // LIGHTMAP
  111. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  112. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER3, g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE );
  113. if ( params[ENVMAPMASK]->IsTexture() )
  114. {
  115. pShaderShadow->EnableTexture( SHADER_SAMPLER6, true );
  116. }
  117. }
  118. // normal map
  119. pShaderShadow->EnableTexture( SHADER_SAMPLER4, true );
  120. int fmt = VERTEX_POSITION | VERTEX_NORMAL | VERTEX_TANGENT_S | VERTEX_TANGENT_T;
  121. // texcoord0 : base texcoord
  122. // texcoord1 : lightmap texcoord
  123. // texcoord2 : lightmap texcoord offset
  124. int numTexCoords = 1;
  125. if( params[BASETEXTURE]->IsTexture() )
  126. {
  127. numTexCoords = 3;
  128. }
  129. pShaderShadow->VertexShaderVertexFormat( fmt, numTexCoords, 0, 0 );
  130. if ( IS_FLAG_SET(MATERIAL_VAR_TRANSLUCENT ) )
  131. {
  132. EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  133. }
  134. DECLARE_STATIC_VERTEX_SHADER( lightmappedreflective_vs20 );
  135. SET_STATIC_VERTEX_SHADER_COMBO( BASETEXTURE, params[BASETEXTURE]->IsTexture() );
  136. SET_STATIC_VERTEX_SHADER( lightmappedreflective_vs20 );
  137. // "REFLECT" "0..1"
  138. // "REFRACT" "0..1"
  139. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  140. {
  141. DECLARE_STATIC_PIXEL_SHADER( lightmappedreflective_ps20b );
  142. SET_STATIC_PIXEL_SHADER_COMBO( REFLECT, bReflection );
  143. SET_STATIC_PIXEL_SHADER_COMBO( REFRACT, bRefraction );
  144. SET_STATIC_PIXEL_SHADER_COMBO( BASETEXTURE, params[BASETEXTURE]->IsTexture() );
  145. SET_STATIC_PIXEL_SHADER_COMBO( ENVMAPMASK, params[ENVMAPMASK]->IsTexture() && params[BASETEXTURE]->IsTexture() );
  146. SET_STATIC_PIXEL_SHADER( lightmappedreflective_ps20b );
  147. }
  148. else
  149. {
  150. DECLARE_STATIC_PIXEL_SHADER( lightmappedreflective_ps20 );
  151. SET_STATIC_PIXEL_SHADER_COMBO( REFLECT, bReflection );
  152. SET_STATIC_PIXEL_SHADER_COMBO( REFRACT, bRefraction );
  153. SET_STATIC_PIXEL_SHADER_COMBO( BASETEXTURE, params[BASETEXTURE]->IsTexture() );
  154. SET_STATIC_PIXEL_SHADER_COMBO( ENVMAPMASK, params[ENVMAPMASK]->IsTexture() && params[BASETEXTURE]->IsTexture() );
  155. SET_STATIC_PIXEL_SHADER( lightmappedreflective_ps20 );
  156. }
  157. FogToFogColor();
  158. if( g_pHardwareConfig->GetHDRType() != HDR_TYPE_NONE )
  159. {
  160. // we are writing linear values from this shader.
  161. pShaderShadow->EnableSRGBWrite( true );
  162. }
  163. pShaderShadow->EnableAlphaWrites( bFullyOpaque );
  164. }
  165. DYNAMIC_STATE
  166. {
  167. if( bRefraction )
  168. {
  169. // HDRFIXME: add comment about binding.. Specify the number of MRTs in the enable
  170. BindTexture( SHADER_SAMPLER0, REFRACTTEXTURE, -1 );
  171. }
  172. if( bReflection )
  173. {
  174. BindTexture( SHADER_SAMPLER2, REFLECTTEXTURE, -1 );
  175. }
  176. BindTexture( SHADER_SAMPLER4, NORMALMAP, BUMPFRAME );
  177. if( params[BASETEXTURE]->IsTexture() )
  178. {
  179. BindTexture( SHADER_SAMPLER1, BASETEXTURE, FRAME );
  180. pShaderAPI->BindStandardTexture( SHADER_SAMPLER3, TEXTURE_LIGHTMAP );
  181. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_3, BASETEXTURETRANSFORM );
  182. if ( params[ENVMAPMASK]->IsTexture() )
  183. {
  184. BindTexture( SHADER_SAMPLER6, ENVMAPMASK, ENVMAPMASKFRAME );
  185. }
  186. }
  187. // Refraction tint
  188. if( bRefraction )
  189. {
  190. SetPixelShaderConstantGammaToLinear( 1, REFRACTTINT );
  191. }
  192. // Reflection tint
  193. if( bReflection )
  194. {
  195. SetPixelShaderConstantGammaToLinear( 4, REFLECTTINT );
  196. }
  197. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_1, BUMPTRANSFORM );
  198. float c0[4] = { 1.0f / 3.0f, 1.0f / 3.0f, 1.0f / 3.0f, 0.0f };
  199. pShaderAPI->SetPixelShaderConstant( 0, c0, 1 );
  200. float c2[4] = { 0.5f, 0.5f, 0.5f, 0.5f };
  201. pShaderAPI->SetPixelShaderConstant( 2, c2, 1 );
  202. // fresnel constants
  203. float flFresnelFactor = params[MAXREFLECTIVITY]->GetFloatValue() - params[MINREFLECTIVITY]->GetFloatValue();
  204. float c3[4] = { flFresnelFactor, params[FRESNELPOWER]->GetFloatValue(), params[MINREFLECTIVITY]->GetFloatValue(), 0.0f };
  205. pShaderAPI->SetPixelShaderConstant( 3, c3, 1 );
  206. float c5[4] = { params[REFLECTAMOUNT]->GetFloatValue(), params[REFLECTAMOUNT]->GetFloatValue(),
  207. params[REFRACTAMOUNT]->GetFloatValue(), params[REFRACTAMOUNT]->GetFloatValue() };
  208. pShaderAPI->SetPixelShaderConstant( 5, c5, 1 );
  209. pShaderAPI->SetPixelShaderFogParams( 8 );
  210. DECLARE_DYNAMIC_VERTEX_SHADER( lightmappedreflective_vs20 );
  211. SET_DYNAMIC_VERTEX_SHADER( lightmappedreflective_vs20 );
  212. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  213. {
  214. DECLARE_DYNAMIC_PIXEL_SHADER( lightmappedreflective_ps20b );
  215. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITE_DEPTH_TO_DESTALPHA, bFullyOpaque && pShaderAPI->ShouldWriteDepthToDestAlpha() );
  216. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  217. SET_DYNAMIC_PIXEL_SHADER( lightmappedreflective_ps20b );
  218. }
  219. else
  220. {
  221. DECLARE_DYNAMIC_PIXEL_SHADER( lightmappedreflective_ps20 );
  222. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  223. SET_DYNAMIC_PIXEL_SHADER( lightmappedreflective_ps20 );
  224. }
  225. }
  226. Draw();
  227. }
  228. SHADER_DRAW
  229. {
  230. bool bRefraction = params[REFRACTTEXTURE]->IsTexture();
  231. bool bReflection = params[REFLECTTEXTURE]->IsTexture();
  232. bool bDrewSomething = false;
  233. if ( bReflection || bRefraction )
  234. {
  235. bDrewSomething = true;
  236. DrawReflectionRefraction( params, pShaderShadow, pShaderAPI, bReflection, bRefraction );
  237. }
  238. if( !bDrewSomething )
  239. {
  240. // We are likely here because of the tools. . . draw something so that
  241. // we won't go into wireframe-land.
  242. Draw();
  243. }
  244. }
  245. END_SHADER