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