mesa: rename src/mesa/shader/ to src/mesa/program/
[mesa.git] / src / mesa / drivers / glslcompiler / glslcompiler.c
index 34cce977c8646daff9948884e4366a6ed4dd42f3..5166600bed84741a0500fd0f176f1f9d68d0720b 100644 (file)
@@ -50,8 +50,8 @@
 #include "main/extensions.h"
 #include "main/framebuffer.h"
 #include "main/shaders.h"
-#include "shader/shader_api.h"
-#include "shader/prog_print.h"
+#include "program/shader_api.h"
+#include "program/prog_print.h"
 #include "drivers/common/driverfuncs.h"
 #include "tnl/tnl.h"
 #include "tnl/t_context.h"
@@ -68,10 +68,13 @@ static const char *Prog = "glslcompiler";
 
 struct options {
    GLboolean LineNumbers;
+   GLboolean Link;
    gl_prog_print_mode Mode;
    const char *VertFile;
    const char *FragFile;
    const char *OutputFile;
+   GLboolean Params;
+   struct gl_sl_pragmas Pragmas;
 };
 
 static struct options Options;
@@ -111,13 +114,13 @@ CreateContext(void)
    GLcontext *ctx;
    CompilerContext *cc;
 
-   vis = _mesa_create_visual(GL_TRUE, GL_FALSE, GL_FALSE, /* RGB */
+   vis = _mesa_create_visual(GL_FALSE, GL_FALSE, /* RGB */
                              8, 8, 8, 8,  /* color */
-                             0, 0, 0,  /* z, stencil */
+                             0, 0,  /* z, stencil */
                              0, 0, 0, 0, 1);  /* accum */
    buf = _mesa_create_framebuffer(vis);
 
-   cc = _mesa_calloc(sizeof(*cc));
+   cc = calloc(1, sizeof(*cc));
    if (!vis || !buf || !cc) {
       if (vis)
          _mesa_destroy_visual(vis);
@@ -141,12 +144,15 @@ CreateContext(void)
        !_swsetup_CreateContext( ctx )) {
       _mesa_destroy_visual(vis);
       _mesa_free_context_data(ctx);
-      _mesa_free(cc);
+      free(cc);
       return GL_FALSE;
    }
    TNL_CONTEXT(ctx)->Driver.RunPipeline = _tnl_run_pipeline;
    _swsetup_Wakeup( ctx );
 
+   /* Override the context's default pragma settings */
+   ctx->Shader.DefaultPragmas = Options.Pragmas;
+
    _mesa_make_current(ctx, buf, buf);
 
    return GL_TRUE;
@@ -202,23 +208,29 @@ ReadShader(GLuint shader, const char *filename)
 }
 
 
-#if 0
 static void
-CheckLink(GLuint prog)
+CheckLink(GLuint v_shader, GLuint f_shader)
 {
+   GLuint prog;
    GLint stat;
+
+   prog = _mesa_CreateProgram();
+
+   _mesa_AttachShader(prog, v_shader);
+   _mesa_AttachShader(prog, f_shader);
+
+   _mesa_LinkProgramARB(prog);
    _mesa_GetProgramiv(prog, GL_LINK_STATUS, &stat);
    if (!stat) {
       GLchar log[1000];
       GLsizei len;
       _mesa_GetProgramInfoLog(prog, 1000, &len, log);
-      fprintf(stderr, "%s: Linker error:\n%s\n", Prog, log);
+      fprintf(stderr, "Linker error:\n%s\n", log);
    }
    else {
-      fprintf(stderr, "%s: Link success!\n", Prog);
+      fprintf(stderr, "Link success!\n");
    }
 }
-#endif
 
 
 static void
@@ -227,7 +239,9 @@ PrintShaderInstructions(GLuint shader, FILE *f)
    GET_CURRENT_CONTEXT(ctx);
    struct gl_shader *sh = _mesa_lookup_shader(ctx, shader);
    struct gl_program *prog = sh->Program;
-   _mesa_print_program_opt(prog, Options.Mode, Options.LineNumbers);
+   _mesa_fprint_program_opt(stdout, prog, Options.Mode, Options.LineNumbers);
+   if (Options.Params)
+      _mesa_print_program_parameters(ctx, prog);
 }
 
 
@@ -253,11 +267,16 @@ Usage(void)
    printf("Usage:\n");
    printf("  --vs FILE          vertex shader input filename\n");
    printf("  --fs FILE          fragment shader input filename\n");
-   printf("  --arb              emit ARB-style instructions (the default)\n");
+   printf("  --arb              emit ARB-style instructions\n");
    printf("  --nv               emit NV-style instructions\n");
