Color and PackedVector

pull/14/head
cosmonaut 2021-01-20 15:16:37 -08:00
parent e251d30aa9
commit a025e9ad76
21 changed files with 4915 additions and 9 deletions

1886
src/Graphics/Color.cs Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,171 @@
#region License
/* MoonWorks - Game Development Framework
* Copyright 2021 Evan Hemsley
*/
/* Derived from code by Ethan Lee (Copyright 2009-2021).
* Released under the Microsoft Public License.
* See fna.LICENSE for details.
* Derived from code by the Mono.Xna Team (Copyright 2006).
* Released under the MIT License. See monoxna.LICENSE for details.
*/
#endregion
#region Using Statements
using System;
using MoonWorks.Math;
#endregion
namespace MoonWorks.Graphics
{
/// <summary>
/// Packed vector type containing unsigned normalized values ranging from 0 to 1.
/// The x and z components use 5 bits, and the y component uses 6 bits.
/// </summary>
public struct Alpha8 : IPackedVector<byte>, IEquatable<Alpha8>, IPackedVector
{
#region Public Properties
/// <summary>
/// Gets and sets the packed value.
/// </summary>
public byte PackedValue
{
get
{
return packedValue;
}
set
{
packedValue = value;
}
}
#endregion
#region Private Variables
private byte packedValue;
#endregion
#region Public Constructor
/// <summary>
/// Creates a new instance of Alpha8.
/// </summary>
/// <param name="alpha">The alpha component</param>
public Alpha8(float alpha)
{
packedValue = Pack(alpha);
}
#endregion
#region Public Methods
/// <summary>
/// Gets the packed vector in float format.
/// </summary>
/// <returns>The packed vector in Vector3 format</returns>
public float ToAlpha()
{
return (float) (packedValue / 255.0f);
}
#endregion
#region IPackedVector Methods
/// <summary>
/// Sets the packed vector from a Vector4.
/// </summary>
/// <param name="vector">Vector containing the components.</param>
void IPackedVector.PackFromVector4(Vector4 vector)
{
packedValue = Pack(vector.W);
}
/// <summary>
/// Gets the packed vector in Vector4 format.
/// </summary>
/// <returns>The packed vector in Vector4 format</returns>
Vector4 IPackedVector.ToVector4()
{
return new Vector4(
0.0f,
0.0f,
0.0f,
(float) (packedValue / 255.0f)
);
}
#endregion
#region Public Static Operators and Override Methods
/// <summary>
/// Compares an object with the packed vector.
/// </summary>
/// <param name="obj">The object to compare.</param>
/// <returns>True if the object is equal to the packed vector.</returns>
public override bool Equals(object obj)
{
return (obj is Alpha8) && Equals((Alpha8) obj);
}
/// <summary>
/// Compares another Bgra5551 packed vector with the packed vector.
/// </summary>
/// <param name="other">The Bgra5551 packed vector to compare.</param>
/// <returns>True if the packed vectors are equal.</returns>
public bool Equals(Alpha8 other)
{
return packedValue == other.packedValue;
}
/// <summary>
/// Gets a string representation of the packed vector.
/// </summary>
/// <returns>A string representation of the packed vector.</returns>
public override string ToString()
{
return packedValue.ToString("X");
}
/// <summary>
/// Gets a hash code of the packed vector.
/// </summary>
/// <returns>The hash code for the packed vector.</returns>
public override int GetHashCode()
{
return packedValue.GetHashCode();
}
public static bool operator ==(Alpha8 lhs, Alpha8 rhs)
{
return lhs.packedValue == rhs.packedValue;
}
public static bool operator !=(Alpha8 lhs, Alpha8 rhs)
{
return lhs.packedValue != rhs.packedValue;
}
#endregion
#region Private Static Pack Method
private static byte Pack(float alpha)
{
return (byte) System.Math.Round(
MathHelper.Clamp(alpha, 0, 1) * 255.0f
);
}
#endregion
}
}

View File

@ -0,0 +1,185 @@
#region License
/* MoonWorks - Game Development Framework
* Copyright 2021 Evan Hemsley
*/
/* Derived from code by Ethan Lee (Copyright 2009-2021).
* Released under the Microsoft Public License.
* See fna.LICENSE for details.
* Derived from code by the Mono.Xna Team (Copyright 2006).
* Released under the MIT License. See monoxna.LICENSE for details.
*/
#endregion
#region Using Statements
using System;
using MoonWorks.Math;
#endregion
namespace MoonWorks.Graphics
{
/// <summary>
/// Packed vector type containing unsigned normalized values ranging from 0 to 1.
/// The x and z components use 5 bits, and the y component uses 6 bits.
/// </summary>
public struct Bgr565 : IPackedVector<ushort>, IEquatable<Bgr565>, IPackedVector
{
#region Public Properties
/// <summary>
/// Gets and sets the packed value.
/// </summary>
public ushort PackedValue
{
get
{
return packedValue;
}
set
{
packedValue = value;
}
}
#endregion
#region Private Variables
private ushort packedValue;
#endregion
#region Public Constructors
/// <summary>
/// Creates a new instance of Bgr565.
/// </summary>
/// <param name="x">The x component</param>
/// <param name="y">The y component</param>
/// <param name="z">The z component</param>
public Bgr565(float x, float y, float z)
{
packedValue = Pack(x, y, z);
}
/// <summary>
/// Creates a new instance of Bgr565.
/// </summary>
/// <param name="vector">
/// Vector containing the components for the packed vector.
/// </param>
public Bgr565(Vector3 vector)
{
packedValue = Pack(vector.X, vector.Y, vector.Z);
}
#endregion
#region Public Methods
/// <summary>
/// Gets the packed vector in Vector3 format.
/// </summary>
/// <returns>The packed vector in Vector3 format</returns>
public Vector3 ToVector3()
{
return new Vector3(
(packedValue >> 11) / 31.0f,
((packedValue >> 5) & 0x3F) / 63.0f,
(packedValue & 0x1F) / 31.0f
);
}
#endregion
#region IPackedVector Methods
/// <summary>
/// Sets the packed vector from a Vector4.
/// </summary>
/// <param name="vector">Vector containing the components.</param>
void IPackedVector.PackFromVector4(Vector4 vector)
{
Pack(vector.X, vector.Y, vector.Z);
}
/// <summary>
/// Gets the packed vector in Vector4 format.
/// </summary>
/// <returns>The packed vector in Vector4 format</returns>
Vector4 IPackedVector.ToVector4()
{
return new Vector4(ToVector3(), 1.0f);
}
#endregion
#region Public Static Operators and Override Methods
/// <summary>
/// Compares an object with the packed vector.
/// </summary>
/// <param name="obj">The object to compare.</param>
/// <returns>True if the object is equal to the packed vector.</returns>
public override bool Equals(object obj)
{
return (obj is Bgr565) && Equals((Bgr565) obj);
}
/// <summary>
/// Compares another Bgr565 packed vector with the packed vector.
/// </summary>
/// <param name="other">The Bgr565 packed vector to compare.</param>
/// <returns>True if the packed vectors are equal.</returns>
public bool Equals(Bgr565 other)
{
return packedValue == other.packedValue;
}
/// <summary>
/// Gets a string representation of the packed vector.
/// </summary>
/// <returns>A string representation of the packed vector.</returns>
public override string ToString()
{
return packedValue.ToString("X");
}
/// <summary>
/// Gets a hash code of the packed vector.
/// </summary>
/// <returns>The hash code for the packed vector.</returns>
public override int GetHashCode()
{
return packedValue.GetHashCode();
}
public static bool operator ==(Bgr565 lhs, Bgr565 rhs)
{
return lhs.packedValue == rhs.packedValue;
}
public static bool operator !=(Bgr565 lhs, Bgr565 rhs)
{
return lhs.packedValue != rhs.packedValue;
}
#endregion
#region Private Static Pack Method
private static ushort Pack(float x, float y, float z)
{
return (ushort) (
(((ushort) System.Math.Round(MathHelper.Clamp(x, 0, 1) * 31.0f)) << 11) |
(((ushort) System.Math.Round(MathHelper.Clamp(y, 0, 1) * 63.0f)) << 5) |
((ushort) System.Math.Round(MathHelper.Clamp(z, 0, 1) * 31.0f))
);
}
#endregion
}
}

