namespace Brutal.VulkanApi.Abstractions; /// /// An interface describing a buffer that can be bound to a command buffer. /// public interface IBindBuffer { /// /// The Vulkan buffer handle. /// public VkBuffer VkBuffer { get; } /// /// The offset within the parent buffer where this buffer starts for binding. /// public ByteSize BindOffset { get; } } public static class BindBufferExtensions { /// /// 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. /// Optional additional offset to add to the buffer's bind offset. public static void BindVertexBuffer( this CommandBuffer inCommandBuffer, int inBinding, T inBuffer, ByteSize inAdditionalOffset = default ) where T : IBindBuffer { inCommandBuffer.BindVertexBuffer(inBinding, inBuffer.VkBuffer, inBuffer.BindOffset + inAdditionalOffset); } ///vkCmdBindVertexBuffers - Bind vertex buffers to a command buffer ///is the command buffer into which the command is recorded. ///is the index of the first vertex input binding whose state is updated by the command. ///is a pointer to an array of buffer handles. public static unsafe void BindVertexBuffers( this CommandBuffer inCommandBuffer, int inFirstBinding, Span inBuffers ) where T : IBindBuffer { Span buffers = stackalloc VkBuffer[inBuffers.Length]; Span offsets = stackalloc ByteSize64[inBuffers.Length]; for (var i = 0; i < inBuffers.Length; i++) { buffers[i] = inBuffers[i].VkBuffer; offsets[i] = inBuffers[i].BindOffset; } inCommandBuffer.BindVertexBuffers(inFirstBinding, buffers, offsets); } ///vkCmdBindVertexBuffers - Bind vertex buffers to a command buffer ///is the command buffer into which the command is recorded. ///is the index of the first vertex input binding whose state is updated by the command. ///is a pointer to an array of buffer handles. ///is a pointer to an array of buffer offsets, beyond the buffer's bind offset. public static unsafe void BindVertexBuffers( this CommandBuffer inCommandBuffer, int inFirstBinding, Span inBuffers, Span inAdditionalOffsets ) where T : IBindBuffer { if (inBuffers.Length != inAdditionalOffsets.Length) throw new ArgumentException("inBuffers and inAdditionalOffsets must be the same length"); Span buffers = stackalloc VkBuffer[inBuffers.Length]; Span offsets = stackalloc ByteSize64[inBuffers.Length]; for (var i = 0; i < inBuffers.Length; i++) { buffers[i] = inBuffers[i].VkBuffer; offsets[i] = inBuffers[i].BindOffset + inAdditionalOffsets[i]; } inCommandBuffer.BindVertexBuffers(inFirstBinding, buffers, offsets); } ///vkCmdBindIndexBuffer - Bind an index buffer to a command buffer ///is the command buffer into which the command is recorded. ///is the buffer being bound. ///is a VkIndexType value specifying the size of the indices. public static void BindIndexBuffer(this CommandBuffer inCommandBuffer, T inBuffer, VkIndexType inIndexType) where T : IBindBuffer { inCommandBuffer.BindIndexBuffer(inBuffer.VkBuffer, inBuffer.BindOffset, inIndexType); } /// vkCmdBindIndexBuffer - Bind an index buffer to a command buffer /// is the command buffer into which the command is recorded. /// is the buffer being bound. /// is an additional offset to add to the buffer's bind offset. /// is a VkIndexType value specifying the size of the indices. public static void BindIndexBuffer( this CommandBuffer inCommandBuffer, T inBuffer, ByteSize inAdditionalOffset, VkIndexType inIndexType ) where T : IBindBuffer { inCommandBuffer.BindIndexBuffer(inBuffer.VkBuffer, inBuffer.BindOffset + inAdditionalOffset, inIndexType); } ///vkCmdDrawIndirect - Draw primitives with indirect parameters ///is the command buffer into which the command is recorded. ///is the buffer containing draw parameters. ///is the number of draws to execute, and can be zero. ///is the byte stride between successive sets of draw parameters. public static void DrawIndirect( this CommandBuffer inCommandBuffer, T inBuffer, ElementCount inDrawCount, ByteSize inStride ) where T : IBindBuffer { inCommandBuffer.DrawIndirect(inBuffer.VkBuffer, inBuffer.BindOffset, inDrawCount, inStride); } /// vkCmdDrawIndirect - Draw primitives with indirect parameters /// is the command buffer into which the command is recorded. /// is the buffer containing draw parameters. /// is an additional offset to add to the buffer's bind offset. /// is the number of draws to execute, and can be zero. /// is the byte stride between successive sets of draw parameters. public static void DrawIndirect( this CommandBuffer inCommandBuffer, T inBuffer, ByteSize inAdditionalOffset, ElementCount inDrawCount, ByteSize inStride ) where T : IBindBuffer { inCommandBuffer.DrawIndirect( inBuffer.VkBuffer, inBuffer.BindOffset + inAdditionalOffset, inDrawCount, inStride ); } }