From 13f3ee33bfdac7f0c39feeed2512cec8bce5f20a Mon Sep 17 00:00:00 2001 From: FrancescoC-Unity Date: Mon, 25 May 2020 17:40:19 +0200 Subject: [PATCH] Port --- .../Runtime/Utilities/ColorUtils.cs | 47 ++++++++++++++----- .../ShaderLibrary/PhysicalCamera.hlsl | 22 +++++++-- .../CHANGELOG.md | 1 + .../Editor/PostProcessing/ExposureEditor.cs | 27 ++++++++++- .../Runtime/Debug/DebugDisplay.cs | 11 +++++ .../Runtime/Debug/DebugExposure.shader | 7 ++- .../Runtime/Debug/LightingDebug.cs | 2 + .../PostProcessing/Components/Exposure.cs | 42 +++++++++++++++++ .../PostProcessing/PostProcessSystem.cs | 7 ++- .../PostProcessing/Shaders/Exposure.compute | 10 ++-- .../Shaders/ExposureCommon.hlsl | 39 ++++++++------- .../Shaders/HistogramExposure.compute | 3 +- .../Shaders/HistogramExposureCommon.hlsl | 3 +- .../Runtime/RenderPipeline/Camera/HDCamera.cs | 18 +++++++ .../RenderPipeline/HDRenderPipeline.cs | 4 ++ 15 files changed, 195 insertions(+), 48 deletions(-) diff --git a/com.unity.render-pipelines.core/Runtime/Utilities/ColorUtils.cs b/com.unity.render-pipelines.core/Runtime/Utilities/ColorUtils.cs index 8a8e96fcdd7..01920973d81 100644 --- a/com.unity.render-pipelines.core/Runtime/Utilities/ColorUtils.cs +++ b/com.unity.render-pipelines.core/Runtime/Utilities/ColorUtils.cs @@ -7,6 +7,28 @@ namespace UnityEngine.Rendering /// public static class ColorUtils { + /// + /// Calibration constant (K) used for our virtual reflected light meter. Modifying this will lead to a change on how average scene luminance + /// gets mapped to exposure. + /// + static public float s_LightMeterCalibrationConstant = 12.5f; + + /// + /// Factor used for our lens system w.r.t. exposure calculation. Modifying this will lead to a change on how linear exposure + /// multipliers are computed from EV100 values (and viceversa). s_LensAttenuation models transmission attenuation and lens vignetting. + /// Note that according to the standard ISO 12232, a lens saturates at s_LensAttenuation = 0.78f (under ISO 100). + /// + static public float s_LensAttenuation = 0.65f; + + /// + /// Scale applied to exposure caused by lens imperfection. It is computed from s_LensAttenuation as follow: + /// (78 / ( S * q )) where S = 100 and q = s_LensAttenuation + /// + static public float lensImperfectionExposureScale + { + get => (78.0f / (100.0f * s_LensAttenuation)); + } + /// /// An analytical model of chromaticity of the standard illuminant, by Judd et al. /// http://en.wikipedia.org/wiki/Standard_illuminant#Illuminant_series_D @@ -215,10 +237,10 @@ public static float ConvertEV100ToExposure(float EV100) // Compute the maximum luminance possible with H_sbs sensitivity // maxLum = 78 / ( S * q ) * N^2 / t // = 78 / ( S * q ) * 2^ EV_100 - // = 78 / (100 * 0.65) * 2^ EV_100 - // = 1.2 * 2^ EV + // = 78 / (100 * s_LensAttenuation) * 2^ EV_100 + // = lensImperfectionExposureScale * 2^ EV // Reference: http://en.wikipedia.org/wiki/Film_speed - float maxLuminance = 1.2f * Mathf.Pow(2.0f, EV100); + float maxLuminance = lensImperfectionExposureScale * Mathf.Pow(2.0f, EV100); return 1.0f / maxLuminance; } @@ -231,10 +253,10 @@ public static float ConvertExposureToEV100(float exposure) { // Compute the maximum luminance possible with H_sbs sensitivity // EV_100 = log2( S * q / (78 * exposure) ) - // = log2( 100 * 0.65 / (78 * exposure) ) - // = log2( 1.0f / (1.2 * exposure) ) + // = log2( 100 * s_LensAttenuation / (78 * exposure) ) + // = log2( 1.0f / (lensImperfectionExposureScale * exposure) ) // Reference: http://en.wikipedia.org/wiki/Film_speed - return Mathf.Log(1.0f / (1.2f * exposure), 2.0f); + return Mathf.Log(1.0f / (lensImperfectionExposureScale * exposure), 2.0f); } /// @@ -244,13 +266,14 @@ public static float ConvertExposureToEV100(float exposure) /// An exposure value, in EV100. public static float ComputeEV100FromAvgLuminance(float avgLuminance) { - // We later use the middle gray at 12.7% in order to have + // The middle grey used will be determined by the s_LightMeterCalibrationConstant. + // The suggested (ISO 2720) range is 10.64 to 13.4. Common values used by + // manufacturers range from 11.37 to 14. Ref: https://en.wikipedia.org/wiki/Light_meter + // The default is 12.5% as it is the closest to 12.7% in order to have // a middle gray at 18% with a sqrt(2) room for specular highlights - // But here we deal with the spot meter measuring the middle gray - // which is fixed at 12.5 for matching standard camera - // constructor settings (i.e. calibration constant K = 12.5) - // Reference: http://en.wikipedia.org/wiki/Film_speed - const float K = 12.5f; // Reflected-light meter calibration constant + // Note that this gives equivalent results as using an incident light meter + // with a calibration constant of C=314. + float K = s_LightMeterCalibrationConstant; return Mathf.Log(avgLuminance * 100f / K, 2f); } diff --git a/com.unity.render-pipelines.core/ShaderLibrary/PhysicalCamera.hlsl b/com.unity.render-pipelines.core/ShaderLibrary/PhysicalCamera.hlsl index 72560c6b6c1..93f08de59ac 100644 --- a/com.unity.render-pipelines.core/ShaderLibrary/PhysicalCamera.hlsl +++ b/com.unity.render-pipelines.core/ShaderLibrary/PhysicalCamera.hlsl @@ -21,6 +21,12 @@ float ComputeEV100(float aperture, float shutterSpeed, float ISO) return log2((aperture * aperture) / shutterSpeed * 100.0 / ISO); } +float ComputeEV100FromAvgLuminance(float avgLuminance, float calibrationConstant) +{ + const float K = calibrationConstant; + return log2(avgLuminance * 100.0 / K); +} + float ComputeEV100FromAvgLuminance(float avgLuminance) { // We later use the middle gray at 12.7% in order to have @@ -30,21 +36,27 @@ float ComputeEV100FromAvgLuminance(float avgLuminance) // constructor settings (i.e. calibration constant K = 12.5) // Reference: http://en.wikipedia.org/wiki/Film_speed const float K = 12.5; // Reflected-light meter calibration constant - return log2(avgLuminance * 100.0 / K); + return ComputeEV100FromAvgLuminance(avgLuminance, K); } -float ConvertEV100ToExposure(float EV100) +float ConvertEV100ToExposure(float EV100, float exposureScale) { // Compute the maximum luminance possible with H_sbs sensitivity // maxLum = 78 / ( S * q ) * N^2 / t // = 78 / ( S * q ) * 2^ EV_100 - // = 78 / (100 * 0.65) * 2^ EV_100 - // = 1.2 * 2^ EV + // = 78 / (100 * s_LensAttenuation) * 2^ EV_100 + // = exposureScale * 2^ EV // Reference: http://en.wikipedia.org/wiki/Film_speed - float maxLuminance = 1.2 * pow(2.0, EV100); + float maxLuminance = exposureScale * pow(2.0, EV100); return 1.0 / maxLuminance; } +float ConvertEV100ToExposure(float EV100) +{ + const float exposureScale = 1.2; + return ConvertEV100ToExposure(EV100, exposureScale); +} + float ComputeISO(float aperture, float shutterSpeed, float targetEV100) { // Compute the required ISO to reach the target EV100 diff --git a/com.unity.render-pipelines.high-definition/CHANGELOG.md b/com.unity.render-pipelines.high-definition/CHANGELOG.md index 3d11f0a06ac..7217dcb63b5 100644 --- a/com.unity.render-pipelines.high-definition/CHANGELOG.md +++ b/com.unity.render-pipelines.high-definition/CHANGELOG.md @@ -130,6 +130,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Added CustomPassUtils API to simplify Blur, Copy and DrawRenderers custom passes. - Added Histogram guided automatic exposure. - Added few exposure debug modes. +- Added custom target mid grey for auto exposure. ### Fixed - Fix when rescale probe all direction below zero (1219246) diff --git a/com.unity.render-pipelines.high-definition/Editor/PostProcessing/ExposureEditor.cs b/com.unity.render-pipelines.high-definition/Editor/PostProcessing/ExposureEditor.cs index 35c7c585bbe..e5883dcc709 100644 --- a/com.unity.render-pipelines.high-definition/Editor/PostProcessing/ExposureEditor.cs +++ b/com.unity.render-pipelines.high-definition/Editor/PostProcessing/ExposureEditor.cs @@ -26,6 +26,12 @@ sealed class ExposureEditor : VolumeComponentEditor SerializedDataParameter m_HistogramPercentages; SerializedDataParameter m_HistogramCurveRemapping; + SerializedDataParameter m_TargetMidGray; + + static readonly string[] s_MidGrayNames = { "Grey 12.5%", "Grey 14.0%", "Grey 18.0%" }; + + public override bool hasAdvancedMode => true; + public override void OnEnable() { var o = new PropertyFetcher(serializedObject); @@ -48,7 +54,7 @@ public override void OnEnable() m_HistogramPercentages = Unpack(o.Find(x => x.histogramPercentages)); m_HistogramCurveRemapping = Unpack(o.Find(x => x.histogramUseCurveRemapping)); - + m_TargetMidGray = Unpack(o.Find(x => x.targetMidGray)); } public override void OnInspectorGUI() @@ -108,6 +114,25 @@ public override void OnInspectorGUI() PropertyField(m_AdaptationSpeedDarkToLight, EditorGUIUtility.TrTextContent("Speed Dark to Light")); PropertyField(m_AdaptationSpeedLightToDark, EditorGUIUtility.TrTextContent("Speed Light to Dark")); } + + if (isInAdvancedMode) + { + EditorGUILayout.Space(); + + using (new EditorGUILayout.HorizontalScope()) + { + // Override checkbox + DrawOverrideCheckbox(m_TargetMidGray); + + // Property + using (new EditorGUI.DisabledScope(!m_TargetMidGray.overrideState.boolValue)) + { + // Default unity field + m_TargetMidGray.value.intValue = EditorGUILayout.Popup(EditorGUIUtility.TrTextContent("Target Mid Grey", "Sets the desired Mid gray level used by the auto exposure (i.e. to what grey value the auto exposure system maps the average scene luminance)."), + m_TargetMidGray.value.intValue, s_MidGrayNames); + } + } + } } } } diff --git a/com.unity.render-pipelines.high-definition/Runtime/Debug/DebugDisplay.cs b/com.unity.render-pipelines.high-definition/Runtime/Debug/DebugDisplay.cs index 27f0ff26983..e7e64927d8c 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Debug/DebugDisplay.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/Debug/DebugDisplay.cs @@ -921,6 +921,17 @@ void RegisterLightingDebug() }); } + exposureFoldout.children.Add( + new DebugUI.FloatField + { + displayName = "Debug Lens Attenuation", + getter = () => Mathf.Clamp01(data.lightingDebugSettings.debugLensAttenuation), + setter = value => data.lightingDebugSettings.debugLensAttenuation = Mathf.Clamp01(value), + min = () => 0.1f, + max = () => 0.78f + + }); + exposureFoldout.children.Add( new DebugUI.FloatField { diff --git a/com.unity.render-pipelines.high-definition/Runtime/Debug/DebugExposure.shader b/com.unity.render-pipelines.high-definition/Runtime/Debug/DebugExposure.shader index d0e4440fcba..de1e4b3cc07 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Debug/DebugExposure.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Debug/DebugExposure.shader @@ -3,7 +3,6 @@ Shader "Hidden/HDRP/DebugExposure" HLSLINCLUDE #include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Components/Tonemapping.cs.hlsl" - #include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/ExposureCommon.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/HistogramExposureCommon.hlsl" #define DEBUG_DISPLAY #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Debug/DebugDisplay.hlsl" @@ -93,7 +92,7 @@ Shader "Hidden/HDRP/DebugExposure" float GetEVAtLocation(float2 uv) { - return ComputeEV100FromAvgLuminance(max(SampleLuminance(uv), 1e-4)); + return ComputeEV100FromAvgLuminance(max(SampleLuminance(uv), 1e-4), MeterCalibrationConstant); } // Returns true if it drew the location of the indicator. @@ -449,9 +448,9 @@ Shader "Hidden/HDRP/DebugExposure" } int displayTextOffsetX = DEBUG_FONT_TEXT_WIDTH; - uint2 textLocation = uint2(_MousePixelCoord.x + displayTextOffsetX, _MousePixelCoord.y); + int2 textLocation = int2(_MousePixelCoord.x + displayTextOffsetX, _MousePixelCoord.y); DrawFloatExplicitPrecision(indicatorEV, textColor, unormCoord, 1, textLocation, outputColor.rgb); - textLocation = uint2(_MousePixelCoord.xy); + textLocation = _MousePixelCoord.xy; DrawCharacter('X', float3(0.0f, 0.0f, 0.0f), unormCoord, textLocation, outputColor.rgb); return outputColor; diff --git a/com.unity.render-pipelines.high-definition/Runtime/Debug/LightingDebug.cs b/com.unity.render-pipelines.high-definition/Runtime/Debug/LightingDebug.cs index b45ea0a39f2..215cf773a45 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Debug/LightingDebug.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/Debug/LightingDebug.cs @@ -317,6 +317,8 @@ public bool IsDebugDisplayEnabled() public ExposureDebugMode exposureDebugMode = ExposureDebugMode.None; /// Exposure compensation to apply on current scene exposure. public float debugExposure = 0.0f; + /// Debug lens attenuation factor for the virtual camera. + public float debugLensAttenuation = 0.65f; /// Whether to show tonemap curve in the histogram debug view or not. public bool showTonemapCurveAlongHistogramView = true; diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Components/Exposure.cs b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Components/Exposure.cs index 7c942954e1e..6ef1b78c581 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Components/Exposure.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Components/Exposure.cs @@ -105,6 +105,13 @@ public sealed class Exposure : VolumeComponent, IPostProcessComponent [Tooltip("Sets whether histogram exposure mode will remap the computed exposure with a curve remapping (akin to Curve Remapping mode).")] public BoolParameter histogramUseCurveRemapping = new BoolParameter(false); + /// + /// Sets the desired Mid gray level used by the auto exposure (i.e. to what grey value the auto exposure system maps the average scene luminance). + /// Note that the lens model used in HDRP is not of a perfect lens, hence it will not map precisely to the selected value. + /// + [Tooltip("Sets the desired Mid gray level used by the auto exposure (i.e. to what grey value the auto exposure system maps the average scene luminance).")] + public TargetMidGrayParameter targetMidGray = new TargetMidGrayParameter(TargetMidGray.Grey125); + /// /// Tells if the effect needs to be rendered or not. /// @@ -205,6 +212,27 @@ public enum LuminanceSource ColorBuffer } + /// + /// The target grey value used by the exposure system. Note this is equivalent of changing the calibration constant K on the used virtual reflected light meter. + /// + public enum TargetMidGray + { + /// + /// Mid Grey 12.5% (reflected light meter K set as 12.5) + /// + Grey125, + + /// + /// Mid Grey 14.0% (reflected light meter K set as 14.0) + /// + Grey14, + + /// + /// Mid Grey 18.0% (reflected light meter K set as 18.0). Note that this value is outside of the suggested K range by the ISO standard. + /// + Grey18 + } + /// /// Methods that HDRP uses to change the exposure when the Camera moves from dark to light and vice versa. /// @@ -279,4 +307,18 @@ public sealed class AdaptationModeParameter : VolumeParameter /// The initial override state for the parameter. public AdaptationModeParameter(AdaptationMode value, bool overrideState = false) : base(value, overrideState) {} } + + /// + /// A that holds a value. + /// + [Serializable] + public sealed class TargetMidGrayParameter : VolumeParameter + { + /// + /// Creates a new instance. + /// + /// The initial value to store in the parameter. + /// The initial override state for the parameter. + public TargetMidGrayParameter(TargetMidGray value, bool overrideState = false) : base(value, overrideState) { } + } } diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs index dd7ce91f082..40521208935 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/PostProcessSystem.cs @@ -953,6 +953,8 @@ void DoDynamicExposure(CommandBuffer cmd, HDCamera camera, RTHandle colorBuffer) cmd.SetComputeIntParams(cs, HDShaderIDs._Variants, m_ExposureVariants); cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._PreviousExposureTexture, prevExposure); cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._SourceTexture, sourceTex); + cmd.SetComputeVectorParam(cs, HDShaderIDs._ExposureParams2, new Vector4(0.0f, 0.0f, ColorUtils.lensImperfectionExposureScale, ColorUtils.s_LightMeterCalibrationConstant)); + if (m_Exposure.meteringMode == MeteringMode.MaskWeighted && m_Exposure.weightTextureMask.value != null) { cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._ExposureWeightMask, m_Exposure.weightTextureMask.value); @@ -983,7 +985,7 @@ void DoDynamicExposure(CommandBuffer cmd, HDCamera camera, RTHandle colorBuffer) PrepareExposureCurveData(m_Exposure.curveMap.value, out float min, out float max); cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._ExposureCurveTexture, m_ExposureCurveTexture); cmd.SetComputeVectorParam(cs, HDShaderIDs._ExposureParams, new Vector4(m_Exposure.compensation.value + m_DebugExposureCompensation, min, max, 0f)); - cmd.SetComputeVectorParam(cs, HDShaderIDs._ExposureParams2, new Vector4(min, max, 0f, 0f)); + cmd.SetComputeVectorParam(cs, HDShaderIDs._ExposureParams2, new Vector4(min, max, ColorUtils.lensImperfectionExposureScale, ColorUtils.s_LightMeterCalibrationConstant)); m_ExposureVariants[3] = 2; } @@ -1041,6 +1043,7 @@ void DoHistogramBasedExposure(CommandBuffer cmd, HDCamera camera, RTHandle sourc // Now read the histogram kernel = cs.FindKernel("KHistogramReduce"); cmd.SetComputeVectorParam(cs, HDShaderIDs._ExposureParams, new Vector4(m_Exposure.compensation.value + m_DebugExposureCompensation, m_Exposure.limitMin.value, m_Exposure.limitMax.value, 0f)); + cmd.SetComputeVectorParam(cs, HDShaderIDs._ExposureParams2, new Vector4(0.0f, 0.0f, ColorUtils.lensImperfectionExposureScale, ColorUtils.s_LightMeterCalibrationConstant)); cmd.SetComputeVectorParam(cs, HDShaderIDs._AdaptationParams, new Vector4(m_Exposure.adaptationSpeedLightToDark.value, m_Exposure.adaptationSpeedDarkToLight.value, 0f, 0f)); cmd.SetComputeBufferParam(cs, kernel, HDShaderIDs._HistogramBuffer, m_HistogramBuffer); cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._PreviousExposureTexture, prevExposure); @@ -1051,7 +1054,7 @@ void DoHistogramBasedExposure(CommandBuffer cmd, HDCamera camera, RTHandle sourc if (m_Exposure.histogramUseCurveRemapping.value) { PrepareExposureCurveData(m_Exposure.curveMap.value, out float min, out float max); - cmd.SetComputeVectorParam(cs, HDShaderIDs._ExposureParams2, new Vector4(min, max, 0f, 0f)); + cmd.SetComputeVectorParam(cs, HDShaderIDs._ExposureParams2, new Vector4(min, max, ColorUtils.lensImperfectionExposureScale, ColorUtils.s_LightMeterCalibrationConstant)); m_ExposureVariants[3] = 2; } cmd.SetComputeIntParams(cs, HDShaderIDs._Variants, m_ExposureVariants); diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/Exposure.compute b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/Exposure.compute index 4e53910f142..1624800459b 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/Exposure.compute +++ b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/Exposure.compute @@ -22,7 +22,7 @@ void KFixedExposure(uint2 dispatchThreadId : SV_DispatchThreadID) { float ev100 = ParamEV100; ev100 -= ParamExposureCompensation; - _OutputTexture[dispatchThreadId] = float2(ConvertEV100ToExposure(ev100), ev100); + _OutputTexture[dispatchThreadId] = float2(ConvertEV100ToExposure(ev100, LensImperfectionExposureScale), ev100); } // @@ -34,7 +34,7 @@ void KManualCameraExposure(uint2 dispatchThreadId : SV_DispatchThreadID) { float ev100 = ComputeEV100(ParamAperture, ParamShutterSpeed, ParamISO); ev100 -= ParamExposureCompensation; - _OutputTexture[dispatchThreadId] = float2(ConvertEV100ToExposure(ev100), ev100); + _OutputTexture[dispatchThreadId] = float2(ConvertEV100ToExposure(ev100, LensImperfectionExposureScale), ev100); } // @@ -53,7 +53,7 @@ void KPrePass(uint2 dispatchThreadId : SV_DispatchThreadID) float weight = WeightSample(dispatchThreadId, PREPASS_TEX_SIZE.xx); - float logLuma = ComputeEV100FromAvgLuminance(max(luma, 1e-4)); + float logLuma = ComputeEV100FromAvgLuminance(max(luma, 1e-4), MeterCalibrationConstant); _OutputTexture[posInputs.positionSS] = float2(logLuma, weight); } @@ -120,7 +120,7 @@ void KReduction(uint2 groupId : SV_GroupID, uint2 groupThreadId : SV_GroupThread // Automatic float exposure = AdaptExposure(avgLuminance - ParamExposureCompensation); exposure = clamp(exposure, ParamExposureLimitMin, ParamExposureLimitMax); - _OutputTexture[groupId.xy] = float2(ConvertEV100ToExposure(exposure), exposure); + _OutputTexture[groupId.xy] = float2(ConvertEV100ToExposure(exposure, LensImperfectionExposureScale), exposure); break; } case 2u: @@ -128,7 +128,7 @@ void KReduction(uint2 groupId : SV_GroupID, uint2 groupThreadId : SV_GroupThread // Curve remapping float exposure = CurveRemap(avgLuminance); exposure = AdaptExposure(exposure - ParamExposureCompensation); - _OutputTexture[groupId.xy] = float2(ConvertEV100ToExposure(exposure), exposure); + _OutputTexture[groupId.xy] = float2(ConvertEV100ToExposure(exposure, LensImperfectionExposureScale), exposure); break; } default: diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/ExposureCommon.hlsl b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/ExposureCommon.hlsl index f12efaea4f9..6d80756323c 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/ExposureCommon.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/ExposureCommon.hlsl @@ -1,3 +1,6 @@ +#ifndef EXPOSURE_COMMON_INCLUDED +#define EXPOSURE_COMMON_INCLUDED + #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl" #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl" #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/PhysicalCamera.hlsl" @@ -18,21 +21,23 @@ float4 _AdaptationParams; uint4 _Variants; CBUFFER_END -#define ParamEV100 _ExposureParams.y -#define ParamExposureCompensation _ExposureParams.x -#define ParamAperture _ExposureParams.y -#define ParamShutterSpeed _ExposureParams.z -#define ParamISO _ExposureParams.w -#define ParamSpeedLightToDark _AdaptationParams.x -#define ParamSpeedDarkToLight _AdaptationParams.y -#define ParamExposureLimitMin _ExposureParams.y -#define ParamExposureLimitMax _ExposureParams.z -#define ParamCurveMin _ExposureParams2.x -#define ParamCurveMax _ExposureParams2.y -#define ParamSourceBuffer _Variants.x -#define ParamMeteringMode _Variants.y -#define ParamAdaptationMode _Variants.z -#define ParamEvaluateMode _Variants.w +#define ParamEV100 _ExposureParams.y +#define ParamExposureCompensation _ExposureParams.x +#define ParamAperture _ExposureParams.y +#define ParamShutterSpeed _ExposureParams.z +#define ParamISO _ExposureParams.w +#define ParamSpeedLightToDark _AdaptationParams.x +#define ParamSpeedDarkToLight _AdaptationParams.y +#define ParamExposureLimitMin _ExposureParams.y +#define ParamExposureLimitMax _ExposureParams.z +#define ParamCurveMin _ExposureParams2.x +#define ParamCurveMax _ExposureParams2.y +#define LensImperfectionExposureScale _ExposureParams2.z +#define MeterCalibrationConstant _ExposureParams2.w +#define ParamSourceBuffer _Variants.x +#define ParamMeteringMode _Variants.y +#define ParamAdaptationMode _Variants.z +#define ParamEvaluateMode _Variants.w float GetPreviousExposureEV100() { @@ -79,7 +84,7 @@ float SampleLuminance(float2 uv) if (ParamSourceBuffer == 1) { // Color buffer - float prevExposure = ConvertEV100ToExposure(GetPreviousExposureEV100()); + float prevExposure = ConvertEV100ToExposure(GetPreviousExposureEV100(), LensImperfectionExposureScale); float3 color = SAMPLE_TEXTURE2D_X_LOD(_SourceTexture, s_linear_clamp_sampler, uv, 0.0).xyz; return Luminance(color / prevExposure); } @@ -106,3 +111,5 @@ float CurveRemap(float inEV) float remap = saturate((inEV - ParamCurveMin) / (ParamCurveMax - ParamCurveMin)); return SAMPLE_TEXTURE2D_LOD(_ExposureCurveTexture, s_linear_clamp_sampler, float2(remap, 0.0), 0.0).x; } + +#endif diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/HistogramExposure.compute b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/HistogramExposure.compute index 43e5ccbdc54..5547b0d17a3 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/HistogramExposure.compute +++ b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/HistogramExposure.compute @@ -1,4 +1,3 @@ -#include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/ExposureCommon.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/HistogramExposureCommon.hlsl" @@ -173,7 +172,7 @@ void KHistogramReduce(uint3 dispatchThreadId : SV_DispatchThreadID) float exposure = AdaptExposure(avgEV - ParamExposureCompensation); exposure = clamp(exposure, ParamExposureLimitMin, ParamExposureLimitMax); - _OutputTexture[uint2(0, 0)] = float2(ConvertEV100ToExposure(exposure), exposure); + _OutputTexture[uint2(0, 0)] = float2(ConvertEV100ToExposure(exposure, LensImperfectionExposureScale), exposure); #ifdef OUTPUT_DEBUG_DATA _ExposureDebugTexture[uint2(0, 0)] = float2(avgEV - ParamExposureCompensation, 0.0f); #endif diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/HistogramExposureCommon.hlsl b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/HistogramExposureCommon.hlsl index 7620df3ec91..966c26650d1 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/HistogramExposureCommon.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/HistogramExposureCommon.hlsl @@ -1,3 +1,4 @@ +#include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/ExposureCommon.hlsl" #define HISTOGRAM_BINS 128 @@ -25,7 +26,7 @@ float UnpackWeight(uint val) float GetFractionWithinHistogram(float value) { - return ComputeEV100FromAvgLuminance(value) * _HistogramRangeScale + _HistogramRangeBias; + return ComputeEV100FromAvgLuminance(value, MeterCalibrationConstant) * _HistogramRangeScale + _HistogramRangeBias; } uint GetHistogramBinLocation(float value) diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs index e77c0825e69..2310ae6347a 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs @@ -1151,6 +1151,24 @@ void UpdateVolumeAndPhysicalParameters() { VolumeManager.instance.Update(volumeStack, volumeAnchor, volumeLayerMask); } + + // Update info about current target mid gray + TargetMidGray requestedMidGray = volumeStack.GetComponent().targetMidGray.value; + switch (requestedMidGray) + { + case TargetMidGray.Grey125: + ColorUtils.s_LightMeterCalibrationConstant = 12.5f; + break; + case TargetMidGray.Grey14: + ColorUtils.s_LightMeterCalibrationConstant = 14.0f; + break; + case TargetMidGray.Grey18: + ColorUtils.s_LightMeterCalibrationConstant = 18.0f; + break; + default: + ColorUtils.s_LightMeterCalibrationConstant = 12.5f; + break; + } } Matrix4x4 GetJitteredProjectionMatrix(Matrix4x4 origProj) diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs index 47ffa5a4dc9..b90d3b6f1ac 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs @@ -2137,6 +2137,9 @@ AOVRequestData aovRequest m_CurrentDebugDisplaySettings = m_DebugDisplaySettings; } + // Now that we have the display settings, we can setup the lens attenuation factor. + ColorUtils.s_LensAttenuation = m_CurrentDebugDisplaySettings.data.lightingDebugSettings.debugLensAttenuation; + aovRequest.SetupDebugData(ref m_CurrentDebugDisplaySettings); if (hdCamera.frameSettings.IsEnabled(FrameSettingsField.RayTracing)) @@ -4725,6 +4728,7 @@ static void RenderExposureDebug(in DebugParameters parameters, parameters.debugExposureMaterial.SetVector(HDShaderIDs._HistogramExposureParams, histogramParams); parameters.debugExposureMaterial.SetVector(HDShaderIDs._Variants, exposureVariants); parameters.debugExposureMaterial.SetVector(HDShaderIDs._ExposureParams, exposureParams); + parameters.debugExposureMaterial.SetVector(HDShaderIDs._ExposureParams2, new Vector4(0.0f, 0.0f, ColorUtils.lensImperfectionExposureScale, ColorUtils.s_LightMeterCalibrationConstant)); parameters.debugExposureMaterial.SetVector(HDShaderIDs._MousePixelCoord, HDUtils.GetMouseCoordinates(parameters.hdCamera)); parameters.debugExposureMaterial.SetTexture(HDShaderIDs._SourceTexture, inputColorBuffer); parameters.debugExposureMaterial.SetTexture(HDShaderIDs._DebugFullScreenTexture, postprocessedColorBuffer);