Skip to content

Commit

Permalink
Vulkan DRM/KMS Support
Browse files Browse the repository at this point in the history
  • Loading branch information
camdenorrb committed Dec 21, 2024
1 parent ada646a commit a49e22e
Show file tree
Hide file tree
Showing 15 changed files with 321 additions and 34 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ option(USE_DISCORD_PRESENCE "Enables Discord Rich Presence, show the current gam
option(USE_MGBA "Enables GBA controllers emulation using libmgba" ON)
option(ENABLE_AUTOUPDATE "Enables support for automatic updates" ON)
option(USE_RETRO_ACHIEVEMENTS "Enables integration with retroachievements.org" ON)
option(ENABLE_DRM "Enables DRM support" OFF)

# Maintainers: if you consider blanket disabling this for your users, please
# consider the following points:
Expand Down Expand Up @@ -501,6 +502,10 @@ if (OPENGL_GL)
include_directories(${OPENGL_INCLUDE_DIR})
endif()

if(ENABLE_DRM)
add_definitions(-DHAVE_DRM)
endif()

if(ENABLE_X11)
pkg_check_modules(X11 x11 IMPORTED_TARGET)
if(X11_FOUND)
Expand Down
1 change: 1 addition & 0 deletions Source/Core/Common/WindowSystemInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ enum class WindowSystemType
Wayland,
FBDev,
Haiku,
DRM,
};

struct WindowSystemInfo
Expand Down
4 changes: 4 additions & 0 deletions Source/Core/DolphinNoGUI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
target_sources(dolphin-nogui PRIVATE PlatformFBDev.cpp)
endif()

if(ENABLE_DRM)
target_sources(dolphin-nogui PRIVATE PlatformDRM.cpp)
endif()

set_target_properties(dolphin-nogui PROPERTIES OUTPUT_NAME dolphin-emu-nogui)

