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