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.

337 lines
8.7 KiB

  1. //========== Copyright (c) Valve Corporation, All rights reserved. ==========//
  2. // DYNAMIC: "MODE" "0..9"
  3. // STATIC: "LINEAR_TO_SRGB" "0..1" [ps20b]
  4. #include "common_ps_fxc.h"
  5. const float g_Alpha : register( c0 );
  6. sampler BaseTextureSampler : register( s0 );
  7. sampler BaseTextureSampler2 : register( s1 );
  8. struct PS_INPUT
  9. {
  10. float2 baseTexCoord : TEXCOORD0;
  11. };
  12. float3 RGBtoHSV( in float3 rgb )
  13. {
  14. float3 hsv;
  15. float fmin, fmax, delta;
  16. fmin = min( min( rgb.r, rgb.g ), rgb.b );
  17. fmax = max( max( rgb.r, rgb.g) , rgb.b );
  18. hsv.b = fmax; // v
  19. delta = fmax - fmin;
  20. // if( fmax != 0 )
  21. {
  22. hsv.g = delta / fmax; // s
  23. if( rgb.r == fmax )
  24. hsv.r = ( rgb.g - rgb.b ) / delta; // between yellow & magenta
  25. else if( rgb.g == fmax )
  26. hsv.r = 2 + ( rgb.b - rgb.r ) / delta; // between cyan & yellow
  27. else
  28. hsv.r = 4 + ( rgb.r - rgb.g ) / delta; // between magenta & cyan
  29. hsv.r *= 60; // degrees
  30. if( hsv.r < 0 )
  31. hsv.r += 360;
  32. }
  33. // else
  34. // {
  35. // // r = g = b = 0 // s = 0, v is undefined
  36. // hsv.g = 0;
  37. // hsv.r = -1;
  38. // }
  39. return hsv;
  40. }
  41. float3 HSVtoRGB( in float3 hsv )
  42. {
  43. int i;
  44. float3 rgb;
  45. float h = hsv.r;
  46. float s = hsv.g;
  47. float v = hsv.b;
  48. float f, p, q, t;
  49. if( s == 0 )
  50. {
  51. // achromatic (grey)
  52. rgb.rgb = v;
  53. }
  54. else
  55. {
  56. h /= 60; // sector 0 to 5
  57. i = floor( h );
  58. f = h - i; // factorial part of h
  59. p = v * ( 1 - s );
  60. q = v * ( 1 - s * f );
  61. t = v * ( 1 - s * ( 1 - f ) );
  62. if( i == 0 )
  63. {
  64. rgb.r = v;
  65. rgb.g = t;
  66. rgb.b = p;
  67. }
  68. else if( i == 1 )
  69. {
  70. rgb.r = q;
  71. rgb.g = v;
  72. rgb.b = p;
  73. }
  74. else if( i == 2 )
  75. {
  76. rgb.r = p;
  77. rgb.g = v;
  78. rgb.b = t;
  79. }
  80. else if( i == 3 )
  81. {
  82. rgb.r = p;
  83. rgb.g = q;
  84. rgb.b = v;
  85. }
  86. else if( i == 4 )
  87. {
  88. rgb.r = t;
  89. rgb.g = p;
  90. rgb.b = v;
  91. }
  92. else // if( i == 5 )
  93. {
  94. rgb.r = v;
  95. rgb.g = p;
  96. rgb.b = q;
  97. }
  98. }
  99. return rgb;
  100. }
  101. // We have to run through this input converter on OpenGL if the
  102. // rest of the shader code is expecting sRGB values
  103. float3 SampleTexture( sampler texSampler, float2 tc )
  104. {
  105. float3 c = tex2D( texSampler, tc ).xyz;
  106. #if ( LINEAR_TO_SRGB )
  107. {
  108. c = LinearToGamma( c );
  109. }
  110. #endif
  111. return c;
  112. }
  113. // We have to run through this output converter on OpenGL if we
  114. // expect to be writing out sRGB values (since sRGB will be forced on)
  115. float3 OutputColor( float3 result )
  116. {
  117. #if ( LINEAR_TO_SRGB )
  118. {
  119. return GammaToLinear( result );
  120. }
  121. #endif
  122. return result;
  123. }
  124. float4 main( PS_INPUT i ) : COLOR
  125. {
  126. float3 result;
  127. #if MODE == 0
  128. // negative greyscale of scene * gman
  129. float3 scene = SampleTexture( BaseTextureSampler, i.baseTexCoord );
  130. float3 gman = SampleTexture( BaseTextureSampler2, i.baseTexCoord );
  131. float scale = 1.0f / 3.0f;
  132. scene.xyz = dot( float3( scale, scale, scale), scene.xyz );
  133. scene = float3( 1, 1, 1 ) - scene;
  134. return FinalOutput( float4( OutputColor( scene * gman ), g_Alpha ), 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
  135. #endif
  136. #if MODE == 1
  137. float3 scene = SampleTexture( BaseTextureSampler, i.baseTexCoord );
  138. float3 gman = SampleTexture( BaseTextureSampler2, i.baseTexCoord );
  139. float scale = 1.0f / 3.0f;
  140. scene.xyz = dot( float3( scale, scale, scale ), scene.xyz );
  141. float gmanLum = dot( float3( scale, scale, scale ), gman );
  142. if( gmanLum < 0.3 )
  143. {
  144. result = OutputColor( float3( 1, 1, 1 ) - gman );
  145. return FinalOutput( float4( result, g_Alpha ), 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
  146. }
  147. else
  148. {
  149. result = OutputColor( ( float3( 1, 1, 1 ) - gman ) * scene );
  150. return FinalOutput( float4( result, g_Alpha ), 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
  151. }
  152. #endif
  153. #if MODE == 2
  154. float3 scene = SampleTexture( BaseTextureSampler, i.baseTexCoord );
  155. float3 gman = SampleTexture( BaseTextureSampler2, i.baseTexCoord );
  156. float startRamp = .2;
  157. float endRamp = .5;
  158. float scale = 1.0f / 3.0f;
  159. float gmanLum = dot( float3( scale, scale, scale ), gman );
  160. float sceneLum = dot( float3( scale, scale, scale ), scene );
  161. float blend = ( gmanLum - startRamp ) * ( 1.0f / ( endRamp - startRamp ) );
  162. blend = saturate( blend );
  163. // return gmanLum * ( 1.0f - blend ) + scene * blend;
  164. result = OutputColor( min( gmanLum.xxx, scene ) );
  165. return FinalOutput( float4( result, g_Alpha ), 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
  166. #endif
  167. #if MODE == 3
  168. float3 scene = SampleTexture( BaseTextureSampler, i.baseTexCoord );
  169. float3 gman = SampleTexture( BaseTextureSampler2, i.baseTexCoord );
  170. float scale = 1.0f / 3.0f;
  171. float gmanLum = dot( float3( scale, scale, scale ), gman );
  172. float sceneLum = dot( float3( scale, scale, scale ), scene );
  173. float a = 0.0f;
  174. float b = 0.4f;
  175. float c = 0.7f;
  176. float d = 1.0f;
  177. float blend;
  178. if( gmanLum < b )
  179. {
  180. blend = ( gmanLum - a ) / ( b - a );
  181. }
  182. else if( gmanLum > c )
  183. {
  184. blend = 1.0f - ( ( gmanLum - c) / ( d - c ) );
  185. }
  186. else
  187. {
  188. blend = 1.0f;
  189. }
  190. blend = saturate( blend );
  191. result = OutputColor( gmanLum.xxx * ( float3( 1, 1, 1 ) - blend.xxx ) + scene * blend.xxx );
  192. return FinalOutput( float4( result, g_Alpha ), 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
  193. #endif
  194. #if MODE == 4
  195. float3 scene = SampleTexture( BaseTextureSampler, i.baseTexCoord );
  196. float3 gman = SampleTexture( BaseTextureSampler2, i.baseTexCoord );
  197. float scale = 1.0f / 3.0f;
  198. float gmanLum = dot( float3( scale, scale, scale ), gman );
  199. float sceneLum = dot( float3( scale, scale, scale ), scene );
  200. float a = 0.0f;
  201. float b = 0.4f;
  202. float c = 0.7f;
  203. float d = 1.0f;
  204. float blend;
  205. if( gmanLum < b )
  206. {
  207. blend = ( gmanLum - a ) / ( b - a );
  208. }
  209. else if( gmanLum > c )
  210. {
  211. blend = 1.0f - ( ( gmanLum - c) / ( d - c ) );
  212. }
  213. else
  214. {
  215. blend = 1.0f;
  216. }
  217. blend = saturate( blend );
  218. result = OutputColor( gman * ( float3( 1, 1, 1 ) - blend.xxx ) + scene * blend.xxx );
  219. return FinalOutput( float4( result, g_Alpha ), 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
  220. #endif
  221. #if MODE == 5
  222. float3 scene = SampleTexture( BaseTextureSampler, i.baseTexCoord );
  223. float3 gman = SampleTexture( BaseTextureSampler2, i.baseTexCoord );
  224. float scale = 1.0f / 3.0f;
  225. // float sceneLum = dot( float3( scale, scale, scale ), scene );
  226. float sceneLum = scene.r;
  227. if( sceneLum > 0.0f )
  228. {
  229. return FinalOutput( float4( OutputColor( scene ), g_Alpha ), 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
  230. }
  231. else
  232. {
  233. float3 hsv = RGBtoHSV( gman );
  234. // float blend = saturate( hsv.b - .5 );
  235. float blend = hsv.b - .5;
  236. hsv.b *= 1.0f + blend;
  237. hsv.g *= 1.0f - blend;
  238. return FinalOutput( float4( OutputColor( HSVtoRGB( hsv ) ), g_Alpha ), 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
  239. }
  240. #endif
  241. #if MODE == 6
  242. float3 scene = SampleTexture( BaseTextureSampler, i.baseTexCoord );
  243. float3 gman = SampleTexture( BaseTextureSampler2, i.baseTexCoord );
  244. return FinalOutput( float4( OutputColor( scene + gman ), g_Alpha ), 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
  245. #endif
  246. #if MODE == 7
  247. float3 scene = SampleTexture( BaseTextureSampler, i.baseTexCoord );
  248. return FinalOutput( float4( OutputColor( scene ), g_Alpha ), 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
  249. #endif
  250. #if MODE == 8
  251. float3 gman = SampleTexture( BaseTextureSampler2, i.baseTexCoord );
  252. return FinalOutput( float4( OutputColor( gman ), g_Alpha ), 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
  253. #endif
  254. #if MODE == 9
  255. // Fetch textures
  256. float3 cLayer1 = SampleTexture( BaseTextureSampler, i.baseTexCoord.xy );
  257. float3 cLayer2 = SampleTexture( BaseTextureSampler2, i.baseTexCoord.xy );
  258. /*
  259. // Put colors roughly back into gamma space
  260. float3 cGammaLayer1 = pow( cLayer1, 0.454545f );
  261. float3 cGammaLayer2 = pow( cLayer2, 0.454545f );
  262. // Brightness
  263. //float flLayer1Brightness = saturate( dot( cGammaLayer1.rgb, float3( 0.3f, 0.59f, 0.11f ) ) );
  264. //float flLayer2Brightness = saturate( dot( cGammaLayer2.rgb, float3( 0.3f, 0.59f, 0.11f ) ) );
  265. float flLayer1Brightness = saturate( dot( cGammaLayer1.rgb, float3( 0.333f, 0.334f, 0.333f ) ) );
  266. float flLayer2Brightness = saturate( dot( cGammaLayer2.rgb, float3( 0.333f, 0.334f, 0.333f ) ) );
  267. // Blend layers in rough gamma space
  268. float3 cGammaOverlayResult;
  269. if ( flLayer1Brightness < 0.5f )
  270. {
  271. cGammaOverlayResult.rgb = ( 2.0f * cGammaLayer1.rgb * cGammaLayer2.rgb );
  272. }
  273. else
  274. {
  275. cGammaOverlayResult.rgb = ( 1.0f - ( 2.0f * ( 1.0f - cGammaLayer1.rgb ) * ( 1.0f - cGammaLayer2.rgb ) ) );
  276. }
  277. // Convert back to linear space
  278. float3 cLinearOverlayResult = pow( cGammaOverlayResult.rgb, 2.2f );
  279. //*/
  280. float flLayer1Brightness = saturate( dot( cLayer1.rgb, float3( 0.333f, 0.334f, 0.333f ) ) );
  281. float flLayer2Brightness = saturate( dot( cLayer2.rgb, float3( 0.333f, 0.334f, 0.333f ) ) );
  282. // Modify layer 1 to be more contrasty
  283. cLayer1.rgb = saturate( cLayer1.rgb * cLayer1.rgb * 2.0f );
  284. float3 cLinearOverlayResult = cLayer1.rgb + cLayer2.rgb * saturate( 1.0f - flLayer1Brightness * 2.0f );
  285. // Tonemap, fog, etc.
  286. return FinalOutput( float4( OutputColor( cLinearOverlayResult.rgb ), g_Alpha ), 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
  287. #endif
  288. }