st_glsl_to_tgsi.cpp 168 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * Copyright (C) 2005-2007  Brian Paul   All Rights Reserved.
 * Copyright (C) 2008  VMware, Inc.   All Rights Reserved.
 * Copyright © 2010 Intel Corporation
 * Copyright © 2011 Bryan Cain
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

/**
 * \file glsl_to_tgsi.cpp
 *
30
 * Translate GLSL IR to TGSI.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 */

#include <stdio.h>
#include "main/compiler.h"
#include "ir.h"
#include "ir_visitor.h"
#include "ir_expression_flattening.h"
#include "glsl_types.h"
#include "glsl_parser_extras.h"
#include "../glsl/program.h"
#include "ir_optimization.h"
#include "ast.h"

#include "main/mtypes.h"
#include "main/shaderobj.h"
46
#include "main/uniforms.h"
47
#include "program/hash_table.h"
48 49 50

extern "C" {
#include "main/shaderapi.h"
51 52 53 54 55 56 57 58 59 60 61 62 63 64
#include "program/prog_instruction.h"
#include "program/prog_optimize.h"
#include "program/prog_print.h"
#include "program/program.h"
#include "program/prog_parameter.h"
#include "program/sampler.h"

#include "pipe/p_compiler.h"
#include "pipe/p_context.h"
#include "pipe/p_screen.h"
#include "pipe/p_shader_tokens.h"
#include "pipe/p_state.h"
#include "util/u_math.h"
#include "tgsi/tgsi_ureg.h"
65
#include "tgsi/tgsi_info.h"
66 67 68 69
#include "st_context.h"
#include "st_program.h"
#include "st_glsl_to_tgsi.h"
#include "st_mesa_to_tgsi.h"
70
}
71

72
#define PROGRAM_IMMEDIATE PROGRAM_FILE_MAX
73
#define PROGRAM_ANY_CONST ((1 << PROGRAM_STATE_VAR) |    \
74 75 76
                           (1 << PROGRAM_CONSTANT) |     \
                           (1 << PROGRAM_UNIFORM))

77 78 79 80 81 82
/**
 * Maximum number of temporary registers.
 *
 * It is too big for stack allocated arrays -- it will cause stack overflow on
 * Windows and likely Mac OS X.
 */
83 84
#define MAX_TEMPS         4096

85 86 87 88 89
/**
 * Maximum number of arrays
 */
#define MAX_ARRAYS        256

90 91 92
/* will be 4 for GLSL 4.00 */
#define MAX_GLSL_TEXTURE_OFFSET 1

93 94 95 96 97 98
class st_src_reg;
class st_dst_reg;

static int swizzle_for_size(int size);

/**
99
 * This struct is a corresponding struct to TGSI ureg_src.
100 101 102 103 104 105 106 107 108 109 110 111
 */
class st_src_reg {
public:
   st_src_reg(gl_register_file file, int index, const glsl_type *type)
   {
      this->file = file;
      this->index = index;
      if (type && (type->is_scalar() || type->is_vector() || type->is_matrix()))
         this->swizzle = swizzle_for_size(type->vector_elements);
      else
         this->swizzle = SWIZZLE_XYZW;
      this->negate = 0;
112
      this->index2D = 0;
113
      this->type = type ? type->base_type : GLSL_TYPE_ERROR;
114
      this->reladdr = NULL;
115 116
      this->reladdr2 = NULL;
      this->has_index2 = false;
117 118
   }

119
   st_src_reg(gl_register_file file, int index, int type)
120
   {
121
      this->type = type;
122 123
      this->file = file;
      this->index = index;
124 125 126 127
      this->index2D = 0;
      this->swizzle = SWIZZLE_XYZW;
      this->negate = 0;
      this->reladdr = NULL;
128 129
      this->reladdr2 = NULL;
      this->has_index2 = false;
130 131 132 133 134 135 136 137
   }

   st_src_reg(gl_register_file file, int index, int type, int index2D)
   {
      this->type = type;
      this->file = file;
      this->index = index;
      this->index2D = index2D;
138 139 140
      this->swizzle = SWIZZLE_XYZW;
      this->negate = 0;
      this->reladdr = NULL;
141 142
      this->reladdr2 = NULL;
      this->has_index2 = false;
143 144
   }

145 146
   st_src_reg()
   {
147
      this->type = GLSL_TYPE_ERROR;
148 149
      this->file = PROGRAM_UNDEFINED;
      this->index = 0;
150
      this->index2D = 0;
151 152 153
      this->swizzle = 0;
      this->negate = 0;
      this->reladdr = NULL;
154 155
      this->reladdr2 = NULL;
      this->has_index2 = false;
156 157 158 159 160
   }

   explicit st_src_reg(st_dst_reg reg);

   gl_register_file file; /**< PROGRAM_* from Mesa */
161
   int index; /**< temporary index, VERT_ATTRIB_*, VARYING_SLOT_*, etc. */
162
   int index2D;
163 164
   GLuint swizzle; /**< SWIZZLE_XYZWONEZERO swizzles from Mesa. */
   int negate; /**< NEGATE_XYZW mask from mesa */
165
   int type; /** GLSL_TYPE_* from GLSL IR (enum glsl_base_type) */
166 167
   /** Register index should be offset by the integer in this reg. */
   st_src_reg *reladdr;
168 169
   st_src_reg *reladdr2;
   bool has_index2;
170 171 172 173
};

class st_dst_reg {
public:
174 175 176 177 178 179 180 181 182 183
   st_dst_reg(gl_register_file file, int writemask, int type, int index)
   {
      this->file = file;
      this->index = index;
      this->writemask = writemask;
      this->cond_mask = COND_TR;
      this->reladdr = NULL;
      this->type = type;
   }

184
   st_dst_reg(gl_register_file file, int writemask, int type)
185 186 187 188 189 190
   {
      this->file = file;
      this->index = 0;
      this->writemask = writemask;
      this->cond_mask = COND_TR;
      this->reladdr = NULL;
191
      this->type = type;
192 193 194 195
   }

   st_dst_reg()
   {
196
      this->type = GLSL_TYPE_ERROR;
197 198 199 200 201 202 203 204 205 206
      this->file = PROGRAM_UNDEFINED;
      this->index = 0;
      this->writemask = 0;
      this->cond_mask = COND_TR;
      this->reladdr = NULL;
   }

   explicit st_dst_reg(st_src_reg reg);

   gl_register_file file; /**< PROGRAM_* from Mesa */
207
   int index; /**< temporary index, VERT_ATTRIB_*, VARYING_SLOT_*, etc. */
208 209
   int writemask; /**< Bitfield of WRITEMASK_[XYZW] */
   GLuint cond_mask:4;
210
   int type; /** GLSL_TYPE_* from GLSL IR (enum glsl_base_type) */
211 212 213 214 215 216
   /** Register index should be offset by the integer in this reg. */
   st_src_reg *reladdr;
};

st_src_reg::st_src_reg(st_dst_reg reg)
{
217
   this->type = reg.type;
218 219 220 221
   this->file = reg.file;
   this->index = reg.index;
   this->swizzle = SWIZZLE_XYZW;
   this->negate = 0;
222
   this->reladdr = reg.reladdr;
223
   this->index2D = 0;
224 225
   this->reladdr2 = NULL;
   this->has_index2 = false;
226 227 228 229
}

st_dst_reg::st_dst_reg(st_src_reg reg)
{
230
   this->type = reg.type;
231 232 233 234 235 236 237 238 239
   this->file = reg.file;
   this->index = reg.index;
   this->writemask = WRITEMASK_XYZW;
   this->cond_mask = COND_TR;
   this->reladdr = reg.reladdr;
}

class glsl_to_tgsi_instruction : public exec_node {
public:
240
   DECLARE_RALLOC_CXX_OPERATORS(glsl_to_tgsi_instruction)
241

242
   unsigned op;
243 244 245 246 247 248 249 250 251
   st_dst_reg dst;
   st_src_reg src[3];
   /** Pointer to the ir source this tree came from for debugging */
   ir_instruction *ir;
   GLboolean cond_update;
   bool saturate;
   int sampler; /**< sampler index */
   int tex_target; /**< One of TEXTURE_*_INDEX */
   GLboolean tex_shadow;
252 253
   struct tgsi_texture_offset tex_offsets[MAX_GLSL_TEXTURE_OFFSET];
   unsigned tex_offset_num_offset;
254
   int dead_mask; /**< Used in dead code elimination */
255

256
   class function_entry *function; /* Set on TGSI_OPCODE_CAL or TGSI_OPCODE_BGNSUB */
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
};

class variable_storage : public exec_node {
public:
   variable_storage(ir_variable *var, gl_register_file file, int index)
      : file(file), index(index), var(var)
   {
      /* empty */
   }

   gl_register_file file;
   int index;
   ir_variable *var; /* variable that maps to this, if any */
};

272 273 274 275 276 277 278 279 280 281 282 283 284 285
class immediate_storage : public exec_node {
public:
   immediate_storage(gl_constant_value *values, int size, int type)
   {
      memcpy(this->values, values, size * sizeof(gl_constant_value));
      this->size = size;
      this->type = type;
   }
   
   gl_constant_value values[4];
   int size; /**< Number of components (1-4) */
   int type; /**< GL_FLOAT, GL_INT, GL_BOOL, or GL_UNSIGNED_INT */
};

286 287 288 289 290 291 292
class function_entry : public exec_node {
public:
   ir_function_signature *sig;

   /**
    * identifier of this function signature used by the program.
    *
Bryan Cain's avatar
Bryan Cain committed
293
    * At the point that TGSI instructions for function calls are
294 295 296 297 298 299 300 301 302 303 304 305 306 307
    * generated, we don't know the address of the first instruction of
    * the function body.  So we make the BranchTarget that is called a
    * small integer and rewrite them during set_branchtargets().
    */
   int sig_id;

   /**
    * Pointer to first instruction of the function body.
    *
    * Set during function body emits after main() is processed.
    */
   glsl_to_tgsi_instruction *bgn_inst;

   /**
Bryan Cain's avatar
Bryan Cain committed
308
    * Index of the first instruction of the function body in actual TGSI.
309
    *
Bryan Cain's avatar
Bryan Cain committed
310
    * Set after conversion from glsl_to_tgsi_instruction to TGSI.
311 312 313 314 315 316 317
    */
   int inst;

   /** Storage for the return value. */
   st_src_reg return_reg;
};

318
struct glsl_to_tgsi_visitor : public ir_visitor {
319 320 321 322 323 324 325 326 327 328 329 330
public:
   glsl_to_tgsi_visitor();
   ~glsl_to_tgsi_visitor();

   function_entry *current_function;

   struct gl_context *ctx;
   struct gl_program *prog;
   struct gl_shader_program *shader_program;
   struct gl_shader_compiler_options *options;

   int next_temp;
331

332 333 334
   unsigned array_sizes[MAX_ARRAYS];
   unsigned next_array;

335
   int num_address_regs;
336
   int samplers_used;
337
   bool indirect_addr_consts;
338 339
   
   int glsl_version;
340
   bool native_integers;
341
   bool have_sqrt;
342 343 344

   variable_storage *find_variable_storage(ir_variable *var);

345 346 347
   int add_constant(gl_register_file file, gl_constant_value values[4],
                    int size, int datatype, GLuint *swizzle_out);

348 349 350 351 352 353
   function_entry *get_function_signature(ir_function_signature *sig);

   st_src_reg get_temp(const glsl_type *type);
   void reladdr_to_temp(ir_instruction *ir, st_src_reg *reg, int *num_reladdr);

   st_src_reg st_src_reg_for_float(float val);
354 355
   st_src_reg st_src_reg_for_int(int val);
   st_src_reg st_src_reg_for_type(int type, int val);
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381

   /**
    * \name Visit methods
    *
    * As typical for the visitor pattern, there must be one \c visit method for
    * each concrete subclass of \c ir_instruction.  Virtual base classes within
    * the hierarchy should not have \c visit methods.
    */
   /*@{*/
   virtual void visit(ir_variable *);
   virtual void visit(ir_loop *);
   virtual void visit(ir_loop_jump *);
   virtual void visit(ir_function_signature *);
   virtual void visit(ir_function *);
   virtual void visit(ir_expression *);
   virtual void visit(ir_swizzle *);
   virtual void visit(ir_dereference_variable  *);
   virtual void visit(ir_dereference_array *);
   virtual void visit(ir_dereference_record *);
   virtual void visit(ir_assignment *);
   virtual void visit(ir_constant *);
   virtual void visit(ir_call *);
   virtual void visit(ir_return *);
   virtual void visit(ir_discard *);
   virtual void visit(ir_texture *);
   virtual void visit(ir_if *);
382 383
   virtual void visit(ir_emit_vertex *);
   virtual void visit(ir_end_primitive *);
384 385 386 387 388 389 390
   /*@}*/

   st_src_reg result;

   /** List of variable_storage */
   exec_list variables;

391 392
   /** List of immediate_storage */
   exec_list immediates;
393
   unsigned num_immediates;
394

395 396 397 398 399 400 401
   /** List of function_entry */
   exec_list function_signatures;
   int next_signature_id;

   /** List of glsl_to_tgsi_instruction */
   exec_list instructions;

402
   glsl_to_tgsi_instruction *emit(ir_instruction *ir, unsigned op);
403

404
   glsl_to_tgsi_instruction *emit(ir_instruction *ir, unsigned op,
405 406
        		        st_dst_reg dst, st_src_reg src0);

407
   glsl_to_tgsi_instruction *emit(ir_instruction *ir, unsigned op,
408 409
        		        st_dst_reg dst, st_src_reg src0, st_src_reg src1);

410
   glsl_to_tgsi_instruction *emit(ir_instruction *ir, unsigned op,
411 412
        		        st_dst_reg dst,
        		        st_src_reg src0, st_src_reg src1, st_src_reg src2);
413 414 415 416
   
   unsigned get_opcode(ir_instruction *ir, unsigned op,
                    st_dst_reg dst,
                    st_src_reg src0, st_src_reg src1);
417 418 419 420

   /**
    * Emit the correct dot-product instruction for the type of arguments
    */
421 422 423 424 425
   glsl_to_tgsi_instruction *emit_dp(ir_instruction *ir,
                                     st_dst_reg dst,
                                     st_src_reg src0,
                                     st_src_reg src1,
                                     unsigned elements);
426

427
   void emit_scalar(ir_instruction *ir, unsigned op,
428 429
        	    st_dst_reg dst, st_src_reg src0);

430
   void emit_scalar(ir_instruction *ir, unsigned op,
431 432
        	    st_dst_reg dst, st_src_reg src0, st_src_reg src1);

433 434
   void emit_arl(ir_instruction *ir, st_dst_reg dst, st_src_reg src0);

435
   void emit_scs(ir_instruction *ir, unsigned op,
436 437
        	 st_dst_reg dst, const st_src_reg &src);

438 439
   bool try_emit_mad(ir_expression *ir,
              int mul_operand);
440 441
   bool try_emit_mad_for_and_not(ir_expression *ir,
              int mul_operand);
442
   bool try_emit_sat(ir_expression *ir);
443 444 445 446 447

   void emit_swz(ir_expression *ir);

   bool process_move_condition(ir_rvalue *ir);

448
   void simplify_cmp(void);
449

450 451 452 453 454 455 456 457
   void rename_temp_register(int index, int new_index);
   int get_first_temp_read(int index);
   int get_first_temp_write(int index);
   int get_last_temp_read(int index);
   int get_last_temp_write(int index);

   void copy_propagate(void);
   void eliminate_dead_code(void);
458
   int eliminate_dead_code_advanced(void);
459 460 461
   void merge_registers(void);
   void renumber_registers(void);

462 463 464
   void emit_block_mov(ir_assignment *ir, const struct glsl_type *type,
                       st_dst_reg *l, st_src_reg *r);

465 466 467
   void *mem_ctx;
};

468
static st_src_reg undef_src = st_src_reg(PROGRAM_UNDEFINED, 0, GLSL_TYPE_ERROR);
469

470
static st_dst_reg undef_dst = st_dst_reg(PROGRAM_UNDEFINED, SWIZZLE_NOOP, GLSL_TYPE_ERROR);
471

472 473
static st_dst_reg address_reg = st_dst_reg(PROGRAM_ADDRESS, WRITEMASK_X, GLSL_TYPE_FLOAT, 0);
static st_dst_reg address_reg2 = st_dst_reg(PROGRAM_ADDRESS, WRITEMASK_X, GLSL_TYPE_FLOAT, 1);
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502

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)
{
   int size_swizzles[4] = {
      MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X),
      MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Y, SWIZZLE_Y),
      MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z),
      MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W),
   };

   assert((size >= 1) && (size <= 4));
   return size_swizzles[size - 1];
}

503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
static bool
is_tex_instruction(unsigned opcode)
{
   const tgsi_opcode_info* info = tgsi_get_opcode_info(opcode);
   return info->is_tex;
}

static unsigned
num_inst_dst_regs(unsigned opcode)
{
   const tgsi_opcode_info* info = tgsi_get_opcode_info(opcode);
   return info->num_dst;
}

static unsigned
num_inst_src_regs(unsigned opcode)
{
   const tgsi_opcode_info* info = tgsi_get_opcode_info(opcode);
   return info->is_tex ? info->num_src - 1 : info->num_src;
}

524
glsl_to_tgsi_instruction *
525
glsl_to_tgsi_visitor::emit(ir_instruction *ir, unsigned op,
526 527 528 529 530
        		 st_dst_reg dst,
        		 st_src_reg src0, st_src_reg src1, st_src_reg src2)
{
   glsl_to_tgsi_instruction *inst = new(mem_ctx) glsl_to_tgsi_instruction();
   int num_reladdr = 0, i;
531 532
   
   op = get_opcode(ir, op, dst, src0, src1);
533 534 535 536 537 538

   /* If we have to do relative addressing, we want to load the ARL
    * reg directly for one of the regs, and preload the other reladdr
    * sources into temps.
    */
   num_reladdr += dst.reladdr != NULL;
539 540 541
   num_reladdr += src0.reladdr != NULL || src0.reladdr2 != NULL;
   num_reladdr += src1.reladdr != NULL || src1.reladdr2 != NULL;
   num_reladdr += src2.reladdr != NULL || src2.reladdr2 != NULL;
542 543 544 545 546 547

   reladdr_to_temp(ir, &src2, &num_reladdr);
   reladdr_to_temp(ir, &src1, &num_reladdr);
   reladdr_to_temp(ir, &src0, &num_reladdr);

   if (dst.reladdr) {
548
      emit_arl(ir, address_reg, *dst.reladdr);
549 550 551 552 553 554 555 556 557 558
      num_reladdr--;
   }
   assert(num_reladdr == 0);

   inst->op = op;
   inst->dst = dst;
   inst->src[0] = src0;
   inst->src[1] = src1;
   inst->src[2] = src2;
   inst->ir = ir;
559
   inst->dead_mask = 0;
560 561 562 563 564 565 566 567 568 569 570

   inst->function = NULL;
   
   /* Update indirect addressing status used by TGSI */
   if (dst.reladdr) {
      switch(dst.file) {
      case PROGRAM_STATE_VAR:
      case PROGRAM_CONSTANT:
      case PROGRAM_UNIFORM:
         this->indirect_addr_consts = true;
         break;
571 572 573
      case PROGRAM_IMMEDIATE:
         assert(!"immediates should not have indirect addressing");
         break;
574 575 576 577 578 579 580
      default:
         break;
      }
   }
   else {
      for (i=0; i<3; i++) {
         if(inst->src[i].reladdr) {
581
            switch(inst->src[i].file) {
582 583 584 585 586
            case PROGRAM_STATE_VAR:
            case PROGRAM_CONSTANT:
            case PROGRAM_UNIFORM:
               this->indirect_addr_consts = true;
               break;
587 588 589
            case PROGRAM_IMMEDIATE:
               assert(!"immediates should not have indirect addressing");
               break;
590 591 592 593 594 595 596 597
            default:
               break;
            }
         }
      }
   }

   this->instructions.push_tail(inst);
598

599 600 601 602 603
   return inst;
}


glsl_to_tgsi_instruction *
604
glsl_to_tgsi_visitor::emit(ir_instruction *ir, unsigned op,
605 606 607 608 609 610
        		 st_dst_reg dst, st_src_reg src0, st_src_reg src1)
{
   return emit(ir, op, dst, src0, src1, undef_src);
}

glsl_to_tgsi_instruction *
611
glsl_to_tgsi_visitor::emit(ir_instruction *ir, unsigned op,
612 613 614 615 616 617 618
        		 st_dst_reg dst, st_src_reg src0)
{
   assert(dst.writemask != 0);
   return emit(ir, op, dst, src0, undef_src, undef_src);
}

glsl_to_tgsi_instruction *
619
glsl_to_tgsi_visitor::emit(ir_instruction *ir, unsigned op)
620 621 622 623
{
   return emit(ir, op, undef_dst, undef_src, undef_src, undef_src);
}

624 625 626 627 628 629 630 631 632 633 634
/**
 * Determines whether to use an integer, unsigned integer, or float opcode 
 * based on the operands and input opcode, then emits the result.
 */
unsigned
glsl_to_tgsi_visitor::get_opcode(ir_instruction *ir, unsigned op,
        		 st_dst_reg dst,
        		 st_src_reg src0, st_src_reg src1)
{
   int type = GLSL_TYPE_FLOAT;
   
635 636 637 638 639
   assert(src0.type != GLSL_TYPE_ARRAY);
   assert(src0.type != GLSL_TYPE_STRUCT);
   assert(src1.type != GLSL_TYPE_ARRAY);
   assert(src1.type != GLSL_TYPE_STRUCT);

640 641
   if (src0.type == GLSL_TYPE_FLOAT || src1.type == GLSL_TYPE_FLOAT)
      type = GLSL_TYPE_FLOAT;
642
   else if (native_integers)
643
      type = src0.type == GLSL_TYPE_BOOL ? GLSL_TYPE_INT : src0.type;
644 645 646

#define case4(c, f, i, u) \
   case TGSI_OPCODE_##c: \
647 648 649 650 651 652
      if (type == GLSL_TYPE_INT) \
         op = TGSI_OPCODE_##i; \
      else if (type == GLSL_TYPE_UINT) \
         op = TGSI_OPCODE_##u; \
      else \
         op = TGSI_OPCODE_##f; \
653
      break;
654

655 656 657
#define case3(f, i, u)  case4(f, f, i, u)
#define case2fi(f, i)   case4(f, f, i, i)
#define case2iu(i, u)   case4(i, LAST, i, u)
658 659 660 661 662 663 664 665 666 667 668 669 670

#define casecomp(c, f, i, u) \
   case TGSI_OPCODE_##c: \
      if (type == GLSL_TYPE_INT) \
         op = TGSI_OPCODE_##i; \
      else if (type == GLSL_TYPE_UINT) \
         op = TGSI_OPCODE_##u; \
      else if (native_integers) \
         op = TGSI_OPCODE_##f; \
      else \
         op = TGSI_OPCODE_##c; \
      break;

671 672 673 674 675 676 677 678
   switch(op) {
      case2fi(ADD, UADD);
      case2fi(MUL, UMUL);
      case2fi(MAD, UMAD);
      case3(DIV, IDIV, UDIV);
      case3(MAX, IMAX, UMAX);
      case3(MIN, IMIN, UMIN);
      case2iu(MOD, UMOD);
679 680 681 682 683 684

      casecomp(SEQ, FSEQ, USEQ, USEQ);
      casecomp(SNE, FSNE, USNE, USNE);
      casecomp(SGE, FSGE, ISGE, USGE);
      casecomp(SLT, FSLT, ISLT, USLT);

685
      case2iu(ISHR, USHR);
686 687 688

      case2fi(SSG, ISSG);
      case3(ABS, IABS, IABS);
689 690 691 692 693 694 695 696
      
      default: break;
   }
   
   assert(op != TGSI_OPCODE_LAST);
   return op;
}

697
glsl_to_tgsi_instruction *
698 699 700 701
glsl_to_tgsi_visitor::emit_dp(ir_instruction *ir,
        		    st_dst_reg dst, st_src_reg src0, st_src_reg src1,
        		    unsigned elements)
{
702 703
   static const unsigned dot_opcodes[] = {
      TGSI_OPCODE_DP2, TGSI_OPCODE_DP3, TGSI_OPCODE_DP4
704 705
   };

706
   return emit(ir, dot_opcodes[elements - 2], dst, src0, src1);
707 708 709
}

/**
710
 * Emits TGSI scalar opcodes to produce unique answers across channels.
711
 *
712
 * Some TGSI opcodes are scalar-only, like ARB_fp/vp.  The src X
713 714 715 716 717
 * channel determines the result across all channels.  So to do a vec4
 * of this operation, we want to emit a scalar per source channel used
 * to produce dest channels.
 */
void
718
glsl_to_tgsi_visitor::emit_scalar(ir_instruction *ir, unsigned op,
719 720 721 722 723 724
        		        st_dst_reg dst,
        			st_src_reg orig_src0, st_src_reg orig_src1)
{
   int i, j;
   int done_mask = ~dst.writemask;

725
   /* TGSI RCP is a scalar operation splatting results to all channels,
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
    * like ARB_fp/vp.  So emit as many RCPs as necessary to cover our
    * dst channels.
    */
   for (i = 0; i < 4; i++) {
      GLuint this_mask = (1 << i);
      glsl_to_tgsi_instruction *inst;
      st_src_reg src0 = orig_src0;
      st_src_reg src1 = orig_src1;

      if (done_mask & this_mask)
         continue;

      GLuint src0_swiz = GET_SWZ(src0.swizzle, i);
      GLuint src1_swiz = GET_SWZ(src1.swizzle, i);
      for (j = i + 1; j < 4; j++) {
         /* If there is another enabled component in the destination that is
          * derived from the same inputs, generate its value on this pass as
          * well.
          */
         if (!(done_mask & (1 << j)) &&
             GET_SWZ(src0.swizzle, j) == src0_swiz &&
             GET_SWZ(src1.swizzle, j) == src1_swiz) {
            this_mask |= (1 << j);
         }
      }
      src0.swizzle = MAKE_SWIZZLE4(src0_swiz, src0_swiz,
        			   src0_swiz, src0_swiz);
      src1.swizzle = MAKE_SWIZZLE4(src1_swiz, src1_swiz,
        			  src1_swiz, src1_swiz);

      inst = emit(ir, op, dst, src0, src1);
      inst->dst.writemask = this_mask;
      done_mask |= this_mask;
   }
}

void
763
glsl_to_tgsi_visitor::emit_scalar(ir_instruction *ir, unsigned op,
764 765 766 767 768 769 770 771 772
        		        st_dst_reg dst, st_src_reg src0)
{
   st_src_reg undef = undef_src;

   undef.swizzle = SWIZZLE_XXXX;

   emit_scalar(ir, op, dst, src0, undef);
}

773 774 775 776
void
glsl_to_tgsi_visitor::emit_arl(ir_instruction *ir,
        		        st_dst_reg dst, st_src_reg src0)
{
777 778 779 780 781
   int op = TGSI_OPCODE_ARL;

   if (src0.type == GLSL_TYPE_INT || src0.type == GLSL_TYPE_UINT)
      op = TGSI_OPCODE_UARL;

782 783 784 785
   assert(dst.file == PROGRAM_ADDRESS);
   if (dst.index >= this->num_address_regs)
      this->num_address_regs = dst.index + 1;

786
   emit(NULL, op, dst, src0);
787 788
}

789
/**
790
 * Emit an TGSI_OPCODE_SCS instruction
791
 *
792 793 794 795
 * The \c SCS opcode functions a bit differently than the other TGSI opcodes.
 * Instead of splatting its result across all four components of the 
 * destination, it writes one value to the \c x component and another value to 
 * the \c y component.
796 797
 *
 * \param ir        IR instruction being processed
798 799
 * \param op        Either \c TGSI_OPCODE_SIN or \c TGSI_OPCODE_COS depending 
 *                  on which value is desired.
800 801 802 803
 * \param dst       Destination register
 * \param src       Source register
 */
