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