parse.c
branchtrunk
changeset 7 18bf77bb8c07
parent 6 89bff6a08fca
--- a/parse.c	Sat Feb 09 19:00:59 2008 -0500
+++ b/parse.c	Sun Feb 10 18:34:12 2008 -0500
@@ -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 <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
 
-#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 @@
 #   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 @@
 
 
 
+// Lookup table for instruction opcodes...
 
 typedef struct
 {
@@ -564,6 +583,8 @@
 };
 
 
+// parse various token types...
+
 static int parse_instruction_token(const uint32 *tokens, const uint32 tokencount)
 {
     const uint32 token = SWAP32(*tokens);
@@ -687,6 +708,8 @@
 } // parse_token
 
 
+// API entry point...
+
 int D3D2GLSL_parse(const uint8 *tokenbuf, const uint32 bufsize)
 {
     const uint32 *tokens = (const uint32 *) tokenbuf;
@@ -704,24 +727,5 @@
     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 ...