Use C-style system headers in C++ code to avoid issues with std:: namespace
[mesa.git] / src / glsl / main.cpp
index 94c14a58a7bf5bfc877a57c1b552c9465c826379..096da93dcef7480e1ccaaff6dba8635be9c8698f 100644 (file)
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  * DEALINGS IN THE SOFTWARE.
  */
-#include <cstdlib>
-#include <cstdio>
 #include <getopt.h>
 
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <unistd.h>
-
 #include "ast.h"
 #include "glsl_parser_extras.h"
 #include "glsl_parser.h"
 #include "loop_analysis.h"
 
 extern "C" struct gl_shader *
-_mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type);
+_mesa_new_shader(struct gl_context *ctx, GLuint name, GLenum type);
+
+extern "C" void
+_mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr,
+                       struct gl_shader *sh);
 
 /* Copied from shader_api.c for the stand-alone compiler.
  */
+void
+_mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr,
+                       struct gl_shader *sh)
+{
+   *ptr = sh;
+}
+
 struct gl_shader *
-_mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type)
+_mesa_new_shader(struct gl_context *ctx, GLuint name, GLenum type)
 {
    struct gl_shader *shader;
 
    (void) ctx;
 
    assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER);
-   shader = talloc_zero(NULL, struct gl_shader);
+   shader = rzalloc(NULL, struct gl_shader);
    if (shader) {
       shader->Type = type;
       shader->Name = name;
@@ -60,17 +64,24 @@ _mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type)
 }
 
 static void
-initialize_context(GLcontext *ctx, gl_api api)
+initialize_context(struct gl_context *ctx, gl_api api)
 {
    memset(ctx, 0, sizeof(*ctx));
 
    ctx->API = api;
 
+   ctx->Extensions.ARB_ES2_compatibility = GL_TRUE;
    ctx->Extensions.ARB_draw_buffers = GL_TRUE;
+   ctx->Extensions.ARB_draw_instanced = GL_TRUE;
    ctx->Extensions.ARB_fragment_coord_conventions = GL_TRUE;
    ctx->Extensions.EXT_texture_array = GL_TRUE;
    ctx->Extensions.NV_texture_rectangle = GL_TRUE;
 
+   /* GLSL 1.30 isn't fully supported, but we need to advertise 1.30 so that
+    * the built-in functions for 1.30 can be built.
+    */
+   ctx->Const.GLSLVersion = 130;
+
    /* 1.10 minimums. */
    ctx->Const.MaxLights = 8;
    ctx->Const.MaxClipPlanes = 8;
@@ -94,56 +105,49 @@ initialize_context(GLcontext *ctx, gl_api api)
    ctx->Driver.NewShader = _mesa_new_shader;
 }
 
