power_play/src/prof_tracy.h
2025-07-25 13:43:47 -05:00

158 lines
7.8 KiB
C

#ifndef PROF_H
#define PROF_H
#if PROFILING
#if COMPILER_MSVC
# error "MSVC not supported for profiling (cleanup attributes are required for profiling markup)"
#endif
#define PROFILING_SYSTEM_TRACE 0
#define PROFILING_CAPTURE_FRAME_IMAGE 0
#define PROFILING_LOCKS 0
#define PROFILING_GPU 1
#define PROFILING_GPU_STABLE_POWER_STATE 1
//#define PROFILER_THREAD_AFFINITY_MASK 0x000000000000F000ull
#define PROFILER_THREAD_AFFINITY_MASK 0
#define PROFILER_THREAD_PREFIX_WSTR L"Tracy"
#define PROFILING_FILE_WSTR L".tracy"
#define PROFILING_CMD_WSTR L"cmd /C start \"\" /wait tracy-capture.exe -o .tracy -a 127.0.0.1 && start \"\" tracy-profiler.exe .tracy"
//#define PROFILING_CMD_WSTR L"tracy-profiler.exe -a 127.0.0.1"
/* Tracy defines */
#define TRACY_ENABLE
#define TRACY_FIBERS
#if !PROFILING_SYSTEM_TRACE
# define TRACY_NO_CALLSTACK
# define TRACY_NO_SYSTEM_TRACING
#endif
/* Include tracy client */
#pragma clang diagnostic ignored "-Wshadow"
#pragma clang diagnostic ignored "-Wextra-semi"
#pragma clang diagnostic ignored "-Wextra-semi-stmt"
#pragma clang diagnostic ignored "-Wpointer-sign"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types-discards-qualifiers"
#include TRACY_CLIENT_HEADER_PATH
INLINE void __prof_zone_cleanup_func(TracyCZoneCtx *ctx) { TracyCZoneEnd(*ctx) }
#define __profnc(name, color) static const struct ___tracy_source_location_data CAT(__tracy_source_location,__LINE__) = { (name), __func__, __FILE__, (uint32_t)__LINE__, BGR32(color) }; __attribute((cleanup(__prof_zone_cleanup_func))) TracyCZoneCtx __tracy_zone_ctx = ___tracy_emit_zone_begin( &CAT(__tracy_source_location,__LINE__), 1 )
#define __profn(name) __profnc(name, 0)
#define __prof __profnc(0, 0)
#define __profvalue(v) TracyCZoneValue(__tracy_zone_ctx, (v))
#define __profalloc(ptr, size) TracyCAlloc((ptr), (size))
#define __proffree(ptr) TracyCFree((ptr))
#define __profmsg(txt, len, col) TracyCMessageC((txt), (len), BGR32(col))
#define __profframe(name) TracyCFrameMarkNamed((name))
#define __profthread(name, group_hint) TracyCSetThreadNameWithHint((name), (group_hint))
enum __prof_plot_type {
__prof_plot_type_number = TracyPlotFormatNumber,
__prof_plot_type_memory = TracyPlotFormatMemory,
__prof_plot_type_percentage = TracyPlotFormatPercentage,
__prof_plot_type_watt = TracyPlotFormatWatt
};
#define __prof_plot_init(name, type, step, fill, color) TracyCPlotConfig(name, type, step, fill, BGR32(color))
#define __prof_plot(name, val) TracyCPlot(name, val)
#define __prof_plot_i(name, val) TracyCPlotI(name, val)
#define __prof_is_connected() ___tracy_connected()
#else
#define PROFILING_CAPTURE_FRAME_IMAGE 0
#define PROFILING_LOCKS 0
#define PROFILING_GPU 0
#define __profnc(name, color)
#define __profn(name)
#define __prof
#define __profvalue(v)
#define __profalloc(ptr, size)
#define __proffree(ptr)
#define __profmsg(txt, len, col)
#define __profframe(name)
#define __profthread(name, group_hint)
#define __prof_plot_init(name, type, step, fill, color)
#define __prof_plot(name, val)
#define __prof_plot_i(name, val)
#define __prof_is_connected() 0
#endif /* PROFILING */
#if PROFILING_LOCKS
# define __proflock_ctx(name) struct TracyCSharedLockCtx *name
# define __proflock_alloc(ctx) TracyCSharedLockAnnounce((ctx))
# define __proflock_release(ctx) TracyCSharedLockTerminate((ctx))
# define __proflock_before_exclusive_lock(ctx) TracyCSharedLockBeforeExclusiveLock((ctx))
# define __proflock_after_exclusive_lock(ctx) TracyCSharedLockAfterExclusiveLock((ctx))
# define __proflock_after_exclusive_unlock(ctx) TracyCSharedLockAfterExclusiveUnlock((ctx))
# define __proflock_after_try_exclusive_lock(ctx, acquired) TracyCSharedLockAfterTryExclusiveLock((ctx), (acquired))
# define __proflock_before_shared_lock(ctx) TracyCSharedLockBeforeSharedLock((ctx))
# define __proflock_after_shared_lock(ctx) TracyCSharedLockAfterSharedLock((ctx))
# define __proflock_after_shared_unlock(ctx) TracyCSharedLockAfterSharedUnlock((ctx))
# define __proflock_after_try_shared_lock(ctx, acquired) TracyCSharedLockAfterTrySharedLock((ctx), (acquired))
# define __proflock_mark(ctx) TracyCSharedLockMark((ctx))
# define __proflock_custom_name(ctx, name, len) TracyCSharedLockCustomName((ctx), (name), (len))
#else
# define __proflock_alloc(ctx)
# define __proflock_release(ctx)
# define __proflock_before_exclusive_lock(ctx)
# define __proflock_after_exclusive_lock(ctx)
# define __proflock_after_exclusive_unlock(ctx)
# define __proflock_after_try_exclusive_lock(ctx, acquired)
# define __proflock_before_shared_lock(ctx)
# define __proflock_after_shared_lock(ctx)
# define __proflock_after_shared_unlock(ctx)
# define __proflock_after_try_shared_lock(ctx, acquired)
# define __proflock_mark(ctx)
# define __proflock_custom_name(ctx, name, len)
#endif /* PROFILING && PROFILING_LOCKS */
#if PROFILING_GPU
/* Dx11 */
INLINE void __prof_dx11_zone_cleanup_func(TracyCD3D11ZoneCtx *ctx) { ___tracy_d3d11_emit_zone_end(*ctx); }
# define __profnc_dx11(dx11_ctx, name, color) static const struct ___tracy_source_location_data CAT(__tracy_gpu_d3d11_source_location,__LINE__) = { name, __func__, __FILE__, (uint32_t)__LINE__, BGR32(color) }; __attribute((cleanup(__prof_dx11_zone_cleanup_func))) TracyCD3D11ZoneCtx __tracy_d3d11_zone_ctx; ___tracy_d3d11_emit_zone_begin( dx11_ctx, &__tracy_d3d11_zone_ctx, &CAT(__tracy_gpu_d3d11_source_location,__LINE__), 1)
# define __prof_dx11_ctx(name) struct TracyCD3D11Ctx *name
# define __prof_dx11_ctx_alloc(ctx, device, device_ctx, name, name_len) ctx = ___tracy_d3d11_context_announce(device, device_ctx, name, name_len)
# define __prof_dx11_ctx_release(ctx) ___tracy_d3d11_context_terminate(ctx)
# define __prof_dx11_collect(ctx) ___tracy_d3d11_context_collect(ctx)
/* Dx12 */
INLINE void __prof_dx12_zone_cleanup_func(TracyCD3D12ZoneCtx *ctx) { ___tracy_d3d12_emit_zone_end(*ctx); }
# define __profnc_dx12(dx12_ctx, cmd_list, name, color) static const struct ___tracy_source_location_data CAT(__tracy_gpu_d3d12_source_location,__LINE__) = { name, __func__, __FILE__, (uint32_t)__LINE__, BGR32(color) }; __attribute((cleanup(__prof_dx12_zone_cleanup_func))) TracyCD3D12ZoneCtx __tracy_d3d12_zone_ctx; ___tracy_d3d12_emit_zone_begin( dx12_ctx, cmd_list, &__tracy_d3d12_zone_ctx, &CAT(__tracy_gpu_d3d12_source_location,__LINE__), 1)
# define __prof_dx12_ctx(name) struct TracyCD3D12Ctx *name
# define __prof_dx12_ctx_alloc(ctx, device, queue, name, name_len) ctx = ___tracy_d3d12_context_announce(device, queue, name, name_len)
# define __prof_dx12_ctx_release(ctx) ___tracy_d3d12_context_terminate(ctx)
# define __prof_dx12_new_frame(ctx) ___tracy_d3d12_context_new_frame(ctx)
# define __prof_dx12_collect(ctx) ___tracy_d3d12_context_collect(ctx)
#else
# define __profnc_dx11(dx11_ctx, name, color)
# define __prof_dx11_ctx_alloc(ctx, device, device_ctx, name, name_len)
# define __prof_dx11_ctx_release(ctx)
# define __prof_dx11_collect(ctx)
# define __profnc_dx12(dx11_ctx, queue, name, color)
# define __prof_dx12_ctx_alloc(ctx, device, queue, name, name_len)
# define __prof_dx12_ctx_release(ctx)
# define __prof_dx12_new_frame(ctx)
# define __prof_dx12_collect(ctx)
#endif /* PROFILING_GPU */
#if PROFILING_CAPTURE_FRAME_IMAGE
# define __profframeimage(image, width, height, offset, flipped) TracyCFrameImage((image), (width), (height), (offset), (flipped))
#else
# define __profframeimage(image, width, height, offset, flipped)
#endif /* PROFILING_CAPTURE_FRAME_IMAGE */
#ifdef TRACY_FIBERS
/* Tracy fiber methods are wrapped in FORCE_NO_INLINE because otherwise issues can arise
* accross fiber context boundaries during optimization */
FORCE_NO_INLINE INLINE void __prof_fiber_enter(char *fiber_name, i32 profiler_group) { TracyCFiberEnterWithHint(fiber_name, profiler_group); }
FORCE_NO_INLINE INLINE void __prof_fiber_leave(void) { TracyCFiberLeave; }
#else
# define __prof_fiber_enter(fiber_name, profiler_group)
# define __prof_fiber_leave()
#endif
#endif