Skip to content

Commit

Permalink
GL|Debug: Assert that a GL context is active when needed
Browse files Browse the repository at this point in the history
OpenGL API calls must not be made without an active context.
The context may need to be manually activated outside the drawing
functions.
  • Loading branch information
skyjake committed Apr 13, 2012
1 parent 966d4c3 commit 52140fb
Show file tree
Hide file tree
Showing 37 changed files with 182 additions and 3 deletions.
8 changes: 8 additions & 0 deletions doomsday/engine/portable/include/gl_main.h
Expand Up @@ -60,6 +60,14 @@ extern int r_detail;

boolean GL_IsInited(void);

#ifdef _DEBUG
# define LIBDENG_ASSERT_GL_CONTEXT_ACTIVE() {GL_AssertContextActive();}
#else
# define LIBDENG_ASSERT_GL_CONTEXT_ACTIVE()
#endif

void GL_AssertContextActive(void);

void GL_Register(void);
boolean GL_EarlyInit(void);
void GL_Init(void);
Expand Down
18 changes: 18 additions & 0 deletions doomsday/engine/portable/include/window.h
Expand Up @@ -312,6 +312,24 @@ boolean Window_ShouldRepaintManually(const Window* wnd);

void Window_UpdateCanvasFormat(Window* wnd);

/**
* Activates the window's GL context so that OpenGL API calls can be made.
* The GL context is automatically active during the drawing of the window's
* contents; at other times it needs to be manually activated.
*
* @param wnd Window instance.
*/
void Window_GLActivate(Window* wnd);

/**
* Dectivates the window's GL context after OpenGL API calls have been done.
* The GL context is automatically deactived after the drawing of the window's
* contents; at other times it needs to be manually deactivated.
*
* @param wnd Window instance.
*/
void Window_GLDone(Window* wnd);

void* Window_NativeHandle(const Window* wnd);

#ifdef __cplusplus
Expand Down
1 change: 1 addition & 0 deletions doomsday/engine/portable/src/blockmapvisual.c
Expand Up @@ -551,6 +551,7 @@ void Rend_BlockmapDebug(void)
}

LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

