From 77063af953a01cc99f77cd529a31a2315c88bf16 Mon Sep 17 00:00:00 2001 From: Glatzemann Date: Tue, 8 Nov 2011 12:18:31 +0000 Subject: [PATCH] implemented HalfSingle struct, but unpacking is still missing --- .../ANX.Framework.TestCenter.csproj | 1 + ANX.Framework.TestCenter/AssertHelper.cs | 15 ++ .../Strukturen/PackedVector/HalfSingleTest.cs | 92 ++++++++++++ ANX.Framework/ANX.Framework.csproj | 1 + ANX.Framework/Graphics/PackedVector/Byte4.cs | 2 +- .../Graphics/PackedVector/HalfSingle.cs | 56 +++++++- .../Graphics/PackedVector/HalfTypeHelper.cs | 134 ++++++++++++++++++ 7 files changed, 293 insertions(+), 8 deletions(-) create mode 100644 ANX.Framework.TestCenter/Strukturen/PackedVector/HalfSingleTest.cs create mode 100644 ANX.Framework/Graphics/PackedVector/HalfTypeHelper.cs diff --git a/ANX.Framework.TestCenter/ANX.Framework.TestCenter.csproj b/ANX.Framework.TestCenter/ANX.Framework.TestCenter.csproj index 11f56ac4..136257d4 100644 --- a/ANX.Framework.TestCenter/ANX.Framework.TestCenter.csproj +++ b/ANX.Framework.TestCenter/ANX.Framework.TestCenter.csproj @@ -68,6 +68,7 @@ + diff --git a/ANX.Framework.TestCenter/AssertHelper.cs b/ANX.Framework.TestCenter/AssertHelper.cs index f7ecfeab..181457ef 100644 --- a/ANX.Framework.TestCenter/AssertHelper.cs +++ b/ANX.Framework.TestCenter/AssertHelper.cs @@ -93,6 +93,9 @@ using ANXBgra4444 = ANX.Framework.Graphics.PackedVector.Bgra4444; using XNAByte4 = Microsoft.Xna.Framework.Graphics.PackedVector.Byte4; using ANXByte4 = ANX.Framework.Graphics.PackedVector.Byte4; +using XNAHalfSingle = Microsoft.Xna.Framework.Graphics.PackedVector.HalfSingle; +using ANXHalfSingle = ANX.Framework.Graphics.PackedVector.HalfSingle; + namespace ANX.Framework.TestCenter { class AssertHelper @@ -145,6 +148,18 @@ namespace ANX.Framework.TestCenter } } + public static void ConvertEquals(XNAHalfSingle lhs, ANXHalfSingle rhs, String test) + { + if (lhs.PackedValue == rhs.PackedValue) + { + Assert.Pass(test + " passed"); + } + else + { + Assert.Fail(String.Format("{0] failed: HalfSingle XNA: ({1}) HalfSingle ANX: ({2})", test, lhs, rhs)); + } + } + public static void ConvertEquals(byte a, byte b, String test) { if (a == b) diff --git a/ANX.Framework.TestCenter/Strukturen/PackedVector/HalfSingleTest.cs b/ANX.Framework.TestCenter/Strukturen/PackedVector/HalfSingleTest.cs new file mode 100644 index 00000000..b48efa71 --- /dev/null +++ b/ANX.Framework.TestCenter/Strukturen/PackedVector/HalfSingleTest.cs @@ -0,0 +1,92 @@ +#region Using Statements + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NUnit.Framework; + +using XNAHalfSingle = Microsoft.Xna.Framework.Graphics.PackedVector.HalfSingle; +using ANXHalfSingle = ANX.Framework.Graphics.PackedVector.HalfSingle; + +using XNAVector4 = Microsoft.Xna.Framework.Vector4; +using ANXVector4 = ANX.Framework.Vector4; + +#endregion // Using Statements + +#region License + +// +// This file is part of the ANX.Framework created by the "ANX.Framework developer group". +// +// This file is released under the Ms-PL license. +// +// +// +// Microsoft Public License (Ms-PL) +// +// This license governs use of the accompanying software. If you use the software, you accept this license. +// If you do not accept the license, do not use the software. +// +// 1.Definitions +// The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning +// here as under U.S. copyright law. +// A "contribution" is the original software, or any additions or changes to the software. +// A "contributor" is any person that distributes its contribution under this license. +// "Licensed patents" are a contributor's patent claims that read directly on its contribution. +// +// 2.Grant of Rights +// (A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations +// in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to +// reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution +// or any derivative works that you create. +// (B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in +// section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed +// patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution +// in the software or derivative works of the contribution in the software. +// +// 3.Conditions and Limitations +// (A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks. +// (B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your +// patent license from such contributor to the software ends automatically. +// (C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution +// notices that are present in the software. +// (D) If you distribute any portion of the software in source code form, you may do so only under this license by including +// a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or +// object code form, you may only do so under a license that complies with this license. +// (E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees, +// or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the +// extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a +// particular purpose and non-infringement. + +#endregion // License + +namespace ANX.Framework.TestCenter.Strukturen.PackedVector +{ + [TestFixture] + class HalfSingleTest + { + #region Testdata + + static object[] floats = + { + new object[] { DataFactory.RandomValue }, + new object[] { DataFactory.RandomValue }, + new object[] { DataFactory.RandomValue }, + new object[] { DataFactory.RandomValue }, + new object[] { DataFactory.RandomValue } + }; + + #endregion + + [Test, TestCaseSource("floats")] + public void contructor1(float single) + { + XNAHalfSingle xnaVal = new XNAHalfSingle(single); + ANXHalfSingle anxVal = new ANXHalfSingle(single); + + AssertHelper.ConvertEquals(xnaVal, anxVal, "Constructor1"); + } + + } +} diff --git a/ANX.Framework/ANX.Framework.csproj b/ANX.Framework/ANX.Framework.csproj index 181fafcd..75be7361 100644 --- a/ANX.Framework/ANX.Framework.csproj +++ b/ANX.Framework/ANX.Framework.csproj @@ -200,6 +200,7 @@ + diff --git a/ANX.Framework/Graphics/PackedVector/Byte4.cs b/ANX.Framework/Graphics/PackedVector/Byte4.cs index d5022c93..97ed20b3 100644 --- a/ANX.Framework/Graphics/PackedVector/Byte4.cs +++ b/ANX.Framework/Graphics/PackedVector/Byte4.cs @@ -100,7 +100,7 @@ namespace ANX.Framework.Graphics.PackedVector public Vector4 ToVector4() { - return new Vector4((packedValue >> 0) & 255, + return new Vector4((packedValue >> 0) & 255, (packedValue >> 8) & 255, (packedValue >> 16) & 255, (packedValue >> 24) & 255); diff --git a/ANX.Framework/Graphics/PackedVector/HalfSingle.cs b/ANX.Framework/Graphics/PackedVector/HalfSingle.cs index 4946ed00..25f1727a 100644 --- a/ANX.Framework/Graphics/PackedVector/HalfSingle.cs +++ b/ANX.Framework/Graphics/PackedVector/HalfSingle.cs @@ -54,31 +54,73 @@ namespace ANX.Framework.Graphics.PackedVector { public struct HalfSingle : IPackedVector, IEquatable, IPackedVector { + UInt16 packedValue; + + public HalfSingle(float single) + { + packedValue = HalfTypeHelper.convert(single); + } + public ushort PackedValue { get { - throw new NotImplementedException(); + return this.packedValue; } set { - throw new NotImplementedException(); + this.packedValue = value; } } - public void PackFromVector4(Vector4 vector) + public float ToSingle() { - throw new NotImplementedException(); + return HalfTypeHelper.convert(this.packedValue); } - public Vector4 ToVector4() + void IPackedVector.PackFromVector4(Vector4 vector) { - throw new NotImplementedException(); + this.packedValue = HalfTypeHelper.convert(vector.X); + } + + Vector4 IPackedVector.ToVector4() + { + return new Vector4(this.ToSingle(), 0f, 0f, 1f); + } + + public override bool Equals(object obj) + { + if (obj != null && obj.GetType() == this.GetType()) + { + return this == (HalfSingle)obj; + } + + return false; } public bool Equals(HalfSingle other) { - throw new NotImplementedException(); + return this.packedValue == other.packedValue; + } + + public override string ToString() + { + return this.ToSingle().ToString(); + } + + public override int GetHashCode() + { + return this.packedValue.GetHashCode(); + } + + public static bool operator ==(HalfSingle lhs, HalfSingle rhs) + { + return lhs.packedValue == rhs.packedValue; + } + + public static bool operator !=(HalfSingle lhs, HalfSingle rhs) + { + return lhs.packedValue != rhs.packedValue; } } } diff --git a/ANX.Framework/Graphics/PackedVector/HalfTypeHelper.cs b/ANX.Framework/Graphics/PackedVector/HalfTypeHelper.cs new file mode 100644 index 00000000..c2809ed1 --- /dev/null +++ b/ANX.Framework/Graphics/PackedVector/HalfTypeHelper.cs @@ -0,0 +1,134 @@ +#region Using Statements +using System; +using System.Runtime.InteropServices; + +#endregion // Using Statements + +#region License + +// +// This file is part of the ANX.Framework created by the "ANX.Framework developer group". +// +// This file is released under the Ms-PL license. +// +// +// +// Microsoft Public License (Ms-PL) +// +// This license governs use of the accompanying software. If you use the software, you accept this license. +// If you do not accept the license, do not use the software. +// +// 1.Definitions +// The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning +// here as under U.S. copyright law. +// A "contribution" is the original software, or any additions or changes to the software. +// A "contributor" is any person that distributes its contribution under this license. +// "Licensed patents" are a contributor's patent claims that read directly on its contribution. +// +// 2.Grant of Rights +// (A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations +// in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to +// reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution +// or any derivative works that you create. +// (B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in +// section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed +// patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution +// in the software or derivative works of the contribution in the software. +// +// 3.Conditions and Limitations +// (A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks. +// (B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your +// patent license from such contributor to the software ends automatically. +// (C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution +// notices that are present in the software. +// (D) If you distribute any portion of the software in source code form, you may do so only under this license by including +// a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or +// object code form, you may only do so under a license that complies with this license. +// (E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees, +// or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the +// extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a +// particular purpose and non-infringement. + +#endregion // License + +namespace ANX.Framework.Graphics.PackedVector +{ + internal class HalfTypeHelper + { + [StructLayout(LayoutKind.Explicit)] + private struct uif + { + [FieldOffset(0)] + public float f; + [FieldOffset(0)] + public int i; + } + + internal static UInt16 convert(float f) + { + uif uif = new uif(); + uif.f = f; + return convert(uif.i); + } + + internal static UInt16 convert(int i) + { + int s = (i >> 16) & 0x00008000; + int e = ((i >> 23) & 0x000000ff) - (127 - 15); + int m = i & 0x007fffff; + + if (e <= 0) + { + if (e < -10) + { + return (UInt16)s; + } + + m = m | 0x00800000; + + int t = 14 - e; + int a = (1 << (t - 1)) - 1; + int b = (m >> t) & 1; + + m = (m + a + b) >> t; + + return (UInt16)(s | m); + } + else if (e == 0xff - (127 - 15)) + { + if (m == 0) + { + return (UInt16)(s | 0x7c00); + } + else + { + m >>= 13; + return (UInt16)(s | 0x7c00 | m | ((m == 0) ? 1 : 0)); + } + } + else + { + m = m + 0x00000fff + ((m >> 13) & 1); + + if ((m & 0x00800000) != 0) + { + m = 0; + e += 1; + } + + if (e > 30) + { + return (UInt16)(s | 0x7c00); + } + + return (UInt16)(s | (e << 10) | (m >> 13)); + } + } + + internal static float convert(UInt16 value) + { + //TODO: implement + throw new NotImplementedException(); + } + } +}