Made GetData for VertexBuffer and IndexBuffer work for DirectX10 and DirectX11. Also added a few parameter checks.
This commit is contained in:
parent
eb1af4eec4
commit
252e337375
@ -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.");
|
||||
}
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ using System;
|
||||
using ANX.Framework.Graphics;
|
||||
using SharpDX;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
#endregion
|
||||
|
||||
@ -12,8 +13,7 @@ using System.IO;
|
||||
|
||||
#if DX10
|
||||
namespace ANX.RenderSystem.Windows.DX10
|
||||
#endif
|
||||
#if DX11
|
||||
#elif DX11
|
||||
namespace ANX.RenderSystem.Windows.DX11
|
||||
#endif
|
||||
{
|
||||
@ -35,7 +35,8 @@ namespace ANX.RenderSystem.Windows.DX11
|
||||
public void SetData<S>(GraphicsDevice graphicsDevice, int offsetInBytes, S[] data, int startIndex, int elementCount)
|
||||
where S : struct
|
||||
{
|
||||
//TODO: check offsetInBytes parameter for bounds etc.
|
||||
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())
|
||||
{
|
||||
@ -67,13 +68,19 @@ namespace ANX.RenderSystem.Windows.DX11
|
||||
|
||||
public void GetData<S>(int offsetInBytes, S[] data, int startIndex, int elementCount) where S : struct
|
||||
{
|
||||
using (var stream = MapBufferRead())
|
||||
if (data == null)
|
||||
throw new ArgumentNullException("data");
|
||||
|
||||
var stagingBuffer = CreateStagingBuffer();
|
||||
CopySubresource(NativeBuffer, stagingBuffer);
|
||||
|
||||
using (var stream = MapBufferRead(stagingBuffer))
|
||||
{
|
||||
if (offsetInBytes > 0)
|
||||
stream.Seek(offsetInBytes, SeekOrigin.Current);
|
||||
|
||||
stream.ReadRange(data, startIndex, elementCount);
|
||||
UnmapBuffer();
|
||||
UnmapBuffer(stagingBuffer);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
@ -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;
|
||||
@ -30,6 +32,9 @@ namespace ANX.RenderSystem.Windows.DX11
|
||||
#region SetData
|
||||
public void SetData<S>(GraphicsDevice graphicsDevice, S[] data) where S : struct
|
||||
{
|
||||
if (data == null)
|
||||
throw new ArgumentNullException("data");
|
||||
|
||||
SetData<S>(graphicsDevice, data, 0, data.Length);
|
||||
}
|
||||
|
||||
@ -41,7 +46,14 @@ namespace ANX.RenderSystem.Windows.DX11
|
||||
public void SetData<S>(GraphicsDevice graphicsDevice, int offsetInBytes, S[] data, int startIndex, int elementCount)
|
||||
where S : struct
|
||||
{
|
||||
//TODO: check offsetInBytes parameter for bounds etc.
|
||||
if (data == null)
|
||||
throw new ArgumentNullException("data");
|
||||
|
||||
if (startIndex + elementCount > data.Length)
|
||||
throw new ArgumentOutOfRangeException("startIndex must be smaller than elementCount + data.Length.");
|
||||
|
||||
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())
|
||||
{
|
||||
@ -69,6 +81,9 @@ namespace ANX.RenderSystem.Windows.DX11
|
||||
#region GetData
|
||||
public void GetData<S>(S[] data) where S : struct
|
||||
{
|
||||
if (data == null)
|
||||
throw new ArgumentNullException("data");
|
||||
|
||||
GetData(data, 0, data.Length);
|
||||
}
|
||||
|
||||
@ -80,7 +95,14 @@ namespace ANX.RenderSystem.Windows.DX11
|
||||
public void GetData<S>(int offsetInBytes, S[] data, int startIndex, int elementCount, int vertexStride)
|
||||
where S : struct
|
||||
{
|
||||
Dx.Buffer stagingBuffer = CreateStagingBuffer(elementCount * vertexStride);
|
||||
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))
|
||||
|
@ -57,6 +57,9 @@
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="..\ANX.RenderSystem.DX.SharedSources\BufferHelper.cs">
|
||||
<Link>BufferHelper.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="BlendState_DX10.cs" />
|
||||
<Compile Include="Creator.cs" />
|
||||
<Compile Include="DepthStencilState_DX10.cs" />
|
||||
|
@ -58,14 +58,39 @@ namespace ANX.RenderSystem.Windows.DX10
|
||||
return NativeBuffer.Map(Dx10.MapMode.WriteDiscard);
|
||||
}
|
||||
|
||||
protected DataStream MapBufferRead()
|
||||
private SharpDX.DataStream MapBufferRead(Dx10.Buffer buffer)
|
||||
{
|
||||
return NativeBuffer.Map(Dx10.MapMode.Read);
|
||||
return buffer.Map(Dx10.MapMode.Read);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -72,11 +72,6 @@ namespace ANX.RenderSystem.Windows.DX11
|
||||
return NativeBuffer.Map(Dx.MapMode.WriteDiscard);
|
||||
}
|
||||
|
||||
private DataStream MapBufferRead()
|
||||
{
|
||||
return NativeBuffer.Map(Dx.MapMode.Read);
|
||||
}
|
||||
|
||||
private SharpDX.DataStream MapBufferRead(Dx.Buffer buffer)
|
||||
{
|
||||
return buffer.Map(Dx.MapMode.ReadWrite);
|
||||
@ -97,12 +92,12 @@ 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
|
||||
};
|
||||
|
@ -70,6 +70,9 @@
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="..\ANX.RenderSystem.DX.SharedSources\BufferHelper.cs">
|
||||
<Link>BufferHelper.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="BlendState_DX11.cs" />
|
||||
<Compile Include="Creator.cs" />
|
||||
<Compile Include="DepthStencilState_DX11.cs" />
|
||||
|
@ -61,18 +61,44 @@ namespace ANX.RenderSystem.Windows.DX11
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -74,14 +74,6 @@ namespace ANX.RenderSystem.Windows.DX11
|
||||
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 MapBufferRead(Dx.Resource buffer)
|
||||
{
|
||||
DataStream stream;
|
||||
@ -97,6 +89,8 @@ namespace ANX.RenderSystem.Windows.DX11
|
||||
|
||||
private void CopySubresource(Dx.Buffer source, Dx.Buffer destination)
|
||||
{
|
||||
BufferHelper.ValidateCopyResource(source, destination);
|
||||
|
||||
this.context.CopyResource(source, destination);
|
||||
}
|
||||
|
||||
@ -105,14 +99,14 @@ 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
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user