Skip to content

Commit

Permalink
Libretro Port.
Browse files Browse the repository at this point in the history
  • Loading branch information
aliaspider committed Apr 10, 2023
1 parent 77d37de commit 502b6f5
Show file tree
Hide file tree
Showing 72 changed files with 6,981 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 3
NamespaceIndentation: All
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
Expand Down
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,14 @@ oprofile_data/
CMakeSettings.json
/ci-artifacts/
/out/

bin/*
build*/
*.cflags
*.config
*.creator
*.creator.user.*
*.cxxflags
*.files
*.includes
*.autosave
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ include(Pcsx2Utils)
check_no_parenthesis_in_path()
detectOperatingSystem()
check_compiler_version("7.0" "7.0")
if(NOT MSVC)
find_program(CCACHE_FOUND ccache)
if(CCACHE_FOUND)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ${CCACHE_FOUND})
set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ${CCACHE_FOUND})
endif()
endif()

#-------------------------------------------------------------------------------
# Include specific module
Expand Down Expand Up @@ -60,6 +67,10 @@ if (QT_BUILD)
endif()
endif()

if(LIBRETRO)
add_subdirectory(libretro)
endif()

# tests
if(ACTUALLY_ENABLE_TESTS)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
Expand Down
15 changes: 15 additions & 0 deletions cmake/BuildParameters.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,28 @@ set(PCSX2_DEFS "")
#-------------------------------------------------------------------------------
option(DISABLE_BUILD_DATE "Disable including the binary compile date")
option(ENABLE_TESTS "Enables building the unit tests" ON)
option(LIBRETRO "Enables building the libretro core" OFF)
set(USE_SYSTEM_LIBS "AUTO" CACHE STRING "Use system libraries instead of bundled libraries. ON - Always use system and fail if unavailable, OFF - Always use bundled, AUTO - Use system if available, otherwise use bundled. Default is AUTO")
if(LIBRETRO)
set(ENABLE_TESTS OFF)
set(CMAKE_BUILD_PO FALSE)
set(BUILD_REPLAY_LOADERS FALSE)
set(CUBEB_API FALSE)
set(DISABLE_SETCAP TRUE)
set(USE_DISCORD_PRESENCE FALSE)
set(USE_ACHIEVEMENTS OFF)
set(QT_BUILD OFF)
set(USE_SYSTEM_LIBS OFF)
add_definitions(-D__LIBRETRO__)
endif()
optional_system_library(fmt)
optional_system_library(ryml)
optional_system_library(zstd)
optional_system_library(libzip)
optional_system_library(SDL2)
option(LTO_PCSX2_CORE "Enable LTO/IPO/LTCG on the subset of pcsx2 that benefits most from it but not anything else")


option(USE_VTUNE "Plug VTUNE to profile GS JIT.")
option(USE_ACHIEVEMENTS "Build with RetroAchievements support" ON)
option(USE_DISCORD_PRESENCE "Enable support for Discord Rich Presence" ON)
Expand Down
4 changes: 2 additions & 2 deletions common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ if(APPLE)
target_link_options(common PRIVATE -fobjc-link-runtime)
endif()