/**
* Draw the blockmap.
Expand Down
6 changes: 6 additions & 0 deletions doomsday/engine/portable/src/con_busy.c
Expand Up @@ -462,6 +462,8 @@ static void BusyTask_Loop(void)

if(canUpload)
{
Window_GLActivate(Window_Main());

// Any deferred content needs to get uploaded.
GL_ProcessDeferredTasks(15);
}
Expand Down Expand Up @@ -503,6 +505,7 @@ static void Con_DrawScreenshotBackground(float x, float y, float width, float he
if(texScreenshot)
{
LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

//GL_BindTextureUnmanaged(texScreenshot, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, texScreenshot);
Expand Down Expand Up @@ -554,6 +557,7 @@ static void Con_BusyDrawIndicator(float x, float y, float radius, float pos)
edgeCount = MAX_OF(1, pos * 30);

LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

// Draw a background.
GL_BlendMode(BM_NORMAL);
Expand Down Expand Up @@ -772,6 +776,7 @@ static void BusyTask_Drawer(void)
float pos = 0;

LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

glMatrixMode(GL_PROJECTION);
glPushMatrix();
Expand Down Expand Up @@ -866,6 +871,7 @@ void Con_DrawTransition(void)
if(!Con_TransitionInProgress()) return;

LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

glMatrixMode(GL_PROJECTION);
glPushMatrix();
Expand Down
6 changes: 4 additions & 2 deletions doomsday/engine/portable/src/dd_input.c
Expand Up @@ -1795,6 +1795,7 @@ void Rend_RenderInputDeviceStateVisual(inputdev_t* device, const inputdev_layout
uint i;

LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

if(retVisualDimensions)
{
Expand Down Expand Up @@ -2156,10 +2157,11 @@ void Rend_AllInputDeviceStateVisuals(void)
Point2Raw origin = { 2, 2 };
Size2Raw dimensions;

LIBDENG_ASSERT_IN_MAIN_THREAD();

if(novideo || isDedicated) return; // Not for us.

LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

// Disabled?
if(!devRendKeyState && !devRendMouseState && !devRendJoyState) return;

Expand Down
1 change: 1 addition & 0 deletions doomsday/engine/portable/src/dd_loop.c
Expand Up @@ -159,6 +159,7 @@ void DD_GameLoopDrawer(void)
assert(!Con_IsBusy()); // Busy mode has its own drawer.

LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

// Frame syncronous I/O operations.
startFrame();
Expand Down
1 change: 1 addition & 0 deletions doomsday/engine/portable/src/dd_zone.c
Expand Up @@ -1247,6 +1247,7 @@ void Z_DebugDrawer(void)
if(!ArgExists("-zonedebug")) return;

LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
Expand Down
21 changes: 21 additions & 0 deletions doomsday/engine/portable/src/dgl_common.c
Expand Up @@ -57,6 +57,7 @@
void envAddColoredAlpha(int activate, GLenum addFactor)
{
LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

if(activate)
{
Expand Down Expand Up @@ -110,6 +111,7 @@ void envAddColoredAlpha(int activate, GLenum addFactor)
void envModMultiTex(int activate)
{
LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

// Setup TU 2: The modulated texture.
glActiveTexture(GL_TEXTURE1);
Expand Down Expand Up @@ -137,6 +139,7 @@ void envModMultiTex(int activate)
void GL_ModulateTexture(int mode)
{
LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

switch(mode)
{
Expand Down Expand Up @@ -326,6 +329,7 @@ void GL_BlendOp(int op)
return;

LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

glBlendEquationEXT(op);
}
Expand Down Expand Up @@ -402,6 +406,7 @@ void DGL_SetScissor(const RectRaw* rect)
if(!rect) return;

LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

glScissor(rect->origin.x, FLIP(rect->origin.y + rect->size.height - 1), rect->size.width, rect->size.height);
}
Expand All @@ -423,6 +428,7 @@ void DGL_Scissor(RectRaw* rect)
if(!rect) return;

LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

glGetIntegerv(GL_SCISSOR_BOX, (GLint*)v);
// Y is flipped.
Expand All @@ -440,6 +446,7 @@ boolean DGL_GetIntegerv(int name, int* v)
int i;

LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

switch(name)
{
Expand Down Expand Up @@ -499,6 +506,7 @@ int DGL_GetInteger(int name)
boolean DGL_SetInteger(int name, int value)
{
LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

switch(name)
{
Expand All @@ -522,6 +530,7 @@ boolean DGL_GetFloatv(int name, float* v)
float color[4];

LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

switch(name)
{
Expand Down Expand Up @@ -578,6 +587,7 @@ float DGL_GetFloat(int name)
boolean DGL_SetFloat(int name, float value)
{
LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

switch(name)
{
Expand All @@ -601,6 +611,7 @@ boolean DGL_SetFloat(int name, float value)
int DGL_Enable(int cap)
{
LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

switch(cap)
{
Expand Down Expand Up @@ -637,6 +648,7 @@ int DGL_Enable(int cap)
void DGL_Disable(int cap)
{
LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

switch(cap)
{
Expand Down Expand Up @@ -676,6 +688,7 @@ void DGL_BlendOp(int op)
void DGL_BlendFunc(int param1, int param2)
{
LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

glBlendFunc(param1 == DGL_ZERO ? GL_ZERO : param1 ==
DGL_ONE ? GL_ONE : param1 ==
Expand Down Expand Up @@ -706,6 +719,7 @@ void DGL_BlendMode(blendmode_t mode)
void DGL_MatrixMode(int mode)
{
LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

glMatrixMode(mode == DGL_PROJECTION ? GL_PROJECTION :
mode == DGL_TEXTURE ? GL_TEXTURE :
Expand All @@ -715,6 +729,7 @@ void DGL_MatrixMode(int mode)
void DGL_PushMatrix(void)
{
LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

glPushMatrix();

Expand Down Expand Up @@ -772,6 +787,7 @@ void DGL_SetRawImage(lumpnum_t lumpNum, DGLint wrapS, DGLint wrapT)
void DGL_PopMatrix(void)
{
LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

glPopMatrix();

Expand All @@ -784,27 +800,31 @@ if(glGetError() == GL_STACK_UNDERFLOW)
void DGL_LoadIdentity(void)
{
LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

glLoadIdentity();
}

void DGL_Translatef(float x, float y, float z)
{
LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

glTranslatef(x, y, z);
}

void DGL_Rotatef(float angle, float x, float y, float z)
{
LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

glRotatef(angle, x, y, z);
}

void DGL_Scalef(float x, float y, float z)
{
LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

glScalef(x, y, z);
}
Expand All @@ -813,6 +833,7 @@ void DGL_Ortho(float left, float top, float right, float bottom, float znear,
float zfar)
{
LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

glOrtho(left, right, bottom, top, znear, zfar);
}
Expand Down

0 comments on commit 52140fb

Please sign in to comment.