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