View File

@ -0,0 +1,179 @@
#region License
/* MoonWorks - Game Development Framework
* Copyright 2021 Evan Hemsley
*/
/* Derived from code by Ethan Lee (Copyright 2009-2021).
* Released under the Microsoft Public License.
* See fna.LICENSE for details.
* Derived from code by the Mono.Xna Team (Copyright 2006).
* Released under the MIT License. See monoxna.LICENSE for details.
*/
#endregion
#region Using Statements
using System;
using MoonWorks.Math;
#endregion
namespace MoonWorks.Graphics
{
/// <summary>
/// Packed vector type containing unsigned normalized values, ranging from 0 to 1, using
/// 4 bits each for x, y, z, and w.
/// </summary>
public struct Bgra4444 : IPackedVector<ushort>, IEquatable<Bgra4444>
{
#region Public Properties
/// <summary>
/// Gets and sets the packed value.
/// </summary>
public ushort PackedValue
{
get
{
return packedValue;
}
set
{
packedValue = value;
}
}
#endregion
#region Private Variables
private ushort packedValue;
#endregion
#region Public Constructors
/// <summary>
/// Creates a new instance of Bgra4444.
/// </summary>
/// <param name="x">The x component</param>
/// <param name="y">The y component</param>
/// <param name="z">The z component</param>
/// <param name="w">The w component</param>
public Bgra4444(float x, float y, float z, float w)
{
packedValue = Pack(x, y, z, w);
}
/// <summary>
/// Creates a new instance of Bgra4444.
/// </summary>
/// <param name="vector">
/// Vector containing the components for the packed vector.
/// </param>
public Bgra4444(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
}
#endregion
#region Public Methods
/// <summary>
/// Gets the packed vector in Vector4 format.
/// </summary>
/// <returns>The packed vector in Vector4 format</returns>
public Vector4 ToVector4()
{
return new Vector4(
((packedValue >> 8) & 0x0F) / 15.0f,
((packedValue >> 4) & 0x0F) / 15.0f,
(packedValue & 0x0F) / 15.0f,
(packedValue >> 12) / 15.0f
);
}
#endregion
#region IPackedVector Methods
/// <summary>
/// Sets the packed vector from a Vector4.
/// </summary>
/// <param name="vector">Vector containing the components.</param>
void IPackedVector.PackFromVector4(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
}
#endregion
#region Public Static Operators and Override Methods
/// <summary>
/// Compares an object with the packed vector.
/// </summary>
/// <param name="obj">The object to compare.</param>
/// <returns>True if the object is equal to the packed vector.</returns>
public override bool Equals(object obj)
{
return (obj is Bgra4444) && Equals((Bgra4444) obj);
}
/// <summary>
/// Compares another Bgra4444 packed vector with the packed vector.
/// </summary>
/// <param name="other">The Bgra4444 packed vector to compare.</param>
/// <returns>True if the packed vectors are equal.</returns>
public bool Equals(Bgra4444 other)
{
return packedValue == other.packedValue;
}
/// <summary>
/// Gets a string representation of the packed vector.
/// </summary>
/// <returns>A string representation of the packed vector.</returns>
public override string ToString()
{
return packedValue.ToString("X");
}
/// <summary>
/// Gets a hash code of the packed vector.
/// </summary>
/// <returns>The hash code for the packed vector.</returns>
public override int GetHashCode()
{
return packedValue.GetHashCode();
}
public static bool operator ==(Bgra4444 lhs, Bgra4444 rhs)
{
return lhs.packedValue == rhs.packedValue;
}
public static bool operator !=(Bgra4444 lhs, Bgra4444 rhs)
{
return lhs.packedValue != rhs.packedValue;
}
#endregion
#region Private Static Pack Method
private static ushort Pack(float x, float y, float z, float w)
{
return (ushort) (
(((ushort) System.Math.Round(MathHelper.Clamp(x, 0, 1) * 15.0f)) << 8) |
(((ushort) System.Math.Round(MathHelper.Clamp(y, 0, 1) * 15.0f)) << 4) |
((ushort) System.Math.Round(MathHelper.Clamp(z, 0, 1) * 15.0f)) |
(((ushort) System.Math.Round(MathHelper.Clamp(w, 0, 1) * 15.0f)) << 12)
);
}
#endregion
}
}

View File

@ -0,0 +1,179 @@
#region License
/* MoonWorks - Game Development Framework
* Copyright 2021 Evan Hemsley
*/
/* Derived from code by Ethan Lee (Copyright 2009-2021).
* Released under the Microsoft Public License.
* See fna.LICENSE for details.
* Derived from code by the Mono.Xna Team (Copyright 2006).
* Released under the MIT License. See monoxna.LICENSE for details.
*/
#endregion
#region Using Statements
using System;
using MoonWorks.Math;
#endregion
namespace MoonWorks.Graphics
{
/// <summary>
/// Packed vector type containing unsigned normalized values ranging from 0 to 1.
/// The x and z components use 5 bits, and the y component uses 6 bits.
/// </summary>
public struct Bgra5551 : IPackedVector<ushort>, IEquatable<Bgra5551>, IPackedVector
{
#region Public Properties
/// <summary>
/// Gets and sets the packed value.
/// </summary>
public ushort PackedValue
{
get
{
return packedValue;
}
set
{
packedValue = value;
}
}
#endregion
#region Private Variables
private ushort packedValue;
#endregion
#region Public Constructors
/// <summary>
/// Creates a new instance of Bgra5551.
/// </summary>
/// <param name="x">The x component</param>
/// <param name="y">The y component</param>
/// <param name="z">The z component</param>
/// <param name="w">The w component</param>
public Bgra5551(float x, float y, float z, float w)
{
packedValue = Pack(x, y, z, w);
}
/// <summary>
/// Creates a new instance of Bgra5551.
/// </summary>
/// <param name="vector">
/// Vector containing the components for the packed vector.
/// </param>
public Bgra5551(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
}
#endregion
#region Public Methods
/// <summary>
/// Gets the packed vector in Vector4 format.
/// </summary>
/// <returns>The packed vector in Vector4 format</returns>
public Vector4 ToVector4()
{
return new Vector4(
((packedValue >> 10) & 0x1F) / 31.0f,
((packedValue >> 5) & 0x1F) / 31.0f,
(packedValue & 0x1F) / 31.0f,
(float) (packedValue >> 15)
);
}
#endregion
#region IPackedVector Methods
/// <summary>
/// Sets the packed vector from a Vector4.
/// </summary>
/// <param name="vector">Vector containing the components.</param>
void IPackedVector.PackFromVector4(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
}
#endregion
#region Public Static Operators and Override Methods
/// <summary>
/// Compares an object with the packed vector.
/// </summary>
/// <param name="obj">The object to compare.</param>
/// <returns>True if the object is equal to the packed vector.</returns>
public override bool Equals(object obj)
{
return (obj is Bgra5551) && Equals((Bgra5551) obj);
}
/// <summary>
/// Compares another Bgra5551 packed vector with the packed vector.
/// </summary>
/// <param name="other">The Bgra5551 packed vector to compare.</param>
/// <returns>True if the packed vectors are equal.</returns>
public bool Equals(Bgra5551 other)
{
return packedValue == other.packedValue;
}
/// <summary>
/// Gets a string representation of the packed vector.
/// </summary>
/// <returns>A string representation of the packed vector.</returns>
public override string ToString()
{
return packedValue.ToString("X");
}
/// <summary>
/// Gets a hash code of the packed vector.
/// </summary>
/// <returns>The hash code for the packed vector.</returns>
public override int GetHashCode()
{
return packedValue.GetHashCode();
}
public static bool operator ==(Bgra5551 lhs, Bgra5551 rhs)
{
return lhs.packedValue == rhs.packedValue;
}
public static bool operator !=(Bgra5551 lhs, Bgra5551 rhs)
{
return lhs.packedValue != rhs.packedValue;
}
#endregion
#region Private Static Pack Method
private static ushort Pack(float x, float y, float z, float w)
{
return (ushort) (
(((ushort) System.Math.Round(MathHelper.Clamp(x, 0, 1) * 31.0f)) << 10) |
(((ushort) System.Math.Round(MathHelper.Clamp(y, 0, 1) * 31.0f)) << 5) |
((ushort) System.Math.Round(MathHelper.Clamp(z, 0, 1) * 31.0f)) |
((ushort) System.Math.Round(MathHelper.Clamp(w, 0, 1)) << 15)
);
}
#endregion
}
}

