add bindings
This commit is contained in:
9
Setup/BeefProj.toml
Normal file
9
Setup/BeefProj.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
FileVersion = 1
|
||||
|
||||
[Project]
|
||||
Name = "VulkanMemoryAllocator.Setup"
|
||||
StartupObject = "VulkanMemoryAllocator.Setup.Program"
|
||||
|
||||
[Dependencies]
|
||||
corlib = "*"
|
||||
"Cpp2Beef.git" = {Git = "https://git.unicon-gmbh.de/Rune/Cpp2Beef.git"}
|
||||
8
Setup/BeefSpace.toml
Normal file
8
Setup/BeefSpace.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
FileVersion = 1
|
||||
|
||||
[Workspace]
|
||||
StartupProject = "VulkanMemoryAllocator.Setup"
|
||||
|
||||
[Projects]
|
||||
"VulkanMemoryAllocator.Setup" = {Path = "."}
|
||||
"Cpp2Beef.git" = {Git = "https://git.unicon-gmbh.de/Rune/Cpp2Beef.git"}
|
||||
11
Setup/BeefSpace_Lock.toml
Normal file
11
Setup/BeefSpace_Lock.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
FileVersion = 1
|
||||
|
||||
[Locks."Clang-C.git".Git]
|
||||
URL = "https://git.unicon-gmbh.de/BeefBindings/Clang-C.git"
|
||||
Tag = ""
|
||||
Hash = "86db0167f15d08a63a91a19e46f983a5511bac1a"
|
||||
|
||||
[Locks."Cpp2Beef.git".Git]
|
||||
URL = "https://git.unicon-gmbh.de/Rune/Cpp2Beef.git"
|
||||
Tag = ""
|
||||
Hash = "91ddcd4f79cf6df28adff5abfc59cbdc489c7deb"
|
||||
267
Setup/src/Program.bf
Normal file
267
Setup/src/Program.bf
Normal file
@@ -0,0 +1,267 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections;
|
||||
using System.Diagnostics;
|
||||
|
||||
using Cpp2Beef;
|
||||
using LibClang;
|
||||
|
||||
namespace VulkanMemoryAllocator.Setup;
|
||||
|
||||
class VkMemAllocGenerator : Cpp2BeefGenerator, this(Span<char8*> args)
|
||||
{
|
||||
protected override Span<char8*> Args => args;
|
||||
protected override Flags Flags => .None;
|
||||
|
||||
StreamWriter writer = new .()..Create("../src/VkMemAlloc.bf")..Write("""
|
||||
// This file was generated by Cpp2Beef
|
||||
|
||||
using System;
|
||||
using System.Interop;
|
||||
|
||||
using Vulkan;
|
||||
|
||||
namespace VulkanMemoryAllocator;
|
||||
|
||||
|
||||
""") ~ delete _;
|
||||
|
||||
protected override StreamWriter GetWriterForHeader(StringView header)
|
||||
{
|
||||
if (header.EndsWith("vk_mem_alloc.h"))
|
||||
return writer;
|
||||
return null;
|
||||
}
|
||||
|
||||
protected override Block GetCursorBlock(CXCursor cursor)
|
||||
{
|
||||
/*if (cursor.kind == .FunctionDecl)
|
||||
return .CustomBlock("Vma");*/
|
||||
return base.GetCursorBlock(cursor);
|
||||
}
|
||||
|
||||
protected override void HandleCursor(CXCursor cursor)
|
||||
{
|
||||
switch (cursor.kind)
|
||||
{
|
||||
case .TypedefDecl:
|
||||
let spelling = GetCursorSpelling!(cursor);
|
||||
let type = GetTypeSpelling!(Clang.GetTypedefDeclUnderlyingType(cursor));
|
||||
if (type == "VkFlags")
|
||||
{
|
||||
BeginCursor(cursor);
|
||||
AccessSpecifier(cursor);
|
||||
str.Append("typealias ", spelling, " = ",
|
||||
scope String(spelling)..Replace("Flags", "FlagBits"));
|
||||
str.Append(';');
|
||||
return;
|
||||
}
|
||||
if (type.EndsWith("_T *"))
|
||||
{
|
||||
BeginCursor(cursor);
|
||||
AccessSpecifier(cursor);
|
||||
str.Append("struct ", spelling, " : int { public static operator Self(decltype(null)) => VK_NULL_HANDLE; }");
|
||||
return;
|
||||
}
|
||||
case .StructDecl:
|
||||
if (GetCursorSpelling!(cursor).EndsWith("_T"))
|
||||
return;
|
||||
default:
|
||||
}
|
||||
base.HandleCursor(cursor);
|
||||
}
|
||||
|
||||
protected override void WriteCustomAttributes(CXCursor cursor)
|
||||
{
|
||||
if (cursor.kind == .TypedefDecl &&
|
||||
{
|
||||
let type = Clang.GetTypedefDeclUnderlyingType(cursor);
|
||||
type.kind == .Pointer && Clang.GetPointeeType(type).kind == .FunctionProto
|
||||
})
|
||||
str.Append("[CallingConvention(VKAPI_PTR)]\n");
|
||||
if (cursor.kind == .EnumConstantDecl)
|
||||
{
|
||||
StringView docString = ScopeCXString!(Clang.Cursor_GetRawCommentText(cursor));
|
||||
for (var line in docString.Split('\n'))
|
||||
{
|
||||
if (!line..TrimStart().StartsWith("\\deprecated"))
|
||||
continue;
|
||||
str.Append("[Obsolete] ");
|
||||
break;
|
||||
}
|
||||
}
|
||||
base.WriteCustomAttributes(cursor);
|
||||
}
|
||||
|
||||
protected override void WriteToken(CXToken token)
|
||||
{
|
||||
{
|
||||
let spelling = GetTokenSpelling!(token);
|
||||
if (spelling.StartsWith("VMA_ALLOCATION_CREATE_"))
|
||||
str.Append("VmaAllocationCreateFlags.");
|
||||
}
|
||||
base.WriteToken(token);
|
||||
}
|
||||
|
||||
protected override bool IsOutParam(CXCursor arg, CXCursor method)
|
||||
{
|
||||
let spelling = GetTypeSpelling!(Clang.GetCursorType(arg));
|
||||
if (spelling.EndsWith("**"))
|
||||
return true;
|
||||
if (spelling.EndsWith('*') && !spelling.StartsWith("const"))
|
||||
return true;
|
||||
return base.IsOutParam(arg, method);
|
||||
}
|
||||
|
||||
protected override void GetNameInBindings(LibClang.CXCursor cursor, String outString)
|
||||
{
|
||||
switch (cursor.kind)
|
||||
{
|
||||
case .EnumConstantDecl:
|
||||
StringView pascal = UpperSnakeCase2PascalCase(GetCursorSpelling!(cursor), ..scope .(128));
|
||||
if (pascal.EndsWith("Bit"))
|
||||
pascal.RemoveFromEnd(3);
|
||||
StringView parentSpelling = GetCursorSpelling!(Clang.GetCursorSemanticParent(cursor));
|
||||
if (pascal.EndsWith("MaxEnum"))
|
||||
outString.Append("//");
|
||||
else if (parentSpelling.EndsWith("FlagBits"))
|
||||
parentSpelling.RemoveFromEnd(8);
|
||||
if (pascal.StartsWith(parentSpelling))
|
||||
pascal.RemoveFromStart(parentSpelling.Length);
|
||||
outString.Append(pascal);
|
||||
/*case .FunctionDecl:
|
||||
var spelling = GetCursorSpelling!(cursor);
|
||||
if (spelling.StartsWith("vma"))
|
||||
spelling.RemoveFromStart(3);
|
||||
outString.Append(spelling);*/
|
||||
default:
|
||||
base.GetNameInBindings(cursor, outString);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class VkMemAllocFunctionsGenerator : Cpp2BeefGenerator, this(Span<char8*> args)
|
||||
{
|
||||
protected override Span<char8*> Args => args;
|
||||
protected override Flags Flags => .None;
|
||||
|
||||
StreamWriter writer = new .()..Create("../src/Functions.bf")..Write("""
|
||||
// This file was generated by Cpp2Beef
|
||||
|
||||
using System;
|
||||
using System.Interop;
|
||||
|
||||
using Vulkan;
|
||||
|
||||
namespace VulkanMemoryAllocator;
|
||||
|
||||
|
||||
""") ~ delete _;
|
||||
|
||||
protected override StreamWriter GetWriterForHeader(StringView header)
|
||||
{
|
||||
if (header.EndsWith("vk_mem_alloc.h"))
|
||||
return writer;
|
||||
return null;
|
||||
}
|
||||
|
||||
protected override Block GetCursorBlock(CXCursor cursor)
|
||||
{
|
||||
return .StaticBlock;
|
||||
}
|
||||
|
||||
protected override bool IsOutParam(CXCursor arg, CXCursor method)
|
||||
{
|
||||
let spelling = GetTypeSpelling!(Clang.GetCursorType(arg));
|
||||
if (spelling.EndsWith("**"))
|
||||
return true;
|
||||
if (spelling.EndsWith('*') && !spelling.StartsWith("const"))
|
||||
return true;
|
||||
return base.IsOutParam(arg, method);
|
||||
}
|
||||
|
||||
List<String> handles = new .(8) ~ DeleteContainerAndItems!(_);
|
||||
|
||||
protected override void HandleCursor(CXCursor cursor)
|
||||
{
|
||||
switch (cursor.kind)
|
||||
{
|
||||
case .TypedefDecl:
|
||||
let type = GetTypeSpelling!(Clang.GetTypedefDeclUnderlyingType(cursor));
|
||||
if (type.EndsWith("_T *"))
|
||||
handles.Add(new .(GetCursorSpelling!(cursor)));
|
||||
case .FunctionDecl:
|
||||
StringView handle = null;
|
||||
int handleI = -1;
|
||||
findHandle: for (let i < Clang.Cursor_GetNumArguments(cursor))
|
||||
{
|
||||
let spelling = GetTypeSpelling!(Clang.GetCursorType(Clang.Cursor_GetArgument(cursor, (.)i)));
|
||||
for (let h in handles)
|
||||
if (h == spelling)
|
||||
{
|
||||
handle = h;
|
||||
handleI = i;
|
||||
continue findHandle;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (handle.IsNull) break;
|
||||
|
||||
BeginCursor(cursor);
|
||||
str.Append("public static ");
|
||||
Type(Clang.GetCursorResultType(cursor));
|
||||
str.Append(' ');
|
||||
{
|
||||
String spelling = scope .(GetCursorSpelling!(cursor)[3...]);
|
||||
spelling.Replace(handle[3...], "");
|
||||
str.Append(spelling);
|
||||
}
|
||||
str.Append("(this ");
|
||||
{
|
||||
Flush();
|
||||
Method_Parameters(cursor);
|
||||
String paramsBuffer = scope .(str);
|
||||
str.Clear();
|
||||
let parameters = paramsBuffer.Split!(',');
|
||||
str.Append(parameters[handleI]..Trim());
|
||||
for (var param in parameters)
|
||||
if (@param != handleI)
|
||||
str.Append(", ", param..Trim());
|
||||
}
|
||||
str.Append(") => ", GetCursorSpelling!(cursor), "(");
|
||||
let numArgs = Clang.Cursor_GetNumArguments(cursor);
|
||||
for (let i < numArgs)
|
||||
{
|
||||
if (i > 0) str.Append(", ");
|
||||
let arg = Clang.Cursor_GetArgument(cursor, (.)i);
|
||||
if (IsOutParam(arg, cursor))
|
||||
str.Append("out ");
|
||||
str.Append(GetCursorSpelling!(arg));
|
||||
}
|
||||
str.Append(");");
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Program
|
||||
{
|
||||
[CLink] static extern int32 system(char8*);
|
||||
|
||||
public static int Main(String[] args)
|
||||
{
|
||||
if (system("wget -O ../src/vk_mem_alloc.h https://raw.githubusercontent.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator/refs/heads/master/include/vk_mem_alloc.h") != 0)
|
||||
Runtime.FatalError("Failed to download vk_mem_alloc.h");
|
||||
|
||||
char8*[?] args = .("--language=c",
|
||||
#if BF_PLATFORM_WINDOWS
|
||||
scope $"-I{Environment.GetEnvironmentVariable("VULKAN_SDK", ..scope .())}\\Include",
|
||||
#endif
|
||||
"-DVMA_NULLABLE=", "-DVMA_NOT_NULL=", "-DVMA_NOT_NULL_NON_DISPATCHABLE=", "-DVMA_NULLABLE_NON_DISPATCHABLE=", "-U_WIN32"
|
||||
);
|
||||
scope VkMemAllocGenerator(args).Generate("../src/vk_mem_alloc.h");
|
||||
scope VkMemAllocFunctionsGenerator(args).Generate("../src/vk_mem_alloc.h");
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user