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