Tizen 2.0 Release
[profile/ivi/osmesa.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 #include "ast.h"
26 #include "glsl_parser_extras.h"
27 #include "glsl_parser.h"
28 #include "ir_optimization.h"
29 #include "ir_print_visitor.h"
30 #include "program.h"
31 #include "loop_analysis.h"
32
33 extern "C" struct gl_shader *
34 _mesa_new_shader(struct gl_context *ctx, GLuint name, GLenum type);
35
36 extern "C" void
37 _mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr,
38                        struct gl_shader *sh);
39
40 /* Copied from shader_api.c for the stand-alone compiler.
41  */
42 void
43 _mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr,
44                        struct gl_shader *sh)
45 {
46    *ptr = sh;
47 }
48
49 struct gl_shader *
50 _mesa_new_shader(struct gl_context *ctx, GLuint name, GLenum type)
51 {
52    struct gl_shader *shader;
53
54    (void) ctx;
55
56    assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER);
57    shader = rzalloc(NULL, struct gl_shader);
58    if (shader) {
59       shader->Type = type;
60       shader->Name = name;
61       shader->RefCount = 1;
62    }
63    return shader;
64 }
65
66 static void
67 initialize_context(struct gl_context *ctx, gl_api api)
68 {
69    memset(ctx, 0, sizeof(*ctx));
70
71    ctx->API = api;
72
73    ctx->Extensions.ARB_ES2_compatibility = GL_TRUE;
74    ctx->Extensions.ARB_draw_buffers = GL_TRUE;
75    ctx->Extensions.ARB_draw_instanced = GL_TRUE;
76    ctx->Extensions.ARB_fragment_coord_conventions = GL_TRUE;
77    ctx->Extensions.EXT_texture_array = GL_TRUE;
78    ctx->Extensions.NV_texture_rectangle = GL_TRUE;
79    ctx->Extensions.EXT_texture3D = GL_TRUE;
80
81    /* GLSL 1.30 isn't fully supported, but we need to advertise 1.30 so that
82     * the built-in functions for 1.30 can be built.
83     */
84    ctx->Const.GLSLVersion = 130;
85
86    /* 1.10 minimums. */
87    ctx->Const.MaxLights = 8;
88    ctx->Const.MaxClipPlanes = 8;
89    ctx->Const.MaxTextureUnits = 2;
90
91    /* More than the 1.10 minimum to appease parser tests taken from
92     * apps that (hopefully) already checked the number of coords.
93     */
94    ctx->Const.MaxTextureCoordUnits = 4;
95
96    ctx->Const.VertexProgram.MaxAttribs = 16;
97    ctx->Const.VertexProgram.MaxUniformComponents = 512;
98    ctx->Const.MaxVarying = 8;
99    ctx->Const.MaxVertexTextureImageUnits = 0;
100    ctx->Const.MaxCombinedTextureImageUnits = 2;
101    ctx->Const.MaxTextureImageUnits = 2;
102    ctx->Const.FragmentProgram.MaxUniformComponents = 64;
103
104    ctx->Const.MaxDrawBuffers = 2;
105
106    ctx->Driver.NewShader = _mesa_new_shader;
107 }
108
109 /* Returned string will have 'ctx' as its ralloc owner. */
110 static char *
111 load_text_file(void *ctx, const char *file_name)
112 {
113         char *text = NULL;
114         size_t size;
115         size_t total_read = 0;
116         FILE *fp = fopen(file_name, "rb");
117
118         if (!fp) {
119                 return NULL;
120         }
121
122         fseek(fp, 0L, SEEK_END);
123         size = ftell(fp);
124         fseek(fp, 0L, SEEK_SET);
125
126         text = (char *) ralloc_size(ctx, size + 1);
127         if (text != NULL) {
128                 do {
129                         size_t bytes = fread(text + total_read,
130                                              1, size - total_read, fp);
131                         if (bytes < size - total_read) {
132                                 free(text);
133                                 text = NULL;
134                                 break;
135                         }
136
137                         if (bytes == 0) {
138                                 break;
139                         }
140
141                         total_read += bytes;
142                 } while (total_read < size);
143
144                 text[total_read] = '\0';
145         }
146
147         fclose(fp);
148
149         return text;
150 }
151
152 int glsl_es = 0;
153 int dump_ast = 0;
154 int dump_hir = 0;
155 int dump_lir = 0;
156 int do_link = 0;
157
158 const struct option compiler_opts[] = {
159    { "glsl-es",  0, &glsl_es,  1 },
160    { "dump-ast", 0, &dump_ast, 1 },
161    { "dump-hir", 0, &dump_hir, 1 },
162    { "dump-lir", 0, &dump_lir, 1 },
163    { "link",     0, &do_link,  1 },
164    { NULL, 0, NULL, 0 }
165 };
166
167 /**
168  * \brief Print proper usage and exit with failure.
169  */
170 void
171 usage_fail(const char *name)
172 {
173
174    const char *header =
175       "usage: %s [options] <file.vert | file.geom | file.frag>\n"
176       "\n"
177       "Possible options are:\n";
178    printf(header, name, name);
179    for (const struct option *o = compiler_opts; o->name != 0; ++o) {
180       printf("    --%s\n", o->name);
181    }
182    exit(EXIT_FAILURE);
183 }
184
185
186 void
187 compile_shader(struct gl_context *ctx, struct gl_shader *shader)
188 {
189    struct _mesa_glsl_parse_state *state =
190       new(shader) _mesa_glsl_parse_state(ctx, shader->Type, shader);
191
192    const char *source = shader->Source;
193    state->error = preprocess(state, &source, &state->info_log,
194                              state->extensions, ctx->API) != 0;
195
196    if (!state->error) {
197       _mesa_glsl_lexer_ctor(state, source);
198       _mesa_glsl_parse(state);
199       _mesa_glsl_lexer_dtor(state);
200    }
201
202    if (dump_ast) {
203       foreach_list_const(n, &state->translation_unit) {
204          ast_node *ast = exec_node_data(ast_node, n, link);
205          ast->print();
206       }
207       printf("\n\n");
208    }
209
210    shader->ir = new(shader) exec_list;
211    if (!state->error && !state->translation_unit.is_empty())
212       _mesa_ast_to_hir(shader->ir, state);
213
214    /* Print out the unoptimized IR. */
215    if (!state->error && dump_hir) {
216       validate_ir_tree(shader->ir);
217       _mesa_print_ir(shader->ir, state);
218    }
219
220    /* Optimization passes */
221    if (!state->error && !shader->ir->is_empty()) {
222       bool progress;
223       do {
224          progress = do_common_optimization(shader->ir, false, 32);
225       } while (progress);
226
227       validate_ir_tree(shader->ir);
228    }
229
230
231    /* Print out the resulting IR */
232    if (!state->error && dump_lir) {
233       _mesa_print_ir(shader->ir, state);
234    }
235
236    shader->symbols = state->symbols;
237    shader->CompileStatus = !state->error;
238    shader->Version = state->language_version;
239    memcpy(shader->builtins_to_link, state->builtins_to_link,
240           sizeof(shader->builtins_to_link[0]) * state->num_builtins_to_link);
241    shader->num_builtins_to_link = state->num_builtins_to_link;
242
243    if (shader->InfoLog)
244       ralloc_free(shader->InfoLog);
245
246    shader->InfoLog = state->info_log;
247
248    /* Retain any live IR, but trash the rest. */
249    reparent_ir(shader->ir, shader);
250
251    ralloc_free(state);
252
253    return;
254 }
255
256 int
257 main(int argc, char **argv)
258 {
259    int status = EXIT_SUCCESS;
260    struct gl_context local_ctx;
261    struct gl_context *ctx = &local_ctx;
262
263    int c;
264    int idx = 0;
265    while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1)
266       /* empty */ ;
267
268
269    if (argc <= optind)
270       usage_fail(argv[0]);
271
272    initialize_context(ctx, (glsl_es) ? API_OPENGLES2 : API_OPENGL);
273
274    struct gl_shader_program *whole_program;
275
276    whole_program = rzalloc (NULL, struct gl_shader_program);
277    assert(whole_program != NULL);
278    whole_program->InfoLog = ralloc_strdup(whole_program, "");
279
280    for (/* empty */; argc > optind; optind++) {
281       whole_program->Shaders =
282          reralloc(whole_program, whole_program->Shaders,
283                   struct gl_shader *, whole_program->NumShaders + 1);
284       assert(whole_program->Shaders != NULL);
285
286       struct gl_shader *shader = rzalloc(whole_program, gl_shader);
287
288       whole_program->Shaders[whole_program->NumShaders] = shader;
289       whole_program->NumShaders++;
290
291       const unsigned len = strlen(argv[optind]);
292       if (len < 6)
293          usage_fail(argv[0]);
294
295       const char *const ext = & argv[optind][len - 5];
296       if (strncmp(".vert", ext, 5) == 0)
297          shader->Type = GL_VERTEX_SHADER;
298       else if (strncmp(".geom", ext, 5) == 0)
299          shader->Type = GL_GEOMETRY_SHADER;
300       else if (strncmp(".frag", ext, 5) == 0)
301          shader->Type = GL_FRAGMENT_SHADER;
302       else
303          usage_fail(argv[0]);
304
305       shader->Source = load_text_file(whole_program, argv[optind]);
306       if (shader->Source == NULL) {
307          printf("File \"%s\" does not exist.\n", argv[optind]);
308          exit(EXIT_FAILURE);
309       }
310
311       compile_shader(ctx, shader);
312
313       if (!shader->CompileStatus) {
314          printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog);
315          status = EXIT_FAILURE;
316          break;
317       }
318    }
319
320    if ((status == EXIT_SUCCESS) && do_link)  {
321       link_shaders(ctx, whole_program);
322       status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
323
324       if (strlen(whole_program->InfoLog) > 0)
325          printf("Info log for linking:\n%s\n", whole_program->InfoLog);
326    }
327
328    for (unsigned i = 0; i < MESA_SHADER_TYPES; i++)
329       ralloc_free(whole_program->_LinkedShaders[i]);
330
331    ralloc_free(whole_program);
332    _mesa_glsl_release_types();
333    _mesa_glsl_release_functions();
334
335    return status;
336 }