egl: Log (debug) native platform type
[mesa.git] / src / mesa / program / ir_to_mesa.cpp
index 67adb8f3dcd4999314fdd0cf12b0a2e13c9fc1eb..1ef609fe15d7f03498e13123a7ac57ca8c2ab3c1 100644 (file)
@@ -134,7 +134,7 @@ src_reg::src_reg(dst_reg reg)
    this->index = reg.index;
    this->swizzle = SWIZZLE_XYZW;
    this->negate = 0;
-   this->reladdr = NULL;
+   this->reladdr = reg.reladdr;
 }
 
 dst_reg::dst_reg(src_reg reg)
@@ -331,20 +331,6 @@ dst_reg undef_dst = dst_reg(PROGRAM_UNDEFINED, SWIZZLE_NOOP);
 
 dst_reg address_reg = dst_reg(PROGRAM_ADDRESS, WRITEMASK_X);
 
-static void
-fail_link(struct gl_shader_program *prog, const char *fmt, ...) PRINTFLIKE(2, 3);
-
-static void
-fail_link(struct gl_shader_program *prog, const char *fmt, ...)
-{
-   va_list args;
-   va_start(args, fmt);
-   ralloc_vasprintf_append(&prog->InfoLog, fmt, args);
-   va_end(args);
-
-   prog->LinkStatus = GL_FALSE;
-}
-
 static int
 swizzle_for_size(int size)
 {
@@ -599,7 +585,7 @@ ir_to_mesa_visitor::src_reg_for_float(float val)
    src_reg src(PROGRAM_CONSTANT, -1, NULL);
 
    src.index = _mesa_add_unnamed_constant(this->prog->Parameters,
-                                         &val, 1, &src.swizzle);
+                                         (const gl_constant_value *)&val, 1, &src.swizzle);
 
    return src;
 }
@@ -655,8 +641,6 @@ src_reg
 ir_to_mesa_visitor::get_temp(const glsl_type *type)
 {
    src_reg src;
-   int swizzle[4];
-   int i;
 
    src.file = PROGRAM_TEMPORARY;
    src.index = next_temp;
@@ -666,12 +650,7 @@ ir_to_mesa_visitor::get_temp(const glsl_type *type)
    if (type->is_array() || type->is_record()) {
       src.swizzle = SWIZZLE_NOOP;
    } else {
-      for (i = 0; i < type->vector_elements; i++)
-        swizzle[i] = i;
-      for (; i < 4; i++)
-        swizzle[i] = type->vector_elements - 1;
-      src.swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
-                                 swizzle[2], swizzle[3]);
+      src.swizzle = swizzle_for_size(type->vector_elements);
    }
    src.negate = 0;
 
@@ -789,10 +768,11 @@ ir_to_mesa_visitor::visit(ir_variable *ir)
 
       if (storage->file == PROGRAM_TEMPORARY &&
          dst.index != storage->index + (int) ir->num_state_slots) {
-        fail_link(this->shader_program,
-                  "failed to load builtin uniform `%s'  (%d/%d regs loaded)\n",
-                  ir->name, dst.index - storage->index,
-                  type_size(ir->type));
+        linker_error(this->shader_program,
+                     "failed to load builtin uniform `%s' "
+                     "(%d/%d regs loaded)\n",
+                     ir->name, dst.index - storage->index,
+                     type_size(ir->type));
       }
    }
 }
@@ -803,48 +783,44 @@ ir_to_mesa_visitor::visit(ir_loop *ir)
    ir_dereference_variable *counter = NULL;
 
    if (ir->counter != NULL)
-      counter = new(ir) ir_dereference_variable(ir->counter);
+      counter = new(mem_ctx) ir_dereference_variable(ir->counter);
 
    if (ir->from != NULL) {
       assert(ir->counter != NULL);
 
-      ir_assignment *a = new(ir) ir_assignment(counter, ir->from, NULL);
+      ir_assignment *a =
+       new(mem_ctx) ir_assignment(counter, ir->from, NULL);
 
       a->accept(this);
-      delete a;
    }
 
    emit(NULL, OPCODE_BGNLOOP);
 
    if (ir->to) {
       ir_expression *e =
-        new(ir) ir_expression(ir->cmp, glsl_type::bool_type,
-                              counter, ir->to);
-      ir_if *if_stmt =  new(ir) ir_if(e);
+        new(mem_ctx) ir_expression(ir->cmp, glsl_type::bool_type,
+                                         counter, ir->to);
+      ir_if *if_stmt =  new(mem_ctx) ir_if(e);
 
-      ir_loop_jump *brk = new(ir) ir_loop_jump(ir_loop_jump::jump_break);
+      ir_loop_jump *brk =
+       new(mem_ctx) ir_loop_jump(ir_loop_jump::jump_break);
 
       if_stmt->then_instructions.push_tail(brk);
 
       if_stmt->accept(this);
-
-      delete if_stmt;
-      delete e;
-      delete brk;
    }
 
    visit_exec_list(&ir->body_instructions, this);
 
    if (ir->increment) {
       ir_expression *e =
-        new(ir) ir_expression(ir_binop_add, counter->type,
-                              counter, ir->increment);
+        new(mem_ctx) ir_expression(ir_binop_add, counter->type,
+                                         counter, ir->increment);
 
-      ir_assignment *a = new(ir) ir_assignment(counter, e, NULL);
+      ir_assignment *a =
+       new(mem_ctx) ir_assignment(counter, e, NULL);
 
       a->accept(this);
-      delete a;
-      delete e;
    }
 
    emit(NULL, OPCODE_ENDLOOP);
@@ -932,10 +908,30 @@ ir_to_mesa_visitor::try_emit_sat(ir_expression *ir)
    sat_src->accept(this);
    src_reg src = this->result;
 
-   this->result = get_temp(ir->type);
-   ir_to_mesa_instruction *inst;
-   inst = emit(ir, OPCODE_MOV, dst_reg(this->result), src);
-   inst->saturate = true;
+   /* If we generated an expression instruction into a temporary in
+    * processing the saturate's operand, apply the saturate to that
+    * instruction.  Otherwise, generate a MOV to do the saturate.
+    *
+    * Note that we have to be careful to only do this optimization if
+    * the instruction in question was what generated src->result.  For
+    * example, ir_dereference_array might generate a MUL instruction
+    * to create the reladdr, and return us a src reg using that
+    * reladdr.  That MUL result is not the value we're trying to
+    * saturate.
+    */
+   ir_expression *sat_src_expr = sat_src->as_expression();
+   ir_to_mesa_instruction *new_inst;
+   new_inst = (ir_to_mesa_instruction *)this->instructions.get_tail();
+   if (sat_src_expr && (sat_src_expr->operation == ir_binop_mul ||
+                       sat_src_expr->operation == ir_binop_add ||
+                       sat_src_expr->operation == ir_binop_dot)) {
+      new_inst->saturate = true;
+   } else {
+      this->result = get_temp(ir->type);
+      ir_to_mesa_instruction *inst;
+      inst = emit(ir, OPCODE_MOV, dst_reg(this->result), src);
+      inst->saturate = true;
+   }
 
    return true;
 }
@@ -1419,9 +1415,9 @@ ir_to_mesa_visitor::visit(ir_dereference_variable *ir)
       case ir_var_in:
       case ir_var_inout:
         /* The linker assigns locations for varyings and attributes,
-         * including deprecated builtins (like gl_Color), user-assign
-         * generic attributes (glBindVertexLocation), and
-         * user-defined varyings.
+         * including deprecated builtins (like gl_Color),
+         * user-assigned generic attributes (glBindVertexLocation),
+         * and user-defined varyings.
          *
          * FINISHME: We would hit this path for function arguments.  Fix!
          */
@@ -1500,6 +1496,18 @@ ir_to_mesa_visitor::visit(ir_dereference_array *ir)
              this->result, src_reg_for_float(element_size));
       }
 
+      /* If there was already a relative address register involved, add the
+       * new and the old together to get the new offset.
+       */
+      if (src.reladdr != NULL)  {
+        src_reg accum_reg = get_temp(glsl_type::float_type);
+
+        emit(ir, OPCODE_ADD, dst_reg(accum_reg),
+             index_reg, *src.reladdr);
+
+        index_reg = accum_reg;
+      }
+
       src.reladdr = ralloc(mem_ctx, src_reg);
       memcpy(src.reladdr, &index_reg, sizeof(index_reg));
    }
@@ -1800,7 +1808,7 @@ ir_to_mesa_visitor::visit(ir_constant *ir)
 
         src = src_reg(PROGRAM_CONSTANT, -1, NULL);
         src.index = _mesa_add_unnamed_constant(this->prog->Parameters,
-                                               values,
+                                               (gl_constant_value *) values,
                                                ir->type->vector_elements,
                                                &src.swizzle);
         emit(ir, OPCODE_MOV, mat_column, src);
@@ -1838,7 +1846,7 @@ ir_to_mesa_visitor::visit(ir_constant *ir)
 
    this->result = src_reg(PROGRAM_CONSTANT, -1, ir->type);
    this->result.index = _mesa_add_unnamed_constant(this->prog->Parameters,
-                                                  values,
+                                                  (gl_constant_value *) values,
                                                   ir->type->vector_elements,
                                                   &this->result.swizzle);
 }
