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