void
804
glsl_to_tgsi_visitor::emit_scs(ir_instruction *ir, unsigned op,
805 806 807 808 809 810 811 812 813 814
        		     st_dst_reg dst,
        		     const st_src_reg &src)
{
   /* Vertex programs cannot use the SCS opcode.
    */
   if (this->prog->Target == GL_VERTEX_PROGRAM_ARB) {
      emit_scalar(ir, op, dst, src);
      return;
   }

815
   const unsigned component = (op == TGSI_OPCODE_SIN) ? 0 : 1;
816 817 818 819
   const unsigned scs_mask = (1U << component);
   int done_mask = ~dst.writemask;
   st_src_reg tmp;

820
   assert(op == TGSI_OPCODE_SIN || op == TGSI_OPCODE_COS);
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862

   /* If there are compnents in the destination that differ from the component
    * that will be written by the SCS instrution, we'll need a temporary.
    */
   if (scs_mask != unsigned(dst.writemask)) {
      tmp = get_temp(glsl_type::vec4_type);
   }

   for (unsigned i = 0; i < 4; i++) {
      unsigned this_mask = (1U << i);
      st_src_reg src0 = src;

      if ((done_mask & this_mask) != 0)
         continue;

      /* The source swizzle specified which component of the source generates
       * sine / cosine for the current component in the destination.  The SCS
       * instruction requires that this value be swizzle to the X component.
       * Replace the current swizzle with a swizzle that puts the source in
       * the X component.
       */
      unsigned src0_swiz = GET_SWZ(src.swizzle, i);

      src0.swizzle = MAKE_SWIZZLE4(src0_swiz, src0_swiz,
        			   src0_swiz, src0_swiz);
      for (unsigned j = i + 1; j < 4; j++) {
         /* If there is another enabled component in the destination that is
          * derived from the same inputs, generate its value on this pass as
          * well.
          */
         if (!(done_mask & (1 << j)) &&
             GET_SWZ(src0.swizzle, j) == src0_swiz) {
            this_mask |= (1 << j);
         }
      }

      if (this_mask != scs_mask) {
         glsl_to_tgsi_instruction *inst;
         st_dst_reg tmp_dst = st_dst_reg(tmp);

         /* Emit the SCS instruction.
          */
863
         inst = emit(ir, TGSI_OPCODE_SCS, tmp_dst, src0);
864 865 866 867 868 869 870
         inst->dst.writemask = scs_mask;

         /* Move the result of the SCS instruction to the desired location in
          * the destination.
          */
         tmp.swizzle = MAKE_SWIZZLE4(component, component,
        			     component, component);
871
         inst = emit(ir, TGSI_OPCODE_SCS, dst, tmp);
872 873 874 875
         inst->dst.writemask = this_mask;
      } else {
         /* Emit the SCS instruction to write directly to the destination.
          */
876
         glsl_to_tgsi_instruction *inst = emit(ir, TGSI_OPCODE_SCS, dst, src0);
877 878 879 880 881 882 883
         inst->dst.writemask = scs_mask;
      }

      done_mask |= this_mask;
   }
}

884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
int
glsl_to_tgsi_visitor::add_constant(gl_register_file file,
        		     gl_constant_value values[4], int size, int datatype,
        		     GLuint *swizzle_out)
{
   if (file == PROGRAM_CONSTANT) {
      return _mesa_add_typed_unnamed_constant(this->prog->Parameters, values,
                                              size, datatype, swizzle_out);
   } else {
      int index = 0;
      immediate_storage *entry;
      assert(file == PROGRAM_IMMEDIATE);

      /* Search immediate storage to see if we already have an identical
       * immediate that we can use instead of adding a duplicate entry.
       */
      foreach_iter(exec_list_iterator, iter, this->immediates) {
         entry = (immediate_storage *)iter.get();
         
         if (entry->size == size &&
             entry->type == datatype &&
             !memcmp(entry->values, values, size * sizeof(gl_constant_value))) {
             return index;
         }
         index++;
      }
      
      /* Add this immediate to the list. */
      entry = new(mem_ctx) immediate_storage(values, size, datatype);
      this->immediates.push_tail(entry);
      this->num_immediates++;
      return index;
   }
}

919
st_src_reg
920 921
glsl_to_tgsi_visitor::st_src_reg_for_float(float val)
{
922
   st_src_reg src(PROGRAM_IMMEDIATE, -1, GLSL_TYPE_FLOAT);
923
   union gl_constant_value uval;
924

925
   uval.f = val;
926
   src.index = add_constant(src.file, &uval, 1, GL_FLOAT, &src.swizzle);
927 928 929 930

   return src;
}

931
st_src_reg
932 933
glsl_to_tgsi_visitor::st_src_reg_for_int(int val)
{
934
   st_src_reg src(PROGRAM_IMMEDIATE, -1, GLSL_TYPE_INT);
935 936
   union gl_constant_value uval;
   
937
   assert(native_integers);
938 939

   uval.i = val;
940
   src.index = add_constant(src.file, &uval, 1, GL_INT, &src.swizzle);
941 942 943 944

   return src;
}

945
st_src_reg
946 947
glsl_to_tgsi_visitor::st_src_reg_for_type(int type, int val)
{
948
   if (native_integers)
949 950 951 952 953 954
      return type == GLSL_TYPE_FLOAT ? st_src_reg_for_float(val) : 
                                       st_src_reg_for_int(val);
   else
      return st_src_reg_for_float(val);
}

