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.

249 lines
8.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "BaseVSShader.h"
  8. #include "core_vs11.inc"
  9. // memdbgon must be the last include file in a .cpp file!!!
  10. #include "tier0/memdbgon.h"
  11. #define MAXBLUR 1
  12. DEFINE_FALLBACK_SHADER( Core, Core_DX80 )
  13. BEGIN_VS_SHADER( Core_DX80,
  14. "Help for Core_DX80" )
  15. BEGIN_SHADER_PARAMS
  16. SHADER_PARAM_OVERRIDE( COLOR, SHADER_PARAM_TYPE_COLOR, "{255 255 255}", "unused", SHADER_PARAM_NOT_EDITABLE )
  17. SHADER_PARAM_OVERRIDE( ALPHA, SHADER_PARAM_TYPE_FLOAT, "1.0", "unused", SHADER_PARAM_NOT_EDITABLE )
  18. SHADER_PARAM( REFRACTAMOUNT, SHADER_PARAM_TYPE_FLOAT, "2", "" )
  19. SHADER_PARAM( REFRACTTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "refraction tint" )
  20. SHADER_PARAM( DUDVMAP, SHADER_PARAM_TYPE_TEXTURE, "models/shadertest/shader1_dudv", "dudv bump map" )
  21. SHADER_PARAM( NORMALMAP, SHADER_PARAM_TYPE_TEXTURE, "models/shadertest/shader1_normal", "normal map" )
  22. SHADER_PARAM( BUMPFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "frame number for $bumpmap" )
  23. SHADER_PARAM( DUDVFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "frame number for $dudvmap" )
  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( TIME, SHADER_PARAM_TYPE_FLOAT, "0.0f", "" )
  26. SHADER_PARAM( BLURAMOUNT, SHADER_PARAM_TYPE_INTEGER, "1", "0, 1, or 2 for how much blur you want" )
  27. SHADER_PARAM( FADEOUTONSILHOUETTE, SHADER_PARAM_TYPE_BOOL, "1", "0 for no fade out on silhouette, 1 for fade out on sillhouette" )
  28. SHADER_PARAM( ENVMAP, SHADER_PARAM_TYPE_TEXTURE, "shadertest/shadertest_env", "envmap" )
  29. SHADER_PARAM( ENVMAPFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "envmap frame number" )
  30. SHADER_PARAM( ENVMAPTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "envmap tint" )
  31. SHADER_PARAM( ENVMAPCONTRAST, SHADER_PARAM_TYPE_FLOAT, "0.0", "contrast 0 == normal 1 == color*color" )
  32. SHADER_PARAM( ENVMAPSATURATION, SHADER_PARAM_TYPE_FLOAT, "1.0", "saturation 0 == greyscale 1 == normal" )
  33. SHADER_PARAM( REFRACTTINTTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "models/shadertest/shield", "" )
  34. SHADER_PARAM( REFRACTTINTTEXTUREFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  35. SHADER_PARAM( FRESNELREFLECTION, SHADER_PARAM_TYPE_FLOAT, "1.0", "1.0 == mirror, 0.0 == water" )
  36. SHADER_PARAM( FALLBACK, SHADER_PARAM_TYPE_STRING, "", "Name of the fallback shader" )
  37. SHADER_PARAM( FORCEREFRACT, SHADER_PARAM_TYPE_BOOL, "0", "Forces refraction on boards that have poor performance" )
  38. SHADER_PARAM( NOWRITEZ, SHADER_PARAM_TYPE_INTEGER, "0", "0 == write z, 1 = no write z" )
  39. SHADER_PARAM( MASKED, SHADER_PARAM_TYPE_BOOL, "0", "mask using dest alpha" )
  40. SHADER_PARAM( CORECOLORTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "" );
  41. SHADER_PARAM( CORECOLORTEXTUREFRAME, SHADER_PARAM_TYPE_INTEGER, "", "" );
  42. SHADER_PARAM( FLOWMAPTEXCOORDOFFSET, SHADER_PARAM_TYPE_FLOAT, "0.0", "" );
  43. END_SHADER_PARAMS
  44. SHADER_INIT_PARAMS()
  45. {
  46. SET_FLAGS2( MATERIAL_VAR2_NEEDS_TANGENT_SPACES );
  47. SET_FLAGS( MATERIAL_VAR_TRANSLUCENT );
  48. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  49. if( !params[ENVMAPTINT]->IsDefined() )
  50. {
  51. params[ENVMAPTINT]->SetVecValue( 1.0f, 1.0f, 1.0f );
  52. }
  53. if( !params[ENVMAPCONTRAST]->IsDefined() )
  54. {
  55. params[ENVMAPCONTRAST]->SetFloatValue( 0.0f );
  56. }
  57. if( !params[ENVMAPSATURATION]->IsDefined() )
  58. {
  59. params[ENVMAPSATURATION]->SetFloatValue( 1.0f );
  60. }
  61. if( !params[ENVMAPFRAME]->IsDefined() )
  62. {
  63. params[ENVMAPFRAME]->SetIntValue( 0 );
  64. }
  65. if( !params[FRESNELREFLECTION]->IsDefined() )
  66. {
  67. params[FRESNELREFLECTION]->SetFloatValue( 1.0f );
  68. }
  69. if( !params[MASKED]->IsDefined() )
  70. {
  71. params[MASKED]->SetIntValue( 0 );
  72. }
  73. if( !params[BLURAMOUNT]->IsDefined() )
  74. {
  75. params[BLURAMOUNT]->SetIntValue( 0 );
  76. }
  77. if( !params[FADEOUTONSILHOUETTE]->IsDefined() )
  78. {
  79. params[FADEOUTONSILHOUETTE]->SetIntValue( 0 );
  80. }
  81. if( !params[BASETEXTURE]->IsDefined() )
  82. {
  83. SET_FLAGS2( MATERIAL_VAR2_NEEDS_POWER_OF_TWO_FRAME_BUFFER_TEXTURE );
  84. }
  85. }
  86. SHADER_FALLBACK
  87. {
  88. if( g_pHardwareConfig->GetDXSupportLevel() < 80 || !g_pHardwareConfig->HasProjectedBumpEnv() )
  89. return "Core_DX70";
  90. return NULL;
  91. }
  92. SHADER_INIT
  93. {
  94. if (params[BASETEXTURE]->IsDefined() )
  95. {
  96. LoadTexture( BASETEXTURE );
  97. }
  98. if (params[DUDVMAP]->IsDefined() )
  99. {
  100. LoadTexture( DUDVMAP );
  101. }
  102. if (params[NORMALMAP]->IsDefined() )
  103. {
  104. LoadBumpMap( NORMALMAP );
  105. }
  106. if( params[ENVMAP]->IsDefined() )
  107. {
  108. LoadCubeMap( ENVMAP );
  109. }
  110. if( params[CORECOLORTEXTURE]->IsDefined() )
  111. {
  112. LoadTexture( CORECOLORTEXTURE );
  113. }
  114. }
  115. SHADER_DRAW
  116. {
  117. bool bHasEnvmap = params[ENVMAP]->IsTexture();
  118. int blurAmount = params[BLURAMOUNT]->GetIntValue();
  119. if( blurAmount < 0 )
  120. {
  121. blurAmount = 0;
  122. }
  123. else if( blurAmount > MAXBLUR )
  124. {
  125. blurAmount = MAXBLUR;
  126. }
  127. bool bMasked = params[MASKED]->GetIntValue()!= 0;
  128. SHADOW_STATE
  129. {
  130. if ( params[NOWRITEZ]->GetIntValue() != 0 )
  131. {
  132. pShaderShadow->EnableDepthWrites( false );
  133. }
  134. // Alpha test: FIXME: shouldn't this be handled in Shader_t::SetInitialShadowState
  135. pShaderShadow->EnableAlphaTest( IS_FLAG_SET(MATERIAL_VAR_ALPHATEST) );
  136. // If envmap is not specified, the alpha channel is the translucency
  137. // (If envmap *is* specified, alpha channel is the reflection amount)
  138. bool bNormalMapAlpha = false;
  139. if ( params[NORMALMAP]->IsTexture() && !bHasEnvmap )
  140. {
  141. SetDefaultBlendingShadowState( NORMALMAP, false );
  142. if ( !bMasked && TextureIsTranslucent( NORMALMAP, false ) )
  143. {
  144. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  145. bNormalMapAlpha = true;
  146. }
  147. }
  148. // dudv map
  149. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  150. // renderable texture for refraction
  151. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  152. // core color texture
  153. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  154. int fmt = VERTEX_POSITION | VERTEX_NORMAL;
  155. int userDataSize = 0;
  156. userDataSize = 4;
  157. pShaderShadow->VertexShaderVertexFormat( fmt, 1, 0, userDataSize );
  158. DECLARE_STATIC_VERTEX_SHADER( core_vs11 );
  159. SET_STATIC_VERTEX_SHADER( core_vs11 );
  160. int pshIndex = 0;
  161. pShaderShadow->SetPixelShader( "Core_ps11", pshIndex );
  162. if( bMasked )
  163. {
  164. EnableAlphaBlending( SHADER_BLEND_ONE_MINUS_SRC_ALPHA, SHADER_BLEND_SRC_ALPHA );
  165. }
  166. DefaultFog();
  167. }
  168. DYNAMIC_STATE
  169. {
  170. pShaderAPI->SetDefaultState();
  171. // The dx9.0c runtime says that we shouldn't have a non-zero dimension when using vertex and pixel shaders.
  172. pShaderAPI->SetTextureTransformDimension( SHADER_TEXTURE_STAGE1, 0, true );
  173. if ( params[DUDVFRAME]->GetIntValue() == 0 )
  174. {
  175. BindTexture( SHADER_SAMPLER0, DUDVMAP, BUMPFRAME );
  176. }
  177. else
  178. {
  179. BindTexture( SHADER_SAMPLER0, DUDVMAP, DUDVFRAME );
  180. }
  181. if ( params[BASETEXTURE]->IsTexture() )
  182. {
  183. BindTexture( SHADER_SAMPLER1, BASETEXTURE, FRAME );
  184. }
  185. else
  186. {
  187. pShaderAPI->BindStandardTexture( SHADER_SAMPLER1, TEXTURE_FRAME_BUFFER_FULL_TEXTURE_0 );
  188. }
  189. BindTexture( SHADER_SAMPLER2, CORECOLORTEXTURE, CORECOLORTEXTUREFRAME );
  190. if ( params[NORMALMAP]->IsTexture() )
  191. {
  192. BindTexture( SHADER_SAMPLER3, NORMALMAP, BUMPFRAME );
  193. }
  194. float fRefractionAmount = params[REFRACTAMOUNT]->GetFloatValue();
  195. pShaderAPI->SetBumpEnvMatrix( SHADER_TEXTURE_STAGE1, fRefractionAmount, 0.0f, 0.0f, fRefractionAmount );
  196. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_1, BUMPTRANSFORM );
  197. SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_3, FLOWMAPTEXCOORDOFFSET );
  198. DECLARE_DYNAMIC_VERTEX_SHADER( core_vs11 );
  199. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  200. SET_DYNAMIC_VERTEX_SHADER( core_vs11 );
  201. }
  202. Draw();
  203. if( bHasEnvmap )
  204. {
  205. bool bNoWriteZ = (params[NOWRITEZ]->GetIntValue() != 0);
  206. const bool bBlendSpecular = true;
  207. DrawModelBumpedSpecularLighting( NORMALMAP, BUMPFRAME,
  208. ENVMAP, ENVMAPFRAME,
  209. ENVMAPTINT, ALPHA,
  210. ENVMAPCONTRAST, ENVMAPSATURATION,
  211. BUMPTRANSFORM,
  212. bBlendSpecular, bNoWriteZ );
  213. }
  214. }
  215. END_SHADER