Skip to content

Commit

Permalink
Rearrange some Tracy-related files
Browse files Browse the repository at this point in the history
- This way, changes to `OpenGlTracyCapture` don't invalidate the entire ccache
  • Loading branch information
JesseTG committed Oct 7, 2024
1 parent 50d1294 commit 6b40c45
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 182 deletions.
9 changes: 8 additions & 1 deletion src/libretro/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/libretro/render/opengl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#ifdef HAVE_TRACY
#include "tracy.hpp"
#include "tracy/opengl.hpp"
#endif

namespace MelonDsDs {
Expand Down
157 changes: 4 additions & 153 deletions src/libretro/tracy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tracy/Tracy.hpp>
#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 <array>
#include <queue>

#include "PlatformOGLPrivate.h"
#include <tracy/TracyOpenGL.hpp>

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<GLuint, 4> _tracyTextures;
std::array<GLuint, 4> _tracyFbos;
std::array<GLuint, 4> _tracyPbos;
std::array<GLsync, 4> _tracyFences;
int _tracyIndex = 0;
std::queue<int> _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
144 changes: 144 additions & 0 deletions src/libretro/tracy/client.hpp
Original file line number Diff line number Diff line change
@@ -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 <tracy/Tracy.hpp>
#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 <tracy/TracyOpenGL.hpp>
#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
40 changes: 40 additions & 0 deletions src/libretro/tracy/memory.cpp
Original file line number Diff line number Diff line change
@@ -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 <tracy/Tracy.hpp>

// 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);
}
Loading

0 comments on commit 6b40c45

Please sign in to comment.