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.

199 lines
7.8 KiB

  1. //========= Copyright (c) 1996-2006, Valve Corporation, All rights reserved. ============//
  2. #include "BaseVSShader.h"
  3. #include "mathlib/vmatrix.h"
  4. #include "aftershock_helper.h"
  5. #include "convar.h"
  6. // Auto generated inc files
  7. #include "aftershock_vs20.inc"
  8. #include "aftershock_ps20.inc"
  9. #include "aftershock_ps20b.inc"
  10. // NOTE: This has to be the last file included!
  11. #include "tier0/memdbgon.h"
  12. void InitParamsAftershock( CBaseVSShader *pShader, IMaterialVar** params, const char *pMaterialName, AftershockVars_t &info )
  13. {
  14. // Set material flags
  15. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  16. SET_FLAGS2( MATERIAL_VAR2_NEEDS_TANGENT_SPACES );
  17. SET_FLAGS( MATERIAL_VAR_TRANSLUCENT );
  18. SET_FLAGS2( MATERIAL_VAR2_NEEDS_POWER_OF_TWO_FRAME_BUFFER_TEXTURE );
  19. // Set material parameter default values
  20. if ( ( info.m_nRefractAmount != -1 ) && ( !params[info.m_nRefractAmount]->IsDefined() ) )
  21. {
  22. params[info.m_nRefractAmount]->SetFloatValue( kDefaultRefractAmount );
  23. }
  24. if ( ( info.m_nColorTint != -1 ) && ( !params[info.m_nColorTint]->IsDefined() ) )
  25. {
  26. params[info.m_nColorTint]->SetVecValue( kDefaultColorTint[0], kDefaultColorTint[1], kDefaultColorTint[2], kDefaultColorTint[3] );
  27. }
  28. if( (info.m_nBumpFrame != -1 ) && !params[info.m_nBumpFrame]->IsDefined() )
  29. {
  30. params[info.m_nBumpFrame]->SetIntValue( 0 );
  31. }
  32. if ( ( info.m_nSilhouetteThickness != -1 ) && ( !params[info.m_nSilhouetteThickness]->IsDefined() ) )
  33. {
  34. params[info.m_nSilhouetteThickness]->SetFloatValue( kDefaultSilhouetteThickness );
  35. }
  36. if ( ( info.m_nSilhouetteColor != -1 ) && ( !params[info.m_nSilhouetteColor]->IsDefined() ) )
  37. {
  38. params[info.m_nSilhouetteColor]->SetVecValue( kDefaultSilhouetteColor[0], kDefaultSilhouetteColor[1], kDefaultSilhouetteColor[2], kDefaultSilhouetteColor[3] );
  39. }
  40. if ( ( info.m_nGroundMin != -1 ) && ( !params[info.m_nGroundMin]->IsDefined() ) )
  41. {
  42. params[info.m_nGroundMin]->SetFloatValue( kDefaultGroundMin );
  43. }
  44. if ( ( info.m_nGroundMax != -1 ) && ( !params[info.m_nGroundMax]->IsDefined() ) )
  45. {
  46. params[info.m_nGroundMax]->SetFloatValue( kDefaultGroundMax );
  47. }
  48. if ( ( info.m_nBlurAmount != -1 ) && ( !params[info.m_nBlurAmount]->IsDefined() ) )
  49. {
  50. params[info.m_nBlurAmount]->SetFloatValue( kDefaultBlurAmount );
  51. }
  52. SET_PARAM_FLOAT_IF_NOT_DEFINED( info.m_nTime, 0.0f );
  53. }
  54. void InitAftershock( CBaseVSShader *pShader, IMaterialVar** params, AftershockVars_t &info )
  55. {
  56. // Load textures
  57. if ( (info.m_nBumpmap != -1) && params[info.m_nBumpmap]->IsDefined() )
  58. {
  59. pShader->LoadTexture( info.m_nBumpmap );
  60. }
  61. }
  62. void DrawAftershock( CBaseVSShader *pShader, IMaterialVar** params, IShaderDynamicAPI *pShaderAPI,
  63. IShaderShadow* pShaderShadow, AftershockVars_t &info, VertexCompressionType_t vertexCompression )
  64. {
  65. bool bBumpMapping = ( info.m_nBumpmap == -1 ) || !params[info.m_nBumpmap]->IsTexture() ? 0 : 1;
  66. SHADOW_STATE
  67. {
  68. // Set stream format (note that this shader supports compression)
  69. unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL | VERTEX_FORMAT_COMPRESSED;
  70. int nTexCoordCount = 1;
  71. int userDataSize = 0;
  72. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, NULL, userDataSize );
  73. // Vertex Shader
  74. DECLARE_STATIC_VERTEX_SHADER( aftershock_vs20 );
  75. SET_STATIC_VERTEX_SHADER( aftershock_vs20 );
  76. // Pixel Shader
  77. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  78. {
  79. DECLARE_STATIC_PIXEL_SHADER( aftershock_ps20b );
  80. SET_STATIC_PIXEL_SHADER( aftershock_ps20b );
  81. }
  82. else
  83. {
  84. DECLARE_STATIC_PIXEL_SHADER( aftershock_ps20 );
  85. SET_STATIC_PIXEL_SHADER( aftershock_ps20 );
  86. }
  87. // Textures
  88. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true ); // Refraction texture
  89. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  90. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true ); // Bump
  91. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, false ); // Not sRGB
  92. pShaderShadow->EnableSRGBWrite( true );
  93. // Blending
  94. pShader->EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  95. pShaderShadow->EnableAlphaWrites( false );
  96. // !!! We need to turn this back on because EnableAlphaBlending() above disables it!
  97. //pShaderShadow->EnableDepthWrites( true );
  98. }
  99. DYNAMIC_STATE
  100. {
  101. // Set Vertex Shader Combos
  102. DECLARE_DYNAMIC_VERTEX_SHADER( aftershock_vs20 );
  103. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  104. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  105. SET_DYNAMIC_VERTEX_SHADER( aftershock_vs20 );
  106. // Set Vertex Shader Constants
  107. if ( info.m_nBumpTransform != -1 )
  108. {
  109. pShader->SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_1, info.m_nBumpTransform );
  110. }
  111. // Time % 1000
  112. float vPackedVsConst1[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  113. float flTime = IS_PARAM_DEFINED( info.m_nTime ) && params[info.m_nTime]->GetFloatValue() > 0.0f ? params[info.m_nTime]->GetFloatValue() : pShaderAPI->CurrentTime();
  114. vPackedVsConst1[0] = flTime;
  115. vPackedVsConst1[0] -= (float)( (int)( vPackedVsConst1[0] / 1000.0f ) ) * 1000.0f;
  116. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, vPackedVsConst1, 1 );
  117. // Set Pixel Shader Combos
  118. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  119. {
  120. DECLARE_DYNAMIC_PIXEL_SHADER( aftershock_ps20b );
  121. SET_DYNAMIC_PIXEL_SHADER( aftershock_ps20b );
  122. }
  123. else
  124. {
  125. DECLARE_DYNAMIC_PIXEL_SHADER( aftershock_ps20 );
  126. SET_DYNAMIC_PIXEL_SHADER( aftershock_ps20 );
  127. }
  128. // Bind textures
  129. pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, TEXTURE_BINDFLAGS_SRGBREAD, TEXTURE_FRAME_BUFFER_FULL_TEXTURE_0 ); // Refraction Map
  130. if ( bBumpMapping )
  131. {
  132. pShader->BindTexture( SHADER_SAMPLER1, TEXTURE_BINDFLAGS_NONE, info.m_nBumpmap, info.m_nBumpFrame );
  133. }
  134. // Set Pixel Shader Constants
  135. float vEyePos[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  136. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos );
  137. pShaderAPI->SetPixelShaderConstant( 5, vEyePos, 1 );
  138. float vPackedConst1[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  139. vPackedConst1[0] = IS_PARAM_DEFINED( info.m_nBlurAmount ) ? params[info.m_nBlurAmount]->GetFloatValue() : kDefaultBlurAmount;
  140. vPackedConst1[1] = IS_PARAM_DEFINED( info.m_nRefractAmount ) ? params[info.m_nRefractAmount]->GetFloatValue() : kDefaultRefractAmount;
  141. vPackedConst1[3] = vPackedVsConst1[0]; // Time
  142. pShaderAPI->SetPixelShaderConstant( 6, vPackedConst1, 1 );
  143. // Refract color tint
  144. pShaderAPI->SetPixelShaderConstant( 7, IS_PARAM_DEFINED( info.m_nColorTint ) ? params[info.m_nColorTint]->GetVecValue() : kDefaultColorTint, 1 );
  145. // Silhouette values
  146. float vPackedConst8[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  147. vPackedConst8[0] = IS_PARAM_DEFINED( info.m_nSilhouetteColor ) ? params[info.m_nSilhouetteColor]->GetVecValue()[0] : kDefaultSilhouetteColor[0];
  148. vPackedConst8[1] = IS_PARAM_DEFINED( info.m_nSilhouetteColor ) ? params[info.m_nSilhouetteColor]->GetVecValue()[1] : kDefaultSilhouetteColor[1];
  149. vPackedConst8[2] = IS_PARAM_DEFINED( info.m_nSilhouetteColor ) ? params[info.m_nSilhouetteColor]->GetVecValue()[2] : kDefaultSilhouetteColor[2];
  150. vPackedConst8[3] = IS_PARAM_DEFINED( info.m_nSilhouetteThickness ) ? params[info.m_nSilhouetteThickness]->GetFloatValue() : kDefaultSilhouetteThickness;
  151. pShaderAPI->SetPixelShaderConstant( 8, vPackedConst8, 1 );
  152. // Ground min/max
  153. float vPackedConst9[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  154. vPackedConst9[0] = IS_PARAM_DEFINED( info.m_nGroundMin ) ? params[info.m_nGroundMin]->GetFloatValue() : kDefaultGroundMin;
  155. vPackedConst9[1] = IS_PARAM_DEFINED( info.m_nGroundMax ) ? params[info.m_nGroundMax]->GetFloatValue() : kDefaultGroundMax;
  156. pShaderAPI->SetPixelShaderConstant( 9, vPackedConst9, 1 );
  157. // Set c0 and c1 to contain first two rows of ViewProj matrix
  158. VMatrix mView, mProj;
  159. pShaderAPI->GetMatrix( MATERIAL_VIEW, mView.m[0] );
  160. pShaderAPI->GetMatrix( MATERIAL_PROJECTION, mProj.m[0] );
  161. VMatrix mViewProj = mView * mProj;
  162. mViewProj = mViewProj.Transpose3x3();
  163. pShaderAPI->SetPixelShaderConstant( 0, mViewProj.m[0], 2 );
  164. }
  165. pShader->Draw();
  166. }