- Fixed the rendering on Metro so all Sprites are now correctly drawn

- Refactored the metro FormatConverter
- Refactored and fixed the metro index and vertex buffer
- Implemented setting multiple texture parameters under metro
This commit is contained in:
SND\AstrorEnales_cp 2012-08-20 09:08:07 +00:00
parent 947287b0ab
commit 7ea3cef569
11 changed files with 254 additions and 348 deletions

View File

@ -55,7 +55,7 @@ namespace ANX.RenderSystem.Windows.Metro
public void SetValue(Matrix value) public void SetValue(Matrix value)
{ {
value = Matrix.Transpose(value); value = Matrix.Transpose(value);
parentEffect.paramBuffer.SetParameter(Name, value); parentEffect.paramBuffer.SetParameter(Name, ref value);
} }
#endregion #endregion
@ -74,7 +74,7 @@ namespace ANX.RenderSystem.Windows.Metro
#region SetValue (Quaternion) #region SetValue (Quaternion)
public void SetValue(Quaternion value) public void SetValue(Quaternion value)
{ {
parentEffect.paramBuffer.SetParameter(Name, value); parentEffect.paramBuffer.SetParameter(Name, ref value);
} }
#endregion #endregion
@ -103,7 +103,7 @@ namespace ANX.RenderSystem.Windows.Metro
#region SetValue (Vector2) #region SetValue (Vector2)
public void SetValue(Vector2 value) public void SetValue(Vector2 value)
{ {
parentEffect.paramBuffer.SetParameter(Name, value); parentEffect.paramBuffer.SetParameter(Name, ref value);
} }
#endregion #endregion
@ -117,7 +117,7 @@ namespace ANX.RenderSystem.Windows.Metro
#region SetValue (Vector3) #region SetValue (Vector3)
public void SetValue(Vector3 value) public void SetValue(Vector3 value)
{ {
parentEffect.paramBuffer.SetParameter(Name, value); parentEffect.paramBuffer.SetParameter(Name, ref value);
} }
#endregion #endregion
@ -131,7 +131,7 @@ namespace ANX.RenderSystem.Windows.Metro
#region SetValue (Vector4) #region SetValue (Vector4)
public void SetValue(Vector4 value) public void SetValue(Vector4 value)
{ {
parentEffect.paramBuffer.SetParameter(Name, value); parentEffect.paramBuffer.SetParameter(Name, ref value);
} }
#endregion #endregion
@ -142,14 +142,26 @@ namespace ANX.RenderSystem.Windows.Metro
} }
#endregion #endregion
#region SetValue (Texture) (TODO) #region SetValue (Texture)
public void SetValue(Texture value) public void SetValue(Texture value)
{ {
Texture2D_Metro tex = value.NativeTexture as Texture2D_Metro; Texture2D_Metro tex = value.NativeTexture as Texture2D_Metro;
var context = NativeDxDevice.Current.NativeContext; var context = NativeDxDevice.Current.NativeContext;
// TODO: slot int textureIndex = -1;
context.PixelShader.SetShaderResource(0, tex.NativeShaderResourceView); foreach (var parameter in parentEffect.shader.Parameters)
{
if (parameter.IsTexture)
{
textureIndex++;
if (parameter.Name == Name)
{
context.PixelShader.SetShaderResource(textureIndex, tex.NativeShaderResourceView);
break;
}
}
}
} }
#endregion #endregion
@ -159,7 +171,7 @@ namespace ANX.RenderSystem.Windows.Metro
if (transpose == false) if (transpose == false)
value = Matrix.Transpose(value); value = Matrix.Transpose(value);
parentEffect.paramBuffer.SetParameter(Name, value); parentEffect.paramBuffer.SetParameter(Name, ref value);
} }
#endregion #endregion

View File

