A game about forced loneliness, made by TACStudios
1using System;
2using System.Runtime.InteropServices;
3using Unity.Collections;
4
5namespace UnityEngine.Rendering.RenderGraphModule.NativeRenderPassCompiler
6{
7 /// <summary>
8 /// A fixed-size array that can contain up to maximum render target attachment amount of items.
9 /// </summary>
10 /// <typeparam name="DataType">The type of data to store in the array.</typeparam>
11 [StructLayout(LayoutKind.Sequential)]
12 public struct FixedAttachmentArray<DataType> where DataType : unmanaged
13 {
14 /// <summary>
15 /// Returns an empty array.
16 /// </summary>
17 public static FixedAttachmentArray<DataType> Empty = new FixedAttachmentArray<DataType>(0);
18
19 /// <summary>
20 /// The maximum number of elements that can be stored in the array.
21 /// </summary>
22 public const int MaxAttachments = 8;
23
24 /// This is a fixed size struct that emulates itself as an array
25 /// similar to how Unity.Math emulates fixed size arrays
26 private DataType a0, a1, a2, a3, a4, a5, a6, a7;
27 private int activeAttachments;
28
29 /// <summary>
30 /// Created an new array with the specified number of attachments.
31 /// </summary>
32 /// <param name="numAttachments">Number of attachments to consider valid.</param>
33 /// <exception cref="ArgumentException">Thrown if the amount of elements is less than 0 or more than MaxAttachments</exception>
34 public FixedAttachmentArray(int numAttachments)
35 {
36#if DEVELOPMENT_BUILD || UNITY_EDITOR
37 if (numAttachments < 0 || numAttachments > MaxAttachments)
38 {
39 throw new ArgumentException($"FixedAttachmentArray - numAttachments must be in range of [0, {MaxAttachments}[");
40 }
41#endif
42 a0 = a1 = a2 = a3 = a4 = a5 = a6 = a7 = new DataType();
43 activeAttachments = numAttachments;
44 }
45
46 /// <summary>
47 /// Intialize the FixedAttachmentArray by copying data from the passed in c# array.
48 /// </summary>
49 /// <param name="attachments">The C# array from which to copy the elements.</param>
50 public FixedAttachmentArray(DataType[] attachments) : this(attachments.Length)
51 {
52 for (int i = 0; i < activeAttachments; ++i)
53 {
54 this[i] = attachments[i];
55 }
56 }
57
58 /// <summary>
59 /// Intialize the FixedAttachmentArray by copying data from the passed in native array.
60 /// </summary>
61 /// <param name="attachments">The native array from which to copy the elements.</param>
62 public FixedAttachmentArray(NativeArray<DataType> attachments) : this(attachments.Length)
63 {
64 for (int i = 0; i < activeAttachments; ++i)
65 {
66 this[i] = attachments[i];
67 }
68 }
69
70 /// <summary>
71 /// Number of attachments in the array alway less or equal than MaxAttachments
72 /// </summary>
73 public int size
74 {
75 get
76 {
77 return activeAttachments;
78 }
79 }
80
81 /// <summary>
82 /// Clear the array.
83 /// </summary>
84 public void Clear()
85 {
86 activeAttachments = 0;
87 }
88
89 /// <summary>
90 /// Add an element tot the array.
91 /// </summary>
92 /// <param name="data">Element to add</param>
93 /// <returns>Returns the index where the item was added.</returns>
94 /// <exception cref="IndexOutOfRangeException">If the maximum amount of elements (MaxAttachments) is reached.</exception>
95 public int Add(in DataType data)
96 {
97#if DEVELOPMENT_BUILD || UNITY_EDITOR
98 if ((uint)activeAttachments >= MaxAttachments)
99 throw new IndexOutOfRangeException($"A FixedAttachmentArray can only contain {MaxAttachments} items.");
100#endif
101 int index = activeAttachments;
102 unsafe
103 {
104 fixed (FixedAttachmentArray<DataType>* self = &this)
105 {
106 DataType* array = (DataType*)self;
107 array[index] = data;
108 }
109 }
110 activeAttachments++;
111 return index;
112 }
113
114 /// <summary>
115 /// Get the element at the specified index in the array.
116 /// </summary>
117 /// <param name="index">Index of the element.</param>
118 /// <value>The value of the element.</value>
119 /// <exception cref="IndexOutOfRangeException">If the index is outside the valid range.</exception>
120 public ref DataType this[int index]
121 {
122 get
123 {
124#if DEVELOPMENT_BUILD || UNITY_EDITOR
125 if ((uint)index >= MaxAttachments)
126 throw new IndexOutOfRangeException($"FixedAttachmentArray - index must be in range of [0, {MaxAttachments}[");
127 if ((uint)index >= activeAttachments)
128 throw new IndexOutOfRangeException($"FixedAttachmentArray - index must be in range of [0, {activeAttachments}[");
129#endif
130 unsafe
131 {
132 fixed (FixedAttachmentArray<DataType>* self = &this)
133 {
134 DataType* array = (DataType*)self;
135 return ref array[index];
136 }
137 }
138 }
139 }
140 }
141}