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.

172 lines
6.4 KiB

  1. //========= Copyright (c) 1996-2009, Valve Corporation, All rights reserved. ============//
  2. //
  3. //=======================================================================================//
  4. #include "BaseVSShader.h"
  5. #include "worldimposter_vs20.inc"
  6. #include "worldimposter_ps20.inc"
  7. #include "worldimposter_ps20b.inc"
  8. // memdbgon must be the last include file in a .cpp file!!!
  9. #include "tier0/memdbgon.h"
  10. BEGIN_VS_SHADER( worldimposter, "Help for worldimposter" )
  11. BEGIN_SHADER_PARAMS
  12. SHADER_PARAM( BASETEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "" )
  13. SHADER_PARAM( ALBEDO, SHADER_PARAM_TYPE_TEXTURE, "", "" )
  14. END_SHADER_PARAMS
  15. SHADER_INIT_PARAMS()
  16. {
  17. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  18. SET_FLAGS( MATERIAL_VAR_VERTEXFOG );
  19. }
  20. SHADER_FALLBACK
  21. {
  22. return 0;
  23. }
  24. SHADER_INIT
  25. {
  26. LoadTexture( BASETEXTURE, IsX360() ? 0 : TEXTUREFLAGS_SRGB );
  27. LoadTexture( ALBEDO, TEXTUREFLAGS_SRGB );
  28. }
  29. SHADER_DRAW
  30. {
  31. bool bHasFlashlight = UsingFlashlight( params );
  32. SHADOW_STATE
  33. {
  34. // Set stream format (note that this shader supports compression)
  35. int flags = VERTEX_POSITION | VERTEX_NORMAL | VERTEX_FORMAT_COMPRESSED;
  36. // NOTE: Have to say that we want 1 texcoord here even though we don't use it or you'll get this Warning in another part of the code:
  37. // "ERROR: shader asking for a too-narrow vertex format - you will see errors if running with debug D3D DLLs!\n\tPadding the vertex format with extra texcoords"
  38. int nTexCoordCount = 1;
  39. int userDataSize = 0;
  40. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, NULL, userDataSize );
  41. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  42. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  43. DECLARE_STATIC_VERTEX_SHADER( worldimposter_vs20 );
  44. SET_STATIC_VERTEX_SHADER( worldimposter_vs20 );
  45. ShadowFilterMode_t nShadowFilterMode = SHADOWFILTERMODE_DEFAULT;
  46. if ( bHasFlashlight )
  47. {
  48. nShadowFilterMode = g_pHardwareConfig->GetShadowFilterMode( true /* bForceLowQuality */ , false /* bPS30 */ ); // Based upon vendor and device dependent formats
  49. }
  50. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  51. {
  52. DECLARE_STATIC_PIXEL_SHADER( worldimposter_ps20b );
  53. SET_STATIC_PIXEL_SHADER_COMBO( FLASHLIGHTDEPTHFILTERMODE, nShadowFilterMode );
  54. SET_STATIC_PIXEL_SHADER( worldimposter_ps20b );
  55. }
  56. else
  57. {
  58. DECLARE_STATIC_PIXEL_SHADER( worldimposter_ps20 );
  59. SET_STATIC_PIXEL_SHADER( worldimposter_ps20 );
  60. }
  61. pShaderShadow->EnableSRGBWrite( true );
  62. if( bHasFlashlight )
  63. {
  64. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true ); // Shadow depth map
  65. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true ); // Noise map
  66. pShaderShadow->EnableTexture( SHADER_SAMPLER4, true ); //[sRGB] Flashlight cookie
  67. }
  68. FogToFogColor();
  69. }
  70. DYNAMIC_STATE
  71. {
  72. bool bFlashlightShadows = false;
  73. if( bHasFlashlight )
  74. {
  75. VMatrix worldToTexture;
  76. ITexture *pFlashlightDepthTexture;
  77. FlashlightState_t flashlightState = pShaderAPI->GetFlashlightStateEx( worldToTexture, &pFlashlightDepthTexture );
  78. bFlashlightShadows = flashlightState.m_bEnableShadows;
  79. BindTexture( SHADER_SAMPLER4, TEXTURE_BINDFLAGS_SRGBREAD, flashlightState.m_pSpotlightTexture, flashlightState.m_nSpotlightTextureFrame );
  80. // SetFlashLightColorFromState( state, pShaderAPI, PSREG_FLASHLIGHT_COLOR );
  81. if( pFlashlightDepthTexture && g_pConfig->ShadowDepthTexture() && bFlashlightShadows )
  82. {
  83. BindTexture( SHADER_SAMPLER2, TEXTURE_BINDFLAGS_SHADOWDEPTH, pFlashlightDepthTexture );
  84. ShaderApiFast( pShaderAPI )->BindStandardTexture( SHADER_SAMPLER3, TEXTURE_BINDFLAGS_NONE, TEXTURE_SHADOW_NOISE_2D );
  85. }
  86. float atten[4], pos[4], tweaks[4];
  87. atten[0] = flashlightState.m_fConstantAtten; // Set the flashlight attenuation factors
  88. atten[1] = flashlightState.m_fLinearAtten;
  89. atten[2] = flashlightState.m_fQuadraticAtten;
  90. atten[3] = flashlightState.m_FarZAtten;
  91. pShaderAPI->SetPixelShaderConstant( 0, atten, 1 );
  92. pos[0] = flashlightState.m_vecLightOrigin[0]; // Set the flashlight origin
  93. pos[1] = flashlightState.m_vecLightOrigin[1];
  94. pos[2] = flashlightState.m_vecLightOrigin[2];
  95. pos[3] = flashlightState.m_FarZ;
  96. pShaderAPI->SetPixelShaderConstant( 1, pos, 1 ); // steps on rim boost
  97. pShaderAPI->SetPixelShaderConstant( 2, worldToTexture.Base(), 4 );
  98. // Tweaks associated with a given flashlight
  99. tweaks[0] = ShadowFilterFromState( flashlightState );
  100. tweaks[1] = ShadowAttenFromState( flashlightState );
  101. HashShadow2DJitter( flashlightState.m_flShadowJitterSeed, &tweaks[2], &tweaks[3] );
  102. pShaderAPI->SetPixelShaderConstant( 6, tweaks, 1 );
  103. // Dimensions of screen, used for screen-space noise map sampling
  104. float vScreenScale[4] = {1280.0f / 32.0f, 720.0f / 32.0f, 0, 0};
  105. int nWidth, nHeight;
  106. pShaderAPI->GetBackBufferDimensions( nWidth, nHeight );
  107. int nTexWidth, nTexHeight;
  108. pShaderAPI->GetStandardTextureDimensions( &nTexWidth, &nTexHeight, TEXTURE_SHADOW_NOISE_2D );
  109. vScreenScale[0] = (float) nWidth / nTexWidth;
  110. vScreenScale[1] = (float) nHeight / nTexHeight;
  111. // pShaderAPI->SetPixelShaderConstant( PSREG_FLASHLIGHT_SCREEN_SCALE, vScreenScale, 1 );
  112. if ( IsX360() )
  113. {
  114. pShaderAPI->SetBooleanPixelShaderConstant( 0, &flashlightState.m_nShadowQuality, 1 );
  115. }
  116. }
  117. DECLARE_DYNAMIC_VERTEX_SHADER( worldimposter_vs20 );
  118. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, s_pShaderAPI->GetCurrentNumBones() > 0 );
  119. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, ( int )vertexCompression );
  120. SET_DYNAMIC_VERTEX_SHADER_COMBO( FLASHLIGHT, bHasFlashlight );
  121. SET_DYNAMIC_VERTEX_SHADER( worldimposter_vs20 );
  122. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  123. {
  124. DECLARE_DYNAMIC_PIXEL_SHADER( worldimposter_ps20b );
  125. SET_DYNAMIC_PIXEL_SHADER_COMBO( FLASHLIGHT, bHasFlashlight );
  126. SET_DYNAMIC_PIXEL_SHADER_COMBO( FLASHLIGHTSHADOWS, bFlashlightShadows );
  127. SET_DYNAMIC_PIXEL_SHADER( worldimposter_ps20b );
  128. }
  129. else
  130. {
  131. DECLARE_DYNAMIC_PIXEL_SHADER( worldimposter_ps20 );
  132. SET_DYNAMIC_PIXEL_SHADER_COMBO( FLASHLIGHT, bHasFlashlight );
  133. SET_DYNAMIC_PIXEL_SHADER_COMBO( FLASHLIGHTSHADOWS, bFlashlightShadows );
  134. SET_DYNAMIC_PIXEL_SHADER( worldimposter_ps20 );
  135. }
  136. BindTexture( SHADER_SAMPLER0, IsX360() ? TEXTURE_BINDFLAGS_NONE : TEXTURE_BINDFLAGS_SRGBREAD, BASETEXTURE, -1 );
  137. BindTexture( SHADER_SAMPLER1, TEXTURE_BINDFLAGS_SRGBREAD, ALBEDO, -1 );
  138. }
  139. Draw();
  140. }
  141. END_SHADER