diff options
| author | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2021-12-03 21:34:45 +0100 |
|---|---|---|
| committer | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2021-12-03 21:34:45 +0100 |
| commit | c80c3593c8123ac010966fae0f68b6578b842ac7 (patch) | |
| tree | d003e73023e18ab97f9724facd2465f09b3a4058 | |
| parent | fbaddfdb4ac23580bc31ebc4470ed0b43ceeb9af (diff) | |
use vector types where possible
| -rw-r--r-- | vk.generator/Program.cs | 34 | ||||
| -rw-r--r-- | vulkan/Vector2i.cs | 77 | ||||
| -rw-r--r-- | vulkan/Vector2ui.cs | 77 | ||||
| -rw-r--r-- | vulkan/Vector3ui.cs | 82 |
4 files changed, 266 insertions, 4 deletions
diff --git a/vk.generator/Program.cs b/vk.generator/Program.cs index c986036..1839705 100644 --- a/vk.generator/Program.cs +++ b/vk.generator/Program.cs @@ -420,6 +420,29 @@ namespace vk.generator { tw.WriteLine (@"}"); } static string[] valueTypes = { "byte", "int", "uint", "float", "ushort", "ulong" }; + static bool tryGetVectorType (string type, int dim, out string vectorType) { + vectorType = null; + switch (type) { + case "float": + switch (dim) { + case 2: vectorType = "Vector2i"; return true; + default: return false; + } + case "int": + switch (dim) { + case 2: vectorType = "Vector2ui"; return true; + case 3: vectorType = "Vector3ui"; return true; + default: return false; + } + case "uint": + switch (dim) { + case 2: vectorType = "Vector2ui"; return true; + case 3: vectorType = "Vector3ui"; return true; + default: return false; + } + default: return false; + } + } static void gen_struct (IndentedTextWriter tw, StructDef sd) { bool hasDoc = tryFindRefPage (sd.Name, out refPage rp); if (hasDoc) @@ -488,8 +511,12 @@ namespace vk.generator { tw.Indent--; tw.WriteLine (@"}"); } else if (dims.Length == 1) { - string strSize = int.TryParse (dims[0], out dim) ? dim.ToString () : $"(int)Vk.{EnumerantValue.GetCSName (dims[0], null)}"; - tw.WriteLine ($"public fixed {typeStr} {mb.Name}[{strSize}];"); + if (int.TryParse (dims[0], out dim) && tryGetVectorType (typeStr, dim, out string vectorType)) + tw.WriteLine ($"public {vectorType} {mb.Name};"); + else { + string strSize = int.TryParse (dims[0], out dim) ? dim.ToString () : $"(int)Vk.{EnumerantValue.GetCSName (dims[0], null)}"; + tw.WriteLine ($"public fixed {typeStr} {mb.Name}[{strSize}];"); + } } else { int totSize = 1; for (int i = 0; i < dims.Length; i++) { @@ -523,11 +550,10 @@ namespace vk.generator { tw.Indent--; tw.WriteLine (@"}"); } - static void gen_structs (string englobingStaticClass) { using (StreamWriter sr = new StreamWriter (vkNetTargetPath($"structs_{englobingStaticClass}"), false, System.Text.Encoding.UTF8)) { using (IndentedTextWriter tw = new IndentedTextWriter (sr)) { - writePreamble (tw, "System.Runtime.InteropServices"); + writePreamble (tw, "System.Runtime.InteropServices", "System.Numerics"); tw.Indent++; foreach (StructDef sd in types.OfType<StructDef> ()) { diff --git a/vulkan/Vector2i.cs b/vulkan/Vector2i.cs new file mode 100644 index 0000000..985ae1e --- /dev/null +++ b/vulkan/Vector2i.cs @@ -0,0 +1,77 @@ +// Copyright (c) 2013-2022 Bruyère Jean-Philippe jp_bruyere@hotmail.com +// +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) + +using System; + +namespace Vulkan +{ + public struct Vector2i : IEquatable<Vector2i>, IEquatable<int> + { + public static readonly Vector2i UnitX = new Vector2i (1,0); + public static readonly Vector2i UnitY = new Vector2i (0,1); + + public int X, Y; + + #region CTOR + public Vector2i (int x, int y) { + X = x; + Y = y; + } + public Vector2i (int pos) { + X = pos; + Y = pos; + } + #endregion + + public int Length => (int)Math.Sqrt (Math.Pow (X, 2) + Math.Pow (Y, 2)); + public double LengthD => Math.Sqrt (Math.Pow (X, 2) + Math.Pow (Y, 2)); + public Vector2i Normalized { + get { + int l = Length; + return new Vector2i (X / l, Y / l); + } + } + //public static implicit operator PointD (Vector2i p) => new PointD (p.X, p.Y); + public static implicit operator Vector2i (int i) => new Vector2i (i, i); + + public static Vector2i operator + (Vector2i p1, Vector2i p2) => new Vector2i (p1.X + p2.X, p1.Y + p2.Y); + public static Vector2i operator + (Vector2i p, int i) => new Vector2i (p.X + i, p.Y + i); + public static Vector2i operator - (Vector2i p1, Vector2i p2) => new Vector2i (p1.X - p2.X, p1.Y - p2.Y); + public static Vector2i operator - (Vector2i p, int i) => new Vector2i (p.X - i, p.Y - i); + public static Vector2i operator * (Vector2i p1, Vector2i p2) => new Vector2i (p1.X * p2.X, p1.Y * p2.Y); + public static Vector2i operator * (Vector2i p, int d) => new Vector2i (p.X * d, p.Y * d); + public static Vector2i operator / (Vector2i p1, Vector2i p2) => new Vector2i (p1.X / p2.X, p1.Y / p2.Y); + public static Vector2i operator / (Vector2i p, int d) => new Vector2i (p.X / d, p.Y / d); + + public static bool operator == (Vector2i s1, Vector2i s2) => s1.Equals (s2); + public static bool operator != (Vector2i s1, Vector2i s2) => !s1.Equals (s2); + public static bool operator == (Vector2i s, int i) => s.Equals(i); + public static bool operator != (Vector2i s, int i) => !s.Equals (i); + public static bool operator > (Vector2i p1, Vector2i p2) => p1.X > p2.X && p1.Y > p2.Y; + public static bool operator > (Vector2i s, int i) => s.X > i && s.Y > i; + public static bool operator < (Vector2i p1, Vector2i p2) => p1.X < p2.X && p1.Y < p2.Y; + public static bool operator < (Vector2i s, int i) => s.X < i && s.Y < i; + public static bool operator >= (Vector2i p1, Vector2i p2) => p1.X >= p2.X && p1.Y >= p2.Y; + public static bool operator >= (Vector2i s, int i) => s.X >= i && s.Y >= i; + public static bool operator <= (Vector2i p1, Vector2i p2) => p1.X <= p2.X && p1.Y <= p2.Y; + public static bool operator <= (Vector2i s, int i) => s.X <= i && s.Y <= i; + + public bool Equals (Vector2i other) => X == other.X && Y == other.Y; + public bool Equals (int other) => X == other && Y == other; + + + public override int GetHashCode () => HashCode.Combine (X, Y); + public override bool Equals (object obj) => obj is Vector2i s ? Equals (s) : false; + public override string ToString () => $"{X},{Y}"; + public static Vector2i Parse (string s) { + ReadOnlySpan<char> tmp = s.AsSpan (); + if (tmp.Length == 0) + return default (Vector2i); + int ioc = tmp.IndexOf (','); + return ioc < 0 ? new Vector2i (int.Parse (tmp)) : new Vector2i ( + int.Parse (tmp.Slice (0, ioc)), + int.Parse (tmp.Slice (ioc + 1))); + } + } +} diff --git a/vulkan/Vector2ui.cs b/vulkan/Vector2ui.cs new file mode 100644 index 0000000..2218140 --- /dev/null +++ b/vulkan/Vector2ui.cs @@ -0,0 +1,77 @@ +// Copyright (c) 2013-2022 Bruyère Jean-Philippe jp_bruyere@hotmail.com +// +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) + +using System; + +namespace Vulkan +{ + public struct Vector2ui : IEquatable<Vector2ui>, IEquatable<uint> + { + public static readonly Vector2ui UnitX = new Vector2ui (1,0); + public static readonly Vector2ui UnitY = new Vector2ui (0,1); + + public uint X, Y; + + #region CTOR + public Vector2ui (uint x, uint y) { + X = x; + Y = y; + } + public Vector2ui (uint pos) { + X = pos; + Y = pos; + } + #endregion + + public uint Length => (uint)Math.Sqrt (Math.Pow (X, 2) + Math.Pow (Y, 2)); + public double LengthD => Math.Sqrt (Math.Pow (X, 2) + Math.Pow (Y, 2)); + public Vector2ui Normalized { + get { + uint l = Length; + return new Vector2ui (X / l, Y / l); + } + } + //public static implicit operator PointD (Vector2i p) => new PointD (p.X, p.Y); + public static implicit operator Vector2ui (uint i) => new Vector2ui (i, i); + + public static Vector2ui operator + (Vector2ui p1, Vector2ui p2) => new Vector2ui (p1.X + p2.X, p1.Y + p2.Y); + public static Vector2ui operator + (Vector2ui p, uint i) => new Vector2ui (p.X + i, p.Y + i); + public static Vector2ui operator - (Vector2ui p1, Vector2ui p2) => new Vector2ui (p1.X - p2.X, p1.Y - p2.Y); + public static Vector2ui operator - (Vector2ui p, uint i) => new Vector2ui (p.X - i, p.Y - i); + public static Vector2ui operator * (Vector2ui p1, Vector2ui p2) => new Vector2ui (p1.X * p2.X, p1.Y * p2.Y); + public static Vector2ui operator * (Vector2ui p, uint d) => new Vector2ui (p.X * d, p.Y * d); + public static Vector2ui operator / (Vector2ui p1, Vector2ui p2) => new Vector2ui (p1.X / p2.X, p1.Y / p2.Y); + public static Vector2ui operator / (Vector2ui p, uint d) => new Vector2ui (p.X / d, p.Y / d); + + public static bool operator == (Vector2ui s1, Vector2ui s2) => s1.Equals (s2); + public static bool operator != (Vector2ui s1, Vector2ui s2) => !s1.Equals (s2); + public static bool operator == (Vector2ui s, uint i) => s.Equals(i); + public static bool operator != (Vector2ui s, uint i) => !s.Equals (i); + public static bool operator > (Vector2ui p1, Vector2ui p2) => p1.X > p2.X && p1.Y > p2.Y; + public static bool operator > (Vector2ui s, uint i) => s.X > i && s.Y > i; + public static bool operator < (Vector2ui p1, Vector2ui p2) => p1.X < p2.X && p1.Y < p2.Y; + public static bool operator < (Vector2ui s, uint i) => s.X < i && s.Y < i; + public static bool operator >= (Vector2ui p1, Vector2ui p2) => p1.X >= p2.X && p1.Y >= p2.Y; + public static bool operator >= (Vector2ui s, uint i) => s.X >= i && s.Y >= i; + public static bool operator <= (Vector2ui p1, Vector2ui p2) => p1.X <= p2.X && p1.Y <= p2.Y; + public static bool operator <= (Vector2ui s, uint i) => s.X <= i && s.Y <= i; + + public bool Equals (Vector2ui other) => X == other.X && Y == other.Y; + public bool Equals (uint other) => X == other && Y == other; + + + public override int GetHashCode () => HashCode.Combine (X, Y); + public override bool Equals (object obj) => obj is Vector2ui s ? Equals (s) : false; + public override string ToString () => $"{X},{Y}"; + public static Vector2ui Parse (string s) { + ReadOnlySpan<char> tmp = s.AsSpan (); + if (tmp.Length == 0) + return default (Vector2ui); + int ioc = tmp.IndexOf (','); + return ioc < 0 ? new Vector2ui (uint.Parse (tmp)) : new Vector2ui ( + uint.Parse (tmp.Slice (0, ioc)), + uint.Parse (tmp.Slice (ioc + 1))); + } + } +} diff --git a/vulkan/Vector3ui.cs b/vulkan/Vector3ui.cs new file mode 100644 index 0000000..0dee93b --- /dev/null +++ b/vulkan/Vector3ui.cs @@ -0,0 +1,82 @@ +// Copyright (c) 2013-2022 Bruyère Jean-Philippe jp_bruyere@hotmail.com +// +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) + +using System; + +namespace Vulkan +{ + public struct Vector3ui : IEquatable<Vector3ui>, IEquatable<uint> + { + public static readonly Vector3ui UnitX = new Vector3ui (1,0,0); + public static readonly Vector3ui UnitY = new Vector3ui (0,1,0); + public static readonly Vector3ui UnitZ = new Vector3ui (0,1,0); + + public uint X, Y, Z; + + #region CTOR + public Vector3ui (uint x, uint y, uint z) { + X = x; + Y = y; + Z = z; + + } + public Vector3ui (uint pos) { + X = pos; + Y = pos; + Z = pos; + } + #endregion + + /*public uint Length => (uint)Math.Sqrt (Math.Pow (X, 2) + Math.Pow (Y, 2)); + public double LengthD => Math.Sqrt (Math.Pow (X, 2) + Math.Pow (Y, 2)); + public Vector3ui Normalized { + get { + uint l = Length; + return new Vector3ui (X / l, Y / l); + } + }*/ + //public static implicit operator PointD (Vector2i p) => new PointD (p.X, p.Y); + public static implicit operator Vector3ui (uint i) => new Vector3ui (i, i, i); + + public static Vector3ui operator + (Vector3ui p1, Vector3ui p2) => new Vector3ui (p1.X + p2.X, p1.Y + p2.Y, p1.Z + p2.Z); + public static Vector3ui operator + (Vector3ui p, uint i) => new Vector3ui (p.X + i, p.Y + i, p.Z + i); + public static Vector3ui operator - (Vector3ui p1, Vector3ui p2) => new Vector3ui (p1.X - p2.X, p1.Y - p2.Y, p1.Z - p2.Z); + public static Vector3ui operator - (Vector3ui p, uint i) => new Vector3ui (p.X - i, p.Y - i, p.Z - i); + public static Vector3ui operator * (Vector3ui p1, Vector3ui p2) => new Vector3ui (p1.X * p2.X, p1.Y * p2.Y, p1.Z * p2.Z); + public static Vector3ui operator * (Vector3ui p, uint d) => new Vector3ui (p.X * d, p.Y * d, p.Z * d); + public static Vector3ui operator / (Vector3ui p1, Vector3ui p2) => new Vector3ui (p1.X / p2.X, p1.Y / p2.Y, p1.Z / p2.Z); + public static Vector3ui operator / (Vector3ui p, uint d) => new Vector3ui (p.X / d, p.Y / d, p.Z / d); + + public static bool operator == (Vector3ui s1, Vector3ui s2) => s1.Equals (s2); + public static bool operator != (Vector3ui s1, Vector3ui s2) => !s1.Equals (s2); + public static bool operator == (Vector3ui s, uint i) => s.Equals(i); + public static bool operator != (Vector3ui s, uint i) => !s.Equals (i); + public static bool operator > (Vector3ui p1, Vector3ui p2) => p1.X > p2.X && p1.Y > p2.Y && p1.Z > p2.Z; + public static bool operator > (Vector3ui s, uint i) => s.X > i && s.Y > i && s.Z > i; + public static bool operator < (Vector3ui p1, Vector3ui p2) => p1.X < p2.X && p1.Y < p2.Y && p1.Z < p2.Z; + public static bool operator < (Vector3ui s, uint i) => s.X < i && s.Y < i && s.Z < i; + public static bool operator >= (Vector3ui p1, Vector3ui p2) => p1.X >= p2.X && p1.Y >= p2.Y && p1.Z >= p2.Z; + public static bool operator >= (Vector3ui s, uint i) => s.X >= i && s.Y >= i && s.Z >= i; + public static bool operator <= (Vector3ui p1, Vector3ui p2) => p1.X <= p2.X && p1.Y <= p2.Y && p1.Z <= p2.Z; + public static bool operator <= (Vector3ui s, uint i) => s.X <= i && s.Y <= i && s.Z <= i; + + public bool Equals (Vector3ui other) => X == other.X && Y == other.Y && Z == other.Z; + public bool Equals (uint other) => X == other && Y == other && Z == other; + + + public override int GetHashCode () => HashCode.Combine (X, Y, Z); + public override bool Equals (object obj) => obj is Vector3ui s ? Equals (s) : false; + public override string ToString () => $"{X},{Y},{Z}"; + public static Vector3ui Parse (string s) { + ReadOnlySpan<char> tmp = s.AsSpan (); + if (tmp.Length == 0) + return default (Vector3ui); + int ioc = tmp.IndexOf (','); + return ioc < 0 ? new Vector3ui (uint.Parse (tmp)) : new Vector3ui ( + uint.Parse (tmp.Slice (0, ioc)), + uint.Parse (tmp.Slice (ioc + 1)), + uint.Parse (tmp.Slice (ioc + 2))); + } + } +} |