using System;
namespace UnityEngine.Rendering
{
///
/// Utility class for outputting to an HDR display.
///
public static class HDROutputUtils
{
/// HDR color operations that the shader applies.
[Flags]
public enum Operation
{
/// Do not perform operations specific to HDR output.
None = 0,
/// Convert colors to the color space of the HDR display.
ColorConversion = 1 << 0,
/// Encode colors with the transfer function corresponding to the HDR display.
ColorEncoding = 1 << 1
}
///
/// This struct Provides access to HDR display settings and information.
///
public struct HDRDisplayInformation
{
///
/// Constructs HDR Display settings.
///
/// Maximum input luminance at which gradation is preserved even when the entire screen is bright.
/// Maximum input luminance at which gradation is preserved when 10% of the screen is bright.
/// Minimum input luminance at which gradation is identifiable.
/// The base luminance of a white paper surface in nits or candela per square meter.
public HDRDisplayInformation(int maxFullFrameToneMapLuminance, int maxToneMapLuminance, int minToneMapLuminance, float hdrPaperWhiteNits)
{
this.maxFullFrameToneMapLuminance = maxFullFrameToneMapLuminance;
this.maxToneMapLuminance = maxToneMapLuminance;
this.minToneMapLuminance = minToneMapLuminance;
this.paperWhiteNits = hdrPaperWhiteNits;
}
/// Maximum input luminance at which gradation is preserved even when the entire screen is bright.
public int maxFullFrameToneMapLuminance;
/// Maximum input luminance at which gradation is preserved when 10% of the screen is bright.
public int maxToneMapLuminance;
/// Minimum input luminance at which gradation is identifiable.
public int minToneMapLuminance;
/// The base luminance of a white paper surface in nits or candela per square meter.
public float paperWhiteNits;
}
/// Shader keywords for communicating with the HDR Output shader implementation.
public static class ShaderKeywords
{
/// Keyword string for converting to the correct output color space.
public const string HDR_COLORSPACE_CONVERSION = "HDR_COLORSPACE_CONVERSION";
/// Keyword string for applying the color encoding.
public const string HDR_ENCODING = "HDR_ENCODING";
/// Keyword string for converting to the correct output color space and applying the color encoding.
public const string HDR_COLORSPACE_CONVERSION_AND_ENCODING = "HDR_COLORSPACE_CONVERSION_AND_ENCODING";
/// Keyword string to enable when a shader must be aware the input color space is in nits HDR range.
public const string HDR_INPUT = "HDR_INPUT";
/// Keyword for converting to the correct output color space.
internal static readonly ShaderKeyword HDRColorSpaceConversion = new ShaderKeyword(HDR_COLORSPACE_CONVERSION);
/// Keyword for applying the color encoding.
internal static readonly ShaderKeyword HDREncoding = new ShaderKeyword(HDR_ENCODING);
/// Keyword for converting to the correct output color space and applying the color encoding.
internal static readonly ShaderKeyword HDRColorSpaceConversionAndEncoding = new ShaderKeyword(HDR_COLORSPACE_CONVERSION_AND_ENCODING);
/// Keyword to enable when a shader must be aware the input color space is in nits HDR range.
internal static readonly ShaderKeyword HDRInput = new ShaderKeyword(HDR_INPUT);
}
static class ShaderPropertyId
{
public static readonly int hdrColorSpace = Shader.PropertyToID("_HDRColorspace");
public static readonly int hdrEncoding = Shader.PropertyToID("_HDREncoding");
}
///
/// Extracts the color space part of the ColorGamut
///
/// Color gamut (a combination of color space and encoding) queried from the device.
/// The HDRColorspace value the color gamut contains as an int.
/// Returns true if there was a valid HDRColorspace for the ColorGamut, false otherwise
public static bool GetColorSpaceForGamut(ColorGamut gamut, out int colorspace)
{
WhitePoint whitePoint = ColorGamutUtility.GetWhitePoint(gamut);
if (whitePoint != WhitePoint.D65)
{
Debug.LogWarningFormat("{0} white point is currently unsupported for outputting to HDR.", gamut.ToString());
colorspace = -1;
return false;
}
ColorPrimaries primaries = ColorGamutUtility.GetColorPrimaries(gamut);
switch (primaries)
{
case ColorPrimaries.Rec709:
colorspace = (int)HDRColorspace.Rec709;
return true;
case ColorPrimaries.Rec2020:
colorspace = (int)HDRColorspace.Rec2020;
return true;
case ColorPrimaries.P3:
colorspace = (int)HDRColorspace.P3D65;
return true;
default:
Debug.LogWarningFormat("{0} color space is currently unsupported for outputting to HDR.", gamut.ToString());
colorspace = -1;
return false;
}
}
///
/// Extracts the encoding part of the ColorGamut
///
/// Color gamut (a combination of color space and encoding) queried from the device.
/// The HDREncoding value the color gamut contains as an int.
/// Returns true if there was a valid HDREncoding for the ColorGamut, false otherwise
public static bool GetColorEncodingForGamut(ColorGamut gamut, out int encoding)
{
TransferFunction transferFunction = ColorGamutUtility.GetTransferFunction(gamut);
switch (transferFunction)
{
case TransferFunction.Linear:
encoding = (int)HDREncoding.Linear;
return true;
case TransferFunction.PQ:
encoding = (int)HDREncoding.PQ;
return true;
case TransferFunction.Gamma22:
encoding = (int)HDREncoding.Gamma22;
return true;
case TransferFunction.sRGB:
encoding = (int)HDREncoding.sRGB;
return true;
default:
Debug.LogWarningFormat("{0} color encoding is currently unsupported for outputting to HDR.", gamut.ToString());
encoding = -1;
return false;
}
}
///
/// Configures the Material keywords to use HDR output parameters.
///
/// The Material used with HDR output.
/// Color gamut (a combination of color space and encoding) queried from the device.
/// HDR color operations the shader applies.
public static void ConfigureHDROutput(Material material, ColorGamut gamut, Operation operations)
{
int colorSpace;
int encoding;
if (!GetColorSpaceForGamut(gamut, out colorSpace) || !GetColorEncodingForGamut(gamut, out encoding))
return; // only exit here if there is an error or unsupported mode
material.SetInteger(ShaderPropertyId.hdrColorSpace, colorSpace);
material.SetInteger(ShaderPropertyId.hdrEncoding, encoding);
CoreUtils.SetKeyword(material, ShaderKeywords.HDRColorSpaceConversionAndEncoding.name, operations.HasFlag(Operation.ColorConversion) && operations.HasFlag(Operation.ColorEncoding));
CoreUtils.SetKeyword(material, ShaderKeywords.HDREncoding.name, operations.HasFlag(Operation.ColorEncoding) && !operations.HasFlag(Operation.ColorConversion));
CoreUtils.SetKeyword(material, ShaderKeywords.HDRColorSpaceConversion.name, operations.HasFlag(Operation.ColorConversion) && !operations.HasFlag(Operation.ColorEncoding));
// Optimizing shader variants: define HDR_INPUT only if HDR_COLORSPACE_CONVERSION and HDR_ENCODING were not previously defined
CoreUtils.SetKeyword(material, ShaderKeywords.HDRInput.name, operations == Operation.None);
}
///
/// Configures the Material Property Block variables to use HDR output parameters.
///
/// The Material Property Block used with HDR output.
/// Color gamut (a combination of color space and encoding) queried from the device.
public static void ConfigureHDROutput(MaterialPropertyBlock properties, ColorGamut gamut)
{
int colorSpace;
int encoding;
if (!GetColorSpaceForGamut(gamut, out colorSpace) || !GetColorEncodingForGamut(gamut, out encoding))
return;
properties.SetInteger(ShaderPropertyId.hdrColorSpace, colorSpace);
properties.SetInteger(ShaderPropertyId.hdrEncoding, encoding);
}
///
/// Configures the Material keywords to use HDR output parameters.
///
/// The Material used with HDR output.
/// HDR color operations the shader applies.
public static void ConfigureHDROutput(Material material, Operation operations)
{
CoreUtils.SetKeyword(material, ShaderKeywords.HDRColorSpaceConversionAndEncoding.name, operations.HasFlag(Operation.ColorConversion) && operations.HasFlag(Operation.ColorEncoding));
CoreUtils.SetKeyword(material, ShaderKeywords.HDREncoding.name, operations.HasFlag(Operation.ColorEncoding) && !operations.HasFlag(Operation.ColorConversion));
CoreUtils.SetKeyword(material, ShaderKeywords.HDRColorSpaceConversion.name, operations.HasFlag(Operation.ColorConversion) && !operations.HasFlag(Operation.ColorEncoding));
// Optimizing shader variants: define HDR_INPUT only if HDR_COLORSPACE_CONVERSION and HDR_ENCODING were not previously defined
CoreUtils.SetKeyword(material, ShaderKeywords.HDRInput.name, operations == Operation.None);
}
///
/// Configures the compute shader keywords to use HDR output parameters.
///
/// The compute shader used with HDR output.
/// Color gamut (a combination of color space and encoding) queried from the device.
/// HDR color operations the shader applies.
public static void ConfigureHDROutput(ComputeShader computeShader, ColorGamut gamut, Operation operations)
{
int colorSpace;
int encoding;
if (!GetColorSpaceForGamut(gamut, out colorSpace) || !GetColorEncodingForGamut(gamut, out encoding))
return; // only exit here if there is an error or unsupported mode
computeShader.SetInt(ShaderPropertyId.hdrColorSpace, colorSpace);
computeShader.SetInt(ShaderPropertyId.hdrEncoding, encoding);
CoreUtils.SetKeyword(computeShader, ShaderKeywords.HDRColorSpaceConversionAndEncoding.name, operations.HasFlag(Operation.ColorConversion) && operations.HasFlag(Operation.ColorEncoding));
CoreUtils.SetKeyword(computeShader, ShaderKeywords.HDREncoding.name, operations.HasFlag(Operation.ColorEncoding) && !operations.HasFlag(Operation.ColorConversion));
CoreUtils.SetKeyword(computeShader, ShaderKeywords.HDRColorSpaceConversion.name, operations.HasFlag(Operation.ColorConversion) && !operations.HasFlag(Operation.ColorEncoding));
// Optimizing shader variants: define HDR_INPUT only if HDR_COLORSPACE_CONVERSION and HDR_ENCODING were not previously defined
CoreUtils.SetKeyword(computeShader, ShaderKeywords.HDRInput.name, operations == Operation.None);
}
///
/// Returns true if the given set of keywords is valid for HDR output.
///
/// Shader keywords combination that represents a shader variant.
/// Whether HDR output shader variants are required.
/// True if the shader variant is valid and should not be stripped.
public static bool IsShaderVariantValid(ShaderKeywordSet shaderKeywordSet, bool isHDREnabled)
{
bool hasHDRKeywords = shaderKeywordSet.IsEnabled(ShaderKeywords.HDREncoding) || shaderKeywordSet.IsEnabled(ShaderKeywords.HDRColorSpaceConversion) || shaderKeywordSet.IsEnabled(ShaderKeywords.HDRColorSpaceConversionAndEncoding) || shaderKeywordSet.IsEnabled(ShaderKeywords.HDRInput);
// If we don't plan to enable HDR, remove all HDR Output variants
if (!isHDREnabled && hasHDRKeywords)
return false;
return true;
}
}
}