mesa: Silence GCC warning 'comparison between signed and unsigned integer expressions'
[mesa.git] / src / mesa / main / ff_fragment_shader.cpp
index 8a42281db0f806ab826bad70702d760db06c8dad..86317efcd5156d96492e4df11d2413566fd3345f 100644 (file)
@@ -31,8 +31,10 @@ extern "C" {
 #include "glheader.h"
 #include "imports.h"
 #include "mtypes.h"
+#include "main/context.h"
 #include "main/uniforms.h"
 #include "main/macros.h"
+#include "main/samplerobj.h"
 #include "program/program.h"
 #include "program/prog_parameter.h"
 #include "program/prog_cache.h"
@@ -49,7 +51,6 @@ extern "C" {
 #include "../glsl/glsl_symbol_table.h"
 #include "../glsl/glsl_parser_extras.h"
 #include "../glsl/ir_optimization.h"
-#include "../glsl/ir_print_visitor.h"
 #include "../program/ir_to_mesa.h"
 
 using namespace ir_builder;
@@ -303,7 +304,6 @@ static GLuint translate_tex_src_bit( GLbitfield bit )
 
 
 #define VERT_BIT_TEX_ANY    (0xff << VERT_ATTRIB_TEX0)
-#define VERT_RESULT_TEX_ANY (0xff << VERT_RESULT_TEX0)
 
 /**
  * Identify all possible varying inputs.  The fragment program will
@@ -333,7 +333,7 @@ static GLbitfield get_fp_input_mask( struct gl_context *ctx )
    }
    else if (ctx->RenderMode == GL_FEEDBACK) {
       /* _NEW_RENDERMODE */
-      fp_inputs = (FRAG_BIT_COL0 | FRAG_BIT_TEX0);
+      fp_inputs = (VARYING_BIT_COL0 | VARYING_BIT_TEX0);
    }
    else if (!(vertexProgram || vertexShader)) {
       /* Fixed function vertex logic */
@@ -345,33 +345,33 @@ static GLbitfield get_fp_input_mask( struct gl_context *ctx )
        */
       /* _NEW_POINT */
       if (ctx->Point.PointSprite)
-         varying_inputs |= FRAG_BITS_TEX_ANY;
+         varying_inputs |= VARYING_BITS_TEX_ANY;
 
       /* First look at what values may be computed by the generated
        * vertex program:
        */
       /* _NEW_LIGHT */
       if (ctx->Light.Enabled) {
-         fp_inputs |= FRAG_BIT_COL0;
+         fp_inputs |= VARYING_BIT_COL0;
 
          if (texenv_doing_secondary_color(ctx))
-            fp_inputs |= FRAG_BIT_COL1;
+            fp_inputs |= VARYING_BIT_COL1;
       }
 
       /* _NEW_TEXTURE */
       fp_inputs |= (ctx->Texture._TexGenEnabled |
-                    ctx->Texture._TexMatEnabled) << FRAG_ATTRIB_TEX0;
+                    ctx->Texture._TexMatEnabled) << VARYING_SLOT_TEX0;
 
       /* Then look at what might be varying as a result of enabled
        * arrays, etc:
        */
       if (varying_inputs & VERT_BIT_COLOR0)
-         fp_inputs |= FRAG_BIT_COL0;
+         fp_inputs |= VARYING_BIT_COL0;
       if (varying_inputs & VERT_BIT_COLOR1)
-         fp_inputs |= FRAG_BIT_COL1;
+         fp_inputs |= VARYING_BIT_COL1;
 
       fp_inputs |= (((varying_inputs & VERT_BIT_TEX_ANY) >> VERT_ATTRIB_TEX0) 
-                    << FRAG_ATTRIB_TEX0);
+                    << VARYING_SLOT_TEX0);
 
    }
    else {
@@ -395,15 +395,15 @@ static GLbitfield get_fp_input_mask( struct gl_context *ctx )
        */
       /* _NEW_POINT */
       if (ctx->Point.PointSprite)
-         vp_outputs |= FRAG_BITS_TEX_ANY;
+         vp_outputs |= VARYING_BITS_TEX_ANY;
 
-      if (vp_outputs & (1 << VERT_RESULT_COL0))
-         fp_inputs |= FRAG_BIT_COL0;
-      if (vp_outputs & (1 << VERT_RESULT_COL1))
-         fp_inputs |= FRAG_BIT_COL1;
+      if (vp_outputs & (1 << VARYING_SLOT_COL0))
+         fp_inputs |= VARYING_BIT_COL0;
+      if (vp_outputs & (1 << VARYING_SLOT_COL1))
+         fp_inputs |= VARYING_BIT_COL1;
 
-      fp_inputs |= (((vp_outputs & VERT_RESULT_TEX_ANY) >> VERT_RESULT_TEX0) 
-                    << FRAG_ATTRIB_TEX0);
+      fp_inputs |= (((vp_outputs & VARYING_BITS_TEX_ANY) >> VARYING_SLOT_TEX0) 
+                    << VARYING_SLOT_TEX0);
    }
    
    return fp_inputs;
