Exemplo n.º 1
0
        public static Vk.ImageView CreateImageView(VkContext state, Vk.Image image, Vk.Format format,
                                                   Vk.ImageAspectFlags aspectFlags)
        {
            var subresourceRange = new Vk.ImageSubresourceRange();

            subresourceRange.AspectMask     = aspectFlags;
            subresourceRange.BaseMipLevel   = 0;
            subresourceRange.LevelCount     = 1;
            subresourceRange.BaseArrayLayer = 0;
            subresourceRange.LayerCount     = 1;

            var viewInfo = new Vk.ImageViewCreateInfo();

            viewInfo.Image            = image;
            viewInfo.ViewType         = Vk.ImageViewType.View2D;
            viewInfo.Format           = format;
            viewInfo.SubresourceRange = subresourceRange;

            return(state.Device.CreateImageView(viewInfo));
        }
Exemplo n.º 2
0
        private void createImageViews(VkContext context)
        {
            var imageViews = new Vk.ImageView[this.wrapper.ImageCapacity];

            for (int i = 0; i < this.wrapper.ImageCapacity; i++)
            {
                Vk.ImageViewCreateInfo createInfo = new Vk.ImageViewCreateInfo();
                createInfo.Image    = this.wrapper.Images[i];
                createInfo.ViewType = Vk.ImageViewType.View2D;
                createInfo.Format   = this.wrapper.Format;

                Vk.ComponentMapping componentMapping = new Vk.ComponentMapping();
                componentMapping.R = Vk.ComponentSwizzle.Identity;
                componentMapping.G = Vk.ComponentSwizzle.Identity;
                componentMapping.B = Vk.ComponentSwizzle.Identity;
                componentMapping.A = Vk.ComponentSwizzle.Identity;

                createInfo.Components = componentMapping;

                Vk.ImageSubresourceRange subresourceRange = new Vk.ImageSubresourceRange();
                subresourceRange.AspectMask     = Vk.ImageAspectFlags.Color;
                subresourceRange.BaseMipLevel   = 0;
                subresourceRange.LevelCount     = 1;
                subresourceRange.BaseArrayLayer = 0;
                subresourceRange.LayerCount     = 1;

                createInfo.SubresourceRange = subresourceRange;

                try {
                    imageViews[i] = context.Device.CreateImageView(createInfo);
                } catch (Vk.ResultException result) {
                    throw new VkException($"An error occurred while creating image view {i}.", result);
                }
            }

            this.wrapper.ImageViews = imageViews;
        }
Exemplo n.º 3
0
        public void TransitionImageLayout(Vk.Image image, Vk.Format format,
                                          Vk.ImageLayout oldLayout, Vk.ImageLayout newLayout)
        {
            var buffer = this.BeginSingleTimeCommands(this.GraphicsCommandPool);

            var subresourceRange = new Vk.ImageSubresourceRange();

            if (newLayout == Vk.ImageLayout.DepthStencilAttachmentOptimal)
            {
                subresourceRange.AspectMask = Vk.ImageAspectFlags.Depth;

                if (VkHelper.HasStencilComponent(format))
                {
                    subresourceRange.AspectMask |= Vk.ImageAspectFlags.Stencil;
                }
            }
            else
            {
                subresourceRange.AspectMask = Vk.ImageAspectFlags.Color;
            }

            subresourceRange.BaseMipLevel   = 0;
            subresourceRange.LevelCount     = 1;
            subresourceRange.BaseArrayLayer = 0;
            subresourceRange.LayerCount     = 1;

            Vk.PipelineStageFlags srcStage;
            Vk.PipelineStageFlags dstStage;

            var barrier = new Vk.ImageMemoryBarrier();

            barrier.OldLayout           = oldLayout;
            barrier.NewLayout           = newLayout;
            barrier.SrcQueueFamilyIndex = VkConstants.VK_QUEUE_FAMILY_IGNORED;
            barrier.DstQueueFamilyIndex = VkConstants.VK_QUEUE_FAMILY_IGNORED;

            if (oldLayout == Vk.ImageLayout.Undefined && newLayout == Vk.ImageLayout.TransferDstOptimal)
            {
                barrier.SrcAccessMask = 0;
                barrier.DstAccessMask = Vk.AccessFlags.TransferWrite;

                srcStage = Vk.PipelineStageFlags.TopOfPipe;
                dstStage = Vk.PipelineStageFlags.Transfer;
            }
            else if (oldLayout == Vk.ImageLayout.TransferDstOptimal && newLayout == Vk.ImageLayout.ShaderReadOnlyOptimal)
            {
                barrier.SrcAccessMask = Vk.AccessFlags.TransferWrite;
                barrier.DstAccessMask = Vk.AccessFlags.ShaderRead;

                srcStage = Vk.PipelineStageFlags.Transfer;
                dstStage = Vk.PipelineStageFlags.FragmentShader;
            }
            else if (oldLayout == Vk.ImageLayout.Undefined && newLayout == Vk.ImageLayout.DepthStencilAttachmentOptimal)
            {
                barrier.SrcAccessMask = 0;
                barrier.DstAccessMask = Vk.AccessFlags.DepthStencilAttachmentRead
                                        | Vk.AccessFlags.DepthStencilAttachmentWrite;

                srcStage = Vk.PipelineStageFlags.TopOfPipe;
                dstStage = Vk.PipelineStageFlags.EarlyFragmentTests;
            }
            else
            {
                throw new Exception("Unsupported layout transition.");
            }

            barrier.Image            = image;
            barrier.SubresourceRange = subresourceRange;

            buffer.CmdPipelineBarrier(srcStage, dstStage, 0, null, null,
                                      new Vk.ImageMemoryBarrier[] { barrier });

            this.EndSingleTimeCommands(this.GraphicsQueue, this.GraphicsCommandPool, buffer);
        }