S0N0S

>GeDoSaTo>assets>dx9>bloom.fx

Oct 24th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.24 KB | None | 0 0
  1. /*
  2. HDR Bloom Effect
  3. by Durante
  4. */
  5.  
  6. // These will be set by GeDoSaTo ////////////////////////////////////////////////////////
  7.  
  8. // e.g. (1/1920, 1/1080, 1920, 1080) at input size 1080p
  9. float4 inputPixelMetrics;
  10. // 1/[number of steps in the pyramid]
  11. float invSteps;
  12.  
  13. // Tweakable variables //////////////////////////////////////////////////////////////////
  14.  
  15. // values above this cutoff are considered to be highlights
  16. #ifndef CUTOFF
  17. #define CUTOFF 0.08
  18. #endif
  19.  
  20. // linearly increases the strength of the bloom effect
  21. #ifndef STRENGTH
  22. #define STRENGTH 7.0
  23. #endif
  24.  
  25. // strength of the "camera lens dirt" effect
  26. #ifndef DIRT_STRENGTH
  27. #define DIRT_STRENGTH 0.5
  28. #endif
  29.  
  30. // to what degree non-HDR can be darkened by HDR eye adjustment
  31. #ifndef MIX_FACTOR
  32. #define MIX_FACTOR 0.7
  33. #endif
  34.  
  35. // how much to progressively desaturate distant bloom
  36. #ifndef SATURATION
  37. #define SATURATION 0.93
  38. #endif
  39.  
  40. // maximum brightness for HDR input - to prevent e.g. signs from blooming too much
  41. #ifndef MAX_BRIGHTNESS
  42. #define MAX_BRIGHTNESS float4(0.7,0.7,0.7,1.0)
  43. #endif
  44.  
  45. // speed of eye adaption - larger number = quicker adaption
  46. #ifndef ADAPT_SPEED
  47. #define ADAPT_SPEED 0.015f
  48. #endif
  49.  
  50. /////////////////////////////////////////////////////////////////////////////////////////
  51.  
  52. texture2D sampleTex; // original texture
  53. texture2D passTex; // previous pass
  54. texture2D avgTex; // 1x1 averaged bloom strength
  55. texture2D dirtTex; // (static) dirt mask
  56.  
  57. sampler sampleSampler = sampler_state {
  58. Texture = <sampleTex>;
  59. MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR;
  60. AddressU = Clamp; AddressV = Clamp; SRGBTexture = TRUE;
  61. };
  62.  
  63. sampler passSampler = sampler_state {
  64. texture = <passTex>;
  65. MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR;
  66. AddressU = Clamp; AddressV = Clamp; SRGBTexture = FALSE;
  67. };
  68.  
  69. sampler dirtSampler = sampler_state {
  70. texture = <dirtTex>;
  71. MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR;
  72. AddressU = Clamp; AddressV = Clamp; SRGBTexture = FALSE;
  73. };
  74.  
  75. sampler avgSampler = sampler_state {
  76. texture = <avgTex>;
  77. MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR;
  78. AddressU = Clamp; AddressV = Clamp; SRGBTexture = FALSE;
  79. };
  80.  
  81. struct VSOUT {
  82. float4 vertPos : POSITION0;
  83. float2 UVCoord : TEXCOORD0;
  84. };
  85.  
  86. struct VSIN {
  87. float4 vertPos : POSITION0;
  88. float2 UVCoord : TEXCOORD0;
  89. };
  90.  
  91. VSOUT FrameVS(VSIN IN)
  92. {
  93. VSOUT OUT;
  94. float4 pos = float4(IN.vertPos.x, IN.vertPos.y, IN.vertPos.z, 1.0f);
  95. OUT.vertPos = pos;
  96. float2 coord = float2(IN.UVCoord.x, IN.UVCoord.y);
  97. OUT.UVCoord = coord;
  98. return OUT;
  99. }
  100.  
  101. float4 initialCutoffAndDownsampleShader(VSOUT IN) : COLOR0 {
  102. float4 cA = tex2D(sampleSampler, IN.UVCoord + float2(-inputPixelMetrics.x, -inputPixelMetrics.y)*0.5);
  103. float4 cB = tex2D(sampleSampler, IN.UVCoord + float2( inputPixelMetrics.x, -inputPixelMetrics.y)*0.5);
  104. float4 cC = tex2D(sampleSampler, IN.UVCoord + float2( inputPixelMetrics.x, inputPixelMetrics.y)*0.5);
  105. float4 cD = tex2D(sampleSampler, IN.UVCoord + float2(-inputPixelMetrics.x, inputPixelMetrics.y)*0.5);
  106. float4 m = max(cA, max(cB, max(cC, cD)));
  107. float4 cut = clamp(m-CUTOFF, float4(0,0,0,0), MAX_BRIGHTNESS);
  108. float3 luminanceWeights = float3(0.299,0.587,0.114);
  109. float luminance = dot(cut.xyz, luminanceWeights);
  110. return m*luminance;
  111. }
  112.  
  113. float4 HBlurShader(VSOUT IN) : COLOR0 {
  114. float4 color = tex2D(passSampler, IN.UVCoord);
  115.  
  116. float4 blurred = color*0.2270270270;
  117. blurred += tex2D(passSampler, IN.UVCoord + float2(inputPixelMetrics.x*(1.3846153846), 0) + inputPixelMetrics.xy*0.55) * 0.3162162162;
  118. blurred += tex2D(passSampler, IN.UVCoord - float2(inputPixelMetrics.x*(1.3846153846), 0) + inputPixelMetrics.xy*0.55) * 0.3162162162;
  119. blurred += tex2D(passSampler, IN.UVCoord + float2(inputPixelMetrics.x*(3.2307692308), 0) + inputPixelMetrics.xy*0.55) * 0.0702702703;
  120. blurred += tex2D(passSampler, IN.UVCoord - float2(inputPixelMetrics.x*(3.2307692308), 0) + inputPixelMetrics.xy*0.55) * 0.0702702703;
  121.  
  122. return blurred;
  123. }
  124.  
  125. float4 VBlurShader(VSOUT IN) : COLOR0 {
  126. float4 color = tex2D(passSampler, IN.UVCoord);
  127.  
  128. float4 blurred = color*0.2270270270;
  129. blurred += tex2D(passSampler, IN.UVCoord + float2(0, inputPixelMetrics.y*(1.3846153846)) + inputPixelMetrics.xy*0.55) * 0.3162162162;
  130. blurred += tex2D(passSampler, IN.UVCoord - float2(0, inputPixelMetrics.y*(1.3846153846)) + inputPixelMetrics.xy*0.55) * 0.3162162162;
  131. blurred += tex2D(passSampler, IN.UVCoord + float2(0, inputPixelMetrics.y*(3.2307692308)) + inputPixelMetrics.xy*0.55) * 0.0702702703;
  132. blurred += tex2D(passSampler, IN.UVCoord - float2(0, inputPixelMetrics.y*(3.2307692308)) + inputPixelMetrics.xy*0.55) * 0.0702702703;
  133.  
  134. float3 luminanceWeights = float3(0.299,0.587,0.114);
  135. float luminance = dot(blurred.rgb, luminanceWeights);
  136. blurred = lerp(luminance, blurred, SATURATION);
  137.  
  138. return blurred;
  139. }
  140.  
  141. float4 integrateUpwardsShader(VSOUT IN) : COLOR0 {
  142. return float4(tex2D(passSampler, IN.UVCoord).rgb, 1.0);
  143. }
  144.  
  145. float4 eyeAdaptionShader(VSOUT IN) : COLOR0 {
  146. float3 current = tex2D(avgSampler, float2(0.5,0.5)).rgb;
  147. return float4(current, ADAPT_SPEED);
  148. }
  149.  
  150. float4 finalComposeShader(VSOUT IN) : COLOR0 {
  151. float4 cBloom = tex2D(passSampler, IN.UVCoord);
  152. float4 color = tex2D(sampleSampler, IN.UVCoord);
  153. float4 avg = tex2D(avgSampler, float2(0.5,0.5));
  154. float totalPower = avg.r+avg.g+avg.b;
  155.  
  156. cBloom = cBloom * invSteps;
  157.  
  158. float4 dirt = tex2D(dirtSampler, IN.UVCoord);
  159. cBloom = cBloom + cBloom*dirt*DIRT_STRENGTH;
  160.  
  161. return MIX_FACTOR*color + ((1-MIX_FACTOR)*color+cBloom*STRENGTH)/(1.0+(totalPower*100));
  162. }
  163.  
  164. technique initialCutoffAndDownsample {
  165. pass initialCutoffAndDownsample {
  166. VertexShader = compile vs_3_0 FrameVS();
  167. PixelShader = compile ps_3_0 initialCutoffAndDownsampleShader();
  168. ZEnable = false; AlphaBlendEnable = false; AlphaTestEnable = false;
  169. ColorWriteEnable = RED|GREEN|BLUE;
  170. }
  171. }
  172.  
  173. technique gaussian {
  174. pass horizontal {
  175. VertexShader = compile vs_3_0 FrameVS();
  176. PixelShader = compile ps_3_0 HBlurShader();
  177. ZEnable = false; AlphaBlendEnable = false; AlphaTestEnable = false;
  178. ColorWriteEnable = RED|GREEN|BLUE;
  179. }
  180. pass vertical {
  181. VertexShader = compile vs_3_0 FrameVS();
  182. PixelShader = compile ps_3_0 VBlurShader();
  183. ZEnable = false; AlphaBlendEnable = false; AlphaTestEnable = false;
  184. ColorWriteEnable = RED|GREEN|BLUE;
  185. }
  186. }
  187.  
  188. technique integrateUpwards {
  189. pass integrate {
  190. VertexShader = compile vs_3_0 FrameVS();
  191. PixelShader = compile ps_3_0 integrateUpwardsShader();
  192. ZEnable = false; AlphaTestEnable = false;
  193. AlphaBlendEnable = true;
  194. BlendOp = ADD;
  195. SrcBlend = ONE;
  196. DestBlend = ONE;
  197. ColorWriteEnable = RED|GREEN|BLUE;
  198. }
  199. }
  200.  
  201. technique eyeAdaption {
  202. pass eyeAdaption {
  203. VertexShader = compile vs_3_0 FrameVS();
  204. PixelShader = compile ps_3_0 eyeAdaptionShader();
  205. ZEnable = false; AlphaTestEnable = false;
  206. AlphaBlendEnable = true;
  207. BlendOp = ADD;
  208. SrcBlend = SRCALPHA;
  209. DestBlend = INVSRCALPHA;
  210. ColorWriteEnable = RED|GREEN|BLUE;
  211. }
  212. }
  213.  
  214. technique finalCompose {
  215. pass finalCompose {
  216. VertexShader = compile vs_3_0 FrameVS();
  217. PixelShader = compile ps_3_0 finalComposeShader();
  218. ZEnable = false; AlphaBlendEnable = false; AlphaTestEnable = false;
  219. ColorWriteEnable = RED|GREEN|BLUE;
  220. SRGBWriteEnable = true;
  221. }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment