Skip to content

Commit

Permalink
OpenGL glue now allows for multiple contexts.
Browse files Browse the repository at this point in the history
--HG--
branch : trunk
  • Loading branch information
icculus committed Apr 28, 2008
1 parent 073c746 commit 85967d9
Show file tree
Hide file tree
Showing 3 changed files with 269 additions and 177 deletions.
7 changes: 5 additions & 2 deletions finderrors.c
Expand Up @@ -113,12 +113,15 @@ int main(int argc, char **argv)
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_LoadLibrary(NULL);
SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
if (!MOJOSHADER_glInit(profile, SDL_GL_GetProcAddress, 0, 0, 0))
MOJOSHADER_glContext *ctx;
ctx = MOJOSHADER_glInit(profile, SDL_GL_GetProcAddress, 0, 0, 0);
if (ctx == NULL)
{
printf("MOJOSHADER_glInit() fail: %s\n", MOJOSHADER_glGetError());
SDL_Quit();
return 1;
} // if
MOJOSHADER_glMakeContextCurrent(ctx);
#endif

for (i = 2; i < argc; i++)
Expand All @@ -127,7 +130,7 @@ int main(int argc, char **argv)
printf("Saw %d bytecode files.\n", total);

#if FINDERRORS_COMPILE_SHADERS
MOJOSHADER_glDeinit();
MOJOSHADER_glDeinit(ctx);
SDL_Quit();
#endif
} // else
Expand Down
101 changes: 86 additions & 15 deletions mojoshader.h
Expand Up @@ -337,6 +337,20 @@ void MOJOSHADER_freeParseData(const MOJOSHADER_parseData *data);

/* OpenGL interface... */

/*
* "Contexts" map to OpenGL contexts...you need one per window, or whatever,
* and need to inform MojoShader when you make a new one current.
*
* "Shaders" refer to individual vertex or pixel programs, and are created
* by "compiling" Direct3D shader bytecode. A vertex and pixel shader are
* "linked" into a "Program" before you can use them to render.
*
* To the calling application, these are all opaque handles.
*/
typedef struct MOJOSHADER_glContext MOJOSHADER_glContext;
typedef struct MOJOSHADER_glShader MOJOSHADER_glShader;
typedef struct MOJOSHADER_glProgram MOJOSHADER_glProgram;

/*
* Prepare MojoShader to manage OpenGL shaders.
*
Expand All @@ -362,16 +376,31 @@ void MOJOSHADER_freeParseData(const MOJOSHADER_parseData *data);
* If your allocator needs instance-specific data, you may supply it with the
* (d) parameter. This pointer is passed as-is to your (m) and (f) functions.
*
* Returns zero on error, non-zero on success.
* Returns a new context on success, NULL on error. If you get a new context,
* you need to make it current before using it with
* MOJOSHADER_glMakeContextCurrent().
*
* This call is NOT thread safe! It must return success before you may call
* any other MOJOSHADER_gl* function. Also, as most OpenGL implementations
* are not thread safe, you should probably only call this from the same
* thread that created the GL context.
*/
int MOJOSHADER_glInit(const char *profile,
void *(*lookup)(const char *fnname),
MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);
MOJOSHADER_glContext *MOJOSHADER_glInit(const char *profile,
void *(*lookup)(const char *fnname),
MOJOSHADER_malloc m, MOJOSHADER_free f,
void *d);

/*
* You must call this before using the context that you got from
* MOJOSHADER_glInit(), and must use it when you switch to a new GL context.
*
* You can only have one MOJOSHADER_glContext per actual GL context, or
* undefined behaviour will result.
*
* It is legal to call this with a NULL pointer to make no context current,
* but you need a valid context to be current to use most of MojoShader.
*/
void MOJOSHADER_glMakeContextCurrent(MOJOSHADER_glContext *ctx);

/*
* Get any error state we might have picked up. MojoShader will NOT call
Expand All @@ -395,18 +424,12 @@ int MOJOSHADER_glInit(const char *profile,
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
*/
const char *MOJOSHADER_glGetError(void);

/*
* "Shaders" refer to individual vertex or pixel programs, and are created
* by "compiling" Direct3D shader bytecode. A vertex and pixel shader are
* "linked" into a "Program" before you can use them to render.
*
* To the calling application, these are opaque handles.
* This call does NOT require a valid MOJOSHADER_glContext to have been made
* current. The error buffer is shared between contexts, so you can get
* error results from a failed MOJOSHADER_glInit().
*/
typedef struct MOJOSHADER_glShader MOJOSHADER_glShader;
typedef struct MOJOSHADER_glProgram MOJOSHADER_glProgram;
const char *MOJOSHADER_glGetError(void);

