tracy/TracyVulkan.hpp

320 lines
12 KiB
C++
Raw Normal View History

2018-06-17 16:14:37 +00:00
#ifndef __TRACYVULKAN_HPP__
#define __TRACYVULKAN_HPP__
#if !defined TRACY_ENABLE
#define TracyVkContext(x,y,z,w) nullptr
#define TracyVkDestroy(x)
2020-01-25 16:44:46 +00:00
#define TracyVkNamedZone(c,x,y,z,w)
#define TracyVkNamedZoneC(c,x,y,z,w,a)
#define TracyVkZone(c,x,y)
#define TracyVkZoneC(c,x,y,z)
#define TracyVkCollect(c,x)
2020-01-25 16:44:46 +00:00
#define TracyVkNamedZoneS(c,x,y,z,w,a)
#define TracyVkNamedZoneCS(c,x,y,z,w,v,a)
#define TracyVkZoneS(c,x,y,z)
#define TracyVkZoneCS(c,x,y,z,w)
namespace tracy
{
class VkCtxScope {};
}
using TracyVkCtx = void*;
2018-06-17 16:14:37 +00:00
#else
2018-06-22 13:10:23 +00:00
#include <assert.h>
2018-06-17 16:52:20 +00:00
#include <stdlib.h>
2018-06-17 16:14:37 +00:00
#include <vulkan/vulkan.h>
#include "Tracy.hpp"
#include "client/TracyProfiler.hpp"
2018-06-21 23:47:08 +00:00
#include "client/TracyCallstack.hpp"
2018-06-17 16:14:37 +00:00
namespace tracy
{
class VkCtx
{
friend class VkCtxScope;
enum { QueryCount = 64 * 1024 };
public:
VkCtx( VkPhysicalDevice physdev, VkDevice device, VkQueue queue, VkCommandBuffer cmdbuf )
: m_device( device )
2019-02-19 18:33:37 +00:00
, m_context( GetGpuCtxCounter().fetch_add( 1, std::memory_order_relaxed ) )
2018-06-17 16:14:37 +00:00
, m_head( 0 )
, m_tail( 0 )
, m_oldCnt( 0 )
2019-06-26 14:42:51 +00:00
, m_queryCount( QueryCount )
2018-06-17 16:14:37 +00:00
{
2018-06-22 13:10:23 +00:00
assert( m_context != 255 );
2018-06-17 16:14:37 +00:00
VkPhysicalDeviceProperties prop;
vkGetPhysicalDeviceProperties( physdev, &prop );
const float period = prop.limits.timestampPeriod;
2018-06-17 16:14:37 +00:00
VkQueryPoolCreateInfo poolInfo = {};
poolInfo.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO;
2019-06-26 14:42:51 +00:00
poolInfo.queryCount = m_queryCount;
2018-06-17 16:14:37 +00:00
poolInfo.queryType = VK_QUERY_TYPE_TIMESTAMP;
while( vkCreateQueryPool( device, &poolInfo, nullptr, &m_query ) != VK_SUCCESS )
{
m_queryCount /= 2;
poolInfo.queryCount = m_queryCount;
}
2018-06-17 16:14:37 +00:00
VkCommandBufferBeginInfo beginInfo = {};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
VkSubmitInfo submitInfo = {};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &cmdbuf;
vkBeginCommandBuffer( cmdbuf, &beginInfo );
2019-06-26 14:42:51 +00:00
vkCmdResetQueryPool( cmdbuf, m_query, 0, m_queryCount );
2018-06-17 16:14:37 +00:00
vkEndCommandBuffer( cmdbuf );
vkQueueSubmit( queue, 1, &submitInfo, VK_NULL_HANDLE );
vkQueueWaitIdle( queue );
vkBeginCommandBuffer( cmdbuf, &beginInfo );
vkCmdWriteTimestamp( cmdbuf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, m_query, 0 );
vkEndCommandBuffer( cmdbuf );
vkQueueSubmit( queue, 1, &submitInfo, VK_NULL_HANDLE );
vkQueueWaitIdle( queue );
int64_t tcpu = Profiler::GetTime();
int64_t tgpu;
vkGetQueryPoolResults( device, m_query, 0, 1, sizeof( tgpu ), &tgpu, sizeof( tgpu ), VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT );
vkBeginCommandBuffer( cmdbuf, &beginInfo );
vkCmdResetQueryPool( cmdbuf, m_query, 0, 1 );
vkEndCommandBuffer( cmdbuf );
vkQueueSubmit( queue, 1, &submitInfo, VK_NULL_HANDLE );
vkQueueWaitIdle( queue );
auto item = Profiler::QueueSerial();
2018-06-17 16:14:37 +00:00
MemWrite( &item->hdr.type, QueueType::GpuNewContext );
MemWrite( &item->gpuNewContext.cpuTime, tcpu );
MemWrite( &item->gpuNewContext.gpuTime, tgpu );
2018-06-17 16:52:20 +00:00
memset( &item->gpuNewContext.thread, 0, sizeof( item->gpuNewContext.thread ) );
MemWrite( &item->gpuNewContext.period, period );
2018-06-17 16:14:37 +00:00
MemWrite( &item->gpuNewContext.context, m_context );
MemWrite( &item->gpuNewContext.accuracyBits, uint8_t( 0 ) );
2018-07-11 13:00:30 +00:00
#ifdef TRACY_ON_DEMAND
2019-02-19 17:38:08 +00:00
GetProfiler().DeferItem( *item );
#endif
Profiler::QueueSerialFinish();
2019-06-26 14:42:51 +00:00
m_res = (int64_t*)tracy_malloc( sizeof( int64_t ) * m_queryCount );
2018-06-17 16:14:37 +00:00
}
~VkCtx()
{
2019-06-26 14:42:51 +00:00
tracy_free( m_res );
2018-06-17 16:14:37 +00:00
vkDestroyQueryPool( m_device, m_query, nullptr );
}
void Collect( VkCommandBuffer cmdbuf )
{
ZoneScopedC( Color::Red4 );
if( m_tail == m_head ) return;
2018-07-11 15:03:00 +00:00
#ifdef TRACY_ON_DEMAND
2019-02-19 17:38:08 +00:00
if( !GetProfiler().IsConnected() )
2018-07-11 15:03:00 +00:00
{
2019-06-26 14:42:51 +00:00
vkCmdResetQueryPool( cmdbuf, m_query, 0, m_queryCount );
2018-07-11 15:03:00 +00:00
m_head = m_tail = 0;
return;
}
#endif
unsigned int cnt;
if( m_oldCnt != 0 )
{
cnt = m_oldCnt;
m_oldCnt = 0;
}
else
{
2019-06-26 14:42:51 +00:00
cnt = m_head < m_tail ? m_queryCount - m_tail : m_head - m_tail;
}
2019-06-26 14:42:51 +00:00
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 )
{
m_oldCnt = cnt;
return;
}
2018-06-17 16:14:37 +00:00
for( unsigned int idx=0; idx<cnt; idx++ )
2018-06-17 16:14:37 +00:00
{
auto item = Profiler::QueueSerial();
2018-06-17 16:14:37 +00:00
MemWrite( &item->hdr.type, QueueType::GpuTime );
2019-06-26 14:42:51 +00:00
MemWrite( &item->gpuTime.gpuTime, m_res[idx] );
2018-06-22 14:19:53 +00:00
MemWrite( &item->gpuTime.queryId, uint16_t( m_tail + idx ) );
2018-06-17 16:14:37 +00:00
MemWrite( &item->gpuTime.context, m_context );
Profiler::QueueSerialFinish();
2018-06-17 16:14:37 +00:00
}
vkCmdResetQueryPool( cmdbuf, m_query, m_tail, cnt );
2018-06-17 16:14:37 +00:00
m_tail += cnt;
2019-06-26 14:42:51 +00:00
if( m_tail == m_queryCount ) m_tail = 0;
2018-06-17 16:14:37 +00:00
}
private:
tracy_force_inline unsigned int NextQueryId()
{
const auto id = m_head;
2019-06-26 14:42:51 +00:00
m_head = ( m_head + 1 ) % m_queryCount;
2018-06-17 16:14:37 +00:00
assert( m_head != m_tail );
return id;
}
2018-06-22 13:10:23 +00:00
tracy_force_inline uint8_t GetId() const
2018-06-17 16:14:37 +00:00
{
return m_context;
}
VkDevice m_device;
VkQueryPool m_query;
2018-06-22 13:10:23 +00:00
uint8_t m_context;
2018-06-17 16:14:37 +00:00
unsigned int m_head;
unsigned int m_tail;
unsigned int m_oldCnt;
2019-06-26 14:42:51 +00:00
unsigned int m_queryCount;
int64_t* m_res;
2018-06-17 16:14:37 +00:00
};
class VkCtxScope
{
public:
2020-01-25 16:44:46 +00:00
tracy_force_inline VkCtxScope( VkCtx* ctx, const SourceLocationData* srcloc, VkCommandBuffer cmdbuf, bool is_active )
2018-07-11 15:03:00 +00:00
#ifdef TRACY_ON_DEMAND
2020-01-25 16:44:46 +00:00
: m_active( is_active && GetProfiler().IsConnected() )
#else
: m_active( is_active )
2018-07-11 15:03:00 +00:00
#endif
2018-06-17 16:14:37 +00:00
{
2018-07-11 15:03:00 +00:00
if( !m_active ) return;
2020-01-25 16:44:46 +00:00
m_cmdbuf = cmdbuf;
m_ctx = ctx;
2018-06-22 13:57:54 +00:00
const auto queryId = ctx->NextQueryId();
vkCmdWriteTimestamp( cmdbuf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, ctx->m_query, queryId );
2018-06-17 16:14:37 +00:00
auto item = Profiler::QueueSerial();
MemWrite( &item->hdr.type, QueueType::GpuZoneBeginSerial );
2018-06-17 16:14:37 +00:00
MemWrite( &item->gpuZoneBegin.cpuTime, Profiler::GetTime() );
MemWrite( &item->gpuZoneBegin.srcloc, (uint64_t)srcloc );
MemWrite( &item->gpuZoneBegin.thread, GetThreadHandle() );
2018-06-22 13:57:54 +00:00
MemWrite( &item->gpuZoneBegin.queryId, uint16_t( queryId ) );
2018-06-17 16:14:37 +00:00
MemWrite( &item->gpuZoneBegin.context, ctx->GetId() );
Profiler::QueueSerialFinish();
2018-06-17 16:14:37 +00:00
}
2020-01-25 16:44:46 +00:00
tracy_force_inline VkCtxScope( VkCtx* ctx, const SourceLocationData* srcloc, VkCommandBuffer cmdbuf, int depth, bool is_active )
2018-07-11 15:03:00 +00:00
#ifdef TRACY_ON_DEMAND
2020-01-25 16:44:46 +00:00
: m_active( is_active && GetProfiler().IsConnected() )
#else
: m_active( is_active )
2018-07-11 15:03:00 +00:00
#endif
2018-06-21 23:47:08 +00:00
{
2018-07-11 15:03:00 +00:00
if( !m_active ) return;
2020-01-25 16:44:46 +00:00
m_cmdbuf = cmdbuf;
m_ctx = ctx;
2018-06-21 23:47:08 +00:00
2018-06-22 13:57:54 +00:00
const auto queryId = ctx->NextQueryId();
vkCmdWriteTimestamp( cmdbuf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, ctx->m_query, queryId );
2018-06-21 23:47:08 +00:00
auto item = Profiler::QueueSerial();
MemWrite( &item->hdr.type, QueueType::GpuZoneBeginCallstackSerial );
2018-06-21 23:47:08 +00:00
MemWrite( &item->gpuZoneBegin.cpuTime, Profiler::GetTime() );
MemWrite( &item->gpuZoneBegin.srcloc, (uint64_t)srcloc );
MemWrite( &item->gpuZoneBegin.thread, GetThreadHandle() );
2018-06-22 13:57:54 +00:00
MemWrite( &item->gpuZoneBegin.queryId, uint16_t( queryId ) );
2018-06-21 23:47:08 +00:00
MemWrite( &item->gpuZoneBegin.context, ctx->GetId() );
Profiler::QueueSerialFinish();
2018-06-21 23:47:08 +00:00
GetProfiler().SendCallstack( depth );
2018-06-21 23:47:08 +00:00
}
2018-06-17 16:14:37 +00:00
tracy_force_inline ~VkCtxScope()
{
2018-07-11 15:03:00 +00:00
if( !m_active ) return;
2020-01-25 16:44:46 +00:00
const auto queryId = m_ctx->NextQueryId();
vkCmdWriteTimestamp( m_cmdbuf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, m_ctx->m_query, queryId );
2018-06-17 16:14:37 +00:00
auto item = Profiler::QueueSerial();
MemWrite( &item->hdr.type, QueueType::GpuZoneEndSerial );
2018-06-17 16:14:37 +00:00
MemWrite( &item->gpuZoneEnd.cpuTime, Profiler::GetTime() );
MemWrite( &item->gpuZoneEnd.thread, GetThreadHandle() );
2018-06-22 13:57:54 +00:00
MemWrite( &item->gpuZoneEnd.queryId, uint16_t( queryId ) );
MemWrite( &item->gpuZoneEnd.context, m_ctx->GetId() );
Profiler::QueueSerialFinish();
2018-06-17 16:14:37 +00:00
}
private:
2020-01-25 16:44:46 +00:00
const bool m_active;
2018-06-17 16:14:37 +00:00
VkCommandBuffer m_cmdbuf;
VkCtx* m_ctx;
2018-06-17 16:14:37 +00:00
};
static inline VkCtx* CreateVkContext( VkPhysicalDevice physdev, VkDevice device, VkQueue queue, VkCommandBuffer cmdbuf )
{
InitRPMallocThread();
auto ctx = (VkCtx*)tracy_malloc( sizeof( VkCtx ) );
new(ctx) VkCtx( physdev, device, queue, cmdbuf );
return ctx;
}
static inline void DestroyVkContext( VkCtx* ctx )
{
ctx->~VkCtx();
tracy_free( ctx );
}
2018-06-17 16:14:37 +00:00
}
using TracyVkCtx = tracy::VkCtx*;
#define TracyVkContext( physdev, device, queue, cmdbuf ) tracy::CreateVkContext( physdev, device, queue, cmdbuf );
#define TracyVkDestroy( ctx ) tracy::DestroyVkContext( ctx );
#if defined TRACY_HAS_CALLSTACK && defined TRACY_CALLSTACK
2020-01-25 16:44:46 +00:00
# define TracyVkNamedZone( ctx, varname, cmdbuf, name, active ) static const 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 );
# define TracyVkNamedZoneC( ctx, varname, cmdbuf, name, color, active ) static const 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 );
# define TracyVkZone( ctx, cmdbuf, name ) TracyVkNamedZoneS( ctx, ___tracy_gpu_zone, cmdbuf, name, TRACY_CALLSTACK, true )
# define TracyVkZoneC( ctx, cmdbuf, name, color ) TracyVkNamedZoneCS( ctx, ___tracy_gpu_zone, cmdbuf, name, color, TRACY_CALLSTACK, true )
#else
2020-01-25 16:44:46 +00:00
# define TracyVkNamedZone( ctx, varname, cmdbuf, name, active ) static const 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 );
# define TracyVkNamedZoneC( ctx, varname, cmdbuf, name, color, active ) static const 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 );
# define TracyVkZone( ctx, cmdbuf, name ) TracyVkNamedZone( ctx, ___tracy_gpu_zone, cmdbuf, name, true )
# define TracyVkZoneC( ctx, cmdbuf, name, color ) TracyVkNamedZoneC( ctx, ___tracy_gpu_zone, cmdbuf, name, color, true )
#endif
#define TracyVkCollect( ctx, cmdbuf ) ctx->Collect( cmdbuf );
#ifdef TRACY_HAS_CALLSTACK
2020-01-25 16:44:46 +00:00
# define TracyVkNamedZoneS( ctx, varname, cmdbuf, name, depth, active ) static const 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 );
# define TracyVkNamedZoneCS( ctx, varname, cmdbuf, name, color, depth, active ) static const 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 );
# define TracyVkZoneS( ctx, cmdbuf, name, depth ) TracyVkNamedZoneS( ctx, ___tracy_gpu_zone, cmdbuf, name, depth, true )
# define TracyVkZoneCS( ctx, cmdbuf, name, color, depth ) TracyVkNamedZoneCS( ctx, ___tracy_gpu_zone, cmdbuf, name, color, depth, true )
#else
2020-01-25 16:44:46 +00:00
# define TracyVkNamedZoneS( ctx, varname, cmdbuf, name, depth, active ) TracyVkNamedZone( ctx, varname, cmdbuf, name, active )
# define TracyVkNamedZoneCS( ctx, varname, cmdbuf, name, color, depth, active ) TracyVkNamedZoneC( ctx, varname, cmdbuf, name, color, active )
# define TracyVkZoneS( ctx, cmdbuf, name, depth ) TracyVkZone( ctx, cmdbuf, name )
# define TracyVkZoneCS( ctx, cmdbuf, name, color, depth ) TracyVkZoneC( ctx, cmdbuf, name, color )
#endif
2018-06-17 16:14:37 +00:00
#endif
#endif