Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //---------------------------------------------------------------------------
- // Prepar3d - Shader Effect Files
- // Copyright (c) 2014, Lockheed Martin Corporation
- //---------------------------------------------------------------------------
- #include "ShaderMacros.h"
- #include "ConstantBuffers.fxh"
- #include "Quad.sh"
- //Cap max luminance to not blow out luminance mip chain based on a few pixels.
- #define MAX_LUMINANCE (2.0)
- #define BLOOM_BRIGHT_FILTER_SCALE (1)
- shared cbuffer cbHDRData : REGISTER(b, POST_PROCESS_CB_REGISTER)
- {
- float BloomThreshold;
- float BloomMagnitude;
- float BloomBlurSigma;
- float SaturationScalar;
- };
- Texture2D<float4> srcTex : register( t0 );
- Texture2D<float4> srcTex1 : register( t1 );
- shared StructuredBuffer<float> exposureBuffer: register(t2);
- //ACES based ToneMapping curve, takes and outputs linear values.
- float3 ToneMap(float3 color)
- {
- const float A = 2.51;
- const float B = 0.03;
- const float C = 2.43;
- const float D = 0.59;
- const float E = 0.14;
- return saturate((color * (A * color + B)) / (color * (C * color + D) + E));
- }
- float RGBToLuma(float3 colorLinear)
- {
- return dot(colorLinear, float3(0.212671, 0.715160, 0.072169));
- }
- // Calculates the gaussian blur weight for a given distance and sigmas
- float CalcGaussianWeight(int sampleDist, float sigma)
- {
- float g = 1.0f / sqrt(2.0f * 3.14159f * sigma * sigma);
- return (g * exp(-(sampleDist * sampleDist) / (2.0f * sigma * sigma)));
- }
- // Performs a gaussian blur in one direction
- float4 Blur( PsQuad vert, float2 texScale, float sigma )
- {
- float4 color = 0;
- float2 uTDim;
- srcTex.GetDimensions(uTDim.x, uTDim.y);
- [unroll]
- for (int i = -3; i < 3; i++)
- {
- float weight = CalcGaussianWeight(i, sigma);
- float2 texCoord = vert.texcoord;
- texCoord += ((float)i / uTDim) * texScale;
- float4 sample = srcTex.Sample(samClamp, texCoord);
- color += sample * weight;
- }
- return color;
- }
- // Uses a lower exposure to produce a value suitable for a bloom pass.
- float4 Threshold( PsQuad vert ) : SV_Target
- {
- float3 color = srcTex.Sample(samClamp, vert.texcoord).rgb;
- float luma = RGBToLuma(color);
- //Scale color.
- float scaledThreshold = BloomThreshold / exposureBuffer[0];
- color *= max(1e-5f, luma - scaledThreshold) / (luma + 1e-5f);
- color *= 1.0 / (luma + BLOOM_BRIGHT_FILTER_SCALE);
- return float4(color, 1.0f);
- }
- // Uses hw bilinear filtering for upscaling or downscaling
- float4 Scale( PsQuad vert ) : SV_Target
- {
- return srcTex.Sample( samClamp, vert.texcoord );
- }
- // Uses hw bilinear filtering for upscaling or downscaling
- float4 CombineScale( PsQuad vert ) : SV_Target
- {
- return (srcTex.Sample( samClamp, vert.texcoord ) + srcTex1.Sample( samClamp, vert.texcoord ));
- }
- // Horizontal gaussian blur
- float4 BloomBlurH( PsQuad vert ) : SV_Target
- {
- return Blur( vert, float2(1, 0), BloomBlurSigma );
- }
- // Vertical gaussian blur
- float4 BloomBlurV( PsQuad vert ) : SV_Target
- {
- return Blur( vert, float2(0, 1), BloomBlurSigma);
- }
- // Applies exposure and tone mapping to the input, and combines it with the
- // results of the bloom pass
- float4 Composite(PsQuad vert) : SV_Target
- {
- //Calculate the exposure.
- float exposure = exposureBuffer[0];
- //Get the HDR color.
- float4 color = srcTex.Sample(samClamp, vert.texcoord);
- //Apply bloom and exposure.
- if (BloomMagnitude > 0)
- {
- color.rgb += BloomMagnitude * srcTex1.Sample(samClamp, vert.texcoord).rgb;
- }
- color.rgb *= exposure;
- //Tone map.
- color.rgb = ToneMap(color.rgb);
- //Saturate the final color if desired.
- float3 finalColor = lerp(RGBToLuma(color.rgb), color.rgb, SaturationScalar);
- //Linear to sRGB happening on write into render target.
- return float4(finalColor, color.a);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement