Browse Source

Update tracy integration, allow parent projects to include RmlUi profiling markers, solves #516

Tracy client can now be included in one of three ways, in prioritized order:
1. Using target Tracy::TracyClient from parent project.
2. As a config package.
3. With Tracy source files located in 'Dependencies/Tracy'.

Adds three new CMake options:
- `RMLUI_TRACY_PROFILING`: Enable profiling, replaces the old option `ENABLE_TRACY_PROFILING`.
- `RMLUI_TRACY_MEMORY_PROFILING`: Overloads global operator new/delete for memory inspection.
- `RMLUI_TRACY_CONFIGURATION`: Only relevant for multi-config generators. If enabled, adds Tracy as a separate configuration, otherwise adds Tracy to all configurations.
Michael Ragazzon 2 years ago
parent
commit
3b75dcaa84

+ 0 - 20
CMake/Modules/FindTracy.cmake

@@ -1,20 +0,0 @@
-# - Try to find Tracy
-# Once done, this will define
-#
-#  TRACY_FOUND - system has Tracy
-#  TRACY_INCLUDE_DIR - the Tracy include directory
-
-set(TRACY_DIR "" CACHE PATH "Parent directory of Tracy library")
-
-find_path(TRACY_INCLUDE_DIR TracyClient.cpp PATHS ${TRACY_DIR} $ENV{TRACY_DIR} PATH_SUFFIXES tracy public tracy/public)
-
-set(TRACY_FOUND "NO")
-if(TRACY_INCLUDE_DIR)
-	message(STATUS "Found Tracy ${TRACY_INCLUDE_DIR}...")
-	mark_as_advanced(TRACY_DIR TRACY_INCLUDE_DIR)
-	set(TRACY_FOUND "YES")
-elseif(Tracy_FIND_REQUIRED)
-	 message(FATAL_ERROR "Required library Tracy not found! Install the library and try again. If the library is already installed, set the missing variables manually in cmake.")
-else()
-	message(STATUS "Library Tracy not found...")
-endif()

+ 52 - 15
CMakeLists.txt

@@ -254,29 +254,64 @@ else()
 	endif()
 	endif()
 endif()
 endif()
 
 
-option(ENABLE_TRACY_PROFILING "Enable profiling with Tracy. Source files can be placed in Dependencies/tracy." OFF)
-if( ENABLE_TRACY_PROFILING )
-	find_package(Tracy REQUIRED)
+function(EnableConfigurationType name enable)
+	if(enable)
+		list(APPEND CMAKE_CONFIGURATION_TYPES "${name}")
+		list(REMOVE_DUPLICATES CMAKE_CONFIGURATION_TYPES)
+	else()
+		list(REMOVE_ITEM CMAKE_CONFIGURATION_TYPES "${name}")
+	endif()
+	set(CMAKE_CONFIGURATION_TYPES "${CMAKE_CONFIGURATION_TYPES}" CACHE STRING "List of configurations to enable" FORCE)
+endfunction()
 
 
-	include_directories(${TRACY_INCLUDE_DIR})
+option(RMLUI_TRACY_PROFILING "Enable profiling with Tracy. Source files can be placed in Dependencies/tracy." OFF)
+if( RMLUI_TRACY_PROFILING )
+	option(RMLUI_TRACY_MEMORY_PROFILING "Overload global operator new/delete to track memory allocations in Tracy." ON)
 
 
 	if( CMAKE_CONFIGURATION_TYPES )
 	if( CMAKE_CONFIGURATION_TYPES )
