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