2017-11-11 18:44:09 +00:00
|
|
|
#ifndef __TRACYOPENGL_HPP__
|
|
|
|
#define __TRACYOPENGL_HPP__
|
|
|
|
|
2017-11-12 00:49:35 +00:00
|
|
|
// Include this file after you include OpenGL 3.2 headers.
|
|
|
|
|
2017-11-17 14:46:49 +00:00
|
|
|
#if !defined TRACY_ENABLE || defined __APPLE__
|
2017-11-12 00:03:14 +00:00
|
|
|
|
2017-11-13 23:48:26 +00:00
|
|
|
#define TracyGpuContext
|
2017-11-16 14:41:22 +00:00
|
|
|
#define TracyGpuZone(x)
|
|
|
|
#define TracyGpuZoneC(x,y)
|
2017-11-13 23:48:26 +00:00
|
|
|
#define TracyGpuCollect
|
2017-11-12 00:03:14 +00:00
|
|
|
|
|
|
|
#else
|
2017-11-11 18:44:09 +00:00
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
2017-11-11 21:08:47 +00:00
|
|
|
#include "Tracy.hpp"
|
2017-11-11 18:44:09 +00:00
|
|
|
#include "client/TracyProfiler.hpp"
|
2018-03-31 12:03:55 +00:00
|
|
|
#include "common/TracyAlign.hpp"
|
2017-11-13 23:48:26 +00:00
|
|
|
#include "common/TracyAlloc.hpp"
|
2017-11-11 18:44:09 +00:00
|
|
|
|
2017-11-14 22:29:48 +00:00
|
|
|
#define TracyGpuContext tracy::s_gpuCtx.ptr = (tracy::GpuCtx*)tracy::tracy_malloc( sizeof( tracy::GpuCtx ) ); new(tracy::s_gpuCtx.ptr) tracy::GpuCtx;
|
2017-11-14 22:06:45 +00:00
|
|
|
#define TracyGpuZone( name ) static const tracy::SourceLocation __tracy_gpu_source_location { name, __FUNCTION__, __FILE__, (uint32_t)__LINE__, 0 }; tracy::GpuCtxScope ___tracy_gpu_zone( &__tracy_gpu_source_location );
|
|
|
|
#define TracyGpuZoneC( name, color ) static const tracy::SourceLocation __tracy_gpu_source_location { name, __FUNCTION__, __FILE__, (uint32_t)__LINE__, color }; tracy::GpuCtxScope ___tracy_gpu_zone( &__tracy_gpu_source_location );
|
2017-11-14 22:29:48 +00:00
|
|
|
#define TracyGpuCollect tracy::s_gpuCtx.ptr->Collect();
|
2017-11-11 20:09:48 +00:00
|
|
|
|
2017-11-11 18:44:09 +00:00
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
|
|
|
extern std::atomic<uint16_t> s_gpuCtxCounter;
|
|
|
|
|
|
|
|
class GpuCtx
|
|
|
|
{
|
2017-11-13 23:48:26 +00:00
|
|
|
friend class GpuCtxScope;
|
|
|
|
|
2017-11-22 13:01:44 +00:00
|
|
|
enum { QueryCount = 64 * 1024 };
|
2017-11-11 20:09:48 +00:00
|
|
|
|
2017-11-11 18:44:09 +00:00
|
|
|
public:
|
|
|
|
GpuCtx()
|
|
|
|
: m_context( s_gpuCtxCounter.fetch_add( 1, std::memory_order_relaxed ) )
|
2017-11-11 20:09:48 +00:00
|
|
|
, m_head( 0 )
|
|
|
|
, m_tail( 0 )
|
2017-11-11 18:44:09 +00:00
|
|
|
{
|
2017-11-13 23:48:26 +00:00
|
|
|
glGenQueries( QueryCount, m_query );
|
2017-11-11 18:44:09 +00:00
|
|
|
|
|
|
|
int64_t tgpu;
|
|
|
|
glGetInteger64v( GL_TIMESTAMP, &tgpu );
|
|
|
|
int64_t tcpu = Profiler::GetTime();
|
|
|
|
|
2017-11-17 13:07:42 +00:00
|
|
|
GLint bits;
|
|
|
|
glGetQueryiv( GL_TIMESTAMP, GL_QUERY_COUNTER_BITS, &bits );
|
|
|
|
|
2018-06-17 16:21:15 +00:00
|
|
|
const float period = 1.f;
|
2017-11-11 18:44:09 +00:00
|
|
|
Magic magic;
|
|
|
|
auto& token = s_token.ptr;
|
|
|
|
auto& tail = token->get_tail_index();
|
|
|
|
auto item = token->enqueue_begin<moodycamel::CanAlloc>( magic );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->hdr.type, QueueType::GpuNewContext );
|
|
|
|
MemWrite( &item->gpuNewContext.cpuTime, tcpu );
|
|
|
|
MemWrite( &item->gpuNewContext.gpuTime, tgpu );
|
|
|
|
MemWrite( &item->gpuNewContext.thread, GetThreadHandle() );
|
2018-06-17 16:21:15 +00:00
|
|
|
MemWrite( &item->gpuNewContext.period, period );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->gpuNewContext.context, m_context );
|
|
|
|
MemWrite( &item->gpuNewContext.accuracyBits, (uint8_t)bits );
|
2017-11-11 18:44:09 +00:00
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
}
|
|
|
|
|
2017-11-11 21:08:47 +00:00
|
|
|
void Collect()
|
|
|
|
{
|
2017-11-25 14:32:44 +00:00
|
|
|
ZoneScopedC( Color::Red4 );
|
2017-11-11 21:08:47 +00:00
|
|
|
|
|
|
|
auto start = m_tail;
|
2017-11-13 23:48:26 +00:00
|
|
|
auto end = m_head + QueryCount;
|
|
|
|
auto cnt = ( end - start ) % QueryCount;
|
2017-11-11 21:08:47 +00:00
|
|
|
while( cnt > 1 )
|
|
|
|
{
|
|
|
|
auto mid = start + cnt / 2;
|
|
|
|
GLint available;
|
2017-11-13 23:48:26 +00:00
|
|
|
glGetQueryObjectiv( m_query[mid % QueryCount], GL_QUERY_RESULT_AVAILABLE, &available );
|
2017-11-11 21:08:47 +00:00
|
|
|
if( available )
|
|
|
|
{
|
|
|
|
start = mid;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
end = mid;
|
|
|
|
}
|
2017-11-13 23:48:26 +00:00
|
|
|
cnt = ( end - start ) % QueryCount;
|
2017-11-11 21:08:47 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 23:48:26 +00:00
|
|
|
start %= QueryCount;
|
2017-11-11 21:08:47 +00:00
|
|
|
|
2017-11-25 12:41:12 +00:00
|
|
|
Magic magic;
|
|
|
|
auto& token = s_token.ptr;
|
|
|
|
auto& tail = token->get_tail_index();
|
|
|
|
|
2017-11-11 21:08:47 +00:00
|
|
|
while( m_tail != start )
|
|
|
|
{
|
|
|
|
uint64_t time;
|
|
|
|
glGetQueryObjectui64v( m_query[m_tail], GL_QUERY_RESULT, &time );
|
|
|
|
|
|
|
|
auto item = token->enqueue_begin<moodycamel::CanAlloc>( magic );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->hdr.type, QueueType::GpuTime );
|
|
|
|
MemWrite( &item->gpuTime.gpuTime, (int64_t)time );
|
|
|
|
MemWrite( &item->gpuTime.context, m_context );
|
2017-11-11 21:08:47 +00:00
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
2017-11-13 23:48:26 +00:00
|
|
|
m_tail = ( m_tail + 1 ) % QueryCount;
|
2017-11-11 21:08:47 +00:00
|
|
|
}
|
2017-11-25 12:33:57 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
int64_t tgpu;
|
|
|
|
glGetInteger64v( GL_TIMESTAMP, &tgpu );
|
|
|
|
int64_t tcpu = Profiler::GetTime();
|
|
|
|
|
|
|
|
auto item = token->enqueue_begin<moodycamel::CanAlloc>( magic );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->hdr.type, QueueType::GpuResync );
|
|
|
|
MemWrite( &item->gpuResync.cpuTime, tcpu );
|
|
|
|
MemWrite( &item->gpuResync.gpuTime, tgpu );
|
|
|
|
MemWrite( &item->gpuResync.context, m_context );
|
2017-11-25 12:33:57 +00:00
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
}
|
2017-11-11 21:08:47 +00:00
|
|
|
}
|
|
|
|
|
2017-11-11 18:44:09 +00:00
|
|
|
private:
|
2017-11-11 20:09:48 +00:00
|
|
|
tracy_force_inline unsigned int NextQueryId()
|
|
|
|
{
|
|
|
|
const auto id = m_head;
|
2017-11-13 23:48:26 +00:00
|
|
|
m_head = ( m_head + 1 ) % QueryCount;
|
2017-11-11 20:19:51 +00:00
|
|
|
assert( m_head != m_tail );
|
2017-11-11 20:09:48 +00:00
|
|
|
return m_query[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
tracy_force_inline uint16_t GetId() const
|
|
|
|
{
|
|
|
|
return m_context;
|
|
|
|
}
|
|
|
|
|
2017-11-13 23:48:26 +00:00
|
|
|
unsigned int m_query[QueryCount];
|
2017-11-11 18:44:09 +00:00
|
|
|
uint16_t m_context;
|
2017-11-11 20:09:48 +00:00
|
|
|
|
|
|
|
unsigned int m_head;
|
|
|
|
unsigned int m_tail;
|
2017-11-11 18:44:09 +00:00
|
|
|
};
|
|
|
|
|
2017-11-14 22:29:48 +00:00
|
|
|
extern thread_local GpuCtxWrapper s_gpuCtx;
|
2017-11-13 23:48:26 +00:00
|
|
|
|
|
|
|
class GpuCtxScope
|
|
|
|
{
|
|
|
|
public:
|
2017-11-14 22:06:45 +00:00
|
|
|
tracy_force_inline GpuCtxScope( const SourceLocation* srcloc )
|
2017-11-13 23:48:26 +00:00
|
|
|
{
|
2017-11-14 22:29:48 +00:00
|
|
|
glQueryCounter( s_gpuCtx.ptr->NextQueryId(), GL_TIMESTAMP );
|
2017-11-13 23:48:26 +00:00
|
|
|
|
|
|
|
Magic magic;
|
|
|
|
auto& token = s_token.ptr;
|
|
|
|
auto& tail = token->get_tail_index();
|
|
|
|
auto item = token->enqueue_begin<moodycamel::CanAlloc>( magic );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->hdr.type, QueueType::GpuZoneBegin );
|
|
|
|
MemWrite( &item->gpuZoneBegin.cpuTime, Profiler::GetTime() );
|
|
|
|
MemWrite( &item->gpuZoneBegin.srcloc, (uint64_t)srcloc );
|
|
|
|
MemWrite( &item->gpuZoneBegin.context, s_gpuCtx.ptr->GetId() );
|
2017-11-13 23:48:26 +00:00
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
}
|
|
|
|
|
|
|
|
tracy_force_inline ~GpuCtxScope()
|
|
|
|
{
|
2017-11-14 22:29:48 +00:00
|
|
|
glQueryCounter( s_gpuCtx.ptr->NextQueryId(), GL_TIMESTAMP );
|
2017-11-13 23:48:26 +00:00
|
|
|
|
|
|
|
Magic magic;
|
|
|
|
auto& token = s_token.ptr;
|
|
|
|
auto& tail = token->get_tail_index();
|
|
|
|
auto item = token->enqueue_begin<moodycamel::CanAlloc>( magic );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->hdr.type, QueueType::GpuZoneEnd );
|
|
|
|
MemWrite( &item->gpuZoneEnd.cpuTime, Profiler::GetTime() );
|
|
|
|
MemWrite( &item->gpuZoneEnd.context, s_gpuCtx.ptr->GetId() );
|
2017-11-13 23:48:26 +00:00
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-11 18:44:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|