testss
This commit is contained in:
@@ -0,0 +1,208 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.Assertions;
|
||||
using UnityEngine.Rendering;
|
||||
|
||||
namespace UnityEngine.Rendering
|
||||
{
|
||||
/// <summary>
|
||||
/// Implement a multiple buffering for RenderTextures.
|
||||
/// </summary>
|
||||
/// <exemple>
|
||||
/// <code>
|
||||
/// enum BufferType
|
||||
/// {
|
||||
/// Color,
|
||||
/// Depth
|
||||
/// }
|
||||
///
|
||||
/// void Render()
|
||||
/// {
|
||||
/// var camera = GetCamera();
|
||||
/// var buffers = GetFrameHistoryBuffersFor(camera);
|
||||
///
|
||||
/// // Set reference size in case the rendering size changed this frame
|
||||
/// buffers.SetReferenceSize(
|
||||
/// GetCameraWidth(camera), GetCameraHeight(camera),
|
||||
/// GetCameraUseMSAA(camera), GetCameraMSAASamples(camera)
|
||||
/// );
|
||||
/// buffers.Swap();
|
||||
///
|
||||
/// var currentColor = buffer.GetFrameRT((int)BufferType.Color, 0);
|
||||
/// if (currentColor == null) // Buffer was not allocated
|
||||
/// {
|
||||
/// buffer.AllocBuffer(
|
||||
/// (int)BufferType.Color, // Color buffer id
|
||||
/// ColorBufferAllocator, // Custom functor to implement allocation
|
||||
/// 2 // Use 2 RT for this buffer for double buffering
|
||||
/// );
|
||||
/// currentColor = buffer.GetFrameRT((int)BufferType.Color, 0);
|
||||
/// }
|
||||
///
|
||||
/// var previousColor = buffers.GetFrameRT((int)BufferType.Color, 1);
|
||||
///
|
||||
/// // Use previousColor and write into currentColor
|
||||
/// }
|
||||
/// </code>
|
||||
/// </exemple>
|
||||
public class BufferedRTHandleSystem : IDisposable
|
||||
{
|
||||
Dictionary<int, RTHandle[]> m_RTHandles = new Dictionary<int, RTHandle[]>();
|
||||
|
||||
RTHandleSystem m_RTHandleSystem = new RTHandleSystem();
|
||||
bool m_DisposedValue = false;
|
||||
|
||||
/// <summary>
|
||||
/// Maximum allocated width of the Buffered RTHandle System
|
||||
/// </summary>
|
||||
public int maxWidth { get { return m_RTHandleSystem.GetMaxWidth(); } }
|
||||
/// <summary>
|
||||
/// Maximum allocated height of the Buffered RTHandle System
|
||||
/// </summary>
|
||||
public int maxHeight { get { return m_RTHandleSystem.GetMaxHeight(); } }
|
||||
/// <summary>
|
||||
/// Current properties of the Buffered RTHandle System
|
||||
/// </summary>
|
||||
public RTHandleProperties rtHandleProperties { get { return m_RTHandleSystem.rtHandleProperties; } }
|
||||
|
||||
/// <summary>
|
||||
/// Return the frame RT or null.
|
||||
/// </summary>
|
||||
/// <param name="bufferId">Defines the buffer to use.</param>
|
||||
/// <param name="frameIndex"></param>
|
||||
/// <returns>The frame RT or null when the <paramref name="bufferId"/> was not previously allocated (<see cref="BufferedRTHandleSystem.AllocBuffer(int, Func{RTHandleSystem, int, RTHandle}, int)" />).</returns>
|
||||
public RTHandle GetFrameRT(int bufferId, int frameIndex)
|
||||
{
|
||||
if (!m_RTHandles.ContainsKey(bufferId))
|
||||
return null;
|
||||
|
||||
Assert.IsTrue(frameIndex >= 0 && frameIndex < m_RTHandles[bufferId].Length);
|
||||
|
||||
return m_RTHandles[bufferId][frameIndex];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocate RT handles for a buffer.
|
||||
/// </summary>
|
||||
/// <param name="bufferId">The buffer to allocate.</param>
|
||||
/// <param name="allocator">The functor to use for allocation.</param>
|
||||
/// <param name="bufferCount">The number of RT handles for this buffer.</param>
|
||||
public void AllocBuffer(
|
||||
int bufferId,
|
||||
Func<RTHandleSystem, int, RTHandle> allocator,
|
||||
int bufferCount
|
||||
)
|
||||
{
|
||||
var buffer = new RTHandle[bufferCount];
|
||||
m_RTHandles.Add(bufferId, buffer);
|
||||
|
||||
// First is autoresized
|
||||
buffer[0] = allocator(m_RTHandleSystem, 0);
|
||||
|
||||
// Other are resized on demand
|
||||
for (int i = 1, c = buffer.Length; i < c; ++i)
|
||||
{
|
||||
buffer[i] = allocator(m_RTHandleSystem, i);
|
||||
m_RTHandleSystem.SwitchResizeMode(buffer[i], RTHandleSystem.ResizeMode.OnDemand);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Release a buffer
|
||||
/// </summary>
|
||||
/// <param name="bufferId">Id of the buffer that needs to be released.</param>
|
||||
public void ReleaseBuffer(int bufferId)
|
||||
{
|
||||
if (m_RTHandles.TryGetValue(bufferId, out var buffers))
|
||||
{
|
||||
foreach (var rt in buffers)
|
||||
m_RTHandleSystem.Release(rt);
|
||||
}
|
||||
|
||||
m_RTHandles.Remove(bufferId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Swap buffers Set the reference size for this RT Handle System (<see cref="RTHandleSystem.SetReferenceSize(int, int, bool, MSAASamples)"/>)
|
||||
/// </summary>
|
||||
/// <param name="width">The width of the RTs of this buffer.</param>
|
||||
/// <param name="height">The height of the RTs of this buffer.</param>
|
||||
/// <param name="msaaSamples">Number of MSAA samples for this buffer.</param>
|
||||
public void SwapAndSetReferenceSize(int width, int height, MSAASamples msaaSamples)
|
||||
{
|
||||
Swap();
|
||||
m_RTHandleSystem.SetReferenceSize(width, height, msaaSamples);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reset the reference size of the system and reallocate all textures.
|
||||
/// </summary>
|
||||
/// <param name="width">New width.</param>
|
||||
/// <param name="height">New height.</param>
|
||||
public void ResetReferenceSize(int width, int height)
|
||||
{
|
||||
m_RTHandleSystem.ResetReferenceSize(width, height);
|
||||
}
|
||||
|
||||
void Swap()
|
||||
{
|
||||
foreach (var item in m_RTHandles)
|
||||
{
|
||||
// Do not index out of bounds...
|
||||
if (item.Value.Length > 1)
|
||||
{
|
||||
var nextFirst = item.Value[item.Value.Length - 1];
|
||||
for (int i = 0, c = item.Value.Length - 1; i < c; ++i)
|
||||
item.Value[i + 1] = item.Value[i];
|
||||
item.Value[0] = nextFirst;
|
||||
|
||||
// First is autoresize, other are on demand
|
||||
m_RTHandleSystem.SwitchResizeMode(item.Value[0], RTHandleSystem.ResizeMode.Auto);
|
||||
m_RTHandleSystem.SwitchResizeMode(item.Value[1], RTHandleSystem.ResizeMode.OnDemand);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_RTHandleSystem.SwitchResizeMode(item.Value[0], RTHandleSystem.ResizeMode.Auto);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Dispose(bool disposing)
|
||||
{
|
||||
if (!m_DisposedValue)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
ReleaseAll();
|
||||
m_RTHandleSystem.Dispose();
|
||||
m_RTHandleSystem = null;
|
||||
}
|
||||
|
||||
m_DisposedValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dispose implementation
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deallocate and clear all buffers.
|
||||
/// </summary>
|
||||
public void ReleaseAll()
|
||||
{
|
||||
foreach (var item in m_RTHandles)
|
||||
{
|
||||
for (int i = 0, c = item.Value.Length; i < c; ++i)
|
||||
{
|
||||
m_RTHandleSystem.Release(item.Value[i]);
|
||||
}
|
||||
}
|
||||
m_RTHandles.Clear();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
namespace UnityEngine.Rendering
|
||||
{
|
||||
/// <summary>
|
||||
/// Bit depths of a Depth render texture.
|
||||
/// Some values may not be supported on all platforms.
|
||||
/// </summary>
|
||||
public enum DepthBits
|
||||
{
|
||||
/// <summary>No Depth Buffer.</summary>
|
||||
None = 0,
|
||||
/// <summary>8 bits Depth Buffer.</summary>
|
||||
Depth8 = 8,
|
||||
/// <summary>16 bits Depth Buffer.</summary>
|
||||
Depth16 = 16,
|
||||
/// <summary>24 bits Depth Buffer.</summary>
|
||||
Depth24 = 24,
|
||||
/// <summary>32 bits Depth Buffer.</summary>
|
||||
Depth32 = 32
|
||||
}
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
namespace UnityEngine.Rendering
|
||||
{
|
||||
/// <summary>
|
||||
/// Number of MSAA samples.
|
||||
/// </summary>
|
||||
public enum MSAASamples
|
||||
{
|
||||
/// <summary>No MSAA.</summary>
|
||||
None = 1,
|
||||
/// <summary>MSAA 2X.</summary>
|
||||
MSAA2x = 2,
|
||||
/// <summary>MSAA 4X.</summary>
|
||||
MSAA4x = 4,
|
||||
/// <summary>MSAA 8X.</summary>
|
||||
MSAA8x = 8
|
||||
}
|
||||
}
|
@@ -0,0 +1,205 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.Rendering;
|
||||
|
||||
namespace UnityEngine.Rendering
|
||||
{
|
||||
/// <summary>
|
||||
/// A RTHandle is a RenderTexture that scales automatically with the camera size.
|
||||
/// This allows proper reutilization of RenderTexture memory when different cameras with various sizes are used during rendering.
|
||||
/// <seealso cref="RTHandleSystem"/>
|
||||
/// </summary>
|
||||
public class RTHandle
|
||||
{
|
||||
internal RTHandleSystem m_Owner;
|
||||
internal RenderTexture m_RT;
|
||||
internal Texture m_ExternalTexture;
|
||||
internal RenderTargetIdentifier m_NameID;
|
||||
internal bool m_EnableMSAA = false;
|
||||
internal bool m_EnableRandomWrite = false;
|
||||
internal bool m_EnableHWDynamicScale = false;
|
||||
internal string m_Name;
|
||||
|
||||
/// <summary>
|
||||
/// Scale factor applied to the RTHandle reference size.
|
||||
/// </summary>
|
||||
public Vector2 scaleFactor { get; internal set; }
|
||||
internal ScaleFunc scaleFunc;
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the RTHandle uses automatic scaling.
|
||||
/// </summary>
|
||||
public bool useScaling { get; internal set; }
|
||||
/// <summary>
|
||||
/// Reference size of the RTHandle System associated with the RTHandle
|
||||
/// </summary>
|
||||
public Vector2Int referenceSize {get; internal set; }
|
||||
/// <summary>
|
||||
/// Current properties of the RTHandle System
|
||||
/// </summary>
|
||||
public RTHandleProperties rtHandleProperties { get { return m_Owner.rtHandleProperties; } }
|
||||
/// <summary>
|
||||
/// RenderTexture associated with the RTHandle
|
||||
/// </summary>
|
||||
public RenderTexture rt { get { return m_RT; } }
|
||||
/// <summary>
|
||||
/// RenderTargetIdentifier associated with the RTHandle
|
||||
/// </summary>
|
||||
public RenderTargetIdentifier nameID { get { return m_NameID; } }
|
||||
/// <summary>
|
||||
/// Name of the RTHandle
|
||||
/// </summary>
|
||||
public string name { get { return m_Name; } }
|
||||
|
||||
/// <summary>
|
||||
/// Returns true is MSAA is enabled, false otherwise.
|
||||
/// </summary>
|
||||
public bool isMSAAEnabled { get { return m_EnableMSAA; } }
|
||||
|
||||
// Keep constructor private
|
||||
internal RTHandle(RTHandleSystem owner)
|
||||
{
|
||||
m_Owner = owner;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicit conversion operator to RenderTargetIdentifier
|
||||
/// </summary>
|
||||
/// <param name="handle">Input RTHandle</param>
|
||||
/// <returns>RenderTargetIdentifier representation of the RTHandle.</returns>
|
||||
public static implicit operator RenderTargetIdentifier(RTHandle handle)
|
||||
{
|
||||
return handle != null ? handle.nameID : default(RenderTargetIdentifier);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicit conversion operator to Texture
|
||||
/// </summary>
|
||||
/// <param name="handle">Input RTHandle</param>
|
||||
/// <returns>Texture representation of the RTHandle.</returns>
|
||||
public static implicit operator Texture(RTHandle handle)
|
||||
{
|
||||
// If RTHandle is null then conversion should give a null Texture
|
||||
if (handle == null)
|
||||
return null;
|
||||
|
||||
Debug.Assert(handle.m_ExternalTexture != null || handle.rt != null);
|
||||
return (handle.rt != null) ? handle.rt : handle.m_ExternalTexture;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicit conversion operator to RenderTexture
|
||||
/// </summary>
|
||||
/// <param name="handle">Input RTHandle</param>
|
||||
/// <returns>RenderTexture representation of the RTHandle.</returns>
|
||||
public static implicit operator RenderTexture(RTHandle handle)
|
||||
{
|
||||
// If RTHandle is null then conversion should give a null RenderTexture
|
||||
if (handle == null)
|
||||
return null;
|
||||
|
||||
Debug.Assert(handle.rt != null, "RTHandle was created using a regular Texture and is used as a RenderTexture");
|
||||
return handle.rt;
|
||||
}
|
||||
|
||||
internal void SetRenderTexture(RenderTexture rt)
|
||||
{
|
||||
m_RT = rt;
|
||||
m_ExternalTexture = null;
|
||||
m_NameID = new RenderTargetIdentifier(rt);
|
||||
}
|
||||
|
||||
internal void SetTexture(Texture tex)
|
||||
{
|
||||
m_RT = null;
|
||||
m_ExternalTexture = tex;
|
||||
m_NameID = new RenderTargetIdentifier(tex);
|
||||
}
|
||||
|
||||
internal void SetTexture(RenderTargetIdentifier tex)
|
||||
{
|
||||
m_RT = null;
|
||||
m_ExternalTexture = null;
|
||||
m_NameID = tex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Release the RTHandle
|
||||
/// </summary>
|
||||
public void Release()
|
||||
{
|
||||
m_Owner.Remove(this);
|
||||
CoreUtils.Destroy(m_RT);
|
||||
m_NameID = BuiltinRenderTextureType.None;
|
||||
m_RT = null;
|
||||
m_ExternalTexture = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the input size, scaled by the RTHandle scale factor.
|
||||
/// </summary>
|
||||
/// <param name="refSize">Input size</param>
|
||||
/// <returns>Input size scaled by the RTHandle scale factor.</returns>
|
||||
public Vector2Int GetScaledSize(Vector2Int refSize)
|
||||
{
|
||||
if (!useScaling)
|
||||
return refSize;
|
||||
|
||||
if (scaleFunc != null)
|
||||
{
|
||||
return scaleFunc(refSize);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Vector2Int(
|
||||
x: Mathf.RoundToInt(scaleFactor.x * refSize.x),
|
||||
y: Mathf.RoundToInt(scaleFactor.y * refSize.y)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_2020_2_OR_NEWER
|
||||
/// <summary>
|
||||
/// Switch the render target to fast memory on platform that have it.
|
||||
/// </summary>
|
||||
/// <param name="cmd">Command buffer used for rendering.</param>
|
||||
/// <param name="residencyFraction">How much of the render target is to be switched into fast memory (between 0 and 1).</param>
|
||||
/// <param name="flags">Flag to determine what parts of the render target is spilled if not fully resident in fast memory.</param>
|
||||
/// <param name="copyContents">Whether the content of render target are copied or not when switching to fast memory.</param>
|
||||
|
||||
public void SwitchToFastMemory(CommandBuffer cmd,
|
||||
float residencyFraction = 1.0f,
|
||||
FastMemoryFlags flags = FastMemoryFlags.SpillTop,
|
||||
bool copyContents = false
|
||||
)
|
||||
{
|
||||
residencyFraction = Mathf.Clamp01(residencyFraction);
|
||||
cmd.SwitchIntoFastMemory(m_RT, flags, residencyFraction, copyContents);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Switch the render target to fast memory on platform that have it and copies the content.
|
||||
/// </summary>
|
||||
/// <param name="cmd">Command buffer used for rendering.</param>
|
||||
/// <param name="residencyFraction">How much of the render target is to be switched into fast memory (between 0 and 1).</param>
|
||||
/// <param name="flags">Flag to determine what parts of the render target is spilled if not fully resident in fast memory.</param>
|
||||
public void CopyToFastMemory(CommandBuffer cmd,
|
||||
float residencyFraction = 1.0f,
|
||||
FastMemoryFlags flags = FastMemoryFlags.SpillTop
|
||||
)
|
||||
{
|
||||
SwitchToFastMemory(cmd, residencyFraction, flags, copyContents: true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Switch out the render target from fast memory back to main memory on platforms that have fast memory.
|
||||
/// </summary>
|
||||
/// <param name="cmd">Command buffer used for rendering.</param>
|
||||
/// <param name="copyContents">Whether the content of render target are copied or not when switching out fast memory.</param>
|
||||
public void SwitchOutFastMemory(CommandBuffer cmd, bool copyContents = true)
|
||||
{
|
||||
cmd.SwitchOutOfFastMemory(m_RT, copyContents);
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
@@ -0,0 +1,898 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.Assertions;
|
||||
using UnityEngine.Experimental.Rendering;
|
||||
|
||||
namespace UnityEngine.Rendering
|
||||
{
|
||||
/// <summary>
|
||||
/// Scaled function used to compute the size of a RTHandle for the current frame.
|
||||
/// </summary>
|
||||
/// <param name="size">Reference size of the RTHandle system for the frame.</param>
|
||||
/// <returns>The size of the RTHandled computed from the reference size.</returns>
|
||||
public delegate Vector2Int ScaleFunc(Vector2Int size);
|
||||
|
||||
/// <summary>
|
||||
/// List of properties of the RTHandle System for the current frame.
|
||||
/// </summary>
|
||||
public struct RTHandleProperties
|
||||
{
|
||||
/// <summary>
|
||||
/// Size set as reference at the previous frame
|
||||
/// </summary>
|
||||
public Vector2Int previousViewportSize;
|
||||
/// <summary>
|
||||
/// Size of the render targets at the previous frame
|
||||
/// </summary>
|
||||
public Vector2Int previousRenderTargetSize;
|
||||
/// <summary>
|
||||
/// Size set as reference at the current frame
|
||||
/// </summary>
|
||||
public Vector2Int currentViewportSize;
|
||||
/// <summary>
|
||||
/// Size of the render targets at the current frame
|
||||
/// </summary>
|
||||
public Vector2Int currentRenderTargetSize;
|
||||
/// <summary>
|
||||
/// Scale factor from RTHandleSystem max size to requested reference size (referenceSize/maxSize)
|
||||
/// (x,y) current frame (z,w) last frame (this is only used for buffered RTHandle Systems)
|
||||
/// </summary>
|
||||
public Vector4 rtHandleScale;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// System managing a set of RTHandle textures
|
||||
/// </summary>
|
||||
public partial class RTHandleSystem : IDisposable
|
||||
{
|
||||
internal enum ResizeMode
|
||||
{
|
||||
Auto,
|
||||
OnDemand
|
||||
}
|
||||
|
||||
// Parameters for auto-scaled Render Textures
|
||||
bool m_HardwareDynamicResRequested = false;
|
||||
bool m_ScaledRTSupportsMSAA = false;
|
||||
MSAASamples m_ScaledRTCurrentMSAASamples = MSAASamples.None;
|
||||
HashSet<RTHandle> m_AutoSizedRTs;
|
||||
RTHandle[] m_AutoSizedRTsArray; // For fast iteration
|
||||
HashSet<RTHandle> m_ResizeOnDemandRTs;
|
||||
RTHandleProperties m_RTHandleProperties;
|
||||
|
||||
/// <summary>
|
||||
/// Current properties of the RTHandle System.
|
||||
/// </summary>
|
||||
public RTHandleProperties rtHandleProperties { get { return m_RTHandleProperties; } }
|
||||
|
||||
int m_MaxWidths = 0;
|
||||
int m_MaxHeights = 0;
|
||||
#if UNITY_EDITOR
|
||||
// In editor every now and then we must reset the size of the rthandle system if it was set very high and then switched back to a much smaller scale.
|
||||
int m_FramesSinceLastReset = 0;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// RTHandleSystem constructor.
|
||||
/// </summary>
|
||||
public RTHandleSystem()
|
||||
{
|
||||
m_AutoSizedRTs = new HashSet<RTHandle>();
|
||||
m_ResizeOnDemandRTs = new HashSet<RTHandle>();
|
||||
m_MaxWidths = 1;
|
||||
m_MaxHeights = 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposable pattern implementation
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the RTHandle system.
|
||||
/// </summary>
|
||||
/// <param name="width">Initial reference rendering width.</param>
|
||||
/// <param name="height">Initial reference rendering height.</param>
|
||||
/// <param name="scaledRTsupportsMSAA">Set to true if automatically scaled RTHandles should support MSAA</param>
|
||||
/// <param name="scaledRTMSAASamples">Number of MSAA samples for automatically scaled RTHandles.</param>
|
||||
public void Initialize(int width, int height, bool scaledRTsupportsMSAA, MSAASamples scaledRTMSAASamples)
|
||||
{
|
||||
if (m_AutoSizedRTs.Count != 0)
|
||||
{
|
||||
string leakingResources = "Unreleased RTHandles:";
|
||||
foreach (var rt in m_AutoSizedRTs)
|
||||
{
|
||||
leakingResources = string.Format("{0}\n {1}", leakingResources, rt.name);
|
||||
}
|
||||
Debug.LogError(string.Format("RTHandle.Initialize should only be called once before allocating any Render Texture. This may be caused by an unreleased RTHandle resource.\n{0}\n", leakingResources));
|
||||
}
|
||||
|
||||
m_MaxWidths = width;
|
||||
m_MaxHeights = height;
|
||||
|
||||
m_ScaledRTSupportsMSAA = scaledRTsupportsMSAA;
|
||||
m_ScaledRTCurrentMSAASamples = scaledRTMSAASamples;
|
||||
|
||||
m_HardwareDynamicResRequested = DynamicResolutionHandler.instance.RequestsHardwareDynamicResolution();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Release memory of a RTHandle from the RTHandle System
|
||||
/// </summary>
|
||||
/// <param name="rth">RTHandle that should be released.</param>
|
||||
public void Release(RTHandle rth)
|
||||
{
|
||||
if (rth != null)
|
||||
{
|
||||
Assert.AreEqual(this, rth.m_Owner);
|
||||
rth.Release();
|
||||
}
|
||||
}
|
||||
|
||||
internal void Remove(RTHandle rth)
|
||||
{
|
||||
m_AutoSizedRTs.Remove(rth);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reset the reference size of the system and reallocate all textures.
|
||||
/// </summary>
|
||||
/// <param name="width">New width.</param>
|
||||
/// <param name="height">New height.</param>
|
||||
public void ResetReferenceSize(int width, int height)
|
||||
{
|
||||
m_MaxWidths = width;
|
||||
m_MaxHeights = height;
|
||||
SetReferenceSize(width, height, m_ScaledRTCurrentMSAASamples, reset: true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the reference rendering size for subsequent rendering for the RTHandle System
|
||||
/// </summary>
|
||||
/// <param name="width">Reference rendering width for subsequent rendering.</param>
|
||||
/// <param name="height">Reference rendering height for subsequent rendering.</param>
|
||||
/// <param name="msaaSamples">Number of MSAA samples for multisampled textures for subsequent rendering.</param>
|
||||
public void SetReferenceSize(int width, int height, MSAASamples msaaSamples)
|
||||
{
|
||||
SetReferenceSize(width, height, msaaSamples, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the reference rendering size for subsequent rendering for the RTHandle System
|
||||
/// </summary>
|
||||
/// <param name="width">Reference rendering width for subsequent rendering.</param>
|
||||
/// <param name="height">Reference rendering height for subsequent rendering.</param>
|
||||
/// <param name="msaaSamples">Number of MSAA samples for multisampled textures for subsequent rendering.</param>
|
||||
/// <param name="reset">If set to true, the new width and height will override the old values even if they are not bigger.</param>
|
||||
public void SetReferenceSize(int width, int height, MSAASamples msaaSamples, bool reset)
|
||||
{
|
||||
m_RTHandleProperties.previousViewportSize = m_RTHandleProperties.currentViewportSize;
|
||||
m_RTHandleProperties.previousRenderTargetSize = m_RTHandleProperties.currentRenderTargetSize;
|
||||
Vector2 lastFrameMaxSize = new Vector2(GetMaxWidth(), GetMaxHeight());
|
||||
|
||||
width = Mathf.Max(width, 1);
|
||||
height = Mathf.Max(height, 1);
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// If the reference size is significantly higher than the current actualWidth/Height and it is larger than 1440p dimensions, we reset the reference size every several frames
|
||||
// in editor to avoid issues if a large resolution was temporarily set.
|
||||
const int resetInterval = 100;
|
||||
if (((m_MaxWidths / (float)width) > 2.0f && m_MaxWidths > 2560) ||
|
||||
((m_MaxHeights / (float)height) > 2.0f && m_MaxHeights > 1440))
|
||||
{
|
||||
if (m_FramesSinceLastReset > resetInterval)
|
||||
{
|
||||
m_FramesSinceLastReset = 0;
|
||||
ResetReferenceSize(width, height);
|
||||
}
|
||||
m_FramesSinceLastReset++;
|
||||
}
|
||||
|
||||
// If some cameras is requesting the same res as the max res, we don't want to reset
|
||||
if (m_MaxWidths == width && m_MaxHeights == height)
|
||||
m_FramesSinceLastReset = 0;
|
||||
#endif
|
||||
|
||||
bool sizeChanged = width > GetMaxWidth() || height > GetMaxHeight() || reset;
|
||||
bool msaaSamplesChanged = (msaaSamples != m_ScaledRTCurrentMSAASamples);
|
||||
|
||||
if (sizeChanged || msaaSamplesChanged)
|
||||
{
|
||||
Resize(width, height, msaaSamples, sizeChanged, msaaSamplesChanged);
|
||||
}
|
||||
|
||||
m_RTHandleProperties.currentViewportSize = new Vector2Int(width, height);
|
||||
m_RTHandleProperties.currentRenderTargetSize = new Vector2Int(GetMaxWidth(), GetMaxHeight());
|
||||
|
||||
// If the currentViewportSize is 0, it mean we are the first frame of rendering (can happen when doing domain reload for example or for reflection probe)
|
||||
// in this case the scalePrevious below could be invalided. But some effect rely on having a correct value like TAA with the history buffer for the first frame.
|
||||
// to work around this, when we detect that size is 0, we setup previous size to current size.
|
||||
if (m_RTHandleProperties.previousViewportSize.x == 0)
|
||||
{
|
||||
m_RTHandleProperties.previousViewportSize = m_RTHandleProperties.currentViewportSize;
|
||||
m_RTHandleProperties.previousRenderTargetSize = m_RTHandleProperties.currentRenderTargetSize;
|
||||
lastFrameMaxSize = new Vector2(GetMaxWidth(), GetMaxHeight());
|
||||
}
|
||||
|
||||
if (DynamicResolutionHandler.instance.HardwareDynamicResIsEnabled() && m_HardwareDynamicResRequested)
|
||||
{
|
||||
Vector2Int maxSize = new Vector2Int(GetMaxWidth(), GetMaxHeight());
|
||||
// Making the final scale in 'drs' space, since the final scale must account for rounding pixel values.
|
||||
var scaledFinalViewport = DynamicResolutionHandler.instance.ApplyScalesOnSize(DynamicResolutionHandler.instance.finalViewport);
|
||||
var scaledMaxSize = DynamicResolutionHandler.instance.ApplyScalesOnSize(maxSize);
|
||||
float xScale = (float)scaledFinalViewport.x / (float)scaledMaxSize.x;
|
||||
float yScale = (float)scaledFinalViewport.y / (float)scaledMaxSize.y;
|
||||
m_RTHandleProperties.rtHandleScale = new Vector4(xScale, yScale, m_RTHandleProperties.rtHandleScale.x, m_RTHandleProperties.rtHandleScale.y);
|
||||
}
|
||||
else
|
||||
{
|
||||
Vector2 maxSize = new Vector2(GetMaxWidth(), GetMaxHeight());
|
||||
Vector2 scaleCurrent = m_RTHandleProperties.currentViewportSize / maxSize;
|
||||
Vector2 scalePrevious = m_RTHandleProperties.previousViewportSize / lastFrameMaxSize;
|
||||
m_RTHandleProperties.rtHandleScale = new Vector4(scaleCurrent.x, scaleCurrent.y, scalePrevious.x, scalePrevious.y);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enable or disable hardware dynamic resolution for the RTHandle System
|
||||
/// </summary>
|
||||
/// <param name="enableHWDynamicRes">State of hardware dynamic resolution.</param>
|
||||
public void SetHardwareDynamicResolutionState(bool enableHWDynamicRes)
|
||||
{
|
||||
if (enableHWDynamicRes != m_HardwareDynamicResRequested)
|
||||
{
|
||||
m_HardwareDynamicResRequested = enableHWDynamicRes;
|
||||
|
||||
Array.Resize(ref m_AutoSizedRTsArray, m_AutoSizedRTs.Count);
|
||||
m_AutoSizedRTs.CopyTo(m_AutoSizedRTsArray);
|
||||
for (int i = 0, c = m_AutoSizedRTsArray.Length; i < c; ++i)
|
||||
{
|
||||
var rth = m_AutoSizedRTsArray[i];
|
||||
|
||||
// Grab the render texture
|
||||
var renderTexture = rth.m_RT;
|
||||
if (renderTexture)
|
||||
{
|
||||
// Free the previous version
|
||||
renderTexture.Release();
|
||||
|
||||
renderTexture.useDynamicScale = m_HardwareDynamicResRequested && rth.m_EnableHWDynamicScale;
|
||||
|
||||
// Create the render texture
|
||||
renderTexture.Create();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal void SwitchResizeMode(RTHandle rth, ResizeMode mode)
|
||||
{
|
||||
// Don't do anything is scaling isn't enabled on this RT
|
||||
// TODO: useScaling should probably be moved to ResizeMode.Fixed or something
|
||||
if (!rth.useScaling)
|
||||
return;
|
||||
|
||||
switch (mode)
|
||||
{
|
||||
case ResizeMode.OnDemand:
|
||||
m_AutoSizedRTs.Remove(rth);
|
||||
m_ResizeOnDemandRTs.Add(rth);
|
||||
break;
|
||||
case ResizeMode.Auto:
|
||||
// Resize now so it is consistent with other auto resize RTHs
|
||||
if (m_ResizeOnDemandRTs.Contains(rth))
|
||||
DemandResize(rth);
|
||||
m_ResizeOnDemandRTs.Remove(rth);
|
||||
m_AutoSizedRTs.Add(rth);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void DemandResize(RTHandle rth)
|
||||
{
|
||||
Assert.IsTrue(m_ResizeOnDemandRTs.Contains(rth), "The RTHandle is not an resize on demand handle in this RTHandleSystem. Please call SwitchToResizeOnDemand(rth, true) before resizing on demand.");
|
||||
|
||||
// Grab the render texture
|
||||
var rt = rth.m_RT;
|
||||
rth.referenceSize = new Vector2Int(m_MaxWidths, m_MaxHeights);
|
||||
var scaledSize = rth.GetScaledSize(rth.referenceSize);
|
||||
scaledSize = Vector2Int.Max(Vector2Int.one, scaledSize);
|
||||
|
||||
// Did the size change?
|
||||
var sizeChanged = rt.width != scaledSize.x || rt.height != scaledSize.y;
|
||||
// If this is an MSAA texture, did the sample count change?
|
||||
var msaaSampleChanged = rth.m_EnableMSAA && rt.antiAliasing != (int)m_ScaledRTCurrentMSAASamples;
|
||||
|
||||
if (sizeChanged || msaaSampleChanged)
|
||||
{
|
||||
// Free this render texture
|
||||
rt.Release();
|
||||
|
||||
// Update the antialiasing count
|
||||
if (rth.m_EnableMSAA)
|
||||
rt.antiAliasing = (int)m_ScaledRTCurrentMSAASamples;
|
||||
|
||||
// Update the size
|
||||
rt.width = scaledSize.x;
|
||||
rt.height = scaledSize.y;
|
||||
|
||||
// Generate a new name
|
||||
rt.name = CoreUtils.GetRenderTargetAutoName(
|
||||
rt.width,
|
||||
rt.height,
|
||||
rt.volumeDepth,
|
||||
rt.graphicsFormat,
|
||||
rt.dimension,
|
||||
rth.m_Name,
|
||||
mips: rt.useMipMap,
|
||||
enableMSAA: rth.m_EnableMSAA,
|
||||
msaaSamples: m_ScaledRTCurrentMSAASamples,
|
||||
dynamicRes: rt.useDynamicScale
|
||||
);
|
||||
|
||||
// Create the new texture
|
||||
rt.Create();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the maximum allocated width of the RTHandle System.
|
||||
/// </summary>
|
||||
/// <returns>Maximum allocated width of the RTHandle System.</returns>
|
||||
public int GetMaxWidth() { return m_MaxWidths; }
|
||||
/// <summary>
|
||||
/// Returns the maximum allocated height of the RTHandle System.
|
||||
/// </summary>
|
||||
/// <returns>Maximum allocated height of the RTHandle System.</returns>
|
||||
public int GetMaxHeight() { return m_MaxHeights; }
|
||||
|
||||
void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
Array.Resize(ref m_AutoSizedRTsArray, m_AutoSizedRTs.Count);
|
||||
m_AutoSizedRTs.CopyTo(m_AutoSizedRTsArray);
|
||||
for (int i = 0, c = m_AutoSizedRTsArray.Length; i < c; ++i)
|
||||
{
|
||||
var rt = m_AutoSizedRTsArray[i];
|
||||
Release(rt);
|
||||
}
|
||||
m_AutoSizedRTs.Clear();
|
||||
|
||||
Array.Resize(ref m_AutoSizedRTsArray, m_ResizeOnDemandRTs.Count);
|
||||
m_ResizeOnDemandRTs.CopyTo(m_AutoSizedRTsArray);
|
||||
for (int i = 0, c = m_AutoSizedRTsArray.Length; i < c; ++i)
|
||||
{
|
||||
var rt = m_AutoSizedRTsArray[i];
|
||||
Release(rt);
|
||||
}
|
||||
m_ResizeOnDemandRTs.Clear();
|
||||
m_AutoSizedRTsArray = null;
|
||||
}
|
||||
}
|
||||
|
||||
void Resize(int width, int height, MSAASamples msaaSamples, bool sizeChanged, bool msaaSampleChanged)
|
||||
{
|
||||
m_MaxWidths = Math.Max(width, m_MaxWidths);
|
||||
m_MaxHeights = Math.Max(height, m_MaxHeights);
|
||||
m_ScaledRTCurrentMSAASamples = msaaSamples;
|
||||
|
||||
var maxSize = new Vector2Int(m_MaxWidths, m_MaxHeights);
|
||||
|
||||
Array.Resize(ref m_AutoSizedRTsArray, m_AutoSizedRTs.Count);
|
||||
m_AutoSizedRTs.CopyTo(m_AutoSizedRTsArray);
|
||||
|
||||
for (int i = 0, c = m_AutoSizedRTsArray.Length; i < c; ++i)
|
||||
{
|
||||
// Grab the RT Handle
|
||||
var rth = m_AutoSizedRTsArray[i];
|
||||
|
||||
// If we are only processing MSAA sample count change, make sure this RT is an MSAA one
|
||||
if (!sizeChanged && msaaSampleChanged && !rth.m_EnableMSAA)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Force its new reference size
|
||||
rth.referenceSize = maxSize;
|
||||
|
||||
// Grab the render texture
|
||||
var renderTexture = rth.m_RT;
|
||||
|
||||
// Free the previous version
|
||||
renderTexture.Release();
|
||||
|
||||
// Get the scaled size
|
||||
var scaledSize = rth.GetScaledSize(maxSize);
|
||||
|
||||
renderTexture.width = Mathf.Max(scaledSize.x, 1);
|
||||
renderTexture.height = Mathf.Max(scaledSize.y, 1);
|
||||
|
||||
// If this is a msaa texture, make sure to update its msaa count
|
||||
if (rth.m_EnableMSAA)
|
||||
{
|
||||
renderTexture.antiAliasing = (int)m_ScaledRTCurrentMSAASamples;
|
||||
}
|
||||
|
||||
// Regenerate the name
|
||||
renderTexture.name = CoreUtils.GetRenderTargetAutoName(renderTexture.width, renderTexture.height, renderTexture.volumeDepth, renderTexture.graphicsFormat, renderTexture.dimension, rth.m_Name, mips: renderTexture.useMipMap, enableMSAA: rth.m_EnableMSAA, msaaSamples: m_ScaledRTCurrentMSAASamples, dynamicRes: renderTexture.useDynamicScale);
|
||||
|
||||
// Create the render texture
|
||||
renderTexture.Create();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocate a new fixed sized RTHandle.
|
||||
/// </summary>
|
||||
/// <param name="width">With of the RTHandle.</param>
|
||||
/// <param name="height">Heigh of the RTHandle.</param>
|
||||
/// <param name="slices">Number of slices of the RTHandle.</param>
|
||||
/// <param name="depthBufferBits">Bit depths of a depth buffer.</param>
|
||||
/// <param name="colorFormat">GraphicsFormat of a color buffer.</param>
|
||||
/// <param name="filterMode">Filtering mode of the RTHandle.</param>
|
||||
/// <param name="wrapMode">Addressing mode of the RTHandle.</param>
|
||||
/// <param name="dimension">Texture dimension of the RTHandle.</param>
|
||||
/// <param name="enableRandomWrite">Set to true to enable UAV random read writes on the texture.</param>
|
||||
/// <param name="useMipMap">Set to true if the texture should have mipmaps.</param>
|
||||
/// <param name="autoGenerateMips">Set to true to automatically generate mipmaps.</param>
|
||||
/// <param name="isShadowMap">Set to true if the depth buffer should be used as a shadow map.</param>
|
||||
/// <param name="anisoLevel">Anisotropic filtering level.</param>
|
||||
/// <param name="mipMapBias">Bias applied to mipmaps during filtering.</param>
|
||||
/// <param name="msaaSamples">Number of MSAA samples for the RTHandle.</param>
|
||||
/// <param name="bindTextureMS">Set to true if the texture needs to be bound as a multisampled texture in the shader.</param>
|
||||
/// <param name="useDynamicScale">Set to true to use hardware dynamic scaling.</param>
|
||||
/// <param name="memoryless">Use this property to set the render texture memoryless modes.</param>
|
||||
/// <param name="name">Name of the RTHandle.</param>
|
||||
/// <returns></returns>
|
||||
public RTHandle Alloc(
|
||||
int width,
|
||||
int height,
|
||||
int slices = 1,
|
||||
DepthBits depthBufferBits = DepthBits.None,
|
||||
GraphicsFormat colorFormat = GraphicsFormat.R8G8B8A8_SRGB,
|
||||
FilterMode filterMode = FilterMode.Point,
|
||||
TextureWrapMode wrapMode = TextureWrapMode.Repeat,
|
||||
TextureDimension dimension = TextureDimension.Tex2D,
|
||||
bool enableRandomWrite = false,
|
||||
bool useMipMap = false,
|
||||
bool autoGenerateMips = true,
|
||||
bool isShadowMap = false,
|
||||
int anisoLevel = 1,
|
||||
float mipMapBias = 0f,
|
||||
MSAASamples msaaSamples = MSAASamples.None,
|
||||
bool bindTextureMS = false,
|
||||
bool useDynamicScale = false,
|
||||
RenderTextureMemoryless memoryless = RenderTextureMemoryless.None,
|
||||
string name = ""
|
||||
)
|
||||
{
|
||||
bool enableMSAA = msaaSamples != MSAASamples.None;
|
||||
if (!enableMSAA && bindTextureMS == true)
|
||||
{
|
||||
Debug.LogWarning("RTHandle allocated without MSAA but with bindMS set to true, forcing bindMS to false.");
|
||||
bindTextureMS = false;
|
||||
}
|
||||
|
||||
// We need to handle this in an explicit way since GraphicsFormat does not expose depth formats. TODO: Get rid of this branch once GraphicsFormat'll expose depth related formats
|
||||
RenderTexture rt;
|
||||
if (isShadowMap || depthBufferBits != DepthBits.None)
|
||||
{
|
||||
RenderTextureFormat format = isShadowMap ? RenderTextureFormat.Shadowmap : RenderTextureFormat.Depth;
|
||||
rt = new RenderTexture(width, height, (int)depthBufferBits, format, RenderTextureReadWrite.Linear)
|
||||
{
|
||||
hideFlags = HideFlags.HideAndDontSave,
|
||||
volumeDepth = slices,
|
||||
filterMode = filterMode,
|
||||
wrapMode = wrapMode,
|
||||
dimension = dimension,
|
||||
enableRandomWrite = enableRandomWrite,
|
||||
useMipMap = useMipMap,
|
||||
autoGenerateMips = autoGenerateMips,
|
||||
anisoLevel = anisoLevel,
|
||||
mipMapBias = mipMapBias,
|
||||
antiAliasing = (int)msaaSamples,
|
||||
bindTextureMS = bindTextureMS,
|
||||
useDynamicScale = m_HardwareDynamicResRequested && useDynamicScale,
|
||||
memorylessMode = memoryless,
|
||||
name = CoreUtils.GetRenderTargetAutoName(width, height, slices, format, name, mips: useMipMap, enableMSAA: enableMSAA, msaaSamples: msaaSamples)
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
rt = new RenderTexture(width, height, (int)depthBufferBits, colorFormat)
|
||||
{
|
||||
hideFlags = HideFlags.HideAndDontSave,
|
||||
volumeDepth = slices,
|
||||
filterMode = filterMode,
|
||||
wrapMode = wrapMode,
|
||||
dimension = dimension,
|
||||
enableRandomWrite = enableRandomWrite,
|
||||
useMipMap = useMipMap,
|
||||
autoGenerateMips = autoGenerateMips,
|
||||
anisoLevel = anisoLevel,
|
||||
mipMapBias = mipMapBias,
|
||||
antiAliasing = (int)msaaSamples,
|
||||
bindTextureMS = bindTextureMS,
|
||||
useDynamicScale = m_HardwareDynamicResRequested && useDynamicScale,
|
||||
memorylessMode = memoryless,
|
||||
name = CoreUtils.GetRenderTargetAutoName(width, height, slices, colorFormat, dimension, name, mips: useMipMap, enableMSAA: enableMSAA, msaaSamples: msaaSamples, dynamicRes: useDynamicScale)
|
||||
};
|
||||
}
|
||||
|
||||
rt.Create();
|
||||
|
||||
var newRT = new RTHandle(this);
|
||||
newRT.SetRenderTexture(rt);
|
||||
newRT.useScaling = false;
|
||||
newRT.m_EnableRandomWrite = enableRandomWrite;
|
||||
newRT.m_EnableMSAA = enableMSAA;
|
||||
newRT.m_EnableHWDynamicScale = useDynamicScale;
|
||||
newRT.m_Name = name;
|
||||
|
||||
newRT.referenceSize = new Vector2Int(width, height);
|
||||
|
||||
return newRT;
|
||||
}
|
||||
|
||||
// Next two methods are used to allocate RenderTexture that depend on the frame settings (resolution and msaa for now)
|
||||
// RenderTextures allocated this way are meant to be defined by a scale of camera resolution (full/half/quarter resolution for example).
|
||||
// The idea is that internally the system will scale up the size of all render texture so that it amortizes with time and not reallocate when a smaller size is required (which is what happens with TemporaryRTs).
|
||||
// Since MSAA cannot be changed on the fly for a given RenderTexture, a separate instance will be created if the user requires it. This instance will be the one used after the next call of SetReferenceSize if MSAA is required.
|
||||
|
||||
/// <summary>
|
||||
/// Allocate a new automatically sized RTHandle.
|
||||
/// </summary>
|
||||
/// <param name="scaleFactor">Constant scale for the RTHandle size computation.</param>
|
||||
/// <param name="slices">Number of slices of the RTHandle.</param>
|
||||
/// <param name="depthBufferBits">Bit depths of a depth buffer.</param>
|
||||
/// <param name="colorFormat">GraphicsFormat of a color buffer.</param>
|
||||
/// <param name="filterMode">Filtering mode of the RTHandle.</param>
|
||||
/// <param name="wrapMode">Addressing mode of the RTHandle.</param>
|
||||
/// <param name="dimension">Texture dimension of the RTHandle.</param>
|
||||
/// <param name="enableRandomWrite">Set to true to enable UAV random read writes on the texture.</param>
|
||||
/// <param name="useMipMap">Set to true if the texture should have mipmaps.</param>
|
||||
/// <param name="autoGenerateMips">Set to true to automatically generate mipmaps.</param>
|
||||
/// <param name="isShadowMap">Set to true if the depth buffer should be used as a shadow map.</param>
|
||||
/// <param name="anisoLevel">Anisotropic filtering level.</param>
|
||||
/// <param name="mipMapBias">Bias applied to mipmaps during filtering.</param>
|
||||
/// <param name="enableMSAA">Enable MSAA for this RTHandle.</param>
|
||||
/// <param name="bindTextureMS">Set to true if the texture needs to be bound as a multisampled texture in the shader.</param>
|
||||
/// <param name="useDynamicScale">Set to true to use hardware dynamic scaling.</param>
|
||||
/// <param name="memoryless">Use this property to set the render texture memoryless modes.</param>
|
||||
/// <param name="name">Name of the RTHandle.</param>
|
||||
/// <returns></returns>
|
||||
public RTHandle Alloc(
|
||||
Vector2 scaleFactor,
|
||||
int slices = 1,
|
||||
DepthBits depthBufferBits = DepthBits.None,
|
||||
GraphicsFormat colorFormat = GraphicsFormat.R8G8B8A8_SRGB,
|
||||
FilterMode filterMode = FilterMode.Point,
|
||||
TextureWrapMode wrapMode = TextureWrapMode.Repeat,
|
||||
TextureDimension dimension = TextureDimension.Tex2D,
|
||||
bool enableRandomWrite = false,
|
||||
bool useMipMap = false,
|
||||
bool autoGenerateMips = true,
|
||||
bool isShadowMap = false,
|
||||
int anisoLevel = 1,
|
||||
float mipMapBias = 0f,
|
||||
bool enableMSAA = false,
|
||||
bool bindTextureMS = false,
|
||||
bool useDynamicScale = false,
|
||||
RenderTextureMemoryless memoryless = RenderTextureMemoryless.None,
|
||||
string name = ""
|
||||
)
|
||||
{
|
||||
// If an MSAA target is requested, make sure the support was on
|
||||
if (enableMSAA)
|
||||
Debug.Assert(m_ScaledRTSupportsMSAA);
|
||||
|
||||
int width = Mathf.Max(Mathf.RoundToInt(scaleFactor.x * GetMaxWidth()), 1);
|
||||
int height = Mathf.Max(Mathf.RoundToInt(scaleFactor.y * GetMaxHeight()), 1);
|
||||
|
||||
var rth = AllocAutoSizedRenderTexture(width,
|
||||
height,
|
||||
slices,
|
||||
depthBufferBits,
|
||||
colorFormat,
|
||||
filterMode,
|
||||
wrapMode,
|
||||
dimension,
|
||||
enableRandomWrite,
|
||||
useMipMap,
|
||||
autoGenerateMips,
|
||||
isShadowMap,
|
||||
anisoLevel,
|
||||
mipMapBias,
|
||||
enableMSAA,
|
||||
bindTextureMS,
|
||||
useDynamicScale,
|
||||
memoryless,
|
||||
name
|
||||
);
|
||||
|
||||
rth.referenceSize = new Vector2Int(width, height);
|
||||
|
||||
rth.scaleFactor = scaleFactor;
|
||||
return rth;
|
||||
}
|
||||
|
||||
//
|
||||
// You can provide your own scaling function for advanced scaling schemes (e.g. scaling to
|
||||
// the next POT). The function takes a Vec2 as parameter that holds max width & height
|
||||
// values for the current manager context and returns a Vec2 of the final size in pixels.
|
||||
//
|
||||
// var rth = Alloc(
|
||||
// size => new Vector2Int(size.x / 2, size.y),
|
||||
// [...]
|
||||
// );
|
||||
//
|
||||
|
||||
/// <summary>
|
||||
/// Allocate a new automatically sized RTHandle.
|
||||
/// </summary>
|
||||
/// <param name="scaleFunc">Function used for the RTHandle size computation.</param>
|
||||
/// <param name="slices">Number of slices of the RTHandle.</param>
|
||||
/// <param name="depthBufferBits">Bit depths of a depth buffer.</param>
|
||||
/// <param name="colorFormat">GraphicsFormat of a color buffer.</param>
|
||||
/// <param name="filterMode">Filtering mode of the RTHandle.</param>
|
||||
/// <param name="wrapMode">Addressing mode of the RTHandle.</param>
|
||||
/// <param name="dimension">Texture dimension of the RTHandle.</param>
|
||||
/// <param name="enableRandomWrite">Set to true to enable UAV random read writes on the texture.</param>
|
||||
/// <param name="useMipMap">Set to true if the texture should have mipmaps.</param>
|
||||
/// <param name="autoGenerateMips">Set to true to automatically generate mipmaps.</param>
|
||||
/// <param name="isShadowMap">Set to true if the depth buffer should be used as a shadow map.</param>
|
||||
/// <param name="anisoLevel">Anisotropic filtering level.</param>
|
||||
/// <param name="mipMapBias">Bias applied to mipmaps during filtering.</param>
|
||||
/// <param name="enableMSAA">Enable MSAA for this RTHandle.</param>
|
||||
/// <param name="bindTextureMS">Set to true if the texture needs to be bound as a multisampled texture in the shader.</param>
|
||||
/// <param name="useDynamicScale">Set to true to use hardware dynamic scaling.</param>
|
||||
/// <param name="memoryless">Use this property to set the render texture memoryless modes.</param>
|
||||
/// <param name="name">Name of the RTHandle.</param>
|
||||
/// <returns></returns>
|
||||
public RTHandle Alloc(
|
||||
ScaleFunc scaleFunc,
|
||||
int slices = 1,
|
||||
DepthBits depthBufferBits = DepthBits.None,
|
||||
GraphicsFormat colorFormat = GraphicsFormat.R8G8B8A8_SRGB,
|
||||
FilterMode filterMode = FilterMode.Point,
|
||||
TextureWrapMode wrapMode = TextureWrapMode.Repeat,
|
||||
TextureDimension dimension = TextureDimension.Tex2D,
|
||||
bool enableRandomWrite = false,
|
||||
bool useMipMap = false,
|
||||
bool autoGenerateMips = true,
|
||||
bool isShadowMap = false,
|
||||
int anisoLevel = 1,
|
||||
float mipMapBias = 0f,
|
||||
bool enableMSAA = false,
|
||||
bool bindTextureMS = false,
|
||||
bool useDynamicScale = false,
|
||||
RenderTextureMemoryless memoryless = RenderTextureMemoryless.None,
|
||||
string name = ""
|
||||
)
|
||||
{
|
||||
var scaleFactor = scaleFunc(new Vector2Int(GetMaxWidth(), GetMaxHeight()));
|
||||
int width = Mathf.Max(scaleFactor.x, 1);
|
||||
int height = Mathf.Max(scaleFactor.y, 1);
|
||||
|
||||
var rth = AllocAutoSizedRenderTexture(width,
|
||||
height,
|
||||
slices,
|
||||
depthBufferBits,
|
||||
colorFormat,
|
||||
filterMode,
|
||||
wrapMode,
|
||||
dimension,
|
||||
enableRandomWrite,
|
||||
useMipMap,
|
||||
autoGenerateMips,
|
||||
isShadowMap,
|
||||
anisoLevel,
|
||||
mipMapBias,
|
||||
enableMSAA,
|
||||
bindTextureMS,
|
||||
useDynamicScale,
|
||||
memoryless,
|
||||
name
|
||||
);
|
||||
|
||||
rth.referenceSize = new Vector2Int(width, height);
|
||||
|
||||
rth.scaleFunc = scaleFunc;
|
||||
return rth;
|
||||
}
|
||||
|
||||
// Internal function
|
||||
RTHandle AllocAutoSizedRenderTexture(
|
||||
int width,
|
||||
int height,
|
||||
int slices,
|
||||
DepthBits depthBufferBits,
|
||||
GraphicsFormat colorFormat,
|
||||
FilterMode filterMode,
|
||||
TextureWrapMode wrapMode,
|
||||
TextureDimension dimension,
|
||||
bool enableRandomWrite,
|
||||
bool useMipMap,
|
||||
bool autoGenerateMips,
|
||||
bool isShadowMap,
|
||||
int anisoLevel,
|
||||
float mipMapBias,
|
||||
bool enableMSAA,
|
||||
bool bindTextureMS,
|
||||
bool useDynamicScale,
|
||||
RenderTextureMemoryless memoryless,
|
||||
string name
|
||||
)
|
||||
{
|
||||
// Here user made a mistake in setting up msaa/bindMS, hence the warning
|
||||
if (!enableMSAA && bindTextureMS == true)
|
||||
{
|
||||
Debug.LogWarning("RTHandle allocated without MSAA but with bindMS set to true, forcing bindMS to false.");
|
||||
bindTextureMS = false;
|
||||
}
|
||||
|
||||
bool allocForMSAA = m_ScaledRTSupportsMSAA ? enableMSAA : false;
|
||||
// Here we purposefully disable MSAA so we just force the bindMS param to false.
|
||||
if (!allocForMSAA)
|
||||
{
|
||||
bindTextureMS = false;
|
||||
}
|
||||
|
||||
// MSAA Does not support random read/write.
|
||||
bool UAV = enableRandomWrite;
|
||||
if (allocForMSAA && (UAV == true))
|
||||
{
|
||||
Debug.LogWarning("RTHandle that is MSAA-enabled cannot allocate MSAA RT with 'enableRandomWrite = true'.");
|
||||
UAV = false;
|
||||
}
|
||||
|
||||
int msaaSamples = allocForMSAA ? (int)m_ScaledRTCurrentMSAASamples : 1;
|
||||
|
||||
// We need to handle this in an explicit way since GraphicsFormat does not expose depth formats. TODO: Get rid of this branch once GraphicsFormat'll expose depth related formats
|
||||
RenderTexture rt;
|
||||
if (isShadowMap || depthBufferBits != DepthBits.None)
|
||||
{
|
||||
RenderTextureFormat format = isShadowMap ? RenderTextureFormat.Shadowmap : RenderTextureFormat.Depth;
|
||||
GraphicsFormat stencilFormat = isShadowMap ? GraphicsFormat.None : GraphicsFormat.R8_UInt;
|
||||
rt = new RenderTexture(width, height, (int)depthBufferBits, format, RenderTextureReadWrite.Linear)
|
||||
{
|
||||
hideFlags = HideFlags.HideAndDontSave,
|
||||
volumeDepth = slices,
|
||||
filterMode = filterMode,
|
||||
wrapMode = wrapMode,
|
||||
dimension = dimension,
|
||||
enableRandomWrite = UAV,
|
||||
useMipMap = useMipMap,
|
||||
autoGenerateMips = autoGenerateMips,
|
||||
anisoLevel = anisoLevel,
|
||||
mipMapBias = mipMapBias,
|
||||
antiAliasing = msaaSamples,
|
||||
bindTextureMS = bindTextureMS,
|
||||
useDynamicScale = m_HardwareDynamicResRequested && useDynamicScale,
|
||||
memorylessMode = memoryless,
|
||||
stencilFormat = stencilFormat,
|
||||
name = CoreUtils.GetRenderTargetAutoName(width, height, slices, colorFormat, dimension, name, mips: useMipMap, enableMSAA: allocForMSAA, msaaSamples: m_ScaledRTCurrentMSAASamples, dynamicRes: useDynamicScale)
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
rt = new RenderTexture(width, height, (int)depthBufferBits, colorFormat)
|
||||
{
|
||||
hideFlags = HideFlags.HideAndDontSave,
|
||||
volumeDepth = slices,
|
||||
filterMode = filterMode,
|
||||
wrapMode = wrapMode,
|
||||
dimension = dimension,
|
||||
enableRandomWrite = UAV,
|
||||
useMipMap = useMipMap,
|
||||
autoGenerateMips = autoGenerateMips,
|
||||
anisoLevel = anisoLevel,
|
||||
mipMapBias = mipMapBias,
|
||||
antiAliasing = msaaSamples,
|
||||
bindTextureMS = bindTextureMS,
|
||||
useDynamicScale = m_HardwareDynamicResRequested && useDynamicScale,
|
||||
memorylessMode = memoryless,
|
||||
name = CoreUtils.GetRenderTargetAutoName(width, height, slices, colorFormat, dimension, name, mips: useMipMap, enableMSAA: allocForMSAA, msaaSamples: m_ScaledRTCurrentMSAASamples, dynamicRes: useDynamicScale)
|
||||
};
|
||||
}
|
||||
|
||||
rt.Create();
|
||||
|
||||
var rth = new RTHandle(this);
|
||||
rth.SetRenderTexture(rt);
|
||||
rth.m_EnableMSAA = enableMSAA;
|
||||
rth.m_EnableRandomWrite = enableRandomWrite;
|
||||
rth.useScaling = true;
|
||||
rth.m_EnableHWDynamicScale = useDynamicScale;
|
||||
rth.m_Name = name;
|
||||
m_AutoSizedRTs.Add(rth);
|
||||
return rth;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocate a RTHandle from a regular RenderTexture.
|
||||
/// </summary>
|
||||
/// <param name="texture">Input texture</param>
|
||||
/// <returns>A new RTHandle referencing the input texture.</returns>
|
||||
public RTHandle Alloc(RenderTexture texture)
|
||||
{
|
||||
var rth = new RTHandle(this);
|
||||
rth.SetRenderTexture(texture);
|
||||
rth.m_EnableMSAA = false;
|
||||
rth.m_EnableRandomWrite = false;
|
||||
rth.useScaling = false;
|
||||
rth.m_EnableHWDynamicScale = false;
|
||||
rth.m_Name = texture.name;
|
||||
return rth;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocate a RTHandle from a regular Texture.
|
||||
/// </summary>
|
||||
/// <param name="texture">Input texture</param>
|
||||
/// <returns>A new RTHandle referencing the input texture.</returns>
|
||||
public RTHandle Alloc(Texture texture)
|
||||
{
|
||||
var rth = new RTHandle(this);
|
||||
rth.SetTexture(texture);
|
||||
rth.m_EnableMSAA = false;
|
||||
rth.m_EnableRandomWrite = false;
|
||||
rth.useScaling = false;
|
||||
rth.m_EnableHWDynamicScale = false;
|
||||
rth.m_Name = texture.name;
|
||||
return rth;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocate a RTHandle from a regular render target identifier.
|
||||
/// </summary>
|
||||
/// <param name="texture">Input render target identifier.</param>
|
||||
/// <returns>A new RTHandle referencing the input render target identifier.</returns>
|
||||
public RTHandle Alloc(RenderTargetIdentifier texture)
|
||||
{
|
||||
return Alloc(texture, "");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocate a RTHandle from a regular render target identifier.
|
||||
/// </summary>
|
||||
/// <param name="texture">Input render target identifier.</param>
|
||||
/// <param name="name">Name of the texture.</param>
|
||||
/// <returns>A new RTHandle referencing the input render target identifier.</returns>
|
||||
public RTHandle Alloc(RenderTargetIdentifier texture, string name)
|
||||
{
|
||||
var rth = new RTHandle(this);
|
||||
rth.SetTexture(texture);
|
||||
rth.m_EnableMSAA = false;
|
||||
rth.m_EnableRandomWrite = false;
|
||||
rth.useScaling = false;
|
||||
rth.m_EnableHWDynamicScale = false;
|
||||
rth.m_Name = name;
|
||||
return rth;
|
||||
}
|
||||
|
||||
private static RTHandle Alloc(RTHandle tex)
|
||||
{
|
||||
Debug.LogError("Allocation a RTHandle from another one is forbidden.");
|
||||
return null;
|
||||
}
|
||||
|
||||
internal string DumpRTInfo()
|
||||
{
|
||||
string result = "";
|
||||
Array.Resize(ref m_AutoSizedRTsArray, m_AutoSizedRTs.Count);
|
||||
m_AutoSizedRTs.CopyTo(m_AutoSizedRTsArray);
|
||||
for (int i = 0, c = m_AutoSizedRTsArray.Length; i < c; ++i)
|
||||
{
|
||||
var rt = m_AutoSizedRTsArray[i].rt;
|
||||
result = string.Format("{0}\nRT ({1})\t Format: {2} W: {3} H {4}\n", result, i, rt.format, rt.width, rt.height);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,339 @@
|
||||
using UnityEngine.Experimental.Rendering;
|
||||
|
||||
namespace UnityEngine.Rendering
|
||||
{
|
||||
/// <summary>
|
||||
/// Default instance of a RTHandleSystem
|
||||
/// </summary>
|
||||
public static class RTHandles
|
||||
{
|
||||
static RTHandleSystem s_DefaultInstance = new RTHandleSystem();
|
||||
|
||||
/// <summary>
|
||||
/// Maximum allocated width of the default RTHandle System
|
||||
/// </summary>
|
||||
public static int maxWidth { get { return s_DefaultInstance.GetMaxWidth(); } }
|
||||
/// <summary>
|
||||
/// Maximum allocated height of the default RTHandle System
|
||||
/// </summary>
|
||||
public static int maxHeight { get { return s_DefaultInstance.GetMaxHeight(); } }
|
||||
/// <summary>
|
||||
/// Current properties of the default RTHandle System
|
||||
/// </summary>
|
||||
public static RTHandleProperties rtHandleProperties { get { return s_DefaultInstance.rtHandleProperties; } }
|
||||
|
||||
/// <summary>
|
||||
/// Allocate a new fixed sized RTHandle with the default RTHandle System.
|
||||
/// </summary>
|
||||
/// <param name="width">With of the RTHandle.</param>
|
||||
/// <param name="height">Heigh of the RTHandle.</param>
|
||||
/// <param name="slices">Number of slices of the RTHandle.</param>
|
||||
/// <param name="depthBufferBits">Bit depths of a depth buffer.</param>
|
||||
/// <param name="colorFormat">GraphicsFormat of a color buffer.</param>
|
||||
/// <param name="filterMode">Filtering mode of the RTHandle.</param>
|
||||
/// <param name="wrapMode">Addressing mode of the RTHandle.</param>
|
||||
/// <param name="dimension">Texture dimension of the RTHandle.</param>
|
||||
/// <param name="enableRandomWrite">Set to true to enable UAV random read writes on the texture.</param>
|
||||
/// <param name="useMipMap">Set to true if the texture should have mipmaps.</param>
|
||||
/// <param name="autoGenerateMips">Set to true to automatically generate mipmaps.</param>
|
||||
/// <param name="isShadowMap">Set to true if the depth buffer should be used as a shadow map.</param>
|
||||
/// <param name="anisoLevel">Anisotropic filtering level.</param>
|
||||
/// <param name="mipMapBias">Bias applied to mipmaps during filtering.</param>
|
||||
/// <param name="msaaSamples">Number of MSAA samples for the RTHandle.</param>
|
||||
/// <param name="bindTextureMS">Set to true if the texture needs to be bound as a multisampled texture in the shader.</param>
|
||||
/// <param name="useDynamicScale">Set to true to use hardware dynamic scaling.</param>
|
||||
/// <param name="memoryless">Use this property to set the render texture memoryless modes.</param>
|
||||
/// <param name="name">Name of the RTHandle.</param>
|
||||
/// <returns></returns>
|
||||
public static RTHandle Alloc(
|
||||
int width,
|
||||
int height,
|
||||
int slices = 1,
|
||||
DepthBits depthBufferBits = DepthBits.None,
|
||||
GraphicsFormat colorFormat = GraphicsFormat.R8G8B8A8_SRGB,
|
||||
FilterMode filterMode = FilterMode.Point,
|
||||
TextureWrapMode wrapMode = TextureWrapMode.Repeat,
|
||||
TextureDimension dimension = TextureDimension.Tex2D,
|
||||
bool enableRandomWrite = false,
|
||||
bool useMipMap = false,
|
||||
bool autoGenerateMips = true,
|
||||
bool isShadowMap = false,
|
||||
int anisoLevel = 1,
|
||||
float mipMapBias = 0,
|
||||
MSAASamples msaaSamples = MSAASamples.None,
|
||||
bool bindTextureMS = false,
|
||||
bool useDynamicScale = false,
|
||||
RenderTextureMemoryless memoryless = RenderTextureMemoryless.None,
|
||||
string name = ""
|
||||
)
|
||||
{
|
||||
return s_DefaultInstance.Alloc(
|
||||
width,
|
||||
height,
|
||||
slices,
|
||||
depthBufferBits,
|
||||
colorFormat,
|
||||
filterMode,
|
||||
wrapMode,
|
||||
dimension,
|
||||
enableRandomWrite,
|
||||
useMipMap,
|
||||
autoGenerateMips,
|
||||
isShadowMap,
|
||||
anisoLevel,
|
||||
mipMapBias,
|
||||
msaaSamples,
|
||||
bindTextureMS,
|
||||
useDynamicScale,
|
||||
memoryless,
|
||||
name
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocate a new automatically sized RTHandle for the default RTHandle System.
|
||||
/// </summary>
|
||||
/// <param name="scaleFactor">Constant scale for the RTHandle size computation.</param>
|
||||
/// <param name="slices">Number of slices of the RTHandle.</param>
|
||||
/// <param name="depthBufferBits">Bit depths of a depth buffer.</param>
|
||||
/// <param name="colorFormat">GraphicsFormat of a color buffer.</param>
|
||||
/// <param name="filterMode">Filtering mode of the RTHandle.</param>
|
||||
/// <param name="wrapMode">Addressing mode of the RTHandle.</param>
|
||||
/// <param name="dimension">Texture dimension of the RTHandle.</param>
|
||||
/// <param name="enableRandomWrite">Set to true to enable UAV random read writes on the texture.</param>
|
||||
/// <param name="useMipMap">Set to true if the texture should have mipmaps.</param>
|
||||
/// <param name="autoGenerateMips">Set to true to automatically generate mipmaps.</param>
|
||||
/// <param name="isShadowMap">Set to true if the depth buffer should be used as a shadow map.</param>
|
||||
/// <param name="anisoLevel">Anisotropic filtering level.</param>
|
||||
/// <param name="mipMapBias">Bias applied to mipmaps during filtering.</param>
|
||||
/// <param name="enableMSAA">Enable MSAA for this RTHandle.</param>
|
||||
/// <param name="bindTextureMS">Set to true if the texture needs to be bound as a multisampled texture in the shader.</param>
|
||||
/// <param name="useDynamicScale">Set to true to use hardware dynamic scaling.</param>
|
||||
/// <param name="memoryless">Use this property to set the render texture memoryless modes.</param>
|
||||
/// <param name="name">Name of the RTHandle.</param>
|
||||
/// <returns></returns>
|
||||
public static RTHandle Alloc(
|
||||
Vector2 scaleFactor,
|
||||
int slices = 1,
|
||||
DepthBits depthBufferBits = DepthBits.None,
|
||||
GraphicsFormat colorFormat = GraphicsFormat.R8G8B8A8_SRGB,
|
||||
FilterMode filterMode = FilterMode.Point,
|
||||
TextureWrapMode wrapMode = TextureWrapMode.Repeat,
|
||||
TextureDimension dimension = TextureDimension.Tex2D,
|
||||
bool enableRandomWrite = false,
|
||||
bool useMipMap = false,
|
||||
bool autoGenerateMips = true,
|
||||
bool isShadowMap = false,
|
||||
int anisoLevel = 1,
|
||||
float mipMapBias = 0,
|
||||
bool enableMSAA = false,
|
||||
bool bindTextureMS = false,
|
||||
bool useDynamicScale = false,
|
||||
RenderTextureMemoryless memoryless = RenderTextureMemoryless.None,
|
||||
string name = ""
|
||||
)
|
||||
{
|
||||
return s_DefaultInstance.Alloc(
|
||||
scaleFactor,
|
||||
slices,
|
||||
depthBufferBits,
|
||||
colorFormat,
|
||||
filterMode,
|
||||
wrapMode,
|
||||
dimension,
|
||||
enableRandomWrite,
|
||||
useMipMap,
|
||||
autoGenerateMips,
|
||||
isShadowMap,
|
||||
anisoLevel,
|
||||
mipMapBias,
|
||||
enableMSAA,
|
||||
bindTextureMS,
|
||||
useDynamicScale,
|
||||
memoryless,
|
||||
name
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocate a new automatically sized RTHandle for the default RTHandle System.
|
||||
/// </summary>
|
||||
/// <param name="scaleFunc">Function used for the RTHandle size computation.</param>
|
||||
/// <param name="slices">Number of slices of the RTHandle.</param>
|
||||
/// <param name="depthBufferBits">Bit depths of a depth buffer.</param>
|
||||
/// <param name="colorFormat">GraphicsFormat of a color buffer.</param>
|
||||
/// <param name="filterMode">Filtering mode of the RTHandle.</param>
|
||||
/// <param name="wrapMode">Addressing mode of the RTHandle.</param>
|
||||
/// <param name="dimension">Texture dimension of the RTHandle.</param>
|
||||
/// <param name="enableRandomWrite">Set to true to enable UAV random read writes on the texture.</param>
|
||||
/// <param name="useMipMap">Set to true if the texture should have mipmaps.</param>
|
||||
/// <param name="autoGenerateMips">Set to true to automatically generate mipmaps.</param>
|
||||
/// <param name="isShadowMap">Set to true if the depth buffer should be used as a shadow map.</param>
|
||||
/// <param name="anisoLevel">Anisotropic filtering level.</param>
|
||||
/// <param name="mipMapBias">Bias applied to mipmaps during filtering.</param>
|
||||
/// <param name="enableMSAA">Enable MSAA for this RTHandle.</param>
|
||||
/// <param name="bindTextureMS">Set to true if the texture needs to be bound as a multisampled texture in the shader.</param>
|
||||
/// <param name="useDynamicScale">Set to true to use hardware dynamic scaling.</param>
|
||||
/// <param name="memoryless">Use this property to set the render texture memoryless modes.</param>
|
||||
/// <param name="name">Name of the RTHandle.</param>
|
||||
/// <returns></returns>
|
||||
public static RTHandle Alloc(
|
||||
ScaleFunc scaleFunc,
|
||||
int slices = 1,
|
||||
DepthBits depthBufferBits = DepthBits.None,
|
||||
GraphicsFormat colorFormat = GraphicsFormat.R8G8B8A8_SRGB,
|
||||
FilterMode filterMode = FilterMode.Point,
|
||||
TextureWrapMode wrapMode = TextureWrapMode.Repeat,
|
||||
TextureDimension dimension = TextureDimension.Tex2D,
|
||||
bool enableRandomWrite = false,
|
||||
bool useMipMap = false,
|
||||
bool autoGenerateMips = true,
|
||||
bool isShadowMap = false,
|
||||
int anisoLevel = 1,
|
||||
float mipMapBias = 0,
|
||||
bool enableMSAA = false,
|
||||
bool bindTextureMS = false,
|
||||
bool useDynamicScale = false,
|
||||
RenderTextureMemoryless memoryless = RenderTextureMemoryless.None,
|
||||
string name = ""
|
||||
)
|
||||
{
|
||||
return s_DefaultInstance.Alloc(
|
||||
scaleFunc,
|
||||
slices,
|
||||
depthBufferBits,
|
||||
colorFormat,
|
||||
filterMode,
|
||||
wrapMode,
|
||||
dimension,
|
||||
enableRandomWrite,
|
||||
useMipMap,
|
||||
autoGenerateMips,
|
||||
isShadowMap,
|
||||
anisoLevel,
|
||||
mipMapBias,
|
||||
enableMSAA,
|
||||
bindTextureMS,
|
||||
useDynamicScale,
|
||||
memoryless,
|
||||
name
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocate a RTHandle from a regular Texture for the default RTHandle system.
|
||||
/// </summary>
|
||||
/// <param name="tex">Input texture</param>
|
||||
/// <returns>A new RTHandle referencing the input texture.</returns>
|
||||
public static RTHandle Alloc(Texture tex)
|
||||
{
|
||||
return s_DefaultInstance.Alloc(tex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocate a RTHandle from a regular RenderTexture for the default RTHandle system.
|
||||
/// </summary>
|
||||
/// <param name="tex">Input texture</param>
|
||||
/// <returns>A new RTHandle referencing the input texture.</returns>
|
||||
public static RTHandle Alloc(RenderTexture tex)
|
||||
{
|
||||
return s_DefaultInstance.Alloc(tex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocate a RTHandle from a regular render target identifier for the default RTHandle system.
|
||||
/// </summary>
|
||||
/// <param name="tex">Input render target identifier.</param>
|
||||
/// <returns>A new RTHandle referencing the input render target identifier.</returns>
|
||||
public static RTHandle Alloc(RenderTargetIdentifier tex)
|
||||
{
|
||||
return s_DefaultInstance.Alloc(tex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocate a RTHandle from a regular render target identifier for the default RTHandle system.
|
||||
/// </summary>
|
||||
/// <param name="tex">Input render target identifier.</param>
|
||||
/// <param name="name">Name of the render target.</param>
|
||||
/// <returns>A new RTHandle referencing the input render target identifier.</returns>
|
||||
public static RTHandle Alloc(RenderTargetIdentifier tex, string name)
|
||||
{
|
||||
return s_DefaultInstance.Alloc(tex, name);
|
||||
}
|
||||
|
||||
private static RTHandle Alloc(RTHandle tex)
|
||||
{
|
||||
Debug.LogError("Allocation a RTHandle from another one is forbidden.");
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the default RTHandle system.
|
||||
/// </summary>
|
||||
/// <param name="width">Initial reference rendering width.</param>
|
||||
/// <param name="height">Initial reference rendering height.</param>
|
||||
/// <param name="scaledRTsupportsMSAA">Set to true if automatically scaled RTHandles should support MSAA</param>
|
||||
/// <param name="scaledRTMSAASamples">Number of MSAA samples for automatically scaled RTHandles.</param>
|
||||
public static void Initialize(
|
||||
int width,
|
||||
int height,
|
||||
bool scaledRTsupportsMSAA,
|
||||
MSAASamples scaledRTMSAASamples
|
||||
)
|
||||
{
|
||||
s_DefaultInstance.Initialize(
|
||||
width,
|
||||
height,
|
||||
scaledRTsupportsMSAA,
|
||||
scaledRTMSAASamples
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Release memory of a RTHandle from the default RTHandle System
|
||||
/// </summary>
|
||||
/// <param name="rth">RTHandle that should be released.</param>
|
||||
public static void Release(RTHandle rth)
|
||||
{
|
||||
s_DefaultInstance.Release(rth);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enable or disable hardware dynamic resolution for the default RTHandle System
|
||||
/// </summary>
|
||||
/// <param name="hwDynamicResRequested">State of hardware dynamic resolution.</param>
|
||||
public static void SetHardwareDynamicResolutionState(bool hwDynamicResRequested)
|
||||
{
|
||||
s_DefaultInstance.SetHardwareDynamicResolutionState(hwDynamicResRequested);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the reference rendering size for subsequent rendering for the default RTHandle System
|
||||
/// </summary>
|
||||
/// <param name="width">Reference rendering width for subsequent rendering.</param>
|
||||
/// <param name="height">Reference rendering height for subsequent rendering.</param>
|
||||
/// <param name="msaaSamples">Number of MSAA samples for multisampled textures for subsequent rendering.</param>
|
||||
public static void SetReferenceSize(
|
||||
int width,
|
||||
int height,
|
||||
MSAASamples msaaSamples
|
||||
)
|
||||
{
|
||||
s_DefaultInstance.SetReferenceSize(
|
||||
width,
|
||||
height,
|
||||
msaaSamples
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reset the reference size of the system and reallocate all textures.
|
||||
/// </summary>
|
||||
/// <param name="width">New width.</param>
|
||||
/// <param name="height">New height.</param>
|
||||
public static void ResetReferenceSize(int width, int height)
|
||||
{
|
||||
s_DefaultInstance.ResetReferenceSize(width, height);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,251 @@
|
||||
using UnityEngine.Experimental.Rendering;
|
||||
|
||||
namespace UnityEngine.Rendering
|
||||
{
|
||||
/// <summary>
|
||||
/// Utility class providing default textures compatible in any XR setup.
|
||||
/// </summary>
|
||||
public static class TextureXR
|
||||
{
|
||||
// Property set by XRSystem
|
||||
private static int m_MaxViews = 1;
|
||||
/// <summary>
|
||||
/// Maximum number of views handled by the XR system.
|
||||
/// </summary>
|
||||
public static int maxViews
|
||||
{
|
||||
set
|
||||
{
|
||||
m_MaxViews = value;
|
||||
}
|
||||
}
|
||||
|
||||
// Property accessed when allocating a render target
|
||||
/// <summary>
|
||||
/// Number of slices used by the XR system.
|
||||
/// </summary>
|
||||
public static int slices { get => m_MaxViews; }
|
||||
|
||||
// Must be in sync with shader define in TextureXR.hlsl
|
||||
/// <summary>
|
||||
/// Returns true if the XR system uses texture arrays.
|
||||
/// </summary>
|
||||
public static bool useTexArray
|
||||
{
|
||||
get
|
||||
{
|
||||
switch (SystemInfo.graphicsDeviceType)
|
||||
{
|
||||
case GraphicsDeviceType.Direct3D11:
|
||||
case GraphicsDeviceType.Direct3D12:
|
||||
case GraphicsDeviceType.PlayStation4:
|
||||
case GraphicsDeviceType.PlayStation5:
|
||||
case GraphicsDeviceType.Vulkan:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dimension of XR textures.
|
||||
/// </summary>
|
||||
public static TextureDimension dimension
|
||||
{
|
||||
get
|
||||
{
|
||||
// TEXTURE2D_X macros will now expand to TEXTURE2D or TEXTURE2D_ARRAY
|
||||
return useTexArray ? TextureDimension.Tex2DArray : TextureDimension.Tex2D;
|
||||
}
|
||||
}
|
||||
|
||||
// Need to keep both the Texture and the RTHandle in order to be able to track lifetime properly.
|
||||
static Texture m_BlackUIntTexture2DArray;
|
||||
static Texture m_BlackUIntTexture;
|
||||
static RTHandle m_BlackUIntTexture2DArrayRTH;
|
||||
static RTHandle m_BlackUIntTextureRTH;
|
||||
/// <summary>
|
||||
/// Default black unsigned integer texture.
|
||||
/// </summary>
|
||||
/// <returns>The default black unsigned integer texture.</returns>
|
||||
public static RTHandle GetBlackUIntTexture() { return useTexArray ? m_BlackUIntTexture2DArrayRTH : m_BlackUIntTextureRTH; }
|
||||
|
||||
static Texture2DArray m_ClearTexture2DArray;
|
||||
static Texture2D m_ClearTexture;
|
||||
static RTHandle m_ClearTexture2DArrayRTH;
|
||||
static RTHandle m_ClearTextureRTH;
|
||||
/// <summary>
|
||||
/// Default clear color (0, 0, 0, 1) texture.
|
||||
/// </summary>
|
||||
/// <returns>The default clear color texture.</returns>
|
||||
public static RTHandle GetClearTexture() { return useTexArray ? m_ClearTexture2DArrayRTH : m_ClearTextureRTH; }
|
||||
|
||||
static Texture2DArray m_MagentaTexture2DArray;
|
||||
static Texture2D m_MagentaTexture;
|
||||
static RTHandle m_MagentaTexture2DArrayRTH;
|
||||
static RTHandle m_MagentaTextureRTH;
|
||||
/// <summary>
|
||||
/// Default magenta texture.
|
||||
/// </summary>
|
||||
/// <returns>The default magenta texture.</returns>
|
||||
public static RTHandle GetMagentaTexture() { return useTexArray ? m_MagentaTexture2DArrayRTH : m_MagentaTextureRTH; }
|
||||
|
||||
static Texture2D m_BlackTexture;
|
||||
static Texture3D m_BlackTexture3D;
|
||||
static Texture2DArray m_BlackTexture2DArray;
|
||||
static RTHandle m_BlackTexture2DArrayRTH;
|
||||
static RTHandle m_BlackTextureRTH;
|
||||
static RTHandle m_BlackTexture3DRTH;
|
||||
/// <summary>
|
||||
/// Default black texture.
|
||||
/// </summary>
|
||||
/// <returns>The default black texture.</returns>
|
||||
public static RTHandle GetBlackTexture() { return useTexArray ? m_BlackTexture2DArrayRTH : m_BlackTextureRTH; }
|
||||
/// <summary>
|
||||
/// Default black texture array.
|
||||
/// </summary>
|
||||
/// <returns>The default black texture array.</returns>
|
||||
public static RTHandle GetBlackTextureArray() { return m_BlackTexture2DArrayRTH; }
|
||||
/// <summary>
|
||||
/// Default black texture 3D.
|
||||
/// </summary>
|
||||
/// <returns>The default black texture 3D.</returns>
|
||||
public static RTHandle GetBlackTexture3D() { return m_BlackTexture3DRTH; }
|
||||
|
||||
static Texture2DArray m_WhiteTexture2DArray;
|
||||
static RTHandle m_WhiteTexture2DArrayRTH;
|
||||
static RTHandle m_WhiteTextureRTH;
|
||||
/// <summary>
|
||||
/// Default white texture.
|
||||
/// </summary>
|
||||
/// <returns>The default white texture.</returns>
|
||||
public static RTHandle GetWhiteTexture() { return useTexArray ? m_WhiteTexture2DArrayRTH : m_WhiteTextureRTH; }
|
||||
|
||||
/// <summary>
|
||||
/// Initialize XR textures. Must be called at least once.
|
||||
/// </summary>
|
||||
/// <param name="cmd">Command Buffer used to initialize textures.</param>
|
||||
/// <param name="clearR32_UIntShader">Compute shader used to intitialize unsigned integer textures.</param>
|
||||
public static void Initialize(CommandBuffer cmd, ComputeShader clearR32_UIntShader)
|
||||
{
|
||||
if (m_BlackUIntTexture2DArray == null) // We assume that everything is invalid if one is invalid.
|
||||
{
|
||||
// Black UINT
|
||||
RTHandles.Release(m_BlackUIntTexture2DArrayRTH);
|
||||
m_BlackUIntTexture2DArray = CreateBlackUIntTextureArray(cmd, clearR32_UIntShader);
|
||||
m_BlackUIntTexture2DArrayRTH = RTHandles.Alloc(m_BlackUIntTexture2DArray);
|
||||
RTHandles.Release(m_BlackUIntTextureRTH);
|
||||
m_BlackUIntTexture = CreateBlackUintTexture(cmd, clearR32_UIntShader);
|
||||
m_BlackUIntTextureRTH = RTHandles.Alloc(m_BlackUIntTexture);
|
||||
|
||||
// Clear
|
||||
RTHandles.Release(m_ClearTextureRTH);
|
||||
m_ClearTexture = new Texture2D(1, 1, TextureFormat.ARGB32, false) { name = "Clear Texture" };
|
||||
m_ClearTexture.SetPixel(0, 0, Color.clear);
|
||||
m_ClearTexture.Apply();
|
||||
m_ClearTextureRTH = RTHandles.Alloc(m_ClearTexture);
|
||||
RTHandles.Release(m_ClearTexture2DArrayRTH);
|
||||
m_ClearTexture2DArray = CreateTexture2DArrayFromTexture2D(m_ClearTexture, "Clear Texture2DArray");
|
||||
m_ClearTexture2DArrayRTH = RTHandles.Alloc(m_ClearTexture2DArray);
|
||||
|
||||
// Magenta
|
||||
RTHandles.Release(m_MagentaTextureRTH);
|
||||
m_MagentaTexture = new Texture2D(1, 1, TextureFormat.ARGB32, false) { name = "Magenta Texture" };
|
||||
m_MagentaTexture.SetPixel(0, 0, Color.magenta);
|
||||
m_MagentaTexture.Apply();
|
||||
m_MagentaTextureRTH = RTHandles.Alloc(m_MagentaTexture);
|
||||
RTHandles.Release(m_MagentaTexture2DArrayRTH);
|
||||
m_MagentaTexture2DArray = CreateTexture2DArrayFromTexture2D(m_MagentaTexture, "Magenta Texture2DArray");
|
||||
m_MagentaTexture2DArrayRTH = RTHandles.Alloc(m_MagentaTexture2DArray);
|
||||
|
||||
// Black
|
||||
RTHandles.Release(m_BlackTextureRTH);
|
||||
m_BlackTexture = new Texture2D(1, 1, GraphicsFormat.R8G8B8A8_SRGB, TextureCreationFlags.None) { name = "Black Texture" };
|
||||
m_BlackTexture.SetPixel(0, 0, Color.black);
|
||||
m_BlackTexture.Apply();
|
||||
m_BlackTextureRTH = RTHandles.Alloc(m_BlackTexture);
|
||||
RTHandles.Release(m_BlackTexture2DArrayRTH);
|
||||
m_BlackTexture2DArray = CreateTexture2DArrayFromTexture2D(m_BlackTexture, "Black Texture2DArray");
|
||||
m_BlackTexture2DArrayRTH = RTHandles.Alloc(m_BlackTexture2DArray);
|
||||
RTHandles.Release(m_BlackTexture3DRTH);
|
||||
m_BlackTexture3D = CreateBlackTexture3D("Black Texture3D");
|
||||
m_BlackTexture3DRTH = RTHandles.Alloc(m_BlackTexture3D);
|
||||
|
||||
// White
|
||||
RTHandles.Release(m_WhiteTextureRTH);
|
||||
m_WhiteTextureRTH = RTHandles.Alloc(Texture2D.whiteTexture);
|
||||
RTHandles.Release(m_WhiteTexture2DArrayRTH);
|
||||
m_WhiteTexture2DArray = CreateTexture2DArrayFromTexture2D(Texture2D.whiteTexture, "White Texture2DArray");
|
||||
m_WhiteTexture2DArrayRTH = RTHandles.Alloc(m_WhiteTexture2DArray);
|
||||
}
|
||||
}
|
||||
|
||||
static Texture2DArray CreateTexture2DArrayFromTexture2D(Texture2D source, string name)
|
||||
{
|
||||
Texture2DArray texArray = new Texture2DArray(source.width, source.height, slices, source.format, false) { name = name };
|
||||
for (int i = 0; i < slices; ++i)
|
||||
Graphics.CopyTexture(source, 0, 0, texArray, i, 0);
|
||||
|
||||
return texArray;
|
||||
}
|
||||
|
||||
static Texture CreateBlackUIntTextureArray(CommandBuffer cmd, ComputeShader clearR32_UIntShader)
|
||||
{
|
||||
RenderTexture blackUIntTexture2DArray = new RenderTexture(1, 1, 0, GraphicsFormat.R32_UInt)
|
||||
{
|
||||
dimension = TextureDimension.Tex2DArray,
|
||||
volumeDepth = slices,
|
||||
useMipMap = false,
|
||||
autoGenerateMips = false,
|
||||
enableRandomWrite = true,
|
||||
name = "Black UInt Texture Array"
|
||||
};
|
||||
|
||||
blackUIntTexture2DArray.Create();
|
||||
|
||||
// Workaround because we currently can't create a Texture2DArray using an R32_UInt format
|
||||
// So we create a R32_UInt RenderTarget and clear it using a compute shader, because we can't
|
||||
// Clear this type of target on metal devices (output type nor compatible: float4 vs uint)
|
||||
int kernel = clearR32_UIntShader.FindKernel("ClearUIntTextureArray");
|
||||
cmd.SetComputeTextureParam(clearR32_UIntShader, kernel, "_TargetArray", blackUIntTexture2DArray);
|
||||
cmd.DispatchCompute(clearR32_UIntShader, kernel, 1, 1, slices);
|
||||
|
||||
return blackUIntTexture2DArray as Texture;
|
||||
}
|
||||
|
||||
static Texture CreateBlackUintTexture(CommandBuffer cmd, ComputeShader clearR32_UIntShader)
|
||||
{
|
||||
RenderTexture blackUIntTexture2D = new RenderTexture(1, 1, 0, GraphicsFormat.R32_UInt)
|
||||
{
|
||||
dimension = TextureDimension.Tex2D,
|
||||
volumeDepth = slices,
|
||||
useMipMap = false,
|
||||
autoGenerateMips = false,
|
||||
enableRandomWrite = true,
|
||||
name = "Black UInt Texture Array"
|
||||
};
|
||||
|
||||
blackUIntTexture2D.Create();
|
||||
|
||||
// Workaround because we currently can't create a Texture2DArray using an R32_UInt format
|
||||
// So we create a R32_UInt RenderTarget and clear it using a compute shader, because we can't
|
||||
// Clear this type of target on metal devices (output type nor compatible: float4 vs uint)
|
||||
int kernel = clearR32_UIntShader.FindKernel("ClearUIntTexture");
|
||||
cmd.SetComputeTextureParam(clearR32_UIntShader, kernel, "_Target", blackUIntTexture2D);
|
||||
cmd.DispatchCompute(clearR32_UIntShader, kernel, 1, 1, slices);
|
||||
|
||||
return blackUIntTexture2D as Texture;
|
||||
}
|
||||
|
||||
static Texture3D CreateBlackTexture3D(string name)
|
||||
{
|
||||
Texture3D texture3D = new Texture3D(width: 1, height: 1, depth: 1, textureFormat: TextureFormat.RGBA32, mipChain: false);
|
||||
texture3D.name = name;
|
||||
texture3D.SetPixel(0, 0, 0, Color.black, 0);
|
||||
texture3D.Apply(updateMipmaps: false);
|
||||
return texture3D;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user