2012-09-15 13:43:31 +00:00
#region Using Statements
using System ;
2012-09-07 09:48:45 +00:00
using System.IO ;
using ANX.Framework.Graphics ;
using SharpDX ;
2012-09-15 13:43:31 +00:00
#endregion
2012-09-07 09:48:45 +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
2015-01-18 13:10:50 +00:00
using System.Runtime.InteropServices ;
2012-09-15 13:43:31 +00:00
#if DX10
2012-09-28 06:16:57 +00:00
using Dx = SharpDX . Direct3D10 ;
using DxDevice = SharpDX . Direct3D10 . Device ;
2012-09-15 13:43:31 +00:00
namespace ANX.RenderSystem.Windows.DX10
#endif
#if DX11
2012-09-28 06:16:57 +00:00
using Dx = SharpDX . Direct3D11 ;
using DxDevice = SharpDX . Direct3D11 . Device ;
2012-09-15 13:43:31 +00:00
namespace ANX.RenderSystem.Windows.DX11
#endif
2012-09-07 09:48:45 +00:00
{
2015-01-18 13:10:50 +00:00
public partial class DxVertexBuffer : IDisposable
{
private int vertexStride ;
#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 ) ;
}
public void SetData < S > ( GraphicsDevice graphicsDevice , S [ ] data , int startIndex , int elementCount ) where S : struct
{
SetData < S > ( graphicsDevice , 0 , data , startIndex , elementCount ) ;
}
public void SetData < S > ( GraphicsDevice graphicsDevice , int offsetInBytes , S [ ] data , int startIndex , int elementCount )
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." ) ;
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 ) ;
if ( startIndex > 0 | | elementCount < data . Length )
for ( int i = startIndex ; i < startIndex + elementCount ; i + + )
stream . Write < S > ( data [ i ] ) ;
else
for ( int i = 0 ; i < data . Length ; i + + )
stream . Write < S > ( data [ i ] ) ;
UnmapBuffer ( ) ;
}
}
public void SetData < S > ( 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 > ( 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 ( ) ;
2012-09-28 06:16:57 +00:00
CopySubresource ( NativeBuffer , stagingBuffer ) ;
2015-01-18 13:10:50 +00:00
using ( var stream = MapBufferRead ( stagingBuffer ) )
{
if ( offsetInBytes > 0 )
stream . Seek ( offsetInBytes , SeekOrigin . Current ) ;
stream . ReadRange ( data , startIndex , elementCount ) ;
UnmapBuffer ( stagingBuffer ) ;
}
}
#endregion
#region Dispose
public void Dispose ( )
{
if ( NativeBuffer ! = null )
{
NativeBuffer . Dispose ( ) ;
NativeBuffer = null ;
}
}
#endregion
}
2012-09-07 09:48:45 +00:00
}