Skip to content

Commit

Permalink
Vita port.
Browse files Browse the repository at this point in the history
Port of https://github.com/Rinnegatamante/flycast-vita.
Credits to @bythos14 for everything concerning the dynarec implementation and page fault handler.
  • Loading branch information
Rinnegatamante authored and scribam committed Sep 25, 2023
1 parent a869e18 commit c446b26
Show file tree
Hide file tree
Showing 45 changed files with 1,336 additions and 48 deletions.
105 changes: 97 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ if(APPLE)
endif()
endif()

if (VITA)
if(NOT DEFINED CMAKE_TOOLCHAIN_FILE)
if(DEFINED ENV{VITASDK})
set(CMAKE_TOOLCHAIN_FILE "$ENV{VITASDK}/share/vita.toolchain.cmake" CACHE PATH "toolchain file")
else()
message(FATAL_ERROR "Please define VITASDK to point to your SDK path!")
endif()
endif()
endif()

if(LIBRETRO)
project(flycast_libretro)
else()
Expand Down Expand Up @@ -111,7 +121,32 @@ endif()
string(TIMESTAMP BUILD_TIMESTAMP UTC)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/core/version.h.in" "${CMAKE_CURRENT_SOURCE_DIR}/core/version.h" @ONLY)

if(NINTENDO_SWITCH)
if(VITA)
include("${VITASDK}/share/vita.cmake" REQUIRED)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I${VITASDK}/arm-vita-eabi/include/SDL2 -Wl,-q,--wrap,memcpy,--wrap,memset,--wrap,malloc,--wrap,memalign,--wrap,free,--wrap,calloc,--wrap,realloc -O3 -DNDEBUG -fno-optimize-sibling-calls -fsigned-char -fno-short-enums -marm")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -I${VITASDK}/arm-vita-eabi/include/SDL2 -Wl,-q,--wrap,malloc,--wrap,memalign,--wrap,free,--wrap,calloc,--wrap,realloc -O3 -DNDEBUG -fno-optimize-sibling-calls -fsigned-char -fno-short-enums -marm")

set(VITA_APP_NAME "Flycast")
set(VITA_TITLEID "FLYCASTDC")
set(VITA_VPKNAME "Flycast")
set(VITA_VERSION "01.00")
set(VITA_MKSFOEX_FLAGS "-d ATTRIBUTE2=12")

set(USE_VULKAN OFF)
set(USE_GLES2 ON)
enable_language(ASM)

add_executable(${PROJECT_NAME} core/emulator.cpp)
target_compile_definitions(${PROJECT_NAME} PRIVATE EGL_NO_PLATFORM_SPECIFIC_TYPES)
# target_compile_definitions(${PROJECT_NAME} PRIVATE TARGET_NO_EXCEPTIONS)
target_compile_definitions(${PROJECT_NAME} PRIVATE VITA _GNU_SOURCE)
target_compile_definitions(${PROJECT_NAME} PRIVATE GLES)

# For proper fseek/ftell definition in external dependency "libchdr", doesn't impact anything else:
target_compile_definitions(${PROJECT_NAME} PRIVATE __PS3__)

elseif(NINTENDO_SWITCH)
set(USE_VULKAN OFF)
enable_language(ASM)

Expand Down Expand Up @@ -200,9 +235,11 @@ if(WINDOWS_STORE)
set(USE_VULKAN OFF)
endif()

set_target_properties(${PROJECT_NAME} PROPERTIES
CXX_EXTENSIONS OFF
LINK_FLAGS_RELEASE -s)
if(NOT VITA)
set_target_properties(${PROJECT_NAME} PROPERTIES
CXX_EXTENSIONS OFF
LINK_FLAGS_RELEASE -s)
endif()
if(MSVC)
target_compile_options(${PROJECT_NAME} PRIVATE /GR /GS-)
if(WINDOWS_STORE)
Expand Down Expand Up @@ -400,6 +437,9 @@ if(NOT LIBRETRO)
if(USE_HOST_SDL)
find_package(SDL2)
endif()
if(VITA)
set(SDL2_FOUND 1)
endif()
if(NOT SDL2_FOUND)
add_subdirectory(core/deps/SDL EXCLUDE_FROM_ALL)
set(SDL2_FOUND 1)
Expand All @@ -411,7 +451,7 @@ if(NOT LIBRETRO)
target_link_libraries(${PROJECT_NAME} PRIVATE SDL2::SDL2main)
endif()

