diff options
| -rw-r--r-- | .vscode/launch.json | 6 | ||||
| -rw-r--r-- | Directory.Build.props | 12 | ||||
| -rw-r--r-- | samples/Directory.Build.props | 7 | ||||
| -rw-r--r-- | samples/create_instance/Program.cs | 17 | ||||
| -rw-r--r-- | vk.generator/Program.cs | 17 | ||||
| -rw-r--r-- | vk.generator/vk.generator.csproj | 3 | ||||
| -rw-r--r-- | vk.rewrite/Program.cs | 29 | ||||
| -rw-r--r-- | vk.rewrite/vk.rewrite.csproj | 3 | ||||
| -rw-r--r-- | vulkan/vulkan.csproj | 4 |
9 files changed, 73 insertions, 25 deletions
diff --git a/.vscode/launch.json b/.vscode/launch.json index 1e002fb..9c772af 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -31,7 +31,7 @@ "type": "coreclr", "request": "launch", "preLaunchTask": "build create_instance", - "program": "${workspaceFolder}/samples/create_instance/bin/Debug/netcoreapp3.1/create_instance", + "program": "${workspaceFolder}/build/Debug/netcoreapp3.1/create_instance", "args": [], "cwd": "${workspaceFolder}", "console": "externalTerminal", @@ -42,7 +42,7 @@ "type": "coreclr", "request": "launch", "preLaunchTask": "build select_physical_dev", - "program": "${workspaceFolder}/samples/select_physical_dev/bin/Debug/netcoreapp3.1/select_physical_dev", + "program": "${workspaceFolder}/build/Debug/netcoreapp3.1/select_physical_dev", "args": [], "cwd": "${workspaceFolder}", "console": "externalTerminal", @@ -53,7 +53,7 @@ "type": "coreclr", "request": "launch", "preLaunchTask": "build create_device", - "program": "${workspaceFolder}/samples/create_device/bin/Debug/netcoreapp3.1/create_device", + "program": "${workspaceFolder}/build/Debug/netcoreapp3.1/create_device", "args": [], "cwd": "${workspaceFolder}", "console": "externalTerminal", diff --git a/Directory.Build.props b/Directory.Build.props index 8df5522..a53c9d5 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -13,6 +13,18 @@ <!--regenerate vk.net sources with vk.generator--> <Regenerate>true</Regenerate> + <!-- + AutoSetSType = TRUE + automatic set of the sType field with corresponding structure type. + - rewrite will emit sType property set + - structure pointer proxies will set sType property for single instance and enumerable + this implies that a loop for the enumerable on each set. + + AutoSetSType = FALSE + Use the New static property to create a new instance of the structure with the correct value for sType. + ex: VkInstanceCreateInfo ci = VkInstanceCreateInfo.New; + --> + <AutoSetSType>true</AutoSetSType> </PropertyGroup> </Project> diff --git a/samples/Directory.Build.props b/samples/Directory.Build.props index 1e82540..136cee2 100644 --- a/samples/Directory.Build.props +++ b/samples/Directory.Build.props @@ -7,9 +7,16 @@ <Authors>Jean-Philippe Bruyère</Authors> <Copyright></Copyright> <Company></Company> + + <SolutionDir>$(MSBuildThisFileDirectory)..\</SolutionDir> + <OutputPath>$(SolutionDir)build\$(Configuration)\</OutputPath> </PropertyGroup> <ItemGroup> <ProjectReference Include="$(MSBuildThisFileDirectory)..\vulkan\vulkan.csproj" /> <Compile Include="$(MSBuildThisFileDirectory)common\*.cs"/> </ItemGroup> + <PropertyGroup Condition=" '$(AutoSetSType)' == 'true'"> + <DefineConstants>$(DefineConstants);AUTO_SET_STYPE</DefineConstants> + </PropertyGroup> + </Project> diff --git a/samples/create_instance/Program.cs b/samples/create_instance/Program.cs index 474ae72..234afc3 100644 --- a/samples/create_instance/Program.cs +++ b/samples/create_instance/Program.cs @@ -1,10 +1,6 @@ using System; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Text; using Vulkan; using static Vulkan.Vk; -using Version = Vulkan.Version; namespace tests { @@ -14,13 +10,22 @@ namespace tests static void Main(string[] args) { VkInstance instance; - +#if AUTO_SET_STYPE + Console.WriteLine ("AUTO_SET_STYPE: ON"); using (VkApplicationInfo ai = new VkApplicationInfo()) { using (VkInstanceCreateInfo ci = new VkInstanceCreateInfo {pApplicationInfo = ai}){ Console.WriteLine (vkCreateInstance (ci, IntPtr.Zero, out instance)); } } - +#else + Console.WriteLine ("AUTO_SET_STYPE: OFF"); + using (VkApplicationInfo ai = VkApplicationInfo.New) { + VkInstanceCreateInfo ci = VkInstanceCreateInfo.New; + ci.pApplicationInfo = ai; + Console.WriteLine (vkCreateInstance (ci, IntPtr.Zero, out instance)); + ci.Dispose(); + } +#endif vkDestroyInstance (instance, IntPtr.Zero); } } diff --git a/vk.generator/Program.cs b/vk.generator/Program.cs index c44a6ad..0a6da2f 100644 --- a/vk.generator/Program.cs +++ b/vk.generator/Program.cs @@ -740,8 +740,9 @@ namespace vk.generator { baseType = baseType.Substring (0, baseType.Length - 1); } TypeDef td = types.FirstOrDefault (t=>t.Name == resolveAlias (baseType)); - string csStructName = null; bool isEnum = td != null && td.IsEnum; +#if AUTO_SET_STYPE + string csStructName = null; if (td != null && td.category == TypeCategories.@struct) { StructDef sd = td as StructDef; MemberDef mdSType = sd.members.Where (mb => mb.Name == "sType").FirstOrDefault (); @@ -750,7 +751,7 @@ namespace vk.generator { structTypeEnum.AllValues.FirstOrDefault(st=>st.Name == mdSType.defaultValue); csStructName = evSType == null ? null : $"{structTypeEnum.CSName}.{evSType.CSName}"; } - +#endif string ptrType = isIEnumerable ? baseType.Replace('.','_') + "CollectionPtr" : baseType.Replace('.','_') + "Ptr"; tw.WriteLine ($"public class {ptrType} {{"); tw.Indent++; @@ -764,6 +765,7 @@ namespace vk.generator { tw.WriteLine ($"internal int Count => instance.Count();"); tw.WriteLine ($"internal {ptrType} (IEnumerable<{baseType}> str) {{"); tw.Indent++; +#if AUTO_SET_STYPE if (csStructName != null) { tw.WriteLine ($"if (str != null) {{"); tw.Indent++; @@ -783,6 +785,10 @@ namespace vk.generator { tw.WriteLine ($"instance = str;"); tw.Indent--; } +#else + tw.WriteLine ($"instance = str;"); + tw.Indent--; +#endif } else { tw.WriteLine ($"internal {baseType} instance;"); if (isEnum) { @@ -792,8 +798,10 @@ namespace vk.generator { tw.WriteLine ($"internal IntPtr handle => instance.PinPointer();"); tw.WriteLine ($"internal {ptrType} ({baseType} str) {{"); tw.Indent++; +#if AUTO_SET_STYPE if (csStructName != null) tw.WriteLine ($"str.sType = {csStructName};"); +#endif tw.WriteLine ($"instance = str;"); } @@ -897,9 +905,10 @@ namespace vk.generator { mdSType.defaultValue == null ? null : structTypeEnum.AllValues.FirstOrDefault(st=>st.Name == mdSType.defaultValue); string csStructName = evSType == null ? null : $"{structTypeEnum.CSName}.{evSType.CSName}"; - if (csStructName!=null) { +#if AUTO_SET_STYPE + if (csStructName!=null) tw.WriteLine ($"[StructureType ({evSType.value})]"); - } +#endif getStructurePtrProxies (sd, out IEnumerable<string> ptrProxies, out IEnumerable<string> utf8StringPointers); diff --git a/vk.generator/vk.generator.csproj b/vk.generator/vk.generator.csproj index 531eb2c..343cbe4 100644 --- a/vk.generator/vk.generator.csproj +++ b/vk.generator/vk.generator.csproj @@ -6,5 +6,8 @@ <AllowUnsafeBlocks>true</AllowUnsafeBlocks> <DefineConstants>$(DefineConstants);LOG_VK_NET_GEN;DEBUG_STRUCT_SIZES</DefineConstants> </PropertyGroup> + <PropertyGroup Condition=" '$(AutoSetSType)' == 'true'"> + <DefineConstants>$(DefineConstants);AUTO_SET_STYPE</DefineConstants> + </PropertyGroup> </Project> diff --git a/vk.rewrite/Program.cs b/vk.rewrite/Program.cs index 2601f48..dcd7103 100644 --- a/vk.rewrite/Program.cs +++ b/vk.rewrite/Program.cs @@ -19,12 +19,11 @@ namespace Vk.Rewrite static TypeReference trefCalliRewrite; static TypeReference trefPin; +#if AUTO_SET_STYPE static TypeReference trefStructureType; - +#endif 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; @@ -60,13 +59,11 @@ namespace Vk.Rewrite trefCalliRewrite = mainModule.GetType (mainVkNamespace+".Generator.CalliRewriteAttribute"); trefPin = mainModule.GetType (mainVkNamespace+".Generator.PinAttribute"); +#if AUTO_SET_STYPE trefStructureType = mainModule.GetType (mainVkNamespace+".StructureTypeAttribute"); - - foreach (TypeDefinition type in mainModule.Types) - { - - foreach (MethodDefinition method in type.Methods) - { +#endif + foreach (TypeDefinition type in mainModule.Types) { + foreach (MethodDefinition method in type.Methods) { if (method.CustomAttributes.Any(ca => ca.AttributeType == trefCalliRewrite)) RewriteMethod (method); } @@ -87,42 +84,49 @@ namespace Vk.Rewrite TypeDefinition td = parameterType.IsByReference ? parameterType.GetElementType() as TypeDefinition : parameterType as TypeDefinition; +#if AUTO_SET_STYPE PropertyDefinition pdSType = td?.Properties.FirstOrDefault (f=>f.Name == "sType"); - CustomAttribute ca = td == null ? null : td.CustomAttributes.FirstOrDefault (ca => ca.AttributeType == trefStructureType); int structType = 0; if (ca != null) { PropertyDefinition pd = (ca.AttributeType as TypeDefinition).Properties[0]; structType = (int)ca.ConstructorArguments[0].Value; } +#endif if (method.Parameters[i].CustomAttributes.Any(ca => ca.AttributeType == trefPin)) { VariableDefinition variable = new VariableDefinition(parameterType); method.Body.Variables.Add(variable); il.Emit(OpCodes.Stloc, variable); +#if AUTO_SET_STYPE if (ca != null) { il.Emit(OpCodes.Ldloca, variable); il.Emit(OpCodes.Ldc_I4, structType); il.Emit(OpCodes.Call, pdSType.SetMethod); } +#endif il.Emit(OpCodes.Ldloca, variable); variable = new VariableDefinition(new PinnedType(new ByReferenceType(parameterType))); method.Body.Variables.Add(variable); il.Emit(OpCodes.Stloc, variable); il.Emit(OpCodes.Ldloc, variable); il.Emit(OpCodes.Conv_I); - } else if (parameterType.IsByReference) { + } else if (parameterType.IsByReference) { VariableDefinition byRefVariable = new VariableDefinition(new PinnedType(parameterType)); method.Body.Variables.Add(byRefVariable); il.Emit(OpCodes.Stloc, byRefVariable); +#if AUTO_SET_STYPE if (pdSType != null) { il.Emit(OpCodes.Ldloc, byRefVariable); il.Emit(OpCodes.Ldc_I4, structType); il.Emit(OpCodes.Call, pdSType.SetMethod); } +#endif il.Emit(OpCodes.Ldloc, byRefVariable); il.Emit(OpCodes.Conv_I); - } else if (ca != null) { + } +#if AUTO_SET_STYPE + else if (ca != null) { VariableDefinition variable = new VariableDefinition(parameterType); method.Body.Variables.Add(variable); il.Emit(OpCodes.Stloc, variable); @@ -130,6 +134,7 @@ namespace Vk.Rewrite il.Emit(OpCodes.Ldc_I4, structType); il.Emit(OpCodes.Call, pdSType.SetMethod); } +#endif } FieldDefinition field = method.DeclaringType.Fields.SingleOrDefault(fd => fd.Name == method.Name + "_ptr"); diff --git a/vk.rewrite/vk.rewrite.csproj b/vk.rewrite/vk.rewrite.csproj index 8808b12..dd8f410 100644 --- a/vk.rewrite/vk.rewrite.csproj +++ b/vk.rewrite/vk.rewrite.csproj @@ -8,5 +8,8 @@ <ItemGroup> <PackageReference Include="Mono.Cecil" Version="0.11.4" /> </ItemGroup> + <PropertyGroup Condition=" '$(AutoSetSType)' == 'true'"> + <DefineConstants>$(DefineConstants);AUTO_SET_STYPE</DefineConstants> + </PropertyGroup> </Project> diff --git a/vulkan/vulkan.csproj b/vulkan/vulkan.csproj index 545cd0e..b60d202 100644 --- a/vulkan/vulkan.csproj +++ b/vulkan/vulkan.csproj @@ -30,6 +30,10 @@ <NoWarn>$(NoWarn);1591;0419</NoWarn> </PropertyGroup> + <PropertyGroup Condition=" '$(AutoSetSType)' == 'true'"> + <DefineConstants>$(DefineConstants);AUTO_SET_STYPE</DefineConstants> + </PropertyGroup> + <ItemGroup> <Content Include="vk.net-64.png" Pack="true" PackagePath="" /> </ItemGroup> |