Пример #1
0
 public unsafe VkImageMemoryBarrier(
     VkImage image, VkImageAspectFlags aspectMask,
     VkImageLayout oldLayout, VkImageLayout newLayout,
     void *pNext = default)
     : this(image, oldLayout, newLayout, new VkImageSubresourceRange(aspectMask, 0, 1, 0, 1), pNext)
 {
 }
Пример #2
0
        public void CreateView(VkImageViewType type = VkImageViewType.ImageView2D, VkImageAspectFlags aspectFlags = VkImageAspectFlags.Color,
                               uint layerCount      = 1,
                               uint baseMipLevel    = 0, uint levelCount = 1, uint baseArrayLayer = 0,
                               VkComponentSwizzle r = VkComponentSwizzle.R,
                               VkComponentSwizzle g = VkComponentSwizzle.G,
                               VkComponentSwizzle b = VkComponentSwizzle.B,
                               VkComponentSwizzle a = VkComponentSwizzle.A)
        {
            VkImageView           view     = default(VkImageView);
            VkImageViewCreateInfo viewInfo = VkImageViewCreateInfo.New();

            viewInfo.image        = handle;
            viewInfo.viewType     = type;
            viewInfo.format       = Format;
            viewInfo.components.r = r;
            viewInfo.components.g = g;
            viewInfo.components.b = b;
            viewInfo.components.a = a;
            viewInfo.subresourceRange.aspectMask     = aspectFlags;
            viewInfo.subresourceRange.baseMipLevel   = baseMipLevel;
            viewInfo.subresourceRange.levelCount     = levelCount;
            viewInfo.subresourceRange.baseArrayLayer = baseArrayLayer;
            viewInfo.subresourceRange.layerCount     = layerCount;

            Utils.CheckResult(vkCreateImageView(Dev.VkDev, ref viewInfo, IntPtr.Zero, out view));

            if (Descriptor.imageView.Handle != 0)
            {
                Dev.DestroyImageView(Descriptor.imageView);
            }
            Descriptor.imageView = view;
        }
Пример #3
0
        protected override void ResolveTextureCore(Texture source, Texture destination)
        {
            if (_activeRenderPass != VkRenderPass.Null)
            {
                EndCurrentRenderPass();
            }

            VkTexture          vkSource      = Util.AssertSubtype <Texture, VkTexture>(source);
            VkTexture          vkDestination = Util.AssertSubtype <Texture, VkTexture>(destination);
            VkImageAspectFlags aspectFlags   = ((source.Usage & TextureUsage.DepthStencil) == TextureUsage.DepthStencil)
                ? VkImageAspectFlags.Depth | VkImageAspectFlags.Stencil
                : VkImageAspectFlags.Color;
            VkImageResolve region = new VkImageResolve
            {
                extent = new VkExtent3D {
                    width = source.Width, height = source.Height, depth = source.Depth
                },
                srcSubresource = new VkImageSubresourceLayers {
                    layerCount = 1, aspectMask = aspectFlags
                },
                dstSubresource = new VkImageSubresourceLayers {
                    layerCount = 1, aspectMask = aspectFlags
                }
            };

            vkCmdResolveImage(
                _cb,
                vkSource.OptimalDeviceImage,
                vkSource.GetImageLayout(0, 0),
                vkDestination.OptimalDeviceImage,
                vkDestination.GetImageLayout(0, 0),
                1,
                ref region);
        }
Пример #4
0
        /// <summary>
        /// Create and Activate a new frabuffer for the supplied RenderPass.
        /// </summary>
        /// <param name="_renderPass">Render pass.</param>
        /// <param name="_width">Width.</param>
        /// <param name="_height">Height.</param>
        /// <param name="views">Array of image views. If null and not in unused state, attachment image and view will be automatically created from the
        /// supplied renderpass configuration.</param>
        public FrameBuffer(RenderPass _renderPass, uint _width, uint _height, params Image[] views)
            : this(_renderPass, _width, _height)
        {
            for (int i = 0; i < views.Length; i++)
            {
                Image v = views[i];
                if (v == null)
                {
                    //automatically create attachment if not in unused state in the renderpass
                    VkAttachmentDescription ad          = renderPass.Attachments[i];
                    VkImageUsageFlags       usage       = 0;
                    VkImageAspectFlags      aspectFlags = 0;

                    Utils.QueryLayoutRequirements(ad.initialLayout, ref usage, ref aspectFlags);
                    Utils.QueryLayoutRequirements(ad.finalLayout, ref usage, ref aspectFlags);
                    foreach (SubPass sp in renderPass.SubPasses)
                    {
                        //TODO:check subpass usage
                    }

                    v = new Image(renderPass.Dev, ad.format, usage, VkMemoryPropertyFlags.DeviceLocal,
                                  _width, _height, VkImageType.Image2D, ad.samples, VkImageTiling.Optimal, 1, createInfo.layers);
                    v.SetName($"fbImg{i}");
                    v.CreateView(VkImageViewType.ImageView2D, aspectFlags);
                }
                else
                {
                    v.Activate();                     //increase ref and create handle if not already activated
                }
                attachments.Add(v);
            }
            Activate();
        }
        private void setImageMemoryBarrier(VkCommandBuffer command,
                                           VkAccessFlags srcAccessMask, VkAccessFlags dstAccessMask,
                                           VkImageLayout oldLayout, VkImageLayout newLayout,
                                           VkImage image, VkImageAspectFlags aspectFlags)
        {
            var imageMemoryBarrier = new VkImageMemoryBarrier()
            {
                srcAccessMask       = srcAccessMask,
                dstAccessMask       = dstAccessMask,
                oldLayout           = oldLayout,
                newLayout           = newLayout,
                srcQueueFamilyIndex = ~0u,
                dstQueueFamilyIndex = ~0u,
                subresourceRange    = new VkImageSubresourceRange
                {
                    aspectMask     = aspectFlags,
                    baseMipLevel   = 0,
                    levelCount     = 1,
                    baseArrayLayer = 0,
                    layerCount     = 1,
                },
                image = image
            };

            VulkanAPI.vkCmdPipelineBarrier(
                command,
                VkPipelineStageFlags.VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
                VkPipelineStageFlags.VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
                0,
                null, // MemoryBarriers
                null, // BufferMemoryBarriers
                new VkImageMemoryBarrier[] { imageMemoryBarrier }
                );
        }
