RenderTargetUtility.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Rendering;
  4. namespace EPOOutline
  5. {
  6. public static class RenderTargetUtility
  7. {
  8. private static RenderTextureFormat? hdrFormat = null;
  9. public static int GetDepthSliceForEye(StereoTargetEyeMask mask)
  10. {
  11. switch (mask)
  12. {
  13. case StereoTargetEyeMask.Both:
  14. return -1;
  15. case StereoTargetEyeMask.None:
  16. case StereoTargetEyeMask.Left:
  17. return 0;
  18. case StereoTargetEyeMask.Right:
  19. return 1;
  20. default:
  21. throw new ArgumentException("Unknown mode");
  22. }
  23. }
  24. public struct RenderTextureInfo
  25. {
  26. public readonly RenderTextureDescriptor Descriptor;
  27. public readonly FilterMode FilterMode;
  28. public RenderTextureInfo(RenderTextureDescriptor descriptor, FilterMode filterMode)
  29. {
  30. Descriptor = descriptor;
  31. FilterMode = filterMode;
  32. }
  33. }
  34. public static RenderTargetIdentifier ComposeTarget(OutlineParameters parameters, RenderTargetIdentifier target)
  35. {
  36. return new RenderTargetIdentifier(target, 0, CubemapFace.Unknown, GetDepthSliceForEye(parameters.EyeMask));
  37. }
  38. public static bool IsUsingVR(OutlineParameters parameters)
  39. {
  40. return UnityEngine.XR.XRSettings.enabled
  41. && !parameters.IsEditorCamera
  42. && parameters.EyeMask != StereoTargetEyeMask.None;
  43. }
  44. public static RenderTextureInfo GetTargetInfo(OutlineParameters parameters, int width, int height, int depthBuffer, bool forceNoAA, bool noFiltering)
  45. {
  46. var filterType = noFiltering ? FilterMode.Point : FilterMode.Bilinear;
  47. var rtFormat = parameters.UseHDR ? GetHDRFormat() : RenderTextureFormat.ARGB32;
  48. if (IsUsingVR(parameters))
  49. {
  50. var descriptor = UnityEngine.XR.XRSettings.eyeTextureDesc;
  51. descriptor.colorFormat = rtFormat;
  52. descriptor.width = width;
  53. descriptor.height = height;
  54. descriptor.depthBufferBits = depthBuffer;
  55. descriptor.msaaSamples = forceNoAA ? 1 : Mathf.Max(parameters.Antialiasing, 1);
  56. var eyesCount = parameters.EyeMask == StereoTargetEyeMask.Both ? VRTextureUsage.TwoEyes : VRTextureUsage.OneEye;
  57. descriptor.vrUsage = eyesCount;
  58. return new RenderTextureInfo(descriptor, filterType);
  59. }
  60. else
  61. {
  62. var descriptor = new RenderTextureDescriptor(width, height, rtFormat, depthBuffer);
  63. descriptor.dimension = TextureDimension.Tex2D;
  64. descriptor.msaaSamples = forceNoAA ? 1 : Mathf.Max(parameters.Antialiasing, 1);
  65. return new RenderTextureInfo(descriptor, filterType);
  66. }
  67. }
  68. public static void GetTemporaryRT(OutlineParameters parameters, int id, int width, int height, int depthBuffer, bool clear, bool forceNoAA, bool noFiltering)
  69. {
  70. var info = GetTargetInfo(parameters, width, height, depthBuffer, forceNoAA, noFiltering);
  71. parameters.Buffer.GetTemporaryRT(id, info.Descriptor, info.FilterMode);
  72. parameters.Buffer.SetRenderTarget(RenderTargetUtility.ComposeTarget(parameters, id));
  73. if (clear)
  74. parameters.Buffer.ClearRenderTarget(true, true, Color.clear);
  75. }
  76. private static RenderTextureFormat GetHDRFormat()
  77. {
  78. if (hdrFormat.HasValue)
  79. return hdrFormat.Value;
  80. if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBHalf))
  81. hdrFormat = RenderTextureFormat.ARGBHalf;
  82. else if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBFloat))
  83. hdrFormat = RenderTextureFormat.ARGBFloat;
  84. else if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGB64))
  85. hdrFormat = RenderTextureFormat.ARGB64;
  86. else
  87. hdrFormat = RenderTextureFormat.ARGB32;
  88. return hdrFormat.Value;
  89. }
  90. }
  91. }