if(USE_OPENGL)
if(USE_OPENGL AND NOT LIBRETRO)
if(WIN32)
target_sources(common PRIVATE
GL/ContextWGL.cpp
Expand Down Expand Up @@ -310,6 +310,6 @@ target_include_directories(common PUBLIC ../3rdparty/include ../)
target_compile_definitions(common PUBLIC "${PCSX2_DEFS}")
target_compile_options(common PRIVATE "${PCSX2_WARNINGS}")

if(COMMAND target_precompile_headers)
if(COMMAND target_precompile_headers AND NOT CCACHE_FOUND)
target_precompile_headers(common PRIVATE PrecompiledHeader.h)
endif()
5 changes: 5 additions & 0 deletions common/Console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ static void MSW_OutputDebugString(const char* text)
static bool hasDebugger = IsDebuggerPresent();
if (hasDebugger)
OutputDebugStringA(text);
else
{
printf(text);
fflush(stdout);
}
#else
fputs(text, stdout_fp);
fflush(stdout_fp);
Expand Down
32 changes: 29 additions & 3 deletions common/D3D12/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
#include <queue>
#include <vector>

#ifdef __LIBRETRO__
#include <libretro_d3d.h>
extern retro_environment_t environ_cb;
retro_hw_render_interface_d3d12 *d3d12;
#endif

std::unique_ptr<D3D12::Context> g_d3d12_context;

using namespace D3D12;
Expand Down Expand Up @@ -136,6 +142,18 @@ bool Context::Create(IDXGIFactory5* dxgi_factory, IDXGIAdapter1* adapter, bool e
}

g_d3d12_context.reset(new Context());
#ifdef __LIBRETRO__
d3d12 = nullptr;
if (!environ_cb(RETRO_ENVIRONMENT_GET_HW_RENDER_INTERFACE, (void **)&d3d12) || !d3d12) {
printf("Failed to get HW rendering interface!\n");
return false;
}

if (d3d12->interface_version != RETRO_HW_RENDER_INTERFACE_D3D12_VERSION) {
printf("HW render interface mismatch, expected %u, got %u!\n", RETRO_HW_RENDER_INTERFACE_D3D12_VERSION, d3d12->interface_version);
return false;
}
#endif
if (!g_d3d12_context->CreateDevice(dxgi_factory, adapter, enable_debug_layer) ||
!g_d3d12_context->CreateCommandQueue() || !g_d3d12_context->CreateAllocator() ||
!g_d3d12_context->CreateFence() || !g_d3d12_context->CreateDescriptorHeaps() ||
Expand Down Expand Up @@ -171,6 +189,9 @@ u32 Context::GetAdapterVendorID() const

bool Context::CreateDevice(IDXGIFactory5* dxgi_factory, IDXGIAdapter1* adapter, bool enable_debug_layer)
{
#ifdef __LIBRETRO__
m_device = d3d12->device;
#else
HRESULT hr;

// Enabling the debug layer will fail if the Graphics Tools feature is not installed.
Expand All @@ -195,12 +216,12 @@ bool Context::CreateDevice(IDXGIFactory5* dxgi_factory, IDXGIAdapter1* adapter,
Console.Error("Failed to create D3D12 device: %08X", hr);
return false;
}

#endif
// get adapter
const LUID luid(m_device->GetAdapterLuid());
if (FAILED(dxgi_factory->EnumAdapterByLuid(luid, IID_PPV_ARGS(m_adapter.put()))))
Console.Error("Failed to get lookup adapter by device LUID");

#ifndef __LIBRETRO__
if (enable_debug_layer)
{
ComPtr<ID3D12InfoQueue> info_queue = m_device.try_query<ID3D12InfoQueue>();
Expand All @@ -225,17 +246,22 @@ bool Context::CreateDevice(IDXGIFactory5* dxgi_factory, IDXGIAdapter1* adapter,
info_queue->PushStorageFilter(&filter);
}
}

#endif
return true;
}

bool Context::CreateCommandQueue()
{
#ifdef __LIBRETRO__
m_command_queue = d3d12->queue;
return true;
#else
const D3D12_COMMAND_QUEUE_DESC queue_desc = {D3D12_COMMAND_LIST_TYPE_DIRECT, D3D12_COMMAND_QUEUE_PRIORITY_NORMAL,
D3D12_COMMAND_QUEUE_FLAG_NONE};
HRESULT hr = m_device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&m_command_queue));
pxAssertRel(SUCCEEDED(hr), "Create command queue");
return SUCCEEDED(hr);
#endif
}

bool Context::CreateAllocator()
Expand Down
8 changes: 7 additions & 1 deletion common/GL/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
#include <malloc.h>
#endif

#if defined(_WIN32) && !defined(_M_ARM64)
#if defined(__LIBRETRO__)
#include "common/GL/ContextRetroGL.h"
#elif defined(_WIN32) && !defined(_M_ARM64)
#include "common/GL/ContextWGL.h"
#elif defined(__APPLE__)
#include "common/GL/ContextAGL.h"
Expand Down Expand Up @@ -89,6 +91,9 @@ namespace GL
}

std::unique_ptr<Context> context;
#if defined(__LIBRETRO__)
context = ContextRetroGL::Create(wi, versions_to_try);
#else
#if defined(_WIN32) && !defined(_M_ARM64)
context = ContextWGL::Create(wi, versions_to_try);
#elif defined(__APPLE__)
Expand All @@ -103,6 +108,7 @@ namespace GL
#if defined(WAYLAND_API)
if (wi.type == WindowInfo::Type::Wayland)
context = ContextEGLWayland::Create(wi, versions_to_try);
#endif
#endif

