Skip to content
Permalink
Browse files
[svn] Cleanups, split things out.
--HG--
branch : trunk
  • Loading branch information
icculus committed Feb 10, 2008
1 parent 2caaf91 commit 7e55d66c443e24172e572080e9c5224ffdb63b29
Showing with 83 additions and 29 deletions.
  1. +1 −1 CMakeLists.txt
  2. +27 −0 d3d2glsl.h
  3. +32 −28 parse.c
  4. +23 −0 testparse.c
@@ -1,5 +1,5 @@
PROJECT(d3d2glsl)
ADD_EXECUTABLE(parse parse.c)
ADD_EXECUTABLE(parse testparse.c parse.c)

# End of CMakeLists.txt ...

@@ -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 ... */

60 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 <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 @@ 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 ...

@@ -0,0 +1,23 @@
#include <stdio.h>
#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 ...

0 comments on commit 7e55d66

Please sign in to comment.