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