Line data Source code
1 : /**
2 : Copyright (c) 2022 Roman Katuntsev <sbkarr@stappler.org>
3 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev>
4 :
5 : Permission is hereby granted, free of charge, to any person obtaining a copy
6 : of this software and associated documentation files (the "Software"), to deal
7 : in the Software without restriction, including without limitation the rights
8 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 : copies of the Software, and to permit persons to whom the Software is
10 : furnished to do so, subject to the following conditions:
11 :
12 : The above copyright notice and this permission notice shall be included in
13 : all copies or substantial portions of the Software.
14 :
15 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 : THE SOFTWARE.
22 : **/
23 :
24 : #ifndef XENOLITH_BACKEND_VKGUI_XLVKSWAPCHAIN_H_
25 : #define XENOLITH_BACKEND_VKGUI_XLVKSWAPCHAIN_H_
26 :
27 : #include "XLVkDevice.h"
28 : #include "XLVkObject.h"
29 : #include "XLCoreImageStorage.h"
30 :
31 : namespace STAPPLER_VERSIONIZED stappler::xenolith::vk {
32 :
33 : class SwapchainImage;
34 :
35 : class Surface : public Ref {
36 : public:
37 : virtual ~Surface();
38 :
39 : bool init(Instance *instance, VkSurfaceKHR surface, Ref * = nullptr);
40 :
41 40 : VkSurfaceKHR getSurface() const { return _surface; }
42 :
43 : protected:
44 : Rc<Ref> _window;
45 : Rc<Instance> _instance;
46 : VkSurfaceKHR _surface = VK_NULL_HANDLE;
47 : };
48 :
49 : class SwapchainHandle : public core::Object {
50 : public:
51 : using ImageStorage = core::ImageStorage;
52 :
53 : struct SwapchainImageData {
54 : Rc<Image> image;
55 : Map<ImageViewInfo, Rc<ImageView>> views;
56 : };
57 :
58 : struct SwapchainAcquiredImage : public Ref {
59 : uint32_t imageIndex;
60 : const SwapchainImageData *data;
61 : Rc<Semaphore> sem;
62 : Rc<SwapchainHandle> swapchain;
63 :
64 4650 : SwapchainAcquiredImage(uint32_t idx, const SwapchainImageData *data, Rc<Semaphore> &&sem, Rc<SwapchainHandle> &&swapchain)
65 4650 : : imageIndex(idx), data(data), sem(move(sem)), swapchain(move(swapchain)) { }
66 : };
67 :
68 : virtual ~SwapchainHandle();
69 :
70 : bool init(Device &dev, const core::SurfaceInfo &, const core::SwapchainConfig &, ImageInfo &&, core::PresentMode,
71 : Surface *, uint32_t families[2], SwapchainHandle * = nullptr);
72 :
73 : core::PresentMode getPresentMode() const { return _presentMode; }
74 10 : core::PresentMode getRebuildMode() const { return _rebuildMode; }
75 15677 : const ImageInfo &getImageInfo() const { return _imageInfo; }
76 9280 : const core::SwapchainConfig &getConfig() const { return _config; }
77 4640 : const core::SurfaceInfo &getSurfaceInfo() const { return _surfaceInfo; }
78 10 : VkSwapchainKHR getSwapchain() const { return _swapchain; }
79 211505 : uint32_t getAcquiredImagesCount() const { return _acquiredImages; }
80 : uint64_t getPresentedFramesCount() const { return _presentedFrames; }
81 20 : const Vector<SwapchainImageData> &getImages() const { return _images; }
82 :
83 : bool isDeprecated();
84 : bool isOptimal() const;
85 :
86 : // returns true if it was first deprecation
87 : bool deprecate(bool fast);
88 :
89 : Rc<SwapchainAcquiredImage> acquire(bool lockfree, const Rc<Fence> &fence);
90 :
91 : VkResult present(DeviceQueue &queue, const Rc<ImageStorage> &);
92 : void invalidateImage(const ImageStorage *);
93 :
94 : Rc<Semaphore> acquireSemaphore();
95 : void releaseSemaphore(Rc<Semaphore> &&);
96 :
97 : Rc<core::ImageView> makeView(const Rc<core::ImageObject> &, const ImageViewInfo &);
98 :
99 : protected:
100 : using core::Object::init;
101 :
102 : ImageViewInfo getSwapchainImageViewInfo(const ImageInfo &image) const;
103 :
104 : Device *_device = nullptr;
105 : bool _deprecated = false;
106 : core::PresentMode _presentMode = core::PresentMode::Unsupported;
107 : ImageInfo _imageInfo;
108 : core::SurfaceInfo _surfaceInfo;
109 : core::SwapchainConfig _config;
110 : VkSwapchainKHR _swapchain = VK_NULL_HANDLE;
111 : Vector<SwapchainImageData> _images;
112 : uint32_t _acquiredImages = 0;
113 : uint64_t _presentedFrames = 0;
114 : uint64_t _presentTime = 0;
115 : core::PresentMode _rebuildMode = core::PresentMode::Unsupported;
116 :
117 : Mutex _resourceMutex;
118 : Vector<Rc<Semaphore>> _semaphores;
119 : Vector<Rc<Semaphore>> _presentSemaphores;
120 : Rc<Surface> _surface;
121 : };
122 :
123 : class SwapchainImage : public core::ImageStorage {
124 : public:
125 : enum class State {
126 : Initial,
127 : Submitted,
128 : Presented,
129 : };
130 :
131 : virtual ~SwapchainImage();
132 :
133 : virtual bool init(Rc<SwapchainHandle> &&, uint64_t frameOrder, uint64_t presentWindow);
134 : virtual bool init(Rc<SwapchainHandle> &&, const SwapchainHandle::SwapchainImageData &, Rc<Semaphore> &&);
135 :
136 : virtual void cleanup() override;
137 : virtual void rearmSemaphores(core::Loop &) override;
138 : virtual void releaseSemaphore(core::Semaphore *) override;
139 :
140 4640 : virtual bool isSemaphorePersistent() const override { return false; }
141 :
142 : virtual ImageInfoData getInfo() const override;
143 :
144 : virtual Rc<core::ImageView> makeView(const ImageViewInfo &) override;
145 :
146 : void setImage(Rc<SwapchainHandle> &&, const SwapchainHandle::SwapchainImageData &, const Rc<Semaphore> &);
147 :
148 0 : uint64_t getOrder() const { return _order; }
149 355512 : uint64_t getPresentWindow() const { return _presentWindow; }
150 :
151 : void setPresented();
152 10 : bool isPresented() const { return _state == State::Presented; }
153 4630 : bool isSubmitted() const { return _state == State::Submitted || _state == State::Presented; }
154 :
155 9270 : const Rc<SwapchainHandle> &getSwapchain() const { return _swapchain; }
156 :
157 : void invalidateImage();
158 : void invalidateSwapchain();
159 :
160 : protected:
161 : using core::ImageStorage::init;
162 :
163 : uint64_t _order = 0;
164 : uint64_t _presentWindow = 0;
165 : State _state = State::Initial;
166 : Rc<SwapchainHandle> _swapchain;
167 : };
168 :
169 : }
170 :
171 : #endif /* XENOLITH_BACKEND_VKGUI_XLVKSWAPCHAIN_H_ */
|