public Device Device => pool?.Dev; //this help //public States State { get; internal set; } internal CommandBuffer(VkDevice _dev, CommandPool _pool, VkCommandBuffer _buff) { pool = _pool; handle = _buff; //State = States.Init; }
public override void TransitionToFinalLayout(VkCommandBuffer cb) { foreach (FramebufferAttachment ca in ColorTargets) { VkTexture vkTex = Util.AssertSubtype <Texture, VkTexture>(ca.Target); vkTex.SetImageLayout(ca.MipLevel, ca.ArrayLayer, VkImageLayout.ColorAttachmentOptimal); if ((vkTex.Usage & TextureUsage.Sampled) != 0) { vkTex.TransitionImageLayout( cb, ca.MipLevel, 1, ca.ArrayLayer, 1, VkImageLayout.ShaderReadOnlyOptimal); } } if (DepthTarget != null) { VkTexture vkTex = Util.AssertSubtype <Texture, VkTexture>(DepthTarget.Value.Target); vkTex.SetImageLayout( DepthTarget.Value.MipLevel, DepthTarget.Value.ArrayLayer, VkImageLayout.DepthStencilAttachmentOptimal); if ((vkTex.Usage & TextureUsage.Sampled) != 0) { vkTex.TransitionImageLayout( cb, DepthTarget.Value.MipLevel, 1, DepthTarget.Value.ArrayLayer, 1, VkImageLayout.ShaderReadOnlyOptimal); } } }
public override void Begin() { if (_commandBufferBegun) { throw new VeldridException( "CommandList must be in its initial state, or End() must have been called, for Begin() to be valid to call."); } if (_commandBufferEnded) { _commandBufferEnded = false; _cb = GetNextCommandBuffer(); if (_currentStagingInfo != null) { RecycleStagingInfo(_currentStagingInfo); } } _currentStagingInfo = GetStagingResourceInfo(); VkCommandBufferBeginInfo beginInfo = VkCommandBufferBeginInfo.New(); beginInfo.flags = VkCommandBufferUsageFlags.OneTimeSubmit; vkBeginCommandBuffer(_cb, ref beginInfo); _commandBufferBegun = true; ClearCachedState(); _currentFramebuffer = null; _currentGraphicsPipeline = null; Util.ClearArray(_currentGraphicsResourceSets); Util.ClearArray(_scissorRects); _currentComputePipeline = null; Util.ClearArray(_currentComputeResourceSets); }
public static void vkCmdSetScissor(VkCommandBuffer commandBuffer, uint firstScissor, ReadOnlySpan <VkRect2D> scissors) { fixed(VkRect2D *scissorsPtr = scissors) { vkCmdSetScissor(commandBuffer, firstScissor, (uint)scissors.Length, scissorsPtr); } }
public static void vkCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits srcStageMask, VkPipelineStageFlagBits dstStageMask, int dependencyFlags, int memoryBarrierCount, VkMemoryBarrier[] pMemoryBarriers, int bufferMemoryBarrierCount, VkBufferMemoryBarrier[] pBufferMemoryBarriers, int imageMemoryBarrierCount, VkImageMemoryBarrier[] pImageMemoryBarriers) { bool hasBarrier = false; VkPreconditions.CheckNull(commandBuffer, nameof(commandBuffer)); if (memoryBarrierCount != 0) { hasBarrier = true; VkPreconditions.CheckNull(pMemoryBarriers, nameof(pMemoryBarriers)); VkPreconditions.CheckRange(memoryBarrierCount, 1, pMemoryBarriers.Length, nameof(memoryBarrierCount)); } if (bufferMemoryBarrierCount != 0) { hasBarrier = true; VkPreconditions.CheckNull(pBufferMemoryBarriers, nameof(pBufferMemoryBarriers)); VkPreconditions.CheckRange(bufferMemoryBarrierCount, 1, pBufferMemoryBarriers.Length, nameof(bufferMemoryBarrierCount)); } if (imageMemoryBarrierCount != 0) { hasBarrier = true; VkPreconditions.CheckNull(pImageMemoryBarriers, nameof(pImageMemoryBarriers)); VkPreconditions.CheckRange(imageMemoryBarrierCount, 1, pImageMemoryBarriers.Length, nameof(imageMemoryBarrierCount)); } VkPreconditions.CheckOperation(!hasBarrier, ("At least one type of barrier must be informed on the command")); GetCommandBuffer(commandBuffer).CmdPipelineBarrier(srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers); }
/** * Finish command buffer recording and submit it to a queue * * @param commandBuffer Command buffer to flush * @param queue Queue to submit the command buffer to * @param free (Optional) Free the command buffer once it has been submitted (Defaults to true) * * @note The queue that the command buffer is submitted to must be from the same family index as the pool it was allocated from * @note Uses a fence to ensure command buffer has finished executing */ public void flushCommandBuffer(VkCommandBuffer commandBuffer, VkQueue queue, bool free = true) { if (commandBuffer.handle == 0) { return; } vkEndCommandBuffer(commandBuffer); VkSubmitInfo submitInfo = new VkSubmitInfo(); submitInfo.sType = SubmitInfo; submitInfo.commandBuffers = commandBuffer; // Create fence to ensure that the command buffer has finished executing VkFenceCreateInfo fenceInfo = new VkFenceCreateInfo(); fenceInfo.sType = FenceCreateInfo; fenceInfo.flags = 0; VkFence fence; vkCreateFence(_logicalDevice, &fenceInfo, null, &fence); // Submit to the queue vkQueueSubmit(queue, 1, &submitInfo, fence); // Wait for the fence to signal that command buffer has finished executing vkWaitForFences(_logicalDevice, 1, &fence, true, DEFAULT_FENCE_TIMEOUT); vkDestroyFence(_logicalDevice, fence, null); if (free) { vkFreeCommandBuffers(_logicalDevice, CommandPool, 1, &commandBuffer); } }
protected void CopyImage(VkImage srcImage, uint srcMipLevel, uint srcArrayLayer, VkImage dstImage, uint dstMipLevel, uint dstArrayLayer, uint width, uint height) { VkImageSubresourceLayers srcSubresource = new VkImageSubresourceLayers(); srcSubresource.mipLevel = srcMipLevel; srcSubresource.aspectMask = VkImageAspectFlags.Color; srcSubresource.baseArrayLayer = srcArrayLayer; srcSubresource.layerCount = 1; VkImageSubresourceLayers dstSubresource = new VkImageSubresourceLayers(); dstSubresource.mipLevel = dstMipLevel; dstSubresource.aspectMask = VkImageAspectFlags.Color; dstSubresource.baseArrayLayer = dstArrayLayer; dstSubresource.layerCount = 1; VkImageCopy region = new VkImageCopy(); region.dstSubresource = dstSubresource; region.srcSubresource = srcSubresource; region.extent.width = width; region.extent.height = height; region.extent.depth = 1; VkCommandBuffer copyCmd = BeginOneTimeCommands(); vkCmdCopyImage(copyCmd, srcImage, VkImageLayout.TransferSrcOptimal, dstImage, VkImageLayout.TransferDstOptimal, 1, ref region); EndOneTimeCommands(copyCmd); }
/// <summary> /// To copy data between buffer objects, call: /// </summary> /// <param name="src">is the source buffer.</param> /// <param name="dest">is the destination buffer.</param> /// <param name="region">is a pointer to an array of <see cref="VkBufferCopy"/> structures specifying the regions to copy.</param> public void CopyBuffer(Buffer src, Buffer dest, VkBufferCopy region) { unsafe { VkCommandBuffer.vkCmdCopyBuffer(Handle, src.Handle, dest.Handle, 1, ®ion); } }
private void OnDraw(VkCommandBuffer commandBuffer, VkFramebuffer framebuffer, Size size) { float g = _green + 0.001f; if (g > 1.0f) { g = 0.0f; } _green = g; VkClearValue clearValue = new VkClearValue(1.0f, _green, 0.0f, 1.0f); // Begin the render pass. VkRenderPassBeginInfo renderPassBeginInfo = new VkRenderPassBeginInfo { sType = VkStructureType.RenderPassBeginInfo, renderPass = _graphicsDevice !.Swapchain.RenderPass, framebuffer = framebuffer, renderArea = new Rectangle(size), clearValueCount = 1, pClearValues = &clearValue }; vkCmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, VkSubpassContents.Inline); vkCmdSetBlendConstants(commandBuffer, new Color4(1.0f, 1.0f, 1.0f, 1.0f)); vkCmdEndRenderPass(commandBuffer); } }
public CommandBuffer(VkCommandBuffer cmd, VkCommandBufferLevel level, CommandPool src, bool transient) { Cmd = cmd; Level = level; SourcePool = src; Transient = transient; }
/// <summary> /// Binds an index buffer to this command buffer /// </summary> /// <param name="buffer">buffer to bind</param> /// <param name="offset">offset into buffer</param> /// <returns>this</returns> public void BindIndexBuffer(IBindableBuffer buffer, VkIndexType type, ulong offset = 0) { var bh = buffer.BindingHandle.Handle; offset += buffer.Offset; VkCommandBuffer.vkCmdBindIndexBuffer(Handle, bh, offset, type); }
/** * Finish command buffer recording and submit it to a queue * * @param commandBuffer Command buffer to flush * @param queue Queue to submit the command buffer to * @param free (Optional) Free the command buffer once it has been submitted (Defaults to true) * * @note The queue that the command buffer is submitted to must be from the same family index as the pool it was allocated from * @note Uses a fence to ensure command buffer has finished executing */ public void flushCommandBuffer(VkCommandBuffer commandBuffer, VkQueue queue, bool free = true) { if (commandBuffer.Handle == NullHandle) { return; } Util.CheckResult(vkEndCommandBuffer(commandBuffer)); VkSubmitInfo submitInfo = VkSubmitInfo.New(); submitInfo.commandBufferCount = 1; submitInfo.pCommandBuffers = &commandBuffer; // Create fence to ensure that the command buffer has finished executing VkFenceCreateInfo fenceInfo = VkFenceCreateInfo.New(); fenceInfo.flags = VkFenceCreateFlags.None; VkFence fence; Util.CheckResult(vkCreateFence(_logicalDevice, &fenceInfo, null, &fence)); // Submit to the queue Util.CheckResult(vkQueueSubmit(queue, 1, &submitInfo, fence)); // Wait for the fence to signal that command buffer has finished executing Util.CheckResult(vkWaitForFences(_logicalDevice, 1, &fence, True, DEFAULT_FENCE_TIMEOUT)); vkDestroyFence(_logicalDevice, fence, null); if (free) { vkFreeCommandBuffers(_logicalDevice, CommandPool, 1, &commandBuffer); } }
public void Submit(VkSemaphore waitSemaphore, VkPipelineStageFlags waitDstStageMask, VkCommandBuffer commandBuffer, VkSemaphore signalSemaphore, VkFence fence = default) { VkCommandBuffer commandBufferHandle = commandBuffer; var nativeSubmit = new VkSubmitInfo { sType = VkStructureType.SubmitInfo }; if (waitSemaphore != default) { nativeSubmit.waitSemaphoreCount = 1; nativeSubmit.pWaitSemaphores = &waitSemaphore; nativeSubmit.pWaitDstStageMask = &waitDstStageMask; } if (commandBuffer != null) { nativeSubmit.commandBufferCount = 1; nativeSubmit.pCommandBuffers = &commandBufferHandle; } if (signalSemaphore != default) { nativeSubmit.signalSemaphoreCount = 1; nativeSubmit.pSignalSemaphores = &signalSemaphore; } VulkanUtil.CheckResult(vkQueueSubmit(this, 1, &nativeSubmit, fence)); }
/// <summary> /// Execute secondary command buffers. /// </summary> public void Execute(params SecondaryCommandBuffer[] secondaryCmds) { if (secondaryCmds.Length == 1) { VkCommandBuffer hnd = secondaryCmds[0].Handle; vkCmdExecuteCommands(handle, 1, ref hnd); return; } int sizeElt = Marshal.SizeOf <IntPtr> (); IntPtr cmdsPtr = Marshal.AllocHGlobal(secondaryCmds.Length * sizeElt); int count = 0; for (int i = 0; i < secondaryCmds.Length; i++) { if (secondaryCmds[i] == null) { continue; } Marshal.WriteIntPtr(cmdsPtr + count * sizeElt, secondaryCmds[i].Handle.Handle); count++; } if (count > 0) { vkCmdExecuteCommands(handle, (uint)count, cmdsPtr); } Marshal.FreeHGlobal(cmdsPtr); }
public static extern void CmdFillBuffer( VkCommandBuffer commandBuffer, VkBuffer dstBuffer, ulong dstOffset, ulong size, uint data );
public static void vkCmdBindDescriptorSets(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint, VkPipelineLayout layout, uint firstSet, ReadOnlySpan <VkDescriptorSet> descriptorSets) { fixed(VkDescriptorSet *descriptorSetsPtr = descriptorSets) { vkCmdBindDescriptorSets(commandBuffer, pipelineBindPoint, layout, firstSet, (uint)descriptorSets.Length, descriptorSetsPtr, 0, null); } }
public static extern void CmdClearAttachments( VkCommandBuffer commandBuffer, uint attachmentCount, IntPtr pAttachments, uint rectCount, IntPtr pRects );
public static void vkCmdExecuteCommands(VkCommandBuffer commandBuffer, ReadOnlySpan <VkCommandBuffer> secondaryCommandBuffers) { fixed(VkCommandBuffer *commandBuffersPtr = secondaryCommandBuffers) { vkCmdExecuteCommands(commandBuffer, (uint)secondaryCommandBuffers.Length, commandBuffersPtr); } }
protected void TransitionImageLayout(VkImage image, uint mipLevels, uint baseArrayLayer, uint layerCount, VkImageLayout oldLayout, VkImageLayout newLayout) { VkCommandBuffer cb = BeginOneTimeCommands(); VkImageMemoryBarrier barrier = VkImageMemoryBarrier.New(); barrier.oldLayout = oldLayout; barrier.newLayout = newLayout; barrier.srcQueueFamilyIndex = QueueFamilyIgnored; barrier.dstQueueFamilyIndex = QueueFamilyIgnored; barrier.image = image; barrier.subresourceRange.aspectMask = VkImageAspectFlags.Color; barrier.subresourceRange.baseMipLevel = 0; barrier.subresourceRange.levelCount = mipLevels; barrier.subresourceRange.baseArrayLayer = baseArrayLayer; barrier.subresourceRange.layerCount = layerCount; vkCmdPipelineBarrier( cb, VkPipelineStageFlags.TopOfPipe, VkPipelineStageFlags.TopOfPipe, VkDependencyFlags.None, 0, null, 0, null, 1, &barrier); EndOneTimeCommands(cb); }
public static extern void CmdBindVertexBuffers( VkCommandBuffer commandBuffer, uint firstBinding, uint bindingCount, IntPtr pBuffers, ref ulong pOffsets );
public static void vkCmdSetViewport(VkCommandBuffer commandBuffer, uint firstViewport, ReadOnlySpan <VkViewport> viewports) { fixed(VkViewport *viewportsPtr = viewports) { vkCmdSetViewport(commandBuffer, firstViewport, (uint)viewports.Length, viewportsPtr); } }
public static extern void CmdDraw( VkCommandBuffer commandBuffer, uint vertexCount, uint instanceCount, uint firstVertex, uint firstInstance );
public static void vkCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint, VkPipeline pipeline) { VkPreconditions.CheckNull(commandBuffer, nameof(commandBuffer)); VkPreconditions.CheckNull(pipeline, nameof(pipeline)); GetCommandBuffer(commandBuffer).CmdBindPipeline(pipelineBindPoint, pipeline); }
public static extern void CmdDrawIndexedIndirect( VkCommandBuffer commandBuffer, VkBuffer buffer, ulong offset, uint drawCount, uint stride );
public void FreeCommandBuffers(params CommandBuffer[] cmds) { if (cmds.Length == 1) { VkCommandBuffer hnd = cmds[0].Handle; vkFreeCommandBuffers(Dev.VkDev, handle, 1, ref hnd); return; } int sizeElt = Marshal.SizeOf <IntPtr> (); IntPtr cmdsPtr = Marshal.AllocHGlobal(cmds.Length * sizeElt); int count = 0; for (int i = 0; i < cmds.Length; i++) { if (cmds[i] == null) { continue; } Marshal.WriteIntPtr(cmdsPtr + count * sizeElt, cmds[i].Handle.Handle); count++; } if (count > 0) { vkFreeCommandBuffers(Dev.VkDev, handle, (uint)count, cmdsPtr); } Marshal.FreeHGlobal(cmdsPtr); }
public static extern void CmdCopyBuffer( VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer, uint regionCount, IntPtr pRegions );
public void CommandBufferCompleted(VkCommandBuffer completedCB) { SubmittedCommandBufferCount -= 1; lock (_commandBufferListLock) { for (int i = 0; i < _submittedCommandBuffers.Count; i++) { VkCommandBuffer submittedCB = _submittedCommandBuffers[i]; if (submittedCB == completedCB) { _availableCommandBuffers.Enqueue(completedCB); _submittedCommandBuffers.RemoveAt(i); i -= 1; } } } lock (_stagingLock) { if (_submittedStagingInfos.TryGetValue(completedCB, out StagingResourceInfo info)) { RecycleStagingInfo(info); } _submittedStagingInfos.Remove(completedCB); } }
public static extern void CmdUpdateBuffer( VkCommandBuffer commandBuffer, VkBuffer dstBuffer, ulong dstOffset, ulong dataSize, IntPtr pData );
internal void TransitionImageLayout( VkCommandBuffer cb, uint baseMipLevel, uint levelCount, uint baseArrayLayer, uint layerCount, VkImageLayout newLayout) { if (_stagingBuffer != Vulkan.VkBuffer.Null) { return; } VkImageLayout oldLayout = _imageLayouts[CalculateSubresource(baseMipLevel, baseArrayLayer)]; #if DEBUG for (uint level = 0; level < levelCount; level++) { for (uint layer = 0; layer < layerCount; layer++) { if (_imageLayouts[CalculateSubresource(baseMipLevel + level, baseArrayLayer + layer)] != oldLayout) { throw new VeldridException("Unexpected image layout."); } } } #endif if (oldLayout != newLayout) { VkImageAspectFlags aspectMask; if ((Usage & TextureUsage.DepthStencil) != 0) { aspectMask = FormatHelpers.IsStencilFormat(Format) ? aspectMask = VkImageAspectFlags.Depth | VkImageAspectFlags.Stencil : aspectMask = VkImageAspectFlags.Depth; } else { aspectMask = VkImageAspectFlags.Color; } VulkanUtil.TransitionImageLayout( cb, OptimalDeviceImage, baseMipLevel, levelCount, baseArrayLayer, layerCount, aspectMask, _imageLayouts[CalculateSubresource(baseMipLevel, baseArrayLayer)], newLayout); for (uint level = 0; level < levelCount; level++) { for (uint layer = 0; layer < layerCount; layer++) { _imageLayouts[CalculateSubresource(baseMipLevel + level, baseArrayLayer + layer)] = newLayout; } } } }
public void Recreate() { // TODO: Reuse the same VkCommandPool only for transfer, compute and graphics use an independent one per thread switch (Type) { case CommandBufferType.Generic: cmd_command_pool = NativeDevice.create_command_pool(NativeDevice.GraphicsFamily); break; case CommandBufferType.AsyncGraphics: cmd_command_pool = NativeDevice.create_command_pool(NativeDevice.GraphicsFamily); break; case CommandBufferType.AsyncCompute: cmd_command_pool = NativeDevice.create_command_pool(NativeDevice.ComputeFamily); break; case CommandBufferType.AsyncTransfer: cmd_command_pool = NativeDevice.transfer_cmd_pool; break; case CommandBufferType.Count: cmd_command_pool = NativeDevice.create_command_pool(NativeDevice.GraphicsFamily); break; } handle = NativeDevice.create_command_buffer_primary(cmd_command_pool); }