2012-09-15 13:43:31 +00:00
#region Using Statements
using System ;
2012-09-07 09:48:45 +00:00
using ANX.Framework.Graphics ;
using SharpDX ;
using System.IO ;
2015-01-18 13:10:50 +00:00
using System.Runtime.InteropServices ;
2012-09-07 09:48:45 +00:00
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
2012-09-15 13:43:31 +00:00
#if DX10
namespace ANX.RenderSystem.Windows.DX10
2015-01-18 13:10:50 +00:00
#elif DX11
2012-09-15 13:43:31 +00:00
namespace ANX.RenderSystem.Windows.DX11
#endif
2012-09-07 09:48:45 +00:00
{
2015-10-04 21:30:00 +02:00
public partial class DxIndexBuffer
2015-01-18 13:10:50 +00:00
{
protected IndexElementSize elementSize ;
#region SetData
2015-09-30 21:31:15 +02:00
public void SetData < S > ( S [ ] data ) where S : struct
2015-01-18 13:10:50 +00:00
{
2015-09-30 21:31:15 +02:00
SetData < S > ( data , 0 , data . Length ) ;
2015-01-18 13:10:50 +00:00
}
2015-09-30 21:31:15 +02:00
public void SetData < S > ( S [ ] data , int startIndex , int elementCount ) where S : struct
2015-01-18 13:10:50 +00:00
{
2015-09-30 21:31:15 +02:00
SetData < S > ( 0 , data , startIndex , elementCount ) ;
2015-01-18 13:10:50 +00:00
}
2015-09-30 21:31:15 +02:00
public void SetData < S > ( int offsetInBytes , S [ ] data , int startIndex , int elementCount )
2015-01-18 13:10:50 +00:00
where S : struct
{
2015-10-14 23:59:27 +02:00
if ( offsetInBytes + elementCount * Marshal . SizeOf ( typeof ( S ) ) > SizeInBytes )
2015-01-18 13:10:50 +00:00
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" ) ) ;
2015-10-04 21:30:00 +02:00
var buffer = this . NativeBuffer ;
if ( this . WriteNeedsStaging )
buffer = this . CreateStagingBuffer ( ResourceMapping . Write ) ;
2015-01-18 13:10:50 +00:00
2015-10-04 21:30:00 +02:00
try
{
using ( var stream = MapBuffer ( buffer , ResourceMapping . Write ) )
{
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 ( buffer ) ;
}
}
finally
{
if ( this . WriteNeedsStaging )
{
CopySubresource ( buffer , this . NativeBuffer ) ;
buffer . Dispose ( ) ;
}
2015-01-18 13:10:50 +00:00
}
}
#endregion
#region GetData
public void GetData < S > ( S [ ] data ) where S : struct
{
2015-09-30 21:31:15 +02:00
if ( data = = null )
throw new ArgumentNullException ( "data" ) ;
2015-01-18 13:10:50 +00:00
GetData ( 0 , data , 0 , data . Length ) ;
}
public void GetData < S > ( S [ ] data , int startIndex , int elementCount ) where S : struct
{
2015-09-30 21:31:15 +02:00
GetData ( 0 , data , startIndex , elementCount ) ;
2015-01-18 13:10:50 +00:00
}
public void GetData < S > ( int offsetInBytes , S [ ] data , int startIndex , int elementCount ) where S : struct
{
if ( data = = null )
throw new ArgumentNullException ( "data" ) ;
2015-10-04 21:30:00 +02:00
using ( var stagingBuffer = CreateStagingBuffer ( ResourceMapping . Read ) )
2015-01-18 13:10:50 +00:00
{
2015-10-04 21:30:00 +02:00
CopySubresource ( NativeBuffer , stagingBuffer ) ;
using ( var stream = MapBuffer ( stagingBuffer , ResourceMapping . Read ) )
{
if ( offsetInBytes > 0 )
stream . Seek ( offsetInBytes , SeekOrigin . Current ) ;
2015-01-18 13:10:50 +00:00
2015-10-04 21:30:00 +02:00
stream . ReadRange ( data , startIndex , elementCount ) ;
UnmapBuffer ( stagingBuffer ) ;
}
2015-01-18 13:10:50 +00:00
}
}
#endregion
protected int GetSizeInBytes ( int indexCount )
{
return ( elementSize = = IndexElementSize . SixteenBits ? 2 : 4 ) * indexCount ;
}
}
2012-09-07 09:48:45 +00:00
}