diff --git a/src/Graphics/OpenGLContext/opengl_WrappedFunctions.h b/src/Graphics/OpenGLContext/opengl_WrappedFunctions.h index 8cf784d42..168201d7d 100644 --- a/src/Graphics/OpenGLContext/opengl_WrappedFunctions.h +++ b/src/Graphics/OpenGLContext/opengl_WrappedFunctions.h @@ -31,15 +31,15 @@ namespace opengl { private: std::atomic m_synced; bool m_executed; - bool m_isGlCommand; - bool m_logIfSynced; + const bool m_isGlCommand; + const bool m_logIfSynced; std::mutex m_condvarMutex; std::condition_variable m_condition; protected: const std::string m_functionName; public: void performCommand(void) { - + std::unique_lock lock(m_condvarMutex); commandToExecute(); #ifdef GL_DEBUG if(m_isGlCommand) @@ -68,8 +68,9 @@ namespace opengl { } void waitOnCommand(void) { - if (m_synced) { - std::unique_lock lock(m_condvarMutex); + std::unique_lock lock(m_condvarMutex); + + if (m_synced && !m_executed) { m_condition.wait(lock, [this]{return m_executed;}); } } @@ -583,7 +584,7 @@ namespace opengl { { public: GlGenTexturesCommand(GLsizei n, GLuint *textures): - OpenGlCommand(true, false, "glGenTextures"), m_n(n), m_textures(textures) + OpenGlCommand(true, true, "glGenTextures"), m_n(n), m_textures(textures) { } diff --git a/src/Graphics/OpenGLContext/opengl_Wrapper.cpp b/src/Graphics/OpenGLContext/opengl_Wrapper.cpp index dd070bb24..a6316814a 100644 --- a/src/Graphics/OpenGLContext/opengl_Wrapper.cpp +++ b/src/Graphics/OpenGLContext/opengl_Wrapper.cpp @@ -4,6 +4,7 @@ namespace opengl { bool FunctionWrapper::m_threaded_wrapper = false; bool FunctionWrapper::m_shutdown = false; + bool FunctionWrapper::m_glInitialized = false; std::thread FunctionWrapper::m_commandExecutionThread; BlockingQueue> FunctionWrapper::m_commandQueue; @@ -17,11 +18,22 @@ namespace opengl { } } + void FunctionWrapper::executePriorityCommand(std::shared_ptr _command) + { + if (m_threaded_wrapper) { + m_commandQueue.pushBack(_command); + _command->waitOnCommand(); + } else { + _command->performCommand(); + } + } + void FunctionWrapper::commandLoop(void) { while(!m_shutdown || m_commandQueue.size() != 0) { std::shared_ptr command; + if(m_commandQueue.tryPop(command, std::chrono::milliseconds(10))) { command->performCommand(); } @@ -151,7 +163,7 @@ namespace opengl { void FunctionWrapper::glGetFloatv(GLenum pname, GLfloat *data) { - executeCommand(std::make_shared(pname, data)); + executePriorityCommand(std::make_shared(pname, data)); } void FunctionWrapper::glDeleteTextures(GLsizei n, std::unique_ptr textures) @@ -161,7 +173,8 @@ namespace opengl { void FunctionWrapper::glGenTextures(GLsizei n, GLuint *textures) { - executeCommand(std::make_shared(n, textures)); + //TODO: This should be possible to do with executePriorityCommand, but it causes bugs with it somehow + executePriorityCommand(std::make_shared(n, textures)); } void FunctionWrapper::glTexParameterf(GLenum target, GLenum pname, GLfloat param) @@ -373,7 +386,7 @@ namespace opengl { void FunctionWrapper::glGenFramebuffers(GLsizei n, GLuint *framebuffers) { - executeCommand(std::make_shared(n, framebuffers)); + executePriorityCommand(std::make_shared(n, framebuffers)); } void FunctionWrapper::glBindFramebuffer(GLenum target, GLuint framebuffer) @@ -403,7 +416,7 @@ namespace opengl { void FunctionWrapper::glGenRenderbuffers(GLsizei n, GLuint *renderbuffers) { - executeCommand(std::make_shared(n, renderbuffers)); + executePriorityCommand(std::make_shared(n, renderbuffers)); } void FunctionWrapper::glBindRenderbuffer(GLenum target, GLuint renderbuffer) @@ -444,7 +457,7 @@ namespace opengl { void FunctionWrapper::glGenVertexArrays(GLsizei n, GLuint *arrays) { - executeCommand(std::make_shared(n, arrays)); + executePriorityCommand(std::make_shared(n, arrays)); } void FunctionWrapper::glBindVertexArray(GLuint array) @@ -459,7 +472,7 @@ namespace opengl { void FunctionWrapper::glGenBuffers(GLsizei n, GLuint *buffers) { - executeCommand(std::make_shared(n, buffers)); + executePriorityCommand(std::make_shared(n, buffers)); } void FunctionWrapper::glBindBuffer(GLenum target, GLuint buffer) @@ -504,7 +517,7 @@ namespace opengl { const GLubyte* FunctionWrapper::glGetStringi(GLenum name, GLuint index) { const GLubyte* returnValue; - executeCommand(std::make_shared(name, index, returnValue)); + executePriorityCommand(std::make_shared(name, index, returnValue)); return returnValue; } @@ -604,17 +617,17 @@ namespace opengl { void FunctionWrapper::glCreateTextures(GLenum target, GLsizei n, GLuint *textures) { - executeCommand(std::make_shared(target, n, textures)); + executePriorityCommand(std::make_shared(target, n, textures)); } void FunctionWrapper::glCreateBuffers(GLsizei n, GLuint *buffers) { - executeCommand(std::make_shared(n, buffers)); + executePriorityCommand(std::make_shared(n, buffers)); } void FunctionWrapper::glCreateFramebuffers(GLsizei n, GLuint *framebuffers) { - executeCommand(std::make_shared(n, framebuffers)); + executePriorityCommand(std::make_shared(n, framebuffers)); } void FunctionWrapper::glNamedFramebufferTexture(GLuint framebuffer, GLenum attachment, GLuint texture, GLint level) @@ -657,6 +670,7 @@ namespace opengl { { m64p_error returnValue; executeCommand(std::make_shared(screenWidth, screenHeight, bitsPerPixel, mode, flags, returnValue)); + m_glInitialized = true; return returnValue; } diff --git a/src/Graphics/OpenGLContext/opengl_Wrapper.h b/src/Graphics/OpenGLContext/opengl_Wrapper.h index 29e217db7..0fc9064bb 100644 --- a/src/Graphics/OpenGLContext/opengl_Wrapper.h +++ b/src/Graphics/OpenGLContext/opengl_Wrapper.h @@ -13,12 +13,15 @@ namespace opengl { private: static void executeCommand(std::shared_ptr _command); + static void executePriorityCommand(std::shared_ptr _command); + static void commandLoop(void); static BlockingQueue> m_commandQueue; static bool m_threaded_wrapper; static bool m_shutdown; + static bool m_glInitialized; static std::thread m_commandExecutionThread; public: static void setThreadedMode(void);