@ -10,10 +10,12 @@ using Dx11 = SharpDX.Direct3D11;
namespace ANX.RenderSystem.Windows.Metro namespace ANX.RenderSystem.Windows.Metro
{ {
internal class FormatConverter internal static class FormatConverter
{ {
#region FormatSize private const string InvalidEnumText = "Can't translate ";
public static int FormatSize(SurfaceFormat format)
#region GetSurfaceFormatSize
public static int GetSurfaceFormatSize(SurfaceFormat format)
{ {
switch (format) switch (format)
{ {
@ -36,13 +38,13 @@ namespace ANX.RenderSystem.Windows.Metro
case SurfaceFormat.Dxt5: case SurfaceFormat.Dxt5:
case SurfaceFormat.Alpha8: case SurfaceFormat.Alpha8:
return 1; return 1;
default:
throw new ArgumentException("Invalid format");
} }
throw new ArgumentException("Invalid SurfaceFormat: " + format);
} }
#endregion #endregion
#region Translate #region Translate (SurfaceFormat)
public static SharpDX.DXGI.Format Translate(SurfaceFormat surfaceFormat) public static SharpDX.DXGI.Format Translate(SurfaceFormat surfaceFormat)
{ {
switch (surfaceFormat) switch (surfaceFormat)
@ -55,11 +57,11 @@ namespace ANX.RenderSystem.Windows.Metro
return SharpDX.DXGI.Format.BC3_UNorm; return SharpDX.DXGI.Format.BC3_UNorm;
} }
throw new Exception("can't translate SurfaceFormat: " + surfaceFormat.ToString()); throw new ArgumentException(InvalidEnumText + "SurfaceFormat: " + surfaceFormat);
} }
#endregion #endregion
#region Translate #region Translate (DepthFormat)
public static Format Translate(DepthFormat depthFormat) public static Format Translate(DepthFormat depthFormat)
{ {
switch (depthFormat) switch (depthFormat)
@ -74,28 +76,11 @@ namespace ANX.RenderSystem.Windows.Metro
return Format.Unknown; return Format.Unknown;
} }
throw new Exception("can't translate DepthFormat: " + depthFormat.ToString()); throw new ArgumentException(InvalidEnumText + "DepthFormat: " + depthFormat);
} }
#endregion #endregion
#region Translate #region Translate (TextureFilter)
public static SurfaceFormat Translate(SharpDX.DXGI.Format format)
{
switch (format)
{
case SharpDX.DXGI.Format.R8G8B8A8_UNorm:
return SurfaceFormat.Color;
case SharpDX.DXGI.Format.BC2_UNorm:
return SurfaceFormat.Dxt3;
case SharpDX.DXGI.Format.BC3_UNorm:
return SurfaceFormat.Dxt5;
}
throw new Exception("can't translate Format: " + format.ToString());
}
#endregion
#region Translate
public static Dx11.Filter Translate(TextureFilter filter) public static Dx11.Filter Translate(TextureFilter filter)
{ {
switch (filter) switch (filter)
@ -120,11 +105,11 @@ namespace ANX.RenderSystem.Windows.Metro
return Dx11.Filter.MinMagPointMipLinear; return Dx11.Filter.MinMagPointMipLinear;
} }
throw new NotImplementedException(); throw new ArgumentException(InvalidEnumText + "TextureFilter: " + filter);
} }
#endregion #endregion
#region Translate #region Translate (TextureAddressMode)
public static Dx11.TextureAddressMode Translate(TextureAddressMode addressMode) public static Dx11.TextureAddressMode Translate(TextureAddressMode addressMode)
{ {
switch (addressMode) switch (addressMode)
@ -137,11 +122,26 @@ namespace ANX.RenderSystem.Windows.Metro
return Dx11.TextureAddressMode.Wrap; return Dx11.TextureAddressMode.Wrap;
} }
return Dx11.TextureAddressMode.Clamp; throw new ArgumentException(InvalidEnumText + "TextureAddressMode: " + addressMode);
} }
#endregion #endregion
#region Translate #region Translate (BufferUsage)
public static Dx11.ResourceUsage Translate(BufferUsage usage)
{
switch (usage)
{
case BufferUsage.None:
return Dx11.ResourceUsage.Default;
case BufferUsage.WriteOnly:
return Dx11.ResourceUsage.Dynamic;
}
throw new ArgumentException(InvalidEnumText + "BufferUsage: " + usage);
}
#endregion
#region Translate (PrimitiveType)
public static PrimitiveTopology Translate(PrimitiveType primitiveType) public static PrimitiveTopology Translate(PrimitiveType primitiveType)
{ {
switch (primitiveType) switch (primitiveType)
@ -154,13 +154,13 @@ namespace ANX.RenderSystem.Windows.Metro
return PrimitiveTopology.TriangleList; return PrimitiveTopology.TriangleList;
case PrimitiveType.TriangleStrip: case PrimitiveType.TriangleStrip:
return PrimitiveTopology.TriangleStrip; return PrimitiveTopology.TriangleStrip;
default:
throw new InvalidOperationException("unknown PrimitiveType: " + primitiveType.ToString());
} }
throw new ArgumentException(InvalidEnumText + "PrimitiveType: " + primitiveType);
} }
#endregion #endregion
#region Translate #region Translate (IndexElementSize)
public static SharpDX.DXGI.Format Translate(IndexElementSize indexElementSize) public static SharpDX.DXGI.Format Translate(IndexElementSize indexElementSize)
{ {
switch (indexElementSize) switch (indexElementSize)
@ -169,13 +169,13 @@ namespace ANX.RenderSystem.Windows.Metro
return Format.R16_UInt; return Format.R16_UInt;
case IndexElementSize.ThirtyTwoBits: case IndexElementSize.ThirtyTwoBits:
return Format.R32_UInt; return Format.R32_UInt;
default:
throw new InvalidOperationException("unknown IndexElementSize: " + indexElementSize.ToString());
} }
throw new ArgumentException(InvalidEnumText + "IndexElementSize: " + indexElementSize);
} }
#endregion #endregion
#region Translate #region Translate (VertexElementUsage) (TODO)
public static string Translate(VertexElementUsage usage) public static string Translate(VertexElementUsage usage)
{ {
//TODO: map the other Usages //TODO: map the other Usages
@ -183,14 +183,12 @@ namespace ANX.RenderSystem.Windows.Metro
{ {
return "TEXCOORD"; return "TEXCOORD";
} }
else
{ return usage.ToString().ToUpperInvariant();
return usage.ToString().ToUpperInvariant();
}
} }
#endregion #endregion
#region Translate #region Translate (BlendFunction)
public static Dx11.BlendOperation Translate(BlendFunction blendFunction) public static Dx11.BlendOperation Translate(BlendFunction blendFunction)
{ {
switch (blendFunction) switch (blendFunction)
@ -207,11 +205,11 @@ namespace ANX.RenderSystem.Windows.Metro
return Dx11.BlendOperation.Subtract; return Dx11.BlendOperation.Subtract;
} }
throw new NotImplementedException(); throw new ArgumentException(InvalidEnumText + "BlendFunction: " + blendFunction);
} }
#endregion #endregion
#region Translate #region Translate (Blend)
public static Dx11.BlendOption Translate(Blend blend) public static Dx11.BlendOption Translate(Blend blend)
{ {
switch (blend) switch (blend)
@ -244,45 +242,35 @@ namespace ANX.RenderSystem.Windows.Metro
return Dx11.BlendOption.Zero; return Dx11.BlendOption.Zero;
} }
throw new NotImplementedException(); throw new ArgumentException(InvalidEnumText + "Blend: " + blend);
} }
#endregion #endregion
#region Translate #region Translate (ColorWriteChannels)
public static Dx11.ColorWriteMaskFlags Translate(ColorWriteChannels colorWriteChannels) public static Dx11.ColorWriteMaskFlags Translate(ColorWriteChannels colorWriteChannels)
{ {
Dx11.ColorWriteMaskFlags mask = 0; Dx11.ColorWriteMaskFlags mask = 0;
if ((colorWriteChannels & ColorWriteChannels.All) > 0) if ((colorWriteChannels & ColorWriteChannels.All) > 0)
{
mask |= Dx11.ColorWriteMaskFlags.All; mask |= Dx11.ColorWriteMaskFlags.All;
}
if ((colorWriteChannels & ColorWriteChannels.Alpha) > 0) if ((colorWriteChannels & ColorWriteChannels.Alpha) > 0)
{
mask |= Dx11.ColorWriteMaskFlags.Alpha; mask |= Dx11.ColorWriteMaskFlags.Alpha;
}
if ((colorWriteChannels & ColorWriteChannels.Blue) > 0) if ((colorWriteChannels & ColorWriteChannels.Blue) > 0)
{
mask |= Dx11.ColorWriteMaskFlags.Blue; mask |= Dx11.ColorWriteMaskFlags.Blue;
}
if ((colorWriteChannels & ColorWriteChannels.Green) > 0) if ((colorWriteChannels & ColorWriteChannels.Green) > 0)
{
mask |= Dx11.ColorWriteMaskFlags.Green; mask |= Dx11.ColorWriteMaskFlags.Green;
}
if ((colorWriteChannels & ColorWriteChannels.Red) > 0) if ((colorWriteChannels & ColorWriteChannels.Red) > 0)
{
mask |= Dx11.ColorWriteMaskFlags.Red; mask |= Dx11.ColorWriteMaskFlags.Red;
}
return mask; return mask;
} }
#endregion #endregion
#region Translate #region Translate (StencilOperation)
public static Dx11.StencilOperation Translate(StencilOperation stencilOperation) public static Dx11.StencilOperation Translate(StencilOperation stencilOperation)
{ {
switch (stencilOperation) switch (stencilOperation)
@ -305,11 +293,11 @@ namespace ANX.RenderSystem.Windows.Metro
return Dx11.StencilOperation.Zero; return Dx11.StencilOperation.Zero;
} }
throw new NotImplementedException("unknown StencilOperation"); throw new ArgumentException(InvalidEnumText + "StencilOperation: " + stencilOperation);
} }
#endregion #endregion
#region Translate #region Translate (CompareFunction)
public static Dx11.Comparison Translate(CompareFunction compareFunction) public static Dx11.Comparison Translate(CompareFunction compareFunction)
{ {
switch (compareFunction) switch (compareFunction)
@ -332,39 +320,58 @@ namespace ANX.RenderSystem.Windows.Metro
return Dx11.Comparison.NotEqual; return Dx11.Comparison.NotEqual;
} }
throw new NotImplementedException("unknown CompareFunction"); throw new ArgumentException(InvalidEnumText + "CompareFunction: " + compareFunction);
}
#endregion
#region Translate
public static Dx11.CullMode Translate(CullMode cullMode)
{
if (cullMode == CullMode.CullClockwiseFace)
{
return Dx11.CullMode.Front;
}
else if (cullMode == CullMode.CullCounterClockwiseFace)
{
return Dx11.CullMode.Back;
}
else
{
return Dx11.CullMode.None;
}
} }
#endregion #endregion
#region Translate #region Translate (CullMode)
public static Dx11.CullMode Translate(CullMode cullMode)
{
switch (cullMode)
{
case CullMode.CullClockwiseFace:
return Dx11.CullMode.Front;
case CullMode.CullCounterClockwiseFace:
return Dx11.CullMode.Back;
case CullMode.None:
return Dx11.CullMode.None;
}
throw new ArgumentException(InvalidEnumText + "CullMode: " + cullMode);
}
#endregion
#region Translate (FillMode)
public static Dx11.FillMode Translate(FillMode fillMode) public static Dx11.FillMode Translate(FillMode fillMode)
{ {
if (fillMode == FillMode.WireFrame) switch (fillMode)
{ {
return Dx11.FillMode.Wireframe; case FillMode.WireFrame:
return Dx11.FillMode.Wireframe;
case FillMode.Solid:
return Dx11.FillMode.Solid;
} }
else
throw new ArgumentException(InvalidEnumText + "FillMode: " + fillMode);
}
#endregion
#region Translate (VertexElementFormat)
public static Format Translate(VertexElementFormat elementFormat)
{
switch (elementFormat)
{ {
return Dx11.FillMode.Solid; case VertexElementFormat.Vector2:
return Format.R32G32_Float;
case VertexElementFormat.Vector3:
return Format.R32G32B32_Float;
case VertexElementFormat.Vector4:
return Format.R32G32B32A32_Float;
case VertexElementFormat.Color:
return Format.R8G8B8A8_UNorm;
} }
throw new ArgumentException(InvalidEnumText + "VertexElementFormat: " + elementFormat);
} }
#endregion #endregion
} }

