~ubuntu-branches/ubuntu/precise/mesa/precise-updates

« back to all changes in this revision

Viewing changes to src/mesa/program/ir_to_mesa.cpp

  • Committer: Package Import Robot
  • Author(s): Robert Hooker
  • Date: 2012-02-02 12:05:48 UTC
  • mfrom: (1.7.1) (3.3.27 sid)
  • Revision ID: package-import@ubuntu.com-20120202120548-nvkma85jq0h4coix
Tags: 8.0~rc2-0ubuntu4
Drop drisearchdir handling, it is no longer needed with multiarch
and dri-alternates being removed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
#include "ir_visitor.h"
36
36
#include "ir_print_visitor.h"
37
37
#include "ir_expression_flattening.h"
 
38
#include "ir_uniform.h"
38
39
#include "glsl_types.h"
39
40
#include "glsl_parser_extras.h"
40
41
#include "../glsl/program.h"
41
42
#include "ir_optimization.h"
42
43
#include "ast.h"
 
44
#include "linker.h"
43
45
 
44
 
extern "C" {
45
46
#include "main/mtypes.h"
 
47
#include "main/shaderobj.h"
 
48
#include "program/hash_table.h"
 
49
 
 
50
extern "C" {
46
51
#include "main/shaderapi.h"
47
 
#include "main/shaderobj.h"
48
52
#include "main/uniforms.h"
49
 
#include "program/hash_table.h"
50
53
#include "program/prog_instruction.h"
51
54
#include "program/prog_optimize.h"
52
55
#include "program/prog_print.h"
53
56
#include "program/program.h"
54
 
#include "program/prog_uniform.h"
55
57
#include "program/prog_parameter.h"
56
58
#include "program/sampler.h"
57
59
}
297
299
   /**
298
300
    * Emit the correct dot-product instruction for the type of arguments
299
301
    */
300
 
   void emit_dp(ir_instruction *ir,
301
 
                dst_reg dst,
302
 
                src_reg src0,
303
 
                src_reg src1,
304
 
                unsigned elements);
 
302
   ir_to_mesa_instruction * emit_dp(ir_instruction *ir,
 
303
                                    dst_reg dst,
 
304
                                    src_reg src0,
 
305
                                    src_reg src1,
 
306
                                    unsigned elements);
305
307
 
306
308
   void emit_scalar(ir_instruction *ir, enum prog_opcode op,
307
309
                    dst_reg dst, src_reg src0);
312
314
   void emit_scs(ir_instruction *ir, enum prog_opcode op,
313
315
                 dst_reg dst, const src_reg &src);
314
316
 
315
 
   GLboolean try_emit_mad(ir_expression *ir,
 
317
   bool try_emit_mad(ir_expression *ir,
316
318
                          int mul_operand);
317
 
   GLboolean try_emit_sat(ir_expression *ir);
 
319
   bool try_emit_mad_for_and_not(ir_expression *ir,
 
320
                                 int mul_operand);
 
321
   bool try_emit_sat(ir_expression *ir);
318
322
 
319
323
   void emit_swz(ir_expression *ir);
320
324
 
331
335
 
332
336
dst_reg address_reg = dst_reg(PROGRAM_ADDRESS, WRITEMASK_X);
333
337
 
334
 
static void
335
 
fail_link(struct gl_shader_program *prog, const char *fmt, ...) PRINTFLIKE(2, 3);
336
 
 
337
 
static void
338
 
fail_link(struct gl_shader_program *prog, const char *fmt, ...)
339
 
{
340
 
   va_list args;
341
 
   va_start(args, fmt);
342
 
   ralloc_vasprintf_append(&prog->InfoLog, fmt, args);
343
 
   va_end(args);
344
 
 
345
 
   prog->LinkStatus = GL_FALSE;
346
 
}
347
 
 
348
338
static int
349
339
swizzle_for_size(int size)
350
340
{
422
412
   return emit(ir, op, undef_dst, undef_src, undef_src, undef_src);
423
413
}
424
414
 
425
 
void
 
415
ir_to_mesa_instruction *
426
416
ir_to_mesa_visitor::emit_dp(ir_instruction *ir,
427
417
                            dst_reg dst, src_reg src0, src_reg src1,
428
418
                            unsigned elements)
431
421
      OPCODE_DP2, OPCODE_DP3, OPCODE_DP4
432
422
   };
433
423
 
434
 
   emit(ir, dot_opcodes[elements - 2], dst, src0, src1);
 
424
   return emit(ir, dot_opcodes[elements - 2], dst, src0, src1);
435
425
}
436
426
 
437
427
/**
593
583
   }
594
584
}
595
585
 
596
 
struct src_reg
 
586
src_reg
597
587
ir_to_mesa_visitor::src_reg_for_float(float val)
598
588
{
599
589
   src_reg src(PROGRAM_CONSTANT, -1, NULL);
600
590
 
601
591
   src.index = _mesa_add_unnamed_constant(this->prog->Parameters,
602
 
                                          &val, 1, &src.swizzle);
 
592
                                          (const gl_constant_value *)&val, 1, &src.swizzle);
603
593
 
604
594
   return src;
605
595
}
655
645
ir_to_mesa_visitor::get_temp(const glsl_type *type)
656
646
{
657
647
   src_reg src;
658
 
   int swizzle[4];
659
 
   int i;
660
648
 
661
649
   src.file = PROGRAM_TEMPORARY;
662
650
   src.index = next_temp;
666
654
   if (type->is_array() || type->is_record()) {
667
655
      src.swizzle = SWIZZLE_NOOP;
668
656
   } else {
669
 
      for (i = 0; i < type->vector_elements; i++)
670
 
         swizzle[i] = i;
671
 
      for (; i < 4; i++)
672
 
         swizzle[i] = type->vector_elements - 1;
673
 
      src.swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
674
 
                                  swizzle[2], swizzle[3]);
 
657
      src.swizzle = swizzle_for_size(type->vector_elements);
675
658
   }
676
659
   src.negate = 0;
677
660
 
702
685
 
703
686
      fp->OriginUpperLeft = ir->origin_upper_left;
704
687
      fp->PixelCenterInteger = ir->pixel_center_integer;
705
 
 
706
 
   } else if (strcmp(ir->name, "gl_FragDepth") == 0) {
707
 
      struct gl_fragment_program *fp = (struct gl_fragment_program *)this->prog;
708
 
      switch (ir->depth_layout) {
709
 
      case ir_depth_layout_none:
710
 
         fp->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
711
 
         break;
712
 
      case ir_depth_layout_any:
713
 
         fp->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
714
 
         break;
715
 
      case ir_depth_layout_greater:
716
 
         fp->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
717
 
         break;
718
 
      case ir_depth_layout_less:
719
 
         fp->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
720
 
         break;
721
 
      case ir_depth_layout_unchanged:
722
 
         fp->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
723
 
         break;
724
 
      default:
725
 
         assert(0);
726
 
         break;
727
 
      }
728
688
   }
729
689
 
730
690
   if (ir->mode == ir_var_uniform && strncmp(ir->name, "gl_", 3) == 0) {
744
704
         }
745
705
      }
746
706
 
747
 
      struct variable_storage *storage;
 
707
      variable_storage *storage;
748
708
      dst_reg dst;
749
709
      if (i == ir->num_state_slots) {
750
710
         /* We'll set the index later. */
789
749
 
790
750
      if (storage->file == PROGRAM_TEMPORARY &&
791
751
          dst.index != storage->index + (int) ir->num_state_slots) {
792
 
         fail_link(this->shader_program,
793
 
                   "failed to load builtin uniform `%s'  (%d/%d regs loaded)\n",
794
 
                   ir->name, dst.index - storage->index,
795
 
                   type_size(ir->type));
 
752
         linker_error(this->shader_program,
 
753
                      "failed to load builtin uniform `%s' "
 
754
                      "(%d/%d regs loaded)\n",
 
755
                      ir->name, dst.index - storage->index,
 
756
                      type_size(ir->type));
796
757
      }
797
758
   }
798
759
}
889
850
   }
890
851
}
891
852
 
892
 
GLboolean
 
853
bool
893
854
ir_to_mesa_visitor::try_emit_mad(ir_expression *ir, int mul_operand)
894
855
{
895
856
   int nonmul_operand = 1 - mul_operand;
912
873
   return true;
913
874
}
914
875
 
915
 
GLboolean
 
876
/**
 
877
 * Emit OPCODE_MAD(a, -b, a) instead of AND(a, NOT(b))
 
878
 *
 
879
 * The logic values are 1.0 for true and 0.0 for false.  Logical-and is
 
880
 * implemented using multiplication, and logical-or is implemented using
 
881
 * addition.  Logical-not can be implemented as (true - x), or (1.0 - x).
 
882
 * As result, the logical expression (a & !b) can be rewritten as:
 
883
 *
 
884
 *     - a * !b
 
885
 *     - a * (1 - b)
 
886
 *     - (a * 1) - (a * b)
 
887
 *     - a + -(a * b)
 
888
 *     - a + (a * -b)
 
889
 *
 
890
 * This final expression can be implemented as a single MAD(a, -b, a)
 
891
 * instruction.
 
892
 */
 
893
bool
 
894
ir_to_mesa_visitor::try_emit_mad_for_and_not(ir_expression *ir, int try_operand)
 
895
{
 
896
   const int other_operand = 1 - try_operand;
 
897
   src_reg a, b;
 
898
 
 
899
   ir_expression *expr = ir->operands[try_operand]->as_expression();
 
900
   if (!expr || expr->operation != ir_unop_logic_not)
 
901
      return false;
 
902
 
 
903
   ir->operands[other_operand]->accept(this);
 
904
   a = this->result;
 
905
   expr->operands[0]->accept(this);
 
906
   b = this->result;
 
907
 
 
908
   b.negate = ~b.negate;
 
909
 
 
910
   this->result = get_temp(ir->type);
 
911
   emit(ir, OPCODE_MAD, dst_reg(this->result), a, b, a);
 
912
 
 
913
   return true;
 
914
}
 
915
 
 
916
bool
916
917
ir_to_mesa_visitor::try_emit_sat(ir_expression *ir)
917
918
{
918
919
   /* Saturates were only introduced to vertex programs in
928
929
   sat_src->accept(this);
929
930
   src_reg src = this->result;
930
931
 
931
 
   this->result = get_temp(ir->type);
932
 
   ir_to_mesa_instruction *inst;
933
 
   inst = emit(ir, OPCODE_MOV, dst_reg(this->result), src);
934
 
   inst->saturate = true;
 
932
   /* If we generated an expression instruction into a temporary in
 
933
    * processing the saturate's operand, apply the saturate to that
 
934
    * instruction.  Otherwise, generate a MOV to do the saturate.
 
935
    *
 
936
    * Note that we have to be careful to only do this optimization if
 
937
    * the instruction in question was what generated src->result.  For
 
938
    * example, ir_dereference_array might generate a MUL instruction
 
939
    * to create the reladdr, and return us a src reg using that
 
940
    * reladdr.  That MUL result is not the value we're trying to
 
941
    * saturate.
 
942
    */
 
943
   ir_expression *sat_src_expr = sat_src->as_expression();
 
944
   ir_to_mesa_instruction *new_inst;
 
945
   new_inst = (ir_to_mesa_instruction *)this->instructions.get_tail();
 
946
   if (sat_src_expr && (sat_src_expr->operation == ir_binop_mul ||
 
947
                        sat_src_expr->operation == ir_binop_add ||
 
948
                        sat_src_expr->operation == ir_binop_dot)) {
 
949
      new_inst->saturate = true;
 
950
   } else {
 
951
      this->result = get_temp(ir->type);
 
952
      ir_to_mesa_instruction *inst;
 
953
      inst = emit(ir, OPCODE_MOV, dst_reg(this->result), src);
 
954
      inst->saturate = true;
 
955
   }
935
956
 
936
957
   return true;
937
958
}
1088
1109
      if (try_emit_mad(ir, 0))
1089
1110
         return;
1090
1111
   }
 
1112
 
 
1113
   /* Quick peephole: Emit OPCODE_MAD(-a, -b, a) instead of AND(a, NOT(b))
 
1114
    */
 
1115
   if (ir->operation == ir_binop_logic_and) {
 
1116
      if (try_emit_mad_for_and_not(ir, 1))
 
1117
         return;
 
1118
      if (try_emit_mad_for_and_not(ir, 0))
 
1119
         return;
 
1120
   }
 
1121
 
1091
1122
   if (try_emit_sat(ir))
1092
1123
      return;
1093
1124
 
1135
1166
 
1136
1167
   switch (ir->operation) {
1137
1168
   case ir_unop_logic_not:
1138
 
      emit(ir, OPCODE_SEQ, result_dst, op[0], src_reg_for_float(0.0));
 
1169
      /* Previously 'SEQ dst, src, 0.0' was used for this.  However, many
 
1170
       * older GPUs implement SEQ using multiple instructions (i915 uses two
 
1171
       * SGE instructions and a MUL instruction).  Since our logic values are
 
1172
       * 0.0 and 1.0, 1-x also implements !x.
 
1173
       */
 
1174
      op[0].negate = ~op[0].negate;
 
1175
      emit(ir, OPCODE_ADD, result_dst, op[0], src_reg_for_float(1.0));
1139
1176
      break;
1140
1177
   case ir_unop_neg:
1141
1178
      op[0].negate = ~op[0].negate;
1203
1240
      break;
1204
1241
   case ir_binop_div:
1205
1242
      assert(!"not reached: should be handled by ir_div_to_mul_rcp");
 
1243
      break;
1206
1244
   case ir_binop_mod:
1207
 
      assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
 
1245
      /* Floating point should be lowered by MOD_TO_FRACT in the compiler. */
 
1246
      assert(ir->type->is_integer());
 
1247
      emit(ir, OPCODE_MUL, result_dst, op[0], op[1]);
1208
1248
      break;
1209
1249
 
1210
1250
   case ir_binop_less:
1231
1271
          ir->operands[1]->type->is_vector()) {
1232
1272
         src_reg temp = get_temp(glsl_type::vec4_type);
1233
1273
         emit(ir, OPCODE_SNE, dst_reg(temp), op[0], op[1]);
 
1274
 
 
1275
         /* After the dot-product, the value will be an integer on the
 
1276
          * range [0,4].  Zero becomes 1.0, and positive values become zero.
 
1277
          */
1234
1278
         emit_dp(ir, result_dst, temp, temp, vector_elements);
1235
 
         emit(ir, OPCODE_SEQ, result_dst, result_src, src_reg_for_float(0.0));
 
1279
 
 
1280
         /* Negating the result of the dot-product gives values on the range
 
1281
          * [-4, 0].  Zero becomes 1.0, and negative values become zero.  This
 
1282
          * achieved using SGE.
 
1283
          */
 
1284
         src_reg sge_src = result_src;
 
1285
         sge_src.negate = ~sge_src.negate;
 
1286
         emit(ir, OPCODE_SGE, result_dst, sge_src, src_reg_for_float(0.0));
1236
1287
      } else {
1237
1288
         emit(ir, OPCODE_SEQ, result_dst, op[0], op[1]);
1238
1289
      }
1243
1294
          ir->operands[1]->type->is_vector()) {
1244
1295
         src_reg temp = get_temp(glsl_type::vec4_type);
1245
1296
         emit(ir, OPCODE_SNE, dst_reg(temp), op[0], op[1]);
1246
 
         emit_dp(ir, result_dst, temp, temp, vector_elements);
1247
 
         emit(ir, OPCODE_SNE, result_dst, result_src, src_reg_for_float(0.0));
 
1297
 
 
1298
         /* After the dot-product, the value will be an integer on the
 
1299
          * range [0,4].  Zero stays zero, and positive values become 1.0.
 
1300
          */
 
1301
         ir_to_mesa_instruction *const dp =
 
1302
            emit_dp(ir, result_dst, temp, temp, vector_elements);
 
1303
         if (this->prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
 
1304
            /* The clamping to [0,1] can be done for free in the fragment
 
1305
             * shader with a saturate.
 
1306
             */
 
1307
            dp->saturate = true;
 
1308
         } else {
 
1309
            /* Negating the result of the dot-product gives values on the range
 
1310
             * [-4, 0].  Zero stays zero, and negative values become 1.0.  This
 
1311
             * achieved using SLT.
 
1312
             */
 
1313
            src_reg slt_src = result_src;
 
1314
            slt_src.negate = ~slt_src.negate;
 
1315
            emit(ir, OPCODE_SLT, result_dst, slt_src, src_reg_for_float(0.0));
 
1316
         }
1248
1317
      } else {
1249
1318
         emit(ir, OPCODE_SNE, result_dst, op[0], op[1]);
1250
1319
      }
1251
1320
      break;
1252
1321
 
1253
 
   case ir_unop_any:
 
1322
   case ir_unop_any: {
1254
1323
      assert(ir->operands[0]->type->is_vector());
1255
 
      emit_dp(ir, result_dst, op[0], op[0],
1256
 
              ir->operands[0]->type->vector_elements);
1257
 
      emit(ir, OPCODE_SNE, result_dst, result_src, src_reg_for_float(0.0));
 
1324
 
 
1325
      /* After the dot-product, the value will be an integer on the
 
1326
       * range [0,4].  Zero stays zero, and positive values become 1.0.
 
1327
       */
 
1328
      ir_to_mesa_instruction *const dp =
 
1329
         emit_dp(ir, result_dst, op[0], op[0],
 
1330
                 ir->operands[0]->type->vector_elements);
 
1331
      if (this->prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
 
1332
         /* The clamping to [0,1] can be done for free in the fragment
 
1333
          * shader with a saturate.
 
1334
          */
 
1335
         dp->saturate = true;
 
1336
      } else {
 
1337
         /* Negating the result of the dot-product gives values on the range
 
1338
          * [-4, 0].  Zero stays zero, and negative values become 1.0.  This
 
1339
          * is achieved using SLT.
 
1340
          */
 
1341
         src_reg slt_src = result_src;
 
1342
         slt_src.negate = ~slt_src.negate;
 
1343
         emit(ir, OPCODE_SLT, result_dst, slt_src, src_reg_for_float(0.0));
 
1344
      }
1258
1345
      break;
 
1346
   }
1259
1347
 
1260
1348
   case ir_binop_logic_xor:
1261
1349
      emit(ir, OPCODE_SNE, result_dst, op[0], op[1]);
1262
1350
      break;
1263
1351
 
1264
 
   case ir_binop_logic_or:
1265
 
      /* This could be a saturated add and skip the SNE. */
1266
 
      emit(ir, OPCODE_ADD, result_dst, op[0], op[1]);
1267
 
      emit(ir, OPCODE_SNE, result_dst, result_src, src_reg_for_float(0.0));
 
1352
   case ir_binop_logic_or: {
 
1353
      /* After the addition, the value will be an integer on the
 
1354
       * range [0,2].  Zero stays zero, and positive values become 1.0.
 
1355
       */
 
1356
      ir_to_mesa_instruction *add =
 
1357
         emit(ir, OPCODE_ADD, result_dst, op[0], op[1]);
 
1358
      if (this->prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
 
1359
         /* The clamping to [0,1] can be done for free in the fragment
 
1360
          * shader with a saturate.
 
1361
          */
 
1362
         add->saturate = true;
 
1363
      } else {
 
1364
         /* Negating the result of the addition gives values on the range
 
1365
          * [-2, 0].  Zero stays zero, and negative values become 1.0.  This
 
1366
          * is achieved using SLT.
 
1367
          */
 
1368
         src_reg slt_src = result_src;
 
1369
         slt_src.negate = ~slt_src.negate;
 
1370
         emit(ir, OPCODE_SLT, result_dst, slt_src, src_reg_for_float(0.0));
 
1371
      }
1268
1372
      break;
 
1373
   }
1269
1374
 
1270
1375
   case ir_binop_logic_and:
1271
1376
      /* the bool args are stored as float 0.0 or 1.0, so "mul" gives us "and". */
1292
1397
      emit_scalar(ir, OPCODE_RSQ, result_dst, op[0]);
1293
1398
      break;
1294
1399
   case ir_unop_i2f:
 
1400
   case ir_unop_u2f:
1295
1401
   case ir_unop_b2f:
1296
1402
   case ir_unop_b2i:
 
1403
   case ir_unop_i2u:
 
1404
   case ir_unop_u2i:
1297
1405
      /* Mesa IR lacks types, ints are stored as truncated floats. */
1298
1406
      result_src = op[0];
1299
1407
      break;
1330
1438
      emit_scalar(ir, OPCODE_POW, result_dst, op[0], op[1]);
1331
1439
      break;
1332
1440
 
1333
 
   case ir_unop_bit_not:
1334
 
   case ir_unop_u2f:
 
1441
      /* GLSL 1.30 integer ops are unsupported in Mesa IR, but since
 
1442
       * hardware backends have no way to avoid Mesa IR generation
 
1443
       * even if they don't use it, we need to emit "something" and
 
1444
       * continue.
 
1445
       */
1335
1446
   case ir_binop_lshift:
1336
1447
   case ir_binop_rshift:
1337
1448
   case ir_binop_bit_and:
1338
1449
   case ir_binop_bit_xor:
1339
1450
   case ir_binop_bit_or:
 
1451
      emit(ir, OPCODE_ADD, result_dst, op[0], op[1]);
 
1452
      break;
 
1453
 
 
1454
   case ir_unop_bit_not:
1340
1455
   case ir_unop_round_even:
1341
 
      assert(!"GLSL 1.30 features unsupported");
 
1456
      emit(ir, OPCODE_MOV, result_dst, op[0]);
1342
1457
      break;
1343
1458
 
1344
1459
   case ir_quadop_vector:
1413
1528
      case ir_var_in:
1414
1529
      case ir_var_inout:
1415
1530
         /* The linker assigns locations for varyings and attributes,
1416
 
          * including deprecated builtins (like gl_Color), user-assign
1417
 
          * generic attributes (glBindVertexLocation), and
1418
 
          * user-defined varyings.
 
1531
          * including deprecated builtins (like gl_Color),
 
1532
          * user-assigned generic attributes (glBindVertexLocation),
 
1533
          * and user-defined varyings.
1419
1534
          *
1420
1535
          * FINISHME: We would hit this path for function arguments.  Fix!
1421
1536
          */
1423
1538
         entry = new(mem_ctx) variable_storage(var,
1424
1539
                                               PROGRAM_INPUT,
1425
1540
                                               var->location);
1426
 
         if (this->prog->Target == GL_VERTEX_PROGRAM_ARB &&
1427
 
             var->location >= VERT_ATTRIB_GENERIC0) {
1428
 
            _mesa_add_attribute(this->prog->Attributes,
1429
 
                                var->name,
1430
 
                                _mesa_sizeof_glsl_type(var->type->gl_type),
1431
 
                                var->type->gl_type,
1432
 
                                var->location - VERT_ATTRIB_GENERIC0);
1433
 
         }
1434
1541
         break;
1435
1542
      case ir_var_out:
1436
1543
         assert(var->location != -1);
1806
1913
 
1807
1914
         src = src_reg(PROGRAM_CONSTANT, -1, NULL);
1808
1915
         src.index = _mesa_add_unnamed_constant(this->prog->Parameters,
1809
 
                                                values,
 
1916
                                                (gl_constant_value *) values,
1810
1917
                                                ir->type->vector_elements,
1811
1918
                                                &src.swizzle);
1812
1919
         emit(ir, OPCODE_MOV, mat_column, src);
1844
1951
 
1845
1952
   this->result = src_reg(PROGRAM_CONSTANT, -1, ir->type);
1846
1953
   this->result.index = _mesa_add_unnamed_constant(this->prog->Parameters,
1847
 
                                                   values,
 
1954
                                                   (gl_constant_value *) values,
1848
1955
                                                   ir->type->vector_elements,
1849
1956
                                                   &this->result.swizzle);
1850
1957
}
1979
2086
   ir_to_mesa_instruction *inst = NULL;
1980
2087
   prog_opcode opcode = OPCODE_NOP;
1981
2088
 
1982
 
   ir->coordinate->accept(this);
 
2089
   if (ir->op == ir_txs)
 
2090
      this->result = src_reg_for_float(0.0);
 
2091
   else
 
2092
      ir->coordinate->accept(this);
1983
2093
 
1984
2094
   /* Put our coords in a temp.  We'll need to modify them for shadow,
1985
2095
    * projection, or LOD, so the only case we'd use it as is is if
2003
2113
 
2004
2114
   switch (ir->op) {
2005
2115
   case ir_tex:
 
2116
   case ir_txs:
2006
2117
      opcode = OPCODE_TEX;
2007
2118
      break;
2008
2119
   case ir_txb:
2010
2121
      ir->lod_info.bias->accept(this);
2011
2122
      lod_info = this->result;
2012
2123
      break;
 
2124
   case ir_txf:
 
2125
      /* Pretend to be TXL so the sampler, coordinate, lod are available */
2013
2126
   case ir_txl:
2014
2127
      opcode = OPCODE_TXL;
2015
2128
      ir->lod_info.lod->accept(this);
2022
2135
      ir->lod_info.grad.dPdy->accept(this);
2023
2136
      dy = this->result;
2024
2137
      break;
2025
 
   case ir_txf:
2026
 
      assert(!"GLSL 1.30 features unsupported");
2027
 
      break;
2028
2138
   }
2029
2139
 
 
2140
   const glsl_type *sampler_type = ir->sampler->type;
 
2141
 
2030
2142
   if (ir->projector) {
2031
2143
      if (opcode == OPCODE_TEX) {
2032
2144
         /* Slot the projector in as the last component of the coord. */
2058
2170
            tmp_src = get_temp(glsl_type::vec4_type);
2059
2171
            dst_reg tmp_dst = dst_reg(tmp_src);
2060
2172
 
 
2173
            /* Projective division not allowed for array samplers. */
 
2174
            assert(!sampler_type->sampler_array);
 
2175
 
2061
2176
            tmp_dst.writemask = WRITEMASK_Z;
2062
2177
            emit(ir, OPCODE_MOV, tmp_dst, this->result);
2063
2178
 
2082
2197
       * coord.
2083
2198
       */
2084
2199
      ir->shadow_comparitor->accept(this);
2085
 
      coord_dst.writemask = WRITEMASK_Z;
 
2200
 
 
2201
      /* XXX This will need to be updated for cubemap array samplers. */
 
2202
      if (sampler_type->sampler_dimensionality == GLSL_SAMPLER_DIM_2D &&
 
2203
          sampler_type->sampler_array) {
 
2204
         coord_dst.writemask = WRITEMASK_W;
 
2205
      } else {
 
2206
         coord_dst.writemask = WRITEMASK_Z;
 
2207
      }
 
2208
 
2086
2209
      emit(ir, OPCODE_MOV, coord_dst, this->result);
2087
2210
      coord_dst.writemask = WRITEMASK_XYZW;
2088
2211
   }
2106
2229
                                                   this->shader_program,
2107
2230
                                                   this->prog);
2108
2231
 
2109
 
   const glsl_type *sampler_type = ir->sampler->type;
2110
 
 
2111
2232
   switch (sampler_type->sampler_dimensionality) {
2112
2233
   case GLSL_SAMPLER_DIM_1D:
2113
2234
      inst->tex_target = (sampler_type->sampler_array)
2129
2250
   case GLSL_SAMPLER_DIM_BUF:
2130
2251
      assert(!"FINISHME: Implement ARB_texture_buffer_object");
2131
2252
      break;
 
2253
   case GLSL_SAMPLER_DIM_EXTERNAL:
 
2254
      inst->tex_target = TEXTURE_EXTERNAL_INDEX;
 
2255
      break;
2132
2256
   default:
2133
2257
      assert(!"Should not get here.");
2134
2258
   }
2366
2490
   }
2367
2491
}
2368
2492
 
2369
 
 
2370
 
/**
2371
 
 * Count resources used by the given gpu program (number of texture
2372
 
 * samplers, etc).
2373
 
 */
2374
 
static void
2375
 
count_resources(struct gl_program *prog)
 
2493
class add_uniform_to_shader : public uniform_field_visitor {
 
2494
public:
 
2495
   add_uniform_to_shader(struct gl_shader_program *shader_program,
 
2496
                         struct gl_program_parameter_list *params)
 
2497
      : shader_program(shader_program), params(params)
 
2498
   {
 
2499
      /* empty */
 
2500
   }
 
2501
 
 
2502
   void process(ir_variable *var)
 
2503
   {
 
2504
      this->idx = -1;
 
2505
      this->uniform_field_visitor::process(var);
 
2506
 
 
2507
      var->location = this->idx;
 
2508
   }
 
2509
 
 
2510
private:
 
2511
   virtual void visit_field(const glsl_type *type, const char *name);
 
2512
 
 
2513
   struct gl_shader_program *shader_program;
 
2514
   struct gl_program_parameter_list *params;
 
2515
   int idx;
 
2516
};
 
2517
 
 
2518
void
 
2519
add_uniform_to_shader::visit_field(const glsl_type *type, const char *name)
2376
2520
{
2377
 
   unsigned int i;
2378
 
 
2379
 
   prog->SamplersUsed = 0;
2380
 
 
2381
 
   for (i = 0; i < prog->NumInstructions; i++) {
2382
 
      struct prog_instruction *inst = &prog->Instructions[i];
2383
 
 
2384
 
      if (_mesa_is_tex_instruction(inst->Opcode)) {
2385
 
         prog->SamplerTargets[inst->TexSrcUnit] =
2386
 
            (gl_texture_index)inst->TexSrcTarget;
2387
 
         prog->SamplersUsed |= 1 << inst->TexSrcUnit;
2388
 
         if (inst->TexShadow) {
2389
 
            prog->ShadowSamplers |= 1 << inst->TexSrcUnit;
2390
 
         }
 
2521
   unsigned int size;
 
2522
 
 
2523
   if (type->is_vector() || type->is_scalar()) {
 
2524
      size = type->vector_elements;
 
2525
   } else {
 
2526
      size = type_size(type) * 4;
 
2527
   }
 
2528
 
 
2529
   gl_register_file file;
 
2530
   if (type->is_sampler() ||
 
2531
       (type->is_array() && type->fields.array->is_sampler())) {
 
2532
      file = PROGRAM_SAMPLER;
 
2533
   } else {
 
2534
      file = PROGRAM_UNIFORM;
 
2535
   }
 
2536
 
 
2537
   int index = _mesa_lookup_parameter_index(params, -1, name);
 
2538
   if (index < 0) {
 
2539
      index = _mesa_add_parameter(params, file, name, size, type->gl_type,
 
2540
                                  NULL, NULL, 0x0);
 
2541
 
 
2542
      /* Sampler uniform values are stored in prog->SamplerUnits,
 
2543
       * and the entry in that array is selected by this index we
 
2544
       * store in ParameterValues[].
 
2545
       */
 
2546
      if (file == PROGRAM_SAMPLER) {
 
2547
         unsigned location;
 
2548
         const bool found =
 
2549
            this->shader_program->UniformHash->get(location,
 
2550
                                                   params->Parameters[index].Name);
 
2551
         assert(found);
 
2552
 
 
2553
         if (!found)
 
2554
            return;
 
2555
 
 
2556
         struct gl_uniform_storage *storage =
 
2557
            &this->shader_program->UniformStorage[location];
 
2558
 
 
2559
         for (unsigned int j = 0; j < size / 4; j++)
 
2560
            params->ParameterValues[index + j][0].f = storage->sampler + j;
2391
2561
      }
2392
2562
   }
2393
2563
 
2394
 
   _mesa_update_shader_textures_used(prog);
 
2564
   /* The first part of the uniform that's processed determines the base
 
2565
    * location of the whole uniform (for structures).
 
2566
    */
 
2567
   if (this->idx < 0)
 
2568
      this->idx = index;
2395
2569
}
2396
2570
 
2397
 
 
2398
2571
/**
2399
 
 * Check if the given vertex/fragment/shader program is within the
2400
 
 * resource limits of the context (number of texture units, etc).
2401
 
 * If any of those checks fail, record a linker error.
 
2572
 * Generate the program parameters list for the user uniforms in a shader
2402
2573
 *
2403
 
 * XXX more checks are needed...
2404
 
 */
2405
 
static void
2406
 
check_resources(const struct gl_context *ctx,
2407
 
                struct gl_shader_program *shader_program,
2408
 
                struct gl_program *prog)
2409
 
{
2410
 
   switch (prog->Target) {
2411
 
   case GL_VERTEX_PROGRAM_ARB:
2412
 
      if (_mesa_bitcount(prog->SamplersUsed) >
2413
 
          ctx->Const.MaxVertexTextureImageUnits) {
2414
 
         fail_link(shader_program, "Too many vertex shader texture samplers");
2415
 
      }
2416
 
      if (prog->Parameters->NumParameters > MAX_UNIFORMS) {
2417
 
         fail_link(shader_program, "Too many vertex shader constants");
2418
 
      }
2419
 
      break;
2420
 
   case MESA_GEOMETRY_PROGRAM:
2421
 
      if (_mesa_bitcount(prog->SamplersUsed) >
2422
 
          ctx->Const.MaxGeometryTextureImageUnits) {
2423
 
         fail_link(shader_program, "Too many geometry shader texture samplers");
2424
 
      }
2425
 
      if (prog->Parameters->NumParameters >
2426
 
          MAX_GEOMETRY_UNIFORM_COMPONENTS / 4) {
2427
 
         fail_link(shader_program, "Too many geometry shader constants");
2428
 
      }
2429
 
      break;
2430
 
   case GL_FRAGMENT_PROGRAM_ARB:
2431
 
      if (_mesa_bitcount(prog->SamplersUsed) >
2432
 
          ctx->Const.MaxTextureImageUnits) {
2433
 
         fail_link(shader_program, "Too many fragment shader texture samplers");
2434
 
      }
2435
 
      if (prog->Parameters->NumParameters > MAX_UNIFORMS) {
2436
 
         fail_link(shader_program, "Too many fragment shader constants");
2437
 
      }
2438
 
      break;
2439
 
   default:
2440
 
      _mesa_problem(ctx, "unexpected program type in check_resources()");
2441
 
   }
2442
 
}
2443
 
 
2444
 
 
2445
 
 
2446
 
struct uniform_sort {
2447
 
   struct gl_uniform *u;
2448
 
   int pos;
2449
 
};
2450
 
 
2451
 
/* The shader_program->Uniforms list is almost sorted in increasing
2452
 
 * uniform->{Frag,Vert}Pos locations, but not quite when there are
2453
 
 * uniforms shared between targets.  We need to add parameters in
2454
 
 * increasing order for the targets.
2455
 
 */
2456
 
static int
2457
 
sort_uniforms(const void *a, const void *b)
2458
 
{
2459
 
   struct uniform_sort *u1 = (struct uniform_sort *)a;
2460
 
   struct uniform_sort *u2 = (struct uniform_sort *)b;
2461
 
 
2462
 
   return u1->pos - u2->pos;
2463
 
}
2464
 
 
2465
 
/* Add the uniforms to the parameters.  The linker chose locations
2466
 
 * in our parameters lists (which weren't created yet), which the
2467
 
 * uniforms code will use to poke values into our parameters list
2468
 
 * when uniforms are updated.
2469
 
 */
2470
 
static void
2471
 
add_uniforms_to_parameters_list(struct gl_shader_program *shader_program,
2472
 
                                struct gl_shader *shader,
2473
 
                                struct gl_program *prog)
2474
 
{
2475
 
   unsigned int i;
2476
 
   unsigned int next_sampler = 0, num_uniforms = 0;
2477
 
   struct uniform_sort *sorted_uniforms;
2478
 
 
2479
 
   sorted_uniforms = ralloc_array(NULL, struct uniform_sort,
2480
 
                                  shader_program->Uniforms->NumUniforms);
2481
 
 
2482
 
   for (i = 0; i < shader_program->Uniforms->NumUniforms; i++) {
2483
 
      struct gl_uniform *uniform = shader_program->Uniforms->Uniforms + i;
2484
 
      int parameter_index = -1;
2485
 
 
2486
 
      switch (shader->Type) {
2487
 
      case GL_VERTEX_SHADER:
2488
 
         parameter_index = uniform->VertPos;
2489
 
         break;
2490
 
      case GL_FRAGMENT_SHADER:
2491
 
         parameter_index = uniform->FragPos;
2492
 
         break;
2493
 
      case GL_GEOMETRY_SHADER:
2494
 
         parameter_index = uniform->GeomPos;
2495
 
         break;
2496
 
      }
2497
 
 
2498
 
      /* Only add uniforms used in our target. */
2499
 
      if (parameter_index != -1) {
2500
 
         sorted_uniforms[num_uniforms].pos = parameter_index;
2501
 
         sorted_uniforms[num_uniforms].u = uniform;
2502
 
         num_uniforms++;
2503
 
      }
2504
 
   }
2505
 
 
2506
 
   qsort(sorted_uniforms, num_uniforms, sizeof(struct uniform_sort),
2507
 
         sort_uniforms);
2508
 
 
2509
 
   for (i = 0; i < num_uniforms; i++) {
2510
 
      struct gl_uniform *uniform = sorted_uniforms[i].u;
2511
 
      int parameter_index = sorted_uniforms[i].pos;
2512
 
      const glsl_type *type = uniform->Type;
2513
 
      unsigned int size;
2514
 
 
2515
 
      if (type->is_vector() ||
2516
 
          type->is_scalar()) {
2517
 
         size = type->vector_elements;
2518
 
      } else {
2519
 
         size = type_size(type) * 4;
2520
 
      }
2521
 
 
2522
 
      gl_register_file file;
2523
 
      if (type->is_sampler() ||
2524
 
          (type->is_array() && type->fields.array->is_sampler())) {
2525
 
         file = PROGRAM_SAMPLER;
2526
 
      } else {
2527
 
         file = PROGRAM_UNIFORM;
2528
 
      }
2529
 
 
2530
 
      GLint index = _mesa_lookup_parameter_index(prog->Parameters, -1,
2531
 
                                                 uniform->Name);
2532
 
 
2533
 
      if (index < 0) {
2534
 
         index = _mesa_add_parameter(prog->Parameters, file,
2535
 
                                     uniform->Name, size, type->gl_type,
2536
 
                                     NULL, NULL, 0x0);
2537
 
 
2538
 
         /* Sampler uniform values are stored in prog->SamplerUnits,
2539
 
          * and the entry in that array is selected by this index we
2540
 
          * store in ParameterValues[].
2541
 
          */
2542
 
         if (file == PROGRAM_SAMPLER) {
2543
 
            for (unsigned int j = 0; j < size / 4; j++)
2544
 
               prog->Parameters->ParameterValues[index + j][0] = next_sampler++;
2545
 
         }
2546
 
 
2547
 
         /* The location chosen in the Parameters list here (returned
2548
 
          * from _mesa_add_uniform) has to match what the linker chose.
2549
 
          */
2550
 
         if (index != parameter_index) {
2551
 
            fail_link(shader_program, "Allocation of uniform `%s' to target "
2552
 
                      "failed (%d vs %d)\n",
2553
 
                      uniform->Name, index, parameter_index);
2554
 
         }
2555
 
      }
2556
 
   }
2557
 
 
2558
 
   ralloc_free(sorted_uniforms);
 
2574
 * \param shader_program Linked shader program.  This is only used to
 
2575
 *                       emit possible link errors to the info log.
 
2576
 * \param sh             Shader whose uniforms are to be processed.
 
2577
 * \param params         Parameter list to be filled in.
 
2578
 */
 
2579
void
 
2580
_mesa_generate_parameters_list_for_uniforms(struct gl_shader_program
 
2581
                                            *shader_program,
 
2582
                                            struct gl_shader *sh,
 
2583
                                            struct gl_program_parameter_list
 
2584
                                            *params)
 
2585
{
 
2586
   add_uniform_to_shader add(shader_program, params);
 
2587
 
 
2588
   foreach_list(node, sh->ir) {
 
2589
      ir_variable *var = ((ir_instruction *) node)->as_variable();
 
2590
 
 
2591
      if ((var == NULL) || (var->mode != ir_var_uniform)
 
2592
          || (strncmp(var->name, "gl_", 3) == 0))
 
2593
         continue;
 
2594
 
 
2595
      add.process(var);
 
2596
   }
 
2597
}
 
2598
 
 
2599
void
 
2600
_mesa_associate_uniform_storage(struct gl_context *ctx,
 
2601
                                struct gl_shader_program *shader_program,
 
2602
                                struct gl_program_parameter_list *params)
 
2603
{
 
2604
   /* After adding each uniform to the parameter list, connect the storage for
 
2605
    * the parameter with the tracking structure used by the API for the
 
2606
    * uniform.
 
2607
    */
 
2608
   unsigned last_location = unsigned(~0);
 
2609
   for (unsigned i = 0; i < params->NumParameters; i++) {
 
2610
      if (params->Parameters[i].Type != PROGRAM_UNIFORM)
 
2611
         continue;
 
2612
 
 
2613
      unsigned location;
 
2614
      const bool found =
 
2615
         shader_program->UniformHash->get(location, params->Parameters[i].Name);
 
2616
      assert(found);
 
2617
 
 
2618
      if (!found)
 
2619
         continue;
 
2620
 
 
2621
      if (location != last_location) {
 
2622
         struct gl_uniform_storage *storage =
 
2623
            &shader_program->UniformStorage[location];
 
2624
         enum gl_uniform_driver_format format = uniform_native;
 
2625
 
 
2626
         unsigned columns = 0;
 
2627
         switch (storage->type->base_type) {
 
2628
         case GLSL_TYPE_UINT:
 
2629
            assert(ctx->Const.NativeIntegers);
 
2630
            format = uniform_native;
 
2631
            columns = 1;
 
2632
            break;
 
2633
         case GLSL_TYPE_INT:
 
2634
            format =
 
2635
               (ctx->Const.NativeIntegers) ? uniform_native : uniform_int_float;
 
2636
            columns = 1;
 
2637
            break;
 
2638
         case GLSL_TYPE_FLOAT:
 
2639
            format = uniform_native;
 
2640
            columns = storage->type->matrix_columns;
 
2641
            break;
 
2642
         case GLSL_TYPE_BOOL:
 
2643
            if (ctx->Const.NativeIntegers) {
 
2644
               format = (ctx->Const.UniformBooleanTrue == 1)
 
2645
                  ? uniform_bool_int_0_1 : uniform_bool_int_0_not0;
 
2646
            } else {
 
2647
               format = uniform_bool_float;
 
2648
            }
 
2649
            columns = 1;
 
2650
            break;
 
2651
         case GLSL_TYPE_SAMPLER:
 
2652
            format = uniform_native;
 
2653
            columns = 1;
 
2654
            break;
 
2655
         default:
 
2656
            assert(!"Should not get here.");
 
2657
            break;
 
2658
         }
 
2659
 
 
2660
         _mesa_uniform_attach_driver_storage(storage,
 
2661
                                             4 * sizeof(float) * columns,
 
2662
                                             4 * sizeof(float),
 
2663
                                             format,
 
2664
                                             &params->ParameterValues[i]);
 
2665
         last_location = location;
 
2666
      }
 
2667
   }
2559
2668
}
2560
2669
 
2561
2670
static void
2583
2692
   int loc = _mesa_get_uniform_location(ctx, shader_program, name);
2584
2693
 
2585
2694
   if (loc == -1) {
2586
 
      fail_link(shader_program,
2587
 
                "Couldn't find uniform for initializer %s\n", name);
 
2695
      linker_error(shader_program,
 
2696
                   "Couldn't find uniform for initializer %s\n", name);
2588
2697
      return;
2589
2698
   }
2590
2699
 
2619
2728
                              element_type->matrix_columns,
2620
2729
                              element_type->vector_elements,
2621
2730
                              loc, 1, GL_FALSE, (GLfloat *)values);
2622
 
         loc += element_type->matrix_columns;
2623
2731
      } else {
2624
2732
         _mesa_uniform(ctx, shader_program, loc, element_type->matrix_columns,
2625
2733
                       values, element_type->gl_type);
2626
 
         loc += type_size(element_type);
2627
2734
      }
 
2735
 
 
2736
      loc++;
2628
2737
   }
2629
2738
}
2630
2739
 
2894
3003
   if (!prog)
2895
3004
      return NULL;
2896
3005
   prog->Parameters = _mesa_new_parameter_list();
2897
 
   prog->Varying = _mesa_new_parameter_list();
2898
 
   prog->Attributes = _mesa_new_parameter_list();
2899
3006
   v.ctx = ctx;
2900
3007
   v.prog = prog;
2901
3008
   v.shader_program = shader_program;
2902
3009
   v.options = options;
2903
3010
 
2904
 
   add_uniforms_to_parameters_list(shader_program, shader, prog);
 
3011
   _mesa_generate_parameters_list_for_uniforms(shader_program, shader,
 
3012
                                               prog->Parameters);
2905
3013
 
2906
3014
   /* Emit Mesa IR for main(). */
2907
3015
   visit_exec_list(shader->ir, &v);
2984
3092
         if (mesa_inst->SrcReg[src].RelAddr)
2985
3093
            prog->IndirectRegisterFiles |= 1 << mesa_inst->SrcReg[src].File;
2986
3094
 
2987
 
      if (options->EmitNoIfs && mesa_inst->Opcode == OPCODE_IF) {
2988
 
         fail_link(shader_program, "Couldn't flatten if statement\n");
2989
 
      }
2990
 
 
2991
3095
      switch (mesa_inst->Opcode) {
 
3096
      case OPCODE_IF:
 
3097
         if (options->MaxIfDepth == 0) {
 
3098
            linker_warning(shader_program,
 
3099
                           "Couldn't flatten if-statement.  "
 
3100
                           "This will likely result in software "
 
3101
                           "rasterization.\n");
 
3102
         }
 
3103
         break;
 
3104
      case OPCODE_BGNLOOP:
 
3105
         if (options->EmitNoLoops) {
 
3106
            linker_warning(shader_program,
 
3107
                           "Couldn't unroll loop.  "
 
3108
                           "This will likely result in software "
 
3109
                           "rasterization.\n");
 
3110
         }
 
3111
         break;
 
3112
      case OPCODE_CONT:
 
3113
         if (options->EmitNoCont) {
 
3114
            linker_warning(shader_program,
 
3115
                           "Couldn't lower continue-statement.  "
 
3116
                           "This will likely result in software "
 
3117
                           "rasterization.\n");
 
3118
         }
 
3119
         break;
2992
3120
      case OPCODE_BGNSUB:
2993
3121
         inst->function->inst = i;
2994
3122
         mesa_inst->Comment = strdup(inst->function->sig->function_name());
3014
3142
   }
3015
3143
 
3016
3144
   if (!shader_program->LinkStatus) {
3017
 
      free(mesa_instructions);
3018
 
      _mesa_reference_program(ctx, &shader->Program, NULL);
3019
 
      return NULL;
 
3145
      goto fail_exit;
3020
3146
   }
3021
3147
 
3022
3148
   set_branchtargets(&v, mesa_instructions, num_instructions);
3037
3163
   prog->Instructions = mesa_instructions;
3038
3164
   prog->NumInstructions = num_instructions;
3039
3165
 
3040
 
   do_set_program_inouts(shader->ir, prog);
3041
 
   count_resources(prog);
3042
 
 
3043
 
   check_resources(ctx, shader_program, prog);
 
3166
   /* Setting this to NULL prevents a possible double free in the fail_exit
 
3167
    * path (far below).
 
3168
    */
 
3169
   mesa_instructions = NULL;
 
3170
 
 
3171
   do_set_program_inouts(shader->ir, prog, shader->Type == GL_FRAGMENT_SHADER);
 
3172
 
 
3173
   prog->SamplersUsed = shader->active_samplers;
 
3174
   prog->ShadowSamplers = shader->shadow_samplers;
 
3175
   _mesa_update_shader_textures_used(shader_program, prog);
 
3176
 
 
3177
   /* Set the gl_FragDepth layout. */
 
3178
   if (target == GL_FRAGMENT_PROGRAM_ARB) {
 
3179
      struct gl_fragment_program *fp = (struct gl_fragment_program *)prog;
 
3180
      fp->FragDepthLayout = shader_program->FragDepthLayout;
 
3181
   }
3044
3182
 
3045
3183
   _mesa_reference_program(ctx, &shader->Program, prog);
3046
3184
 
3048
3186
      _mesa_optimize_program(ctx, prog);
3049
3187
   }
3050
3188
 
 
3189
   /* This has to be done last.  Any operation that can cause
 
3190
    * prog->ParameterValues to get reallocated (e.g., anything that adds a
 
3191
    * program constant) has to happen before creating this linkage.
 
3192
    */
 
3193
   _mesa_associate_uniform_storage(ctx, shader_program, prog->Parameters);
 
3194
   if (!shader_program->LinkStatus) {
 
3195
      goto fail_exit;
 
3196
   }
 
3197
 
3051
3198
   return prog;
 
3199
 
 
3200
fail_exit:
 
3201
   free(mesa_instructions);
 
3202
   _mesa_reference_program(ctx, &shader->Program, NULL);
 
3203
   return NULL;
3052
3204
}
3053
3205
 
3054
3206
extern "C" {
3079
3231
         /* Lowering */
3080
3232
         do_mat_op_to_vec(ir);
3081
3233
         lower_instructions(ir, (MOD_TO_FRACT | DIV_TO_MUL_RCP | EXP_TO_EXP2
3082
 
                                 | LOG_TO_LOG2
 
3234
                                 | LOG_TO_LOG2 | INT_DIV_TO_MUL_RCP
3083
3235
                                 | ((options->EmitNoPow) ? POW_TO_EXP2 : 0)));
3084
3236
 
3085
3237
         progress = do_lower_jumps(ir, true, true, options->EmitNoMainReturn, options->EmitNoCont, options->EmitNoLoops) || progress;
3086
3238
 
3087
 
         progress = do_common_optimization(ir, true, options->MaxUnrollIterations) || progress;
 
3239
         progress = do_common_optimization(ir, true, true,
 
3240
                                           options->MaxUnrollIterations)
 
3241
           || progress;
3088
3242
 
3089
3243
         progress = lower_quadop_vector(ir, true) || progress;
3090
3244
 
3091
 
         if (options->EmitNoIfs) {
 
3245
         if (options->MaxIfDepth == 0)
3092
3246
            progress = lower_discard(ir) || progress;
3093
 
            progress = lower_if_to_cond_assign(ir) || progress;
3094
 
         }
 
3247
 
 
3248
         progress = lower_if_to_cond_assign(ir, options->MaxIfDepth) || progress;
3095
3249
 
3096
3250
         if (options->EmitNoNoise)
3097
3251
            progress = lower_noise(ir) || progress;
3124
3278
      linked_prog = get_mesa_program(ctx, prog, prog->_LinkedShaders[i]);
3125
3279
 
3126
3280
      if (linked_prog) {
3127
 
         bool ok = true;
3128
 
 
3129
 
         switch (prog->_LinkedShaders[i]->Type) {
3130
 
         case GL_VERTEX_SHADER:
3131
 
            _mesa_reference_vertprog(ctx, &prog->VertexProgram,
3132
 
                                     (struct gl_vertex_program *)linked_prog);
3133
 
            ok = ctx->Driver.ProgramStringNotify(ctx, GL_VERTEX_PROGRAM_ARB,
3134
 
                                                 linked_prog);
3135
 
            break;
3136
 
         case GL_FRAGMENT_SHADER:
3137
 
            _mesa_reference_fragprog(ctx, &prog->FragmentProgram,
3138
 
                                     (struct gl_fragment_program *)linked_prog);
3139
 
            ok = ctx->Driver.ProgramStringNotify(ctx, GL_FRAGMENT_PROGRAM_ARB,
3140
 
                                                 linked_prog);
3141
 
            break;
3142
 
         case GL_GEOMETRY_SHADER:
3143
 
            _mesa_reference_geomprog(ctx, &prog->GeometryProgram,
3144
 
                                     (struct gl_geometry_program *)linked_prog);
3145
 
            ok = ctx->Driver.ProgramStringNotify(ctx, GL_GEOMETRY_PROGRAM_NV,
3146
 
                                                 linked_prog);
3147
 
            break;
3148
 
         }
3149
 
         if (!ok) {
 
3281
         static const GLenum targets[] = {
 
3282
            GL_VERTEX_PROGRAM_ARB,
 
3283
            GL_FRAGMENT_PROGRAM_ARB,
 
3284
            GL_GEOMETRY_PROGRAM_NV
 
3285
         };
 
3286
 
 
3287
         if (i == MESA_SHADER_VERTEX) {
 
3288
            ((struct gl_vertex_program *)linked_prog)->UsesClipDistance
 
3289
               = prog->Vert.UsesClipDistance;
 
3290
         }
 
3291
 
 
3292
         _mesa_reference_program(ctx, &prog->_LinkedShaders[i]->Program,
 
3293
                                 linked_prog);
 
3294
         if (!ctx->Driver.ProgramStringNotify(ctx, targets[i], linked_prog)) {
3150
3295
            return GL_FALSE;
3151
3296
         }
3152
3297
      }
3154
3299
      _mesa_reference_program(ctx, &linked_prog, NULL);
3155
3300
   }
3156
3301
 
3157
 
   return GL_TRUE;
 
3302
   return prog->LinkStatus;
3158
3303
}
3159
3304
 
3160
3305
 
3202
3347
      /* Do some optimization at compile time to reduce shader IR size
3203
3348
       * and reduce later work if the same shader is linked multiple times
3204
3349
       */
3205
 
      while (do_common_optimization(shader->ir, false, 32))
 
3350
      while (do_common_optimization(shader->ir, false, false, 32))
3206
3351
         ;
3207
3352
 
3208
3353
      validate_ir_tree(shader->ir);
3256
3401
 
3257
3402
   for (i = 0; i < prog->NumShaders; i++) {
3258
3403
      if (!prog->Shaders[i]->CompileStatus) {
3259
 
         fail_link(prog, "linking with uncompiled shader");
 
3404
         linker_error(prog, "linking with uncompiled shader");
3260
3405
         prog->LinkStatus = GL_FALSE;
3261
3406
      }
3262
3407
   }
3263
3408
 
3264
 
   prog->Varying = _mesa_new_parameter_list();
3265
 
   _mesa_reference_vertprog(ctx, &prog->VertexProgram, NULL);
3266
 
   _mesa_reference_fragprog(ctx, &prog->FragmentProgram, NULL);
3267
 
   _mesa_reference_geomprog(ctx, &prog->GeometryProgram, NULL);
3268
 
 
3269
3409
   if (prog->LinkStatus) {
3270
3410
      link_shaders(ctx, prog);
3271
3411
   }
3276
3416
      }
3277
3417
   }
3278
3418
 
3279
 
   set_uniform_initializers(ctx, prog);
 
3419
   if (prog->LinkStatus) {
 
3420
      set_uniform_initializers(ctx, prog);
 
3421
   }
3280
3422
 
3281
3423
   if (ctx->Shader.Flags & GLSL_DUMP) {
3282
3424
      if (!prog->LinkStatus) {