-/* Returned string will have 'ctx' as its talloc owner. */
+/* Returned string will have 'ctx' as its ralloc owner. */
 static char *
 load_text_file(void *ctx, const char *file_name)
 {
        char *text = NULL;
-       struct stat st;
-       ssize_t total_read = 0;
-       int fd = open(file_name, O_RDONLY);
+       size_t size;
+       size_t total_read = 0;
+       FILE *fp = fopen(file_name, "rb");
 
-       if (fd < 0) {
+       if (!fp) {
                return NULL;
        }
 
-       if (fstat(fd, & st) == 0) {
-          text = (char *) talloc_size(ctx, st.st_size + 1);
-               if (text != NULL) {
-                       do {
-                               ssize_t bytes = read(fd, text + total_read,
-                                                    st.st_size - total_read);
-                               if (bytes < 0) {
-                                       free(text);
-                                       text = NULL;
-                                       break;
-                               }
-
-                               if (bytes == 0) {
-                                       break;
-                               }
-
-                               total_read += bytes;
-                       } while (total_read < st.st_size);
-
-                       text[total_read] = '\0';
-               }
+       fseek(fp, 0L, SEEK_END);
+       size = ftell(fp);
+       fseek(fp, 0L, SEEK_SET);
+
+       text = (char *) ralloc_size(ctx, size + 1);
+       if (text != NULL) {
+               do {
+                       size_t bytes = fread(text + total_read,
+                                            1, size - total_read, fp);
+                       if (bytes < size - total_read) {
+                               free(text);
+                               text = NULL;
+                               break;
+                       }
+
+                       if (bytes == 0) {
+                               break;
+                       }
+
+                       total_read += bytes;
+               } while (total_read < size);
+
+               text[total_read] = '\0';
        }
 
-       close(fd);
+       fclose(fp);
 
        return text;
 }
 
-
-void
-usage_fail(const char *name)
-{
-      printf("%s <filename.frag|filename.vert>\n", name);
-      exit(EXIT_FAILURE);
-}
-
-
 int glsl_es = 0;
 int dump_ast = 0;
 int dump_hir = 0;
@@ -159,15 +163,34 @@ const struct option compiler_opts[] = {
    { NULL, 0, NULL, 0 }
 };
 
+/**
+ * \brief Print proper usage and exit with failure.
+ */
+void
+usage_fail(const char *name)
+{
+
+   const char *header =
+      "usage: %s [options] <file.vert | file.geom | file.frag>\n"
+      "\n"
+      "Possible options are:\n";
+   printf(header, name, name);
+   for (const struct option *o = compiler_opts; o->name != 0; ++o) {
+      printf("    --%s\n", o->name);
+   }
+   exit(EXIT_FAILURE);
+}
+
+
 void
-compile_shader(GLcontext *ctx, struct gl_shader *shader)
+compile_shader(struct gl_context *ctx, struct gl_shader *shader)
 {
    struct _mesa_glsl_parse_state *state =
       new(shader) _mesa_glsl_parse_state(ctx, shader->Type, shader);
 
    const char *source = shader->Source;
    state->error = preprocess(state, &source, &state->info_log,
-                            state->extensions, ctx->API);
+                            state->extensions, ctx->API) != 0;
 
    if (!state->error) {
       _mesa_glsl_lexer_ctor(state, source);
@@ -197,26 +220,7 @@ compile_shader(GLcontext *ctx, struct gl_shader *shader)
    if (!state->error && !shader->ir->is_empty()) {
       bool progress;
       do {
-        progress = false;
-
-        progress = do_function_inlining(shader->ir) || progress;
-        progress = do_if_simplification(shader->ir) || progress;
-        progress = do_copy_propagation(shader->ir) || progress;
-        progress = do_dead_code_local(shader->ir) || progress;
-        progress = do_dead_code_unlinked(shader->ir) || progress;
-        progress = do_tree_grafting(shader->ir) || progress;
-        progress = do_constant_propagation(shader->ir) || progress;
-        progress = do_constant_variable_unlinked(shader->ir) || progress;
-        progress = do_constant_folding(shader->ir) || progress;
-        progress = do_algebraic(shader->ir) || progress;
-        progress = do_vec_index_to_swizzle(shader->ir) || progress;
-        progress = do_vec_index_to_cond_assign(shader->ir) || progress;
-        progress = do_swizzle_swizzle(shader->ir) || progress;
-
-        loop_state *ls = analyze_loop_variables(shader->ir);
-        progress = set_loop_controls(shader->ir, ls) || progress;
-        progress = unroll_loops(shader->ir, ls, 32) || progress;
-        delete ls;
+        progress = do_common_optimization(shader->ir, false, 32);
       } while (progress);
 
       validate_ir_tree(shader->ir);
@@ -236,14 +240,14 @@ compile_shader(GLcontext *ctx, struct gl_shader *shader)
    shader->num_builtins_to_link = state->num_builtins_to_link;
 
    if (shader->InfoLog)
-      talloc_free(shader->InfoLog);
+      ralloc_free(shader->InfoLog);
 
    shader->InfoLog = state->info_log;
 
    /* Retain any live IR, but trash the rest. */
    reparent_ir(shader->ir, shader);
 
-   talloc_free(state);
+   ralloc_free(state);
 
    return;
 }
@@ -252,8 +256,8 @@ int
 main(int argc, char **argv)
 {
    int status = EXIT_SUCCESS;
-   GLcontext local_ctx;
-   GLcontext *ctx = &local_ctx;
+   struct gl_context local_ctx;
+   struct gl_context *ctx = &local_ctx;
 
    int c;
    int idx = 0;
@@ -268,16 +272,16 @@ main(int argc, char **argv)
 
    struct gl_shader_program *whole_program;
 
-   whole_program = talloc_zero (NULL, struct gl_shader_program);
+   whole_program = rzalloc (NULL, struct gl_shader_program);
    assert(whole_program != NULL);
 
    for (/* empty */; argc > optind; optind++) {
-      whole_program->Shaders = (struct gl_shader **)
-        talloc_realloc(whole_program, whole_program->Shaders,
-                       struct gl_shader *, whole_program->NumShaders + 1);
+      whole_program->Shaders =
+        reralloc(whole_program, whole_program->Shaders,
+                 struct gl_shader *, whole_program->NumShaders + 1);
       assert(whole_program->Shaders != NULL);
 
-      struct gl_shader *shader = talloc_zero(whole_program, gl_shader);
+      struct gl_shader *shader = rzalloc(whole_program, gl_shader);
 
       whole_program->Shaders[whole_program->NumShaders] = shader;
       whole_program->NumShaders++;
@@ -319,10 +323,10 @@ main(int argc, char **argv)
         printf("Info log for linking:\n%s\n", whole_program->InfoLog);
    }
 
-   for (unsigned i = 0; i < whole_program->_NumLinkedShaders; i++)
-      talloc_free(whole_program->_LinkedShaders[i]);
+   for (unsigned i = 0; i < MESA_SHADER_TYPES; i++)
+      ralloc_free(whole_program->_LinkedShaders[i]);
 
-   talloc_free(whole_program);
+   ralloc_free(whole_program);
    _mesa_glsl_release_types();
    _mesa_glsl_release_functions();