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