Merge branch 'draw-instanced'
[mesa.git] / src / glsl / main.cpp
1 /*
2 * Copyright © 2008, 2009 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23 #include <cstdlib>
24 #include <cstdio>
25 #include <getopt.h>
26
27 #include "ast.h"
28 #include "glsl_parser_extras.h"
29 #include "glsl_parser.h"
30 #include "ir_optimization.h"
31 #include "ir_print_visitor.h"
32 #include "program.h"
33 #include "loop_analysis.h"
34
35 extern "C" struct gl_shader *
36 _mesa_new_shader(struct gl_context *ctx, GLuint name, GLenum type);
37
38 extern "C" void
39 _mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr,
40 struct gl_shader *sh);
41
42 /* Copied from shader_api.c for the stand-alone compiler.
43 */
44 void
45 _mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr,
46 struct gl_shader *sh)
47 {
48 *ptr = sh;
49 }
50
51 struct gl_shader *
52 _mesa_new_shader(struct gl_context *ctx, GLuint name, GLenum type)
53 {
54 struct gl_shader *shader;
55
56 (void) ctx;
57
58 assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER);
59 shader = talloc_zero(NULL, struct gl_shader);
60 if (shader) {
61 shader->Type = type;
62 shader->Name = name;
63 shader->RefCount = 1;
64 }
65 return shader;
66 }
67
68 static void
69 initialize_context(struct gl_context *ctx, gl_api api)
70 {
71 memset(ctx, 0, sizeof(*ctx));
72
73 ctx->API = api;
74
75 ctx->Extensions.ARB_draw_buffers = GL_TRUE;
76 ctx->Extensions.ARB_draw_instanced = GL_TRUE;
77 ctx->Extensions.ARB_fragment_coord_conventions = GL_TRUE;
78 ctx->Extensions.EXT_texture_array = GL_TRUE;
79 ctx->Extensions.NV_texture_rectangle = GL_TRUE;
80
81 /* 1.10 minimums. */
82 ctx->Const.MaxLights = 8;
83 ctx->Const.MaxClipPlanes = 8;
84 ctx->Const.MaxTextureUnits = 2;
85
86 /* More than the 1.10 minimum to appease parser tests taken from
87 * apps that (hopefully) already checked the number of coords.
88 */
89 ctx->Const.MaxTextureCoordUnits = 4;
90
91 ctx->Const.VertexProgram.MaxAttribs = 16;
92 ctx->Const.VertexProgram.MaxUniformComponents = 512;
93 ctx->Const.MaxVarying = 8;
94 ctx->Const.MaxVertexTextureImageUnits = 0;
95 ctx->Const.MaxCombinedTextureImageUnits = 2;
96 ctx->Const.MaxTextureImageUnits = 2;
97 ctx->Const.FragmentProgram.MaxUniformComponents = 64;
98
99 ctx->Const.MaxDrawBuffers = 2;
100
101 ctx->Driver.NewShader = _mesa_new_shader;
102 }
103
104 /* Returned string will have 'ctx' as its talloc owner. */
105 static char *
106 load_text_file(void *ctx, const char *file_name)
107 {
108 char *text = NULL;
109 size_t size;
110 size_t total_read = 0;
111 FILE *fp = fopen(file_name, "rb");
112
113 if (!fp) {
114 return NULL;
115 }
116
117 fseek(fp, 0L, SEEK_END);
118 size = ftell(fp);
119 fseek(fp, 0L, SEEK_SET);
120
121 text = (char *) talloc_size(ctx, size + 1);
122 if (text != NULL) {
123 do {
124 size_t bytes = fread(text + total_read,
125 1, size - total_read, fp);
126 if (bytes < size - total_read) {
127 free(text);
128 text = NULL;
129 break;
130 }
131
132 if (bytes == 0) {
133 break;
134 }
135
136 total_read += bytes;
137 } while (total_read < size);
138
139 text[total_read] = '\0';
140 }
141
142 fclose(fp);
143
144 return text;
145 }
146
147 int glsl_es = 0;
148 int dump_ast = 0;
149 int dump_hir = 0;
150 int dump_lir = 0;
151 int do_link = 0;
152
153 const struct option compiler_opts[] = {
154 { "glsl-es", 0, &glsl_es, 1 },
155 { "dump-ast", 0, &dump_ast, 1 },
156 { "dump-hir", 0, &dump_hir, 1 },
157 { "dump-lir", 0, &dump_lir, 1 },
158 { "link", 0, &do_link, 1 },
159 { NULL, 0, NULL, 0 }
160 };
161
162 /**
163 * \brief Print proper usage and exit with failure.
164 */
165 void
166 usage_fail(const char *name)
167 {
168
169 const char *header =
170 "usage: %s [options] <file.vert | file.geom | file.frag>\n"
171 "\n"
172 "Possible options are:\n";
173 printf(header, name, name);
174 for (const struct option *o = compiler_opts; o->name != 0; ++o) {
175 printf(" --%s\n", o->name);
176 }
177 exit(EXIT_FAILURE);
178 }
179
180
181 void
182 compile_shader(struct gl_context *ctx, struct gl_shader *shader)
183 {
184 struct _mesa_glsl_parse_state *state =
185 new(shader) _mesa_glsl_parse_state(ctx, shader->Type, shader);
186
187 const char *source = shader->Source;
188 state->error = preprocess(state, &source, &state->info_log,
189 state->extensions, ctx->API) != 0;
190
191 if (!state->error) {
192 _mesa_glsl_lexer_ctor(state, source);
193 _mesa_glsl_parse(state);
194 _mesa_glsl_lexer_dtor(state);
195 }
196
197 if (dump_ast) {
198 foreach_list_const(n, &state->translation_unit) {
199 ast_node *ast = exec_node_data(ast_node, n, link);
200 ast->print();
201 }
202 printf("\n\n");
203 }
204
205 shader->ir = new(shader) exec_list;
206 if (!state->error && !state->translation_unit.is_empty())
207 _mesa_ast_to_hir(shader->ir, state);
208
209 /* Print out the unoptimized IR. */
210 if (!state->error && dump_hir) {
211 validate_ir_tree(shader->ir);
212 _mesa_print_ir(shader->ir, state);
213 }
214
215 /* Optimization passes */
216 if (!state->error && !shader->ir->is_empty()) {
217 bool progress;
218 do {
219 progress = do_common_optimization(shader->ir, false, 32);
220 } while (progress);
221
222 validate_ir_tree(shader->ir);
223 }
224
225
226 /* Print out the resulting IR */
227 if (!state->error && dump_lir) {
228 _mesa_print_ir(shader->ir, state);
229 }
230
231 shader->symbols = state->symbols;
232 shader->CompileStatus = !state->error;
233 shader->Version = state->language_version;
234 memcpy(shader->builtins_to_link, state->builtins_to_link,
235 sizeof(shader->builtins_to_link[0]) * state->num_builtins_to_link);
236 shader->num_builtins_to_link = state->num_builtins_to_link;
237
238 if (shader->InfoLog)
239 talloc_free(shader->InfoLog);
240
241 shader->InfoLog = state->info_log;
242
243 /* Retain any live IR, but trash the rest. */
244 reparent_ir(shader->ir, shader);
245
246 talloc_free(state);
247
248 return;
249 }
250
251 int
252 main(int argc, char **argv)
253 {
254 int status = EXIT_SUCCESS;
255 struct gl_context local_ctx;
256 struct gl_context *ctx = &local_ctx;
257
258 int c;
259 int idx = 0;
260 while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1)
261 /* empty */ ;
262
263
264 if (argc <= optind)
265 usage_fail(argv[0]);
266
267 initialize_context(ctx, (glsl_es) ? API_OPENGLES2 : API_OPENGL);
268
269 struct gl_shader_program *whole_program;
270
271 whole_program = talloc_zero (NULL, struct gl_shader_program);
272 assert(whole_program != NULL);
273
274 for (/* empty */; argc > optind; optind++) {
275 whole_program->Shaders = (struct gl_shader **)
276 talloc_realloc(whole_program, whole_program->Shaders,
277 struct gl_shader *, whole_program->NumShaders + 1);
278 assert(whole_program->Shaders != NULL);
279
280 struct gl_shader *shader = talloc_zero(whole_program, gl_shader);
281
282 whole_program->Shaders[whole_program->NumShaders] = shader;
283 whole_program->NumShaders++;
284
285 const unsigned len = strlen(argv[optind]);
286 if (len < 6)
287 usage_fail(argv[0]);
288
289 const char *const ext = & argv[optind][len - 5];
290 if (strncmp(".vert", ext, 5) == 0)
291 shader->Type = GL_VERTEX_SHADER;
292 else if (strncmp(".geom", ext, 5) == 0)
293 shader->Type = GL_GEOMETRY_SHADER;
294 else if (strncmp(".frag", ext, 5) == 0)
295 shader->Type = GL_FRAGMENT_SHADER;
296 else
297 usage_fail(argv[0]);
298
299 shader->Source = load_text_file(whole_program, argv[optind]);
300 if (shader->Source == NULL) {
301 printf("File \"%s\" does not exist.\n", argv[optind]);
302 exit(EXIT_FAILURE);
303 }
304
305 compile_shader(ctx, shader);
306
307 if (!shader->CompileStatus) {
308 printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog);
309 status = EXIT_FAILURE;
310 break;
311 }
312 }
313
314 if ((status == EXIT_SUCCESS) && do_link) {
315 link_shaders(ctx, whole_program);
316 status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
317
318 if (strlen(whole_program->InfoLog) > 0)
319 printf("Info log for linking:\n%s\n", whole_program->InfoLog);
320 }
321
322 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++)
323 talloc_free(whole_program->_LinkedShaders[i]);
324
325 talloc_free(whole_program);
326 _mesa_glsl_release_types();
327 _mesa_glsl_release_functions();
328
329 return status;
330 }