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.

271 lines
9.4 KiB

  1. //============ Copyright (c) Valve Corporation, All rights reserved. ============
  2. #include "BaseVSShader.h"
  3. #include "mathlib/vmatrix.h"
  4. #include "common_hlsl_cpp_consts.h" // hack hack hack!
  5. #include "lightmappedreflective_vs20.inc"
  6. #include "lightmappedreflective_ps20.inc"
  7. #include "lightmappedreflective_ps20b.inc"
  8. // NOTE: This has to be the last file included!
  9. #include "tier0/memdbgon.h"
  10. DEFINE_FALLBACK_SHADER( LightmappedReflective, LightmappedReflective_DX90 )
  11. BEGIN_VS_SHADER( LightmappedReflective_DX90, "Help for Lightmapped Reflective" )
  12. BEGIN_SHADER_PARAMS
  13. SHADER_PARAM( REFRACTTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "_rt_WaterRefraction", "" )
  14. SHADER_PARAM( REFLECTTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "_rt_WaterReflection", "" )
  15. SHADER_PARAM( REFRACTAMOUNT, SHADER_PARAM_TYPE_FLOAT, "0", "" )
  16. SHADER_PARAM( REFRACTTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "refraction tint" )
  17. SHADER_PARAM( REFLECTAMOUNT, SHADER_PARAM_TYPE_FLOAT, "0.8", "" )
  18. SHADER_PARAM( REFLECTTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "reflection tint" )
  19. SHADER_PARAM( NORMALMAP, SHADER_PARAM_TYPE_TEXTURE, "dev/water_normal", "normal map" )
  20. SHADER_PARAM( BUMPFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "frame number for $bumpmap" )
  21. SHADER_PARAM( BUMPTRANSFORM, SHADER_PARAM_TYPE_MATRIX, "center .5 .5 scale 1 1 rotate 0 translate 0 0", "$bumpmap texcoord transform" )
  22. SHADER_PARAM( ENVMAPMASK, SHADER_PARAM_TYPE_TEXTURE, "shadertest/shadertest_envmask", "envmap mask" )
  23. SHADER_PARAM( ENVMAPMASKFRAME, SHADER_PARAM_TYPE_INTEGER, "", "" )
  24. SHADER_PARAM( REFLECTANCE, SHADER_PARAM_TYPE_FLOAT, "0.25", "" )
  25. END_SHADER_PARAMS
  26. SHADER_INIT_PARAMS()
  27. {
  28. if ( !params[REFLECTANCE]->IsDefined() )
  29. {
  30. params[REFLECTANCE]->SetFloatValue( 0.25f );
  31. }
  32. SET_FLAGS2( MATERIAL_VAR2_NEEDS_TANGENT_SPACES );
  33. if ( params[BASETEXTURE]->IsDefined() )
  34. {
  35. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_LIGHTMAP );
  36. if ( g_pConfig->UseBumpmapping() && params[NORMALMAP]->IsDefined() )
  37. {
  38. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_BUMPED_LIGHTMAP );
  39. }
  40. }
  41. }
  42. SHADER_FALLBACK
  43. {
  44. return 0;
  45. }
  46. SHADER_INIT
  47. {
  48. if ( params[REFRACTTEXTURE]->IsDefined() )
  49. {
  50. LoadTexture( REFRACTTEXTURE, g_pHardwareConfig->GetHDRType() == HDR_TYPE_INTEGER || IsOSX() ? TEXTUREFLAGS_SRGB : 0 );
  51. }
  52. if ( params[REFLECTTEXTURE]->IsDefined() )
  53. {
  54. LoadTexture( REFLECTTEXTURE, g_pHardwareConfig->GetHDRType() == HDR_TYPE_INTEGER || IsOSX() ? TEXTUREFLAGS_SRGB : 0 );
  55. }
  56. if ( params[NORMALMAP]->IsDefined() )
  57. {
  58. LoadBumpMap( NORMALMAP );
  59. }
  60. if( params[BASETEXTURE]->IsDefined() )
  61. {
  62. LoadTexture( BASETEXTURE, TEXTUREFLAGS_SRGB );
  63. if( params[ENVMAPMASK]->IsDefined() )
  64. {
  65. LoadTexture( ENVMAPMASK );
  66. }
  67. }
  68. else
  69. {
  70. params[ENVMAPMASK]->SetUndefined();
  71. }
  72. }
  73. inline void DrawReflectionRefraction( IMaterialVar **params, IShaderShadow *pShaderShadow,
  74. IShaderDynamicAPI *pShaderAPI, bool bReflection, bool bRefraction )
  75. {
  76. BlendType_t nBlendType = EvaluateBlendRequirements( BASETEXTURE, true );
  77. bool bFullyOpaque = ( nBlendType != BT_BLENDADD ) && ( nBlendType != BT_BLEND ) && !IS_FLAG_SET( MATERIAL_VAR_ALPHATEST ); //dest alpha is free for special use
  78. SHADOW_STATE
  79. {
  80. SetInitialShadowState( );
  81. if( bRefraction )
  82. {
  83. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true ); // Refract
  84. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, g_pHardwareConfig->GetHDRType() == HDR_TYPE_INTEGER || IsOSX() );
  85. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true ); // Base
  86. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, true );
  87. }
  88. if( bReflection )
  89. {
  90. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true ); // Reflect
  91. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER2, g_pHardwareConfig->GetHDRType() == HDR_TYPE_INTEGER || IsOSX() );
  92. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true ); // Lightmap
  93. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER3, g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE );
  94. }
  95. if( params[BASETEXTURE]->IsTexture() )
  96. {
  97. // BASETEXTURE
  98. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  99. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, true );
  100. // LIGHTMAP
  101. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  102. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER3, g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE );
  103. if ( params[ENVMAPMASK]->IsTexture() )
  104. {
  105. pShaderShadow->EnableTexture( SHADER_SAMPLER6, true );
  106. }
  107. }
  108. // normal map
  109. pShaderShadow->EnableTexture( SHADER_SAMPLER4, true );
  110. int fmt = VERTEX_POSITION | VERTEX_NORMAL | VERTEX_TANGENT_S | VERTEX_TANGENT_T;
  111. // texcoord0 : base texcoord
  112. // texcoord1 : lightmap texcoord
  113. // texcoord2 : lightmap texcoord offset
  114. int numTexCoords = 1;
  115. if ( params[BASETEXTURE]->IsTexture() )
  116. {
  117. numTexCoords = 3;
  118. }
  119. pShaderShadow->VertexShaderVertexFormat( fmt, numTexCoords, 0, 0 );
  120. if ( IS_FLAG_SET( MATERIAL_VAR_TRANSLUCENT ) )
  121. {
  122. EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  123. }
  124. DECLARE_STATIC_VERTEX_SHADER( lightmappedreflective_vs20 );
  125. SET_STATIC_VERTEX_SHADER_COMBO( BASETEXTURE, params[BASETEXTURE]->IsTexture() );
  126. SET_STATIC_VERTEX_SHADER( lightmappedreflective_vs20 );
  127. // "REFLECT" "0..1"
  128. // "REFRACT" "0..1"
  129. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  130. {
  131. DECLARE_STATIC_PIXEL_SHADER( lightmappedreflective_ps20b );
  132. SET_STATIC_PIXEL_SHADER_COMBO( REFLECT, bReflection );
  133. SET_STATIC_PIXEL_SHADER_COMBO( REFRACT, bRefraction );
  134. SET_STATIC_PIXEL_SHADER_COMBO( BASETEXTURE, params[BASETEXTURE]->IsTexture() );
  135. SET_STATIC_PIXEL_SHADER_COMBO( ENVMAPMASK, params[ENVMAPMASK]->IsTexture() && params[BASETEXTURE]->IsTexture() );
  136. SET_STATIC_PIXEL_SHADER( lightmappedreflective_ps20b );
  137. }
  138. else
  139. {
  140. DECLARE_STATIC_PIXEL_SHADER( lightmappedreflective_ps20 );
  141. SET_STATIC_PIXEL_SHADER_COMBO( REFLECT, bReflection );
  142. SET_STATIC_PIXEL_SHADER_COMBO( REFRACT, bRefraction );
  143. SET_STATIC_PIXEL_SHADER_COMBO( BASETEXTURE, params[BASETEXTURE]->IsTexture() );
  144. SET_STATIC_PIXEL_SHADER_COMBO( ENVMAPMASK, params[ENVMAPMASK]->IsTexture() && params[BASETEXTURE]->IsTexture() );
  145. SET_STATIC_PIXEL_SHADER( lightmappedreflective_ps20 );
  146. }
  147. FogToFogColor();
  148. // we are writing linear values from this shader.
  149. pShaderShadow->EnableSRGBWrite( true );
  150. pShaderShadow->EnableAlphaWrites( bFullyOpaque );
  151. }
  152. DYNAMIC_STATE
  153. {
  154. TextureBindFlags_t nBindFlags = ( g_pHardwareConfig->GetHDRType() == HDR_TYPE_INTEGER || IsOSXOpenGL() ) ? TEXTURE_BINDFLAGS_SRGBREAD : TEXTURE_BINDFLAGS_NONE;
  155. if ( bRefraction )
  156. {
  157. // HDRFIXME: add comment about binding.. Specify the number of MRTs in the enable
  158. BindTexture( SHADER_SAMPLER0, nBindFlags, REFRACTTEXTURE, -1 );
  159. }
  160. if ( bReflection )
  161. {
  162. BindTexture( SHADER_SAMPLER2, nBindFlags, REFLECTTEXTURE, -1 );
  163. }
  164. BindTexture( SHADER_SAMPLER4, TEXTURE_BINDFLAGS_NONE, NORMALMAP, BUMPFRAME );
  165. if ( params[BASETEXTURE]->IsTexture() )
  166. {
  167. BindTexture( SHADER_SAMPLER1, TEXTURE_BINDFLAGS_SRGBREAD, BASETEXTURE, FRAME );
  168. pShaderAPI->BindStandardTexture( SHADER_SAMPLER3, ( g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE ) ? TEXTURE_BINDFLAGS_SRGBREAD : TEXTURE_BINDFLAGS_NONE, TEXTURE_LIGHTMAP );
  169. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_3, BASETEXTURETRANSFORM );
  170. if ( params[ENVMAPMASK]->IsTexture() )
  171. {
  172. BindTexture( SHADER_SAMPLER6, TEXTURE_BINDFLAGS_NONE, ENVMAPMASK, ENVMAPMASKFRAME );
  173. }
  174. }
  175. // Refraction tint
  176. if ( bRefraction )
  177. {
  178. SetPixelShaderConstantGammaToLinear( 1, REFRACTTINT );
  179. }
  180. // Reflection tint
  181. if ( bReflection )
  182. {
  183. SetPixelShaderConstantGammaToLinear( 4, REFLECTTINT );
  184. }
  185. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_1, BUMPTRANSFORM );
  186. float c0[4] = { 1.0f / 3.0f, 1.0f / 3.0f, 1.0f / 3.0f, 0.0f };
  187. pShaderAPI->SetPixelShaderConstant( 0, c0, 1 );
  188. float c2[4] = { 0.5f, 0.5f, 0.5f, 0.5f };
  189. pShaderAPI->SetPixelShaderConstant( 2, c2, 1 );
  190. // fresnel constants
  191. float c3[4] = { clamp( params[REFLECTANCE]->GetFloatValue(), 0.0f, 1.0f ), 0.0f, 0.0f, 0.0f };
  192. pShaderAPI->SetPixelShaderConstant( 3, c3, 1 );
  193. float c5[4] = { params[REFLECTAMOUNT]->GetFloatValue(), params[REFLECTAMOUNT]->GetFloatValue(),
  194. params[REFRACTAMOUNT]->GetFloatValue(), params[REFRACTAMOUNT]->GetFloatValue() };
  195. pShaderAPI->SetPixelShaderConstant( 5, c5, 1 );
  196. pShaderAPI->SetPixelShaderFogParams( PSREG_FOG_PARAMS );
  197. float vEyePos_SpecExponent[4];
  198. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos_SpecExponent );
  199. vEyePos_SpecExponent[3] = 0.0f;
  200. pShaderAPI->SetPixelShaderConstant( PSREG_EYEPOS_SPEC_EXPONENT, vEyePos_SpecExponent, 1 );
  201. DECLARE_DYNAMIC_VERTEX_SHADER( lightmappedreflective_vs20 );
  202. SET_DYNAMIC_VERTEX_SHADER( lightmappedreflective_vs20 );
  203. if ( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  204. {
  205. DECLARE_DYNAMIC_PIXEL_SHADER( lightmappedreflective_ps20b );
  206. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITE_DEPTH_TO_DESTALPHA, bFullyOpaque && pShaderAPI->ShouldWriteDepthToDestAlpha() );
  207. SET_DYNAMIC_PIXEL_SHADER( lightmappedreflective_ps20b );
  208. }
  209. else
  210. {
  211. DECLARE_DYNAMIC_PIXEL_SHADER( lightmappedreflective_ps20 );
  212. SET_DYNAMIC_PIXEL_SHADER( lightmappedreflective_ps20 );
  213. }
  214. }
  215. Draw();
  216. }
  217. SHADER_DRAW
  218. {
  219. bool bRefraction = params[REFRACTTEXTURE]->IsTexture();
  220. bool bReflection = params[REFLECTTEXTURE]->IsTexture();
  221. DrawReflectionRefraction( params, pShaderShadow, pShaderAPI, bReflection, bRefraction );
  222. }
  223. END_SHADER