-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
GraphicsBuffer.bindings.cs
485 lines (395 loc) · 20.7 KB
/
GraphicsBuffer.bindings.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine.Scripting;
using UnityEngine.Bindings;
namespace UnityEngine
{
[NativeHeader("Runtime/GfxDevice/GfxDeviceTypes.h")]
[NativeClass("GfxBufferID")]
[StructLayout(LayoutKind.Sequential)]
public readonly struct GraphicsBufferHandle : IEquatable<GraphicsBufferHandle>
{
public readonly UInt32 value;
public override int GetHashCode()
{
return value.GetHashCode();
}
public override bool Equals(object obj)
{
if (obj is GraphicsBufferHandle)
{
return Equals((GraphicsBufferHandle)obj);
}
return false;
}
public bool Equals(GraphicsBufferHandle other)
{
return value == other.value;
}
public int CompareTo(GraphicsBufferHandle other)
{
return value.CompareTo(other.value);
}
public static bool operator ==(GraphicsBufferHandle a, GraphicsBufferHandle b)
{
return a.Equals(b);
}
public static bool operator !=(GraphicsBufferHandle a, GraphicsBufferHandle b)
{
return !a.Equals(b);
}
}
// Note: both C# ComputeBuffer and GraphicsBuffer
// use C++ GraphicsBuffer as an implementation object.
[UsedByNativeCode]
[NativeHeader("Runtime/Shaders/GraphicsBuffer.h")]
[NativeHeader("Runtime/Export/Graphics/GraphicsBuffer.bindings.h")]
public sealed class GraphicsBuffer : IDisposable
{
#pragma warning disable 414
internal IntPtr m_Ptr;
#pragma warning restore 414
AtomicSafetyHandle m_Safety;
[Flags]
public enum Target
{
Vertex = 1 << 0,
Index = 1 << 1,
CopySource = 1 << 2,
CopyDestination = 1 << 3,
Structured = 1 << 4,
Raw = 1 << 5,
Append = 1 << 6,
Counter = 1 << 7,
IndirectArguments = 1 << 8,
Constant = 1 << 9,
}
[Flags]
public enum UsageFlags
{
None = 0,
LockBufferForWrite = 1 << 0,
}
public struct IndirectDrawArgs
{
public const int size = 16;
public uint vertexCountPerInstance {get; set;}
public uint instanceCount {get; set;}
public uint startVertex {get; set;}
public uint startInstance {get; set;}
}
public struct IndirectDrawIndexedArgs
{
public const int size = 20;
public uint indexCountPerInstance {get; set;}
public uint instanceCount {get; set;}
public uint startIndex {get; set;}
public uint baseVertexIndex {get; set;}
public uint startInstance {get; set;}
}
~GraphicsBuffer()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
void Dispose(bool disposing)
{
if (disposing)
{
// Release native resources
DestroyBuffer(this);
}
else if (m_Ptr != IntPtr.Zero)
{
Debug.LogWarning($"GarbageCollector disposing of GraphicsBuffer allocated in {GetFileName()} at line {GetLineNumber()}. Please use GraphicsBuffer.Release() or .Dispose() to manually release the buffer.");
}
m_Ptr = IntPtr.Zero;
}
static bool RequiresCompute(Target target)
{
var requiresComputeMask = Target.Structured | Target.Raw | Target.Append | Target.Counter | Target.IndirectArguments;
return (target & requiresComputeMask) != 0;
}
static bool IsVertexIndexOrCopyOnly(Target target)
{
var mask = Target.Vertex | Target.Index | Target.CopySource | Target.CopyDestination;
return (target & mask) == target;
}
[FreeFunction("GraphicsBuffer_Bindings::InitBuffer")]
static extern IntPtr InitBuffer(Target target, UsageFlags usageFlags, int count, int stride);
[FreeFunction("GraphicsBuffer_Bindings::DestroyBuffer")]
static extern void DestroyBuffer(GraphicsBuffer buf);
// Create a Graphics Buffer.
public GraphicsBuffer(Target target, int count, int stride)
{
// If usage is not explicitly specified, then it defaults to:
// - Pure vertex or index buffer: "sub-updates",
// - All other targets: "immutable".
// It does not make sense, except that at some point C# classes behavior was
// like that (C# GraphicsBuffer, when it only supported Vertex/Index buffers always used
// sub-updates; and C# ComputeBuffer, which only suppported non-Vertex/Index always used
// immutable). And now we can't change this default, ever :/
bool onlyVBIB = (target & (Target.Index | Target.Vertex)) == target;
var usageFlags = onlyVBIB ? UsageFlags.LockBufferForWrite : UsageFlags.None;
InternalInitialization(target, usageFlags, count, stride);
SaveCallstack(2);
}
// Create a Graphics Buffer.
public GraphicsBuffer(Target target, UsageFlags usageFlags, int count, int stride)
{
InternalInitialization(target, usageFlags, count, stride);
SaveCallstack(2);
}
private void InternalInitialization(Target target, UsageFlags usageFlags, int count, int stride)
{
if (RequiresCompute(target) && !SystemInfo.supportsComputeShaders)
{
throw new ArgumentException("Attempting to create a graphics buffer that requires compute shader support, but compute shaders are not supported on this platform. Target: " + target);
}
if (count <= 0)
{
throw new ArgumentException("Attempting to create a zero length graphics buffer", "count");
}
if (stride <= 0)
{
throw new ArgumentException("Attempting to create a graphics buffer with a negative or null stride", "stride");
}
if ((target & Target.Index) != 0 && stride != 2 && stride != 4)
{
throw new ArgumentException("Attempting to create an index buffer with an invalid stride: " + stride, "stride");
}
else if (!IsVertexIndexOrCopyOnly(target) && stride % 4 != 0)
{
throw new ArgumentException("Stride must be a multiple of 4 unless the buffer is only used as a vertex buffer and/or index buffer ", "stride");
}
var bufferSize = (long)count * stride;
var maxBufferSize = SystemInfo.maxGraphicsBufferSize;
if (bufferSize > maxBufferSize)
{
throw new ArgumentException($"The total size of the graphics buffer ({bufferSize} bytes) exceeds the maximum buffer size. Maximum supported buffer size: {maxBufferSize} bytes.");
}
m_Ptr = InitBuffer(target, usageFlags, count, stride);
}
// Release a Graphics Buffer.
public void Release()
{
Dispose();
}
[FreeFunction("GraphicsBuffer_Bindings::IsValidBuffer")]
static extern bool IsValidBuffer(GraphicsBuffer buf);
public bool IsValid()
{
return m_Ptr != IntPtr.Zero && IsValidBuffer(this);
}
// Number of elements in the buffer (RO).
public extern int count { get; }
// Size of one element in the buffer (RO).
public extern int stride { get; }
public extern Target target { get; }
[FreeFunction(Name = "GraphicsBuffer_Bindings::GetUsageFlags", HasExplicitThis = true)]
extern UsageFlags GetUsageFlags();
public UsageFlags usageFlags { get { return GetUsageFlags(); } }
public extern GraphicsBufferHandle bufferHandle { get; }
// Set buffer data.
[System.Security.SecuritySafeCritical] // due to Marshal.SizeOf
public void SetData(System.Array data)
{
if (data == null)
throw new ArgumentNullException("data");
if (!UnsafeUtility.IsArrayBlittable(data))
{
throw new ArgumentException(
string.Format("Array passed to GraphicsBuffer.SetData(array) must be blittable.\n{0}",
UnsafeUtility.GetReasonForArrayNonBlittable(data)));
}
InternalSetData(data, 0, 0, data.Length, UnsafeUtility.SizeOf(data.GetType().GetElementType()));
}
// Set buffer data.
[System.Security.SecuritySafeCritical] // due to Marshal.SizeOf
public void SetData<T>(List<T> data) where T : struct
{
if (data == null)
throw new ArgumentNullException("data");
if (!UnsafeUtility.IsGenericListBlittable<T>())
{
throw new ArgumentException(
string.Format("List<{0}> passed to GraphicsBuffer.SetData(List<>) must be blittable.\n{1}",
typeof(T), UnsafeUtility.GetReasonForGenericListNonBlittable<T>()));
}
InternalSetData(NoAllocHelpers.ExtractArrayFromList(data), 0, 0, NoAllocHelpers.SafeLength(data), Marshal.SizeOf(typeof(T)));
}
[System.Security.SecuritySafeCritical] // due to Marshal.SizeOf
unsafe public void SetData<T>(NativeArray<T> data) where T : struct
{
// Note: no IsBlittable test here because it's already done at NativeArray creation time
InternalSetNativeData((IntPtr)data.GetUnsafeReadOnlyPtr(), 0, 0, data.Length, UnsafeUtility.SizeOf<T>());
}
// Set partial buffer data
[System.Security.SecuritySafeCritical] // due to Marshal.SizeOf
public void SetData(System.Array data, int managedBufferStartIndex, int graphicsBufferStartIndex, int count)
{
if (data == null)
throw new ArgumentNullException("data");
if (!UnsafeUtility.IsArrayBlittable(data))
{
throw new ArgumentException(
string.Format("Array passed to GraphicsBuffer.SetData(array) must be blittable.\n{0}",
UnsafeUtility.GetReasonForArrayNonBlittable(data)));
}
if (managedBufferStartIndex < 0 || graphicsBufferStartIndex < 0 || count < 0 || managedBufferStartIndex + count > data.Length)
throw new ArgumentOutOfRangeException(String.Format("Bad indices/count arguments (managedBufferStartIndex:{0} graphicsBufferStartIndex:{1} count:{2})", managedBufferStartIndex, graphicsBufferStartIndex, count));
InternalSetData(data, managedBufferStartIndex, graphicsBufferStartIndex, count, Marshal.SizeOf(data.GetType().GetElementType()));
}
// Set partial buffer data
[System.Security.SecuritySafeCritical] // due to Marshal.SizeOf
public void SetData<T>(List<T> data, int managedBufferStartIndex, int graphicsBufferStartIndex, int count) where T : struct
{
if (data == null)
throw new ArgumentNullException("data");
if (!UnsafeUtility.IsGenericListBlittable<T>())
{
throw new ArgumentException(
string.Format("List<{0}> passed to GraphicsBuffer.SetData(List<>) must be blittable.\n{1}",
typeof(T), UnsafeUtility.GetReasonForGenericListNonBlittable<T>()));
}
if (managedBufferStartIndex < 0 || graphicsBufferStartIndex < 0 || count < 0 || managedBufferStartIndex + count > data.Count)
throw new ArgumentOutOfRangeException(String.Format("Bad indices/count arguments (managedBufferStartIndex:{0} graphicsBufferStartIndex:{1} count:{2})", managedBufferStartIndex, graphicsBufferStartIndex, count));
InternalSetData(NoAllocHelpers.ExtractArrayFromList(data), managedBufferStartIndex, graphicsBufferStartIndex, count, Marshal.SizeOf(typeof(T)));
}
[System.Security.SecuritySafeCritical] // due to Marshal.SizeOf
public unsafe void SetData<T>(NativeArray<T> data, int nativeBufferStartIndex, int graphicsBufferStartIndex, int count) where T : struct
{
// Note: no IsBlittable test here because it's already done at NativeArray creation time
if (nativeBufferStartIndex < 0 || graphicsBufferStartIndex < 0 || count < 0 || nativeBufferStartIndex + count > data.Length)
throw new ArgumentOutOfRangeException(String.Format("Bad indices/count arguments (nativeBufferStartIndex:{0} graphicsBufferStartIndex:{1} count:{2})", nativeBufferStartIndex, graphicsBufferStartIndex, count));
InternalSetNativeData((IntPtr)data.GetUnsafeReadOnlyPtr(), nativeBufferStartIndex, graphicsBufferStartIndex, count, UnsafeUtility.SizeOf<T>());
}
[System.Security.SecurityCritical] // to prevent accidentally making this public in the future
[FreeFunction(Name = "GraphicsBuffer_Bindings::InternalSetNativeData", HasExplicitThis = true, ThrowsException = true)]
extern private void InternalSetNativeData(IntPtr data, int nativeBufferStartIndex, int graphicsBufferStartIndex, int count, int elemSize);
[System.Security.SecurityCritical] // to prevent accidentally making this public in the future
[FreeFunction(Name = "GraphicsBuffer_Bindings::InternalSetData", HasExplicitThis = true, ThrowsException = true)]
extern private void InternalSetData(System.Array data, int managedBufferStartIndex, int graphicsBufferStartIndex, int count, int elemSize);
[System.Security.SecurityCritical] // due to Marshal.SizeOf
public void GetData(System.Array data)
{
if (data == null)
throw new ArgumentNullException("data");
if (!UnsafeUtility.IsArrayBlittable(data))
{
throw new ArgumentException(
string.Format("Array passed to GraphicsBuffer.GetData(array) must be blittable.\n{0}",
UnsafeUtility.GetReasonForArrayNonBlittable(data)));
}
InternalGetData(data, 0, 0, data.Length, Marshal.SizeOf(data.GetType().GetElementType()));
}
// Read partial buffer data.
[System.Security.SecurityCritical] // due to Marshal.SizeOf
public void GetData(System.Array data, int managedBufferStartIndex, int computeBufferStartIndex, int count)
{
if (data == null)
throw new ArgumentNullException("data");
if (!UnsafeUtility.IsArrayBlittable(data))
{
throw new ArgumentException(
string.Format("Array passed to GraphicsBuffer.GetData(array) must be blittable.\n{0}",
UnsafeUtility.GetReasonForArrayNonBlittable(data)));
}
if (managedBufferStartIndex < 0 || computeBufferStartIndex < 0 || count < 0 || managedBufferStartIndex + count > data.Length)
throw new ArgumentOutOfRangeException(String.Format("Bad indices/count argument (managedBufferStartIndex:{0} computeBufferStartIndex:{1} count:{2})", managedBufferStartIndex, computeBufferStartIndex, count));
InternalGetData(data, managedBufferStartIndex, computeBufferStartIndex, count, Marshal.SizeOf(data.GetType().GetElementType()));
}
[System.Security.SecurityCritical] // to prevent accidentally making this public in the future
[FreeFunction(Name = "GraphicsBuffer_Bindings::InternalGetData", HasExplicitThis = true, ThrowsException = true)]
extern private void InternalGetData(System.Array data, int managedBufferStartIndex, int computeBufferStartIndex, int count, int elemSize);
[FreeFunction(Name = "GraphicsBuffer_Bindings::InternalGetNativeBufferPtr", HasExplicitThis = true)]
extern public IntPtr GetNativeBufferPtr();
extern unsafe private void* BeginBufferWrite(int offset = 0, int size = 0);
public NativeArray<T> LockBufferForWrite<T>(int bufferStartIndex, int count) where T : struct
{
if (!IsValid())
throw new InvalidOperationException("LockBufferForWrite requires a valid GraphicsBuffer");
if ((usageFlags & UsageFlags.LockBufferForWrite) == 0)
throw new InvalidOperationException("GraphicsBuffer must be created with usage mode UsageFlage.LockBufferForWrite to use LockBufferForWrite");
var elementSize = UnsafeUtility.SizeOf<T>();
if (bufferStartIndex < 0 || count < 0 || (bufferStartIndex + count) * elementSize > this.count * this.stride)
throw new ArgumentOutOfRangeException(String.Format("Bad indices/count arguments (bufferStartIndex:{0} count:{1} elementSize:{2}, this.count:{3}, this.stride{4})", bufferStartIndex, count, elementSize, this.count, this.stride));
NativeArray<T> array;
unsafe
{
var ptr = BeginBufferWrite(bufferStartIndex * elementSize, count * elementSize);
array = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<T>((void*)ptr, count, Allocator.Invalid);
}
m_Safety = AtomicSafetyHandle.Create();
AtomicSafetyHandle.SetAllowSecondaryVersionWriting(m_Safety, true);
NativeArrayUnsafeUtility.SetAtomicSafetyHandle(ref array, m_Safety);
return array;
}
extern private void EndBufferWrite(int bytesWritten = 0);
public void UnlockBufferAfterWrite<T>(int countWritten) where T : struct
{
try
{
AtomicSafetyHandle.CheckExistsAndThrow(m_Safety);
AtomicSafetyHandle.Release(m_Safety);
}
catch (Exception e)
{
throw new InvalidOperationException("GraphicsBuffer.UnlockBufferAfterWrite was called without matching GraphicsBuffer.LockBufferForWrite", e);
}
if (countWritten < 0)
throw new ArgumentOutOfRangeException(String.Format("Bad indices/count arguments (countWritten:{0})", countWritten));
var elementSize = UnsafeUtility.SizeOf<T>();
EndBufferWrite(countWritten * elementSize);
}
public string name { set => SetName(value); }
[FreeFunction(Name = "GraphicsBuffer_Bindings::SetName", HasExplicitThis = true)]
extern void SetName(string name);
// Set counter value of append/consume buffer.
extern public void SetCounterValue(uint counterValue);
// Copy counter value of append/consume buffer into another buffer.
[FreeFunction(Name = "GraphicsBuffer_Bindings::CopyCount")]
extern private static void CopyCountCC(ComputeBuffer src, ComputeBuffer dst, int dstOffsetBytes);
[FreeFunction(Name = "GraphicsBuffer_Bindings::CopyCount")]
extern private static void CopyCountGC(GraphicsBuffer src, ComputeBuffer dst, int dstOffsetBytes);
[FreeFunction(Name = "GraphicsBuffer_Bindings::CopyCount")]
extern private static void CopyCountCG(ComputeBuffer src, GraphicsBuffer dst, int dstOffsetBytes);
[FreeFunction(Name = "GraphicsBuffer_Bindings::CopyCount")]
extern private static void CopyCountGG(GraphicsBuffer src, GraphicsBuffer dst, int dstOffsetBytes);
public static void CopyCount(ComputeBuffer src, ComputeBuffer dst, int dstOffsetBytes)
{
CopyCountCC(src, dst, dstOffsetBytes);
}
public static void CopyCount(GraphicsBuffer src, ComputeBuffer dst, int dstOffsetBytes)
{
CopyCountGC(src, dst, dstOffsetBytes);
}
public static void CopyCount(ComputeBuffer src, GraphicsBuffer dst, int dstOffsetBytes)
{
CopyCountCG(src, dst, dstOffsetBytes);
}
public static void CopyCount(GraphicsBuffer src, GraphicsBuffer dst, int dstOffsetBytes)
{
CopyCountGG(src, dst, dstOffsetBytes);
}
[ThreadSafe] extern string GetFileName();
[ThreadSafe] extern int GetLineNumber();
internal void SaveCallstack(int stackDepth)
{
var frame = new StackFrame(stackDepth, true);
SetAllocationData(frame.GetFileName(), frame.GetFileLineNumber());
}
extern void SetAllocationData(string fileName, int lineNumber);
}
}