using System;
using UnityEngine.Scripting.APIUpdating;
namespace UnityEngine.Rendering.Universal
{
///
/// Class for render target handles in URP.
/// Deprecated in favor of RTHandle.
///
// RenderTargetHandle can be thought of as a kind of ShaderProperty string hash
[Obsolete("Deprecated in favor of RTHandle", true)]
public struct RenderTargetHandle
{
///
/// The ID of the handle for the handle.
///
public int id { set; get; }
///
/// The render target ID for the handle.
///
private RenderTargetIdentifier rtid { set; get; }
///
/// The render target handle for the Camera target.
///
public static readonly RenderTargetHandle CameraTarget = new RenderTargetHandle { id = -1 };
///
/// Constructor for a render target handle.
///
/// The render target ID for the new handle.
public RenderTargetHandle(RenderTargetIdentifier renderTargetIdentifier)
{
id = -2;
rtid = renderTargetIdentifier;
}
///
/// Constructor for a render target handle.
///
/// The rt handle for the new handle.
public RenderTargetHandle(RTHandle rtHandle)
{
if (rtHandle.nameID == BuiltinRenderTextureType.CameraTarget)
id = -1;
else if (rtHandle.name.Length == 0)
id = -2;
else
id = Shader.PropertyToID(rtHandle.name);
rtid = rtHandle.nameID;
if (rtHandle.rt != null && id != rtid)
id = -2;
}
internal static RenderTargetHandle GetCameraTarget(ref CameraData cameraData)
{
#if ENABLE_VR && ENABLE_XR_MODULE
if (cameraData.xr.enabled)
return new RenderTargetHandle(cameraData.xr.renderTarget);
#endif
return CameraTarget;
}
///
/// Initializes the ID for the handle.
///
/// The shader property to initialize with.
public void Init(string shaderProperty)
{
// Shader.PropertyToID returns what is internally referred to as a "ShaderLab::FastPropertyName".
// It is a value coming from an internal global std::map that converts shader property strings into unique integer handles (that are faster to work with).
id = Shader.PropertyToID(shaderProperty);
}
///
/// Initializes the render target ID for the handle.
///
/// The render target ID to initialize with.
public void Init(RenderTargetIdentifier renderTargetIdentifier)
{
id = -2;
rtid = renderTargetIdentifier;
}
///
/// The render target ID for this render target handle.
///
/// The render target ID for this render target handle.
public RenderTargetIdentifier Identifier()
{
if (id == -1)
{
return BuiltinRenderTextureType.CameraTarget;
}
if (id == -2)
{
return rtid;
}
return new RenderTargetIdentifier(id, 0, CubemapFace.Unknown, -1);
}
///
/// Does this handle have internal render target ID?
///
/// True if it has internal render target ID.
public bool HasInternalRenderTargetId()
{
return id == -2;
}
///
/// Equality check with another render target handle.
///
/// Other render target handle to compare with.
/// True if the handles have the same ID, otherwise false.
public bool Equals(RenderTargetHandle other)
{
if (id == -2 || other.id == -2)
return Identifier() == other.Identifier();
return id == other.id;
}
///
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
return obj is RenderTargetHandle && Equals((RenderTargetHandle)obj);
}
///
public override int GetHashCode()
{
return id;
}
///
/// Equality check between two render target handles.
///
/// First handle for the check.
/// Second handle for the check.
/// True if the handles have the same ID, otherwise false.
public static bool operator ==(RenderTargetHandle c1, RenderTargetHandle c2)
{
return c1.Equals(c2);
}
///
/// Equality check between two render target handles.
///
/// First handle for the check.
/// Second handle for the check.
/// True if the handles do not have the same ID, otherwise false.
public static bool operator !=(RenderTargetHandle c1, RenderTargetHandle c2)
{
return !c1.Equals(c2);
}
}
}