Skip to content

Commit

Permalink
utils: Added testglcompile.c, to see if the GL accepts our generated …
Browse files Browse the repository at this point in the history
…shaders.
  • Loading branch information
icculus committed Oct 13, 2020
1 parent ffe4285 commit 5618e7c
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Expand Up @@ -223,6 +223,8 @@ IF(SDL2)
TARGET_LINK_LIBRARIES(bestprofile mojoshader ${SDL2} ${LIBM} ${LOBJC} ${CARBON_FRAMEWORK})
ADD_EXECUTABLE(availableprofiles utils/availableprofiles.c)
TARGET_LINK_LIBRARIES(availableprofiles mojoshader ${SDL2} ${LIBM} ${LOBJC} ${CARBON_FRAMEWORK})
ADD_EXECUTABLE(testglcompile utils/testglcompile.c)
TARGET_LINK_LIBRARIES(testglcompile mojoshader ${SDL2} ${LIBM} ${LOBJC} ${CARBON_FRAMEWORK})
ENDIF(SDL2)

IF(COMPILER_SUPPORT)
Expand Down
110 changes: 110 additions & 0 deletions utils/testglcompile.c
@@ -0,0 +1,110 @@
// A simple program to see if the GL implementation will accept a shader
// generated by MojoShader, as they could also fail to compile what
// we produce.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "SDL.h"
#include "mojoshader.h"

static void * MOJOSHADERCALL get_proc_addr(const char *fnname, void *data)
{
return SDL_GL_GetProcAddress(fnname);
}

int main(int argc, char **argv)
{
int retval = 1;
SDL_Window *sdlwindow = NULL;
MOJOSHADER_glContext *mojo = NULL;
int i;

printf("MojoShader testglcompile\n");
printf("Compiled against changeset %s\n", MOJOSHADER_CHANGESET);
printf("Linked against changeset %s\n", MOJOSHADER_changeset());
printf("\n");

if (argc <= 2)
printf("\n\nUSAGE: %s <profile> [file1] ... [fileN]\n\n", argv[0]);
else if (SDL_Init(SDL_INIT_VIDEO) == -1)
fprintf(stderr, "SDL_Init() error: %s\n", SDL_GetError());
else if (SDL_GL_LoadLibrary(NULL) == -1)
fprintf(stderr, "SDL_GL_LoadLibrary() error: %s\n", SDL_GetError());
else if ((sdlwindow = SDL_CreateWindow(argv[0], SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_OPENGL)) == NULL)
fprintf(stderr, "SDL_CreateWindow() error: %s\n", SDL_GetError());
else if (SDL_GL_CreateContext(sdlwindow) == NULL)
fprintf(stderr, "SDL_GL_CreateContext() error: %s\n", SDL_GetError());
else if ((mojo = MOJOSHADER_glCreateContext(argv[1], get_proc_addr, NULL, NULL, NULL, NULL)) == NULL)
fprintf(stderr, "MOJOSHADER_glCreateContext() error: %s\n", MOJOSHADER_glGetError());
else
retval = 0;

if (retval != 0)
{
SDL_Quit();
return retval;
} // if

MOJOSHADER_glMakeContextCurrent(mojo);

for (i = 2; i < argc; i++) {
const char *fname = argv[i];
static unsigned char buffer[1024 * 512];
MOJOSHADER_glShader *shader;
MOJOSHADER_glProgram *program;
const MOJOSHADER_parseData *pd;
int isvshader;
FILE *io;
size_t br;

io = fopen(fname, "rb");
if (!io) {
fprintf(stderr, "Failed to open %s: %s\n", fname, strerror(errno));
retval = 1;
continue;
}

br = fread(buffer, 1, sizeof (buffer), io);
fclose(io);

shader = MOJOSHADER_glCompileShader(buffer, (unsigned int) br, NULL, 0, NULL, 0);
if (!shader) {
fprintf(stderr, "Compiling %s failed: %s\n", fname, MOJOSHADER_glGetError());
retval = 1;
continue;
}

shader = MOJOSHADER_glCompileShader(buffer, (unsigned int) br, NULL, 0, NULL, 0);
if (!shader) {
fprintf(stderr, "Compiling %s failed: %s\n", fname, MOJOSHADER_glGetError());
retval = 1;
continue;
}

pd = MOJOSHADER_glGetShaderParseData(shader);
isvshader = (pd->shader_type == MOJOSHADER_TYPE_VERTEX);
program = MOJOSHADER_glLinkProgram(isvshader ? shader : NULL, isvshader ? NULL : shader);
if (!program) {
fprintf(stderr, "Linking %s failed: %s\n", fname, MOJOSHADER_glGetError());
retval = 1;
MOJOSHADER_glDeleteShader(shader);
continue;
}

printf("%s: okay\n", fname);

MOJOSHADER_glDeleteProgram(program);
MOJOSHADER_glDeleteShader(shader);
}

MOJOSHADER_glDestroyContext(mojo);
SDL_Quit();

return retval;
}

// end of testglcompile.c ...

0 comments on commit 5618e7c

Please sign in to comment.