fix w component of glsl vec4 asin
[mesa.git] / src / mesa / shader / slang / slang_compile.c
index 1a4c7d31f1687ba87488c5eb737a6ddc244fb466..2be89a5ce05235c0915d1870cb5637fc6960b88d 100644 (file)
  * \author Michal Krol
  */
 
-#include "imports.h"
-#include "context.h"
-#include "program.h"
-#include "prog_parameter.h"
-#include "grammar_mesa.h"
+#include "main/imports.h"
+#include "main/context.h"
+#include "shader/program.h"
+#include "shader/prog_parameter.h"
+#include "shader/grammar/grammar_mesa.h"
 #include "slang_codegen.h"
 #include "slang_compile.h"
 #include "slang_preprocess.h"
 #include "slang_storage.h"
 #include "slang_emit.h"
 #include "slang_log.h"
+#include "slang_mem.h"
 #include "slang_vartable.h"
 #include "slang_simplify.h"
 
@@ -193,9 +194,9 @@ parse_float(slang_parse_ctx * C, float *number)
    parse_identifier_str(C, &fractional);
    parse_identifier_str(C, &exponent);
 
-   whole = (char *) (slang_alloc_malloc((_mesa_strlen(integral) +
-                                         _mesa_strlen(fractional) +
-                                         _mesa_strlen(exponent) + 3) * sizeof(char)));
+   whole = (char *) _slang_alloc((_mesa_strlen(integral) +
+                                  _mesa_strlen(fractional) +
+                                  _mesa_strlen(exponent) + 3) * sizeof(char));
    if (whole == NULL) {
       slang_info_log_memory(C->L);
       return 0;
@@ -209,7 +210,8 @@ parse_float(slang_parse_ctx * C, float *number)
 
    *number = (float) (_mesa_strtod(whole, (char **) NULL));
 
-   slang_alloc_free(whole);
+   _slang_free(whole);
+
    return 1;
 }
 
