public void SubmitPrePresentBarrier(IMgCommandBuffer prePresent, IMgImage image) { if (prePresent == null) { throw new InvalidOperationException(); } MgCommandBufferBeginInfo cmdBufInfo = new MgCommandBufferBeginInfo { }; var vkRes = prePresent.BeginCommandBuffer(cmdBufInfo); Debug.Assert(vkRes == Result.SUCCESS, vkRes + " != Result.SUCCESS"); const uint VK_QUEUE_FAMILY_IGNORED = ~0U; var prePresentBarrier = new MgImageMemoryBarrier { Image = image, SrcAccessMask = MgAccessFlagBits.COLOR_ATTACHMENT_WRITE_BIT, DstAccessMask = 0, OldLayout = MgImageLayout.COLOR_ATTACHMENT_OPTIMAL, NewLayout = MgImageLayout.PRESENT_SRC_KHR, SrcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, DstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, SubresourceRange = new MgImageSubresourceRange { AspectMask = MgImageAspectFlagBits.COLOR_BIT, BaseArrayLayer = 0, LayerCount = 1, BaseMipLevel = 0, LevelCount = 1, }, }; const int VK_FLAGS_NONE = 0; prePresent.CmdPipelineBarrier( MgPipelineStageFlagBits.ALL_COMMANDS_BIT, MgPipelineStageFlagBits.TOP_OF_PIPE_BIT, VK_FLAGS_NONE, null, // No memory barriers, null, // No buffer barriers, new[] { prePresentBarrier }); vkRes = prePresent.EndCommandBuffer(); Debug.Assert(vkRes == Result.SUCCESS, vkRes + " != Result.SUCCESS"); }
// Create an image memory barrier for changing the layout of // an image and put it into an active command buffer public void SetImageLayout( IMgCommandBuffer cmdBuffer, IMgImage image, MgImageAspectFlagBits aspectMask, MgImageLayout oldImageLayout, MgImageLayout newImageLayout, uint mipLevel, uint mipLevelCount) { // Create an image barrier object var imageMemoryBarrier = new MgImageMemoryBarrier() { OldLayout = oldImageLayout, NewLayout = newImageLayout, Image = image, SubresourceRange = new MgImageSubresourceRange { AspectMask = aspectMask, BaseMipLevel = mipLevel, LevelCount = mipLevelCount, LayerCount = 1, }, }; // Only sets masks for layouts used in this example // For a more complete version that can be used with // other layouts see vkTools::setImageLayout // Source layouts (new) if (oldImageLayout == MgImageLayout.PREINITIALIZED) { imageMemoryBarrier.SrcAccessMask = MgAccessFlagBits.HOST_WRITE_BIT | MgAccessFlagBits.TRANSFER_WRITE_BIT; } // Target layouts (new) // New layout is transfer destination (copy, blit) // Make sure any reads from and writes to the image have been finished if (newImageLayout == MgImageLayout.TRANSFER_DST_OPTIMAL) { imageMemoryBarrier.DstAccessMask = MgAccessFlagBits.TRANSFER_READ_BIT | MgAccessFlagBits.HOST_WRITE_BIT | MgAccessFlagBits.TRANSFER_WRITE_BIT; } // New layout is shader read (sampler, input attachment) // Make sure any writes to the image have been finished if (newImageLayout == MgImageLayout.SHADER_READ_ONLY_OPTIMAL) { imageMemoryBarrier.SrcAccessMask = MgAccessFlagBits.HOST_WRITE_BIT | MgAccessFlagBits.TRANSFER_WRITE_BIT; imageMemoryBarrier.DstAccessMask = MgAccessFlagBits.SHADER_READ_BIT; } // New layout is transfer source (copy, blit) // Make sure any reads from and writes to the image have been finished if (newImageLayout == MgImageLayout.TRANSFER_SRC_OPTIMAL) { imageMemoryBarrier.DstAccessMask = MgAccessFlagBits.TRANSFER_READ_BIT | MgAccessFlagBits.HOST_WRITE_BIT | MgAccessFlagBits.TRANSFER_WRITE_BIT; } // Put barrier on top MgPipelineStageFlagBits srcStageFlags = MgPipelineStageFlagBits.TOP_OF_PIPE_BIT; MgPipelineStageFlagBits destStageFlags = MgPipelineStageFlagBits.TOP_OF_PIPE_BIT; const int VK_FLAGS_NONE = 0; // Put barrier inside setup command buffer cmdBuffer.CmdPipelineBarrier( srcStageFlags, destStageFlags, VK_FLAGS_NONE, null, null, new [] { imageMemoryBarrier }); }
// Create an image memory barrier for changing the layout of // an image and put it into an active command buffer // See chapter 11.4 "Image Layout" for details private void PushImageMemoryBarrier( IMgCommandBuffer cmdbuffer, IMgImage image, MgImageAspectFlagBits aspectMask, MgImageLayout oldImageLayout, MgImageLayout newImageLayout, MgImageSubresourceRange subresourceRange) { const uint VK_QUEUE_FAMILY_IGNORED = ~0U; // 0xffffffff; // Create an image barrier object MgImageMemoryBarrier imageMemoryBarrier = new MgImageMemoryBarrier { OldLayout = oldImageLayout, NewLayout = newImageLayout, Image = image, SubresourceRange = subresourceRange, // Some default values SrcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, DstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, }; // Source layouts (old) // Undefined layout // Only allowed as initial layout! // Make sure any writes to the image have been finished if (oldImageLayout == MgImageLayout.PREINITIALIZED) { imageMemoryBarrier.SrcAccessMask = (MgAccessFlagBits.HOST_WRITE_BIT | MgAccessFlagBits.TRANSFER_WRITE_BIT); } // Old layout is color attachment // Make sure any writes to the color buffer have been finished if (oldImageLayout == MgImageLayout.COLOR_ATTACHMENT_OPTIMAL) { imageMemoryBarrier.SrcAccessMask = MgAccessFlagBits.COLOR_ATTACHMENT_WRITE_BIT; } // Old layout is depth/stencil attachment // Make sure any writes to the depth/stencil buffer have been finished if (oldImageLayout == MgImageLayout.DEPTH_STENCIL_ATTACHMENT_OPTIMAL) { imageMemoryBarrier.SrcAccessMask = MgAccessFlagBits.DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; } // Old layout is transfer source // Make sure any reads from the image have been finished if (oldImageLayout == MgImageLayout.TRANSFER_SRC_OPTIMAL) { imageMemoryBarrier.SrcAccessMask = MgAccessFlagBits.TRANSFER_READ_BIT; } // Old layout is shader read (sampler, input attachment) // Make sure any shader reads from the image have been finished if (oldImageLayout == MgImageLayout.SHADER_READ_ONLY_OPTIMAL) { imageMemoryBarrier.SrcAccessMask = MgAccessFlagBits.SHADER_READ_BIT; } // Target layouts (new) // New layout is transfer destination (copy, blit) // Make sure any copyies to the image have been finished if (newImageLayout == MgImageLayout.TRANSFER_DST_OPTIMAL) { imageMemoryBarrier.DstAccessMask = MgAccessFlagBits.TRANSFER_WRITE_BIT; } // New layout is transfer source (copy, blit) // Make sure any reads from and writes to the image have been finished if (newImageLayout == MgImageLayout.TRANSFER_SRC_OPTIMAL) { imageMemoryBarrier.SrcAccessMask = imageMemoryBarrier.SrcAccessMask | MgAccessFlagBits.TRANSFER_READ_BIT; imageMemoryBarrier.DstAccessMask = MgAccessFlagBits.TRANSFER_READ_BIT; } // New layout is color attachment // Make sure any writes to the color buffer have been finished if (newImageLayout == MgImageLayout.COLOR_ATTACHMENT_OPTIMAL) { imageMemoryBarrier.DstAccessMask = MgAccessFlagBits.COLOR_ATTACHMENT_WRITE_BIT; imageMemoryBarrier.SrcAccessMask = MgAccessFlagBits.TRANSFER_READ_BIT; } // New layout is depth attachment // Make sure any writes to depth/stencil buffer have been finished if (newImageLayout == MgImageLayout.DEPTH_STENCIL_ATTACHMENT_OPTIMAL) { imageMemoryBarrier.DstAccessMask = imageMemoryBarrier.DstAccessMask | MgAccessFlagBits.DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; } // New layout is shader read (sampler, input attachment) // Make sure any writes to the image have been finished if (newImageLayout == MgImageLayout.SHADER_READ_ONLY_OPTIMAL) { imageMemoryBarrier.SrcAccessMask = MgAccessFlagBits.HOST_WRITE_BIT | MgAccessFlagBits.TRANSFER_WRITE_BIT; imageMemoryBarrier.DstAccessMask = MgAccessFlagBits.SHADER_READ_BIT; } // Put barrier on top MgPipelineStageFlagBits srcStageFlags = MgPipelineStageFlagBits.TOP_OF_PIPE_BIT; MgPipelineStageFlagBits destStageFlags = MgPipelineStageFlagBits.TOP_OF_PIPE_BIT; // Put barrier inside setup command buffer cmdbuffer.CmdPipelineBarrier( srcStageFlags, destStageFlags, 0, null, null, new [] { imageMemoryBarrier }); }