View File

@ -116,7 +116,7 @@ namespace ANX.RenderSystem.Windows.Metro
NativeDevice.Present(this.vSyncEnabled ? 1 : 0); NativeDevice.Present(this.vSyncEnabled ? 1 : 0);
} }
#endregion // Present #endregion
#region DrawIndexedPrimitives #region DrawIndexedPrimitives
public void DrawIndexedPrimitives(PrimitiveType primitiveType, int baseVertex, public void DrawIndexedPrimitives(PrimitiveType primitiveType, int baseVertex,
@ -125,7 +125,6 @@ namespace ANX.RenderSystem.Windows.Metro
SetInputLayout(); SetInputLayout();
ApplyPrimitiveType(primitiveType); ApplyPrimitiveType(primitiveType);
NativeDevice.SetDefaultTargets(); NativeDevice.SetDefaultTargets();
//d3dContext.PixelShader.SetSampler(0, sampler);
int indexCount = CalculateVertexCount(primitiveType, primitiveCount); int indexCount = CalculateVertexCount(primitiveType, primitiveCount);
for (int passIndex = 0; passIndex < currentTechnique.PassCount; passIndex++) for (int passIndex = 0; passIndex < currentTechnique.PassCount; passIndex++)
@ -142,7 +141,6 @@ namespace ANX.RenderSystem.Windows.Metro
SetInputLayout(); SetInputLayout();
ApplyPrimitiveType(primitiveType); ApplyPrimitiveType(primitiveType);
NativeDevice.SetDefaultTargets(); NativeDevice.SetDefaultTargets();
//d3dContext.PixelShader.SetSampler(0, sampler);
for (int passIndex = 0; passIndex < currentTechnique.PassCount; passIndex++) for (int passIndex = 0; passIndex < currentTechnique.PassCount; passIndex++)
{ {
@ -317,27 +315,10 @@ namespace ANX.RenderSystem.Windows.Metro
private Dx11.InputElement CreateInputElementFromVertexElement(VertexElement vertexElement) private Dx11.InputElement CreateInputElementFromVertexElement(VertexElement vertexElement)
{ {
string elementName = FormatConverter.Translate(vertexElement.VertexElementUsage); string elementName = FormatConverter.Translate(vertexElement.VertexElementUsage);
Format elementFormat = FormatConverter.Translate(vertexElement.VertexElementFormat);
Format elementFormat; return new Dx11.InputElement(elementName, vertexElement.UsageIndex, elementFormat,
switch (vertexElement.VertexElementFormat) vertexElement.Offset, 0);
{
case VertexElementFormat.Vector2:
elementFormat = Format.R32G32_Float;
break;
case VertexElementFormat.Vector3:
elementFormat = Format.R32G32B32_Float;
break;
case VertexElementFormat.Vector4:
elementFormat = Format.R32G32B32A32_Float;
break;
case VertexElementFormat.Color:
elementFormat = Format.R8G8B8A8_UNorm;
break;
default:
throw new Exception("can't map '" + vertexElement.VertexElementFormat.ToString() + "' to DXGI.Format in DirectX10 RenderSystem CreateInputElementFromVertexElement");
}
return new Dx11.InputElement(elementName, vertexElement.UsageIndex, elementFormat, vertexElement.Offset, 0);
} }
#endregion #endregion

View File

@ -1,6 +1,4 @@
using System; using System;
using System.IO;
using System.Runtime.InteropServices;
using ANX.Framework.Graphics; using ANX.Framework.Graphics;
using ANX.Framework.NonXNA.RenderSystem; using ANX.Framework.NonXNA.RenderSystem;
using Dx11 = SharpDX.Direct3D11; using Dx11 = SharpDX.Direct3D11;
@ -14,18 +12,14 @@ namespace ANX.RenderSystem.Windows.Metro
public class IndexBuffer_Metro : INativeIndexBuffer, IDisposable public class IndexBuffer_Metro : INativeIndexBuffer, IDisposable
{ {
#region Private #region Private
private Dx11.Buffer buffer;
private IndexElementSize size;
private int indexSizeInBytes; private int indexSizeInBytes;
#endregion #endregion
#region Public #region Public
public Dx11.Buffer NativeBuffer public Dx11.Buffer NativeBuffer
{ {
get get;
{ private set;
return this.buffer;
}
} }
#endregion #endregion
@ -33,13 +27,9 @@ namespace ANX.RenderSystem.Windows.Metro
public IndexBuffer_Metro(GraphicsDevice graphics, IndexElementSize size, public IndexBuffer_Metro(GraphicsDevice graphics, IndexElementSize size,
int indexCount, BufferUsage usage) int indexCount, BufferUsage usage)
{ {
this.size = size;
indexSizeInBytes = size == IndexElementSize.SixteenBits ? 2 : 4; indexSizeInBytes = size == IndexElementSize.SixteenBits ? 2 : 4;
//TODO: translate and use usage GraphicsDeviceWindowsMetro gdMetro = graphics.NativeDevice as GraphicsDeviceWindowsMetro;
GraphicsDeviceWindowsMetro gdMetro =
graphics.NativeDevice as GraphicsDeviceWindowsMetro;
var device = gdMetro.NativeDevice.NativeDevice; var device = gdMetro.NativeDevice.NativeDevice;
InitializeBuffer(device, indexCount, usage); InitializeBuffer(device, indexCount, usage);
@ -48,8 +38,7 @@ namespace ANX.RenderSystem.Windows.Metro
internal IndexBuffer_Metro(Dx11.Device device, IndexElementSize size, internal IndexBuffer_Metro(Dx11.Device device, IndexElementSize size,
int indexCount, BufferUsage usage) int indexCount, BufferUsage usage)
{ {
this.size = size; indexSizeInBytes = size == IndexElementSize.SixteenBits ? 2 : 4;
InitializeBuffer(device, indexCount, usage); InitializeBuffer(device, indexCount, usage);
} }
#endregion #endregion
@ -60,14 +49,14 @@ namespace ANX.RenderSystem.Windows.Metro
{ {
var description = new Dx11.BufferDescription() var description = new Dx11.BufferDescription()
{ {
Usage = Dx11.ResourceUsage.Dynamic, Usage = FormatConverter.Translate(usage),
SizeInBytes = indexSizeInBytes * indexCount, SizeInBytes = indexSizeInBytes * indexCount,
BindFlags = Dx11.BindFlags.IndexBuffer, BindFlags = Dx11.BindFlags.IndexBuffer,
CpuAccessFlags = Dx11.CpuAccessFlags.Write, CpuAccessFlags = Dx11.CpuAccessFlags.Write,
OptionFlags = Dx11.ResourceOptionFlags.None OptionFlags = Dx11.ResourceOptionFlags.None
}; };
this.buffer = new Dx11.Buffer(device, description); NativeBuffer = new Dx11.Buffer(device, description);
} }
#endregion #endregion
@ -77,50 +66,37 @@ namespace ANX.RenderSystem.Windows.Metro
SetData<T>(graphicsDevice, data, 0, data.Length); SetData<T>(graphicsDevice, data, 0, data.Length);
} }
public void SetData<T>(GraphicsDevice graphicsDevice, int offsetInBytes, public void SetData<T>(GraphicsDevice graphicsDevice, T[] data, int startIndex,
T[] data, int startIndex, int elementCount) where T : struct int elementCount) where T : struct
{
//TODO: check offsetInBytes parameter for bounds etc.
GCHandle pinnedArray = GCHandle.Alloc(data, GCHandleType.Pinned);
IntPtr dataPointer = pinnedArray.AddrOfPinnedObject();
int dataLength = Marshal.SizeOf(typeof(T)) * data.Length;
unsafe
{
using (var vData = new SharpDX.DataStream(dataPointer, dataLength, true, true))
{
if (offsetInBytes > 0)
{
vData.Seek(offsetInBytes / indexSizeInBytes, SeekOrigin.Begin);
}
SharpDX.DataStream stream;
SharpDX.DataBox box = NativeDxDevice.Current.MapSubresource(buffer, out stream);
if (startIndex > 0 || elementCount < data.Length)
{
for (int i = startIndex; i < startIndex + elementCount; i++)
{
vData.Write<T>(data[i]);
}
}
else
{
vData.CopyTo(stream);
}
NativeDxDevice.Current.UnmapSubresource(buffer, 0);
}
}
pinnedArray.Free();
}
public void SetData<T>(GraphicsDevice graphicsDevice, T[] data,
int startIndex, int elementCount) where T : struct
{ {
SetData<T>(graphicsDevice, 0, data, startIndex, elementCount); SetData<T>(graphicsDevice, 0, data, startIndex, elementCount);
} }
public void SetData<T>(GraphicsDevice graphicsDevice, int offsetInBytes, T[] data,
int startIndex, int elementCount) where T : struct
{
GraphicsDeviceWindowsMetro gdMetro = graphicsDevice.NativeDevice as GraphicsDeviceWindowsMetro;
var device = gdMetro.NativeDevice;
//TODO: check offsetInBytes parameter for bounds etc.
SharpDX.DataStream stream = device.MapSubresource(NativeBuffer);
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 #endregion
#region GetData (TODO) #region GetData (TODO)
@ -145,10 +121,10 @@ namespace ANX.RenderSystem.Windows.Metro
#region Dispose #region Dispose
public void Dispose() public void Dispose()
{ {
if (buffer != null) if (NativeBuffer != null)
{ {
buffer.Dispose(); NativeBuffer.Dispose();
buffer = null; NativeBuffer = null;
} }
} }
#endregion #endregion

View File

@ -1,9 +1,9 @@
using System; using System;
using ANX.Framework.Graphics;
using SharpDX; using SharpDX;
using SharpDX.Direct3D; using SharpDX.Direct3D;
using Windows.Foundation; using Windows.Foundation;
using Dx11 = SharpDX.Direct3D11; using Dx11 = SharpDX.Direct3D11;
using PresentationParameters = ANX.Framework.Graphics.PresentationParameters;
// This file is part of the ANX.Framework created by the // This file is part of the ANX.Framework created by the
// "ANX.Framework developer group" and released under the Ms-PL license. // "ANX.Framework developer group" and released under the Ms-PL license.
@ -133,11 +133,13 @@ namespace ANX.RenderSystem.Windows.Metro
} }
#endregion #endregion
public void SetDefaultTargets() #region SetDefaultTargets
public void SetDefaultTargets()
{ {
nativeContext.Rasterizer.SetViewports(viewport); nativeContext.Rasterizer.SetViewports(viewport);
nativeContext.OutputMerger.SetTargets(depthStencilView, renderTargetView); nativeContext.OutputMerger.SetTargets(depthStencilView, renderTargetView);
} }
#endregion
#region Clear #region Clear
public void Clear(Color4 color) public void Clear(Color4 color)
@ -167,11 +169,12 @@ namespace ANX.RenderSystem.Windows.Metro
#endregion #endregion
#region MapSubresource #region MapSubresource
public SharpDX.DataBox MapSubresource(Dx11.Buffer resource, public SharpDX.DataStream MapSubresource(Dx11.Buffer resource)
out SharpDX.DataStream stream)
{ {
return nativeContext.MapSubresource(resource, Dx11.MapMode.WriteDiscard, SharpDX.DataStream result;
Dx11.MapFlags.None, out stream); nativeContext.MapSubresource(resource, Dx11.MapMode.WriteDiscard,
Dx11.MapFlags.None, out result);
return result;
} }
public SharpDX.DataBox MapSubresource(Dx11.Resource resource, int subresource) public SharpDX.DataBox MapSubresource(Dx11.Resource resource, int subresource)

View File

@ -46,7 +46,7 @@ namespace ANX.RenderSystem.Windows.Metro
// description of texture formats of DX10: http://msdn.microsoft.com/en-us/library/bb694531(v=VS.85).aspx // description of texture formats of DX10: http://msdn.microsoft.com/en-us/library/bb694531(v=VS.85).aspx
// more helpfull information on DX10 textures: http://msdn.microsoft.com/en-us/library/windows/desktop/bb205131(v=vs.85).aspx // more helpfull information on DX10 textures: http://msdn.microsoft.com/en-us/library/windows/desktop/bb205131(v=vs.85).aspx
this.formatSize = FormatConverter.FormatSize(surfaceFormat); this.formatSize = FormatConverter.GetSurfaceFormatSize(surfaceFormat);
} }
#endregion #endregion
} }

View File

@ -22,8 +22,7 @@ namespace ANX.RenderSystem.Windows.Metro
set set
{ {
Dx11.TextureAddressMode mode = FormatConverter.Translate(value); Dx11.TextureAddressMode mode = FormatConverter.Translate(value);
SetValueIfDifferentAndMarkDirty( SetValueIfDifferentAndMarkDirty(ref description.AddressU, ref mode);
ref description.AddressU, ref mode);
} }
} }
@ -32,8 +31,7 @@ namespace ANX.RenderSystem.Windows.Metro
set set
{ {
Dx11.TextureAddressMode mode = FormatConverter.Translate(value); Dx11.TextureAddressMode mode = FormatConverter.Translate(value);
SetValueIfDifferentAndMarkDirty( SetValueIfDifferentAndMarkDirty(ref description.AddressV, ref mode);
ref description.AddressV, ref mode);
} }
} }
@ -42,8 +40,7 @@ namespace ANX.RenderSystem.Windows.Metro
set set
{ {
Dx11.TextureAddressMode mode = FormatConverter.Translate(value); Dx11.TextureAddressMode mode = FormatConverter.Translate(value);
SetValueIfDifferentAndMarkDirty( SetValueIfDifferentAndMarkDirty(ref description.AddressW, ref mode);
ref description.AddressW, ref mode);
} }
} }
@ -52,8 +49,7 @@ namespace ANX.RenderSystem.Windows.Metro
set set
{ {
Dx11.Filter filter = FormatConverter.Translate(value); Dx11.Filter filter = FormatConverter.Translate(value);
SetValueIfDifferentAndMarkDirty( SetValueIfDifferentAndMarkDirty(ref description.Filter, ref filter);
ref description.Filter, ref filter);
} }
} }
@ -61,8 +57,7 @@ namespace ANX.RenderSystem.Windows.Metro
{ {
set set
{ {
SetValueIfDifferentAndMarkDirty( SetValueIfDifferentAndMarkDirty(ref description.MaximumAnisotropy, ref value);
ref description.MaximumAnisotropy, ref value);
} }
} }
@ -82,8 +77,7 @@ namespace ANX.RenderSystem.Windows.Metro
{ {
set set
{ {
SetValueIfDifferentAndMarkDirty( SetValueIfDifferentAndMarkDirty(ref description.MipLodBias, ref value);
ref description.MipLodBias, ref value);
} }
} }
#endregion #endregion
@ -101,9 +95,9 @@ namespace ANX.RenderSystem.Windows.Metro
{ {
UpdateNativeSamplerState(); UpdateNativeSamplerState();
bound = true; bound = true;
NativeDxDevice.Current.NativeContext.PixelShader.SetSampler( NativeDxDevice.Current.NativeContext.PixelShader.SetSampler(
index, this.nativeSamplerState); index, this.nativeSamplerState);
} }
#endregion #endregion
@ -123,14 +117,10 @@ namespace ANX.RenderSystem.Windows.Metro
{ {
if (isDirty == true || nativeSamplerState == null) if (isDirty == true || nativeSamplerState == null)
{ {
if (nativeSamplerState != null) Dispose();
{
nativeSamplerState.Dispose();
nativeSamplerState = null;
}
// TODO: otherwise crashes for now // TODO: otherwise crashes for now
description.MaximumLod = float.MaxValue; description.MaximumLod = float.MaxValue;
nativeSamplerState = new Dx11.SamplerState( nativeSamplerState = new Dx11.SamplerState(
NativeDxDevice.Current.NativeDevice, ref description); NativeDxDevice.Current.NativeDevice, ref description);

View File

@ -40,18 +40,27 @@ namespace ANX.RenderSystem.Windows.Metro.Shader
parameterOffsets = offsets.ToArray(); parameterOffsets = offsets.ToArray();
setData = new byte[dataSize]; setData = new byte[dataSize];
nativeBuffer = new Dx11.Buffer(graphicsDevice.NativeDevice, dataSize,
Dx11.ResourceUsage.Default, Dx11.BindFlags.ConstantBuffer,
Dx11.CpuAccessFlags.None, Dx11.ResourceOptionFlags.None, 0);
} }
#endregion #endregion
#region SetParameter (T) #region SetParameter (T)
public void SetParameter<T>(string parameterName, T value) where T : struct public void SetParameter<T>(string parameterName, ref T value) where T : struct
{ {
int indexOfParameter = FindParameterIndex(parameterName); int indexOfParameter = FindParameterIndex(parameterName);
if (indexOfParameter == -1) if (indexOfParameter == -1)
return; return;
byte[] dataToAdd = StructureToBytes(value); int size = Marshal.SizeOf(typeof(T));
Array.Copy(dataToAdd, 0, setData, parameterOffsets[indexOfParameter], dataToAdd.Length); byte[] dataToAdd = new byte[size];
IntPtr ptr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(value, ptr, true);
Marshal.Copy(ptr, setData, parameterOffsets[indexOfParameter], size);
Marshal.FreeHGlobal(ptr);
} }
#endregion #endregion
@ -61,9 +70,7 @@ namespace ANX.RenderSystem.Windows.Metro.Shader
int indexOfParameter = FindParameterIndex(parameterName); int indexOfParameter = FindParameterIndex(parameterName);
if (indexOfParameter == -1) if (indexOfParameter == -1)
return; return;
value = FillArrayIfNeeded(value, indexOfParameter);
int sizePerItem = Marshal.SizeOf(typeof(T)); int sizePerItem = Marshal.SizeOf(typeof(T));
int offset = 0; int offset = 0;
IntPtr ptr = Marshal.AllocHGlobal(sizePerItem); IntPtr ptr = Marshal.AllocHGlobal(sizePerItem);
@ -83,9 +90,7 @@ namespace ANX.RenderSystem.Windows.Metro.Shader
int indexOfParameter = FindParameterIndex(parameterName); int indexOfParameter = FindParameterIndex(parameterName);
if (indexOfParameter == -1) if (indexOfParameter == -1)
return; return;
value = FillArrayIfNeeded(value, indexOfParameter);
byte[] result = UnionArraySerializer.Unify(value); byte[] result = UnionArraySerializer.Unify(value);
Array.Copy(result, 0, setData, parameterOffsets[indexOfParameter], result.Length); Array.Copy(result, 0, setData, parameterOffsets[indexOfParameter], result.Length);
} }
@ -97,9 +102,7 @@ namespace ANX.RenderSystem.Windows.Metro.Shader
int indexOfParameter = FindParameterIndex(parameterName); int indexOfParameter = FindParameterIndex(parameterName);
if (indexOfParameter == -1) if (indexOfParameter == -1)
return; return;
value = FillArrayIfNeeded(value, indexOfParameter);
byte[] result = UnionArraySerializer.Unify(value); byte[] result = UnionArraySerializer.Unify(value);
Array.Copy(result, 0, setData, parameterOffsets[indexOfParameter], result.Length); Array.Copy(result, 0, setData, parameterOffsets[indexOfParameter], result.Length);
} }
@ -116,21 +119,6 @@ namespace ANX.RenderSystem.Windows.Metro.Shader
} }
#endregion #endregion
#region StructureToBytes
private byte[] StructureToBytes<T>(T value) where T : struct
{
int size = Marshal.SizeOf(value);
byte[] result = new byte[size];
IntPtr ptr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(value, ptr, true);
Marshal.Copy(ptr, result, 0, size);
Marshal.FreeHGlobal(ptr);
return result;
}
#endregion
#region FindParameterIndex #region FindParameterIndex
private int FindParameterIndex(string parameterName) private int FindParameterIndex(string parameterName)
{ {
@ -138,9 +126,7 @@ namespace ANX.RenderSystem.Windows.Metro.Shader
foreach (var parameter in parentEffect.shader.Parameters) foreach (var parameter in parentEffect.shader.Parameters)
{ {
if (parameter.Name == parameterName) if (parameter.Name == parameterName)
{
return searchIndex; return searchIndex;
}
searchIndex++; searchIndex++;
} }
@ -148,50 +134,23 @@ namespace ANX.RenderSystem.Windows.Metro.Shader
return -1; return -1;
} }
#endregion #endregion
#region FillArrayIfNeeded
private T[] FillArrayIfNeeded<T>(T[] original, int parameterIndex) where T : struct
{
int paramArraySize = parentEffect.shader.Parameters[parameterIndex].ArraySize;
if (paramArraySize > 0)
{
T[] filledArray = new T[paramArraySize];
Array.Copy(original, filledArray, original.Length);
return filledArray;
}
return original;
}
#endregion
#region Apply #region Apply
public void Apply() public void Apply()
{ {
var data = CreateBufferData(); graphicsDevice.NativeContext.VertexShader.SetConstantBuffer(0, nativeBuffer);
nativeBuffer = new Dx11.Buffer(graphicsDevice.NativeDevice, dataSize, IntPtr dataPtr;
Dx11.ResourceUsage.Default, Dx11.BindFlags.ConstantBuffer, unsafe
Dx11.CpuAccessFlags.None, Dx11.ResourceOptionFlags.None, 0); {
graphicsDevice.NativeContext.VertexShader.SetConstantBuffer(0, nativeBuffer); fixed (byte* ptr = &setData[0])
graphicsDevice.NativeContext.UpdateSubresource(data, nativeBuffer); dataPtr = (IntPtr)ptr;
} }
#endregion
#region CreateBufferData
private SharpDX.DataBox CreateBufferData()
{
IntPtr dataPtr;
unsafe
{
fixed (byte* ptr = &setData[0])
{
dataPtr = (IntPtr)ptr;
}
}
// Reset really needed? evaluate // Reset really needed? evaluate
setData = new byte[dataSize]; setData = new byte[dataSize];
return new SharpDX.DataBox(dataPtr); var dataBox = new SharpDX.DataBox(dataPtr);
graphicsDevice.NativeContext.UpdateSubresource(dataBox, nativeBuffer);
} }
#endregion #endregion