View File

@ -0,0 +1,204 @@
#region License
/* MoonWorks - Game Development Framework
* Copyright 2021 Evan Hemsley
*/
/* Derived from code by Ethan Lee (Copyright 2009-2021).
* Released under the Microsoft Public License.
* See fna.LICENSE for details.
* Derived from code by the Mono.Xna Team (Copyright 2006).
* Released under the MIT License. See monoxna.LICENSE for details.
*/
#endregion
#region Using Statements
using System;
using MoonWorks.Math;
#endregion
namespace MoonWorks.Graphics
{
/// <summary>
/// Packed vector type containing four 8-bit unsigned integer values, ranging from 0 to 255.
/// </summary>
public struct Byte4 : IPackedVector<uint>, IEquatable<Byte4>, IPackedVector
{
#region Public Properties
/// <summary>
/// Directly gets or sets the packed representation of the value.
/// </summary>
/// <value>The packed representation of the value.</value>
public uint PackedValue
{
get
{
return packedValue;
}
set
{
packedValue = value;
}
}
#endregion
#region Private Variables
private uint packedValue;
#endregion
#region Public Constructors
/// <summary>
/// Initializes a new instance of the Byte4 class.
/// </summary>
/// <param name="vector">
/// A vector containing the initial values for the components of the Byte4 structure.
/// </param>
public Byte4(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
}
/// <summary>
/// Initializes a new instance of the Byte4 class.
/// </summary>
/// <param name="x">Initial value for the x component.</param>
/// <param name="y">Initial value for the y component.</param>
/// <param name="z">Initial value for the z component.</param>
/// <param name="w">Initial value for the w component.</param>
public Byte4(float x, float y, float z, float w)
{
packedValue = Pack(x, y, z, w);
}
#endregion
#region Public Methods
/// <summary>
/// Expands the packed representation into a Vector4.
/// </summary>
/// <returns>The expanded vector.</returns>
public Vector4 ToVector4()
{
return new Vector4(
(packedValue & 0xFF),
((packedValue >> 8) & 0xFF),
((packedValue >> 16) & 0xFF),
(packedValue >> 24)
);
}
#endregion
#region IPackedVector Methods
/// <summary>
/// Sets the packed representation from a Vector4.
/// </summary>
/// <param name="vector">The vector to create the packed representation from.</param>
void IPackedVector.PackFromVector4(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
}
#endregion
#region Public Static Operators and Override Methods
/// <summary>
/// Compares the current instance of a class to another instance to determine
/// whether they are different.
/// </summary>
/// <param name="a">The object to the left of the equality operator.</param>
/// <param name="b">The object to the right of the equality operator.</param>
/// <returns>True if the objects are different; false otherwise.</returns>
public static bool operator !=(Byte4 a, Byte4 b)
{
return a.packedValue != b.packedValue;
}
/// <summary>
/// Compares the current instance of a class to another instance to determine
/// whether they are the same.
/// </summary>
/// <param name="a">The object to the left of the equality operator.</param>
/// <param name="b">The object to the right of the equality operator.</param>
/// <returns>True if the objects are the same; false otherwise.</returns>
public static bool operator ==(Byte4 a, Byte4 b)
{
return a.packedValue == b.packedValue;
}
/// <summary>
/// Returns a value that indicates whether the current instance is equal to a
/// specified object.
/// </summary>
/// <param name="obj">The object with which to make the comparison.</param>
/// <returns>
/// True if the current instance is equal to the specified object; false otherwise.
/// </returns>
public override bool Equals(object obj)
{
return (obj is Byte4) && Equals((Byte4) obj);
}
/// <summary>
/// Returns a value that indicates whether the current instance is equal to a
/// specified object.
/// </summary>
/// <param name="other">The object with which to make the comparison.</param>
/// <returns>
/// True if the current instance is equal to the specified object; false otherwise.
/// </returns>
public bool Equals(Byte4 other)
{
return this == other;
}
/// <summary>
/// Gets the hash code for the current instance.
/// </summary>
/// <returns>Hash code for the instance.</returns>
public override int GetHashCode()
{
return packedValue.GetHashCode();
}
/// <summary>
/// Returns a string representation of the current instance.
/// </summary>
/// <returns>String that represents the object.</returns>
public override string ToString()
{
return packedValue.ToString("X");
}
#endregion
#region Private Static Pack Method
/// <summary>
/// Packs a vector into a uint.
/// </summary>
/// <param name="vector">The vector containing the values to pack.</param>
/// <returns>The ulong containing the packed values.</returns>
static uint Pack(float x, float y, float z, float w)
{
return (uint) (
((uint) System.Math.Round(MathHelper.Clamp(x, 0, 255))) |
((uint) System.Math.Round(MathHelper.Clamp(y, 0, 255)) << 8) |
((uint) System.Math.Round(MathHelper.Clamp(z, 0, 255)) << 16) |
((uint) System.Math.Round(MathHelper.Clamp(w, 0, 255)) << 24)
);
}
#endregion
}
}

View File

@ -0,0 +1,114 @@
#region License
/* MoonWorks - Game Development Framework
* Copyright 2021 Evan Hemsley
*/
/* Derived from code by Ethan Lee (Copyright 2009-2021).
* Released under the Microsoft Public License.
* See fna.LICENSE for details.
* Derived from code by the Mono.Xna Team (Copyright 2006).
* Released under the MIT License. See monoxna.LICENSE for details.
*/
#endregion
#region Using Statements
using System;
using MoonWorks.Math;
#endregion
namespace MoonWorks.Graphics
{
public struct HalfSingle : IPackedVector<ushort>, IEquatable<HalfSingle>, IPackedVector
{
#region Public Properties
public ushort PackedValue
{
get
{
return packedValue;
}
set
{
packedValue = value;
}
}
#endregion
#region Private Variables
private ushort packedValue;
#endregion
#region Public Constructors
public HalfSingle(float single)
{
packedValue = HalfTypeHelper.Convert(single);
}
#endregion
#region Public Methods
public float ToSingle()
{
return HalfTypeHelper.Convert(packedValue);
}
#endregion
#region IPackedVector Methods
void IPackedVector.PackFromVector4(Vector4 vector)
{
packedValue = HalfTypeHelper.Convert(vector.X);
}
Vector4 IPackedVector.ToVector4()
{
return new Vector4(ToSingle(), 0.0f, 0.0f, 1.0f);
}
#endregion
#region Public Static Operators and Override Methods
public override bool Equals(object obj)
{
return (obj is HalfSingle) && Equals((HalfSingle) obj);
}
public bool Equals(HalfSingle other)
{
return packedValue == other.packedValue;
}
public override string ToString()
{
return packedValue.ToString("X");
}
public override int GetHashCode()
{
return 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;
}
#endregion
}
}

View File

@ -0,0 +1,139 @@
#region License
/* MoonWorks - Game Development Framework
* Copyright 2021 Evan Hemsley
*/
/* Derived from code by Ethan Lee (Copyright 2009-2021).
* Released under the Microsoft Public License.
* See fna.LICENSE for details.
* Derived from code by the Mono.Xna Team (Copyright 2006).
* Released under the MIT License. See monoxna.LICENSE for details.
*/
#endregion
#region Using Statements
using System;
using System.Runtime.InteropServices;
#endregion
namespace MoonWorks.Graphics
{
internal static class HalfTypeHelper
{
#region Private Struct uif
[StructLayout(LayoutKind.Explicit)]
private struct uif
{
[FieldOffset(0)]
public float f;
[FieldOffset(0)]
public int i;
[FieldOffset(0)]
public uint u;
}
#endregion
#region Internal Static Methods
internal static ushort Convert(float f)
{
uif uif = new uif();
uif.f = f;
return Convert(uif.i);
}
internal static ushort 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 (ushort) 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 (ushort) (s | m);
}
else if (e == 0xff - (127 - 15))
{
if (m == 0)
{
return (ushort) (s | 0x7c00);
}
else
{
m >>= 13;
return (ushort) (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 (ushort) (s | 0x7c00);
}
return (ushort) (s | (e << 10) | (m >> 13));
}
}
internal static float Convert(ushort value)
{
uint rst;
uint mantissa = (uint)(value & 1023);
uint exp = 0xfffffff2;
if ((value & -33792) == 0)
{
if (mantissa != 0)
{
while ((mantissa & 1024) == 0)
{
exp--;
mantissa = mantissa << 1;
}
mantissa &= 0xfffffbff;
rst = ((uint) ((((uint) value & 0x8000) << 16) | ((exp + 127) << 23))) | (mantissa << 13);
}
else
{
rst = (uint) ((value & 0x8000) << 16);
}
}
else
{
rst = (uint) (((((uint) value & 0x8000) << 16) | ((((((uint) value >> 10) & 0x1f) - 15) + 127) << 23)) | (mantissa << 13));
}
uif uif = new uif();
uif.u = rst;
return uif.f;
}
#endregion
}
}

