Made GetData for VertexBuffer and IndexBuffer work for DirectX10 and DirectX11. Also added a few parameter checks.

This commit is contained in:
Konstantin Koch 2015-01-18 13:10:50 +00:00 committed by Konstantin Koch
parent eb1af4eec4
commit 252e337375
9 changed files with 440 additions and 337 deletions

View File

@ -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.");
}
}
}

View File

@ -3,6 +3,7 @@ using System;
using ANX.Framework.Graphics; using ANX.Framework.Graphics;
using SharpDX; using SharpDX;
using System.IO; using System.IO;
using System.Runtime.InteropServices;
#endregion #endregion
@ -12,86 +13,92 @@ using System.IO;
#if DX10 #if DX10
namespace ANX.RenderSystem.Windows.DX10 namespace ANX.RenderSystem.Windows.DX10
#endif #elif DX11
#if DX11
namespace ANX.RenderSystem.Windows.DX11 namespace ANX.RenderSystem.Windows.DX11
#endif #endif
{ {
public partial class DxIndexBuffer : IDisposable public partial class DxIndexBuffer : IDisposable
{ {
protected IndexElementSize elementSize; protected IndexElementSize elementSize;
#region SetData #region SetData
public void SetData<S>(GraphicsDevice graphicsDevice, S[] data) where S : struct public void SetData<S>(GraphicsDevice graphicsDevice, S[] data) where S : struct
{ {
SetData<S>(graphicsDevice, data, 0, data.Length); SetData<S>(graphicsDevice, data, 0, data.Length);
} }
public void SetData<S>(GraphicsDevice graphicsDevice, S[] data, int startIndex, int elementCount) where S : struct public void SetData<S>(GraphicsDevice graphicsDevice, S[] data, int startIndex, int elementCount) where S : struct
{ {
SetData<S>(graphicsDevice, 0, data, startIndex, elementCount); SetData<S>(graphicsDevice, 0, data, startIndex, elementCount);
} }
public void SetData<S>(GraphicsDevice graphicsDevice, int offsetInBytes, S[] data, int startIndex, int elementCount) public void SetData<S>(GraphicsDevice graphicsDevice, int offsetInBytes, S[] data, int startIndex, int elementCount)
where S : struct 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()) using (var stream = MapBufferWrite())
{ {
if (offsetInBytes > 0) if (offsetInBytes > 0)
stream.Seek(offsetInBytes, SeekOrigin.Current); stream.Seek(offsetInBytes, SeekOrigin.Current);
if (startIndex > 0 || elementCount < data.Length) if (startIndex > 0 || elementCount < data.Length)
for (int i = startIndex; i < startIndex + elementCount; i++) for (int i = startIndex; i < startIndex + elementCount; i++)
stream.Write<S>(data[i]); stream.Write<S>(data[i]);
else else
for (int i = 0; i < data.Length; i++) for (int i = 0; i < data.Length; i++)
stream.Write<S>(data[i]); stream.Write<S>(data[i]);
UnmapBuffer(); UnmapBuffer();
} }
} }
#endregion #endregion
#region GetData #region GetData
public void GetData<S>(S[] data) where S : struct public void GetData<S>(S[] data) where S : struct
{ {
GetData(0, data, 0, data.Length); GetData(0, data, 0, data.Length);
} }
public void GetData<S>(S[] data, int startIndex, int elementCount) where S : struct public void GetData<S>(S[] data, int startIndex, int elementCount) where S : struct
{ {
GetData(0, data, 0, data.Length); GetData(0, data, 0, data.Length);
} }
public void GetData<S>(int offsetInBytes, S[] data, int startIndex, int elementCount) where S : struct 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");
if (offsetInBytes > 0)
stream.Seek(offsetInBytes, SeekOrigin.Current);
stream.ReadRange(data, startIndex, elementCount); var stagingBuffer = CreateStagingBuffer();
UnmapBuffer(); CopySubresource(NativeBuffer, stagingBuffer);
}
}
#endregion
protected int GetSizeInBytes(int indexCount) using (var stream = MapBufferRead(stagingBuffer))
{ {
return (elementSize == IndexElementSize.SixteenBits ? 2 : 4) * indexCount; if (offsetInBytes > 0)
} stream.Seek(offsetInBytes, SeekOrigin.Current);
#region Dispose stream.ReadRange(data, startIndex, elementCount);
public void Dispose() UnmapBuffer(stagingBuffer);
{ }
if (NativeBuffer != null) }
{ #endregion
NativeBuffer.Dispose();
NativeBuffer = null; protected int GetSizeInBytes(int indexCount)
} {
} return (elementSize == IndexElementSize.SixteenBits ? 2 : 4) * indexCount;
#endregion }
}
#region Dispose
public void Dispose()
{
if (NativeBuffer != null)
{
NativeBuffer.Dispose();
NativeBuffer = null;
}
}
#endregion
}
} }

