aboutsummaryrefslogtreecommitdiffstatsci
diff options
context:
space:
mode:
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>2021-12-04 04:13:56 +0100
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>2021-12-04 04:13:56 +0100
commitba592ca9099fe4e378692bd2e8c953de90a4beb5 (patch)
tree766a1bf12e789bf37b86818584b5ed1998e01cb0
parent0bbba34c81f356da8e00e04d0f61c6b9a66337c9 (diff)
Utf8StringPointer
-rw-r--r--tests/Program.cs24
-rw-r--r--vk.generator/Program.cs15
-rw-r--r--vulkan/ExtensionMethods.cs30
-rw-r--r--vulkan/Utf8StringPointer.cs33
-rw-r--r--vulkan/Vector2i.cs2
-rw-r--r--vulkan/VkBool32.cs2
6 files changed, 70 insertions, 36 deletions
diff --git a/tests/Program.cs b/tests/Program.cs
index b2050d2..f7b743d 100644
--- a/tests/Program.cs
+++ b/tests/Program.cs
@@ -8,24 +8,6 @@ using Version = Vulkan.Version;
namespace tests
{
- [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
- public struct NewStuff
- {
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)Vulkan.Vk.UuidSize)]
- public StringBuilder name;
-
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4*16)]
- public float[,] calibrate;
-
- [MarshalAs(UnmanagedType.R4)]
- public float DMTI;
-
- [MarshalAs(UnmanagedType.R4)]
- public float DMTII;
-
- // Etc
- }
-
class Program
{
@@ -35,8 +17,10 @@ namespace tests
VkInstanceCreateInfo ci = VkInstanceCreateInfo.New();
VkApplicationInfo ai = VkApplicationInfo.New();
- //ai.apiVersion = new Version (1,0,0);
- //ai.applicationVersion = new Version (1,0,0);
+ ai.apiVersion = new Version (1,0,0);
+ ai.applicationVersion = new Version (1,0,0);
+ ai.pApplicationName = "new application name";
+ ai.pEngineName = "new engine name";
VkClearColorValue clear = new VkClearColorValue(255,0,0);
diff --git a/vk.generator/Program.cs b/vk.generator/Program.cs
index 3c298e5..1828143 100644
--- a/vk.generator/Program.cs
+++ b/vk.generator/Program.cs
@@ -488,6 +488,15 @@ namespace vk.generator {
tw.WriteLine (@"}");
if (kvp.Value == "char") {
+ tw.WriteLine ($"public static implicit operator string ({structName} s) => s.ToString();");
+ tw.WriteLine ($"public static implicit operator {structName} (string s)");
+ tw.WriteLine (@"{");
+ tw.Indent++;
+ tw.WriteLine ($"{structName} tmp = default;");
+ tw.WriteLine ($"tmp.Update (s);");
+ tw.WriteLine ($"return tmp;");
+ tw.Indent--;
+ tw.WriteLine (@"}");
tw.WriteLine ($"public override string ToString()");
tw.WriteLine (@"{");
tw.Indent++;
@@ -496,7 +505,7 @@ namespace vk.generator {
tw.WriteLine ($"return System.Text.Encoding.UTF8.GetString (bytes);");
tw.Indent--;
tw.WriteLine (@"}");
- tw.WriteLine ($"public void Update(string value)");
+ tw.WriteLine ($"void Update(string value)");
tw.WriteLine (@"{");
tw.Indent++;
tw.WriteLine ($"Span<{structName}> valSpan = MemoryMarshal.CreateSpan(ref this, 1);");
@@ -533,7 +542,9 @@ namespace vk.generator {
EnumDef ed = enums.FirstOrDefault (e => e.Name == mb.typedef.Name);
- if (mb.typedef.IndirectionLevel > 0 || mb.typedef.Name.StartsWith ("PFN_", StringComparison.Ordinal))
+ if (mb.typedef.IndirectionLevel == 1 && mb.typedef.Name == "char")
+ typeStr = "Utf8StringPointer";
+ else if (mb.typedef.IndirectionLevel > 0 || mb.typedef.Name.StartsWith ("PFN_", StringComparison.Ordinal))
typeStr = "IntPtr";
else if (ed != null) {
//enums has to be stored in struct as int or uint for enums are not yet blittable in ms .NET
diff --git a/vulkan/ExtensionMethods.cs b/vulkan/ExtensionMethods.cs
index 0b9797f..4102f07 100644
--- a/vulkan/ExtensionMethods.cs
+++ b/vulkan/ExtensionMethods.cs
@@ -38,18 +38,18 @@ namespace Vulkan {
return handles[obj].AddrOfPinnedObject ();
}
- GCHandle hnd = GCHandle.Alloc (obj, GCHandleType.Pinned);
- handles.Add (obj, hnd);
- return hnd.AddrOfPinnedObject ();
- }
- public static IntPtr Pin<T> (this List<T> obj) {
- if (handles.ContainsKey (obj))
- Debug.WriteLine ("Pinning already pinned object: {0}", obj);
+ GCHandle hnd = GCHandle.Alloc (obj, GCHandleType.Pinned);
+ handles.Add (obj, hnd);
+ return hnd.AddrOfPinnedObject ();
+ }
+ public static IntPtr Pin<T> (this List<T> obj) {
+ if (handles.ContainsKey (obj))
+ Debug.WriteLine ("Pinning already pinned object: {0}", obj);
- GCHandle hnd = GCHandle.Alloc (obj.ToArray(), GCHandleType.Pinned);
- handles.Add (obj, hnd);
- return hnd.AddrOfPinnedObject ();
- }
+ GCHandle hnd = GCHandle.Alloc (obj.ToArray(), GCHandleType.Pinned);
+ handles.Add (obj, hnd);
+ return hnd.AddrOfPinnedObject ();
+ }
public static IntPtr Pin<T> (this T[] obj) {
if (handles.ContainsKey (obj))
Debug.WriteLine ("Pinning already pinned object: {0}", obj);
@@ -63,9 +63,15 @@ namespace Vulkan {
Debug.WriteLine ("Trying to pin already pinned object: {0}", obj);
return handles[obj].AddrOfPinnedObject ();
}
+ byte[] n = System.Text.Encoding.UTF8.GetBytes(obj +'\0');
+ GCHandle hnd = GCHandle.Alloc (n, GCHandleType.Pinned);
+ handles.Add (obj, hnd);
+ return hnd.AddrOfPinnedObject ();
+ }
+ public static IntPtr PinPointer (this string obj) {
byte[] n = System.Text.Encoding.UTF8.GetBytes(obj +'\0');
GCHandle hnd = GCHandle.Alloc (n, GCHandleType.Pinned);
- handles.Add (obj, hnd);
+ handles.Add (hnd.AddrOfPinnedObject (), hnd);
return hnd.AddrOfPinnedObject ();
}
diff --git a/vulkan/Utf8StringPointer.cs b/vulkan/Utf8StringPointer.cs
new file mode 100644
index 0000000..93ae3d3
--- /dev/null
+++ b/vulkan/Utf8StringPointer.cs
@@ -0,0 +1,33 @@
+// Copyright (c) 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 Utf8StringPointer : IDisposable
+ {
+ IntPtr handle;
+ Utf8StringPointer (IntPtr ptr) {
+ handle = ptr;
+ }
+ public Utf8StringPointer (string str) {
+ handle = str.PinPointer();
+ }
+ public static implicit operator string (Utf8StringPointer pt)
+ => pt.ToString();
+ public static implicit operator IntPtr (Utf8StringPointer pt)
+ => pt.handle;
+ public static implicit operator Utf8StringPointer (IntPtr ptr)
+ => new Utf8StringPointer (ptr);
+ public static implicit operator Utf8StringPointer (string s)
+ => new Utf8StringPointer (s);
+ public override string ToString()
+ => handle == IntPtr.Zero ? "" : System.Runtime.InteropServices.Marshal.PtrToStringUTF8 (handle);
+ public void Dispose()
+ {
+ handle.Unpin();
+ }
+ }
+} \ No newline at end of file
diff --git a/vulkan/Vector2i.cs b/vulkan/Vector2i.cs
index 985ae1e..d090b33 100644
--- a/vulkan/Vector2i.cs
+++ b/vulkan/Vector2i.cs
@@ -1,4 +1,4 @@
-// Copyright (c) 2013-2022 Bruyère Jean-Philippe jp_bruyere@hotmail.com
+// Copyright (c) 2022 Bruyère Jean-Philippe jp_bruyere@hotmail.com
//
// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
diff --git a/vulkan/VkBool32.cs b/vulkan/VkBool32.cs
index f424a4b..b93da0f 100644
--- a/vulkan/VkBool32.cs
+++ b/vulkan/VkBool32.cs
@@ -16,7 +16,7 @@ namespace Vulkan
public uint Value;
/// <summary>
- /// Constructs a new <see cref="VkBool32"/> with the given raw value.
+ /// Constructs a new <see cref="VkBool32"/> with the given raw value.
/// </summary>
/// <param name="value"></param>
public VkBool32(uint value)