diff --git a/Core/Config.h b/Core/Config.h index 686bab841d..6c79fe5c1d 100644 --- a/Core/Config.h +++ b/Core/Config.h @@ -21,6 +21,7 @@ #include #include +#include "ppsspp_config.h" #include "Common/CommonTypes.h" extern const char *PPSSPP_GIT_VERSION; @@ -75,7 +76,7 @@ public: bool bIgnoreWindowsKey; bool bRestartRequired; #endif -#if defined(USING_WIN_UI) || defined(USING_QT_UI) +#if defined(USING_WIN_UI) || defined(USING_QT_UI) || PPSSPP_PLATFORM(UWP) std::string sFont; #endif diff --git a/Core/MIPS/ARM64/Arm64CompVFPU.cpp b/Core/MIPS/ARM64/Arm64CompVFPU.cpp index 19ad4ed8d5..243890f1ea 100644 --- a/Core/MIPS/ARM64/Arm64CompVFPU.cpp +++ b/Core/MIPS/ARM64/Arm64CompVFPU.cpp @@ -1239,7 +1239,8 @@ namespace MIPSComp { fp.FMUL(fpr.V(dregs[a * 4 + b]), fpr.V(sregs[b * 4]), fpr.V(tregs[a * 4])); for (int c = 1; c < n; c++) { fpr.MapDirtyInInV(dregs[a * 4 + b], sregs[b * 4 + c], tregs[a * 4 + c], false); - fp.FMADD(fpr.V(dregs[a * 4 + b]), fpr.V(sregs[b * 4 + c]), fpr.V(tregs[a * 4 + c]), fpr.V(dregs[a * 4 + b])); + fp.FMUL(S0, fpr.V(sregs[b * 4 + c]), fpr.V(tregs[a * 4 + c])); + fp.FADD(fpr.V(dregs[a * 4 + b]), fpr.V(dregs[a * 4 + b]), S0); } } } diff --git a/Core/MIPS/MIPSIntVFPU.cpp b/Core/MIPS/MIPSIntVFPU.cpp index 1314b1239e..24d7210069 100644 --- a/Core/MIPS/MIPSIntVFPU.cpp +++ b/Core/MIPS/MIPSIntVFPU.cpp @@ -463,20 +463,21 @@ namespace MIPSInt for (int a = 0; a < n; a++) { for (int b = 0; b < n; b++) { - float sum = 0.0f; + union { float f; uint32_t u; } sum = { 0.0f }; if (a == n - 1 && b == n - 1) { // S and T prefixes work on the final (or maybe first, in reverse?) dot. ApplySwizzleS(&s[b * 4], V_Quad); ApplySwizzleT(&t[a * 4], V_Quad); - for (int c = 0; c < 4; c++) { - sum += s[b * 4 + c] * t[a * 4 + c]; - } - } else { - for (int c = 0; c < n; c++) { - sum += s[b * 4 + c] * t[a * 4 + c]; - } } - d[a * 4 + b] = sum; + + sum.f = vfpu_dot(&s[b * 4], &t[a * 4]); + if (my_isnan(sum.f)) { + sum.u = 0x7f800001; + } else if ((sum.u & 0x7F800000) == 0) { + sum.u &= 0xFF800000; + } + + d[a * 4 + b] = sum.f; } } @@ -1116,7 +1117,7 @@ namespace MIPSInt void Int_VDot(MIPSOpcode op) { float s[4]{}, t[4]{}; - float d; + union { float f; uint32_t u; } d; int vd = _VD; int vs = _VS; int vt = _VT; @@ -1125,12 +1126,16 @@ namespace MIPSInt ApplySwizzleS(s, V_Quad); ReadVector(t, sz, vt); ApplySwizzleT(t, V_Quad); - d = 0.0f; - for (int i = 0; i < 4; i++) { - d += s[i] * t[i]; + + d.f = vfpu_dot(s, t); + if (my_isnan(d.f)) { + d.u = 0x7f800001; + } else if ((d.u & 0x7F800000) == 0) { + d.u &= 0xFF800000; } - ApplyPrefixD(&d, V_Single); - WriteVector(&d, V_Single, vd); + + ApplyPrefixD(&d.f, V_Single); + WriteVector(&d.f, V_Single, vd); PC += 4; EatPrefixes(); } @@ -1165,10 +1170,7 @@ namespace MIPSInt } ApplyPrefixST(s, VFPURewritePrefix(VFPU_CTRL_SPREFIX, sprefixRemove, sprefixAdd), V_Quad); - float sum = 0.0f; - for (int i = 0; i < 4; i++) { - sum += s[i] * t[i]; - } + float sum = vfpu_dot(s, t); d = my_isnan(sum) ? fabsf(sum) : sum; ApplyPrefixD(&d, V_Single); WriteVector(&d, V_Single, vd); @@ -1404,10 +1406,7 @@ namespace MIPSInt u32 tprefixAdd = VFPU_MAKE_CONSTANTS(VFPUConst::ONE, VFPUConst::ONE, VFPUConst::ONE, VFPUConst::ONE); ApplyPrefixST(t, VFPURewritePrefix(VFPU_CTRL_TPREFIX, tprefixRemove, tprefixAdd), V_Quad); - d = 0.0f; - for (int i = 0; i < 4; i++) { - d += s[i] * t[i]; - } + d = vfpu_dot(s, t); ApplyPrefixD(&d, V_Single); WriteVector(&d, V_Single, vd); PC += 4; @@ -1438,10 +1437,7 @@ namespace MIPSInt tprefixAdd = 0; ApplyPrefixST(t, VFPURewritePrefix(VFPU_CTRL_TPREFIX, tprefixRemove, tprefixAdd), V_Quad); - d = 0.0f; - for (int i = 0; i < 4; i++) { - d += s[i] * t[i]; - } + d = vfpu_dot(s, t); ApplyPrefixD(&d, V_Single); WriteVector(&d, V_Single, vd); PC += 4; @@ -1558,7 +1554,8 @@ namespace MIPSInt } void Int_Vtfm(MIPSOpcode op) { - float s[16]{}, t[4]{}, d[4]; + float s[16]{}, t[4]{}; + FloatBits d; int vd = _VD; int vs = _VS; int vt = _VT; @@ -1572,13 +1569,24 @@ namespace MIPSInt ReadMatrix(s, msz, vs); ReadVector(t, sz, vt); - for (int i = 0; i < ins; i++) { - d[i] = s[i * 4] * t[0]; - for (int k = 1; k < tn; k++) { - d[i] += s[i * 4 + k] * t[k]; + float t2[4]; + for (int i = 0; i < 4; i++) { + if (i < tn) { + t2[i] = t[i]; + } else if (i == ins) { + t2[i] = 1.0f; + } else { + t2[i] = 0.0f; } - if (ins >= n) { - d[i] += s[i * 4 + ins]; + } + + for (int i = 0; i < ins; i++) { + d.f[i] = vfpu_dot(&s[i * 4], t2); + + if (my_isnan(d.f[i])) { + d.u[i] = 0x7f800001; + } else if ((d.u[i] & 0x7F800000) == 0) { + d.u[i] &= 0xFF800000; } } @@ -1599,17 +1607,20 @@ namespace MIPSInt ApplyPrefixST(t, VFPURewritePrefix(VFPU_CTRL_TPREFIX, tprefixRemove, tprefixAdd), V_Quad); // Really this is the operation all rows probably use (with constant wiring.) - d[ins] = s[ins * 4] * t[0]; - for (int k = 1; k < 4; k++) { - d[ins] += s[ins * 4 + k] * t[k]; + d.f[ins] = vfpu_dot(&s[ins * 4], t); + + if (my_isnan(d.f[ins])) { + d.u[ins] = 0x7f800001; + } else if ((d.u[ins] & 0x7F800000) == 0) { + d.u[ins] &= 0xFF800000; } // D prefix applies to the last element only. u32 lastmask = (currentMIPS->vfpuCtrl[VFPU_CTRL_DPREFIX] & (1 << 8)) << ins; u32 lastsat = (currentMIPS->vfpuCtrl[VFPU_CTRL_DPREFIX] & 3) << (ins + ins); currentMIPS->vfpuCtrl[VFPU_CTRL_DPREFIX] = lastmask | lastsat; - ApplyPrefixD(d, sz); - WriteVector(d, sz, vd); + ApplyPrefixD(d.f, sz); + WriteVector(d.f, sz, vd); PC += 4; EatPrefixes(); } @@ -1956,7 +1967,8 @@ namespace MIPSInt } void Int_VecDo3(MIPSOpcode op) { - float s[4], t[4], d[4]; + float s[4], t[4]; + FloatBits d; int vd = _VD; int vs = _VS; int vt = _VT; @@ -2000,10 +2012,16 @@ namespace MIPSInt for (int i = 0; i < n; i++) { switch (optype) { - case 0: d[i] = s[i] + t[i]; break; //vadd - case 1: d[i] = s[i] - t[i]; break; //vsub - case 7: d[i] = s[i] / t[i]; break; //vdiv - case 8: d[i] = s[i] * t[i]; break; //vmul + case 0: d.f[i] = s[i] + t[i]; break; //vadd + case 1: d.f[i] = s[i] - t[i]; break; //vsub + case 7: d.f[i] = s[i] / t[i]; break; //vdiv + case 8: d.f[i] = s[i] * t[i]; break; //vmul + } + + if (my_isnan(d.f[i])) { + d.u[i] = 0x7f800001; + } else if ((d.u[i] & 0x7F800000) == 0) { + d.u[i] &= 0xFF800000; } } @@ -2012,12 +2030,12 @@ namespace MIPSInt u32 lastmask = (currentMIPS->vfpuCtrl[VFPU_CTRL_DPREFIX] & (1 << 8)) << (n - 1); u32 lastsat = (currentMIPS->vfpuCtrl[VFPU_CTRL_DPREFIX] & 3) << (n + n - 2); currentMIPS->vfpuCtrl[VFPU_CTRL_DPREFIX] = lastmask | lastsat; - ApplyPrefixD(d, sz); + ApplyPrefixD(d.f, sz); } else { - RetainInvalidSwizzleST(d, sz); - ApplyPrefixD(d, sz); + RetainInvalidSwizzleST(d.f, sz); + ApplyPrefixD(d.f, sz); } - WriteVector(d, sz, vd); + WriteVector(d.f, sz, vd); PC += 4; EatPrefixes(); } diff --git a/Core/MIPS/MIPSVFPUUtils.cpp b/Core/MIPS/MIPSVFPUUtils.cpp index 6c07bfef5a..8de54a7834 100644 --- a/Core/MIPS/MIPSVFPUUtils.cpp +++ b/Core/MIPS/MIPSVFPUUtils.cpp @@ -607,3 +607,97 @@ float Float16ToFloat32(unsigned short l) } return f; } + +static uint32_t get_uexp(uint32_t x) { + return (x >> 23) & 0xFF; +} + +static int32_t get_mant(uint32_t x) { + // Note: this returns the hidden 1. + return (x & 0x007FFFFF) | 0x00800000; +} + +static int32_t get_sign(uint32_t x) { + return x & 0x80000000; +} + +float vfpu_dot(float a[4], float b[4]) { + static const int EXTRA_BITS = 2; + union float2int { + uint32_t i; + float f; + } intermed[4]; + + for (int i = 0; i < 4; i++) { + intermed[i].f = a[i] * b[i]; + } + + uint32_t exps[4]; + int32_t mants[4]; + uint32_t max_exp = 0; + uint32_t last_inf = 0; + + for (int i = 0; i < 4; i++) { + exps[i] = get_uexp(intermed[i].i); + // Preserve extra bits of precision in the mantissa during the add. + mants[i] = get_mant(intermed[i].i) << EXTRA_BITS; + if (exps[i] > max_exp) { + max_exp = exps[i]; + } + if (exps[i] == 255) { + bool diff_sign = last_inf && get_sign(last_inf) != get_sign(intermed[i].i); + bool mant_nan = mants[i] != (0x00800000 << EXTRA_BITS); + if (diff_sign || mant_nan) { + intermed[0].i = 0x7F800001; + return intermed[0].f; + } + last_inf = intermed[i].i; + } + } + + int32_t mant_sum = 0; + for (int i = 0; i < 4; i++) { + int exp = max_exp - exps[i]; + if (exp >= 32) { + mants[i] = 0; + } else { + mants[i] >>= max_exp - exps[i]; + } + if (get_sign(intermed[i].i)) { + mants[i] = -mants[i]; + } + mant_sum += mants[i]; + } + + uint32_t sign_sum = 0; + if (mant_sum < 0) { + sign_sum = 0x80000000; + mant_sum = -mant_sum; + } + // Chop off the extra bits. + mant_sum >>= EXTRA_BITS; + + if (mant_sum == 0 || max_exp == 0) { + return 0.0f; + } + + while (mant_sum < 0x00800000) { + mant_sum <<= 1; + max_exp -= 1; + } + while (mant_sum >= 0x01000000) { + mant_sum >>= 1; + max_exp += 1; + } + _dbg_assert_(JIT, (mant_sum & 0x00800000) != 0); + + if (max_exp >= 255) { + max_exp = 255; + mant_sum = 0; + } else if (max_exp == 0) { + return 0.0f; + } + + intermed[0].i = sign_sum | (max_exp << 23) | (mant_sum & 0x007FFFFF); + return intermed[0].f; +} diff --git a/Core/MIPS/MIPSVFPUUtils.h b/Core/MIPS/MIPSVFPUUtils.h index 5a9c59ae70..cb0ef9fa4c 100644 --- a/Core/MIPS/MIPSVFPUUtils.h +++ b/Core/MIPS/MIPSVFPUUtils.h @@ -97,6 +97,8 @@ inline float vfpu_clamp(float v, float min, float max) { return v >= max ? max : (v <= min ? min : v); } +float vfpu_dot(float a[4], float b[4]); + #define VFPU_FLOAT16_EXP_MAX 0x1f #define VFPU_SH_FLOAT16_SIGN 15 #define VFPU_MASK_FLOAT16_SIGN 0x1 diff --git a/SDL/SDLGLGraphicsContext.cpp b/SDL/SDLGLGraphicsContext.cpp index 12d43f790b..1717f61c63 100644 --- a/SDL/SDLGLGraphicsContext.cpp +++ b/SDL/SDLGLGraphicsContext.cpp @@ -108,10 +108,12 @@ EGLConfig EGL_FindConfig(int *contextVersion) { }; // We don't want HDR modes with more than 8 bits per component. - auto readConfigMax = [&](EGLint attr, EGLint m) -> EGLint { + // But let's assume some color is better than no color at all. + auto readConfigMax = [&](EGLint attr, EGLint m, EGLint def = 1) -> EGLint { EGLint val = readConfig(attr); - return val > m ? 1 : val; // why not 0? + return val > m ? def : val; }; + int colorScore = readConfigMax(EGL_RED_SIZE, 8) + readConfigMax(EGL_BLUE_SIZE, 8) + readConfigMax(EGL_GREEN_SIZE, 8); int alphaScore = readConfigMax(EGL_ALPHA_SIZE, 8); int depthScore = readConfig(EGL_DEPTH_SIZE); @@ -122,11 +124,13 @@ EGLConfig EGL_FindConfig(int *contextVersion) { int transparentScore = readConfig(EGL_TRANSPARENT_TYPE) == EGL_NONE ? 50 : 0; EGLint caveat = readConfig(EGL_CONFIG_CAVEAT); - int caveatScore = caveat == EGL_NONE ? 100 : (caveat == EGL_NON_CONFORMANT_CONFIG ? 50 : 0); + // Let's assume that non-conformant configs aren't so awful. + int caveatScore = caveat == EGL_NONE ? 100 : (caveat == EGL_NON_CONFORMANT_CONFIG ? 95 : 0); #ifndef USING_FBDEV EGLint surfaceType = readConfig(EGL_SURFACE_TYPE); - int surfaceScore = (surfaceType & EGL_WINDOW_BIT) ? 100 : 0; + // Only try a non-Window config in the worst case when there are only non-Window configs. + int surfaceScore = (surfaceType & EGL_WINDOW_BIT) ? 1000 : 0; #endif EGLint renderable = readConfig(EGL_RENDERABLE_TYPE); diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index 1c4bb35ea8..a8e4e4f90b 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -108,6 +108,9 @@ #if defined(USING_QT_UI) #include #endif +#if PPSSPP_PLATFORM(UWP) +#include +#endif // The new UI framework, for initialization @@ -646,7 +649,10 @@ void NativeInit(int argc, const char *argv[], const char *savegame_dir, const ch // Note to translators: do not translate this/add this to PPSSPP-lang's files. // It's intended to be custom for every user. // Only add it to your own personal copies of PPSSPP. -#if defined(USING_WIN_UI) && !PPSSPP_PLATFORM(UWP) +#if PPSSPP_PLATFORM(UWP) + // Roboto font is loaded in TextDrawerUWP. + g_Config.sFont = des->T("Font", "Roboto"); +#elif defined(USING_WIN_UI) && !PPSSPP_PLATFORM(UWP) // TODO: Could allow a setting to specify a font file to load? // TODO: Make this a constant if we can sanely load the font on other systems? AddFontResourceEx(L"assets/Roboto-Condensed.ttf", FR_PRIVATE, NULL); @@ -729,7 +735,7 @@ static UI::Style MakeStyle(uint32_t fg, uint32_t bg) { } static void UIThemeInit() { -#if (defined(USING_WIN_UI) && !PPSSPP_PLATFORM(UWP)) || defined(USING_QT_UI) +#if defined(USING_WIN_UI) || PPSSPP_PLATFORM(UWP) || defined(USING_QT_UI) ui_theme.uiFont = UI::FontStyle(UBUNTU24, g_Config.sFont.c_str(), 22); ui_theme.uiFontSmall = UI::FontStyle(UBUNTU24, g_Config.sFont.c_str(), 15); ui_theme.uiFontSmaller = UI::FontStyle(UBUNTU24, g_Config.sFont.c_str(), 12); diff --git a/UWP/App.cpp b/UWP/App.cpp index c190c308dd..586d679581 100644 --- a/UWP/App.cpp +++ b/UWP/App.cpp @@ -217,9 +217,10 @@ void App::OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ // Run() won't start until the CoreWindow is activated. CoreWindow::GetForCurrentThread()->Activate(); // On mobile, we force-enter fullscreen mode. - if (m_isPhone) { + if (m_isPhone) g_Config.bFullScreen = true; + + if (g_Config.bFullScreen) Windows::UI::ViewManagement::ApplicationView::GetForCurrentView()->TryEnterFullScreenMode(); - } } void App::OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args) { @@ -228,9 +229,10 @@ void App::OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args) { // aware that a deferral may not be held indefinitely. After about five seconds, // the app will be forced to exit. SuspendingDeferral^ deferral = args->SuspendingOperation->GetDeferral(); + auto app = this; - create_task([this, deferral]() { - m_deviceResources->Trim(); + create_task([app, deferral]() { + app->m_deviceResources->Trim(); deferral->Complete(); }); } @@ -246,6 +248,9 @@ void App::OnResuming(Platform::Object^ sender, Platform::Object^ args) { // Window event handlers. void App::OnWindowSizeChanged(CoreWindow^ sender, WindowSizeChangedEventArgs^ args) { + auto view = Windows::UI::ViewManagement::ApplicationView::GetForCurrentView(); + g_Config.bFullScreen = view->IsFullScreenMode; + int width = sender->Bounds.Width; int height = sender->Bounds.Height; float scale = m_deviceResources->GetDpi() / 96.0f; @@ -263,6 +268,13 @@ void App::OnWindowSizeChanged(CoreWindow^ sender, WindowSizeChangedEventArgs^ ar } void App::OnVisibilityChanged(CoreWindow^ sender, VisibilityChangedEventArgs^ args) { + + if (args->Visible == false) { + // MainScreen::OnExit and even App::OnWindowClosed + // doesn't seem to be called when closing the window + // Try to save the config here + g_Config.Save("App::OnVisibilityChanged"); + } m_windowVisible = args->Visible; } diff --git a/UWP/NativeUWP/NativeUWP.vcxproj b/UWP/NativeUWP/NativeUWP.vcxproj index 0d09121db9..5e629dc1dc 100644 --- a/UWP/NativeUWP/NativeUWP.vcxproj +++ b/UWP/NativeUWP/NativeUWP.vcxproj @@ -419,6 +419,7 @@ + @@ -1462,6 +1463,7 @@ + @@ -1642,4 +1644,4 @@ - + \ No newline at end of file diff --git a/UWP/NativeUWP/NativeUWP.vcxproj.filters b/UWP/NativeUWP/NativeUWP.vcxproj.filters index f7c7dddd41..2bda07e6a3 100644 --- a/UWP/NativeUWP/NativeUWP.vcxproj.filters +++ b/UWP/NativeUWP/NativeUWP.vcxproj.filters @@ -474,6 +474,9 @@ data + + gfx + @@ -747,5 +750,8 @@ data + + gfx + \ No newline at end of file diff --git a/UWP/PPSSPP_UWPMain.cpp b/UWP/PPSSPP_UWPMain.cpp index 2ff75df9e1..b7187575f4 100644 --- a/UWP/PPSSPP_UWPMain.cpp +++ b/UWP/PPSSPP_UWPMain.cpp @@ -42,6 +42,7 @@ using namespace Concurrency; // UGLY! PPSSPP_UWPMain *g_main; extern WindowsAudioBackend *winAudioBackend; +std::string langRegion; // TODO: Use Microsoft::WRL::ComPtr<> for D3D11 objects? // TODO: See https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/WindowsAudioSession for WASAPI with UWP // TODO: Low latency input: https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/LowLatencyInput/cpp @@ -76,7 +77,6 @@ PPSSPP_UWPMain::PPSSPP_UWPMain(App ^app, const std::shared_ptrLoadStorageFile(file); } }); + } else if (!strcmp(command, "toggle_fullscreen")) { + auto view = Windows::UI::ViewManagement::ApplicationView::GetForCurrentView(); + if (strcmp(parameter, "0") == 0) { + view->ExitFullScreenMode(); + } + else if (strcmp(parameter, "1") == 0){ + view->TryEnterFullScreenMode(); + } } } diff --git a/UWP/UWP.vcxproj b/UWP/UWP.vcxproj index 5f5c849fa4..f581533162 100644 --- a/UWP/UWP.vcxproj +++ b/UWP/UWP.vcxproj @@ -189,6 +189,9 @@ False Never B57919568D3F60B8D25EC6856DE94173682C548C + False + 1 + OnApplicationRun @@ -205,10 +208,14 @@ pch.h - copy PackageNormal.appxmanifest Package.appxmanifest /Y -mkdir Assets -copy AssetsNormal\*.* Assets /Y -../Windows/git-version-gen.cmd + + copy PackageNormal.appxmanifest Package.appxmanifest /Y + mkdir Assets + copy AssetsNormal\*.* Assets /Y + robocopy "..\Assets\." "Content\." /xf *.vsh /E /R:0 /W:0 + ../Windows/git-version-gen.cmd + EXIT 0 + @@ -226,10 +233,14 @@ copy AssetsNormal\*.* Assets /Y pch.h - copy PackageNormal.appxmanifest Package.appxmanifest /Y -mkdir Assets -copy AssetsNormal\*.* Assets /Y -../Windows/git-version-gen.cmd + + copy PackageNormal.appxmanifest Package.appxmanifest /Y + mkdir Assets + copy AssetsNormal\*.* Assets /Y + robocopy "..\Assets\." "Content\." /xf *.vsh /E /R:0 /W:0 + ../Windows/git-version-gen.cmd + EXIT 0 + @@ -247,10 +258,14 @@ copy AssetsNormal\*.* Assets /Y pch.h - copy PackageNormal.appxmanifest Package.appxmanifest /Y -mkdir Assets -copy AssetsNormal\*.* Assets /Y -../Windows/git-version-gen.cmd + + copy PackageNormal.appxmanifest Package.appxmanifest /Y + mkdir Assets + copy AssetsNormal\*.* Assets /Y + robocopy "..\Assets\." "Content\." /xf *.vsh /E /R:0 /W:0 + ../Windows/git-version-gen.cmd + EXIT 0 + @@ -268,10 +283,14 @@ copy AssetsNormal\*.* Assets /Y pch.h - copy PackageNormal.appxmanifest Package.appxmanifest /Y -mkdir Assets -copy AssetsNormal\*.* Assets /Y -../Windows/git-version-gen.cmd + + copy PackageNormal.appxmanifest Package.appxmanifest /Y + mkdir Assets + copy AssetsNormal\*.* Assets /Y + robocopy "..\Assets\." "Content\." /xf *.vsh /E /R:0 /W:0 + ../Windows/git-version-gen.cmd + EXIT 0 + @@ -289,10 +308,14 @@ copy AssetsNormal\*.* Assets /Y pch.h - copy PackageGold.appxmanifest Package.appxmanifest /Y -mkdir Assets -copy AssetsGold\*.* Assets /Y -../Windows/git-version-gen.cmd + + copy PackageGold.appxmanifest Package.appxmanifest /Y + mkdir Assets + copy AssetsGold\*.* Assets /Y + robocopy "..\Assets\." "Content\." /xf *.vsh /E /R:0 /W:0 + ../Windows/git-version-gen.cmd + EXIT 0 + @@ -310,10 +333,14 @@ copy AssetsGold\*.* Assets /Y pch.h - copy PackageGold.appxmanifest Package.appxmanifest /Y -mkdir Assets -copy AssetsGold\*.* Assets /Y -../Windows/git-version-gen.cmd + + copy PackageGold.appxmanifest Package.appxmanifest /Y + mkdir Assets + copy AssetsGold\*.* Assets /Y + robocopy "..\Assets\." "Content\." /xf *.vsh /E /R:0 /W:0 + ../Windows/git-version-gen.cmd + EXIT 0 + @@ -331,10 +358,14 @@ copy AssetsGold\*.* Assets /Y pch.h - copy PackageNormal.appxmanifest Package.appxmanifest /Y -mkdir Assets -copy AssetsNormal\*.* Assets /Y -../Windows/git-version-gen.cmd + + copy PackageNormal.appxmanifest Package.appxmanifest /Y + mkdir Assets + copy AssetsNormal\*.* Assets /Y + robocopy "..\Assets\." "Content\." /xf *.vsh /E /R:0 /W:0 + ../Windows/git-version-gen.cmd + EXIT 0 + @@ -352,10 +383,14 @@ copy AssetsNormal\*.* Assets /Y pch.h - copy PackageNormal.appxmanifest Package.appxmanifest /Y -mkdir Assets -copy AssetsNormal\*.* Assets /Y -../Windows/git-version-gen.cmd + + copy PackageNormal.appxmanifest Package.appxmanifest /Y + mkdir Assets + copy AssetsNormal\*.* Assets /Y + robocopy "..\Assets\." "Content\." /xf *.vsh /E /R:0 /W:0 + ../Windows/git-version-gen.cmd + EXIT 0 + @@ -373,10 +408,14 @@ copy AssetsNormal\*.* Assets /Y pch.h - copy PackageGold.appxmanifest Package.appxmanifest /Y -mkdir Assets -copy AssetsGold\*.* Assets /Y -../Windows/git-version-gen.cmd + + copy PackageGold.appxmanifest Package.appxmanifest /Y + mkdir Assets + copy AssetsGold\*.* Assets /Y + robocopy "..\Assets\." "Content\." /xf *.vsh /E /R:0 /W:0 + ../Windows/git-version-gen.cmd + EXIT 0 + @@ -394,10 +433,14 @@ copy AssetsGold\*.* Assets /Y pch.h - copy PackageNormal.appxmanifest Package.appxmanifest /Y -mkdir Assets -copy AssetsNormal\*.* Assets /Y -../Windows/git-version-gen.cmd + + copy PackageNormal.appxmanifest Package.appxmanifest /Y + mkdir Assets + copy AssetsNormal\*.* Assets /Y + robocopy "..\Assets\." "Content\." /xf *.vsh /E /R:0 /W:0 + ../Windows/git-version-gen.cmd + EXIT 0 + @@ -415,10 +458,14 @@ copy AssetsNormal\*.* Assets /Y pch.h - copy PackageNormal.appxmanifest Package.appxmanifest /Y -mkdir Assets -copy AssetsNormal\*.* Assets /Y -../Windows/git-version-gen.cmd + + copy PackageNormal.appxmanifest Package.appxmanifest /Y + mkdir Assets + copy AssetsNormal\*.* Assets /Y + robocopy "..\Assets\." "Content\." /xf *.vsh /E /R:0 /W:0 + ../Windows/git-version-gen.cmd + EXIT 0 + @@ -435,19 +482,15 @@ copy AssetsNormal\*.* Assets /Y GOLD;NDEBUG;%(PreprocessorDefinitions) pch.h - - copy PackageGold.appxmanifest Package.appxmanifest /Y -mkdir Assets -copy AssetsGold\*.* Assets /Y -../Windows/git-version-gen.cmd - - - - robocopy "..\Assets\." "Content\." /xf *.vsh /E /R:0 /W:0 - EXIT 0 - + copy PackageGold.appxmanifest Package.appxmanifest /Y + mkdir Assets + copy AssetsGold\*.* Assets /Y + robocopy "..\Assets\." "Content\." /xf *.vsh /E /R:0 /W:0 + ../Windows/git-version-gen.cmd + EXIT 0 + @@ -560,91 +603,253 @@ copy AssetsGold\*.* Assets /Y true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true @@ -656,206 +861,575 @@ copy AssetsGold\*.* Assets /Y true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true @@ -864,96 +1438,267 @@ copy AssetsGold\*.* Assets /Y true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true true true + true + true + true + true + true + true + true + true + true true diff --git a/UWP/UWP.vcxproj.filters b/UWP/UWP.vcxproj.filters index dce1a52757..747090f89e 100644 --- a/UWP/UWP.vcxproj.filters +++ b/UWP/UWP.vcxproj.filters @@ -38,6 +38,18 @@ {ef87b390-608f-4c6d-a0ad-88963875ed11} + + {0c1cdb21-de65-4a10-962e-46b9bb0ff717} + + + {a861f712-3285-448d-b7a0-f6d39ac4c4b9} + + + {d51ae335-1279-4484-bfbf-2caa2efba6d9} + + + {d2294c79-82bd-470f-b984-f838f0fbcd25} + @@ -110,89 +122,249 @@ Content - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\lang + + + Content\shaders + + + Content\shaders + + + Content\shaders + + + Content\shaders + + + Content\shaders + + + Content\shaders + + + Content\shaders + + + Content\shaders + + + Content\shaders + + + Content\shaders + + + Content\shaders + + + Content\shaders + + + Content\flash0\font + + + Content\flash0\font + + + Content\flash0\font + + + Content\flash0\font + + + Content\flash0\font + + + Content\flash0\font + + + Content\flash0\font + + + Content\flash0\font + + + Content\flash0\font + + + Content\flash0\font + + + Content\flash0\font + + + Content\flash0\font + + + Content\flash0\font + + + Content\flash0\font + + + Content\flash0\font + + + Content\flash0\font + + + Content\flash0\font + + + Content\flash0\font + + + Content\shaders + + + Content\shaders + + + Content\shaders + + + Content\shaders + + + Content\shaders + + + Content\shaders + + + Content\shaders + Content + + + Content + + \ No newline at end of file diff --git a/assets/lang b/assets/lang index a1993a6dd7..9d93983574 160000 --- a/assets/lang +++ b/assets/lang @@ -1 +1 @@ -Subproject commit a1993a6dd7e963c9bba0d8b81d826169b2627c6c +Subproject commit 9d93983574606d2af15be8cc0b49604bb4cafad6 diff --git a/ext/native/gfx_es2/draw_text.cpp b/ext/native/gfx_es2/draw_text.cpp index b71c1ddc4c..168f84f151 100644 --- a/ext/native/gfx_es2/draw_text.cpp +++ b/ext/native/gfx_es2/draw_text.cpp @@ -7,6 +7,7 @@ #include "util/text/utf8.h" #include "gfx_es2/draw_text.h" #include "gfx_es2/draw_text_win.h" +#include "gfx_es2/draw_text_uwp.h" #include "gfx_es2/draw_text_qt.h" #include "gfx_es2/draw_text_android.h" @@ -45,6 +46,8 @@ TextDrawer *TextDrawer::Create(Draw::DrawContext *draw) { TextDrawer *drawer = nullptr; #if defined(_WIN32) && !PPSSPP_PLATFORM(UWP) drawer = new TextDrawerWin32(draw); +#elif PPSSPP_PLATFORM(UWP) + drawer = new TextDrawerUWP(draw); #elif defined(USING_QT_UI) drawer = new TextDrawerQt(draw); #elif PPSSPP_PLATFORM(ANDROID) diff --git a/ext/native/gfx_es2/draw_text_uwp.cpp b/ext/native/gfx_es2/draw_text_uwp.cpp new file mode 100644 index 0000000000..52183a5c40 --- /dev/null +++ b/ext/native/gfx_es2/draw_text_uwp.cpp @@ -0,0 +1,538 @@ +#include "base/display.h" +#include "base/logging.h" +#include "base/stringutil.h" +#include "thin3d/thin3d.h" +#include "util/hash/hash.h" +#include "util/text/wrap_text.h" +#include "util/text/utf8.h" +#include "gfx_es2/draw_text.h" +#include "gfx_es2/draw_text_uwp.h" + + +#if PPSSPP_PLATFORM(UWP) + +#include +#include +#include +#include + +enum { + MAX_TEXT_WIDTH = 4096, + MAX_TEXT_HEIGHT = 512 +}; + +class TextDrawerFontContext { +public: + ~TextDrawerFontContext() { + Destroy(); + } + + void Create() { + if (textFmt) { + Destroy(); + } + + if (factory == nullptr) return; + + HRESULT hr = factory->CreateTextFormat( + fname.c_str(), + fontCollection, + weight, + DWRITE_FONT_STYLE_NORMAL, + DWRITE_FONT_STRETCH_NORMAL, + MulDiv(height, (int)(96.0f * (1.0f / dpiScale)), 72), + L"", + &textFmt + ); + } + void Destroy() { + textFmt->Release(); + textFmt = nullptr; + } + + IDWriteFactory4* factory = nullptr; + IDWriteFontCollection1* fontCollection = nullptr; + IDWriteTextFormat* textFmt = nullptr; + std::wstring fname; + int height; + DWRITE_FONT_WEIGHT weight; + float dpiScale; +}; + +struct TextDrawerContext { + ID2D1Bitmap1* bitmap; + ID2D1Bitmap1* mirror_bmp; +}; + +TextDrawerUWP::TextDrawerUWP(Draw::DrawContext *draw) : TextDrawer(draw), ctx_(nullptr) { + HRESULT hr; + // It's fine to assume we are using D3D11 in UWP + ID3D11Device* d3ddevice = (ID3D11Device *)draw->GetNativeObject(Draw::NativeObject::DEVICE); + + IDXGIDevice* dxgiDevice; + hr = d3ddevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice); + if (FAILED(hr)) FLOG("ID3DDevice QueryInterface IDXGIDevice failed"); + + // Initialize the Direct2D Factory. + D2D1_FACTORY_OPTIONS options = {}; + D2D1CreateFactory( + D2D1_FACTORY_TYPE_SINGLE_THREADED, + __uuidof(ID2D1Factory5), + &options, + (void**)&m_d2dFactory + ); + + // Initialize the DirectWrite Factory. + DWriteCreateFactory( + DWRITE_FACTORY_TYPE_SHARED, + __uuidof(IDWriteFactory5), + (IUnknown**)&m_dwriteFactory + ); + + // Create D2D Device and DeviceContext. + // TODO: We have one sitting right in DX::DeviceResource, there might be a way to use that instead. + hr = m_d2dFactory->CreateDevice(dxgiDevice, &m_d2dDevice); + if (FAILED(hr)) FLOG("D2D CreateDevice failed"); + hr = m_d2dDevice->CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &m_d2dContext); + if (FAILED(hr)) FLOG("D2D CreateDeviceContext failed"); + + // Load the Roboto font + hr = m_dwriteFactory->CreateFontFileReference(L"Content/Roboto-Condensed.ttf", nullptr, &m_fontFile); + if (FAILED(hr)) ELOG("CreateFontFileReference failed"); + hr = m_dwriteFactory->CreateFontSetBuilder(&m_fontSetBuilder); + if (FAILED(hr)) ELOG("CreateFontSetBuilder failed"); + hr = m_fontSetBuilder->AddFontFile(m_fontFile); + hr = m_fontSetBuilder->CreateFontSet(&m_fontSet); + hr = m_dwriteFactory->CreateFontCollectionFromFontSet(m_fontSet, &m_fontCollection); + + ctx_ = new TextDrawerContext(); + + D2D1_BITMAP_PROPERTIES1 properties; + properties.pixelFormat.format = DXGI_FORMAT_B8G8R8A8_UNORM; + properties.pixelFormat.alphaMode = D2D1_ALPHA_MODE_PREMULTIPLIED; + properties.dpiX = 96.0f; + properties.dpiY = 96.0f; + properties.bitmapOptions = D2D1_BITMAP_OPTIONS_CANNOT_DRAW | D2D1_BITMAP_OPTIONS_TARGET; + properties.colorContext = nullptr; + + // Create main drawing bitmap + m_d2dContext->CreateBitmap( + D2D1::SizeU(MAX_TEXT_WIDTH, MAX_TEXT_HEIGHT), + nullptr, + 0, + &properties, + &ctx_->bitmap + ); + m_d2dContext->SetTarget(ctx_->bitmap); + + // Create mirror bitmap for mapping + properties.bitmapOptions = D2D1_BITMAP_OPTIONS_CANNOT_DRAW | D2D1_BITMAP_OPTIONS_CPU_READ; + m_d2dContext->CreateBitmap( + D2D1::SizeU(MAX_TEXT_WIDTH, MAX_TEXT_HEIGHT), + nullptr, + 0, + &properties, + &ctx_->mirror_bmp + ); + + m_d2dContext->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::White, 1.0f), &m_d2dWhiteBrush); + +} + +TextDrawerUWP::~TextDrawerUWP() { + ClearCache(); + + fontMap_.clear(); + + ctx_->bitmap->Release(); + ctx_->mirror_bmp->Release(); + + m_d2dWhiteBrush->Release(); + m_d2dContext->Release(); + m_d2dDevice->Release(); + m_d2dFactory->Release(); + + m_fontCollection->Release(); + m_fontSet->Release(); + m_fontFile->Release(); + m_fontSetBuilder->Release(); + m_dwriteFactory->Release(); + delete ctx_; +} + +uint32_t TextDrawerUWP::SetFont(const char *fontName, int size, int flags) { + uint32_t fontHash = fontName ? hash::Adler32((const uint8_t *)fontName, strlen(fontName)) : 0; + fontHash ^= size; + fontHash ^= flags << 10; + + auto iter = fontMap_.find(fontHash); + if (iter != fontMap_.end()) { + fontHash_ = fontHash; + return fontHash; + } + + std::wstring fname; + if (fontName) + fname = ConvertUTF8ToWString(fontName); + else + fname = L"Tahoma"; + + TextDrawerFontContext *font = new TextDrawerFontContext(); + font->weight = DWRITE_FONT_WEIGHT_LIGHT; + font->height = size; + font->fname = fname; + font->dpiScale = dpiScale_; + font->factory = m_dwriteFactory; + font->fontCollection = m_fontCollection; + font->Create(); + + fontMap_[fontHash] = std::unique_ptr(font); + fontHash_ = fontHash; + return fontHash; +} + +void TextDrawerUWP::SetFont(uint32_t fontHandle) { + auto iter = fontMap_.find(fontHandle); + if (iter != fontMap_.end()) { + fontHash_ = fontHandle; + } +} + +void TextDrawerUWP::MeasureString(const char *str, size_t len, float *w, float *h) { + CacheKey key{ std::string(str, len), fontHash_ }; + + TextMeasureEntry *entry; + auto iter = sizeCache_.find(key); + if (iter != sizeCache_.end()) { + entry = iter->second.get(); + } else { + IDWriteTextFormat* format = nullptr; + auto iter = fontMap_.find(fontHash_); + if (iter != fontMap_.end()) { + format = iter->second->textFmt; + } + if (!format) return; + + std::wstring wstr = ConvertUTF8ToWString(ReplaceAll(ReplaceAll(std::string(str, len), "\n", "\r\n"), "&&", "&")); + + format->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING); + + IDWriteTextLayout* layout; + m_dwriteFactory->CreateTextLayout( + (LPWSTR)wstr.c_str(), + (int)wstr.size(), + format, + MAX_TEXT_WIDTH, + MAX_TEXT_HEIGHT, + &layout + ); + + DWRITE_TEXT_METRICS metrics; + layout->GetMetrics(&metrics); + layout->Release(); + + entry = new TextMeasureEntry(); + entry->width = metrics.width + 1; + entry->height = metrics.height + 1; + sizeCache_[key] = std::unique_ptr(entry); + } + + entry->lastUsedFrame = frameCount_; + *w = entry->width * fontScaleX_ * dpiScale_; + *h = entry->height * fontScaleY_ * dpiScale_; +} + +void TextDrawerUWP::MeasureStringRect(const char *str, size_t len, const Bounds &bounds, float *w, float *h, int align) { + + IDWriteTextFormat* format = nullptr; + auto iter = fontMap_.find(fontHash_); + if (iter != fontMap_.end()) { + format = iter->second->textFmt; + } + if (!format) return; + + std::string toMeasure = std::string(str, len); + if (align & FLAG_WRAP_TEXT) { + bool rotated = (align & (ROTATE_90DEG_LEFT | ROTATE_90DEG_RIGHT)) != 0; + WrapString(toMeasure, toMeasure.c_str(), rotated ? bounds.h : bounds.w); + } + + std::vector lines; + SplitString(toMeasure, '\n', lines); + float total_w = 0.0f; + float total_h = 0.0f; + for (size_t i = 0; i < lines.size(); i++) { + CacheKey key{ lines[i], fontHash_ }; + + TextMeasureEntry *entry; + auto iter = sizeCache_.find(key); + if (iter != sizeCache_.end()) { + entry = iter->second.get(); + } else { + std::wstring wstr = ConvertUTF8ToWString(lines[i].length() == 0 ? " " : ReplaceAll(lines[i], "&&", "&")); + + if (align & ALIGN_HCENTER) + format->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER); + else if (align & ALIGN_LEFT) + format->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING); + else if (align & ALIGN_RIGHT) + format->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_TRAILING); + + IDWriteTextLayout* layout; + m_dwriteFactory->CreateTextLayout( + (LPWSTR)wstr.c_str(), + (int)wstr.size(), + format, + MAX_TEXT_WIDTH, + MAX_TEXT_HEIGHT, + &layout + ); + + DWRITE_TEXT_METRICS metrics; + layout->GetMetrics(&metrics); + layout->Release(); + + entry = new TextMeasureEntry(); + entry->width = metrics.width + 1; + entry->height = metrics.height + 1; + sizeCache_[key] = std::unique_ptr(entry); + } + entry->lastUsedFrame = frameCount_; + + if (total_w < entry->width * fontScaleX_) { + total_w = entry->width * fontScaleX_; + } + total_h += entry->height * fontScaleY_; + } + *w = total_w * dpiScale_; + *h = total_h * dpiScale_; +} + +void TextDrawerUWP::DrawString(DrawBuffer &target, const char *str, float x, float y, uint32_t color, int align) { + using namespace Draw; + if (!strlen(str)) + return; + + CacheKey key{ std::string(str), fontHash_ }; + + target.Flush(true); + + TextStringEntry *entry; + + auto iter = cache_.find(key); + if (iter != cache_.end()) { + entry = iter->second.get(); + entry->lastUsedFrame = frameCount_; + } + else { + // Render the string to our bitmap and save to a GL texture. + std::wstring wstr = ConvertUTF8ToWString(ReplaceAll(ReplaceAll(str, "\n", "\r\n"), "&&", "&")); + SIZE size; + + IDWriteTextFormat* format = nullptr; + auto iter = fontMap_.find(fontHash_); + if (iter != fontMap_.end()) { + format = iter->second->textFmt; + } + if (!format) return; + + if (align & ALIGN_HCENTER) + format->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER); + else if (align & ALIGN_LEFT) + format->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING); + else if (align & ALIGN_RIGHT) + format->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_TRAILING); + + IDWriteTextLayout* layout; + m_dwriteFactory->CreateTextLayout( + (LPWSTR)wstr.c_str(), + (int)wstr.size(), + format, + MAX_TEXT_WIDTH, + MAX_TEXT_HEIGHT, + &layout + ); + + DWRITE_TEXT_METRICS metrics; + layout->GetMetrics(&metrics); + + // Set the max size the same as current size, avoiding alignment issues + layout->SetMaxHeight(metrics.height); + layout->SetMaxWidth(metrics.width); + + size.cx = metrics.width + 1; + size.cy = metrics.height + 1; + + // Prevent zero-sized textures, which can occur. Not worth to avoid + // creating the texture altogether in this case. One example is a string + // containing only '\r\n', see issue #10764. + if (size.cx == 0) + size.cx = 1; + if (size.cy == 0) + size.cy = 1; + + entry = new TextStringEntry(); + entry->width = size.cx; + entry->height = size.cy; + entry->bmWidth = (size.cx + 3) & ~3; + entry->bmHeight = (size.cy + 3) & ~3; + entry->lastUsedFrame = frameCount_; + + m_d2dContext->BeginDraw(); + m_d2dContext->Clear(); + m_d2dContext->DrawTextLayout(D2D1::Point2F(0.0f, 0.0f), layout, m_d2dWhiteBrush); + m_d2dContext->EndDraw(); + + layout->Release(); + + D2D1_POINT_2U dstP = D2D1::Point2U(0, 0); + D2D1_RECT_U srcR = D2D1::RectU(0, 0, entry->bmWidth, entry->bmHeight); + D2D1_MAPPED_RECT map; + ctx_->mirror_bmp->CopyFromBitmap(&dstP, ctx_->bitmap, &srcR); + ctx_->mirror_bmp->Map(D2D1_MAP_OPTIONS_READ, &map); + + DataFormat texFormat; + // For our purposes these are equivalent, so just choose the supported one. D3D can emulate them. + if (draw_->GetDataFormatSupport(Draw::DataFormat::A4R4G4B4_UNORM_PACK16) & FMT_TEXTURE) + texFormat = Draw::DataFormat::A4R4G4B4_UNORM_PACK16; + else if (draw_->GetDataFormatSupport(Draw::DataFormat::B4G4R4A4_UNORM_PACK16) & FMT_TEXTURE) + texFormat = Draw::DataFormat::B4G4R4A4_UNORM_PACK16; + else + texFormat = Draw::DataFormat::R8G8B8A8_UNORM; + + // Convert the bitmap to a Thin3D compatible array of 16-bit pixels. Can't use a single channel format + // because we need white. Well, we could using swizzle, but not all our backends support that. + TextureDesc desc{}; + uint32_t *bitmapData32 = nullptr; + uint16_t *bitmapData16 = nullptr; + if (texFormat == Draw::DataFormat::R8G8B8A8_UNORM || texFormat == Draw::DataFormat::B8G8R8A8_UNORM) { + bitmapData32 = new uint32_t[entry->bmWidth * entry->bmHeight]; + for (int y = 0; y < entry->bmHeight; y++) { + for (int x = 0; x < entry->bmWidth; x++) { + uint8_t bAlpha = (uint8_t)(map.bits[map.pitch * y + x * 4] & 0xff); + bitmapData32[entry->bmWidth * y + x] = (bAlpha << 24) | 0x00ffffff; + } + } + desc.initData.push_back((uint8_t *)bitmapData32); + } else if (texFormat == Draw::DataFormat::B4G4R4A4_UNORM_PACK16) { + bitmapData16 = new uint16_t[entry->bmWidth * entry->bmHeight]; + for (int y = 0; y < entry->bmHeight; y++) { + for (int x = 0; x < entry->bmWidth; x++) { + uint8_t bAlpha = (uint8_t)((map.bits[map.pitch * y + x * 4] & 0xff) >> 4); + bitmapData16[entry->bmWidth * y + x] = (bAlpha) | 0xfff0; + } + } + desc.initData.push_back((uint8_t *)bitmapData16); + } else if (texFormat == Draw::DataFormat::A4R4G4B4_UNORM_PACK16) { + bitmapData16 = new uint16_t[entry->bmWidth * entry->bmHeight]; + for (int y = 0; y < entry->bmHeight; y++) { + for (int x = 0; x < entry->bmWidth; x++) { + uint8_t bAlpha = (uint8_t)((map.bits[map.pitch * y + x * 4] & 0xff) >> 4); + bitmapData16[entry->bmWidth * y + x] = (bAlpha << 12) | 0x0fff; + } + } + desc.initData.push_back((uint8_t *)bitmapData16); + } + + ctx_->mirror_bmp->Unmap(); + + desc.type = TextureType::LINEAR2D; + desc.format = texFormat; + desc.width = entry->bmWidth; + desc.height = entry->bmHeight; + desc.depth = 1; + desc.mipLevels = 1; + desc.tag = "TextDrawer"; + entry->texture = draw_->CreateTexture(desc); + if (bitmapData16) + delete[] bitmapData16; + if (bitmapData32) + delete[] bitmapData32; + cache_[key] = std::unique_ptr(entry); + } + + draw_->BindTexture(0, entry->texture); + + // Okay, the texture is bound, let's draw. + float w = entry->width * fontScaleX_ * dpiScale_; + float h = entry->height * fontScaleY_ * dpiScale_; + float u = entry->width / (float)entry->bmWidth; + float v = entry->height / (float)entry->bmHeight; + DrawBuffer::DoAlign(align, &x, &y, &w, &h); + target.DrawTexRect(x, y, x + w, y + h, 0.0f, 0.0f, u, v, color); + target.Flush(true); + +} + +void TextDrawerUWP::RecreateFonts() { + for (auto& iter : fontMap_) { + iter.second->dpiScale = dpiScale_; + iter.second->Create(); + } +} + +void TextDrawerUWP::ClearCache() { + for (auto &iter : cache_) { + if (iter.second->texture) + iter.second->texture->Release(); + } + cache_.clear(); + sizeCache_.clear(); +} + +void TextDrawerUWP::DrawStringRect(DrawBuffer &target, const char *str, const Bounds &bounds, uint32_t color, int align) { + float x = bounds.x; + float y = bounds.y; + if (align & ALIGN_HCENTER) { + x = bounds.centerX(); + } else if (align & ALIGN_RIGHT) { + x = bounds.x2(); + } + if (align & ALIGN_VCENTER) { + y = bounds.centerY(); + } else if (align & ALIGN_BOTTOM) { + y = bounds.y2(); + } + + std::string toDraw = str; + if (align & FLAG_WRAP_TEXT) { + bool rotated = (align & (ROTATE_90DEG_LEFT | ROTATE_90DEG_RIGHT)) != 0; + WrapString(toDraw, str, rotated ? bounds.h : bounds.w); + } + + DrawString(target, toDraw.c_str(), x, y, color, align); +} + +void TextDrawerUWP::OncePerFrame() { + frameCount_++; + // If DPI changed (small-mode, future proper monitor DPI support), drop everything. + float newDpiScale = CalculateDPIScale(); + if (newDpiScale != dpiScale_) { + dpiScale_ = newDpiScale; + ClearCache(); + RecreateFonts(); + } + + // Drop old strings. Use a prime number to reduce clashing with other rhythms + if (frameCount_ % 23 == 0) { + for (auto iter = cache_.begin(); iter != cache_.end();) { + if (frameCount_ - iter->second->lastUsedFrame > 100) { + if (iter->second->texture) + iter->second->texture->Release(); + cache_.erase(iter++); + } else { + iter++; + } + } + + for (auto iter = sizeCache_.begin(); iter != sizeCache_.end(); ) { + if (frameCount_ - iter->second->lastUsedFrame > 100) { + sizeCache_.erase(iter++); + } else { + iter++; + } + } + } +} + +#endif diff --git a/ext/native/gfx_es2/draw_text_uwp.h b/ext/native/gfx_es2/draw_text_uwp.h new file mode 100644 index 0000000000..0697ea62d6 --- /dev/null +++ b/ext/native/gfx_es2/draw_text_uwp.h @@ -0,0 +1,58 @@ +#pragma once + +#include "ppsspp_config.h" + +#include +#include "gfx_es2/draw_text.h" + +#if PPSSPP_PLATFORM(UWP) + +#include +#include + +struct TextDrawerContext; +// Internal struct but all details in .cpp file (pimpl to avoid pulling in excessive headers here) +class TextDrawerFontContext; + +class TextDrawerUWP : public TextDrawer { +public: + TextDrawerUWP(Draw::DrawContext *draw); + ~TextDrawerUWP(); + + uint32_t SetFont(const char *fontName, int size, int flags) override; + void SetFont(uint32_t fontHandle) override; // Shortcut once you've set the font once. + void MeasureString(const char *str, size_t len, float *w, float *h) override; + void MeasureStringRect(const char *str, size_t len, const Bounds &bounds, float *w, float *h, int align = ALIGN_TOPLEFT) override; + void DrawString(DrawBuffer &target, const char *str, float x, float y, uint32_t color, int align = ALIGN_TOPLEFT) override; + void DrawStringRect(DrawBuffer &target, const char *str, const Bounds &bounds, uint32_t color, int align) override; + // Use for housekeeping like throwing out old strings. + void OncePerFrame() override; + +protected: + void ClearCache() override; + void RecreateFonts(); // On DPI change + + TextDrawerContext *ctx_; + std::map> fontMap_; + + uint32_t fontHash_; + std::map> cache_; + std::map> sizeCache_; + + + // Direct2D drawing components. + ID2D1Factory5* m_d2dFactory; + ID2D1Device4* m_d2dDevice; + ID2D1DeviceContext4* m_d2dContext; + ID2D1SolidColorBrush* m_d2dWhiteBrush; + + // DirectWrite drawing components. + IDWriteFactory5* m_dwriteFactory; + IDWriteFontFile* m_fontFile; + IDWriteFontSet* m_fontSet; + IDWriteFontSetBuilder1* m_fontSetBuilder; + IDWriteFontCollection1* m_fontCollection; + +}; + +#endif diff --git a/libretro/Makefile b/libretro/Makefile index 8970ddc036..28d145afed 100644 --- a/libretro/Makefile +++ b/libretro/Makefile @@ -85,6 +85,29 @@ else ifneq (,$(findstring rpi,$(platform))) PLATFORM_EXT := unix TARGET_ARCH = arm LDFLAGS += -lrt -ldl + +# ARM64 +else ifneq (,$(findstring arm64,$(platform))) + TARGET := $(TARGET_NAME)_libretro.so + TARGET_ARCH = arm64 + fpic := -fPIC + LDFLAGS += -shared -Wl,--version-script=link.T -Wl,--no-undefined + FFMPEGINCFLAGS += -I$(FFMPEGDIR)/linux/aarch64/include + FFMPEGLIBDIR := $(FFMPEGDIR)/linux/aarch64/lib + FFMPEGLDFLAGS += -L$(FFMPEGLIBDIR) -lavformat -lavcodec -lavutil -lswresample -lswscale + INCFLAGS += -I. + HAVE_NEON=1 + ifneq (,$(findstring gles,$(platform))) + GLES := 1 + GL_LIB := -lGLESv2 -lEGL + LDFLAGS += -lGLESv2 -lEGL + else + GL_LIB := -lGL + endif + CPUFLAGS += -D__arm64__ -DARM64_ASM -D__NEON_OPT + PLATCFLAGS += -DARM64 + LDFLAGS += -lrt -ldl + PLATFORM_EXT := unix # i.MX6 else ifneq (,$(findstring imx6,$(platform))) diff --git a/libretro/Makefile.common b/libretro/Makefile.common index d310bc0b81..0fa0187167 100644 --- a/libretro/Makefile.common +++ b/libretro/Makefile.common @@ -243,6 +243,7 @@ SOURCES_CXX += \ $(EXTDIR)/glslang/SPIRV/GlslangToSpv.cpp \ $(EXTDIR)/glslang/SPIRV/Logger.cpp \ $(EXTDIR)/glslang/SPIRV/SpvBuilder.cpp \ + $(EXTDIR)/glslang/SPIRV/SpvPostProcess.cpp \ $(EXTDIR)/SPIRV-Cross/spirv_cfg.cpp \ $(EXTDIR)/SPIRV-Cross/spirv_cross.cpp \ $(EXTDIR)/SPIRV-Cross/spirv_cross_util.cpp \