@@ -288,7 +290,7 @@ convert_to_array(slang_parse_ctx * C, slang_variable * var,
     * parse the expression */
    var->type.specifier.type = SLANG_SPEC_ARRAY;
    var->type.specifier._array = (slang_type_specifier *)
-      slang_alloc_malloc(sizeof(slang_type_specifier));
+      _slang_alloc(sizeof(slang_type_specifier));
    if (var->type.specifier._array == NULL) {
       slang_info_log_memory(C->L);
       return GL_FALSE;
@@ -371,13 +373,13 @@ parse_struct(slang_parse_ctx * C, slang_output_ctx * O, slang_struct ** st)
    }
 
    /* set-up a new struct */
-   *st = (slang_struct *) slang_alloc_malloc(sizeof(slang_struct));
+   *st = (slang_struct *) _slang_alloc(sizeof(slang_struct));
    if (*st == NULL) {
       slang_info_log_memory(C->L);
       return 0;
    }
    if (!slang_struct_construct(*st)) {
-      slang_alloc_free(*st);
+      _slang_free(*st);
       *st = NULL;
       slang_info_log_memory(C->L);
       return 0;
@@ -403,11 +405,11 @@ parse_struct(slang_parse_ctx * C, slang_output_ctx * O, slang_struct ** st)
       slang_struct *s;
 
       O->structs->structs =
-         (slang_struct *) slang_alloc_realloc(O->structs->structs,
-                                              O->structs->num_structs *
-                                              sizeof(slang_struct),
-                                              (O->structs->num_structs +
-                                               1) * sizeof(slang_struct));
+         (slang_struct *) _slang_realloc(O->structs->structs,
+                                         O->structs->num_structs
+                                         * sizeof(slang_struct),
+                                         (O->structs->num_structs + 1)
+                                         * sizeof(slang_struct));
       if (O->structs->structs == NULL) {
          slang_info_log_memory(C->L);
          return 0;
@@ -487,8 +489,17 @@ parse_type_qualifier(slang_parse_ctx * C, slang_type_qualifier * qual)
 #define TYPE_SPECIFIER_SAMPLERCUBE 19
 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
-#define TYPE_SPECIFIER_STRUCT 22
-#define TYPE_SPECIFIER_TYPENAME 23
+#define TYPE_SPECIFIER_SAMPLER2DRECT 22
+#define TYPE_SPECIFIER_SAMPLER2DRECTSHADOW 23
+#define TYPE_SPECIFIER_STRUCT 24
+#define TYPE_SPECIFIER_TYPENAME 25
+#define TYPE_SPECIFIER_MAT23 26
+#define TYPE_SPECIFIER_MAT32 27
+#define TYPE_SPECIFIER_MAT24 28
+#define TYPE_SPECIFIER_MAT42 29
+#define TYPE_SPECIFIER_MAT34 30
+#define TYPE_SPECIFIER_MAT43 31
+
 
 static int
 parse_type_specifier(slang_parse_ctx * C, slang_output_ctx * O,
@@ -543,6 +554,24 @@ parse_type_specifier(slang_parse_ctx * C, slang_output_ctx * O,
    case TYPE_SPECIFIER_MAT4:
       spec->type = SLANG_SPEC_MAT4;
       break;
+   case TYPE_SPECIFIER_MAT23:
+      spec->type = SLANG_SPEC_MAT23;
+      break;
+   case TYPE_SPECIFIER_MAT32:
+      spec->type = SLANG_SPEC_MAT32;
+      break;
+   case TYPE_SPECIFIER_MAT24:
+      spec->type = SLANG_SPEC_MAT24;
+      break;
+   case TYPE_SPECIFIER_MAT42:
+      spec->type = SLANG_SPEC_MAT42;
+      break;
+   case TYPE_SPECIFIER_MAT34:
+      spec->type = SLANG_SPEC_MAT34;
+      break;
+   case TYPE_SPECIFIER_MAT43:
+      spec->type = SLANG_SPEC_MAT43;
+      break;
    case TYPE_SPECIFIER_SAMPLER1D:
       spec->type = SLANG_SPEC_SAMPLER1D;
       break;
@@ -555,12 +584,18 @@ parse_type_specifier(slang_parse_ctx * C, slang_output_ctx * O,
    case TYPE_SPECIFIER_SAMPLERCUBE:
       spec->type = SLANG_SPEC_SAMPLERCUBE;
       break;
+   case TYPE_SPECIFIER_SAMPLER2DRECT:
+      spec->type = SLANG_SPEC_SAMPLER2DRECT;
+      break;
    case TYPE_SPECIFIER_SAMPLER1DSHADOW:
       spec->type = SLANG_SPEC_SAMPLER1DSHADOW;
       break;
    case TYPE_SPECIFIER_SAMPLER2DSHADOW:
       spec->type = SLANG_SPEC_SAMPLER2DSHADOW;
       break;
+   case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW:
+      spec->type = SLANG_SPEC_SAMPLER2DRECTSHADOW;
+      break;
    case TYPE_SPECIFIER_STRUCT:
       spec->type = SLANG_SPEC_STRUCT;
       if (!parse_struct(C, O, &spec->_struct))
@@ -583,14 +618,13 @@ parse_type_specifier(slang_parse_ctx * C, slang_output_ctx * O,
             return 0;
          }
 
-         spec->_struct =
-            (slang_struct *) slang_alloc_malloc(sizeof(slang_struct));
+         spec->_struct = (slang_struct *) _slang_alloc(sizeof(slang_struct));
          if (spec->_struct == NULL) {
             slang_info_log_memory(C->L);
             return 0;
          }
          if (!slang_struct_construct(spec->_struct)) {
-            slang_alloc_free(spec->_struct);
+            _slang_free(spec->_struct);
             spec->_struct = NULL;
             return 0;
          }
@@ -683,7 +717,7 @@ parse_fully_specified_type(slang_parse_ctx * C, slang_output_ctx * O,
 /**
  * When parsing a compound production, this function is used to parse the
  * children.
- * For example, a while-loop compound will have two children, the
+ * For example, a while-loop compound will have two children, the
  * while condition expression and the loop body.  So, this function will
  * be called twice to parse those two sub-expressions.
  * \param C  the parsing context
@@ -750,7 +784,7 @@ parse_statement(slang_parse_ctx * C, slang_output_ctx * O,
          if (first_var < O->vars->num_variables) {
             const unsigned int num_vars = O->vars->num_variables - first_var;
             unsigned int i;
-
+            assert(oper->num_children == 0);
             oper->num_children = num_vars;
             oper->children = slang_operation_new(num_vars);
             if (oper->children == NULL) {
@@ -856,23 +890,25 @@ handle_nary_expression(slang_parse_ctx * C, slang_operation * op,
 {
    unsigned int i;
 
-   op->children =
-      (slang_operation *) slang_alloc_malloc(n * sizeof(slang_operation));
+   op->children = slang_operation_new(n);
    if (op->children == NULL) {
       slang_info_log_memory(C->L);
       return 0;
    }
    op->num_children = n;
 
-   for (i = 0; i < n; i++)
+   for (i = 0; i < n; i++) {
+      slang_operation_destruct(&op->children[i]);
       op->children[i] = (*ops)[*total_ops - (n + 1 - i)];
+   }
+
    (*ops)[*total_ops - (n + 1)] = (*ops)[*total_ops - 1];
    *total_ops -= n;
 
    *ops = (slang_operation *)
-      slang_alloc_realloc(*ops,
-                          (*total_ops + n) * sizeof(slang_operation),
-                          *total_ops * sizeof(slang_operation));
+      _slang_realloc(*ops,
+                     (*total_ops + n) * sizeof(slang_operation),
+                     *total_ops * sizeof(slang_operation));
    if (*ops == NULL) {
       slang_info_log_memory(C->L);
       return 0;
@@ -903,9 +939,9 @@ parse_expression(slang_parse_ctx * C, slang_output_ctx * O,
 
       /* allocate default operation, becomes a no-op if not used  */
       ops = (slang_operation *)
-         slang_alloc_realloc(ops,
-                             num_ops * sizeof(slang_operation),
-                             (num_ops + 1) * sizeof(slang_operation));
+         _slang_realloc(ops,
+                        num_ops * sizeof(slang_operation),
+                        (num_ops + 1) * sizeof(slang_operation));
       if (ops == NULL) {
          slang_info_log_memory(C->L);
          return 0;
@@ -1037,12 +1073,12 @@ parse_expression(slang_parse_ctx * C, slang_output_ctx * O,
             return 0;
          break;
       case OP_LESSEQUAL:
-         op->type = SLANG_OPER_LESSequal;
+         op->type = SLANG_OPER_LESSEQUAL;
          if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
             return 0;
          break;
       case OP_GREATEREQUAL:
-         op->type = SLANG_OPER_GREATERequal;
+         op->type = SLANG_OPER_GREATEREQUAL;
          if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
             return 0;
          break;
@@ -1145,8 +1181,9 @@ parse_expression(slang_parse_ctx * C, slang_output_ctx * O,
    }
    C->I++;
 
-   *oper = *ops;
-   slang_alloc_free(ops);
+   slang_operation_destruct(oper);
+   *oper = *ops; /* struct copy */
+   _slang_free(ops);
 
    return 1;
 }
@@ -1410,14 +1447,13 @@ parse_function_definition(slang_parse_ctx * C, slang_output_ctx * O,
       return 0;
 
    /* create function's body operation */
-   func->body =
-      (slang_operation *) slang_alloc_malloc(sizeof(slang_operation));
+   func->body = (slang_operation *) _slang_alloc(sizeof(slang_operation));
    if (func->body == NULL) {
       slang_info_log_memory(C->L);
       return 0;
    }
    if (!slang_operation_construct(func->body)) {
-      slang_alloc_free(func->body);
+      _slang_free(func->body);
       func->body = NULL;
       slang_info_log_memory(C->L);
       return 0;
@@ -1449,7 +1485,7 @@ initialize_global(slang_assemble_ctx * A, slang_variable * var)
 
    /* put the variable into operation's scope */
    op_id.locals->variables =
-      (slang_variable **) slang_alloc_malloc(sizeof(slang_variable *));
+      (slang_variable **) _slang_alloc(sizeof(slang_variable *));
    if (op_id.locals->variables == NULL) {
       slang_operation_destruct(&op_id);
       return GL_FALSE;
@@ -1465,7 +1501,7 @@ initialize_global(slang_assemble_ctx * A, slang_variable * var)
    }
    op_assign.type = SLANG_OPER_ASSIGN;
    op_assign.children =
-      (slang_operation *) slang_alloc_malloc(2 * sizeof(slang_operation));
+      (slang_operation *) _slang_alloc(2 * sizeof(slang_operation));
    if (op_assign.children == NULL) {
       slang_operation_destruct(&op_assign);
       op_id.locals->num_variables = 0;
@@ -1480,7 +1516,7 @@ initialize_global(slang_assemble_ctx * A, slang_variable * var)
 
    /* carefully destroy the operations */
    op_assign.num_children = 0;
-   slang_alloc_free(op_assign.children);
+   _slang_free(op_assign.children);
    op_assign.children = NULL;
    slang_operation_destruct(&op_assign);
    op_id.locals->num_variables = 0;
@@ -1541,13 +1577,13 @@ parse_init_declarator(slang_parse_ctx * C, slang_output_ctx * O,
       if (!slang_type_specifier_copy(&var->type.specifier, &type->specifier))
          return 0;
       var->initializer =
-         (slang_operation *) slang_alloc_malloc(sizeof(slang_operation));
+         (slang_operation *) _slang_alloc(sizeof(slang_operation));
       if (var->initializer == NULL) {
          slang_info_log_memory(C->L);
          return 0;
       }
       if (!slang_operation_construct(var->initializer)) {
-         slang_alloc_free(var->initializer);
+         _slang_free(var->initializer);
          var->initializer = NULL;
          slang_info_log_memory(C->L);
          return 0;
@@ -1581,7 +1617,9 @@ parse_init_declarator(slang_parse_ctx * C, slang_output_ctx * O,
       A.space.vars = O->vars;
       A.program = O->program;
       A.vartable = O->vartable;
-      _slang_codegen_global_variable(&A, var, C->type);
+      A.curFuncEndLabel = NULL;
+      if (!_slang_codegen_global_variable(&A, var, C->type))
+         return 0;
    }
 
    /* allocate global address space for a variable with a known size */
@@ -1678,11 +1716,11 @@ parse_function(slang_parse_ctx * C, slang_output_ctx * O, int definition,
    if (found_func == NULL) {
       /* New function, add it to the function list */
       O->funs->functions =
-         (slang_function *) slang_alloc_realloc(O->funs->functions,
-                                                O->funs->num_functions *
-                                                sizeof(slang_function),
-                                                (O->funs->num_functions +
-                                                 1) * sizeof(slang_function));
+         (slang_function *) _slang_realloc(O->funs->functions,
+                                           O->funs->num_functions
+                                           * sizeof(slang_function),
+                                           (O->funs->num_functions + 1)
+                                           * sizeof(slang_function));
       if (O->funs->functions == NULL) {
          slang_info_log_memory(C->L);
          slang_function_destruct(&parsed_func);
@@ -1825,6 +1863,8 @@ parse_code_unit(slang_parse_ctx * C, slang_code_unit * unit,
    C->I++;
 
    _slang_pop_var_table(o.vartable);
+   _slang_delete_var_table(o.vartable);
+
    return GL_TRUE;
 }
 
@@ -1868,14 +1908,22 @@ compile_with_grammar(grammar id, const char *source, slang_code_unit * unit,
    byte *prod;
    GLuint size, start, version;
    slang_string preprocessed;
+   int maxVersion;
+
+#if FEATURE_ARB_shading_language_120
+   maxVersion = 120;
+#else
+   maxVersion = 110;
+#endif
 
    /* First retrieve the version number. */
    if (!_slang_preprocess_version(source, &version, &start, infolog))
       return GL_FALSE;
 
-   if (version > 110) {
+   if (version > maxVersion) {
       slang_info_log_error(infolog,
-                           "language version specified is not supported.");
+                           "language version %.2f is not supported.",
+                           version * 0.01);
       return GL_FALSE;
    }
 
@@ -1898,6 +1946,17 @@ compile_with_grammar(grammar id, const char *source, slang_code_unit * unit,
       grammar_get_last_error((byte *) (buf), sizeof(buf), &pos);
       slang_info_log_error(infolog, buf);
       /* syntax error (possibly in library code) */
+#if 0
+      {
+         int line, col;
+         char *s;
+         s = (char *) _mesa_find_line_column((const GLubyte *) source,
+                                             (const GLubyte *) source + pos,
+                                             &line, &col);
+         printf("Error on line %d, col %d: %s\n", line, col, s);
+      }
+#endif
+      return GL_FALSE;
    }
    slang_string_free(&preprocessed);
 
@@ -1920,6 +1979,14 @@ static const byte slang_core_gc[] = {
 #include "library/slang_core_gc.h"
 };
 
+static const byte slang_120_core_gc[] = {
+#include "library/slang_120_core_gc.h"
+};
+
+static const byte slang_120_fragment_gc[] = {
+#include "library/slang_builtin_120_fragment_gc.h"
+};
+
 static const byte slang_common_builtin_gc[] = {
 #include "library/slang_common_builtin_gc.h"
 };
@@ -1969,11 +2036,24 @@ compile_object(grammar * id, const char *source, slang_code_object * object,
                           NULL, NULL, NULL))
          return GL_FALSE;
 
+#if FEATURE_ARB_shading_language_120
+      if (!compile_binary(slang_120_core_gc,
+                          &object->builtin[SLANG_BUILTIN_120_CORE],
+                          SLANG_UNIT_FRAGMENT_BUILTIN, infolog,
+                          NULL, &object->builtin[SLANG_BUILTIN_CORE], NULL))
+         return GL_FALSE;
+#endif
+
       /* compile common functions and variables, link to core */
       if (!compile_binary(slang_common_builtin_gc,
                           &object->builtin[SLANG_BUILTIN_COMMON],
                           SLANG_UNIT_FRAGMENT_BUILTIN, infolog, NULL,
-                          &object->builtin[SLANG_BUILTIN_CORE], NULL))
+#if FEATURE_ARB_shading_language_120
+                          &object->builtin[SLANG_BUILTIN_120_CORE],
+#else
+                          &object->builtin[SLANG_BUILTIN_CORE],
+#endif
+                          NULL))
          return GL_FALSE;
 
       /* compile target-specific functions and variables, link to common */
@@ -1983,6 +2063,13 @@ compile_object(grammar * id, const char *source, slang_code_object * object,
                              SLANG_UNIT_FRAGMENT_BUILTIN, infolog, NULL,
                              &object->builtin[SLANG_BUILTIN_COMMON], NULL))
             return GL_FALSE;
+#if FEATURE_ARB_shading_language_120
+         if (!compile_binary(slang_120_fragment_gc,
+                             &object->builtin[SLANG_BUILTIN_TARGET],
+                             SLANG_UNIT_FRAGMENT_BUILTIN, infolog, NULL,
+                             &object->builtin[SLANG_BUILTIN_COMMON], NULL))
+            return GL_FALSE;
+#endif
       }
       else if (type == SLANG_UNIT_VERTEX_SHADER) {
          if (!compile_binary(slang_vertex_builtin_gc,
@@ -2048,6 +2135,8 @@ _slang_compile(GLcontext *ctx, struct gl_shader *shader)
       type = SLANG_UNIT_FRAGMENT_SHADER;
    }
 
+   ctx->Shader.MemPool = _slang_new_mempool(1024*1024);
+
    /* XXX temporary hack */
    if (!shader->Programs) {
       GLenum progTarget;
@@ -2057,7 +2146,7 @@ _slang_compile(GLcontext *ctx, struct gl_shader *shader)
          progTarget = GL_FRAGMENT_PROGRAM_ARB;
       shader->Programs
          = (struct gl_program **) malloc(sizeof(struct gl_program*));
-      shader->Programs[0] = _mesa_new_program(ctx, progTarget, 1);
+      shader->Programs[0] = ctx->Driver.NewProgram(ctx, progTarget, 1);
       shader->NumPrograms = 1;
 
       shader->Programs[0]->Parameters = _mesa_new_parameter_list();
@@ -2070,28 +2159,27 @@ _slang_compile(GLcontext *ctx, struct gl_shader *shader)
 
    success = compile_shader(ctx, &obj, type, &info_log, shader);
 
-   if (success && !info_log.text) {
-#if 0
-      slang_create_uniforms(&object->expdata, shader);
-      _mesa_print_program(program);
-      _mesa_print_program_parameters(ctx, program);
-#endif
+   /* free shader's prev info log */
+   if (shader->InfoLog) {
+      _mesa_free(shader->InfoLog);
+      shader->InfoLog = NULL;
    }
-   else {
+
+   if (info_log.text) {
+      /* copy info-log string to shader object */
+      shader->InfoLog = _mesa_strdup(info_log.text);
+   }
+
+   if (info_log.error_flag) {
       success = GL_FALSE;
-      /* XXX more work on info log needed here */
-      if (info_log.text) {
-         if (shader->InfoLog) {
-            free(shader->InfoLog);
-            shader->InfoLog = NULL;
-         }
-         shader->InfoLog = strdup(info_log.text);
-      }
    }
 
    slang_info_log_destruct(&info_log);
    _slang_code_object_dtr(&obj);
 
+   _slang_delete_mempool((slang_mempool *) ctx->Shader.MemPool);
+   ctx->Shader.MemPool = NULL;
+
    return success;
 }