ir_to_mesa: Support texture rectangle targets
[mesa/nouveau-pmpeg.git] / src / glsl / apps / compile.c
blob3aa4fd4d53e7c3b88075ba8126bac929693c9134
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
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
16 * of the Software.
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 **************************************************************************/
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <assert.h>
32 #include "../pp/sl_pp_public.h"
33 #include "../cl/sl_cl_parse.h"
36 static void
37 usage(void)
39 printf("Usage:\n");
40 printf(" compile fragment|vertex|geometry <source> <output>\n");
43 int
44 main(int argc,
45 char *argv[])
47 FILE *in;
48 long size;
49 char *inbuf;
50 struct sl_pp_purify_options options;
51 char errmsg[100] = "";
52 struct sl_pp_context *context;
53 unsigned int version;
54 FILE *out;
55 unsigned char *outbytes;
56 unsigned int cboutbytes;
57 unsigned int shader_type;
59 if (argc != 4) {
60 usage();
61 return 1;
64 if (!strcmp(argv[1], "fragment")) {
65 shader_type = 1;
66 } else if (!strcmp(argv[1], "vertex")) {
67 shader_type = 2;
68 } else if (!strcmp(argv[1], "geometry")) {
69 shader_type = 3;
70 } else {
71 usage();
72 return 1;
75 in = fopen(argv[2], "rb");
76 if (!in) {
77 printf("Could not open `%s' for read.\n", argv[2]);
78 usage();
79 return 1;
82 fseek(in, 0, SEEK_END);
83 size = ftell(in);
84 assert(size != -1);
85 if (size == -1) {
86 return 1;
88 fseek(in, 0, SEEK_SET);
90 out = fopen(argv[3], "w");
91 if (!out) {
92 fclose(in);
93 printf("Could not open `%s' for write.\n", argv[3]);
94 usage();
95 return 1;
98 inbuf = malloc(size + 1);
99 if (!inbuf) {
100 fprintf(out, "$OOMERROR\n");
102 fclose(out);
103 fclose(in);
104 printf("Out of memory.\n");
105 return 0;
108 if (fread(inbuf, 1, size, in) != size) {
109 fprintf(out, "$READERROR\n");
111 free(inbuf);
112 fclose(out);
113 fclose(in);
114 printf("Could not read from `%s'.\n", argv[2]);
115 return 0;
117 inbuf[size] = '\0';
119 fclose(in);
121 memset(&options, 0, sizeof(options));
123 context = sl_pp_context_create(inbuf, &options);
124 if (!context) {
125 fprintf(out, "$CONTEXERROR\n");
127 free(inbuf);
128 fclose(out);
129 printf("Could not create parse context.\n");
130 return 0;
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);
138 free(inbuf);
139 fclose(out);
140 return 0;
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);
149 free(inbuf);
150 fclose(out);
151 return 0;
154 if (sl_cl_compile(context, shader_type, 1, &outbytes, &cboutbytes, errmsg, sizeof(errmsg)) == 0) {
155 unsigned int i;
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++) {
163 unsigned int a;
165 if (outbytes[i] < 10) {
166 a = 1;
167 } else if (outbytes[i] < 100) {
168 a = 2;
169 } else {
170 a = 3;
172 if (i < cboutbytes - 1) {
173 a++;
175 if (line + a >= 100) {
176 fprintf (out, "\n");
177 line = 0;
179 line += a;
180 fprintf (out, "%u", outbytes[i]);
181 if (i < cboutbytes - 1) {
182 fprintf (out, ",");
185 fprintf (out, "\n");
186 free(outbytes);
187 } else {
188 fprintf(out, "$SYNTAXERROR: `%s'\n", errmsg);
190 printf("Error: %s\n", errmsg);
193 sl_pp_context_destroy(context);
194 free(inbuf);
195 fclose(out);
196 return 0;