No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RenderGraphResourceBuffer.cs 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System.Diagnostics;
  2. using UnityEngine.Scripting.APIUpdating;
  3. namespace UnityEngine.Rendering.RenderGraphModule
  4. {
  5. /// <summary>
  6. /// Graphics Buffer resource handle.
  7. /// </summary>
  8. [DebuggerDisplay("Buffer ({handle.index})")]
  9. [MovedFrom(true, "UnityEngine.Experimental.Rendering.RenderGraphModule", "UnityEngine.Rendering.RenderGraphModule")]
  10. public struct BufferHandle
  11. {
  12. // Minor Warning: This calls the zeroing constructor this means that the embedded ResourceHandle struct will also be zero-ed
  13. // which then means ResourceHandle.type will be set to zero == Texture. As this is an "invalid" bufferhandle I guess setting it
  14. // to type texture just makes it even more properly invalid and not a big issue. But something to keep in mind for tooling/logging.
  15. private static BufferHandle s_NullHandle = new BufferHandle();
  16. /// <summary>
  17. /// Returns a null graphics buffer handle
  18. /// </summary>
  19. /// <value>A null graphics buffer handle.</value>
  20. public static BufferHandle nullHandle { get { return s_NullHandle; } }
  21. internal ResourceHandle handle;
  22. internal BufferHandle(in ResourceHandle h) { handle = h; }
  23. internal BufferHandle(int handle, bool shared = false) { this.handle = new ResourceHandle(handle, RenderGraphResourceType.Buffer, shared); }
  24. /// <summary>
  25. /// Cast to GraphicsBuffer
  26. /// </summary>
  27. /// <param name="buffer">Input BufferHandle</param>
  28. /// <returns>Resource as a Graphics Buffer.</returns>
  29. public static implicit operator GraphicsBuffer(BufferHandle buffer) => buffer.IsValid() ? RenderGraphResourceRegistry.current.GetBuffer(buffer) : null;
  30. /// <summary>
  31. /// Return true if the handle is valid.
  32. /// </summary>
  33. /// <returns>True if the handle is valid.</returns>
  34. public bool IsValid() => handle.IsValid();
  35. }
  36. /// <summary>
  37. /// Descriptor used to create graphics buffer resources
  38. /// </summary>
  39. public struct BufferDesc
  40. {
  41. ///<summary>Number of elements in the buffer..</summary>
  42. public int count;
  43. ///<summary>Size of one element in the buffer. Has to match size of buffer type in the shader.</summary>
  44. public int stride;
  45. /// <summary>Graphics Buffer name.</summary>
  46. public string name;
  47. /// <summary>The intended usage of a GraphicsBuffer.</summary>
  48. public GraphicsBuffer.Target target;
  49. /// <summary>The intended update mode of a GraphicsBuffer.</summary>
  50. public GraphicsBuffer.UsageFlags usageFlags;
  51. /// <summary>
  52. /// BufferDesc constructor.
  53. /// </summary>
  54. /// <param name="count">Number of elements in the buffer.</param>
  55. /// <param name="stride">Size of one element in the buffer.</param>
  56. public BufferDesc(int count, int stride)
  57. : this()
  58. {
  59. this.count = count;
  60. this.stride = stride;
  61. this.target = GraphicsBuffer.Target.Structured;
  62. this.usageFlags = GraphicsBuffer.UsageFlags.None;
  63. }
  64. /// <summary>
  65. /// BufferDesc constructor.
  66. /// </summary>
  67. /// <param name="count">Number of elements in the buffer.</param>
  68. /// <param name="stride">Size of one element in the buffer.</param>
  69. /// <param name="target">Type of the buffer.</param>
  70. public BufferDesc(int count, int stride, GraphicsBuffer.Target target)
  71. : this()
  72. {
  73. this.count = count;
  74. this.stride = stride;
  75. this.target = target;
  76. this.usageFlags = GraphicsBuffer.UsageFlags.None;
  77. }
  78. /// <summary>
  79. /// Hash function
  80. /// </summary>
  81. /// <returns>The texture descriptor hash.</returns>
  82. public override int GetHashCode()
  83. {
  84. int hashCode = 17;
  85. hashCode = hashCode * 23 + count;
  86. hashCode = hashCode * 23 + stride;
  87. hashCode = hashCode * 23 + (int)target;
  88. hashCode = hashCode * 23 + (int)usageFlags;
  89. return hashCode;
  90. }
  91. }
  92. [DebuggerDisplay("BufferResource ({desc.name})")]
  93. class BufferResource : RenderGraphResource<BufferDesc, GraphicsBuffer>
  94. {
  95. public override string GetName()
  96. {
  97. if (imported)
  98. return "ImportedGraphicsBuffer"; // No getter for graphics buffer name.
  99. else
  100. return desc.name;
  101. }
  102. public override int GetDescHashCode() { return desc.GetHashCode(); }
  103. public override void CreateGraphicsResource()
  104. {
  105. var name = GetName();
  106. graphicsResource = new GraphicsBuffer(desc.target, desc.usageFlags, desc.count, desc.stride);
  107. #if DEVELOPMENT_BUILD || UNITY_EDITOR
  108. graphicsResource.name = name == "" ? $"RenderGraphBuffer_{desc.count}_{desc.stride}_{desc.target}" : name;
  109. #endif
  110. }
  111. public override void UpdateGraphicsResource()
  112. {
  113. if (graphicsResource != null)
  114. graphicsResource.name = GetName();
  115. }
  116. public override void ReleaseGraphicsResource()
  117. {
  118. if (graphicsResource != null)
  119. graphicsResource.Release();
  120. base.ReleaseGraphicsResource();
  121. }
  122. public override void LogCreation(RenderGraphLogger logger)
  123. {
  124. logger.LogLine($"Created GraphicsBuffer: {desc.name}");
  125. }
  126. public override void LogRelease(RenderGraphLogger logger)
  127. {
  128. logger.LogLine($"Released GraphicsBuffer: {desc.name}");
  129. }
  130. }
  131. class BufferPool : RenderGraphResourcePool<GraphicsBuffer>
  132. {
  133. protected override void ReleaseInternalResource(GraphicsBuffer res)
  134. {
  135. res.Release();
  136. }
  137. protected override string GetResourceName(in GraphicsBuffer res)
  138. {
  139. return "GraphicsBufferNameNotAvailable"; // GraphicsBuffer.name is a setter only :(
  140. }
  141. protected override long GetResourceSize(in GraphicsBuffer res)
  142. {
  143. return res.count * res.stride;
  144. }
  145. override protected string GetResourceTypeName()
  146. {
  147. return "GraphicsBuffer";
  148. }
  149. override protected int GetSortIndex(GraphicsBuffer res)
  150. {
  151. return res.GetHashCode();
  152. }
  153. }
  154. }