Пример #6
0
 public void SetLayout(CommandBuffer cmdbuffer,
                       VkImageAspectFlags aspectMask,
                       VkImageLayout oldImageLayout,
                       VkImageLayout newImageLayout)
 {
     SetLayout(cmdbuffer, aspectMask, oldImageLayout, newImageLayout, oldImageLayout.GetDefaultStage(), newImageLayout.GetDefaultStage());
 }
Пример #7
0
 public VkImageSubresourceLayers(VkImageAspectFlags _aspectMask = VkImageAspectFlags.Color,
                                 uint _layerCount = 1, uint _mipLevel = 0, uint _baseArrayLayer = 0)
 {
     this._aspectMask = (uint)_aspectMask;
     layerCount       = _layerCount;
     mipLevel         = _mipLevel;
     baseArrayLayer   = _baseArrayLayer;
 }
Пример #8
0
 public VkImageSubresourceLayers(
     VkImageAspectFlags aspectMask,
     uint mipLevel,
     uint baseArrayLayer, uint layerCount)
 {
     this.aspectMask     = aspectMask;
     this.mipLevel       = mipLevel;
     this.baseArrayLayer = baseArrayLayer;
     this.layerCount     = layerCount;
 }
 public VkImageSubresourceRange(
     VkImageAspectFlags aspectMask,
     uint baseMipLevel, uint levelCount,
     uint baseArrayLayer, uint layerCount)
 {
     this.aspectMask     = aspectMask;
     this.baseMipLevel   = baseMipLevel;
     this.levelCount     = levelCount;
     this.baseArrayLayer = baseArrayLayer;
     this.layerCount     = layerCount;
 }
Пример #10
0
        public VkSubresourceLayout GetSubresourceLayout(VkImageAspectFlags aspectMask = VkImageAspectFlags.Color, uint mipLevel = 0, uint arrayLayer = 0)
        {
            VkImageSubresource subresource = new VkImageSubresource {
                aspectMask = aspectMask,
                mipLevel   = mipLevel,
                arrayLayer = arrayLayer
            };

            vkGetImageSubresourceLayout(Dev.VkDev, this.handle, ref subresource, out VkSubresourceLayout result);
            return(result);
        }
Пример #11
0
 public void SetFullHandles(VkImage image, VkImageView attachmentView,
                            VkImageLayout layout, VkAccessFlags accessMask,
                            VkFormat nativeFormat, VkImageAspectFlags aspect)
 {
     NativeImage = image;
     NativeColorAttachmentView = attachmentView;
     NativeLayout      = layout;
     NativeAccessMask  = accessMask;
     NativeFormat      = nativeFormat;
     NativeImageAspect = aspect;
 }
Пример #12
0
        public void SetLayout(
            CommandBuffer cmdbuffer,
            VkImageAspectFlags aspectMask,
            VkImageLayout oldImageLayout,
            VkImageLayout newImageLayout,
            VkPipelineStageFlags srcStageMask = VkPipelineStageFlags.AllCommands,
            VkPipelineStageFlags dstStageMask = VkPipelineStageFlags.AllCommands)
        {
            VkImageSubresourceRange subresourceRange = new VkImageSubresourceRange {
                aspectMask   = aspectMask,
                baseMipLevel = 0,
                levelCount   = CreateInfo.mipLevels,
                layerCount   = CreateInfo.arrayLayers,
            };

            SetLayout(cmdbuffer, oldImageLayout, newImageLayout, subresourceRange, srcStageMask, dstStageMask);
        }
Пример #13
0
        // Fixed sub resource on first mip level and layer
        public static void setImageLayout(
            VkCommandBuffer cmdbuffer,
            VkImage image,
            VkImageAspectFlags aspectMask,
            VkImageLayout oldImageLayout,
            VkImageLayout newImageLayout,
            VkPipelineStageFlags srcStageMask = VkPipelineStageFlags.AllCommands,
            VkPipelineStageFlags dstStageMask = VkPipelineStageFlags.AllCommands)
        {
            VkImageSubresourceRange subresourceRange = new VkImageSubresourceRange();

            subresourceRange.aspectMask   = aspectMask;
            subresourceRange.baseMipLevel = 0;
            subresourceRange.levelCount   = 1;
            subresourceRange.layerCount   = 1;
            setImageLayout(cmdbuffer, image, aspectMask, oldImageLayout, newImageLayout, subresourceRange);
        }