955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
static int
type_size(const struct glsl_type *type)
{
   unsigned int i;
   int size;

   switch (type->base_type) {
   case GLSL_TYPE_UINT:
   case GLSL_TYPE_INT:
   case GLSL_TYPE_FLOAT:
   case GLSL_TYPE_BOOL:
      if (type->is_matrix()) {
         return type->matrix_columns;
      } else {
         /* Regardless of size of vector, it gets a vec4. This is bad
          * packing for things like floats, but otherwise arrays become a
          * mess.  Hopefully a later pass over the code can pack scalars
          * down if appropriate.
          */
         return 1;
      }
   case GLSL_TYPE_ARRAY:
      assert(type->length > 0);
      return type_size(type->fields.array) * type->length;
   case GLSL_TYPE_STRUCT:
      size = 0;
      for (i = 0; i < type->length; i++) {
         size += type_size(type->fields.structure[i].type);
      }
      return size;
   case GLSL_TYPE_SAMPLER:
      /* Samplers take up one slot in UNIFORMS[], but they're baked in
       * at link time.
       */
      return 1;
990
   case GLSL_TYPE_ATOMIC_UINT:
991
   case GLSL_TYPE_INTERFACE:
992 993 994 995
   case GLSL_TYPE_VOID:
   case GLSL_TYPE_ERROR:
      assert(!"Invalid type in type_size");
      break;
996
   }
997
   return 0;
998 999 1000 1001 1002
}

/**
 * In the initial pass of codegen, we assign temporary numbers to
 * intermediate results.  (not SSA -- variable assignments will reuse
1003
 * storage).
1004 1005 1006 1007 1008 1009
 */
st_src_reg
glsl_to_tgsi_visitor::get_temp(const glsl_type *type)
{
   st_src_reg src;

1010
   src.type = native_integers ? type->base_type : GLSL_TYPE_FLOAT;
1011
   src.reladdr = NULL;
1012 1013
   src.negate = 0;

1014 1015 1016
   if (!options->EmitNoIndirectTemp &&
       (type->is_array() || type->is_matrix())) {

1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
      src.file = PROGRAM_ARRAY;
      src.index = next_array << 16 | 0x8000;
      array_sizes[next_array] = type_size(type);
      ++next_array;

   } else {
      src.file = PROGRAM_TEMPORARY;
      src.index = next_temp;
      next_temp += type_size(type);
   }
1027 1028 1029 1030

   if (type->is_array() || type->is_record()) {
      src.swizzle = SWIZZLE_NOOP;
   } else {
1031
      src.swizzle = swizzle_for_size(type->vector_elements);
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
   }

   return src;
}

variable_storage *
glsl_to_tgsi_visitor::find_variable_storage(ir_variable *var)
{
   
   variable_storage *entry;

   foreach_iter(exec_list_iterator, iter, this->variables) {
      entry = (variable_storage *)iter.get();

      if (entry->var == var)
         return entry;
   }

   return NULL;
}

void
glsl_to_tgsi_visitor::visit(ir_variable *ir)
{
   if (strcmp(ir->name, "gl_FragCoord") == 0) {
      struct gl_fragment_program *fp = (struct gl_fragment_program *)this->prog;

1059 1060
      fp->OriginUpperLeft = ir->data.origin_upper_left;
      fp->PixelCenterInteger = ir->data.pixel_center_integer;
1061 1062
   }

1063
   if (ir->data.mode == ir_var_uniform && strncmp(ir->name, "gl_", 3) == 0) {
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
      unsigned int i;
      const ir_state_slot *const slots = ir->state_slots;
      assert(ir->state_slots != NULL);

      /* Check if this statevar's setup in the STATE file exactly
       * matches how we'll want to reference it as a
       * struct/array/whatever.  If not, then we need to move it into
       * temporary storage and hope that it'll get copy-propagated
       * out.
       */
      for (i = 0; i < ir->num_state_slots; i++) {
         if (slots[i].swizzle != SWIZZLE_XYZW) {
            break;
         }
      }

1080
      variable_storage *storage;
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
      st_dst_reg dst;
      if (i == ir->num_state_slots) {
         /* We'll set the index later. */
         storage = new(mem_ctx) variable_storage(ir, PROGRAM_STATE_VAR, -1);
         this->variables.push_tail(storage);

         dst = undef_dst;
      } else {
         /* The variable_storage constructor allocates slots based on the size
          * of the type.  However, this had better match the number of state
          * elements that we're going to copy into the new temporary.
          */
         assert((int) ir->num_state_slots == type_size(ir->type));

1095 1096 1097
         dst = st_dst_reg(get_temp(ir->type));

         storage = new(mem_ctx) variable_storage(ir, dst.file, dst.index);
1098

1099
         this->variables.push_tail(storage);
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
      }


      for (unsigned int i = 0; i < ir->num_state_slots; i++) {
         int index = _mesa_add_state_reference(this->prog->Parameters,
        				       (gl_state_index *)slots[i].tokens);

         if (storage->file == PROGRAM_STATE_VAR) {
            if (storage->index == -1) {
               storage->index = index;
            } else {
               assert(index == storage->index + (int)i);
            }
         } else {
1114 1115 1116 1117 1118 1119
         	/* We use GLSL_TYPE_FLOAT here regardless of the actual type of
         	 * the data being moved since MOV does not care about the type of
         	 * data it is moving, and we don't want to declare registers with
         	 * array or struct types.
         	 */
            st_src_reg src(PROGRAM_STATE_VAR, index, GLSL_TYPE_FLOAT);
1120
            src.swizzle = slots[i].swizzle;
1121
            emit(ir, TGSI_OPCODE_MOV, dst, src);
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
            /* even a float takes up a whole vec4 reg in a struct/array. */
            dst.index++;
         }
      }

      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));
      }
   }
}

void
glsl_to_tgsi_visitor::visit(ir_loop *ir)
{
1140
   emit(NULL, TGSI_OPCODE_BGNLOOP);
1141 1142 1143

   visit_exec_list(&ir->body_instructions, this);

1144
   emit(NULL, TGSI_OPCODE_ENDLOOP);
1145 1146 1147 1148 1149 1150 1151
}

