added picker for present and formats

This commit is contained in:
Warwick New 2024-12-24 18:36:22 +00:00
parent 0fb09b9e43
commit a95e3a79b1

View file

@ -304,20 +304,57 @@ bool checkDeviceExtensionSupport(VkPhysicalDevice device) {
return true; return true;
} }
// TODO: find a better way of managing memory
SwapChainSupportDetails querySwapchainSupport(VkPhysicalDevice device, SwapChainSupportDetails querySwapchainSupport(VkPhysicalDevice device,
VkSurfaceKHR *surface) { VkSurfaceKHR *surface) {
SwapChainSupportDetails details = {0}; SwapChainSupportDetails details = {0};
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, *surface, vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, *surface,
&details.capabilities); &details.capabilities);
vkGetPhysicalDeviceSurfaceFormatsKHR(device, *surface, &details.numFormats, vkGetPhysicalDeviceSurfaceFormatsKHR(device, *surface, &details.numFormats,
details.formats); NULL);
vkGetPhysicalDeviceSurfacePresentModesKHR( if (details.numFormats > 0) {
device, *surface, &details.numPresentModes, details.presentModes); details.formats = malloc(details.numFormats * sizeof(VkSurfaceFormatKHR));
vkGetPhysicalDeviceSurfaceFormatsKHR(device, *surface, &details.numFormats,
details.formats);
}
vkGetPhysicalDeviceSurfacePresentModesKHR(device, *surface,
&details.numPresentModes, NULL);
if (details.numPresentModes > 0) {
details.presentModes =
malloc(details.numFormats * sizeof(VkPresentModeKHR));
vkGetPhysicalDeviceSurfacePresentModesKHR(
device, *surface, &details.numPresentModes, details.presentModes);
}
return details; return details;
} }
// Prefer one format but otherwise go for
VkSurfaceFormatKHR chooseSwapSurfaceFormat(VkSurfaceFormatKHR *availableFormats,
size_t availableFormatCount) {
for (uint i = 0; i < availableFormatCount; i++) {
if (availableFormats[i].format == VK_FORMAT_B8G8R8_SRGB &&
availableFormats[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
return availableFormats[i];
}
}
return availableFormats[0];
}
// Prefer mailbox otherwise go for the garenteed available present mode
VkPresentModeKHR chooseSwapPresentMode(VkPresentModeKHR *availablePresentModes,
size_t availablePresentModesCount) {
for (uint i = 0; i < availablePresentModesCount; i++) {
if (availablePresentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
return availablePresentModes[i];
}
}
return VK_PRESENT_MODE_FIFO_KHR;
}
bool isDeviceSuitable(VkPhysicalDevice device, VkSurfaceKHR *surface) { bool isDeviceSuitable(VkPhysicalDevice device, VkSurfaceKHR *surface) {
QueueFamilyIndices indices = findQueueFamilies(device, surface); QueueFamilyIndices indices = findQueueFamilies(device, surface);
bool completeIndeces = bool completeIndeces =
@ -325,10 +362,21 @@ bool isDeviceSuitable(VkPhysicalDevice device, VkSurfaceKHR *surface) {
bool extensionsSupported = checkDeviceExtensionSupport(device); bool extensionsSupported = checkDeviceExtensionSupport(device);
SwapChainSupportDetails swapChaindetails = bool swapChainAdequate = false;
querySwapchainSupport(device, surface); if (extensionsSupported) {
SwapChainSupportDetails swapChainDetails =
querySwapchainSupport(device, surface);
return completeIndeces && extensionsSupported; if (swapChainDetails.numFormats > 0 &&
swapChainDetails.numPresentModes > 0) {
swapChainAdequate = true;
}
free(swapChainDetails.presentModes);
free(swapChainDetails.formats);
}
return completeIndeces && extensionsSupported && swapChainAdequate;
} }
void pickPhysicalDevice(Application *app) { void pickPhysicalDevice(Application *app) {