2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul, Tungsten Graphics, Inc.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions 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 MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * Stand-alone Shading Language compiler.
30 * Basically, a command-line program which accepts GLSL shaders and emits
31 * vertex/fragment programs (GPU instructions).
33 * This file is basically just a Mesa device driver but instead of building
34 * a shared library we build an executable.
36 * We can emit programs in three different formats:
37 * 1. ARB-style (GL_ARB_vertex/fragment_program)
38 * 2. NV-style (GL_NV_vertex/fragment_program)
39 * 3. debug-style (a slightly more sophisticated, internal format)
41 * Note that the ARB and NV program languages can't express all the
42 * features that might be used by a fragment program (examples being
43 * uniform and varying vars). So, the ARB/NV programs that are
44 * emitted aren't always legal programs in those languages.
48 #include "main/imports.h"
49 #include "main/context.h"
50 #include "main/extensions.h"
51 #include "main/framebuffer.h"
52 #include "main/shaders.h"
53 #include "shader/shader_api.h"
54 #include "shader/prog_print.h"
55 #include "drivers/common/driverfuncs.h"
57 #include "tnl/t_context.h"
58 #include "tnl/t_pipeline.h"
59 #include "swrast/swrast.h"
60 #include "swrast/s_context.h"
61 #include "swrast/s_triangle.h"
62 #include "swrast_setup/swrast_setup.h"
66 static const char *Prog
= "glslcompiler";
70 GLboolean LineNumbers
;
71 gl_prog_print_mode Mode
;
74 const char *OutputFile
;
76 struct gl_sl_pragmas Pragmas
;
79 static struct options Options
;
83 * GLSL compiler driver context. (kind of an artificial thing for now)
85 struct compiler_context
87 GLcontext MesaContext
;
91 typedef struct compiler_context CompilerContext
;
96 UpdateState(GLcontext
*ctx
, GLuint new_state
)
98 /* easy - just propogate */
99 _swrast_InvalidateState( ctx
, new_state
);
100 _swsetup_InvalidateState( ctx
, new_state
);
101 _tnl_InvalidateState( ctx
, new_state
);
102 _vbo_InvalidateState( ctx
, new_state
);
110 struct dd_function_table ddFuncs
;
116 vis
= _mesa_create_visual(GL_TRUE
, GL_FALSE
, GL_FALSE
, /* RGB */
117 8, 8, 8, 8, /* color */
118 0, 0, 0, /* z, stencil */
119 0, 0, 0, 0, 1); /* accum */
120 buf
= _mesa_create_framebuffer(vis
);
122 cc
= _mesa_calloc(sizeof(*cc
));
123 if (!vis
|| !buf
|| !cc
) {
125 _mesa_destroy_visual(vis
);
127 _mesa_destroy_framebuffer(buf
);
131 _mesa_init_driver_functions(&ddFuncs
);
132 ddFuncs
.GetString
= NULL
;/*get_string;*/
133 ddFuncs
.UpdateState
= UpdateState
;
134 ddFuncs
.GetBufferSize
= NULL
;
136 ctx
= &cc
->MesaContext
;
137 _mesa_initialize_context(ctx
, vis
, NULL
, &ddFuncs
, cc
);
138 _mesa_enable_sw_extensions(ctx
);
140 if (!_swrast_CreateContext( ctx
) ||
141 !_vbo_CreateContext( ctx
) ||
142 !_tnl_CreateContext( ctx
) ||
143 !_swsetup_CreateContext( ctx
)) {
144 _mesa_destroy_visual(vis
);
145 _mesa_free_context_data(ctx
);
149 TNL_CONTEXT(ctx
)->Driver
.RunPipeline
= _tnl_run_pipeline
;
150 _swsetup_Wakeup( ctx
);
152 /* Override the context's default pragma settings */
153 ctx
->Shader
.DefaultPragmas
= Options
.Pragmas
;
155 _mesa_make_current(ctx
, buf
, buf
);
162 LoadAndCompileShader(GLuint shader
, const char *text
)
165 _mesa_ShaderSourceARB(shader
, 1, (const GLchar
**) &text
, NULL
);
166 _mesa_CompileShaderARB(shader
);
167 _mesa_GetShaderiv(shader
, GL_COMPILE_STATUS
, &stat
);
171 _mesa_GetShaderInfoLog(shader
, 1000, &len
, log
);
172 fprintf(stderr
, "%s: problem compiling shader: %s\n", Prog
, log
);
176 printf("Shader compiled OK\n");
182 * Read a shader from a file.
185 ReadShader(GLuint shader
, const char *filename
)
187 const int max
= 100*1000;
189 char *buffer
= (char*) malloc(max
);
190 FILE *f
= fopen(filename
, "r");
192 fprintf(stderr
, "%s: Unable to open shader file %s\n", Prog
, filename
);
196 n
= fread(buffer
, 1, max
, f
);
198 printf("%s: read %d bytes from shader file %s\n", Prog, n, filename);
202 LoadAndCompileShader(shader
, buffer
);
212 CheckLink(GLuint prog
)
215 _mesa_GetProgramiv(prog
, GL_LINK_STATUS
, &stat
);
219 _mesa_GetProgramInfoLog(prog
, 1000, &len
, log
);
220 fprintf(stderr
, "%s: Linker error:\n%s\n", Prog
, log
);
223 fprintf(stderr
, "%s: Link success!\n", Prog
);
230 PrintShaderInstructions(GLuint shader
, FILE *f
)
232 GET_CURRENT_CONTEXT(ctx
);
233 struct gl_shader
*sh
= _mesa_lookup_shader(ctx
, shader
);
234 struct gl_program
*prog
= sh
->Program
;
235 _mesa_fprint_program_opt(stdout
, prog
, Options
.Mode
, Options
.LineNumbers
);
237 _mesa_print_program_parameters(ctx
, prog
);
242 CompileShader(const char *filename
, GLenum type
)
246 assert(type
== GL_FRAGMENT_SHADER
||
247 type
== GL_VERTEX_SHADER
);
249 shader
= _mesa_CreateShader(type
);
250 ReadShader(shader
, filename
);
259 printf("Mesa GLSL stand-alone compiler\n");
261 printf(" --vs FILE vertex shader input filename\n");
262 printf(" --fs FILE fragment shader input filename\n");
263 printf(" --arb emit ARB-style instructions\n");
264 printf(" --nv emit NV-style instructions\n");
265 printf(" --debug force #pragma debug(on)\n");
266 printf(" --nodebug force #pragma debug(off)\n");
267 printf(" --opt force #pragma optimize(on)\n");
268 printf(" --noopt force #pragma optimize(off)\n");
269 printf(" --number, -n emit line numbers (if --arb or --nv)\n");
270 printf(" --output, -o FILE output filename\n");
271 printf(" --params also emit program parameter info\n");
272 printf(" --help display this information\n");
277 ParseOptions(int argc
, char *argv
[])
281 Options
.LineNumbers
= GL_FALSE
;
282 Options
.Mode
= PROG_PRINT_DEBUG
;
283 Options
.VertFile
= NULL
;
284 Options
.FragFile
= NULL
;
285 Options
.OutputFile
= NULL
;
286 Options
.Params
= GL_FALSE
;
287 Options
.Pragmas
.IgnoreOptimize
= GL_FALSE
;
288 Options
.Pragmas
.IgnoreDebug
= GL_FALSE
;
289 Options
.Pragmas
.Debug
= GL_FALSE
;
290 Options
.Pragmas
.Optimize
= GL_TRUE
;
297 for (i
= 1; i
< argc
; i
++) {
298 if (strcmp(argv
[i
], "--vs") == 0) {
299 Options
.VertFile
= argv
[i
+ 1];
302 else if (strcmp(argv
[i
], "--fs") == 0) {
303 Options
.FragFile
= argv
[i
+ 1];
306 else if (strcmp(argv
[i
], "--arb") == 0) {
307 Options
.Mode
= PROG_PRINT_ARB
;
309 else if (strcmp(argv
[i
], "--nv") == 0) {
310 Options
.Mode
= PROG_PRINT_NV
;
312 else if (strcmp(argv
[i
], "--debug") == 0) {
313 Options
.Pragmas
.IgnoreDebug
= GL_TRUE
;
314 Options
.Pragmas
.Debug
= GL_TRUE
;
316 else if (strcmp(argv
[i
], "--nodebug") == 0) {
317 Options
.Pragmas
.IgnoreDebug
= GL_TRUE
;
318 Options
.Pragmas
.Debug
= GL_FALSE
;
320 else if (strcmp(argv
[i
], "--opt") == 0) {
321 Options
.Pragmas
.IgnoreOptimize
= GL_TRUE
;
322 Options
.Pragmas
.Optimize
= GL_TRUE
;
324 else if (strcmp(argv
[i
], "--noopt") == 0) {
325 Options
.Pragmas
.IgnoreOptimize
= GL_TRUE
;
326 Options
.Pragmas
.Optimize
= GL_FALSE
;
328 else if (strcmp(argv
[i
], "--number") == 0 ||
329 strcmp(argv
[i
], "-n") == 0) {
330 Options
.LineNumbers
= GL_TRUE
;
332 else if (strcmp(argv
[i
], "--output") == 0 ||
333 strcmp(argv
[i
], "-o") == 0) {
334 Options
.OutputFile
= argv
[i
+ 1];
337 else if (strcmp(argv
[i
], "--params") == 0) {
338 Options
.Params
= GL_TRUE
;
340 else if (strcmp(argv
[i
], "--help") == 0) {
345 printf("Unknown option: %s\n", argv
[i
]);
351 if (Options
.Mode
== PROG_PRINT_DEBUG
) {
352 /* always print line numbers when emitting debug-style output */
353 Options
.LineNumbers
= GL_TRUE
;
359 main(int argc
, char *argv
[])
363 ParseOptions(argc
, argv
);
365 if (!CreateContext()) {
366 fprintf(stderr
, "%s: Failed to create compiler context\n", Prog
);
370 if (Options
.VertFile
) {
371 shader
= CompileShader(Options
.VertFile
, GL_VERTEX_SHADER
);
373 else if (Options
.FragFile
) {
374 shader
= CompileShader(Options
.FragFile
, GL_FRAGMENT_SHADER
);
378 if (Options
.OutputFile
) {
380 /*stdout =*/ freopen(Options
.OutputFile
, "w", stdout
);
383 PrintShaderInstructions(shader
, stdout
);
385 if (Options
.OutputFile
) {