Skip to content

Commit 799ad5b

Browse files
author
Dean Beeler
committed
Adding support for profiling using Tracy. (PR is outstanding currently, expected to be merged soon).
1 parent d4c64d9 commit 799ad5b

File tree

7 files changed

+182
-1
lines changed

7 files changed

+182
-1
lines changed

include/private/vkd3d_profiling.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,39 @@
2525

2626
#ifdef VKD3D_ENABLE_PROFILING
2727

28+
#ifdef TRACY_ENABLE
29+
30+
#include <TracyC.h>
31+
32+
void tracy_set_thread_name( const char* name );
33+
void tracy_emit_frame_mark();
34+
TracyCZoneCtx tracy_emit_zone_begin( const struct ___tracy_source_location_data* srcloc, int active );
35+
void tracy_emit_zone_end( TracyCZoneCtx ctx );
36+
37+
#define VKD3D_PROFILE_THREAD_NAME(name) tracy_set_thread_name((name))
38+
#define VKD3D_PROFILE_FRAME() tracy_emit_frame_mark()
39+
40+
void vkd3d_init_profiling(void);
41+
bool vkd3d_uses_profiling(void);
42+
43+
#define VKD3D_STRINGIZE1(x) #x
44+
#define VKD3D_STRINGIZE(x) VKD3D_STRINGIZE1(x)
45+
46+
#define VKD3D_REGION_DECL(name) TracyCZoneCtx _vkd3d_tracy_##name
47+
/* Copied from definition of TracyCZoneN. In this implementation, _vkd3d_tracy_##name is expected
48+
* to be defined by VKD3D_REGION_DECL. */
49+
#define VKD3D_REGION_BEGIN(name) static const struct ___tracy_source_location_data TracyConcat(__tracy_source_location,__LINE__) = { VKD3D_STRINGIZE(name), __func__, __FILE__, (uint32_t)__LINE__, 0 }; _vkd3d_tracy_##name = tracy_emit_zone_begin( &TracyConcat(__tracy_source_location,__LINE__), 1 )
50+
#define VKD3D_REGION_END_ITERATIONS(name, iter) tracy_emit_zone_end(_vkd3d_tracy_##name)
51+
52+
#else
53+
54+
#ifdef _WIN32
55+
#define WIN32_LEAN_AND_MEAN
56+
#include <windows.h>
57+
#else
58+
#include <time.h>
59+
#endif
60+
2861
void vkd3d_init_profiling(void);
2962
bool vkd3d_uses_profiling(void);
3063
unsigned int vkd3d_profiling_register_region(const char *name, spinlock_t *lock, uint32_t *latch);
@@ -50,13 +83,21 @@ void vkd3d_profiling_notify_work(unsigned int index, uint64_t start_ticks, uint6
5083
vkd3d_profiling_notify_work(_vkd3d_region_index_##name, _vkd3d_region_begin_tick_##name, _vkd3d_region_end_tick_##name, iter); \
5184
} while(0)
5285

86+
87+
#define VKD3D_PROFILE_THREAD_NAME(name) ((void)0)
88+
#define VKD3D_PROFILE_FRAME() ((void)0)
89+
90+
#endif /* TRACY_ENABLE */
91+
5392
#else
5493
static inline void vkd3d_init_profiling(void)
5594
{
5695
}
5796
#define VKD3D_REGION_DECL(name) ((void)0)
5897
#define VKD3D_REGION_BEGIN(name) ((void)0)
5998
#define VKD3D_REGION_END_ITERATIONS(name, iter) ((void)0)
99+
#define VKD3D_PROFILE_THREAD_NAME(name) ((void)0)
100+
#define VKD3D_PROFILE_FRAME() ((void)0)
60101
#endif /* VKD3D_ENABLE_PROFILING */
61102

62103
#define VKD3D_REGION_END(name) VKD3D_REGION_END_ITERATIONS(name, 1)

include/private/vkd3d_threads.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ static inline int condvar_reltime_wait_timeout_seconds(condvar_reltime_t *cond,
254254

255255
static inline void vkd3d_set_thread_name(const char *name)
256256
{
257+
VKD3D_PROFILE_THREAD_NAME(name);
257258
(void)name;
258259
}
259260

libs/vkd3d-common/meson.build

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@ vkd3d_common_src = [
88
'platform.c',
99
]
1010

11+
vkd3d_common_dependencies = []
12+
13+
if enable_tracy
14+
vkd3d_common_dependencies += [ tracy_dep ]
15+
endif
16+
1117
vkd3d_common_lib = static_library('vkd3d_common', vkd3d_common_src, vkd3d_header_files,
18+
dependencies : vkd3d_common_dependencies,
1219
include_directories : vkd3d_private_includes,
1320
dependencies : vkd3d_extra_libs,
1421
override_options : [ 'c_std='+vkd3d_c_std ])
1522

1623
vkd3d_common_dep = declare_dependency(
1724
link_with : vkd3d_common_lib,
25+
dependencies : vkd3d_common_dependencies,
1826
include_directories : [ vkd3d_public_includes, vkd3d_common_lib.private_dir_include() ])

libs/vkd3d-common/profiling.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ static void vkd3d_init_profiling_path(const char *path)
123123
}
124124
#endif
125125

126+
#ifndef TRACY_ENABLE
126127
static void vkd3d_init_profiling_once(void)
127128
{
128129
char path[VKD3D_PATH_MAX];
@@ -131,16 +132,57 @@ static void vkd3d_init_profiling_once(void)
131132
if (strlen(path) > 0)
132133
vkd3d_init_profiling_path(path);
133134
}
135+
#else
136+
#include "vkd3d_platform.h"
137+
138+
#ifdef DYNAMIC_TRACY
139+
140+
static void (*pfn_tracy_set_thread_name)( const char* name );
141+
static void (*pfn_tracy_emit_frame_mark)( const char* name );
142+
static TracyCZoneCtx (*pfn_tracy_emit_zone_begin)( const struct ___tracy_source_location_data* srcloc, int active );
143+
static void (*pfn_tracy_emit_zone_end)( TracyCZoneCtx ctx );
144+
145+
static void vkd3d_init_profiling_once(void)
146+
{
147+
#if defined(_WIN32)
148+
#define SONAME_TRACY "tracy.dll"
149+
#else
150+
#error "Unrecognized platform."
151+
#endif
152+
153+
vkd3d_module_t tracy = vkd3d_dlopen(SONAME_TRACY);
154+
if(tracy)
155+
{
156+
pfn_tracy_set_thread_name = vkd3d_dlsym(tracy, "___tracy_set_thread_name");
157+
pfn_tracy_emit_frame_mark = vkd3d_dlsym(tracy, "___tracy_emit_frame_mark");
158+
pfn_tracy_emit_zone_begin = vkd3d_dlsym(tracy, "___tracy_emit_zone_begin");
159+
pfn_tracy_emit_zone_end = vkd3d_dlsym(tracy, "___tracy_emit_zone_end");
160+
}
161+
}
162+
#else
163+
static void vkd3d_init_profiling_once(void)
164+
{
165+
}
166+
#endif /* DYNAMIC_TRACY */
167+
168+
#endif /* TRACY_ENABLE */
134169

135170
void vkd3d_init_profiling(void)
136171
{
137172
pthread_once(&profiling_block_once, vkd3d_init_profiling_once);
138173
}
139174

175+
#ifndef TRACY_ENABLE
140176
bool vkd3d_uses_profiling(void)
141177
{
142178
return mapped_blocks != NULL;
143179
}
180+
#else
181+
bool vkd3d_uses_profiling(void)
182+
{
183+
return true;
184+
}
185+
#endif /* TRACY_ENABLE */
144186

145187
unsigned int vkd3d_profiling_register_region(const char *name, spinlock_t *lock, uint32_t *latch)
146188
{
@@ -195,4 +237,60 @@ void vkd3d_profiling_notify_work(unsigned int index,
195237
spinlock_release(lock);
196238
}
197239

240+
#ifdef TRACY_ENABLE
241+
242+
#ifdef DYNAMIC_TRACY
243+
244+
void tracy_set_thread_name(const char *name)
245+
{
246+
if(pfn_tracy_set_thread_name)
247+
pfn_tracy_set_thread_name(name);
248+
}
249+
250+
void tracy_emit_frame_mark()
251+
{
252+
if(pfn_tracy_emit_frame_mark)
253+
pfn_tracy_emit_frame_mark(0);
254+
}
255+
256+
TracyCZoneCtx tracy_emit_zone_begin(const struct ___tracy_source_location_data *srcloc, int active)
257+
{
258+
TracyCZoneCtx ctx;
259+
if(pfn_tracy_emit_zone_begin)
260+
ctx = pfn_tracy_emit_zone_begin(srcloc, active);
261+
return ctx;
262+
}
263+
264+
void tracy_emit_zone_end(TracyCZoneCtx ctx)
265+
{
266+
if(pfn_tracy_emit_zone_end)
267+
pfn_tracy_emit_zone_end(ctx);
268+
}
269+
270+
#else
271+
272+
void tracy_set_thread_name(const char *name)
273+
{
274+
___tracy_set_thread_name(name);
275+
}
276+
277+
void tracy_emit_frame_mark()
278+
{
279+
___tracy_emit_frame_mark(0);
280+
}
281+
282+
TracyCZoneCtx tracy_emit_zone_begin(const struct ___tracy_source_location_data *srcloc, int active)
283+
{
284+
return ___tracy_emit_zone_begin(srcloc, active);
285+
}
286+
287+
void tracy_emit_zone_end(TracyCZoneCtx ctx)
288+
{
289+
___tracy_emit_zone_end(ctx);
290+
}
291+
292+
#endif /* DYNAMIC_TRACY */
293+
294+
#endif /* TRACY_ENABLE */
295+
198296
#endif /* VKD3D_ENABLE_PROFILING */

meson.build

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ enable_profiling = get_option('enable_profiling')
1919
enable_renderdoc = get_option('enable_renderdoc')
2020
enable_descriptor_qa = get_option('enable_descriptor_qa')
2121
enable_trace = get_option('enable_trace')
22+
enable_tracy = get_option('enable_tracy')
23+
dynamic_tracy = get_option('dynamic_tracy')
2224

2325
if enable_trace == 'auto'
2426
enable_trace = vkd3d_debug
@@ -33,7 +35,7 @@ if vkd3d_platform == 'windows'
3335
add_project_arguments('-D_WIN32_WINNT=0x600', language : 'c')
3436
endif
3537

36-
if enable_profiling
38+
if enable_profiling or enable_tracy
3739
add_project_arguments('-DVKD3D_ENABLE_PROFILING', language : 'c')
3840
endif
3941

@@ -54,6 +56,14 @@ if enable_breadcrumbs
5456
add_project_arguments('-DVKD3D_ENABLE_BREADCRUMBS', language : 'c')
5557
endif
5658

59+
if enable_tracy
60+
add_project_arguments('-DTRACY_ENABLE', language : 'c')
61+
62+
if dynamic_tracy
63+
add_project_arguments('-DDYNAMIC_TRACY', language : 'c')
64+
endif
65+
endif
66+
5767
vkd3d_external_includes = [ '../../common/vulkan/include', './subprojects/SPIRV-Headers/include' ]
5868
vkd3d_public_includes = [ './include' ] + vkd3d_external_includes
5969
vkd3d_private_includes = [ './include/private' ] + vkd3d_public_includes
@@ -145,6 +155,24 @@ vkd3d_version = vcs_tag(
145155
dxil_spirv = subproject('dxil-spirv')
146156
dxil_spirv_dep = dxil_spirv.get_variable('dxil_spirv_dep')
147157

158+
if enable_tracy
159+
tracy_options = [
160+
'tracy_on_demand=true',
161+
'tracy_no_callstack=true',
162+
'tracy_delayed_init=true',
163+
'tracy_no_sys_trace=true'
164+
]
165+
166+
if dynamic_tracy
167+
tracy_options += [
168+
'tracy_shared_libs=true'
169+
]
170+
endif
171+
172+
tracy = subproject('tracy', default_options : tracy_options)
173+
tracy_dep = tracy.get_variable('tracy_dep')
174+
endif
175+
148176
subdir('include')
149177
subdir('libs')
150178

meson_options.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ option('enable_profiling', type : 'boolean', value : false)
44
option('enable_renderdoc', type : 'boolean', value : false)
55
option('enable_descriptor_qa', type : 'boolean', value : false)
66
option('enable_trace', type : 'combo', value : 'auto', choices : ['false', 'true', 'auto'])
7+
option('enable_tracy', type : 'boolean', value : false)
8+
option('dynamic_tracy', type : 'boolean', value : false)

subprojects/tracy.wrap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[wrap-git]
2+
url = https://github.com/wolfpld/tracy.git
3+
revision = ccf35eb24a9468d03c1725caf1c843ae89dbede4

0 commit comments

Comments
 (0)