$darkmode
Suckless OGL 1.0.0
A lean, high-performance C11 PBR Renderer
gpu_profiler.h
Go to the documentation of this file.
1 #ifndef GPU_PROFILER_H
2 #define GPU_PROFILER_H
3 
4 #include "adaptive_sampler.h"
5 #include "metric_stack.h"
6 #include "perf_timer.h"
7 #include <stdint.h>
8 
9 #ifdef TRACY_ENABLE
10 #include "../deps/tracy/public/tracy/TracyC.h"
11 #include "tracy_gpu.h"
12 #endif
13 
14 enum {
18 };
19 
20 /* --- Profiler Colors (Nord Theme) --- */
21 static const int GPU_PROFILER_TOTAL_FRAME_COLOR = 0xECEFF4;
22 static const int GPU_PROFILER_ENV_COLOR = 0x88C0D0;
23 static const int GPU_PROFILER_SCENE_COLOR = 0xD08770;
24 static const int GPU_PROFILER_AUTO_EXPOSURE_COLOR = 0xEBCB8B;
25 static const int GPU_PROFILER_BLOOM_COLOR = 0x5E81AC;
26 static const int GPU_PROFILER_DOF_COLOR = 0xA3BE8C;
27 static const int GPU_PROFILER_MOTION_BLUR_COLOR = 0xBF616A;
28 static const int GPU_PROFILER_COMPOSITE_COLOR = 0x81A1C1;
29 static const int GPU_PROFILER_POSTPROCESS_COLOR = 0xB48EAD;
30 static const int GPU_PROFILER_UI_COLOR = 0x4C566A;
31 static const int GPU_PROFILER_GI_SYNC_COLOR = 0x8FBCBB;
32 static const int GPU_PROFILER_GI_DEBUG_COLOR = 0xB48EAD;
33 
34 /* --- Capture Settings --- */
35 static const float GPU_PROFILER_WINDOW_DURATION_S = 0.5F;
36 static const float GPU_PROFILER_WINDOW_TRANSITION_S = 0.2F;
37 static const float GPU_PROFILER_ROW_HEIGHT = 28.0F;
38 
39 #include "app_settings.h"
40 
45 typedef struct {
46  char name[MAX_GPU_STAGE_NAME];
47  uint32_t color;
51  float duration_ms;
54  float alpha;
55  float prev_alpha;
56  int depth;
58 } GPUStage;
59 
64 typedef struct {
65  char name[MAX_GPU_STAGE_NAME];
66  uint32_t color;
67  int depth;
69 } GPUStageInfo;
70 
75 typedef struct {
79  uint64_t frame_index;
81 
86 typedef struct {
94  // Double buffering for queries
98  int read_index;
100  // State tracking for the current frame
105  bool enabled;
106 } GPUProfiler;
107 
112 void gpu_profiler_init(GPUProfiler* profiler);
113 
118 void gpu_profiler_cleanup(GPUProfiler* profiler);
119 
126 void gpu_profiler_begin_frame(GPUProfiler* profiler, uint64_t frame_index);
127 
133 void gpu_profiler_set_enabled(GPUProfiler* profiler, bool enabled);
134 
141 void gpu_profiler_start_stage(GPUProfiler* profiler, const char* name,
142  uint32_t color);
143 
148 void gpu_profiler_end_stage(GPUProfiler* profiler);
149 
153 void gpu_profiler_reset_samplers(GPUProfiler* profiler, double current_time);
154 
159 typedef struct {
161 #ifdef TRACY_ENABLE
162  TracyCZoneCtx tracy_ctx;
163  void* tracy_gpu_ctx;
164 #endif
165 } GPUStageRAII;
166 
168 static inline void gpu_stage_cleanup_raii(GPUStageRAII* stage_raii)
169 {
170  gpu_profiler_end_stage(stage_raii->profiler);
171 #ifdef TRACY_ENABLE
172  TracyCZoneEnd(stage_raii->tracy_ctx);
173  tracy_gpu_zone_end(stage_raii->tracy_gpu_ctx);
174 #endif
175 }
176 
180 #ifdef TRACY_ENABLE
181 #define GPU_STAGE_PROFILER(profiler_ptr, name, color) \
182  TracyCZoneN(_tracy_ctx##__LINE__, name, 1); \
183  void* _tracy_gpu_ctx##__LINE__ = \
184  tracy_gpu_zone_begin(name, __func__, __FILE__, __LINE__, color); \
185  GPUStageRAII _stage_raii##__LINE__ \
186  __attribute__((cleanup(gpu_stage_cleanup_raii))) \
187  __attribute__((unused)) = {profiler_ptr, _tracy_ctx##__LINE__, \
188  _tracy_gpu_ctx##__LINE__}; \
189  gpu_profiler_start_stage(profiler_ptr, name, color)
190 #else
191 #define GPU_STAGE_PROFILER(profiler_ptr, name, color) \
192  GPUStageRAII _stage_raii##__LINE__ \
193  __attribute__((cleanup(gpu_stage_cleanup_raii))) \
194  __attribute__((unused)) = {profiler_ptr}; \
195  gpu_profiler_start_stage(profiler_ptr, name, color)
196 #endif
197 
198 #endif // GPU_PROFILER_H
Adaptive sampling utility for performance metrics.
Global application constants, configuration macros, and default values.
static const int GPU_PROFILER_ENV_COLOR
Definition: gpu_profiler.h:22
void gpu_profiler_begin_frame(GPUProfiler *profiler, uint64_t frame_index)
Marks the beginning of a frame. Swaps buffers and processes previous results.
Definition: gpu_profiler.c:98
static const int GPU_PROFILER_MOTION_BLUR_COLOR
Definition: gpu_profiler.h:27
static const int GPU_PROFILER_UI_COLOR
Definition: gpu_profiler.h:30
static const float GPU_PROFILER_ROW_HEIGHT
Definition: gpu_profiler.h:37
void gpu_profiler_set_enabled(GPUProfiler *profiler, bool enabled)
Enables or disables the profiler.
Definition: gpu_profiler.c:91
static const int GPU_PROFILER_GI_SYNC_COLOR
Definition: gpu_profiler.h:31
static const int GPU_PROFILER_SCENE_COLOR
Definition: gpu_profiler.h:23
static const int GPU_PROFILER_GI_DEBUG_COLOR
Definition: gpu_profiler.h:32
static const int GPU_PROFILER_BLOOM_COLOR
Definition: gpu_profiler.h:25
static const float GPU_PROFILER_WINDOW_TRANSITION_S
Definition: gpu_profiler.h:36
void gpu_profiler_init(GPUProfiler *profiler)
Initializes the GPU profiler.
Definition: gpu_profiler.c:19
void gpu_profiler_cleanup(GPUProfiler *profiler)
Cleans up GPU resources.
Definition: gpu_profiler.c:63
static const float GPU_PROFILER_WINDOW_DURATION_S
Definition: gpu_profiler.h:35
void gpu_profiler_start_stage(GPUProfiler *profiler, const char *name, uint32_t color)
Starts a new profiling stage.
Definition: gpu_profiler.c:203
static void gpu_stage_cleanup_raii(GPUStageRAII *stage_raii)
Internal cleanup function for GPUStageRAII.
Definition: gpu_profiler.h:168
@ GPU_QUERY_BUFFER_COUNT
Definition: gpu_profiler.h:17
@ MAX_GPU_STAGE_NAME
Definition: gpu_profiler.h:16
@ MAX_GPU_STAGES
Definition: gpu_profiler.h:15
static const int GPU_PROFILER_TOTAL_FRAME_COLOR
Definition: gpu_profiler.h:21
void gpu_profiler_reset_samplers(GPUProfiler *profiler, double current_time)
Resets all samplers for a new capture window.
Definition: gpu_profiler.c:252
static const int GPU_PROFILER_POSTPROCESS_COLOR
Definition: gpu_profiler.h:29
static const int GPU_PROFILER_DOF_COLOR
Definition: gpu_profiler.h:26
void gpu_profiler_end_stage(GPUProfiler *profiler)
Ends the current profiling stage.
Definition: gpu_profiler.c:236
static const int GPU_PROFILER_COMPOSITE_COLOR
Definition: gpu_profiler.h:28
static const int GPU_PROFILER_AUTO_EXPOSURE_COLOR
Definition: gpu_profiler.h:24
High-precision performance measurement (CPU and GPU).
Collector for performance samples within a rolling or fixed window.
Definition: adaptive_sampler.h:38
Manages GPU timing using double-buffered queries to avoid stalls.
Definition: gpu_profiler.h:86
int stage_count
Definition: gpu_profiler.h:88
int read_index
Definition: gpu_profiler.h:98
float transition_progress
Definition: gpu_profiler.h:104
int current_stage_index
Definition: gpu_profiler.h:101
bool enabled
Definition: gpu_profiler.h:105
int write_index
Definition: gpu_profiler.h:96
int recording_count
Definition: gpu_profiler.h:91
MetricStack hierarchy_stack
Definition: gpu_profiler.h:102
Set of GL queries for one frame, with stage metadata.
Definition: gpu_profiler.h:75
int stage_count
Definition: gpu_profiler.h:78
uint64_t frame_index
Definition: gpu_profiler.h:79
Per-frame stage metadata stored alongside queries.
Definition: gpu_profiler.h:64
int depth
Definition: gpu_profiler.h:67
uint32_t color
Definition: gpu_profiler.h:66
int parent_index
Definition: gpu_profiler.h:68
RAII container for automatic GPU stage management and Tracy zones.
Definition: gpu_profiler.h:159
GPUProfiler * profiler
Definition: gpu_profiler.h:160
Represents a single profiling stage (e.g., "Shadow Map", "G-Buffer").
Definition: gpu_profiler.h:45
int parent_index
Definition: gpu_profiler.h:57
float prev_start_offset_ms
Definition: gpu_profiler.h:52
float duration_ms
Definition: gpu_profiler.h:51
int depth
Definition: gpu_profiler.h:56
AdaptiveSampler duration_sampler
Definition: gpu_profiler.h:48
float start_offset_ms
Definition: gpu_profiler.h:50
uint32_t color
Definition: gpu_profiler.h:47
AdaptiveSampler offset_sampler
Definition: gpu_profiler.h:49
float prev_alpha
Definition: gpu_profiler.h:55
float prev_duration_ms
Definition: gpu_profiler.h:53
float alpha
Definition: gpu_profiler.h:54
GPU performance timer using OpenGL Query Objects.
Definition: perf_timer.h:34
Definition: metric_stack.h:8
#define tracy_gpu_zone_end(ctx)
Definition: tracy_gpu.h:36