nv50,nvc0: buffer resources can be bound as other things down the line
[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 <getopt.h>
24
25 /** @file main.cpp
26 *
27 * This file is the main() routine and scaffolding for producing
28 * builtin_compiler (which doesn't include builtins itself and is used
29 * to generate the profile information for builtin_function.cpp), and
30 * for glsl_compiler (which does include builtins and can be used to
31 * offline compile GLSL code and examine the resulting GLSL IR.
32 */
33
34 #include "ast.h"
35 #include "glsl_parser_extras.h"
36 #include "ir_optimization.h"
37 #include "program.h"
38 #include "program/hash_table.h"
39 #include "loop_analysis.h"
40 #include "standalone_scaffolding.h"
41
42 static int glsl_version = 330;
43
44 extern "C" void
45 _mesa_error_no_memory(const char *caller)
46 {
47 fprintf(stderr, "Mesa error: out of memory in %s", caller);
48 }
49
50 static void
51 initialize_context(struct gl_context *ctx, gl_api api)
52 {
53 initialize_context_to_defaults(ctx, api);
54
55 /* The standalone compiler needs to claim support for almost
56 * everything in order to compile the built-in functions.
57 */
58 ctx->Const.GLSLVersion = glsl_version;
59 ctx->Extensions.ARB_ES3_compatibility = true;
60 ctx->Const.MaxComputeWorkGroupCount[0] = 65535;
61 ctx->Const.MaxComputeWorkGroupCount[1] = 65535;
62 ctx->Const.MaxComputeWorkGroupCount[2] = 65535;
63 ctx->Const.MaxComputeWorkGroupSize[0] = 1024;
64 ctx->Const.MaxComputeWorkGroupSize[1] = 1024;
65 ctx->Const.MaxComputeWorkGroupSize[2] = 64;
66 ctx->Const.MaxComputeWorkGroupInvocations = 1024;
67 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits = 16;
68 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxUniformComponents = 1024;
69 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxInputComponents = 0; /* not used */
70 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxOutputComponents = 0; /* not used */
71
72 switch (ctx->Const.GLSLVersion) {
73 case 100:
74 ctx->Const.MaxClipPlanes = 0;
75 ctx->Const.MaxCombinedTextureImageUnits = 8;
76 ctx->Const.MaxDrawBuffers = 2;
77 ctx->Const.MinProgramTexelOffset = 0;
78 ctx->Const.MaxProgramTexelOffset = 0;
79 ctx->Const.MaxLights = 0;
80 ctx->Const.MaxTextureCoordUnits = 0;
81 ctx->Const.MaxTextureUnits = 8;
82
83 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 8;
84 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 0;
85 ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 128 * 4;
86 ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
87 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 32;
88
89 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits =
90 ctx->Const.MaxCombinedTextureImageUnits;
91 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 16 * 4;
92 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
93 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
94 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
95
96 ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4;
97 break;
98 case 110:
99 case 120:
100 ctx->Const.MaxClipPlanes = 6;
101 ctx->Const.MaxCombinedTextureImageUnits = 2;
102 ctx->Const.MaxDrawBuffers = 1;
103 ctx->Const.MinProgramTexelOffset = 0;
104 ctx->Const.MaxProgramTexelOffset = 0;
105 ctx->Const.MaxLights = 8;
106 ctx->Const.MaxTextureCoordUnits = 2;
107 ctx->Const.MaxTextureUnits = 2;
108
109 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
110 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 0;
111 ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 512;
112 ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
113 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 32;
114
115 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits =
116 ctx->Const.MaxCombinedTextureImageUnits;
117 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 64;
118 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
119 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
120 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
121
122 ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4;
123 break;
124 case 130:
125 case 140:
126 ctx->Const.MaxClipPlanes = 8;
127 ctx->Const.MaxCombinedTextureImageUnits = 16;
128 ctx->Const.MaxDrawBuffers = 8;
129 ctx->Const.MinProgramTexelOffset = -8;
130 ctx->Const.MaxProgramTexelOffset = 7;
131 ctx->Const.MaxLights = 8;
132 ctx->Const.MaxTextureCoordUnits = 8;
133 ctx->Const.MaxTextureUnits = 2;
134
135 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
136 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16;
137 ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024;
138 ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
139 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 64;
140
141 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16;
142 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 1024;
143 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
144 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
145 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
146
147 ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4;
148 break;
149 case 150:
150 case 330:
151 ctx->Const.MaxClipPlanes = 8;
152 ctx->Const.MaxDrawBuffers = 8;
153 ctx->Const.MinProgramTexelOffset = -8;
154 ctx->Const.MaxProgramTexelOffset = 7;
155 ctx->Const.MaxLights = 8;
156 ctx->Const.MaxTextureCoordUnits = 8;
157 ctx->Const.MaxTextureUnits = 2;
158
159 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
160 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16;
161 ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024;
162 ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
163 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 64;
164
165 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits = 16;
166 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxUniformComponents = 1024;
167 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxInputComponents =
168 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
169 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents = 128;
170
171 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16;
172 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 1024;
173 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
174 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents;
175 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
176
177 ctx->Const.MaxCombinedTextureImageUnits =
178 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits
179 + ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits
180 + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits;
181
182 ctx->Const.MaxGeometryOutputVertices = 256;
183 ctx->Const.MaxGeometryTotalOutputComponents = 1024;
184
185 // ctx->Const.MaxGeometryVaryingComponents = 64;
186
187 ctx->Const.MaxVarying = 60 / 4;
188 break;
189 case 300:
190 ctx->Const.MaxClipPlanes = 8;
191 ctx->Const.MaxCombinedTextureImageUnits = 32;
192 ctx->Const.MaxDrawBuffers = 4;
193 ctx->Const.MinProgramTexelOffset = -8;
194 ctx->Const.MaxProgramTexelOffset = 7;
195 ctx->Const.MaxLights = 0;
196 ctx->Const.MaxTextureCoordUnits = 0;
197 ctx->Const.MaxTextureUnits = 0;
198
199 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
200 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16;
201 ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024;
202 ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
203 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 16 * 4;
204
205 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16;
206 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 224;
207 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = 15 * 4;
208 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
209
210 ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents / 4;
211 break;
212 }
213
214 ctx->Const.GenerateTemporaryNames = true;
215 ctx->Driver.NewShader = _mesa_new_shader;
216 }
217
218 /* Returned string will have 'ctx' as its ralloc owner. */
219 static char *
220 load_text_file(void *ctx, const char *file_name)
221 {
222 char *text = NULL;
223 size_t size;
224 size_t total_read = 0;
225 FILE *fp = fopen(file_name, "rb");
226
227 if (!fp) {
228 return NULL;
229 }
230
231 fseek(fp, 0L, SEEK_END);
232 size = ftell(fp);
233 fseek(fp, 0L, SEEK_SET);
234
235 text = (char *) ralloc_size(ctx, size + 1);
236 if (text != NULL) {
237 do {
238 size_t bytes = fread(text + total_read,
239 1, size - total_read, fp);
240 if (bytes < size - total_read) {
241 free(text);
242 text = NULL;
243 goto error;
244 }
245
246 if (bytes == 0) {
247 break;
248 }
249
250 total_read += bytes;
251 } while (total_read < size);
252
253 text[total_read] = '\0';
254 error:;
255 }
256
257 fclose(fp);
258
259 return text;
260 }
261
262 int dump_ast = 0;
263 int dump_hir = 0;
264 int dump_lir = 0;
265 int do_link = 0;
266
267 const struct option compiler_opts[] = {
268 { "dump-ast", no_argument, &dump_ast, 1 },
269 { "dump-hir", no_argument, &dump_hir, 1 },
270 { "dump-lir", no_argument, &dump_lir, 1 },
271 { "link", no_argument, &do_link, 1 },
272 { "version", required_argument, NULL, 'v' },
273 { NULL, 0, NULL, 0 }
274 };
275
276 /**
277 * \brief Print proper usage and exit with failure.
278 */
279 void
280 usage_fail(const char *name)
281 {
282
283 const char *header =
284 "usage: %s [options] <file.vert | file.geom | file.frag>\n"
285 "\n"
286 "Possible options are:\n";
287 printf(header, name, name);
288 for (const struct option *o = compiler_opts; o->name != 0; ++o) {
289 printf(" --%s\n", o->name);
290 }
291 exit(EXIT_FAILURE);
292 }
293
294
295 void
296 compile_shader(struct gl_context *ctx, struct gl_shader *shader)
297 {
298 struct _mesa_glsl_parse_state *state =
299 new(shader) _mesa_glsl_parse_state(ctx, shader->Stage, shader);
300
301 _mesa_glsl_compile_shader(ctx, shader, dump_ast, dump_hir);
302
303 /* Print out the resulting IR */
304 if (!state->error && dump_lir) {
305 _mesa_print_ir(stdout, shader->ir, state);
306 }
307
308 return;
309 }
310
311 int
312 main(int argc, char **argv)
313 {
314 int status = EXIT_SUCCESS;
315 struct gl_context local_ctx;
316 struct gl_context *ctx = &local_ctx;
317 bool glsl_es = false;
318
319 int c;
320 int idx = 0;
321 while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1) {
322 switch (c) {
323 case 'v':
324 glsl_version = strtol(optarg, NULL, 10);
325 switch (glsl_version) {
326 case 100:
327 case 300:
328 glsl_es = true;
329 break;
330 case 110:
331 case 120:
332 case 130:
333 case 140:
334 case 150:
335 case 330:
336 glsl_es = false;
337 break;
338 default:
339 fprintf(stderr, "Unrecognized GLSL version `%s'\n", optarg);
340 usage_fail(argv[0]);
341 break;
342 }
343 break;
344 default:
345 break;
346 }
347 }
348
349
350 if (argc <= optind)
351 usage_fail(argv[0]);
352
353 initialize_context(ctx, (glsl_es) ? API_OPENGLES2 : API_OPENGL_COMPAT);
354
355 struct gl_shader_program *whole_program;
356
357 whole_program = rzalloc (NULL, struct gl_shader_program);
358 assert(whole_program != NULL);
359 whole_program->InfoLog = ralloc_strdup(whole_program, "");
360
361 /* Created just to avoid segmentation faults */
362 whole_program->AttributeBindings = new string_to_uint_map;
363 whole_program->FragDataBindings = new string_to_uint_map;
364 whole_program->FragDataIndexBindings = new string_to_uint_map;
365
366 for (/* empty */; argc > optind; optind++) {
367 whole_program->Shaders =
368 reralloc(whole_program, whole_program->Shaders,
369 struct gl_shader *, whole_program->NumShaders + 1);
370 assert(whole_program->Shaders != NULL);
371
372 struct gl_shader *shader = rzalloc(whole_program, gl_shader);
373
374 whole_program->Shaders[whole_program->NumShaders] = shader;
375 whole_program->NumShaders++;
376
377 const unsigned len = strlen(argv[optind]);
378 if (len < 6)
379 usage_fail(argv[0]);
380
381 const char *const ext = & argv[optind][len - 5];
382 if (strncmp(".vert", ext, 5) == 0 || strncmp(".glsl", ext, 5) == 0)
383 shader->Type = GL_VERTEX_SHADER;
384 else if (strncmp(".geom", ext, 5) == 0)
385 shader->Type = GL_GEOMETRY_SHADER;
386 else if (strncmp(".frag", ext, 5) == 0)
387 shader->Type = GL_FRAGMENT_SHADER;
388 else if (strncmp(".comp", ext, 5) == 0)
389 shader->Type = GL_COMPUTE_SHADER;
390 else
391 usage_fail(argv[0]);
392 shader->Stage = _mesa_shader_enum_to_shader_stage(shader->Type);
393
394 shader->Source = load_text_file(whole_program, argv[optind]);
395 if (shader->Source == NULL) {
396 printf("File \"%s\" does not exist.\n", argv[optind]);
397 exit(EXIT_FAILURE);
398 }
399
400 compile_shader(ctx, shader);
401
402 if (strlen(shader->InfoLog) > 0)
403 printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog);
404
405 if (!shader->CompileStatus) {
406 status = EXIT_FAILURE;
407 break;
408 }
409 }
410
411 if ((status == EXIT_SUCCESS) && do_link) {
412 _mesa_clear_shader_program_data(whole_program);
413
414 link_shaders(ctx, whole_program);
415 status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
416
417 if (strlen(whole_program->InfoLog) > 0)
418 printf("Info log for linking:\n%s\n", whole_program->InfoLog);
419 }
420
421 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++)
422 ralloc_free(whole_program->_LinkedShaders[i]);
423
424 delete whole_program->AttributeBindings;
425 delete whole_program->FragDataBindings;
426 delete whole_program->FragDataIndexBindings;
427
428 ralloc_free(whole_program);
429 _mesa_glsl_release_types();
430 _mesa_glsl_release_builtin_functions();
431
432 return status;
433 }