add bindings

This commit is contained in:
2026-06-08 16:15:11 +02:00
commit c1d7ca709d
13 changed files with 59885 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
build
recovery
BeefSpace_User.toml

72
BeefProj.toml Normal file
View File

@@ -0,0 +1,72 @@
FileVersion = 1
[Project]
Name = "VulkanMemoryAllocator"
TargetType = "BeefLib"
StartupObject = "VulkanMemoryAllocator.Program"
[Dependencies]
corlib = "*"
corlib = "*"
"CxxBuildTool.git" = {Git = "https://git.unicon-gmbh.de/Rune/CxxBuildTool.git"}
"Vulkan-Beef.git" = {Git = "https://git.unicon-gmbh.de/Rune/Vulkan-Beef.git"}
[Configs.Debug.Win32]
LibPaths = ["$(BuildDir)/vk_mem_alloc.lib"]
[Configs.Debug.Win64]
LibPaths = ["$(BuildDir)/vk_mem_alloc.lib"]
[Configs.Debug.Linux32]
LibPaths = ["$(BuildDir)/vk_mem_alloc.a"]
[Configs.Debug.Linux64]
LibPaths = ["$(BuildDir)/vk_mem_alloc.a"]
[Configs.Debug.macOS]
LibPaths = ["$(BuildDir)/vk_mem_alloc.a"]
[Configs.Release.Win32]
LibPaths = ["$(BuildDir)/vk_mem_alloc.lib"]
[Configs.Release.Win64]
LibPaths = ["$(BuildDir)/vk_mem_alloc.lib"]
[Configs.Release.Linux32]
LibPaths = ["$(BuildDir)/vk_mem_alloc.a"]
[Configs.Release.Linux64]
LibPaths = ["$(BuildDir)/vk_mem_alloc.a"]
[Configs.Release.macOS]
LibPaths = ["$(BuildDir)/vk_mem_alloc.a"]
[Configs.Paranoid.Win32]
LibPaths = ["$(BuildDir)/vk_mem_alloc.lib"]
[Configs.Paranoid.Win64]
LibPaths = ["$(BuildDir)/vk_mem_alloc.lib"]
[Configs.Paranoid.Linux32]
LibPaths = ["$(BuildDir)/vk_mem_alloc.a"]
[Configs.Paranoid.Linux64]
LibPaths = ["$(BuildDir)/vk_mem_alloc.a"]
[Configs.Paranoid.macOS]
LibPaths = ["$(BuildDir)/vk_mem_alloc.a"]
[Configs.Test.Win32]
LibPaths = ["$(BuildDir)/vk_mem_alloc.lib"]
[Configs.Test.Win64]
LibPaths = ["$(BuildDir)/vk_mem_alloc.lib"]
[Configs.Test.Linux32]
LibPaths = ["$(BuildDir)/vk_mem_alloc.a"]
[Configs.Test.Linux64]
LibPaths = ["$(BuildDir)/vk_mem_alloc.a"]
[Configs.Test.macOS]
LibPaths = ["$(BuildDir)/vk_mem_alloc.a"]

10
BeefSpace.toml Normal file
View File

@@ -0,0 +1,10 @@
FileVersion = 1
ExtraPlatforms = ["Linux32", "Linux64", "macOS"]
[Workspace]
StartupProject = "VulkanMemoryAllocator"
[Projects]
VulkanMemoryAllocator = {Path = "."}
"CxxBuildTool.git" = {Git = "https://git.unicon-gmbh.de/Rune/CxxBuildTool.git"}
"Vulkan-Beef.git" = {Git = "https://git.unicon-gmbh.de/Rune/Vulkan-Beef.git"}

11
BeefSpace_Lock.toml Normal file
View File

@@ -0,0 +1,11 @@
FileVersion = 1
[Locks."CxxBuildTool.git".Git]
URL = "https://git.unicon-gmbh.de/Rune/CxxBuildTool.git"
Tag = ""
Hash = "b64a6221b8cb235a5e040c8597365dabeef849ca"
[Locks."Vulkan-Beef.git".Git]
URL = "https://git.unicon-gmbh.de/Rune/Vulkan-Beef.git"
Tag = ""
Hash = "60bb497ae6fb8ef2db7c08251633f65c254aa250"

9
Setup/BeefProj.toml Normal file
View 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
View 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
View 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
View 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;
}
}

19745
src/Functions.bf Normal file

File diff suppressed because it is too large Load Diff

26
src/Library.bf Normal file
View File

@@ -0,0 +1,26 @@
using System;
using CxxBuildTool;
namespace VulkanMemoryAllocator;
static
{
[CLink, AlwaysInclude, Export]
private static void __VMA_BF_FATAL_ERROR(char8* msg, char8* file, int32 line)
{
Runtime.FatalError(scope .(msg), scope .(file), line);
}
[OnCompile(.TypeDone)]
private static void Build()
{
if (!Compiler.IsBuilding) return;
String flags = scope .(128);
if (Environment.GetEnvironmentVariable("VULKAN_SDK", flags) case .Ok)
{
flags.Insert(0, "-I");
flags.Append("/include");
}
CxxBuildTool.Ninja(Compiler.ProjectDir + "/src", Compiler.BuildDir + "/" + Compiler.ProjectName, "vk_mem_alloc", flags, "vk_mem_alloc.cpp");
}
}

19739
src/VkMemAlloc.bf Normal file

File diff suppressed because it is too large Load Diff

9
src/vk_mem_alloc.cpp Normal file
View File

@@ -0,0 +1,9 @@
extern "C" void
__VMA_BF_FATAL_ERROR(const char* message, const char* file, int line);
#define VMA_ASSERT(expr) \
if (!(expr)) \
__VMA_BF_FATAL_ERROR("VMA Assert Failed: " #expr, __FILE__, __LINE__);
#define VMA_IMPLEMENTATION
#define VK_NO_PROTOTYPES
#include "vk_mem_alloc.h"

19975
src/vk_mem_alloc.h Normal file

File diff suppressed because it is too large Load Diff