/*
* Compile a buffer of Direct3D shader bytecode into an OpenGL shader.
Expand All @@ -420,6 +443,11 @@ typedef struct MOJOSHADER_glProgram MOJOSHADER_glProgram;
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
*
* Compiled shaders from this function may not be shared between contexts.
*/
MOJOSHADER_glShader *MOJOSHADER_glCompileShader(const unsigned char *tokenbuf,
const unsigned int bufsize);
Expand All @@ -441,6 +469,11 @@ MOJOSHADER_glShader *MOJOSHADER_glCompileShader(const unsigned char *tokenbuf,
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
*
* Linked programs from this function may not be shared between contexts.
*/
MOJOSHADER_glProgram *MOJOSHADER_glLinkProgram(MOJOSHADER_glShader *vshader,
MOJOSHADER_glShader *pshader);
Expand All @@ -462,6 +495,9 @@ MOJOSHADER_glProgram *MOJOSHADER_glLinkProgram(MOJOSHADER_glShader *vshader,
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
*/
void MOJOSHADER_glBindProgram(MOJOSHADER_glProgram *program);

Expand All @@ -481,6 +517,9 @@ void MOJOSHADER_glBindProgram(MOJOSHADER_glProgram *program);
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
*/
void MOJOSHADER_glSetVertexShaderUniformF(unsigned int idx, const float *data,
unsigned int vec4count);
Expand All @@ -500,6 +539,9 @@ void MOJOSHADER_glSetVertexShaderUniformF(unsigned int idx, const float *data,
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
*/
void MOJOSHADER_glSetVertexShaderUniformI(unsigned int idx, const int *data,
unsigned int ivec4count);
Expand All @@ -524,6 +566,9 @@ void MOJOSHADER_glSetVertexShaderUniformI(unsigned int idx, const int *data,
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
*/
void MOJOSHADER_glSetVertexShaderUniformB(unsigned int idx, const int *data,
unsigned int bcount);
Expand All @@ -536,6 +581,9 @@ void MOJOSHADER_glSetVertexShaderUniformB(unsigned int idx, const int *data,
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
*/
void MOJOSHADER_glSetPixelShaderUniformF(unsigned int idx, const float *data,
unsigned int vec4count);
Expand All @@ -548,6 +596,9 @@ void MOJOSHADER_glSetPixelShaderUniformF(unsigned int idx, const float *data,
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
*/
void MOJOSHADER_glSetPixelShaderUniformI(unsigned int idx, const int *data,
unsigned int ivec4count);
Expand All @@ -560,6 +611,9 @@ void MOJOSHADER_glSetPixelShaderUniformI(unsigned int idx, const int *data,
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
*/
void MOJOSHADER_glSetPixelShaderUniformB(unsigned int idx, const int *data,
unsigned int bcount);
Expand All @@ -583,6 +637,11 @@ void MOJOSHADER_glSetPixelShaderUniformB(unsigned int idx, const int *data,
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
*
* Vertex attributes are not shared between contexts.
*/
void MOJOSHADER_glSetVertexAttribute(MOJOSHADER_usage usage,
int index, unsigned int size,
Expand All @@ -599,6 +658,9 @@ void MOJOSHADER_glSetVertexAttribute(MOJOSHADER_usage usage,
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
*/
void MOJOSHADER_glProgramReady(void);

Expand All @@ -612,6 +674,9 @@ void MOJOSHADER_glProgramReady(void);
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
*/
void MOJOSHADER_glDeleteProgram(MOJOSHADER_glProgram *program);

Expand All @@ -625,6 +690,9 @@ void MOJOSHADER_glDeleteProgram(MOJOSHADER_glProgram *program);
* This call is NOT thread safe! As most OpenGL implementations are not thread
* safe, you should probably only call this from the same thread that created
* the GL context.
*
* This call requires a valid MOJOSHADER_glContext to have been made current,
* or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
*/
void MOJOSHADER_glDeleteShader(MOJOSHADER_glShader *shader);

Expand All @@ -642,12 +710,15 @@ void MOJOSHADER_glDeleteShader(MOJOSHADER_glShader *shader);
* MOJOSHADER_glDeleteShader() and MOJOSHADER_glDeleteProgram() to clean
* those up before calling this function!
*
* This function destroys the MOJOSHADER_glContext you pass it. If it's the
* current context, then no context will be current upon return.
*
* This call is NOT thread safe! There must not be any other MOJOSHADER_gl*
* functions running when this is called. Also, as most OpenGL implementations
* are not thread safe, you should probably only call this from the same
* thread that created the GL context.
*/
void MOJOSHADER_glDeinit(void);
void MOJOSHADER_glDeinit(MOJOSHADER_glContext *ctx);

#ifdef __cplusplus
}
Expand Down

0 comments on commit 85967d9

Please sign in to comment.