5992e202f41220986194db60cb91a10899f9282d
[mesa.git] / src / mesa / program / ir_to_mesa.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 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 */
25
26 /**
27 * \file ir_to_mesa.cpp
28 *
29 * Translate GLSL IR to Mesa's gl_program representation.
30 */
31
32 #include <stdio.h>
33 #include "main/compiler.h"
34 #include "ir.h"
35 #include "ir_visitor.h"
36 #include "ir_print_visitor.h"
37 #include "ir_expression_flattening.h"
38 #include "glsl_types.h"
39 #include "glsl_parser_extras.h"
40 #include "../glsl/program.h"
41 #include "ir_optimization.h"
42 #include "ast.h"
43
44 extern "C" {
45 #include "main/mtypes.h"
46 #include "main/shaderapi.h"
47 #include "main/shaderobj.h"
48 #include "main/uniforms.h"
49 #include "program/hash_table.h"
50 #include "program/prog_instruction.h"
51 #include "program/prog_optimize.h"
52 #include "program/prog_print.h"
53 #include "program/program.h"
54 #include "program/prog_uniform.h"
55 #include "program/prog_parameter.h"
56 #include "program/sampler.h"
57 }
58
59 class src_reg;
60 class dst_reg;
61
62 static int swizzle_for_size(int size);
63
64 /**
65 * This struct is a corresponding struct to Mesa prog_src_register, with
66 * wider fields.
67 */
68 class src_reg {
69 public:
70 src_reg(gl_register_file file, int index, const glsl_type *type)
71 {
72 this->file = file;
73 this->index = index;
74 if (type && (type->is_scalar() || type->is_vector() || type->is_matrix()))
75 this->swizzle = swizzle_for_size(type->vector_elements);
76 else
77 this->swizzle = SWIZZLE_XYZW;
78 this->negate = 0;
79 this->reladdr = NULL;
80 }
81
82 src_reg()
83 {
84 this->file = PROGRAM_UNDEFINED;
85 this->index = 0;
86 this->swizzle = 0;
87 this->negate = 0;
88 this->reladdr = NULL;
89 }
90
91 explicit src_reg(dst_reg reg);
92
93 gl_register_file file; /**< PROGRAM_* from Mesa */
94 int index; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
95 GLuint swizzle; /**< SWIZZLE_XYZWONEZERO swizzles from Mesa. */
96 int negate; /**< NEGATE_XYZW mask from mesa */
97 /** Register index should be offset by the integer in this reg. */
98 src_reg *reladdr;
99 };
100
101 class dst_reg {
102 public:
103 dst_reg(gl_register_file file, int writemask)
104 {
105 this->file = file;
106 this->index = 0;
107 this->writemask = writemask;
108 this->cond_mask = COND_TR;
109 this->reladdr = NULL;
110 }
111
112 dst_reg()
113 {
114 this->file = PROGRAM_UNDEFINED;
115 this->index = 0;
116 this->writemask = 0;
117 this->cond_mask = COND_TR;
118 this->reladdr = NULL;
119 }
120
121 explicit dst_reg(src_reg reg);
122
123 gl_register_file file; /**< PROGRAM_* from Mesa */
124 int index; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
125 int writemask; /**< Bitfield of WRITEMASK_[XYZW] */
126 GLuint cond_mask:4;
127 /** Register index should be offset by the integer in this reg. */
128 src_reg *reladdr;
129 };
130
131 src_reg::src_reg(dst_reg reg)
132 {
133 this->file = reg.file;
134 this->index = reg.index;
135 this->swizzle = SWIZZLE_XYZW;
136 this->negate = 0;
137 this->reladdr = reg.reladdr;
138 }
139
140 dst_reg::dst_reg(src_reg reg)
141 {
142 this->file = reg.file;
143 this->index = reg.index;
144 this->writemask = WRITEMASK_XYZW;
145 this->cond_mask = COND_TR;
146 this->reladdr = reg.reladdr;
147 }
148
149 class ir_to_mesa_instruction : public exec_node {
150 public:
151 /* Callers of this ralloc-based new need not call delete. It's
152 * easier to just ralloc_free 'ctx' (or any of its ancestors). */
153 static void* operator new(size_t size, void *ctx)
154 {
155 void *node;
156
157 node = rzalloc_size(ctx, size);
158 assert(node != NULL);
159
160 return node;
161 }
162
163 enum prog_opcode op;
164 dst_reg dst;
165 src_reg src[3];
166 /** Pointer to the ir source this tree came from for debugging */
167 ir_instruction *ir;
168 GLboolean cond_update;
169 bool saturate;
170 int sampler; /**< sampler index */
171 int tex_target; /**< One of TEXTURE_*_INDEX */
172 GLboolean tex_shadow;
173
174 class function_entry *function; /* Set on OPCODE_CAL or OPCODE_BGNSUB */
175 };
176
177 class variable_storage : public exec_node {
178 public:
179 variable_storage(ir_variable *var, gl_register_file file, int index)
180 : file(file), index(index), var(var)
181 {
182 /* empty */
183 }
184
185 gl_register_file file;
186 int index;
187 ir_variable *var; /* variable that maps to this, if any */
188 };
189
190 class function_entry : public exec_node {
191 public:
192 ir_function_signature *sig;
193
194 /**
195 * identifier of this function signature used by the program.
196 *
197 * At the point that Mesa instructions for function calls are
198 * generated, we don't know the address of the first instruction of
199 * the function body. So we make the BranchTarget that is called a
200 * small integer and rewrite them during set_branchtargets().
201 */
202 int sig_id;
203
204 /**
205 * Pointer to first instruction of the function body.
206 *
207 * Set during function body emits after main() is processed.
208 */
209 ir_to_mesa_instruction *bgn_inst;
210
211 /**
212 * Index of the first instruction of the function body in actual
213 * Mesa IR.
214 *
215 * Set after convertion from ir_to_mesa_instruction to prog_instruction.
216 */
217 int inst;
218
219 /** Storage for the return value. */
220 src_reg return_reg;
221 };
222
223 class ir_to_mesa_visitor : public ir_visitor {
224 public:
225 ir_to_mesa_visitor();
226 ~ir_to_mesa_visitor();
227
228 function_entry *current_function;
229
230 struct gl_context *ctx;
231 struct gl_program *prog;
232 struct gl_shader_program *shader_program;
233 struct gl_shader_compiler_options *options;
234
235 int next_temp;
236
237 variable_storage *find_variable_storage(ir_variable *var);
238
239 function_entry *get_function_signature(ir_function_signature *sig);
240
241 src_reg get_temp(const glsl_type *type);
242 void reladdr_to_temp(ir_instruction *ir, src_reg *reg, int *num_reladdr);
243
244 src_reg src_reg_for_float(float val);
245
246 /**
247 * \name Visit methods
248 *
249 * As typical for the visitor pattern, there must be one \c visit method for
250 * each concrete subclass of \c ir_instruction. Virtual base classes within
251 * the hierarchy should not have \c visit methods.
252 */
253 /*@{*/
254 virtual void visit(ir_variable *);
255 virtual void visit(ir_loop *);
256 virtual void visit(ir_loop_jump *);
257 virtual void visit(ir_function_signature *);
258 virtual void visit(ir_function *);
259 virtual void visit(ir_expression *);
260 virtual void visit(ir_swizzle *);
261 virtual void visit(ir_dereference_variable *);
262 virtual void visit(ir_dereference_array *);
263 virtual void visit(ir_dereference_record *);
264 virtual void visit(ir_assignment *);
265 virtual void visit(ir_constant *);
266 virtual void visit(ir_call *);
267 virtual void visit(ir_return *);
268 virtual void visit(ir_discard *);
269 virtual void visit(ir_texture *);
270 virtual void visit(ir_if *);
271 /*@}*/
272
273 src_reg result;
274
275 /** List of variable_storage */
276 exec_list variables;
277
278 /** List of function_entry */
279 exec_list function_signatures;
280 int next_signature_id;
281
282 /** List of ir_to_mesa_instruction */
283 exec_list instructions;
284
285 ir_to_mesa_instruction *emit(ir_instruction *ir, enum prog_opcode op);
286
287 ir_to_mesa_instruction *emit(ir_instruction *ir, enum prog_opcode op,
288 dst_reg dst, src_reg src0);
289
290 ir_to_mesa_instruction *emit(ir_instruction *ir, enum prog_opcode op,
291 dst_reg dst, src_reg src0, src_reg src1);
292
293 ir_to_mesa_instruction *emit(ir_instruction *ir, enum prog_opcode op,
294 dst_reg dst,
295 src_reg src0, src_reg src1, src_reg src2);
296
297 /**
298 * Emit the correct dot-product instruction for the type of arguments
299 */
300 ir_to_mesa_instruction * emit_dp(ir_instruction *ir,
301 dst_reg dst,
302 src_reg src0,
303 src_reg src1,
304 unsigned elements);
305
306 void emit_scalar(ir_instruction *ir, enum prog_opcode op,
307 dst_reg dst, src_reg src0);
308
309 void emit_scalar(ir_instruction *ir, enum prog_opcode op,
310 dst_reg dst, src_reg src0, src_reg src1);
311
312 void emit_scs(ir_instruction *ir, enum prog_opcode op,
313 dst_reg dst, const src_reg &src);
314
315 bool try_emit_mad(ir_expression *ir,
316 int mul_operand);
317 bool try_emit_mad_for_and_not(ir_expression *ir,
318 int mul_operand);
319 bool try_emit_sat(ir_expression *ir);
320
321 void emit_swz(ir_expression *ir);
322
323 bool process_move_condition(ir_rvalue *ir);
324
325 void copy_propagate(void);
326
327 void *mem_ctx;
328 };
329
330 src_reg undef_src = src_reg(PROGRAM_UNDEFINED, 0, NULL);
331
332 dst_reg undef_dst = dst_reg(PROGRAM_UNDEFINED, SWIZZLE_NOOP);
333
334 dst_reg address_reg = dst_reg(PROGRAM_ADDRESS, WRITEMASK_X);
335
336 static int
337 swizzle_for_size(int size)
338 {
339 int size_swizzles[4] = {
340 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X),
341 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Y, SWIZZLE_Y),
342 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z),
343 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W),
344 };
345
346 assert((size >= 1) && (size <= 4));
347 return size_swizzles[size - 1];
348 }
349
350 ir_to_mesa_instruction *
351 ir_to_mesa_visitor::emit(ir_instruction *ir, enum prog_opcode op,
352 dst_reg dst,
353 src_reg src0, src_reg src1, src_reg src2)
354 {
355 ir_to_mesa_instruction *inst = new(mem_ctx) ir_to_mesa_instruction();
356 int num_reladdr = 0;
357
358 /* If we have to do relative addressing, we want to load the ARL
359 * reg directly for one of the regs, and preload the other reladdr
360 * sources into temps.
361 */
362 num_reladdr += dst.reladdr != NULL;
363 num_reladdr += src0.reladdr != NULL;
364 num_reladdr += src1.reladdr != NULL;
365 num_reladdr += src2.reladdr != NULL;
366
367 reladdr_to_temp(ir, &src2, &num_reladdr);
368 reladdr_to_temp(ir, &src1, &num_reladdr);
369 reladdr_to_temp(ir, &src0, &num_reladdr);
370
371 if (dst.reladdr) {
372 emit(ir, OPCODE_ARL, address_reg, *dst.reladdr);
373 num_reladdr--;
374 }
375 assert(num_reladdr == 0);
376
377 inst->op = op;
378 inst->dst = dst;
379 inst->src[0] = src0;
380 inst->src[1] = src1;
381 inst->src[2] = src2;
382 inst->ir = ir;
383
384 inst->function = NULL;
385
386 this->instructions.push_tail(inst);
387
388 return inst;
389 }
390
391
392 ir_to_mesa_instruction *
393 ir_to_mesa_visitor::emit(ir_instruction *ir, enum prog_opcode op,
394 dst_reg dst, src_reg src0, src_reg src1)
395 {
396 return emit(ir, op, dst, src0, src1, undef_src);
397 }
398
399 ir_to_mesa_instruction *
400 ir_to_mesa_visitor::emit(ir_instruction *ir, enum prog_opcode op,
401 dst_reg dst, src_reg src0)
402 {
403 assert(dst.writemask != 0);
404 return emit(ir, op, dst, src0, undef_src, undef_src);
405 }
406
407 ir_to_mesa_instruction *
408 ir_to_mesa_visitor::emit(ir_instruction *ir, enum prog_opcode op)
409 {
410 return emit(ir, op, undef_dst, undef_src, undef_src, undef_src);
411 }
412
413 ir_to_mesa_instruction *
414 ir_to_mesa_visitor::emit_dp(ir_instruction *ir,
415 dst_reg dst, src_reg src0, src_reg src1,
416 unsigned elements)
417 {
418 static const gl_inst_opcode dot_opcodes[] = {
419 OPCODE_DP2, OPCODE_DP3, OPCODE_DP4
420 };
421
422 return emit(ir, dot_opcodes[elements - 2], dst, src0, src1);
423 }
424
425 /**
426 * Emits Mesa scalar opcodes to produce unique answers across channels.
427 *
428 * Some Mesa opcodes are scalar-only, like ARB_fp/vp. The src X
429 * channel determines the result across all channels. So to do a vec4
430 * of this operation, we want to emit a scalar per source channel used
431 * to produce dest channels.
432 */
433 void
434 ir_to_mesa_visitor::emit_scalar(ir_instruction *ir, enum prog_opcode op,
435 dst_reg dst,
436 src_reg orig_src0, src_reg orig_src1)
437 {
438 int i, j;
439 int done_mask = ~dst.writemask;
440
441 /* Mesa RCP is a scalar operation splatting results to all channels,
442 * like ARB_fp/vp. So emit as many RCPs as necessary to cover our
443 * dst channels.
444 */
445 for (i = 0; i < 4; i++) {
446 GLuint this_mask = (1 << i);
447 ir_to_mesa_instruction *inst;
448 src_reg src0 = orig_src0;
449 src_reg src1 = orig_src1;
450
451 if (done_mask & this_mask)
452 continue;
453
454 GLuint src0_swiz = GET_SWZ(src0.swizzle, i);
455 GLuint src1_swiz = GET_SWZ(src1.swizzle, i);
456 for (j = i + 1; j < 4; j++) {
457 /* If there is another enabled component in the destination that is
458 * derived from the same inputs, generate its value on this pass as
459 * well.
460 */
461 if (!(done_mask & (1 << j)) &&
462 GET_SWZ(src0.swizzle, j) == src0_swiz &&
463 GET_SWZ(src1.swizzle, j) == src1_swiz) {
464 this_mask |= (1 << j);
465 }
466 }
467 src0.swizzle = MAKE_SWIZZLE4(src0_swiz, src0_swiz,
468 src0_swiz, src0_swiz);
469 src1.swizzle = MAKE_SWIZZLE4(src1_swiz, src1_swiz,
470 src1_swiz, src1_swiz);
471
472 inst = emit(ir, op, dst, src0, src1);
473 inst->dst.writemask = this_mask;
474 done_mask |= this_mask;
475 }
476 }
477
478 void
479 ir_to_mesa_visitor::emit_scalar(ir_instruction *ir, enum prog_opcode op,
480 dst_reg dst, src_reg src0)
481 {
482 src_reg undef = undef_src;
483
484 undef.swizzle = SWIZZLE_XXXX;
485
486 emit_scalar(ir, op, dst, src0, undef);
487 }
488
489 /**
490 * Emit an OPCODE_SCS instruction
491 *
492 * The \c SCS opcode functions a bit differently than the other Mesa (or
493 * ARB_fragment_program) opcodes. Instead of splatting its result across all
494 * four components of the destination, it writes one value to the \c x
495 * component and another value to the \c y component.
496 *
497 * \param ir IR instruction being processed
498 * \param op Either \c OPCODE_SIN or \c OPCODE_COS depending on which
499 * value is desired.
500 * \param dst Destination register
501 * \param src Source register
502 */
503 void
504 ir_to_mesa_visitor::emit_scs(ir_instruction *ir, enum prog_opcode op,
505 dst_reg dst,
506 const src_reg &src)
507 {
508 /* Vertex programs cannot use the SCS opcode.
509 */
510 if (this->prog->Target == GL_VERTEX_PROGRAM_ARB) {
511 emit_scalar(ir, op, dst, src);
512 return;
513 }
514
515 const unsigned component = (op == OPCODE_SIN) ? 0 : 1;
516 const unsigned scs_mask = (1U << component);
517 int done_mask = ~dst.writemask;
518 src_reg tmp;
519
520 assert(op == OPCODE_SIN || op == OPCODE_COS);
521
522 /* If there are compnents in the destination that differ from the component
523 * that will be written by the SCS instrution, we'll need a temporary.
524 */
525 if (scs_mask != unsigned(dst.writemask)) {
526 tmp = get_temp(glsl_type::vec4_type);
527 }
528
529 for (unsigned i = 0; i < 4; i++) {
530 unsigned this_mask = (1U << i);
531 src_reg src0 = src;
532
533 if ((done_mask & this_mask) != 0)
534 continue;
535
536 /* The source swizzle specified which component of the source generates
537 * sine / cosine for the current component in the destination. The SCS
538 * instruction requires that this value be swizzle to the X component.
539 * Replace the current swizzle with a swizzle that puts the source in
540 * the X component.
541 */
542 unsigned src0_swiz = GET_SWZ(src.swizzle, i);
543
544 src0.swizzle = MAKE_SWIZZLE4(src0_swiz, src0_swiz,
545 src0_swiz, src0_swiz);
546 for (unsigned j = i + 1; j < 4; j++) {
547 /* If there is another enabled component in the destination that is
548 * derived from the same inputs, generate its value on this pass as
549 * well.
550 */
551 if (!(done_mask & (1 << j)) &&
552 GET_SWZ(src0.swizzle, j) == src0_swiz) {
553 this_mask |= (1 << j);
554 }
555 }
556
557 if (this_mask != scs_mask) {
558 ir_to_mesa_instruction *inst;
559 dst_reg tmp_dst = dst_reg(tmp);
560
561 /* Emit the SCS instruction.
562 */
563 inst = emit(ir, OPCODE_SCS, tmp_dst, src0);
564 inst->dst.writemask = scs_mask;
565
566 /* Move the result of the SCS instruction to the desired location in
567 * the destination.
568 */
569 tmp.swizzle = MAKE_SWIZZLE4(component, component,
570 component, component);
571 inst = emit(ir, OPCODE_SCS, dst, tmp);
572 inst->dst.writemask = this_mask;
573 } else {
574 /* Emit the SCS instruction to write directly to the destination.
575 */
576 ir_to_mesa_instruction *inst = emit(ir, OPCODE_SCS, dst, src0);
577 inst->dst.writemask = scs_mask;
578 }
579
580 done_mask |= this_mask;
581 }
582 }
583
584 src_reg
585 ir_to_mesa_visitor::src_reg_for_float(float val)
586 {
587 src_reg src(PROGRAM_CONSTANT, -1, NULL);
588
589 src.index = _mesa_add_unnamed_constant(this->prog->Parameters,
590 (const gl_constant_value *)&val, 1, &src.swizzle);
591
592 return src;
593 }
594
595 static int
596 type_size(const struct glsl_type *type)
597 {
598 unsigned int i;
599 int size;
600
601 switch (type->base_type) {
602 case GLSL_TYPE_UINT:
603 case GLSL_TYPE_INT:
604 case GLSL_TYPE_FLOAT:
605 case GLSL_TYPE_BOOL:
606 if (type->is_matrix()) {
607 return type->matrix_columns;
608 } else {
609 /* Regardless of size of vector, it gets a vec4. This is bad
610 * packing for things like floats, but otherwise arrays become a
611 * mess. Hopefully a later pass over the code can pack scalars
612 * down if appropriate.
613 */
614 return 1;
615 }
616 case GLSL_TYPE_ARRAY:
617 assert(type->length > 0);
618 return type_size(type->fields.array) * type->length;
619 case GLSL_TYPE_STRUCT:
620 size = 0;
621 for (i = 0; i < type->length; i++) {
622 size += type_size(type->fields.structure[i].type);
623 }
624 return size;
625 case GLSL_TYPE_SAMPLER:
626 /* Samplers take up one slot in UNIFORMS[], but they're baked in
627 * at link time.
628 */
629 return 1;
630 default:
631 assert(0);
632 return 0;
633 }
634 }
635
636 /**
637 * In the initial pass of codegen, we assign temporary numbers to
638 * intermediate results. (not SSA -- variable assignments will reuse
639 * storage). Actual register allocation for the Mesa VM occurs in a
640 * pass over the Mesa IR later.
641 */
642 src_reg
643 ir_to_mesa_visitor::get_temp(const glsl_type *type)
644 {
645 src_reg src;
646
647 src.file = PROGRAM_TEMPORARY;
648 src.index = next_temp;
649 src.reladdr = NULL;
650 next_temp += type_size(type);
651
652 if (type->is_array() || type->is_record()) {
653 src.swizzle = SWIZZLE_NOOP;
654 } else {
655 src.swizzle = swizzle_for_size(type->vector_elements);
656 }
657 src.negate = 0;
658
659 return src;
660 }
661
662 variable_storage *
663 ir_to_mesa_visitor::find_variable_storage(ir_variable *var)
664 {
665
666 variable_storage *entry;
667
668 foreach_iter(exec_list_iterator, iter, this->variables) {
669 entry = (variable_storage *)iter.get();
670
671 if (entry->var == var)
672 return entry;
673 }
674
675 return NULL;
676 }
677
678 void
679 ir_to_mesa_visitor::visit(ir_variable *ir)
680 {
681 if (strcmp(ir->name, "gl_FragCoord") == 0) {
682 struct gl_fragment_program *fp = (struct gl_fragment_program *)this->prog;
683
684 fp->OriginUpperLeft = ir->origin_upper_left;
685 fp->PixelCenterInteger = ir->pixel_center_integer;
686
687 } else if (strcmp(ir->name, "gl_FragDepth") == 0) {
688 struct gl_fragment_program *fp = (struct gl_fragment_program *)this->prog;
689 switch (ir->depth_layout) {
690 case ir_depth_layout_none:
691 fp->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
692 break;
693 case ir_depth_layout_any:
694 fp->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
695 break;
696 case ir_depth_layout_greater:
697 fp->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
698 break;
699 case ir_depth_layout_less:
700 fp->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
701 break;
702 case ir_depth_layout_unchanged:
703 fp->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
704 break;
705 default:
706 assert(0);
707 break;
708 }
709 }
710
711 if (ir->mode == ir_var_uniform && strncmp(ir->name, "gl_", 3) == 0) {
712 unsigned int i;
713 const ir_state_slot *const slots = ir->state_slots;
714 assert(ir->state_slots != NULL);
715
716 /* Check if this statevar's setup in the STATE file exactly
717 * matches how we'll want to reference it as a
718 * struct/array/whatever. If not, then we need to move it into
719 * temporary storage and hope that it'll get copy-propagated
720 * out.
721 */
722 for (i = 0; i < ir->num_state_slots; i++) {
723 if (slots[i].swizzle != SWIZZLE_XYZW) {
724 break;
725 }
726 }
727
728 variable_storage *storage;
729 dst_reg dst;
730 if (i == ir->num_state_slots) {
731 /* We'll set the index later. */
732 storage = new(mem_ctx) variable_storage(ir, PROGRAM_STATE_VAR, -1);
733 this->variables.push_tail(storage);
734
735 dst = undef_dst;
736 } else {
737 /* The variable_storage constructor allocates slots based on the size
738 * of the type. However, this had better match the number of state
739 * elements that we're going to copy into the new temporary.
740 */
741 assert((int) ir->num_state_slots == type_size(ir->type));
742
743 storage = new(mem_ctx) variable_storage(ir, PROGRAM_TEMPORARY,
744 this->next_temp);
745 this->variables.push_tail(storage);
746 this->next_temp += type_size(ir->type);
747
748 dst = dst_reg(src_reg(PROGRAM_TEMPORARY, storage->index, NULL));
749 }
750
751
752 for (unsigned int i = 0; i < ir->num_state_slots; i++) {
753 int index = _mesa_add_state_reference(this->prog->Parameters,
754 (gl_state_index *)slots[i].tokens);
755
756 if (storage->file == PROGRAM_STATE_VAR) {
757 if (storage->index == -1) {
758 storage->index = index;
759 } else {
760 assert(index == storage->index + (int)i);
761 }
762 } else {
763 src_reg src(PROGRAM_STATE_VAR, index, NULL);
764 src.swizzle = slots[i].swizzle;
765 emit(ir, OPCODE_MOV, dst, src);
766 /* even a float takes up a whole vec4 reg in a struct/array. */
767 dst.index++;
768 }
769 }
770
771 if (storage->file == PROGRAM_TEMPORARY &&
772 dst.index != storage->index + (int) ir->num_state_slots) {
773 linker_error(this->shader_program,
774 "failed to load builtin uniform `%s' "
775 "(%d/%d regs loaded)\n",
776 ir->name, dst.index - storage->index,
777 type_size(ir->type));
778 }
779 }
780 }
781
782 void
783 ir_to_mesa_visitor::visit(ir_loop *ir)
784 {
785 ir_dereference_variable *counter = NULL;
786
787 if (ir->counter != NULL)
788 counter = new(mem_ctx) ir_dereference_variable(ir->counter);
789
790 if (ir->from != NULL) {
791 assert(ir->counter != NULL);
792
793 ir_assignment *a =
794 new(mem_ctx) ir_assignment(counter, ir->from, NULL);
795
796 a->accept(this);
797 }
798
799 emit(NULL, OPCODE_BGNLOOP);
800
801 if (ir->to) {
802 ir_expression *e =
803 new(mem_ctx) ir_expression(ir->cmp, glsl_type::bool_type,
804 counter, ir->to);
805 ir_if *if_stmt = new(mem_ctx) ir_if(e);
806
807 ir_loop_jump *brk =
808 new(mem_ctx) ir_loop_jump(ir_loop_jump::jump_break);
809
810 if_stmt->then_instructions.push_tail(brk);
811
812 if_stmt->accept(this);
813 }
814
815 visit_exec_list(&ir->body_instructions, this);
816
817 if (ir->increment) {
818 ir_expression *e =
819 new(mem_ctx) ir_expression(ir_binop_add, counter->type,
820 counter, ir->increment);
821
822 ir_assignment *a =
823 new(mem_ctx) ir_assignment(counter, e, NULL);
824
825 a->accept(this);
826 }
827
828 emit(NULL, OPCODE_ENDLOOP);
829 }
830
831 void
832 ir_to_mesa_visitor::visit(ir_loop_jump *ir)
833 {
834 switch (ir->mode) {
835 case ir_loop_jump::jump_break:
836 emit(NULL, OPCODE_BRK);
837 break;
838 case ir_loop_jump::jump_continue:
839 emit(NULL, OPCODE_CONT);
840 break;
841 }
842 }
843
844
845 void
846 ir_to_mesa_visitor::visit(ir_function_signature *ir)
847 {
848 assert(0);
849 (void)ir;
850 }
851
852 void
853 ir_to_mesa_visitor::visit(ir_function *ir)
854 {
855 /* Ignore function bodies other than main() -- we shouldn't see calls to
856 * them since they should all be inlined before we get to ir_to_mesa.
857 */
858 if (strcmp(ir->name, "main") == 0) {
859 const ir_function_signature *sig;
860 exec_list empty;
861
862 sig = ir->matching_signature(&empty);
863
864 assert(sig);
865
866 foreach_iter(exec_list_iterator, iter, sig->body) {
867 ir_instruction *ir = (ir_instruction *)iter.get();
868
869 ir->accept(this);
870 }
871 }
872 }
873
874 bool
875 ir_to_mesa_visitor::try_emit_mad(ir_expression *ir, int mul_operand)
876 {
877 int nonmul_operand = 1 - mul_operand;
878 src_reg a, b, c;
879
880 ir_expression *expr = ir->operands[mul_operand]->as_expression();
881 if (!expr || expr->operation != ir_binop_mul)
882 return false;
883
884 expr->operands[0]->accept(this);
885 a = this->result;
886 expr->operands[1]->accept(this);
887 b = this->result;
888 ir->operands[nonmul_operand]->accept(this);
889 c = this->result;
890
891 this->result = get_temp(ir->type);
892 emit(ir, OPCODE_MAD, dst_reg(this->result), a, b, c);
893
894 return true;
895 }
896
897 /**
898 * Emit OPCODE_MAD(a, -b, a) instead of AND(a, NOT(b))
899 *
900 * The logic values are 1.0 for true and 0.0 for false. Logical-and is
901 * implemented using multiplication, and logical-or is implemented using
902 * addition. Logical-not can be implemented as (true - x), or (1.0 - x).
903 * As result, the logical expression (a & !b) can be rewritten as:
904 *
905 * - a * !b
906 * - a * (1 - b)
907 * - (a * 1) - (a * b)
908 * - a + -(a * b)
909 * - a + (a * -b)
910 *
911 * This final expression can be implemented as a single MAD(a, -b, a)
912 * instruction.
913 */
914 bool
915 ir_to_mesa_visitor::try_emit_mad_for_and_not(ir_expression *ir, int try_operand)
916 {
917 const int other_operand = 1 - try_operand;
918 src_reg a, b;
919
920 ir_expression *expr = ir->operands[try_operand]->as_expression();
921 if (!expr || expr->operation != ir_unop_logic_not)
922 return false;
923
924 ir->operands[other_operand]->accept(this);
925 a = this->result;
926 expr->operands[0]->accept(this);
927 b = this->result;
928
929 b.negate = ~b.negate;
930
931 this->result = get_temp(ir->type);
932 emit(ir, OPCODE_MAD, dst_reg(this->result), a, b, a);
933
934 return true;
935 }
936
937 bool
938 ir_to_mesa_visitor::try_emit_sat(ir_expression *ir)
939 {
940 /* Saturates were only introduced to vertex programs in
941 * NV_vertex_program3, so don't give them to drivers in the VP.
942 */
943 if (this->prog->Target == GL_VERTEX_PROGRAM_ARB)
944 return false;
945
946 ir_rvalue *sat_src = ir->as_rvalue_to_saturate();
947 if (!sat_src)
948 return false;
949
950 sat_src->accept(this);
951 src_reg src = this->result;
952
953 /* If we generated an expression instruction into a temporary in
954 * processing the saturate's operand, apply the saturate to that
955 * instruction. Otherwise, generate a MOV to do the saturate.
956 *
957 * Note that we have to be careful to only do this optimization if
958 * the instruction in question was what generated src->result. For
959 * example, ir_dereference_array might generate a MUL instruction
960 * to create the reladdr, and return us a src reg using that
961 * reladdr. That MUL result is not the value we're trying to
962 * saturate.
963 */
964 ir_expression *sat_src_expr = sat_src->as_expression();
965 ir_to_mesa_instruction *new_inst;
966 new_inst = (ir_to_mesa_instruction *)this->instructions.get_tail();
967 if (sat_src_expr && (sat_src_expr->operation == ir_binop_mul ||
968 sat_src_expr->operation == ir_binop_add ||
969 sat_src_expr->operation == ir_binop_dot)) {
970 new_inst->saturate = true;
971 } else {
972 this->result = get_temp(ir->type);
973 ir_to_mesa_instruction *inst;
974 inst = emit(ir, OPCODE_MOV, dst_reg(this->result), src);
975 inst->saturate = true;
976 }
977
978 return true;
979 }
980
981 void
982 ir_to_mesa_visitor::reladdr_to_temp(ir_instruction *ir,
983 src_reg *reg, int *num_reladdr)
984 {
985 if (!reg->reladdr)
986 return;
987
988 emit(ir, OPCODE_ARL, address_reg, *reg->reladdr);
989
990 if (*num_reladdr != 1) {
991 src_reg temp = get_temp(glsl_type::vec4_type);
992
993 emit(ir, OPCODE_MOV, dst_reg(temp), *reg);
994 *reg = temp;
995 }
996
997 (*num_reladdr)--;
998 }
999
1000 void
1001 ir_to_mesa_visitor::emit_swz(ir_expression *ir)
1002 {
1003 /* Assume that the vector operator is in a form compatible with OPCODE_SWZ.
1004 * This means that each of the operands is either an immediate value of -1,
1005 * 0, or 1, or is a component from one source register (possibly with
1006 * negation).
1007 */
1008 uint8_t components[4] = { 0 };
1009 bool negate[4] = { false };
1010 ir_variable *var = NULL;
1011
1012 for (unsigned i = 0; i < ir->type->vector_elements; i++) {
1013 ir_rvalue *op = ir->operands[i];
1014
1015 assert(op->type->is_scalar());
1016
1017 while (op != NULL) {
1018 switch (op->ir_type) {
1019 case ir_type_constant: {
1020
1021 assert(op->type->is_scalar());
1022
1023 const ir_constant *const c = op->as_constant();
1024 if (c->is_one()) {
1025 components[i] = SWIZZLE_ONE;
1026 } else if (c->is_zero()) {
1027 components[i] = SWIZZLE_ZERO;
1028 } else if (c->is_negative_one()) {
1029 components[i] = SWIZZLE_ONE;
1030 negate[i] = true;
1031 } else {
1032 assert(!"SWZ constant must be 0.0 or 1.0.");
1033 }
1034
1035 op = NULL;
1036 break;
1037 }
1038
1039 case ir_type_dereference_variable: {
1040 ir_dereference_variable *const deref =
1041 (ir_dereference_variable *) op;
1042
1043 assert((var == NULL) || (deref->var == var));
1044 components[i] = SWIZZLE_X;
1045 var = deref->var;
1046 op = NULL;
1047 break;
1048 }
1049
1050 case ir_type_expression: {
1051 ir_expression *const expr = (ir_expression *) op;
1052
1053 assert(expr->operation == ir_unop_neg);
1054 negate[i] = true;
1055
1056 op = expr->operands[0];
1057 break;
1058 }
1059
1060 case ir_type_swizzle: {
1061 ir_swizzle *const swiz = (ir_swizzle *) op;
1062
1063 components[i] = swiz->mask.x;
1064 op = swiz->val;
1065 break;
1066 }
1067
1068 default:
1069 assert(!"Should not get here.");
1070 return;
1071 }
1072 }
1073 }
1074
1075 assert(var != NULL);
1076
1077 ir_dereference_variable *const deref =
1078 new(mem_ctx) ir_dereference_variable(var);
1079
1080 this->result.file = PROGRAM_UNDEFINED;
1081 deref->accept(this);
1082 if (this->result.file == PROGRAM_UNDEFINED) {
1083 ir_print_visitor v;
1084 printf("Failed to get tree for expression operand:\n");
1085 deref->accept(&v);
1086 exit(1);
1087 }
1088
1089 src_reg src;
1090
1091 src = this->result;
1092 src.swizzle = MAKE_SWIZZLE4(components[0],
1093 components[1],
1094 components[2],
1095 components[3]);
1096 src.negate = ((unsigned(negate[0]) << 0)
1097 | (unsigned(negate[1]) << 1)
1098 | (unsigned(negate[2]) << 2)
1099 | (unsigned(negate[3]) << 3));
1100
1101 /* Storage for our result. Ideally for an assignment we'd be using the
1102 * actual storage for the result here, instead.
1103 */
1104 const src_reg result_src = get_temp(ir->type);
1105 dst_reg result_dst = dst_reg(result_src);
1106
1107 /* Limit writes to the channels that will be used by result_src later.
1108 * This does limit this temp's use as a temporary for multi-instruction
1109 * sequences.
1110 */
1111 result_dst.writemask = (1 << ir->type->vector_elements) - 1;
1112
1113 emit(ir, OPCODE_SWZ, result_dst, src);
1114 this->result = result_src;
1115 }
1116
1117 void
1118 ir_to_mesa_visitor::visit(ir_expression *ir)
1119 {
1120 unsigned int operand;
1121 src_reg op[Elements(ir->operands)];
1122 src_reg result_src;
1123 dst_reg result_dst;
1124
1125 /* Quick peephole: Emit OPCODE_MAD(a, b, c) instead of ADD(MUL(a, b), c)
1126 */
1127 if (ir->operation == ir_binop_add) {
1128 if (try_emit_mad(ir, 1))
1129 return;
1130 if (try_emit_mad(ir, 0))
1131 return;
1132 }
1133
1134 /* Quick peephole: Emit OPCODE_MAD(-a, -b, a) instead of AND(a, NOT(b))
1135 */
1136 if (ir->operation == ir_binop_logic_and) {
1137 if (try_emit_mad_for_and_not(ir, 1))
1138 return;
1139 if (try_emit_mad_for_and_not(ir, 0))
1140 return;
1141 }
1142
1143 if (try_emit_sat(ir))
1144 return;
1145
1146 if (ir->operation == ir_quadop_vector) {
1147 this->emit_swz(ir);
1148 return;
1149 }
1150
1151 for (operand = 0; operand < ir->get_num_operands(); operand++) {
1152 this->result.file = PROGRAM_UNDEFINED;
1153 ir->operands[operand]->accept(this);
1154 if (this->result.file == PROGRAM_UNDEFINED) {
1155 ir_print_visitor v;
1156 printf("Failed to get tree for expression operand:\n");
1157 ir->operands[operand]->accept(&v);
1158 exit(1);
1159 }
1160 op[operand] = this->result;
1161
1162 /* Matrix expression operands should have been broken down to vector
1163 * operations already.
1164 */
1165 assert(!ir->operands[operand]->type->is_matrix());
1166 }
1167
1168 int vector_elements = ir->operands[0]->type->vector_elements;
1169 if (ir->operands[1]) {
1170 vector_elements = MAX2(vector_elements,
1171 ir->operands[1]->type->vector_elements);
1172 }
1173
1174 this->result.file = PROGRAM_UNDEFINED;
1175
1176 /* Storage for our result. Ideally for an assignment we'd be using
1177 * the actual storage for the result here, instead.
1178 */
1179 result_src = get_temp(ir->type);
1180 /* convenience for the emit functions below. */
1181 result_dst = dst_reg(result_src);
1182 /* Limit writes to the channels that will be used by result_src later.
1183 * This does limit this temp's use as a temporary for multi-instruction
1184 * sequences.
1185 */
1186 result_dst.writemask = (1 << ir->type->vector_elements) - 1;
1187
1188 switch (ir->operation) {
1189 case ir_unop_logic_not:
1190 /* Previously 'SEQ dst, src, 0.0' was used for this. However, many
1191 * older GPUs implement SEQ using multiple instructions (i915 uses two
1192 * SGE instructions and a MUL instruction). Since our logic values are
1193 * 0.0 and 1.0, 1-x also implements !x.
1194 */
1195 op[0].negate = ~op[0].negate;
1196 emit(ir, OPCODE_ADD, result_dst, op[0], src_reg_for_float(1.0));
1197 break;
1198 case ir_unop_neg:
1199 op[0].negate = ~op[0].negate;
1200 result_src = op[0];
1201 break;
1202 case ir_unop_abs:
1203 emit(ir, OPCODE_ABS, result_dst, op[0]);
1204 break;
1205 case ir_unop_sign:
1206 emit(ir, OPCODE_SSG, result_dst, op[0]);
1207 break;
1208 case ir_unop_rcp:
1209 emit_scalar(ir, OPCODE_RCP, result_dst, op[0]);
1210 break;
1211
1212 case ir_unop_exp2:
1213 emit_scalar(ir, OPCODE_EX2, result_dst, op[0]);
1214 break;
1215 case ir_unop_exp:
1216 case ir_unop_log:
1217 assert(!"not reached: should be handled by ir_explog_to_explog2");
1218 break;
1219 case ir_unop_log2:
1220 emit_scalar(ir, OPCODE_LG2, result_dst, op[0]);
1221 break;
1222 case ir_unop_sin:
1223 emit_scalar(ir, OPCODE_SIN, result_dst, op[0]);
1224 break;
1225 case ir_unop_cos:
1226 emit_scalar(ir, OPCODE_COS, result_dst, op[0]);
1227 break;
1228 case ir_unop_sin_reduced:
1229 emit_scs(ir, OPCODE_SIN, result_dst, op[0]);
1230 break;
1231 case ir_unop_cos_reduced:
1232 emit_scs(ir, OPCODE_COS, result_dst, op[0]);
1233 break;
1234
1235 case ir_unop_dFdx:
1236 emit(ir, OPCODE_DDX, result_dst, op[0]);
1237 break;
1238 case ir_unop_dFdy:
1239 emit(ir, OPCODE_DDY, result_dst, op[0]);
1240 break;
1241
1242 case ir_unop_noise: {
1243 const enum prog_opcode opcode =
1244 prog_opcode(OPCODE_NOISE1
1245 + (ir->operands[0]->type->vector_elements) - 1);
1246 assert((opcode >= OPCODE_NOISE1) && (opcode <= OPCODE_NOISE4));
1247
1248 emit(ir, opcode, result_dst, op[0]);
1249 break;
1250 }
1251
1252 case ir_binop_add:
1253 emit(ir, OPCODE_ADD, result_dst, op[0], op[1]);
1254 break;
1255 case ir_binop_sub:
1256 emit(ir, OPCODE_SUB, result_dst, op[0], op[1]);
1257 break;
1258
1259 case ir_binop_mul:
1260 emit(ir, OPCODE_MUL, result_dst, op[0], op[1]);
1261 break;
1262 case ir_binop_div:
1263 assert(!"not reached: should be handled by ir_div_to_mul_rcp");
1264 case ir_binop_mod:
1265 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
1266 break;
1267
1268 case ir_binop_less:
1269 emit(ir, OPCODE_SLT, result_dst, op[0], op[1]);
1270 break;
1271 case ir_binop_greater:
1272 emit(ir, OPCODE_SGT, result_dst, op[0], op[1]);
1273 break;
1274 case ir_binop_lequal:
1275 emit(ir, OPCODE_SLE, result_dst, op[0], op[1]);
1276 break;
1277 case ir_binop_gequal:
1278 emit(ir, OPCODE_SGE, result_dst, op[0], op[1]);
1279 break;
1280 case ir_binop_equal:
1281 emit(ir, OPCODE_SEQ, result_dst, op[0], op[1]);
1282 break;
1283 case ir_binop_nequal:
1284 emit(ir, OPCODE_SNE, result_dst, op[0], op[1]);
1285 break;
1286 case ir_binop_all_equal:
1287 /* "==" operator producing a scalar boolean. */
1288 if (ir->operands[0]->type->is_vector() ||
1289 ir->operands[1]->type->is_vector()) {
1290 src_reg temp = get_temp(glsl_type::vec4_type);
1291 emit(ir, OPCODE_SNE, dst_reg(temp), op[0], op[1]);
1292
1293 /* After the dot-product, the value will be an integer on the
1294 * range [0,4]. Zero becomes 1.0, and positive values become zero.
1295 */
1296 emit_dp(ir, result_dst, temp, temp, vector_elements);
1297
1298 /* Negating the result of the dot-product gives values on the range
1299 * [-4, 0]. Zero becomes 1.0, and negative values become zero. This
1300 * achieved using SGE.
1301 */
1302 src_reg sge_src = result_src;
1303 sge_src.negate = ~sge_src.negate;
1304 emit(ir, OPCODE_SGE, result_dst, sge_src, src_reg_for_float(0.0));
1305 } else {
1306 emit(ir, OPCODE_SEQ, result_dst, op[0], op[1]);
1307 }
1308 break;
1309 case ir_binop_any_nequal:
1310 /* "!=" operator producing a scalar boolean. */
1311 if (ir->operands[0]->type->is_vector() ||
1312 ir->operands[1]->type->is_vector()) {
1313 src_reg temp = get_temp(glsl_type::vec4_type);
1314 emit(ir, OPCODE_SNE, dst_reg(temp), op[0], op[1]);
1315
1316 /* After the dot-product, the value will be an integer on the
1317 * range [0,4]. Zero stays zero, and positive values become 1.0.
1318 */
1319 ir_to_mesa_instruction *const dp =
1320 emit_dp(ir, result_dst, temp, temp, vector_elements);
1321 if (this->prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
1322 /* The clamping to [0,1] can be done for free in the fragment
1323 * shader with a saturate.
1324 */
1325 dp->saturate = true;
1326 } else {
1327 /* Negating the result of the dot-product gives values on the range
1328 * [-4, 0]. Zero stays zero, and negative values become 1.0. This
1329 * achieved using SLT.
1330 */
1331 src_reg slt_src = result_src;
1332 slt_src.negate = ~slt_src.negate;
1333 emit(ir, OPCODE_SLT, result_dst, slt_src, src_reg_for_float(0.0));
1334 }
1335 } else {
1336 emit(ir, OPCODE_SNE, result_dst, op[0], op[1]);
1337 }
1338 break;
1339
1340 case ir_unop_any: {
1341 assert(ir->operands[0]->type->is_vector());
1342
1343 /* After the dot-product, the value will be an integer on the
1344 * range [0,4]. Zero stays zero, and positive values become 1.0.
1345 */
1346 ir_to_mesa_instruction *const dp =
1347 emit_dp(ir, result_dst, op[0], op[0],
1348 ir->operands[0]->type->vector_elements);
1349 if (this->prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
1350 /* The clamping to [0,1] can be done for free in the fragment
1351 * shader with a saturate.
1352 */
1353 dp->saturate = true;
1354 } else {
1355 /* Negating the result of the dot-product gives values on the range
1356 * [-4, 0]. Zero stays zero, and negative values become 1.0. This
1357 * is achieved using SLT.
1358 */
1359 src_reg slt_src = result_src;
1360 slt_src.negate = ~slt_src.negate;
1361 emit(ir, OPCODE_SLT, result_dst, slt_src, src_reg_for_float(0.0));
1362 }
1363 break;
1364 }
1365
1366 case ir_binop_logic_xor:
1367 emit(ir, OPCODE_SNE, result_dst, op[0], op[1]);
1368 break;
1369
1370 case ir_binop_logic_or: {
1371 /* After the addition, the value will be an integer on the
1372 * range [0,2]. Zero stays zero, and positive values become 1.0.
1373 */
1374 ir_to_mesa_instruction *add =
1375 emit(ir, OPCODE_ADD, result_dst, op[0], op[1]);
1376 if (this->prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
1377 /* The clamping to [0,1] can be done for free in the fragment
1378 * shader with a saturate.
1379 */
1380 add->saturate = true;
1381 } else {
1382 /* Negating the result of the addition gives values on the range
1383 * [-2, 0]. Zero stays zero, and negative values become 1.0. This
1384 * is achieved using SLT.
1385 */
1386 src_reg slt_src = result_src;
1387 slt_src.negate = ~slt_src.negate;
1388 emit(ir, OPCODE_SLT, result_dst, slt_src, src_reg_for_float(0.0));
1389 }
1390 break;
1391 }
1392
1393 case ir_binop_logic_and:
1394 /* the bool args are stored as float 0.0 or 1.0, so "mul" gives us "and". */
1395 emit(ir, OPCODE_MUL, result_dst, op[0], op[1]);
1396 break;
1397
1398 case ir_binop_dot:
1399 assert(ir->operands[0]->type->is_vector());
1400 assert(ir->operands[0]->type == ir->operands[1]->type);
1401 emit_dp(ir, result_dst, op[0], op[1],
1402 ir->operands[0]->type->vector_elements);
1403 break;
1404
1405 case ir_unop_sqrt:
1406 /* sqrt(x) = x * rsq(x). */
1407 emit_scalar(ir, OPCODE_RSQ, result_dst, op[0]);
1408 emit(ir, OPCODE_MUL, result_dst, result_src, op[0]);
1409 /* For incoming channels <= 0, set the result to 0. */
1410 op[0].negate = ~op[0].negate;
1411 emit(ir, OPCODE_CMP, result_dst,
1412 op[0], result_src, src_reg_for_float(0.0));
1413 break;
1414 case ir_unop_rsq:
1415 emit_scalar(ir, OPCODE_RSQ, result_dst, op[0]);
1416 break;
1417 case ir_unop_i2f:
1418 case ir_unop_u2f:
1419 case ir_unop_b2f:
1420 case ir_unop_b2i:
1421 case ir_unop_i2u:
1422 case ir_unop_u2i:
1423 /* Mesa IR lacks types, ints are stored as truncated floats. */
1424 result_src = op[0];
1425 break;
1426 case ir_unop_f2i:
1427 emit(ir, OPCODE_TRUNC, result_dst, op[0]);
1428 break;
1429 case ir_unop_f2b:
1430 case ir_unop_i2b:
1431 emit(ir, OPCODE_SNE, result_dst,
1432 op[0], src_reg_for_float(0.0));
1433 break;
1434 case ir_unop_trunc:
1435 emit(ir, OPCODE_TRUNC, result_dst, op[0]);
1436 break;
1437 case ir_unop_ceil:
1438 op[0].negate = ~op[0].negate;
1439 emit(ir, OPCODE_FLR, result_dst, op[0]);
1440 result_src.negate = ~result_src.negate;
1441 break;
1442 case ir_unop_floor:
1443 emit(ir, OPCODE_FLR, result_dst, op[0]);
1444 break;
1445 case ir_unop_fract:
1446 emit(ir, OPCODE_FRC, result_dst, op[0]);
1447 break;
1448
1449 case ir_binop_min:
1450 emit(ir, OPCODE_MIN, result_dst, op[0], op[1]);
1451 break;
1452 case ir_binop_max:
1453 emit(ir, OPCODE_MAX, result_dst, op[0], op[1]);
1454 break;
1455 case ir_binop_pow:
1456 emit_scalar(ir, OPCODE_POW, result_dst, op[0], op[1]);
1457 break;
1458
1459 /* GLSL 1.30 integer ops are unsupported in Mesa IR, but since
1460 * hardware backends have no way to avoid Mesa IR generation
1461 * even if they don't use it, we need to emit "something" and
1462 * continue.
1463 */
1464 case ir_binop_lshift:
1465 case ir_binop_rshift:
1466 case ir_binop_bit_and:
1467 case ir_binop_bit_xor:
1468 case ir_binop_bit_or:
1469 emit(ir, OPCODE_ADD, result_dst, op[0], op[1]);
1470 break;
1471
1472 case ir_unop_bit_not:
1473 case ir_unop_round_even:
1474 emit(ir, OPCODE_MOV, result_dst, op[0]);
1475 break;
1476
1477 case ir_quadop_vector:
1478 /* This operation should have already been handled.
1479 */
1480 assert(!"Should not get here.");
1481 break;
1482 }
1483
1484 this->result = result_src;
1485 }
1486
1487
1488 void
1489 ir_to_mesa_visitor::visit(ir_swizzle *ir)
1490 {
1491 src_reg src;
1492 int i;
1493 int swizzle[4];
1494
1495 /* Note that this is only swizzles in expressions, not those on the left
1496 * hand side of an assignment, which do write masking. See ir_assignment
1497 * for that.
1498 */
1499
1500 ir->val->accept(this);
1501 src = this->result;
1502 assert(src.file != PROGRAM_UNDEFINED);
1503
1504 for (i = 0; i < 4; i++) {
1505 if (i < ir->type->vector_elements) {
1506 switch (i) {
1507 case 0:
1508 swizzle[i] = GET_SWZ(src.swizzle, ir->mask.x);
1509 break;
1510 case 1:
1511 swizzle[i] = GET_SWZ(src.swizzle, ir->mask.y);
1512 break;
1513 case 2:
1514 swizzle[i] = GET_SWZ(src.swizzle, ir->mask.z);
1515 break;
1516 case 3:
1517 swizzle[i] = GET_SWZ(src.swizzle, ir->mask.w);
1518 break;
1519 }
1520 } else {
1521 /* If the type is smaller than a vec4, replicate the last
1522 * channel out.
1523 */
1524 swizzle[i] = swizzle[ir->type->vector_elements - 1];
1525 }
1526 }
1527
1528 src.swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
1529
1530 this->result = src;
1531 }
1532
1533 void
1534 ir_to_mesa_visitor::visit(ir_dereference_variable *ir)
1535 {
1536 variable_storage *entry = find_variable_storage(ir->var);
1537 ir_variable *var = ir->var;
1538
1539 if (!entry) {
1540 switch (var->mode) {
1541 case ir_var_uniform:
1542 entry = new(mem_ctx) variable_storage(var, PROGRAM_UNIFORM,
1543 var->location);
1544 this->variables.push_tail(entry);
1545 break;
1546 case ir_var_in:
1547 case ir_var_inout:
1548 /* The linker assigns locations for varyings and attributes,
1549 * including deprecated builtins (like gl_Color),
1550 * user-assigned generic attributes (glBindVertexLocation),
1551 * and user-defined varyings.
1552 *
1553 * FINISHME: We would hit this path for function arguments. Fix!
1554 */
1555 assert(var->location != -1);
1556 entry = new(mem_ctx) variable_storage(var,
1557 PROGRAM_INPUT,
1558 var->location);
1559 if (this->prog->Target == GL_VERTEX_PROGRAM_ARB &&
1560 var->location >= VERT_ATTRIB_GENERIC0) {
1561 _mesa_add_attribute(this->prog->Attributes,
1562 var->name,
1563 _mesa_sizeof_glsl_type(var->type->gl_type),
1564 var->type->gl_type,
1565 var->location - VERT_ATTRIB_GENERIC0);
1566 }
1567 break;
1568 case ir_var_out:
1569 assert(var->location != -1);
1570 entry = new(mem_ctx) variable_storage(var,
1571 PROGRAM_OUTPUT,
1572 var->location);
1573 break;
1574 case ir_var_system_value:
1575 entry = new(mem_ctx) variable_storage(var,
1576 PROGRAM_SYSTEM_VALUE,
1577 var->location);
1578 break;
1579 case ir_var_auto:
1580 case ir_var_temporary:
1581 entry = new(mem_ctx) variable_storage(var, PROGRAM_TEMPORARY,
1582 this->next_temp);
1583 this->variables.push_tail(entry);
1584
1585 next_temp += type_size(var->type);
1586 break;
1587 }
1588
1589 if (!entry) {
1590 printf("Failed to make storage for %s\n", var->name);
1591 exit(1);
1592 }
1593 }
1594
1595 this->result = src_reg(entry->file, entry->index, var->type);
1596 }
1597
1598 void
1599 ir_to_mesa_visitor::visit(ir_dereference_array *ir)
1600 {
1601 ir_constant *index;
1602 src_reg src;
1603 int element_size = type_size(ir->type);
1604
1605 index = ir->array_index->constant_expression_value();
1606
1607 ir->array->accept(this);
1608 src = this->result;
1609
1610 if (index) {
1611 src.index += index->value.i[0] * element_size;
1612 } else {
1613 /* Variable index array dereference. It eats the "vec4" of the
1614 * base of the array and an index that offsets the Mesa register
1615 * index.
1616 */
1617 ir->array_index->accept(this);
1618
1619 src_reg index_reg;
1620
1621 if (element_size == 1) {
1622 index_reg = this->result;
1623 } else {
1624 index_reg = get_temp(glsl_type::float_type);
1625
1626 emit(ir, OPCODE_MUL, dst_reg(index_reg),
1627 this->result, src_reg_for_float(element_size));
1628 }
1629
1630 /* If there was already a relative address register involved, add the
1631 * new and the old together to get the new offset.
1632 */
1633 if (src.reladdr != NULL) {
1634 src_reg accum_reg = get_temp(glsl_type::float_type);
1635
1636 emit(ir, OPCODE_ADD, dst_reg(accum_reg),
1637 index_reg, *src.reladdr);
1638
1639 index_reg = accum_reg;
1640 }
1641
1642 src.reladdr = ralloc(mem_ctx, src_reg);
1643 memcpy(src.reladdr, &index_reg, sizeof(index_reg));
1644 }
1645
1646 /* If the type is smaller than a vec4, replicate the last channel out. */
1647 if (ir->type->is_scalar() || ir->type->is_vector())
1648 src.swizzle = swizzle_for_size(ir->type->vector_elements);
1649 else
1650 src.swizzle = SWIZZLE_NOOP;
1651
1652 this->result = src;
1653 }
1654
1655 void
1656 ir_to_mesa_visitor::visit(ir_dereference_record *ir)
1657 {
1658 unsigned int i;
1659 const glsl_type *struct_type = ir->record->type;
1660 int offset = 0;
1661
1662 ir->record->accept(this);
1663
1664 for (i = 0; i < struct_type->length; i++) {
1665 if (strcmp(struct_type->fields.structure[i].name, ir->field) == 0)
1666 break;
1667 offset += type_size(struct_type->fields.structure[i].type);
1668 }
1669
1670 /* If the type is smaller than a vec4, replicate the last channel out. */
1671 if (ir->type->is_scalar() || ir->type->is_vector())
1672 this->result.swizzle = swizzle_for_size(ir->type->vector_elements);
1673 else
1674 this->result.swizzle = SWIZZLE_NOOP;
1675
1676 this->result.index += offset;
1677 }
1678
1679 /**
1680 * We want to be careful in assignment setup to hit the actual storage
1681 * instead of potentially using a temporary like we might with the
1682 * ir_dereference handler.
1683 */
1684 static dst_reg
1685 get_assignment_lhs(ir_dereference *ir, ir_to_mesa_visitor *v)
1686 {
1687 /* The LHS must be a dereference. If the LHS is a variable indexed array
1688 * access of a vector, it must be separated into a series conditional moves
1689 * before reaching this point (see ir_vec_index_to_cond_assign).
1690 */
1691 assert(ir->as_dereference());
1692 ir_dereference_array *deref_array = ir->as_dereference_array();
1693 if (deref_array) {
1694 assert(!deref_array->array->type->is_vector());
1695 }
1696
1697 /* Use the rvalue deref handler for the most part. We'll ignore
1698 * swizzles in it and write swizzles using writemask, though.
1699 */
1700 ir->accept(v);
1701 return dst_reg(v->result);
1702 }
1703
1704 /**
1705 * Process the condition of a conditional assignment
1706 *
1707 * Examines the condition of a conditional assignment to generate the optimal
1708 * first operand of a \c CMP instruction. If the condition is a relational
1709 * operator with 0 (e.g., \c ir_binop_less), the value being compared will be
1710 * used as the source for the \c CMP instruction. Otherwise the comparison
1711 * is processed to a boolean result, and the boolean result is used as the
1712 * operand to the CMP instruction.
1713 */
1714 bool
1715 ir_to_mesa_visitor::process_move_condition(ir_rvalue *ir)
1716 {
1717 ir_rvalue *src_ir = ir;
1718 bool negate = true;
1719 bool switch_order = false;
1720
1721 ir_expression *const expr = ir->as_expression();
1722 if ((expr != NULL) && (expr->get_num_operands() == 2)) {
1723 bool zero_on_left = false;
1724
1725 if (expr->operands[0]->is_zero()) {
1726 src_ir = expr->operands[1];
1727 zero_on_left = true;
1728 } else if (expr->operands[1]->is_zero()) {
1729 src_ir = expr->operands[0];
1730 zero_on_left = false;
1731 }
1732
1733 /* a is - 0 + - 0 +
1734 * (a < 0) T F F ( a < 0) T F F
1735 * (0 < a) F F T (-a < 0) F F T
1736 * (a <= 0) T T F (-a < 0) F F T (swap order of other operands)
1737 * (0 <= a) F T T ( a < 0) T F F (swap order of other operands)
1738 * (a > 0) F F T (-a < 0) F F T
1739 * (0 > a) T F F ( a < 0) T F F
1740 * (a >= 0) F T T ( a < 0) T F F (swap order of other operands)
1741 * (0 >= a) T T F (-a < 0) F F T (swap order of other operands)
1742 *
1743 * Note that exchanging the order of 0 and 'a' in the comparison simply
1744 * means that the value of 'a' should be negated.
1745 */
1746 if (src_ir != ir) {
1747 switch (expr->operation) {
1748 case ir_binop_less:
1749 switch_order = false;
1750 negate = zero_on_left;
1751 break;
1752
1753 case ir_binop_greater:
1754 switch_order = false;
1755 negate = !zero_on_left;
1756 break;
1757
1758 case ir_binop_lequal:
1759 switch_order = true;
1760 negate = !zero_on_left;
1761 break;
1762
1763 case ir_binop_gequal:
1764 switch_order = true;
1765 negate = zero_on_left;
1766 break;
1767
1768 default:
1769 /* This isn't the right kind of comparison afterall, so make sure
1770 * the whole condition is visited.
1771 */
1772 src_ir = ir;
1773 break;
1774 }
1775 }
1776 }
1777
1778 src_ir->accept(this);
1779
1780 /* We use the OPCODE_CMP (a < 0 ? b : c) for conditional moves, and the
1781 * condition we produced is 0.0 or 1.0. By flipping the sign, we can
1782 * choose which value OPCODE_CMP produces without an extra instruction
1783 * computing the condition.
1784 */
1785 if (negate)
1786 this->result.negate = ~this->result.negate;
1787
1788 return switch_order;
1789 }
1790
1791 void
1792 ir_to_mesa_visitor::visit(ir_assignment *ir)
1793 {
1794 dst_reg l;
1795 src_reg r;
1796 int i;
1797
1798 ir->rhs->accept(this);
1799 r = this->result;
1800
1801 l = get_assignment_lhs(ir->lhs, this);
1802
1803 /* FINISHME: This should really set to the correct maximal writemask for each
1804 * FINISHME: component written (in the loops below). This case can only
1805 * FINISHME: occur for matrices, arrays, and structures.
1806 */
1807 if (ir->write_mask == 0) {
1808 assert(!ir->lhs->type->is_scalar() && !ir->lhs->type->is_vector());
1809 l.writemask = WRITEMASK_XYZW;
1810 } else if (ir->lhs->type->is_scalar()) {
1811 /* FINISHME: This hack makes writing to gl_FragDepth, which lives in the
1812 * FINISHME: W component of fragment shader output zero, work correctly.
1813 */
1814 l.writemask = WRITEMASK_XYZW;
1815 } else {
1816 int swizzles[4];
1817 int first_enabled_chan = 0;
1818 int rhs_chan = 0;
1819
1820 assert(ir->lhs->type->is_vector());
1821 l.writemask = ir->write_mask;
1822
1823 for (int i = 0; i < 4; i++) {
1824 if (l.writemask & (1 << i)) {
1825 first_enabled_chan = GET_SWZ(r.swizzle, i);
1826 break;
1827 }
1828 }
1829
1830 /* Swizzle a small RHS vector into the channels being written.
1831 *
1832 * glsl ir treats write_mask as dictating how many channels are
1833 * present on the RHS while Mesa IR treats write_mask as just
1834 * showing which channels of the vec4 RHS get written.
1835 */
1836 for (int i = 0; i < 4; i++) {
1837 if (l.writemask & (1 << i))
1838 swizzles[i] = GET_SWZ(r.swizzle, rhs_chan++);
1839 else
1840 swizzles[i] = first_enabled_chan;
1841 }
1842 r.swizzle = MAKE_SWIZZLE4(swizzles[0], swizzles[1],
1843 swizzles[2], swizzles[3]);
1844 }
1845
1846 assert(l.file != PROGRAM_UNDEFINED);
1847 assert(r.file != PROGRAM_UNDEFINED);
1848
1849 if (ir->condition) {
1850 const bool switch_order = this->process_move_condition(ir->condition);
1851 src_reg condition = this->result;
1852
1853 for (i = 0; i < type_size(ir->lhs->type); i++) {
1854 if (switch_order) {
1855 emit(ir, OPCODE_CMP, l, condition, src_reg(l), r);
1856 } else {
1857 emit(ir, OPCODE_CMP, l, condition, r, src_reg(l));
1858 }
1859
1860 l.index++;
1861 r.index++;
1862 }
1863 } else {
1864 for (i = 0; i < type_size(ir->lhs->type); i++) {
1865 emit(ir, OPCODE_MOV, l, r);
1866 l.index++;
1867 r.index++;
1868 }
1869 }
1870 }
1871
1872
1873 void
1874 ir_to_mesa_visitor::visit(ir_constant *ir)
1875 {
1876 src_reg src;
1877 GLfloat stack_vals[4] = { 0 };
1878 GLfloat *values = stack_vals;
1879 unsigned int i;
1880
1881 /* Unfortunately, 4 floats is all we can get into
1882 * _mesa_add_unnamed_constant. So, make a temp to store an
1883 * aggregate constant and move each constant value into it. If we
1884 * get lucky, copy propagation will eliminate the extra moves.
1885 */
1886
1887 if (ir->type->base_type == GLSL_TYPE_STRUCT) {
1888 src_reg temp_base = get_temp(ir->type);
1889 dst_reg temp = dst_reg(temp_base);
1890
1891 foreach_iter(exec_list_iterator, iter, ir->components) {
1892 ir_constant *field_value = (ir_constant *)iter.get();
1893 int size = type_size(field_value->type);
1894
1895 assert(size > 0);
1896
1897 field_value->accept(this);
1898 src = this->result;
1899
1900 for (i = 0; i < (unsigned int)size; i++) {
1901 emit(ir, OPCODE_MOV, temp, src);
1902
1903 src.index++;
1904 temp.index++;
1905 }
1906 }
1907 this->result = temp_base;
1908 return;
1909 }
1910
1911 if (ir->type->is_array()) {
1912 src_reg temp_base = get_temp(ir->type);
1913 dst_reg temp = dst_reg(temp_base);
1914 int size = type_size(ir->type->fields.array);
1915
1916 assert(size > 0);
1917
1918 for (i = 0; i < ir->type->length; i++) {
1919 ir->array_elements[i]->accept(this);
1920 src = this->result;
1921 for (int j = 0; j < size; j++) {
1922 emit(ir, OPCODE_MOV, temp, src);
1923
1924 src.index++;
1925 temp.index++;
1926 }
1927 }
1928 this->result = temp_base;
1929 return;
1930 }
1931
1932 if (ir->type->is_matrix()) {
1933 src_reg mat = get_temp(ir->type);
1934 dst_reg mat_column = dst_reg(mat);
1935
1936 for (i = 0; i < ir->type->matrix_columns; i++) {
1937 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
1938 values = &ir->value.f[i * ir->type->vector_elements];
1939
1940 src = src_reg(PROGRAM_CONSTANT, -1, NULL);
1941 src.index = _mesa_add_unnamed_constant(this->prog->Parameters,
1942 (gl_constant_value *) values,
1943 ir->type->vector_elements,
1944 &src.swizzle);
1945 emit(ir, OPCODE_MOV, mat_column, src);
1946
1947 mat_column.index++;
1948 }
1949
1950 this->result = mat;
1951 return;
1952 }
1953
1954 src.file = PROGRAM_CONSTANT;
1955 switch (ir->type->base_type) {
1956 case GLSL_TYPE_FLOAT:
1957 values = &ir->value.f[0];
1958 break;
1959 case GLSL_TYPE_UINT:
1960 for (i = 0; i < ir->type->vector_elements; i++) {
1961 values[i] = ir->value.u[i];
1962 }
1963 break;
1964 case GLSL_TYPE_INT:
1965 for (i = 0; i < ir->type->vector_elements; i++) {
1966 values[i] = ir->value.i[i];
1967 }
1968 break;
1969 case GLSL_TYPE_BOOL:
1970 for (i = 0; i < ir->type->vector_elements; i++) {
1971 values[i] = ir->value.b[i];
1972 }
1973 break;
1974 default:
1975 assert(!"Non-float/uint/int/bool constant");
1976 }
1977
1978 this->result = src_reg(PROGRAM_CONSTANT, -1, ir->type);
1979 this->result.index = _mesa_add_unnamed_constant(this->prog->Parameters,
1980 (gl_constant_value *) values,
1981 ir->type->vector_elements,
1982 &this->result.swizzle);
1983 }
1984
1985 function_entry *
1986 ir_to_mesa_visitor::get_function_signature(ir_function_signature *sig)
1987 {
1988 function_entry *entry;
1989
1990 foreach_iter(exec_list_iterator, iter, this->function_signatures) {
1991 entry = (function_entry *)iter.get();
1992
1993 if (entry->sig == sig)
1994 return entry;
1995 }
1996
1997 entry = ralloc(mem_ctx, function_entry);
1998 entry->sig = sig;
1999 entry->sig_id = this->next_signature_id++;
2000 entry->bgn_inst = NULL;
2001
2002 /* Allocate storage for all the parameters. */
2003 foreach_iter(exec_list_iterator, iter, sig->parameters) {
2004 ir_variable *param = (ir_variable *)iter.get();
2005 variable_storage *storage;
2006
2007 storage = find_variable_storage(param);
2008 assert(!storage);
2009
2010 storage = new(mem_ctx) variable_storage(param, PROGRAM_TEMPORARY,
2011 this->next_temp);
2012 this->variables.push_tail(storage);
2013
2014 this->next_temp += type_size(param->type);
2015 }
2016
2017 if (!sig->return_type->is_void()) {
2018 entry->return_reg = get_temp(sig->return_type);
2019 } else {
2020 entry->return_reg = undef_src;
2021 }
2022
2023 this->function_signatures.push_tail(entry);
2024 return entry;
2025 }
2026
2027 void
2028 ir_to_mesa_visitor::visit(ir_call *ir)
2029 {
2030 ir_to_mesa_instruction *call_inst;
2031 ir_function_signature *sig = ir->get_callee();
2032 function_entry *entry = get_function_signature(sig);
2033 int i;
2034
2035 /* Process in parameters. */
2036 exec_list_iterator sig_iter = sig->parameters.iterator();
2037 foreach_iter(exec_list_iterator, iter, *ir) {
2038 ir_rvalue *param_rval = (ir_rvalue *)iter.get();
2039 ir_variable *param = (ir_variable *)sig_iter.get();
2040
2041 if (param->mode == ir_var_in ||
2042 param->mode == ir_var_inout) {
2043 variable_storage *storage = find_variable_storage(param);
2044 assert(storage);
2045
2046 param_rval->accept(this);
2047 src_reg r = this->result;
2048
2049 dst_reg l;
2050 l.file = storage->file;
2051 l.index = storage->index;
2052 l.reladdr = NULL;
2053 l.writemask = WRITEMASK_XYZW;
2054 l.cond_mask = COND_TR;
2055
2056 for (i = 0; i < type_size(param->type); i++) {
2057 emit(ir, OPCODE_MOV, l, r);
2058 l.index++;
2059 r.index++;
2060 }
2061 }
2062
2063 sig_iter.next();
2064 }
2065 assert(!sig_iter.has_next());
2066
2067 /* Emit call instruction */
2068 call_inst = emit(ir, OPCODE_CAL);
2069 call_inst->function = entry;
2070
2071 /* Process out parameters. */
2072 sig_iter = sig->parameters.iterator();
2073 foreach_iter(exec_list_iterator, iter, *ir) {
2074 ir_rvalue *param_rval = (ir_rvalue *)iter.get();
2075 ir_variable *param = (ir_variable *)sig_iter.get();
2076
2077 if (param->mode == ir_var_out ||
2078 param->mode == ir_var_inout) {
2079 variable_storage *storage = find_variable_storage(param);
2080 assert(storage);
2081
2082 src_reg r;
2083 r.file = storage->file;
2084 r.index = storage->index;
2085 r.reladdr = NULL;
2086 r.swizzle = SWIZZLE_NOOP;
2087 r.negate = 0;
2088
2089 param_rval->accept(this);
2090 dst_reg l = dst_reg(this->result);
2091
2092 for (i = 0; i < type_size(param->type); i++) {
2093 emit(ir, OPCODE_MOV, l, r);
2094 l.index++;
2095 r.index++;
2096 }
2097 }
2098
2099 sig_iter.next();
2100 }
2101 assert(!sig_iter.has_next());
2102
2103 /* Process return value. */
2104 this->result = entry->return_reg;
2105 }
2106
2107 void
2108 ir_to_mesa_visitor::visit(ir_texture *ir)
2109 {
2110 src_reg result_src, coord, lod_info, projector, dx, dy;
2111 dst_reg result_dst, coord_dst;
2112 ir_to_mesa_instruction *inst = NULL;
2113 prog_opcode opcode = OPCODE_NOP;
2114
2115 if (ir->op == ir_txs)
2116 this->result = src_reg_for_float(0.0);
2117 else
2118 ir->coordinate->accept(this);
2119
2120 /* Put our coords in a temp. We'll need to modify them for shadow,
2121 * projection, or LOD, so the only case we'd use it as is is if
2122 * we're doing plain old texturing. Mesa IR optimization should
2123 * handle cleaning up our mess in that case.
2124 */
2125 coord = get_temp(glsl_type::vec4_type);
2126 coord_dst = dst_reg(coord);
2127 emit(ir, OPCODE_MOV, coord_dst, this->result);
2128
2129 if (ir->projector) {
2130 ir->projector->accept(this);
2131 projector = this->result;
2132 }
2133
2134 /* Storage for our result. Ideally for an assignment we'd be using
2135 * the actual storage for the result here, instead.
2136 */
2137 result_src = get_temp(glsl_type::vec4_type);
2138 result_dst = dst_reg(result_src);
2139
2140 switch (ir->op) {
2141 case ir_tex:
2142 case ir_txs:
2143 opcode = OPCODE_TEX;
2144 break;
2145 case ir_txb:
2146 opcode = OPCODE_TXB;
2147 ir->lod_info.bias->accept(this);
2148 lod_info = this->result;
2149 break;
2150 case ir_txf:
2151 /* Pretend to be TXL so the sampler, coordinate, lod are available */
2152 case ir_txl:
2153 opcode = OPCODE_TXL;
2154 ir->lod_info.lod->accept(this);
2155 lod_info = this->result;
2156 break;
2157 case ir_txd:
2158 opcode = OPCODE_TXD;
2159 ir->lod_info.grad.dPdx->accept(this);
2160 dx = this->result;
2161 ir->lod_info.grad.dPdy->accept(this);
2162 dy = this->result;
2163 break;
2164 }
2165
2166 const glsl_type *sampler_type = ir->sampler->type;
2167
2168 if (ir->projector) {
2169 if (opcode == OPCODE_TEX) {
2170 /* Slot the projector in as the last component of the coord. */
2171 coord_dst.writemask = WRITEMASK_W;
2172 emit(ir, OPCODE_MOV, coord_dst, projector);
2173 coord_dst.writemask = WRITEMASK_XYZW;
2174 opcode = OPCODE_TXP;
2175 } else {
2176 src_reg coord_w = coord;
2177 coord_w.swizzle = SWIZZLE_WWWW;
2178
2179 /* For the other TEX opcodes there's no projective version
2180 * since the last slot is taken up by lod info. Do the
2181 * projective divide now.
2182 */
2183 coord_dst.writemask = WRITEMASK_W;
2184 emit(ir, OPCODE_RCP, coord_dst, projector);
2185
2186 /* In the case where we have to project the coordinates "by hand,"
2187 * the shadow comparitor value must also be projected.
2188 */
2189 src_reg tmp_src = coord;
2190 if (ir->shadow_comparitor) {
2191 /* Slot the shadow value in as the second to last component of the
2192 * coord.
2193 */
2194 ir->shadow_comparitor->accept(this);
2195
2196 tmp_src = get_temp(glsl_type::vec4_type);
2197 dst_reg tmp_dst = dst_reg(tmp_src);
2198
2199 /* Projective division not allowed for array samplers. */
2200 assert(!sampler_type->sampler_array);
2201
2202 tmp_dst.writemask = WRITEMASK_Z;
2203 emit(ir, OPCODE_MOV, tmp_dst, this->result);
2204
2205 tmp_dst.writemask = WRITEMASK_XY;
2206 emit(ir, OPCODE_MOV, tmp_dst, coord);
2207 }
2208
2209 coord_dst.writemask = WRITEMASK_XYZ;
2210 emit(ir, OPCODE_MUL, coord_dst, tmp_src, coord_w);
2211
2212 coord_dst.writemask = WRITEMASK_XYZW;
2213 coord.swizzle = SWIZZLE_XYZW;
2214 }
2215 }
2216
2217 /* If projection is done and the opcode is not OPCODE_TXP, then the shadow
2218 * comparitor was put in the correct place (and projected) by the code,
2219 * above, that handles by-hand projection.
2220 */
2221 if (ir->shadow_comparitor && (!ir->projector || opcode == OPCODE_TXP)) {
2222 /* Slot the shadow value in as the second to last component of the
2223 * coord.
2224 */
2225 ir->shadow_comparitor->accept(this);
2226
2227 /* XXX This will need to be updated for cubemap array samplers. */
2228 if (sampler_type->sampler_dimensionality == GLSL_SAMPLER_DIM_2D &&
2229 sampler_type->sampler_array) {
2230 coord_dst.writemask = WRITEMASK_W;
2231 } else {
2232 coord_dst.writemask = WRITEMASK_Z;
2233 }
2234
2235 emit(ir, OPCODE_MOV, coord_dst, this->result);
2236 coord_dst.writemask = WRITEMASK_XYZW;
2237 }
2238
2239 if (opcode == OPCODE_TXL || opcode == OPCODE_TXB) {
2240 /* Mesa IR stores lod or lod bias in the last channel of the coords. */
2241 coord_dst.writemask = WRITEMASK_W;
2242 emit(ir, OPCODE_MOV, coord_dst, lod_info);
2243 coord_dst.writemask = WRITEMASK_XYZW;
2244 }
2245
2246 if (opcode == OPCODE_TXD)
2247 inst = emit(ir, opcode, result_dst, coord, dx, dy);
2248 else
2249 inst = emit(ir, opcode, result_dst, coord);
2250
2251 if (ir->shadow_comparitor)
2252 inst->tex_shadow = GL_TRUE;
2253
2254 inst->sampler = _mesa_get_sampler_uniform_value(ir->sampler,
2255 this->shader_program,
2256 this->prog);
2257
2258 switch (sampler_type->sampler_dimensionality) {
2259 case GLSL_SAMPLER_DIM_1D:
2260 inst->tex_target = (sampler_type->sampler_array)
2261 ? TEXTURE_1D_ARRAY_INDEX : TEXTURE_1D_INDEX;
2262 break;
2263 case GLSL_SAMPLER_DIM_2D:
2264 inst->tex_target = (sampler_type->sampler_array)
2265 ? TEXTURE_2D_ARRAY_INDEX : TEXTURE_2D_INDEX;
2266 break;
2267 case GLSL_SAMPLER_DIM_3D:
2268 inst->tex_target = TEXTURE_3D_INDEX;
2269 break;
2270 case GLSL_SAMPLER_DIM_CUBE:
2271 inst->tex_target = TEXTURE_CUBE_INDEX;
2272 break;
2273 case GLSL_SAMPLER_DIM_RECT:
2274 inst->tex_target = TEXTURE_RECT_INDEX;
2275 break;
2276 case GLSL_SAMPLER_DIM_BUF:
2277 assert(!"FINISHME: Implement ARB_texture_buffer_object");
2278 break;
2279 default:
2280 assert(!"Should not get here.");
2281 }
2282
2283 this->result = result_src;
2284 }
2285
2286 void
2287 ir_to_mesa_visitor::visit(ir_return *ir)
2288 {
2289 if (ir->get_value()) {
2290 dst_reg l;
2291 int i;
2292
2293 assert(current_function);
2294
2295 ir->get_value()->accept(this);
2296 src_reg r = this->result;
2297
2298 l = dst_reg(current_function->return_reg);
2299
2300 for (i = 0; i < type_size(current_function->sig->return_type); i++) {
2301 emit(ir, OPCODE_MOV, l, r);
2302 l.index++;
2303 r.index++;
2304 }
2305 }
2306
2307 emit(ir, OPCODE_RET);
2308 }
2309
2310 void
2311 ir_to_mesa_visitor::visit(ir_discard *ir)
2312 {
2313 struct gl_fragment_program *fp = (struct gl_fragment_program *)this->prog;
2314
2315 if (ir->condition) {
2316 ir->condition->accept(this);
2317 this->result.negate = ~this->result.negate;
2318 emit(ir, OPCODE_KIL, undef_dst, this->result);
2319 } else {
2320 emit(ir, OPCODE_KIL_NV);
2321 }
2322
2323 fp->UsesKill = GL_TRUE;
2324 }
2325
2326 void
2327 ir_to_mesa_visitor::visit(ir_if *ir)
2328 {
2329 ir_to_mesa_instruction *cond_inst, *if_inst;
2330 ir_to_mesa_instruction *prev_inst;
2331
2332 prev_inst = (ir_to_mesa_instruction *)this->instructions.get_tail();
2333
2334 ir->condition->accept(this);
2335 assert(this->result.file != PROGRAM_UNDEFINED);
2336
2337 if (this->options->EmitCondCodes) {
2338 cond_inst = (ir_to_mesa_instruction *)this->instructions.get_tail();
2339
2340 /* See if we actually generated any instruction for generating
2341 * the condition. If not, then cook up a move to a temp so we
2342 * have something to set cond_update on.
2343 */
2344 if (cond_inst == prev_inst) {
2345 src_reg temp = get_temp(glsl_type::bool_type);
2346 cond_inst = emit(ir->condition, OPCODE_MOV, dst_reg(temp), result);
2347 }
2348 cond_inst->cond_update = GL_TRUE;
2349
2350 if_inst = emit(ir->condition, OPCODE_IF);
2351 if_inst->dst.cond_mask = COND_NE;
2352 } else {
2353 if_inst = emit(ir->condition, OPCODE_IF, undef_dst, this->result);
2354 }
2355
2356 this->instructions.push_tail(if_inst);
2357
2358 visit_exec_list(&ir->then_instructions, this);
2359
2360 if (!ir->else_instructions.is_empty()) {
2361 emit(ir->condition, OPCODE_ELSE);
2362 visit_exec_list(&ir->else_instructions, this);
2363 }
2364
2365 if_inst = emit(ir->condition, OPCODE_ENDIF);
2366 }
2367
2368 ir_to_mesa_visitor::ir_to_mesa_visitor()
2369 {
2370 result.file = PROGRAM_UNDEFINED;
2371 next_temp = 1;
2372 next_signature_id = 1;
2373 current_function = NULL;
2374 mem_ctx = ralloc_context(NULL);
2375 }
2376
2377 ir_to_mesa_visitor::~ir_to_mesa_visitor()
2378 {
2379 ralloc_free(mem_ctx);
2380 }
2381
2382 static struct prog_src_register
2383 mesa_src_reg_from_ir_src_reg(src_reg reg)
2384 {
2385 struct prog_src_register mesa_reg;
2386
2387 mesa_reg.File = reg.file;
2388 assert(reg.index < (1 << INST_INDEX_BITS));
2389 mesa_reg.Index = reg.index;
2390 mesa_reg.Swizzle = reg.swizzle;
2391 mesa_reg.RelAddr = reg.reladdr != NULL;
2392 mesa_reg.Negate = reg.negate;
2393 mesa_reg.Abs = 0;
2394 mesa_reg.HasIndex2 = GL_FALSE;
2395 mesa_reg.RelAddr2 = 0;
2396 mesa_reg.Index2 = 0;
2397
2398 return mesa_reg;
2399 }
2400
2401 static void
2402 set_branchtargets(ir_to_mesa_visitor *v,
2403 struct prog_instruction *mesa_instructions,
2404 int num_instructions)
2405 {
2406 int if_count = 0, loop_count = 0;
2407 int *if_stack, *loop_stack;
2408 int if_stack_pos = 0, loop_stack_pos = 0;
2409 int i, j;
2410
2411 for (i = 0; i < num_instructions; i++) {
2412 switch (mesa_instructions[i].Opcode) {
2413 case OPCODE_IF:
2414 if_count++;
2415 break;
2416 case OPCODE_BGNLOOP:
2417 loop_count++;
2418 break;
2419 case OPCODE_BRK:
2420 case OPCODE_CONT:
2421 mesa_instructions[i].BranchTarget = -1;
2422 break;
2423 default:
2424 break;
2425 }
2426 }
2427
2428 if_stack = rzalloc_array(v->mem_ctx, int, if_count);
2429 loop_stack = rzalloc_array(v->mem_ctx, int, loop_count);
2430
2431 for (i = 0; i < num_instructions; i++) {
2432 switch (mesa_instructions[i].Opcode) {
2433 case OPCODE_IF:
2434 if_stack[if_stack_pos] = i;
2435 if_stack_pos++;
2436 break;
2437 case OPCODE_ELSE:
2438 mesa_instructions[if_stack[if_stack_pos - 1]].BranchTarget = i;
2439 if_stack[if_stack_pos - 1] = i;
2440 break;
2441 case OPCODE_ENDIF:
2442 mesa_instructions[if_stack[if_stack_pos - 1]].BranchTarget = i;
2443 if_stack_pos--;
2444 break;
2445 case OPCODE_BGNLOOP:
2446 loop_stack[loop_stack_pos] = i;
2447 loop_stack_pos++;
2448 break;
2449 case OPCODE_ENDLOOP:
2450 loop_stack_pos--;
2451 /* Rewrite any breaks/conts at this nesting level (haven't
2452 * already had a BranchTarget assigned) to point to the end
2453 * of the loop.
2454 */
2455 for (j = loop_stack[loop_stack_pos]; j < i; j++) {
2456 if (mesa_instructions[j].Opcode == OPCODE_BRK ||
2457 mesa_instructions[j].Opcode == OPCODE_CONT) {
2458 if (mesa_instructions[j].BranchTarget == -1) {
2459 mesa_instructions[j].BranchTarget = i;
2460 }
2461 }
2462 }
2463 /* The loop ends point at each other. */
2464 mesa_instructions[i].BranchTarget = loop_stack[loop_stack_pos];
2465 mesa_instructions[loop_stack[loop_stack_pos]].BranchTarget = i;
2466 break;
2467 case OPCODE_CAL:
2468 foreach_iter(exec_list_iterator, iter, v->function_signatures) {
2469 function_entry *entry = (function_entry *)iter.get();
2470
2471 if (entry->sig_id == mesa_instructions[i].BranchTarget) {
2472 mesa_instructions[i].BranchTarget = entry->inst;
2473 break;
2474 }
2475 }
2476 break;
2477 default:
2478 break;
2479 }
2480 }
2481 }
2482
2483 static void
2484 print_program(struct prog_instruction *mesa_instructions,
2485 ir_instruction **mesa_instruction_annotation,
2486 int num_instructions)
2487 {
2488 ir_instruction *last_ir = NULL;
2489 int i;
2490 int indent = 0;
2491
2492 for (i = 0; i < num_instructions; i++) {
2493 struct prog_instruction *mesa_inst = mesa_instructions + i;
2494 ir_instruction *ir = mesa_instruction_annotation[i];
2495
2496 fprintf(stdout, "%3d: ", i);
2497
2498 if (last_ir != ir && ir) {
2499 int j;
2500
2501 for (j = 0; j < indent; j++) {
2502 fprintf(stdout, " ");
2503 }
2504 ir->print();
2505 printf("\n");
2506 last_ir = ir;
2507
2508 fprintf(stdout, " "); /* line number spacing. */
2509 }
2510
2511 indent = _mesa_fprint_instruction_opt(stdout, mesa_inst, indent,
2512 PROG_PRINT_DEBUG, NULL);
2513 }
2514 }
2515
2516
2517 /**
2518 * Count resources used by the given gpu program (number of texture
2519 * samplers, etc).
2520 */
2521 static void
2522 count_resources(struct gl_program *prog)
2523 {
2524 unsigned int i;
2525
2526 prog->SamplersUsed = 0;
2527
2528 for (i = 0; i < prog->NumInstructions; i++) {
2529 struct prog_instruction *inst = &prog->Instructions[i];
2530
2531 if (_mesa_is_tex_instruction(inst->Opcode)) {
2532 prog->SamplerTargets[inst->TexSrcUnit] =
2533 (gl_texture_index)inst->TexSrcTarget;
2534 prog->SamplersUsed |= 1 << inst->TexSrcUnit;
2535 if (inst->TexShadow) {
2536 prog->ShadowSamplers |= 1 << inst->TexSrcUnit;
2537 }
2538 }
2539 }
2540
2541 _mesa_update_shader_textures_used(prog);
2542 }
2543
2544
2545 /**
2546 * Check if the given vertex/fragment/shader program is within the
2547 * resource limits of the context (number of texture units, etc).
2548 * If any of those checks fail, record a linker error.
2549 *
2550 * XXX more checks are needed...
2551 */
2552 static void
2553 check_resources(const struct gl_context *ctx,
2554 struct gl_shader_program *shader_program,
2555 struct gl_program *prog)
2556 {
2557 switch (prog->Target) {
2558 case GL_VERTEX_PROGRAM_ARB:
2559 if (_mesa_bitcount(prog->SamplersUsed) >
2560 ctx->Const.MaxVertexTextureImageUnits) {
2561 linker_error(shader_program,
2562 "Too many vertex shader texture samplers");
2563 }
2564 if (prog->Parameters->NumParameters > MAX_UNIFORMS) {
2565 linker_error(shader_program, "Too many vertex shader constants");
2566 }
2567 break;
2568 case MESA_GEOMETRY_PROGRAM:
2569 if (_mesa_bitcount(prog->SamplersUsed) >
2570 ctx->Const.MaxGeometryTextureImageUnits) {
2571 linker_error(shader_program,
2572 "Too many geometry shader texture samplers");
2573 }
2574 if (prog->Parameters->NumParameters >
2575 MAX_GEOMETRY_UNIFORM_COMPONENTS / 4) {
2576 linker_error(shader_program, "Too many geometry shader constants");
2577 }
2578 break;
2579 case GL_FRAGMENT_PROGRAM_ARB:
2580 if (_mesa_bitcount(prog->SamplersUsed) >
2581 ctx->Const.MaxTextureImageUnits) {
2582 linker_error(shader_program,
2583 "Too many fragment shader texture samplers");
2584 }
2585 if (prog->Parameters->NumParameters > MAX_UNIFORMS) {
2586 linker_error(shader_program, "Too many fragment shader constants");
2587 }
2588 break;
2589 default:
2590 _mesa_problem(ctx, "unexpected program type in check_resources()");
2591 }
2592 }
2593
2594
2595
2596 struct uniform_sort {
2597 struct gl_uniform *u;
2598 int pos;
2599 };
2600
2601 /* The shader_program->Uniforms list is almost sorted in increasing
2602 * uniform->{Frag,Vert}Pos locations, but not quite when there are
2603 * uniforms shared between targets. We need to add parameters in
2604 * increasing order for the targets.
2605 */
2606 static int
2607 sort_uniforms(const void *a, const void *b)
2608 {
2609 struct uniform_sort *u1 = (struct uniform_sort *)a;
2610 struct uniform_sort *u2 = (struct uniform_sort *)b;
2611
2612 return u1->pos - u2->pos;
2613 }
2614
2615 /* Add the uniforms to the parameters. The linker chose locations
2616 * in our parameters lists (which weren't created yet), which the
2617 * uniforms code will use to poke values into our parameters list
2618 * when uniforms are updated.
2619 */
2620 static void
2621 add_uniforms_to_parameters_list(struct gl_shader_program *shader_program,
2622 struct gl_shader *shader,
2623 struct gl_program *prog)
2624 {
2625 unsigned int i;
2626 unsigned int next_sampler = 0, num_uniforms = 0;
2627 struct uniform_sort *sorted_uniforms;
2628
2629 sorted_uniforms = ralloc_array(NULL, struct uniform_sort,
2630 shader_program->Uniforms->NumUniforms);
2631
2632 for (i = 0; i < shader_program->Uniforms->NumUniforms; i++) {
2633 struct gl_uniform *uniform = shader_program->Uniforms->Uniforms + i;
2634 int parameter_index = -1;
2635
2636 switch (shader->Type) {
2637 case GL_VERTEX_SHADER:
2638 parameter_index = uniform->VertPos;
2639 break;
2640 case GL_FRAGMENT_SHADER:
2641 parameter_index = uniform->FragPos;
2642 break;
2643 case GL_GEOMETRY_SHADER:
2644 parameter_index = uniform->GeomPos;
2645 break;
2646 }
2647
2648 /* Only add uniforms used in our target. */
2649 if (parameter_index != -1) {
2650 sorted_uniforms[num_uniforms].pos = parameter_index;
2651 sorted_uniforms[num_uniforms].u = uniform;
2652 num_uniforms++;
2653 }
2654 }
2655
2656 qsort(sorted_uniforms, num_uniforms, sizeof(struct uniform_sort),
2657 sort_uniforms);
2658
2659 for (i = 0; i < num_uniforms; i++) {
2660 struct gl_uniform *uniform = sorted_uniforms[i].u;
2661 int parameter_index = sorted_uniforms[i].pos;
2662 const glsl_type *type = uniform->Type;
2663 unsigned int size;
2664
2665 if (type->is_vector() ||
2666 type->is_scalar()) {
2667 size = type->vector_elements;
2668 } else {
2669 size = type_size(type) * 4;
2670 }
2671
2672 gl_register_file file;
2673 if (type->is_sampler() ||
2674 (type->is_array() && type->fields.array->is_sampler())) {
2675 file = PROGRAM_SAMPLER;
2676 } else {
2677 file = PROGRAM_UNIFORM;
2678 }
2679
2680 GLint index = _mesa_lookup_parameter_index(prog->Parameters, -1,
2681 uniform->Name);
2682
2683 if (index < 0) {
2684 index = _mesa_add_parameter(prog->Parameters, file,
2685 uniform->Name, size, type->gl_type,
2686 NULL, NULL, 0x0);
2687
2688 /* Sampler uniform values are stored in prog->SamplerUnits,
2689 * and the entry in that array is selected by this index we
2690 * store in ParameterValues[].
2691 */
2692 if (file == PROGRAM_SAMPLER) {
2693 for (unsigned int j = 0; j < size / 4; j++)
2694 prog->Parameters->ParameterValues[index + j][0].f = next_sampler++;
2695 }
2696
2697 /* The location chosen in the Parameters list here (returned
2698 * from _mesa_add_uniform) has to match what the linker chose.
2699 */
2700 if (index != parameter_index) {
2701 linker_error(shader_program,
2702 "Allocation of uniform `%s' to target failed "
2703 "(%d vs %d)\n",
2704 uniform->Name, index, parameter_index);
2705 }
2706 }
2707 }
2708
2709 ralloc_free(sorted_uniforms);
2710 }
2711
2712 static void
2713 set_uniform_initializer(struct gl_context *ctx, void *mem_ctx,
2714 struct gl_shader_program *shader_program,
2715 const char *name, const glsl_type *type,
2716 ir_constant *val)
2717 {
2718 if (type->is_record()) {
2719 ir_constant *field_constant;
2720
2721 field_constant = (ir_constant *)val->components.get_head();
2722
2723 for (unsigned int i = 0; i < type->length; i++) {
2724 const glsl_type *field_type = type->fields.structure[i].type;
2725 const char *field_name = ralloc_asprintf(mem_ctx, "%s.%s", name,
2726 type->fields.structure[i].name);
2727 set_uniform_initializer(ctx, mem_ctx, shader_program, field_name,
2728 field_type, field_constant);
2729 field_constant = (ir_constant *)field_constant->next;
2730 }
2731 return;
2732 }
2733
2734 int loc = _mesa_get_uniform_location(ctx, shader_program, name);
2735
2736 if (loc == -1) {
2737 linker_error(shader_program,
2738 "Couldn't find uniform for initializer %s\n", name);
2739 return;
2740 }
2741
2742 for (unsigned int i = 0; i < (type->is_array() ? type->length : 1); i++) {
2743 ir_constant *element;
2744 const glsl_type *element_type;
2745 if (type->is_array()) {
2746 element = val->array_elements[i];
2747 element_type = type->fields.array;
2748 } else {
2749 element = val;
2750 element_type = type;
2751 }
2752
2753 void *values;
2754
2755 if (element_type->base_type == GLSL_TYPE_BOOL) {
2756 int *conv = ralloc_array(mem_ctx, int, element_type->components());
2757 for (unsigned int j = 0; j < element_type->components(); j++) {
2758 conv[j] = element->value.b[j];
2759 }
2760 values = (void *)conv;
2761 element_type = glsl_type::get_instance(GLSL_TYPE_INT,
2762 element_type->vector_elements,
2763 1);
2764 } else {
2765 values = &element->value;
2766 }
2767
2768 if (element_type->is_matrix()) {
2769 _mesa_uniform_matrix(ctx, shader_program,
2770 element_type->matrix_columns,
2771 element_type->vector_elements,
2772 loc, 1, GL_FALSE, (GLfloat *)values);
2773 loc += element_type->matrix_columns;
2774 } else {
2775 _mesa_uniform(ctx, shader_program, loc, element_type->matrix_columns,
2776 values, element_type->gl_type);
2777 loc += type_size(element_type);
2778 }
2779 }
2780 }
2781
2782 static void
2783 set_uniform_initializers(struct gl_context *ctx,
2784 struct gl_shader_program *shader_program)
2785 {
2786 void *mem_ctx = NULL;
2787
2788 for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
2789 struct gl_shader *shader = shader_program->_LinkedShaders[i];
2790
2791 if (shader == NULL)
2792 continue;
2793
2794 foreach_iter(exec_list_iterator, iter, *shader->ir) {
2795 ir_instruction *ir = (ir_instruction *)iter.get();
2796 ir_variable *var = ir->as_variable();
2797
2798 if (!var || var->mode != ir_var_uniform || !var->constant_value)
2799 continue;
2800
2801 if (!mem_ctx)
2802 mem_ctx = ralloc_context(NULL);
2803
2804 set_uniform_initializer(ctx, mem_ctx, shader_program, var->name,
2805 var->type, var->constant_value);
2806 }
2807 }
2808
2809 ralloc_free(mem_ctx);
2810 }
2811
2812 /*
2813 * On a basic block basis, tracks available PROGRAM_TEMPORARY register
2814 * channels for copy propagation and updates following instructions to
2815 * use the original versions.
2816 *
2817 * The ir_to_mesa_visitor lazily produces code assuming that this pass
2818 * will occur. As an example, a TXP production before this pass:
2819 *
2820 * 0: MOV TEMP[1], INPUT[4].xyyy;
2821 * 1: MOV TEMP[1].w, INPUT[4].wwww;
2822 * 2: TXP TEMP[2], TEMP[1], texture[0], 2D;
2823 *
2824 * and after:
2825 *
2826 * 0: MOV TEMP[1], INPUT[4].xyyy;
2827 * 1: MOV TEMP[1].w, INPUT[4].wwww;
2828 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
2829 *
2830 * which allows for dead code elimination on TEMP[1]'s writes.
2831 */
2832 void
2833 ir_to_mesa_visitor::copy_propagate(void)
2834 {
2835 ir_to_mesa_instruction **acp = rzalloc_array(mem_ctx,
2836 ir_to_mesa_instruction *,
2837 this->next_temp * 4);
2838 int *acp_level = rzalloc_array(mem_ctx, int, this->next_temp * 4);
2839 int level = 0;
2840
2841 foreach_iter(exec_list_iterator, iter, this->instructions) {
2842 ir_to_mesa_instruction *inst = (ir_to_mesa_instruction *)iter.get();
2843
2844 assert(inst->dst.file != PROGRAM_TEMPORARY
2845 || inst->dst.index < this->next_temp);
2846
2847 /* First, do any copy propagation possible into the src regs. */
2848 for (int r = 0; r < 3; r++) {
2849 ir_to_mesa_instruction *first = NULL;
2850 bool good = true;
2851 int acp_base = inst->src[r].index * 4;
2852
2853 if (inst->src[r].file != PROGRAM_TEMPORARY ||
2854 inst->src[r].reladdr)
2855 continue;
2856
2857 /* See if we can find entries in the ACP consisting of MOVs
2858 * from the same src register for all the swizzled channels
2859 * of this src register reference.
2860 */
2861 for (int i = 0; i < 4; i++) {
2862 int src_chan = GET_SWZ(inst->src[r].swizzle, i);
2863 ir_to_mesa_instruction *copy_chan = acp[acp_base + src_chan];
2864
2865 if (!copy_chan) {
2866 good = false;
2867 break;
2868 }
2869
2870 assert(acp_level[acp_base + src_chan] <= level);
2871
2872 if (!first) {
2873 first = copy_chan;
2874 } else {
2875 if (first->src[0].file != copy_chan->src[0].file ||
2876 first->src[0].index != copy_chan->src[0].index) {
2877 good = false;
2878 break;
2879 }
2880 }
2881 }
2882
2883 if (good) {
2884 /* We've now validated that we can copy-propagate to
2885 * replace this src register reference. Do it.
2886 */
2887 inst->src[r].file = first->src[0].file;
2888 inst->src[r].index = first->src[0].index;
2889
2890 int swizzle = 0;
2891 for (int i = 0; i < 4; i++) {
2892 int src_chan = GET_SWZ(inst->src[r].swizzle, i);
2893 ir_to_mesa_instruction *copy_inst = acp[acp_base + src_chan];
2894 swizzle |= (GET_SWZ(copy_inst->src[0].swizzle, src_chan) <<
2895 (3 * i));
2896 }
2897 inst->src[r].swizzle = swizzle;
2898 }
2899 }
2900
2901 switch (inst->op) {
2902 case OPCODE_BGNLOOP:
2903 case OPCODE_ENDLOOP:
2904 /* End of a basic block, clear the ACP entirely. */
2905 memset(acp, 0, sizeof(*acp) * this->next_temp * 4);
2906 break;
2907
2908 case OPCODE_IF:
2909 ++level;
2910 break;
2911
2912 case OPCODE_ENDIF:
2913 case OPCODE_ELSE:
2914 /* Clear all channels written inside the block from the ACP, but
2915 * leaving those that were not touched.
2916 */
2917 for (int r = 0; r < this->next_temp; r++) {
2918 for (int c = 0; c < 4; c++) {
2919 if (!acp[4 * r + c])
2920 continue;
2921
2922 if (acp_level[4 * r + c] >= level)
2923 acp[4 * r + c] = NULL;
2924 }
2925 }
2926 if (inst->op == OPCODE_ENDIF)
2927 --level;
2928 break;
2929
2930 default:
2931 /* Continuing the block, clear any written channels from
2932 * the ACP.
2933 */
2934 if (inst->dst.file == PROGRAM_TEMPORARY && inst->dst.reladdr) {
2935 /* Any temporary might be written, so no copy propagation
2936 * across this instruction.
2937 */
2938 memset(acp, 0, sizeof(*acp) * this->next_temp * 4);
2939 } else if (inst->dst.file == PROGRAM_OUTPUT &&
2940 inst->dst.reladdr) {
2941 /* Any output might be written, so no copy propagation
2942 * from outputs across this instruction.
2943 */
2944 for (int r = 0; r < this->next_temp; r++) {
2945 for (int c = 0; c < 4; c++) {
2946 if (!acp[4 * r + c])
2947 continue;
2948
2949 if (acp[4 * r + c]->src[0].file == PROGRAM_OUTPUT)
2950 acp[4 * r + c] = NULL;
2951 }
2952 }
2953 } else if (inst->dst.file == PROGRAM_TEMPORARY ||
2954 inst->dst.file == PROGRAM_OUTPUT) {
2955 /* Clear where it's used as dst. */
2956 if (inst->dst.file == PROGRAM_TEMPORARY) {
2957 for (int c = 0; c < 4; c++) {
2958 if (inst->dst.writemask & (1 << c)) {
2959 acp[4 * inst->dst.index + c] = NULL;
2960 }
2961 }
2962 }
2963
2964 /* Clear where it's used as src. */
2965 for (int r = 0; r < this->next_temp; r++) {
2966 for (int c = 0; c < 4; c++) {
2967 if (!acp[4 * r + c])
2968 continue;
2969
2970 int src_chan = GET_SWZ(acp[4 * r + c]->src[0].swizzle, c);
2971
2972 if (acp[4 * r + c]->src[0].file == inst->dst.file &&
2973 acp[4 * r + c]->src[0].index == inst->dst.index &&
2974 inst->dst.writemask & (1 << src_chan))
2975 {
2976 acp[4 * r + c] = NULL;
2977 }
2978 }
2979 }
2980 }
2981 break;
2982 }
2983
2984 /* If this is a copy, add it to the ACP. */
2985 if (inst->op == OPCODE_MOV &&
2986 inst->dst.file == PROGRAM_TEMPORARY &&
2987 !inst->dst.reladdr &&
2988 !inst->saturate &&
2989 !inst->src[0].reladdr &&
2990 !inst->src[0].negate) {
2991 for (int i = 0; i < 4; i++) {
2992 if (inst->dst.writemask & (1 << i)) {
2993 acp[4 * inst->dst.index + i] = inst;
2994 acp_level[4 * inst->dst.index + i] = level;
2995 }
2996 }
2997 }
2998 }
2999
3000 ralloc_free(acp_level);
3001 ralloc_free(acp);
3002 }
3003
3004
3005 /**
3006 * Convert a shader's GLSL IR into a Mesa gl_program.
3007 */
3008 static struct gl_program *
3009 get_mesa_program(struct gl_context *ctx,
3010 struct gl_shader_program *shader_program,
3011 struct gl_shader *shader)
3012 {
3013 ir_to_mesa_visitor v;
3014 struct prog_instruction *mesa_instructions, *mesa_inst;
3015 ir_instruction **mesa_instruction_annotation;
3016 int i;
3017 struct gl_program *prog;
3018 GLenum target;
3019 const char *target_string;
3020 GLboolean progress;
3021 struct gl_shader_compiler_options *options =
3022 &ctx->ShaderCompilerOptions[_mesa_shader_type_to_index(shader->Type)];
3023
3024 switch (shader->Type) {
3025 case GL_VERTEX_SHADER:
3026 target = GL_VERTEX_PROGRAM_ARB;
3027 target_string = "vertex";
3028 break;
3029 case GL_FRAGMENT_SHADER:
3030 target = GL_FRAGMENT_PROGRAM_ARB;
3031 target_string = "fragment";
3032 break;
3033 case GL_GEOMETRY_SHADER:
3034 target = GL_GEOMETRY_PROGRAM_NV;
3035 target_string = "geometry";
3036 break;
3037 default:
3038 assert(!"should not be reached");
3039 return NULL;
3040 }
3041
3042 validate_ir_tree(shader->ir);
3043
3044 prog = ctx->Driver.NewProgram(ctx, target, shader_program->Name);
3045 if (!prog)
3046 return NULL;
3047 prog->Parameters = _mesa_new_parameter_list();
3048 prog->Attributes = _mesa_new_parameter_list();
3049 v.ctx = ctx;
3050 v.prog = prog;
3051 v.shader_program = shader_program;
3052 v.options = options;
3053
3054 add_uniforms_to_parameters_list(shader_program, shader, prog);
3055
3056 /* Emit Mesa IR for main(). */
3057 visit_exec_list(shader->ir, &v);
3058 v.emit(NULL, OPCODE_END);
3059
3060 /* Now emit bodies for any functions that were used. */
3061 do {
3062 progress = GL_FALSE;
3063
3064 foreach_iter(exec_list_iterator, iter, v.function_signatures) {
3065 function_entry *entry = (function_entry *)iter.get();
3066
3067 if (!entry->bgn_inst) {
3068 v.current_function = entry;
3069
3070 entry->bgn_inst = v.emit(NULL, OPCODE_BGNSUB);
3071 entry->bgn_inst->function = entry;
3072
3073 visit_exec_list(&entry->sig->body, &v);
3074
3075 ir_to_mesa_instruction *last;
3076 last = (ir_to_mesa_instruction *)v.instructions.get_tail();
3077 if (last->op != OPCODE_RET)
3078 v.emit(NULL, OPCODE_RET);
3079
3080 ir_to_mesa_instruction *end;
3081 end = v.emit(NULL, OPCODE_ENDSUB);
3082 end->function = entry;
3083
3084 progress = GL_TRUE;
3085 }
3086 }
3087 } while (progress);
3088
3089 prog->NumTemporaries = v.next_temp;
3090
3091 int num_instructions = 0;
3092 foreach_iter(exec_list_iterator, iter, v.instructions) {
3093 num_instructions++;
3094 }
3095
3096 mesa_instructions =
3097 (struct prog_instruction *)calloc(num_instructions,
3098 sizeof(*mesa_instructions));
3099 mesa_instruction_annotation = ralloc_array(v.mem_ctx, ir_instruction *,
3100 num_instructions);
3101
3102 v.copy_propagate();
3103
3104 /* Convert ir_mesa_instructions into prog_instructions.
3105 */
3106 mesa_inst = mesa_instructions;
3107 i = 0;
3108 foreach_iter(exec_list_iterator, iter, v.instructions) {
3109 const ir_to_mesa_instruction *inst = (ir_to_mesa_instruction *)iter.get();
3110
3111 mesa_inst->Opcode = inst->op;
3112 mesa_inst->CondUpdate = inst->cond_update;
3113 if (inst->saturate)
3114 mesa_inst->SaturateMode = SATURATE_ZERO_ONE;
3115 mesa_inst->DstReg.File = inst->dst.file;
3116 mesa_inst->DstReg.Index = inst->dst.index;
3117 mesa_inst->DstReg.CondMask = inst->dst.cond_mask;
3118 mesa_inst->DstReg.WriteMask = inst->dst.writemask;
3119 mesa_inst->DstReg.RelAddr = inst->dst.reladdr != NULL;
3120 mesa_inst->SrcReg[0] = mesa_src_reg_from_ir_src_reg(inst->src[0]);
3121 mesa_inst->SrcReg[1] = mesa_src_reg_from_ir_src_reg(inst->src[1]);
3122 mesa_inst->SrcReg[2] = mesa_src_reg_from_ir_src_reg(inst->src[2]);
3123 mesa_inst->TexSrcUnit = inst->sampler;
3124 mesa_inst->TexSrcTarget = inst->tex_target;
3125 mesa_inst->TexShadow = inst->tex_shadow;
3126 mesa_instruction_annotation[i] = inst->ir;
3127
3128 /* Set IndirectRegisterFiles. */
3129 if (mesa_inst->DstReg.RelAddr)
3130 prog->IndirectRegisterFiles |= 1 << mesa_inst->DstReg.File;
3131
3132 /* Update program's bitmask of indirectly accessed register files */
3133 for (unsigned src = 0; src < 3; src++)
3134 if (mesa_inst->SrcReg[src].RelAddr)
3135 prog->IndirectRegisterFiles |= 1 << mesa_inst->SrcReg[src].File;
3136
3137 switch (mesa_inst->Opcode) {
3138 case OPCODE_IF:
3139 if (options->MaxIfDepth == 0) {
3140 linker_warning(shader_program,
3141 "Couldn't flatten if-statement. "
3142 "This will likely result in software "
3143 "rasterization.\n");
3144 }
3145 break;
3146 case OPCODE_BGNLOOP:
3147 if (options->EmitNoLoops) {
3148 linker_warning(shader_program,
3149 "Couldn't unroll loop. "
3150 "This will likely result in software "
3151 "rasterization.\n");
3152 }
3153 break;
3154 case OPCODE_CONT:
3155 if (options->EmitNoCont) {
3156 linker_warning(shader_program,
3157 "Couldn't lower continue-statement. "
3158 "This will likely result in software "
3159 "rasterization.\n");
3160 }
3161 break;
3162 case OPCODE_BGNSUB:
3163 inst->function->inst = i;
3164 mesa_inst->Comment = strdup(inst->function->sig->function_name());
3165 break;
3166 case OPCODE_ENDSUB:
3167 mesa_inst->Comment = strdup(inst->function->sig->function_name());
3168 break;
3169 case OPCODE_CAL:
3170 mesa_inst->BranchTarget = inst->function->sig_id; /* rewritten later */
3171 break;
3172 case OPCODE_ARL:
3173 prog->NumAddressRegs = 1;
3174 break;
3175 default:
3176 break;
3177 }
3178
3179 mesa_inst++;
3180 i++;
3181
3182 if (!shader_program->LinkStatus)
3183 break;
3184 }
3185
3186 if (!shader_program->LinkStatus) {
3187 free(mesa_instructions);
3188 _mesa_reference_program(ctx, &shader->Program, NULL);
3189 return NULL;
3190 }
3191
3192 set_branchtargets(&v, mesa_instructions, num_instructions);
3193
3194 if (ctx->Shader.Flags & GLSL_DUMP) {
3195 printf("\n");
3196 printf("GLSL IR for linked %s program %d:\n", target_string,
3197 shader_program->Name);
3198 _mesa_print_ir(shader->ir, NULL);
3199 printf("\n");
3200 printf("\n");
3201 printf("Mesa IR for linked %s program %d:\n", target_string,
3202 shader_program->Name);
3203 print_program(mesa_instructions, mesa_instruction_annotation,
3204 num_instructions);
3205 }
3206
3207 prog->Instructions = mesa_instructions;
3208 prog->NumInstructions = num_instructions;
3209
3210 do_set_program_inouts(shader->ir, prog);
3211 count_resources(prog);
3212
3213 check_resources(ctx, shader_program, prog);
3214
3215 _mesa_reference_program(ctx, &shader->Program, prog);
3216
3217 if ((ctx->Shader.Flags & GLSL_NO_OPT) == 0) {
3218 _mesa_optimize_program(ctx, prog);
3219 }
3220
3221 return prog;
3222 }
3223
3224 extern "C" {
3225
3226 /**
3227 * Link a shader.
3228 * Called via ctx->Driver.LinkShader()
3229 * This actually involves converting GLSL IR into Mesa gl_programs with
3230 * code lowering and other optimizations.
3231 */
3232 GLboolean
3233 _mesa_ir_link_shader(struct gl_context *ctx, struct gl_shader_program *prog)
3234 {
3235 assert(prog->LinkStatus);
3236
3237 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
3238 if (prog->_LinkedShaders[i] == NULL)
3239 continue;
3240
3241 bool progress;
3242 exec_list *ir = prog->_LinkedShaders[i]->ir;
3243 const struct gl_shader_compiler_options *options =
3244 &ctx->ShaderCompilerOptions[_mesa_shader_type_to_index(prog->_LinkedShaders[i]->Type)];
3245
3246 do {
3247 progress = false;
3248
3249 /* Lowering */
3250 do_mat_op_to_vec(ir);
3251 lower_instructions(ir, (MOD_TO_FRACT | DIV_TO_MUL_RCP | EXP_TO_EXP2
3252 | LOG_TO_LOG2 | INT_DIV_TO_MUL_RCP
3253 | ((options->EmitNoPow) ? POW_TO_EXP2 : 0)));
3254
3255 progress = do_lower_jumps(ir, true, true, options->EmitNoMainReturn, options->EmitNoCont, options->EmitNoLoops) || progress;
3256
3257 progress = do_common_optimization(ir, true, options->MaxUnrollIterations) || progress;
3258
3259 progress = lower_quadop_vector(ir, true) || progress;
3260
3261 if (options->MaxIfDepth == 0)
3262 progress = lower_discard(ir) || progress;
3263
3264 progress = lower_if_to_cond_assign(ir, options->MaxIfDepth) || progress;
3265
3266 if (options->EmitNoNoise)
3267 progress = lower_noise(ir) || progress;
3268
3269 /* If there are forms of indirect addressing that the driver
3270 * cannot handle, perform the lowering pass.
3271 */
3272 if (options->EmitNoIndirectInput || options->EmitNoIndirectOutput
3273 || options->EmitNoIndirectTemp || options->EmitNoIndirectUniform)
3274 progress =
3275 lower_variable_index_to_cond_assign(ir,
3276 options->EmitNoIndirectInput,
3277 options->EmitNoIndirectOutput,
3278 options->EmitNoIndirectTemp,
3279 options->EmitNoIndirectUniform)
3280 || progress;
3281
3282 progress = do_vec_index_to_cond_assign(ir) || progress;
3283 } while (progress);
3284
3285 validate_ir_tree(ir);
3286 }
3287
3288 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
3289 struct gl_program *linked_prog;
3290
3291 if (prog->_LinkedShaders[i] == NULL)
3292 continue;
3293
3294 linked_prog = get_mesa_program(ctx, prog, prog->_LinkedShaders[i]);
3295
3296 if (linked_prog) {
3297 bool ok = true;
3298
3299 switch (prog->_LinkedShaders[i]->Type) {
3300 case GL_VERTEX_SHADER:
3301 ((struct gl_vertex_program *)linked_prog)->UsesClipDistance
3302 = prog->Vert.UsesClipDistance;
3303 _mesa_reference_vertprog(ctx, &prog->VertexProgram,
3304 (struct gl_vertex_program *)linked_prog);
3305 ok = ctx->Driver.ProgramStringNotify(ctx, GL_VERTEX_PROGRAM_ARB,
3306 linked_prog);
3307 break;
3308 case GL_FRAGMENT_SHADER:
3309 _mesa_reference_fragprog(ctx, &prog->FragmentProgram,
3310 (struct gl_fragment_program *)linked_prog);
3311 ok = ctx->Driver.ProgramStringNotify(ctx, GL_FRAGMENT_PROGRAM_ARB,
3312 linked_prog);
3313 break;
3314 case GL_GEOMETRY_SHADER:
3315 _mesa_reference_geomprog(ctx, &prog->GeometryProgram,
3316 (struct gl_geometry_program *)linked_prog);
3317 ok = ctx->Driver.ProgramStringNotify(ctx, GL_GEOMETRY_PROGRAM_NV,
3318 linked_prog);
3319 break;
3320 }
3321 if (!ok) {
3322 return GL_FALSE;
3323 }
3324 }
3325
3326 _mesa_reference_program(ctx, &linked_prog, NULL);
3327 }
3328
3329 return GL_TRUE;
3330 }
3331
3332
3333 /**
3334 * Compile a GLSL shader. Called via glCompileShader().
3335 */
3336 void
3337 _mesa_glsl_compile_shader(struct gl_context *ctx, struct gl_shader *shader)
3338 {
3339 struct _mesa_glsl_parse_state *state =
3340 new(shader) _mesa_glsl_parse_state(ctx, shader->Type, shader);
3341
3342 const char *source = shader->Source;
3343 /* Check if the user called glCompileShader without first calling
3344 * glShaderSource. This should fail to compile, but not raise a GL_ERROR.
3345 */
3346 if (source == NULL) {
3347 shader->CompileStatus = GL_FALSE;
3348 return;
3349 }
3350
3351 state->error = preprocess(state, &source, &state->info_log,
3352 &ctx->Extensions, ctx->API);
3353
3354 if (ctx->Shader.Flags & GLSL_DUMP) {
3355 printf("GLSL source for %s shader %d:\n",
3356 _mesa_glsl_shader_target_name(state->target), shader->Name);
3357 printf("%s\n", shader->Source);
3358 }
3359
3360 if (!state->error) {
3361 _mesa_glsl_lexer_ctor(state, source);
3362 _mesa_glsl_parse(state);
3363 _mesa_glsl_lexer_dtor(state);
3364 }
3365
3366 ralloc_free(shader->ir);
3367 shader->ir = new(shader) exec_list;
3368 if (!state->error && !state->translation_unit.is_empty())
3369 _mesa_ast_to_hir(shader->ir, state);
3370
3371 if (!state->error && !shader->ir->is_empty()) {
3372 validate_ir_tree(shader->ir);
3373
3374 /* Do some optimization at compile time to reduce shader IR size
3375 * and reduce later work if the same shader is linked multiple times
3376 */
3377 while (do_common_optimization(shader->ir, false, 32))
3378 ;
3379
3380 validate_ir_tree(shader->ir);
3381 }
3382
3383 shader->symbols = state->symbols;
3384
3385 shader->CompileStatus = !state->error;
3386 shader->InfoLog = state->info_log;
3387 shader->Version = state->language_version;
3388 memcpy(shader->builtins_to_link, state->builtins_to_link,
3389 sizeof(shader->builtins_to_link[0]) * state->num_builtins_to_link);
3390 shader->num_builtins_to_link = state->num_builtins_to_link;
3391
3392 if (ctx->Shader.Flags & GLSL_LOG) {
3393 _mesa_write_shader_to_file(shader);
3394 }
3395
3396 if (ctx->Shader.Flags & GLSL_DUMP) {
3397 if (shader->CompileStatus) {
3398 printf("GLSL IR for shader %d:\n", shader->Name);
3399 _mesa_print_ir(shader->ir, NULL);
3400 printf("\n\n");
3401 } else {
3402 printf("GLSL shader %d failed to compile.\n", shader->Name);
3403 }
3404 if (shader->InfoLog && shader->InfoLog[0] != 0) {
3405 printf("GLSL shader %d info log:\n", shader->Name);
3406 printf("%s\n", shader->InfoLog);
3407 }
3408 }
3409
3410 /* Retain any live IR, but trash the rest. */
3411 reparent_ir(shader->ir, shader->ir);
3412
3413 ralloc_free(state);
3414 }
3415
3416
3417 /**
3418 * Link a GLSL shader program. Called via glLinkProgram().
3419 */
3420 void
3421 _mesa_glsl_link_shader(struct gl_context *ctx, struct gl_shader_program *prog)
3422 {
3423 unsigned int i;
3424
3425 _mesa_clear_shader_program_data(ctx, prog);
3426
3427 prog->LinkStatus = GL_TRUE;
3428
3429 for (i = 0; i < prog->NumShaders; i++) {
3430 if (!prog->Shaders[i]->CompileStatus) {
3431 linker_error(prog, "linking with uncompiled shader");
3432 prog->LinkStatus = GL_FALSE;
3433 }
3434 }
3435
3436 _mesa_reference_vertprog(ctx, &prog->VertexProgram, NULL);
3437 _mesa_reference_fragprog(ctx, &prog->FragmentProgram, NULL);
3438 _mesa_reference_geomprog(ctx, &prog->GeometryProgram, NULL);
3439
3440 if (prog->LinkStatus) {
3441 link_shaders(ctx, prog);
3442 }
3443
3444 if (prog->LinkStatus) {
3445 if (!ctx->Driver.LinkShader(ctx, prog)) {
3446 prog->LinkStatus = GL_FALSE;
3447 }
3448 }
3449
3450 set_uniform_initializers(ctx, prog);
3451
3452 if (ctx->Shader.Flags & GLSL_DUMP) {
3453 if (!prog->LinkStatus) {
3454 printf("GLSL shader program %d failed to link\n", prog->Name);
3455 }
3456
3457 if (prog->InfoLog && prog->InfoLog[0] != 0) {
3458 printf("GLSL shader program %d info log:\n", prog->Name);
3459 printf("%s\n", prog->InfoLog);
3460 }
3461 }
3462 }
3463
3464 } /* extern "C" */