Kale
Loading...
Searching...
No Matches
CoreSetup.cpp
Go to the documentation of this file.
1/*
2 Copyright 2022 Rishi Challa
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#ifdef KALE_VULKAN
18
19#include "Core.hpp"
20
24
25#include <stdexcept>
26#include <exception>
27#include <algorithm>
28#include <limits>
29
30using namespace Kale;
31using namespace Kale::Vulkan;
32
37void Core::setupCore(std::optional<uint32_t> gpuID) {
38
39 try {
41
42 #ifdef KALE_DEBUG
43 setupDebugMessageCallback();
44 #endif
45
46 // Create window surface for rendering
48
49 // Choose the physical device and set it up
50 chooseDevice(gpuID);
51
53 // pipeline.init("shader.vert.spv", "shader.frag.spv", device);
54 // swapchain.createFrameBuffers(pipeline.renderPass);
56 // commandPool.init(device, Vector4f(0.5f, 0.0f, 1.0f, 1.0f));
57 }
58 catch (const std::exception& e) {
59 console.error(e.what());
60 exit(0);
61 }
62}
63
64
69 try {
70 #ifdef KALE_DEBUG
71 destroyDebugMessageCallback();
72 #endif
73
74 commandPool.reset();
76 surface.reset();
77 instance.reset();
78 }
79 catch (const std::exception& e) {
80 console.error(e.what());
81 exit(0);
82 }
83}
84
89 vk::ApplicationInfo appInfo;
90 appInfo.pApplicationName = mainApp->applicationName.c_str();
91 appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
92 appInfo.pEngineName = "No Engine";
93 appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
94 appInfo.apiVersion = VK_API_VERSION_1_2;
95
96 vk::InstanceCreateInfo createInfo;
97 createInfo.pApplicationInfo = &appInfo;
98
99 std::vector<const char*> extensions;
100
101 // Load all required extensions
102 try {
103 // Get all available extensions
104 std::vector<vk::ExtensionProperties> availableExtensions(vk::enumerateInstanceExtensionProperties());
105
106 // Get all extensions given the available required and requested
107 extensions = getExtensions<vk::ExtensionProperties>(availableExtensions, requiredInstanceExtensions,
108 requestedInstanceExtensions, [](const vk::ExtensionProperties& p) {
109
110 // Map the vulkan extension type to a const char*
111 return std::string(p.extensionName);
112 });
113
114 // Add the lower windowing API required extensions, usually don't need to check for support for these
115 std::vector<const char*> windowRequiredExtensions(mainApp->getWindow().getInstanceExtensions());
116 extensions.reserve(extensions.size() + windowRequiredExtensions.size());
117 extensions.insert(extensions.end(), windowRequiredExtensions.begin(), windowRequiredExtensions.end());
118
119 // Add the extensions to the create info
120 createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
121 createInfo.ppEnabledExtensionNames = extensions.data();
122 }
123 catch (const std::exception& e) {
124 console.error(e.what());
125 exit(0);
126 }
127
128 // Validation Layers
129 #ifdef KALE_DEBUG
130
131 std::vector<const char*> layers;
132
133 try {
134
135 // Get all available layers
136 std::vector<vk::LayerProperties> availableLayers(vk::enumerateInstanceLayerProperties());
137
138 // Get all layers given the available required and requested
139 layers = getExtensions<vk::LayerProperties>(availableLayers, requiredValidationLayers,
140 requestedValidationLayers, [](const vk::LayerProperties& p) {
141
142 // Map the vulkan layer type to a const char*
143 return std::string(p.layerName);
144 });
145
146 // Add the layers to the create info
147 createInfo.enabledLayerCount = static_cast<uint32_t>(layers.size());
148 createInfo.ppEnabledLayerNames = layers.data();
149 }
150 catch (const std::exception&) {
151 console.warn("Validation Layers not Available");
152 createInfo.enabledLayerCount = 0;
153 }
154
155 #else
156
157 createInfo.enabledLayerCount = 0;
158
159 #endif
160
161 instance = vk::createInstanceUnique(createInfo);
162}
163
168void Core::chooseDevice(std::optional<uint32_t> gpuID) {
169 // Choose the GPU, useGPU will handle logical device creation
170 if (gpuID.has_value()) device.init(gpuID.value());
171 else {
172 std::vector<vk::PhysicalDevice> devices(Device::availableDevices());
173 if (devices.empty()) throw std::runtime_error("No Available GPU Found");
174 device.init(devices[0]);
175 }
176}
177
182 vk::CommandPoolCreateInfo createInfo(vk::CommandPoolCreateFlags(),
184 commandPool = device.logicalDevice->createCommandPoolUnique(createInfo);
185}
186
187#endif
const std::string applicationName
Window & getWindow() noexcept
void warn(T msg)
Definition Logger.hpp:182
void error(T msg)
Definition Logger.hpp:198
static Device device
Definition Core.hpp:99
static vk::UniqueInstance instance
Definition Core.hpp:89
static vk::UniqueSurfaceKHR surface
Definition Core.hpp:94
static void chooseDevice(std::optional< uint32_t > gpuID)
static void cleanupCore()
Definition CoreSetup.cpp:68
static void createCommandPool()
static vk::UniqueCommandPool commandPool
Definition Core.hpp:109
static void setupCore(std::optional< uint32_t > gpuID=std::optional< uint32_t >())
Definition CoreSetup.cpp:37
static SwapChain swapchain
Definition Core.hpp:104
static void createInstance()
Definition CoreSetup.cpp:88
void init(const vk::PhysicalDevice &device)
Definition Device.cpp:83
vk::UniqueDevice logicalDevice
Definition Device.hpp:59
QueueFamilyIndices queueIndices
Definition Device.hpp:79
static std::vector< vk::PhysicalDevice > availableDevices()
Definition Device.cpp:191
std::optional< uint32_t > graphicsFamilyIndex
void init(Device &device) override
Definition SwapChain.cpp:41
std::vector< const char * > getInstanceExtensions() const
void createWindowSurface(const vk::UniqueInstance &instance, vk::UniqueSurfaceKHR &surface) const
const std::vector< std::string > requestedInstanceExtensions
const std::vector< std::string > requiredInstanceExtensions
Application * mainApp
Logger console
Definition Logger.hpp:317