Chronos 0.0
A advanced 2D rendering and animation system
Loading...
Searching...
No Matches
Chronos::Engine::Device Class Reference

This initializes, manages and destroys the logical and physical devices(GPU). More...

#include <device.hpp>

Public Member Functions

void init (VkInstance instance, VkSurfaceKHR surface)
 This is the function that initializes the devices and queues.
 
void destroy ()
 This is the function that destroys the device.
 

Public Attributes

VkDevice device
 This is the logical device that is used by Vulkan.
 
VkPhysicalDevice physicalDevice
 This is the physical device that is used by Vulkan.
 
VkQueue graphicsQueue
 This is the queue that is used for graphics rendering.
 
VkQueue presentQueue
 This is the queue that is used for presentation of the rendered frames.
 
VkSampleCountFlagBits msaaSamples = VK_SAMPLE_COUNT_1_BIT
 This sets the MSAA samples count.
 
VkSampleCountFlagBits maxMsaaSamples = VK_SAMPLE_COUNT_1_BIT
 

Private Member Functions

void pickPhysicalDevice (VkInstance instance, VkSurfaceKHR surface)
 This is the function that picks the best device based on the requirements and features that we require.
 
void createLogicalDevice (VkSurfaceKHR surface)
 This creates the logical device.
 

Detailed Description

This initializes, manages and destroys the logical and physical devices(GPU).

It also manages the various queues that are used for rendering and presentation. MSAA is also handled here due to the fact that it is a device managed property.

Definition at line 47 of file device.hpp.

Member Function Documentation

◆ createLogicalDevice()

void Chronos::Engine::Device::createLogicalDevice ( VkSurfaceKHR  surface)
private

This creates the logical device.

Along with creating the logical device, it creates the queues that are used for rendering and presentation. anisotropy and other features are also enabled here.

Parameters
surfaceThe surface that is used by the application for presenting the rendered images.

Definition at line 182 of file device.cpp.

183{
184 QueueFamilyIndices indices = findQueueFamilies(physicalDevice, surface);
185
186 std::vector<VkDeviceQueueCreateInfo> queueCreateInfos;
187 std::set<uint32_t> uniqueQueueFamilies
188 = { indices.graphicsFamily.value(), indices.presentFamily.value() };
189
190 float queuePriority = 1.0f;
191 for (uint32_t queueFamily : uniqueQueueFamilies) {
192 VkDeviceQueueCreateInfo queueCreateInfo {};
193 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
194 queueCreateInfo.queueFamilyIndex = queueFamily;
195 queueCreateInfo.queueCount = 1;
196 queueCreateInfo.pQueuePriorities = &queuePriority;
197 queueCreateInfos.push_back(queueCreateInfo);
198 }
199
200 VkPhysicalDeviceFeatures deviceFeatures {};
201 deviceFeatures.samplerAnisotropy = VK_TRUE;
202 deviceFeatures.sampleRateShading = VK_TRUE;
203
204 VkDeviceCreateInfo createInfo {};
205 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
206 createInfo.pQueueCreateInfos = queueCreateInfos.data();
207 createInfo.queueCreateInfoCount
208 = static_cast<uint32_t>(queueCreateInfos.size());
209 createInfo.pEnabledFeatures = &deviceFeatures;
210 createInfo.enabledExtensionCount
211 = static_cast<uint32_t>(deviceExtensions.size());
212 createInfo.ppEnabledExtensionNames = deviceExtensions.data();
213 createInfo.enabledLayerCount = 0;
214#ifdef ENABLE_VALIDATION_LAYERS
215 createInfo.enabledLayerCount
216 = static_cast<uint32_t>(validationLayers.size());
217 createInfo.ppEnabledLayerNames = validationLayers.data();
218#endif
219
220 if (vkCreateDevice(physicalDevice, &createInfo, nullptr, &device)
221 != VK_SUCCESS) {
222 LOG(1, "Failed to create logical device")
223 throw std::runtime_error("Failed to create logical device");
224 }
225
226 LOG(3, "Logical device created")
227 // get the queue handle
228 vkGetDeviceQueue(device, indices.graphicsFamily.value(), 0, &graphicsQueue);
229 vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue);
230}
VkDevice device
This is the logical device that is used by Vulkan.
Definition device.hpp:52
VkQueue presentQueue
This is the queue that is used for presentation of the rendered frames.
Definition device.hpp:71
VkPhysicalDevice physicalDevice
This is the physical device that is used by Vulkan.
Definition device.hpp:60
VkQueue graphicsQueue
This is the queue that is used for graphics rendering.
Definition device.hpp:65
#define LOG(LEVEL, MESSAGE)
Definition logging.hpp:60
QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device, VkSurfaceKHR surface)
Gets the indices of the needed queue families.
Definition helper.cpp:126
const std::vector< const char * > deviceExtensions
THis contain the device extensions that are needed to be enabled in vulkan.
Definition device.hpp:37

References Chronos::Engine::deviceExtensions, Chronos::Engine::findQueueFamilies(), Chronos::Engine::QueueFamilyIndices::graphicsFamily, LOG, and Chronos::Engine::QueueFamilyIndices::presentFamily.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ destroy()

void Chronos::Engine::Device::destroy ( )

This is the function that destroys the device.

We need to make sure that all the objects that are created using the device are destroyed before this is called. Else Vulkan Valiation layers will become angry at us.

