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