RenderTargetHandle.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using UnityEngine.Scripting.APIUpdating;
  2. namespace UnityEngine.Rendering.Universal
  3. {
  4. // RenderTargetHandle can be thought of as a kind of ShaderProperty string hash
  5. public struct RenderTargetHandle
  6. {
  7. public int id { set; get; }
  8. private RenderTargetIdentifier rtid { set; get; }
  9. public static readonly RenderTargetHandle CameraTarget = new RenderTargetHandle { id = -1 };
  10. public RenderTargetHandle(RenderTargetIdentifier renderTargetIdentifier)
  11. {
  12. id = -2;
  13. rtid = renderTargetIdentifier;
  14. }
  15. internal static RenderTargetHandle GetCameraTarget(XRPass xr)
  16. {
  17. #if ENABLE_VR && ENABLE_XR_MODULE
  18. if (xr.enabled)
  19. return new RenderTargetHandle(xr.renderTarget);
  20. #endif
  21. return CameraTarget;
  22. }
  23. public void Init(string shaderProperty)
  24. {
  25. // Shader.PropertyToID returns what is internally referred to as a "ShaderLab::FastPropertyName".
  26. // It is a value coming from an internal global std::map<char*,int> that converts shader property strings into unique integer handles (that are faster to work with).
  27. id = Shader.PropertyToID(shaderProperty);
  28. }
  29. public void Init(RenderTargetIdentifier renderTargetIdentifier)
  30. {
  31. id = -2;
  32. rtid = renderTargetIdentifier;
  33. }
  34. public RenderTargetIdentifier Identifier()
  35. {
  36. if (id == -1)
  37. {
  38. return BuiltinRenderTextureType.CameraTarget;
  39. }
  40. if (id == -2)
  41. {
  42. return rtid;
  43. }
  44. return new RenderTargetIdentifier(id, 0, CubemapFace.Unknown, -1);
  45. }
  46. public bool HasInternalRenderTargetId()
  47. {
  48. return id == -2;
  49. }
  50. public bool Equals(RenderTargetHandle other)
  51. {
  52. if (id == -2 || other.id == -2)
  53. return Identifier() == other.Identifier();
  54. return id == other.id;
  55. }
  56. public override bool Equals(object obj)
  57. {
  58. if (ReferenceEquals(null, obj)) return false;
  59. return obj is RenderTargetHandle && Equals((RenderTargetHandle)obj);
  60. }
  61. public override int GetHashCode()
  62. {
  63. return id;
  64. }
  65. public static bool operator ==(RenderTargetHandle c1, RenderTargetHandle c2)
  66. {
  67. return c1.Equals(c2);
  68. }
  69. public static bool operator !=(RenderTargetHandle c1, RenderTargetHandle c2)
  70. {
  71. return !c1.Equals(c2);
  72. }
  73. }
  74. }