From 7e55d66c443e24172e572080e9c5224ffdb63b29 Mon Sep 17 00:00:00 2001 From: icculus Date: Sun, 10 Feb 2008 18:34:12 -0500 Subject: [PATCH] [svn] Cleanups, split things out. --HG-- branch : trunk --- CMakeLists.txt | 2 +- d3d2glsl.h | 27 +++++++++++++++++++++++ parse.c | 60 +++++++++++++++++++++++++++----------------------- testparse.c | 23 +++++++++++++++++++ 4 files changed, 83 insertions(+), 29 deletions(-) create mode 100644 d3d2glsl.h create mode 100644 testparse.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d367bb4..c1cfd99f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ PROJECT(d3d2glsl) -ADD_EXECUTABLE(parse parse.c) +ADD_EXECUTABLE(parse testparse.c parse.c) # End of CMakeLists.txt ... diff --git a/d3d2glsl.h b/d3d2glsl.h new file mode 100644 index 00000000..614c4683 --- /dev/null +++ b/d3d2glsl.h @@ -0,0 +1,27 @@ +/** + * d3d2glsl; generate GLSL 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. + */ + +#ifndef __INCL_D3D2GLSL_H_ +#define __INCL_D3D2GLSL_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +/* !!! FIXME: documentation. */ +/* !!! FIXME: this needs to change to return a buffer of GLSL code. */ +int D3D2GLSL_parse(const uint8 *tokenbuf, const uint32 bufsize); + +#ifdef __cplusplus +} +#endif + +#endif /* include-once blocker. */ + +/* end of d3d2glsl.h ... */ + diff --git a/parse.c b/parse.c index e6620f0b..1124fa81 100644 --- a/parse.c +++ b/parse.c @@ -1,20 +1,24 @@ +/** + * d3d2glsl; generate GLSL 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. + */ + +// Shader bytecode format is described at MSDN: +// http://msdn2.microsoft.com/en-us/library/ms800307.aspx + #include #include #include -#define END_OF_STREAM (-2) -//#define FAIL(x) (-1) -static int FAIL(const char *reason) -{ - printf("%s FAIL.\n", reason); - return -1; -} // FAIL - - typedef unsigned int uint; // this is a printf() helper. don't use for code. typedef uint8_t uint8; typedef uint32_t uint32; +// Byteswap magic... + #if ((defined __GNUC__) && (defined __POWERPC__)) static inline uint32 SWAP32(uint32 x) { @@ -31,6 +35,20 @@ typedef uint32_t uint32; # define SWAP32(x) (x) #endif + +// Special-case return values from the parsing pipeline... + +#define END_OF_STREAM (-2) +//#define FAIL(x) (-1) +static int FAIL(const char *reason) +{ + printf("%s FAIL.\n", reason); + return -1; +} // FAIL + + +// one function for each opcode... + typedef int (*parse_instruction_function)(const uint32 *argtokens); static int parse_NOP(const uint32 *argtokens) @@ -450,6 +468,7 @@ static int parse_RESERVED(const uint32 *argtokens) +// Lookup table for instruction opcodes... typedef struct { @@ -564,6 +583,8 @@ static Instruction instructions[] = { }; +// parse various token types... + static int parse_instruction_token(const uint32 *tokens, const uint32 tokencount) { const uint32 token = SWAP32(*tokens); @@ -687,6 +708,8 @@ static int parse_token(const uint32 *tokens, const uint32 tokencount) } // parse_token +// API entry point... + int D3D2GLSL_parse(const uint8 *tokenbuf, const uint32 bufsize) { const uint32 *tokens = (const uint32 *) tokenbuf; @@ -704,24 +727,5 @@ int D3D2GLSL_parse(const uint8 *tokenbuf, const uint32 bufsize) return (rc == END_OF_STREAM); } // D3D2GLSL_parse - -int main(int argc, char **argv) -{ - if (argv[1] != NULL) - { - FILE *io = fopen(argv[1], "rb"); - if (io != NULL) - { - uint8 *buf = (uint8 *) malloc(1000000); - int rc = fread(buf, 1, 1000000, io); - fclose(io); - D3D2GLSL_parse(buf, rc); - free(buf); - } // if - } // if - - return 0; -} // main - // end of parse.c ... diff --git a/testparse.c b/testparse.c new file mode 100644 index 00000000..7584da4a --- /dev/null +++ b/testparse.c @@ -0,0 +1,23 @@ +#include +#include "d3d2glsl.h" + +int main(int argc, char **argv) +{ + if (argv[1] != NULL) + { + FILE *io = fopen(argv[1], "rb"); + if (io != NULL) + { + uint8 *buf = (uint8 *) malloc(1000000); + int rc = fread(buf, 1, 1000000, io); + fclose(io); + D3D2GLSL_parse(buf, rc); + free(buf); + } // if + } // if + + return 0; +} // main + +// end of testparse.c ... +