stories/engine/surface.cpp

92 lines
2.8 KiB
C++
Raw Normal View History

2023-09-24 16:16:10 +00:00
#include "surface.h"
#include "window.h"
#include "instance.h"
Engine::Surface::Surface(const Instance* instance, const Window* window):
vk(),
instance(instance),
window(window)
{
window->createSurface(instance->vk, &vk);
}
Engine::Surface::~Surface() {
vkDestroySurfaceKHR(instance->vk, vk, nullptr);
}
bool Engine::Surface::isDeviceSutable(VkPhysicalDevice device) const {
QueueFamilyIndices indices = findQueueFamilies(device);
if (!indices.isComplete())
return false;
return querySwapChainSupport(device).adequate();
}
Engine::SwapChainSupportDetails Engine::Surface::querySwapChainSupport(VkPhysicalDevice device) const {
SwapChainSupportDetails details;
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, vk, &details.capabilities);
uint32_t formatCount;
vkGetPhysicalDeviceSurfaceFormatsKHR(device, vk, &formatCount, nullptr);
if (formatCount != 0) {
details.formats.resize(formatCount);
vkGetPhysicalDeviceSurfaceFormatsKHR(device, vk, &formatCount, details.formats.data());
}
uint32_t presentModeCount;
vkGetPhysicalDeviceSurfacePresentModesKHR(device, vk, &presentModeCount, nullptr);
if (presentModeCount != 0) {
details.presentModes.resize(presentModeCount);
vkGetPhysicalDeviceSurfacePresentModesKHR(device, vk, &presentModeCount, details.presentModes.data());
}
return details;
}
Engine::QueueFamilyIndices Engine::Surface::findQueueFamilies(VkPhysicalDevice device) const {
QueueFamilyIndices indices;
uint32_t queueFamilyCount = 0;
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
for (std::vector<VkQueueFamilyProperties>::size_type i = 0; i < queueFamilyCount; ++i) {
const VkQueueFamilyProperties& queueFamily = queueFamilies[i];
if (queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT)
indices.graphicsFamily = i;
VkBool32 presentSupport = false;
vkGetPhysicalDeviceSurfaceSupportKHR(device, i, vk, &presentSupport);
if (presentSupport)
indices.presentFamily = i;
if (indices.isComplete())
break;
}
return indices;
}
VkExtent2D Engine::Surface::chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities) const {
return window->chooseSwapExtent(capabilities);
}
VkSurfaceFormatKHR Engine::Surface::chooseSwapSurfaceFormat(const SwapChainSupportDetails& swapChainSupport) const {
return ::Engine::chooseSwapSurfaceFormat(swapChainSupport.formats);
}
VkExtent2D Engine::Surface::waitForResize() const {
return window->waitForResize();
}
VkExtent2D Engine::Surface::getSize() const {
return window->getSize();
}