namespace Brutal.VulkanApi.Abstractions; /// /// 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); } /// /// 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 ); } } } }