author | icculus |
Thu, 27 Mar 2008 22:35:19 -0400 | |
branch | trunk |
changeset 46 | ff5a0ec44f00 |
parent 36 | 5ea7c5f6cf2d |
child 92 | bc1bb138e855 |
permissions | -rw-r--r-- |
7 | 1 |
/** |
35 | 2 |
* MojoShader; generate shader programs from bytecode of compiled |
3 |
* Direct3D shaders. |
|
7 | 4 |
* |
5 |
* Please see the file LICENSE.txt in the source's root directory. |
|
6 |
* |
|
7 |
* This file written by Ryan C. Gordon. |
|
8 |
*/ |
|
9 |
||
35 | 10 |
#ifndef __INCL_MOJOSHADER_H_ |
11 |
#define __INCL_MOJOSHADER_H_ |
|
7 | 12 |
|
13 |
#ifdef __cplusplus |
|
14 |
extern "C" { |
|
15 |
#endif |
|
16 |
||
20
bb2e8f285acc
[svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents:
18
diff
changeset
|
17 |
/* |
46 | 18 |
* For determining the version of MojoShader you are using: |
19 |
* const int compiled_against = MOJOSHADER_VERSION; |
|
20 |
* const int linked_against = MOJOSHADER_version(); |
|
21 |
* |
|
22 |
* The version is a single integer that increments, not a major/minor value. |
|
20
bb2e8f285acc
[svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents:
18
diff
changeset
|
23 |
*/ |
35 | 24 |
#define MOJOSHADER_VERSION 1 |
25 |
int MOJOSHADER_version(void); |
|
20
bb2e8f285acc
[svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents:
18
diff
changeset
|
26 |
|
bb2e8f285acc
[svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents:
18
diff
changeset
|
27 |
/* |
bb2e8f285acc
[svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents:
18
diff
changeset
|
28 |
* These allocators work just like the C runtime's malloc() and free() |
46 | 29 |
* (in fact, they probably use malloc() and free() internally if you don't |
30 |
* specify your own allocator, but don't rely on that behaviour). |
|
20
bb2e8f285acc
[svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents:
18
diff
changeset
|
31 |
*/ |
35 | 32 |
typedef void *(*MOJOSHADER_malloc)(int bytes); |
33 |
typedef void (*MOJOSHADER_free)(void *ptr); |
|
18
0dbdb2be8bf8
[svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents:
14
diff
changeset
|
34 |
|
46 | 35 |
|
36 |
/* |
|
37 |
* These are enum values, but they also can be used in bitmasks, so we can |
|
38 |
* test if an opcode is acceptable: if (op->shader_types & ourtype) {} ... |
|
39 |
*/ |
|
40 |
typedef enum |
|
41 |
{ |
|
42 |
MOJOSHADER_TYPE_UNKNOWN = 0, |
|
43 |
MOJOSHADER_TYPE_PIXEL = (1 << 0), |
|
44 |
MOJOSHADER_TYPE_VERTEX = (1 << 1), |
|
45 |
MOJOSHADER_TYPE_GEOMETRY = (1 << 2), /* (not supported yet.) */ |
|
46 |
MOJOSHADER_TYPE_ANY = 0xFFFFFFFF /* used for bitmasks */ |
|
47 |
} MOJOSHADER_shaderType; |
|
48 |
||
49 |
||
50 |
/* |
|
51 |
* Structure used to return data from parsing of a shader... |
|
52 |
*/ |
|
53 |
typedef struct |
|
54 |
{ |
|
55 |
/* |
|
56 |
* Human-readable error, if there is one. Will be NULL if there was no |
|
57 |
* error. The string will be UTF-8 encoded, and English only. Most of |
|
58 |
* these shouldn't be shown to the end-user anyhow. |
|
59 |
*/ |
|
60 |
const char *error; |
|
61 |
||
62 |
/* |
|
63 |
* Bytes of output from parsing. Most profiles produce a string of source |
|
64 |
* code, but profiles that do binary output may not be text at all. |
|
65 |
* Will be NULL on error. |
|
66 |
*/ |
|
67 |
const char *output; |
|
68 |
||
69 |
/* |
|
70 |
* Byte count for output, not counting any null terminator. Most profiles |
|
71 |
* produce an ASCII string of source code, but profiles that do binary |
|
72 |
* output may not be text at all. Will be 0 on error. |
|
73 |
*/ |
|
74 |
int output_len; |
|
75 |
||
76 |
/* |
|
77 |
* Count of Direct3D instructions we parsed. This is meaningless in terms |
|
78 |
* of the actual output, as the profile will probably grow or reduce |
|
79 |
* the count (or for high-level languages, not have that information at |
|
80 |
* all), but it can give you a rough idea of the size of your shader. |
|
81 |
* Will be zero on error. |
|
82 |
*/ |
|
83 |
int instruction_count; |
|
84 |
||
85 |
/* |
|
86 |
* The type of shader we parsed. Will be MOJOSHADER_TYPE_UNKNOWN on error. |
|
87 |
*/ |
|
88 |
MOJOSHADER_shaderType shader_type; |
|
89 |
||
90 |
/* |
|
91 |
* The shader's major version. If this was a "vs_3_0", this would be 3. |
|
92 |
*/ |
|
93 |
int major_ver; |
|
94 |
||
95 |
/* |
|
96 |
* The shader's minor version. If this was a "ps_1_4", this would be 4. |
|
97 |
* Two notes: for "vs_2_x", this is 1, and for "vs_3_sw", this is 255. |
|
98 |
*/ |
|
99 |
int minor_ver; |
|
100 |
||
101 |
/* |
|
102 |
* This is the malloc implementation you passed to MOJOSHADER_parse(). |
|
103 |
*/ |
|
104 |
MOJOSHADER_malloc malloc; |
|
105 |
||
106 |
/* |
|
107 |
* This is the free implementation you passed to MOJOSHADER_parse(). |
|
108 |
*/ |
|
109 |
MOJOSHADER_free free; |
|
110 |
} MOJOSHADER_parseData; |
|
111 |
||
112 |
||
113 |
/* |
|
114 |
* Profile string for Direct3D assembly language output. |
|
115 |
*/ |
|
116 |
#define MOJOSHADER_PROFILE_D3D "d3d" |
|
117 |
||
118 |
/* |
|
119 |
* Profile string for GLSL: OpenGL high-level shader language output. |
|
120 |
*/ |
|
121 |
#define MOJOSHADER_PROFILE_GLSL "glsl" |
|
122 |
||
123 |
/* |
|
124 |
* Parse a compiled Direct3D shader's bytecode. |
|
125 |
* |
|
126 |
* This is your primary entry point into MojoShader. You need to pass it |
|
127 |
* a compiled D3D shader and tell it which "profile" you want to use to |
|
128 |
* convert it into useful data. |
|
129 |
* |
|
130 |
* The available profiles are the set of MOJOSHADER_PROFILE_* defines. |
|
131 |
* Note that MojoShader may be built without support for all listed |
|
132 |
* profiles (in which case using one here will return with an error). |
|
133 |
* |
|
134 |
* As parsing requires some memory to be allocated, you may provide a custom |
|
135 |
* allocator to this function, which will be used to allocate/free memory. |
|
136 |
* They function just like malloc() and free(). We do not use realloc(). |
|
137 |
* If you don't care, pass NULL in for the allocator functions. |
|
138 |
* |
|
139 |
* This function returns a MOJOSHADER_parseData |
|
140 |
* |
|
141 |
* This function will never return NULL, even if the system is completely |
|
142 |
* out of memory upon entry (in which case, this function returns a static |
|
143 |
* MOJOSHADER_parseData object, which is still safe to pass to |
|
144 |
* MOJOSHADER_freeParseData()). |
|
145 |
* |
|
146 |
* This function is thread safe, so long (m) and (f) are too, and that |
|
147 |
* (tokenbuf) remains intact for the duration of the call. This allows you |
|
148 |
* to parse several shaders on separate CPU cores at the same time. |
|
149 |
*/ |
|
150 |
const MOJOSHADER_parseData *MOJOSHADER_parse(const char *profile, |
|
151 |
const unsigned char *tokenbuf, |
|
152 |
const unsigned int bufsize, |
|
153 |
MOJOSHADER_malloc m, |
|
154 |
MOJOSHADER_free f); |
|
155 |
||
156 |
/* |
|
157 |
* Call this to dispose of parsing results when you are done with them. |
|
158 |
* This will call the MOJOSHADER_free function you provided to |
|
159 |
* MOJOSHADER_parse multiple times, if you provided one. |
|
160 |
* Passing a NULL here is a safe no-op. |
|
161 |
* |
|
162 |
* This function is thread safe, so long as any allocator you passed into |
|
163 |
* MOJOSHADER_parse() is, too. |
|
164 |
*/ |
|
165 |
void MOJOSHADER_freeParseData(const MOJOSHADER_parseData *data); |
|
7 | 166 |
|
167 |
#ifdef __cplusplus |
|
168 |
} |
|
169 |
#endif |
|
170 |
||
171 |
#endif /* include-once blocker. */ |
|
172 |
||
35 | 173 |
/* end of mojoshader.h ... */ |
7 | 174 |