Skip to content

Commit

Permalink
Allow some commands to skip the line
Browse files Browse the repository at this point in the history
  • Loading branch information
fzurita committed Apr 6, 2017
1 parent 1fe5303 commit a0b8720
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
9 changes: 9 additions & 0 deletions src/BlockingQueue.h
Expand Up @@ -21,6 +21,15 @@ class BlockingQueue
}
this->d_condition.notify_one();
}

void pushBack(T const& value) {
{
std::unique_lock<std::mutex> lock(this->d_mutex);
d_queue.push_back(value);
}
this->d_condition.notify_one();
}

T pop() {
std::unique_lock<std::mutex> lock(this->d_mutex);
this->d_condition.wait(lock, [this]{ return !this->d_queue.empty(); });
Expand Down
13 changes: 7 additions & 6 deletions src/Graphics/OpenGLContext/opengl_WrappedFunctions.h
Expand Up @@ -31,15 +31,15 @@ namespace opengl {
private:
std::atomic<bool> 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<std::mutex> lock(m_condvarMutex);
commandToExecute();
#ifdef GL_DEBUG
if(m_isGlCommand)
Expand Down Expand Up @@ -68,8 +68,9 @@ namespace opengl {
}

void waitOnCommand(void) {
if (m_synced) {
std::unique_lock<std::mutex> lock(m_condvarMutex);
std::unique_lock<std::mutex> lock(m_condvarMutex);

if (m_synced && !m_executed) {
m_condition.wait(lock, [this]{return m_executed;});
}
}
Expand Down Expand Up @@ -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)
{
}

Expand Down
32 changes: 22 additions & 10 deletions src/Graphics/OpenGLContext/opengl_Wrapper.cpp
Expand Up @@ -17,11 +17,22 @@ namespace opengl {
}
}

void FunctionWrapper::executePriorityCommand(std::shared_ptr<OpenGlCommand> _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<OpenGlCommand> command;

if(m_commandQueue.tryPop(command, std::chrono::milliseconds(10))) {
command->performCommand();
}
Expand Down Expand Up @@ -151,7 +162,7 @@ namespace opengl {

void FunctionWrapper::glGetFloatv(GLenum pname, GLfloat *data)
{
executeCommand(std::make_shared<GlGetFloatvCommand>(pname, data));
executePriorityCommand(std::make_shared<GlGetFloatvCommand>(pname, data));
}

void FunctionWrapper::glDeleteTextures(GLsizei n, std::unique_ptr<GLuint[]> textures)
Expand All @@ -161,7 +172,8 @@ namespace opengl {

void FunctionWrapper::glGenTextures(GLsizei n, GLuint *textures)
{
executeCommand(std::make_shared<GlGenTexturesCommand>(n, textures));
//TODO: This should be possible to do with executePriorityCommand, but it causes bugs with it somehow
executePriorityCommand(std::make_shared<GlGenTexturesCommand>(n, textures));
}

void FunctionWrapper::glTexParameterf(GLenum target, GLenum pname, GLfloat param)
Expand Down Expand Up @@ -373,7 +385,7 @@ namespace opengl {

void FunctionWrapper::glGenFramebuffers(GLsizei n, GLuint *framebuffers)
{
executeCommand(std::make_shared<GlGenFramebuffersCommand>(n, framebuffers));
executePriorityCommand(std::make_shared<GlGenFramebuffersCommand>(n, framebuffers));
}

void FunctionWrapper::glBindFramebuffer(GLenum target, GLuint framebuffer)
Expand Down Expand Up @@ -403,7 +415,7 @@ namespace opengl {

void FunctionWrapper::glGenRenderbuffers(GLsizei n, GLuint *renderbuffers)
{
executeCommand(std::make_shared<GlGenRenderbuffersCommand>(n, renderbuffers));
executePriorityCommand(std::make_shared<GlGenRenderbuffersCommand>(n, renderbuffers));
}

void FunctionWrapper::glBindRenderbuffer(GLenum target, GLuint renderbuffer)
Expand Down Expand Up @@ -444,7 +456,7 @@ namespace opengl {

void FunctionWrapper::glGenVertexArrays(GLsizei n, GLuint *arrays)
{
executeCommand(std::make_shared<GlGenVertexArraysCommand>(n, arrays));
executePriorityCommand(std::make_shared<GlGenVertexArraysCommand>(n, arrays));
}

void FunctionWrapper::glBindVertexArray(GLuint array)
Expand All @@ -459,7 +471,7 @@ namespace opengl {

void FunctionWrapper::glGenBuffers(GLsizei n, GLuint *buffers)
{
executeCommand(std::make_shared<GlGenBuffersCommand>(n, buffers));
executePriorityCommand(std::make_shared<GlGenBuffersCommand>(n, buffers));
}

void FunctionWrapper::glBindBuffer(GLenum target, GLuint buffer)
Expand Down Expand Up @@ -504,7 +516,7 @@ namespace opengl {
const GLubyte* FunctionWrapper::glGetStringi(GLenum name, GLuint index)
{
const GLubyte* returnValue;
executeCommand(std::make_shared<GlGetStringiCommand>(name, index, returnValue));
executePriorityCommand(std::make_shared<GlGetStringiCommand>(name, index, returnValue));
return returnValue;
}

Expand Down Expand Up @@ -604,17 +616,17 @@ namespace opengl {

void FunctionWrapper::glCreateTextures(GLenum target, GLsizei n, GLuint *textures)
{
executeCommand(std::make_shared<GlCreateTexturesCommand>(target, n, textures));
executePriorityCommand(std::make_shared<GlCreateTexturesCommand>(target, n, textures));
}

void FunctionWrapper::glCreateBuffers(GLsizei n, GLuint *buffers)
{
executeCommand(std::make_shared<GlCreateBuffersCommand>(n, buffers));
executePriorityCommand(std::make_shared<GlCreateBuffersCommand>(n, buffers));
}

void FunctionWrapper::glCreateFramebuffers(GLsizei n, GLuint *framebuffers)
{
executeCommand(std::make_shared<GlCreateFramebuffersCommand>(n, framebuffers));
executePriorityCommand(std::make_shared<GlCreateFramebuffersCommand>(n, framebuffers));
}

void FunctionWrapper::glNamedFramebufferTexture(GLuint framebuffer, GLenum attachment, GLuint texture, GLint level)
Expand Down
2 changes: 2 additions & 0 deletions src/Graphics/OpenGLContext/opengl_Wrapper.h
Expand Up @@ -13,6 +13,8 @@ namespace opengl {
private:
static void executeCommand(std::shared_ptr<OpenGlCommand> _command);

static void executePriorityCommand(std::shared_ptr<OpenGlCommand> _command);

static void commandLoop(void);

static BlockingQueue<std::shared_ptr<OpenGlCommand>> m_commandQueue;
Expand Down

0 comments on commit a0b8720

Please sign in to comment.