Generator: More bindings improvements and handle primitive types function/method usage.

This commit is contained in:
Amer Koleci
2022-09-07 09:48:52 +02:00
parent 916ece6365
commit b1f807dcfb
5 changed files with 682 additions and 590 deletions

View File

@@ -24,7 +24,7 @@
</PropertyGroup>
<!-- https://github.com/dotnet/sourcelink -->
<PropertyGroup>
<!--<PropertyGroup>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>false</IncludeSymbols>
@@ -37,6 +37,6 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
</ItemGroup>-->
</Project>

View File

@@ -568,7 +568,7 @@ public static class Program
else
{
string fullTypeName = $"{parameter.Type.Api}.{parameter.Type.Name}";
if (!IsEnum(fullTypeName))
if (!IsPrimitive(parameter.Type) && !IsEnum(fullTypeName))
{
asPointer = true;
}
@@ -583,6 +583,7 @@ public static class Program
parameterType = NormalizeTypeName(writer.Api, parameterType);
string parameterName = parameter.Name;
bool isOptional = parameter.Attrs.Any(item => item is string str && str == "Optional");
if (parameter.Attrs.Any(item => item is string str && str == "ComOutPtr"))
{
@@ -1075,6 +1076,11 @@ public static class Program
foreach (var parameter in method.Params)
{
if (method.Name == "SetBreakOnSeverity")
{
}
bool asPointer = false;
string parameterType = default;
if (parameter.Type.Kind == "ApiRef")
@@ -1087,7 +1093,7 @@ public static class Program
else
{
string fullTypeName = $"{parameter.Type.Api}.{parameter.Type.Name}";
if (!IsEnum(fullTypeName))
if (!IsPrimitive(parameter.Type) && !IsEnum(fullTypeName))
{
asPointer = true;
}
@@ -1163,7 +1169,17 @@ public static class Program
writer.WriteLine("[MethodImpl(MethodImplOptions.AggressiveInlining)]");
writer.WriteLine($"[VtblIndex({vtblIndex})]");
using (writer.PushBlock($"public {returnType} {method.Name}({argumentsString})"))
string methodSuffix = string.Empty;
if (method.Name == "GetType")
{
if (string.IsNullOrEmpty(argumentsString))
{
methodSuffix = "new ";
}
}
using (writer.PushBlock($"public {methodSuffix}{returnType} {method.Name}({argumentsString})"))
{
writer.WriteLineUndindented("#if NET6_0_OR_GREATER");
if (returnType != "void")
@@ -1487,11 +1503,33 @@ public static class Program
return GetTypeName(dataType.Name);
}
private static bool IsPrimitive(string typeName)
{
switch (typeName)
{
case "void":
case "bool":
case "int":
case "uint":
case "Bool32":
return true;
case "nint":
case "nuint":
case "IntPtr":
case "UIntPtr":
return true;
}
return false;
}
private static bool IsPrimitive(ApiDataType dataType)
{
if (dataType.Kind == "ApiRef")
{
string apiRefType = GetTypeName($"{dataType.Api}.{dataType.Name}");
return IsPrimitive(apiRefType);
}
else if (dataType.Kind == "PointerTo")
{
@@ -1504,19 +1542,7 @@ public static class Program
}
string typeName = GetTypeName(dataType.Name);
switch (typeName)
{
case "void":
case "int":
case "uint":
return true;
case "nint":
case "nuint":
return true;
}
return false;
return IsPrimitive(typeName);
}
private static bool IsEnum(string typeName)

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,24 +1,58 @@
// Copyright © Amer Koleci and Contributors.
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.
using System.Reflection;
using System.Runtime.InteropServices;
using Win32;
using Win32.Graphics.Dxgi;
using Win32.Graphics.Direct3D11;
using static Win32.Apis;
using static Win32.Graphics.Dxgi.Apis;
using static Win32.Graphics.Direct3D11.Apis;
using Win32.Graphics.Direct3D;
using Win32.Graphics.Direct3D11;
using Win32.Graphics.Dxgi;
using static Win32.Apis;
using static Win32.Graphics.Direct3D11.Apis;
using static Win32.Graphics.Dxgi.Apis;
using MessageId = Win32.Graphics.Direct3D11.MessageId;
using InfoQueueFilter = Win32.Graphics.Direct3D11.InfoQueueFilter;
namespace ClearScreen;
public static unsafe class Program
{
#if DEBUG
static bool SdkLayersAvailable()
{
HResult hr = D3D11CreateDevice(
null,
DriverType.Null, // There is no need to create a real hardware device.
IntPtr.Zero,
CreateDeviceFlag.Debug, // Check for the SDK layers.
null, // Any feature level will do.
0,
D3D11_SDK_VERSION,
null, // No need to keep the D3D device reference.
null, // No need to know the feature level.
null // No need to keep the D3D device context reference.
);
return hr.Success;
}
#endif
public static void Main()
{
using ComPtr<IDXGIFactory1> factory = default;
HResult hr = CreateDXGIFactory1(__uuidof<IDXGIFactory4>(), (void**)&factory);
using ComPtr<IDXGIFactory2> factory = default;
uint factoryFlags = 0;
#if DEBUG
{
using ComPtr<IDXGIInfoQueue> dxgiInfoQueue = default;
if (DXGIGetDebugInterface1(0, __uuidof<IDXGIInfoQueue>(), (void**)dxgiInfoQueue.GetAddressOf()).Success)
{
factoryFlags = DXGI_CREATE_FACTORY_DEBUG;
dxgiInfoQueue.Get()->SetBreakOnSeverity(DXGI_DEBUG_ALL, InfoQueueMessageSeverity.Error, true);
dxgiInfoQueue.Get()->SetBreakOnSeverity(DXGI_DEBUG_ALL, InfoQueueMessageSeverity.Corruption, true);
}
}
#endif
HResult hr = CreateDXGIFactory2(factoryFlags, __uuidof<IDXGIFactory2>(), (void**)&factory);
{
using ComPtr<IDXGIFactory5> factory5 = default;
@@ -42,7 +76,7 @@ public static unsafe class Program
adapterIndex++)
{
AdapterDescription1 desc = default;
adapter.Get()->GetDesc1(&desc);
adapter.Get()->GetDesc1(&desc).ThrowIfFailed();
if ((desc.Flags & AdapterFlags.Software) != AdapterFlags.None)
continue;
@@ -55,11 +89,11 @@ public static unsafe class Program
if (adapter.Get() == null)
{
for (uint adapterIndex = 0;
factory.Get()->EnumAdapters1(adapterIndex, adapter.ReleaseAndGetAddressOf()).Success;
adapterIndex++)
factory.Get()->EnumAdapters1(adapterIndex, adapter.ReleaseAndGetAddressOf()).Success;
adapterIndex++)
{
AdapterDescription1 desc = default;
adapter.Get()->GetDesc1(&desc);
adapter.Get()->GetDesc1(&desc).ThrowIfFailed();
if ((desc.Flags & AdapterFlags.Software) != AdapterFlags.None)
continue;
@@ -74,17 +108,49 @@ public static unsafe class Program
FeatureLevel.Level_11_0
};
using ComPtr<ID3D11Device> d3dDevice = default;
CreateDeviceFlag creationFlags = CreateDeviceFlag.BgraSupport;
#if DEBUG
if (SdkLayersAvailable())
{
// If the project is in a debug build, enable debugging via SDK Layers with this flag.
creationFlags |= CreateDeviceFlag.Debug;
}
#endif
using ComPtr<ID3D11Device> tempDevice = default;
FeatureLevel featureLevel;
using ComPtr<ID3D11DeviceContext> immediateContext = default;
D3D11CreateDevice(
(IDXGIAdapter*)adapter.Get(),
DriverType.Hardware,
CreateDeviceFlag.None,
DriverType.Unknown,
creationFlags,
featureLevels,
d3dDevice.GetAddressOf(),
tempDevice.GetAddressOf(),
&featureLevel,
immediateContext.GetAddressOf()).ThrowIfFailed();
#if DEBUG
using ComPtr<ID3D11Debug> d3dDebug = default;
if (tempDevice.CopyTo(&d3dDebug).Success)
{
using ComPtr<ID3D11InfoQueue> d3dInfoQueue = default;
if (d3dDebug.CopyTo(&d3dInfoQueue).Success)
{
d3dInfoQueue.Get()->SetBreakOnSeverity(MessageSeverity.Corruption, true);
d3dInfoQueue.Get()->SetBreakOnSeverity(MessageSeverity.Error, true);
MessageId* hide = stackalloc MessageId[1]
{
MessageId.SetprivatedataChangingparams,
};
InfoQueueFilter filter = new();
filter.DenyList.NumIDs = 1u;
filter.DenyList.pIDList = hide;
d3dInfoQueue.Get()->AddStorageFilterEntries(&filter);
}
}
#endif
}
}