From f31f6c5a93f33839686f99464b2ed72317ea30cd Mon Sep 17 00:00:00 2001 From: Dennis Brakhane Date: Tue, 6 Aug 2024 06:06:25 +0200 Subject: [PATCH] add RAII style profiling (#910) --- WickedEngine/wiProfiler.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/WickedEngine/wiProfiler.h b/WickedEngine/wiProfiler.h index 56e103be0..fe92d7e8e 100644 --- a/WickedEngine/wiProfiler.h +++ b/WickedEngine/wiProfiler.h @@ -3,6 +3,20 @@ #include "wiCanvas.h" #include "wiColor.h" + +// QoL macros, allows writing just ScopedXxxProfiling without needing to declare a variable manually +#define ScopedCPUProfiling(name) wi::profiler::ScopedRangeCPU WI_PROFILER_CONCAT(_wi_profiler_cpu_range,__LINE__)(name) +#define ScopedGPUProfiling(name, cmd) wi::profiler::ScopedRangeGPU WI_PROFILER_CONCAT(_wi_profiler_gpu_range,__LINE__)(name, cmd) + +// same as ScopedXxxProfiling, just will automatically use the function name as name, should only be used at the beginning of a function +#define ScopedCPUProfilingF ScopedCPUProfiling(__FUNCTION__) +#define ScopedGPUProfilingF(cmd) ScopedGPUProfiling(__FUNCTION__, cmd) + +// internal helper macros to make somewhat unique variable names based on line numbers to prevent some compilers +// warning about shadowed variables +#define WI_PROFILER_CONCAT(x,y) WI_PROFILER_CONCAT_INDIRECT(x,y) +#define WI_PROFILER_CONCAT_INDIRECT(x,y) x##y + namespace wi::profiler { typedef size_t range_id; @@ -22,6 +36,22 @@ namespace wi::profiler // End a profiling range void EndRange(range_id id); + // helper using RAII to avoid having to manually call BeginRangeCPU/EndRange at beginning/end + struct ScopedRangeCPU + { + range_id id; + inline ScopedRangeCPU(const char* name) { id = BeginRangeCPU(name); } + inline ~ScopedRangeCPU() { EndRange(id); } + }; + + // same for BeginRangeGPU + struct ScopedRangeGPU + { + range_id id; + inline ScopedRangeGPU(const char* name, wi::graphics::CommandList cmd) { id = BeginRangeGPU(name, cmd); } + inline ~ScopedRangeGPU() { EndRange(id); } + }; + // Renders a basic text of the Profiling results to the (x,y) screen coordinate void DrawData( const wi::Canvas& canvas,