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