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.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Header: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "BaseVSShader.h"
  9. #include "sky_vs20.inc"
  10. #include "sky_ps20.inc"
  11. #include "sky_ps20b.inc"
  12. #include "sky_hdr_compressed_ps20.inc"
  13. #include "sky_hdr_compressed_ps20b.inc"
  14. #include "sky_hdr_compressed_rgbs_ps20.inc"
  15. #include "sky_hdr_compressed_rgbs_ps20b.inc"
  16. #include "convar.h"
  17. static ConVar mat_use_compressed_hdr_textures( "mat_use_compressed_hdr_textures", "1" );
  18. DEFINE_FALLBACK_SHADER( Sky, Sky_HDR_DX9 )
  19. BEGIN_VS_SHADER( Sky_HDR_DX9, "Help for Sky_HDR_DX9 shader" )
  20. BEGIN_SHADER_PARAMS
  21. SHADER_PARAM( HDRBASETEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "base texture when running with HDR enabled" )
  22. SHADER_PARAM( HDRCOMPRESSEDTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "base texture (compressed) for hdr compression method A" )
  23. SHADER_PARAM( HDRCOMPRESSEDTEXTURE0, SHADER_PARAM_TYPE_TEXTURE, "", "compressed base texture0 for hdr compression method B" )
  24. SHADER_PARAM( HDRCOMPRESSEDTEXTURE1, SHADER_PARAM_TYPE_TEXTURE, "", "compressed base texture1 for hdr compression method B" )
  25. SHADER_PARAM( HDRCOMPRESSEDTEXTURE2, SHADER_PARAM_TYPE_TEXTURE, "", "compressed base texture2 for hdr compression method B" )
  26. SHADER_PARAM_OVERRIDE( COLOR, SHADER_PARAM_TYPE_VEC3, "[ 1 1 1]", "color multiplier", SHADER_PARAM_NOT_EDITABLE )
  27. END_SHADER_PARAMS
  28. SHADER_FALLBACK
  29. {
  30. if( g_pHardwareConfig->GetDXSupportLevel() < 90 || g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE )
  31. {
  32. return "Sky_DX9";
  33. }
  34. return 0;
  35. }
  36. SHADER_INIT_PARAMS()
  37. {
  38. SET_FLAGS( MATERIAL_VAR_NOFOG );
  39. SET_FLAGS( MATERIAL_VAR_IGNOREZ );
  40. }
  41. SHADER_INIT
  42. {
  43. // First figure out if sampler zero wants to be sRGB
  44. int nSamplerZeroFlags = 0;
  45. if ( (params[HDRCOMPRESSEDTEXTURE]->IsDefined()) && mat_use_compressed_hdr_textures.GetBool() )
  46. {
  47. nSamplerZeroFlags = 0;
  48. }
  49. else
  50. {
  51. if (params[HDRCOMPRESSEDTEXTURE0]->IsDefined())
  52. {
  53. nSamplerZeroFlags = 0;
  54. }
  55. else
  56. {
  57. nSamplerZeroFlags = TEXTUREFLAGS_SRGB;
  58. if ( params[HDRBASETEXTURE]->IsDefined() && params[HDRBASETEXTURE]->IsTexture() )
  59. {
  60. ITexture *txtr=params[HDRBASETEXTURE]->GetTextureValue();
  61. ImageFormat fmt=txtr->GetImageFormat();
  62. if ( ( fmt == IMAGE_FORMAT_RGBA16161616F ) || ( fmt == IMAGE_FORMAT_RGBA16161616 ) )
  63. {
  64. nSamplerZeroFlags = 0;
  65. }
  66. }
  67. }
  68. }
  69. // Next, figure out which texture will be on sampler zero
  70. int nSampler0 = HDRCOMPRESSEDTEXTURE;
  71. if ( params[HDRCOMPRESSEDTEXTURE]->IsDefined() && mat_use_compressed_hdr_textures.GetBool() )
  72. {
  73. nSampler0 = HDRCOMPRESSEDTEXTURE;
  74. }
  75. else
  76. {
  77. if ( params[HDRCOMPRESSEDTEXTURE0]->IsDefined() )
  78. {
  79. nSampler0 = HDRCOMPRESSEDTEXTURE0;
  80. }
  81. else
  82. {
  83. nSampler0 = HDRBASETEXTURE;
  84. }
  85. }
  86. // Load the appropriate textures, making sure that the texture set on sampler 0 is sRGB if necessary
  87. if ( params[HDRCOMPRESSEDTEXTURE]->IsDefined() && (mat_use_compressed_hdr_textures.GetBool() ) )
  88. {
  89. LoadTexture( HDRCOMPRESSEDTEXTURE, HDRCOMPRESSEDTEXTURE == nSampler0 ? nSamplerZeroFlags : 0 );
  90. }
  91. else
  92. {
  93. if (params[HDRCOMPRESSEDTEXTURE0]->IsDefined())
  94. {
  95. LoadTexture( HDRCOMPRESSEDTEXTURE0, HDRCOMPRESSEDTEXTURE0 == nSampler0 ? nSamplerZeroFlags : 0 );
  96. if ( params[HDRCOMPRESSEDTEXTURE1]->IsDefined() )
  97. {
  98. LoadTexture( HDRCOMPRESSEDTEXTURE1, HDRCOMPRESSEDTEXTURE1 == nSampler0 ? nSamplerZeroFlags : 0 );
  99. }
  100. if ( params[HDRCOMPRESSEDTEXTURE2]->IsDefined())
  101. {
  102. LoadTexture( HDRCOMPRESSEDTEXTURE2, HDRCOMPRESSEDTEXTURE2 == nSampler0 ? nSamplerZeroFlags : 0 );
  103. }
  104. }
  105. else
  106. {
  107. if ( params[HDRBASETEXTURE]->IsDefined() )
  108. {
  109. LoadTexture( HDRBASETEXTURE, HDRBASETEXTURE == nSampler0 ? nSamplerZeroFlags : 0 );
  110. }
  111. }
  112. }
  113. }
  114. SHADER_DRAW
  115. {
  116. SHADOW_STATE
  117. {
  118. SetInitialShadowState();
  119. // pShaderShadow->EnableAlphaWrites( true );
  120. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  121. pShaderShadow->VertexShaderVertexFormat( VERTEX_POSITION, 1, NULL, 0 );
  122. DECLARE_STATIC_VERTEX_SHADER( sky_vs20 );
  123. SET_STATIC_VERTEX_SHADER( sky_vs20 );
  124. if ( (params[HDRCOMPRESSEDTEXTURE]->IsDefined()) &&
  125. mat_use_compressed_hdr_textures.GetBool() )
  126. {
  127. pShaderShadow->EnableSRGBRead(SHADER_SAMPLER0,false);
  128. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  129. {
  130. DECLARE_STATIC_PIXEL_SHADER( sky_hdr_compressed_rgbs_ps20b );
  131. SET_STATIC_PIXEL_SHADER( sky_hdr_compressed_rgbs_ps20b );
  132. }
  133. else
  134. {
  135. DECLARE_STATIC_PIXEL_SHADER( sky_hdr_compressed_rgbs_ps20 );
  136. SET_STATIC_PIXEL_SHADER( sky_hdr_compressed_rgbs_ps20 );
  137. }
  138. }
  139. else
  140. {
  141. if (params[HDRCOMPRESSEDTEXTURE0]->IsDefined())
  142. {
  143. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  144. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  145. pShaderShadow->EnableSRGBRead(SHADER_SAMPLER0,false);
  146. pShaderShadow->EnableSRGBRead(SHADER_SAMPLER1,false);
  147. pShaderShadow->EnableSRGBRead(SHADER_SAMPLER2,false);
  148. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  149. {
  150. DECLARE_STATIC_PIXEL_SHADER( sky_hdr_compressed_ps20b );
  151. SET_STATIC_PIXEL_SHADER( sky_hdr_compressed_ps20b );
  152. }
  153. else
  154. {
  155. DECLARE_STATIC_PIXEL_SHADER( sky_hdr_compressed_ps20 );
  156. SET_STATIC_PIXEL_SHADER( sky_hdr_compressed_ps20 );
  157. }
  158. }
  159. else
  160. {
  161. ITexture *txtr=params[HDRBASETEXTURE]->GetTextureValue();
  162. ImageFormat fmt=txtr->GetImageFormat();
  163. if ((fmt==IMAGE_FORMAT_RGBA16161616F) || (fmt==IMAGE_FORMAT_RGBA16161616))
  164. pShaderShadow->EnableSRGBRead(SHADER_SAMPLER0,false);
  165. else
  166. pShaderShadow->EnableSRGBRead(SHADER_SAMPLER0,true);
  167. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  168. {
  169. DECLARE_STATIC_PIXEL_SHADER( sky_ps20b );
  170. SET_STATIC_PIXEL_SHADER( sky_ps20b );
  171. }
  172. else
  173. {
  174. DECLARE_STATIC_PIXEL_SHADER( sky_ps20 );
  175. SET_STATIC_PIXEL_SHADER( sky_ps20 );
  176. }
  177. }
  178. }
  179. // we are writing linear values from this shader.
  180. pShaderShadow->EnableSRGBWrite( true );
  181. pShaderShadow->EnableAlphaWrites( true );
  182. }
  183. DYNAMIC_STATE
  184. {
  185. DECLARE_DYNAMIC_VERTEX_SHADER( sky_vs20 );
  186. SET_DYNAMIC_VERTEX_SHADER( sky_vs20 );
  187. // Texture coord transform
  188. SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_1, BASETEXTURETRANSFORM );
  189. float c0[4]={1,1,1,1};
  190. if (params[COLOR]->IsDefined())
  191. {
  192. memcpy(c0,params[COLOR]->GetVecValue(),3*sizeof(float));
  193. }
  194. if (
  195. params[HDRCOMPRESSEDTEXTURE]->IsDefined() &&
  196. mat_use_compressed_hdr_textures.GetBool()
  197. )
  198. {
  199. // set up data needs for pixel shader interpolation
  200. ITexture *txtr=params[HDRCOMPRESSEDTEXTURE]->GetTextureValue();
  201. float w=txtr->GetActualWidth();
  202. float h=txtr->GetActualHeight();
  203. float FUDGE=0.01/max(w,h); // per ATI
  204. float c1[4]={(float)(0.5/w-FUDGE), (float)(0.5/h-FUDGE), w, h };
  205. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, c1);
  206. BindTexture( SHADER_SAMPLER0, HDRCOMPRESSEDTEXTURE, FRAME );
  207. c0[0]*=8.0;
  208. c0[1]*=8.0;
  209. c0[2]*=8.0;
  210. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  211. {
  212. DECLARE_DYNAMIC_PIXEL_SHADER( sky_hdr_compressed_rgbs_ps20b );
  213. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITE_DEPTH_TO_DESTALPHA, pShaderAPI->ShouldWriteDepthToDestAlpha() );
  214. SET_DYNAMIC_PIXEL_SHADER( sky_hdr_compressed_rgbs_ps20b );
  215. }
  216. else
  217. {
  218. DECLARE_DYNAMIC_PIXEL_SHADER( sky_hdr_compressed_rgbs_ps20 );
  219. SET_DYNAMIC_PIXEL_SHADER( sky_hdr_compressed_rgbs_ps20 );
  220. }
  221. }
  222. else
  223. {
  224. float c1[4]={0,0,0,0};
  225. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, c1);
  226. if (params[HDRCOMPRESSEDTEXTURE0]->IsDefined() )
  227. {
  228. BindTexture( SHADER_SAMPLER0, HDRCOMPRESSEDTEXTURE0, FRAME );
  229. BindTexture( SHADER_SAMPLER1, HDRCOMPRESSEDTEXTURE1, FRAME );
  230. BindTexture( SHADER_SAMPLER2, HDRCOMPRESSEDTEXTURE2, FRAME );
  231. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  232. {
  233. DECLARE_DYNAMIC_PIXEL_SHADER( sky_hdr_compressed_ps20b );
  234. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITE_DEPTH_TO_DESTALPHA, pShaderAPI->ShouldWriteDepthToDestAlpha() );
  235. SET_DYNAMIC_PIXEL_SHADER( sky_hdr_compressed_ps20b );
  236. }
  237. else
  238. {
  239. DECLARE_DYNAMIC_PIXEL_SHADER( sky_hdr_compressed_ps20 );
  240. SET_DYNAMIC_PIXEL_SHADER( sky_hdr_compressed_ps20 );
  241. }
  242. }
  243. else
  244. {
  245. BindTexture( SHADER_SAMPLER0, HDRBASETEXTURE, FRAME );
  246. ITexture *txtr=params[HDRBASETEXTURE]->GetTextureValue();
  247. ImageFormat fmt=txtr->GetImageFormat();
  248. if (
  249. (fmt==IMAGE_FORMAT_RGBA16161616) ||
  250. ( (fmt==IMAGE_FORMAT_RGBA16161616F) &&
  251. (g_pHardwareConfig->GetHDRType()==HDR_TYPE_INTEGER))
  252. )
  253. {
  254. c0[0]*=16.0;
  255. c0[1]*=16.0;
  256. c0[2]*=16.0;
  257. }
  258. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  259. {
  260. DECLARE_DYNAMIC_PIXEL_SHADER( sky_ps20b );
  261. SET_DYNAMIC_PIXEL_SHADER_COMBO( WRITE_DEPTH_TO_DESTALPHA, pShaderAPI->ShouldWriteDepthToDestAlpha() );
  262. SET_DYNAMIC_PIXEL_SHADER( sky_ps20b );
  263. }
  264. else
  265. {
  266. DECLARE_DYNAMIC_PIXEL_SHADER( sky_ps20 );
  267. SET_DYNAMIC_PIXEL_SHADER( sky_ps20 );
  268. }
  269. }
  270. }
  271. pShaderAPI->SetPixelShaderConstant( 0, c0, 1 );
  272. }
  273. Draw( );
  274. }
  275. END_SHADER