3 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_DIRECT3D
5 using System.Collections.Generic;
9 using SharpDX.Direct3D11;
11 namespace SiliconStudio.
Paradox.Graphics
13 public partial class Buffer
15 private readonly SharpDX.Direct3D11.BufferDescription nativeDescription;
17 internal SharpDX.Direct3D11.Buffer NativeBuffer
21 return (SharpDX.Direct3D11.Buffer)NativeDeviceChild;
33 protected Buffer(GraphicsDevice device, BufferDescription description,
BufferFlags bufferFlags,
PixelFormat viewFormat, IntPtr dataPointer) : base(device)
35 Description = description;
36 nativeDescription = ConvertToNativeDescription(Description);
38 ViewFormat = viewFormat;
39 InitCountAndViewFormat(out this.elementCount, ref ViewFormat);
40 NativeDeviceChild =
new SharpDX.Direct3D11.Buffer(device.RootDevice.NativeDevice, dataPointer, nativeDescription);
43 if (nativeDescription.Usage != ResourceUsage.Staging)
44 this.InitializeViews();
48 protected internal override void OnDestroyed()
55 protected internal override bool OnRecreate()
63 NativeDeviceChild =
new SharpDX.Direct3D11.Buffer(GraphicsDevice.RootDevice.NativeDevice, IntPtr.Zero, nativeDescription);
66 if (nativeDescription.Usage != ResourceUsage.Staging)
67 this.InitializeViews();
77 public void Recreate(IntPtr dataPointer)
79 NativeDeviceChild =
new SharpDX.Direct3D11.Buffer(GraphicsDevice.RootDevice.NativeDevice, dataPointer, nativeDescription);
82 if (nativeDescription.Usage != ResourceUsage.Staging)
83 this.InitializeViews();
98 if ((nativeDescription.BindFlags & BindFlags.ShaderResource) != 0)
100 var description =
new ShaderResourceViewDescription
102 Format = (SharpDX.DXGI.Format)viewFormat,
103 Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.ExtendedBuffer,
106 ElementCount =
this.ElementCount,
108 Flags = ShaderResourceViewExtendedBufferFlags.None
113 description.BufferEx.Flags |= ShaderResourceViewExtendedBufferFlags.Raw;
115 srv =
new ShaderResourceView(this.GraphicsDevice.NativeDevice, NativeResource, description);
128 internal RenderTargetView GetRenderTargetView(
PixelFormat pixelFormat,
int width)
130 RenderTargetView srv = null;
131 if ((nativeDescription.BindFlags & BindFlags.RenderTarget) != 0)
133 var description =
new RenderTargetViewDescription()
135 Format = (SharpDX.DXGI.Format)pixelFormat,
136 Dimension = RenderTargetViewDimension.Buffer,
139 ElementWidth = pixelFormat.SizeInBytes() * width,
144 srv =
new RenderTargetView(this.GraphicsDevice.NativeDevice, NativeBuffer, description);
149 protected override void OnNameChanged()
151 base.OnNameChanged();
152 if (GraphicsDevice != null && GraphicsDevice.IsDebugMode)
154 if (NativeShaderResourceView != null)
155 NativeShaderResourceView.DebugName = Name == null ? null : string.Format(
"{0} SRV", Name);
157 if (NativeUnorderedAccessView != null)
158 NativeUnorderedAccessView.DebugName = Name == null ? null : string.Format(
"{0} UAV", Name);
162 private void InitCountAndViewFormat(out
int count, ref
PixelFormat viewFormat)
164 if (Description.StructureByteStride == 0)
167 count = Description.SizeInBytes /
sizeof(int);
170 count = (
BufferFlags & BufferFlags.ShaderResource) != 0 ? Description.SizeInBytes / ViewFormat.SizeInBytes() : 0;
178 count = Description.SizeInBytes / Description.StructureByteStride;
179 viewFormat = PixelFormat.None;
183 private static SharpDX.Direct3D11.BufferDescription ConvertToNativeDescription(BufferDescription bufferDescription)
185 var desc =
new SharpDX.Direct3D11.BufferDescription()
187 SizeInBytes = bufferDescription.SizeInBytes,
188 StructureByteStride = bufferDescription.StructureByteStride,
189 CpuAccessFlags = GetCpuAccessFlagsFromUsage(bufferDescription.Usage),
190 BindFlags = BindFlags.None,
191 OptionFlags = ResourceOptionFlags.None,
192 Usage = (SharpDX.Direct3D11.ResourceUsage)bufferDescription.Usage,
197 if ((bufferFlags &
BufferFlags.ConstantBuffer) != 0)
198 desc.BindFlags |= BindFlags.ConstantBuffer;
201 desc.BindFlags |= BindFlags.IndexBuffer;
204 desc.BindFlags |= BindFlags.VertexBuffer;
207 desc.BindFlags |= BindFlags.RenderTarget;
209 if ((bufferFlags &
BufferFlags.ShaderResource) != 0)
210 desc.BindFlags |= BindFlags.ShaderResource;
212 if ((bufferFlags &
BufferFlags.UnorderedAccess) != 0)
213 desc.BindFlags |= BindFlags.UnorderedAccess;
215 if ((bufferFlags &
BufferFlags.StructuredBuffer) != 0)
217 desc.OptionFlags |= ResourceOptionFlags.BufferStructured;
218 if (bufferDescription.StructureByteStride == 0)
219 throw new ArgumentException(
"Element size cannot be set to 0 for structured buffer");
223 desc.OptionFlags |= ResourceOptionFlags.BufferAllowRawViews;
226 desc.OptionFlags |= ResourceOptionFlags.DrawIndirectArguments;
234 private void InitializeViews()
236 var bindFlags = nativeDescription.BindFlags;
238 var srvFormat = ViewFormat;
239 var uavFormat = ViewFormat;
243 srvFormat = PixelFormat.R32_Typeless;
244 uavFormat = PixelFormat.R32_Typeless;
247 if ((bindFlags & BindFlags.ShaderResource) != 0)
249 this.NativeShaderResourceView = GetShaderResourceView(srvFormat);
252 if ((bindFlags & BindFlags.UnorderedAccess) != 0)
254 var description =
new UnorderedAccessViewDescription()
256 Format = (SharpDX.DXGI.Format)uavFormat,
257 Dimension = UnorderedAccessViewDimension.Buffer,
260 ElementCount =
this.ElementCount,
262 Flags = UnorderedAccessViewBufferFlags.None
267 description.Buffer.Flags |= UnorderedAccessViewBufferFlags.Raw;
270 description.Buffer.Flags |= UnorderedAccessViewBufferFlags.Append;
273 description.Buffer.Flags |= UnorderedAccessViewBufferFlags.Counter;
275 this.NativeUnorderedAccessView =
new UnorderedAccessView(this.GraphicsDevice.NativeDevice, NativeBuffer, description);
GraphicsResourceUsage
Identifies expected resource use during rendering. The usage directly reflects whether a resource is ...
SiliconStudio.Paradox.Graphics.Buffer Buffer
Flags
Enumeration of the new Assimp's flags.
PixelFormat
Defines various types of pixel formats.