1 module sbylib.wrapper.vulkan.swapchain;
2 
3 import std;
4 import erupted;
5 import sbylib.wrapper.vulkan.enums;
6 import sbylib.wrapper.vulkan.device;
7 import sbylib.wrapper.vulkan.fence;
8 import sbylib.wrapper.vulkan.image;
9 import sbylib.wrapper.vulkan.surface;
10 import sbylib.wrapper.vulkan.util;
11 
12 class Swapchain {
13     static struct CreateInfo {
14         @vkProp() {
15             immutable VkSwapchainCreateFlagsKHR flags;
16             Surface surface;
17             immutable uint minImageCount;
18             immutable VkFormat imageFormat;
19             immutable VkColorSpaceKHR imageColorSpace;
20             immutable VkExtent2D imageExtent;
21             immutable uint imageArrayLayers;
22             immutable ImageUsage imageUsage;
23             immutable SharingMode imageSharingMode;
24             immutable SurfaceTransform preTransform;
25             immutable CompositeAlpha compositeAlpha;
26             immutable PresentMode presentMode;
27             immutable bool clipped;
28             Swapchain oldSwapchain;
29         }
30 
31         @vkProp("pQueueFamilyIndices", "queueFamilyIndexCount") {
32             const uint[] queueFamilyIndices;
33         }
34 
35         mixin VkTo!(VkSwapchainCreateInfoKHR);
36     }
37 
38     private Device device;
39     private VkSwapchainKHR swapchain;
40 
41     mixin ImplNameSetter!(device, swapchain, DebugReportObjectType.Swapchain);
42 
43     this(Device device, CreateInfo _info) {
44         import std.exception : enforce;
45 
46         this.device = device;
47 
48         auto info = _info.vkTo();
49 
50         enforceVK(vkCreateSwapchainKHR(device.device, &info, null, &swapchain));
51         enforce(swapchain != VK_NULL_HANDLE);
52     }
53 
54     ~this() {
55         vkDestroySwapchainKHR(device.device, swapchain, null);
56     }
57 
58     mixin VkTo!(VkSwapchainKHR);
59 
60     Image[] getImages() {
61         uint numSwapchainImage;
62         vkGetSwapchainImagesKHR(device.device, swapchain, &numSwapchainImage, null);
63 
64         VkImage[] swapchainImages = new VkImage[numSwapchainImage];
65         vkGetSwapchainImagesKHR(device.device, swapchain,
66                 &numSwapchainImage, swapchainImages.ptr);
67 
68         Image[] result = new Image[numSwapchainImage];
69         foreach (i; 0..numSwapchainImage)
70             result[i] = new Image(swapchainImages[i]);
71 
72         return result;
73     }
74 
75     uint acquireNextImageIndex(ulong timeout, VkSemaphore semaphore, Fence fence) 
76         in (device !is null)
77         in (fence !is null)
78     {
79         uint imageIndex;
80         enforceVK(vkAcquireNextImageKHR(device.device, swapchain, timeout,
81                 semaphore, fence.fence, &imageIndex));
82         return imageIndex;
83     }
84 }