Implemented GetData and SetData for Metro Rendersystem too. Together with commit 32081, this implements patch 15996.

This commit is contained in:
Konstantin Koch 2015-01-18 15:50:00 +00:00 committed by Konstantin Koch
parent 252e337375
commit a7fb23b599
2 changed files with 291 additions and 210 deletions

View File

@ -1,8 +1,9 @@
using System;
using ANX.Framework.Graphics;
using ANX.Framework.NonXNA.RenderSystem;
using Dx11 = SharpDX.Direct3D11;
using Dx = SharpDX.Direct3D11;
using System.IO;
using SharpDX;
// This file is part of the ANX.Framework created by the
// "ANX.Framework developer group" and released under the Ms-PL license.
@ -17,7 +18,7 @@ namespace ANX.RenderSystem.Windows.Metro
#endregion
#region Public
public Dx11.Buffer NativeBuffer
public Dx.Buffer NativeBuffer
{
get;
private set;
@ -36,7 +37,7 @@ namespace ANX.RenderSystem.Windows.Metro
InitializeBuffer(device, indexCount, usage);
}
internal IndexBuffer_Metro(Dx11.Device device, IndexElementSize size,
internal IndexBuffer_Metro(Dx.Device device, IndexElementSize size,
int indexCount, BufferUsage usage)
{
indexSizeInBytes = size == IndexElementSize.SixteenBits ? 2 : 4;
@ -45,19 +46,19 @@ namespace ANX.RenderSystem.Windows.Metro
#endregion
#region InitializeBuffer
private void InitializeBuffer(Dx11.Device device,
private void InitializeBuffer(Dx.Device device,
int indexCount, BufferUsage usage)
{
var description = new Dx11.BufferDescription()
var description = new Dx.BufferDescription()
{
Usage = FormatConverter.Translate(usage),
SizeInBytes = indexSizeInBytes * indexCount,
BindFlags = Dx11.BindFlags.IndexBuffer,
CpuAccessFlags = Dx11.CpuAccessFlags.Write,
OptionFlags = Dx11.ResourceOptionFlags.None
BindFlags = Dx.BindFlags.IndexBuffer,
CpuAccessFlags = Dx.CpuAccessFlags.Write,
OptionFlags = Dx.ResourceOptionFlags.None
};
NativeBuffer = new Dx11.Buffer(device, description);
NativeBuffer = new Dx.Buffer(device, description);
}
#endregion
@ -97,25 +98,65 @@ namespace ANX.RenderSystem.Windows.Metro
}
#endregion
#region GetData (TODO)
public void GetData<T>(int offsetInBytes, T[] data, int startIndex,
int elementCount) where T : struct
#region GetData
public void GetData<S>(S[] data) where S : struct
{
throw new NotImplementedException();
GetData(0, data, 0, data.Length);
}
public void GetData<T>(T[] data) where T : struct
public void GetData<S>(S[] data, int startIndex, int elementCount) where S : struct
{
throw new NotImplementedException();
GetData(0, data, 0, data.Length);
}
public void GetData<T>(T[] data, int startIndex, int elementCount)
where T : struct
public void GetData<S>(int offsetInBytes, S[] data, int startIndex, int elementCount) where S : struct
{
throw new NotImplementedException();
Dx.Buffer stagingBuffer = CreateStagingBuffer(indexSizeInBytes * elementCount);
CopySubresource(NativeBuffer, stagingBuffer);
using (var stream = MapBufferRead(stagingBuffer))
{
if (offsetInBytes > 0)
stream.Seek(offsetInBytes, SeekOrigin.Current);
stream.ReadRange(data, startIndex, elementCount);
UnmapBuffer(stagingBuffer);
}
}
#endregion
private SharpDX.DataStream MapBufferRead(Dx.Buffer buffer)
{
Dx.DeviceContext context = buffer.Device.ImmediateContext;
DataStream stream;
context.MapSubresource(buffer, Dx.MapMode.Read, Dx.MapFlags.None, out stream);
return stream;
}
private void UnmapBuffer(Dx.Buffer buffer)
{
Dx.DeviceContext context = buffer.Device.ImmediateContext;
context.UnmapSubresource(buffer, 0);
}
private void CopySubresource(Dx.Buffer source, Dx.Buffer destination)
{
this.NativeBuffer.Device.ImmediateContext.CopyResource(source, destination);
}
private Dx.Buffer CreateStagingBuffer(int sizeInBytes)
{
var description = new Dx.BufferDescription()
{
Usage = Dx.ResourceUsage.Staging,
SizeInBytes = sizeInBytes,
CpuAccessFlags = Dx.CpuAccessFlags.Read,
OptionFlags = Dx.ResourceOptionFlags.None,
};
return new Dx.Buffer(NativeBuffer.Device, description);
}
#region Dispose
public void Dispose()
{

View File

@ -2,7 +2,8 @@ using System;
using System.IO;
using ANX.Framework.Graphics;
using ANX.Framework.NonXNA.RenderSystem;
using Dx11 = SharpDX.Direct3D11;
using Dx = SharpDX.Direct3D11;
using System.Runtime.InteropServices;
// This file is part of the ANX.Framework created by the
// "ANX.Framework developer group" and released under the Ms-PL license.
@ -17,7 +18,7 @@ namespace ANX.RenderSystem.Windows.Metro
#endregion
#region Public
public Dx11.Buffer NativeBuffer
public Dx.Buffer NativeBuffer
{
get;
private set;
@ -35,7 +36,7 @@ namespace ANX.RenderSystem.Windows.Metro
InitializeBuffer(device, vertexCount, usage);
}
internal VertexBuffer_Metro(Dx11.Device device, VertexDeclaration vertexDeclaration,
internal VertexBuffer_Metro(Dx.Device device, VertexDeclaration vertexDeclaration,
int vertexCount, BufferUsage usage)
{
vertexStride = vertexDeclaration.VertexStride;
@ -44,21 +45,21 @@ namespace ANX.RenderSystem.Windows.Metro
#endregion
#region InitializeBuffer
private void InitializeBuffer(Dx11.Device device, int vertexCount,
private void InitializeBuffer(Dx.Device device, int vertexCount,
BufferUsage usage)
{
if (device != null)
{
var description = new Dx11.BufferDescription()
var description = new Dx.BufferDescription()
{
Usage = FormatConverter.Translate(usage),
SizeInBytes = vertexStride * vertexCount,
BindFlags = Dx11.BindFlags.VertexBuffer,
CpuAccessFlags = Dx11.CpuAccessFlags.Write,
OptionFlags = Dx11.ResourceOptionFlags.None
BindFlags = Dx.BindFlags.VertexBuffer,
CpuAccessFlags = Dx.CpuAccessFlags.Write,
OptionFlags = Dx.ResourceOptionFlags.None
};
NativeBuffer = new Dx11.Buffer(device, description);
NativeBuffer = new Dx.Buffer(device, description);
}
}
#endregion
@ -84,46 +85,85 @@ namespace ANX.RenderSystem.Windows.Metro
public void SetData<T>(GraphicsDevice graphicsDevice, int offsetInBytes, T[] data,
int startIndex, int elementCount, int vertexStride) where T : struct
{
if (offsetInBytes + elementCount * Marshal.SizeOf(typeof(T)) > 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"));
if (startIndex + elementCount > data.Length)
throw new ArgumentOutOfRangeException(string.Format("The parameters {0} + {1} must be smaller than {2}.", "startIndex", "elementCount", "data.Length"));
GraphicsDeviceWindowsMetro gdMetro = graphicsDevice.NativeDevice as GraphicsDeviceWindowsMetro;
var device = gdMetro.NativeDevice;
//TODO: check offsetInBytes parameter for bounds etc.
SharpDX.DataStream stream = device.MapSubresource(NativeBuffer);
if (offsetInBytes > 0)
stream.Seek(offsetInBytes, SeekOrigin.Current);
if (startIndex > 0 || elementCount < data.Length)
for (int i = startIndex; i < startIndex + elementCount; i++)
stream.Write<T>(data[i]);
else
for (int i = 0; i < data.Length; i++)
stream.Write<T>(data[i]);
device.UnmapSubresource(NativeBuffer, 0);
}
#endregion
#region GetData (TODO)
public void GetData<T>(int offsetInBytes, T[] data, int startIndex,
int elementCount, int vertexStride) where T : struct
{
throw new NotImplementedException();
}
#region GetData
public void GetData<T>(T[] data) where T : struct
{
throw new NotImplementedException();
GetData(data, 0, data.Length);
}
public void GetData<T>(T[] data, int startIndex, int elementCount)
public void GetData<T>(T[] data, int startIndex, int elementCount) where T : struct
{
GetData(0, data, startIndex, elementCount, vertexStride);
}
public void GetData<T>(int offsetInBytes, T[] data, int startIndex, int elementCount, int vertexStride)
where T : struct
{
throw new NotImplementedException();
Dx.Buffer stagingBuffer = CreateStagingBuffer(elementCount * vertexStride);
CopySubresource(NativeBuffer, stagingBuffer);
using (var stream = MapBufferRead(stagingBuffer))
{
if (offsetInBytes > 0)
stream.Seek(offsetInBytes, SeekOrigin.Current);
stream.ReadRange(data, startIndex, elementCount);
UnmapBuffer(stagingBuffer);
}
}
#endregion
private SharpDX.DataStream MapBufferRead(Dx.Resource buffer)
{
SharpDX.DataStream stream;
buffer.Device.ImmediateContext.MapSubresource(buffer, 0, Dx.MapMode.Read, Dx.MapFlags.None, out stream);
return stream;
}
private void CopySubresource(Dx.Buffer source, Dx.Buffer destination)
{
this.NativeBuffer.Device.ImmediateContext.CopyResource(source, destination);
}
private void UnmapBuffer(Dx.Resource buffer)
{
buffer.Device.ImmediateContext.UnmapSubresource(buffer, 0);
}
private Dx.Buffer CreateStagingBuffer(int sizeInBytes)
{
var description = new Dx.BufferDescription()
{
Usage = Dx.ResourceUsage.Staging,
SizeInBytes = sizeInBytes,
CpuAccessFlags = Dx.CpuAccessFlags.Read,
OptionFlags = Dx.ResourceOptionFlags.None,
};
return new Dx.Buffer(NativeBuffer.Device, description);
}
#region Dispose
public void Dispose()
{