View File

@ -17,6 +17,7 @@ namespace ANX.RenderSystem.Windows.Metro
private SwapChain1 swapChain; private SwapChain1 swapChain;
private NativeDxDevice graphicsDevice; private NativeDxDevice graphicsDevice;
private PresentationParameters presentationParameters; private PresentationParameters presentationParameters;
private PresentParameters presentParameters;
#endregion #endregion
#region Constructor #region Constructor
@ -25,6 +26,8 @@ namespace ANX.RenderSystem.Windows.Metro
{ {
graphicsDevice = setGraphicsDevice; graphicsDevice = setGraphicsDevice;
this.presentationParameters = presentationParameters; this.presentationParameters = presentationParameters;
presentParameters = new PresentParameters();
} }
#endregion #endregion
@ -71,7 +74,7 @@ namespace ANX.RenderSystem.Windows.Metro
var comWindow = new ComObject(gameWindow.Form); var comWindow = new ComObject(gameWindow.Form);
swapChain = dxgiFactory2.CreateSwapChainForCoreWindow(graphicsDevice.NativeDevice, swapChain = dxgiFactory2.CreateSwapChainForCoreWindow(graphicsDevice.NativeDevice,
comWindow, ref desc, null); comWindow, ref desc, null);
dxgiDevice2.MaximumFrameLatency = 1; dxgiDevice2.MaximumFrameLatency = 1;
} }
} }
@ -98,12 +101,10 @@ namespace ANX.RenderSystem.Windows.Metro
#region Present #region Present
public void Present(int interval) public void Present(int interval)
{ {
var parameters = new PresentParameters();
if (swapChain == null) if (swapChain == null)
{
ResizeOrCreate(presentationParameters); ResizeOrCreate(presentationParameters);
}
swapChain.Present(interval, PresentFlags.None, parameters); swapChain.Present(interval, PresentFlags.None, presentParameters);
} }
#endregion #endregion

