Skip to content

Commit

Permalink
rsx: Make X11 optional on Linux
Browse files Browse the repository at this point in the history
This makes it possible to build rpcs3 on a pure Wayland system, without
the Xlib installed.
  • Loading branch information
linkmauve committed Nov 20, 2019
1 parent 8d5ef2c commit bc2b03d
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 16 deletions.
9 changes: 6 additions & 3 deletions rpcs3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@ if (WIN32)
endif()

if(UNIX AND NOT APPLE)
find_package(X11 REQUIRED)
target_include_directories(rpcs3 PUBLIC ${X11_INCLUDE_DIR})
target_link_libraries(rpcs3 ${X11_LIBRARIES})
find_package(X11)
if(X11_FOUND)
target_include_directories(rpcs3 PUBLIC ${X11_INCLUDE_DIR})
target_link_libraries(rpcs3 ${X11_LIBRARIES})
target_compile_definitions(rpcs3 INTERFACE -DHAVE_X11)
endif()
endif()

if(UNIX)
Expand Down
4 changes: 2 additions & 2 deletions rpcs3/Emu/RSX/GL/OpenGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void gl::init()
#ifdef __unix__
glewExperimental = true;
glewInit();
#ifndef __APPLE__
#ifdef HAVE_X11
glxewInit();
#endif
#endif
Expand All @@ -44,7 +44,7 @@ void gl::set_swapinterval(int interval)
{
#ifdef _WIN32
wglSwapIntervalEXT(interval);
#elif !defined(__APPLE__)
#elif defined(HAVE_X11)
if (glXSwapIntervalEXT)
{
if (auto window = glXGetCurrentDrawable())
Expand Down
4 changes: 3 additions & 1 deletion rpcs3/Emu/RSX/GL/OpenGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ typedef BOOL (WINAPI* PFNWGLSWAPINTERVALEXTPROC) (int interval);
#include <OpenGL/glu.h>

#else
#include <GL/glxew.h>
#include <GL/gl.h>
#ifdef HAVE_X11
#include <GL/glxew.h>
#include <GL/glx.h>
#include <GL/glxext.h>
#endif
#endif

#ifndef GL_TEXTURE_BUFFER_BINDING
//During spec release, this enum was removed during upgrade from ARB equivalent
Expand Down
9 changes: 7 additions & 2 deletions rpcs3/Emu/RSX/GSRender.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <windows.h>
#elif defined(__APPLE__)
// nothing
#else
#elif defined(HAVE_X11)
// Cannot include Xlib.h before Qt5
// and we don't need all of Xlib anyway
using Display = struct _XDisplay;
Expand Down Expand Up @@ -57,9 +57,14 @@ using display_handle_t = HWND;
using display_handle_t = void*; // NSView
#else
using display_handle_t = std::variant<
#ifdef HAVE_X11
std::pair<Display*, Window>
#endif
#if defined(HAVE_X11) && defined(VK_USE_PLATFORM_WAYLAND_KHR)
,
#endif
#ifdef VK_USE_PLATFORM_WAYLAND_KHR
, std::pair<wl_display*, wl_surface*>
std::pair<wl_display*, wl_surface*>
#endif
>;
#endif
Expand Down
4 changes: 2 additions & 2 deletions rpcs3/Emu/RSX/VK/VKGSRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ VKGSRender::VKGSRender() : GSRender()

display_handle_t display = m_frame->handle();

#if !defined(_WIN32) && !defined(__APPLE__)
#ifdef HAVE_X11
std::visit([this](auto&& p) {
using T = std::decay_t<decltype(p)>;
if constexpr (std::is_same_v<T, std::pair<Display*, Window>>)
Expand Down Expand Up @@ -653,7 +653,7 @@ VKGSRender::~VKGSRender()
m_swapchain->destroy();
m_thread_context.close();

#if !defined(_WIN32) && !defined(__APPLE__) && defined(HAVE_VULKAN)
#if defined(HAVE_X11) && defined(HAVE_VULKAN)
if (m_display_handle)
XCloseDisplay(m_display_handle);
#endif
Expand Down
2 changes: 1 addition & 1 deletion rpcs3/Emu/RSX/VK/VKGSRender.h
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ class VKGSRender : public GSRender, public ::rsx::reports::ZCULL_control
//Vertex layout
rsx::vertex_input_layout m_vertex_layout;

#if !defined(_WIN32) && !defined(__APPLE__) && defined(HAVE_VULKAN)
#if defined(HAVE_X11) && defined(HAVE_VULKAN)
Display *m_display_handle = nullptr;
#endif

Expand Down
64 changes: 60 additions & 4 deletions rpcs3/Emu/RSX/VK/VKHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <variant>
#include <stack>

#if !defined(_WIN32) && !defined(__APPLE__)
#ifdef HAVE_X11
#include <X11/Xutil.h>
#endif

Expand Down Expand Up @@ -2071,7 +2071,7 @@ namespace vk
{
fmt::throw_exception("Native macOS swapchain is not implemented yet!");
}
#else
#elif defined(HAVE_X11)

class swapchain_X11 : public native_swapchain_base
{
Expand Down Expand Up @@ -2172,6 +2172,51 @@ namespace vk
src.first = false;
return VK_SUCCESS;
}
#else

class swapchain_generic : public native_swapchain_base
{

public:
swapchain_generic(physical_device &gpu, uint32_t _present_queue, uint32_t _graphics_queue, VkFormat format = VK_FORMAT_B8G8R8A8_UNORM)
: native_swapchain_base(gpu, _present_queue, _graphics_queue, format)
{}

~swapchain_generic(){}

bool init() override
{
//TODO: get from `nsView`
m_width = 0;
m_height = 0;

if (m_width == 0 || m_height == 0)
{
LOG_ERROR(RSX, "Invalid window dimensions %d x %d", m_width, m_height);
return false;
}

init_swapchain_images(dev, 3);
return true;
}

void create(display_handle_t& window_handle) override
{
//nsView = window_handle;
}

void destroy(bool full=true) override
{
swapchain_images.clear();

if (full)
dev.destroy();
}

VkResult present(VkSemaphore /*semaphore*/, u32 index) override
{
fmt::throw_exception("Native Wayland swapchain is not implemented yet!");
}
#endif

VkResult acquire_next_swapchain_image(VkSemaphore /*semaphore*/, u64 /*timeout*/, u32* result) override
Expand Down Expand Up @@ -2581,11 +2626,13 @@ namespace vk
extensions.push_back(VK_MVK_MACOS_SURFACE_EXTENSION_NAME);
#else
bool found_surface_ext = false;
#ifdef HAVE_X11
if (support.is_supported(VK_KHR_XLIB_SURFACE_EXTENSION_NAME))
{
extensions.push_back(VK_KHR_XLIB_SURFACE_EXTENSION_NAME);
found_surface_ext = true;
}
#endif
#ifdef VK_USE_PLATFORM_WAYLAND_KHR
if (support.is_supported(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME))
{
Expand Down Expand Up @@ -2684,12 +2731,17 @@ namespace vk

CHECK_RESULT(vkCreateMacOSSurfaceMVK(m_instance, &createInfo, NULL, &m_surface));
#else
#ifdef HAVE_X11
using swapchain_NATIVE = swapchain_X11;
#else
using swapchain_NATIVE = swapchain_generic;
#endif

std::visit([&](auto&& p)
{
using T = std::decay_t<decltype(p)>;

#ifdef HAVE_X11
if constexpr (std::is_same_v<T, std::pair<Display*, Window>>)
{
VkXlibSurfaceCreateInfoKHR createInfo = {};
Expand All @@ -2698,8 +2750,12 @@ namespace vk
createInfo.window = p.second;
CHECK_RESULT(vkCreateXlibSurfaceKHR(this->m_instance, &createInfo, nullptr, &m_surface));
}
#endif
#if defined(HAVE_X11) && defined(VK_USE_PLATFORM_WAYLAND_KHR)
else
#endif
#ifdef VK_USE_PLATFORM_WAYLAND_KHR
else if constexpr (std::is_same_v<T, std::pair<wl_display*, wl_surface*>>)
if constexpr (std::is_same_v<T, std::pair<wl_display*, wl_surface*>>)
{
VkWaylandSurfaceCreateInfoKHR createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
Expand All @@ -2708,11 +2764,11 @@ namespace vk
CHECK_RESULT(vkCreateWaylandSurfaceKHR(this->m_instance, &createInfo, nullptr, &m_surface));
force_wm_reporting_off = true;
}
#endif
else
{
static_assert(std::conditional_t<true, std::false_type, T>::value, "Unhandled window_handle type in std::variant");
}
#endif
}, window_handle);
#endif

Expand Down
2 changes: 1 addition & 1 deletion rpcs3/Emu/RSX/VK/VulkanAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#define VK_USE_PLATFORM_WIN32_KHR
#elif defined(__APPLE__)
#define VK_USE_PLATFORM_MACOS_MVK
#else
#elif HAVE_X11
#define VK_USE_PLATFORM_XLIB_KHR
#endif

Expand Down
6 changes: 6 additions & 0 deletions rpcs3/rpcs3qt/gs_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
#include <QGuiApplication>
#include <qpa/qplatformnativeinterface.h>
#endif
#ifdef HAVE_X11
#include <X11/Xlib.h>
#endif
#endif

constexpr auto qstr = QString::fromStdString;

Expand Down Expand Up @@ -235,7 +237,11 @@ display_handle_t gs_frame::handle() const
else
{
#endif
#ifdef HAVE_X11
return std::make_pair(XOpenDisplay(0), (unsigned long)(this->winId()));
#else
fmt::throw_exception("Vulkan X11 support disabled at compile-time.");
#endif
#ifdef VK_USE_PLATFORM_WAYLAND_KHR
}
#endif
Expand Down

0 comments on commit bc2b03d

Please sign in to comment.