using System.Diagnostics; namespace Brutal.VulkanApi.Abstractions; public static class DebugNameExtensions { private const int MAX_DEBUG_NAME_LENGTH = 256; /// /// Set a debug name for the handle. /// /// Handle to the device that will assign the name /// The Vulkan handle we want to assign a name to /// The name to assign to the handle [Conditional("DEBUG")] public static void SetDebugName(this Device device, T handle, string name) where T : IVulkanHandle { if (!device.PhysicalDevice.Instance.IsExtensionEnabled(VK.EXT_DEBUG_UTILS_EXTENSION_NAME)) return; var bytes = SpanOwner.Allocate(MAX_DEBUG_NAME_LENGTH); var written = Utf8.GetBytes(name, bytes.Span); bytes.Span[written] = 0; device.SetDebugName(handle.VkObjectType, handle.VkHandle, bytes.Span[..written]); } /// /// Set a debug name for the handle. /// /// Handle to the device that will assign the name /// The Vulkan handle we want to assign a name to /// The name to assign to the handle [Conditional("DEBUG")] public static void SetDebugName(this Device device, T handle, ReadOnlySpan name) where T : IVulkanHandle { if (!device.PhysicalDevice.Instance.IsExtensionEnabled(VK.EXT_DEBUG_UTILS_EXTENSION_NAME)) return; device.SetDebugName(handle.VkObjectType, handle.VkHandle, name); } private static unsafe void SetDebugName( this Device device, VkObjectType objectType, nint handle, ReadOnlySpan name ) { fixed (byte* namePtr = name) device.SetDebugUtilsObjectNameEXT( new VkDebugUtilsObjectNameInfoEXT { ObjectType = objectType, ObjectHandle = handle, ObjectName = namePtr, } ); } }