author | Ryan C. Gordon <icculus@icculus.org> |
Fri, 04 Apr 2008 14:41:47 -0400 | |
branch | trunk |
changeset 97 | 4a41e3d17297 |
parent 94 | 57adfb4769a0 |
child 100 | 2b88649b6f98 |
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). |
|
97
4a41e3d17297
Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents:
94
diff
changeset
|
31 |
* (data) is the pointer you supplied when specifying these allocator |
4a41e3d17297
Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents:
94
diff
changeset
|
32 |
* callbacks, in case you need instance-specific data...it is passed through |
4a41e3d17297
Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents:
94
diff
changeset
|
33 |
* to your allocator unmolested, and can be NULL if you like. |
20
bb2e8f285acc
[svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents:
18
diff
changeset
|
34 |
*/ |
97
4a41e3d17297
Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents:
94
diff
changeset
|
35 |
typedef void *(*MOJOSHADER_malloc)(int bytes, void *data); |
4a41e3d17297
Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents:
94
diff
changeset
|
36 |
typedef void (*MOJOSHADER_free)(void *ptr, void *data); |
18
0dbdb2be8bf8
[svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents:
14
diff
changeset
|
37 |
|
46 | 38 |
|
39 |
/* |
|
40 |
* These are enum values, but they also can be used in bitmasks, so we can |
|
41 |
* test if an opcode is acceptable: if (op->shader_types & ourtype) {} ... |
|
42 |
*/ |
|
43 |
typedef enum |
|
44 |
{ |
|
45 |
MOJOSHADER_TYPE_UNKNOWN = 0, |
|
46 |
MOJOSHADER_TYPE_PIXEL = (1 << 0), |
|
47 |
MOJOSHADER_TYPE_VERTEX = (1 << 1), |
|
48 |
MOJOSHADER_TYPE_GEOMETRY = (1 << 2), /* (not supported yet.) */ |
|
49 |
MOJOSHADER_TYPE_ANY = 0xFFFFFFFF /* used for bitmasks */ |
|
50 |
} MOJOSHADER_shaderType; |
|
51 |
||
94
57adfb4769a0
Simplifed public uniform information.
Ryan C. Gordon <icculus@icculus.org>
parents:
92
diff
changeset
|
52 |
/* |
57adfb4769a0
Simplifed public uniform information.
Ryan C. Gordon <icculus@icculus.org>
parents:
92
diff
changeset
|
53 |
* Data types for uniforms. See MOJOSHADER_uniform for more information. |
57adfb4769a0
Simplifed public uniform information.
Ryan C. Gordon <icculus@icculus.org>
parents:
92
diff
changeset
|
54 |
*/ |
92
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
55 |
typedef enum |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
56 |
{ |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
57 |
MOJOSHADER_UNIFORM_FLOAT, |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
58 |
MOJOSHADER_UNIFORM_INT, |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
59 |
MOJOSHADER_UNIFORM_BOOL |
94
57adfb4769a0
Simplifed public uniform information.
Ryan C. Gordon <icculus@icculus.org>
parents:
92
diff
changeset
|
60 |
} MOJOSHADER_uniformType; |
92
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
61 |
|
94
57adfb4769a0
Simplifed public uniform information.
Ryan C. Gordon <icculus@icculus.org>
parents:
92
diff
changeset
|
62 |
/* |
57adfb4769a0
Simplifed public uniform information.
Ryan C. Gordon <icculus@icculus.org>
parents:
92
diff
changeset
|
63 |
* These are the uniforms to be set for a shader. "Uniforms" are what Direct3D |
57adfb4769a0
Simplifed public uniform information.
Ryan C. Gordon <icculus@icculus.org>
parents:
92
diff
changeset
|
64 |
* calls "Constants" ... IDirect3DDevice::SetVertexShaderConstantF() would |
57adfb4769a0
Simplifed public uniform information.
Ryan C. Gordon <icculus@icculus.org>
parents:
92
diff
changeset
|
65 |
* need this data, for example. These integers are register indexes. So if |
57adfb4769a0
Simplifed public uniform information.
Ryan C. Gordon <icculus@icculus.org>
parents:
92
diff
changeset
|
66 |
* index==6 and type==MOJOSHADER_UNIFORM_FLOAT, that means we'd expect a |
57adfb4769a0
Simplifed public uniform information.
Ryan C. Gordon <icculus@icculus.org>
parents:
92
diff
changeset
|
67 |
* 4-float vector to be specified for what would be register "c6" in D3D |
57adfb4769a0
Simplifed public uniform information.
Ryan C. Gordon <icculus@icculus.org>
parents:
92
diff
changeset
|
68 |
* assembly language, before drawing with the shader. |
57adfb4769a0
Simplifed public uniform information.
Ryan C. Gordon <icculus@icculus.org>
parents:
92
diff
changeset
|
69 |
*/ |
92
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
70 |
typedef struct |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
71 |
{ |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
72 |
int index; |
94
57adfb4769a0
Simplifed public uniform information.
Ryan C. Gordon <icculus@icculus.org>
parents:
92
diff
changeset
|
73 |
MOJOSHADER_uniformType type; |
92
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
74 |
} MOJOSHADER_uniform; |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
75 |
|
46 | 76 |
|
77 |
/* |
|
78 |
* Structure used to return data from parsing of a shader... |
|
79 |
*/ |
|
80 |
typedef struct |
|
81 |
{ |
|
82 |
/* |
|
83 |
* Human-readable error, if there is one. Will be NULL if there was no |
|
84 |
* error. The string will be UTF-8 encoded, and English only. Most of |
|
85 |
* these shouldn't be shown to the end-user anyhow. |
|
86 |
*/ |
|
87 |
const char *error; |
|
88 |
||
89 |
/* |
|
90 |
* Bytes of output from parsing. Most profiles produce a string of source |
|
91 |
* code, but profiles that do binary output may not be text at all. |
|
92 |
* Will be NULL on error. |
|
93 |
*/ |
|
94 |
const char *output; |
|
95 |
||
96 |
/* |
|
97 |
* Byte count for output, not counting any null terminator. Most profiles |
|
98 |
* produce an ASCII string of source code, but profiles that do binary |
|
99 |
* output may not be text at all. Will be 0 on error. |
|
100 |
*/ |
|
101 |
int output_len; |
|
102 |
||
103 |
/* |
|
104 |
* Count of Direct3D instructions we parsed. This is meaningless in terms |
|
105 |
* of the actual output, as the profile will probably grow or reduce |
|
106 |
* the count (or for high-level languages, not have that information at |
|
107 |
* all), but it can give you a rough idea of the size of your shader. |
|
108 |
* Will be zero on error. |
|
109 |
*/ |
|
110 |
int instruction_count; |
|
111 |
||
112 |
/* |
|
113 |
* The type of shader we parsed. Will be MOJOSHADER_TYPE_UNKNOWN on error. |
|
114 |
*/ |
|
115 |
MOJOSHADER_shaderType shader_type; |
|
116 |
||
117 |
/* |
|
118 |
* The shader's major version. If this was a "vs_3_0", this would be 3. |
|
119 |
*/ |
|
120 |
int major_ver; |
|
121 |
||
122 |
/* |
|
123 |
* The shader's minor version. If this was a "ps_1_4", this would be 4. |
|
124 |
* Two notes: for "vs_2_x", this is 1, and for "vs_3_sw", this is 255. |
|
125 |
*/ |
|
126 |
int minor_ver; |
|
127 |
||
128 |
/* |
|
92
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
129 |
* The number of elements pointed to by (uniforms). |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
130 |
*/ |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
131 |
int uniform_count; |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
132 |
|
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
133 |
/* |
94
57adfb4769a0
Simplifed public uniform information.
Ryan C. Gordon <icculus@icculus.org>
parents:
92
diff
changeset
|
134 |
* (uniform_count) elements of data that specify Uniforms to be set for |
57adfb4769a0
Simplifed public uniform information.
Ryan C. Gordon <icculus@icculus.org>
parents:
92
diff
changeset
|
135 |
* this shader. See discussion on MOJOSHADER_uniform for details. |
92
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
136 |
*/ |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
137 |
MOJOSHADER_uniform *uniforms; |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
138 |
|
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
139 |
/* |
46 | 140 |
* This is the malloc implementation you passed to MOJOSHADER_parse(). |
141 |
*/ |
|
142 |
MOJOSHADER_malloc malloc; |
|
143 |
||
144 |
/* |
|
145 |
* This is the free implementation you passed to MOJOSHADER_parse(). |
|
146 |
*/ |
|
147 |
MOJOSHADER_free free; |
|
97
4a41e3d17297
Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents:
94
diff
changeset
|
148 |
|
4a41e3d17297
Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents:
94
diff
changeset
|
149 |
/* |
4a41e3d17297
Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents:
94
diff
changeset
|
150 |
* This is the pointer you passed as opaque data for your allocator. |
4a41e3d17297
Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents:
94
diff
changeset
|
151 |
*/ |
4a41e3d17297
Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents:
94
diff
changeset
|
152 |
void *malloc_data; |
46 | 153 |
} MOJOSHADER_parseData; |
154 |
||
155 |
||
156 |
/* |
|
157 |
* Profile string for Direct3D assembly language output. |
|
158 |
*/ |
|
159 |
#define MOJOSHADER_PROFILE_D3D "d3d" |
|
160 |
||
161 |
/* |
|
162 |
* Profile string for GLSL: OpenGL high-level shader language output. |
|
163 |
*/ |
|
164 |
#define MOJOSHADER_PROFILE_GLSL "glsl" |
|
165 |
||
97
4a41e3d17297
Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents:
94
diff
changeset
|
166 |
|
46 | 167 |
/* |
168 |
* Parse a compiled Direct3D shader's bytecode. |
|
169 |
* |
|
170 |
* This is your primary entry point into MojoShader. You need to pass it |
|
171 |
* a compiled D3D shader and tell it which "profile" you want to use to |
|
172 |
* convert it into useful data. |
|
173 |
* |
|
174 |
* The available profiles are the set of MOJOSHADER_PROFILE_* defines. |
|
175 |
* Note that MojoShader may be built without support for all listed |
|
176 |
* profiles (in which case using one here will return with an error). |
|
177 |
* |
|
178 |
* As parsing requires some memory to be allocated, you may provide a custom |
|
179 |
* allocator to this function, which will be used to allocate/free memory. |
|
180 |
* They function just like malloc() and free(). We do not use realloc(). |
|
97
4a41e3d17297
Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents:
94
diff
changeset
|
181 |
* If you don't care, pass NULL in for the allocator functions. If your |
4a41e3d17297
Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents:
94
diff
changeset
|
182 |
* allocator needs instance-specific data, you may supply it with the |
4a41e3d17297
Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents:
94
diff
changeset
|
183 |
* (d) parameter. This pointer is passed as-is to your (m) and (f) functions. |
46 | 184 |
* |
97
4a41e3d17297
Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents:
94
diff
changeset
|
185 |
* This function returns a MOJOSHADER_parseData. |
46 | 186 |
* |
187 |
* This function will never return NULL, even if the system is completely |
|
188 |
* out of memory upon entry (in which case, this function returns a static |
|
189 |
* MOJOSHADER_parseData object, which is still safe to pass to |
|
190 |
* MOJOSHADER_freeParseData()). |
|
191 |
* |
|
192 |
* This function is thread safe, so long (m) and (f) are too, and that |
|
193 |
* (tokenbuf) remains intact for the duration of the call. This allows you |
|
194 |
* to parse several shaders on separate CPU cores at the same time. |
|
195 |
*/ |
|
196 |
const MOJOSHADER_parseData *MOJOSHADER_parse(const char *profile, |
|
197 |
const unsigned char *tokenbuf, |
|
198 |
const unsigned int bufsize, |
|
199 |
MOJOSHADER_malloc m, |
|
97
4a41e3d17297
Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents:
94
diff
changeset
|
200 |
MOJOSHADER_free f, |
4a41e3d17297
Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents:
94
diff
changeset
|
201 |
void *d); |
46 | 202 |
|
203 |
/* |
|
204 |
* Call this to dispose of parsing results when you are done with them. |
|
205 |
* This will call the MOJOSHADER_free function you provided to |
|
206 |
* MOJOSHADER_parse multiple times, if you provided one. |
|
207 |
* Passing a NULL here is a safe no-op. |
|
208 |
* |
|
209 |
* This function is thread safe, so long as any allocator you passed into |
|
210 |
* MOJOSHADER_parse() is, too. |
|
211 |
*/ |
|
212 |
void MOJOSHADER_freeParseData(const MOJOSHADER_parseData *data); |
|
7 | 213 |
|
214 |
#ifdef __cplusplus |
|
215 |
} |
|
216 |
#endif |
|
217 |
||
218 |
#endif /* include-once blocker. */ |
|
219 |
||
35 | 220 |
/* end of mojoshader.h ... */ |
7 | 221 |