Skip to content

Commit

Permalink
Fix lints
Browse files Browse the repository at this point in the history
  • Loading branch information
camdenorrb committed Dec 21, 2024
1 parent 4e6788d commit 0977b66
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 125 deletions.
262 changes: 138 additions & 124 deletions Source/Core/VideoBackends/Vulkan/VKSwapChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,159 +36,172 @@ SwapChain::~SwapChain()
DestroySurface();
}

VkSurfaceKHR SwapChain::CreateVulkanSurface(VkInstance instance, VkPhysicalDevice physical_device, const WindowSystemInfo& wsi)
VkSurfaceKHR SwapChain::CreateVulkanSurface(VkInstance instance, VkPhysicalDevice physical_device,
const WindowSystemInfo& wsi)
{

#if defined(VK_USE_PLATFORM_DISPLAY_KHR)
if (wsi.type == WindowSystemType::DRM)
{
// Get the first display
uint32_t display_count = 1;
VkDisplayPropertiesKHR display_props;
if (VkResult err = vkGetPhysicalDeviceDisplayPropertiesKHR(physical_device, &display_count, &display_props); err != VK_SUCCESS && err != VK_INCOMPLETE)
{
LOG_VULKAN_ERROR(err, "vkGetPhysicalDeviceDisplayPropertiesKHR failed: ");
return VK_NULL_HANDLE;
}

// Get the first mode of the display
uint32_t mode_count = 0;
if (VkResult err = vkGetDisplayModePropertiesKHR(physical_device, display_props.display, &mode_count, nullptr); err != VK_SUCCESS)
{
LOG_VULKAN_ERROR(err, "vkGetDisplayModePropertiesKHR failed: ");
return VK_NULL_HANDLE;
}

if (mode_count == 0)
{
ERROR_LOG_FMT(VIDEO, "Cannot find any mode for the display!");
return VK_NULL_HANDLE;
}

VkDisplayModePropertiesKHR mode_props;
mode_count = 1;
if (VkResult err = vkGetDisplayModePropertiesKHR(physical_device, display_props.display, &mode_count, &mode_props); err != VK_SUCCESS && err != VK_INCOMPLETE)
{
LOG_VULKAN_ERROR(err, "vkGetDisplayModePropertiesKHR failed: ");
return VK_NULL_HANDLE;
}

// Get the list of planes
uint32_t plane_count = 0;
if (VkResult err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(physical_device, &plane_count, nullptr); err != VK_SUCCESS)
{
LOG_VULKAN_ERROR(err, "vkGetPhysicalDeviceDisplayPlanePropertiesKHR failed: ");
return VK_NULL_HANDLE;
}
if (wsi.type == WindowSystemType::DRM)
{
// Get the first display
uint32_t display_count = 1;
VkDisplayPropertiesKHR display_props;
if (VkResult err = vkGetPhysicalDeviceDisplayPropertiesKHR(physical_device, &display_count,
&display_props);
err != VK_SUCCESS && err != VK_INCOMPLETE)
{
LOG_VULKAN_ERROR(err, "vkGetPhysicalDeviceDisplayPropertiesKHR failed: ");
return VK_NULL_HANDLE;
}

if (plane_count == 0)
{
ERROR_LOG_FMT(VIDEO, "No display planes found!");
return VK_NULL_HANDLE;
}
// Get the first mode of the display
uint32_t mode_count = 0;
if (VkResult err = vkGetDisplayModePropertiesKHR(physical_device, display_props.display,
&mode_count, nullptr);
err != VK_SUCCESS)
{
LOG_VULKAN_ERROR(err, "vkGetDisplayModePropertiesKHR failed: ");
return VK_NULL_HANDLE;
}

// Find a plane compatible with the display
// Find a plane compatible with the display
uint32_t compatible_plane_index = UINT32_MAX;
if (mode_count == 0)
{
ERROR_LOG_FMT(VIDEO, "Cannot find any mode for the display!");
return VK_NULL_HANDLE;
}

for (uint32_t plane_index = 0; plane_index < plane_count; plane_index++)
{
// Query the number of displays supported by the plane
display_count = 0;
VkResult err = vkGetDisplayPlaneSupportedDisplaysKHR(physical_device, plane_index, &display_count, nullptr);
if (err != VK_SUCCESS)
VkDisplayModePropertiesKHR mode_props;
mode_count = 1;
if (VkResult err = vkGetDisplayModePropertiesKHR(physical_device, display_props.display,
&mode_count, &mode_props);
err != VK_SUCCESS && err != VK_INCOMPLETE)
{
LOG_VULKAN_ERROR(err, "vkGetDisplayPlaneSupportedDisplaysKHR (count query) failed: ");
LOG_VULKAN_ERROR(err, "vkGetDisplayModePropertiesKHR failed: ");
return VK_NULL_HANDLE;
}

if (display_count == 0)
continue; // Skip planes that support no displays
// Get the list of planes
uint32_t plane_count = 0;
if (VkResult err =
vkGetPhysicalDeviceDisplayPlanePropertiesKHR(physical_device, &plane_count, nullptr);
err != VK_SUCCESS)
{
LOG_VULKAN_ERROR(err, "vkGetPhysicalDeviceDisplayPlanePropertiesKHR failed: ");
return VK_NULL_HANDLE;
}

// Allocate memory to hold the supported displays
std::vector<VkDisplayKHR> displays(display_count);
err = vkGetDisplayPlaneSupportedDisplaysKHR(physical_device, plane_index, &display_count, displays.data());
if (err != VK_SUCCESS)
if (plane_count == 0)
{
LOG_VULKAN_ERROR(err, "vkGetDisplayPlaneSupportedDisplaysKHR (fetch displays) failed: ");
ERROR_LOG_FMT(VIDEO, "No display planes found!");
return VK_NULL_HANDLE;
}

// Check if the target display is among the supported displays
for (const auto& display : displays)
// Find a plane compatible with the display
// Find a plane compatible with the display
uint32_t compatible_plane_index = UINT32_MAX;

for (uint32_t plane_index = 0; plane_index < plane_count; plane_index++)
{
if (display == display_props.display)
// Query the number of displays supported by the plane
display_count = 0;
VkResult err = vkGetDisplayPlaneSupportedDisplaysKHR(physical_device, plane_index,
&display_count, nullptr);
if (err != VK_SUCCESS)
{
compatible_plane_index = plane_index;
break;
LOG_VULKAN_ERROR(err, "vkGetDisplayPlaneSupportedDisplaysKHR (count query) failed: ");
return VK_NULL_HANDLE;
}

if (display_count == 0)
continue; // Skip planes that support no displays

// Allocate memory to hold the supported displays
std::vector<VkDisplayKHR> displays(display_count);
err = vkGetDisplayPlaneSupportedDisplaysKHR(physical_device, plane_index, &display_count,
displays.data());
if (err != VK_SUCCESS)
{
LOG_VULKAN_ERROR(err, "vkGetDisplayPlaneSupportedDisplaysKHR (fetch displays) failed: ");
return VK_NULL_HANDLE;
}

// Check if the target display is among the supported displays
for (const auto& display : displays)
{
if (display == display_props.display)
{
compatible_plane_index = plane_index;
break;
}
}

if (compatible_plane_index != UINT32_MAX)
break; // Exit early if a compatible plane is found
}

if (compatible_plane_index != UINT32_MAX)
break; // Exit early if a compatible plane is found
}
if (compatible_plane_index == UINT32_MAX)
{
ERROR_LOG_FMT(VIDEO, "No compatible plane found for the display!");
return VK_NULL_HANDLE;
}

if (compatible_plane_index == UINT32_MAX)
{
ERROR_LOG_FMT(VIDEO, "No compatible plane found for the display!");
return VK_NULL_HANDLE;
}
if (compatible_plane_index == UINT32_MAX)
{
ERROR_LOG_FMT(VIDEO, "No compatible plane found for the display!");
return VK_NULL_HANDLE;
}

// Get capabilities of the compatible plane
VkDisplayPlaneCapabilitiesKHR plane_capabilities;
if (VkResult err = vkGetDisplayPlaneCapabilitiesKHR(
physical_device, mode_props.displayMode, compatible_plane_index, &plane_capabilities);
err != VK_SUCCESS)
{
LOG_VULKAN_ERROR(err, "vkGetDisplayPlaneCapabilitiesKHR failed: ");
return VK_NULL_HANDLE;
}

if (compatible_plane_index == UINT32_MAX)
{
ERROR_LOG_FMT(VIDEO, "No compatible plane found for the display!");
return VK_NULL_HANDLE;
}
// Find a supported alpha mode
VkDisplayPlaneAlphaFlagBitsKHR alpha_mode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR;
VkDisplayPlaneAlphaFlagBitsKHR alpha_modes[] = {
VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR,
VK_DISPLAY_PLANE_ALPHA_GLOBAL_BIT_KHR,
VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_BIT_KHR,
VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_PREMULTIPLIED_BIT_KHR,
};

// Get capabilities of the compatible plane
VkDisplayPlaneCapabilitiesKHR plane_capabilities;
if (VkResult err = vkGetDisplayPlaneCapabilitiesKHR(physical_device, mode_props.displayMode, compatible_plane_index, &plane_capabilities); err != VK_SUCCESS)
{
LOG_VULKAN_ERROR(err, "vkGetDisplayPlaneCapabilitiesKHR failed: ");
return VK_NULL_HANDLE;
}
for (auto& curr_alpha_mode : alpha_modes)
{
if (plane_capabilities.supportedAlpha & curr_alpha_mode)
{
alpha_mode = curr_alpha_mode;
break;
}
}

// Find a supported alpha mode
VkDisplayPlaneAlphaFlagBitsKHR alpha_mode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR;
VkDisplayPlaneAlphaFlagBitsKHR alpha_modes[] = {
VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR,
VK_DISPLAY_PLANE_ALPHA_GLOBAL_BIT_KHR,
VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_BIT_KHR,
VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_PREMULTIPLIED_BIT_KHR,
};
// Create the display surface
VkDisplaySurfaceCreateInfoKHR surface_create_info = {};
surface_create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR;
surface_create_info.displayMode = mode_props.displayMode;
surface_create_info.planeIndex = compatible_plane_index;
surface_create_info.planeStackIndex = 0;
surface_create_info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
surface_create_info.globalAlpha = 1.0f;
surface_create_info.alphaMode = alpha_mode;
surface_create_info.imageExtent.width = display_props.physicalResolution.width;
surface_create_info.imageExtent.height = display_props.physicalResolution.height;

for (auto& curr_alpha_mode : alpha_modes)
{
if (plane_capabilities.supportedAlpha & curr_alpha_mode)
VkSurfaceKHR surface;
if (VkResult res =
vkCreateDisplayPlaneSurfaceKHR(instance, &surface_create_info, nullptr, &surface);
res != VK_SUCCESS)
{
alpha_mode = curr_alpha_mode;
break;
LOG_VULKAN_ERROR(res, "vkCreateDisplayPlaneSurfaceKHR failed: ");
return VK_NULL_HANDLE;
}
}

// Create the display surface
VkDisplaySurfaceCreateInfoKHR surface_create_info = {};
surface_create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR;
surface_create_info.displayMode = mode_props.displayMode;
surface_create_info.planeIndex = compatible_plane_index;
surface_create_info.planeStackIndex = 0;
surface_create_info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
surface_create_info.globalAlpha = 1.0f;
surface_create_info.alphaMode = alpha_mode;
surface_create_info.imageExtent.width = display_props.physicalResolution.width;
surface_create_info.imageExtent.height = display_props.physicalResolution.height;

VkSurfaceKHR surface;
if (VkResult res = vkCreateDisplayPlaneSurfaceKHR(instance, &surface_create_info, nullptr, &surface); res != VK_SUCCESS)
{
LOG_VULKAN_ERROR(res, "vkCreateDisplayPlaneSurfaceKHR failed: ");
return VK_NULL_HANDLE;
return surface;
}

return surface;
}

#endif

#if defined(VK_USE_PLATFORM_WIN32_KHR)
Expand Down Expand Up @@ -736,7 +749,8 @@ bool SwapChain::RecreateSurface(void* native_handle)

// Re-create the surface with the new native handle
m_wsi.render_surface = native_handle;
m_surface = CreateVulkanSurface(g_vulkan_context->GetVulkanInstance(), g_vulkan_context->GetPhysicalDevice(), m_wsi);
m_surface = CreateVulkanSurface(g_vulkan_context->GetVulkanInstance(),
g_vulkan_context->GetPhysicalDevice(), m_wsi);
if (m_surface == VK_NULL_HANDLE)
return false;

Expand Down
4 changes: 4 additions & 0 deletions Source/Core/VideoBackends/Vulkan/VulkanLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ static void ResetVulkanLibraryFunctionPointers()
#define VULKAN_INSTANCE_ENTRY_POINT(name, required) name = nullptr;
#define VULKAN_DEVICE_ENTRY_POINT(name, required) name = nullptr;
#include "VideoBackends/Vulkan/VulkanEntryPoints.inl"

#undef VULKAN_DEVICE_ENTRY_POINT
#undef VULKAN_INSTANCE_ENTRY_POINT
#undef VULKAN_MODULE_ENTRY_POINT
Expand Down Expand Up @@ -108,6 +109,7 @@ bool LoadVulkanLibrary(bool force_system_library)
return false; \
}
#include "VideoBackends/Vulkan/VulkanEntryPoints.inl"

#undef VULKAN_MODULE_ENTRY_POINT

return true;
Expand Down Expand Up @@ -135,6 +137,7 @@ bool LoadVulkanInstanceFunctions(VkInstance instance)
#define VULKAN_INSTANCE_ENTRY_POINT(name, required) \
LoadFunction(reinterpret_cast<PFN_vkVoidFunction*>(&name), #name, required);
#include "VideoBackends/Vulkan/VulkanEntryPoints.inl"

#undef VULKAN_INSTANCE_ENTRY_POINT

return !required_functions_missing;
Expand All @@ -155,6 +158,7 @@ bool LoadVulkanDeviceFunctions(VkDevice device)
#define VULKAN_DEVICE_ENTRY_POINT(name, required) \
LoadFunction(reinterpret_cast<PFN_vkVoidFunction*>(&name), #name, required);
#include "VideoBackends/Vulkan/VulkanEntryPoints.inl"

#undef VULKAN_DEVICE_ENTRY_POINT

return !required_functions_missing;
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/VideoBackends/Vulkan/VulkanLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#if defined(HAVE_DRM)
#define VK_USE_PLATFORM_DISPLAY_KHR
//#define VK_KHR_display
// #define VK_KHR_display
#endif

#if defined(HAVE_X11)
Expand Down

0 comments on commit 0977b66

Please sign in to comment.