View File

@ -0,0 +1,134 @@
#region License
/* MoonWorks - Game Development Framework
* Copyright 2021 Evan Hemsley
*/
/* Derived from code by Ethan Lee (Copyright 2009-2021).
* Released under the Microsoft Public License.
* See fna.LICENSE for details.
* Derived from code by the Mono.Xna Team (Copyright 2006).
* Released under the MIT License. See monoxna.LICENSE for details.
*/
#endregion
#region Using Statements
using System;
using MoonWorks.Math;
#endregion
namespace MoonWorks.Graphics
{
public struct HalfVector2 : IPackedVector<uint>, IPackedVector, IEquatable<HalfVector2>
{
#region Public Properties
public uint PackedValue
{
get
{
return packedValue;
}
set
{
packedValue = value;
}
}
#endregion
#region Private Variables
private uint packedValue;
#endregion
#region Public Constructors
public HalfVector2(float x, float y)
{
packedValue = PackHelper(x, y);
}
public HalfVector2(Vector2 vector)
{
packedValue = PackHelper(vector.X, vector.Y);
}
#endregion
#region Public Methods
public Vector2 ToVector2()
{
Vector2 vector;
vector.X = HalfTypeHelper.Convert((ushort) packedValue);
vector.Y = HalfTypeHelper.Convert((ushort) (packedValue >> 0x10));
return vector;
}
#endregion
#region IPackedVector Methods
void IPackedVector.PackFromVector4(Vector4 vector)
{
packedValue = PackHelper(vector.X, vector.Y);
}
Vector4 IPackedVector.ToVector4()
{
return new Vector4(ToVector2(), 0.0f, 1.0f);
}
#endregion
#region Public Static Operators and Override Methods
public override string ToString()
{
return packedValue.ToString("X");
}
public override int GetHashCode()
{
return packedValue.GetHashCode();
}
public override bool Equals(object obj)
{
return ((obj is HalfVector2) && Equals((HalfVector2) obj));
}
public bool Equals(HalfVector2 other)
{
return packedValue.Equals(other.packedValue);
}
public static bool operator ==(HalfVector2 a, HalfVector2 b)
{
return a.Equals(b);
}
public static bool operator !=(HalfVector2 a, HalfVector2 b)
{
return !a.Equals(b);
}
#endregion
#region Private Static Pack Method
private static uint PackHelper(float vectorX, float vectorY)
{
return (uint) (
HalfTypeHelper.Convert(vectorX) |
((uint) (HalfTypeHelper.Convert(vectorY) << 0x10))
);
}
#endregion
}
}

View File

@ -0,0 +1,205 @@
#region License
/* MoonWorks - Game Development Framework
* Copyright 2021 Evan Hemsley
*/
/* Derived from code by Ethan Lee (Copyright 2009-2021).
* Released under the Microsoft Public License.
* See fna.LICENSE for details.
* Derived from code by the Mono.Xna Team (Copyright 2006).
* Released under the MIT License. See monoxna.LICENSE for details.
*/
#endregion
#region Using Statements
using System;
using MoonWorks.Math;
#endregion
namespace MoonWorks.Graphics
{
/// <summary>
/// Packed vector type containing four 16-bit floating-point values.
/// </summary>
public struct HalfVector4 : IPackedVector<ulong>, IPackedVector, IEquatable<HalfVector4>
{
#region Public Properties
/// <summary>
/// Directly gets or sets the packed representation of the value.
/// </summary>
/// <value>The packed representation of the value.</value>
public ulong PackedValue
{
get
{
return packedValue;
}
set
{
packedValue = value;
}
}
#endregion
#region Private Variables
private ulong packedValue;
#endregion
#region Public Constructors
/// <summary>
/// Initializes a new instance of the HalfVector4 structure.
/// </summary>
/// <param name="x">Initial value for the x component.</param>
/// <param name="y">Initial value for the y component.</param>
/// <param name="z">Initial value for the z component.</param>
/// <param name="w">Initial value for the q component.</param>
public HalfVector4(float x, float y, float z, float w)
{
packedValue = Pack(x, y, z, w);
}
/// <summary>
/// Initializes a new instance of the HalfVector4 structure.
/// </summary>
/// <param name="vector">
/// A vector containing the initial values for the components of the HalfVector4
/// structure.
/// </param>
public HalfVector4(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
}
#endregion
#region Public Methods
/// <summary>
/// Expands the packed representation into a Vector4.
/// </summary>
/// <returns>The expanded vector.</returns>
public Vector4 ToVector4()
{
return new Vector4(
HalfTypeHelper.Convert((ushort) packedValue),
HalfTypeHelper.Convert((ushort) (packedValue >> 0x10)),
HalfTypeHelper.Convert((ushort) (packedValue >> 0x20)),
HalfTypeHelper.Convert((ushort) (packedValue >> 0x30))
);
}
#endregion
#region IPackedVector Methods
/// <summary>
/// Sets the packed representation from a Vector4.
/// </summary>
/// <param name="vector">The vector to create the packed representation from.</param>
void IPackedVector.PackFromVector4(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
}
#endregion
#region Public Static Operators and Override Methods
/// <summary>
/// Returns a string representation of the current instance.
/// </summary>
/// <returns>String that represents the object.</returns>
public override string ToString()
{
return packedValue.ToString("X");
}
/// <summary>
/// Gets the hash code for the current instance.
/// </summary>
/// <returns>Hash code for the instance.</returns>
public override int GetHashCode()
{
return packedValue.GetHashCode();
}
/// <summary>
/// Returns a value that indicates whether the current instance is equal to a
/// specified object.
/// </summary>
/// <param name="obj">The object with which to make the comparison.</param>
/// <returns>
/// True if the current instance is equal to the specified object; false otherwise.
/// </returns>
public override bool Equals(object obj)
{
return ((obj is HalfVector4) && Equals((HalfVector4) obj));
}
/// <summary>
/// Returns a value that indicates whether the current instance is equal to a
/// specified object.
/// </summary>
/// <param name="other">The object with which to make the comparison.</param>
/// <returns>
/// True if the current instance is equal to the specified object; false otherwise.
/// </returns>
public bool Equals(HalfVector4 other)
{
return packedValue.Equals(other.packedValue);
}
/// <summary>
/// Compares the current instance of a class to another instance to determine
/// whether they are the same.
/// </summary>
/// <param name="a">The object to the left of the equality operator.</param>
/// <param name="b">The object to the right of the equality operator.</param>
/// <returns>True if the objects are the same; false otherwise.</returns>
public static bool operator ==(HalfVector4 a, HalfVector4 b)
{
return a.Equals(b);
}
/// <summary>
/// Compares the current instance of a class to another instance to determine
/// whether they are different.
/// </summary>
/// <param name="a">The object to the left of the equality operator.</param>
/// <param name="b">The object to the right of the equality operator.</param>
/// <returns>True if the objects are different; false otherwise.</returns>
public static bool operator !=(HalfVector4 a, HalfVector4 b)
{
return !a.Equals(b);
}
#endregion
#region Private Static Pack Method
/// <summary>
/// Packs a vector into a ulong.
/// </summary>
/// <param name="vector">The vector containing the values to pack.</param>
/// <returns>The ulong containing the packed values.</returns>
private static ulong Pack(float x, float y, float z, float w)
{
return (ulong) (
((ulong) HalfTypeHelper.Convert(x)) |
(((ulong) HalfTypeHelper.Convert(y) << 0x10)) |
(((ulong) HalfTypeHelper.Convert(z) << 0x20)) |
(((ulong) HalfTypeHelper.Convert(w) << 0x30))
);
}
#endregion
}
}

