From d52c89caa28a2afe36c5fa5153418d1d6c8252c4 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Wed, 10 Dec 2008 01:54:08 -0500 Subject: [PATCH] Added assemble.c test program. --- CMakeLists.txt | 2 ++ assemble.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 assemble.c diff --git a/CMakeLists.txt b/CMakeLists.txt index be4bb3ba..d987f9a3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,6 +64,8 @@ ADD_EXECUTABLE(testoutput testoutput.c) TARGET_LINK_LIBRARIES(testoutput mojoshader) ADD_EXECUTABLE(finderrors finderrors.c) TARGET_LINK_LIBRARIES(finderrors mojoshader ${SDL_LIBRARY}) +ADD_EXECUTABLE(assemble assemble.c) +TARGET_LINK_LIBRARIES(assemble mojoshader) # End of CMakeLists.txt ... diff --git a/assemble.c b/assemble.c new file mode 100644 index 00000000..4b774c4c --- /dev/null +++ b/assemble.c @@ -0,0 +1,81 @@ +/** + * MojoShader; generate shader programs from bytecode of compiled + * Direct3D shaders. + * + * Please see the file LICENSE.txt in the source's root directory. + * + * This file written by Ryan C. Gordon. + */ + +#include +#include +#include "mojoshader.h" + +static int assemble(const char *buf, const char *outfile) +{ + FILE *io = fopen(outfile, "wb"); + if (io == NULL) + { + printf(" ... fopen('%s') failed.\n", outfile); + return 0; + } // if + + const MOJOSHADER_parseData *pd; + int retval = 0; + + pd = MOJOSHADER_assemble(buf, NULL, NULL, NULL); + if (pd->error != NULL) + printf("ERROR: %s\n", pd->error); + else + { + if (pd->output != NULL) + { + if (fwrite(pd->output, pd->output_len, 1, io) != 1) + printf(" ... fwrite('%s') failed.\n", outfile); + else if (fclose(io) == EOF) + printf(" ... fclose('%s') failed.\n", outfile); + else + retval = 1; + } // if + } // else + MOJOSHADER_freeParseData(pd); + + return retval; +} // assemble + + +int main(int argc, char **argv) +{ + int retval = 1; + + if (argc != 3) + printf("\n\nUSAGE: %s \n\n", argv[0]); + else + { + const char *infile = argv[1]; + const char *outfile = argv[2]; + FILE *io = fopen(infile, "rb"); + if (io == NULL) + printf(" ... fopen('%s') failed.\n", infile); + else + { + char *buf = (char *) malloc(1000000); + int rc = fread(buf, 1, 1000000-1, io); + fclose(io); + if (rc == EOF) + printf(" ... fread('%s') failed.\n", infile); + else + { + buf[rc] = '\0'; + if (assemble(buf, outfile)) + retval = 0; + free(buf); + } // else + } // for + } // else + + return retval; +} // main + +// end of assemble.c ... +