-
-
Notifications
You must be signed in to change notification settings - Fork 911
/
Buffer.Direct3D.cs
298 lines (252 loc) · 11.9 KB
/
Buffer.Direct3D.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
// Copyright (c) Stride contributors (https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
#if STRIDE_GRAPHICS_API_DIRECT3D11
using System;
using System.Collections.Generic;
using SharpDX;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
namespace Stride.Graphics
{
public partial class Buffer
{
private SharpDX.Direct3D11.BufferDescription nativeDescription;
internal SharpDX.Direct3D11.Buffer NativeBuffer
{
get
{
return (SharpDX.Direct3D11.Buffer)NativeDeviceChild;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="Buffer" /> class.
/// </summary>
/// <param name="description">The description.</param>
/// <param name="viewFlags">Type of the buffer.</param>
/// <param name="viewFormat">The view format.</param>
/// <param name="dataPointer">The data pointer.</param>
protected Buffer InitializeFromImpl(BufferDescription description, BufferFlags viewFlags, PixelFormat viewFormat, IntPtr dataPointer)
{
bufferDescription = description;
nativeDescription = ConvertToNativeDescription(Description);
ViewFlags = viewFlags;
InitCountAndViewFormat(out this.elementCount, ref viewFormat);
ViewFormat = viewFormat;
NativeDeviceChild = new SharpDX.Direct3D11.Buffer(GraphicsDevice.NativeDevice, dataPointer, nativeDescription);
// Staging resource don't have any views
if (nativeDescription.Usage != ResourceUsage.Staging)
this.InitializeViews();
if (GraphicsDevice != null)
{
GraphicsDevice.RegisterBufferMemoryUsage(SizeInBytes);
}
return this;
}
/// <inheritdoc/>
protected internal override void OnDestroyed()
{
if (GraphicsDevice != null)
{
GraphicsDevice.RegisterBufferMemoryUsage(-SizeInBytes);
}
base.OnDestroyed();
}
/// <inheritdoc/>
protected internal override bool OnRecreate()
{
base.OnRecreate();
if (Description.Usage == GraphicsResourceUsage.Immutable
|| Description.Usage == GraphicsResourceUsage.Default)
return false;
NativeDeviceChild = new SharpDX.Direct3D11.Buffer(GraphicsDevice.NativeDevice, IntPtr.Zero, nativeDescription);
// Staging resource don't have any views
if (nativeDescription.Usage != ResourceUsage.Staging)
this.InitializeViews();
return true;
}
/// <summary>
/// Explicitly recreate buffer with given data. Usually called after a <see cref="GraphicsDevice"/> reset.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dataPointer"></param>
public void Recreate(IntPtr dataPointer)
{
NativeDeviceChild = new SharpDX.Direct3D11.Buffer(GraphicsDevice.NativeDevice, dataPointer, nativeDescription);
// Staging resource don't have any views
if (nativeDescription.Usage != ResourceUsage.Staging)
this.InitializeViews();
}
/// <summary>
/// Gets a <see cref="ShaderResourceView"/> for a particular <see cref="PixelFormat"/>.
/// </summary>
/// <param name="viewFormat">The view format.</param>
/// <returns>A <see cref="ShaderResourceView"/> for the particular view format.</returns>
/// <remarks>
/// The buffer must have been declared with <see cref="Graphics.BufferFlags.ShaderResource"/>.
/// The ShaderResourceView instance is kept by this buffer and will be disposed when this buffer is disposed.
/// </remarks>
internal ShaderResourceView GetShaderResourceView(PixelFormat viewFormat)
{
ShaderResourceView srv = null;
if ((nativeDescription.BindFlags & BindFlags.ShaderResource) != 0)
{
var description = new ShaderResourceViewDescription
{
Format = (SharpDX.DXGI.Format)viewFormat,
Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.ExtendedBuffer,
BufferEx =
{
ElementCount = this.ElementCount,
FirstElement = 0,
Flags = ShaderResourceViewExtendedBufferFlags.None,
},
};
if (((ViewFlags & BufferFlags.RawBuffer) == BufferFlags.RawBuffer))
description.BufferEx.Flags |= ShaderResourceViewExtendedBufferFlags.Raw;
srv = new ShaderResourceView(this.GraphicsDevice.NativeDevice, NativeResource, description);
}
return srv;
}
/// <summary>
/// Gets a <see cref="RenderTargetView" /> for a particular <see cref="PixelFormat" />.
/// </summary>
/// <param name="pixelFormat">The view format.</param>
/// <param name="width">The width in pixels of the render target.</param>
/// <returns>A <see cref="RenderTargetView" /> for the particular view format.</returns>
/// <remarks>The buffer must have been declared with <see cref="Graphics.BufferFlags.RenderTarget" />.
/// The RenderTargetView instance is kept by this buffer and will be disposed when this buffer is disposed.</remarks>
internal RenderTargetView GetRenderTargetView(PixelFormat pixelFormat, int width)
{
RenderTargetView srv = null;
if ((nativeDescription.BindFlags & BindFlags.RenderTarget) != 0)
{
var description = new RenderTargetViewDescription()
{
Format = (SharpDX.DXGI.Format)pixelFormat,
Dimension = RenderTargetViewDimension.Buffer,
Buffer =
{
ElementWidth = pixelFormat.SizeInBytes() * width,
ElementOffset = 0,
},
};
srv = new RenderTargetView(this.GraphicsDevice.NativeDevice, NativeBuffer, description);
}
return srv;
}
protected override void OnNameChanged()
{
base.OnNameChanged();
if (GraphicsDevice != null && GraphicsDevice.IsDebugMode)
{
if (NativeShaderResourceView != null)
NativeShaderResourceView.DebugName = Name == null ? null : string.Format("{0} SRV", Name);
if (NativeUnorderedAccessView != null)
NativeUnorderedAccessView.DebugName = Name == null ? null : string.Format("{0} UAV", Name);
}
}
private void InitCountAndViewFormat(out int count, ref PixelFormat viewFormat)
{
if (Description.StructureByteStride == 0)
{
// TODO: The way to calculate the count is not always correct depending on the ViewFlags...etc.
if ((ViewFlags & BufferFlags.RawBuffer) != 0)
{
count = Description.SizeInBytes / sizeof(int);
}
else if ((ViewFlags & BufferFlags.ShaderResource) != 0)
{
count = Description.SizeInBytes / viewFormat.SizeInBytes();
}
else
{
count = 0;
}
}
else
{
// For structured buffer
count = Description.SizeInBytes / Description.StructureByteStride;
viewFormat = PixelFormat.None;
}
}
private static SharpDX.Direct3D11.BufferDescription ConvertToNativeDescription(BufferDescription bufferDescription)
{
var desc = new SharpDX.Direct3D11.BufferDescription()
{
SizeInBytes = bufferDescription.SizeInBytes,
StructureByteStride = bufferDescription.StructureByteStride,
CpuAccessFlags = GetCpuAccessFlagsFromUsage(bufferDescription.Usage),
BindFlags = BindFlags.None,
OptionFlags = ResourceOptionFlags.None,
Usage = (SharpDX.Direct3D11.ResourceUsage)bufferDescription.Usage,
};
var bufferFlags = bufferDescription.BufferFlags;
if ((bufferFlags & BufferFlags.ConstantBuffer) != 0)
desc.BindFlags |= BindFlags.ConstantBuffer;
if ((bufferFlags & BufferFlags.IndexBuffer) != 0)
desc.BindFlags |= BindFlags.IndexBuffer;
if ((bufferFlags & BufferFlags.VertexBuffer) != 0)
desc.BindFlags |= BindFlags.VertexBuffer;
if ((bufferFlags & BufferFlags.RenderTarget) != 0)
desc.BindFlags |= BindFlags.RenderTarget;
if ((bufferFlags & BufferFlags.ShaderResource) != 0)
desc.BindFlags |= BindFlags.ShaderResource;
if ((bufferFlags & BufferFlags.UnorderedAccess) != 0)
desc.BindFlags |= BindFlags.UnorderedAccess;
if ((bufferFlags & BufferFlags.StructuredBuffer) != 0)
{
desc.OptionFlags |= ResourceOptionFlags.BufferStructured;
if (bufferDescription.StructureByteStride <= 0)
throw new ArgumentException("Element size cannot be less or equal 0 for structured buffer");
}
if ((bufferFlags & BufferFlags.RawBuffer) == BufferFlags.RawBuffer)
desc.OptionFlags |= ResourceOptionFlags.BufferAllowRawViews;
if ((bufferFlags & BufferFlags.ArgumentBuffer) == BufferFlags.ArgumentBuffer)
desc.OptionFlags |= ResourceOptionFlags.DrawIndirectArguments;
if ((bufferFlags & BufferFlags.StreamOutput) != 0)
desc.BindFlags |= BindFlags.StreamOutput;
return desc;
}
/// <summary>
/// Initializes the views.
/// </summary>
private void InitializeViews()
{
var bindFlags = nativeDescription.BindFlags;
var srvFormat = ViewFormat;
var uavFormat = ViewFormat;
if (((ViewFlags & BufferFlags.RawBuffer) != 0))
{
srvFormat = PixelFormat.R32_Typeless;
uavFormat = PixelFormat.R32_Typeless;
}
if ((bindFlags & BindFlags.ShaderResource) != 0)
{
this.NativeShaderResourceView = GetShaderResourceView(srvFormat);
}
if ((bindFlags & BindFlags.UnorderedAccess) != 0)
{
var description = new UnorderedAccessViewDescription()
{
Format = (SharpDX.DXGI.Format)uavFormat,
Dimension = UnorderedAccessViewDimension.Buffer,
Buffer =
{
ElementCount = this.ElementCount,
FirstElement = 0,
Flags = UnorderedAccessViewBufferFlags.None,
},
};
if (((ViewFlags & BufferFlags.RawBuffer) == BufferFlags.RawBuffer))
description.Buffer.Flags |= UnorderedAccessViewBufferFlags.Raw;
if (((ViewFlags & BufferFlags.StructuredAppendBuffer) == BufferFlags.StructuredAppendBuffer))
description.Buffer.Flags |= UnorderedAccessViewBufferFlags.Append;
if (((ViewFlags & BufferFlags.StructuredCounterBuffer) == BufferFlags.StructuredCounterBuffer))
description.Buffer.Flags |= UnorderedAccessViewBufferFlags.Counter;
this.NativeUnorderedAccessView = new UnorderedAccessView(this.GraphicsDevice.NativeDevice, NativeBuffer, description);
}
}
}
}
#endif