Suckless OGL 1.0.0
A lean, high-performance C11 PBR Renderer
Loading...
Searching...
No Matches
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
14enum {
18};
19
20/* --- Profiler Colors (Nord Theme) --- */
21static const int GPU_PROFILER_TOTAL_FRAME_COLOR = 0xECEFF4;
22static const int GPU_PROFILER_ENV_COLOR = 0x88C0D0;
23static const int GPU_PROFILER_SCENE_COLOR = 0xD08770;
24static const int GPU_PROFILER_AUTO_EXPOSURE_COLOR = 0xEBCB8B;
25static const int GPU_PROFILER_BLOOM_COLOR = 0x5E81AC;
26static const int GPU_PROFILER_DOF_COLOR = 0xA3BE8C;
27static const int GPU_PROFILER_MOTION_BLUR_COLOR = 0xBF616A;
28static const int GPU_PROFILER_COMPOSITE_COLOR = 0x81A1C1;
29static const int GPU_PROFILER_POSTPROCESS_COLOR = 0xB48EAD;
30static const int GPU_PROFILER_UI_COLOR = 0x4C566A;
31static const int GPU_PROFILER_GI_SYNC_COLOR = 0x8FBCBB;
32static const int GPU_PROFILER_GI_DEBUG_COLOR = 0xB48EAD;
33static const int GPU_PROFILER_NBODY_COLOR = 0xD8DEE9;
34static const int GPU_PROFILER_SHOCKWAVE_COLOR = 0xE5A3C9;
35
36/* --- Capture Settings --- */
37static const float GPU_PROFILER_WINDOW_DURATION_S = 0.5F;
38static const float GPU_PROFILER_WINDOW_TRANSITION_S = 0.2F;
39static const float GPU_PROFILER_ROW_HEIGHT = 28.0F;
40
59
64typedef struct {
66 uint32_t color;
67 int depth;
70
75typedef struct {
79 uint64_t frame_index;
81
107
112void gpu_profiler_init(GPUProfiler* profiler);
113
118void gpu_profiler_cleanup(GPUProfiler* profiler);
119
126void gpu_profiler_begin_frame(GPUProfiler* profiler, uint64_t frame_index);
127
133void gpu_profiler_set_enabled(GPUProfiler* profiler, bool enabled);
134
141void gpu_profiler_start_stage(GPUProfiler* profiler, const char* name,
142 uint32_t color);
143
148void gpu_profiler_end_stage(GPUProfiler* profiler);
149
153void gpu_profiler_reset_samplers(GPUProfiler* profiler, double current_time);
154
159typedef struct {
161#ifdef TRACY_ENABLE
162 TracyCZoneCtx tracy_ctx;
163 void* tracy_gpu_ctx;
164#endif
166
168static 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.
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:100
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:39
void gpu_profiler_set_enabled(GPUProfiler *profiler, bool enabled)
Enables or disables the profiler.
Definition gpu_profiler.c:93
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
@ 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 float GPU_PROFILER_WINDOW_TRANSITION_S
Definition gpu_profiler.h:38
void gpu_profiler_init(GPUProfiler *profiler)
Initializes the GPU profiler.
Definition gpu_profiler.c:21
void gpu_profiler_cleanup(GPUProfiler *profiler)
Cleans up GPU resources.
Definition gpu_profiler.c:65
static const float GPU_PROFILER_WINDOW_DURATION_S
Definition gpu_profiler.h:37
void gpu_profiler_start_stage(GPUProfiler *profiler, const char *name, uint32_t color)
Starts a new profiling stage.
Definition gpu_profiler.c:205
static void gpu_stage_cleanup_raii(GPUStageRAII *stage_raii)
Internal cleanup function for GPUStageRAII.
Definition gpu_profiler.h:168
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:254
static const int GPU_PROFILER_POSTPROCESS_COLOR
Definition gpu_profiler.h:29
static const int GPU_PROFILER_DOF_COLOR
Definition gpu_profiler.h:26
static const int GPU_PROFILER_SHOCKWAVE_COLOR
Definition gpu_profiler.h:34
void gpu_profiler_end_stage(GPUProfiler *profiler)
Ends the current profiling stage.
Definition gpu_profiler.c:238
static const int GPU_PROFILER_NBODY_COLOR
Definition gpu_profiler.h:33
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
GPUStage stages[MAX_GPU_STAGES]
Definition gpu_profiler.h:87
GPUQueryBuffer buffers[GPU_QUERY_BUFFER_COUNT]
Definition gpu_profiler.h:95
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:35
Definition metric_stack.h:8
#define tracy_gpu_zone_end(ctx)
Definition tracy_gpu.h:35