View File

@ -0,0 +1,39 @@
#region License
/* MoonWorks - Game Development Framework
* Copyright 2021 Evan Hemsley
*/
/* Derived from code by Ethan Lee (Copyright 2009-2021).
* Released under the Microsoft Public License.
* See fna.LICENSE for details.
* Derived from code by the Mono.Xna Team (Copyright 2006).
* Released under the MIT License. See monoxna.LICENSE for details.
*/
#endregion
using MoonWorks.Math;
namespace MoonWorks.Graphics
{
// http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.packedvector.ipackedvector.aspx
public interface IPackedVector
{
void PackFromVector4(Vector4 vector);
Vector4 ToVector4();
}
// PackedVector Generic interface
// http://msdn.microsoft.com/en-us/library/bb197661.aspx
public interface IPackedVector<TPacked> : IPackedVector
{
TPacked PackedValue
{
get;
set;
}
}
}

View File

@ -0,0 +1,142 @@
#region License
/* MoonWorks - Game Development Framework
* Copyright 2021 Evan Hemsley
*/
/* Derived from code by Ethan Lee (Copyright 2009-2021).
* Released under the Microsoft Public License.
* See fna.LICENSE for details.
* Derived from code by the Mono.Xna Team (Copyright 2006).
* Released under the MIT License. See monoxna.LICENSE for details.
*/
#endregion
#region Using Statements
using System;
using MoonWorks.Math;
#endregion
namespace MoonWorks.Graphics
{
public struct NormalizedByte2 : IPackedVector<ushort>, IEquatable<NormalizedByte2>
{
#region Public Properties
public ushort PackedValue
{
get
{
return packedValue;
}
set
{
packedValue = value;
}
}
#endregion
#region Private Variables
private ushort packedValue;
#endregion
#region Public Constructors
public NormalizedByte2(Vector2 vector)
{
packedValue = Pack(vector.X, vector.Y);
}
public NormalizedByte2(float x, float y)
{
packedValue = Pack(x, y);
}
#endregion
#region Public Methods
public Vector2 ToVector2()
{
return new Vector2(
((sbyte) (packedValue & 0xFF)) / 127.0f,
((sbyte) ((packedValue >> 8) & 0xFF)) / 127.0f
);
}
#endregion
#region IPackedVector Methods
void IPackedVector.PackFromVector4(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y);
}
Vector4 IPackedVector.ToVector4()
{
return new Vector4(ToVector2(), 0.0f, 1.0f);
}
#endregion
#region Public Static Operators and Override Methods
public static bool operator !=(NormalizedByte2 a, NormalizedByte2 b)
{
return a.packedValue != b.packedValue;
}
public static bool operator ==(NormalizedByte2 a, NormalizedByte2 b)
{
return a.packedValue == b.packedValue;
}
public override bool Equals(object obj)
{
return (obj is NormalizedByte2) && Equals((NormalizedByte2) obj);
}
public bool Equals(NormalizedByte2 other)
{
return packedValue == other.packedValue;
}
public override int GetHashCode()
{
return packedValue.GetHashCode();
}
public override string ToString()
{
return packedValue.ToString("X");
}
#endregion
#region Private Static Pack Method
private static ushort Pack(float x, float y)
{
int byte2 = (
((ushort)
System.Math.Round(MathHelper.Clamp(x, -1.0f, 1.0f) * 127.0f)
)
) & 0x00FF;
int byte1 = (
((ushort)
System.Math.Round(MathHelper.Clamp(y, -1.0f, 1.0f) * 127.0f)
) << 8
) & 0xFF00;
return (ushort) (byte2 | byte1);
}
#endregion
}
}

View File

@ -0,0 +1,147 @@
#region License
/* MoonWorks - Game Development Framework
* Copyright 2021 Evan Hemsley
*/
/* Derived from code by Ethan Lee (Copyright 2009-2021).
* Released under the Microsoft Public License.
* See fna.LICENSE for details.
* Derived from code by the Mono.Xna Team (Copyright 2006).
* Released under the MIT License. See monoxna.LICENSE for details.
*/
#endregion
#region Using Statements
using System;
using MoonWorks.Math;
#endregion
namespace MoonWorks.Graphics
{
public struct NormalizedByte4 : IPackedVector<uint>, IEquatable<NormalizedByte4>
{
#region Public Properties
public uint PackedValue
{
get
{
return packedValue;
}
set
{
packedValue = value;
}
}
#endregion
#region Private Variables
private uint packedValue;
#endregion
#region Public Constructors
public NormalizedByte4(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
}
public NormalizedByte4(float x, float y, float z, float w)
{
packedValue = Pack(x, y, z, w);
}
#endregion
#region Public Methods
public Vector4 ToVector4()
{
return new Vector4(
((sbyte) (packedValue & 0xFF)) / 127.0f,
((sbyte) ((packedValue >> 8) & 0xFF)) / 127.0f,
((sbyte) ((packedValue >> 16) & 0xFF)) / 127.0f,
((sbyte) ((packedValue >> 24) & 0xFF)) / 127.0f
);
}
#endregion
#region IPackedVector Methods
void IPackedVector.PackFromVector4(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
}
#endregion
#region Public Static Operators and Override Methods
public static bool operator !=(NormalizedByte4 a, NormalizedByte4 b)
{
return a.packedValue != b.packedValue;
}
public static bool operator ==(NormalizedByte4 a, NormalizedByte4 b)
{
return a.packedValue == b.packedValue;
}
public override bool Equals(object obj)
{
return (obj is NormalizedByte4) && Equals((NormalizedByte4) obj);
}
public bool Equals(NormalizedByte4 other)
{
return packedValue == other.packedValue;
}
public override int GetHashCode()
{
return packedValue.GetHashCode();
}
public override string ToString()
{
return packedValue.ToString("X");
}
#endregion
#region Private Static Pack Method
private static uint Pack(float x, float y, float z, float w)
{
uint byte4 = (
(uint) System.Math.Round(MathHelper.Clamp(x, -1.0f, 1.0f) * 127.0f)
) & 0x000000FF;
uint byte3 = (
(
(uint) System.Math.Round(MathHelper.Clamp(y, -1.0f, 1.0f) * 127.0f)
) << 8
) & 0x0000FF00;
uint byte2 = (
(
(uint) System.Math.Round(MathHelper.Clamp(z, -1.0f, 1.0f) * 127.0f)
) << 16
) & 0x00FF0000;
uint byte1 = (
(
(uint) System.Math.Round(MathHelper.Clamp(w, -1.0f, 1.0f) * 127.0f)
) << 24
) & 0xFF000000;
return byte4 | byte3 | byte2 | byte1;
}
#endregion
}
}

View File

@ -0,0 +1,151 @@
#region License
/* MoonWorks - Game Development Framework
* Copyright 2021 Evan Hemsley
*/
/* Derived from code by Ethan Lee (Copyright 2009-2021).
* Released under the Microsoft Public License.
* See fna.LICENSE for details.
* Derived from code by the Mono.Xna Team (Copyright 2006).
* Released under the MIT License. See monoxna.LICENSE for details.
*/
#endregion
#region Using Statements
using System;
using MoonWorks.Math;
#endregion
namespace MoonWorks.Graphics
{
public struct NormalizedShort2 : IPackedVector<uint>, IEquatable<NormalizedShort2>
{
#region Public Properties
public uint PackedValue
{
get
{
return packedValue;
}
set
{
packedValue = value;
}
}
#endregion
#region Private Variables
private uint packedValue;
#endregion
#region Public Constructors
public NormalizedShort2(Vector2 vector)
{
packedValue = Pack(vector.X, vector.Y);
}
public NormalizedShort2(float x, float y)
{
packedValue = Pack(x, y);
}
#endregion
#region Public Methods
public Vector2 ToVector2()
{
const float maxVal = 0x7FFF;
return new Vector2(
(short) (packedValue & 0xFFFF) / maxVal,
(short) (packedValue >> 0x10) / maxVal
);
}
#endregion
#region IPackedVector Methods
void IPackedVector.PackFromVector4(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y);
}
Vector4 IPackedVector.ToVector4()
{
return new Vector4(ToVector2(), 0.0f, 1.0f);
}
#endregion
#region Public Static Operators and Override Methods
public static bool operator !=(NormalizedShort2 a, NormalizedShort2 b)
{
return !a.Equals(b);
}
public static bool operator ==(NormalizedShort2 a, NormalizedShort2 b)
{
return a.Equals(b);
}
public override bool Equals(object obj)
{
return (obj is NormalizedShort2) && Equals((NormalizedShort2) obj);
}
public bool Equals(NormalizedShort2 other)
{
return packedValue.Equals(other.packedValue);
}
public override int GetHashCode()
{
return packedValue.GetHashCode();
}
public override string ToString()
{
return packedValue.ToString("X");
}
#endregion
#region Private Static Pack Method
private static uint Pack(float x, float y)
{
const float max = 0x7FFF;
const float min = -max;
uint word2 = (uint) (
(int) MathHelper.Clamp(
(float) System.Math.Round(x * max),
min,
max
) & 0xFFFF
);
uint word1 = (uint) ((
(int) MathHelper.Clamp(
(float) System.Math.Round(y * max),
min,
max
) & 0xFFFF
) << 0x10);
return (word2 | word1);
}
#endregion
}
}

View File

@ -0,0 +1,162 @@
#region License
/* MoonWorks - Game Development Framework
* Copyright 2021 Evan Hemsley
*/
/* Derived from code by Ethan Lee (Copyright 2009-2021).
* Released under the Microsoft Public License.
* See fna.LICENSE for details.
* Derived from code by the Mono.Xna Team (Copyright 2006).
* Released under the MIT License. See monoxna.LICENSE for details.
*/
#endregion
#region Using Statements
using System;
using MoonWorks.Math;
#endregion
namespace MoonWorks.Graphics
{
public struct NormalizedShort4 : IPackedVector<ulong>, IEquatable<NormalizedShort4>
{
#region Public Properties
public ulong PackedValue
{
get
{
return packedValue;
}
set
{
packedValue = value;
}
}
#endregion
#region Private Variables
private ulong packedValue;
#endregion
#region Public Constructors
public NormalizedShort4(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
}
public NormalizedShort4(float x, float y, float z, float w)
{
packedValue = Pack(x, y, z, w);
}
#endregion
#region Public Methods
public Vector4 ToVector4()
{
const float maxVal = 0x7FFF;
return new Vector4(
((short) (packedValue & 0xFFFF)) / maxVal,
((short) ((packedValue >> 0x10) & 0xFFFF)) / maxVal,
((short) ((packedValue >> 0x20) & 0xFFFF)) / maxVal,
((short) ((packedValue >> 0x30) & 0xFFFF)) / maxVal
);
}
#endregion
#region IPackedVector Methods
void IPackedVector.PackFromVector4(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
}
#endregion
#region Public Static Operators and Override Methods
public static bool operator !=(NormalizedShort4 a, NormalizedShort4 b)
{
return !a.Equals(b);
}
public static bool operator ==(NormalizedShort4 a, NormalizedShort4 b)
{
return a.Equals(b);
}
public override bool Equals(object obj)
{
return (obj is NormalizedShort4) && Equals((NormalizedShort4) obj);
}
public bool Equals(NormalizedShort4 other)
{
return packedValue.Equals(other.packedValue);
}
public override int GetHashCode()
{
return packedValue.GetHashCode();
}
public override string ToString()
{
return packedValue.ToString("X");
}
#endregion
#region Private Static Pack Method
private static ulong Pack(float x, float y, float z, float w)
{
const float max = 0x7FFF;
const float min = -max;
ulong word4 = (
(ulong) MathHelper.Clamp(
(float) System.Math.Round(x * max),
min,
max
) & 0xFFFF
);
ulong word3 = (
(ulong) MathHelper.Clamp(
(float) System.Math.Round(y * max),
min,
max
) & 0xFFFF
) << 0x10;
ulong word2 = (
(ulong) MathHelper.Clamp(
(float) System.Math.Round(z * max),
min,
max
) & 0xFFFF
) << 0x20;
ulong word1 = (
(ulong) MathHelper.Clamp(
(float) System.Math.Round(w * max),
min,
max
) & 0xFFFF
) << 0x30;
return (word4 | word3 | word2 | word1);
}
#endregion
}
}

View File

@ -0,0 +1,182 @@
#region License
/* MoonWorks - Game Development Framework
* Copyright 2021 Evan Hemsley
*/
/* Derived from code by Ethan Lee (Copyright 2009-2021).
* Released under the Microsoft Public License.
* See fna.LICENSE for details.
* Derived from code by the Mono.Xna Team (Copyright 2006).
* Released under the MIT License. See monoxna.LICENSE for details.
*/
#endregion
#region Using Statements
using System;
using MoonWorks.Math;
#endregion
namespace MoonWorks.Graphics
{
/// <summary>
/// Packed vector type containing unsigned normalized values ranging from 0 to 1.
/// The x and z components use 5 bits, and the y component uses 6 bits.
/// </summary>
public struct Rg32 : IPackedVector<uint>, IEquatable<Rg32>, IPackedVector
{
#region Public Properties
/// <summary>
/// Gets and sets the packed value.
/// </summary>
public uint PackedValue
{
get
{
return packedValue;
}
set
{
packedValue = value;
}
}
#endregion
#region Private Variables
private uint packedValue;
#endregion
#region Public Constructors
/// <summary>
/// Creates a new instance of Rg32.
/// </summary>
/// <param name="x">The x component</param>
/// <param name="y">The y component</param>
public Rg32(float x, float y)
{
packedValue = Pack(x, y);
}
/// <summary>
/// Creates a new instance of Rg32.
/// </summary>
/// <param name="vector">
/// Vector containing the components for the packed vector.
/// </param>
public Rg32(Vector2 vector)
{
packedValue = Pack(vector.X, vector.Y);
}
#endregion
#region Public Methods
/// <summary>
/// Gets the packed vector in Vector2 format.
/// </summary>
/// <returns>The packed vector in Vector2 format</returns>
public Vector2 ToVector2()
{
return new Vector2(
(packedValue & 0xFFFF) / 65535.0f,
(packedValue >> 16) / 65535.0f
);
}
#endregion
#region IPackedVector Methods
/// <summary>
/// Sets the packed vector from a Vector4.
/// </summary>
/// <param name="vector">Vector containing the components.</param>
void IPackedVector.PackFromVector4(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y);
}
/// <summary>
/// Gets the packed vector in Vector4 format.
/// </summary>
/// <returns>The packed vector in Vector4 format</returns>
Vector4 IPackedVector.ToVector4()
{
return new Vector4(ToVector2(), 0.0f, 1.0f);
}
#endregion
#region Public Static Operators and Override Methods
/// <summary>
/// Compares an object with the packed vector.
/// </summary>
/// <param name="obj">The object to compare.</param>
/// <returns>True if the object is equal to the packed vector.</returns>
public override bool Equals(object obj)
{
return (obj is Rg32) && Equals((Rg32) obj);
}
/// <summary>
/// Compares another Rg32 packed vector with the packed vector.
/// </summary>
/// <param name="other">The Rg32 packed vector to compare.</param>
/// <returns>True if the packed vectors are equal.</returns>
public bool Equals(Rg32 other)
{
return packedValue == other.packedValue;
}
/// <summary>
/// Gets a string representation of the packed vector.
/// </summary>
/// <returns>A string representation of the packed vector.</returns>
public override string ToString()
{
return packedValue.ToString("X");
}
/// <summary>
/// Gets a hash code of the packed vector.
/// </summary>
/// <returns>The hash code for the packed vector.</returns>
public override int GetHashCode()
{
return packedValue.GetHashCode();
}
public static bool operator ==(Rg32 lhs, Rg32 rhs)
{
return lhs.packedValue == rhs.packedValue;
}
public static bool operator !=(Rg32 lhs, Rg32 rhs)
{
return lhs.packedValue != rhs.packedValue;
}
#endregion
#region Private Static Pack Method
private static uint Pack(float x, float y)
{
return (uint) (
((uint) System.Math.Round(MathHelper.Clamp(x, 0, 1) * 65535.0f)) |
(((uint) System.Math.Round(MathHelper.Clamp(y, 0, 1) * 65535.0f)) << 16)
);
}
#endregion
}
}

