1 /* 2 * Copyright 2023 Bas Nieuwenhuizen 3 * SPDX-License-Identifier: MIT 4 */ 5 6 #include "u_gpuvis.h" 7 8 #include <threads.h> 9 10 #define GPUVIS_TRACE_IMPLEMENTATION 11 #include "gpuvis_trace_utils.h" 12 13 /* Random base value to prevent collisions. As contexts are considered thread 14 * global by gpuvis, collisions are quite likely if we start at 0 and there 15 * are independent libraries tacing 16 */ 17 static unsigned int gpuvis_base_ctx; 18 19 static _Thread_local unsigned int gpuvis_current_ctx; 20 21 static once_flag gpuvis_once_flag = ONCE_FLAG_INIT; 22 23 static void util_gpuvis_init_once()24util_gpuvis_init_once() 25 { 26 gpuvis_trace_init(); 27 28 /* Initialize it by address to avoid collisions between libraries using 29 * this code (e.g. GL & vulkan) */ 30 gpuvis_base_ctx = (uintptr_t) util_gpuvis_init_once >> 12; 31 } 32 33 void util_gpuvis_init(void)34util_gpuvis_init(void) 35 { 36 call_once(&gpuvis_once_flag, util_gpuvis_init_once); 37 } 38 39 void util_gpuvis_begin(const char * name)40util_gpuvis_begin(const char *name) 41 { 42 unsigned int ctx = gpuvis_base_ctx + ++gpuvis_current_ctx; 43 gpuvis_trace_begin_ctx_printf(ctx, "mesa:%s", name); 44 } 45 46 void util_gpuvis_end(void)47util_gpuvis_end(void) 48 { 49 unsigned int ctx = gpuvis_base_ctx + gpuvis_current_ctx--; 50 51 /* Use an empty string to avoid warnings about an empty format string. */ 52 gpuvis_trace_end_ctx_printf(ctx, "%s", ""); 53 }