e2505a709ae9f5558b5560024513d9934c2185d5
[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, image 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 case ir_unop_unpack_sampler_2x32:
2335 case ir_unop_pack_sampler_2x32:
2336 case ir_unop_unpack_image_2x32:
2337 case ir_unop_pack_image_2x32:
2338 emit_asm(ir, TGSI_OPCODE_MOV, result_dst, op[0]);
2339 break;
2340
2341 case ir_binop_ldexp:
2342 if (ir->operands[0]->type->is_double()) {
2343 emit_asm(ir, TGSI_OPCODE_DLDEXP, result_dst, op[0], op[1]);
2344 } else {
2345 assert(!"Invalid ldexp for non-double opcode in glsl_to_tgsi_visitor::visit()");
2346 }
2347 break;
2348
2349 case ir_unop_pack_half_2x16:
2350 emit_asm(ir, TGSI_OPCODE_PK2H, result_dst, op[0]);
2351 break;
2352 case ir_unop_unpack_half_2x16:
2353 emit_asm(ir, TGSI_OPCODE_UP2H, result_dst, op[0]);
2354 break;
2355
2356 case ir_unop_get_buffer_size: {
2357 ir_constant *const_offset = ir->operands[0]->as_constant();
2358 st_src_reg buffer(
2359 PROGRAM_BUFFER,
2360 ctx->Const.Program[shader->Stage].MaxAtomicBuffers +
2361 (const_offset ? const_offset->value.u[0] : 0),
2362 GLSL_TYPE_UINT);
2363 if (!const_offset) {
2364 buffer.reladdr = ralloc(mem_ctx, st_src_reg);
2365 *buffer.reladdr = op[0];
2366 emit_arl(ir, sampler_reladdr, op[0]);
2367 }
2368 emit_asm(ir, TGSI_OPCODE_RESQ, result_dst)->resource = buffer;
2369 break;
2370 }
2371
2372 case ir_unop_u2i64:
2373 case ir_unop_u2u64:
2374 case ir_unop_b2i64: {
2375 st_src_reg temp = get_temp(glsl_type::uvec4_type);
2376 st_dst_reg temp_dst = st_dst_reg(temp);
2377 unsigned orig_swz = op[0].swizzle;
2378 /*
2379 * To convert unsigned to 64-bit:
2380 * zero Y channel, copy X channel.
2381 */
2382 temp_dst.writemask = WRITEMASK_Y;
2383 if (vector_elements > 1)
2384 temp_dst.writemask |= WRITEMASK_W;
2385 emit_asm(ir, TGSI_OPCODE_MOV, temp_dst, st_src_reg_for_int(0));
2386 temp_dst.writemask = WRITEMASK_X;
2387 if (vector_elements > 1)
2388 temp_dst.writemask |= WRITEMASK_Z;
2389 op[0].swizzle = MAKE_SWIZZLE4(GET_SWZ(orig_swz, 0), GET_SWZ(orig_swz, 0),
2390 GET_SWZ(orig_swz, 1), GET_SWZ(orig_swz, 1));
2391 if (ir->operation == ir_unop_u2i64 || ir->operation == ir_unop_u2u64)
2392 emit_asm(ir, TGSI_OPCODE_MOV, temp_dst, op[0]);
2393 else
2394 emit_asm(ir, TGSI_OPCODE_AND, temp_dst, op[0], st_src_reg_for_int(1));
2395 result_src = temp;
2396 result_src.type = GLSL_TYPE_UINT64;
2397 if (vector_elements > 2) {
2398 /* Subtle: We rely on the fact that get_temp here returns the next
2399 * TGSI temporary register directly after the temp register used for
2400 * the first two components, so that the result gets picked up
2401 * automatically.
2402 */
2403 st_src_reg temp = get_temp(glsl_type::uvec4_type);
2404 st_dst_reg temp_dst = st_dst_reg(temp);
2405 temp_dst.writemask = WRITEMASK_Y;
2406 if (vector_elements > 3)
2407 temp_dst.writemask |= WRITEMASK_W;
2408 emit_asm(ir, TGSI_OPCODE_MOV, temp_dst, st_src_reg_for_int(0));
2409
2410 temp_dst.writemask = WRITEMASK_X;
2411 if (vector_elements > 3)
2412 temp_dst.writemask |= WRITEMASK_Z;
2413 op[0].swizzle = MAKE_SWIZZLE4(GET_SWZ(orig_swz, 2), GET_SWZ(orig_swz, 2),
2414 GET_SWZ(orig_swz, 3), GET_SWZ(orig_swz, 3));
2415 if (ir->operation == ir_unop_u2i64 || ir->operation == ir_unop_u2u64)
2416 emit_asm(ir, TGSI_OPCODE_MOV, temp_dst, op[0]);
2417 else
2418 emit_asm(ir, TGSI_OPCODE_AND, temp_dst, op[0], st_src_reg_for_int(1));
2419 }
2420 break;
2421 }
2422 case ir_unop_i642i:
2423 case ir_unop_u642i:
2424 case ir_unop_u642u:
2425 case ir_unop_i642u: {
2426 st_src_reg temp = get_temp(glsl_type::uvec4_type);
2427 st_dst_reg temp_dst = st_dst_reg(temp);
2428 unsigned orig_swz = op[0].swizzle;
2429 unsigned orig_idx = op[0].index;
2430 int el;
2431 temp_dst.writemask = WRITEMASK_X;
2432
2433 for (el = 0; el < vector_elements; el++) {
2434 unsigned swz = GET_SWZ(orig_swz, el);
2435 if (swz & 1)
2436 op[0].swizzle = MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_Z, SWIZZLE_Z, SWIZZLE_Z);
2437 else
2438 op[0].swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X);
2439 if (swz > 2)
2440 op[0].index = orig_idx + 1;
2441 op[0].type = GLSL_TYPE_UINT;
2442 temp_dst.writemask = WRITEMASK_X << el;
2443 emit_asm(ir, TGSI_OPCODE_MOV, temp_dst, op[0]);
2444 }
2445 result_src = temp;
2446 if (ir->operation == ir_unop_u642u || ir->operation == ir_unop_i642u)
2447 result_src.type = GLSL_TYPE_UINT;
2448 else
2449 result_src.type = GLSL_TYPE_INT;
2450 break;
2451 }
2452 case ir_unop_i642b:
2453 emit_asm(ir, TGSI_OPCODE_U64SNE, result_dst, op[0], st_src_reg_for_int(0));
2454 break;
2455 case ir_unop_i642f:
2456 emit_asm(ir, TGSI_OPCODE_I642F, result_dst, op[0]);
2457 break;
2458 case ir_unop_u642f:
2459 emit_asm(ir, TGSI_OPCODE_U642F, result_dst, op[0]);
2460 break;
2461 case ir_unop_i642d:
2462 emit_asm(ir, TGSI_OPCODE_I642D, result_dst, op[0]);
2463 break;
2464 case ir_unop_u642d:
2465 emit_asm(ir, TGSI_OPCODE_U642D, result_dst, op[0]);
2466 break;
2467 case ir_unop_i2i64:
2468 emit_asm(ir, TGSI_OPCODE_I2I64, result_dst, op[0]);
2469 break;
2470 case ir_unop_f2i64:
2471 emit_asm(ir, TGSI_OPCODE_F2I64, result_dst, op[0]);
2472 break;
2473 case ir_unop_d2i64:
2474 emit_asm(ir, TGSI_OPCODE_D2I64, result_dst, op[0]);
2475 break;
2476 case ir_unop_i2u64:
2477 emit_asm(ir, TGSI_OPCODE_I2I64, result_dst, op[0]);
2478 break;
2479 case ir_unop_f2u64:
2480 emit_asm(ir, TGSI_OPCODE_F2U64, result_dst, op[0]);
2481 break;
2482 case ir_unop_d2u64:
2483 emit_asm(ir, TGSI_OPCODE_D2U64, result_dst, op[0]);
2484 break;
2485 /* these might be needed */
2486 case ir_unop_pack_snorm_2x16:
2487 case ir_unop_pack_unorm_2x16:
2488 case ir_unop_pack_snorm_4x8:
2489 case ir_unop_pack_unorm_4x8:
2490
2491 case ir_unop_unpack_snorm_2x16:
2492 case ir_unop_unpack_unorm_2x16:
2493 case ir_unop_unpack_snorm_4x8:
2494 case ir_unop_unpack_unorm_4x8:
2495
2496 case ir_quadop_vector:
2497 case ir_binop_vector_extract:
2498 case ir_triop_vector_insert:
2499 case ir_binop_carry:
2500 case ir_binop_borrow:
2501 case ir_unop_ssbo_unsized_array_length:
2502 /* This operation is not supported, or should have already been handled.
2503 */
2504 assert(!"Invalid ir opcode in glsl_to_tgsi_visitor::visit()");
2505 break;
2506 }
2507
2508 this->result = result_src;
2509 }
2510
2511
2512 void
2513 glsl_to_tgsi_visitor::visit(ir_swizzle *ir)
2514 {
2515 st_src_reg src;
2516 int i;
2517 int swizzle[4];
2518
2519 /* Note that this is only swizzles in expressions, not those on the left
2520 * hand side of an assignment, which do write masking. See ir_assignment
2521 * for that.
2522 */
2523
2524 ir->val->accept(this);
2525 src = this->result;
2526 assert(src.file != PROGRAM_UNDEFINED);
2527 assert(ir->type->vector_elements > 0);
2528
2529 for (i = 0; i < 4; i++) {
2530 if (i < ir->type->vector_elements) {
2531 switch (i) {
2532 case 0:
2533 swizzle[i] = GET_SWZ(src.swizzle, ir->mask.x);
2534 break;
2535 case 1:
2536 swizzle[i] = GET_SWZ(src.swizzle, ir->mask.y);
2537 break;
2538 case 2:
2539 swizzle[i] = GET_SWZ(src.swizzle, ir->mask.z);
2540 break;
2541 case 3:
2542 swizzle[i] = GET_SWZ(src.swizzle, ir->mask.w);
2543 break;
2544 }
2545 } else {
2546 /* If the type is smaller than a vec4, replicate the last
2547 * channel out.
2548 */
2549 swizzle[i] = swizzle[ir->type->vector_elements - 1];
2550 }
2551 }
2552
2553 src.swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
2554
2555 this->result = src;
2556 }
2557
2558 /* Test if the variable is an array. Note that geometry and
2559 * tessellation shader inputs are outputs are always arrays (except
2560 * for patch inputs), so only the array element type is considered.
2561 */
2562 static bool
2563 is_inout_array(unsigned stage, ir_variable *var, bool *remove_array)
2564 {
2565 const glsl_type *type = var->type;
2566
2567 *remove_array = false;
2568
2569 if ((stage == MESA_SHADER_VERTEX && var->data.mode == ir_var_shader_in) ||
2570 (stage == MESA_SHADER_FRAGMENT && var->data.mode == ir_var_shader_out))
2571 return false;
2572
2573 if (((stage == MESA_SHADER_GEOMETRY && var->data.mode == ir_var_shader_in) ||
2574 (stage == MESA_SHADER_TESS_EVAL && var->data.mode == ir_var_shader_in) ||
2575 stage == MESA_SHADER_TESS_CTRL) &&
2576 !var->data.patch) {
2577 if (!var->type->is_array())
2578 return false; /* a system value probably */
2579
2580 type = var->type->fields.array;
2581 *remove_array = true;
2582 }
2583
2584 return type->is_array() || type->is_matrix();
2585 }
2586
2587 static unsigned
2588 st_translate_interp_loc(ir_variable *var)
2589 {
2590 if (var->data.centroid)
2591 return TGSI_INTERPOLATE_LOC_CENTROID;
2592 else if (var->data.sample)
2593 return TGSI_INTERPOLATE_LOC_SAMPLE;
2594 else
2595 return TGSI_INTERPOLATE_LOC_CENTER;
2596 }
2597
2598 void
2599 glsl_to_tgsi_visitor::visit(ir_dereference_variable *ir)
2600 {
2601 variable_storage *entry = find_variable_storage(ir->var);
2602 ir_variable *var = ir->var;
2603 bool remove_array;
2604
2605 if (!entry) {
2606 switch (var->data.mode) {
2607 case ir_var_uniform:
2608 entry = new(mem_ctx) variable_storage(var, PROGRAM_UNIFORM,
2609 var->data.param_index);
2610 _mesa_hash_table_insert(this->variables, var, entry);
2611 break;
2612 case ir_var_shader_in: {
2613 /* The linker assigns locations for varyings and attributes,
2614 * including deprecated builtins (like gl_Color), user-assign
2615 * generic attributes (glBindVertexLocation), and
2616 * user-defined varyings.
2617 */
2618 assert(var->data.location != -1);
2619
2620 const glsl_type *type_without_array = var->type->without_array();
2621 struct inout_decl *decl = &inputs[num_inputs];
2622 unsigned component = var->data.location_frac;
2623 unsigned num_components;
2624 num_inputs++;
2625
2626 if (type_without_array->is_64bit())
2627 component = component / 2;
2628 if (type_without_array->vector_elements)
2629 num_components = type_without_array->vector_elements;
2630 else
2631 num_components = 4;
2632
2633 decl->mesa_index = var->data.location;
2634 decl->interp = (glsl_interp_mode) var->data.interpolation;
2635 decl->interp_loc = st_translate_interp_loc(var);
2636 decl->base_type = type_without_array->base_type;
2637 decl->usage_mask = u_bit_consecutive(component, num_components);
2638
2639 if (is_inout_array(shader->Stage, var, &remove_array)) {
2640 decl->array_id = num_input_arrays + 1;
2641 num_input_arrays++;
2642 } else {
2643 decl->array_id = 0;
2644 }
2645
2646 if (remove_array)
2647 decl->size = type_size(var->type->fields.array);
2648 else
2649 decl->size = type_size(var->type);
2650
2651 entry = new(mem_ctx) variable_storage(var,
2652 PROGRAM_INPUT,
2653 decl->mesa_index,
2654 decl->array_id);
2655 entry->component = component;
2656
2657 _mesa_hash_table_insert(this->variables, var, entry);
2658
2659 break;
2660 }
2661 case ir_var_shader_out: {
2662 assert(var->data.location != -1);
2663
2664 const glsl_type *type_without_array = var->type->without_array();
2665 struct inout_decl *decl = &outputs[num_outputs];
2666 unsigned component = var->data.location_frac;
2667 unsigned num_components;
2668 num_outputs++;
2669
2670 if (type_without_array->is_64bit())
2671 component = component / 2;
2672 if (type_without_array->vector_elements)
2673 num_components = type_without_array->vector_elements;
2674 else
2675 num_components = 4;
2676
2677 decl->mesa_index = var->data.location + FRAG_RESULT_MAX * var->data.index;
2678 decl->base_type = type_without_array->base_type;
2679 decl->usage_mask = u_bit_consecutive(component, num_components);
2680 if (var->data.stream & (1u << 31)) {
2681 decl->gs_out_streams = var->data.stream & ~(1u << 31);
2682 } else {
2683 assert(var->data.stream < 4);
2684 decl->gs_out_streams = 0;
2685 for (unsigned i = 0; i < num_components; ++i)
2686 decl->gs_out_streams |= var->data.stream << (2 * (component + i));
2687 }
2688
2689 if (is_inout_array(shader->Stage, var, &remove_array)) {
2690 decl->array_id = num_output_arrays + 1;
2691 num_output_arrays++;
2692 } else {
2693 decl->array_id = 0;
2694 }
2695
2696 if (remove_array)
2697 decl->size = type_size(var->type->fields.array);
2698 else
2699 decl->size = type_size(var->type);
2700
2701 if (var->data.fb_fetch_output) {
2702 st_dst_reg dst = st_dst_reg(get_temp(var->type));
2703 st_src_reg src = st_src_reg(PROGRAM_OUTPUT, decl->mesa_index,
2704 var->type, component, decl->array_id);
2705 emit_asm(NULL, TGSI_OPCODE_FBFETCH, dst, src);
2706 entry = new(mem_ctx) variable_storage(var, dst.file, dst.index,
2707 dst.array_id);
2708 } else {
2709 entry = new(mem_ctx) variable_storage(var,
2710 PROGRAM_OUTPUT,
2711 decl->mesa_index,
2712 decl->array_id);
2713 }
2714 entry->component = component;
2715
2716 _mesa_hash_table_insert(this->variables, var, entry);
2717
2718 break;
2719 }
2720 case ir_var_system_value:
2721 entry = new(mem_ctx) variable_storage(var,
2722 PROGRAM_SYSTEM_VALUE,
2723 var->data.location);
2724 break;
2725 case ir_var_auto:
2726 case ir_var_temporary:
2727 st_src_reg src = get_temp(var->type);
2728
2729 entry = new(mem_ctx) variable_storage(var, src.file, src.index,
2730 src.array_id);
2731 _mesa_hash_table_insert(this->variables, var, entry);
2732
2733 break;
2734 }
2735
2736 if (!entry) {
2737 printf("Failed to make storage for %s\n", var->name);
2738 exit(1);
2739 }
2740 }
2741
2742 this->result = st_src_reg(entry->file, entry->index, var->type,
2743 entry->component, entry->array_id);
2744 if (this->shader->Stage == MESA_SHADER_VERTEX && var->data.mode == ir_var_shader_in && var->type->is_double())
2745 this->result.is_double_vertex_input = true;
2746 if (!native_integers)
2747 this->result.type = GLSL_TYPE_FLOAT;
2748 }
2749
2750 static void
2751 shrink_array_declarations(struct inout_decl *decls, unsigned count,
2752 GLbitfield64* usage_mask,
2753 GLbitfield64 double_usage_mask,
2754 GLbitfield* patch_usage_mask)
2755 {
2756 unsigned i;
2757 int j;
2758
2759 /* Fix array declarations by removing unused array elements at both ends
2760 * of the arrays. For example, mat4[3] where only mat[1] is used.
2761 */
2762 for (i = 0; i < count; i++) {
2763 struct inout_decl *decl = &decls[i];
2764 if (!decl->array_id)
2765 continue;
2766
2767 /* Shrink the beginning. */
2768 for (j = 0; j < (int)decl->size; j++) {
2769 if (decl->mesa_index >= VARYING_SLOT_PATCH0) {
2770 if (*patch_usage_mask &
2771 BITFIELD64_BIT(decl->mesa_index - VARYING_SLOT_PATCH0 + j))
2772 break;
2773 }
2774 else {
2775 if (*usage_mask & BITFIELD64_BIT(decl->mesa_index+j))
2776 break;
2777 if (double_usage_mask & BITFIELD64_BIT(decl->mesa_index+j-1))
2778 break;
2779 }
2780
2781 decl->mesa_index++;
2782 decl->size--;
2783 j--;
2784 }
2785
2786 /* Shrink the end. */
2787 for (j = decl->size-1; j >= 0; j--) {
2788 if (decl->mesa_index >= VARYING_SLOT_PATCH0) {
2789 if (*patch_usage_mask &
2790 BITFIELD64_BIT(decl->mesa_index - VARYING_SLOT_PATCH0 + j))
2791 break;
2792 }
2793 else {
2794 if (*usage_mask & BITFIELD64_BIT(decl->mesa_index+j))
2795 break;
2796 if (double_usage_mask & BITFIELD64_BIT(decl->mesa_index+j-1))
2797 break;
2798 }
2799
2800 decl->size--;
2801 }
2802
2803 /* When not all entries of an array are accessed, we mark them as used
2804 * here anyway, to ensure that the input/output mapping logic doesn't get
2805 * confused.
2806 *
2807 * TODO This happens when an array isn't used via indirect access, which
2808 * some game ports do (at least eON-based). There is an optimization
2809 * opportunity here by replacing the array declaration with non-array
2810 * declarations of those slots that are actually used.
2811 */
2812 for (j = 1; j < (int)decl->size; ++j) {
2813 if (decl->mesa_index >= VARYING_SLOT_PATCH0)
2814 *patch_usage_mask |= BITFIELD64_BIT(decl->mesa_index - VARYING_SLOT_PATCH0 + j);
2815 else
2816 *usage_mask |= BITFIELD64_BIT(decl->mesa_index + j);
2817 }
2818 }
2819 }
2820
2821 void
2822 glsl_to_tgsi_visitor::visit(ir_dereference_array *ir)
2823 {
2824 ir_constant *index;
2825 st_src_reg src;
2826 int element_size = type_size(ir->type);
2827 bool is_2D = false;
2828
2829 index = ir->array_index->constant_expression_value();
2830
2831 ir->array->accept(this);
2832 src = this->result;
2833
2834 if (ir->array->ir_type != ir_type_dereference_array) {
2835 switch (this->prog->Target) {
2836 case GL_TESS_CONTROL_PROGRAM_NV:
2837 is_2D = (src.file == PROGRAM_INPUT || src.file == PROGRAM_OUTPUT) &&
2838 !ir->variable_referenced()->data.patch;
2839 break;
2840 case GL_TESS_EVALUATION_PROGRAM_NV:
2841 is_2D = src.file == PROGRAM_INPUT &&
2842 !ir->variable_referenced()->data.patch;
2843 break;
2844 case GL_GEOMETRY_PROGRAM_NV:
2845 is_2D = src.file == PROGRAM_INPUT;
2846 break;
2847 }
2848 }
2849
2850 if (is_2D)
2851 element_size = 1;
2852
2853 if (index) {
2854
2855 if (this->prog->Target == GL_VERTEX_PROGRAM_ARB &&
2856 src.file == PROGRAM_INPUT)
2857 element_size = attrib_type_size(ir->type, true);
2858 if (is_2D) {
2859 src.index2D = index->value.i[0];
2860 src.has_index2 = true;
2861 } else
2862 src.index += index->value.i[0] * element_size;
2863 } else {
2864 /* Variable index array dereference. It eats the "vec4" of the
2865 * base of the array and an index that offsets the TGSI register
2866 * index.
2867 */
2868 ir->array_index->accept(this);
2869
2870 st_src_reg index_reg;
2871
2872 if (element_size == 1) {
2873 index_reg = this->result;
2874 } else {
2875 index_reg = get_temp(native_integers ?
2876 glsl_type::int_type : glsl_type::float_type);
2877
2878 emit_asm(ir, TGSI_OPCODE_MUL, st_dst_reg(index_reg),
2879 this->result, st_src_reg_for_type(index_reg.type, element_size));
2880 }
2881
2882 /* If there was already a relative address register involved, add the
2883 * new and the old together to get the new offset.
2884 */
2885 if (!is_2D && src.reladdr != NULL) {
2886 st_src_reg accum_reg = get_temp(native_integers ?
2887 glsl_type::int_type : glsl_type::float_type);
2888
2889 emit_asm(ir, TGSI_OPCODE_ADD, st_dst_reg(accum_reg),
2890 index_reg, *src.reladdr);
2891
2892 index_reg = accum_reg;
2893 }
2894
2895 if (is_2D) {
2896 src.reladdr2 = ralloc(mem_ctx, st_src_reg);
2897 memcpy(src.reladdr2, &index_reg, sizeof(index_reg));
2898 src.index2D = 0;
2899 src.has_index2 = true;
2900 } else {
2901 src.reladdr = ralloc(mem_ctx, st_src_reg);
2902 memcpy(src.reladdr, &index_reg, sizeof(index_reg));
2903 }
2904 }
2905
2906 /* Change the register type to the element type of the array. */
2907 src.type = ir->type->base_type;
2908
2909 this->result = src;
2910 }
2911
2912 void
2913 glsl_to_tgsi_visitor::visit(ir_dereference_record *ir)
2914 {
2915 unsigned int i;
2916 const glsl_type *struct_type = ir->record->type;
2917 int offset = 0;
2918
2919 ir->record->accept(this);
2920
2921 for (i = 0; i < struct_type->length; i++) {
2922 if (strcmp(struct_type->fields.structure[i].name, ir->field) == 0)
2923 break;
2924 offset += type_size(struct_type->fields.structure[i].type);
2925 }
2926
2927 /* If the type is smaller than a vec4, replicate the last channel out. */
2928 if (ir->type->is_scalar() || ir->type->is_vector())
2929 this->result.swizzle = swizzle_for_size(ir->type->vector_elements);
2930 else
2931 this->result.swizzle = SWIZZLE_NOOP;
2932
2933 this->result.index += offset;
2934 this->result.type = ir->type->base_type;
2935 }
2936
2937 /**
2938 * We want to be careful in assignment setup to hit the actual storage
2939 * instead of potentially using a temporary like we might with the
2940 * ir_dereference handler.
2941 */
2942 static st_dst_reg
2943 get_assignment_lhs(ir_dereference *ir, glsl_to_tgsi_visitor *v, int *component)
2944 {
2945 /* The LHS must be a dereference. If the LHS is a variable indexed array
2946 * access of a vector, it must be separated into a series conditional moves
2947 * before reaching this point (see ir_vec_index_to_cond_assign).
2948 */
2949 assert(ir->as_dereference());
2950 ir_dereference_array *deref_array = ir->as_dereference_array();
2951 if (deref_array) {
2952 assert(!deref_array->array->type->is_vector());
2953 }
2954
2955 /* Use the rvalue deref handler for the most part. We write swizzles using
2956 * the writemask, but we do extract the base component for enhanced layouts
2957 * from the source swizzle.
2958 */
2959 ir->accept(v);
2960 *component = GET_SWZ(v->result.swizzle, 0);
2961 return st_dst_reg(v->result);
2962 }
2963
2964 /**
2965 * Process the condition of a conditional assignment
2966 *
2967 * Examines the condition of a conditional assignment to generate the optimal
2968 * first operand of a \c CMP instruction. If the condition is a relational
2969 * operator with 0 (e.g., \c ir_binop_less), the value being compared will be
2970 * used as the source for the \c CMP instruction. Otherwise the comparison
2971 * is processed to a boolean result, and the boolean result is used as the
2972 * operand to the CMP instruction.
2973 */
2974 bool
2975 glsl_to_tgsi_visitor::process_move_condition(ir_rvalue *ir)
2976 {
2977 ir_rvalue *src_ir = ir;
2978 bool negate = true;
2979 bool switch_order = false;
2980
2981 ir_expression *const expr = ir->as_expression();
2982
2983 if (native_integers) {
2984 if ((expr != NULL) && (expr->get_num_operands() == 2)) {
2985 enum glsl_base_type type = expr->operands[0]->type->base_type;
2986 if (type == GLSL_TYPE_INT || type == GLSL_TYPE_UINT ||
2987 type == GLSL_TYPE_BOOL) {
2988 if (expr->operation == ir_binop_equal) {
2989 if (expr->operands[0]->is_zero()) {
2990 src_ir = expr->operands[1];
2991 switch_order = true;
2992 }
2993 else if (expr->operands[1]->is_zero()) {
2994 src_ir = expr->operands[0];
2995 switch_order = true;
2996 }
2997 }
2998 else if (expr->operation == ir_binop_nequal) {
2999 if (expr->operands[0]->is_zero()) {
3000 src_ir = expr->operands[1];
3001 }
3002 else if (expr->operands[1]->is_zero()) {
3003 src_ir = expr->operands[0];
3004 }
3005 }
3006 }
3007 }
3008
3009 src_ir->accept(this);
3010 return switch_order;
3011 }
3012
3013 if ((expr != NULL) && (expr->get_num_operands() == 2)) {
3014 bool zero_on_left = false;
3015
3016 if (expr->operands[0]->is_zero()) {
3017 src_ir = expr->operands[1];
3018 zero_on_left = true;
3019 } else if (expr->operands[1]->is_zero()) {
3020 src_ir = expr->operands[0];
3021 zero_on_left = false;
3022 }
3023
3024 /* a is - 0 + - 0 +
3025 * (a < 0) T F F ( a < 0) T F F
3026 * (0 < a) F F T (-a < 0) F F T
3027 * (a <= 0) T T F (-a < 0) F F T (swap order of other operands)
3028 * (0 <= a) F T T ( a < 0) T F F (swap order of other operands)
3029 * (a > 0) F F T (-a < 0) F F T
3030 * (0 > a) T F F ( a < 0) T F F
3031 * (a >= 0) F T T ( a < 0) T F F (swap order of other operands)
3032 * (0 >= a) T T F (-a < 0) F F T (swap order of other operands)
3033 *
3034 * Note that exchanging the order of 0 and 'a' in the comparison simply
3035 * means that the value of 'a' should be negated.
3036 */
3037 if (src_ir != ir) {
3038 switch (expr->operation) {
3039 case ir_binop_less:
3040 switch_order = false;
3041 negate = zero_on_left;
3042 break;
3043
3044 case ir_binop_greater:
3045 switch_order = false;
3046 negate = !zero_on_left;
3047 break;
3048
3049 case ir_binop_lequal:
3050 switch_order = true;
3051 negate = !zero_on_left;
3052 break;
3053
3054 case ir_binop_gequal:
3055 switch_order = true;
3056 negate = zero_on_left;
3057 break;
3058
3059 default:
3060 /* This isn't the right kind of comparison afterall, so make sure
3061 * the whole condition is visited.
3062 */
3063 src_ir = ir;
3064 break;
3065 }
3066 }
3067 }
3068
3069 src_ir->accept(this);
3070
3071 /* We use the TGSI_OPCODE_CMP (a < 0 ? b : c) for conditional moves, and the
3072 * condition we produced is 0.0 or 1.0. By flipping the sign, we can
3073 * choose which value TGSI_OPCODE_CMP produces without an extra instruction
3074 * computing the condition.
3075 */
3076 if (negate)
3077 this->result.negate = ~this->result.negate;
3078
3079 return switch_order;
3080 }
3081
3082 void
3083 glsl_to_tgsi_visitor::emit_block_mov(ir_assignment *ir, const struct glsl_type *type,
3084 st_dst_reg *l, st_src_reg *r,
3085 st_src_reg *cond, bool cond_swap)
3086 {
3087 if (type->is_record()) {
3088 for (unsigned int i = 0; i < type->length; i++) {
3089 emit_block_mov(ir, type->fields.structure[i].type, l, r,
3090 cond, cond_swap);
3091 }
3092 return;
3093 }
3094
3095 if (type->is_array()) {
3096 for (unsigned int i = 0; i < type->length; i++) {
3097 emit_block_mov(ir, type->fields.array, l, r, cond, cond_swap);
3098 }
3099 return;
3100 }
3101
3102 if (type->is_matrix()) {
3103 const struct glsl_type *vec_type;
3104
3105 vec_type = glsl_type::get_instance(type->is_double() ? GLSL_TYPE_DOUBLE : GLSL_TYPE_FLOAT,
3106 type->vector_elements, 1);
3107
3108 for (int i = 0; i < type->matrix_columns; i++) {
3109 emit_block_mov(ir, vec_type, l, r, cond, cond_swap);
3110 }
3111 return;
3112 }
3113
3114 assert(type->is_scalar() || type->is_vector());
3115
3116 l->type = type->base_type;
3117 r->type = type->base_type;
3118 if (cond) {
3119 st_src_reg l_src = st_src_reg(*l);
3120 l_src.swizzle = swizzle_for_size(type->vector_elements);
3121
3122 if (native_integers) {
3123 emit_asm(ir, TGSI_OPCODE_UCMP, *l, *cond,
3124 cond_swap ? l_src : *r,
3125 cond_swap ? *r : l_src);
3126 } else {
3127 emit_asm(ir, TGSI_OPCODE_CMP, *l, *cond,
3128 cond_swap ? l_src : *r,
3129 cond_swap ? *r : l_src);
3130 }
3131 } else {
3132 emit_asm(ir, TGSI_OPCODE_MOV, *l, *r);
3133 }
3134 l->index++;
3135 r->index++;
3136 if (type->is_dual_slot()) {
3137 l->index++;
3138 if (r->is_double_vertex_input == false)
3139 r->index++;
3140 }
3141 }
3142
3143 void
3144 glsl_to_tgsi_visitor::visit(ir_assignment *ir)
3145 {
3146 int dst_component;
3147 st_dst_reg l;
3148 st_src_reg r;
3149
3150 ir->rhs->accept(this);
3151 r = this->result;
3152
3153 l = get_assignment_lhs(ir->lhs, this, &dst_component);
3154
3155 {
3156 int swizzles[4];
3157 int first_enabled_chan = 0;
3158 int rhs_chan = 0;
3159 ir_variable *variable = ir->lhs->variable_referenced();
3160
3161 if (shader->Stage == MESA_SHADER_FRAGMENT &&
3162 variable->data.mode == ir_var_shader_out &&
3163 (variable->data.location == FRAG_RESULT_DEPTH ||
3164 variable->data.location == FRAG_RESULT_STENCIL)) {
3165 assert(ir->lhs->type->is_scalar());
3166 assert(ir->write_mask == WRITEMASK_X);
3167
3168 if (variable->data.location == FRAG_RESULT_DEPTH)
3169 l.writemask = WRITEMASK_Z;
3170 else {
3171 assert(variable->data.location == FRAG_RESULT_STENCIL);
3172 l.writemask = WRITEMASK_Y;
3173 }
3174 } else if (ir->write_mask == 0) {
3175 assert(!ir->lhs->type->is_scalar() && !ir->lhs->type->is_vector());
3176
3177 unsigned num_elements = ir->lhs->type->without_array()->vector_elements;
3178
3179 if (num_elements) {
3180 l.writemask = u_bit_consecutive(0, num_elements);
3181 } else {
3182 /* The type is a struct or an array of (array of) structs. */
3183 l.writemask = WRITEMASK_XYZW;
3184 }
3185 } else {
3186 l.writemask = ir->write_mask;
3187 }
3188
3189 for (int i = 0; i < 4; i++) {
3190 if (l.writemask & (1 << i)) {
3191 first_enabled_chan = GET_SWZ(r.swizzle, i);
3192 break;
3193 }
3194 }
3195
3196 l.writemask = l.writemask << dst_component;
3197
3198 /* Swizzle a small RHS vector into the channels being written.
3199 *
3200 * glsl ir treats write_mask as dictating how many channels are
3201 * present on the RHS while TGSI treats write_mask as just
3202 * showing which channels of the vec4 RHS get written.
3203 */
3204 for (int i = 0; i < 4; i++) {
3205 if (l.writemask & (1 << i))
3206 swizzles[i] = GET_SWZ(r.swizzle, rhs_chan++);
3207 else
3208 swizzles[i] = first_enabled_chan;
3209 }
3210 r.swizzle = MAKE_SWIZZLE4(swizzles[0], swizzles[1],
3211 swizzles[2], swizzles[3]);
3212 }
3213
3214 assert(l.file != PROGRAM_UNDEFINED);
3215 assert(r.file != PROGRAM_UNDEFINED);
3216
3217 if (ir->condition) {
3218 const bool switch_order = this->process_move_condition(ir->condition);
3219 st_src_reg condition = this->result;
3220
3221 emit_block_mov(ir, ir->lhs->type, &l, &r, &condition, switch_order);
3222 } else if (ir->rhs->as_expression() &&
3223 this->instructions.get_tail() &&
3224 ir->rhs == ((glsl_to_tgsi_instruction *)this->instructions.get_tail())->ir &&
3225 !((glsl_to_tgsi_instruction *)this->instructions.get_tail())->is_64bit_expanded &&
3226 type_size(ir->lhs->type) == 1 &&
3227 l.writemask == ((glsl_to_tgsi_instruction *)this->instructions.get_tail())->dst[0].writemask) {
3228 /* To avoid emitting an extra MOV when assigning an expression to a
3229 * variable, emit the last instruction of the expression again, but
3230 * replace the destination register with the target of the assignment.
3231 * Dead code elimination will remove the original instruction.
3232 */
3233 glsl_to_tgsi_instruction *inst, *new_inst;
3234 inst = (glsl_to_tgsi_instruction *)this->instructions.get_tail();
3235 new_inst = emit_asm(ir, inst->op, l, inst->src[0], inst->src[1], inst->src[2], inst->src[3]);
3236 new_inst->saturate = inst->saturate;
3237 inst->dead_mask = inst->dst[0].writemask;
3238 } else {
3239 emit_block_mov(ir, ir->rhs->type, &l, &r, NULL, false);
3240 }
3241 }
3242
3243
3244 void
3245 glsl_to_tgsi_visitor::visit(ir_constant *ir)
3246 {
3247 st_src_reg src;
3248 GLdouble stack_vals[4] = { 0 };
3249 gl_constant_value *values = (gl_constant_value *) stack_vals;
3250 GLenum gl_type = GL_NONE;
3251 unsigned int i;
3252 static int in_array = 0;
3253 gl_register_file file = in_array ? PROGRAM_CONSTANT : PROGRAM_IMMEDIATE;
3254
3255 /* Unfortunately, 4 floats is all we can get into
3256 * _mesa_add_typed_unnamed_constant. So, make a temp to store an
3257 * aggregate constant and move each constant value into it. If we
3258 * get lucky, copy propagation will eliminate the extra moves.
3259 */
3260 if (ir->type->is_record()) {
3261 st_src_reg temp_base = get_temp(ir->type);
3262 st_dst_reg temp = st_dst_reg(temp_base);
3263
3264 foreach_in_list(ir_constant, field_value, &ir->components) {
3265 int size = type_size(field_value->type);
3266
3267 assert(size > 0);
3268
3269 field_value->accept(this);
3270 src = this->result;
3271
3272 for (i = 0; i < (unsigned int)size; i++) {
3273 emit_asm(ir, TGSI_OPCODE_MOV, temp, src);
3274
3275 src.index++;
3276 temp.index++;
3277 }
3278 }
3279 this->result = temp_base;
3280 return;
3281 }
3282
3283 if (ir->type->is_array()) {
3284 st_src_reg temp_base = get_temp(ir->type);
3285 st_dst_reg temp = st_dst_reg(temp_base);
3286 int size = type_size(ir->type->fields.array);
3287
3288 assert(size > 0);
3289 in_array++;
3290
3291 for (i = 0; i < ir->type->length; i++) {
3292 ir->array_elements[i]->accept(this);
3293 src = this->result;
3294 for (int j = 0; j < size; j++) {
3295 emit_asm(ir, TGSI_OPCODE_MOV, temp, src);
3296
3297 src.index++;
3298 temp.index++;
3299 }
3300 }
3301 this->result = temp_base;
3302 in_array--;
3303 return;
3304 }
3305
3306 if (ir->type->is_matrix()) {
3307 st_src_reg mat = get_temp(ir->type);
3308 st_dst_reg mat_column = st_dst_reg(mat);
3309
3310 for (i = 0; i < ir->type->matrix_columns; i++) {
3311 switch (ir->type->base_type) {
3312 case GLSL_TYPE_FLOAT:
3313 values = (gl_constant_value *) &ir->value.f[i * ir->type->vector_elements];
3314
3315 src = st_src_reg(file, -1, ir->type->base_type);
3316 src.index = add_constant(file,
3317 values,
3318 ir->type->vector_elements,
3319 GL_FLOAT,
3320 &src.swizzle);
3321 emit_asm(ir, TGSI_OPCODE_MOV, mat_column, src);
3322 break;
3323 case GLSL_TYPE_DOUBLE:
3324 values = (gl_constant_value *) &ir->value.d[i * ir->type->vector_elements];
3325 src = st_src_reg(file, -1, ir->type->base_type);
3326 src.index = add_constant(file,
3327 values,
3328 ir->type->vector_elements,
3329 GL_DOUBLE,
3330 &src.swizzle);
3331 if (ir->type->vector_elements >= 2) {
3332 mat_column.writemask = WRITEMASK_XY;
3333 src.swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_X, SWIZZLE_Y);
3334 emit_asm(ir, TGSI_OPCODE_MOV, mat_column, src);
3335 } else {
3336 mat_column.writemask = WRITEMASK_X;
3337 src.swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X);
3338 emit_asm(ir, TGSI_OPCODE_MOV, mat_column, src);
3339 }
3340 src.index++;
3341 if (ir->type->vector_elements > 2) {
3342 if (ir->type->vector_elements == 4) {
3343 mat_column.writemask = WRITEMASK_ZW;
3344 src.swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_X, SWIZZLE_Y);
3345 emit_asm(ir, TGSI_OPCODE_MOV, mat_column, src);
3346 } else {
3347 mat_column.writemask = WRITEMASK_Z;
3348 src.swizzle = MAKE_SWIZZLE4(SWIZZLE_Y, SWIZZLE_Y, SWIZZLE_Y, SWIZZLE_Y);
3349 emit_asm(ir, TGSI_OPCODE_MOV, mat_column, src);
3350 mat_column.writemask = WRITEMASK_XYZW;
3351 src.swizzle = SWIZZLE_XYZW;
3352 }
3353 mat_column.index++;
3354 }
3355 break;
3356 default:
3357 unreachable("Illegal matrix constant type.\n");
3358 break;
3359 }
3360 mat_column.index++;
3361 }
3362 this->result = mat;
3363 return;
3364 }
3365
3366 switch (ir->type->base_type) {
3367 case GLSL_TYPE_FLOAT:
3368 gl_type = GL_FLOAT;
3369 for (i = 0; i < ir->type->vector_elements; i++) {
3370 values[i].f = ir->value.f[i];
3371 }
3372 break;
3373 case GLSL_TYPE_DOUBLE:
3374 gl_type = GL_DOUBLE;
3375 for (i = 0; i < ir->type->vector_elements; i++) {
3376 memcpy(&values[i * 2], &ir->value.d[i], sizeof(double));
3377 }
3378 break;
3379 case GLSL_TYPE_INT64:
3380 gl_type = GL_INT64_ARB;
3381 for (i = 0; i < ir->type->vector_elements; i++) {
3382 memcpy(&values[i * 2], &ir->value.d[i], sizeof(int64_t));
3383 }
3384 break;
3385 case GLSL_TYPE_UINT64:
3386 gl_type = GL_UNSIGNED_INT64_ARB;
3387 for (i = 0; i < ir->type->vector_elements; i++) {
3388 memcpy(&values[i * 2], &ir->value.d[i], sizeof(uint64_t));
3389 }
3390 break;
3391 case GLSL_TYPE_UINT:
3392 gl_type = native_integers ? GL_UNSIGNED_INT : GL_FLOAT;
3393 for (i = 0; i < ir->type->vector_elements; i++) {
3394 if (native_integers)
3395 values[i].u = ir->value.u[i];
3396 else
3397 values[i].f = ir->value.u[i];
3398 }
3399 break;
3400 case GLSL_TYPE_INT:
3401 gl_type = native_integers ? GL_INT : GL_FLOAT;
3402 for (i = 0; i < ir->type->vector_elements; i++) {
3403 if (native_integers)
3404 values[i].i = ir->value.i[i];
3405 else
3406 values[i].f = ir->value.i[i];
3407 }
3408 break;
3409 case GLSL_TYPE_BOOL:
3410 gl_type = native_integers ? GL_BOOL : GL_FLOAT;
3411 for (i = 0; i < ir->type->vector_elements; i++) {
3412 values[i].u = ir->value.b[i] ? ctx->Const.UniformBooleanTrue : 0;
3413 }
3414 break;
3415 default:
3416 assert(!"Non-float/uint/int/bool constant");
3417 }
3418
3419 this->result = st_src_reg(file, -1, ir->type);
3420 this->result.index = add_constant(file,
3421 values,
3422 ir->type->vector_elements,
3423 gl_type,
3424 &this->result.swizzle);
3425 }
3426
3427 void
3428 glsl_to_tgsi_visitor::visit_atomic_counter_intrinsic(ir_call *ir)
3429 {
3430 exec_node *param = ir->actual_parameters.get_head();
3431 ir_dereference *deref = static_cast<ir_dereference *>(param);
3432 ir_variable *location = deref->variable_referenced();
3433
3434 st_src_reg buffer(
3435 PROGRAM_BUFFER, location->data.binding, GLSL_TYPE_ATOMIC_UINT);
3436
3437 /* Calculate the surface offset */
3438 st_src_reg offset;
3439 unsigned array_size = 0, base = 0;
3440 uint16_t index = 0;
3441
3442 get_deref_offsets(deref, &array_size, &base, &index, &offset, false);
3443
3444 if (offset.file != PROGRAM_UNDEFINED) {
3445 emit_asm(ir, TGSI_OPCODE_MUL, st_dst_reg(offset),
3446 offset, st_src_reg_for_int(ATOMIC_COUNTER_SIZE));
3447 emit_asm(ir, TGSI_OPCODE_ADD, st_dst_reg(offset),
3448 offset, st_src_reg_for_int(location->data.offset + index * ATOMIC_COUNTER_SIZE));
3449 } else {
3450 offset = st_src_reg_for_int(location->data.offset + index * ATOMIC_COUNTER_SIZE);
3451 }
3452
3453 ir->return_deref->accept(this);
3454 st_dst_reg dst(this->result);
3455 dst.writemask = WRITEMASK_X;
3456
3457 glsl_to_tgsi_instruction *inst;
3458
3459 if (ir->callee->intrinsic_id == ir_intrinsic_atomic_counter_read) {
3460 inst = emit_asm(ir, TGSI_OPCODE_LOAD, dst, offset);
3461 } else if (ir->callee->intrinsic_id == ir_intrinsic_atomic_counter_increment) {
3462 inst = emit_asm(ir, TGSI_OPCODE_ATOMUADD, dst, offset,
3463 st_src_reg_for_int(1));
3464 } else if (ir->callee->intrinsic_id == ir_intrinsic_atomic_counter_predecrement) {
3465 inst = emit_asm(ir, TGSI_OPCODE_ATOMUADD, dst, offset,
3466 st_src_reg_for_int(-1));
3467 emit_asm(ir, TGSI_OPCODE_ADD, dst, this->result, st_src_reg_for_int(-1));
3468 } else {
3469 param = param->get_next();
3470 ir_rvalue *val = ((ir_instruction *)param)->as_rvalue();
3471 val->accept(this);
3472
3473 st_src_reg data = this->result, data2 = undef_src;
3474 unsigned opcode;
3475 switch (ir->callee->intrinsic_id) {
3476 case ir_intrinsic_atomic_counter_add:
3477 opcode = TGSI_OPCODE_ATOMUADD;
3478 break;
3479 case ir_intrinsic_atomic_counter_min:
3480 opcode = TGSI_OPCODE_ATOMIMIN;
3481 break;
3482 case ir_intrinsic_atomic_counter_max:
3483 opcode = TGSI_OPCODE_ATOMIMAX;
3484 break;
3485 case ir_intrinsic_atomic_counter_and:
3486 opcode = TGSI_OPCODE_ATOMAND;
3487 break;
3488 case ir_intrinsic_atomic_counter_or:
3489 opcode = TGSI_OPCODE_ATOMOR;
3490 break;
3491 case ir_intrinsic_atomic_counter_xor:
3492 opcode = TGSI_OPCODE_ATOMXOR;
3493 break;
3494 case ir_intrinsic_atomic_counter_exchange:
3495 opcode = TGSI_OPCODE_ATOMXCHG;
3496 break;
3497 case ir_intrinsic_atomic_counter_comp_swap: {
3498 opcode = TGSI_OPCODE_ATOMCAS;
3499 param = param->get_next();
3500 val = ((ir_instruction *)param)->as_rvalue();
3501 val->accept(this);
3502 data2 = this->result;
3503 break;
3504 }
3505 default:
3506 assert(!"Unexpected intrinsic");
3507 return;
3508 }
3509
3510 inst = emit_asm(ir, opcode, dst, offset, data, data2);
3511 }
3512
3513 inst->resource = buffer;
3514 }
3515
3516 void
3517 glsl_to_tgsi_visitor::visit_ssbo_intrinsic(ir_call *ir)
3518 {
3519 exec_node *param = ir->actual_parameters.get_head();
3520
3521 ir_rvalue *block = ((ir_instruction *)param)->as_rvalue();
3522
3523 param = param->get_next();
3524 ir_rvalue *offset = ((ir_instruction *)param)->as_rvalue();
3525
3526 ir_constant *const_block = block->as_constant();
3527
3528 st_src_reg buffer(
3529 PROGRAM_BUFFER,
3530 ctx->Const.Program[shader->Stage].MaxAtomicBuffers +
3531 (const_block ? const_block->value.u[0] : 0),
3532 GLSL_TYPE_UINT);
3533
3534 if (!const_block) {
3535 block->accept(this);
3536 buffer.reladdr = ralloc(mem_ctx, st_src_reg);
3537 *buffer.reladdr = this->result;
3538 emit_arl(ir, sampler_reladdr, this->result);
3539 }
3540
3541 /* Calculate the surface offset */
3542 offset->accept(this);
3543 st_src_reg off = this->result;
3544
3545 st_dst_reg dst = undef_dst;
3546 if (ir->return_deref) {
3547 ir->return_deref->accept(this);
3548 dst = st_dst_reg(this->result);
3549 dst.writemask = (1 << ir->return_deref->type->vector_elements) - 1;
3550 }
3551
3552 glsl_to_tgsi_instruction *inst;
3553
3554 if (ir->callee->intrinsic_id == ir_intrinsic_ssbo_load) {
3555 inst = emit_asm(ir, TGSI_OPCODE_LOAD, dst, off);
3556 if (dst.type == GLSL_TYPE_BOOL)
3557 emit_asm(ir, TGSI_OPCODE_USNE, dst, st_src_reg(dst), st_src_reg_for_int(0));
3558 } else if (ir->callee->intrinsic_id == ir_intrinsic_ssbo_store) {
3559 param = param->get_next();
3560 ir_rvalue *val = ((ir_instruction *)param)->as_rvalue();
3561 val->accept(this);
3562
3563 param = param->get_next();
3564 ir_constant *write_mask = ((ir_instruction *)param)->as_constant();
3565 assert(write_mask);
3566 dst.writemask = write_mask->value.u[0];
3567
3568 dst.type = this->result.type;
3569 inst = emit_asm(ir, TGSI_OPCODE_STORE, dst, off, this->result);
3570 } else {
3571 param = param->get_next();
3572 ir_rvalue *val = ((ir_instruction *)param)->as_rvalue();
3573 val->accept(this);
3574
3575 st_src_reg data = this->result, data2 = undef_src;
3576 unsigned opcode;
3577 switch (ir->callee->intrinsic_id) {
3578 case ir_intrinsic_ssbo_atomic_add:
3579 opcode = TGSI_OPCODE_ATOMUADD;
3580 break;
3581 case ir_intrinsic_ssbo_atomic_min:
3582 opcode = TGSI_OPCODE_ATOMIMIN;
3583 break;
3584 case ir_intrinsic_ssbo_atomic_max:
3585 opcode = TGSI_OPCODE_ATOMIMAX;
3586 break;
3587 case ir_intrinsic_ssbo_atomic_and:
3588 opcode = TGSI_OPCODE_ATOMAND;
3589 break;
3590 case ir_intrinsic_ssbo_atomic_or:
3591 opcode = TGSI_OPCODE_ATOMOR;
3592 break;
3593 case ir_intrinsic_ssbo_atomic_xor:
3594 opcode = TGSI_OPCODE_ATOMXOR;
3595 break;
3596 case ir_intrinsic_ssbo_atomic_exchange:
3597 opcode = TGSI_OPCODE_ATOMXCHG;
3598 break;
3599 case ir_intrinsic_ssbo_atomic_comp_swap:
3600 opcode = TGSI_OPCODE_ATOMCAS;
3601 param = param->get_next();
3602 val = ((ir_instruction *)param)->as_rvalue();
3603 val->accept(this);
3604 data2 = this->result;
3605 break;
3606 default:
3607 assert(!"Unexpected intrinsic");
3608 return;
3609 }
3610
3611 inst = emit_asm(ir, opcode, dst, off, data, data2);
3612 }
3613
3614 param = param->get_next();
3615 ir_constant *access = NULL;
3616 if (!param->is_tail_sentinel()) {
3617 access = ((ir_instruction *)param)->as_constant();
3618 assert(access);
3619 }
3620
3621 /* The emit_asm() might have actually split the op into pieces, e.g. for
3622 * double stores. We have to go back and fix up all the generated ops.
3623 */
3624 unsigned op = inst->op;
3625 do {
3626 inst->resource = buffer;
3627 if (access)
3628 inst->buffer_access = access->value.u[0];
3629
3630 if (inst == this->instructions.get_head_raw())
3631 break;
3632 inst = (glsl_to_tgsi_instruction *)inst->get_prev();
3633
3634 if (inst->op == TGSI_OPCODE_UADD) {
3635 if (inst == this->instructions.get_head_raw())
3636 break;
3637 inst = (glsl_to_tgsi_instruction *)inst->get_prev();
3638 }
3639 } while (inst->op == op && inst->resource.file == PROGRAM_UNDEFINED);
3640 }
3641
3642 void
3643 glsl_to_tgsi_visitor::visit_membar_intrinsic(ir_call *ir)
3644 {
3645 switch (ir->callee->intrinsic_id) {
3646 case ir_intrinsic_memory_barrier:
3647 emit_asm(ir, TGSI_OPCODE_MEMBAR, undef_dst,
3648 st_src_reg_for_int(TGSI_MEMBAR_SHADER_BUFFER |
3649 TGSI_MEMBAR_ATOMIC_BUFFER |
3650 TGSI_MEMBAR_SHADER_IMAGE |
3651 TGSI_MEMBAR_SHARED));
3652 break;
3653 case ir_intrinsic_memory_barrier_atomic_counter:
3654 emit_asm(ir, TGSI_OPCODE_MEMBAR, undef_dst,
3655 st_src_reg_for_int(TGSI_MEMBAR_ATOMIC_BUFFER));
3656 break;
3657 case ir_intrinsic_memory_barrier_buffer:
3658 emit_asm(ir, TGSI_OPCODE_MEMBAR, undef_dst,
3659 st_src_reg_for_int(TGSI_MEMBAR_SHADER_BUFFER));
3660 break;
3661 case ir_intrinsic_memory_barrier_image:
3662 emit_asm(ir, TGSI_OPCODE_MEMBAR, undef_dst,
3663 st_src_reg_for_int(TGSI_MEMBAR_SHADER_IMAGE));
3664 break;
3665 case ir_intrinsic_memory_barrier_shared:
3666 emit_asm(ir, TGSI_OPCODE_MEMBAR, undef_dst,
3667 st_src_reg_for_int(TGSI_MEMBAR_SHARED));
3668 break;
3669 case ir_intrinsic_group_memory_barrier:
3670 emit_asm(ir, TGSI_OPCODE_MEMBAR, undef_dst,
3671 st_src_reg_for_int(TGSI_MEMBAR_SHADER_BUFFER |
3672 TGSI_MEMBAR_ATOMIC_BUFFER |
3673 TGSI_MEMBAR_SHADER_IMAGE |
3674 TGSI_MEMBAR_SHARED |
3675 TGSI_MEMBAR_THREAD_GROUP));
3676 break;
3677 default:
3678 assert(!"Unexpected memory barrier intrinsic");
3679 }
3680 }
3681
3682 void
3683 glsl_to_tgsi_visitor::visit_shared_intrinsic(ir_call *ir)
3684 {
3685 exec_node *param = ir->actual_parameters.get_head();
3686
3687 ir_rvalue *offset = ((ir_instruction *)param)->as_rvalue();
3688
3689 st_src_reg buffer(PROGRAM_MEMORY, 0, GLSL_TYPE_UINT);
3690
3691 /* Calculate the surface offset */
3692 offset->accept(this);
3693 st_src_reg off = this->result;
3694
3695 st_dst_reg dst = undef_dst;
3696 if (ir->return_deref) {
3697 ir->return_deref->accept(this);
3698 dst = st_dst_reg(this->result);
3699 dst.writemask = (1 << ir->return_deref->type->vector_elements) - 1;
3700 }
3701
3702 glsl_to_tgsi_instruction *inst;
3703
3704 if (ir->callee->intrinsic_id == ir_intrinsic_shared_load) {
3705 inst = emit_asm(ir, TGSI_OPCODE_LOAD, dst, off);
3706 inst->resource = buffer;
3707 } else if (ir->callee->intrinsic_id == ir_intrinsic_shared_store) {
3708 param = param->get_next();
3709 ir_rvalue *val = ((ir_instruction *)param)->as_rvalue();
3710 val->accept(this);
3711
3712 param = param->get_next();
3713 ir_constant *write_mask = ((ir_instruction *)param)->as_constant();
3714 assert(write_mask);
3715 dst.writemask = write_mask->value.u[0];
3716
3717 dst.type = this->result.type;
3718 inst = emit_asm(ir, TGSI_OPCODE_STORE, dst, off, this->result);
3719 inst->resource = buffer;
3720 } else {
3721 param = param->get_next();
3722 ir_rvalue *val = ((ir_instruction *)param)->as_rvalue();
3723 val->accept(this);
3724
3725 st_src_reg data = this->result, data2 = undef_src;
3726 unsigned opcode;
3727 switch (ir->callee->intrinsic_id) {
3728 case ir_intrinsic_shared_atomic_add:
3729 opcode = TGSI_OPCODE_ATOMUADD;
3730 break;
3731 case ir_intrinsic_shared_atomic_min:
3732 opcode = TGSI_OPCODE_ATOMIMIN;
3733 break;
3734 case ir_intrinsic_shared_atomic_max:
3735 opcode = TGSI_OPCODE_ATOMIMAX;
3736 break;
3737 case ir_intrinsic_shared_atomic_and:
3738 opcode = TGSI_OPCODE_ATOMAND;
3739 break;
3740 case ir_intrinsic_shared_atomic_or:
3741 opcode = TGSI_OPCODE_ATOMOR;
3742 break;
3743 case ir_intrinsic_shared_atomic_xor:
3744 opcode = TGSI_OPCODE_ATOMXOR;
3745 break;
3746 case ir_intrinsic_shared_atomic_exchange:
3747 opcode = TGSI_OPCODE_ATOMXCHG;
3748 break;
3749 case ir_intrinsic_shared_atomic_comp_swap:
3750 opcode = TGSI_OPCODE_ATOMCAS;
3751 param = param->get_next();
3752 val = ((ir_instruction *)param)->as_rvalue();
3753 val->accept(this);
3754 data2 = this->result;
3755 break;
3756 default:
3757 assert(!"Unexpected intrinsic");
3758 return;
3759 }
3760
3761 inst = emit_asm(ir, opcode, dst, off, data, data2);
3762 inst->resource = buffer;
3763 }
3764 }
3765
3766 static void
3767 get_image_qualifiers(ir_dereference *ir, const glsl_type **type,
3768 bool *memory_coherent, bool *memory_volatile,
3769 bool *memory_restrict, unsigned *image_format)
3770 {
3771
3772 switch (ir->ir_type) {
3773 case ir_type_dereference_record: {
3774 ir_dereference_record *deref_record = ir->as_dereference_record();
3775 const glsl_type *struct_type = deref_record->record->type;
3776
3777 for (unsigned i = 0; i < struct_type->length; i++) {
3778 if (!strcmp(struct_type->fields.structure[i].name,
3779 deref_record->field)) {
3780 *type = struct_type->fields.structure[i].type;
3781 *memory_coherent =
3782 struct_type->fields.structure[i].memory_coherent;
3783 *memory_volatile =
3784 struct_type->fields.structure[i].memory_volatile;
3785 *memory_restrict =
3786 struct_type->fields.structure[i].memory_restrict;
3787 *image_format =
3788 struct_type->fields.structure[i].image_format;
3789 break;
3790 }
3791 }
3792 break;
3793 }
3794
3795 case ir_type_dereference_array: {
3796 ir_dereference_array *deref_arr = ir->as_dereference_array();
3797 get_image_qualifiers((ir_dereference *)deref_arr->array, type,
3798 memory_coherent, memory_volatile, memory_restrict,
3799 image_format);
3800 break;
3801 }
3802
3803 case ir_type_dereference_variable: {
3804 ir_variable *var = ir->variable_referenced();
3805
3806 *type = var->type->without_array();
3807 *memory_coherent = var->data.memory_coherent;
3808 *memory_volatile = var->data.memory_volatile;
3809 *memory_restrict = var->data.memory_restrict;
3810 *image_format = var->data.image_format;
3811 break;
3812 }
3813
3814 default:
3815 break;
3816 }
3817 }
3818
3819 void
3820 glsl_to_tgsi_visitor::visit_image_intrinsic(ir_call *ir)
3821 {
3822 exec_node *param = ir->actual_parameters.get_head();
3823
3824 ir_dereference *img = (ir_dereference *)param;
3825 const ir_variable *imgvar = img->variable_referenced();
3826 unsigned sampler_array_size = 1, sampler_base = 0;
3827 bool memory_coherent = false, memory_volatile = false, memory_restrict = false;
3828 unsigned image_format = 0;
3829 const glsl_type *type = NULL;
3830
3831 get_image_qualifiers(img, &type, &memory_coherent, &memory_volatile,
3832 &memory_restrict, &image_format);
3833
3834 st_src_reg reladdr;
3835 st_src_reg image(PROGRAM_IMAGE, 0, GLSL_TYPE_UINT);
3836 uint16_t index = 0;
3837 get_deref_offsets(img, &sampler_array_size, &sampler_base,
3838 &index, &reladdr, !imgvar->contains_bindless());
3839
3840 image.index = index;
3841 if (reladdr.file != PROGRAM_UNDEFINED) {
3842 image.reladdr = ralloc(mem_ctx, st_src_reg);
3843 *image.reladdr = reladdr;
3844 emit_arl(ir, sampler_reladdr, reladdr);
3845 }
3846
3847 st_dst_reg dst = undef_dst;
3848 if (ir->return_deref) {
3849 ir->return_deref->accept(this);
3850 dst = st_dst_reg(this->result);
3851 dst.writemask = (1 << ir->return_deref->type->vector_elements) - 1;
3852 }
3853
3854 glsl_to_tgsi_instruction *inst;
3855
3856 if (ir->callee->intrinsic_id == ir_intrinsic_image_size) {
3857 dst.writemask = WRITEMASK_XYZ;
3858 inst = emit_asm(ir, TGSI_OPCODE_RESQ, dst);
3859 } else if (ir->callee->intrinsic_id == ir_intrinsic_image_samples) {
3860 st_src_reg res = get_temp(glsl_type::ivec4_type);
3861 st_dst_reg dstres = st_dst_reg(res);
3862 dstres.writemask = WRITEMASK_W;
3863 inst = emit_asm(ir, TGSI_OPCODE_RESQ, dstres);
3864 res.swizzle = SWIZZLE_WWWW;
3865 emit_asm(ir, TGSI_OPCODE_MOV, dst, res);
3866 } else {
3867 st_src_reg arg1 = undef_src, arg2 = undef_src;
3868 st_src_reg coord;
3869 st_dst_reg coord_dst;
3870 coord = get_temp(glsl_type::ivec4_type);
3871 coord_dst = st_dst_reg(coord);
3872 coord_dst.writemask = (1 << type->coordinate_components()) - 1;
3873 param = param->get_next();
3874 ((ir_dereference *)param)->accept(this);
3875 emit_asm(ir, TGSI_OPCODE_MOV, coord_dst, this->result);
3876 coord.swizzle = SWIZZLE_XXXX;
3877 switch (type->coordinate_components()) {
3878 case 4: assert(!"unexpected coord count");
3879 /* fallthrough */
3880 case 3: coord.swizzle |= SWIZZLE_Z << 6;
3881 /* fallthrough */
3882 case 2: coord.swizzle |= SWIZZLE_Y << 3;
3883 }
3884
3885 if (type->sampler_dimensionality == GLSL_SAMPLER_DIM_MS) {
3886 param = param->get_next();
3887 ((ir_dereference *)param)->accept(this);
3888 st_src_reg sample = this->result;
3889 sample.swizzle = SWIZZLE_XXXX;
3890 coord_dst.writemask = WRITEMASK_W;
3891 emit_asm(ir, TGSI_OPCODE_MOV, coord_dst, sample);
3892 coord.swizzle |= SWIZZLE_W << 9;
3893 }
3894
3895 param = param->get_next();
3896 if (!param->is_tail_sentinel()) {
3897 ((ir_dereference *)param)->accept(this);
3898 arg1 = this->result;
3899 param = param->get_next();
3900 }
3901
3902 if (!param->is_tail_sentinel()) {
3903 ((ir_dereference *)param)->accept(this);
3904 arg2 = this->result;
3905 param = param->get_next();
3906 }
3907
3908 assert(param->is_tail_sentinel());
3909
3910 unsigned opcode;
3911 switch (ir->callee->intrinsic_id) {
3912 case ir_intrinsic_image_load:
3913 opcode = TGSI_OPCODE_LOAD;
3914 break;
3915 case ir_intrinsic_image_store:
3916 opcode = TGSI_OPCODE_STORE;
3917 break;
3918 case ir_intrinsic_image_atomic_add:
3919 opcode = TGSI_OPCODE_ATOMUADD;
3920 break;
3921 case ir_intrinsic_image_atomic_min:
3922 opcode = TGSI_OPCODE_ATOMIMIN;
3923 break;
3924 case ir_intrinsic_image_atomic_max:
3925 opcode = TGSI_OPCODE_ATOMIMAX;
3926 break;
3927 case ir_intrinsic_image_atomic_and:
3928 opcode = TGSI_OPCODE_ATOMAND;
3929 break;
3930 case ir_intrinsic_image_atomic_or:
3931 opcode = TGSI_OPCODE_ATOMOR;
3932 break;
3933 case ir_intrinsic_image_atomic_xor:
3934 opcode = TGSI_OPCODE_ATOMXOR;
3935 break;
3936 case ir_intrinsic_image_atomic_exchange:
3937 opcode = TGSI_OPCODE_ATOMXCHG;
3938 break;
3939 case ir_intrinsic_image_atomic_comp_swap:
3940 opcode = TGSI_OPCODE_ATOMCAS;
3941 break;
3942 default:
3943 assert(!"Unexpected intrinsic");
3944 return;
3945 }
3946
3947 inst = emit_asm(ir, opcode, dst, coord, arg1, arg2);
3948 if (opcode == TGSI_OPCODE_STORE)
3949 inst->dst[0].writemask = WRITEMASK_XYZW;
3950 }
3951
3952 if (imgvar->contains_bindless()) {
3953 img->accept(this);
3954 inst->resource = this->result;
3955 inst->resource.swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y,
3956 SWIZZLE_X, SWIZZLE_Y);
3957 } else {
3958 inst->resource = image;
3959 inst->sampler_array_size = sampler_array_size;
3960 inst->sampler_base = sampler_base;
3961 }
3962
3963 inst->tex_target = type->sampler_index();
3964 inst->image_format = st_mesa_format_to_pipe_format(st_context(ctx),
3965 _mesa_get_shader_image_format(image_format));
3966
3967 if (memory_coherent)
3968 inst->buffer_access |= TGSI_MEMORY_COHERENT;
3969 if (memory_restrict)
3970 inst->buffer_access |= TGSI_MEMORY_RESTRICT;
3971 if (memory_volatile)
3972 inst->buffer_access |= TGSI_MEMORY_VOLATILE;
3973 }
3974
3975 void
3976 glsl_to_tgsi_visitor::visit_generic_intrinsic(ir_call *ir, unsigned op)
3977 {
3978 ir->return_deref->accept(this);
3979 st_dst_reg dst = st_dst_reg(this->result);
3980
3981 st_src_reg src[4] = { undef_src, undef_src, undef_src, undef_src };
3982 unsigned num_src = 0;
3983 foreach_in_list(ir_rvalue, param, &ir->actual_parameters) {
3984 assert(num_src < ARRAY_SIZE(src));
3985
3986 this->result.file = PROGRAM_UNDEFINED;
3987 param->accept(this);
3988 assert(this->result.file != PROGRAM_UNDEFINED);
3989
3990 src[num_src] = this->result;
3991 num_src++;
3992 }
3993
3994 emit_asm(ir, op, dst, src[0], src[1], src[2], src[3]);
3995 }
3996
3997 void
3998 glsl_to_tgsi_visitor::visit(ir_call *ir)
3999 {
4000 ir_function_signature *sig = ir->callee;
4001
4002 /* Filter out intrinsics */
4003 switch (sig->intrinsic_id) {
4004 case ir_intrinsic_atomic_counter_read:
4005 case ir_intrinsic_atomic_counter_increment:
4006 case ir_intrinsic_atomic_counter_predecrement:
4007 case ir_intrinsic_atomic_counter_add:
4008 case ir_intrinsic_atomic_counter_min:
4009 case ir_intrinsic_atomic_counter_max:
4010 case ir_intrinsic_atomic_counter_and:
4011 case ir_intrinsic_atomic_counter_or:
4012 case ir_intrinsic_atomic_counter_xor:
4013 case ir_intrinsic_atomic_counter_exchange:
4014 case ir_intrinsic_atomic_counter_comp_swap:
4015 visit_atomic_counter_intrinsic(ir);
4016 return;
4017
4018 case ir_intrinsic_ssbo_load:
4019 case ir_intrinsic_ssbo_store:
4020 case ir_intrinsic_ssbo_atomic_add:
4021 case ir_intrinsic_ssbo_atomic_min:
4022 case ir_intrinsic_ssbo_atomic_max:
4023 case ir_intrinsic_ssbo_atomic_and:
4024 case ir_intrinsic_ssbo_atomic_or:
4025 case ir_intrinsic_ssbo_atomic_xor:
4026 case ir_intrinsic_ssbo_atomic_exchange:
4027 case ir_intrinsic_ssbo_atomic_comp_swap:
4028 visit_ssbo_intrinsic(ir);
4029 return;
4030
4031 case ir_intrinsic_memory_barrier:
4032 case ir_intrinsic_memory_barrier_atomic_counter:
4033 case ir_intrinsic_memory_barrier_buffer:
4034 case ir_intrinsic_memory_barrier_image:
4035 case ir_intrinsic_memory_barrier_shared:
4036 case ir_intrinsic_group_memory_barrier:
4037 visit_membar_intrinsic(ir);
4038 return;
4039
4040 case ir_intrinsic_shared_load:
4041 case ir_intrinsic_shared_store:
4042 case ir_intrinsic_shared_atomic_add:
4043 case ir_intrinsic_shared_atomic_min:
4044 case ir_intrinsic_shared_atomic_max:
4045 case ir_intrinsic_shared_atomic_and:
4046 case ir_intrinsic_shared_atomic_or:
4047 case ir_intrinsic_shared_atomic_xor:
4048 case ir_intrinsic_shared_atomic_exchange:
4049 case ir_intrinsic_shared_atomic_comp_swap:
4050 visit_shared_intrinsic(ir);
4051 return;
4052
4053 case ir_intrinsic_image_load:
4054 case ir_intrinsic_image_store:
4055 case ir_intrinsic_image_atomic_add:
4056 case ir_intrinsic_image_atomic_min:
4057 case ir_intrinsic_image_atomic_max:
4058 case ir_intrinsic_image_atomic_and:
4059 case ir_intrinsic_image_atomic_or:
4060 case ir_intrinsic_image_atomic_xor:
4061 case ir_intrinsic_image_atomic_exchange:
4062 case ir_intrinsic_image_atomic_comp_swap:
4063 case ir_intrinsic_image_size:
4064 case ir_intrinsic_image_samples:
4065 visit_image_intrinsic(ir);
4066 return;
4067
4068 case ir_intrinsic_shader_clock:
4069 visit_generic_intrinsic(ir, TGSI_OPCODE_CLOCK);
4070 return;
4071
4072 case ir_intrinsic_vote_all:
4073 visit_generic_intrinsic(ir, TGSI_OPCODE_VOTE_ALL);
4074 return;
4075 case ir_intrinsic_vote_any:
4076 visit_generic_intrinsic(ir, TGSI_OPCODE_VOTE_ANY);
4077 return;
4078 case ir_intrinsic_vote_eq:
4079 visit_generic_intrinsic(ir, TGSI_OPCODE_VOTE_EQ);
4080 return;
4081 case ir_intrinsic_ballot:
4082 visit_generic_intrinsic(ir, TGSI_OPCODE_BALLOT);
4083 return;
4084 case ir_intrinsic_read_first_invocation:
4085 visit_generic_intrinsic(ir, TGSI_OPCODE_READ_FIRST);
4086 return;
4087 case ir_intrinsic_read_invocation:
4088 visit_generic_intrinsic(ir, TGSI_OPCODE_READ_INVOC);
4089 return;
4090
4091 case ir_intrinsic_invalid:
4092 case ir_intrinsic_generic_load:
4093 case ir_intrinsic_generic_store:
4094 case ir_intrinsic_generic_atomic_add:
4095 case ir_intrinsic_generic_atomic_and:
4096 case ir_intrinsic_generic_atomic_or:
4097 case ir_intrinsic_generic_atomic_xor:
4098 case ir_intrinsic_generic_atomic_min:
4099 case ir_intrinsic_generic_atomic_max:
4100 case ir_intrinsic_generic_atomic_exchange:
4101 case ir_intrinsic_generic_atomic_comp_swap:
4102 unreachable("Invalid intrinsic");
4103 }
4104 }
4105
4106 void
4107 glsl_to_tgsi_visitor::calc_deref_offsets(ir_dereference *tail,
4108 unsigned *array_elements,
4109 uint16_t *index,
4110 st_src_reg *indirect,
4111 unsigned *location)
4112 {
4113 switch (tail->ir_type) {
4114 case ir_type_dereference_record: {
4115 ir_dereference_record *deref_record = tail->as_dereference_record();
4116 const glsl_type *struct_type = deref_record->record->type;
4117 int field_index = deref_record->record->type->field_index(deref_record->field);
4118
4119 calc_deref_offsets(deref_record->record->as_dereference(), array_elements, index, indirect, location);
4120
4121 assert(field_index >= 0);
4122 *location += struct_type->record_location_offset(field_index);
4123 break;
4124 }
4125
4126 case ir_type_dereference_array: {
4127 ir_dereference_array *deref_arr = tail->as_dereference_array();
4128 ir_constant *array_index = deref_arr->array_index->constant_expression_value();
4129
4130 if (!array_index) {
4131 st_src_reg temp_reg;
4132 st_dst_reg temp_dst;
4133
4134 temp_reg = get_temp(glsl_type::uint_type);
4135 temp_dst = st_dst_reg(temp_reg);
4136 temp_dst.writemask = 1;
4137
4138 deref_arr->array_index->accept(this);
4139 if (*array_elements != 1)
4140 emit_asm(NULL, TGSI_OPCODE_MUL, temp_dst, this->result, st_src_reg_for_int(*array_elements));
4141 else
4142 emit_asm(NULL, TGSI_OPCODE_MOV, temp_dst, this->result);
4143
4144 if (indirect->file == PROGRAM_UNDEFINED)
4145 *indirect = temp_reg;
4146 else {
4147 temp_dst = st_dst_reg(*indirect);
4148 temp_dst.writemask = 1;
4149 emit_asm(NULL, TGSI_OPCODE_ADD, temp_dst, *indirect, temp_reg);
4150 }
4151 } else
4152 *index += array_index->value.u[0] * *array_elements;
4153
4154 *array_elements *= deref_arr->array->type->length;
4155
4156 calc_deref_offsets(deref_arr->array->as_dereference(), array_elements, index, indirect, location);
4157 break;
4158 }
4159 default:
4160 break;
4161 }
4162 }
4163
4164 void
4165 glsl_to_tgsi_visitor::get_deref_offsets(ir_dereference *ir,
4166 unsigned *array_size,
4167 unsigned *base,
4168 uint16_t *index,
4169 st_src_reg *reladdr,
4170 bool opaque)
4171 {
4172 GLuint shader = _mesa_program_enum_to_shader_stage(this->prog->Target);
4173 unsigned location = 0;
4174 ir_variable *var = ir->variable_referenced();
4175
4176 memset(reladdr, 0, sizeof(*reladdr));
4177 reladdr->file = PROGRAM_UNDEFINED;
4178
4179 *base = 0;
4180 *array_size = 1;
4181
4182 assert(var);
4183 location = var->data.location;
4184 calc_deref_offsets(ir, array_size, index, reladdr, &location);
4185
4186 /*
4187 * If we end up with no indirect then adjust the base to the index,
4188 * and set the array size to 1.
4189 */
4190 if (reladdr->file == PROGRAM_UNDEFINED) {
4191 *base = *index;
4192 *array_size = 1;
4193 }
4194
4195 if (opaque) {
4196 assert(location != 0xffffffff);
4197 *base += this->shader_program->data->UniformStorage[location].opaque[shader].index;
4198 *index += this->shader_program->data->UniformStorage[location].opaque[shader].index;
4199 }
4200 }
4201
4202 st_src_reg
4203 glsl_to_tgsi_visitor::canonicalize_gather_offset(st_src_reg offset)
4204 {
4205 if (offset.reladdr || offset.reladdr2) {
4206 st_src_reg tmp = get_temp(glsl_type::ivec2_type);
4207 st_dst_reg tmp_dst = st_dst_reg(tmp);
4208 tmp_dst.writemask = WRITEMASK_XY;
4209 emit_asm(NULL, TGSI_OPCODE_MOV, tmp_dst, offset);
4210 return tmp;
4211 }
4212
4213 return offset;
4214 }
4215
4216 void
4217 glsl_to_tgsi_visitor::visit(ir_texture *ir)
4218 {
4219 st_src_reg result_src, coord, cube_sc, lod_info, projector, dx, dy;
4220 st_src_reg offset[MAX_GLSL_TEXTURE_OFFSET], sample_index, component;
4221 st_src_reg levels_src, reladdr;
4222 st_dst_reg result_dst, coord_dst, cube_sc_dst;
4223 glsl_to_tgsi_instruction *inst = NULL;
4224 unsigned opcode = TGSI_OPCODE_NOP;
4225 const glsl_type *sampler_type = ir->sampler->type;
4226 unsigned sampler_array_size = 1, sampler_base = 0;
4227 bool is_cube_array = false, is_cube_shadow = false;
4228 ir_variable *var = ir->sampler->variable_referenced();
4229 unsigned i;
4230
4231 /* if we are a cube array sampler or a cube shadow */
4232 if (sampler_type->sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE) {
4233 is_cube_array = sampler_type->sampler_array;
4234 is_cube_shadow = sampler_type->sampler_shadow;
4235 }
4236
4237 if (ir->coordinate) {
4238 ir->coordinate->accept(this);
4239
4240 /* Put our coords in a temp. We'll need to modify them for shadow,
4241 * projection, or LOD, so the only case we'd use it as-is is if
4242 * we're doing plain old texturing. The optimization passes on
4243 * glsl_to_tgsi_visitor should handle cleaning up our mess in that case.
4244 */
4245 coord = get_temp(glsl_type::vec4_type);
4246 coord_dst = st_dst_reg(coord);
4247 coord_dst.writemask = (1 << ir->coordinate->type->vector_elements) - 1;
4248 emit_asm(ir, TGSI_OPCODE_MOV, coord_dst, this->result);
4249 }
4250
4251 if (ir->projector) {
4252 ir->projector->accept(this);
4253 projector = this->result;
4254 }
4255
4256 /* Storage for our result. Ideally for an assignment we'd be using
4257 * the actual storage for the result here, instead.
4258 */
4259 result_src = get_temp(ir->type);
4260 result_dst = st_dst_reg(result_src);
4261 result_dst.writemask = (1 << ir->type->vector_elements) - 1;
4262
4263 switch (ir->op) {
4264 case ir_tex:
4265 opcode = (is_cube_array && ir->shadow_comparator) ? TGSI_OPCODE_TEX2 : TGSI_OPCODE_TEX;
4266 if (ir->offset) {
4267 ir->offset->accept(this);
4268 offset[0] = this->result;
4269 }
4270 break;
4271 case ir_txb:
4272 if (is_cube_array || is_cube_shadow) {
4273 opcode = TGSI_OPCODE_TXB2;
4274 }
4275 else {
4276 opcode = TGSI_OPCODE_TXB;
4277 }
4278 ir->lod_info.bias->accept(this);
4279 lod_info = this->result;
4280 if (ir->offset) {
4281 ir->offset->accept(this);
4282 offset[0] = this->result;
4283 }
4284 break;
4285 case ir_txl:
4286 if (this->has_tex_txf_lz && ir->lod_info.lod->is_zero()) {
4287 opcode = TGSI_OPCODE_TEX_LZ;
4288 } else {
4289 opcode = is_cube_array ? TGSI_OPCODE_TXL2 : TGSI_OPCODE_TXL;
4290 ir->lod_info.lod->accept(this);
4291 lod_info = this->result;
4292 }
4293 if (ir->offset) {
4294 ir->offset->accept(this);
4295 offset[0] = this->result;
4296 }
4297 break;
4298 case ir_txd:
4299 opcode = TGSI_OPCODE_TXD;
4300 ir->lod_info.grad.dPdx->accept(this);
4301 dx = this->result;
4302 ir->lod_info.grad.dPdy->accept(this);
4303 dy = this->result;
4304 if (ir->offset) {
4305 ir->offset->accept(this);
4306 offset[0] = this->result;
4307 }
4308 break;
4309 case ir_txs:
4310 opcode = TGSI_OPCODE_TXQ;
4311 ir->lod_info.lod->accept(this);
4312 lod_info = this->result;
4313 break;
4314 case ir_query_levels:
4315 opcode = TGSI_OPCODE_TXQ;
4316 lod_info = undef_src;
4317 levels_src = get_temp(ir->type);
4318 break;
4319 case ir_txf:
4320 if (this->has_tex_txf_lz && ir->lod_info.lod->is_zero()) {
4321 opcode = TGSI_OPCODE_TXF_LZ;
4322 } else {
4323 opcode = TGSI_OPCODE_TXF;
4324 ir->lod_info.lod->accept(this);
4325 lod_info = this->result;
4326 }
4327 if (ir->offset) {
4328 ir->offset->accept(this);
4329 offset[0] = this->result;
4330 }
4331 break;
4332 case ir_txf_ms:
4333 opcode = TGSI_OPCODE_TXF;
4334 ir->lod_info.sample_index->accept(this);
4335 sample_index = this->result;
4336 break;
4337 case ir_tg4:
4338 opcode = TGSI_OPCODE_TG4;
4339 ir->lod_info.component->accept(this);
4340 component = this->result;
4341 if (ir->offset) {
4342 ir->offset->accept(this);
4343 if (ir->offset->type->is_array()) {
4344 const glsl_type *elt_type = ir->offset->type->fields.array;
4345 for (i = 0; i < ir->offset->type->length; i++) {
4346 offset[i] = this->result;
4347 offset[i].index += i * type_size(elt_type);
4348 offset[i].type = elt_type->base_type;
4349 offset[i].swizzle = swizzle_for_size(elt_type->vector_elements);
4350 offset[i] = canonicalize_gather_offset(offset[i]);
4351 }
4352 } else {
4353 offset[0] = canonicalize_gather_offset(this->result);
4354 }
4355 }
4356 break;
4357 case ir_lod:
4358 opcode = TGSI_OPCODE_LODQ;
4359 break;
4360 case ir_texture_samples:
4361 opcode = TGSI_OPCODE_TXQS;
4362 break;
4363 case ir_samples_identical:
4364 unreachable("Unexpected ir_samples_identical opcode");
4365 }
4366
4367 if (ir->projector) {
4368 if (opcode == TGSI_OPCODE_TEX) {
4369 /* Slot the projector in as the last component of the coord. */
4370 coord_dst.writemask = WRITEMASK_W;
4371 emit_asm(ir, TGSI_OPCODE_MOV, coord_dst, projector);
4372 coord_dst.writemask = WRITEMASK_XYZW;
4373 opcode = TGSI_OPCODE_TXP;
4374 } else {
4375 st_src_reg coord_w = coord;
4376 coord_w.swizzle = SWIZZLE_WWWW;
4377
4378 /* For the other TEX opcodes there's no projective version
4379 * since the last slot is taken up by LOD info. Do the
4380 * projective divide now.
4381 */
4382 coord_dst.writemask = WRITEMASK_W;
4383 emit_asm(ir, TGSI_OPCODE_RCP, coord_dst, projector);
4384
4385 /* In the case where we have to project the coordinates "by hand,"
4386 * the shadow comparator value must also be projected.
4387 */
4388 st_src_reg tmp_src = coord;
4389 if (ir->shadow_comparator) {
4390 /* Slot the shadow value in as the second to last component of the
4391 * coord.
4392 */
4393 ir->shadow_comparator->accept(this);
4394
4395 tmp_src = get_temp(glsl_type::vec4_type);
4396 st_dst_reg tmp_dst = st_dst_reg(tmp_src);
4397
4398 /* Projective division not allowed for array samplers. */
4399 assert(!sampler_type->sampler_array);
4400
4401 tmp_dst.writemask = WRITEMASK_Z;
4402 emit_asm(ir, TGSI_OPCODE_MOV, tmp_dst, this->result);
4403
4404 tmp_dst.writemask = WRITEMASK_XY;
4405 emit_asm(ir, TGSI_OPCODE_MOV, tmp_dst, coord);
4406 }
4407
4408 coord_dst.writemask = WRITEMASK_XYZ;
4409 emit_asm(ir, TGSI_OPCODE_MUL, coord_dst, tmp_src, coord_w);
4410
4411 coord_dst.writemask = WRITEMASK_XYZW;
4412 coord.swizzle = SWIZZLE_XYZW;
4413 }
4414 }
4415
4416 /* If projection is done and the opcode is not TGSI_OPCODE_TXP, then the shadow
4417 * comparator was put in the correct place (and projected) by the code,
4418 * above, that handles by-hand projection.
4419 */
4420 if (ir->shadow_comparator && (!ir->projector || opcode == TGSI_OPCODE_TXP)) {
4421 /* Slot the shadow value in as the second to last component of the
4422 * coord.
4423 */
4424 ir->shadow_comparator->accept(this);
4425
4426 if (is_cube_array) {
4427 cube_sc = get_temp(glsl_type::float_type);
4428 cube_sc_dst = st_dst_reg(cube_sc);
4429 cube_sc_dst.writemask = WRITEMASK_X;
4430 emit_asm(ir, TGSI_OPCODE_MOV, cube_sc_dst, this->result);
4431 cube_sc_dst.writemask = WRITEMASK_X;
4432 }
4433 else {
4434 if ((sampler_type->sampler_dimensionality == GLSL_SAMPLER_DIM_2D &&
4435 sampler_type->sampler_array) ||
4436 sampler_type->sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE) {
4437 coord_dst.writemask = WRITEMASK_W;
4438 } else {
4439 coord_dst.writemask = WRITEMASK_Z;
4440 }
4441 emit_asm(ir, TGSI_OPCODE_MOV, coord_dst, this->result);
4442 coord_dst.writemask = WRITEMASK_XYZW;
4443 }
4444 }
4445
4446 if (ir->op == ir_txf_ms) {
4447 coord_dst.writemask = WRITEMASK_W;
4448 emit_asm(ir, TGSI_OPCODE_MOV, coord_dst, sample_index);
4449 coord_dst.writemask = WRITEMASK_XYZW;
4450 } else if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXB ||
4451 opcode == TGSI_OPCODE_TXF) {
4452 /* TGSI stores LOD or LOD bias in the last channel of the coords. */
4453 coord_dst.writemask = WRITEMASK_W;
4454 emit_asm(ir, TGSI_OPCODE_MOV, coord_dst, lod_info);
4455 coord_dst.writemask = WRITEMASK_XYZW;
4456 }
4457
4458 st_src_reg sampler(PROGRAM_SAMPLER, 0, GLSL_TYPE_UINT);
4459
4460 uint16_t index = 0;
4461 get_deref_offsets(ir->sampler, &sampler_array_size, &sampler_base,
4462 &index, &reladdr, !var->contains_bindless());
4463
4464 sampler.index = index;
4465 if (reladdr.file != PROGRAM_UNDEFINED) {
4466 sampler.reladdr = ralloc(mem_ctx, st_src_reg);
4467 *sampler.reladdr = reladdr;
4468 emit_arl(ir, sampler_reladdr, reladdr);
4469 }
4470
4471 if (opcode == TGSI_OPCODE_TXD)
4472 inst = emit_asm(ir, opcode, result_dst, coord, dx, dy);
4473 else if (opcode == TGSI_OPCODE_TXQ) {
4474 if (ir->op == ir_query_levels) {
4475 /* the level is stored in W */
4476 inst = emit_asm(ir, opcode, st_dst_reg(levels_src), lod_info);
4477 result_dst.writemask = WRITEMASK_X;
4478 levels_src.swizzle = SWIZZLE_WWWW;
4479 emit_asm(ir, TGSI_OPCODE_MOV, result_dst, levels_src);
4480 } else
4481 inst = emit_asm(ir, opcode, result_dst, lod_info);
4482 } else if (opcode == TGSI_OPCODE_TXQS) {
4483 inst = emit_asm(ir, opcode, result_dst);
4484 } else if (opcode == TGSI_OPCODE_TXL2 || opcode == TGSI_OPCODE_TXB2) {
4485 inst = emit_asm(ir, opcode, result_dst, coord, lod_info);
4486 } else if (opcode == TGSI_OPCODE_TEX2) {
4487 inst = emit_asm(ir, opcode, result_dst, coord, cube_sc);
4488 } else if (opcode == TGSI_OPCODE_TG4) {
4489 if (is_cube_array && ir->shadow_comparator) {
4490 inst = emit_asm(ir, opcode, result_dst, coord, cube_sc);
4491 } else {
4492 inst = emit_asm(ir, opcode, result_dst, coord, component);
4493 }
4494 } else
4495 inst = emit_asm(ir, opcode, result_dst, coord);
4496
4497 if (ir->shadow_comparator)
4498 inst->tex_shadow = GL_TRUE;
4499
4500 if (var->contains_bindless()) {
4501 ir->sampler->accept(this);
4502 inst->resource = this->result;
4503 inst->resource.swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y,
4504 SWIZZLE_X, SWIZZLE_Y);
4505 } else {
4506 inst->resource = sampler;
4507 inst->sampler_array_size = sampler_array_size;
4508 inst->sampler_base = sampler_base;
4509 }
4510
4511 if (ir->offset) {
4512 if (!inst->tex_offsets)
4513 inst->tex_offsets = rzalloc_array(inst, st_src_reg, MAX_GLSL_TEXTURE_OFFSET);
4514
4515 for (i = 0; i < MAX_GLSL_TEXTURE_OFFSET && offset[i].file != PROGRAM_UNDEFINED; i++)
4516 inst->tex_offsets[i] = offset[i];
4517 inst->tex_offset_num_offset = i;
4518 }
4519
4520 inst->tex_target = sampler_type->sampler_index();
4521 inst->tex_type = ir->type->base_type;
4522
4523 this->result = result_src;
4524 }
4525
4526 void
4527 glsl_to_tgsi_visitor::visit(ir_return *ir)
4528 {
4529 assert(!ir->get_value());
4530
4531 emit_asm(ir, TGSI_OPCODE_RET);
4532 }
4533
4534 void
4535 glsl_to_tgsi_visitor::visit(ir_discard *ir)
4536 {
4537 if (ir->condition) {
4538 ir->condition->accept(this);
4539 st_src_reg condition = this->result;
4540
4541 /* Convert the bool condition to a float so we can negate. */
4542 if (native_integers) {
4543 st_src_reg temp = get_temp(ir->condition->type);
4544 emit_asm(ir, TGSI_OPCODE_AND, st_dst_reg(temp),
4545 condition, st_src_reg_for_float(1.0));
4546 condition = temp;
4547 }
4548
4549 condition.negate = ~condition.negate;
4550 emit_asm(ir, TGSI_OPCODE_KILL_IF, undef_dst, condition);
4551 } else {
4552 /* unconditional kil */
4553 emit_asm(ir, TGSI_OPCODE_KILL);
4554 }
4555 }
4556
4557 void
4558 glsl_to_tgsi_visitor::visit(ir_if *ir)
4559 {
4560 unsigned if_opcode;
4561 glsl_to_tgsi_instruction *if_inst;
4562
4563 ir->condition->accept(this);
4564 assert(this->result.file != PROGRAM_UNDEFINED);
4565
4566 if_opcode = native_integers ? TGSI_OPCODE_UIF : TGSI_OPCODE_IF;
4567
4568 if_inst = emit_asm(ir->condition, if_opcode, undef_dst, this->result);
4569
4570 this->instructions.push_tail(if_inst);
4571
4572 visit_exec_list(&ir->then_instructions, this);
4573
4574 if (!ir->else_instructions.is_empty()) {
4575 emit_asm(ir->condition, TGSI_OPCODE_ELSE);
4576 visit_exec_list(&ir->else_instructions, this);
4577 }
4578
4579 if_inst = emit_asm(ir->condition, TGSI_OPCODE_ENDIF);
4580 }
4581
4582
4583 void
4584 glsl_to_tgsi_visitor::visit(ir_emit_vertex *ir)
4585 {
4586 assert(this->prog->Target == GL_GEOMETRY_PROGRAM_NV);
4587
4588 ir->stream->accept(this);
4589 emit_asm(ir, TGSI_OPCODE_EMIT, undef_dst, this->result);
4590 }
4591
4592 void
4593 glsl_to_tgsi_visitor::visit(ir_end_primitive *ir)
4594 {
4595 assert(this->prog->Target == GL_GEOMETRY_PROGRAM_NV);
4596
4597 ir->stream->accept(this);
4598 emit_asm(ir, TGSI_OPCODE_ENDPRIM, undef_dst, this->result);
4599 }
4600
4601 void
4602 glsl_to_tgsi_visitor::visit(ir_barrier *ir)
4603 {
4604 assert(this->prog->Target == GL_TESS_CONTROL_PROGRAM_NV ||
4605 this->prog->Target == GL_COMPUTE_PROGRAM_NV);
4606
4607 emit_asm(ir, TGSI_OPCODE_BARRIER);
4608 }
4609
4610 glsl_to_tgsi_visitor::glsl_to_tgsi_visitor()
4611 {
4612 STATIC_ASSERT(sizeof(samplers_used) * 8 >= PIPE_MAX_SAMPLERS);
4613
4614 result.file = PROGRAM_UNDEFINED;
4615 next_temp = 1;
4616 array_sizes = NULL;
4617 max_num_arrays = 0;
4618 next_array = 0;
4619 num_inputs = 0;
4620 num_outputs = 0;
4621 num_input_arrays = 0;
4622 num_output_arrays = 0;
4623 num_immediates = 0;
4624 num_address_regs = 0;
4625 samplers_used = 0;
4626 images_used = 0;
4627 indirect_addr_consts = false;
4628 wpos_transform_const = -1;
4629 glsl_version = 0;
4630 native_integers = false;
4631 mem_ctx = ralloc_context(NULL);
4632 ctx = NULL;
4633 prog = NULL;
4634 shader_program = NULL;
4635 shader = NULL;
4636 options = NULL;
4637 have_sqrt = false;
4638 have_fma = false;
4639 use_shared_memory = false;
4640 has_tex_txf_lz = false;
4641 variables = NULL;
4642 }
4643
4644 static void var_destroy(struct hash_entry *entry)
4645 {
4646 variable_storage *storage = (variable_storage *)entry->data;
4647
4648 delete storage;
4649 }
4650
4651 glsl_to_tgsi_visitor::~glsl_to_tgsi_visitor()
4652 {
4653 _mesa_hash_table_destroy(variables, var_destroy);
4654 free(array_sizes);
4655 ralloc_free(mem_ctx);
4656 }
4657
4658 extern "C" void free_glsl_to_tgsi_visitor(glsl_to_tgsi_visitor *v)
4659 {
4660 delete v;
4661 }
4662
4663
4664 /**
4665 * Count resources used by the given gpu program (number of texture
4666 * samplers, etc).
4667 */
4668 static void
4669 count_resources(glsl_to_tgsi_visitor *v, gl_program *prog)
4670 {
4671 v->samplers_used = 0;
4672 v->images_used = 0;
4673
4674 foreach_in_list(glsl_to_tgsi_instruction, inst, &v->instructions) {
4675 if (inst->info->is_tex) {
4676 for (int i = 0; i < inst->sampler_array_size; i++) {
4677 unsigned idx = inst->sampler_base + i;
4678 v->samplers_used |= 1u << idx;
4679
4680 debug_assert(idx < (int)ARRAY_SIZE(v->sampler_types));
4681 v->sampler_types[idx] = inst->tex_type;
4682 v->sampler_targets[idx] =
4683 st_translate_texture_target(inst->tex_target, inst->tex_shadow);
4684
4685 if (inst->tex_shadow) {
4686 prog->ShadowSamplers |= 1 << (inst->resource.index + i);
4687 }
4688 }
4689 }
4690
4691 if (inst->tex_target == TEXTURE_EXTERNAL_INDEX)
4692 prog->ExternalSamplersUsed |= 1 << inst->resource.index;
4693
4694 if (inst->resource.file != PROGRAM_UNDEFINED && (
4695 is_resource_instruction(inst->op) ||
4696 inst->op == TGSI_OPCODE_STORE)) {
4697 if (inst->resource.file == PROGRAM_MEMORY) {
4698 v->use_shared_memory = true;
4699 } else if (inst->resource.file == PROGRAM_IMAGE) {
4700 for (int i = 0; i < inst->sampler_array_size; i++) {
4701 unsigned idx = inst->sampler_base + i;
4702 v->images_used |= 1 << idx;
4703 v->image_targets[idx] =
4704 st_translate_texture_target(inst->tex_target, false);
4705 v->image_formats[idx] = inst->image_format;
4706 }
4707 }
4708 }
4709 }
4710 prog->SamplersUsed = v->samplers_used;
4711
4712 if (v->shader_program != NULL)
4713 _mesa_update_shader_textures_used(v->shader_program, prog);
4714 }
4715
4716 /**
4717 * Returns the mask of channels (bitmask of WRITEMASK_X,Y,Z,W) which
4718 * are read from the given src in this instruction
4719 */
4720 static int
4721 get_src_arg_mask(st_dst_reg dst, st_src_reg src)
4722 {
4723 int read_mask = 0, comp;
4724
4725 /* Now, given the src swizzle and the written channels, find which
4726 * components are actually read
4727 */
4728 for (comp = 0; comp < 4; ++comp) {
4729 const unsigned coord = GET_SWZ(src.swizzle, comp);
4730 assert(coord < 4);
4731 if (dst.writemask & (1 << comp) && coord <= SWIZZLE_W)
4732 read_mask |= 1 << coord;
4733 }
4734
4735 return read_mask;
4736 }
4737
4738 /**
4739 * This pass replaces CMP T0, T1 T2 T0 with MOV T0, T2 when the CMP
4740 * instruction is the first instruction to write to register T0. There are
4741 * several lowering passes done in GLSL IR (e.g. branches and
4742 * relative addressing) that create a large number of conditional assignments
4743 * that ir_to_mesa converts to CMP instructions like the one mentioned above.
4744 *
4745 * Here is why this conversion is safe:
4746 * CMP T0, T1 T2 T0 can be expanded to:
4747 * if (T1 < 0.0)
4748 * MOV T0, T2;
4749 * else
4750 * MOV T0, T0;
4751 *
4752 * If (T1 < 0.0) evaluates to true then our replacement MOV T0, T2 is the same
4753 * as the original program. If (T1 < 0.0) evaluates to false, executing
4754 * MOV T0, T0 will store a garbage value in T0 since T0 is uninitialized.
4755 * Therefore, it doesn't matter that we are replacing MOV T0, T0 with MOV T0, T2
4756 * because any instruction that was going to read from T0 after this was going
4757 * to read a garbage value anyway.
4758 */
4759 void
4760 glsl_to_tgsi_visitor::simplify_cmp(void)
4761 {
4762 int tempWritesSize = 0;
4763 unsigned *tempWrites = NULL;
4764 unsigned outputWrites[VARYING_SLOT_TESS_MAX];
4765
4766 memset(outputWrites, 0, sizeof(outputWrites));
4767
4768 foreach_in_list(glsl_to_tgsi_instruction, inst, &this->instructions) {
4769 unsigned prevWriteMask = 0;
4770
4771 /* Give up if we encounter relative addressing or flow control. */
4772 if (inst->dst[0].reladdr || inst->dst[0].reladdr2 ||
4773 inst->dst[1].reladdr || inst->dst[1].reladdr2 ||
4774 inst->info->is_branch ||
4775 inst->op == TGSI_OPCODE_CONT ||
4776 inst->op == TGSI_OPCODE_END ||
4777 inst->op == TGSI_OPCODE_RET) {
4778 break;
4779 }
4780
4781 if (inst->dst[0].file == PROGRAM_OUTPUT) {
4782 assert(inst->dst[0].index < (signed)ARRAY_SIZE(outputWrites));
4783 prevWriteMask = outputWrites[inst->dst[0].index];
4784 outputWrites[inst->dst[0].index] |= inst->dst[0].writemask;
4785 } else if (inst->dst[0].file == PROGRAM_TEMPORARY) {
4786 if (inst->dst[0].index >= tempWritesSize) {
4787 const int inc = 4096;
4788
4789 tempWrites = (unsigned*)
4790 realloc(tempWrites,
4791 (tempWritesSize + inc) * sizeof(unsigned));
4792 if (!tempWrites)
4793 return;
4794
4795 memset(tempWrites + tempWritesSize, 0, inc * sizeof(unsigned));
4796 tempWritesSize += inc;
4797 }
4798
4799 prevWriteMask = tempWrites[inst->dst[0].index];
4800 tempWrites[inst->dst[0].index] |= inst->dst[0].writemask;
4801 } else
4802 continue;
4803
4804 /* For a CMP to be considered a conditional write, the destination
4805 * register and source register two must be the same. */
4806 if (inst->op == TGSI_OPCODE_CMP
4807 && !(inst->dst[0].writemask & prevWriteMask)
4808 && inst->src[2].file == inst->dst[0].file
4809 && inst->src[2].index == inst->dst[0].index
4810 && inst->dst[0].writemask == get_src_arg_mask(inst->dst[0], inst->src[2])) {
4811
4812 inst->op = TGSI_OPCODE_MOV;
4813 inst->info = tgsi_get_opcode_info(inst->op);
4814 inst->src[0] = inst->src[1];
4815 }
4816 }
4817
4818 free(tempWrites);
4819 }
4820
4821 /* Replaces all references to a temporary register index with another index. */
4822 void
4823 glsl_to_tgsi_visitor::rename_temp_registers(struct rename_reg_pair *renames)
4824 {
4825 foreach_in_list(glsl_to_tgsi_instruction, inst, &this->instructions) {
4826 unsigned j;
4827 for (j = 0; j < num_inst_src_regs(inst); j++) {
4828 if (inst->src[j].file == PROGRAM_TEMPORARY) {
4829 int old_idx = inst->src[j].index;
4830 if (renames[old_idx].valid)
4831 inst->src[j].index = renames[old_idx].new_reg;
4832 }
4833 }
4834
4835 for (j = 0; j < inst->tex_offset_num_offset; j++) {
4836 if (inst->tex_offsets[j].file == PROGRAM_TEMPORARY) {
4837 int old_idx = inst->tex_offsets[j].index;
4838 if (renames[old_idx].valid)
4839 inst->tex_offsets[j].index = renames[old_idx].new_reg;
4840 }
4841 }
4842
4843 for (j = 0; j < num_inst_dst_regs(inst); j++) {
4844 if (inst->dst[j].file == PROGRAM_TEMPORARY) {
4845 int old_idx = inst->dst[j].index;
4846 if (renames[old_idx].valid)
4847 inst->dst[j].index = renames[old_idx].new_reg;}
4848 }
4849 }
4850 }
4851
4852 void
4853 glsl_to_tgsi_visitor::get_first_temp_write(int *first_writes)
4854 {
4855 int depth = 0; /* loop depth */
4856 int loop_start = -1; /* index of the first active BGNLOOP (if any) */
4857 unsigned i = 0, j;
4858
4859 foreach_in_list(glsl_to_tgsi_instruction, inst, &this->instructions) {
4860 for (j = 0; j < num_inst_dst_regs(inst); j++) {
4861 if (inst->dst[j].file == PROGRAM_TEMPORARY) {
4862 if (first_writes[inst->dst[j].index] == -1)
4863 first_writes[inst->dst[j].index] = (depth == 0) ? i : loop_start;
4864 }
4865 }
4866
4867 if (inst->op == TGSI_OPCODE_BGNLOOP) {
4868 if(depth++ == 0)
4869 loop_start = i;
4870 } else if (inst->op == TGSI_OPCODE_ENDLOOP) {
4871 if (--depth == 0)
4872 loop_start = -1;
4873 }
4874 assert(depth >= 0);
4875 i++;
4876 }
4877 }
4878
4879 void
4880 glsl_to_tgsi_visitor::get_first_temp_read(int *first_reads)
4881 {
4882 int depth = 0; /* loop depth */
4883 int loop_start = -1; /* index of the first active BGNLOOP (if any) */
4884 unsigned i = 0, j;
4885
4886 foreach_in_list(glsl_to_tgsi_instruction, inst, &this->instructions) {
4887 for (j = 0; j < num_inst_src_regs(inst); j++) {
4888 if (inst->src[j].file == PROGRAM_TEMPORARY) {
4889 if (first_reads[inst->src[j].index] == -1)
4890 first_reads[inst->src[j].index] = (depth == 0) ? i : loop_start;
4891 }
4892 }
4893 for (j = 0; j < inst->tex_offset_num_offset; j++) {
4894 if (inst->tex_offsets[j].file == PROGRAM_TEMPORARY) {
4895 if (first_reads[inst->tex_offsets[j].index] == -1)
4896 first_reads[inst->tex_offsets[j].index] = (depth == 0) ? i : loop_start;
4897 }
4898 }
4899 if (inst->op == TGSI_OPCODE_BGNLOOP) {
4900 if(depth++ == 0)
4901 loop_start = i;
4902 } else if (inst->op == TGSI_OPCODE_ENDLOOP) {
4903 if (--depth == 0)
4904 loop_start = -1;
4905 }
4906 assert(depth >= 0);
4907 i++;
4908 }
4909 }
4910
4911 void
4912 glsl_to_tgsi_visitor::get_last_temp_read_first_temp_write(int *last_reads, int *first_writes)
4913 {
4914 int depth = 0; /* loop depth */
4915 int loop_start = -1; /* index of the first active BGNLOOP (if any) */
4916 unsigned i = 0, j;
4917 int k;
4918 foreach_in_list(glsl_to_tgsi_instruction, inst, &this->instructions) {
4919 for (j = 0; j < num_inst_src_regs(inst); j++) {
4920 if (inst->src[j].file == PROGRAM_TEMPORARY)
4921 last_reads[inst->src[j].index] = (depth == 0) ? i : -2;
4922 }
4923 for (j = 0; j < num_inst_dst_regs(inst); j++) {
4924 if (inst->dst[j].file == PROGRAM_TEMPORARY) {
4925 if (first_writes[inst->dst[j].index] == -1)
4926 first_writes[inst->dst[j].index] = (depth == 0) ? i : loop_start;
4927 last_reads[inst->dst[j].index] = (depth == 0) ? i : -2;
4928 }
4929 }
4930 for (j = 0; j < inst->tex_offset_num_offset; j++) {
4931 if (inst->tex_offsets[j].file == PROGRAM_TEMPORARY)
4932 last_reads[inst->tex_offsets[j].index] = (depth == 0) ? i : -2;
4933 }
4934 if (inst->op == TGSI_OPCODE_BGNLOOP) {
4935 if(depth++ == 0)
4936 loop_start = i;
4937 } else if (inst->op == TGSI_OPCODE_ENDLOOP) {
4938 if (--depth == 0) {
4939 loop_start = -1;
4940 for (k = 0; k < this->next_temp; k++) {
4941 if (last_reads[k] == -2) {
4942 last_reads[k] = i;
4943 }
4944 }
4945 }
4946 }
4947 assert(depth >= 0);
4948 i++;
4949 }
4950 }
4951
4952 void
4953 glsl_to_tgsi_visitor::get_last_temp_write(int *last_writes)
4954 {
4955 int depth = 0; /* loop depth */
4956 int i = 0, k;
4957 unsigned j;
4958
4959 foreach_in_list(glsl_to_tgsi_instruction, inst, &this->instructions) {
4960 for (j = 0; j < num_inst_dst_regs(inst); j++) {
4961 if (inst->dst[j].file == PROGRAM_TEMPORARY)
4962 last_writes[inst->dst[j].index] = (depth == 0) ? i : -2;
4963 }
4964
4965 if (inst->op == TGSI_OPCODE_BGNLOOP)
4966 depth++;
4967 else if (inst->op == TGSI_OPCODE_ENDLOOP)
4968 if (--depth == 0) {
4969 for (k = 0; k < this->next_temp; k++) {
4970 if (last_writes[k] == -2) {
4971 last_writes[k] = i;
4972 }
4973 }
4974 }
4975 assert(depth >= 0);
4976 i++;
4977 }
4978 }
4979
4980 /*
4981 * On a basic block basis, tracks available PROGRAM_TEMPORARY register
4982 * channels for copy propagation and updates following instructions to
4983 * use the original versions.
4984 *
4985 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
4986 * will occur. As an example, a TXP production before this pass:
4987 *
4988 * 0: MOV TEMP[1], INPUT[4].xyyy;
4989 * 1: MOV TEMP[1].w, INPUT[4].wwww;
4990 * 2: TXP TEMP[2], TEMP[1], texture[0], 2D;
4991 *
4992 * and after:
4993 *
4994 * 0: MOV TEMP[1], INPUT[4].xyyy;
4995 * 1: MOV TEMP[1].w, INPUT[4].wwww;
4996 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
4997 *
4998 * which allows for dead code elimination on TEMP[1]'s writes.
4999 */
5000 void
5001 glsl_to_tgsi_visitor::copy_propagate(void)
5002 {
5003 glsl_to_tgsi_instruction **acp = rzalloc_array(mem_ctx,
5004 glsl_to_tgsi_instruction *,
5005 this->next_temp * 4);
5006 int *acp_level = rzalloc_array(mem_ctx, int, this->next_temp * 4);
5007 int level = 0;
5008
5009 foreach_in_list(glsl_to_tgsi_instruction, inst, &this->instructions) {
5010 assert(inst->dst[0].file != PROGRAM_TEMPORARY
5011 || inst->dst[0].index < this->next_temp);
5012
5013 /* First, do any copy propagation possible into the src regs. */
5014 for (int r = 0; r < 3; r++) {
5015 glsl_to_tgsi_instruction *first = NULL;
5016 bool good = true;
5017 int acp_base = inst->src[r].index * 4;
5018
5019 if (inst->src[r].file != PROGRAM_TEMPORARY ||
5020 inst->src[r].reladdr ||
5021 inst->src[r].reladdr2)
5022 continue;
5023
5024 /* See if we can find entries in the ACP consisting of MOVs
5025 * from the same src register for all the swizzled channels
5026 * of this src register reference.
5027 */
5028 for (int i = 0; i < 4; i++) {
5029 int src_chan = GET_SWZ(inst->src[r].swizzle, i);
5030 glsl_to_tgsi_instruction *copy_chan = acp[acp_base + src_chan];
5031
5032 if (!copy_chan) {
5033 good = false;
5034 break;
5035 }
5036
5037 assert(acp_level[acp_base + src_chan] <= level);
5038
5039 if (!first) {
5040 first = copy_chan;
5041 } else {
5042 if (first->src[0].file != copy_chan->src[0].file ||
5043 first->src[0].index != copy_chan->src[0].index ||
5044 first->src[0].double_reg2 != copy_chan->src[0].double_reg2 ||
5045 first->src[0].index2D != copy_chan->src[0].index2D) {
5046 good = false;
5047 break;
5048 }
5049 }
5050 }
5051
5052 if (good) {
5053 /* We've now validated that we can copy-propagate to
5054 * replace this src register reference. Do it.
5055 */
5056 inst->src[r].file = first->src[0].file;
5057 inst->src[r].index = first->src[0].index;
5058 inst->src[r].index2D = first->src[0].index2D;
5059 inst->src[r].has_index2 = first->src[0].has_index2;
5060 inst->src[r].double_reg2 = first->src[0].double_reg2;
5061 inst->src[r].array_id = first->src[0].array_id;
5062
5063 int swizzle = 0;
5064 for (int i = 0; i < 4; i++) {
5065 int src_chan = GET_SWZ(inst->src[r].swizzle, i);
5066 glsl_to_tgsi_instruction *copy_inst = acp[acp_base + src_chan];
5067 swizzle |= (GET_SWZ(copy_inst->src[0].swizzle, src_chan) << (3 * i));
5068 }
5069 inst->src[r].swizzle = swizzle;
5070 }
5071 }
5072
5073 switch (inst->op) {
5074 case TGSI_OPCODE_BGNLOOP:
5075 case TGSI_OPCODE_ENDLOOP:
5076 /* End of a basic block, clear the ACP entirely. */
5077 memset(acp, 0, sizeof(*acp) * this->next_temp * 4);
5078 break;
5079
5080 case TGSI_OPCODE_IF:
5081 case TGSI_OPCODE_UIF:
5082 ++level;
5083 break;
5084
5085 case TGSI_OPCODE_ENDIF:
5086 case TGSI_OPCODE_ELSE:
5087 /* Clear all channels written inside the block from the ACP, but
5088 * leaving those that were not touched.
5089 */
5090 for (int r = 0; r < this->next_temp; r++) {
5091 for (int c = 0; c < 4; c++) {
5092 if (!acp[4 * r + c])
5093 continue;
5094
5095 if (acp_level[4 * r + c] >= level)
5096 acp[4 * r + c] = NULL;
5097 }
5098 }
5099 if (inst->op == TGSI_OPCODE_ENDIF)
5100 --level;
5101 break;
5102
5103 default:
5104 /* Continuing the block, clear any written channels from
5105 * the ACP.
5106 */
5107 for (int d = 0; d < 2; d++) {
5108 if (inst->dst[d].file == PROGRAM_TEMPORARY && inst->dst[d].reladdr) {
5109 /* Any temporary might be written, so no copy propagation
5110 * across this instruction.
5111 */
5112 memset(acp, 0, sizeof(*acp) * this->next_temp * 4);
5113 } else if (inst->dst[d].file == PROGRAM_OUTPUT &&
5114 inst->dst[d].reladdr) {
5115 /* Any output might be written, so no copy propagation
5116 * from outputs across this instruction.
5117 */
5118 for (int r = 0; r < this->next_temp; r++) {
5119 for (int c = 0; c < 4; c++) {
5120 if (!acp[4 * r + c])
5121 continue;
5122
5123 if (acp[4 * r + c]->src[0].file == PROGRAM_OUTPUT)
5124 acp[4 * r + c] = NULL;
5125 }
5126 }
5127 } else if (inst->dst[d].file == PROGRAM_TEMPORARY ||
5128 inst->dst[d].file == PROGRAM_OUTPUT) {
5129 /* Clear where it's used as dst. */
5130 if (inst->dst[d].file == PROGRAM_TEMPORARY) {
5131 for (int c = 0; c < 4; c++) {
5132 if (inst->dst[d].writemask & (1 << c))
5133 acp[4 * inst->dst[d].index + c] = NULL;
5134 }
5135 }
5136
5137 /* Clear where it's used as src. */
5138 for (int r = 0; r < this->next_temp; r++) {
5139 for (int c = 0; c < 4; c++) {
5140 if (!acp[4 * r + c])
5141 continue;
5142
5143 int src_chan = GET_SWZ(acp[4 * r + c]->src[0].swizzle, c);
5144
5145 if (acp[4 * r + c]->src[0].file == inst->dst[d].file &&
5146 acp[4 * r + c]->src[0].index == inst->dst[d].index &&
5147 inst->dst[d].writemask & (1 << src_chan)) {
5148 acp[4 * r + c] = NULL;
5149 }
5150 }
5151 }
5152 }
5153 }
5154 break;
5155 }
5156
5157 /* If this is a copy, add it to the ACP. */
5158 if (inst->op == TGSI_OPCODE_MOV &&
5159 inst->dst[0].file == PROGRAM_TEMPORARY &&
5160 !(inst->dst[0].file == inst->src[0].file &&
5161 inst->dst[0].index == inst->src[0].index) &&
5162 !inst->dst[0].reladdr &&
5163 !inst->dst[0].reladdr2 &&
5164 !inst->saturate &&
5165 inst->src[0].file != PROGRAM_ARRAY &&
5166 !inst->src[0].reladdr &&
5167 !inst->src[0].reladdr2 &&
5168 !inst->src[0].negate &&
5169 !inst->src[0].abs) {
5170 for (int i = 0; i < 4; i++) {
5171 if (inst->dst[0].writemask & (1 << i)) {
5172 acp[4 * inst->dst[0].index + i] = inst;
5173 acp_level[4 * inst->dst[0].index + i] = level;
5174 }
5175 }
5176 }
5177 }
5178
5179 ralloc_free(acp_level);
5180 ralloc_free(acp);
5181 }
5182
5183 /*
5184 * On a basic block basis, tracks available PROGRAM_TEMPORARY registers for dead
5185 * code elimination.
5186 *
5187 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
5188 * will occur. As an example, a TXP production after copy propagation but
5189 * before this pass:
5190 *
5191 * 0: MOV TEMP[1], INPUT[4].xyyy;
5192 * 1: MOV TEMP[1].w, INPUT[4].wwww;
5193 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
5194 *
5195 * and after this pass:
5196 *
5197 * 0: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
5198 */
5199 int
5200 glsl_to_tgsi_visitor::eliminate_dead_code(void)
5201 {
5202 glsl_to_tgsi_instruction **writes = rzalloc_array(mem_ctx,
5203 glsl_to_tgsi_instruction *,
5204 this->next_temp * 4);
5205 int *write_level = rzalloc_array(mem_ctx, int, this->next_temp * 4);
5206 int level = 0;
5207 int removed = 0;
5208
5209 foreach_in_list(glsl_to_tgsi_instruction, inst, &this->instructions) {
5210 assert(inst->dst[0].file != PROGRAM_TEMPORARY
5211 || inst->dst[0].index < this->next_temp);
5212
5213 switch (inst->op) {
5214 case TGSI_OPCODE_BGNLOOP:
5215 case TGSI_OPCODE_ENDLOOP:
5216 case TGSI_OPCODE_CONT:
5217 case TGSI_OPCODE_BRK:
5218 /* End of a basic block, clear the write array entirely.
5219 *
5220 * This keeps us from killing dead code when the writes are
5221 * on either side of a loop, even when the register isn't touched
5222 * inside the loop. However, glsl_to_tgsi_visitor doesn't seem to emit
5223 * dead code of this type, so it shouldn't make a difference as long as
5224 * the dead code elimination pass in the GLSL compiler does its job.
5225 */
5226 memset(writes, 0, sizeof(*writes) * this->next_temp * 4);
5227 break;
5228
5229 case TGSI_OPCODE_ENDIF:
5230 case TGSI_OPCODE_ELSE:
5231 /* Promote the recorded level of all channels written inside the
5232 * preceding if or else block to the level above the if/else block.
5233 */
5234 for (int r = 0; r < this->next_temp; r++) {
5235 for (int c = 0; c < 4; c++) {
5236 if (!writes[4 * r + c])
5237 continue;
5238
5239 if (write_level[4 * r + c] == level)
5240 write_level[4 * r + c] = level-1;
5241 }
5242 }
5243 if(inst->op == TGSI_OPCODE_ENDIF)
5244 --level;
5245 break;
5246
5247 case TGSI_OPCODE_IF:
5248 case TGSI_OPCODE_UIF:
5249 ++level;
5250 /* fallthrough to default case to mark the condition as read */
5251 default:
5252 /* Continuing the block, clear any channels from the write array that
5253 * are read by this instruction.
5254 */
5255 for (unsigned i = 0; i < ARRAY_SIZE(inst->src); i++) {
5256 if (inst->src[i].file == PROGRAM_TEMPORARY && inst->src[i].reladdr){
5257 /* Any temporary might be read, so no dead code elimination
5258 * across this instruction.
5259 */
5260 memset(writes, 0, sizeof(*writes) * this->next_temp * 4);
5261 } else if (inst->src[i].file == PROGRAM_TEMPORARY) {
5262 /* Clear where it's used as src. */
5263 int src_chans = 1 << GET_SWZ(inst->src[i].swizzle, 0);
5264 src_chans |= 1 << GET_SWZ(inst->src[i].swizzle, 1);
5265 src_chans |= 1 << GET_SWZ(inst->src[i].swizzle, 2);
5266 src_chans |= 1 << GET_SWZ(inst->src[i].swizzle, 3);
5267
5268 for (int c = 0; c < 4; c++) {
5269 if (src_chans & (1 << c))
5270 writes[4 * inst->src[i].index + c] = NULL;
5271 }
5272 }
5273 }
5274 for (unsigned i = 0; i < inst->tex_offset_num_offset; i++) {
5275 if (inst->tex_offsets[i].file == PROGRAM_TEMPORARY && inst->tex_offsets[i].reladdr){
5276 /* Any temporary might be read, so no dead code elimination
5277 * across this instruction.
5278 */
5279 memset(writes, 0, sizeof(*writes) * this->next_temp * 4);
5280 } else if (inst->tex_offsets[i].file == PROGRAM_TEMPORARY) {
5281 /* Clear where it's used as src. */
5282 int src_chans = 1 << GET_SWZ(inst->tex_offsets[i].swizzle, 0);
5283 src_chans |= 1 << GET_SWZ(inst->tex_offsets[i].swizzle, 1);
5284 src_chans |= 1 << GET_SWZ(inst->tex_offsets[i].swizzle, 2);
5285 src_chans |= 1 << GET_SWZ(inst->tex_offsets[i].swizzle, 3);
5286
5287 for (int c = 0; c < 4; c++) {
5288 if (src_chans & (1 << c))
5289 writes[4 * inst->tex_offsets[i].index + c] = NULL;
5290 }
5291 }
5292 }
5293 break;
5294 }
5295
5296 /* If this instruction writes to a temporary, add it to the write array.
5297 * If there is already an instruction in the write array for one or more
5298 * of the channels, flag that channel write as dead.
5299 */
5300 for (unsigned i = 0; i < ARRAY_SIZE(inst->dst); i++) {
5301 if (inst->dst[i].file == PROGRAM_TEMPORARY &&
5302 !inst->dst[i].reladdr) {
5303 for (int c = 0; c < 4; c++) {
5304 if (inst->dst[i].writemask & (1 << c)) {
5305 if (writes[4 * inst->dst[i].index + c]) {
5306 if (write_level[4 * inst->dst[i].index + c] < level)
5307 continue;
5308 else
5309 writes[4 * inst->dst[i].index + c]->dead_mask |= (1 << c);
5310 }
5311 writes[4 * inst->dst[i].index + c] = inst;
5312 write_level[4 * inst->dst[i].index + c] = level;
5313 }
5314 }
5315 }
5316 }
5317 }
5318
5319 /* Anything still in the write array at this point is dead code. */
5320 for (int r = 0; r < this->next_temp; r++) {
5321 for (int c = 0; c < 4; c++) {
5322 glsl_to_tgsi_instruction *inst = writes[4 * r + c];
5323 if (inst)
5324 inst->dead_mask |= (1 << c);
5325 }
5326 }
5327
5328 /* Now actually remove the instructions that are completely dead and update
5329 * the writemask of other instructions with dead channels.
5330 */
5331 foreach_in_list_safe(glsl_to_tgsi_instruction, inst, &this->instructions) {
5332 if (!inst->dead_mask || !inst->dst[0].writemask)
5333 continue;
5334 /* No amount of dead masks should remove memory stores */
5335 if (inst->info->is_store)
5336 continue;
5337
5338 if ((inst->dst[0].writemask & ~inst->dead_mask) == 0) {
5339 inst->remove();
5340 delete inst;
5341 removed++;
5342 } else {
5343 if (glsl_base_type_is_64bit(inst->dst[0].type)) {
5344 if (inst->dead_mask == WRITEMASK_XY ||
5345 inst->dead_mask == WRITEMASK_ZW)
5346 inst->dst[0].writemask &= ~(inst->dead_mask);
5347 } else
5348 inst->dst[0].writemask &= ~(inst->dead_mask);
5349 }
5350 }
5351
5352 ralloc_free(write_level);
5353 ralloc_free(writes);
5354
5355 return removed;
5356 }
5357
5358 /* merge DFRACEXP instructions into one. */
5359 void
5360 glsl_to_tgsi_visitor::merge_two_dsts(void)
5361 {
5362 foreach_in_list_safe(glsl_to_tgsi_instruction, inst, &this->instructions) {
5363 glsl_to_tgsi_instruction *inst2;
5364 bool merged;
5365 if (num_inst_dst_regs(inst) != 2)
5366 continue;
5367
5368 if (inst->dst[0].file != PROGRAM_UNDEFINED &&
5369 inst->dst[1].file != PROGRAM_UNDEFINED)
5370 continue;
5371
5372 inst2 = (glsl_to_tgsi_instruction *) inst->next;
5373 do {
5374
5375 if (inst->src[0].file == inst2->src[0].file &&
5376 inst->src[0].index == inst2->src[0].index &&
5377 inst->src[0].type == inst2->src[0].type &&
5378 inst->src[0].swizzle == inst2->src[0].swizzle)
5379 break;
5380 inst2 = (glsl_to_tgsi_instruction *) inst2->next;
5381 } while (inst2);
5382
5383 if (!inst2)
5384 continue;
5385 merged = false;
5386 if (inst->dst[0].file == PROGRAM_UNDEFINED) {
5387 merged = true;
5388 inst->dst[0] = inst2->dst[0];
5389 } else if (inst->dst[1].file == PROGRAM_UNDEFINED) {
5390 inst->dst[1] = inst2->dst[1];
5391 merged = true;
5392 }
5393
5394 if (merged) {
5395 inst2->remove();
5396 delete inst2;
5397 }
5398 }
5399 }
5400
5401 /* Merges temporary registers together where possible to reduce the number of
5402 * registers needed to run a program.
5403 *
5404 * Produces optimal code only after copy propagation and dead code elimination
5405 * have been run. */
5406 void
5407 glsl_to_tgsi_visitor::merge_registers(void)
5408 {
5409 int *last_reads = ralloc_array(mem_ctx, int, this->next_temp);
5410 int *first_writes = ralloc_array(mem_ctx, int, this->next_temp);
5411 struct rename_reg_pair *renames = rzalloc_array(mem_ctx, struct rename_reg_pair, this->next_temp);
5412 int i, j;
5413
5414 /* Read the indices of the last read and first write to each temp register
5415 * into an array so that we don't have to traverse the instruction list as
5416 * much. */
5417 for (i = 0; i < this->next_temp; i++) {
5418 last_reads[i] = -1;
5419 first_writes[i] = -1;
5420 }
5421 get_last_temp_read_first_temp_write(last_reads, first_writes);
5422
5423 /* Start looking for registers with non-overlapping usages that can be
5424 * merged together. */
5425 for (i = 0; i < this->next_temp; i++) {
5426 /* Don't touch unused registers. */
5427 if (last_reads[i] < 0 || first_writes[i] < 0) continue;
5428
5429 for (j = 0; j < this->next_temp; j++) {
5430 /* Don't touch unused registers. */
5431 if (last_reads[j] < 0 || first_writes[j] < 0) continue;
5432
5433 /* We can merge the two registers if the first write to j is after or
5434 * in the same instruction as the last read from i. Note that the
5435 * register at index i will always be used earlier or at the same time
5436 * as the register at index j. */
5437 if (first_writes[i] <= first_writes[j] &&
5438 last_reads[i] <= first_writes[j]) {
5439 renames[j].new_reg = i;
5440 renames[j].valid = true;
5441
5442 /* Update the first_writes and last_reads arrays with the new
5443 * values for the merged register index, and mark the newly unused
5444 * register index as such. */
5445 assert(last_reads[j] >= last_reads[i]);
5446 last_reads[i] = last_reads[j];
5447 first_writes[j] = -1;
5448 last_reads[j] = -1;
5449 }
5450 }
5451 }
5452
5453 rename_temp_registers(renames);
5454 ralloc_free(renames);
5455 ralloc_free(last_reads);
5456 ralloc_free(first_writes);
5457 }
5458
5459 /* Reassign indices to temporary registers by reusing unused indices created
5460 * by optimization passes. */
5461 void
5462 glsl_to_tgsi_visitor::renumber_registers(void)
5463 {
5464 int i = 0;
5465 int new_index = 0;
5466 int *first_writes = ralloc_array(mem_ctx, int, this->next_temp);
5467 struct rename_reg_pair *renames = rzalloc_array(mem_ctx, struct rename_reg_pair, this->next_temp);
5468
5469 for (i = 0; i < this->next_temp; i++) {
5470 first_writes[i] = -1;
5471 }
5472 get_first_temp_write(first_writes);
5473
5474 for (i = 0; i < this->next_temp; i++) {
5475 if (first_writes[i] < 0) continue;
5476 if (i != new_index) {
5477 renames[i].new_reg = new_index;
5478 renames[i].valid = true;
5479 }
5480 new_index++;
5481 }
5482
5483 rename_temp_registers(renames);
5484 this->next_temp = new_index;
5485 ralloc_free(renames);
5486 ralloc_free(first_writes);
5487 }
5488
5489 /* ------------------------- TGSI conversion stuff -------------------------- */
5490
5491 /**
5492 * Intermediate state used during shader translation.
5493 */
5494 struct st_translate {
5495 struct ureg_program *ureg;
5496
5497 unsigned temps_size;
5498 struct ureg_dst *temps;
5499
5500 struct ureg_dst *arrays;
5501 unsigned num_temp_arrays;
5502 struct ureg_src *constants;
5503 int num_constants;
5504 struct ureg_src *immediates;
5505 int num_immediates;
5506 struct ureg_dst outputs[PIPE_MAX_SHADER_OUTPUTS];
5507 struct ureg_src inputs[PIPE_MAX_SHADER_INPUTS];
5508 struct ureg_dst address[3];
5509 struct ureg_src samplers[PIPE_MAX_SAMPLERS];
5510 struct ureg_src buffers[PIPE_MAX_SHADER_BUFFERS];
5511 struct ureg_src images[PIPE_MAX_SHADER_IMAGES];
5512 struct ureg_src systemValues[SYSTEM_VALUE_MAX];
5513 struct ureg_src shared_memory;
5514 unsigned *array_sizes;
5515 struct inout_decl *input_decls;
5516 unsigned num_input_decls;
5517 struct inout_decl *output_decls;
5518 unsigned num_output_decls;
5519
5520 const ubyte *inputMapping;
5521 const ubyte *outputMapping;
5522
5523 unsigned procType; /**< PIPE_SHADER_VERTEX/FRAGMENT */
5524 };
5525
5526 /** Map Mesa's SYSTEM_VALUE_x to TGSI_SEMANTIC_x */
5527 unsigned
5528 _mesa_sysval_to_semantic(unsigned sysval)
5529 {
5530 switch (sysval) {
5531 /* Vertex shader */
5532 case SYSTEM_VALUE_VERTEX_ID:
5533 return TGSI_SEMANTIC_VERTEXID;
5534 case SYSTEM_VALUE_INSTANCE_ID:
5535 return TGSI_SEMANTIC_INSTANCEID;
5536 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE:
5537 return TGSI_SEMANTIC_VERTEXID_NOBASE;
5538 case SYSTEM_VALUE_BASE_VERTEX:
5539 return TGSI_SEMANTIC_BASEVERTEX;
5540 case SYSTEM_VALUE_BASE_INSTANCE:
5541 return TGSI_SEMANTIC_BASEINSTANCE;
5542 case SYSTEM_VALUE_DRAW_ID:
5543 return TGSI_SEMANTIC_DRAWID;
5544
5545 /* Geometry shader */
5546 case SYSTEM_VALUE_INVOCATION_ID:
5547 return TGSI_SEMANTIC_INVOCATIONID;
5548
5549 /* Fragment shader */
5550 case SYSTEM_VALUE_FRAG_COORD:
5551 return TGSI_SEMANTIC_POSITION;
5552 case SYSTEM_VALUE_FRONT_FACE:
5553 return TGSI_SEMANTIC_FACE;
5554 case SYSTEM_VALUE_SAMPLE_ID:
5555 return TGSI_SEMANTIC_SAMPLEID;
5556 case SYSTEM_VALUE_SAMPLE_POS:
5557 return TGSI_SEMANTIC_SAMPLEPOS;
5558 case SYSTEM_VALUE_SAMPLE_MASK_IN:
5559 return TGSI_SEMANTIC_SAMPLEMASK;
5560 case SYSTEM_VALUE_HELPER_INVOCATION:
5561 return TGSI_SEMANTIC_HELPER_INVOCATION;
5562
5563 /* Tessellation shader */
5564 case SYSTEM_VALUE_TESS_COORD:
5565 return TGSI_SEMANTIC_TESSCOORD;
5566 case SYSTEM_VALUE_VERTICES_IN:
5567 return TGSI_SEMANTIC_VERTICESIN;
5568 case SYSTEM_VALUE_PRIMITIVE_ID:
5569 return TGSI_SEMANTIC_PRIMID;
5570 case SYSTEM_VALUE_TESS_LEVEL_OUTER:
5571 return TGSI_SEMANTIC_TESSOUTER;
5572 case SYSTEM_VALUE_TESS_LEVEL_INNER:
5573 return TGSI_SEMANTIC_TESSINNER;
5574
5575 /* Compute shader */
5576 case SYSTEM_VALUE_LOCAL_INVOCATION_ID:
5577 return TGSI_SEMANTIC_THREAD_ID;
5578 case SYSTEM_VALUE_WORK_GROUP_ID:
5579 return TGSI_SEMANTIC_BLOCK_ID;
5580 case SYSTEM_VALUE_NUM_WORK_GROUPS:
5581 return TGSI_SEMANTIC_GRID_SIZE;
5582 case SYSTEM_VALUE_LOCAL_GROUP_SIZE:
5583 return TGSI_SEMANTIC_BLOCK_SIZE;
5584
5585 /* ARB_shader_ballot */
5586 case SYSTEM_VALUE_SUBGROUP_SIZE:
5587 return TGSI_SEMANTIC_SUBGROUP_SIZE;
5588 case SYSTEM_VALUE_SUBGROUP_INVOCATION:
5589 return TGSI_SEMANTIC_SUBGROUP_INVOCATION;
5590 case SYSTEM_VALUE_SUBGROUP_EQ_MASK:
5591 return TGSI_SEMANTIC_SUBGROUP_EQ_MASK;
5592 case SYSTEM_VALUE_SUBGROUP_GE_MASK:
5593 return TGSI_SEMANTIC_SUBGROUP_GE_MASK;
5594 case SYSTEM_VALUE_SUBGROUP_GT_MASK:
5595 return TGSI_SEMANTIC_SUBGROUP_GT_MASK;
5596 case SYSTEM_VALUE_SUBGROUP_LE_MASK:
5597 return TGSI_SEMANTIC_SUBGROUP_LE_MASK;
5598 case SYSTEM_VALUE_SUBGROUP_LT_MASK:
5599 return TGSI_SEMANTIC_SUBGROUP_LT_MASK;
5600
5601 /* Unhandled */
5602 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX:
5603 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID:
5604 case SYSTEM_VALUE_VERTEX_CNT:
5605 default:
5606 assert(!"Unexpected SYSTEM_VALUE_ enum");
5607 return TGSI_SEMANTIC_COUNT;
5608 }
5609 }
5610
5611 /**
5612 * Map a glsl_to_tgsi constant/immediate to a TGSI immediate.
5613 */
5614 static struct ureg_src
5615 emit_immediate(struct st_translate *t,
5616 gl_constant_value values[4],
5617 int type, int size)
5618 {
5619 struct ureg_program *ureg = t->ureg;
5620
5621 switch(type)
5622 {
5623 case GL_FLOAT:
5624 return ureg_DECL_immediate(ureg, &values[0].f, size);
5625 case GL_DOUBLE:
5626 return ureg_DECL_immediate_f64(ureg, (double *)&values[0].f, size);
5627 case GL_INT64_ARB:
5628 return ureg_DECL_immediate_int64(ureg, (int64_t *)&values[0].f, size);
5629 case GL_UNSIGNED_INT64_ARB:
5630 return ureg_DECL_immediate_uint64(ureg, (uint64_t *)&values[0].f, size);
5631 case GL_INT:
5632 return ureg_DECL_immediate_int(ureg, &values[0].i, size);
5633 case GL_UNSIGNED_INT:
5634 case GL_BOOL:
5635 return ureg_DECL_immediate_uint(ureg, &values[0].u, size);
5636 default:
5637 assert(!"should not get here - type must be float, int, uint, or bool");
5638 return ureg_src_undef();
5639 }
5640 }
5641
5642 /**
5643 * Map a glsl_to_tgsi dst register to a TGSI ureg_dst register.
5644 */
5645 static struct ureg_dst
5646 dst_register(struct st_translate *t, gl_register_file file, unsigned index,
5647 unsigned array_id)
5648 {
5649 unsigned array;
5650
5651 switch(file) {
5652 case PROGRAM_UNDEFINED:
5653 return ureg_dst_undef();
5654
5655 case PROGRAM_TEMPORARY:
5656 /* Allocate space for temporaries on demand. */
5657 if (index >= t->temps_size) {
5658 const int inc = align(index - t->temps_size + 1, 4096);
5659
5660 t->temps = (struct ureg_dst*)
5661 realloc(t->temps,
5662 (t->temps_size + inc) * sizeof(struct ureg_dst));
5663 if (!t->temps)
5664 return ureg_dst_undef();
5665
5666 memset(t->temps + t->temps_size, 0, inc * sizeof(struct ureg_dst));
5667 t->temps_size += inc;
5668 }
5669
5670 if (ureg_dst_is_undef(t->temps[index]))
5671 t->temps[index] = ureg_DECL_local_temporary(t->ureg);
5672
5673 return t->temps[index];
5674
5675 case PROGRAM_ARRAY:
5676 assert(array_id && array_id <= t->num_temp_arrays);
5677 array = array_id - 1;
5678
5679 if (ureg_dst_is_undef(t->arrays[array]))
5680 t->arrays[array] = ureg_DECL_array_temporary(
5681 t->ureg, t->array_sizes[array], TRUE);
5682
5683 return ureg_dst_array_offset(t->arrays[array], index);
5684
5685 case PROGRAM_OUTPUT:
5686 if (!array_id) {
5687 if (t->procType == PIPE_SHADER_FRAGMENT)
5688 assert(index < 2 * FRAG_RESULT_MAX);
5689 else if (t->procType == PIPE_SHADER_TESS_CTRL ||
5690 t->procType == PIPE_SHADER_TESS_EVAL)
5691 assert(index < VARYING_SLOT_TESS_MAX);
5692 else
5693 assert(index < VARYING_SLOT_MAX);
5694
5695 assert(t->outputMapping[index] < ARRAY_SIZE(t->outputs));
5696 assert(t->outputs[t->outputMapping[index]].File != TGSI_FILE_NULL);
5697 return t->outputs[t->outputMapping[index]];
5698 }
5699 else {
5700 struct inout_decl *decl = find_inout_array(t->output_decls, t->num_output_decls, array_id);
5701 unsigned mesa_index = decl->mesa_index;
5702 int slot = t->outputMapping[mesa_index];
5703
5704 assert(slot != -1 && t->outputs[slot].File == TGSI_FILE_OUTPUT);
5705
5706 struct ureg_dst dst = t->outputs[slot];
5707 dst.ArrayID = array_id;
5708 return ureg_dst_array_offset(dst, index - mesa_index);
5709 }
5710
5711 case PROGRAM_ADDRESS:
5712 return t->address[index];
5713
5714 default:
5715 assert(!"unknown dst register file");
5716 return ureg_dst_undef();
5717 }
5718 }
5719
5720 /**
5721 * Map a glsl_to_tgsi src register to a TGSI ureg_src register.
5722 */
5723 static struct ureg_src
5724 src_register(struct st_translate *t, const st_src_reg *reg)
5725 {
5726 int index = reg->index;
5727 int double_reg2 = reg->double_reg2 ? 1 : 0;
5728
5729 switch(reg->file) {
5730 case PROGRAM_UNDEFINED:
5731 return ureg_imm4f(t->ureg, 0, 0, 0, 0);
5732
5733 case PROGRAM_TEMPORARY:
5734 case PROGRAM_ARRAY:
5735 return ureg_src(dst_register(t, reg->file, reg->index, reg->array_id));
5736
5737 case PROGRAM_OUTPUT: {
5738 struct ureg_dst dst = dst_register(t, reg->file, reg->index, reg->array_id);
5739 assert(dst.WriteMask != 0);
5740 unsigned shift = ffs(dst.WriteMask) - 1;
5741 return ureg_swizzle(ureg_src(dst),
5742 shift,
5743 MIN2(shift + 1, 3),
5744 MIN2(shift + 2, 3),
5745 MIN2(shift + 3, 3));
5746 }
5747
5748 case PROGRAM_UNIFORM:
5749 assert(reg->index >= 0);
5750 return reg->index < t->num_constants ?
5751 t->constants[reg->index] : ureg_imm4f(t->ureg, 0, 0, 0, 0);
5752 case PROGRAM_STATE_VAR:
5753 case PROGRAM_CONSTANT: /* ie, immediate */
5754 if (reg->has_index2)
5755 return ureg_src_register(TGSI_FILE_CONSTANT, reg->index);
5756 else
5757 return reg->index >= 0 && reg->index < t->num_constants ?
5758 t->constants[reg->index] : ureg_imm4f(t->ureg, 0, 0, 0, 0);
5759
5760 case PROGRAM_IMMEDIATE:
5761 assert(reg->index >= 0 && reg->index < t->num_immediates);
5762 return t->immediates[reg->index];
5763
5764 case PROGRAM_INPUT:
5765 /* GLSL inputs are 64-bit containers, so we have to
5766 * map back to the original index and add the offset after
5767 * mapping. */
5768 index -= double_reg2;
5769 if (!reg->array_id) {
5770 assert(t->inputMapping[index] < ARRAY_SIZE(t->inputs));
5771 assert(t->inputs[t->inputMapping[index]].File != TGSI_FILE_NULL);
5772 return t->inputs[t->inputMapping[index] + double_reg2];
5773 }
5774 else {
5775 struct inout_decl *decl = find_inout_array(t->input_decls, t->num_input_decls, reg->array_id);
5776 unsigned mesa_index = decl->mesa_index;
5777 int slot = t->inputMapping[mesa_index];
5778
5779 assert(slot != -1 && t->inputs[slot].File == TGSI_FILE_INPUT);
5780
5781 struct ureg_src src = t->inputs[slot];
5782 src.ArrayID = reg->array_id;
5783 return ureg_src_array_offset(src, index + double_reg2 - mesa_index);
5784 }
5785
5786 case PROGRAM_ADDRESS:
5787 return ureg_src(t->address[reg->index]);
5788
5789 case PROGRAM_SYSTEM_VALUE:
5790 assert(reg->index < (int) ARRAY_SIZE(t->systemValues));
5791 return t->systemValues[reg->index];
5792
5793 default:
5794 assert(!"unknown src register file");
5795 return ureg_src_undef();
5796 }
5797 }
5798
5799 /**
5800 * Create a TGSI ureg_dst register from an st_dst_reg.
5801 */
5802 static struct ureg_dst
5803 translate_dst(struct st_translate *t,
5804 const st_dst_reg *dst_reg,
5805 bool saturate)
5806 {
5807 struct ureg_dst dst = dst_register(t, dst_reg->file, dst_reg->index,
5808 dst_reg->array_id);
5809
5810 if (dst.File == TGSI_FILE_NULL)
5811 return dst;
5812
5813 dst = ureg_writemask(dst, dst_reg->writemask);
5814
5815 if (saturate)
5816 dst = ureg_saturate(dst);
5817
5818 if (dst_reg->reladdr != NULL) {
5819 assert(dst_reg->file != PROGRAM_TEMPORARY);
5820 dst = ureg_dst_indirect(dst, ureg_src(t->address[0]));
5821 }
5822
5823 if (dst_reg->has_index2) {
5824 if (dst_reg->reladdr2)
5825 dst = ureg_dst_dimension_indirect(dst, ureg_src(t->address[1]),
5826 dst_reg->index2D);
5827 else
5828 dst = ureg_dst_dimension(dst, dst_reg->index2D);
5829 }
5830
5831 return dst;
5832 }
5833
5834 /**
5835 * Create a TGSI ureg_src register from an st_src_reg.
5836 */
5837 static struct ureg_src
5838 translate_src(struct st_translate *t, const st_src_reg *src_reg)
5839 {
5840 struct ureg_src src = src_register(t, src_reg);
5841
5842 if (src_reg->has_index2) {
5843 /* 2D indexes occur with geometry shader inputs (attrib, vertex)
5844 * and UBO constant buffers (buffer, position).
5845 */
5846 if (src_reg->reladdr2)
5847 src = ureg_src_dimension_indirect(src, ureg_src(t->address[1]),
5848 src_reg->index2D);
5849 else
5850 src = ureg_src_dimension(src, src_reg->index2D);
5851 }
5852
5853 src = ureg_swizzle(src,
5854 GET_SWZ(src_reg->swizzle, 0) & 0x3,
5855 GET_SWZ(src_reg->swizzle, 1) & 0x3,
5856 GET_SWZ(src_reg->swizzle, 2) & 0x3,
5857 GET_SWZ(src_reg->swizzle, 3) & 0x3);
5858
5859 if (src_reg->abs)
5860 src = ureg_abs(src);
5861
5862 if ((src_reg->negate & 0xf) == NEGATE_XYZW)
5863 src = ureg_negate(src);
5864
5865 if (src_reg->reladdr != NULL) {
5866 assert(src_reg->file != PROGRAM_TEMPORARY);
5867 src = ureg_src_indirect(src, ureg_src(t->address[0]));
5868 }
5869
5870 return src;
5871 }
5872
5873 static struct tgsi_texture_offset
5874 translate_tex_offset(struct st_translate *t,
5875 const st_src_reg *in_offset)
5876 {
5877 struct tgsi_texture_offset offset;
5878 struct ureg_src src = translate_src(t, in_offset);
5879
5880 offset.File = src.File;
5881 offset.Index = src.Index;
5882 offset.SwizzleX = src.SwizzleX;
5883 offset.SwizzleY = src.SwizzleY;
5884 offset.SwizzleZ = src.SwizzleZ;
5885 offset.Padding = 0;
5886
5887 assert(!src.Indirect);
5888 assert(!src.DimIndirect);
5889 assert(!src.Dimension);
5890 assert(!src.Absolute); /* those shouldn't be used with integers anyway */
5891 assert(!src.Negate);
5892
5893 return offset;
5894 }
5895
5896 static void
5897 compile_tgsi_instruction(struct st_translate *t,
5898 const glsl_to_tgsi_instruction *inst)
5899 {
5900 struct ureg_program *ureg = t->ureg;
5901 int i;
5902 struct ureg_dst dst[2];
5903 struct ureg_src src[4];
5904 struct tgsi_texture_offset texoffsets[MAX_GLSL_TEXTURE_OFFSET];
5905
5906 int num_dst;
5907 int num_src;
5908 unsigned tex_target = 0;
5909
5910 num_dst = num_inst_dst_regs(inst);
5911 num_src = num_inst_src_regs(inst);
5912
5913 for (i = 0; i < num_dst; i++)
5914 dst[i] = translate_dst(t,
5915 &inst->dst[i],
5916 inst->saturate);
5917
5918 for (i = 0; i < num_src; i++)
5919 src[i] = translate_src(t, &inst->src[i]);
5920
5921 switch(inst->op) {
5922 case TGSI_OPCODE_BGNLOOP:
5923 case TGSI_OPCODE_ELSE:
5924 case TGSI_OPCODE_ENDLOOP:
5925 case TGSI_OPCODE_IF:
5926 case TGSI_OPCODE_UIF:
5927 assert(num_dst == 0);
5928 ureg_insn(ureg, inst->op, NULL, 0, src, num_src);
5929 return;
5930
5931 case TGSI_OPCODE_TEX:
5932 case TGSI_OPCODE_TEX_LZ:
5933 case TGSI_OPCODE_TXB:
5934 case TGSI_OPCODE_TXD:
5935 case TGSI_OPCODE_TXL:
5936 case TGSI_OPCODE_TXP:
5937 case TGSI_OPCODE_TXQ:
5938 case TGSI_OPCODE_TXQS:
5939 case TGSI_OPCODE_TXF:
5940 case TGSI_OPCODE_TXF_LZ:
5941 case TGSI_OPCODE_TEX2:
5942 case TGSI_OPCODE_TXB2:
5943 case TGSI_OPCODE_TXL2:
5944 case TGSI_OPCODE_TG4:
5945 case TGSI_OPCODE_LODQ:
5946 if (inst->resource.file == PROGRAM_SAMPLER) {
5947 src[num_src] = t->samplers[inst->resource.index];
5948 } else {
5949 /* Bindless samplers. */
5950 src[num_src] = translate_src(t, &inst->resource);
5951 }
5952 assert(src[num_src].File != TGSI_FILE_NULL);
5953 if (inst->resource.reladdr)
5954 src[num_src] =
5955 ureg_src_indirect(src[num_src], ureg_src(t->address[2]));
5956 num_src++;
5957 for (i = 0; i < (int)inst->tex_offset_num_offset; i++) {
5958 texoffsets[i] = translate_tex_offset(t, &inst->tex_offsets[i]);
5959 }
5960 tex_target = st_translate_texture_target(inst->tex_target, inst->tex_shadow);
5961
5962 ureg_tex_insn(ureg,
5963 inst->op,
5964 dst, num_dst,
5965 tex_target,
5966 st_translate_texture_type(inst->tex_type),
5967 texoffsets, inst->tex_offset_num_offset,
5968 src, num_src);
5969 return;
5970
5971 case TGSI_OPCODE_RESQ:
5972 case TGSI_OPCODE_LOAD:
5973 case TGSI_OPCODE_ATOMUADD:
5974 case TGSI_OPCODE_ATOMXCHG:
5975 case TGSI_OPCODE_ATOMCAS:
5976 case TGSI_OPCODE_ATOMAND:
5977 case TGSI_OPCODE_ATOMOR:
5978 case TGSI_OPCODE_ATOMXOR:
5979 case TGSI_OPCODE_ATOMUMIN:
5980 case TGSI_OPCODE_ATOMUMAX:
5981 case TGSI_OPCODE_ATOMIMIN:
5982 case TGSI_OPCODE_ATOMIMAX:
5983 for (i = num_src - 1; i >= 0; i--)
5984 src[i + 1] = src[i];
5985 num_src++;
5986 if (inst->resource.file == PROGRAM_MEMORY) {
5987 src[0] = t->shared_memory;
5988 } else if (inst->resource.file == PROGRAM_BUFFER) {
5989 src[0] = t->buffers[inst->resource.index];
5990 } else {
5991 if (inst->resource.file == PROGRAM_IMAGE) {
5992 src[0] = t->images[inst->resource.index];
5993 } else {
5994 /* Bindless images. */
5995 src[0] = translate_src(t, &inst->resource);
5996 }
5997 tex_target = st_translate_texture_target(inst->tex_target, inst->tex_shadow);
5998 }
5999 if (inst->resource.reladdr)
6000 src[0] = ureg_src_indirect(src[0], ureg_src(t->address[2]));
6001 assert(src[0].File != TGSI_FILE_NULL);
6002 ureg_memory_insn(ureg, inst->op, dst, num_dst, src, num_src,
6003 inst->buffer_access,
6004 tex_target, inst->image_format);
6005 break;
6006
6007 case TGSI_OPCODE_STORE:
6008 if (inst->resource.file == PROGRAM_MEMORY) {
6009 dst[0] = ureg_dst(t->shared_memory);
6010 } else if (inst->resource.file == PROGRAM_BUFFER) {
6011 dst[0] = ureg_dst(t->buffers[inst->resource.index]);
6012 } else {
6013 if (inst->resource.file == PROGRAM_IMAGE) {
6014 dst[0] = ureg_dst(t->images[inst->resource.index]);
6015 } else {
6016 /* Bindless images. */
6017 dst[0] = ureg_dst(translate_src(t, &inst->resource));
6018 }
6019 tex_target = st_translate_texture_target(inst->tex_target, inst->tex_shadow);
6020 }
6021 dst[0] = ureg_writemask(dst[0], inst->dst[0].writemask);
6022 if (inst->resource.reladdr)
6023 dst[0] = ureg_dst_indirect(dst[0], ureg_src(t->address[2]));
6024 assert(dst[0].File != TGSI_FILE_NULL);
6025 ureg_memory_insn(ureg, inst->op, dst, num_dst, src, num_src,
6026 inst->buffer_access,
6027 tex_target, inst->image_format);
6028 break;
6029
6030 case TGSI_OPCODE_SCS:
6031 dst[0] = ureg_writemask(dst[0], TGSI_WRITEMASK_XY);
6032 ureg_insn(ureg, inst->op, dst, num_dst, src, num_src);
6033 break;
6034
6035 default:
6036 ureg_insn(ureg,
6037 inst->op,
6038 dst, num_dst,
6039 src, num_src);
6040 break;
6041 }
6042 }
6043
6044 /**
6045 * Emit the TGSI instructions for inverting and adjusting WPOS.
6046 * This code is unavoidable because it also depends on whether
6047 * a FBO is bound (STATE_FB_WPOS_Y_TRANSFORM).
6048 */
6049 static void
6050 emit_wpos_adjustment(struct gl_context *ctx,
6051 struct st_translate *t,
6052 int wpos_transform_const,
6053 boolean invert,
6054 GLfloat adjX, GLfloat adjY[2])
6055 {
6056 struct ureg_program *ureg = t->ureg;
6057
6058 assert(wpos_transform_const >= 0);
6059
6060 /* Fragment program uses fragment position input.
6061 * Need to replace instances of INPUT[WPOS] with temp T
6062 * where T = INPUT[WPOS] is inverted by Y.
6063 */
6064 struct ureg_src wpostrans = ureg_DECL_constant(ureg, wpos_transform_const);
6065 struct ureg_dst wpos_temp = ureg_DECL_temporary( ureg );
6066 struct ureg_src *wpos =
6067 ctx->Const.GLSLFragCoordIsSysVal ?
6068 &t->systemValues[SYSTEM_VALUE_FRAG_COORD] :
6069 &t->inputs[t->inputMapping[VARYING_SLOT_POS]];
6070 struct ureg_src wpos_input = *wpos;
6071
6072 /* First, apply the coordinate shift: */
6073 if (adjX || adjY[0] || adjY[1]) {
6074 if (adjY[0] != adjY[1]) {
6075 /* Adjust the y coordinate by adjY[1] or adjY[0] respectively
6076 * depending on whether inversion is actually going to be applied
6077 * or not, which is determined by testing against the inversion
6078 * state variable used below, which will be either +1 or -1.
6079 */
6080 struct ureg_dst adj_temp = ureg_DECL_local_temporary(ureg);
6081
6082 ureg_CMP(ureg, adj_temp,
6083 ureg_scalar(wpostrans, invert ? 2 : 0),
6084 ureg_imm4f(ureg, adjX, adjY[0], 0.0f, 0.0f),
6085 ureg_imm4f(ureg, adjX, adjY[1], 0.0f, 0.0f));
6086 ureg_ADD(ureg, wpos_temp, wpos_input, ureg_src(adj_temp));
6087 } else {
6088 ureg_ADD(ureg, wpos_temp, wpos_input,
6089 ureg_imm4f(ureg, adjX, adjY[0], 0.0f, 0.0f));
6090 }
6091 wpos_input = ureg_src(wpos_temp);
6092 } else {
6093 /* MOV wpos_temp, input[wpos]
6094 */
6095 ureg_MOV( ureg, wpos_temp, wpos_input );
6096 }
6097
6098 /* Now the conditional y flip: STATE_FB_WPOS_Y_TRANSFORM.xy/zw will be
6099 * inversion/identity, or the other way around if we're drawing to an FBO.
6100 */
6101 if (invert) {
6102 /* MAD wpos_temp.y, wpos_input, wpostrans.xxxx, wpostrans.yyyy
6103 */
6104 ureg_MAD( ureg,
6105 ureg_writemask(wpos_temp, TGSI_WRITEMASK_Y ),
6106 wpos_input,
6107 ureg_scalar(wpostrans, 0),
6108 ureg_scalar(wpostrans, 1));
6109 } else {
6110 /* MAD wpos_temp.y, wpos_input, wpostrans.zzzz, wpostrans.wwww
6111 */
6112 ureg_MAD( ureg,
6113 ureg_writemask(wpos_temp, TGSI_WRITEMASK_Y ),
6114 wpos_input,
6115 ureg_scalar(wpostrans, 2),
6116 ureg_scalar(wpostrans, 3));
6117 }
6118
6119 /* Use wpos_temp as position input from here on:
6120 */
6121 *wpos = ureg_src(wpos_temp);
6122 }
6123
6124
6125 /**
6126 * Emit fragment position/ooordinate code.
6127 */
6128 static void
6129 emit_wpos(struct st_context *st,
6130 struct st_translate *t,
6131 const struct gl_program *program,
6132 struct ureg_program *ureg,
6133 int wpos_transform_const)
6134 {
6135 struct pipe_screen *pscreen = st->pipe->screen;
6136 GLfloat adjX = 0.0f;
6137 GLfloat adjY[2] = { 0.0f, 0.0f };
6138 boolean invert = FALSE;
6139
6140 /* Query the pixel center conventions supported by the pipe driver and set
6141 * adjX, adjY to help out if it cannot handle the requested one internally.
6142 *
6143 * The bias of the y-coordinate depends on whether y-inversion takes place
6144 * (adjY[1]) or not (adjY[0]), which is in turn dependent on whether we are
6145 * drawing to an FBO (causes additional inversion), and whether the pipe
6146 * driver origin and the requested origin differ (the latter condition is
6147 * stored in the 'invert' variable).
6148 *
6149 * For height = 100 (i = integer, h = half-integer, l = lower, u = upper):
6150 *
6151 * center shift only:
6152 * i -> h: +0.5
6153 * h -> i: -0.5
6154 *
6155 * inversion only:
6156 * l,i -> u,i: ( 0.0 + 1.0) * -1 + 100 = 99
6157 * l,h -> u,h: ( 0.5 + 0.0) * -1 + 100 = 99.5
6158 * u,i -> l,i: (99.0 + 1.0) * -1 + 100 = 0
6159 * u,h -> l,h: (99.5 + 0.0) * -1 + 100 = 0.5
6160 *
6161 * inversion and center shift:
6162 * l,i -> u,h: ( 0.0 + 0.5) * -1 + 100 = 99.5
6163 * l,h -> u,i: ( 0.5 + 0.5) * -1 + 100 = 99
6164 * u,i -> l,h: (99.0 + 0.5) * -1 + 100 = 0.5
6165 * u,h -> l,i: (99.5 + 0.5) * -1 + 100 = 0
6166 */
6167 if (program->OriginUpperLeft) {
6168 /* Fragment shader wants origin in upper-left */
6169 if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT)) {
6170 /* the driver supports upper-left origin */
6171 }
6172 else if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT)) {
6173 /* the driver supports lower-left origin, need to invert Y */
6174 ureg_property(ureg, TGSI_PROPERTY_FS_COORD_ORIGIN,
6175 TGSI_FS_COORD_ORIGIN_LOWER_LEFT);
6176 invert = TRUE;
6177 }
6178 else
6179 assert(0);
6180 }
6181 else {
6182 /* Fragment shader wants origin in lower-left */
6183 if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT))
6184 /* the driver supports lower-left origin */
6185 ureg_property(ureg, TGSI_PROPERTY_FS_COORD_ORIGIN,
6186 TGSI_FS_COORD_ORIGIN_LOWER_LEFT);
6187 else if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT))
6188 /* the driver supports upper-left origin, need to invert Y */
6189 invert = TRUE;
6190 else
6191 assert(0);
6192 }
6193
6194 if (program->PixelCenterInteger) {
6195 /* Fragment shader wants pixel center integer */
6196 if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER)) {
6197 /* the driver supports pixel center integer */
6198 adjY[1] = 1.0f;
6199 ureg_property(ureg, TGSI_PROPERTY_FS_COORD_PIXEL_CENTER,
6200 TGSI_FS_COORD_PIXEL_CENTER_INTEGER);
6201 }
6202 else if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER)) {
6203 /* the driver supports pixel center half integer, need to bias X,Y */
6204 adjX = -0.5f;
6205 adjY[0] = -0.5f;
6206 adjY[1] = 0.5f;
6207 }
6208 else
6209 assert(0);
6210 }
6211 else {
6212 /* Fragment shader wants pixel center half integer */
6213 if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER)) {
6214 /* the driver supports pixel center half integer */
6215 }
6216 else if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER)) {
6217 /* the driver supports pixel center integer, need to bias X,Y */
6218 adjX = adjY[0] = adjY[1] = 0.5f;
6219 ureg_property(ureg, TGSI_PROPERTY_FS_COORD_PIXEL_CENTER,
6220 TGSI_FS_COORD_PIXEL_CENTER_INTEGER);
6221 }
6222 else
6223 assert(0);
6224 }
6225
6226 /* we invert after adjustment so that we avoid the MOV to temporary,
6227 * and reuse the adjustment ADD instead */
6228 emit_wpos_adjustment(st->ctx, t, wpos_transform_const, invert, adjX, adjY);
6229 }
6230
6231 /**
6232 * OpenGL's fragment gl_FrontFace input is 1 for front-facing, 0 for back.
6233 * TGSI uses +1 for front, -1 for back.
6234 * This function converts the TGSI value to the GL value. Simply clamping/
6235 * saturating the value to [0,1] does the job.
6236 */
6237 static void
6238 emit_face_var(struct gl_context *ctx, struct st_translate *t)
6239 {
6240 struct ureg_program *ureg = t->ureg;
6241 struct ureg_dst face_temp = ureg_DECL_temporary(ureg);
6242 struct ureg_src face_input = t->inputs[t->inputMapping[VARYING_SLOT_FACE]];
6243
6244 if (ctx->Const.NativeIntegers) {
6245 ureg_FSGE(ureg, face_temp, face_input, ureg_imm1f(ureg, 0));
6246 }
6247 else {
6248 /* MOV_SAT face_temp, input[face] */
6249 ureg_MOV(ureg, ureg_saturate(face_temp), face_input);
6250 }
6251
6252 /* Use face_temp as face input from here on: */
6253 t->inputs[t->inputMapping[VARYING_SLOT_FACE]] = ureg_src(face_temp);
6254 }
6255
6256 static void
6257 emit_compute_block_size(const struct gl_program *prog,
6258 struct ureg_program *ureg) {
6259 ureg_property(ureg, TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH,
6260 prog->info.cs.local_size[0]);
6261 ureg_property(ureg, TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT,
6262 prog->info.cs.local_size[1]);
6263 ureg_property(ureg, TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH,
6264 prog->info.cs.local_size[2]);
6265 }
6266
6267 struct sort_inout_decls {
6268 bool operator()(const struct inout_decl &a, const struct inout_decl &b) const {
6269 return mapping[a.mesa_index] < mapping[b.mesa_index];
6270 }
6271
6272 const ubyte *mapping;
6273 };
6274
6275 /* Sort the given array of decls by the corresponding slot (TGSI file index).
6276 *
6277 * This is for the benefit of older drivers which are broken when the
6278 * declarations aren't sorted in this way.
6279 */
6280 static void
6281 sort_inout_decls_by_slot(struct inout_decl *decls,
6282 unsigned count,
6283 const ubyte mapping[])
6284 {
6285 sort_inout_decls sorter;
6286 sorter.mapping = mapping;
6287 std::sort(decls, decls + count, sorter);
6288 }
6289
6290 static unsigned
6291 st_translate_interp(enum glsl_interp_mode glsl_qual, GLuint varying)
6292 {
6293 switch (glsl_qual) {
6294 case INTERP_MODE_NONE:
6295 if (varying == VARYING_SLOT_COL0 || varying == VARYING_SLOT_COL1)
6296 return TGSI_INTERPOLATE_COLOR;
6297 return TGSI_INTERPOLATE_PERSPECTIVE;
6298 case INTERP_MODE_SMOOTH:
6299 return TGSI_INTERPOLATE_PERSPECTIVE;
6300 case INTERP_MODE_FLAT:
6301 return TGSI_INTERPOLATE_CONSTANT;
6302 case INTERP_MODE_NOPERSPECTIVE:
6303 return TGSI_INTERPOLATE_LINEAR;
6304 default:
6305 assert(0 && "unexpected interp mode in st_translate_interp()");
6306 return TGSI_INTERPOLATE_PERSPECTIVE;
6307 }
6308 }
6309
6310 /**
6311 * Translate intermediate IR (glsl_to_tgsi_instruction) to TGSI format.
6312 * \param program the program to translate
6313 * \param numInputs number of input registers used
6314 * \param inputMapping maps Mesa fragment program inputs to TGSI generic
6315 * input indexes
6316 * \param inputSemanticName the TGSI_SEMANTIC flag for each input
6317 * \param inputSemanticIndex the semantic index (ex: which texcoord) for
6318 * each input
6319 * \param interpMode the TGSI_INTERPOLATE_LINEAR/PERSP mode for each input
6320 * \param numOutputs number of output registers used
6321 * \param outputMapping maps Mesa fragment program outputs to TGSI
6322 * generic outputs
6323 * \param outputSemanticName the TGSI_SEMANTIC flag for each output
6324 * \param outputSemanticIndex the semantic index (ex: which texcoord) for
6325 * each output
6326 *
6327 * \return PIPE_OK or PIPE_ERROR_OUT_OF_MEMORY
6328 */
6329 extern "C" enum pipe_error
6330 st_translate_program(
6331 struct gl_context *ctx,
6332 uint procType,
6333 struct ureg_program *ureg,
6334 glsl_to_tgsi_visitor *program,
6335 const struct gl_program *proginfo,
6336 GLuint numInputs,
6337 const ubyte inputMapping[],
6338 const ubyte inputSlotToAttr[],
6339 const ubyte inputSemanticName[],
6340 const ubyte inputSemanticIndex[],
6341 const ubyte interpMode[],
6342 GLuint numOutputs,
6343 const ubyte outputMapping[],
6344 const ubyte outputSemanticName[],
6345 const ubyte outputSemanticIndex[])
6346 {
6347 struct st_translate *t;
6348 unsigned i;
6349 struct gl_program_constants *frag_const =
6350 &ctx->Const.Program[MESA_SHADER_FRAGMENT];
6351 enum pipe_error ret = PIPE_OK;
6352
6353 assert(numInputs <= ARRAY_SIZE(t->inputs));
6354 assert(numOutputs <= ARRAY_SIZE(t->outputs));
6355
6356 t = CALLOC_STRUCT(st_translate);
6357 if (!t) {
6358 ret = PIPE_ERROR_OUT_OF_MEMORY;
6359 goto out;
6360 }
6361
6362 t->procType = procType;
6363 t->inputMapping = inputMapping;
6364 t->outputMapping = outputMapping;
6365 t->ureg = ureg;
6366 t->num_temp_arrays = program->next_array;
6367 if (t->num_temp_arrays)
6368 t->arrays = (struct ureg_dst*)
6369 calloc(t->num_temp_arrays, sizeof(t->arrays[0]));
6370
6371 /*
6372 * Declare input attributes.
6373 */
6374 switch (procType) {
6375 case PIPE_SHADER_FRAGMENT:
6376 case PIPE_SHADER_GEOMETRY:
6377 case PIPE_SHADER_TESS_EVAL:
6378 case PIPE_SHADER_TESS_CTRL:
6379 sort_inout_decls_by_slot(program->inputs, program->num_inputs, inputMapping);
6380
6381 for (i = 0; i < program->num_inputs; ++i) {
6382 struct inout_decl *decl = &program->inputs[i];
6383 unsigned slot = inputMapping[decl->mesa_index];
6384 struct ureg_src src;
6385 ubyte tgsi_usage_mask = decl->usage_mask;
6386
6387 if (glsl_base_type_is_64bit(decl->base_type)) {
6388 if (tgsi_usage_mask == 1)
6389 tgsi_usage_mask = TGSI_WRITEMASK_XY;
6390 else if (tgsi_usage_mask == 2)
6391 tgsi_usage_mask = TGSI_WRITEMASK_ZW;
6392 else
6393 tgsi_usage_mask = TGSI_WRITEMASK_XYZW;
6394 }
6395
6396 unsigned interp_mode = 0;
6397 unsigned interp_location = 0;
6398 if (procType == PIPE_SHADER_FRAGMENT) {
6399 assert(interpMode);
6400 interp_mode = interpMode[slot] != TGSI_INTERPOLATE_COUNT ?
6401 interpMode[slot] :
6402 st_translate_interp(decl->interp, inputSlotToAttr[slot]);
6403
6404 interp_location = decl->interp_loc;
6405 }
6406
6407 src = ureg_DECL_fs_input_cyl_centroid_layout(ureg,
6408 inputSemanticName[slot], inputSemanticIndex[slot],
6409 interp_mode, 0, interp_location, slot, tgsi_usage_mask,
6410 decl->array_id, decl->size);
6411
6412 for (unsigned j = 0; j < decl->size; ++j) {
6413 if (t->inputs[slot + j].File != TGSI_FILE_INPUT) {
6414 /* The ArrayID is set up in dst_register */
6415 t->inputs[slot + j] = src;
6416 t->inputs[slot + j].ArrayID = 0;
6417 t->inputs[slot + j].Index += j;
6418 }
6419 }
6420 }
6421 break;
6422 case PIPE_SHADER_VERTEX:
6423 for (i = 0; i < numInputs; i++) {
6424 t->inputs[i] = ureg_DECL_vs_input(ureg, i);
6425 }
6426 break;
6427 case PIPE_SHADER_COMPUTE:
6428 break;
6429 default:
6430 assert(0);
6431 }
6432
6433 /*
6434 * Declare output attributes.
6435 */
6436 switch (procType) {
6437 case PIPE_SHADER_FRAGMENT:
6438 case PIPE_SHADER_COMPUTE:
6439 break;
6440 case PIPE_SHADER_GEOMETRY:
6441 case PIPE_SHADER_TESS_EVAL:
6442 case PIPE_SHADER_TESS_CTRL:
6443 case PIPE_SHADER_VERTEX:
6444 sort_inout_decls_by_slot(program->outputs, program->num_outputs, outputMapping);
6445
6446 for (i = 0; i < program->num_outputs; ++i) {
6447 struct inout_decl *decl = &program->outputs[i];
6448 unsigned slot = outputMapping[decl->mesa_index];
6449 struct ureg_dst dst;
6450 ubyte tgsi_usage_mask = decl->usage_mask;
6451
6452 if (glsl_base_type_is_64bit(decl->base_type)) {
6453 if (tgsi_usage_mask == 1)
6454 tgsi_usage_mask = TGSI_WRITEMASK_XY;
6455 else if (tgsi_usage_mask == 2)
6456 tgsi_usage_mask = TGSI_WRITEMASK_ZW;
6457 else
6458 tgsi_usage_mask = TGSI_WRITEMASK_XYZW;
6459 }
6460
6461 dst = ureg_DECL_output_layout(ureg,
6462 outputSemanticName[slot], outputSemanticIndex[slot],
6463 decl->gs_out_streams,
6464 slot, tgsi_usage_mask, decl->array_id, decl->size);
6465
6466 for (unsigned j = 0; j < decl->size; ++j) {
6467 if (t->outputs[slot + j].File != TGSI_FILE_OUTPUT) {
6468 /* The ArrayID is set up in dst_register */
6469 t->outputs[slot + j] = dst;
6470 t->outputs[slot + j].ArrayID = 0;
6471 t->outputs[slot + j].Index += j;
6472 }
6473 }
6474 }
6475 break;
6476 default:
6477 assert(0);
6478 }
6479
6480 if (procType == PIPE_SHADER_FRAGMENT) {
6481 if (program->shader->Program->info.fs.early_fragment_tests ||
6482 program->shader->Program->info.fs.post_depth_coverage) {
6483 ureg_property(ureg, TGSI_PROPERTY_FS_EARLY_DEPTH_STENCIL, 1);
6484
6485 if (program->shader->Program->info.fs.post_depth_coverage)
6486 ureg_property(ureg, TGSI_PROPERTY_FS_POST_DEPTH_COVERAGE, 1);
6487 }
6488
6489 if (proginfo->info.inputs_read & VARYING_BIT_POS) {
6490 /* Must do this after setting up t->inputs. */
6491 emit_wpos(st_context(ctx), t, proginfo, ureg,
6492 program->wpos_transform_const);
6493 }
6494
6495 if (proginfo->info.inputs_read & VARYING_BIT_FACE)
6496 emit_face_var(ctx, t);
6497
6498 for (i = 0; i < numOutputs; i++) {
6499 switch (outputSemanticName[i]) {
6500 case TGSI_SEMANTIC_POSITION:
6501 t->outputs[i] = ureg_DECL_output(ureg,
6502 TGSI_SEMANTIC_POSITION, /* Z/Depth */
6503 outputSemanticIndex[i]);
6504 t->outputs[i] = ureg_writemask(t->outputs[i], TGSI_WRITEMASK_Z);
6505 break;
6506 case TGSI_SEMANTIC_STENCIL:
6507 t->outputs[i] = ureg_DECL_output(ureg,
6508 TGSI_SEMANTIC_STENCIL, /* Stencil */
6509 outputSemanticIndex[i]);
6510 t->outputs[i] = ureg_writemask(t->outputs[i], TGSI_WRITEMASK_Y);
6511 break;
6512 case TGSI_SEMANTIC_COLOR:
6513 t->outputs[i] = ureg_DECL_output(ureg,
6514 TGSI_SEMANTIC_COLOR,
6515 outputSemanticIndex[i]);
6516 break;
6517 case TGSI_SEMANTIC_SAMPLEMASK:
6518 t->outputs[i] = ureg_DECL_output(ureg,
6519 TGSI_SEMANTIC_SAMPLEMASK,
6520 outputSemanticIndex[i]);
6521 /* TODO: If we ever support more than 32 samples, this will have
6522 * to become an array.
6523 */
6524 t->outputs[i] = ureg_writemask(t->outputs[i], TGSI_WRITEMASK_X);
6525 break;
6526 default:
6527 assert(!"fragment shader outputs must be POSITION/STENCIL/COLOR");
6528 ret = PIPE_ERROR_BAD_INPUT;
6529 goto out;
6530 }
6531 }
6532 }
6533 else if (procType == PIPE_SHADER_VERTEX) {
6534 for (i = 0; i < numOutputs; i++) {
6535 if (outputSemanticName[i] == TGSI_SEMANTIC_FOG) {
6536 /* force register to contain a fog coordinate in the form (F, 0, 0, 1). */
6537 ureg_MOV(ureg,
6538 ureg_writemask(t->outputs[i], TGSI_WRITEMASK_YZW),
6539 ureg_imm4f(ureg, 0.0f, 0.0f, 0.0f, 1.0f));
6540 t->outputs[i] = ureg_writemask(t->outputs[i], TGSI_WRITEMASK_X);
6541 }
6542 }
6543 }
6544
6545 if (procType == PIPE_SHADER_COMPUTE) {
6546 emit_compute_block_size(proginfo, ureg);
6547 }
6548
6549 /* Declare address register.
6550 */
6551 if (program->num_address_regs > 0) {
6552 assert(program->num_address_regs <= 3);
6553 for (int i = 0; i < program->num_address_regs; i++)
6554 t->address[i] = ureg_DECL_address(ureg);
6555 }
6556
6557 /* Declare misc input registers
6558 */
6559 {
6560 GLbitfield sysInputs = proginfo->info.system_values_read;
6561
6562 for (i = 0; sysInputs; i++) {
6563 if (sysInputs & (1 << i)) {
6564 unsigned semName = _mesa_sysval_to_semantic(i);
6565
6566 t->systemValues[i] = ureg_DECL_system_value(ureg, semName, 0);
6567
6568 if (semName == TGSI_SEMANTIC_INSTANCEID ||
6569 semName == TGSI_SEMANTIC_VERTEXID) {
6570 /* From Gallium perspective, these system values are always
6571 * integer, and require native integer support. However, if
6572 * native integer is supported on the vertex stage but not the
6573 * pixel stage (e.g, i915g + draw), Mesa will generate IR that
6574 * assumes these system values are floats. To resolve the
6575 * inconsistency, we insert a U2F.
6576 */
6577 struct st_context *st = st_context(ctx);
6578 struct pipe_screen *pscreen = st->pipe->screen;
6579 assert(procType == PIPE_SHADER_VERTEX);
6580 assert(pscreen->get_shader_param(pscreen, PIPE_SHADER_VERTEX, PIPE_SHADER_CAP_INTEGERS));
6581 (void) pscreen;
6582 if (!ctx->Const.NativeIntegers) {
6583 struct ureg_dst temp = ureg_DECL_local_temporary(t->ureg);
6584 ureg_U2F( t->ureg, ureg_writemask(temp, TGSI_WRITEMASK_X), t->systemValues[i]);
6585 t->systemValues[i] = ureg_scalar(ureg_src(temp), 0);
6586 }
6587 }
6588
6589 if (procType == PIPE_SHADER_FRAGMENT &&
6590 semName == TGSI_SEMANTIC_POSITION)
6591 emit_wpos(st_context(ctx), t, proginfo, ureg,
6592 program->wpos_transform_const);
6593
6594 sysInputs &= ~(1 << i);
6595 }
6596 }
6597 }
6598
6599 t->array_sizes = program->array_sizes;
6600 t->input_decls = program->inputs;
6601 t->num_input_decls = program->num_inputs;
6602 t->output_decls = program->outputs;
6603 t->num_output_decls = program->num_outputs;
6604
6605 /* Emit constants and uniforms. TGSI uses a single index space for these,
6606 * so we put all the translated regs in t->constants.
6607 */
6608 if (proginfo->Parameters) {
6609 t->constants = (struct ureg_src *)
6610 calloc(proginfo->Parameters->NumParameters, sizeof(t->constants[0]));
6611 if (t->constants == NULL) {
6612 ret = PIPE_ERROR_OUT_OF_MEMORY;
6613 goto out;
6614 }
6615 t->num_constants = proginfo->Parameters->NumParameters;
6616
6617 for (i = 0; i < proginfo->Parameters->NumParameters; i++) {
6618 switch (proginfo->Parameters->Parameters[i].Type) {
6619 case PROGRAM_STATE_VAR:
6620 case PROGRAM_UNIFORM:
6621 t->constants[i] = ureg_DECL_constant(ureg, i);
6622 break;
6623
6624 /* Emit immediates for PROGRAM_CONSTANT only when there's no indirect
6625 * addressing of the const buffer.
6626 * FIXME: Be smarter and recognize param arrays:
6627 * indirect addressing is only valid within the referenced
6628 * array.
6629 */
6630 case PROGRAM_CONSTANT:
6631 if (program->indirect_addr_consts)
6632 t->constants[i] = ureg_DECL_constant(ureg, i);
6633 else
6634 t->constants[i] = emit_immediate(t,
6635 proginfo->Parameters->ParameterValues[i],
6636 proginfo->Parameters->Parameters[i].DataType,
6637 4);
6638 break;
6639 default:
6640 break;
6641 }
6642 }
6643 }
6644
6645 for (i = 0; i < proginfo->info.num_ubos; i++) {
6646 unsigned size = proginfo->sh.UniformBlocks[i]->UniformBufferSize;
6647 unsigned num_const_vecs = (size + 15) / 16;
6648 unsigned first, last;
6649 assert(num_const_vecs > 0);
6650 first = 0;
6651 last = num_const_vecs > 0 ? num_const_vecs - 1 : 0;
6652 ureg_DECL_constant2D(t->ureg, first, last, i + 1);
6653 }
6654
6655 /* Emit immediate values.
6656 */
6657 t->immediates = (struct ureg_src *)
6658 calloc(program->num_immediates, sizeof(struct ureg_src));
6659 if (t->immediates == NULL) {
6660 ret = PIPE_ERROR_OUT_OF_MEMORY;
6661 goto out;
6662 }
6663 t->num_immediates = program->num_immediates;
6664
6665 i = 0;
6666 foreach_in_list(immediate_storage, imm, &program->immediates) {
6667 assert(i < program->num_immediates);
6668 t->immediates[i++] = emit_immediate(t, imm->values, imm->type, imm->size32);
6669 }
6670 assert(i == program->num_immediates);
6671
6672 /* texture samplers */
6673 for (i = 0; i < frag_const->MaxTextureImageUnits; i++) {
6674 if (program->samplers_used & (1u << i)) {
6675 unsigned type = st_translate_texture_type(program->sampler_types[i]);
6676
6677 t->samplers[i] = ureg_DECL_sampler(ureg, i);
6678
6679 ureg_DECL_sampler_view( ureg, i, program->sampler_targets[i],
6680 type, type, type, type );
6681 }
6682 }
6683
6684 /* Declare atomic and shader storage buffers. */
6685 {
6686 struct gl_program *prog = program->prog;
6687
6688 for (i = 0; i < prog->info.num_abos; i++) {
6689 unsigned index = prog->sh.AtomicBuffers[i]->Binding;
6690 assert(index < frag_const->MaxAtomicBuffers);
6691 t->buffers[index] = ureg_DECL_buffer(ureg, index, true);
6692 }
6693
6694 assert(prog->info.num_ssbos <= frag_const->MaxShaderStorageBlocks);
6695 for (i = 0; i < prog->info.num_ssbos; i++) {
6696 unsigned index = frag_const->MaxAtomicBuffers + i;
6697 t->buffers[index] = ureg_DECL_buffer(ureg, index, false);
6698 }
6699 }
6700
6701 if (program->use_shared_memory)
6702 t->shared_memory = ureg_DECL_memory(ureg, TGSI_MEMORY_TYPE_SHARED);
6703
6704 for (i = 0; i < program->shader->Program->info.num_images; i++) {
6705 if (program->images_used & (1 << i)) {
6706 t->images[i] = ureg_DECL_image(ureg, i,
6707 program->image_targets[i],
6708 program->image_formats[i],
6709 true, false);
6710 }
6711 }
6712
6713 /* Emit each instruction in turn:
6714 */
6715 foreach_in_list(glsl_to_tgsi_instruction, inst, &program->instructions)
6716 compile_tgsi_instruction(t, inst);
6717
6718 /* Set the next shader stage hint for VS and TES. */
6719 switch (procType) {
6720 case PIPE_SHADER_VERTEX:
6721 case PIPE_SHADER_TESS_EVAL:
6722 if (program->shader_program->SeparateShader)
6723 break;
6724
6725 for (i = program->shader->Stage+1; i <= MESA_SHADER_FRAGMENT; i++) {
6726 if (program->shader_program->_LinkedShaders[i]) {
6727 unsigned next;
6728
6729 switch (i) {
6730 case MESA_SHADER_TESS_CTRL:
6731 next = PIPE_SHADER_TESS_CTRL;
6732 break;
6733 case MESA_SHADER_TESS_EVAL:
6734 next = PIPE_SHADER_TESS_EVAL;
6735 break;
6736 case MESA_SHADER_GEOMETRY:
6737 next = PIPE_SHADER_GEOMETRY;
6738 break;
6739 case MESA_SHADER_FRAGMENT:
6740 next = PIPE_SHADER_FRAGMENT;
6741 break;
6742 default:
6743 assert(0);
6744 continue;
6745 }
6746
6747 ureg_set_next_shader_processor(ureg, next);
6748 break;
6749 }
6750 }
6751 break;
6752 }
6753
6754 out:
6755 if (t) {
6756 free(t->arrays);
6757 free(t->temps);
6758 free(t->constants);
6759 t->num_constants = 0;
6760 free(t->immediates);
6761 t->num_immediates = 0;
6762 FREE(t);
6763 }
6764
6765 return ret;
6766 }
6767 /* ----------------------------- End TGSI code ------------------------------ */
6768
6769
6770 /**
6771 * Convert a shader's GLSL IR into a Mesa gl_program, although without
6772 * generating Mesa IR.
6773 */
6774 static struct gl_program *
6775 get_mesa_program_tgsi(struct gl_context *ctx,
6776 struct gl_shader_program *shader_program,
6777 struct gl_linked_shader *shader)
6778 {
6779 glsl_to_tgsi_visitor* v;
6780 struct gl_program *prog;
6781 struct gl_shader_compiler_options *options =
6782 &ctx->Const.ShaderCompilerOptions[shader->Stage];
6783 struct pipe_screen *pscreen = ctx->st->pipe->screen;
6784 enum pipe_shader_type ptarget = st_shader_stage_to_ptarget(shader->Stage);
6785 unsigned skip_merge_registers;
6786
6787 validate_ir_tree(shader->ir);
6788
6789 prog = shader->Program;
6790
6791 prog->Parameters = _mesa_new_parameter_list();
6792 v = new glsl_to_tgsi_visitor();
6793 v->ctx = ctx;
6794 v->prog = prog;
6795 v->shader_program = shader_program;
6796 v->shader = shader;
6797 v->options = options;
6798 v->glsl_version = ctx->Const.GLSLVersion;
6799 v->native_integers = ctx->Const.NativeIntegers;
6800
6801 v->have_sqrt = pscreen->get_shader_param(pscreen, ptarget,
6802 PIPE_SHADER_CAP_TGSI_SQRT_SUPPORTED);
6803 v->have_fma = pscreen->get_shader_param(pscreen, ptarget,
6804 PIPE_SHADER_CAP_TGSI_FMA_SUPPORTED);
6805 v->has_tex_txf_lz = pscreen->get_param(pscreen,
6806 PIPE_CAP_TGSI_TEX_TXF_LZ);
6807
6808 v->variables = _mesa_hash_table_create(v->mem_ctx, _mesa_hash_pointer,
6809 _mesa_key_pointer_equal);
6810 skip_merge_registers =
6811 pscreen->get_shader_param(pscreen, ptarget,
6812 PIPE_SHADER_CAP_TGSI_SKIP_MERGE_REGISTERS);
6813
6814 _mesa_generate_parameters_list_for_uniforms(shader_program, shader,
6815 prog->Parameters);
6816
6817 /* Remove reads from output registers. */
6818 if (!pscreen->get_param(pscreen, PIPE_CAP_TGSI_CAN_READ_OUTPUTS))
6819 lower_output_reads(shader->Stage, shader->ir);
6820
6821 /* Emit intermediate IR for main(). */
6822 visit_exec_list(shader->ir, v);
6823
6824 #if 0
6825 /* Print out some information (for debugging purposes) used by the
6826 * optimization passes. */
6827 {
6828 int i;
6829 int *first_writes = ralloc_array(v->mem_ctx, int, v->next_temp);
6830 int *first_reads = ralloc_array(v->mem_ctx, int, v->next_temp);
6831 int *last_writes = ralloc_array(v->mem_ctx, int, v->next_temp);
6832 int *last_reads = ralloc_array(v->mem_ctx, int, v->next_temp);
6833
6834 for (i = 0; i < v->next_temp; i++) {
6835 first_writes[i] = -1;
6836 first_reads[i] = -1;
6837 last_writes[i] = -1;
6838 last_reads[i] = -1;
6839 }
6840 v->get_first_temp_read(first_reads);
6841 v->get_last_temp_read_first_temp_write(last_reads, first_writes);
6842 v->get_last_temp_write(last_writes);
6843 for (i = 0; i < v->next_temp; i++)
6844 printf("Temp %d: FR=%3d FW=%3d LR=%3d LW=%3d\n", i, first_reads[i],
6845 first_writes[i],
6846 last_reads[i],
6847 last_writes[i]);
6848 ralloc_free(first_writes);
6849 ralloc_free(first_reads);
6850 ralloc_free(last_writes);
6851 ralloc_free(last_reads);
6852 }
6853 #endif
6854
6855 /* Perform optimizations on the instructions in the glsl_to_tgsi_visitor. */
6856 v->simplify_cmp();
6857
6858 if (shader->Stage != MESA_SHADER_TESS_CTRL &&
6859 shader->Stage != MESA_SHADER_TESS_EVAL)
6860 v->copy_propagate();
6861
6862 while (v->eliminate_dead_code());
6863
6864 v->merge_two_dsts();
6865 if (!skip_merge_registers)
6866 v->merge_registers();
6867 v->renumber_registers();
6868
6869 /* Write the END instruction. */
6870 v->emit_asm(NULL, TGSI_OPCODE_END);
6871
6872 if (ctx->_Shader->Flags & GLSL_DUMP) {
6873 _mesa_log("\n");
6874 _mesa_log("GLSL IR for linked %s program %d:\n",
6875 _mesa_shader_stage_to_string(shader->Stage),
6876 shader_program->Name);
6877 _mesa_print_ir(_mesa_get_log_file(), shader->ir, NULL);
6878 _mesa_log("\n\n");
6879 }
6880
6881 do_set_program_inouts(shader->ir, prog, shader->Stage);
6882 _mesa_copy_linked_program_data(shader_program, shader);
6883 shrink_array_declarations(v->inputs, v->num_inputs,
6884 &prog->info.inputs_read,
6885 prog->info.double_inputs_read,
6886 &prog->info.patch_inputs_read);
6887 shrink_array_declarations(v->outputs, v->num_outputs,
6888 &prog->info.outputs_written, 0ULL,
6889 &prog->info.patch_outputs_written);
6890 count_resources(v, prog);
6891
6892 /* The GLSL IR won't be needed anymore. */
6893 ralloc_free(shader->ir);
6894 shader->ir = NULL;
6895
6896 /* This must be done before the uniform storage is associated. */
6897 if (shader->Stage == MESA_SHADER_FRAGMENT &&
6898 (prog->info.inputs_read & VARYING_BIT_POS ||
6899 prog->info.system_values_read & (1 << SYSTEM_VALUE_FRAG_COORD))) {
6900 static const gl_state_index wposTransformState[STATE_LENGTH] = {
6901 STATE_INTERNAL, STATE_FB_WPOS_Y_TRANSFORM
6902 };
6903
6904 v->wpos_transform_const = _mesa_add_state_reference(prog->Parameters,
6905 wposTransformState);
6906 }
6907
6908 /* Avoid reallocation of the program parameter list, because the uniform
6909 * storage is only associated with the original parameter list.
6910 * This should be enough for Bitmap and DrawPixels constants.
6911 */
6912 _mesa_reserve_parameter_storage(prog->Parameters, 8);
6913
6914 /* This has to be done last. Any operation the can cause
6915 * prog->ParameterValues to get reallocated (e.g., anything that adds a
6916 * program constant) has to happen before creating this linkage.
6917 */
6918 _mesa_associate_uniform_storage(ctx, shader_program, prog, true);
6919 if (!shader_program->data->LinkStatus) {
6920 free_glsl_to_tgsi_visitor(v);
6921 _mesa_reference_program(ctx, &shader->Program, NULL);
6922 return NULL;
6923 }
6924
6925 struct st_vertex_program *stvp;
6926 struct st_fragment_program *stfp;
6927 struct st_common_program *stp;
6928 struct st_compute_program *stcp;
6929
6930 switch (shader->Stage) {
6931 case MESA_SHADER_VERTEX:
6932 stvp = (struct st_vertex_program *)prog;
6933 stvp->glsl_to_tgsi = v;
6934 break;
6935 case MESA_SHADER_FRAGMENT:
6936 stfp = (struct st_fragment_program *)prog;
6937 stfp->glsl_to_tgsi = v;
6938 break;
6939 case MESA_SHADER_TESS_CTRL:
6940 case MESA_SHADER_TESS_EVAL:
6941 case MESA_SHADER_GEOMETRY:
6942 stp = st_common_program(prog);
6943 stp->glsl_to_tgsi = v;
6944 break;
6945 case MESA_SHADER_COMPUTE:
6946 stcp = (struct st_compute_program *)prog;
6947 stcp->glsl_to_tgsi = v;
6948 break;
6949 default:
6950 assert(!"should not be reached");
6951 return NULL;
6952 }
6953
6954 return prog;
6955 }
6956
6957 /* See if there are unsupported control flow statements. */
6958 class ir_control_flow_info_visitor : public ir_hierarchical_visitor {
6959 private:
6960 const struct gl_shader_compiler_options *options;
6961 public:
6962 ir_control_flow_info_visitor(const struct gl_shader_compiler_options *options)
6963 : options(options),
6964 unsupported(false)
6965 {
6966 }
6967
6968 virtual ir_visitor_status visit_enter(ir_function *ir)
6969 {
6970 /* Other functions are skipped (same as glsl_to_tgsi). */
6971 if (strcmp(ir->name, "main") == 0)
6972 return visit_continue;
6973
6974 return visit_continue_with_parent;
6975 }
6976
6977 virtual ir_visitor_status visit_enter(ir_call *ir)
6978 {
6979 if (!ir->callee->is_intrinsic()) {
6980 unsupported = true; /* it's a function call */
6981 return visit_stop;
6982 }
6983 return visit_continue;
6984 }
6985
6986 virtual ir_visitor_status visit_enter(ir_return *ir)
6987 {
6988 if (options->EmitNoMainReturn) {
6989 unsupported = true;
6990 return visit_stop;
6991 }
6992 return visit_continue;
6993 }
6994
6995 bool unsupported;
6996 };
6997
6998 static bool
6999 has_unsupported_control_flow(exec_list *ir,
7000 const struct gl_shader_compiler_options *options)
7001 {
7002 ir_control_flow_info_visitor visitor(options);
7003 visit_list_elements(&visitor, ir);
7004 return visitor.unsupported;
7005 }
7006
7007 extern "C" {
7008
7009 /**
7010 * Link a shader.
7011 * Called via ctx->Driver.LinkShader()
7012 * This actually involves converting GLSL IR into an intermediate TGSI-like IR
7013 * with code lowering and other optimizations.
7014 */
7015 GLboolean
7016 st_link_shader(struct gl_context *ctx, struct gl_shader_program *prog)
7017 {
7018 /* Return early if we are loading the shader from on-disk cache */
7019 if (st_load_tgsi_from_disk_cache(ctx, prog)) {
7020 return GL_TRUE;
7021 }
7022
7023 struct pipe_screen *pscreen = ctx->st->pipe->screen;
7024 assert(prog->data->LinkStatus);
7025
7026 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
7027 if (prog->_LinkedShaders[i] == NULL)
7028 continue;
7029
7030 struct gl_linked_shader *shader = prog->_LinkedShaders[i];
7031 exec_list *ir = shader->ir;
7032 gl_shader_stage stage = shader->Stage;
7033 const struct gl_shader_compiler_options *options =
7034 &ctx->Const.ShaderCompilerOptions[stage];
7035 enum pipe_shader_type ptarget = st_shader_stage_to_ptarget(stage);
7036 bool have_dround = pscreen->get_shader_param(pscreen, ptarget,
7037 PIPE_SHADER_CAP_TGSI_DROUND_SUPPORTED);
7038 bool have_dfrexp = pscreen->get_shader_param(pscreen, ptarget,
7039 PIPE_SHADER_CAP_TGSI_DFRACEXP_DLDEXP_SUPPORTED);
7040 unsigned if_threshold = pscreen->get_shader_param(pscreen, ptarget,
7041 PIPE_SHADER_CAP_LOWER_IF_THRESHOLD);
7042
7043 /* If there are forms of indirect addressing that the driver
7044 * cannot handle, perform the lowering pass.
7045 */
7046 if (options->EmitNoIndirectInput || options->EmitNoIndirectOutput ||
7047 options->EmitNoIndirectTemp || options->EmitNoIndirectUniform) {
7048 lower_variable_index_to_cond_assign(stage, ir,
7049 options->EmitNoIndirectInput,
7050 options->EmitNoIndirectOutput,
7051 options->EmitNoIndirectTemp,
7052 options->EmitNoIndirectUniform);
7053 }
7054
7055 if (!pscreen->get_param(pscreen, PIPE_CAP_INT64_DIVMOD))
7056 lower_64bit_integer_instructions(ir, DIV64 | MOD64);
7057
7058 if (ctx->Extensions.ARB_shading_language_packing) {
7059 unsigned lower_inst = LOWER_PACK_SNORM_2x16 |
7060 LOWER_UNPACK_SNORM_2x16 |
7061 LOWER_PACK_UNORM_2x16 |
7062 LOWER_UNPACK_UNORM_2x16 |
7063 LOWER_PACK_SNORM_4x8 |
7064 LOWER_UNPACK_SNORM_4x8 |
7065 LOWER_UNPACK_UNORM_4x8 |
7066 LOWER_PACK_UNORM_4x8;
7067
7068 if (ctx->Extensions.ARB_gpu_shader5)
7069 lower_inst |= LOWER_PACK_USE_BFI |
7070 LOWER_PACK_USE_BFE;
7071 if (!ctx->st->has_half_float_packing)
7072 lower_inst |= LOWER_PACK_HALF_2x16 |
7073 LOWER_UNPACK_HALF_2x16;
7074
7075 lower_packing_builtins(ir, lower_inst);
7076 }
7077
7078 if (!pscreen->get_param(pscreen, PIPE_CAP_TEXTURE_GATHER_OFFSETS))
7079 lower_offset_arrays(ir);
7080 do_mat_op_to_vec(ir);
7081
7082 if (stage == MESA_SHADER_FRAGMENT)
7083 lower_blend_equation_advanced(shader);
7084
7085 lower_instructions(ir,
7086 MOD_TO_FLOOR |
7087 FDIV_TO_MUL_RCP |
7088 EXP_TO_EXP2 |
7089 LOG_TO_LOG2 |
7090 LDEXP_TO_ARITH |
7091 (have_dfrexp ? 0 : DFREXP_DLDEXP_TO_ARITH) |
7092 CARRY_TO_ARITH |
7093 BORROW_TO_ARITH |
7094 (have_dround ? 0 : DOPS_TO_DFRAC) |
7095 (options->EmitNoPow ? POW_TO_EXP2 : 0) |
7096 (!ctx->Const.NativeIntegers ? INT_DIV_TO_MUL_RCP : 0) |
7097 (options->EmitNoSat ? SAT_TO_CLAMP : 0) |
7098 (ctx->Const.ForceGLSLAbsSqrt ? SQRT_TO_ABS_SQRT : 0) |
7099 /* Assume that if ARB_gpu_shader5 is not supported
7100 * then all of the extended integer functions need
7101 * lowering. It may be necessary to add some caps
7102 * for individual instructions.
7103 */
7104 (!ctx->Extensions.ARB_gpu_shader5
7105 ? BIT_COUNT_TO_MATH |
7106 EXTRACT_TO_SHIFTS |
7107 INSERT_TO_SHIFTS |
7108 REVERSE_TO_SHIFTS |
7109 FIND_LSB_TO_FLOAT_CAST |
7110 FIND_MSB_TO_FLOAT_CAST |
7111 IMUL_HIGH_TO_MUL
7112 : 0));
7113
7114 do_vec_index_to_cond_assign(ir);
7115 lower_vector_insert(ir, true);
7116 lower_quadop_vector(ir, false);
7117 lower_noise(ir);
7118 if (options->MaxIfDepth == 0) {
7119 lower_discard(ir);
7120 }
7121
7122 if (ctx->Const.GLSLOptimizeConservatively) {
7123 /* Do it once and repeat only if there's unsupported control flow. */
7124 do {
7125 do_common_optimization(ir, true, true, options,
7126 ctx->Const.NativeIntegers);
7127 lower_if_to_cond_assign((gl_shader_stage)i, ir,
7128 options->MaxIfDepth, if_threshold);
7129 } while (has_unsupported_control_flow(ir, options));
7130 } else {
7131 /* Repeat it until it stops making changes. */
7132 bool progress;
7133 do {
7134 progress = do_common_optimization(ir, true, true, options,
7135 ctx->Const.NativeIntegers);
7136 progress |= lower_if_to_cond_assign((gl_shader_stage)i, ir,
7137 options->MaxIfDepth, if_threshold);
7138 } while (progress);
7139 }
7140
7141 validate_ir_tree(ir);
7142 }
7143
7144 build_program_resource_list(ctx, prog);
7145
7146 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
7147 struct gl_linked_shader *shader = prog->_LinkedShaders[i];
7148 if (shader == NULL)
7149 continue;
7150
7151 enum pipe_shader_type ptarget =
7152 st_shader_stage_to_ptarget(shader->Stage);
7153 enum pipe_shader_ir preferred_ir = (enum pipe_shader_ir)
7154 pscreen->get_shader_param(pscreen, ptarget,
7155 PIPE_SHADER_CAP_PREFERRED_IR);
7156
7157 struct gl_program *linked_prog = NULL;
7158 if (preferred_ir == PIPE_SHADER_IR_NIR) {
7159 /* TODO only for GLSL VS/FS/CS for now: */
7160 switch (shader->Stage) {
7161 case MESA_SHADER_VERTEX:
7162 case MESA_SHADER_FRAGMENT:
7163 case MESA_SHADER_COMPUTE:
7164 linked_prog = st_nir_get_mesa_program(ctx, prog, shader);
7165 default:
7166 break;
7167 }
7168 } else {
7169 linked_prog = get_mesa_program_tgsi(ctx, prog, shader);
7170 }
7171
7172 if (linked_prog) {
7173 st_set_prog_affected_state_flags(linked_prog);
7174 if (!ctx->Driver.ProgramStringNotify(ctx,
7175 _mesa_shader_stage_to_program(i),
7176 linked_prog)) {
7177 _mesa_reference_program(ctx, &shader->Program, NULL);
7178 return GL_FALSE;
7179 }
7180 }
7181 }
7182
7183 return GL_TRUE;
7184 }
7185
7186 void
7187 st_translate_stream_output_info(glsl_to_tgsi_visitor *glsl_to_tgsi,
7188 const ubyte outputMapping[],
7189 struct pipe_stream_output_info *so)
7190 {
7191 if (!glsl_to_tgsi->shader_program->last_vert_prog)
7192 return;
7193
7194 struct gl_transform_feedback_info *info =
7195 glsl_to_tgsi->shader_program->last_vert_prog->sh.LinkedTransformFeedback;
7196 st_translate_stream_output_info2(info, outputMapping, so);
7197 }
7198
7199 void
7200 st_translate_stream_output_info2(struct gl_transform_feedback_info *info,
7201 const ubyte outputMapping[],
7202 struct pipe_stream_output_info *so)
7203 {
7204 unsigned i;
7205
7206 for (i = 0; i < info->NumOutputs; i++) {
7207 so->output[i].register_index =
7208 outputMapping[info->Outputs[i].OutputRegister];
7209 so->output[i].start_component = info->Outputs[i].ComponentOffset;
7210 so->output[i].num_components = info->Outputs[i].NumComponents;
7211 so->output[i].output_buffer = info->Outputs[i].OutputBuffer;
7212 so->output[i].dst_offset = info->Outputs[i].DstOffset;
7213 so->output[i].stream = info->Outputs[i].StreamId;
7214 }
7215
7216 for (i = 0; i < PIPE_MAX_SO_BUFFERS; i++) {
7217 so->stride[i] = info->Buffers[i].Stride;
7218 }
7219 so->num_outputs = info->NumOutputs;
7220 }
7221
7222 } /* extern "C" */