Files
UnrealEngineUWP/Engine/Shaders/PostProcessVisualizeHDR.usf
Martin Mittring 17c02942e4 fixed opengl shader compile
[CL 2316555 by Martin Mittring in Main branch]
2014-10-01 17:05:35 -04:00

194 lines
5.7 KiB
Plaintext

// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
/*=============================================================================
PostProcessVisualizeHDR.usf: PostProcessing shader to visualize HDR histogram
=============================================================================*/
#include "Common.usf"
#include "PostProcessCommon.usf"
#include "PostProcessHistogramCommon.usf"
#include "DeferredShadingCommon.usf"
#include "TonemapCommon.usf"
#include "MiniFontCommon.usf" // for PrintFloat()
// only needed for nice visualization
float ComputeHistogramMax(Texture2D HistogramTexture)
{
float Max = 0;
for(uint i = 0; i < HISTOGRAM_SIZE; ++i)
{
Max = max(Max, GetHistogramBucket(HistogramTexture, i));
}
return Max;
}
uint ComputeAdvice(float3 HDRColor)
{
float Lum = max(HDRColor.r, max(HDRColor.g, HDRColor.b));
if(Lum < EyeAdaptationParams[0].z)
{
return 1;
}
// for some reasons HLSL compiler seems to supress the return 1 when we comment in this code
// if(Lum > EyeAdaptationParams[0].w)
// {
// return 2;
// }
return 0;
}
uint ComputeAdviceUV(float2 UV)
{
float3 HDRColor = Texture2DSample(PostprocessInput2, PostprocessInput2Sampler, UV).rgb;
return ComputeAdvice(HDRColor);
}
// to highlight areas that have unrealistic materials
void HighlightAdvice(inout float3 OutColor, float2 UV, int2 PixelPos)
{
uint AdviceInner = ComputeAdviceUV(UV);
uint AdviceOuter = 0;
bool SpecialDotInArea = ((PixelPos.x + PixelPos.y) % 6) == 0 && ((PixelPos.x - PixelPos.y) % 6) == 0;
AdviceOuter = max(AdviceOuter, ComputeAdviceUV(UV + float2( 1, 0) * PostprocessInput0Size.zw));
AdviceOuter = max(AdviceOuter, ComputeAdviceUV(UV + float2( 0, 1) * PostprocessInput0Size.zw));
AdviceOuter = max(AdviceOuter, ComputeAdviceUV(UV + float2(-1, 0) * PostprocessInput0Size.zw));
AdviceOuter = max(AdviceOuter, ComputeAdviceUV(UV + float2( 0, -1) * PostprocessInput0Size.zw));
uint Advice = (AdviceInner == AdviceOuter && !SpecialDotInArea) ? 0 : AdviceOuter;
FLATTEN if(Advice)
{
FLATTEN if(Advice == 1)
{
// heavy shading cost
OutColor = float3(0, 0, 0.8f);
}
else
FLATTEN if(Advice == 2)
{
// warning
OutColor = float3(0.8f, 0.8f, 0);
}
else // if(Advice == 3)
{
// error
OutColor = float3(1, 0, 0);
}
}
}
//
void MainPS(float4 UVAndScreenPos : TEXCOORD0, out float4 OutColor : SV_Target0)
{
float2 UV = UVAndScreenPos.xy;
int2 PixelPos = (int2)(UVAndScreenPos.zw * ScreenPosToPixel.xy + ScreenPosToPixel.zw + 0.5f);
// background is the scene color
OutColor = Texture2DSample(PostprocessInput0, PostprocessInput0Sampler, UV);
// not fully functional yet
// HighlightAdvice(OutColor.rgb, UV, PixelPos);
// left top of the border
const int2 HistogramLeftTop = int2(64, ViewportRect.w - 128 - 32);
const int2 HistogramSize = int2(ViewportRect.z - ViewportRect.x - 64 * 2, 128);
const int HistogramOuterBorder = 4;
const float3 BorderColor = float3(0.5f, 0.5f, 0.5f);
float BorderDistance = ComputeDistanceToRect(PixelPos, HistogramLeftTop, HistogramSize);
// thin black border around the histogram
OutColor.xyz = lerp(float3(0, 0, 0), OutColor.xyz, saturate(BorderDistance - (HistogramOuterBorder + 2)));
// big solid border around the histogram
OutColor.xyz = lerp(BorderColor, OutColor.xyz, saturate(BorderDistance - (HistogramOuterBorder + 1)));
// thin black border around the histogram
OutColor.xyz = lerp(float3(0, 0, 0), OutColor.xyz, saturate(BorderDistance - 1));
if(BorderDistance > 0)
{
// outside of the histogram
return;
}
// inside the histogram
// (0, 0) .. (1, 1)
float2 InsetPx = PixelPos - HistogramLeftTop;
float2 InsetUV = InsetPx / HistogramSize;
uint Bucket = (uint)(InsetUV.x * HISTOGRAM_SIZE);
// Texture2D HistogramTexture = InputNew1;
#define HistogramTexture PostprocessInput1 // WAR (workaround) for HLSLCC not allowing assignment to samplers (yet)
float HistogramSum = ComputeHistogramSum(HistogramTexture);
if(InsetUV.x < ComputeHistogramPositionFromLuminance(EyeAdaptationParams[0].z))
{
// < min: grey
OutColor.xyz = lerp(OutColor.xyz, float3(0.5f, 0.5f, 0.5f), 0.5f);
}
else if(InsetUV.x < ComputeHistogramPositionFromLuminance(EyeAdaptationParams[0].w))
{
// >= min && < max: green
OutColor.xyz = lerp(OutColor.xyz, float3(0.5f, 0.8f, 0.5f), 0.5f);
}
else
{
// >= max: grey
OutColor.xyz = lerp(OutColor.xyz, float3(0.5f, 0.5f, 0.5f), 0.5f);
}
float LocalHistogramValue = GetHistogramBucket(HistogramTexture, Bucket) / ComputeHistogramMax(HistogramTexture);
if(LocalHistogramValue >= 1 - InsetUV.y)
{
// histogram bars
OutColor.xyz = lerp(OutColor.xyz, float3(0.5f, 0.5f, 0.5f), 0.5f);
}
{
// HDR luminance >0
float LuminanceVal = ComputeLuminanceFromHistogramPosition(InsetUV.x);
// HDR > 0
float3 AdpatedLuminance = LuminanceVal * HistogramTexture.Load(int3(0, 1, 0)).xxx;
// 0..1
float3 TonemappedLuminance = FilmPostProcess(AdpatedLuminance);
float3 DistMask = saturate(1.0 - 100.0 * abs(TonemappedLuminance - (1.0 - InsetUV.y)));
OutColor = lerp(OutColor, float4(1, 1, 1, 0), float4(DistMask, 0.0));
}
{
float ValuePx = ComputeHistogramPositionFromLuminance(ComputeEyeAdaptationExposure(HistogramTexture)) * HistogramSize.x;
if(abs(InsetPx.x - ValuePx) < 3)
{
// blue line to show the clamped percentil
OutColor = lerp(OutColor, float4(0, 0, 1, 0), 0.5f);
}
}
{
float Value = ComputeHistogramPositionFromLuminance(1.0f / HistogramTexture.Load(int3(0, 1, 0)).x);
PrintFloat(PixelPos, OutColor.xyz, float3(1, 1, 1), HistogramLeftTop + int2(HistogramSize.x - 7 * 8 - 2, 3), Value);
float ValuePx = Value * HistogramSize.x;
if(abs(InsetPx.x - ValuePx) < 2)
{
// white line to show the smoothed exposure
OutColor = lerp(OutColor, float4(1, 1, 1, 0), 1.0f);
}
}
}