namespace Brutal.VulkanApi.Abstractions;
///
/// Scope handle returned by ; ends the
/// debug label region on dispose.
///
public readonly struct DebugLabelScope(CommandBuffer inCommandBuffer) : IDisposable
{
///
public void Dispose()
{
inCommandBuffer.EndDebugLabel();
}
}
///
/// Provides extension methods for recording Vulkan command buffer commands.
///
public static class CommandBufferExtensions
{
///
/// Resets the command buffer to its initial state using
/// vkResetCommandBuffer.
/// The command buffer must not be in a pending state and will be reset to the initial state.
///
/// The command buffer to reset.
public static void Reset(this CommandBuffer inCommandBuffer)
{
inCommandBuffer.Reset(VkCommandBufferResetFlags.None);
}
///
/// Begins recording commands on the command buffer using
/// vkBeginCommandBuffer.
///
/// The command buffer to begin recording.
public static void Begin(this CommandBuffer inCommandBuffer)
{
inCommandBuffer.Begin(new VkCommandBufferBeginInfo { Flags = VkCommandBufferUsageFlags.None });
}
///
/// Begins a Vulkan debug label region on the command buffer using
/// vkCmdBeginDebugUtilsLabelEXT.
/// This allows grouping commands within a labeled region for better debugging and profiling.
///
/// The Vulkan command buffer on which to begin the debug label.
/// A read-only span of bytes representing the label name. The label should be null-terminated.
/// A structure representing the RGBA color of the label.
public static unsafe void BeginDebugLabel(
this CommandBuffer inCommandBuffer,
in ReadOnlySpan label,
in float4 color
)
{
var vkLabel = new VkDebugUtilsLabelEXT { LabelName = label.AsPointer() };
vkLabel.Color[0] = color.R;
vkLabel.Color[1] = color.G;
vkLabel.Color[2] = color.B;
vkLabel.Color[3] = color.A;
inCommandBuffer.BeginDebugUtilsLabelEXT(vkLabel);
}
///
/// Begins a Vulkan debug label region on the command buffer, encoding the label from a managed string.
/// Labels longer than 255 UTF-8 bytes are truncated at a character boundary. See
/// .
///
/// The Vulkan command buffer on which to begin the debug label.
/// The label text.
/// A structure representing the RGBA color of the label.
public static void BeginDebugLabel(this CommandBuffer inCommandBuffer, string inLabel, in float4 inColor = default)
{
Span bytes = stackalloc byte[MAX_DEBUG_LABEL_LENGTH];
// Encode as much as fits in the 255-byte budget (one byte reserved for the terminator).
// FromUtf16 stops at a character boundary when the destination runs out, so a truncated
// label is still valid UTF-8 rather than ending mid-character.
System.Text.Unicode.Utf8.FromUtf16(
inLabel,
bytes[..(MAX_DEBUG_LABEL_LENGTH - 1)],
out _,
out var written,
replaceInvalidSequences: true,
isFinalBlock: true
);
bytes[written] = 0;
BeginDebugLabel(inCommandBuffer, bytes[..(written + 1)], inColor);
}
private const int MAX_DEBUG_LABEL_LENGTH = 256;
///
/// Begins a debug label region and returns a scope that ends it when disposed:
/// using var _ = cmd.DebugScope("Shadow pass");
///
/// The Vulkan command buffer to label.
/// The label text.
/// A structure representing the RGBA color of the label.
public static DebugLabelScope DebugScope(
this CommandBuffer inCommandBuffer,
string inLabel,
in float4 inColor = default
)
{
BeginDebugLabel(inCommandBuffer, inLabel, inColor);
return new DebugLabelScope(inCommandBuffer);
}
///
/// Ends the current Vulkan debug label region on the command buffer using
/// vkCmdEndDebugUtilsLabelEXT.
/// This marks the end of a previously started debug label region, allowing Vulkan debugging tools to identify command groups.
///
/// The Vulkan command buffer on which to end the debug label.
public static void EndDebugLabel(this CommandBuffer inCommandBuffer)
{
inCommandBuffer.EndDebugUtilsLabelEXT();
}
///
/// Begins recording commands on the command buffer with specified usage flags using
/// vkBeginCommandBuffer.
///
/// The command buffer to begin recording.
/// The usage flags determining how the command buffer will be used.
public static void Begin(this CommandBuffer inCommandBuffer, VkCommandBufferUsageFlags usage)
{
inCommandBuffer.Begin(new VkCommandBufferBeginInfo { Flags = usage });
}
///
/// Begins recording commands on the command buffer with specified usage flags and inheritance information
/// for secondary command buffers, using vkBeginCommandBuffer.
///
/// The command buffer to begin recording.
/// The usage flags for the command buffer.
///
/// For secondary command buffers, a pointer to a VkCommandBufferInheritanceInfo structure providing additional state;
/// ignored for primary command buffers.
///
public static void Begin(
this CommandBuffer inCommandBuffer,
VkCommandBufferUsageFlags usage,
VkCommandBufferInheritanceInfo inheritanceInfo
)
{
unsafe
{
inCommandBuffer.Begin(new VkCommandBufferBeginInfo { Flags = usage, InheritanceInfo = &inheritanceInfo });
}
}
///
/// Records a command to copy data between buffers using
/// vkCmdCopyBuffer.
/// Copies the specified region from the source buffer to the destination buffer.
///
/// The command buffer that will record the copy command.
/// The source buffer.
/// The destination buffer.
/// A VkBufferCopy structure defining the region to copy.
public static void CopyBuffer(
this CommandBuffer inCommandBuffer,
VkBuffer inSrcBuffer,
VkBuffer inDstBuffer,
VkBufferCopy inRegion
)
{
inCommandBuffer.CopyBuffer(inSrcBuffer, inDstBuffer, [inRegion]);
}
///
/// Records a command to copy a specified number of bytes from the source buffer to the destination buffer.
/// This is a convenience overload that assumes zero offsets.
///
/// The command buffer that will record the copy command.
/// The source buffer.
/// The destination buffer.
/// The number of bytes to copy from the start of the buffers.
public static void CopyBuffer(
this CommandBuffer inCommandBuffer,
VkBuffer inSrcBuffer,
VkBuffer inDstBuffer,
ByteSize inSize
)
{
CopyBuffer(
inCommandBuffer,
inSrcBuffer,
inDstBuffer,
new VkBufferCopy
{
Size = inSize,
DstOffset = ByteSize.Zero,
SrcOffset = ByteSize.Zero,
}
);
}
///
/// Binds a vertex buffer to the command buffer at the specified binding index using
/// vkCmdBindVertexBuffers.
///
/// The command buffer to record the binding command.
/// The vertex input binding index to update.
/// The vertex buffer to bind.
/// The offset (in bytes) within the buffer to start binding from. Defaults to 0.
public static void BindVertexBuffer(
this CommandBuffer inCommandBuffer,
int inBinding,
VkBuffer inBuffer,
ByteSize inOffset = default
)
{
inCommandBuffer.BindVertexBuffers(inBinding, [inBuffer], [inOffset]);
}
///
/// Records a command to update a push constant value of type in the command buffer using
/// vkCmdPushConstants.
///
/// The command buffer to record the push constant update.
/// The pipeline layout that defines the push constant ranges.
/// A bitmask of VkShaderStageFlags indicating which shader stages will access the push constant.
/// The byte offset within the push constant range at which to start updating.
/// The push constant value to update.
public static void PushConstants(
this CommandBuffer inCommandBuffer,
VkPipelineLayout inLayout,
VkShaderStageFlags inStageFlags,
ByteSize inOffset,
T inData
)
where T : unmanaged
{
inCommandBuffer.PushConstants(inLayout, inStageFlags, inOffset, [inData]);
}
///
/// Records a command to update a sequence of push constants of type in the command buffer using
/// vkCmdPushConstants.
///
/// The command buffer to record the push constant update.
/// The pipeline layout that defines the push constant ranges.
/// A bitmask of VkShaderStageFlags indicating which shader stages will access the push constants.
/// The byte offset within the push constant range at which to start updating.
/// A span containing the push constant values to update.
public static void PushConstants(
this CommandBuffer inCommandBuffer,
VkPipelineLayout inLayout,
VkShaderStageFlags inStageFlags,
ByteSize inOffset,
ReadOnlySpan inData
)
where T : unmanaged
{
unsafe
{
fixed (T* ptr = inData)
{
inCommandBuffer.PInvoke.vkCmdPushConstants.Invoke(
inCommandBuffer.Handle,
inLayout,
inStageFlags,
(int)inOffset,
(int)ByteSize.Of(inData.Length),
ptr
);
}
}
}
}