Пример #14
0
 public unsafe VkImageMemoryBarrier(VkImage image, VkAccessFlags srcAccessMask, VkAccessFlags dstAccessMask, VkImageLayout oldLayout, VkImageLayout newLayout,
                                    VkImageAspectFlags aspectMask = VkImageAspectFlags.Color, uint baseMipLevel = 0, uint levelCount = uint.MaxValue)
 {
     this.sType                           = VkStructureType.ImageMemoryBarrier;
     this.pNext                           = null;
     this.srcAccessMask                   = (VkAccessFlags)srcAccessMask;
     this.dstAccessMask                   = (VkAccessFlags)dstAccessMask;
     this.oldLayout                       = (VkImageLayout)oldLayout;
     this.newLayout                       = (VkImageLayout)newLayout;
     this.srcQueueFamilyIndex             = uint.MaxValue;
     this.dstQueueFamilyIndex             = uint.MaxValue;
     this.image                           = image;
     this.subresourceRange.aspectMask     = (VkImageAspectFlags)aspectMask;
     this.subresourceRange.baseMipLevel   = baseMipLevel;
     this.subresourceRange.baseArrayLayer = 0;
     this.subresourceRange.levelCount     = levelCount;
     this.subresourceRange.layerCount     = uint.MaxValue;
 }
Пример #15
0
        public unsafe ImageView(
            Image image,
            VkImageAspectFlags aspectMask
            )
        {
            _device = image.Device;
            _image  = image;
            var imageViewInfo = new VkImageViewCreateInfo
            {
                sType      = VkStructureType.ImageViewCreateInfo,
                image      = image.Handle,
                viewType   = VkImageViewType.Image2D,
                format     = image.Format,
                components = new VkComponentMapping
                {
                    r = VkComponentSwizzle.Identity,
                    g = VkComponentSwizzle.Identity,
                    b = VkComponentSwizzle.Identity,
                    a = VkComponentSwizzle.Identity
                },
                subresourceRange = new VkImageSubresourceRange
                {
                    aspectMask     = aspectMask,
                    baseMipLevel   = 0,
                    levelCount     = image.MipLevel,
                    baseArrayLayer = 0,
                    layerCount     = 1
                }
            };

            VkImageView imageView;

            if (VulkanNative.vkCreateImageView(
                    _device.Handle,
                    &imageViewInfo,
                    null,
                    &imageView
                    ) != VkResult.Success)
            {
                throw new Exception("failed to create image view");
            }
            _handle = imageView;
        }
Пример #16
0
        protected override void ClearDepthStencilCore(float depth, byte stencil)
        {
            VkClearValue clearValue = new VkClearValue {
                depthStencil = new VkClearDepthStencilValue(depth, stencil)
            };

            if (_activeRenderPass != VkRenderPass.Null)
            {
                VkImageAspectFlags aspectMask = VkImageAspectFlags.Depth;
                if (FormatHelpers.IsStencilFormat(_currentFramebuffer.DepthTarget.Value.Target.Format))
                {
                    aspectMask |= VkImageAspectFlags.Stencil;
                }
                VkClearAttachment clearAttachment = new VkClearAttachment
                {
                    aspectMask = aspectMask,
                    clearValue = clearValue
                };

                Texture depthTex         = _currentFramebuffer.DepthTarget.Value.Target;
                uint    renderableWidth  = _currentFramebuffer.RenderableWidth;
                uint    renderableHeight = _currentFramebuffer.RenderableHeight;
                if (renderableWidth > 0 && renderableHeight > 0)
                {
                    VkClearRect clearRect = new VkClearRect
                    {
                        baseArrayLayer = 0,
                        layerCount     = 1,
                        rect           = new VkRect2D(0, 0, renderableWidth, renderableHeight)
                    };

                    vkCmdClearAttachments(_cb, 1, ref clearAttachment, 1, ref clearRect);
                }
            }
            else
            {
                // Queue up the clear value for the next RenderPass.
                _depthClearValue = clearValue;
            }
        }
Пример #17
0
        internal VkSubresourceLayout GetSubresourceLayout(uint subresource)
        {
            bool staging = _stagingBuffer.Handle != 0;

            Util.GetMipLevelAndArrayLayer(this, subresource, out uint mipLevel, out uint arrayLayer);
            if (!staging)
            {
                VkImageAspectFlags aspect = (Usage & TextureUsage.DepthStencil) == TextureUsage.DepthStencil
                  ? (VkImageAspectFlags.Depth | VkImageAspectFlags.Stencil)
                  : VkImageAspectFlags.Color;
                VkImageSubresource imageSubresource = new VkImageSubresource
                {
                    arrayLayer = arrayLayer,
                    mipLevel   = mipLevel,
                    aspectMask = aspect,
                };

                vkGetImageSubresourceLayout(_gd.Device, _optimalImage, ref imageSubresource, out VkSubresourceLayout layout);
                return(layout);
            }
            else
            {
                uint blockSize = FormatHelpers.IsCompressedFormat(Format) ? 4u : 1u;
                Util.GetMipDimensions(this, mipLevel, out uint mipWidth, out uint mipHeight, out uint mipDepth);
                uint rowPitch   = FormatHelpers.GetRowPitch(mipWidth, Format);
                uint depthPitch = FormatHelpers.GetDepthPitch(rowPitch, mipHeight, Format);

                VkSubresourceLayout layout = new VkSubresourceLayout()
                {
                    rowPitch   = rowPitch,
                    depthPitch = depthPitch,
                    arrayPitch = depthPitch,
                    size       = depthPitch,
                };
                layout.offset = Util.ComputeSubresourceOffset(this, mipLevel, arrayLayer);

                return(layout);
            }
        }
Пример #18
0
        internal VkSubresourceLayout GetSubresourceLayout(uint subresource)
        {
            bool staging = _stagingImages != null;

            Util.GetMipLevelAndArrayLayer(this, subresource, out uint mipLevel, out uint arrayLayer);
            VkImageAspectFlags aspect = (Usage & TextureUsage.DepthStencil) == TextureUsage.DepthStencil
                ? (VkImageAspectFlags.Depth | VkImageAspectFlags.Stencil)
                : VkImageAspectFlags.Color;
            VkImageSubresource imageSubresource = new VkImageSubresource
            {
                arrayLayer = staging ? 0 : arrayLayer,
                mipLevel   = staging ? 0 : mipLevel,
                aspectMask = aspect,
            };

            VkImage image = staging
                ? _stagingImages[subresource]
                : _optimalImage;

            vkGetImageSubresourceLayout(_gd.Device, image, ref imageSubresource, out VkSubresourceLayout layout);
            return(layout);
        }
Пример #19
0
        public void SetLayout(
            CommandBuffer cmdbuffer,
            VkImageAspectFlags aspectMask,
            VkAccessFlags srcAccessMask,
            VkAccessFlags dstAccessMask,
            VkImageLayout oldImageLayout,
            VkImageLayout newImageLayout,
            VkPipelineStageFlags srcStageMask = VkPipelineStageFlags.AllCommands,
            VkPipelineStageFlags dstStageMask = VkPipelineStageFlags.AllCommands,
            uint srcQueueFamilyIndex          = Vk.QueueFamilyIgnored,
            uint dstQueueFamilyIndex          = Vk.QueueFamilyIgnored)
        {
            VkImageSubresourceRange subresourceRange = new VkImageSubresourceRange {
                aspectMask   = aspectMask,
                baseMipLevel = 0,
                levelCount   = CreateInfo.mipLevels,
                layerCount   = CreateInfo.arrayLayers,
            };

            SetLayout(cmdbuffer, srcAccessMask, dstAccessMask, oldImageLayout, newImageLayout, subresourceRange, srcStageMask, dstStageMask,
                      srcQueueFamilyIndex, dstQueueFamilyIndex);
        }
Пример #20
0
        internal VkSubresourceLayout GetSubresourceLayout(uint subresource)
        {
            bool staging = _stagingBuffer != null;

            Util.GetMipLevelAndArrayLayer(this, subresource, out uint mipLevel, out uint arrayLayer);
            if (!staging)
            {
                VkImageAspectFlags aspect = (Usage & TextureUsage.DepthStencil) == TextureUsage.DepthStencil
                  ? (VkImageAspectFlags.Depth | VkImageAspectFlags.Stencil)
                  : VkImageAspectFlags.Color;
                VkImageSubresource imageSubresource = new VkImageSubresource
                {
                    arrayLayer = arrayLayer,
                    mipLevel   = mipLevel,
                    aspectMask = aspect,
                };

                vkGetImageSubresourceLayout(_gd.Device, _optimalImage, ref imageSubresource, out VkSubresourceLayout layout);
                return(layout);
            }
            else
            {
                uint pixelSize = FormatHelpers.GetSizeInBytes(Format);
                Util.GetMipDimensions(this, mipLevel, out uint mipWidth, out uint mipHeight, out uint mipDepth);
                VkSubresourceLayout layout = new VkSubresourceLayout()
                {
                    rowPitch   = mipWidth * pixelSize,
                    depthPitch = mipWidth * mipHeight * pixelSize,
                    arrayPitch = mipWidth * mipHeight * pixelSize,
                    size       = mipWidth * mipHeight * mipDepth * pixelSize
                };
                layout.offset = Util.ComputeSubresourceOffset(this, mipLevel, arrayLayer);

                return(layout);
            }
        }
