2018-06-17 16:14:37 +00:00
|
|
|
#ifndef __TRACYVULKAN_HPP__
|
|
|
|
#define __TRACYVULKAN_HPP__
|
|
|
|
|
|
|
|
#if !defined TRACY_ENABLE
|
|
|
|
|
|
|
|
#define TracyVkContext(x,y,z,w)
|
|
|
|
#define TracyVkDestroy
|
|
|
|
#define TracyVkZone(x,y)
|
|
|
|
#define TracyVkZoneC(x,y,z)
|
|
|
|
#define TracyVkCollect(x)
|
2018-06-21 23:47:08 +00:00
|
|
|
#define TracyVkZoneS(x,y,z)
|
|
|
|
#define TracyVkZoneCS(x,y,z,w)
|
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
|
|
|
|
|
|
|
#define TracyVkContext( physdev, device, queue, cmdbuf ) tracy::s_vkCtx.ptr = (tracy::VkCtx*)tracy::tracy_malloc( sizeof( tracy::VkCtx ) ); new(tracy::s_vkCtx.ptr) tracy::VkCtx( physdev, device, queue, cmdbuf );
|
|
|
|
#define TracyVkDestroy() tracy::s_vkCtx.ptr->~VkCtx(); tracy::tracy_free( tracy::s_vkCtx.ptr ); tracy::s_vkCtx.ptr = nullptr;
|
2018-07-26 17:52:27 +00:00
|
|
|
#define TracyVkZone( cmdbuf, name ) static const tracy::SourceLocation TracyConcat(__tracy_gpu_source_location,__LINE__) { name, __FUNCTION__, __FILE__, (uint32_t)__LINE__, 0 }; tracy::VkCtxScope ___tracy_gpu_zone( &TracyConcat(__tracy_gpu_source_location,__LINE__), cmdbuf );
|
|
|
|
#define TracyVkZoneC( cmdbuf, name, color ) static const tracy::SourceLocation TracyConcat(__tracy_gpu_source_location,__LINE__) { name, __FUNCTION__, __FILE__, (uint32_t)__LINE__, color }; tracy::VkCtxScope ___tracy_gpu_zone( &TracyConcat(__tracy_gpu_source_location,__LINE__), cmdbuf );
|
2018-06-17 16:14:37 +00:00
|
|
|
#define TracyVkCollect( cmdbuf ) tracy::s_vkCtx.ptr->Collect( cmdbuf );
|
|
|
|
|
2018-06-21 23:47:08 +00:00
|
|
|
#ifdef TRACY_HAS_CALLSTACK
|
2018-07-26 17:52:27 +00:00
|
|
|
# define TracyVkZoneS( cmdbuf, name, depth ) static const tracy::SourceLocation TracyConcat(__tracy_gpu_source_location,__LINE__) { name, __FUNCTION__, __FILE__, (uint32_t)__LINE__, 0 }; tracy::VkCtxScope ___tracy_gpu_zone( &TracyConcat(__tracy_gpu_source_location,__LINE__), cmdbuf, depth );
|
|
|
|
# define TracyVkZoneCS( cmdbuf, name, color, depth ) static const tracy::SourceLocation TracyConcat(__tracy_gpu_source_location,__LINE__) { name, __FUNCTION__, __FILE__, (uint32_t)__LINE__, color }; tracy::VkCtxScope ___tracy_gpu_zone( &TracyConcat(__tracy_gpu_source_location,__LINE__), cmdbuf, depth );
|
2018-06-21 23:47:08 +00:00
|
|
|
#else
|
|
|
|
# define TracyVkZoneS( cmdbuf, name, depth ) TracyVkZone( cmdbuf, name )
|
|
|
|
# define TracyVkZoneCS( cmdbuf, name, color, depth ) TracyVkZoneC( cmdbuf, name, color )
|
|
|
|
#endif
|
|
|
|
|
2018-06-17 16:14:37 +00:00
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
2018-06-22 13:10:23 +00:00
|
|
|
extern std::atomic<uint8_t> s_gpuCtxCounter;
|
2018-06-17 16:14:37 +00:00
|
|
|
|
|
|
|
class VkCtx
|
|
|
|
{
|
|
|
|
friend class VkCtxScope;
|
|
|
|
|
|
|
|
enum { QueryCount = 64 * 1024 };
|
|
|
|
|
|
|
|
public:
|
|
|
|
VkCtx( VkPhysicalDevice physdev, VkDevice device, VkQueue queue, VkCommandBuffer cmdbuf )
|
|
|
|
: m_device( device )
|
|
|
|
, m_queue( queue )
|
2018-06-17 23:10:43 +00:00
|
|
|
, m_context( s_gpuCtxCounter.fetch_add( 1, std::memory_order_relaxed ) )
|
2018-06-17 16:14:37 +00:00
|
|
|
, m_head( 0 )
|
|
|
|
, m_tail( 0 )
|
2018-06-17 18:56:46 +00:00
|
|
|
, m_oldCnt( 0 )
|
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 );
|
2018-06-17 16:21:15 +00:00
|
|
|
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;
|
|
|
|
poolInfo.queryCount = QueryCount;
|
|
|
|
poolInfo.queryType = VK_QUERY_TYPE_TIMESTAMP;
|
|
|
|
vkCreateQueryPool( device, &poolInfo, nullptr, &m_query );
|
|
|
|
|
|
|
|
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 );
|
|
|
|
vkCmdResetQueryPool( cmdbuf, m_query, 0, QueryCount );
|
|
|
|
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 );
|
|
|
|
|
|
|
|
Magic magic;
|
|
|
|
auto& token = s_token.ptr;
|
|
|
|
auto& tail = token->get_tail_index();
|
2018-07-13 18:01:27 +00:00
|
|
|
auto item = token->enqueue_begin<tracy::moodycamel::CanAlloc>( magic );
|
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 ) );
|
2018-06-17 16:21:15 +00:00
|
|
|
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
|
|
|
|
s_profiler.DeferItem( *item );
|
2018-07-10 18:42:30 +00:00
|
|
|
#endif
|
2018-07-11 13:00:30 +00:00
|
|
|
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
2018-06-17 16:14:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~VkCtx()
|
|
|
|
{
|
|
|
|
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
|
|
|
|
if( !s_profiler.IsConnected() )
|
|
|
|
{
|
|
|
|
vkCmdResetQueryPool( cmdbuf, m_query, 0, QueryCount );
|
|
|
|
m_head = m_tail = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-06-17 18:56:46 +00:00
|
|
|
unsigned int cnt;
|
|
|
|
if( m_oldCnt != 0 )
|
|
|
|
{
|
|
|
|
cnt = m_oldCnt;
|
|
|
|
m_oldCnt = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cnt = m_head < m_tail ? QueryCount - m_tail : m_head - m_tail;
|
|
|
|
}
|
2018-06-17 18:48:02 +00:00
|
|
|
|
2018-06-17 18:56:46 +00:00
|
|
|
int64_t res[QueryCount];
|
|
|
|
if( vkGetQueryPoolResults( m_device, m_query, m_tail, cnt, sizeof( res ), res, sizeof( *res ), VK_QUERY_RESULT_64_BIT ) == VK_NOT_READY )
|
2018-06-17 18:48:02 +00:00
|
|
|
{
|
2018-06-17 18:56:46 +00:00
|
|
|
m_oldCnt = cnt;
|
|
|
|
return;
|
2018-06-17 18:48:02 +00:00
|
|
|
}
|
2018-06-17 16:14:37 +00:00
|
|
|
|
|
|
|
Magic magic;
|
|
|
|
auto& token = s_token.ptr;
|
|
|
|
auto& tail = token->get_tail_index();
|
|
|
|
|
2018-06-17 18:48:02 +00:00
|
|
|
for( unsigned int idx=0; idx<cnt; idx++ )
|
2018-06-17 16:14:37 +00:00
|
|
|
{
|
2018-07-13 18:01:27 +00:00
|
|
|
auto item = token->enqueue_begin<tracy::moodycamel::CanAlloc>( magic );
|
2018-06-17 16:14:37 +00:00
|
|
|
MemWrite( &item->hdr.type, QueueType::GpuTime );
|
|
|
|
MemWrite( &item->gpuTime.gpuTime, 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 );
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
}
|
|
|
|
|
2018-06-17 18:48:02 +00:00
|
|
|
vkCmdResetQueryPool( cmdbuf, m_query, m_tail, cnt );
|
2018-06-17 16:14:37 +00:00
|
|
|
|
2018-06-17 18:48:02 +00:00
|
|
|
m_tail += cnt;
|
2018-06-17 16:14:37 +00:00
|
|
|
if( m_tail == QueryCount ) m_tail = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
tracy_force_inline unsigned int NextQueryId()
|
|
|
|
{
|
|
|
|
const auto id = m_head;
|
|
|
|
m_head = ( m_head + 1 ) % QueryCount;
|
|
|
|
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;
|
|
|
|
VkQueue m_queue;
|
|
|
|
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;
|
2018-06-17 18:56:46 +00:00
|
|
|
unsigned int m_oldCnt;
|
2018-06-17 16:14:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern VkCtxWrapper s_vkCtx;
|
|
|
|
|
|
|
|
class VkCtxScope
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
tracy_force_inline VkCtxScope( const SourceLocation* srcloc, VkCommandBuffer cmdbuf )
|
|
|
|
: m_cmdbuf( cmdbuf )
|
2018-07-11 15:03:00 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
, m_active( s_profiler.IsConnected() )
|
|
|
|
#endif
|
2018-06-17 16:14:37 +00:00
|
|
|
{
|
2018-07-11 15:03:00 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
if( !m_active ) return;
|
|
|
|
#endif
|
2018-06-17 16:14:37 +00:00
|
|
|
auto ctx = s_vkCtx.ptr;
|
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
|
|
|
|
|
|
|
Magic magic;
|
|
|
|
auto& token = s_token.ptr;
|
|
|
|
auto& tail = token->get_tail_index();
|
2018-07-13 18:01:27 +00:00
|
|
|
auto item = token->enqueue_begin<tracy::moodycamel::CanAlloc>( magic );
|
2018-06-17 16:14:37 +00:00
|
|
|
MemWrite( &item->hdr.type, QueueType::GpuZoneBegin );
|
|
|
|
MemWrite( &item->gpuZoneBegin.cpuTime, Profiler::GetTime() );
|
|
|
|
MemWrite( &item->gpuZoneBegin.srcloc, (uint64_t)srcloc );
|
2018-06-17 16:55:12 +00:00
|
|
|
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() );
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
}
|
|
|
|
|
2018-06-21 23:47:08 +00:00
|
|
|
tracy_force_inline VkCtxScope( const SourceLocation* srcloc, VkCommandBuffer cmdbuf, int depth )
|
|
|
|
: m_cmdbuf( cmdbuf )
|
2018-07-11 15:03:00 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
, m_active( s_profiler.IsConnected() )
|
|
|
|
#endif
|
2018-06-21 23:47:08 +00:00
|
|
|
{
|
2018-07-11 15:03:00 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
if( !m_active ) return;
|
|
|
|
#endif
|
2018-06-21 23:47:08 +00:00
|
|
|
const auto thread = GetThreadHandle();
|
|
|
|
|
|
|
|
auto ctx = s_vkCtx.ptr;
|
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
|
|
|
|
|
|
|
Magic magic;
|
|
|
|
auto& token = s_token.ptr;
|
|
|
|
auto& tail = token->get_tail_index();
|
2018-07-13 18:01:27 +00:00
|
|
|
auto item = token->enqueue_begin<tracy::moodycamel::CanAlloc>( magic );
|
2018-06-21 23:47:08 +00:00
|
|
|
MemWrite( &item->hdr.type, QueueType::GpuZoneBeginCallstack );
|
|
|
|
MemWrite( &item->gpuZoneBegin.cpuTime, Profiler::GetTime() );
|
|
|
|
MemWrite( &item->gpuZoneBegin.srcloc, (uint64_t)srcloc );
|
|
|
|
MemWrite( &item->gpuZoneBegin.thread, thread );
|
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() );
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
|
|
|
|
s_profiler.SendCallstack( depth, thread );
|
|
|
|
}
|
|
|
|
|
2018-06-17 16:14:37 +00:00
|
|
|
tracy_force_inline ~VkCtxScope()
|
|
|
|
{
|
2018-07-11 15:03:00 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
if( !m_active ) return;
|
|
|
|
#endif
|
2018-06-17 16:14:37 +00:00
|
|
|
auto ctx = s_vkCtx.ptr;
|
2018-06-22 13:57:54 +00:00
|
|
|
const auto queryId = ctx->NextQueryId();
|
|
|
|
vkCmdWriteTimestamp( m_cmdbuf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, ctx->m_query, queryId );
|
2018-06-17 16:14:37 +00:00
|
|
|
|
|
|
|
Magic magic;
|
|
|
|
auto& token = s_token.ptr;
|
|
|
|
auto& tail = token->get_tail_index();
|
2018-07-13 18:01:27 +00:00
|
|
|
auto item = token->enqueue_begin<tracy::moodycamel::CanAlloc>( magic );
|
2018-06-17 16:14:37 +00:00
|
|
|
MemWrite( &item->hdr.type, QueueType::GpuZoneEnd );
|
|
|
|
MemWrite( &item->gpuZoneEnd.cpuTime, Profiler::GetTime() );
|
2018-06-22 13:57:54 +00:00
|
|
|
MemWrite( &item->gpuZoneEnd.queryId, uint16_t( queryId ) );
|
2018-06-17 16:14:37 +00:00
|
|
|
MemWrite( &item->gpuZoneEnd.context, ctx->GetId() );
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
VkCommandBuffer m_cmdbuf;
|
2018-07-11 15:03:00 +00:00
|
|
|
|
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
const bool m_active;
|
|
|
|
#endif
|
2018-06-17 16:14:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|