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.

284 lines
11 KiB

  1. //========= Copyright � 1996-2006, Valve Corporation, All rights reserved. ============//
  2. #include "BaseVSShader.h"
  3. #include "mathlib/vmatrix.h"
  4. #include "portal_refract_helper.h"
  5. #include "convar.h"
  6. // Auto generated inc files
  7. #include "portal_refract_vs20.inc"
  8. #include "portal_refract_ps20.inc"
  9. #include "portal_refract_ps20b.inc"
  10. #include "shaderapifast.h"
  11. // NOTE: This has to be the last file included!
  12. #include "tier0/memdbgon.h"
  13. void InitParamsPortalRefract( CBaseVSShader *pShader, IMaterialVar** params, const char *pMaterialName, PortalRefractVars_t &info )
  14. {
  15. // Set material flags
  16. SET_FLAGS( MATERIAL_VAR_MODEL );
  17. SET_FLAGS2( MATERIAL_VAR2_NEEDS_TANGENT_SPACES );
  18. SET_FLAGS( MATERIAL_VAR_TRANSLUCENT );
  19. // Set material parameter default values
  20. if ( ( info.m_nStage != -1 ) && ( !params[info.m_nStage]->IsDefined() ) )
  21. {
  22. params[info.m_nStage]->SetIntValue( 0 );
  23. }
  24. if ( ( info.m_nStage != -1 ) && ( params[info.m_nStage]->GetIntValue() == 0 ) )
  25. {
  26. SET_FLAGS2( MATERIAL_VAR2_NEEDS_POWER_OF_TWO_FRAME_BUFFER_TEXTURE );
  27. }
  28. if ( ( info.m_nPortalOpenAmount != -1 ) && ( !params[info.m_nPortalOpenAmount]->IsDefined() ) )
  29. {
  30. params[info.m_nPortalOpenAmount]->SetFloatValue( kDefaultPortalOpenAmount );
  31. }
  32. if ( ( info.m_nPortalStatic != -1 ) && ( !params[info.m_nPortalStatic]->IsDefined() ) )
  33. {
  34. params[info.m_nPortalStatic]->SetFloatValue( kDefaultPortalStatic );
  35. }
  36. SET_PARAM_FLOAT_IF_NOT_DEFINED( info.m_nTime, 0.0f );
  37. SET_PARAM_INT_IF_NOT_DEFINED( info.m_nUseOnStaticProp, 0 );
  38. }
  39. void InitPortalRefract( CBaseVSShader *pShader, IMaterialVar** params, PortalRefractVars_t &info )
  40. {
  41. int nStage = IS_PARAM_DEFINED( info.m_nStage ) ? params[info.m_nStage]->GetIntValue() : 0;
  42. // Load textures
  43. if ( nStage == 2 ) // Only load textures for third stage
  44. {
  45. if ( (info.m_nPortalMaskTexture != -1) && params[info.m_nPortalMaskTexture]->IsDefined() )
  46. {
  47. pShader->LoadTexture( info.m_nPortalMaskTexture );
  48. }
  49. if ( (info.m_nPortalColorTexture != -1) && params[info.m_nPortalColorTexture]->IsDefined() )
  50. {
  51. pShader->LoadTexture( info.m_nPortalColorTexture, TEXTUREFLAGS_SRGB );
  52. }
  53. }
  54. }
  55. void DrawPortalRefract( CBaseVSShader *pShader, IMaterialVar** params,
  56. IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow, PortalRefractVars_t &info, VertexCompressionType_t vertexCompression )
  57. {
  58. int nStage = IS_PARAM_DEFINED( info.m_nStage ) ? params[info.m_nStage]->GetIntValue() : 0;
  59. int nTinted = ((nStage == 2) && !IS_PARAM_DEFINED( info.m_nPortalColorTexture )) ? 1 : 0; //only in stage 2 with no color texture defined
  60. // On Leopard / 10.5.8, we can't do an sRGB read from a render target, so we must fake it in shader code
  61. bool bShaderSRGBRead = false;
  62. bool bUseOnStaticProp = IS_PARAM_DEFINED( info.m_nUseOnStaticProp ) ? ( params[info.m_nUseOnStaticProp]->GetIntValue() != 0 ): 0;
  63. if ( bUseOnStaticProp )
  64. {
  65. nStage = 2;
  66. }
  67. SHADOW_STATE
  68. {
  69. // Set stream format
  70. if ( !bUseOnStaticProp )
  71. {
  72. pShaderShadow->VertexShaderVertexFormat( VERTEX_POSITION | VERTEX_NORMAL, 2, NULL, 4 );
  73. }
  74. else
  75. {
  76. pShaderShadow->VertexShaderVertexFormat( VERTEX_POSITION | VERTEX_FORMAT_COMPRESSED, 1, NULL, 0 );
  77. }
  78. // Vertex Shader
  79. DECLARE_STATIC_VERTEX_SHADER( portal_refract_vs20 );
  80. SET_STATIC_VERTEX_SHADER_COMBO( STAGE, nStage );
  81. SET_STATIC_VERTEX_SHADER_COMBO( USEONSTATICPROP, bUseOnStaticProp );
  82. SET_STATIC_VERTEX_SHADER( portal_refract_vs20 );
  83. // On Leopard / 10.5.8, we can't do an sRGB read from a render target, so we must fake it in shader code
  84. bool bShaderSRGBRead = false;
  85. // Pixel Shader
  86. if( g_pHardwareConfig->SupportsPixelShaders_2_b() || g_pHardwareConfig->ShouldAlwaysUseShaderModel2bShaders() ) // Always send OpenGL / Posix down this path
  87. {
  88. DECLARE_STATIC_PIXEL_SHADER( portal_refract_ps20b );
  89. SET_STATIC_PIXEL_SHADER_COMBO( STAGE, nStage );
  90. SET_STATIC_PIXEL_SHADER_COMBO( TINTED, nTinted );
  91. SET_STATIC_PIXEL_SHADER_COMBO( SHADER_SRGB_READ, bShaderSRGBRead ? 1 : 0 );
  92. SET_STATIC_PIXEL_SHADER( portal_refract_ps20b );
  93. }
  94. else
  95. {
  96. DECLARE_STATIC_PIXEL_SHADER( portal_refract_ps20 );
  97. SET_STATIC_PIXEL_SHADER_COMBO( STAGE, nStage );
  98. SET_STATIC_PIXEL_SHADER_COMBO( TINTED, nTinted );
  99. SET_STATIC_PIXEL_SHADER( portal_refract_ps20 );
  100. }
  101. // Textures
  102. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true ); // Refraction texture
  103. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, !bShaderSRGBRead && !IsX360() );
  104. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true ); // Noise
  105. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, false ); // Not sRGB
  106. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true ); // Color
  107. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER2, true ); // sRGB
  108. pShaderShadow->EnableSRGBWrite( true ); //Stage 1 doesn't actually need srgb conversion, but it skips the software srgb solution anyway.
  109. // Sampler for nvidia's stereo hackery
  110. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  111. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER3, false );
  112. // Enable alpha testing for all stages
  113. pShaderShadow->EnableAlphaTest( true );
  114. pShaderShadow->AlphaFunc( SHADER_ALPHAFUNC_GREATER, 0.5f );
  115. // Enable alpha blending for stage 2
  116. if ( nStage == 2 )
  117. {
  118. pShader->EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  119. pShaderShadow->AlphaFunc( SHADER_ALPHAFUNC_GREATER, 1.0f/255.0f );
  120. }
  121. // Disable z-writes for all passes
  122. if ( nStage != 1 )
  123. {
  124. pShaderShadow->EnableDepthWrites( false );
  125. }
  126. else
  127. {
  128. pShaderShadow->EnableDepthWrites( true );
  129. }
  130. // Disable alpha-writes for all passes
  131. pShaderShadow->EnableAlphaWrites( false );
  132. pShaderShadow->EnablePolyOffset( SHADER_POLYOFFSET_DECAL );
  133. if( nStage == 2 )
  134. {
  135. pShader->DefaultFog();
  136. }
  137. }
  138. DYNAMIC_STATE
  139. {
  140. // Set Vertex Shader Combos
  141. DECLARE_DYNAMIC_VERTEX_SHADER( portal_refract_vs20 );
  142. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  143. SET_DYNAMIC_VERTEX_SHADER( portal_refract_vs20 );
  144. // Set Vertex Shader Constants
  145. if ( IS_PARAM_DEFINED( info.m_nTextureTransform ) )
  146. {
  147. pShader->SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_1, info.m_nTextureTransform );
  148. }
  149. // Time % 1000
  150. float vPackedVsConst1[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  151. float flTime = IS_PARAM_DEFINED( info.m_nTime ) && params[info.m_nTime]->GetFloatValue() > 0.0f ? params[info.m_nTime]->GetFloatValue() : ShaderApiFast( pShaderAPI )->CurrentTime();
  152. vPackedVsConst1[0] = flTime;
  153. vPackedVsConst1[0] -= (float)( floor( vPackedVsConst1[0] / 1000.0f ) ) * 1000.0f;
  154. ShaderApiFast( pShaderAPI )->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, vPackedVsConst1, 1 );
  155. bool bNvidiaStereoActiveThisFrame = pShaderAPI->IsStereoActiveThisFrame();
  156. if ( bNvidiaStereoActiveThisFrame )
  157. {
  158. pShaderAPI->BindStandardTexture( SHADER_SAMPLER3, TEXTURE_BINDFLAGS_NONE, TEXTURE_STEREO_PARAM_MAP );
  159. }
  160. // Set Pixel Shader Combos
  161. if( g_pHardwareConfig->SupportsPixelShaders_2_b() || g_pHardwareConfig->ShouldAlwaysUseShaderModel2bShaders() ) // Always send OpenGL / Posix down this path
  162. {
  163. DECLARE_DYNAMIC_PIXEL_SHADER( portal_refract_ps20b );
  164. SET_DYNAMIC_PIXEL_SHADER_COMBO( D_NVIDIA_STEREO, bNvidiaStereoActiveThisFrame );
  165. SET_DYNAMIC_PIXEL_SHADER( portal_refract_ps20b );
  166. }
  167. else
  168. {
  169. DECLARE_DYNAMIC_PIXEL_SHADER( portal_refract_ps20 );
  170. SET_DYNAMIC_PIXEL_SHADER( portal_refract_ps20 );
  171. }
  172. // Bind textures
  173. if ( nStage == 0 ) // Only bind frame buffer texture for first stage
  174. {
  175. ShaderApiFast( pShaderAPI )->BindStandardTexture( SHADER_SAMPLER0, ( !bShaderSRGBRead && !IsX360() ) ? TEXTURE_BINDFLAGS_SRGBREAD : TEXTURE_BINDFLAGS_NONE, TEXTURE_FRAME_BUFFER_FULL_TEXTURE_0 ); // Refraction Map
  176. }
  177. else if ( nStage == 2 ) // Only load textures for third stage
  178. {
  179. pShader->BindTexture( SHADER_SAMPLER1, TEXTURE_BINDFLAGS_NONE, info.m_nPortalMaskTexture );
  180. if ( nTinted == 0 )
  181. {
  182. pShader->BindTexture( SHADER_SAMPLER2, TEXTURE_BINDFLAGS_SRGBREAD, info.m_nPortalColorTexture );
  183. }
  184. }
  185. // Set Pixel Shader Constants
  186. ShaderApiFast( pShaderAPI )->SetPixelShaderFogParams( 6 );
  187. // Set c0-c3 to contain four rows of ViewProj matrix
  188. VMatrix mView, mProj;
  189. ShaderApiFast( pShaderAPI )->GetMatrix( MATERIAL_VIEW, mView.m[0] );
  190. ShaderApiFast( pShaderAPI )->GetMatrix( MATERIAL_PROJECTION, mProj.m[0] );
  191. VMatrix mViewProj = mView * mProj;
  192. mViewProj = mViewProj.Transpose();
  193. ShaderApiFast( pShaderAPI )->SetPixelShaderConstant( 0, mViewProj.m[0], 4 );
  194. // Portal open amount
  195. float vPackedConst4[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  196. vPackedConst4[0] = ( IS_PARAM_DEFINED( info.m_nPortalOpenAmount ) ? params[info.m_nPortalOpenAmount]->GetFloatValue() : kDefaultPortalOpenAmount );
  197. vPackedConst4[1] = 1.0f - ( IS_PARAM_DEFINED( info.m_nPortalStatic ) ? params[info.m_nPortalStatic]->GetFloatValue() : kDefaultPortalStatic );
  198. vPackedConst4[2] = ( IS_PARAM_DEFINED( info.m_nPortalColorScale ) ? params[info.m_nPortalColorScale]->GetFloatValue() : kDefaultPortalColorScale );
  199. //vPackedConst4[0] = 0.6f;
  200. //DevMsg( "Refract Time: %f\n", vPackedConst4[0] );
  201. ShaderApiFast( pShaderAPI )->SetPixelShaderConstant( 4, vPackedConst4, 1 );
  202. ShaderApiFast( pShaderAPI )->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_3, vPackedConst4, 1 );
  203. // Camera position
  204. float vEyePos[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  205. ShaderApiFast( pShaderAPI )->GetWorldSpaceCameraPosition( vEyePos );
  206. ShaderApiFast( pShaderAPI )->SetPixelShaderConstant( 5, vEyePos, 1 );
  207. // Tinting
  208. if ( nTinted )
  209. {
  210. ShaderApiFast( pShaderAPI )->SetPixelShaderConstant( 7, IS_PARAM_DEFINED( info.m_nPortalColorGradientDark ) ? params[info.m_nPortalColorGradientDark]->GetVecValue() : kDefaultPortalColorGradientDark, 1 );
  211. ShaderApiFast( pShaderAPI )->SetPixelShaderConstant( 8, IS_PARAM_DEFINED( info.m_nPortalColorGradientLight ) ? params[info.m_nPortalColorGradientLight]->GetVecValue() : kDefaultPortalColorGradientLight, 1 );
  212. }
  213. // Get viewport and render target dimensions and set shader constant to do a 2D mad
  214. int nViewportX, nViewportY, nViewportWidth, nViewportHeight;
  215. pShaderAPI->GetCurrentViewport( nViewportX, nViewportY, nViewportWidth, nViewportHeight );
  216. int nRtWidth, nRtHeight;
  217. pShaderAPI->GetCurrentRenderTargetDimensions( nRtWidth, nRtHeight );
  218. float vViewportMad[4] = { 1.0f, 1.0f, 0.0f, 0.0f };
  219. vViewportMad[0] = ( float )nViewportWidth / ( float )nRtWidth;
  220. vViewportMad[1] = ( float )nViewportHeight / ( float )nRtHeight;
  221. vViewportMad[2] = ( float )nViewportX / ( float )nRtWidth;
  222. vViewportMad[3] = ( float )nViewportY / ( float )nRtHeight;
  223. ShaderApiFast( pShaderAPI )->SetPixelShaderConstant( 9, vViewportMad, 1 );
  224. int nPortalRecursionDepth = ShaderApiFast( pShaderAPI )->GetIntRenderingParameter( INT_RENDERPARM_PORTAL_RECURSION_DEPTH );
  225. if( g_pHardwareConfig->UseFastClipping() )
  226. {
  227. vPackedConst4[0] = ( nPortalRecursionDepth > 0 ) ? 1.0f : 0.0f;
  228. }
  229. else
  230. {
  231. vPackedConst4[0] = ( nPortalRecursionDepth > 1 )? 1.0f : 0.0f;
  232. }
  233. vPackedConst4[1] = 0.0f;
  234. vPackedConst4[2] = 0.0f;
  235. vPackedConst4[3] = 0.0f;
  236. ShaderApiFast( pShaderAPI )->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_4, vPackedConst4, 1 );
  237. }
  238. pShader->Draw();
  239. }