Пример #21
0
        public static void TransitionImageLayout(
            VkCommandBuffer cb,
            VkImage image,
            uint baseMipLevel,
            uint levelCount,
            uint baseArrayLayer,
            uint layerCount,
            VkImageAspectFlags aspectMask,
            VkImageLayout oldLayout,
            VkImageLayout newLayout)
        {
            Debug.Assert(oldLayout != newLayout);
            VkImageMemoryBarrier barrier = VkImageMemoryBarrier.New();

            barrier.oldLayout           = oldLayout;
            barrier.newLayout           = newLayout;
            barrier.srcQueueFamilyIndex = QueueFamilyIgnored;
            barrier.dstQueueFamilyIndex = QueueFamilyIgnored;
            barrier.image = image;
            barrier.subresourceRange.aspectMask     = aspectMask;
            barrier.subresourceRange.baseMipLevel   = baseMipLevel;
            barrier.subresourceRange.levelCount     = levelCount;
            barrier.subresourceRange.baseArrayLayer = baseArrayLayer;
            barrier.subresourceRange.layerCount     = layerCount;

            VkPipelineStageFlags srcStageFlags = VkPipelineStageFlags.None;
            VkPipelineStageFlags dstStageFlags = VkPipelineStageFlags.None;

            if ((oldLayout == VkImageLayout.Undefined || oldLayout == VkImageLayout.Preinitialized) && newLayout == VkImageLayout.TransferDstOptimal)
            {
                barrier.srcAccessMask = VkAccessFlags.None;
                barrier.dstAccessMask = VkAccessFlags.TransferWrite;
                srcStageFlags         = VkPipelineStageFlags.TopOfPipe;
                dstStageFlags         = VkPipelineStageFlags.Transfer;
            }
            else if (oldLayout == VkImageLayout.ShaderReadOnlyOptimal && newLayout == VkImageLayout.TransferSrcOptimal)
            {
                barrier.srcAccessMask = VkAccessFlags.ShaderRead;
                barrier.dstAccessMask = VkAccessFlags.TransferRead;
                srcStageFlags         = VkPipelineStageFlags.FragmentShader;
                dstStageFlags         = VkPipelineStageFlags.Transfer;
            }
            else if (oldLayout == VkImageLayout.ShaderReadOnlyOptimal && newLayout == VkImageLayout.TransferDstOptimal)
            {
                barrier.srcAccessMask = VkAccessFlags.ShaderRead;
                barrier.dstAccessMask = VkAccessFlags.TransferWrite;
                srcStageFlags         = VkPipelineStageFlags.FragmentShader;
                dstStageFlags         = VkPipelineStageFlags.Transfer;
            }
            else if (oldLayout == VkImageLayout.Preinitialized && newLayout == VkImageLayout.TransferSrcOptimal)
            {
                barrier.srcAccessMask = VkAccessFlags.None;
                barrier.dstAccessMask = VkAccessFlags.TransferRead;
                srcStageFlags         = VkPipelineStageFlags.TopOfPipe;
                dstStageFlags         = VkPipelineStageFlags.Transfer;
            }
            else if (oldLayout == VkImageLayout.TransferSrcOptimal && newLayout == VkImageLayout.ShaderReadOnlyOptimal)
            {
                barrier.srcAccessMask = VkAccessFlags.TransferRead;
                barrier.dstAccessMask = VkAccessFlags.ShaderRead;
                srcStageFlags         = VkPipelineStageFlags.Transfer;
                dstStageFlags         = VkPipelineStageFlags.FragmentShader;
            }
            else if (oldLayout == VkImageLayout.TransferDstOptimal && newLayout == VkImageLayout.ShaderReadOnlyOptimal)
            {
                barrier.srcAccessMask = VkAccessFlags.TransferWrite;
                barrier.dstAccessMask = VkAccessFlags.ShaderRead;
                srcStageFlags         = VkPipelineStageFlags.Transfer;
                dstStageFlags         = VkPipelineStageFlags.FragmentShader;
            }
            else if (oldLayout == VkImageLayout.TransferSrcOptimal && newLayout == VkImageLayout.TransferDstOptimal)
            {
                barrier.srcAccessMask = VkAccessFlags.TransferRead;
                barrier.dstAccessMask = VkAccessFlags.TransferWrite;
                srcStageFlags         = VkPipelineStageFlags.Transfer;
                dstStageFlags         = VkPipelineStageFlags.Transfer;
            }
            else if (oldLayout == VkImageLayout.TransferDstOptimal && newLayout == VkImageLayout.TransferSrcOptimal)
            {
                barrier.srcAccessMask = VkAccessFlags.TransferWrite;
                barrier.dstAccessMask = VkAccessFlags.TransferRead;
                srcStageFlags         = VkPipelineStageFlags.Transfer;
                dstStageFlags         = VkPipelineStageFlags.Transfer;
            }
            else if (oldLayout == VkImageLayout.ColorAttachmentOptimal && newLayout == VkImageLayout.TransferSrcOptimal)
            {
                barrier.srcAccessMask = VkAccessFlags.ColorAttachmentWrite;
                barrier.dstAccessMask = VkAccessFlags.TransferRead;
                srcStageFlags         = VkPipelineStageFlags.ColorAttachmentOutput;
                dstStageFlags         = VkPipelineStageFlags.Transfer;
            }
            else if (oldLayout == VkImageLayout.ColorAttachmentOptimal && newLayout == VkImageLayout.TransferDstOptimal)
            {
                barrier.srcAccessMask = VkAccessFlags.ColorAttachmentWrite;
                barrier.dstAccessMask = VkAccessFlags.TransferWrite;
                srcStageFlags         = VkPipelineStageFlags.ColorAttachmentOutput;
                dstStageFlags         = VkPipelineStageFlags.Transfer;
            }
            else if (oldLayout == VkImageLayout.ColorAttachmentOptimal && newLayout == VkImageLayout.ShaderReadOnlyOptimal)
            {
                barrier.srcAccessMask = VkAccessFlags.ColorAttachmentWrite;
                barrier.dstAccessMask = VkAccessFlags.ShaderRead;
                srcStageFlags         = VkPipelineStageFlags.ColorAttachmentOutput;
                dstStageFlags         = VkPipelineStageFlags.FragmentShader;
            }
            else if (oldLayout == VkImageLayout.DepthStencilAttachmentOptimal && newLayout == VkImageLayout.ShaderReadOnlyOptimal)
            {
                barrier.srcAccessMask = VkAccessFlags.DepthStencilAttachmentWrite;
                barrier.dstAccessMask = VkAccessFlags.ShaderRead;
                srcStageFlags         = VkPipelineStageFlags.LateFragmentTests;
                dstStageFlags         = VkPipelineStageFlags.FragmentShader;
            }
            else if (oldLayout == VkImageLayout.ColorAttachmentOptimal && newLayout == VkImageLayout.PresentSrcKHR)
            {
                barrier.srcAccessMask = VkAccessFlags.ColorAttachmentWrite;
                barrier.dstAccessMask = VkAccessFlags.MemoryRead;
                srcStageFlags         = VkPipelineStageFlags.ColorAttachmentOutput;
                dstStageFlags         = VkPipelineStageFlags.BottomOfPipe;
            }
            else if (oldLayout == VkImageLayout.TransferDstOptimal && newLayout == VkImageLayout.ColorAttachmentOptimal)
            {
                barrier.srcAccessMask = VkAccessFlags.TransferWrite;
                barrier.dstAccessMask = VkAccessFlags.ColorAttachmentWrite;
                srcStageFlags         = VkPipelineStageFlags.Transfer;
                dstStageFlags         = VkPipelineStageFlags.ColorAttachmentOutput;
            }
            else if (oldLayout == VkImageLayout.TransferDstOptimal && newLayout == VkImageLayout.DepthStencilAttachmentOptimal)
            {
                barrier.srcAccessMask = VkAccessFlags.TransferWrite;
                barrier.dstAccessMask = VkAccessFlags.DepthStencilAttachmentWrite;
                srcStageFlags         = VkPipelineStageFlags.Transfer;
                dstStageFlags         = VkPipelineStageFlags.LateFragmentTests;
            }
            else
            {
                Debug.Fail("Invalid image layout transition.");
            }

            vkCmdPipelineBarrier(
                cb,
                srcStageFlags,
                dstStageFlags,
                VkDependencyFlags.None,
                0, null,
                0, null,
                1, &barrier);
        }
