1 #ifndef __TRACYVULKAN_HPP__
2 #define __TRACYVULKAN_HPP__
4 #if !defined TRACY_ENABLE
6 #define TracyVkContext(x,y,z,w) nullptr
7 #define TracyVkContextCalibrated(x,y,z,w,a,b) nullptr
8 #define TracyVkDestroy(x)
9 #define TracyVkContextName(c,x,y)
10 #define TracyVkNamedZone(c,x,y,z,w)
11 #define TracyVkNamedZoneC(c,x,y,z,w,a)
12 #define TracyVkZone(c,x,y)
13 #define TracyVkZoneC(c,x,y,z)
14 #define TracyVkZoneTransient(c,x,y,z,w)
15 #define TracyVkCollect(c,x)
17 #define TracyVkNamedZoneS(c,x,y,z,w,a)
18 #define TracyVkNamedZoneCS(c,x,y,z,w,v,a)
19 #define TracyVkZoneS(c,x,y,z)
20 #define TracyVkZoneCS(c,x,y,z,w)
21 #define TracyVkZoneTransientS(c,x,y,z,w,a)
28 using TracyVkCtx =
void*;
34 #include <vulkan/vulkan.h>
44 friend class VkCtxScope;
46 enum { QueryCount = 64 * 1024 };
49 VkCtx( VkPhysicalDevice physdev, VkDevice device, VkQueue queue, VkCommandBuffer cmdbuf, PFN_vkGetPhysicalDeviceCalibrateableTimeDomainsEXT _vkGetPhysicalDeviceCalibrateableTimeDomainsEXT, PFN_vkGetCalibratedTimestampsEXT _vkGetCalibratedTimestampsEXT )
51 , m_timeDomain( VK_TIME_DOMAIN_DEVICE_EXT )
56 , m_queryCount( QueryCount )
57 , m_vkGetCalibratedTimestampsEXT( _vkGetCalibratedTimestampsEXT )
59 assert( m_context != 255 );
61 if( _vkGetPhysicalDeviceCalibrateableTimeDomainsEXT && _vkGetCalibratedTimestampsEXT )
64 _vkGetPhysicalDeviceCalibrateableTimeDomainsEXT( physdev, &num,
nullptr );
65 if( num > 4 ) num = 4;
66 VkTimeDomainEXT
data[4];
67 _vkGetPhysicalDeviceCalibrateableTimeDomainsEXT( physdev, &num, data );
71 if( data[i] == VK_TIME_DOMAIN_QUERY_PERFORMANCE_COUNTER_EXT )
73 m_timeDomain = data[i];
79 VkPhysicalDeviceProperties prop;
80 vkGetPhysicalDeviceProperties( physdev, &prop );
81 const float period = prop.limits.timestampPeriod;
83 VkQueryPoolCreateInfo poolInfo = {};
84 poolInfo.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO;
85 poolInfo.queryCount = m_queryCount;
86 poolInfo.queryType = VK_QUERY_TYPE_TIMESTAMP;
87 while( vkCreateQueryPool( device, &poolInfo,
nullptr, &m_query ) != VK_SUCCESS )
90 poolInfo.queryCount = m_queryCount;
93 VkCommandBufferBeginInfo beginInfo = {};
94 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
95 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
97 VkSubmitInfo submitInfo = {};
98 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
99 submitInfo.commandBufferCount = 1;
100 submitInfo.pCommandBuffers = &cmdbuf;
102 vkBeginCommandBuffer( cmdbuf, &beginInfo );
103 vkCmdResetQueryPool( cmdbuf, m_query, 0, m_queryCount );
104 vkEndCommandBuffer( cmdbuf );
105 vkQueueSubmit( queue, 1, &submitInfo, VK_NULL_HANDLE );
106 vkQueueWaitIdle( queue );
109 if( m_timeDomain == VK_TIME_DOMAIN_DEVICE_EXT )
111 vkBeginCommandBuffer( cmdbuf, &beginInfo );
112 vkCmdWriteTimestamp( cmdbuf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, m_query, 0 );
113 vkEndCommandBuffer( cmdbuf );
114 vkQueueSubmit( queue, 1, &submitInfo, VK_NULL_HANDLE );
115 vkQueueWaitIdle( queue );
118 vkGetQueryPoolResults( device, m_query, 0, 1,
sizeof( tgpu ), &tgpu,
sizeof( tgpu ), VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT );
120 vkBeginCommandBuffer( cmdbuf, &beginInfo );
121 vkCmdResetQueryPool( cmdbuf, m_query, 0, 1 );
122 vkEndCommandBuffer( cmdbuf );
123 vkQueueSubmit( queue, 1, &submitInfo, VK_NULL_HANDLE );
124 vkQueueWaitIdle( queue );
128 enum { NumProbes = 32 };
130 VkCalibratedTimestampInfoEXT spec[2] = {
131 { VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT,
nullptr, VK_TIME_DOMAIN_DEVICE_EXT },
132 { VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT,
nullptr, m_timeDomain },
136 for(
int i=0; i<NumProbes; i++ )
138 _vkGetCalibratedTimestampsEXT( device, 2, spec, ts, deviation+i );
140 uint64_t minDeviation = deviation[0];
141 for(
int i=1; i<NumProbes; i++ )
143 if( minDeviation > deviation[i] )
145 minDeviation = deviation[i];
148 m_deviation = minDeviation * 3 / 2;
152 Calibrate( device, m_prevCalibration, tgpu );
160 MemWrite( &item->hdr.type, QueueType::GpuNewContext );
161 MemWrite( &item->gpuNewContext.cpuTime, tcpu );
162 MemWrite( &item->gpuNewContext.gpuTime, tgpu );
163 memset( &item->gpuNewContext.thread, 0,
sizeof( item->gpuNewContext.thread ) );
164 MemWrite( &item->gpuNewContext.period, period );
165 MemWrite( &item->gpuNewContext.context, m_context );
166 MemWrite( &item->gpuNewContext.flags, flags );
167 MemWrite( &item->gpuNewContext.type, GpuContextType::Vulkan );
169 #ifdef TRACY_ON_DEMAND
180 vkDestroyQueryPool( m_device, m_query,
nullptr );
186 memcpy( ptr, name, len );
189 MemWrite( &item->hdr.type, QueueType::GpuContextName );
190 MemWrite( &item->gpuContextNameFat.context, m_context );
192 MemWrite( &item->gpuContextNameFat.size, len );
193 #ifdef TRACY_ON_DEMAND
199 void Collect( VkCommandBuffer cmdbuf )
203 if( m_tail == m_head )
return;
205 #ifdef TRACY_ON_DEMAND
208 vkCmdResetQueryPool( cmdbuf, m_query, 0, m_queryCount );
211 if( m_timeDomain != VK_TIME_DOMAIN_DEVICE_EXT ) Calibrate( m_device, m_prevCalibration, tgpu );
224 cnt = m_head < m_tail ? m_queryCount - m_tail : m_head - m_tail;
227 if( vkGetQueryPoolResults( m_device, m_query, m_tail, cnt,
sizeof(
int64_t ) * m_queryCount, m_res,
sizeof(
int64_t ), VK_QUERY_RESULT_64_BIT ) == VK_NOT_READY )
233 for(
unsigned int idx=0; idx<cnt; idx++ )
236 MemWrite( &item->hdr.type, QueueType::GpuTime );
237 MemWrite( &item->gpuTime.gpuTime, m_res[idx] );
239 MemWrite( &item->gpuTime.context, m_context );
243 if( m_timeDomain != VK_TIME_DOMAIN_DEVICE_EXT )
246 Calibrate( m_device, tcpu, tgpu );
248 const auto delta = tcpu - m_prevCalibration;
251 m_prevCalibration = tcpu;
253 MemWrite( &item->hdr.type, QueueType::GpuCalibration );
254 MemWrite( &item->gpuCalibration.gpuTime, tgpu );
255 MemWrite( &item->gpuCalibration.cpuTime, refCpu );
256 MemWrite( &item->gpuCalibration.cpuDelta, delta );
257 MemWrite( &item->gpuCalibration.context, m_context );
262 vkCmdResetQueryPool( cmdbuf, m_query, m_tail, cnt );
265 if( m_tail == m_queryCount ) m_tail = 0;
271 const auto id = m_head;
272 m_head = ( m_head + 1 ) % m_queryCount;
273 assert( m_head != m_tail );
284 assert( m_timeDomain != VK_TIME_DOMAIN_DEVICE_EXT );
285 VkCalibratedTimestampInfoEXT spec[2] = {
286 { VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT,
nullptr, VK_TIME_DOMAIN_DEVICE_EXT },
287 { VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT,
nullptr, m_timeDomain },
293 m_vkGetCalibratedTimestampsEXT( device, 2, spec, ts, &deviation );
295 while( deviation > m_deviation );
297 #if defined _WIN32 || defined __CYGWIN__
299 tCpu = ts[1] * m_qpcToNs;
307 VkTimeDomainEXT m_timeDomain;
315 unsigned int m_oldCnt;
316 unsigned int m_queryCount;
320 PFN_vkGetCalibratedTimestampsEXT m_vkGetCalibratedTimestampsEXT;
326 tracy_force_inline VkCtxScope( VkCtx* ctx,
const SourceLocationData* srcloc, VkCommandBuffer cmdbuf,
bool is_active )
327 #ifdef TRACY_ON_DEMAND
328 : m_active( is_active &&
GetProfiler().IsConnected() )
330 : m_active( is_active )
333 if( !m_active )
return;
337 const auto queryId = ctx->NextQueryId();
338 vkCmdWriteTimestamp( cmdbuf, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, ctx->m_query,
queryId );
341 MemWrite( &item->hdr.type, QueueType::GpuZoneBeginSerial );
346 MemWrite( &item->gpuZoneBegin.context, ctx->GetId() );
350 tracy_force_inline VkCtxScope( VkCtx* ctx,
const SourceLocationData* srcloc, VkCommandBuffer cmdbuf,
int depth,
bool is_active )
351 #ifdef TRACY_ON_DEMAND
352 : m_active( is_active &&
GetProfiler().IsConnected() )
354 : m_active( is_active )
357 if( !m_active )
return;
361 const auto queryId = ctx->NextQueryId();
362 vkCmdWriteTimestamp( cmdbuf, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, ctx->m_query,
queryId );
365 MemWrite( &item->hdr.type, QueueType::GpuZoneBeginCallstackSerial );
370 MemWrite( &item->gpuZoneBegin.context, ctx->GetId() );
374 tracy_force_inline VkCtxScope( VkCtx* ctx,
uint32_t line,
const char*
source,
size_t sourceSz,
const char*
function,
size_t functionSz,
const char* name,
size_t nameSz, VkCommandBuffer cmdbuf,
bool is_active )
375 #ifdef TRACY_ON_DEMAND
376 : m_active( is_active &&
GetProfiler().IsConnected() )
378 : m_active( is_active )
381 if( !m_active )
return;
385 const auto queryId = ctx->NextQueryId();
386 vkCmdWriteTimestamp( cmdbuf, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, ctx->m_query,
queryId );
390 MemWrite( &item->hdr.type, QueueType::GpuZoneBeginAllocSrcLocSerial );
392 MemWrite( &item->gpuZoneBegin.srcloc, srcloc );
395 MemWrite( &item->gpuZoneBegin.context, ctx->GetId() );
399 tracy_force_inline VkCtxScope( VkCtx* ctx,
uint32_t line,
const char* source,
size_t sourceSz,
const char*
function,
size_t functionSz,
const char* name,
size_t nameSz, VkCommandBuffer cmdbuf,
int depth,
bool is_active )
400 #ifdef TRACY_ON_DEMAND
401 : m_active( is_active &&
GetProfiler().IsConnected() )
403 : m_active( is_active )
406 if( !m_active )
return;
410 const auto queryId = ctx->NextQueryId();
411 vkCmdWriteTimestamp( cmdbuf, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, ctx->m_query,
queryId );
415 MemWrite( &item->hdr.type, QueueType::GpuZoneBeginAllocSrcLocCallstackSerial );
417 MemWrite( &item->gpuZoneBegin.srcloc, srcloc );
420 MemWrite( &item->gpuZoneBegin.context, ctx->GetId() );
426 if( !m_active )
return;
428 const auto queryId = m_ctx->NextQueryId();
429 vkCmdWriteTimestamp( m_cmdbuf, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, m_ctx->m_query,
queryId );
432 MemWrite( &item->hdr.type, QueueType::GpuZoneEndSerial );
436 MemWrite( &item->gpuZoneEnd.context, m_ctx->GetId() );
443 VkCommandBuffer m_cmdbuf;
447 static inline VkCtx* CreateVkContext( VkPhysicalDevice physdev, VkDevice device, VkQueue queue, VkCommandBuffer cmdbuf, PFN_vkGetPhysicalDeviceCalibrateableTimeDomainsEXT gpdctd, PFN_vkGetCalibratedTimestampsEXT gct )
451 new(ctx) VkCtx( physdev, device, queue, cmdbuf, gpdctd, gct );
455 static inline void DestroyVkContext( VkCtx* ctx )
463 using TracyVkCtx = tracy::VkCtx*;
465 #define TracyVkContext( physdev, device, queue, cmdbuf ) tracy::CreateVkContext( physdev, device, queue, cmdbuf, nullptr, nullptr );
466 #define TracyVkContextCalibrated( physdev, device, queue, cmdbuf, gpdctd, gct ) tracy::CreateVkContext( physdev, device, queue, cmdbuf, gpdctd, gct );
467 #define TracyVkDestroy( ctx ) tracy::DestroyVkContext( ctx );
468 #define TracyVkContextName( ctx, name, size ) ctx->Name( name, size );
469 #if defined TRACY_HAS_CALLSTACK && defined TRACY_CALLSTACK
470 # define TracyVkNamedZone( ctx, varname, cmdbuf, name, active ) static constexpr tracy::SourceLocationData TracyConcat(__tracy_gpu_source_location,__LINE__) { name, __FUNCTION__, __FILE__, (uint32_t)__LINE__, 0 }; tracy::VkCtxScope varname( ctx, &TracyConcat(__tracy_gpu_source_location,__LINE__), cmdbuf, TRACY_CALLSTACK, active );
471 # define TracyVkNamedZoneC( ctx, varname, cmdbuf, name, color, active ) static constexpr tracy::SourceLocationData TracyConcat(__tracy_gpu_source_location,__LINE__) { name, __FUNCTION__, __FILE__, (uint32_t)__LINE__, color }; tracy::VkCtxScope varname( ctx, &TracyConcat(__tracy_gpu_source_location,__LINE__), cmdbuf, TRACY_CALLSTACK, active );
472 # define TracyVkZone( ctx, cmdbuf, name ) TracyVkNamedZoneS( ctx, ___tracy_gpu_zone, cmdbuf, name, TRACY_CALLSTACK, true )
473 # define TracyVkZoneC( ctx, cmdbuf, name, color ) TracyVkNamedZoneCS( ctx, ___tracy_gpu_zone, cmdbuf, name, color, TRACY_CALLSTACK, true )
474 # define TracyVkZoneTransient( ctx, varname, cmdbuf, name, active ) TracyVkZoneTransientS( ctx, varname, cmdbuf, name, TRACY_CALLSTACK, active )
476 # define TracyVkNamedZone( ctx, varname, cmdbuf, name, active ) static constexpr tracy::SourceLocationData TracyConcat(__tracy_gpu_source_location,__LINE__) { name, __FUNCTION__, __FILE__, (uint32_t)__LINE__, 0 }; tracy::VkCtxScope varname( ctx, &TracyConcat(__tracy_gpu_source_location,__LINE__), cmdbuf, active );
477 # define TracyVkNamedZoneC( ctx, varname, cmdbuf, name, color, active ) static constexpr tracy::SourceLocationData TracyConcat(__tracy_gpu_source_location,__LINE__) { name, __FUNCTION__, __FILE__, (uint32_t)__LINE__, color }; tracy::VkCtxScope varname( ctx, &TracyConcat(__tracy_gpu_source_location,__LINE__), cmdbuf, active );
478 # define TracyVkZone( ctx, cmdbuf, name ) TracyVkNamedZone( ctx, ___tracy_gpu_zone, cmdbuf, name, true )
479 # define TracyVkZoneC( ctx, cmdbuf, name, color ) TracyVkNamedZoneC( ctx, ___tracy_gpu_zone, cmdbuf, name, color, true )
480 # define TracyVkZoneTransient( ctx, varname, cmdbuf, name, active ) tracy::VkCtxScope varname( ctx, __LINE__, __FILE__, strlen( __FILE__ ), __FUNCTION__, strlen( __FUNCTION__ ), name, strlen( name ), cmdbuf, active );
482 #define TracyVkCollect( ctx, cmdbuf ) ctx->Collect( cmdbuf );
484 #ifdef TRACY_HAS_CALLSTACK
485 # define TracyVkNamedZoneS( ctx, varname, cmdbuf, name, depth, active ) static constexpr tracy::SourceLocationData TracyConcat(__tracy_gpu_source_location,__LINE__) { name, __FUNCTION__, __FILE__, (uint32_t)__LINE__, 0 }; tracy::VkCtxScope varname( ctx, &TracyConcat(__tracy_gpu_source_location,__LINE__), cmdbuf, depth, active );
486 # define TracyVkNamedZoneCS( ctx, varname, cmdbuf, name, color, depth, active ) static constexpr tracy::SourceLocationData TracyConcat(__tracy_gpu_source_location,__LINE__) { name, __FUNCTION__, __FILE__, (uint32_t)__LINE__, color }; tracy::VkCtxScope varname( ctx, &TracyConcat(__tracy_gpu_source_location,__LINE__), cmdbuf, depth, active );
487 # define TracyVkZoneS( ctx, cmdbuf, name, depth ) TracyVkNamedZoneS( ctx, ___tracy_gpu_zone, cmdbuf, name, depth, true )
488 # define TracyVkZoneCS( ctx, cmdbuf, name, color, depth ) TracyVkNamedZoneCS( ctx, ___tracy_gpu_zone, cmdbuf, name, color, depth, true )
489 # define TracyVkZoneTransientS( ctx, varname, cmdbuf, name, depth, active ) tracy::VkCtxScope varname( ctx, __LINE__, __FILE__, strlen( __FILE__ ), __FUNCTION__, strlen( __FUNCTION__ ), name, strlen( name ), cmdbuf, depth, active );
491 # define TracyVkNamedZoneS( ctx, varname, cmdbuf, name, depth, active ) TracyVkNamedZone( ctx, varname, cmdbuf, name, active )
492 # define TracyVkNamedZoneCS( ctx, varname, cmdbuf, name, color, depth, active ) TracyVkNamedZoneC( ctx, varname, cmdbuf, name, color, active )
493 # define TracyVkZoneS( ctx, cmdbuf, name, depth ) TracyVkZone( ctx, cmdbuf, name )
494 # define TracyVkZoneCS( ctx, cmdbuf, name, color, depth ) TracyVkZoneC( ctx, cmdbuf, name, color )
495 # define TracyVkZoneTransientS( ctx, varname, cmdbuf, name, depth, active ) TracyVkZoneTransient( ctx, varname, cmdbuf, name, active )