private void FramebufferResize(Vector2D <int> obj) { if (obj.X <= 0 || obj.Y <= 0) { return; } _khrSwapchain.DestroySwapchain(_logicalDevice, _swapchain, null); _khrSwapchain.CreateSwapchain(_logicalDevice, new SwapchainCreateInfoKHR(surface: _surface, minImageCount: 3, imageFormat: _swapchainFormat, imageColorSpace: _swapchainColorSpace, imageExtent: new Extent2D((uint)obj.X, (uint)obj.Y), imageArrayLayers: 1, imageUsage: ImageUsageFlags.ImageUsageTransferDstBit | ImageUsageFlags.ImageUsageStorageBit, imageSharingMode: SharingMode.Exclusive, queueFamilyIndexCount: 0, pQueueFamilyIndices: null, // ignored due to SharingMode.Exclusive presentMode: _presentMode, clipped: false, preTransform: SurfaceTransformFlagsKHR.SurfaceTransformIdentityBitKhr, compositeAlpha: CompositeAlphaFlagsKHR.CompositeAlphaOpaqueBitKhr), null, out _swapchain) .ThrowCode(); uint swapchainCount = 0; _khrSwapchain.GetSwapchainImages(_logicalDevice, _swapchain, ref swapchainCount, null).ThrowCode(); _swapchainImages = new Image[swapchainCount]; fixed(Image *p = _swapchainImages) _khrSwapchain.GetSwapchainImages(_logicalDevice, _swapchain, ref swapchainCount, p).ThrowCode(); _renderGraph.ChangeTargetImages((Vector2D <uint>)obj, _swapchainImages, _swapchainFormat, _swapchainColorSpace); }
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); }