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.

270 lines
9.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #include "BaseVSShader.h"
  7. #include "mathlib/bumpvects.h"
  8. #include "cpp_shader_constant_register_map.h"
  9. #include "lightmappedgeneric_vs20.inc"
  10. #include "lightmappedgeneric_decal_vs20.inc"
  11. #include "lightmappedgeneric_decal_ps20.inc"
  12. #include "lightmappedgeneric_decal_ps20b.inc"
  13. #include "decalbasetimeslightmapalphablendselfillum2_ps20.inc"
  14. #include "decalbasetimeslightmapalphablendselfillum2_ps20b.inc"
  15. // memdbgon must be the last include file in a .cpp file!!!
  16. #include "tier0/memdbgon.h"
  17. DEFINE_FALLBACK_SHADER( DecalBaseTimesLightmapAlphaBlendSelfIllum, DecalBaseTimesLightmapAlphaBlendSelfIllum_DX9 )
  18. extern ConVar r_flashlight_version2;
  19. BEGIN_VS_SHADER( DecalBaseTimesLightmapAlphaBlendSelfIllum_DX9, "" )
  20. BEGIN_SHADER_PARAMS
  21. SHADER_PARAM_OVERRIDE( BASETEXTURE, SHADER_PARAM_TYPE_TEXTURE, "decals/decalporthole001b", "decal base texture", 0 )
  22. SHADER_PARAM( SELFILLUMTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "decals/decalporthole001b_mask", "self-illum texture" )
  23. SHADER_PARAM( SELFILLUMTEXTUREFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "self-illum texture frame" )
  24. END_SHADER_PARAMS
  25. SHADER_INIT_PARAMS()
  26. {
  27. if ( g_pHardwareConfig->SupportsBorderColor() )
  28. {
  29. params[FLASHLIGHTTEXTURE]->SetStringValue( "effects/flashlight_border" );
  30. }
  31. else
  32. {
  33. params[FLASHLIGHTTEXTURE]->SetStringValue( "effects/flashlight001" );
  34. }
  35. SET_FLAGS( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  36. SET_FLAGS( MATERIAL_VAR_TRANSLUCENT );
  37. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_LIGHTMAP );
  38. }
  39. SHADER_FALLBACK
  40. {
  41. if ( g_pHardwareConfig->GetDXSupportLevel() < 90 )
  42. {
  43. return "DecalBaseTimesLightmapAlphaBlendSelfIllum_DX8";
  44. }
  45. return 0;
  46. }
  47. SHADER_INIT
  48. {
  49. LoadTexture( FLASHLIGHTTEXTURE, TEXTUREFLAGS_SRGB );
  50. LoadTexture( BASETEXTURE, TEXTUREFLAGS_SRGB );
  51. LoadTexture( SELFILLUMTEXTURE );
  52. }
  53. void DrawDecal( IMaterialVar **params, IShaderDynamicAPI *pShaderAPI, IShaderShadow *pShaderShadow )
  54. {
  55. if( IsSnapshotting() )
  56. {
  57. pShaderShadow->EnableDepthWrites( false );
  58. pShaderShadow->EnablePolyOffset( SHADER_POLYOFFSET_DECAL );
  59. pShaderShadow->EnableBlending( true );
  60. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  61. pShaderShadow->EnableSRGBWrite( true );
  62. // Base Texture
  63. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  64. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  65. // Lightmaps
  66. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  67. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE );
  68. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  69. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER2, g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE );
  70. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  71. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER3, g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE );
  72. int pTexCoords[3] = { 2, 2, 1 };
  73. pShaderShadow->VertexShaderVertexFormat( VERTEX_POSITION | VERTEX_COLOR, 3, pTexCoords, 0 );
  74. DECLARE_STATIC_VERTEX_SHADER( lightmappedgeneric_decal_vs20 );
  75. SET_STATIC_VERTEX_SHADER( lightmappedgeneric_decal_vs20 );
  76. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  77. {
  78. DECLARE_STATIC_PIXEL_SHADER( lightmappedgeneric_decal_ps20b );
  79. SET_STATIC_PIXEL_SHADER( lightmappedgeneric_decal_ps20b );
  80. }
  81. else
  82. {
  83. DECLARE_STATIC_PIXEL_SHADER( lightmappedgeneric_decal_ps20 );
  84. SET_STATIC_PIXEL_SHADER( lightmappedgeneric_decal_ps20 );
  85. }
  86. FogToFogColor();
  87. }
  88. else
  89. {
  90. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  91. // Load the z^2 components of the lightmap coordinate axes only
  92. // This is (N dot basis)^2
  93. Vector vecZValues( g_localBumpBasis[0].z, g_localBumpBasis[1].z, g_localBumpBasis[2].z );
  94. vecZValues *= vecZValues;
  95. Vector4D basis[3];
  96. basis[0].Init( vecZValues.x, vecZValues.x, vecZValues.x, 0.0f );
  97. basis[1].Init( vecZValues.y, vecZValues.y, vecZValues.y, 0.0f );
  98. basis[2].Init( vecZValues.z, vecZValues.z, vecZValues.z, 0.0f );
  99. pShaderAPI->SetPixelShaderConstant( 0, (float*)basis, 3 );
  100. pShaderAPI->BindStandardTexture( SHADER_SAMPLER1, TEXTURE_LIGHTMAP_BUMPED );
  101. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, BASETEXTURETRANSFORM );
  102. SetModulationPixelShaderDynamicState( 3 );
  103. DECLARE_DYNAMIC_VERTEX_SHADER( lightmappedgeneric_decal_vs20 );
  104. SET_DYNAMIC_VERTEX_SHADER_COMBO( DOWATERFOG, pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  105. SET_DYNAMIC_VERTEX_SHADER( lightmappedgeneric_decal_vs20 );
  106. pShaderAPI->SetPixelShaderFogParams( PSREG_FOG_PARAMS );
  107. float vEyePos_SpecExponent[4];
  108. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos_SpecExponent );
  109. vEyePos_SpecExponent[3] = 0.0f;
  110. pShaderAPI->SetPixelShaderConstant( PSREG_EYEPOS_SPEC_EXPONENT, vEyePos_SpecExponent, 1 );
  111. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  112. {
  113. DECLARE_DYNAMIC_PIXEL_SHADER( lightmappedgeneric_decal_ps20b );
  114. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  115. SET_DYNAMIC_PIXEL_SHADER( lightmappedgeneric_decal_ps20b );
  116. }
  117. else
  118. {
  119. DECLARE_DYNAMIC_PIXEL_SHADER( lightmappedgeneric_decal_ps20 );
  120. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  121. SET_DYNAMIC_PIXEL_SHADER( lightmappedgeneric_decal_ps20 );
  122. }
  123. }
  124. Draw();
  125. if( IsSnapshotting() )
  126. {
  127. SetInitialShadowState( );
  128. pShaderShadow->EnableDepthWrites( false );
  129. pShaderShadow->EnablePolyOffset( SHADER_POLYOFFSET_DECAL );
  130. pShaderShadow->EnableBlending( true );
  131. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  132. // Base texture
  133. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  134. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  135. pShaderShadow->EnableSRGBWrite( true );
  136. pShaderShadow->VertexShaderVertexFormat( VERTEX_POSITION, 1, 0, 0 );
  137. DECLARE_STATIC_VERTEX_SHADER( lightmappedgeneric_vs20 );
  138. SET_STATIC_VERTEX_SHADER_COMBO( ENVMAP_MASK, false );
  139. SET_STATIC_VERTEX_SHADER_COMBO( TANGENTSPACE, false );
  140. SET_STATIC_VERTEX_SHADER_COMBO( BUMPMAP, false );
  141. SET_STATIC_VERTEX_SHADER_COMBO( DIFFUSEBUMPMAP, false );
  142. SET_STATIC_VERTEX_SHADER_COMBO( VERTEXCOLOR, false );
  143. SET_STATIC_VERTEX_SHADER_COMBO( VERTEXALPHATEXBLENDFACTOR, false );
  144. SET_STATIC_VERTEX_SHADER_COMBO( RELIEF_MAPPING, false );
  145. SET_STATIC_VERTEX_SHADER_COMBO( SEAMLESS, false );
  146. SET_STATIC_VERTEX_SHADER_COMBO( BUMPMASK, false );
  147. #ifdef _X360
  148. SET_STATIC_VERTEX_SHADER_COMBO( FLASHLIGHT, 0 );
  149. #endif
  150. SET_STATIC_VERTEX_SHADER( lightmappedgeneric_vs20 );
  151. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  152. {
  153. DECLARE_STATIC_PIXEL_SHADER( decalbasetimeslightmapalphablendselfillum2_ps20b );
  154. SET_STATIC_PIXEL_SHADER( decalbasetimeslightmapalphablendselfillum2_ps20b );
  155. }
  156. else
  157. {
  158. DECLARE_STATIC_PIXEL_SHADER( decalbasetimeslightmapalphablendselfillum2_ps20 );
  159. SET_STATIC_PIXEL_SHADER( decalbasetimeslightmapalphablendselfillum2_ps20 );
  160. }
  161. FogToFogColor();
  162. }
  163. else
  164. {
  165. BindTexture( SHADER_SAMPLER0, SELFILLUMTEXTURE, SELFILLUMTEXTUREFRAME );
  166. DECLARE_DYNAMIC_VERTEX_SHADER( lightmappedgeneric_vs20 );
  167. SET_DYNAMIC_VERTEX_SHADER_COMBO( FASTPATH, false );
  168. SET_DYNAMIC_VERTEX_SHADER_COMBO( DOWATERFOG, pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  169. SET_DYNAMIC_VERTEX_SHADER_COMBO( LIGHTING_PREVIEW, false );
  170. SET_DYNAMIC_VERTEX_SHADER( lightmappedgeneric_vs20 );
  171. pShaderAPI->SetPixelShaderFogParams( PSREG_FOG_PARAMS );
  172. float vEyePos_SpecExponent[4];
  173. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos_SpecExponent );
  174. vEyePos_SpecExponent[3] = 0.0f;
  175. pShaderAPI->SetPixelShaderConstant( PSREG_EYEPOS_SPEC_EXPONENT, vEyePos_SpecExponent, 1 );
  176. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  177. {
  178. DECLARE_DYNAMIC_PIXEL_SHADER( decalbasetimeslightmapalphablendselfillum2_ps20b );
  179. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  180. SET_DYNAMIC_PIXEL_SHADER( decalbasetimeslightmapalphablendselfillum2_ps20b );
  181. }
  182. else
  183. {
  184. DECLARE_DYNAMIC_PIXEL_SHADER( decalbasetimeslightmapalphablendselfillum2_ps20 );
  185. SET_DYNAMIC_PIXEL_SHADER_COMBO( PIXELFOGTYPE, pShaderAPI->GetPixelFogCombo() );
  186. SET_DYNAMIC_PIXEL_SHADER( decalbasetimeslightmapalphablendselfillum2_ps20 );
  187. }
  188. }
  189. Draw();
  190. }
  191. void DrawPass( IMaterialVar** params, IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow, bool bUsingFlashlight )
  192. {
  193. if( bUsingFlashlight )
  194. {
  195. DrawFlashlight_dx90_Vars_t flashlightVars;
  196. flashlightVars.m_bBump = false;
  197. flashlightVars.m_nBumpmapVar = -1;
  198. flashlightVars.m_nBumpmapFrame = -1;
  199. flashlightVars.m_nBumpTransform = -1;
  200. flashlightVars.m_nFlashlightTextureVar = FLASHLIGHTTEXTURE;
  201. flashlightVars.m_nFlashlightTextureFrameVar = FLASHLIGHTTEXTUREFRAME;
  202. flashlightVars.m_bLightmappedGeneric = true;
  203. flashlightVars.m_bWorldVertexTransition = false;
  204. // int nWorldVertexTransitionPassID = 0
  205. flashlightVars.m_nBaseTexture2Var = -1;
  206. flashlightVars.m_nBaseTexture2FrameVar = -1;
  207. flashlightVars.m_bTeeth = false;
  208. flashlightVars.m_nTeethForwardVar = 0;
  209. flashlightVars.m_nTeethIllumFactorVar = 0;
  210. DrawFlashlight_dx90( params, pShaderAPI, pShaderShadow, flashlightVars );
  211. }
  212. else
  213. {
  214. DrawDecal( params, pShaderAPI, pShaderShadow );
  215. }
  216. }
  217. SHADER_DRAW
  218. {
  219. bool bUsingFlashlight = UsingFlashlight( params );
  220. if ( bUsingFlashlight && ( IsX360() || r_flashlight_version2.GetInt() ) )
  221. {
  222. DrawPass( params, pShaderAPI, pShaderShadow, false );
  223. if ( pShaderShadow )
  224. {
  225. SetInitialShadowState( );
  226. }
  227. }
  228. DrawPass( params, pShaderAPI, pShaderShadow, bUsingFlashlight );
  229. }
  230. END_SHADER