Team Fortress 2 Source Code as on 22/4/2020
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.

297 lines
9.6 KiB

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