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 5, 2017
1 parent 1fe5303 commit 1d51917
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
13 changes: 7 additions & 6 deletions src/Graphics/OpenGLContext/opengl_WrappedFunctions.h
Original file line number Diff line number Diff line change
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
48 changes: 38 additions & 10 deletions src/Graphics/OpenGLContext/opengl_Wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ namespace opengl {

bool FunctionWrapper::m_threaded_wrapper = false;
bool FunctionWrapper::m_shutdown = false;
bool FunctionWrapper::m_glInitialized = false;
std::thread FunctionWrapper::m_commandExecutionThread;
BlockingQueue<std::shared_ptr<OpenGlCommand>> FunctionWrapper::m_commandQueue;
BlockingQueue<std::shared_ptr<OpenGlCommand>> FunctionWrapper::m_priorityCommandQueue;

void FunctionWrapper::executeCommand(std::shared_ptr<OpenGlCommand> _command)
{
Expand All @@ -17,12 +19,36 @@ namespace opengl {
}
}

void FunctionWrapper::executePriorityCommand(std::shared_ptr<OpenGlCommand> _command)
{
if (m_threaded_wrapper) {
m_priorityCommandQueue.push(_command);
_command->waitOnCommand();
} else {
_command->performCommand();
}
}

void FunctionWrapper::commandLoop(void)
{
while(!m_shutdown || m_commandQueue.size() != 0)
while(!m_shutdown || m_commandQueue.size() != 0 || m_priorityCommandQueue.size() != 0)
{
std::shared_ptr<OpenGlCommand> command;
std::shared_ptr<OpenGlCommand> priorityCommand;
//Take care of all the priority commands before other commands
while(m_glInitialized && m_priorityCommandQueue.size() > 0) {
priorityCommand = m_priorityCommandQueue.pop();
priorityCommand->performCommand();
}

if(m_commandQueue.tryPop(command, std::chrono::milliseconds(10))) {

//Something could had been inserted while we were waiting for a low priority command
while(m_glInitialized && m_priorityCommandQueue.size() > 0) {
priorityCommand = m_priorityCommandQueue.pop();
priorityCommand->performCommand();
}

command->performCommand();
}
}
Expand Down Expand Up @@ -151,7 +177,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,6 +187,7 @@ namespace opengl {

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

Expand Down Expand Up @@ -373,7 +400,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 +430,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 +471,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 +486,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 +531,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 +631,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 Expand Up @@ -657,6 +684,7 @@ namespace opengl {
{
m64p_error returnValue;
executeCommand(std::make_shared<CoreVideoSetVideoModeCommand>(screenWidth, screenHeight, bitsPerPixel, mode, flags, returnValue));
m_glInitialized = true;
return returnValue;
}

Expand Down
4 changes: 4 additions & 0 deletions src/Graphics/OpenGLContext/opengl_Wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ 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;
static BlockingQueue<std::shared_ptr<OpenGlCommand>> m_priorityCommandQueue;

static bool m_threaded_wrapper;
static bool m_shutdown;
static bool m_glInitialized;
static std::thread m_commandExecutionThread;
public:
static void setThreadedMode(void);
Expand Down

0 comments on commit 1d51917

Please sign in to comment.