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.

192 lines
5.9 KiB

  1. //========= Copyright (c) Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "BaseVSShader.h"
  8. #include "mathlib/vmatrix.h"
  9. #include "common_hlsl_cpp_consts.h" // hack hack hack!
  10. #include "convar.h"
  11. #include "unlitworld_screensample_vs20.inc"
  12. #include "unlitworld_screensample_ps20.inc"
  13. #include "unlitworld_screensample_ps20b.inc"
  14. #include "shaderlib/commandbuilder.h"
  15. // NOTE: This has to be the last file included!
  16. #include "tier0/memdbgon.h"
  17. BEGIN_VS_SHADER( UnLitWorld_ScreenSample, "Help for UnLitWorld_ScreenSample" )
  18. BEGIN_SHADER_PARAMS
  19. //always required
  20. SHADER_PARAM( BASETEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "" )
  21. END_SHADER_PARAMS
  22. SHADER_INIT_PARAMS()
  23. {
  24. SET_PARAM_STRING_IF_NOT_DEFINED( BASETEXTURE, "Dev/flat_normal" );
  25. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  26. }
  27. SHADER_FALLBACK
  28. {
  29. return 0;
  30. }
  31. SHADER_INIT
  32. {
  33. if ( params[BASETEXTURE]->IsDefined() ) { LoadTexture( BASETEXTURE ); }
  34. }
  35. inline void DrawUnLitWorld_ScreenSample( IMaterialVar **params, IShaderShadow* pShaderShadow, IShaderDynamicAPI* pShaderAPI, int vertexCompression )
  36. {
  37. SHADOW_STATE
  38. {
  39. SetInitialShadowState( );
  40. if ( params[BASETEXTURE]->IsDefined() ) { pShaderShadow->EnableTexture( SHADER_SAMPLER0, true ); }
  41. if ( IS_FLAG_SET(MATERIAL_VAR_ADDITIVE) )
  42. {
  43. EnableAlphaBlending( SHADER_BLEND_ONE, SHADER_BLEND_ONE );
  44. }
  45. else
  46. {
  47. EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  48. }
  49. //pShaderShadow->EnableBlending( true );
  50. //pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  51. //pShaderShadow->AlphaFunc( SHADER_ALPHAFUNC_ALWAYS, 0.0f );
  52. /*
  53. BlendFunc( SHADER_BLEND_ZERO, SHADER_BLEND_ZERO );
  54. BlendOp( SHADER_BLEND_OP_ADD );
  55. EnableBlendingSeparateAlpha( false );
  56. BlendFuncSeparateAlpha( SHADER_BLEND_ZERO, SHADER_BLEND_ZERO );
  57. BlendOpSeparateAlpha( SHADER_BLEND_OP_ADD );
  58. AlphaFunc( SHADER_ALPHAFUNC_GEQUAL, 0.7f );
  59. */
  60. unsigned int flags = VERTEX_POSITION| VERTEX_FORMAT_COMPRESSED;
  61. flags |= VERTEX_NORMAL;
  62. int nTexCoordCount = 1;
  63. int userDataSize = 0;
  64. if (IS_FLAG_SET( MATERIAL_VAR_VERTEXCOLOR ))
  65. {
  66. flags |= VERTEX_COLOR;
  67. }
  68. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, NULL, userDataSize );
  69. DECLARE_STATIC_VERTEX_SHADER( unlitworld_screensample_vs20 );
  70. SET_STATIC_VERTEX_SHADER( unlitworld_screensample_vs20 );
  71. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  72. {
  73. DECLARE_STATIC_PIXEL_SHADER( unlitworld_screensample_ps20b );
  74. SET_STATIC_PIXEL_SHADER( unlitworld_screensample_ps20b );
  75. }
  76. else
  77. {
  78. DECLARE_STATIC_PIXEL_SHADER( unlitworld_screensample_ps20 );
  79. SET_STATIC_PIXEL_SHADER( unlitworld_screensample_ps20 );
  80. }
  81. //pShaderShadow->EnableAlphaWrites( true );
  82. //pShaderShadow->EnableDepthWrites( true );
  83. pShaderShadow->EnableSRGBWrite( true );
  84. pShaderShadow->EnableBlending( true );
  85. //pShaderShadow->EnableAlphaTest( true );
  86. PI_BeginCommandBuffer();
  87. PI_SetModulationPixelShaderDynamicState_LinearColorSpace( 1 );
  88. PI_EndCommandBuffer();
  89. }
  90. DYNAMIC_STATE
  91. {
  92. pShaderAPI->SetDefaultState();
  93. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, BASETEXTURETRANSFORM );
  94. if ( params[BASETEXTURE]->IsDefined() ) { BindTexture( SHADER_SAMPLER0, TEXTURE_BINDFLAGS_SRGBREAD, BASETEXTURE, -1 ); }
  95. // Get viewport and render target dimensions and set shader constant to do a 2D mad
  96. int nViewportX, nViewportY, nViewportWidth, nViewportHeight;
  97. pShaderAPI->GetCurrentViewport( nViewportX, nViewportY, nViewportWidth, nViewportHeight );
  98. int nRtWidth, nRtHeight;
  99. pShaderAPI->GetCurrentRenderTargetDimensions( nRtWidth, nRtHeight );
  100. // Compute viewport mad that takes projection space coords (post divide by W) into normalized screenspace, taking into account the currently set viewport.
  101. float vViewportMad[4];
  102. vViewportMad[0] = .5f * ( ( float )nViewportWidth / ( float )nRtWidth );
  103. vViewportMad[1] = -.5f * ( ( float )nViewportHeight / ( float )nRtHeight );
  104. vViewportMad[2] = vViewportMad[0] + ( ( float )nViewportX / ( float )nRtWidth );
  105. vViewportMad[3] = -vViewportMad[1] + ( ( float )nViewportY / ( float )nRtHeight );
  106. pShaderAPI->SetPixelShaderConstant( 17, vViewportMad, 1 );
  107. float c18[4];
  108. pShaderAPI->GetWorldSpaceCameraPosition( c18 );
  109. c18[3] = 0;
  110. pShaderAPI->SetPixelShaderConstant( 18, c18, 1 );
  111. float c0[4] = { 1.0f / (float)MAX(nViewportWidth, 1.0f) , // one X pixel in normalized 0..1 screenspace
  112. 1.0f / (float)MAX(nViewportHeight, 1.0f) , // one Y pixel in normalized 0..1 screenspace
  113. 0,
  114. 0 };
  115. pShaderAPI->SetPixelShaderConstant( 0, c0, 1 );
  116. DECLARE_DYNAMIC_VERTEX_SHADER( unlitworld_screensample_vs20 );
  117. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  118. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  119. SET_DYNAMIC_VERTEX_SHADER( unlitworld_screensample_vs20 );
  120. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  121. {
  122. DECLARE_DYNAMIC_PIXEL_SHADER( unlitworld_screensample_ps20b );
  123. SET_DYNAMIC_PIXEL_SHADER( unlitworld_screensample_ps20b );
  124. }
  125. else
  126. {
  127. DECLARE_DYNAMIC_PIXEL_SHADER( unlitworld_screensample_ps20 );
  128. SET_DYNAMIC_PIXEL_SHADER( unlitworld_screensample_ps20 );
  129. }
  130. }
  131. Draw();
  132. }
  133. SHADER_DRAW
  134. {
  135. DrawUnLitWorld_ScreenSample( params, pShaderShadow, pShaderAPI, vertexCompression );
  136. }
  137. //void ExecuteFastPath( int *dynVSIdx, int *dynPSIdx, IMaterialVar** params, IShaderDynamicAPI * pShaderAPI,
  138. // VertexCompressionType_t vertexCompression, CBasePerMaterialContextData **pContextDataPtr, BOOL bCSMEnabled )
  139. //{
  140. // DrawUnLitWorld_ScreenSample( params, pShaderShadow, pShaderAPI, vertexCompression );
  141. //}
  142. bool IsTranslucent( IMaterialVar **params ) const
  143. {
  144. return false;
  145. }
  146. END_SHADER