View File

@ -10,6 +10,8 @@ using SharpDX;
// "ANX.Framework developer group" and released under the Ms-PL license. // "ANX.Framework developer group" and released under the Ms-PL license.
// For details see: http://anxframework.codeplex.com/license // For details see: http://anxframework.codeplex.com/license
using System.Runtime.InteropServices;
#if DX10 #if DX10
using Dx = SharpDX.Direct3D10; using Dx = SharpDX.Direct3D10;
using DxDevice = SharpDX.Direct3D10.Device; using DxDevice = SharpDX.Direct3D10.Device;
@ -23,86 +25,106 @@ using DxDevice = SharpDX.Direct3D11.Device;
namespace ANX.RenderSystem.Windows.DX11 namespace ANX.RenderSystem.Windows.DX11
#endif #endif
{ {
public partial class DxVertexBuffer : IDisposable public partial class DxVertexBuffer : IDisposable
{ {
private int vertexStride; private int vertexStride;
#region SetData #region SetData
public void SetData<S>(GraphicsDevice graphicsDevice, S[] data) where S : struct public void SetData<S>(GraphicsDevice graphicsDevice, S[] data) where S : struct
{ {
SetData<S>(graphicsDevice, data, 0, data.Length); if (data == null)
} throw new ArgumentNullException("data");
public void SetData<S>(GraphicsDevice graphicsDevice, S[] data, int startIndex, int elementCount) where S : struct SetData<S>(graphicsDevice, data, 0, data.Length);
{ }
SetData<S>(graphicsDevice, 0, data, startIndex, elementCount);
}
public void SetData<S>(GraphicsDevice graphicsDevice, int offsetInBytes, S[] data, int startIndex, int elementCount) public void SetData<S>(GraphicsDevice graphicsDevice, S[] data, int startIndex, int elementCount) where S : struct
where S : struct {
{ SetData<S>(graphicsDevice, 0, data, startIndex, elementCount);
//TODO: check offsetInBytes parameter for bounds etc. }
using (var stream = MapBufferWrite()) public void SetData<S>(GraphicsDevice graphicsDevice, int offsetInBytes, S[] data, int startIndex, int elementCount)
{ where S : struct
if (offsetInBytes > 0) {
stream.Seek(offsetInBytes, SeekOrigin.Current); if (data == null)
throw new ArgumentNullException("data");
if (startIndex > 0 || elementCount < data.Length) if (startIndex + elementCount > data.Length)
for (int i = startIndex; i < startIndex + elementCount; i++) throw new ArgumentOutOfRangeException("startIndex must be smaller than elementCount + data.Length.");
stream.Write<S>(data[i]);
else
for (int i = 0; i < data.Length; i++)
stream.Write<S>(data[i]);
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<S>(GraphicsDevice graphicsDevice, int offsetInBytes, S[] data, int startIndex, int elementCount, using (var stream = MapBufferWrite())
int vertexStride) where S : struct {
{ if (offsetInBytes > 0)
throw new NotImplementedException(); stream.Seek(offsetInBytes, SeekOrigin.Current);
}
#endregion
#region GetData if (startIndex > 0 || elementCount < data.Length)
public void GetData<S>(S[] data) where S : struct for (int i = startIndex; i < startIndex + elementCount; i++)
{ stream.Write<S>(data[i]);
GetData(data, 0, data.Length); else
} for (int i = 0; i < data.Length; i++)
stream.Write<S>(data[i]);
public void GetData<S>(S[] data, int startIndex, int elementCount) where S : struct UnmapBuffer();
{ }
GetData(0, data, startIndex, elementCount, vertexStride); }
}
public void GetData<S>(int offsetInBytes, S[] data, int startIndex, int elementCount, int vertexStride) public void SetData<S>(GraphicsDevice graphicsDevice, int offsetInBytes, S[] data, int startIndex, int elementCount,
where S : struct int vertexStride) where S : struct
{ {
Dx.Buffer stagingBuffer = CreateStagingBuffer(elementCount * vertexStride); throw new NotImplementedException();
}
#endregion
#region GetData
public void GetData<S>(S[] data) where S : struct
{
if (data == null)
throw new ArgumentNullException("data");
GetData(data, 0, data.Length);
}
public void GetData<S>(S[] data, int startIndex, int elementCount) where S : struct
{
GetData(0, data, startIndex, elementCount, vertexStride);
}
public void GetData<S>(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); CopySubresource(NativeBuffer, stagingBuffer);
using (var stream = MapBufferRead(stagingBuffer)) using (var stream = MapBufferRead(stagingBuffer))
{ {
if (offsetInBytes > 0) if (offsetInBytes > 0)
stream.Seek(offsetInBytes, SeekOrigin.Current); stream.Seek(offsetInBytes, SeekOrigin.Current);
stream.ReadRange(data, startIndex, elementCount); stream.ReadRange(data, startIndex, elementCount);
UnmapBuffer(stagingBuffer); UnmapBuffer(stagingBuffer);
} }
} }
#endregion #endregion
#region Dispose #region Dispose
public void Dispose() public void Dispose()
{ {
if (NativeBuffer != null) if (NativeBuffer != null)
{ {
NativeBuffer.Dispose(); NativeBuffer.Dispose();
NativeBuffer = null; NativeBuffer = null;
} }
} }
#endregion #endregion
} }
} }

View File

@ -57,6 +57,9 @@
<Reference Include="System.Windows.Forms" /> <Reference Include="System.Windows.Forms" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="..\ANX.RenderSystem.DX.SharedSources\BufferHelper.cs">
<Link>BufferHelper.cs</Link>
</Compile>
<Compile Include="BlendState_DX10.cs" /> <Compile Include="BlendState_DX10.cs" />
<Compile Include="Creator.cs" /> <Compile Include="Creator.cs" />
<Compile Include="DepthStencilState_DX10.cs" /> <Compile Include="DepthStencilState_DX10.cs" />

View File

@ -14,58 +14,83 @@ using Dx10 = SharpDX.Direct3D10;
namespace ANX.RenderSystem.Windows.DX10 namespace ANX.RenderSystem.Windows.DX10
{ {
public partial class DxIndexBuffer : INativeIndexBuffer, IDisposable public partial class DxIndexBuffer : INativeIndexBuffer, IDisposable
{ {
public Dx10.Buffer NativeBuffer { get; protected set; } public Dx10.Buffer NativeBuffer { get; protected set; }
#region Constructor #region Constructor
public DxIndexBuffer(GraphicsDevice graphics, IndexElementSize size, int indexCount, BufferUsage usage) public DxIndexBuffer(GraphicsDevice graphics, IndexElementSize size, int indexCount, BufferUsage usage)
{ {
elementSize = size; elementSize = size;
GraphicsDeviceDX gd10 = graphics.NativeDevice as GraphicsDeviceDX; GraphicsDeviceDX gd10 = graphics.NativeDevice as GraphicsDeviceDX;
Dx10.Device device = gd10 != null ? gd10.NativeDevice as Dx10.Device : null; 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) internal DxIndexBuffer(Dx10.Device device, IndexElementSize size, int indexCount, BufferUsage usage)
{ {
elementSize = size; elementSize = size;
InitializeBuffer(device, size, indexCount, usage); InitializeBuffer(device, size, indexCount, usage);
} }
#endregion #endregion
#region InitializeBuffer #region InitializeBuffer
private void InitializeBuffer(Dx10.Device device, IndexElementSize size, int indexCount, BufferUsage usage) private void InitializeBuffer(Dx10.Device device, IndexElementSize size, int indexCount, BufferUsage usage)
{ {
//TODO: translate and use usage //TODO: translate and use usage
var description = new Dx10.BufferDescription() var description = new Dx10.BufferDescription()
{ {
Usage = Dx10.ResourceUsage.Dynamic, Usage = Dx10.ResourceUsage.Dynamic,
SizeInBytes = GetSizeInBytes(indexCount), SizeInBytes = GetSizeInBytes(indexCount),
BindFlags = Dx10.BindFlags.IndexBuffer, BindFlags = Dx10.BindFlags.IndexBuffer,
CpuAccessFlags = Dx10.CpuAccessFlags.Write, CpuAccessFlags = Dx10.CpuAccessFlags.Write,
OptionFlags = Dx10.ResourceOptionFlags.None OptionFlags = Dx10.ResourceOptionFlags.None
}; };
NativeBuffer = new Dx10.Buffer(device, description); NativeBuffer = new Dx10.Buffer(device, description);
//NativeBuffer.Unmap(); //NativeBuffer.Unmap();
} }
#endregion #endregion
protected DataStream MapBufferWrite() protected DataStream MapBufferWrite()
{ {
return NativeBuffer.Map(Dx10.MapMode.WriteDiscard); 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() protected void UnmapBuffer(Dx10.Buffer buffer)
{ {
NativeBuffer.Unmap(); 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);
}
}
} }

View File

@ -23,69 +23,64 @@ using DxDevice = SharpDX.Direct3D11.Device;
namespace ANX.RenderSystem.Windows.DX11 namespace ANX.RenderSystem.Windows.DX11
#endif #endif
{ {
public partial class DxVertexBuffer : INativeVertexBuffer, IDisposable public partial class DxVertexBuffer : INativeVertexBuffer, IDisposable
{ {
public Dx.Buffer NativeBuffer { get; protected set; } public Dx.Buffer NativeBuffer { get; protected set; }
private Dx.Device device; private Dx.Device device;
#region Constructor #region Constructor
public DxVertexBuffer(GraphicsDevice graphics, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) public DxVertexBuffer(GraphicsDevice graphics, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage)
{ {
GraphicsDeviceDX gd10 = graphics.NativeDevice as GraphicsDeviceDX; GraphicsDeviceDX gd10 = graphics.NativeDevice as GraphicsDeviceDX;
this.device = gd10 != null ? gd10.NativeDevice as Dx.Device : null; 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) internal DxVertexBuffer(Dx.Device device, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage)
{ {
InitializeBuffer(device, vertexDeclaration, vertexCount, usage); InitializeBuffer(device, vertexDeclaration, vertexCount, usage);
} }
#endregion #endregion
#region InitializeBuffer #region InitializeBuffer
private void InitializeBuffer(Dx.Device device, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) private void InitializeBuffer(Dx.Device device, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage)
{ {
this.vertexStride = vertexDeclaration.VertexStride; this.vertexStride = vertexDeclaration.VertexStride;
//TODO: translate and use usage //TODO: translate and use usage
if (device != null) if (device != null)
{ {
var description = new Dx.BufferDescription() var description = new Dx.BufferDescription()
{ {
Usage = Dx.ResourceUsage.Dynamic, Usage = Dx.ResourceUsage.Dynamic,
SizeInBytes = vertexDeclaration.VertexStride * vertexCount, SizeInBytes = vertexDeclaration.VertexStride * vertexCount,
BindFlags = Dx.BindFlags.VertexBuffer, BindFlags = Dx.BindFlags.VertexBuffer,
CpuAccessFlags = Dx.CpuAccessFlags.Write, CpuAccessFlags = Dx.CpuAccessFlags.Write,
OptionFlags = Dx.ResourceOptionFlags.None OptionFlags = Dx.ResourceOptionFlags.None
}; };
NativeBuffer = new Dx.Buffer(device, description); NativeBuffer = new Dx.Buffer(device, description);
//NativeBuffer.Unmap(); //NativeBuffer.Unmap();
} }
} }
#endregion #endregion
private DataStream MapBufferWrite() private DataStream MapBufferWrite()
{ {
return NativeBuffer.Map(Dx.MapMode.WriteDiscard); return NativeBuffer.Map(Dx.MapMode.WriteDiscard);
} }
private DataStream MapBufferRead()
{
return NativeBuffer.Map(Dx.MapMode.Read);
}
private SharpDX.DataStream MapBufferRead(Dx.Buffer buffer) private SharpDX.DataStream MapBufferRead(Dx.Buffer buffer)
{ {
return buffer.Map(Dx.MapMode.ReadWrite); return buffer.Map(Dx.MapMode.ReadWrite);
} }
private void UnmapBuffer() private void UnmapBuffer()
{ {
NativeBuffer.Unmap(); NativeBuffer.Unmap();
} }
private void UnmapBuffer(Dx.Buffer buffer) private void UnmapBuffer(Dx.Buffer buffer)
{ {
@ -97,17 +92,17 @@ namespace ANX.RenderSystem.Windows.DX11
this.device.CopyResource(source, destination); this.device.CopyResource(source, destination);
} }
private Dx.Buffer CreateStagingBuffer(int sizeInBytes) private Dx.Buffer CreateStagingBuffer()
{ {
var description = new Dx.BufferDescription() var description = new Dx.BufferDescription()
{ {
Usage = Dx.ResourceUsage.Staging, Usage = Dx.ResourceUsage.Staging,
SizeInBytes = sizeInBytes, SizeInBytes = NativeBuffer.Description.SizeInBytes,
CpuAccessFlags = Dx.CpuAccessFlags.Read | Dx.CpuAccessFlags.Write, CpuAccessFlags = Dx.CpuAccessFlags.Read | Dx.CpuAccessFlags.Write,
OptionFlags = Dx.ResourceOptionFlags.None OptionFlags = Dx.ResourceOptionFlags.None
}; };
return new Dx.Buffer(device, description); return new Dx.Buffer(device, description);
} }
} }
} }

View File

@ -70,6 +70,9 @@
<Reference Include="System.Windows.Forms" /> <Reference Include="System.Windows.Forms" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="..\ANX.RenderSystem.DX.SharedSources\BufferHelper.cs">
<Link>BufferHelper.cs</Link>
</Compile>
<Compile Include="BlendState_DX11.cs" /> <Compile Include="BlendState_DX11.cs" />
<Compile Include="Creator.cs" /> <Compile Include="Creator.cs" />
<Compile Include="DepthStencilState_DX11.cs" /> <Compile Include="DepthStencilState_DX11.cs" />

View File

@ -15,64 +15,90 @@ using Dx11 = SharpDX.Direct3D11;
namespace ANX.RenderSystem.Windows.DX11 namespace ANX.RenderSystem.Windows.DX11
{ {
public partial class DxIndexBuffer : INativeIndexBuffer, IDisposable public partial class DxIndexBuffer : INativeIndexBuffer, IDisposable
{ {
public Dx11.Buffer NativeBuffer { get; protected set; } public Dx11.Buffer NativeBuffer { get; protected set; }
#region Constructor #region Constructor
public DxIndexBuffer(GraphicsDevice graphics, IndexElementSize size, int indexCount, BufferUsage usage) public DxIndexBuffer(GraphicsDevice graphics, IndexElementSize size, int indexCount, BufferUsage usage)
{ {
elementSize = size; elementSize = size;
GraphicsDeviceDX gd11 = graphics.NativeDevice as GraphicsDeviceDX; GraphicsDeviceDX gd11 = graphics.NativeDevice as GraphicsDeviceDX;
Dx11.DeviceContext context = gd11 != null ? gd11.NativeDevice as Dx11.DeviceContext : null; 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) internal DxIndexBuffer(SharpDX.Direct3D11.Device device, IndexElementSize size, int indexCount, BufferUsage usage)
{ {
elementSize = size; elementSize = size;
InitializeBuffer(device, size, indexCount, usage); InitializeBuffer(device, size, indexCount, usage);
} }
#endregion #endregion
#region InitializeBuffer #region InitializeBuffer
private void InitializeBuffer(Dx11.Device device, IndexElementSize size, int indexCount, BufferUsage usage) private void InitializeBuffer(Dx11.Device device, IndexElementSize size, int indexCount, BufferUsage usage)
{ {
//TODO: translate and use usage //TODO: translate and use usage
var description = new Dx11.BufferDescription() var description = new Dx11.BufferDescription()
{ {
// TODO: translate usage // TODO: translate usage
Usage = Dx11.ResourceUsage.Dynamic, Usage = Dx11.ResourceUsage.Dynamic,
SizeInBytes = GetSizeInBytes(indexCount), SizeInBytes = GetSizeInBytes(indexCount),
BindFlags = Dx11.BindFlags.IndexBuffer, BindFlags = Dx11.BindFlags.IndexBuffer,
CpuAccessFlags = Dx11.CpuAccessFlags.Write, CpuAccessFlags = Dx11.CpuAccessFlags.Write,
OptionFlags = Dx11.ResourceOptionFlags.None OptionFlags = Dx11.ResourceOptionFlags.None
}; };
NativeBuffer = new SharpDX.Direct3D11.Buffer(device, description); NativeBuffer = new SharpDX.Direct3D11.Buffer(device, description);
} }
#endregion #endregion
protected DataStream MapBufferWrite() protected DataStream MapBufferWrite()
{ {
Dx11.DeviceContext context = NativeBuffer.Device.ImmediateContext; Dx11.DeviceContext context = NativeBuffer.Device.ImmediateContext;
DataStream stream; DataStream stream;
context.MapSubresource(NativeBuffer, Dx11.MapMode.WriteDiscard, Dx11.MapFlags.None, out stream); context.MapSubresource(NativeBuffer, Dx11.MapMode.WriteDiscard, Dx11.MapFlags.None, out stream);
return stream; return stream;
} }
protected DataStream MapBufferRead() protected void UnmapBuffer()
{ {
Dx11.DeviceContext context = NativeBuffer.Device.ImmediateContext; Dx11.DeviceContext context = NativeBuffer.Device.ImmediateContext;
DataStream stream; context.UnmapSubresource(NativeBuffer, 0);
context.MapSubresource(NativeBuffer, Dx11.MapMode.Read, Dx11.MapFlags.None, out stream); }
return stream;
}
protected void UnmapBuffer() private SharpDX.DataStream MapBufferRead(Dx11.Buffer buffer)
{ {
Dx11.DeviceContext context = NativeBuffer.Device.ImmediateContext; Dx11.DeviceContext context = buffer.Device.ImmediateContext;
context.UnmapSubresource(NativeBuffer, 0); 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);
}
}
} }

View File

@ -23,64 +23,56 @@ using DxDevice = SharpDX.Direct3D11.Device;
namespace ANX.RenderSystem.Windows.DX11 namespace ANX.RenderSystem.Windows.DX11
#endif #endif
{ {
public partial class DxVertexBuffer : INativeVertexBuffer, IDisposable public partial class DxVertexBuffer : INativeVertexBuffer, IDisposable
{ {
public Dx.Buffer NativeBuffer { get; protected set; } public Dx.Buffer NativeBuffer { get; protected set; }
private Dx.DeviceContext context; private Dx.DeviceContext context;
#region Constructor #region Constructor
public DxVertexBuffer(GraphicsDevice graphics, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) public DxVertexBuffer(GraphicsDevice graphics, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage)
{ {
var gd11 = graphics.NativeDevice as GraphicsDeviceDX; var gd11 = graphics.NativeDevice as GraphicsDeviceDX;
this.context = gd11 != null ? gd11.NativeDevice as Dx.DeviceContext : null; 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) internal DxVertexBuffer(Dx.Device device, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage)
{ {
InitializeBuffer(device, vertexDeclaration, vertexCount, usage); InitializeBuffer(device, vertexDeclaration, vertexCount, usage);
} }
#endregion #endregion
#region InitializeBuffer (TODO) #region InitializeBuffer (TODO)
private void InitializeBuffer(Dx.Device device, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) private void InitializeBuffer(Dx.Device device, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage)
{ {
vertexStride = vertexDeclaration.VertexStride; vertexStride = vertexDeclaration.VertexStride;
//TODO: translate and use usage //TODO: translate and use usage
if (device != null) if (device != null)
{ {
var description = new Dx.BufferDescription() var description = new Dx.BufferDescription()
{ {
Usage = Dx.ResourceUsage.Dynamic, Usage = Dx.ResourceUsage.Dynamic,
SizeInBytes = vertexDeclaration.VertexStride * vertexCount, SizeInBytes = vertexDeclaration.VertexStride * vertexCount,
BindFlags = Dx.BindFlags.VertexBuffer, BindFlags = Dx.BindFlags.VertexBuffer,
CpuAccessFlags = Dx.CpuAccessFlags.Write, CpuAccessFlags = Dx.CpuAccessFlags.Write,
OptionFlags = Dx.ResourceOptionFlags.None OptionFlags = Dx.ResourceOptionFlags.None
}; };
NativeBuffer = new Dx.Buffer(device, description); NativeBuffer = new Dx.Buffer(device, description);
} }
} }
#endregion #endregion
private SharpDX.DataStream MapBufferWrite() private SharpDX.DataStream MapBufferWrite()
{ {
Dx.DeviceContext context = NativeBuffer.Device.ImmediateContext; Dx.DeviceContext context = NativeBuffer.Device.ImmediateContext;
DataStream stream; DataStream stream;
context.MapSubresource(NativeBuffer, Dx.MapMode.WriteDiscard, Dx.MapFlags.None, out stream); context.MapSubresource(NativeBuffer, Dx.MapMode.WriteDiscard, Dx.MapFlags.None, out stream);
return 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 MapBufferRead(Dx.Resource buffer) private SharpDX.DataStream MapBufferRead(Dx.Resource buffer)
{ {
@ -89,14 +81,16 @@ namespace ANX.RenderSystem.Windows.DX11
return stream; return stream;
} }
private void UnmapBuffer() private void UnmapBuffer()
{ {
Dx.DeviceContext context = NativeBuffer.Device.ImmediateContext; Dx.DeviceContext context = NativeBuffer.Device.ImmediateContext;
context.UnmapSubresource(NativeBuffer, 0); context.UnmapSubresource(NativeBuffer, 0);
} }
private void CopySubresource(Dx.Buffer source, Dx.Buffer destination) private void CopySubresource(Dx.Buffer source, Dx.Buffer destination)
{ {
BufferHelper.ValidateCopyResource(source, destination);
this.context.CopyResource(source, destination); this.context.CopyResource(source, destination);
} }
@ -105,18 +99,18 @@ namespace ANX.RenderSystem.Windows.DX11
buffer.Device.ImmediateContext.UnmapSubresource(buffer, 0); buffer.Device.ImmediateContext.UnmapSubresource(buffer, 0);
} }
private Dx.Buffer CreateStagingBuffer(int sizeInBytes) private Dx.Buffer CreateStagingBuffer()
{ {
var description = new Dx.BufferDescription() var description = new Dx.BufferDescription()
{ {
Usage = Dx.ResourceUsage.Staging, Usage = Dx.ResourceUsage.Staging,
SizeInBytes = sizeInBytes, SizeInBytes = NativeBuffer.Description.SizeInBytes,
BindFlags = Dx.BindFlags.VertexBuffer, BindFlags = Dx.BindFlags.VertexBuffer,
CpuAccessFlags = Dx.CpuAccessFlags.Write | Dx.CpuAccessFlags.Read, CpuAccessFlags = Dx.CpuAccessFlags.Read,
OptionFlags = Dx.ResourceOptionFlags.None OptionFlags = Dx.ResourceOptionFlags.None
}; };
return new Dx.Buffer(context.Device, description); return new Dx.Buffer(context.Device, description);
} }
} }
} }