vsg  1.1.0
VulkanSceneGraph library
Window.h
1 #pragma once
2 
3 /* <editor-fold desc="MIT License">
4 
5 Copyright(c) 2018 Robert Osfield
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8 
9 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10 
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 </editor-fold> */
14 
15 #include <vsg/app/WindowTraits.h>
16 #include <vsg/core/ref_ptr.h>
17 #include <vsg/ui/UIEvent.h>
18 #include <vsg/vk/CommandBuffer.h>
19 #include <vsg/vk/CommandPool.h>
20 #include <vsg/vk/DeviceMemory.h>
21 #include <vsg/vk/Framebuffer.h>
22 #include <vsg/vk/Semaphore.h>
23 
24 namespace vsg
25 {
26 
30  class VSG_DECLSPEC Window : public Inherit<Object, Window>
31  {
32  public:
33  Window(const Window&) = delete;
34  Window& operator=(const Window&) = delete;
35 
38 
39  virtual const char* instanceExtensionSurfaceName() const = 0;
40 
41  virtual bool valid() const { return false; }
42 
43  virtual bool visible() const { return valid(); }
44 
47  virtual void releaseWindow() {}
48 
51  virtual void releaseConnection() {}
52 
54  UIEvents bufferedEvents;
55 
57  virtual bool pollEvents(UIEvents& events);
58 
59  virtual void resize() {}
60 
61  ref_ptr<WindowTraits> traits() { return _traits; }
62  const ref_ptr<WindowTraits> traits() const { return _traits; }
63 
64  const VkExtent2D& extent2D() const { return _extent2D; }
65 
66  vec4& clearColor() { return _clearColor; }
67  const vec4& clearColor() const { return _clearColor; }
68 
69  VkSurfaceFormatKHR surfaceFormat();
70 
71  VkFormat depthFormat();
72 
73  VkSampleCountFlagBits framebufferSamples() const { return _framebufferSamples; }
74 
75  void setInstance(ref_ptr<Instance> instance);
76  ref_ptr<Instance> getInstance() { return _instance; }
77  ref_ptr<Instance> getOrCreateInstance();
78 
79  void setSurface(ref_ptr<Surface> surface);
80  ref_ptr<Surface> getSurface() { return _surface; }
81  ref_ptr<Surface> getOrCreateSurface();
82 
83  void setPhysicalDevice(ref_ptr<PhysicalDevice> physicalDevice);
84  ref_ptr<PhysicalDevice> getPhysicalDevice() { return _physicalDevice; }
85  ref_ptr<PhysicalDevice> getOrCreatePhysicalDevice();
86 
87  void setDevice(ref_ptr<Device> device);
88  ref_ptr<Device> getDevice() { return _device; }
89  ref_ptr<Device> getOrCreateDevice();
90 
91  void setRenderPass(ref_ptr<RenderPass> renderPass);
92  ref_ptr<RenderPass> getRenderPass() { return _renderPass; }
93  ref_ptr<RenderPass> getOrCreateRenderPass();
94 
95  ref_ptr<Swapchain> getSwapchain() { return _swapchain; }
96  ref_ptr<Swapchain> getOrCreateSwapchain();
97 
98  ref_ptr<Image> getDepthImage() { return _depthImage; }
99  ref_ptr<Image> getOrCreateDepthImage();
100 
101  ref_ptr<ImageView> getDepthImageView() { return _depthImageView; }
102  ref_ptr<ImageView> getOrCreateDepthImageView();
103 
104  size_t numFrames() const { return _frames.size(); }
105 
106  ref_ptr<ImageView> imageView(size_t i) { return _frames[i].imageView; }
107  const ref_ptr<ImageView> imageView(size_t i) const { return _frames[i].imageView; }
108 
109  ref_ptr<Framebuffer> framebuffer(size_t i) { return _frames[i].framebuffer; }
110  const ref_ptr<Framebuffer> framebuffer(size_t i) const { return _frames[i].framebuffer; }
111 
113  VkResult acquireNextImage(uint64_t timeout = std::numeric_limits<uint64_t>::max());
114 
116  size_t imageIndex(size_t relativeFrameIndex = 0) const { return relativeFrameIndex < _indices.size() ? _indices[relativeFrameIndex] : _indices.size(); }
117 
118  bool debugLayersEnabled() const { return _traits->debugLayer; }
119 
120  struct Frame
121  {
122  ref_ptr<ImageView> imageView;
123  ref_ptr<Framebuffer> framebuffer;
124  ref_ptr<Semaphore> imageAvailableSemaphore;
125  };
126 
127  using Frames = std::vector<Frame>;
128 
129  Frame& frame(size_t i) { return _frames[i]; }
130  Frames& frames() { return _frames; }
131 
132  protected:
133  Window(ref_ptr<WindowTraits> traits);
134 
135  virtual ~Window();
136 
137  virtual void _initSurface() = 0;
138  void _initFormats();
139  void _initInstance();
140  void _initPhysicalDevice();
141  void _initDevice();
142  void _initRenderPass();
143  void _initSwapchain();
144 
145  virtual void clear();
146  void share(ref_ptr<Device> device);
147  void buildSwapchain();
148 
149  ref_ptr<WindowTraits> _traits;
150 
151  VkExtent2D _extent2D;
152  vec4 _clearColor;
153  VkSurfaceFormatKHR _imageFormat;
154  VkFormat _depthFormat;
155 
156  VkSampleCountFlagBits _framebufferSamples;
157 
158  ref_ptr<Instance> _instance;
159  ref_ptr<PhysicalDevice> _physicalDevice;
160  ref_ptr<Device> _device;
161  ref_ptr<Surface> _surface;
162  ref_ptr<Swapchain> _swapchain;
163  ref_ptr<RenderPass> _renderPass;
164 
165  ref_ptr<Image> _depthImage;
166  ref_ptr<ImageView> _depthImageView;
167 
168  // only used when multisampling is required
169  ref_ptr<Image> _multisampleImage;
170  ref_ptr<ImageView> _multisampleImageView;
171 
172  // only used when multisampling and with Traits::requiresDepthRead == true
173  ref_ptr<Image> _multisampleDepthImage;
174  ref_ptr<ImageView> _multisampleDepthImageView;
175 
176  ref_ptr<Semaphore> _availableSemaphore;
177 
178  Frames _frames;
179  std::vector<size_t> _indices;
180  };
181  VSG_type_name(vsg::Window);
182 
183  using Windows = std::vector<ref_ptr<Window>>;
184 
185 } // namespace vsg
Definition: Inherit.h:28
Definition: Window.h:31
VkResult acquireNextImage(uint64_t timeout=std::numeric_limits< uint64_t >::max())
call vkAquireNextImageKHR to find the next imageIndex of the swapchain images/framebuffers
virtual void releaseWindow()
Definition: Window.h:47
static ref_ptr< Window > create(vsg::ref_ptr< WindowTraits > traits)
static Window::create(traits) method creates a platform specific Window instance based on specified w...
size_t imageIndex(size_t relativeFrameIndex=0) const
get the image index for specified relative frame index, a 0 value is the current frame being rendered...
Definition: Window.h:116
virtual bool pollEvents(UIEvents &events)
get the list of events since the last pollEvents() call by splicing bufferEvents with polled windowin...
virtual void releaseConnection()
Definition: Window.h:51
UIEvents bufferedEvents
events buffered since the last pollEvents.
Definition: Window.h:54
Definition: ref_ptr.h:22
Definition: Window.h:121