diff options
| -rw-r--r-- | vk.net.sln | 3 | ||||
| -rw-r--r-- | vk.rewrite/Program.cs | 206 | ||||
| -rw-r--r-- | vulkan/BindingsHelpers.cs | 36 | ||||
| -rw-r--r-- | vulkan/Delegates.cs | 24 | ||||
| -rw-r--r-- | vulkan/Structs.cs | 2 | ||||
| -rw-r--r-- | vulkan/vulkan.csproj | 7 |
6 files changed, 111 insertions, 167 deletions
@@ -40,9 +40,6 @@ Global EndGlobalSection GlobalSection(NestedProjects) = preSolution EndGlobalSection - GlobalSection(ProjectDependencies) = postSolution - {37D01BE5-5FE2-4742-BD5B-5D2C25FCF146} = {B105512C-9461-4802-B299-A6E7A29CDAEE} - EndGlobalSection GlobalSection(MonoDevelopProperties) = preSolution Policies = $0 $0.TextStylePolicy = $1 diff --git a/vk.rewrite/Program.cs b/vk.rewrite/Program.cs index db41bab..d2a254e 100644 --- a/vk.rewrite/Program.cs +++ b/vk.rewrite/Program.cs @@ -13,148 +13,128 @@ using System.Text; namespace Vk.Rewrite { - public class Program - { + public class Program + { const string mainVkNamespace = "Vulkan"; - static TypeReference s_calliRewriteRef; - static MethodReference s_stringToHGlobalUtf8Ref; - static MethodDefinition s_freeHGlobalRef; - static TypeReference s_stringHandleRef; + static TypeReference s_calliRewriteRef; - public static int Main(string[] args) - { - + public static int Main(string[] args) + { - if ((args.Length < 1) == true || args[0] == null) { - Console.WriteLine ("Error: argument 1 must be the path of VK.dll."); - return -1; - } - string vkDllPath = args[0]; - string outputPath = null; - bool rewriteInPlace = false; - - if (outputPath == null) - { - outputPath = Path.GetTempFileName (); - rewriteInPlace = true; - } - Rewrite(vkDllPath, outputPath); + if ((args.Length < 1) == true || args[0] == null) { + Console.WriteLine ("Error: argument 1 must be the path of VK.dll."); + return -1; + } + string vkDllPath = args[0]; + string outputPath = null; + bool rewriteInPlace = false; - if (rewriteInPlace) { - File.Delete (vkDllPath); - File.Copy (outputPath, vkDllPath); - File.Delete (outputPath); - outputPath = vkDllPath; - } + if (outputPath == null) + { + outputPath = Path.GetTempFileName (); + rewriteInPlace = true; + } - Console.WriteLine (outputPath + " rewrited."); - return 0; - } + Rewrite(vkDllPath, outputPath); - private static void Rewrite(string vkDllPath, string outputPath) - { - using (AssemblyDefinition vkDll = AssemblyDefinition.ReadAssembly (vkDllPath)) - { - ModuleDefinition mainModule = vkDll.Modules[0]; + if (rewriteInPlace) { + File.Delete (vkDllPath); + File.Copy (outputPath, vkDllPath); + File.Delete (outputPath); + outputPath = vkDllPath; + } - s_calliRewriteRef = mainModule.GetType (mainVkNamespace+".Generator.CalliRewriteAttribute"); - s_stringHandleRef = mainModule.GetType (mainVkNamespace+".StringHandle"); + Console.WriteLine (outputPath + " rewrited."); + return 0; + } - TypeDefinition bindingHelpers = mainModule.GetType(mainVkNamespace+".BindingsHelpers"); + private static void Rewrite(string vkDllPath, string outputPath) + { + using (AssemblyDefinition vkDll = AssemblyDefinition.ReadAssembly (vkDllPath)) + { + ModuleDefinition mainModule = vkDll.Modules[0]; - s_stringToHGlobalUtf8Ref = bindingHelpers.Methods.Single (md => md.Name == "StringToHGlobalUtf8"); - s_freeHGlobalRef = bindingHelpers.Methods.Single (md => md.Name == "FreeHGlobal"); + s_calliRewriteRef = mainModule.GetType (mainVkNamespace+".Generator.CalliRewriteAttribute"); - foreach (TypeDefinition type in mainModule.Types) - { + foreach (TypeDefinition type in mainModule.Types) + { - foreach (MethodDefinition method in type.Methods) - { - if (method.CustomAttributes.Any(ca => ca.AttributeType == s_calliRewriteRef)) - RewriteMethod (method); - } + foreach (MethodDefinition method in type.Methods) + { + if (method.CustomAttributes.Any(ca => ca.AttributeType == s_calliRewriteRef)) + RewriteMethod (method); + } } - vkDll.Write(outputPath); - } - } + vkDll.Write(outputPath); + } + } - static void RewriteMethod (MethodDefinition method) - { - ILProcessor il = method.Body.GetILProcessor(); - il.Body.Instructions.Clear(); + static void RewriteMethod (MethodDefinition method) + { + ILProcessor il = method.Body.GetILProcessor(); + il.Body.Instructions.Clear(); - List<VariableDefinition> stringParams = new List<VariableDefinition>(); - for (int i = 0; i < method.Parameters.Count; i++) - { - EmitLoadArgument (il, i, method.Parameters); - TypeReference parameterType = method.Parameters[i].ParameterType; + List<VariableDefinition> stringParams = new List<VariableDefinition>(); + for (int i = 0; i < method.Parameters.Count; i++) + { + EmitLoadArgument (il, i, method.Parameters); + TypeReference parameterType = method.Parameters[i].ParameterType; if (parameterType.IsGenericInstance) { if (parameterType.Name == "Nullable`1") System.Diagnostics.Debugger.Break (); } - if (parameterType.FullName == "System.String") - { - VariableDefinition variableDef = new VariableDefinition(s_stringHandleRef); - method.Body.Variables.Add(variableDef); - il.Emit(OpCodes.Call, s_stringToHGlobalUtf8Ref); - il.Emit(OpCodes.Stloc, variableDef); - il.Emit(OpCodes.Ldloc, variableDef); - stringParams.Add(variableDef); - } - else if (parameterType.IsByReference) - { - VariableDefinition byRefVariable = new VariableDefinition(new PinnedType(parameterType)); - method.Body.Variables.Add(byRefVariable); - il.Emit(OpCodes.Stloc, byRefVariable); - il.Emit(OpCodes.Ldloc, byRefVariable); - il.Emit(OpCodes.Conv_I); - } - } - - FieldDefinition field = method.DeclaringType.Fields.SingleOrDefault(fd => fd.Name == method.Name + "_ptr"); - - if (field == null) - throw new InvalidOperationException("Can't find function pointer field for " + method.Name); - - il.Emit(OpCodes.Ldsfld, field); + if (parameterType.IsByReference) + { + VariableDefinition byRefVariable = new VariableDefinition(new PinnedType(parameterType)); + method.Body.Variables.Add(byRefVariable); + il.Emit(OpCodes.Stloc, byRefVariable); + il.Emit(OpCodes.Ldloc, byRefVariable); + il.Emit(OpCodes.Conv_I); + } + } + + FieldDefinition field = method.DeclaringType.Fields.SingleOrDefault(fd => fd.Name == method.Name + "_ptr"); + + if (field == null) + throw new InvalidOperationException("Can't find function pointer field for " + method.Name); + + il.Emit(OpCodes.Ldsfld, field); + + CallSite callSite = new CallSite(method.ReturnType) {CallingConvention = MethodCallingConvention.StdCall}; - CallSite callSite = new CallSite(method.ReturnType) {CallingConvention = MethodCallingConvention.StdCall}; - foreach (ParameterDefinition pd in method.Parameters) - { - TypeReference parameterType; + { + TypeReference parameterType; - if (pd.ParameterType.IsByReference) - parameterType = new PointerType(pd.ParameterType.GetElementType()); - else if (pd.ParameterType.FullName == "System.String") - parameterType = s_stringHandleRef; - else - parameterType = pd.ParameterType; + if (pd.ParameterType.IsByReference) + parameterType = new PointerType(pd.ParameterType.GetElementType()); + else + parameterType = pd.ParameterType; - ParameterDefinition calliPD = new ParameterDefinition(pd.Name, pd.Attributes, parameterType); + ParameterDefinition calliPD = new ParameterDefinition(pd.Name, pd.Attributes, parameterType); - callSite.Parameters.Add(calliPD); - } - il.Emit(OpCodes.Calli, callSite); + callSite.Parameters.Add(calliPD); + } + il.Emit(OpCodes.Calli, callSite); - foreach (VariableDefinition stringVar in stringParams) - { - il.Emit(OpCodes.Ldloc, stringVar); - il.Emit(OpCodes.Call, s_freeHGlobalRef); - } + /*foreach (VariableDefinition stringVar in stringParams) + { + il.Emit(OpCodes.Ldloc, stringVar); + il.Emit(OpCodes.Call, s_freeHGlobalRef); + }*/ - il.Emit(OpCodes.Ret); + il.Emit(OpCodes.Ret); - if (method.Body.Variables.Count > 0) - method.Body.InitLocals = true; - } + if (method.Body.Variables.Count > 0) + method.Body.InitLocals = true; + } - static void EmitLoadArgument(ILProcessor il, int i, Collection<ParameterDefinition> parameters) - { + static void EmitLoadArgument(ILProcessor il, int i, Collection<ParameterDefinition> parameters) + { switch (i) { case 0: il.Emit(OpCodes.Ldarg_0); @@ -172,6 +152,6 @@ namespace Vk.Rewrite il.Emit(OpCodes.Ldarg, i); break; } - } - } + } + } }
\ No newline at end of file diff --git a/vulkan/BindingsHelpers.cs b/vulkan/BindingsHelpers.cs deleted file mode 100644 index 60a4ae2..0000000 --- a/vulkan/BindingsHelpers.cs +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) 2017 Eric Mellino -// Copyright (c) 2019 Jean-Philippe Bruyère <jp_bruyere@hotmail.com> -// -// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) -using System; -using System.Diagnostics; -using System.Runtime.InteropServices; -using System.Text; - -namespace Vulkan { - public static class BindingsHelpers - { - public static unsafe StringHandle StringToHGlobalUtf8(string s) - { - Debug.Assert(s != null); - int byteCount = Encoding.UTF8.GetByteCount(s); - IntPtr retPtr = Marshal.AllocHGlobal(byteCount); - fixed (char* stringPtr = s) - { - Encoding.UTF8.GetBytes(stringPtr, s.Length, (byte*)retPtr.ToPointer(), byteCount); - } - - return new StringHandle() { Handle = retPtr }; - } - - public static void FreeHGlobal(StringHandle ptr) - { - Marshal.FreeHGlobal(ptr.Handle); - } - } - - public struct StringHandle - { - public IntPtr Handle; - } -} diff --git a/vulkan/Delegates.cs b/vulkan/Delegates.cs index f400aca..10a2fb0 100644 --- a/vulkan/Delegates.cs +++ b/vulkan/Delegates.cs @@ -6,31 +6,31 @@ using System; namespace Vulkan { - public unsafe delegate void* PFN_vkAllocationFunction( - void* pUserData, + public delegate IntPtr PFN_vkAllocationFunction( + IntPtr pUserData, UIntPtr size, UIntPtr alignment, VkSystemAllocationScope allocationScope); - public unsafe delegate void* PFN_vkReallocationFunction( - void* pUserData, - void* pOriginal, + public delegate IntPtr PFN_vkReallocationFunction( + IntPtr pUserData, + IntPtr pOriginal, UIntPtr size, UIntPtr alignment, VkSystemAllocationScope allocationScope); - public unsafe delegate void PFN_vkFreeFunction( - void* pUserData, - void* pMemory); + public delegate void PFN_vkFreeFunction( + IntPtr pUserData, + IntPtr pMemory); - public unsafe delegate void PFN_vkInternalAllocationNotification( - void* pUserData, + public delegate void PFN_vkInternalAllocationNotification( + IntPtr pUserData, UIntPtr size, VkInternalAllocationType allocationType, VkSystemAllocationScope allocationScope); - public unsafe delegate void PFN_vkInternalFreeNotification( - void* pUserData, + public delegate void PFN_vkInternalFreeNotification( + IntPtr pUserData, UIntPtr size, VkInternalAllocationType allocationType, VkSystemAllocationScope allocationScope); diff --git a/vulkan/Structs.cs b/vulkan/Structs.cs index 022a679..bb449c1 100644 --- a/vulkan/Structs.cs +++ b/vulkan/Structs.cs @@ -265,7 +265,7 @@ namespace Vulkan descriptorCount = count; } } - public unsafe partial struct VkAttachmentReference + public partial struct VkAttachmentReference { public VkAttachmentReference(uint attachment, VkImageLayout layout) { diff --git a/vulkan/vulkan.csproj b/vulkan/vulkan.csproj index 0c49024..558eb20 100644 --- a/vulkan/vulkan.csproj +++ b/vulkan/vulkan.csproj @@ -19,9 +19,12 @@ <PackageIcon>vk.net-64.png</PackageIcon> <SynchReleaseVersion>false</SynchReleaseVersion> <PackageReleaseNotes> - + * Docummentation extracted from Vulkan-Docs + * no usafe code. + * use Vector type where possible + * Proxy structures to handle arrays. </PackageReleaseNotes> - <AllowUnsafeBlocks>true</AllowUnsafeBlocks> + <AllowUnsafeBlocks>false</AllowUnsafeBlocks> <GenerateDocumentationFile>true</GenerateDocumentationFile> <NoWarn>$(NoWarn);1591;0419</NoWarn> |