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