up to right before choosing the right settings for the swap chain

This commit is contained in:
Mars 2024-10-01 16:10:20 -04:00
parent 623c51d335
commit 9ae5a4675a
Signed by untrusted user: pupbrained
GPG key ID: 874E22DF2F9DFCB5

View file

@ -18,6 +18,13 @@ constexpr i32 HEIGHT = 600;
constexpr std::array<const char*, 1> validationLayers = { "VK_LAYER_KHRONOS_validation" }; constexpr std::array<const char*, 1> validationLayers = { "VK_LAYER_KHRONOS_validation" };
#ifdef __APPLE__
constexpr std::array<const char*, 2> deviceExtensions = { vk::KHRSwapchainExtensionName,
vk::KHRPortabilitySubsetExtensionName };
#else
constexpr std::array<const char*, 1> deviceExtensions = { vk::KHRSwapchainExtensionName };
#endif
#ifdef NDEBUG #ifdef NDEBUG
constexpr bool enableValidationLayers = false; constexpr bool enableValidationLayers = false;
#else #else
@ -56,6 +63,12 @@ class VulkanApp {
fn isComplete() -> bool { return graphics_family.has_value() && present_family.has_value(); } fn isComplete() -> bool { return graphics_family.has_value() && present_family.has_value(); }
}; };
struct SwapChainSupportDetails {
vk::SurfaceCapabilitiesKHR capabilities;
std::vector<vk::SurfaceFormatKHR> formats;
std::vector<vk::PresentModeKHR> present_modes;
};
fn initWindow() -> void { fn initWindow() -> void {
mGLFWInstance = vkfw::initUnique(); mGLFWInstance = vkfw::initUnique();
@ -92,9 +105,9 @@ class VulkanApp {
// Retrieve extensions using custom function // Retrieve extensions using custom function
std::vector<const char*> extensions = getRequiredExtensions(); std::vector<const char*> extensions = getRequiredExtensions();
#ifdef __APPLE__
// Enable the portability extension and set flags // Enable the portability extension and set flags
extensions.emplace_back(vk::KHRPortabilityEnumerationExtensionName); extensions.emplace_back(vk::KHRPortabilityEnumerationExtensionName);
#ifdef __APPLE__
// Technically deprecated but vulkan complains if I don't include it for macOS // Technically deprecated but vulkan complains if I don't include it for macOS
// So instead of using the vk::KHRPortabilitySubsetExtensionName, I just use // So instead of using the vk::KHRPortabilitySubsetExtensionName, I just use
// the direct string. // the direct string.
@ -187,19 +200,11 @@ class VulkanApp {
vk::PhysicalDeviceFeatures deviceFeatures; vk::PhysicalDeviceFeatures deviceFeatures;
#ifdef __APPLE__
vk::DeviceCreateInfo createInfo { .queueCreateInfoCount = static_cast<u32>(queueCreateInfos.size()), vk::DeviceCreateInfo createInfo { .queueCreateInfoCount = static_cast<u32>(queueCreateInfos.size()),
.pQueueCreateInfos = queueCreateInfos.data(), .pQueueCreateInfos = queueCreateInfos.data(),
.enabledExtensionCount = 1, .enabledExtensionCount = static_cast<u32>(deviceExtensions.size()),
.ppEnabledExtensionNames = &vk::KHRPortabilitySubsetExtensionName, .ppEnabledExtensionNames = deviceExtensions.data(),
.pEnabledFeatures = &deviceFeatures }; .pEnabledFeatures = &deviceFeatures };
#else
vk::DeviceCreateInfo createInfo { .queueCreateInfoCount = static_cast<u32>(queueCreateInfos.size()),
.pQueueCreateInfos = queueCreateInfos.data(),
.enabledExtensionCount = 0,
.ppEnabledExtensionNames = nullptr,
.pEnabledFeatures = &deviceFeatures };
#endif
mDevice = mPhysicalDevice.createDeviceUnique(createInfo); mDevice = mPhysicalDevice.createDeviceUnique(createInfo);
@ -210,7 +215,26 @@ class VulkanApp {
fn isDeviceSuitable(vk::PhysicalDevice device) -> bool { fn isDeviceSuitable(vk::PhysicalDevice device) -> bool {
QueueFamilyIndices indices = findQueueFamilies(device); QueueFamilyIndices indices = findQueueFamilies(device);
return indices.isComplete(); bool extensionsSupported = checkDeviceExtensionSupport(device);
bool swapChainAdequate = false;
if (extensionsSupported) {
SwapChainSupportDetails swapChainSupport = querySwapChainSupport(device);
swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.present_modes.empty();
}
return indices.isComplete() && extensionsSupported && swapChainAdequate;
}
static fn checkDeviceExtensionSupport(vk::PhysicalDevice device) -> bool {
std::vector<vk::ExtensionProperties> availableExtensions = device.enumerateDeviceExtensionProperties();
std::set<string> requiredExtensions(deviceExtensions.begin(), deviceExtensions.end());
for (const auto& extension : availableExtensions) requiredExtensions.erase(extension.extensionName);
return requiredExtensions.empty();
} }
fn findQueueFamilies(vk::PhysicalDevice device) -> QueueFamilyIndices { fn findQueueFamilies(vk::PhysicalDevice device) -> QueueFamilyIndices {
@ -234,6 +258,16 @@ class VulkanApp {
return indices; return indices;
} }
fn querySwapChainSupport(vk::PhysicalDevice device) -> SwapChainSupportDetails {
SwapChainSupportDetails details;
details.capabilities = device.getSurfaceCapabilitiesKHR(mSurface.get());
details.formats = device.getSurfaceFormatsKHR(mSurface.get());
details.present_modes = device.getSurfacePresentModesKHR(mSurface.get());
return details;
}
static fn getRequiredExtensions() -> std::vector<const char*> { static fn getRequiredExtensions() -> std::vector<const char*> {
std::span<const char*> extensionsSpan = vkfw::getRequiredInstanceExtensions(); std::span<const char*> extensionsSpan = vkfw::getRequiredInstanceExtensions();