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