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