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

View File

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

View File

@ -116,7 +116,7 @@ namespace ANX.RenderSystem.Windows.Metro
NativeDevice.Present(this.vSyncEnabled ? 1 : 0);
}
#endregion // Present
#endregion
#region DrawIndexedPrimitives
public void DrawIndexedPrimitives(PrimitiveType primitiveType, int baseVertex,
@ -125,7 +125,6 @@ namespace ANX.RenderSystem.Windows.Metro
SetInputLayout();
ApplyPrimitiveType(primitiveType);
NativeDevice.SetDefaultTargets();
//d3dContext.PixelShader.SetSampler(0, sampler);
int indexCount = CalculateVertexCount(primitiveType, primitiveCount);
for (int passIndex = 0; passIndex < currentTechnique.PassCount; passIndex++)
@ -142,7 +141,6 @@ namespace ANX.RenderSystem.Windows.Metro
SetInputLayout();
ApplyPrimitiveType(primitiveType);
NativeDevice.SetDefaultTargets();
//d3dContext.PixelShader.SetSampler(0, sampler);
for (int passIndex = 0; passIndex < currentTechnique.PassCount; passIndex++)
{
@ -317,27 +315,10 @@ namespace ANX.RenderSystem.Windows.Metro
private Dx11.InputElement CreateInputElementFromVertexElement(VertexElement vertexElement)
{
string elementName = FormatConverter.Translate(vertexElement.VertexElementUsage);
Format elementFormat = FormatConverter.Translate(vertexElement.VertexElementFormat);
Format elementFormat;
switch (vertexElement.VertexElementFormat)
{
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);
return new Dx11.InputElement(elementName, vertexElement.UsageIndex, elementFormat,
vertexElement.Offset, 0);
}
#endregion

View File

@ -1,6 +1,4 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using ANX.Framework.Graphics;
using ANX.Framework.NonXNA.RenderSystem;
using Dx11 = SharpDX.Direct3D11;
@ -14,18 +12,14 @@ namespace ANX.RenderSystem.Windows.Metro
public class IndexBuffer_Metro : INativeIndexBuffer, IDisposable
{
#region Private
private Dx11.Buffer buffer;
private IndexElementSize size;
private int indexSizeInBytes;
#endregion
#region Public
public Dx11.Buffer NativeBuffer
{
get
{
return this.buffer;
}
get;
private set;
}
#endregion
@ -33,13 +27,9 @@ namespace ANX.RenderSystem.Windows.Metro
public IndexBuffer_Metro(GraphicsDevice graphics, IndexElementSize size,
int indexCount, BufferUsage usage)
{
this.size = size;
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;
InitializeBuffer(device, indexCount, usage);
@ -48,8 +38,7 @@ namespace ANX.RenderSystem.Windows.Metro
internal IndexBuffer_Metro(Dx11.Device device, IndexElementSize size,
int indexCount, BufferUsage usage)
{
this.size = size;
indexSizeInBytes = size == IndexElementSize.SixteenBits ? 2 : 4;
InitializeBuffer(device, indexCount, usage);
}
#endregion
@ -60,14 +49,14 @@ namespace ANX.RenderSystem.Windows.Metro
{
var description = new Dx11.BufferDescription()
{
Usage = Dx11.ResourceUsage.Dynamic,
Usage = FormatConverter.Translate(usage),
SizeInBytes = indexSizeInBytes * indexCount,
BindFlags = Dx11.BindFlags.IndexBuffer,
CpuAccessFlags = Dx11.CpuAccessFlags.Write,
OptionFlags = Dx11.ResourceOptionFlags.None
};
this.buffer = new Dx11.Buffer(device, description);
NativeBuffer = new Dx11.Buffer(device, description);
}
#endregion
@ -77,50 +66,37 @@ namespace ANX.RenderSystem.Windows.Metro
SetData<T>(graphicsDevice, data, 0, data.Length);
}
public void SetData<T>(GraphicsDevice graphicsDevice, int offsetInBytes,
T[] data, int startIndex, 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
public void SetData<T>(GraphicsDevice graphicsDevice, T[] data, int startIndex,
int elementCount) where T : struct
{
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
#region GetData (TODO)
@ -145,10 +121,10 @@ namespace ANX.RenderSystem.Windows.Metro
#region Dispose
public void Dispose()
{
if (buffer != null)
if (NativeBuffer != null)
{
buffer.Dispose();
buffer = null;
NativeBuffer.Dispose();
NativeBuffer = null;
}
}
#endregion

View File

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

View File

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

View File

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

View File

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

View File

@ -1,6 +1,4 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using ANX.Framework.Graphics;
using ANX.Framework.NonXNA.RenderSystem;
using Dx11 = SharpDX.Direct3D11;
@ -14,34 +12,30 @@ namespace ANX.RenderSystem.Windows.Metro
public class VertexBuffer_Metro : INativeVertexBuffer, IDisposable
{
#region Private
private Dx11.Buffer buffer;
private int vertexStride;
#endregion
#region Public
public Dx11.Buffer NativeBuffer
{
get
{
return this.buffer;
}
get;
private set;
}
#endregion
#region Constructor
public VertexBuffer_Metro(GraphicsDevice graphics,
VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage)
public VertexBuffer_Metro(GraphicsDevice graphics, VertexDeclaration vertexDeclaration,
int vertexCount, BufferUsage usage)
{
GraphicsDeviceWindowsMetro gdMetro =
graphics.NativeDevice as GraphicsDeviceWindowsMetro;
GraphicsDeviceWindowsMetro gdMetro = graphics.NativeDevice as GraphicsDeviceWindowsMetro;
var device = gdMetro.NativeDevice.NativeDevice;
vertexStride = vertexDeclaration.VertexStride;
InitializeBuffer(device, vertexCount, usage);
}
internal VertexBuffer_Metro(Dx11.Device device,
VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage)
internal VertexBuffer_Metro(Dx11.Device device, VertexDeclaration vertexDeclaration,
int vertexCount, BufferUsage usage)
{
vertexStride = vertexDeclaration.VertexStride;
InitializeBuffer(device, vertexCount, usage);
@ -52,20 +46,18 @@ namespace ANX.RenderSystem.Windows.Metro
private void InitializeBuffer(Dx11.Device device, int vertexCount,
BufferUsage usage)
{
//TODO: translate and use usage
if (device != null)
{
var description = new Dx11.BufferDescription()
{
Usage = Dx11.ResourceUsage.Dynamic,
Usage = FormatConverter.Translate(usage),
SizeInBytes = vertexStride * vertexCount,
BindFlags = Dx11.BindFlags.VertexBuffer,
CpuAccessFlags = Dx11.CpuAccessFlags.Write,
OptionFlags = Dx11.ResourceOptionFlags.None
};
buffer = new Dx11.Buffer(device, description);
NativeBuffer = new Dx11.Buffer(device, description);
}
}
#endregion
@ -74,59 +66,44 @@ namespace ANX.RenderSystem.Windows.Metro
public void SetData<T>(GraphicsDevice graphicsDevice, int offsetInBytes,
T[] data, int startIndex, 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 / 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();
SetData<T>(graphicsDevice, offsetInBytes, data, startIndex, elementCount, vertexStride);
}
public void SetData<T>(GraphicsDevice graphicsDevice, T[] data)
where T : struct
public void SetData<T>(GraphicsDevice graphicsDevice, T[] data) where T : struct
{
SetData<T>(graphicsDevice, data, 0, data.Length);
}
public void SetData<T>(GraphicsDevice graphicsDevice, T[] data,
int startIndex, int elementCount) where T : struct
public void SetData<T>(GraphicsDevice graphicsDevice, T[] data, int startIndex,
int elementCount) where T : struct
{
SetData<T>(graphicsDevice, 0, data, startIndex, elementCount);
}
public void SetData<T>(GraphicsDevice graphicsDevice, int offsetInBytes,
T[] data, int startIndex, int elementCount, int vertexStride)
where T : struct
public void SetData<T>(GraphicsDevice graphicsDevice, int offsetInBytes, T[] data,
int startIndex, int elementCount, int vertexStride) 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
@ -152,10 +129,10 @@ namespace ANX.RenderSystem.Windows.Metro
#region Dispose
public void Dispose()
{
if (buffer != null)
if (NativeBuffer != null)
{
buffer.Dispose();
buffer = null;
NativeBuffer.Dispose();
NativeBuffer = null;
}
}
#endregion