if((APPLE OR WIN32) AND TARGET SDL2::SDL2-static)
if((APPLE OR WIN32 OR VITA) AND TARGET SDL2::SDL2-static)
target_link_libraries(${PROJECT_NAME} PRIVATE SDL2::SDL2-static)
elseif(TARGET SDL2::SDL2)
target_link_libraries(${PROJECT_NAME} PRIVATE SDL2::SDL2)
Expand Down Expand Up @@ -510,6 +550,10 @@ if(NINTENDO_SWITCH AND USE_GLES)
target_link_libraries(${PROJECT_NAME} PRIVATE PkgConfig::GLESV2)
endif()

if(VITA)
target_link_libraries(${PROJECT_NAME} PRIVATE vitaGL vitashark SceShaccCg_stub SceShaccCgExt taihen_stub mathneon curl ssl crypto)
endif()

if(UNIX AND NOT APPLE AND NOT ANDROID)
add_definitions(
-DFLYCAST_DATADIR="${CMAKE_INSTALL_FULL_DATADIR}/${PROJECT_NAME}/"
Expand Down Expand Up @@ -598,7 +642,7 @@ target_compile_definitions(nowide PRIVATE $<$<BOOL:${NINTENDO_SWITCH}>:_DEFAULT_
target_link_libraries(${PROJECT_NAME} PRIVATE nowide::nowide)

if(NOT MINIUPNP_FOUND)
if(NINTENDO_SWITCH)
if(NINTENDO_SWITCH OR VITA)
target_compile_definitions(${PROJECT_NAME} PRIVATE FEAT_NO_MINIUPNPC)
else()
option(UPNPC_BUILD_SHARED "Build shared library" OFF)
Expand All @@ -615,7 +659,7 @@ if(NOT MINIUPNP_FOUND)
endif()
endif()

if(NOT LIBZIP_FOUND OR NINTENDO_SWITCH)
if(NOT LIBZIP_FOUND OR NINTENDO_SWITCH OR VITA)
option(ENABLE_COMMONCRYPTO "Enable use of CommonCrypto" OFF)
option(ENABLE_GNUTLS "Enable use of GnuTLS" OFF)
option(ENABLE_MBEDTLS "Enable use of mbed TLS" OFF)
Expand All @@ -635,6 +679,32 @@ if(NOT LIBZIP_FOUND OR NINTENDO_SWITCH)
target_link_libraries(${PROJECT_NAME} PRIVATE zip)
endif()

if(VITA)
target_link_libraries(${PROJECT_NAME} PRIVATE
-Wl,--whole-archive pthread -Wl,--no-whole-archive
stdc++
SDL2
kubridge_stub
ScePgf_stub
SceHid_stub
SceAppMgr_stub
SceAppUtil_stub
SceAudio_stub
SceAudioIn_stub
SceDisplay_stub
SceKernelDmacMgr_stub
SceIme_stub
ScePower_stub
SceSysmodule_stub
SceTouch_stub
SceMotion_stub
SceCommonDialog_stub
SceCtrl_stub
SceGxm_stub
SceVshBridge_stub
)
endif()

if(WIN32)
target_include_directories(${PROJECT_NAME} PRIVATE core/deps/dirent)
endif()
Expand Down Expand Up @@ -1023,6 +1093,12 @@ if(WIN32)
core/windows/fault_handler.cpp
core/windows/unwind_info.cpp
core/windows/win_vmem.cpp)
elseif(VITA)
target_sources(${PROJECT_NAME} PRIVATE
core/linux/common.cpp
core/linux/unwind_info.cpp
core/linux/vita_fault_handler.cpp
core/linux/vita_vmem.cpp)
else()
target_sources(${PROJECT_NAME} PRIVATE
core/linux/common.cpp
Expand Down Expand Up @@ -1702,7 +1778,7 @@ if(NOT LIBRETRO)
${CMAKE_CURRENT_BINARY_DIR}/Flycast.app/Contents/Frameworks/libvulkan.dylib)
endif()
endif()
elseif(UNIX OR NINTENDO_SWITCH)
elseif(UNIX OR NINTENDO_SWITCH OR VITA)
if(NOT BUILD_TESTING)
target_sources(${PROJECT_NAME} PRIVATE
core/linux-dist/main.cpp)
Expand Down Expand Up @@ -1787,6 +1863,19 @@ if(NINTENDO_SWITCH)
endif()
endif()

if(VITA)
vita_create_self(eboot.bin ${PROJECT_NAME} UNSAFE STRIPPED NOASLR)
vita_create_vpk(${VITA_VPKNAME}.vpk ${VITA_TITLEID} eboot.bin
VERSION ${VITA_VERSION}
NAME ${VITA_APP_NAME}
FILE ${CMAKE_SOURCE_DIR}/shell/vita/icon0.png sce_sys/icon0.png
${CMAKE_SOURCE_DIR}/shell/vita/startup.png sce_sys/livearea/contents/startup.png
${CMAKE_SOURCE_DIR}/shell/vita/bg.png sce_sys/livearea/contents/bg.png
${CMAKE_SOURCE_DIR}/shell/vita/template.xml sce_sys/livearea/contents/template.xml
${VPK_INCLUDES}
)
endif()

if(IOS)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>-${CMAKE_OSX_SYSROOT}/Flycast.ipa" TYPE BIN)
elseif(NINTENDO_SWITCH AND NOT LIBRETRO)
Expand Down
2 changes: 1 addition & 1 deletion core/build.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
#endif

#if !defined(__ANDROID__) && !defined(TARGET_IPHONE) && !defined(TARGET_UWP) \
&& !defined(__SWITCH__) && !defined(LIBRETRO) && !defined(__NetBSD__) && !defined(__OpenBSD__)
&& !defined(__SWITCH__) && !defined(LIBRETRO) && !defined(__NetBSD__) && !defined(__OpenBSD__) && !defined(__vita__)
#define NAOMI_MULTIBOARD
#endif

Expand Down
12 changes: 12 additions & 0 deletions core/cfg/option.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ namespace config {

Option<bool> DynarecEnabled("Dynarec.Enabled", true);
Option<int> Sh4Clock("Sh4Clock", 200);
#ifdef __vita__
Option<int> DynarecSmcChecks("Dynarec.smcChecks", 0);
#endif

// General

Expand All @@ -38,8 +41,13 @@ Option<bool> AutoLoadState("Dreamcast.AutoLoadState");
Option<bool> AutoSaveState("Dreamcast.AutoSaveState");
Option<int, false> SavestateSlot("Dreamcast.SavestateSlot");
Option<bool> ForceFreePlay("ForceFreePlay", true);
#ifdef __vita__
Option<bool, false> FetchBoxart("FetchBoxart", false);
Option<bool, false> BoxartDisplayMode("BoxartDisplayMode", false);
#else
Option<bool, false> FetchBoxart("FetchBoxart", true);
Option<bool, false> BoxartDisplayMode("BoxartDisplayMode", true);
#endif

// Sound

Expand Down Expand Up @@ -81,6 +89,10 @@ Option<bool> Fog("rend.Fog", true);
Option<bool> FloatVMUs("rend.FloatVMUs");
Option<bool> Rotate90("rend.Rotate90");
Option<bool> PerStripSorting("rend.PerStripSorting");
#ifdef __vita__
Option<bool> UseSimpleShaders("rend.UseSimpleShaders", true);
Option<bool> FastSorting("rend.FastSorting", true);
#endif
#ifdef __APPLE__
Option<bool> DelayFrameSwapping("rend.DelayFrameSwapping", false);
#else
Expand Down
5 changes: 5 additions & 0 deletions core/cfg/option.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,11 @@ extern Option<bool> DynarecEnabled;
#ifndef LIBRETRO
extern Option<int> Sh4Clock;
#endif
#ifdef __vita__
extern Option<int> DynarecSmcChecks;
extern Option<bool> FastSorting;
extern Option<bool> UseSimpleShaders;
#endif

// General

Expand Down
2 changes: 1 addition & 1 deletion core/deps/ggpo/lib/ggpo/ggpo_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class GGPOException : public std::runtime_error {
*/
#if defined(_WIN32)
# include "platform_windows.h"
#elif defined(__unix__) || defined(__APPLE__) || defined(__SWITCH__)
#elif defined(__unix__) || defined(__APPLE__) || defined(__SWITCH__) || defined(__vita__)
# include "platform_linux.h"
#else
# error Unsupported platform
Expand Down
2 changes: 1 addition & 1 deletion core/deps/ggpo/lib/ggpo/platform_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Use of this software is governed by the MIT license that can be found
* in the LICENSE file.
*/
#if defined(__unix__) || defined(__APPLE__) || defined(__SWITCH__)
#if defined(__unix__) || defined(__APPLE__) || defined(__SWITCH__) || defined(__vita__)

#include "platform_linux.h"
#include <time.h>
Expand Down
2 changes: 1 addition & 1 deletion core/deps/ggpo/lib/ggpo/platform_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <netinet/in.h>
#ifdef __SWITCH__
#include "nswitch.h"
#else
#elif !defined(__vita__)
#include <netinet/ip.h>
#endif
#include <netinet/tcp.h>
Expand Down
48 changes: 48 additions & 0 deletions core/deps/imgui/backends/imgui_impl_opengl3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,17 @@ bool ImGui_ImplOpenGL3_Init()

// Store GLSL version string so we can refer to it later in case we recreate shaders. Note: GLSL version is NOT the same as GL version. Leave this to NULL if unsure.
const char* glsl_version;
#ifdef __vita__
glsl_version = "";
#else
if (theGLContext.isGLES())
glsl_version = "#version 100"; // OpenGL ES 2.0
else
#if defined(__APPLE__)
glsl_version = "#version 140"; // OpenGL 3.1
#else
glsl_version = "#version 130"; // OpenGL 3.0
#endif
#endif
IM_ASSERT((int)strlen(glsl_version) + 2 < IM_ARRAYSIZE(g_GlslVersionString));
strcpy(g_GlslVersionString, glsl_version);
Expand Down Expand Up @@ -252,8 +256,10 @@ static bool ImGui_ImplOpenGL3_CreateFontsTexture()
glcache.BindTexture(GL_TEXTURE_2D, g_FontTexture);
glcache.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glcache.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
#if !defined(__vita__)
if (theGLContext.getMajorVersion() >= 3)
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
#endif
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

// Store our identifier
Expand Down Expand Up @@ -313,6 +319,43 @@ static bool ImGui_ImplOpenGL3_CreateDeviceObjects()
{
// Parse GLSL version string
int glsl_version = 130;
#ifdef __vita__
const GLchar* vertex_shader_glsl_120 = "";

const GLchar* vertex_shader_glsl_130 =
"uniform float4x4 ProjMtx;\n"
"void main(\n"
"float2 Position,\n"
"float2 UV,\n"
"float4 Color,\n"
"float2 out Frag_UV : TEXCOORD0,\n"
"float4 out Frag_Color : COLOR,\n"
"float4 out gl_Position : POSITION\n"
") {\n"
" Frag_UV = UV;\n"
" Frag_Color = Color;\n"
" gl_Position = mul(float4(Position.xy, 0, 1), ProjMtx);\n"
"}\n";

const GLchar* vertex_shader_glsl_300_es = "";

const GLchar* vertex_shader_glsl_410_core = "";

const GLchar* fragment_shader_glsl_120 = "";

const GLchar* fragment_shader_glsl_130 =
"uniform sampler2D tex;\n"
"float4 main(\n"
"float2 Frag_UV : TEXCOORD0,\n"
"float4 Frag_Color : COLOR\n"
") {\n"
" return Frag_Color * tex2D(tex, Frag_UV);\n"
"}\n";

const GLchar* fragment_shader_glsl_300_es = "";

const GLchar* fragment_shader_glsl_410_core = "";
#else
sscanf(g_GlslVersionString, "#version %d", &glsl_version);

const GLchar* vertex_shader_glsl_120 =
Expand Down Expand Up @@ -414,6 +457,7 @@ static bool ImGui_ImplOpenGL3_CreateDeviceObjects()
"{\n"
" Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
"}\n";
#endif

// Select shaders matching our GLSL versions
const GLchar* vertex_shader = NULL;
Expand Down Expand Up @@ -463,7 +507,11 @@ static bool ImGui_ImplOpenGL3_CreateDeviceObjects()
glDeleteShader(vert_handle);
glDeleteShader(frag_handle);

#ifdef __vita__
g_AttribLocationTex = glGetUniformLocation(g_ShaderHandle, "tex");
#else
g_AttribLocationTex = glGetUniformLocation(g_ShaderHandle, "Texture");
#endif
g_AttribLocationProjMtx = glGetUniformLocation(g_ShaderHandle, "ProjMtx");
g_AttribLocationPosition = glGetAttribLocation(g_ShaderHandle, "Position");
g_AttribLocationUV = glGetAttribLocation(g_ShaderHandle, "UV");
Expand Down
Loading

0 comments on commit c446b26

Please sign in to comment.