SimpleGL  1.1.0
A framework for platform independent rendering
vulkan_context.cpp
Go to the documentation of this file.
1 
9 
11 #ifdef NDEBUG
12 const bool debugMode = false;
13 #else
14 const bool debugMode = true;
15 #endif
16 
17 namespace sgl {
18  VulkanContext* VulkanContext::current = nullptr;
19 
21  return current;
22  }
23 
25  /* Only set if context is not a nullptr */
26  if (context != nullptr) {
27  current = context;
28  }
29  }
30 
32  /* Create the Vulkan instance */
33  instance = new Instance(debugMode);
34  }
35 
37  /* Wait for completion */
38  vkDeviceWaitIdle(device->getHandle());
39 
40  /* Delete the synchronization objects */
41  delete renderFence;
42 
43  /* Delete the descriptor pool */
44  delete descriptorPool;
45 
46  /* Delete the command pool */
47  delete commandPool;
48 
49  /* Delete the framebuffers */
50  for (uint32_t i = 0; i < getSwapchainCount(); i++) {
51  delete swapchainFramebuffers[i];
52  }
53 
54  /* Delete the swapchain */
55  delete swapchain;
56 
57  /* Delete the logical device */
58  delete device;
59 
60  /* Delete debug report callback */
61  delete debugReportCallback;
62 
63  /* Delete Vulkan instance */
64  delete instance;
65 
66  /* Set context to nullptr if this was the current context */
67  if (VulkanContext::current == this) {
68  VulkanContext::current = nullptr;
69  }
70  }
71 
73  return instance;
74  }
75 
77  return debugReportCallback;
78  }
79 
80  std::vector<PhysicalDevice*> VulkanContext::getAvailablePhysicalDevices() {
81  return availablePhysicalDevices;
82  }
83 
85  return physicalDevice;
86  }
87 
89  return device;
90  }
91 
93  return swapchain;
94  }
95 
97  return swapchain->getSwapchainData().images.size();
98  }
99 
101  return swapchain->getSwapchainData().index;
102  }
103 
105  return swapchainFramebuffers[getSwapchainIndex()];
106  }
107 
109  return commandPool;
110  }
111 
113  return descriptorPool;
114  }
115 
117  return renderFence;
118  }
119 
121  /* Setup a debug report callback */
122  if (debugMode) {
123  debugReportCallback = new DebugReportCallback();
124  DebugReportCallback::setCurrent(debugReportCallback);
125  } else {
126  debugReportCallback = nullptr;
127  }
128  }
129 
131  /* Pick a physical device */
132  availablePhysicalDevices = PhysicalDevice::getAvailablePhysicalDevices(surface);
133  physicalDevice = PhysicalDevice::getSuitablePhysicalDevice(availablePhysicalDevices);
134  }
135 
137  /* Create a logical device */
138  device = new Device(debugMode);
139  }
140 
141  void VulkanContext::createSwapchain(Surface* surface, glm::ivec2 size) {
142  /* Create a swapchain */
143  swapchain = new Swapchain(surface, size);
144  }
145 
147  /* Resize the framebuffer vector */
148  swapchainFramebuffers.resize(getSwapchainCount());
149 
150  /* Create a framebuffer for each swapchain image */
151  for (uint32_t i = 0; i < getSwapchainCount(); i++) {
152  swapchainFramebuffers[i] = new FramebufferVK(renderPass, swapchain->getSwapchainData().imageViews[i]);
153  }
154  }
155 
157  /* Create a command pool */
158  commandPool = new CommandPool();
159  }
160 
162  /* Create a descriptor pool */
163  descriptorPool = new DescriptorPool();
164  }
165 
167  /* Create render fence */
168  renderFence = new Fence();
169  }
170 }
This class wraps a Vulkan instance.
This class wraps a Vulkan descriptor pool.
static void setCurrent(DebugReportCallback *callback)
Sets the current debug report callback.
Device * getDevice()
Returns the logical device of this context.
std::vector< VkImage > images
The available swapchain images.
Definition: swapchain.hpp:31
std::vector< PhysicalDevice * > getAvailablePhysicalDevices()
Returns the available physical devices of this context.
This class wraps a Vulkan physical device and represents a graphics card.
Swapchain * getSwapchain()
Returns the swapchain of this context.
static VulkanContext * getCurrent()
Returns the current Vulkan context.
PhysicalDevice * getPhysicalDevice()
Returns the physical device of this context.
uint32_t getSwapchainCount()
Returns the number of swapchain images.
void createCommandPool()
Creates the command pool for this context.
This class contains all relevant Vulkan objects to setup a Vulkan application.
SwapchainData getSwapchainData()
Returns the swapchain data.
Definition: swapchain.cpp:159
static std::vector< PhysicalDevice * > getAvailablePhysicalDevices(Surface *surface)
Returns a list of available physical devices.
VkDevice getHandle()
Returns the handle of the logical device.
Generic namespace for the SimpleGL framework.
Definition: application.hpp:18
void createFramebuffers(RenderPass *renderPass)
Creates the swapchain framebuffers for this context.
CommandPool * getCommandPool()
Returns the command pool of this context.
This class wraps a Vulkan command pool.
This class wraps a Vulkan framebuffer.
Definition: framebuffer.hpp:44
void createSwapchain(Surface *surface, glm::ivec2 size)
Creates a swapchain for this context.
static PhysicalDevice * getSuitablePhysicalDevice(std::vector< PhysicalDevice * > physicalDevices)
Returns the most suitable physical device.
Instance * getInstance()
Returns the Vulkan instance of this context.
Fence * getRenderFence()
Returns the current fence for signaling that a command buffer has finished rendering.
static void setCurrent(VulkanContext *context)
Sets the current Vulkan context.
const bool debugMode
Tells if the library is compiled in debug mode.
This class wraps a Vulkan swapchain.
Definition: swapchain.hpp:51
void setupDebugCallback()
Creates a debug report callback, if compiled in debug mode.
std::vector< VkImageView > imageViews
The image views for the swapchain images.
Definition: swapchain.hpp:34
DescriptorPool * getDescriptorPool()
Returns the descriptor pool of this context.
void createLogicalDevice()
Creates the logical device for rendering.
void createDescriptorPool()
Create the descriptor pool for this context.
uint32_t getSwapchainIndex()
Returns the current swapchain image index.
~VulkanContext(void)
Destroys the Vulkan context.
This class wraps a Vulkan debug report callback.
This class wraps a Vulkan render pass.
Definition: render_pass.hpp:22
This class wraps a Vulkan fence.
VulkanContext(void)
Creates a new Vulkan context.
void pickPhysicalDevice(Surface *surface)
Enumerates all available physical devices and picks a suitable physical device.
This class wraps a Vulkan surface.
Definition: surface.hpp:25
uint32_t index
The current swapchain image index.
Definition: swapchain.hpp:43
DebugReportCallback * getDebugReportCallback()
Returns the debug report callback of this context.
This class wraps a Vulkan logical device.
void createRenderFence()
Creates the render fence.
FramebufferVK * getSwapchainFramebuffer()
Returns the current swapchain framebuffers of this context.