-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGpuIsolate.cs
More file actions
469 lines (417 loc) · 14.9 KB
/
GpuIsolate.cs
File metadata and controls
469 lines (417 loc) · 14.9 KB
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
using System.Buffers;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using Coplt.Dropping;
using Coplt.Graphics.Native;
using Coplt.Graphics.States;
namespace Coplt.Graphics.Core;
public ref struct GpuIsolateConfig(ref FGpuIsolateConfig @ref)
{
internal ref FGpuIsolateConfig m_ref = ref @ref;
public ref bool MultiThreadRecord => ref Unsafe.As<B8, bool>(ref m_ref.MultiThreadRecord);
public ref bool UseSplitBarrier => ref Unsafe.As<B8, bool>(ref m_ref.UseSplitBarrier);
}
[Dropping(Unmanaged = true)]
public sealed unsafe partial class GpuIsolate : DeviceChild
{
#region Fields
internal FGpuIsolateData* m_data;
#endregion
#region Props
public new FGpuIsolate* Ptr => (FGpuIsolate*)m_ptr;
public ref readonly FGpuIsolateData Data => ref *m_data;
public GpuIsolateConfig Config => new(ref *Data.Config);
public FrameId FrameId => new(Data.FrameId);
#endregion
#region Ctor
internal GpuIsolate(FGpuIsolate* ptr, FGpuIsolateData* data, string? name, GpuDevice device)
: base((FGpuObject*)ptr, name, device)
{
m_data = data;
}
#endregion
#region Drop
[Drop]
private void Drop()
{
m_data = null;
}
#endregion
#region Rent Reocrds
public GpuRecord RentRecord()
{
GpuRecord? record = null;
RentRecords(new Span<GpuRecord?>(ref record));
return record!;
}
public void RentRecords(Span<GpuRecord?> records)
{
if (records.Length == 0) return;
var arr = ArrayPool<FGpuRecordCreateResult>.Shared.Rent(records.Length);
try
{
fixed (FGpuRecordCreateResult* p = arr)
{
Ptr->RentRecords((uint)records.Length, p).TryThrow();
for (var i = 0; i < records.Length; i++)
{
records[i] = new(p[i].Record, p[i].Data, null, this);
}
}
}
finally
{
ArrayPool<FGpuRecordCreateResult>.Shared.Return(arr);
}
}
#endregion
#region Return Reocrds
public void ReturnRecords(params ReadOnlySpan<GpuRecord> records)
{
if (records.Length == 0) return;
bool disposed = false;
var arr = ArrayPool<IntPtr>.Shared.Rent(records.Length);
var c = 0u;
try
{
foreach (var t in records)
{
if (ExchangeUtils.ExchangePtr(ref t.m_ptr, null, out var ptr) is null) disposed = true;
else arr[c++] = (IntPtr)ptr;
}
if (c > 0)
{
fixed (IntPtr* p = arr)
{
Ptr->ReturnRecords(c, (FGpuRecord**)p).TryThrow();
}
}
if (disposed) throw new ObjectDisposedException(nameof(GpuRecord));
}
finally
{
ArrayPool<IntPtr>.Shared.Return(arr);
}
}
#endregion
#region Submit
public void Submit(params ReadOnlySpan<GpuRecord> records)
{
if (records.Length == 0) return;
var arr = ArrayPool<IntPtr>.Shared.Rent(records.Length);
var @out = ArrayPool<FGpuRecordCreateResult>.Shared.Rent(records.Length);
try
{
for (var i = 0; i < records.Length; i++)
{
if ((arr[i] = (IntPtr)records[i].Ptr) == 0) throw new NullReferenceException();
records[i].AssertEndOrCanEnd();
}
fixed (IntPtr* p = arr)
fixed (FGpuRecordCreateResult* o = @out)
{
for (var i = 0; i < records.Length; i++)
{
if ((p[i] = (IntPtr)records[i].Ptr) == 0) throw new NullReferenceException();
records[i].AssertEndOrCanEnd();
}
try
{
Ptr->Submit((uint)records.Length, (FGpuRecord**)p, o).TryThrow();
for (var i = 0; i < records.Length; i++)
{
records[i].m_ptr = (FUnknown*)o[i].Record;
records[i].m_data = o[i].Data;
records[i].Reset();
}
}
catch (Exception e)
{
throw new UnrecoverableException(e);
}
}
}
finally
{
ArrayPool<IntPtr>.Shared.Return(arr);
ArrayPool<FGpuRecordCreateResult>.Shared.Return(@out);
}
}
#endregion
#region Submit Return
public void SubmitReturn(params ReadOnlySpan<GpuRecord> records)
{
if (records.Length == 0) return;
var arr = ArrayPool<IntPtr>.Shared.Rent(records.Length);
try
{
for (var i = 0; i < records.Length; i++)
{
if ((arr[i] = (IntPtr)records[i].Ptr) == 0) throw new NullReferenceException();
records[i].AssertEndOrCanEnd();
}
fixed (IntPtr* p = arr)
{
try
{
Ptr->SubmitReturn((uint)records.Length, (FGpuRecord**)p).TryThrow();
foreach (var a in records)
{
a.m_ptr = null;
a.m_data = null;
a.Reset();
}
}
catch (Exception e)
{
throw new UnrecoverableException(e);
}
}
}
finally
{
ArrayPool<IntPtr>.Shared.Return(arr);
}
}
#endregion
#region CreateSwapChainOnWindows
private enum SwapChainFor
{
Composition,
CoreWindow,
Hwnd,
}
/// <summary>
/// Will fail on non Windows systems
/// </summary>
private GpuSwapChain CreateSwapChainOnWindows(
IntPtr Handle, SwapChainFor For, in GpuSwapChainCreateOptions Options, string? Name = null, ReadOnlySpan<byte> Name8 = default
)
{
fixed (char* p_name = Name)
fixed (byte* p_name8 = Name8)
{
FGpuSwapChainCreateOptions f_options = new()
{
Base = new()
{
Name = new(Name, Name8, p_name, p_name8),
PresentMode = (FPresentMode)Options.PresentMode,
Hdr = (FHdrType)Options.Hdr,
Srgb = Options.Srgb,
},
Width = Options.Width,
Height = Options.Height,
AlphaMode = (FOutputAlphaMode)Options.AlphaMode,
VSync = Options.VSync,
};
FGpuSwapChainCreateResult r;
switch (For)
{
case SwapChainFor.Composition:
Ptr->CreateSwapChainForComposition(&f_options, &r).TryThrow();
break;
case SwapChainFor.CoreWindow:
Ptr->CreateSwapChainForCoreWindow(&f_options, (void*)Handle, &r).TryThrow();
break;
case SwapChainFor.Hwnd:
Ptr->CreateSwapChainForHwnd(&f_options, (void*)Handle, &r).TryThrow();
break;
default:
throw new ArgumentOutOfRangeException(nameof(For), For, null);
}
return new(r.SwapChain, r.Data, Name, this);
}
}
#endregion
#region CreateSwapChainForComposition
/// <summary>
/// Will fail on non Windows systems
/// </summary>
public GpuSwapChain CreateSwapChainForComposition(in GpuSwapChainCreateOptions Options, string? Name = null, ReadOnlySpan<byte> Name8 = default)
=> CreateSwapChainOnWindows(0, SwapChainFor.Composition, in Options, Name, Name8);
#endregion
#region CreateSwapChainForCoreWindow
/// <summary>
/// Will fail on non Windows systems
/// </summary>
public GpuSwapChain CreateSwapChainForCoreWindow(
IntPtr Window, in GpuSwapChainCreateOptions Options, string? Name = null, ReadOnlySpan<byte> Name8 = default
) => CreateSwapChainOnWindows(Window, SwapChainFor.CoreWindow, in Options, Name, Name8);
#endregion
#region CreateSwapChainForHwnd
/// <summary>
/// Will fail on non Windows systems
/// </summary>
public GpuSwapChain CreateSwapChainForHwnd(IntPtr Hwnd, in GpuSwapChainCreateOptions Options, string? Name = null, ReadOnlySpan<byte> Name8 = default)
=> CreateSwapChainOnWindows(Hwnd, SwapChainFor.Hwnd, in Options, Name, Name8);
#endregion
#region CreateBindGroup
public ShaderBindGroup CreateBindGroup(
ShaderBindGroupLayout layout, ReadOnlySpan<SetShaderBindItem> items,
string? Name = null, ReadOnlySpan<byte> Name8 = default
)
{
ShaderBindGroup.CheckSet(layout, items);
var f_items = ArrayPool<FSetBindItem>.Shared.Rent(items.Length);
try
{
for (var i = 0; i < items.Length; i++)
{
ref readonly var item = ref items[i];
f_items[i] = new()
{
View = item.View.ToFFI(),
Slot = item.BindIndex,
Index = item.ArrayIndex,
};
}
fixed (char* p_name = Name)
fixed (byte* p_name8 = Name8)
fixed (FSetBindItem* p_items = f_items)
{
FShaderBindGroupCreateOptions f_options = new()
{
Name = new(Name, Name8, p_name, p_name8),
Layout = layout.Ptr,
Bindings = p_items,
NumBindings = (uint)items.Length,
};
FShaderBindGroupCreateResult result;
Device.Ptr->CreateShaderBindGroup(&f_options, &result).TryThrow();
return new(result, Name, this, layout);
}
}
finally
{
ArrayPool<FSetBindItem>.Shared.Return(f_items);
}
}
#endregion
#region CreateBinding
public ShaderBinding CreateBinding(
ShaderBindingLayout layout, ReadOnlySpan<SetShaderBindGroupItem> items,
string? Name = null, ReadOnlySpan<byte> Name8 = default
)
{
ShaderBinding.CheckSet(layout, items);
var f_items = ArrayPool<FSetBindGroupItem>.Shared.Rent(items.Length);
try
{
for (var i = 0; i < items.Length; i++)
{
ref readonly var item = ref items[i];
f_items[i] = new()
{
BindGroup = item.Group.Ptr,
Index = item.Index,
};
}
fixed (char* p_name = Name)
fixed (byte* p_name8 = Name8)
fixed (FSetBindGroupItem* p_items = f_items)
{
FShaderBindingCreateOptions f_options = new()
{
Name = new(Name, Name8, p_name, p_name8),
Layout = layout.Ptr,
BindGroups = p_items,
NumBindGroups = (uint)items.Length,
};
FShaderBindingCreateResult result;
Device.Ptr->CreateShaderBinding(&f_options, &result).TryThrow();
return new(result, Name, this, layout);
}
}
finally
{
ArrayPool<FSetBindGroupItem>.Shared.Return(f_items);
}
}
#endregion
#region CreateBuffer
public GpuBuffer CreateBuffer(
in GpuBufferCreateOptions options,
string? Name = null, ReadOnlySpan<byte> Name8 = default
)
{
fixed (char* p_name = Name)
fixed (byte* p_name8 = Name8)
{
FGpuBufferCreateOptions f_options = new()
{
Base =
{
Name = new(Name, Name8, p_name, p_name8),
Purpose = options.Purpose.ToFFI(),
CpuAccess = options.CpuAccess.ToFFI(),
},
Size = options.Size,
Count = options.Count,
Stride = options.Stride,
Usage = options.Usage.ToFFI(),
};
FGpuBuffer* ptr;
Device.Ptr->CreateBuffer(&f_options, &ptr).TryThrow();
return new(ptr, Name, this);
}
}
#endregion
#region CreateImage
public GpuImage CreateImage(
in GpuImageCreateOptions options,
string? Name = null, ReadOnlySpan<byte> Name8 = default
)
{
if (options.Format == GraphicsFormat.Unknown)
throw new ArgumentException("The image format cannot be Unknown");
if (options.Purpose == ResourcePurpose.None)
throw new ArgumentException("The image purpose cannot be None");
fixed (char* p_name = Name)
fixed (byte* p_name8 = Name8)
{
var DepthOrLength = Math.Max(options.DepthOrLength, 1);
if (options.Dimension == ImageDimension.Cube)
{
if (options.DepthOrLength == 0) DepthOrLength = 6;
if (options.DepthOrLength % 6 != 0) throw new ArgumentException("The number of cube texture arrays must be a multiple of 6");
}
FGpuImageCreateOptions f_options = new()
{
Base =
{
Name = new(Name, Name8, p_name, p_name8),
Purpose = options.Purpose.ToFFI(),
CpuAccess = options.CpuAccess.ToFFI(),
},
Format = options.Format.ToFFI(),
Width = Math.Max(options.Width, 1),
Height = Math.Max(options.Height, 1),
DepthOrLength = DepthOrLength,
MipLevels = (ushort)Math.Max(options.MipLevels, 1),
MultisampleCount = (byte)Math.Max(options.MultisampleCount, 1),
Dimension = options.Dimension.ToFFI(),
Layout = options.Layout.ToFFI(),
HasOptimizedClearValue = !options.OptimizedClearColor.IsNone,
};
if (options.OptimizedClearColor.IsColor)
{
f_options.OptimizedClearValue.Format = options.OptimizedClearColor.Color.Format.ToFFI();
f_options.OptimizedClearValue.Anonymous.Color =
Unsafe.BitCast<Color, FOptimizedClearColor._Anonymous_e__Union._Color_e__FixedBuffer>(
options.OptimizedClearColor.Color.Color
);
}
else if (options.OptimizedClearColor.IsDepth)
{
f_options.OptimizedClearValue.Format = options.OptimizedClearColor.Depth.Format.ToFFI();
f_options.OptimizedClearValue.Depth = options.OptimizedClearColor.Depth.Depth;
f_options.OptimizedClearValue.Stencil = options.OptimizedClearColor.Depth.Stencil;
}
FGpuImage* ptr;
Device.Ptr->CreateImage(&f_options, &ptr).TryThrow();
return new(ptr, Name, this);
}
}
#endregion
}