@@ -417,7 +417,7 @@ static GLbitfield get_fp_input_mask( struct gl_context *ctx )
 static GLuint make_state_key( struct gl_context *ctx,  struct state_key *key )
 {
    GLuint i, j;
-   GLbitfield inputs_referenced = FRAG_BIT_COL0;
+   GLbitfield inputs_referenced = VARYING_BIT_COL0;
    const GLbitfield inputs_available = get_fp_input_mask( ctx );
    GLuint keySize;
 
@@ -428,23 +428,25 @@ static GLuint make_state_key( struct gl_context *ctx,  struct state_key *key )
       const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
       const struct gl_texture_object *texObj = texUnit->_Current;
       const struct gl_tex_env_combine_state *comb = texUnit->_CurrentCombine;
+      const struct gl_sampler_object *samp;
       GLenum format;
 
       if (!texUnit->_ReallyEnabled || !texUnit->Enabled)
          continue;
 
+      samp = _mesa_get_samplerobj(ctx, i);
       format = texObj->Image[0][texObj->BaseLevel]->_BaseFormat;
 
       key->unit[i].enabled = 1;
       key->enabled_units |= (1<<i);
       key->nr_enabled_units = i + 1;
-      inputs_referenced |= FRAG_BIT_TEX(i);
+      inputs_referenced |= VARYING_BIT_TEX(i);
 
       key->unit[i].source_index =
          translate_tex_src_bit(texUnit->_ReallyEnabled);
 
       key->unit[i].shadow =
-         ((texObj->Sampler.CompareMode == GL_COMPARE_R_TO_TEXTURE) &&
+         ((samp->CompareMode == GL_COMPARE_R_TO_TEXTURE) &&
           ((format == GL_DEPTH_COMPONENT) || 
            (format == GL_DEPTH_STENCIL_EXT)));
 
@@ -480,14 +482,14 @@ static GLuint make_state_key( struct gl_context *ctx,  struct state_key *key )
    /* _NEW_LIGHT | _NEW_FOG */
    if (texenv_doing_secondary_color(ctx)) {
       key->separate_specular = 1;
-      inputs_referenced |= FRAG_BIT_COL1;
+      inputs_referenced |= VARYING_BIT_COL1;
    }
 
    /* _NEW_FOG */
    if (ctx->Fog.Enabled) {
       key->fog_enabled = 1;
       key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
-      inputs_referenced |= FRAG_BIT_FOGC; /* maybe */
+      inputs_referenced |= VARYING_BIT_FOGC; /* maybe */
    }
 
    /* _NEW_BUFFERS */
@@ -536,7 +538,7 @@ public:
 };
 
 static ir_rvalue *
-get_current_attrib(struct texenv_fragment_program *p, GLuint attrib)
+get_current_attrib(texenv_fragment_program *p, GLuint attrib)
 {
    ir_variable *current;
    ir_rvalue *val;
@@ -549,9 +551,9 @@ get_current_attrib(struct texenv_fragment_program *p, GLuint attrib)
 }
 
 static ir_rvalue *
-get_gl_Color(struct texenv_fragment_program *p)
+get_gl_Color(texenv_fragment_program *p)
 {
-   if (p->state->inputs_available & FRAG_BIT_COL0) {
+   if (p->state->inputs_available & VARYING_BIT_COL0) {
       ir_variable *var = p->shader->symbols->get_variable("gl_Color");
       assert(var);
       return new(p->mem_ctx) ir_dereference_variable(var);
@@ -561,7 +563,7 @@ get_gl_Color(struct texenv_fragment_program *p)
 }
 
 static ir_rvalue *
-get_source(struct texenv_fragment_program *p,
+get_source(texenv_fragment_program *p,
           GLuint src, GLuint unit)
 {
    ir_variable *var;
@@ -612,7 +614,7 @@ get_source(struct texenv_fragment_program *p,
 }
 
 static ir_rvalue *
-emit_combine_source(struct texenv_fragment_program *p,
+emit_combine_source(texenv_fragment_program *p,
                    GLuint unit,
                    GLuint source,
                    GLuint operand)
@@ -687,7 +689,7 @@ static GLboolean args_match( const struct state_key *key, GLuint unit )
 }
 
 static ir_rvalue *
-smear(struct texenv_fragment_program *p, ir_rvalue *val)
+smear(texenv_fragment_program *p, ir_rvalue *val)
 {
    if (!val->type->is_scalar())
       return val;
@@ -696,7 +698,7 @@ smear(struct texenv_fragment_program *p, ir_rvalue *val)
 }
 
 static ir_rvalue *
-emit_combine(struct texenv_fragment_program *p,
+emit_combine(texenv_fragment_program *p,
             GLuint unit,
             GLuint nr,
             GLuint mode,
@@ -777,7 +779,7 @@ emit_combine(struct texenv_fragment_program *p,
  * Generate instructions for one texture unit's env/combiner mode.
  */
 static ir_rvalue *
-emit_texenv(struct texenv_fragment_program *p, GLuint unit)
+emit_texenv(texenv_fragment_program *p, GLuint unit)
 {
    const struct state_key *key = p->state;
    GLboolean rgb_saturate, alpha_saturate;
@@ -887,10 +889,10 @@ emit_texenv(struct texenv_fragment_program *p, GLuint unit)
       }
       else {
         float const_data[4] = {
-           1 << rgb_shift,
-           1 << rgb_shift,
-           1 << rgb_shift,
-           1 << alpha_shift
+           float(1 << rgb_shift),
+           float(1 << rgb_shift),
+           float(1 << rgb_shift),
+           float(1 << alpha_shift)
         };
         shift = new(p->mem_ctx) ir_constant(glsl_type::vec4_type,
                                             (ir_constant_data *)const_data);
@@ -906,7 +908,7 @@ emit_texenv(struct texenv_fragment_program *p, GLuint unit)
 /**
  * Generate instruction for getting a texture source term.
  */
-static void load_texture( struct texenv_fragment_program *p, GLuint unit )
+static void load_texture( texenv_fragment_program *p, GLuint unit )
 {
    ir_dereference *deref;
 
@@ -916,7 +918,7 @@ static void load_texture( struct texenv_fragment_program *p, GLuint unit )
    const GLuint texTarget = p->state->unit[unit].source_index;
    ir_rvalue *texcoord;
 
-   if (!(p->state->inputs_available & (FRAG_BIT_TEX0 << unit))) {
+   if (!(p->state->inputs_available & (VARYING_BIT_TEX0 << unit))) {
       texcoord = get_current_attrib(p, VERT_ATTRIB_TEX0 + unit);
    } else if (p->texcoord_tex[unit]) {
       texcoord = new(p->mem_ctx) ir_dereference_variable(p->texcoord_tex[unit]);
@@ -1007,6 +1009,15 @@ static void load_texture( struct texenv_fragment_program *p, GLuint unit )
                                                      sampler_name,
                                                      ir_var_uniform);
    p->top_instructions->push_head(sampler);
+
+   /* Set the texture unit for this sampler.  The linker will pick this value
+    * up and do-the-right-thing.
+    *
+    * NOTE: The cast to int is important.  Without it, the constant will have
+    * type uint, and things later on may get confused.
+    */
+   sampler->constant_value = new(p->mem_ctx) ir_constant(int(unit));
+
    deref = new(p->mem_ctx) ir_dereference_variable(sampler);
    tex->set_sampler(deref, glsl_type::vec4_type);
 
@@ -1027,7 +1038,7 @@ static void load_texture( struct texenv_fragment_program *p, GLuint unit )
 }
 
 static void
-load_texenv_source(struct texenv_fragment_program *p,
+load_texenv_source(texenv_fragment_program *p,
                   GLuint src, GLuint unit)
 {
    switch (src) {
@@ -1057,7 +1068,7 @@ load_texenv_source(struct texenv_fragment_program *p,
  * Generate instructions for loading all texture source terms.
  */
 static GLboolean
-load_texunit_sources( struct texenv_fragment_program *p, GLuint unit )
+load_texunit_sources( texenv_fragment_program *p, GLuint unit )
 {
    const struct state_key *key = p->state;
    GLuint i;
@@ -1077,7 +1088,7 @@ load_texunit_sources( struct texenv_fragment_program *p, GLuint unit )
  * Generate instructions for loading bump map textures.
  */
 static void
-load_texunit_bumpmap( struct texenv_fragment_program *p, GLuint unit )
+load_texunit_bumpmap( texenv_fragment_program *p, GLuint unit )
 {
    const struct state_key *key = p->state;
    GLuint bumpedUnitNr = key->unit[unit].OptRGB[1].Source - SRC_TEXTURE0;
@@ -1129,7 +1140,7 @@ load_texunit_bumpmap( struct texenv_fragment_program *p, GLuint unit )
  * GL_FOG_COORDINATE_EXT is set to GL_FRAGMENT_DEPTH_EXT.
  */
 static ir_rvalue *
-emit_fog_instructions(struct texenv_fragment_program *p,
+emit_fog_instructions(texenv_fragment_program *p,
                      ir_rvalue *fragcolor)
 {
    struct state_key *key = p->state;
@@ -1202,7 +1213,7 @@ emit_fog_instructions(struct texenv_fragment_program *p,
 }
 
 static void
-emit_instructions(struct texenv_fragment_program *p)
+emit_instructions(texenv_fragment_program *p)
 {
    struct state_key *key = p->state;
    GLuint unit;
@@ -1242,7 +1253,7 @@ emit_instructions(struct texenv_fragment_program *p)
       p->emit(assign(spec_result, cf));
 
       ir_rvalue *secondary;
-      if (p->state->inputs_available & FRAG_BIT_COL1) {
+      if (p->state->inputs_available & VARYING_BIT_COL1) {
         ir_variable *var =
            p->shader->symbols->get_variable("gl_SecondaryColor");
         assert(var);
@@ -1297,7 +1308,8 @@ create_new_program(struct gl_context *ctx, struct state_key *key)
    p.shader_program->InternalSeparateShader = GL_TRUE;
 
    state->language_version = 130;
-   if (ctx->Extensions.OES_EGL_image_external)
+   state->es_shader = false;
+   if (_mesa_is_gles(ctx) && ctx->Extensions.OES_EGL_image_external)
       state->OES_EGL_image_external_enable = true;
    _mesa_glsl_initialize_types(state);
    _mesa_glsl_initialize_variables(p.instructions, state);
@@ -1324,7 +1336,10 @@ create_new_program(struct gl_context *ctx, struct state_key *key)
 
    validate_ir_tree(p.shader->ir);
 
-   while (do_common_optimization(p.shader->ir, false, false, 32))
+   const struct gl_shader_compiler_options *options =
+      &ctx->ShaderCompilerOptions[MESA_SHADER_FRAGMENT];
+
+   while (do_common_optimization(p.shader->ir, false, false, 32, options))
       ;
    reparent_ir(p.shader->ir, p.shader->ir);
 
@@ -1338,53 +1353,6 @@ create_new_program(struct gl_context *ctx, struct state_key *key)
 
    _mesa_glsl_link_shader(ctx, p.shader_program);
 
-   /* Set the sampler uniforms, and relink to get them into the linked
-    * program.
-    */
-   struct gl_shader *const fs =
-      p.shader_program->_LinkedShaders[MESA_SHADER_FRAGMENT];
-   struct gl_program *const fp = fs->Program;
-
-   _mesa_generate_parameters_list_for_uniforms(p.shader_program, fs,
-                                              fp->Parameters);
-
-   _mesa_associate_uniform_storage(ctx, p.shader_program, fp->Parameters);
-
-   for (unsigned int i = 0; i < MAX_TEXTURE_UNITS; i++) {
-      /* Enough space for 'sampler_999\0'.
-       */
-      char name[12];
-
-      snprintf(name, sizeof(name), "sampler_%d", i);
-
-      int loc = _mesa_get_uniform_location(ctx, p.shader_program, name);
-      if (loc != -1) {
-        unsigned base;
-        unsigned idx;
-
-        /* Avoid using _mesa_uniform() because it flags state
-         * updates, so if we're generating this shader_program in a
-         * state update, we end up recursing.  Instead, just set the
-         * value, which is picked up at re-link.
-         */
-        _mesa_uniform_split_location_offset(loc, &base, &idx);
-        assert(idx == 0);
-
-        struct gl_uniform_storage *const storage =
-           &p.shader_program->UniformStorage[base];
-
-        /* Update the storage, the SamplerUnits in the shader program, and
-         * the SamplerUnits in the assembly shader.
-         */
-        storage->storage[idx].i = i;
-        fp->SamplerUnits[storage->sampler] = i;
-        p.shader_program->SamplerUnits[storage->sampler] = i;
-        _mesa_propagate_uniforms_to_driver_storage(storage, 0, 1);
-      }
-   }
-   _mesa_update_shader_textures_used(p.shader_program, fp);
-   (void) ctx->Driver.ProgramStringNotify(ctx, fp->Target, fp);
-
    if (!p.shader_program->LinkStatus)
       _mesa_problem(ctx, "Failed to link fixed function fragment shader: %s\n",
                    p.shader_program->InfoLog);