From 252e3373759b261b1038fad408bf6160d4b04886 Mon Sep 17 00:00:00 2001 From: Konstantin Koch Date: Sun, 18 Jan 2015 13:10:50 +0000 Subject: [PATCH] Made GetData for VertexBuffer and IndexBuffer work for DirectX10 and DirectX11. Also added a few parameter checks. --- .../BufferHelper.cs | 28 ++++ .../SharedIndexBuffer.cs | 141 ++++++++-------- .../SharedVertexBuffer.cs | 158 ++++++++++-------- .../ANX.RenderSystem.Windows.DX10.csproj | 3 + .../DxIndexBuffer.cs | 115 ++++++++----- .../DxVertexBuffer.cs | 93 +++++------ .../ANX.RenderSystem.Windows.DX11.csproj | 3 + .../DxIndexBuffer.cs | 128 ++++++++------ .../DxVertexBuffer.cs | 108 ++++++------ 9 files changed, 440 insertions(+), 337 deletions(-) create mode 100644 RenderSystems/ANX.RenderSystem.DX.SharedSources/BufferHelper.cs diff --git a/RenderSystems/ANX.RenderSystem.DX.SharedSources/BufferHelper.cs b/RenderSystems/ANX.RenderSystem.DX.SharedSources/BufferHelper.cs new file mode 100644 index 00000000..ebc97618 --- /dev/null +++ b/RenderSystems/ANX.RenderSystem.DX.SharedSources/BufferHelper.cs @@ -0,0 +1,28 @@ +using System; + +#if DX10 +using Dx = SharpDX.Direct3D10; +#elif DX11 +using Dx = SharpDX.Direct3D11; +#endif + +// This file is part of the ANX.Framework created by the +// "ANX.Framework developer group" and released under the Ms-PL license. +// For details see: http://anxframework.codeplex.com/license + +#if DX10 +namespace ANX.RenderSystem.Windows.DX10 +#elif DX11 +namespace ANX.RenderSystem.Windows.DX11 +#endif +{ + class BufferHelper + { + [System.Diagnostics.Conditional("DEBUG")] + public static void ValidateCopyResource(Dx.Buffer source, Dx.Buffer destination) + { + if (source.Description.SizeInBytes != destination.Description.SizeInBytes) + throw new InvalidOperationException("source and destination must have the same size."); + } + } +} \ No newline at end of file diff --git a/RenderSystems/ANX.RenderSystem.DX.SharedSources/SharedIndexBuffer.cs b/RenderSystems/ANX.RenderSystem.DX.SharedSources/SharedIndexBuffer.cs index c845c2dc..63111ecc 100644 --- a/RenderSystems/ANX.RenderSystem.DX.SharedSources/SharedIndexBuffer.cs +++ b/RenderSystems/ANX.RenderSystem.DX.SharedSources/SharedIndexBuffer.cs @@ -3,6 +3,7 @@ using System; using ANX.Framework.Graphics; using SharpDX; using System.IO; +using System.Runtime.InteropServices; #endregion @@ -12,86 +13,92 @@ using System.IO; #if DX10 namespace ANX.RenderSystem.Windows.DX10 -#endif -#if DX11 +#elif DX11 namespace ANX.RenderSystem.Windows.DX11 #endif { - public partial class DxIndexBuffer : IDisposable - { - protected IndexElementSize elementSize; + public partial class DxIndexBuffer : IDisposable + { + protected IndexElementSize elementSize; - #region SetData - public void SetData(GraphicsDevice graphicsDevice, S[] data) where S : struct - { - SetData(graphicsDevice, data, 0, data.Length); - } + #region SetData + public void SetData(GraphicsDevice graphicsDevice, S[] data) where S : struct + { + SetData(graphicsDevice, data, 0, data.Length); + } - public void SetData(GraphicsDevice graphicsDevice, S[] data, int startIndex, int elementCount) where S : struct - { - SetData(graphicsDevice, 0, data, startIndex, elementCount); - } + public void SetData(GraphicsDevice graphicsDevice, S[] data, int startIndex, int elementCount) where S : struct + { + SetData(graphicsDevice, 0, data, startIndex, elementCount); + } - public void SetData(GraphicsDevice graphicsDevice, int offsetInBytes, S[] data, int startIndex, int elementCount) - where S : struct - { - //TODO: check offsetInBytes parameter for bounds etc. + public void SetData(GraphicsDevice graphicsDevice, int offsetInBytes, S[] data, int startIndex, int elementCount) + where S : struct + { + if (offsetInBytes + elementCount * Marshal.SizeOf(typeof(S)) > NativeBuffer.Description.SizeInBytes) + throw new ArgumentOutOfRangeException(string.Format("The offset by \"{0}\" plus the byte length described by \"{1}\" is over the bounds of the buffer.", "offsetInBytes", "elementCount")); - using (var stream = MapBufferWrite()) - { - if (offsetInBytes > 0) - stream.Seek(offsetInBytes, SeekOrigin.Current); + using (var stream = MapBufferWrite()) + { + if (offsetInBytes > 0) + stream.Seek(offsetInBytes, SeekOrigin.Current); - if (startIndex > 0 || elementCount < data.Length) - for (int i = startIndex; i < startIndex + elementCount; i++) - stream.Write(data[i]); - else - for (int i = 0; i < data.Length; i++) - stream.Write(data[i]); + if (startIndex > 0 || elementCount < data.Length) + for (int i = startIndex; i < startIndex + elementCount; i++) + stream.Write(data[i]); + else + for (int i = 0; i < data.Length; i++) + stream.Write(data[i]); - UnmapBuffer(); - } - } - #endregion + UnmapBuffer(); + } + } + #endregion - #region GetData - public void GetData(S[] data) where S : struct - { - GetData(0, data, 0, data.Length); - } + #region GetData + public void GetData(S[] data) where S : struct + { + GetData(0, data, 0, data.Length); + } - public void GetData(S[] data, int startIndex, int elementCount) where S : struct - { - GetData(0, data, 0, data.Length); - } + public void GetData(S[] data, int startIndex, int elementCount) where S : struct + { + GetData(0, data, 0, data.Length); + } - public void GetData(int offsetInBytes, S[] data, int startIndex, int elementCount) where S : struct - { - using (var stream = MapBufferRead()) - { - if (offsetInBytes > 0) - stream.Seek(offsetInBytes, SeekOrigin.Current); + public void GetData(int offsetInBytes, S[] data, int startIndex, int elementCount) where S : struct + { + if (data == null) + throw new ArgumentNullException("data"); - stream.ReadRange(data, startIndex, elementCount); - UnmapBuffer(); - } - } - #endregion + var stagingBuffer = CreateStagingBuffer(); + CopySubresource(NativeBuffer, stagingBuffer); - protected int GetSizeInBytes(int indexCount) - { - return (elementSize == IndexElementSize.SixteenBits ? 2 : 4) * indexCount; - } + using (var stream = MapBufferRead(stagingBuffer)) + { + if (offsetInBytes > 0) + stream.Seek(offsetInBytes, SeekOrigin.Current); - #region Dispose - public void Dispose() - { - if (NativeBuffer != null) - { - NativeBuffer.Dispose(); - NativeBuffer = null; - } - } - #endregion - } + stream.ReadRange(data, startIndex, elementCount); + UnmapBuffer(stagingBuffer); + } + } + #endregion + + protected int GetSizeInBytes(int indexCount) + { + return (elementSize == IndexElementSize.SixteenBits ? 2 : 4) * indexCount; + } + + #region Dispose + public void Dispose() + { + if (NativeBuffer != null) + { + NativeBuffer.Dispose(); + NativeBuffer = null; + } + } + #endregion + } } diff --git a/RenderSystems/ANX.RenderSystem.DX.SharedSources/SharedVertexBuffer.cs b/RenderSystems/ANX.RenderSystem.DX.SharedSources/SharedVertexBuffer.cs index 74a732ee..bbed41ae 100644 --- a/RenderSystems/ANX.RenderSystem.DX.SharedSources/SharedVertexBuffer.cs +++ b/RenderSystems/ANX.RenderSystem.DX.SharedSources/SharedVertexBuffer.cs @@ -10,6 +10,8 @@ using SharpDX; // "ANX.Framework developer group" and released under the Ms-PL license. // For details see: http://anxframework.codeplex.com/license +using System.Runtime.InteropServices; + #if DX10 using Dx = SharpDX.Direct3D10; using DxDevice = SharpDX.Direct3D10.Device; @@ -23,86 +25,106 @@ using DxDevice = SharpDX.Direct3D11.Device; namespace ANX.RenderSystem.Windows.DX11 #endif { - public partial class DxVertexBuffer : IDisposable - { - private int vertexStride; + public partial class DxVertexBuffer : IDisposable + { + private int vertexStride; - #region SetData - public void SetData(GraphicsDevice graphicsDevice, S[] data) where S : struct - { - SetData(graphicsDevice, data, 0, data.Length); - } + #region SetData + public void SetData(GraphicsDevice graphicsDevice, S[] data) where S : struct + { + if (data == null) + throw new ArgumentNullException("data"); - public void SetData(GraphicsDevice graphicsDevice, S[] data, int startIndex, int elementCount) where S : struct - { - SetData(graphicsDevice, 0, data, startIndex, elementCount); - } + SetData(graphicsDevice, data, 0, data.Length); + } - public void SetData(GraphicsDevice graphicsDevice, int offsetInBytes, S[] data, int startIndex, int elementCount) - where S : struct - { - //TODO: check offsetInBytes parameter for bounds etc. + public void SetData(GraphicsDevice graphicsDevice, S[] data, int startIndex, int elementCount) where S : struct + { + SetData(graphicsDevice, 0, data, startIndex, elementCount); + } - using (var stream = MapBufferWrite()) - { - if (offsetInBytes > 0) - stream.Seek(offsetInBytes, SeekOrigin.Current); + public void SetData(GraphicsDevice graphicsDevice, int offsetInBytes, S[] data, int startIndex, int elementCount) + where S : struct + { + if (data == null) + throw new ArgumentNullException("data"); - if (startIndex > 0 || elementCount < data.Length) - for (int i = startIndex; i < startIndex + elementCount; i++) - stream.Write(data[i]); - else - for (int i = 0; i < data.Length; i++) - stream.Write(data[i]); + if (startIndex + elementCount > data.Length) + throw new ArgumentOutOfRangeException("startIndex must be smaller than elementCount + data.Length."); - UnmapBuffer(); - } - } + if (offsetInBytes + elementCount * Marshal.SizeOf(typeof(S)) > NativeBuffer.Description.SizeInBytes) + throw new ArgumentOutOfRangeException(string.Format("The offset by \"{0}\" plus the byte length described by \"{1}\" is over the bounds of the buffer.", "offsetInBytes", "elementCount")); - public void SetData(GraphicsDevice graphicsDevice, int offsetInBytes, S[] data, int startIndex, int elementCount, - int vertexStride) where S : struct - { - throw new NotImplementedException(); - } - #endregion + using (var stream = MapBufferWrite()) + { + if (offsetInBytes > 0) + stream.Seek(offsetInBytes, SeekOrigin.Current); - #region GetData - public void GetData(S[] data) where S : struct - { - GetData(data, 0, data.Length); - } + if (startIndex > 0 || elementCount < data.Length) + for (int i = startIndex; i < startIndex + elementCount; i++) + stream.Write(data[i]); + else + for (int i = 0; i < data.Length; i++) + stream.Write(data[i]); - public void GetData(S[] data, int startIndex, int elementCount) where S : struct - { - GetData(0, data, startIndex, elementCount, vertexStride); - } + UnmapBuffer(); + } + } - public void GetData(int offsetInBytes, S[] data, int startIndex, int elementCount, int vertexStride) - where S : struct - { - Dx.Buffer stagingBuffer = CreateStagingBuffer(elementCount * vertexStride); + public void SetData(GraphicsDevice graphicsDevice, int offsetInBytes, S[] data, int startIndex, int elementCount, + int vertexStride) where S : struct + { + throw new NotImplementedException(); + } + #endregion + + #region GetData + public void GetData(S[] data) where S : struct + { + if (data == null) + throw new ArgumentNullException("data"); + + GetData(data, 0, data.Length); + } + + public void GetData(S[] data, int startIndex, int elementCount) where S : struct + { + GetData(0, data, startIndex, elementCount, vertexStride); + } + + public void GetData(int offsetInBytes, S[] data, int startIndex, int elementCount, int vertexStride) + where S : struct + { + if (data == null) + throw new ArgumentNullException("data"); + + if (startIndex + elementCount > data.Length) + throw new ArgumentOutOfRangeException("startIndex must be smaller than elementCount + data.Length."); + + //TODO: Create a staging buffer only with the needed size that correctly handles startIndex and offsetInBytes. + Dx.Buffer stagingBuffer = CreateStagingBuffer(); CopySubresource(NativeBuffer, stagingBuffer); - using (var stream = MapBufferRead(stagingBuffer)) - { - if (offsetInBytes > 0) - stream.Seek(offsetInBytes, SeekOrigin.Current); + using (var stream = MapBufferRead(stagingBuffer)) + { + if (offsetInBytes > 0) + stream.Seek(offsetInBytes, SeekOrigin.Current); - stream.ReadRange(data, startIndex, elementCount); - UnmapBuffer(stagingBuffer); - } - } - #endregion + stream.ReadRange(data, startIndex, elementCount); + UnmapBuffer(stagingBuffer); + } + } + #endregion - #region Dispose - public void Dispose() - { - if (NativeBuffer != null) - { - NativeBuffer.Dispose(); - NativeBuffer = null; - } - } - #endregion - } + #region Dispose + public void Dispose() + { + if (NativeBuffer != null) + { + NativeBuffer.Dispose(); + NativeBuffer = null; + } + } + #endregion + } } diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX10/ANX.RenderSystem.Windows.DX10.csproj b/RenderSystems/ANX.RenderSystem.Windows.DX10/ANX.RenderSystem.Windows.DX10.csproj index f7cb5673..55c42425 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX10/ANX.RenderSystem.Windows.DX10.csproj +++ b/RenderSystems/ANX.RenderSystem.Windows.DX10/ANX.RenderSystem.Windows.DX10.csproj @@ -57,6 +57,9 @@ + + BufferHelper.cs + diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX10/DxIndexBuffer.cs b/RenderSystems/ANX.RenderSystem.Windows.DX10/DxIndexBuffer.cs index 54d1e969..498e7ac2 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX10/DxIndexBuffer.cs +++ b/RenderSystems/ANX.RenderSystem.Windows.DX10/DxIndexBuffer.cs @@ -14,58 +14,83 @@ using Dx10 = SharpDX.Direct3D10; namespace ANX.RenderSystem.Windows.DX10 { - public partial class DxIndexBuffer : INativeIndexBuffer, IDisposable - { + public partial class DxIndexBuffer : INativeIndexBuffer, IDisposable + { public Dx10.Buffer NativeBuffer { get; protected set; } - #region Constructor - public DxIndexBuffer(GraphicsDevice graphics, IndexElementSize size, int indexCount, BufferUsage usage) - { - elementSize = size; - GraphicsDeviceDX gd10 = graphics.NativeDevice as GraphicsDeviceDX; - Dx10.Device device = gd10 != null ? gd10.NativeDevice as Dx10.Device : null; + #region Constructor + public DxIndexBuffer(GraphicsDevice graphics, IndexElementSize size, int indexCount, BufferUsage usage) + { + elementSize = size; + GraphicsDeviceDX gd10 = graphics.NativeDevice as GraphicsDeviceDX; + Dx10.Device device = gd10 != null ? gd10.NativeDevice as Dx10.Device : null; - InitializeBuffer(device, size, indexCount, usage); - } + InitializeBuffer(device, size, indexCount, usage); + } - internal DxIndexBuffer(Dx10.Device device, IndexElementSize size, int indexCount, BufferUsage usage) - { - elementSize = size; - InitializeBuffer(device, size, indexCount, usage); - } - #endregion + internal DxIndexBuffer(Dx10.Device device, IndexElementSize size, int indexCount, BufferUsage usage) + { + elementSize = size; + InitializeBuffer(device, size, indexCount, usage); + } + #endregion - #region InitializeBuffer - private void InitializeBuffer(Dx10.Device device, IndexElementSize size, int indexCount, BufferUsage usage) - { - //TODO: translate and use usage - var description = new Dx10.BufferDescription() - { - Usage = Dx10.ResourceUsage.Dynamic, - SizeInBytes = GetSizeInBytes(indexCount), - BindFlags = Dx10.BindFlags.IndexBuffer, - CpuAccessFlags = Dx10.CpuAccessFlags.Write, - OptionFlags = Dx10.ResourceOptionFlags.None - }; + #region InitializeBuffer + private void InitializeBuffer(Dx10.Device device, IndexElementSize size, int indexCount, BufferUsage usage) + { + //TODO: translate and use usage + var description = new Dx10.BufferDescription() + { + Usage = Dx10.ResourceUsage.Dynamic, + SizeInBytes = GetSizeInBytes(indexCount), + BindFlags = Dx10.BindFlags.IndexBuffer, + CpuAccessFlags = Dx10.CpuAccessFlags.Write, + OptionFlags = Dx10.ResourceOptionFlags.None + }; - NativeBuffer = new Dx10.Buffer(device, description); - //NativeBuffer.Unmap(); - } - #endregion + NativeBuffer = new Dx10.Buffer(device, description); + //NativeBuffer.Unmap(); + } + #endregion - protected DataStream MapBufferWrite() - { - return NativeBuffer.Map(Dx10.MapMode.WriteDiscard); - } + protected DataStream MapBufferWrite() + { + return NativeBuffer.Map(Dx10.MapMode.WriteDiscard); + } - protected DataStream MapBufferRead() - { - return NativeBuffer.Map(Dx10.MapMode.Read); - } + private SharpDX.DataStream MapBufferRead(Dx10.Buffer buffer) + { + return buffer.Map(Dx10.MapMode.Read); + } - protected void UnmapBuffer() - { - NativeBuffer.Unmap(); - } - } + protected void UnmapBuffer(Dx10.Buffer buffer) + { + buffer.Unmap(); + } + + protected void UnmapBuffer() + { + NativeBuffer.Unmap(); + } + + private void CopySubresource(Dx10.Buffer source, Dx10.Buffer destination) + { + BufferHelper.ValidateCopyResource(source, destination); + + this.NativeBuffer.Device.CopyResource(source, destination); + } + + private Dx10.Buffer CreateStagingBuffer() + { + var description = new Dx10.BufferDescription() + { + Usage = Dx10.ResourceUsage.Staging, + SizeInBytes = NativeBuffer.Description.SizeInBytes, + CpuAccessFlags = Dx10.CpuAccessFlags.Read, + OptionFlags = Dx10.ResourceOptionFlags.None, + }; + + return new Dx10.Buffer(NativeBuffer.Device, description); + } + } } diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX10/DxVertexBuffer.cs b/RenderSystems/ANX.RenderSystem.Windows.DX10/DxVertexBuffer.cs index 67173860..983d4d27 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX10/DxVertexBuffer.cs +++ b/RenderSystems/ANX.RenderSystem.Windows.DX10/DxVertexBuffer.cs @@ -23,69 +23,64 @@ using DxDevice = SharpDX.Direct3D11.Device; namespace ANX.RenderSystem.Windows.DX11 #endif { - public partial class DxVertexBuffer : INativeVertexBuffer, IDisposable - { + public partial class DxVertexBuffer : INativeVertexBuffer, IDisposable + { public Dx.Buffer NativeBuffer { get; protected set; } private Dx.Device device; - #region Constructor - public DxVertexBuffer(GraphicsDevice graphics, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) - { - GraphicsDeviceDX gd10 = graphics.NativeDevice as GraphicsDeviceDX; - this.device = gd10 != null ? gd10.NativeDevice as Dx.Device : null; + #region Constructor + public DxVertexBuffer(GraphicsDevice graphics, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) + { + GraphicsDeviceDX gd10 = graphics.NativeDevice as GraphicsDeviceDX; + this.device = gd10 != null ? gd10.NativeDevice as Dx.Device : null; - InitializeBuffer(device, vertexDeclaration, vertexCount, usage); - } + InitializeBuffer(device, vertexDeclaration, vertexCount, usage); + } - internal DxVertexBuffer(Dx.Device device, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) - { - InitializeBuffer(device, vertexDeclaration, vertexCount, usage); - } - #endregion + internal DxVertexBuffer(Dx.Device device, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) + { + InitializeBuffer(device, vertexDeclaration, vertexCount, usage); + } + #endregion - #region InitializeBuffer - private void InitializeBuffer(Dx.Device device, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) - { - this.vertexStride = vertexDeclaration.VertexStride; + #region InitializeBuffer + private void InitializeBuffer(Dx.Device device, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) + { + this.vertexStride = vertexDeclaration.VertexStride; - //TODO: translate and use usage + //TODO: translate and use usage - if (device != null) - { - var description = new Dx.BufferDescription() - { - Usage = Dx.ResourceUsage.Dynamic, - SizeInBytes = vertexDeclaration.VertexStride * vertexCount, - BindFlags = Dx.BindFlags.VertexBuffer, - CpuAccessFlags = Dx.CpuAccessFlags.Write, - OptionFlags = Dx.ResourceOptionFlags.None - }; + if (device != null) + { + var description = new Dx.BufferDescription() + { + Usage = Dx.ResourceUsage.Dynamic, + SizeInBytes = vertexDeclaration.VertexStride * vertexCount, + BindFlags = Dx.BindFlags.VertexBuffer, + CpuAccessFlags = Dx.CpuAccessFlags.Write, + OptionFlags = Dx.ResourceOptionFlags.None + }; - NativeBuffer = new Dx.Buffer(device, description); - //NativeBuffer.Unmap(); + NativeBuffer = new Dx.Buffer(device, description); + //NativeBuffer.Unmap(); } - } - #endregion + } + #endregion - private DataStream MapBufferWrite() - { - return NativeBuffer.Map(Dx.MapMode.WriteDiscard); - } - - private DataStream MapBufferRead() - { - return NativeBuffer.Map(Dx.MapMode.Read); - } + private DataStream MapBufferWrite() + { + return NativeBuffer.Map(Dx.MapMode.WriteDiscard); + } private SharpDX.DataStream MapBufferRead(Dx.Buffer buffer) { return buffer.Map(Dx.MapMode.ReadWrite); } - private void UnmapBuffer() - { - NativeBuffer.Unmap(); - } + private void UnmapBuffer() + { + NativeBuffer.Unmap(); + } private void UnmapBuffer(Dx.Buffer buffer) { @@ -97,17 +92,17 @@ namespace ANX.RenderSystem.Windows.DX11 this.device.CopyResource(source, destination); } - private Dx.Buffer CreateStagingBuffer(int sizeInBytes) + private Dx.Buffer CreateStagingBuffer() { var description = new Dx.BufferDescription() { Usage = Dx.ResourceUsage.Staging, - SizeInBytes = sizeInBytes, + SizeInBytes = NativeBuffer.Description.SizeInBytes, CpuAccessFlags = Dx.CpuAccessFlags.Read | Dx.CpuAccessFlags.Write, OptionFlags = Dx.ResourceOptionFlags.None }; return new Dx.Buffer(device, description); } - } + } } diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX11/ANX.RenderSystem.Windows.DX11.csproj b/RenderSystems/ANX.RenderSystem.Windows.DX11/ANX.RenderSystem.Windows.DX11.csproj index 21729157..a7a00b36 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX11/ANX.RenderSystem.Windows.DX11.csproj +++ b/RenderSystems/ANX.RenderSystem.Windows.DX11/ANX.RenderSystem.Windows.DX11.csproj @@ -70,6 +70,9 @@ + + BufferHelper.cs + diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX11/DxIndexBuffer.cs b/RenderSystems/ANX.RenderSystem.Windows.DX11/DxIndexBuffer.cs index 8e486427..ab28a63e 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX11/DxIndexBuffer.cs +++ b/RenderSystems/ANX.RenderSystem.Windows.DX11/DxIndexBuffer.cs @@ -15,64 +15,90 @@ using Dx11 = SharpDX.Direct3D11; namespace ANX.RenderSystem.Windows.DX11 { public partial class DxIndexBuffer : INativeIndexBuffer, IDisposable - { + { public Dx11.Buffer NativeBuffer { get; protected set; } - #region Constructor - public DxIndexBuffer(GraphicsDevice graphics, IndexElementSize size, int indexCount, BufferUsage usage) - { - elementSize = size; - GraphicsDeviceDX gd11 = graphics.NativeDevice as GraphicsDeviceDX; - Dx11.DeviceContext context = gd11 != null ? gd11.NativeDevice as Dx11.DeviceContext : null; + #region Constructor + public DxIndexBuffer(GraphicsDevice graphics, IndexElementSize size, int indexCount, BufferUsage usage) + { + elementSize = size; + GraphicsDeviceDX gd11 = graphics.NativeDevice as GraphicsDeviceDX; + Dx11.DeviceContext context = gd11 != null ? gd11.NativeDevice as Dx11.DeviceContext : null; - InitializeBuffer(context.Device, size, indexCount, usage); - } + InitializeBuffer(context.Device, size, indexCount, usage); + } - internal DxIndexBuffer(SharpDX.Direct3D11.Device device, IndexElementSize size, int indexCount, BufferUsage usage) - { - elementSize = size; - InitializeBuffer(device, size, indexCount, usage); - } - #endregion + internal DxIndexBuffer(SharpDX.Direct3D11.Device device, IndexElementSize size, int indexCount, BufferUsage usage) + { + elementSize = size; + InitializeBuffer(device, size, indexCount, usage); + } + #endregion - #region InitializeBuffer - private void InitializeBuffer(Dx11.Device device, IndexElementSize size, int indexCount, BufferUsage usage) - { - //TODO: translate and use usage - var description = new Dx11.BufferDescription() - { - // TODO: translate usage - Usage = Dx11.ResourceUsage.Dynamic, - SizeInBytes = GetSizeInBytes(indexCount), - BindFlags = Dx11.BindFlags.IndexBuffer, - CpuAccessFlags = Dx11.CpuAccessFlags.Write, - OptionFlags = Dx11.ResourceOptionFlags.None - }; + #region InitializeBuffer + private void InitializeBuffer(Dx11.Device device, IndexElementSize size, int indexCount, BufferUsage usage) + { + //TODO: translate and use usage + var description = new Dx11.BufferDescription() + { + // TODO: translate usage + Usage = Dx11.ResourceUsage.Dynamic, + SizeInBytes = GetSizeInBytes(indexCount), + BindFlags = Dx11.BindFlags.IndexBuffer, + CpuAccessFlags = Dx11.CpuAccessFlags.Write, + OptionFlags = Dx11.ResourceOptionFlags.None + }; - NativeBuffer = new SharpDX.Direct3D11.Buffer(device, description); - } - #endregion + NativeBuffer = new SharpDX.Direct3D11.Buffer(device, description); + } + #endregion - protected DataStream MapBufferWrite() - { - Dx11.DeviceContext context = NativeBuffer.Device.ImmediateContext; - DataStream stream; - context.MapSubresource(NativeBuffer, Dx11.MapMode.WriteDiscard, Dx11.MapFlags.None, out stream); - return stream; - } + protected DataStream MapBufferWrite() + { + Dx11.DeviceContext context = NativeBuffer.Device.ImmediateContext; + DataStream stream; + context.MapSubresource(NativeBuffer, Dx11.MapMode.WriteDiscard, Dx11.MapFlags.None, out stream); + return stream; + } - protected DataStream MapBufferRead() - { - Dx11.DeviceContext context = NativeBuffer.Device.ImmediateContext; - DataStream stream; - context.MapSubresource(NativeBuffer, Dx11.MapMode.Read, Dx11.MapFlags.None, out stream); - return stream; - } + protected void UnmapBuffer() + { + Dx11.DeviceContext context = NativeBuffer.Device.ImmediateContext; + context.UnmapSubresource(NativeBuffer, 0); + } - protected void UnmapBuffer() - { - Dx11.DeviceContext context = NativeBuffer.Device.ImmediateContext; - context.UnmapSubresource(NativeBuffer, 0); - } - } + private SharpDX.DataStream MapBufferRead(Dx11.Buffer buffer) + { + Dx11.DeviceContext context = buffer.Device.ImmediateContext; + DataStream stream; + context.MapSubresource(buffer, Dx11.MapMode.Read, Dx11.MapFlags.None, out stream); + return stream; + } + + private void UnmapBuffer(Dx11.Buffer buffer) + { + Dx11.DeviceContext context = buffer.Device.ImmediateContext; + context.UnmapSubresource(buffer, 0); + } + + private void CopySubresource(Dx11.Buffer source, Dx11.Buffer destination) + { + BufferHelper.ValidateCopyResource(source, destination); + + this.NativeBuffer.Device.ImmediateContext.CopyResource(source, destination); + } + + private Dx11.Buffer CreateStagingBuffer() + { + var description = new Dx11.BufferDescription() + { + Usage = Dx11.ResourceUsage.Staging, + SizeInBytes = NativeBuffer.Description.SizeInBytes, + CpuAccessFlags = Dx11.CpuAccessFlags.Read, + OptionFlags = Dx11.ResourceOptionFlags.None, + }; + + return new Dx11.Buffer(NativeBuffer.Device, description); + } + } } diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX11/DxVertexBuffer.cs b/RenderSystems/ANX.RenderSystem.Windows.DX11/DxVertexBuffer.cs index 95b52604..5c707fd1 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX11/DxVertexBuffer.cs +++ b/RenderSystems/ANX.RenderSystem.Windows.DX11/DxVertexBuffer.cs @@ -23,64 +23,56 @@ using DxDevice = SharpDX.Direct3D11.Device; namespace ANX.RenderSystem.Windows.DX11 #endif { - public partial class DxVertexBuffer : INativeVertexBuffer, IDisposable - { + public partial class DxVertexBuffer : INativeVertexBuffer, IDisposable + { public Dx.Buffer NativeBuffer { get; protected set; } private Dx.DeviceContext context; - #region Constructor - public DxVertexBuffer(GraphicsDevice graphics, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) - { - var gd11 = graphics.NativeDevice as GraphicsDeviceDX; - this.context = gd11 != null ? gd11.NativeDevice as Dx.DeviceContext : null; + #region Constructor + public DxVertexBuffer(GraphicsDevice graphics, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) + { + var gd11 = graphics.NativeDevice as GraphicsDeviceDX; + this.context = gd11 != null ? gd11.NativeDevice as Dx.DeviceContext : null; - InitializeBuffer(context.Device, vertexDeclaration, vertexCount, usage); - } + InitializeBuffer(context.Device, vertexDeclaration, vertexCount, usage); + } - internal DxVertexBuffer(Dx.Device device, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) - { - InitializeBuffer(device, vertexDeclaration, vertexCount, usage); - } - #endregion + internal DxVertexBuffer(Dx.Device device, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) + { + InitializeBuffer(device, vertexDeclaration, vertexCount, usage); + } + #endregion - #region InitializeBuffer (TODO) - private void InitializeBuffer(Dx.Device device, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) - { - vertexStride = vertexDeclaration.VertexStride; + #region InitializeBuffer (TODO) + private void InitializeBuffer(Dx.Device device, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) + { + vertexStride = vertexDeclaration.VertexStride; - //TODO: translate and use usage + //TODO: translate and use usage - if (device != null) - { - var description = new Dx.BufferDescription() - { - Usage = Dx.ResourceUsage.Dynamic, - SizeInBytes = vertexDeclaration.VertexStride * vertexCount, - BindFlags = Dx.BindFlags.VertexBuffer, - CpuAccessFlags = Dx.CpuAccessFlags.Write, - OptionFlags = Dx.ResourceOptionFlags.None - }; + if (device != null) + { + var description = new Dx.BufferDescription() + { + Usage = Dx.ResourceUsage.Dynamic, + SizeInBytes = vertexDeclaration.VertexStride * vertexCount, + BindFlags = Dx.BindFlags.VertexBuffer, + CpuAccessFlags = Dx.CpuAccessFlags.Write, + OptionFlags = Dx.ResourceOptionFlags.None + }; - NativeBuffer = new Dx.Buffer(device, description); - } - } - #endregion + NativeBuffer = new Dx.Buffer(device, description); + } + } + #endregion - private SharpDX.DataStream MapBufferWrite() - { - Dx.DeviceContext context = NativeBuffer.Device.ImmediateContext; - DataStream stream; - context.MapSubresource(NativeBuffer, Dx.MapMode.WriteDiscard, Dx.MapFlags.None, out stream); - return stream; - } - - private SharpDX.DataStream MapBufferRead() - { - Dx.DeviceContext context = NativeBuffer.Device.ImmediateContext; - DataStream stream; - context.MapSubresource(NativeBuffer, Dx.MapMode.Read, Dx.MapFlags.None, out stream); - return stream; - } + private SharpDX.DataStream MapBufferWrite() + { + Dx.DeviceContext context = NativeBuffer.Device.ImmediateContext; + DataStream stream; + context.MapSubresource(NativeBuffer, Dx.MapMode.WriteDiscard, Dx.MapFlags.None, out stream); + return stream; + } private SharpDX.DataStream MapBufferRead(Dx.Resource buffer) { @@ -89,14 +81,16 @@ namespace ANX.RenderSystem.Windows.DX11 return stream; } - private void UnmapBuffer() - { - Dx.DeviceContext context = NativeBuffer.Device.ImmediateContext; - context.UnmapSubresource(NativeBuffer, 0); - } + private void UnmapBuffer() + { + Dx.DeviceContext context = NativeBuffer.Device.ImmediateContext; + context.UnmapSubresource(NativeBuffer, 0); + } private void CopySubresource(Dx.Buffer source, Dx.Buffer destination) { + BufferHelper.ValidateCopyResource(source, destination); + this.context.CopyResource(source, destination); } @@ -105,18 +99,18 @@ namespace ANX.RenderSystem.Windows.DX11 buffer.Device.ImmediateContext.UnmapSubresource(buffer, 0); } - private Dx.Buffer CreateStagingBuffer(int sizeInBytes) + private Dx.Buffer CreateStagingBuffer() { var description = new Dx.BufferDescription() { Usage = Dx.ResourceUsage.Staging, - SizeInBytes = sizeInBytes, + SizeInBytes = NativeBuffer.Description.SizeInBytes, BindFlags = Dx.BindFlags.VertexBuffer, - CpuAccessFlags = Dx.CpuAccessFlags.Write | Dx.CpuAccessFlags.Read, + CpuAccessFlags = Dx.CpuAccessFlags.Read, OptionFlags = Dx.ResourceOptionFlags.None }; return new Dx.Buffer(context.Device, description); } - } + } }