Counter Strike : Global Offensive Source Code
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.

245 lines
8.1 KiB

  1. //===== Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $Header: $
  6. // $NoKeywords: $
  7. //===========================================================================//
  8. #include "BaseVSShader.h"
  9. #include "cpp_shader_constant_register_map.h"
  10. #include "decalmodulate_vs20.inc"
  11. #include "decalmodulate_ps20.inc"
  12. #include "decalmodulate_ps20b.inc"
  13. #if !defined( _X360 ) && !defined( _PS3 )
  14. #include "decalmodulate_vs30.inc"
  15. #include "decalmodulate_ps30.inc"
  16. #endif
  17. // memdbgon must be the last include file in a .cpp file!!!
  18. #include "tier0/memdbgon.h"
  19. DEFINE_FALLBACK_SHADER( DecalModulate, DecalModulate_DX9 )
  20. BEGIN_VS_SHADER( DecalModulate_dx9,
  21. "Help for DecalModulate_dx9" )
  22. BEGIN_SHADER_PARAMS
  23. SHADER_PARAM( FOGEXPONENT, SHADER_PARAM_TYPE_FLOAT, "0.4", "exponent to tweak fog fade" )
  24. SHADER_PARAM( FOGSCALE, SHADER_PARAM_TYPE_FLOAT, "1.0", "scale to tweak fog fade" )
  25. SHADER_PARAM( FOGFADESTART, SHADER_PARAM_TYPE_FLOAT, "0", "fog amount at which to start fading decal" )
  26. SHADER_PARAM( FOGFADEEND, SHADER_PARAM_TYPE_FLOAT, "0", "fog amount at which to end fading decal" )
  27. END_SHADER_PARAMS
  28. SHADER_FALLBACK
  29. {
  30. return 0;
  31. }
  32. SHADER_INIT_PARAMS()
  33. {
  34. if( !params[ FOGEXPONENT ]->IsDefined() )
  35. {
  36. params[ FOGEXPONENT ]->SetFloatValue( 0.4f );
  37. }
  38. if( !params[ FOGSCALE ]->IsDefined() )
  39. {
  40. params[ FOGSCALE ]->SetFloatValue( 1.0f );
  41. }
  42. if( !params[ FOGFADESTART]->IsDefined() )
  43. {
  44. params[ FOGFADESTART ]->SetFloatValue( 0 );
  45. }
  46. if( !params[ FOGFADEEND ]->IsDefined() )
  47. {
  48. params[ FOGFADEEND ]->SetFloatValue( 0.0f );
  49. }
  50. SET_FLAGS( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  51. #if !defined( _X360 ) && !defined( _PS3 )
  52. if ( g_pHardwareConfig->HasFastVertexTextures() )
  53. {
  54. // The vertex shader uses the vertex id stream
  55. SET_FLAGS2( MATERIAL_VAR2_USES_VERTEXID );
  56. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  57. }
  58. #endif
  59. }
  60. SHADER_INIT
  61. {
  62. LoadTexture( BASETEXTURE );
  63. }
  64. SHADER_DRAW
  65. {
  66. SHADOW_STATE
  67. {
  68. pShaderShadow->EnableAlphaTest( true );
  69. pShaderShadow->AlphaFunc( SHADER_ALPHAFUNC_GREATER, 0.0f );
  70. pShaderShadow->EnableDepthWrites( false );
  71. pShaderShadow->EnablePolyOffset( SHADER_POLYOFFSET_DECAL );
  72. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  73. // Be sure not to write to dest alpha
  74. pShaderShadow->EnableAlphaWrites( false );
  75. // SRGB conversions hose the blend on some hardware, so keep we everything in gamma space
  76. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, false );
  77. pShaderShadow->EnableSRGBWrite( false );
  78. pShaderShadow->EnableBlending( true );
  79. pShaderShadow->BlendFunc( SHADER_BLEND_DST_COLOR, SHADER_BLEND_SRC_COLOR );
  80. pShaderShadow->DisableFogGammaCorrection( true ); //fog should stay exactly middle grey
  81. FogToGrey();
  82. int nLightingPreviewMode = IS_FLAG2_SET( MATERIAL_VAR2_USE_GBUFFER0 ) + 2 * IS_FLAG2_SET( MATERIAL_VAR2_USE_GBUFFER1 );
  83. bool bHasVertexAlpha = IS_FLAG_SET( MATERIAL_VAR_VERTEXCOLOR ) && IS_FLAG_SET( MATERIAL_VAR_VERTEXALPHA );
  84. bool bHasFogFade = ( params[FOGFADEEND]->GetFloatValue() > 0.0f );
  85. #if !defined( _X360 ) && !defined( _PS3 )
  86. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  87. #endif
  88. {
  89. DECLARE_STATIC_VERTEX_SHADER( decalmodulate_vs20 );
  90. SET_STATIC_VERTEX_SHADER_COMBO( VERTEXCOLOR, bHasVertexAlpha );
  91. SET_STATIC_VERTEX_SHADER_COMBO( LIGHTING_PREVIEW, nLightingPreviewMode != 0 );
  92. SET_STATIC_VERTEX_SHADER( decalmodulate_vs20 );
  93. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  94. {
  95. DECLARE_STATIC_PIXEL_SHADER( decalmodulate_ps20b );
  96. SET_STATIC_PIXEL_SHADER_COMBO( VERTEXALPHA, bHasVertexAlpha );
  97. SET_STATIC_PIXEL_SHADER_COMBO( FOGFADE, bHasFogFade );
  98. SET_STATIC_PIXEL_SHADER( decalmodulate_ps20b );
  99. }
  100. else
  101. {
  102. DECLARE_STATIC_PIXEL_SHADER( decalmodulate_ps20 );
  103. SET_STATIC_PIXEL_SHADER_COMBO( VERTEXALPHA, bHasVertexAlpha );
  104. SET_STATIC_PIXEL_SHADER_COMBO( FOGFADE, bHasFogFade );
  105. SET_STATIC_PIXEL_SHADER( decalmodulate_ps20 );
  106. }
  107. }
  108. #if !defined( _X360 ) && !defined( _PS3 )
  109. else
  110. {
  111. DECLARE_STATIC_VERTEX_SHADER( decalmodulate_vs30 );
  112. SET_STATIC_VERTEX_SHADER_COMBO( VERTEXCOLOR, bHasVertexAlpha );
  113. SET_STATIC_VERTEX_SHADER_COMBO( LIGHTING_PREVIEW, nLightingPreviewMode != 0 );
  114. SET_STATIC_VERTEX_SHADER( decalmodulate_vs30 );
  115. DECLARE_STATIC_PIXEL_SHADER( decalmodulate_ps30 );
  116. SET_STATIC_PIXEL_SHADER_COMBO( VERTEXALPHA, bHasVertexAlpha );
  117. SET_STATIC_PIXEL_SHADER_COMBO( FOGFADE, bHasFogFade );
  118. SET_STATIC_PIXEL_SHADER( decalmodulate_ps30 );
  119. }
  120. #endif
  121. // Set stream format (note that this shader supports compression)
  122. unsigned int flags = VERTEX_POSITION | VERTEX_FORMAT_COMPRESSED;
  123. if ( bHasVertexAlpha )
  124. {
  125. flags |= VERTEX_COLOR;
  126. }
  127. #if !defined( _X360 ) && !defined( _PS3 )
  128. // The VS30 shader offsets decals along the normal (for morphed geom)
  129. flags |= g_pHardwareConfig->HasFastVertexTextures() ? VERTEX_NORMAL : 0;
  130. #endif
  131. int pTexCoordDim[3] = { 2, 0, 3 };
  132. int nTexCoordCount = 1;
  133. int userDataSize = 0;
  134. #if !defined( _X360 ) && !defined( _PS3 )
  135. if ( g_pHardwareConfig->HasFastVertexTextures() )
  136. {
  137. nTexCoordCount = 3;
  138. }
  139. #endif
  140. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, pTexCoordDim, userDataSize );
  141. }
  142. DYNAMIC_STATE
  143. {
  144. if ( pShaderAPI->InFlashlightMode() && !( IsX360() || IsPS3() ) )
  145. {
  146. // Don't draw anything for the flashlight pass
  147. Draw( false );
  148. return;
  149. }
  150. BindTexture( SHADER_SAMPLER0, TEXTURE_BINDFLAGS_NONE, BASETEXTURE, FRAME );
  151. // Set an identity base texture transformation
  152. Vector4D transformation[2];
  153. transformation[0].Init( 1.0f, 0.0f, 0.0f, 0.0f );
  154. transformation[1].Init( 0.0f, 1.0f, 0.0f, 0.0f );
  155. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, transformation[0].Base(), 2 );
  156. pShaderAPI->SetPixelShaderFogParams( PSREG_FOG_PARAMS );
  157. float vEyePos_SpecExponent[4];
  158. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos_SpecExponent );
  159. vEyePos_SpecExponent[3] = 0.0f;
  160. pShaderAPI->SetPixelShaderConstant( PSREG_EYEPOS_SPEC_EXPONENT, vEyePos_SpecExponent, 1 );
  161. // fog tweaks
  162. float fConsts[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  163. fConsts[0] = params[ FOGEXPONENT ]->GetFloatValue();
  164. fConsts[1] = params[ FOGSCALE ]->GetFloatValue();
  165. fConsts[2] = params[ FOGFADESTART ]->GetFloatValue();
  166. fConsts[3] = params[ FOGFADEEND ]->GetFloatValue();
  167. pShaderAPI->SetPixelShaderConstant( 0, fConsts );
  168. #if !defined( _X360 ) && !defined( _PS3 )
  169. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  170. #endif
  171. {
  172. DECLARE_DYNAMIC_VERTEX_SHADER( decalmodulate_vs20 );
  173. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, 0 );
  174. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  175. // SET_DYNAMIC_VERTEX_SHADER_COMBO( TESSELLATION, 0 ); // JasonM TODO: set this appropriately when we care about decals on subds
  176. SET_DYNAMIC_VERTEX_SHADER( decalmodulate_vs20 );
  177. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  178. {
  179. DECLARE_DYNAMIC_PIXEL_SHADER( decalmodulate_ps20b );
  180. SET_DYNAMIC_PIXEL_SHADER( decalmodulate_ps20b );
  181. }
  182. else
  183. {
  184. DECLARE_DYNAMIC_PIXEL_SHADER( decalmodulate_ps20 );
  185. SET_DYNAMIC_PIXEL_SHADER( decalmodulate_ps20 );
  186. }
  187. }
  188. #if !defined( _X360 ) && !defined( _PS3 )
  189. else
  190. {
  191. SetHWMorphVertexShaderState( VERTEX_SHADER_SHADER_SPECIFIC_CONST_6, VERTEX_SHADER_SHADER_SPECIFIC_CONST_7, SHADER_VERTEXTEXTURE_SAMPLER0 );
  192. DECLARE_DYNAMIC_VERTEX_SHADER( decalmodulate_vs30 );
  193. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  194. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  195. // SET_DYNAMIC_VERTEX_SHADER_COMBO( TESSELLATION, 0 ); // JasonM TODO: set this appropriately when we care about decals on subds
  196. SET_DYNAMIC_VERTEX_SHADER( decalmodulate_vs30 );
  197. DECLARE_DYNAMIC_PIXEL_SHADER( decalmodulate_ps30 );
  198. SET_DYNAMIC_PIXEL_SHADER( decalmodulate_ps30 );
  199. bool bUnusedTexCoords[3] = { false, false, !pShaderAPI->IsHWMorphingEnabled() };
  200. pShaderAPI->MarkUnusedVertexFields( 0, 3, bUnusedTexCoords );
  201. }
  202. #endif
  203. }
  204. Draw( );
  205. }
  206. END_SHADER