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.

218 lines
7.2 KiB

  1. //========= Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: A shader that builds the shadow using render-to-texture
  4. //
  5. // $Header: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "BaseVSShader.h"
  9. #include "convar.h"
  10. #include "mathlib/vmatrix.h"
  11. #include "unlitgeneric_vs20.inc"
  12. #include "shadowbuildtexture_ps20.inc"
  13. #include "shadowbuildtexture_ps20b.inc"
  14. #if !defined( _X360 ) && !defined( _PS3 )
  15. #include "shadowbuildtexture_ps30.inc"
  16. #include "unlitgeneric_vs30.inc"
  17. #endif
  18. // memdbgon must be the last include file in a .cpp file!!!
  19. #include "tier0/memdbgon.h"
  20. static ConVar mat_displacementmap( "mat_displacementmap", "1", FCVAR_CHEAT );
  21. DEFINE_FALLBACK_SHADER( ShadowBuild, ShadowBuild_DX9 )
  22. BEGIN_VS_SHADER_FLAGS( ShadowBuild_DX9, "Help for ShadowBuild", SHADER_NOT_EDITABLE )
  23. BEGIN_SHADER_PARAMS
  24. SHADER_PARAM( TRANSLUCENT_MATERIAL, SHADER_PARAM_TYPE_MATERIAL, "", "Points to a material to grab translucency from" )
  25. END_SHADER_PARAMS
  26. SHADER_INIT_PARAMS()
  27. {
  28. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  29. SET_FLAGS( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  30. }
  31. SHADER_FALLBACK
  32. {
  33. return 0;
  34. }
  35. SHADER_INIT
  36. {
  37. if (params[BASETEXTURE]->IsDefined())
  38. {
  39. LoadTexture( BASETEXTURE, TEXTUREFLAGS_SRGB );
  40. }
  41. }
  42. SHADER_DRAW
  43. {
  44. bool bHDR = g_pHardwareConfig->GetHDRType() != HDR_TYPE_NONE;
  45. // Snack important parameters from the original material
  46. // FIXME: What about alpha modulation? Need a solution for that
  47. ITexture *pTexture = NULL;
  48. IMaterialVar **ppTranslucentParams = NULL;
  49. if (params[TRANSLUCENT_MATERIAL]->IsDefined())
  50. {
  51. IMaterial *pMaterial = params[TRANSLUCENT_MATERIAL]->GetMaterialValue();
  52. if (pMaterial)
  53. {
  54. ppTranslucentParams = pMaterial->GetShaderParams();
  55. if ( ppTranslucentParams[BASETEXTURE]->IsTexture() )
  56. {
  57. pTexture = ppTranslucentParams[BASETEXTURE]->GetTextureValue();
  58. }
  59. }
  60. }
  61. SHADOW_STATE
  62. {
  63. // Add the alphas into the frame buffer
  64. EnableAlphaBlending( SHADER_BLEND_ONE, SHADER_BLEND_ONE );
  65. // Base texture. We just use this for alpha, but enable SRGB read to make everything consistent.
  66. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  67. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, pTexture || !bHDR );
  68. pShaderShadow->EnableSRGBWrite( true );
  69. pShaderShadow->EnableAlphaWrites( true );
  70. pShaderShadow->EnableDepthWrites( false );
  71. // pShaderShadow->DepthFunc( SHADER_DEPTHFUNC_ALWAYS );
  72. pShaderShadow->EnableDepthTest( false );
  73. #if defined( _PS3 )
  74. pShaderShadow->EnableDepthTest( false );
  75. #else
  76. #endif
  77. // Specify vertex format (note that this shader supports compression)
  78. unsigned int flags = VERTEX_POSITION | VERTEX_FORMAT_COMPRESSED;
  79. unsigned int nTexCoordCount = 1;
  80. unsigned int userDataSize = 0;
  81. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, NULL, userDataSize );
  82. #if !defined( _X360 ) && !defined( _PS3 )
  83. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  84. #endif
  85. {
  86. DECLARE_STATIC_VERTEX_SHADER( unlitgeneric_vs20 );
  87. SET_STATIC_VERTEX_SHADER_COMBO( VERTEXCOLOR, 0 );
  88. SET_STATIC_VERTEX_SHADER( unlitgeneric_vs20 );
  89. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  90. {
  91. DECLARE_STATIC_PIXEL_SHADER( shadowbuildtexture_ps20b );
  92. SET_STATIC_PIXEL_SHADER( shadowbuildtexture_ps20b );
  93. }
  94. else
  95. {
  96. DECLARE_STATIC_PIXEL_SHADER( shadowbuildtexture_ps20 );
  97. SET_STATIC_PIXEL_SHADER( shadowbuildtexture_ps20 );
  98. }
  99. }
  100. #if !defined( _X360 ) && !defined( _PS3 )
  101. else
  102. {
  103. SET_FLAGS2( MATERIAL_VAR2_USES_VERTEXID );
  104. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_TESSELLATION );
  105. DECLARE_STATIC_VERTEX_SHADER( unlitgeneric_vs30 );
  106. SET_STATIC_VERTEX_SHADER_COMBO( VERTEXCOLOR, 0 );
  107. SET_STATIC_VERTEX_SHADER( unlitgeneric_vs30 );
  108. DECLARE_STATIC_PIXEL_SHADER( shadowbuildtexture_ps30 );
  109. SET_STATIC_PIXEL_SHADER( shadowbuildtexture_ps30 );
  110. }
  111. #endif
  112. PI_BeginCommandBuffer();
  113. PI_SetModulationVertexShaderDynamicState();
  114. PI_EndCommandBuffer();
  115. }
  116. DYNAMIC_STATE
  117. {
  118. if ( pTexture )
  119. {
  120. BindTexture( SHADER_SAMPLER0, TEXTURE_BINDFLAGS_SRGBREAD, pTexture, ppTranslucentParams[FRAME]->GetIntValue() );
  121. Vector4D transformation[2];
  122. const VMatrix &mat = ppTranslucentParams[BASETEXTURETRANSFORM]->GetMatrixValue();
  123. transformation[0].Init( mat[0][0], mat[0][1], mat[0][2], mat[0][3] );
  124. transformation[1].Init( mat[1][0], mat[1][1], mat[1][2], mat[1][3] );
  125. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, transformation[0].Base(), 2 );
  126. }
  127. else
  128. {
  129. pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, bHDR ? TEXTURE_BINDFLAGS_NONE : TEXTURE_BINDFLAGS_SRGBREAD, TEXTURE_LIGHTMAP_FULLBRIGHT );
  130. }
  131. #if !defined( _X360 ) && !defined( _PS3 )
  132. TessellationMode_t nTessellationMode = TESSELLATION_MODE_DISABLED;
  133. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  134. #endif
  135. {
  136. // Compute the vertex shader index.
  137. DECLARE_DYNAMIC_VERTEX_SHADER( unlitgeneric_vs20 );
  138. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  139. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  140. SET_DYNAMIC_VERTEX_SHADER_COMBO( TESSELLATION, 0 );
  141. SET_DYNAMIC_VERTEX_SHADER( unlitgeneric_vs20 );
  142. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  143. {
  144. DECLARE_DYNAMIC_PIXEL_SHADER( shadowbuildtexture_ps20b );
  145. SET_DYNAMIC_PIXEL_SHADER( shadowbuildtexture_ps20b );
  146. }
  147. else
  148. {
  149. DECLARE_DYNAMIC_PIXEL_SHADER( shadowbuildtexture_ps20 );
  150. SET_DYNAMIC_PIXEL_SHADER( shadowbuildtexture_ps20 );
  151. }
  152. }
  153. #if !defined( _X360 ) && !defined( _PS3 )
  154. else
  155. {
  156. nTessellationMode = pShaderAPI->GetTessellationMode();
  157. if ( nTessellationMode != TESSELLATION_MODE_DISABLED )
  158. {
  159. pShaderAPI->BindStandardVertexTexture( SHADER_VERTEXTEXTURE_SAMPLER1, TEXTURE_SUBDIVISION_PATCHES );
  160. bool bHasDisplacement = false; // TODO
  161. float vSubDDimensions[4] = { 1.0f/pShaderAPI->GetSubDHeight(), bHasDisplacement && mat_displacementmap.GetBool() ? 1.0f : 0.0f, 0.0f, 0.0f };
  162. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_8, vSubDDimensions );
  163. // JasonM - revisit this later...requires plumbing in a separate vertex texture param type??
  164. // bool bHasDisplacement = (info.m_nDisplacementMap != -1) && params[info.m_nDisplacementMap]->IsTexture();
  165. // if( bHasDisplacement )
  166. // {
  167. // pShader->BindVertexTexture( SHADER_VERTEXTEXTURE_SAMPLER2, info.m_nDisplacementMap );
  168. // }
  169. // else
  170. // {
  171. // pShaderAPI->BindStandardVertexTexture( SHADER_VERTEXTEXTURE_SAMPLER2, VERTEX_TEXTURE_BLACK );
  172. // }
  173. }
  174. // Compute the vertex shader index.
  175. DECLARE_DYNAMIC_VERTEX_SHADER( unlitgeneric_vs30 );
  176. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  177. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  178. SET_DYNAMIC_VERTEX_SHADER_COMBO( TESSELLATION, nTessellationMode );
  179. SET_DYNAMIC_VERTEX_SHADER( unlitgeneric_vs30 );
  180. DECLARE_DYNAMIC_PIXEL_SHADER( shadowbuildtexture_ps30 );
  181. SET_DYNAMIC_PIXEL_SHADER( shadowbuildtexture_ps30 );
  182. }
  183. #endif
  184. }
  185. Draw( );
  186. }
  187. END_SHADER