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.

301 lines
9.7 KiB

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