Advertisement
Guest User

HDR.hlsl

a guest
Nov 23rd, 2020
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.86 KB | None | 0 0
  1. //---------------------------------------------------------------------------
  2. // Prepar3d - Shader Effect Files
  3. // Copyright (c) 2014, Lockheed Martin Corporation
  4. //---------------------------------------------------------------------------
  5.  
  6. #include "ShaderMacros.h"
  7. #include "ConstantBuffers.fxh"
  8. #include "Quad.sh"
  9.  
  10. //Cap max luminance to not blow out luminance mip chain based on a few pixels.
  11. #define MAX_LUMINANCE (2.0)
  12. #define BLOOM_BRIGHT_FILTER_SCALE (1)
  13.  
  14. shared cbuffer cbHDRData : REGISTER(b, POST_PROCESS_CB_REGISTER)
  15. {
  16.     float BloomThreshold;
  17.     float BloomMagnitude;
  18.     float BloomBlurSigma;
  19.     float SaturationScalar;
  20. };
  21.  
  22. Texture2D<float4> srcTex : register( t0 );
  23. Texture2D<float4> srcTex1 : register( t1 );
  24. shared StructuredBuffer<float> exposureBuffer: register(t2);
  25.  
  26. //ACES based ToneMapping curve, takes and outputs linear values.
  27. float3 ToneMap(float3 color)
  28. {  
  29.     const float A = 2.51;
  30.     const float B = 0.03;
  31.     const float C = 2.43;
  32.     const float D = 0.59;
  33.     const float E = 0.14;
  34.     return saturate((color * (A * color + B)) / (color * (C * color + D) + E));
  35. }
  36.  
  37. float RGBToLuma(float3 colorLinear)
  38. {
  39.     return dot(colorLinear, float3(0.212671, 0.715160, 0.072169));
  40. }
  41.  
  42. // Calculates the gaussian blur weight for a given distance and sigmas
  43. float CalcGaussianWeight(int sampleDist, float sigma)
  44. {
  45.     float g = 1.0f / sqrt(2.0f * 3.14159f * sigma * sigma);  
  46.     return (g * exp(-(sampleDist * sampleDist) / (2.0f * sigma * sigma)));
  47. }
  48.  
  49. // Performs a gaussian blur in one direction
  50. float4 Blur( PsQuad vert, float2 texScale, float sigma )
  51. {
  52.     float4 color = 0;
  53.     float2 uTDim;
  54.     srcTex.GetDimensions(uTDim.x, uTDim.y);
  55.  
  56.     [unroll]
  57.     for (int i = -3; i < 3; i++)
  58.     {  
  59.         float weight = CalcGaussianWeight(i, sigma);
  60.         float2 texCoord = vert.texcoord;
  61.         texCoord += ((float)i / uTDim) * texScale;
  62.         float4 sample = srcTex.Sample(samClamp, texCoord);
  63.         color += sample * weight;
  64.     }
  65.  
  66.     return color;
  67. }
  68.  
  69. // Uses a lower exposure to produce a value suitable for a bloom pass.
  70. float4 Threshold( PsQuad vert ) : SV_Target
  71. {            
  72.     float3 color = srcTex.Sample(samClamp, vert.texcoord).rgb;
  73.     float luma = RGBToLuma(color);
  74.  
  75.     //Scale color.
  76.     float scaledThreshold = BloomThreshold / exposureBuffer[0];
  77.     color *= max(1e-5f, luma - scaledThreshold) / (luma + 1e-5f);
  78.     color *= 1.0 / (luma + BLOOM_BRIGHT_FILTER_SCALE);
  79.  
  80.     return float4(color, 1.0f);
  81. }
  82.  
  83. // Uses hw bilinear filtering for upscaling or downscaling
  84. float4 Scale( PsQuad vert ) : SV_Target
  85. {
  86.     return srcTex.Sample( samClamp, vert.texcoord );
  87. }
  88.  
  89. // Uses hw bilinear filtering for upscaling or downscaling
  90. float4 CombineScale( PsQuad vert ) : SV_Target
  91. {
  92.     return (srcTex.Sample( samClamp, vert.texcoord ) + srcTex1.Sample( samClamp, vert.texcoord ));
  93. }
  94.  
  95. // Horizontal gaussian blur
  96. float4 BloomBlurH( PsQuad vert ) : SV_Target
  97. {
  98.     return Blur( vert, float2(1, 0), BloomBlurSigma );
  99. }
  100.  
  101. // Vertical gaussian blur
  102. float4 BloomBlurV( PsQuad vert ) : SV_Target
  103. {
  104.     return Blur( vert, float2(0, 1), BloomBlurSigma);
  105. }
  106.  
  107. // Applies exposure and tone mapping to the input, and combines it with the
  108. // results of the bloom pass
  109. float4 Composite(PsQuad vert) : SV_Target
  110. {
  111.     //Calculate the exposure.
  112.     float exposure = exposureBuffer[0];
  113.  
  114.     //Get the HDR color.
  115.     float4 color = srcTex.Sample(samClamp, vert.texcoord);
  116.  
  117.     //Apply bloom and exposure.
  118.     if (BloomMagnitude > 0)
  119.     {
  120.         color.rgb += BloomMagnitude * srcTex1.Sample(samClamp, vert.texcoord).rgb;
  121.     }
  122.     color.rgb *= exposure;
  123.    
  124.     //Tone map.
  125.     color.rgb = ToneMap(color.rgb);
  126.  
  127.     //Saturate the final color if desired.
  128.     float3 finalColor = lerp(RGBToLuma(color.rgb), color.rgb, SaturationScalar);
  129.    
  130.     //Linear to sRGB happening on write into render target.
  131.     return float4(finalColor, color.a);
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement