mesa/glsl: set num_textures per stage directly in shader_info
[mesa.git] / src / mesa / main / ff_fragment_shader.cpp
index 605f3713e7c290494ee02df399c5766cbe96a6c5..48b84e8fd7324211b178428d418b75521c2f2030 100644 (file)
  * 
  **************************************************************************/
 
-extern "C" {
-#include "glheader.h"
-#include "imports.h"
-#include "mtypes.h"
+#include "main/glheader.h"
 #include "main/context.h"
+#include "main/imports.h"
 #include "main/macros.h"
 #include "main/samplerobj.h"
+#include "main/shaderobj.h"
+#include "main/texenvprogram.h"
+#include "main/texobj.h"
+#include "main/uniforms.h"
+#include "compiler/glsl/ir_builder.h"
+#include "compiler/glsl/ir_optimization.h"
+#include "compiler/glsl/glsl_parser_extras.h"
+#include "compiler/glsl/glsl_symbol_table.h"
+#include "compiler/glsl_types.h"
+#include "program/ir_to_mesa.h"
 #include "program/program.h"
-#include "program/prog_parameter.h"
+#include "program/programopt.h"
 #include "program/prog_cache.h"
 #include "program/prog_instruction.h"
+#include "program/prog_parameter.h"
 #include "program/prog_print.h"
 #include "program/prog_statevars.h"
-#include "program/programopt.h"
-#include "texenvprogram.h"
-#include "texobj.h"
-}
-#include "main/uniforms.h"
-#include "../glsl/glsl_types.h"
-#include "../glsl/ir.h"
-#include "../glsl/ir_builder.h"
-#include "../glsl/glsl_symbol_table.h"
-#include "../glsl/glsl_parser_extras.h"
-#include "../glsl/ir_optimization.h"
-#include "../program/ir_to_mesa.h"
+#include "util/bitscan.h"
 
 using namespace ir_builder;
 
@@ -105,7 +103,6 @@ struct state_key {
    GLuint nr_enabled_units:8;
    GLuint enabled_units:8;
    GLuint separate_specular:1;
-   GLuint fog_enabled:1;
    GLuint fog_mode:2;          /**< FOG_x */
    GLuint inputs_available:12;
    GLuint num_draw_buffers:4;
@@ -129,10 +126,10 @@ struct state_key {
    } unit[MAX_TEXTURE_UNITS];
 };
 
-#define FOG_LINEAR  0
-#define FOG_EXP     1
-#define FOG_EXP2    2
-#define FOG_UNKNOWN 3
+#define FOG_NONE    0
+#define FOG_LINEAR  1
+#define FOG_EXP     2
+#define FOG_EXP2    3
 
 static GLuint translate_fog_mode( GLenum mode )
 {
@@ -140,7 +137,7 @@ static GLuint translate_fog_mode( GLenum mode )
    case GL_LINEAR: return FOG_LINEAR;
    case GL_EXP: return FOG_EXP;
    case GL_EXP2: return FOG_EXP2;
-   default: return FOG_UNKNOWN;
+   default: return FOG_NONE;
    }
 }
 
@@ -220,7 +217,6 @@ static GLuint translate_source( GLenum src )
 #define MODE_MODULATE_SUBTRACT_ATI      12  /* r = a0 * a2 - a1 */
 #define MODE_ADD_PRODUCTS               13  /* r = a0 * a1 + a2 * a3 */
 #define MODE_ADD_PRODUCTS_SIGNED        14  /* r = a0 * a1 + a2 * a3 - 0.5 */
-#define MODE_BUMP_ENVMAP_ATI            15  /* special */
 #define MODE_UNKNOWN                    16
 
 /**
@@ -250,7 +246,6 @@ static GLuint translate_mode( GLenum envMode, GLenum mode )
    case GL_MODULATE_ADD_ATI: return MODE_MODULATE_ADD_ATI;
    case GL_MODULATE_SIGNED_ADD_ATI: return MODE_MODULATE_SIGNED_ADD_ATI;
    case GL_MODULATE_SUBTRACT_ATI: return MODE_MODULATE_SUBTRACT_ATI;
-   case GL_BUMP_ENVMAP_ATI: return MODE_BUMP_ENVMAP_ATI;
    default:
       assert(0);
       return MODE_UNKNOWN;
@@ -283,7 +278,6 @@ need_saturate( GLuint mode )
    case MODE_MODULATE_SUBTRACT_ATI:
    case MODE_ADD_PRODUCTS:
    case MODE_ADD_PRODUCTS_SIGNED:
-   case MODE_BUMP_ENVMAP_ATI:
       return GL_TRUE;
    default:
       assert(0);
@@ -307,7 +301,7 @@ static GLbitfield get_fp_input_mask( struct gl_context *ctx )
    /* _NEW_PROGRAM */
    const GLboolean vertexShader =
       (ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX] &&
-       ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX]->LinkStatus &&
+       ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX]->data->LinkStatus &&
        ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX]->_LinkedShaders[MESA_SHADER_VERTEX]);
    const GLboolean vertexProgram = ctx->VertexProgram._Enabled;
    GLbitfield fp_inputs = 0x0;
@@ -374,9 +368,9 @@ static GLbitfield get_fp_input_mask( struct gl_context *ctx )
       if (vertexShader)
          vprog = ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX]->_LinkedShaders[MESA_SHADER_VERTEX]->Program;
       else
-         vprog = &ctx->VertexProgram.Current->Base;
+         vprog = ctx->VertexProgram.Current;
 
-      vp_outputs = vprog->OutputsWritten;
+      vp_outputs = vprog->info.outputs_written;
 
       /* These get generated in the setup routine regardless of the
        * vertex program:
@@ -404,26 +398,29 @@ 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;
+   GLuint j;
    GLbitfield inputs_referenced = VARYING_BIT_COL0;
    const GLbitfield inputs_available = get_fp_input_mask( ctx );
+   GLbitfield mask;
    GLuint keySize;
 
    memset(key, 0, sizeof(*key));
 
    /* _NEW_TEXTURE */
-   for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
+   mask = ctx->Texture._EnabledCoordUnits;
+   while (mask) {
+      const int i = u_bit_scan(&mask);
       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->_Current || !texUnit->Enabled)
+      if (!texObj)
          continue;
 
       samp = _mesa_get_samplerobj(ctx, i);
-      format = texObj->Image[0][texObj->BaseLevel]->_BaseFormat;
+      format = _mesa_texture_base_format(texObj);
 
       key->unit[i].enabled = 1;
       key->enabled_units |= (1<<i);
@@ -455,16 +452,6 @@ static GLuint make_state_key( struct gl_context *ctx,  struct state_key *key )
          key->unit[i].OptRGB[j].Source = translate_source(comb->SourceRGB[j]);
          key->unit[i].OptA[j].Source = translate_source(comb->SourceA[j]);
       }
-
-      if (key->unit[i].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
-         /* requires some special translation */
-         key->unit[i].NumArgsRGB = 2;
-         key->unit[i].ScaleShiftRGB = 0;
-         key->unit[i].OptRGB[0].Operand = OPR_SRC_COLOR;
-         key->unit[i].OptRGB[0].Source = SRC_TEXTURE;
-         key->unit[i].OptRGB[1].Operand = OPR_SRC_COLOR;
-         key->unit[i].OptRGB[1].Source = texUnit->BumpTarget - GL_TEXTURE0 + SRC_TEXTURE0;
-       }
    }
 
    /* _NEW_LIGHT | _NEW_FOG */
@@ -475,7 +462,6 @@ static GLuint make_state_key( struct gl_context *ctx,  struct state_key *key )
 
    /* _NEW_FOG */
    if (ctx->Fog.Enabled) {
-      key->fog_enabled = 1;
       key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
       inputs_referenced |= VARYING_BIT_FOGC; /* maybe */
    }
@@ -533,7 +519,7 @@ get_current_attrib(texenv_fragment_program *p, GLuint attrib)
 
    current = p->shader->symbols->get_variable("gl_CurrentAttribFragMESA");
    assert(current);
-   current->data.max_array_access = MAX2(current->data.max_array_access, attrib);
+   current->data.max_array_access = MAX2(current->data.max_array_access, (int)attrib);
    val = new(p->mem_ctx) ir_dereference_variable(current);
    ir_rvalue *index = new(p->mem_ctx) ir_constant(attrib);
    return new(p->mem_ctx) ir_dereference_array(val, index);
@@ -577,7 +563,7 @@ get_source(texenv_fragment_program *p,
       var = p->shader->symbols->get_variable("gl_TextureEnvColor");
       assert(var);
       deref = new(p->mem_ctx) ir_dereference_variable(var);
-      var->data.max_array_access = MAX2(var->data.max_array_access, unit);
+      var->data.max_array_access = MAX2(var->data.max_array_access, (int)unit);
       return new(p->mem_ctx) ir_dereference_array(deref,
                                                  new(p->mem_ctx) ir_constant(unit));
 
@@ -678,7 +664,7 @@ static GLboolean args_match( const struct state_key *key, GLuint unit )
 }
 
 static ir_rvalue *
-smear(texenv_fragment_program *p, ir_rvalue *val)
+smear(ir_rvalue *val)
 {
    if (!val->type->is_scalar())
       return val;
@@ -735,7 +721,7 @@ emit_combine(texenv_fragment_program *p,
       tmp1 = mul(src[1], new(p->mem_ctx) ir_constant(2.0f));
       tmp1 = add(tmp1, new(p->mem_ctx) ir_constant(-1.0f));
 
-      return dot(swizzle_xyz(smear(p, tmp0)), swizzle_xyz(smear(p, tmp1)));
+      return dot(swizzle_xyz(smear(tmp0)), swizzle_xyz(smear(tmp1)));
    }
    case MODE_MODULATE_ADD_ATI:
       return add(mul(src[0], src[2]), src[1]);
@@ -753,11 +739,6 @@ emit_combine(texenv_fragment_program *p,
    case MODE_ADD_PRODUCTS_SIGNED:
       return add(add(mul(src[0], src[1]), mul(src[2], src[3])),
                 new(p->mem_ctx) ir_constant(-0.5f));
-
-   case MODE_BUMP_ENVMAP_ATI:
-      /* special - not handled here */
-      assert(0);
-      return src[0];
    default: 
       assert(0);
       return src[0];
@@ -777,10 +758,6 @@ emit_texenv(texenv_fragment_program *p, GLuint unit)
    if (!key->unit[unit].enabled) {
       return get_source(p, SRC_PREVIOUS, 0);
    }
-   if (key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
-      /* this isn't really a env stage delivering a color and handled elsewhere */
-      return get_source(p, SRC_PREVIOUS, 0);
-   }
    
    switch (key->unit[unit].ModeRGB) {
    case MODE_DOT3_RGB_EXT:
@@ -826,7 +803,7 @@ emit_texenv(texenv_fragment_program *p, GLuint unit)
                         key->unit[unit].NumArgsRGB,
                         key->unit[unit].ModeRGB,
                         key->unit[unit].OptRGB);
-      val = smear(p, val);
+      val = smear(val);
       if (rgb_saturate)
         val = saturate(val);
 
@@ -838,7 +815,7 @@ emit_texenv(texenv_fragment_program *p, GLuint unit)
                                    key->unit[unit].NumArgsRGB,
                                    key->unit[unit].ModeRGB,
                                    key->unit[unit].OptRGB);
-      val = smear(p, val);
+      val = smear(val);
       if (rgb_saturate)
         val = saturate(val);
       p->emit(assign(temp_var, val));
@@ -851,7 +828,7 @@ emit_texenv(texenv_fragment_program *p, GLuint unit)
                         key->unit[unit].NumArgsRGB,
                         key->unit[unit].ModeRGB,
                         key->unit[unit].OptRGB);
-      val = swizzle_xyz(smear(p, val));
+      val = swizzle_xyz(smear(val));
       if (rgb_saturate)
         val = saturate(val);
       p->emit(assign(temp_var, val, WRITEMASK_XYZ));
@@ -860,7 +837,7 @@ emit_texenv(texenv_fragment_program *p, GLuint unit)
                         key->unit[unit].NumArgsA,
                         key->unit[unit].ModeA,
                         key->unit[unit].OptA);
-      val = swizzle_w(smear(p, val));
+      val = swizzle_w(smear(val));
       if (alpha_saturate)
         val = saturate(val);
       p->emit(assign(temp_var, val, WRITEMASK_W));
@@ -877,14 +854,15 @@ emit_texenv(texenv_fragment_program *p, GLuint unit)
         shift = new(p->mem_ctx) ir_constant((float)(1 << rgb_shift));
       }
       else {
-        float const_data[4] = {
-           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);
+         ir_constant_data const_data;
+
+         const_data.f[0] = float(1 << rgb_shift);
+         const_data.f[1] = float(1 << rgb_shift);
+         const_data.f[2] = float(1 << rgb_shift);
+         const_data.f[3] = float(1 << alpha_shift);
+
+         shift = new(p->mem_ctx) ir_constant(glsl_type::vec4_type,
+                                             &const_data);
       }
 
       return saturate(mul(deref, shift));
@@ -917,7 +895,7 @@ static void load_texture( texenv_fragment_program *p, GLuint unit )
       texcoord = new(p->mem_ctx) ir_dereference_variable(tc_array);
       ir_rvalue *index = new(p->mem_ctx) ir_constant(unit);
       texcoord = new(p->mem_ctx) ir_dereference_array(texcoord, index);
-      tc_array->data.max_array_access = MAX2(tc_array->data.max_array_access, unit);
+      tc_array->data.max_array_access = MAX2(tc_array->data.max_array_access, (int)unit);
    }
 
    if (!p->state->unit[unit].enabled) {
@@ -935,54 +913,54 @@ static void load_texture( texenv_fragment_program *p, GLuint unit )
    switch (texTarget) {
    case TEXTURE_1D_INDEX:
       if (p->state->unit[unit].shadow)
-        sampler_type = p->shader->symbols->get_type("sampler1DShadow");
+        sampler_type = glsl_type::sampler1DShadow_type;
       else
-        sampler_type = p->shader->symbols->get_type("sampler1D");
+        sampler_type = glsl_type::sampler1D_type;
       coords = 1;
       break;
    case TEXTURE_1D_ARRAY_INDEX:
       if (p->state->unit[unit].shadow)
-        sampler_type = p->shader->symbols->get_type("sampler1DArrayShadow");
+        sampler_type = glsl_type::sampler1DArrayShadow_type;
       else
-        sampler_type = p->shader->symbols->get_type("sampler1DArray");
+        sampler_type = glsl_type::sampler1DArray_type;
       coords = 2;
       break;
    case TEXTURE_2D_INDEX:
       if (p->state->unit[unit].shadow)
-        sampler_type = p->shader->symbols->get_type("sampler2DShadow");
+        sampler_type = glsl_type::sampler2DShadow_type;
       else
-        sampler_type = p->shader->symbols->get_type("sampler2D");
+        sampler_type = glsl_type::sampler2D_type;
       coords = 2;
       break;
    case TEXTURE_2D_ARRAY_INDEX:
       if (p->state->unit[unit].shadow)
-        sampler_type = p->shader->symbols->get_type("sampler2DArrayShadow");
+        sampler_type = glsl_type::sampler2DArrayShadow_type;
       else
-        sampler_type = p->shader->symbols->get_type("sampler2DArray");
+        sampler_type = glsl_type::sampler2DArray_type;
       coords = 3;
       break;
    case TEXTURE_RECT_INDEX:
       if (p->state->unit[unit].shadow)
-        sampler_type = p->shader->symbols->get_type("sampler2DRectShadow");
+        sampler_type = glsl_type::sampler2DRectShadow_type;
       else
-        sampler_type = p->shader->symbols->get_type("sampler2DRect");
+        sampler_type = glsl_type::sampler2DRect_type;
       coords = 2;
       break;
    case TEXTURE_3D_INDEX:
       assert(!p->state->unit[unit].shadow);
-      sampler_type = p->shader->symbols->get_type("sampler3D");
+      sampler_type = glsl_type::sampler3D_type;
       coords = 3;
       break;
    case TEXTURE_CUBE_INDEX:
       if (p->state->unit[unit].shadow)
-        sampler_type = p->shader->symbols->get_type("samplerCubeShadow");
+        sampler_type = glsl_type::samplerCubeShadow_type;
       else
-        sampler_type = p->shader->symbols->get_type("samplerCube");
+        sampler_type = glsl_type::samplerCube_type;
       coords = 3;
       break;
    case TEXTURE_EXTERNAL_INDEX:
       assert(!p->state->unit[unit].shadow);
-      sampler_type = p->shader->symbols->get_type("samplerExternalOES");
+      sampler_type = glsl_type::samplerExternalOES_type;
       coords = 2;
       break;
    }
@@ -999,13 +977,11 @@ static void load_texture( texenv_fragment_program *p, GLuint unit )
                                                      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.
+   /* Set the texture unit for this sampler in the same way that
+    * layout(binding=X) would.
     */
-   sampler->constant_value = new(p->mem_ctx) ir_constant(int(unit));
+   sampler->data.explicit_binding = true;
+   sampler->data.binding = unit;
 
    deref = new(p->mem_ctx) ir_dereference_variable(sampler);
    tex->set_sampler(deref, glsl_type::vec4_type);
@@ -1014,7 +990,7 @@ static void load_texture( texenv_fragment_program *p, GLuint unit )
 
    if (p->state->unit[unit].shadow) {
       texcoord = texcoord->clone(p->mem_ctx, NULL);
-      tex->shadow_comparitor = new(p->mem_ctx) ir_swizzle(texcoord,
+      tex->shadow_comparator = new(p->mem_ctx) ir_swizzle(texcoord,
                                                          coords, 0, 0, 0,
                                                          1);
       coords++;
@@ -1073,56 +1049,6 @@ load_texunit_sources( texenv_fragment_program *p, GLuint unit )
    return GL_TRUE;
 }
 
-/**
- * Generate instructions for loading bump map textures.
- */
-static void
-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;
-   ir_rvalue *bump;
-   ir_rvalue *texcoord;
-   ir_variable *rot_mat_0, *rot_mat_1;
-
-   rot_mat_0 = p->shader->symbols->get_variable("gl_BumpRotMatrix0MESA");
-   assert(rot_mat_0);
-   rot_mat_1 = p->shader->symbols->get_variable("gl_BumpRotMatrix1MESA");
-   assert(rot_mat_1);
-
-   ir_variable *tc_array = p->shader->symbols->get_variable("gl_TexCoord");
-   assert(tc_array);
-   texcoord = new(p->mem_ctx) ir_dereference_variable(tc_array);
-   ir_rvalue *index = new(p->mem_ctx) ir_constant(bumpedUnitNr);
-   texcoord = new(p->mem_ctx) ir_dereference_array(texcoord, index);
-   tc_array->data.max_array_access = MAX2(tc_array->data.max_array_access, unit);
-
-   load_texenv_source( p, unit + SRC_TEXTURE0, unit );
-
-   /* Apply rot matrix and add coords to be available in next phase.
-    * dest = Arg1 + (Arg0.xx * rotMat0) + (Arg0.yy * rotMat1)
-    * note only 2 coords are affected the rest are left unchanged (mul by 0)
-    */
-   ir_rvalue *bump_x, *bump_y;
-
-   texcoord = smear(p, texcoord);
-
-   /* bump_texcoord = texcoord */
-   ir_variable *bumped = p->make_temp(texcoord->type, "bump_texcoord");
-   p->emit(bumped);
-   p->emit(assign(bumped, texcoord));
-
-   /* bump_texcoord.xy += arg0.x * rotmat0 + arg0.y * rotmat1 */
-   bump = get_source(p, key->unit[unit].OptRGB[0].Source, unit);
-   bump_x = mul(swizzle_x(bump), rot_mat_0);
-   bump_y = mul(swizzle_y(bump->clone(p->mem_ctx, NULL)), rot_mat_1);
-
-   p->emit(assign(bumped, add(swizzle_xy(bumped), add(bump_x, bump_y)),
-                 WRITEMASK_XY));
-
-   p->texcoord_tex[bumpedUnitNr] = bumped;
-}
-
 /**
  * Applies the fog calculations.
  *
@@ -1213,14 +1139,6 @@ emit_instructions(texenv_fragment_program *p)
    GLuint unit;
 
    if (key->enabled_units) {
-      /* Zeroth pass - bump map textures first */
-      for (unit = 0; unit < key->nr_enabled_units; unit++) {
-        if (key->unit[unit].enabled &&
-             key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
-           load_texunit_bumpmap(p, unit);
-        }
-      }
-
       /* First pass - to support texture_env_crossbar, first identify
        * all referenced texture sources and emit texld instructions
        * for each:
@@ -1262,7 +1180,7 @@ emit_instructions(texenv_fragment_program *p)
       cf = new(p->mem_ctx) ir_dereference_variable(spec_result);
    }
 
-   if (key->fog_enabled) {
+   if (key->fog_mode) {
       cf = emit_fog_instructions(p, cf);
    }
 
@@ -1283,7 +1201,10 @@ create_new_program(struct gl_context *ctx, struct state_key *key)
    _mesa_glsl_parse_state *state;
 
    p.mem_ctx = ralloc_context(NULL);
-   p.shader = ctx->Driver.NewShader(ctx, 0, GL_FRAGMENT_SHADER);
+   p.shader = _mesa_new_shader(0, MESA_SHADER_FRAGMENT);
+#ifdef DEBUG
+   p.shader->SourceChecksum = 0xf18ed; /* fixed */
+#endif
    p.shader->ir = new(p.shader) exec_list;
    state = new(p.shader) _mesa_glsl_parse_state(ctx, MESA_SHADER_FRAGMENT,
                                                p.shader);
@@ -1291,7 +1212,7 @@ create_new_program(struct gl_context *ctx, struct state_key *key)
    p.top_instructions = p.shader->ir;
    p.instructions = p.shader->ir;
    p.state = key;
-   p.shader_program = ctx->Driver.NewShaderProgram(ctx, 0);
+   p.shader_program = _mesa_new_shader_program(0);
 
    /* Tell the linker to ignore the fact that we're building a
     * separate shader, in case we're in a GLES2 context that would
@@ -1299,7 +1220,7 @@ create_new_program(struct gl_context *ctx, struct state_key *key)
     * fixed function program in a GLES2 context at all, but that's a
     * big mess to clean up.
     */
-   p.shader_program->InternalSeparateShader = GL_TRUE;
+   p.shader_program->SeparateShader = GL_TRUE;
 
    state->language_version = 130;
    state->es_shader = false;
@@ -1320,7 +1241,7 @@ create_new_program(struct gl_context *ctx, struct state_key *key)
    state->symbols->add_function(main_f);
 
    ir_function_signature *main_sig =
-      new(p.mem_ctx) ir_function_signature(p.shader->symbols->get_type("void"));
+      new(p.mem_ctx) ir_function_signature(glsl_type::void_type);
    main_sig->is_defined = true;
    main_f->add_signature(main_sig);
 
@@ -1331,16 +1252,19 @@ create_new_program(struct gl_context *ctx, struct state_key *key)
    validate_ir_tree(p.shader->ir);
 
    const struct gl_shader_compiler_options *options =
-      &ctx->ShaderCompilerOptions[MESA_SHADER_FRAGMENT];
+      &ctx->Const.ShaderCompilerOptions[MESA_SHADER_FRAGMENT];
+
+   /* Conservative approach: Don't optimize here, the linker does it too. */
+   if (!ctx->Const.GLSLOptimizeConservatively) {
+      while (do_common_optimization(p.shader->ir, false, false, options,
+                                    ctx->Const.NativeIntegers))
+         ;
+   }
 
-   while (do_common_optimization(p.shader->ir, false, false, options,
-                                 ctx->Const.NativeIntegers))
-      ;
    reparent_ir(p.shader->ir, p.shader->ir);
 
    p.shader->CompileStatus = true;
    p.shader->Version = state->language_version;
-   p.shader->uses_builtin_functions = state->uses_builtin_functions;
    p.shader_program->Shaders =
       (gl_shader **)malloc(sizeof(*p.shader_program->Shaders));
    p.shader_program->Shaders[0] = p.shader;
@@ -1348,9 +1272,9 @@ create_new_program(struct gl_context *ctx, struct state_key *key)
 
    _mesa_glsl_link_shader(ctx, p.shader_program);
 
-   if (!p.shader_program->LinkStatus)
+   if (!p.shader_program->data->LinkStatus)
       _mesa_problem(ctx, "Failed to link fixed function fragment shader: %s\n",
-                   p.shader_program->InfoLog);
+                    p.shader_program->data->InfoLog);
 
    ralloc_free(p.mem_ctx);
    return p.shader_program;