9cac30995afede45d6dbcd44c8123025465b96bd
[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 <stdio.h>
34 #include "main/compiler.h"
35 #include "ir.h"
36 #include "ir_visitor.h"
37 #include "ir_print_visitor.h"
38 #include "ir_expression_flattening.h"
39 #include "glsl_types.h"
40 #include "glsl_parser_extras.h"
41 #include "../glsl/program.h"
42 #include "ir_optimization.h"
43 #include "ast.h"
44
45 extern "C" {
46 #include "main/mtypes.h"
47 #include "main/shaderapi.h"
48 #include "main/shaderobj.h"
49 #include "main/uniforms.h"
50 #include "program/hash_table.h"
51 #include "program/prog_instruction.h"
52 #include "program/prog_optimize.h"
53 #include "program/prog_print.h"
54 #include "program/program.h"
55 #include "program/prog_uniform.h"
56 #include "program/prog_parameter.h"
57 #include "program/sampler.h"
58
59 #include "pipe/p_compiler.h"
60 #include "pipe/p_context.h"
61 #include "pipe/p_screen.h"
62 #include "pipe/p_shader_tokens.h"
63 #include "pipe/p_state.h"
64 #include "util/u_math.h"
65 #include "tgsi/tgsi_ureg.h"
66 #include "tgsi/tgsi_info.h"
67 #include "st_context.h"
68 #include "st_program.h"
69 #include "st_glsl_to_tgsi.h"
70 #include "st_mesa_to_tgsi.h"
71 }
72
73 #define PROGRAM_IMMEDIATE PROGRAM_FILE_MAX
74 #define PROGRAM_ANY_CONST ((1 << PROGRAM_LOCAL_PARAM) | \
75 (1 << PROGRAM_ENV_PARAM) | \
76 (1 << PROGRAM_STATE_VAR) | \
77 (1 << PROGRAM_NAMED_PARAM) | \
78 (1 << PROGRAM_CONSTANT) | \
79 (1 << PROGRAM_UNIFORM))
80
81 #define MAX_TEMPS 4096
82
83 class st_src_reg;
84 class st_dst_reg;
85
86 static int swizzle_for_size(int size);
87
88 /**
89 * This struct is a corresponding struct to TGSI ureg_src.
90 */
91 class st_src_reg {
92 public:
93 st_src_reg(gl_register_file file, int index, const glsl_type *type)
94 {
95 this->file = file;
96 this->index = index;
97 if (type && (type->is_scalar() || type->is_vector() || type->is_matrix()))
98 this->swizzle = swizzle_for_size(type->vector_elements);
99 else
100 this->swizzle = SWIZZLE_XYZW;
101 this->negate = 0;
102 this->type = type ? type->base_type : GLSL_TYPE_ERROR;
103 this->reladdr = NULL;
104 }
105
106 st_src_reg(gl_register_file file, int index, int type)
107 {
108 this->type = type;
109 this->file = file;
110 this->index = index;
111 this->swizzle = SWIZZLE_XYZW;
112 this->negate = 0;
113 this->reladdr = NULL;
114 }
115
116 st_src_reg()
117 {
118 this->type = GLSL_TYPE_ERROR;
119 this->file = PROGRAM_UNDEFINED;
120 this->index = 0;
121 this->swizzle = 0;
122 this->negate = 0;
123 this->reladdr = NULL;
124 }
125
126 explicit st_src_reg(st_dst_reg reg);
127
128 gl_register_file file; /**< PROGRAM_* from Mesa */
129 int index; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
130 GLuint swizzle; /**< SWIZZLE_XYZWONEZERO swizzles from Mesa. */
131 int negate; /**< NEGATE_XYZW mask from mesa */
132 int type; /** GLSL_TYPE_* from GLSL IR (enum glsl_base_type) */
133 /** Register index should be offset by the integer in this reg. */
134 st_src_reg *reladdr;
135 };
136
137 class st_dst_reg {
138 public:
139 st_dst_reg(gl_register_file file, int writemask, int type)
140 {
141 this->file = file;
142 this->index = 0;
143 this->writemask = writemask;
144 this->cond_mask = COND_TR;
145 this->reladdr = NULL;
146 this->type = type;
147 }
148
149 st_dst_reg()
150 {
151 this->type = GLSL_TYPE_ERROR;
152 this->file = PROGRAM_UNDEFINED;
153 this->index = 0;
154 this->writemask = 0;
155 this->cond_mask = COND_TR;
156 this->reladdr = NULL;
157 }
158
159 explicit st_dst_reg(st_src_reg reg);
160
161 gl_register_file file; /**< PROGRAM_* from Mesa */
162 int index; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
163 int writemask; /**< Bitfield of WRITEMASK_[XYZW] */
164 GLuint cond_mask:4;
165 int type; /** GLSL_TYPE_* from GLSL IR (enum glsl_base_type) */
166 /** Register index should be offset by the integer in this reg. */
167 st_src_reg *reladdr;
168 };
169
170 st_src_reg::st_src_reg(st_dst_reg reg)
171 {
172 this->type = reg.type;
173 this->file = reg.file;
174 this->index = reg.index;
175 this->swizzle = SWIZZLE_XYZW;
176 this->negate = 0;
177 this->reladdr = reg.reladdr;
178 }
179
180 st_dst_reg::st_dst_reg(st_src_reg reg)
181 {
182 this->type = reg.type;
183 this->file = reg.file;
184 this->index = reg.index;
185 this->writemask = WRITEMASK_XYZW;
186 this->cond_mask = COND_TR;
187 this->reladdr = reg.reladdr;
188 }
189
190 class glsl_to_tgsi_instruction : public exec_node {
191 public:
192 /* Callers of this ralloc-based new need not call delete. It's
193 * easier to just ralloc_free 'ctx' (or any of its ancestors). */
194 static void* operator new(size_t size, void *ctx)
195 {
196 void *node;
197
198 node = rzalloc_size(ctx, size);
199 assert(node != NULL);
200
201 return node;
202 }
203
204 unsigned op;
205 st_dst_reg dst;
206 st_src_reg src[3];
207 /** Pointer to the ir source this tree came from for debugging */
208 ir_instruction *ir;
209 GLboolean cond_update;
210 bool saturate;
211 int sampler; /**< sampler index */
212 int tex_target; /**< One of TEXTURE_*_INDEX */
213 GLboolean tex_shadow;
214 int dead_mask; /**< Used in dead code elimination */
215
216 class function_entry *function; /* Set on TGSI_OPCODE_CAL or TGSI_OPCODE_BGNSUB */
217 };
218
219 class variable_storage : public exec_node {
220 public:
221 variable_storage(ir_variable *var, gl_register_file file, int index)
222 : file(file), index(index), var(var)
223 {
224 /* empty */
225 }
226
227 gl_register_file file;
228 int index;
229 ir_variable *var; /* variable that maps to this, if any */
230 };
231
232 class immediate_storage : public exec_node {
233 public:
234 immediate_storage(gl_constant_value *values, int size, int type)
235 {
236 memcpy(this->values, values, size * sizeof(gl_constant_value));
237 this->size = size;
238 this->type = type;
239 }
240
241 gl_constant_value values[4];
242 int size; /**< Number of components (1-4) */
243 int type; /**< GL_FLOAT, GL_INT, GL_BOOL, or GL_UNSIGNED_INT */
244 };
245
246 class function_entry : public exec_node {
247 public:
248 ir_function_signature *sig;
249
250 /**
251 * identifier of this function signature used by the program.
252 *
253 * At the point that TGSI instructions for function calls are
254 * generated, we don't know the address of the first instruction of
255 * the function body. So we make the BranchTarget that is called a
256 * small integer and rewrite them during set_branchtargets().
257 */
258 int sig_id;
259
260 /**
261 * Pointer to first instruction of the function body.
262 *
263 * Set during function body emits after main() is processed.
264 */
265 glsl_to_tgsi_instruction *bgn_inst;
266
267 /**
268 * Index of the first instruction of the function body in actual TGSI.
269 *
270 * Set after conversion from glsl_to_tgsi_instruction to TGSI.
271 */
272 int inst;
273
274 /** Storage for the return value. */
275 st_src_reg return_reg;
276 };
277
278 class glsl_to_tgsi_visitor : public ir_visitor {
279 public:
280 glsl_to_tgsi_visitor();
281 ~glsl_to_tgsi_visitor();
282
283 function_entry *current_function;
284
285 struct gl_context *ctx;
286 struct gl_program *prog;
287 struct gl_shader_program *shader_program;
288 struct gl_shader_compiler_options *options;
289
290 int next_temp;
291
292 int num_address_regs;
293 int samplers_used;
294 bool indirect_addr_temps;
295 bool indirect_addr_consts;
296
297 int glsl_version;
298 bool native_integers;
299
300 variable_storage *find_variable_storage(ir_variable *var);
301
302 int add_constant(gl_register_file file, gl_constant_value values[4],
303 int size, int datatype, GLuint *swizzle_out);
304
305 function_entry *get_function_signature(ir_function_signature *sig);
306
307 st_src_reg get_temp(const glsl_type *type);
308 void reladdr_to_temp(ir_instruction *ir, st_src_reg *reg, int *num_reladdr);
309
310 st_src_reg st_src_reg_for_float(float val);
311 st_src_reg st_src_reg_for_int(int val);
312 st_src_reg st_src_reg_for_type(int type, int val);
313
314 /**
315 * \name Visit methods
316 *
317 * As typical for the visitor pattern, there must be one \c visit method for
318 * each concrete subclass of \c ir_instruction. Virtual base classes within
319 * the hierarchy should not have \c visit methods.
320 */
321 /*@{*/
322 virtual void visit(ir_variable *);
323 virtual void visit(ir_loop *);
324 virtual void visit(ir_loop_jump *);
325 virtual void visit(ir_function_signature *);
326 virtual void visit(ir_function *);
327 virtual void visit(ir_expression *);
328 virtual void visit(ir_swizzle *);
329 virtual void visit(ir_dereference_variable *);
330 virtual void visit(ir_dereference_array *);
331 virtual void visit(ir_dereference_record *);
332 virtual void visit(ir_assignment *);
333 virtual void visit(ir_constant *);
334 virtual void visit(ir_call *);
335 virtual void visit(ir_return *);
336 virtual void visit(ir_discard *);
337 virtual void visit(ir_texture *);
338 virtual void visit(ir_if *);
339 /*@}*/
340
341 st_src_reg result;
342
343 /** List of variable_storage */
344 exec_list variables;
345
346 /** List of immediate_storage */
347 exec_list immediates;
348 int num_immediates;
349
350 /** List of function_entry */
351 exec_list function_signatures;
352 int next_signature_id;
353
354 /** List of glsl_to_tgsi_instruction */
355 exec_list instructions;
356
357 glsl_to_tgsi_instruction *emit(ir_instruction *ir, unsigned op);
358
359 glsl_to_tgsi_instruction *emit(ir_instruction *ir, unsigned op,
360 st_dst_reg dst, st_src_reg src0);
361
362 glsl_to_tgsi_instruction *emit(ir_instruction *ir, unsigned op,
363 st_dst_reg dst, st_src_reg src0, st_src_reg src1);
364
365 glsl_to_tgsi_instruction *emit(ir_instruction *ir, unsigned op,
366 st_dst_reg dst,
367 st_src_reg src0, st_src_reg src1, st_src_reg src2);
368
369 unsigned get_opcode(ir_instruction *ir, unsigned op,
370 st_dst_reg dst,
371 st_src_reg src0, st_src_reg src1);
372
373 /**
374 * Emit the correct dot-product instruction for the type of arguments
375 */
376 glsl_to_tgsi_instruction *emit_dp(ir_instruction *ir,
377 st_dst_reg dst,
378 st_src_reg src0,
379 st_src_reg src1,
380 unsigned elements);
381
382 void emit_scalar(ir_instruction *ir, unsigned op,
383 st_dst_reg dst, st_src_reg src0);
384
385 void emit_scalar(ir_instruction *ir, unsigned op,
386 st_dst_reg dst, st_src_reg src0, st_src_reg src1);
387
388 void emit_arl(ir_instruction *ir, st_dst_reg dst, st_src_reg src0);
389
390 void emit_scs(ir_instruction *ir, unsigned op,
391 st_dst_reg dst, const st_src_reg &src);
392
393 bool try_emit_mad(ir_expression *ir,
394 int mul_operand);
395 bool try_emit_mad_for_and_not(ir_expression *ir,
396 int mul_operand);
397 bool try_emit_sat(ir_expression *ir);
398
399 void emit_swz(ir_expression *ir);
400
401 bool process_move_condition(ir_rvalue *ir);
402
403 void remove_output_reads(gl_register_file type);
404 void simplify_cmp(void);
405
406 void rename_temp_register(int index, int new_index);
407 int get_first_temp_read(int index);
408 int get_first_temp_write(int index);
409 int get_last_temp_read(int index);
410 int get_last_temp_write(int index);
411
412 void copy_propagate(void);
413 void eliminate_dead_code(void);
414 int eliminate_dead_code_advanced(void);
415 void merge_registers(void);
416 void renumber_registers(void);
417
418 void *mem_ctx;
419 };
420
421 static st_src_reg undef_src = st_src_reg(PROGRAM_UNDEFINED, 0, GLSL_TYPE_ERROR);
422
423 static st_dst_reg undef_dst = st_dst_reg(PROGRAM_UNDEFINED, SWIZZLE_NOOP, GLSL_TYPE_ERROR);
424
425 static st_dst_reg address_reg = st_dst_reg(PROGRAM_ADDRESS, WRITEMASK_X, GLSL_TYPE_FLOAT);
426
427 static void
428 fail_link(struct gl_shader_program *prog, const char *fmt, ...) PRINTFLIKE(2, 3);
429
430 static void
431 fail_link(struct gl_shader_program *prog, const char *fmt, ...)
432 {
433 va_list args;
434 va_start(args, fmt);
435 ralloc_vasprintf_append(&prog->InfoLog, fmt, args);
436 va_end(args);
437
438 prog->LinkStatus = GL_FALSE;
439 }
440
441 static int
442 swizzle_for_size(int size)
443 {
444 int size_swizzles[4] = {
445 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X),
446 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Y, SWIZZLE_Y),
447 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z),
448 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W),
449 };
450
451 assert((size >= 1) && (size <= 4));
452 return size_swizzles[size - 1];
453 }
454
455 static bool
456 is_tex_instruction(unsigned opcode)
457 {
458 const tgsi_opcode_info* info = tgsi_get_opcode_info(opcode);
459 return info->is_tex;
460 }
461
462 static unsigned
463 num_inst_dst_regs(unsigned opcode)
464 {
465 const tgsi_opcode_info* info = tgsi_get_opcode_info(opcode);
466 return info->num_dst;
467 }
468
469 static unsigned
470 num_inst_src_regs(unsigned opcode)
471 {
472 const tgsi_opcode_info* info = tgsi_get_opcode_info(opcode);
473 return info->is_tex ? info->num_src - 1 : info->num_src;
474 }
475
476 glsl_to_tgsi_instruction *
477 glsl_to_tgsi_visitor::emit(ir_instruction *ir, unsigned op,
478 st_dst_reg dst,
479 st_src_reg src0, st_src_reg src1, st_src_reg src2)
480 {
481 glsl_to_tgsi_instruction *inst = new(mem_ctx) glsl_to_tgsi_instruction();
482 int num_reladdr = 0, i;
483
484 op = get_opcode(ir, op, dst, src0, src1);
485
486 /* If we have to do relative addressing, we want to load the ARL
487 * reg directly for one of the regs, and preload the other reladdr
488 * sources into temps.
489 */
490 num_reladdr += dst.reladdr != NULL;
491 num_reladdr += src0.reladdr != NULL;
492 num_reladdr += src1.reladdr != NULL;
493 num_reladdr += src2.reladdr != NULL;
494
495 reladdr_to_temp(ir, &src2, &num_reladdr);
496 reladdr_to_temp(ir, &src1, &num_reladdr);
497 reladdr_to_temp(ir, &src0, &num_reladdr);
498
499 if (dst.reladdr) {
500 emit_arl(ir, address_reg, *dst.reladdr);
501 num_reladdr--;
502 }
503 assert(num_reladdr == 0);
504
505 inst->op = op;
506 inst->dst = dst;
507 inst->src[0] = src0;
508 inst->src[1] = src1;
509 inst->src[2] = src2;
510 inst->ir = ir;
511 inst->dead_mask = 0;
512
513 inst->function = NULL;
514
515 if (op == TGSI_OPCODE_ARL)
516 this->num_address_regs = 1;
517
518 /* Update indirect addressing status used by TGSI */
519 if (dst.reladdr) {
520 switch(dst.file) {
521 case PROGRAM_TEMPORARY:
522 this->indirect_addr_temps = true;
523 break;
524 case PROGRAM_LOCAL_PARAM:
525 case PROGRAM_ENV_PARAM:
526 case PROGRAM_STATE_VAR:
527 case PROGRAM_NAMED_PARAM:
528 case PROGRAM_CONSTANT:
529 case PROGRAM_UNIFORM:
530 this->indirect_addr_consts = true;
531 break;
532 case PROGRAM_IMMEDIATE:
533 assert(!"immediates should not have indirect addressing");
534 break;
535 default:
536 break;
537 }
538 }
539 else {
540 for (i=0; i<3; i++) {
541 if(inst->src[i].reladdr) {
542 switch(inst->src[i].file) {
543 case PROGRAM_TEMPORARY:
544 this->indirect_addr_temps = true;
545 break;
546 case PROGRAM_LOCAL_PARAM:
547 case PROGRAM_ENV_PARAM:
548 case PROGRAM_STATE_VAR:
549 case PROGRAM_NAMED_PARAM:
550 case PROGRAM_CONSTANT:
551 case PROGRAM_UNIFORM:
552 this->indirect_addr_consts = true;
553 break;
554 case PROGRAM_IMMEDIATE:
555 assert(!"immediates should not have indirect addressing");
556 break;
557 default:
558 break;
559 }
560 }
561 }
562 }
563
564 this->instructions.push_tail(inst);
565
566 return inst;
567 }
568
569
570 glsl_to_tgsi_instruction *
571 glsl_to_tgsi_visitor::emit(ir_instruction *ir, unsigned op,
572 st_dst_reg dst, st_src_reg src0, st_src_reg src1)
573 {
574 return emit(ir, op, dst, src0, src1, undef_src);
575 }
576
577 glsl_to_tgsi_instruction *
578 glsl_to_tgsi_visitor::emit(ir_instruction *ir, unsigned op,
579 st_dst_reg dst, st_src_reg src0)
580 {
581 assert(dst.writemask != 0);
582 return emit(ir, op, dst, src0, undef_src, undef_src);
583 }
584
585 glsl_to_tgsi_instruction *
586 glsl_to_tgsi_visitor::emit(ir_instruction *ir, unsigned op)
587 {
588 return emit(ir, op, undef_dst, undef_src, undef_src, undef_src);
589 }
590
591 /**
592 * Determines whether to use an integer, unsigned integer, or float opcode
593 * based on the operands and input opcode, then emits the result.
594 *
595 * TODO: type checking for remaining TGSI opcodes
596 */
597 unsigned
598 glsl_to_tgsi_visitor::get_opcode(ir_instruction *ir, unsigned op,
599 st_dst_reg dst,
600 st_src_reg src0, st_src_reg src1)
601 {
602 int type = GLSL_TYPE_FLOAT;
603
604 if (src0.type == GLSL_TYPE_FLOAT || src1.type == GLSL_TYPE_FLOAT)
605 type = GLSL_TYPE_FLOAT;
606 else if (native_integers)
607 type = src0.type;
608
609 #define case4(c, f, i, u) \
610 case TGSI_OPCODE_##c: \
611 if (type == GLSL_TYPE_INT) op = TGSI_OPCODE_##i; \
612 else if (type == GLSL_TYPE_UINT) op = TGSI_OPCODE_##u; \
613 else op = TGSI_OPCODE_##f; \
614 break;
615 #define case3(f, i, u) case4(f, f, i, u)
616 #define case2fi(f, i) case4(f, f, i, i)
617 #define case2iu(i, u) case4(i, LAST, i, u)
618
619 switch(op) {
620 case2fi(ADD, UADD);
621 case2fi(MUL, UMUL);
622 case2fi(MAD, UMAD);
623 case3(DIV, IDIV, UDIV);
624 case3(MAX, IMAX, UMAX);
625 case3(MIN, IMIN, UMIN);
626 case2iu(MOD, UMOD);
627
628 case2fi(SEQ, USEQ);
629 case2fi(SNE, USNE);
630 case3(SGE, ISGE, USGE);
631 case3(SLT, ISLT, USLT);
632
633 case2iu(SHL, SHL);
634 case2iu(ISHR, USHR);
635 case2iu(NOT, NOT);
636 case2iu(AND, AND);
637 case2iu(OR, OR);
638 case2iu(XOR, XOR);
639
640 default: break;
641 }
642
643 assert(op != TGSI_OPCODE_LAST);
644 return op;
645 }
646
647 glsl_to_tgsi_instruction *
648 glsl_to_tgsi_visitor::emit_dp(ir_instruction *ir,
649 st_dst_reg dst, st_src_reg src0, st_src_reg src1,
650 unsigned elements)
651 {
652 static const unsigned dot_opcodes[] = {
653 TGSI_OPCODE_DP2, TGSI_OPCODE_DP3, TGSI_OPCODE_DP4
654 };
655
656 return emit(ir, dot_opcodes[elements - 2], dst, src0, src1);
657 }
658
659 /**
660 * Emits TGSI scalar opcodes to produce unique answers across channels.
661 *
662 * Some TGSI opcodes are scalar-only, like ARB_fp/vp. The src X
663 * channel determines the result across all channels. So to do a vec4
664 * of this operation, we want to emit a scalar per source channel used
665 * to produce dest channels.
666 */
667 void
668 glsl_to_tgsi_visitor::emit_scalar(ir_instruction *ir, unsigned op,
669 st_dst_reg dst,
670 st_src_reg orig_src0, st_src_reg orig_src1)
671 {
672 int i, j;
673 int done_mask = ~dst.writemask;
674
675 /* TGSI RCP is a scalar operation splatting results to all channels,
676 * like ARB_fp/vp. So emit as many RCPs as necessary to cover our
677 * dst channels.
678 */
679 for (i = 0; i < 4; i++) {
680 GLuint this_mask = (1 << i);
681 glsl_to_tgsi_instruction *inst;
682 st_src_reg src0 = orig_src0;
683 st_src_reg src1 = orig_src1;
684
685 if (done_mask & this_mask)
686 continue;
687
688 GLuint src0_swiz = GET_SWZ(src0.swizzle, i);
689 GLuint src1_swiz = GET_SWZ(src1.swizzle, i);
690 for (j = i + 1; j < 4; j++) {
691 /* If there is another enabled component in the destination that is
692 * derived from the same inputs, generate its value on this pass as
693 * well.
694 */
695 if (!(done_mask & (1 << j)) &&
696 GET_SWZ(src0.swizzle, j) == src0_swiz &&
697 GET_SWZ(src1.swizzle, j) == src1_swiz) {
698 this_mask |= (1 << j);
699 }
700 }
701 src0.swizzle = MAKE_SWIZZLE4(src0_swiz, src0_swiz,
702 src0_swiz, src0_swiz);
703 src1.swizzle = MAKE_SWIZZLE4(src1_swiz, src1_swiz,
704 src1_swiz, src1_swiz);
705
706 inst = emit(ir, op, dst, src0, src1);
707 inst->dst.writemask = this_mask;
708 done_mask |= this_mask;
709 }
710 }
711
712 void
713 glsl_to_tgsi_visitor::emit_scalar(ir_instruction *ir, unsigned op,
714 st_dst_reg dst, st_src_reg src0)
715 {
716 st_src_reg undef = undef_src;
717
718 undef.swizzle = SWIZZLE_XXXX;
719
720 emit_scalar(ir, op, dst, src0, undef);
721 }
722
723 void
724 glsl_to_tgsi_visitor::emit_arl(ir_instruction *ir,
725 st_dst_reg dst, st_src_reg src0)
726 {
727 st_src_reg tmp = get_temp(glsl_type::float_type);
728
729 if (src0.type == GLSL_TYPE_INT)
730 emit(NULL, TGSI_OPCODE_I2F, st_dst_reg(tmp), src0);
731 else if (src0.type == GLSL_TYPE_UINT)
732 emit(NULL, TGSI_OPCODE_U2F, st_dst_reg(tmp), src0);
733 else
734 tmp = src0;
735
736 emit(NULL, TGSI_OPCODE_ARL, dst, tmp);
737 }
738
739 /**
740 * Emit an TGSI_OPCODE_SCS instruction
741 *
742 * The \c SCS opcode functions a bit differently than the other TGSI opcodes.
743 * Instead of splatting its result across all four components of the
744 * destination, it writes one value to the \c x component and another value to
745 * the \c y component.
746 *
747 * \param ir IR instruction being processed
748 * \param op Either \c TGSI_OPCODE_SIN or \c TGSI_OPCODE_COS depending
749 * on which value is desired.
750 * \param dst Destination register
751 * \param src Source register
752 */
753 void
754 glsl_to_tgsi_visitor::emit_scs(ir_instruction *ir, unsigned op,
755 st_dst_reg dst,
756 const st_src_reg &src)
757 {
758 /* Vertex programs cannot use the SCS opcode.
759 */
760 if (this->prog->Target == GL_VERTEX_PROGRAM_ARB) {
761 emit_scalar(ir, op, dst, src);
762 return;
763 }
764
765 const unsigned component = (op == TGSI_OPCODE_SIN) ? 0 : 1;
766 const unsigned scs_mask = (1U << component);
767 int done_mask = ~dst.writemask;
768 st_src_reg tmp;
769
770 assert(op == TGSI_OPCODE_SIN || op == TGSI_OPCODE_COS);
771
772 /* If there are compnents in the destination that differ from the component
773 * that will be written by the SCS instrution, we'll need a temporary.
774 */
775 if (scs_mask != unsigned(dst.writemask)) {
776 tmp = get_temp(glsl_type::vec4_type);
777 }
778
779 for (unsigned i = 0; i < 4; i++) {
780 unsigned this_mask = (1U << i);
781 st_src_reg src0 = src;
782
783 if ((done_mask & this_mask) != 0)
784 continue;
785
786 /* The source swizzle specified which component of the source generates
787 * sine / cosine for the current component in the destination. The SCS
788 * instruction requires that this value be swizzle to the X component.
789 * Replace the current swizzle with a swizzle that puts the source in
790 * the X component.
791 */
792 unsigned src0_swiz = GET_SWZ(src.swizzle, i);
793
794 src0.swizzle = MAKE_SWIZZLE4(src0_swiz, src0_swiz,
795 src0_swiz, src0_swiz);
796 for (unsigned j = i + 1; j < 4; j++) {
797 /* If there is another enabled component in the destination that is
798 * derived from the same inputs, generate its value on this pass as
799 * well.
800 */
801 if (!(done_mask & (1 << j)) &&
802 GET_SWZ(src0.swizzle, j) == src0_swiz) {
803 this_mask |= (1 << j);
804 }
805 }
806
807 if (this_mask != scs_mask) {
808 glsl_to_tgsi_instruction *inst;
809 st_dst_reg tmp_dst = st_dst_reg(tmp);
810
811 /* Emit the SCS instruction.
812 */
813 inst = emit(ir, TGSI_OPCODE_SCS, tmp_dst, src0);
814 inst->dst.writemask = scs_mask;
815
816 /* Move the result of the SCS instruction to the desired location in
817 * the destination.
818 */
819 tmp.swizzle = MAKE_SWIZZLE4(component, component,
820 component, component);
821 inst = emit(ir, TGSI_OPCODE_SCS, dst, tmp);
822 inst->dst.writemask = this_mask;
823 } else {
824 /* Emit the SCS instruction to write directly to the destination.
825 */
826 glsl_to_tgsi_instruction *inst = emit(ir, TGSI_OPCODE_SCS, dst, src0);
827 inst->dst.writemask = scs_mask;
828 }
829
830 done_mask |= this_mask;
831 }
832 }
833
834 int
835 glsl_to_tgsi_visitor::add_constant(gl_register_file file,
836 gl_constant_value values[4], int size, int datatype,
837 GLuint *swizzle_out)
838 {
839 if (file == PROGRAM_CONSTANT) {
840 return _mesa_add_typed_unnamed_constant(this->prog->Parameters, values,
841 size, datatype, swizzle_out);
842 } else {
843 int index = 0;
844 immediate_storage *entry;
845 assert(file == PROGRAM_IMMEDIATE);
846
847 /* Search immediate storage to see if we already have an identical
848 * immediate that we can use instead of adding a duplicate entry.
849 */
850 foreach_iter(exec_list_iterator, iter, this->immediates) {
851 entry = (immediate_storage *)iter.get();
852
853 if (entry->size == size &&
854 entry->type == datatype &&
855 !memcmp(entry->values, values, size * sizeof(gl_constant_value))) {
856 return index;
857 }
858 index++;
859 }
860
861 /* Add this immediate to the list. */
862 entry = new(mem_ctx) immediate_storage(values, size, datatype);
863 this->immediates.push_tail(entry);
864 this->num_immediates++;
865 return index;
866 }
867 }
868
869 st_src_reg
870 glsl_to_tgsi_visitor::st_src_reg_for_float(float val)
871 {
872 st_src_reg src(PROGRAM_IMMEDIATE, -1, GLSL_TYPE_FLOAT);
873 union gl_constant_value uval;
874
875 uval.f = val;
876 src.index = add_constant(src.file, &uval, 1, GL_FLOAT, &src.swizzle);
877
878 return src;
879 }
880
881 st_src_reg
882 glsl_to_tgsi_visitor::st_src_reg_for_int(int val)
883 {
884 st_src_reg src(PROGRAM_IMMEDIATE, -1, GLSL_TYPE_INT);
885 union gl_constant_value uval;
886
887 assert(native_integers);
888
889 uval.i = val;
890 src.index = add_constant(src.file, &uval, 1, GL_INT, &src.swizzle);
891
892 return src;
893 }
894
895 st_src_reg
896 glsl_to_tgsi_visitor::st_src_reg_for_type(int type, int val)
897 {
898 if (native_integers)
899 return type == GLSL_TYPE_FLOAT ? st_src_reg_for_float(val) :
900 st_src_reg_for_int(val);
901 else
902 return st_src_reg_for_float(val);
903 }
904
905 static int
906 type_size(const struct glsl_type *type)
907 {
908 unsigned int i;
909 int size;
910
911 switch (type->base_type) {
912 case GLSL_TYPE_UINT:
913 case GLSL_TYPE_INT:
914 case GLSL_TYPE_FLOAT:
915 case GLSL_TYPE_BOOL:
916 if (type->is_matrix()) {
917 return type->matrix_columns;
918 } else {
919 /* Regardless of size of vector, it gets a vec4. This is bad
920 * packing for things like floats, but otherwise arrays become a
921 * mess. Hopefully a later pass over the code can pack scalars
922 * down if appropriate.
923 */
924 return 1;
925 }
926 case GLSL_TYPE_ARRAY:
927 assert(type->length > 0);
928 return type_size(type->fields.array) * type->length;
929 case GLSL_TYPE_STRUCT:
930 size = 0;
931 for (i = 0; i < type->length; i++) {
932 size += type_size(type->fields.structure[i].type);
933 }
934 return size;
935 case GLSL_TYPE_SAMPLER:
936 /* Samplers take up one slot in UNIFORMS[], but they're baked in
937 * at link time.
938 */
939 return 1;
940 default:
941 assert(0);
942 return 0;
943 }
944 }
945
946 /**
947 * In the initial pass of codegen, we assign temporary numbers to
948 * intermediate results. (not SSA -- variable assignments will reuse
949 * storage).
950 */
951 st_src_reg
952 glsl_to_tgsi_visitor::get_temp(const glsl_type *type)
953 {
954 st_src_reg src;
955
956 src.type = native_integers ? type->base_type : GLSL_TYPE_FLOAT;
957 src.file = PROGRAM_TEMPORARY;
958 src.index = next_temp;
959 src.reladdr = NULL;
960 next_temp += type_size(type);
961
962 if (type->is_array() || type->is_record()) {
963 src.swizzle = SWIZZLE_NOOP;
964 } else {
965 src.swizzle = swizzle_for_size(type->vector_elements);
966 }
967 src.negate = 0;
968
969 return src;
970 }
971
972 variable_storage *
973 glsl_to_tgsi_visitor::find_variable_storage(ir_variable *var)
974 {
975
976 variable_storage *entry;
977
978 foreach_iter(exec_list_iterator, iter, this->variables) {
979 entry = (variable_storage *)iter.get();
980
981 if (entry->var == var)
982 return entry;
983 }
984
985 return NULL;
986 }
987
988 void
989 glsl_to_tgsi_visitor::visit(ir_variable *ir)
990 {
991 if (strcmp(ir->name, "gl_FragCoord") == 0) {
992 struct gl_fragment_program *fp = (struct gl_fragment_program *)this->prog;
993
994 fp->OriginUpperLeft = ir->origin_upper_left;
995 fp->PixelCenterInteger = ir->pixel_center_integer;
996
997 } else if (strcmp(ir->name, "gl_FragDepth") == 0) {
998 struct gl_fragment_program *fp = (struct gl_fragment_program *)this->prog;
999 switch (ir->depth_layout) {
1000 case ir_depth_layout_none:
1001 fp->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
1002 break;
1003 case ir_depth_layout_any:
1004 fp->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
1005 break;
1006 case ir_depth_layout_greater:
1007 fp->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
1008 break;
1009 case ir_depth_layout_less:
1010 fp->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
1011 break;
1012 case ir_depth_layout_unchanged:
1013 fp->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
1014 break;
1015 default:
1016 assert(0);
1017 break;
1018 }
1019 }
1020
1021 if (ir->mode == ir_var_uniform && strncmp(ir->name, "gl_", 3) == 0) {
1022 unsigned int i;
1023 const ir_state_slot *const slots = ir->state_slots;
1024 assert(ir->state_slots != NULL);
1025
1026 /* Check if this statevar's setup in the STATE file exactly
1027 * matches how we'll want to reference it as a
1028 * struct/array/whatever. If not, then we need to move it into
1029 * temporary storage and hope that it'll get copy-propagated
1030 * out.
1031 */
1032 for (i = 0; i < ir->num_state_slots; i++) {
1033 if (slots[i].swizzle != SWIZZLE_XYZW) {
1034 break;
1035 }
1036 }
1037
1038 variable_storage *storage;
1039 st_dst_reg dst;
1040 if (i == ir->num_state_slots) {
1041 /* We'll set the index later. */
1042 storage = new(mem_ctx) variable_storage(ir, PROGRAM_STATE_VAR, -1);
1043 this->variables.push_tail(storage);
1044
1045 dst = undef_dst;
1046 } else {
1047 /* The variable_storage constructor allocates slots based on the size
1048 * of the type. However, this had better match the number of state
1049 * elements that we're going to copy into the new temporary.
1050 */
1051 assert((int) ir->num_state_slots == type_size(ir->type));
1052
1053 storage = new(mem_ctx) variable_storage(ir, PROGRAM_TEMPORARY,
1054 this->next_temp);
1055 this->variables.push_tail(storage);
1056 this->next_temp += type_size(ir->type);
1057
1058 dst = st_dst_reg(st_src_reg(PROGRAM_TEMPORARY, storage->index,
1059 native_integers ? ir->type->base_type : GLSL_TYPE_FLOAT));
1060 }
1061
1062
1063 for (unsigned int i = 0; i < ir->num_state_slots; i++) {
1064 int index = _mesa_add_state_reference(this->prog->Parameters,
1065 (gl_state_index *)slots[i].tokens);
1066
1067 if (storage->file == PROGRAM_STATE_VAR) {
1068 if (storage->index == -1) {
1069 storage->index = index;
1070 } else {
1071 assert(index == storage->index + (int)i);
1072 }
1073 } else {
1074 st_src_reg src(PROGRAM_STATE_VAR, index,
1075 native_integers ? ir->type->base_type : GLSL_TYPE_FLOAT);
1076 src.swizzle = slots[i].swizzle;
1077 emit(ir, TGSI_OPCODE_MOV, dst, src);
1078 /* even a float takes up a whole vec4 reg in a struct/array. */
1079 dst.index++;
1080 }
1081 }
1082
1083 if (storage->file == PROGRAM_TEMPORARY &&
1084 dst.index != storage->index + (int) ir->num_state_slots) {
1085 fail_link(this->shader_program,
1086 "failed to load builtin uniform `%s' (%d/%d regs loaded)\n",
1087 ir->name, dst.index - storage->index,
1088 type_size(ir->type));
1089 }
1090 }
1091 }
1092
1093 void
1094 glsl_to_tgsi_visitor::visit(ir_loop *ir)
1095 {
1096 ir_dereference_variable *counter = NULL;
1097
1098 if (ir->counter != NULL)
1099 counter = new(ir) ir_dereference_variable(ir->counter);
1100
1101 if (ir->from != NULL) {
1102 assert(ir->counter != NULL);
1103
1104 ir_assignment *a = new(ir) ir_assignment(counter, ir->from, NULL);
1105
1106 a->accept(this);
1107 delete a;
1108 }
1109
1110 emit(NULL, TGSI_OPCODE_BGNLOOP);
1111
1112 if (ir->to) {
1113 ir_expression *e =
1114 new(ir) ir_expression(ir->cmp, glsl_type::bool_type,
1115 counter, ir->to);
1116 ir_if *if_stmt = new(ir) ir_if(e);
1117
1118 ir_loop_jump *brk = new(ir) ir_loop_jump(ir_loop_jump::jump_break);
1119
1120 if_stmt->then_instructions.push_tail(brk);
1121
1122 if_stmt->accept(this);
1123
1124 delete if_stmt;
1125 delete e;
1126 delete brk;
1127 }
1128
1129 visit_exec_list(&ir->body_instructions, this);
1130
1131 if (ir->increment) {
1132 ir_expression *e =
1133 new(ir) ir_expression(ir_binop_add, counter->type,
1134 counter, ir->increment);
1135
1136 ir_assignment *a = new(ir) ir_assignment(counter, e, NULL);
1137
1138 a->accept(this);
1139 delete a;
1140 delete e;
1141 }
1142
1143 emit(NULL, TGSI_OPCODE_ENDLOOP);
1144 }
1145
1146 void
1147 glsl_to_tgsi_visitor::visit(ir_loop_jump *ir)
1148 {
1149 switch (ir->mode) {
1150 case ir_loop_jump::jump_break:
1151 emit(NULL, TGSI_OPCODE_BRK);
1152 break;
1153 case ir_loop_jump::jump_continue:
1154 emit(NULL, TGSI_OPCODE_CONT);
1155 break;
1156 }
1157 }
1158
1159
1160 void
1161 glsl_to_tgsi_visitor::visit(ir_function_signature *ir)
1162 {
1163 assert(0);
1164 (void)ir;
1165 }
1166
1167 void
1168 glsl_to_tgsi_visitor::visit(ir_function *ir)
1169 {
1170 /* Ignore function bodies other than main() -- we shouldn't see calls to
1171 * them since they should all be inlined before we get to glsl_to_tgsi.
1172 */
1173 if (strcmp(ir->name, "main") == 0) {
1174 const ir_function_signature *sig;
1175 exec_list empty;
1176
1177 sig = ir->matching_signature(&empty);
1178
1179 assert(sig);
1180
1181 foreach_iter(exec_list_iterator, iter, sig->body) {
1182 ir_instruction *ir = (ir_instruction *)iter.get();
1183
1184 ir->accept(this);
1185 }
1186 }
1187 }
1188
1189 bool
1190 glsl_to_tgsi_visitor::try_emit_mad(ir_expression *ir, int mul_operand)
1191 {
1192 int nonmul_operand = 1 - mul_operand;
1193 st_src_reg a, b, c;
1194 st_dst_reg result_dst;
1195
1196 ir_expression *expr = ir->operands[mul_operand]->as_expression();
1197 if (!expr || expr->operation != ir_binop_mul)
1198 return false;
1199
1200 expr->operands[0]->accept(this);
1201 a = this->result;
1202 expr->operands[1]->accept(this);
1203 b = this->result;
1204 ir->operands[nonmul_operand]->accept(this);
1205 c = this->result;
1206
1207 this->result = get_temp(ir->type);
1208 result_dst = st_dst_reg(this->result);
1209 result_dst.writemask = (1 << ir->type->vector_elements) - 1;
1210 emit(ir, TGSI_OPCODE_MAD, result_dst, a, b, c);
1211
1212 return true;
1213 }
1214
1215 /**
1216 * Emit MAD(a, -b, a) instead of AND(a, NOT(b))
1217 *
1218 * The logic values are 1.0 for true and 0.0 for false. Logical-and is
1219 * implemented using multiplication, and logical-or is implemented using
1220 * addition. Logical-not can be implemented as (true - x), or (1.0 - x).
1221 * As result, the logical expression (a & !b) can be rewritten as:
1222 *
1223 * - a * !b
1224 * - a * (1 - b)
1225 * - (a * 1) - (a * b)
1226 * - a + -(a * b)
1227 * - a + (a * -b)
1228 *
1229 * This final expression can be implemented as a single MAD(a, -b, a)
1230 * instruction.
1231 */
1232 bool
1233 glsl_to_tgsi_visitor::try_emit_mad_for_and_not(ir_expression *ir, int try_operand)
1234 {
1235 const int other_operand = 1 - try_operand;
1236 st_src_reg a, b;
1237
1238 ir_expression *expr = ir->operands[try_operand]->as_expression();
1239 if (!expr || expr->operation != ir_unop_logic_not)
1240 return false;
1241
1242 ir->operands[other_operand]->accept(this);
1243 a = this->result;
1244 expr->operands[0]->accept(this);
1245 b = this->result;
1246
1247 b.negate = ~b.negate;
1248
1249 this->result = get_temp(ir->type);
1250 emit(ir, TGSI_OPCODE_MAD, st_dst_reg(this->result), a, b, a);
1251
1252 return true;
1253 }
1254
1255 bool
1256 glsl_to_tgsi_visitor::try_emit_sat(ir_expression *ir)
1257 {
1258 /* Saturates were only introduced to vertex programs in
1259 * NV_vertex_program3, so don't give them to drivers in the VP.
1260 */
1261 if (this->prog->Target == GL_VERTEX_PROGRAM_ARB)
1262 return false;
1263
1264 ir_rvalue *sat_src = ir->as_rvalue_to_saturate();
1265 if (!sat_src)
1266 return false;
1267
1268 sat_src->accept(this);
1269 st_src_reg src = this->result;
1270
1271 /* If we generated an expression instruction into a temporary in
1272 * processing the saturate's operand, apply the saturate to that
1273 * instruction. Otherwise, generate a MOV to do the saturate.
1274 *
1275 * Note that we have to be careful to only do this optimization if
1276 * the instruction in question was what generated src->result. For
1277 * example, ir_dereference_array might generate a MUL instruction
1278 * to create the reladdr, and return us a src reg using that
1279 * reladdr. That MUL result is not the value we're trying to
1280 * saturate.
1281 */
1282 ir_expression *sat_src_expr = sat_src->as_expression();
1283 if (sat_src_expr && (sat_src_expr->operation == ir_binop_mul ||
1284 sat_src_expr->operation == ir_binop_add ||
1285 sat_src_expr->operation == ir_binop_dot)) {
1286 glsl_to_tgsi_instruction *new_inst;
1287 new_inst = (glsl_to_tgsi_instruction *)this->instructions.get_tail();
1288 new_inst->saturate = true;
1289 } else {
1290 this->result = get_temp(ir->type);
1291 st_dst_reg result_dst = st_dst_reg(this->result);
1292 result_dst.writemask = (1 << ir->type->vector_elements) - 1;
1293 glsl_to_tgsi_instruction *inst;
1294 inst = emit(ir, TGSI_OPCODE_MOV, result_dst, src);
1295 inst->saturate = true;
1296 }
1297
1298 return true;
1299 }
1300
1301 void
1302 glsl_to_tgsi_visitor::reladdr_to_temp(ir_instruction *ir,
1303 st_src_reg *reg, int *num_reladdr)
1304 {
1305 if (!reg->reladdr)
1306 return;
1307
1308 emit_arl(ir, address_reg, *reg->reladdr);
1309
1310 if (*num_reladdr != 1) {
1311 st_src_reg temp = get_temp(glsl_type::vec4_type);
1312
1313 emit(ir, TGSI_OPCODE_MOV, st_dst_reg(temp), *reg);
1314 *reg = temp;
1315 }
1316
1317 (*num_reladdr)--;
1318 }
1319
1320 void
1321 glsl_to_tgsi_visitor::visit(ir_expression *ir)
1322 {
1323 unsigned int operand;
1324 st_src_reg op[Elements(ir->operands)];
1325 st_src_reg result_src;
1326 st_dst_reg result_dst;
1327
1328 /* Quick peephole: Emit MAD(a, b, c) instead of ADD(MUL(a, b), c)
1329 */
1330 if (ir->operation == ir_binop_add) {
1331 if (try_emit_mad(ir, 1))
1332 return;
1333 if (try_emit_mad(ir, 0))
1334 return;
1335 }
1336
1337 /* Quick peephole: Emit OPCODE_MAD(-a, -b, a) instead of AND(a, NOT(b))
1338 */
1339 if (ir->operation == ir_binop_logic_and) {
1340 if (try_emit_mad_for_and_not(ir, 1))
1341 return;
1342 if (try_emit_mad_for_and_not(ir, 0))
1343 return;
1344 }
1345
1346 if (try_emit_sat(ir))
1347 return;
1348
1349 if (ir->operation == ir_quadop_vector)
1350 assert(!"ir_quadop_vector should have been lowered");
1351
1352 for (operand = 0; operand < ir->get_num_operands(); operand++) {
1353 this->result.file = PROGRAM_UNDEFINED;
1354 ir->operands[operand]->accept(this);
1355 if (this->result.file == PROGRAM_UNDEFINED) {
1356 ir_print_visitor v;
1357 printf("Failed to get tree for expression operand:\n");
1358 ir->operands[operand]->accept(&v);
1359 exit(1);
1360 }
1361 op[operand] = this->result;
1362
1363 /* Matrix expression operands should have been broken down to vector
1364 * operations already.
1365 */
1366 assert(!ir->operands[operand]->type->is_matrix());
1367 }
1368
1369 int vector_elements = ir->operands[0]->type->vector_elements;
1370 if (ir->operands[1]) {
1371 vector_elements = MAX2(vector_elements,
1372 ir->operands[1]->type->vector_elements);
1373 }
1374
1375 this->result.file = PROGRAM_UNDEFINED;
1376
1377 /* Storage for our result. Ideally for an assignment we'd be using
1378 * the actual storage for the result here, instead.
1379 */
1380 result_src = get_temp(ir->type);
1381 /* convenience for the emit functions below. */
1382 result_dst = st_dst_reg(result_src);
1383 /* Limit writes to the channels that will be used by result_src later.
1384 * This does limit this temp's use as a temporary for multi-instruction
1385 * sequences.
1386 */
1387 result_dst.writemask = (1 << ir->type->vector_elements) - 1;
1388
1389 switch (ir->operation) {
1390 case ir_unop_logic_not:
1391 if (result_dst.type != GLSL_TYPE_FLOAT)
1392 emit(ir, TGSI_OPCODE_SEQ, result_dst, op[0], st_src_reg_for_type(result_dst.type, 0));
1393 else {
1394 /* Previously 'SEQ dst, src, 0.0' was used for this. However, many
1395 * older GPUs implement SEQ using multiple instructions (i915 uses two
1396 * SGE instructions and a MUL instruction). Since our logic values are
1397 * 0.0 and 1.0, 1-x also implements !x.
1398 */
1399 op[0].negate = ~op[0].negate;
1400 emit(ir, TGSI_OPCODE_ADD, result_dst, op[0], st_src_reg_for_float(1.0));
1401 }
1402 break;
1403 case ir_unop_neg:
1404 assert(result_dst.type == GLSL_TYPE_FLOAT || result_dst.type == GLSL_TYPE_INT);
1405 if (result_dst.type == GLSL_TYPE_INT)
1406 emit(ir, TGSI_OPCODE_INEG, result_dst, op[0]);
1407 else {
1408 op[0].negate = ~op[0].negate;
1409 result_src = op[0];
1410 }
1411 break;
1412 case ir_unop_abs:
1413 assert(result_dst.type == GLSL_TYPE_FLOAT);
1414 emit(ir, TGSI_OPCODE_ABS, result_dst, op[0]);
1415 break;
1416 case ir_unop_sign:
1417 emit(ir, TGSI_OPCODE_SSG, result_dst, op[0]);
1418 break;
1419 case ir_unop_rcp:
1420 emit_scalar(ir, TGSI_OPCODE_RCP, result_dst, op[0]);
1421 break;
1422
1423 case ir_unop_exp2:
1424 emit_scalar(ir, TGSI_OPCODE_EX2, result_dst, op[0]);
1425 break;
1426 case ir_unop_exp:
1427 case ir_unop_log:
1428 assert(!"not reached: should be handled by ir_explog_to_explog2");
1429 break;
1430 case ir_unop_log2:
1431 emit_scalar(ir, TGSI_OPCODE_LG2, result_dst, op[0]);
1432 break;
1433 case ir_unop_sin:
1434 emit_scalar(ir, TGSI_OPCODE_SIN, result_dst, op[0]);
1435 break;
1436 case ir_unop_cos:
1437 emit_scalar(ir, TGSI_OPCODE_COS, result_dst, op[0]);
1438 break;
1439 case ir_unop_sin_reduced:
1440 emit_scs(ir, TGSI_OPCODE_SIN, result_dst, op[0]);
1441 break;
1442 case ir_unop_cos_reduced:
1443 emit_scs(ir, TGSI_OPCODE_COS, result_dst, op[0]);
1444 break;
1445
1446 case ir_unop_dFdx:
1447 emit(ir, TGSI_OPCODE_DDX, result_dst, op[0]);
1448 break;
1449 case ir_unop_dFdy:
1450 op[0].negate = ~op[0].negate;
1451 emit(ir, TGSI_OPCODE_DDY, result_dst, op[0]);
1452 break;
1453
1454 case ir_unop_noise: {
1455 /* At some point, a motivated person could add a better
1456 * implementation of noise. Currently not even the nvidia
1457 * binary drivers do anything more than this. In any case, the
1458 * place to do this is in the GL state tracker, not the poor
1459 * driver.
1460 */
1461 emit(ir, TGSI_OPCODE_MOV, result_dst, st_src_reg_for_float(0.5));
1462 break;
1463 }
1464
1465 case ir_binop_add:
1466 emit(ir, TGSI_OPCODE_ADD, result_dst, op[0], op[1]);
1467 break;
1468 case ir_binop_sub:
1469 emit(ir, TGSI_OPCODE_SUB, result_dst, op[0], op[1]);
1470 break;
1471
1472 case ir_binop_mul:
1473 emit(ir, TGSI_OPCODE_MUL, result_dst, op[0], op[1]);
1474 break;
1475 case ir_binop_div:
1476 if (result_dst.type == GLSL_TYPE_FLOAT)
1477 assert(!"not reached: should be handled by ir_div_to_mul_rcp");
1478 else
1479 emit(ir, TGSI_OPCODE_DIV, result_dst, op[0], op[1]);
1480 break;
1481 case ir_binop_mod:
1482 if (result_dst.type == GLSL_TYPE_FLOAT)
1483 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
1484 else
1485 emit(ir, TGSI_OPCODE_MOD, result_dst, op[0], op[1]);
1486 break;
1487
1488 case ir_binop_less:
1489 emit(ir, TGSI_OPCODE_SLT, result_dst, op[0], op[1]);
1490 break;
1491 case ir_binop_greater:
1492 emit(ir, TGSI_OPCODE_SGT, result_dst, op[0], op[1]);
1493 break;
1494 case ir_binop_lequal:
1495 emit(ir, TGSI_OPCODE_SLE, result_dst, op[0], op[1]);
1496 break;
1497 case ir_binop_gequal:
1498 emit(ir, TGSI_OPCODE_SGE, result_dst, op[0], op[1]);
1499 break;
1500 case ir_binop_equal:
1501 emit(ir, TGSI_OPCODE_SEQ, result_dst, op[0], op[1]);
1502 break;
1503 case ir_binop_nequal:
1504 emit(ir, TGSI_OPCODE_SNE, result_dst, op[0], op[1]);
1505 break;
1506 case ir_binop_all_equal:
1507 /* "==" operator producing a scalar boolean. */
1508 if (ir->operands[0]->type->is_vector() ||
1509 ir->operands[1]->type->is_vector()) {
1510 st_src_reg temp = get_temp(native_integers ?
1511 glsl_type::get_instance(ir->operands[0]->type->base_type, 4, 1) :
1512 glsl_type::vec4_type);
1513 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
1514 emit(ir, TGSI_OPCODE_SNE, st_dst_reg(temp), op[0], op[1]);
1515
1516 /* After the dot-product, the value will be an integer on the
1517 * range [0,4]. Zero becomes 1.0, and positive values become zero.
1518 */
1519 emit_dp(ir, result_dst, temp, temp, vector_elements);
1520
1521 if (result_dst.type == GLSL_TYPE_FLOAT) {
1522 /* Negating the result of the dot-product gives values on the range
1523 * [-4, 0]. Zero becomes 1.0, and negative values become zero.
1524 * This is achieved using SGE.
1525 */
1526 st_src_reg sge_src = result_src;
1527 sge_src.negate = ~sge_src.negate;
1528 emit(ir, TGSI_OPCODE_SGE, result_dst, sge_src, st_src_reg_for_float(0.0));
1529 } else {
1530 /* The TGSI negate flag doesn't work for integers, so use SEQ 0
1531 * instead.
1532 */
1533 emit(ir, TGSI_OPCODE_SEQ, result_dst, result_src, st_src_reg_for_int(0));
1534 }
1535 } else {
1536 emit(ir, TGSI_OPCODE_SEQ, result_dst, op[0], op[1]);
1537 }
1538 break;
1539 case ir_binop_any_nequal:
1540 /* "!=" operator producing a scalar boolean. */
1541 if (ir->operands[0]->type->is_vector() ||
1542 ir->operands[1]->type->is_vector()) {
1543 st_src_reg temp = get_temp(native_integers ?
1544 glsl_type::get_instance(ir->operands[0]->type->base_type, 4, 1) :
1545 glsl_type::vec4_type);
1546 assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
1547 emit(ir, TGSI_OPCODE_SNE, st_dst_reg(temp), op[0], op[1]);
1548
1549 /* After the dot-product, the value will be an integer on the
1550 * range [0,4]. Zero stays zero, and positive values become 1.0.
1551 */
1552 glsl_to_tgsi_instruction *const dp =
1553 emit_dp(ir, result_dst, temp, temp, vector_elements);
1554 if (this->prog->Target == GL_FRAGMENT_PROGRAM_ARB &&
1555 result_dst.type == GLSL_TYPE_FLOAT) {
1556 /* The clamping to [0,1] can be done for free in the fragment
1557 * shader with a saturate.
1558 */
1559 dp->saturate = true;
1560 } else if (result_dst.type == GLSL_TYPE_FLOAT) {
1561 /* Negating the result of the dot-product gives values on the range
1562 * [-4, 0]. Zero stays zero, and negative values become 1.0. This
1563 * achieved using SLT.
1564 */
1565 st_src_reg slt_src = result_src;
1566 slt_src.negate = ~slt_src.negate;
1567 emit(ir, TGSI_OPCODE_SLT, result_dst, slt_src, st_src_reg_for_float(0.0));
1568 } else {
1569 emit(ir, TGSI_OPCODE_SNE, result_dst, result_src, st_src_reg_for_int(0));
1570 }
1571 } else {
1572 emit(ir, TGSI_OPCODE_SNE, result_dst, op[0], op[1]);
1573 }
1574 break;
1575
1576 case ir_unop_any: {
1577 assert(ir->operands[0]->type->is_vector());
1578
1579 /* After the dot-product, the value will be an integer on the
1580 * range [0,4]. Zero stays zero, and positive values become 1.0.
1581 */
1582 glsl_to_tgsi_instruction *const dp =
1583 emit_dp(ir, result_dst, op[0], op[0],
1584 ir->operands[0]->type->vector_elements);
1585 if (this->prog->Target == GL_FRAGMENT_PROGRAM_ARB &&
1586 result_dst.type == GLSL_TYPE_FLOAT) {
1587 /* The clamping to [0,1] can be done for free in the fragment
1588 * shader with a saturate.
1589 */
1590 dp->saturate = true;
1591 } else if (result_dst.type == GLSL_TYPE_FLOAT) {
1592 /* Negating the result of the dot-product gives values on the range
1593 * [-4, 0]. Zero stays zero, and negative values become 1.0. This
1594 * is achieved using SLT.
1595 */
1596 st_src_reg slt_src = result_src;
1597 slt_src.negate = ~slt_src.negate;
1598 emit(ir, TGSI_OPCODE_SLT, result_dst, slt_src, st_src_reg_for_float(0.0));
1599 }
1600 else {
1601 /* Use SNE 0 if integers are being used as boolean values. */
1602 emit(ir, TGSI_OPCODE_SNE, result_dst, result_src, st_src_reg_for_int(0));
1603 }
1604 break;
1605 }
1606
1607 case ir_binop_logic_xor:
1608 emit(ir, TGSI_OPCODE_SNE, result_dst, op[0], op[1]);
1609 break;
1610
1611 case ir_binop_logic_or: {
1612 /* After the addition, the value will be an integer on the
1613 * range [0,2]. Zero stays zero, and positive values become 1.0.
1614 */
1615 glsl_to_tgsi_instruction *add =
1616 emit(ir, TGSI_OPCODE_ADD, result_dst, op[0], op[1]);
1617 if (this->prog->Target == GL_FRAGMENT_PROGRAM_ARB &&
1618 result_dst.type == GLSL_TYPE_FLOAT) {
1619 /* The clamping to [0,1] can be done for free in the fragment
1620 * shader with a saturate if floats are being used as boolean values.
1621 */
1622 add->saturate = true;
1623 } else if (result_dst.type == GLSL_TYPE_FLOAT) {
1624 /* Negating the result of the addition gives values on the range
1625 * [-2, 0]. Zero stays zero, and negative values become 1.0. This
1626 * is achieved using SLT.
1627 */
1628 st_src_reg slt_src = result_src;
1629 slt_src.negate = ~slt_src.negate;
1630 emit(ir, TGSI_OPCODE_SLT, result_dst, slt_src, st_src_reg_for_float(0.0));
1631 } else {
1632 /* Use an SNE on the result of the addition. Zero stays zero,
1633 * 1 stays 1, and 2 becomes 1.
1634 */
1635 emit(ir, TGSI_OPCODE_SNE, result_dst, result_src, st_src_reg_for_int(0));
1636 }
1637 break;
1638 }
1639
1640 case ir_binop_logic_and:
1641 /* the bool args are stored as float 0.0 or 1.0, so "mul" gives us "and". */
1642 emit(ir, TGSI_OPCODE_MUL, result_dst, op[0], op[1]);
1643 break;
1644
1645 case ir_binop_dot:
1646 assert(ir->operands[0]->type->is_vector());
1647 assert(ir->operands[0]->type == ir->operands[1]->type);
1648 emit_dp(ir, result_dst, op[0], op[1],
1649 ir->operands[0]->type->vector_elements);
1650 break;
1651
1652 case ir_unop_sqrt:
1653 /* sqrt(x) = x * rsq(x). */
1654 emit_scalar(ir, TGSI_OPCODE_RSQ, result_dst, op[0]);
1655 emit(ir, TGSI_OPCODE_MUL, result_dst, result_src, op[0]);
1656 /* For incoming channels <= 0, set the result to 0. */
1657 op[0].negate = ~op[0].negate;
1658 emit(ir, TGSI_OPCODE_CMP, result_dst,
1659 op[0], result_src, st_src_reg_for_float(0.0));
1660 break;
1661 case ir_unop_rsq:
1662 emit_scalar(ir, TGSI_OPCODE_RSQ, result_dst, op[0]);
1663 break;
1664 case ir_unop_i2f:
1665 case ir_unop_b2f:
1666 if (native_integers) {
1667 emit(ir, TGSI_OPCODE_I2F, result_dst, op[0]);
1668 break;
1669 }
1670 case ir_unop_i2u:
1671 case ir_unop_u2i:
1672 /* Converting between signed and unsigned integers is a no-op. */
1673 case ir_unop_b2i:
1674 /* Booleans are stored as integers (or floats in GLSL 1.20 and lower). */
1675 result_src = op[0];
1676 break;
1677 case ir_unop_f2i:
1678 if (native_integers)
1679 emit(ir, TGSI_OPCODE_F2I, result_dst, op[0]);
1680 else
1681 emit(ir, TGSI_OPCODE_TRUNC, result_dst, op[0]);
1682 break;
1683 case ir_unop_f2b:
1684 case ir_unop_i2b:
1685 emit(ir, TGSI_OPCODE_SNE, result_dst, op[0],
1686 st_src_reg_for_type(result_dst.type, 0));
1687 break;
1688 case ir_unop_trunc:
1689 emit(ir, TGSI_OPCODE_TRUNC, result_dst, op[0]);
1690 break;
1691 case ir_unop_ceil:
1692 op[0].negate = ~op[0].negate;
1693 emit(ir, TGSI_OPCODE_FLR, result_dst, op[0]);
1694 result_src.negate = ~result_src.negate;
1695 break;
1696 case ir_unop_floor:
1697 emit(ir, TGSI_OPCODE_FLR, result_dst, op[0]);
1698 break;
1699 case ir_unop_fract:
1700 emit(ir, TGSI_OPCODE_FRC, result_dst, op[0]);
1701 break;
1702
1703 case ir_binop_min:
1704 emit(ir, TGSI_OPCODE_MIN, result_dst, op[0], op[1]);
1705 break;
1706 case ir_binop_max:
1707 emit(ir, TGSI_OPCODE_MAX, result_dst, op[0], op[1]);
1708 break;
1709 case ir_binop_pow:
1710 emit_scalar(ir, TGSI_OPCODE_POW, result_dst, op[0], op[1]);
1711 break;
1712
1713 case ir_unop_bit_not:
1714 if (glsl_version >= 130) {
1715 emit(ir, TGSI_OPCODE_NOT, result_dst, op[0]);
1716 break;
1717 }
1718 case ir_unop_u2f:
1719 if (native_integers) {
1720 emit(ir, TGSI_OPCODE_U2F, result_dst, op[0]);
1721 break;
1722 }
1723 case ir_binop_lshift:
1724 if (glsl_version >= 130) {
1725 emit(ir, TGSI_OPCODE_SHL, result_dst, op[0]);
1726 break;
1727 }
1728 case ir_binop_rshift:
1729 if (glsl_version >= 130) {
1730 emit(ir, TGSI_OPCODE_ISHR, result_dst, op[0]);
1731 break;
1732 }
1733 case ir_binop_bit_and:
1734 if (glsl_version >= 130) {
1735 emit(ir, TGSI_OPCODE_AND, result_dst, op[0]);
1736 break;
1737 }
1738 case ir_binop_bit_xor:
1739 if (glsl_version >= 130) {
1740 emit(ir, TGSI_OPCODE_XOR, result_dst, op[0]);
1741 break;
1742 }
1743 case ir_binop_bit_or:
1744 if (glsl_version >= 130) {
1745 emit(ir, TGSI_OPCODE_OR, result_dst, op[0]);
1746 break;
1747 }
1748 case ir_unop_round_even:
1749 assert(!"GLSL 1.30 features unsupported");
1750 break;
1751
1752 case ir_quadop_vector:
1753 /* This operation should have already been handled.
1754 */
1755 assert(!"Should not get here.");
1756 break;
1757 }
1758
1759 this->result = result_src;
1760 }
1761
1762
1763 void
1764 glsl_to_tgsi_visitor::visit(ir_swizzle *ir)
1765 {
1766 st_src_reg src;
1767 int i;
1768 int swizzle[4];
1769
1770 /* Note that this is only swizzles in expressions, not those on the left
1771 * hand side of an assignment, which do write masking. See ir_assignment
1772 * for that.
1773 */
1774
1775 ir->val->accept(this);
1776 src = this->result;
1777 assert(src.file != PROGRAM_UNDEFINED);
1778
1779 for (i = 0; i < 4; i++) {
1780 if (i < ir->type->vector_elements) {
1781 switch (i) {
1782 case 0:
1783 swizzle[i] = GET_SWZ(src.swizzle, ir->mask.x);
1784 break;
1785 case 1:
1786 swizzle[i] = GET_SWZ(src.swizzle, ir->mask.y);
1787 break;
1788 case 2:
1789 swizzle[i] = GET_SWZ(src.swizzle, ir->mask.z);
1790 break;
1791 case 3:
1792 swizzle[i] = GET_SWZ(src.swizzle, ir->mask.w);
1793 break;
1794 }
1795 } else {
1796 /* If the type is smaller than a vec4, replicate the last
1797 * channel out.
1798 */
1799 swizzle[i] = swizzle[ir->type->vector_elements - 1];
1800 }
1801 }
1802
1803 src.swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
1804
1805 this->result = src;
1806 }
1807
1808 void
1809 glsl_to_tgsi_visitor::visit(ir_dereference_variable *ir)
1810 {
1811 variable_storage *entry = find_variable_storage(ir->var);
1812 ir_variable *var = ir->var;
1813
1814 if (!entry) {
1815 switch (var->mode) {
1816 case ir_var_uniform:
1817 entry = new(mem_ctx) variable_storage(var, PROGRAM_UNIFORM,
1818 var->location);
1819 this->variables.push_tail(entry);
1820 break;
1821 case ir_var_in:
1822 case ir_var_inout:
1823 /* The linker assigns locations for varyings and attributes,
1824 * including deprecated builtins (like gl_Color), user-assign
1825 * generic attributes (glBindVertexLocation), and
1826 * user-defined varyings.
1827 *
1828 * FINISHME: We would hit this path for function arguments. Fix!
1829 */
1830 assert(var->location != -1);
1831 entry = new(mem_ctx) variable_storage(var,
1832 PROGRAM_INPUT,
1833 var->location);
1834 if (this->prog->Target == GL_VERTEX_PROGRAM_ARB &&
1835 var->location >= VERT_ATTRIB_GENERIC0) {
1836 _mesa_add_attribute(this->prog->Attributes,
1837 var->name,
1838 _mesa_sizeof_glsl_type(var->type->gl_type),
1839 var->type->gl_type,
1840 var->location - VERT_ATTRIB_GENERIC0);
1841 }
1842 break;
1843 case ir_var_out:
1844 assert(var->location != -1);
1845 entry = new(mem_ctx) variable_storage(var,
1846 PROGRAM_OUTPUT,
1847 var->location);
1848 break;
1849 case ir_var_system_value:
1850 entry = new(mem_ctx) variable_storage(var,
1851 PROGRAM_SYSTEM_VALUE,
1852 var->location);
1853 break;
1854 case ir_var_auto:
1855 case ir_var_temporary:
1856 entry = new(mem_ctx) variable_storage(var, PROGRAM_TEMPORARY,
1857 this->next_temp);
1858 this->variables.push_tail(entry);
1859
1860 next_temp += type_size(var->type);
1861 break;
1862 }
1863
1864 if (!entry) {
1865 printf("Failed to make storage for %s\n", var->name);
1866 exit(1);
1867 }
1868 }
1869
1870 this->result = st_src_reg(entry->file, entry->index, var->type);
1871 if (!native_integers)
1872 this->result.type = GLSL_TYPE_FLOAT;
1873 }
1874
1875 void
1876 glsl_to_tgsi_visitor::visit(ir_dereference_array *ir)
1877 {
1878 ir_constant *index;
1879 st_src_reg src;
1880 int element_size = type_size(ir->type);
1881
1882 index = ir->array_index->constant_expression_value();
1883
1884 ir->array->accept(this);
1885 src = this->result;
1886
1887 if (index) {
1888 src.index += index->value.i[0] * element_size;
1889 } else {
1890 /* Variable index array dereference. It eats the "vec4" of the
1891 * base of the array and an index that offsets the TGSI register
1892 * index.
1893 */
1894 ir->array_index->accept(this);
1895
1896 st_src_reg index_reg;
1897
1898 if (element_size == 1) {
1899 index_reg = this->result;
1900 } else {
1901 index_reg = get_temp(glsl_type::float_type);
1902
1903 emit(ir, TGSI_OPCODE_MUL, st_dst_reg(index_reg),
1904 this->result, st_src_reg_for_float(element_size));
1905 }
1906
1907 /* If there was already a relative address register involved, add the
1908 * new and the old together to get the new offset.
1909 */
1910 if (src.reladdr != NULL) {
1911 st_src_reg accum_reg = get_temp(glsl_type::float_type);
1912
1913 emit(ir, TGSI_OPCODE_ADD, st_dst_reg(accum_reg),
1914 index_reg, *src.reladdr);
1915
1916 index_reg = accum_reg;
1917 }
1918
1919 src.reladdr = ralloc(mem_ctx, st_src_reg);
1920 memcpy(src.reladdr, &index_reg, sizeof(index_reg));
1921 }
1922
1923 /* If the type is smaller than a vec4, replicate the last channel out. */
1924 if (ir->type->is_scalar() || ir->type->is_vector())
1925 src.swizzle = swizzle_for_size(ir->type->vector_elements);
1926 else
1927 src.swizzle = SWIZZLE_NOOP;
1928
1929 this->result = src;
1930 }
1931
1932 void
1933 glsl_to_tgsi_visitor::visit(ir_dereference_record *ir)
1934 {
1935 unsigned int i;
1936 const glsl_type *struct_type = ir->record->type;
1937 int offset = 0;
1938
1939 ir->record->accept(this);
1940
1941 for (i = 0; i < struct_type->length; i++) {
1942 if (strcmp(struct_type->fields.structure[i].name, ir->field) == 0)
1943 break;
1944 offset += type_size(struct_type->fields.structure[i].type);
1945 }
1946
1947 /* If the type is smaller than a vec4, replicate the last channel out. */
1948 if (ir->type->is_scalar() || ir->type->is_vector())
1949 this->result.swizzle = swizzle_for_size(ir->type->vector_elements);
1950 else
1951 this->result.swizzle = SWIZZLE_NOOP;
1952
1953 this->result.index += offset;
1954 }
1955
1956 /**
1957 * We want to be careful in assignment setup to hit the actual storage
1958 * instead of potentially using a temporary like we might with the
1959 * ir_dereference handler.
1960 */
1961 static st_dst_reg
1962 get_assignment_lhs(ir_dereference *ir, glsl_to_tgsi_visitor *v)
1963 {
1964 /* The LHS must be a dereference. If the LHS is a variable indexed array
1965 * access of a vector, it must be separated into a series conditional moves
1966 * before reaching this point (see ir_vec_index_to_cond_assign).
1967 */
1968 assert(ir->as_dereference());
1969 ir_dereference_array *deref_array = ir->as_dereference_array();
1970 if (deref_array) {
1971 assert(!deref_array->array->type->is_vector());
1972 }
1973
1974 /* Use the rvalue deref handler for the most part. We'll ignore
1975 * swizzles in it and write swizzles using writemask, though.
1976 */
1977 ir->accept(v);
1978 return st_dst_reg(v->result);
1979 }
1980
1981 /**
1982 * Process the condition of a conditional assignment
1983 *
1984 * Examines the condition of a conditional assignment to generate the optimal
1985 * first operand of a \c CMP instruction. If the condition is a relational
1986 * operator with 0 (e.g., \c ir_binop_less), the value being compared will be
1987 * used as the source for the \c CMP instruction. Otherwise the comparison
1988 * is processed to a boolean result, and the boolean result is used as the
1989 * operand to the CMP instruction.
1990 */
1991 bool
1992 glsl_to_tgsi_visitor::process_move_condition(ir_rvalue *ir)
1993 {
1994 ir_rvalue *src_ir = ir;
1995 bool negate = true;
1996 bool switch_order = false;
1997
1998 ir_expression *const expr = ir->as_expression();
1999 if ((expr != NULL) && (expr->get_num_operands() == 2)) {
2000 bool zero_on_left = false;
2001
2002 if (expr->operands[0]->is_zero()) {
2003 src_ir = expr->operands[1];
2004 zero_on_left = true;
2005 } else if (expr->operands[1]->is_zero()) {
2006 src_ir = expr->operands[0];
2007 zero_on_left = false;
2008 }
2009
2010 /* a is - 0 + - 0 +
2011 * (a < 0) T F F ( a < 0) T F F
2012 * (0 < a) F F T (-a < 0) F F T
2013 * (a <= 0) T T F (-a < 0) F F T (swap order of other operands)
2014 * (0 <= a) F T T ( a < 0) T F F (swap order of other operands)
2015 * (a > 0) F F T (-a < 0) F F T
2016 * (0 > a) T F F ( a < 0) T F F
2017 * (a >= 0) F T T ( a < 0) T F F (swap order of other operands)
2018 * (0 >= a) T T F (-a < 0) F F T (swap order of other operands)
2019 *
2020 * Note that exchanging the order of 0 and 'a' in the comparison simply
2021 * means that the value of 'a' should be negated.
2022 */
2023 if (src_ir != ir) {
2024 switch (expr->operation) {
2025 case ir_binop_less:
2026 switch_order = false;
2027 negate = zero_on_left;
2028 break;
2029
2030 case ir_binop_greater:
2031 switch_order = false;
2032 negate = !zero_on_left;
2033 break;
2034
2035 case ir_binop_lequal:
2036 switch_order = true;
2037 negate = !zero_on_left;
2038 break;
2039
2040 case ir_binop_gequal:
2041 switch_order = true;
2042 negate = zero_on_left;
2043 break;
2044
2045 default:
2046 /* This isn't the right kind of comparison afterall, so make sure
2047 * the whole condition is visited.
2048 */
2049 src_ir = ir;
2050 break;
2051 }
2052 }
2053 }
2054
2055 src_ir->accept(this);
2056
2057 /* We use the TGSI_OPCODE_CMP (a < 0 ? b : c) for conditional moves, and the
2058 * condition we produced is 0.0 or 1.0. By flipping the sign, we can
2059 * choose which value TGSI_OPCODE_CMP produces without an extra instruction
2060 * computing the condition.
2061 */
2062 if (negate)
2063 this->result.negate = ~this->result.negate;
2064
2065 return switch_order;
2066 }
2067
2068 void
2069 glsl_to_tgsi_visitor::visit(ir_assignment *ir)
2070 {
2071 st_dst_reg l;
2072 st_src_reg r;
2073 int i;
2074
2075 ir->rhs->accept(this);
2076 r = this->result;
2077
2078 l = get_assignment_lhs(ir->lhs, this);
2079
2080 /* FINISHME: This should really set to the correct maximal writemask for each
2081 * FINISHME: component written (in the loops below). This case can only
2082 * FINISHME: occur for matrices, arrays, and structures.
2083 */
2084 if (ir->write_mask == 0) {
2085 assert(!ir->lhs->type->is_scalar() && !ir->lhs->type->is_vector());
2086 l.writemask = WRITEMASK_XYZW;
2087 } else if (ir->lhs->type->is_scalar() &&
2088 ir->lhs->variable_referenced()->mode == ir_var_out) {
2089 /* FINISHME: This hack makes writing to gl_FragDepth, which lives in the
2090 * FINISHME: W component of fragment shader output zero, work correctly.
2091 */
2092 l.writemask = WRITEMASK_XYZW;
2093 } else {
2094 int swizzles[4];
2095 int first_enabled_chan = 0;
2096 int rhs_chan = 0;
2097
2098 l.writemask = ir->write_mask;
2099
2100 for (int i = 0; i < 4; i++) {
2101 if (l.writemask & (1 << i)) {
2102 first_enabled_chan = GET_SWZ(r.swizzle, i);
2103 break;
2104 }
2105 }
2106
2107 /* Swizzle a small RHS vector into the channels being written.
2108 *
2109 * glsl ir treats write_mask as dictating how many channels are
2110 * present on the RHS while TGSI treats write_mask as just
2111 * showing which channels of the vec4 RHS get written.
2112 */
2113 for (int i = 0; i < 4; i++) {
2114 if (l.writemask & (1 << i))
2115 swizzles[i] = GET_SWZ(r.swizzle, rhs_chan++);
2116 else
2117 swizzles[i] = first_enabled_chan;
2118 }
2119 r.swizzle = MAKE_SWIZZLE4(swizzles[0], swizzles[1],
2120 swizzles[2], swizzles[3]);
2121 }
2122
2123 assert(l.file != PROGRAM_UNDEFINED);
2124 assert(r.file != PROGRAM_UNDEFINED);
2125
2126 if (ir->condition) {
2127 const bool switch_order = this->process_move_condition(ir->condition);
2128 st_src_reg condition = this->result;
2129
2130 for (i = 0; i < type_size(ir->lhs->type); i++) {
2131 st_src_reg l_src = st_src_reg(l);
2132 l_src.swizzle = swizzle_for_size(ir->lhs->type->vector_elements);
2133
2134 if (switch_order) {
2135 emit(ir, TGSI_OPCODE_CMP, l, condition, l_src, r);
2136 } else {
2137 emit(ir, TGSI_OPCODE_CMP, l, condition, r, l_src);
2138 }
2139
2140 l.index++;
2141 r.index++;
2142 }
2143 } else if (ir->rhs->as_expression() &&
2144 this->instructions.get_tail() &&
2145 ir->rhs == ((glsl_to_tgsi_instruction *)this->instructions.get_tail())->ir &&
2146 type_size(ir->lhs->type) == 1 &&
2147 l.writemask == ((glsl_to_tgsi_instruction *)this->instructions.get_tail())->dst.writemask) {
2148 /* To avoid emitting an extra MOV when assigning an expression to a
2149 * variable, emit the last instruction of the expression again, but
2150 * replace the destination register with the target of the assignment.
2151 * Dead code elimination will remove the original instruction.
2152 */
2153 glsl_to_tgsi_instruction *inst, *new_inst;
2154 inst = (glsl_to_tgsi_instruction *)this->instructions.get_tail();
2155 new_inst = emit(ir, inst->op, l, inst->src[0], inst->src[1], inst->src[2]);
2156 new_inst->saturate = inst->saturate;
2157 } else {
2158 for (i = 0; i < type_size(ir->lhs->type); i++) {
2159 emit(ir, TGSI_OPCODE_MOV, l, r);
2160 l.index++;
2161 r.index++;
2162 }
2163 }
2164 }
2165
2166
2167 void
2168 glsl_to_tgsi_visitor::visit(ir_constant *ir)
2169 {
2170 st_src_reg src;
2171 GLfloat stack_vals[4] = { 0 };
2172 gl_constant_value *values = (gl_constant_value *) stack_vals;
2173 GLenum gl_type = GL_NONE;
2174 unsigned int i;
2175 static int in_array = 0;
2176 gl_register_file file = in_array ? PROGRAM_CONSTANT : PROGRAM_IMMEDIATE;
2177
2178 /* Unfortunately, 4 floats is all we can get into
2179 * _mesa_add_typed_unnamed_constant. So, make a temp to store an
2180 * aggregate constant and move each constant value into it. If we
2181 * get lucky, copy propagation will eliminate the extra moves.
2182 */
2183 if (ir->type->base_type == GLSL_TYPE_STRUCT) {
2184 st_src_reg temp_base = get_temp(ir->type);
2185 st_dst_reg temp = st_dst_reg(temp_base);
2186
2187 foreach_iter(exec_list_iterator, iter, ir->components) {
2188 ir_constant *field_value = (ir_constant *)iter.get();
2189 int size = type_size(field_value->type);
2190
2191 assert(size > 0);
2192
2193 field_value->accept(this);
2194 src = this->result;
2195
2196 for (i = 0; i < (unsigned int)size; i++) {
2197 emit(ir, TGSI_OPCODE_MOV, temp, src);
2198
2199 src.index++;
2200 temp.index++;
2201 }
2202 }
2203 this->result = temp_base;
2204 return;
2205 }
2206
2207 if (ir->type->is_array()) {
2208 st_src_reg temp_base = get_temp(ir->type);
2209 st_dst_reg temp = st_dst_reg(temp_base);
2210 int size = type_size(ir->type->fields.array);
2211
2212 assert(size > 0);
2213 in_array++;
2214
2215 for (i = 0; i < ir->type->length; i++) {
2216 ir->array_elements[i]->accept(this);
2217 src = this->result;
2218 for (int j = 0; j < size; j++) {
2219 emit(ir, TGSI_OPCODE_MOV, temp, src);
2220
2221 src.index++;
2222 temp.index++;
2223 }
2224 }
2225 this->result = temp_base;
2226 in_array--;
2227 return;
2228 }
2229
2230 if (ir->type->is_matrix()) {
2231 st_src_reg mat = get_temp(ir->type);
2232 st_dst_reg mat_column = st_dst_reg(mat);
2233
2234 for (i = 0; i < ir->type->matrix_columns; i++) {
2235 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
2236 values = (gl_constant_value *) &ir->value.f[i * ir->type->vector_elements];
2237
2238 src = st_src_reg(file, -1, ir->type->base_type);
2239 src.index = add_constant(file,
2240 values,
2241 ir->type->vector_elements,
2242 GL_FLOAT,
2243 &src.swizzle);
2244 emit(ir, TGSI_OPCODE_MOV, mat_column, src);
2245
2246 mat_column.index++;
2247 }
2248
2249 this->result = mat;
2250 return;
2251 }
2252
2253 switch (ir->type->base_type) {
2254 case GLSL_TYPE_FLOAT:
2255 gl_type = GL_FLOAT;
2256 for (i = 0; i < ir->type->vector_elements; i++) {
2257 values[i].f = ir->value.f[i];
2258 }
2259 break;
2260 case GLSL_TYPE_UINT:
2261 gl_type = native_integers ? GL_UNSIGNED_INT : GL_FLOAT;
2262 for (i = 0; i < ir->type->vector_elements; i++) {
2263 if (native_integers)
2264 values[i].u = ir->value.u[i];
2265 else
2266 values[i].f = ir->value.u[i];
2267 }
2268 break;
2269 case GLSL_TYPE_INT:
2270 gl_type = native_integers ? GL_INT : GL_FLOAT;
2271 for (i = 0; i < ir->type->vector_elements; i++) {
2272 if (native_integers)
2273 values[i].i = ir->value.i[i];
2274 else
2275 values[i].f = ir->value.i[i];
2276 }
2277 break;
2278 case GLSL_TYPE_BOOL:
2279 gl_type = native_integers ? GL_BOOL : GL_FLOAT;
2280 for (i = 0; i < ir->type->vector_elements; i++) {
2281 if (native_integers)
2282 values[i].b = ir->value.b[i];
2283 else
2284 values[i].f = ir->value.b[i];
2285 }
2286 break;
2287 default:
2288 assert(!"Non-float/uint/int/bool constant");
2289 }
2290
2291 this->result = st_src_reg(file, -1, ir->type);
2292 this->result.index = add_constant(file,
2293 values,
2294 ir->type->vector_elements,
2295 gl_type,
2296 &this->result.swizzle);
2297 }
2298
2299 function_entry *
2300 glsl_to_tgsi_visitor::get_function_signature(ir_function_signature *sig)
2301 {
2302 function_entry *entry;
2303
2304 foreach_iter(exec_list_iterator, iter, this->function_signatures) {
2305 entry = (function_entry *)iter.get();
2306
2307 if (entry->sig == sig)
2308 return entry;
2309 }
2310
2311 entry = ralloc(mem_ctx, function_entry);
2312 entry->sig = sig;
2313 entry->sig_id = this->next_signature_id++;
2314 entry->bgn_inst = NULL;
2315
2316 /* Allocate storage for all the parameters. */
2317 foreach_iter(exec_list_iterator, iter, sig->parameters) {
2318 ir_variable *param = (ir_variable *)iter.get();
2319 variable_storage *storage;
2320
2321 storage = find_variable_storage(param);
2322 assert(!storage);
2323
2324 storage = new(mem_ctx) variable_storage(param, PROGRAM_TEMPORARY,
2325 this->next_temp);
2326 this->variables.push_tail(storage);
2327
2328 this->next_temp += type_size(param->type);
2329 }
2330
2331 if (!sig->return_type->is_void()) {
2332 entry->return_reg = get_temp(sig->return_type);
2333 } else {
2334 entry->return_reg = undef_src;
2335 }
2336
2337 this->function_signatures.push_tail(entry);
2338 return entry;
2339 }
2340
2341 void
2342 glsl_to_tgsi_visitor::visit(ir_call *ir)
2343 {
2344 glsl_to_tgsi_instruction *call_inst;
2345 ir_function_signature *sig = ir->get_callee();
2346 function_entry *entry = get_function_signature(sig);
2347 int i;
2348
2349 /* Process in parameters. */
2350 exec_list_iterator sig_iter = sig->parameters.iterator();
2351 foreach_iter(exec_list_iterator, iter, *ir) {
2352 ir_rvalue *param_rval = (ir_rvalue *)iter.get();
2353 ir_variable *param = (ir_variable *)sig_iter.get();
2354
2355 if (param->mode == ir_var_in ||
2356 param->mode == ir_var_inout) {
2357 variable_storage *storage = find_variable_storage(param);
2358 assert(storage);
2359
2360 param_rval->accept(this);
2361 st_src_reg r = this->result;
2362
2363 st_dst_reg l;
2364 l.file = storage->file;
2365 l.index = storage->index;
2366 l.reladdr = NULL;
2367 l.writemask = WRITEMASK_XYZW;
2368 l.cond_mask = COND_TR;
2369
2370 for (i = 0; i < type_size(param->type); i++) {
2371 emit(ir, TGSI_OPCODE_MOV, l, r);
2372 l.index++;
2373 r.index++;
2374 }
2375 }
2376
2377 sig_iter.next();
2378 }
2379 assert(!sig_iter.has_next());
2380
2381 /* Emit call instruction */
2382 call_inst = emit(ir, TGSI_OPCODE_CAL);
2383 call_inst->function = entry;
2384
2385 /* Process out parameters. */
2386 sig_iter = sig->parameters.iterator();
2387 foreach_iter(exec_list_iterator, iter, *ir) {
2388 ir_rvalue *param_rval = (ir_rvalue *)iter.get();
2389 ir_variable *param = (ir_variable *)sig_iter.get();
2390
2391 if (param->mode == ir_var_out ||
2392 param->mode == ir_var_inout) {
2393 variable_storage *storage = find_variable_storage(param);
2394 assert(storage);
2395
2396 st_src_reg r;
2397 r.file = storage->file;
2398 r.index = storage->index;
2399 r.reladdr = NULL;
2400 r.swizzle = SWIZZLE_NOOP;
2401 r.negate = 0;
2402
2403 param_rval->accept(this);
2404 st_dst_reg l = st_dst_reg(this->result);
2405
2406 for (i = 0; i < type_size(param->type); i++) {
2407 emit(ir, TGSI_OPCODE_MOV, l, r);
2408 l.index++;
2409 r.index++;
2410 }
2411 }
2412
2413 sig_iter.next();
2414 }
2415 assert(!sig_iter.has_next());
2416
2417 /* Process return value. */
2418 this->result = entry->return_reg;
2419 }
2420
2421 void
2422 glsl_to_tgsi_visitor::visit(ir_texture *ir)
2423 {
2424 st_src_reg result_src, coord, lod_info, projector, dx, dy;
2425 st_dst_reg result_dst, coord_dst;
2426 glsl_to_tgsi_instruction *inst = NULL;
2427 unsigned opcode = TGSI_OPCODE_NOP;
2428
2429 if (ir->coordinate) {
2430 ir->coordinate->accept(this);
2431
2432 /* Put our coords in a temp. We'll need to modify them for shadow,
2433 * projection, or LOD, so the only case we'd use it as is is if
2434 * we're doing plain old texturing. The optimization passes on
2435 * glsl_to_tgsi_visitor should handle cleaning up our mess in that case.
2436 */
2437 coord = get_temp(glsl_type::vec4_type);
2438 coord_dst = st_dst_reg(coord);
2439 emit(ir, TGSI_OPCODE_MOV, coord_dst, this->result);
2440 }
2441
2442 if (ir->projector) {
2443 ir->projector->accept(this);
2444 projector = this->result;
2445 }
2446
2447 /* Storage for our result. Ideally for an assignment we'd be using
2448 * the actual storage for the result here, instead.
2449 */
2450 result_src = get_temp(glsl_type::vec4_type);
2451 result_dst = st_dst_reg(result_src);
2452
2453 switch (ir->op) {
2454 case ir_tex:
2455 opcode = TGSI_OPCODE_TEX;
2456 break;
2457 case ir_txb:
2458 opcode = TGSI_OPCODE_TXB;
2459 ir->lod_info.bias->accept(this);
2460 lod_info = this->result;
2461 break;
2462 case ir_txl:
2463 opcode = TGSI_OPCODE_TXL;
2464 ir->lod_info.lod->accept(this);
2465 lod_info = this->result;
2466 break;
2467 case ir_txd:
2468 opcode = TGSI_OPCODE_TXD;
2469 ir->lod_info.grad.dPdx->accept(this);
2470 dx = this->result;
2471 ir->lod_info.grad.dPdy->accept(this);
2472 dy = this->result;
2473 break;
2474 case ir_txs:
2475 opcode = TGSI_OPCODE_TXQ;
2476 ir->lod_info.lod->accept(this);
2477 lod_info = this->result;
2478 break;
2479 case ir_txf:
2480 opcode = TGSI_OPCODE_TXF;
2481 ir->lod_info.lod->accept(this);
2482 lod_info = this->result;
2483 break;
2484 }
2485
2486 if (ir->projector) {
2487 if (opcode == TGSI_OPCODE_TEX) {
2488 /* Slot the projector in as the last component of the coord. */
2489 coord_dst.writemask = WRITEMASK_W;
2490 emit(ir, TGSI_OPCODE_MOV, coord_dst, projector);
2491 coord_dst.writemask = WRITEMASK_XYZW;
2492 opcode = TGSI_OPCODE_TXP;
2493 } else {
2494 st_src_reg coord_w = coord;
2495 coord_w.swizzle = SWIZZLE_WWWW;
2496
2497 /* For the other TEX opcodes there's no projective version
2498 * since the last slot is taken up by LOD info. Do the
2499 * projective divide now.
2500 */
2501 coord_dst.writemask = WRITEMASK_W;
2502 emit(ir, TGSI_OPCODE_RCP, coord_dst, projector);
2503
2504 /* In the case where we have to project the coordinates "by hand,"
2505 * the shadow comparator value must also be projected.
2506 */
2507 st_src_reg tmp_src = coord;
2508 if (ir->shadow_comparitor) {
2509 /* Slot the shadow value in as the second to last component of the
2510 * coord.
2511 */
2512 ir->shadow_comparitor->accept(this);
2513
2514 tmp_src = get_temp(glsl_type::vec4_type);
2515 st_dst_reg tmp_dst = st_dst_reg(tmp_src);
2516
2517 tmp_dst.writemask = WRITEMASK_Z;
2518 emit(ir, TGSI_OPCODE_MOV, tmp_dst, this->result);
2519
2520 tmp_dst.writemask = WRITEMASK_XY;
2521 emit(ir, TGSI_OPCODE_MOV, tmp_dst, coord);
2522 }
2523
2524 coord_dst.writemask = WRITEMASK_XYZ;
2525 emit(ir, TGSI_OPCODE_MUL, coord_dst, tmp_src, coord_w);
2526
2527 coord_dst.writemask = WRITEMASK_XYZW;
2528 coord.swizzle = SWIZZLE_XYZW;
2529 }
2530 }
2531
2532 /* If projection is done and the opcode is not TGSI_OPCODE_TXP, then the shadow
2533 * comparator was put in the correct place (and projected) by the code,
2534 * above, that handles by-hand projection.
2535 */
2536 if (ir->shadow_comparitor && (!ir->projector || opcode == TGSI_OPCODE_TXP)) {
2537 /* Slot the shadow value in as the second to last component of the
2538 * coord.
2539 */
2540 ir->shadow_comparitor->accept(this);
2541 coord_dst.writemask = WRITEMASK_Z;
2542 emit(ir, TGSI_OPCODE_MOV, coord_dst, this->result);
2543 coord_dst.writemask = WRITEMASK_XYZW;
2544 }
2545
2546 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXB ||
2547 opcode == TGSI_OPCODE_TXF) {
2548 /* TGSI stores LOD or LOD bias in the last channel of the coords. */
2549 coord_dst.writemask = WRITEMASK_W;
2550 emit(ir, TGSI_OPCODE_MOV, coord_dst, lod_info);
2551 coord_dst.writemask = WRITEMASK_XYZW;
2552 }
2553
2554 if (opcode == TGSI_OPCODE_TXD)
2555 inst = emit(ir, opcode, result_dst, coord, dx, dy);
2556 else if (opcode == TGSI_OPCODE_TXQ)
2557 inst = emit(ir, opcode, result_dst, lod_info);
2558 else
2559 inst = emit(ir, opcode, result_dst, coord);
2560
2561 if (ir->shadow_comparitor)
2562 inst->tex_shadow = GL_TRUE;
2563
2564 inst->sampler = _mesa_get_sampler_uniform_value(ir->sampler,
2565 this->shader_program,
2566 this->prog);
2567
2568 const glsl_type *sampler_type = ir->sampler->type;
2569
2570 switch (sampler_type->sampler_dimensionality) {
2571 case GLSL_SAMPLER_DIM_1D:
2572 inst->tex_target = (sampler_type->sampler_array)
2573 ? TEXTURE_1D_ARRAY_INDEX : TEXTURE_1D_INDEX;
2574 break;
2575 case GLSL_SAMPLER_DIM_2D:
2576 inst->tex_target = (sampler_type->sampler_array)
2577 ? TEXTURE_2D_ARRAY_INDEX : TEXTURE_2D_INDEX;
2578 break;
2579 case GLSL_SAMPLER_DIM_3D:
2580 inst->tex_target = TEXTURE_3D_INDEX;
2581 break;
2582 case GLSL_SAMPLER_DIM_CUBE:
2583 inst->tex_target = TEXTURE_CUBE_INDEX;
2584 break;
2585 case GLSL_SAMPLER_DIM_RECT:
2586 inst->tex_target = TEXTURE_RECT_INDEX;
2587 break;
2588 case GLSL_SAMPLER_DIM_BUF:
2589 assert(!"FINISHME: Implement ARB_texture_buffer_object");
2590 break;
2591 default:
2592 assert(!"Should not get here.");
2593 }
2594
2595 this->result = result_src;
2596 }
2597
2598 void
2599 glsl_to_tgsi_visitor::visit(ir_return *ir)
2600 {
2601 if (ir->get_value()) {
2602 st_dst_reg l;
2603 int i;
2604
2605 assert(current_function);
2606
2607 ir->get_value()->accept(this);
2608 st_src_reg r = this->result;
2609
2610 l = st_dst_reg(current_function->return_reg);
2611
2612 for (i = 0; i < type_size(current_function->sig->return_type); i++) {
2613 emit(ir, TGSI_OPCODE_MOV, l, r);
2614 l.index++;
2615 r.index++;
2616 }
2617 }
2618
2619 emit(ir, TGSI_OPCODE_RET);
2620 }
2621
2622 void
2623 glsl_to_tgsi_visitor::visit(ir_discard *ir)
2624 {
2625 struct gl_fragment_program *fp = (struct gl_fragment_program *)this->prog;
2626
2627 if (ir->condition) {
2628 ir->condition->accept(this);
2629 this->result.negate = ~this->result.negate;
2630 emit(ir, TGSI_OPCODE_KIL, undef_dst, this->result);
2631 } else {
2632 emit(ir, TGSI_OPCODE_KILP);
2633 }
2634
2635 fp->UsesKill = GL_TRUE;
2636 }
2637
2638 void
2639 glsl_to_tgsi_visitor::visit(ir_if *ir)
2640 {
2641 glsl_to_tgsi_instruction *cond_inst, *if_inst;
2642 glsl_to_tgsi_instruction *prev_inst;
2643
2644 prev_inst = (glsl_to_tgsi_instruction *)this->instructions.get_tail();
2645
2646 ir->condition->accept(this);
2647 assert(this->result.file != PROGRAM_UNDEFINED);
2648
2649 if (this->options->EmitCondCodes) {
2650 cond_inst = (glsl_to_tgsi_instruction *)this->instructions.get_tail();
2651
2652 /* See if we actually generated any instruction for generating
2653 * the condition. If not, then cook up a move to a temp so we
2654 * have something to set cond_update on.
2655 */
2656 if (cond_inst == prev_inst) {
2657 st_src_reg temp = get_temp(glsl_type::bool_type);
2658 cond_inst = emit(ir->condition, TGSI_OPCODE_MOV, st_dst_reg(temp), result);
2659 }
2660 cond_inst->cond_update = GL_TRUE;
2661
2662 if_inst = emit(ir->condition, TGSI_OPCODE_IF);
2663 if_inst->dst.cond_mask = COND_NE;
2664 } else {
2665 if_inst = emit(ir->condition, TGSI_OPCODE_IF, undef_dst, this->result);
2666 }
2667
2668 this->instructions.push_tail(if_inst);
2669
2670 visit_exec_list(&ir->then_instructions, this);
2671
2672 if (!ir->else_instructions.is_empty()) {
2673 emit(ir->condition, TGSI_OPCODE_ELSE);
2674 visit_exec_list(&ir->else_instructions, this);
2675 }
2676
2677 if_inst = emit(ir->condition, TGSI_OPCODE_ENDIF);
2678 }
2679
2680 glsl_to_tgsi_visitor::glsl_to_tgsi_visitor()
2681 {
2682 result.file = PROGRAM_UNDEFINED;
2683 next_temp = 1;
2684 next_signature_id = 1;
2685 num_immediates = 0;
2686 current_function = NULL;
2687 num_address_regs = 0;
2688 indirect_addr_temps = false;
2689 indirect_addr_consts = false;
2690 mem_ctx = ralloc_context(NULL);
2691 }
2692
2693 glsl_to_tgsi_visitor::~glsl_to_tgsi_visitor()
2694 {
2695 ralloc_free(mem_ctx);
2696 }
2697
2698 extern "C" void free_glsl_to_tgsi_visitor(glsl_to_tgsi_visitor *v)
2699 {
2700 delete v;
2701 }
2702
2703
2704 /**
2705 * Count resources used by the given gpu program (number of texture
2706 * samplers, etc).
2707 */
2708 static void
2709 count_resources(glsl_to_tgsi_visitor *v, gl_program *prog)
2710 {
2711 v->samplers_used = 0;
2712
2713 foreach_iter(exec_list_iterator, iter, v->instructions) {
2714 glsl_to_tgsi_instruction *inst = (glsl_to_tgsi_instruction *)iter.get();
2715
2716 if (is_tex_instruction(inst->op)) {
2717 v->samplers_used |= 1 << inst->sampler;
2718
2719 prog->SamplerTargets[inst->sampler] =
2720 (gl_texture_index)inst->tex_target;
2721 if (inst->tex_shadow) {
2722 prog->ShadowSamplers |= 1 << inst->sampler;
2723 }
2724 }
2725 }
2726
2727 prog->SamplersUsed = v->samplers_used;
2728 _mesa_update_shader_textures_used(prog);
2729 }
2730
2731
2732 /**
2733 * Check if the given vertex/fragment/shader program is within the
2734 * resource limits of the context (number of texture units, etc).
2735 * If any of those checks fail, record a linker error.
2736 *
2737 * XXX more checks are needed...
2738 */
2739 static void
2740 check_resources(const struct gl_context *ctx,
2741 struct gl_shader_program *shader_program,
2742 glsl_to_tgsi_visitor *prog,
2743 struct gl_program *proginfo)
2744 {
2745 switch (proginfo->Target) {
2746 case GL_VERTEX_PROGRAM_ARB:
2747 if (_mesa_bitcount(prog->samplers_used) >
2748 ctx->Const.MaxVertexTextureImageUnits) {
2749 fail_link(shader_program, "Too many vertex shader texture samplers");
2750 }
2751 if (proginfo->Parameters->NumParameters > MAX_UNIFORMS) {
2752 fail_link(shader_program, "Too many vertex shader constants");
2753 }
2754 break;
2755 case MESA_GEOMETRY_PROGRAM:
2756 if (_mesa_bitcount(prog->samplers_used) >
2757 ctx->Const.MaxGeometryTextureImageUnits) {
2758 fail_link(shader_program, "Too many geometry shader texture samplers");
2759 }
2760 if (proginfo->Parameters->NumParameters >
2761 MAX_GEOMETRY_UNIFORM_COMPONENTS / 4) {
2762 fail_link(shader_program, "Too many geometry shader constants");
2763 }
2764 break;
2765 case GL_FRAGMENT_PROGRAM_ARB:
2766 if (_mesa_bitcount(prog->samplers_used) >
2767 ctx->Const.MaxTextureImageUnits) {
2768 fail_link(shader_program, "Too many fragment shader texture samplers");
2769 }
2770 if (proginfo->Parameters->NumParameters > MAX_UNIFORMS) {
2771 fail_link(shader_program, "Too many fragment shader constants");
2772 }
2773 break;
2774 default:
2775 _mesa_problem(ctx, "unexpected program type in check_resources()");
2776 }
2777 }
2778
2779
2780
2781 struct uniform_sort {
2782 struct gl_uniform *u;
2783 int pos;
2784 };
2785
2786 /* The shader_program->Uniforms list is almost sorted in increasing
2787 * uniform->{Frag,Vert}Pos locations, but not quite when there are
2788 * uniforms shared between targets. We need to add parameters in
2789 * increasing order for the targets.
2790 */
2791 static int
2792 sort_uniforms(const void *a, const void *b)
2793 {
2794 struct uniform_sort *u1 = (struct uniform_sort *)a;
2795 struct uniform_sort *u2 = (struct uniform_sort *)b;
2796
2797 return u1->pos - u2->pos;
2798 }
2799
2800 /* Add the uniforms to the parameters. The linker chose locations
2801 * in our parameters lists (which weren't created yet), which the
2802 * uniforms code will use to poke values into our parameters list
2803 * when uniforms are updated.
2804 */
2805 static void
2806 add_uniforms_to_parameters_list(struct gl_shader_program *shader_program,
2807 struct gl_shader *shader,
2808 struct gl_program *prog)
2809 {
2810 unsigned int i;
2811 unsigned int next_sampler = 0, num_uniforms = 0;
2812 struct uniform_sort *sorted_uniforms;
2813
2814 sorted_uniforms = ralloc_array(NULL, struct uniform_sort,
2815 shader_program->Uniforms->NumUniforms);
2816
2817 for (i = 0; i < shader_program->Uniforms->NumUniforms; i++) {
2818 struct gl_uniform *uniform = shader_program->Uniforms->Uniforms + i;
2819 int parameter_index = -1;
2820
2821 switch (shader->Type) {
2822 case GL_VERTEX_SHADER:
2823 parameter_index = uniform->VertPos;
2824 break;
2825 case GL_FRAGMENT_SHADER:
2826 parameter_index = uniform->FragPos;
2827 break;
2828 case GL_GEOMETRY_SHADER:
2829 parameter_index = uniform->GeomPos;
2830 break;
2831 }
2832
2833 /* Only add uniforms used in our target. */
2834 if (parameter_index != -1) {
2835 sorted_uniforms[num_uniforms].pos = parameter_index;
2836 sorted_uniforms[num_uniforms].u = uniform;
2837 num_uniforms++;
2838 }
2839 }
2840
2841 qsort(sorted_uniforms, num_uniforms, sizeof(struct uniform_sort),
2842 sort_uniforms);
2843
2844 for (i = 0; i < num_uniforms; i++) {
2845 struct gl_uniform *uniform = sorted_uniforms[i].u;
2846 int parameter_index = sorted_uniforms[i].pos;
2847 const glsl_type *type = uniform->Type;
2848 unsigned int size;
2849
2850 if (type->is_vector() ||
2851 type->is_scalar()) {
2852 size = type->vector_elements;
2853 } else {
2854 size = type_size(type) * 4;
2855 }
2856
2857 gl_register_file file;
2858 if (type->is_sampler() ||
2859 (type->is_array() && type->fields.array->is_sampler())) {
2860 file = PROGRAM_SAMPLER;
2861 } else {
2862 file = PROGRAM_UNIFORM;
2863 }
2864
2865 GLint index = _mesa_lookup_parameter_index(prog->Parameters, -1,
2866 uniform->Name);
2867
2868 if (index < 0) {
2869 index = _mesa_add_parameter(prog->Parameters, file,
2870 uniform->Name, size, type->gl_type,
2871 NULL, NULL, 0x0);
2872
2873 /* Sampler uniform values are stored in prog->SamplerUnits,
2874 * and the entry in that array is selected by this index we
2875 * store in ParameterValues[].
2876 */
2877 if (file == PROGRAM_SAMPLER) {
2878 for (unsigned int j = 0; j < size / 4; j++)
2879 prog->Parameters->ParameterValues[index + j][0].f = next_sampler++;
2880 }
2881
2882 /* The location chosen in the Parameters list here (returned
2883 * from _mesa_add_uniform) has to match what the linker chose.
2884 */
2885 if (index != parameter_index) {
2886 fail_link(shader_program, "Allocation of uniform `%s' to target "
2887 "failed (%d vs %d)\n",
2888 uniform->Name, index, parameter_index);
2889 }
2890 }
2891 }
2892
2893 ralloc_free(sorted_uniforms);
2894 }
2895
2896 static void
2897 set_uniform_initializer(struct gl_context *ctx, void *mem_ctx,
2898 struct gl_shader_program *shader_program,
2899 const char *name, const glsl_type *type,
2900 ir_constant *val)
2901 {
2902 if (type->is_record()) {
2903 ir_constant *field_constant;
2904
2905 field_constant = (ir_constant *)val->components.get_head();
2906
2907 for (unsigned int i = 0; i < type->length; i++) {
2908 const glsl_type *field_type = type->fields.structure[i].type;
2909 const char *field_name = ralloc_asprintf(mem_ctx, "%s.%s", name,
2910 type->fields.structure[i].name);
2911 set_uniform_initializer(ctx, mem_ctx, shader_program, field_name,
2912 field_type, field_constant);
2913 field_constant = (ir_constant *)field_constant->next;
2914 }
2915 return;
2916 }
2917
2918 int loc = _mesa_get_uniform_location(ctx, shader_program, name);
2919
2920 if (loc == -1) {
2921 fail_link(shader_program,
2922 "Couldn't find uniform for initializer %s\n", name);
2923 return;
2924 }
2925
2926 for (unsigned int i = 0; i < (type->is_array() ? type->length : 1); i++) {
2927 ir_constant *element;
2928 const glsl_type *element_type;
2929 if (type->is_array()) {
2930 element = val->array_elements[i];
2931 element_type = type->fields.array;
2932 } else {
2933 element = val;
2934 element_type = type;
2935 }
2936
2937 void *values;
2938
2939 if (element_type->base_type == GLSL_TYPE_BOOL) {
2940 int *conv = ralloc_array(mem_ctx, int, element_type->components());
2941 for (unsigned int j = 0; j < element_type->components(); j++) {
2942 conv[j] = element->value.b[j];
2943 }
2944 values = (void *)conv;
2945 element_type = glsl_type::get_instance(GLSL_TYPE_INT,
2946 element_type->vector_elements,
2947 1);
2948 } else {
2949 values = &element->value;
2950 }
2951
2952 if (element_type->is_matrix()) {
2953 _mesa_uniform_matrix(ctx, shader_program,
2954 element_type->matrix_columns,
2955 element_type->vector_elements,
2956 loc, 1, GL_FALSE, (GLfloat *)values);
2957 loc += element_type->matrix_columns;
2958 } else {
2959 _mesa_uniform(ctx, shader_program, loc, element_type->matrix_columns,
2960 values, element_type->gl_type);
2961 loc += type_size(element_type);
2962 }
2963 }
2964 }
2965
2966 static void
2967 set_uniform_initializers(struct gl_context *ctx,
2968 struct gl_shader_program *shader_program)
2969 {
2970 void *mem_ctx = NULL;
2971
2972 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
2973 struct gl_shader *shader = shader_program->_LinkedShaders[i];
2974
2975 if (shader == NULL)
2976 continue;
2977
2978 foreach_iter(exec_list_iterator, iter, *shader->ir) {
2979 ir_instruction *ir = (ir_instruction *)iter.get();
2980 ir_variable *var = ir->as_variable();
2981
2982 if (!var || var->mode != ir_var_uniform || !var->constant_value)
2983 continue;
2984
2985 if (!mem_ctx)
2986 mem_ctx = ralloc_context(NULL);
2987
2988 set_uniform_initializer(ctx, mem_ctx, shader_program, var->name,
2989 var->type, var->constant_value);
2990 }
2991 }
2992
2993 ralloc_free(mem_ctx);
2994 }
2995
2996 /*
2997 * Scan/rewrite program to remove reads of custom (output) registers.
2998 * The passed type has to be either PROGRAM_OUTPUT or PROGRAM_VARYING
2999 * (for vertex shaders).
3000 * In GLSL shaders, varying vars can be read and written.
3001 * On some hardware, trying to read an output register causes trouble.
3002 * So, rewrite the program to use a temporary register in this case.
3003 *
3004 * Based on _mesa_remove_output_reads from programopt.c.
3005 */
3006 void
3007 glsl_to_tgsi_visitor::remove_output_reads(gl_register_file type)
3008 {
3009 GLuint i;
3010 GLint outputMap[VERT_RESULT_MAX];
3011 GLint outputTypes[VERT_RESULT_MAX];
3012 GLuint numVaryingReads = 0;
3013 GLboolean usedTemps[MAX_TEMPS];
3014 GLuint firstTemp = 0;
3015
3016 _mesa_find_used_registers(prog, PROGRAM_TEMPORARY,
3017 usedTemps, MAX_TEMPS);
3018
3019 assert(type == PROGRAM_VARYING || type == PROGRAM_OUTPUT);
3020 assert(prog->Target == GL_VERTEX_PROGRAM_ARB || type != PROGRAM_VARYING);
3021
3022 for (i = 0; i < VERT_RESULT_MAX; i++)
3023 outputMap[i] = -1;
3024
3025 /* look for instructions which read from varying vars */
3026 foreach_iter(exec_list_iterator, iter, this->instructions) {
3027 glsl_to_tgsi_instruction *inst = (glsl_to_tgsi_instruction *)iter.get();
3028 const GLuint numSrc = num_inst_src_regs(inst->op);
3029 GLuint j;
3030 for (j = 0; j < numSrc; j++) {
3031 if (inst->src[j].file == type) {
3032 /* replace the read with a temp reg */
3033 const GLuint var = inst->src[j].index;
3034 if (outputMap[var] == -1) {
3035 numVaryingReads++;
3036 outputMap[var] = _mesa_find_free_register(usedTemps,
3037 MAX_TEMPS,
3038 firstTemp);
3039 outputTypes[var] = inst->src[j].type;
3040 firstTemp = outputMap[var] + 1;
3041 }
3042 inst->src[j].file = PROGRAM_TEMPORARY;
3043 inst->src[j].index = outputMap[var];
3044 }
3045 }
3046 }
3047
3048 if (numVaryingReads == 0)
3049 return; /* nothing to be done */
3050
3051 /* look for instructions which write to the varying vars identified above */
3052 foreach_iter(exec_list_iterator, iter, this->instructions) {
3053 glsl_to_tgsi_instruction *inst = (glsl_to_tgsi_instruction *)iter.get();
3054 if (inst->dst.file == type && outputMap[inst->dst.index] >= 0) {
3055 /* change inst to write to the temp reg, instead of the varying */
3056 inst->dst.file = PROGRAM_TEMPORARY;
3057 inst->dst.index = outputMap[inst->dst.index];
3058 }
3059 }
3060
3061 /* insert new MOV instructions at the end */
3062 for (i = 0; i < VERT_RESULT_MAX; i++) {
3063 if (outputMap[i] >= 0) {
3064 /* MOV VAR[i], TEMP[tmp]; */
3065 st_src_reg src = st_src_reg(PROGRAM_TEMPORARY, outputMap[i], outputTypes[i]);
3066 st_dst_reg dst = st_dst_reg(type, WRITEMASK_XYZW, outputTypes[i]);
3067 dst.index = i;
3068 this->emit(NULL, TGSI_OPCODE_MOV, dst, src);
3069 }
3070 }
3071 }
3072
3073 /**
3074 * Returns the mask of channels (bitmask of WRITEMASK_X,Y,Z,W) which
3075 * are read from the given src in this instruction
3076 */
3077 static int
3078 get_src_arg_mask(st_dst_reg dst, st_src_reg src)
3079 {
3080 int read_mask = 0, comp;
3081
3082 /* Now, given the src swizzle and the written channels, find which
3083 * components are actually read
3084 */
3085 for (comp = 0; comp < 4; ++comp) {
3086 const unsigned coord = GET_SWZ(src.swizzle, comp);
3087 ASSERT(coord < 4);
3088 if (dst.writemask & (1 << comp) && coord <= SWIZZLE_W)
3089 read_mask |= 1 << coord;
3090 }
3091
3092 return read_mask;
3093 }
3094
3095 /**
3096 * This pass replaces CMP T0, T1 T2 T0 with MOV T0, T2 when the CMP
3097 * instruction is the first instruction to write to register T0. There are
3098 * several lowering passes done in GLSL IR (e.g. branches and
3099 * relative addressing) that create a large number of conditional assignments
3100 * that ir_to_mesa converts to CMP instructions like the one mentioned above.
3101 *
3102 * Here is why this conversion is safe:
3103 * CMP T0, T1 T2 T0 can be expanded to:
3104 * if (T1 < 0.0)
3105 * MOV T0, T2;
3106 * else
3107 * MOV T0, T0;
3108 *
3109 * If (T1 < 0.0) evaluates to true then our replacement MOV T0, T2 is the same
3110 * as the original program. If (T1 < 0.0) evaluates to false, executing
3111 * MOV T0, T0 will store a garbage value in T0 since T0 is uninitialized.
3112 * Therefore, it doesn't matter that we are replacing MOV T0, T0 with MOV T0, T2
3113 * because any instruction that was going to read from T0 after this was going
3114 * to read a garbage value anyway.
3115 */
3116 void
3117 glsl_to_tgsi_visitor::simplify_cmp(void)
3118 {
3119 unsigned tempWrites[MAX_TEMPS];
3120 unsigned outputWrites[MAX_PROGRAM_OUTPUTS];
3121
3122 memset(tempWrites, 0, sizeof(tempWrites));
3123 memset(outputWrites, 0, sizeof(outputWrites));
3124
3125 foreach_iter(exec_list_iterator, iter, this->instructions) {
3126 glsl_to_tgsi_instruction *inst = (glsl_to_tgsi_instruction *)iter.get();
3127 unsigned prevWriteMask = 0;
3128
3129 /* Give up if we encounter relative addressing or flow control. */
3130 if (inst->dst.reladdr ||
3131 tgsi_get_opcode_info(inst->op)->is_branch ||
3132 inst->op == TGSI_OPCODE_BGNSUB ||
3133 inst->op == TGSI_OPCODE_CONT ||
3134 inst->op == TGSI_OPCODE_END ||
3135 inst->op == TGSI_OPCODE_ENDSUB ||
3136 inst->op == TGSI_OPCODE_RET) {
3137 return;
3138 }
3139
3140 if (inst->dst.file == PROGRAM_OUTPUT) {
3141 assert(inst->dst.index < MAX_PROGRAM_OUTPUTS);
3142 prevWriteMask = outputWrites[inst->dst.index];
3143 outputWrites[inst->dst.index] |= inst->dst.writemask;
3144 } else if (inst->dst.file == PROGRAM_TEMPORARY) {
3145 assert(inst->dst.index < MAX_TEMPS);
3146 prevWriteMask = tempWrites[inst->dst.index];
3147 tempWrites[inst->dst.index] |= inst->dst.writemask;
3148 }
3149
3150 /* For a CMP to be considered a conditional write, the destination
3151 * register and source register two must be the same. */
3152 if (inst->op == TGSI_OPCODE_CMP
3153 && !(inst->dst.writemask & prevWriteMask)
3154 && inst->src[2].file == inst->dst.file
3155 && inst->src[2].index == inst->dst.index
3156 && inst->dst.writemask == get_src_arg_mask(inst->dst, inst->src[2])) {
3157
3158 inst->op = TGSI_OPCODE_MOV;
3159 inst->src[0] = inst->src[1];
3160 }
3161 }
3162 }
3163
3164 /* Replaces all references to a temporary register index with another index. */
3165 void
3166 glsl_to_tgsi_visitor::rename_temp_register(int index, int new_index)
3167 {
3168 foreach_iter(exec_list_iterator, iter, this->instructions) {
3169 glsl_to_tgsi_instruction *inst = (glsl_to_tgsi_instruction *)iter.get();
3170 unsigned j;
3171
3172 for (j=0; j < num_inst_src_regs(inst->op); j++) {
3173 if (inst->src[j].file == PROGRAM_TEMPORARY &&
3174 inst->src[j].index == index) {
3175 inst->src[j].index = new_index;
3176 }
3177 }
3178
3179 if (inst->dst.file == PROGRAM_TEMPORARY && inst->dst.index == index) {
3180 inst->dst.index = new_index;
3181 }
3182 }
3183 }
3184
3185 int
3186 glsl_to_tgsi_visitor::get_first_temp_read(int index)
3187 {
3188 int depth = 0; /* loop depth */
3189 int loop_start = -1; /* index of the first active BGNLOOP (if any) */
3190 unsigned i = 0, j;
3191
3192 foreach_iter(exec_list_iterator, iter, this->instructions) {
3193 glsl_to_tgsi_instruction *inst = (glsl_to_tgsi_instruction *)iter.get();
3194
3195 for (j=0; j < num_inst_src_regs(inst->op); j++) {
3196 if (inst->src[j].file == PROGRAM_TEMPORARY &&
3197 inst->src[j].index == index) {
3198 return (depth == 0) ? i : loop_start;
3199 }
3200 }
3201
3202 if (inst->op == TGSI_OPCODE_BGNLOOP) {
3203 if(depth++ == 0)
3204 loop_start = i;
3205 } else if (inst->op == TGSI_OPCODE_ENDLOOP) {
3206 if (--depth == 0)
3207 loop_start = -1;
3208 }
3209 assert(depth >= 0);
3210
3211 i++;
3212 }
3213
3214 return -1;
3215 }
3216
3217 int
3218 glsl_to_tgsi_visitor::get_first_temp_write(int index)
3219 {
3220 int depth = 0; /* loop depth */
3221 int loop_start = -1; /* index of the first active BGNLOOP (if any) */
3222 int i = 0;
3223
3224 foreach_iter(exec_list_iterator, iter, this->instructions) {
3225 glsl_to_tgsi_instruction *inst = (glsl_to_tgsi_instruction *)iter.get();
3226
3227 if (inst->dst.file == PROGRAM_TEMPORARY && inst->dst.index == index) {
3228 return (depth == 0) ? i : loop_start;
3229 }
3230
3231 if (inst->op == TGSI_OPCODE_BGNLOOP) {
3232 if(depth++ == 0)
3233 loop_start = i;
3234 } else if (inst->op == TGSI_OPCODE_ENDLOOP) {
3235 if (--depth == 0)
3236 loop_start = -1;
3237 }
3238 assert(depth >= 0);
3239
3240 i++;
3241 }
3242
3243 return -1;
3244 }
3245
3246 int
3247 glsl_to_tgsi_visitor::get_last_temp_read(int index)
3248 {
3249 int depth = 0; /* loop depth */
3250 int last = -1; /* index of last instruction that reads the temporary */
3251 unsigned i = 0, j;
3252
3253 foreach_iter(exec_list_iterator, iter, this->instructions) {
3254 glsl_to_tgsi_instruction *inst = (glsl_to_tgsi_instruction *)iter.get();
3255
3256 for (j=0; j < num_inst_src_regs(inst->op); j++) {
3257 if (inst->src[j].file == PROGRAM_TEMPORARY &&
3258 inst->src[j].index == index) {
3259 last = (depth == 0) ? i : -2;
3260 }
3261 }
3262
3263 if (inst->op == TGSI_OPCODE_BGNLOOP)
3264 depth++;
3265 else if (inst->op == TGSI_OPCODE_ENDLOOP)
3266 if (--depth == 0 && last == -2)
3267 last = i;
3268 assert(depth >= 0);
3269
3270 i++;
3271 }
3272
3273 assert(last >= -1);
3274 return last;
3275 }
3276
3277 int
3278 glsl_to_tgsi_visitor::get_last_temp_write(int index)
3279 {
3280 int depth = 0; /* loop depth */
3281 int last = -1; /* index of last instruction that writes to the temporary */
3282 int i = 0;
3283
3284 foreach_iter(exec_list_iterator, iter, this->instructions) {
3285 glsl_to_tgsi_instruction *inst = (glsl_to_tgsi_instruction *)iter.get();
3286
3287 if (inst->dst.file == PROGRAM_TEMPORARY && inst->dst.index == index)
3288 last = (depth == 0) ? i : -2;
3289
3290 if (inst->op == TGSI_OPCODE_BGNLOOP)
3291 depth++;
3292 else if (inst->op == TGSI_OPCODE_ENDLOOP)
3293 if (--depth == 0 && last == -2)
3294 last = i;
3295 assert(depth >= 0);
3296
3297 i++;
3298 }
3299
3300 assert(last >= -1);
3301 return last;
3302 }
3303
3304 /*
3305 * On a basic block basis, tracks available PROGRAM_TEMPORARY register
3306 * channels for copy propagation and updates following instructions to
3307 * use the original versions.
3308 *
3309 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
3310 * will occur. As an example, a TXP production before this pass:
3311 *
3312 * 0: MOV TEMP[1], INPUT[4].xyyy;
3313 * 1: MOV TEMP[1].w, INPUT[4].wwww;
3314 * 2: TXP TEMP[2], TEMP[1], texture[0], 2D;
3315 *
3316 * and after:
3317 *
3318 * 0: MOV TEMP[1], INPUT[4].xyyy;
3319 * 1: MOV TEMP[1].w, INPUT[4].wwww;
3320 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
3321 *
3322 * which allows for dead code elimination on TEMP[1]'s writes.
3323 */
3324 void
3325 glsl_to_tgsi_visitor::copy_propagate(void)
3326 {
3327 glsl_to_tgsi_instruction **acp = rzalloc_array(mem_ctx,
3328 glsl_to_tgsi_instruction *,
3329 this->next_temp * 4);
3330 int *acp_level = rzalloc_array(mem_ctx, int, this->next_temp * 4);
3331 int level = 0;
3332
3333 foreach_iter(exec_list_iterator, iter, this->instructions) {
3334 glsl_to_tgsi_instruction *inst = (glsl_to_tgsi_instruction *)iter.get();
3335
3336 assert(inst->dst.file != PROGRAM_TEMPORARY
3337 || inst->dst.index < this->next_temp);
3338
3339 /* First, do any copy propagation possible into the src regs. */
3340 for (int r = 0; r < 3; r++) {
3341 glsl_to_tgsi_instruction *first = NULL;
3342 bool good = true;
3343 int acp_base = inst->src[r].index * 4;
3344
3345 if (inst->src[r].file != PROGRAM_TEMPORARY ||
3346 inst->src[r].reladdr)
3347 continue;
3348
3349 /* See if we can find entries in the ACP consisting of MOVs
3350 * from the same src register for all the swizzled channels
3351 * of this src register reference.
3352 */
3353 for (int i = 0; i < 4; i++) {
3354 int src_chan = GET_SWZ(inst->src[r].swizzle, i);
3355 glsl_to_tgsi_instruction *copy_chan = acp[acp_base + src_chan];
3356
3357 if (!copy_chan) {
3358 good = false;
3359 break;
3360 }
3361
3362 assert(acp_level[acp_base + src_chan] <= level);
3363
3364 if (!first) {
3365 first = copy_chan;
3366 } else {
3367 if (first->src[0].file != copy_chan->src[0].file ||
3368 first->src[0].index != copy_chan->src[0].index) {
3369 good = false;
3370 break;
3371 }
3372 }
3373 }
3374
3375 if (good) {
3376 /* We've now validated that we can copy-propagate to
3377 * replace this src register reference. Do it.
3378 */
3379 inst->src[r].file = first->src[0].file;
3380 inst->src[r].index = first->src[0].index;
3381
3382 int swizzle = 0;
3383 for (int i = 0; i < 4; i++) {
3384 int src_chan = GET_SWZ(inst->src[r].swizzle, i);
3385 glsl_to_tgsi_instruction *copy_inst = acp[acp_base + src_chan];
3386 swizzle |= (GET_SWZ(copy_inst->src[0].swizzle, src_chan) <<
3387 (3 * i));
3388 }
3389 inst->src[r].swizzle = swizzle;
3390 }
3391 }
3392
3393 switch (inst->op) {
3394 case TGSI_OPCODE_BGNLOOP:
3395 case TGSI_OPCODE_ENDLOOP:
3396 /* End of a basic block, clear the ACP entirely. */
3397 memset(acp, 0, sizeof(*acp) * this->next_temp * 4);
3398 break;
3399
3400 case TGSI_OPCODE_IF:
3401 ++level;
3402 break;
3403
3404 case TGSI_OPCODE_ENDIF:
3405 case TGSI_OPCODE_ELSE:
3406 /* Clear all channels written inside the block from the ACP, but
3407 * leaving those that were not touched.
3408 */
3409 for (int r = 0; r < this->next_temp; r++) {
3410 for (int c = 0; c < 4; c++) {
3411 if (!acp[4 * r + c])
3412 continue;
3413
3414 if (acp_level[4 * r + c] >= level)
3415 acp[4 * r + c] = NULL;
3416 }
3417 }
3418 if (inst->op == TGSI_OPCODE_ENDIF)
3419 --level;
3420 break;
3421
3422 default:
3423 /* Continuing the block, clear any written channels from
3424 * the ACP.
3425 */
3426 if (inst->dst.file == PROGRAM_TEMPORARY && inst->dst.reladdr) {
3427 /* Any temporary might be written, so no copy propagation
3428 * across this instruction.
3429 */
3430 memset(acp, 0, sizeof(*acp) * this->next_temp * 4);
3431 } else if (inst->dst.file == PROGRAM_OUTPUT &&
3432 inst->dst.reladdr) {
3433 /* Any output might be written, so no copy propagation
3434 * from outputs across this instruction.
3435 */
3436 for (int r = 0; r < this->next_temp; r++) {
3437 for (int c = 0; c < 4; c++) {
3438 if (!acp[4 * r + c])
3439 continue;
3440
3441 if (acp[4 * r + c]->src[0].file == PROGRAM_OUTPUT)
3442 acp[4 * r + c] = NULL;
3443 }
3444 }
3445 } else if (inst->dst.file == PROGRAM_TEMPORARY ||
3446 inst->dst.file == PROGRAM_OUTPUT) {
3447 /* Clear where it's used as dst. */
3448 if (inst->dst.file == PROGRAM_TEMPORARY) {
3449 for (int c = 0; c < 4; c++) {
3450 if (inst->dst.writemask & (1 << c)) {
3451 acp[4 * inst->dst.index + c] = NULL;
3452 }
3453 }
3454 }
3455
3456 /* Clear where it's used as src. */
3457 for (int r = 0; r < this->next_temp; r++) {
3458 for (int c = 0; c < 4; c++) {
3459 if (!acp[4 * r + c])
3460 continue;
3461
3462 int src_chan = GET_SWZ(acp[4 * r + c]->src[0].swizzle, c);
3463
3464 if (acp[4 * r + c]->src[0].file == inst->dst.file &&
3465 acp[4 * r + c]->src[0].index == inst->dst.index &&
3466 inst->dst.writemask & (1 << src_chan))
3467 {
3468 acp[4 * r + c] = NULL;
3469 }
3470 }
3471 }
3472 }
3473 break;
3474 }
3475
3476 /* If this is a copy, add it to the ACP. */
3477 if (inst->op == TGSI_OPCODE_MOV &&
3478 inst->dst.file == PROGRAM_TEMPORARY &&
3479 !inst->dst.reladdr &&
3480 !inst->saturate &&
3481 !inst->src[0].reladdr &&
3482 !inst->src[0].negate) {
3483 for (int i = 0; i < 4; i++) {
3484 if (inst->dst.writemask & (1 << i)) {
3485 acp[4 * inst->dst.index + i] = inst;
3486 acp_level[4 * inst->dst.index + i] = level;
3487 }
3488 }
3489 }
3490 }
3491
3492 ralloc_free(acp_level);
3493 ralloc_free(acp);
3494 }
3495
3496 /*
3497 * Tracks available PROGRAM_TEMPORARY registers for dead code elimination.
3498 *
3499 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
3500 * will occur. As an example, a TXP production after copy propagation but
3501 * before this pass:
3502 *
3503 * 0: MOV TEMP[1], INPUT[4].xyyy;
3504 * 1: MOV TEMP[1].w, INPUT[4].wwww;
3505 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
3506 *
3507 * and after this pass:
3508 *
3509 * 0: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
3510 *
3511 * FIXME: assumes that all functions are inlined (no support for BGNSUB/ENDSUB)
3512 * FIXME: doesn't eliminate all dead code inside of loops; it steps around them
3513 */
3514 void
3515 glsl_to_tgsi_visitor::eliminate_dead_code(void)
3516 {
3517 int i;
3518
3519 for (i=0; i < this->next_temp; i++) {
3520 int last_read = get_last_temp_read(i);
3521 int j = 0;
3522
3523 foreach_iter(exec_list_iterator, iter, this->instructions) {
3524 glsl_to_tgsi_instruction *inst = (glsl_to_tgsi_instruction *)iter.get();
3525
3526 if (inst->dst.file == PROGRAM_TEMPORARY && inst->dst.index == i &&
3527 j > last_read)
3528 {
3529 iter.remove();
3530 delete inst;
3531 }
3532
3533 j++;
3534 }
3535 }
3536 }
3537
3538 /*
3539 * On a basic block basis, tracks available PROGRAM_TEMPORARY registers for dead
3540 * code elimination. This is less primitive than eliminate_dead_code(), as it
3541 * is per-channel and can detect consecutive writes without a read between them
3542 * as dead code. However, there is some dead code that can be eliminated by
3543 * eliminate_dead_code() but not this function - for example, this function
3544 * cannot eliminate an instruction writing to a register that is never read and
3545 * is the only instruction writing to that register.
3546 *
3547 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
3548 * will occur.
3549 */
3550 int
3551 glsl_to_tgsi_visitor::eliminate_dead_code_advanced(void)
3552 {
3553 glsl_to_tgsi_instruction **writes = rzalloc_array(mem_ctx,
3554 glsl_to_tgsi_instruction *,
3555 this->next_temp * 4);
3556 int *write_level = rzalloc_array(mem_ctx, int, this->next_temp * 4);
3557 int level = 0;
3558 int removed = 0;
3559
3560 foreach_iter(exec_list_iterator, iter, this->instructions) {
3561 glsl_to_tgsi_instruction *inst = (glsl_to_tgsi_instruction *)iter.get();
3562
3563 assert(inst->dst.file != PROGRAM_TEMPORARY
3564 || inst->dst.index < this->next_temp);
3565
3566 switch (inst->op) {
3567 case TGSI_OPCODE_BGNLOOP:
3568 case TGSI_OPCODE_ENDLOOP:
3569 /* End of a basic block, clear the write array entirely.
3570 * FIXME: This keeps us from killing dead code when the writes are
3571 * on either side of a loop, even when the register isn't touched
3572 * inside the loop.
3573 */
3574 memset(writes, 0, sizeof(*writes) * this->next_temp * 4);
3575 break;
3576
3577 case TGSI_OPCODE_ENDIF:
3578 --level;
3579 break;
3580
3581 case TGSI_OPCODE_ELSE:
3582 /* Clear all channels written inside the preceding if block from the
3583 * write array, but leave those that were not touched.
3584 *
3585 * FIXME: This destroys opportunities to remove dead code inside of
3586 * IF blocks that are followed by an ELSE block.
3587 */
3588 for (int r = 0; r < this->next_temp; r++) {
3589 for (int c = 0; c < 4; c++) {
3590 if (!writes[4 * r + c])
3591 continue;
3592
3593 if (write_level[4 * r + c] >= level)
3594 writes[4 * r + c] = NULL;
3595 }
3596 }
3597 break;
3598
3599 case TGSI_OPCODE_IF:
3600 ++level;
3601 /* fallthrough to default case to mark the condition as read */
3602
3603 default:
3604 /* Continuing the block, clear any channels from the write array that
3605 * are read by this instruction.
3606 */
3607 for (unsigned i = 0; i < Elements(inst->src); i++) {
3608 if (inst->src[i].file == PROGRAM_TEMPORARY && inst->src[i].reladdr){
3609 /* Any temporary might be read, so no dead code elimination
3610 * across this instruction.
3611 */
3612 memset(writes, 0, sizeof(*writes) * this->next_temp * 4);
3613 } else if (inst->src[i].file == PROGRAM_TEMPORARY) {
3614 /* Clear where it's used as src. */
3615 int src_chans = 1 << GET_SWZ(inst->src[i].swizzle, 0);
3616 src_chans |= 1 << GET_SWZ(inst->src[i].swizzle, 1);
3617 src_chans |= 1 << GET_SWZ(inst->src[i].swizzle, 2);
3618 src_chans |= 1 << GET_SWZ(inst->src[i].swizzle, 3);
3619
3620 for (int c = 0; c < 4; c++) {
3621 if (src_chans & (1 << c)) {
3622 writes[4 * inst->src[i].index + c] = NULL;
3623 }
3624 }
3625 }
3626 }
3627 break;
3628 }
3629
3630 /* If this instruction writes to a temporary, add it to the write array.
3631 * If there is already an instruction in the write array for one or more
3632 * of the channels, flag that channel write as dead.
3633 */
3634 if (inst->dst.file == PROGRAM_TEMPORARY &&
3635 !inst->dst.reladdr &&
3636 !inst->saturate) {
3637 for (int c = 0; c < 4; c++) {
3638 if (inst->dst.writemask & (1 << c)) {
3639 if (writes[4 * inst->dst.index + c]) {
3640 if (write_level[4 * inst->dst.index + c] < level)
3641 continue;
3642 else
3643 writes[4 * inst->dst.index + c]->dead_mask |= (1 << c);
3644 }
3645 writes[4 * inst->dst.index + c] = inst;
3646 write_level[4 * inst->dst.index + c] = level;
3647 }
3648 }
3649 }
3650 }
3651
3652 /* Anything still in the write array at this point is dead code. */
3653 for (int r = 0; r < this->next_temp; r++) {
3654 for (int c = 0; c < 4; c++) {
3655 glsl_to_tgsi_instruction *inst = writes[4 * r + c];
3656 if (inst)
3657 inst->dead_mask |= (1 << c);
3658 }
3659 }
3660
3661 /* Now actually remove the instructions that are completely dead and update
3662 * the writemask of other instructions with dead channels.
3663 */
3664 foreach_iter(exec_list_iterator, iter, this->instructions) {
3665 glsl_to_tgsi_instruction *inst = (glsl_to_tgsi_instruction *)iter.get();
3666
3667 if (!inst->dead_mask || !inst->dst.writemask)
3668 continue;
3669 else if (inst->dead_mask == inst->dst.writemask) {
3670 iter.remove();
3671 delete inst;
3672 removed++;
3673 } else
3674 inst->dst.writemask &= ~(inst->dead_mask);
3675 }
3676
3677 ralloc_free(write_level);
3678 ralloc_free(writes);
3679
3680 return removed;
3681 }
3682
3683 /* Merges temporary registers together where possible to reduce the number of
3684 * registers needed to run a program.
3685 *
3686 * Produces optimal code only after copy propagation and dead code elimination
3687 * have been run. */
3688 void
3689 glsl_to_tgsi_visitor::merge_registers(void)
3690 {
3691 int *last_reads = rzalloc_array(mem_ctx, int, this->next_temp);
3692 int *first_writes = rzalloc_array(mem_ctx, int, this->next_temp);
3693 int i, j;
3694
3695 /* Read the indices of the last read and first write to each temp register
3696 * into an array so that we don't have to traverse the instruction list as
3697 * much. */
3698 for (i=0; i < this->next_temp; i++) {
3699 last_reads[i] = get_last_temp_read(i);
3700 first_writes[i] = get_first_temp_write(i);
3701 }
3702
3703 /* Start looking for registers with non-overlapping usages that can be
3704 * merged together. */
3705 for (i=0; i < this->next_temp; i++) {
3706 /* Don't touch unused registers. */
3707 if (last_reads[i] < 0 || first_writes[i] < 0) continue;
3708
3709 for (j=0; j < this->next_temp; j++) {
3710 /* Don't touch unused registers. */
3711 if (last_reads[j] < 0 || first_writes[j] < 0) continue;
3712
3713 /* We can merge the two registers if the first write to j is after or
3714 * in the same instruction as the last read from i. Note that the
3715 * register at index i will always be used earlier or at the same time
3716 * as the register at index j. */
3717 if (first_writes[i] <= first_writes[j] &&
3718 last_reads[i] <= first_writes[j])
3719 {
3720 rename_temp_register(j, i); /* Replace all references to j with i.*/
3721
3722 /* Update the first_writes and last_reads arrays with the new
3723 * values for the merged register index, and mark the newly unused
3724 * register index as such. */
3725 last_reads[i] = last_reads[j];
3726 first_writes[j] = -1;
3727 last_reads[j] = -1;
3728 }
3729 }
3730 }
3731
3732 ralloc_free(last_reads);
3733 ralloc_free(first_writes);
3734 }
3735
3736 /* Reassign indices to temporary registers by reusing unused indices created
3737 * by optimization passes. */
3738 void
3739 glsl_to_tgsi_visitor::renumber_registers(void)
3740 {
3741 int i = 0;
3742 int new_index = 0;
3743
3744 for (i=0; i < this->next_temp; i++) {
3745 if (get_first_temp_read(i) < 0) continue;
3746 if (i != new_index)
3747 rename_temp_register(i, new_index);
3748 new_index++;
3749 }
3750
3751 this->next_temp = new_index;
3752 }
3753
3754 /**
3755 * Returns a fragment program which implements the current pixel transfer ops.
3756 * Based on get_pixel_transfer_program in st_atom_pixeltransfer.c.
3757 */
3758 extern "C" void
3759 get_pixel_transfer_visitor(struct st_fragment_program *fp,
3760 glsl_to_tgsi_visitor *original,
3761 int scale_and_bias, int pixel_maps)
3762 {
3763 glsl_to_tgsi_visitor *v = new glsl_to_tgsi_visitor();
3764 struct st_context *st = st_context(original->ctx);
3765 struct gl_program *prog = &fp->Base.Base;
3766 struct gl_program_parameter_list *params = _mesa_new_parameter_list();
3767 st_src_reg coord, src0;
3768 st_dst_reg dst0;
3769 glsl_to_tgsi_instruction *inst;
3770
3771 /* Copy attributes of the glsl_to_tgsi_visitor in the original shader. */
3772 v->ctx = original->ctx;
3773 v->prog = prog;
3774 v->glsl_version = original->glsl_version;
3775 v->native_integers = original->native_integers;
3776 v->options = original->options;
3777 v->next_temp = original->next_temp;
3778 v->num_address_regs = original->num_address_regs;
3779 v->samplers_used = prog->SamplersUsed = original->samplers_used;
3780 v->indirect_addr_temps = original->indirect_addr_temps;
3781 v->indirect_addr_consts = original->indirect_addr_consts;
3782 memcpy(&v->immediates, &original->immediates, sizeof(v->immediates));
3783
3784 /*
3785 * Get initial pixel color from the texture.
3786 * TEX colorTemp, fragment.texcoord[0], texture[0], 2D;
3787 */
3788 coord = st_src_reg(PROGRAM_INPUT, FRAG_ATTRIB_TEX0, glsl_type::vec2_type);
3789 src0 = v->get_temp(glsl_type::vec4_type);
3790 dst0 = st_dst_reg(src0);
3791 inst = v->emit(NULL, TGSI_OPCODE_TEX, dst0, coord);
3792 inst->sampler = 0;
3793 inst->tex_target = TEXTURE_2D_INDEX;
3794
3795 prog->InputsRead |= (1 << FRAG_ATTRIB_TEX0);
3796 prog->SamplersUsed |= (1 << 0); /* mark sampler 0 as used */
3797 v->samplers_used |= (1 << 0);
3798
3799 if (scale_and_bias) {
3800 static const gl_state_index scale_state[STATE_LENGTH] =
3801 { STATE_INTERNAL, STATE_PT_SCALE,
3802 (gl_state_index) 0, (gl_state_index) 0, (gl_state_index) 0 };
3803 static const gl_state_index bias_state[STATE_LENGTH] =
3804 { STATE_INTERNAL, STATE_PT_BIAS,
3805 (gl_state_index) 0, (gl_state_index) 0, (gl_state_index) 0 };
3806 GLint scale_p, bias_p;
3807 st_src_reg scale, bias;
3808
3809 scale_p = _mesa_add_state_reference(params, scale_state);
3810 bias_p = _mesa_add_state_reference(params, bias_state);
3811
3812 /* MAD colorTemp, colorTemp, scale, bias; */
3813 scale = st_src_reg(PROGRAM_STATE_VAR, scale_p, GLSL_TYPE_FLOAT);
3814 bias = st_src_reg(PROGRAM_STATE_VAR, bias_p, GLSL_TYPE_FLOAT);
3815 inst = v->emit(NULL, TGSI_OPCODE_MAD, dst0, src0, scale, bias);
3816 }
3817
3818 if (pixel_maps) {
3819 st_src_reg temp = v->get_temp(glsl_type::vec4_type);
3820 st_dst_reg temp_dst = st_dst_reg(temp);
3821
3822 assert(st->pixel_xfer.pixelmap_texture);
3823
3824 /* With a little effort, we can do four pixel map look-ups with
3825 * two TEX instructions:
3826 */
3827
3828 /* TEX temp.rg, colorTemp.rgba, texture[1], 2D; */
3829 temp_dst.writemask = WRITEMASK_XY; /* write R,G */
3830 inst = v->emit(NULL, TGSI_OPCODE_TEX, temp_dst, src0);
3831 inst->sampler = 1;
3832 inst->tex_target = TEXTURE_2D_INDEX;
3833
3834 /* TEX temp.ba, colorTemp.baba, texture[1], 2D; */
3835 src0.swizzle = MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, SWIZZLE_Z, SWIZZLE_W);
3836 temp_dst.writemask = WRITEMASK_ZW; /* write B,A */
3837 inst = v->emit(NULL, TGSI_OPCODE_TEX, temp_dst, src0);
3838 inst->sampler = 1;
3839 inst->tex_target = TEXTURE_2D_INDEX;
3840
3841 prog->SamplersUsed |= (1 << 1); /* mark sampler 1 as used */
3842 v->samplers_used |= (1 << 1);
3843
3844 /* MOV colorTemp, temp; */
3845 inst = v->emit(NULL, TGSI_OPCODE_MOV, dst0, temp);
3846 }
3847
3848 /* Now copy the instructions from the original glsl_to_tgsi_visitor into the
3849 * new visitor. */
3850 foreach_iter(exec_list_iterator, iter, original->instructions) {
3851 glsl_to_tgsi_instruction *inst = (glsl_to_tgsi_instruction *)iter.get();
3852 st_src_reg src_regs[3];
3853
3854 if (inst->dst.file == PROGRAM_OUTPUT)
3855 prog->OutputsWritten |= BITFIELD64_BIT(inst->dst.index);
3856
3857 for (int i=0; i<3; i++) {
3858 src_regs[i] = inst->src[i];
3859 if (src_regs[i].file == PROGRAM_INPUT &&
3860 src_regs[i].index == FRAG_ATTRIB_COL0)
3861 {
3862 src_regs[i].file = PROGRAM_TEMPORARY;
3863 src_regs[i].index = src0.index;
3864 }
3865 else if (src_regs[i].file == PROGRAM_INPUT)
3866 prog->InputsRead |= (1 << src_regs[i].index);
3867 }
3868
3869 v->emit(NULL, inst->op, inst->dst, src_regs[0], src_regs[1], src_regs[2]);
3870 }
3871
3872 /* Make modifications to fragment program info. */
3873 prog->Parameters = _mesa_combine_parameter_lists(params,
3874 original->prog->Parameters);
3875 prog->Attributes = _mesa_clone_parameter_list(original->prog->Attributes);
3876 prog->Varying = _mesa_clone_parameter_list(original->prog->Varying);
3877 _mesa_free_parameter_list(params);
3878 count_resources(v, prog);
3879 fp->glsl_to_tgsi = v;
3880 }
3881
3882 /**
3883 * Make fragment program for glBitmap:
3884 * Sample the texture and kill the fragment if the bit is 0.
3885 * This program will be combined with the user's fragment program.
3886 *
3887 * Based on make_bitmap_fragment_program in st_cb_bitmap.c.
3888 */
3889 extern "C" void
3890 get_bitmap_visitor(struct st_fragment_program *fp,
3891 glsl_to_tgsi_visitor *original, int samplerIndex)
3892 {
3893 glsl_to_tgsi_visitor *v = new glsl_to_tgsi_visitor();
3894 struct st_context *st = st_context(original->ctx);
3895 struct gl_program *prog = &fp->Base.Base;
3896 st_src_reg coord, src0;
3897 st_dst_reg dst0;
3898 glsl_to_tgsi_instruction *inst;
3899
3900 /* Copy attributes of the glsl_to_tgsi_visitor in the original shader. */
3901 v->ctx = original->ctx;
3902 v->prog = prog;
3903 v->glsl_version = original->glsl_version;
3904 v->native_integers = original->native_integers;
3905 v->options = original->options;
3906 v->next_temp = original->next_temp;
3907 v->num_address_regs = original->num_address_regs;
3908 v->samplers_used = prog->SamplersUsed = original->samplers_used;
3909 v->indirect_addr_temps = original->indirect_addr_temps;
3910 v->indirect_addr_consts = original->indirect_addr_consts;
3911 memcpy(&v->immediates, &original->immediates, sizeof(v->immediates));
3912
3913 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
3914 coord = st_src_reg(PROGRAM_INPUT, FRAG_ATTRIB_TEX0, glsl_type::vec2_type);
3915 src0 = v->get_temp(glsl_type::vec4_type);
3916 dst0 = st_dst_reg(src0);
3917 inst = v->emit(NULL, TGSI_OPCODE_TEX, dst0, coord);
3918 inst->sampler = samplerIndex;
3919 inst->tex_target = TEXTURE_2D_INDEX;
3920
3921 prog->InputsRead |= (1 << FRAG_ATTRIB_TEX0);
3922 prog->SamplersUsed |= (1 << samplerIndex); /* mark sampler as used */
3923 v->samplers_used |= (1 << samplerIndex);
3924
3925 /* KIL if -tmp0 < 0 # texel=0 -> keep / texel=0 -> discard */
3926 src0.negate = NEGATE_XYZW;
3927 if (st->bitmap.tex_format == PIPE_FORMAT_L8_UNORM)
3928 src0.swizzle = SWIZZLE_XXXX;
3929 inst = v->emit(NULL, TGSI_OPCODE_KIL, undef_dst, src0);
3930
3931 /* Now copy the instructions from the original glsl_to_tgsi_visitor into the
3932 * new visitor. */
3933 foreach_iter(exec_list_iterator, iter, original->instructions) {
3934 glsl_to_tgsi_instruction *inst = (glsl_to_tgsi_instruction *)iter.get();
3935 st_src_reg src_regs[3];
3936
3937 if (inst->dst.file == PROGRAM_OUTPUT)
3938 prog->OutputsWritten |= BITFIELD64_BIT(inst->dst.index);
3939
3940 for (int i=0; i<3; i++) {
3941 src_regs[i] = inst->src[i];
3942 if (src_regs[i].file == PROGRAM_INPUT)
3943 prog->InputsRead |= (1 << src_regs[i].index);
3944 }
3945
3946 v->emit(NULL, inst->op, inst->dst, src_regs[0], src_regs[1], src_regs[2]);
3947 }
3948
3949 /* Make modifications to fragment program info. */
3950 prog->Parameters = _mesa_clone_parameter_list(original->prog->Parameters);
3951 prog->Attributes = _mesa_clone_parameter_list(original->prog->Attributes);
3952 prog->Varying = _mesa_clone_parameter_list(original->prog->Varying);
3953 count_resources(v, prog);
3954 fp->glsl_to_tgsi = v;
3955 }
3956
3957 /* ------------------------- TGSI conversion stuff -------------------------- */
3958 struct label {
3959 unsigned branch_target;
3960 unsigned token;
3961 };
3962
3963 /**
3964 * Intermediate state used during shader translation.
3965 */
3966 struct st_translate {
3967 struct ureg_program *ureg;
3968
3969 struct ureg_dst temps[MAX_TEMPS];
3970 struct ureg_src *constants;
3971 struct ureg_src *immediates;
3972 struct ureg_dst outputs[PIPE_MAX_SHADER_OUTPUTS];
3973 struct ureg_src inputs[PIPE_MAX_SHADER_INPUTS];
3974 struct ureg_dst address[1];
3975 struct ureg_src samplers[PIPE_MAX_SAMPLERS];
3976 struct ureg_src systemValues[SYSTEM_VALUE_MAX];
3977
3978 /* Extra info for handling point size clamping in vertex shader */
3979 struct ureg_dst pointSizeResult; /**< Actual point size output register */
3980 struct ureg_src pointSizeConst; /**< Point size range constant register */
3981 GLint pointSizeOutIndex; /**< Temp point size output register */
3982 GLboolean prevInstWrotePointSize;
3983
3984 const GLuint *inputMapping;
3985 const GLuint *outputMapping;
3986
3987 /* For every instruction that contains a label (eg CALL), keep
3988 * details so that we can go back afterwards and emit the correct
3989 * tgsi instruction number for each label.
3990 */
3991 struct label *labels;
3992 unsigned labels_size;
3993 unsigned labels_count;
3994
3995 /* Keep a record of the tgsi instruction number that each mesa
3996 * instruction starts at, will be used to fix up labels after
3997 * translation.
3998 */
3999 unsigned *insn;
4000 unsigned insn_size;
4001 unsigned insn_count;
4002
4003 unsigned procType; /**< TGSI_PROCESSOR_VERTEX/FRAGMENT */
4004
4005 boolean error;
4006 };
4007
4008 /** Map Mesa's SYSTEM_VALUE_x to TGSI_SEMANTIC_x */
4009 static unsigned mesa_sysval_to_semantic[SYSTEM_VALUE_MAX] = {
4010 TGSI_SEMANTIC_FACE,
4011 TGSI_SEMANTIC_INSTANCEID
4012 };
4013
4014 /**
4015 * Make note of a branch to a label in the TGSI code.
4016 * After we've emitted all instructions, we'll go over the list
4017 * of labels built here and patch the TGSI code with the actual
4018 * location of each label.
4019 */
4020 static unsigned *get_label(struct st_translate *t, unsigned branch_target)
4021 {
4022 unsigned i;
4023
4024 if (t->labels_count + 1 >= t->labels_size) {
4025 t->labels_size = 1 << (util_logbase2(t->labels_size) + 1);
4026 t->labels = (struct label *)realloc(t->labels,
4027 t->labels_size * sizeof(struct label));
4028 if (t->labels == NULL) {
4029 static unsigned dummy;
4030 t->error = TRUE;
4031 return &dummy;
4032 }
4033 }
4034
4035 i = t->labels_count++;
4036 t->labels[i].branch_target = branch_target;
4037 return &t->labels[i].token;
4038 }
4039
4040 /**
4041 * Called prior to emitting the TGSI code for each instruction.
4042 * Allocate additional space for instructions if needed.
4043 * Update the insn[] array so the next glsl_to_tgsi_instruction points to
4044 * the next TGSI instruction.
4045 */
4046 static void set_insn_start(struct st_translate *t, unsigned start)
4047 {
4048 if (t->insn_count + 1 >= t->insn_size) {
4049 t->insn_size = 1 << (util_logbase2(t->insn_size) + 1);
4050 t->insn = (unsigned *)realloc(t->insn, t->insn_size * sizeof(t->insn[0]));
4051 if (t->insn == NULL) {
4052 t->error = TRUE;
4053 return;
4054 }
4055 }
4056
4057 t->insn[t->insn_count++] = start;
4058 }
4059
4060 /**
4061 * Map a glsl_to_tgsi constant/immediate to a TGSI immediate.
4062 */
4063 static struct ureg_src
4064 emit_immediate(struct st_translate *t,
4065 gl_constant_value values[4],
4066 int type, int size)
4067 {
4068 struct ureg_program *ureg = t->ureg;
4069
4070 switch(type)
4071 {
4072 case GL_FLOAT:
4073 return ureg_DECL_immediate(ureg, &values[0].f, size);
4074 case GL_INT:
4075 return ureg_DECL_immediate_int(ureg, &values[0].i, size);
4076 case GL_UNSIGNED_INT:
4077 case GL_BOOL:
4078 return ureg_DECL_immediate_uint(ureg, &values[0].u, size);
4079 default:
4080 assert(!"should not get here - type must be float, int, uint, or bool");
4081 return ureg_src_undef();
4082 }
4083 }
4084
4085 /**
4086 * Map a glsl_to_tgsi dst register to a TGSI ureg_dst register.
4087 */
4088 static struct ureg_dst
4089 dst_register(struct st_translate *t,
4090 gl_register_file file,
4091 GLuint index)
4092 {
4093 switch(file) {
4094 case PROGRAM_UNDEFINED:
4095 return ureg_dst_undef();
4096
4097 case PROGRAM_TEMPORARY:
4098 if (ureg_dst_is_undef(t->temps[index]))
4099 t->temps[index] = ureg_DECL_temporary(t->ureg);
4100
4101 return t->temps[index];
4102
4103 case PROGRAM_OUTPUT:
4104 if (t->procType == TGSI_PROCESSOR_VERTEX && index == VERT_RESULT_PSIZ)
4105 t->prevInstWrotePointSize = GL_TRUE;
4106
4107 if (t->procType == TGSI_PROCESSOR_VERTEX)
4108 assert(index < VERT_RESULT_MAX);
4109 else if (t->procType == TGSI_PROCESSOR_FRAGMENT)
4110 assert(index < FRAG_RESULT_MAX);
4111 else
4112 assert(index < GEOM_RESULT_MAX);
4113
4114 assert(t->outputMapping[index] < Elements(t->outputs));
4115
4116 return t->outputs[t->outputMapping[index]];
4117
4118 case PROGRAM_ADDRESS:
4119 return t->address[index];
4120
4121 default:
4122 assert(!"unknown dst register file");
4123 return ureg_dst_undef();
4124 }
4125 }
4126
4127 /**
4128 * Map a glsl_to_tgsi src register to a TGSI ureg_src register.
4129 */
4130 static struct ureg_src
4131 src_register(struct st_translate *t,
4132 gl_register_file file,
4133 GLuint index)
4134 {
4135 switch(file) {
4136 case PROGRAM_UNDEFINED:
4137 return ureg_src_undef();
4138
4139 case PROGRAM_TEMPORARY:
4140 assert(index >= 0);
4141 assert(index < Elements(t->temps));
4142 if (ureg_dst_is_undef(t->temps[index]))
4143 t->temps[index] = ureg_DECL_temporary(t->ureg);
4144 return ureg_src(t->temps[index]);
4145
4146 case PROGRAM_NAMED_PARAM:
4147 case PROGRAM_ENV_PARAM:
4148 case PROGRAM_LOCAL_PARAM:
4149 case PROGRAM_UNIFORM:
4150 assert(index >= 0);
4151 return t->constants[index];
4152 case PROGRAM_STATE_VAR:
4153 case PROGRAM_CONSTANT: /* ie, immediate */
4154 if (index < 0)
4155 return ureg_DECL_constant(t->ureg, 0);
4156 else
4157 return t->constants[index];
4158
4159 case PROGRAM_IMMEDIATE:
4160 return t->immediates[index];
4161
4162 case PROGRAM_INPUT:
4163 assert(t->inputMapping[index] < Elements(t->inputs));
4164 return t->inputs[t->inputMapping[index]];
4165
4166 case PROGRAM_OUTPUT:
4167 assert(t->outputMapping[index] < Elements(t->outputs));
4168 return ureg_src(t->outputs[t->outputMapping[index]]); /* not needed? */
4169
4170 case PROGRAM_ADDRESS:
4171 return ureg_src(t->address[index]);
4172
4173 case PROGRAM_SYSTEM_VALUE:
4174 assert(index < Elements(t->systemValues));
4175 return t->systemValues[index];
4176
4177 default:
4178 assert(!"unknown src register file");
4179 return ureg_src_undef();
4180 }
4181 }
4182
4183 /**
4184 * Create a TGSI ureg_dst register from an st_dst_reg.
4185 */
4186 static struct ureg_dst
4187 translate_dst(struct st_translate *t,
4188 const st_dst_reg *dst_reg,
4189 bool saturate)
4190 {
4191 struct ureg_dst dst = dst_register(t,
4192 dst_reg->file,
4193 dst_reg->index);
4194
4195 dst = ureg_writemask(dst, dst_reg->writemask);
4196
4197 if (saturate)
4198 dst = ureg_saturate(dst);
4199
4200 if (dst_reg->reladdr != NULL)
4201 dst = ureg_dst_indirect(dst, ureg_src(t->address[0]));
4202
4203 return dst;
4204 }
4205
4206 /**
4207 * Create a TGSI ureg_src register from an st_src_reg.
4208 */
4209 static struct ureg_src
4210 translate_src(struct st_translate *t, const st_src_reg *src_reg)
4211 {
4212 struct ureg_src src = src_register(t, src_reg->file, src_reg->index);
4213
4214 src = ureg_swizzle(src,
4215 GET_SWZ(src_reg->swizzle, 0) & 0x3,
4216 GET_SWZ(src_reg->swizzle, 1) & 0x3,
4217 GET_SWZ(src_reg->swizzle, 2) & 0x3,
4218 GET_SWZ(src_reg->swizzle, 3) & 0x3);
4219
4220 if ((src_reg->negate & 0xf) == NEGATE_XYZW)
4221 src = ureg_negate(src);
4222
4223 if (src_reg->reladdr != NULL) {
4224 /* Normally ureg_src_indirect() would be used here, but a stupid compiler
4225 * bug in g++ makes ureg_src_indirect (an inline C function) erroneously
4226 * set the bit for src.Negate. So we have to do the operation manually
4227 * here to work around the compiler's problems. */
4228 /*src = ureg_src_indirect(src, ureg_src(t->address[0]));*/
4229 struct ureg_src addr = ureg_src(t->address[0]);
4230 src.Indirect = 1;
4231 src.IndirectFile = addr.File;
4232 src.IndirectIndex = addr.Index;
4233 src.IndirectSwizzle = addr.SwizzleX;
4234
4235 if (src_reg->file != PROGRAM_INPUT &&
4236 src_reg->file != PROGRAM_OUTPUT) {
4237 /* If src_reg->index was negative, it was set to zero in
4238 * src_register(). Reassign it now. But don't do this
4239 * for input/output regs since they get remapped while
4240 * const buffers don't.
4241 */
4242 src.Index = src_reg->index;
4243 }
4244 }
4245
4246 return src;
4247 }
4248
4249 static void
4250 compile_tgsi_instruction(struct st_translate *t,
4251 const glsl_to_tgsi_instruction *inst)
4252 {
4253 struct ureg_program *ureg = t->ureg;
4254 GLuint i;
4255 struct ureg_dst dst[1];
4256 struct ureg_src src[4];
4257 unsigned num_dst;
4258 unsigned num_src;
4259
4260 num_dst = num_inst_dst_regs(inst->op);
4261 num_src = num_inst_src_regs(inst->op);
4262
4263 if (num_dst)
4264 dst[0] = translate_dst(t,
4265 &inst->dst,
4266 inst->saturate);
4267
4268 for (i = 0; i < num_src; i++)
4269 src[i] = translate_src(t, &inst->src[i]);
4270
4271 switch(inst->op) {
4272 case TGSI_OPCODE_BGNLOOP:
4273 case TGSI_OPCODE_CAL:
4274 case TGSI_OPCODE_ELSE:
4275 case TGSI_OPCODE_ENDLOOP:
4276 case TGSI_OPCODE_IF:
4277 assert(num_dst == 0);
4278 ureg_label_insn(ureg,
4279 inst->op,
4280 src, num_src,
4281 get_label(t,
4282 inst->op == TGSI_OPCODE_CAL ? inst->function->sig_id : 0));
4283 return;
4284
4285 case TGSI_OPCODE_TEX:
4286 case TGSI_OPCODE_TXB:
4287 case TGSI_OPCODE_TXD:
4288 case TGSI_OPCODE_TXL:
4289 case TGSI_OPCODE_TXP:
4290 case TGSI_OPCODE_TXQ:
4291 case TGSI_OPCODE_TXF:
4292 src[num_src++] = t->samplers[inst->sampler];
4293 ureg_tex_insn(ureg,
4294 inst->op,
4295 dst, num_dst,
4296 translate_texture_target(inst->tex_target, inst->tex_shadow),
4297 src, num_src);
4298 return;
4299
4300 case TGSI_OPCODE_SCS:
4301 dst[0] = ureg_writemask(dst[0], TGSI_WRITEMASK_XY);
4302 ureg_insn(ureg, inst->op, dst, num_dst, src, num_src);
4303 break;
4304
4305 default:
4306 ureg_insn(ureg,
4307 inst->op,
4308 dst, num_dst,
4309 src, num_src);
4310 break;
4311 }
4312 }
4313
4314 /**
4315 * Emit the TGSI instructions to adjust the WPOS pixel center convention
4316 * Basically, add (adjX, adjY) to the fragment position.
4317 */
4318 static void
4319 emit_adjusted_wpos(struct st_translate *t,
4320 const struct gl_program *program,
4321 float adjX, float adjY)
4322 {
4323 struct ureg_program *ureg = t->ureg;
4324 struct ureg_dst wpos_temp = ureg_DECL_temporary(ureg);
4325 struct ureg_src wpos_input = t->inputs[t->inputMapping[FRAG_ATTRIB_WPOS]];
4326
4327 /* Note that we bias X and Y and pass Z and W through unchanged.
4328 * The shader might also use gl_FragCoord.w and .z.
4329 */
4330 ureg_ADD(ureg, wpos_temp, wpos_input,
4331 ureg_imm4f(ureg, adjX, adjY, 0.0f, 0.0f));
4332
4333 t->inputs[t->inputMapping[FRAG_ATTRIB_WPOS]] = ureg_src(wpos_temp);
4334 }
4335
4336
4337 /**
4338 * Emit the TGSI instructions for inverting the WPOS y coordinate.
4339 * This code is unavoidable because it also depends on whether
4340 * a FBO is bound (STATE_FB_WPOS_Y_TRANSFORM).
4341 */
4342 static void
4343 emit_wpos_inversion(struct st_translate *t,
4344 const struct gl_program *program,
4345 bool invert)
4346 {
4347 struct ureg_program *ureg = t->ureg;
4348
4349 /* Fragment program uses fragment position input.
4350 * Need to replace instances of INPUT[WPOS] with temp T
4351 * where T = INPUT[WPOS] by y is inverted.
4352 */
4353 static const gl_state_index wposTransformState[STATE_LENGTH]
4354 = { STATE_INTERNAL, STATE_FB_WPOS_Y_TRANSFORM,
4355 (gl_state_index)0, (gl_state_index)0, (gl_state_index)0 };
4356
4357 /* XXX: note we are modifying the incoming shader here! Need to
4358 * do this before emitting the constant decls below, or this
4359 * will be missed:
4360 */
4361 unsigned wposTransConst = _mesa_add_state_reference(program->Parameters,
4362 wposTransformState);
4363
4364 struct ureg_src wpostrans = ureg_DECL_constant(ureg, wposTransConst);
4365 struct ureg_dst wpos_temp;
4366 struct ureg_src wpos_input = t->inputs[t->inputMapping[FRAG_ATTRIB_WPOS]];
4367
4368 /* MOV wpos_temp, input[wpos]
4369 */
4370 if (wpos_input.File == TGSI_FILE_TEMPORARY)
4371 wpos_temp = ureg_dst(wpos_input);
4372 else {
4373 wpos_temp = ureg_DECL_temporary(ureg);
4374 ureg_MOV(ureg, wpos_temp, wpos_input);
4375 }
4376
4377 if (invert) {
4378 /* MAD wpos_temp.y, wpos_input, wpostrans.xxxx, wpostrans.yyyy
4379 */
4380 ureg_MAD(ureg,
4381 ureg_writemask(wpos_temp, TGSI_WRITEMASK_Y),
4382 wpos_input,
4383 ureg_scalar(wpostrans, 0),
4384 ureg_scalar(wpostrans, 1));
4385 } else {
4386 /* MAD wpos_temp.y, wpos_input, wpostrans.zzzz, wpostrans.wwww
4387 */
4388 ureg_MAD(ureg,
4389 ureg_writemask(wpos_temp, TGSI_WRITEMASK_Y),
4390 wpos_input,
4391 ureg_scalar(wpostrans, 2),
4392 ureg_scalar(wpostrans, 3));
4393 }
4394
4395 /* Use wpos_temp as position input from here on:
4396 */
4397 t->inputs[t->inputMapping[FRAG_ATTRIB_WPOS]] = ureg_src(wpos_temp);
4398 }
4399
4400
4401 /**
4402 * Emit fragment position/ooordinate code.
4403 */
4404 static void
4405 emit_wpos(struct st_context *st,
4406 struct st_translate *t,
4407 const struct gl_program *program,
4408 struct ureg_program *ureg)
4409 {
4410 const struct gl_fragment_program *fp =
4411 (const struct gl_fragment_program *) program;
4412 struct pipe_screen *pscreen = st->pipe->screen;
4413 boolean invert = FALSE;
4414
4415 if (fp->OriginUpperLeft) {
4416 /* Fragment shader wants origin in upper-left */
4417 if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT)) {
4418 /* the driver supports upper-left origin */
4419 }
4420 else if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT)) {
4421 /* the driver supports lower-left origin, need to invert Y */
4422 ureg_property_fs_coord_origin(ureg, TGSI_FS_COORD_ORIGIN_LOWER_LEFT);
4423 invert = TRUE;
4424 }
4425 else
4426 assert(0);
4427 }
4428 else {
4429 /* Fragment shader wants origin in lower-left */
4430 if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT))
4431 /* the driver supports lower-left origin */
4432 ureg_property_fs_coord_origin(ureg, TGSI_FS_COORD_ORIGIN_LOWER_LEFT);
4433 else if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT))
4434 /* the driver supports upper-left origin, need to invert Y */
4435 invert = TRUE;
4436 else
4437 assert(0);
4438 }
4439
4440 if (fp->PixelCenterInteger) {
4441 /* Fragment shader wants pixel center integer */
4442 if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER))
4443 /* the driver supports pixel center integer */
4444 ureg_property_fs_coord_pixel_center(ureg, TGSI_FS_COORD_PIXEL_CENTER_INTEGER);
4445 else if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER))
4446 /* the driver supports pixel center half integer, need to bias X,Y */
4447 emit_adjusted_wpos(t, program, 0.5f, invert ? 0.5f : -0.5f);
4448 else
4449 assert(0);
4450 }
4451 else {
4452 /* Fragment shader wants pixel center half integer */
4453 if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER)) {
4454 /* the driver supports pixel center half integer */
4455 }
4456 else if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER)) {
4457 /* the driver supports pixel center integer, need to bias X,Y */
4458 ureg_property_fs_coord_pixel_center(ureg, TGSI_FS_COORD_PIXEL_CENTER_INTEGER);
4459 emit_adjusted_wpos(t, program, 0.5f, invert ? -0.5f : 0.5f);
4460 }
4461 else
4462 assert(0);
4463 }
4464
4465 /* we invert after adjustment so that we avoid the MOV to temporary,
4466 * and reuse the adjustment ADD instead */
4467 emit_wpos_inversion(t, program, invert);
4468 }
4469
4470 /**
4471 * OpenGL's fragment gl_FrontFace input is 1 for front-facing, 0 for back.
4472 * TGSI uses +1 for front, -1 for back.
4473 * This function converts the TGSI value to the GL value. Simply clamping/
4474 * saturating the value to [0,1] does the job.
4475 */
4476 static void
4477 emit_face_var(struct st_translate *t)
4478 {
4479 struct ureg_program *ureg = t->ureg;
4480 struct ureg_dst face_temp = ureg_DECL_temporary(ureg);
4481 struct ureg_src face_input = t->inputs[t->inputMapping[FRAG_ATTRIB_FACE]];
4482
4483 /* MOV_SAT face_temp, input[face] */
4484 face_temp = ureg_saturate(face_temp);
4485 ureg_MOV(ureg, face_temp, face_input);
4486
4487 /* Use face_temp as face input from here on: */
4488 t->inputs[t->inputMapping[FRAG_ATTRIB_FACE]] = ureg_src(face_temp);
4489 }
4490
4491 static void
4492 emit_edgeflags(struct st_translate *t)
4493 {
4494 struct ureg_program *ureg = t->ureg;
4495 struct ureg_dst edge_dst = t->outputs[t->outputMapping[VERT_RESULT_EDGE]];
4496 struct ureg_src edge_src = t->inputs[t->inputMapping[VERT_ATTRIB_EDGEFLAG]];
4497
4498 ureg_MOV(ureg, edge_dst, edge_src);
4499 }
4500
4501 /**
4502 * Translate intermediate IR (glsl_to_tgsi_instruction) to TGSI format.
4503 * \param program the program to translate
4504 * \param numInputs number of input registers used
4505 * \param inputMapping maps Mesa fragment program inputs to TGSI generic
4506 * input indexes
4507 * \param inputSemanticName the TGSI_SEMANTIC flag for each input
4508 * \param inputSemanticIndex the semantic index (ex: which texcoord) for
4509 * each input
4510 * \param interpMode the TGSI_INTERPOLATE_LINEAR/PERSP mode for each input
4511 * \param numOutputs number of output registers used
4512 * \param outputMapping maps Mesa fragment program outputs to TGSI
4513 * generic outputs
4514 * \param outputSemanticName the TGSI_SEMANTIC flag for each output
4515 * \param outputSemanticIndex the semantic index (ex: which texcoord) for
4516 * each output
4517 *
4518 * \return PIPE_OK or PIPE_ERROR_OUT_OF_MEMORY
4519 */
4520 extern "C" enum pipe_error
4521 st_translate_program(
4522 struct gl_context *ctx,
4523 uint procType,
4524 struct ureg_program *ureg,
4525 glsl_to_tgsi_visitor *program,
4526 const struct gl_program *proginfo,
4527 GLuint numInputs,
4528 const GLuint inputMapping[],
4529 const ubyte inputSemanticName[],
4530 const ubyte inputSemanticIndex[],
4531 const GLuint interpMode[],
4532 GLuint numOutputs,
4533 const GLuint outputMapping[],
4534 const ubyte outputSemanticName[],
4535 const ubyte outputSemanticIndex[],
4536 boolean passthrough_edgeflags)
4537 {
4538 struct st_translate translate, *t;
4539 unsigned i;
4540 enum pipe_error ret = PIPE_OK;
4541
4542 assert(numInputs <= Elements(t->inputs));
4543 assert(numOutputs <= Elements(t->outputs));
4544
4545 t = &translate;
4546 memset(t, 0, sizeof *t);
4547
4548 t->procType = procType;
4549 t->inputMapping = inputMapping;
4550 t->outputMapping = outputMapping;
4551 t->ureg = ureg;
4552 t->pointSizeOutIndex = -1;
4553 t->prevInstWrotePointSize = GL_FALSE;
4554
4555 /*
4556 * Declare input attributes.
4557 */
4558 if (procType == TGSI_PROCESSOR_FRAGMENT) {
4559 for (i = 0; i < numInputs; i++) {
4560 t->inputs[i] = ureg_DECL_fs_input(ureg,
4561 inputSemanticName[i],
4562 inputSemanticIndex[i],
4563 interpMode[i]);
4564 }
4565
4566 if (proginfo->InputsRead & FRAG_BIT_WPOS) {
4567 /* Must do this after setting up t->inputs, and before
4568 * emitting constant references, below:
4569 */
4570 emit_wpos(st_context(ctx), t, proginfo, ureg);
4571 }
4572
4573 if (proginfo->InputsRead & FRAG_BIT_FACE)
4574 emit_face_var(t);
4575
4576 /*
4577 * Declare output attributes.
4578 */
4579 for (i = 0; i < numOutputs; i++) {
4580 switch (outputSemanticName[i]) {
4581 case TGSI_SEMANTIC_POSITION:
4582 t->outputs[i] = ureg_DECL_output(ureg,
4583 TGSI_SEMANTIC_POSITION, /* Z/Depth */
4584 outputSemanticIndex[i]);
4585 t->outputs[i] = ureg_writemask(t->outputs[i], TGSI_WRITEMASK_Z);
4586 break;
4587 case TGSI_SEMANTIC_STENCIL:
4588 t->outputs[i] = ureg_DECL_output(ureg,
4589 TGSI_SEMANTIC_STENCIL, /* Stencil */
4590 outputSemanticIndex[i]);
4591 t->outputs[i] = ureg_writemask(t->outputs[i], TGSI_WRITEMASK_Y);
4592 break;
4593 case TGSI_SEMANTIC_COLOR:
4594 t->outputs[i] = ureg_DECL_output(ureg,
4595 TGSI_SEMANTIC_COLOR,
4596 outputSemanticIndex[i]);
4597 break;
4598 default:
4599 assert(!"fragment shader outputs must be POSITION/STENCIL/COLOR");
4600 return PIPE_ERROR_BAD_INPUT;
4601 }
4602 }
4603 }
4604 else if (procType == TGSI_PROCESSOR_GEOMETRY) {
4605 for (i = 0; i < numInputs; i++) {
4606 t->inputs[i] = ureg_DECL_gs_input(ureg,
4607 i,
4608 inputSemanticName[i],
4609 inputSemanticIndex[i]);
4610 }
4611
4612 for (i = 0; i < numOutputs; i++) {
4613 t->outputs[i] = ureg_DECL_output(ureg,
4614 outputSemanticName[i],
4615 outputSemanticIndex[i]);
4616 }
4617 }
4618 else {
4619 assert(procType == TGSI_PROCESSOR_VERTEX);
4620
4621 for (i = 0; i < numInputs; i++) {
4622 t->inputs[i] = ureg_DECL_vs_input(ureg, i);
4623 }
4624
4625 for (i = 0; i < numOutputs; i++) {
4626 t->outputs[i] = ureg_DECL_output(ureg,
4627 outputSemanticName[i],
4628 outputSemanticIndex[i]);
4629 if ((outputSemanticName[i] == TGSI_SEMANTIC_PSIZE) && proginfo->Id) {
4630 /* Writing to the point size result register requires special
4631 * handling to implement clamping.
4632 */
4633 static const gl_state_index pointSizeClampState[STATE_LENGTH]
4634 = { STATE_INTERNAL, STATE_POINT_SIZE_IMPL_CLAMP, (gl_state_index)0, (gl_state_index)0, (gl_state_index)0 };
4635 /* XXX: note we are modifying the incoming shader here! Need to
4636 * do this before emitting the constant decls below, or this
4637 * will be missed.
4638 */
4639 unsigned pointSizeClampConst =
4640 _mesa_add_state_reference(proginfo->Parameters,
4641 pointSizeClampState);
4642 struct ureg_dst psizregtemp = ureg_DECL_temporary(ureg);
4643 t->pointSizeConst = ureg_DECL_constant(ureg, pointSizeClampConst);
4644 t->pointSizeResult = t->outputs[i];
4645 t->pointSizeOutIndex = i;
4646 t->outputs[i] = psizregtemp;
4647 }
4648 }
4649 if (passthrough_edgeflags)
4650 emit_edgeflags(t);
4651 }
4652
4653 /* Declare address register.
4654 */
4655 if (program->num_address_regs > 0) {
4656 assert(program->num_address_regs == 1);
4657 t->address[0] = ureg_DECL_address(ureg);
4658 }
4659
4660 /* Declare misc input registers
4661 */
4662 {
4663 GLbitfield sysInputs = proginfo->SystemValuesRead;
4664 unsigned numSys = 0;
4665 for (i = 0; sysInputs; i++) {
4666 if (sysInputs & (1 << i)) {
4667 unsigned semName = mesa_sysval_to_semantic[i];
4668 t->systemValues[i] = ureg_DECL_system_value(ureg, numSys, semName, 0);
4669 numSys++;
4670 sysInputs &= ~(1 << i);
4671 }
4672 }
4673 }
4674
4675 if (program->indirect_addr_temps) {
4676 /* If temps are accessed with indirect addressing, declare temporaries
4677 * in sequential order. Else, we declare them on demand elsewhere.
4678 * (Note: the number of temporaries is equal to program->next_temp)
4679 */
4680 for (i = 0; i < (unsigned)program->next_temp; i++) {
4681 /* XXX use TGSI_FILE_TEMPORARY_ARRAY when it's supported by ureg */
4682 t->temps[i] = ureg_DECL_temporary(t->ureg);
4683 }
4684 }
4685
4686 /* Emit constants and uniforms. TGSI uses a single index space for these,
4687 * so we put all the translated regs in t->constants.
4688 */
4689 if (proginfo->Parameters) {
4690 t->constants = (struct ureg_src *)CALLOC(proginfo->Parameters->NumParameters * sizeof(t->constants[0]));
4691 if (t->constants == NULL) {
4692 ret = PIPE_ERROR_OUT_OF_MEMORY;
4693 goto out;
4694 }
4695
4696 for (i = 0; i < proginfo->Parameters->NumParameters; i++) {
4697 switch (proginfo->Parameters->Parameters[i].Type) {
4698 case PROGRAM_ENV_PARAM:
4699 case PROGRAM_LOCAL_PARAM:
4700 case PROGRAM_STATE_VAR:
4701 case PROGRAM_NAMED_PARAM:
4702 case PROGRAM_UNIFORM:
4703 t->constants[i] = ureg_DECL_constant(ureg, i);
4704 break;
4705
4706 /* Emit immediates for PROGRAM_CONSTANT only when there's no indirect
4707 * addressing of the const buffer.
4708 * FIXME: Be smarter and recognize param arrays:
4709 * indirect addressing is only valid within the referenced
4710 * array.
4711 */
4712 case PROGRAM_CONSTANT:
4713 if (program->indirect_addr_consts)
4714 t->constants[i] = ureg_DECL_constant(ureg, i);
4715 else
4716 t->constants[i] = emit_immediate(t,
4717 proginfo->Parameters->ParameterValues[i],
4718 proginfo->Parameters->Parameters[i].DataType,
4719 4);
4720 break;
4721 default:
4722 break;
4723 }
4724 }
4725 }
4726
4727 /* Emit immediate values.
4728 */
4729 t->immediates = (struct ureg_src *)CALLOC(program->num_immediates * sizeof(struct ureg_src));
4730 if (t->immediates == NULL) {
4731 ret = PIPE_ERROR_OUT_OF_MEMORY;
4732 goto out;
4733 }
4734 i = 0;
4735 foreach_iter(exec_list_iterator, iter, program->immediates) {
4736 immediate_storage *imm = (immediate_storage *)iter.get();
4737 t->immediates[i++] = emit_immediate(t, imm->values, imm->type, imm->size);
4738 }
4739
4740 /* texture samplers */
4741 for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
4742 if (program->samplers_used & (1 << i)) {
4743 t->samplers[i] = ureg_DECL_sampler(ureg, i);
4744 }
4745 }
4746
4747 /* Emit each instruction in turn:
4748 */
4749 foreach_iter(exec_list_iterator, iter, program->instructions) {
4750 set_insn_start(t, ureg_get_instruction_number(ureg));
4751 compile_tgsi_instruction(t, (glsl_to_tgsi_instruction *)iter.get());
4752
4753 if (t->prevInstWrotePointSize && proginfo->Id) {
4754 /* The previous instruction wrote to the (fake) vertex point size
4755 * result register. Now we need to clamp that value to the min/max
4756 * point size range, putting the result into the real point size
4757 * register.
4758 * Note that we can't do this easily at the end of program due to
4759 * possible early return.
4760 */
4761 set_insn_start(t, ureg_get_instruction_number(ureg));
4762 ureg_MAX(t->ureg,
4763 ureg_writemask(t->outputs[t->pointSizeOutIndex], WRITEMASK_X),
4764 ureg_src(t->outputs[t->pointSizeOutIndex]),
4765 ureg_swizzle(t->pointSizeConst, 1,1,1,1));
4766 ureg_MIN(t->ureg, ureg_writemask(t->pointSizeResult, WRITEMASK_X),
4767 ureg_src(t->outputs[t->pointSizeOutIndex]),
4768 ureg_swizzle(t->pointSizeConst, 2,2,2,2));
4769 }
4770 t->prevInstWrotePointSize = GL_FALSE;
4771 }
4772
4773 /* Fix up all emitted labels:
4774 */
4775 for (i = 0; i < t->labels_count; i++) {
4776 ureg_fixup_label(ureg, t->labels[i].token,
4777 t->insn[t->labels[i].branch_target]);
4778 }
4779
4780 out:
4781 FREE(t->insn);
4782 FREE(t->labels);
4783 FREE(t->constants);
4784 FREE(t->immediates);
4785
4786 if (t->error) {
4787 debug_printf("%s: translate error flag set\n", __FUNCTION__);
4788 }
4789
4790 return ret;
4791 }
4792 /* ----------------------------- End TGSI code ------------------------------ */
4793
4794 /**
4795 * Convert a shader's GLSL IR into a Mesa gl_program, although without
4796 * generating Mesa IR.
4797 */
4798 static struct gl_program *
4799 get_mesa_program(struct gl_context *ctx,
4800 struct gl_shader_program *shader_program,
4801 struct gl_shader *shader)
4802 {
4803 glsl_to_tgsi_visitor* v = new glsl_to_tgsi_visitor();
4804 struct gl_program *prog;
4805 GLenum target;
4806 const char *target_string;
4807 bool progress;
4808 struct gl_shader_compiler_options *options =
4809 &ctx->ShaderCompilerOptions[_mesa_shader_type_to_index(shader->Type)];
4810
4811 switch (shader->Type) {
4812 case GL_VERTEX_SHADER:
4813 target = GL_VERTEX_PROGRAM_ARB;
4814 target_string = "vertex";
4815 break;
4816 case GL_FRAGMENT_SHADER:
4817 target = GL_FRAGMENT_PROGRAM_ARB;
4818 target_string = "fragment";
4819 break;
4820 case GL_GEOMETRY_SHADER:
4821 target = GL_GEOMETRY_PROGRAM_NV;
4822 target_string = "geometry";
4823 break;
4824 default:
4825 assert(!"should not be reached");
4826 return NULL;
4827 }
4828
4829 validate_ir_tree(shader->ir);
4830
4831 prog = ctx->Driver.NewProgram(ctx, target, shader_program->Name);
4832 if (!prog)
4833 return NULL;
4834 prog->Parameters = _mesa_new_parameter_list();
4835 prog->Varying = _mesa_new_parameter_list();
4836 prog->Attributes = _mesa_new_parameter_list();
4837 v->ctx = ctx;
4838 v->prog = prog;
4839 v->shader_program = shader_program;
4840 v->options = options;
4841 v->glsl_version = ctx->Const.GLSLVersion;
4842 v->native_integers = ctx->Const.NativeIntegers;
4843
4844 add_uniforms_to_parameters_list(shader_program, shader, prog);
4845
4846 /* Emit intermediate IR for main(). */
4847 visit_exec_list(shader->ir, v);
4848
4849 /* Now emit bodies for any functions that were used. */
4850 do {
4851 progress = GL_FALSE;
4852
4853 foreach_iter(exec_list_iterator, iter, v->function_signatures) {
4854 function_entry *entry = (function_entry *)iter.get();
4855
4856 if (!entry->bgn_inst) {
4857 v->current_function = entry;
4858
4859 entry->bgn_inst = v->emit(NULL, TGSI_OPCODE_BGNSUB);
4860 entry->bgn_inst->function = entry;
4861
4862 visit_exec_list(&entry->sig->body, v);
4863
4864 glsl_to_tgsi_instruction *last;
4865 last = (glsl_to_tgsi_instruction *)v->instructions.get_tail();
4866 if (last->op != TGSI_OPCODE_RET)
4867 v->emit(NULL, TGSI_OPCODE_RET);
4868
4869 glsl_to_tgsi_instruction *end;
4870 end = v->emit(NULL, TGSI_OPCODE_ENDSUB);
4871 end->function = entry;
4872
4873 progress = GL_TRUE;
4874 }
4875 }
4876 } while (progress);
4877
4878 #if 0
4879 /* Print out some information (for debugging purposes) used by the
4880 * optimization passes. */
4881 for (i=0; i < v->next_temp; i++) {
4882 int fr = v->get_first_temp_read(i);
4883 int fw = v->get_first_temp_write(i);
4884 int lr = v->get_last_temp_read(i);
4885 int lw = v->get_last_temp_write(i);
4886
4887 printf("Temp %d: FR=%3d FW=%3d LR=%3d LW=%3d\n", i, fr, fw, lr, lw);
4888 assert(fw <= fr);
4889 }
4890 #endif
4891
4892 /* Remove reads to output registers, and to varyings in vertex shaders. */
4893 v->remove_output_reads(PROGRAM_OUTPUT);
4894 if (target == GL_VERTEX_PROGRAM_ARB)
4895 v->remove_output_reads(PROGRAM_VARYING);
4896
4897 /* Perform optimizations on the instructions in the glsl_to_tgsi_visitor. */
4898 v->simplify_cmp();
4899 v->copy_propagate();
4900 while (v->eliminate_dead_code_advanced());
4901
4902 /* FIXME: These passes to optimize temporary registers don't work when there
4903 * is indirect addressing of the temporary register space. We need proper
4904 * array support so that we don't have to give up these passes in every
4905 * shader that uses arrays.
4906 */
4907 if (!v->indirect_addr_temps) {
4908 v->eliminate_dead_code();
4909 v->merge_registers();
4910 v->renumber_registers();
4911 }
4912
4913 /* Write the END instruction. */
4914 v->emit(NULL, TGSI_OPCODE_END);
4915
4916 if (ctx->Shader.Flags & GLSL_DUMP) {
4917 printf("\n");
4918 printf("GLSL IR for linked %s program %d:\n", target_string,
4919 shader_program->Name);
4920 _mesa_print_ir(shader->ir, NULL);
4921 printf("\n");
4922 printf("\n");
4923 }
4924
4925 prog->Instructions = NULL;
4926 prog->NumInstructions = 0;
4927
4928 do_set_program_inouts(shader->ir, prog);
4929 count_resources(v, prog);
4930
4931 check_resources(ctx, shader_program, v, prog);
4932
4933 _mesa_reference_program(ctx, &shader->Program, prog);
4934
4935 struct st_vertex_program *stvp;
4936 struct st_fragment_program *stfp;
4937 struct st_geometry_program *stgp;
4938
4939 switch (shader->Type) {
4940 case GL_VERTEX_SHADER:
4941 stvp = (struct st_vertex_program *)prog;
4942 stvp->glsl_to_tgsi = v;
4943 break;
4944 case GL_FRAGMENT_SHADER:
4945 stfp = (struct st_fragment_program *)prog;
4946 stfp->glsl_to_tgsi = v;
4947 break;
4948 case GL_GEOMETRY_SHADER:
4949 stgp = (struct st_geometry_program *)prog;
4950 stgp->glsl_to_tgsi = v;
4951 break;
4952 default:
4953 assert(!"should not be reached");
4954 return NULL;
4955 }
4956
4957 return prog;
4958 }
4959
4960 extern "C" {
4961
4962 struct gl_shader *
4963 st_new_shader(struct gl_context *ctx, GLuint name, GLuint type)
4964 {
4965 struct gl_shader *shader;
4966 assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER ||
4967 type == GL_GEOMETRY_SHADER_ARB);
4968 shader = rzalloc(NULL, struct gl_shader);
4969 if (shader) {
4970 shader->Type = type;
4971 shader->Name = name;
4972 _mesa_init_shader(ctx, shader);
4973 }
4974 return shader;
4975 }
4976
4977 struct gl_shader_program *
4978 st_new_shader_program(struct gl_context *ctx, GLuint name)
4979 {
4980 struct gl_shader_program *shProg;
4981 shProg = rzalloc(NULL, struct gl_shader_program);
4982 if (shProg) {
4983 shProg->Name = name;
4984 _mesa_init_shader_program(ctx, shProg);
4985 }
4986 return shProg;
4987 }
4988
4989 /**
4990 * Link a shader.
4991 * Called via ctx->Driver.LinkShader()
4992 * This actually involves converting GLSL IR into an intermediate TGSI-like IR
4993 * with code lowering and other optimizations.
4994 */
4995 GLboolean
4996 st_link_shader(struct gl_context *ctx, struct gl_shader_program *prog)
4997 {
4998 assert(prog->LinkStatus);
4999
5000 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
5001 if (prog->_LinkedShaders[i] == NULL)
5002 continue;
5003
5004 bool progress;
5005 exec_list *ir = prog->_LinkedShaders[i]->ir;
5006 const struct gl_shader_compiler_options *options =
5007 &ctx->ShaderCompilerOptions[_mesa_shader_type_to_index(prog->_LinkedShaders[i]->Type)];
5008
5009 do {
5010 progress = false;
5011
5012 /* Lowering */
5013 do_mat_op_to_vec(ir);
5014 lower_instructions(ir, (MOD_TO_FRACT | DIV_TO_MUL_RCP | EXP_TO_EXP2
5015 | LOG_TO_LOG2
5016 | ((options->EmitNoPow) ? POW_TO_EXP2 : 0)));
5017
5018 progress = do_lower_jumps(ir, true, true, options->EmitNoMainReturn, options->EmitNoCont, options->EmitNoLoops) || progress;
5019
5020 progress = do_common_optimization(ir, true, options->MaxUnrollIterations) || progress;
5021
5022 progress = lower_quadop_vector(ir, false) || progress;
5023
5024 if (options->EmitNoIfs) {
5025 progress = lower_discard(ir) || progress;
5026 progress = lower_if_to_cond_assign(ir) || progress;
5027 }
5028
5029 if (options->EmitNoNoise)
5030 progress = lower_noise(ir) || progress;
5031
5032 /* If there are forms of indirect addressing that the driver
5033 * cannot handle, perform the lowering pass.
5034 */
5035 if (options->EmitNoIndirectInput || options->EmitNoIndirectOutput
5036 || options->EmitNoIndirectTemp || options->EmitNoIndirectUniform)
5037 progress =
5038 lower_variable_index_to_cond_assign(ir,
5039 options->EmitNoIndirectInput,
5040 options->EmitNoIndirectOutput,
5041 options->EmitNoIndirectTemp,
5042 options->EmitNoIndirectUniform)
5043 || progress;
5044
5045 progress = do_vec_index_to_cond_assign(ir) || progress;
5046 } while (progress);
5047
5048 validate_ir_tree(ir);
5049 }
5050
5051 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
5052 struct gl_program *linked_prog;
5053
5054 if (prog->_LinkedShaders[i] == NULL)
5055 continue;
5056
5057 linked_prog = get_mesa_program(ctx, prog, prog->_LinkedShaders[i]);
5058
5059 if (linked_prog) {
5060 bool ok = true;
5061
5062 switch (prog->_LinkedShaders[i]->Type) {
5063 case GL_VERTEX_SHADER:
5064 _mesa_reference_vertprog(ctx, &prog->VertexProgram,
5065 (struct gl_vertex_program *)linked_prog);
5066 ok = ctx->Driver.ProgramStringNotify(ctx, GL_VERTEX_PROGRAM_ARB,
5067 linked_prog);
5068 break;
5069 case GL_FRAGMENT_SHADER:
5070 _mesa_reference_fragprog(ctx, &prog->FragmentProgram,
5071 (struct gl_fragment_program *)linked_prog);
5072 ok = ctx->Driver.ProgramStringNotify(ctx, GL_FRAGMENT_PROGRAM_ARB,
5073 linked_prog);
5074 break;
5075 case GL_GEOMETRY_SHADER:
5076 _mesa_reference_geomprog(ctx, &prog->GeometryProgram,
5077 (struct gl_geometry_program *)linked_prog);
5078 ok = ctx->Driver.ProgramStringNotify(ctx, GL_GEOMETRY_PROGRAM_NV,
5079 linked_prog);
5080 break;
5081 }
5082 if (!ok) {
5083 return GL_FALSE;
5084 }
5085 }
5086
5087 _mesa_reference_program(ctx, &linked_prog, NULL);
5088 }
5089
5090 return GL_TRUE;
5091 }
5092
5093
5094 /**
5095 * Link a GLSL shader program. Called via glLinkProgram().
5096 */
5097 void
5098 st_glsl_link_shader(struct gl_context *ctx, struct gl_shader_program *prog)
5099 {
5100 unsigned int i;
5101
5102 _mesa_clear_shader_program_data(ctx, prog);
5103
5104 prog->LinkStatus = GL_TRUE;
5105
5106 for (i = 0; i < prog->NumShaders; i++) {
5107 if (!prog->Shaders[i]->CompileStatus) {
5108 fail_link(prog, "linking with uncompiled shader");
5109 prog->LinkStatus = GL_FALSE;
5110 }
5111 }
5112
5113 prog->Varying = _mesa_new_parameter_list();
5114 _mesa_reference_vertprog(ctx, &prog->VertexProgram, NULL);
5115 _mesa_reference_fragprog(ctx, &prog->FragmentProgram, NULL);
5116 _mesa_reference_geomprog(ctx, &prog->GeometryProgram, NULL);
5117
5118 if (prog->LinkStatus) {
5119 link_shaders(ctx, prog);
5120 }
5121
5122 if (prog->LinkStatus) {
5123 if (!ctx->Driver.LinkShader(ctx, prog)) {
5124 prog->LinkStatus = GL_FALSE;
5125 }
5126 }
5127
5128 set_uniform_initializers(ctx, prog);
5129
5130 if (ctx->Shader.Flags & GLSL_DUMP) {
5131 if (!prog->LinkStatus) {
5132 printf("GLSL shader program %d failed to link\n", prog->Name);
5133 }
5134
5135 if (prog->InfoLog && prog->InfoLog[0] != 0) {
5136 printf("GLSL shader program %d info log:\n", prog->Name);
5137 printf("%s\n", prog->InfoLog);
5138 }
5139 }
5140 }
5141
5142 } /* extern "C" */