Definition at line 36 of file device.cpp.

36{ vkDestroyDevice(device, nullptr); }
Here is the caller graph for this function:

◆ init()

void Chronos::Engine::Device::init ( VkInstance  instance,
VkSurfaceKHR  surface 
)

This is the function that initializes the devices and queues.

It picks the best device(GPU) based on the requirements and features that we require.

Parameters
instanceThe Vulkan instance that is used by the application.
surfaceThe surface that is used by the application for presenting the rendered images.

Definition at line 28 of file device.cpp.

29{
30 pickPhysicalDevice(instance, surface);
31 LOG(3, "Physical device picked")
32 createLogicalDevice(surface);
33 LOG(3, "Logical device created")
34}
void pickPhysicalDevice(VkInstance instance, VkSurfaceKHR surface)
This is the function that picks the best device based on the requirements and features that we requir...
Definition device.cpp:142
void createLogicalDevice(VkSurfaceKHR surface)
This creates the logical device.
Definition device.cpp:182

References createLogicalDevice(), LOG, and pickPhysicalDevice().

Here is the call graph for this function:

◆ pickPhysicalDevice()

void Chronos::Engine::Device::pickPhysicalDevice ( VkInstance  instance,
VkSurfaceKHR  surface 
)
private

This is the function that picks the best device based on the requirements and features that we require.

It finds all the physical devices(GPUs) that are available and then checks if they are suitable for our application. If none of the devices are suitable then it throws an error. If there are multiple suitable devices, then it gets the first usable device. the maximum number of MSAA samples is also set here.

Parameters
instanceThe Vulkan instance that is used by the application.
surfaceThe surface that is used by the application for presenting the rendered images.

Definition at line 142 of file device.cpp.

144{
145 // get the number of devices
146 uint32_t deviceCount = 0;
147 vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
148 if (deviceCount == 0) {
149 LOG(1, "Failed to find GPUs with Vulkan support")
150 throw std::runtime_error("Failed to find GPUs with Vulkan support");
151 }
152 LOG(3, "Number of devices found: " + std::to_string(deviceCount))
153 std::vector<VkPhysicalDevice> devices(deviceCount);
154 vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());
155
156 // check if the device is suitable
157 for (const auto& device : devices) {
158 if (isDeviceSuitable(device, surface)) {
161 break;
162 }
163 }
164 if (physicalDevice == VK_NULL_HANDLE) {
165 LOG(1, "Failed to find a suitable GPU")
166 throw std::runtime_error("Failed to find a suitable GPU");
167 }
168#ifdef CHRONOS_ENABLE_LOGGING
169 VkPhysicalDeviceProperties deviceProperties;
170 vkGetPhysicalDeviceProperties(physicalDevice, &deviceProperties);
171 LOG(3, "****PHYSICAL DEVICE INFORMATION*****")
172 LOG(3,
173 "Physical device picked: " + std::string(deviceProperties.deviceName))
174 LOG(3, "Driver version: " + std::to_string(deviceProperties.driverVersion))
175 LOG(3, "API version: " + std::to_string(deviceProperties.apiVersion))
176 LOG(3, "Device ID: " + std::to_string(deviceProperties.deviceID))
177 LOG(3, "Vendor ID: " + std::to_string(deviceProperties.vendorID))
178 LOG(3, "MSAA samples: " + std::to_string(msaaSamples))
179#endif
180}
VkSampleCountFlagBits maxMsaaSamples
Definition device.hpp:82
VkSampleCountFlagBits msaaSamples
This sets the MSAA samples count.
Definition device.hpp:80
static bool isDeviceSuitable(VkPhysicalDevice device, VkSurfaceKHR surface)
Checks if the physical device that we provide it is suitable for our needs.
Definition device.cpp:81
VkSampleCountFlagBits getMaxUsableSampleCount(VkPhysicalDevice physicalDevice)
Gets the maxmimum supported MSAA sample count.
Definition device.cpp:113

References getMaxUsableSampleCount(), isDeviceSuitable(), and LOG.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ device

VkDevice Chronos::Engine::Device::device

This is the logical device that is used by Vulkan.

Definition at line 52 of file device.hpp.

◆ graphicsQueue

VkQueue Chronos::Engine::Device::graphicsQueue

This is the queue that is used for graphics rendering.

Definition at line 65 of file device.hpp.

◆ maxMsaaSamples

VkSampleCountFlagBits Chronos::Engine::Device::maxMsaaSamples = VK_SAMPLE_COUNT_1_BIT

Definition at line 82 of file device.hpp.

◆ msaaSamples

VkSampleCountFlagBits Chronos::Engine::Device::msaaSamples = VK_SAMPLE_COUNT_1_BIT

This sets the MSAA samples count.

During initialization it is set to maximium number possible. This can be changed on the fly. However it requires the swapchain to be recreated along with textures and any other assosicated buffers and objects.

Definition at line 80 of file device.hpp.

◆ physicalDevice

VkPhysicalDevice Chronos::Engine::Device::physicalDevice

This is the physical device that is used by Vulkan.

Generally used for allocating memory and buffers.

Definition at line 60 of file device.hpp.

◆ presentQueue

VkQueue Chronos::Engine::Device::presentQueue

This is the queue that is used for presentation of the rendered frames.

Definition at line 71 of file device.hpp.


The documentation for this class was generated from the following files: