diff --git a/src/libretro/CMakeLists.txt b/src/libretro/CMakeLists.txt index d96a663..7e4b5a3 100644 --- a/src/libretro/CMakeLists.txt +++ b/src/libretro/CMakeLists.txt @@ -96,9 +96,12 @@ add_library(melondsds_libretro ${LIBRARY_TYPE} sram.cpp sram.hpp tracy.hpp + tracy/client.hpp + tracy/opengl.hpp utils.cpp utils.hpp ../pntr/pntr.c + tracy/memory.cpp ) target_include_directories(melondsds_libretro SYSTEM PUBLIC @@ -175,7 +178,11 @@ if (HAVE_NETWORKING) endif () if (TRACY_ENABLE) - target_sources(melondsds_libretro PRIVATE tracy.cpp) + target_sources(melondsds_libretro PRIVATE tracy/memory.cpp) + + if (HAVE_OPENGL OR HAVE_OPENGLES) + target_sources(melondsds_libretro PRIVATE tracy/opengl.cpp) + endif() endif () if (HAVE_OPENGL OR HAVE_OPENGLES) diff --git a/src/libretro/render/opengl.hpp b/src/libretro/render/opengl.hpp index 18d92a9..8afe9e1 100644 --- a/src/libretro/render/opengl.hpp +++ b/src/libretro/render/opengl.hpp @@ -30,6 +30,7 @@ #ifdef HAVE_TRACY #include "tracy.hpp" +#include "tracy/opengl.hpp" #endif namespace MelonDsDs { diff --git a/src/libretro/tracy.hpp b/src/libretro/tracy.hpp index 77a868b..291a524 100644 --- a/src/libretro/tracy.hpp +++ b/src/libretro/tracy.hpp @@ -17,158 +17,9 @@ #ifndef MELONDS_DS_TRACY_HPP #define MELONDS_DS_TRACY_HPP -#if defined(__clang__) || defined(__GNUC__) -# define TracyFunction __PRETTY_FUNCTION__ -#elif defined(_MSC_VER) -# define TracyFunction __FUNCSIG__ -#endif - -#ifdef HAVE_TRACY - -#include -#else -#define ZoneNamed(x,y) -#define ZoneNamedN(x,y,z) -#define ZoneNamedC(x,y,z) -#define ZoneNamedNC(x,y,z,w) - -#define ZoneTransient(x,y) -#define ZoneTransientN(x,y,z) - -#define ZoneScoped -#define ZoneScopedN(x) -#define ZoneScopedC(x) -#define ZoneScopedNC(x,y) - -#define ZoneText(x,y) -#define ZoneTextV(x,y,z) -#define ZoneName(x,y) -#define ZoneNameV(x,y,z) -#define ZoneColor(x) -#define ZoneColorV(x,y) -#define ZoneValue(x) -#define ZoneValueV(x,y) -#define ZoneIsActive false -#define ZoneIsActiveV(x) false - -#define FrameMark -#define FrameMarkNamed(x) -#define FrameMarkStart(x) -#define FrameMarkEnd(x) - -#define FrameImage(x,y,z,w,a) - -#define TracyLockable( type, varname ) type varname -#define TracyLockableN( type, varname, desc ) type varname -#define TracySharedLockable( type, varname ) type varname -#define TracySharedLockableN( type, varname, desc ) type varname -#define LockableBase( type ) type -#define SharedLockableBase( type ) type -#define LockMark(x) (void)x -#define LockableName(x,y,z) - -#define TracyPlot(x,y) -#define TracyPlotConfig(x,y,z,w,a) - -#define TracyMessage(x,y) -#define TracyMessageL(x) -#define TracyMessageC(x,y,z) -#define TracyMessageLC(x,y) -#define TracyAppInfo(x,y) - -#define TracyAlloc(x,y) -#define TracyFree(x) -#define TracySecureAlloc(x,y) -#define TracySecureFree(x) - -#define TracyAllocN(x,y,z) -#define TracyFreeN(x,y) -#define TracySecureAllocN(x,y,z) -#define TracySecureFreeN(x,y) - -#define ZoneNamedS(x,y,z) -#define ZoneNamedNS(x,y,z,w) -#define ZoneNamedCS(x,y,z,w) -#define ZoneNamedNCS(x,y,z,w,a) - -#define ZoneTransientS(x,y,z) -#define ZoneTransientNS(x,y,z,w) - -#define ZoneScopedS(x) -#define ZoneScopedNS(x,y) -#define ZoneScopedCS(x,y) -#define ZoneScopedNCS(x,y,z) - -#define TracyAllocS(x,y,z) -#define TracyFreeS(x,y) -#define TracySecureAllocS(x,y,z) -#define TracySecureFreeS(x,y) - -#define TracyAllocNS(x,y,z,w) -#define TracyFreeNS(x,y,z) -#define TracySecureAllocNS(x,y,z,w) -#define TracySecureFreeNS(x,y,z) - -#define TracyMessageS(x,y,z) -#define TracyMessageLS(x,y) -#define TracyMessageCS(x,y,z,w) -#define TracyMessageLCS(x,y,z) - -#define TracySourceCallbackRegister(x,y) -#define TracyParameterRegister(x,y) -#define TracyParameterSetup(x,y,z,w) -#define TracyIsConnected false -#define TracySetProgramName(x) - -#define TracyFiberEnter(x) -#define TracyFiberLeave -#endif - -#if defined(HAVE_TRACY) && (defined(HAVE_OPENGL) || defined(HAVE_OPENGLES)) -#include -#include - -#include "PlatformOGLPrivate.h" -#include - -namespace MelonDsDs { - /// \brief Class for capturing OpenGL frames for Tracy. - /// Suitable for both OpenGL renderers. - class OpenGlTracyCapture { - public: - OpenGlTracyCapture(); - ~OpenGlTracyCapture() noexcept; - - // Copying the OpenGL objects is too much of a hassle. - OpenGlTracyCapture(const OpenGlTracyCapture&) = delete; - OpenGlTracyCapture& operator=(const OpenGlTracyCapture&) = delete; - OpenGlTracyCapture(OpenGlTracyCapture&&) = delete; - OpenGlTracyCapture& operator=(OpenGlTracyCapture&&) = delete; - void CaptureFrame(float scale) noexcept; - private: - std::array _tracyTextures; - std::array _tracyFbos; - std::array _tracyPbos; - std::array _tracyFences; - int _tracyIndex = 0; - std::queue _tracyQueue; - }; -} -#else -#define TracyGpuContext -#define TracyGpuContextName(x,y) -#define TracyGpuNamedZone(x,y,z) -#define TracyGpuNamedZoneC(x,y,z,w) -#define TracyGpuZone(x) -#define TracyGpuZoneC(x,y) -#define TracyGpuZoneTransient(x,y,z) -#define TracyGpuCollect - -#define TracyGpuNamedZoneS(x,y,z,w) -#define TracyGpuNamedZoneCS(x,y,z,w,a) -#define TracyGpuZoneS(x,y) -#define TracyGpuZoneCS(x,y,z) -#define TracyGpuZoneTransientS(x,y,z,w) -#endif +#include "tracy/client.hpp" +// All Tracy-related declarations were originally in this header, +// but I moved them to a new directory to keep the codebase clean. +// This file still exists to keep the diff smaller. #endif //MELONDS_DS_TRACY_HPP diff --git a/src/libretro/tracy/client.hpp b/src/libretro/tracy/client.hpp new file mode 100644 index 0000000..5832cef --- /dev/null +++ b/src/libretro/tracy/client.hpp @@ -0,0 +1,144 @@ +/* + Copyright 2024 Jesse Talavera + + melonDS DS is free software: you can redistribute it and/or modify it under + the terms of the GNU General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + melonDS DS is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with melonDS DS. If not, see http://www.gnu.org/licenses/. +*/ + +#pragma once + + +#if defined(__clang__) || defined(__GNUC__) +# define TracyFunction __PRETTY_FUNCTION__ +#elif defined(_MSC_VER) +# define TracyFunction __FUNCSIG__ +#endif + +#ifdef HAVE_TRACY +#include +#else +#define ZoneNamed(x,y) +#define ZoneNamedN(x,y,z) +#define ZoneNamedC(x,y,z) +#define ZoneNamedNC(x,y,z,w) + +#define ZoneTransient(x,y) +#define ZoneTransientN(x,y,z) + +#define ZoneScoped +#define ZoneScopedN(x) +#define ZoneScopedC(x) +#define ZoneScopedNC(x,y) + +#define ZoneText(x,y) +#define ZoneTextV(x,y,z) +#define ZoneName(x,y) +#define ZoneNameV(x,y,z) +#define ZoneColor(x) +#define ZoneColorV(x,y) +#define ZoneValue(x) +#define ZoneValueV(x,y) +#define ZoneIsActive false +#define ZoneIsActiveV(x) false + +#define FrameMark +#define FrameMarkNamed(x) +#define FrameMarkStart(x) +#define FrameMarkEnd(x) + +#define FrameImage(x,y,z,w,a) + +#define TracyLockable( type, varname ) type varname +#define TracyLockableN( type, varname, desc ) type varname +#define TracySharedLockable( type, varname ) type varname +#define TracySharedLockableN( type, varname, desc ) type varname +#define LockableBase( type ) type +#define SharedLockableBase( type ) type +#define LockMark(x) (void)x +#define LockableName(x,y,z) + +#define TracyPlot(x,y) +#define TracyPlotConfig(x,y,z,w,a) + +#define TracyMessage(x,y) +#define TracyMessageL(x) +#define TracyMessageC(x,y,z) +#define TracyMessageLC(x,y) +#define TracyAppInfo(x,y) + +#define TracyAlloc(x,y) +#define TracyFree(x) +#define TracySecureAlloc(x,y) +#define TracySecureFree(x) + +#define TracyAllocN(x,y,z) +#define TracyFreeN(x,y) +#define TracySecureAllocN(x,y,z) +#define TracySecureFreeN(x,y) + +#define ZoneNamedS(x,y,z) +#define ZoneNamedNS(x,y,z,w) +#define ZoneNamedCS(x,y,z,w) +#define ZoneNamedNCS(x,y,z,w,a) + +#define ZoneTransientS(x,y,z) +#define ZoneTransientNS(x,y,z,w) + +#define ZoneScopedS(x) +#define ZoneScopedNS(x,y) +#define ZoneScopedCS(x,y) +#define ZoneScopedNCS(x,y,z) + +#define TracyAllocS(x,y,z) +#define TracyFreeS(x,y) +#define TracySecureAllocS(x,y,z) +#define TracySecureFreeS(x,y) + +#define TracyAllocNS(x,y,z,w) +#define TracyFreeNS(x,y,z) +#define TracySecureAllocNS(x,y,z,w) +#define TracySecureFreeNS(x,y,z) + +#define TracyMessageS(x,y,z) +#define TracyMessageLS(x,y) +#define TracyMessageCS(x,y,z,w) +#define TracyMessageLCS(x,y,z) + +#define TracySourceCallbackRegister(x,y) +#define TracyParameterRegister(x,y) +#define TracyParameterSetup(x,y,z,w) +#define TracyIsConnected false +#define TracySetProgramName(x) + +#define TracyFiberEnter(x) +#define TracyFiberLeave +#endif + +#if defined(HAVE_TRACY) && (defined(HAVE_OPENGL) || defined(HAVE_OPENGLES)) +#include "PlatformOGLPrivate.h" +#include +#else +#define TracyGpuContext +#define TracyGpuContextName(x,y) +#define TracyGpuNamedZone(x,y,z) +#define TracyGpuNamedZoneC(x,y,z,w) +#define TracyGpuZone(x) +#define TracyGpuZoneC(x,y) +#define TracyGpuZoneTransient(x,y,z) +#define TracyGpuCollect + +#define TracyGpuNamedZoneS(x,y,z,w) +#define TracyGpuNamedZoneCS(x,y,z,w,a) +#define TracyGpuZoneS(x,y) +#define TracyGpuZoneCS(x,y,z) +#define TracyGpuZoneTransientS(x,y,z,w) +#endif \ No newline at end of file diff --git a/src/libretro/tracy/memory.cpp b/src/libretro/tracy/memory.cpp new file mode 100644 index 0000000..ea60cc5 --- /dev/null +++ b/src/libretro/tracy/memory.cpp @@ -0,0 +1,40 @@ +/* + Copyright 2024 Jesse Talavera + + melonDS DS is free software: you can redistribute it and/or modify it under + the terms of the GNU General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + melonDS DS is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with melonDS DS. If not, see http://www.gnu.org/licenses/. +*/ + +#include + +// Defining these functions in the global scope +// overrides operator new and operator delete +// for all linked translation units. + +void* operator new(std::size_t count) +{ + if (count == 0) + ++count; // avoid std::malloc(0) which may return nullptr on success + + if (void *ptr = std::malloc(count)) { + TracySecureAlloc(ptr, count); + return ptr; + } + + throw std::bad_alloc{}; // required by [new.delete.single]/3 +} + +void operator delete(void* ptr) noexcept +{ + TracySecureFree(ptr); + std::free(ptr); +} diff --git a/src/libretro/tracy.cpp b/src/libretro/tracy/opengl.cpp similarity index 88% rename from src/libretro/tracy.cpp rename to src/libretro/tracy/opengl.cpp index bd0056b..663c365 100644 --- a/src/libretro/tracy.cpp +++ b/src/libretro/tracy/opengl.cpp @@ -1,5 +1,5 @@ /* - Copyright 2023 Jesse Talavera-Greenberg + Copyright 2024 Jesse Talavera melonDS DS is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free @@ -14,32 +14,9 @@ with melonDS DS. If not, see http://www.gnu.org/licenses/. */ -#include "tracy.hpp" - -#if defined(HAVE_OPENGL) || defined(HAVE_OPENGLES) +#include "opengl.hpp" #include "screenlayout.hpp" -#endif -void* operator new(std::size_t count) -{ - if (count == 0) - ++count; // avoid std::malloc(0) which may return nullptr on success - - if (void *ptr = std::malloc(count)) { - TracySecureAlloc(ptr, count); - return ptr; - } - - throw std::bad_alloc{}; // required by [new.delete.single]/3 -} - -void operator delete(void* ptr) noexcept -{ - TracySecureFree(ptr); - std::free(ptr); -} - -#if defined(HAVE_OPENGL) || defined(HAVE_OPENGLES) MelonDsDs::OpenGlTracyCapture::OpenGlTracyCapture() { if (!tracy::ProfilerAvailable()) { throw std::runtime_error("Tracy not available"); @@ -140,6 +117,4 @@ void MelonDsDs::OpenGlTracyCapture::CaptureFrame(float scale) noexcept { _tracyFences[_tracyIndex] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); _tracyQueue.push(_tracyIndex); _tracyIndex = (_tracyIndex + 1) % 4; -} - -#endif // defined(HAVE_OPENGL) || defined(HAVE_OPENGLES) \ No newline at end of file +} \ No newline at end of file diff --git a/src/libretro/tracy/opengl.hpp b/src/libretro/tracy/opengl.hpp new file mode 100644 index 0000000..c2e0e8a --- /dev/null +++ b/src/libretro/tracy/opengl.hpp @@ -0,0 +1,49 @@ +/* + Copyright 2024 Jesse Talavera + + melonDS DS is free software: you can redistribute it and/or modify it under + the terms of the GNU General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + melonDS DS is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with melonDS DS. If not, see http://www.gnu.org/licenses/. +*/ + +#pragma once + +#if defined(HAVE_TRACY) && (defined(HAVE_OPENGL) || defined(HAVE_OPENGLES)) +#include +#include + +#include "PlatformOGLPrivate.h" +#include + +namespace MelonDsDs { + /// \brief Class for capturing OpenGL frames for Tracy. + /// Suitable for both OpenGL renderers. + class OpenGlTracyCapture { + public: + OpenGlTracyCapture(); + ~OpenGlTracyCapture() noexcept; + + // Copying the OpenGL objects is too much of a hassle. + OpenGlTracyCapture(const OpenGlTracyCapture&) = delete; + OpenGlTracyCapture& operator=(const OpenGlTracyCapture&) = delete; + OpenGlTracyCapture(OpenGlTracyCapture&&) = delete; + OpenGlTracyCapture& operator=(OpenGlTracyCapture&&) = delete; + void CaptureFrame(float scale) noexcept; + private: + std::array _tracyTextures; + std::array _tracyFbos; + std::array _tracyPbos; + std::array _tracyFences; + int _tracyIndex = 0; + std::queue _tracyQueue; + }; +} +#endif \ No newline at end of file