1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
32 #include "../pp/sl_pp_public.h"
33 #include "../cl/sl_cl_parse.h"
40 printf(" compile fragment|vertex <source> <output>\n");
50 struct sl_pp_purify_options options
;
51 char errmsg
[100] = "";
52 struct sl_pp_context
*context
;
53 struct sl_pp_token_info
*tokens
;
55 unsigned int tokens_eaten
;
56 struct sl_pp_token_info
*outtokens
;
59 unsigned char *outbytes
;
60 unsigned int cboutbytes
;
61 unsigned int shader_type
;
68 if (!strcmp(argv
[1], "fragment")) {
70 } else if (!strcmp(argv
[1], "vertex")) {
77 in
= fopen(argv
[2], "rb");
79 printf("Could not open `%s' for read.\n", argv
[2]);
84 fseek(in
, 0, SEEK_END
);
86 fseek(in
, 0, SEEK_SET
);
88 out
= fopen(argv
[3], "w");
91 printf("Could not open `%s' for write.\n", argv
[3]);
96 inbuf
= malloc(size
+ 1);
98 fprintf(out
, "$OOMERROR\n");
102 printf("Out of memory.\n");
106 if (fread(inbuf
, 1, size
, in
) != size
) {
107 fprintf(out
, "$READERROR\n");
112 printf("Could not read from `%s'.\n", argv
[2]);
119 memset(&options
, 0, sizeof(options
));
121 context
= sl_pp_context_create();
123 fprintf(out
, "$CONTEXERROR\n");
127 printf("Could not create parse context.\n");
131 if (sl_pp_tokenise(context
, inbuf
, &options
, &tokens
)) {
132 fprintf(out
, "$ERROR: `%s'\n", sl_pp_context_error_message(context
));
134 printf("Error: %s.\n", sl_pp_context_error_message(context
));
135 sl_pp_context_destroy(context
);
143 if (sl_pp_version(context
, tokens
, &version
, &tokens_eaten
)) {
144 fprintf(out
, "$ERROR: `%s'\n", sl_pp_context_error_message(context
));
146 printf("Error: %s\n", sl_pp_context_error_message(context
));
147 sl_pp_context_destroy(context
);
153 if (sl_pp_context_add_extension(context
, "ARB_draw_buffers", "GL_ARB_draw_buffers") ||
154 sl_pp_context_add_extension(context
, "ARB_texture_rectangle", "GL_ARB_texture_rectangle")) {
155 fprintf(out
, "$ERROR: `%s'\n", sl_pp_context_error_message(context
));
157 printf("Error: %s\n", sl_pp_context_error_message(context
));
158 sl_pp_context_destroy(context
);
164 if (sl_pp_process(context
, &tokens
[tokens_eaten
], &outtokens
)) {
165 fprintf(out
, "$ERROR: `%s'\n", sl_pp_context_error_message(context
));
167 printf("Error: %s\n", sl_pp_context_error_message(context
));
168 sl_pp_context_destroy(context
);
176 for (i
= j
= 0; outtokens
[i
].token
!= SL_PP_EOF
; i
++) {
177 switch (outtokens
[i
].token
) {
179 case SL_PP_EXTENSION_REQUIRE
:
180 case SL_PP_EXTENSION_ENABLE
:
181 case SL_PP_EXTENSION_WARN
:
182 case SL_PP_EXTENSION_DISABLE
:
186 outtokens
[j
++] = outtokens
[i
];
189 outtokens
[j
] = outtokens
[i
];
191 if (sl_cl_compile(context
, outtokens
, shader_type
, 1, &outbytes
, &cboutbytes
, errmsg
, sizeof(errmsg
)) == 0) {
193 unsigned int line
= 0;
195 fprintf(out
, "\n/* DO NOT EDIT - THIS FILE IS AUTOMATICALLY GENERATED FROM THE FOLLOWING FILE: */");
196 fprintf(out
, "\n/* %s */", argv
[2]);
197 fprintf(out
, "\n\n");
199 for (i
= 0; i
< cboutbytes
; i
++) {
202 if (outbytes
[i
] < 10) {
204 } else if (outbytes
[i
] < 100) {
209 if (i
< cboutbytes
- 1) {
212 if (line
+ a
>= 100) {
217 fprintf (out
, "%u", outbytes
[i
]);
218 if (i
< cboutbytes
- 1) {
225 fprintf(out
, "$SYNTAXERROR: `%s'\n", errmsg
);
227 printf("Error: %s\n", errmsg
);
230 sl_pp_context_destroy(context
);