2017-11-11 18:44:09 +00:00
|
|
|
#ifndef __TRACYOPENGL_HPP__
|
|
|
|
#define __TRACYOPENGL_HPP__
|
|
|
|
|
2020-03-02 16:05:29 +00:00
|
|
|
#if !defined GL_TIMESTAMP && !defined GL_TIMESTAMP_EXT
|
2020-01-27 16:50:44 +00:00
|
|
|
# error "You must include OpenGL 3.2 headers before including TracyOpenGL.hpp"
|
|
|
|
#endif
|
2017-11-12 00:49:35 +00:00
|
|
|
|
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
|
2020-01-25 16:44:46 +00:00
|
|
|
#define TracyGpuNamedZone(x,y,z)
|
|
|
|
#define TracyGpuNamedZoneC(x,y,z,w)
|
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
|
2018-07-26 18:12:16 +00:00
|
|
|
|
2020-01-25 16:44:46 +00:00
|
|
|
#define TracyGpuNamedZoneS(x,y,z,w)
|
|
|
|
#define TracyGpuNamedZoneCS(x,y,z,w,a)
|
2018-06-22 00:12:42 +00:00
|
|
|
#define TracyGpuZoneS(x,y)
|
|
|
|
#define TracyGpuZoneCS(x,y,z)
|
2017-11-12 00:03:14 +00:00
|
|
|
|
2019-02-18 13:45:09 +00:00
|
|
|
namespace tracy
|
|
|
|
{
|
2019-02-18 13:56:53 +00:00
|
|
|
struct SourceLocationData;
|
|
|
|
class GpuCtxScope
|
|
|
|
{
|
|
|
|
public:
|
2020-01-25 16:44:46 +00:00
|
|
|
GpuCtxScope( const SourceLocationData*, bool ) {}
|
|
|
|
GpuCtxScope( const SourceLocationData*, int, bool ) {}
|
2019-02-18 13:56:53 +00:00
|
|
|
};
|
2019-02-18 13:45:09 +00:00
|
|
|
}
|
|
|
|
|
2017-11-12 00:03:14 +00:00
|
|
|
#else
|
2017-11-11 18:44:09 +00:00
|
|
|
|
|
|
|
#include <atomic>
|
2018-06-22 13:10:23 +00:00
|
|
|
#include <assert.h>
|
2018-06-17 16:55:12 +00:00
|
|
|
#include <stdlib.h>
|
2017-11-11 18:44:09 +00:00
|
|
|
|
2017-11-11 21:08:47 +00:00
|
|
|
#include "Tracy.hpp"
|
2017-11-11 18:44:09 +00:00
|
|
|
#include "client/TracyProfiler.hpp"
|
2018-06-22 00:12:42 +00:00
|
|
|
#include "client/TracyCallstack.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
|
|
|
|
2019-06-18 14:33:35 +00:00
|
|
|
#if !defined GL_TIMESTAMP && defined GL_TIMESTAMP_EXT
|
|
|
|
# define GL_TIMESTAMP GL_TIMESTAMP_EXT
|
|
|
|
# define GL_QUERY_COUNTER_BITS GL_QUERY_COUNTER_BITS_EXT
|
|
|
|
# define glGetQueryObjectiv glGetQueryObjectivEXT
|
|
|
|
# define glGetQueryObjectui64v glGetQueryObjectui64vEXT
|
|
|
|
# define glQueryCounter glQueryCounterEXT
|
|
|
|
#endif
|
|
|
|
|
2020-05-19 11:48:19 +00:00
|
|
|
#define TracyGpuContext tracy::InitRPMallocThread(); tracy::GetGpuCtx().ptr = (tracy::GpuCtx*)tracy::tracy_malloc( sizeof( tracy::GpuCtx ) ); new(tracy::GetGpuCtx().ptr) tracy::GpuCtx;
|
2018-12-13 13:43:37 +00:00
|
|
|
#if defined TRACY_HAS_CALLSTACK && defined TRACY_CALLSTACK
|
2020-08-16 00:32:17 +00:00
|
|
|
# define TracyGpuNamedZone( varname, name, active ) static constexpr tracy::SourceLocationData TracyConcat(__tracy_gpu_source_location,__LINE__) { name, __FUNCTION__, __FILE__, (uint32_t)__LINE__, 0 }; tracy::GpuCtxScope varname( &TracyConcat(__tracy_gpu_source_location,__LINE__), TRACY_CALLSTACK, active );
|
|
|
|
# define TracyGpuNamedZoneC( varname, name, color, active ) static constexpr tracy::SourceLocationData TracyConcat(__tracy_gpu_source_location,__LINE__) { name, __FUNCTION__, __FILE__, (uint32_t)__LINE__, color }; tracy::GpuCtxScope varname( &TracyConcat(__tracy_gpu_source_location,__LINE__), TRACY_CALLSTACK, active );
|
2020-01-25 16:44:46 +00:00
|
|
|
# define TracyGpuZone( name ) TracyGpuNamedZoneS( ___tracy_gpu_zone, name, TRACY_CALLSTACK, true )
|
|
|
|
# define TracyGpuZoneC( name, color ) TracyGpuNamedZoneCS( ___tracy_gpu_zone, name, color, TRACY_CALLSTACK, true )
|
2018-12-13 13:43:37 +00:00
|
|
|
#else
|
2020-08-16 00:32:17 +00:00
|
|
|
# define TracyGpuNamedZone( varname, name, active ) static constexpr tracy::SourceLocationData TracyConcat(__tracy_gpu_source_location,__LINE__) { name, __FUNCTION__, __FILE__, (uint32_t)__LINE__, 0 }; tracy::GpuCtxScope varname( &TracyConcat(__tracy_gpu_source_location,__LINE__), active );
|
|
|
|
# define TracyGpuNamedZoneC( varname, name, color, active ) static constexpr tracy::SourceLocationData TracyConcat(__tracy_gpu_source_location,__LINE__) { name, __FUNCTION__, __FILE__, (uint32_t)__LINE__, color }; tracy::GpuCtxScope varname( &TracyConcat(__tracy_gpu_source_location,__LINE__), active );
|
2020-01-25 16:44:46 +00:00
|
|
|
# define TracyGpuZone( name ) TracyGpuNamedZone( ___tracy_gpu_zone, name, true )
|
|
|
|
# define TracyGpuZoneC( name, color ) TracyGpuNamedZoneC( ___tracy_gpu_zone, name, color, true )
|
2018-12-13 13:43:37 +00:00
|
|
|
#endif
|
2019-02-19 18:33:37 +00:00
|
|
|
#define TracyGpuCollect tracy::GetGpuCtx().ptr->Collect();
|
2017-11-11 20:09:48 +00:00
|
|
|
|
2018-06-22 00:12:42 +00:00
|
|
|
#ifdef TRACY_HAS_CALLSTACK
|
2020-08-16 00:32:17 +00:00
|
|
|
# define TracyGpuNamedZoneS( varname, name, depth, active ) static constexpr tracy::SourceLocationData TracyConcat(__tracy_gpu_source_location,__LINE__) { name, __FUNCTION__, __FILE__, (uint32_t)__LINE__, 0 }; tracy::GpuCtxScope varname( &TracyConcat(__tracy_gpu_source_location,__LINE__), depth, active );
|
|
|
|
# define TracyGpuNamedZoneCS( varname, name, color, depth, active ) static constexpr tracy::SourceLocationData TracyConcat(__tracy_gpu_source_location,__LINE__) { name, __FUNCTION__, __FILE__, (uint32_t)__LINE__, color }; tracy::GpuCtxScope varname( &TracyConcat(__tracy_gpu_source_location,__LINE__), depth, active );
|
2020-01-25 16:44:46 +00:00
|
|
|
# define TracyGpuZoneS( name, depth ) TracyGpuNamedZoneS( ___tracy_gpu_zone, name, depth, true )
|
|
|
|
# define TracyGpuZoneCS( name, color, depth ) TracyGpuNamedZoneCS( ___tracy_gpu_zone, name, color, depth, true )
|
2018-06-22 00:12:42 +00:00
|
|
|
#else
|
2020-01-25 16:44:46 +00:00
|
|
|
# define TracyGpuNamedZoneS( varname, name, depth, active ) TracyGpuNamedZone( varname, name, active )
|
|
|
|
# define TracyGpuNamedZoneCS( varname, name, color, depth, active ) TracyGpuNamedZoneC( varname, name, color, active )
|
2018-06-22 00:12:42 +00:00
|
|
|
# define TracyGpuZoneS( name, depth ) TracyGpuZone( name )
|
|
|
|
# define TracyGpuZoneCS( name, color, depth ) TracyGpuZoneC( name, color )
|
|
|
|
#endif
|
|
|
|
|
2017-11-11 18:44:09 +00:00
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
|
|
|
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()
|
2019-02-19 18:33:37 +00:00
|
|
|
: m_context( GetGpuCtxCounter().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
|
|
|
{
|
2018-06-22 13:10:23 +00:00
|
|
|
assert( m_context != 255 );
|
|
|
|
|
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;
|
2019-06-24 17:38:44 +00:00
|
|
|
const auto thread = GetThreadHandle();
|
2020-01-19 14:06:11 +00:00
|
|
|
TracyLfqPrepare( QueueType::GpuNewContext );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->gpuNewContext.cpuTime, tcpu );
|
|
|
|
MemWrite( &item->gpuNewContext.gpuTime, tgpu );
|
2019-06-24 17:38:44 +00:00
|
|
|
MemWrite( &item->gpuNewContext.thread, thread );
|
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 );
|
2020-07-07 18:32:25 +00:00
|
|
|
MemWrite( &item->gpuNewContext.flags, uint8_t( 0 ) );
|
2020-05-27 16:16:53 +00:00
|
|
|
MemWrite( &item->gpuNewContext.type, GpuContextType::OpenGl );
|
2018-07-11 13:00:30 +00:00
|
|
|
|
|
|
|
#ifdef TRACY_ON_DEMAND
|
2019-02-19 17:38:08 +00:00
|
|
|
GetProfiler().DeferItem( *item );
|
2018-07-10 18:42:30 +00:00
|
|
|
#endif
|
2018-07-11 13:00:30 +00:00
|
|
|
|
2020-01-19 14:06:11 +00:00
|
|
|
TracyLfqCommit;
|
2017-11-11 18:44:09 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-07-11 15:10:34 +00:00
|
|
|
if( m_tail == m_head ) return;
|
|
|
|
|
2018-07-11 15:10:53 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
2019-02-19 17:38:08 +00:00
|
|
|
if( !GetProfiler().IsConnected() )
|
2018-07-11 15:10:53 +00:00
|
|
|
{
|
|
|
|
m_head = m_tail = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-07-16 17:37:48 +00:00
|
|
|
while( m_tail != m_head )
|
2017-11-11 21:08:47 +00:00
|
|
|
{
|
2019-07-16 17:37:48 +00:00
|
|
|
GLint available;
|
|
|
|
glGetQueryObjectiv( m_query[m_tail], GL_QUERY_RESULT_AVAILABLE, &available );
|
|
|
|
if( !available ) return;
|
|
|
|
|
2017-11-11 21:08:47 +00:00
|
|
|
uint64_t time;
|
|
|
|
glGetQueryObjectui64v( m_query[m_tail], GL_QUERY_RESULT, &time );
|
|
|
|
|
2020-01-19 14:06:11 +00:00
|
|
|
TracyLfqPrepare( QueueType::GpuTime );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->gpuTime.gpuTime, (int64_t)time );
|
2018-06-22 14:19:53 +00:00
|
|
|
MemWrite( &item->gpuTime.queryId, (uint16_t)m_tail );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->gpuTime.context, m_context );
|
2020-01-19 14:06:11 +00:00
|
|
|
TracyLfqCommit;
|
2019-07-16 17:37:48 +00:00
|
|
|
|
2017-11-13 23:48:26 +00:00
|
|
|
m_tail = ( m_tail + 1 ) % QueryCount;
|
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 );
|
2018-06-22 14:46:47 +00:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
tracy_force_inline unsigned int TranslateOpenGlQueryId( unsigned int id )
|
|
|
|
{
|
2017-11-11 20:09:48 +00:00
|
|
|
return m_query[id];
|
|
|
|
}
|
|
|
|
|
2018-06-22 13:10:23 +00:00
|
|
|
tracy_force_inline uint8_t GetId() const
|
2017-11-11 20:09:48 +00:00
|
|
|
{
|
|
|
|
return m_context;
|
|
|
|
}
|
|
|
|
|
2017-11-13 23:48:26 +00:00
|
|
|
unsigned int m_query[QueryCount];
|
2018-06-22 13:10:23 +00:00
|
|
|
uint8_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-13 23:48:26 +00:00
|
|
|
class GpuCtxScope
|
|
|
|
{
|
|
|
|
public:
|
2020-01-25 16:44:46 +00:00
|
|
|
tracy_force_inline GpuCtxScope( const SourceLocationData* srcloc, bool is_active )
|
2018-07-11 15:10:53 +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:10:53 +00:00
|
|
|
#endif
|
2017-11-13 23:48:26 +00:00
|
|
|
{
|
2018-07-11 15:10:53 +00:00
|
|
|
if( !m_active ) return;
|
2020-01-25 16:44:46 +00:00
|
|
|
|
2019-02-19 18:33:37 +00:00
|
|
|
const auto queryId = GetGpuCtx().ptr->NextQueryId();
|
|
|
|
glQueryCounter( GetGpuCtx().ptr->TranslateOpenGlQueryId( queryId ), GL_TIMESTAMP );
|
2017-11-13 23:48:26 +00:00
|
|
|
|
2020-01-19 14:06:11 +00:00
|
|
|
TracyLfqPrepare( QueueType::GpuZoneBegin );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->gpuZoneBegin.cpuTime, Profiler::GetTime() );
|
|
|
|
MemWrite( &item->gpuZoneBegin.srcloc, (uint64_t)srcloc );
|
2018-06-17 16:55:12 +00:00
|
|
|
memset( &item->gpuZoneBegin.thread, 0, sizeof( item->gpuZoneBegin.thread ) );
|
2018-06-22 13:57:54 +00:00
|
|
|
MemWrite( &item->gpuZoneBegin.queryId, uint16_t( queryId ) );
|
2019-02-19 18:33:37 +00:00
|
|
|
MemWrite( &item->gpuZoneBegin.context, GetGpuCtx().ptr->GetId() );
|
2020-01-19 14:06:11 +00:00
|
|
|
TracyLfqCommit;
|
2017-11-13 23:48:26 +00:00
|
|
|
}
|
|
|
|
|
2020-01-25 16:44:46 +00:00
|
|
|
tracy_force_inline GpuCtxScope( const SourceLocationData* srcloc, int depth, bool is_active )
|
2018-07-11 15:10:53 +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:10:53 +00:00
|
|
|
#endif
|
2018-06-22 00:12:42 +00:00
|
|
|
{
|
2018-07-11 15:10:53 +00:00
|
|
|
if( !m_active ) return;
|
2020-01-25 16:44:46 +00:00
|
|
|
|
2019-02-19 18:33:37 +00:00
|
|
|
const auto queryId = GetGpuCtx().ptr->NextQueryId();
|
|
|
|
glQueryCounter( GetGpuCtx().ptr->TranslateOpenGlQueryId( queryId ), GL_TIMESTAMP );
|
2018-06-22 00:12:42 +00:00
|
|
|
|
2019-06-24 17:38:44 +00:00
|
|
|
const auto thread = GetThreadHandle();
|
2020-01-19 14:06:11 +00:00
|
|
|
TracyLfqPrepare( QueueType::GpuZoneBeginCallstack );
|
2018-06-22 00:12:42 +00:00
|
|
|
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 ) );
|
2019-02-19 18:33:37 +00:00
|
|
|
MemWrite( &item->gpuZoneBegin.context, GetGpuCtx().ptr->GetId() );
|
2020-01-19 14:06:11 +00:00
|
|
|
TracyLfqCommit;
|
2018-06-22 00:12:42 +00:00
|
|
|
|
2019-07-29 22:42:31 +00:00
|
|
|
GetProfiler().SendCallstack( depth );
|
2018-06-22 00:12:42 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 23:48:26 +00:00
|
|
|
tracy_force_inline ~GpuCtxScope()
|
|
|
|
{
|
2018-07-11 15:10:53 +00:00
|
|
|
if( !m_active ) return;
|
2020-01-25 16:44:46 +00:00
|
|
|
|
2019-02-19 18:33:37 +00:00
|
|
|
const auto queryId = GetGpuCtx().ptr->NextQueryId();
|
|
|
|
glQueryCounter( GetGpuCtx().ptr->TranslateOpenGlQueryId( queryId ), GL_TIMESTAMP );
|
2017-11-13 23:48:26 +00:00
|
|
|
|
2020-01-19 14:06:11 +00:00
|
|
|
TracyLfqPrepare( QueueType::GpuZoneEnd );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->gpuZoneEnd.cpuTime, Profiler::GetTime() );
|
2019-09-23 14:05:49 +00:00
|
|
|
memset( &item->gpuZoneEnd.thread, 0, sizeof( item->gpuZoneEnd.thread ) );
|
2018-06-22 13:57:54 +00:00
|
|
|
MemWrite( &item->gpuZoneEnd.queryId, uint16_t( queryId ) );
|
2019-02-19 18:33:37 +00:00
|
|
|
MemWrite( &item->gpuZoneEnd.context, GetGpuCtx().ptr->GetId() );
|
2020-01-19 14:06:11 +00:00
|
|
|
TracyLfqCommit;
|
2017-11-13 23:48:26 +00:00
|
|
|
}
|
2018-07-11 15:10:53 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
const bool m_active;
|
2017-11-13 23:48:26 +00:00
|
|
|
};
|
|
|
|
|
2017-11-11 18:44:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|