void
glsl_to_tgsi_visitor::visit(ir_loop_jump *ir)
{
   switch (ir->mode) {
   case ir_loop_jump::jump_break:
1152
      emit(NULL, TGSI_OPCODE_BRK);
1153 1154
      break;
   case ir_loop_jump::jump_continue:
1155
      emit(NULL, TGSI_OPCODE_CONT);
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
      break;
   }
}


void
glsl_to_tgsi_visitor::visit(ir_function_signature *ir)
{
   assert(0);
   (void)ir;
}

void
glsl_to_tgsi_visitor::visit(ir_function *ir)
{
   /* Ignore function bodies other than main() -- we shouldn't see calls to
    * them since they should all be inlined before we get to glsl_to_tgsi.
    */
   if (strcmp(ir->name, "main") == 0) {
      const ir_function_signature *sig;
      exec_list empty;

1178
      sig = ir->matching_signature(NULL, &empty);
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189

      assert(sig);

      foreach_iter(exec_list_iterator, iter, sig->body) {
         ir_instruction *ir = (ir_instruction *)iter.get();

         ir->accept(this);
      }
   }
}

1190
bool
1191 1192 1193 1194
glsl_to_tgsi_visitor::try_emit_mad(ir_expression *ir, int mul_operand)
{
   int nonmul_operand = 1 - mul_operand;
   st_src_reg a, b, c;
1195
   st_dst_reg result_dst;
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208

   ir_expression *expr = ir->operands[mul_operand]->as_expression();
   if (!expr || expr->operation != ir_binop_mul)
      return false;

   expr->operands[0]->accept(this);
   a = this->result;
   expr->operands[1]->accept(this);
   b = this->result;
   ir->operands[nonmul_operand]->accept(this);
   c = this->result;

   this->result = get_temp(ir->type);
1209 1210 1211
   result_dst = st_dst_reg(this->result);
   result_dst.writemask = (1 << ir->type->vector_elements) - 1;
   emit(ir, TGSI_OPCODE_MAD, result_dst, a, b, c);
1212 1213 1214 1215

   return true;
}

1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
/**
 * Emit MAD(a, -b, a) instead of AND(a, NOT(b))
 *
 * The logic values are 1.0 for true and 0.0 for false.  Logical-and is
 * implemented using multiplication, and logical-or is implemented using
 * addition.  Logical-not can be implemented as (true - x), or (1.0 - x).
 * As result, the logical expression (a & !b) can be rewritten as:
 *
 *     - a * !b
 *     - a * (1 - b)
 *     - (a * 1) - (a * b)
 *     - a + -(a * b)
 *     - a + (a * -b)
 *
 * This final expression can be implemented as a single MAD(a, -b, a)
 * instruction.
 */
bool
glsl_to_tgsi_visitor::try_emit_mad_for_and_not(ir_expression *ir, int try_operand)
{
   const int other_operand = 1 - try_operand;
   st_src_reg a, b;

   ir_expression *expr = ir->operands[try_operand]->as_expression();
   if (!expr || expr->operation != ir_unop_logic_not)
      return false;

   ir->operands[other_operand]->accept(this);
   a = this->result;
   expr->operands[0]->accept(this);
   b = this->result;

   b.negate = ~b.negate;

   this->result = get_temp(ir->type);
   emit(ir, TGSI_OPCODE_MAD, st_dst_reg(this->result), a, b, a);

   return true;
}

1256
bool
1257 1258
glsl_to_tgsi_visitor::try_emit_sat(ir_expression *ir)
{
1259
   /* Emit saturates in the vertex shader only if SM 3.0 is supported.
1260
    */
1261 1262
   if (this->prog->Target == GL_VERTEX_PROGRAM_ARB &&
       !st_context(this->ctx)->has_shader_model3) {
1263
      return false;
1264
   }
1265 1266 1267 1268 1269 1270 1271 1272

   ir_rvalue *sat_src = ir->as_rvalue_to_saturate();
   if (!sat_src)
      return false;

   sat_src->accept(this);
   st_src_reg src = this->result;

1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
   /* 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();
   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)) {
      glsl_to_tgsi_instruction *new_inst;
      new_inst = (glsl_to_tgsi_instruction *)this->instructions.get_tail();
      new_inst->saturate = true;
   } else {
      this->result = get_temp(ir->type);
      st_dst_reg result_dst = st_dst_reg(this->result);
      result_dst.writemask = (1 << ir->type->vector_elements) - 1;
      glsl_to_tgsi_instruction *inst;
      inst = emit(ir, TGSI_OPCODE_MOV, result_dst, src);
      inst->saturate = true;
   }
1299 1300 1301 1302 1303 1304 1305 1306

   return true;
}

void
glsl_to_tgsi_visitor::reladdr_to_temp(ir_instruction *ir,
        			    st_src_reg *reg, int *num_reladdr)
{
1307
   if (!reg->reladdr && !reg->reladdr2)
1308 1309
      return;

1310 1311
   if (reg->reladdr) emit_arl(ir, address_reg, *reg->reladdr);
   if (reg->reladdr2) emit_arl(ir, address_reg2, *reg->reladdr2);
1312 1313 1314 1315

   if (*num_reladdr != 1) {
      st_src_reg temp = get_temp(glsl_type::vec4_type);

1316
      emit(ir, TGSI_OPCODE_MOV, st_dst_reg(temp), *reg);
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
      *reg = temp;
   }

   (*num_reladdr)--;
}

void
glsl_to_tgsi_visitor::visit(ir_expression *ir)
{
   unsigned int operand;
   st_src_reg op[Elements(ir->operands)];
   st_src_reg result_src;
   st_dst_reg result_dst;

Bryan Cain's avatar