-		list(APPEND CMAKE_CONFIGURATION_TYPES Tracy)
-		list(REMOVE_DUPLICATES CMAKE_CONFIGURATION_TYPES)
-		set(CMAKE_CONFIGURATION_TYPES "${CMAKE_CONFIGURATION_TYPES}" CACHE STRING
-			"Add the configurations that we need"
-			FORCE)
-		set(CMAKE_C_FLAGS_TRACY "${CMAKE_CXX_FLAGS_RELEASE} -DRMLUI_ENABLE_PROFILING")
-		set(CMAKE_CXX_FLAGS_TRACY "${CMAKE_CXX_FLAGS_RELEASE} -DRMLUI_ENABLE_PROFILING")
-		set(CMAKE_EXE_LINKER_FLAGS_TRACY "${CMAKE_EXE_LINKER_FLAGS_RELEASE}")
-		set(CMAKE_SHARED_LINKER_FLAGS_TRACY "${CMAKE_SHARED_LINKER_FLAGS_RELEASE}")
+		option(RMLUI_TRACY_CONFIGURATION "Enable a separate Tracy configuration type for multi-config generators such as Visual Studio, otherwise enable Tracy in all configurations." ON)
+
+		if( RMLUI_TRACY_CONFIGURATION )
+			EnableConfigurationType(Tracy ON)
+			list(APPEND CMAKE_MAP_IMPORTED_CONFIG_TRACY Release)
+			set(CMAKE_C_FLAGS_TRACY "${CMAKE_C_FLAGS_RELEASE}")
+			set(CMAKE_CXX_FLAGS_TRACY "${CMAKE_CXX_FLAGS_RELEASE}")
+			set(CMAKE_EXE_LINKER_FLAGS_TRACY "${CMAKE_EXE_LINKER_FLAGS_RELEASE}")
+			set(CMAKE_SHARED_LINKER_FLAGS_TRACY "${CMAKE_SHARED_LINKER_FLAGS_RELEASE}")
+		else()
+			EnableConfigurationType(Tracy OFF)
+		endif()
+	endif()
+
+	if(NOT TARGET Tracy::TracyClient)
+		find_package(Tracy QUIET)
+	endif()
+
+	if(NOT TARGET Tracy::TracyClient)
+		message("Trying to add Tracy from subdirectory 'Dependencies/tracy'.")
+		add_subdirectory("Dependencies/tracy")
+	endif()
+
+	if(NOT TARGET Tracy::TracyClient)
+		message(FATAL_ERROR "Tracy client not found. Either (a) make sure target Tracy::TracyClient is available from parent project,"
+			"(b) Tracy can be found as a config package, or (c) Tracy source files are located in 'Dependencies/Tracy'.")
+	endif()
+
+	if( CMAKE_CONFIGURATION_TYPES AND RMLUI_TRACY_CONFIGURATION )
 		message("-- Tracy profiling enabled in configuration 'Tracy'.")
 		message("-- Tracy profiling enabled in configuration 'Tracy'.")
+		set(RMLUI_TRACY_CONDITION "$<CONFIG:Tracy>")
 	else()
 	else()
 		message("-- Tracy profiling enabled.")
 		message("-- Tracy profiling enabled.")
-		list(APPEND CORE_PUBLIC_DEFS -DRMLUI_ENABLE_PROFILING)
+		set(RMLUI_TRACY_CONDITION "1")
+	endif()
+
+	list(APPEND CORE_PUBLIC_LINK_LIBS "$<${RMLUI_TRACY_CONDITION}:Tracy::TracyClient>")
+	list(APPEND CORE_PUBLIC_DEFS "$<${RMLUI_TRACY_CONDITION}:RMLUI_TRACY_PROFILING>")
+	if(RMLUI_TRACY_MEMORY_PROFILING)
+		list(APPEND CORE_PRIVATE_DEFS "$<${RMLUI_TRACY_CONDITION}:RMLUI_TRACY_MEMORY_PROFILING>")
 	endif()
 	endif()
 elseif( CMAKE_CONFIGURATION_TYPES )
 elseif( CMAKE_CONFIGURATION_TYPES )
-	list(REMOVE_ITEM CMAKE_CONFIGURATION_TYPES Tracy)
+	EnableConfigurationType(Tracy OFF)
 endif()
 endif()
 
 
 option(ENABLE_LOTTIE_PLUGIN "Enable plugin for Lottie animations. Requires the rlottie library." OFF)
 option(ENABLE_LOTTIE_PLUGIN "Enable plugin for Lottie animations. Requires the rlottie library." OFF)
@@ -600,12 +635,14 @@ if(NOT BUILD_FRAMEWORK)
 	target_include_directories(RmlCore PRIVATE ${CORE_INCLUDE_DIRS})
 	target_include_directories(RmlCore PRIVATE ${CORE_INCLUDE_DIRS})
 	target_include_directories(RmlCore INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/Include> $<INSTALL_INTERFACE:include>)
 	target_include_directories(RmlCore INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/Include> $<INSTALL_INTERFACE:include>)
 	target_link_libraries(RmlCore PRIVATE ${CORE_LINK_LIBS})
 	target_link_libraries(RmlCore PRIVATE ${CORE_LINK_LIBS})
+	target_link_libraries(RmlCore PUBLIC ${CORE_PUBLIC_LINK_LIBS})
 	target_link_libraries(RmlDebugger RmlCore)
 	target_link_libraries(RmlDebugger RmlCore)
 	target_compile_definitions(RmlCore PRIVATE ${CORE_PRIVATE_DEFS})
 	target_compile_definitions(RmlCore PRIVATE ${CORE_PRIVATE_DEFS})
 	target_compile_definitions(RmlCore PUBLIC ${CORE_PUBLIC_DEFS})
 	target_compile_definitions(RmlCore PUBLIC ${CORE_PUBLIC_DEFS})
 else()
 else()
 	target_include_directories(RmlUi PRIVATE ${CORE_INCLUDE_DIRS})
 	target_include_directories(RmlUi PRIVATE ${CORE_INCLUDE_DIRS})
 	target_link_libraries(RmlUi PRIVATE ${CORE_LINK_LIBS})
 	target_link_libraries(RmlUi PRIVATE ${CORE_LINK_LIBS})
