namespace Brutal.SdlApi; public static unsafe partial class Sdl { public static unsafe partial class PInvoke { // SDL_vulkan.h is not part of the umbrella header the generator's shim consumes // (Brutal.Sdl.Generate/sdl3_all.h only `#include `, which does not itself pull // in SDL_vulkan.h - unlike SDL_render.h, which is why SDL_AddVulkanRenderSemaphores exists in // the generated surface but nothing else Vulkan-related does). Rather than widen the // generator's input header - which would also mint brand-new generated VkInstance/ // VkPhysicalDevice/VkSurfaceKHR handle types with no relationship to // RocketWerkz.Brutal.Vulkan's own types of the same name, the whole point of the cross-package // sample - the handful of entry points VulkanSurfaceSample actually needs are bound by hand // here, following the same manual-gap pattern as SDL_LogMessage in Sdl.PInvoke.cs. // // Vulkan's opaque handles (VkInstance, VkSurfaceKHR) are spelled as `nint` rather than // referencing Brutal.Vulkan's own VkInstance/VkSurfaceKHR types: this package takes no // dependency on Vulkan, and both handles are pointer-sized opaque values on every platform // this project targets (Windows x64, Linux x64, macOS arm64 - all 64-bit), so `nint` // round-trips losslessly through Brutal.Vulkan's own `Instance.VkHandle` (an `IntPtr`). [LibraryImport(Dll, EntryPoint = "SDL_Vulkan_GetInstanceExtensions")] private static partial byte** _Vulkan_GetInstanceExtensions(uint* count); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static byte** Vulkan_GetInstanceExtensions(uint* count) { Sdl.TraceLog.Trace(); return _Vulkan_GetInstanceExtensions(count); } [LibraryImport(Dll, EntryPoint = "SDL_Vulkan_CreateSurface")] private static partial byte _Vulkan_CreateSurface( SdlWindow window, nint instance, nint allocator, nint* surface ); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static byte Vulkan_CreateSurface(SdlWindow window, nint instance, nint allocator, nint* surface) { Sdl.TraceLog.Trace(); return _Vulkan_CreateSurface(window, instance, allocator, surface); } [LibraryImport(Dll, EntryPoint = "SDL_Vulkan_DestroySurface")] private static partial void _Vulkan_DestroySurface(nint instance, nint surface, nint allocator); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Vulkan_DestroySurface(nint instance, nint surface, nint allocator) { Sdl.TraceLog.Trace(); _Vulkan_DestroySurface(instance, surface, allocator); } } /// /// Ergonomic wrappers over SDL's Vulkan support functions (SDL_Vulkan_GetInstanceExtensions, /// SDL_Vulkan_CreateSurface, SDL_Vulkan_DestroySurface). See /// VulkanSurfaceSample (in the samples project) for a full cross-package usage example /// pairing this with RocketWerkz.Brutal.Vulkan. /// /// /// Only the three entry points needed to create and tear down a VkSurfaceKHR for an /// are bound (see for why the rest of /// SDL_vulkan.h is not). SDL_Vulkan_LoadLibrary is not called explicitly: creating a /// window with loads the Vulkan library as a side effect. /// public static unsafe class Vulkan { /// /// Ergonomic wrapper over SDL_Vulkan_GetInstanceExtensions. Returns the instance /// extension names SDL requires for vkCreateInstance to later succeed in creating a /// surface for a Vulkan-flagged window. /// /// The SDL call failed; the message carries the /// SDL_GetError text. public static string[] GetInstanceExtensions() { uint count; var ptr = PInvoke.Vulkan_GetInstanceExtensions(&count); if (ptr is null) throw SdlException.FromError("SDL_Vulkan_GetInstanceExtensions failed"); // Owned by SDL - must not be freed here (see SDL_vulkan.h's doc comment). var result = new string[count]; for (var i = 0; i < count; i++) result[i] = Marshal.PtrToStringUTF8((nint)ptr[i])!; return result; } /// /// Ergonomic wrapper over SDL_Vulkan_CreateSurface. must /// have been created with the extensions returned enabled, /// and must have been created with /// . /// /// The Vulkan-flagged window to attach the surface to. /// The VkInstance handle, as a raw pointer-sized value /// (e.g. Instance.VkHandle from RocketWerkz.Brutal.Vulkan). /// The newly created VkSurfaceKHR, as a raw pointer-sized value. /// The SDL call failed; the message carries the /// SDL_GetError text. public static nint CreateSurface(SdlWindow window, nint instance) { nint surface; if (PInvoke.Vulkan_CreateSurface(window, instance, 0, &surface) == 0) throw SdlException.FromError("SDL_Vulkan_CreateSurface failed"); return surface; } /// /// Ergonomic wrapper over SDL_Vulkan_DestroySurface. Must be called before the /// is destroyed, and before the window is destroyed if the /// surface was created after the window. /// /// The VkInstance the surface was created against. /// The VkSurfaceKHR to destroy. public static void DestroySurface(nint instance, nint surface) => PInvoke.Vulkan_DestroySurface(instance, surface, 0); } }