GLS  1.0.0
GL Stuff - A library aimed at reducing the boilerplate OpenGL code you always have to write.
sync.hpp
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 #include <gls/headercheck.hpp>
8 #include <gls/errorcheck.hpp>
9 #include <gls/objects/object.hpp>
10 
11 namespace gls {
12 
17 class sync {
18 public:
23  sync() :
24  m_sync( 0 )
25  {
26  }
27 
32  ~sync() {
33  check_gl_error( glDeleteSync( m_sync ) );
34  }
35 
42  sync( sync&& other ) :
43  m_sync( 0 )
44  {
45  std::swap( m_sync, other.m_sync );
46  }
47 
55  sync& operator=( sync&& other ) {
56  std::swap( m_sync, other.m_sync );
57  return *this;
58  }
59 
64  sync( const sync& ) = delete;
65  operator=( const sync& ) = delete;
70 
77  GLsync name() const {
78  return m_sync;
79  }
80 
89  void insert() {
90  check_gl_error( glDeleteSync( m_sync ) );
91  m_sync = check_gl_error( glFenceSync( GL_SYNC_GPU_COMMANDS_COMPLETE, 0 ) );
92  }
93 
110  bool wait( GLuint64 timeout ) {
111  auto result = check_gl_error( glClientWaitSync( m_sync, GL_SYNC_FLUSH_COMMANDS_BIT, timeout ) );
112 
113  auto signaled = ( ( result == GL_ALREADY_SIGNALED ) || ( result == GL_CONDITION_SATISFIED ) );
114 
115  return signaled;
116  }
117 
130  bool expired() {
131  return wait( 0 );
132  }
133 
144  void server_wait() {
145  check_gl_error( glFlush() );
146  check_gl_error( glWaitSync( m_sync, 0, GL_TIMEOUT_IGNORED ) );
147  }
148 
149 private:
150  GLsync m_sync;
151 };
152 
153 }
154 
bool wait(GLuint64 timeout)
Wait for the sync to expire.
Definition: sync.hpp:110
void insert()
Insert into the command queue.
Definition: sync.hpp:89
sync(sync &&other)
Move constructor.
Definition: sync.hpp:42
void server_wait()
Make server execution wait for this sync to expire.
Definition: sync.hpp:144
sync & operator=(sync &&other)
Move assignment operator.
Definition: sync.hpp:55
Class encapsulating an OpenGL sync object.
Definition: sync.hpp:17
~sync()
Destructor.
Definition: sync.hpp:32
Definition: buffer.hpp:12
GLsync name() const
Retrieve the OpenGL name of this sync object.
Definition: sync.hpp:77
bool expired()
Check if the current sync object has expired.
Definition: sync.hpp:130
sync()
Default constructor.
Definition: sync.hpp:23