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.

290 lines
10 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. /* Based heavily on cloak_blended_pass, look at cloak_blended_pass_helper.cpp
  3. ==================================================================================================== */
  4. #include "BaseVSShader.h"
  5. #include "mathlib/vmatrix.h"
  6. #include "weapon_sheen_pass_helper.h"
  7. #include "convar.h"
  8. // Auto generated inc files
  9. #include "weapon_sheen_pass_vs20.inc"
  10. #include "weapon_sheen_pass_ps20.inc"
  11. #include "weapon_sheen_pass_ps20b.inc"
  12. #ifndef _X360
  13. #include "weapon_sheen_pass_vs30.inc"
  14. #include "weapon_sheen_pass_ps30.inc"
  15. #endif
  16. void InitParamsWeaponSheenPass( CBaseVSShader *pShader, IMaterialVar** params, const char *pMaterialName, WeaponSheenPassVars_t &info )
  17. {
  18. // Set material flags
  19. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  20. SET_FLAGS( MATERIAL_VAR_MODEL );
  21. SET_FLAGS2( MATERIAL_VAR2_NEEDS_TANGENT_SPACES );
  22. // Set material parameter default values
  23. if ( ( info.m_nSheenMapMaskFrame != -1 ) && ( !params[info.m_nSheenMapMaskFrame]->IsDefined() ) )
  24. {
  25. params[info.m_nSheenMapMaskFrame]->SetFloatValue( 0 );
  26. }
  27. if ( ( info.m_nSheenMapTint != -1 ) && ( !params[info.m_nSheenMapTint]->IsDefined() ) )
  28. {
  29. params[info.m_nSheenMapTint]->SetVecValue( 1.0f, 1.0f, 1.0f, 1.0f );
  30. }
  31. if ( ( info.m_nSheenMapMaskScaleX != -1 ) && ( !params[info.m_nSheenMapMaskScaleX]->IsDefined() ) )
  32. {
  33. params[info.m_nSheenMapMaskScaleX]->SetFloatValue( 1.0f );
  34. }
  35. if ( ( info.m_nSheenMapMaskScaleY != -1 ) && ( !params[info.m_nSheenMapMaskScaleY]->IsDefined() ) )
  36. {
  37. params[info.m_nSheenMapMaskScaleY]->SetFloatValue( 1.0f );
  38. }
  39. if ( ( info.m_nSheenMapMaskOffsetX != -1 ) && ( !params[info.m_nSheenMapMaskOffsetX]->IsDefined() ) )
  40. {
  41. params[info.m_nSheenMapMaskOffsetX]->SetFloatValue( 0 );
  42. }
  43. if ( ( info.m_nSheenMapMaskOffsetY != -1 ) && ( !params[info.m_nSheenMapMaskOffsetY]->IsDefined() ) )
  44. {
  45. params[info.m_nSheenMapMaskOffsetY]->SetFloatValue( 0 );
  46. }
  47. if ( ( info.m_nSheenMapMaskDirection != -1 ) && ( !params[info.m_nSheenMapMaskDirection]->IsDefined() ) )
  48. {
  49. params[info.m_nSheenMapMaskDirection]->SetFloatValue( 0 );
  50. }
  51. if( (info.m_nSheenIndex != -1 ) && !params[info.m_nSheenIndex]->IsDefined() )
  52. {
  53. params[info.m_nSheenIndex]->SetIntValue( 0 );
  54. }
  55. if( (info.m_nBumpFrame != -1 ) && !params[info.m_nBumpFrame]->IsDefined() )
  56. {
  57. params[info.m_nBumpFrame]->SetIntValue( 0 );
  58. }
  59. }
  60. void InitWeaponSheenPass( CBaseVSShader *pShader, IMaterialVar** params, WeaponSheenPassVars_t &info )
  61. {
  62. // Load textures
  63. if ( g_pConfig->UseBumpmapping() )
  64. {
  65. if ( (info.m_nBumpmap != -1) && params[info.m_nBumpmap]->IsDefined() )
  66. {
  67. pShader->LoadTexture( info.m_nBumpmap );
  68. }
  69. }
  70. if ( (info.m_nSheenMap != -1) && params[info.m_nSheenMap]->IsDefined() )
  71. {
  72. pShader->LoadCubeMap( info.m_nSheenMap, g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE ? TEXTUREFLAGS_SRGB : 0 );
  73. }
  74. if ( (info.m_nSheenMapMask != -1) && params[info.m_nSheenMapMask]->IsDefined() )
  75. {
  76. pShader->LoadTexture( info.m_nSheenMapMask );
  77. }
  78. }
  79. void DrawWeaponSheenPass( CBaseVSShader *pShader, IMaterialVar** params, IShaderDynamicAPI *pShaderAPI,
  80. IShaderShadow* pShaderShadow, WeaponSheenPassVars_t &info, VertexCompressionType_t vertexCompression )
  81. {
  82. bool bBumpMapping = ( !g_pConfig->UseBumpmapping() ) || ( info.m_nBumpmap == -1 ) || !params[info.m_nBumpmap]->IsTexture() ? 0 : 1;
  83. SHADOW_STATE
  84. {
  85. // Reset shadow state manually since we're drawing from two materials
  86. pShader->SetInitialShadowState( );
  87. // Set stream format (note that this shader supports compression)
  88. unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL | VERTEX_FORMAT_COMPRESSED;
  89. int nTexCoordCount = 1;
  90. int userDataSize = 0;
  91. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, NULL, userDataSize );
  92. #ifndef _X360
  93. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  94. #endif
  95. {
  96. // Vertex Shader
  97. DECLARE_STATIC_VERTEX_SHADER( weapon_sheen_pass_vs20 );
  98. SET_STATIC_VERTEX_SHADER_COMBO( BUMPMAP, bBumpMapping ? 1 : 0 );
  99. SET_STATIC_VERTEX_SHADER( weapon_sheen_pass_vs20 );
  100. // Pixel Shader
  101. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  102. {
  103. DECLARE_STATIC_PIXEL_SHADER( weapon_sheen_pass_ps20b );
  104. SET_STATIC_PIXEL_SHADER_COMBO( BUMPMAP, bBumpMapping ? 1 : 0 );
  105. SET_STATIC_PIXEL_SHADER( weapon_sheen_pass_ps20b );
  106. }
  107. else
  108. {
  109. DECLARE_STATIC_PIXEL_SHADER( weapon_sheen_pass_ps20 );
  110. SET_STATIC_PIXEL_SHADER_COMBO( BUMPMAP, bBumpMapping ? 1 : 0 );
  111. SET_STATIC_PIXEL_SHADER( weapon_sheen_pass_ps20 );
  112. }
  113. }
  114. #ifndef _X360
  115. else
  116. {
  117. // The vertex shader uses the vertex id stream
  118. SET_FLAGS2( MATERIAL_VAR2_USES_VERTEXID );
  119. // Vertex Shader
  120. DECLARE_STATIC_VERTEX_SHADER( weapon_sheen_pass_vs30 );
  121. SET_STATIC_VERTEX_SHADER_COMBO( BUMPMAP, bBumpMapping ? 1 : 0 );
  122. SET_STATIC_VERTEX_SHADER( weapon_sheen_pass_vs30 );
  123. // Pixel Shader
  124. DECLARE_STATIC_PIXEL_SHADER( weapon_sheen_pass_ps30 );
  125. SET_STATIC_PIXEL_SHADER_COMBO( BUMPMAP, bBumpMapping ? 1 : 0 );
  126. SET_STATIC_PIXEL_SHADER( weapon_sheen_pass_ps30 );
  127. }
  128. #endif
  129. // Textures
  130. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true ); // Refraction texture
  131. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  132. if ( bBumpMapping )
  133. {
  134. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true ); // Bump
  135. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, false ); // Not sRGB
  136. }
  137. pShaderShadow->EnableSRGBWrite( true );
  138. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  139. if( g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE )
  140. {
  141. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER2, true );
  142. }
  143. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  144. if( g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE )
  145. {
  146. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER3, true );
  147. }
  148. // Blending
  149. pShader->EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  150. pShaderShadow->EnableAlphaWrites( false );
  151. // !!! We need to turn this back on because EnableAlphaBlending() above disables it!
  152. pShaderShadow->EnableDepthWrites( true );
  153. }
  154. DYNAMIC_STATE
  155. {
  156. // Reset render state manually since we're drawing from two materials
  157. pShaderAPI->SetDefaultState();
  158. // Set Vertex Shader Constants
  159. if ( ( bBumpMapping ) && ( info.m_nBumpTransform != -1 ) )
  160. {
  161. pShader->SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, info.m_nBumpTransform );
  162. }
  163. #ifndef _X360
  164. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  165. #endif
  166. {
  167. // Set Vertex Shader Combos
  168. DECLARE_DYNAMIC_VERTEX_SHADER( weapon_sheen_pass_vs20 );
  169. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  170. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  171. SET_DYNAMIC_VERTEX_SHADER( weapon_sheen_pass_vs20 );
  172. // Set Pixel Shader Combos
  173. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  174. {
  175. DECLARE_DYNAMIC_PIXEL_SHADER( weapon_sheen_pass_ps20b );
  176. SET_DYNAMIC_PIXEL_SHADER( weapon_sheen_pass_ps20b );
  177. }
  178. else
  179. {
  180. DECLARE_DYNAMIC_PIXEL_SHADER( weapon_sheen_pass_ps20 );
  181. SET_DYNAMIC_PIXEL_SHADER( weapon_sheen_pass_ps20 );
  182. }
  183. }
  184. #ifndef _X360
  185. else
  186. {
  187. pShader->SetHWMorphVertexShaderState( VERTEX_SHADER_SHADER_SPECIFIC_CONST_6, VERTEX_SHADER_SHADER_SPECIFIC_CONST_7, SHADER_VERTEXTEXTURE_SAMPLER0 );
  188. // Set Vertex Shader Combos
  189. DECLARE_DYNAMIC_VERTEX_SHADER( weapon_sheen_pass_vs30 );
  190. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  191. SET_DYNAMIC_VERTEX_SHADER_COMBO( MORPHING, pShaderAPI->IsHWMorphingEnabled() );
  192. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  193. SET_DYNAMIC_VERTEX_SHADER( weapon_sheen_pass_vs30 );
  194. // Set Pixel Shader Combos
  195. DECLARE_DYNAMIC_PIXEL_SHADER( weapon_sheen_pass_ps30 );
  196. SET_DYNAMIC_PIXEL_SHADER( weapon_sheen_pass_ps30 );
  197. }
  198. #endif
  199. // Bind textures
  200. pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, TEXTURE_FRAME_BUFFER_FULL_TEXTURE_0 ); // Refraction Map
  201. if ( bBumpMapping )
  202. {
  203. pShader->BindTexture( SHADER_SAMPLER1, info.m_nBumpmap, info.m_nBumpFrame );
  204. }
  205. if ( info.m_nSheenMap != -1 )
  206. {
  207. pShader->BindTexture( SHADER_SAMPLER2, info.m_nSheenMap, -1 );
  208. }
  209. if ( info.m_nSheenMapMask != -1 )
  210. {
  211. pShader->BindTexture( SHADER_SAMPLER3, info.m_nSheenMapMask, info.m_nSheenMapMaskFrame );
  212. }
  213. // Set Pixel Shader Constants
  214. float vEyePos[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  215. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos );
  216. pShaderAPI->SetPixelShaderConstant( 5, vEyePos, 1 );
  217. float vPackedConst1[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  218. vPackedConst1[0] = IS_PARAM_DEFINED( info.m_nSheenMapMaskScaleX ) ? params[info.m_nSheenMapMaskScaleX]->GetFloatValue() : 1.0f;
  219. vPackedConst1[1] = IS_PARAM_DEFINED( info.m_nSheenMapMaskScaleY ) ? params[info.m_nSheenMapMaskScaleY]->GetFloatValue() : 1.0f;
  220. vPackedConst1[2] = IS_PARAM_DEFINED( info.m_nSheenMapMaskOffsetX ) ? params[info.m_nSheenMapMaskOffsetX]->GetFloatValue() : 0.0f;
  221. vPackedConst1[3] = IS_PARAM_DEFINED( info.m_nSheenMapMaskOffsetY ) ? params[info.m_nSheenMapMaskOffsetY]->GetFloatValue() : 0.0f;
  222. pShaderAPI->SetPixelShaderConstant( 6, vPackedConst1, 1 );
  223. float vPackedConst2[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  224. vPackedConst2[0] = IS_PARAM_DEFINED( info.m_nSheenMapMaskDirection ) ? params[info.m_nSheenMapMaskDirection]->GetFloatValue() : 0.0f;
  225. vPackedConst2[1] = IS_PARAM_DEFINED( info.m_nSheenIndex ) ? params[info.m_nSheenIndex]->GetFloatValue() : 0.0f;
  226. pShaderAPI->SetPixelShaderConstant( 7, vPackedConst2, 1 );
  227. // Map color tint
  228. pShaderAPI->SetPixelShaderConstant( 8, IS_PARAM_DEFINED( info.m_nSheenMapTint ) ? params[info.m_nSheenMapTint]->GetVecValue() : kDefaultSheenColorTint, 1 );
  229. // Set c0 and c1 to contain first two rows of ViewProj matrix
  230. VMatrix mView, mProj;
  231. pShaderAPI->GetMatrix( MATERIAL_VIEW, mView.m[0] );
  232. pShaderAPI->GetMatrix( MATERIAL_PROJECTION, mProj.m[0] );
  233. VMatrix mViewProj = mView * mProj;
  234. mViewProj = mViewProj.Transpose3x3();
  235. pShaderAPI->SetPixelShaderConstant( 0, mViewProj.m[0], 2 );
  236. }
  237. pShader->Draw();
  238. }
  239. bool ShouldDrawMaterialSheen ( IMaterialVar** params, WeaponSheenPassVars_t &info )
  240. {
  241. // If the frame is zero we're not rendering
  242. if ( IS_PARAM_DEFINED( info.m_nSheenMapMaskFrame ) && params[info.m_nSheenMapMaskFrame]->GetIntValue() > 0 )
  243. {
  244. return true;
  245. }
  246. return false;
  247. }