Ejemplo n.º 1
0
        internal unsafe void EndPresentation(VulkanCommandBufferPool.VulkanCommandBuffer commandBuffer)
        {
            VulkanMemoryHelper.TransitionLayout(_device, commandBuffer.InternalHandle,
                                                _swapchainImages[_nextImage], ImageLayout.TransferDstOptimal,
                                                AccessFlags.AccessNoneKhr,
                                                ImageLayout.PresentSrcKhr,
                                                AccessFlags.AccessNoneKhr,
                                                1);

            commandBuffer.Submit(
                stackalloc[] { _semaphorePair.ImageAvailableSemaphore },
Ejemplo n.º 2
0
        internal void BlitImageToCurrentImage(VulkanSurfaceRenderTarget renderTarget, CommandBuffer commandBuffer)
        {
            var image = renderTarget.GetImage();

            VulkanMemoryHelper.TransitionLayout(_device, commandBuffer,
                                                image.InternalHandle.Value, (ImageLayout)image.CurrentLayout,
                                                AccessFlags.AccessNoneKhr,
                                                ImageLayout.TransferSrcOptimal,
                                                AccessFlags.AccessTransferReadBit,
                                                renderTarget.MipLevels);

            var srcBlitRegion = new ImageBlit
            {
                SrcOffsets = new ImageBlit.SrcOffsetsBuffer
                {
                    Element0 = new Offset3D(0, 0, 0),
                    Element1 = new Offset3D(renderTarget.Size.Width, renderTarget.Size.Height, 1),
                },
                DstOffsets = new ImageBlit.DstOffsetsBuffer
                {
                    Element0 = new Offset3D(0, 0, 0),
                    Element1 = new Offset3D(Size.Width, Size.Height, 1),
                },
                SrcSubresource = new ImageSubresourceLayers
                {
                    AspectMask     = ImageAspectFlags.ImageAspectColorBit,
                    BaseArrayLayer = 0,
                    LayerCount     = 1,
                    MipLevel       = 0
                },
                DstSubresource = new ImageSubresourceLayers
                {
                    AspectMask     = ImageAspectFlags.ImageAspectColorBit,
                    BaseArrayLayer = 0,
                    LayerCount     = 1,
                    MipLevel       = 0
                }
            };

            _device.Api.CmdBlitImage(commandBuffer, image.InternalHandle.Value,
                                     ImageLayout.TransferSrcOptimal,
                                     _swapchainImages[_nextImage],
                                     ImageLayout.TransferDstOptimal,
                                     1,
                                     srcBlitRegion,
                                     Filter.Linear);

            VulkanMemoryHelper.TransitionLayout(_device, commandBuffer,
                                                image.InternalHandle.Value, ImageLayout.TransferSrcOptimal,
                                                AccessFlags.AccessTransferReadBit,
                                                (ImageLayout)image.CurrentLayout,
                                                AccessFlags.AccessNoneKhr,
                                                renderTarget.MipLevels);
        }
Ejemplo n.º 3
0
        internal void TransitionLayout(ImageLayout destinationLayout, AccessFlags destinationAccessFlags)
        {
            var commandBuffer = _commandBufferPool.CreateCommandBuffer();

            commandBuffer.BeginRecording();

            VulkanMemoryHelper.TransitionLayout(_device, commandBuffer.InternalHandle, InternalHandle.Value,
                                                _currentLayout,
                                                _currentAccessFlags,
                                                destinationLayout, destinationAccessFlags,
                                                MipLevels);

            commandBuffer.EndRecording();

            commandBuffer.Submit();

            _currentLayout      = destinationLayout;
            _currentAccessFlags = destinationAccessFlags;
        }
Ejemplo n.º 4
0
        internal VulkanCommandBufferPool.VulkanCommandBuffer StartPresentation()
        {
            _nextImage = 0;
            while (true)
            {
                var acquireResult = _swapchainExtension.AcquireNextImage(
                    _device.InternalHandle,
                    _swapchain,
                    ulong.MaxValue,
                    _semaphorePair.ImageAvailableSemaphore,
                    new Fence(),
                    ref _nextImage);

                if (acquireResult == Result.ErrorOutOfDateKhr ||
                    acquireResult == Result.SuboptimalKhr)
                {
                    Recreate();
                }
                else
                {
                    acquireResult.ThrowOnError();
                    break;
                }
            }

            var commandBuffer = CommandBufferPool.CreateCommandBuffer();

            commandBuffer.BeginRecording();

            VulkanMemoryHelper.TransitionLayout(_device, commandBuffer.InternalHandle,
                                                _swapchainImages[_nextImage], ImageLayout.Undefined,
                                                AccessFlags.AccessNoneKhr,
                                                ImageLayout.TransferDstOptimal,
                                                AccessFlags.AccessTransferWriteBit,
                                                1);

            return(commandBuffer);
        }
Ejemplo n.º 5
0
        public unsafe void Initialize()
        {
            if (!InternalHandle.HasValue)
            {
                MipLevels = MipLevels != 0 ? MipLevels : (uint)Math.Floor(Math.Log(Math.Max(Size.Width, Size.Height), 2));

                var imageCreateInfo = new ImageCreateInfo
                {
                    SType         = StructureType.ImageCreateInfo,
                    ImageType     = ImageType.ImageType2D,
                    Format        = Format,
                    Extent        = new Extent3D((uint?)Size.Width, (uint?)Size.Height, 1),
                    MipLevels     = MipLevels,
                    ArrayLayers   = 1,
                    Samples       = SampleCountFlags.SampleCount1Bit,
                    Tiling        = Tiling,
                    Usage         = _imageUsageFlags,
                    SharingMode   = SharingMode.Exclusive,
                    InitialLayout = ImageLayout.Undefined,
                    Flags         = ImageCreateFlags.ImageCreateMutableFormatBit
                };

                _device.Api.CreateImage(_device.InternalHandle, imageCreateInfo, null, out var image).ThrowOnError();
                InternalHandle = image;

                _device.Api.GetImageMemoryRequirements(_device.InternalHandle, InternalHandle.Value,
                                                       out var memoryRequirements);

                var memoryAllocateInfo = new MemoryAllocateInfo
                {
                    SType           = StructureType.MemoryAllocateInfo,
                    AllocationSize  = memoryRequirements.Size,
                    MemoryTypeIndex = (uint)VulkanMemoryHelper.FindSuitableMemoryTypeIndex(
                        _physicalDevice,
                        memoryRequirements.MemoryTypeBits, MemoryPropertyFlags.MemoryPropertyDeviceLocalBit)
                };

                _device.Api.AllocateMemory(_device.InternalHandle, memoryAllocateInfo, null,
                                           out var imageMemory);

                _imageMemory = imageMemory;

                _device.Api.BindImageMemory(_device.InternalHandle, InternalHandle.Value, _imageMemory, 0);

                MemorySize = memoryRequirements.Size;

                var componentMapping = new ComponentMapping(
                    ComponentSwizzle.Identity,
                    ComponentSwizzle.Identity,
                    ComponentSwizzle.Identity,
                    ComponentSwizzle.Identity);

                AspectFlags = ImageAspectFlags.ImageAspectColorBit;

                var subresourceRange = new ImageSubresourceRange(AspectFlags, 0, MipLevels, 0, 1);

                var imageViewCreateInfo = new ImageViewCreateInfo
                {
                    SType            = StructureType.ImageViewCreateInfo,
                    Image            = InternalHandle.Value,
                    ViewType         = ImageViewType.ImageViewType2D,
                    Format           = Format,
                    Components       = componentMapping,
                    SubresourceRange = subresourceRange
                };

                _device.Api
                .CreateImageView(_device.InternalHandle, imageViewCreateInfo, null, out var imageView)
                .ThrowOnError();

                _imageView = imageView;

                _currentLayout = ImageLayout.Undefined;

                TransitionLayout(ImageLayout.ColorAttachmentOptimal, AccessFlags.AccessNoneKhr);
            }
        }