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 TracyVkNamedZone(c,x,y,z,w)
10 #define TracyVkNamedZoneC(c,x,y,z,w,a)
11 #define TracyVkZone(c,x,y)
12 #define TracyVkZoneC(c,x,y,z)
13 #define TracyVkCollect(c,x)
15 #define TracyVkNamedZoneS(c,x,y,z,w,a)
16 #define TracyVkNamedZoneCS(c,x,y,z,w,v,a)
17 #define TracyVkZoneS(c,x,y,z)
18 #define TracyVkZoneCS(c,x,y,z,w)
25 using TracyVkCtx =
void*;
31 #include <vulkan/vulkan.h>
41 friend class VkCtxScope;
43 enum { QueryCount = 64 * 1024 };
46 VkCtx( VkPhysicalDevice physdev, VkDevice device, VkQueue queue, VkCommandBuffer cmdbuf, PFN_vkGetPhysicalDeviceCalibrateableTimeDomainsEXT _vkGetPhysicalDeviceCalibrateableTimeDomainsEXT, PFN_vkGetCalibratedTimestampsEXT _vkGetCalibratedTimestampsEXT )
48 , m_timeDomain( VK_TIME_DOMAIN_DEVICE_EXT )
53 , m_queryCount( QueryCount )
54 , m_vkGetCalibratedTimestampsEXT( _vkGetCalibratedTimestampsEXT )
56 assert( m_context != 255 );
58 if( _vkGetPhysicalDeviceCalibrateableTimeDomainsEXT && _vkGetCalibratedTimestampsEXT )
61 _vkGetPhysicalDeviceCalibrateableTimeDomainsEXT( physdev, &num,
nullptr );
62 if( num > 4 ) num = 4;
63 VkTimeDomainEXT
data[4];
64 _vkGetPhysicalDeviceCalibrateableTimeDomainsEXT( physdev, &num, data );
68 if( data[i] == VK_TIME_DOMAIN_QUERY_PERFORMANCE_COUNTER_EXT )
70 m_timeDomain = data[i];
76 VkPhysicalDeviceProperties prop;
77 vkGetPhysicalDeviceProperties( physdev, &prop );
78 const float period = prop.limits.timestampPeriod;
80 VkQueryPoolCreateInfo poolInfo = {};
81 poolInfo.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO;
82 poolInfo.queryCount = m_queryCount;
83 poolInfo.queryType = VK_QUERY_TYPE_TIMESTAMP;
84 while( vkCreateQueryPool( device, &poolInfo,
nullptr, &m_query ) != VK_SUCCESS )
87 poolInfo.queryCount = m_queryCount;
90 VkCommandBufferBeginInfo beginInfo = {};
91 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
92 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
94 VkSubmitInfo submitInfo = {};
95 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
96 submitInfo.commandBufferCount = 1;
97 submitInfo.pCommandBuffers = &cmdbuf;
99 vkBeginCommandBuffer( cmdbuf, &beginInfo );
100 vkCmdResetQueryPool( cmdbuf, m_query, 0, m_queryCount );
101 vkEndCommandBuffer( cmdbuf );
102 vkQueueSubmit( queue, 1, &submitInfo, VK_NULL_HANDLE );
103 vkQueueWaitIdle( queue );
106 if( m_timeDomain == VK_TIME_DOMAIN_DEVICE_EXT )
108 vkBeginCommandBuffer( cmdbuf, &beginInfo );
109 vkCmdWriteTimestamp( cmdbuf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, m_query, 0 );
110 vkEndCommandBuffer( cmdbuf );
111 vkQueueSubmit( queue, 1, &submitInfo, VK_NULL_HANDLE );
112 vkQueueWaitIdle( queue );
115 vkGetQueryPoolResults( device, m_query, 0, 1,
sizeof( tgpu ), &tgpu,
sizeof( tgpu ), VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT );
117 vkBeginCommandBuffer( cmdbuf, &beginInfo );
118 vkCmdResetQueryPool( cmdbuf, m_query, 0, 1 );
119 vkEndCommandBuffer( cmdbuf );
120 vkQueueSubmit( queue, 1, &submitInfo, VK_NULL_HANDLE );
121 vkQueueWaitIdle( queue );
125 enum { NumProbes = 32 };
127 VkCalibratedTimestampInfoEXT spec[2] = {
128 { VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT,
nullptr, VK_TIME_DOMAIN_DEVICE_EXT },
129 { VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT,
nullptr, m_timeDomain },
133 for(
int i=0; i<NumProbes; i++ )
135 _vkGetCalibratedTimestampsEXT( device, 2, spec, ts, deviation+i );
137 uint64_t minDeviation = deviation[0];
138 for(
int i=1; i<NumProbes; i++ )
140 if( minDeviation > deviation[i] )
142 minDeviation = deviation[i];
145 m_deviation = minDeviation * 3 / 2;
149 Calibrate( device, m_prevCalibration, tgpu );
157 MemWrite( &item->hdr.type, QueueType::GpuNewContext );
158 MemWrite( &item->gpuNewContext.cpuTime, tcpu );
159 MemWrite( &item->gpuNewContext.gpuTime, tgpu );
160 memset( &item->gpuNewContext.thread, 0,
sizeof( item->gpuNewContext.thread ) );
161 MemWrite( &item->gpuNewContext.period, period );
162 MemWrite( &item->gpuNewContext.context, m_context );
163 MemWrite( &item->gpuNewContext.flags, flags );
164 MemWrite( &item->gpuNewContext.type, GpuContextType::Vulkan );
166 #ifdef TRACY_ON_DEMAND
177 vkDestroyQueryPool( m_device, m_query,
nullptr );
180 void Collect( VkCommandBuffer cmdbuf )
184 if( m_tail == m_head )
return;
186 #ifdef TRACY_ON_DEMAND
189 vkCmdResetQueryPool( cmdbuf, m_query, 0, m_queryCount );
192 if( m_timeDomain != VK_TIME_DOMAIN_DEVICE_EXT ) Calibrate( m_device, m_prevCalibration, tgpu );
205 cnt = m_head < m_tail ? m_queryCount - m_tail : m_head - m_tail;
208 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 )
214 for(
unsigned int idx=0; idx<cnt; idx++ )
217 MemWrite( &item->hdr.type, QueueType::GpuTime );
218 MemWrite( &item->gpuTime.gpuTime, m_res[idx] );
220 MemWrite( &item->gpuTime.context, m_context );
224 if( m_timeDomain != VK_TIME_DOMAIN_DEVICE_EXT )
227 Calibrate( m_device, tcpu, tgpu );
229 const auto delta = tcpu - m_prevCalibration;
232 m_prevCalibration = tcpu;
234 MemWrite( &item->hdr.type, QueueType::GpuCalibration );
235 MemWrite( &item->gpuCalibration.gpuTime, tgpu );
236 MemWrite( &item->gpuCalibration.cpuTime, refCpu );
237 MemWrite( &item->gpuCalibration.cpuDelta, delta );
238 MemWrite( &item->gpuCalibration.context, m_context );
243 vkCmdResetQueryPool( cmdbuf, m_query, m_tail, cnt );
246 if( m_tail == m_queryCount ) m_tail = 0;
252 const auto id = m_head;
253 m_head = ( m_head + 1 ) % m_queryCount;
254 assert( m_head != m_tail );
265 assert( m_timeDomain != VK_TIME_DOMAIN_DEVICE_EXT );
266 VkCalibratedTimestampInfoEXT spec[2] = {
267 { VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT,
nullptr, VK_TIME_DOMAIN_DEVICE_EXT },
268 { VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT,
nullptr, m_timeDomain },
274 m_vkGetCalibratedTimestampsEXT( device, 2, spec, ts, &deviation );
276 while( deviation > m_deviation );
278 #if defined _WIN32 || defined __CYGWIN__
280 tCpu = ts[1] * m_qpcToNs;
288 VkTimeDomainEXT m_timeDomain;
296 unsigned int m_oldCnt;
297 unsigned int m_queryCount;
301 PFN_vkGetCalibratedTimestampsEXT m_vkGetCalibratedTimestampsEXT;
307 tracy_force_inline VkCtxScope( VkCtx* ctx,
const SourceLocationData* srcloc, VkCommandBuffer cmdbuf,
bool is_active )
308 #ifdef TRACY_ON_DEMAND
309 : m_active( is_active &&
GetProfiler().IsConnected() )
311 : m_active( is_active )
314 if( !m_active )
return;
318 const auto queryId = ctx->NextQueryId();
319 vkCmdWriteTimestamp( cmdbuf, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, ctx->m_query,
queryId );
322 MemWrite( &item->hdr.type, QueueType::GpuZoneBeginSerial );
327 MemWrite( &item->gpuZoneBegin.context, ctx->GetId() );
331 tracy_force_inline VkCtxScope( VkCtx* ctx,
const SourceLocationData* srcloc, VkCommandBuffer cmdbuf,
int depth,
bool is_active )
332 #ifdef TRACY_ON_DEMAND
333 : m_active( is_active &&
GetProfiler().IsConnected() )
335 : m_active( is_active )
338 if( !m_active )
return;
342 const auto queryId = ctx->NextQueryId();
343 vkCmdWriteTimestamp( cmdbuf, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, ctx->m_query,
queryId );
346 MemWrite( &item->hdr.type, QueueType::GpuZoneBeginCallstackSerial );
351 MemWrite( &item->gpuZoneBegin.context, ctx->GetId() );
359 if( !m_active )
return;
361 const auto queryId = m_ctx->NextQueryId();
362 vkCmdWriteTimestamp( m_cmdbuf, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, m_ctx->m_query,
queryId );
365 MemWrite( &item->hdr.type, QueueType::GpuZoneEndSerial );
369 MemWrite( &item->gpuZoneEnd.context, m_ctx->GetId() );
376 VkCommandBuffer m_cmdbuf;
380 static inline VkCtx* CreateVkContext( VkPhysicalDevice physdev, VkDevice device, VkQueue queue, VkCommandBuffer cmdbuf, PFN_vkGetPhysicalDeviceCalibrateableTimeDomainsEXT gpdctd, PFN_vkGetCalibratedTimestampsEXT gct )
384 new(ctx) VkCtx( physdev, device, queue, cmdbuf, gpdctd, gct );
388 static inline void DestroyVkContext( VkCtx* ctx )
396 using TracyVkCtx = tracy::VkCtx*;
398 #define TracyVkContext( physdev, device, queue, cmdbuf ) tracy::CreateVkContext( physdev, device, queue, cmdbuf, nullptr, nullptr );
399 #define TracyVkContextCalibrated( physdev, device, queue, cmdbuf, gpdctd, gct ) tracy::CreateVkContext( physdev, device, queue, cmdbuf, gpdctd, gct );
400 #define TracyVkDestroy( ctx ) tracy::DestroyVkContext( ctx );
401 #if defined TRACY_HAS_CALLSTACK && defined TRACY_CALLSTACK
402 # 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 );
403 # 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 );
404 # define TracyVkZone( ctx, cmdbuf, name ) TracyVkNamedZoneS( ctx, ___tracy_gpu_zone, cmdbuf, name, TRACY_CALLSTACK, true )
405 # define TracyVkZoneC( ctx, cmdbuf, name, color ) TracyVkNamedZoneCS( ctx, ___tracy_gpu_zone, cmdbuf, name, color, TRACY_CALLSTACK, true )
407 # 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 );
408 # 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 );
409 # define TracyVkZone( ctx, cmdbuf, name ) TracyVkNamedZone( ctx, ___tracy_gpu_zone, cmdbuf, name, true )
410 # define TracyVkZoneC( ctx, cmdbuf, name, color ) TracyVkNamedZoneC( ctx, ___tracy_gpu_zone, cmdbuf, name, color, true )
412 #define TracyVkCollect( ctx, cmdbuf ) ctx->Collect( cmdbuf );
414 #ifdef TRACY_HAS_CALLSTACK
415 # 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 );
416 # 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 );
417 # define TracyVkZoneS( ctx, cmdbuf, name, depth ) TracyVkNamedZoneS( ctx, ___tracy_gpu_zone, cmdbuf, name, depth, true )
418 # define TracyVkZoneCS( ctx, cmdbuf, name, color, depth ) TracyVkNamedZoneCS( ctx, ___tracy_gpu_zone, cmdbuf, name, color, depth, true )
420 # define TracyVkNamedZoneS( ctx, varname, cmdbuf, name, depth, active ) TracyVkNamedZone( ctx, varname, cmdbuf, name, active )
421 # define TracyVkNamedZoneCS( ctx, varname, cmdbuf, name, color, depth, active ) TracyVkNamedZoneC( ctx, varname, cmdbuf, name, color, active )
422 # define TracyVkZoneS( ctx, cmdbuf, name, depth ) TracyVkZone( ctx, cmdbuf, name )
423 # define TracyVkZoneCS( ctx, cmdbuf, name, color, depth ) TracyVkZoneC( ctx, cmdbuf, name, color )