if (!context)
Expand Down
93 changes: 93 additions & 0 deletions common/GL/ContextRetroGL.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2021 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCSX2.
* If not, see <http://www.gnu.org/licenses/>.
*/

#include "common/PrecompiledHeader.h"

#include "common/Console.h"
#include "ContextRetroGL.h"
#include <optional>
#include <vector>
#include "common/WindowInfo.h"
#include "GS/Renderers/Common/GSDevice.h"
#include "GS/GSVector.h"

#include <libretro.h>
extern retro_video_refresh_t video_cb;
extern retro_hw_render_callback hw_render;

namespace GL
{
ContextRetroGL::ContextRetroGL(const WindowInfo& wi)
: Context(wi)
{
}

ContextRetroGL::~ContextRetroGL()
{
}

std::unique_ptr<Context> ContextRetroGL::Create(const WindowInfo& wi, gsl::span<const Version> versions_to_try)
{
std::unique_ptr<ContextRetroGL> context = std::make_unique<ContextRetroGL>(wi);
return context;
}

void* ContextRetroGL::GetProcAddress(const char* name)
{
return reinterpret_cast<void*>(hw_render.get_proc_address(name));
}

bool ContextRetroGL::ChangeSurface(const WindowInfo& new_wi)
{
return true;
}

void ContextRetroGL::ResizeSurface(u32 new_surface_width /*= 0*/, u32 new_surface_height /*= 0*/)
{
m_wi.surface_width = new_surface_width;
m_wi.surface_height = new_surface_height;
}

bool ContextRetroGL::SwapBuffers()
{
if(g_gs_device->GetCurrent())
video_cb(RETRO_HW_FRAME_BUFFER_VALID, g_gs_device->GetCurrent()->GetWidth(), g_gs_device->GetCurrent()->GetHeight(), 0);
else
video_cb(NULL, 0, 0, 0);
return true;
}

bool ContextRetroGL::MakeCurrent()
{
return true;
}

bool ContextRetroGL::DoneCurrent()
{
return true;
}

bool ContextRetroGL::SetSwapInterval(s32 interval)
{
return true;
}

std::unique_ptr<Context> ContextRetroGL::CreateSharedContext(const WindowInfo& wi)
{
std::unique_ptr<ContextRetroGL> context = std::make_unique<ContextRetroGL>(wi);
return context;
}

} // namespace GL
40 changes: 40 additions & 0 deletions common/GL/ContextRetroGL.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2021 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCSX2.
* If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include "common/GL/Context.h"

namespace GL
{
class ContextRetroGL : public Context
{
public:
ContextRetroGL(const WindowInfo& wi);
~ContextRetroGL() override;

static std::unique_ptr<Context> Create(const WindowInfo& wi, gsl::span<const Version> versions_to_try);

void* GetProcAddress(const char* name) override;
virtual bool ChangeSurface(const WindowInfo& new_wi) override;
virtual void ResizeSurface(u32 new_surface_width = 0, u32 new_surface_height = 0) override;
bool SwapBuffers() override;
bool MakeCurrent() override;
bool DoneCurrent() override;
bool SetSwapInterval(s32 interval) override;
virtual std::unique_ptr<Context> CreateSharedContext(const WindowInfo& wi) override;
};

} // namespace GL
1 change: 1 addition & 0 deletions common/Misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* If not, see <http://www.gnu.org/licenses/>.
*/

#include <algorithm>
#include "General.h"
#include "Console.h"

Expand Down
1 change: 1 addition & 0 deletions common/Perf.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#pragma once

#include <cstdio>
#include <vector>
#include <cstdio>
#include "common/Pcsx2Types.h"
Expand Down
1 change: 1 addition & 0 deletions common/Vulkan/Builders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* If not, see <http://www.gnu.org/licenses/>.
*/

#include <limits>
#include "common/Vulkan/Builders.h"
#include "common/Vulkan/Util.h"
#include "common/Assertions.h"
Expand Down
3 changes: 2 additions & 1 deletion common/WindowInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ struct WindowInfo
Win32,
X11,
Wayland,
MacOS
MacOS,
Libretro
};

/// The type of the surface. Surfaceless indicates it will not be displayed on screen at all.
Expand Down
Loading

0 comments on commit 502b6f5

Please sign in to comment.