Пример #22
0
        public VkImageView CreateImageView(VkImage image, VkFormat format, VkImageViewType viewType = VkImageViewType.ImageView2D, VkImageAspectFlags aspectFlags = VkImageAspectFlags.Color)
        {
            VkImageView           view;
            VkImageViewCreateInfo infos = VkImageViewCreateInfo.New();

            infos.image      = image;
            infos.viewType   = viewType;
            infos.format     = format;
            infos.components = new VkComponentMapping {
                r = VkComponentSwizzle.R, g = VkComponentSwizzle.G, b = VkComponentSwizzle.B, a = VkComponentSwizzle.A
            };
            infos.subresourceRange = new VkImageSubresourceRange(aspectFlags);

            Utils.CheckResult(vkCreateImageView(dev, ref infos, IntPtr.Zero, out view));
            return(view);
        }
Пример #23
0
        // 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

        public static void setImageLayout(
            VkCommandBuffer cmdbuffer,
            VkImage image,
            VkImageAspectFlags aspectMask,
            VkImageLayout oldImageLayout,
            VkImageLayout newImageLayout,
            VkImageSubresourceRange subresourceRange,
            VkPipelineStageFlags srcStageMask = VkPipelineStageFlags.AllCommands,
            VkPipelineStageFlags dstStageMask = VkPipelineStageFlags.AllCommands)
        {
            // Create an image barrier object
            VkImageMemoryBarrier imageMemoryBarrier = Initializers.imageMemoryBarrier();

            imageMemoryBarrier.oldLayout        = oldImageLayout;
            imageMemoryBarrier.newLayout        = newImageLayout;
            imageMemoryBarrier.image            = image;
            imageMemoryBarrier.subresourceRange = subresourceRange;

            // Source layouts (old)
            // Source access mask controls actions that have to be finished on the old layout
            // before it will be transitioned to the new layout
            switch (oldImageLayout)
            {
            case VkImageLayout.Undefined:
                // Image layout is undefined (or does not matter)
                // Only valid as initial layout
                // No flags required, listed only for completeness
                imageMemoryBarrier.srcAccessMask = 0;
                break;

            case VkImageLayout.Preinitialized:
                // Image is preinitialized
                // Only valid as initial layout for linear images, preserves memory contents
                // Make sure host writes have been finished
                imageMemoryBarrier.srcAccessMask = VkAccessFlags.HostWrite;
                break;

            case VkImageLayout.ColorAttachmentOptimal:
                // Image is a color attachment
                // Make sure any writes to the color buffer have been finished
                imageMemoryBarrier.srcAccessMask = VkAccessFlags.ColorAttachmentWrite;
                break;

            case VkImageLayout.DepthStencilAttachmentOptimal:
                // Image is a depth/stencil attachment
                // Make sure any writes to the depth/stencil buffer have been finished
                imageMemoryBarrier.srcAccessMask = VkAccessFlags.DepthStencilAttachmentWrite;
                break;

            case VkImageLayout.TransferSrcOptimal:
                // Image is a transfer source
                // Make sure any reads from the image have been finished
                imageMemoryBarrier.srcAccessMask = VkAccessFlags.TransferRead;
                break;

            case VkImageLayout.TransferDstOptimal:
                // Image is a transfer destination
                // Make sure any writes to the image have been finished
                imageMemoryBarrier.srcAccessMask = VkAccessFlags.TransferWrite;
                break;

            case VkImageLayout.ShaderReadOnlyOptimal:
                // Image is read by a shader
                // Make sure any shader reads from the image have been finished
                imageMemoryBarrier.srcAccessMask = VkAccessFlags.ShaderRead;
                break;
            }

            // Target layouts (new)
            // Destination access mask controls the dependency for the new image layout
            switch (newImageLayout)
            {
            case VkImageLayout.TransferDstOptimal:
                // Image will be used as a transfer destination
                // Make sure any writes to the image have been finished
                imageMemoryBarrier.dstAccessMask = VkAccessFlags.TransferWrite;
                break;

            case VkImageLayout.TransferSrcOptimal:
                // Image will be used as a transfer source
                // Make sure any reads from and writes to the image have been finished
                imageMemoryBarrier.srcAccessMask = imageMemoryBarrier.srcAccessMask | VkAccessFlags.TransferRead;
                imageMemoryBarrier.dstAccessMask = VkAccessFlags.TransferRead;
                break;

            case VkImageLayout.ColorAttachmentOptimal:
                // Image will be used as a color attachment
                // Make sure any writes to the color buffer have been finished
                imageMemoryBarrier.srcAccessMask = VkAccessFlags.TransferRead;
                imageMemoryBarrier.dstAccessMask = VkAccessFlags.ColorAttachmentWrite;
                break;

            case VkImageLayout.DepthStencilAttachmentOptimal:
                // Image layout will be used as a depth/stencil attachment
                // Make sure any writes to depth/stencil buffer have been finished
                imageMemoryBarrier.dstAccessMask = imageMemoryBarrier.dstAccessMask | VkAccessFlags.DepthStencilAttachmentWrite;
                break;

            case VkImageLayout.ShaderReadOnlyOptimal:
                // Image will be read in a shader (sampler, input attachment)
                // Make sure any writes to the image have been finished
                if (imageMemoryBarrier.srcAccessMask == 0)
                {
                    imageMemoryBarrier.srcAccessMask = VkAccessFlags.HostWrite | VkAccessFlags.TransferWrite;
                }
                imageMemoryBarrier.dstAccessMask = VkAccessFlags.ShaderRead;
                break;
            }

            // Put barrier inside setup command buffer
            vkCmdPipelineBarrier(
                cmdbuffer,
                srcStageMask,
                dstStageMask,
                0,
                0, null,
                0, null,
                1, &imageMemoryBarrier);
        }
Пример #24
0
        // Create an image memory barrier for changing the layout of
        // an image and put it into an active command buffer
        void setImageLayout(
            VkCommandBuffer cmdBuffer,
            VkImage image,
            VkImageAspectFlags aspectMask,
            VkImageLayout oldImageLayout,
            VkImageLayout newImageLayout,
            VkImageSubresourceRange subresourceRange)
        {
            // Create an image barrier object
            VkImageMemoryBarrier imageMemoryBarrier = Initializers.imageMemoryBarrier();;

            imageMemoryBarrier.oldLayout        = oldImageLayout;
            imageMemoryBarrier.newLayout        = newImageLayout;
            imageMemoryBarrier.image            = image;
            imageMemoryBarrier.subresourceRange = subresourceRange;

            // Only sets masks for layouts used in this example
            // For a more complete version that can be used with other layouts see vks::tools::setImageLayout

            // Source layouts (old)
            switch (oldImageLayout)
            {
            case VkImageLayout.Undefined:
                // Only valid as initial layout, memory contents are not preserved
                // Can be accessed directly, no source dependency required
                imageMemoryBarrier.srcAccessMask = 0;
                break;

            case VkImageLayout.Preinitialized:
                // Only valid as initial layout for linear images, preserves memory contents
                // Make sure host writes to the image have been finished
                imageMemoryBarrier.srcAccessMask = VkAccessFlags.HostWrite;
                break;

            case VkImageLayout.TransferDstOptimal:
                // Old layout is transfer destination
                // Make sure any writes to the image have been finished
                imageMemoryBarrier.srcAccessMask = VkAccessFlags.TransferWrite;
                break;
            }

            // Target layouts (new)
            switch (newImageLayout)
            {
            case VkImageLayout.TransferSrcOptimal:
                // Transfer source (copy, blit)
                // Make sure any reads from the image have been finished
                imageMemoryBarrier.dstAccessMask = VkAccessFlags.TransferRead;
                break;

            case VkImageLayout.TransferDstOptimal:
                // Transfer destination (copy, blit)
                // Make sure any writes to the image have been finished
                imageMemoryBarrier.dstAccessMask = VkAccessFlags.TransferWrite;
                break;

            case VkImageLayout.ShaderReadOnlyOptimal:
                // Shader read (sampler, input attachment)
                imageMemoryBarrier.dstAccessMask = VkAccessFlags.ShaderRead;
                break;
            }

            // Put barrier on top of pipeline
            VkPipelineStageFlags srcStageFlags  = VkPipelineStageFlags.TopOfPipe;
            VkPipelineStageFlags destStageFlags = VkPipelineStageFlags.TopOfPipe;

            // Put barrier inside setup command buffer
            vkCmdPipelineBarrier(
                cmdBuffer,
                srcStageFlags,
                destStageFlags,
                VkDependencyFlags.None,
                0, null,
                0, null,
                1, &imageMemoryBarrier);
        }
Пример #25
0
        public static ImageView Create(Image image, VkImageViewType viewType, VkFormat format, VkImageAspectFlags aspectMask, uint baseMipLevel, uint numMipLevels, uint baseArrayLayer = 0, uint arrayLayers = 1)
        {
            var viewCreateInfo = new VkImageViewCreateInfo
            {
                sType      = VkStructureType.ImageViewCreateInfo,
                image      = image,
                viewType   = viewType,
                format     = format,
                components = new VkComponentMapping(VkComponentSwizzle.R, VkComponentSwizzle.G, VkComponentSwizzle.B, VkComponentSwizzle.A),

                subresourceRange = new VkImageSubresourceRange
                {
                    aspectMask     = aspectMask,
                    baseMipLevel   = baseMipLevel,
                    levelCount     = numMipLevels,
                    baseArrayLayer = baseArrayLayer,
                    layerCount     = arrayLayers,
                }
            };

            return(new ImageView(image, ref viewCreateInfo));
        }
Пример #26
0
        private void InitializeFromImpl(DataBox[] dataBoxes = null)
        {
            NativeFormat = VulkanConvertExtensions.ConvertPixelFormat(ViewFormat);
            HasStencil   = IsStencilFormat(ViewFormat);

            NativeImageAspect = IsDepthStencil ? VkImageAspectFlags.Depth : VkImageAspectFlags.Color;
            if (HasStencil)
            {
                NativeImageAspect |= VkImageAspectFlags.Stencil;
            }

            // For depth-stencil formats, automatically fall back to a supported one
            if (IsDepthStencil && HasStencil)
            {
                NativeFormat = GetFallbackDepthStencilFormat(GraphicsDevice, NativeFormat);
            }

            if (Usage == GraphicsResourceUsage.Staging)
            {
                if (NativeImage != VkImage.Null)
                {
                    throw new InvalidOperationException();
                }

                if (isNotOwningResources)
                {
                    throw new InvalidOperationException();
                }

                NativeAccessMask = VkAccessFlags.HostRead | VkAccessFlags.HostWrite;

                NativePipelineStageMask = VkPipelineStageFlags.Host;

                if (ParentTexture != null)
                {
                    // Create only a view
                    NativeBuffer = ParentTexture.NativeBuffer;
                    NativeMemory = ParentTexture.NativeMemory;
                }
                else
                {
                    CreateBuffer();

                    if (dataBoxes != null && dataBoxes.Length > 0)
                    {
                        throw new InvalidOperationException();
                    }
                }
            }
            else
            {
                if (NativeImage != VkImage.Null)
                {
                    throw new InvalidOperationException();
                }

                NativeLayout =
                    IsRenderTarget ? VkImageLayout.ColorAttachmentOptimal :
                    IsDepthStencil ? VkImageLayout.DepthStencilAttachmentOptimal :
                    IsShaderResource ? VkImageLayout.ShaderReadOnlyOptimal :
                    VkImageLayout.General;

                if (NativeLayout == VkImageLayout.TransferDstOptimal)
                {
                    NativeAccessMask = VkAccessFlags.TransferRead;
                }

                if (NativeLayout == VkImageLayout.ColorAttachmentOptimal)
                {
                    NativeAccessMask = VkAccessFlags.ColorAttachmentWrite;
                }

                if (NativeLayout == VkImageLayout.DepthStencilAttachmentOptimal)
                {
                    NativeAccessMask = VkAccessFlags.DepthStencilAttachmentWrite;
                }

                if (NativeLayout == VkImageLayout.ShaderReadOnlyOptimal)
                {
                    NativeAccessMask = VkAccessFlags.ShaderRead | VkAccessFlags.InputAttachmentRead;
                }

                NativePipelineStageMask =
                    IsRenderTarget ? VkPipelineStageFlags.ColorAttachmentOutput :
                    IsDepthStencil ? VkPipelineStageFlags.ColorAttachmentOutput | VkPipelineStageFlags.EarlyFragmentTests | VkPipelineStageFlags.LateFragmentTests :
                    IsShaderResource ? VkPipelineStageFlags.VertexInput | VkPipelineStageFlags.FragmentShader :
                    VkPipelineStageFlags.None;

                if (ParentTexture != null)
                {
                    // Create only a view
                    NativeImage  = ParentTexture.NativeImage;
                    NativeMemory = ParentTexture.NativeMemory;
                }
                else
                {
                    if (!isNotOwningResources)
                    {
                        CreateImage();

                        InitializeImage(dataBoxes);
                    }
                }

                if (!isNotOwningResources)
                {
                    NativeImageView           = GetImageView(ViewType, ArraySlice, MipLevel);
                    NativeColorAttachmentView = GetColorAttachmentView(ViewType, ArraySlice, MipLevel);
                    NativeDepthStencilView    = GetDepthStencilView();
                }
            }
        }