-
Notifications
You must be signed in to change notification settings - Fork 45
/
Buffer.cs
381 lines (299 loc) · 14.1 KB
/
Buffer.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
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using DirectX12GameEngine.Core;
using Vortice.Direct3D12;
namespace DirectX12GameEngine.Graphics
{
public partial class GraphicsBuffer : GraphicsResource
{
[EditorBrowsable(EditorBrowsableState.Never)]
public GraphicsBuffer()
{
}
protected internal GraphicsBuffer(GraphicsDevice device) : base(device)
{
}
public BufferDescription Description { get; private set; }
public int SizeInBytes => Description.SizeInBytes;
public BufferFlags Flags => Description.Flags;
public GraphicsHeapType HeapType => Description.HeapType;
public int StructureByteStride => Description.StructureByteStride;
public int FirstElement { get; protected set; }
public int ElementCount { get; protected set; }
internal CpuDescriptorHandle NativeConstantBufferView { get; private set; }
public static GraphicsBuffer New(GraphicsDevice device, BufferDescription description)
{
return new GraphicsBuffer(device).InitializeFrom(description);
}
public static GraphicsBuffer New(GraphicsDevice device, int size, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default)
{
return New(device, new BufferDescription(size, bufferFlags, heapType));
}
public static GraphicsBuffer New(GraphicsDevice device, int size, int structureByteStride, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default)
{
return New(device, new BufferDescription(size, bufferFlags, heapType, structureByteStride));
}
public static GraphicsBuffer<T> New<T>(GraphicsDevice device, int elementCount, int structureByteStride, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged
{
int size = structureByteStride * elementCount;
return new GraphicsBuffer<T>(device, new BufferDescription(size, bufferFlags, heapType, structureByteStride));
}
public static GraphicsBuffer<T> New<T>(GraphicsDevice device, int elementCount, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged
{
return New<T>(device, elementCount, Unsafe.SizeOf<T>(), bufferFlags, heapType);
}
public static unsafe GraphicsBuffer<T> New<T>(GraphicsDevice device, in T data, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged
{
fixed (T* pointer = &data)
{
return New(device, new Span<T>(pointer, 1), bufferFlags, heapType);
}
}
public static GraphicsBuffer<T> New<T>(GraphicsDevice device, Span<T> data, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged
{
return New(device, data, Unsafe.SizeOf<T>(), bufferFlags, heapType);
}
public static GraphicsBuffer<T> New<T>(GraphicsDevice device, Span<T> data, int structureByteStride, BufferFlags bufferFlags, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged
{
GraphicsBuffer<T> buffer = New<T>(device, data.Length, structureByteStride, bufferFlags, heapType);
buffer.SetData(data);
return buffer;
}
public T[] GetData<T>() where T : unmanaged
{
T[] data = new T[SizeInBytes / Unsafe.SizeOf<T>()];
GetData(data.AsSpan());
return data;
}
public unsafe void GetData<T>(ref T data, int offsetInBytes = 0) where T : unmanaged
{
fixed (T* pointer = &data)
{
GetData(new Span<T>(pointer, 1), offsetInBytes);
}
}
public void GetData<T>(Span<T> data, int offsetInBytes = 0) where T : unmanaged
{
offsetInBytes = FirstElement * StructureByteStride + offsetInBytes;
if (HeapType == GraphicsHeapType.Default)
{
using GraphicsBuffer<T> readbackBaffer = New<T>(GraphicsDevice, data.Length, BufferFlags.None, GraphicsHeapType.Readback);
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
copyCommandList.CopyBufferRegion(this, offsetInBytes, readbackBaffer, 0, data.Length * Unsafe.SizeOf<T>());
copyCommandList.Flush(true);
readbackBaffer.GetData(data);
}
else
{
Map(0);
MemoryHelper.Copy(MappedResource + offsetInBytes, data);
Unmap(0);
}
}
public unsafe void SetData<T>(in T data, int offsetInBytes = 0) where T : unmanaged
{
fixed (T* pointer = &data)
{
SetData(new Span<T>(pointer, 1), offsetInBytes);
}
}
public void SetData<T>(Span<T> data, int offsetInBytes = 0) where T : unmanaged
{
offsetInBytes = FirstElement * StructureByteStride + offsetInBytes;
if (HeapType == GraphicsHeapType.Default)
{
using GraphicsBuffer<T> uploadBuffer = New(GraphicsDevice, data, BufferFlags.None, GraphicsHeapType.Upload);
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
copyCommandList.CopyBufferRegion(uploadBuffer, 0, this, offsetInBytes, data.Length * Unsafe.SizeOf<T>());
copyCommandList.Flush(true);
}
else
{
Map(0);
MemoryHelper.Copy(data, MappedResource + offsetInBytes);
Unmap(0);
}
}
public GraphicsBuffer Slice(int start, int length)
{
return new GraphicsBuffer(GraphicsDevice).InitializeFrom(NativeResource!, Description, start, length);
}
public GraphicsBuffer InitializeFrom(BufferDescription description)
{
ResourceStates resourceStates = ResourceStates.Common;
if (description.HeapType == GraphicsHeapType.Upload)
{
resourceStates = ResourceStates.GenericRead;
}
else if (description.HeapType == GraphicsHeapType.Readback)
{
resourceStates = ResourceStates.CopyDestination;
}
ID3D12Resource resource = GraphicsDevice.NativeDevice.CreateCommittedResource(
new HeapProperties((HeapType)description.HeapType), HeapFlags.None,
ConvertToNativeDescription(description), resourceStates);
return InitializeFrom(resource, description);
}
protected internal GraphicsBuffer InitializeFrom(ID3D12Resource resource, bool isShaderResource = false)
{
resource.GetHeapProperties(out HeapProperties heapProperties, out _);
BufferDescription description = ConvertFromNativeDescription(resource.Description, (GraphicsHeapType)heapProperties.Type, isShaderResource);
return InitializeFrom(resource, description);
}
protected internal GraphicsBuffer InitializeFrom(ID3D12Resource resource, BufferDescription description, int firstElement = 0, int elementCount = 0)
{
NativeResource = resource;
Description = description;
FirstElement = firstElement;
if (description.StructureByteStride != 0)
{
ElementCount = elementCount == 0 ? description.SizeInBytes / description.StructureByteStride : elementCount;
}
if (description.Flags.HasFlag(BufferFlags.RenderTarget))
{
NativeRenderTargetView = CreateRenderTargetView();
}
if (description.Flags.HasFlag(BufferFlags.ConstantBuffer))
{
NativeConstantBufferView = CreateConstantBufferView();
}
if (description.Flags.HasFlag(BufferFlags.ShaderResource))
{
NativeShaderResourceView = CreateShaderResourceView();
}
if (description.Flags.HasFlag(BufferFlags.UnorderedAccess))
{
NativeUnorderedAccessView = CreateUnorderedAccessView();
}
return this;
}
private static BufferDescription ConvertFromNativeDescription(ResourceDescription description, GraphicsHeapType heapType, bool isShaderResource = false)
{
BufferDescription bufferDescription = new BufferDescription
{
SizeInBytes = (int)description.Width,
HeapType = heapType,
Flags = BufferFlags.None
};
if (description.Flags.HasFlag(ResourceFlags.AllowUnorderedAccess))
{
bufferDescription.Flags |= BufferFlags.UnorderedAccess;
}
if (!description.Flags.HasFlag(ResourceFlags.DenyShaderResource) && isShaderResource)
{
bufferDescription.Flags |= BufferFlags.ShaderResource;
}
return bufferDescription;
}
private static ResourceDescription ConvertToNativeDescription(BufferDescription description)
{
int size = description.SizeInBytes;
ResourceFlags flags = ResourceFlags.None;
if (description.Flags.HasFlag(BufferFlags.UnorderedAccess))
{
flags |= ResourceFlags.AllowUnorderedAccess;
}
return ResourceDescription.Buffer(size, flags);
}
protected internal CpuDescriptorHandle CreateRenderTargetView()
{
CpuDescriptorHandle cpuHandle = GraphicsDevice.RenderTargetViewAllocator.Allocate(1);
RenderTargetViewDescription description = new RenderTargetViewDescription
{
ViewDimension = RenderTargetViewDimension.Buffer,
Buffer =
{
FirstElement = FirstElement,
NumElements = ElementCount
}
};
GraphicsDevice.NativeDevice.CreateRenderTargetView(NativeResource, description, cpuHandle);
return cpuHandle;
}
protected internal CpuDescriptorHandle CreateConstantBufferView()
{
CpuDescriptorHandle cpuHandle = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1);
int constantBufferSize = (SizeInBytes + 255) & ~255;
ConstantBufferViewDescription cbvDescription = new ConstantBufferViewDescription
{
BufferLocation = NativeResource!.GPUVirtualAddress,
SizeInBytes = constantBufferSize
};
GraphicsDevice.NativeDevice.CreateConstantBufferView(cbvDescription, cpuHandle);
return cpuHandle;
}
protected internal CpuDescriptorHandle CreateShaderResourceView()
{
CpuDescriptorHandle cpuHandle = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1);
ShaderResourceViewDescription description = new ShaderResourceViewDescription
{
Shader4ComponentMapping = D3DXUtilities.DefaultComponentMapping(),
ViewDimension = ShaderResourceViewDimension.Buffer,
Buffer =
{
FirstElement = FirstElement,
NumElements = ElementCount,
StructureByteStride = StructureByteStride
},
};
GraphicsDevice.NativeDevice.CreateShaderResourceView(NativeResource, description, cpuHandle);
return cpuHandle;
}
protected internal CpuDescriptorHandle CreateUnorderedAccessView()
{
CpuDescriptorHandle cpuHandle = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1);
UnorderedAccessViewDescription description = new UnorderedAccessViewDescription
{
ViewDimension = UnorderedAccessViewDimension.Buffer,
Buffer =
{
FirstElement = FirstElement,
NumElements = ElementCount,
StructureByteStride = StructureByteStride
}
};
GraphicsDevice.NativeDevice.CreateUnorderedAccessView(NativeResource, null, description, cpuHandle);
return cpuHandle;
}
}
public class GraphicsBuffer<T> : GraphicsBuffer where T : unmanaged
{
protected internal GraphicsBuffer(GraphicsDevice device) : base(device)
{
}
public GraphicsBuffer(GraphicsDevice device, BufferDescription description) : base(device)
{
InitializeFrom(description);
}
public new GraphicsBuffer<T> Slice(int start, int length)
{
GraphicsBuffer<T> buffer = new GraphicsBuffer<T>(GraphicsDevice);
buffer.InitializeFrom(NativeResource!, Description, start, length);
return buffer;
}
public T[] GetData() => GetData<T>();
}
internal class D3DXUtilities
{
public const int ComponentMappingMask = 0x7;
public const int ComponentMappingShift = 3;
public const int ComponentMappingAlwaysSetBitAvoidingZeromemMistakes = 1 << (ComponentMappingShift * 4);
public static int ComponentMapping(int src0, int src1, int src2, int src3)
{
return ((src0) & ComponentMappingMask)
| (((src1) & ComponentMappingMask) << ComponentMappingShift)
| (((src2) & ComponentMappingMask) << (ComponentMappingShift * 2))
| (((src3) & ComponentMappingMask) << (ComponentMappingShift * 3))
| ComponentMappingAlwaysSetBitAvoidingZeromemMistakes;
}
public static int DefaultComponentMapping()
{
return ComponentMapping(0, 1, 2, 3);
}
public static int ComponentMapping(int ComponentToExtract, int Mapping)
{
return (Mapping >> (ComponentMappingShift * ComponentToExtract)) & ComponentMappingMask;
}
}
}