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