View File

@ -0,0 +1,179 @@
#region License
/* MoonWorks - Game Development Framework
* Copyright 2021 Evan Hemsley
*/
/* Derived from code by Ethan Lee (Copyright 2009-2021).
* Released under the Microsoft Public License.
* See fna.LICENSE for details.
* Derived from code by the Mono.Xna Team (Copyright 2006).
* Released under the MIT License. See monoxna.LICENSE for details.
*/
#endregion
#region Using Statements
using System;
using MoonWorks.Math;
#endregion
namespace MoonWorks.Graphics
{
/// <summary>
/// Packed vector type containing unsigned normalized values ranging from 0 to 1.
/// The x and z components use 5 bits, and the y component uses 6 bits.
/// </summary>
public struct Rgba1010102 : IPackedVector<uint>, IEquatable<Rgba1010102>, IPackedVector
{
#region Public Properties
/// <summary>
/// Gets and sets the packed value.
/// </summary>
public uint PackedValue
{
get
{
return packedValue;
}
set
{
packedValue = value;
}
}
#endregion
#region Private Variables
private uint packedValue;
#endregion
#region Public Constructors
/// <summary>
/// Creates a new instance of Rgba1010102.
/// </summary>
/// <param name="x">The x component</param>
/// <param name="y">The y component</param>
/// <param name="z">The z component</param>
/// <param name="w">The w component</param>
public Rgba1010102(float x, float y, float z, float w)
{
packedValue = Pack(x, y, z, w);
}
/// <summary>
/// Creates a new instance of Rgba1010102.
/// </summary>
/// <param name="vector">
/// Vector containing the components for the packed vector.
/// </param>
public Rgba1010102(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
}
#endregion
#region Public Methods
/// <summary>
/// Gets the packed vector in Vector4 format.
/// </summary>
/// <returns>The packed vector in Vector4 format</returns>
public Vector4 ToVector4()
{
return new Vector4(
(packedValue & 0x03FF) / 1023.0f,
((packedValue >> 10) & 0x03FF) / 1023.0f,
((packedValue >> 20) & 0x03FF) / 1023.0f,
(packedValue >> 30) / 3.0f
);
}
#endregion
#region IPackedVector Methods
/// <summary>
/// Sets the packed vector from a Vector4.
/// </summary>
/// <param name="vector">Vector containing the components.</param>
void IPackedVector.PackFromVector4(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
}
#endregion
#region Public Static Operators and Override Methods
/// <summary>
/// Compares an object with the packed vector.
/// </summary>
/// <param name="obj">The object to compare.</param>
/// <returns>True if the object is equal to the packed vector.</returns>
public override bool Equals(object obj)
{
return (obj is Rgba1010102) && Equals((Rgba1010102) obj);
}
/// <summary>
/// Compares another Rgba1010102 packed vector with the packed vector.
/// </summary>
/// <param name="other">The Rgba1010102 packed vector to compare.</param>
/// <returns>True if the packed vectors are equal.</returns>
public bool Equals(Rgba1010102 other)
{
return packedValue == other.packedValue;
}
/// <summary>
/// Gets a string representation of the packed vector.
/// </summary>
/// <returns>A string representation of the packed vector.</returns>
public override string ToString()
{
return packedValue.ToString("X");
}
/// <summary>
/// Gets a hash code of the packed vector.
/// </summary>
/// <returns>The hash code for the packed vector.</returns>
public override int GetHashCode()
{
return packedValue.GetHashCode();
}
public static bool operator ==(Rgba1010102 lhs, Rgba1010102 rhs)
{
return lhs.packedValue == rhs.packedValue;
}
public static bool operator !=(Rgba1010102 lhs, Rgba1010102 rhs)
{
return lhs.packedValue != rhs.packedValue;
}
#endregion
#region Private Static Pack Method
private static uint Pack(float x, float y, float z, float w)
{
return (uint) (
((uint) System.Math.Round(MathHelper.Clamp(x, 0, 1) * 1023.0f)) |
((uint) System.Math.Round(MathHelper.Clamp(y, 0, 1) * 1023.0f) << 10) |
((uint) System.Math.Round(MathHelper.Clamp(z, 0, 1) * 1023.0f) << 20) |
((uint) System.Math.Round(MathHelper.Clamp(w, 0, 1) * 3.0f) << 30)
);
}
#endregion
}
}

View File

@ -0,0 +1,179 @@
#region License
/* MoonWorks - Game Development Framework
* Copyright 2021 Evan Hemsley
*/
/* Derived from code by Ethan Lee (Copyright 2009-2021).
* Released under the Microsoft Public License.
* See fna.LICENSE for details.
* Derived from code by the Mono.Xna Team (Copyright 2006).
* Released under the MIT License. See monoxna.LICENSE for details.
*/
#endregion
#region Using Statements
using System;
using MoonWorks.Math;
#endregion
namespace MoonWorks.Graphics
{
/// <summary>
/// Packed vector type containing unsigned normalized values ranging from 0 to 1.
/// The x and z components use 5 bits, and the y component uses 6 bits.
/// </summary>
public struct Rgba64 : IPackedVector<ulong>, IEquatable<Rgba64>, IPackedVector
{
#region Public Properties
/// <summary>
/// Gets and sets the packed value.
/// </summary>
public ulong PackedValue
{
get
{
return packedValue;
}
set
{
packedValue = value;
}
}
#endregion
#region Private Variables
private ulong packedValue;
#endregion
#region Public Constructors
/// <summary>
/// Creates a new instance of Rgba64.
/// </summary>
/// <param name="x">The x component</param>
/// <param name="y">The y component</param>
/// <param name="z">The z component</param>
/// <param name="w">The w component</param>
public Rgba64(float x, float y, float z, float w)
{
packedValue = Pack(x, y, z, w);
}
/// <summary>
/// Creates a new instance of Rgba64.
/// </summary>
/// <param name="vector">
/// Vector containing the components for the packed vector.
/// </param>
public Rgba64(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
}
#endregion
#region Public Methods
/// <summary>
/// Gets the packed vector in Vector4 format.
/// </summary>
/// <returns>The packed vector in Vector4 format</returns>
public Vector4 ToVector4()
{
return new Vector4(
(packedValue & 0xFFFF) / 65535.0f,
((packedValue >> 16) & 0xFFFF) / 65535.0f,
((packedValue >> 32) & 0xFFFF) / 65535.0f,
(packedValue >> 48) / 65535.0f
);
}
#endregion
#region IPackedVector Methods
/// <summary>
/// Sets the packed vector from a Vector4.
/// </summary>
/// <param name="vector">Vector containing the components.</param>
void IPackedVector.PackFromVector4(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
}
#endregion
#region Public Static Operators and Override Methods
/// <summary>
/// Compares an object with the packed vector.
/// </summary>
/// <param name="obj">The object to compare.</param>
/// <returns>True if the object is equal to the packed vector.</returns>
public override bool Equals(object obj)
{
return (obj is Rgba64) && Equals((Rgba64) obj);
}
/// <summary>
/// Compares another Rgba64 packed vector with the packed vector.
/// </summary>
/// <param name="other">The Rgba64 packed vector to compare.</param>
/// <returns>True if the packed vectors are equal.</returns>
public bool Equals(Rgba64 other)
{
return packedValue == other.packedValue;
}
/// <summary>
/// Gets a string representation of the packed vector.
/// </summary>
/// <returns>A string representation of the packed vector.</returns>
public override string ToString()
{
return packedValue.ToString("X");
}
/// <summary>
/// Gets a hash code of the packed vector.
/// </summary>
/// <returns>The hash code for the packed vector.</returns>
public override int GetHashCode()
{
return packedValue.GetHashCode();
}
public static bool operator ==(Rgba64 lhs, Rgba64 rhs)
{
return lhs.packedValue == rhs.packedValue;
}
public static bool operator !=(Rgba64 lhs, Rgba64 rhs)
{
return lhs.packedValue != rhs.packedValue;
}
#endregion
#region Private Static Pack Method
private static ulong Pack(float x, float y, float z, float w)
{
return (ulong) (
((ulong) System.Math.Round(MathHelper.Clamp(x, 0, 1) * 65535.0f)) |
(((ulong) System.Math.Round(MathHelper.Clamp(y, 0, 1) * 65535.0f)) << 16) |
(((ulong) System.Math.Round(MathHelper.Clamp(z, 0, 1) * 65535.0f)) << 32) |
(((ulong) System.Math.Round(MathHelper.Clamp(w, 0, 1) * 65535.0f)) << 48)
);
}
#endregion
}
}

