2012-09-15 13:43:31 +00:00
|
|
|
#region Using Statements
|
2011-11-16 14:27:53 +00:00
|
|
|
using System;
|
|
|
|
using ANX.Framework.Graphics;
|
2012-02-19 13:41:02 +00:00
|
|
|
using ANX.Framework.NonXNA.RenderSystem;
|
2012-09-07 09:48:45 +00:00
|
|
|
using SharpDX;
|
2012-09-15 13:43:31 +00:00
|
|
|
|
|
|
|
#endregion
|
2011-11-16 14:27:53 +00:00
|
|
|
|
2012-08-09 09:45:04 +00:00
|
|
|
// 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
|
2011-11-16 14:27:53 +00:00
|
|
|
|
2012-09-15 13:43:31 +00:00
|
|
|
using Dx11 = SharpDX.Direct3D11;
|
|
|
|
|
2011-11-16 14:27:53 +00:00
|
|
|
namespace ANX.RenderSystem.Windows.DX11
|
|
|
|
{
|
2012-09-15 13:43:31 +00:00
|
|
|
public partial class DxVertexBuffer : INativeVertexBuffer, IDisposable
|
2012-08-29 18:56:51 +00:00
|
|
|
{
|
2012-09-15 13:43:31 +00:00
|
|
|
public Dx11.Buffer NativeBuffer { get; protected set; }
|
|
|
|
|
2012-08-31 16:02:52 +00:00
|
|
|
#region Constructor
|
2012-09-15 13:43:31 +00:00
|
|
|
public DxVertexBuffer(GraphicsDevice graphics, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage)
|
2012-08-29 18:56:51 +00:00
|
|
|
{
|
2012-09-15 13:43:31 +00:00
|
|
|
var gd11 = graphics.NativeDevice as GraphicsDeviceDX;
|
2012-09-07 09:48:45 +00:00
|
|
|
Dx11.DeviceContext context = gd11 != null ? gd11.NativeDevice as Dx11.DeviceContext : null;
|
2012-02-19 13:41:02 +00:00
|
|
|
|
2012-08-29 18:56:51 +00:00
|
|
|
InitializeBuffer(context.Device, vertexDeclaration, vertexCount, usage);
|
|
|
|
}
|
|
|
|
|
2012-09-15 13:43:31 +00:00
|
|
|
internal DxVertexBuffer(Dx11.Device device, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage)
|
2012-08-29 18:56:51 +00:00
|
|
|
{
|
|
|
|
InitializeBuffer(device, vertexDeclaration, vertexCount, usage);
|
|
|
|
}
|
2012-08-31 16:02:52 +00:00
|
|
|
#endregion
|
2012-02-19 13:41:02 +00:00
|
|
|
|
2012-08-31 16:02:52 +00:00
|
|
|
#region InitializeBuffer (TODO)
|
2012-09-07 09:48:45 +00:00
|
|
|
private void InitializeBuffer(Dx11.Device device, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage)
|
2012-08-29 18:56:51 +00:00
|
|
|
{
|
2012-09-07 09:48:45 +00:00
|
|
|
vertexStride = vertexDeclaration.VertexStride;
|
2012-02-19 13:41:02 +00:00
|
|
|
|
2012-08-29 18:56:51 +00:00
|
|
|
//TODO: translate and use usage
|
2012-02-19 13:41:02 +00:00
|
|
|
|
2012-08-29 18:56:51 +00:00
|
|
|
if (device != null)
|
|
|
|
{
|
2012-09-07 09:48:45 +00:00
|
|
|
var description = new Dx11.BufferDescription()
|
2012-02-19 13:41:02 +00:00
|
|
|
{
|
2012-09-07 09:48:45 +00:00
|
|
|
Usage = Dx11.ResourceUsage.Dynamic,
|
2012-08-29 18:56:51 +00:00
|
|
|
SizeInBytes = vertexDeclaration.VertexStride * vertexCount,
|
2012-09-07 09:48:45 +00:00
|
|
|
BindFlags = Dx11.BindFlags.VertexBuffer,
|
|
|
|
CpuAccessFlags = Dx11.CpuAccessFlags.Write,
|
|
|
|
OptionFlags = Dx11.ResourceOptionFlags.None
|
2012-08-29 18:56:51 +00:00
|
|
|
};
|
|
|
|
|
2012-09-07 09:48:45 +00:00
|
|
|
NativeBuffer = new Dx11.Buffer(device, description);
|
2012-08-29 18:56:51 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-31 16:02:52 +00:00
|
|
|
#endregion
|
2012-08-29 18:56:51 +00:00
|
|
|
|
2012-09-15 13:43:31 +00:00
|
|
|
protected SharpDX.DataStream MapBufferWrite()
|
2012-08-29 18:56:51 +00:00
|
|
|
{
|
2012-09-07 09:48:45 +00:00
|
|
|
Dx11.DeviceContext context = NativeBuffer.Device.ImmediateContext;
|
|
|
|
DataStream stream;
|
|
|
|
context.MapSubresource(NativeBuffer, Dx11.MapMode.WriteDiscard, Dx11.MapFlags.None, out stream);
|
|
|
|
return stream;
|
2012-02-19 13:41:02 +00:00
|
|
|
}
|
2012-08-29 18:56:51 +00:00
|
|
|
|
2012-09-15 13:43:31 +00:00
|
|
|
protected SharpDX.DataStream MapBufferRead()
|
2012-08-29 18:56:51 +00:00
|
|
|
{
|
2012-09-07 09:48:45 +00:00
|
|
|
Dx11.DeviceContext context = NativeBuffer.Device.ImmediateContext;
|
|
|
|
DataStream stream;
|
|
|
|
context.MapSubresource(NativeBuffer, Dx11.MapMode.Read, Dx11.MapFlags.None, out stream);
|
|
|
|
return stream;
|
2012-08-29 18:56:51 +00:00
|
|
|
}
|
|
|
|
|
2012-09-15 13:43:31 +00:00
|
|
|
protected void UnmapBuffer()
|
2012-08-29 18:56:51 +00:00
|
|
|
{
|
2012-09-07 09:48:45 +00:00
|
|
|
Dx11.DeviceContext context = NativeBuffer.Device.ImmediateContext;
|
2012-09-04 21:36:46 +00:00
|
|
|
context.UnmapSubresource(NativeBuffer, 0);
|
2012-08-29 18:56:51 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-16 14:27:53 +00:00
|
|
|
}
|