-   printf("  --debug            emit debug-style instructions\n");
-   printf("  --number, -n       emit line numbers\n");
+   printf("  --link             run linker\n");
+   printf("  --debug            force #pragma debug(on)\n");
+   printf("  --nodebug          force #pragma debug(off)\n");
+   printf("  --opt              force #pragma optimize(on)\n");
+   printf("  --noopt            force #pragma optimize(off)\n");
+   printf("  --number, -n       emit line numbers (if --arb or --nv)\n");
    printf("  --output, -o FILE  output filename\n");
+   printf("  --params           also emit program parameter info\n");
    printf("  --help             display this information\n");
 }
 
@@ -268,10 +287,15 @@ ParseOptions(int argc, char *argv[])
    int i;
 
    Options.LineNumbers = GL_FALSE;
-   Options.Mode = PROG_PRINT_ARB;
+   Options.Mode = PROG_PRINT_DEBUG;
    Options.VertFile = NULL;
    Options.FragFile = NULL;
    Options.OutputFile = NULL;
+   Options.Params = GL_FALSE;
+   Options.Pragmas.IgnoreOptimize = GL_FALSE;
+   Options.Pragmas.IgnoreDebug = GL_FALSE;
+   Options.Pragmas.Debug = GL_FALSE;
+   Options.Pragmas.Optimize = GL_TRUE;
 
    if (argc == 1) {
       Usage();
@@ -293,8 +317,24 @@ ParseOptions(int argc, char *argv[])
       else if (strcmp(argv[i], "--nv") == 0) {
          Options.Mode = PROG_PRINT_NV;
       }
+      else if (strcmp(argv[i], "--link") == 0) {
+         Options.Link = GL_TRUE;
+      }
       else if (strcmp(argv[i], "--debug") == 0) {
-         Options.Mode = PROG_PRINT_DEBUG;
+         Options.Pragmas.IgnoreDebug = GL_TRUE;
+         Options.Pragmas.Debug = GL_TRUE;
+      }
+      else if (strcmp(argv[i], "--nodebug") == 0) {
+         Options.Pragmas.IgnoreDebug = GL_TRUE;
+         Options.Pragmas.Debug = GL_FALSE;
+      }
+      else if (strcmp(argv[i], "--opt") == 0) {
+         Options.Pragmas.IgnoreOptimize = GL_TRUE;
+         Options.Pragmas.Optimize = GL_TRUE;
+      }
+      else if (strcmp(argv[i], "--noopt") == 0) {
+         Options.Pragmas.IgnoreOptimize = GL_TRUE;
+         Options.Pragmas.Optimize = GL_FALSE;
       }
       else if (strcmp(argv[i], "--number") == 0 ||
                strcmp(argv[i], "-n") == 0) {
@@ -305,6 +345,9 @@ ParseOptions(int argc, char *argv[])
          Options.OutputFile = argv[i + 1];
          i++;
       }
+      else if (strcmp(argv[i], "--params") == 0) {
+         Options.Params = GL_TRUE;
+      }
       else if (strcmp(argv[i], "--help") == 0) {
          Usage();
          exit(0);
@@ -315,40 +358,59 @@ ParseOptions(int argc, char *argv[])
          exit(1);
       }
    }
+
+   if (Options.Mode == PROG_PRINT_DEBUG) {
+      /* always print line numbers when emitting debug-style output */
+      Options.LineNumbers = GL_TRUE;
+   }
 }
 
 
 int
 main(int argc, char *argv[])
 {
-   GLuint shader = 0;
+   GLuint v_shader = 0, f_shader = 0;
+
+   ParseOptions(argc, argv);
 
    if (!CreateContext()) {
       fprintf(stderr, "%s: Failed to create compiler context\n", Prog);
       exit(1);
    }
 
-   ParseOptions(argc, argv);
-
    if (Options.VertFile) {
-      shader = CompileShader(Options.VertFile, GL_VERTEX_SHADER);
+      v_shader = CompileShader(Options.VertFile, GL_VERTEX_SHADER);
    }
-   else if (Options.FragFile) {
-      shader = CompileShader(Options.FragFile, GL_FRAGMENT_SHADER);
+
+   if (Options.FragFile) {
+      f_shader = CompileShader(Options.FragFile, GL_FRAGMENT_SHADER);
    }
 
-   if (shader) {
+   if (v_shader || f_shader) {
       if (Options.OutputFile) {
          fclose(stdout);
          /*stdout =*/ freopen(Options.OutputFile, "w", stdout);
       }
-      if (stdout) {
-         PrintShaderInstructions(shader, stdout);
+      if (stdout && v_shader) {
+         PrintShaderInstructions(v_shader, stdout);
+      }
+      if (stdout && f_shader) {
+         PrintShaderInstructions(f_shader, stdout);
       }
       if (Options.OutputFile) {
          fclose(stdout);
       }
    }
 
+   if (Options.Link) {
+      if (!v_shader || !f_shader) {
+         fprintf(stderr,
+                 "--link option requires both a vertex and fragment shader.\n");
+         exit(1);
+      }
+
+      CheckLink(v_shader, f_shader);
+   }
+
    return 0;
 }