target_link_libraries(dolphin-nogui
Expand Down
28 changes: 18 additions & 10 deletions Source/Core/DolphinNoGUI/MainNoGUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ static std::unique_ptr<Platform> GetPlatform(const optparse::Values& options)
{
std::string platform_name = static_cast<const char*>(options.get("platform"));

#if HAVE_DRM
if (platform_name == "drm" || platform_name.empty())
return Platform::CreateDRMPlatform();
#endif

#if HAVE_X11
if (platform_name == "x11" || platform_name.empty())
return Platform::CreateX11Platform();
Expand Down Expand Up @@ -209,23 +214,26 @@ int main(int argc, char* argv[])
parser->add_option("-p", "--platform")
.action("store")
.help("Window platform to use [%choices]")
.choices({
"headless"
.choices({"headless"
#ifdef __linux__
,
"fbdev"
,
"fbdev"
#endif
#if HAVE_DRM
,
"drm"
#endif
#if HAVE_X11
,
"x11"
,
"x11"
#endif
#ifdef _WIN32
,
"win32"
,
"win32"
#endif
#ifdef __APPLE__
,
"macos"
,
"macos"
#endif
});

Expand Down
4 changes: 4 additions & 0 deletions Source/Core/DolphinNoGUI/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class Platform
// Request an immediate shutdown.
void Stop();

#if HAVE_DRM
static std::unique_ptr<Platform> CreateDRMPlatform();
#endif

static std::unique_ptr<Platform> CreateHeadlessPlatform();
#ifdef HAVE_X11
static std::unique_ptr<Platform> CreateX11Platform();
Expand Down
63 changes: 63 additions & 0 deletions Source/Core/DolphinNoGUI/PlatformDRM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2018 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <unistd.h>

#include "DolphinNoGUI/Platform.h"

#include "Common/MsgHandler.h"
#include "Core/ConfigManager.h"
#include "Core/Core.h"
#include "Core/State.h"
#include "Core/System.h"

#include <climits>
#include <cstdio>
#include <thread>

#include <fcntl.h>
#include <sys/types.h>

namespace
{
class PlatformDRM : public Platform
{
public:
void SetTitle(const std::string& string) override;
void MainLoop() override;

WindowSystemInfo GetWindowSystemInfo() const override;
};

void PlatformDRM::SetTitle(const std::string& string)
{
std::fprintf(stdout, "%s\n", string.c_str());
}

void PlatformDRM::MainLoop()
{
while (IsRunning())
{
UpdateRunningFlag();
Core::HostDispatchJobs(Core::System::GetInstance());

// TODO: Is this sleep appropriate?
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}

WindowSystemInfo PlatformDRM::GetWindowSystemInfo() const
{
WindowSystemInfo wsi;
wsi.type = WindowSystemType::DRM;
wsi.display_connection = nullptr; // EGL_DEFAULT_DISPLAY
wsi.render_window = nullptr;
wsi.render_surface = nullptr;
return wsi;
}
} // namespace

std::unique_ptr<Platform> Platform::CreateDRMPlatform()
{
return std::make_unique<PlatformDRM>();
}
8 changes: 4 additions & 4 deletions Source/Core/VideoBackends/Vulkan/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ endif()

# Only include the Vulkan headers when building the Vulkan backend
target_include_directories(videovulkan
PRIVATE
${CMAKE_SOURCE_DIR}/Externals/Vulkan-Headers/include
${CMAKE_SOURCE_DIR}/Externals/VulkanMemoryAllocator/include
${CMAKE_SOURCE_DIR}/Externals/libadrenotools/include
PRIVATE
${CMAKE_SOURCE_DIR}/Externals/Vulkan-Headers/include
${CMAKE_SOURCE_DIR}/Externals/VulkanMemoryAllocator/include
${CMAKE_SOURCE_DIR}/Externals/libadrenotools/include
)

if(MSVC)
Expand Down
20 changes: 10 additions & 10 deletions Source/Core/VideoBackends/Vulkan/VKMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,20 @@ bool VideoBackend::Initialize(const WindowSystemInfo& wsi)
VulkanContext::PopulateBackendInfo(&g_Config);
VulkanContext::PopulateBackendInfoAdapters(&g_Config, gpu_list);

// Since we haven't called InitializeShared yet, iAdapter may be out of range,
// so we have to check it ourselves.
size_t selected_adapter_index = static_cast<size_t>(g_Config.iAdapter);
if (selected_adapter_index >= gpu_list.size())
{
WARN_LOG_FMT(VIDEO, "Vulkan adapter index out of range, selecting first adapter.");
selected_adapter_index = 0;
}

// We need the surface before we can create a device, as some parameters depend on it.
VkSurfaceKHR surface = VK_NULL_HANDLE;
if (enable_surface)
{
surface = SwapChain::CreateVulkanSurface(instance, wsi);
surface = SwapChain::CreateVulkanSurface(instance, gpu_list[selected_adapter_index], wsi);
if (surface == VK_NULL_HANDLE)
{
PanicAlertFmt("Failed to create Vulkan surface.");
Expand All @@ -161,15 +170,6 @@ bool VideoBackend::Initialize(const WindowSystemInfo& wsi)
}
}

// Since we haven't called InitializeShared yet, iAdapter may be out of range,
// so we have to check it ourselves.
size_t selected_adapter_index = static_cast<size_t>(g_Config.iAdapter);
if (selected_adapter_index >= gpu_list.size())
{
WARN_LOG_FMT(VIDEO, "Vulkan adapter index out of range, selecting first adapter.");
selected_adapter_index = 0;
}

// Now we can create the Vulkan device. VulkanContext takes ownership of the instance and surface.
g_vulkan_context =
VulkanContext::Create(instance, gpu_list[selected_adapter_index], surface, enable_debug_utils,
Expand Down
Loading

0 comments on commit a49e22e

Please sign in to comment.