st/mesa: set result writemask based on ir type
[mesa.git] / src / mesa / state_tracker / st_glsl_to_tgsi.cpp
1 /*
2 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
3 * Copyright (C) 2008 VMware, Inc. All Rights Reserved.
4 * Copyright © 2010 Intel Corporation
5 * Copyright © 2011 Bryan Cain
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 */
26
27 /**
28 * \file glsl_to_tgsi.cpp
29 *
30 * Translate GLSL IR to TGSI.
31 */
32
33 #include "st_glsl_to_tgsi.h"
34
35 #include "compiler/glsl/glsl_parser_extras.h"
36 #include "compiler/glsl/ir_optimization.h"
37 #include "compiler/glsl/program.h"
38
39 #include "main/errors.h"
40 #include "main/shaderobj.h"
41 #include "main/uniforms.h"
42 #include "main/shaderapi.h"
43 #include "main/shaderimage.h"
44 #include "program/prog_instruction.h"
45
46 #include "pipe/p_context.h"
47 #include "pipe/p_screen.h"
48 #include "tgsi/tgsi_ureg.h"
49 #include "tgsi/tgsi_info.h"
50 #include "util/u_math.h"
51 #include "util/u_memory.h"
52 #include "st_program.h"
53 #include "st_mesa_to_tgsi.h"
54 #include "st_format.h"
55 #include "st_glsl_types.h"
56 #include "st_nir.h"
57 #include "st_shader_cache.h"
58
59 #include <algorithm>
60
61 #define PROGRAM_ANY_CONST ((1 << PROGRAM_STATE_VAR) | \
62 (1 << PROGRAM_CONSTANT) | \
63 (1 << PROGRAM_UNIFORM))
64
65 #define MAX_GLSL_TEXTURE_OFFSET 4
66
67 class st_src_reg;
68 class st_dst_reg;
69
70 static int swizzle_for_size(int size);
71
72 static int swizzle_for_type(const glsl_type *type, int component = 0)
73 {
74 unsigned num_elements = 4;
75
76 if (type) {
77 type = type->without_array();
78 if (type->is_scalar() || type->is_vector() || type->is_matrix())
79 num_elements = type->vector_elements;
80 }
81
82 int swizzle = swizzle_for_size(num_elements);
83 assert(num_elements + component <= 4);
84
85 swizzle += component * MAKE_SWIZZLE4(1, 1, 1, 1);
86 return swizzle;
87 }
88
89 /**
90 * This struct is a corresponding struct to TGSI ureg_src.
91 */
92 class st_src_reg {
93 public:
94 st_src_reg(gl_register_file file, int index, const glsl_type *type,
95 int component = 0, unsigned array_id = 0)
96 {
97 assert(file != PROGRAM_ARRAY || array_id != 0);
98 this->file = file;
99 this->index = index;
100 this->swizzle = swizzle_for_type(type, component);
101 this->negate = 0;
102 this->abs = 0;
103 this->index2D = 0;
104 this->type = type ? type->base_type : GLSL_TYPE_ERROR;
105 this->reladdr = NULL;
106 this->reladdr2 = NULL;
107 this->has_index2 = false;
108 this->double_reg2 = false;
109 this->array_id = array_id;
110 this->is_double_vertex_input = false;
111 }
112
113 st_src_reg(gl_register_file file, int index, enum glsl_base_type type)
114 {
115 assert(file != PROGRAM_ARRAY); /* need array_id > 0 */
116 this->type = type;
117 this->file = file;
118 this->index = index;
119 this->index2D = 0;
120 this->swizzle = SWIZZLE_XYZW;
121 this->negate = 0;
122 this->abs = 0;
123 this->reladdr = NULL;
124 this->reladdr2 = NULL;
125 this->has_index2 = false;
126 this->double_reg2 = false;
127 this->array_id = 0;
128 this->is_double_vertex_input = false;
129 }
130
131 st_src_reg(gl_register_file file, int index, enum glsl_base_type type, int index2D)
132 {
133 assert(file != PROGRAM_ARRAY); /* need array_id > 0 */
134 this->type = type;
135 this->file = file;
136 this->index = index;
137 this->index2D = index2D;
138 this->swizzle = SWIZZLE_XYZW;
139 this->negate = 0;
140 this->abs = 0;
141 this->reladdr = NULL;
142 this->reladdr2 = NULL;
143 this->has_index2 = false;
144 this->double_reg2 = false;
145 this->array_id = 0;
146 this->is_double_vertex_input = false;
147 }
148
149 st_src_reg()
150 {
151 this->type = GLSL_TYPE_ERROR;
152 this->file = PROGRAM_UNDEFINED;
153 this->index = 0;
154 this->index2D = 0;
155 this->swizzle = 0;
156 this->negate = 0;
157 this->abs = 0;
158 this->reladdr = NULL;
159 this->reladdr2 = NULL;
160 this->has_index2 = false;
161 this->double_reg2 = false;
162 this->array_id = 0;
163 this->is_double_vertex_input = false;
164 }
165
166 explicit st_src_reg(st_dst_reg reg);
167
168 int16_t index; /**< temporary index, VERT_ATTRIB_*, VARYING_SLOT_*, etc. */
169 int16_t index2D;
170 uint16_t swizzle; /**< SWIZZLE_XYZWONEZERO swizzles from Mesa. */
171 int negate:4; /**< NEGATE_XYZW mask from mesa */
172 unsigned abs:1;
173 enum glsl_base_type type:5; /** GLSL_TYPE_* from GLSL IR (enum glsl_base_type) */
174 unsigned has_index2:1;
175 gl_register_file file:5; /**< PROGRAM_* from Mesa */
176 /*
177 * Is this the second half of a double register pair?
178 * currently used for input mapping only.
179 */
180 unsigned double_reg2:1;
181 unsigned is_double_vertex_input:1;
182 unsigned array_id:10;
183
184 /** Register index should be offset by the integer in this reg. */
185 st_src_reg *reladdr;
186 st_src_reg *reladdr2;
187
188 st_src_reg get_abs()
189 {
190 st_src_reg reg = *this;
191 reg.negate = 0;
192 reg.abs = 1;
193 return reg;
194 }
195 };
196
197 class st_dst_reg {
198 public:
199 st_dst_reg(gl_register_file file, int writemask, enum glsl_base_type type, int index)
200 {
201 assert(file != PROGRAM_ARRAY); /* need array_id > 0 */
202 this->file = file;
203 this->index = index;
204 this->index2D = 0;
205 this->writemask = writemask;
206 this->reladdr = NULL;
207 this->reladdr2 = NULL;
208 this->has_index2 = false;
209 this->type = type;
210 this->array_id = 0;
211 }
212
213 st_dst_reg(gl_register_file file, int writemask, enum glsl_base_type type)
214 {
215 assert(file != PROGRAM_ARRAY); /* need array_id > 0 */
216 this->file = file;
217 this->index = 0;
218 this->index2D = 0;
219 this->writemask = writemask;
220 this->reladdr = NULL;
221 this->reladdr2 = NULL;
222 this->has_index2 = false;
223 this->type = type;
224 this->array_id = 0;
225 }
226
227 st_dst_reg()
228 {
229 this->type = GLSL_TYPE_ERROR;
230 this->file = PROGRAM_UNDEFINED;
231 this->index = 0;
232 this->index2D = 0;
233 this->writemask = 0;
234 this->reladdr = NULL;
235 this->reladdr2 = NULL;
236 this->has_index2 = false;
237 this->array_id = 0;
238 }
239
240 explicit st_dst_reg(st_src_reg reg);
241
242 int16_t index; /**< temporary index, VERT_ATTRIB_*, VARYING_SLOT_*, etc. */
243 int16_t index2D;
244 gl_register_file file:5; /**< PROGRAM_* from Mesa */
245 unsigned writemask:4; /**< Bitfield of WRITEMASK_[XYZW] */
246 enum glsl_base_type type:5; /** GLSL_TYPE_* from GLSL IR (enum glsl_base_type) */
247 unsigned has_index2:1;
248 unsigned array_id:10;
249
250 /** Register index should be offset by the integer in this reg. */
251 st_src_reg *reladdr;
252 st_src_reg *reladdr2;
253 };
254
255 st_src_reg::st_src_reg(st_dst_reg reg)
256 {
257 this->type = reg.type;
258 this->file = reg.file;
259 this->index = reg.index;
260 this->swizzle = SWIZZLE_XYZW;
261 this->negate = 0;
262 this->abs = 0;
263 this->reladdr = reg.reladdr;
264 this->index2D = reg.index2D;
265 this->reladdr2 = reg.reladdr2;
266 this->has_index2 = reg.has_index2;
267 this->double_reg2 = false;
268 this->array_id = reg.array_id;
269 this->is_double_vertex_input = false;
270 }
271
272 st_dst_reg::st_dst_reg(st_src_reg reg)
273 {
274 this->type = reg.type;
275 this->file = reg.file;
276 this->index = reg.index;
277 this->writemask = WRITEMASK_XYZW;
278 this->reladdr = reg.reladdr;
279 this->index2D = reg.index2D;
280 this->reladdr2 = reg.reladdr2;
281 this->has_index2 = reg.has_index2;
282 this->array_id = reg.array_id;
283 }
284
285 class glsl_to_tgsi_instruction : public exec_node {
286 public:
287 DECLARE_RALLOC_CXX_OPERATORS(glsl_to_tgsi_instruction)
288
289 st_dst_reg dst[2];
290 st_src_reg src[4];
291 st_src_reg resource; /**< sampler or buffer register */
292 st_src_reg *tex_offsets;
293
294 /** Pointer to the ir source this tree came from for debugging */
295 ir_instruction *ir;
296
297 unsigned op:8; /**< TGSI opcode */
298 unsigned saturate:1;
299 unsigned is_64bit_expanded:1;
300 unsigned sampler_base:5;
301 unsigned sampler_array_size:6; /**< 1-based size of sampler array, 1 if not array */
302 unsigned tex_target:4; /**< One of TEXTURE_*_INDEX */
303 glsl_base_type tex_type:5;
304 unsigned tex_shadow:1;
305 unsigned image_format:9;
306 unsigned tex_offset_num_offset:3;
307 unsigned dead_mask:4; /**< Used in dead code elimination */
308 unsigned buffer_access:3; /**< buffer access type */
309
310 const struct tgsi_opcode_info *info;
311 };
312
313 class variable_storage : public exec_node {
314 public:
315 variable_storage(ir_variable *var, gl_register_file file, int index,
316 unsigned array_id = 0)
317 : file(file), index(index), component(0), var(var), array_id(array_id)
318 {
319 assert(file != PROGRAM_ARRAY || array_id != 0);
320 }
321
322 gl_register_file file;
323 int index;
324
325 /* Explicit component location. This is given in terms of the GLSL-style
326 * swizzles where each double is a single component, i.e. for 64-bit types
327 * it can only be 0 or 1.
328 */
329 int component;
330 ir_variable *var; /* variable that maps to this, if any */
331 unsigned array_id;
332 };
333
334 class immediate_storage : public exec_node {
335 public:
336 immediate_storage(gl_constant_value *values, int size32, int type)
337 {
338 memcpy(this->values, values, size32 * sizeof(gl_constant_value));
339 this->size32 = size32;
340 this->type = type;
341 }
342
343 /* doubles are stored across 2 gl_constant_values */
344 gl_constant_value values[4];
345 int size32; /**< Number of 32-bit components (1-4) */
346 int type; /**< GL_DOUBLE, GL_FLOAT, GL_INT, GL_BOOL, or GL_UNSIGNED_INT */
347 };
348
349 static st_src_reg undef_src = st_src_reg(PROGRAM_UNDEFINED, 0, GLSL_TYPE_ERROR);
350 static st_dst_reg undef_dst = st_dst_reg(PROGRAM_UNDEFINED, SWIZZLE_NOOP, GLSL_TYPE_ERROR);
351
352 struct inout_decl {
353 unsigned mesa_index;
354 unsigned array_id; /* TGSI ArrayID; 1-based: 0 means not an array */
355 unsigned size;
356 unsigned interp_loc;
357 unsigned gs_out_streams;
358 enum glsl_interp_mode interp;
359 enum glsl_base_type base_type;
360 ubyte usage_mask; /* GLSL-style usage-mask, i.e. single bit per double */
361 };
362
363 static struct inout_decl *
364 find_inout_array(struct inout_decl *decls, unsigned count, unsigned array_id)
365 {
366 assert(array_id != 0);
367
368 for (unsigned i = 0; i < count; i++) {
369 struct inout_decl *decl = &decls[i];
370
371 if (array_id == decl->array_id) {
372 return decl;
373 }
374 }
375
376 return NULL;
377 }
378
379 static enum glsl_base_type
380 find_array_type(struct inout_decl *decls, unsigned count, unsigned array_id)
381 {
382 if (!array_id)
383 return GLSL_TYPE_ERROR;
384 struct inout_decl *decl = find_inout_array(decls, count, array_id);
385 if (decl)
386 return decl->base_type;
387 return GLSL_TYPE_ERROR;
388 }
389
390 struct rename_reg_pair {
391 int old_reg;
392 int new_reg;
393 };
394
395 struct glsl_to_tgsi_visitor : public ir_visitor {
396 public:
397 glsl_to_tgsi_visitor();
398 ~glsl_to_tgsi_visitor();
399
400 struct gl_context *ctx;
401 struct gl_program *prog;
402 struct gl_shader_program *shader_program;
403 struct gl_linked_shader *shader;
404 struct gl_shader_compiler_options *options;
405
406 int next_temp;
407
408 unsigned *array_sizes;
409 unsigned max_num_arrays;
410 unsigned next_array;
411
412 struct inout_decl inputs[4 * PIPE_MAX_SHADER_INPUTS];
413 unsigned num_inputs;
414 unsigned num_input_arrays;
415 struct inout_decl outputs[4 * PIPE_MAX_SHADER_OUTPUTS];
416 unsigned num_outputs;
417 unsigned num_output_arrays;
418
419 int num_address_regs;
420 uint32_t samplers_used;
421 glsl_base_type sampler_types[PIPE_MAX_SAMPLERS];
422 int sampler_targets[PIPE_MAX_SAMPLERS]; /**< One of TGSI_TEXTURE_* */
423 int buffers_used;
424 int images_used;
425 int image_targets[PIPE_MAX_SHADER_IMAGES];
426 unsigned image_formats[PIPE_MAX_SHADER_IMAGES];
427 bool indirect_addr_consts;
428 int wpos_transform_const;
429
430 int glsl_version;
431 bool native_integers;
432 bool have_sqrt;
433 bool have_fma;
434 bool use_shared_memory;
435 bool has_tex_txf_lz;
436
437 variable_storage *find_variable_storage(ir_variable *var);
438
439 int add_constant(gl_register_file file, gl_constant_value values[8],
440 int size, int datatype, uint16_t *swizzle_out);
441
442 st_src_reg get_temp(const glsl_type *type);
443 void reladdr_to_temp(ir_instruction *ir, st_src_reg *reg, int *num_reladdr);
444
445 st_src_reg st_src_reg_for_double(double val);
446 st_src_reg st_src_reg_for_float(float val);
447 st_src_reg st_src_reg_for_int(int val);
448 st_src_reg st_src_reg_for_type(enum glsl_base_type type, int val);
449
450 /**
451 * \name Visit methods
452 *
453 * As typical for the visitor pattern, there must be one \c visit method for
454 * each concrete subclass of \c ir_instruction. Virtual base classes within
455 * the hierarchy should not have \c visit methods.
456 */
457 /*@{*/
458 virtual void visit(ir_variable *);
459 virtual void visit(ir_loop *);
460 virtual void visit(ir_loop_jump *);
461 virtual void visit(ir_function_signature *);
462 virtual void visit(ir_function *);
463 virtual void visit(ir_expression *);
464 virtual void visit(ir_swizzle *);
465 virtual void visit(ir_dereference_variable *);
466 virtual void visit(ir_dereference_array *);
467 virtual void visit(ir_dereference_record *);
468 virtual void visit(ir_assignment *);
469 virtual void visit(ir_constant *);
470 virtual void visit(ir_call *);
471 virtual void visit(ir_return *);
472 virtual void visit(ir_discard *);
473 virtual void visit(ir_texture *);
474 virtual void visit(ir_if *);
475 virtual void visit(ir_emit_vertex *);
476 virtual void visit(ir_end_primitive *);
477 virtual void visit(ir_barrier *);
478 /*@}*/
479
480 void visit_expression(ir_expression *, st_src_reg *) ATTRIBUTE_NOINLINE;
481
482 void visit_atomic_counter_intrinsic(ir_call *);
483 void visit_ssbo_intrinsic(ir_call *);
484 void visit_membar_intrinsic(ir_call *);
485 void visit_shared_intrinsic(ir_call *);
486 void visit_image_intrinsic(ir_call *);
487
488 st_src_reg result;
489
490 /** List of variable_storage */
491 exec_list variables;
492
493 /** List of immediate_storage */
494 exec_list immediates;
495 unsigned num_immediates;
496
497 /** List of glsl_to_tgsi_instruction */
498 exec_list instructions;
499
500 glsl_to_tgsi_instruction *emit_asm(ir_instruction *ir, unsigned op,
501 st_dst_reg dst = undef_dst,
502 st_src_reg src0 = undef_src,
503 st_src_reg src1 = undef_src,
504 st_src_reg src2 = undef_src,
505 st_src_reg src3 = undef_src);
506
507 glsl_to_tgsi_instruction *emit_asm(ir_instruction *ir, unsigned op,
508 st_dst_reg dst, st_dst_reg dst1,
509 st_src_reg src0 = undef_src,
510 st_src_reg src1 = undef_src,
511 st_src_reg src2 = undef_src,
512 st_src_reg src3 = undef_src);
513
514 unsigned get_opcode(unsigned op,
515 st_dst_reg dst,
516 st_src_reg src0, st_src_reg src1);
517
518 /**
519 * Emit the correct dot-product instruction for the type of arguments
520 */
521 glsl_to_tgsi_instruction *emit_dp(ir_instruction *ir,
522 st_dst_reg dst,
523 st_src_reg src0,
524 st_src_reg src1,
525 unsigned elements);
526
527 void emit_scalar(ir_instruction *ir, unsigned op,
528 st_dst_reg dst, st_src_reg src0);
529
530 void emit_scalar(ir_instruction *ir, unsigned op,
531 st_dst_reg dst, st_src_reg src0, st_src_reg src1);
532
533 void emit_arl(ir_instruction *ir, st_dst_reg dst, st_src_reg src0);
534
535 void get_deref_offsets(ir_dereference *ir,
536 unsigned *array_size,
537 unsigned *base,
538 uint16_t *index,
539 st_src_reg *reladdr,
540 bool opaque);
541 void calc_deref_offsets(ir_dereference *tail,
542 unsigned *array_elements,
543 uint16_t *index,
544 st_src_reg *indirect,
545 unsigned *location);
546 st_src_reg canonicalize_gather_offset(st_src_reg offset);
547
548 bool try_emit_mad(ir_expression *ir,
549 int mul_operand);
550 bool try_emit_mad_for_and_not(ir_expression *ir,
551 int mul_operand);
552
553 void emit_swz(ir_expression *ir);
554
555 bool process_move_condition(ir_rvalue *ir);
556
557 void simplify_cmp(void);
558
559 void rename_temp_registers(int num_renames, struct rename_reg_pair *renames);
560 void get_first_temp_read(int *first_reads);
561 void get_last_temp_read_first_temp_write(int *last_reads, int *first_writes);
562 void get_last_temp_write(int *last_writes);
563
564 void copy_propagate(void);
565 int eliminate_dead_code(void);
566
567 void merge_two_dsts(void);
568 void merge_registers(void);
569 void renumber_registers(void);
570
571 void emit_block_mov(ir_assignment *ir, const struct glsl_type *type,
572 st_dst_reg *l, st_src_reg *r,
573 st_src_reg *cond, bool cond_swap);
574
575 void *mem_ctx;
576 };
577
578 static st_dst_reg address_reg = st_dst_reg(PROGRAM_ADDRESS, WRITEMASK_X, GLSL_TYPE_FLOAT, 0);
579 static st_dst_reg address_reg2 = st_dst_reg(PROGRAM_ADDRESS, WRITEMASK_X, GLSL_TYPE_FLOAT, 1);
580 static st_dst_reg sampler_reladdr = st_dst_reg(PROGRAM_ADDRESS, WRITEMASK_X, GLSL_TYPE_FLOAT, 2);
581
582 static void
583 fail_link(struct gl_shader_program *prog, const char *fmt, ...) PRINTFLIKE(2, 3);
584
585 static void
586 fail_link(struct gl_shader_program *prog, const char *fmt, ...)
587 {
588 va_list args;
589 va_start(args, fmt);
590 ralloc_vasprintf_append(&prog->data->InfoLog, fmt, args);
591 va_end(args);
592
593 prog->data->LinkStatus = linking_failure;
594 }
595
596 static int
597 swizzle_for_size(int size)
598 {
599 static const int size_swizzles[4] = {
600 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X),
601 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Y, SWIZZLE_Y),
602 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z),
603 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W),
604 };
605
606 assert((size >= 1) && (size <= 4));
607 return size_swizzles[size - 1];
608 }
609
610 static bool
611 is_resource_instruction(unsigned opcode)
612 {
613 switch (opcode) {
614 case TGSI_OPCODE_RESQ:
615 case TGSI_OPCODE_LOAD:
616 case TGSI_OPCODE_ATOMUADD:
617 case TGSI_OPCODE_ATOMXCHG:
618 case TGSI_OPCODE_ATOMCAS:
619 case TGSI_OPCODE_ATOMAND:
620 case TGSI_OPCODE_ATOMOR:
621 case TGSI_OPCODE_ATOMXOR:
622 case TGSI_OPCODE_ATOMUMIN:
623 case TGSI_OPCODE_ATOMUMAX:
624 case TGSI_OPCODE_ATOMIMIN:
625 case TGSI_OPCODE_ATOMIMAX:
626 return true;
627 default:
628 return false;
629 }
630 }
631
632 static unsigned
633 num_inst_dst_regs(const glsl_to_tgsi_instruction *op)
634 {
635 return op->info->num_dst;
636 }
637
638 static unsigned
639 num_inst_src_regs(const glsl_to_tgsi_instruction *op)
640 {
641 return op->info->is_tex || is_resource_instruction(op->op) ?
642 op->info->num_src - 1 : op->info->num_src;
643 }
644
645 glsl_to_tgsi_instruction *
646 glsl_to_tgsi_visitor::emit_asm(ir_instruction *ir, unsigned op,
647 st_dst_reg dst, st_dst_reg dst1,
648 st_src_reg src0, st_src_reg src1,
649 st_src_reg src2, st_src_reg src3)
650 {
651 glsl_to_tgsi_instruction *inst = new(mem_ctx) glsl_to_tgsi_instruction();
652 int num_reladdr = 0, i, j;
653 bool dst_is_64bit[2];
654
655 op = get_opcode(op, dst, src0, src1);
656
657 /* If we have to do relative addressing, we want to load the ARL
658 * reg directly for one of the regs, and preload the other reladdr
659 * sources into temps.
660 */
661 num_reladdr += dst.reladdr != NULL || dst.reladdr2;
662 num_reladdr += dst1.reladdr != NULL || dst1.reladdr2;
663 num_reladdr += src0.reladdr != NULL || src0.reladdr2 != NULL;
664 num_reladdr += src1.reladdr != NULL || src1.reladdr2 != NULL;
665 num_reladdr += src2.reladdr != NULL || src2.reladdr2 != NULL;
666 num_reladdr += src3.reladdr != NULL || src3.reladdr2 != NULL;
667
668 reladdr_to_temp(ir, &src3, &num_reladdr);
669 reladdr_to_temp(ir, &src2, &num_reladdr);
670 reladdr_to_temp(ir, &src1, &num_reladdr);
671 reladdr_to_temp(ir, &src0, &num_reladdr);
672
673 if (dst.reladdr || dst.reladdr2) {
674 if (dst.reladdr)
675 emit_arl(ir, address_reg, *dst.reladdr);
676 if (dst.reladdr2)
677 emit_arl(ir, address_reg2, *dst.reladdr2);
678 num_reladdr--;
679 }
680 if (dst1.reladdr) {
681 emit_arl(ir, address_reg, *dst1.reladdr);
682 num_reladdr--;
683 }
684 assert(num_reladdr == 0);
685
686 /* inst->op has only 8 bits. */
687 STATIC_ASSERT(TGSI_OPCODE_LAST <= 255);
688
689 inst->op = op;
690 inst->info = tgsi_get_opcode_info(op);
691 inst->dst[0] = dst;
692 inst->dst[1] = dst1;
693 inst->src[0] = src0;
694 inst->src[1] = src1;
695 inst->src[2] = src2;
696 inst->src[3] = src3;
697 inst->is_64bit_expanded = false;
698 inst->ir = ir;
699 inst->dead_mask = 0;
700 inst->tex_offsets = NULL;
701 inst->tex_offset_num_offset = 0;
702 inst->saturate = 0;
703 inst->tex_shadow = 0;
704 /* default to float, for paths where this is not initialized
705 * (since 0==UINT which is likely wrong):
706 */
707 inst->tex_type = GLSL_TYPE_FLOAT;
708
709 /* Update indirect addressing status used by TGSI */
710 if (dst.reladdr || dst.reladdr2) {
711 switch(dst.file) {
712 case PROGRAM_STATE_VAR:
713 case PROGRAM_CONSTANT:
714 case PROGRAM_UNIFORM:
715 this->indirect_addr_consts = true;
716 break;
717 case PROGRAM_IMMEDIATE:
718 assert(!"immediates should not have indirect addressing");
719 break;
720 default:
721 break;
722 }
723 }
724 else {
725 for (i = 0; i < 4; i++) {
726 if(inst->src[i].reladdr) {
727 switch(inst->src[i].file) {
728 case PROGRAM_STATE_VAR:
729 case PROGRAM_CONSTANT:
730 case PROGRAM_UNIFORM:
731 this->indirect_addr_consts = true;
732 break;
733 case PROGRAM_IMMEDIATE:
734 assert(!"immediates should not have indirect addressing");
735 break;
736 default:
737 break;
738 }
739 }
740 }
741 }
742
743 /*
744 * This section contains the double processing.
745 * GLSL just represents doubles as single channel values,
746 * however most HW and TGSI represent doubles as pairs of register channels.
747 *
748 * so we have to fixup destination writemask/index and src swizzle/indexes.
749 * dest writemasks need to translate from single channel write mask
750 * to a dual-channel writemask, but also need to modify the index,
751 * if we are touching the Z,W fields in the pre-translated writemask.
752 *
753 * src channels have similiar index modifications along with swizzle
754 * changes to we pick the XY, ZW pairs from the correct index.
755 *
756 * GLSL [0].x -> TGSI [0].xy
757 * GLSL [0].y -> TGSI [0].zw
758 * GLSL [0].z -> TGSI [1].xy
759 * GLSL [0].w -> TGSI [1].zw
760 */
761 for (j = 0; j < 2; j++) {
762 dst_is_64bit[j] = glsl_base_type_is_64bit(inst->dst[j].type);
763 if (!dst_is_64bit[j] && inst->dst[j].file == PROGRAM_OUTPUT && inst->dst[j].type == GLSL_TYPE_ARRAY) {
764 enum glsl_base_type type = find_array_type(this->outputs, this->num_outputs, inst->dst[j].array_id);
765 if (glsl_base_type_is_64bit(type))
766 dst_is_64bit[j] = true;
767 }
768 }
769
770 if (dst_is_64bit[0] || dst_is_64bit[1] ||
771 glsl_base_type_is_64bit(inst->src[0].type)) {
772 glsl_to_tgsi_instruction *dinst = NULL;
773 int initial_src_swz[4], initial_src_idx[4];
774 int initial_dst_idx[2], initial_dst_writemask[2];
775 /* select the writemask for dst0 or dst1 */
776 unsigned writemask = inst->dst[1].file == PROGRAM_UNDEFINED ? inst->dst[0].writemask : inst->dst[1].writemask;
777
778 /* copy out the writemask, index and swizzles for all src/dsts. */
779 for (j = 0; j < 2; j++) {
780 initial_dst_writemask[j] = inst->dst[j].writemask;
781 initial_dst_idx[j] = inst->dst[j].index;
782 }
783
784 for (j = 0; j < 4; j++) {
785 initial_src_swz[j] = inst->src[j].swizzle;
786 initial_src_idx[j] = inst->src[j].index;
787 }
788
789 /*
790 * scan all the components in the dst writemask
791 * generate an instruction for each of them if required.
792 */
793 st_src_reg addr;
794 while (writemask) {
795
796 int i = u_bit_scan(&writemask);
797
798 /* before emitting the instruction, see if we have to adjust load / store
799 * address */
800 if (i > 1 && (inst->op == TGSI_OPCODE_LOAD || inst->op == TGSI_OPCODE_STORE) &&
801 addr.file == PROGRAM_UNDEFINED) {
802 /* We have to advance the buffer address by 16 */
803 addr = get_temp(glsl_type::uint_type);
804 emit_asm(ir, TGSI_OPCODE_UADD, st_dst_reg(addr),
805 inst->src[0], st_src_reg_for_int(16));
806 }
807
808 /* first time use previous instruction */
809 if (dinst == NULL) {
810 dinst = inst;
811 } else {
812 /* create a new instructions for subsequent attempts */
813 dinst = new(mem_ctx) glsl_to_tgsi_instruction();
814 *dinst = *inst;
815 dinst->next = NULL;
816 dinst->prev = NULL;
817 }
818 this->instructions.push_tail(dinst);
819 dinst->is_64bit_expanded = true;
820
821 /* modify the destination if we are splitting */
822 for (j = 0; j < 2; j++) {
823 if (dst_is_64bit[j]) {
824 dinst->dst[j].writemask = (i & 1) ? WRITEMASK_ZW : WRITEMASK_XY;
825 dinst->dst[j].index = initial_dst_idx[j];
826 if (i > 1) {
827 if (dinst->op == TGSI_OPCODE_LOAD || dinst->op == TGSI_OPCODE_STORE)
828 dinst->src[0] = addr;
829 if (dinst->op != TGSI_OPCODE_STORE)
830 dinst->dst[j].index++;
831 }
832 } else {
833 /* if we aren't writing to a double, just get the bit of the initial writemask
834 for this channel */
835 dinst->dst[j].writemask = initial_dst_writemask[j] & (1 << i);
836 }
837 }
838
839 /* modify the src registers */
840 for (j = 0; j < 4; j++) {
841 int swz = GET_SWZ(initial_src_swz[j], i);
842
843 if (glsl_base_type_is_64bit(dinst->src[j].type)) {
844 dinst->src[j].index = initial_src_idx[j];
845 if (swz > 1) {
846 dinst->src[j].double_reg2 = true;
847 dinst->src[j].index++;
848 }
849
850 if (swz & 1)
851 dinst->src[j].swizzle = MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, SWIZZLE_Z, SWIZZLE_W);
852 else
853 dinst->src[j].swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_X, SWIZZLE_Y);
854
855 } else {
856 /* some opcodes are special case in what they use as sources
857 - [FUI]2D/[UI]2I64 is a float/[u]int src0, DLDEXP is integer src1 */
858 if (op == TGSI_OPCODE_F2D || op == TGSI_OPCODE_U2D || op == TGSI_OPCODE_I2D ||
859 op == TGSI_OPCODE_I2I64 || op == TGSI_OPCODE_U2I64 ||
860 op == TGSI_OPCODE_DLDEXP ||
861 (op == TGSI_OPCODE_UCMP && dst_is_64bit[0])) {
862 dinst->src[j].swizzle = MAKE_SWIZZLE4(swz, swz, swz, swz);
863 }
864 }
865 }
866 }
867 inst = dinst;
868 } else {
869 this->instructions.push_tail(inst);
870 }
871
872
873 return inst;
874 }
875
876 glsl_to_tgsi_instruction *
877 glsl_to_tgsi_visitor::emit_asm(ir_instruction *ir, unsigned op,
878 st_dst_reg dst,
879 st_src_reg src0, st_src_reg src1,
880 st_src_reg src2, st_src_reg src3)
881 {
882 return emit_asm(ir, op, dst, undef_dst, src0, src1, src2, src3);
883 }
884
885 /**
886 * Determines whether to use an integer, unsigned integer, or float opcode
887 * based on the operands and input opcode, then emits the result.
888 */
889 unsigned
890 glsl_to_tgsi_visitor::get_opcode(unsigned op,
891 st_dst_reg dst,
892 st_src_reg src0, st_src_reg src1)
893 {
894 enum glsl_base_type type = GLSL_TYPE_FLOAT;
895
896 if (op == TGSI_OPCODE_MOV)
897 return op;
898
899 assert(src0.type != GLSL_TYPE_ARRAY);
900 assert(src0.type != GLSL_TYPE_STRUCT);
901 assert(src1.type != GLSL_TYPE_ARRAY);
902 assert(src1.type != GLSL_TYPE_STRUCT);
903
904 if (is_resource_instruction(op))
905 type = src1.type;
906 else if (src0.type == GLSL_TYPE_INT64 || src1.type == GLSL_TYPE_INT64)
907 type = GLSL_TYPE_INT64;
908 else if (src0.type == GLSL_TYPE_UINT64 || src1.type == GLSL_TYPE_UINT64)
909 type = GLSL_TYPE_UINT64;
910 else if (src0.type == GLSL_TYPE_DOUBLE || src1.type == GLSL_TYPE_DOUBLE)
911 type = GLSL_TYPE_DOUBLE;
912 else if (src0.type == GLSL_TYPE_FLOAT || src1.type == GLSL_TYPE_FLOAT)
913 type = GLSL_TYPE_FLOAT;
914 else if (native_integers)
915 type = src0.type == GLSL_TYPE_BOOL ? GLSL_TYPE_INT : src0.type;
916
917 #define case7(c, f, i, u, d, i64, ui64) \
918 case TGSI_OPCODE_##c: \
919 if (type == GLSL_TYPE_UINT64) \
920 op = TGSI_OPCODE_##ui64; \
921 else if (type == GLSL_TYPE_INT64) \
922 op = TGSI_OPCODE_##i64; \
923 else if (type == GLSL_TYPE_DOUBLE) \
924 op = TGSI_OPCODE_##d; \
925 else if (type == GLSL_TYPE_INT) \
926 op = TGSI_OPCODE_##i; \
927 else if (type == GLSL_TYPE_UINT) \
928 op = TGSI_OPCODE_##u; \
929 else \
930 op = TGSI_OPCODE_##f; \
931 break;
932 #define case5(c, f, i, u, d) \
933 case TGSI_OPCODE_##c: \
934 if (type == GLSL_TYPE_DOUBLE) \
935 op = TGSI_OPCODE_##d; \
936 else if (type == GLSL_TYPE_INT) \
937 op = TGSI_OPCODE_##i; \
938 else if (type == GLSL_TYPE_UINT) \
939 op = TGSI_OPCODE_##u; \
940 else \
941 op = TGSI_OPCODE_##f; \
942 break;
943
944 #define case4(c, f, i, u) \
945 case TGSI_OPCODE_##c: \
946 if (type == GLSL_TYPE_INT) \
947 op = TGSI_OPCODE_##i; \
948 else if (type == GLSL_TYPE_UINT) \
949 op = TGSI_OPCODE_##u; \
950 else \
951 op = TGSI_OPCODE_##f; \
952 break;
953
954 #define case3(f, i, u) case4(f, f, i, u)
955 #define case6d(f, i, u, d, i64, u64) case7(f, f, i, u, d, i64, u64)
956 #define case3fid(f, i, d) case5(f, f, i, i, d)
957 #define case3fid64(f, i, d, i64) case7(f, f, i, i, d, i64, i64)
958 #define case2fi(f, i) case4(f, f, i, i)
959 #define case2iu(i, u) case4(i, LAST, i, u)
960
961 #define case2iu64(i, i64) case7(i, LAST, i, i, LAST, i64, i64)
962 #define case4iu64(i, u, i64, u64) case7(i, LAST, i, u, LAST, i64, u64)
963
964 #define casecomp(c, f, i, u, d, i64, ui64) \
965 case TGSI_OPCODE_##c: \
966 if (type == GLSL_TYPE_INT64) \
967 op = TGSI_OPCODE_##i64; \
968 else if (type == GLSL_TYPE_UINT64) \
969 op = TGSI_OPCODE_##ui64; \
970 else if (type == GLSL_TYPE_DOUBLE) \
971 op = TGSI_OPCODE_##d; \
972 else if (type == GLSL_TYPE_INT || type == GLSL_TYPE_SUBROUTINE) \
973 op = TGSI_OPCODE_##i; \
974 else if (type == GLSL_TYPE_UINT) \
975 op = TGSI_OPCODE_##u; \
976 else if (native_integers) \
977 op = TGSI_OPCODE_##f; \
978 else \
979 op = TGSI_OPCODE_##c; \
980 break;
981
982 switch(op) {
983 case3fid64(ADD, UADD, DADD, U64ADD);
984 case3fid64(MUL, UMUL, DMUL, U64MUL);
985 case3fid(MAD, UMAD, DMAD);
986 case3fid(FMA, UMAD, DFMA);
987 case6d(DIV, IDIV, UDIV, DDIV, I64DIV, U64DIV);
988 case6d(MAX, IMAX, UMAX, DMAX, I64MAX, U64MAX);
989 case6d(MIN, IMIN, UMIN, DMIN, I64MIN, U64MIN);
990 case4iu64(MOD, UMOD, I64MOD, U64MOD);
991
992 casecomp(SEQ, FSEQ, USEQ, USEQ, DSEQ, U64SEQ, U64SEQ);
993 casecomp(SNE, FSNE, USNE, USNE, DSNE, U64SNE, U64SNE);
994 casecomp(SGE, FSGE, ISGE, USGE, DSGE, I64SGE, U64SGE);
995 casecomp(SLT, FSLT, ISLT, USLT, DSLT, I64SLT, U64SLT);
996
997 case2iu64(SHL, U64SHL);
998 case4iu64(ISHR, USHR, I64SHR, U64SHR);
999
1000 case3fid64(SSG, ISSG, DSSG, I64SSG);
1001
1002 case2iu(IBFE, UBFE);
1003 case2iu(IMSB, UMSB);
1004 case2iu(IMUL_HI, UMUL_HI);
1005
1006 case3fid(SQRT, SQRT, DSQRT);
1007
1008 case3fid(RCP, RCP, DRCP);
1009 case3fid(RSQ, RSQ, DRSQ);
1010
1011 case3fid(FRC, FRC, DFRAC);
1012 case3fid(TRUNC, TRUNC, DTRUNC);
1013 case3fid(CEIL, CEIL, DCEIL);
1014 case3fid(FLR, FLR, DFLR);
1015 case3fid(ROUND, ROUND, DROUND);
1016
1017 case2iu(ATOMIMAX, ATOMUMAX);
1018 case2iu(ATOMIMIN, ATOMUMIN);
1019
1020 default: break;
1021 }
1022
1023 assert(op != TGSI_OPCODE_LAST);
1024 return op;
1025 }
1026
1027 glsl_to_tgsi_instruction *
1028 glsl_to_tgsi_visitor::emit_dp(ir_instruction *ir,
1029 st_dst_reg dst, st_src_reg src0, st_src_reg src1,
1030 unsigned elements)
1031 {
1032 static const unsigned dot_opcodes[] = {
1033 TGSI_OPCODE_DP2, TGSI_OPCODE_DP3, TGSI_OPCODE_DP4
1034 };
1035
1036 return emit_asm(ir, dot_opcodes[elements - 2], dst, src0, src1);
1037 }
1038
1039 /**
1040 * Emits TGSI scalar opcodes to produce unique answers across channels.
1041 *
1042 * Some TGSI opcodes are scalar-only, like ARB_fp/vp. The src X
1043 * channel determines the result across all channels. So to do a vec4
1044 * of this operation, we want to emit a scalar per source channel used
1045 * to produce dest channels.
1046 */
1047 void
1048 glsl_to_tgsi_visitor::emit_scalar(ir_instruction *ir, unsigned op,
1049 st_dst_reg dst,
1050 st_src_reg orig_src0, st_src_reg orig_src1)
1051 {
1052 int i, j;
1053 int done_mask = ~dst.writemask;
1054
1055 /* TGSI RCP is a scalar operation splatting results to all channels,
1056 * like ARB_fp/vp. So emit as many RCPs as necessary to cover our
1057 * dst channels.
1058 */
1059 for (i = 0; i < 4; i++) {
1060 GLuint this_mask = (1 << i);
1061 st_src_reg src0 = orig_src0;
1062 st_src_reg src1 = orig_src1;
1063
1064 if (done_mask & this_mask)
1065 continue;
1066
1067 GLuint src0_swiz = GET_SWZ(src0.swizzle, i);
1068 GLuint src1_swiz = GET_SWZ(src1.swizzle, i);
1069 for (j = i + 1; j < 4; j++) {
1070 /* If there is another enabled component in the destination that is
1071 * derived from the same inputs, generate its value on this pass as
1072 * well.
1073 */
1074 if (!(done_mask & (1 << j)) &&
1075 GET_SWZ(src0.swizzle, j) == src0_swiz &&
1076 GET_SWZ(src1.swizzle, j) == src1_swiz) {
1077 this_mask |= (1 << j);
1078 }
1079 }
1080 src0.swizzle = MAKE_SWIZZLE4(src0_swiz, src0_swiz,
1081 src0_swiz, src0_swiz);
1082 src1.swizzle = MAKE_SWIZZLE4(src1_swiz, src1_swiz,
1083 src1_swiz, src1_swiz);
1084
1085 dst.writemask = this_mask;
1086 emit_asm(ir, op, dst, src0, src1);
1087 done_mask |= this_mask;
1088 }
1089 }
1090
1091 void
1092 glsl_to_tgsi_visitor::emit_scalar(ir_instruction *ir, unsigned op,
1093 st_dst_reg dst, st_src_reg src0)
1094 {
1095 st_src_reg undef = undef_src;
1096
1097 undef.swizzle = SWIZZLE_XXXX;
1098
1099 emit_scalar(ir, op, dst, src0, undef);
1100 }
1101
1102 void
1103 glsl_to_tgsi_visitor::emit_arl(ir_instruction *ir,
1104 st_dst_reg dst, st_src_reg src0)
1105 {
1106 int op = TGSI_OPCODE_ARL;
1107
1108 if (src0.type == GLSL_TYPE_INT || src0.type == GLSL_TYPE_UINT)
1109 op = TGSI_OPCODE_UARL;
1110
1111 assert(dst.file == PROGRAM_ADDRESS);
1112 if (dst.index >= this->num_address_regs)
1113 this->num_address_regs = dst.index + 1;
1114
1115 emit_asm(NULL, op, dst, src0);
1116 }
1117
1118 int
1119 glsl_to_tgsi_visitor::add_constant(gl_register_file file,
1120 gl_constant_value values[8], int size, int datatype,
1121 uint16_t *swizzle_out)
1122 {
1123 if (file == PROGRAM_CONSTANT) {
1124 GLuint swizzle = swizzle_out ? *swizzle_out : 0;
1125 int result = _mesa_add_typed_unnamed_constant(this->prog->Parameters, values,
1126 size, datatype, &swizzle);
1127 if (swizzle_out)
1128 *swizzle_out = swizzle;
1129 return result;
1130 }
1131
1132 assert(file == PROGRAM_IMMEDIATE);
1133
1134 int index = 0;
1135 immediate_storage *entry;
1136 int size32 = size * ((datatype == GL_DOUBLE ||
1137 datatype == GL_INT64_ARB ||
1138 datatype == GL_UNSIGNED_INT64_ARB)? 2 : 1);
1139 int i;
1140
1141 /* Search immediate storage to see if we already have an identical
1142 * immediate that we can use instead of adding a duplicate entry.
1143 */
1144 foreach_in_list(immediate_storage, entry, &this->immediates) {
1145 immediate_storage *tmp = entry;
1146
1147 for (i = 0; i * 4 < size32; i++) {
1148 int slot_size = MIN2(size32 - (i * 4), 4);
1149 if (tmp->type != datatype || tmp->size32 != slot_size)
1150 break;
1151 if (memcmp(tmp->values, &values[i * 4],
1152 slot_size * sizeof(gl_constant_value)))
1153 break;
1154
1155 /* Everything matches, keep going until the full size is matched */
1156 tmp = (immediate_storage *)tmp->next;
1157 }
1158
1159 /* The full value matched */
1160 if (i * 4 >= size32)
1161 return index;
1162
1163 index++;
1164 }
1165
1166 for (i = 0; i * 4 < size32; i++) {
1167 int slot_size = MIN2(size32 - (i * 4), 4);
1168 /* Add this immediate to the list. */
1169 entry = new(mem_ctx) immediate_storage(&values[i * 4], slot_size, datatype);
1170 this->immediates.push_tail(entry);
1171 this->num_immediates++;
1172 }
1173 return index;
1174 }
1175
1176 st_src_reg
1177 glsl_to_tgsi_visitor::st_src_reg_for_float(float val)
1178 {
1179 st_src_reg src(PROGRAM_IMMEDIATE, -1, GLSL_TYPE_FLOAT);
1180 union gl_constant_value uval;
1181
1182 uval.f = val;
1183 src.index = add_constant(src.file, &uval, 1, GL_FLOAT, &src.swizzle);
1184
1185 return src;
1186 }
1187
1188 st_src_reg
1189 glsl_to_tgsi_visitor::st_src_reg_for_double(double val)
1190 {
1191 st_src_reg src(PROGRAM_IMMEDIATE, -1, GLSL_TYPE_DOUBLE);
1192 union gl_constant_value uval[2];
1193
1194 memcpy(uval, &val, sizeof(uval));
1195 src.index = add_constant(src.file, uval, 1, GL_DOUBLE, &src.swizzle);
1196 src.swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_X, SWIZZLE_Y);
1197 return src;
1198 }
1199
1200 st_src_reg
1201 glsl_to_tgsi_visitor::st_src_reg_for_int(int val)
1202 {
1203 st_src_reg src(PROGRAM_IMMEDIATE, -1, GLSL_TYPE_INT);
1204 union gl_constant_value uval;
1205
1206 assert(native_integers);
1207
1208 uval.i = val;
1209 src.index = add_constant(src.file, &uval, 1, GL_INT, &src.swizzle);
1210
1211 return src;
1212 }
1213
1214 st_src_reg
1215 glsl_to_tgsi_visitor::st_src_reg_for_type(enum glsl_base_type type, int val)
1216 {
1217 if (native_integers)
1218 return type == GLSL_TYPE_FLOAT ? st_src_reg_for_float(val) :
1219 st_src_reg_for_int(val);
1220 else
1221 return st_src_reg_for_float(val);
1222 }
1223
1224 static int
1225 attrib_type_size(const struct glsl_type *type, bool is_vs_input)
1226 {
1227 return st_glsl_attrib_type_size(type, is_vs_input);
1228 }
1229
1230 static int
1231 type_size(const struct glsl_type *type)
1232 {
1233 return st_glsl_type_size(type);
1234 }
1235
1236 /**
1237 * If the given GLSL type is an array or matrix or a structure containing
1238 * an array/matrix member, return true. Else return false.
1239 *
1240 * This is used to determine which kind of temp storage (PROGRAM_TEMPORARY
1241 * or PROGRAM_ARRAY) should be used for variables of this type. Anytime
1242 * we have an array that might be indexed with a variable, we need to use
1243 * the later storage type.
1244 */
1245 static bool
1246 type_has_array_or_matrix(const glsl_type *type)
1247 {
1248 if (type->is_array() || type->is_matrix())
1249 return true;
1250
1251 if (type->is_record()) {
1252 for (unsigned i = 0; i < type->length; i++) {
1253 if (type_has_array_or_matrix(type->fields.structure[i].type)) {
1254 return true;
1255 }
1256 }
1257 }
1258
1259 return false;
1260 }
1261
1262
1263 /**
1264 * In the initial pass of codegen, we assign temporary numbers to
1265 * intermediate results. (not SSA -- variable assignments will reuse
1266 * storage).
1267 */
1268 st_src_reg
1269 glsl_to_tgsi_visitor::get_temp(const glsl_type *type)
1270 {
1271 st_src_reg src;
1272
1273 src.type = native_integers ? type->base_type : GLSL_TYPE_FLOAT;
1274 src.reladdr = NULL;
1275 src.negate = 0;
1276 src.abs = 0;
1277
1278 if (!options->EmitNoIndirectTemp && type_has_array_or_matrix(type)) {
1279 if (next_array >= max_num_arrays) {
1280 max_num_arrays += 32;
1281 array_sizes = (unsigned*)
1282 realloc(array_sizes, sizeof(array_sizes[0]) * max_num_arrays);
1283 }
1284
1285 src.file = PROGRAM_ARRAY;
1286 src.index = 0;
1287 src.array_id = next_array + 1;
1288 array_sizes[next_array] = type_size(type);
1289 ++next_array;
1290
1291 } else {
1292 src.file = PROGRAM_TEMPORARY;
1293 src.index = next_temp;
1294 next_temp += type_size(type);
1295 }
1296
1297 if (type->is_array() || type->is_record()) {
1298 src.swizzle = SWIZZLE_NOOP;
1299 } else {
1300 src.swizzle = swizzle_for_size(type->vector_elements);
1301 }
1302
1303 return src;
1304 }
1305
1306 variable_storage *
1307 glsl_to_tgsi_visitor::find_variable_storage(ir_variable *var)
1308 {
1309
1310 foreach_in_list(variable_storage, entry, &this->variables) {
1311 if (entry->var == var)
1312 return entry;
1313 }
1314
1315 return NULL;
1316 }
1317
1318 void
1319 glsl_to_tgsi_visitor::visit(ir_variable *ir)
1320 {
1321 if (strcmp(ir->name, "gl_FragCoord") == 0) {
1322 this->prog->OriginUpperLeft = ir->data.origin_upper_left;
1323 this->prog->PixelCenterInteger = ir->data.pixel_center_integer;
1324 }
1325
1326 if (ir->data.mode == ir_var_uniform && strncmp(ir->name, "gl_", 3) == 0) {
1327 unsigned int i;
1328 const ir_state_slot *const slots = ir->get_state_slots();
1329 assert(slots != NULL);
1330
1331 /* Check if this statevar's setup in the STATE file exactly
1332 * matches how we'll want to reference it as a
1333 * struct/array/whatever. If not, then we need to move it into
1334 * temporary storage and hope that it'll get copy-propagated
1335 * out.
1336 */
1337 for (i = 0; i < ir->get_num_state_slots(); i++) {
1338 if (slots[i].swizzle != SWIZZLE_XYZW) {
1339 break;
1340 }
1341 }
1342
1343 variable_storage *storage;
1344 st_dst_reg dst;
1345 if (i == ir->get_num_state_slots()) {
1346 /* We'll set the index later. */
1347 storage = new(mem_ctx) variable_storage(ir, PROGRAM_STATE_VAR, -1);
1348 this->variables.push_tail(storage);
1349
1350 dst = undef_dst;
1351 } else {
1352 /* The variable_storage constructor allocates slots based on the size
1353 * of the type. However, this had better match the number of state
1354 * elements that we're going to copy into the new temporary.
1355 */
1356 assert((int) ir->get_num_state_slots() == type_size(ir->type));
1357
1358 dst = st_dst_reg(get_temp(ir->type));
1359
1360 storage = new(mem_ctx) variable_storage(ir, dst.file, dst.index,
1361 dst.array_id);
1362
1363 this->variables.push_tail(storage);
1364 }
1365
1366
1367 for (unsigned int i = 0; i < ir->get_num_state_slots(); i++) {
1368 int index = _mesa_add_state_reference(this->prog->Parameters,
1369 (gl_state_index *)slots[i].tokens);
1370
1371 if (storage->file == PROGRAM_STATE_VAR) {
1372 if (storage->index == -1) {
1373 storage->index = index;
1374 } else {
1375 assert(index == storage->index + (int)i);
1376 }
1377 } else {
1378 /* We use GLSL_TYPE_FLOAT here regardless of the actual type of
1379 * the data being moved since MOV does not care about the type of
1380 * data it is moving, and we don't want to declare registers with
1381 * array or struct types.
1382 */
1383 st_src_reg src(PROGRAM_STATE_VAR, index, GLSL_TYPE_FLOAT);
1384 src.swizzle = slots[i].swizzle;
1385 emit_asm(ir, TGSI_OPCODE_MOV, dst, src);
1386 /* even a float takes up a whole vec4 reg in a struct/array. */
1387 dst.index++;
1388 }
1389 }
1390
1391 if (storage->file == PROGRAM_TEMPORARY &&
1392 dst.index != storage->index + (int) ir->get_num_state_slots()) {
1393 fail_link(this->shader_program,
1394 "failed to load builtin uniform `%s' (%d/%d regs loaded)\n",
1395 ir->name, dst.index - storage->index,
1396 type_size(ir->type));
1397 }
1398 }
1399 }
1400
1401 void
1402 glsl_to_tgsi_visitor::visit(ir_loop *ir)
1403 {
1404 emit_asm(NULL, TGSI_OPCODE_BGNLOOP);
1405
1406 visit_exec_list(&ir->body_instructions, this);
1407
1408 emit_asm(NULL, TGSI_OPCODE_ENDLOOP);
1409 }
1410
1411 void
1412 glsl_to_tgsi_visitor::visit(ir_loop_jump *ir)
1413 {
1414 switch (ir->mode) {
1415 case ir_loop_jump::jump_break:
1416 emit_asm(NULL, TGSI_OPCODE_BRK);
1417 break;
1418 case ir_loop_jump::jump_continue:
1419 emit_asm(NULL, TGSI_OPCODE_CONT);
1420 break;
1421 }
1422 }
1423
1424
1425 void
1426 glsl_to_tgsi_visitor::visit(ir_function_signature *ir)
1427 {
1428 assert(0);
1429 (void)ir;
1430 }
1431
1432 void
1433 glsl_to_tgsi_visitor::visit(ir_function *ir)
1434 {
1435 /* Ignore function bodies other than main() -- we shouldn't see calls to
1436 * them since they should all be inlined before we get to glsl_to_tgsi.
1437 */
1438 if (strcmp(ir->name, "main") == 0) {
1439 const ir_function_signature *sig;
1440 exec_list empty;
1441
1442 sig = ir->matching_signature(NULL, &empty, false);
1443
1444 assert(sig);
1445
1446 foreach_in_list(ir_instruction, ir, &sig->body) {
1447 ir->accept(this);
1448 }
1449 }
1450 }
1451
1452 bool
1453 glsl_to_tgsi_visitor::try_emit_mad(ir_expression *ir, int mul_operand)
1454 {
1455 int nonmul_operand = 1 - mul_operand;
1456 st_src_reg a, b, c;
1457 st_dst_reg result_dst;
1458
1459 ir_expression *expr = ir->operands[mul_operand]->as_expression();
1460 if (!expr || expr->operation != ir_binop_mul)
1461 return false;
1462
1463 expr->operands[0]->accept(this);
1464 a = this->result;
1465 expr->operands[1]->accept(this);
1466 b = this->result;
1467 ir->operands[nonmul_operand]->accept(this);
1468 c = this->result;
1469
1470 this->result = get_temp(ir->type);
1471 result_dst = st_dst_reg(this->result);
1472 result_dst.writemask = (1 << ir->type->vector_elements) - 1;
1473 emit_asm(ir, TGSI_OPCODE_MAD, result_dst, a, b, c);
1474
1475 return true;
1476 }
1477
1478 /**
1479 * Emit MAD(a, -b, a) instead of AND(a, NOT(b))
1480 *
1481 * The logic values are 1.0 for true and 0.0 for false. Logical-and is
1482 * implemented using multiplication, and logical-or is implemented using
1483 * addition. Logical-not can be implemented as (true - x), or (1.0 - x).
1484 * As result, the logical expression (a & !b) can be rewritten as:
1485 *
1486 * - a * !b
1487 * - a * (1 - b)
1488 * - (a * 1) - (a * b)
1489 * - a + -(a * b)
1490 * - a + (a * -b)
1491 *
1492 * This final expression can be implemented as a single MAD(a, -b, a)
1493 * instruction.
1494 */
1495 bool
1496 glsl_to_tgsi_visitor::try_emit_mad_for_and_not(ir_expression *ir, int try_operand)
1497 {
1498 const int other_operand = 1 - try_operand;
1499 st_src_reg a, b;
1500
1501 ir_expression *expr = ir->operands[try_operand]->as_expression();
1502 if (!expr || expr->operation != ir_unop_logic_not)
1503 return false;
1504
1505 ir->operands[other_operand]->accept(this);
1506 a = this->result;
1507 expr->operands[0]->accept(this);
1508 b = this->result;
1509
1510 b.negate = ~b.negate;
1511
1512 this->result = get_temp(ir->type);
1513 emit_asm(ir, TGSI_OPCODE_MAD, st_dst_reg(this->result), a, b, a);
1514
1515 return true;
1516 }
1517
1518 void
1519 glsl_to_tgsi_visitor::reladdr_to_temp(ir_instruction *ir,
1520 st_src_reg *reg, int *num_reladdr)
1521 {
1522 if (!reg->reladdr && !reg->reladdr2)
1523 return;
1524
1525 if (reg->reladdr) emit_arl(ir, address_reg, *reg->reladdr);
1526 if (reg->reladdr2) emit_arl(ir, address_reg2, *reg->reladdr2);
1527
1528 if (*num_reladdr != 1) {
1529 st_src_reg temp = get_temp(reg->type == GLSL_TYPE_DOUBLE ? glsl_type::dvec4_type : glsl_type::vec4_type);
1530
1531 emit_asm(ir, TGSI_OPCODE_MOV, st_dst_reg(temp), *reg);
1532 *reg = temp;
1533 }
1534
1535 (*num_reladdr)--;
1536 }
1537
1538 void
1539 glsl_to_tgsi_visitor::visit(ir_expression *ir)
1540 {
1541 st_src_reg op[ARRAY_SIZE(ir->operands)];
1542
1543 /* Quick peephole: Emit MAD(a, b, c) instead of ADD(MUL(a, b), c)
1544 */
1545 if (ir->operation == ir_binop_add) {
1546 if (try_emit_mad(ir, 1))
1547 return;
1548 if (try_emit_mad(ir, 0))
1549 return;
1550 }
1551
1552 /* Quick peephole: Emit OPCODE_MAD(-a, -b, a) instead of AND(a, NOT(b))
1553 */
1554 if (!native_integers && ir->operation == ir_binop_logic_and) {
1555 if (try_emit_mad_for_and_not(ir, 1))
1556 return;
1557 if (try_emit_mad_for_and_not(ir, 0))
1558 return;
1559 }
1560
1561 if (ir->operation == ir_quadop_vector)
1562 assert(!"ir_quadop_vector should have been lowered");
1563
1564 for (unsigned int operand = 0; operand < ir->get_num_operands(); operand++) {
1565 this->result.file = PROGRAM_UNDEFINED;
1566 ir->operands[operand]->accept(this);
1567 if (this->result.file == PROGRAM_UNDEFINED) {
1568 printf("Failed to get tree for expression operand:\n");
1569 ir->operands[operand]->print();
1570 printf("\n");
1571 exit(1);
1572 }
1573 op[operand] = this->result;
1574
1575 /* Matrix expression operands should have been broken down to vector
1576 * operations already.
1577 */
1578 assert(!ir->operands[operand]->type->is_matrix());
1579 }
1580
1581 visit_expression(ir, op);
1582 }
1583
1584 /* The non-recursive part of the expression visitor lives in a separate
1585 * function and should be prevented from being inlined, to avoid a stack
1586 * explosion when deeply nested expressions are visited.
1587 */
1588 void
1589 glsl_to_tgsi_visitor::visit_expression(ir_expression* ir, st_src_reg *op)
1590 {
1591 st_src_reg result_src;
1592 st_dst_reg result_dst;
1593
1594 int vector_elements = ir->operands[0]->type->vector_elements;
1595 if (ir->operands[1]) {
1596 vector_elements = MAX2(vector_elements,
1597 ir->operands[1]->type->vector_elements);
1598 }
1599
1600 this->result.file = PROGRAM_UNDEFINED;
1601
1602 /* Storage for our result. Ideally for an assignment we'd be using
1603 * the actual storage for the result here, instead.
1604 */
1605 result_src = get_temp(ir->type);
1606 /* convenience for the emit functions below. */
1607 result_dst = st_dst_reg(result_src);
1608 /* Limit writes to the channels that will be used by result_src later.
1609 * This does limit this temp's use as a temporary for multi-instruction
1610 * sequences.
1611 */
1612 result_dst.writemask = (1 << ir->type->vector_elements) - 1;
1613
1614 switch (ir->operation) {
1615 case ir_unop_logic_not:
1616 if (result_dst.type != GLSL_TYPE_FLOAT)
1617 emit_asm(ir, TGSI_OPCODE_NOT, result_dst, op[0]);
1618 else {
1619 /* Previously 'SEQ dst, src, 0.0' was used for this. However, many
1620 * older GPUs implement SEQ using multiple instructions (i915 uses two
1621 * SGE instructions and a MUL instruction). Since our logic values are
1622 * 0.0 and 1.0, 1-x also implements !x.
1623 */
1624 op[0].negate = ~op[0].negate;
1625 emit_asm(ir, TGSI_OPCODE_ADD, result_dst, op[0], st_src_reg_for_float(1.0));
1626 }
1627 break;
1628 case ir_unop_neg:
1629 if (result_dst.type == GLSL_TYPE_INT64 || result_dst.type == GLSL_TYPE_UINT64)
1630 emit_asm(ir, TGSI_OPCODE_I64NEG, result_dst, op[0]);
1631 else if (result_dst.type == GLSL_TYPE_INT || result_dst.type == GLSL_TYPE_UINT)
1632 emit_asm(ir, TGSI_OPCODE_INEG, result_dst, op[0]);
1633 else if (result_dst.type == GLSL_TYPE_DOUBLE)
1634 emit_asm(ir, TGSI_OPCODE_DNEG, result_dst, op[0]);
1635 else {
1636 op[0].negate = ~op[0].negate;
1637 result_src = op[0];
1638 }
1639 break;
1640 case ir_unop_subroutine_to_int:
1641 emit_asm(ir, TGSI_OPCODE_MOV, result_dst, op[0]);
1642 break;
1643 case ir_unop_abs:
1644 if (result_dst.type == GLSL_TYPE_FLOAT)
1645 emit_asm(ir, TGSI_OPCODE_MOV, result_dst, op[0].get_abs());
1646 else if (result_dst.type == GLSL_TYPE_DOUBLE)
1647 emit_asm(ir, TGSI_OPCODE_DABS, result_dst, op[0]);
1648 else if (result_dst.type == GLSL_TYPE_INT64 || result_dst.type == GLSL_TYPE_UINT64)
1649 emit_asm(ir, TGSI_OPCODE_I64ABS, result_dst, op[0]);
1650 else
1651 emit_asm(ir, TGSI_OPCODE_IABS, result_dst, op[0]);
1652 break;
1653 case ir_unop_sign:
1654 emit_asm(ir, TGSI_OPCODE_SSG, result_dst, op[0]);
1655 break;
1656 case ir_unop_rcp:
1657 emit_scalar(ir, TGSI_OPCODE_RCP, result_dst, op[0]);
1658 break;
1659
1660 case ir_unop_exp2:
1661 emit_scalar(ir, TGSI_OPCODE_EX2, result_dst, op[0]);
1662 break;
1663 case ir_unop_exp:
1664 case ir_unop_log:
1665 assert(!"not reached: should be handled by ir_explog_to_explog2");
1666 break;
1667 case ir_unop_log2:
1668 emit_scalar(ir, TGSI_OPCODE_LG2, result_dst, op[0]);
1669 break;
1670 case ir_unop_sin:
1671 emit_scalar(ir, TGSI_OPCODE_SIN, result_dst, op[0]);
1672 break;
1673 case ir_unop_cos:
1674 emit_scalar(ir, TGSI_OPCODE_COS, result_dst, op[0]);
1675 break;
1676 case ir_unop_saturate: {
1677 glsl_to_tgsi_instruction *inst;
1678 inst = emit_asm(ir, TGSI_OPCODE_MOV, result_dst, op[0]);
1679 inst->saturate = true;
1680 break;
1681 }
1682
1683 case ir_unop_dFdx:
1684 case ir_unop_dFdx_coarse:
1685 emit_asm(ir, TGSI_OPCODE_DDX, result_dst, op[0]);
1686 break;
1687 case ir_unop_dFdx_fine:
1688 emit_asm(ir, TGSI_OPCODE_DDX_FINE, result_dst, op[0]);
1689 break;
1690 case ir_unop_dFdy:
1691 case ir_unop_dFdy_coarse:
1692 case ir_unop_dFdy_fine:
1693 {
1694 /* The X component contains 1 or -1 depending on whether the framebuffer
1695 * is a FBO or the window system buffer, respectively.
1696 * It is then multiplied with the source operand of DDY.
1697 */
1698 static const gl_state_index transform_y_state[STATE_LENGTH]
1699 = { STATE_INTERNAL, STATE_FB_WPOS_Y_TRANSFORM };
1700
1701 unsigned transform_y_index =
1702 _mesa_add_state_reference(this->prog->Parameters,
1703 transform_y_state);
1704
1705 st_src_reg transform_y = st_src_reg(PROGRAM_STATE_VAR,
1706 transform_y_index,
1707 glsl_type::vec4_type);
1708 transform_y.swizzle = SWIZZLE_XXXX;
1709
1710 st_src_reg temp = get_temp(glsl_type::vec4_type);
1711
1712 emit_asm(ir, TGSI_OPCODE_MUL, st_dst_reg(temp), transform_y, op[0]);
1713 emit_asm(ir, ir->operation == ir_unop_dFdy_fine ?
1714 TGSI_OPCODE_DDY_FINE : TGSI_OPCODE_DDY, result_dst, temp);
1715 break;
1716 }
1717
1718 case ir_unop_frexp_sig:
1719 emit_asm(ir, TGSI_OPCODE_DFRACEXP, result_dst, undef_dst, op[0]);
1720 break;
1721
1722 case ir_unop_frexp_exp:
1723 emit_asm(ir, TGSI_OPCODE_DFRACEXP, undef_dst, result_dst, op[0]);
1724 break;
1725
1726 case ir_unop_noise: {
1727 /* At some point, a motivated person could add a better
1728 * implementation of noise. Currently not even the nvidia
1729 * binary drivers do anything more than this. In any case, the
1730 * place to do this is in the GL state tracker, not the poor
1731 * driver.
1732 */
1733 emit_asm(ir, TGSI_OPCODE_MOV, result_dst, st_src_reg_for_float(0.5));
1734 break;
1735 }
1736
1737 case ir_binop_add:
1738 emit_asm(ir, TGSI_OPCODE_ADD, result_dst, op[0], op[1]);
1739 break;
1740 case ir_binop_sub:
1741 op[1].negate = ~op[1].negate;
1742 emit_asm(ir, TGSI_OPCODE_ADD, result_dst, op[0], op[1]);
1743 break;
1744
1745 case ir_binop_mul:
1746 emit_asm(ir, TGSI_OPCODE_MUL, result_dst, op[0], op[1]);
1747 break;
1748 case ir_binop_div:
1749 emit_asm(ir, TGSI_OPCODE_DIV, result_dst, op[0], op[1]);
1750 break;
1751 case ir_binop_mod:
1752 if (result_dst.type == GLSL_TYPE_FLOAT)
1753 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
1754 else
1755 emit_asm(ir, TGSI_OPCODE_MOD, result_dst, op[0], op[1]);
1756 break;
1757
1758 case ir_binop_less:
1759 emit_asm(ir, TGSI_OPCODE_SLT, result_dst, op[0], op[1]);
1760 break;
1761 case ir_binop_greater:
1762 emit_asm(ir, TGSI_OPCODE_SLT, result_dst, op[1], op[0]);
1763 break;
1764 case ir_binop_lequal:
1765 emit_asm(ir, TGSI_OPCODE_SGE, result_dst, op[1], op[0]);
1766 break;
1767 case ir_binop_gequal:
1768 emit_asm(ir, TGSI_OPCODE_SGE, result_dst, op[0], op[1]);
1769 break;
1770 case ir_binop_equal:
1771 emit_asm(ir, TGSI_OPCODE_SEQ, result_dst, op[0], op[1]);
1772 break;
1773 case ir_binop_nequal:
1774 emit_asm(ir, TGSI_OPCODE_SNE, result_dst, op[0], op[1]);
1775 break;
1776 case ir_binop_all_equal:
1777 /* "==" operator producing a scalar boolean. */
1778 if (ir->operands[0]->type->is_vector() ||
1779 ir->operands[1]->type->is_vector()) {
1780 st_src_reg temp = get_temp(native_integers ?
1781 glsl_type::uvec4_type :
1782 glsl_type::vec4_type);
1783
1784 if (native_integers) {
1785 st_dst_reg temp_dst = st_dst_reg(temp);
1786 st_src_reg temp1 = st_src_reg(temp), temp2 = st_src_reg(temp);
1787
1788 if (ir->operands[0]->type->is_boolean() &&
1789 ir->operands[1]->as_constant() &&
1790 ir->operands[1]->as_constant()->is_one()) {
1791 emit_asm(ir, TGSI_OPCODE_MOV, st_dst_reg(temp), op[0]);
1792 } else {
1793 emit_asm(ir, TGSI_OPCODE_SEQ, st_dst_reg(temp), op[0], op[1]);
1794 }
1795
1796 /* Emit 1-3 AND operations to combine the SEQ results. */
1797 switch (ir->operands[0]->type->vector_elements) {
1798 case 2:
1799 break;
1800 case 3:
1801 temp_dst.writemask = WRITEMASK_Y;
1802 temp1.swizzle = SWIZZLE_YYYY;
1803 temp2.swizzle = SWIZZLE_ZZZZ;
1804 emit_asm(ir, TGSI_OPCODE_AND, temp_dst, temp1, temp2);
1805 break;
1806 case 4:
1807 temp_dst.writemask = WRITEMASK_X;
1808 temp1.swizzle = SWIZZLE_XXXX;
1809 temp2.swizzle = SWIZZLE_YYYY;
1810 emit_asm(ir, TGSI_OPCODE_AND, temp_dst, temp1, temp2);
1811 temp_dst.writemask = WRITEMASK_Y;
1812 temp1.swizzle = SWIZZLE_ZZZZ;
1813 temp2.swizzle = SWIZZLE_WWWW;
1814 emit_asm(ir, TGSI_OPCODE_AND, temp_dst, temp1, temp2);
1815 }
1816
1817 temp1.swizzle = SWIZZLE_XXXX;
1818 temp2.swizzle = SWIZZLE_YYYY;
1819 emit_asm(ir, TGSI_OPCODE_AND, result_dst, temp1, temp2);
1820 } else {
1821 emit_asm(ir, TGSI_OPCODE_SNE, st_dst_reg(temp), op[0], op[1]);
1822
1823 /* After the dot-product, the value will be an integer on the
1824 * range [0,4]. Zero becomes 1.0, and positive values become zero.
1825 */
1826 emit_dp(ir, result_dst, temp, temp, vector_elements);
1827
1828 /* Negating the result of the dot-product gives values on the range
1829 * [-4, 0]. Zero becomes 1.0, and negative values become zero.
1830 * This is achieved using SGE.
1831 */
1832 st_src_reg sge_src = result_src;
1833 sge_src.negate = ~sge_src.negate;
1834 emit_asm(ir, TGSI_OPCODE_SGE, result_dst, sge_src, st_src_reg_for_float(0.0));
1835 }
1836 } else {
1837 emit_asm(ir, TGSI_OPCODE_SEQ, result_dst, op[0], op[1]);
1838 }
1839 break;
1840 case ir_binop_any_nequal:
1841 /* "!=" operator producing a scalar boolean. */
1842 if (ir->operands[0]->type->is_vector() ||
1843 ir->operands[1]->type->is_vector()) {
1844 st_src_reg temp = get_temp(native_integers ?
1845 glsl_type::uvec4_type :
1846 glsl_type::vec4_type);
1847 if (ir->operands[0]->type->is_boolean() &&
1848 ir->operands[1]->as_constant() &&
1849 ir->operands[1]->as_constant()->is_zero()) {
1850 emit_asm(ir, TGSI_OPCODE_MOV, st_dst_reg(temp), op[0]);
1851 } else {
1852 emit_asm(ir, TGSI_OPCODE_SNE, st_dst_reg(temp), op[0], op[1]);
1853 }
1854
1855 if (native_integers) {
1856 st_dst_reg temp_dst = st_dst_reg(temp);
1857 st_src_reg temp1 = st_src_reg(temp), temp2 = st_src_reg(temp);
1858
1859 /* Emit 1-3 OR operations to combine the SNE results. */
1860 switch (ir->operands[0]->type->vector_elements) {
1861 case 2:
1862 break;
1863 case 3:
1864 temp_dst.writemask = WRITEMASK_Y;
1865 temp1.swizzle = SWIZZLE_YYYY;
1866 temp2.swizzle = SWIZZLE_ZZZZ;
1867 emit_asm(ir, TGSI_OPCODE_OR, temp_dst, temp1, temp2);
1868 break;
1869 case 4:
1870 temp_dst.writemask = WRITEMASK_X;
1871 temp1.swizzle = SWIZZLE_XXXX;
1872 temp2.swizzle = SWIZZLE_YYYY;
1873 emit_asm(ir, TGSI_OPCODE_OR, temp_dst, temp1, temp2);
1874 temp_dst.writemask = WRITEMASK_Y;
1875 temp1.swizzle = SWIZZLE_ZZZZ;
1876 temp2.swizzle = SWIZZLE_WWWW;
1877 emit_asm(ir, TGSI_OPCODE_OR, temp_dst, temp1, temp2);
1878 }
1879
1880 temp1.swizzle = SWIZZLE_XXXX;
1881 temp2.swizzle = SWIZZLE_YYYY;
1882 emit_asm(ir, TGSI_OPCODE_OR, result_dst, temp1, temp2);
1883 } else {
1884 /* After the dot-product, the value will be an integer on the
1885 * range [0,4]. Zero stays zero, and positive values become 1.0.
1886 */
1887 glsl_to_tgsi_instruction *const dp =
1888 emit_dp(ir, result_dst, temp, temp, vector_elements);
1889 if (this->prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
1890 /* The clamping to [0,1] can be done for free in the fragment
1891 * shader with a saturate.
1892 */
1893 dp->saturate = true;
1894 } else {
1895 /* Negating the result of the dot-product gives values on the range
1896 * [-4, 0]. Zero stays zero, and negative values become 1.0. This
1897 * achieved using SLT.
1898 */
1899 st_src_reg slt_src = result_src;
1900 slt_src.negate = ~slt_src.negate;
1901 emit_asm(ir, TGSI_OPCODE_SLT, result_dst, slt_src, st_src_reg_for_float(0.0));
1902 }
1903 }
1904 } else {
1905 emit_asm(ir, TGSI_OPCODE_SNE, result_dst, op[0], op[1]);
1906 }
1907 break;
1908
1909 case ir_binop_logic_xor:
1910 if (native_integers)
1911 emit_asm(ir, TGSI_OPCODE_XOR, result_dst, op[0], op[1]);
1912 else
1913 emit_asm(ir, TGSI_OPCODE_SNE, result_dst, op[0], op[1]);
1914 break;
1915
1916 case ir_binop_logic_or: {
1917 if (native_integers) {
1918 /* If integers are used as booleans, we can use an actual "or"
1919 * instruction.
1920 */
1921 assert(native_integers);
1922 emit_asm(ir, TGSI_OPCODE_OR, result_dst, op[0], op[1]);
1923 } else {
1924 /* After the addition, the value will be an integer on the
1925 * range [0,2]. Zero stays zero, and positive values become 1.0.
1926 */
1927 glsl_to_tgsi_instruction *add =
1928 emit_asm(ir, TGSI_OPCODE_ADD, result_dst, op[0], op[1]);
1929 if (this->prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
1930 /* The clamping to [0,1] can be done for free in the fragment
1931 * shader with a saturate if floats are being used as boolean values.
1932 */
1933 add->saturate = true;
1934 } else {
1935 /* Negating the result of the addition gives values on the range
1936 * [-2, 0]. Zero stays zero, and negative values become 1.0. This
1937 * is achieved using SLT.
1938 */
1939 st_src_reg slt_src = result_src;
1940 slt_src.negate = ~slt_src.negate;
1941 emit_asm(ir, TGSI_OPCODE_SLT, result_dst, slt_src, st_src_reg_for_float(0.0));
1942 }
1943 }
1944 break;
1945 }
1946
1947 case ir_binop_logic_and:
1948 /* If native integers are disabled, the bool args are stored as float 0.0
1949 * or 1.0, so "mul" gives us "and". If they're enabled, just use the
1950 * actual AND opcode.
1951 */
1952 if (native_integers)
1953 emit_asm(ir, TGSI_OPCODE_AND, result_dst, op[0], op[1]);
1954 else
1955 emit_asm(ir, TGSI_OPCODE_MUL, result_dst, op[0], op[1]);
1956 break;
1957
1958 case ir_binop_dot:
1959 assert(ir->operands[0]->type->is_vector());
1960 assert(ir->operands[0]->type == ir->operands[1]->type);
1961 emit_dp(ir, result_dst, op[0], op[1],
1962 ir->operands[0]->type->vector_elements);
1963 break;
1964
1965 case ir_unop_sqrt:
1966 if (have_sqrt) {
1967 emit_scalar(ir, TGSI_OPCODE_SQRT, result_dst, op[0]);
1968 } else {
1969 /* This is the only instruction sequence that makes the game "Risen"
1970 * render correctly. ABS is not required for the game, but since GLSL
1971 * declares negative values as "undefined", allowing us to do whatever
1972 * we want, I choose to use ABS to match DX9 and pre-GLSL RSQ
1973 * behavior.
1974 */
1975 emit_scalar(ir, TGSI_OPCODE_RSQ, result_dst, op[0].get_abs());
1976 emit_scalar(ir, TGSI_OPCODE_RCP, result_dst, result_src);
1977 }
1978 break;
1979 case ir_unop_rsq:
1980 emit_scalar(ir, TGSI_OPCODE_RSQ, result_dst, op[0]);
1981 break;
1982 case ir_unop_i2f:
1983 if (native_integers) {
1984 emit_asm(ir, TGSI_OPCODE_I2F, result_dst, op[0]);
1985 break;
1986 }
1987 /* fallthrough to next case otherwise */
1988 case ir_unop_b2f:
1989 if (native_integers) {
1990 emit_asm(ir, TGSI_OPCODE_AND, result_dst, op[0], st_src_reg_for_float(1.0));
1991 break;
1992 }
1993 /* fallthrough to next case otherwise */
1994 case ir_unop_i2u:
1995 case ir_unop_u2i:
1996 case ir_unop_i642u64:
1997 case ir_unop_u642i64:
1998 /* Converting between signed and unsigned integers is a no-op. */
1999 result_src = op[0];
2000 result_src.type = result_dst.type;
2001 break;
2002 case ir_unop_b2i:
2003 if (native_integers) {
2004 /* Booleans are stored as integers using ~0 for true and 0 for false.
2005 * GLSL requires that int(bool) return 1 for true and 0 for false.
2006 * This conversion is done with AND, but it could be done with NEG.
2007 */
2008 emit_asm(ir, TGSI_OPCODE_AND, result_dst, op[0], st_src_reg_for_int(1));
2009 } else {
2010 /* Booleans and integers are both stored as floats when native
2011 * integers are disabled.
2012 */
2013 result_src = op[0];
2014 }
2015 break;
2016 case ir_unop_f2i:
2017 if (native_integers)
2018 emit_asm(ir, TGSI_OPCODE_F2I, result_dst, op[0]);
2019 else
2020 emit_asm(ir, TGSI_OPCODE_TRUNC, result_dst, op[0]);
2021 break;
2022 case ir_unop_f2u:
2023 if (native_integers)
2024 emit_asm(ir, TGSI_OPCODE_F2U, result_dst, op[0]);
2025 else
2026 emit_asm(ir, TGSI_OPCODE_TRUNC, result_dst, op[0]);
2027 break;
2028 case ir_unop_bitcast_f2i:
2029 case ir_unop_bitcast_f2u:
2030 /* Make sure we don't propagate the negate modifier to integer opcodes. */
2031 if (op[0].negate || op[0].abs)
2032 emit_asm(ir, TGSI_OPCODE_MOV, result_dst, op[0]);
2033 else
2034 result_src = op[0];
2035 result_src.type = ir->operation == ir_unop_bitcast_f2i ? GLSL_TYPE_INT :
2036 GLSL_TYPE_UINT;
2037 break;
2038 case ir_unop_bitcast_i2f:
2039 case ir_unop_bitcast_u2f:
2040 result_src = op[0];
2041 result_src.type = GLSL_TYPE_FLOAT;
2042 break;
2043 case ir_unop_f2b:
2044 emit_asm(ir, TGSI_OPCODE_SNE, result_dst, op[0], st_src_reg_for_float(0.0));
2045 break;
2046 case ir_unop_d2b:
2047 emit_asm(ir, TGSI_OPCODE_SNE, result_dst, op[0], st_src_reg_for_double(0.0));
2048 break;
2049 case ir_unop_i2b:
2050 if (native_integers)
2051 emit_asm(ir, TGSI_OPCODE_USNE, result_dst, op[0], st_src_reg_for_int(0));
2052 else
2053 emit_asm(ir, TGSI_OPCODE_SNE, result_dst, op[0], st_src_reg_for_float(0.0));
2054 break;
2055 case ir_unop_bitcast_u642d:
2056 case ir_unop_bitcast_i642d:
2057 result_src = op[0];
2058 result_src.type = GLSL_TYPE_DOUBLE;
2059 break;
2060 case ir_unop_bitcast_d2i64:
2061 result_src = op[0];
2062 result_src.type = GLSL_TYPE_INT64;
2063 break;
2064 case ir_unop_bitcast_d2u64:
2065 result_src = op[0];
2066 result_src.type = GLSL_TYPE_UINT64;
2067 break;
2068 case ir_unop_trunc:
2069 emit_asm(ir, TGSI_OPCODE_TRUNC, result_dst, op[0]);
2070 break;
2071 case ir_unop_ceil:
2072 emit_asm(ir, TGSI_OPCODE_CEIL, result_dst, op[0]);
2073 break;
2074 case ir_unop_floor:
2075 emit_asm(ir, TGSI_OPCODE_FLR, result_dst, op[0]);
2076 break;
2077 case ir_unop_round_even:
2078 emit_asm(ir, TGSI_OPCODE_ROUND, result_dst, op[0]);
2079 break;
2080 case ir_unop_fract:
2081 emit_asm(ir, TGSI_OPCODE_FRC, result_dst, op[0]);
2082 break;
2083
2084 case ir_binop_min:
2085 emit_asm(ir, TGSI_OPCODE_MIN, result_dst, op[0], op[1]);
2086 break;
2087 case ir_binop_max:
2088 emit_asm(ir, TGSI_OPCODE_MAX, result_dst, op[0], op[1]);
2089 break;
2090 case ir_binop_pow:
2091 emit_scalar(ir, TGSI_OPCODE_POW, result_dst, op[0], op[1]);
2092 break;
2093
2094 case ir_unop_bit_not:
2095 if (native_integers) {
2096 emit_asm(ir, TGSI_OPCODE_NOT, result_dst, op[0]);
2097 break;
2098 }
2099 case ir_unop_u2f:
2100 if (native_integers) {
2101 emit_asm(ir, TGSI_OPCODE_U2F, result_dst, op[0]);
2102 break;
2103 }
2104 case ir_binop_lshift:
2105 if (native_integers) {
2106 emit_asm(ir, TGSI_OPCODE_SHL, result_dst, op[0], op[1]);
2107 break;
2108 }
2109 case ir_binop_rshift:
2110 if (native_integers) {
2111 emit_asm(ir, TGSI_OPCODE_ISHR, result_dst, op[0], op[1]);
2112 break;
2113 }
2114 case ir_binop_bit_and:
2115 if (native_integers) {
2116 emit_asm(ir, TGSI_OPCODE_AND, result_dst, op[0], op[1]);
2117 break;
2118 }
2119 case ir_binop_bit_xor:
2120 if (native_integers) {
2121 emit_asm(ir, TGSI_OPCODE_XOR, result_dst, op[0], op[1]);
2122 break;
2123 }
2124 case ir_binop_bit_or:
2125 if (native_integers) {
2126 emit_asm(ir, TGSI_OPCODE_OR, result_dst, op[0], op[1]);
2127 break;
2128 }
2129
2130 assert(!"GLSL 1.30 features unsupported");
2131 break;
2132
2133 case ir_binop_ubo_load: {
2134 ir_constant *const_uniform_block = ir->operands[0]->as_constant();
2135 ir_constant *const_offset_ir = ir->operands[1]->as_constant();
2136 unsigned const_offset = const_offset_ir ? const_offset_ir->value.u[0] : 0;
2137 unsigned const_block = const_uniform_block ? const_uniform_block->value.u[0] + 1 : 0;
2138 st_src_reg index_reg = get_temp(glsl_type::uint_type);
2139 st_src_reg cbuf;
2140
2141 cbuf.type = ir->type->base_type;
2142 cbuf.file = PROGRAM_CONSTANT;
2143 cbuf.index = 0;
2144 cbuf.reladdr = NULL;
2145 cbuf.negate = 0;
2146 cbuf.abs = 0;
2147
2148 assert(ir->type->is_vector() || ir->type->is_scalar());
2149
2150 if (const_offset_ir) {
2151 /* Constant index into constant buffer */
2152 cbuf.reladdr = NULL;
2153 cbuf.index = const_offset / 16;
2154 }
2155 else {
2156 ir_expression *offset_expr = ir->operands[1]->as_expression();
2157 st_src_reg offset = op[1];
2158
2159 /* The OpenGL spec is written in such a way that accesses with
2160 * non-constant offset are almost always vec4-aligned. The only
2161 * exception to this are members of structs in arrays of structs:
2162 * each struct in an array of structs is at least vec4-aligned,
2163 * but single-element and [ui]vec2 members of the struct may be at
2164 * an offset that is not a multiple of 16 bytes.
2165 *
2166 * Here, we extract that offset, relying on previous passes to always
2167 * generate offset expressions of the form (+ expr constant_offset).
2168 *
2169 * Note that the std430 layout, which allows more cases of alignment
2170 * less than vec4 in arrays, is not supported for uniform blocks, so
2171 * we do not have to deal with it here.
2172 */
2173 if (offset_expr && offset_expr->operation == ir_binop_add) {
2174 const_offset_ir = offset_expr->operands[1]->as_constant();
2175 if (const_offset_ir) {
2176 const_offset = const_offset_ir->value.u[0];
2177 cbuf.index = const_offset / 16;
2178 offset_expr->operands[0]->accept(this);
2179 offset = this->result;
2180 }
2181 }
2182
2183 /* Relative/variable index into constant buffer */
2184 emit_asm(ir, TGSI_OPCODE_USHR, st_dst_reg(index_reg), offset,
2185 st_src_reg_for_int(4));
2186 cbuf.reladdr = ralloc(mem_ctx, st_src_reg);
2187 memcpy(cbuf.reladdr, &index_reg, sizeof(index_reg));
2188 }
2189
2190 if (const_uniform_block) {
2191 /* Constant constant buffer */
2192 cbuf.reladdr2 = NULL;
2193 cbuf.index2D = const_block;
2194 cbuf.has_index2 = true;
2195 }
2196 else {
2197 /* Relative/variable constant buffer */
2198 cbuf.reladdr2 = ralloc(mem_ctx, st_src_reg);
2199 cbuf.index2D = 1;
2200 memcpy(cbuf.reladdr2, &op[0], sizeof(st_src_reg));
2201 cbuf.has_index2 = true;
2202 }
2203
2204 cbuf.swizzle = swizzle_for_size(ir->type->vector_elements);
2205 if (glsl_base_type_is_64bit(cbuf.type))
2206 cbuf.swizzle += MAKE_SWIZZLE4(const_offset % 16 / 8,
2207 const_offset % 16 / 8,
2208 const_offset % 16 / 8,
2209 const_offset % 16 / 8);
2210 else
2211 cbuf.swizzle += MAKE_SWIZZLE4(const_offset % 16 / 4,
2212 const_offset % 16 / 4,
2213 const_offset % 16 / 4,
2214 const_offset % 16 / 4);
2215
2216 if (ir->type->base_type == GLSL_TYPE_BOOL) {
2217 emit_asm(ir, TGSI_OPCODE_USNE, result_dst, cbuf, st_src_reg_for_int(0));
2218 } else {
2219 emit_asm(ir, TGSI_OPCODE_MOV, result_dst, cbuf);
2220 }
2221 break;
2222 }
2223 case ir_triop_lrp:
2224 /* note: we have to reorder the three args here */
2225 emit_asm(ir, TGSI_OPCODE_LRP, result_dst, op[2], op[1], op[0]);
2226 break;
2227 case ir_triop_csel:
2228 if (this->ctx->Const.NativeIntegers)
2229 emit_asm(ir, TGSI_OPCODE_UCMP, result_dst, op[0], op[1], op[2]);
2230 else {
2231 op[0].negate = ~op[0].negate;
2232 emit_asm(ir, TGSI_OPCODE_CMP, result_dst, op[0], op[1], op[2]);
2233 }
2234 break;
2235 case ir_triop_bitfield_extract:
2236 emit_asm(ir, TGSI_OPCODE_IBFE, result_dst, op[0], op[1], op[2]);
2237 break;
2238 case ir_quadop_bitfield_insert:
2239 emit_asm(ir, TGSI_OPCODE_BFI, result_dst, op[0], op[1], op[2], op[3]);
2240 break;
2241 case ir_unop_bitfield_reverse:
2242 emit_asm(ir, TGSI_OPCODE_BREV, result_dst, op[0]);
2243 break;
2244 case ir_unop_bit_count:
2245 emit_asm(ir, TGSI_OPCODE_POPC, result_dst, op[0]);
2246 break;
2247 case ir_unop_find_msb:
2248 emit_asm(ir, TGSI_OPCODE_IMSB, result_dst, op[0]);
2249 break;
2250 case ir_unop_find_lsb:
2251 emit_asm(ir, TGSI_OPCODE_LSB, result_dst, op[0]);
2252 break;
2253 case ir_binop_imul_high:
2254 emit_asm(ir, TGSI_OPCODE_IMUL_HI, result_dst, op[0], op[1]);
2255 break;
2256 case ir_triop_fma:
2257 /* In theory, MAD is incorrect here. */
2258 if (have_fma)
2259 emit_asm(ir, TGSI_OPCODE_FMA, result_dst, op[0], op[1], op[2]);
2260 else
2261 emit_asm(ir, TGSI_OPCODE_MAD, result_dst, op[0], op[1], op[2]);
2262 break;
2263 case ir_unop_interpolate_at_centroid:
2264 emit_asm(ir, TGSI_OPCODE_INTERP_CENTROID, result_dst, op[0]);
2265 break;
2266 case ir_binop_interpolate_at_offset: {
2267 /* The y coordinate needs to be flipped for the default fb */
2268 static const gl_state_index transform_y_state[STATE_LENGTH]
2269 = { STATE_INTERNAL, STATE_FB_WPOS_Y_TRANSFORM };
2270
2271 unsigned transform_y_index =
2272 _mesa_add_state_reference(this->prog->Parameters,
2273 transform_y_state);
2274
2275 st_src_reg transform_y = st_src_reg(PROGRAM_STATE_VAR,
2276 transform_y_index,
2277 glsl_type::vec4_type);
2278 transform_y.swizzle = SWIZZLE_XXXX;
2279
2280 st_src_reg temp = get_temp(glsl_type::vec2_type);
2281 st_dst_reg temp_dst = st_dst_reg(temp);
2282
2283 emit_asm(ir, TGSI_OPCODE_MOV, temp_dst, op[1]);
2284 temp_dst.writemask = WRITEMASK_Y;
2285 emit_asm(ir, TGSI_OPCODE_MUL, temp_dst, transform_y, op[1]);
2286 emit_asm(ir, TGSI_OPCODE_INTERP_OFFSET, result_dst, op[0], temp);
2287 break;
2288 }
2289 case ir_binop_interpolate_at_sample:
2290 emit_asm(ir, TGSI_OPCODE_INTERP_SAMPLE, result_dst, op[0], op[1]);
2291 break;
2292
2293 case ir_unop_d2f:
2294 emit_asm(ir, TGSI_OPCODE_D2F, result_dst, op[0]);
2295 break;
2296 case ir_unop_f2d:
2297 emit_asm(ir, TGSI_OPCODE_F2D, result_dst, op[0]);
2298 break;
2299 case ir_unop_d2i:
2300 emit_asm(ir, TGSI_OPCODE_D2I, result_dst, op[0]);
2301 break;
2302 case ir_unop_i2d:
2303 emit_asm(ir, TGSI_OPCODE_I2D, result_dst, op[0]);
2304 break;
2305 case ir_unop_d2u:
2306 emit_asm(ir, TGSI_OPCODE_D2U, result_dst, op[0]);
2307 break;
2308 case ir_unop_u2d:
2309 emit_asm(ir, TGSI_OPCODE_U2D, result_dst, op[0]);
2310 break;
2311 case ir_unop_unpack_double_2x32:
2312 case ir_unop_pack_double_2x32:
2313 case ir_unop_unpack_int_2x32:
2314 case ir_unop_pack_int_2x32:
2315 case ir_unop_unpack_uint_2x32:
2316 case ir_unop_pack_uint_2x32:
2317 emit_asm(ir, TGSI_OPCODE_MOV, result_dst, op[0]);
2318 break;
2319
2320 case ir_binop_ldexp:
2321 if (ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE) {
2322 emit_asm(ir, TGSI_OPCODE_DLDEXP, result_dst, op[0], op[1]);
2323 } else {
2324 assert(!"Invalid ldexp for non-double opcode in glsl_to_tgsi_visitor::visit()");
2325 }
2326 break;
2327
2328 case ir_unop_pack_half_2x16:
2329 emit_asm(ir, TGSI_OPCODE_PK2H, result_dst, op[0]);
2330 break;
2331 case ir_unop_unpack_half_2x16:
2332 emit_asm(ir, TGSI_OPCODE_UP2H, result_dst, op[0]);
2333 break;
2334
2335 case ir_unop_get_buffer_size: {
2336 ir_constant *const_offset = ir->operands[0]->as_constant();
2337 st_src_reg buffer(
2338 PROGRAM_BUFFER,
2339 ctx->Const.Program[shader->Stage].MaxAtomicBuffers +
2340 (const_offset ? const_offset->value.u[0] : 0),
2341 GLSL_TYPE_UINT);
2342 if (!const_offset) {
2343 buffer.reladdr = ralloc(mem_ctx, st_src_reg);
2344 *buffer.reladdr = op[0];
2345 emit_arl(ir, sampler_reladdr, op[0]);
2346 }
2347 emit_asm(ir, TGSI_OPCODE_RESQ, result_dst)->resource = buffer;
2348 break;
2349 }
2350
2351 case ir_unop_vote_any:
2352 emit_asm(ir, TGSI_OPCODE_VOTE_ANY, result_dst, op[0]);
2353 break;
2354 case ir_unop_vote_all:
2355 emit_asm(ir, TGSI_OPCODE_VOTE_ALL, result_dst, op[0]);
2356 break;
2357 case ir_unop_vote_eq:
2358 emit_asm(ir, TGSI_OPCODE_VOTE_EQ, result_dst, op[0]);
2359 break;
2360 case ir_unop_u2i64:
2361 case ir_unop_u2u64:
2362 case ir_unop_b2i64: {
2363 st_src_reg temp = get_temp(glsl_type::uvec4_type);
2364 st_dst_reg temp_dst = st_dst_reg(temp);
2365 unsigned orig_swz = op[0].swizzle;
2366 /*
2367 * To convert unsigned to 64-bit:
2368 * zero Y channel, copy X channel.
2369 */
2370 temp_dst.writemask = WRITEMASK_Y;
2371 if (vector_elements > 1)
2372 temp_dst.writemask |= WRITEMASK_W;
2373 emit_asm(ir, TGSI_OPCODE_MOV, temp_dst, st_src_reg_for_int(0));
2374 temp_dst.writemask = WRITEMASK_X;
2375 if (vector_elements > 1)
2376 temp_dst.writemask |= WRITEMASK_Z;
2377 op[0].swizzle = MAKE_SWIZZLE4(GET_SWZ(orig_swz, 0), GET_SWZ(orig_swz, 0),
2378 GET_SWZ(orig_swz, 1), GET_SWZ(orig_swz, 1));
2379 if (ir->operation == ir_unop_u2i64 || ir->operation == ir_unop_u2u64)
2380 emit_asm(ir, TGSI_OPCODE_MOV, temp_dst, op[0]);
2381 else
2382 emit_asm(ir, TGSI_OPCODE_AND, temp_dst, op[0], st_src_reg_for_int(1));
2383 result_src = temp;
2384 result_src.type = GLSL_TYPE_UINT64;
2385 if (vector_elements > 2) {
2386 /* Subtle: We rely on the fact that get_temp here returns the next
2387 * TGSI temporary register directly after the temp register used for
2388 * the first two components, so that the result gets picked up
2389 * automatically.
2390 */
2391 st_src_reg temp = get_temp(glsl_type::uvec4_type);
2392 st_dst_reg temp_dst = st_dst_reg(temp);
2393 temp_dst.writemask = WRITEMASK_Y;
2394 if (vector_elements > 3)
2395 temp_dst.writemask |= WRITEMASK_W;
2396 emit_asm(ir, TGSI_OPCODE_MOV, temp_dst, st_src_reg_for_int(0));
2397
2398 temp_dst.writemask = WRITEMASK_X;
2399 if (vector_elements > 3)
2400 temp_dst.writemask |= WRITEMASK_Z;
2401 op[0].swizzle = MAKE_SWIZZLE4(GET_SWZ(orig_swz, 2), GET_SWZ(orig_swz, 2),
2402 GET_SWZ(orig_swz, 3), GET_SWZ(orig_swz, 3));
2403 if (ir->operation == ir_unop_u2i64 || ir->operation == ir_unop_u2u64)
2404 emit_asm(ir, TGSI_OPCODE_MOV, temp_dst, op[0]);
2405 else
2406 emit_asm(ir, TGSI_OPCODE_AND, temp_dst, op[0], st_src_reg_for_int(1));
2407 }
2408 break;
2409 }
2410 case ir_unop_i642i:
2411 case ir_unop_u642i:
2412 case ir_unop_u642u:
2413 case ir_unop_i642u: {
2414 st_src_reg temp = get_temp(glsl_type::uvec4_type);
2415 st_dst_reg temp_dst = st_dst_reg(temp);
2416 unsigned orig_swz = op[0].swizzle;
2417 unsigned orig_idx = op[0].index;
2418 int el;
2419 temp_dst.writemask = WRITEMASK_X;
2420
2421 for (el = 0; el < vector_elements; el++) {
2422 unsigned swz = GET_SWZ(orig_swz, el);
2423 if (swz & 1)
2424 op[0].swizzle = MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_Z, SWIZZLE_Z, SWIZZLE_Z);
2425 else
2426 op[0].swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X);
2427 if (swz > 2)
2428 op[0].index = orig_idx + 1;
2429 op[0].type = GLSL_TYPE_UINT;
2430 temp_dst.writemask = WRITEMASK_X << el;
2431 emit_asm(ir, TGSI_OPCODE_MOV, temp_dst, op[0]);
2432 }
2433 result_src = temp;
2434 if (ir->operation == ir_unop_u642u || ir->operation == ir_unop_i642u)
2435 result_src.type = GLSL_TYPE_UINT;
2436 else
2437 result_src.type = GLSL_TYPE_INT;
2438 break;
2439 }
2440 case ir_unop_i642b:
2441 emit_asm(ir, TGSI_OPCODE_U64SNE, result_dst, op[0], st_src_reg_for_int(0));
2442 break;
2443 case ir_unop_i642f:
2444 emit_asm(ir, TGSI_OPCODE_I642F, result_dst, op[0]);
2445 break;
2446 case ir_unop_u642f:
2447 emit_asm(ir, TGSI_OPCODE_U642F, result_dst, op[0]);
2448 break;
2449 case ir_unop_i642d:
2450 emit_asm(ir, TGSI_OPCODE_I642D, result_dst, op[0]);
2451 break;
2452 case ir_unop_u642d:
2453 emit_asm(ir, TGSI_OPCODE_U642D, result_dst, op[0]);
2454 break;
2455 case ir_unop_i2i64:
2456 emit_asm(ir, TGSI_OPCODE_I2I64, result_dst, op[0]);
2457 break;
2458 case ir_unop_f2i64:
2459 emit_asm(ir, TGSI_OPCODE_F2I64, result_dst, op[0]);
2460 break;
2461 case ir_unop_d2i64:
2462 emit_asm(ir, TGSI_OPCODE_D2I64, result_dst, op[0]);
2463 break;
2464 case ir_unop_i2u64:
2465 emit_asm(ir, TGSI_OPCODE_I2I64, result_dst, op[0]);
2466 break;
2467 case ir_unop_f2u64:
2468 emit_asm(ir, TGSI_OPCODE_F2U64, result_dst, op[0]);
2469 break;
2470 case ir_unop_d2u64:
2471 emit_asm(ir, TGSI_OPCODE_D2U64, result_dst, op[0]);
2472 break;
2473 /* these might be needed */
2474 case ir_unop_pack_snorm_2x16:
2475 case ir_unop_pack_unorm_2x16:
2476 case ir_unop_pack_snorm_4x8:
2477 case ir_unop_pack_unorm_4x8:
2478
2479 case ir_unop_unpack_snorm_2x16:
2480 case ir_unop_unpack_unorm_2x16:
2481 case ir_unop_unpack_snorm_4x8:
2482 case ir_unop_unpack_unorm_4x8:
2483
2484 case ir_quadop_vector:
2485 case ir_binop_vector_extract:
2486 case ir_triop_vector_insert:
2487 case ir_binop_carry:
2488 case ir_binop_borrow:
2489 case ir_unop_ssbo_unsized_array_length:
2490 /* This operation is not supported, or should have already been handled.
2491 */
2492 assert(!"Invalid ir opcode in glsl_to_tgsi_visitor::visit()");
2493 break;
2494 }
2495
2496 this->result = result_src;
2497 }
2498
2499
2500 void
2501 glsl_to_tgsi_visitor::visit(ir_swizzle *ir)
2502 {
2503 st_src_reg src;
2504 int i;
2505 int swizzle[4];
2506
2507 /* Note that this is only swizzles in expressions, not those on the left
2508 * hand side of an assignment, which do write masking. See ir_assignment
2509 * for that.
2510 */
2511
2512 ir->val->accept(this);
2513 src = this->result;
2514 assert(src.file != PROGRAM_UNDEFINED);
2515 assert(ir->type->vector_elements > 0);
2516
2517 for (i = 0; i < 4; i++) {
2518 if (i < ir->type->vector_elements) {
2519 switch (i) {
2520 case 0:
2521 swizzle[i] = GET_SWZ(src.swizzle, ir->mask.x);
2522 break;
2523 case 1:
2524 swizzle[i] = GET_SWZ(src.swizzle, ir->mask.y);
2525 break;
2526 case 2:
2527 swizzle[i] = GET_SWZ(src.swizzle, ir->mask.z);
2528 break;
2529 case 3:
2530 swizzle[i] = GET_SWZ(src.swizzle, ir->mask.w);
2531 break;
2532 }
2533 } else {
2534 /* If the type is smaller than a vec4, replicate the last
2535 * channel out.
2536 */
2537 swizzle[i] = swizzle[ir->type->vector_elements - 1];
2538 }
2539 }
2540
2541 src.swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
2542
2543 this->result = src;
2544 }
2545
2546 /* Test if the variable is an array. Note that geometry and
2547 * tessellation shader inputs are outputs are always arrays (except
2548 * for patch inputs), so only the array element type is considered.
2549 */
2550 static bool
2551 is_inout_array(unsigned stage, ir_variable *var, bool *remove_array)
2552 {
2553 const glsl_type *type = var->type;
2554
2555 *remove_array = false;
2556
2557 if ((stage == MESA_SHADER_VERTEX && var->data.mode == ir_var_shader_in) ||
2558 (stage == MESA_SHADER_FRAGMENT && var->data.mode == ir_var_shader_out))
2559 return false;
2560
2561 if (((stage == MESA_SHADER_GEOMETRY && var->data.mode == ir_var_shader_in) ||
2562 (stage == MESA_SHADER_TESS_EVAL && var->data.mode == ir_var_shader_in) ||
2563 stage == MESA_SHADER_TESS_CTRL) &&
2564 !var->data.patch) {
2565 if (!var->type->is_array())
2566 return false; /* a system value probably */
2567
2568 type = var->type->fields.array;
2569 *remove_array = true;
2570 }
2571
2572 return type->is_array() || type->is_matrix();
2573 }
2574
2575 static unsigned
2576 st_translate_interp_loc(ir_variable *var)
2577 {
2578 if (var->data.centroid)
2579 return TGSI_INTERPOLATE_LOC_CENTROID;
2580 else if (var->data.sample)
2581 return TGSI_INTERPOLATE_LOC_SAMPLE;
2582 else
2583 return TGSI_INTERPOLATE_LOC_CENTER;
2584 }
2585
2586 void
2587 glsl_to_tgsi_visitor::visit(ir_dereference_variable *ir)
2588 {
2589 variable_storage *entry = find_variable_storage(ir->var);
2590 ir_variable *var = ir->var;
2591 bool remove_array;
2592
2593 if (!entry) {
2594 switch (var->data.mode) {
2595 case ir_var_uniform:
2596 entry = new(mem_ctx) variable_storage(var, PROGRAM_UNIFORM,
2597 var->data.param_index);
2598 this->variables.push_tail(entry);
2599 break;
2600 case ir_var_shader_in: {
2601 /* The linker assigns locations for varyings and attributes,
2602 * including deprecated builtins (like gl_Color), user-assign
2603 * generic attributes (glBindVertexLocation), and
2604 * user-defined varyings.
2605 */
2606 assert(var->data.location != -1);
2607
2608 const glsl_type *type_without_array = var->type->without_array();
2609 struct inout_decl *decl = &inputs[num_inputs];
2610 unsigned component = var->data.location_frac;
2611 unsigned num_components;
2612 num_inputs++;
2613
2614 if (type_without_array->is_64bit())
2615 component = component / 2;
2616 if (type_without_array->vector_elements)
2617 num_components = type_without_array->vector_elements;
2618 else
2619 num_components = 4;
2620
2621 decl->mesa_index = var->data.location;
2622 decl->interp = (glsl_interp_mode) var->data.interpolation;
2623 decl->interp_loc = st_translate_interp_loc(var);
2624 decl->base_type = type_without_array->base_type;
2625 decl->usage_mask = u_bit_consecutive(component, num_components);
2626
2627 if (is_inout_array(shader->Stage, var, &remove_array)) {
2628 decl->array_id = num_input_arrays + 1;
2629 num_input_arrays++;
2630 } else {
2631 decl->array_id = 0;
2632 }
2633
2634 if (remove_array)
2635 decl->size = type_size(var->type->fields.array);
2636 else
2637 decl->size = type_size(var->type);
2638
2639 entry = new(mem_ctx) variable_storage(var,
2640 PROGRAM_INPUT,
2641 decl->mesa_index,
2642 decl->array_id);
2643 entry->component = component;
2644
2645 this->variables.push_tail(entry);
2646 break;
2647 }
2648 case ir_var_shader_out: {
2649 assert(var->data.location != -1);
2650
2651 const glsl_type *type_without_array = var->type->without_array();
2652 struct inout_decl *decl = &outputs[num_outputs];
2653 unsigned component = var->data.location_frac;
2654 unsigned num_components;
2655 num_outputs++;
2656
2657 if (type_without_array->is_64bit())
2658 component = component / 2;
2659 if (type_without_array->vector_elements)
2660 num_components = type_without_array->vector_elements;
2661 else
2662 num_components = 4;
2663
2664 decl->mesa_index = var->data.location + FRAG_RESULT_MAX * var->data.index;
2665 decl->base_type = type_without_array->base_type;
2666 decl->usage_mask = u_bit_consecutive(component, num_components);
2667 if (var->data.stream & (1u << 31)) {
2668 decl->gs_out_streams = var->data.stream & ~(1u << 31);
2669 } else {
2670 assert(var->data.stream < 4);
2671 decl->gs_out_streams = 0;
2672 for (unsigned i = 0; i < num_components; ++i)
2673 decl->gs_out_streams |= var->data.stream << (2 * (component + i));
2674 }
2675
2676 if (is_inout_array(shader->Stage, var, &remove_array)) {
2677 decl->array_id = num_output_arrays + 1;
2678 num_output_arrays++;
2679 } else {
2680 decl->array_id = 0;
2681 }
2682
2683 if (remove_array)
2684 decl->size = type_size(var->type->fields.array);
2685 else
2686 decl->size = type_size(var->type);
2687
2688 if (var->data.fb_fetch_output) {
2689 st_dst_reg dst = st_dst_reg(get_temp(var->type));
2690 st_src_reg src = st_src_reg(PROGRAM_OUTPUT, decl->mesa_index,
2691 var->type, component, decl->array_id);
2692 emit_asm(NULL, TGSI_OPCODE_FBFETCH, dst, src);
2693 entry = new(mem_ctx) variable_storage(var, dst.file, dst.index,
2694 dst.array_id);
2695 } else {
2696 entry = new(mem_ctx) variable_storage(var,
2697 PROGRAM_OUTPUT,
2698 decl->mesa_index,
2699 decl->array_id);
2700 }
2701 entry->component = component;
2702
2703 this->variables.push_tail(entry);
2704 break;
2705 }
2706 case ir_var_system_value:
2707 entry = new(mem_ctx) variable_storage(var,
2708 PROGRAM_SYSTEM_VALUE,
2709 var->data.location);
2710 break;
2711 case ir_var_auto:
2712 case ir_var_temporary:
2713 st_src_reg src = get_temp(var->type);
2714
2715 entry = new(mem_ctx) variable_storage(var, src.file, src.index,
2716 src.array_id);
2717 this->variables.push_tail(entry);
2718
2719 break;
2720 }
2721
2722 if (!entry) {
2723 printf("Failed to make storage for %s\n", var->name);
2724 exit(1);
2725 }
2726 }
2727
2728 this->result = st_src_reg(entry->file, entry->index, var->type,
2729 entry->component, entry->array_id);
2730 if (this->shader->Stage == MESA_SHADER_VERTEX && var->data.mode == ir_var_shader_in && var->type->is_double())
2731 this->result.is_double_vertex_input = true;
2732 if (!native_integers)
2733 this->result.type = GLSL_TYPE_FLOAT;
2734 }
2735
2736 static void
2737 shrink_array_declarations(struct inout_decl *decls, unsigned count,
2738 GLbitfield64* usage_mask,
2739 GLbitfield64 double_usage_mask,
2740 GLbitfield* patch_usage_mask)
2741 {
2742 unsigned i;
2743 int j;
2744
2745 /* Fix array declarations by removing unused array elements at both ends
2746 * of the arrays. For example, mat4[3] where only mat[1] is used.
2747 */
2748 for (i = 0; i < count; i++) {
2749 struct inout_decl *decl = &decls[i];
2750 if (!decl->array_id)
2751 continue;
2752
2753 /* Shrink the beginning. */
2754 for (j = 0; j < (int)decl->size; j++) {
2755 if (decl->mesa_index >= VARYING_SLOT_PATCH0) {
2756 if (*patch_usage_mask &
2757 BITFIELD64_BIT(decl->mesa_index - VARYING_SLOT_PATCH0 + j))
2758 break;
2759 }
2760 else {
2761 if (*usage_mask & BITFIELD64_BIT(decl->mesa_index+j))
2762 break;
2763 if (double_usage_mask & BITFIELD64_BIT(decl->mesa_index+j-1))
2764 break;
2765 }
2766
2767 decl->mesa_index++;
2768 decl->size--;
2769 j--;
2770 }
2771
2772 /* Shrink the end. */
2773 for (j = decl->size-1; j >= 0; j--) {
2774 if (decl->mesa_index >= VARYING_SLOT_PATCH0) {
2775 if (*patch_usage_mask &
2776 BITFIELD64_BIT(decl->mesa_index - VARYING_SLOT_PATCH0 + j))
2777 break;
2778 }
2779 else {
2780 if (*usage_mask & BITFIELD64_BIT(decl->mesa_index+j))
2781 break;
2782 if (double_usage_mask & BITFIELD64_BIT(decl->mesa_index+j-1))
2783 break;
2784 }
2785
2786 decl->size--;
2787 }
2788
2789 /* When not all entries of an array are accessed, we mark them as used
2790 * here anyway, to ensure that the input/output mapping logic doesn't get
2791 * confused.
2792 *
2793 * TODO This happens when an array isn't used via indirect access, which
2794 * some game ports do (at least eON-based). There is an optimization
2795 * opportunity here by replacing the array declaration with non-array
2796 * declarations of those slots that are actually used.
2797 */
2798 for (j = 1; j < (int)decl->size; ++j) {
2799 if (decl->mesa_index >= VARYING_SLOT_PATCH0)
2800 *patch_usage_mask |= BITFIELD64_BIT(decl->mesa_index - VARYING_SLOT_PATCH0 + j);
2801 else
2802 *usage_mask |= BITFIELD64_BIT(decl->mesa_index + j);
2803 }
2804 }
2805 }
2806
2807 void
2808 glsl_to_tgsi_visitor::visit(ir_dereference_array *ir)
2809 {
2810 ir_constant *index;
2811 st_src_reg src;
2812 int element_size = type_size(ir->type);
2813 bool is_2D = false;
2814
2815 index = ir->array_index->constant_expression_value();
2816
2817 ir->array->accept(this);
2818 src = this->result;
2819
2820 if (ir->array->ir_type != ir_type_dereference_array) {
2821 switch (this->prog->Target) {
2822 case GL_TESS_CONTROL_PROGRAM_NV:
2823 is_2D = (src.file == PROGRAM_INPUT || src.file == PROGRAM_OUTPUT) &&
2824 !ir->variable_referenced()->data.patch;
2825 break;
2826 case GL_TESS_EVALUATION_PROGRAM_NV:
2827 is_2D = src.file == PROGRAM_INPUT &&
2828 !ir->variable_referenced()->data.patch;
2829 break;
2830 case GL_GEOMETRY_PROGRAM_NV:
2831 is_2D = src.file == PROGRAM_INPUT;
2832 break;
2833 }
2834 }
2835
2836 if (is_2D)
2837 element_size = 1;
2838
2839 if (index) {
2840
2841 if (this->prog->Target == GL_VERTEX_PROGRAM_ARB &&
2842 src.file == PROGRAM_INPUT)
2843 element_size = attrib_type_size(ir->type, true);
2844 if (is_2D) {
2845 src.index2D = index->value.i[0];
2846 src.has_index2 = true;
2847 } else
2848 src.index += index->value.i[0] * element_size;
2849 } else {
2850 /* Variable index array dereference. It eats the "vec4" of the
2851 * base of the array and an index that offsets the TGSI register
2852 * index.
2853 */
2854 ir->array_index->accept(this);
2855
2856 st_src_reg index_reg;
2857
2858 if (element_size == 1) {
2859 index_reg = this->result;
2860 } else {
2861 index_reg = get_temp(native_integers ?
2862 glsl_type::int_type : glsl_type::float_type);
2863
2864 emit_asm(ir, TGSI_OPCODE_MUL, st_dst_reg(index_reg),
2865 this->result, st_src_reg_for_type(index_reg.type, element_size));
2866 }
2867
2868 /* If there was already a relative address register involved, add the
2869 * new and the old together to get the new offset.
2870 */
2871 if (!is_2D && src.reladdr != NULL) {
2872 st_src_reg accum_reg = get_temp(native_integers ?
2873 glsl_type::int_type : glsl_type::float_type);
2874
2875 emit_asm(ir, TGSI_OPCODE_ADD, st_dst_reg(accum_reg),
2876 index_reg, *src.reladdr);
2877
2878 index_reg = accum_reg;
2879 }
2880
2881 if (is_2D) {
2882 src.reladdr2 = ralloc(mem_ctx, st_src_reg);
2883 memcpy(src.reladdr2, &index_reg, sizeof(index_reg));
2884 src.index2D = 0;
2885 src.has_index2 = true;
2886 } else {
2887 src.reladdr = ralloc(mem_ctx, st_src_reg);
2888 memcpy(src.reladdr, &index_reg, sizeof(index_reg));
2889 }
2890 }
2891
2892 /* Change the register type to the element type of the array. */
2893 src.type = ir->type->base_type;
2894
2895 this->result = src;
2896 }
2897
2898 void
2899 glsl_to_tgsi_visitor::visit(ir_dereference_record *ir)
2900 {
2901 unsigned int i;
2902 const glsl_type *struct_type = ir->record->type;
2903 int offset = 0;
2904
2905 ir->record->accept(this);
2906
2907 for (i = 0; i < struct_type->length; i++) {
2908 if (strcmp(struct_type->fields.structure[i].name, ir->field) == 0)
2909 break;
2910 offset += type_size(struct_type->fields.structure[i].type);
2911 }
2912
2913 /* If the type is smaller than a vec4, replicate the last channel out. */
2914 if (ir->type->is_scalar() || ir->type->is_vector())
2915 this->result.swizzle = swizzle_for_size(ir->type->vector_elements);
2916 else
2917 this->result.swizzle = SWIZZLE_NOOP;
2918
2919 this->result.index += offset;
2920 this->result.type = ir->type->base_type;
2921 }
2922
2923 /**
2924 * We want to be careful in assignment setup to hit the actual storage
2925 * instead of potentially using a temporary like we might with the
2926 * ir_dereference handler.
2927 */
2928 static st_dst_reg
2929 get_assignment_lhs(ir_dereference *ir, glsl_to_tgsi_visitor *v, int *component)
2930 {
2931 /* The LHS must be a dereference. If the LHS is a variable indexed array
2932 * access of a vector, it must be separated into a series conditional moves
2933 * before reaching this point (see ir_vec_index_to_cond_assign).
2934 */
2935 assert(ir->as_dereference());
2936 ir_dereference_array *deref_array = ir->as_dereference_array();
2937 if (deref_array) {
2938 assert(!deref_array->array->type->is_vector());
2939 }
2940
2941 /* Use the rvalue deref handler for the most part. We write swizzles using
2942 * the writemask, but we do extract the base component for enhanced layouts
2943 * from the source swizzle.
2944 */
2945 ir->accept(v);
2946 *component = GET_SWZ(v->result.swizzle, 0);
2947 return st_dst_reg(v->result);
2948 }
2949
2950 /**
2951 * Process the condition of a conditional assignment
2952 *
2953 * Examines the condition of a conditional assignment to generate the optimal
2954 * first operand of a \c CMP instruction. If the condition is a relational
2955 * operator with 0 (e.g., \c ir_binop_less), the value being compared will be
2956 * used as the source for the \c CMP instruction. Otherwise the comparison
2957 * is processed to a boolean result, and the boolean result is used as the
2958 * operand to the CMP instruction.
2959 */
2960 bool
2961 glsl_to_tgsi_visitor::process_move_condition(ir_rvalue *ir)
2962 {
2963 ir_rvalue *src_ir = ir;
2964 bool negate = true;
2965 bool switch_order = false;
2966
2967 ir_expression *const expr = ir->as_expression();
2968
2969 if (native_integers) {
2970 if ((expr != NULL) && (expr->get_num_operands() == 2)) {
2971 enum glsl_base_type type = expr->operands[0]->type->base_type;
2972 if (type == GLSL_TYPE_INT || type == GLSL_TYPE_UINT ||
2973 type == GLSL_TYPE_BOOL) {
2974 if (expr->operation == ir_binop_equal) {
2975 if (expr->operands[0]->is_zero()) {
2976 src_ir = expr->operands[1];
2977 switch_order = true;
2978 }
2979 else if (expr->operands[1]->is_zero()) {
2980 src_ir = expr->operands[0];
2981 switch_order = true;
2982 }
2983 }
2984 else if (expr->operation == ir_binop_nequal) {
2985 if (expr->operands[0]->is_zero()) {
2986 src_ir = expr->operands[1];
2987 }
2988 else if (expr->operands[1]->is_zero()) {
2989 src_ir = expr->operands[0];
2990 }
2991 }
2992 }
2993 }
2994
2995 src_ir->accept(this);
2996 return switch_order;
2997 }
2998
2999 if ((expr != NULL) && (expr->get_num_operands() == 2)) {
3000 bool zero_on_left = false;
3001
3002 if (expr->operands[0]->is_zero()) {
3003 src_ir = expr->operands[1];
3004 zero_on_left = true;
3005 } else if (expr->operands[1]->is_zero()) {
3006 src_ir = expr->operands[0];
3007 zero_on_left = false;
3008 }
3009
3010 /* a is - 0 + - 0 +
3011 * (a < 0) T F F ( a < 0) T F F
3012 * (0 < a) F F T (-a < 0) F F T
3013 * (a <= 0) T T F (-a < 0) F F T (swap order of other operands)
3014 * (0 <= a) F T T ( a < 0) T F F (swap order of other operands)
3015 * (a > 0) F F T (-a < 0) F F T
3016 * (0 > a) T F F ( a < 0) T F F
3017 * (a >= 0) F T T ( a < 0) T F F (swap order of other operands)
3018 * (0 >= a) T T F (-a < 0) F F T (swap order of other operands)
3019 *
3020 * Note that exchanging the order of 0 and 'a' in the comparison simply
3021 * means that the value of 'a' should be negated.
3022 */
3023 if (src_ir != ir) {
3024 switch (expr->operation) {
3025 case ir_binop_less:
3026 switch_order = false;
3027 negate = zero_on_left;
3028 break;
3029
3030 case ir_binop_greater:
3031 switch_order = false;
3032 negate = !zero_on_left;
3033 break;
3034
3035 case ir_binop_lequal:
3036 switch_order = true;
3037 negate = !zero_on_left;
3038 break;
3039
3040 case ir_binop_gequal:
3041 switch_order = true;
3042 negate = zero_on_left;
3043 break;
3044
3045 default:
3046 /* This isn't the right kind of comparison afterall, so make sure
3047 * the whole condition is visited.
3048 */
3049 src_ir = ir;
3050 break;
3051 }
3052 }
3053 }
3054
3055 src_ir->accept(this);
3056
3057 /* We use the TGSI_OPCODE_CMP (a < 0 ? b : c) for conditional moves, and the
3058 * condition we produced is 0.0 or 1.0. By flipping the sign, we can
3059 * choose which value TGSI_OPCODE_CMP produces without an extra instruction
3060 * computing the condition.
3061 */
3062 if (negate)
3063 this->result.negate = ~this->result.negate;
3064
3065 return switch_order;
3066 }
3067
3068 void
3069 glsl_to_tgsi_visitor::emit_block_mov(ir_assignment *ir, const struct glsl_type *type,
3070 st_dst_reg *l, st_src_reg *r,
3071 st_src_reg *cond, bool cond_swap)
3072 {
3073 if (type->base_type == GLSL_TYPE_STRUCT) {
3074 for (unsigned int i = 0; i < type->length; i++) {
3075 emit_block_mov(ir, type->fields.structure[i].type, l, r,
3076 cond, cond_swap);
3077 }
3078 return;
3079 }
3080
3081 if (type->is_array()) {
3082 for (unsigned int i = 0; i < type->length; i++) {
3083 emit_block_mov(ir, type->fields.array, l, r, cond, cond_swap);
3084 }
3085 return;
3086 }
3087
3088 if (type->is_matrix()) {
3089 const struct glsl_type *vec_type;
3090
3091 vec_type = glsl_type::get_instance(type->is_double() ? GLSL_TYPE_DOUBLE : GLSL_TYPE_FLOAT,
3092 type->vector_elements, 1);
3093
3094 for (int i = 0; i < type->matrix_columns; i++) {
3095 emit_block_mov(ir, vec_type, l, r, cond, cond_swap);
3096 }
3097 return;
3098 }
3099
3100 assert(type->is_scalar() || type->is_vector());
3101
3102 l->type = type->base_type;
3103 r->type = type->base_type;
3104 if (cond) {
3105 st_src_reg l_src = st_src_reg(*l);
3106 l_src.swizzle = swizzle_for_size(type->vector_elements);
3107
3108 if (native_integers) {
3109 emit_asm(ir, TGSI_OPCODE_UCMP, *l, *cond,
3110 cond_swap ? l_src : *r,
3111 cond_swap ? *r : l_src);
3112 } else {
3113 emit_asm(ir, TGSI_OPCODE_CMP, *l, *cond,
3114 cond_swap ? l_src : *r,
3115 cond_swap ? *r : l_src);
3116 }
3117 } else {
3118 emit_asm(ir, TGSI_OPCODE_MOV, *l, *r);
3119 }
3120 l->index++;
3121 r->index++;
3122 if (type->is_dual_slot()) {
3123 l->index++;
3124 if (r->is_double_vertex_input == false)
3125 r->index++;
3126 }
3127 }
3128
3129 void
3130 glsl_to_tgsi_visitor::visit(ir_assignment *ir)
3131 {
3132 int dst_component;
3133 st_dst_reg l;
3134 st_src_reg r;
3135
3136 ir->rhs->accept(this);
3137 r = this->result;
3138
3139 l = get_assignment_lhs(ir->lhs, this, &dst_component);
3140
3141 {
3142 int swizzles[4];
3143 int first_enabled_chan = 0;
3144 int rhs_chan = 0;
3145 ir_variable *variable = ir->lhs->variable_referenced();
3146
3147 if (shader->Stage == MESA_SHADER_FRAGMENT &&
3148 variable->data.mode == ir_var_shader_out &&
3149 (variable->data.location == FRAG_RESULT_DEPTH ||
3150 variable->data.location == FRAG_RESULT_STENCIL)) {
3151 assert(ir->lhs->type->is_scalar());
3152 assert(ir->write_mask == WRITEMASK_X);
3153
3154 if (variable->data.location == FRAG_RESULT_DEPTH)
3155 l.writemask = WRITEMASK_Z;
3156 else {
3157 assert(variable->data.location == FRAG_RESULT_STENCIL);
3158 l.writemask = WRITEMASK_Y;
3159 }
3160 } else if (ir->write_mask == 0) {
3161 assert(!ir->lhs->type->is_scalar() && !ir->lhs->type->is_vector());
3162
3163 unsigned num_elements = ir->lhs->type->without_array()->vector_elements;
3164
3165 if (num_elements) {
3166 l.writemask = u_bit_consecutive(0, num_elements);
3167 } else {
3168 /* The type is a struct or an array of (array of) structs. */
3169 l.writemask = WRITEMASK_XYZW;
3170 }
3171 } else {
3172 l.writemask = ir->write_mask;
3173 }
3174
3175 for (int i = 0; i < 4; i++) {
3176 if (l.writemask & (1 << i)) {
3177 first_enabled_chan = GET_SWZ(r.swizzle, i);
3178 break;
3179 }
3180 }
3181
3182 l.writemask = l.writemask << dst_component;
3183
3184 /* Swizzle a small RHS vector into the channels being written.
3185 *
3186 * glsl ir treats write_mask as dictating how many channels are
3187 * present on the RHS while TGSI treats write_mask as just
3188 * showing which channels of the vec4 RHS get written.
3189 */
3190 for (int i = 0; i < 4; i++) {
3191 if (l.writemask & (1 << i))
3192 swizzles[i] = GET_SWZ(r.swizzle, rhs_chan++);
3193 else
3194 swizzles[i] = first_enabled_chan;
3195 }
3196 r.swizzle = MAKE_SWIZZLE4(swizzles[0], swizzles[1],
3197 swizzles[2], swizzles[3]);
3198 }
3199
3200 assert(l.file != PROGRAM_UNDEFINED);
3201 assert(r.file != PROGRAM_UNDEFINED);
3202
3203 if (ir->condition) {
3204 const bool switch_order = this->process_move_condition(ir->condition);
3205 st_src_reg condition = this->result;
3206
3207 emit_block_mov(ir, ir->lhs->type, &l, &r, &condition, switch_order);
3208 } else if (ir->rhs->as_expression() &&
3209 this->instructions.get_tail() &&
3210 ir->rhs == ((glsl_to_tgsi_instruction *)this->instructions.get_tail())->ir &&
3211 !((glsl_to_tgsi_instruction *)this->instructions.get_tail())->is_64bit_expanded &&
3212 type_size(ir->lhs->type) == 1 &&
3213 l.writemask == ((glsl_to_tgsi_instruction *)this->instructions.get_tail())->dst[0].writemask) {
3214 /* To avoid emitting an extra MOV when assigning an expression to a
3215 * variable, emit the last instruction of the expression again, but
3216 * replace the destination register with the target of the assignment.
3217 * Dead code elimination will remove the original instruction.
3218 */
3219 glsl_to_tgsi_instruction *inst, *new_inst;
3220 inst = (glsl_to_tgsi_instruction *)this->instructions.get_tail();
3221 new_inst = emit_asm(ir, inst->op, l, inst->src[0], inst->src[1], inst->src[2], inst->src[3]);
3222 new_inst->saturate = inst->saturate;
3223 inst->dead_mask = inst->dst[0].writemask;
3224 } else {
3225 emit_block_mov(ir, ir->rhs->type, &l, &r, NULL, false);
3226 }
3227 }
3228
3229
3230 void
3231 glsl_to_tgsi_visitor::visit(ir_constant *ir)
3232 {
3233 st_src_reg src;
3234 GLdouble stack_vals[4] = { 0 };
3235 gl_constant_value *values = (gl_constant_value *) stack_vals;
3236 GLenum gl_type = GL_NONE;
3237 unsigned int i;
3238 static int in_array = 0;
3239 gl_register_file file = in_array ? PROGRAM_CONSTANT : PROGRAM_IMMEDIATE;
3240
3241 /* Unfortunately, 4 floats is all we can get into
3242 * _mesa_add_typed_unnamed_constant. So, make a temp to store an
3243 * aggregate constant and move each constant value into it. If we
3244 * get lucky, copy propagation will eliminate the extra moves.
3245 */
3246 if (ir->type->base_type == GLSL_TYPE_STRUCT) {
3247 st_src_reg temp_base = get_temp(ir->type);
3248 st_dst_reg temp = st_dst_reg(temp_base);
3249
3250 foreach_in_list(ir_constant, field_value, &ir->components) {
3251 int size = type_size(field_value->type);
3252
3253 assert(size > 0);
3254
3255 field_value->accept(this);
3256 src = this->result;
3257
3258 for (i = 0; i < (unsigned int)size; i++) {
3259 emit_asm(ir, TGSI_OPCODE_MOV, temp, src);
3260
3261 src.index++;
3262 temp.index++;
3263 }
3264 }
3265 this->result = temp_base;
3266 return;
3267 }
3268
3269 if (ir->type->is_array()) {
3270 st_src_reg temp_base = get_temp(ir->type);
3271 st_dst_reg temp = st_dst_reg(temp_base);
3272 int size = type_size(ir->type->fields.array);
3273
3274 assert(size > 0);
3275 in_array++;
3276
3277 for (i = 0; i < ir->type->length; i++) {
3278 ir->array_elements[i]->accept(this);
3279 src = this->result;
3280 for (int j = 0; j < size; j++) {
3281 emit_asm(ir, TGSI_OPCODE_MOV, temp, src);
3282
3283 src.index++;
3284 temp.index++;
3285 }
3286 }
3287 this->result = temp_base;
3288 in_array--;
3289 return;
3290 }
3291
3292 if (ir->type->is_matrix()) {
3293 st_src_reg mat = get_temp(ir->type);
3294 st_dst_reg mat_column = st_dst_reg(mat);
3295
3296 for (i = 0; i < ir->type->matrix_columns; i++) {
3297 switch (ir->type->base_type) {
3298 case GLSL_TYPE_FLOAT:
3299 values = (gl_constant_value *) &ir->value.f[i * ir->type->vector_elements];
3300
3301 src = st_src_reg(file, -1, ir->type->base_type);
3302 src.index = add_constant(file,
3303 values,
3304 ir->type->vector_elements,
3305 GL_FLOAT,
3306 &src.swizzle);
3307 emit_asm(ir, TGSI_OPCODE_MOV, mat_column, src);
3308 break;
3309 case GLSL_TYPE_DOUBLE:
3310 values = (gl_constant_value *) &ir->value.d[i * ir->type->vector_elements];
3311 src = st_src_reg(file, -1, ir->type->base_type);
3312 src.index = add_constant(file,
3313 values,
3314 ir->type->vector_elements,
3315 GL_DOUBLE,
3316 &src.swizzle);
3317 if (ir->type->vector_elements >= 2) {
3318 mat_column.writemask = WRITEMASK_XY;
3319 src.swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_X, SWIZZLE_Y);
3320 emit_asm(ir, TGSI_OPCODE_MOV, mat_column, src);
3321 } else {
3322 mat_column.writemask = WRITEMASK_X;
3323 src.swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X);
3324 emit_asm(ir, TGSI_OPCODE_MOV, mat_column, src);
3325 }
3326 src.index++;
3327 if (ir->type->vector_elements > 2) {
3328 if (ir->type->vector_elements == 4) {
3329 mat_column.writemask = WRITEMASK_ZW;
3330 src.swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_X, SWIZZLE_Y);
3331 emit_asm(ir, TGSI_OPCODE_MOV, mat_column, src);
3332 } else {
3333 mat_column.writemask = WRITEMASK_Z;
3334 src.swizzle = MAKE_SWIZZLE4(SWIZZLE_Y, SWIZZLE_Y, SWIZZLE_Y, SWIZZLE_Y);
3335 emit_asm(ir, TGSI_OPCODE_MOV, mat_column, src);
3336 mat_column.writemask = WRITEMASK_XYZW;
3337 src.swizzle = SWIZZLE_XYZW;
3338 }
3339 mat_column.index++;
3340 }
3341 break;
3342 default:
3343 unreachable("Illegal matrix constant type.\n");
3344 break;
3345 }
3346 mat_column.index++;
3347 }
3348 this->result = mat;
3349 return;
3350 }
3351
3352 switch (ir->type->base_type) {
3353 case GLSL_TYPE_FLOAT:
3354 gl_type = GL_FLOAT;
3355 for (i = 0; i < ir->type->vector_elements; i++) {
3356 values[i].f = ir->value.f[i];
3357 }
3358 break;
3359 case GLSL_TYPE_DOUBLE:
3360 gl_type = GL_DOUBLE;
3361 for (i = 0; i < ir->type->vector_elements; i++) {
3362 memcpy(&values[i * 2], &ir->value.d[i], sizeof(double));
3363 }
3364 break;
3365 case GLSL_TYPE_INT64:
3366 gl_type = GL_INT64_ARB;
3367 for (i = 0; i < ir->type->vector_elements; i++) {
3368 memcpy(&values[i * 2], &ir->value.d[i], sizeof(int64_t));
3369 }
3370 break;
3371 case GLSL_TYPE_UINT64:
3372 gl_type = GL_UNSIGNED_INT64_ARB;
3373 for (i = 0; i < ir->type->vector_elements; i++) {
3374 memcpy(&values[i * 2], &ir->value.d[i], sizeof(uint64_t));
3375 }
3376 break;
3377 case GLSL_TYPE_UINT:
3378 gl_type = native_integers ? GL_UNSIGNED_INT : GL_FLOAT;
3379 for (i = 0; i < ir->type->vector_elements; i++) {
3380 if (native_integers)
3381 values[i].u = ir->value.u[i];
3382 else
3383 values[i].f = ir->value.u[i];
3384 }
3385 break;
3386 case GLSL_TYPE_INT:
3387 gl_type = native_integers ? GL_INT : GL_FLOAT;
3388 for (i = 0; i < ir->type->vector_elements; i++) {
3389 if (native_integers)
3390 values[i].i = ir->value.i[i];
3391 else
3392 values[i].f = ir->value.i[i];
3393 }
3394 break;
3395 case GLSL_TYPE_BOOL:
3396 gl_type = native_integers ? GL_BOOL : GL_FLOAT;
3397 for (i = 0; i < ir->type->vector_elements; i++) {
3398 values[i].u = ir->value.b[i] ? ctx->Const.UniformBooleanTrue : 0;
3399 }
3400 break;
3401 default:
3402 assert(!"Non-float/uint/int/bool constant");
3403 }
3404
3405 this->result = st_src_reg(file, -1, ir->type);
3406 this->result.index = add_constant(file,
3407 values,
3408 ir->type->vector_elements,
3409 gl_type,
3410 &this->result.swizzle);
3411 }
3412
3413 void
3414 glsl_to_tgsi_visitor::visit_atomic_counter_intrinsic(ir_call *ir)
3415 {
3416 exec_node *param = ir->actual_parameters.get_head();
3417 ir_dereference *deref = static_cast<ir_dereference *>(param);
3418 ir_variable *location = deref->variable_referenced();
3419
3420 st_src_reg buffer(
3421 PROGRAM_BUFFER, location->data.binding, GLSL_TYPE_ATOMIC_UINT);
3422
3423 /* Calculate the surface offset */
3424 st_src_reg offset;
3425 unsigned array_size = 0, base = 0;
3426 uint16_t index = 0;
3427
3428 get_deref_offsets(deref, &array_size, &base, &index, &offset, false);
3429
3430 if (offset.file != PROGRAM_UNDEFINED) {
3431 emit_asm(ir, TGSI_OPCODE_MUL, st_dst_reg(offset),
3432 offset, st_src_reg_for_int(ATOMIC_COUNTER_SIZE));
3433 emit_asm(ir, TGSI_OPCODE_ADD, st_dst_reg(offset),
3434 offset, st_src_reg_for_int(location->data.offset + index * ATOMIC_COUNTER_SIZE));
3435 } else {
3436 offset = st_src_reg_for_int(location->data.offset + index * ATOMIC_COUNTER_SIZE);
3437 }
3438
3439 ir->return_deref->accept(this);
3440 st_dst_reg dst(this->result);
3441 dst.writemask = WRITEMASK_X;
3442
3443 glsl_to_tgsi_instruction *inst;
3444
3445 if (ir->callee->intrinsic_id == ir_intrinsic_atomic_counter_read) {
3446 inst = emit_asm(ir, TGSI_OPCODE_LOAD, dst, offset);
3447 } else if (ir->callee->intrinsic_id == ir_intrinsic_atomic_counter_increment) {
3448 inst = emit_asm(ir, TGSI_OPCODE_ATOMUADD, dst, offset,
3449 st_src_reg_for_int(1));
3450 } else if (ir->callee->intrinsic_id == ir_intrinsic_atomic_counter_predecrement) {
3451 inst = emit_asm(ir, TGSI_OPCODE_ATOMUADD, dst, offset,
3452 st_src_reg_for_int(-1));
3453 emit_asm(ir, TGSI_OPCODE_ADD, dst, this->result, st_src_reg_for_int(-1));
3454 } else {
3455 param = param->get_next();
3456 ir_rvalue *val = ((ir_instruction *)param)->as_rvalue();
3457 val->accept(this);
3458
3459 st_src_reg data = this->result, data2 = undef_src;
3460 unsigned opcode;
3461 switch (ir->callee->intrinsic_id) {
3462 case ir_intrinsic_atomic_counter_add:
3463 opcode = TGSI_OPCODE_ATOMUADD;
3464 break;
3465 case ir_intrinsic_atomic_counter_min:
3466 opcode = TGSI_OPCODE_ATOMIMIN;
3467 break;
3468 case ir_intrinsic_atomic_counter_max:
3469 opcode = TGSI_OPCODE_ATOMIMAX;
3470 break;
3471 case ir_intrinsic_atomic_counter_and:
3472 opcode = TGSI_OPCODE_ATOMAND;
3473 break;
3474 case ir_intrinsic_atomic_counter_or:
3475 opcode = TGSI_OPCODE_ATOMOR;
3476 break;
3477 case ir_intrinsic_atomic_counter_xor:
3478 opcode = TGSI_OPCODE_ATOMXOR;
3479 break;
3480 case ir_intrinsic_atomic_counter_exchange:
3481 opcode = TGSI_OPCODE_ATOMXCHG;
3482 break;
3483 case ir_intrinsic_atomic_counter_comp_swap: {
3484 opcode = TGSI_OPCODE_ATOMCAS;
3485 param = param->get_next();
3486 val = ((ir_instruction *)param)->as_rvalue();
3487 val->accept(this);
3488 data2 = this->result;
3489 break;
3490 }
3491 default:
3492 assert(!"Unexpected intrinsic");
3493 return;
3494 }
3495
3496 inst = emit_asm(ir, opcode, dst, offset, data, data2);
3497 }
3498
3499 inst->resource = buffer;
3500 }
3501
3502 void
3503 glsl_to_tgsi_visitor::visit_ssbo_intrinsic(ir_call *ir)
3504 {
3505 exec_node *param = ir->actual_parameters.get_head();
3506
3507 ir_rvalue *block = ((ir_instruction *)param)->as_rvalue();
3508
3509 param = param->get_next();
3510 ir_rvalue *offset = ((ir_instruction *)param)->as_rvalue();
3511
3512 ir_constant *const_block = block->as_constant();
3513
3514 st_src_reg buffer(
3515 PROGRAM_BUFFER,
3516 ctx->Const.Program[shader->Stage].MaxAtomicBuffers +
3517 (const_block ? const_block->value.u[0] : 0),
3518 GLSL_TYPE_UINT);
3519
3520 if (!const_block) {
3521 block->accept(this);
3522 buffer.reladdr = ralloc(mem_ctx, st_src_reg);
3523 *buffer.reladdr = this->result;
3524 emit_arl(ir, sampler_reladdr, this->result);
3525 }
3526
3527 /* Calculate the surface offset */
3528 offset->accept(this);
3529 st_src_reg off = this->result;
3530
3531 st_dst_reg dst = undef_dst;
3532 if (ir->return_deref) {
3533 ir->return_deref->accept(this);
3534 dst = st_dst_reg(this->result);
3535 dst.writemask = (1 << ir->return_deref->type->vector_elements) - 1;
3536 }
3537
3538 glsl_to_tgsi_instruction *inst;
3539
3540 if (ir->callee->intrinsic_id == ir_intrinsic_ssbo_load) {
3541 inst = emit_asm(ir, TGSI_OPCODE_LOAD, dst, off);
3542 if (dst.type == GLSL_TYPE_BOOL)
3543 emit_asm(ir, TGSI_OPCODE_USNE, dst, st_src_reg(dst), st_src_reg_for_int(0));
3544 } else if (ir->callee->intrinsic_id == ir_intrinsic_ssbo_store) {
3545 param = param->get_next();
3546 ir_rvalue *val = ((ir_instruction *)param)->as_rvalue();
3547 val->accept(this);
3548
3549 param = param->get_next();
3550 ir_constant *write_mask = ((ir_instruction *)param)->as_constant();
3551 assert(write_mask);
3552 dst.writemask = write_mask->value.u[0];
3553
3554 dst.type = this->result.type;
3555 inst = emit_asm(ir, TGSI_OPCODE_STORE, dst, off, this->result);
3556 } else {
3557 param = param->get_next();
3558 ir_rvalue *val = ((ir_instruction *)param)->as_rvalue();
3559 val->accept(this);
3560
3561 st_src_reg data = this->result, data2 = undef_src;
3562 unsigned opcode;
3563 switch (ir->callee->intrinsic_id) {
3564 case ir_intrinsic_ssbo_atomic_add:
3565 opcode = TGSI_OPCODE_ATOMUADD;
3566 break;
3567 case ir_intrinsic_ssbo_atomic_min:
3568 opcode = TGSI_OPCODE_ATOMIMIN;
3569 break;
3570 case ir_intrinsic_ssbo_atomic_max:
3571 opcode = TGSI_OPCODE_ATOMIMAX;
3572 break;
3573 case ir_intrinsic_ssbo_atomic_and:
3574 opcode = TGSI_OPCODE_ATOMAND;
3575 break;
3576 case ir_intrinsic_ssbo_atomic_or:
3577 opcode = TGSI_OPCODE_ATOMOR;
3578 break;
3579 case ir_intrinsic_ssbo_atomic_xor:
3580 opcode = TGSI_OPCODE_ATOMXOR;
3581 break;
3582 case ir_intrinsic_ssbo_atomic_exchange:
3583 opcode = TGSI_OPCODE_ATOMXCHG;
3584 break;
3585 case ir_intrinsic_ssbo_atomic_comp_swap:
3586 opcode = TGSI_OPCODE_ATOMCAS;
3587 param = param->get_next();
3588 val = ((ir_instruction *)param)->as_rvalue();
3589 val->accept(this);
3590 data2 = this->result;
3591 break;
3592 default:
3593 assert(!"Unexpected intrinsic");
3594 return;
3595 }
3596
3597 inst = emit_asm(ir, opcode, dst, off, data, data2);
3598 }
3599
3600 param = param->get_next();
3601 ir_constant *access = NULL;
3602 if (!param->is_tail_sentinel()) {
3603 access = ((ir_instruction *)param)->as_constant();
3604 assert(access);
3605 }
3606
3607 /* The emit_asm() might have actually split the op into pieces, e.g. for
3608 * double stores. We have to go back and fix up all the generated ops.
3609 */
3610 unsigned op = inst->op;
3611 do {
3612 inst->resource = buffer;
3613 if (access)
3614 inst->buffer_access = access->value.u[0];
3615
3616 if (inst == this->instructions.get_head_raw())
3617 break;
3618 inst = (glsl_to_tgsi_instruction *)inst->get_prev();
3619
3620 if (inst->op == TGSI_OPCODE_UADD) {
3621 if (inst == this->instructions.get_head_raw())
3622 break;
3623 inst = (glsl_to_tgsi_instruction *)inst->get_prev();
3624 }
3625 } while (inst->op == op && inst->resource.file == PROGRAM_UNDEFINED);
3626 }
3627
3628 void
3629 glsl_to_tgsi_visitor::visit_membar_intrinsic(ir_call *ir)
3630 {
3631 switch (ir->callee->intrinsic_id) {
3632 case ir_intrinsic_memory_barrier:
3633 emit_asm(ir, TGSI_OPCODE_MEMBAR, undef_dst,
3634 st_src_reg_for_int(TGSI_MEMBAR_SHADER_BUFFER |
3635 TGSI_MEMBAR_ATOMIC_BUFFER |
3636 TGSI_MEMBAR_SHADER_IMAGE |
3637 TGSI_MEMBAR_SHARED));
3638 break;
3639 case ir_intrinsic_memory_barrier_atomic_counter:
3640 emit_asm(ir, TGSI_OPCODE_MEMBAR, undef_dst,
3641 st_src_reg_for_int(TGSI_MEMBAR_ATOMIC_BUFFER));
3642 break;
3643 case ir_intrinsic_memory_barrier_buffer:
3644 emit_asm(ir, TGSI_OPCODE_MEMBAR, undef_dst,
3645 st_src_reg_for_int(TGSI_MEMBAR_SHADER_BUFFER));
3646 break;
3647 case ir_intrinsic_memory_barrier_image:
3648 emit_asm(ir, TGSI_OPCODE_MEMBAR, undef_dst,
3649 st_src_reg_for_int(TGSI_MEMBAR_SHADER_IMAGE));
3650 break;
3651 case ir_intrinsic_memory_barrier_shared:
3652 emit_asm(ir, TGSI_OPCODE_MEMBAR, undef_dst,
3653 st_src_reg_for_int(TGSI_MEMBAR_SHARED));
3654 break;
3655 case ir_intrinsic_group_memory_barrier:
3656 emit_asm(ir, TGSI_OPCODE_MEMBAR, undef_dst,
3657 st_src_reg_for_int(TGSI_MEMBAR_SHADER_BUFFER |
3658 TGSI_MEMBAR_ATOMIC_BUFFER |
3659 TGSI_MEMBAR_SHADER_IMAGE |
3660 TGSI_MEMBAR_SHARED |
3661 TGSI_MEMBAR_THREAD_GROUP));
3662 break;
3663 default:
3664 assert(!"Unexpected memory barrier intrinsic");
3665 }
3666 }
3667
3668 void
3669 glsl_to_tgsi_visitor::visit_shared_intrinsic(ir_call *ir)
3670 {
3671 exec_node *param = ir->actual_parameters.get_head();
3672
3673 ir_rvalue *offset = ((ir_instruction *)param)->as_rvalue();
3674
3675 st_src_reg buffer(PROGRAM_MEMORY, 0, GLSL_TYPE_UINT);
3676
3677 /* Calculate the surface offset */
3678 offset->accept(this);
3679 st_src_reg off = this->result;
3680
3681 st_dst_reg dst = undef_dst;
3682 if (ir->return_deref) {
3683 ir->return_deref->accept(this);
3684 dst = st_dst_reg(this->result);
3685 dst.writemask = (1 << ir->return_deref->type->vector_elements) - 1;
3686 }
3687
3688 glsl_to_tgsi_instruction *inst;
3689
3690 if (ir->callee->intrinsic_id == ir_intrinsic_shared_load) {
3691 inst = emit_asm(ir, TGSI_OPCODE_LOAD, dst, off);
3692 inst->resource = buffer;
3693 } else if (ir->callee->intrinsic_id == ir_intrinsic_shared_store) {
3694 param = param->get_next();
3695 ir_rvalue *val = ((ir_instruction *)param)->as_rvalue();
3696 val->accept(this);
3697
3698 param = param->get_next();
3699 ir_constant *write_mask = ((ir_instruction *)param)->as_constant();
3700 assert(write_mask);
3701 dst.writemask = write_mask->value.u[0];
3702
3703 dst.type = this->result.type;
3704 inst = emit_asm(ir, TGSI_OPCODE_STORE, dst, off, this->result);
3705 inst->resource = buffer;
3706 } else {
3707 param = param->get_next();
3708 ir_rvalue *val = ((ir_instruction *)param)->as_rvalue();
3709 val->accept(this);
3710
3711 st_src_reg data = this->result, data2 = undef_src;
3712 unsigned opcode;
3713 switch (ir->callee->intrinsic_id) {
3714 case ir_intrinsic_shared_atomic_add:
3715 opcode = TGSI_OPCODE_ATOMUADD;
3716 break;
3717 case ir_intrinsic_shared_atomic_min:
3718 opcode = TGSI_OPCODE_ATOMIMIN;
3719 break;
3720 case ir_intrinsic_shared_atomic_max:
3721 opcode = TGSI_OPCODE_ATOMIMAX;
3722 break;
3723 case ir_intrinsic_shared_atomic_and:
3724 opcode = TGSI_OPCODE_ATOMAND;
3725 break;
3726 case ir_intrinsic_shared_atomic_or:
3727 opcode = TGSI_OPCODE_ATOMOR;
3728 break;
3729 case ir_intrinsic_shared_atomic_xor:
3730 opcode = TGSI_OPCODE_ATOMXOR;
3731 break;
3732 case ir_intrinsic_shared_atomic_exchange:
3733 opcode = TGSI_OPCODE_ATOMXCHG;
3734 break;
3735 case ir_intrinsic_shared_atomic_comp_swap:
3736 opcode = TGSI_OPCODE_ATOMCAS;
3737 param = param->get_next();
3738 val = ((ir_instruction *)param)->as_rvalue();
3739 val->accept(this);
3740 data2 = this->result;
3741 break;
3742 default:
3743 assert(!"Unexpected intrinsic");
3744 return;
3745 }
3746
3747 inst = emit_asm(ir, opcode, dst, off, data, data2);
3748 inst->resource = buffer;
3749 }
3750 }
3751
3752 void
3753 glsl_to_tgsi_visitor::visit_image_intrinsic(ir_call *ir)
3754 {
3755 exec_node *param = ir->actual_parameters.get_head();
3756
3757 ir_dereference *img = (ir_dereference *)param;
3758 const ir_variable *imgvar = img->variable_referenced();
3759 const glsl_type *type = imgvar->type->without_array();
3760 unsigned sampler_array_size = 1, sampler_base = 0;
3761
3762 st_src_reg reladdr;
3763 st_src_reg image(PROGRAM_IMAGE, 0, GLSL_TYPE_UINT);
3764
3765 get_deref_offsets(img, &sampler_array_size, &sampler_base,
3766 (uint16_t*)&image.index, &reladdr, true);
3767
3768 if (reladdr.file != PROGRAM_UNDEFINED) {
3769 image.reladdr = ralloc(mem_ctx, st_src_reg);
3770 *image.reladdr = reladdr;
3771 emit_arl(ir, sampler_reladdr, reladdr);
3772 }
3773
3774 st_dst_reg dst = undef_dst;
3775 if (ir->return_deref) {
3776 ir->return_deref->accept(this);
3777 dst = st_dst_reg(this->result);
3778 dst.writemask = (1 << ir->return_deref->type->vector_elements) - 1;
3779 }
3780
3781 glsl_to_tgsi_instruction *inst;
3782
3783 if (ir->callee->intrinsic_id == ir_intrinsic_image_size) {
3784 dst.writemask = WRITEMASK_XYZ;
3785 inst = emit_asm(ir, TGSI_OPCODE_RESQ, dst);
3786 } else if (ir->callee->intrinsic_id == ir_intrinsic_image_samples) {
3787 st_src_reg res = get_temp(glsl_type::ivec4_type);
3788 st_dst_reg dstres = st_dst_reg(res);
3789 dstres.writemask = WRITEMASK_W;
3790 inst = emit_asm(ir, TGSI_OPCODE_RESQ, dstres);
3791 res.swizzle = SWIZZLE_WWWW;
3792 emit_asm(ir, TGSI_OPCODE_MOV, dst, res);
3793 } else {
3794 st_src_reg arg1 = undef_src, arg2 = undef_src;
3795 st_src_reg coord;
3796 st_dst_reg coord_dst;
3797 coord = get_temp(glsl_type::ivec4_type);
3798 coord_dst = st_dst_reg(coord);
3799 coord_dst.writemask = (1 << type->coordinate_components()) - 1;
3800 param = param->get_next();
3801 ((ir_dereference *)param)->accept(this);
3802 emit_asm(ir, TGSI_OPCODE_MOV, coord_dst, this->result);
3803 coord.swizzle = SWIZZLE_XXXX;
3804 switch (type->coordinate_components()) {
3805 case 4: assert(!"unexpected coord count");
3806 /* fallthrough */
3807 case 3: coord.swizzle |= SWIZZLE_Z << 6;
3808 /* fallthrough */
3809 case 2: coord.swizzle |= SWIZZLE_Y << 3;
3810 }
3811
3812 if (type->sampler_dimensionality == GLSL_SAMPLER_DIM_MS) {
3813 param = param->get_next();
3814 ((ir_dereference *)param)->accept(this);
3815 st_src_reg sample = this->result;
3816 sample.swizzle = SWIZZLE_XXXX;
3817 coord_dst.writemask = WRITEMASK_W;
3818 emit_asm(ir, TGSI_OPCODE_MOV, coord_dst, sample);
3819 coord.swizzle |= SWIZZLE_W << 9;
3820 }
3821
3822 param = param->get_next();
3823 if (!param->is_tail_sentinel()) {
3824 ((ir_dereference *)param)->accept(this);
3825 arg1 = this->result;
3826 param = param->get_next();
3827 }
3828
3829 if (!param->is_tail_sentinel()) {
3830 ((ir_dereference *)param)->accept(this);
3831 arg2 = this->result;
3832 param = param->get_next();
3833 }
3834
3835 assert(param->is_tail_sentinel());
3836
3837 unsigned opcode;
3838 switch (ir->callee->intrinsic_id) {
3839 case ir_intrinsic_image_load:
3840 opcode = TGSI_OPCODE_LOAD;
3841 break;
3842 case ir_intrinsic_image_store:
3843 opcode = TGSI_OPCODE_STORE;
3844 break;
3845 case ir_intrinsic_image_atomic_add:
3846 opcode = TGSI_OPCODE_ATOMUADD;
3847 break;
3848 case ir_intrinsic_image_atomic_min:
3849 opcode = TGSI_OPCODE_ATOMIMIN;
3850 break;
3851 case ir_intrinsic_image_atomic_max:
3852 opcode = TGSI_OPCODE_ATOMIMAX;
3853 break;
3854 case ir_intrinsic_image_atomic_and:
3855 opcode = TGSI_OPCODE_ATOMAND;
3856 break;
3857 case ir_intrinsic_image_atomic_or:
3858 opcode = TGSI_OPCODE_ATOMOR;
3859 break;
3860 case ir_intrinsic_image_atomic_xor:
3861 opcode = TGSI_OPCODE_ATOMXOR;
3862 break;
3863 case ir_intrinsic_image_atomic_exchange:
3864 opcode = TGSI_OPCODE_ATOMXCHG;
3865 break;
3866 case ir_intrinsic_image_atomic_comp_swap:
3867 opcode = TGSI_OPCODE_ATOMCAS;
3868 break;
3869 default:
3870 assert(!"Unexpected intrinsic");
3871 return;
3872 }
3873
3874 inst = emit_asm(ir, opcode, dst, coord, arg1, arg2);
3875 if (opcode == TGSI_OPCODE_STORE)
3876 inst->dst[0].writemask = WRITEMASK_XYZW;
3877 }
3878
3879 inst->resource = image;
3880 inst->sampler_array_size = sampler_array_size;
3881 inst->sampler_base = sampler_base;
3882
3883 switch (type->sampler_dimensionality) {
3884 case GLSL_SAMPLER_DIM_1D:
3885 inst->tex_target = (type->sampler_array)
3886 ? TEXTURE_1D_ARRAY_INDEX : TEXTURE_1D_INDEX;
3887 break;
3888 case GLSL_SAMPLER_DIM_2D:
3889 inst->tex_target = (type->sampler_array)
3890 ? TEXTURE_2D_ARRAY_INDEX : TEXTURE_2D_INDEX;
3891 break;
3892 case GLSL_SAMPLER_DIM_3D:
3893 inst->tex_target = TEXTURE_3D_INDEX;
3894 break;
3895 case GLSL_SAMPLER_DIM_CUBE:
3896 inst->tex_target = (type->sampler_array)
3897 ? TEXTURE_CUBE_ARRAY_INDEX : TEXTURE_CUBE_INDEX;
3898 break;
3899 case GLSL_SAMPLER_DIM_RECT:
3900 inst->tex_target = TEXTURE_RECT_INDEX;
3901 break;
3902 case GLSL_SAMPLER_DIM_BUF:
3903 inst->tex_target = TEXTURE_BUFFER_INDEX;
3904 break;
3905 case GLSL_SAMPLER_DIM_EXTERNAL:
3906 inst->tex_target = TEXTURE_EXTERNAL_INDEX;
3907 break;
3908 case GLSL_SAMPLER_DIM_MS:
3909 inst->tex_target = (type->sampler_array)
3910 ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX : TEXTURE_2D_MULTISAMPLE_INDEX;
3911 break;
3912 default:
3913 assert(!"Should not get here.");
3914 }
3915
3916 inst->image_format = st_mesa_format_to_pipe_format(st_context(ctx),
3917 _mesa_get_shader_image_format(imgvar->data.image_format));
3918
3919 if (imgvar->data.image_coherent)
3920 inst->buffer_access |= TGSI_MEMORY_COHERENT;
3921 if (imgvar->data.image_restrict)
3922 inst->buffer_access |= TGSI_MEMORY_RESTRICT;
3923 if (imgvar->data.image_volatile)
3924 inst->buffer_access |= TGSI_MEMORY_VOLATILE;
3925 }
3926
3927 void
3928 glsl_to_tgsi_visitor::visit(ir_call *ir)
3929 {
3930 ir_function_signature *sig = ir->callee;
3931
3932 /* Filter out intrinsics */
3933 switch (sig->intrinsic_id) {
3934 case ir_intrinsic_atomic_counter_read:
3935 case ir_intrinsic_atomic_counter_increment:
3936 case ir_intrinsic_atomic_counter_predecrement:
3937 case ir_intrinsic_atomic_counter_add:
3938 case ir_intrinsic_atomic_counter_min:
3939 case ir_intrinsic_atomic_counter_max:
3940 case ir_intrinsic_atomic_counter_and:
3941 case ir_intrinsic_atomic_counter_or:
3942 case ir_intrinsic_atomic_counter_xor:
3943 case ir_intrinsic_atomic_counter_exchange:
3944 case ir_intrinsic_atomic_counter_comp_swap:
3945 visit_atomic_counter_intrinsic(ir);
3946 return;
3947
3948 case ir_intrinsic_ssbo_load:
3949 case ir_intrinsic_ssbo_store:
3950 case ir_intrinsic_ssbo_atomic_add:
3951 case ir_intrinsic_ssbo_atomic_min:
3952 case ir_intrinsic_ssbo_atomic_max:
3953 case ir_intrinsic_ssbo_atomic_and:
3954 case ir_intrinsic_ssbo_atomic_or:
3955 case ir_intrinsic_ssbo_atomic_xor:
3956 case ir_intrinsic_ssbo_atomic_exchange:
3957 case ir_intrinsic_ssbo_atomic_comp_swap:
3958 visit_ssbo_intrinsic(ir);
3959 return;
3960
3961 case ir_intrinsic_memory_barrier:
3962 case ir_intrinsic_memory_barrier_atomic_counter:
3963 case ir_intrinsic_memory_barrier_buffer:
3964 case ir_intrinsic_memory_barrier_image:
3965 case ir_intrinsic_memory_barrier_shared:
3966 case ir_intrinsic_group_memory_barrier:
3967 visit_membar_intrinsic(ir);
3968 return;
3969
3970 case ir_intrinsic_shared_load:
3971 case ir_intrinsic_shared_store:
3972 case ir_intrinsic_shared_atomic_add:
3973 case ir_intrinsic_shared_atomic_min:
3974 case ir_intrinsic_shared_atomic_max:
3975 case ir_intrinsic_shared_atomic_and:
3976 case ir_intrinsic_shared_atomic_or:
3977 case ir_intrinsic_shared_atomic_xor:
3978 case ir_intrinsic_shared_atomic_exchange:
3979 case ir_intrinsic_shared_atomic_comp_swap:
3980 visit_shared_intrinsic(ir);
3981 return;
3982
3983 case ir_intrinsic_image_load:
3984 case ir_intrinsic_image_store:
3985 case ir_intrinsic_image_atomic_add:
3986 case ir_intrinsic_image_atomic_min:
3987 case ir_intrinsic_image_atomic_max:
3988 case ir_intrinsic_image_atomic_and:
3989 case ir_intrinsic_image_atomic_or:
3990 case ir_intrinsic_image_atomic_xor:
3991 case ir_intrinsic_image_atomic_exchange:
3992 case ir_intrinsic_image_atomic_comp_swap:
3993 case ir_intrinsic_image_size:
3994 case ir_intrinsic_image_samples:
3995 visit_image_intrinsic(ir);
3996 return;
3997
3998 case ir_intrinsic_invalid:
3999 case ir_intrinsic_generic_load:
4000 case ir_intrinsic_generic_store:
4001 case ir_intrinsic_generic_atomic_add:
4002 case ir_intrinsic_generic_atomic_and:
4003 case ir_intrinsic_generic_atomic_or:
4004 case ir_intrinsic_generic_atomic_xor:
4005 case ir_intrinsic_generic_atomic_min:
4006 case ir_intrinsic_generic_atomic_max:
4007 case ir_intrinsic_generic_atomic_exchange:
4008 case ir_intrinsic_generic_atomic_comp_swap:
4009 case ir_intrinsic_shader_clock:
4010 unreachable("Invalid intrinsic");
4011 }
4012 }
4013
4014 void
4015 glsl_to_tgsi_visitor::calc_deref_offsets(ir_dereference *tail,
4016 unsigned *array_elements,
4017 uint16_t *index,
4018 st_src_reg *indirect,
4019 unsigned *location)
4020 {
4021 switch (tail->ir_type) {
4022 case ir_type_dereference_record: {
4023 ir_dereference_record *deref_record = tail->as_dereference_record();
4024 const glsl_type *struct_type = deref_record->record->type;
4025 int field_index = deref_record->record->type->field_index(deref_record->field);
4026
4027 calc_deref_offsets(deref_record->record->as_dereference(), array_elements, index, indirect, location);
4028
4029 assert(field_index >= 0);
4030 *location += struct_type->record_location_offset(field_index);
4031 break;
4032 }
4033
4034 case ir_type_dereference_array: {
4035 ir_dereference_array *deref_arr = tail->as_dereference_array();
4036 ir_constant *array_index = deref_arr->array_index->constant_expression_value();
4037
4038 if (!array_index) {
4039 st_src_reg temp_reg;
4040 st_dst_reg temp_dst;
4041
4042 temp_reg = get_temp(glsl_type::uint_type);
4043 temp_dst = st_dst_reg(temp_reg);
4044 temp_dst.writemask = 1;
4045
4046 deref_arr->array_index->accept(this);
4047 if (*array_elements != 1)
4048 emit_asm(NULL, TGSI_OPCODE_MUL, temp_dst, this->result, st_src_reg_for_int(*array_elements));
4049 else
4050 emit_asm(NULL, TGSI_OPCODE_MOV, temp_dst, this->result);
4051
4052 if (indirect->file == PROGRAM_UNDEFINED)
4053 *indirect = temp_reg;
4054 else {
4055 temp_dst = st_dst_reg(*indirect);
4056 temp_dst.writemask = 1;
4057 emit_asm(NULL, TGSI_OPCODE_ADD, temp_dst, *indirect, temp_reg);
4058 }
4059 } else
4060 *index += array_index->value.u[0] * *array_elements;
4061
4062 *array_elements *= deref_arr->array->type->length;
4063
4064 calc_deref_offsets(deref_arr->array->as_dereference(), array_elements, index, indirect, location);
4065 break;
4066 }
4067 default:
4068 break;
4069 }
4070 }
4071
4072 void
4073 glsl_to_tgsi_visitor::get_deref_offsets(ir_dereference *ir,
4074 unsigned *array_size,
4075 unsigned *base,
4076 uint16_t *index,
4077 st_src_reg *reladdr,
4078 bool opaque)
4079 {
4080 GLuint shader = _mesa_program_enum_to_shader_stage(this->prog->Target);
4081 unsigned location = 0;
4082 ir_variable *var = ir->variable_referenced();
4083
4084 memset(reladdr, 0, sizeof(*reladdr));
4085 reladdr->file = PROGRAM_UNDEFINED;
4086
4087 *base = 0;
4088 *array_size = 1;
4089
4090 assert(var);
4091 location = var->data.location;
4092 calc_deref_offsets(ir, array_size, index, reladdr, &location);
4093
4094 /*
4095 * If we end up with no indirect then adjust the base to the index,
4096 * and set the array size to 1.
4097 */
4098 if (reladdr->file == PROGRAM_UNDEFINED) {
4099 *base = *index;
4100 *array_size = 1;
4101 }
4102
4103 if (opaque) {
4104 assert(location != 0xffffffff);
4105 *base += this->shader_program->data->UniformStorage[location].opaque[shader].index;
4106 *index += this->shader_program->data->UniformStorage[location].opaque[shader].index;
4107 }
4108 }
4109
4110 st_src_reg
4111 glsl_to_tgsi_visitor::canonicalize_gather_offset(st_src_reg offset)
4112 {
4113 if (offset.reladdr || offset.reladdr2) {
4114 st_src_reg tmp = get_temp(glsl_type::ivec2_type);
4115 st_dst_reg tmp_dst = st_dst_reg(tmp);
4116 tmp_dst.writemask = WRITEMASK_XY;
4117 emit_asm(NULL, TGSI_OPCODE_MOV, tmp_dst, offset);
4118 return tmp;
4119 }
4120
4121 return offset;
4122 }
4123
4124 void
4125 glsl_to_tgsi_visitor::visit(ir_texture *ir)
4126 {
4127 st_src_reg result_src, coord, cube_sc, lod_info, projector, dx, dy;
4128 st_src_reg offset[MAX_GLSL_TEXTURE_OFFSET], sample_index, component;
4129 st_src_reg levels_src, reladdr;
4130 st_dst_reg result_dst, coord_dst, cube_sc_dst;
4131 glsl_to_tgsi_instruction *inst = NULL;
4132 unsigned opcode = TGSI_OPCODE_NOP;
4133 const glsl_type *sampler_type = ir->sampler->type;
4134 unsigned sampler_array_size = 1, sampler_base = 0;
4135 uint16_t sampler_index = 0;
4136 bool is_cube_array = false;
4137 unsigned i;
4138
4139 /* if we are a cube array sampler */
4140 if ((sampler_type->sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE &&
4141 sampler_type->sampler_array)) {
4142 is_cube_array = true;
4143 }
4144
4145 if (ir->coordinate) {
4146 ir->coordinate->accept(this);
4147
4148 /* Put our coords in a temp. We'll need to modify them for shadow,
4149 * projection, or LOD, so the only case we'd use it as-is is if
4150 * we're doing plain old texturing. The optimization passes on
4151 * glsl_to_tgsi_visitor should handle cleaning up our mess in that case.
4152 */
4153 coord = get_temp(glsl_type::vec4_type);
4154 coord_dst = st_dst_reg(coord);
4155 coord_dst.writemask = (1 << ir->coordinate->type->vector_elements) - 1;
4156 emit_asm(ir, TGSI_OPCODE_MOV, coord_dst, this->result);
4157 }
4158
4159 if (ir->projector) {
4160 ir->projector->accept(this);
4161 projector = this->result;
4162 }
4163
4164 /* Storage for our result. Ideally for an assignment we'd be using
4165 * the actual storage for the result here, instead.
4166 */
4167 result_src = get_temp(ir->type);
4168 result_dst = st_dst_reg(result_src);
4169 result_dst.writemask = (1 << ir->type->vector_elements) - 1;
4170
4171 switch (ir->op) {
4172 case ir_tex:
4173 opcode = (is_cube_array && ir->shadow_comparator) ? TGSI_OPCODE_TEX2 : TGSI_OPCODE_TEX;
4174 if (ir->offset) {
4175 ir->offset->accept(this);
4176 offset[0] = this->result;
4177 }
4178 break;
4179 case ir_txb:
4180 if (is_cube_array ||
4181 sampler_type == glsl_type::samplerCubeShadow_type) {
4182 opcode = TGSI_OPCODE_TXB2;
4183 }
4184 else {
4185 opcode = TGSI_OPCODE_TXB;
4186 }
4187 ir->lod_info.bias->accept(this);
4188 lod_info = this->result;
4189 if (ir->offset) {
4190 ir->offset->accept(this);
4191 offset[0] = this->result;
4192 }
4193 break;
4194 case ir_txl:
4195 if (this->has_tex_txf_lz && ir->lod_info.lod->is_zero()) {
4196 opcode = TGSI_OPCODE_TEX_LZ;
4197 } else {
4198 opcode = is_cube_array ? TGSI_OPCODE_TXL2 : TGSI_OPCODE_TXL;
4199 ir->lod_info.lod->accept(this);
4200 lod_info = this->result;
4201 }
4202 if (ir->offset) {
4203 ir->offset->accept(this);
4204 offset[0] = this->result;
4205 }
4206 break;
4207 case ir_txd:
4208 opcode = TGSI_OPCODE_TXD;
4209 ir->lod_info.grad.dPdx->accept(this);
4210 dx = this->result;
4211 ir->lod_info.grad.dPdy->accept(this);
4212 dy = this->result;
4213 if (ir->offset) {
4214 ir->offset->accept(this);
4215 offset[0] = this->result;
4216 }
4217 break;
4218 case ir_txs:
4219 opcode = TGSI_OPCODE_TXQ;
4220 ir->lod_info.lod->accept(this);
4221 lod_info = this->result;
4222 break;
4223 case ir_query_levels:
4224 opcode = TGSI_OPCODE_TXQ;
4225 lod_info = undef_src;
4226 levels_src = get_temp(ir->type);
4227 break;
4228 case ir_txf:
4229 if (this->has_tex_txf_lz && ir->lod_info.lod->is_zero()) {
4230 opcode = TGSI_OPCODE_TXF_LZ;
4231 } else {
4232 opcode = TGSI_OPCODE_TXF;
4233 ir->lod_info.lod->accept(this);
4234 lod_info = this->result;
4235 }
4236 if (ir->offset) {
4237 ir->offset->accept(this);
4238 offset[0] = this->result;
4239 }
4240 break;
4241 case ir_txf_ms:
4242 opcode = TGSI_OPCODE_TXF;
4243 ir->lod_info.sample_index->accept(this);
4244 sample_index = this->result;
4245 break;
4246 case ir_tg4:
4247 opcode = TGSI_OPCODE_TG4;
4248 ir->lod_info.component->accept(this);
4249 component = this->result;
4250 if (ir->offset) {
4251 ir->offset->accept(this);
4252 if (ir->offset->type->base_type == GLSL_TYPE_ARRAY) {
4253 const glsl_type *elt_type = ir->offset->type->fields.array;
4254 for (i = 0; i < ir->offset->type->length; i++) {
4255 offset[i] = this->result;
4256 offset[i].index += i * type_size(elt_type);
4257 offset[i].type = elt_type->base_type;
4258 offset[i].swizzle = swizzle_for_size(elt_type->vector_elements);
4259 offset[i] = canonicalize_gather_offset(offset[i]);
4260 }
4261 } else {
4262 offset[0] = canonicalize_gather_offset(this->result);
4263 }
4264 }
4265 break;
4266 case ir_lod:
4267 opcode = TGSI_OPCODE_LODQ;
4268 break;
4269 case ir_texture_samples:
4270 opcode = TGSI_OPCODE_TXQS;
4271 break;
4272 case ir_samples_identical:
4273 unreachable("Unexpected ir_samples_identical opcode");
4274 }
4275
4276 if (ir->projector) {
4277 if (opcode == TGSI_OPCODE_TEX) {
4278 /* Slot the projector in as the last component of the coord. */
4279 coord_dst.writemask = WRITEMASK_W;
4280 emit_asm(ir, TGSI_OPCODE_MOV, coord_dst, projector);
4281 coord_dst.writemask = WRITEMASK_XYZW;
4282 opcode = TGSI_OPCODE_TXP;
4283 } else {
4284 st_src_reg coord_w = coord;
4285 coord_w.swizzle = SWIZZLE_WWWW;
4286
4287 /* For the other TEX opcodes there's no projective version
4288 * since the last slot is taken up by LOD info. Do the
4289 * projective divide now.
4290 */
4291 coord_dst.writemask = WRITEMASK_W;
4292 emit_asm(ir, TGSI_OPCODE_RCP, coord_dst, projector);
4293
4294 /* In the case where we have to project the coordinates "by hand,"
4295 * the shadow comparator value must also be projected.
4296 */
4297 st_src_reg tmp_src = coord;
4298 if (ir->shadow_comparator) {
4299 /* Slot the shadow value in as the second to last component of the
4300 * coord.
4301 */
4302 ir->shadow_comparator->accept(this);
4303
4304 tmp_src = get_temp(glsl_type::vec4_type);
4305 st_dst_reg tmp_dst = st_dst_reg(tmp_src);
4306
4307 /* Projective division not allowed for array samplers. */
4308 assert(!sampler_type->sampler_array);
4309
4310 tmp_dst.writemask = WRITEMASK_Z;
4311 emit_asm(ir, TGSI_OPCODE_MOV, tmp_dst, this->result);
4312
4313 tmp_dst.writemask = WRITEMASK_XY;
4314 emit_asm(ir, TGSI_OPCODE_MOV, tmp_dst, coord);
4315 }
4316
4317 coord_dst.writemask = WRITEMASK_XYZ;
4318 emit_asm(ir, TGSI_OPCODE_MUL, coord_dst, tmp_src, coord_w);
4319
4320 coord_dst.writemask = WRITEMASK_XYZW;
4321 coord.swizzle = SWIZZLE_XYZW;
4322 }
4323 }
4324
4325 /* If projection is done and the opcode is not TGSI_OPCODE_TXP, then the shadow
4326 * comparator was put in the correct place (and projected) by the code,
4327 * above, that handles by-hand projection.
4328 */
4329 if (ir->shadow_comparator && (!ir->projector || opcode == TGSI_OPCODE_TXP)) {
4330 /* Slot the shadow value in as the second to last component of the
4331 * coord.
4332 */
4333 ir->shadow_comparator->accept(this);
4334
4335 if (is_cube_array) {
4336 cube_sc = get_temp(glsl_type::float_type);
4337 cube_sc_dst = st_dst_reg(cube_sc);
4338 cube_sc_dst.writemask = WRITEMASK_X;
4339 emit_asm(ir, TGSI_OPCODE_MOV, cube_sc_dst, this->result);
4340 cube_sc_dst.writemask = WRITEMASK_X;
4341 }
4342 else {
4343 if ((sampler_type->sampler_dimensionality == GLSL_SAMPLER_DIM_2D &&
4344 sampler_type->sampler_array) ||
4345 sampler_type->sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE) {
4346 coord_dst.writemask = WRITEMASK_W;
4347 } else {
4348 coord_dst.writemask = WRITEMASK_Z;
4349 }
4350 emit_asm(ir, TGSI_OPCODE_MOV, coord_dst, this->result);
4351 coord_dst.writemask = WRITEMASK_XYZW;
4352 }
4353 }
4354
4355 if (ir->op == ir_txf_ms) {
4356 coord_dst.writemask = WRITEMASK_W;
4357 emit_asm(ir, TGSI_OPCODE_MOV, coord_dst, sample_index);
4358 coord_dst.writemask = WRITEMASK_XYZW;
4359 } else if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXB ||
4360 opcode == TGSI_OPCODE_TXF) {
4361 /* TGSI stores LOD or LOD bias in the last channel of the coords. */
4362 coord_dst.writemask = WRITEMASK_W;
4363 emit_asm(ir, TGSI_OPCODE_MOV, coord_dst, lod_info);
4364 coord_dst.writemask = WRITEMASK_XYZW;
4365 }
4366
4367 get_deref_offsets(ir->sampler, &sampler_array_size, &sampler_base,
4368 &sampler_index, &reladdr, true);
4369 if (reladdr.file != PROGRAM_UNDEFINED)
4370 emit_arl(ir, sampler_reladdr, reladdr);
4371
4372 if (opcode == TGSI_OPCODE_TXD)
4373 inst = emit_asm(ir, opcode, result_dst, coord, dx, dy);
4374 else if (opcode == TGSI_OPCODE_TXQ) {
4375 if (ir->op == ir_query_levels) {
4376 /* the level is stored in W */
4377 inst = emit_asm(ir, opcode, st_dst_reg(levels_src), lod_info);
4378 result_dst.writemask = WRITEMASK_X;
4379 levels_src.swizzle = SWIZZLE_WWWW;
4380 emit_asm(ir, TGSI_OPCODE_MOV, result_dst, levels_src);
4381 } else
4382 inst = emit_asm(ir, opcode, result_dst, lod_info);
4383 } else if (opcode == TGSI_OPCODE_TXQS) {
4384 inst = emit_asm(ir, opcode, result_dst);
4385 } else if (opcode == TGSI_OPCODE_TXL2 || opcode == TGSI_OPCODE_TXB2) {
4386 inst = emit_asm(ir, opcode, result_dst, coord, lod_info);
4387 } else if (opcode == TGSI_OPCODE_TEX2) {
4388 inst = emit_asm(ir, opcode, result_dst, coord, cube_sc);
4389 } else if (opcode == TGSI_OPCODE_TG4) {
4390 if (is_cube_array && ir->shadow_comparator) {
4391 inst = emit_asm(ir, opcode, result_dst, coord, cube_sc);
4392 } else {
4393 inst = emit_asm(ir, opcode, result_dst, coord, component);
4394 }
4395 } else
4396 inst = emit_asm(ir, opcode, result_dst, coord);
4397
4398 if (ir->shadow_comparator)
4399 inst->tex_shadow = GL_TRUE;
4400
4401 inst->resource.index = sampler_index;
4402 inst->sampler_array_size = sampler_array_size;
4403 inst->sampler_base = sampler_base;
4404
4405 if (reladdr.file != PROGRAM_UNDEFINED) {
4406 inst->resource.reladdr = ralloc(mem_ctx, st_src_reg);
4407 memcpy(inst->resource.reladdr, &reladdr, sizeof(reladdr));
4408 }
4409
4410 if (ir->offset) {
4411 if (!inst->tex_offsets)
4412 inst->tex_offsets = rzalloc_array(inst, st_src_reg, MAX_GLSL_TEXTURE_OFFSET);
4413
4414 for (i = 0; i < MAX_GLSL_TEXTURE_OFFSET && offset[i].file != PROGRAM_UNDEFINED; i++)
4415 inst->tex_offsets[i] = offset[i];
4416 inst->tex_offset_num_offset = i;
4417 }
4418
4419 switch (sampler_type->sampler_dimensionality) {
4420 case GLSL_SAMPLER_DIM_1D:
4421 inst->tex_target = (sampler_type->sampler_array)
4422 ? TEXTURE_1D_ARRAY_INDEX : TEXTURE_1D_INDEX;
4423 break;
4424 case GLSL_SAMPLER_DIM_2D:
4425 inst->tex_target = (sampler_type->sampler_array)
4426 ? TEXTURE_2D_ARRAY_INDEX : TEXTURE_2D_INDEX;
4427 break;
4428 case GLSL_SAMPLER_DIM_3D:
4429 inst->tex_target = TEXTURE_3D_INDEX;
4430 break;
4431 case GLSL_SAMPLER_DIM_CUBE:
4432 inst->tex_target = (sampler_type->sampler_array)
4433 ? TEXTURE_CUBE_ARRAY_INDEX : TEXTURE_CUBE_INDEX;
4434 break;
4435 case GLSL_SAMPLER_DIM_RECT:
4436 inst->tex_target = TEXTURE_RECT_INDEX;
4437 break;
4438 case GLSL_SAMPLER_DIM_BUF:
4439 inst->tex_target = TEXTURE_BUFFER_INDEX;
4440 break;
4441 case GLSL_SAMPLER_DIM_EXTERNAL:
4442 inst->tex_target = TEXTURE_EXTERNAL_INDEX;
4443 break;
4444 case GLSL_SAMPLER_DIM_MS:
4445 inst->tex_target = (sampler_type->sampler_array)
4446 ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX : TEXTURE_2D_MULTISAMPLE_INDEX;
4447 break;
4448 default:
4449 assert(!"Should not get here.");
4450 }
4451
4452 inst->tex_type = ir->type->base_type;
4453
4454 this->result = result_src;
4455 }
4456
4457 void
4458 glsl_to_tgsi_visitor::visit(ir_return *ir)
4459 {
4460 assert(!ir->get_value());
4461
4462 emit_asm(ir, TGSI_OPCODE_RET);
4463 }
4464
4465 void
4466 glsl_to_tgsi_visitor::visit(ir_discard *ir)
4467 {
4468 if (ir->condition) {
4469 ir->condition->accept(this);
4470 st_src_reg condition = this->result;
4471
4472 /* Convert the bool condition to a float so we can negate. */
4473 if (native_integers) {
4474 st_src_reg temp = get_temp(ir->condition->type);
4475 emit_asm(ir, TGSI_OPCODE_AND, st_dst_reg(temp),
4476 condition, st_src_reg_for_float(1.0));
4477 condition = temp;
4478 }
4479
4480 condition.negate = ~condition.negate;
4481 emit_asm(ir, TGSI_OPCODE_KILL_IF, undef_dst, condition);
4482 } else {
4483 /* unconditional kil */
4484 emit_asm(ir, TGSI_OPCODE_KILL);
4485 }
4486 }
4487
4488 void
4489 glsl_to_tgsi_visitor::visit(ir_if *ir)
4490 {
4491 unsigned if_opcode;
4492 glsl_to_tgsi_instruction *if_inst;
4493
4494 ir->condition->accept(this);
4495 assert(this->result.file != PROGRAM_UNDEFINED);
4496
4497 if_opcode = native_integers ? TGSI_OPCODE_UIF : TGSI_OPCODE_IF;
4498
4499 if_inst = emit_asm(ir->condition, if_opcode, undef_dst, this->result);
4500
4501 this->instructions.push_tail(if_inst);
4502
4503 visit_exec_list(&ir->then_instructions, this);
4504
4505 if (!ir->else_instructions.is_empty()) {
4506 emit_asm(ir->condition, TGSI_OPCODE_ELSE);
4507 visit_exec_list(&ir->else_instructions, this);
4508 }
4509
4510 if_inst = emit_asm(ir->condition, TGSI_OPCODE_ENDIF);
4511 }
4512
4513
4514 void
4515 glsl_to_tgsi_visitor::visit(ir_emit_vertex *ir)
4516 {
4517 assert(this->prog->Target == GL_GEOMETRY_PROGRAM_NV);
4518
4519 ir->stream->accept(this);
4520 emit_asm(ir, TGSI_OPCODE_EMIT, undef_dst, this->result);
4521 }
4522
4523 void
4524 glsl_to_tgsi_visitor::visit(ir_end_primitive *ir)
4525 {
4526 assert(this->prog->Target == GL_GEOMETRY_PROGRAM_NV);
4527
4528 ir->stream->accept(this);
4529 emit_asm(ir, TGSI_OPCODE_ENDPRIM, undef_dst, this->result);
4530 }
4531
4532 void
4533 glsl_to_tgsi_visitor::visit(ir_barrier *ir)
4534 {
4535 assert(this->prog->Target == GL_TESS_CONTROL_PROGRAM_NV ||
4536 this->prog->Target == GL_COMPUTE_PROGRAM_NV);
4537
4538 emit_asm(ir, TGSI_OPCODE_BARRIER);
4539 }
4540
4541 glsl_to_tgsi_visitor::glsl_to_tgsi_visitor()
4542 {
4543 STATIC_ASSERT(sizeof(samplers_used) * 8 >= PIPE_MAX_SAMPLERS);
4544
4545 result.file = PROGRAM_UNDEFINED;
4546 next_temp = 1;
4547 array_sizes = NULL;
4548 max_num_arrays = 0;
4549 next_array = 0;
4550 num_inputs = 0;
4551 num_outputs = 0;
4552 num_input_arrays = 0;
4553 num_output_arrays = 0;
4554 num_immediates = 0;
4555 num_address_regs = 0;
4556 samplers_used = 0;
4557 buffers_used = 0;
4558 images_used = 0;
4559 indirect_addr_consts = false;
4560 wpos_transform_const = -1;
4561 glsl_version = 0;
4562 native_integers = false;
4563 mem_ctx = ralloc_context(NULL);
4564 ctx = NULL;
4565 prog = NULL;
4566 shader_program = NULL;
4567 shader = NULL;
4568 options = NULL;
4569 have_sqrt = false;
4570 have_fma = false;
4571 use_shared_memory = false;
4572 has_tex_txf_lz = false;
4573 }
4574
4575 glsl_to_tgsi_visitor::~glsl_to_tgsi_visitor()
4576 {
4577 free(array_sizes);
4578 ralloc_free(mem_ctx);
4579 }
4580
4581 extern "C" void free_glsl_to_tgsi_visitor(glsl_to_tgsi_visitor *v)
4582 {
4583 delete v;
4584 }
4585
4586
4587 /**
4588 * Count resources used by the given gpu program (number of texture
4589 * samplers, etc).
4590 */
4591 static void
4592 count_resources(glsl_to_tgsi_visitor *v, gl_program *prog)
4593 {
4594 v->samplers_used = 0;
4595 v->buffers_used = 0;
4596 v->images_used = 0;
4597
4598 foreach_in_list(glsl_to_tgsi_instruction, inst, &v->instructions) {
4599 if (inst->info->is_tex) {
4600 for (int i = 0; i < inst->sampler_array_size; i++) {
4601 unsigned idx = inst->sampler_base + i;
4602 v->samplers_used |= 1u << idx;
4603
4604 debug_assert(idx < (int)ARRAY_SIZE(v->sampler_types));
4605 v->sampler_types[idx] = inst->tex_type;
4606 v->sampler_targets[idx] =
4607 st_translate_texture_target(inst->tex_target, inst->tex_shadow);
4608
4609 if (inst->tex_shadow) {
4610 prog->ShadowSamplers |= 1 << (inst->resource.index + i);
4611 }
4612 }
4613 }
4614
4615 if (inst->tex_target == TEXTURE_EXTERNAL_INDEX)
4616 prog->ExternalSamplersUsed |= 1 << inst->resource.index;
4617
4618 if (inst->resource.file != PROGRAM_UNDEFINED && (
4619 is_resource_instruction(inst->op) ||
4620 inst->op == TGSI_OPCODE_STORE)) {
4621 if (inst->resource.file == PROGRAM_BUFFER) {
4622 v->buffers_used |= 1 << inst->resource.index;
4623 } else if (inst->resource.file == PROGRAM_MEMORY) {
4624 v->use_shared_memory = true;
4625 } else {
4626 assert(inst->resource.file == PROGRAM_IMAGE);
4627 for (int i = 0; i < inst->sampler_array_size; i++) {
4628 unsigned idx = inst->sampler_base + i;
4629 v->images_used |= 1 << idx;
4630 v->image_targets[idx] =
4631 st_translate_texture_target(inst->tex_target, false);
4632 v->image_formats[idx] = inst->image_format;
4633 }
4634 }
4635 }
4636 }
4637 prog->SamplersUsed = v->samplers_used;
4638
4639 if (v->shader_program != NULL)
4640 _mesa_update_shader_textures_used(v->shader_program, prog);
4641 }
4642
4643 /**
4644 * Returns the mask of channels (bitmask of WRITEMASK_X,Y,Z,W) which
4645 * are read from the given src in this instruction
4646 */
4647 static int
4648 get_src_arg_mask(st_dst_reg dst, st_src_reg src)
4649 {
4650 int read_mask = 0, comp;
4651
4652 /* Now, given the src swizzle and the written channels, find which
4653 * components are actually read
4654 */
4655 for (comp = 0; comp < 4; ++comp) {
4656 const unsigned coord = GET_SWZ(src.swizzle, comp);
4657 assert(coord < 4);
4658 if (dst.writemask & (1 << comp) && coord <= SWIZZLE_W)
4659 read_mask |= 1 << coord;
4660 }
4661
4662 return read_mask;
4663 }
4664
4665 /**
4666 * This pass replaces CMP T0, T1 T2 T0 with MOV T0, T2 when the CMP
4667 * instruction is the first instruction to write to register T0. There are
4668 * several lowering passes done in GLSL IR (e.g. branches and
4669 * relative addressing) that create a large number of conditional assignments
4670 * that ir_to_mesa converts to CMP instructions like the one mentioned above.
4671 *
4672 * Here is why this conversion is safe:
4673 * CMP T0, T1 T2 T0 can be expanded to:
4674 * if (T1 < 0.0)
4675 * MOV T0, T2;
4676 * else
4677 * MOV T0, T0;
4678 *
4679 * If (T1 < 0.0) evaluates to true then our replacement MOV T0, T2 is the same
4680 * as the original program. If (T1 < 0.0) evaluates to false, executing
4681 * MOV T0, T0 will store a garbage value in T0 since T0 is uninitialized.
4682 * Therefore, it doesn't matter that we are replacing MOV T0, T0 with MOV T0, T2
4683 * because any instruction that was going to read from T0 after this was going
4684 * to read a garbage value anyway.
4685 */
4686 void
4687 glsl_to_tgsi_visitor::simplify_cmp(void)
4688 {
4689 int tempWritesSize = 0;
4690 unsigned *tempWrites = NULL;
4691 unsigned outputWrites[VARYING_SLOT_TESS_MAX];
4692
4693 memset(outputWrites, 0, sizeof(outputWrites));
4694
4695 foreach_in_list(glsl_to_tgsi_instruction, inst, &this->instructions) {
4696 unsigned prevWriteMask = 0;
4697
4698 /* Give up if we encounter relative addressing or flow control. */
4699 if (inst->dst[0].reladdr || inst->dst[0].reladdr2 ||
4700 inst->dst[1].reladdr || inst->dst[1].reladdr2 ||
4701 tgsi_get_opcode_info(inst->op)->is_branch ||
4702 inst->op == TGSI_OPCODE_CONT ||
4703 inst->op == TGSI_OPCODE_END ||
4704 inst->op == TGSI_OPCODE_RET) {
4705 break;
4706 }
4707
4708 if (inst->dst[0].file == PROGRAM_OUTPUT) {
4709 assert(inst->dst[0].index < (signed)ARRAY_SIZE(outputWrites));
4710 prevWriteMask = outputWrites[inst->dst[0].index];
4711 outputWrites[inst->dst[0].index] |= inst->dst[0].writemask;
4712 } else if (inst->dst[0].file == PROGRAM_TEMPORARY) {
4713 if (inst->dst[0].index >= tempWritesSize) {
4714 const int inc = 4096;
4715
4716 tempWrites = (unsigned*)
4717 realloc(tempWrites,
4718 (tempWritesSize + inc) * sizeof(unsigned));
4719 if (!tempWrites)
4720 return;
4721
4722 memset(tempWrites + tempWritesSize, 0, inc * sizeof(unsigned));
4723 tempWritesSize += inc;
4724 }
4725
4726 prevWriteMask = tempWrites[inst->dst[0].index];
4727 tempWrites[inst->dst[0].index] |= inst->dst[0].writemask;
4728 } else
4729 continue;
4730
4731 /* For a CMP to be considered a conditional write, the destination
4732 * register and source register two must be the same. */
4733 if (inst->op == TGSI_OPCODE_CMP
4734 && !(inst->dst[0].writemask & prevWriteMask)
4735 && inst->src[2].file == inst->dst[0].file
4736 && inst->src[2].index == inst->dst[0].index
4737 && inst->dst[0].writemask == get_src_arg_mask(inst->dst[0], inst->src[2])) {
4738
4739 inst->op = TGSI_OPCODE_MOV;
4740 inst->info = tgsi_get_opcode_info(inst->op);
4741 inst->src[0] = inst->src[1];
4742 }
4743 }
4744
4745 free(tempWrites);
4746 }
4747
4748 /* Replaces all references to a temporary register index with another index. */
4749 void
4750 glsl_to_tgsi_visitor::rename_temp_registers(int num_renames, struct rename_reg_pair *renames)
4751 {
4752 foreach_in_list(glsl_to_tgsi_instruction, inst, &this->instructions) {
4753 unsigned j;
4754 int k;
4755 for (j = 0; j < num_inst_src_regs(inst); j++) {
4756 if (inst->src[j].file == PROGRAM_TEMPORARY)
4757 for (k = 0; k < num_renames; k++)
4758 if (inst->src[j].index == renames[k].old_reg)
4759 inst->src[j].index = renames[k].new_reg;
4760 }
4761
4762 for (j = 0; j < inst->tex_offset_num_offset; j++) {
4763 if (inst->tex_offsets[j].file == PROGRAM_TEMPORARY)
4764 for (k = 0; k < num_renames; k++)
4765 if (inst->tex_offsets[j].index == renames[k].old_reg)
4766 inst->tex_offsets[j].index = renames[k].new_reg;
4767 }
4768
4769 for (j = 0; j < num_inst_dst_regs(inst); j++) {
4770 if (inst->dst[j].file == PROGRAM_TEMPORARY)
4771 for (k = 0; k < num_renames; k++)
4772 if (inst->dst[j].index == renames[k].old_reg)
4773 inst->dst[j].index = renames[k].new_reg;
4774 }
4775 }
4776 }
4777
4778 void
4779 glsl_to_tgsi_visitor::get_first_temp_read(int *first_reads)
4780 {
4781 int depth = 0; /* loop depth */
4782 int loop_start = -1; /* index of the first active BGNLOOP (if any) */
4783 unsigned i = 0, j;
4784
4785 foreach_in_list(glsl_to_tgsi_instruction, inst, &this->instructions) {
4786 for (j = 0; j < num_inst_src_regs(inst); j++) {
4787 if (inst->src[j].file == PROGRAM_TEMPORARY) {
4788 if (first_reads[inst->src[j].index] == -1)
4789 first_reads[inst->src[j].index] = (depth == 0) ? i : loop_start;
4790 }
4791 }
4792 for (j = 0; j < inst->tex_offset_num_offset; j++) {
4793 if (inst->tex_offsets[j].file == PROGRAM_TEMPORARY) {
4794 if (first_reads[inst->tex_offsets[j].index] == -1)
4795 first_reads[inst->tex_offsets[j].index] = (depth == 0) ? i : loop_start;
4796 }
4797 }
4798 if (inst->op == TGSI_OPCODE_BGNLOOP) {
4799 if(depth++ == 0)
4800 loop_start = i;
4801 } else if (inst->op == TGSI_OPCODE_ENDLOOP) {
4802 if (--depth == 0)
4803 loop_start = -1;
4804 }
4805 assert(depth >= 0);
4806 i++;
4807 }
4808 }
4809
4810 void
4811 glsl_to_tgsi_visitor::get_last_temp_read_first_temp_write(int *last_reads, int *first_writes)
4812 {
4813 int depth = 0; /* loop depth */
4814 int loop_start = -1; /* index of the first active BGNLOOP (if any) */
4815 unsigned i = 0, j;
4816 int k;
4817 foreach_in_list(glsl_to_tgsi_instruction, inst, &this->instructions) {
4818 for (j = 0; j < num_inst_src_regs(inst); j++) {
4819 if (inst->src[j].file == PROGRAM_TEMPORARY)
4820 last_reads[inst->src[j].index] = (depth == 0) ? i : -2;
4821 }
4822 for (j = 0; j < num_inst_dst_regs(inst); j++) {
4823 if (inst->dst[j].file == PROGRAM_TEMPORARY) {
4824 if (first_writes[inst->dst[j].index] == -1)
4825 first_writes[inst->dst[j].index] = (depth == 0) ? i : loop_start;
4826 last_reads[inst->dst[j].index] = (depth == 0) ? i : -2;
4827 }
4828 }
4829 for (j = 0; j < inst->tex_offset_num_offset; j++) {
4830 if (inst->tex_offsets[j].file == PROGRAM_TEMPORARY)
4831 last_reads[inst->tex_offsets[j].index] = (depth == 0) ? i : -2;
4832 }
4833 if (inst->op == TGSI_OPCODE_BGNLOOP) {
4834 if(depth++ == 0)
4835 loop_start = i;
4836 } else if (inst->op == TGSI_OPCODE_ENDLOOP) {
4837 if (--depth == 0) {
4838 loop_start = -1;
4839 for (k = 0; k < this->next_temp; k++) {
4840 if (last_reads[k] == -2) {
4841 last_reads[k] = i;
4842 }
4843 }
4844 }
4845 }
4846 assert(depth >= 0);
4847 i++;
4848 }
4849 }
4850
4851 void
4852 glsl_to_tgsi_visitor::get_last_temp_write(int *last_writes)
4853 {
4854 int depth = 0; /* loop depth */
4855 int i = 0, k;
4856 unsigned j;
4857
4858 foreach_in_list(glsl_to_tgsi_instruction, inst, &this->instructions) {
4859 for (j = 0; j < num_inst_dst_regs(inst); j++) {
4860 if (inst->dst[j].file == PROGRAM_TEMPORARY)
4861 last_writes[inst->dst[j].index] = (depth == 0) ? i : -2;
4862 }
4863
4864 if (inst->op == TGSI_OPCODE_BGNLOOP)
4865 depth++;
4866 else if (inst->op == TGSI_OPCODE_ENDLOOP)
4867 if (--depth == 0) {
4868 for (k = 0; k < this->next_temp; k++) {
4869 if (last_writes[k] == -2) {
4870 last_writes[k] = i;
4871 }
4872 }
4873 }
4874 assert(depth >= 0);
4875 i++;
4876 }
4877 }
4878
4879 /*
4880 * On a basic block basis, tracks available PROGRAM_TEMPORARY register
4881 * channels for copy propagation and updates following instructions to
4882 * use the original versions.
4883 *
4884 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
4885 * will occur. As an example, a TXP production before this pass:
4886 *
4887 * 0: MOV TEMP[1], INPUT[4].xyyy;
4888 * 1: MOV TEMP[1].w, INPUT[4].wwww;
4889 * 2: TXP TEMP[2], TEMP[1], texture[0], 2D;
4890 *
4891 * and after:
4892 *
4893 * 0: MOV TEMP[1], INPUT[4].xyyy;
4894 * 1: MOV TEMP[1].w, INPUT[4].wwww;
4895 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
4896 *
4897 * which allows for dead code elimination on TEMP[1]'s writes.
4898 */
4899 void
4900 glsl_to_tgsi_visitor::copy_propagate(void)
4901 {
4902 glsl_to_tgsi_instruction **acp = rzalloc_array(mem_ctx,
4903 glsl_to_tgsi_instruction *,
4904 this->next_temp * 4);
4905 int *acp_level = rzalloc_array(mem_ctx, int, this->next_temp * 4);
4906 int level = 0;
4907
4908 foreach_in_list(glsl_to_tgsi_instruction, inst, &this->instructions) {
4909 assert(inst->dst[0].file != PROGRAM_TEMPORARY
4910 || inst->dst[0].index < this->next_temp);
4911
4912 /* First, do any copy propagation possible into the src regs. */
4913 for (int r = 0; r < 3; r++) {
4914 glsl_to_tgsi_instruction *first = NULL;
4915 bool good = true;
4916 int acp_base = inst->src[r].index * 4;
4917
4918 if (inst->src[r].file != PROGRAM_TEMPORARY ||
4919 inst->src[r].reladdr ||
4920 inst->src[r].reladdr2)
4921 continue;
4922
4923 /* See if we can find entries in the ACP consisting of MOVs
4924 * from the same src register for all the swizzled channels
4925 * of this src register reference.
4926 */
4927 for (int i = 0; i < 4; i++) {
4928 int src_chan = GET_SWZ(inst->src[r].swizzle, i);
4929 glsl_to_tgsi_instruction *copy_chan = acp[acp_base + src_chan];
4930
4931 if (!copy_chan) {
4932 good = false;
4933 break;
4934 }
4935
4936 assert(acp_level[acp_base + src_chan] <= level);
4937
4938 if (!first) {
4939 first = copy_chan;
4940 } else {
4941 if (first->src[0].file != copy_chan->src[0].file ||
4942 first->src[0].index != copy_chan->src[0].index ||
4943 first->src[0].double_reg2 != copy_chan->src[0].double_reg2 ||
4944 first->src[0].index2D != copy_chan->src[0].index2D) {
4945 good = false;
4946 break;
4947 }
4948 }
4949 }
4950
4951 if (good) {
4952 /* We've now validated that we can copy-propagate to
4953 * replace this src register reference. Do it.
4954 */
4955 inst->src[r].file = first->src[0].file;
4956 inst->src[r].index = first->src[0].index;
4957 inst->src[r].index2D = first->src[0].index2D;
4958 inst->src[r].has_index2 = first->src[0].has_index2;
4959 inst->src[r].double_reg2 = first->src[0].double_reg2;
4960 inst->src[r].array_id = first->src[0].array_id;
4961
4962 int swizzle = 0;
4963 for (int i = 0; i < 4; i++) {
4964 int src_chan = GET_SWZ(inst->src[r].swizzle, i);
4965 glsl_to_tgsi_instruction *copy_inst = acp[acp_base + src_chan];
4966 swizzle |= (GET_SWZ(copy_inst->src[0].swizzle, src_chan) << (3 * i));
4967 }
4968 inst->src[r].swizzle = swizzle;
4969 }
4970 }
4971
4972 switch (inst->op) {
4973 case TGSI_OPCODE_BGNLOOP:
4974 case TGSI_OPCODE_ENDLOOP:
4975 /* End of a basic block, clear the ACP entirely. */
4976 memset(acp, 0, sizeof(*acp) * this->next_temp * 4);
4977 break;
4978
4979 case TGSI_OPCODE_IF:
4980 case TGSI_OPCODE_UIF:
4981 ++level;
4982 break;
4983
4984 case TGSI_OPCODE_ENDIF:
4985 case TGSI_OPCODE_ELSE:
4986 /* Clear all channels written inside the block from the ACP, but
4987 * leaving those that were not touched.
4988 */
4989 for (int r = 0; r < this->next_temp; r++) {
4990 for (int c = 0; c < 4; c++) {
4991 if (!acp[4 * r + c])
4992 continue;
4993
4994 if (acp_level[4 * r + c] >= level)
4995 acp[4 * r + c] = NULL;
4996 }
4997 }
4998 if (inst->op == TGSI_OPCODE_ENDIF)
4999 --level;
5000 break;
5001
5002 default:
5003 /* Continuing the block, clear any written channels from
5004 * the ACP.
5005 */
5006 for (int d = 0; d < 2; d++) {
5007 if (inst->dst[d].file == PROGRAM_TEMPORARY && inst->dst[d].reladdr) {
5008 /* Any temporary might be written, so no copy propagation
5009 * across this instruction.
5010 */
5011 memset(acp, 0, sizeof(*acp) * this->next_temp * 4);
5012 } else if (inst->dst[d].file == PROGRAM_OUTPUT &&
5013 inst->dst[d].reladdr) {
5014 /* Any output might be written, so no copy propagation
5015 * from outputs across this instruction.
5016 */
5017 for (int r = 0; r < this->next_temp; r++) {
5018 for (int c = 0; c < 4; c++) {
5019 if (!acp[4 * r + c])
5020 continue;
5021
5022 if (acp[4 * r + c]->src[0].file == PROGRAM_OUTPUT)
5023 acp[4 * r + c] = NULL;
5024 }
5025 }
5026 } else if (inst->dst[d].file == PROGRAM_TEMPORARY ||
5027 inst->dst[d].file == PROGRAM_OUTPUT) {
5028 /* Clear where it's used as dst. */
5029 if (inst->dst[d].file == PROGRAM_TEMPORARY) {
5030 for (int c = 0; c < 4; c++) {
5031 if (inst->dst[d].writemask & (1 << c))
5032 acp[4 * inst->dst[d].index + c] = NULL;
5033 }
5034 }
5035
5036 /* Clear where it's used as src. */
5037 for (int r = 0; r < this->next_temp; r++) {
5038 for (int c = 0; c < 4; c++) {
5039 if (!acp[4 * r + c])
5040 continue;
5041
5042 int src_chan = GET_SWZ(acp[4 * r + c]->src[0].swizzle, c);
5043
5044 if (acp[4 * r + c]->src[0].file == inst->dst[d].file &&
5045 acp[4 * r + c]->src[0].index == inst->dst[d].index &&
5046 inst->dst[d].writemask & (1 << src_chan)) {
5047 acp[4 * r + c] = NULL;
5048 }
5049 }
5050 }
5051 }
5052 }
5053 break;
5054 }
5055
5056 /* If this is a copy, add it to the ACP. */
5057 if (inst->op == TGSI_OPCODE_MOV &&
5058 inst->dst[0].file == PROGRAM_TEMPORARY &&
5059 !(inst->dst[0].file == inst->src[0].file &&
5060 inst->dst[0].index == inst->src[0].index) &&
5061 !inst->dst[0].reladdr &&
5062 !inst->dst[0].reladdr2 &&
5063 !inst->saturate &&
5064 inst->src[0].file != PROGRAM_ARRAY &&
5065 !inst->src[0].reladdr &&
5066 !inst->src[0].reladdr2 &&
5067 !inst->src[0].negate &&
5068 !inst->src[0].abs) {
5069 for (int i = 0; i < 4; i++) {
5070 if (inst->dst[0].writemask & (1 << i)) {
5071 acp[4 * inst->dst[0].index + i] = inst;
5072 acp_level[4 * inst->dst[0].index + i] = level;
5073 }
5074 }
5075 }
5076 }
5077
5078 ralloc_free(acp_level);
5079 ralloc_free(acp);
5080 }
5081
5082 /*
5083 * On a basic block basis, tracks available PROGRAM_TEMPORARY registers for dead
5084 * code elimination.
5085 *
5086 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
5087 * will occur. As an example, a TXP production after copy propagation but
5088 * before this pass:
5089 *
5090 * 0: MOV TEMP[1], INPUT[4].xyyy;
5091 * 1: MOV TEMP[1].w, INPUT[4].wwww;
5092 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
5093 *
5094 * and after this pass:
5095 *
5096 * 0: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
5097 */
5098 int
5099 glsl_to_tgsi_visitor::eliminate_dead_code(void)
5100 {
5101 glsl_to_tgsi_instruction **writes = rzalloc_array(mem_ctx,
5102 glsl_to_tgsi_instruction *,
5103 this->next_temp * 4);
5104 int *write_level = rzalloc_array(mem_ctx, int, this->next_temp * 4);
5105 int level = 0;
5106 int removed = 0;
5107
5108 foreach_in_list(glsl_to_tgsi_instruction, inst, &this->instructions) {
5109 assert(inst->dst[0].file != PROGRAM_TEMPORARY
5110 || inst->dst[0].index < this->next_temp);
5111
5112 switch (inst->op) {
5113 case TGSI_OPCODE_BGNLOOP:
5114 case TGSI_OPCODE_ENDLOOP:
5115 case TGSI_OPCODE_CONT:
5116 case TGSI_OPCODE_BRK:
5117 /* End of a basic block, clear the write array entirely.
5118 *
5119 * This keeps us from killing dead code when the writes are
5120 * on either side of a loop, even when the register isn't touched
5121 * inside the loop. However, glsl_to_tgsi_visitor doesn't seem to emit
5122 * dead code of this type, so it shouldn't make a difference as long as
5123 * the dead code elimination pass in the GLSL compiler does its job.
5124 */
5125 memset(writes, 0, sizeof(*writes) * this->next_temp * 4);
5126 break;
5127
5128 case TGSI_OPCODE_ENDIF:
5129 case TGSI_OPCODE_ELSE:
5130 /* Promote the recorded level of all channels written inside the
5131 * preceding if or else block to the level above the if/else block.
5132 */
5133 for (int r = 0; r < this->next_temp; r++) {
5134 for (int c = 0; c < 4; c++) {
5135 if (!writes[4 * r + c])
5136 continue;
5137
5138 if (write_level[4 * r + c] == level)
5139 write_level[4 * r + c] = level-1;
5140 }
5141 }
5142 if(inst->op == TGSI_OPCODE_ENDIF)
5143 --level;
5144 break;
5145
5146 case TGSI_OPCODE_IF:
5147 case TGSI_OPCODE_UIF:
5148 ++level;
5149 /* fallthrough to default case to mark the condition as read */
5150 default:
5151 /* Continuing the block, clear any channels from the write array that
5152 * are read by this instruction.
5153 */
5154 for (unsigned i = 0; i < ARRAY_SIZE(inst->src); i++) {
5155 if (inst->src[i].file == PROGRAM_TEMPORARY && inst->src[i].reladdr){
5156 /* Any temporary might be read, so no dead code elimination
5157 * across this instruction.
5158 */
5159 memset(writes, 0, sizeof(*writes) * this->next_temp * 4);
5160 } else if (inst->src[i].file == PROGRAM_TEMPORARY) {
5161 /* Clear where it's used as src. */
5162 int src_chans = 1 << GET_SWZ(inst->src[i].swizzle, 0);
5163 src_chans |= 1 << GET_SWZ(inst->src[i].swizzle, 1);
5164 src_chans |= 1 << GET_SWZ(inst->src[i].swizzle, 2);
5165 src_chans |= 1 << GET_SWZ(inst->src[i].swizzle, 3);
5166
5167 for (int c = 0; c < 4; c++) {
5168 if (src_chans & (1 << c))
5169 writes[4 * inst->src[i].index + c] = NULL;
5170 }
5171 }
5172 }
5173 for (unsigned i = 0; i < inst->tex_offset_num_offset; i++) {
5174 if (inst->tex_offsets[i].file == PROGRAM_TEMPORARY && inst->tex_offsets[i].reladdr){
5175 /* Any temporary might be read, so no dead code elimination
5176 * across this instruction.
5177 */
5178 memset(writes, 0, sizeof(*writes) * this->next_temp * 4);
5179 } else if (inst->tex_offsets[i].file == PROGRAM_TEMPORARY) {
5180 /* Clear where it's used as src. */
5181 int src_chans = 1 << GET_SWZ(inst->tex_offsets[i].swizzle, 0);
5182 src_chans |= 1 << GET_SWZ(inst->tex_offsets[i].swizzle, 1);
5183 src_chans |= 1 << GET_SWZ(inst->tex_offsets[i].swizzle, 2);
5184 src_chans |= 1 << GET_SWZ(inst->tex_offsets[i].swizzle, 3);
5185
5186 for (int c = 0; c < 4; c++) {
5187 if (src_chans & (1 << c))
5188 writes[4 * inst->tex_offsets[i].index + c] = NULL;
5189 }
5190 }
5191 }
5192 break;
5193 }
5194
5195 /* If this instruction writes to a temporary, add it to the write array.
5196 * If there is already an instruction in the write array for one or more
5197 * of the channels, flag that channel write as dead.
5198 */
5199 for (unsigned i = 0; i < ARRAY_SIZE(inst->dst); i++) {
5200 if (inst->dst[i].file == PROGRAM_TEMPORARY &&
5201 !inst->dst[i].reladdr) {
5202 for (int c = 0; c < 4; c++) {
5203 if (inst->dst[i].writemask & (1 << c)) {
5204 if (writes[4 * inst->dst[i].index + c]) {
5205 if (write_level[4 * inst->dst[i].index + c] < level)
5206 continue;
5207 else
5208 writes[4 * inst->dst[i].index + c]->dead_mask |= (1 << c);
5209 }
5210 writes[4 * inst->dst[i].index + c] = inst;
5211 write_level[4 * inst->dst[i].index + c] = level;
5212 }
5213 }
5214 }
5215 }
5216 }
5217
5218 /* Anything still in the write array at this point is dead code. */
5219 for (int r = 0; r < this->next_temp; r++) {
5220 for (int c = 0; c < 4; c++) {
5221 glsl_to_tgsi_instruction *inst = writes[4 * r + c];
5222 if (inst)
5223 inst->dead_mask |= (1 << c);
5224 }
5225 }
5226
5227 /* Now actually remove the instructions that are completely dead and update
5228 * the writemask of other instructions with dead channels.
5229 */
5230 foreach_in_list_safe(glsl_to_tgsi_instruction, inst, &this->instructions) {
5231 if (!inst->dead_mask || !inst->dst[0].writemask)
5232 continue;
5233 /* No amount of dead masks should remove memory stores */
5234 if (inst->info->is_store)
5235 continue;
5236
5237 if ((inst->dst[0].writemask & ~inst->dead_mask) == 0) {
5238 inst->remove();
5239 delete inst;
5240 removed++;
5241 } else {
5242 if (glsl_base_type_is_64bit(inst->dst[0].type)) {
5243 if (inst->dead_mask == WRITEMASK_XY ||
5244 inst->dead_mask == WRITEMASK_ZW)
5245 inst->dst[0].writemask &= ~(inst->dead_mask);
5246 } else
5247 inst->dst[0].writemask &= ~(inst->dead_mask);
5248 }
5249 }
5250
5251 ralloc_free(write_level);
5252 ralloc_free(writes);
5253
5254 return removed;
5255 }
5256
5257 /* merge DFRACEXP instructions into one. */
5258 void
5259 glsl_to_tgsi_visitor::merge_two_dsts(void)
5260 {
5261 foreach_in_list_safe(glsl_to_tgsi_instruction, inst, &this->instructions) {
5262 glsl_to_tgsi_instruction *inst2;
5263 bool merged;
5264 if (num_inst_dst_regs(inst) != 2)
5265 continue;
5266
5267 if (inst->dst[0].file != PROGRAM_UNDEFINED &&
5268 inst->dst[1].file != PROGRAM_UNDEFINED)
5269 continue;
5270
5271 inst2 = (glsl_to_tgsi_instruction *) inst->next;
5272 do {
5273
5274 if (inst->src[0].file == inst2->src[0].file &&
5275 inst->src[0].index == inst2->src[0].index &&
5276 inst->src[0].type == inst2->src[0].type &&
5277 inst->src[0].swizzle == inst2->src[0].swizzle)
5278 break;
5279 inst2 = (glsl_to_tgsi_instruction *) inst2->next;
5280 } while (inst2);
5281
5282 if (!inst2)
5283 continue;
5284 merged = false;
5285 if (inst->dst[0].file == PROGRAM_UNDEFINED) {
5286 merged = true;
5287 inst->dst[0] = inst2->dst[0];
5288 } else if (inst->dst[1].file == PROGRAM_UNDEFINED) {
5289 inst->dst[1] = inst2->dst[1];
5290 merged = true;
5291 }
5292
5293 if (merged) {
5294 inst2->remove();
5295 delete inst2;
5296 }
5297 }
5298 }
5299
5300 /* Merges temporary registers together where possible to reduce the number of
5301 * registers needed to run a program.
5302 *
5303 * Produces optimal code only after copy propagation and dead code elimination
5304 * have been run. */
5305 void
5306 glsl_to_tgsi_visitor::merge_registers(void)
5307 {
5308 int *last_reads = rzalloc_array(mem_ctx, int, this->next_temp);
5309 int *first_writes = rzalloc_array(mem_ctx, int, this->next_temp);
5310 struct rename_reg_pair *renames = rzalloc_array(mem_ctx, struct rename_reg_pair, this->next_temp);
5311 int i, j;
5312 int num_renames = 0;
5313
5314 /* Read the indices of the last read and first write to each temp register
5315 * into an array so that we don't have to traverse the instruction list as
5316 * much. */
5317 for (i = 0; i < this->next_temp; i++) {
5318 last_reads[i] = -1;
5319 first_writes[i] = -1;
5320 }
5321 get_last_temp_read_first_temp_write(last_reads, first_writes);
5322
5323 /* Start looking for registers with non-overlapping usages that can be
5324 * merged together. */
5325 for (i = 0; i < this->next_temp; i++) {
5326 /* Don't touch unused registers. */
5327 if (last_reads[i] < 0 || first_writes[i] < 0) continue;
5328
5329 for (j = 0; j < this->next_temp; j++) {
5330 /* Don't touch unused registers. */
5331 if (last_reads[j] < 0 || first_writes[j] < 0) continue;
5332
5333 /* We can merge the two registers if the first write to j is after or
5334 * in the same instruction as the last read from i. Note that the
5335 * register at index i will always be used earlier or at the same time
5336 * as the register at index j. */
5337 if (first_writes[i] <= first_writes[j] &&
5338 last_reads[i] <= first_writes[j]) {
5339 renames[num_renames].old_reg = j;
5340 renames[num_renames].new_reg = i;
5341 num_renames++;
5342
5343 /* Update the first_writes and last_reads arrays with the new
5344 * values for the merged register index, and mark the newly unused
5345 * register index as such. */
5346 assert(last_reads[j] >= last_reads[i]);
5347 last_reads[i] = last_reads[j];
5348 first_writes[j] = -1;
5349 last_reads[j] = -1;
5350 }
5351 }
5352 }
5353
5354 rename_temp_registers(num_renames, renames);
5355 ralloc_free(renames);
5356 ralloc_free(last_reads);
5357 ralloc_free(first_writes);
5358 }
5359
5360 /* Reassign indices to temporary registers by reusing unused indices created
5361 * by optimization passes. */
5362 void
5363 glsl_to_tgsi_visitor::renumber_registers(void)
5364 {
5365 int i = 0;
5366 int new_index = 0;
5367 int *first_reads = rzalloc_array(mem_ctx, int, this->next_temp);
5368 struct rename_reg_pair *renames = rzalloc_array(mem_ctx, struct rename_reg_pair, this->next_temp);
5369 int num_renames = 0;
5370 for (i = 0; i < this->next_temp; i++) {
5371 first_reads[i] = -1;
5372 }
5373 get_first_temp_read(first_reads);
5374
5375 for (i = 0; i < this->next_temp; i++) {
5376 if (first_reads[i] < 0) continue;
5377 if (i != new_index) {
5378 renames[num_renames].old_reg = i;
5379 renames[num_renames].new_reg = new_index;
5380 num_renames++;
5381 }
5382 new_index++;
5383 }
5384
5385 rename_temp_registers(num_renames, renames);
5386 this->next_temp = new_index;
5387 ralloc_free(renames);
5388 ralloc_free(first_reads);
5389 }
5390
5391 /* ------------------------- TGSI conversion stuff -------------------------- */
5392
5393 /**
5394 * Intermediate state used during shader translation.
5395 */
5396 struct st_translate {
5397 struct ureg_program *ureg;
5398
5399 unsigned temps_size;
5400 struct ureg_dst *temps;
5401
5402 struct ureg_dst *arrays;
5403 unsigned num_temp_arrays;
5404 struct ureg_src *constants;
5405 int num_constants;
5406 struct ureg_src *immediates;
5407 int num_immediates;
5408 struct ureg_dst outputs[PIPE_MAX_SHADER_OUTPUTS];
5409 struct ureg_src inputs[PIPE_MAX_SHADER_INPUTS];
5410 struct ureg_dst address[3];
5411 struct ureg_src samplers[PIPE_MAX_SAMPLERS];
5412 struct ureg_src buffers[PIPE_MAX_SHADER_BUFFERS];
5413 struct ureg_src images[PIPE_MAX_SHADER_IMAGES];
5414 struct ureg_src systemValues[SYSTEM_VALUE_MAX];
5415 struct ureg_src shared_memory;
5416 unsigned *array_sizes;
5417 struct inout_decl *input_decls;
5418 unsigned num_input_decls;
5419 struct inout_decl *output_decls;
5420 unsigned num_output_decls;
5421
5422 const GLuint *inputMapping;
5423 const GLuint *outputMapping;
5424
5425 unsigned procType; /**< PIPE_SHADER_VERTEX/FRAGMENT */
5426 };
5427
5428 /** Map Mesa's SYSTEM_VALUE_x to TGSI_SEMANTIC_x */
5429 unsigned
5430 _mesa_sysval_to_semantic(unsigned sysval)
5431 {
5432 switch (sysval) {
5433 /* Vertex shader */
5434 case SYSTEM_VALUE_VERTEX_ID:
5435 return TGSI_SEMANTIC_VERTEXID;
5436 case SYSTEM_VALUE_INSTANCE_ID:
5437 return TGSI_SEMANTIC_INSTANCEID;
5438 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE:
5439 return TGSI_SEMANTIC_VERTEXID_NOBASE;
5440 case SYSTEM_VALUE_BASE_VERTEX:
5441 return TGSI_SEMANTIC_BASEVERTEX;
5442 case SYSTEM_VALUE_BASE_INSTANCE:
5443 return TGSI_SEMANTIC_BASEINSTANCE;
5444 case SYSTEM_VALUE_DRAW_ID:
5445 return TGSI_SEMANTIC_DRAWID;
5446
5447 /* Geometry shader */
5448 case SYSTEM_VALUE_INVOCATION_ID:
5449 return TGSI_SEMANTIC_INVOCATIONID;
5450
5451 /* Fragment shader */
5452 case SYSTEM_VALUE_FRAG_COORD:
5453 return TGSI_SEMANTIC_POSITION;
5454 case SYSTEM_VALUE_FRONT_FACE:
5455 return TGSI_SEMANTIC_FACE;
5456 case SYSTEM_VALUE_SAMPLE_ID:
5457 return TGSI_SEMANTIC_SAMPLEID;
5458 case SYSTEM_VALUE_SAMPLE_POS:
5459 return TGSI_SEMANTIC_SAMPLEPOS;
5460 case SYSTEM_VALUE_SAMPLE_MASK_IN:
5461 return TGSI_SEMANTIC_SAMPLEMASK;
5462 case SYSTEM_VALUE_HELPER_INVOCATION:
5463 return TGSI_SEMANTIC_HELPER_INVOCATION;
5464
5465 /* Tessellation shader */
5466 case SYSTEM_VALUE_TESS_COORD:
5467 return TGSI_SEMANTIC_TESSCOORD;
5468 case SYSTEM_VALUE_VERTICES_IN:
5469 return TGSI_SEMANTIC_VERTICESIN;
5470 case SYSTEM_VALUE_PRIMITIVE_ID:
5471 return TGSI_SEMANTIC_PRIMID;
5472 case SYSTEM_VALUE_TESS_LEVEL_OUTER:
5473 return TGSI_SEMANTIC_TESSOUTER;
5474 case SYSTEM_VALUE_TESS_LEVEL_INNER:
5475 return TGSI_SEMANTIC_TESSINNER;
5476
5477 /* Compute shader */
5478 case SYSTEM_VALUE_LOCAL_INVOCATION_ID:
5479 return TGSI_SEMANTIC_THREAD_ID;
5480 case SYSTEM_VALUE_WORK_GROUP_ID:
5481 return TGSI_SEMANTIC_BLOCK_ID;
5482 case SYSTEM_VALUE_NUM_WORK_GROUPS:
5483 return TGSI_SEMANTIC_GRID_SIZE;
5484 case SYSTEM_VALUE_LOCAL_GROUP_SIZE:
5485 return TGSI_SEMANTIC_BLOCK_SIZE;
5486
5487 /* Unhandled */
5488 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX:
5489 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID:
5490 case SYSTEM_VALUE_VERTEX_CNT:
5491 default:
5492 assert(!"Unexpected SYSTEM_VALUE_ enum");
5493 return TGSI_SEMANTIC_COUNT;
5494 }
5495 }
5496
5497 /**
5498 * Map a glsl_to_tgsi constant/immediate to a TGSI immediate.
5499 */
5500 static struct ureg_src
5501 emit_immediate(struct st_translate *t,
5502 gl_constant_value values[4],
5503 int type, int size)
5504 {
5505 struct ureg_program *ureg = t->ureg;
5506
5507 switch(type)
5508 {
5509 case GL_FLOAT:
5510 return ureg_DECL_immediate(ureg, &values[0].f, size);
5511 case GL_DOUBLE:
5512 return ureg_DECL_immediate_f64(ureg, (double *)&values[0].f, size);
5513 case GL_INT64_ARB:
5514 return ureg_DECL_immediate_int64(ureg, (int64_t *)&values[0].f, size);
5515 case GL_UNSIGNED_INT64_ARB:
5516 return ureg_DECL_immediate_uint64(ureg, (uint64_t *)&values[0].f, size);
5517 case GL_INT:
5518 return ureg_DECL_immediate_int(ureg, &values[0].i, size);
5519 case GL_UNSIGNED_INT:
5520 case GL_BOOL:
5521 return ureg_DECL_immediate_uint(ureg, &values[0].u, size);
5522 default:
5523 assert(!"should not get here - type must be float, int, uint, or bool");
5524 return ureg_src_undef();
5525 }
5526 }
5527
5528 /**
5529 * Map a glsl_to_tgsi dst register to a TGSI ureg_dst register.
5530 */
5531 static struct ureg_dst
5532 dst_register(struct st_translate *t, gl_register_file file, unsigned index,
5533 unsigned array_id)
5534 {
5535 unsigned array;
5536
5537 switch(file) {
5538 case PROGRAM_UNDEFINED:
5539 return ureg_dst_undef();
5540
5541 case PROGRAM_TEMPORARY:
5542 /* Allocate space for temporaries on demand. */
5543 if (index >= t->temps_size) {
5544 const int inc = align(index - t->temps_size + 1, 4096);
5545
5546 t->temps = (struct ureg_dst*)
5547 realloc(t->temps,
5548 (t->temps_size + inc) * sizeof(struct ureg_dst));
5549 if (!t->temps)
5550 return ureg_dst_undef();
5551
5552 memset(t->temps + t->temps_size, 0, inc * sizeof(struct ureg_dst));
5553 t->temps_size += inc;
5554 }
5555
5556 if (ureg_dst_is_undef(t->temps[index]))
5557 t->temps[index] = ureg_DECL_local_temporary(t->ureg);
5558
5559 return t->temps[index];
5560
5561 case PROGRAM_ARRAY:
5562 assert(array_id && array_id <= t->num_temp_arrays);
5563 array = array_id - 1;
5564
5565 if (ureg_dst_is_undef(t->arrays[array]))
5566 t->arrays[array] = ureg_DECL_array_temporary(
5567 t->ureg, t->array_sizes[array], TRUE);
5568
5569 return ureg_dst_array_offset(t->arrays[array], index);
5570
5571 case PROGRAM_OUTPUT:
5572 if (!array_id) {
5573 if (t->procType == PIPE_SHADER_FRAGMENT)
5574 assert(index < 2 * FRAG_RESULT_MAX);
5575 else if (t->procType == PIPE_SHADER_TESS_CTRL ||
5576 t->procType == PIPE_SHADER_TESS_EVAL)
5577 assert(index < VARYING_SLOT_TESS_MAX);
5578 else
5579 assert(index < VARYING_SLOT_MAX);
5580
5581 assert(t->outputMapping[index] < ARRAY_SIZE(t->outputs));
5582 assert(t->outputs[t->outputMapping[index]].File != TGSI_FILE_NULL);
5583 return t->outputs[t->outputMapping[index]];
5584 }
5585 else {
5586 struct inout_decl *decl = find_inout_array(t->output_decls, t->num_output_decls, array_id);
5587 unsigned mesa_index = decl->mesa_index;
5588 int slot = t->outputMapping[mesa_index];
5589
5590 assert(slot != -1 && t->outputs[slot].File == TGSI_FILE_OUTPUT);
5591
5592 struct ureg_dst dst = t->outputs[slot];
5593 dst.ArrayID = array_id;
5594 return ureg_dst_array_offset(dst, index - mesa_index);
5595 }
5596
5597 case PROGRAM_ADDRESS:
5598 return t->address[index];
5599
5600 default:
5601 assert(!"unknown dst register file");
5602 return ureg_dst_undef();
5603 }
5604 }
5605
5606 /**
5607 * Map a glsl_to_tgsi src register to a TGSI ureg_src register.
5608 */
5609 static struct ureg_src
5610 src_register(struct st_translate *t, const st_src_reg *reg)
5611 {
5612 int index = reg->index;
5613 int double_reg2 = reg->double_reg2 ? 1 : 0;
5614
5615 switch(reg->file) {
5616 case PROGRAM_UNDEFINED:
5617 return ureg_imm4f(t->ureg, 0, 0, 0, 0);
5618
5619 case PROGRAM_TEMPORARY:
5620 case PROGRAM_ARRAY:
5621 return ureg_src(dst_register(t, reg->file, reg->index, reg->array_id));
5622
5623 case PROGRAM_OUTPUT: {
5624 struct ureg_dst dst = dst_register(t, reg->file, reg->index, reg->array_id);
5625 assert(dst.WriteMask != 0);
5626 unsigned shift = ffs(dst.WriteMask) - 1;
5627 return ureg_swizzle(ureg_src(dst),
5628 shift,
5629 MIN2(shift + 1, 3),
5630 MIN2(shift + 2, 3),
5631 MIN2(shift + 3, 3));
5632 }
5633
5634 case PROGRAM_UNIFORM:
5635 assert(reg->index >= 0);
5636 return reg->index < t->num_constants ?
5637 t->constants[reg->index] : ureg_imm4f(t->ureg, 0, 0, 0, 0);
5638 case PROGRAM_STATE_VAR:
5639 case PROGRAM_CONSTANT: /* ie, immediate */
5640 if (reg->has_index2)
5641 return ureg_src_register(TGSI_FILE_CONSTANT, reg->index);
5642 else
5643 return reg->index >= 0 && reg->index < t->num_constants ?
5644 t->constants[reg->index] : ureg_imm4f(t->ureg, 0, 0, 0, 0);
5645
5646 case PROGRAM_IMMEDIATE:
5647 assert(reg->index >= 0 && reg->index < t->num_immediates);
5648 return t->immediates[reg->index];
5649
5650 case PROGRAM_INPUT:
5651 /* GLSL inputs are 64-bit containers, so we have to
5652 * map back to the original index and add the offset after
5653 * mapping. */
5654 index -= double_reg2;
5655 if (!reg->array_id) {
5656 assert(t->inputMapping[index] < ARRAY_SIZE(t->inputs));
5657 assert(t->inputs[t->inputMapping[index]].File != TGSI_FILE_NULL);
5658 return t->inputs[t->inputMapping[index] + double_reg2];
5659 }
5660 else {
5661 struct inout_decl *decl = find_inout_array(t->input_decls, t->num_input_decls, reg->array_id);
5662 unsigned mesa_index = decl->mesa_index;
5663 int slot = t->inputMapping[mesa_index];
5664
5665 assert(slot != -1 && t->inputs[slot].File == TGSI_FILE_INPUT);
5666
5667 struct ureg_src src = t->inputs[slot];
5668 src.ArrayID = reg->array_id;
5669 return ureg_src_array_offset(src, index + double_reg2 - mesa_index);
5670 }
5671
5672 case PROGRAM_ADDRESS:
5673 return ureg_src(t->address[reg->index]);
5674
5675 case PROGRAM_SYSTEM_VALUE:
5676 assert(reg->index < (int) ARRAY_SIZE(t->systemValues));
5677 return t->systemValues[reg->index];
5678
5679 default:
5680 assert(!"unknown src register file");
5681 return ureg_src_undef();
5682 }
5683 }
5684
5685 /**
5686 * Create a TGSI ureg_dst register from an st_dst_reg.
5687 */
5688 static struct ureg_dst
5689 translate_dst(struct st_translate *t,
5690 const st_dst_reg *dst_reg,
5691 bool saturate)
5692 {
5693 struct ureg_dst dst = dst_register(t, dst_reg->file, dst_reg->index,
5694 dst_reg->array_id);
5695
5696 if (dst.File == TGSI_FILE_NULL)
5697 return dst;
5698
5699 dst = ureg_writemask(dst, dst_reg->writemask);
5700
5701 if (saturate)
5702 dst = ureg_saturate(dst);
5703
5704 if (dst_reg->reladdr != NULL) {
5705 assert(dst_reg->file != PROGRAM_TEMPORARY);
5706 dst = ureg_dst_indirect(dst, ureg_src(t->address[0]));
5707 }
5708
5709 if (dst_reg->has_index2) {
5710 if (dst_reg->reladdr2)
5711 dst = ureg_dst_dimension_indirect(dst, ureg_src(t->address[1]),
5712 dst_reg->index2D);
5713 else
5714 dst = ureg_dst_dimension(dst, dst_reg->index2D);
5715 }
5716
5717 return dst;
5718 }
5719
5720 /**
5721 * Create a TGSI ureg_src register from an st_src_reg.
5722 */
5723 static struct ureg_src
5724 translate_src(struct st_translate *t, const st_src_reg *src_reg)
5725 {
5726 struct ureg_src src = src_register(t, src_reg);
5727
5728 if (src_reg->has_index2) {
5729 /* 2D indexes occur with geometry shader inputs (attrib, vertex)
5730 * and UBO constant buffers (buffer, position).
5731 */
5732 if (src_reg->reladdr2)
5733 src = ureg_src_dimension_indirect(src, ureg_src(t->address[1]),
5734 src_reg->index2D);
5735 else
5736 src = ureg_src_dimension(src, src_reg->index2D);
5737 }
5738
5739 src = ureg_swizzle(src,
5740 GET_SWZ(src_reg->swizzle, 0) & 0x3,
5741 GET_SWZ(src_reg->swizzle, 1) & 0x3,
5742 GET_SWZ(src_reg->swizzle, 2) & 0x3,
5743 GET_SWZ(src_reg->swizzle, 3) & 0x3);
5744
5745 if (src_reg->abs)
5746 src = ureg_abs(src);
5747
5748 if ((src_reg->negate & 0xf) == NEGATE_XYZW)
5749 src = ureg_negate(src);
5750
5751 if (src_reg->reladdr != NULL) {
5752 assert(src_reg->file != PROGRAM_TEMPORARY);
5753 src = ureg_src_indirect(src, ureg_src(t->address[0]));
5754 }
5755
5756 return src;
5757 }
5758
5759 static struct tgsi_texture_offset
5760 translate_tex_offset(struct st_translate *t,
5761 const st_src_reg *in_offset)
5762 {
5763 struct tgsi_texture_offset offset;
5764 struct ureg_src src = translate_src(t, in_offset);
5765
5766 offset.File = src.File;
5767 offset.Index = src.Index;
5768 offset.SwizzleX = src.SwizzleX;
5769 offset.SwizzleY = src.SwizzleY;
5770 offset.SwizzleZ = src.SwizzleZ;
5771 offset.Padding = 0;
5772
5773 assert(!src.Indirect);
5774 assert(!src.DimIndirect);
5775 assert(!src.Dimension);
5776 assert(!src.Absolute); /* those shouldn't be used with integers anyway */
5777 assert(!src.Negate);
5778
5779 return offset;
5780 }
5781
5782 static void
5783 compile_tgsi_instruction(struct st_translate *t,
5784 const glsl_to_tgsi_instruction *inst)
5785 {
5786 struct ureg_program *ureg = t->ureg;
5787 int i;
5788 struct ureg_dst dst[2];
5789 struct ureg_src src[4];
5790 struct tgsi_texture_offset texoffsets[MAX_GLSL_TEXTURE_OFFSET];
5791
5792 int num_dst;
5793 int num_src;
5794 unsigned tex_target = 0;
5795
5796 num_dst = num_inst_dst_regs(inst);
5797 num_src = num_inst_src_regs(inst);
5798
5799 for (i = 0; i < num_dst; i++)
5800 dst[i] = translate_dst(t,
5801 &inst->dst[i],
5802 inst->saturate);
5803
5804 for (i = 0; i < num_src; i++)
5805 src[i] = translate_src(t, &inst->src[i]);
5806
5807 switch(inst->op) {
5808 case TGSI_OPCODE_BGNLOOP:
5809 case TGSI_OPCODE_ELSE:
5810 case TGSI_OPCODE_ENDLOOP:
5811 case TGSI_OPCODE_IF:
5812 case TGSI_OPCODE_UIF:
5813 assert(num_dst == 0);
5814 ureg_insn(ureg, inst->op, NULL, 0, src, num_src);
5815 return;
5816
5817 case TGSI_OPCODE_TEX:
5818 case TGSI_OPCODE_TEX_LZ:
5819 case TGSI_OPCODE_TXB:
5820 case TGSI_OPCODE_TXD:
5821 case TGSI_OPCODE_TXL:
5822 case TGSI_OPCODE_TXP:
5823 case TGSI_OPCODE_TXQ:
5824 case TGSI_OPCODE_TXQS:
5825 case TGSI_OPCODE_TXF:
5826 case TGSI_OPCODE_TXF_LZ:
5827 case TGSI_OPCODE_TEX2:
5828 case TGSI_OPCODE_TXB2:
5829 case TGSI_OPCODE_TXL2:
5830 case TGSI_OPCODE_TG4:
5831 case TGSI_OPCODE_LODQ:
5832 src[num_src] = t->samplers[inst->resource.index];
5833 assert(src[num_src].File != TGSI_FILE_NULL);
5834 if (inst->resource.reladdr)
5835 src[num_src] =
5836 ureg_src_indirect(src[num_src], ureg_src(t->address[2]));
5837 num_src++;
5838 for (i = 0; i < (int)inst->tex_offset_num_offset; i++) {
5839 texoffsets[i] = translate_tex_offset(t, &inst->tex_offsets[i]);
5840 }
5841 tex_target = st_translate_texture_target(inst->tex_target, inst->tex_shadow);
5842
5843 ureg_tex_insn(ureg,
5844 inst->op,
5845 dst, num_dst,
5846 tex_target,
5847 texoffsets, inst->tex_offset_num_offset,
5848 src, num_src);
5849 return;
5850
5851 case TGSI_OPCODE_RESQ:
5852 case TGSI_OPCODE_LOAD:
5853 case TGSI_OPCODE_ATOMUADD:
5854 case TGSI_OPCODE_ATOMXCHG:
5855 case TGSI_OPCODE_ATOMCAS:
5856 case TGSI_OPCODE_ATOMAND:
5857 case TGSI_OPCODE_ATOMOR:
5858 case TGSI_OPCODE_ATOMXOR:
5859 case TGSI_OPCODE_ATOMUMIN:
5860 case TGSI_OPCODE_ATOMUMAX:
5861 case TGSI_OPCODE_ATOMIMIN:
5862 case TGSI_OPCODE_ATOMIMAX:
5863 for (i = num_src - 1; i >= 0; i--)
5864 src[i + 1] = src[i];
5865 num_src++;
5866 if (inst->resource.file == PROGRAM_MEMORY) {
5867 src[0] = t->shared_memory;
5868 } else if (inst->resource.file == PROGRAM_BUFFER) {
5869 src[0] = t->buffers[inst->resource.index];
5870 } else {
5871 src[0] = t->images[inst->resource.index];
5872 tex_target = st_translate_texture_target(inst->tex_target, inst->tex_shadow);
5873 }
5874 if (inst->resource.reladdr)
5875 src[0] = ureg_src_indirect(src[0], ureg_src(t->address[2]));
5876 assert(src[0].File != TGSI_FILE_NULL);
5877 ureg_memory_insn(ureg, inst->op, dst, num_dst, src, num_src,
5878 inst->buffer_access,
5879 tex_target, inst->image_format);
5880 break;
5881
5882 case TGSI_OPCODE_STORE:
5883 if (inst->resource.file == PROGRAM_MEMORY) {
5884 dst[0] = ureg_dst(t->shared_memory);
5885 } else if (inst->resource.file == PROGRAM_BUFFER) {
5886 dst[0] = ureg_dst(t->buffers[inst->resource.index]);
5887 } else {
5888 dst[0] = ureg_dst(t->images[inst->resource.index]);
5889 tex_target = st_translate_texture_target(inst->tex_target, inst->tex_shadow);
5890 }
5891 dst[0] = ureg_writemask(dst[0], inst->dst[0].writemask);
5892 if (inst->resource.reladdr)
5893 dst[0] = ureg_dst_indirect(dst[0], ureg_src(t->address[2]));
5894 assert(dst[0].File != TGSI_FILE_NULL);
5895 ureg_memory_insn(ureg, inst->op, dst, num_dst, src, num_src,
5896 inst->buffer_access,
5897 tex_target, inst->image_format);
5898 break;
5899
5900 case TGSI_OPCODE_SCS:
5901 dst[0] = ureg_writemask(dst[0], TGSI_WRITEMASK_XY);
5902 ureg_insn(ureg, inst->op, dst, num_dst, src, num_src);
5903 break;
5904
5905 default:
5906 ureg_insn(ureg,
5907 inst->op,
5908 dst, num_dst,
5909 src, num_src);
5910 break;
5911 }
5912 }
5913
5914 /**
5915 * Emit the TGSI instructions for inverting and adjusting WPOS.
5916 * This code is unavoidable because it also depends on whether
5917 * a FBO is bound (STATE_FB_WPOS_Y_TRANSFORM).
5918 */
5919 static void
5920 emit_wpos_adjustment(struct gl_context *ctx,
5921 struct st_translate *t,
5922 int wpos_transform_const,
5923 boolean invert,
5924 GLfloat adjX, GLfloat adjY[2])
5925 {
5926 struct ureg_program *ureg = t->ureg;
5927
5928 assert(wpos_transform_const >= 0);
5929
5930 /* Fragment program uses fragment position input.
5931 * Need to replace instances of INPUT[WPOS] with temp T
5932 * where T = INPUT[WPOS] is inverted by Y.
5933 */
5934 struct ureg_src wpostrans = ureg_DECL_constant(ureg, wpos_transform_const);
5935 struct ureg_dst wpos_temp = ureg_DECL_temporary( ureg );
5936 struct ureg_src *wpos =
5937 ctx->Const.GLSLFragCoordIsSysVal ?
5938 &t->systemValues[SYSTEM_VALUE_FRAG_COORD] :
5939 &t->inputs[t->inputMapping[VARYING_SLOT_POS]];
5940 struct ureg_src wpos_input = *wpos;
5941
5942 /* First, apply the coordinate shift: */
5943 if (adjX || adjY[0] || adjY[1]) {
5944 if (adjY[0] != adjY[1]) {
5945 /* Adjust the y coordinate by adjY[1] or adjY[0] respectively
5946 * depending on whether inversion is actually going to be applied
5947 * or not, which is determined by testing against the inversion
5948 * state variable used below, which will be either +1 or -1.
5949 */
5950 struct ureg_dst adj_temp = ureg_DECL_local_temporary(ureg);
5951
5952 ureg_CMP(ureg, adj_temp,
5953 ureg_scalar(wpostrans, invert ? 2 : 0),
5954 ureg_imm4f(ureg, adjX, adjY[0], 0.0f, 0.0f),
5955 ureg_imm4f(ureg, adjX, adjY[1], 0.0f, 0.0f));
5956 ureg_ADD(ureg, wpos_temp, wpos_input, ureg_src(adj_temp));
5957 } else {
5958 ureg_ADD(ureg, wpos_temp, wpos_input,
5959 ureg_imm4f(ureg, adjX, adjY[0], 0.0f, 0.0f));
5960 }
5961 wpos_input = ureg_src(wpos_temp);
5962 } else {
5963 /* MOV wpos_temp, input[wpos]
5964 */
5965 ureg_MOV( ureg, wpos_temp, wpos_input );
5966 }
5967
5968 /* Now the conditional y flip: STATE_FB_WPOS_Y_TRANSFORM.xy/zw will be
5969 * inversion/identity, or the other way around if we're drawing to an FBO.
5970 */
5971 if (invert) {
5972 /* MAD wpos_temp.y, wpos_input, wpostrans.xxxx, wpostrans.yyyy
5973 */
5974 ureg_MAD( ureg,
5975 ureg_writemask(wpos_temp, TGSI_WRITEMASK_Y ),
5976 wpos_input,
5977 ureg_scalar(wpostrans, 0),
5978 ureg_scalar(wpostrans, 1));
5979 } else {
5980 /* MAD wpos_temp.y, wpos_input, wpostrans.zzzz, wpostrans.wwww
5981 */
5982 ureg_MAD( ureg,
5983 ureg_writemask(wpos_temp, TGSI_WRITEMASK_Y ),
5984 wpos_input,
5985 ureg_scalar(wpostrans, 2),
5986 ureg_scalar(wpostrans, 3));
5987 }
5988
5989 /* Use wpos_temp as position input from here on:
5990 */
5991 *wpos = ureg_src(wpos_temp);
5992 }
5993
5994
5995 /**
5996 * Emit fragment position/ooordinate code.
5997 */
5998 static void
5999 emit_wpos(struct st_context *st,
6000 struct st_translate *t,
6001 const struct gl_program *program,
6002 struct ureg_program *ureg,
6003 int wpos_transform_const)
6004 {
6005 struct pipe_screen *pscreen = st->pipe->screen;
6006 GLfloat adjX = 0.0f;
6007 GLfloat adjY[2] = { 0.0f, 0.0f };
6008 boolean invert = FALSE;
6009
6010 /* Query the pixel center conventions supported by the pipe driver and set
6011 * adjX, adjY to help out if it cannot handle the requested one internally.
6012 *
6013 * The bias of the y-coordinate depends on whether y-inversion takes place
6014 * (adjY[1]) or not (adjY[0]), which is in turn dependent on whether we are
6015 * drawing to an FBO (causes additional inversion), and whether the pipe
6016 * driver origin and the requested origin differ (the latter condition is
6017 * stored in the 'invert' variable).
6018 *
6019 * For height = 100 (i = integer, h = half-integer, l = lower, u = upper):
6020 *
6021 * center shift only:
6022 * i -> h: +0.5
6023 * h -> i: -0.5
6024 *
6025 * inversion only:
6026 * l,i -> u,i: ( 0.0 + 1.0) * -1 + 100 = 99
6027 * l,h -> u,h: ( 0.5 + 0.0) * -1 + 100 = 99.5
6028 * u,i -> l,i: (99.0 + 1.0) * -1 + 100 = 0
6029 * u,h -> l,h: (99.5 + 0.0) * -1 + 100 = 0.5
6030 *
6031 * inversion and center shift:
6032 * l,i -> u,h: ( 0.0 + 0.5) * -1 + 100 = 99.5
6033 * l,h -> u,i: ( 0.5 + 0.5) * -1 + 100 = 99
6034 * u,i -> l,h: (99.0 + 0.5) * -1 + 100 = 0.5
6035 * u,h -> l,i: (99.5 + 0.5) * -1 + 100 = 0
6036 */
6037 if (program->OriginUpperLeft) {
6038 /* Fragment shader wants origin in upper-left */
6039 if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT)) {
6040 /* the driver supports upper-left origin */
6041 }
6042 else if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT)) {
6043 /* the driver supports lower-left origin, need to invert Y */
6044 ureg_property(ureg, TGSI_PROPERTY_FS_COORD_ORIGIN,
6045 TGSI_FS_COORD_ORIGIN_LOWER_LEFT);
6046 invert = TRUE;
6047 }
6048 else
6049 assert(0);
6050 }
6051 else {
6052 /* Fragment shader wants origin in lower-left */
6053 if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT))
6054 /* the driver supports lower-left origin */
6055 ureg_property(ureg, TGSI_PROPERTY_FS_COORD_ORIGIN,
6056 TGSI_FS_COORD_ORIGIN_LOWER_LEFT);
6057 else if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT))
6058 /* the driver supports upper-left origin, need to invert Y */
6059 invert = TRUE;
6060 else
6061 assert(0);
6062 }
6063
6064 if (program->PixelCenterInteger) {
6065 /* Fragment shader wants pixel center integer */
6066 if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER)) {
6067 /* the driver supports pixel center integer */
6068 adjY[1] = 1.0f;
6069 ureg_property(ureg, TGSI_PROPERTY_FS_COORD_PIXEL_CENTER,
6070 TGSI_FS_COORD_PIXEL_CENTER_INTEGER);
6071 }
6072 else if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER)) {
6073 /* the driver supports pixel center half integer, need to bias X,Y */
6074 adjX = -0.5f;
6075 adjY[0] = -0.5f;
6076 adjY[1] = 0.5f;
6077 }
6078 else
6079 assert(0);
6080 }
6081 else {
6082 /* Fragment shader wants pixel center half integer */
6083 if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER)) {
6084 /* the driver supports pixel center half integer */
6085 }
6086 else if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER)) {
6087 /* the driver supports pixel center integer, need to bias X,Y */
6088 adjX = adjY[0] = adjY[1] = 0.5f;
6089 ureg_property(ureg, TGSI_PROPERTY_FS_COORD_PIXEL_CENTER,
6090 TGSI_FS_COORD_PIXEL_CENTER_INTEGER);
6091 }
6092 else
6093 assert(0);
6094 }
6095
6096 /* we invert after adjustment so that we avoid the MOV to temporary,
6097 * and reuse the adjustment ADD instead */
6098 emit_wpos_adjustment(st->ctx, t, wpos_transform_const, invert, adjX, adjY);
6099 }
6100
6101 /**
6102 * OpenGL's fragment gl_FrontFace input is 1 for front-facing, 0 for back.
6103 * TGSI uses +1 for front, -1 for back.
6104 * This function converts the TGSI value to the GL value. Simply clamping/
6105 * saturating the value to [0,1] does the job.
6106 */
6107 static void
6108 emit_face_var(struct gl_context *ctx, struct st_translate *t)
6109 {
6110 struct ureg_program *ureg = t->ureg;
6111 struct ureg_dst face_temp = ureg_DECL_temporary(ureg);
6112 struct ureg_src face_input = t->inputs[t->inputMapping[VARYING_SLOT_FACE]];
6113
6114 if (ctx->Const.NativeIntegers) {
6115 ureg_FSGE(ureg, face_temp, face_input, ureg_imm1f(ureg, 0));
6116 }
6117 else {
6118 /* MOV_SAT face_temp, input[face] */
6119 ureg_MOV(ureg, ureg_saturate(face_temp), face_input);
6120 }
6121
6122 /* Use face_temp as face input from here on: */
6123 t->inputs[t->inputMapping[VARYING_SLOT_FACE]] = ureg_src(face_temp);
6124 }
6125
6126 static void
6127 emit_compute_block_size(const struct gl_program *prog,
6128 struct ureg_program *ureg) {
6129 ureg_property(ureg, TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH,
6130 prog->info.cs.local_size[0]);
6131 ureg_property(ureg, TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT,
6132 prog->info.cs.local_size[1]);
6133 ureg_property(ureg, TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH,
6134 prog->info.cs.local_size[2]);
6135 }
6136
6137 struct sort_inout_decls {
6138 bool operator()(const struct inout_decl &a, const struct inout_decl &b) const {
6139 return mapping[a.mesa_index] < mapping[b.mesa_index];
6140 }
6141
6142 const GLuint *mapping;
6143 };
6144
6145 /* Sort the given array of decls by the corresponding slot (TGSI file index).
6146 *
6147 * This is for the benefit of older drivers which are broken when the
6148 * declarations aren't sorted in this way.
6149 */
6150 static void
6151 sort_inout_decls_by_slot(struct inout_decl *decls,
6152 unsigned count,
6153 const GLuint mapping[])
6154 {
6155 sort_inout_decls sorter;
6156 sorter.mapping = mapping;
6157 std::sort(decls, decls + count, sorter);
6158 }
6159
6160 static unsigned
6161 st_translate_interp(enum glsl_interp_mode glsl_qual, GLuint varying)
6162 {
6163 switch (glsl_qual) {
6164 case INTERP_MODE_NONE:
6165 if (varying == VARYING_SLOT_COL0 || varying == VARYING_SLOT_COL1)
6166 return TGSI_INTERPOLATE_COLOR;
6167 return TGSI_INTERPOLATE_PERSPECTIVE;
6168 case INTERP_MODE_SMOOTH:
6169 return TGSI_INTERPOLATE_PERSPECTIVE;
6170 case INTERP_MODE_FLAT:
6171 return TGSI_INTERPOLATE_CONSTANT;
6172 case INTERP_MODE_NOPERSPECTIVE:
6173 return TGSI_INTERPOLATE_LINEAR;
6174 default:
6175 assert(0 && "unexpected interp mode in st_translate_interp()");
6176 return TGSI_INTERPOLATE_PERSPECTIVE;
6177 }
6178 }
6179
6180 /**
6181 * Translate intermediate IR (glsl_to_tgsi_instruction) to TGSI format.
6182 * \param program the program to translate
6183 * \param numInputs number of input registers used
6184 * \param inputMapping maps Mesa fragment program inputs to TGSI generic
6185 * input indexes
6186 * \param inputSemanticName the TGSI_SEMANTIC flag for each input
6187 * \param inputSemanticIndex the semantic index (ex: which texcoord) for
6188 * each input
6189 * \param interpMode the TGSI_INTERPOLATE_LINEAR/PERSP mode for each input
6190 * \param numOutputs number of output registers used
6191 * \param outputMapping maps Mesa fragment program outputs to TGSI
6192 * generic outputs
6193 * \param outputSemanticName the TGSI_SEMANTIC flag for each output
6194 * \param outputSemanticIndex the semantic index (ex: which texcoord) for
6195 * each output
6196 *
6197 * \return PIPE_OK or PIPE_ERROR_OUT_OF_MEMORY
6198 */
6199 extern "C" enum pipe_error
6200 st_translate_program(
6201 struct gl_context *ctx,
6202 uint procType,
6203 struct ureg_program *ureg,
6204 glsl_to_tgsi_visitor *program,
6205 const struct gl_program *proginfo,
6206 GLuint numInputs,
6207 const GLuint inputMapping[],
6208 const GLuint inputSlotToAttr[],
6209 const ubyte inputSemanticName[],
6210 const ubyte inputSemanticIndex[],
6211 const GLuint interpMode[],
6212 GLuint numOutputs,
6213 const GLuint outputMapping[],
6214 const GLuint outputSlotToAttr[],
6215 const ubyte outputSemanticName[],
6216 const ubyte outputSemanticIndex[])
6217 {
6218 struct st_translate *t;
6219 unsigned i;
6220 struct gl_program_constants *frag_const =
6221 &ctx->Const.Program[MESA_SHADER_FRAGMENT];
6222 enum pipe_error ret = PIPE_OK;
6223
6224 assert(numInputs <= ARRAY_SIZE(t->inputs));
6225 assert(numOutputs <= ARRAY_SIZE(t->outputs));
6226
6227 t = CALLOC_STRUCT(st_translate);
6228 if (!t) {
6229 ret = PIPE_ERROR_OUT_OF_MEMORY;
6230 goto out;
6231 }
6232
6233 t->procType = procType;
6234 t->inputMapping = inputMapping;
6235 t->outputMapping = outputMapping;
6236 t->ureg = ureg;
6237 t->num_temp_arrays = program->next_array;
6238 if (t->num_temp_arrays)
6239 t->arrays = (struct ureg_dst*)
6240 calloc(t->num_temp_arrays, sizeof(t->arrays[0]));
6241
6242 /*
6243 * Declare input attributes.
6244 */
6245 switch (procType) {
6246 case PIPE_SHADER_FRAGMENT:
6247 case PIPE_SHADER_GEOMETRY:
6248 case PIPE_SHADER_TESS_EVAL:
6249 case PIPE_SHADER_TESS_CTRL:
6250 sort_inout_decls_by_slot(program->inputs, program->num_inputs, inputMapping);
6251
6252 for (i = 0; i < program->num_inputs; ++i) {
6253 struct inout_decl *decl = &program->inputs[i];
6254 unsigned slot = inputMapping[decl->mesa_index];
6255 struct ureg_src src;
6256 ubyte tgsi_usage_mask = decl->usage_mask;
6257
6258 if (glsl_base_type_is_64bit(decl->base_type)) {
6259 if (tgsi_usage_mask == 1)
6260 tgsi_usage_mask = TGSI_WRITEMASK_XY;
6261 else if (tgsi_usage_mask == 2)
6262 tgsi_usage_mask = TGSI_WRITEMASK_ZW;
6263 else
6264 tgsi_usage_mask = TGSI_WRITEMASK_XYZW;
6265 }
6266
6267 unsigned interp_mode = 0;
6268 unsigned interp_location = 0;
6269 if (procType == PIPE_SHADER_FRAGMENT) {
6270 assert(interpMode);
6271 interp_mode = interpMode[slot] != TGSI_INTERPOLATE_COUNT ?
6272 interpMode[slot] :
6273 st_translate_interp(decl->interp, inputSlotToAttr[slot]);
6274
6275 interp_location = decl->interp_loc;
6276 }
6277
6278 src = ureg_DECL_fs_input_cyl_centroid_layout(ureg,
6279 inputSemanticName[slot], inputSemanticIndex[slot],
6280 interp_mode, 0, interp_location, slot, tgsi_usage_mask,
6281 decl->array_id, decl->size);
6282
6283 for (unsigned j = 0; j < decl->size; ++j) {
6284 if (t->inputs[slot + j].File != TGSI_FILE_INPUT) {
6285 /* The ArrayID is set up in dst_register */
6286 t->inputs[slot + j] = src;
6287 t->inputs[slot + j].ArrayID = 0;
6288 t->inputs[slot + j].Index += j;
6289 }
6290 }
6291 }
6292 break;
6293 case PIPE_SHADER_VERTEX:
6294 for (i = 0; i < numInputs; i++) {
6295 t->inputs[i] = ureg_DECL_vs_input(ureg, i);
6296 }
6297 break;
6298 case PIPE_SHADER_COMPUTE:
6299 break;
6300 default:
6301 assert(0);
6302 }
6303
6304 /*
6305 * Declare output attributes.
6306 */
6307 switch (procType) {
6308 case PIPE_SHADER_FRAGMENT:
6309 case PIPE_SHADER_COMPUTE:
6310 break;
6311 case PIPE_SHADER_GEOMETRY:
6312 case PIPE_SHADER_TESS_EVAL:
6313 case PIPE_SHADER_TESS_CTRL:
6314 case PIPE_SHADER_VERTEX:
6315 sort_inout_decls_by_slot(program->outputs, program->num_outputs, outputMapping);
6316
6317 for (i = 0; i < program->num_outputs; ++i) {
6318 struct inout_decl *decl = &program->outputs[i];
6319 unsigned slot = outputMapping[decl->mesa_index];
6320 struct ureg_dst dst;
6321 ubyte tgsi_usage_mask = decl->usage_mask;
6322
6323 if (glsl_base_type_is_64bit(decl->base_type)) {
6324 if (tgsi_usage_mask == 1)
6325 tgsi_usage_mask = TGSI_WRITEMASK_XY;
6326 else if (tgsi_usage_mask == 2)
6327 tgsi_usage_mask = TGSI_WRITEMASK_ZW;
6328 else
6329 tgsi_usage_mask = TGSI_WRITEMASK_XYZW;
6330 }
6331
6332 dst = ureg_DECL_output_layout(ureg,
6333 outputSemanticName[slot], outputSemanticIndex[slot],
6334 decl->gs_out_streams,
6335 slot, tgsi_usage_mask, decl->array_id, decl->size);
6336
6337 for (unsigned j = 0; j < decl->size; ++j) {
6338 if (t->outputs[slot + j].File != TGSI_FILE_OUTPUT) {
6339 /* The ArrayID is set up in dst_register */
6340 t->outputs[slot + j] = dst;
6341 t->outputs[slot + j].ArrayID = 0;
6342 t->outputs[slot + j].Index += j;
6343 }
6344 }
6345 }
6346 break;
6347 default:
6348 assert(0);
6349 }
6350
6351 if (procType == PIPE_SHADER_FRAGMENT) {
6352 if (program->shader->Program->info.fs.early_fragment_tests)
6353 ureg_property(ureg, TGSI_PROPERTY_FS_EARLY_DEPTH_STENCIL, 1);
6354
6355 if (proginfo->info.inputs_read & VARYING_BIT_POS) {
6356 /* Must do this after setting up t->inputs. */
6357 emit_wpos(st_context(ctx), t, proginfo, ureg,
6358 program->wpos_transform_const);
6359 }
6360
6361 if (proginfo->info.inputs_read & VARYING_BIT_FACE)
6362 emit_face_var(ctx, t);
6363
6364 for (i = 0; i < numOutputs; i++) {
6365 switch (outputSemanticName[i]) {
6366 case TGSI_SEMANTIC_POSITION:
6367 t->outputs[i] = ureg_DECL_output(ureg,
6368 TGSI_SEMANTIC_POSITION, /* Z/Depth */
6369 outputSemanticIndex[i]);
6370 t->outputs[i] = ureg_writemask(t->outputs[i], TGSI_WRITEMASK_Z);
6371 break;
6372 case TGSI_SEMANTIC_STENCIL:
6373 t->outputs[i] = ureg_DECL_output(ureg,
6374 TGSI_SEMANTIC_STENCIL, /* Stencil */
6375 outputSemanticIndex[i]);
6376 t->outputs[i] = ureg_writemask(t->outputs[i], TGSI_WRITEMASK_Y);
6377 break;
6378 case TGSI_SEMANTIC_COLOR:
6379 t->outputs[i] = ureg_DECL_output(ureg,
6380 TGSI_SEMANTIC_COLOR,
6381 outputSemanticIndex[i]);
6382 break;
6383 case TGSI_SEMANTIC_SAMPLEMASK:
6384 t->outputs[i] = ureg_DECL_output(ureg,
6385 TGSI_SEMANTIC_SAMPLEMASK,
6386 outputSemanticIndex[i]);
6387 /* TODO: If we ever support more than 32 samples, this will have
6388 * to become an array.
6389 */
6390 t->outputs[i] = ureg_writemask(t->outputs[i], TGSI_WRITEMASK_X);
6391 break;
6392 default:
6393 assert(!"fragment shader outputs must be POSITION/STENCIL/COLOR");
6394 ret = PIPE_ERROR_BAD_INPUT;
6395 goto out;
6396 }
6397 }
6398 }
6399 else if (procType == PIPE_SHADER_VERTEX) {
6400 for (i = 0; i < numOutputs; i++) {
6401 if (outputSemanticName[i] == TGSI_SEMANTIC_FOG) {
6402 /* force register to contain a fog coordinate in the form (F, 0, 0, 1). */
6403 ureg_MOV(ureg,
6404 ureg_writemask(t->outputs[i], TGSI_WRITEMASK_YZW),
6405 ureg_imm4f(ureg, 0.0f, 0.0f, 0.0f, 1.0f));
6406 t->outputs[i] = ureg_writemask(t->outputs[i], TGSI_WRITEMASK_X);
6407 }
6408 }
6409 }
6410
6411 if (procType == PIPE_SHADER_COMPUTE) {
6412 emit_compute_block_size(proginfo, ureg);
6413 }
6414
6415 /* Declare address register.
6416 */
6417 if (program->num_address_regs > 0) {
6418 assert(program->num_address_regs <= 3);
6419 for (int i = 0; i < program->num_address_regs; i++)
6420 t->address[i] = ureg_DECL_address(ureg);
6421 }
6422
6423 /* Declare misc input registers
6424 */
6425 {
6426 GLbitfield sysInputs = proginfo->info.system_values_read;
6427
6428 for (i = 0; sysInputs; i++) {
6429 if (sysInputs & (1 << i)) {
6430 unsigned semName = _mesa_sysval_to_semantic(i);
6431
6432 t->systemValues[i] = ureg_DECL_system_value(ureg, semName, 0);
6433
6434 if (semName == TGSI_SEMANTIC_INSTANCEID ||
6435 semName == TGSI_SEMANTIC_VERTEXID) {
6436 /* From Gallium perspective, these system values are always
6437 * integer, and require native integer support. However, if
6438 * native integer is supported on the vertex stage but not the
6439 * pixel stage (e.g, i915g + draw), Mesa will generate IR that
6440 * assumes these system values are floats. To resolve the
6441 * inconsistency, we insert a U2F.
6442 */
6443 struct st_context *st = st_context(ctx);
6444 struct pipe_screen *pscreen = st->pipe->screen;
6445 assert(procType == PIPE_SHADER_VERTEX);
6446 assert(pscreen->get_shader_param(pscreen, PIPE_SHADER_VERTEX, PIPE_SHADER_CAP_INTEGERS));
6447 (void) pscreen;
6448 if (!ctx->Const.NativeIntegers) {
6449 struct ureg_dst temp = ureg_DECL_local_temporary(t->ureg);
6450 ureg_U2F( t->ureg, ureg_writemask(temp, TGSI_WRITEMASK_X), t->systemValues[i]);
6451 t->systemValues[i] = ureg_scalar(ureg_src(temp), 0);
6452 }
6453 }
6454
6455 if (procType == PIPE_SHADER_FRAGMENT &&
6456 semName == TGSI_SEMANTIC_POSITION)
6457 emit_wpos(st_context(ctx), t, proginfo, ureg,
6458 program->wpos_transform_const);
6459
6460 sysInputs &= ~(1 << i);
6461 }
6462 }
6463 }
6464
6465 t->array_sizes = program->array_sizes;
6466 t->input_decls = program->inputs;
6467 t->num_input_decls = program->num_inputs;
6468 t->output_decls = program->outputs;
6469 t->num_output_decls = program->num_outputs;
6470
6471 /* Emit constants and uniforms. TGSI uses a single index space for these,
6472 * so we put all the translated regs in t->constants.
6473 */
6474 if (proginfo->Parameters) {
6475 t->constants = (struct ureg_src *)
6476 calloc(proginfo->Parameters->NumParameters, sizeof(t->constants[0]));
6477 if (t->constants == NULL) {
6478 ret = PIPE_ERROR_OUT_OF_MEMORY;
6479 goto out;
6480 }
6481 t->num_constants = proginfo->Parameters->NumParameters;
6482
6483 for (i = 0; i < proginfo->Parameters->NumParameters; i++) {
6484 switch (proginfo->Parameters->Parameters[i].Type) {
6485 case PROGRAM_STATE_VAR:
6486 case PROGRAM_UNIFORM:
6487 t->constants[i] = ureg_DECL_constant(ureg, i);
6488 break;
6489
6490 /* Emit immediates for PROGRAM_CONSTANT only when there's no indirect
6491 * addressing of the const buffer.
6492 * FIXME: Be smarter and recognize param arrays:
6493 * indirect addressing is only valid within the referenced
6494 * array.
6495 */
6496 case PROGRAM_CONSTANT:
6497 if (program->indirect_addr_consts)
6498 t->constants[i] = ureg_DECL_constant(ureg, i);
6499 else
6500 t->constants[i] = emit_immediate(t,
6501 proginfo->Parameters->ParameterValues[i],
6502 proginfo->Parameters->Parameters[i].DataType,
6503 4);
6504 break;
6505 default:
6506 break;
6507 }
6508 }
6509 }
6510
6511 for (i = 0; i < proginfo->info.num_ubos; i++) {
6512 unsigned size = proginfo->sh.UniformBlocks[i]->UniformBufferSize;
6513 unsigned num_const_vecs = (size + 15) / 16;
6514 unsigned first, last;
6515 assert(num_const_vecs > 0);
6516 first = 0;
6517 last = num_const_vecs > 0 ? num_const_vecs - 1 : 0;
6518 ureg_DECL_constant2D(t->ureg, first, last, i + 1);
6519 }
6520
6521 /* Emit immediate values.
6522 */
6523 t->immediates = (struct ureg_src *)
6524 calloc(program->num_immediates, sizeof(struct ureg_src));
6525 if (t->immediates == NULL) {
6526 ret = PIPE_ERROR_OUT_OF_MEMORY;
6527 goto out;
6528 }
6529 t->num_immediates = program->num_immediates;
6530
6531 i = 0;
6532 foreach_in_list(immediate_storage, imm, &program->immediates) {
6533 assert(i < program->num_immediates);
6534 t->immediates[i++] = emit_immediate(t, imm->values, imm->type, imm->size32);
6535 }
6536 assert(i == program->num_immediates);
6537
6538 /* texture samplers */
6539 for (i = 0; i < frag_const->MaxTextureImageUnits; i++) {
6540 if (program->samplers_used & (1u << i)) {
6541 unsigned type;
6542
6543 t->samplers[i] = ureg_DECL_sampler(ureg, i);
6544
6545 switch (program->sampler_types[i]) {
6546 case GLSL_TYPE_INT:
6547 type = TGSI_RETURN_TYPE_SINT;
6548 break;
6549 case GLSL_TYPE_UINT:
6550 type = TGSI_RETURN_TYPE_UINT;
6551 break;
6552 case GLSL_TYPE_FLOAT:
6553 type = TGSI_RETURN_TYPE_FLOAT;
6554 break;
6555 default:
6556 unreachable("not reached");
6557 }
6558
6559 ureg_DECL_sampler_view( ureg, i, program->sampler_targets[i],
6560 type, type, type, type );
6561 }
6562 }
6563
6564 for (i = 0; i < frag_const->MaxAtomicBuffers; i++) {
6565 if (program->buffers_used & (1 << i)) {
6566 t->buffers[i] = ureg_DECL_buffer(ureg, i, true);
6567 }
6568 }
6569
6570 for (; i < frag_const->MaxAtomicBuffers + frag_const->MaxShaderStorageBlocks;
6571 i++) {
6572 if (program->buffers_used & (1 << i)) {
6573 t->buffers[i] = ureg_DECL_buffer(ureg, i, false);
6574 }
6575 }
6576
6577 if (program->use_shared_memory)
6578 t->shared_memory = ureg_DECL_memory(ureg, TGSI_MEMORY_TYPE_SHARED);
6579
6580 for (i = 0; i < program->shader->Program->info.num_images; i++) {
6581 if (program->images_used & (1 << i)) {
6582 t->images[i] = ureg_DECL_image(ureg, i,
6583 program->image_targets[i],
6584 program->image_formats[i],
6585 true, false);
6586 }
6587 }
6588
6589 /* Emit each instruction in turn:
6590 */
6591 foreach_in_list(glsl_to_tgsi_instruction, inst, &program->instructions)
6592 compile_tgsi_instruction(t, inst);
6593
6594 /* Set the next shader stage hint for VS and TES. */
6595 switch (procType) {
6596 case PIPE_SHADER_VERTEX:
6597 case PIPE_SHADER_TESS_EVAL:
6598 if (program->shader_program->SeparateShader)
6599 break;
6600
6601 for (i = program->shader->Stage+1; i <= MESA_SHADER_FRAGMENT; i++) {
6602 if (program->shader_program->_LinkedShaders[i]) {
6603 unsigned next;
6604
6605 switch (i) {
6606 case MESA_SHADER_TESS_CTRL:
6607 next = PIPE_SHADER_TESS_CTRL;
6608 break;
6609 case MESA_SHADER_TESS_EVAL:
6610 next = PIPE_SHADER_TESS_EVAL;
6611 break;
6612 case MESA_SHADER_GEOMETRY:
6613 next = PIPE_SHADER_GEOMETRY;
6614 break;
6615 case MESA_SHADER_FRAGMENT:
6616 next = PIPE_SHADER_FRAGMENT;
6617 break;
6618 default:
6619 assert(0);
6620 continue;
6621 }
6622
6623 ureg_set_next_shader_processor(ureg, next);
6624 break;
6625 }
6626 }
6627 break;
6628 }
6629
6630 out:
6631 if (t) {
6632 free(t->arrays);
6633 free(t->temps);
6634 free(t->constants);
6635 t->num_constants = 0;
6636 free(t->immediates);
6637 t->num_immediates = 0;
6638 FREE(t);
6639 }
6640
6641 return ret;
6642 }
6643 /* ----------------------------- End TGSI code ------------------------------ */
6644
6645
6646 /**
6647 * Convert a shader's GLSL IR into a Mesa gl_program, although without
6648 * generating Mesa IR.
6649 */
6650 static struct gl_program *
6651 get_mesa_program_tgsi(struct gl_context *ctx,
6652 struct gl_shader_program *shader_program,
6653 struct gl_linked_shader *shader)
6654 {
6655 glsl_to_tgsi_visitor* v;
6656 struct gl_program *prog;
6657 struct gl_shader_compiler_options *options =
6658 &ctx->Const.ShaderCompilerOptions[shader->Stage];
6659 struct pipe_screen *pscreen = ctx->st->pipe->screen;
6660 enum pipe_shader_type ptarget = st_shader_stage_to_ptarget(shader->Stage);
6661
6662 validate_ir_tree(shader->ir);
6663
6664 prog = shader->Program;
6665
6666 prog->Parameters = _mesa_new_parameter_list();
6667 v = new glsl_to_tgsi_visitor();
6668 v->ctx = ctx;
6669 v->prog = prog;
6670 v->shader_program = shader_program;
6671 v->shader = shader;
6672 v->options = options;
6673 v->glsl_version = ctx->Const.GLSLVersion;
6674 v->native_integers = ctx->Const.NativeIntegers;
6675
6676 v->have_sqrt = pscreen->get_shader_param(pscreen, ptarget,
6677 PIPE_SHADER_CAP_TGSI_SQRT_SUPPORTED);
6678 v->have_fma = pscreen->get_shader_param(pscreen, ptarget,
6679 PIPE_SHADER_CAP_TGSI_FMA_SUPPORTED);
6680 v->has_tex_txf_lz = pscreen->get_param(pscreen,
6681 PIPE_CAP_TGSI_TEX_TXF_LZ);
6682
6683 _mesa_generate_parameters_list_for_uniforms(shader_program, shader,
6684 prog->Parameters);
6685
6686 /* Remove reads from output registers. */
6687 if (!pscreen->get_param(pscreen, PIPE_CAP_TGSI_CAN_READ_OUTPUTS))
6688 lower_output_reads(shader->Stage, shader->ir);
6689
6690 /* Emit intermediate IR for main(). */
6691 visit_exec_list(shader->ir, v);
6692
6693 #if 0
6694 /* Print out some information (for debugging purposes) used by the
6695 * optimization passes. */
6696 {
6697 int i;
6698 int *first_writes = rzalloc_array(v->mem_ctx, int, v->next_temp);
6699 int *first_reads = rzalloc_array(v->mem_ctx, int, v->next_temp);
6700 int *last_writes = rzalloc_array(v->mem_ctx, int, v->next_temp);
6701 int *last_reads = rzalloc_array(v->mem_ctx, int, v->next_temp);
6702
6703 for (i = 0; i < v->next_temp; i++) {
6704 first_writes[i] = -1;
6705 first_reads[i] = -1;
6706 last_writes[i] = -1;
6707 last_reads[i] = -1;
6708 }
6709 v->get_first_temp_read(first_reads);
6710 v->get_last_temp_read_first_temp_write(last_reads, first_writes);
6711 v->get_last_temp_write(last_writes);
6712 for (i = 0; i < v->next_temp; i++)
6713 printf("Temp %d: FR=%3d FW=%3d LR=%3d LW=%3d\n", i, first_reads[i],
6714 first_writes[i],
6715 last_reads[i],
6716 last_writes[i]);
6717 ralloc_free(first_writes);
6718 ralloc_free(first_reads);
6719 ralloc_free(last_writes);
6720 ralloc_free(last_reads);
6721 }
6722 #endif
6723
6724 /* Perform optimizations on the instructions in the glsl_to_tgsi_visitor. */
6725 v->simplify_cmp();
6726
6727 if (shader->Stage != MESA_SHADER_TESS_CTRL &&
6728 shader->Stage != MESA_SHADER_TESS_EVAL)
6729 v->copy_propagate();
6730
6731 while (v->eliminate_dead_code());
6732
6733 v->merge_two_dsts();
6734 v->merge_registers();
6735 v->renumber_registers();
6736
6737 /* Write the END instruction. */
6738 v->emit_asm(NULL, TGSI_OPCODE_END);
6739
6740 if (ctx->_Shader->Flags & GLSL_DUMP) {
6741 _mesa_log("\n");
6742 _mesa_log("GLSL IR for linked %s program %d:\n",
6743 _mesa_shader_stage_to_string(shader->Stage),
6744 shader_program->Name);
6745 _mesa_print_ir(_mesa_get_log_file(), shader->ir, NULL);
6746 _mesa_log("\n\n");
6747 }
6748
6749 do_set_program_inouts(shader->ir, prog, shader->Stage);
6750 _mesa_copy_linked_program_data(shader_program, shader);
6751 shrink_array_declarations(v->inputs, v->num_inputs,
6752 &prog->info.inputs_read,
6753 prog->info.double_inputs_read,
6754 &prog->info.patch_inputs_read);
6755 shrink_array_declarations(v->outputs, v->num_outputs,
6756 &prog->info.outputs_written, 0ULL,
6757 &prog->info.patch_outputs_written);
6758 count_resources(v, prog);
6759
6760 /* The GLSL IR won't be needed anymore. */
6761 ralloc_free(shader->ir);
6762 shader->ir = NULL;
6763
6764 /* This must be done before the uniform storage is associated. */
6765 if (shader->Stage == MESA_SHADER_FRAGMENT &&
6766 (prog->info.inputs_read & VARYING_BIT_POS ||
6767 prog->info.system_values_read & (1 << SYSTEM_VALUE_FRAG_COORD))) {
6768 static const gl_state_index wposTransformState[STATE_LENGTH] = {
6769 STATE_INTERNAL, STATE_FB_WPOS_Y_TRANSFORM
6770 };
6771
6772 v->wpos_transform_const = _mesa_add_state_reference(prog->Parameters,
6773 wposTransformState);
6774 }
6775
6776 /* Avoid reallocation of the program parameter list, because the uniform
6777 * storage is only associated with the original parameter list.
6778 * This should be enough for Bitmap and DrawPixels constants.
6779 */
6780 _mesa_reserve_parameter_storage(prog->Parameters, 8);
6781
6782 /* This has to be done last. Any operation the can cause
6783 * prog->ParameterValues to get reallocated (e.g., anything that adds a
6784 * program constant) has to happen before creating this linkage.
6785 */
6786 _mesa_associate_uniform_storage(ctx, shader_program, prog->Parameters,
6787 true);
6788 if (!shader_program->data->LinkStatus) {
6789 free_glsl_to_tgsi_visitor(v);
6790 _mesa_reference_program(ctx, &shader->Program, NULL);
6791 return NULL;
6792 }
6793
6794 struct st_vertex_program *stvp;
6795 struct st_fragment_program *stfp;
6796 struct st_geometry_program *stgp;
6797 struct st_tessctrl_program *sttcp;
6798 struct st_tesseval_program *sttep;
6799 struct st_compute_program *stcp;
6800
6801 switch (shader->Stage) {
6802 case MESA_SHADER_VERTEX:
6803 stvp = (struct st_vertex_program *)prog;
6804 stvp->glsl_to_tgsi = v;
6805 break;
6806 case MESA_SHADER_FRAGMENT:
6807 stfp = (struct st_fragment_program *)prog;
6808 stfp->glsl_to_tgsi = v;
6809 break;
6810 case MESA_SHADER_GEOMETRY:
6811 stgp = (struct st_geometry_program *)prog;
6812 stgp->glsl_to_tgsi = v;
6813 break;
6814 case MESA_SHADER_TESS_CTRL:
6815 sttcp = (struct st_tessctrl_program *)prog;
6816 sttcp->glsl_to_tgsi = v;
6817 break;
6818 case MESA_SHADER_TESS_EVAL:
6819 sttep = (struct st_tesseval_program *)prog;
6820 sttep->glsl_to_tgsi = v;
6821 break;
6822 case MESA_SHADER_COMPUTE:
6823 stcp = (struct st_compute_program *)prog;
6824 stcp->glsl_to_tgsi = v;
6825 break;
6826 default:
6827 assert(!"should not be reached");
6828 return NULL;
6829 }
6830
6831 return prog;
6832 }
6833
6834 /* See if there are unsupported control flow statements. */
6835 class ir_control_flow_info_visitor : public ir_hierarchical_visitor {
6836 private:
6837 const struct gl_shader_compiler_options *options;
6838 public:
6839 ir_control_flow_info_visitor(const struct gl_shader_compiler_options *options)
6840 : options(options),
6841 unsupported(false)
6842 {
6843 }
6844
6845 virtual ir_visitor_status visit_enter(ir_function *ir)
6846 {
6847 /* Other functions are skipped (same as glsl_to_tgsi). */
6848 if (strcmp(ir->name, "main") == 0)
6849 return visit_continue;
6850
6851 return visit_continue_with_parent;
6852 }
6853
6854 virtual ir_visitor_status visit_enter(ir_call *ir)
6855 {
6856 if (!ir->callee->is_intrinsic()) {
6857 unsupported = true; /* it's a function call */
6858 return visit_stop;
6859 }
6860 return visit_continue;
6861 }
6862
6863 virtual ir_visitor_status visit_enter(ir_return *ir)
6864 {
6865 if (options->EmitNoMainReturn) {
6866 unsupported = true;
6867 return visit_stop;
6868 }
6869 return visit_continue;
6870 }
6871
6872 bool unsupported;
6873 };
6874
6875 static bool
6876 has_unsupported_control_flow(exec_list *ir,
6877 const struct gl_shader_compiler_options *options)
6878 {
6879 ir_control_flow_info_visitor visitor(options);
6880 visit_list_elements(&visitor, ir);
6881 return visitor.unsupported;
6882 }
6883
6884 extern "C" {
6885
6886 /**
6887 * Link a shader.
6888 * Called via ctx->Driver.LinkShader()
6889 * This actually involves converting GLSL IR into an intermediate TGSI-like IR
6890 * with code lowering and other optimizations.
6891 */
6892 GLboolean
6893 st_link_shader(struct gl_context *ctx, struct gl_shader_program *prog)
6894 {
6895 /* Return early if we are loading the shader from on-disk cache */
6896 if (st_load_tgsi_from_disk_cache(ctx, prog)) {
6897 return GL_TRUE;
6898 }
6899
6900 struct pipe_screen *pscreen = ctx->st->pipe->screen;
6901 assert(prog->data->LinkStatus);
6902
6903 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
6904 if (prog->_LinkedShaders[i] == NULL)
6905 continue;
6906
6907 struct gl_linked_shader *shader = prog->_LinkedShaders[i];
6908 exec_list *ir = shader->ir;
6909 gl_shader_stage stage = shader->Stage;
6910 const struct gl_shader_compiler_options *options =
6911 &ctx->Const.ShaderCompilerOptions[stage];
6912 enum pipe_shader_type ptarget = st_shader_stage_to_ptarget(stage);
6913 bool have_dround = pscreen->get_shader_param(pscreen, ptarget,
6914 PIPE_SHADER_CAP_TGSI_DROUND_SUPPORTED);
6915 bool have_dfrexp = pscreen->get_shader_param(pscreen, ptarget,
6916 PIPE_SHADER_CAP_TGSI_DFRACEXP_DLDEXP_SUPPORTED);
6917 unsigned if_threshold = pscreen->get_shader_param(pscreen, ptarget,
6918 PIPE_SHADER_CAP_LOWER_IF_THRESHOLD);
6919
6920 /* If there are forms of indirect addressing that the driver
6921 * cannot handle, perform the lowering pass.
6922 */
6923 if (options->EmitNoIndirectInput || options->EmitNoIndirectOutput ||
6924 options->EmitNoIndirectTemp || options->EmitNoIndirectUniform) {
6925 lower_variable_index_to_cond_assign(stage, ir,
6926 options->EmitNoIndirectInput,
6927 options->EmitNoIndirectOutput,
6928 options->EmitNoIndirectTemp,
6929 options->EmitNoIndirectUniform);
6930 }
6931
6932 if (!pscreen->get_param(pscreen, PIPE_CAP_INT64_DIVMOD))
6933 lower_64bit_integer_instructions(ir, DIV64 | MOD64);
6934
6935 if (ctx->Extensions.ARB_shading_language_packing) {
6936 unsigned lower_inst = LOWER_PACK_SNORM_2x16 |
6937 LOWER_UNPACK_SNORM_2x16 |
6938 LOWER_PACK_UNORM_2x16 |
6939 LOWER_UNPACK_UNORM_2x16 |
6940 LOWER_PACK_SNORM_4x8 |
6941 LOWER_UNPACK_SNORM_4x8 |
6942 LOWER_UNPACK_UNORM_4x8 |
6943 LOWER_PACK_UNORM_4x8;
6944
6945 if (ctx->Extensions.ARB_gpu_shader5)
6946 lower_inst |= LOWER_PACK_USE_BFI |
6947 LOWER_PACK_USE_BFE;
6948 if (!ctx->st->has_half_float_packing)
6949 lower_inst |= LOWER_PACK_HALF_2x16 |
6950 LOWER_UNPACK_HALF_2x16;
6951
6952 lower_packing_builtins(ir, lower_inst);
6953 }
6954
6955 if (!pscreen->get_param(pscreen, PIPE_CAP_TEXTURE_GATHER_OFFSETS))
6956 lower_offset_arrays(ir);
6957 do_mat_op_to_vec(ir);
6958
6959 if (stage == MESA_SHADER_FRAGMENT)
6960 lower_blend_equation_advanced(shader);
6961
6962 lower_instructions(ir,
6963 MOD_TO_FLOOR |
6964 FDIV_TO_MUL_RCP |
6965 EXP_TO_EXP2 |
6966 LOG_TO_LOG2 |
6967 LDEXP_TO_ARITH |
6968 (have_dfrexp ? 0 : DFREXP_DLDEXP_TO_ARITH) |
6969 CARRY_TO_ARITH |
6970 BORROW_TO_ARITH |
6971 (have_dround ? 0 : DOPS_TO_DFRAC) |
6972 (options->EmitNoPow ? POW_TO_EXP2 : 0) |
6973 (!ctx->Const.NativeIntegers ? INT_DIV_TO_MUL_RCP : 0) |
6974 (options->EmitNoSat ? SAT_TO_CLAMP : 0) |
6975 /* Assume that if ARB_gpu_shader5 is not supported
6976 * then all of the extended integer functions need
6977 * lowering. It may be necessary to add some caps
6978 * for individual instructions.
6979 */
6980 (!ctx->Extensions.ARB_gpu_shader5
6981 ? BIT_COUNT_TO_MATH |
6982 EXTRACT_TO_SHIFTS |
6983 INSERT_TO_SHIFTS |
6984 REVERSE_TO_SHIFTS |
6985 FIND_LSB_TO_FLOAT_CAST |
6986 FIND_MSB_TO_FLOAT_CAST |
6987 IMUL_HIGH_TO_MUL
6988 : 0));
6989
6990 do_vec_index_to_cond_assign(ir);
6991 lower_vector_insert(ir, true);
6992 lower_quadop_vector(ir, false);
6993 lower_noise(ir);
6994 if (options->MaxIfDepth == 0) {
6995 lower_discard(ir);
6996 }
6997
6998 if (ctx->Const.GLSLOptimizeConservatively) {
6999 /* Do it once and repeat only if there's unsupported control flow. */
7000 do {
7001 do_common_optimization(ir, true, true, options,
7002 ctx->Const.NativeIntegers);
7003 lower_if_to_cond_assign((gl_shader_stage)i, ir,
7004 options->MaxIfDepth, if_threshold);
7005 } while (has_unsupported_control_flow(ir, options));
7006 } else {
7007 /* Repeat it until it stops making changes. */
7008 bool progress;
7009 do {
7010 progress = do_common_optimization(ir, true, true, options,
7011 ctx->Const.NativeIntegers);
7012 progress |= lower_if_to_cond_assign((gl_shader_stage)i, ir,
7013 options->MaxIfDepth, if_threshold);
7014 } while (progress);
7015 }
7016
7017 validate_ir_tree(ir);
7018 }
7019
7020 build_program_resource_list(ctx, prog);
7021
7022 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
7023 struct gl_linked_shader *shader = prog->_LinkedShaders[i];
7024 if (shader == NULL)
7025 continue;
7026
7027 enum pipe_shader_type ptarget =
7028 st_shader_stage_to_ptarget(shader->Stage);
7029 enum pipe_shader_ir preferred_ir = (enum pipe_shader_ir)
7030 pscreen->get_shader_param(pscreen, ptarget,
7031 PIPE_SHADER_CAP_PREFERRED_IR);
7032
7033 struct gl_program *linked_prog = NULL;
7034 if (preferred_ir == PIPE_SHADER_IR_NIR) {
7035 /* TODO only for GLSL VS/FS for now: */
7036 switch (shader->Stage) {
7037 case MESA_SHADER_VERTEX:
7038 case MESA_SHADER_FRAGMENT:
7039 linked_prog = st_nir_get_mesa_program(ctx, prog, shader);
7040 default:
7041 break;
7042 }
7043 } else {
7044 linked_prog = get_mesa_program_tgsi(ctx, prog, shader);
7045 }
7046
7047 if (linked_prog) {
7048 st_set_prog_affected_state_flags(linked_prog);
7049 if (!ctx->Driver.ProgramStringNotify(ctx,
7050 _mesa_shader_stage_to_program(i),
7051 linked_prog)) {
7052 _mesa_reference_program(ctx, &shader->Program, NULL);
7053 return GL_FALSE;
7054 }
7055 }
7056 }
7057
7058 return GL_TRUE;
7059 }
7060
7061 void
7062 st_translate_stream_output_info(glsl_to_tgsi_visitor *glsl_to_tgsi,
7063 const GLuint outputMapping[],
7064 struct pipe_stream_output_info *so)
7065 {
7066 if (!glsl_to_tgsi->shader_program->last_vert_prog)
7067 return;
7068
7069 struct gl_transform_feedback_info *info =
7070 glsl_to_tgsi->shader_program->last_vert_prog->sh.LinkedTransformFeedback;
7071 st_translate_stream_output_info2(info, outputMapping, so);
7072 }
7073
7074 void
7075 st_translate_stream_output_info2(struct gl_transform_feedback_info *info,
7076 const GLuint outputMapping[],
7077 struct pipe_stream_output_info *so)
7078 {
7079 unsigned i;
7080
7081 for (i = 0; i < info->NumOutputs; i++) {
7082 so->output[i].register_index =
7083 outputMapping[info->Outputs[i].OutputRegister];
7084 so->output[i].start_component = info->Outputs[i].ComponentOffset;
7085 so->output[i].num_components = info->Outputs[i].NumComponents;
7086 so->output[i].output_buffer = info->Outputs[i].OutputBuffer;
7087 so->output[i].dst_offset = info->Outputs[i].DstOffset;
7088 so->output[i].stream = info->Outputs[i].StreamId;
7089 }
7090
7091 for (i = 0; i < PIPE_MAX_SO_BUFFERS; i++) {
7092 so->stride[i] = info->Buffers[i].Stride;
7093 }
7094 so->num_outputs = info->NumOutputs;
7095 }
7096
7097 } /* extern "C" */