glsl: don't crash if a field is specified for a non-struct uniform
[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 * Translates the IR to ARB_fragment_program text if possible,
30 * printing the result
31 */
32
33 #include <stdio.h>
34 #include "main/compiler.h"
35 #include "ir.h"
36 #include "ir_visitor.h"
37 #include "ir_print_visitor.h"
38 #include "ir_expression_flattening.h"
39 #include "glsl_types.h"
40 #include "glsl_parser_extras.h"
41 #include "../glsl/program.h"
42 #include "ir_optimization.h"
43 #include "ast.h"
44
45 extern "C" {
46 #include "main/mtypes.h"
47 #include "main/shaderapi.h"
48 #include "main/shaderobj.h"
49 #include "main/uniforms.h"
50 #include "program/hash_table.h"
51 #include "program/prog_instruction.h"
52 #include "program/prog_optimize.h"
53 #include "program/prog_print.h"
54 #include "program/program.h"
55 #include "program/prog_uniform.h"
56 #include "program/prog_parameter.h"
57 }
58
59 static int swizzle_for_size(int size);
60
61 /**
62 * This struct is a corresponding struct to Mesa prog_src_register, with
63 * wider fields.
64 */
65 typedef struct ir_to_mesa_src_reg {
66 ir_to_mesa_src_reg(int file, int index, const glsl_type *type)
67 {
68 this->file = file;
69 this->index = index;
70 if (type && (type->is_scalar() || type->is_vector() || type->is_matrix()))
71 this->swizzle = swizzle_for_size(type->vector_elements);
72 else
73 this->swizzle = SWIZZLE_XYZW;
74 this->negate = 0;
75 this->reladdr = NULL;
76 }
77
78 ir_to_mesa_src_reg()
79 {
80 this->file = PROGRAM_UNDEFINED;
81 }
82
83 int file; /**< PROGRAM_* from Mesa */
84 int index; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
85 GLuint swizzle; /**< SWIZZLE_XYZWONEZERO swizzles from Mesa. */
86 int negate; /**< NEGATE_XYZW mask from mesa */
87 /** Register index should be offset by the integer in this reg. */
88 ir_to_mesa_src_reg *reladdr;
89 } ir_to_mesa_src_reg;
90
91 typedef struct ir_to_mesa_dst_reg {
92 int file; /**< PROGRAM_* from Mesa */
93 int index; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
94 int writemask; /**< Bitfield of WRITEMASK_[XYZW] */
95 GLuint cond_mask:4;
96 /** Register index should be offset by the integer in this reg. */
97 ir_to_mesa_src_reg *reladdr;
98 } ir_to_mesa_dst_reg;
99
100 extern ir_to_mesa_src_reg ir_to_mesa_undef;
101
102 class ir_to_mesa_instruction : public exec_node {
103 public:
104 enum prog_opcode op;
105 ir_to_mesa_dst_reg dst_reg;
106 ir_to_mesa_src_reg src_reg[3];
107 /** Pointer to the ir source this tree came from for debugging */
108 ir_instruction *ir;
109 GLboolean cond_update;
110 int sampler; /**< sampler index */
111 int tex_target; /**< One of TEXTURE_*_INDEX */
112 GLboolean tex_shadow;
113
114 class function_entry *function; /* Set on OPCODE_CAL or OPCODE_BGNSUB */
115 };
116
117 class variable_storage : public exec_node {
118 public:
119 variable_storage(ir_variable *var, int file, int index)
120 : file(file), index(index), var(var)
121 {
122 /* empty */
123 }
124
125 int file;
126 int index;
127 ir_variable *var; /* variable that maps to this, if any */
128 };
129
130 class function_entry : public exec_node {
131 public:
132 ir_function_signature *sig;
133
134 /**
135 * identifier of this function signature used by the program.
136 *
137 * At the point that Mesa instructions for function calls are
138 * generated, we don't know the address of the first instruction of
139 * the function body. So we make the BranchTarget that is called a
140 * small integer and rewrite them during set_branchtargets().
141 */
142 int sig_id;
143
144 /**
145 * Pointer to first instruction of the function body.
146 *
147 * Set during function body emits after main() is processed.
148 */
149 ir_to_mesa_instruction *bgn_inst;
150
151 /**
152 * Index of the first instruction of the function body in actual
153 * Mesa IR.
154 *
155 * Set after convertion from ir_to_mesa_instruction to prog_instruction.
156 */
157 int inst;
158
159 /** Storage for the return value. */
160 ir_to_mesa_src_reg return_reg;
161 };
162
163 class ir_to_mesa_visitor : public ir_visitor {
164 public:
165 ir_to_mesa_visitor();
166 ~ir_to_mesa_visitor();
167
168 function_entry *current_function;
169
170 GLcontext *ctx;
171 struct gl_program *prog;
172
173 int next_temp;
174
175 variable_storage *find_variable_storage(ir_variable *var);
176
177 function_entry *get_function_signature(ir_function_signature *sig);
178
179 ir_to_mesa_src_reg get_temp(const glsl_type *type);
180 void reladdr_to_temp(ir_instruction *ir,
181 ir_to_mesa_src_reg *reg, int *num_reladdr);
182
183 struct ir_to_mesa_src_reg src_reg_for_float(float val);
184
185 /**
186 * \name Visit methods
187 *
188 * As typical for the visitor pattern, there must be one \c visit method for
189 * each concrete subclass of \c ir_instruction. Virtual base classes within
190 * the hierarchy should not have \c visit methods.
191 */
192 /*@{*/
193 virtual void visit(ir_variable *);
194 virtual void visit(ir_loop *);
195 virtual void visit(ir_loop_jump *);
196 virtual void visit(ir_function_signature *);
197 virtual void visit(ir_function *);
198 virtual void visit(ir_expression *);
199 virtual void visit(ir_swizzle *);
200 virtual void visit(ir_dereference_variable *);
201 virtual void visit(ir_dereference_array *);
202 virtual void visit(ir_dereference_record *);
203 virtual void visit(ir_assignment *);
204 virtual void visit(ir_constant *);
205 virtual void visit(ir_call *);
206 virtual void visit(ir_return *);
207 virtual void visit(ir_discard *);
208 virtual void visit(ir_texture *);
209 virtual void visit(ir_if *);
210 /*@}*/
211
212 struct ir_to_mesa_src_reg result;
213
214 /** List of variable_storage */
215 exec_list variables;
216
217 /** List of function_entry */
218 exec_list function_signatures;
219 int next_signature_id;
220
221 /** List of ir_to_mesa_instruction */
222 exec_list instructions;
223
224 ir_to_mesa_instruction *ir_to_mesa_emit_op0(ir_instruction *ir,
225 enum prog_opcode op);
226
227 ir_to_mesa_instruction *ir_to_mesa_emit_op1(ir_instruction *ir,
228 enum prog_opcode op,
229 ir_to_mesa_dst_reg dst,
230 ir_to_mesa_src_reg src0);
231
232 ir_to_mesa_instruction *ir_to_mesa_emit_op2(ir_instruction *ir,
233 enum prog_opcode op,
234 ir_to_mesa_dst_reg dst,
235 ir_to_mesa_src_reg src0,
236 ir_to_mesa_src_reg src1);
237
238 ir_to_mesa_instruction *ir_to_mesa_emit_op3(ir_instruction *ir,
239 enum prog_opcode op,
240 ir_to_mesa_dst_reg dst,
241 ir_to_mesa_src_reg src0,
242 ir_to_mesa_src_reg src1,
243 ir_to_mesa_src_reg src2);
244
245 void ir_to_mesa_emit_scalar_op1(ir_instruction *ir,
246 enum prog_opcode op,
247 ir_to_mesa_dst_reg dst,
248 ir_to_mesa_src_reg src0);
249
250 void ir_to_mesa_emit_scalar_op2(ir_instruction *ir,
251 enum prog_opcode op,
252 ir_to_mesa_dst_reg dst,
253 ir_to_mesa_src_reg src0,
254 ir_to_mesa_src_reg src1);
255
256 GLboolean try_emit_mad(ir_expression *ir,
257 int mul_operand);
258
259 int add_uniform(const char *name,
260 const glsl_type *type,
261 ir_constant *constant);
262 void add_aggregate_uniform(ir_instruction *ir,
263 const char *name,
264 const struct glsl_type *type,
265 ir_constant *constant,
266 struct ir_to_mesa_dst_reg temp);
267
268 struct hash_table *sampler_map;
269
270 void set_sampler_location(ir_variable *sampler, int location);
271 int get_sampler_location(ir_variable *sampler);
272
273 void *mem_ctx;
274 };
275
276 ir_to_mesa_src_reg ir_to_mesa_undef = ir_to_mesa_src_reg(PROGRAM_UNDEFINED, 0, NULL);
277
278 ir_to_mesa_dst_reg ir_to_mesa_undef_dst = {
279 PROGRAM_UNDEFINED, 0, SWIZZLE_NOOP, COND_TR, NULL,
280 };
281
282 ir_to_mesa_dst_reg ir_to_mesa_address_reg = {
283 PROGRAM_ADDRESS, 0, WRITEMASK_X, COND_TR, NULL
284 };
285
286 static int swizzle_for_size(int size)
287 {
288 int size_swizzles[4] = {
289 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X),
290 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Y, SWIZZLE_Y),
291 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z),
292 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W),
293 };
294
295 return size_swizzles[size - 1];
296 }
297
298 ir_to_mesa_instruction *
299 ir_to_mesa_visitor::ir_to_mesa_emit_op3(ir_instruction *ir,
300 enum prog_opcode op,
301 ir_to_mesa_dst_reg dst,
302 ir_to_mesa_src_reg src0,
303 ir_to_mesa_src_reg src1,
304 ir_to_mesa_src_reg src2)
305 {
306 ir_to_mesa_instruction *inst = new(mem_ctx) ir_to_mesa_instruction();
307 int num_reladdr = 0;
308
309 /* If we have to do relative addressing, we want to load the ARL
310 * reg directly for one of the regs, and preload the other reladdr
311 * sources into temps.
312 */
313 num_reladdr += dst.reladdr != NULL;
314 num_reladdr += src0.reladdr != NULL;
315 num_reladdr += src1.reladdr != NULL;
316 num_reladdr += src2.reladdr != NULL;
317
318 reladdr_to_temp(ir, &src2, &num_reladdr);
319 reladdr_to_temp(ir, &src1, &num_reladdr);
320 reladdr_to_temp(ir, &src0, &num_reladdr);
321
322 if (dst.reladdr) {
323 ir_to_mesa_emit_op1(ir, OPCODE_ARL, ir_to_mesa_address_reg,
324 *dst.reladdr);
325
326 num_reladdr--;
327 }
328 assert(num_reladdr == 0);
329
330 inst->op = op;
331 inst->dst_reg = dst;
332 inst->src_reg[0] = src0;
333 inst->src_reg[1] = src1;
334 inst->src_reg[2] = src2;
335 inst->ir = ir;
336
337 inst->function = NULL;
338
339 this->instructions.push_tail(inst);
340
341 return inst;
342 }
343
344
345 ir_to_mesa_instruction *
346 ir_to_mesa_visitor::ir_to_mesa_emit_op2(ir_instruction *ir,
347 enum prog_opcode op,
348 ir_to_mesa_dst_reg dst,
349 ir_to_mesa_src_reg src0,
350 ir_to_mesa_src_reg src1)
351 {
352 return ir_to_mesa_emit_op3(ir, op, dst, src0, src1, ir_to_mesa_undef);
353 }
354
355 ir_to_mesa_instruction *
356 ir_to_mesa_visitor::ir_to_mesa_emit_op1(ir_instruction *ir,
357 enum prog_opcode op,
358 ir_to_mesa_dst_reg dst,
359 ir_to_mesa_src_reg src0)
360 {
361 assert(dst.writemask != 0);
362 return ir_to_mesa_emit_op3(ir, op, dst,
363 src0, ir_to_mesa_undef, ir_to_mesa_undef);
364 }
365
366 ir_to_mesa_instruction *
367 ir_to_mesa_visitor::ir_to_mesa_emit_op0(ir_instruction *ir,
368 enum prog_opcode op)
369 {
370 return ir_to_mesa_emit_op3(ir, op, ir_to_mesa_undef_dst,
371 ir_to_mesa_undef,
372 ir_to_mesa_undef,
373 ir_to_mesa_undef);
374 }
375
376 void
377 ir_to_mesa_visitor::set_sampler_location(ir_variable *sampler, int location)
378 {
379 if (this->sampler_map == NULL) {
380 this->sampler_map = hash_table_ctor(0, hash_table_pointer_hash,
381 hash_table_pointer_compare);
382 }
383
384 hash_table_insert(this->sampler_map, (void *)(uintptr_t)location, sampler);
385 }
386
387 int
388 ir_to_mesa_visitor::get_sampler_location(ir_variable *sampler)
389 {
390 void *result = hash_table_find(this->sampler_map, sampler);
391
392 return (int)(uintptr_t)result;
393 }
394
395 inline ir_to_mesa_dst_reg
396 ir_to_mesa_dst_reg_from_src(ir_to_mesa_src_reg reg)
397 {
398 ir_to_mesa_dst_reg dst_reg;
399
400 dst_reg.file = reg.file;
401 dst_reg.index = reg.index;
402 dst_reg.writemask = WRITEMASK_XYZW;
403 dst_reg.cond_mask = COND_TR;
404 dst_reg.reladdr = reg.reladdr;
405
406 return dst_reg;
407 }
408
409 inline ir_to_mesa_src_reg
410 ir_to_mesa_src_reg_from_dst(ir_to_mesa_dst_reg reg)
411 {
412 return ir_to_mesa_src_reg(reg.file, reg.index, NULL);
413 }
414
415 /**
416 * Emits Mesa scalar opcodes to produce unique answers across channels.
417 *
418 * Some Mesa opcodes are scalar-only, like ARB_fp/vp. The src X
419 * channel determines the result across all channels. So to do a vec4
420 * of this operation, we want to emit a scalar per source channel used
421 * to produce dest channels.
422 */
423 void
424 ir_to_mesa_visitor::ir_to_mesa_emit_scalar_op2(ir_instruction *ir,
425 enum prog_opcode op,
426 ir_to_mesa_dst_reg dst,
427 ir_to_mesa_src_reg orig_src0,
428 ir_to_mesa_src_reg orig_src1)
429 {
430 int i, j;
431 int done_mask = ~dst.writemask;
432
433 /* Mesa RCP is a scalar operation splatting results to all channels,
434 * like ARB_fp/vp. So emit as many RCPs as necessary to cover our
435 * dst channels.
436 */
437 for (i = 0; i < 4; i++) {
438 GLuint this_mask = (1 << i);
439 ir_to_mesa_instruction *inst;
440 ir_to_mesa_src_reg src0 = orig_src0;
441 ir_to_mesa_src_reg src1 = orig_src1;
442
443 if (done_mask & this_mask)
444 continue;
445
446 GLuint src0_swiz = GET_SWZ(src0.swizzle, i);
447 GLuint src1_swiz = GET_SWZ(src1.swizzle, i);
448 for (j = i + 1; j < 4; j++) {
449 if (!(done_mask & (1 << j)) &&
450 GET_SWZ(src0.swizzle, j) == src0_swiz &&
451 GET_SWZ(src1.swizzle, j) == src1_swiz) {
452 this_mask |= (1 << j);
453 }
454 }
455 src0.swizzle = MAKE_SWIZZLE4(src0_swiz, src0_swiz,
456 src0_swiz, src0_swiz);
457 src1.swizzle = MAKE_SWIZZLE4(src1_swiz, src1_swiz,
458 src1_swiz, src1_swiz);
459
460 inst = ir_to_mesa_emit_op2(ir, op,
461 dst,
462 src0,
463 src1);
464 inst->dst_reg.writemask = this_mask;
465 done_mask |= this_mask;
466 }
467 }
468
469 void
470 ir_to_mesa_visitor::ir_to_mesa_emit_scalar_op1(ir_instruction *ir,
471 enum prog_opcode op,
472 ir_to_mesa_dst_reg dst,
473 ir_to_mesa_src_reg src0)
474 {
475 ir_to_mesa_src_reg undef = ir_to_mesa_undef;
476
477 undef.swizzle = SWIZZLE_XXXX;
478
479 ir_to_mesa_emit_scalar_op2(ir, op, dst, src0, undef);
480 }
481
482 struct ir_to_mesa_src_reg
483 ir_to_mesa_visitor::src_reg_for_float(float val)
484 {
485 ir_to_mesa_src_reg src_reg(PROGRAM_CONSTANT, -1, NULL);
486
487 src_reg.index = _mesa_add_unnamed_constant(this->prog->Parameters,
488 &val, 1, &src_reg.swizzle);
489
490 return src_reg;
491 }
492
493 static int
494 type_size(const struct glsl_type *type)
495 {
496 unsigned int i;
497 int size;
498
499 switch (type->base_type) {
500 case GLSL_TYPE_UINT:
501 case GLSL_TYPE_INT:
502 case GLSL_TYPE_FLOAT:
503 case GLSL_TYPE_BOOL:
504 if (type->is_matrix()) {
505 return type->matrix_columns;
506 } else {
507 /* Regardless of size of vector, it gets a vec4. This is bad
508 * packing for things like floats, but otherwise arrays become a
509 * mess. Hopefully a later pass over the code can pack scalars
510 * down if appropriate.
511 */
512 return 1;
513 }
514 case GLSL_TYPE_ARRAY:
515 return type_size(type->fields.array) * type->length;
516 case GLSL_TYPE_STRUCT:
517 size = 0;
518 for (i = 0; i < type->length; i++) {
519 size += type_size(type->fields.structure[i].type);
520 }
521 return size;
522 case GLSL_TYPE_SAMPLER:
523 /* Samplers take up no register space, since they're baked in at
524 * link time.
525 */
526 return 0;
527 default:
528 assert(0);
529 return 0;
530 }
531 }
532
533 /**
534 * In the initial pass of codegen, we assign temporary numbers to
535 * intermediate results. (not SSA -- variable assignments will reuse
536 * storage). Actual register allocation for the Mesa VM occurs in a
537 * pass over the Mesa IR later.
538 */
539 ir_to_mesa_src_reg
540 ir_to_mesa_visitor::get_temp(const glsl_type *type)
541 {
542 ir_to_mesa_src_reg src_reg;
543 int swizzle[4];
544 int i;
545
546 src_reg.file = PROGRAM_TEMPORARY;
547 src_reg.index = next_temp;
548 src_reg.reladdr = NULL;
549 next_temp += type_size(type);
550
551 if (type->is_array() || type->is_record()) {
552 src_reg.swizzle = SWIZZLE_NOOP;
553 } else {
554 for (i = 0; i < type->vector_elements; i++)
555 swizzle[i] = i;
556 for (; i < 4; i++)
557 swizzle[i] = type->vector_elements - 1;
558 src_reg.swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
559 swizzle[2], swizzle[3]);
560 }
561 src_reg.negate = 0;
562
563 return src_reg;
564 }
565
566 variable_storage *
567 ir_to_mesa_visitor::find_variable_storage(ir_variable *var)
568 {
569
570 variable_storage *entry;
571
572 foreach_iter(exec_list_iterator, iter, this->variables) {
573 entry = (variable_storage *)iter.get();
574
575 if (entry->var == var)
576 return entry;
577 }
578
579 return NULL;
580 }
581
582 void
583 ir_to_mesa_visitor::visit(ir_variable *ir)
584 {
585 if (strcmp(ir->name, "gl_FragCoord") == 0) {
586 struct gl_fragment_program *fp = (struct gl_fragment_program *)this->prog;
587
588 fp->OriginUpperLeft = ir->origin_upper_left;
589 fp->PixelCenterInteger = ir->pixel_center_integer;
590 }
591 }
592
593 void
594 ir_to_mesa_visitor::visit(ir_loop *ir)
595 {
596 assert(!ir->from);
597 assert(!ir->to);
598 assert(!ir->increment);
599 assert(!ir->counter);
600
601 ir_to_mesa_emit_op0(NULL, OPCODE_BGNLOOP);
602 visit_exec_list(&ir->body_instructions, this);
603 ir_to_mesa_emit_op0(NULL, OPCODE_ENDLOOP);
604 }
605
606 void
607 ir_to_mesa_visitor::visit(ir_loop_jump *ir)
608 {
609 switch (ir->mode) {
610 case ir_loop_jump::jump_break:
611 ir_to_mesa_emit_op0(NULL, OPCODE_BRK);
612 break;
613 case ir_loop_jump::jump_continue:
614 ir_to_mesa_emit_op0(NULL, OPCODE_CONT);
615 break;
616 }
617 }
618
619
620 void
621 ir_to_mesa_visitor::visit(ir_function_signature *ir)
622 {
623 assert(0);
624 (void)ir;
625 }
626
627 void
628 ir_to_mesa_visitor::visit(ir_function *ir)
629 {
630 /* Ignore function bodies other than main() -- we shouldn't see calls to
631 * them since they should all be inlined before we get to ir_to_mesa.
632 */
633 if (strcmp(ir->name, "main") == 0) {
634 const ir_function_signature *sig;
635 exec_list empty;
636
637 sig = ir->matching_signature(&empty);
638
639 assert(sig);
640
641 foreach_iter(exec_list_iterator, iter, sig->body) {
642 ir_instruction *ir = (ir_instruction *)iter.get();
643
644 ir->accept(this);
645 }
646 }
647 }
648
649 GLboolean
650 ir_to_mesa_visitor::try_emit_mad(ir_expression *ir, int mul_operand)
651 {
652 int nonmul_operand = 1 - mul_operand;
653 ir_to_mesa_src_reg a, b, c;
654
655 ir_expression *expr = ir->operands[mul_operand]->as_expression();
656 if (!expr || expr->operation != ir_binop_mul)
657 return false;
658
659 expr->operands[0]->accept(this);
660 a = this->result;
661 expr->operands[1]->accept(this);
662 b = this->result;
663 ir->operands[nonmul_operand]->accept(this);
664 c = this->result;
665
666 this->result = get_temp(ir->type);
667 ir_to_mesa_emit_op3(ir, OPCODE_MAD,
668 ir_to_mesa_dst_reg_from_src(this->result), a, b, c);
669
670 return true;
671 }
672
673 void
674 ir_to_mesa_visitor::reladdr_to_temp(ir_instruction *ir,
675 ir_to_mesa_src_reg *reg, int *num_reladdr)
676 {
677 if (!reg->reladdr)
678 return;
679
680 ir_to_mesa_emit_op1(ir, OPCODE_ARL, ir_to_mesa_address_reg, *reg->reladdr);
681
682 if (*num_reladdr != 1) {
683 ir_to_mesa_src_reg temp = get_temp(glsl_type::vec4_type);
684
685 ir_to_mesa_emit_op1(ir, OPCODE_MOV,
686 ir_to_mesa_dst_reg_from_src(temp), *reg);
687 *reg = temp;
688 }
689
690 (*num_reladdr)--;
691 }
692
693 void
694 ir_to_mesa_visitor::visit(ir_expression *ir)
695 {
696 unsigned int operand;
697 struct ir_to_mesa_src_reg op[2];
698 struct ir_to_mesa_src_reg result_src;
699 struct ir_to_mesa_dst_reg result_dst;
700 const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
701 const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
702 const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
703
704 /* Quick peephole: Emit OPCODE_MAD(a, b, c) instead of ADD(MUL(a, b), c)
705 */
706 if (ir->operation == ir_binop_add) {
707 if (try_emit_mad(ir, 1))
708 return;
709 if (try_emit_mad(ir, 0))
710 return;
711 }
712
713 for (operand = 0; operand < ir->get_num_operands(); operand++) {
714 this->result.file = PROGRAM_UNDEFINED;
715 ir->operands[operand]->accept(this);
716 if (this->result.file == PROGRAM_UNDEFINED) {
717 ir_print_visitor v;
718 printf("Failed to get tree for expression operand:\n");
719 ir->operands[operand]->accept(&v);
720 exit(1);
721 }
722 op[operand] = this->result;
723
724 /* Matrix expression operands should have been broken down to vector
725 * operations already.
726 */
727 assert(!ir->operands[operand]->type->is_matrix());
728 }
729
730 this->result.file = PROGRAM_UNDEFINED;
731
732 /* Storage for our result. Ideally for an assignment we'd be using
733 * the actual storage for the result here, instead.
734 */
735 result_src = get_temp(ir->type);
736 /* convenience for the emit functions below. */
737 result_dst = ir_to_mesa_dst_reg_from_src(result_src);
738 /* Limit writes to the channels that will be used by result_src later.
739 * This does limit this temp's use as a temporary for multi-instruction
740 * sequences.
741 */
742 result_dst.writemask = (1 << ir->type->vector_elements) - 1;
743
744 switch (ir->operation) {
745 case ir_unop_logic_not:
746 ir_to_mesa_emit_op2(ir, OPCODE_SEQ, result_dst,
747 op[0], src_reg_for_float(0.0));
748 break;
749 case ir_unop_neg:
750 op[0].negate = ~op[0].negate;
751 result_src = op[0];
752 break;
753 case ir_unop_abs:
754 ir_to_mesa_emit_op1(ir, OPCODE_ABS, result_dst, op[0]);
755 break;
756 case ir_unop_sign:
757 ir_to_mesa_emit_op1(ir, OPCODE_SSG, result_dst, op[0]);
758 break;
759 case ir_unop_rcp:
760 ir_to_mesa_emit_scalar_op1(ir, OPCODE_RCP, result_dst, op[0]);
761 break;
762
763 case ir_unop_exp2:
764 ir_to_mesa_emit_scalar_op1(ir, OPCODE_EX2, result_dst, op[0]);
765 break;
766 case ir_unop_exp:
767 case ir_unop_log:
768 assert(!"not reached: should be handled by ir_explog_to_explog2");
769 break;
770 case ir_unop_log2:
771 ir_to_mesa_emit_scalar_op1(ir, OPCODE_LG2, result_dst, op[0]);
772 break;
773 case ir_unop_sin:
774 ir_to_mesa_emit_scalar_op1(ir, OPCODE_SIN, result_dst, op[0]);
775 break;
776 case ir_unop_cos:
777 ir_to_mesa_emit_scalar_op1(ir, OPCODE_COS, result_dst, op[0]);
778 break;
779
780 case ir_unop_dFdx:
781 ir_to_mesa_emit_op1(ir, OPCODE_DDX, result_dst, op[0]);
782 break;
783 case ir_unop_dFdy:
784 ir_to_mesa_emit_op1(ir, OPCODE_DDY, result_dst, op[0]);
785 break;
786
787 case ir_binop_add:
788 ir_to_mesa_emit_op2(ir, OPCODE_ADD, result_dst, op[0], op[1]);
789 break;
790 case ir_binop_sub:
791 ir_to_mesa_emit_op2(ir, OPCODE_SUB, result_dst, op[0], op[1]);
792 break;
793
794 case ir_binop_mul:
795 ir_to_mesa_emit_op2(ir, OPCODE_MUL, result_dst, op[0], op[1]);
796 break;
797 case ir_binop_div:
798 assert(!"not reached: should be handled by ir_div_to_mul_rcp");
799 case ir_binop_mod:
800 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
801 break;
802
803 case ir_binop_less:
804 ir_to_mesa_emit_op2(ir, OPCODE_SLT, result_dst, op[0], op[1]);
805 break;
806 case ir_binop_greater:
807 ir_to_mesa_emit_op2(ir, OPCODE_SGT, result_dst, op[0], op[1]);
808 break;
809 case ir_binop_lequal:
810 ir_to_mesa_emit_op2(ir, OPCODE_SLE, result_dst, op[0], op[1]);
811 break;
812 case ir_binop_gequal:
813 ir_to_mesa_emit_op2(ir, OPCODE_SGE, result_dst, op[0], op[1]);
814 break;
815 case ir_binop_equal:
816 /* "==" operator producing a scalar boolean. */
817 if (ir->operands[0]->type->is_vector() ||
818 ir->operands[1]->type->is_vector()) {
819 ir_to_mesa_src_reg temp = get_temp(glsl_type::vec4_type);
820 ir_to_mesa_emit_op2(ir, OPCODE_SNE,
821 ir_to_mesa_dst_reg_from_src(temp), op[0], op[1]);
822 ir_to_mesa_emit_op2(ir, OPCODE_DP4, result_dst, temp, temp);
823 ir_to_mesa_emit_op2(ir, OPCODE_SEQ,
824 result_dst, result_src, src_reg_for_float(0.0));
825 } else {
826 ir_to_mesa_emit_op2(ir, OPCODE_SEQ, result_dst, op[0], op[1]);
827 }
828 break;
829 case ir_binop_nequal:
830 /* "!=" operator producing a scalar boolean. */
831 if (ir->operands[0]->type->is_vector() ||
832 ir->operands[1]->type->is_vector()) {
833 ir_to_mesa_src_reg temp = get_temp(glsl_type::vec4_type);
834 ir_to_mesa_emit_op2(ir, OPCODE_SNE,
835 ir_to_mesa_dst_reg_from_src(temp), op[0], op[1]);
836 ir_to_mesa_emit_op2(ir, OPCODE_DP4, result_dst, temp, temp);
837 ir_to_mesa_emit_op2(ir, OPCODE_SNE,
838 result_dst, result_src, src_reg_for_float(0.0));
839 } else {
840 ir_to_mesa_emit_op2(ir, OPCODE_SNE, result_dst, op[0], op[1]);
841 }
842 break;
843 case ir_binop_logic_xor:
844 ir_to_mesa_emit_op2(ir, OPCODE_SNE, result_dst, op[0], op[1]);
845 break;
846
847 case ir_binop_logic_or:
848 /* This could be a saturated add and skip the SNE. */
849 ir_to_mesa_emit_op2(ir, OPCODE_ADD,
850 result_dst,
851 op[0], op[1]);
852
853 ir_to_mesa_emit_op2(ir, OPCODE_SNE,
854 result_dst,
855 result_src, src_reg_for_float(0.0));
856 break;
857
858 case ir_binop_logic_and:
859 /* the bool args are stored as float 0.0 or 1.0, so "mul" gives us "and". */
860 ir_to_mesa_emit_op2(ir, OPCODE_MUL,
861 result_dst,
862 op[0], op[1]);
863 break;
864
865 case ir_binop_dot:
866 if (ir->operands[0]->type == vec4_type) {
867 assert(ir->operands[1]->type == vec4_type);
868 ir_to_mesa_emit_op2(ir, OPCODE_DP4,
869 result_dst,
870 op[0], op[1]);
871 } else if (ir->operands[0]->type == vec3_type) {
872 assert(ir->operands[1]->type == vec3_type);
873 ir_to_mesa_emit_op2(ir, OPCODE_DP3,
874 result_dst,
875 op[0], op[1]);
876 } else if (ir->operands[0]->type == vec2_type) {
877 assert(ir->operands[1]->type == vec2_type);
878 ir_to_mesa_emit_op2(ir, OPCODE_DP2,
879 result_dst,
880 op[0], op[1]);
881 }
882 break;
883
884 case ir_binop_cross:
885 ir_to_mesa_emit_op2(ir, OPCODE_XPD, result_dst, op[0], op[1]);
886 break;
887
888 case ir_unop_sqrt:
889 ir_to_mesa_emit_scalar_op1(ir, OPCODE_RSQ, result_dst, op[0]);
890 ir_to_mesa_emit_scalar_op1(ir, OPCODE_RCP, result_dst, result_src);
891 /* For incoming channels < 0, set the result to 0. */
892 ir_to_mesa_emit_op3(ir, OPCODE_CMP, result_dst,
893 op[0], src_reg_for_float(0.0), result_src);
894 break;
895 case ir_unop_rsq:
896 ir_to_mesa_emit_scalar_op1(ir, OPCODE_RSQ, result_dst, op[0]);
897 break;
898 case ir_unop_i2f:
899 case ir_unop_b2f:
900 case ir_unop_b2i:
901 /* Mesa IR lacks types, ints are stored as truncated floats. */
902 result_src = op[0];
903 break;
904 case ir_unop_f2i:
905 ir_to_mesa_emit_op1(ir, OPCODE_TRUNC, result_dst, op[0]);
906 break;
907 case ir_unop_f2b:
908 case ir_unop_i2b:
909 ir_to_mesa_emit_op2(ir, OPCODE_SNE, result_dst,
910 result_src, src_reg_for_float(0.0));
911 break;
912 case ir_unop_trunc:
913 ir_to_mesa_emit_op1(ir, OPCODE_TRUNC, result_dst, op[0]);
914 break;
915 case ir_unop_ceil:
916 op[0].negate = ~op[0].negate;
917 ir_to_mesa_emit_op1(ir, OPCODE_FLR, result_dst, op[0]);
918 result_src.negate = ~result_src.negate;
919 break;
920 case ir_unop_floor:
921 ir_to_mesa_emit_op1(ir, OPCODE_FLR, result_dst, op[0]);
922 break;
923 case ir_unop_fract:
924 ir_to_mesa_emit_op1(ir, OPCODE_FRC, result_dst, op[0]);
925 break;
926
927 case ir_binop_min:
928 ir_to_mesa_emit_op2(ir, OPCODE_MIN, result_dst, op[0], op[1]);
929 break;
930 case ir_binop_max:
931 ir_to_mesa_emit_op2(ir, OPCODE_MAX, result_dst, op[0], op[1]);
932 break;
933 case ir_binop_pow:
934 ir_to_mesa_emit_scalar_op2(ir, OPCODE_POW, result_dst, op[0], op[1]);
935 break;
936
937 case ir_unop_bit_not:
938 case ir_unop_u2f:
939 case ir_binop_lshift:
940 case ir_binop_rshift:
941 case ir_binop_bit_and:
942 case ir_binop_bit_xor:
943 case ir_binop_bit_or:
944 assert(!"GLSL 1.30 features unsupported");
945 break;
946 }
947
948 this->result = result_src;
949 }
950
951
952 void
953 ir_to_mesa_visitor::visit(ir_swizzle *ir)
954 {
955 ir_to_mesa_src_reg src_reg;
956 int i;
957 int swizzle[4];
958
959 /* Note that this is only swizzles in expressions, not those on the left
960 * hand side of an assignment, which do write masking. See ir_assignment
961 * for that.
962 */
963
964 ir->val->accept(this);
965 src_reg = this->result;
966 assert(src_reg.file != PROGRAM_UNDEFINED);
967
968 for (i = 0; i < 4; i++) {
969 if (i < ir->type->vector_elements) {
970 switch (i) {
971 case 0:
972 swizzle[i] = GET_SWZ(src_reg.swizzle, ir->mask.x);
973 break;
974 case 1:
975 swizzle[i] = GET_SWZ(src_reg.swizzle, ir->mask.y);
976 break;
977 case 2:
978 swizzle[i] = GET_SWZ(src_reg.swizzle, ir->mask.z);
979 break;
980 case 3:
981 swizzle[i] = GET_SWZ(src_reg.swizzle, ir->mask.w);
982 break;
983 }
984 } else {
985 /* If the type is smaller than a vec4, replicate the last
986 * channel out.
987 */
988 swizzle[i] = swizzle[ir->type->vector_elements - 1];
989 }
990 }
991
992 src_reg.swizzle = MAKE_SWIZZLE4(swizzle[0],
993 swizzle[1],
994 swizzle[2],
995 swizzle[3]);
996
997 this->result = src_reg;
998 }
999
1000 static const struct {
1001 const char *name;
1002 const char *field;
1003 int tokens[STATE_LENGTH];
1004 int swizzle;
1005 bool array_indexed;
1006 } statevars[] = {
1007 {"gl_DepthRange", "near",
1008 {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_XXXX, false},
1009 {"gl_DepthRange", "far",
1010 {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_YYYY, false},
1011 {"gl_DepthRange", "diff",
1012 {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_ZZZZ, false},
1013
1014 {"gl_ClipPlane", NULL,
1015 {STATE_CLIPPLANE, 0, 0}, SWIZZLE_XYZW, true}
1016 ,
1017 {"gl_Point", "size",
1018 {STATE_POINT_SIZE}, SWIZZLE_XXXX, false},
1019 {"gl_Point", "sizeMin",
1020 {STATE_POINT_SIZE}, SWIZZLE_YYYY, false},
1021 {"gl_Point", "sizeMax",
1022 {STATE_POINT_SIZE}, SWIZZLE_ZZZZ, false},
1023 {"gl_Point", "fadeThresholdSize",
1024 {STATE_POINT_SIZE}, SWIZZLE_WWWW, false},
1025 {"gl_Point", "distanceConstantAttenuation",
1026 {STATE_POINT_ATTENUATION}, SWIZZLE_XXXX, false},
1027 {"gl_Point", "distanceLinearAttenuation",
1028 {STATE_POINT_ATTENUATION}, SWIZZLE_YYYY, false},
1029 {"gl_Point", "distanceQuadraticAttenuation",
1030 {STATE_POINT_ATTENUATION}, SWIZZLE_ZZZZ, false},
1031
1032 {"gl_FrontMaterial", "emission",
1033 {STATE_MATERIAL, 0, STATE_EMISSION}, SWIZZLE_XYZW, false},
1034 {"gl_FrontMaterial", "ambient",
1035 {STATE_MATERIAL, 0, STATE_AMBIENT}, SWIZZLE_XYZW, false},
1036 {"gl_FrontMaterial", "diffuse",
1037 {STATE_MATERIAL, 0, STATE_DIFFUSE}, SWIZZLE_XYZW, false},
1038 {"gl_FrontMaterial", "specular",
1039 {STATE_MATERIAL, 0, STATE_SPECULAR}, SWIZZLE_XYZW, false},
1040 {"gl_FrontMaterial", "shininess",
1041 {STATE_MATERIAL, 0, STATE_SHININESS}, SWIZZLE_XXXX, false},
1042
1043 {"gl_BackMaterial", "emission",
1044 {STATE_MATERIAL, 1, STATE_EMISSION}, SWIZZLE_XYZW, false},
1045 {"gl_BackMaterial", "ambient",
1046 {STATE_MATERIAL, 1, STATE_AMBIENT}, SWIZZLE_XYZW, false},
1047 {"gl_BackMaterial", "diffuse",
1048 {STATE_MATERIAL, 1, STATE_DIFFUSE}, SWIZZLE_XYZW, false},
1049 {"gl_BackMaterial", "specular",
1050 {STATE_MATERIAL, 1, STATE_SPECULAR}, SWIZZLE_XYZW, false},
1051 {"gl_BackMaterial", "shininess",
1052 {STATE_MATERIAL, 1, STATE_SHININESS}, SWIZZLE_XXXX, false},
1053
1054 {"gl_LightSource", "ambient",
1055 {STATE_LIGHT, 0, STATE_AMBIENT}, SWIZZLE_XYZW, true},
1056 {"gl_LightSource", "diffuse",
1057 {STATE_LIGHT, 0, STATE_DIFFUSE}, SWIZZLE_XYZW, true},
1058 {"gl_LightSource", "specular",
1059 {STATE_LIGHT, 0, STATE_SPECULAR}, SWIZZLE_XYZW, true},
1060 {"gl_LightSource", "position",
1061 {STATE_LIGHT, 0, STATE_POSITION}, SWIZZLE_XYZW, true},
1062 {"gl_LightSource", "halfVector",
1063 {STATE_LIGHT, 0, STATE_HALF_VECTOR}, SWIZZLE_XYZW, true},
1064 {"gl_LightSource", "spotDirection",
1065 {STATE_LIGHT, 0, STATE_SPOT_DIRECTION}, SWIZZLE_XYZW, true},
1066 {"gl_LightSource", "spotCosCutoff",
1067 {STATE_LIGHT, 0, STATE_SPOT_DIRECTION}, SWIZZLE_WWWW, true},
1068 {"gl_LightSource", "spotCutoff",
1069 {STATE_LIGHT, 0, STATE_SPOT_CUTOFF}, SWIZZLE_XXXX, true},
1070 {"gl_LightSource", "spotExponent",
1071 {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_WWWW, true},
1072 {"gl_LightSource", "constantAttenuation",
1073 {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_XXXX, true},
1074 {"gl_LightSource", "linearAttenuation",
1075 {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_YYYY, true},
1076 {"gl_LightSource", "quadraticAttenuation",
1077 {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_ZZZZ, true},
1078
1079 {"gl_LightModel", NULL,
1080 {STATE_LIGHTMODEL_AMBIENT, 0}, SWIZZLE_XYZW, false},
1081
1082 {"gl_FrontLightModelProduct", "sceneColor",
1083 {STATE_LIGHTMODEL_SCENECOLOR, 0}, SWIZZLE_XYZW, false},
1084 {"gl_BackLightModelProduct", "sceneColor",
1085 {STATE_LIGHTMODEL_SCENECOLOR, 1}, SWIZZLE_XYZW, false},
1086
1087 {"gl_FrontLightProduct", "ambient",
1088 {STATE_LIGHTPROD, 0, 0, STATE_AMBIENT}, SWIZZLE_XYZW, true},
1089 {"gl_FrontLightProduct", "diffuse",
1090 {STATE_LIGHTPROD, 0, 0, STATE_DIFFUSE}, SWIZZLE_XYZW, true},
1091 {"gl_FrontLightProduct", "specular",
1092 {STATE_LIGHTPROD, 0, 0, STATE_SPECULAR}, SWIZZLE_XYZW, true},
1093
1094 {"gl_BackLightProduct", "ambient",
1095 {STATE_LIGHTPROD, 0, 1, STATE_AMBIENT}, SWIZZLE_XYZW, true},
1096 {"gl_BackLightProduct", "diffuse",
1097 {STATE_LIGHTPROD, 0, 1, STATE_DIFFUSE}, SWIZZLE_XYZW, true},
1098 {"gl_BackLightProduct", "specular",
1099 {STATE_LIGHTPROD, 0, 1, STATE_SPECULAR}, SWIZZLE_XYZW, true},
1100
1101 {"gl_TextureEnvColor", "ambient",
1102 {STATE_TEXENV_COLOR, 0}, SWIZZLE_XYZW, true},
1103
1104 {"gl_EyePlaneS", NULL,
1105 {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_S}, SWIZZLE_XYZW, true},
1106 {"gl_EyePlaneT", NULL,
1107 {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_T}, SWIZZLE_XYZW, true},
1108 {"gl_EyePlaneR", NULL,
1109 {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_R}, SWIZZLE_XYZW, true},
1110 {"gl_EyePlaneQ", NULL,
1111 {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_Q}, SWIZZLE_XYZW, true},
1112
1113 {"gl_ObjectPlaneS", NULL,
1114 {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_S}, SWIZZLE_XYZW, true},
1115 {"gl_ObjectPlaneT", NULL,
1116 {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_T}, SWIZZLE_XYZW, true},
1117 {"gl_ObjectPlaneR", NULL,
1118 {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_R}, SWIZZLE_XYZW, true},
1119 {"gl_ObjectPlaneQ", NULL,
1120 {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_Q}, SWIZZLE_XYZW, true},
1121
1122 {"gl_Fog", "color",
1123 {STATE_FOG_COLOR}, SWIZZLE_XYZW, false},
1124 {"gl_Fog", "density",
1125 {STATE_FOG_PARAMS}, SWIZZLE_XXXX, false},
1126 {"gl_Fog", "start",
1127 {STATE_FOG_PARAMS}, SWIZZLE_YYYY, false},
1128 {"gl_Fog", "end",
1129 {STATE_FOG_PARAMS}, SWIZZLE_ZZZZ, false},
1130 {"gl_Fog", "scale",
1131 {STATE_FOG_PARAMS}, SWIZZLE_WWWW, false},
1132 };
1133
1134 static ir_to_mesa_src_reg
1135 get_builtin_uniform_reg(struct gl_program *prog,
1136 const char *name, int array_index, const char *field)
1137 {
1138 unsigned int i;
1139 ir_to_mesa_src_reg src_reg;
1140 int tokens[STATE_LENGTH];
1141
1142 for (i = 0; i < Elements(statevars); i++) {
1143 if (strcmp(statevars[i].name, name) != 0)
1144 continue;
1145 if (!field && statevars[i].field) {
1146 assert(!"FINISHME: whole-structure state var dereference");
1147 }
1148 if (field && (!statevars[i].field || strcmp(statevars[i].field, field) != 0))
1149 continue;
1150 break;
1151 }
1152
1153 if (i == Elements(statevars)) {
1154 printf("builtin uniform %s%s%s not found\n",
1155 name,
1156 field ? "." : "",
1157 field ? field : "");
1158 abort();
1159 }
1160
1161 memcpy(&tokens, statevars[i].tokens, sizeof(tokens));
1162 if (statevars[i].array_indexed)
1163 tokens[1] = array_index;
1164
1165 src_reg.file = PROGRAM_STATE_VAR;
1166 src_reg.index = _mesa_add_state_reference(prog->Parameters,
1167 (gl_state_index *)tokens);
1168 src_reg.swizzle = statevars[i].swizzle;
1169 src_reg.negate = 0;
1170 src_reg.reladdr = false;
1171
1172 return src_reg;
1173 }
1174
1175 static int
1176 add_matrix_ref(struct gl_program *prog, int *tokens)
1177 {
1178 int base_pos = -1;
1179 int i;
1180
1181 /* Add a ref for each column. It looks like the reason we do
1182 * it this way is that _mesa_add_state_reference doesn't work
1183 * for things that aren't vec4s, so the tokens[2]/tokens[3]
1184 * range has to be equal.
1185 */
1186 for (i = 0; i < 4; i++) {
1187 tokens[2] = i;
1188 tokens[3] = i;
1189 int pos = _mesa_add_state_reference(prog->Parameters,
1190 (gl_state_index *)tokens);
1191 if (base_pos == -1)
1192 base_pos = pos;
1193 else
1194 assert(base_pos + i == pos);
1195 }
1196
1197 return base_pos;
1198 }
1199
1200 static variable_storage *
1201 get_builtin_matrix_ref(void *mem_ctx, struct gl_program *prog, ir_variable *var,
1202 ir_rvalue *array_index)
1203 {
1204 /*
1205 * NOTE: The ARB_vertex_program extension specified that matrices get
1206 * loaded in registers in row-major order. With GLSL, we want column-
1207 * major order. So, we need to transpose all matrices here...
1208 */
1209 static const struct {
1210 const char *name;
1211 int matrix;
1212 int modifier;
1213 } matrices[] = {
1214 { "gl_ModelViewMatrix", STATE_MODELVIEW_MATRIX, STATE_MATRIX_TRANSPOSE },
1215 { "gl_ModelViewMatrixInverse", STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVTRANS },
1216 { "gl_ModelViewMatrixTranspose", STATE_MODELVIEW_MATRIX, 0 },
1217 { "gl_ModelViewMatrixInverseTranspose", STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVERSE },
1218
1219 { "gl_ProjectionMatrix", STATE_PROJECTION_MATRIX, STATE_MATRIX_TRANSPOSE },
1220 { "gl_ProjectionMatrixInverse", STATE_PROJECTION_MATRIX, STATE_MATRIX_INVTRANS },
1221 { "gl_ProjectionMatrixTranspose", STATE_PROJECTION_MATRIX, 0 },
1222 { "gl_ProjectionMatrixInverseTranspose", STATE_PROJECTION_MATRIX, STATE_MATRIX_INVERSE },
1223
1224 { "gl_ModelViewProjectionMatrix", STATE_MVP_MATRIX, STATE_MATRIX_TRANSPOSE },
1225 { "gl_ModelViewProjectionMatrixInverse", STATE_MVP_MATRIX, STATE_MATRIX_INVTRANS },
1226 { "gl_ModelViewProjectionMatrixTranspose", STATE_MVP_MATRIX, 0 },
1227 { "gl_ModelViewProjectionMatrixInverseTranspose", STATE_MVP_MATRIX, STATE_MATRIX_INVERSE },
1228
1229 { "gl_TextureMatrix", STATE_TEXTURE_MATRIX, STATE_MATRIX_TRANSPOSE },
1230 { "gl_TextureMatrixInverse", STATE_TEXTURE_MATRIX, STATE_MATRIX_INVTRANS },
1231 { "gl_TextureMatrixTranspose", STATE_TEXTURE_MATRIX, 0 },
1232 { "gl_TextureMatrixInverseTranspose", STATE_TEXTURE_MATRIX, STATE_MATRIX_INVERSE },
1233
1234 { "gl_NormalMatrix", STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVERSE },
1235
1236 };
1237 unsigned int i;
1238 variable_storage *entry;
1239
1240 /* C++ gets angry when we try to use an int as a gl_state_index, so we use
1241 * ints for gl_state_index. Make sure they're compatible.
1242 */
1243 assert(sizeof(gl_state_index) == sizeof(int));
1244
1245 for (i = 0; i < Elements(matrices); i++) {
1246 if (strcmp(var->name, matrices[i].name) == 0) {
1247 int tokens[STATE_LENGTH];
1248 int base_pos = -1;
1249
1250 tokens[0] = matrices[i].matrix;
1251 tokens[4] = matrices[i].modifier;
1252 if (matrices[i].matrix == STATE_TEXTURE_MATRIX) {
1253 ir_constant *index = array_index->constant_expression_value();
1254 if (index) {
1255 tokens[1] = index->value.i[0];
1256 base_pos = add_matrix_ref(prog, tokens);
1257 } else {
1258 for (i = 0; i < var->type->length; i++) {
1259 tokens[1] = i;
1260 int pos = add_matrix_ref(prog, tokens);
1261 if (base_pos == -1)
1262 base_pos = pos;
1263 else
1264 assert(base_pos + (int)i * 4 == pos);
1265 }
1266 }
1267 } else {
1268 tokens[1] = 0; /* unused array index */
1269 base_pos = add_matrix_ref(prog, tokens);
1270 }
1271 tokens[4] = matrices[i].modifier;
1272
1273 entry = new(mem_ctx) variable_storage(var,
1274 PROGRAM_STATE_VAR,
1275 base_pos);
1276
1277 return entry;
1278 }
1279 }
1280
1281 return NULL;
1282 }
1283
1284 int
1285 ir_to_mesa_visitor::add_uniform(const char *name,
1286 const glsl_type *type,
1287 ir_constant *constant)
1288 {
1289 int len;
1290
1291 if (type->is_vector() ||
1292 type->is_scalar()) {
1293 len = type->vector_elements;
1294 } else {
1295 len = type_size(type) * 4;
1296 }
1297
1298 float *values = NULL;
1299 if (constant && type->is_array()) {
1300 values = (float *)malloc(type->length * 4 * sizeof(float));
1301
1302 assert(type->fields.array->is_scalar() ||
1303 type->fields.array->is_vector() ||
1304 !"FINISHME: uniform array initializers for non-vector");
1305
1306 for (unsigned int i = 0; i < type->length; i++) {
1307 ir_constant *element = constant->array_elements[i];
1308 unsigned int c;
1309
1310 for (c = 0; c < type->fields.array->vector_elements; c++) {
1311 switch (type->fields.array->base_type) {
1312 case GLSL_TYPE_FLOAT:
1313 values[4 * i + c] = element->value.f[c];
1314 break;
1315 case GLSL_TYPE_INT:
1316 values[4 * i + c] = element->value.i[c];
1317 break;
1318 case GLSL_TYPE_UINT:
1319 values[4 * i + c] = element->value.u[c];
1320 break;
1321 case GLSL_TYPE_BOOL:
1322 values[4 * i + c] = element->value.b[c];
1323 break;
1324 default:
1325 assert(!"not reached");
1326 }
1327 }
1328 }
1329 } else if (constant) {
1330 values = (float *)malloc(16 * sizeof(float));
1331 for (unsigned int i = 0; i < type->components(); i++) {
1332 switch (type->base_type) {
1333 case GLSL_TYPE_FLOAT:
1334 values[i] = constant->value.f[i];
1335 break;
1336 case GLSL_TYPE_INT:
1337 values[i] = constant->value.i[i];
1338 break;
1339 case GLSL_TYPE_UINT:
1340 values[i] = constant->value.u[i];
1341 break;
1342 case GLSL_TYPE_BOOL:
1343 values[i] = constant->value.b[i];
1344 break;
1345 default:
1346 assert(!"not reached");
1347 }
1348 }
1349 }
1350
1351 int loc = _mesa_add_uniform(this->prog->Parameters,
1352 name,
1353 len,
1354 type->gl_type,
1355 values);
1356 free(values);
1357
1358 return loc;
1359 }
1360
1361 /* Recursively add all the members of the aggregate uniform as uniform names
1362 * to Mesa, moving those uniforms to our structured temporary.
1363 */
1364 void
1365 ir_to_mesa_visitor::add_aggregate_uniform(ir_instruction *ir,
1366 const char *name,
1367 const struct glsl_type *type,
1368 ir_constant *constant,
1369 struct ir_to_mesa_dst_reg temp)
1370 {
1371 int loc;
1372
1373 if (type->is_record()) {
1374 void *mem_ctx = talloc_new(NULL);
1375 ir_constant *field_constant = NULL;
1376
1377 if (constant)
1378 field_constant = (ir_constant *)constant->components.get_head();
1379
1380 for (unsigned int i = 0; i < type->length; i++) {
1381 const glsl_type *field_type = type->fields.structure[i].type;
1382
1383 add_aggregate_uniform(ir,
1384 talloc_asprintf(mem_ctx, "%s.%s", name,
1385 type->fields.structure[i].name),
1386 field_type, field_constant, temp);
1387 temp.index += type_size(field_type);
1388
1389 if (constant)
1390 field_constant = (ir_constant *)field_constant->next;
1391 }
1392
1393 talloc_free(mem_ctx);
1394
1395 return;
1396 }
1397
1398 assert(type->is_vector() || type->is_scalar() || !"FINISHME: other types");
1399
1400 loc = add_uniform(name, type, constant);
1401
1402 ir_to_mesa_src_reg uniform(PROGRAM_UNIFORM, loc, type);
1403
1404 for (int i = 0; i < type_size(type); i++) {
1405 ir_to_mesa_emit_op1(ir, OPCODE_MOV, temp, uniform);
1406 temp.index++;
1407 uniform.index++;
1408 }
1409 }
1410
1411
1412 void
1413 ir_to_mesa_visitor::visit(ir_dereference_variable *ir)
1414 {
1415 variable_storage *entry = find_variable_storage(ir->var);
1416 unsigned int loc;
1417
1418 if (!entry) {
1419 switch (ir->var->mode) {
1420 case ir_var_uniform:
1421 entry = get_builtin_matrix_ref(this->mem_ctx, this->prog, ir->var,
1422 NULL);
1423 if (entry)
1424 break;
1425
1426 /* FINISHME: Fix up uniform name for arrays and things */
1427 if (ir->var->type->base_type == GLSL_TYPE_SAMPLER ||
1428 (ir->var->type->base_type == GLSL_TYPE_ARRAY &&
1429 ir->var->type->fields.array->base_type == GLSL_TYPE_SAMPLER)) {
1430 int array_length;
1431
1432 if (ir->var->type->base_type == GLSL_TYPE_ARRAY)
1433 array_length = ir->var->type->length;
1434 else
1435 array_length = 1;
1436 int sampler = _mesa_add_sampler(this->prog->Parameters,
1437 ir->var->name,
1438 ir->var->type->gl_type,
1439 array_length);
1440 set_sampler_location(ir->var, sampler);
1441
1442 entry = new(mem_ctx) variable_storage(ir->var, PROGRAM_SAMPLER,
1443 sampler);
1444 this->variables.push_tail(entry);
1445 break;
1446 }
1447
1448 assert(ir->var->type->gl_type != 0 &&
1449 ir->var->type->gl_type != GL_INVALID_ENUM);
1450
1451 /* Oh, the joy of aggregate types in Mesa. Like constants,
1452 * we can only really do vec4s. So, make a temp, chop the
1453 * aggregate up into vec4s, and move those vec4s to the temp.
1454 */
1455 if (ir->var->type->is_record()) {
1456 ir_to_mesa_src_reg temp = get_temp(ir->var->type);
1457
1458 entry = new(mem_ctx) variable_storage(ir->var,
1459 temp.file,
1460 temp.index);
1461 this->variables.push_tail(entry);
1462
1463 add_aggregate_uniform(ir->var, ir->var->name, ir->var->type,
1464 ir->var->constant_value,
1465 ir_to_mesa_dst_reg_from_src(temp));
1466 break;
1467 }
1468
1469 loc = add_uniform(ir->var->name,
1470 ir->var->type,
1471 ir->var->constant_value);
1472
1473 /* Always mark the uniform used at this point. If it isn't
1474 * used, dead code elimination should have nuked the decl already.
1475 */
1476 this->prog->Parameters->Parameters[loc].Used = GL_TRUE;
1477
1478 entry = new(mem_ctx) variable_storage(ir->var, PROGRAM_UNIFORM, loc);
1479 this->variables.push_tail(entry);
1480 break;
1481 case ir_var_in:
1482 case ir_var_out:
1483 case ir_var_inout:
1484 /* The linker assigns locations for varyings and attributes,
1485 * including deprecated builtins (like gl_Color), user-assign
1486 * generic attributes (glBindVertexLocation), and
1487 * user-defined varyings.
1488 *
1489 * FINISHME: We would hit this path for function arguments. Fix!
1490 */
1491 assert(ir->var->location != -1);
1492 if (ir->var->mode == ir_var_in ||
1493 ir->var->mode == ir_var_inout) {
1494 entry = new(mem_ctx) variable_storage(ir->var,
1495 PROGRAM_INPUT,
1496 ir->var->location);
1497
1498 if (this->prog->Target == GL_VERTEX_PROGRAM_ARB &&
1499 ir->var->location >= VERT_ATTRIB_GENERIC0) {
1500 _mesa_add_attribute(prog->Attributes,
1501 ir->var->name,
1502 _mesa_sizeof_glsl_type(ir->var->type->gl_type),
1503 ir->var->type->gl_type,
1504 ir->var->location - VERT_ATTRIB_GENERIC0);
1505 }
1506 } else {
1507 entry = new(mem_ctx) variable_storage(ir->var,
1508 PROGRAM_OUTPUT,
1509 ir->var->location);
1510 }
1511
1512 break;
1513 case ir_var_auto:
1514 case ir_var_temporary:
1515 entry = new(mem_ctx) variable_storage(ir->var, PROGRAM_TEMPORARY,
1516 this->next_temp);
1517 this->variables.push_tail(entry);
1518
1519 next_temp += type_size(ir->var->type);
1520 break;
1521 }
1522
1523 if (!entry) {
1524 printf("Failed to make storage for %s\n", ir->var->name);
1525 exit(1);
1526 }
1527 }
1528
1529 this->result = ir_to_mesa_src_reg(entry->file, entry->index, ir->var->type);
1530 }
1531
1532 void
1533 ir_to_mesa_visitor::visit(ir_dereference_array *ir)
1534 {
1535 ir_variable *var = ir->variable_referenced();
1536 ir_constant *index;
1537 ir_to_mesa_src_reg src_reg;
1538 ir_dereference_variable *deref_var = ir->array->as_dereference_variable();
1539 int element_size = type_size(ir->type);
1540
1541 index = ir->array_index->constant_expression_value();
1542
1543 if (deref_var && strncmp(deref_var->var->name,
1544 "gl_TextureMatrix",
1545 strlen("gl_TextureMatrix")) == 0) {
1546 struct variable_storage *entry;
1547
1548 entry = get_builtin_matrix_ref(this->mem_ctx, this->prog, deref_var->var,
1549 ir->array_index);
1550 assert(entry);
1551
1552 ir_to_mesa_src_reg src_reg(entry->file, entry->index, ir->type);
1553
1554 if (index) {
1555 src_reg.reladdr = NULL;
1556 } else {
1557 ir_to_mesa_src_reg index_reg = get_temp(glsl_type::float_type);
1558
1559 ir->array_index->accept(this);
1560 ir_to_mesa_emit_op2(ir, OPCODE_MUL,
1561 ir_to_mesa_dst_reg_from_src(index_reg),
1562 this->result, src_reg_for_float(element_size));
1563
1564 src_reg.reladdr = talloc(mem_ctx, ir_to_mesa_src_reg);
1565 memcpy(src_reg.reladdr, &index_reg, sizeof(index_reg));
1566 }
1567
1568 this->result = src_reg;
1569 return;
1570 }
1571
1572 if (strncmp(var->name, "gl_", 3) == 0 && var->mode == ir_var_uniform &&
1573 !var->type->is_matrix()) {
1574 ir_dereference_record *record = NULL;
1575 if (ir->array->ir_type == ir_type_dereference_record)
1576 record = (ir_dereference_record *)ir->array;
1577
1578 assert(index || !"FINISHME: variable-indexed builtin uniform access");
1579
1580 this->result = get_builtin_uniform_reg(prog,
1581 var->name,
1582 index->value.i[0],
1583 record ? record->field : NULL);
1584 }
1585
1586 ir->array->accept(this);
1587 src_reg = this->result;
1588
1589 if (index) {
1590 src_reg.index += index->value.i[0] * element_size;
1591 } else {
1592 ir_to_mesa_src_reg array_base = this->result;
1593 /* Variable index array dereference. It eats the "vec4" of the
1594 * base of the array and an index that offsets the Mesa register
1595 * index.
1596 */
1597 ir->array_index->accept(this);
1598
1599 ir_to_mesa_src_reg index_reg;
1600
1601 if (element_size == 1) {
1602 index_reg = this->result;
1603 } else {
1604 index_reg = get_temp(glsl_type::float_type);
1605
1606 ir_to_mesa_emit_op2(ir, OPCODE_MUL,
1607 ir_to_mesa_dst_reg_from_src(index_reg),
1608 this->result, src_reg_for_float(element_size));
1609 }
1610
1611 src_reg.reladdr = talloc(mem_ctx, ir_to_mesa_src_reg);
1612 memcpy(src_reg.reladdr, &index_reg, sizeof(index_reg));
1613 }
1614
1615 /* If the type is smaller than a vec4, replicate the last channel out. */
1616 if (ir->type->is_scalar() || ir->type->is_vector())
1617 src_reg.swizzle = swizzle_for_size(ir->type->vector_elements);
1618 else
1619 src_reg.swizzle = SWIZZLE_NOOP;
1620
1621 this->result = src_reg;
1622 }
1623
1624 void
1625 ir_to_mesa_visitor::visit(ir_dereference_record *ir)
1626 {
1627 unsigned int i;
1628 const glsl_type *struct_type = ir->record->type;
1629 int offset = 0;
1630 ir_variable *var = ir->record->variable_referenced();
1631
1632 if (strncmp(var->name, "gl_", 3) == 0 && var->mode == ir_var_uniform) {
1633 assert(var);
1634
1635 this->result = get_builtin_uniform_reg(prog,
1636 var->name,
1637 0,
1638 ir->field);
1639 return;
1640 }
1641
1642 ir->record->accept(this);
1643
1644 for (i = 0; i < struct_type->length; i++) {
1645 if (strcmp(struct_type->fields.structure[i].name, ir->field) == 0)
1646 break;
1647 offset += type_size(struct_type->fields.structure[i].type);
1648 }
1649 this->result.swizzle = swizzle_for_size(ir->type->vector_elements);
1650 this->result.index += offset;
1651 }
1652
1653 /**
1654 * We want to be careful in assignment setup to hit the actual storage
1655 * instead of potentially using a temporary like we might with the
1656 * ir_dereference handler.
1657 */
1658 static struct ir_to_mesa_dst_reg
1659 get_assignment_lhs(ir_dereference *ir, ir_to_mesa_visitor *v)
1660 {
1661 /* The LHS must be a dereference. If the LHS is a variable indexed array
1662 * access of a vector, it must be separated into a series conditional moves
1663 * before reaching this point (see ir_vec_index_to_cond_assign).
1664 */
1665 assert(ir->as_dereference());
1666 ir_dereference_array *deref_array = ir->as_dereference_array();
1667 if (deref_array) {
1668 assert(!deref_array->array->type->is_vector());
1669 }
1670
1671 /* Use the rvalue deref handler for the most part. We'll ignore
1672 * swizzles in it and write swizzles using writemask, though.
1673 */
1674 ir->accept(v);
1675 return ir_to_mesa_dst_reg_from_src(v->result);
1676 }
1677
1678 void
1679 ir_to_mesa_visitor::visit(ir_assignment *ir)
1680 {
1681 struct ir_to_mesa_dst_reg l;
1682 struct ir_to_mesa_src_reg r;
1683 int i;
1684
1685 ir->rhs->accept(this);
1686 r = this->result;
1687
1688 l = get_assignment_lhs(ir->lhs, this);
1689
1690 /* FINISHME: This should really set to the correct maximal writemask for each
1691 * FINISHME: component written (in the loops below). This case can only
1692 * FINISHME: occur for matrices, arrays, and structures.
1693 */
1694 if (ir->write_mask == 0) {
1695 assert(!ir->lhs->type->is_scalar() && !ir->lhs->type->is_vector());
1696 l.writemask = WRITEMASK_XYZW;
1697 } else if (ir->lhs->type->is_scalar()) {
1698 /* FINISHME: This hack makes writing to gl_FragData, which lives in the
1699 * FINISHME: W component of fragment shader output zero, work correctly.
1700 */
1701 l.writemask = WRITEMASK_XYZW;
1702 } else {
1703 assert(ir->lhs->type->is_vector());
1704 l.writemask = ir->write_mask;
1705 }
1706
1707 assert(l.file != PROGRAM_UNDEFINED);
1708 assert(r.file != PROGRAM_UNDEFINED);
1709
1710 if (ir->condition) {
1711 ir_to_mesa_src_reg condition;
1712
1713 ir->condition->accept(this);
1714 condition = this->result;
1715
1716 /* We use the OPCODE_CMP (a < 0 ? b : c) for conditional moves,
1717 * and the condition we produced is 0.0 or 1.0. By flipping the
1718 * sign, we can choose which value OPCODE_CMP produces without
1719 * an extra computing the condition.
1720 */
1721 condition.negate = ~condition.negate;
1722 for (i = 0; i < type_size(ir->lhs->type); i++) {
1723 ir_to_mesa_emit_op3(ir, OPCODE_CMP, l,
1724 condition, r, ir_to_mesa_src_reg_from_dst(l));
1725 l.index++;
1726 r.index++;
1727 }
1728 } else {
1729 for (i = 0; i < type_size(ir->lhs->type); i++) {
1730 ir_to_mesa_emit_op1(ir, OPCODE_MOV, l, r);
1731 l.index++;
1732 r.index++;
1733 }
1734 }
1735 }
1736
1737
1738 void
1739 ir_to_mesa_visitor::visit(ir_constant *ir)
1740 {
1741 ir_to_mesa_src_reg src_reg;
1742 GLfloat stack_vals[4];
1743 GLfloat *values = stack_vals;
1744 unsigned int i;
1745
1746 /* Unfortunately, 4 floats is all we can get into
1747 * _mesa_add_unnamed_constant. So, make a temp to store an
1748 * aggregate constant and move each constant value into it. If we
1749 * get lucky, copy propagation will eliminate the extra moves.
1750 */
1751
1752 if (ir->type->base_type == GLSL_TYPE_STRUCT) {
1753 ir_to_mesa_src_reg temp_base = get_temp(ir->type);
1754 ir_to_mesa_dst_reg temp = ir_to_mesa_dst_reg_from_src(temp_base);
1755
1756 foreach_iter(exec_list_iterator, iter, ir->components) {
1757 ir_constant *field_value = (ir_constant *)iter.get();
1758 int size = type_size(field_value->type);
1759
1760 assert(size > 0);
1761
1762 field_value->accept(this);
1763 src_reg = this->result;
1764
1765 for (i = 0; i < (unsigned int)size; i++) {
1766 ir_to_mesa_emit_op1(ir, OPCODE_MOV, temp, src_reg);
1767
1768 src_reg.index++;
1769 temp.index++;
1770 }
1771 }
1772 this->result = temp_base;
1773 return;
1774 }
1775
1776 if (ir->type->is_array()) {
1777 ir_to_mesa_src_reg temp_base = get_temp(ir->type);
1778 ir_to_mesa_dst_reg temp = ir_to_mesa_dst_reg_from_src(temp_base);
1779 int size = type_size(ir->type->fields.array);
1780
1781 assert(size > 0);
1782
1783 for (i = 0; i < ir->type->length; i++) {
1784 ir->array_elements[i]->accept(this);
1785 src_reg = this->result;
1786 for (int j = 0; j < size; j++) {
1787 ir_to_mesa_emit_op1(ir, OPCODE_MOV, temp, src_reg);
1788
1789 src_reg.index++;
1790 temp.index++;
1791 }
1792 }
1793 this->result = temp_base;
1794 return;
1795 }
1796
1797 if (ir->type->is_matrix()) {
1798 ir_to_mesa_src_reg mat = get_temp(ir->type);
1799 ir_to_mesa_dst_reg mat_column = ir_to_mesa_dst_reg_from_src(mat);
1800
1801 for (i = 0; i < ir->type->matrix_columns; i++) {
1802 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
1803 values = &ir->value.f[i * ir->type->vector_elements];
1804
1805 src_reg = ir_to_mesa_src_reg(PROGRAM_CONSTANT, -1, NULL);
1806 src_reg.index = _mesa_add_unnamed_constant(this->prog->Parameters,
1807 values,
1808 ir->type->vector_elements,
1809 &src_reg.swizzle);
1810 ir_to_mesa_emit_op1(ir, OPCODE_MOV, mat_column, src_reg);
1811
1812 mat_column.index++;
1813 }
1814
1815 this->result = mat;
1816 }
1817
1818 src_reg.file = PROGRAM_CONSTANT;
1819 switch (ir->type->base_type) {
1820 case GLSL_TYPE_FLOAT:
1821 values = &ir->value.f[0];
1822 break;
1823 case GLSL_TYPE_UINT:
1824 for (i = 0; i < ir->type->vector_elements; i++) {
1825 values[i] = ir->value.u[i];
1826 }
1827 break;
1828 case GLSL_TYPE_INT:
1829 for (i = 0; i < ir->type->vector_elements; i++) {
1830 values[i] = ir->value.i[i];
1831 }
1832 break;
1833 case GLSL_TYPE_BOOL:
1834 for (i = 0; i < ir->type->vector_elements; i++) {
1835 values[i] = ir->value.b[i];
1836 }
1837 break;
1838 default:
1839 assert(!"Non-float/uint/int/bool constant");
1840 }
1841
1842 this->result = ir_to_mesa_src_reg(PROGRAM_CONSTANT, -1, ir->type);
1843 this->result.index = _mesa_add_unnamed_constant(this->prog->Parameters,
1844 values,
1845 ir->type->vector_elements,
1846 &this->result.swizzle);
1847 }
1848
1849 function_entry *
1850 ir_to_mesa_visitor::get_function_signature(ir_function_signature *sig)
1851 {
1852 function_entry *entry;
1853
1854 foreach_iter(exec_list_iterator, iter, this->function_signatures) {
1855 entry = (function_entry *)iter.get();
1856
1857 if (entry->sig == sig)
1858 return entry;
1859 }
1860
1861 entry = talloc(mem_ctx, function_entry);
1862 entry->sig = sig;
1863 entry->sig_id = this->next_signature_id++;
1864 entry->bgn_inst = NULL;
1865
1866 /* Allocate storage for all the parameters. */
1867 foreach_iter(exec_list_iterator, iter, sig->parameters) {
1868 ir_variable *param = (ir_variable *)iter.get();
1869 variable_storage *storage;
1870
1871 storage = find_variable_storage(param);
1872 assert(!storage);
1873
1874 storage = new(mem_ctx) variable_storage(param, PROGRAM_TEMPORARY,
1875 this->next_temp);
1876 this->variables.push_tail(storage);
1877
1878 this->next_temp += type_size(param->type);
1879 }
1880
1881 if (!sig->return_type->is_void()) {
1882 entry->return_reg = get_temp(sig->return_type);
1883 } else {
1884 entry->return_reg = ir_to_mesa_undef;
1885 }
1886
1887 this->function_signatures.push_tail(entry);
1888 return entry;
1889 }
1890
1891 void
1892 ir_to_mesa_visitor::visit(ir_call *ir)
1893 {
1894 ir_to_mesa_instruction *call_inst;
1895 ir_function_signature *sig = ir->get_callee();
1896 function_entry *entry = get_function_signature(sig);
1897 int i;
1898
1899 /* Process in parameters. */
1900 exec_list_iterator sig_iter = sig->parameters.iterator();
1901 foreach_iter(exec_list_iterator, iter, *ir) {
1902 ir_rvalue *param_rval = (ir_rvalue *)iter.get();
1903 ir_variable *param = (ir_variable *)sig_iter.get();
1904
1905 if (param->mode == ir_var_in ||
1906 param->mode == ir_var_inout) {
1907 variable_storage *storage = find_variable_storage(param);
1908 assert(storage);
1909
1910 param_rval->accept(this);
1911 ir_to_mesa_src_reg r = this->result;
1912
1913 ir_to_mesa_dst_reg l;
1914 l.file = storage->file;
1915 l.index = storage->index;
1916 l.reladdr = NULL;
1917 l.writemask = WRITEMASK_XYZW;
1918 l.cond_mask = COND_TR;
1919
1920 for (i = 0; i < type_size(param->type); i++) {
1921 ir_to_mesa_emit_op1(ir, OPCODE_MOV, l, r);
1922 l.index++;
1923 r.index++;
1924 }
1925 }
1926
1927 sig_iter.next();
1928 }
1929 assert(!sig_iter.has_next());
1930
1931 /* Emit call instruction */
1932 call_inst = ir_to_mesa_emit_op1(ir, OPCODE_CAL,
1933 ir_to_mesa_undef_dst, ir_to_mesa_undef);
1934 call_inst->function = entry;
1935
1936 /* Process out parameters. */
1937 sig_iter = sig->parameters.iterator();
1938 foreach_iter(exec_list_iterator, iter, *ir) {
1939 ir_rvalue *param_rval = (ir_rvalue *)iter.get();
1940 ir_variable *param = (ir_variable *)sig_iter.get();
1941
1942 if (param->mode == ir_var_out ||
1943 param->mode == ir_var_inout) {
1944 variable_storage *storage = find_variable_storage(param);
1945 assert(storage);
1946
1947 ir_to_mesa_src_reg r;
1948 r.file = storage->file;
1949 r.index = storage->index;
1950 r.reladdr = NULL;
1951 r.swizzle = SWIZZLE_NOOP;
1952 r.negate = 0;
1953
1954 param_rval->accept(this);
1955 ir_to_mesa_dst_reg l = ir_to_mesa_dst_reg_from_src(this->result);
1956
1957 for (i = 0; i < type_size(param->type); i++) {
1958 ir_to_mesa_emit_op1(ir, OPCODE_MOV, l, r);
1959 l.index++;
1960 r.index++;
1961 }
1962 }
1963
1964 sig_iter.next();
1965 }
1966 assert(!sig_iter.has_next());
1967
1968 /* Process return value. */
1969 this->result = entry->return_reg;
1970 }
1971
1972
1973 void
1974 ir_to_mesa_visitor::visit(ir_texture *ir)
1975 {
1976 ir_to_mesa_src_reg result_src, coord, lod_info, projector;
1977 ir_to_mesa_dst_reg result_dst, coord_dst;
1978 ir_to_mesa_instruction *inst = NULL;
1979 prog_opcode opcode = OPCODE_NOP;
1980
1981 ir->coordinate->accept(this);
1982
1983 /* Put our coords in a temp. We'll need to modify them for shadow,
1984 * projection, or LOD, so the only case we'd use it as is is if
1985 * we're doing plain old texturing. Mesa IR optimization should
1986 * handle cleaning up our mess in that case.
1987 */
1988 coord = get_temp(glsl_type::vec4_type);
1989 coord_dst = ir_to_mesa_dst_reg_from_src(coord);
1990 ir_to_mesa_emit_op1(ir, OPCODE_MOV, coord_dst,
1991 this->result);
1992
1993 if (ir->projector) {
1994 ir->projector->accept(this);
1995 projector = this->result;
1996 }
1997
1998 /* Storage for our result. Ideally for an assignment we'd be using
1999 * the actual storage for the result here, instead.
2000 */
2001 result_src = get_temp(glsl_type::vec4_type);
2002 result_dst = ir_to_mesa_dst_reg_from_src(result_src);
2003
2004 switch (ir->op) {
2005 case ir_tex:
2006 opcode = OPCODE_TEX;
2007 break;
2008 case ir_txb:
2009 opcode = OPCODE_TXB;
2010 ir->lod_info.bias->accept(this);
2011 lod_info = this->result;
2012 break;
2013 case ir_txl:
2014 opcode = OPCODE_TXL;
2015 ir->lod_info.lod->accept(this);
2016 lod_info = this->result;
2017 break;
2018 case ir_txd:
2019 case ir_txf:
2020 assert(!"GLSL 1.30 features unsupported");
2021 break;
2022 }
2023
2024 if (ir->projector) {
2025 if (opcode == OPCODE_TEX) {
2026 /* Slot the projector in as the last component of the coord. */
2027 coord_dst.writemask = WRITEMASK_W;
2028 ir_to_mesa_emit_op1(ir, OPCODE_MOV, coord_dst, projector);
2029 coord_dst.writemask = WRITEMASK_XYZW;
2030 opcode = OPCODE_TXP;
2031 } else {
2032 ir_to_mesa_src_reg coord_w = coord;
2033 coord_w.swizzle = SWIZZLE_WWWW;
2034
2035 /* For the other TEX opcodes there's no projective version
2036 * since the last slot is taken up by lod info. Do the
2037 * projective divide now.
2038 */
2039 coord_dst.writemask = WRITEMASK_W;
2040 ir_to_mesa_emit_op1(ir, OPCODE_RCP, coord_dst, projector);
2041
2042 coord_dst.writemask = WRITEMASK_XYZ;
2043 ir_to_mesa_emit_op2(ir, OPCODE_MUL, coord_dst, coord, coord_w);
2044
2045 coord_dst.writemask = WRITEMASK_XYZW;
2046 coord.swizzle = SWIZZLE_XYZW;
2047 }
2048 }
2049
2050 if (ir->shadow_comparitor) {
2051 /* Slot the shadow value in as the second to last component of the
2052 * coord.
2053 */
2054 ir->shadow_comparitor->accept(this);
2055 coord_dst.writemask = WRITEMASK_Z;
2056 ir_to_mesa_emit_op1(ir, OPCODE_MOV, coord_dst, this->result);
2057 coord_dst.writemask = WRITEMASK_XYZW;
2058 }
2059
2060 if (opcode == OPCODE_TXL || opcode == OPCODE_TXB) {
2061 /* Mesa IR stores lod or lod bias in the last channel of the coords. */
2062 coord_dst.writemask = WRITEMASK_W;
2063 ir_to_mesa_emit_op1(ir, OPCODE_MOV, coord_dst, lod_info);
2064 coord_dst.writemask = WRITEMASK_XYZW;
2065 }
2066
2067 inst = ir_to_mesa_emit_op1(ir, opcode, result_dst, coord);
2068
2069 if (ir->shadow_comparitor)
2070 inst->tex_shadow = GL_TRUE;
2071
2072 ir_variable *sampler = ir->sampler->variable_referenced();
2073
2074 /* generate the mapping, remove when we generate storage at
2075 * declaration time
2076 */
2077 ir->sampler->accept(this);
2078
2079 inst->sampler = get_sampler_location(sampler);
2080
2081 ir_dereference_array *sampler_array = ir->sampler->as_dereference_array();
2082 if (sampler_array) {
2083 ir_constant *array_index =
2084 sampler_array->array_index->constant_expression_value();
2085
2086 /* GLSL 1.10 and 1.20 allowed variable sampler array indices,
2087 * while GLSL 1.30 requires that the array indices be constant
2088 * integer expressions. We don't expect any driver to actually
2089 * work with a really variable array index, and in 1.20 all that
2090 * would work would be an unrolled loop counter, so assert that
2091 * we ended up with a constant at least..
2092 */
2093 assert(array_index);
2094 inst->sampler += array_index->value.i[0];
2095 }
2096
2097 const glsl_type *sampler_type = sampler->type;
2098 while (sampler_type->base_type == GLSL_TYPE_ARRAY)
2099 sampler_type = sampler_type->fields.array;
2100
2101 switch (sampler_type->sampler_dimensionality) {
2102 case GLSL_SAMPLER_DIM_1D:
2103 inst->tex_target = (sampler_type->sampler_array)
2104 ? TEXTURE_1D_ARRAY_INDEX : TEXTURE_1D_INDEX;
2105 break;
2106 case GLSL_SAMPLER_DIM_2D:
2107 inst->tex_target = (sampler_type->sampler_array)
2108 ? TEXTURE_2D_ARRAY_INDEX : TEXTURE_2D_INDEX;
2109 break;
2110 case GLSL_SAMPLER_DIM_3D:
2111 inst->tex_target = TEXTURE_3D_INDEX;
2112 break;
2113 case GLSL_SAMPLER_DIM_CUBE:
2114 inst->tex_target = TEXTURE_CUBE_INDEX;
2115 break;
2116 case GLSL_SAMPLER_DIM_RECT:
2117 inst->tex_target = TEXTURE_RECT_INDEX;
2118 break;
2119 case GLSL_SAMPLER_DIM_BUF:
2120 assert(!"FINISHME: Implement ARB_texture_buffer_object");
2121 break;
2122 default:
2123 assert(!"Should not get here.");
2124 }
2125
2126 this->result = result_src;
2127 }
2128
2129 void
2130 ir_to_mesa_visitor::visit(ir_return *ir)
2131 {
2132 if (ir->get_value()) {
2133 ir_to_mesa_dst_reg l;
2134 int i;
2135
2136 assert(current_function);
2137
2138 ir->get_value()->accept(this);
2139 ir_to_mesa_src_reg r = this->result;
2140
2141 l = ir_to_mesa_dst_reg_from_src(current_function->return_reg);
2142
2143 for (i = 0; i < type_size(current_function->sig->return_type); i++) {
2144 ir_to_mesa_emit_op1(ir, OPCODE_MOV, l, r);
2145 l.index++;
2146 r.index++;
2147 }
2148 }
2149
2150 ir_to_mesa_emit_op0(ir, OPCODE_RET);
2151 }
2152
2153 void
2154 ir_to_mesa_visitor::visit(ir_discard *ir)
2155 {
2156 assert(ir->condition == NULL); /* FINISHME */
2157
2158 ir_to_mesa_emit_op0(ir, OPCODE_KIL_NV);
2159 }
2160
2161 void
2162 ir_to_mesa_visitor::visit(ir_if *ir)
2163 {
2164 ir_to_mesa_instruction *cond_inst, *if_inst, *else_inst = NULL;
2165 ir_to_mesa_instruction *prev_inst;
2166
2167 prev_inst = (ir_to_mesa_instruction *)this->instructions.get_tail();
2168
2169 ir->condition->accept(this);
2170 assert(this->result.file != PROGRAM_UNDEFINED);
2171
2172 if (ctx->Shader.EmitCondCodes) {
2173 cond_inst = (ir_to_mesa_instruction *)this->instructions.get_tail();
2174
2175 /* See if we actually generated any instruction for generating
2176 * the condition. If not, then cook up a move to a temp so we
2177 * have something to set cond_update on.
2178 */
2179 if (cond_inst == prev_inst) {
2180 ir_to_mesa_src_reg temp = get_temp(glsl_type::bool_type);
2181 cond_inst = ir_to_mesa_emit_op1(ir->condition, OPCODE_MOV,
2182 ir_to_mesa_dst_reg_from_src(temp),
2183 result);
2184 }
2185 cond_inst->cond_update = GL_TRUE;
2186
2187 if_inst = ir_to_mesa_emit_op0(ir->condition, OPCODE_IF);
2188 if_inst->dst_reg.cond_mask = COND_NE;
2189 } else {
2190 if_inst = ir_to_mesa_emit_op1(ir->condition,
2191 OPCODE_IF, ir_to_mesa_undef_dst,
2192 this->result);
2193 }
2194
2195 this->instructions.push_tail(if_inst);
2196
2197 visit_exec_list(&ir->then_instructions, this);
2198
2199 if (!ir->else_instructions.is_empty()) {
2200 else_inst = ir_to_mesa_emit_op0(ir->condition, OPCODE_ELSE);
2201 visit_exec_list(&ir->else_instructions, this);
2202 }
2203
2204 if_inst = ir_to_mesa_emit_op1(ir->condition, OPCODE_ENDIF,
2205 ir_to_mesa_undef_dst, ir_to_mesa_undef);
2206 }
2207
2208 ir_to_mesa_visitor::ir_to_mesa_visitor()
2209 {
2210 result.file = PROGRAM_UNDEFINED;
2211 next_temp = 1;
2212 next_signature_id = 1;
2213 sampler_map = NULL;
2214 current_function = NULL;
2215 mem_ctx = talloc_new(NULL);
2216 }
2217
2218 ir_to_mesa_visitor::~ir_to_mesa_visitor()
2219 {
2220 talloc_free(mem_ctx);
2221 if (this->sampler_map)
2222 hash_table_dtor(this->sampler_map);
2223 }
2224
2225 static struct prog_src_register
2226 mesa_src_reg_from_ir_src_reg(ir_to_mesa_src_reg reg)
2227 {
2228 struct prog_src_register mesa_reg;
2229
2230 mesa_reg.File = reg.file;
2231 assert(reg.index < (1 << INST_INDEX_BITS) - 1);
2232 mesa_reg.Index = reg.index;
2233 mesa_reg.Swizzle = reg.swizzle;
2234 mesa_reg.RelAddr = reg.reladdr != NULL;
2235 mesa_reg.Negate = reg.negate;
2236 mesa_reg.Abs = 0;
2237 mesa_reg.HasIndex2 = GL_FALSE;
2238
2239 return mesa_reg;
2240 }
2241
2242 static void
2243 set_branchtargets(ir_to_mesa_visitor *v,
2244 struct prog_instruction *mesa_instructions,
2245 int num_instructions)
2246 {
2247 int if_count = 0, loop_count = 0;
2248 int *if_stack, *loop_stack;
2249 int if_stack_pos = 0, loop_stack_pos = 0;
2250 int i, j;
2251
2252 for (i = 0; i < num_instructions; i++) {
2253 switch (mesa_instructions[i].Opcode) {
2254 case OPCODE_IF:
2255 if_count++;
2256 break;
2257 case OPCODE_BGNLOOP:
2258 loop_count++;
2259 break;
2260 case OPCODE_BRK:
2261 case OPCODE_CONT:
2262 mesa_instructions[i].BranchTarget = -1;
2263 break;
2264 default:
2265 break;
2266 }
2267 }
2268
2269 if_stack = talloc_zero_array(v->mem_ctx, int, if_count);
2270 loop_stack = talloc_zero_array(v->mem_ctx, int, loop_count);
2271
2272 for (i = 0; i < num_instructions; i++) {
2273 switch (mesa_instructions[i].Opcode) {
2274 case OPCODE_IF:
2275 if_stack[if_stack_pos] = i;
2276 if_stack_pos++;
2277 break;
2278 case OPCODE_ELSE:
2279 mesa_instructions[if_stack[if_stack_pos - 1]].BranchTarget = i;
2280 if_stack[if_stack_pos - 1] = i;
2281 break;
2282 case OPCODE_ENDIF:
2283 mesa_instructions[if_stack[if_stack_pos - 1]].BranchTarget = i;
2284 if_stack_pos--;
2285 break;
2286 case OPCODE_BGNLOOP:
2287 loop_stack[loop_stack_pos] = i;
2288 loop_stack_pos++;
2289 break;
2290 case OPCODE_ENDLOOP:
2291 loop_stack_pos--;
2292 /* Rewrite any breaks/conts at this nesting level (haven't
2293 * already had a BranchTarget assigned) to point to the end
2294 * of the loop.
2295 */
2296 for (j = loop_stack[loop_stack_pos]; j < i; j++) {
2297 if (mesa_instructions[j].Opcode == OPCODE_BRK ||
2298 mesa_instructions[j].Opcode == OPCODE_CONT) {
2299 if (mesa_instructions[j].BranchTarget == -1) {
2300 mesa_instructions[j].BranchTarget = i;
2301 }
2302 }
2303 }
2304 /* The loop ends point at each other. */
2305 mesa_instructions[i].BranchTarget = loop_stack[loop_stack_pos];
2306 mesa_instructions[loop_stack[loop_stack_pos]].BranchTarget = i;
2307 break;
2308 case OPCODE_CAL:
2309 foreach_iter(exec_list_iterator, iter, v->function_signatures) {
2310 function_entry *entry = (function_entry *)iter.get();
2311
2312 if (entry->sig_id == mesa_instructions[i].BranchTarget) {
2313 mesa_instructions[i].BranchTarget = entry->inst;
2314 break;
2315 }
2316 }
2317 break;
2318 default:
2319 break;
2320 }
2321 }
2322 }
2323
2324 static void
2325 print_program(struct prog_instruction *mesa_instructions,
2326 ir_instruction **mesa_instruction_annotation,
2327 int num_instructions)
2328 {
2329 ir_instruction *last_ir = NULL;
2330 int i;
2331 int indent = 0;
2332
2333 for (i = 0; i < num_instructions; i++) {
2334 struct prog_instruction *mesa_inst = mesa_instructions + i;
2335 ir_instruction *ir = mesa_instruction_annotation[i];
2336
2337 fprintf(stdout, "%3d: ", i);
2338
2339 if (last_ir != ir && ir) {
2340 int j;
2341
2342 for (j = 0; j < indent; j++) {
2343 fprintf(stdout, " ");
2344 }
2345 ir->print();
2346 printf("\n");
2347 last_ir = ir;
2348
2349 fprintf(stdout, " "); /* line number spacing. */
2350 }
2351
2352 indent = _mesa_fprint_instruction_opt(stdout, mesa_inst, indent,
2353 PROG_PRINT_DEBUG, NULL);
2354 }
2355 }
2356
2357 static void
2358 count_resources(struct gl_program *prog)
2359 {
2360 unsigned int i;
2361
2362 prog->SamplersUsed = 0;
2363
2364 for (i = 0; i < prog->NumInstructions; i++) {
2365 struct prog_instruction *inst = &prog->Instructions[i];
2366
2367 /* Instead of just using the uniform's value to map to a
2368 * sampler, Mesa first allocates a separate number for the
2369 * sampler (_mesa_add_sampler), then we reindex it down to a
2370 * small integer (sampler_map[], SamplersUsed), then that gets
2371 * mapped to the uniform's value, and we get an actual sampler.
2372 */
2373 if (_mesa_is_tex_instruction(inst->Opcode)) {
2374 prog->SamplerTargets[inst->TexSrcUnit] =
2375 (gl_texture_index)inst->TexSrcTarget;
2376 prog->SamplersUsed |= 1 << inst->TexSrcUnit;
2377 if (inst->TexShadow) {
2378 prog->ShadowSamplers |= 1 << inst->TexSrcUnit;
2379 }
2380 }
2381 }
2382
2383 _mesa_update_shader_textures_used(prog);
2384 }
2385
2386 /* Each stage has some uniforms in its Parameters list. The Uniforms
2387 * list for the linked shader program has a pointer to these uniforms
2388 * in each of the stage's Parameters list, so that their values can be
2389 * updated when a uniform is set.
2390 */
2391 static void
2392 link_uniforms_to_shared_uniform_list(struct gl_uniform_list *uniforms,
2393 struct gl_program *prog)
2394 {
2395 unsigned int i;
2396
2397 for (i = 0; i < prog->Parameters->NumParameters; i++) {
2398 const struct gl_program_parameter *p = prog->Parameters->Parameters + i;
2399
2400 if (p->Type == PROGRAM_UNIFORM || p->Type == PROGRAM_SAMPLER) {
2401 struct gl_uniform *uniform =
2402 _mesa_append_uniform(uniforms, p->Name, prog->Target, i);
2403 if (uniform)
2404 uniform->Initialized = p->Initialized;
2405 }
2406 }
2407 }
2408
2409 struct gl_program *
2410 get_mesa_program(GLcontext *ctx, struct gl_shader_program *shader_program,
2411 struct gl_shader *shader)
2412 {
2413 ir_to_mesa_visitor v;
2414 struct prog_instruction *mesa_instructions, *mesa_inst;
2415 ir_instruction **mesa_instruction_annotation;
2416 int i;
2417 struct gl_program *prog;
2418 GLenum target;
2419 const char *target_string;
2420 GLboolean progress;
2421
2422 switch (shader->Type) {
2423 case GL_VERTEX_SHADER:
2424 target = GL_VERTEX_PROGRAM_ARB;
2425 target_string = "vertex";
2426 break;
2427 case GL_FRAGMENT_SHADER:
2428 target = GL_FRAGMENT_PROGRAM_ARB;
2429 target_string = "fragment";
2430 break;
2431 default:
2432 assert(!"should not be reached");
2433 break;
2434 }
2435
2436 validate_ir_tree(shader->ir);
2437
2438 prog = ctx->Driver.NewProgram(ctx, target, shader_program->Name);
2439 if (!prog)
2440 return NULL;
2441 prog->Parameters = _mesa_new_parameter_list();
2442 prog->Varying = _mesa_new_parameter_list();
2443 prog->Attributes = _mesa_new_parameter_list();
2444 v.ctx = ctx;
2445 v.prog = prog;
2446
2447 /* Emit Mesa IR for main(). */
2448 visit_exec_list(shader->ir, &v);
2449 v.ir_to_mesa_emit_op0(NULL, OPCODE_END);
2450
2451 /* Now emit bodies for any functions that were used. */
2452 do {
2453 progress = GL_FALSE;
2454
2455 foreach_iter(exec_list_iterator, iter, v.function_signatures) {
2456 function_entry *entry = (function_entry *)iter.get();
2457
2458 if (!entry->bgn_inst) {
2459 v.current_function = entry;
2460
2461 entry->bgn_inst = v.ir_to_mesa_emit_op0(NULL, OPCODE_BGNSUB);
2462 entry->bgn_inst->function = entry;
2463
2464 visit_exec_list(&entry->sig->body, &v);
2465
2466 ir_to_mesa_instruction *last;
2467 last = (ir_to_mesa_instruction *)v.instructions.get_tail();
2468 if (last->op != OPCODE_RET)
2469 v.ir_to_mesa_emit_op0(NULL, OPCODE_RET);
2470
2471 ir_to_mesa_instruction *end;
2472 end = v.ir_to_mesa_emit_op0(NULL, OPCODE_ENDSUB);
2473 end->function = entry;
2474
2475 progress = GL_TRUE;
2476 }
2477 }
2478 } while (progress);
2479
2480 prog->NumTemporaries = v.next_temp;
2481
2482 int num_instructions = 0;
2483 foreach_iter(exec_list_iterator, iter, v.instructions) {
2484 num_instructions++;
2485 }
2486
2487 mesa_instructions =
2488 (struct prog_instruction *)calloc(num_instructions,
2489 sizeof(*mesa_instructions));
2490 mesa_instruction_annotation = talloc_array(v.mem_ctx, ir_instruction *,
2491 num_instructions);
2492
2493 mesa_inst = mesa_instructions;
2494 i = 0;
2495 foreach_iter(exec_list_iterator, iter, v.instructions) {
2496 ir_to_mesa_instruction *inst = (ir_to_mesa_instruction *)iter.get();
2497
2498 mesa_inst->Opcode = inst->op;
2499 mesa_inst->CondUpdate = inst->cond_update;
2500 mesa_inst->DstReg.File = inst->dst_reg.file;
2501 mesa_inst->DstReg.Index = inst->dst_reg.index;
2502 mesa_inst->DstReg.CondMask = inst->dst_reg.cond_mask;
2503 mesa_inst->DstReg.WriteMask = inst->dst_reg.writemask;
2504 mesa_inst->DstReg.RelAddr = inst->dst_reg.reladdr != NULL;
2505 mesa_inst->SrcReg[0] = mesa_src_reg_from_ir_src_reg(inst->src_reg[0]);
2506 mesa_inst->SrcReg[1] = mesa_src_reg_from_ir_src_reg(inst->src_reg[1]);
2507 mesa_inst->SrcReg[2] = mesa_src_reg_from_ir_src_reg(inst->src_reg[2]);
2508 mesa_inst->TexSrcUnit = inst->sampler;
2509 mesa_inst->TexSrcTarget = inst->tex_target;
2510 mesa_inst->TexShadow = inst->tex_shadow;
2511 mesa_instruction_annotation[i] = inst->ir;
2512
2513 if (ctx->Shader.EmitNoIfs && mesa_inst->Opcode == OPCODE_IF) {
2514 shader_program->InfoLog =
2515 talloc_asprintf_append(shader_program->InfoLog,
2516 "Couldn't flatten if statement\n");
2517 shader_program->LinkStatus = false;
2518 }
2519
2520 switch (mesa_inst->Opcode) {
2521 case OPCODE_BGNSUB:
2522 inst->function->inst = i;
2523 mesa_inst->Comment = strdup(inst->function->sig->function_name());
2524 break;
2525 case OPCODE_ENDSUB:
2526 mesa_inst->Comment = strdup(inst->function->sig->function_name());
2527 break;
2528 case OPCODE_CAL:
2529 mesa_inst->BranchTarget = inst->function->sig_id; /* rewritten later */
2530 break;
2531 case OPCODE_ARL:
2532 prog->NumAddressRegs = 1;
2533 break;
2534 default:
2535 break;
2536 }
2537
2538 mesa_inst++;
2539 i++;
2540 }
2541
2542 set_branchtargets(&v, mesa_instructions, num_instructions);
2543
2544 if (ctx->Shader.Flags & GLSL_DUMP) {
2545 printf("\n");
2546 printf("GLSL IR for linked %s program %d:\n", target_string,
2547 shader_program->Name);
2548 _mesa_print_ir(shader->ir, NULL);
2549 printf("\n");
2550 printf("\n");
2551 printf("Mesa IR for linked %s program %d:\n", target_string,
2552 shader_program->Name);
2553 print_program(mesa_instructions, mesa_instruction_annotation,
2554 num_instructions);
2555 }
2556
2557 prog->Instructions = mesa_instructions;
2558 prog->NumInstructions = num_instructions;
2559
2560 do_set_program_inouts(shader->ir, prog);
2561 count_resources(prog);
2562
2563 _mesa_reference_program(ctx, &shader->Program, prog);
2564
2565 if ((ctx->Shader.Flags & GLSL_NO_OPT) == 0) {
2566 _mesa_optimize_program(ctx, prog);
2567 }
2568
2569 return prog;
2570 }
2571
2572 extern "C" {
2573 GLboolean
2574 _mesa_ir_compile_shader(GLcontext *ctx, struct gl_shader *shader)
2575 {
2576 assert(shader->CompileStatus);
2577 (void) ctx;
2578
2579 return GL_TRUE;
2580 }
2581
2582 GLboolean
2583 _mesa_ir_link_shader(GLcontext *ctx, struct gl_shader_program *prog)
2584 {
2585 assert(prog->LinkStatus);
2586
2587 for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) {
2588 bool progress;
2589 exec_list *ir = prog->_LinkedShaders[i]->ir;
2590
2591 do {
2592 progress = false;
2593
2594 /* Lowering */
2595 do_mat_op_to_vec(ir);
2596 do_mod_to_fract(ir);
2597 do_div_to_mul_rcp(ir);
2598 do_explog_to_explog2(ir);
2599
2600 progress = do_common_optimization(ir, true) || progress;
2601
2602 if (ctx->Shader.EmitNoIfs)
2603 progress = do_if_to_cond_assign(ir) || progress;
2604
2605 progress = do_vec_index_to_cond_assign(ir) || progress;
2606 } while (progress);
2607
2608 validate_ir_tree(ir);
2609 }
2610
2611 for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) {
2612 struct gl_program *linked_prog;
2613 bool ok = true;
2614
2615 linked_prog = get_mesa_program(ctx, prog, prog->_LinkedShaders[i]);
2616
2617 link_uniforms_to_shared_uniform_list(prog->Uniforms, linked_prog);
2618
2619 switch (prog->_LinkedShaders[i]->Type) {
2620 case GL_VERTEX_SHADER:
2621 _mesa_reference_vertprog(ctx, &prog->VertexProgram,
2622 (struct gl_vertex_program *)linked_prog);
2623 ok = ctx->Driver.ProgramStringNotify(ctx, GL_VERTEX_PROGRAM_ARB,
2624 linked_prog);
2625 break;
2626 case GL_FRAGMENT_SHADER:
2627 _mesa_reference_fragprog(ctx, &prog->FragmentProgram,
2628 (struct gl_fragment_program *)linked_prog);
2629 ok = ctx->Driver.ProgramStringNotify(ctx, GL_FRAGMENT_PROGRAM_ARB,
2630 linked_prog);
2631 break;
2632 }
2633 if (!ok) {
2634 return GL_FALSE;
2635 }
2636 _mesa_reference_program(ctx, &linked_prog, NULL);
2637 }
2638
2639 return GL_TRUE;
2640 }
2641
2642 void
2643 _mesa_glsl_compile_shader(GLcontext *ctx, struct gl_shader *shader)
2644 {
2645 struct _mesa_glsl_parse_state *state =
2646 new(shader) _mesa_glsl_parse_state(ctx, shader->Type, shader);
2647
2648 const char *source = shader->Source;
2649 state->error = preprocess(state, &source, &state->info_log,
2650 &ctx->Extensions);
2651
2652 if (ctx->Shader.Flags & GLSL_DUMP) {
2653 printf("GLSL source for shader %d:\n", shader->Name);
2654 printf("%s\n", shader->Source);
2655 }
2656
2657 if (!state->error) {
2658 _mesa_glsl_lexer_ctor(state, source);
2659 _mesa_glsl_parse(state);
2660 _mesa_glsl_lexer_dtor(state);
2661 }
2662
2663 talloc_free(shader->ir);
2664 shader->ir = new(shader) exec_list;
2665 if (!state->error && !state->translation_unit.is_empty())
2666 _mesa_ast_to_hir(shader->ir, state);
2667
2668 if (!state->error && !shader->ir->is_empty()) {
2669 validate_ir_tree(shader->ir);
2670
2671 /* Do some optimization at compile time to reduce shader IR size
2672 * and reduce later work if the same shader is linked multiple times
2673 */
2674 while (do_common_optimization(shader->ir, false))
2675 ;
2676
2677 validate_ir_tree(shader->ir);
2678 }
2679
2680 shader->symbols = state->symbols;
2681
2682 shader->CompileStatus = !state->error;
2683 shader->InfoLog = state->info_log;
2684 shader->Version = state->language_version;
2685 memcpy(shader->builtins_to_link, state->builtins_to_link,
2686 sizeof(shader->builtins_to_link[0]) * state->num_builtins_to_link);
2687 shader->num_builtins_to_link = state->num_builtins_to_link;
2688
2689 if (ctx->Shader.Flags & GLSL_LOG) {
2690 _mesa_write_shader_to_file(shader);
2691 }
2692
2693 if (ctx->Shader.Flags & GLSL_DUMP) {
2694 if (shader->CompileStatus) {
2695 printf("GLSL IR for shader %d:\n", shader->Name);
2696 _mesa_print_ir(shader->ir, NULL);
2697 printf("\n\n");
2698 } else {
2699 printf("GLSL shader %d failed to compile.\n", shader->Name);
2700 }
2701 if (shader->InfoLog && shader->InfoLog[0] != 0) {
2702 printf("GLSL shader %d info log:\n", shader->Name);
2703 printf("%s\n", shader->InfoLog);
2704 }
2705 }
2706
2707 /* Retain any live IR, but trash the rest. */
2708 reparent_ir(shader->ir, shader->ir);
2709
2710 talloc_free(state);
2711
2712 if (shader->CompileStatus) {
2713 if (!ctx->Driver.CompileShader(ctx, shader))
2714 shader->CompileStatus = GL_FALSE;
2715 }
2716 }
2717
2718 void
2719 _mesa_glsl_link_shader(GLcontext *ctx, struct gl_shader_program *prog)
2720 {
2721 unsigned int i;
2722
2723 _mesa_clear_shader_program_data(ctx, prog);
2724
2725 prog->LinkStatus = GL_TRUE;
2726
2727 for (i = 0; i < prog->NumShaders; i++) {
2728 if (!prog->Shaders[i]->CompileStatus) {
2729 prog->InfoLog =
2730 talloc_asprintf_append(prog->InfoLog,
2731 "linking with uncompiled shader");
2732 prog->LinkStatus = GL_FALSE;
2733 }
2734 }
2735
2736 prog->Varying = _mesa_new_parameter_list();
2737 _mesa_reference_vertprog(ctx, &prog->VertexProgram, NULL);
2738 _mesa_reference_fragprog(ctx, &prog->FragmentProgram, NULL);
2739
2740 if (prog->LinkStatus) {
2741 link_shaders(ctx, prog);
2742
2743 /* We don't use the linker's uniforms list, and cook up our own at
2744 * generate time.
2745 */
2746 _mesa_free_uniform_list(prog->Uniforms);
2747 prog->Uniforms = _mesa_new_uniform_list();
2748 }
2749
2750 if (prog->LinkStatus) {
2751 if (!ctx->Driver.LinkShader(ctx, prog)) {
2752 prog->LinkStatus = GL_FALSE;
2753 }
2754 }
2755
2756 if (ctx->Shader.Flags & GLSL_DUMP) {
2757 if (!prog->LinkStatus) {
2758 printf("GLSL shader program %d failed to link\n", prog->Name);
2759 }
2760
2761 if (prog->InfoLog && prog->InfoLog[0] != 0) {
2762 printf("GLSL shader program %d info log:\n", prog->Name);
2763 printf("%s\n", prog->InfoLog);
2764 }
2765 }
2766 }
2767
2768 } /* extern "C" */