From 88426e0399849601c0e34b6f9848b76deba62234 Mon Sep 17 00:00:00 2001 From: "Daniel R." <47796739+polybiusproxy@users.noreply.github.com> Date: Fri, 12 Jul 2024 11:00:11 +0200 Subject: [PATCH 01/34] gnmdriver: Implement shader functions --- src/core/libraries/gnmdriver/gnmdriver.cpp | 141 +++++++++++++++++++-- src/core/libraries/gnmdriver/gnmdriver.h | 10 +- 2 files changed, 136 insertions(+), 15 deletions(-) diff --git a/src/core/libraries/gnmdriver/gnmdriver.cpp b/src/core/libraries/gnmdriver/gnmdriver.cpp index dcf6d99e..f057f4ed 100644 --- a/src/core/libraries/gnmdriver/gnmdriver.cpp +++ b/src/core/libraries/gnmdriver/gnmdriver.cpp @@ -1453,8 +1453,33 @@ s32 PS4_SYSV_ABI sceGnmSetEmbeddedVsShader(u32* cmdbuf, u32 size, u32 shader_id, return ORBIS_OK; } -int PS4_SYSV_ABI sceGnmSetEsShader() { - LOG_ERROR(Lib_GnmDriver, "(STUBBED) called"); +s32 PS4_SYSV_ABI sceGnmSetEsShader(u32* cmdbuf, u32 size, const u32* es_regs, u32 shader_modifier) { + LOG_TRACE(Lib_GnmDriver, "called"); + + if (!cmdbuf || size < 0x14) { + return -1; + } + + if (!es_regs) { + LOG_ERROR(Lib_GnmDriver, "Null pointer passed as argument"); + return -1; + } + + if (shader_modifier & 0xfcfffc3f) { + LOG_ERROR(Lib_GnmDriver, "Invalid modifier mask"); + return -1; + } + + if (es_regs[1] != 0) { + LOG_ERROR(Lib_GnmDriver, "Invalid shader address"); + return -1; + } + + const u32 var = shader_modifier == 0 ? es_regs[2] : (es_regs[2] & 0xfcfffc3f | shader_modifier); + cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0xc8u, es_regs[0], 0u); // SPI_SHADER_PGM_LO_ES + cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0xcau, var, es_regs[3]); // SPI_SHADER_PGM_RSRC1_ES + + WriteTrailingNop<11>(cmdbuf); return ORBIS_OK; } @@ -1463,18 +1488,91 @@ int PS4_SYSV_ABI sceGnmSetGsRingSizes() { return ORBIS_OK; } -int PS4_SYSV_ABI sceGnmSetGsShader() { - LOG_ERROR(Lib_GnmDriver, "(STUBBED) called"); +s32 PS4_SYSV_ABI sceGnmSetGsShader(u32* cmdbuf, u32 size, const u32* gs_regs) { + LOG_TRACE(Lib_GnmDriver, "called"); + + if (!cmdbuf || size < 0x1d) { + return -1; + } + + if (!gs_regs) { + LOG_ERROR(Lib_GnmDriver, "Null pointer passed as argument"); + return -1; + } + + if (gs_regs[1] != 0) { + LOG_ERROR(Lib_GnmDriver, "Invalid shader address"); + return -1; + } + + cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x88u, gs_regs[0], 0u); // SPI_SHADER_PGM_LO_GS + cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x8au, gs_regs[1], + gs_regs[1]); // SPI_SHADER_PGM_RSRC1_GS + + cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x2e5u, gs_regs[4]); // VGT_STRMOUT_CONFIG + cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x29bu, gs_regs[5]); // VGT_GS_OUT_PRIM_TYPE + cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x2e4u, gs_regs[6]); // VGT_GS_INSTANCE_CNT + + WriteTrailingNop<11>(cmdbuf); return ORBIS_OK; } -int PS4_SYSV_ABI sceGnmSetHsShader() { - LOG_ERROR(Lib_GnmDriver, "(STUBBED) called"); +s32 PS4_SYSV_ABI sceGnmSetHsShader(u32* cmdbuf, u32 size, const u32* hs_regs, u32 param4) { + LOG_TRACE(Lib_GnmDriver, "called"); + + if (!cmdbuf || size < 0x1E) { + return -1; + } + + if (!hs_regs) { + LOG_ERROR(Lib_GnmDriver, "Null pointer passed as argument"); + return -1; + } + + if (hs_regs[1] != 0) { + LOG_ERROR(Lib_GnmDriver, "Invalid shader address"); + return -1; + } + + cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x108u, hs_regs[0], 0u); // SPI_SHADER_PGM_LO_HS + cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x10au, hs_regs[1], hs_regs[1]); // SPI_SHADER_PGM_RSRC1_HS + + cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x286u, hs_regs[5], hs_regs[5]); // VGT_HOS_MAX_TESS_LEVEL + cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x2dbu, hs_regs[4]); // VGT_TF_PARAM + cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x2d6u, param4); // VGT_LS_HS_CONFIG + + WriteTrailingNop<11>(cmdbuf); return ORBIS_OK; } -int PS4_SYSV_ABI sceGnmSetLsShader() { - LOG_ERROR(Lib_GnmDriver, "(STUBBED) called"); +s32 PS4_SYSV_ABI sceGnmSetLsShader(u32* cmdbuf, u32 size, const u32* ls_regs, u32 shader_modifier) { + LOG_TRACE(Lib_GnmDriver, "called"); + + if (!cmdbuf || size < 0x17) { + return -1; + } + + if (!ls_regs) { + LOG_ERROR(Lib_GnmDriver, "Null pointer passed as argument"); + return -1; + } + + if (shader_modifier & 0xfcfffc3f) { + LOG_ERROR(Lib_GnmDriver, "Invalid modifier mask"); + return -1; + } + + if (ls_regs[1] != 0) { + LOG_ERROR(Lib_GnmDriver, "Invalid shader address"); + return -1; + } + + const u32 var = shader_modifier == 0 ? ls_regs[2] : (ls_regs[2] & 0xfcfffc3f | shader_modifier); + cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x148u, ls_regs[0], 0u); // SPI_SHADER_PGM_LO_LS + cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x14bu, ls_regs[3]); // SPI_SHADER_PGM_RSRC2_LS + cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x14au, var, ls_regs[3]); // SPI_SHADER_PGM_RSRC1_LS + + WriteTrailingNop<11>(cmdbuf); return ORBIS_OK; } @@ -2027,8 +2125,31 @@ int PS4_SYSV_ABI sceGnmUnregisterResource() { return ORBIS_OK; } -int PS4_SYSV_ABI sceGnmUpdateGsShader() { - LOG_ERROR(Lib_GnmDriver, "(STUBBED) called"); +s32 PS4_SYSV_ABI sceGnmUpdateGsShader(u32* cmdbuf, u32 size, const u32* gs_regs) { + LOG_TRACE(Lib_GnmDriver, "called"); + + if (!cmdbuf || size < 0x1d) { + return -1; + } + + if (!gs_regs) { + LOG_ERROR(Lib_GnmDriver, "Null pointer passed as argument"); + return -1; + } + + if (gs_regs[1] != 0) { + LOG_ERROR(Lib_GnmDriver, "Invalid shader address"); + return -1; + } + + cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x88u, gs_regs[0], 0u); // SPI_SHADER_PGM_LO_GS + cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x8au, gs_regs[1], gs_regs[1]); // SPI_SHADER_PGM_RSRC1_GS + + cmdbuf = WritePacket(cmdbuf, PM4ShaderType::ShaderGraphics, 0xc01e02e5u, gs_regs[4]); + cmdbuf = WritePacket(cmdbuf, PM4ShaderType::ShaderGraphics, 0xc01e029bu, gs_regs[5]); + cmdbuf = WritePacket(cmdbuf, PM4ShaderType::ShaderGraphics, 0xc01e02e4u, gs_regs[6]); + + WriteTrailingNop<11>(cmdbuf); return ORBIS_OK; } diff --git a/src/core/libraries/gnmdriver/gnmdriver.h b/src/core/libraries/gnmdriver/gnmdriver.h index 808cdf51..8100b116 100644 --- a/src/core/libraries/gnmdriver/gnmdriver.h +++ b/src/core/libraries/gnmdriver/gnmdriver.h @@ -149,11 +149,11 @@ s32 PS4_SYSV_ABI sceGnmSetCsShaderWithModifier(u32* cmdbuf, u32 size, const u32* s32 PS4_SYSV_ABI sceGnmSetEmbeddedPsShader(u32* cmdbuf, u32 size, u32 shader_id, u32 shader_modifier); s32 PS4_SYSV_ABI sceGnmSetEmbeddedVsShader(u32* cmdbuf, u32 size, u32 shader_id, u32 modifier); -int PS4_SYSV_ABI sceGnmSetEsShader(); +s32 PS4_SYSV_ABI sceGnmSetEsShader(u32* cmdbuf, u32 size, const u32* es_regs, u32 shader_modifier); int PS4_SYSV_ABI sceGnmSetGsRingSizes(); -int PS4_SYSV_ABI sceGnmSetGsShader(); -int PS4_SYSV_ABI sceGnmSetHsShader(); -int PS4_SYSV_ABI sceGnmSetLsShader(); +s32 PS4_SYSV_ABI sceGnmSetGsShader(u32* cmdbuf, u32 size, const u32* gs_regs); +s32 PS4_SYSV_ABI sceGnmSetHsShader(u32* cmdbuf, u32 size, const u32* hs_regs, u32 param4); +s32 PS4_SYSV_ABI sceGnmSetLsShader(u32* cmdbuf, u32 size, const u32* ls_regs, u32 shader_modifier); s32 PS4_SYSV_ABI sceGnmSetPsShader(u32* cmdbuf, u32 size, const u32* ps_regs); s32 PS4_SYSV_ABI sceGnmSetPsShader350(u32* cmdbuf, u32 size, const u32* ps_regs); int PS4_SYSV_ABI sceGnmSetResourceRegistrationUserMemory(); @@ -216,7 +216,7 @@ int PS4_SYSV_ABI sceGnmUnmapComputeQueue(); int PS4_SYSV_ABI sceGnmUnregisterAllResourcesForOwner(); int PS4_SYSV_ABI sceGnmUnregisterOwnerAndResources(); int PS4_SYSV_ABI sceGnmUnregisterResource(); -int PS4_SYSV_ABI sceGnmUpdateGsShader(); +s32 PS4_SYSV_ABI sceGnmUpdateGsShader(u32* cmdbuf, u32 size, const u32* gs_regs); int PS4_SYSV_ABI sceGnmUpdateHsShader(); s32 PS4_SYSV_ABI sceGnmUpdatePsShader(u32* cmdbuf, u32 size, const u32* ps_regs); s32 PS4_SYSV_ABI sceGnmUpdatePsShader350(u32* cmdbuf, u32 size, const u32* ps_regs); From 0649234fc2f9bbc98f71cd8838030c708a9b27d3 Mon Sep 17 00:00:00 2001 From: "Daniel R." <47796739+polybiusproxy@users.noreply.github.com> Date: Fri, 12 Jul 2024 11:03:55 +0200 Subject: [PATCH 02/34] `clang-format` --- src/core/libraries/gnmdriver/gnmdriver.cpp | 36 +++++++++++++--------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/core/libraries/gnmdriver/gnmdriver.cpp b/src/core/libraries/gnmdriver/gnmdriver.cpp index f057f4ed..9c62c129 100644 --- a/src/core/libraries/gnmdriver/gnmdriver.cpp +++ b/src/core/libraries/gnmdriver/gnmdriver.cpp @@ -1505,9 +1505,9 @@ s32 PS4_SYSV_ABI sceGnmSetGsShader(u32* cmdbuf, u32 size, const u32* gs_regs) { return -1; } - cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x88u, gs_regs[0], 0u); // SPI_SHADER_PGM_LO_GS + cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x88u, gs_regs[0], 0u); // SPI_SHADER_PGM_LO_GS cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x8au, gs_regs[1], - gs_regs[1]); // SPI_SHADER_PGM_RSRC1_GS + gs_regs[1]); // SPI_SHADER_PGM_RSRC1_GS cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x2e5u, gs_regs[4]); // VGT_STRMOUT_CONFIG cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x29bu, gs_regs[5]); // VGT_GS_OUT_PRIM_TYPE @@ -1534,12 +1534,14 @@ s32 PS4_SYSV_ABI sceGnmSetHsShader(u32* cmdbuf, u32 size, const u32* hs_regs, u3 return -1; } - cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x108u, hs_regs[0], 0u); // SPI_SHADER_PGM_LO_HS - cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x10au, hs_regs[1], hs_regs[1]); // SPI_SHADER_PGM_RSRC1_HS + cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x108u, hs_regs[0], 0u); // SPI_SHADER_PGM_LO_HS + cmdbuf = + PM4CmdSetData::SetShReg(cmdbuf, 0x10au, hs_regs[1], hs_regs[1]); // SPI_SHADER_PGM_RSRC1_HS - cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x286u, hs_regs[5], hs_regs[5]); // VGT_HOS_MAX_TESS_LEVEL - cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x2dbu, hs_regs[4]); // VGT_TF_PARAM - cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x2d6u, param4); // VGT_LS_HS_CONFIG + cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x286u, hs_regs[5], + hs_regs[5]); // VGT_HOS_MAX_TESS_LEVEL + cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x2dbu, hs_regs[4]); // VGT_TF_PARAM + cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x2d6u, param4); // VGT_LS_HS_CONFIG WriteTrailingNop<11>(cmdbuf); return ORBIS_OK; @@ -1568,9 +1570,9 @@ s32 PS4_SYSV_ABI sceGnmSetLsShader(u32* cmdbuf, u32 size, const u32* ls_regs, u3 } const u32 var = shader_modifier == 0 ? ls_regs[2] : (ls_regs[2] & 0xfcfffc3f | shader_modifier); - cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x148u, ls_regs[0], 0u); // SPI_SHADER_PGM_LO_LS - cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x14bu, ls_regs[3]); // SPI_SHADER_PGM_RSRC2_LS - cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x14au, var, ls_regs[3]); // SPI_SHADER_PGM_RSRC1_LS + cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x148u, ls_regs[0], 0u); // SPI_SHADER_PGM_LO_LS + cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x14bu, ls_regs[3]); // SPI_SHADER_PGM_RSRC2_LS + cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x14au, var, ls_regs[3]); // SPI_SHADER_PGM_RSRC1_LS WriteTrailingNop<11>(cmdbuf); return ORBIS_OK; @@ -2142,12 +2144,16 @@ s32 PS4_SYSV_ABI sceGnmUpdateGsShader(u32* cmdbuf, u32 size, const u32* gs_regs) return -1; } - cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x88u, gs_regs[0], 0u); // SPI_SHADER_PGM_LO_GS - cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x8au, gs_regs[1], gs_regs[1]); // SPI_SHADER_PGM_RSRC1_GS + cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x88u, gs_regs[0], 0u); // SPI_SHADER_PGM_LO_GS + cmdbuf = + PM4CmdSetData::SetShReg(cmdbuf, 0x8au, gs_regs[1], gs_regs[1]); // SPI_SHADER_PGM_RSRC1_GS - cmdbuf = WritePacket(cmdbuf, PM4ShaderType::ShaderGraphics, 0xc01e02e5u, gs_regs[4]); - cmdbuf = WritePacket(cmdbuf, PM4ShaderType::ShaderGraphics, 0xc01e029bu, gs_regs[5]); - cmdbuf = WritePacket(cmdbuf, PM4ShaderType::ShaderGraphics, 0xc01e02e4u, gs_regs[6]); + cmdbuf = WritePacket(cmdbuf, PM4ShaderType::ShaderGraphics, 0xc01e02e5u, + gs_regs[4]); + cmdbuf = WritePacket(cmdbuf, PM4ShaderType::ShaderGraphics, 0xc01e029bu, + gs_regs[5]); + cmdbuf = WritePacket(cmdbuf, PM4ShaderType::ShaderGraphics, 0xc01e02e4u, + gs_regs[6]); WriteTrailingNop<11>(cmdbuf); return ORBIS_OK; From a1fbab5de7f3171d04cd66f0e534963ea880f6d6 Mon Sep 17 00:00:00 2001 From: Live session user Date: Sat, 13 Jul 2024 16:19:02 -0700 Subject: [PATCH 03/34] Move game_data to RW directory inside user folder for linux --- src/core/file_format/trp.cpp | 4 ++-- src/qt_gui/game_grid_frame.cpp | 9 +++++---- src/qt_gui/game_list_frame.cpp | 8 ++++---- src/qt_gui/main.cpp | 2 +- src/qt_gui/trophy_viewer.cpp | 4 ++-- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/core/file_format/trp.cpp b/src/core/file_format/trp.cpp index cb55af2e..b4d4c95e 100644 --- a/src/core/file_format/trp.cpp +++ b/src/core/file_format/trp.cpp @@ -48,7 +48,7 @@ bool TRP::Extract(std::filesystem::path trophyPath) { return false; s64 seekPos = sizeof(TrpHeader); - std::filesystem::path trpFilesPath(std::filesystem::current_path() / "game_data" / + std::filesystem::path trpFilesPath(std::filesystem::current_path() / "user/game_data" / title / "TrophyFiles" / it.path().stem()); std::filesystem::create_directories(trpFilesPath / "Icons"); std::filesystem::create_directory(trpFilesPath / "Xml"); @@ -88,4 +88,4 @@ bool TRP::Extract(std::filesystem::path trophyPath) { index++; } return true; -} \ No newline at end of file +} diff --git a/src/qt_gui/game_grid_frame.cpp b/src/qt_gui/game_grid_frame.cpp index ca28e9ce..80f4823b 100644 --- a/src/qt_gui/game_grid_frame.cpp +++ b/src/qt_gui/game_grid_frame.cpp @@ -114,8 +114,9 @@ void GameGridFrame::SetGridBackgroundImage(int row, int column) { if (item) { QString pic1Path = QString::fromStdString((*m_games_shared)[itemID].pic_path); QString blurredPic1Path = - qApp->applicationDirPath() + - QString::fromStdString("/game_data/" + (*m_games_shared)[itemID].serial + "/pic1.png"); + QDir::currentPath() + + QString::fromStdString("/user/game_data/" + (*m_games_shared)[itemID].serial + + "/pic1.png"); backgroundImage = QImage(blurredPic1Path); if (backgroundImage.isNull()) { @@ -123,7 +124,7 @@ void GameGridFrame::SetGridBackgroundImage(int row, int column) { backgroundImage = m_game_list_utils.BlurImage(image, image.rect(), 16); std::filesystem::path img_path = - std::filesystem::path("game_data/") / (*m_games_shared)[itemID].serial; + std::filesystem::path("user/game_data/") / (*m_games_shared)[itemID].serial; std::filesystem::create_directories(img_path); if (!backgroundImage.save(blurredPic1Path, "PNG")) { // qDebug() << "Error: Unable to save image."; @@ -142,4 +143,4 @@ void GameGridFrame::RefreshGridBackgroundImage() { palette.setColor(QPalette::Highlight, transparentColor); this->setPalette(palette); } -} \ No newline at end of file +} diff --git a/src/qt_gui/game_list_frame.cpp b/src/qt_gui/game_list_frame.cpp index 5bc00c7f..327da72f 100644 --- a/src/qt_gui/game_list_frame.cpp +++ b/src/qt_gui/game_list_frame.cpp @@ -99,8 +99,8 @@ void GameListFrame::SetListBackgroundImage(QTableWidgetItem* item) { QString pic1Path = QString::fromStdString(m_game_info->m_games[item->row()].pic_path); QString blurredPic1Path = - qApp->applicationDirPath() + - QString::fromStdString("/game_data/" + m_game_info->m_games[item->row()].serial + + QDir::currentPath() + + QString::fromStdString("/user/game_data/" + m_game_info->m_games[item->row()].serial + "/pic1.png"); backgroundImage = QImage(blurredPic1Path); @@ -109,7 +109,7 @@ void GameListFrame::SetListBackgroundImage(QTableWidgetItem* item) { backgroundImage = m_game_list_utils.BlurImage(image, image.rect(), 16); std::filesystem::path img_path = - std::filesystem::path("game_data/") / m_game_info->m_games[item->row()].serial; + std::filesystem::path("user/game_data/") / m_game_info->m_games[item->row()].serial; std::filesystem::create_directories(img_path); if (!backgroundImage.save(blurredPic1Path, "PNG")) { // qDebug() << "Error: Unable to save image."; @@ -206,4 +206,4 @@ void GameListFrame::SetRegionFlag(int row, int column, QString itemStr) { widget->setLayout(layout); this->setItem(row, column, item); this->setCellWidget(row, column, widget); -} \ No newline at end of file +} diff --git a/src/qt_gui/main.cpp b/src/qt_gui/main.cpp index 071e12b7..08c363b3 100644 --- a/src/qt_gui/main.cpp +++ b/src/qt_gui/main.cpp @@ -20,7 +20,7 @@ int main(int argc, char* argv[]) { // Load configurations and initialize Qt application const auto config_dir = Common::FS::GetUserPath(Common::FS::PathType::UserDir); Config::load(config_dir / "config.toml"); - QString gameDataPath = qApp->applicationDirPath() + "/game_data/"; + QString gameDataPath = QDir::currentPath() + "/user/game_data/"; std::string stdStr = gameDataPath.toStdString(); std::filesystem::path path(stdStr); #ifdef _WIN64 diff --git a/src/qt_gui/trophy_viewer.cpp b/src/qt_gui/trophy_viewer.cpp index 8c28019e..4293745e 100644 --- a/src/qt_gui/trophy_viewer.cpp +++ b/src/qt_gui/trophy_viewer.cpp @@ -19,7 +19,7 @@ TrophyViewer::TrophyViewer(QString trophyPath, QString gameTrpPath) : QMainWindo } void TrophyViewer::PopulateTrophyWidget(QString title) { - QString trophyDir = qApp->applicationDirPath() + "/game_data/" + title + "/TrophyFiles"; + QString trophyDir = QDir::currentPath() + "/user/game_data/" + title + "/TrophyFiles"; QDir dir(trophyDir); if (!dir.exists()) { std::filesystem::path path(gameTrpPath_.toStdString()); @@ -142,4 +142,4 @@ void TrophyViewer::SetTableItem(QTableWidget* parent, int row, int column, QStri widget->setLayout(layout); parent->setItem(row, column, item); parent->setCellWidget(row, column, widget); -} \ No newline at end of file +} From ed37fb32a7f3e73ae67b517148c02646afa917e4 Mon Sep 17 00:00:00 2001 From: psucien Date: Sun, 14 Jul 2024 23:25:41 +0200 Subject: [PATCH 04/34] review comments applied --- src/core/libraries/gnmdriver/gnmdriver.cpp | 66 +++++++++---------- .../backend/spirv/spirv_emit_context.cpp | 10 +-- src/shader_recompiler/runtime_info.h | 11 ++-- src/video_core/amdgpu/liverpool.h | 29 ++++++-- .../renderer_vulkan/vk_graphics_pipeline.cpp | 19 +++--- .../renderer_vulkan/vk_graphics_pipeline.h | 2 +- .../renderer_vulkan/vk_pipeline_cache.cpp | 6 ++ 7 files changed, 86 insertions(+), 57 deletions(-) diff --git a/src/core/libraries/gnmdriver/gnmdriver.cpp b/src/core/libraries/gnmdriver/gnmdriver.cpp index 9c62c129..8abcbbf7 100644 --- a/src/core/libraries/gnmdriver/gnmdriver.cpp +++ b/src/core/libraries/gnmdriver/gnmdriver.cpp @@ -1384,9 +1384,8 @@ s32 PS4_SYSV_ABI sceGnmSetEmbeddedPsShader(u32* cmdbuf, u32 size, u32 shader_id, // repeat set shader functionality here as it is trivial. cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 8u, ps_regs[0], 0u); // SPI_SHADER_PGM_LO_PS/SPI_SHADER_PGM_HI_PS - cmdbuf = - PM4CmdSetData::SetShReg(cmdbuf, 10u, ps_regs[2], - ps_regs[3]); // SPI_SHADER_USER_DATA_PS_4/SPI_SHADER_USER_DATA_PS_5 + cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 10u, ps_regs[2], + ps_regs[3]); // SPI_SHADER_PGM_RSRC1_PS/SPI_SHADER_PGM_RSRC2_PS cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x1c4u, ps_regs[4], ps_regs[5]); // SPI_SHADER_Z_FORMAT/SPI_SHADER_COL_FORMAT cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x1b3u, ps_regs[6], @@ -1443,11 +1442,11 @@ s32 PS4_SYSV_ABI sceGnmSetEmbeddedVsShader(u32* cmdbuf, u32 size, u32 shader_id, // pointer to a stack memory, so the check will likely fail. To workaround it we will // repeat set shader functionality here as it is trivial. cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x48u, vs_regs[0], vs_regs[1]); // SPI_SHADER_PGM_LO_VS - cmdbuf = - PM4CmdSetData::SetShReg(cmdbuf, 0x4au, vs_regs[2], vs_regs[3]); // SPI_SHADER_PGM_RSRC1_VS - cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x207u, vs_regs[6]); // PA_CL_VS_OUT_CNTL - cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x1b1u, vs_regs[4]); // SPI_VS_OUT_CONFIG - cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x1c3u, vs_regs[5]); // SPI_SHADER_POS_FORMAT + cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x4au, vs_regs[2], + vs_regs[3]); // SPI_SHADER_PGM_RSRC1_VS/SPI_SHADER_PGM_RSRC2_VS + cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x207u, vs_regs[6]); // PA_CL_VS_OUT_CNTL + cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x1b1u, vs_regs[4]); // SPI_VS_OUT_CONFIG + cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x1c3u, vs_regs[5]); // SPI_SHADER_POS_FORMAT WriteTrailingNop<11>(cmdbuf); return ORBIS_OK; @@ -1475,7 +1474,8 @@ s32 PS4_SYSV_ABI sceGnmSetEsShader(u32* cmdbuf, u32 size, const u32* es_regs, u3 return -1; } - const u32 var = shader_modifier == 0 ? es_regs[2] : (es_regs[2] & 0xfcfffc3f | shader_modifier); + const u32 var = + shader_modifier == 0 ? es_regs[2] : ((es_regs[2] & 0xfcfffc3f) | shader_modifier); cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0xc8u, es_regs[0], 0u); // SPI_SHADER_PGM_LO_ES cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0xcau, var, es_regs[3]); // SPI_SHADER_PGM_RSRC1_ES @@ -1506,9 +1506,8 @@ s32 PS4_SYSV_ABI sceGnmSetGsShader(u32* cmdbuf, u32 size, const u32* gs_regs) { } cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x88u, gs_regs[0], 0u); // SPI_SHADER_PGM_LO_GS - cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x8au, gs_regs[1], - gs_regs[1]); // SPI_SHADER_PGM_RSRC1_GS - + cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x8au, gs_regs[2], + gs_regs[3]); // SPI_SHADER_PGM_RSRC1_GS/SPI_SHADER_PGM_RSRC2_GS cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x2e5u, gs_regs[4]); // VGT_STRMOUT_CONFIG cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x29bu, gs_regs[5]); // VGT_GS_OUT_PRIM_TYPE cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x2e4u, gs_regs[6]); // VGT_GS_INSTANCE_CNT @@ -1535,9 +1534,8 @@ s32 PS4_SYSV_ABI sceGnmSetHsShader(u32* cmdbuf, u32 size, const u32* hs_regs, u3 } cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x108u, hs_regs[0], 0u); // SPI_SHADER_PGM_LO_HS - cmdbuf = - PM4CmdSetData::SetShReg(cmdbuf, 0x10au, hs_regs[1], hs_regs[1]); // SPI_SHADER_PGM_RSRC1_HS - + cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x10au, hs_regs[2], + hs_regs[3]); // SPI_SHADER_PGM_RSRC1_HS/SPI_SHADER_PGM_RSRC2_HS cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x286u, hs_regs[5], hs_regs[5]); // VGT_HOS_MAX_TESS_LEVEL cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x2dbu, hs_regs[4]); // VGT_TF_PARAM @@ -1559,7 +1557,8 @@ s32 PS4_SYSV_ABI sceGnmSetLsShader(u32* cmdbuf, u32 size, const u32* ls_regs, u3 return -1; } - if (shader_modifier & 0xfcfffc3f) { + const auto modifier_mask = ((shader_modifier & 0xfffffc3f) == 0) ? 0xfffffc3f : 0xfcfffc3f; + if (shader_modifier & modifier_mask) { LOG_ERROR(Lib_GnmDriver, "Invalid modifier mask"); return -1; } @@ -1569,7 +1568,8 @@ s32 PS4_SYSV_ABI sceGnmSetLsShader(u32* cmdbuf, u32 size, const u32* ls_regs, u3 return -1; } - const u32 var = shader_modifier == 0 ? ls_regs[2] : (ls_regs[2] & 0xfcfffc3f | shader_modifier); + const u32 var = + shader_modifier == 0 ? ls_regs[2] : ((ls_regs[2] & modifier_mask) | shader_modifier); cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x148u, ls_regs[0], 0u); // SPI_SHADER_PGM_LO_LS cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x14bu, ls_regs[3]); // SPI_SHADER_PGM_RSRC2_LS cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x14au, var, ls_regs[3]); // SPI_SHADER_PGM_RSRC1_LS @@ -1598,9 +1598,9 @@ s32 PS4_SYSV_ABI sceGnmSetPsShader(u32* cmdbuf, u32 size, const u32* ps_regs) { cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 8u, ps_regs[0], 0u); // SPI_SHADER_PGM_LO_PS/SPI_SHADER_PGM_HI_PS - cmdbuf = PM4CmdSetData::SetShReg( - cmdbuf, 10u, ps_regs[2], - ps_regs[3]); // SPI_SHADER_USER_DATA_PS_4/SPI_SHADER_USER_DATA_PS_5 + cmdbuf = + PM4CmdSetData::SetShReg(cmdbuf, 10u, ps_regs[2], + ps_regs[3]); // SPI_SHADER_PGM_RSRC1_PS/SPI_SHADER_PGM_RSRC2_PS cmdbuf = PM4CmdSetData::SetContextReg( cmdbuf, 0x1c4u, ps_regs[4], ps_regs[5]); // SPI_SHADER_Z_FORMAT/SPI_SHADER_COL_FORMAT cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x1b3u, ps_regs[6], @@ -1636,9 +1636,9 @@ s32 PS4_SYSV_ABI sceGnmSetPsShader350(u32* cmdbuf, u32 size, const u32* ps_regs) cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 8u, ps_regs[0], 0u); // SPI_SHADER_PGM_LO_PS/SPI_SHADER_PGM_HI_PS - cmdbuf = PM4CmdSetData::SetShReg( - cmdbuf, 10u, ps_regs[2], - ps_regs[3]); // SPI_SHADER_USER_DATA_PS_4/SPI_SHADER_USER_DATA_PS_5 + cmdbuf = + PM4CmdSetData::SetShReg(cmdbuf, 10u, ps_regs[2], + ps_regs[3]); // SPI_SHADER_PGM_RSRC1_PS/SPI_SHADER_PGM_RSRC2_PS cmdbuf = PM4CmdSetData::SetContextReg( cmdbuf, 0x1c4u, ps_regs[4], ps_regs[5]); // SPI_SHADER_Z_FORMAT/SPI_SHADER_COL_FORMAT cmdbuf = PM4CmdSetData::SetContextReg(cmdbuf, 0x1b3u, ps_regs[6], @@ -2145,9 +2145,8 @@ s32 PS4_SYSV_ABI sceGnmUpdateGsShader(u32* cmdbuf, u32 size, const u32* gs_regs) } cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x88u, gs_regs[0], 0u); // SPI_SHADER_PGM_LO_GS - cmdbuf = - PM4CmdSetData::SetShReg(cmdbuf, 0x8au, gs_regs[1], gs_regs[1]); // SPI_SHADER_PGM_RSRC1_GS - + cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x8au, gs_regs[2], + gs_regs[3]); // SPI_SHADER_PGM_RSRC1_GS/SPI_SHADER_PGM_RSRC2_GS cmdbuf = WritePacket(cmdbuf, PM4ShaderType::ShaderGraphics, 0xc01e02e5u, gs_regs[4]); cmdbuf = WritePacket(cmdbuf, PM4ShaderType::ShaderGraphics, 0xc01e029bu, @@ -2184,9 +2183,9 @@ s32 PS4_SYSV_ABI sceGnmUpdatePsShader(u32* cmdbuf, u32 size, const u32* ps_regs) cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 8u, ps_regs[0], 0u); // SPI_SHADER_PGM_LO_PS/SPI_SHADER_PGM_HI_PS - cmdbuf = PM4CmdSetData::SetShReg( - cmdbuf, 10u, ps_regs[2], - ps_regs[3]); // SPI_SHADER_USER_DATA_PS_4/SPI_SHADER_USER_DATA_PS_5 + cmdbuf = + PM4CmdSetData::SetShReg(cmdbuf, 10u, ps_regs[2], + ps_regs[3]); // SPI_SHADER_PGM_RSRC1_PS/SPI_SHADER_PGM_RSRC2_PS cmdbuf = WritePacket( cmdbuf, PM4ShaderType::ShaderGraphics, 0xc01e01c4u, ps_regs[4], ps_regs[5]); // SPI_SHADER_Z_FORMAT/SPI_SHADER_COL_FORMAT update @@ -2229,9 +2228,9 @@ s32 PS4_SYSV_ABI sceGnmUpdatePsShader350(u32* cmdbuf, u32 size, const u32* ps_re cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 8u, ps_regs[0], 0u); // SPI_SHADER_PGM_LO_PS/SPI_SHADER_PGM_HI_PS - cmdbuf = PM4CmdSetData::SetShReg( - cmdbuf, 10u, ps_regs[2], - ps_regs[3]); // SPI_SHADER_USER_DATA_PS_4/SPI_SHADER_USER_DATA_PS_5 + cmdbuf = + PM4CmdSetData::SetShReg(cmdbuf, 10u, ps_regs[2], + ps_regs[3]); // SPI_SHADER_PGM_RSRC1_PS/SPI_SHADER_PGM_RSRC2_PS cmdbuf = WritePacket( cmdbuf, PM4ShaderType::ShaderGraphics, 0xc01e01c4u, ps_regs[4], ps_regs[5]); // SPI_SHADER_Z_FORMAT/SPI_SHADER_COL_FORMAT update @@ -2275,7 +2274,8 @@ s32 PS4_SYSV_ABI sceGnmUpdateVsShader(u32* cmdbuf, u32 size, const u32* vs_regs, return -1; } - const u32 var = shader_modifier == 0 ? vs_regs[2] : (vs_regs[2] & 0xfcfffc3f | shader_modifier); + const u32 var = + shader_modifier == 0 ? vs_regs[2] : ((vs_regs[2] & 0xfcfffc3f) | shader_modifier); cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x48u, vs_regs[0], 0u); // SPI_SHADER_PGM_LO_VS cmdbuf = PM4CmdSetData::SetShReg(cmdbuf, 0x4au, var, vs_regs[3]); // SPI_SHADER_PGM_RSRC1_VS cmdbuf = WritePacket(cmdbuf, PM4ShaderType::ShaderGraphics, 0xc01e0207u, diff --git a/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp b/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp index 61ed29d5..98f9d1c7 100644 --- a/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp +++ b/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp @@ -13,10 +13,12 @@ std::string_view StageName(Stage stage) { switch (stage) { case Stage::Vertex: return "vs"; - case Stage::TessellationControl: - return "tcs"; - case Stage::TessellationEval: - return "tes"; + case Stage::Local: + return "ls"; + case Stage::Export: + return "es"; + case Stage::Hull: + return "hs"; case Stage::Geometry: return "gs"; case Stage::Fragment: diff --git a/src/shader_recompiler/runtime_info.h b/src/shader_recompiler/runtime_info.h index ce3b64fc..67b96185 100644 --- a/src/shader_recompiler/runtime_info.h +++ b/src/shader_recompiler/runtime_info.h @@ -17,11 +17,12 @@ namespace Shader { static constexpr size_t NumUserDataRegs = 16; enum class Stage : u32 { - Vertex, - TessellationControl, - TessellationEval, - Geometry, Fragment, + Vertex, + Geometry, + Export, + Hull, + Local, Compute, }; constexpr u32 MaxStageTypes = 6; @@ -203,7 +204,7 @@ struct fmt::formatter { return ctx.begin(); } auto format(const Shader::Stage& stage, format_context& ctx) const { - constexpr static std::array names = {"vs", "tc", "te", "gs", "fs", "cs"}; + constexpr static std::array names = {"fs", "vs", "gs", "es", "hs", "ls", "cs"}; return fmt::format_to(ctx.out(), "{}", names[static_cast(stage)]); } }; diff --git a/src/video_core/amdgpu/liverpool.h b/src/video_core/amdgpu/liverpool.h index 536167ff..54b6c586 100644 --- a/src/video_core/amdgpu/liverpool.h +++ b/src/video_core/amdgpu/liverpool.h @@ -828,7 +828,15 @@ struct Liverpool { ShaderProgram ps_program; INSERT_PADDING_WORDS(0x2C); ShaderProgram vs_program; - INSERT_PADDING_WORDS(0x2E00 - 0x2C4C - 16); + INSERT_PADDING_WORDS(0x2C); + ShaderProgram gs_program; + INSERT_PADDING_WORDS(0x2C); + ShaderProgram es_program; + INSERT_PADDING_WORDS(0x2C); + ShaderProgram hs_program; + INSERT_PADDING_WORDS(0x2C); + ShaderProgram ls_program; + INSERT_PADDING_WORDS(0xA4); ComputeProgram cs_program; INSERT_PADDING_WORDS(0xA008 - 0x2E00 - 80 - 3 - 5); DepthRenderControl depth_render_control; @@ -907,12 +915,19 @@ struct Liverpool { const ShaderProgram* ProgramForStage(u32 index) const { switch (index) { case 0: - return &vs_program; - case 4: return &ps_program; - default: - return nullptr; + case 1: + return &vs_program; + case 2: + return &gs_program; + case 3: + return &es_program; + case 4: + return &hs_program; + case 5: + return &ls_program; } + return nullptr; } }; @@ -1017,6 +1032,10 @@ private: static_assert(GFX6_3D_REG_INDEX(ps_program) == 0x2C08); static_assert(GFX6_3D_REG_INDEX(vs_program) == 0x2C48); static_assert(GFX6_3D_REG_INDEX(vs_program.user_data) == 0x2C4C); +static_assert(GFX6_3D_REG_INDEX(gs_program) == 0x2C88); +static_assert(GFX6_3D_REG_INDEX(es_program) == 0x2CC8); +static_assert(GFX6_3D_REG_INDEX(hs_program) == 0x2D08); +static_assert(GFX6_3D_REG_INDEX(ls_program) == 0x2D48); static_assert(GFX6_3D_REG_INDEX(cs_program) == 0x2E00); static_assert(GFX6_3D_REG_INDEX(cs_program.dim_z) == 0x2E03); static_assert(GFX6_3D_REG_INDEX(cs_program.address_lo) == 0x2E0C); diff --git a/src/video_core/renderer_vulkan/vk_graphics_pipeline.cpp b/src/video_core/renderer_vulkan/vk_graphics_pipeline.cpp index 01901836..a3ba2f77 100644 --- a/src/video_core/renderer_vulkan/vk_graphics_pipeline.cpp +++ b/src/video_core/renderer_vulkan/vk_graphics_pipeline.cpp @@ -48,7 +48,7 @@ GraphicsPipeline::GraphicsPipeline(const Instance& instance_, Scheduler& schedul boost::container::static_vector bindings; boost::container::static_vector attributes; - const auto& vs_info = stages[0]; + const auto& vs_info = stages[u32(Shader::Stage::Vertex)]; for (const auto& input : vs_info.vs_inputs) { if (input.instance_step_rate == Shader::Info::VsInput::InstanceIdType::OverStepRate0 || input.instance_step_rate == Shader::Info::VsInput::InstanceIdType::OverStepRate1) { @@ -179,20 +179,21 @@ GraphicsPipeline::GraphicsPipeline(const Instance& instance_, Scheduler& schedul .maxDepthBounds = key.depth_bounds_max, }; - u32 shader_count = 1; + u32 shader_count{}; + auto stage = u32(Shader::Stage::Vertex); std::array shader_stages; - shader_stages[0] = vk::PipelineShaderStageCreateInfo{ + shader_stages[shader_count++] = vk::PipelineShaderStageCreateInfo{ .stage = vk::ShaderStageFlagBits::eVertex, - .module = modules[0], + .module = modules[stage], .pName = "main", }; - if (modules[4]) { - shader_stages[1] = vk::PipelineShaderStageCreateInfo{ + stage = u32(Shader::Stage::Fragment); + if (modules[stage]) { + shader_stages[shader_count++] = vk::PipelineShaderStageCreateInfo{ .stage = vk::ShaderStageFlagBits::eFragment, - .module = modules[4], + .module = modules[stage], .pName = "main", }; - ++shader_count; } const auto it = std::ranges::find(key.color_formats, vk::Format::eUndefined); @@ -411,7 +412,7 @@ void GraphicsPipeline::BindResources(Core::MemoryManager* memory, StreamBuffer& } void GraphicsPipeline::BindVertexBuffers(StreamBuffer& staging) const { - const auto& vs_info = stages[0]; + const auto& vs_info = stages[u32(Shader::Stage::Vertex)]; if (vs_info.vs_inputs.empty()) { return; } diff --git a/src/video_core/renderer_vulkan/vk_graphics_pipeline.h b/src/video_core/renderer_vulkan/vk_graphics_pipeline.h index ab8be78f..e1564f8f 100644 --- a/src/video_core/renderer_vulkan/vk_graphics_pipeline.h +++ b/src/video_core/renderer_vulkan/vk_graphics_pipeline.h @@ -77,7 +77,7 @@ public: bool IsEmbeddedVs() const noexcept { static constexpr size_t EmbeddedVsHash = 0x9b2da5cf47f8c29f; - return key.stage_hashes[0] == EmbeddedVsHash; + return key.stage_hashes[u32(Shader::Stage::Vertex)] == EmbeddedVsHash; } auto GetWriteMasks() const { diff --git a/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp b/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp index bf4bbc10..d601e525 100644 --- a/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp +++ b/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp @@ -255,6 +255,12 @@ std::unique_ptr PipelineCache::CreateGraphicsPipeline() { block_pool.ReleaseContents(); inst_pool.ReleaseContents(); + if (stage != Shader::Stage::Compute && stage != Shader::Stage::Fragment && + stage != Shader::Stage::Vertex) { + LOG_ERROR(Render_Vulkan, "Unsupported shader stage {}. PL creation skipped.", stage); + return {}; + } + // Recompile shader to IR. try { LOG_INFO(Render_Vulkan, "Compiling {} shader {:#x}", stage, hash); From dd7d232ac3e7257a22aaada5b52408911e050ec1 Mon Sep 17 00:00:00 2001 From: IndecisiveTurtle <47210458+raphaelthegreat@users.noreply.github.com> Date: Mon, 15 Jul 2024 01:28:08 +0300 Subject: [PATCH 05/34] event_queues: Use std placeholders --- src/core/libraries/kernel/event_queues.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/core/libraries/kernel/event_queues.cpp b/src/core/libraries/kernel/event_queues.cpp index 12f59e50..e42cc60d 100644 --- a/src/core/libraries/kernel/event_queues.cpp +++ b/src/core/libraries/kernel/event_queues.cpp @@ -7,8 +7,6 @@ #include "core/libraries/error_codes.h" #include "core/libraries/kernel/event_queues.h" -#include - namespace Libraries::Kernel { extern boost::asio::io_context io_context; @@ -137,7 +135,7 @@ s32 PS4_SYSV_ABI sceKernelAddHRTimerEvent(SceKernelEqueue eq, int id, timespec* io_context, std::chrono::microseconds(total_us - HrTimerSpinlockThresholdUs)); event.timer->async_wait( - std::bind(SmallTimerCallback, boost::asio::placeholders::error, eq, event.event)); + std::bind(SmallTimerCallback, std::placeholders::_1, eq, event.event)); if (!eq->AddEvent(event)) { return ORBIS_KERNEL_ERROR_ENOMEM; From 73d4aafc092e8237accacf4c1c612421c7895125 Mon Sep 17 00:00:00 2001 From: IndecisiveTurtle <47210458+raphaelthegreat@users.noreply.github.com> Date: Mon, 15 Jul 2024 01:28:36 +0300 Subject: [PATCH 06/34] liverpool: Num submits should be atomic --- src/video_core/amdgpu/liverpool.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/video_core/amdgpu/liverpool.h b/src/video_core/amdgpu/liverpool.h index 0711b074..4245a995 100644 --- a/src/video_core/amdgpu/liverpool.h +++ b/src/video_core/amdgpu/liverpool.h @@ -1018,7 +1018,7 @@ private: Vulkan::Rasterizer* rasterizer{}; std::jthread process_thread{}; - u32 num_submits{}; + std::atomic num_submits{}; std::mutex submit_mutex; std::condition_variable_any submit_cv; }; From 432defd9929b77fd890b9205b06278768e5cd049 Mon Sep 17 00:00:00 2001 From: IndecisiveTurtle <47210458+raphaelthegreat@users.noreply.github.com> Date: Mon, 15 Jul 2024 01:29:16 +0300 Subject: [PATCH 07/34] externals: Disable sdl pipewire * Fixes audio problems in certain games --- externals/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/externals/CMakeLists.txt b/externals/CMakeLists.txt index a86c393f..08353fb3 100644 --- a/externals/CMakeLists.txt +++ b/externals/CMakeLists.txt @@ -59,6 +59,7 @@ endif() # SDL3 if (NOT TARGET SDL3::SDL3) + set(SDL_PIPEWIRE OFF) add_subdirectory(sdl3) endif() From 26f8fbf62865600fcfedc5ce070a7bfb47dcf092 Mon Sep 17 00:00:00 2001 From: IndecisiveTurtle <47210458+raphaelthegreat@users.noreply.github.com> Date: Mon, 15 Jul 2024 01:48:57 +0300 Subject: [PATCH 08/34] tls: Use gs segment for linux --- src/core/tls.cpp | 38 ++++++++++---------------------------- 1 file changed, 10 insertions(+), 28 deletions(-) diff --git a/src/core/tls.cpp b/src/core/tls.cpp index 3f7bbceb..2afa5896 100644 --- a/src/core/tls.cpp +++ b/src/core/tls.cpp @@ -9,7 +9,8 @@ #ifdef _WIN32 #include #else -#include +#include /* Definition of ARCH_* constants */ +#include /* Definition of SYS_* constants */ #endif namespace Core { @@ -89,47 +90,28 @@ static void PatchFsAccess(u8* code, const TLSPattern& tls_pattern, Xbyak::CodeGe #else -static pthread_key_t slot = 0; +static u32 slot = 0; void SetTcbBase(void* image_address) { - ASSERT(pthread_setspecific(slot, image_address) == 0); + asm volatile("wrgsbase %0" :: "r" (image_address) : "memory"); } Tcb* GetTcbBase() { - return reinterpret_cast(pthread_getspecific(slot)); + Tcb* tcb; + asm volatile("rdgsbase %0" : "=r" (tcb) :: "memory"); + return tcb; } -static void AllocTcbKey() { - ASSERT(pthread_key_create(&slot, nullptr) == 0); -} +static void AllocTcbKey() {} static void PatchFsAccess(u8* code, const TLSPattern& tls_pattern, Xbyak::CodeGenerator& c) { using namespace Xbyak::util; const auto total_size = tls_pattern.pattern_size + tls_pattern.imm_size; - // Replace mov instruction with near jump to the trampoline. - static constexpr u32 NearJmpSize = 5; + // Replace fs read with gs read. auto patch = Xbyak::CodeGenerator(total_size, code); - patch.jmp(c.getCurr(), Xbyak::CodeGenerator::LabelType::T_NEAR); - patch.nop(total_size - NearJmpSize); - - // Write the trampoline. - // The following logic is based on the glibc implementation of pthread_getspecific - // https://github.com/bminor/glibc/blob/29807a27/nptl/pthread_getspecific.c#L23 - static constexpr u32 PthreadKeySecondLevelSize = 32; - static constexpr u32 PthreadSpecificOffset = 0x510; - static constexpr u32 PthreadKeyDataSize = 16; - ASSERT(slot >= PthreadKeySecondLevelSize); - - const u32 idx1st = slot / PthreadKeySecondLevelSize; - const u32 idx2nd = slot % PthreadKeySecondLevelSize; const auto target_reg = Xbyak::Reg64(tls_pattern.target_reg); - c.mov(target_reg, PthreadSpecificOffset); - c.putSeg(fs); - c.mov(target_reg, qword[target_reg + idx1st * 8]); // Load first level specific array. - c.mov(target_reg, qword[target_reg + idx2nd * 16 + - 8]); // Load data member of pthread_key_data our slot specifies. - c.jmp(code + total_size); // Return to the instruction right after the mov. + patch.putSeg(gs); } #endif From dbeed80e3b6248703c20aa14fe39725f6ca1d404 Mon Sep 17 00:00:00 2001 From: IndecisiveTurtle <47210458+raphaelthegreat@users.noreply.github.com> Date: Mon, 15 Jul 2024 03:37:30 +0300 Subject: [PATCH 09/34] fs: Actually functional linux case insensitive search --- src/common/io_file.h | 4 +- src/core/file_sys/fs.cpp | 111 +++++++++++++--------- src/core/file_sys/fs.h | 33 +++++-- src/core/libraries/kernel/file_system.cpp | 13 ++- src/core/libraries/kernel/libkernel.cpp | 2 +- src/core/libraries/libc/libc_stdio.cpp | 9 +- src/core/libraries/save_data/savedata.cpp | 81 ++++++++-------- 7 files changed, 145 insertions(+), 108 deletions(-) diff --git a/src/common/io_file.h b/src/common/io_file.h index 7cb3246c..2c3df3f6 100644 --- a/src/common/io_file.h +++ b/src/common/io_file.h @@ -205,9 +205,9 @@ public: return WriteSpan(string); } - static void WriteBytes(const std::filesystem::path path, std::span vec) { + static void WriteBytes(const std::filesystem::path path, std::span data) { IOFile out(path, FileAccessMode::Write); - out.Write(vec); + out.Write(data); } private: diff --git a/src/core/file_sys/fs.cpp b/src/core/file_sys/fs.cpp index e06a4b6e..f009dc2d 100644 --- a/src/core/file_sys/fs.cpp +++ b/src/core/file_sys/fs.cpp @@ -11,18 +11,12 @@ constexpr int RESERVED_HANDLES = 3; // First 3 handles are stdin,stdout,stderr void MntPoints::Mount(const std::filesystem::path& host_folder, const std::string& guest_folder) { std::scoped_lock lock{m_mutex}; - - MntPair pair; - pair.host_path = host_folder.string(); - std::replace(pair.host_path.begin(), pair.host_path.end(), '\\', '/'); - pair.guest_path = guest_folder; - - m_mnt_pairs.push_back(pair); + m_mnt_pairs.emplace_back(host_folder, guest_folder); } void MntPoints::Unmount(const std::filesystem::path& host_folder, const std::string& guest_folder) { auto it = std::remove_if(m_mnt_pairs.begin(), m_mnt_pairs.end(), - [&](const MntPair& pair) { return pair.guest_path == guest_folder; }); + [&](const MntPair& pair) { return pair.mount == guest_folder; }); m_mnt_pairs.erase(it, m_mnt_pairs.end()); } @@ -31,47 +25,74 @@ void MntPoints::UnmountAll() { m_mnt_pairs.clear(); } -std::string MntPoints::GetHostDirectory(const std::string& guest_directory) { - std::scoped_lock lock{m_mutex}; - for (auto& pair : m_mnt_pairs) { - // horrible code but it works :D - int find = guest_directory.find(pair.guest_path); - if (find == 0) { - std::string npath = - guest_directory.substr(pair.guest_path.size(), guest_directory.size() - 1); - std::replace(pair.host_path.begin(), pair.host_path.end(), '\\', '/'); - return pair.host_path + npath; - } +std::filesystem::path MntPoints::GetHostPath(const std::string& guest_directory) { + const MntPair* mount = GetMount(guest_directory); + if (!mount) { + return guest_directory; } - return ""; -} -std::string MntPoints::GetHostFile(const std::string& guest_file) { - std::scoped_lock lock{m_mutex}; - - for (auto& pair : m_mnt_pairs) { - // horrible code but it works :D - int find = guest_file.find(pair.guest_path); - if (find != 0) { - continue; - } - std::string npath = guest_file.substr(pair.guest_path.size(), guest_file.size() - 1); - const auto host_path = pair.host_path + npath; -#ifndef _WIN64 - const std::filesystem::path path{host_path}; - if (!std::filesystem::exists(path)) { - const auto filename = Common::ToLower(path.filename()); - for (const auto& file : std::filesystem::directory_iterator(path.parent_path())) { - const auto exist_filename = Common::ToLower(file.path().filename()); - if (filename == exist_filename) { - return file.path(); - } - } - } -#endif + // Remove device (e.g /app0) from path to retrieve relative path. + const u32 pos = mount->mount.size() + 1; + const auto rel_path = std::string_view(guest_directory).substr(pos); + const auto host_path = mount->host_path / rel_path; + if (!NeedsCaseInsensiveSearch || std::filesystem::exists(host_path)) { return host_path; } - return ""; + + // If the path does not exist attempt to verify this. + // Retrieve parent path until we find one that exists. + path_parts.clear(); + auto current_path = host_path; + while (!std::filesystem::exists(current_path)) { + // We have probably cached this if it's a folder. + if (auto it = path_cache.find(current_path); it != path_cache.end()) { + current_path = it->second; + break; + } + path_parts.emplace_back(current_path.filename()); + current_path = current_path.parent_path(); + } + + // We have found an anchor. Traverse parts we recoded and see if they + // exist in filesystem but in different case. + auto guest_path = current_path; + while (!path_parts.empty()) { + const auto& part = path_parts.back(); + const auto add_match = [&](const auto& host_part) { + current_path += host_part; + guest_path += part; + path_cache[guest_path] = current_path; + path_parts.pop_back(); + }; + + // Can happen when the mismatch is in upper folder. + if (std::filesystem::exists(current_path / part)) { + add_match(part); + continue; + } + const auto part_low = Common::ToLower(part.string()); + bool found_match = false; + for (const auto& path : std::filesystem::directory_iterator(current_path)) { + const auto candidate = path.path().filename(); + const auto filename = Common::ToLower(candidate); + // Check if a filename matches in case insensitive manner. + if (filename != part_low) { + continue; + } + // We found a match, record the actual path in the cache. + add_match(candidate); + found_match = true; + break; + } + if (!found_match) { + // Opening the guest path will surely fail but at least gives + // a better error message than the empty path. + return guest_directory; + } + } + + // The path was found. + return current_path; } int HandleTable::CreateHandle() { diff --git a/src/core/file_sys/fs.h b/src/core/file_sys/fs.h index b31931d1..10fd10c4 100644 --- a/src/core/file_sys/fs.h +++ b/src/core/file_sys/fs.h @@ -7,28 +7,45 @@ #include #include #include +#include #include "common/io_file.h" namespace Core::FileSys { class MntPoints { +#ifdef _WIN64 + static constexpr bool NeedsCaseInsensiveSearch = false; +#else + static constexpr bool NeedsCaseInsensiveSearch = true; +#endif public: struct MntPair { - std::string host_path; - std::string guest_path; // e.g /app0/ + std::filesystem::path host_path; + std::string mount; // e.g /app0/ }; - MntPoints() = default; - virtual ~MntPoints() = default; + explicit MntPoints() = default; + ~MntPoints() = default; - void Mount(const std::filesystem::path& host_folder, const std::string& guest_folder); - void Unmount(const std::filesystem::path& host_folder, const std::string& guest_folder); + void Mount(const std::filesystem::path& host_folder, + const std::string& guest_folder); + void Unmount(const std::filesystem::path& host_folder, + const std::string& guest_folder); void UnmountAll(); - std::string GetHostDirectory(const std::string& guest_directory); - std::string GetHostFile(const std::string& guest_file); + + std::filesystem::path GetHostPath(const std::string& guest_directory); + + const MntPair* GetMount(const std::string& guest_path) { + const auto it = std::ranges::find_if(m_mnt_pairs, [&](const auto& mount) { + return guest_path.starts_with(mount.mount); + }); + return it == m_mnt_pairs.end() ? nullptr : &*it; + } private: std::vector m_mnt_pairs; + std::vector path_parts; + tsl::robin_map path_cache; std::mutex m_mutex; }; diff --git a/src/core/libraries/kernel/file_system.cpp b/src/core/libraries/kernel/file_system.cpp index eb6248d0..be173320 100644 --- a/src/core/libraries/kernel/file_system.cpp +++ b/src/core/libraries/kernel/file_system.cpp @@ -58,7 +58,7 @@ int PS4_SYSV_ABI sceKernelOpen(const char* path, int flags, u16 mode) { if (directory) { file->is_directory = true; file->m_guest_name = path; - file->m_host_name = mnt->GetHostDirectory(file->m_guest_name); + file->m_host_name = mnt->GetHostPath(file->m_guest_name); if (!std::filesystem::is_directory(file->m_host_name)) { // directory doesn't exist h->DeleteHandle(handle); return ORBIS_KERNEL_ERROR_ENOTDIR; @@ -72,7 +72,7 @@ int PS4_SYSV_ABI sceKernelOpen(const char* path, int flags, u16 mode) { } } else { file->m_guest_name = path; - file->m_host_name = mnt->GetHostFile(file->m_guest_name); + file->m_host_name = mnt->GetHostPath(file->m_guest_name); int e = 0; if (read) { e = file->f.Open(file->m_host_name, Common::FS::FileAccessMode::Read); @@ -165,8 +165,7 @@ int PS4_SYSV_ABI sceKernelUnlink(const char* path) { auto* h = Common::Singleton::Instance(); auto* mnt = Common::Singleton::Instance(); - std::string host_path = mnt->GetHostFile(path); - + const auto host_path = mnt->GetHostPath(path); if (host_path.empty()) { return SCE_KERNEL_ERROR_EACCES; } @@ -250,7 +249,7 @@ int PS4_SYSV_ABI sceKernelMkdir(const char* path, u16 mode) { return SCE_KERNEL_ERROR_EINVAL; } auto* mnt = Common::Singleton::Instance(); - std::string dir_name = mnt->GetHostFile(path); + const auto dir_name = mnt->GetHostPath(path); if (std::filesystem::exists(dir_name)) { return SCE_KERNEL_ERROR_EEXIST; } @@ -279,7 +278,7 @@ int PS4_SYSV_ABI posix_mkdir(const char* path, u16 mode) { int PS4_SYSV_ABI sceKernelStat(const char* path, OrbisKernelStat* sb) { LOG_INFO(Kernel_Fs, "(PARTIAL) path = {}", path); auto* mnt = Common::Singleton::Instance(); - const auto& path_name = mnt->GetHostFile(path); + const auto path_name = mnt->GetHostPath(path); std::memset(sb, 0, sizeof(OrbisKernelStat)); const bool is_dir = std::filesystem::is_directory(path_name); const bool is_file = std::filesystem::is_regular_file(path_name); @@ -314,7 +313,7 @@ int PS4_SYSV_ABI posix_stat(const char* path, OrbisKernelStat* sb) { int PS4_SYSV_ABI sceKernelCheckReachability(const char* path) { auto* mnt = Common::Singleton::Instance(); - std::string path_name = mnt->GetHostFile(path); + const auto path_name = mnt->GetHostPath(path); if (!std::filesystem::exists(path_name)) { return SCE_KERNEL_ERROR_ENOENT; } diff --git a/src/core/libraries/kernel/libkernel.cpp b/src/core/libraries/kernel/libkernel.cpp index 9f57ff53..c9073029 100644 --- a/src/core/libraries/kernel/libkernel.cpp +++ b/src/core/libraries/kernel/libkernel.cpp @@ -222,7 +222,7 @@ s32 PS4_SYSV_ABI sceKernelLoadStartModule(const char* moduleFileName, size_t arg } auto* mnt = Common::Singleton::Instance(); - const auto path = mnt->GetHostFile(moduleFileName); + const auto path = mnt->GetHostPath(moduleFileName); // Load PRX module and relocate any modules that import it. auto* linker = Common::Singleton::Instance(); diff --git a/src/core/libraries/libc/libc_stdio.cpp b/src/core/libraries/libc/libc_stdio.cpp index b1e94676..72337b83 100644 --- a/src/core/libraries/libc/libc_stdio.cpp +++ b/src/core/libraries/libc/libc_stdio.cpp @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later -#include "common/assert.h" +#include "common/logging/log.h" #include "common/singleton.h" #include "core/file_sys/fs.h" #include "core/libraries/libc/libc_stdio.h" @@ -10,11 +10,12 @@ namespace Libraries::LibC { std::FILE* PS4_SYSV_ABI ps4_fopen(const char* filename, const char* mode) { auto* mnt = Common::Singleton::Instance(); - FILE* f = std::fopen(mnt->GetHostFile(filename).c_str(), mode); + const auto host_path = mnt->GetHostPath(filename); + FILE* f = std::fopen(host_path.c_str(), mode); if (f != nullptr) { - LOG_INFO(Lib_LibC, "fopen = {}", mnt->GetHostFile(filename).c_str()); + LOG_INFO(Lib_LibC, "fopen = {}", host_path.native()); } else { - LOG_INFO(Lib_LibC, "fopen can't open = {}", mnt->GetHostFile(filename).c_str()); + LOG_INFO(Lib_LibC, "fopen can't open = {}", host_path.native()); } return f; } diff --git a/src/core/libraries/save_data/savedata.cpp b/src/core/libraries/save_data/savedata.cpp index 7d12ed81..2a2855cc 100644 --- a/src/core/libraries/save_data/savedata.cpp +++ b/src/core/libraries/save_data/savedata.cpp @@ -15,7 +15,8 @@ #include "error_codes.h" namespace Libraries::SaveData { -static std::string g_mount_point = "/savedata0"; // temp mount point (todo) + +static constexpr std::string_view g_mount_point = "/savedata0"; // temp mount point (todo) std::string game_serial; int PS4_SYSV_ABI sceSaveDataAbort() { @@ -50,11 +51,11 @@ int PS4_SYSV_ABI sceSaveDataChangeInternal() { int PS4_SYSV_ABI sceSaveDataCheckBackupData(const OrbisSaveDataCheckBackupData* check) { auto* mnt = Common::Singleton::Instance(); - std::string mount_dir = mnt->GetHostFile(check->dirName->data); + const auto mount_dir = mnt->GetHostPath(check->dirName->data); if (!std::filesystem::exists(mount_dir)) { return ORBIS_SAVE_DATA_ERROR_NOT_FOUND; } - LOG_INFO(Lib_SaveData, "called = {}", mount_dir); + LOG_INFO(Lib_SaveData, "called = {}", mount_dir.native()); return ORBIS_OK; } @@ -317,14 +318,14 @@ int PS4_SYSV_ABI sceSaveDataGetSaveDataMemory2(OrbisSaveDataMemoryGet2* getParam return false; } file.Seek(getParam->data->offset); - size_t nbytes = file.ReadRaw(getParam->data->buf, getParam->data->bufSize); + file.ReadRaw(getParam->data->buf, getParam->data->bufSize); LOG_INFO(Lib_SaveData, "called: bufSize = {}, offset = {}", getParam->data->bufSize, getParam->data->offset); } if (getParam->param != nullptr) { - Common::FS::IOFile file1(mount_dir / "param.txt", Common::FS::FileAccessMode::Read); - size_t nbytes = file1.ReadRaw(getParam->param, sizeof(OrbisSaveDataParam)); + Common::FS::IOFile file(mount_dir / "param.txt", Common::FS::FileAccessMode::Read); + file.ReadRaw(getParam->param, sizeof(OrbisSaveDataParam)); } return ORBIS_OK; @@ -394,13 +395,13 @@ int PS4_SYSV_ABI sceSaveDataIsMounted() { int PS4_SYSV_ABI sceSaveDataLoadIcon(const OrbisSaveDataMountPoint* mountPoint, OrbisSaveDataIcon* icon) { auto* mnt = Common::Singleton::Instance(); - std::string mount_dir = mnt->GetHostFile(mountPoint->data); - LOG_INFO(Lib_SaveData, "called: dir = {}", mount_dir); + const auto mount_dir = mnt->GetHostPath(mountPoint->data); + LOG_INFO(Lib_SaveData, "called: dir = {}", mount_dir.native()); if (icon != nullptr) { - Common::FS::IOFile file(mount_dir + "/save_data.png", Common::FS::FileAccessMode::Read); + Common::FS::IOFile file(mount_dir / "save_data.png", Common::FS::FileAccessMode::Read); icon->bufSize = file.GetSize(); - size_t nbytes = file.ReadRaw(icon->buf, icon->bufSize); + file.ReadRaw(icon->buf, icon->bufSize); } return ORBIS_OK; } @@ -409,6 +410,7 @@ s32 saveDataMount(u32 user_id, char* dir_name, u32 mount_mode, OrbisSaveDataMountResult* mount_result) { const auto& mount_dir = Common::FS::GetUserPath(Common::FS::PathType::SaveDataDir) / std::to_string(user_id) / game_serial / dir_name; + auto* mnt = Common::Singleton::Instance(); switch (mount_mode) { case ORBIS_SAVE_DATA_MOUNT_MODE_RDONLY: case ORBIS_SAVE_DATA_MOUNT_MODE_RDWR: @@ -417,9 +419,8 @@ s32 saveDataMount(u32 user_id, char* dir_name, u32 mount_mode, if (!std::filesystem::exists(mount_dir)) { return ORBIS_SAVE_DATA_ERROR_NOT_FOUND; } - auto* mnt = Common::Singleton::Instance(); mount_result->mount_status = 0; - std::strncpy(mount_result->mount_point.data, g_mount_point.c_str(), 16); + g_mount_point.copy(mount_result->mount_point.data, 16); mnt->Mount(mount_dir, mount_result->mount_point.data); } break; case ORBIS_SAVE_DATA_MOUNT_MODE_CREATE: @@ -431,16 +432,15 @@ s32 saveDataMount(u32 user_id, char* dir_name, u32 mount_mode, ORBIS_SAVE_DATA_MOUNT_MODE_COPY_ICON: case ORBIS_SAVE_DATA_MOUNT_MODE_CREATE | ORBIS_SAVE_DATA_MOUNT_MODE_DESTRUCT_OFF | ORBIS_SAVE_DATA_MOUNT_MODE_COPY_ICON: { - auto* mnt = Common::Singleton::Instance(); if (std::filesystem::exists(mount_dir)) { - std::strncpy(mount_result->mount_point.data, g_mount_point.c_str(), 16); + g_mount_point.copy(mount_result->mount_point.data, 16); mnt->Mount(mount_dir, mount_result->mount_point.data); mount_result->required_blocks = 0; mount_result->mount_status = 0; return ORBIS_SAVE_DATA_ERROR_EXISTS; } if (std::filesystem::create_directories(mount_dir)) { - std::strncpy(mount_result->mount_point.data, g_mount_point.c_str(), 16); + g_mount_point.copy(mount_result->mount_point.data, 16); mnt->Mount(mount_dir, mount_result->mount_point.data); mount_result->mount_status = 1; } @@ -451,8 +451,7 @@ s32 saveDataMount(u32 user_id, char* dir_name, u32 mount_mode, if (!std::filesystem::exists(mount_dir)) { std::filesystem::create_directories(mount_dir); } - auto* mnt = Common::Singleton::Instance(); - std::strncpy(mount_result->mount_point.data, g_mount_point.c_str(), 16); + g_mount_point.copy(mount_result->mount_point.data, 16); mnt->Mount(mount_dir, mount_result->mount_point.data); mount_result->mount_status = 1; } break; @@ -534,12 +533,12 @@ int PS4_SYSV_ABI sceSaveDataRestoreLoadSaveDataMemory() { int PS4_SYSV_ABI sceSaveDataSaveIcon(const OrbisSaveDataMountPoint* mountPoint, const OrbisSaveDataIcon* icon) { auto* mnt = Common::Singleton::Instance(); - std::string mount_dir = mnt->GetHostFile(mountPoint->data); - LOG_INFO(Lib_SaveData, "called = {}", mount_dir); + const auto mount_dir = mnt->GetHostPath(mountPoint->data); + LOG_INFO(Lib_SaveData, "called = {}", mount_dir.native()); - if (icon != nullptr) { - Common::FS::IOFile file(mount_dir + "/save_data.png", Common::FS::FileAccessMode::Write); - file.WriteRaw((void*)icon->buf, icon->bufSize); + if (icon != nullptr) { + Common::FS::IOFile file(mount_dir / "save_data.png", Common::FS::FileAccessMode::Write); + file.WriteRaw(icon->buf, icon->bufSize); } return ORBIS_OK; } @@ -558,12 +557,13 @@ int PS4_SYSV_ABI sceSaveDataSetParam(const OrbisSaveDataMountPoint* mountPoint, OrbisSaveDataParamType paramType, const void* paramBuf, size_t paramBufSize) { auto* mnt = Common::Singleton::Instance(); - std::string mount_dir = mnt->GetHostFile(mountPoint->data); - LOG_INFO(Lib_SaveData, "called = {}, mountPoint->data = {}", mount_dir, mountPoint->data); + const auto mount_dir = mnt->GetHostPath(mountPoint->data); + LOG_INFO(Lib_SaveData, "called = {}, mountPoint->data = {}", mount_dir.native(), + mountPoint->data); if (paramBuf != nullptr) { - Common::FS::IOFile file(mount_dir + "/param.txt", Common::FS::FileAccessMode::Write); - file.WriteRaw((void*)paramBuf, paramBufSize); + Common::FS::IOFile file(mount_dir / "param.txt", Common::FS::FileAccessMode::Write); + file.WriteRaw(paramBuf, paramBufSize); } return ORBIS_OK; } @@ -711,24 +711,23 @@ int PS4_SYSV_ABI sceSaveDataUmountSys() { int PS4_SYSV_ABI sceSaveDataUmountWithBackup(const OrbisSaveDataMountPoint* mountPoint) { LOG_ERROR(Lib_SaveData, "called = {}", std::string(mountPoint->data)); auto* mnt = Common::Singleton::Instance(); - std::string mount_dir = mnt->GetHostFile(mountPoint->data); + const auto mount_dir = mnt->GetHostPath(mountPoint->data); if (!std::filesystem::exists(mount_dir)) { return ORBIS_SAVE_DATA_ERROR_NOT_FOUND; - } else { - std::filesystem::path mnt_dir(mount_dir); - std::filesystem::create_directories(mnt_dir.parent_path() / "backup"); - - for (const auto& entry : std::filesystem::recursive_directory_iterator(mnt_dir)) { - const auto& path = entry.path(); - std::filesystem::path target_path = mnt_dir.parent_path() / "backup"; - - if (std::filesystem::is_regular_file(path)) { - std::filesystem::copy(path, target_path, - std::filesystem::copy_options::overwrite_existing); - } - } - mnt->Unmount(mount_dir, mountPoint->data); } + + std::filesystem::create_directories(mount_dir.parent_path() / "backup"); + + for (const auto& entry : std::filesystem::recursive_directory_iterator(mount_dir)) { + const auto& path = entry.path(); + const auto target_path = mount_dir.parent_path() / "backup"; + if (std::filesystem::is_regular_file(path)) { + std::filesystem::copy(path, target_path, + std::filesystem::copy_options::overwrite_existing); + } + } + + mnt->Unmount(mount_dir, mountPoint->data); return ORBIS_OK; } From ce6a01bd418913f41d87b3c6a7edf76239aad656 Mon Sep 17 00:00:00 2001 From: IndecisiveTurtle <47210458+raphaelthegreat@users.noreply.github.com> Date: Mon, 15 Jul 2024 03:47:10 +0300 Subject: [PATCH 10/34] clang format --- src/core/file_sys/fs.h | 11 ++++------- src/core/libraries/kernel/event_flag/event_flag_obj.h | 2 +- src/core/libraries/kernel/event_queues.cpp | 3 +-- src/core/libraries/save_data/savedata.cpp | 2 +- src/core/tls.cpp | 8 ++++---- src/shader_recompiler/ir/breadth_first_search.h | 4 ++-- src/shader_recompiler/ir/opcodes.h | 2 +- 7 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/core/file_sys/fs.h b/src/core/file_sys/fs.h index 10fd10c4..ede386e5 100644 --- a/src/core/file_sys/fs.h +++ b/src/core/file_sys/fs.h @@ -27,18 +27,15 @@ public: explicit MntPoints() = default; ~MntPoints() = default; - void Mount(const std::filesystem::path& host_folder, - const std::string& guest_folder); - void Unmount(const std::filesystem::path& host_folder, - const std::string& guest_folder); + void Mount(const std::filesystem::path& host_folder, const std::string& guest_folder); + void Unmount(const std::filesystem::path& host_folder, const std::string& guest_folder); void UnmountAll(); std::filesystem::path GetHostPath(const std::string& guest_directory); const MntPair* GetMount(const std::string& guest_path) { - const auto it = std::ranges::find_if(m_mnt_pairs, [&](const auto& mount) { - return guest_path.starts_with(mount.mount); - }); + const auto it = std::ranges::find_if( + m_mnt_pairs, [&](const auto& mount) { return guest_path.starts_with(mount.mount); }); return it == m_mnt_pairs.end() ? nullptr : &*it; } diff --git a/src/core/libraries/kernel/event_flag/event_flag_obj.h b/src/core/libraries/kernel/event_flag/event_flag_obj.h index fe50be69..99ae1981 100644 --- a/src/core/libraries/kernel/event_flag/event_flag_obj.h +++ b/src/core/libraries/kernel/event_flag/event_flag_obj.h @@ -20,7 +20,7 @@ public: EventFlagInternal(const std::string& name, ThreadMode thread_mode, QueueMode queue_mode, uint64_t bits) - : m_name(name), m_thread_mode(thread_mode), m_queue_mode(queue_mode), m_bits(bits){}; + : m_name(name), m_thread_mode(thread_mode), m_queue_mode(queue_mode), m_bits(bits) {}; int Wait(u64 bits, WaitMode wait_mode, ClearMode clear_mode, u64* result, u32* ptr_micros); int Poll(u64 bits, WaitMode wait_mode, ClearMode clear_mode, u64* result); diff --git a/src/core/libraries/kernel/event_queues.cpp b/src/core/libraries/kernel/event_queues.cpp index e42cc60d..bb3d8ba7 100644 --- a/src/core/libraries/kernel/event_queues.cpp +++ b/src/core/libraries/kernel/event_queues.cpp @@ -134,8 +134,7 @@ s32 PS4_SYSV_ABI sceKernelAddHRTimerEvent(SceKernelEqueue eq, int id, timespec* event.timer = std::make_unique( io_context, std::chrono::microseconds(total_us - HrTimerSpinlockThresholdUs)); - event.timer->async_wait( - std::bind(SmallTimerCallback, std::placeholders::_1, eq, event.event)); + event.timer->async_wait(std::bind(SmallTimerCallback, std::placeholders::_1, eq, event.event)); if (!eq->AddEvent(event)) { return ORBIS_KERNEL_ERROR_ENOMEM; diff --git a/src/core/libraries/save_data/savedata.cpp b/src/core/libraries/save_data/savedata.cpp index 2a2855cc..9f3564e8 100644 --- a/src/core/libraries/save_data/savedata.cpp +++ b/src/core/libraries/save_data/savedata.cpp @@ -536,7 +536,7 @@ int PS4_SYSV_ABI sceSaveDataSaveIcon(const OrbisSaveDataMountPoint* mountPoint, const auto mount_dir = mnt->GetHostPath(mountPoint->data); LOG_INFO(Lib_SaveData, "called = {}", mount_dir.native()); - if (icon != nullptr) { + if (icon != nullptr) { Common::FS::IOFile file(mount_dir / "save_data.png", Common::FS::FileAccessMode::Write); file.WriteRaw(icon->buf, icon->bufSize); } diff --git a/src/core/tls.cpp b/src/core/tls.cpp index 2afa5896..0c2d973b 100644 --- a/src/core/tls.cpp +++ b/src/core/tls.cpp @@ -9,8 +9,8 @@ #ifdef _WIN32 #include #else -#include /* Definition of ARCH_* constants */ -#include /* Definition of SYS_* constants */ +#include /* Definition of ARCH_* constants */ +#include /* Definition of SYS_* constants */ #endif namespace Core { @@ -93,12 +93,12 @@ static void PatchFsAccess(u8* code, const TLSPattern& tls_pattern, Xbyak::CodeGe static u32 slot = 0; void SetTcbBase(void* image_address) { - asm volatile("wrgsbase %0" :: "r" (image_address) : "memory"); + asm volatile("wrgsbase %0" ::"r"(image_address) : "memory"); } Tcb* GetTcbBase() { Tcb* tcb; - asm volatile("rdgsbase %0" : "=r" (tcb) :: "memory"); + asm volatile("rdgsbase %0" : "=r"(tcb)::"memory"); return tcb; } diff --git a/src/shader_recompiler/ir/breadth_first_search.h b/src/shader_recompiler/ir/breadth_first_search.h index 21a34a90..be46b48c 100644 --- a/src/shader_recompiler/ir/breadth_first_search.h +++ b/src/shader_recompiler/ir/breadth_first_search.h @@ -12,8 +12,8 @@ namespace Shader::IR { template -auto BreadthFirstSearch(const Value& value, Pred&& pred) - -> std::invoke_result_t { +auto BreadthFirstSearch(const Value& value, + Pred&& pred) -> std::invoke_result_t { if (value.IsImmediate()) { // Nothing to do with immediates return std::nullopt; diff --git a/src/shader_recompiler/ir/opcodes.h b/src/shader_recompiler/ir/opcodes.h index 66b60221..22cc8c2b 100644 --- a/src/shader_recompiler/ir/opcodes.h +++ b/src/shader_recompiler/ir/opcodes.h @@ -52,7 +52,7 @@ constexpr Type F64x2{Type::F64x2}; constexpr Type F64x3{Type::F64x3}; constexpr Type F64x4{Type::F64x4}; -constexpr OpcodeMeta META_TABLE[]{ +constexpr OpcodeMeta META_TABLE[] { #define OPCODE(name_token, type_token, ...) \ { \ .name{#name_token}, \ From 6ec4a3d1b93b39d6b2cac9a11bf84a5c5e7be970 Mon Sep 17 00:00:00 2001 From: jas0n098 Date: Mon, 15 Jul 2024 00:55:58 +0000 Subject: [PATCH 11/34] Replace deprecated pthread functions (#290) pthread_attr_(set,get)stackaddr are deprecated and don't exist anymore on musl libc --- src/core/libraries/kernel/thread_management.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/core/libraries/kernel/thread_management.cpp b/src/core/libraries/kernel/thread_management.cpp index b31e777d..0838a4d5 100644 --- a/src/core/libraries/kernel/thread_management.cpp +++ b/src/core/libraries/kernel/thread_management.cpp @@ -318,7 +318,8 @@ int PS4_SYSV_ABI scePthreadAttrGetstackaddr(const ScePthreadAttr* attr, void** s return SCE_KERNEL_ERROR_EINVAL; } - int result = pthread_attr_getstackaddr(&(*attr)->pth_attr, stack_addr); + size_t stack_size = 0; + int result = pthread_attr_getstack(&(*attr)->pth_attr, stack_addr, &stack_size); return result == 0 ? SCE_OK : SCE_KERNEL_ERROR_EINVAL; } @@ -340,7 +341,10 @@ int PS4_SYSV_ABI scePthreadAttrSetstackaddr(ScePthreadAttr* attr, void* addr) { return SCE_KERNEL_ERROR_EINVAL; } - int result = pthread_attr_setstackaddr(&(*attr)->pth_attr, addr); + size_t stack_size = 0; + pthread_attr_getstacksize(&(*attr)->pth_attr, &stack_size); + + int result = pthread_attr_setstack(&(*attr)->pth_attr, addr, stack_size); return result == 0 ? SCE_OK : SCE_KERNEL_ERROR_EINVAL; } From 58d1cbd9b7b1c919e498657959b551ee8e13bddd Mon Sep 17 00:00:00 2001 From: IndecisiveTurtle <47210458+raphaelthegreat@users.noreply.github.com> Date: Mon, 15 Jul 2024 13:33:57 +0300 Subject: [PATCH 12/34] ssa_rewrite_pass: Correct phi node type for thread bitmask --- src/shader_recompiler/frontend/translate/export.cpp | 2 +- src/shader_recompiler/ir/passes/ssa_rewrite_pass.cpp | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/shader_recompiler/frontend/translate/export.cpp b/src/shader_recompiler/frontend/translate/export.cpp index cc631ff2..51840537 100644 --- a/src/shader_recompiler/frontend/translate/export.cpp +++ b/src/shader_recompiler/frontend/translate/export.cpp @@ -7,7 +7,7 @@ namespace Shader::Gcn { void Translator::EXP(const GcnInst& inst) { - if (ir.block->has_multiple_predecessors) { + if (ir.block->has_multiple_predecessors && info.stage == Stage::Fragment) { LOG_WARNING(Render_Recompiler, "An ambiguous export appeared in translation"); ir.Discard(ir.LogicalNot(ir.GetExec())); } diff --git a/src/shader_recompiler/ir/passes/ssa_rewrite_pass.cpp b/src/shader_recompiler/ir/passes/ssa_rewrite_pass.cpp index 8a24a68b..4699f09a 100644 --- a/src/shader_recompiler/ir/passes/ssa_rewrite_pass.cpp +++ b/src/shader_recompiler/ir/passes/ssa_rewrite_pass.cpp @@ -173,7 +173,7 @@ public: } template - IR::Value ReadVariable(Type variable, IR::Block* root_block) { + IR::Value ReadVariable(Type variable, IR::Block* root_block, bool is_thread_bit = false) { boost::container::small_vector, 64> stack{ ReadState(nullptr), ReadState(root_block), @@ -201,7 +201,7 @@ public: } else if (!block->IsSsaSealed()) { // Incomplete CFG IR::Inst* phi{&*block->PrependNewInst(block->begin(), IR::Opcode::Phi)}; - phi->SetFlags(IR::TypeOf(UndefOpcode(variable))); + phi->SetFlags(is_thread_bit ? IR::Type::U1 : IR::TypeOf(UndefOpcode(variable))); incomplete_phis[block].insert_or_assign(variable, phi); stack.back().result = IR::Value{&*phi}; @@ -214,7 +214,7 @@ public: } else { // Break potential cycles with operandless phi IR::Inst* const phi{&*block->PrependNewInst(block->begin(), IR::Opcode::Phi)}; - phi->SetFlags(IR::TypeOf(UndefOpcode(variable))); + phi->SetFlags(is_thread_bit ? IR::Type::U1 : IR::TypeOf(UndefOpcode(variable))); WriteVariable(variable, block, IR::Value{phi}); @@ -263,7 +263,8 @@ private: template IR::Value AddPhiOperands(Type variable, IR::Inst& phi, IR::Block* block) { for (IR::Block* const imm_pred : block->ImmPredecessors()) { - phi.AddPhiOperand(imm_pred, ReadVariable(variable, imm_pred)); + const bool is_thread_bit = std::is_same_v && phi.Flags() == IR::Type::U1; + phi.AddPhiOperand(imm_pred, ReadVariable(variable, imm_pred, is_thread_bit)); } return TryRemoveTrivialPhi(phi, block, UndefOpcode(variable)); } @@ -346,7 +347,7 @@ void VisitInst(Pass& pass, IR::Block* block, IR::Inst& inst) { case IR::Opcode::GetThreadBitScalarReg: case IR::Opcode::GetScalarRegister: { const IR::ScalarReg reg{inst.Arg(0).ScalarReg()}; - inst.ReplaceUsesWith(pass.ReadVariable(reg, block)); + inst.ReplaceUsesWith(pass.ReadVariable(reg, block, opcode == IR::Opcode::GetThreadBitScalarReg)); break; } case IR::Opcode::GetVectorRegister: { From ba0be91c73a940bccdaf13a880ce8a3f72f60599 Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Mon, 15 Jul 2024 14:18:28 +0300 Subject: [PATCH 13/34] clang format fix --- src/core/libraries/kernel/event_flag/event_flag_obj.h | 2 +- src/shader_recompiler/ir/breadth_first_search.h | 4 ++-- src/shader_recompiler/ir/opcodes.h | 2 +- src/shader_recompiler/ir/passes/ssa_rewrite_pass.cpp | 6 ++++-- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/core/libraries/kernel/event_flag/event_flag_obj.h b/src/core/libraries/kernel/event_flag/event_flag_obj.h index 99ae1981..fe50be69 100644 --- a/src/core/libraries/kernel/event_flag/event_flag_obj.h +++ b/src/core/libraries/kernel/event_flag/event_flag_obj.h @@ -20,7 +20,7 @@ public: EventFlagInternal(const std::string& name, ThreadMode thread_mode, QueueMode queue_mode, uint64_t bits) - : m_name(name), m_thread_mode(thread_mode), m_queue_mode(queue_mode), m_bits(bits) {}; + : m_name(name), m_thread_mode(thread_mode), m_queue_mode(queue_mode), m_bits(bits){}; int Wait(u64 bits, WaitMode wait_mode, ClearMode clear_mode, u64* result, u32* ptr_micros); int Poll(u64 bits, WaitMode wait_mode, ClearMode clear_mode, u64* result); diff --git a/src/shader_recompiler/ir/breadth_first_search.h b/src/shader_recompiler/ir/breadth_first_search.h index be46b48c..21a34a90 100644 --- a/src/shader_recompiler/ir/breadth_first_search.h +++ b/src/shader_recompiler/ir/breadth_first_search.h @@ -12,8 +12,8 @@ namespace Shader::IR { template -auto BreadthFirstSearch(const Value& value, - Pred&& pred) -> std::invoke_result_t { +auto BreadthFirstSearch(const Value& value, Pred&& pred) + -> std::invoke_result_t { if (value.IsImmediate()) { // Nothing to do with immediates return std::nullopt; diff --git a/src/shader_recompiler/ir/opcodes.h b/src/shader_recompiler/ir/opcodes.h index 22cc8c2b..66b60221 100644 --- a/src/shader_recompiler/ir/opcodes.h +++ b/src/shader_recompiler/ir/opcodes.h @@ -52,7 +52,7 @@ constexpr Type F64x2{Type::F64x2}; constexpr Type F64x3{Type::F64x3}; constexpr Type F64x4{Type::F64x4}; -constexpr OpcodeMeta META_TABLE[] { +constexpr OpcodeMeta META_TABLE[]{ #define OPCODE(name_token, type_token, ...) \ { \ .name{#name_token}, \ diff --git a/src/shader_recompiler/ir/passes/ssa_rewrite_pass.cpp b/src/shader_recompiler/ir/passes/ssa_rewrite_pass.cpp index 4699f09a..6a43ad6b 100644 --- a/src/shader_recompiler/ir/passes/ssa_rewrite_pass.cpp +++ b/src/shader_recompiler/ir/passes/ssa_rewrite_pass.cpp @@ -263,7 +263,8 @@ private: template IR::Value AddPhiOperands(Type variable, IR::Inst& phi, IR::Block* block) { for (IR::Block* const imm_pred : block->ImmPredecessors()) { - const bool is_thread_bit = std::is_same_v && phi.Flags() == IR::Type::U1; + const bool is_thread_bit = + std::is_same_v && phi.Flags() == IR::Type::U1; phi.AddPhiOperand(imm_pred, ReadVariable(variable, imm_pred, is_thread_bit)); } return TryRemoveTrivialPhi(phi, block, UndefOpcode(variable)); @@ -347,7 +348,8 @@ void VisitInst(Pass& pass, IR::Block* block, IR::Inst& inst) { case IR::Opcode::GetThreadBitScalarReg: case IR::Opcode::GetScalarRegister: { const IR::ScalarReg reg{inst.Arg(0).ScalarReg()}; - inst.ReplaceUsesWith(pass.ReadVariable(reg, block, opcode == IR::Opcode::GetThreadBitScalarReg)); + inst.ReplaceUsesWith( + pass.ReadVariable(reg, block, opcode == IR::Opcode::GetThreadBitScalarReg)); break; } case IR::Opcode::GetVectorRegister: { From 02988891313c523e2c901478fc3d8f3614a2ee8b Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Mon, 15 Jul 2024 14:50:23 +0300 Subject: [PATCH 14/34] windows std::filesystem fixes --- src/core/file_sys/fs.cpp | 2 +- src/core/libraries/kernel/file_system.cpp | 6 +++--- src/core/libraries/libc/libc_stdio.cpp | 6 +++--- src/core/libraries/save_data/savedata.cpp | 8 ++++---- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/core/file_sys/fs.cpp b/src/core/file_sys/fs.cpp index f009dc2d..8f7ec08b 100644 --- a/src/core/file_sys/fs.cpp +++ b/src/core/file_sys/fs.cpp @@ -74,7 +74,7 @@ std::filesystem::path MntPoints::GetHostPath(const std::string& guest_directory) bool found_match = false; for (const auto& path : std::filesystem::directory_iterator(current_path)) { const auto candidate = path.path().filename(); - const auto filename = Common::ToLower(candidate); + const auto filename = Common::ToLower(candidate.string()); // Check if a filename matches in case insensitive manner. if (filename != part_low) { continue; diff --git a/src/core/libraries/kernel/file_system.cpp b/src/core/libraries/kernel/file_system.cpp index be173320..f56b05da 100644 --- a/src/core/libraries/kernel/file_system.cpp +++ b/src/core/libraries/kernel/file_system.cpp @@ -58,7 +58,7 @@ int PS4_SYSV_ABI sceKernelOpen(const char* path, int flags, u16 mode) { if (directory) { file->is_directory = true; file->m_guest_name = path; - file->m_host_name = mnt->GetHostPath(file->m_guest_name); + file->m_host_name = mnt->GetHostPath(file->m_guest_name).string(); if (!std::filesystem::is_directory(file->m_host_name)) { // directory doesn't exist h->DeleteHandle(handle); return ORBIS_KERNEL_ERROR_ENOTDIR; @@ -72,7 +72,7 @@ int PS4_SYSV_ABI sceKernelOpen(const char* path, int flags, u16 mode) { } } else { file->m_guest_name = path; - file->m_host_name = mnt->GetHostPath(file->m_guest_name); + file->m_host_name = mnt->GetHostPath(file->m_guest_name).string(); int e = 0; if (read) { e = file->f.Open(file->m_host_name, Common::FS::FileAccessMode::Read); @@ -174,7 +174,7 @@ int PS4_SYSV_ABI sceKernelUnlink(const char* path) { return SCE_KERNEL_ERROR_EPERM; } - auto* file = h->getFile(host_path); + auto* file = h->getFile(host_path.string()); if (file != nullptr) { file->f.Unlink(); } diff --git a/src/core/libraries/libc/libc_stdio.cpp b/src/core/libraries/libc/libc_stdio.cpp index 72337b83..2b15bd36 100644 --- a/src/core/libraries/libc/libc_stdio.cpp +++ b/src/core/libraries/libc/libc_stdio.cpp @@ -10,12 +10,12 @@ namespace Libraries::LibC { std::FILE* PS4_SYSV_ABI ps4_fopen(const char* filename, const char* mode) { auto* mnt = Common::Singleton::Instance(); - const auto host_path = mnt->GetHostPath(filename); + const auto host_path = mnt->GetHostPath(filename).string(); FILE* f = std::fopen(host_path.c_str(), mode); if (f != nullptr) { - LOG_INFO(Lib_LibC, "fopen = {}", host_path.native()); + LOG_INFO(Lib_LibC, "fopen = {}", host_path); } else { - LOG_INFO(Lib_LibC, "fopen can't open = {}", host_path.native()); + LOG_INFO(Lib_LibC, "fopen can't open = {}", host_path); } return f; } diff --git a/src/core/libraries/save_data/savedata.cpp b/src/core/libraries/save_data/savedata.cpp index 9f3564e8..71828b33 100644 --- a/src/core/libraries/save_data/savedata.cpp +++ b/src/core/libraries/save_data/savedata.cpp @@ -55,7 +55,7 @@ int PS4_SYSV_ABI sceSaveDataCheckBackupData(const OrbisSaveDataCheckBackupData* if (!std::filesystem::exists(mount_dir)) { return ORBIS_SAVE_DATA_ERROR_NOT_FOUND; } - LOG_INFO(Lib_SaveData, "called = {}", mount_dir.native()); + LOG_INFO(Lib_SaveData, "called = {}", mount_dir.string()); return ORBIS_OK; } @@ -396,7 +396,7 @@ int PS4_SYSV_ABI sceSaveDataLoadIcon(const OrbisSaveDataMountPoint* mountPoint, OrbisSaveDataIcon* icon) { auto* mnt = Common::Singleton::Instance(); const auto mount_dir = mnt->GetHostPath(mountPoint->data); - LOG_INFO(Lib_SaveData, "called: dir = {}", mount_dir.native()); + LOG_INFO(Lib_SaveData, "called: dir = {}", mount_dir.string()); if (icon != nullptr) { Common::FS::IOFile file(mount_dir / "save_data.png", Common::FS::FileAccessMode::Read); @@ -534,7 +534,7 @@ int PS4_SYSV_ABI sceSaveDataSaveIcon(const OrbisSaveDataMountPoint* mountPoint, const OrbisSaveDataIcon* icon) { auto* mnt = Common::Singleton::Instance(); const auto mount_dir = mnt->GetHostPath(mountPoint->data); - LOG_INFO(Lib_SaveData, "called = {}", mount_dir.native()); + LOG_INFO(Lib_SaveData, "called = {}", mount_dir.string()); if (icon != nullptr) { Common::FS::IOFile file(mount_dir / "save_data.png", Common::FS::FileAccessMode::Write); @@ -558,7 +558,7 @@ int PS4_SYSV_ABI sceSaveDataSetParam(const OrbisSaveDataMountPoint* mountPoint, size_t paramBufSize) { auto* mnt = Common::Singleton::Instance(); const auto mount_dir = mnt->GetHostPath(mountPoint->data); - LOG_INFO(Lib_SaveData, "called = {}, mountPoint->data = {}", mount_dir.native(), + LOG_INFO(Lib_SaveData, "called = {}, mountPoint->data = {}", mount_dir.string(), mountPoint->data); if (paramBuf != nullptr) { From 0599f4e6a5f0ccd5a767a7ea1ce4e846e107c6bb Mon Sep 17 00:00:00 2001 From: IndecisiveTurtle <47210458+raphaelthegreat@users.noreply.github.com> Date: Mon, 15 Jul 2024 14:38:42 +0300 Subject: [PATCH 15/34] fs: Fix case of getting the mount itself --- src/core/file_sys/fs.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/core/file_sys/fs.cpp b/src/core/file_sys/fs.cpp index f009dc2d..ee900a9f 100644 --- a/src/core/file_sys/fs.cpp +++ b/src/core/file_sys/fs.cpp @@ -31,11 +31,16 @@ std::filesystem::path MntPoints::GetHostPath(const std::string& guest_directory) return guest_directory; } + // Nothing to do if getting the mount itself. + if (guest_directory == mount->mount) { + return mount->host_path; + } + // Remove device (e.g /app0) from path to retrieve relative path. const u32 pos = mount->mount.size() + 1; const auto rel_path = std::string_view(guest_directory).substr(pos); const auto host_path = mount->host_path / rel_path; - if (!NeedsCaseInsensiveSearch || std::filesystem::exists(host_path)) { + if (!NeedsCaseInsensiveSearch) { return host_path; } @@ -87,7 +92,7 @@ std::filesystem::path MntPoints::GetHostPath(const std::string& guest_directory) if (!found_match) { // Opening the guest path will surely fail but at least gives // a better error message than the empty path. - return guest_directory; + return host_path; } } From 356b59b041a72941c92147c8347b056f5880ad53 Mon Sep 17 00:00:00 2001 From: IndecisiveTurtle <47210458+raphaelthegreat@users.noreply.github.com> Date: Mon, 15 Jul 2024 14:51:45 +0300 Subject: [PATCH 16/34] kernel: Remove linux hack * Breaks many games, only benefits BB. Tbd --- src/core/libraries/kernel/thread_management.cpp | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/core/libraries/kernel/thread_management.cpp b/src/core/libraries/kernel/thread_management.cpp index b31e777d..351f12ee 100644 --- a/src/core/libraries/kernel/thread_management.cpp +++ b/src/core/libraries/kernel/thread_management.cpp @@ -1002,17 +1002,7 @@ ScePthread PThreadPool::Create() { } } -#ifdef _WIN64 auto* ret = new PthreadInternal{}; -#else - // TODO: Linux specific hack - static u8* hint_address = reinterpret_cast(0x7FFFFC000ULL); - auto* ret = reinterpret_cast( - mmap(hint_address, sizeof(PthreadInternal), PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0)); - hint_address += Common::AlignUp(sizeof(PthreadInternal), 4_KB); -#endif - ret->is_free = false; ret->is_detached = false; ret->is_almost_done = false; From 3c8b3f9a290f99368814465e0c5a13ac6921c942 Mon Sep 17 00:00:00 2001 From: IndecisiveTurtle <47210458+raphaelthegreat@users.noreply.github.com> Date: Mon, 15 Jul 2024 15:14:04 +0300 Subject: [PATCH 17/34] fs: Remove some unnecessary string conversions --- src/core/file_sys/fs.cpp | 11 +++-------- src/core/file_sys/fs.h | 4 ++-- src/core/libraries/kernel/file_system.cpp | 8 ++++---- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/core/file_sys/fs.cpp b/src/core/file_sys/fs.cpp index bfbd6737..5f462937 100644 --- a/src/core/file_sys/fs.cpp +++ b/src/core/file_sys/fs.cpp @@ -131,14 +131,9 @@ File* HandleTable::GetFile(int d) { return m_files.at(d - RESERVED_HANDLES); } -File* HandleTable::getFile(const std::string& host_name) { - std::scoped_lock lock{m_mutex}; - for (auto* file : m_files) { - if (file != nullptr && file->m_host_name == host_name) { - return file; - } - } - return nullptr; +File* HandleTable::GetFile(const std::filesystem::path& host_name) { + const auto it = std::ranges::find(m_files, host_name, &File::m_host_name); + return it == m_files.end() ? nullptr : *it; } } // namespace Core::FileSys diff --git a/src/core/file_sys/fs.h b/src/core/file_sys/fs.h index ede386e5..d636f8bf 100644 --- a/src/core/file_sys/fs.h +++ b/src/core/file_sys/fs.h @@ -54,7 +54,7 @@ struct DirEntry { struct File { std::atomic_bool is_opened{}; std::atomic_bool is_directory{}; - std::string m_host_name; + std::filesystem::path m_host_name; std::string m_guest_name; Common::FS::IOFile f; std::vector dirents; @@ -70,7 +70,7 @@ public: int CreateHandle(); void DeleteHandle(int d); File* GetFile(int d); - File* getFile(const std::string& host_name); + File* GetFile(const std::filesystem::path& host_name); private: std::vector m_files; diff --git a/src/core/libraries/kernel/file_system.cpp b/src/core/libraries/kernel/file_system.cpp index f56b05da..23b949a8 100644 --- a/src/core/libraries/kernel/file_system.cpp +++ b/src/core/libraries/kernel/file_system.cpp @@ -13,7 +13,7 @@ namespace Libraries::Kernel { -std::vector GetDirectoryEntries(const std::string& path) { +std::vector GetDirectoryEntries(const std::filesystem::path& path) { std::vector files; for (const auto& entry : std::filesystem::directory_iterator(path)) { auto& dir_entry = files.emplace_back(); @@ -58,7 +58,7 @@ int PS4_SYSV_ABI sceKernelOpen(const char* path, int flags, u16 mode) { if (directory) { file->is_directory = true; file->m_guest_name = path; - file->m_host_name = mnt->GetHostPath(file->m_guest_name).string(); + file->m_host_name = mnt->GetHostPath(file->m_guest_name); if (!std::filesystem::is_directory(file->m_host_name)) { // directory doesn't exist h->DeleteHandle(handle); return ORBIS_KERNEL_ERROR_ENOTDIR; @@ -72,7 +72,7 @@ int PS4_SYSV_ABI sceKernelOpen(const char* path, int flags, u16 mode) { } } else { file->m_guest_name = path; - file->m_host_name = mnt->GetHostPath(file->m_guest_name).string(); + file->m_host_name = mnt->GetHostPath(file->m_guest_name); int e = 0; if (read) { e = file->f.Open(file->m_host_name, Common::FS::FileAccessMode::Read); @@ -174,7 +174,7 @@ int PS4_SYSV_ABI sceKernelUnlink(const char* path) { return SCE_KERNEL_ERROR_EPERM; } - auto* file = h->getFile(host_path.string()); + auto* file = h->GetFile(host_path); if (file != nullptr) { file->f.Unlink(); } From 3b0a5aed333a3ef61a7cbb65bb9a14bf751039ea Mon Sep 17 00:00:00 2001 From: TheTurtle <47210458+raphaelthegreat@users.noreply.github.com> Date: Mon, 15 Jul 2024 15:42:35 +0300 Subject: [PATCH 18/34] fs: Use proper append operator --- src/core/file_sys/fs.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/file_sys/fs.cpp b/src/core/file_sys/fs.cpp index 5f462937..f2fda657 100644 --- a/src/core/file_sys/fs.cpp +++ b/src/core/file_sys/fs.cpp @@ -64,8 +64,8 @@ std::filesystem::path MntPoints::GetHostPath(const std::string& guest_directory) while (!path_parts.empty()) { const auto& part = path_parts.back(); const auto add_match = [&](const auto& host_part) { - current_path += host_part; - guest_path += part; + current_path /= host_part; + guest_path /= part; path_cache[guest_path] = current_path; path_parts.pop_back(); }; From 0dda5eb6ef320e16d8e49fd1da5798d28ca6f819 Mon Sep 17 00:00:00 2001 From: TheTurtle <47210458+raphaelthegreat@users.noreply.github.com> Date: Mon, 15 Jul 2024 16:52:24 +0300 Subject: [PATCH 19/34] fs: Fix nullptr check in GetFile --- src/core/file_sys/fs.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/core/file_sys/fs.cpp b/src/core/file_sys/fs.cpp index f2fda657..c6bdf7ad 100644 --- a/src/core/file_sys/fs.cpp +++ b/src/core/file_sys/fs.cpp @@ -132,8 +132,12 @@ File* HandleTable::GetFile(int d) { } File* HandleTable::GetFile(const std::filesystem::path& host_name) { - const auto it = std::ranges::find(m_files, host_name, &File::m_host_name); - return it == m_files.end() ? nullptr : *it; + for (auto* file : m_files) { + if (file != nullptr && file->m_host_name == host_name) { + return file; + } + } + return nullptr; } } // namespace Core::FileSys From 3ff11818ab7c9eaa7b5b165b1e3c6332896ce079 Mon Sep 17 00:00:00 2001 From: squidbus <175574877+squidbus@users.noreply.github.com> Date: Tue, 16 Jul 2024 04:40:43 -0700 Subject: [PATCH 20/34] Return EBUSY from sceKernelPollEventFlag instead of ETIMEDOUT. --- src/core/libraries/kernel/event_flag/event_flag_obj.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/core/libraries/kernel/event_flag/event_flag_obj.cpp b/src/core/libraries/kernel/event_flag/event_flag_obj.cpp index 46429d40..fe0f34b8 100644 --- a/src/core/libraries/kernel/event_flag/event_flag_obj.cpp +++ b/src/core/libraries/kernel/event_flag/event_flag_obj.cpp @@ -73,7 +73,12 @@ int EventFlagInternal::Wait(u64 bits, WaitMode wait_mode, ClearMode clear_mode, int EventFlagInternal::Poll(u64 bits, WaitMode wait_mode, ClearMode clear_mode, u64* result) { u32 micros = 0; - return Wait(bits, wait_mode, clear_mode, result, µs); + auto ret = Wait(bits, wait_mode, clear_mode, result, µs); + if (ret == ORBIS_KERNEL_ERROR_ETIMEDOUT) { + // Poll returns EBUSY instead. + ret = ORBIS_KERNEL_ERROR_EBUSY; + } + return ret; } void EventFlagInternal::Set(u64 bits) { From f9e96793ccd0fabd33fc1cda4805f3a388ba27c1 Mon Sep 17 00:00:00 2001 From: Vladislav Mikhalin Date: Tue, 16 Jul 2024 15:03:07 +0300 Subject: [PATCH 21/34] Implemented load_buffer_format_* conversions (#295) * Implemented load_buffer_format_* conversions * clang-format insists on ugly things --- .../spirv/emit_spirv_context_get_set.cpp | 241 +++++++++++++++--- .../backend/spirv/emit_spirv_instructions.h | 4 + .../backend/spirv/spirv_emit_context.cpp | 5 +- .../backend/spirv/spirv_emit_context.h | 1 + .../frontend/translate/translate.cpp | 33 ++- .../frontend/translate/translate.h | 2 +- .../frontend/translate/vector_memory.cpp | 6 +- src/shader_recompiler/ir/ir_emitter.cpp | 16 ++ src/shader_recompiler/ir/ir_emitter.h | 2 + src/shader_recompiler/ir/opcodes.inc | 4 + .../ir/passes/resource_tracking_pass.cpp | 133 ++++++++-- src/shader_recompiler/runtime_info.h | 3 +- src/video_core/amdgpu/pixel_format.cpp | 106 ++++++++ src/video_core/amdgpu/pixel_format.h | 2 + src/video_core/amdgpu/resource.h | 8 - 15 files changed, 475 insertions(+), 91 deletions(-) diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp b/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp index 75ee3ae9..c88a1cbb 100644 --- a/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp +++ b/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp @@ -4,6 +4,8 @@ #include "shader_recompiler/backend/spirv/emit_spirv_instructions.h" #include "shader_recompiler/backend/spirv/spirv_emit_context.h" +#include + namespace Shader::Backend::SPIRV { namespace { @@ -209,57 +211,216 @@ void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, Id value, u32 elemen ctx.OpStore(pointer, value); } -Id EmitLoadBufferF32(EmitContext& ctx, IR::Inst* inst, u32 handle, Id address) { - const auto info = inst->Flags(); - const auto& buffer = ctx.buffers[handle]; - if (info.index_enable && info.offset_enable) { - UNREACHABLE(); - } else if (info.index_enable) { - const Id ptr{ - ctx.OpAccessChain(buffer.pointer_type, buffer.id, ctx.u32_zero_value, address)}; - return ctx.OpLoad(buffer.data_types->Get(1), ptr); - } - UNREACHABLE(); -} - Id EmitLoadBufferU32(EmitContext& ctx, IR::Inst* inst, u32 handle, Id address) { return EmitLoadBufferF32(ctx, inst, handle, address); } -Id EmitLoadBufferF32x2(EmitContext& ctx, IR::Inst* inst, u32 handle, Id address) { - const auto info = inst->Flags(); +template +static Id EmitLoadBufferF32xN(EmitContext& ctx, u32 handle, Id address) { const auto& buffer = ctx.buffers[handle]; - boost::container::static_vector ids; - for (u32 i = 0; i < 2; i++) { - const Id index{ctx.OpIAdd(ctx.U32[1], address, ctx.ConstU32(i))}; - const Id ptr{ctx.OpAccessChain(buffer.pointer_type, buffer.id, ctx.u32_zero_value, index)}; - ids.push_back(ctx.OpLoad(buffer.data_types->Get(1), ptr)); + Id index = ctx.OpShiftRightLogical(ctx.U32[1], address, ctx.ConstU32(2u)); + if constexpr (N == 1) { + const Id ptr{ + ctx.OpAccessChain(buffer.pointer_type, buffer.id, ctx.u32_zero_value, address)}; + return ctx.OpLoad(buffer.data_types->Get(1), ptr); + } else { + boost::container::static_vector ids; + for (u32 i = 0; i < N; i++) { + index = ctx.OpIAdd(ctx.U32[1], index, ctx.ConstU32(i)); + const Id ptr{ + ctx.OpAccessChain(buffer.pointer_type, buffer.id, ctx.u32_zero_value, index)}; + ids.push_back(ctx.OpLoad(buffer.data_types->Get(1), ptr)); + } + return ctx.OpCompositeConstruct(buffer.data_types->Get(N), ids); } - return ctx.OpCompositeConstruct(buffer.data_types->Get(2), ids); } -Id EmitLoadBufferF32x3(EmitContext& ctx, IR::Inst* inst, u32 handle, Id address) { - const auto info = inst->Flags(); - const auto& buffer = ctx.buffers[handle]; - boost::container::static_vector ids; - for (u32 i = 0; i < 3; i++) { - const Id index{ctx.OpIAdd(ctx.U32[1], address, ctx.ConstU32(i))}; - const Id ptr{ctx.OpAccessChain(buffer.pointer_type, buffer.id, ctx.u32_zero_value, index)}; - ids.push_back(ctx.OpLoad(buffer.data_types->Get(1), ptr)); - } - return ctx.OpCompositeConstruct(buffer.data_types->Get(3), ids); +Id EmitLoadBufferF32(EmitContext& ctx, IR::Inst*, u32 handle, Id address) { + return EmitLoadBufferF32xN<1>(ctx, handle, address); } -Id EmitLoadBufferF32x4(EmitContext& ctx, IR::Inst* inst, u32 handle, Id address) { - const auto info = inst->Flags(); - const auto& buffer = ctx.buffers[handle]; - boost::container::static_vector ids; - for (u32 i = 0; i < 4; i++) { - const Id index{ctx.OpIAdd(ctx.U32[1], address, ctx.ConstU32(i))}; - const Id ptr{ctx.OpAccessChain(buffer.pointer_type, buffer.id, ctx.u32_zero_value, index)}; - ids.push_back(ctx.OpLoad(buffer.data_types->Get(1), ptr)); +Id EmitLoadBufferF32x2(EmitContext& ctx, IR::Inst*, u32 handle, Id address) { + return EmitLoadBufferF32xN<2>(ctx, handle, address); +} + +Id EmitLoadBufferF32x3(EmitContext& ctx, IR::Inst*, u32 handle, Id address) { + return EmitLoadBufferF32xN<3>(ctx, handle, address); +} + +Id EmitLoadBufferF32x4(EmitContext& ctx, IR::Inst*, u32 handle, Id address) { + return EmitLoadBufferF32xN<4>(ctx, handle, address); +} + +static bool IsSignedInteger(AmdGpu::NumberFormat format) { + switch (format) { + case AmdGpu::NumberFormat::Unorm: + case AmdGpu::NumberFormat::Uscaled: + case AmdGpu::NumberFormat::Uint: + return false; + case AmdGpu::NumberFormat::Snorm: + case AmdGpu::NumberFormat::Sscaled: + case AmdGpu::NumberFormat::Sint: + case AmdGpu::NumberFormat::SnormNz: + return true; + case AmdGpu::NumberFormat::Float: + default: + UNREACHABLE(); } - return ctx.OpCompositeConstruct(buffer.data_types->Get(4), ids); +} + +static u32 UXBitsMax(u32 bit_width) { + return (1u << bit_width) - 1u; +} + +static u32 SXBitsMax(u32 bit_width) { + return (1u << (bit_width - 1u)) - 1u; +} + +static Id ConvertValue(EmitContext& ctx, Id value, AmdGpu::NumberFormat format, u32 bit_width) { + switch (format) { + case AmdGpu::NumberFormat::Unorm: + return ctx.OpFDiv(ctx.F32[1], value, ctx.ConstF32(float(UXBitsMax(bit_width)))); + case AmdGpu::NumberFormat::Snorm: + return ctx.OpFDiv(ctx.F32[1], value, ctx.ConstF32(float(SXBitsMax(bit_width)))); + case AmdGpu::NumberFormat::SnormNz: + // (x * 2 + 1) / (Format::SMAX * 2) + value = ctx.OpFMul(ctx.F32[1], value, ctx.ConstF32(2.f)); + value = ctx.OpFAdd(ctx.F32[1], value, ctx.ConstF32(1.f)); + return ctx.OpFDiv(ctx.F32[1], value, ctx.ConstF32(float(SXBitsMax(bit_width) * 2))); + case AmdGpu::NumberFormat::Uscaled: + case AmdGpu::NumberFormat::Sscaled: + case AmdGpu::NumberFormat::Uint: + case AmdGpu::NumberFormat::Sint: + case AmdGpu::NumberFormat::Float: + return value; + default: + UNREACHABLE_MSG("Unsupported number fromat for conversion: {}", + magic_enum::enum_name(format)); + } +} + +static Id ComponentOffset(EmitContext& ctx, Id address, u32 stride, u32 bit_offset) { + Id comp_offset = ctx.ConstU32(bit_offset); + if (stride < 4) { + // comp_offset += (address % 4) * 8; + const Id byte_offset = ctx.OpUMod(ctx.U32[1], address, ctx.ConstU32(4u)); + const Id bit_offset = ctx.OpShiftLeftLogical(ctx.U32[1], byte_offset, ctx.ConstU32(3u)); + comp_offset = ctx.OpIAdd(ctx.U32[1], comp_offset, bit_offset); + } + return comp_offset; +} + +static Id GetBufferFormatValue(EmitContext& ctx, u32 handle, Id address, u32 comp) { + const auto& buffer = ctx.buffers[handle]; + const auto format = buffer.buffer.GetDataFmt(); + switch (format) { + case AmdGpu::DataFormat::FormatInvalid: + return ctx.f32_zero_value; + case AmdGpu::DataFormat::Format8: + case AmdGpu::DataFormat::Format16: + case AmdGpu::DataFormat::Format32: + case AmdGpu::DataFormat::Format8_8: + case AmdGpu::DataFormat::Format16_16: + case AmdGpu::DataFormat::Format10_11_11: + case AmdGpu::DataFormat::Format11_11_10: + case AmdGpu::DataFormat::Format10_10_10_2: + case AmdGpu::DataFormat::Format2_10_10_10: + case AmdGpu::DataFormat::Format8_8_8_8: + case AmdGpu::DataFormat::Format32_32: + case AmdGpu::DataFormat::Format16_16_16_16: + case AmdGpu::DataFormat::Format32_32_32: + case AmdGpu::DataFormat::Format32_32_32_32: { + const u32 num_components = AmdGpu::NumComponents(format); + if (comp >= num_components) { + return ctx.f32_zero_value; + } + + // uint index = address / 4; + Id index = ctx.OpShiftRightLogical(ctx.U32[1], address, ctx.ConstU32(2u)); + const u32 stride = buffer.buffer.GetStride(); + if (stride > 4) { + const u32 index_offset = u32(AmdGpu::ComponentOffset(format, comp) / 32); + if (index_offset > 0) { + // index += index_offset; + index = ctx.OpIAdd(ctx.U32[1], index, ctx.ConstU32(index_offset)); + } + } + const Id ptr = ctx.OpAccessChain(buffer.pointer_type, buffer.id, ctx.u32_zero_value, index); + + const u32 bit_offset = AmdGpu::ComponentOffset(format, comp) % 32; + const u32 bit_width = AmdGpu::ComponentBits(format, comp); + const auto num_format = buffer.buffer.GetNumberFmt(); + if (num_format == AmdGpu::NumberFormat::Float) { + if (bit_width == 32) { + return ctx.OpLoad(ctx.F32[1], ptr); + } else if (bit_width == 16) { + const Id comp_offset = ComponentOffset(ctx, address, stride, bit_offset); + Id value = ctx.OpLoad(ctx.U32[1], ptr); + value = + ctx.OpBitFieldSExtract(ctx.S32[1], value, comp_offset, ctx.ConstU32(bit_width)); + value = ctx.OpSConvert(ctx.U16, value); + value = ctx.OpBitcast(ctx.F16[1], value); + return ctx.OpFConvert(ctx.F32[1], value); + } else { + UNREACHABLE_MSG("Invalid float bit width {}", bit_width); + } + } else { + Id value = ctx.OpLoad(ctx.U32[1], ptr); + const bool is_signed = IsSignedInteger(num_format); + if (bit_width < 32) { + const Id comp_offset = ComponentOffset(ctx, address, stride, bit_offset); + if (is_signed) { + value = ctx.OpBitFieldSExtract(ctx.S32[1], value, comp_offset, + ctx.ConstU32(bit_width)); + value = ctx.OpConvertSToF(ctx.F32[1], value); + } else { + value = ctx.OpBitFieldUExtract(ctx.U32[1], value, comp_offset, + ctx.ConstU32(bit_width)); + value = ctx.OpConvertUToF(ctx.F32[1], value); + } + } else { + if (is_signed) { + value = ctx.OpConvertSToF(ctx.F32[1], value); + } else { + value = ctx.OpConvertUToF(ctx.F32[1], value); + } + } + return ConvertValue(ctx, value, num_format, bit_width); + } + break; + } + default: + UNREACHABLE_MSG("Invalid format for conversion: {}", magic_enum::enum_name(format)); + } +} + +template +static Id EmitLoadBufferFormatF32xN(EmitContext& ctx, IR::Inst* inst, u32 handle, Id address) { + if constexpr (N == 1) { + return GetBufferFormatValue(ctx, handle, address, 0); + } else { + boost::container::static_vector ids; + for (u32 i = 0; i < N; i++) { + ids.push_back(GetBufferFormatValue(ctx, handle, address, i)); + } + return ctx.OpCompositeConstruct(ctx.F32[N], ids); + } +} + +Id EmitLoadBufferFormatF32(EmitContext& ctx, IR::Inst* inst, u32 handle, Id address) { + return EmitLoadBufferFormatF32xN<1>(ctx, inst, handle, address); +} + +Id EmitLoadBufferFormatF32x2(EmitContext& ctx, IR::Inst* inst, u32 handle, Id address) { + return EmitLoadBufferFormatF32xN<2>(ctx, inst, handle, address); +} + +Id EmitLoadBufferFormatF32x3(EmitContext& ctx, IR::Inst* inst, u32 handle, Id address) { + return EmitLoadBufferFormatF32xN<3>(ctx, inst, handle, address); +} + +Id EmitLoadBufferFormatF32x4(EmitContext& ctx, IR::Inst* inst, u32 handle, Id address) { + return EmitLoadBufferFormatF32xN<4>(ctx, inst, handle, address); } void EmitStoreBufferF32(EmitContext& ctx, IR::Inst* inst, u32 handle, Id address, Id value) { diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h b/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h index e0b19f4f..f43ea3b3 100644 --- a/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h +++ b/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h @@ -66,6 +66,10 @@ Id EmitLoadBufferF32(EmitContext& ctx, IR::Inst* inst, u32 handle, Id address); Id EmitLoadBufferF32x2(EmitContext& ctx, IR::Inst* inst, u32 handle, Id address); Id EmitLoadBufferF32x3(EmitContext& ctx, IR::Inst* inst, u32 handle, Id address); Id EmitLoadBufferF32x4(EmitContext& ctx, IR::Inst* inst, u32 handle, Id address); +Id EmitLoadBufferFormatF32(EmitContext& ctx, IR::Inst* inst, u32 handle, Id address); +Id EmitLoadBufferFormatF32x2(EmitContext& ctx, IR::Inst* inst, u32 handle, Id address); +Id EmitLoadBufferFormatF32x3(EmitContext& ctx, IR::Inst* inst, u32 handle, Id address); +Id EmitLoadBufferFormatF32x4(EmitContext& ctx, IR::Inst* inst, u32 handle, Id address); Id EmitLoadBufferU32(EmitContext& ctx, IR::Inst* inst, u32 handle, Id address); void EmitStoreBufferF32(EmitContext& ctx, IR::Inst* inst, u32 handle, Id address, Id value); void EmitStoreBufferF32x2(EmitContext& ctx, IR::Inst* inst, u32 handle, Id address, Id value); diff --git a/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp b/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp index 98f9d1c7..3ea01a1d 100644 --- a/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp +++ b/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp @@ -301,9 +301,7 @@ void EmitContext::DefineBuffers(const Info& info) { for (u32 i = 0; const auto& buffer : info.buffers) { const auto* data_types = True(buffer.used_types & IR::Type::F32) ? &F32 : &U32; const Id data_type = (*data_types)[1]; - const u32 stride = buffer.stride == 0 ? 1 : buffer.stride; - const u32 num_elements = stride * buffer.num_records; - const Id record_array_type{TypeArray(data_type, ConstU32(num_elements))}; + const Id record_array_type{TypeArray(data_type, ConstU32(buffer.length))}; const Id struct_type{TypeStruct(record_array_type)}; if (std::ranges::find(type_ids, record_array_type.value, &Id::value) == type_ids.end()) { Decorate(record_array_type, spv::Decoration::ArrayStride, 4); @@ -333,6 +331,7 @@ void EmitContext::DefineBuffers(const Info& info) { .id = id, .data_types = data_types, .pointer_type = pointer_type, + .buffer = buffer.GetVsharp(info), }); interfaces.push_back(id); i++; diff --git a/src/shader_recompiler/backend/spirv/spirv_emit_context.h b/src/shader_recompiler/backend/spirv/spirv_emit_context.h index b51edd63..0f8081fd 100644 --- a/src/shader_recompiler/backend/spirv/spirv_emit_context.h +++ b/src/shader_recompiler/backend/spirv/spirv_emit_context.h @@ -201,6 +201,7 @@ public: Id id; const VectorIds* data_types; Id pointer_type; + AmdGpu::Buffer buffer; }; u32& binding; diff --git a/src/shader_recompiler/frontend/translate/translate.cpp b/src/shader_recompiler/frontend/translate/translate.cpp index 96f08519..bc2e0bf2 100644 --- a/src/shader_recompiler/frontend/translate/translate.cpp +++ b/src/shader_recompiler/frontend/translate/translate.cpp @@ -254,8 +254,7 @@ void Translator::EmitFetch(const GcnInst& inst) { info.buffers.push_back({ .sgpr_base = attrib.sgpr_base, .dword_offset = attrib.dword_offset, - .stride = buffer.GetStride(), - .num_records = buffer.num_records, + .length = buffer.num_records, .used_types = IR::Type::F32, .is_storage = true, // we may not fit into UBO with large meshes .is_instance_data = true, @@ -571,28 +570,40 @@ void Translate(IR::Block* block, u32 block_base, std::span inst_l translator.V_CNDMASK_B32(inst); break; case Opcode::TBUFFER_LOAD_FORMAT_X: - translator.BUFFER_LOAD_FORMAT(1, true, inst); + translator.BUFFER_LOAD_FORMAT(1, true, true, inst); break; case Opcode::TBUFFER_LOAD_FORMAT_XY: - translator.BUFFER_LOAD_FORMAT(2, true, inst); + translator.BUFFER_LOAD_FORMAT(2, true, true, inst); break; case Opcode::TBUFFER_LOAD_FORMAT_XYZ: - translator.BUFFER_LOAD_FORMAT(3, true, inst); + translator.BUFFER_LOAD_FORMAT(3, true, true, inst); break; case Opcode::TBUFFER_LOAD_FORMAT_XYZW: - translator.BUFFER_LOAD_FORMAT(4, true, inst); + translator.BUFFER_LOAD_FORMAT(4, true, true, inst); break; case Opcode::BUFFER_LOAD_FORMAT_X: - case Opcode::BUFFER_LOAD_DWORD: - translator.BUFFER_LOAD_FORMAT(1, false, inst); + translator.BUFFER_LOAD_FORMAT(1, false, true, inst); + break; + case Opcode::BUFFER_LOAD_FORMAT_XY: + translator.BUFFER_LOAD_FORMAT(2, false, true, inst); break; case Opcode::BUFFER_LOAD_FORMAT_XYZ: - case Opcode::BUFFER_LOAD_DWORDX3: - translator.BUFFER_LOAD_FORMAT(3, false, inst); + translator.BUFFER_LOAD_FORMAT(3, false, true, inst); break; case Opcode::BUFFER_LOAD_FORMAT_XYZW: + translator.BUFFER_LOAD_FORMAT(4, false, true, inst); + break; + case Opcode::BUFFER_LOAD_DWORD: + translator.BUFFER_LOAD_FORMAT(1, false, false, inst); + break; + case Opcode::BUFFER_LOAD_DWORDX2: + translator.BUFFER_LOAD_FORMAT(2, false, false, inst); + break; + case Opcode::BUFFER_LOAD_DWORDX3: + translator.BUFFER_LOAD_FORMAT(3, false, false, inst); + break; case Opcode::BUFFER_LOAD_DWORDX4: - translator.BUFFER_LOAD_FORMAT(4, false, inst); + translator.BUFFER_LOAD_FORMAT(4, false, false, inst); break; case Opcode::BUFFER_STORE_FORMAT_X: case Opcode::BUFFER_STORE_DWORD: diff --git a/src/shader_recompiler/frontend/translate/translate.h b/src/shader_recompiler/frontend/translate/translate.h index 6dd0a481..2aa6f712 100644 --- a/src/shader_recompiler/frontend/translate/translate.h +++ b/src/shader_recompiler/frontend/translate/translate.h @@ -160,7 +160,7 @@ public: void V_CMP_CLASS_F32(const GcnInst& inst); // Vector Memory - void BUFFER_LOAD_FORMAT(u32 num_dwords, bool is_typed, const GcnInst& inst); + void BUFFER_LOAD_FORMAT(u32 num_dwords, bool is_typed, bool is_format, const GcnInst& inst); void BUFFER_STORE_FORMAT(u32 num_dwords, bool is_typed, const GcnInst& inst); // Vector interpolation diff --git a/src/shader_recompiler/frontend/translate/vector_memory.cpp b/src/shader_recompiler/frontend/translate/vector_memory.cpp index f0ef85b3..1ddee523 100644 --- a/src/shader_recompiler/frontend/translate/vector_memory.cpp +++ b/src/shader_recompiler/frontend/translate/vector_memory.cpp @@ -225,7 +225,8 @@ void Translator::IMAGE_STORE(const GcnInst& inst) { ir.ImageWrite(handle, body, value, {}); } -void Translator::BUFFER_LOAD_FORMAT(u32 num_dwords, bool is_typed, const GcnInst& inst) { +void Translator::BUFFER_LOAD_FORMAT(u32 num_dwords, bool is_typed, bool is_format, + const GcnInst& inst) { const auto& mtbuf = inst.control.mtbuf; const IR::VectorReg vaddr{inst.src[0].code}; const IR::ScalarReg sharp{inst.src[2].code * 4}; @@ -254,7 +255,8 @@ void Translator::BUFFER_LOAD_FORMAT(u32 num_dwords, bool is_typed, const GcnInst const IR::Value handle = ir.CompositeConstruct(ir.GetScalarReg(sharp), ir.GetScalarReg(sharp + 1), ir.GetScalarReg(sharp + 2), ir.GetScalarReg(sharp + 3)); - const IR::Value value = ir.LoadBuffer(num_dwords, handle, address, info); + const IR::Value value = is_format ? ir.LoadBufferFormat(num_dwords, handle, address, info) + : ir.LoadBuffer(num_dwords, handle, address, info); const IR::VectorReg dst_reg{inst.src[1].code}; if (num_dwords == 1) { ir.SetVectorReg(dst_reg, IR::F32{value}); diff --git a/src/shader_recompiler/ir/ir_emitter.cpp b/src/shader_recompiler/ir/ir_emitter.cpp index 5dabbb4c..cd4fdaa2 100644 --- a/src/shader_recompiler/ir/ir_emitter.cpp +++ b/src/shader_recompiler/ir/ir_emitter.cpp @@ -327,6 +327,22 @@ Value IREmitter::LoadBuffer(int num_dwords, const Value& handle, const Value& ad } } +Value IREmitter::LoadBufferFormat(int num_dwords, const Value& handle, const Value& address, + BufferInstInfo info) { + switch (num_dwords) { + case 1: + return Inst(Opcode::LoadBufferFormatF32, Flags{info}, handle, address); + case 2: + return Inst(Opcode::LoadBufferFormatF32x2, Flags{info}, handle, address); + case 3: + return Inst(Opcode::LoadBufferFormatF32x3, Flags{info}, handle, address); + case 4: + return Inst(Opcode::LoadBufferFormatF32x4, Flags{info}, handle, address); + default: + UNREACHABLE_MSG("Invalid number of dwords {}", num_dwords); + } +} + void IREmitter::StoreBuffer(int num_dwords, const Value& handle, const Value& address, const Value& data, BufferInstInfo info) { switch (num_dwords) { diff --git a/src/shader_recompiler/ir/ir_emitter.h b/src/shader_recompiler/ir/ir_emitter.h index 5d6fd714..e7512430 100644 --- a/src/shader_recompiler/ir/ir_emitter.h +++ b/src/shader_recompiler/ir/ir_emitter.h @@ -89,6 +89,8 @@ public: [[nodiscard]] Value LoadBuffer(int num_dwords, const Value& handle, const Value& address, BufferInstInfo info); + [[nodiscard]] Value LoadBufferFormat(int num_dwords, const Value& handle, const Value& address, + BufferInstInfo info); void StoreBuffer(int num_dwords, const Value& handle, const Value& address, const Value& data, BufferInstInfo info); diff --git a/src/shader_recompiler/ir/opcodes.inc b/src/shader_recompiler/ir/opcodes.inc index 94ef1784..9aefc8b3 100644 --- a/src/shader_recompiler/ir/opcodes.inc +++ b/src/shader_recompiler/ir/opcodes.inc @@ -79,6 +79,10 @@ OPCODE(LoadBufferF32, F32, Opaq OPCODE(LoadBufferF32x2, F32x2, Opaque, Opaque, ) OPCODE(LoadBufferF32x3, F32x3, Opaque, Opaque, ) OPCODE(LoadBufferF32x4, F32x4, Opaque, Opaque, ) +OPCODE(LoadBufferFormatF32, F32, Opaque, Opaque, ) +OPCODE(LoadBufferFormatF32x2, F32x2, Opaque, Opaque, ) +OPCODE(LoadBufferFormatF32x3, F32x3, Opaque, Opaque, ) +OPCODE(LoadBufferFormatF32x4, F32x4, Opaque, Opaque, ) OPCODE(LoadBufferU32, U32, Opaque, Opaque, ) OPCODE(StoreBufferF32, Void, Opaque, Opaque, F32, ) OPCODE(StoreBufferF32x2, Void, Opaque, Opaque, F32x2, ) diff --git a/src/shader_recompiler/ir/passes/resource_tracking_pass.cpp b/src/shader_recompiler/ir/passes/resource_tracking_pass.cpp index b7d6a722..f58b4d96 100644 --- a/src/shader_recompiler/ir/passes/resource_tracking_pass.cpp +++ b/src/shader_recompiler/ir/passes/resource_tracking_pass.cpp @@ -27,6 +27,10 @@ bool IsBufferInstruction(const IR::Inst& inst) { case IR::Opcode::LoadBufferF32x2: case IR::Opcode::LoadBufferF32x3: case IR::Opcode::LoadBufferF32x4: + case IR::Opcode::LoadBufferFormatF32: + case IR::Opcode::LoadBufferFormatF32x2: + case IR::Opcode::LoadBufferFormatF32x3: + case IR::Opcode::LoadBufferFormatF32x4: case IR::Opcode::LoadBufferU32: case IR::Opcode::ReadConstBuffer: case IR::Opcode::ReadConstBufferU32: @@ -41,8 +45,49 @@ bool IsBufferInstruction(const IR::Inst& inst) { } } -IR::Type BufferDataType(const IR::Inst& inst) { +static bool UseFP16(AmdGpu::DataFormat data_format, AmdGpu::NumberFormat num_format) { + switch (num_format) { + case AmdGpu::NumberFormat::Float: + switch (data_format) { + case AmdGpu::DataFormat::Format16: + case AmdGpu::DataFormat::Format16_16: + case AmdGpu::DataFormat::Format16_16_16_16: + return true; + default: + return false; + } + case AmdGpu::NumberFormat::Unorm: + case AmdGpu::NumberFormat::Snorm: + case AmdGpu::NumberFormat::Uscaled: + case AmdGpu::NumberFormat::Sscaled: + case AmdGpu::NumberFormat::Uint: + case AmdGpu::NumberFormat::Sint: + case AmdGpu::NumberFormat::SnormNz: + default: + return false; + } +} + +IR::Type BufferDataType(const IR::Inst& inst, AmdGpu::NumberFormat num_format) { switch (inst.GetOpcode()) { + case IR::Opcode::LoadBufferFormatF32: + case IR::Opcode::LoadBufferFormatF32x2: + case IR::Opcode::LoadBufferFormatF32x3: + case IR::Opcode::LoadBufferFormatF32x4: + switch (num_format) { + case AmdGpu::NumberFormat::Unorm: + case AmdGpu::NumberFormat::Snorm: + case AmdGpu::NumberFormat::Uscaled: + case AmdGpu::NumberFormat::Sscaled: + case AmdGpu::NumberFormat::Uint: + case AmdGpu::NumberFormat::Sint: + case AmdGpu::NumberFormat::SnormNz: + return IR::Type::U32; + case AmdGpu::NumberFormat::Float: + return IR::Type::F32; + default: + UNREACHABLE(); + } case IR::Opcode::LoadBufferF32: case IR::Opcode::LoadBufferF32x2: case IR::Opcode::LoadBufferF32x3: @@ -141,7 +186,7 @@ public: desc.inline_cbuf == existing.inline_cbuf; })}; auto& buffer = buffer_resources[index]; - ASSERT(buffer.stride == desc.stride && buffer.num_records == desc.num_records); + ASSERT(buffer.length == desc.length); buffer.is_storage |= desc.is_storage; buffer.used_types |= desc.used_types; return index; @@ -263,6 +308,41 @@ SharpLocation TrackSharp(const IR::Inst* inst) { static constexpr size_t MaxUboSize = 65536; +static bool IsLoadBufferFormat(const IR::Inst& inst) { + switch (inst.GetOpcode()) { + case IR::Opcode::LoadBufferFormatF32: + case IR::Opcode::LoadBufferFormatF32x2: + case IR::Opcode::LoadBufferFormatF32x3: + case IR::Opcode::LoadBufferFormatF32x4: + return true; + default: + return false; + } +} + +static bool IsReadConstBuffer(const IR::Inst& inst) { + switch (inst.GetOpcode()) { + case IR::Opcode::ReadConstBuffer: + case IR::Opcode::ReadConstBufferU32: + return true; + default: + return false; + } +} + +static u32 BufferLength(const AmdGpu::Buffer& buffer) { + const auto stride = buffer.GetStride(); + if (stride < sizeof(f32)) { + ASSERT(sizeof(f32) % stride == 0); + return (((buffer.num_records - 1) / sizeof(f32)) + 1) * stride; + } else if (stride == sizeof(f32)) { + return buffer.num_records; + } else { + ASSERT(stride % sizeof(f32) == 0); + return buffer.num_records * (stride / sizeof(f32)); + } +} + s32 TryHandleInlineCbuf(IR::Inst& inst, Info& info, Descriptors& descriptors, AmdGpu::Buffer& cbuf) { @@ -298,9 +378,8 @@ s32 TryHandleInlineCbuf(IR::Inst& inst, Info& info, Descriptors& descriptors, return descriptors.Add(BufferResource{ .sgpr_base = std::numeric_limits::max(), .dword_offset = 0, - .stride = cbuf.GetStride(), - .num_records = u32(cbuf.num_records), - .used_types = BufferDataType(inst), + .length = BufferLength(cbuf), + .used_types = BufferDataType(inst, cbuf.GetNumberFmt()), .inline_cbuf = cbuf, .is_storage = IsBufferStore(inst) || cbuf.GetSize() > MaxUboSize, }); @@ -318,9 +397,8 @@ void PatchBufferInstruction(IR::Block& block, IR::Inst& inst, Info& info, binding = descriptors.Add(BufferResource{ .sgpr_base = sharp.sgpr_base, .dword_offset = sharp.dword_offset, - .stride = buffer.GetStride(), - .num_records = u32(buffer.num_records), - .used_types = BufferDataType(inst), + .length = BufferLength(buffer), + .used_types = BufferDataType(inst, buffer.GetNumberFmt()), .is_storage = IsBufferStore(inst) || buffer.GetSize() > MaxUboSize, }); } @@ -337,24 +415,31 @@ void PatchBufferInstruction(IR::Block& block, IR::Inst& inst, Info& info, inst_info.dmft == AmdGpu::DataFormat::Format32_32 || inst_info.dmft == AmdGpu::DataFormat::Format32)); } - if (inst.GetOpcode() == IR::Opcode::ReadConstBuffer || - inst.GetOpcode() == IR::Opcode::ReadConstBufferU32) { + + if (IsReadConstBuffer(inst)) { return; } - // Calculate buffer address. - const u32 dword_stride = buffer.GetStrideElements(sizeof(u32)); - const u32 dword_offset = inst_info.inst_offset.Value() / sizeof(u32); - IR::U32 address = ir.Imm32(dword_offset); - if (inst_info.index_enable && inst_info.offset_enable) { - const IR::U32 offset{ir.CompositeExtract(inst.Arg(1), 1)}; - const IR::U32 index{ir.CompositeExtract(inst.Arg(1), 0)}; - address = ir.IAdd(ir.IMul(index, ir.Imm32(dword_stride)), address); - address = ir.IAdd(address, ir.ShiftRightLogical(offset, ir.Imm32(2))); - } else if (inst_info.index_enable) { - const IR::U32 index{inst.Arg(1)}; - address = ir.IAdd(ir.IMul(index, ir.Imm32(dword_stride)), address); - } else if (inst_info.offset_enable) { - const IR::U32 offset{inst.Arg(1)}; + + if (IsLoadBufferFormat(inst)) { + if (UseFP16(buffer.GetDataFmt(), buffer.GetNumberFmt())) { + info.uses_fp16 = true; + } + } else { + const u32 stride = buffer.GetStride(); + ASSERT_MSG(stride >= 4, "non-formatting load_buffer_* is not implemented for stride {}", + stride); + } + + IR::U32 address = ir.Imm32(inst_info.inst_offset.Value()); + if (inst_info.index_enable) { + const IR::U32 index = inst_info.offset_enable ? IR::U32{ir.CompositeExtract(inst.Arg(1), 0)} + : IR::U32{inst.Arg(1)}; + address = ir.IAdd(address, ir.IMul(index, ir.Imm32(buffer.GetStride()))); + } + if (inst_info.offset_enable) { + const IR::U32 offset = inst_info.index_enable ? IR::U32{ir.CompositeExtract(inst.Arg(1), 1)} + : IR::U32{inst.Arg(1)}; + address = ir.IAdd(address, offset); } inst.SetArg(1, address); } diff --git a/src/shader_recompiler/runtime_info.h b/src/shader_recompiler/runtime_info.h index 2b2103ca..8824e344 100644 --- a/src/shader_recompiler/runtime_info.h +++ b/src/shader_recompiler/runtime_info.h @@ -74,8 +74,7 @@ struct Info; struct BufferResource { u32 sgpr_base; u32 dword_offset; - u32 stride; - u32 num_records; + u32 length; IR::Type used_types; AmdGpu::Buffer inline_cbuf; bool is_storage{false}; diff --git a/src/video_core/amdgpu/pixel_format.cpp b/src/video_core/amdgpu/pixel_format.cpp index 5f6eb903..6618e72a 100644 --- a/src/video_core/amdgpu/pixel_format.cpp +++ b/src/video_core/amdgpu/pixel_format.cpp @@ -66,4 +66,110 @@ int NumBits(DataFormat format) { return num_bits_per_element[index]; } +static constexpr std::array component_bits = { + std::array{0, 0, 0, 0}, // 0 FormatInvalid + std::array{8, 0, 0, 0}, // 1 Format8 + std::array{16, 0, 0, 0}, // 2 Format16 + std::array{8, 8, 0, 0}, // 3 Format8_8 + std::array{32, 0, 0, 0}, // 4 Format32 + std::array{16, 16, 0, 0}, // 5 Format16_16 + std::array{10, 11, 11, 0}, // 6 Format10_11_11 + std::array{11, 11, 10, 0}, // 7 Format11_11_10 + std::array{10, 10, 10, 2}, // 8 Format10_10_10_2 + std::array{2, 10, 10, 10}, // 9 Format2_10_10_10 + std::array{8, 8, 8, 8}, // 10 Format8_8_8_8 + std::array{32, 32, 0, 0}, // 11 Format32_32 + std::array{16, 16, 16, 16}, // 12 Format16_16_16_16 + std::array{32, 32, 32, 0}, // 13 Format32_32_32 + std::array{32, 32, 32, 32}, // 14 Format32_32_32_32 + std::array{0, 0, 0, 0}, // 15 + std::array{5, 6, 5, 0}, // 16 Format5_6_5 + std::array{1, 5, 5, 5}, // 17 Format1_5_5_5 + std::array{5, 5, 5, 1}, // 18 Format5_5_5_1 + std::array{4, 4, 4, 4}, // 19 Format4_4_4_4 + std::array{8, 24, 0, 0}, // 20 Format8_24 + std::array{24, 8, 0, 0}, // 21 Format24_8 + std::array{24, 8, 0, 0}, // 22 FormatX24_8_32 + std::array{0, 0, 0, 0}, // 23 + std::array{0, 0, 0, 0}, // 24 + std::array{0, 0, 0, 0}, // 25 + std::array{0, 0, 0, 0}, // 26 + std::array{0, 0, 0, 0}, // 27 + std::array{0, 0, 0, 0}, // 28 + std::array{0, 0, 0, 0}, // 29 + std::array{0, 0, 0, 0}, // 30 + std::array{0, 0, 0, 0}, // 31 + std::array{0, 0, 0, 0}, // 32 FormatGB_GR + std::array{0, 0, 0, 0}, // 33 FormatBG_RG + std::array{0, 0, 0, 0}, // 34 Format5_9_9_9 + std::array{0, 0, 0, 0}, // 35 FormatBc1 + std::array{0, 0, 0, 0}, // 36 FormatBc2 + std::array{0, 0, 0, 0}, // 37 FormatBc3 + std::array{0, 0, 0, 0}, // 38 FormatBc4 + std::array{0, 0, 0, 0}, // 39 FormatBc5 + std::array{0, 0, 0, 0}, // 40 FormatBc6 + std::array{0, 0, 0, 0}, // 41 FormatBc7 +}; + +u32 ComponentBits(DataFormat format, u32 comp) { + const u32 index = static_cast(format); + if (index >= component_bits.size() || comp >= 4) { + return 0; + } + return component_bits[index][comp]; +} + +static constexpr std::array component_offset = { + std::array{-1, -1, -1, -1}, // 0 FormatInvalid + std::array{0, -1, -1, -1}, // 1 Format8 + std::array{0, -1, -1, -1}, // 2 Format16 + std::array{0, 8, -1, -1}, // 3 Format8_8 + std::array{0, -1, -1, -1}, // 4 Format32 + std::array{0, 16, -1, -1}, // 5 Format16_16 + std::array{0, 10, 21, -1}, // 6 Format10_11_11 + std::array{0, 11, 22, -1}, // 7 Format11_11_10 + std::array{0, 10, 20, 30}, // 8 Format10_10_10_2 + std::array{0, 2, 12, 22}, // 9 Format2_10_10_10 + std::array{0, 8, 16, 24}, // 10 Format8_8_8_8 + std::array{0, 32, -1, -1}, // 11 Format32_32 + std::array{0, 16, 32, 48}, // 12 Format16_16_16_16 + std::array{0, 32, 64, -1}, // 13 Format32_32_32 + std::array{0, 32, 64, 96}, // 14 Format32_32_32_32 + std::array{-1, -1, -1, -1}, // 15 + std::array{0, 5, 11, -1}, // 16 Format5_6_5 + std::array{0, 1, 6, 11}, // 17 Format1_5_5_5 + std::array{0, 5, 10, 15}, // 18 Format5_5_5_1 + std::array{0, 4, 8, 12}, // 19 Format4_4_4_4 + std::array{0, 8, -1, -1}, // 20 Format8_24 + std::array{0, 24, -1, -1}, // 21 Format24_8 + std::array{0, 24, -1, -1}, // 22 FormatX24_8_32 + std::array{-1, -1, -1, -1}, // 23 + std::array{-1, -1, -1, -1}, // 24 + std::array{-1, -1, -1, -1}, // 25 + std::array{-1, -1, -1, -1}, // 26 + std::array{-1, -1, -1, -1}, // 27 + std::array{-1, -1, -1, -1}, // 28 + std::array{-1, -1, -1, -1}, // 29 + std::array{-1, -1, -1, -1}, // 30 + std::array{-1, -1, -1, -1}, // 31 + std::array{-1, -1, -1, -1}, // 32 FormatGB_GR + std::array{-1, -1, -1, -1}, // 33 FormatBG_RG + std::array{-1, -1, -1, -1}, // 34 Format5_9_9_9 + std::array{-1, -1, -1, -1}, // 35 FormatBc1 + std::array{-1, -1, -1, -1}, // 36 FormatBc2 + std::array{-1, -1, -1, -1}, // 37 FormatBc3 + std::array{-1, -1, -1, -1}, // 38 FormatBc4 + std::array{-1, -1, -1, -1}, // 39 FormatBc5 + std::array{-1, -1, -1, -1}, // 40 FormatBc6 + std::array{-1, -1, -1, -1}, // 41 FormatBc7 +}; + +s32 ComponentOffset(DataFormat format, u32 comp) { + const u32 index = static_cast(format); + if (index >= component_offset.size() || comp >= 4) { + return -1; + } + return component_offset[index][comp]; +} + } // namespace AmdGpu diff --git a/src/video_core/amdgpu/pixel_format.h b/src/video_core/amdgpu/pixel_format.h index 22d102af..2a38c5a0 100644 --- a/src/video_core/amdgpu/pixel_format.h +++ b/src/video_core/amdgpu/pixel_format.h @@ -65,6 +65,8 @@ enum class NumberFormat : u32 { int NumComponents(DataFormat format); int NumBits(DataFormat format); +u32 ComponentBits(DataFormat format, u32 comp); +s32 ComponentOffset(DataFormat format, u32 comp); } // namespace AmdGpu diff --git a/src/video_core/amdgpu/resource.h b/src/video_core/amdgpu/resource.h index ba3de154..1247c025 100644 --- a/src/video_core/amdgpu/resource.h +++ b/src/video_core/amdgpu/resource.h @@ -62,14 +62,6 @@ struct Buffer { return stride == 0 ? 1U : stride; } - u32 GetStrideElements(u32 element_size) const noexcept { - if (stride == 0) { - return 1U; - } - ASSERT(stride % element_size == 0); - return stride / element_size; - } - u32 GetSize() const noexcept { return GetStride() * num_records; } From 815ce11d2b679627283e1d37c48b10d019b1b051 Mon Sep 17 00:00:00 2001 From: IndecisiveTurtle <47210458+raphaelthegreat@users.noreply.github.com> Date: Wed, 17 Jul 2024 14:46:37 +0300 Subject: [PATCH 22/34] fs: Defeat games with double slashes --- src/core/file_sys/fs.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/core/file_sys/fs.cpp b/src/core/file_sys/fs.cpp index c6bdf7ad..2f57c9f3 100644 --- a/src/core/file_sys/fs.cpp +++ b/src/core/file_sys/fs.cpp @@ -37,7 +37,11 @@ std::filesystem::path MntPoints::GetHostPath(const std::string& guest_directory) } // Remove device (e.g /app0) from path to retrieve relative path. - const u32 pos = mount->mount.size() + 1; + u32 pos = mount->mount.size() + 1; + // Evil games like Turok2 pass double slashes e.g /app0//game.kpf + if (guest_directory[pos] == '/') { + pos++; + } const auto rel_path = std::string_view(guest_directory).substr(pos); const auto host_path = mount->host_path / rel_path; if (!NeedsCaseInsensiveSearch) { From c64694c6734a5158faacda4e2c49669383aee611 Mon Sep 17 00:00:00 2001 From: IndecisiveTurtle <47210458+raphaelthegreat@users.noreply.github.com> Date: Wed, 17 Jul 2024 14:48:30 +0300 Subject: [PATCH 23/34] gnmdriver: Fix incorrect trailing nop insertion in indirect draws --- src/core/libraries/gnmdriver/gnmdriver.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/core/libraries/gnmdriver/gnmdriver.cpp b/src/core/libraries/gnmdriver/gnmdriver.cpp index 26b71eb4..37403d13 100644 --- a/src/core/libraries/gnmdriver/gnmdriver.cpp +++ b/src/core/libraries/gnmdriver/gnmdriver.cpp @@ -649,6 +649,7 @@ s32 PS4_SYSV_ABI sceGnmDrawIndexIndirect(u32* cmdbuf, u32 size, u32 data_offset, cmdbuf[2] = instance_vgpr_offset == 0 ? 0 : (instance_vgpr_offset & 0xffffu) + sgpr_offset; cmdbuf[3] = 0; + cmdbuf += 4; WriteTrailingNop<3>(cmdbuf); return ORBIS_OK; } @@ -704,6 +705,7 @@ s32 PS4_SYSV_ABI sceGnmDrawIndirect(u32* cmdbuf, u32 size, u32 data_offset, u32 cmdbuf[2] = instance_vgpr_offset == 0 ? 0 : (instance_vgpr_offset & 0xffffu) + sgpr_offset; cmdbuf[3] = 2; // auto index + cmdbuf += 4; WriteTrailingNop<3>(cmdbuf); return ORBIS_OK; } From b6f011c8f68c2919725ed992d3cd6219c483746b Mon Sep 17 00:00:00 2001 From: IndecisiveTurtle <47210458+raphaelthegreat@users.noreply.github.com> Date: Wed, 17 Jul 2024 14:50:26 +0300 Subject: [PATCH 24/34] event_queue: Add clear flag to events and don't return error --- src/core/libraries/gnmdriver/gnmdriver.cpp | 2 +- src/core/libraries/kernel/event_queue.cpp | 2 +- src/core/libraries/videoout/video_out.cpp | 8 ++++++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/core/libraries/gnmdriver/gnmdriver.cpp b/src/core/libraries/gnmdriver/gnmdriver.cpp index 37403d13..77a13800 100644 --- a/src/core/libraries/gnmdriver/gnmdriver.cpp +++ b/src/core/libraries/gnmdriver/gnmdriver.cpp @@ -346,7 +346,7 @@ s32 PS4_SYSV_ABI sceGnmAddEqEvent(SceKernelEqueue eq, u64 id, void* udata) { EqueueEvent kernel_event{}; kernel_event.event.ident = id; kernel_event.event.filter = SceKernelEvent::Filter::GraphicsCore; - kernel_event.event.flags = SceKernelEvent::Flags::Add; + kernel_event.event.flags = SceKernelEvent::Flags::Clear; kernel_event.event.fflags = 0; kernel_event.event.data = id; kernel_event.event.udata = udata; diff --git a/src/core/libraries/kernel/event_queue.cpp b/src/core/libraries/kernel/event_queue.cpp index 18561d6b..7d5163cf 100644 --- a/src/core/libraries/kernel/event_queue.cpp +++ b/src/core/libraries/kernel/event_queue.cpp @@ -94,7 +94,7 @@ int EqueueInternal::GetTriggeredEvents(SceKernelEvent* ev, int num) { for (auto& event : m_events) { if (event.IsTriggered()) { - if (ev->flags & SceKernelEvent::Flags::Clear) { + if (event.event.flags & SceKernelEvent::Flags::Clear) { event.Reset(); } diff --git a/src/core/libraries/videoout/video_out.cpp b/src/core/libraries/videoout/video_out.cpp index 51cfcf4c..826b2247 100644 --- a/src/core/libraries/videoout/video_out.cpp +++ b/src/core/libraries/videoout/video_out.cpp @@ -50,13 +50,15 @@ s32 PS4_SYSV_ABI sceVideoOutAddFlipEvent(Kernel::SceKernelEqueue eq, s32 handle, Kernel::EqueueEvent event{}; event.event.ident = SCE_VIDEO_OUT_EVENT_FLIP; event.event.filter = Kernel::SceKernelEvent::Filter::VideoOut; + event.event.flags = Kernel::SceKernelEvent::Flags::Clear; event.event.udata = udata; event.event.fflags = 0; event.event.data = 0; event.data = port; + eq->AddEvent(event); port->flip_events.push_back(eq); - return eq->AddEvent(event); + return ORBIS_OK; } s32 PS4_SYSV_ABI sceVideoOutAddVblankEvent(Kernel::SceKernelEqueue eq, s32 handle, void* udata) { @@ -74,13 +76,15 @@ s32 PS4_SYSV_ABI sceVideoOutAddVblankEvent(Kernel::SceKernelEqueue eq, s32 handl Kernel::EqueueEvent event{}; event.event.ident = SCE_VIDEO_OUT_EVENT_VBLANK; event.event.filter = Kernel::SceKernelEvent::Filter::VideoOut; + event.event.flags = Kernel::SceKernelEvent::Flags::Clear; event.event.udata = udata; event.event.fflags = 0; event.event.data = 0; event.data = port; + eq->AddEvent(event); port->vblank_events.push_back(eq); - return eq->AddEvent(event); + return ORBIS_OK; } s32 PS4_SYSV_ABI sceVideoOutRegisterBuffers(s32 handle, s32 startIndex, void* const* addresses, From 0408d8596def4f6a2feea5b7f90ab94d768fdbb2 Mon Sep 17 00:00:00 2001 From: IndecisiveTurtle <47210458+raphaelthegreat@users.noreply.github.com> Date: Wed, 17 Jul 2024 15:37:32 +0300 Subject: [PATCH 25/34] emulator: Add libSceFiber and simplify logic --- src/emulator.cpp | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/emulator.cpp b/src/emulator.cpp index a60aea1b..3f674cf6 100644 --- a/src/emulator.cpp +++ b/src/emulator.cpp @@ -148,39 +148,35 @@ void Emulator::Run(const std::filesystem::path& file) { } void Emulator::LoadSystemModules(const std::filesystem::path& file) { - constexpr std::array ModulesToLoad{ + constexpr std::array ModulesToLoad{ {{"libSceNgs2.sprx", nullptr}, + {"libSceFiber.sprx", nullptr}, {"libSceLibcInternal.sprx", &Libraries::LibcInternal::RegisterlibSceLibcInternal}, {"libSceDiscMap.sprx", &Libraries::DiscMap::RegisterlibSceDiscMap}, {"libSceRtc.sprx", &Libraries::Rtc::RegisterlibSceRtc}, {"libSceJpegEnc.sprx", nullptr}, - {"libSceJson2.sprx", nullptr}}}; + {"libSceJson2.sprx", nullptr}}, + }; std::vector found_modules; const auto& sys_module_path = Common::FS::GetUserPath(Common::FS::PathType::SysModuleDir); for (const auto& entry : std::filesystem::directory_iterator(sys_module_path)) { found_modules.push_back(entry.path()); } - for (auto it : ModulesToLoad) { - bool found = false; - std::filesystem::path foundpath; - for (auto f : found_modules) { - if (f.filename().string() == it.module_name) { - found = true; - foundpath = f; - break; - } + for (const auto& [module_name, init_func] : ModulesToLoad) { + const auto it = std::ranges::find_if(found_modules, [&](const auto& path) { + return path.filename() == module_name; + }); + if (it != found_modules.end()) { + LOG_INFO(Loader, "Loading {}", it->string()); + linker->LoadModule(*it); + continue; } - if (found) { - LOG_INFO(Loader, "Loading {}", foundpath.string().c_str()); - linker->LoadModule(foundpath); + if (init_func) { + LOG_INFO(Loader, "Can't Load {} switching to HLE", module_name); + init_func(&linker->GetHLESymbols()); } else { - if (it.callback != nullptr) { - LOG_INFO(Loader, "Can't Load {} switching to HLE", it.module_name); - it.callback(&linker->GetHLESymbols()); - } else { - LOG_INFO(Loader, "No HLE available for {} module", it.module_name); - } + LOG_INFO(Loader, "No HLE available for {} module", module_name); } } } From b89941fbd9b0045888c32f9bf0260c1d6357ab0e Mon Sep 17 00:00:00 2001 From: IndecisiveTurtle <47210458+raphaelthegreat@users.noreply.github.com> Date: Wed, 17 Jul 2024 15:44:20 +0300 Subject: [PATCH 26/34] emulator: Add libSceUlt too --- src/emulator.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/emulator.cpp b/src/emulator.cpp index 3f674cf6..9eafe7a6 100644 --- a/src/emulator.cpp +++ b/src/emulator.cpp @@ -148,9 +148,10 @@ void Emulator::Run(const std::filesystem::path& file) { } void Emulator::LoadSystemModules(const std::filesystem::path& file) { - constexpr std::array ModulesToLoad{ + constexpr std::array ModulesToLoad{ {{"libSceNgs2.sprx", nullptr}, {"libSceFiber.sprx", nullptr}, + {"libSceUlt.sprx", nullptr}, {"libSceLibcInternal.sprx", &Libraries::LibcInternal::RegisterlibSceLibcInternal}, {"libSceDiscMap.sprx", &Libraries::DiscMap::RegisterlibSceDiscMap}, {"libSceRtc.sprx", &Libraries::Rtc::RegisterlibSceRtc}, From 01dc7f5bfca2550cd69cd04f0be21fca253f9ba9 Mon Sep 17 00:00:00 2001 From: IndecisiveTurtle <47210458+raphaelthegreat@users.noreply.github.com> Date: Wed, 17 Jul 2024 15:52:40 +0300 Subject: [PATCH 27/34] Add comments --- src/core/libraries/gnmdriver/gnmdriver.cpp | 1 + src/core/libraries/videoout/video_out.cpp | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/core/libraries/gnmdriver/gnmdriver.cpp b/src/core/libraries/gnmdriver/gnmdriver.cpp index 77a13800..ead4ff23 100644 --- a/src/core/libraries/gnmdriver/gnmdriver.cpp +++ b/src/core/libraries/gnmdriver/gnmdriver.cpp @@ -346,6 +346,7 @@ s32 PS4_SYSV_ABI sceGnmAddEqEvent(SceKernelEqueue eq, u64 id, void* udata) { EqueueEvent kernel_event{}; kernel_event.event.ident = id; kernel_event.event.filter = SceKernelEvent::Filter::GraphicsCore; + // The library only sets EV_ADD but it is suspected the kernel driver forces EV_CLEAR kernel_event.event.flags = SceKernelEvent::Flags::Clear; kernel_event.event.fflags = 0; kernel_event.event.data = id; diff --git a/src/core/libraries/videoout/video_out.cpp b/src/core/libraries/videoout/video_out.cpp index 826b2247..8fbd69c4 100644 --- a/src/core/libraries/videoout/video_out.cpp +++ b/src/core/libraries/videoout/video_out.cpp @@ -50,6 +50,7 @@ s32 PS4_SYSV_ABI sceVideoOutAddFlipEvent(Kernel::SceKernelEqueue eq, s32 handle, Kernel::EqueueEvent event{}; event.event.ident = SCE_VIDEO_OUT_EVENT_FLIP; event.event.filter = Kernel::SceKernelEvent::Filter::VideoOut; + // The library only sets EV_ADD but kernel driver forces EV_CLEAR event.event.flags = Kernel::SceKernelEvent::Flags::Clear; event.event.udata = udata; event.event.fflags = 0; @@ -76,6 +77,7 @@ s32 PS4_SYSV_ABI sceVideoOutAddVblankEvent(Kernel::SceKernelEqueue eq, s32 handl Kernel::EqueueEvent event{}; event.event.ident = SCE_VIDEO_OUT_EVENT_VBLANK; event.event.filter = Kernel::SceKernelEvent::Filter::VideoOut; + // The library only sets EV_ADD but kernel driver forces EV_CLEAR event.event.flags = Kernel::SceKernelEvent::Flags::Clear; event.event.udata = udata; event.event.fflags = 0; From fcbaec51df8d75b7ba23f30534235768956777ce Mon Sep 17 00:00:00 2001 From: IndecisiveTurtle <47210458+raphaelthegreat@users.noreply.github.com> Date: Wed, 17 Jul 2024 16:19:08 +0300 Subject: [PATCH 28/34] liverpool_to_vk: Add more surface formats --- src/video_core/renderer_vulkan/liverpool_to_vk.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/video_core/renderer_vulkan/liverpool_to_vk.cpp b/src/video_core/renderer_vulkan/liverpool_to_vk.cpp index 4280e6e6..0cbe0de8 100644 --- a/src/video_core/renderer_vulkan/liverpool_to_vk.cpp +++ b/src/video_core/renderer_vulkan/liverpool_to_vk.cpp @@ -441,6 +441,18 @@ vk::Format SurfaceFormat(AmdGpu::DataFormat data_format, AmdGpu::NumberFormat nu num_format == AmdGpu::NumberFormat::Unorm) { return vk::Format::eR16G16B16A16Unorm; } + if (data_format == AmdGpu::DataFormat::Format8 && + num_format == AmdGpu::NumberFormat::Uint) { + return vk::Format::eR8Uint; + } + if (data_format == AmdGpu::DataFormat::Format16_16_16_16 && + num_format == AmdGpu::NumberFormat::SnormNz) { + return vk::Format::eR16G16B16A16Snorm; + } + if (data_format == AmdGpu::DataFormat::FormatBc6 && + num_format == AmdGpu::NumberFormat::Unorm) { + return vk::Format::eBc6HUfloatBlock; + } UNREACHABLE_MSG("Unknown data_format={} and num_format={}", u32(data_format), u32(num_format)); } From 53fb73e95fbbe5376208f556161cbe5b6cdc5de2 Mon Sep 17 00:00:00 2001 From: IndecisiveTurtle <47210458+raphaelthegreat@users.noreply.github.com> Date: Wed, 17 Jul 2024 16:22:04 +0300 Subject: [PATCH 29/34] rasterizer: Bump staging to 1GB * Less chances of crashing --- src/video_core/renderer_vulkan/vk_rasterizer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/video_core/renderer_vulkan/vk_rasterizer.cpp b/src/video_core/renderer_vulkan/vk_rasterizer.cpp index d0944fcc..fff9bc33 100644 --- a/src/video_core/renderer_vulkan/vk_rasterizer.cpp +++ b/src/video_core/renderer_vulkan/vk_rasterizer.cpp @@ -23,7 +23,7 @@ Rasterizer::Rasterizer(const Instance& instance_, Scheduler& scheduler_, : instance{instance_}, scheduler{scheduler_}, texture_cache{texture_cache_}, liverpool{liverpool_}, memory{Core::Memory::Instance()}, pipeline_cache{instance, scheduler, liverpool}, - vertex_index_buffer{instance, scheduler, VertexIndexFlags, 512_MB, BufferType::Upload} { + vertex_index_buffer{instance, scheduler, VertexIndexFlags, 1_GB, BufferType::Upload} { if (!Config::nullGpu()) { liverpool->BindRasterizer(this); } From cd009cfec6719b3edf5616736b0efd85225ecff5 Mon Sep 17 00:00:00 2001 From: IndecisiveTurtle <47210458+raphaelthegreat@users.noreply.github.com> Date: Wed, 17 Jul 2024 16:49:45 +0300 Subject: [PATCH 30/34] shader_recompiler: Normal gathers --- .../backend/spirv/emit_spirv_image.cpp | 35 +++++++++++++------ .../backend/spirv/emit_spirv_instructions.h | 6 ++-- .../frontend/translate/translate.cpp | 2 ++ .../frontend/translate/vector_memory.cpp | 1 + .../ir/passes/resource_tracking_pass.cpp | 4 ++- src/shader_recompiler/ir/reg.h | 1 + .../renderer_vulkan/liverpool_to_vk.cpp | 4 +++ 7 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_image.cpp b/src/shader_recompiler/backend/spirv/emit_spirv_image.cpp index 2d35b97c..b7249100 100644 --- a/src/shader_recompiler/backend/spirv/emit_spirv_image.cpp +++ b/src/shader_recompiler/backend/spirv/emit_spirv_image.cpp @@ -9,6 +9,9 @@ namespace Shader::Backend::SPIRV { struct ImageOperands { void Add(spv::ImageOperandsMask new_mask, Id value) { + if (!Sirit::ValidId(value)) { + return; + } mask = static_cast(static_cast(mask) | static_cast(new_mask)); operands.push_back(value); @@ -25,9 +28,7 @@ Id EmitImageSampleImplicitLod(EmitContext& ctx, IR::Inst* inst, u32 handle, Id c const Id sampler = ctx.OpLoad(ctx.sampler_type, ctx.samplers[handle >> 16]); const Id sampled_image = ctx.OpSampledImage(texture.sampled_type, image, sampler); ImageOperands operands; - if (Sirit::ValidId(offset)) { - operands.Add(spv::ImageOperandsMask::ConstOffset, offset); - } + operands.Add(spv::ImageOperandsMask::Offset, offset); return ctx.OpImageSampleImplicitLod(ctx.F32[4], sampled_image, coords, operands.mask, operands.operands); } @@ -61,18 +62,30 @@ Id EmitImageSampleDrefExplicitLod(EmitContext& ctx, IR::Inst* inst, u32 handle, spv::ImageOperandsMask::Lod, ctx.ConstF32(0.f)); } -Id EmitImageGather(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords, - const IR::Value& offset, const IR::Value& offset2) { - UNREACHABLE_MSG("SPIR-V Instruction"); -} - -Id EmitImageGatherDref(EmitContext& ctx, IR::Inst* inst, u32 handle, Id coords, - const IR::Value& offset, const IR::Value& offset2, Id dref) { +Id EmitImageGather(EmitContext& ctx, IR::Inst* inst, u32 handle, Id coords, + Id offset, Id offset2) { const auto& texture = ctx.images[handle & 0xFFFF]; const Id image = ctx.OpLoad(texture.image_type, texture.id); const Id sampler = ctx.OpLoad(ctx.sampler_type, ctx.samplers[handle >> 16]); const Id sampled_image = ctx.OpSampledImage(texture.sampled_type, image, sampler); - return ctx.OpImageDrefGather(ctx.F32[4], sampled_image, coords, dref); + const u32 comp = inst->Flags().gather_comp.Value(); + ImageOperands operands; + operands.Add(spv::ImageOperandsMask::Offset, offset); + operands.Add(spv::ImageOperandsMask::Lod, ctx.ConstF32(0.f)); + return ctx.OpImageGather(ctx.F32[4], sampled_image, coords, ctx.ConstU32(comp), + operands.mask, operands.operands); +} + +Id EmitImageGatherDref(EmitContext& ctx, IR::Inst* inst, u32 handle, Id coords, + Id offset, Id offset2, Id dref) { + const auto& texture = ctx.images[handle & 0xFFFF]; + const Id image = ctx.OpLoad(texture.image_type, texture.id); + const Id sampler = ctx.OpLoad(ctx.sampler_type, ctx.samplers[handle >> 16]); + const Id sampled_image = ctx.OpSampledImage(texture.sampled_type, image, sampler); + ImageOperands operands; + operands.Add(spv::ImageOperandsMask::Offset, offset); + return ctx.OpImageDrefGather(ctx.F32[4], sampled_image, coords, dref, + operands.mask, operands.operands); } Id EmitImageFetch(EmitContext& ctx, IR::Inst* inst, u32 handle, Id coords, Id offset, Id lod, diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h b/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h index f43ea3b3..1432041c 100644 --- a/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h +++ b/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h @@ -358,10 +358,10 @@ Id EmitImageSampleDrefImplicitLod(EmitContext& ctx, IR::Inst* inst, u32 handle, Id bias_lc, const IR::Value& offset); Id EmitImageSampleDrefExplicitLod(EmitContext& ctx, IR::Inst* inst, u32 handle, Id coords, Id dref, Id bias_lc, Id offset); -Id EmitImageGather(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords, - const IR::Value& offset, const IR::Value& offset2); +Id EmitImageGather(EmitContext& ctx, IR::Inst* inst, u32 handle, Id coords, + Id offset, Id offset2); Id EmitImageGatherDref(EmitContext& ctx, IR::Inst* inst, u32 handle, Id coords, - const IR::Value& offset, const IR::Value& offset2, Id dref); + Id offset, Id offset2, Id dref); Id EmitImageFetch(EmitContext& ctx, IR::Inst* inst, u32 handle, Id coords, Id offset, Id lod, Id ms); Id EmitImageQueryDimensions(EmitContext& ctx, IR::Inst* inst, u32 handle, Id lod, bool skip_mips); diff --git a/src/shader_recompiler/frontend/translate/translate.cpp b/src/shader_recompiler/frontend/translate/translate.cpp index bc2e0bf2..6e5f7f8b 100644 --- a/src/shader_recompiler/frontend/translate/translate.cpp +++ b/src/shader_recompiler/frontend/translate/translate.cpp @@ -456,6 +456,8 @@ void Translate(IR::Block* block, u32 block_base, std::span inst_l translator.IMAGE_GET_LOD(inst); break; case Opcode::IMAGE_GATHER4_C: + case Opcode::IMAGE_GATHER4_LZ: + case Opcode::IMAGE_GATHER4_LZ_O: translator.IMAGE_GATHER(inst); break; case Opcode::IMAGE_STORE: diff --git a/src/shader_recompiler/frontend/translate/vector_memory.cpp b/src/shader_recompiler/frontend/translate/vector_memory.cpp index 1ddee523..f4383c61 100644 --- a/src/shader_recompiler/frontend/translate/vector_memory.cpp +++ b/src/shader_recompiler/frontend/translate/vector_memory.cpp @@ -158,6 +158,7 @@ void Translator::IMAGE_GATHER(const GcnInst& inst) { info.has_lod_clamp.Assign(flags.test(MimgModifier::LodClamp)); info.force_level0.Assign(flags.test(MimgModifier::Level0)); info.explicit_lod.Assign(explicit_lod); + info.gather_comp.Assign(std::bit_width(mimg.dmask) - 1); // Issue IR instruction, leaving unknown fields blank to patch later. const IR::Value texel = [&]() -> IR::Value { diff --git a/src/shader_recompiler/ir/passes/resource_tracking_pass.cpp b/src/shader_recompiler/ir/passes/resource_tracking_pass.cpp index f58b4d96..6526ece6 100644 --- a/src/shader_recompiler/ir/passes/resource_tracking_pass.cpp +++ b/src/shader_recompiler/ir/passes/resource_tracking_pass.cpp @@ -543,7 +543,9 @@ void PatchImageInstruction(IR::Block& block, IR::Inst& inst, Info& info, Descrip if (inst_info.has_offset) { // The offsets are six-bit signed integers: X=[5:0], Y=[13:8], and Z=[21:16]. - const u32 arg_pos = inst_info.is_depth ? 4 : 3; + const bool is_gather = inst.GetOpcode() == IR::Opcode::ImageGather || + inst.GetOpcode() == IR::Opcode::ImageGatherDref; + const u32 arg_pos = is_gather ? 2 : (inst_info.is_depth ? 4 : 3); const IR::Value arg = inst.Arg(arg_pos); ASSERT_MSG(arg.Type() == IR::Type::U32, "Unexpected offset type"); const auto sign_ext = [&](u32 value) { return ir.Imm32(s32(value << 24) >> 24); }; diff --git a/src/shader_recompiler/ir/reg.h b/src/shader_recompiler/ir/reg.h index ae38ecf3..d9e9b030 100644 --- a/src/shader_recompiler/ir/reg.h +++ b/src/shader_recompiler/ir/reg.h @@ -39,6 +39,7 @@ union TextureInstInfo { BitField<3, 1, u32> force_level0; BitField<4, 1, u32> explicit_lod; BitField<5, 1, u32> has_offset; + BitField<6, 2, u32> gather_comp; }; union BufferInstInfo { diff --git a/src/video_core/renderer_vulkan/liverpool_to_vk.cpp b/src/video_core/renderer_vulkan/liverpool_to_vk.cpp index 0cbe0de8..d9ca8b1f 100644 --- a/src/video_core/renderer_vulkan/liverpool_to_vk.cpp +++ b/src/video_core/renderer_vulkan/liverpool_to_vk.cpp @@ -420,6 +420,10 @@ vk::Format SurfaceFormat(AmdGpu::DataFormat data_format, AmdGpu::NumberFormat nu num_format == AmdGpu::NumberFormat::Uint) { return vk::Format::eR32G32B32A32Uint; } + if (data_format == AmdGpu::DataFormat::Format32_32_32_32 && + num_format == AmdGpu::NumberFormat::Sint) { + return vk::Format::eR32G32B32A32Sint; + } if (data_format == AmdGpu::DataFormat::Format8 && num_format == AmdGpu::NumberFormat::Sint) { return vk::Format::eR8Sint; } From 1208661d649df194f5af3a429b42626ff975d65c Mon Sep 17 00:00:00 2001 From: IndecisiveTurtle <47210458+raphaelthegreat@users.noreply.github.com> Date: Wed, 17 Jul 2024 17:06:09 +0300 Subject: [PATCH 31/34] Remove bc6 --- src/video_core/renderer_vulkan/liverpool_to_vk.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/video_core/renderer_vulkan/liverpool_to_vk.cpp b/src/video_core/renderer_vulkan/liverpool_to_vk.cpp index d9ca8b1f..bf520874 100644 --- a/src/video_core/renderer_vulkan/liverpool_to_vk.cpp +++ b/src/video_core/renderer_vulkan/liverpool_to_vk.cpp @@ -453,10 +453,6 @@ vk::Format SurfaceFormat(AmdGpu::DataFormat data_format, AmdGpu::NumberFormat nu num_format == AmdGpu::NumberFormat::SnormNz) { return vk::Format::eR16G16B16A16Snorm; } - if (data_format == AmdGpu::DataFormat::FormatBc6 && - num_format == AmdGpu::NumberFormat::Unorm) { - return vk::Format::eBc6HUfloatBlock; - } UNREACHABLE_MSG("Unknown data_format={} and num_format={}", u32(data_format), u32(num_format)); } From 439c0be9a6af818358801a298a23a356ead6688c Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Wed, 17 Jul 2024 17:57:54 +0300 Subject: [PATCH 32/34] clang format fix --- src/emulator.cpp | 5 ++--- .../backend/spirv/emit_spirv_image.cpp | 15 +++++++-------- .../backend/spirv/emit_spirv_instructions.h | 7 +++---- .../renderer_vulkan/liverpool_to_vk.cpp | 3 +-- 4 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/emulator.cpp b/src/emulator.cpp index 9eafe7a6..91d92cd6 100644 --- a/src/emulator.cpp +++ b/src/emulator.cpp @@ -165,9 +165,8 @@ void Emulator::LoadSystemModules(const std::filesystem::path& file) { found_modules.push_back(entry.path()); } for (const auto& [module_name, init_func] : ModulesToLoad) { - const auto it = std::ranges::find_if(found_modules, [&](const auto& path) { - return path.filename() == module_name; - }); + const auto it = std::ranges::find_if( + found_modules, [&](const auto& path) { return path.filename() == module_name; }); if (it != found_modules.end()) { LOG_INFO(Loader, "Loading {}", it->string()); linker->LoadModule(*it); diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_image.cpp b/src/shader_recompiler/backend/spirv/emit_spirv_image.cpp index b7249100..17def57a 100644 --- a/src/shader_recompiler/backend/spirv/emit_spirv_image.cpp +++ b/src/shader_recompiler/backend/spirv/emit_spirv_image.cpp @@ -62,8 +62,7 @@ Id EmitImageSampleDrefExplicitLod(EmitContext& ctx, IR::Inst* inst, u32 handle, spv::ImageOperandsMask::Lod, ctx.ConstF32(0.f)); } -Id EmitImageGather(EmitContext& ctx, IR::Inst* inst, u32 handle, Id coords, - Id offset, Id offset2) { +Id EmitImageGather(EmitContext& ctx, IR::Inst* inst, u32 handle, Id coords, Id offset, Id offset2) { const auto& texture = ctx.images[handle & 0xFFFF]; const Id image = ctx.OpLoad(texture.image_type, texture.id); const Id sampler = ctx.OpLoad(ctx.sampler_type, ctx.samplers[handle >> 16]); @@ -72,20 +71,20 @@ Id EmitImageGather(EmitContext& ctx, IR::Inst* inst, u32 handle, Id coords, ImageOperands operands; operands.Add(spv::ImageOperandsMask::Offset, offset); operands.Add(spv::ImageOperandsMask::Lod, ctx.ConstF32(0.f)); - return ctx.OpImageGather(ctx.F32[4], sampled_image, coords, ctx.ConstU32(comp), - operands.mask, operands.operands); + return ctx.OpImageGather(ctx.F32[4], sampled_image, coords, ctx.ConstU32(comp), operands.mask, + operands.operands); } -Id EmitImageGatherDref(EmitContext& ctx, IR::Inst* inst, u32 handle, Id coords, - Id offset, Id offset2, Id dref) { +Id EmitImageGatherDref(EmitContext& ctx, IR::Inst* inst, u32 handle, Id coords, Id offset, + Id offset2, Id dref) { const auto& texture = ctx.images[handle & 0xFFFF]; const Id image = ctx.OpLoad(texture.image_type, texture.id); const Id sampler = ctx.OpLoad(ctx.sampler_type, ctx.samplers[handle >> 16]); const Id sampled_image = ctx.OpSampledImage(texture.sampled_type, image, sampler); ImageOperands operands; operands.Add(spv::ImageOperandsMask::Offset, offset); - return ctx.OpImageDrefGather(ctx.F32[4], sampled_image, coords, dref, - operands.mask, operands.operands); + return ctx.OpImageDrefGather(ctx.F32[4], sampled_image, coords, dref, operands.mask, + operands.operands); } Id EmitImageFetch(EmitContext& ctx, IR::Inst* inst, u32 handle, Id coords, Id offset, Id lod, diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h b/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h index 1432041c..e2b411e4 100644 --- a/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h +++ b/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h @@ -358,10 +358,9 @@ Id EmitImageSampleDrefImplicitLod(EmitContext& ctx, IR::Inst* inst, u32 handle, Id bias_lc, const IR::Value& offset); Id EmitImageSampleDrefExplicitLod(EmitContext& ctx, IR::Inst* inst, u32 handle, Id coords, Id dref, Id bias_lc, Id offset); -Id EmitImageGather(EmitContext& ctx, IR::Inst* inst, u32 handle, Id coords, - Id offset, Id offset2); -Id EmitImageGatherDref(EmitContext& ctx, IR::Inst* inst, u32 handle, Id coords, - Id offset, Id offset2, Id dref); +Id EmitImageGather(EmitContext& ctx, IR::Inst* inst, u32 handle, Id coords, Id offset, Id offset2); +Id EmitImageGatherDref(EmitContext& ctx, IR::Inst* inst, u32 handle, Id coords, Id offset, + Id offset2, Id dref); Id EmitImageFetch(EmitContext& ctx, IR::Inst* inst, u32 handle, Id coords, Id offset, Id lod, Id ms); Id EmitImageQueryDimensions(EmitContext& ctx, IR::Inst* inst, u32 handle, Id lod, bool skip_mips); diff --git a/src/video_core/renderer_vulkan/liverpool_to_vk.cpp b/src/video_core/renderer_vulkan/liverpool_to_vk.cpp index bf520874..f361fcad 100644 --- a/src/video_core/renderer_vulkan/liverpool_to_vk.cpp +++ b/src/video_core/renderer_vulkan/liverpool_to_vk.cpp @@ -445,8 +445,7 @@ vk::Format SurfaceFormat(AmdGpu::DataFormat data_format, AmdGpu::NumberFormat nu num_format == AmdGpu::NumberFormat::Unorm) { return vk::Format::eR16G16B16A16Unorm; } - if (data_format == AmdGpu::DataFormat::Format8 && - num_format == AmdGpu::NumberFormat::Uint) { + if (data_format == AmdGpu::DataFormat::Format8 && num_format == AmdGpu::NumberFormat::Uint) { return vk::Format::eR8Uint; } if (data_format == AmdGpu::DataFormat::Format16_16_16_16 && From 0ed9ea5b859a76f72623ecf64d79e9449fb06c44 Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Wed, 17 Jul 2024 20:32:14 +0300 Subject: [PATCH 33/34] force imedialog to input "shadps4" as name . That way we can pass games that want username . temp solution till imgui dialog prepared --- src/core/libraries/dialogs/ime_dialog.cpp | 18 ++++++++++++++---- src/core/libraries/dialogs/ime_dialog.h | 3 +-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/core/libraries/dialogs/ime_dialog.cpp b/src/core/libraries/dialogs/ime_dialog.cpp index 89136a03..3fa35c2c 100644 --- a/src/core/libraries/dialogs/ime_dialog.cpp +++ b/src/core/libraries/dialogs/ime_dialog.cpp @@ -9,6 +9,8 @@ namespace Libraries::ImeDialog { +static OrbisImeDialogStatus g_ime_dlg_status = OrbisImeDialogStatus::ORBIS_IME_DIALOG_STATUS_NONE; + int PS4_SYSV_ABI sceImeDialogAbort() { LOG_ERROR(Lib_ImeDialog, "(STUBBED) called"); return ORBIS_OK; @@ -45,18 +47,25 @@ int PS4_SYSV_ABI sceImeDialogGetPanelSizeExtended() { } int PS4_SYSV_ABI sceImeDialogGetResult(OrbisImeDialogResult* result) { + result->endstatus = OrbisImeDialogEndStatus::ORBIS_IME_DIALOG_END_STATUS_OK; LOG_ERROR(Lib_ImeDialog, "(STUBBED) called"); return ORBIS_OK; } int PS4_SYSV_ABI sceImeDialogGetStatus() { - LOG_ERROR(Lib_ImeDialog, "(STUBBED) called"); - return ORBIS_OK; + if (g_ime_dlg_status == OrbisImeDialogStatus::ORBIS_IME_DIALOG_STATUS_RUNNING) { + return OrbisImeDialogStatus::ORBIS_IME_DIALOG_STATUS_FINISHED; + } + + return g_ime_dlg_status; } -int PS4_SYSV_ABI sceImeDialogInit(const OrbisImeDialogParam* param, - const OrbisImeParamExtended* extended) { +int PS4_SYSV_ABI sceImeDialogInit(OrbisImeDialogParam* param, OrbisImeParamExtended* extended) { LOG_ERROR(Lib_ImeDialog, "(STUBBED) called"); + const std::wstring_view text = L"shadPS4"; + param->maxTextLength = text.size(); + std::memcpy(param->inputTextBuffer, text.data(), text.size() * sizeof(wchar_t)); + g_ime_dlg_status = OrbisImeDialogStatus::ORBIS_IME_DIALOG_STATUS_RUNNING; return ORBIS_OK; } @@ -82,6 +91,7 @@ int PS4_SYSV_ABI sceImeDialogSetPanelPosition() { int PS4_SYSV_ABI sceImeDialogTerm() { LOG_ERROR(Lib_ImeDialog, "(STUBBED) called"); + g_ime_dlg_status = OrbisImeDialogStatus::ORBIS_IME_DIALOG_STATUS_NONE; return ORBIS_OK; } diff --git a/src/core/libraries/dialogs/ime_dialog.h b/src/core/libraries/dialogs/ime_dialog.h index 08c980a4..ffe42b31 100644 --- a/src/core/libraries/dialogs/ime_dialog.h +++ b/src/core/libraries/dialogs/ime_dialog.h @@ -174,8 +174,7 @@ int PS4_SYSV_ABI sceImeDialogGetPanelSize(); int PS4_SYSV_ABI sceImeDialogGetPanelSizeExtended(); int PS4_SYSV_ABI sceImeDialogGetResult(OrbisImeDialogResult* result); /*OrbisImeDialogStatus*/ int PS4_SYSV_ABI sceImeDialogGetStatus(); -int PS4_SYSV_ABI sceImeDialogInit(const OrbisImeDialogParam* param, - const OrbisImeParamExtended* extended); +int PS4_SYSV_ABI sceImeDialogInit(OrbisImeDialogParam* param, OrbisImeParamExtended* extended); int PS4_SYSV_ABI sceImeDialogInitInternal(); int PS4_SYSV_ABI sceImeDialogInitInternal2(); int PS4_SYSV_ABI sceImeDialogInitInternal3(); From 485d4fd0fbeca34a4b43c05b12c0cf65a09d208e Mon Sep 17 00:00:00 2001 From: Dzmitry Dubrova Date: Wed, 17 Jul 2024 23:34:22 +0300 Subject: [PATCH 34/34] kernel: Implement posix_pthread_once (#297) * kernel: Implement posix_pthread_once * Fix clang format --- src/core/libraries/kernel/thread_management.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/core/libraries/kernel/thread_management.cpp b/src/core/libraries/kernel/thread_management.cpp index 62282473..f58b365b 100644 --- a/src/core/libraries/kernel/thread_management.cpp +++ b/src/core/libraries/kernel/thread_management.cpp @@ -835,6 +835,10 @@ int PS4_SYSV_ABI posix_pthread_mutexattr_destroy(ScePthreadMutexattr* attr) { return result; } +int PS4_SYSV_ABI posix_pthread_once(pthread_once_t* once_control, void (*init_routine)(void)) { + return pthread_once(once_control, init_routine); +} + int PS4_SYSV_ABI posix_pthread_mutexattr_setprotocol(ScePthreadMutexattr* attr, int protocol) { int result = scePthreadMutexattrSetprotocol(attr, protocol); LOG_INFO(Kernel_Pthread, "redirect to scePthreadMutexattrSetprotocol: result = {}", result); @@ -1412,6 +1416,7 @@ void pthreadSymbolsRegister(Core::Loader::SymbolsResolver* sym) { posix_pthread_mutexattr_setprotocol); LIB_FUNCTION("HF7lK46xzjY", "libScePosix", 1, "libkernel", 1, 1, posix_pthread_mutexattr_destroy); + LIB_FUNCTION("Z4QosVuAsA0", "libScePosix", 1, "libkernel", 1, 1, posix_pthread_once); // openorbis weird functions LIB_FUNCTION("7H0iTOciTLo", "libkernel", 1, "libkernel", 1, 1, posix_pthread_mutex_lock);