View File

@ -83,7 +83,7 @@ namespace ANX.RenderSystem.Windows.Metro
// description of texture formats of DX10: http://msdn.microsoft.com/en-us/library/bb694531(v=VS.85).aspx // description of texture formats of DX10: http://msdn.microsoft.com/en-us/library/bb694531(v=VS.85).aspx
// more helpfull information on DX10 textures: http://msdn.microsoft.com/en-us/library/windows/desktop/bb205131(v=vs.85).aspx // more helpfull information on DX10 textures: http://msdn.microsoft.com/en-us/library/windows/desktop/bb205131(v=vs.85).aspx
this.formatSize = FormatConverter.FormatSize(surfaceFormat); this.formatSize = FormatConverter.GetSurfaceFormatSize(surfaceFormat);
} }
#endregion #endregion

View File

@ -1,6 +1,4 @@
using System; using System;
using System.IO;
using System.Runtime.InteropServices;
using ANX.Framework.Graphics; using ANX.Framework.Graphics;
using ANX.Framework.NonXNA.RenderSystem; using ANX.Framework.NonXNA.RenderSystem;
using Dx11 = SharpDX.Direct3D11; using Dx11 = SharpDX.Direct3D11;
@ -14,34 +12,30 @@ namespace ANX.RenderSystem.Windows.Metro
public class VertexBuffer_Metro : INativeVertexBuffer, IDisposable public class VertexBuffer_Metro : INativeVertexBuffer, IDisposable
{ {
#region Private #region Private
private Dx11.Buffer buffer;
private int vertexStride; private int vertexStride;
#endregion #endregion
#region Public #region Public
public Dx11.Buffer NativeBuffer public Dx11.Buffer NativeBuffer
{ {
get get;
{ private set;
return this.buffer;
}
} }
#endregion #endregion
#region Constructor #region Constructor
public VertexBuffer_Metro(GraphicsDevice graphics, public VertexBuffer_Metro(GraphicsDevice graphics, VertexDeclaration vertexDeclaration,
VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) int vertexCount, BufferUsage usage)
{ {
GraphicsDeviceWindowsMetro gdMetro = GraphicsDeviceWindowsMetro gdMetro = graphics.NativeDevice as GraphicsDeviceWindowsMetro;
graphics.NativeDevice as GraphicsDeviceWindowsMetro;
var device = gdMetro.NativeDevice.NativeDevice; var device = gdMetro.NativeDevice.NativeDevice;
vertexStride = vertexDeclaration.VertexStride; vertexStride = vertexDeclaration.VertexStride;
InitializeBuffer(device, vertexCount, usage); InitializeBuffer(device, vertexCount, usage);
} }
internal VertexBuffer_Metro(Dx11.Device device, internal VertexBuffer_Metro(Dx11.Device device, VertexDeclaration vertexDeclaration,
VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) int vertexCount, BufferUsage usage)
{ {
vertexStride = vertexDeclaration.VertexStride; vertexStride = vertexDeclaration.VertexStride;
InitializeBuffer(device, vertexCount, usage); InitializeBuffer(device, vertexCount, usage);
@ -52,20 +46,18 @@ namespace ANX.RenderSystem.Windows.Metro
private void InitializeBuffer(Dx11.Device device, int vertexCount, private void InitializeBuffer(Dx11.Device device, int vertexCount,
BufferUsage usage) BufferUsage usage)
{ {
//TODO: translate and use usage
if (device != null) if (device != null)
{ {
var description = new Dx11.BufferDescription() var description = new Dx11.BufferDescription()
{ {
Usage = Dx11.ResourceUsage.Dynamic, Usage = FormatConverter.Translate(usage),
SizeInBytes = vertexStride * vertexCount, SizeInBytes = vertexStride * vertexCount,
BindFlags = Dx11.BindFlags.VertexBuffer, BindFlags = Dx11.BindFlags.VertexBuffer,
CpuAccessFlags = Dx11.CpuAccessFlags.Write, CpuAccessFlags = Dx11.CpuAccessFlags.Write,
OptionFlags = Dx11.ResourceOptionFlags.None OptionFlags = Dx11.ResourceOptionFlags.None
}; };
buffer = new Dx11.Buffer(device, description); NativeBuffer = new Dx11.Buffer(device, description);
} }
} }
#endregion #endregion
@ -74,59 +66,44 @@ namespace ANX.RenderSystem.Windows.Metro
public void SetData<T>(GraphicsDevice graphicsDevice, int offsetInBytes, public void SetData<T>(GraphicsDevice graphicsDevice, int offsetInBytes,
T[] data, int startIndex, int elementCount) where T : struct T[] data, int startIndex, int elementCount) where T : struct
{ {
//TODO: check offsetInBytes parameter for bounds etc. SetData<T>(graphicsDevice, offsetInBytes, data, startIndex, elementCount, vertexStride);
GCHandle pinnedArray = GCHandle.Alloc(data, GCHandleType.Pinned);
IntPtr dataPointer = pinnedArray.AddrOfPinnedObject();
int dataLength = Marshal.SizeOf(typeof(T)) * data.Length;
unsafe
{
using (var vData = new SharpDX.DataStream(dataPointer, dataLength, true, true))
{
if (offsetInBytes > 0)
{
vData.Seek(offsetInBytes / vertexStride, SeekOrigin.Begin);
}
SharpDX.DataStream stream;
SharpDX.DataBox box = NativeDxDevice.Current.MapSubresource(buffer, out stream);
if (startIndex > 0 || elementCount < data.Length)
{
for (int i = startIndex; i < startIndex + elementCount; i++)
{
vData.Write<T>(data[i]);
}
}
else
{
vData.CopyTo(stream);
}
NativeDxDevice.Current.UnmapSubresource(buffer, 0);
}
}
pinnedArray.Free();
} }
public void SetData<T>(GraphicsDevice graphicsDevice, T[] data) public void SetData<T>(GraphicsDevice graphicsDevice, T[] data) where T : struct
where T : struct
{ {
SetData<T>(graphicsDevice, data, 0, data.Length); SetData<T>(graphicsDevice, data, 0, data.Length);
} }
public void SetData<T>(GraphicsDevice graphicsDevice, T[] data, public void SetData<T>(GraphicsDevice graphicsDevice, T[] data, int startIndex,
int startIndex, int elementCount) where T : struct int elementCount) where T : struct
{ {
SetData<T>(graphicsDevice, 0, data, startIndex, elementCount); SetData<T>(graphicsDevice, 0, data, startIndex, elementCount);
} }
public void SetData<T>(GraphicsDevice graphicsDevice, int offsetInBytes, public void SetData<T>(GraphicsDevice graphicsDevice, int offsetInBytes, T[] data,
T[] data, int startIndex, int elementCount, int vertexStride) int startIndex, int elementCount, int vertexStride) where T : struct
where T : struct
{ {
throw new NotImplementedException(); GraphicsDeviceWindowsMetro gdMetro = graphicsDevice.NativeDevice as GraphicsDeviceWindowsMetro;
var device = gdMetro.NativeDevice;
//TODO: check offsetInBytes parameter for bounds etc.
SharpDX.DataStream stream = device.MapSubresource(NativeBuffer);
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 #endregion
@ -152,10 +129,10 @@ namespace ANX.RenderSystem.Windows.Metro
#region Dispose #region Dispose
public void Dispose() public void Dispose()
{ {
if (buffer != null) if (NativeBuffer != null)
{ {
buffer.Dispose(); NativeBuffer.Dispose();
buffer = null; NativeBuffer = null;
} }
} }
#endregion #endregion