@@ -2405,29 +2413,32 @@ check_resources(const struct gl_context *ctx,
    case GL_VERTEX_PROGRAM_ARB:
       if (_mesa_bitcount(prog->SamplersUsed) >
           ctx->Const.MaxVertexTextureImageUnits) {
-         fail_link(shader_program, "Too many vertex shader texture samplers");
+         linker_error(shader_program,
+                     "Too many vertex shader texture samplers");
       }
       if (prog->Parameters->NumParameters > MAX_UNIFORMS) {
-         fail_link(shader_program, "Too many vertex shader constants");
+         linker_error(shader_program, "Too many vertex shader constants");
       }
       break;
    case MESA_GEOMETRY_PROGRAM:
       if (_mesa_bitcount(prog->SamplersUsed) >
           ctx->Const.MaxGeometryTextureImageUnits) {
-         fail_link(shader_program, "Too many geometry shader texture samplers");
+         linker_error(shader_program,
+                     "Too many geometry shader texture samplers");
       }
       if (prog->Parameters->NumParameters >
           MAX_GEOMETRY_UNIFORM_COMPONENTS / 4) {
-         fail_link(shader_program, "Too many geometry shader constants");
+         linker_error(shader_program, "Too many geometry shader constants");
       }
       break;
    case GL_FRAGMENT_PROGRAM_ARB:
       if (_mesa_bitcount(prog->SamplersUsed) >
           ctx->Const.MaxTextureImageUnits) {
-         fail_link(shader_program, "Too many fragment shader texture samplers");
+         linker_error(shader_program,
+                     "Too many fragment shader texture samplers");
       }
       if (prog->Parameters->NumParameters > MAX_UNIFORMS) {
-         fail_link(shader_program, "Too many fragment shader constants");
+         linker_error(shader_program, "Too many fragment shader constants");
       }
       break;
    default:
@@ -2535,16 +2546,17 @@ add_uniforms_to_parameters_list(struct gl_shader_program *shader_program,
          */
         if (file == PROGRAM_SAMPLER) {
            for (unsigned int j = 0; j < size / 4; j++)
-              prog->Parameters->ParameterValues[index + j][0] = next_sampler++;
+              prog->Parameters->ParameterValues[index + j][0].f = next_sampler++;
         }
 
         /* The location chosen in the Parameters list here (returned
          * from _mesa_add_uniform) has to match what the linker chose.
          */
         if (index != parameter_index) {
-           fail_link(shader_program, "Allocation of uniform `%s' to target "
-                     "failed (%d vs %d)\n",
-                     uniform->Name, index, parameter_index);
+           linker_error(shader_program,
+                        "Allocation of uniform `%s' to target failed "
+                        "(%d vs %d)\n",
+                        uniform->Name, index, parameter_index);
         }
       }
    }
@@ -2577,8 +2589,8 @@ set_uniform_initializer(struct gl_context *ctx, void *mem_ctx,
    int loc = _mesa_get_uniform_location(ctx, shader_program, name);
 
    if (loc == -1) {
-      fail_link(shader_program,
-               "Couldn't find uniform for initializer %s\n", name);
+      linker_error(shader_program,
+                  "Couldn't find uniform for initializer %s\n", name);
       return;
    }
 
@@ -2978,11 +2990,31 @@ get_mesa_program(struct gl_context *ctx,
          if (mesa_inst->SrcReg[src].RelAddr)
             prog->IndirectRegisterFiles |= 1 << mesa_inst->SrcReg[src].File;
 
-      if (options->EmitNoIfs && mesa_inst->Opcode == OPCODE_IF) {
-        fail_link(shader_program, "Couldn't flatten if statement\n");
-      }
-
       switch (mesa_inst->Opcode) {
+      case OPCODE_IF:
+        if (options->EmitNoIfs) {
+           linker_warning(shader_program,
+                          "Couldn't flatten if-statement.  "
+                          "This will likely result in software "
+                          "rasterization.\n");
+        }
+        break;
+      case OPCODE_BGNLOOP:
+        if (options->EmitNoLoops) {
+           linker_warning(shader_program,
+                          "Couldn't unroll loop.  "
+                          "This will likely result in software "
+                          "rasterization.\n");
+        }
+        break;
+      case OPCODE_CONT:
+        if (options->EmitNoCont) {
+           linker_warning(shader_program,
+                          "Couldn't lower continue-statement.  "
+                          "This will likely result in software "
+                          "rasterization.\n");
+        }
+        break;
       case OPCODE_BGNSUB:
         inst->function->inst = i;
         mesa_inst->Comment = strdup(inst->function->sig->function_name());
@@ -3250,7 +3282,7 @@ _mesa_glsl_link_shader(struct gl_context *ctx, struct gl_shader_program *prog)
 
    for (i = 0; i < prog->NumShaders; i++) {
       if (!prog->Shaders[i]->CompileStatus) {
-        fail_link(prog, "linking with uncompiled shader");
+        linker_error(prog, "linking with uncompiled shader");
         prog->LinkStatus = GL_FALSE;
       }
    }