View File

@ -0,0 +1,134 @@
#region License
/* MoonWorks - Game Development Framework
* Copyright 2021 Evan Hemsley
*/
/* Derived from code by Ethan Lee (Copyright 2009-2021).
* Released under the Microsoft Public License.
* See fna.LICENSE for details.
* Derived from code by the Mono.Xna Team (Copyright 2006).
* Released under the MIT License. See monoxna.LICENSE for details.
*/
#endregion
#region Using Statements
using System;
using MoonWorks.Math;
#endregion
namespace MoonWorks.Graphics
{
public struct Short2 : IPackedVector<uint>, IEquatable<Short2>
{
#region Public Properties
public uint PackedValue
{
get
{
return packedValue;
}
set
{
packedValue = value;
}
}
#endregion
#region Private Variables
private uint packedValue;
#endregion
#region Public Constructors
public Short2(Vector2 vector)
{
packedValue = Pack(vector.X, vector.Y);
}
public Short2(float x, float y)
{
packedValue = Pack(x, y);
}
#endregion
#region Public Methods
public Vector2 ToVector2()
{
return new Vector2(
(short) (packedValue & 0xFFFF),
(short) (packedValue >> 16)
);
}
#endregion
#region IPackedVector Methods
void IPackedVector.PackFromVector4(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y);
}
Vector4 IPackedVector.ToVector4()
{
return new Vector4(ToVector2(), 0.0f, 1.0f);
}
#endregion
#region Public Static Operators and Override Methods
public static bool operator !=(Short2 a, Short2 b)
{
return a.packedValue != b.packedValue;
}
public static bool operator ==(Short2 a, Short2 b)
{
return a.packedValue == b.packedValue;
}
public override bool Equals(object obj)
{
return (obj is Short2) && Equals((Short2) obj);
}
public bool Equals(Short2 other)
{
return this == other;
}
public override int GetHashCode()
{
return packedValue.GetHashCode();
}
public override string ToString()
{
return packedValue.ToString("X");
}
#endregion
#region Private Static Pack Method
private static uint Pack(float x, float y)
{
return (uint) (
((int) System.Math.Round(MathHelper.Clamp(x, -32768, 32767)) & 0x0000FFFF) |
(((int) System.Math.Round(MathHelper.Clamp(y, -32768, 32767))) << 16)
);
}
#endregion
}
}

View File

@ -0,0 +1,204 @@
#region License
/* MoonWorks - Game Development Framework
* Copyright 2021 Evan Hemsley
*/
/* Derived from code by Ethan Lee (Copyright 2009-2021).
* Released under the Microsoft Public License.
* See fna.LICENSE for details.
* Derived from code by the Mono.Xna Team (Copyright 2006).
* Released under the MIT License. See monoxna.LICENSE for details.
*/
#endregion
#region Using Statements
using System;
using MoonWorks.Math;
#endregion
namespace MoonWorks.Graphics
{
/// <summary>
/// Packed vector type containing four 16-bit signed integer values.
/// </summary>
public struct Short4 : IPackedVector<ulong>, IEquatable<Short4>
{
#region Public Properties
/// <summary>
/// Directly gets or sets the packed representation of the value.
/// </summary>
/// <value>The packed representation of the value.</value>
public ulong PackedValue
{
get
{
return packedValue;
}
set
{
packedValue = value;
}
}
#endregion
#region Private Variables
private ulong packedValue;
#endregion
#region Public Constructors
/// <summary>
/// Initializes a new instance of the Short4 class.
/// </summary>
/// <param name="vector">
/// A vector containing the initial values for the components of the Short4 structure.
/// </param>
public Short4(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
}
/// <summary>
/// Initializes a new instance of the Short4 class.
/// </summary>
/// <param name="x">Initial value for the x component.</param>
/// <param name="y">Initial value for the y component.</param>
/// <param name="z">Initial value for the z component.</param>
/// <param name="w">Initial value for the w component.</param>
public Short4(float x, float y, float z, float w)
{
packedValue = Pack(x, y, z, w);
}
#endregion
#region Public Methods
/// <summary>
/// Expands the packed representation into a Vector4.
/// </summary>
/// <returns>The expanded vector.</returns>
public Vector4 ToVector4()
{
return new Vector4(
(short) (packedValue & 0xFFFF),
(short) ((packedValue >> 16) & 0xFFFF),
(short) ((packedValue >> 32) & 0xFFFF),
(short) (packedValue >> 48)
);
}
#endregion
#region IPackedVector Methods
/// <summary>
/// Sets the packed representation from a Vector4.
/// </summary>
/// <param name="vector">The vector to create the packed representation from.</param>
void IPackedVector.PackFromVector4(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
}
#endregion
#region Public Static Operators and Override Methods
/// <summary>
/// Compares the current instance of a class to another instance to determine
/// whether they are different.
/// </summary>
/// <param name="a">The object to the left of the equality operator.</param>
/// <param name="b">The object to the right of the equality operator.</param>
/// <returns>True if the objects are different; false otherwise.</returns>
public static bool operator !=(Short4 a, Short4 b)
{
return a.PackedValue != b.PackedValue;
}
/// <summary>
/// Compares the current instance of a class to another instance to determine
/// whether they are the same.
/// </summary>
/// <param name="a">The object to the left of the equality operator.</param>
/// <param name="b">The object to the right of the equality operator.</param>
/// <returns>True if the objects are the same; false otherwise.</returns>
public static bool operator ==(Short4 a, Short4 b)
{
return a.PackedValue == b.PackedValue;
}
/// <summary>
/// Returns a value that indicates whether the current instance is equal to a
/// specified object.
/// </summary>
/// <param name="obj">The object with which to make the comparison.</param>
/// <returns>
/// True if the current instance is equal to the specified object; false otherwise.
/// </returns>
public override bool Equals(object obj)
{
return (obj is Short4) && Equals((Short4) obj);
}
/// <summary>
/// Returns a value that indicates whether the current instance is equal to a
/// specified object.
/// </summary>
/// <param name="other">The object with which to make the comparison.</param>
/// <returns>
/// True if the current instance is equal to the specified object; false otherwise.
/// </returns>
public bool Equals(Short4 other)
{
return this == other;
}
/// <summary>
/// Gets the hash code for the current instance.
/// </summary>
/// <returns>Hash code for the instance.</returns>
public override int GetHashCode()
{
return packedValue.GetHashCode();
}
/// <summary>
/// Returns a string representation of the current instance.
/// </summary>
/// <returns>String that represents the object.</returns>
public override string ToString()
{
return packedValue.ToString("X");
}
#endregion
#region Private Static Pack Method
/// <summary>
/// Packs a vector into a ulong.
/// </summary>
/// <param name="vector">The vector containing the values to pack.</param>
/// <returns>The ulong containing the packed values.</returns>
static ulong Pack(float x, float y, float z, float w)
{
return (ulong) (
((long) System.Math.Round(MathHelper.Clamp(x, -32768, 32767)) & 0xFFFF ) |
(((long) System.Math.Round(MathHelper.Clamp(y, -32768, 32767)) << 16) & 0xFFFF0000) |
(((long) System.Math.Round(MathHelper.Clamp(z, -32768, 32767)) << 32) & 0xFFFF00000000) |
((long) System.Math.Round(MathHelper.Clamp(w, -32768, 32767)) << 48)
);
}
#endregion
}
}

View File

@ -6,15 +6,6 @@ using System.Runtime.InteropServices;
*/
namespace MoonWorks.Graphics
{
[StructLayout(LayoutKind.Sequential)]
public struct Color
{
public byte r;
public byte g;
public byte b;
public byte a;
}
[StructLayout(LayoutKind.Sequential)]
public struct DepthStencilValue
{