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|geometry <source> <output>\n");
50 struct sl_pp_purify_options options
;
51 char errmsg
[100] = "";
52 struct sl_pp_context
*context
;
55 unsigned char *outbytes
;
56 unsigned int cboutbytes
;
57 unsigned int shader_type
;
64 if (!strcmp(argv
[1], "fragment")) {
66 } else if (!strcmp(argv
[1], "vertex")) {
68 } else if (!strcmp(argv
[1], "geometry")) {
75 in
= fopen(argv
[2], "rb");
77 printf("Could not open `%s' for read.\n", argv
[2]);
82 fseek(in
, 0, SEEK_END
);
88 fseek(in
, 0, SEEK_SET
);
90 out
= fopen(argv
[3], "w");
93 printf("Could not open `%s' for write.\n", argv
[3]);
98 inbuf
= malloc(size
+ 1);
100 fprintf(out
, "$OOMERROR\n");
104 printf("Out of memory.\n");
108 if (fread(inbuf
, 1, size
, in
) != size
) {
109 fprintf(out
, "$READERROR\n");
114 printf("Could not read from `%s'.\n", argv
[2]);
121 memset(&options
, 0, sizeof(options
));
123 context
= sl_pp_context_create(inbuf
, &options
);
125 fprintf(out
, "$CONTEXERROR\n");
129 printf("Could not create parse context.\n");
133 if (sl_pp_version(context
, &version
)) {
134 fprintf(out
, "$ERROR: `%s'\n", sl_pp_context_error_message(context
));
136 printf("Error: %s\n", sl_pp_context_error_message(context
));
137 sl_pp_context_destroy(context
);
143 if (sl_pp_context_add_extension(context
, "GL_ARB_draw_buffers") ||
144 sl_pp_context_add_extension(context
, "GL_ARB_texture_rectangle")) {
145 fprintf(out
, "$ERROR: `%s'\n", sl_pp_context_error_message(context
));
147 printf("Error: %s\n", sl_pp_context_error_message(context
));
148 sl_pp_context_destroy(context
);
154 if (sl_cl_compile(context
, shader_type
, 1, &outbytes
, &cboutbytes
, errmsg
, sizeof(errmsg
)) == 0) {
156 unsigned int line
= 0;
158 fprintf(out
, "\n/* DO NOT EDIT - THIS FILE IS AUTOMATICALLY GENERATED FROM THE FOLLOWING FILE: */");
159 fprintf(out
, "\n/* %s */", argv
[2]);
160 fprintf(out
, "\n\n");
162 for (i
= 0; i
< cboutbytes
; i
++) {
165 if (outbytes
[i
] < 10) {
167 } else if (outbytes
[i
] < 100) {
172 if (i
< cboutbytes
- 1) {
175 if (line
+ a
>= 100) {
180 fprintf (out
, "%u", outbytes
[i
]);
181 if (i
< cboutbytes
- 1) {
188 fprintf(out
, "$SYNTAXERROR: `%s'\n", errmsg
);
190 printf("Error: %s\n", errmsg
);
193 sl_pp_context_destroy(context
);