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
9.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "BaseVSShader.h"
  8. #include "core_vs20.inc"
  9. #include "core_ps20.inc"
  10. #include "core_ps20b.inc"
  11. #define MAXBLUR 1
  12. DEFINE_FALLBACK_SHADER( Core, Core_DX90 )
  13. BEGIN_VS_SHADER( Core_DX90,
  14. "Help for Core" )
  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( NORMALMAP, SHADER_PARAM_TYPE_TEXTURE, "models/shadertest/shader1_normal", "normal map" )
  21. SHADER_PARAM( BUMPFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "frame number for $bumpmap" )
  22. SHADER_PARAM( BUMPTRANSFORM, SHADER_PARAM_TYPE_MATRIX, "center .5 .5 scale 1 1 rotate 0 translate 0 0", "$bumpmap texcoord transform" )
  23. SHADER_PARAM( TIME, SHADER_PARAM_TYPE_FLOAT, "0.0f", "" )
  24. SHADER_PARAM( ENVMAP, SHADER_PARAM_TYPE_TEXTURE, "shadertest/shadertest_env", "envmap" )
  25. SHADER_PARAM( ENVMAPFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "envmap frame number" )
  26. SHADER_PARAM( ENVMAPTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "envmap tint" )
  27. SHADER_PARAM( ENVMAPCONTRAST, SHADER_PARAM_TYPE_FLOAT, "0.0", "contrast 0 == normal 1 == color*color" )
  28. SHADER_PARAM( ENVMAPSATURATION, SHADER_PARAM_TYPE_FLOAT, "1.0", "saturation 0 == greyscale 1 == normal" )
  29. SHADER_PARAM( FLOWMAP, SHADER_PARAM_TYPE_TEXTURE, "", "flowmap" )
  30. SHADER_PARAM( FLOWMAPFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "frame number for $flowmap" )
  31. SHADER_PARAM( FLOWMAPSCROLLRATE, SHADER_PARAM_TYPE_VEC2, "[0 0", "2D rate to scroll $flowmap" )
  32. SHADER_PARAM( CORECOLORTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "" );
  33. SHADER_PARAM( CORECOLORTEXTUREFRAME, SHADER_PARAM_TYPE_INTEGER, "", "" );
  34. SHADER_PARAM( FLOWMAPTEXCOORDOFFSET, SHADER_PARAM_TYPE_FLOAT, "0.0", "" );
  35. END_SHADER_PARAMS
  36. SHADER_INIT_PARAMS()
  37. {
  38. SET_FLAGS2( MATERIAL_VAR2_NEEDS_TANGENT_SPACES );
  39. SET_FLAGS( MATERIAL_VAR_TRANSLUCENT );
  40. if( !params[ENVMAPTINT]->IsDefined() )
  41. {
  42. params[ENVMAPTINT]->SetVecValue( 1.0f, 1.0f, 1.0f );
  43. }
  44. if( !params[ENVMAPCONTRAST]->IsDefined() )
  45. {
  46. params[ENVMAPCONTRAST]->SetFloatValue( 0.0f );
  47. }
  48. if( !params[ENVMAPSATURATION]->IsDefined() )
  49. {
  50. params[ENVMAPSATURATION]->SetFloatValue( 1.0f );
  51. }
  52. if( !params[ENVMAPFRAME]->IsDefined() )
  53. {
  54. params[ENVMAPFRAME]->SetIntValue( 0 );
  55. }
  56. if( !params[BASETEXTURE]->IsDefined() )
  57. {
  58. SET_FLAGS2( MATERIAL_VAR2_NEEDS_POWER_OF_TWO_FRAME_BUFFER_TEXTURE );
  59. }
  60. }
  61. SHADER_FALLBACK
  62. {
  63. if( g_pHardwareConfig->GetDXSupportLevel() < 90 )
  64. return "Core_dx80";
  65. return 0;
  66. }
  67. SHADER_INIT
  68. {
  69. if (params[BASETEXTURE]->IsDefined() )
  70. {
  71. LoadTexture( BASETEXTURE, TEXTUREFLAGS_SRGB );
  72. }
  73. if (params[NORMALMAP]->IsDefined() )
  74. {
  75. LoadBumpMap( NORMALMAP );
  76. }
  77. if ( params[ENVMAP]->IsDefined() )
  78. {
  79. LoadCubeMap( ENVMAP, TEXTUREFLAGS_SRGB );
  80. }
  81. if ( params[FLOWMAP]->IsDefined() )
  82. {
  83. LoadTexture( FLOWMAP );
  84. }
  85. if ( params[CORECOLORTEXTURE]->IsDefined() )
  86. {
  87. LoadTexture( CORECOLORTEXTURE );
  88. }
  89. }
  90. inline void DrawPass( IMaterialVar **params, IShaderShadow* pShaderShadow,
  91. IShaderDynamicAPI* pShaderAPI, int nPass, VertexCompressionType_t vertexCompression )
  92. {
  93. bool bIsModel = IS_FLAG_SET( MATERIAL_VAR_MODEL );
  94. bool bHasEnvmap = params[ENVMAP]->IsTexture();
  95. bool bHasFlowmap = params[FLOWMAP]->IsTexture();
  96. bool bHasCoreColorTexture = params[CORECOLORTEXTURE]->IsTexture();
  97. SHADOW_STATE
  98. {
  99. SetInitialShadowState( );
  100. if( nPass == 0 )
  101. {
  102. // Alpha test: FIXME: shouldn't this be handled in Shader_t::SetInitialShadowState
  103. pShaderShadow->EnableAlphaTest( IS_FLAG_SET(MATERIAL_VAR_ALPHATEST) );
  104. }
  105. else
  106. {
  107. pShaderShadow->DepthFunc( SHADER_DEPTHFUNC_EQUAL );
  108. EnableAlphaBlending( SHADER_BLEND_ONE, SHADER_BLEND_ONE );
  109. }
  110. // If envmap is not specified, the alpha channel is the translucency
  111. // (If envmap *is* specified, alpha channel is the reflection amount)
  112. if ( params[NORMALMAP]->IsTexture() && !bHasEnvmap )
  113. {
  114. SetDefaultBlendingShadowState( NORMALMAP, false );
  115. }
  116. // source render target that contains the image that we are warping.
  117. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  118. if( g_pHardwareConfig->GetHDRType() == HDR_TYPE_INTEGER )
  119. {
  120. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER2, true );
  121. }
  122. // normal map
  123. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  124. if( bHasEnvmap )
  125. {
  126. // envmap
  127. pShaderShadow->EnableTexture( SHADER_SAMPLER4, true );
  128. if( g_pHardwareConfig->GetHDRType() == HDR_TYPE_INTEGER )
  129. {
  130. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER4, true );
  131. }
  132. }
  133. if( bHasFlowmap )
  134. {
  135. pShaderShadow->EnableTexture( SHADER_SAMPLER6, true );
  136. }
  137. if( bHasCoreColorTexture )
  138. {
  139. pShaderShadow->EnableTexture( SHADER_SAMPLER7, true );
  140. }
  141. if( g_pHardwareConfig->GetHDRType() != HDR_TYPE_NONE )
  142. {
  143. pShaderShadow->EnableSRGBWrite( true );
  144. }
  145. unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL;
  146. int userDataSize = 0;
  147. int nTexCoordCount = 1;
  148. if( bIsModel )
  149. {
  150. userDataSize = 4;
  151. }
  152. else
  153. {
  154. flags |= VERTEX_TANGENT_S | VERTEX_TANGENT_T;
  155. }
  156. // This shader supports compressed vertices, so OR in that flag:
  157. flags |= VERTEX_FORMAT_COMPRESSED;
  158. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, NULL, userDataSize );
  159. DECLARE_STATIC_VERTEX_SHADER( core_vs20 );
  160. SET_STATIC_VERTEX_SHADER_COMBO( MODEL, bIsModel );
  161. SET_STATIC_VERTEX_SHADER( core_vs20 );
  162. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  163. {
  164. DECLARE_STATIC_PIXEL_SHADER( core_ps20b );
  165. SET_STATIC_PIXEL_SHADER_COMBO( CUBEMAP, bHasEnvmap && ( nPass == 1 ) );
  166. SET_STATIC_PIXEL_SHADER_COMBO( FLOWMAP, bHasFlowmap );
  167. SET_STATIC_PIXEL_SHADER_COMBO( CORECOLORTEXTURE, bHasCoreColorTexture && ( nPass == 0 ) );
  168. SET_STATIC_PIXEL_SHADER_COMBO( REFRACT, nPass == 0 );
  169. SET_STATIC_PIXEL_SHADER( core_ps20b );
  170. }
  171. else
  172. {
  173. DECLARE_STATIC_PIXEL_SHADER( core_ps20 );
  174. SET_STATIC_PIXEL_SHADER_COMBO( CUBEMAP, bHasEnvmap && ( nPass == 1 ) );
  175. SET_STATIC_PIXEL_SHADER_COMBO( FLOWMAP, bHasFlowmap );
  176. SET_STATIC_PIXEL_SHADER_COMBO( CORECOLORTEXTURE, bHasCoreColorTexture && ( nPass == 0 ) );
  177. SET_STATIC_PIXEL_SHADER_COMBO( REFRACT, nPass == 0 );
  178. SET_STATIC_PIXEL_SHADER( core_ps20 );
  179. }
  180. DefaultFog();
  181. }
  182. DYNAMIC_STATE
  183. {
  184. pShaderAPI->SetDefaultState();
  185. if ( params[BASETEXTURE]->IsTexture() )
  186. {
  187. BindTexture( SHADER_SAMPLER2, BASETEXTURE, FRAME );
  188. }
  189. else
  190. {
  191. pShaderAPI->BindStandardTexture( SHADER_SAMPLER2, TEXTURE_FRAME_BUFFER_FULL_TEXTURE_0 );
  192. }
  193. BindTexture( SHADER_SAMPLER3, NORMALMAP, BUMPFRAME );
  194. if( bHasEnvmap )
  195. {
  196. BindTexture( SHADER_SAMPLER4, ENVMAP, ENVMAPFRAME );
  197. }
  198. if( bHasFlowmap )
  199. {
  200. BindTexture( SHADER_SAMPLER6, FLOWMAP, FLOWMAPFRAME );
  201. }
  202. if( bHasCoreColorTexture )
  203. {
  204. BindTexture( SHADER_SAMPLER7, CORECOLORTEXTURE, CORECOLORTEXTUREFRAME );
  205. }
  206. DECLARE_DYNAMIC_VERTEX_SHADER( core_vs20 );
  207. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  208. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  209. SET_DYNAMIC_VERTEX_SHADER( core_vs20 );
  210. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  211. {
  212. DECLARE_DYNAMIC_PIXEL_SHADER( core_ps20b );
  213. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  214. SET_DYNAMIC_PIXEL_SHADER( core_ps20b );
  215. }
  216. else
  217. {
  218. DECLARE_DYNAMIC_PIXEL_SHADER( core_ps20 );
  219. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  220. SET_DYNAMIC_PIXEL_SHADER( core_ps20 );
  221. }
  222. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_1, BUMPTRANSFORM );
  223. if( g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE )
  224. {
  225. SetPixelShaderConstant( 0, ENVMAPTINT );
  226. SetPixelShaderConstant( 1, REFRACTTINT );
  227. }
  228. else
  229. {
  230. SetPixelShaderConstantGammaToLinear( 0, ENVMAPTINT );
  231. SetPixelShaderConstantGammaToLinear( 1, REFRACTTINT );
  232. }
  233. SetPixelShaderConstant( 2, ENVMAPCONTRAST );
  234. SetPixelShaderConstant( 3, ENVMAPSATURATION );
  235. float c5[4] = { params[REFRACTAMOUNT]->GetFloatValue(),
  236. params[REFRACTAMOUNT]->GetFloatValue(), 0.0f, 0.0f };
  237. pShaderAPI->SetPixelShaderConstant( 5, c5, 1 );
  238. float eyePos[4];
  239. s_pShaderAPI->GetWorldSpaceCameraPosition( eyePos );
  240. s_pShaderAPI->SetPixelShaderConstant( 8, eyePos, 1 );
  241. pShaderAPI->SetPixelShaderFogParams( 11 );
  242. if( bHasFlowmap )
  243. {
  244. float curTime = pShaderAPI->CurrentTime();
  245. float timeVec[4] = { curTime, curTime, curTime, curTime };
  246. pShaderAPI->SetPixelShaderConstant( 6, timeVec, 1 );
  247. SetPixelShaderConstant( 7, FLOWMAPSCROLLRATE );
  248. SetPixelShaderConstant( 9, FLOWMAPTEXCOORDOFFSET );
  249. }
  250. }
  251. Draw();
  252. }
  253. SHADER_DRAW
  254. {
  255. DrawPass( params, pShaderShadow, pShaderAPI, 0, vertexCompression );
  256. DrawPass( params, pShaderShadow, pShaderAPI, 1, vertexCompression );
  257. }
  258. END_SHADER