2012-08-09 09:45:04 +00:00
|
|
|
using System;
|
2012-01-16 15:03:28 +00:00
|
|
|
using System.Runtime.InteropServices;
|
2012-02-19 11:24:23 +00:00
|
|
|
using ANX.Framework.NonXNA;
|
2012-02-19 13:41:02 +00:00
|
|
|
using ANX.Framework.NonXNA.Development;
|
|
|
|
using ANX.Framework.NonXNA.RenderSystem;
|
2011-10-31 05:36:24 +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-10-31 05:36:24 +00:00
|
|
|
|
|
|
|
namespace ANX.Framework.Graphics
|
|
|
|
{
|
2012-02-19 13:41:02 +00:00
|
|
|
[PercentageComplete(100)]
|
|
|
|
[TestState(TestStateAttribute.TestState.Untested)]
|
2012-08-29 13:14:00 +00:00
|
|
|
[Developer("Glatzemann")]
|
2012-02-19 13:41:02 +00:00
|
|
|
public class VertexBuffer : GraphicsResource, IGraphicsResource
|
|
|
|
{
|
|
|
|
#region Private
|
|
|
|
private INativeVertexBuffer nativeVertexBuffer;
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Public
|
|
|
|
// This is now internal because via befriending the assemblies
|
|
|
|
// it's usable in the modules but doesn't confuse the enduser.
|
|
|
|
internal INativeVertexBuffer NativeVertexBuffer
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2012-09-11 13:51:20 +00:00
|
|
|
if (nativeVertexBuffer == null)
|
2012-02-19 13:41:02 +00:00
|
|
|
CreateNativeBuffer();
|
2012-02-19 11:24:23 +00:00
|
|
|
|
2012-09-11 13:51:20 +00:00
|
|
|
return nativeVertexBuffer;
|
2012-02-19 13:41:02 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-19 11:24:23 +00:00
|
|
|
|
2012-09-11 13:51:20 +00:00
|
|
|
public BufferUsage BufferUsage { get; private set; }
|
|
|
|
public int VertexCount { get; private set; }
|
|
|
|
public VertexDeclaration VertexDeclaration { get; private set; }
|
2012-02-19 13:41:02 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Constructor
|
2012-09-11 13:51:20 +00:00
|
|
|
public VertexBuffer(GraphicsDevice graphicsDevice, Type vertexType, int vertexCount, BufferUsage usage)
|
|
|
|
: this(graphicsDevice, VertexTypeHelper.GetDeclaration(vertexType), vertexCount, usage)
|
2012-02-19 13:41:02 +00:00
|
|
|
{
|
|
|
|
}
|
2011-10-31 05:36:24 +00:00
|
|
|
|
2012-09-11 13:51:20 +00:00
|
|
|
public VertexBuffer(GraphicsDevice graphicsDevice, VertexDeclaration vertexDeclaration, int vertexCount,
|
|
|
|
BufferUsage usage)
|
2012-02-19 13:41:02 +00:00
|
|
|
: base(graphicsDevice)
|
|
|
|
{
|
2012-09-11 13:51:20 +00:00
|
|
|
VertexCount = vertexCount;
|
|
|
|
VertexDeclaration = vertexDeclaration;
|
|
|
|
BufferUsage = usage;
|
2012-02-19 13:41:02 +00:00
|
|
|
|
|
|
|
base.GraphicsDevice.ResourceCreated += GraphicsDevice_ResourceCreated;
|
|
|
|
base.GraphicsDevice.ResourceDestroyed += GraphicsDevice_ResourceDestroyed;
|
|
|
|
|
|
|
|
CreateNativeBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
~VertexBuffer()
|
|
|
|
{
|
|
|
|
Dispose();
|
|
|
|
base.GraphicsDevice.ResourceCreated -= GraphicsDevice_ResourceCreated;
|
|
|
|
base.GraphicsDevice.ResourceDestroyed -= GraphicsDevice_ResourceDestroyed;
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region GraphicsDevice_ResourceDestroyed
|
|
|
|
private void GraphicsDevice_ResourceDestroyed(object sender, ResourceDestroyedEventArgs e)
|
|
|
|
{
|
|
|
|
if (nativeVertexBuffer != null)
|
|
|
|
{
|
|
|
|
nativeVertexBuffer.Dispose();
|
|
|
|
nativeVertexBuffer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region GraphicsDevice_ResourceCreated
|
|
|
|
private void GraphicsDevice_ResourceCreated(object sender, ResourceCreatedEventArgs e)
|
|
|
|
{
|
|
|
|
if (nativeVertexBuffer != null)
|
|
|
|
{
|
|
|
|
nativeVertexBuffer.Dispose();
|
|
|
|
nativeVertexBuffer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
CreateNativeBuffer();
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region CreateNativeBuffer
|
|
|
|
private void CreateNativeBuffer()
|
|
|
|
{
|
2012-09-11 13:51:20 +00:00
|
|
|
var creator = AddInSystemFactory.Instance.GetDefaultCreator<IRenderSystemCreator>();
|
|
|
|
this.nativeVertexBuffer = creator.CreateVertexBuffer(GraphicsDevice, this, VertexDeclaration, VertexCount,
|
|
|
|
BufferUsage);
|
2012-02-19 13:41:02 +00:00
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region GetData
|
2012-09-11 13:51:20 +00:00
|
|
|
public void GetData<T>(int offsetInBytes, T[] data, int startIndex, int elementCount, int vertexStride) where T : struct
|
2012-02-19 13:41:02 +00:00
|
|
|
{
|
2012-09-11 13:51:20 +00:00
|
|
|
NativeVertexBuffer.GetData(offsetInBytes, data, startIndex, elementCount, vertexStride);
|
2012-02-19 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void GetData<T>(T[] data) where T : struct
|
|
|
|
{
|
|
|
|
NativeVertexBuffer.GetData(data);
|
|
|
|
}
|
|
|
|
|
2012-09-11 13:51:20 +00:00
|
|
|
public void GetData<T>(T[] data, int startIndex, int elementCount) where T : struct
|
2012-02-19 13:41:02 +00:00
|
|
|
{
|
|
|
|
NativeVertexBuffer.GetData(data, startIndex, elementCount);
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region SetData
|
2012-09-11 13:51:20 +00:00
|
|
|
public void SetData<T>(int offsetInBytes, T[] data, int startIndex, int elementCount, int vertexStride) where T : struct
|
2012-02-19 13:41:02 +00:00
|
|
|
{
|
2012-09-11 13:51:20 +00:00
|
|
|
NativeVertexBuffer.SetData(GraphicsDevice, offsetInBytes, data, startIndex, elementCount, vertexStride);
|
2012-02-19 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetData<T>(T[] data) where T : struct
|
|
|
|
{
|
|
|
|
NativeVertexBuffer.SetData(GraphicsDevice, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetData<T>(T[] data, int startIndex, int elementCount) where T : struct
|
|
|
|
{
|
|
|
|
NativeVertexBuffer.SetData(GraphicsDevice, data, startIndex, elementCount);
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Dispose
|
|
|
|
public override void Dispose()
|
|
|
|
{
|
|
|
|
if (nativeVertexBuffer != null)
|
|
|
|
{
|
|
|
|
nativeVertexBuffer.Dispose();
|
|
|
|
nativeVertexBuffer = null;
|
|
|
|
}
|
|
|
|
|
2012-09-11 13:51:20 +00:00
|
|
|
// do not dispose the VertexDeclaration here, because it's only a reference
|
|
|
|
if (VertexDeclaration != null)
|
|
|
|
VertexDeclaration = null;
|
2012-02-19 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Dispose([MarshalAs(UnmanagedType.U1)] bool disposeManaged)
|
|
|
|
{
|
2012-09-11 13:51:20 +00:00
|
|
|
if (disposeManaged)
|
|
|
|
{
|
|
|
|
Dispose();
|
|
|
|
}
|
2012-02-19 13:41:02 +00:00
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
2011-10-31 05:36:24 +00:00
|
|
|
}
|