+	target_link_libraries(RmlUi PUBLIC ${CORE_PUBLIC_LINK_LIBS})
 	target_compile_definitions(RmlUi PRIVATE ${CORE_PRIVATE_DEFS})
 	target_compile_definitions(RmlUi PRIVATE ${CORE_PRIVATE_DEFS})
 	target_compile_definitions(RmlUi PUBLIC ${CORE_PUBLIC_DEFS})
 	target_compile_definitions(RmlUi PUBLIC ${CORE_PUBLIC_DEFS})
 endif()
 endif()

+ 1 - 2
Include/RmlUi/Core/Profiling.h

@@ -29,9 +29,8 @@
 #ifndef RMLUI_CORE_PROFILING_H
 #ifndef RMLUI_CORE_PROFILING_H
 #define RMLUI_CORE_PROFILING_H
 #define RMLUI_CORE_PROFILING_H
 
 
-#ifdef RMLUI_ENABLE_PROFILING
+#ifdef RMLUI_TRACY_PROFILING
 
 
-	#define TRACY_ENABLE
 	#include <tracy/Tracy.hpp>
 	#include <tracy/Tracy.hpp>
 
 
 	#define RMLUI_ZoneNamed(varname, active) ZoneNamed(varname, active)
 	#define RMLUI_ZoneNamed(varname, active) ZoneNamed(varname, active)

+ 2 - 2
Source/Core/Element.cpp

@@ -153,7 +153,7 @@ Element::~Element()
 
 
 void Element::Update(float dp_ratio, Vector2f vp_dimensions)
 void Element::Update(float dp_ratio, Vector2f vp_dimensions)
 {
 {
-#ifdef RMLUI_ENABLE_PROFILING
+#ifdef RMLUI_TRACY_PROFILING
 	auto name = GetAddress(false, false);
 	auto name = GetAddress(false, false);
 	RMLUI_ZoneScoped;
 	RMLUI_ZoneScoped;
 	RMLUI_ZoneText(name.c_str(), name.size());
 	RMLUI_ZoneText(name.c_str(), name.size());
@@ -213,7 +213,7 @@ void Element::UpdateProperties(const float dp_ratio, const Vector2f vp_dimension
 
 
 void Element::Render()
 void Element::Render()
 {
 {
-#ifdef RMLUI_ENABLE_PROFILING
+#ifdef RMLUI_TRACY_PROFILING
 	auto name = GetAddress(false, false);
 	auto name = GetAddress(false, false);
 	RMLUI_ZoneScoped;
 	RMLUI_ZoneScoped;
 	RMLUI_ZoneText(name.c_str(), name.size());
 	RMLUI_ZoneText(name.c_str(), name.size());

+ 2 - 2
Source/Core/Layout/BlockFormattingContext.cpp

@@ -116,7 +116,7 @@ UniquePtr<LayoutBox> BlockFormattingContext::Format(ContainerBox* parent_contain
 {
 {
 	RMLUI_ASSERT(parent_container && element);
 	RMLUI_ASSERT(parent_container && element);
 
 
-#ifdef RMLUI_ENABLE_PROFILING
+#ifdef RMLUI_TRACY_PROFILING
 	RMLUI_ZoneScopedC(0xB22222);
 	RMLUI_ZoneScopedC(0xB22222);
 	auto name = CreateString(80, "%s %x", element->GetAddress(false, false).c_str(), element);
 	auto name = CreateString(80, "%s %x", element->GetAddress(false, false).c_str(), element);
 	RMLUI_ZoneName(name.c_str(), name.size());
 	RMLUI_ZoneName(name.c_str(), name.size());
@@ -212,7 +212,7 @@ bool BlockFormattingContext::FormatInlineBox(BlockContainer* parent_container, E
 
 
 bool BlockFormattingContext::FormatBlockContainerChild(BlockContainer* parent_container, Element* element)
 bool BlockFormattingContext::FormatBlockContainerChild(BlockContainer* parent_container, Element* element)
 {
 {
-#ifdef RMLUI_ENABLE_PROFILING
+#ifdef RMLUI_TRACY_PROFILING
 	RMLUI_ZoneScoped;
 	RMLUI_ZoneScoped;
 	auto name = CreateString(80, ">%s %x", element->GetAddress(false, false).c_str(), element);
 	auto name = CreateString(80, ">%s %x", element->GetAddress(false, false).c_str(), element);
 	RMLUI_ZoneName(name.c_str(), name.size());
 	RMLUI_ZoneName(name.c_str(), name.size());

+ 1 - 2
Source/Core/Profiling.cpp

@@ -28,8 +28,7 @@
 
 
 #include "../../Include/RmlUi/Core/Profiling.h"
 #include "../../Include/RmlUi/Core/Profiling.h"
 
 
-#ifdef RMLUI_ENABLE_PROFILING
-	#include <TracyClient.cpp>
+#ifdef RMLUI_TRACY_MEMORY_PROFILING
 	#include <memory>
 	#include <memory>
 
 
 void* operator new(std::size_t n)
 void* operator new(std::size_t n)