Skip to content

Commit 9402d29

Browse files
committed
allow OpenGL on MacOS when possible
1 parent 9ab39d8 commit 9402d29

1 file changed

Lines changed: 41 additions & 11 deletions

File tree

public/tracy/TracyOpenGL.hpp

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef __TRACYOPENGL_HPP__
22
#define __TRACYOPENGL_HPP__
33

4-
#if !defined TRACY_ENABLE || defined __APPLE__
4+
#if !defined TRACY_ENABLE
55

66
#define TracyGpuContext
77
#define TracyGpuContextName(x,y)
@@ -31,6 +31,10 @@ class GpuCtxScope
3131

3232
#else
3333

34+
#if (defined __APPLE__ && !TARGET_OS_OSX)
35+
#error Apple devices do not support OpenGL (except on MacOS where it's deprecated).
36+
#endif
37+
3438
#include <atomic>
3539
#include <assert.h>
3640
#include <stdlib.h>
@@ -94,21 +98,30 @@ class GpuCtx
9498

9599
public:
96100
GpuCtx()
97-
: m_context( GetGpuCtxCounter().fetch_add( 1, std::memory_order_relaxed ) )
101+
: m_context( 255 )
98102
, m_head( 0 )
99103
, m_tail( 0 )
100104
{
101-
assert( m_context != 255 );
105+
ZoneScopedC( Color::Red4 );
106+
107+
GLint bits = 0;
108+
glGetQueryiv( GL_TIMESTAMP, GL_QUERY_COUNTER_BITS, &bits );
109+
assert( bits > 0 );
110+
if( bits == 0 )
111+
{
112+
// all timestamp queries would resolve to 0 (and produce 0ns GPU zones).
113+
// (this is the case for many TBDR GPUs, including Apple Silicon)
114+
Profiler::LogString( MessageSourceType::Tracy, MessageSeverity::Error, Color::Red4, 0,
115+
"OpenGL driver does not implement GL_TIMESTAMP precision." );
116+
return;
117+
}
102118

103119
glGenQueries( QueryCount, m_query );
104120

105121
int64_t tgpu;
106122
glGetInteger64v( GL_TIMESTAMP, &tgpu );
107123
int64_t tcpu = Profiler::GetTime();
108124

109-
GLint bits;
110-
glGetQueryiv( GL_TIMESTAMP, GL_QUERY_COUNTER_BITS, &bits );
111-
112125
#ifdef TRACY_OPENGL_AUTO_CALIBRATION
113126
// The anchor above is never refreshed; advertise calibration and emit periodic
114127
// GpuCalibration events to correct CPU/GPU drift (see Recalibrate). Opt-in,
@@ -117,6 +130,9 @@ class GpuCtx
117130
m_prevCalibration = GetHostTimeNs();
118131
#endif
119132

133+
assert( m_context != 255 );
134+
m_context = GetGpuCtxCounter().fetch_add( 1, std::memory_order_relaxed );
135+
120136
const float period = 1.f;
121137
const auto thread = GetThreadHandle();
122138
TracyLfqPrepare( QueueType::GpuNewContext );
@@ -141,6 +157,8 @@ class GpuCtx
141157

142158
void Name( const char* name, uint16_t len )
143159
{
160+
if ( !Valid() ) return;
161+
144162
auto ptr = (char*)tracy_malloc( len );
145163
memcpy( ptr, name, len );
146164

@@ -156,6 +174,8 @@ class GpuCtx
156174

157175
void Collect()
158176
{
177+
if ( !Valid() ) return;
178+
159179
ZoneScopedC( Color::Red4 );
160180

161181
#ifdef TRACY_ON_DEMAND
@@ -244,6 +264,11 @@ class GpuCtx
244264
return m_context;
245265
}
246266

267+
tracy_force_inline bool Valid() const
268+
{
269+
return m_context != 255;
270+
}
271+
247272
unsigned int m_query[QueryCount];
248273
uint8_t m_context;
249274

@@ -265,7 +290,7 @@ class GpuCtxScope
265290
: m_active( is_active )
266291
#endif
267292
{
268-
if( !m_active ) return;
293+
if( !m_active || !Valid() ) return;
269294

270295
const auto queryId = GetGpuCtx().ptr->NextQueryId();
271296
glQueryCounter( GetGpuCtx().ptr->TranslateOpenGlQueryId( queryId ), GL_TIMESTAMP );
@@ -286,7 +311,7 @@ class GpuCtxScope
286311
: m_active( is_active )
287312
#endif
288313
{
289-
if( !m_active ) return;
314+
if( !m_active || !Valid() ) return;
290315

291316
const auto queryId = GetGpuCtx().ptr->NextQueryId();
292317
glQueryCounter( GetGpuCtx().ptr->TranslateOpenGlQueryId( queryId ), GL_TIMESTAMP );
@@ -313,7 +338,7 @@ class GpuCtxScope
313338
: m_active( is_active )
314339
#endif
315340
{
316-
if( !m_active ) return;
341+
if( !m_active || !Valid() ) return;
317342

318343
const auto queryId = GetGpuCtx().ptr->NextQueryId();
319344
glQueryCounter( GetGpuCtx().ptr->TranslateOpenGlQueryId( queryId ), GL_TIMESTAMP );
@@ -335,7 +360,7 @@ class GpuCtxScope
335360
: m_active( is_active )
336361
#endif
337362
{
338-
if( !m_active ) return;
363+
if( !m_active || !Valid() ) return;
339364

340365
const auto queryId = GetGpuCtx().ptr->NextQueryId();
341366
glQueryCounter( GetGpuCtx().ptr->TranslateOpenGlQueryId( queryId ), GL_TIMESTAMP );
@@ -358,7 +383,7 @@ class GpuCtxScope
358383

359384
tracy_force_inline ~GpuCtxScope()
360385
{
361-
if( !m_active ) return;
386+
if( !m_active || !Valid() ) return;
362387

363388
const auto queryId = GetGpuCtx().ptr->NextQueryId();
364389
glQueryCounter( GetGpuCtx().ptr->TranslateOpenGlQueryId( queryId ), GL_TIMESTAMP );
@@ -371,6 +396,11 @@ class GpuCtxScope
371396
TracyLfqCommit;
372397
}
373398

399+
tracy_force_inline bool Valid()
400+
{
401+
return GetGpuCtx().ptr->Valid();
402+
}
403+
374404
private:
375405
const bool m_active;
376406
};

0 commit comments

Comments
 (0)