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