Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[svn] Cleanups, split things out.
--HG--
branch : trunk
  • Loading branch information
icculus committed Feb 10, 2008
1 parent 2caaf91 commit 7e55d66
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 29 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
@@ -1,5 +1,5 @@
PROJECT(d3d2glsl)
ADD_EXECUTABLE(parse parse.c)
ADD_EXECUTABLE(parse testparse.c parse.c)

# End of CMakeLists.txt ...

27 changes: 27 additions & 0 deletions 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 ... */

60 changes: 32 additions & 28 deletions 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)
{
Expand All @@ -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)
Expand Down Expand Up @@ -450,6 +468,7 @@ static int parse_RESERVED(const uint32 *argtokens)



// Lookup table for instruction opcodes...

typedef struct
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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 ...

23 changes: 23 additions & 0 deletions testparse.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.