This commit is contained in:
2026-11-24 15:51:02 +01:00
parent cca4d73e41
commit f4aa17114a
22092 changed files with 5042406 additions and 0 deletions
@@ -0,0 +1,90 @@
using System;
using System.Threading;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine.Assertions;
using UnityEngine.Jobs;
namespace UnityEngine.Rendering
{
/// <summary>
/// Array utilities functions
/// </summary>
public static class ArrayExtensions
{
/// <summary>
/// Resizes a native array. If an empty native array is passed, it will create a new one.
/// </summary>
/// <typeparam name="T">The type of the array</typeparam>
/// <param name="array">Target array to resize</param>
/// <param name="capacity">New size of native array to resize</param>
public static void ResizeArray<T>(this ref NativeArray<T> array, int capacity) where T : struct
{
var newArray = new NativeArray<T>(capacity, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
if (array.IsCreated)
{
NativeArray<T>.Copy(array, newArray, array.Length);
array.Dispose();
}
array = newArray;
}
/// <summary>
/// Resizes a transform access array.
/// </summary>
/// <param name="array">Target array to resize</param>
/// <param name="capacity">New size of transform access array to resize</param>
public static void ResizeArray(this ref TransformAccessArray array, int capacity)
{
var newArray = new TransformAccessArray(capacity);
if (array.isCreated)
{
for (int i = 0; i < array.length; ++i)
newArray.Add(array[i]);
array.Dispose();
}
array = newArray;
}
/// <summary>
/// Resizes an array. If a null reference is passed, it will allocate the desired array.
/// </summary>
/// <typeparam name="T">The type of the array</typeparam>
/// <param name="array">Target array to resize</param>
/// <param name="capacity">New size of array to resize</param>
public static void ResizeArray<T>(ref T[] array, int capacity)
{
if (array == null)
{
array = new T[capacity];
return;
}
Array.Resize<T>(ref array, capacity);
}
/// <summary>
/// Fills an array with the same value.
/// </summary>
/// <typeparam name="T">The type of the array</typeparam>
/// <param name="array">Target array to fill</param>
/// <param name="value">Value to fill</param>
/// <param name="startIndex">Start index to fill</param>
/// <param name="length">The number of entries to write, or -1 to fill until the end of the array</param>
public static void FillArray<T>(this ref NativeArray<T> array, in T value, int startIndex = 0, int length = -1) where T : unmanaged
{
Assert.IsTrue(startIndex >= 0);
unsafe
{
T* ptr = (T*)array.GetUnsafePtr<T>();
int endIndex = length == -1 ? array.Length : startIndex + length;
for (int i = startIndex; i < endIndex; ++i)
ptr[i] = value;
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2bfeecf9b5e9f7c4d99b0e400429e25a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,260 @@
using System;
using System.Runtime.InteropServices;
namespace UnityEngine.Rendering
{
/// <summary>
/// Contains spherical harmonic coefficients used for lighting representation in the format
/// expected by <c>DOTS_INSTANCING_ON</c> shaders.
///
/// The size of the struct is padded to a power of two so arrays of such structs can be efficiently
/// indexed in shaders.
/// </summary>
/// <seealso cref="SphericalHarmonicsL2"/>
[StructLayout(LayoutKind.Sequential)]
[Serializable]
public struct SHCoefficients : IEquatable<SHCoefficients>
{
/// <summary>
/// Contains the SH coefficients that correspond to the <c>unity_SHAr</c> shader property.
/// </summary>
public Vector4 SHAr;
/// <summary>
/// Contains the SH coefficients that correspond to the <c>unity_SHAg</c> shader property.
/// </summary>
public Vector4 SHAg;
/// <summary>
/// Contains the SH coefficients that correspond to the <c>unity_SHAb</c> shader property.
/// </summary>
public Vector4 SHAb;
/// <summary>
/// Contains the SH coefficients that correspond to the <c>unity_SHBr</c> shader property.
/// </summary>
public Vector4 SHBr;
/// <summary>
/// Contains the SH coefficients that correspond to the <c>unity_SHBg</c> shader property.
/// </summary>
public Vector4 SHBg;
/// <summary>
/// Contains the SH coefficients that correspond to the <c>unity_SHBb</c> shader property.
/// </summary>
public Vector4 SHBb;
/// <summary>
/// Contains the SH coefficients that correspond to the <c>unity_SHC</c> shader property.
/// </summary>
public Vector4 SHC;
/// <summary>
/// Contains the baked shadowing data that corresponds to the <c>unity_ProbesOcclusion</c> shader property.
/// </summary>
public Vector4 ProbesOcclusion;
/// <summary>
/// Construct an instance of <c>SHCoefficients</c> that represents the same spherical
/// harmonic coefficients as the parameter.
/// </summary>
/// <param name="sh">The spherical harmonic coefficients to initialize with.</param>
public SHCoefficients(SphericalHarmonicsL2 sh)
{
SHAr = GetSHA(sh, 0);
SHAg = GetSHA(sh, 1);
SHAb = GetSHA(sh, 2);
SHBr = GetSHB(sh, 0);
SHBg = GetSHB(sh, 1);
SHBb = GetSHB(sh, 2);
SHC = GetSHC(sh);
ProbesOcclusion = Vector4.one;
}
/// <summary>
/// Construct an instance of <c>SHCoefficients</c> that represents the same spherical
/// harmonic coefficients as the parameter.
/// </summary>
/// <param name="sh">The spherical harmonic coefficients to initialize with.</param>
/// <param name="probesOcclusion">The baked shadowing data to include with this set of spherical harmonic coefficients.</param>
public SHCoefficients(SphericalHarmonicsL2 sh, Vector4 probesOcclusion)
: this(sh)
{
ProbesOcclusion = probesOcclusion;
}
static Vector4 GetSHA(SphericalHarmonicsL2 sh, int i)
{
return new Vector4(sh[i, 3], sh[i, 1], sh[i, 2], sh[i, 0] - sh[i, 6]);
}
static Vector4 GetSHB(SphericalHarmonicsL2 sh, int i)
{
return new Vector4(sh[i, 4], sh[i, 5], sh[i, 6] * 3f, sh[i, 7]);
}
static Vector4 GetSHC(SphericalHarmonicsL2 sh)
{
return new Vector4(sh[0, 8], sh[1, 8], sh[2, 8], 1);
}
/// <summary>
/// Equals implementation.
/// </summary>
/// <param name="other">Other SHCoefficients instance to comapre this against.</param>
/// <returns>True if contents are equal, False otherwise.</returns>
public bool Equals(SHCoefficients other)
{
return SHAr.Equals(other.SHAr) && SHAg.Equals(other.SHAg) && SHAb.Equals(other.SHAb) && SHBr.Equals(other.SHBr) && SHBg.Equals(other.SHBg) && SHBb.Equals(other.SHBb) && SHC.Equals(other.SHC) && ProbesOcclusion.Equals(other.ProbesOcclusion);
}
/// <summary>
/// Equals implementation.
/// </summary>
/// <param name="obj">Other object to compare this object against</param>
/// <returns>True if contents are equal, False otherwise.</returns>
public override bool Equals(object obj)
{
return obj is SHCoefficients other && Equals(other);
}
/// <summary>
/// GetHashCode implementation.
/// </summary>
/// <returns>Returns a hashcode based on SHA coefficients.</returns>
public override int GetHashCode()
{
return HashCode.Combine(SHAr, SHAg, SHAb, SHBr, SHBg, SHBb, SHC, ProbesOcclusion);
}
/// <summary>
/// Equality operator implementation.
/// </summary>
/// <param name="left">Left operand of comparison</param>
/// <param name="right">Right operand of comparison</param>
/// <returns>True if contents are equal, False otherwise.</returns>
public static bool operator ==(SHCoefficients left, SHCoefficients right)
{
return left.Equals(right);
}
/// <summary>
/// Not equals operator implementation.
/// </summary>
/// <param name="left">Left operand of comparison</param>
/// <param name="right">Right operand of comparison</param>
/// <returns>True if contents are not equal, False otherwise.</returns>
public static bool operator !=(SHCoefficients left, SHCoefficients right)
{
return !left.Equals(right);
}
}
/// <summary>
/// Contains default values for built-in properties that the user is expected to manually
/// provide for <c>DOTS_INSTANCING_ON</c> shaders. The struct layout matches the
/// <c>unity_DOTSInstanceGlobalValues</c> constant buffer the shader expects the default
/// values in.
/// </summary>
[Obsolete("BatchRendererGroupGlobals and associated cbuffer are now set automatically by Unity. Setting it manually is no longer necessary or supported. #from(2023.1)")]
[StructLayout(LayoutKind.Sequential)]
[Serializable]
public struct BatchRendererGroupGlobals : IEquatable<BatchRendererGroupGlobals>
{
/// <summary>
/// The string name of the constant buffer <c>DOTS_INSTANCING_ON</c> shaders use
/// to read default values for the built-in properties contained in this struct.
/// </summary>
public const string kGlobalsPropertyName = "unity_DOTSInstanceGlobalValues";
/// <summary>
/// The unique identifier for <see cref="kGlobalsPropertyName"/>, retrieved using
/// <see cref="Shader.PropertyToID"/>.
/// </summary>
/// <seealso cref="Shader.PropertyToID"/>
public static readonly int kGlobalsPropertyId = Shader.PropertyToID(kGlobalsPropertyName);
/// <summary>
/// The default value to use for the <c>unity_ProbesOcclusion</c> built-in shader property.
/// </summary>
public Vector4 ProbesOcclusion;
/// <summary>
/// The default value to use for the <c>unity_SpecCube0_HDR</c> built-in shader property.
/// </summary>
public Vector4 SpecCube0_HDR;
/// <summary>
/// The default value to use for the <c>unity_SpecCube1_HDR</c> built-in shader property.
/// </summary>
public Vector4 SpecCube1_HDR;
/// <summary>
/// The default values to use for the built-in spherical harmonics shader properties.
/// </summary>
/// <seealso cref="SHCoefficients"/>
public SHCoefficients SHCoefficients;
/// <summary>
/// Construct a struct with default values based on the currently active reflection probe
/// and ambient lighting settings.
/// </summary>
public static BatchRendererGroupGlobals Default
{
get
{
var globals = new BatchRendererGroupGlobals();
globals.ProbesOcclusion = Vector4.one;
globals.SpecCube0_HDR = ReflectionProbe.defaultTextureHDRDecodeValues;
globals.SpecCube1_HDR = globals.SpecCube0_HDR;
globals.SHCoefficients = new SHCoefficients(RenderSettings.ambientProbe);
return globals;
}
}
/// <summary>
/// Equals implementation.
/// </summary>
/// <param name="other">Other BatchRendererGroupGlobals instance to comapre this against.</param>
/// <returns>True if contents are equal, False otherwise.</returns>
public bool Equals(BatchRendererGroupGlobals other)
{
return ProbesOcclusion.Equals(other.ProbesOcclusion) && SpecCube0_HDR.Equals(other.SpecCube0_HDR) && SpecCube1_HDR.Equals(other.SpecCube1_HDR) && SHCoefficients.Equals(other.SHCoefficients);
}
/// <summary>
/// Equals implementation.
/// </summary>
/// <param name="obj">Other object to comapre this against.</param>
/// <returns>True if contents are equal, False otherwise.</returns>
public override bool Equals(object obj)
{
return obj is BatchRendererGroupGlobals other && Equals(other);
}
/// <summary>
/// GetHashCode implementation.
/// </summary>
/// <returns>Returns a hashcode based on ProbesOcclusion, SpecCube and SH coefficients parameters</returns>
public override int GetHashCode()
{
return HashCode.Combine(ProbesOcclusion, SpecCube0_HDR, SpecCube1_HDR, SHCoefficients);
}
/// <summary>
/// Equality operator implementation.
/// </summary>
/// <param name="left">Left operand of comparison</param>
/// <param name="right">Right operand of comparison</param>
/// <returns>True if contents are equal, False otherwise.</returns>
public static bool operator ==(BatchRendererGroupGlobals left, BatchRendererGroupGlobals right)
{
return left.Equals(right);
}
/// <summary>
/// Not equals operator implementation.
/// </summary>
/// <param name="left">Left operand of comparison</param>
/// <param name="right">Right operand of comparison</param>
/// <returns>True if contents are not equal, False otherwise.</returns>
public static bool operator !=(BatchRendererGroupGlobals left, BatchRendererGroupGlobals right)
{
return !left.Equals(right);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ab8ad79e3d094044af2eb94ea63279cf
timeCreated: 1648738004
@@ -0,0 +1,957 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace UnityEngine.Rendering
{
/// <summary>
/// IBitArray interface.
/// </summary>
public interface IBitArray
{
/// <summary>Gets the capacity of this BitArray. This is the number of bits that are usable.</summary>
uint capacity { get; }
/// <summary>Return `true` if all the bits of this BitArray are set to 0. Returns `false` otherwise.</summary>
bool allFalse { get; }
/// <summary>Return `true` if all the bits of this BitArray are set to 1. Returns `false` otherwise.</summary>
bool allTrue { get; }
/// <summary>
/// An indexer that allows access to the bit at a given index. This provides both read and write access.
/// </summary>
/// <param name="index">Index of the bit.</param>
/// <value>State of the bit at the provided index.</value>
bool this[uint index] { get; set; }
/// <summary>Writes the bits in the array in a human-readable form. This is as a string of 0s and 1s packed by 8 bits. This is useful for debugging.</summary>
string humanizedData { get; }
/// <summary>
/// Perform an AND bitwise operation between this BitArray and the one you pass into the function and return the result. Both BitArrays must have the same capacity. This will not change current BitArray values.
/// </summary>
/// <param name="other">BitArray with which to the And operation.</param>
/// <returns>The resulting bit array.</returns>
IBitArray BitAnd(IBitArray other);
/// <summary>
/// Perform an OR bitwise operation between this BitArray and the one you pass into the function and return the result. Both BitArrays must have the same capacity. This will not change current BitArray values.
/// </summary>
/// <param name="other">BitArray with which to the Or operation.</param>
/// <returns>The resulting bit array.</returns>
IBitArray BitOr(IBitArray other);
/// <summary>
/// Return the BitArray with every bit inverted.
/// </summary>
/// <returns>The resulting bit array.</returns>
IBitArray BitNot();
}
// /!\ Important for serialization:
// Serialization helper will rely on the name of the struct type.
// In order to work, it must be BitArrayN where N is the capacity without suffix.
/// <summary>
/// Bit array of size 8.
/// </summary>
[Serializable]
[System.Diagnostics.DebuggerDisplay("{this.GetType().Name} {humanizedData}")]
public struct BitArray8 : IBitArray
{
[SerializeField]
byte data;
/// <summary>Number of elements in the bit array.</summary>
public uint capacity => 8u;
/// <summary>True if all bits are 0.</summary>
public bool allFalse => data == 0u;
/// <summary>True if all bits are 1.</summary>
public bool allTrue => data == byte.MaxValue;
/// <summary>Returns the bit array in a human readable form.</summary>
public string humanizedData => String.Format("{0, " + capacity + "}", Convert.ToString(data, 2)).Replace(' ', '0');
/// <summary>
/// Returns the state of the bit at a specific index.
/// </summary>
/// <param name="index">Index of the bit.</param>
/// <value>State of the bit at the provided index.</value>
public bool this[uint index]
{
get => BitArrayUtilities.Get8(index, data);
set => BitArrayUtilities.Set8(index, ref data, value);
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="initValue">Initialization value.</param>
public BitArray8(byte initValue) => data = initValue;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="bitIndexTrue">List of indices where bits should be set to true.</param>
public BitArray8(IEnumerable<uint> bitIndexTrue)
{
data = (byte)0u;
if (bitIndexTrue == null)
return;
for (int index = bitIndexTrue.Count() - 1; index >= 0; --index)
{
uint bitIndex = bitIndexTrue.ElementAt(index);
if (bitIndex >= capacity) continue;
data |= (byte)(1u << (int)bitIndex);
}
}
/// <summary>
/// Bit-wise Not operator
/// </summary>
/// <param name="a">Bit array with which to do the operation.</param>
/// <returns>The resulting bit array.</returns>
public static BitArray8 operator ~(BitArray8 a) => new BitArray8((byte)~a.data);
/// <summary>
/// Bit-wise Or operator
/// </summary>
/// <param name="a">First bit array.</param>
/// <param name="b">Second bit array.</param>
/// <returns>The resulting bit array.</returns>
public static BitArray8 operator |(BitArray8 a, BitArray8 b) => new BitArray8((byte)(a.data | b.data));
/// <summary>
/// Bit-wise And operator
/// </summary>
/// <param name="a">First bit array.</param>
/// <param name="b">Second bit array.</param>
/// <returns>The resulting bit array.</returns>
public static BitArray8 operator &(BitArray8 a, BitArray8 b) => new BitArray8((byte)(a.data & b.data));
/// <summary>
/// Bit-wise And
/// </summary>
/// <param name="other">Bit array with which to do the operation.</param>
/// <returns>The resulting bit array.</returns>
public IBitArray BitAnd(IBitArray other) => this & (BitArray8)other;
/// <summary>
/// Bit-wise Or
/// </summary>
/// <param name="other">Bit array with which to do the operation.</param>
/// <returns>The resulting bit array.</returns>
public IBitArray BitOr(IBitArray other) => this | (BitArray8)other;
/// <summary>
/// Bit-wise Not
/// </summary>
/// <returns>The resulting bit array.</returns>
public IBitArray BitNot() => ~this;
/// <summary>
/// Equality operator.
/// </summary>
/// <param name="a">First bit array.</param>
/// <param name="b">Second bit array.</param>
/// <returns>True if both bit arrays are equals.</returns>
public static bool operator ==(BitArray8 a, BitArray8 b) => a.data == b.data;
/// <summary>
/// Inequality operator.
/// </summary>
/// <param name="a">First bit array.</param>
/// <param name="b">Second bit array.</param>
/// <returns>True if the bit arrays are not equals.</returns>
public static bool operator !=(BitArray8 a, BitArray8 b) => a.data != b.data;
/// <summary>
/// Equality operator.
/// </summary>
/// <param name="obj">Bit array to compare to.</param>
/// <returns>True if the provided bit array is equal to this..</returns>
public override bool Equals(object obj) => obj is BitArray8 ba8 && ba8.data == data;
/// <summary>
/// Get the hashcode of the bit array.
/// </summary>
/// <returns>Hashcode of the bit array.</returns>
public override int GetHashCode() => 1768953197 + data.GetHashCode();
}
/// <summary>
/// Bit array of size 16.
/// </summary>
[Serializable]
[System.Diagnostics.DebuggerDisplay("{this.GetType().Name} {humanizedData}")]
public struct BitArray16 : IBitArray
{
[SerializeField]
ushort data;
/// <summary>Number of elements in the bit array.</summary>
public uint capacity => 16u;
/// <summary>True if all bits are 0.</summary>
public bool allFalse => data == 0u;
/// <summary>True if all bits are 1.</summary>
public bool allTrue => data == ushort.MaxValue;
/// <summary>Returns the bit array in a human readable form.</summary>
public string humanizedData => System.Text.RegularExpressions.Regex.Replace(String.Format("{0, " + capacity + "}", Convert.ToString(data, 2)).Replace(' ', '0'), ".{8}", "$0.").TrimEnd('.');
/// <summary>
/// Returns the state of the bit at a specific index.
/// </summary>
/// <param name="index">Index of the bit.</param>
/// <value>State of the bit at the provided index.</value>
public bool this[uint index]
{
get => BitArrayUtilities.Get16(index, data);
set => BitArrayUtilities.Set16(index, ref data, value);
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="initValue">Initialization value.</param>
public BitArray16(ushort initValue) => data = initValue;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="bitIndexTrue">List of indices where bits should be set to true.</param>
public BitArray16(IEnumerable<uint> bitIndexTrue)
{
data = (ushort)0u;
if (bitIndexTrue == null)
return;
for (int index = bitIndexTrue.Count() - 1; index >= 0; --index)
{
uint bitIndex = bitIndexTrue.ElementAt(index);
if (bitIndex >= capacity) continue;
data |= (ushort)(1u << (int)bitIndex);
}
}
/// <summary>
/// Bit-wise Not operator
/// </summary>
/// <param name="a">Bit array with which to do the operation.</param>
/// <returns>The resulting bit array.</returns>
public static BitArray16 operator ~(BitArray16 a) => new BitArray16((ushort)~a.data);
/// <summary>
/// Bit-wise Or operator
/// </summary>
/// <param name="a">First bit array.</param>
/// <param name="b">Second bit array.</param>
/// <returns>The resulting bit array.</returns>
public static BitArray16 operator |(BitArray16 a, BitArray16 b) => new BitArray16((ushort)(a.data | b.data));
/// <summary>
/// Bit-wise And operator
/// </summary>
/// <param name="a">First bit array.</param>
/// <param name="b">Second bit array.</param>
/// <returns>The resulting bit array.</returns>
public static BitArray16 operator &(BitArray16 a, BitArray16 b) => new BitArray16((ushort)(a.data & b.data));
/// <summary>
/// Bit-wise And
/// </summary>
/// <param name="other">Bit array with which to do the operation.</param>
/// <returns>The resulting bit array.</returns>
public IBitArray BitAnd(IBitArray other) => this & (BitArray16)other;
/// <summary>
/// Bit-wise Or
/// </summary>
/// <param name="other">Bit array with which to do the operation.</param>
/// <returns>The resulting bit array.</returns>
public IBitArray BitOr(IBitArray other) => this | (BitArray16)other;
/// <summary>
/// Bit-wise Not
/// </summary>
/// <returns>The resulting bit array.</returns>
public IBitArray BitNot() => ~this;
/// <summary>
/// Equality operator.
/// </summary>
/// <param name="a">First bit array.</param>
/// <param name="b">Second bit array.</param>
/// <returns>True if both bit arrays are equals.</returns>
public static bool operator ==(BitArray16 a, BitArray16 b) => a.data == b.data;
/// <summary>
/// Inequality operator.
/// </summary>
/// <param name="a">First bit array.</param>
/// <param name="b">Second bit array.</param>
/// <returns>True if the bit arrays are not equals.</returns>
public static bool operator !=(BitArray16 a, BitArray16 b) => a.data != b.data;
/// <summary>
/// Equality operator.
/// </summary>
/// <param name="obj">Bit array to compare to.</param>
/// <returns>True if the provided bit array is equal to this..</returns>
public override bool Equals(object obj) => obj is BitArray16 ba16 && ba16.data == data;
/// <summary>
/// Get the hashcode of the bit array.
/// </summary>
/// <returns>Hashcode of the bit array.</returns>
public override int GetHashCode() => 1768953197 + data.GetHashCode();
}
/// <summary>
/// Bit array of size 32.
/// </summary>
[Serializable]
[System.Diagnostics.DebuggerDisplay("{this.GetType().Name} {humanizedData}")]
public struct BitArray32 : IBitArray
{
[SerializeField]
uint data;
/// <summary>Number of elements in the bit array.</summary>
public uint capacity => 32u;
/// <summary>True if all bits are 0.</summary>
public bool allFalse => data == 0u;
/// <summary>True if all bits are 1.</summary>
public bool allTrue => data == uint.MaxValue;
string humanizedVersion => Convert.ToString(data, 2);
/// <summary>Returns the bit array in a human readable form.</summary>
public string humanizedData => System.Text.RegularExpressions.Regex.Replace(String.Format("{0, " + capacity + "}", Convert.ToString(data, 2)).Replace(' ', '0'), ".{8}", "$0.").TrimEnd('.');
/// <summary>
/// Returns the state of the bit at a specific index.
/// </summary>
/// <param name="index">Index of the bit.</param>
/// <value>State of the bit at the provided index.</value>
public bool this[uint index]
{
get => BitArrayUtilities.Get32(index, data);
set => BitArrayUtilities.Set32(index, ref data, value);
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="initValue">Initialization value.</param>
public BitArray32(uint initValue) => data = initValue;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="bitIndexTrue">List of indices where bits should be set to true.</param>
public BitArray32(IEnumerable<uint> bitIndexTrue)
{
data = 0u;
if (bitIndexTrue == null)
return;
for (int index = bitIndexTrue.Count() - 1; index >= 0; --index)
{
uint bitIndex = bitIndexTrue.ElementAt(index);
if (bitIndex >= capacity) continue;
data |= 1u << (int)bitIndex;
}
}
/// <summary>
/// Bit-wise And
/// </summary>
/// <param name="other">Bit array with which to do the operation.</param>
/// <returns>The resulting bit array.</returns>
public IBitArray BitAnd(IBitArray other) => this & (BitArray32)other;
/// <summary>
/// Bit-wise Or
/// </summary>
/// <param name="other">Bit array with which to do the operation.</param>
/// <returns>The resulting bit array.</returns>
public IBitArray BitOr(IBitArray other) => this | (BitArray32)other;
/// <summary>
/// Bit-wise Not
/// </summary>
/// <returns>The resulting bit array.</returns>
public IBitArray BitNot() => ~this;
/// <summary>
/// Bit-wise Not operator
/// </summary>
/// <param name="a">Bit array with which to do the operation.</param>
/// <returns>The resulting bit array.</returns>
public static BitArray32 operator ~(BitArray32 a) => new BitArray32(~a.data);
/// <summary>
/// Bit-wise Or operator
/// </summary>
/// <param name="a">First bit array.</param>
/// <param name="b">Second bit array.</param>
/// <returns>The resulting bit array.</returns>
public static BitArray32 operator |(BitArray32 a, BitArray32 b) => new BitArray32(a.data | b.data);
/// <summary>
/// Bit-wise And operator
/// </summary>
/// <param name="a">First bit array.</param>
/// <param name="b">Second bit array.</param>
/// <returns>The resulting bit array.</returns>
public static BitArray32 operator &(BitArray32 a, BitArray32 b) => new BitArray32(a.data & b.data);
/// <summary>
/// Equality operator.
/// </summary>
/// <param name="a">First bit array.</param>
/// <param name="b">Second bit array.</param>
/// <returns>True if both bit arrays are equals.</returns>
public static bool operator ==(BitArray32 a, BitArray32 b) => a.data == b.data;
/// <summary>
/// Inequality operator.
/// </summary>
/// <param name="a">First bit array.</param>
/// <param name="b">Second bit array.</param>
/// <returns>True if the bit arrays are not equals.</returns>
public static bool operator !=(BitArray32 a, BitArray32 b) => a.data != b.data;
/// <summary>
/// Equality operator.
/// </summary>
/// <param name="obj">Bit array to compare to.</param>
/// <returns>True if the provided bit array is equal to this..</returns>
public override bool Equals(object obj) => obj is BitArray32 ba32 && ba32.data == data;
/// <summary>
/// Get the hashcode of the bit array.
/// </summary>
/// <returns>Hashcode of the bit array.</returns>
public override int GetHashCode() => 1768953197 + data.GetHashCode();
}
/// <summary>
/// Bit array of size 64.
/// </summary>
[Serializable]
[System.Diagnostics.DebuggerDisplay("{this.GetType().Name} {humanizedData}")]
public struct BitArray64 : IBitArray
{
[SerializeField]
ulong data;
/// <summary>Number of elements in the bit array.</summary>
public uint capacity => 64u;
/// <summary>True if all bits are 0.</summary>
public bool allFalse => data == 0uL;
/// <summary>True if all bits are 1.</summary>
public bool allTrue => data == ulong.MaxValue;
/// <summary>Returns the bit array in a human readable form.</summary>
public string humanizedData => System.Text.RegularExpressions.Regex.Replace(String.Format("{0, " + capacity + "}", Convert.ToString((long)data, 2)).Replace(' ', '0'), ".{8}", "$0.").TrimEnd('.');
/// <summary>
/// Returns the state of the bit at a specific index.
/// </summary>
/// <param name="index">Index of the bit.</param>
/// <value>State of the bit at the provided index.</value>
public bool this[uint index]
{
get => BitArrayUtilities.Get64(index, data);
set => BitArrayUtilities.Set64(index, ref data, value);
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="initValue">Initialization value.</param>
public BitArray64(ulong initValue) => data = initValue;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="bitIndexTrue">List of indices where bits should be set to true.</param>
public BitArray64(IEnumerable<uint> bitIndexTrue)
{
data = 0L;
if (bitIndexTrue == null)
return;
for (int index = bitIndexTrue.Count() - 1; index >= 0; --index)
{
uint bitIndex = bitIndexTrue.ElementAt(index);
if (bitIndex >= capacity) continue;
data |= 1uL << (int)bitIndex;
}
}
/// <summary>
/// Bit-wise Not operator
/// </summary>
/// <param name="a">Bit array with which to do the operation.</param>
/// <returns>The resulting bit array.</returns>
public static BitArray64 operator ~(BitArray64 a) => new BitArray64(~a.data);
/// <summary>
/// Bit-wise Or operator
/// </summary>
/// <param name="a">First bit array.</param>
/// <param name="b">Second bit array.</param>
/// <returns>The resulting bit array.</returns>
public static BitArray64 operator |(BitArray64 a, BitArray64 b) => new BitArray64(a.data | b.data);
/// <summary>
/// Bit-wise And operator
/// </summary>
/// <param name="a">First bit array.</param>
/// <param name="b">Second bit array.</param>
/// <returns>The resulting bit array.</returns>
public static BitArray64 operator &(BitArray64 a, BitArray64 b) => new BitArray64(a.data & b.data);
/// <summary>
/// Bit-wise And
/// </summary>
/// <param name="other">Bit array with which to do the operation.</param>
/// <returns>The resulting bit array.</returns>
public IBitArray BitAnd(IBitArray other) => this & (BitArray64)other;
/// <summary>
/// Bit-wise Or
/// </summary>
/// <param name="other">Bit array with which to do the operation.</param>
/// <returns>The resulting bit array.</returns>
public IBitArray BitOr(IBitArray other) => this | (BitArray64)other;
/// <summary>
/// Bit-wise Not
/// </summary>
/// <returns>The resulting bit array.</returns>
public IBitArray BitNot() => ~this;
/// <summary>
/// Equality operator.
/// </summary>
/// <param name="a">First bit array.</param>
/// <param name="b">Second bit array.</param>
/// <returns>True if both bit arrays are equals.</returns>
public static bool operator ==(BitArray64 a, BitArray64 b) => a.data == b.data;
/// <summary>
/// Inequality operator.
/// </summary>
/// <param name="a">First bit array.</param>
/// <param name="b">Second bit array.</param>
/// <returns>True if the bit arrays are not equals.</returns>
public static bool operator !=(BitArray64 a, BitArray64 b) => a.data != b.data;
/// <summary>
/// Equality operator.
/// </summary>
/// <param name="obj">Bit array to compare to.</param>
/// <returns>True if the provided bit array is equal to this..</returns>
public override bool Equals(object obj) => obj is BitArray64 ba64 && ba64.data == data;
/// <summary>
/// Get the hashcode of the bit array.
/// </summary>
/// <returns>Hashcode of the bit array.</returns>
public override int GetHashCode() => 1768953197 + data.GetHashCode();
}
/// <summary>
/// Bit array of size 128.
/// </summary>
[Serializable]
[System.Diagnostics.DebuggerDisplay("{this.GetType().Name} {humanizedData}")]
public struct BitArray128 : IBitArray
{
[SerializeField]
ulong data1;
[SerializeField]
ulong data2;
/// <summary>Number of elements in the bit array.</summary>
public uint capacity => 128u;
/// <summary>True if all bits are 0.</summary>
public bool allFalse => data1 == 0uL && data2 == 0uL;
/// <summary>True if all bits are 1.</summary>
public bool allTrue => data1 == ulong.MaxValue && data2 == ulong.MaxValue;
/// <summary>Returns the bit array in a human readable form.</summary>
public string humanizedData =>
System.Text.RegularExpressions.Regex.Replace(String.Format("{0, " + 64u + "}", Convert.ToString((long)data2, 2)).Replace(' ', '0'), ".{8}", "$0.")
+ System.Text.RegularExpressions.Regex.Replace(String.Format("{0, " + 64u + "}", Convert.ToString((long)data1, 2)).Replace(' ', '0'), ".{8}", "$0.").TrimEnd('.');
/// <summary>
/// Returns the state of the bit at a specific index.
/// </summary>
/// <param name="index">Index of the bit.</param>
/// <value>State of the bit at the provided index.</value>
public bool this[uint index]
{
get => index < 64u
? (data1 & (1uL << (int)index)) != 0uL
: (data2 & (1uL << (int)(index - 64u))) != 0uL;
set
{
if (index < 64u)
data1 = (value ? (data1 | (1uL << (int)index)) : (data1 & ~(1uL << (int)index)));
else
data2 = (value ? (data2 | (1uL << (int)(index - 64u))) : (data2 & ~(1uL << (int)(index - 64u))));
}
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="initValue1">Initialization value 1.</param>
/// <param name="initValue2">Initialization value 2.</param>
public BitArray128(ulong initValue1, ulong initValue2)
{
data1 = initValue1;
data2 = initValue2;
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="bitIndexTrue">List of indices where bits should be set to true.</param>
public BitArray128(IEnumerable<uint> bitIndexTrue)
{
data1 = data2 = 0uL;
if (bitIndexTrue == null)
return;
for (int index = bitIndexTrue.Count() - 1; index >= 0; --index)
{
uint bitIndex = bitIndexTrue.ElementAt(index);
if (bitIndex < 64u)
data1 |= 1uL << (int)bitIndex;
else if (bitIndex < capacity)
data2 |= 1uL << (int)(bitIndex - 64u);
}
}
/// <summary>
/// Bit-wise Not operator
/// </summary>
/// <param name="a">First bit array.</param>
/// <returns>The resulting bit array.</returns>
public static BitArray128 operator ~(BitArray128 a) => new BitArray128(~a.data1, ~a.data2);
/// <summary>
/// Bit-wise Or operator
/// </summary>
/// <param name="a">First bit array.</param>
/// <param name="b">Second bit array.</param>
/// <returns>The resulting bit array.</returns>
public static BitArray128 operator |(BitArray128 a, BitArray128 b) => new BitArray128(a.data1 | b.data1, a.data2 | b.data2);
/// <summary>
/// Bit-wise And operator
/// </summary>
/// <param name="a">First bit array.</param>
/// <param name="b">Second bit array.</param>
/// <returns>The resulting bit array.</returns>
public static BitArray128 operator &(BitArray128 a, BitArray128 b) => new BitArray128(a.data1 & b.data1, a.data2 & b.data2);
/// <summary>
/// Bit-wise And
/// </summary>
/// <param name="other">Bit array with which to do the operation.</param>
/// <returns>The resulting bit array.</returns>
public IBitArray BitAnd(IBitArray other) => this & (BitArray128)other;
/// <summary>
/// Bit-wise Or
/// </summary>
/// <param name="other">Bit array with which to do the operation.</param>
/// <returns>The resulting bit array.</returns>
public IBitArray BitOr(IBitArray other) => this | (BitArray128)other;
/// <summary>
/// Bit-wise Not
/// </summary>
/// <returns>The resulting bit array.</returns>
public IBitArray BitNot() => ~this;
/// <summary>
/// Equality operator.
/// </summary>
/// <param name="a">First bit array.</param>
/// <param name="b">Second bit array.</param>
/// <returns>True if both bit arrays are equals.</returns>
public static bool operator ==(BitArray128 a, BitArray128 b) => a.data1 == b.data1 && a.data2 == b.data2;
/// <summary>
/// Inequality operator.
/// </summary>
/// <param name="a">First bit array.</param>
/// <param name="b">Second bit array.</param>
/// <returns>True if the bit arrays are not equals.</returns>
public static bool operator !=(BitArray128 a, BitArray128 b) => a.data1 != b.data1 || a.data2 != b.data2;
/// <summary>
/// Equality operator.
/// </summary>
/// <param name="obj">Bit array to compare to.</param>
/// <returns>True if the provided bit array is equal to this..</returns>
public override bool Equals(object obj) => obj is BitArray128 ba128 && data1.Equals(ba128.data1) && data2.Equals(ba128.data2);
/// <summary>
/// Get the hashcode of the bit array.
/// </summary>
/// <returns>Hashcode of the bit array.</returns>
public override int GetHashCode()
{
var hashCode = 1755735569;
hashCode = hashCode * -1521134295 + data1.GetHashCode();
hashCode = hashCode * -1521134295 + data2.GetHashCode();
return hashCode;
}
}
/// <summary>
/// Bit array of size 256.
/// </summary>
[Serializable]
[System.Diagnostics.DebuggerDisplay("{this.GetType().Name} {humanizedData}")]
public struct BitArray256 : IBitArray
{
[SerializeField]
ulong data1;
[SerializeField]
ulong data2;
[SerializeField]
ulong data3;
[SerializeField]
ulong data4;
/// <summary>Number of elements in the bit array.</summary>
public uint capacity => 256u;
/// <summary>True if all bits are 0.</summary>
public bool allFalse => data1 == 0uL && data2 == 0uL && data3 == 0uL && data4 == 0uL;
/// <summary>True if all bits are 1.</summary>
public bool allTrue => data1 == ulong.MaxValue && data2 == ulong.MaxValue && data3 == ulong.MaxValue && data4 == ulong.MaxValue;
/// <summary>Returns the bit array in a human readable form.</summary>
public string humanizedData =>
System.Text.RegularExpressions.Regex.Replace(String.Format("{0, " + 64u + "}", Convert.ToString((long)data4, 2)).Replace(' ', '0'), ".{8}", "$0.")
+ System.Text.RegularExpressions.Regex.Replace(String.Format("{0, " + 64u + "}", Convert.ToString((long)data3, 2)).Replace(' ', '0'), ".{8}", "$0.")
+ System.Text.RegularExpressions.Regex.Replace(String.Format("{0, " + 64u + "}", Convert.ToString((long)data2, 2)).Replace(' ', '0'), ".{8}", "$0.")
+ System.Text.RegularExpressions.Regex.Replace(String.Format("{0, " + 64u + "}", Convert.ToString((long)data1, 2)).Replace(' ', '0'), ".{8}", "$0.").TrimEnd('.');
/// <summary>
/// Returns the state of the bit at a specific index.
/// </summary>
/// <param name="index">Index of the bit.</param>
/// <value>State of the bit at the provided index.</value>
public bool this[uint index]
{
get => BitArrayUtilities.Get256(index, data1, data2, data3, data4);
set => BitArrayUtilities.Set256(index, ref data1, ref data2, ref data3, ref data4, value);
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="initValue1">Initialization value 1.</param>
/// <param name="initValue2">Initialization value 2.</param>
/// <param name="initValue3">Initialization value 3.</param>
/// <param name="initValue4">Initialization value 4.</param>
public BitArray256(ulong initValue1, ulong initValue2, ulong initValue3, ulong initValue4)
{
data1 = initValue1;
data2 = initValue2;
data3 = initValue3;
data4 = initValue4;
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="bitIndexTrue">List of indices where bits should be set to true.</param>
public BitArray256(IEnumerable<uint> bitIndexTrue)
{
data1 = data2 = data3 = data4 = 0uL;
if (bitIndexTrue == null)
return;
for (int index = bitIndexTrue.Count() - 1; index >= 0; --index)
{
uint bitIndex = bitIndexTrue.ElementAt(index);
if (bitIndex < 64u)
data1 |= 1uL << (int)bitIndex;
else if (bitIndex < 128u)
data2 |= 1uL << (int)(bitIndex - 64u);
else if (bitIndex < 192u)
data3 |= 1uL << (int)(bitIndex - 128u);
else if (bitIndex < capacity)
data4 |= 1uL << (int)(bitIndex - 192u);
}
}
/// <summary>
/// Bit-wise Not operator
/// </summary>
/// <param name="a">Bit array with which to do the operation.</param>
/// <returns>The resulting bit array.</returns>
public static BitArray256 operator ~(BitArray256 a) => new BitArray256(~a.data1, ~a.data2, ~a.data3, ~a.data4);
/// <summary>
/// Bit-wise Or operator
/// </summary>
/// <param name="a">First bit array.</param>
/// <param name="b">Second bit array.</param>
/// <returns>The resulting bit array.</returns>
public static BitArray256 operator |(BitArray256 a, BitArray256 b) => new BitArray256(a.data1 | b.data1, a.data2 | b.data2, a.data3 | b.data3, a.data4 | b.data4);
/// <summary>
/// Bit-wise And operator
/// </summary>
/// <param name="a">First bit array.</param>
/// <param name="b">Second bit array.</param>
/// <returns>The resulting bit array.</returns>
public static BitArray256 operator &(BitArray256 a, BitArray256 b) => new BitArray256(a.data1 & b.data1, a.data2 & b.data2, a.data3 & b.data3, a.data4 & b.data4);
/// <summary>
/// Bit-wise And
/// </summary>
/// <param name="other">Bit array with which to do the operation.</param>
/// <returns>The resulting bit array.</returns>
public IBitArray BitAnd(IBitArray other) => this & (BitArray256)other;
/// <summary>
/// Bit-wise Or
/// </summary>
/// <param name="other">Bit array with which to do the operation.</param>
/// <returns>The resulting bit array.</returns>
public IBitArray BitOr(IBitArray other) => this | (BitArray256)other;
/// <summary>
/// Bit-wise Not
/// </summary>
/// <returns>The resulting bit array.</returns>
public IBitArray BitNot() => ~this;
/// <summary>
/// Equality operator.
/// </summary>
/// <param name="a">First bit array.</param>
/// <param name="b">Second bit array.</param>
/// <returns>True if both bit arrays are equals.</returns>
public static bool operator ==(BitArray256 a, BitArray256 b) => a.data1 == b.data1 && a.data2 == b.data2 && a.data3 == b.data3 && a.data4 == b.data4;
/// <summary>
/// Inequality operator.
/// </summary>
/// <param name="a">First bit array.</param>
/// <param name="b">Second bit array.</param>
/// <returns>True if the bit arrays are not equals.</returns>
public static bool operator !=(BitArray256 a, BitArray256 b) => a.data1 != b.data1 || a.data2 != b.data2 || a.data3 != b.data3 || a.data4 != b.data4;
/// <summary>
/// Equality operator.
/// </summary>
/// <param name="obj">Bit array to compare to.</param>
/// <returns>True if the provided bit array is equal to this..</returns>
public override bool Equals(object obj)
=> obj is BitArray256 ba256
&& data1.Equals(ba256.data1)
&& data2.Equals(ba256.data2)
&& data3.Equals(ba256.data3)
&& data4.Equals(ba256.data4);
/// <summary>
/// Get the hashcode of the bit array.
/// </summary>
/// <returns>Hashcode of the bit array.</returns>
public override int GetHashCode()
{
var hashCode = 1870826326;
hashCode = hashCode * -1521134295 + data1.GetHashCode();
hashCode = hashCode * -1521134295 + data2.GetHashCode();
hashCode = hashCode * -1521134295 + data3.GetHashCode();
hashCode = hashCode * -1521134295 + data4.GetHashCode();
return hashCode;
}
}
/// <summary>
/// Bit array utility class.
/// </summary>
public static class BitArrayUtilities
{
//written here to not duplicate the serialized accessor and runtime accessor
/// <summary>
/// Get a bit at a specific index.
/// </summary>
/// <param name="index">Bit index.</param>
/// <param name="data">Bit array data.</param>
/// <returns>The value of the bit at the specific index.</returns>
public static bool Get8(uint index, byte data) => (data & (1u << (int)index)) != 0u;
/// <summary>
/// Get a bit at a specific index.
/// </summary>
/// <param name="index">Bit index.</param>
/// <param name="data">Bit array data.</param>
/// <returns>The value of the bit at the specific index.</returns>
public static bool Get16(uint index, ushort data) => (data & (1u << (int)index)) != 0u;
/// <summary>
/// Get a bit at a specific index.
/// </summary>
/// <param name="index">Bit index.</param>
/// <param name="data">Bit array data.</param>
/// <returns>The value of the bit at the specific index.</returns>
public static bool Get32(uint index, uint data) => (data & (1u << (int)index)) != 0u;
/// <summary>
/// Get a bit at a specific index.
/// </summary>
/// <param name="index">Bit index.</param>
/// <param name="data">Bit array data.</param>
/// <returns>The value of the bit at the specific index.</returns>
public static bool Get64(uint index, ulong data) => (data & (1uL << (int)index)) != 0uL;
/// <summary>
/// Get a bit at a specific index.
/// </summary>
/// <param name="index">Bit index.</param>
/// <param name="data1">Bit array data 1.</param>
/// <param name="data2">Bit array data 2.</param>
/// <returns>The value of the bit at the specific index.</returns>
public static bool Get128(uint index, ulong data1, ulong data2)
=> index < 64u
? (data1 & (1uL << (int)index)) != 0uL
: (data2 & (1uL << (int)(index - 64u))) != 0uL;
/// <summary>
/// Get a bit at a specific index.
/// </summary>
/// <param name="index">Bit index.</param>
/// <param name="data1">Bit array data 1.</param>
/// <param name="data2">Bit array data 2.</param>
/// <param name="data3">Bit array data 3.</param>
/// <param name="data4">Bit array data 4.</param>
/// <returns>The value of the bit at the specific index.</returns>
public static bool Get256(uint index, ulong data1, ulong data2, ulong data3, ulong data4)
=> index < 128u
? index < 64u
? (data1 & (1uL << (int)index)) != 0uL
: (data2 & (1uL << (int)(index - 64u))) != 0uL
: index < 192u
? (data3 & (1uL << (int)(index - 128u))) != 0uL
: (data4 & (1uL << (int)(index - 192u))) != 0uL;
/// <summary>
/// Set a bit at a specific index.
/// </summary>
/// <param name="index">Bit index.</param>
/// <param name="data">Bit array data.</param>
/// <param name="value">Value to set the bit to.</param>
public static void Set8(uint index, ref byte data, bool value) => data = (byte)(value ? (data | (1u << (int)index)) : (data & ~(1u << (int)index)));
/// <summary>
/// Set a bit at a specific index.
/// </summary>
/// <param name="index">Bit index.</param>
/// <param name="data">Bit array data.</param>
/// <param name="value">Value to set the bit to.</param>
public static void Set16(uint index, ref ushort data, bool value) => data = (ushort)(value ? (data | (1u << (int)index)) : (data & ~(1u << (int)index)));
/// <summary>
/// Set a bit at a specific index.
/// </summary>
/// <param name="index">Bit index.</param>
/// <param name="data">Bit array data.</param>
/// <param name="value">Value to set the bit to.</param>
public static void Set32(uint index, ref uint data, bool value) => data = (value ? (data | (1u << (int)index)) : (data & ~(1u << (int)index)));
/// <summary>
/// Set a bit at a specific index.
/// </summary>
/// <param name="index">Bit index.</param>
/// <param name="data">Bit array data.</param>
/// <param name="value">Value to set the bit to.</param>
public static void Set64(uint index, ref ulong data, bool value) => data = (value ? (data | (1uL << (int)index)) : (data & ~(1uL << (int)index)));
/// <summary>
/// Set a bit at a specific index.
/// </summary>
/// <param name="index">Bit index.</param>
/// <param name="data1">Bit array data 1.</param>
/// <param name="data2">Bit array data 2.</param>
/// <param name="value">Value to set the bit to.</param>
public static void Set128(uint index, ref ulong data1, ref ulong data2, bool value)
{
if (index < 64u)
data1 = (value ? (data1 | (1uL << (int)index)) : (data1 & ~(1uL << (int)index)));
else
data2 = (value ? (data2 | (1uL << (int)(index - 64u))) : (data2 & ~(1uL << (int)(index - 64u))));
}
/// <summary>
/// Set a bit at a specific index.
/// </summary>
/// <param name="index">Bit index.</param>
/// <param name="data1">Bit array data 1.</param>
/// <param name="data2">Bit array data 2.</param>
/// <param name="data3">Bit array data 3.</param>
/// <param name="data4">Bit array data 4.</param>
/// <param name="value">Value to set the bit to.</param>
public static void Set256(uint index, ref ulong data1, ref ulong data2, ref ulong data3, ref ulong data4, bool value)
{
if (index < 64u)
data1 = (value ? (data1 | (1uL << (int)index)) : (data1 & ~(1uL << (int)index)));
else if (index < 128u)
data2 = (value ? (data2 | (1uL << (int)(index - 64u))) : (data2 & ~(1uL << (int)(index - 64u))));
else if (index < 192u)
data3 = (value ? (data3 | (1uL << (int)(index - 64u))) : (data3 & ~(1uL << (int)(index - 128u))));
else
data4 = (value ? (data4 | (1uL << (int)(index - 64u))) : (data4 & ~(1uL << (int)(index - 192u))));
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 523324f7505f93548a2a27d8fb75e7b0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,219 @@
#ifndef UNITY_CORE_BLIT_INCLUDED
#define UNITY_CORE_BLIT_INCLUDED
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/DynamicScaling.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/GlobalSamplers.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/EntityLighting.hlsl"
#ifdef USE_FULL_PRECISION_BLIT_TEXTURE
TEXTURE2D_X_FLOAT(_BlitTexture);
#else
TEXTURE2D_X(_BlitTexture);
#endif
TEXTURECUBE(_BlitCubeTexture);
uniform float4 _BlitScaleBias;
uniform float4 _BlitScaleBiasRt;
uniform float4 _BlitTexture_TexelSize;
uniform float _BlitMipLevel;
uniform float2 _BlitTextureSize;
uniform uint _BlitPaddingSize;
uniform int _BlitTexArraySlice;
uniform float4 _BlitDecodeInstructions;
struct Attributes
{
uint vertexID : SV_VertexID;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float2 texcoord : TEXCOORD0;
UNITY_VERTEX_OUTPUT_STEREO
};
Varyings Vert(Attributes input)
{
Varyings output;
UNITY_SETUP_INSTANCE_ID(input);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
float4 pos = GetFullScreenTriangleVertexPosition(input.vertexID);
float2 uv = GetFullScreenTriangleTexCoord(input.vertexID);
output.positionCS = pos;
output.texcoord = DYNAMIC_SCALING_APPLY_SCALEBIAS(uv);
return output;
}
Varyings VertQuad(Attributes input)
{
Varyings output;
UNITY_SETUP_INSTANCE_ID(input);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
float4 pos = GetQuadVertexPosition(input.vertexID);
float2 uv = GetQuadTexCoord(input.vertexID);
output.positionCS = pos * float4(_BlitScaleBiasRt.x, _BlitScaleBiasRt.y, 1, 1) + float4(_BlitScaleBiasRt.z, _BlitScaleBiasRt.w, 0, 0);
output.positionCS.xy = output.positionCS.xy * float2(2.0f, -2.0f) + float2(-1.0f, 1.0f); //convert to -1..1
output.texcoord = DYNAMIC_SCALING_APPLY_SCALEBIAS(uv);
return output;
}
Varyings VertQuadPadding(Attributes input)
{
Varyings output;
UNITY_SETUP_INSTANCE_ID(input);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
float2 scalePadding = ((_BlitTextureSize + float(_BlitPaddingSize)) / _BlitTextureSize);
float2 offsetPadding = (float(_BlitPaddingSize) / 2.0) / (_BlitTextureSize + _BlitPaddingSize);
float4 pos = GetQuadVertexPosition(input.vertexID);
float2 uv = GetQuadTexCoord(input.vertexID);
output.positionCS = pos * float4(_BlitScaleBiasRt.x, _BlitScaleBiasRt.y, 1, 1) + float4(_BlitScaleBiasRt.z, _BlitScaleBiasRt.w, 0, 0);
output.positionCS.xy = output.positionCS.xy * float2(2.0f, -2.0f) + float2(-1.0f, 1.0f); //convert to -1..1
output.texcoord = uv;
output.texcoord = (output.texcoord - offsetPadding) * scalePadding;
output.texcoord = DYNAMIC_SCALING_APPLY_SCALEBIAS(output.texcoord);
return output;
}
float4 FragBlit(Varyings input, SamplerState s)
{
#if defined(USE_TEXTURE2D_X_AS_ARRAY) && defined(BLIT_SINGLE_SLICE)
return SAMPLE_TEXTURE2D_ARRAY_LOD(_BlitTexture, s, input.texcoord.xy, _BlitTexArraySlice, _BlitMipLevel);
#endif
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
return SAMPLE_TEXTURE2D_X_LOD(_BlitTexture, s, input.texcoord.xy, _BlitMipLevel);
}
float4 FragNearest(Varyings input) : SV_Target
{
return FragBlit(input, sampler_PointClamp);
}
float4 FragBilinear(Varyings input) : SV_Target
{
return FragBlit(input, sampler_LinearClamp);
}
float4 FragBilinearRepeat(Varyings input) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
float2 uv = input.texcoord.xy;
return SAMPLE_TEXTURE2D_X_LOD(_BlitTexture, sampler_LinearRepeat, uv, _BlitMipLevel);
}
float4 FragNearestRepeat(Varyings input) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
float2 uv = input.texcoord.xy;
return SAMPLE_TEXTURE2D_X_LOD(_BlitTexture, sampler_PointRepeat, uv, _BlitMipLevel);
}
float4 FragOctahedralBilinearRepeat(Varyings input) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
float2 uv = RepeatOctahedralUV(input.texcoord.x, input.texcoord.y);
return SAMPLE_TEXTURE2D_X_LOD(_BlitTexture, sampler_LinearRepeat, uv, _BlitMipLevel);
}
float4 FragOctahedralNearestRepeat(Varyings input) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
float2 uv = RepeatOctahedralUV(input.texcoord.x, input.texcoord.y);
return SAMPLE_TEXTURE2D_X_LOD(_BlitTexture, sampler_PointRepeat, uv, _BlitMipLevel);
}
float4 FragOctahedralProject(Varyings input) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
float2 UV = saturate(input.texcoord);
float3 dir = UnpackNormalOctQuadEncode(2.0f*UV - 1.0f);
return float4(SAMPLE_TEXTURECUBE_LOD(_BlitCubeTexture, sampler_LinearRepeat, dir, _BlitMipLevel).rgb, 1);
}
float4 FragOctahedralProjectNearestRepeat(Varyings input) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
float2 uv = RepeatOctahedralUV(input.texcoord.x, input.texcoord.y);
float3 dir = UnpackNormalOctQuadEncode(2.0f * uv - 1.0f);
#ifdef BLIT_DECODE_HDR
return float4(DecodeHDREnvironment(SAMPLE_TEXTURECUBE_LOD(_BlitCubeTexture, sampler_PointRepeat, dir, _BlitMipLevel), _BlitDecodeInstructions), 1);
#else
return float4(SAMPLE_TEXTURECUBE_LOD(_BlitCubeTexture, sampler_PointRepeat, dir, _BlitMipLevel).rgb, 1);
#endif
}
float4 FragOctahedralProjectBilinearRepeat(Varyings input) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
float2 uv = RepeatOctahedralUV(input.texcoord.x, input.texcoord.y);
float3 dir = UnpackNormalOctQuadEncode(2.0f * uv - 1.0f);
#ifdef BLIT_DECODE_HDR
return float4(DecodeHDREnvironment(SAMPLE_TEXTURECUBE_LOD(_BlitCubeTexture, sampler_LinearRepeat, dir, _BlitMipLevel), _BlitDecodeInstructions), 1);
#else
return float4(SAMPLE_TEXTURECUBE_LOD(_BlitCubeTexture, sampler_LinearRepeat, dir, _BlitMipLevel).rgb, 1);
#endif
}
// 8-bit single channel sampling/format conversions
float4 FragOctahedralProjectLuminance(Varyings input) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
float2 UV = saturate(input.texcoord);
float3 dir = UnpackNormalOctQuadEncode(2.0f*UV - 1.0f);
// sRGB/Rec.709
return Luminance(SAMPLE_TEXTURECUBE_LOD(_BlitCubeTexture, sampler_LinearRepeat, dir, _BlitMipLevel)).xxxx;
}
float4 FragOctahedralProjectRedToRGBA(Varyings input) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
float2 UV = saturate(input.texcoord);
float3 dir = UnpackNormalOctQuadEncode(2.0f*UV - 1.0f);
return SAMPLE_TEXTURECUBE_LOD(_BlitCubeTexture, sampler_LinearRepeat, dir, _BlitMipLevel).rrrr;
}
float4 FragOctahedralProjectAlphaToRGBA(Varyings input) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
float2 UV = saturate(input.texcoord);
float3 dir = UnpackNormalOctQuadEncode(2.0f*UV - 1.0f);
return SAMPLE_TEXTURECUBE_LOD(_BlitCubeTexture, sampler_LinearRepeat, dir, _BlitMipLevel).aaaa;
}
float4 FragBilinearLuminance(Varyings input) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
float2 uv = input.texcoord.xy;
// sRGB/Rec.709
return Luminance(SAMPLE_TEXTURE2D_X_LOD(_BlitTexture, sampler_LinearRepeat, uv, _BlitMipLevel)).xxxx;
}
float4 FragBilinearRedToRGBA(Varyings input) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
float2 uv = input.texcoord.xy;
return SAMPLE_TEXTURE2D_X_LOD(_BlitTexture, sampler_LinearRepeat, uv, _BlitMipLevel).rrrr;
}
float4 FragBilinearAlphaToRGBA(Varyings input) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
float2 uv = input.texcoord.xy;
return SAMPLE_TEXTURE2D_X_LOD(_BlitTexture, sampler_LinearRepeat, uv, _BlitMipLevel).aaaa;
}
#endif //UNITY_CORE_BLIT_INCLUDED
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: d55706914cfa84431bb4f9277ebd1054
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,112 @@
#ifndef UNITY_CORE_BLIT_COLOR_AND_DEPTH_INCLUDED
#define UNITY_CORE_BLIT_COLOR_AND_DEPTH_INCLUDED
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/TextureXR.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/DynamicScaling.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/GlobalSamplers.hlsl"
#pragma multi_compile _ _MSAA_2X _MSAA_4X _MSAA_8X
#if defined(_MSAA_2X)
# define SAMPLE_COUNT 2
#elif defined (_MSAA_4X)
# define SAMPLE_COUNT 4
#elif defined(_MSAA_8X)
# define SAMPLE_COUNT 8
#else
# define SAMPLE_COUNT 1
#endif
#if UNITY_REVERSED_Z
#define DEPTH_DEFAULT_VALUE 1.0
#define DEPTH_OP min
#else
#define DEPTH_DEFAULT_VALUE 0.0
#define DEPTH_OP max
#endif
TEXTURE2D_X(_BlitTexture);
TEXTURE2D(_InputDepthTexture);
TEXTURE2D_X(_InputDepthTextureXR);
TEXTURE2D_X_MSAA(float4, _InputDepthTextureXR_MS);
uniform float4 _BlitScaleBias;
uniform float _BlitMipLevel;
uniform float2 _SourceResolution;
struct Attributes
{
uint vertexID : SV_VertexID;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float2 texcoord : TEXCOORD0;
UNITY_VERTEX_OUTPUT_STEREO
};
Varyings Vert(Attributes input)
{
Varyings output;
UNITY_SETUP_INSTANCE_ID(input);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
float4 pos = GetFullScreenTriangleVertexPosition(input.vertexID);
float2 uv = GetFullScreenTriangleTexCoord(input.vertexID);
output.positionCS = pos;
output.texcoord = DYNAMIC_SCALING_APPLY_SCALEBIAS(uv);
return output;
}
float4 FragColorOnly(Varyings input) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
return SAMPLE_TEXTURE2D_X_LOD(_BlitTexture, sampler_LinearClamp, input.texcoord.xy, _BlitMipLevel);
}
struct PixelData
{
float4 color : SV_Target;
float depth : SV_Depth;
};
PixelData FragColorAndDepth(Varyings input)
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
PixelData pd;
pd.color = SAMPLE_TEXTURE2D_X_LOD(_BlitTexture, sampler_LinearClamp, input.texcoord.xy, _BlitMipLevel);
pd.depth = SAMPLE_TEXTURE2D_LOD(_InputDepthTexture, sampler_PointClamp, input.texcoord.xy, _BlitMipLevel).x;
return pd;
}
float SampleDepth(float2 uv)
{
uint2 pixelCoords = uint2(uv * _SourceResolution);
#if SAMPLE_COUNT == 1
return LOAD_TEXTURE2D_X_LOD(_InputDepthTextureXR, pixelCoords, _BlitMipLevel).x;
#else
float outDepth = DEPTH_DEFAULT_VALUE;
UNITY_UNROLL
for (int i = 0; i < SAMPLE_COUNT; ++i)
outDepth = DEPTH_OP(LOAD_TEXTURE2D_X_MSAA(_InputDepthTextureXR_MS, pixelCoords, i), outDepth).x;
return outDepth;
#endif
}
float FragDepthOnly(Varyings input) : SV_Depth
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
return SampleDepth(input.texcoord.xy);
}
#endif // UNITY_CORE_BLIT_COLOR_AND_DEPTH_INCLUDED
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: cdea49463de0449b48cacd6a3925dcf7
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ab6d43b2dea214c3e8bf2e9aad114f27
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,103 @@
using System;
using System.Collections.Generic;
namespace UnityEngine.Rendering
{
/// <summary>
/// Bridge class for camera captures.
/// </summary>
public static class CameraCaptureBridge
{
private class CameraEntry
{
internal HashSet<Action<RenderTargetIdentifier, CommandBuffer>> actions;
internal IEnumerator<Action<RenderTargetIdentifier, CommandBuffer>> cachedEnumerator;
}
private static readonly Dictionary<Camera, CameraEntry> actionDict = new();
private static bool _enabled;
#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
static void ResetStaticsOnLoad()
{
actionDict.Clear();
_enabled = false;
}
#endif
/// <summary>
/// Enable camera capture.
/// </summary>
public static bool enabled
{
get
{
return _enabled;
}
set
{
_enabled = value;
}
}
/// <summary>
/// Provides the set actions to the renderer to be triggered at the end of the render loop for camera capture
/// </summary>
/// <param name="camera">The camera to get actions for</param>
/// <returns>Enumeration of actions</returns>
public static IEnumerator<Action<RenderTargetIdentifier, CommandBuffer>> GetCaptureActions(Camera camera)
{
if (!actionDict.TryGetValue(camera, out var entry) || entry.actions.Count == 0)
return null;
return entry.actions.GetEnumerator();
}
internal static IEnumerator<Action<RenderTargetIdentifier, CommandBuffer>> GetCachedCaptureActionsEnumerator(Camera camera)
{
if (!actionDict.TryGetValue(camera, out var entry) || entry.actions.Count == 0)
return null;
// internal use only
entry.cachedEnumerator.Reset();
return entry.cachedEnumerator;
}
/// <summary>
/// Adds actions for camera capture
/// </summary>
/// <param name="camera">The camera to add actions for</param>
/// <param name="action">The action to add</param>
public static void AddCaptureAction(Camera camera, Action<RenderTargetIdentifier, CommandBuffer> action)
{
actionDict.TryGetValue(camera, out var entry);
if (entry == null)
{
entry = new CameraEntry {actions = new HashSet<Action<RenderTargetIdentifier, CommandBuffer>>()};
actionDict.Add(camera, entry);
}
entry.actions.Add(action);
entry.cachedEnumerator = entry.actions.GetEnumerator();
}
/// <summary>
/// Removes actions for camera capture
/// </summary>
/// <param name="camera">The camera to remove actions for</param>
/// <param name="action">The action to remove</param>
public static void RemoveCaptureAction(Camera camera, Action<RenderTargetIdentifier, CommandBuffer> action)
{
if (camera == null)
return;
if (actionDict.TryGetValue(camera, out var entry))
{
entry.actions.Remove(action);
entry.cachedEnumerator = entry.actions.GetEnumerator();
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b15bfa6d4567d46ada191b75389088a7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,57 @@
using Unity.Mathematics;
using static Unity.Mathematics.math;
// The contents of this file should be kept is sync with the ShaderLibrary\HDROutput.hlsl "Color Space transforms" section.
namespace UnityEngine.Rendering
{
/// <summary>
/// This class holds a collection of color space transformation matrices.
/// </summary>
public static class ColorSpaceUtils
{
/// <summary>
/// Matrix to convert from color space Rec709 to Rec2020.
/// </summary>
public static readonly float3x3 Rec709ToRec2020Mat = new float3x3(
0.627402f, 0.329292f, 0.043306f,
0.069095f, 0.919544f, 0.011360f,
0.016394f, 0.088028f, 0.895578f
);
/// <summary>
/// Matrix to convert from color space Rec709 to P3D65.
/// </summary>
public static readonly float3x3 Rec709ToP3D65Mat = float3x3(
0.822462f, 0.177538f, 0.000000f,
0.033194f, 0.966806f, 0.000000f,
0.017083f, 0.072397f, 0.910520f
);
/// <summary>
/// Matrix to convert from color space Rec2020 to Rec709.
/// </summary>
public static readonly float3x3 Rec2020ToRec709Mat = float3x3(
1.660496f, -0.587656f, -0.072840f,
-0.124547f, 1.132895f, -0.008348f,
-0.018154f, -0.100597f, 1.118751f
);
/// <summary>
/// Matrix to convert from color space Rec2020 to P3D65.
/// </summary>
public static readonly float3x3 Rec2020ToP3D65Mat = float3x3(
1.343578f, -0.28218f, -0.0613986f,
-0.065298f, 1.075788f, -0.010491f,
0.002822f, -0.019599f, 1.016777f
);
/// <summary>
/// Matrix to convert from color space P3D65 to Rec2020.
/// </summary>
public static readonly float3x3 P3D65ToRec2020Mat = float3x3(
0.753833f, 0.198597f, 0.04757f,
0.045744f, 0.941777f, 0.012479f,
-0.001210f, 0.017602f, 0.983609f
);
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 704b415098e24d8fbc8a7ad9b3a16f15
timeCreated: 1678104478
@@ -0,0 +1,315 @@
namespace UnityEngine.Rendering
{
// Has to be kept in sync with PhysicalCamera.hlsl
/// <summary>
/// A set of color manipulation utilities.
/// </summary>
public static class ColorUtils
{
const float k_LightMeterCalibrationConstant = 12.5f;
const float k_LensAttenuation = 0.65f;
/// <summary>
/// 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.
/// </summary>
public static float s_LightMeterCalibrationConstant = k_LightMeterCalibrationConstant;
/// <summary>
/// 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).
/// </summary>
public static float s_LensAttenuation = k_LensAttenuation;
#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
static void ResetStaticsOnLoad()
{
s_LightMeterCalibrationConstant = k_LightMeterCalibrationConstant;
s_LensAttenuation = k_LensAttenuation;
}
#endif
/// <summary>
/// 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
/// </summary>
static public float lensImperfectionExposureScale
{
get => (78.0f / (100.0f * s_LensAttenuation));
}
/// <summary>
/// An analytical model of chromaticity of the standard illuminant, by Judd et al.
/// http://en.wikipedia.org/wiki/Standard_illuminant#Illuminant_series_D
/// Slightly modifed to adjust it with the D65 white point (x=0.31271, y=0.32902).
/// </summary>
/// <param name="x">The input value representing the chromaticity measure.</param>
/// <returns>Returns the calculated value of the standard illuminant's Y-coordinate based on the input x.</returns>
public static float StandardIlluminantY(float x) => 2.87f * x - 3f * x * x - 0.27509507f;
/// <summary>
/// CIE xy chromaticity to CAT02 LMS.
/// http://en.wikipedia.org/wiki/LMS_color_space#CAT02
/// </summary>
/// <param name="x">The x value in the CIE xy chromaticity.</param>
/// <param name="y">The y value in the CIE xy chromaticity.</param>
/// <returns>Vector3 representing the conversion from CIE xy chromaticity to CAT02 LMS color space.</returns>
public static Vector3 CIExyToLMS(float x, float y)
{
float Y = 1f;
float X = Y * x / y;
float Z = Y * (1f - x - y) / y;
float L = 0.7328f * X + 0.4296f * Y - 0.1624f * Z;
float M = -0.7036f * X + 1.6975f * Y + 0.0061f * Z;
float S = 0.0030f * X + 0.0136f * Y + 0.9834f * Z;
return new Vector3(L, M, S);
}
/// <summary>
/// Converts white balancing parameter to LMS coefficients.
/// </summary>
/// <param name="temperature">A temperature offset, in range [-100;100].</param>
/// <param name="tint">A tint offset, in range [-100;100].</param>
/// <returns>LMS coefficients.</returns>
public static Vector3 ColorBalanceToLMSCoeffs(float temperature, float tint)
{
// Range ~[-1.5;1.5] works best
float t1 = temperature / 65f;
float t2 = tint / 65f;
// Get the CIE xy chromaticity of the reference white point.
// Note: 0.31271 = x value on the D65 white point
float x = 0.31271f - t1 * (t1 < 0f ? 0.1f : 0.05f);
float y = StandardIlluminantY(x) + t2 * 0.05f;
// Calculate the coefficients in the LMS space.
var w1 = new Vector3(0.949237f, 1.03542f, 1.08728f); // D65 white point
var w2 = CIExyToLMS(x, y);
return new Vector3(w1.x / w2.x, w1.y / w2.y, w1.z / w2.z);
}
/// <summary>
/// Pre-filters shadows, midtones and highlights trackball values for shader use.
/// </summary>
/// <param name="inShadows">A color used for shadows.</param>
/// <param name="inMidtones">A color used for midtones.</param>
/// <param name="inHighlights">A color used for highlights.</param>
/// <returns>The three input colors pre-filtered for shader use.</returns>
public static (Vector4, Vector4, Vector4) PrepareShadowsMidtonesHighlights(in Vector4 inShadows, in Vector4 inMidtones, in Vector4 inHighlights)
{
float weight;
var shadows = inShadows;
shadows.x = Mathf.GammaToLinearSpace(shadows.x);
shadows.y = Mathf.GammaToLinearSpace(shadows.y);
shadows.z = Mathf.GammaToLinearSpace(shadows.z);
weight = shadows.w * (Mathf.Sign(shadows.w) < 0f ? 1f : 4f);
shadows.x = Mathf.Max(shadows.x + weight, 0f);
shadows.y = Mathf.Max(shadows.y + weight, 0f);
shadows.z = Mathf.Max(shadows.z + weight, 0f);
shadows.w = 0f;
var midtones = inMidtones;
midtones.x = Mathf.GammaToLinearSpace(midtones.x);
midtones.y = Mathf.GammaToLinearSpace(midtones.y);
midtones.z = Mathf.GammaToLinearSpace(midtones.z);
weight = midtones.w * (Mathf.Sign(midtones.w) < 0f ? 1f : 4f);
midtones.x = Mathf.Max(midtones.x + weight, 0f);
midtones.y = Mathf.Max(midtones.y + weight, 0f);
midtones.z = Mathf.Max(midtones.z + weight, 0f);
midtones.w = 0f;
var highlights = inHighlights;
highlights.x = Mathf.GammaToLinearSpace(highlights.x);
highlights.y = Mathf.GammaToLinearSpace(highlights.y);
highlights.z = Mathf.GammaToLinearSpace(highlights.z);
weight = highlights.w * (Mathf.Sign(highlights.w) < 0f ? 1f : 4f);
highlights.x = Mathf.Max(highlights.x + weight, 0f);
highlights.y = Mathf.Max(highlights.y + weight, 0f);
highlights.z = Mathf.Max(highlights.z + weight, 0f);
highlights.w = 0f;
return (shadows, midtones, highlights);
}
/// <summary>
/// Pre-filters lift, gamma and gain trackball values for shader use.
/// </summary>
/// <param name="inLift">A color used for lift.</param>
/// <param name="inGamma">A color used for gamma.</param>
/// <param name="inGain">A color used for gain.</param>
/// <returns>The three input colors pre-filtered for shader use.</returns>
public static (Vector4, Vector4, Vector4) PrepareLiftGammaGain(in Vector4 inLift, in Vector4 inGamma, in Vector4 inGain)
{
var lift = inLift;
lift.x = Mathf.GammaToLinearSpace(lift.x) * 0.15f;
lift.y = Mathf.GammaToLinearSpace(lift.y) * 0.15f;
lift.z = Mathf.GammaToLinearSpace(lift.z) * 0.15f;
float lumLift = Luminance(lift);
lift.x = lift.x - lumLift + lift.w;
lift.y = lift.y - lumLift + lift.w;
lift.z = lift.z - lumLift + lift.w;
lift.w = 0f;
var gamma = inGamma;
gamma.x = Mathf.GammaToLinearSpace(gamma.x) * 0.8f;
gamma.y = Mathf.GammaToLinearSpace(gamma.y) * 0.8f;
gamma.z = Mathf.GammaToLinearSpace(gamma.z) * 0.8f;
float lumGamma = Luminance(gamma);
gamma.w += 1f;
gamma.x = 1f / Mathf.Max(gamma.x - lumGamma + gamma.w, 1e-03f);
gamma.y = 1f / Mathf.Max(gamma.y - lumGamma + gamma.w, 1e-03f);
gamma.z = 1f / Mathf.Max(gamma.z - lumGamma + gamma.w, 1e-03f);
gamma.w = 0f;
var gain = inGain;
gain.x = Mathf.GammaToLinearSpace(gain.x) * 0.8f;
gain.y = Mathf.GammaToLinearSpace(gain.y) * 0.8f;
gain.z = Mathf.GammaToLinearSpace(gain.z) * 0.8f;
float lumGain = Luminance(gain);
gain.w += 1f;
gain.x = gain.x - lumGain + gain.w;
gain.y = gain.y - lumGain + gain.w;
gain.z = gain.z - lumGain + gain.w;
gain.w = 0f;
return (lift, gamma, gain);
}
/// <summary>
/// Pre-filters colors used for the split toning effect.
/// </summary>
/// <param name="inShadows">A color used for shadows.</param>
/// <param name="inHighlights">A color used for highlights.</param>
/// <param name="balance">The balance between the shadow and highlight colors, in range [-100;100].</param>
/// <returns>The two input colors pre-filtered for shader use.</returns>
public static (Vector4, Vector4) PrepareSplitToning(in Vector4 inShadows, in Vector4 inHighlights, float balance)
{
// As counter-intuitive as it is, to make split-toning work the same way it does in
// Adobe products we have to do all the maths in sRGB... So do not convert these to
// linear before sending them to the shader, this isn't a bug!
var shadows = inShadows;
var highlights = inHighlights;
// Balance is stored in `shadows.w`
shadows.w = balance / 100f;
highlights.w = 0f;
return (shadows, highlights);
}
/// <summary>
/// Returns the luminance of the specified color. The input is considered to be in linear
/// space with sRGB primaries and a D65 white point.
/// </summary>
/// <param name="color">The color to compute the luminance for.</param>
/// <returns>A luminance value.</returns>
public static float Luminance(in Color color) => color.r * 0.2126729f + color.g * 0.7151522f + color.b * 0.072175f;
/// <summary>
/// Computes an exposure value (EV100) from physical camera settings.
/// </summary>
/// <param name="aperture">The camera aperture.</param>
/// <param name="shutterSpeed">The camera exposure time.</param>
/// <param name="ISO">The camera sensor sensitivity.</param>
/// <returns>An exposure value, in EV100.</returns>
public static float ComputeEV100(float aperture, float shutterSpeed, float ISO)
{
// References:
// "Moving Frostbite to PBR" (Sébastien Lagarde & Charles de Rousiers)
// https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf
// "Implementing a Physically Based Camera" (Padraic Hennessy)
// https://placeholderart.wordpress.com/2014/11/16/implementing-a-physically-based-camera-understanding-exposure/
// EV number is defined as:
// 2^ EV_s = N^2 / t and EV_s = EV_100 + log2 (S /100)
// This gives
// EV_s = log2 (N^2 / t)
// EV_100 + log2 (S /100) = log2 (N^2 / t)
// EV_100 = log2 (N^2 / t) - log2 (S /100)
// EV_100 = log2 (N^2 / t . 100 / S)
return Mathf.Log((aperture * aperture) / shutterSpeed * 100f / ISO, 2f);
}
/// <summary>
/// Converts an exposure value (EV100) to a linear multiplier.
/// </summary>
/// <param name="EV100">The exposure value to convert, in EV100.</param>
/// <returns>A linear multiplier.</returns>
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 * s_LensAttenuation) * 2^ EV_100
// = lensImperfectionExposureScale * 2^ EV
// Reference: http://en.wikipedia.org/wiki/Film_speed
float maxLuminance = lensImperfectionExposureScale * Mathf.Pow(2.0f, EV100);
return 1.0f / maxLuminance;
}
/// <summary>
/// Converts a linear multiplier to an exposure value (EV100).
/// </summary>
/// <param name="exposure">A linear multiplier.</param>
/// <returns>An exposure value, in EV100.</returns>
public static float ConvertExposureToEV100(float exposure)
{
// Compute the maximum luminance possible with H_sbs sensitivity
// EV_100 = log2( S * q / (78 * 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 / (lensImperfectionExposureScale * exposure), 2.0f);
}
/// <summary>
/// Computes an exposure value (EV100) from an average luminance value.
/// </summary>
/// <param name="avgLuminance">An average luminance value.</param>
/// <returns>An exposure value, in EV100.</returns>
public static float ComputeEV100FromAvgLuminance(float avgLuminance)
{
// 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
// 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);
}
/// <summary>
/// Computes the required ISO to reach <paramref name="targetEV100"/>.
/// </summary>
/// <param name="aperture">The camera aperture.</param>
/// <param name="shutterSpeed">The camera exposure time.</param>
/// <param name="targetEV100">The target exposure value (EV100) to reach.</param>
/// <returns>The required sensor sensitivity (ISO).</returns>
public static float ComputeISO(float aperture, float shutterSpeed, float targetEV100) => ((aperture * aperture) * 100f) / (shutterSpeed * Mathf.Pow(2f, targetEV100));
/// <summary>
/// Converts a color value to its 32-bit hexadecimal representation.
/// </summary>
/// <param name="c">The color to convert.</param>
/// <returns>A 32-bit hexadecimal representation of the color.</returns>
public static uint ToHex(Color c) => ((uint)(c.a * 255) << 24) | ((uint)(c.r * 255) << 16) | ((uint)(c.g * 255) << 8) | (uint)(c.b * 255);
/// <summary>
/// Converts a 32-bit hexadecimal value to a color value.
/// </summary>
/// <param name="hex">A 32-bit hexadecimal value.</param>
/// <returns>A color value.</returns>
public static Color ToRGBA(uint hex) => new Color(((hex >> 16) & 0xff) / 255f, ((hex >> 8) & 0xff) / 255f, (hex & 0xff) / 255f, ((hex >> 24) & 0xff) / 255f);
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3ead45caf3764e9458334a2b99e6d421
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,192 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Experimental.Rendering;
namespace UnityEngine.Rendering
{
using UnityObject = UnityEngine.Object;
/// <summary>
/// Set of utility functions for the Core Scriptable Render Pipeline Library related to Matrix operations
/// </summary>
public static class CoreMatrixUtils
{
/// <summary>
/// This function provides the equivalent of multiplying matrix parameter inOutMatrix with a translation matrix defined by the parameter translation.
/// The order of the equivalent multiplication is inOutMatrix * translation.
/// </summary>
/// <param name="inOutMatrix">Matrix to multiply with translation.</param>
/// <param name="translation">Translation component to multiply to the matrix.</param>
public static void MatrixTimesTranslation(ref Matrix4x4 inOutMatrix, Vector3 translation)
{
inOutMatrix.m03 += (inOutMatrix.m00 * translation.x + inOutMatrix.m01 * translation.y + inOutMatrix.m02 * translation.z);
inOutMatrix.m13 += (inOutMatrix.m10 * translation.x + inOutMatrix.m11 * translation.y + inOutMatrix.m12 * translation.z);
inOutMatrix.m23 += (inOutMatrix.m20 * translation.x + inOutMatrix.m21 * translation.y + inOutMatrix.m22 * translation.z);
}
/// <summary>
/// This function provides the equivalent of multiplying a translation matrix defined by the parameter translation with the matrix specified by the parameter inOutMatrix.
/// The order of the equivalent multiplication is translation * inOutMatrix.
/// </summary>
/// <param name="inOutMatrix">Matrix to multiply with translation.</param>
/// <param name="translation">Translation component to multiply to the matrix.</param>
public static void TranslationTimesMatrix(ref Matrix4x4 inOutMatrix, Vector3 translation)
{
inOutMatrix.m00 += translation.x * inOutMatrix.m30;
inOutMatrix.m01 += translation.x * inOutMatrix.m31;
inOutMatrix.m02 += translation.x * inOutMatrix.m32;
inOutMatrix.m03 += translation.x * inOutMatrix.m33;
inOutMatrix.m10 += translation.y * inOutMatrix.m30;
inOutMatrix.m11 += translation.y * inOutMatrix.m31;
inOutMatrix.m12 += translation.y * inOutMatrix.m32;
inOutMatrix.m13 += translation.y * inOutMatrix.m33;
inOutMatrix.m20 += translation.z * inOutMatrix.m30;
inOutMatrix.m21 += translation.z * inOutMatrix.m31;
inOutMatrix.m22 += translation.z * inOutMatrix.m32;
inOutMatrix.m23 += translation.z * inOutMatrix.m33;
}
/// <summary>
/// Multiplies a matrix with a perspective matrix. This function is faster than performing the full matrix multiplication.
/// The operation order is perspective * rhs.
/// </summary>
/// <param name="perspective">The perspective matrix to multiply with rhs.</param>
/// <param name="rhs">A matrix to be multiply to perspective.</param>
/// <returns>Returns the matrix that is the result of the multiplication.</returns>
public static Matrix4x4 MultiplyPerspectiveMatrix(Matrix4x4 perspective, Matrix4x4 rhs)
{
Matrix4x4 outMat;
outMat.m00 = perspective.m00 * rhs.m00;
outMat.m01 = perspective.m00 * rhs.m01;
outMat.m02 = perspective.m00 * rhs.m02;
outMat.m03 = perspective.m00 * rhs.m03;
outMat.m10 = perspective.m11 * rhs.m10;
outMat.m11 = perspective.m11 * rhs.m11;
outMat.m12 = perspective.m11 * rhs.m12;
outMat.m13 = perspective.m11 * rhs.m13;
outMat.m20 = perspective.m22 * rhs.m20 + perspective.m23 * rhs.m30;
outMat.m21 = perspective.m22 * rhs.m21 + perspective.m23 * rhs.m31;
outMat.m22 = perspective.m22 * rhs.m22 + perspective.m23 * rhs.m32;
outMat.m23 = perspective.m22 * rhs.m23 + perspective.m23 * rhs.m33;
outMat.m30 = -rhs.m20;
outMat.m31 = -rhs.m21;
outMat.m32 = -rhs.m22;
outMat.m33 = -rhs.m23;
return outMat;
}
// An orthographic projection is centered if (right+left) == 0 and (top+bottom) == 0
private static Matrix4x4 MultiplyOrthoMatrixCentered(Matrix4x4 ortho, Matrix4x4 rhs)
{
Matrix4x4 outMat;
outMat.m00 = ortho.m00 * rhs.m00;
outMat.m01 = ortho.m00 * rhs.m01;
outMat.m02 = ortho.m00 * rhs.m02;
outMat.m03 = ortho.m00 * rhs.m03;
outMat.m10 = ortho.m11 * rhs.m10;
outMat.m11 = ortho.m11 * rhs.m11;
outMat.m12 = ortho.m11 * rhs.m12;
outMat.m13 = ortho.m11 * rhs.m13;
outMat.m20 = ortho.m22 * rhs.m20 + ortho.m23 * rhs.m30;
outMat.m21 = ortho.m22 * rhs.m21 + ortho.m23 * rhs.m31;
outMat.m22 = ortho.m22 * rhs.m22 + ortho.m23 * rhs.m32;
outMat.m23 = ortho.m22 * rhs.m23 + ortho.m23 * rhs.m33;
outMat.m30 = rhs.m20;
outMat.m31 = rhs.m21;
outMat.m32 = rhs.m22;
outMat.m33 = rhs.m23;
return outMat;
}
// General case has m03 and m13 != 0
private static Matrix4x4 MultiplyGenericOrthoMatrix(Matrix4x4 ortho, Matrix4x4 rhs)
{
Matrix4x4 outMat;
outMat.m00 = ortho.m00 * rhs.m00 + ortho.m03 * rhs.m30;
outMat.m01 = ortho.m00 * rhs.m01 + ortho.m03 * rhs.m31;
outMat.m02 = ortho.m00 * rhs.m02 + ortho.m03 * rhs.m32;
outMat.m03 = ortho.m00 * rhs.m03 + ortho.m03 * rhs.m33;
outMat.m10 = ortho.m11 * rhs.m10 + ortho.m13 * rhs.m30;
outMat.m11 = ortho.m11 * rhs.m11 + ortho.m13 * rhs.m31;
outMat.m12 = ortho.m11 * rhs.m12 + ortho.m13 * rhs.m32;
outMat.m13 = ortho.m11 * rhs.m13 + ortho.m13 * rhs.m33;
outMat.m20 = ortho.m22 * rhs.m20 + ortho.m23 * rhs.m30;
outMat.m21 = ortho.m22 * rhs.m21 + ortho.m23 * rhs.m31;
outMat.m22 = ortho.m22 * rhs.m22 + ortho.m23 * rhs.m32;
outMat.m23 = ortho.m22 * rhs.m23 + ortho.m23 * rhs.m33;
outMat.m30 = rhs.m20;
outMat.m31 = rhs.m21;
outMat.m32 = rhs.m22;
outMat.m33 = rhs.m23;
return outMat;
}
/// <summary>
/// Multiplies a matrix with an orthographic matrix. This function is faster than performing the full matrix multiplication.
/// The operation order is ortho * rhs.
/// </summary>
/// <param name="ortho">The ortho matrix to multiply with rhs.</param>
/// <param name="rhs">A matrix to be multiply to perspective.</param>
/// <param name="centered">If true, it means that right and left are equivalently distant from center and similarly top/bottom are equivalently distant from center.</param>
/// <returns>Returns the matrix that is the result of the multiplication.</returns>
public static Matrix4x4 MultiplyOrthoMatrix(Matrix4x4 ortho, Matrix4x4 rhs, bool centered)
{
return centered ? MultiplyGenericOrthoMatrix(ortho, rhs) : MultiplyOrthoMatrixCentered(ortho, rhs);
}
/// <summary>
/// Multiplies a matrix with a projection matrix. This function is faster than performing the full matrix multiplication.
/// The operation order is projMatrix * rhs.
/// </summary>
/// <param name="projMatrix">The projection matrix to multiply with rhs.</param>
/// <param name="rhs">A matrix to be multiply to perspective.</param>
/// <param name="orthoCentered">If true, the projection matrix is a centered ( right+left == top+bottom == 0) orthographic projection, otherwise it is a perspective matrix..</param>
/// <returns>Returns the matrix that is the result of the multiplication.</returns>
public static Matrix4x4 MultiplyProjectionMatrix(Matrix4x4 projMatrix, Matrix4x4 rhs, bool orthoCentered)
{
return orthoCentered
? MultiplyOrthoMatrixCentered(projMatrix, rhs)
: MultiplyPerspectiveMatrix(projMatrix, rhs);
}
/// <summary>
/// Returns the world-space camera position from a view matrix whose rotation is orthonormal, computing
/// <c>-transpose(R) * t</c> (R = upper-left 3x3 rotation, t = column 3 translation). This is faster than inverting
/// the full matrix (<c>viewMatrix.inverse.GetColumn(3)</c>), at roughly nine multiplies.
/// </summary>
/// <remarks>
/// The result is correct only under two preconditions:
/// <list type="bullet">
/// <item>The rotation R must be orthonormal, so that transpose(R) == inverse(R). A pure rotation/translation view
/// matrix qualifies (Unity's view-space Z flip is fine); a matrix with scale, skew, or projection does not, so
/// invert the full matrix for those.</item>
/// <item>The view matrix must be world-absolute. A camera-relative matrix (for example, HDRP strips the camera
/// translation) yields a camera-relative result, not the absolute world position.</item>
/// </list>
/// </remarks>
/// <param name="viewMatrix">The world-absolute view matrix, with orthonormal rotation, to extract the camera position from.</param>
/// <returns>The world-space camera position.</returns>
public static Vector3 GetWorldPositionFromOrthonormalViewMatrix(in Matrix4x4 viewMatrix)
{
Vector3 t = viewMatrix.GetPosition();
return new Vector3(
-(viewMatrix.m00 * t.x + viewMatrix.m10 * t.y + viewMatrix.m20 * t.z),
-(viewMatrix.m01 * t.x + viewMatrix.m11 * t.y + viewMatrix.m21 * t.z),
-(viewMatrix.m02 * t.x + viewMatrix.m12 * t.y + viewMatrix.m22 * t.z));
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d96647d3fcceac0429f12837ce7e2e3e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,67 @@
using System;
using System.Collections.Concurrent;
using System.Linq.Expressions;
using System.Reflection;
namespace UnityEngine.Rendering
{
// This file can't be in the editor assembly as we need to access it in runtime-editor-specific
// places like OnGizmo etc and we don't want to add the editor assembly as a dependency of the
// runtime one
// The UI layout/styling in this panel is broken and can't match the one from built-ins
// preference panels as everything needed is internal/private (at the time of writing this
// comment)
#if UNITY_EDITOR
using UnityEditor;
public static class CoreRenderPipelinePreferences
{
// We do not want that GC frees the preferences that have been added, used to store their references
static readonly ConcurrentStack<object> s_ColorPref = new ConcurrentStack<object>();
#region Volumes Gizmo Color
[Obsolete("Use VolumePreferences #from(2022.1)")]
public static Color volumeGizmoColor { get; } = new Color(0.2f, 0.8f, 0.1f, 0.125f);
#endregion
#region Preview Camera Background Color
static readonly Color kPreviewCameraBackgroundColorDefault = new Color(82f / 255.0f, 82f / 255.0f, 82.0f / 255.0f, 0.0f);
public static Color previewBackgroundColor => kPreviewCameraBackgroundColorDefault;
#endregion
/// <summary>
/// Adds a <see cref="PrefColor"/> into the **Preferences > Colors** panel./>
/// </summary>
/// <param name="name">The name the color has in the **Colors** panel. This is in the format of 'group/name'.</param>
/// <param name="defaultColor">The initial color to use for the new entry in the **Colors** panel. This is also the value Unity uses when it resets the colors to their defaults.</param>
public static Func<Color> RegisterPreferenceColor(string name, Color defaultColor)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentException("You must give a valid name for the color property", nameof(name));
// PrefColor is the type to use to have a Color that is customizable inside the Preference/Colors panel.
// Sadly it is internal so we must create it and grab color from it by reflection.
Type prefColorType = typeof(Editor).Assembly.GetType("UnityEditor.PrefColor");
PropertyInfo colorInfo = prefColorType.GetProperty("Color");
var colorPref = Activator.CreateInstance(prefColorType, name, defaultColor.r, defaultColor.g, defaultColor.b, defaultColor.a);
s_ColorPref.Push(colorPref);
MemberExpression colorProperty = Expression.Property(Expression.Constant(colorPref, prefColorType), colorInfo);
// Make sure that the new preference color is being loaded into the Preference/Colors panel
MethodInfo loadMethod = prefColorType.GetMethod("Load");
loadMethod.Invoke(colorPref, null);
// Return the method to obtain the color
return Expression.Lambda<Func<Color>>(colorProperty).Compile();
}
}
#endif
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b285dd5cbeada294fa9399020ef6921f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: c86deb7236b4fe146b8b75ab0ac89586
timeCreated: 1507041147
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,59 @@
using System;
using Unity.Collections;
namespace UnityEngine.Rendering.Universal
{
/// <summary>
/// Class that holds data related to culling.
/// </summary>
public class CullContextData : ContextItem
{
internal ScriptableRenderContext? m_RenderContext;
/// <inheritdoc/>
public override void Reset()
{
m_RenderContext = null;
}
/// <summary>
/// Assigns the render context once at initialization time.
/// </summary>
/// <param name="renderContext">The render context to assign.</param>
public void SetRenderContext(in ScriptableRenderContext renderContext)
{
m_RenderContext = renderContext;
}
/// <summary>
/// Performs scene culling based on the provided parameters.
/// </summary>
/// <param name="parameters">The parameters used for the culling.</param>
/// <returns>The culling results.</returns>
public CullingResults Cull(ref ScriptableCullingParameters parameters)
{
if (!m_RenderContext.HasValue)
{
throw new InvalidOperationException("The ScriptableRenderContext member is not set.");
}
return m_RenderContext.Value.Cull(ref parameters);
}
/// <summary>
/// Performs shadow casters culling based on the provided parameters.
/// </summary>
/// <param name="cullingResults">The scene culling results.</param>
/// <param name="shadowCastersCullingInfos">The shadow casters culling informations.</param>
/// <exception cref="InvalidOperationException">The parameters used for the shadow culling.</exception>
public void CullShadowCasters(CullingResults cullingResults, ShadowCastersCullingInfos shadowCastersCullingInfos)
{
if (!m_RenderContext.HasValue)
{
throw new InvalidOperationException("The ScriptableRenderContext member is not set.");
}
m_RenderContext.Value.CullShadowCasters(cullingResults, shadowCastersCullingInfos);
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: fba8806c6dc73604d8243aebe488e60d
@@ -0,0 +1,31 @@
using System;
namespace UnityEngine.Rendering
{
/// <summary>
/// Delegate utility class.
/// </summary>
public static class DelegateUtility
{
/// <summary>
/// Cast a delegate.
/// </summary>
/// <param name="source">Source delegate.</param>
/// <param name="type">Type of the delegate.</param>
/// <returns>Cast delegate.</returns>
public static Delegate Cast(Delegate source, Type type)
{
if (source == null)
return null;
Delegate[] delegates = source.GetInvocationList();
if (delegates.Length == 1)
return Delegate.CreateDelegate(type,
delegates[0].Target, delegates[0].Method);
Delegate[] delegatesDest = new Delegate[delegates.Length];
for (int nDelegate = 0; nDelegate < delegates.Length; nDelegate++)
delegatesDest[nDelegate] = Delegate.CreateDelegate(type,
delegates[nDelegate].Target, delegates[nDelegate].Method);
return Delegate.Combine(delegatesDest);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 13bcacf4237fb3c4e9b547cc7420a120
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,204 @@
using System;
using Unity.Collections;
using UnityEngine.Experimental.Rendering;
namespace UnityEngine.Rendering
{
/// <summary>
/// Utility functions relating to FidelityFX Super Resolution (FSR)
///
/// These functions are expected to be used in conjuction with the helper functions provided by FSRCommon.hlsl.
/// </summary>
public static class FSRUtils
{
/// Shader constant ids used to communicate with the FSR shader implementation
static class ShaderConstants
{
// EASU
public static readonly int _FsrEasuConstants0 = Shader.PropertyToID("_FsrEasuConstants0");
public static readonly int _FsrEasuConstants1 = Shader.PropertyToID("_FsrEasuConstants1");
public static readonly int _FsrEasuConstants2 = Shader.PropertyToID("_FsrEasuConstants2");
public static readonly int _FsrEasuConstants3 = Shader.PropertyToID("_FsrEasuConstants3");
// RCAS
public static readonly int _FsrRcasConstants = Shader.PropertyToID("_FsrRcasConstants");
}
/// <summary>
/// Sets the constant values required by the FSR EASU shader on the provided command buffer
///
/// Logic ported from "FsrEasuCon()" in Runtime/PostProcessing/Shaders/ffx/ffx_fsr1.hlsl
/// </summary>
/// <param name="cmd">Command buffer to modify</param>
/// <param name="inputViewportSizeInPixels">This the rendered image resolution being upscaled</param>
/// <param name="inputImageSizeInPixels">This is the resolution of the resource containing the input image (useful for dynamic resolution)</param>
/// <param name="outputImageSizeInPixels">This is the display resolution which the input image gets upscaled to</param>
public static void SetEasuConstants(CommandBuffer cmd, Vector2 inputViewportSizeInPixels, Vector2 inputImageSizeInPixels, Vector2 outputImageSizeInPixels)
{
Vector4 constants0;
Vector4 constants1;
Vector4 constants2;
Vector4 constants3;
// Output integer position to a pixel position in viewport.
constants0.x = (inputViewportSizeInPixels.x / outputImageSizeInPixels.x);
constants0.y = (inputViewportSizeInPixels.y / outputImageSizeInPixels.y);
constants0.z = (0.5f * inputViewportSizeInPixels.x / outputImageSizeInPixels.x - 0.5f);
constants0.w = (0.5f * inputViewportSizeInPixels.y / outputImageSizeInPixels.y - 0.5f);
// Viewport pixel position to normalized image space.
// This is used to get upper-left of 'F' tap.
constants1.x = (1.0f / inputImageSizeInPixels.x);
constants1.y = (1.0f / inputImageSizeInPixels.y);
// Centers of gather4, first offset from upper-left of 'F'.
// +---+---+
// | | |
// +--(0)--+
// | b | c |
// +---F---+---+---+
// | e | f | g | h |
// +--(1)--+--(2)--+
// | i | j | k | l |
// +---+---+---+---+
// | n | o |
// +--(3)--+
// | | |
// +---+---+
constants1.z = (1.0f / inputImageSizeInPixels.x);
constants1.w = (-1.0f / inputImageSizeInPixels.y);
// These are from (0) instead of 'F'.
constants2.x = (-1.0f / inputImageSizeInPixels.x);
constants2.y = (2.0f / inputImageSizeInPixels.y);
constants2.z = (1.0f / inputImageSizeInPixels.x);
constants2.w = (2.0f / inputImageSizeInPixels.y);
constants3.x = (0.0f / inputImageSizeInPixels.x);
constants3.y = (4.0f / inputImageSizeInPixels.y);
// Fill the last constant with zeros to avoid using uninitialized memory
constants3.z = 0.0f;
constants3.w = 0.0f;
cmd.SetGlobalVector(ShaderConstants._FsrEasuConstants0, constants0);
cmd.SetGlobalVector(ShaderConstants._FsrEasuConstants1, constants1);
cmd.SetGlobalVector(ShaderConstants._FsrEasuConstants2, constants2);
cmd.SetGlobalVector(ShaderConstants._FsrEasuConstants3, constants3);
}
/// <summary>
/// Sets the constant values required by the FSR EASU shader on the provided command buffer
///
/// Logic ported from "FsrEasuCon()" in Runtime/PostProcessing/Shaders/ffx/ffx_fsr1.hlsl
/// </summary>
/// <param name="cmd">RasterCommandBuffer/ComputeCommandBuffer/UnsafeCommandBuffer to modify</param>
/// <param name="inputViewportSizeInPixels">This the rendered image resolution being upscaled</param>
/// <param name="inputImageSizeInPixels">This is the resolution of the resource containing the input image (useful for dynamic resolution)</param>
/// <param name="outputImageSizeInPixels">This is the display resolution which the input image gets upscaled to</param>
public static void SetEasuConstants(BaseCommandBuffer cmd, Vector2 inputViewportSizeInPixels,
Vector2 inputImageSizeInPixels, Vector2 outputImageSizeInPixels)
{
SetEasuConstants(cmd.m_WrappedCommandBuffer, inputViewportSizeInPixels, inputImageSizeInPixels, outputImageSizeInPixels);
}
/// <summary>
/// The maximum sharpness value in stops before the effect of RCAS is no longer visible.
/// This value is used to map between linear and stops.
/// </summary>
internal const float kMaxSharpnessStops = 2.5f;
/// <summary>
/// AMD's FidelityFX Super Resolution integration guide recommends a value of 0.2 for the RCAS sharpness parameter when specified in stops
/// </summary>
public const float kDefaultSharpnessStops = 0.2f;
/// <summary>
/// The default RCAS sharpness parameter as a linear value
/// </summary>
public const float kDefaultSharpnessLinear = (1.0f - (kDefaultSharpnessStops / kMaxSharpnessStops));
/// <summary>
/// Sets the constant values required by the FSR RCAS shader on the provided command buffer
///
/// Logic ported from "FsrRcasCon()" in Runtime/PostProcessing/Shaders/ffx/ffx_fsr1.hlsl
/// For a more user-friendly version of this function, see SetRcasConstantsLinear().
/// </summary>
/// <param name="cmd">Command buffer to modify</param>
/// <param name="sharpnessStops">The scale is {0.0 := maximum, to N>0, where N is the number of stops(halving) of the reduction of sharpness</param>
public static void SetRcasConstants(CommandBuffer cmd, float sharpnessStops = kDefaultSharpnessStops)
{
// Transform from stops to linear value.
float sharpnessLinear = Mathf.Pow(2.0f, -sharpnessStops);
Vector4 constants;
uint sharpnessAsHalf = Mathf.FloatToHalf(sharpnessLinear);
int packedSharpness = (int)(sharpnessAsHalf | (sharpnessAsHalf << 16));
float packedSharpnessAsFloat = BitConverter.Int32BitsToSingle(packedSharpness);
constants.x = sharpnessLinear;
constants.y = packedSharpnessAsFloat;
// Fill the last constant with zeros to avoid using uninitialized memory
constants.z = 0.0f;
constants.w = 0.0f;
cmd.SetGlobalVector(ShaderConstants._FsrRcasConstants, constants);
}
/// <summary>
/// Sets the constant values required by the FSR RCAS shader on the provided command buffer
///
/// Logic ported from "FsrRcasCon()" in Runtime/PostProcessing/Shaders/ffx/ffx_fsr1.hlsl
/// For a more user-friendly version of this function, see SetRcasConstantsLinear().
/// </summary>
/// <param name="cmd">Command buffer to modify</param>
/// <param name="sharpnessStops">The scale is {0.0 := maximum, to N>0, where N is the number of stops(halving) of the reduction of sharpness</param>
public static void SetRcasConstants(BaseCommandBuffer cmd, float sharpnessStops = kDefaultSharpnessStops)
{
SetRcasConstants(cmd.m_WrappedCommandBuffer, sharpnessStops);
}
/// <summary>
/// Sets the constant values required by the FSR RCAS shader on the provided command buffer
///
/// Equivalent to SetRcasConstants(), but handles the sharpness parameter as a linear value instead of one specified in stops.
/// This is intended to simplify code that allows users to configure the sharpening behavior from a GUI.
/// </summary>
/// <param name="cmd">Command buffer to modify</param>
/// <param name="sharpnessLinear">The level of intensity of the sharpening filter where 0.0 is the least sharp and 1.0 is the most sharp</param>
public static void SetRcasConstantsLinear(CommandBuffer cmd, float sharpnessLinear = kDefaultSharpnessLinear)
{
// Ensure that the input value is between 0.0 and 1.0 prevent incorrect results
Assertions.Assert.IsTrue((sharpnessLinear >= 0.0f) && (sharpnessLinear <= 1.0f));
float sharpnessStops = (1.0f - sharpnessLinear) * kMaxSharpnessStops;
SetRcasConstants(cmd, sharpnessStops);
}
/// <summary>
/// Sets the constant values required by the FSR RCAS shader on the provided command buffer
///
/// Equivalent to SetRcasConstants(), but handles the sharpness parameter as a linear value instead of one specified in stops.
/// This is intended to simplify code that allows users to configure the sharpening behavior from a GUI.
/// </summary>
/// <param name="cmd">RasterCommandBuffer to modify</param>
/// <param name="sharpnessLinear">The level of intensity of the sharpening filter where 0.0 is the least sharp and 1.0 is the most sharp</param>
public static void SetRcasConstantsLinear(RasterCommandBuffer cmd, float sharpnessLinear = kDefaultSharpnessLinear)
{
SetRcasConstantsLinear(cmd.m_WrappedCommandBuffer, sharpnessLinear);
}
/// <summary>
/// Returns true if FidelityFX Super Resolution (FSR) is supported on the current system
/// FSR requires the textureGather shader instruction which wasn't supported by OpenGL ES until version 3.1
/// </summary>
/// <returns>True if supported</returns>
public static bool IsSupported()
{
return SystemInfo.graphicsShaderLevel >= 45;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 437e2de0f30b78448b42fe5aafc1d26f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4fd6fd0a85444385a35a13e9adbcb49d
timeCreated: 1663981296
@@ -0,0 +1,297 @@
using System;
using UnityEngine.Rendering.RenderGraphModule;
namespace UnityEngine.Rendering
{
public partial struct GPUPrefixSum
{
[GenerateHLSL]
internal static class ShaderDefs
{
public const int GroupSize = 128;
// Stride of the indirect arguement buffer in uints, the buffer is split into two sections dispatch options ( a lower or upper arguement set )
public const int ArgsBufferStride = 16;
public const int ArgsBufferUpper = 0;
public const int ArgsBufferLower = 8;
public static int DivUpGroup(int value)
{
return (value + GroupSize - 1) / GroupSize;
}
public static int AlignUpGroup(int value)
{
return DivUpGroup(value) * GroupSize;
}
public static void CalculateTotalBufferSize(int maxElementCount, out int totalSize, out int levelCounts)
{
int alignedSupportMaxCount = AlignUpGroup(maxElementCount);
totalSize = alignedSupportMaxCount;
levelCounts = 1;
while (alignedSupportMaxCount > GroupSize)
{
alignedSupportMaxCount = AlignUpGroup(DivUpGroup(alignedSupportMaxCount));
totalSize += alignedSupportMaxCount;
++levelCounts;
}
}
}
/// <summary>
/// Structure defining level offsets.
/// </summary>
[GenerateHLSL]
public struct LevelOffsets
{
/// <summary> Number of levels. </summary>
public uint count;
/// <summary> Level offset. </summary>
public uint offset;
/// <summary> Parent level offset. </summary>
public uint parentOffset;
}
/// <summary>
/// Utility for adapting to render graph usage.
/// </summary>
public struct RenderGraphResources
{
internal int alignedElementCount;
internal int maxBufferCount;
internal int maxLevelCount;
internal BufferHandle prefixBuffer0;
internal BufferHandle prefixBuffer1;
internal BufferHandle totalLevelCountBuffer;
internal BufferHandle levelOffsetBuffer;
internal BufferHandle indirectDispatchArgsBuffer;
/// <summary>The prefix sum result.</summary>
public BufferHandle output => prefixBuffer0;
/// <summary>
/// Creates the render graph buffer resources from an input count.
/// </summary>
/// <param name="newMaxElementCount">The maximum number of elements that the buffer will support.</param>
/// <param name="renderGraph">Render Graph</param>
/// <param name="builder">Render Graph Builder</param>
/// <param name="outputIsTemp">Whether or not to allocate a transient resource.</param>
/// <returns>The created Render Graph Resources.</returns>
[Obsolete("This Create signature is deprecated and will be removed in the future. Please use Create(IBaseRenderGraphBuilder) instead. #from(6000.5)", true)]
public static RenderGraphResources Create(int newMaxElementCount, RenderGraph renderGraph, RenderGraphBuilder builder, bool outputIsTemp = false) { return new RenderGraphResources(); }
/// <summary>
/// Creates the render graph buffer resources from an input count.
/// </summary>
/// <param name="newMaxElementCount">The maximum number of elements that the buffer will support.</param>
/// <param name="renderGraph">Render Graph</param>
/// <param name="builder">Render Graph Builder</param>
/// <param name="outputIsTemp">Whether or not to allocate a transient resource.</param>
/// <returns>The created Render Graph Resources.</returns>
public static RenderGraphResources Create(int newMaxElementCount, RenderGraph renderGraph, IBaseRenderGraphBuilder builder, bool outputIsTemp = false)
{
var resources = new RenderGraphResources();
resources.Initialize(newMaxElementCount, renderGraph, builder, outputIsTemp);
return resources;
}
void Initialize(int newMaxElementCount, RenderGraph renderGraph, IBaseRenderGraphBuilder builder, bool outputIsTemp = false)
{
newMaxElementCount = Math.Max(newMaxElementCount, 1);
ShaderDefs.CalculateTotalBufferSize(newMaxElementCount, out int totalSize, out int levelCounts);
var prefixBuffer0Desc = new BufferDesc(totalSize, 4, GraphicsBuffer.Target.Raw) { name = "prefixBuffer0" };
if (outputIsTemp)
prefixBuffer0 = builder.CreateTransientBuffer(prefixBuffer0Desc);
else
{
prefixBuffer0 = renderGraph.CreateBuffer(prefixBuffer0Desc);
builder.UseBuffer(prefixBuffer0, AccessFlags.Write);
}
prefixBuffer1 = builder.CreateTransientBuffer(new BufferDesc(newMaxElementCount, 4, GraphicsBuffer.Target.Raw) { name = "prefixBuffer1" });
totalLevelCountBuffer = builder.CreateTransientBuffer(new BufferDesc(1, 4, GraphicsBuffer.Target.Raw) { name = "totalLevelCountBuffer" });
levelOffsetBuffer = builder.CreateTransientBuffer(new BufferDesc(levelCounts, System.Runtime.InteropServices.Marshal.SizeOf<LevelOffsets>(), GraphicsBuffer.Target.Structured) { name = "levelOffsetBuffer" });
indirectDispatchArgsBuffer = builder.CreateTransientBuffer(new BufferDesc(ShaderDefs.ArgsBufferStride * levelCounts, sizeof(uint), GraphicsBuffer.Target.Structured | GraphicsBuffer.Target.IndirectArguments) { name = "indirectDispatchArgsBuffer" });//3 arguments for upp dispatch, 3 arguments for lower dispatch
alignedElementCount = ShaderDefs.AlignUpGroup(newMaxElementCount);
maxBufferCount = totalSize;
maxLevelCount = levelCounts;
}
}
/// <summary>
/// Data structure containing the runtime resources that are bound by the command buffer.
/// </summary>
public struct SupportResources
{
internal bool ownsResources;
internal int alignedElementCount;
internal int maxBufferCount;
internal int maxLevelCount;
internal GraphicsBuffer prefixBuffer0;
internal GraphicsBuffer prefixBuffer1;
internal GraphicsBuffer totalLevelCountBuffer;
internal GraphicsBuffer levelOffsetBuffer;
internal GraphicsBuffer indirectDispatchArgsBuffer;
/// <summary>The prefix sum result.</summary>
public GraphicsBuffer output => prefixBuffer0;
/// <summary>
/// Allocate support resources to accomodate a max count.
/// </summary>
/// <param name="maxElementCount">The max element count.</param>
/// <returns>The created support resources.</returns>
public static SupportResources Create(int maxElementCount)
{
var resources = new SupportResources() { alignedElementCount = 0, ownsResources = true };
resources.Resize(maxElementCount);
return resources;
}
/// <summary>
/// Load supporting resources from Render Graph Resources.
/// </summary>
/// <param name="shaderGraphResources">Render Graph Resources</param>
/// <returns>The created support resources.</returns>
public static SupportResources Load(RenderGraphResources shaderGraphResources)
{
var resources = new SupportResources() { alignedElementCount = 0, ownsResources = false };
resources.LoadFromShaderGraph(shaderGraphResources);
return resources;
}
internal void Resize(int newMaxElementCount)
{
if (!ownsResources)
throw new Exception("Cannot resize resources unless they are owned. Use GpuPrefixSumSupportResources.Create() for this.");
newMaxElementCount = Math.Max(newMaxElementCount, 1); //at bare minimum support a single group.
if (alignedElementCount >= newMaxElementCount)
return;
Dispose();
ShaderDefs.CalculateTotalBufferSize(newMaxElementCount, out int totalSize, out int levelCounts);
alignedElementCount = ShaderDefs.AlignUpGroup(newMaxElementCount);
maxBufferCount = totalSize;
maxLevelCount = levelCounts;
prefixBuffer0 = new GraphicsBuffer(GraphicsBuffer.Target.Raw, totalSize, 4);
prefixBuffer1 = new GraphicsBuffer(GraphicsBuffer.Target.Raw, newMaxElementCount, 4);
totalLevelCountBuffer = new GraphicsBuffer(GraphicsBuffer.Target.Raw, 1, 4);
levelOffsetBuffer = new GraphicsBuffer(GraphicsBuffer.Target.Structured, levelCounts, System.Runtime.InteropServices.Marshal.SizeOf<LevelOffsets>());
indirectDispatchArgsBuffer = new GraphicsBuffer(GraphicsBuffer.Target.IndirectArguments, ShaderDefs.ArgsBufferStride * levelCounts, sizeof(uint));//3 arguments for upp dispatch, 3 arguments for lower dispatch
}
void LoadFromShaderGraph(RenderGraphResources shaderGraphResources)
{
alignedElementCount = shaderGraphResources.alignedElementCount;
maxBufferCount = shaderGraphResources.maxBufferCount;
maxLevelCount = shaderGraphResources.maxLevelCount;
prefixBuffer0 = (GraphicsBuffer)shaderGraphResources.prefixBuffer0;
prefixBuffer1 = (GraphicsBuffer)shaderGraphResources.prefixBuffer1;
totalLevelCountBuffer = (GraphicsBuffer)shaderGraphResources.totalLevelCountBuffer;
levelOffsetBuffer = (GraphicsBuffer)shaderGraphResources.levelOffsetBuffer;
indirectDispatchArgsBuffer = (GraphicsBuffer)shaderGraphResources.indirectDispatchArgsBuffer;
}
/// <summary>
/// Dispose the supporting resources.
/// </summary>
public void Dispose()
{
if (alignedElementCount == 0 || !ownsResources)
return;
alignedElementCount = 0;
void TryFreeBuffer(GraphicsBuffer resource)
{
if (resource != null)
{
resource.Dispose();
resource = null;
}
}
TryFreeBuffer(prefixBuffer0);
TryFreeBuffer(prefixBuffer1);
TryFreeBuffer(levelOffsetBuffer);
TryFreeBuffer(indirectDispatchArgsBuffer);
TryFreeBuffer(totalLevelCountBuffer);
}
}
/// <summary>
/// Arguments for a direct prefix sum.
/// </summary>
public struct DirectArgs
{
/// <summary>An inclusive or exclusive prefix sum.</summary>
public bool exclusive;
/// <summary>The size of the input list.</summary>
public int inputCount;
/// <summary>The input list.</summary>
public GraphicsBuffer input;
/// <summary>Required runtime resources.</summary>
public SupportResources supportResources;
}
/// <summary>
/// Arguments for an indirect prefix sum.
/// </summary>
public struct IndirectDirectArgs
{
/// <summary>An inclusive or exclusive prefix sum.</summary>
public bool exclusive;
/// <summary>Byte offset of the count inside the input count buffer.</summary>
public int inputCountBufferByteOffset;
/// <summary>GPU buffer defining the size of the input list.</summary>
public ComputeBuffer inputCountBuffer;
/// <summary>The input list.</summary>
public GraphicsBuffer input;
/// <summary>Required runtime resources.</summary>
public SupportResources supportResources;
}
/// <summary>
/// Structure defining any required assets used by the GPU sort.
/// </summary>
public struct SystemResources
{
/// <summary>
/// The compute asset that defines all of the kernels for the GPU prefix sum.
/// </summary>
public ComputeShader computeAsset;
internal int kernelCalculateLevelDispatchArgsFromConst;
internal int kernelCalculateLevelDispatchArgsFromBuffer;
internal int kernelPrefixSumOnGroup;
internal int kernelPrefixSumOnGroupExclusive;
internal int kernelPrefixSumNextInput;
internal int kernelPrefixSumResolveParent;
internal int kernelPrefixSumResolveParentExclusive;
internal void LoadKernels()
{
if (computeAsset == null)
return;
kernelCalculateLevelDispatchArgsFromConst = computeAsset.FindKernel("MainCalculateLevelDispatchArgsFromConst");
kernelCalculateLevelDispatchArgsFromBuffer = computeAsset.FindKernel("MainCalculateLevelDispatchArgsFromBuffer");
kernelPrefixSumOnGroup = computeAsset.FindKernel("MainPrefixSumOnGroup");
kernelPrefixSumOnGroupExclusive = computeAsset.FindKernel("MainPrefixSumOnGroupExclusive");
kernelPrefixSumNextInput = computeAsset.FindKernel("MainPrefixSumNextInput");
kernelPrefixSumResolveParent = computeAsset.FindKernel("MainPrefixSumResolveParent");
kernelPrefixSumResolveParentExclusive = computeAsset.FindKernel("MainPrefixSumResolveParentExclusive");
}
}
}
}
@@ -0,0 +1,40 @@
//
// This file was automatically generated. Please don't edit by hand. Execute Editor command [ Edit > Rendering > Generate Shader Includes ] instead
//
#ifndef GPUPREFIXSUM_DATA_CS_HLSL
#define GPUPREFIXSUM_DATA_CS_HLSL
//
// UnityEngine.Rendering.GPUPrefixSum+ShaderDefs: static fields
//
#define GROUP_SIZE (128)
#define ARGS_BUFFER_STRIDE (16)
#define ARGS_BUFFER_UPPER (0)
#define ARGS_BUFFER_LOWER (8)
// Generated from UnityEngine.Rendering.GPUPrefixSum+LevelOffsets
// PackingRules = Exact
struct LevelOffsets
{
uint count;
uint offset;
uint parentOffset;
};
//
// Accessors for UnityEngine.Rendering.GPUPrefixSum+LevelOffsets
//
uint GetCount(LevelOffsets value)
{
return value.count;
}
uint GetOffset(LevelOffsets value)
{
return value.offset;
}
uint GetParentOffset(LevelOffsets value)
{
return value.parentOffset;
}
#endif
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 56dcfb5655d6cc74cb661e531de4bbdf
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b287d5adc6e94882ac5a76f78323f37c
timeCreated: 1663981745
@@ -0,0 +1,18 @@
namespace UnityEngine.Rendering
{
public partial struct GPUPrefixSum
{
private static class ShaderIDs
{
public static readonly int _InputBuffer = Shader.PropertyToID("_InputBuffer");
public static readonly int _OutputBuffer = Shader.PropertyToID("_OutputBuffer");
public static readonly int _InputCountBuffer = Shader.PropertyToID("_InputCountBuffer");
public static readonly int _TotalLevelsBuffer = Shader.PropertyToID("_TotalLevelsBuffer");
public static readonly int _OutputTotalLevelsBuffer = Shader.PropertyToID("_OutputTotalLevelsBuffer");
public static readonly int _OutputDispatchLevelArgsBuffer = Shader.PropertyToID("_OutputDispatchLevelArgsBuffer");
public static readonly int _LevelsOffsetsBuffer = Shader.PropertyToID("_LevelsOffsetsBuffer");
public static readonly int _OutputLevelsOffsetsBuffer = Shader.PropertyToID("_OutputLevelsOffsetsBuffer");
public static readonly int _PrefixSumIntArgs = Shader.PropertyToID("_PrefixSumIntArgs");
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 851342c498014903a38e985d3dc2c005
timeCreated: 1663981675
@@ -0,0 +1,162 @@
using System;
namespace UnityEngine.Rendering
{
/// <summary>
/// Utility class for computing inclusive or exclusive prefix sums, directly or indirectly dispatched on the GPU.
/// </summary>
public partial struct GPUPrefixSum
{
private SystemResources resources;
/// <summary>
/// Initializes a re-usable GPU prefix sum instance.
/// </summary>
/// <param name="resources">The required system resources.</param>
public GPUPrefixSum(SystemResources resources)
{
this.resources = resources;
this.resources.LoadKernels();
}
Vector4 PackPrefixSumArgs(int a, int b, int c, int d)
{
unsafe
{
return new Vector4(
*((float*)&a),
*((float*)&b),
*((float*)&c),
*((float*)&d));
}
}
internal void ExecuteCommonIndirect(CommandBuffer cmdBuffer, GraphicsBuffer inputBuffer, in SupportResources supportResources, bool isExclusive)
{
int sumOnGroupKernel = isExclusive ? resources.kernelPrefixSumOnGroupExclusive : resources.kernelPrefixSumOnGroup;
int sumResolveParentKernel = isExclusive ? resources.kernelPrefixSumResolveParentExclusive : resources.kernelPrefixSumResolveParent;
//hierarchy up
for (int levelId = 0; levelId < supportResources.maxLevelCount; ++levelId)
{
var packedArgs = PackPrefixSumArgs(0, 0, 0, levelId);
cmdBuffer.SetComputeVectorParam(resources.computeAsset, ShaderIDs._PrefixSumIntArgs, packedArgs);
if (levelId == 0)
cmdBuffer.SetComputeBufferParam(resources.computeAsset, sumOnGroupKernel, ShaderIDs._InputBuffer, inputBuffer);
else
cmdBuffer.SetComputeBufferParam(resources.computeAsset, sumOnGroupKernel, ShaderIDs._InputBuffer, supportResources.prefixBuffer1);
cmdBuffer.SetComputeBufferParam(resources.computeAsset, sumOnGroupKernel, ShaderIDs._TotalLevelsBuffer, supportResources.totalLevelCountBuffer);
cmdBuffer.SetComputeBufferParam(resources.computeAsset, sumOnGroupKernel, ShaderIDs._LevelsOffsetsBuffer, supportResources.levelOffsetBuffer);
cmdBuffer.SetComputeBufferParam(resources.computeAsset, sumOnGroupKernel, ShaderIDs._OutputBuffer, supportResources.prefixBuffer0);
cmdBuffer.DispatchCompute(resources.computeAsset, sumOnGroupKernel, supportResources.indirectDispatchArgsBuffer, (uint)(levelId * ShaderDefs.ArgsBufferStride * 4));
if (levelId == supportResources.maxLevelCount - 1)
continue;
cmdBuffer.SetComputeBufferParam(resources.computeAsset, resources.kernelPrefixSumNextInput, ShaderIDs._InputBuffer, supportResources.prefixBuffer0);
cmdBuffer.SetComputeBufferParam(resources.computeAsset, resources.kernelPrefixSumNextInput, ShaderIDs._LevelsOffsetsBuffer, supportResources.levelOffsetBuffer);
cmdBuffer.SetComputeBufferParam(resources.computeAsset, resources.kernelPrefixSumNextInput, ShaderIDs._OutputBuffer, supportResources.prefixBuffer1);
cmdBuffer.DispatchCompute(resources.computeAsset, resources.kernelPrefixSumNextInput, supportResources.indirectDispatchArgsBuffer, (uint)((levelId + 1) * ShaderDefs.ArgsBufferStride * 4));
}
//down the hierarchy
for (int levelId = supportResources.maxLevelCount - 1; levelId >= 1; --levelId)
{
var packedArgs = PackPrefixSumArgs(0, 0, 0, levelId);
cmdBuffer.SetComputeVectorParam(resources.computeAsset, ShaderIDs._PrefixSumIntArgs, packedArgs);
cmdBuffer.SetComputeBufferParam(resources.computeAsset, sumResolveParentKernel, ShaderIDs._InputBuffer, inputBuffer);
cmdBuffer.SetComputeBufferParam(resources.computeAsset, sumResolveParentKernel, ShaderIDs._OutputBuffer, supportResources.prefixBuffer0);
cmdBuffer.SetComputeBufferParam(resources.computeAsset, sumResolveParentKernel, ShaderIDs._LevelsOffsetsBuffer, supportResources.levelOffsetBuffer);
cmdBuffer.DispatchCompute(resources.computeAsset, sumResolveParentKernel, supportResources.indirectDispatchArgsBuffer, (uint)(((levelId - 1) * ShaderDefs.ArgsBufferStride + ShaderDefs.ArgsBufferLower) * 4));
}
}
/// <summary>
/// Prefix sum a list of data from a CPU-defined count.
/// </summary>
/// <param name="cmdBuffer">Command Buffer for recording the prefix sum commands.</param>
/// <param name="arguments">Runtime arguments for the prefix sum.</param>
/// <exception cref="Exception">When the input data is invalid.</exception>
public void DispatchDirect(IComputeCommandBuffer cmdBuffer, in DirectArgs arguments)
{
if (cmdBuffer is BaseCommandBuffer baseBuffer)
DispatchDirect(baseBuffer.m_WrappedCommandBuffer, arguments);
#if UNITY_EDITOR
else
throw new ArgumentException("Command buffer must inherit from BaseCommandBuffer.");
#endif
}
/// <summary>
/// Prefix sum a list of data from a CPU-defined count.
/// </summary>
/// <param name="cmdBuffer">Command Buffer for recording the prefix sum commands.</param>
/// <param name="arguments">Runtime arguments for the prefix sum.</param>
/// <exception cref="Exception">When the input data is invalid.</exception>
public void DispatchDirect(CommandBuffer cmdBuffer, in DirectArgs arguments)
{
if (arguments.supportResources.prefixBuffer0 == null || arguments.supportResources.prefixBuffer1 == null)
throw new Exception("Support resources are not valid.");
if (arguments.input == null)
throw new Exception("Input source buffer cannot be null.");
if (arguments.inputCount > arguments.supportResources.alignedElementCount)
throw new Exception("Input count exceeds maximum count of support resources. Ensure to create support resources with enough space.");
//Generate level offsets first, from const value.
var packedArgs = PackPrefixSumArgs(arguments.inputCount, arguments.supportResources.maxLevelCount, 0, 0);
cmdBuffer.SetComputeVectorParam(resources.computeAsset, ShaderIDs._PrefixSumIntArgs, packedArgs);
cmdBuffer.SetComputeBufferParam(resources.computeAsset, resources.kernelCalculateLevelDispatchArgsFromConst, ShaderIDs._OutputLevelsOffsetsBuffer, arguments.supportResources.levelOffsetBuffer);
cmdBuffer.SetComputeBufferParam(resources.computeAsset, resources.kernelCalculateLevelDispatchArgsFromConst, ShaderIDs._OutputDispatchLevelArgsBuffer, arguments.supportResources.indirectDispatchArgsBuffer);
cmdBuffer.SetComputeBufferParam(resources.computeAsset, resources.kernelCalculateLevelDispatchArgsFromConst, ShaderIDs._OutputTotalLevelsBuffer, arguments.supportResources.totalLevelCountBuffer);
cmdBuffer.DispatchCompute(resources.computeAsset, resources.kernelCalculateLevelDispatchArgsFromConst, 1, 1, 1);
ExecuteCommonIndirect(cmdBuffer, arguments.input, arguments.supportResources, arguments.exclusive);
}
/// <summary>
/// Prefix sum a list of data from a GPU-defined count.
/// </summary>
/// <param name="cmdBuffer">Command Buffer for recording the prefix sum commands.</param>
/// <param name="arguments">Runtime arguments for the prefix sum.</param>
/// <exception cref="Exception">When the input data is invalid.</exception>
public void DispatchIndirect(IComputeCommandBuffer cmdBuffer, in IndirectDirectArgs arguments)
{
if (cmdBuffer is BaseCommandBuffer baseBuffer)
DispatchIndirect(baseBuffer.m_WrappedCommandBuffer, arguments);
#if UNITY_EDITOR
else
throw new ArgumentException("Command buffer must inherit from BaseCommandBuffer.");
#endif
}
/// <summary>
/// Prefix sum a list of data from a GPU-defined count.
/// </summary>
/// <param name="cmdBuffer">Command Buffer for recording the prefix sum commands.</param>
/// <param name="arguments">Runtime arguments for the prefix sum.</param>
/// <exception cref="Exception">When the input data is invalid.</exception>
public void DispatchIndirect(CommandBuffer cmdBuffer, in IndirectDirectArgs arguments)
{
if (arguments.supportResources.prefixBuffer0 == null || arguments.supportResources.prefixBuffer1 == null)
throw new Exception("Support resources are not valid.");
if (arguments.input == null || arguments.inputCountBuffer == null)
throw new Exception("Input source buffer and inputCountBuffer cannot be null.");
//Generate level offsets first, from const value.
var packedArgs = PackPrefixSumArgs(0, arguments.supportResources.maxLevelCount, arguments.inputCountBufferByteOffset, 0);
cmdBuffer.SetComputeVectorParam(resources.computeAsset, ShaderIDs._PrefixSumIntArgs, packedArgs);
cmdBuffer.SetComputeBufferParam(resources.computeAsset, resources.kernelCalculateLevelDispatchArgsFromBuffer, ShaderIDs._InputCountBuffer, arguments.inputCountBuffer);
cmdBuffer.SetComputeBufferParam(resources.computeAsset, resources.kernelCalculateLevelDispatchArgsFromBuffer, ShaderIDs._OutputLevelsOffsetsBuffer, arguments.supportResources.levelOffsetBuffer);
cmdBuffer.SetComputeBufferParam(resources.computeAsset, resources.kernelCalculateLevelDispatchArgsFromBuffer, ShaderIDs._OutputDispatchLevelArgsBuffer, arguments.supportResources.indirectDispatchArgsBuffer);
cmdBuffer.SetComputeBufferParam(resources.computeAsset, resources.kernelCalculateLevelDispatchArgsFromBuffer, ShaderIDs._OutputTotalLevelsBuffer, arguments.supportResources.totalLevelCountBuffer);
cmdBuffer.DispatchCompute(resources.computeAsset, resources.kernelCalculateLevelDispatchArgsFromBuffer, 1, 1, 1);
ExecuteCommonIndirect(cmdBuffer, arguments.input, arguments.supportResources, arguments.exclusive);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 47f5c7dc134c478aafbb3d86421da54f
timeCreated: 1663981351
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e5d689573b624639aac8d00ca42bd729
timeCreated: 1663986309
@@ -0,0 +1,135 @@
using System;
using UnityEngine.Rendering.RenderGraphModule;
namespace UnityEngine.Rendering
{
public partial struct GPUSort
{
/// <summary>
/// Data structure containing runtime dispatch parameters for the sort.
/// </summary>
public struct Args
{
/// <summary>Count</summary>
public uint count;
/// <summary>Defines the maximum height of the bitonic sort. By default, should be the same as count for a full sort.</summary>
public uint maxDepth;
/// <summary>Input Keys</summary>
public GraphicsBuffer inputKeys;
/// <summary>Input Values</summary>
public GraphicsBuffer inputValues;
/// <summary>Required runtime resources.</summary>
public SupportResources resources;
internal int workGroupCount;
}
/// <summary>
/// Utility for adapting to render graph usage.
/// </summary>
public struct RenderGraphResources
{
/// <summary>Render Graph Buffer Handle to the sorted keys.</summary>
public BufferHandle sortBufferKeys;
/// <summary>Render Graph Buffer Handle to the sorted values.</summary>
public BufferHandle sortBufferValues;
/// <summary>
/// Creates the render graph buffer resources from an input count.
/// </summary>
/// <param name="count">The number of (key, value) elements.</param>
/// <param name="renderGraph">Render Graph</param>
/// <param name="builder">Render Graph Builder</param>
/// <returns>An initialized RenderGraphResources object containing the created sort buffers.</returns>
[Obsolete("This Create signature is deprecated and will be removed in the future. Please use Create(IBaseRenderGraphBuilder) instead. #from(6000.3)")]
public static RenderGraphResources Create(int count, RenderGraph renderGraph, RenderGraphBuilder builder)
{
var targets = GraphicsBuffer.Target.Raw | GraphicsBuffer.Target.CopyDestination;
var resources = new RenderGraphResources
{
sortBufferKeys = builder.CreateTransientBuffer(new BufferDesc(count, 4, targets) { name = "Keys" }),
sortBufferValues = builder.CreateTransientBuffer(new BufferDesc(count, 4, targets) { name = "Values" })
};
return resources;
}
/// <summary>
/// Creates the render graph buffer resources from an input count.
/// </summary>
/// <param name="count">The number of (key, value) elements.</param>
/// <param name="renderGraph">Render Graph</param>
/// <param name="builder">Render Graph Builder</param>
/// <returns>An initialized RenderGraphResources object containing the created sort buffers.</returns>
public static RenderGraphResources Create(int count, RenderGraph renderGraph, IBaseRenderGraphBuilder builder)
{
var targets = GraphicsBuffer.Target.Raw | GraphicsBuffer.Target.CopyDestination;
var resources = new RenderGraphResources
{
sortBufferKeys = builder.CreateTransientBuffer(new BufferDesc(count, 4, targets) { name = "Keys" }),
sortBufferValues = builder.CreateTransientBuffer(new BufferDesc(count, 4, targets) { name = "Values" })
};
return resources;
}
}
/// <summary>
/// Data structure containing the runtime resources that are bound by the command buffer.
/// </summary>
public struct SupportResources
{
/// <summary>Sorted key buffer.</summary>
public GraphicsBuffer sortBufferKeys;
/// <summary>Sorted values buffer.</summary>
public GraphicsBuffer sortBufferValues;
/// <summary>
/// Load supporting resources from Render Graph Resources.
/// </summary>
/// <param name="renderGraphResources">Render Graph Resources</param>
/// <returns>A new instance of SupportResources with key and value buffers loaded from the given RenderGraphResources.</returns>
public static SupportResources Load(RenderGraphResources renderGraphResources)
{
var resources = new SupportResources
{
sortBufferKeys = renderGraphResources.sortBufferKeys,
sortBufferValues = renderGraphResources.sortBufferValues
};
return resources;
}
/// <summary>
/// Dispose the supporting resources.
/// </summary>
public void Dispose()
{
if (sortBufferKeys != null)
{
sortBufferKeys.Dispose();
sortBufferKeys = null;
}
if (sortBufferValues != null)
{
sortBufferValues.Dispose();
sortBufferValues = null;
}
}
}
/// <summary>
/// Structure defining any required assets used by the GPU sort.
/// </summary>
public struct SystemResources
{
/// <summary>
/// The compute asset that defines all of the kernels for the GPU sorting.
/// </summary>
public ComputeShader computeAsset;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6ae7551dd2f145dabc729526be5a6a2f
timeCreated: 1663986426
@@ -0,0 +1,167 @@
using System;
using Unity.Profiling.LowLevel;
using UnityEngine.Assertions;
// Ref: https://poniesandlight.co.uk/reflect/bitonic_merge_sort/
namespace UnityEngine.Rendering
{
/// <summary>
/// Utility class for sorting (key, value) pairs on the GPU.
/// </summary>
public partial struct GPUSort
{
private const uint kWorkGroupSize = 1024;
private LocalKeyword[] m_Keywords;
enum Stage
{
LocalBMS,
LocalDisperse,
BigFlip,
BigDisperse
}
/// <summary>
/// Dispatches the initial bitonic merge sort within each workgroup (up to 2048 elements
/// in each group). Called once per sort. Subsequent passes use the Big/Local Flip and
/// Disperse stages.
/// </summary>
static readonly ProfilingSampler k_LocalBMS = ProfilingSampler.Create("LocalBMS", MarkerFlags.Default);
/// <summary>
/// Dispatches the disperse step confined to a single workgroup once the sub-sequence
/// length falls within the 2048-element workgroup boundary. Terminates the inner
/// per-level loop early, avoiding the costlier cross-workgroup BigDisperse dispatch.
/// </summary>
static readonly ProfilingSampler k_LocalDisperse = ProfilingSampler.Create("LocalDisperse", MarkerFlags.Default);
/// <summary>
/// Dispatches the cross-workgroup flip step that mirrors and compares elements across
/// the full sort sequence. Called once per doubling of the merge height. Each invocation
/// count grows as O(log n).
/// </summary>
static readonly ProfilingSampler k_BigFlip = ProfilingSampler.Create("BigFlip", MarkerFlags.Default);
/// <summary>
/// Dispatches the cross-workgroup disperse step that propagates comparisons across
/// workgroup boundaries. Called for each inner-loop level where the sub-sequence
/// length exceeds 2048. Each invocation count grows as O(log² n).
/// </summary>
static readonly ProfilingSampler k_BigDisperse = ProfilingSampler.Create("BigDisperse", MarkerFlags.Default);
static readonly ProfilingSampler[] k_StageMarkers = { k_LocalBMS, k_LocalDisperse, k_BigFlip, k_BigDisperse };
private SystemResources resources;
/// <summary>
/// Initializes a re-usable GPU sorting instance.
/// </summary>
/// <param name="resources">The required system resources.</param>
public GPUSort(SystemResources resources)
{
this.resources = resources;
m_Keywords = new LocalKeyword[4]
{
new(resources.computeAsset, "STAGE_BMS"),
new(resources.computeAsset, "STAGE_LOCAL_DISPERSE"),
new(resources.computeAsset, "STAGE_BIG_FLIP"),
new(resources.computeAsset, "STAGE_BIG_DISPERSE")
};
}
void DispatchStage(CommandBuffer cmd, Args args, uint h, Stage stage)
{
Assert.IsTrue(args.workGroupCount != -1);
Assert.IsNotNull(resources.computeAsset);
// When the is no geometry, instead of computing the distance field, we clear it with a big value.
using (new ProfilingScope(cmd, k_StageMarkers[(int)stage], resources.computeAsset))
{
#if false
m_SortCS.enabledKeywords = new[] { keywords[(int)stage] };
#else
// Unfortunately need to configure the keywords like this. Might be worth just having a kernel per stage.
foreach (var k in m_Keywords)
cmd.SetKeyword(resources.computeAsset, k, false);
cmd.SetKeyword(resources.computeAsset, m_Keywords[(int) stage], true);
#endif
cmd.SetComputeIntParam(resources.computeAsset, "_H", (int) h);
cmd.SetComputeIntParam(resources.computeAsset, "_Total", (int) args.count);
cmd.SetComputeBufferParam(resources.computeAsset, 0, "_KeyBuffer", args.resources.sortBufferKeys);
cmd.SetComputeBufferParam(resources.computeAsset, 0, "_ValueBuffer", args.resources.sortBufferValues);
cmd.DispatchCompute(resources.computeAsset, 0, args.workGroupCount, 1, 1);
}
}
void CopyBuffer(CommandBuffer cmd, GraphicsBuffer src, GraphicsBuffer dst)
{
//disable all keywords for copy
foreach (var k in m_Keywords)
cmd.SetKeyword(resources.computeAsset, k, false);
int entriesToCopy = src.count * src.stride / 4;
cmd.SetComputeBufferParam(resources.computeAsset, 1, "_CopySrcBuffer", src);
cmd.SetComputeBufferParam(resources.computeAsset, 1, "_CopyDstBuffer", dst);
cmd.SetComputeIntParam(resources.computeAsset, "_CopyEntriesCount", entriesToCopy);
cmd.DispatchCompute(resources.computeAsset, 1, (entriesToCopy + 63) / 64, 1, 1);
}
internal static int DivRoundUp(int x, int y) => (x + y - 1) / y;
/// <summary>
/// Sorts a list of (key, value) pairs.
/// </summary>
/// <param name="cmd">Command buffer for recording the sorting commands.</param>
/// <param name="args">Runtime arguments for the sorting.</param>
public void Dispatch(IComputeCommandBuffer cmd, Args args)
{
if (cmd is BaseCommandBuffer baseCmd)
Dispatch(baseCmd.m_WrappedCommandBuffer, args);
#if UNITY_EDITOR
else
throw new ArgumentException("Command buffer must inherit from BaseCommandBuffer.");
#endif
}
/// <summary>
/// Sorts a list of (key, value) pairs.
/// </summary>
/// <param name="cmd">Command buffer for recording the sorting commands.</param>
/// <param name="args">Runtime arguments for the sorting.</param>
public void Dispatch(CommandBuffer cmd, Args args)
{
var n = args.count;
CopyBuffer(cmd, args.inputKeys, args.resources.sortBufferKeys);
CopyBuffer(cmd, args.inputValues, args.resources.sortBufferValues);
args.workGroupCount = Math.Max(1, DivRoundUp((int) n, (int) kWorkGroupSize * 2));
uint h = Math.Min(kWorkGroupSize * 2, args.maxDepth);
DispatchStage(cmd, args, h, Stage.LocalBMS);
h *= 2;
for (; h <= Math.Min(n, args.maxDepth); h *= 2)
{
DispatchStage(cmd, args, h, Stage.BigFlip);
for (uint hh = h / 2; hh > 1; hh /= 2)
{
if (hh <= kWorkGroupSize * 2)
{
DispatchStage(cmd, args, hh, Stage.LocalDisperse);
break;
}
DispatchStage(cmd, args, hh, Stage.BigDisperse);
}
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4c368ba38709458cb76d6105db148c79
timeCreated: 1663986326
@@ -0,0 +1,258 @@
using System;
namespace UnityEngine.Rendering
{
/// <summary>
/// Utility class for outputting to an HDR display.
/// </summary>
public static class HDROutputUtils
{
/// <summary> HDR color operations that the shader applies. </summary>
[Flags]
public enum Operation
{
/// <summary> Do not perform operations specific to HDR output. </summary>
None = 0,
/// <summary> Convert colors to the color space of the HDR display. </summary>
ColorConversion = 1 << 0,
/// <summary> Encode colors with the transfer function corresponding to the HDR display. </summary>
ColorEncoding = 1 << 1
}
/// <summary>
/// This struct Provides access to HDR display settings and information.
/// </summary>
public struct HDRDisplayInformation
{
/// <summary>
/// Constructs HDR Display settings.
/// </summary>
/// <param name="maxFullFrameToneMapLuminance">Maximum input luminance at which gradation is preserved even when the entire screen is bright.</param>
/// <param name="maxToneMapLuminance">Maximum input luminance at which gradation is preserved when 10% of the screen is bright.</param>
/// <param name="minToneMapLuminance">Minimum input luminance at which gradation is identifiable.</param>
/// <param name="hdrPaperWhiteNits">The base luminance of a white paper surface in nits or candela per square meter.</param>
public HDRDisplayInformation(int maxFullFrameToneMapLuminance, int maxToneMapLuminance, int minToneMapLuminance, float hdrPaperWhiteNits)
{
this.maxFullFrameToneMapLuminance = maxFullFrameToneMapLuminance;
this.maxToneMapLuminance = maxToneMapLuminance;
this.minToneMapLuminance = minToneMapLuminance;
this.paperWhiteNits = hdrPaperWhiteNits;
}
/// <summary>Maximum input luminance at which gradation is preserved even when the entire screen is bright. </summary>
public int maxFullFrameToneMapLuminance;
/// <summary>Maximum input luminance at which gradation is preserved when 10% of the screen is bright. </summary>
public int maxToneMapLuminance;
/// <summary>Minimum input luminance at which gradation is identifiable. </summary>
public int minToneMapLuminance;
/// <summary>The base luminance of a white paper surface in nits or candela per square meter. </summary>
public float paperWhiteNits;
}
/// <summary>Shader keywords for communicating with the HDR Output shader implementation.</summary>
public static class ShaderKeywords
{
/// <summary>Keyword string for converting to the correct output color space. </summary>
public const string HDR_COLORSPACE_CONVERSION = "HDR_COLORSPACE_CONVERSION";
/// <summary>Keyword string for applying the color encoding. </summary>
public const string HDR_ENCODING = "HDR_ENCODING";
/// <summary>Keyword string for converting to the correct output color space and applying the color encoding. </summary>
public const string HDR_COLORSPACE_CONVERSION_AND_ENCODING = "HDR_COLORSPACE_CONVERSION_AND_ENCODING";
/// <summary>Keyword string to enable when a shader must be aware the input color space is in nits HDR range. </summary>
public const string HDR_INPUT = "HDR_INPUT";
/// <summary>Keyword for converting to the correct output color space. </summary>
internal static readonly ShaderKeyword HDRColorSpaceConversion = new ShaderKeyword(HDR_COLORSPACE_CONVERSION);
/// <summary>Keyword for applying the color encoding. </summary>
internal static readonly ShaderKeyword HDREncoding = new ShaderKeyword(HDR_ENCODING);
/// <summary>Keyword for converting to the correct output color space and applying the color encoding. </summary>
internal static readonly ShaderKeyword HDRColorSpaceConversionAndEncoding = new ShaderKeyword(HDR_COLORSPACE_CONVERSION_AND_ENCODING);
/// <summary>Keyword to enable when a shader must be aware the input color space is in nits HDR range. </summary>
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");
}
/// <summary>
/// Extracts the color space part of the ColorGamut
/// </summary>
/// <param name="gamut">Color gamut (a combination of color space and encoding) queried from the device.</param>
/// <param name="colorspace">The HDRColorspace value the color gamut contains as an int.</param>
/// <returns>Returns true if there was a valid HDRColorspace for the ColorGamut, false otherwise</returns>
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;
}
}
/// <summary>
/// Extracts the encoding part of the ColorGamut
/// </summary>
/// <param name="gamut">Color gamut (a combination of color space and encoding) queried from the device.</param>
/// <param name="encoding">The HDREncoding value the color gamut contains as an int.</param>
/// <returns>Returns true if there was a valid HDREncoding for the ColorGamut, false otherwise</returns>
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;
}
}
/// <summary>
/// Configures the Material keywords to use HDR output parameters.
/// </summary>
/// <param name="material">The Material used with HDR output.</param>
/// <param name="gamut">Color gamut (a combination of color space and encoding) queried from the device.</param>
/// <param name="operations">HDR color operations the shader applies.</param>
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);
}
/// <summary>
/// Configures the Material Property Block variables to use HDR output parameters.
/// </summary>
/// <param name="properties">The Material Property Block used with HDR output.</param>
/// <param name="gamut">Color gamut (a combination of color space and encoding) queried from the device.</param>
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);
}
/// <summary>
/// Configures the Material keywords to use HDR output parameters.
/// </summary>
/// <param name="material">The Material used with HDR output.</param>
/// <param name="operations">HDR color operations the shader applies.</param>
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);
}
/// <summary>
/// Configures the compute shader keywords to use HDR output parameters.
/// </summary>
/// <param name="computeShader">The compute shader used with HDR output.</param>
/// <param name="gamut">Color gamut (a combination of color space and encoding) queried from the device.</param>
/// <param name="operations">HDR color operations the shader applies.</param>
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);
}
/// <summary>
/// Returns true if the given set of keywords is valid for HDR output.
/// </summary>
/// <param name="shaderKeywordSet">Shader keywords combination that represents a shader variant.</param>
/// <param name="isHDREnabled">Whether HDR output shader variants are required.</param>
/// <returns>True if the shader variant is valid and should not be stripped.</returns>
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;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8cd8322e74feda6438d39b753348b807
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,386 @@
using static UnityEngine.Mathf;
namespace UnityEngine.Rendering
{
/// <summary>
/// An implementation of Hable's artist-friendly tonemapping curve.
/// http://filmicworlds.com/blog/filmic-tonemapping-with-piecewise-power-curves/
/// </summary>
public class HableCurve
{
/// <summary>
/// Individual curve segment.
/// </summary>
public class Segment
{
/// <summary>
/// The offset of the segment on the X axis.
/// </summary>
public float offsetX;
/// <summary>
/// The offset of the segment on the Y axis.
/// </summary>
public float offsetY;
/// <summary>
/// The scale of the segment on the X axis.
/// </summary>
public float scaleX;
/// <summary>
/// The scale of the segment on the Y axis.
/// </summary>
public float scaleY;
/// <summary>
/// <c>ln(A)</c> constant in the power curve <c>y = e^(ln(A) + B*ln(x))</c>.
/// </summary>
public float lnA;
/// <summary>
/// <c>B</c> constant in the power curve <c>y = e^(ln(A) + B*ln(x))</c>.
/// </summary>
public float B;
/// <summary>
/// Evaluate a point on the curve.
/// </summary>
/// <param name="x">The point to evaluate.</param>
/// <returns>The value of the curve, at the point specified.</returns>
public float Eval(float x)
{
float x0 = (x - offsetX) * scaleX;
float y0 = 0f;
// log(0) is undefined but our function should evaluate to 0. There are better ways
// to handle this, but it's doing it the slow way here for clarity.
if (x0 > 0)
y0 = Exp(lnA + B * Log(x0));
return y0 * scaleY + offsetY;
}
}
struct DirectParams
{
internal float x0;
internal float y0;
internal float x1;
internal float y1;
internal float W;
internal float overshootX;
internal float overshootY;
internal float gamma;
}
/// <summary>
/// The white point.
/// </summary>
public float whitePoint { get; private set; }
/// <summary>
/// The inverse of the white point.
/// </summary>
/// <seealso cref="whitePoint"/>
public float inverseWhitePoint { get; private set; }
/// <summary>
/// The start of the linear section (middle segment of the curve).
/// </summary>
public float x0 { get; private set; }
/// <summary>
/// The end of the linear section (middle segment of the curve).
/// </summary>
public float x1 { get; private set; }
/// <summary>
/// The three segments of the curve.
/// </summary>
public readonly Segment[] segments = new Segment[3];
/// <summary>
/// Creates a new curve.
/// </summary>
public HableCurve()
{
for (int i = 0; i < 3; i++)
segments[i] = new Segment();
uniforms = new Uniforms(this);
}
/// <summary>
/// Evaluates a point on the curve.
/// </summary>
/// <param name="x">The x-coordinate at which to evaluate the curve.</param>
/// <returns>The y-coordinate (value) of the curve at the specified x-coordinate.</returns>
public float Eval(float x)
{
float normX = x * inverseWhitePoint;
int index = (normX < x0) ? 0 : ((normX < x1) ? 1 : 2);
var segment = segments[index];
float ret = segment.Eval(normX);
return ret;
}
/// <summary>
/// Initializes the curve.
/// </summary>
/// <param name="toeStrength">The strength of the transition between the curve's toe and the curve's mid-section. A value of 0 results in no transition and a value of 1 results in a very hard transition.</param>
/// <param name="toeLength">The length of the curve's toe. Higher values result in longer toes and therefore contain more of the dynamic range.</param>
/// <param name="shoulderStrength">The strength of the transition between the curve's midsection and the curve's shoulder. A value of 0 results in no transition and a value of 1 results in a very hard transition.</param>
/// <param name="shoulderLength">The amount of f-stops to add to the dynamic range of the curve. This is how much of the highlights that the curve takes into account.</param>
/// <param name="shoulderAngle">How much overshoot to add to the curve's shoulder.</param>
/// <param name="gamma">A gamma correction to the entire curve.</param>
public void Init(float toeStrength, float toeLength, float shoulderStrength, float shoulderLength, float shoulderAngle, float gamma)
{
var dstParams = new DirectParams();
// This is not actually the display gamma. It's just a UI space to avoid having to
// enter small numbers for the input.
const float kPerceptualGamma = 2.2f;
// Constraints
{
toeLength = Pow(Clamp01(toeLength), kPerceptualGamma);
toeStrength = Clamp01(toeStrength);
shoulderAngle = Clamp01(shoulderAngle);
shoulderStrength = Clamp(shoulderStrength, 1e-5f, 1f - 1e-5f);
shoulderLength = Max(0f, shoulderLength);
gamma = Max(1e-5f, gamma);
}
// Apply base params
{
// Toe goes from 0 to 0.5
float x0 = toeLength * 0.5f;
float y0 = (1f - toeStrength) * x0; // Lerp from 0 to x0
float remainingY = 1f - y0;
float initialW = x0 + remainingY;
float y1_offset = (1f - shoulderStrength) * remainingY;
float x1 = x0 + y1_offset;
float y1 = y0 + y1_offset;
// Filmic shoulder strength is in F stops
float extraW = Pow(2f, shoulderLength) - 1f;
float W = initialW + extraW;
dstParams.x0 = x0;
dstParams.y0 = y0;
dstParams.x1 = x1;
dstParams.y1 = y1;
dstParams.W = W;
// Bake the linear to gamma space conversion
dstParams.gamma = gamma;
}
dstParams.overshootX = (dstParams.W * 2f) * shoulderAngle * shoulderLength;
dstParams.overshootY = 0.5f * shoulderAngle * shoulderLength;
InitSegments(dstParams);
}
void InitSegments(DirectParams srcParams)
{
var paramsCopy = srcParams;
whitePoint = srcParams.W;
inverseWhitePoint = 1f / srcParams.W;
// normalize params to 1.0 range
paramsCopy.W = 1f;
paramsCopy.x0 /= srcParams.W;
paramsCopy.x1 /= srcParams.W;
paramsCopy.overshootX = srcParams.overshootX / srcParams.W;
float toeM = 0f;
float shoulderM = 0f;
{
float m, b;
AsSlopeIntercept(out m, out b, paramsCopy.x0, paramsCopy.x1, paramsCopy.y0, paramsCopy.y1);
float g = srcParams.gamma;
// Base function of linear section plus gamma is
// y = (mx+b)^g
//
// which we can rewrite as
// y = exp(g*ln(m) + g*ln(x+b/m))
//
// and our evaluation function is (skipping the if parts):
/*
float x0 = (x - offsetX) * scaleX;
y0 = exp(m_lnA + m_B*log(x0));
return y0*scaleY + m_offsetY;
*/
var midSegment = segments[1];
midSegment.offsetX = -(b / m);
midSegment.offsetY = 0f;
midSegment.scaleX = 1f;
midSegment.scaleY = 1f;
midSegment.lnA = g * Log(m);
midSegment.B = g;
toeM = EvalDerivativeLinearGamma(m, b, g, paramsCopy.x0);
shoulderM = EvalDerivativeLinearGamma(m, b, g, paramsCopy.x1);
// apply gamma to endpoints
paramsCopy.y0 = Max(1e-5f, Pow(paramsCopy.y0, paramsCopy.gamma));
paramsCopy.y1 = Max(1e-5f, Pow(paramsCopy.y1, paramsCopy.gamma));
paramsCopy.overshootY = Pow(1f + paramsCopy.overshootY, paramsCopy.gamma) - 1f;
}
this.x0 = paramsCopy.x0;
this.x1 = paramsCopy.x1;
// Toe section
{
var toeSegment = segments[0];
toeSegment.offsetX = 0;
toeSegment.offsetY = 0f;
toeSegment.scaleX = 1f;
toeSegment.scaleY = 1f;
float lnA, B;
SolveAB(out lnA, out B, paramsCopy.x0, paramsCopy.y0, toeM);
toeSegment.lnA = lnA;
toeSegment.B = B;
}
// Shoulder section
{
// Use the simple version that is usually too flat
var shoulderSegment = segments[2];
float x0 = (1f + paramsCopy.overshootX) - paramsCopy.x1;
float y0 = (1f + paramsCopy.overshootY) - paramsCopy.y1;
float lnA, B;
SolveAB(out lnA, out B, x0, y0, shoulderM);
shoulderSegment.offsetX = (1f + paramsCopy.overshootX);
shoulderSegment.offsetY = (1f + paramsCopy.overshootY);
shoulderSegment.scaleX = -1f;
shoulderSegment.scaleY = -1f;
shoulderSegment.lnA = lnA;
shoulderSegment.B = B;
}
// Normalize so that we hit 1.0 at our white point. We wouldn't have do this if we
// skipped the overshoot part.
{
// Evaluate shoulder at the end of the curve
float scale = segments[2].Eval(1f);
float invScale = 1f / scale;
segments[0].offsetY *= invScale;
segments[0].scaleY *= invScale;
segments[1].offsetY *= invScale;
segments[1].scaleY *= invScale;
segments[2].offsetY *= invScale;
segments[2].scaleY *= invScale;
}
}
// Find a function of the form:
// f(x) = e^(lnA + Bln(x))
// where
// f(0) = 0; not really a constraint
// f(x0) = y0
// f'(x0) = m
void SolveAB(out float lnA, out float B, float x0, float y0, float m)
{
B = (m * x0) / y0;
lnA = Log(y0) - B * Log(x0);
}
// Convert to y=mx+b
void AsSlopeIntercept(out float m, out float b, float x0, float x1, float y0, float y1)
{
float dy = (y1 - y0);
float dx = (x1 - x0);
if (dx == 0)
m = 1f;
else
m = dy / dx;
b = y0 - x0 * m;
}
// f(x) = (mx+b)^g
// f'(x) = gm(mx+b)^(g-1)
float EvalDerivativeLinearGamma(float m, float b, float g, float x)
{
return g * m * Pow(m * x + b, g - 1f);
}
/// <summary>
/// An utility class to ease the binding of curve parameters to shaders.
/// </summary>
public class Uniforms
{
HableCurve parent;
internal Uniforms(HableCurve parent)
{
this.parent = parent;
}
/// <summary>
/// Main curve settings, stored as <c>(inverseWhitePoint, x0, x1, 0)</c>.
/// </summary>
public Vector4 curve => new Vector4(parent.inverseWhitePoint, parent.x0, parent.x1, 0f);
/// <summary>
/// Toe segment settings, stored as <c>(offsetX, offsetY, scaleX, scaleY)</c>.
/// </summary>
public Vector4 toeSegmentA => new Vector4(parent.segments[0].offsetX, parent.segments[0].offsetY, parent.segments[0].scaleX, parent.segments[0].scaleY);
/// <summary>
/// Toe segment settings, stored as <c>(ln1, B, 0, 0)</c>.
/// </summary>
public Vector4 toeSegmentB => new Vector4(parent.segments[0].lnA, parent.segments[0].B, 0f, 0f);
/// <summary>
/// Mid segment settings, stored as <c>(offsetX, offsetY, scaleX, scaleY)</c>.
/// </summary>
public Vector4 midSegmentA => new Vector4(parent.segments[1].offsetX, parent.segments[1].offsetY, parent.segments[1].scaleX, parent.segments[1].scaleY);
/// <summary>
/// Mid segment settings, stored as <c>(ln1, B, 0, 0)</c>.
/// </summary>
public Vector4 midSegmentB => new Vector4(parent.segments[1].lnA, parent.segments[1].B, 0f, 0f);
/// <summary>
/// Shoulder segment settings, stored as <c>(offsetX, offsetY, scaleX, scaleY)</c>.
/// </summary>
public Vector4 shoSegmentA => new Vector4(parent.segments[2].offsetX, parent.segments[2].offsetY, parent.segments[2].scaleX, parent.segments[2].scaleY);
/// <summary>
/// Shoulder segment settings, stored as <c>(ln1, B, 0, 0)</c>.
/// </summary>
public Vector4 shoSegmentB => new Vector4(parent.segments[2].lnA, parent.segments[2].B, 0f, 0f);
}
/// <summary>
/// An instance of the <see cref="Uniforms"/> utility class for this curve.
/// </summary>
public readonly Uniforms uniforms;
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4d3b530d53faf114e9c17ced1f0bbbf1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,31 @@
namespace UnityEngine.Rendering
{
/// <summary>
/// An utility class to compute samples on the Halton sequence.
/// https://en.wikipedia.org/wiki/Halton_sequence
/// </summary>
public static class HaltonSequence
{
/// <summary>
/// Gets a deterministic sample in the Halton sequence.
/// </summary>
/// <param name="index">The index in the sequence.</param>
/// <param name="radix">The radix of the sequence.</param>
/// <returns>A sample from the Halton sequence.</returns>
public static float Get(int index, int radix)
{
float result = 0f;
float fraction = 1f / radix;
while (index > 0)
{
result += (index % radix) * fraction;
index /= radix;
fraction /= radix;
}
return result;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4c45974a160f56c4c8d8ed0373c53ef5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,179 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Unity.Collections;
namespace UnityEngine.Rendering
{
/// <summary>
/// Incremental 32-bit FNV-1a hash builder.
/// Use this for change detection or other non-cryptographic hashing where
/// equally-shaped inputs should produce well-distributed hashes.
/// </summary>
/// <remarks>
/// As a <c>ref struct</c>, instances cannot be stored in fields, boxed, or
/// captured by lambdas/async methods; create one, append inputs, then read
/// <see cref="value"/>.
/// </remarks>
internal ref struct HashFNV1A32
{
/// <summary>
/// FNV prime.
/// </summary>
const uint k_Prime = 16777619;
/// <summary>
/// FNV offset basis.
/// </summary>
const uint k_OffsetBasis = 2166136261;
uint m_Hash;
/// <summary>
/// Creates a new hash builder seeded with the FNV offset basis.
/// </summary>
/// <returns>An empty <see cref="HashFNV1A32"/> ready to receive <see cref="Append(in int)"/> calls.</returns>
public static HashFNV1A32 Create()
{
return new HashFNV1A32 { m_Hash = k_OffsetBasis };
}
/// <summary>Mixes an <see cref="int"/> into the running hash.</summary>
/// <param name="input">Value to fold in.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(in int input)
{
unchecked
{
m_Hash = (m_Hash ^ (uint)input) * k_Prime;
}
}
/// <summary>Mixes a <see cref="uint"/> into the running hash.</summary>
/// <param name="input">Value to fold in.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(in uint input)
{
unchecked
{
m_Hash = (m_Hash ^ input) * k_Prime;
}
}
/// <summary>Mixes a <see cref="bool"/> into the running hash.</summary>
/// <param name="input">Value to fold in.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(in bool input)
{
m_Hash = (m_Hash ^ (input ? 1u : 0u)) * k_Prime;
}
/// <summary>Mixes a <see cref="float"/> into the running hash.</summary>
/// <param name="input">Value to fold in.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(in float input)
{
unchecked
{
m_Hash = (m_Hash ^ (uint)input.GetHashCode()) * k_Prime;
}
}
/// <summary>Mixes a <see cref="double"/> into the running hash.</summary>
/// <param name="input">Value to fold in.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(in double input)
{
unchecked
{
m_Hash = (m_Hash ^ (uint)input.GetHashCode()) * k_Prime;
}
}
/// <summary>Mixes a <see cref="Vector2"/> into the running hash.</summary>
/// <param name="input">Value to fold in.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(in Vector2 input)
{
Append(input.x);
Append(input.y);
}
/// <summary>Mixes a <see cref="Vector3"/> into the running hash.</summary>
/// <param name="input">Value to fold in.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(in Vector3 input)
{
Append(input.x);
Append(input.y);
Append(input.z);
}
/// <summary>Mixes a <see cref="Vector4"/> into the running hash.</summary>
/// <param name="input">Value to fold in.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(in Vector4 input)
{
Append(input.x);
Append(input.y);
Append(input.z);
Append(input.w);
}
/// <summary>Mixes any value-type input into the running hash via its <see cref="object.GetHashCode"/>.</summary>
/// <typeparam name="T">Any value type.</typeparam>
/// <param name="input">Value to fold in.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append<T>(T input) where T : struct
{
unchecked
{
m_Hash = (m_Hash ^ (uint)input.GetHashCode()) * k_Prime;
}
}
/// <summary>The current hash value as a signed 32-bit integer.</summary>
public int value => (int)m_Hash;
/// <inheritdoc/>
public override int GetHashCode()
{
return value;
}
}
static class DelegateHashCodeUtils
{
//Cache to prevent CompilerGeneratedAttribute extraction for known delegate
static readonly Lazy<Dictionary<int, bool>> s_MethodHashCodeToSkipTargetHashMap = new(() => new Dictionary<int, bool>(64));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetFuncHashCode(Delegate del)
{
//Get MethodInfo hash code as the main one to be used
var methodHashCode = RuntimeHelpers.GetHashCode(del.Method);
//Check if we are dealing with lambda or static delegates and skip target if we are.
//Static methods have a null Target.
//Lambdas have a CompilerGeneratedAttribute as they are generated by a compiler.
//If Lambda have any captured variable Target hashcode will be different each time we re-create lambda.
if (!s_MethodHashCodeToSkipTargetHashMap.Value.TryGetValue(methodHashCode, out var skipTarget))
{
skipTarget = del.Target == null || (
del.Method.DeclaringType?.IsNestedPrivate == true &&
Attribute.IsDefined(del.Method.DeclaringType, typeof(CompilerGeneratedAttribute), false)
);
s_MethodHashCodeToSkipTargetHashMap.Value[methodHashCode] = skipTarget;
}
//Combine method info hashcode and target hashcode if needed
return skipTarget ? methodHashCode : methodHashCode ^ RuntimeHelpers.GetHashCode(del.Target);
}
//used for testing
internal static int GetTotalCacheCount() => s_MethodHashCodeToSkipTargetHashMap.Value.Count;
internal static void ClearCache() => s_MethodHashCodeToSkipTargetHashMap.Value.Clear();
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 06752bfeb5aa4120929229ade28a7cba
timeCreated: 1720533699
@@ -0,0 +1,526 @@
using System;
namespace UnityEngine.Rendering
{
/// <summary>
/// Light Unit Utils contains functions and definitions to facilitate conversion between different light intensity units.
/// </summary>
public static class LightUnitUtils
{
static float k_LuminanceToEvFactor => Mathf.Log(100f / ColorUtils.s_LightMeterCalibrationConstant, 2);
static float k_EvToLuminanceFactor => -k_LuminanceToEvFactor;
/// <summary>
/// The solid angle of a full sphere in steradians.
/// </summary>
public const float SphereSolidAngle = 4.0f * Mathf.PI;
/// <summary>
/// Get the unit that light intensity is measured in, for a specific light type.
/// </summary>
/// <param name="lightType">The type of light to get the native light unit for.</param>
/// <returns>The native unit of that light types intensity.</returns>
public static LightUnit GetNativeLightUnit(LightType lightType)
{
switch (lightType)
{
// Punctual lights
case LightType.Spot:
case LightType.Point:
case LightType.Pyramid:
return LightUnit.Candela;
// Directional lights
case LightType.Directional:
case LightType.Box:
return LightUnit.Lux;
// Area lights
case LightType.Rectangle:
case LightType.Disc:
case LightType.Tube:
return LightUnit.Nits;
default:
throw new ArgumentOutOfRangeException();
}
}
/// <summary>
/// Check if a light types intensity can be converted to/from a light unit.
/// </summary>
/// <param name="lightType">Light type to check.</param>
/// <param name="lightUnit">Unit to check.</param>
/// <returns>True if light unit is supported.</returns>
public static bool IsLightUnitSupported(LightType lightType, LightUnit lightUnit)
{
const int punctualUnits = 1 << (int)LightUnit.Lumen |
1 << (int)LightUnit.Candela |
1 << (int)LightUnit.Lux |
1 << (int)LightUnit.Ev100;
const int directionalUnits = 1 << (int)LightUnit.Lux;
const int areaUnits = 1 << (int)LightUnit.Lumen |
1 << (int)LightUnit.Nits |
1 << (int)LightUnit.Ev100;
int lightUnitFlag = 1 << (int)lightUnit;
switch (lightType)
{
// Punctual lights
case LightType.Point:
case LightType.Spot:
case LightType.Pyramid:
return (lightUnitFlag & punctualUnits) > 0;
// Directional lights
case LightType.Directional:
case LightType.Box:
return (lightUnitFlag & directionalUnits) > 0;
// Area lights
case LightType.Rectangle:
case LightType.Disc:
case LightType.Tube:
return (lightUnitFlag & areaUnits) > 0;
default:
return false;
}
}
/// <summary>
/// Get the solid angle of a Point light.
/// </summary>
/// <returns>4 * Pi steradians.</returns>
public static float GetSolidAngleFromPointLight()
{
return SphereSolidAngle;
}
/// <summary>
/// Get the solid angle of a Spot light.
/// </summary>
/// <param name="spotAngle">The spot angle in degrees.</param>
/// <returns>Solid angle in steradians.</returns>
public static float GetSolidAngleFromSpotLight(float spotAngle)
{
double angle = Math.PI * spotAngle / 180.0;
double solidAngle = 2.0 * Math.PI * (1.0 - Math.Cos(angle * 0.5));
return (float)solidAngle;
}
/// <summary>
/// Get the solid angle of a Pyramid light.
/// </summary>
/// <param name="spotAngle">The spot angle in degrees.</param>
/// <param name="aspectRatio">The aspect ratio of the pyramid.</param>
/// <returns>Solid angle in steradians.</returns>
public static float GetSolidAngleFromPyramidLight(float spotAngle, float aspectRatio)
{
if (aspectRatio < 1.0f)
{
aspectRatio = (float)(1.0 / aspectRatio);
}
double angleA = Math.PI * spotAngle / 180.0;
double length = Math.Tan(0.5 * angleA) * aspectRatio;
double angleB = Math.Atan(length) * 2.0;
double solidAngle = 4.0 * Math.Asin(Math.Sin(angleA * 0.5) * Math.Sin(angleB * 0.5));
return (float)solidAngle;
}
internal static float GetSolidAngle(LightType lightType, bool spotReflector, float spotAngle, float aspectRatio)
{
return lightType switch
{
LightType.Spot => spotReflector ? GetSolidAngleFromSpotLight(spotAngle) : SphereSolidAngle,
LightType.Pyramid => spotReflector ? GetSolidAngleFromPyramidLight(spotAngle, aspectRatio) : SphereSolidAngle,
LightType.Point => GetSolidAngleFromPointLight(),
_ => throw new ArgumentException("Solid angle is undefined for lights of type " + lightType)
};
}
/// <summary>
/// Get the projected surface area of a Rectangle light.
/// </summary>
/// <param name="rectSizeX">The width of the rectangle.</param>
/// <param name="rectSizeY">The height of the rectangle.</param>
/// <returns>Surface area.</returns>
public static float GetAreaFromRectangleLight(float rectSizeX, float rectSizeY)
{
return Mathf.Abs(rectSizeX * rectSizeY) * Mathf.PI;
}
/// <summary>
/// Get the projected surface area of a Rectangle light.
/// </summary>
/// <param name="rectSize">The size of the rectangle.</param>
/// <returns>Projected surface area.</returns>
public static float GetAreaFromRectangleLight(Vector2 rectSize)
{
return GetAreaFromRectangleLight(rectSize.x, rectSize.y);
}
/// <summary>
/// Get the projected surface area of a Disc light.
/// </summary>
/// <param name="discRadius">The radius of the disc.</param>
/// <returns>Projected surface area.</returns>
public static float GetAreaFromDiscLight(float discRadius)
{
return discRadius * discRadius * Mathf.PI * Mathf.PI;
}
/// <summary>
/// Get the projected surface area of a Tube light.
/// </summary>
/// <remarks>Note that Tube lights have no physical surface area.
/// Instead this method returns a value suitable for Nits&lt;=&gt;Lumen unit conversion.</remarks>
/// <param name="tubeLength">The length of the tube.</param>
/// <returns>4 * Pi * (tube length).</returns>
public static float GetAreaFromTubeLight(float tubeLength)
{
// Line lights expect radiance (W / (sr * m^2)) in the shader.
// In the UI, we specify luminous flux (power) in lumens.
// First, it needs to be converted to radiometric units (radian flux, W).
//
// Then we must recall how to compute power from radiance:
//
// radiance = differential_power / (differential_projected_area * differential_solid_angle),
// radiance = differential_power / (differential_area * differential_solid_angle * <N, L>),
// power = Integral{area, Integral{hemisphere, radiance * <N, L>}}.
//
// Unlike line lights, our line lights have no surface area, so the integral becomes:
//
// power = Integral{length, Integral{sphere, radiance}}.
//
// For an isotropic line light, radiance is constant, therefore:
//
// power = length * (4 * Pi) * radiance,
// radiance = power / (length * (4 * Pi)).
return Mathf.Abs(tubeLength) * 4.0f * Mathf.PI;
}
/// <summary>
/// Convert intensity in Lumen to Candela.
/// </summary>
/// <param name="lumen">Intensity in Lumen.</param>
/// <param name="solidAngle">Light solid angle in steradians.</param>
/// <returns>Intensity in Candela.</returns>
public static float LumenToCandela(float lumen, float solidAngle)
{
return lumen / solidAngle;
}
/// <summary>
/// Convert intensity in Candela to Lumen.
/// </summary>
/// <param name="candela">Intensity in Candela.</param>
/// <param name="solidAngle">Light solid angle in steradians.</param>
/// <returns>Intensity in Lumen.</returns>
public static float CandelaToLumen(float candela, float solidAngle)
{
return candela * solidAngle;
}
/// <summary>
/// Convert intensity in Lumen to Nits.
/// </summary>
/// <param name="lumen">Intensity in Lumen.</param>
/// <param name="area">Projected surface area of the light source.</param>
/// <returns>Intensity in Nits.</returns>
public static float LumenToNits(float lumen, float area)
{
return lumen / area;
}
/// <summary>
/// Convert intensity in Nits to Lumen.
/// </summary>
/// <param name="nits">Intensity in Nits.</param>
/// <param name="area">Projected surface area of the light source.</param>
/// <returns>Intensity in Lumen.</returns>
public static float NitsToLumen(float nits, float area)
{
return nits * area;
}
/// <summary>
/// Convert intensity in Lux to Candela.
/// </summary>
/// <param name="lux">Intensity in Lux.</param>
/// <param name="distance">Distance between light and surface.</param>
/// <returns>Intensity in Candela.</returns>
public static float LuxToCandela(float lux, float distance)
{
return lux * (distance * distance);
}
/// <summary>
/// Convert intensity in Candela to Lux.
/// </summary>
/// <param name="candela">Intensity in Lux.</param>
/// <param name="distance">Distance between light and surface.</param>
/// <returns>Intensity in Lux.</returns>
public static float CandelaToLux(float candela, float distance)
{
return candela / (distance * distance);
}
/// <summary>
/// Convert intensity in Ev100 to Nits.
/// </summary>
/// <param name="ev100">Intensity in Ev100.</param>
/// <returns>Intensity in Nits.</returns>
public static float Ev100ToNits(float ev100)
{
return Mathf.Pow(2.0f, ev100 + k_EvToLuminanceFactor);
}
/// <summary>
/// Convert intensity in Nits to Ev100.
/// </summary>
/// <param name="nits">Intensity in Nits.</param>
/// <returns>Intensity in Ev100.</returns>
public static float NitsToEv100(float nits)
{
return Mathf.Log(nits, 2) + k_LuminanceToEvFactor;
}
/// <summary>
/// Convert intensity in Ev100 to Candela.
/// </summary>
/// <param name="ev100">Intensity in Ev100.</param>
/// <returns>Intensity in Candela.</returns>
public static float Ev100ToCandela(float ev100)
{
return Ev100ToNits(ev100);
}
/// <summary>
/// Convert intensity in Candela to Ev100.
/// </summary>
/// <param name="candela">Intensity in Candela.</param>
/// <returns>Intensity in Ev100.</returns>
public static float CandelaToEv100(float candela)
{
return NitsToEv100(candela);
}
internal static float ConvertIntensityInternal(float intensity, LightUnit fromUnit, LightUnit toUnit,
LightType lightType, float area, float luxAtDistance, float solidAngle)
{
if (!IsLightUnitSupported(lightType, fromUnit) || !IsLightUnitSupported(lightType, toUnit))
{
throw new ArgumentException("Converting " + fromUnit + " to " + toUnit
+ " is undefined for lights of type " + lightType);
}
if (fromUnit == toUnit)
{
return intensity;
}
switch (fromUnit)
{
case LightUnit.Lumen:
{
switch (toUnit)
{
case LightUnit.Candela:
{
// Lumen => Candela:
return LumenToCandela(intensity, solidAngle);
}
case LightUnit.Lux:
{
// Lumen => Candela => Lux
float candela = LumenToCandela(intensity, solidAngle);
return CandelaToLux(candela, luxAtDistance);
}
case LightUnit.Nits:
{
// Lumen => Nits
return LumenToNits(intensity, area);
}
case LightUnit.Ev100:
{
// Lumen => Candela/Nits => Ev100
float candelaNits = lightType switch
{
LightType.Point or LightType.Spot or LightType.Pyramid =>
LumenToCandela(intensity, solidAngle),
LightType.Rectangle or LightType.Disc or LightType.Tube =>
LumenToNits(intensity, area),
_ =>
throw new ArgumentException("Converting from Lumen to Ev100 is undefined for light type "
+ lightType)
};
return NitsToEv100(candelaNits);
}
default:
throw new ArgumentOutOfRangeException(nameof(toUnit), toUnit, null);
}
}
case LightUnit.Candela:
{
switch (toUnit)
{
case LightUnit.Lumen:
{
// Candela => Lumen
return CandelaToLumen(intensity, solidAngle);
}
case LightUnit.Lux:
{
// Candela => Lux
return CandelaToLux(intensity, luxAtDistance);
}
case LightUnit.Ev100:
{
// Candela => Ev100
return NitsToEv100(intensity);
}
default:
throw new ArgumentOutOfRangeException(nameof(toUnit), toUnit, null);
}
}
case LightUnit.Lux:
{
switch (toUnit)
{
case LightUnit.Lumen:
{
// Lux => Candela => Lumen
float candela = LuxToCandela(intensity, luxAtDistance);
return CandelaToLumen(candela, solidAngle);
}
case LightUnit.Candela:
{
// Lux => Candela
return LuxToCandela(intensity, luxAtDistance);
}
case LightUnit.Ev100:
{
// Lux => Candela => Ev100
float candela = LuxToCandela(intensity, luxAtDistance);
return NitsToEv100(candela);
}
default:
throw new ArgumentOutOfRangeException(nameof(toUnit), toUnit, null);
}
}
case LightUnit.Nits:
{
switch (toUnit)
{
case LightUnit.Lumen:
{
return NitsToLumen(intensity, area);
}
case LightUnit.Ev100:
{
return NitsToEv100(intensity);
}
default:
throw new ArgumentOutOfRangeException(nameof(toUnit), toUnit, null);
}
}
case LightUnit.Ev100:
{
switch (toUnit)
{
case LightUnit.Lumen:
{
// Ev100 => Candela/Nits => Lumen
float candelaOrNits = Ev100ToNits(intensity);
return lightType switch
{
LightType.Point or LightType.Spot or LightType.Pyramid =>
CandelaToLumen(candelaOrNits, solidAngle),
LightType.Rectangle or LightType.Disc or LightType.Tube =>
NitsToLumen(candelaOrNits, area),
_ =>
throw new ArgumentException("Converting from Lumen to Ev100 is undefined for light type "
+ lightType)
};
}
case LightUnit.Nits:
case LightUnit.Candela:
{
// Ev100 => Candela/Nits
return Ev100ToNits(intensity);
}
case LightUnit.Lux:
{
// Ev100 => Candela => Lux
float candela = Ev100ToNits(intensity);
return CandelaToLux(candela, luxAtDistance);
}
default:
throw new ArgumentOutOfRangeException(nameof(toUnit), toUnit, null);
}
}
default:
throw new ArgumentOutOfRangeException(nameof(fromUnit), fromUnit, null);
}
}
/// <summary>
/// Convert intensity from one unit to another using the parameters of a given Light.
/// </summary>
/// <param name="light">Light to use parameters from.</param>
/// <param name="intensity">Intensity to be converted.</param>
/// <param name="fromUnit">Unit to convert from.</param>
/// <param name="toUnit">Unit to convert to.</param>
/// <returns>Converted intensity.</returns>
public static float ConvertIntensity(Light light, float intensity, LightUnit fromUnit, LightUnit toUnit)
{
LightType lightType = light.type;
float area = lightType switch
{
LightType.Rectangle => GetAreaFromRectangleLight(light.areaSize),
LightType.Disc => GetAreaFromDiscLight(light.areaSize.x), // Disc radius is stored in areaSize.x
LightType.Tube => GetAreaFromTubeLight(light.areaSize.x), // Tube length is stored in areaSize.x
_ => 0.0f
};
float luxAtDistance = light.luxAtDistance;
float solidAngle = lightType switch
{
LightType.Spot or LightType.Pyramid or LightType.Point => GetSolidAngle(lightType, light.enableSpotReflector,
light.spotAngle, light.areaSize.x), // Pyramid aspect ratio is store in areaSize.x
_ => 0.0f
};
return ConvertIntensityInternal(intensity, fromUnit, toUnit, lightType, area, luxAtDistance, solidAngle);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4ae71d47e308c48719d317d0874b23d1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,167 @@
using System;
using UnityEngine.Scripting.APIUpdating;
namespace UnityEngine.Rendering
{
/// <summary>
/// Material quality flags.
/// </summary>
[Flags]
[MovedFrom("Utilities")]
public enum MaterialQuality
{
/// <summary>Low Material Quality.</summary>
Low = 1 << 0,
/// <summary>Medium Material Quality.</summary>
Medium = 1 << 1,
/// <summary>High Material Quality.</summary>
High = 1 << 2
}
/// <summary>
/// Material Quality utility class.
/// </summary>
[MovedFrom("Utilities")]
public static class MaterialQualityUtilities
{
/// <summary>
/// Keywords strings for Material Quality levels.
/// </summary>
public static readonly string[] KeywordNames =
{
"MATERIAL_QUALITY_LOW",
"MATERIAL_QUALITY_MEDIUM",
"MATERIAL_QUALITY_HIGH",
};
/// <summary>
/// String representation of the MaterialQuality enum.
/// </summary>
public static readonly string[] EnumNames = Enum.GetNames(typeof(MaterialQuality));
/// <summary>
/// Keywords for Material Quality levels.
/// </summary>
public static readonly ShaderKeyword[] Keywords =
{
new ShaderKeyword(KeywordNames[0]),
new ShaderKeyword(KeywordNames[1]),
new ShaderKeyword(KeywordNames[2]),
};
/// <summary>
/// Returns the highest available quality level in a MaterialQuality bitfield.
/// </summary>
/// <param name="levels">Input MaterialQuality bitfield.</param>
/// <returns>The highest available quality level.</returns>
public static MaterialQuality GetHighestQuality(this MaterialQuality levels)
{
for (var i = Keywords.Length - 1; i >= 0; --i)
{
var level = (MaterialQuality)(1 << i);
if ((levels & level) != 0)
return level;
}
return 0;
}
/// <summary>
/// Returns the closest available quality level in a MaterialQuality bitfield.
/// </summary>
/// <param name="availableLevels">Available MaterialQuality bitfield.</param>
/// <param name="requestedLevel">Input MaterialQuality level.</param>
/// <returns>The closest available quality level.</returns>
public static MaterialQuality GetClosestQuality(this MaterialQuality availableLevels, MaterialQuality requestedLevel)
{
// Special fallback when there are no available quality levels. Needs to match in the shader stripping code
if (availableLevels == 0)
return MaterialQuality.Low;
// First we want to find the closest available quality level below the requested one.
int requestedLevelIndex = ToFirstIndex(requestedLevel);
MaterialQuality chosenQuality = (MaterialQuality)0;
for (int i = requestedLevelIndex; i >= 0; --i)
{
var level = FromIndex(i);
if ((level & availableLevels) != 0)
{
chosenQuality = level;
break;
}
}
if (chosenQuality != 0)
return chosenQuality;
// If none is found then we fallback to the closest above.
for (var i = requestedLevelIndex + 1; i < Keywords.Length; ++i)
{
var level = FromIndex(i);
var diff = Math.Abs(requestedLevel - level);
if ((level & availableLevels) != 0)
{
chosenQuality = level;
break;
}
}
Debug.Assert(chosenQuality != 0);
return chosenQuality;
}
/// <summary>
/// Set the global keyword for the provided MaterialQuality.
/// </summary>
/// <param name="level">MaterialQuality level to set the keyword for.</param>
public static void SetGlobalShaderKeywords(this MaterialQuality level)
{
for (var i = 0; i < KeywordNames.Length; ++i)
{
if ((level & (MaterialQuality)(1 << i)) != 0)
Shader.EnableKeyword(KeywordNames[i]);
else
Shader.DisableKeyword(KeywordNames[i]);
}
}
/// <summary>
/// Set the global keyword for the provided MaterialQuality.
/// </summary>
/// <param name="level">MaterialQuality level to set the keyword for.</param>
/// <param name="cmd">Command Buffer used to setup the keyword.</param>
public static void SetGlobalShaderKeywords(this MaterialQuality level, CommandBuffer cmd)
{
for (var i = 0; i < KeywordNames.Length; ++i)
{
if ((level & (MaterialQuality)(1 << i)) != 0)
cmd.EnableShaderKeyword(KeywordNames[i]);
else
cmd.DisableShaderKeyword(KeywordNames[i]);
}
}
/// <summary>
/// Returns the index (in the MaterialQuality enum) of the first available level.
/// </summary>
/// <param name="level">MaterialQuality bitfield.</param>
/// <returns>The index of the first available level.</returns>
public static int ToFirstIndex(this MaterialQuality level)
{
for (var i = 0; i < KeywordNames.Length; ++i)
{
if ((level & (MaterialQuality)(1 << i)) != 0)
return i;
}
return -1;
}
/// <summary>
/// Returns the enum equivalent of the index in the MaterialQuality enum list.
/// </summary>
/// <param name="index">Index of the material quality.</param>
/// <returns>The equivalent enum.</returns>
public static MaterialQuality FromIndex(int index) => (MaterialQuality)(1 << index);
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 14221b1c07bee064e8078151055512fc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,105 @@
using UnityEngine;
using System.Collections.Generic;
using System.Reflection;
using System;
namespace UnityEngine.Rendering
{
/// <summary>
/// Helper to build and render a mesh for Gizmos, it is a lot more faster than drawing a ton of gizmos separately
/// </summary>
class MeshGizmo : IDisposable
{
public static readonly int vertexCountPerCube = 24;
public Mesh mesh;
List<Vector3> vertices;
List<int> indices;
List<Color> colors;
Material wireMaterial;
Material dottedWireMaterial;
Material solidMaterial;
public MeshGizmo(int capacity = 0)
{
vertices = new List<Vector3>(capacity);
indices = new List<int>(capacity);
colors = new List<Color>(capacity);
mesh = new Mesh { indexFormat = IndexFormat.UInt32, hideFlags = HideFlags.HideAndDontSave };
#if UNITY_EDITOR
wireMaterial = (Material)UnityEditor.EditorGUIUtility.LoadRequired("SceneView/HandleLines.mat");
dottedWireMaterial = (Material)UnityEditor.EditorGUIUtility.LoadRequired("SceneView/HandleDottedLines.mat");
solidMaterial = UnityEditor.HandleUtility.handleMaterial;
#endif
}
public void Clear()
{
vertices.Clear();
indices.Clear();
colors.Clear();
}
public void AddWireCube(Vector3 center, Vector3 size, Color color)
{
var halfSize = size / 2.0f;
Vector3 p0 = new Vector3(halfSize.x, halfSize.y, halfSize.z);
Vector3 p1 = new Vector3(-halfSize.x, halfSize.y, halfSize.z);
Vector3 p2 = new Vector3(-halfSize.x, -halfSize.y, halfSize.z);
Vector3 p3 = new Vector3(halfSize.x, -halfSize.y, halfSize.z);
Vector3 p4 = new Vector3(halfSize.x, halfSize.y, -halfSize.z);
Vector3 p5 = new Vector3(-halfSize.x, halfSize.y, -halfSize.z);
Vector3 p6 = new Vector3(-halfSize.x, -halfSize.y, -halfSize.z);
Vector3 p7 = new Vector3(halfSize.x, -halfSize.y, -halfSize.z);
AddEdge(center + p0, center + p1);
AddEdge(center + p1, center + p2);
AddEdge(center + p2, center + p3);
AddEdge(center + p3, center + p0);
AddEdge(center + p4, center + p5);
AddEdge(center + p5, center + p6);
AddEdge(center + p6, center + p7);
AddEdge(center + p7, center + p4);
AddEdge(center + p0, center + p4);
AddEdge(center + p1, center + p5);
AddEdge(center + p2, center + p6);
AddEdge(center + p3, center + p7);
void AddEdge(Vector3 p1, Vector3 p2)
{
vertices.Add(p1);
vertices.Add(p2);
indices.Add(indices.Count);
indices.Add(indices.Count);
colors.Add(color);
colors.Add(color);
}
}
void DrawMesh(Matrix4x4 trs, Material mat, MeshTopology topology, CompareFunction depthTest, string gizmoName)
{
mesh.Clear();
mesh.SetVertices(vertices);
mesh.SetColors(colors);
mesh.SetIndices(indices, topology, 0);
mat.SetFloat("_HandleZTest", (int)depthTest);
var cmd = CommandBufferPool.Get(gizmoName ?? "Mesh Gizmo Rendering");
cmd.DrawMesh(mesh, trs, mat, 0, 0);
Graphics.ExecuteCommandBuffer(cmd);
}
public void RenderWireframe(Matrix4x4 trs, CompareFunction depthTest = CompareFunction.LessEqual, string gizmoName = null)
=> DrawMesh(trs, wireMaterial, MeshTopology.Lines, depthTest, gizmoName);
public void Dispose()
{
CoreUtils.Destroy(mesh);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 28a28989c1f84bb4f9d812b78b97afcb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,353 @@
using System;
using System.IO;
using UnityEngine.Assertions;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditorInternal;
using System.Reflection;
#endif
namespace UnityEngine.Rendering
{
#if UNITY_EDITOR
/// <summary>
/// The resources that need to be reloaded in Editor can live in Runtime.
/// The reload call should only be done in Editor context though but it
/// could be called from runtime entities.
/// </summary>
/// <seealso cref="ReloadAttribute"/>
/// <seealso cref="ReloadGroupAttribute"/>
public static class ResourceReloader
{
/// <summary>
/// Looks for resources in the given <paramref name="container"/> object and reload the ones
/// that are missing or broken, using the <see cref="ReloadAttribute"/> to find the paths.
/// This version returns a null value without throwing an error if the issue is
/// AssetDatabase being not ready. But in this case the assetDatabaseNotReady result will be true.
/// </summary>
/// <param name="container">The object containing reload-able resources</param>
/// <param name="basePath">The base path for the package</param>
/// <returns>
/// - 1 hasChange: True if something have been reloaded.
/// - 2 assetDatabaseNotReady: True if the issue preventing loading is due to state of AssetDatabase
/// </returns>
public static (bool hasChange, bool assetDatabaseNotReady) TryReloadAllNullIn(System.Object container, string basePath)
{
try
{
return (ReloadAllNullIn(container, basePath), false);
}
catch (InvalidImportException)
{
return (false, true);
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// Looks for resources in the given <paramref name="container"/> object and reload the ones
/// that are missing or broken, using the <see cref="ReloadAttribute"/> to find the paths.
/// See <see cref="ReloadAttribute"/> for an example.
/// </summary>
/// <param name="container">The object containing reload-able resources</param>
/// <param name="basePath">The base path for the package</param>
/// <returns>True if something have been reloaded.</returns>
public static bool ReloadAllNullIn(System.Object container, string basePath)
{
if (IsNull(container))
return false;
var changed = false;
foreach (var fieldInfo in container.GetType()
.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static))
{
//Recurse on sub-containers
if (IsReloadGroup(fieldInfo))
{
changed |= FixGroupIfNeeded(container, fieldInfo);
changed |= ReloadAllNullIn(fieldInfo.GetValue(container), basePath);
}
//Find null field and reload them
var attribute = GetReloadAttribute(fieldInfo);
if (attribute != null)
{
if (attribute.paths.Length == 1)
{
changed |= SetAndLoadIfNull(container, fieldInfo, GetFullPath(basePath, attribute),
attribute.package);
}
else if (attribute.paths.Length > 1)
{
changed |= FixArrayIfNeeded(container, fieldInfo, attribute.paths.Length);
var array = (Array)fieldInfo.GetValue(container);
if (IsReloadGroup(array))
{
//Recurse on each sub-containers
for (int index = 0; index < attribute.paths.Length; ++index)
{
changed |= FixGroupIfNeeded(array, index);
changed |= ReloadAllNullIn(array.GetValue(index), basePath);
}
}
else
{
//Find each null element and reload them
for (int index = 0; index < attribute.paths.Length; ++index)
changed |= SetAndLoadIfNull(array, index, GetFullPath(basePath, attribute, index),
attribute.package);
}
}
}
}
if (changed && container is UnityEngine.Object c)
EditorUtility.SetDirty(c);
return changed;
}
static void CheckReloadGroupSupportedType(Type type)
{
if (type.IsSubclassOf(typeof(ScriptableObject)))
throw new Exception(@$"ReloadGroup attribute must not be used on {nameof(ScriptableObject)}.
If {nameof(ResourceReloader)} create an instance of it, it will be not saved as a file, resulting in corrupted ID when building.");
}
static bool FixGroupIfNeeded(System.Object container, FieldInfo info)
{
var type = info.FieldType;
CheckReloadGroupSupportedType(type);
if (IsNull(container, info))
{
var value = Activator.CreateInstance(type);
info.SetValue(
container,
value
);
return true;
}
return false;
}
static bool FixGroupIfNeeded(Array array, int index)
{
Assert.IsNotNull(array);
var type = array.GetType().GetElementType();
CheckReloadGroupSupportedType(type);
if (IsNull(array.GetValue(index)))
{
var value = type.IsSubclassOf(typeof(ScriptableObject))
? ScriptableObject.CreateInstance(type)
: Activator.CreateInstance(type);
array.SetValue(value, index);
return true;
}
return false;
}
static bool FixArrayIfNeeded(System.Object container, FieldInfo info, int length)
{
if (IsNull(container, info) || ((Array)info.GetValue(container)).Length < length)
{
info.SetValue(container, Activator.CreateInstance(info.FieldType, length));
return true;
}
return false;
}
static ReloadAttribute GetReloadAttribute(FieldInfo fieldInfo)
{
var attributes = (ReloadAttribute[])fieldInfo
.GetCustomAttributes(typeof(ReloadAttribute), false);
if (attributes.Length == 0)
return null;
return attributes[0];
}
static bool IsReloadGroup(FieldInfo info)
=> info.FieldType
.GetCustomAttributes(typeof(ReloadGroupAttribute), false).Length > 0;
static bool IsReloadGroup(Array field)
=> field.GetType().GetElementType()
.GetCustomAttributes(typeof(ReloadGroupAttribute), false).Length > 0;
static bool IsNull(System.Object container, FieldInfo info)
=> IsNull(info.GetValue(container));
static bool IsNull(System.Object field)
=> field == null || field.Equals(null);
static UnityEngine.Object Load(string path, Type type, ReloadAttribute.Package location)
{
// Check if asset exist.
// Direct loading can be prevented by AssetDatabase being reloading.
var guid = AssetDatabase.AssetPathToGUID(path);
if (location == ReloadAttribute.Package.Root && String.IsNullOrEmpty(guid))
throw new Exception($"Cannot load. Incorrect path: {path}");
// Else the path is good. Attempt loading resource if AssetDatabase available.
UnityEngine.Object result;
switch (location)
{
case ReloadAttribute.Package.Builtin:
if (type == typeof(Shader))
result = Shader.Find(path);
else
result = Resources.GetBuiltinResource(type, path); //handle wrong path error
break;
case ReloadAttribute.Package.BuiltinExtra:
if (type == typeof(Shader))
result = Shader.Find(path);
else
result = AssetDatabase.GetBuiltinExtraResource(type, path); //handle wrong path error
break;
case ReloadAttribute.Package.Root:
result = AssetDatabase.LoadAssetAtPath(path, type);
break;
default:
throw new NotImplementedException($"Unknown {location}");
}
if (IsNull(result))
{
throw new InvalidImportException($"Cannot load. Path {path} is correct but AssetDatabase cannot load now.");
}
return result;
}
static bool SetAndLoadIfNull(System.Object container, FieldInfo info,
string path, ReloadAttribute.Package location)
{
if (IsNull(container, info))
{
info.SetValue(container, Load(path, info.FieldType, location));
return true;
}
return false;
}
static bool SetAndLoadIfNull(Array array, int index, string path, ReloadAttribute.Package location)
{
var element = array.GetValue(index);
if (IsNull(element))
{
array.SetValue(Load(path, array.GetType().GetElementType(), location), index);
return true;
}
return false;
}
static string GetFullPath(string basePath, ReloadAttribute attribute, int index = 0)
{
string path;
switch (attribute.package)
{
case ReloadAttribute.Package.Builtin:
path = attribute.paths[index];
break;
case ReloadAttribute.Package.Root:
path = basePath + "/" + attribute.paths[index];
break;
default:
throw new ArgumentException("Unknown Package Path!");
}
return path;
}
// It's not perfect retrying right away but making it called in EditorApplication.delayCall
// from EnsureResources creates GC which we want to avoid
static void DelayedNullReload<T>(string resourcePath)
where T : RenderPipelineResources
{
T resourcesDelayed = AssetDatabase.LoadAssetAtPath<T>(resourcePath);
if (resourcesDelayed == null)
EditorApplication.delayCall += () => DelayedNullReload<T>(resourcePath);
else
ResourceReloader.ReloadAllNullIn(resourcesDelayed, resourcesDelayed.packagePath_Internal);
}
/// <summary>
/// Ensures that all resources in a container has been loaded
/// </summary>
/// <param name="forceReload">Set to true to force all resources to be reloaded even if they are loaded already</param>
/// <param name="resources">The resource container with the resulting loaded resources</param>
/// <param name="resourcePath">The asset path to load the resource container from</param>
/// <param name="checker">Function to test if the resource container is present in a RenderPipelineGlobalSettings</param>
/// <param name="settings">RenderPipelineGlobalSettings to be passed to checker to test of the resource container is already loaded</param>
public static void EnsureResources<T, S>(bool forceReload, ref T resources, string resourcePath, Func<S, bool> checker, S settings)
where T : RenderPipelineResources where S : RenderPipelineGlobalSettings
{
T resourceChecked = null;
if (checker(settings))
{
if (!EditorUtility.IsPersistent(resources)) // if not loaded from the Asset database
{
// try to load from AssetDatabase if it is ready
resourceChecked = AssetDatabase.LoadAssetAtPath<T>(resourcePath);
if (resourceChecked && !resourceChecked.Equals(null))
resources = resourceChecked;
}
if (forceReload)
ResourceReloader.ReloadAllNullIn(resources, resources.packagePath_Internal);
return;
}
resourceChecked = AssetDatabase.LoadAssetAtPath<T>(resourcePath);
if (resourceChecked != null && !resourceChecked.Equals(null))
{
resources = resourceChecked;
if (forceReload)
ResourceReloader.ReloadAllNullIn(resources, resources.packagePath_Internal);
}
else
{
// Asset database may not be ready
var objs = InternalEditorUtility.LoadSerializedFileAndForget(resourcePath);
resources = (objs != null && objs.Length > 0) ? objs[0] as T : null;
if (forceReload)
{
try
{
if (ResourceReloader.ReloadAllNullIn(resources, resources.packagePath_Internal))
{
InternalEditorUtility.SaveToSerializedFileAndForget(
new Object[] { resources },
resourcePath,
true);
}
}
catch (System.Exception e)
{
// This can be called at a time where AssetDatabase is not available for loading.
// When this happens, the GUID can be get but the resource loaded will be null.
// Using the ResourceReloader mechanism in CoreRP, it checks this and add InvalidImport data when this occurs.
if (!(e.Data.Contains("InvalidImport") && e.Data["InvalidImport"] is int dii && dii == 1))
Debug.LogException(e);
else
DelayedNullReload<T>(resourcePath);
}
}
}
Debug.Assert(checker(settings), $"Could not load {typeof(T).Name}.");
}
}
#endif
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 722e53e6319a6da4994e39d6e63bfe9e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,84 @@
#if UNITY_EDITOR
using UnityEditor;
#endif
using System;
using UnityEngine.Analytics;
namespace UnityEngine.Rendering
{
/// <summary>
/// Setup a specific render pipeline on scene loading.
/// This need to be used with caution as it will change project configuration.
/// </summary>
#if UNITY_EDITOR
[ExecuteAlways]
#endif
public class SceneRenderPipeline : MonoBehaviour
{
#if UNITY_EDITOR
[SerializeField] bool firstTimeCreated = true;
/// <summary>
/// Scriptable Render Pipeline Asset to setup on scene load.
/// </summary>
public RenderPipelineAsset renderPipelineAsset;
void Awake()
{
if (firstTimeCreated)
{
renderPipelineAsset = GraphicsSettings.defaultRenderPipeline;
firstTimeCreated = false;
}
//Send analytics each time to find usage in content dl on the asset store too
SceneRenderPipelineAnalytic.SendAnalytic(this);
}
void OnEnable()
{
GraphicsSettings.defaultRenderPipeline = renderPipelineAsset;
}
[AnalyticInfo(eventName: "sceneRenderPipelineAssignment", vendorKey: "unity.srp", maxEventsPerHour: 10, maxNumberOfElements: 1000)]
class SceneRenderPipelineAnalytic : IAnalytic
{
public SceneRenderPipelineAnalytic(string guid)
{
m_Data = new Data
{
scene_guid = guid
};
}
[System.Diagnostics.DebuggerDisplay("{scene_guid}")]
[Serializable]
internal struct Data : IAnalytic.IData
{
// Naming convention for analytics data
public string scene_guid;
};
public bool TryGatherData(out IAnalytic.IData data, out Exception error)
{
data = m_Data;
error = null;
return true;
}
static public void SendAnalytic(SceneRenderPipeline sender)
{
SceneRenderPipelineAnalytic analytic = new SceneRenderPipelineAnalytic(sender.gameObject.scene.GetGUID());
EditorAnalytics.SendAnalytic(analytic);
}
Data m_Data;
}
#endif
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 06c0317e0632e6c47bb855ee99f95544
timeCreated: 1504094117
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,173 @@
using System.Collections.Generic;
using Unity.Collections;
namespace UnityEngine.Rendering
{
/// <summary>
/// Utility class that computes the total rect area via a sweep-line.
/// </summary>
public static class SweepLineRectUtils
{
struct EventComparer : IComparer<Vector4>
{
public int Compare(Vector4 a, Vector4 b)
{
int cx = a.x.CompareTo(b.x);
if (cx != 0) return cx;
return b.y.CompareTo(a.y);
}
}
struct ActiveComparer : IComparer<Vector2>
{
public int Compare(Vector2 a, Vector2 b)
{
return a.x.CompareTo(b.x);
}
}
static Vector4[] s_EventsBuffer = new Vector4[32];
static Vector2[] s_ActiveBuffer = new Vector2[16];
/// <summary>
/// Computes the total covered area (union) of a set of axis-aligned rectangles, counting overlaps only once.
/// </summary>
/// <param name="rects">List of rects to compute.</param>
/// <returns>The normalized union area in [0,1], with overlaps counted once.</returns>
public static float CalculateRectUnionArea(List<Rect> rects)
{
int rectsCount = rects.Count;
int eventsCapacity = rectsCount * 2;
if (eventsCapacity > s_EventsBuffer.Length)
s_EventsBuffer = new Vector4[eventsCapacity];
if (rectsCount > s_ActiveBuffer.Length)
s_ActiveBuffer = new Vector2[rectsCount];
int eventCount = 0;
foreach (var rect in rects)
InsertEvents(rect, s_EventsBuffer, ref eventCount);
return CalculateRectUnionArea(s_EventsBuffer, s_ActiveBuffer, eventCount);
}
// Merge overlapping intervals and return total covered Y length
static float MergeLengthY(Vector2[] activeBuffer, int count)
{
if (count <= 0)
return 0f;
// ActiveBuffer stores (yMin, yMax)
float total = 0f;
float cy0 = activeBuffer[0].x;
float cy1 = activeBuffer[0].y;
for (int i = 1; i < count; i++)
{
float y0 = activeBuffer[i].x;
float y1 = activeBuffer[i].y;
if (y0 <= cy1)
{
if (y1 > cy1) cy1 = y1;
}
else
{
total += (cy1 - cy0);
cy0 = y0; cy1 = y1;
}
}
total += (cy1 - cy0);
return Mathf.Clamp01(total);
}
/// <summary>
/// Computes the total covered area (union) of a set of axis-aligned rectangles using a sweep-line.
/// </summary>
static unsafe float CalculateRectUnionArea(Vector4[] eventsBuffer, Vector2[] activeBuffer, int eventCount)
{
if (eventCount == 0)
return 0f;
// Sort events by (x asc, enter first)
fixed (Vector4* ptr = eventsBuffer)
{
NativeSortExtension.Sort(ptr, eventCount, new EventComparer());
}
int activeCount = 0;
float area = 0f;
float lastX = eventsBuffer[0].x;
bool needLastXUpdate = false;
int i = 0;
while (i < eventCount)
{
float x = eventsBuffer[i].x;
if (needLastXUpdate)
{
lastX = x;
needLastXUpdate = false;
}
// Accumulate area over [lastX, x)
float dx = x - lastX;
if (dx > 0f && activeCount > 0)
{
fixed (Vector2* ptr = activeBuffer)
{
NativeSortExtension.Sort(ptr, activeCount, new ActiveComparer());
}
area += MergeLengthY(activeBuffer, activeCount) * dx;
lastX = x;
}
// Consume all events at this x (group approx-equal x's)
do
{
Vector4 ev = eventsBuffer[i];
float y0 = ev.z;
float y1 = ev.w;
if (ev.y > 0f) // Enter
{
activeBuffer[activeCount++] = new Vector2(y0, y1);
}
else // Leave
{
// Remove once (swap with last)
for (int k = 0; k < activeCount; k++)
{
Vector2 v = activeBuffer[k];
if (Mathf.Approximately(v.x, y0) && Mathf.Approximately(v.y, y1))
{
int last = activeCount - 1;
activeBuffer[k] = activeBuffer[last];
activeCount = last;
break;
}
}
if (activeCount == 0)
needLastXUpdate = true;
}
i++;
}
while (i < eventCount && Mathf.Approximately(eventsBuffer[i].x, x));
}
return area;
}
// Insert events with clamped rects
static void InsertEvents(in Rect rect, Vector4[] eventsBuffer, ref int eventCount)
{
if (rect.width > 0f && rect.height > 0f)
{
var y0 = Mathf.Clamp01(rect.yMin);
var y1 = Mathf.Clamp01(rect.yMax);
eventsBuffer[eventCount++] = new Vector4(Mathf.Clamp01(rect.xMin), +1f, y0, y1); // enter
eventsBuffer[eventCount++] = new Vector4(Mathf.Clamp01(rect.xMax), -1f, y0, y1); // leave
}
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: ff1aad7bfc102a847b1979aebf787494
@@ -0,0 +1,267 @@
using System;
using System.Runtime.CompilerServices;
using UnityEngine.Experimental.Rendering;
namespace UnityEngine.Rendering
{
// Due to limitations in the builtin AnimationCurve we need this custom wrapper.
// Improvements:
// - Dirty state handling so we know when a curve has changed or not
// - Looping support (infinite curve)
// - Zero-value curve
// - Cheaper length property
/// <summary>
/// A wrapper around <c>AnimationCurve</c> to automatically bake it into a texture.
/// </summary>
[Serializable]
public class TextureCurve : IDisposable
{
const int k_Precision = 128; // Edit LutBuilder3D if you change this value
const float k_Step = 1f / k_Precision;
/// <summary>
/// The number of keys in the curve.
/// </summary>
[field: SerializeField]
public int length { get; private set; } // Calling AnimationCurve.length is very slow, let's cache it
[SerializeField]
bool m_Loop;
[SerializeField]
float m_ZeroValue;
[SerializeField]
float m_Range;
/// <summary>
/// Internal curve used to generate the Texture
/// </summary>
[SerializeField]
AnimationCurve m_Curve;
AnimationCurve m_LoopingCurve;
Texture2D m_Texture;
bool m_IsCurveDirty;
bool m_IsTextureDirty;
/// <summary>
/// Retrieves the key at index.
/// </summary>
/// <param name="index">The index to look for.</param>
/// <value>A key.</value>
public Keyframe this[int index] => m_Curve[index];
/// <summary>
/// Creates a new <see cref="TextureCurve"/> from an existing <c>AnimationCurve</c>.
/// </summary>
/// <param name="baseCurve">The source <c>AnimationCurve</c>.</param>
/// <param name="zeroValue">The default value to use when the curve doesn't have any key.</param>
/// <param name="loop">Should the curve automatically loop in the given <paramref name="bounds"/>?</param>
/// <param name="bounds">The boundaries of the curve.</param>
public TextureCurve(AnimationCurve baseCurve, float zeroValue, bool loop, in Vector2 bounds)
: this(baseCurve.keys, zeroValue, loop, bounds) { }
/// <summary>
/// Creates a new <see cref="TextureCurve"/> from an arbitrary number of keyframes.
/// </summary>
/// <param name="keys">An array of Keyframes used to define the curve.</param>
/// <param name="zeroValue">The default value to use when the curve doesn't have any key.</param>
/// <param name="loop">Should the curve automatically loop in the given <paramref name="bounds"/>?</param>
/// <param name="bounds">The boundaries of the curve.</param>
public TextureCurve(Keyframe[] keys, float zeroValue, bool loop, in Vector2 bounds)
{
m_Curve = new AnimationCurve(keys);
m_ZeroValue = zeroValue;
m_Loop = loop;
m_Range = bounds.magnitude;
length = keys.Length;
SetDirty();
}
/// <summary>
/// Cleans up the internal texture resource.
/// </summary>
public void Dispose()
{
Release();
}
/// <summary>
/// Releases the internal texture resource.
/// </summary>
public void Release()
{
if (m_Texture != null)
CoreUtils.Destroy(m_Texture);
m_Texture = null;
}
/// <summary>
/// Marks the curve as dirty to trigger a redraw of the texture the next time <see cref="GetTexture"/>
/// is called.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetDirty()
{
m_IsCurveDirty = true;
m_IsTextureDirty = true;
}
static GraphicsFormat GetTextureFormat()
{
// UUM-41070: We require `Sample | SetPixels` but with the deprecated FormatUsage this was checking `SetPixels`
// For now, we keep checking for `SetPixels` until the performance hit of doing the correct checks is evaluated
if (SystemInfo.IsFormatSupported(GraphicsFormat.R16_SFloat, GraphicsFormatUsage.SetPixels))
return GraphicsFormat.R16_SFloat;
if (SystemInfo.IsFormatSupported(GraphicsFormat.R8_UNorm, GraphicsFormatUsage.SetPixels))
return GraphicsFormat.R8_UNorm;
return GraphicsFormat.R8G8B8A8_UNorm;
}
/// <summary>
/// Gets the texture representation of this curve.
/// </summary>
/// <returns>A 128x1 texture.</returns>
public Texture2D GetTexture()
{
if (m_Texture == null)
{
m_Texture = new Texture2D(k_Precision, 1, GetTextureFormat(), TextureCreationFlags.None);
m_Texture.name = "CurveTexture";
m_Texture.hideFlags = HideFlags.HideAndDontSave;
m_Texture.filterMode = FilterMode.Bilinear;
m_Texture.wrapMode = TextureWrapMode.Clamp;
m_Texture.anisoLevel = 0;
m_IsTextureDirty = true;
}
if (m_IsTextureDirty)
{
var pixels = new Color[k_Precision];
for (int i = 0; i < pixels.Length; i++)
pixels[i].r = Evaluate(i * k_Step);
m_Texture.SetPixels(pixels);
m_Texture.Apply(false, false);
m_IsTextureDirty = false;
}
return m_Texture;
}
/// <summary>
/// Evaluate a time value on the curve.
/// </summary>
/// <param name="time">The time within the curve you want to evaluate.</param>
/// <returns>The value of the curve, at the point in time specified.</returns>
public float Evaluate(float time)
{
if (m_IsCurveDirty)
length = m_Curve.length;
if (length == 0)
return m_ZeroValue;
if (!m_Loop || length == 1)
return m_Curve.Evaluate(time);
if (m_IsCurveDirty)
{
if (m_LoopingCurve == null)
m_LoopingCurve = new AnimationCurve();
var prev = m_Curve[length - 1];
prev.time -= m_Range;
var next = m_Curve[0];
next.time += m_Range;
m_LoopingCurve.keys = m_Curve.keys; // GC pressure
m_LoopingCurve.AddKey(prev);
m_LoopingCurve.AddKey(next);
m_IsCurveDirty = false;
}
return m_LoopingCurve.Evaluate(time);
}
/// <summary>
/// Adds a new key to the curve.
/// </summary>
/// <param name="time">The time at which to add the key.</param>
/// <param name="value">The value for the key.</param>
/// <returns>The index of the added key, or -1 if the key could not be added.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int AddKey(float time, float value)
{
int r = m_Curve.AddKey(time, value);
if (r > -1)
SetDirty();
return r;
}
/// <summary>
/// Removes the keyframe at <paramref name="index"/> and inserts <paramref name="key"/>.
/// </summary>
/// <param name="index">The index of the keyframe to replace.</param>
/// <param name="key">The new keyframe to insert at the specified index.</param>
/// <returns>The index of the keyframe after moving it.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int MoveKey(int index, in Keyframe key)
{
int r = m_Curve.MoveKey(index, key);
SetDirty();
return r;
}
/// <summary>
/// Removes a key.
/// </summary>
/// <param name="index">The index of the key to remove.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void RemoveKey(int index)
{
m_Curve.RemoveKey(index);
SetDirty();
}
/// <summary>
/// Smoothes the in and out tangents of the keyframe at <paramref name="index"/>. A <paramref name="weight"/> of 0 evens out tangents.
/// </summary>
/// <param name="index">The index of the keyframe to be smoothed.</param>
/// <param name="weight">The smoothing weight to apply to the keyframe's tangents.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SmoothTangents(int index, float weight)
{
m_Curve.SmoothTangents(index, weight);
SetDirty();
}
}
/// <summary>
/// A <see cref="VolumeParameter"/> that holds a <see cref="TextureCurve"/> value.
/// </summary>
[Serializable]
public class TextureCurveParameter : VolumeParameter<TextureCurve>
{
/// <summary>
/// Creates a new <see cref="TextureCurveParameter"/> instance.
/// </summary>
/// <param name="value">The initial value to store in the parameter.</param>
/// <param name="overrideState">The initial override state for the parameter.</param>
public TextureCurveParameter(TextureCurve value, bool overrideState = false)
: base(value, overrideState) { }
/// <summary>
/// Release implementation.
/// </summary>
public override void Release() => m_Value.Release();
// TODO: TextureCurve interpolation
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 687539e57047cee4fac5fd3507becbe9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,264 @@
using System;
using System.Runtime.CompilerServices;
using UnityEngine.Experimental.Rendering;
namespace UnityEngine.Rendering
{
// Due to limitations in the builtin Gradient we need this custom wrapper.
/// <summary>
/// A wrapper around <c>Gradient</c> to automatically bake it into a texture.
/// </summary>
[Serializable]
public class TextureGradient : IDisposable
{
/// <summary>
/// Texture Size computed.
/// </summary>
[field: SerializeField, HideInInspector]
public int textureSize { get; private set; }
/// <summary>
/// Internal Gradient used to generate the Texture
/// </summary>
[SerializeField]
Gradient m_Gradient;
Texture2D m_Texture = null;
int m_RequestedTextureSize = -1;
bool m_IsTextureDirty;
bool m_Precise;
/// <summary>All color keys defined in the gradient.</summary>
[HideInInspector]
public GradientColorKey[] colorKeys => m_Gradient?.colorKeys;
/// <summary>All alpha keys defined in the gradient.</summary>
[HideInInspector]
public GradientAlphaKey[] alphaKeys => m_Gradient?.alphaKeys;
/// <summary>Controls how the gradient colors are interpolated.</summary>
[SerializeField, HideInInspector]
public GradientMode mode = GradientMode.PerceptualBlend;
/// <summary>Indicates the color space that the gradient color keys are using.</summary>
[SerializeField, HideInInspector]
public ColorSpace colorSpace = ColorSpace.Uninitialized;
/// <summary>
/// Creates a new <see cref="TextureGradient"/> from an existing <c>Gradient</c>.
/// </summary>
/// <param name="baseCurve">The source <c>Gradient</c>.</param>
public TextureGradient(Gradient baseCurve)
: this(baseCurve.colorKeys, baseCurve.alphaKeys)
{
mode = baseCurve.mode;
colorSpace = baseCurve.colorSpace;
m_Gradient.mode = baseCurve.mode;
m_Gradient.colorSpace = baseCurve.colorSpace;
}
/// <summary>
/// Creates a new <see cref="TextureCurve"/> from an arbitrary number of keyframes.
/// </summary>
/// <param name="colorKeys">An array of keyframes used to define the color of gradient.</param>
/// <param name="alphaKeys">An array of keyframes used to define the alpha of gradient.</param>
/// <param name="mode">Indicates the color space that the gradient color keys are using.</param>
/// <param name="colorSpace">Controls how the gradient colors are interpolated.</param>
/// <param name="requestedTextureSize">Texture Size used internally, if '-1' using Nyquist-Shannon limits.</param>
/// <param name="precise">if precise uses 4*Nyquist-Shannon limits, 2* otherwise.</param>
public TextureGradient(GradientColorKey[] colorKeys, GradientAlphaKey[] alphaKeys, GradientMode mode = GradientMode.PerceptualBlend, ColorSpace colorSpace = ColorSpace.Uninitialized, int requestedTextureSize = -1, bool precise = false)
{
Rebuild(colorKeys, alphaKeys, mode, colorSpace, requestedTextureSize, precise);
}
void Rebuild(GradientColorKey[] colorKeys, GradientAlphaKey[] alphaKeys, GradientMode mode, ColorSpace colorSpace, int requestedTextureSize, bool precise)
{
m_Gradient = new Gradient();
m_Gradient.mode = mode;
m_Gradient.colorSpace = colorSpace;
m_Gradient.SetKeys(colorKeys, alphaKeys);
m_Precise = precise;
m_RequestedTextureSize = requestedTextureSize;
if (requestedTextureSize > 0)
{
textureSize = requestedTextureSize;
}
else
{
float smallestDelta = 1.0f;
float[] times = new float[colorKeys.Length + alphaKeys.Length];
for (int i = 0; i < colorKeys.Length; ++i)
{
times[i] = colorKeys[i].time;
}
for (int i = 0; i < alphaKeys.Length; ++i)
{
times[colorKeys.Length + i] = alphaKeys[i].time;
}
Array.Sort(times);
// Found the smallest increment between 2 keys
for (int i = 1; i < times.Length; ++i)
{
int k0 = Math.Max(i - 1, 0);
int k1 = Math.Min(i, times.Length - 1);
float delta = Mathf.Abs(times[k0] - times[k1]);
// Do not compare if time is duplicated
if (delta > 0 && delta < smallestDelta)
smallestDelta = delta;
}
// Nyquist-Shannon
// smallestDelta: 1.00f => Sampling => 2
// smallestDelta: 0.50f => Sampling => 3
// smallestDelta: 0.33f => Sampling => 4
// smallestDelta: 0.25f => Sampling => 5
// 2x: Theoretical limits
// 4x: Preserve original frequency
// Round to the closest 4 * Nyquist-Shannon limits
// 4x for Fixed to capture sharp discontinuity
float scale;
if (precise || mode == GradientMode.Fixed)
scale = 4.0f;
else
scale = 2.0f;
float sizef = scale * Mathf.Ceil(1.0f / smallestDelta + 1.0f);
textureSize = Mathf.RoundToInt(sizef);
// Arbitrary max (1024)
textureSize = Math.Min(textureSize, 1024);
}
SetDirty();
}
/// <summary>
/// Cleans up the internal texture resource.
/// </summary>
public void Dispose()
{
//Release();
}
/// <summary>
/// Releases the internal texture resource.
/// </summary>
public void Release()
{
if (m_Texture != null)
CoreUtils.Destroy(m_Texture);
m_Texture = null;
}
/// <summary>
/// Marks the curve as dirty to trigger a redraw of the texture the next time <see cref="GetTexture"/>
/// is called.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetDirty()
{
m_IsTextureDirty = true;
}
static GraphicsFormat GetTextureFormat()
{
return GraphicsFormat.R8G8B8A8_UNorm;
}
/// <summary>
/// Gets the texture representation of this Gradient.
/// </summary>
/// <returns>A texture.</returns>
public Texture2D GetTexture()
{
float step = 1.0f / (float)(textureSize - 1);
if (m_Texture != null && m_Texture.width != textureSize)
{
Object.DestroyImmediate(m_Texture);
m_Texture = null;
}
if (m_Texture == null)
{
m_Texture = new Texture2D(textureSize, 1, GetTextureFormat(), TextureCreationFlags.None);
m_Texture.name = "GradientTexture";
m_Texture.hideFlags = HideFlags.HideAndDontSave;
m_Texture.filterMode = FilterMode.Bilinear;
m_Texture.wrapMode = TextureWrapMode.Clamp;
m_Texture.anisoLevel = 0;
m_IsTextureDirty = true;
}
if (m_IsTextureDirty)
{
var pixels = new Color[textureSize];
for (int i = 0; i < textureSize; i++)
pixels[i] = Evaluate(i * step);
m_Texture.SetPixels(pixels);
m_Texture.Apply(false, false);
m_IsTextureDirty = false;
m_Texture.IncrementUpdateCount();
}
return m_Texture;
}
/// <summary>
/// Evaluate a time value on the Gradient.
/// </summary>
/// <param name="time">The time within the Gradient you want to evaluate.</param>
/// <returns>The value of the Gradient, at the point in time specified.</returns>
public Color Evaluate(float time)
{
if (textureSize <= 0)
return Color.black;
return m_Gradient.Evaluate(time);
}
/// <summary>
/// Setup Gradient with an array of color keys and alpha keys.
/// </summary>
/// <param name="colorKeys">Color keys of the gradient (maximum 8 color keys).</param>
/// <param name="alphaKeys">Alpha keys of the gradient (maximum 8 alpha keys).</param>
/// <param name="mode">Indicates the color space that the gradient color keys are using.</param>
/// <param name="colorSpace">Controls how the gradient colors are interpolated.</param>
public void SetKeys(GradientColorKey[] colorKeys, GradientAlphaKey[] alphaKeys, GradientMode mode, ColorSpace colorSpace)
{
m_Gradient.SetKeys(colorKeys, alphaKeys);
m_Gradient.mode = mode;
m_Gradient.colorSpace = colorSpace;
// Rebuild will make the TextureGradient Dirty.
Rebuild(colorKeys, alphaKeys, mode, colorSpace, m_RequestedTextureSize, m_Precise);
}
}
/// <summary>
/// A <see cref="VolumeParameter"/> that holds a <see cref="TextureGradient"/> value.
/// </summary>
[Serializable]
public class TextureGradientParameter : VolumeParameter<TextureGradient>
{
/// <summary>
/// Creates a new <see cref="TextureGradientParameter"/> instance.
/// </summary>
/// <param name="value">The initial value to store in the parameter.</param>
/// <param name="overrideState">The initial override state for the parameter.</param>
public TextureGradientParameter(TextureGradient value, bool overrideState = false)
: base(value, overrideState) { }
/// <summary>
/// Release implementation.
/// </summary>
public override void Release() => m_Value.Release();
// TODO: TextureGradient interpolation
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 92b333b1e77fb4e4a9348235a62c28b2
@@ -0,0 +1,148 @@
namespace UnityEngine.Rendering
{
/// <summary>Utility for tiles layout</summary>
public static class TileLayoutUtils
{
/// <summary>Try decompose the givent rect into tiles given the parameter</summary>
/// <param name="src">The rect to split</param>
/// <param name="tileSize">The size of the tiles</param>
/// <param name="main">Computed main area</param>
/// <param name="topRow">Computed top row area</param>
/// <param name="rightCol">Computed right column area</param>
/// <param name="topRight">Computed top right corner area</param>
/// <returns>If true, the tiles decomposition is a success</returns>
public static bool TryLayoutByTiles(
RectInt src,
uint tileSize,
out RectInt main,
out RectInt topRow,
out RectInt rightCol,
out RectInt topRight)
{
if (src.width < tileSize || src.height < tileSize)
{
main = new RectInt(0, 0, 0, 0);
topRow = new RectInt(0, 0, 0, 0);
rightCol = new RectInt(0, 0, 0, 0);
topRight = new RectInt(0, 0, 0, 0);
return false;
}
int mainRows = src.height / (int)tileSize;
int mainCols = src.width / (int)tileSize;
int mainWidth = mainCols * (int)tileSize;
int mainHeight = mainRows * (int)tileSize;
main = new RectInt
{
x = src.x,
y = src.y,
width = mainWidth,
height = mainHeight,
};
topRow = new RectInt
{
x = src.x,
y = src.y + mainHeight,
width = mainWidth,
height = src.height - mainHeight
};
rightCol = new RectInt
{
x = src.x + mainWidth,
y = src.y,
width = src.width - mainWidth,
height = mainHeight
};
topRight = new RectInt
{
x = src.x + mainWidth,
y = src.y + mainHeight,
width = src.width - mainWidth,
height = src.height - mainHeight
};
return true;
}
/// <summary>Try decompose the givent rect into rows given the parameter</summary>
/// <param name="src">The rect to split</param>
/// <param name="tileSize">The size of the tiles</param>
/// <param name="main">Computed main area</param>
/// <param name="other">Computed other area</param>
/// <returns>If true, the tiles decomposition is a success</returns>
public static bool TryLayoutByRow(
RectInt src,
uint tileSize,
out RectInt main,
out RectInt other)
{
if (src.height < tileSize)
{
main = new RectInt(0, 0, 0, 0);
other = new RectInt(0, 0, 0, 0);
return false;
}
int mainRows = src.height / (int)tileSize;
int mainHeight = mainRows * (int)tileSize;
main = new RectInt
{
x = src.x,
y = src.y,
width = src.width,
height = mainHeight,
};
other = new RectInt
{
x = src.x,
y = src.y + mainHeight,
width = src.width,
height = src.height - mainHeight
};
return true;
}
/// <summary>Try decompose the givent rect into columns given the parameter</summary>
/// <param name="src">The rect to split</param>
/// <param name="tileSize">The size of the tiles</param>
/// <param name="main">Computed main area</param>
/// <param name="other">Computed other area</param>
/// <returns>If true, the tiles decomposition is a success</returns>
public static bool TryLayoutByCol(
RectInt src,
uint tileSize,
out RectInt main,
out RectInt other)
{
if (src.width < tileSize)
{
main = new RectInt(0, 0, 0, 0);
other = new RectInt(0, 0, 0, 0);
return false;
}
int mainCols = src.width / (int)tileSize;
int mainWidth = mainCols * (int)tileSize;
main = new RectInt
{
x = src.x,
y = src.y,
width = mainWidth,
height = src.height,
};
other = new RectInt
{
x = src.x + mainWidth,
y = src.y,
width = src.width - mainWidth,
height = src.height
};
return true;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 39aaac96b648c194298b5441a11fd53a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: