Ejemplo n.º 1
0
        internal static VulkanDisplay CreateDisplay(VulkanInstance instance, VulkanDevice device,
                                                    VulkanPhysicalDevice physicalDevice, VulkanSurface surface)
        {
            var swapchain = CreateSwapchain(instance, device, physicalDevice, surface, out var extent, null, true);

            return(new VulkanDisplay(instance, device, physicalDevice, surface, swapchain, extent));
        }
Ejemplo n.º 2
0
 public VulkanSurfaceRenderingSession(VulkanDisplay display, VulkanDevice device,
                                      VulkanSurfaceRenderTarget renderTarget, float scaling)
 {
     Display       = display;
     _device       = device;
     _renderTarget = renderTarget;
     Scaling       = scaling;
     Begin();
 }
Ejemplo n.º 3
0
        public unsafe VulkanSemaphorePair(VulkanDevice device)
        {
            _device = device;

            var semaphoreCreateInfo = new SemaphoreCreateInfo {
                SType = StructureType.SemaphoreCreateInfo
            };

            _device.Api.CreateSemaphore(_device.InternalHandle, semaphoreCreateInfo, null, out var semaphore).ThrowOnError();
            ImageAvailableSemaphore = semaphore;

            _device.Api.CreateSemaphore(_device.InternalHandle, semaphoreCreateInfo, null, out semaphore).ThrowOnError();
            RenderFinishedSemaphore = semaphore;
        }
Ejemplo n.º 4
0
        private VulkanDisplay(VulkanInstance instance, VulkanDevice device,
                              VulkanPhysicalDevice physicalDevice, VulkanSurface surface, SwapchainKHR swapchain,
                              Extent2D swapchainExtent)
        {
            _instance        = instance;
            _device          = device;
            _physicalDevice  = physicalDevice;
            _swapchain       = swapchain;
            _swapchainExtent = swapchainExtent;
            _surface         = surface;

            CreateSwapchainImages();

            _semaphorePair = new VulkanSemaphorePair(_device);

            CommandBufferPool = new VulkanCommandBufferPool(device, physicalDevice);
        }
Ejemplo n.º 5
0
        public VulkanImage(
            VulkanDevice device,
            VulkanPhysicalDevice physicalDevice,
            VulkanCommandBufferPool commandBufferPool,
            uint format,
            PixelSize size,
            uint mipLevels = 0)
        {
            _device            = device;
            _physicalDevice    = physicalDevice;
            _commandBufferPool = commandBufferPool;
            Format             = (Format)format;
            Size             = size;
            MipLevels        = mipLevels;
            _imageUsageFlags =
                ImageUsageFlags.ImageUsageColorAttachmentBit | ImageUsageFlags.ImageUsageTransferDstBit |
                ImageUsageFlags.ImageUsageTransferSrcBit | ImageUsageFlags.ImageUsageSampledBit;

            Initialize();
        }
Ejemplo n.º 6
0
        internal static unsafe void TransitionLayout(VulkanDevice device,
                                                     CommandBuffer commandBuffer,
                                                     Image image,
                                                     ImageLayout sourceLayout,
                                                     AccessFlags sourceAccessMask,
                                                     ImageLayout destinationLayout,
                                                     AccessFlags destinationAccessMask,
                                                     uint mipLevels)
        {
            var subresourceRange = new ImageSubresourceRange(ImageAspectFlags.ImageAspectColorBit, 0, mipLevels, 0, 1);

            var barrier = new ImageMemoryBarrier
            {
                SType               = StructureType.ImageMemoryBarrier,
                SrcAccessMask       = sourceAccessMask,
                DstAccessMask       = destinationAccessMask,
                OldLayout           = sourceLayout,
                NewLayout           = destinationLayout,
                SrcQueueFamilyIndex = Vk.QueueFamilyIgnored,
                DstQueueFamilyIndex = Vk.QueueFamilyIgnored,
                Image               = image,
                SubresourceRange    = subresourceRange
            };

            device.Api.CmdPipelineBarrier(
                commandBuffer,
                PipelineStageFlags.PipelineStageAllCommandsBit,
                PipelineStageFlags.PipelineStageAllCommandsBit,
                0,
                0,
                null,
                0,
                null,
                1,
                barrier);
        }
Ejemplo n.º 7
0
 public VulkanQueue(VulkanDevice device, Queue apiHandle)
 {
     Device         = device;
     InternalHandle = apiHandle;
 }
Ejemplo n.º 8
0
        private static unsafe SwapchainKHR CreateSwapchain(VulkanInstance instance, VulkanDevice device,
                                                           VulkanPhysicalDevice physicalDevice, VulkanSurface surface, out Extent2D swapchainExtent,
                                                           SwapchainKHR?oldswapchain = null, bool vsyncEnabled = true)
        {
            if (_swapchainExtension == null)
            {
                instance.Api.TryGetDeviceExtension(instance.InternalHandle, device.InternalHandle, out _swapchainExtension);
            }

            while (!surface.CanSurfacePresent(physicalDevice))
            {
                Thread.Sleep(16);
            }

            VulkanSurface.SurfaceExtension.GetPhysicalDeviceSurfaceCapabilities(physicalDevice.InternalHandle,
                                                                                surface.ApiHandle, out var capabilities);

            var imageCount = capabilities.MinImageCount + 1;

            if (capabilities.MaxImageCount > 0 && imageCount > capabilities.MaxImageCount)
            {
                imageCount = capabilities.MaxImageCount;
            }

            var surfaceFormat = surface.GetSurfaceFormat(physicalDevice);

            bool supportsIdentityTransform = capabilities.SupportedTransforms.HasFlag(SurfaceTransformFlagsKHR.SurfaceTransformIdentityBitKhr);
            bool isRotated = capabilities.CurrentTransform.HasFlag(SurfaceTransformFlagsKHR.SurfaceTransformRotate90BitKhr) ||
                             capabilities.CurrentTransform.HasFlag(SurfaceTransformFlagsKHR.SurfaceTransformRotate270BitKhr);

            swapchainExtent = GetSwapchainExtent(surface, capabilities);

            CompositeAlphaFlagsKHR compositeAlphaFlags = GetSuitableCompositeAlphaFlags(capabilities);

            PresentModeKHR presentMode = GetSuitablePresentMode(physicalDevice, surface, vsyncEnabled);

            var swapchainCreateInfo = new SwapchainCreateInfoKHR
            {
                SType           = StructureType.SwapchainCreateInfoKhr,
                Surface         = surface.ApiHandle,
                MinImageCount   = imageCount,
                ImageFormat     = surfaceFormat.Format,
                ImageColorSpace = surfaceFormat.ColorSpace,
                ImageExtent     = swapchainExtent,
                ImageUsage      =
                    ImageUsageFlags.ImageUsageColorAttachmentBit | ImageUsageFlags.ImageUsageTransferDstBit,
                ImageSharingMode = SharingMode.Exclusive,
                ImageArrayLayers = 1,
                PreTransform     = supportsIdentityTransform && isRotated ?
                                   SurfaceTransformFlagsKHR.SurfaceTransformIdentityBitKhr :
                                   capabilities.CurrentTransform,
                CompositeAlpha = compositeAlphaFlags,
                PresentMode    = presentMode,
                Clipped        = true,
                OldSwapchain   = oldswapchain ?? new SwapchainKHR()
            };

            _swapchainExtension.CreateSwapchain(device.InternalHandle, swapchainCreateInfo, null, out var swapchain)
            .ThrowOnError();

            if (oldswapchain != null)
            {
                _swapchainExtension.DestroySwapchain(device.InternalHandle, oldswapchain.Value, null);
            }

            return(swapchain);
        }