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.

279 lines
9.3 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Lightmap only shader
  4. //
  5. // $Header: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "BaseVSShader.h"
  9. #include "ShatteredGlass_ps20.inc"
  10. #include "ShatteredGlass_ps20b.inc"
  11. #include "ShatteredGlass_vs20.inc"
  12. // NOTE: This has to be the last file included!
  13. #include "tier0/memdbgon.h"
  14. BEGIN_VS_SHADER( ShatteredGlass,
  15. "Help for ShatteredGlass" )
  16. BEGIN_SHADER_PARAMS
  17. SHADER_PARAM_OVERRIDE( BASETEXTURE, SHADER_PARAM_TYPE_TEXTURE, "Glass/glasswindowbreak070b", "unused", SHADER_PARAM_NOT_EDITABLE )
  18. SHADER_PARAM( DETAIL, SHADER_PARAM_TYPE_TEXTURE, "Glass/glasswindowbreak070b", "detail" )
  19. SHADER_PARAM( DETAILSCALE, SHADER_PARAM_TYPE_FLOAT, "1.0", "detail scale" )
  20. SHADER_PARAM( ENVMAP, SHADER_PARAM_TYPE_TEXTURE, "shadertest/shadertest_env", "envmap" )
  21. SHADER_PARAM( ENVMAPFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  22. SHADER_PARAM( ENVMAPMASK, SHADER_PARAM_TYPE_TEXTURE, "glass/glasswindowbreak070b_mask", "envmap mask" )
  23. SHADER_PARAM( ENVMAPMASKFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  24. SHADER_PARAM( ENVMAPMASKTRANSFORM, SHADER_PARAM_TYPE_MATRIX, "center .5 .5 scale 1 1 rotate 0 translate 0 0", "$envmapmask texcoord transform" )
  25. SHADER_PARAM( ENVMAPTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "envmap tint" )
  26. SHADER_PARAM( ENVMAPCONTRAST, SHADER_PARAM_TYPE_FLOAT, "0.0", "contrast 0 == normal 1 == color*color" )
  27. SHADER_PARAM( ENVMAPSATURATION, SHADER_PARAM_TYPE_FLOAT, "1.0", "saturation 0 == greyscale 1 == normal" )
  28. SHADER_PARAM( FRESNELREFLECTION, SHADER_PARAM_TYPE_FLOAT, "1.0", "1.0 == mirror, 0.0 == water" )
  29. SHADER_PARAM( UNLITFACTOR, SHADER_PARAM_TYPE_FLOAT, "0.7", "0.0 == multiply by lightmap, 1.0 == multiply by 1" )
  30. END_SHADER_PARAMS
  31. SHADER_INIT_PARAMS()
  32. {
  33. if( !params[DETAILSCALE]->IsDefined() )
  34. params[DETAILSCALE]->SetFloatValue( 1.0f );
  35. if( !params[ENVMAPTINT]->IsDefined() )
  36. params[ENVMAPTINT]->SetVecValue( 1.0f, 1.0f, 1.0f );
  37. if( !params[ENVMAPCONTRAST]->IsDefined() )
  38. params[ENVMAPCONTRAST]->SetFloatValue( 0.0f );
  39. if( !params[ENVMAPSATURATION]->IsDefined() )
  40. params[ENVMAPSATURATION]->SetFloatValue( 1.0f );
  41. if( !params[UNLITFACTOR]->IsDefined() )
  42. params[UNLITFACTOR]->SetFloatValue( 0.3f );
  43. if( !params[FRESNELREFLECTION]->IsDefined() )
  44. params[FRESNELREFLECTION]->SetFloatValue( 1.0f );
  45. if( !params[ENVMAPMASKFRAME]->IsDefined() )
  46. params[ENVMAPMASKFRAME]->SetIntValue( 0 );
  47. if( !params[ENVMAPFRAME]->IsDefined() )
  48. params[ENVMAPFRAME]->SetIntValue( 0 );
  49. // No texture means no self-illum or env mask in base alpha
  50. if ( !params[BASETEXTURE]->IsDefined() )
  51. {
  52. CLEAR_FLAGS( MATERIAL_VAR_BASEALPHAENVMAPMASK );
  53. }
  54. // If in decal mode, no debug override...
  55. if (IS_FLAG_SET(MATERIAL_VAR_DECAL))
  56. {
  57. SET_FLAGS( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  58. }
  59. }
  60. SHADER_FALLBACK
  61. {
  62. return 0;
  63. }
  64. SHADER_INIT
  65. {
  66. if (params[BASETEXTURE]->IsDefined())
  67. {
  68. LoadTexture( BASETEXTURE, TEXTUREFLAGS_SRGB );
  69. if ( !params[BASETEXTURE]->GetTextureValue()->IsTranslucent() )
  70. {
  71. if ( IS_FLAG_SET( MATERIAL_VAR_BASEALPHAENVMAPMASK ) )
  72. CLEAR_FLAGS( MATERIAL_VAR_BASEALPHAENVMAPMASK );
  73. }
  74. }
  75. if ( params[DETAIL]->IsDefined() )
  76. {
  77. LoadTexture( DETAIL, TEXTUREFLAGS_SRGB );
  78. }
  79. // Don't alpha test if the alpha channel is used for other purposes
  80. if ( IS_FLAG_SET(MATERIAL_VAR_BASEALPHAENVMAPMASK) )
  81. CLEAR_FLAGS( MATERIAL_VAR_ALPHATEST );
  82. if (params[ENVMAP]->IsDefined())
  83. {
  84. LoadCubeMap( ENVMAP );
  85. if ( params[ENVMAPMASK]->IsDefined() )
  86. {
  87. LoadTexture( ENVMAPMASK, g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE ? TEXTUREFLAGS_SRGB : 0 );
  88. }
  89. }
  90. }
  91. SHADER_DRAW
  92. {
  93. bool bHasEnvmapMask = false;
  94. bool bHasEnvmap = false;
  95. if ( params[ENVMAP]->IsTexture() )
  96. {
  97. bHasEnvmap = true;
  98. if ( params[ENVMAPMASK]->IsTexture() )
  99. {
  100. bHasEnvmapMask = true;
  101. }
  102. }
  103. bool bHasVertexColor = IS_FLAG_SET(MATERIAL_VAR_VERTEXCOLOR);
  104. bool bHasBaseAlphaEnvmapMask = IS_FLAG_SET(MATERIAL_VAR_BASEALPHAENVMAPMASK);
  105. TextureBindFlags_t nLightmapBindFlags = ( g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE ) ? TEXTURE_BINDFLAGS_SRGBREAD : TEXTURE_BINDFLAGS_NONE;
  106. // Base
  107. SHADOW_STATE
  108. {
  109. // alpha test
  110. pShaderShadow->EnableAlphaTest( IS_FLAG_SET(MATERIAL_VAR_ALPHATEST) );
  111. // Alpha blending, enable alpha blending if the detail texture is translucent
  112. bool detailIsTranslucent = TextureIsTranslucent( DETAIL, false );
  113. if ( detailIsTranslucent )
  114. {
  115. if ( IS_FLAG_SET( MATERIAL_VAR_ADDITIVE ) )
  116. EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE );
  117. else
  118. EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  119. }
  120. else
  121. {
  122. SetDefaultBlendingShadowState( BASETEXTURE, true );
  123. }
  124. pShaderShadow->EnableSRGBWrite( true );
  125. // Base texture
  126. unsigned int flags = VERTEX_POSITION;
  127. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  128. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  129. // Lightmap
  130. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  131. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, ( nLightmapBindFlags & TEXTURE_BINDFLAGS_SRGBREAD ) != 0 );
  132. // Detail texture
  133. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  134. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER3, true );
  135. // Envmap
  136. if ( bHasEnvmap )
  137. {
  138. flags |= VERTEX_NORMAL;
  139. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  140. bool bSRGBReadEnvMap = g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE;
  141. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER2, bSRGBReadEnvMap );
  142. if( bHasEnvmapMask )
  143. {
  144. pShaderShadow->EnableTexture( SHADER_SAMPLER5, true );
  145. }
  146. }
  147. // Normalizing cube map
  148. pShaderShadow->EnableTexture( SHADER_SAMPLER6, true );
  149. if ( bHasVertexColor )
  150. {
  151. flags |= VERTEX_COLOR;
  152. }
  153. pShaderShadow->VertexShaderVertexFormat( flags, 3, 0, 0 );
  154. DECLARE_STATIC_VERTEX_SHADER( shatteredglass_vs20 );
  155. SET_STATIC_VERTEX_SHADER_COMBO( ENVMAP_MASK, bHasEnvmapMask );
  156. SET_STATIC_VERTEX_SHADER( shatteredglass_vs20 );
  157. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  158. {
  159. DECLARE_STATIC_PIXEL_SHADER( shatteredglass_ps20b );
  160. SET_STATIC_PIXEL_SHADER_COMBO( CUBEMAP, bHasEnvmap );
  161. SET_STATIC_PIXEL_SHADER_COMBO( VERTEXCOLOR, bHasVertexColor );
  162. SET_STATIC_PIXEL_SHADER_COMBO( ENVMAPMASK, bHasEnvmapMask );
  163. SET_STATIC_PIXEL_SHADER_COMBO( BASEALPHAENVMAPMASK, bHasBaseAlphaEnvmapMask );
  164. SET_STATIC_PIXEL_SHADER_COMBO( HDRTYPE, g_pHardwareConfig->GetHDRType() );
  165. SET_STATIC_PIXEL_SHADER( shatteredglass_ps20b );
  166. }
  167. else
  168. {
  169. DECLARE_STATIC_PIXEL_SHADER( shatteredglass_ps20 );
  170. SET_STATIC_PIXEL_SHADER_COMBO( CUBEMAP, bHasEnvmap );
  171. SET_STATIC_PIXEL_SHADER_COMBO( VERTEXCOLOR, bHasVertexColor );
  172. SET_STATIC_PIXEL_SHADER_COMBO( ENVMAPMASK, bHasEnvmapMask );
  173. SET_STATIC_PIXEL_SHADER_COMBO( BASEALPHAENVMAPMASK, bHasBaseAlphaEnvmapMask );
  174. SET_STATIC_PIXEL_SHADER_COMBO( HDRTYPE, g_pHardwareConfig->GetHDRType() );
  175. SET_STATIC_PIXEL_SHADER( shatteredglass_ps20 );
  176. }
  177. DefaultFog();
  178. PI_BeginCommandBuffer();
  179. PI_SetModulationPixelShaderDynamicState( 1 );
  180. PI_EndCommandBuffer();
  181. }
  182. DYNAMIC_STATE
  183. {
  184. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, BASETEXTURETRANSFORM );
  185. SetVertexShaderTextureScale( VERTEX_SHADER_SHADER_SPECIFIC_CONST_2, DETAILSCALE );
  186. BindTexture( SHADER_SAMPLER0, TEXTURE_BINDFLAGS_SRGBREAD, BASETEXTURE, FRAME );
  187. pShaderAPI->BindStandardTexture( SHADER_SAMPLER1, nLightmapBindFlags, TEXTURE_LIGHTMAP );
  188. BindTexture( SHADER_SAMPLER3, TEXTURE_BINDFLAGS_SRGBREAD, DETAIL );
  189. if( bHasEnvmap )
  190. {
  191. bool bSRGBReadEnvMap = g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE;
  192. BindTexture( SHADER_SAMPLER2, SRGBReadMask( bSRGBReadEnvMap ), ENVMAP, ENVMAPFRAME );
  193. if( bHasEnvmapMask )
  194. {
  195. BindTexture( SHADER_SAMPLER5, TEXTURE_BINDFLAGS_NONE, ENVMAPMASK, ENVMAPMASKFRAME );
  196. }
  197. }
  198. pShaderAPI->BindStandardTexture( SHADER_SAMPLER6, TEXTURE_BINDFLAGS_NONE, TEXTURE_NORMALIZATION_CUBEMAP_SIGNED );
  199. DECLARE_DYNAMIC_VERTEX_SHADER( shatteredglass_vs20 );
  200. SET_DYNAMIC_VERTEX_SHADER( shatteredglass_vs20 );
  201. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  202. {
  203. DECLARE_DYNAMIC_PIXEL_SHADER( shatteredglass_ps20b );
  204. SET_DYNAMIC_PIXEL_SHADER( shatteredglass_ps20b );
  205. }
  206. else
  207. {
  208. DECLARE_DYNAMIC_PIXEL_SHADER( shatteredglass_ps20 );
  209. SET_DYNAMIC_PIXEL_SHADER( shatteredglass_ps20 );
  210. }
  211. SetEnvMapTintPixelShaderDynamicState( 0, ENVMAPTINT, -1 );
  212. SetPixelShaderConstant( 2, ENVMAPCONTRAST );
  213. SetPixelShaderConstant( 3, ENVMAPSATURATION );
  214. // [ 0, 0 ,0, R(0) ]
  215. float fresnel[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  216. fresnel[3] = params[FRESNELREFLECTION]->GetFloatValue();
  217. fresnel[0] = fresnel[1] = fresnel[2] = 1.0f - fresnel[3];
  218. pShaderAPI->SetPixelShaderConstant( 4, fresnel );
  219. float eyePos[4];
  220. pShaderAPI->GetWorldSpaceCameraPosition( eyePos );
  221. pShaderAPI->SetPixelShaderConstant( 5, eyePos, 1 );
  222. pShaderAPI->SetPixelShaderFogParams( 12 );
  223. float overbright[4];
  224. overbright[0] = OVERBRIGHT;
  225. overbright[1] = params[UNLITFACTOR]->GetFloatValue();
  226. overbright[2] = overbright[3] = 1.0f - params[UNLITFACTOR]->GetFloatValue();
  227. pShaderAPI->SetPixelShaderConstant( 6, overbright );
  228. }
  229. Draw();
  230. }
  231. END_SHADER