2 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
3 * Copyright (C) 2008 VMware, Inc. All Rights Reserved.
4 * Copyright © 2010 Intel Corporation
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:
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
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.
27 * \file ir_to_mesa.cpp
29 * Translates the IR to ARB_fragment_program text if possible,
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"
45 #include "main/mtypes.h"
46 #include "main/shaderobj.h"
47 #include "main/uniforms.h"
48 #include "program/prog_instruction.h"
49 #include "program/prog_optimize.h"
50 #include "program/prog_print.h"
51 #include "program/program.h"
52 #include "program/prog_uniform.h"
53 #include "program/prog_parameter.h"
57 * This struct is a corresponding struct to Mesa prog_src_register, with
60 typedef struct ir_to_mesa_src_reg
{
61 int file
; /**< PROGRAM_* from Mesa */
62 int index
; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
63 GLuint swizzle
; /**< SWIZZLE_XYZWONEZERO swizzles from Mesa. */
64 int negate
; /**< NEGATE_XYZW mask from mesa */
65 /** Register index should be offset by the integer in this reg. */
66 ir_to_mesa_src_reg
*reladdr
;
69 typedef struct ir_to_mesa_dst_reg
{
70 int file
; /**< PROGRAM_* from Mesa */
71 int index
; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
72 int writemask
; /**< Bitfield of WRITEMASK_[XYZW] */
74 /** Register index should be offset by the integer in this reg. */
75 ir_to_mesa_src_reg
*reladdr
;
78 extern ir_to_mesa_src_reg ir_to_mesa_undef
;
80 class ir_to_mesa_instruction
: public exec_node
{
83 ir_to_mesa_dst_reg dst_reg
;
84 ir_to_mesa_src_reg src_reg
[3];
85 /** Pointer to the ir source this tree came from for debugging */
87 GLboolean cond_update
;
88 int sampler
; /**< sampler index */
89 int tex_target
; /**< One of TEXTURE_*_INDEX */
92 class function_entry
*function
; /* Set on OPCODE_CAL or OPCODE_BGNSUB */
95 class variable_storage
: public exec_node
{
97 variable_storage(ir_variable
*var
, int file
, int index
)
98 : file(file
), index(index
), var(var
)
105 ir_variable
*var
; /* variable that maps to this, if any */
108 class function_entry
: public exec_node
{
110 ir_function_signature
*sig
;
113 * identifier of this function signature used by the program.
115 * At the point that Mesa instructions for function calls are
116 * generated, we don't know the address of the first instruction of
117 * the function body. So we make the BranchTarget that is called a
118 * small integer and rewrite them during set_branchtargets().
123 * Pointer to first instruction of the function body.
125 * Set during function body emits after main() is processed.
127 ir_to_mesa_instruction
*bgn_inst
;
130 * Index of the first instruction of the function body in actual
133 * Set after convertion from ir_to_mesa_instruction to prog_instruction.
137 /** Storage for the return value. */
138 ir_to_mesa_src_reg return_reg
;
141 class ir_to_mesa_visitor
: public ir_visitor
{
143 ir_to_mesa_visitor();
145 function_entry
*current_function
;
148 struct gl_program
*prog
;
152 variable_storage
*find_variable_storage(ir_variable
*var
);
154 function_entry
*get_function_signature(ir_function_signature
*sig
);
156 ir_to_mesa_src_reg
get_temp(const glsl_type
*type
);
157 void reladdr_to_temp(ir_instruction
*ir
,
158 ir_to_mesa_src_reg
*reg
, int *num_reladdr
);
160 struct ir_to_mesa_src_reg
src_reg_for_float(float val
);
163 * \name Visit methods
165 * As typical for the visitor pattern, there must be one \c visit method for
166 * each concrete subclass of \c ir_instruction. Virtual base classes within
167 * the hierarchy should not have \c visit methods.
170 virtual void visit(ir_variable
*);
171 virtual void visit(ir_loop
*);
172 virtual void visit(ir_loop_jump
*);
173 virtual void visit(ir_function_signature
*);
174 virtual void visit(ir_function
*);
175 virtual void visit(ir_expression
*);
176 virtual void visit(ir_swizzle
*);
177 virtual void visit(ir_dereference_variable
*);
178 virtual void visit(ir_dereference_array
*);
179 virtual void visit(ir_dereference_record
*);
180 virtual void visit(ir_assignment
*);
181 virtual void visit(ir_constant
*);
182 virtual void visit(ir_call
*);
183 virtual void visit(ir_return
*);
184 virtual void visit(ir_discard
*);
185 virtual void visit(ir_texture
*);
186 virtual void visit(ir_if
*);
189 struct ir_to_mesa_src_reg result
;
191 /** List of variable_storage */
194 /** List of function_entry */
195 exec_list function_signatures
;
196 int next_signature_id
;
198 /** List of ir_to_mesa_instruction */
199 exec_list instructions
;
201 ir_to_mesa_instruction
*ir_to_mesa_emit_op0(ir_instruction
*ir
,
202 enum prog_opcode op
);
204 ir_to_mesa_instruction
*ir_to_mesa_emit_op1(ir_instruction
*ir
,
206 ir_to_mesa_dst_reg dst
,
207 ir_to_mesa_src_reg src0
);
209 ir_to_mesa_instruction
*ir_to_mesa_emit_op2(ir_instruction
*ir
,
211 ir_to_mesa_dst_reg dst
,
212 ir_to_mesa_src_reg src0
,
213 ir_to_mesa_src_reg src1
);
215 ir_to_mesa_instruction
*ir_to_mesa_emit_op3(ir_instruction
*ir
,
217 ir_to_mesa_dst_reg dst
,
218 ir_to_mesa_src_reg src0
,
219 ir_to_mesa_src_reg src1
,
220 ir_to_mesa_src_reg src2
);
222 void ir_to_mesa_emit_scalar_op1(ir_instruction
*ir
,
224 ir_to_mesa_dst_reg dst
,
225 ir_to_mesa_src_reg src0
);
227 void ir_to_mesa_emit_scalar_op2(ir_instruction
*ir
,
229 ir_to_mesa_dst_reg dst
,
230 ir_to_mesa_src_reg src0
,
231 ir_to_mesa_src_reg src1
);
233 GLboolean
try_emit_mad(ir_expression
*ir
,
237 int sampler_map_size
;
239 void map_sampler(int location
, int sampler
);
240 int get_sampler_number(int location
);
245 ir_to_mesa_src_reg ir_to_mesa_undef
= {
246 PROGRAM_UNDEFINED
, 0, SWIZZLE_NOOP
, NEGATE_NONE
, NULL
,
249 ir_to_mesa_dst_reg ir_to_mesa_undef_dst
= {
250 PROGRAM_UNDEFINED
, 0, SWIZZLE_NOOP
, COND_TR
, NULL
,
253 ir_to_mesa_dst_reg ir_to_mesa_address_reg
= {
254 PROGRAM_ADDRESS
, 0, WRITEMASK_X
, COND_TR
, NULL
257 static int swizzle_for_size(int size
)
259 int size_swizzles
[4] = {
260 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
),
261 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
),
262 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
),
263 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
266 return size_swizzles
[size
- 1];
269 ir_to_mesa_instruction
*
270 ir_to_mesa_visitor::ir_to_mesa_emit_op3(ir_instruction
*ir
,
272 ir_to_mesa_dst_reg dst
,
273 ir_to_mesa_src_reg src0
,
274 ir_to_mesa_src_reg src1
,
275 ir_to_mesa_src_reg src2
)
277 ir_to_mesa_instruction
*inst
= new(mem_ctx
) ir_to_mesa_instruction();
280 /* If we have to do relative addressing, we want to load the ARL
281 * reg directly for one of the regs, and preload the other reladdr
282 * sources into temps.
284 num_reladdr
+= dst
.reladdr
!= NULL
;
285 num_reladdr
+= src0
.reladdr
!= NULL
;
286 num_reladdr
+= src1
.reladdr
!= NULL
;
287 num_reladdr
+= src2
.reladdr
!= NULL
;
289 reladdr_to_temp(ir
, &src2
, &num_reladdr
);
290 reladdr_to_temp(ir
, &src1
, &num_reladdr
);
291 reladdr_to_temp(ir
, &src0
, &num_reladdr
);
294 ir_to_mesa_emit_op1(ir
, OPCODE_ARL
, ir_to_mesa_address_reg
,
299 assert(num_reladdr
== 0);
303 inst
->src_reg
[0] = src0
;
304 inst
->src_reg
[1] = src1
;
305 inst
->src_reg
[2] = src2
;
308 inst
->function
= NULL
;
310 this->instructions
.push_tail(inst
);
316 ir_to_mesa_instruction
*
317 ir_to_mesa_visitor::ir_to_mesa_emit_op2(ir_instruction
*ir
,
319 ir_to_mesa_dst_reg dst
,
320 ir_to_mesa_src_reg src0
,
321 ir_to_mesa_src_reg src1
)
323 return ir_to_mesa_emit_op3(ir
, op
, dst
, src0
, src1
, ir_to_mesa_undef
);
326 ir_to_mesa_instruction
*
327 ir_to_mesa_visitor::ir_to_mesa_emit_op1(ir_instruction
*ir
,
329 ir_to_mesa_dst_reg dst
,
330 ir_to_mesa_src_reg src0
)
332 return ir_to_mesa_emit_op3(ir
, op
, dst
,
333 src0
, ir_to_mesa_undef
, ir_to_mesa_undef
);
336 ir_to_mesa_instruction
*
337 ir_to_mesa_visitor::ir_to_mesa_emit_op0(ir_instruction
*ir
,
340 return ir_to_mesa_emit_op3(ir
, op
, ir_to_mesa_undef_dst
,
347 ir_to_mesa_visitor::map_sampler(int location
, int sampler
)
349 if (this->sampler_map_size
<= location
) {
350 this->sampler_map
= talloc_realloc(this->mem_ctx
, this->sampler_map
,
352 this->sampler_map_size
= location
+ 1;
355 this->sampler_map
[location
] = sampler
;
359 ir_to_mesa_visitor::get_sampler_number(int location
)
361 assert(location
< this->sampler_map_size
);
362 return this->sampler_map
[location
];
365 inline ir_to_mesa_dst_reg
366 ir_to_mesa_dst_reg_from_src(ir_to_mesa_src_reg reg
)
368 ir_to_mesa_dst_reg dst_reg
;
370 dst_reg
.file
= reg
.file
;
371 dst_reg
.index
= reg
.index
;
372 dst_reg
.writemask
= WRITEMASK_XYZW
;
373 dst_reg
.cond_mask
= COND_TR
;
374 dst_reg
.reladdr
= reg
.reladdr
;
379 inline ir_to_mesa_src_reg
380 ir_to_mesa_src_reg_from_dst(ir_to_mesa_dst_reg reg
)
382 ir_to_mesa_src_reg src_reg
;
384 src_reg
.file
= reg
.file
;
385 src_reg
.index
= reg
.index
;
386 src_reg
.swizzle
= SWIZZLE_XYZW
;
388 src_reg
.reladdr
= reg
.reladdr
;
394 * Emits Mesa scalar opcodes to produce unique answers across channels.
396 * Some Mesa opcodes are scalar-only, like ARB_fp/vp. The src X
397 * channel determines the result across all channels. So to do a vec4
398 * of this operation, we want to emit a scalar per source channel used
399 * to produce dest channels.
402 ir_to_mesa_visitor::ir_to_mesa_emit_scalar_op2(ir_instruction
*ir
,
404 ir_to_mesa_dst_reg dst
,
405 ir_to_mesa_src_reg orig_src0
,
406 ir_to_mesa_src_reg orig_src1
)
409 int done_mask
= ~dst
.writemask
;
411 /* Mesa RCP is a scalar operation splatting results to all channels,
412 * like ARB_fp/vp. So emit as many RCPs as necessary to cover our
415 for (i
= 0; i
< 4; i
++) {
416 GLuint this_mask
= (1 << i
);
417 ir_to_mesa_instruction
*inst
;
418 ir_to_mesa_src_reg src0
= orig_src0
;
419 ir_to_mesa_src_reg src1
= orig_src1
;
421 if (done_mask
& this_mask
)
424 GLuint src0_swiz
= GET_SWZ(src0
.swizzle
, i
);
425 GLuint src1_swiz
= GET_SWZ(src1
.swizzle
, i
);
426 for (j
= i
+ 1; j
< 4; j
++) {
427 if (!(done_mask
& (1 << j
)) &&
428 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
&&
429 GET_SWZ(src1
.swizzle
, j
) == src1_swiz
) {
430 this_mask
|= (1 << j
);
433 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
434 src0_swiz
, src0_swiz
);
435 src1
.swizzle
= MAKE_SWIZZLE4(src1_swiz
, src1_swiz
,
436 src1_swiz
, src1_swiz
);
438 inst
= ir_to_mesa_emit_op2(ir
, op
,
442 inst
->dst_reg
.writemask
= this_mask
;
443 done_mask
|= this_mask
;
448 ir_to_mesa_visitor::ir_to_mesa_emit_scalar_op1(ir_instruction
*ir
,
450 ir_to_mesa_dst_reg dst
,
451 ir_to_mesa_src_reg src0
)
453 ir_to_mesa_src_reg undef
= ir_to_mesa_undef
;
455 undef
.swizzle
= SWIZZLE_XXXX
;
457 ir_to_mesa_emit_scalar_op2(ir
, op
, dst
, src0
, undef
);
460 struct ir_to_mesa_src_reg
461 ir_to_mesa_visitor::src_reg_for_float(float val
)
463 ir_to_mesa_src_reg src_reg
;
465 src_reg
.file
= PROGRAM_CONSTANT
;
466 src_reg
.index
= _mesa_add_unnamed_constant(this->prog
->Parameters
,
467 &val
, 1, &src_reg
.swizzle
);
468 src_reg
.reladdr
= NULL
;
475 type_size(const struct glsl_type
*type
)
480 switch (type
->base_type
) {
483 case GLSL_TYPE_FLOAT
:
485 if (type
->is_matrix()) {
486 return type
->matrix_columns
;
488 /* Regardless of size of vector, it gets a vec4. This is bad
489 * packing for things like floats, but otherwise arrays become a
490 * mess. Hopefully a later pass over the code can pack scalars
491 * down if appropriate.
495 case GLSL_TYPE_ARRAY
:
496 return type_size(type
->fields
.array
) * type
->length
;
497 case GLSL_TYPE_STRUCT
:
499 for (i
= 0; i
< type
->length
; i
++) {
500 size
+= type_size(type
->fields
.structure
[i
].type
);
509 * In the initial pass of codegen, we assign temporary numbers to
510 * intermediate results. (not SSA -- variable assignments will reuse
511 * storage). Actual register allocation for the Mesa VM occurs in a
512 * pass over the Mesa IR later.
515 ir_to_mesa_visitor::get_temp(const glsl_type
*type
)
517 ir_to_mesa_src_reg src_reg
;
521 src_reg
.file
= PROGRAM_TEMPORARY
;
522 src_reg
.index
= next_temp
;
523 src_reg
.reladdr
= NULL
;
524 next_temp
+= type_size(type
);
526 if (type
->is_array() || type
->is_record()) {
527 src_reg
.swizzle
= SWIZZLE_NOOP
;
529 for (i
= 0; i
< type
->vector_elements
; i
++)
532 swizzle
[i
] = type
->vector_elements
- 1;
533 src_reg
.swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1],
534 swizzle
[2], swizzle
[3]);
542 ir_to_mesa_visitor::find_variable_storage(ir_variable
*var
)
545 variable_storage
*entry
;
547 foreach_iter(exec_list_iterator
, iter
, this->variables
) {
548 entry
= (variable_storage
*)iter
.get();
550 if (entry
->var
== var
)
558 ir_to_mesa_visitor::visit(ir_variable
*ir
)
560 if (strcmp(ir
->name
, "gl_FragCoord") == 0) {
561 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
563 fp
->OriginUpperLeft
= ir
->origin_upper_left
;
564 fp
->PixelCenterInteger
= ir
->pixel_center_integer
;
569 ir_to_mesa_visitor::visit(ir_loop
*ir
)
573 assert(!ir
->increment
);
574 assert(!ir
->counter
);
576 ir_to_mesa_emit_op0(NULL
, OPCODE_BGNLOOP
);
577 visit_exec_list(&ir
->body_instructions
, this);
578 ir_to_mesa_emit_op0(NULL
, OPCODE_ENDLOOP
);
582 ir_to_mesa_visitor::visit(ir_loop_jump
*ir
)
585 case ir_loop_jump::jump_break
:
586 ir_to_mesa_emit_op0(NULL
, OPCODE_BRK
);
588 case ir_loop_jump::jump_continue
:
589 ir_to_mesa_emit_op0(NULL
, OPCODE_CONT
);
596 ir_to_mesa_visitor::visit(ir_function_signature
*ir
)
603 ir_to_mesa_visitor::visit(ir_function
*ir
)
605 /* Ignore function bodies other than main() -- we shouldn't see calls to
606 * them since they should all be inlined before we get to ir_to_mesa.
608 if (strcmp(ir
->name
, "main") == 0) {
609 const ir_function_signature
*sig
;
612 sig
= ir
->matching_signature(&empty
);
616 foreach_iter(exec_list_iterator
, iter
, sig
->body
) {
617 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
625 ir_to_mesa_visitor::try_emit_mad(ir_expression
*ir
, int mul_operand
)
627 int nonmul_operand
= 1 - mul_operand
;
628 ir_to_mesa_src_reg a
, b
, c
;
630 ir_expression
*expr
= ir
->operands
[mul_operand
]->as_expression();
631 if (!expr
|| expr
->operation
!= ir_binop_mul
)
634 expr
->operands
[0]->accept(this);
636 expr
->operands
[1]->accept(this);
638 ir
->operands
[nonmul_operand
]->accept(this);
641 this->result
= get_temp(ir
->type
);
642 ir_to_mesa_emit_op3(ir
, OPCODE_MAD
,
643 ir_to_mesa_dst_reg_from_src(this->result
), a
, b
, c
);
649 ir_to_mesa_visitor::reladdr_to_temp(ir_instruction
*ir
,
650 ir_to_mesa_src_reg
*reg
, int *num_reladdr
)
655 ir_to_mesa_emit_op1(ir
, OPCODE_ARL
, ir_to_mesa_address_reg
, *reg
->reladdr
);
657 if (*num_reladdr
!= 1) {
658 ir_to_mesa_src_reg temp
= get_temp(glsl_type::vec4_type
);
660 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
,
661 ir_to_mesa_dst_reg_from_src(temp
), *reg
);
669 ir_to_mesa_visitor::visit(ir_expression
*ir
)
671 unsigned int operand
;
672 struct ir_to_mesa_src_reg op
[2];
673 struct ir_to_mesa_src_reg result_src
;
674 struct ir_to_mesa_dst_reg result_dst
;
675 const glsl_type
*vec4_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
, 4, 1);
676 const glsl_type
*vec3_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
, 3, 1);
677 const glsl_type
*vec2_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
, 2, 1);
679 /* Quick peephole: Emit OPCODE_MAD(a, b, c) instead of ADD(MUL(a, b), c)
681 if (ir
->operation
== ir_binop_add
) {
682 if (try_emit_mad(ir
, 1))
684 if (try_emit_mad(ir
, 0))
688 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
689 this->result
.file
= PROGRAM_UNDEFINED
;
690 ir
->operands
[operand
]->accept(this);
691 if (this->result
.file
== PROGRAM_UNDEFINED
) {
693 printf("Failed to get tree for expression operand:\n");
694 ir
->operands
[operand
]->accept(&v
);
697 op
[operand
] = this->result
;
699 /* Matrix expression operands should have been broken down to vector
700 * operations already.
702 assert(!ir
->operands
[operand
]->type
->is_matrix());
705 this->result
.file
= PROGRAM_UNDEFINED
;
707 /* Storage for our result. Ideally for an assignment we'd be using
708 * the actual storage for the result here, instead.
710 result_src
= get_temp(ir
->type
);
711 /* convenience for the emit functions below. */
712 result_dst
= ir_to_mesa_dst_reg_from_src(result_src
);
713 /* Limit writes to the channels that will be used by result_src later.
714 * This does limit this temp's use as a temporary for multi-instruction
717 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
719 switch (ir
->operation
) {
720 case ir_unop_logic_not
:
721 ir_to_mesa_emit_op2(ir
, OPCODE_SEQ
, result_dst
,
722 op
[0], src_reg_for_float(0.0));
725 op
[0].negate
= ~op
[0].negate
;
729 ir_to_mesa_emit_op1(ir
, OPCODE_ABS
, result_dst
, op
[0]);
732 ir_to_mesa_emit_op1(ir
, OPCODE_SSG
, result_dst
, op
[0]);
735 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_RCP
, result_dst
, op
[0]);
739 ir_to_mesa_emit_scalar_op2(ir
, OPCODE_POW
, result_dst
,
740 src_reg_for_float(M_E
), op
[0]);
743 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_EX2
, result_dst
, op
[0]);
746 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_LOG
, result_dst
, op
[0]);
749 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_LG2
, result_dst
, op
[0]);
752 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_SIN
, result_dst
, op
[0]);
755 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_COS
, result_dst
, op
[0]);
759 ir_to_mesa_emit_op1(ir
, OPCODE_DDX
, result_dst
, op
[0]);
762 ir_to_mesa_emit_op1(ir
, OPCODE_DDY
, result_dst
, op
[0]);
766 ir_to_mesa_emit_op2(ir
, OPCODE_ADD
, result_dst
, op
[0], op
[1]);
769 ir_to_mesa_emit_op2(ir
, OPCODE_SUB
, result_dst
, op
[0], op
[1]);
773 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
, result_dst
, op
[0], op
[1]);
776 assert(!"not reached: should be handled by ir_div_to_mul_rcp");
778 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
782 ir_to_mesa_emit_op2(ir
, OPCODE_SLT
, result_dst
, op
[0], op
[1]);
784 case ir_binop_greater
:
785 ir_to_mesa_emit_op2(ir
, OPCODE_SGT
, result_dst
, op
[0], op
[1]);
787 case ir_binop_lequal
:
788 ir_to_mesa_emit_op2(ir
, OPCODE_SLE
, result_dst
, op
[0], op
[1]);
790 case ir_binop_gequal
:
791 ir_to_mesa_emit_op2(ir
, OPCODE_SGE
, result_dst
, op
[0], op
[1]);
794 ir_to_mesa_emit_op2(ir
, OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
796 case ir_binop_logic_xor
:
797 case ir_binop_nequal
:
798 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
, result_dst
, op
[0], op
[1]);
801 case ir_binop_logic_or
:
802 /* This could be a saturated add and skip the SNE. */
803 ir_to_mesa_emit_op2(ir
, OPCODE_ADD
,
807 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
,
809 result_src
, src_reg_for_float(0.0));
812 case ir_binop_logic_and
:
813 /* the bool args are stored as float 0.0 or 1.0, so "mul" gives us "and". */
814 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
,
820 if (ir
->operands
[0]->type
== vec4_type
) {
821 assert(ir
->operands
[1]->type
== vec4_type
);
822 ir_to_mesa_emit_op2(ir
, OPCODE_DP4
,
825 } else if (ir
->operands
[0]->type
== vec3_type
) {
826 assert(ir
->operands
[1]->type
== vec3_type
);
827 ir_to_mesa_emit_op2(ir
, OPCODE_DP3
,
830 } else if (ir
->operands
[0]->type
== vec2_type
) {
831 assert(ir
->operands
[1]->type
== vec2_type
);
832 ir_to_mesa_emit_op2(ir
, OPCODE_DP2
,
839 ir_to_mesa_emit_op2(ir
, OPCODE_XPD
, result_dst
, op
[0], op
[1]);
843 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_RSQ
, result_dst
, op
[0]);
844 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_RCP
, result_dst
, result_src
);
845 /* For incoming channels < 0, set the result to 0. */
846 ir_to_mesa_emit_op3(ir
, OPCODE_CMP
, result_dst
,
847 op
[0], src_reg_for_float(0.0), result_src
);
850 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_RSQ
, result_dst
, op
[0]);
855 /* Mesa IR lacks types, ints are stored as truncated floats. */
859 ir_to_mesa_emit_op1(ir
, OPCODE_TRUNC
, result_dst
, op
[0]);
863 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
, result_dst
,
864 result_src
, src_reg_for_float(0.0));
867 ir_to_mesa_emit_op1(ir
, OPCODE_TRUNC
, result_dst
, op
[0]);
870 op
[0].negate
= ~op
[0].negate
;
871 ir_to_mesa_emit_op1(ir
, OPCODE_FLR
, result_dst
, op
[0]);
872 result_src
.negate
= ~result_src
.negate
;
875 ir_to_mesa_emit_op1(ir
, OPCODE_FLR
, result_dst
, op
[0]);
878 ir_to_mesa_emit_op1(ir
, OPCODE_FRC
, result_dst
, op
[0]);
882 ir_to_mesa_emit_op2(ir
, OPCODE_MIN
, result_dst
, op
[0], op
[1]);
885 ir_to_mesa_emit_op2(ir
, OPCODE_MAX
, result_dst
, op
[0], op
[1]);
888 ir_to_mesa_emit_scalar_op2(ir
, OPCODE_POW
, result_dst
, op
[0], op
[1]);
891 case ir_unop_bit_not
:
893 case ir_binop_lshift
:
894 case ir_binop_rshift
:
895 case ir_binop_bit_and
:
896 case ir_binop_bit_xor
:
897 case ir_binop_bit_or
:
898 assert(!"GLSL 1.30 features unsupported");
902 this->result
= result_src
;
907 ir_to_mesa_visitor::visit(ir_swizzle
*ir
)
909 ir_to_mesa_src_reg src_reg
;
913 /* Note that this is only swizzles in expressions, not those on the left
914 * hand side of an assignment, which do write masking. See ir_assignment
918 ir
->val
->accept(this);
919 src_reg
= this->result
;
920 assert(src_reg
.file
!= PROGRAM_UNDEFINED
);
922 for (i
= 0; i
< 4; i
++) {
923 if (i
< ir
->type
->vector_elements
) {
926 swizzle
[i
] = GET_SWZ(src_reg
.swizzle
, ir
->mask
.x
);
929 swizzle
[i
] = GET_SWZ(src_reg
.swizzle
, ir
->mask
.y
);
932 swizzle
[i
] = GET_SWZ(src_reg
.swizzle
, ir
->mask
.z
);
935 swizzle
[i
] = GET_SWZ(src_reg
.swizzle
, ir
->mask
.w
);
939 /* If the type is smaller than a vec4, replicate the last
942 swizzle
[i
] = swizzle
[ir
->type
->vector_elements
- 1];
946 src_reg
.swizzle
= MAKE_SWIZZLE4(swizzle
[0],
951 this->result
= src_reg
;
954 static const struct {
957 int tokens
[STATE_LENGTH
];
961 {"gl_DepthRange", "near",
962 {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_XXXX
},
963 {"gl_DepthRange", "far",
964 {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_YYYY
},
965 {"gl_DepthRange", "diff",
966 {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_ZZZZ
},
968 {"gl_ClipPlane", NULL
,
969 {STATE_CLIPPLANE
, 0, 0}, SWIZZLE_XYZW
, true}
972 {STATE_POINT_SIZE
}, SWIZZLE_XXXX
},
973 {"gl_Point", "sizeMin",
974 {STATE_POINT_SIZE
}, SWIZZLE_YYYY
},
975 {"gl_Point", "sizeMax",
976 {STATE_POINT_SIZE
}, SWIZZLE_ZZZZ
},
977 {"gl_Point", "fadeThresholdSize",
978 {STATE_POINT_SIZE
}, SWIZZLE_WWWW
},
979 {"gl_Point", "distanceConstantAttenuation",
980 {STATE_POINT_ATTENUATION
}, SWIZZLE_XXXX
},
981 {"gl_Point", "distanceLinearAttenuation",
982 {STATE_POINT_ATTENUATION
}, SWIZZLE_YYYY
},
983 {"gl_Point", "distanceQuadraticAttenuation",
984 {STATE_POINT_ATTENUATION
}, SWIZZLE_ZZZZ
},
986 {"gl_FrontMaterial", "emission",
987 {STATE_MATERIAL
, 0, STATE_EMISSION
}, SWIZZLE_XYZW
},
988 {"gl_FrontMaterial", "ambient",
989 {STATE_MATERIAL
, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
990 {"gl_FrontMaterial", "diffuse",
991 {STATE_MATERIAL
, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
992 {"gl_FrontMaterial", "specular",
993 {STATE_MATERIAL
, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
994 {"gl_FrontMaterial", "shininess",
995 {STATE_MATERIAL
, 0, STATE_SHININESS
}, SWIZZLE_XXXX
},
997 {"gl_BackMaterial", "emission",
998 {STATE_MATERIAL
, 1, STATE_EMISSION
}, SWIZZLE_XYZW
},
999 {"gl_BackMaterial", "ambient",
1000 {STATE_MATERIAL
, 1, STATE_AMBIENT
}, SWIZZLE_XYZW
},
1001 {"gl_BackMaterial", "diffuse",
1002 {STATE_MATERIAL
, 1, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
1003 {"gl_BackMaterial", "specular",
1004 {STATE_MATERIAL
, 1, STATE_SPECULAR
}, SWIZZLE_XYZW
},
1005 {"gl_BackMaterial", "shininess",
1006 {STATE_MATERIAL
, 1, STATE_SHININESS
}, SWIZZLE_XXXX
},
1008 {"gl_LightSource", "ambient",
1009 {STATE_LIGHT
, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
, true},
1010 {"gl_LightSource", "diffuse",
1011 {STATE_LIGHT
, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
, true},
1012 {"gl_LightSource", "specular",
1013 {STATE_LIGHT
, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
, true},
1014 {"gl_LightSource", "position",
1015 {STATE_LIGHT
, 0, STATE_POSITION
}, SWIZZLE_XYZW
, true},
1016 {"gl_LightSource", "halfVector",
1017 {STATE_LIGHT
, 0, STATE_HALF_VECTOR
}, SWIZZLE_XYZW
, true},
1018 {"gl_LightSource", "spotDirection",
1019 {STATE_LIGHT
, 0, STATE_SPOT_DIRECTION
}, SWIZZLE_XYZW
, true},
1020 {"gl_LightSource", "spotCosCutoff",
1021 {STATE_LIGHT
, 0, STATE_SPOT_DIRECTION
}, SWIZZLE_WWWW
, true},
1022 {"gl_LightSource", "spotCutoff",
1023 {STATE_LIGHT
, 0, STATE_SPOT_CUTOFF
}, SWIZZLE_XXXX
, true},
1024 {"gl_LightSource", "spotExponent",
1025 {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_WWWW
, true},
1026 {"gl_LightSource", "constantAttenuation",
1027 {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_XXXX
, true},
1028 {"gl_LightSource", "linearAttenuation",
1029 {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_YYYY
, true},
1030 {"gl_LightSource", "quadraticAttenuation",
1031 {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_ZZZZ
, true},
1033 {"gl_LightModel", NULL
,
1034 {STATE_LIGHTMODEL_AMBIENT
, 0}, SWIZZLE_XYZW
},
1036 {"gl_FrontLightModelProduct", NULL
,
1037 {STATE_LIGHTMODEL_SCENECOLOR
, 0}, SWIZZLE_XYZW
},
1038 {"gl_BackLightModelProduct", NULL
,
1039 {STATE_LIGHTMODEL_SCENECOLOR
, 1}, SWIZZLE_XYZW
},
1041 {"gl_FrontLightProduct", "ambient",
1042 {STATE_LIGHTPROD
, 0, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
, true},
1043 {"gl_FrontLightProduct", "diffuse",
1044 {STATE_LIGHTPROD
, 0, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
, true},
1045 {"gl_FrontLightProduct", "specular",
1046 {STATE_LIGHTPROD
, 0, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
, true},
1048 {"gl_BackLightProduct", "ambient",
1049 {STATE_LIGHTPROD
, 0, 1, STATE_AMBIENT
}, SWIZZLE_XYZW
, true},
1050 {"gl_BackLightProduct", "diffuse",
1051 {STATE_LIGHTPROD
, 0, 1, STATE_DIFFUSE
}, SWIZZLE_XYZW
, true},
1052 {"gl_BackLightProduct", "specular",
1053 {STATE_LIGHTPROD
, 0, 1, STATE_SPECULAR
}, SWIZZLE_XYZW
, true},
1055 {"gl_TextureEnvColor", "ambient",
1056 {STATE_TEXENV_COLOR
, 0}, SWIZZLE_XYZW
, true},
1058 {"gl_EyePlaneS", NULL
,
1059 {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_S
}, SWIZZLE_XYZW
, true},
1060 {"gl_EyePlaneT", NULL
,
1061 {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_T
}, SWIZZLE_XYZW
, true},
1062 {"gl_EyePlaneR", NULL
,
1063 {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_R
}, SWIZZLE_XYZW
, true},
1064 {"gl_EyePlaneQ", NULL
,
1065 {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_Q
}, SWIZZLE_XYZW
, true},
1067 {"gl_ObjectPlaneS", NULL
,
1068 {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_S
}, SWIZZLE_XYZW
, true},
1069 {"gl_ObjectPlaneT", NULL
,
1070 {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_T
}, SWIZZLE_XYZW
, true},
1071 {"gl_ObjectPlaneR", NULL
,
1072 {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_R
}, SWIZZLE_XYZW
, true},
1073 {"gl_ObjectPlaneQ", NULL
,
1074 {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_Q
}, SWIZZLE_XYZW
, true},
1077 {STATE_FOG_COLOR
}, SWIZZLE_XYZW
},
1078 {"gl_Fog", "density",
1079 {STATE_FOG_PARAMS
}, SWIZZLE_XXXX
},
1081 {STATE_FOG_PARAMS
}, SWIZZLE_YYYY
},
1083 {STATE_FOG_PARAMS
}, SWIZZLE_ZZZZ
},
1085 {STATE_FOG_PARAMS
}, SWIZZLE_WWWW
},
1088 static ir_to_mesa_src_reg
1089 get_builtin_uniform_reg(struct gl_program
*prog
,
1090 const char *name
, int array_index
, const char *field
)
1093 ir_to_mesa_src_reg src_reg
;
1094 int tokens
[STATE_LENGTH
];
1096 for (i
= 0; i
< Elements(statevars
); i
++) {
1097 if (strcmp(statevars
[i
].name
, name
) != 0)
1099 if (!field
&& statevars
[i
].field
) {
1100 assert(!"FINISHME: whole-structure state var dereference");
1102 if (field
&& strcmp(statevars
[i
].field
, field
) != 0)
1107 if (i
== Elements(statevars
)) {
1108 printf("builtin uniform %s%s%s not found\n",
1111 field
? field
: "");
1115 memcpy(&tokens
, statevars
[i
].tokens
, sizeof(tokens
));
1116 if (statevars
[i
].array_indexed
)
1117 tokens
[1] = array_index
;
1119 src_reg
.file
= PROGRAM_STATE_VAR
;
1120 src_reg
.index
= _mesa_add_state_reference(prog
->Parameters
,
1121 (gl_state_index
*)tokens
);
1122 src_reg
.swizzle
= statevars
[i
].swizzle
;
1124 src_reg
.reladdr
= false;
1130 add_matrix_ref(struct gl_program
*prog
, int *tokens
)
1135 /* Add a ref for each column. It looks like the reason we do
1136 * it this way is that _mesa_add_state_reference doesn't work
1137 * for things that aren't vec4s, so the tokens[2]/tokens[3]
1138 * range has to be equal.
1140 for (i
= 0; i
< 4; i
++) {
1143 int pos
= _mesa_add_state_reference(prog
->Parameters
,
1144 (gl_state_index
*)tokens
);
1148 assert(base_pos
+ i
== pos
);
1154 static variable_storage
*
1155 get_builtin_matrix_ref(void *mem_ctx
, struct gl_program
*prog
, ir_variable
*var
,
1156 ir_rvalue
*array_index
)
1159 * NOTE: The ARB_vertex_program extension specified that matrices get
1160 * loaded in registers in row-major order. With GLSL, we want column-
1161 * major order. So, we need to transpose all matrices here...
1163 static const struct {
1168 { "gl_ModelViewMatrix", STATE_MODELVIEW_MATRIX
, STATE_MATRIX_TRANSPOSE
},
1169 { "gl_ModelViewMatrixInverse", STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVTRANS
},
1170 { "gl_ModelViewMatrixTranspose", STATE_MODELVIEW_MATRIX
, 0 },
1171 { "gl_ModelViewMatrixInverseTranspose", STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVERSE
},
1173 { "gl_ProjectionMatrix", STATE_PROJECTION_MATRIX
, STATE_MATRIX_TRANSPOSE
},
1174 { "gl_ProjectionMatrixInverse", STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVTRANS
},
1175 { "gl_ProjectionMatrixTranspose", STATE_PROJECTION_MATRIX
, 0 },
1176 { "gl_ProjectionMatrixInverseTranspose", STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVERSE
},
1178 { "gl_ModelViewProjectionMatrix", STATE_MVP_MATRIX
, STATE_MATRIX_TRANSPOSE
},
1179 { "gl_ModelViewProjectionMatrixInverse", STATE_MVP_MATRIX
, STATE_MATRIX_INVTRANS
},
1180 { "gl_ModelViewProjectionMatrixTranspose", STATE_MVP_MATRIX
, 0 },
1181 { "gl_ModelViewProjectionMatrixInverseTranspose", STATE_MVP_MATRIX
, STATE_MATRIX_INVERSE
},
1183 { "gl_TextureMatrix", STATE_TEXTURE_MATRIX
, STATE_MATRIX_TRANSPOSE
},
1184 { "gl_TextureMatrixInverse", STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVTRANS
},
1185 { "gl_TextureMatrixTranspose", STATE_TEXTURE_MATRIX
, 0 },
1186 { "gl_TextureMatrixInverseTranspose", STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVERSE
},
1188 { "gl_NormalMatrix", STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVERSE
},
1192 variable_storage
*entry
;
1194 /* C++ gets angry when we try to use an int as a gl_state_index, so we use
1195 * ints for gl_state_index. Make sure they're compatible.
1197 assert(sizeof(gl_state_index
) == sizeof(int));
1199 for (i
= 0; i
< Elements(matrices
); i
++) {
1200 if (strcmp(var
->name
, matrices
[i
].name
) == 0) {
1201 int tokens
[STATE_LENGTH
];
1204 tokens
[0] = matrices
[i
].matrix
;
1205 tokens
[4] = matrices
[i
].modifier
;
1206 if (matrices
[i
].matrix
== STATE_TEXTURE_MATRIX
) {
1207 ir_constant
*index
= array_index
->constant_expression_value();
1209 tokens
[1] = index
->value
.i
[0];
1210 base_pos
= add_matrix_ref(prog
, tokens
);
1212 for (i
= 0; i
< var
->type
->length
; i
++) {
1214 int pos
= add_matrix_ref(prog
, tokens
);
1218 assert(base_pos
+ (int)i
* 4 == pos
);
1222 tokens
[1] = 0; /* unused array index */
1223 base_pos
= add_matrix_ref(prog
, tokens
);
1225 tokens
[4] = matrices
[i
].modifier
;
1227 entry
= new(mem_ctx
) variable_storage(var
,
1239 ir_to_mesa_visitor::visit(ir_dereference_variable
*ir
)
1241 ir_to_mesa_src_reg src_reg
;
1242 variable_storage
*entry
= find_variable_storage(ir
->var
);
1247 switch (ir
->var
->mode
) {
1248 case ir_var_uniform
:
1249 entry
= get_builtin_matrix_ref(this->mem_ctx
, this->prog
, ir
->var
,
1254 /* FINISHME: Fix up uniform name for arrays and things */
1255 if (ir
->var
->type
->base_type
== GLSL_TYPE_SAMPLER
) {
1256 /* FINISHME: we whack the location of the var here, which
1257 * is probably not expected. But we need to communicate
1258 * mesa's sampler number to the tex instruction.
1260 int sampler
= _mesa_add_sampler(this->prog
->Parameters
,
1262 ir
->var
->type
->gl_type
);
1263 map_sampler(ir
->var
->location
, sampler
);
1265 entry
= new(mem_ctx
) variable_storage(ir
->var
, PROGRAM_SAMPLER
,
1267 this->variables
.push_tail(entry
);
1271 assert(ir
->var
->type
->gl_type
!= 0 &&
1272 ir
->var
->type
->gl_type
!= GL_INVALID_ENUM
);
1274 if (ir
->var
->type
->is_vector() ||
1275 ir
->var
->type
->is_scalar()) {
1276 len
= ir
->var
->type
->vector_elements
;
1278 len
= type_size(ir
->var
->type
) * 4;
1281 loc
= _mesa_add_uniform(this->prog
->Parameters
,
1284 ir
->var
->type
->gl_type
,
1287 /* Always mark the uniform used at this point. If it isn't
1288 * used, dead code elimination should have nuked the decl already.
1290 this->prog
->Parameters
->Parameters
[loc
].Used
= GL_TRUE
;
1292 entry
= new(mem_ctx
) variable_storage(ir
->var
, PROGRAM_UNIFORM
, loc
);
1293 this->variables
.push_tail(entry
);
1298 /* The linker assigns locations for varyings and attributes,
1299 * including deprecated builtins (like gl_Color), user-assign
1300 * generic attributes (glBindVertexLocation), and
1301 * user-defined varyings.
1303 * FINISHME: We would hit this path for function arguments. Fix!
1305 assert(ir
->var
->location
!= -1);
1306 if (ir
->var
->mode
== ir_var_in
||
1307 ir
->var
->mode
== ir_var_inout
) {
1308 entry
= new(mem_ctx
) variable_storage(ir
->var
,
1312 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
&&
1313 ir
->var
->location
>= VERT_ATTRIB_GENERIC0
) {
1314 _mesa_add_attribute(prog
->Attributes
,
1316 type_size(ir
->var
->type
) * 4,
1317 ir
->var
->type
->gl_type
,
1318 ir
->var
->location
- VERT_ATTRIB_GENERIC0
);
1321 entry
= new(mem_ctx
) variable_storage(ir
->var
,
1328 case ir_var_temporary
:
1329 entry
= new(mem_ctx
) variable_storage(ir
->var
, PROGRAM_TEMPORARY
,
1331 this->variables
.push_tail(entry
);
1333 next_temp
+= type_size(ir
->var
->type
);
1338 printf("Failed to make storage for %s\n", ir
->var
->name
);
1343 src_reg
.file
= entry
->file
;
1344 src_reg
.index
= entry
->index
;
1345 /* If the type is smaller than a vec4, replicate the last channel out. */
1346 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
1347 src_reg
.swizzle
= swizzle_for_size(ir
->var
->type
->vector_elements
);
1349 src_reg
.swizzle
= SWIZZLE_NOOP
;
1350 src_reg
.reladdr
= NULL
;
1353 this->result
= src_reg
;
1357 ir_to_mesa_visitor::visit(ir_dereference_array
*ir
)
1359 ir_variable
*var
= ir
->variable_referenced();
1361 ir_to_mesa_src_reg src_reg
;
1362 ir_dereference_variable
*deref_var
= ir
->array
->as_dereference_variable();
1363 int element_size
= type_size(ir
->type
);
1365 index
= ir
->array_index
->constant_expression_value();
1367 if (deref_var
&& strncmp(deref_var
->var
->name
,
1369 strlen("gl_TextureMatrix")) == 0) {
1370 ir_to_mesa_src_reg src_reg
;
1371 struct variable_storage
*entry
;
1373 entry
= get_builtin_matrix_ref(this->mem_ctx
, this->prog
, deref_var
->var
,
1377 src_reg
.file
= entry
->file
;
1378 src_reg
.index
= entry
->index
;
1379 src_reg
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1383 src_reg
.reladdr
= NULL
;
1385 ir_to_mesa_src_reg index_reg
= get_temp(glsl_type::float_type
);
1387 ir
->array_index
->accept(this);
1388 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
,
1389 ir_to_mesa_dst_reg_from_src(index_reg
),
1390 this->result
, src_reg_for_float(element_size
));
1392 src_reg
.reladdr
= talloc(mem_ctx
, ir_to_mesa_src_reg
);
1393 memcpy(src_reg
.reladdr
, &index_reg
, sizeof(index_reg
));
1396 this->result
= src_reg
;
1400 if (strncmp(var
->name
, "gl_", 3) == 0 && var
->mode
== ir_var_uniform
&&
1401 !var
->type
->is_matrix()) {
1402 ir_dereference_record
*record
= NULL
;
1403 if (ir
->array
->ir_type
== ir_type_dereference_record
)
1404 record
= (ir_dereference_record
*)ir
->array
;
1406 assert(index
|| !"FINISHME: variable-indexed builtin uniform access");
1408 this->result
= get_builtin_uniform_reg(prog
,
1411 record
? record
->field
: NULL
);
1414 ir
->array
->accept(this);
1415 src_reg
= this->result
;
1418 src_reg
.index
+= index
->value
.i
[0] * element_size
;
1420 ir_to_mesa_src_reg array_base
= this->result
;
1421 /* Variable index array dereference. It eats the "vec4" of the
1422 * base of the array and an index that offsets the Mesa register
1425 ir
->array_index
->accept(this);
1427 ir_to_mesa_src_reg index_reg
;
1429 if (element_size
== 1) {
1430 index_reg
= this->result
;
1432 index_reg
= get_temp(glsl_type::float_type
);
1434 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
,
1435 ir_to_mesa_dst_reg_from_src(index_reg
),
1436 this->result
, src_reg_for_float(element_size
));
1439 src_reg
.reladdr
= talloc(mem_ctx
, ir_to_mesa_src_reg
);
1440 memcpy(src_reg
.reladdr
, &index_reg
, sizeof(index_reg
));
1443 /* If the type is smaller than a vec4, replicate the last channel out. */
1444 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
1445 src_reg
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1447 src_reg
.swizzle
= SWIZZLE_NOOP
;
1449 this->result
= src_reg
;
1453 ir_to_mesa_visitor::visit(ir_dereference_record
*ir
)
1456 const glsl_type
*struct_type
= ir
->record
->type
;
1458 ir_variable
*var
= ir
->record
->variable_referenced();
1460 if (strncmp(var
->name
, "gl_", 3) == 0 && var
->mode
== ir_var_uniform
) {
1463 this->result
= get_builtin_uniform_reg(prog
,
1470 ir
->record
->accept(this);
1472 for (i
= 0; i
< struct_type
->length
; i
++) {
1473 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
1475 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
1477 this->result
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1478 this->result
.index
+= offset
;
1482 * We want to be careful in assignment setup to hit the actual storage
1483 * instead of potentially using a temporary like we might with the
1484 * ir_dereference handler.
1486 * Thanks to ir_swizzle_swizzle, and ir_vec_index_to_swizzle, we
1487 * should only see potentially one variable array index of a vector,
1488 * and one swizzle, before getting to actual vec4 storage. So handle
1489 * those, then go use ir_dereference to handle the rest.
1491 static struct ir_to_mesa_dst_reg
1492 get_assignment_lhs(ir_instruction
*ir
, ir_to_mesa_visitor
*v
,
1493 ir_to_mesa_src_reg
*r
)
1495 struct ir_to_mesa_dst_reg dst_reg
;
1498 ir_dereference_array
*deref_array
= ir
->as_dereference_array();
1499 /* This should have been handled by ir_vec_index_to_cond_assign */
1501 assert(!deref_array
->array
->type
->is_vector());
1504 /* Use the rvalue deref handler for the most part. We'll ignore
1505 * swizzles in it and write swizzles using writemask, though.
1508 dst_reg
= ir_to_mesa_dst_reg_from_src(v
->result
);
1510 if ((swiz
= ir
->as_swizzle())) {
1517 int new_r_swizzle
[4];
1518 int orig_r_swizzle
= r
->swizzle
;
1521 for (i
= 0; i
< 4; i
++) {
1522 new_r_swizzle
[i
] = GET_SWZ(orig_r_swizzle
, 0);
1525 dst_reg
.writemask
= 0;
1526 for (i
= 0; i
< 4; i
++) {
1527 if (i
< swiz
->mask
.num_components
) {
1528 dst_reg
.writemask
|= 1 << swizzles
[i
];
1529 new_r_swizzle
[swizzles
[i
]] = GET_SWZ(orig_r_swizzle
, i
);
1533 r
->swizzle
= MAKE_SWIZZLE4(new_r_swizzle
[0],
1543 ir_to_mesa_visitor::visit(ir_assignment
*ir
)
1545 struct ir_to_mesa_dst_reg l
;
1546 struct ir_to_mesa_src_reg r
;
1549 ir
->rhs
->accept(this);
1552 l
= get_assignment_lhs(ir
->lhs
, this, &r
);
1554 assert(l
.file
!= PROGRAM_UNDEFINED
);
1555 assert(r
.file
!= PROGRAM_UNDEFINED
);
1557 if (ir
->condition
) {
1558 ir_to_mesa_src_reg condition
;
1560 ir
->condition
->accept(this);
1561 condition
= this->result
;
1563 /* We use the OPCODE_CMP (a < 0 ? b : c) for conditional moves,
1564 * and the condition we produced is 0.0 or 1.0. By flipping the
1565 * sign, we can choose which value OPCODE_CMP produces without
1566 * an extra computing the condition.
1568 condition
.negate
= ~condition
.negate
;
1569 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
1570 ir_to_mesa_emit_op3(ir
, OPCODE_CMP
, l
,
1571 condition
, r
, ir_to_mesa_src_reg_from_dst(l
));
1576 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
1577 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, l
, r
);
1586 ir_to_mesa_visitor::visit(ir_constant
*ir
)
1588 ir_to_mesa_src_reg src_reg
;
1589 GLfloat stack_vals
[4];
1590 GLfloat
*values
= stack_vals
;
1593 /* Unfortunately, 4 floats is all we can get into
1594 * _mesa_add_unnamed_constant. So, make a temp to store an
1595 * aggregate constant and move each constant value into it. If we
1596 * get lucky, copy propagation will eliminate the extra moves.
1599 if (ir
->type
->base_type
== GLSL_TYPE_STRUCT
) {
1600 ir_to_mesa_src_reg temp_base
= get_temp(ir
->type
);
1601 ir_to_mesa_dst_reg temp
= ir_to_mesa_dst_reg_from_src(temp_base
);
1603 foreach_iter(exec_list_iterator
, iter
, ir
->components
) {
1604 ir_constant
*field_value
= (ir_constant
*)iter
.get();
1605 int size
= type_size(field_value
->type
);
1609 field_value
->accept(this);
1610 src_reg
= this->result
;
1612 for (i
= 0; i
< (unsigned int)size
; i
++) {
1613 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, temp
, src_reg
);
1619 this->result
= temp_base
;
1623 if (ir
->type
->is_array()) {
1624 ir_to_mesa_src_reg temp_base
= get_temp(ir
->type
);
1625 ir_to_mesa_dst_reg temp
= ir_to_mesa_dst_reg_from_src(temp_base
);
1626 int size
= type_size(ir
->type
->fields
.array
);
1630 for (i
= 0; i
< ir
->type
->length
; i
++) {
1631 ir
->array_elements
[i
]->accept(this);
1632 src_reg
= this->result
;
1633 for (int j
= 0; j
< size
; j
++) {
1634 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, temp
, src_reg
);
1640 this->result
= temp_base
;
1644 if (ir
->type
->is_matrix()) {
1645 ir_to_mesa_src_reg mat
= get_temp(ir
->type
);
1646 ir_to_mesa_dst_reg mat_column
= ir_to_mesa_dst_reg_from_src(mat
);
1648 for (i
= 0; i
< ir
->type
->matrix_columns
; i
++) {
1649 src_reg
.file
= PROGRAM_CONSTANT
;
1651 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
1652 values
= &ir
->value
.f
[i
* ir
->type
->vector_elements
];
1654 src_reg
.index
= _mesa_add_unnamed_constant(this->prog
->Parameters
,
1656 ir
->type
->vector_elements
,
1658 src_reg
.reladdr
= NULL
;
1660 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, mat_column
, src_reg
);
1668 src_reg
.file
= PROGRAM_CONSTANT
;
1669 switch (ir
->type
->base_type
) {
1670 case GLSL_TYPE_FLOAT
:
1671 values
= &ir
->value
.f
[0];
1673 case GLSL_TYPE_UINT
:
1674 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1675 values
[i
] = ir
->value
.u
[i
];
1679 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1680 values
[i
] = ir
->value
.i
[i
];
1683 case GLSL_TYPE_BOOL
:
1684 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1685 values
[i
] = ir
->value
.b
[i
];
1689 assert(!"Non-float/uint/int/bool constant");
1692 src_reg
.index
= _mesa_add_unnamed_constant(this->prog
->Parameters
,
1693 values
, ir
->type
->vector_elements
,
1695 src_reg
.reladdr
= NULL
;
1698 this->result
= src_reg
;
1702 ir_to_mesa_visitor::get_function_signature(ir_function_signature
*sig
)
1704 function_entry
*entry
;
1706 foreach_iter(exec_list_iterator
, iter
, this->function_signatures
) {
1707 entry
= (function_entry
*)iter
.get();
1709 if (entry
->sig
== sig
)
1713 entry
= talloc(mem_ctx
, function_entry
);
1715 entry
->sig_id
= this->next_signature_id
++;
1716 entry
->bgn_inst
= NULL
;
1718 /* Allocate storage for all the parameters. */
1719 foreach_iter(exec_list_iterator
, iter
, sig
->parameters
) {
1720 ir_variable
*param
= (ir_variable
*)iter
.get();
1721 variable_storage
*storage
;
1723 storage
= find_variable_storage(param
);
1726 storage
= new(mem_ctx
) variable_storage(param
, PROGRAM_TEMPORARY
,
1728 this->variables
.push_tail(storage
);
1730 this->next_temp
+= type_size(param
->type
);
1733 if (!sig
->return_type
->is_void()) {
1734 entry
->return_reg
= get_temp(sig
->return_type
);
1736 entry
->return_reg
= ir_to_mesa_undef
;
1739 this->function_signatures
.push_tail(entry
);
1744 ir_to_mesa_visitor::visit(ir_call
*ir
)
1746 ir_to_mesa_instruction
*call_inst
;
1747 ir_function_signature
*sig
= ir
->get_callee();
1748 function_entry
*entry
= get_function_signature(sig
);
1751 /* Process in parameters. */
1752 exec_list_iterator sig_iter
= sig
->parameters
.iterator();
1753 foreach_iter(exec_list_iterator
, iter
, *ir
) {
1754 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
1755 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
1757 if (param
->mode
== ir_var_in
||
1758 param
->mode
== ir_var_inout
) {
1759 variable_storage
*storage
= find_variable_storage(param
);
1762 param_rval
->accept(this);
1763 ir_to_mesa_src_reg r
= this->result
;
1765 ir_to_mesa_dst_reg l
;
1766 l
.file
= storage
->file
;
1767 l
.index
= storage
->index
;
1769 l
.writemask
= WRITEMASK_XYZW
;
1770 l
.cond_mask
= COND_TR
;
1772 for (i
= 0; i
< type_size(param
->type
); i
++) {
1773 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, l
, r
);
1781 assert(!sig_iter
.has_next());
1783 /* Emit call instruction */
1784 call_inst
= ir_to_mesa_emit_op1(ir
, OPCODE_CAL
,
1785 ir_to_mesa_undef_dst
, ir_to_mesa_undef
);
1786 call_inst
->function
= entry
;
1788 /* Process out parameters. */
1789 sig_iter
= sig
->parameters
.iterator();
1790 foreach_iter(exec_list_iterator
, iter
, *ir
) {
1791 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
1792 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
1794 if (param
->mode
== ir_var_out
||
1795 param
->mode
== ir_var_inout
) {
1796 variable_storage
*storage
= find_variable_storage(param
);
1799 ir_to_mesa_src_reg r
;
1800 r
.file
= storage
->file
;
1801 r
.index
= storage
->index
;
1803 r
.swizzle
= SWIZZLE_NOOP
;
1806 param_rval
->accept(this);
1807 ir_to_mesa_dst_reg l
= ir_to_mesa_dst_reg_from_src(this->result
);
1809 for (i
= 0; i
< type_size(param
->type
); i
++) {
1810 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, l
, r
);
1818 assert(!sig_iter
.has_next());
1820 /* Process return value. */
1821 this->result
= entry
->return_reg
;
1826 ir_to_mesa_visitor::visit(ir_texture
*ir
)
1828 ir_to_mesa_src_reg result_src
, coord
, lod_info
= { 0 }, projector
;
1829 ir_to_mesa_dst_reg result_dst
, coord_dst
;
1830 ir_to_mesa_instruction
*inst
= NULL
;
1831 prog_opcode opcode
= OPCODE_NOP
;
1833 ir
->coordinate
->accept(this);
1835 /* Put our coords in a temp. We'll need to modify them for shadow,
1836 * projection, or LOD, so the only case we'd use it as is is if
1837 * we're doing plain old texturing. Mesa IR optimization should
1838 * handle cleaning up our mess in that case.
1840 coord
= get_temp(glsl_type::vec4_type
);
1841 coord_dst
= ir_to_mesa_dst_reg_from_src(coord
);
1842 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, coord_dst
,
1845 if (ir
->projector
) {
1846 ir
->projector
->accept(this);
1847 projector
= this->result
;
1850 /* Storage for our result. Ideally for an assignment we'd be using
1851 * the actual storage for the result here, instead.
1853 result_src
= get_temp(glsl_type::vec4_type
);
1854 result_dst
= ir_to_mesa_dst_reg_from_src(result_src
);
1858 opcode
= OPCODE_TEX
;
1861 opcode
= OPCODE_TXB
;
1862 ir
->lod_info
.bias
->accept(this);
1863 lod_info
= this->result
;
1866 opcode
= OPCODE_TXL
;
1867 ir
->lod_info
.lod
->accept(this);
1868 lod_info
= this->result
;
1872 assert(!"GLSL 1.30 features unsupported");
1876 if (ir
->projector
) {
1877 if (opcode
== OPCODE_TEX
) {
1878 /* Slot the projector in as the last component of the coord. */
1879 coord_dst
.writemask
= WRITEMASK_W
;
1880 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, coord_dst
, projector
);
1881 coord_dst
.writemask
= WRITEMASK_XYZW
;
1882 opcode
= OPCODE_TXP
;
1884 ir_to_mesa_src_reg coord_w
= coord
;
1885 coord_w
.swizzle
= SWIZZLE_WWWW
;
1887 /* For the other TEX opcodes there's no projective version
1888 * since the last slot is taken up by lod info. Do the
1889 * projective divide now.
1891 coord_dst
.writemask
= WRITEMASK_W
;
1892 ir_to_mesa_emit_op1(ir
, OPCODE_RCP
, coord_dst
, projector
);
1894 coord_dst
.writemask
= WRITEMASK_XYZ
;
1895 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
, coord_dst
, coord
, coord_w
);
1897 coord_dst
.writemask
= WRITEMASK_XYZW
;
1898 coord
.swizzle
= SWIZZLE_XYZW
;
1902 if (ir
->shadow_comparitor
) {
1903 /* Slot the shadow value in as the second to last component of the
1906 ir
->shadow_comparitor
->accept(this);
1907 coord_dst
.writemask
= WRITEMASK_Z
;
1908 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, coord_dst
, this->result
);
1909 coord_dst
.writemask
= WRITEMASK_XYZW
;
1912 if (opcode
== OPCODE_TXL
|| opcode
== OPCODE_TXB
) {
1913 /* Mesa IR stores lod or lod bias in the last channel of the coords. */
1914 coord_dst
.writemask
= WRITEMASK_W
;
1915 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, coord_dst
, lod_info
);
1916 coord_dst
.writemask
= WRITEMASK_XYZW
;
1919 inst
= ir_to_mesa_emit_op1(ir
, opcode
, result_dst
, coord
);
1921 if (ir
->shadow_comparitor
)
1922 inst
->tex_shadow
= GL_TRUE
;
1924 ir_dereference_variable
*sampler
= ir
->sampler
->as_dereference_variable();
1925 assert(sampler
); /* FINISHME: sampler arrays */
1926 /* generate the mapping, remove when we generate storage at
1929 sampler
->accept(this);
1931 inst
->sampler
= get_sampler_number(sampler
->var
->location
);
1933 switch (sampler
->type
->sampler_dimensionality
) {
1934 case GLSL_SAMPLER_DIM_1D
:
1935 inst
->tex_target
= TEXTURE_1D_INDEX
;
1937 case GLSL_SAMPLER_DIM_2D
:
1938 inst
->tex_target
= TEXTURE_2D_INDEX
;
1940 case GLSL_SAMPLER_DIM_3D
:
1941 inst
->tex_target
= TEXTURE_3D_INDEX
;
1943 case GLSL_SAMPLER_DIM_CUBE
:
1944 inst
->tex_target
= TEXTURE_CUBE_INDEX
;
1947 assert(!"FINISHME: other texture targets");
1950 this->result
= result_src
;
1954 ir_to_mesa_visitor::visit(ir_return
*ir
)
1956 assert(current_function
);
1958 if (ir
->get_value()) {
1959 ir_to_mesa_dst_reg l
;
1962 ir
->get_value()->accept(this);
1963 ir_to_mesa_src_reg r
= this->result
;
1965 l
= ir_to_mesa_dst_reg_from_src(current_function
->return_reg
);
1967 for (i
= 0; i
< type_size(current_function
->sig
->return_type
); i
++) {
1968 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, l
, r
);
1974 ir_to_mesa_emit_op0(ir
, OPCODE_RET
);
1978 ir_to_mesa_visitor::visit(ir_discard
*ir
)
1980 assert(ir
->condition
== NULL
); /* FINISHME */
1982 ir_to_mesa_emit_op0(ir
, OPCODE_KIL_NV
);
1986 ir_to_mesa_visitor::visit(ir_if
*ir
)
1988 ir_to_mesa_instruction
*cond_inst
, *if_inst
, *else_inst
= NULL
;
1989 ir_to_mesa_instruction
*prev_inst
;
1991 prev_inst
= (ir_to_mesa_instruction
*)this->instructions
.get_tail();
1993 ir
->condition
->accept(this);
1994 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
1996 if (ctx
->Shader
.EmitCondCodes
) {
1997 cond_inst
= (ir_to_mesa_instruction
*)this->instructions
.get_tail();
1999 /* See if we actually generated any instruction for generating
2000 * the condition. If not, then cook up a move to a temp so we
2001 * have something to set cond_update on.
2003 if (cond_inst
== prev_inst
) {
2004 ir_to_mesa_src_reg temp
= get_temp(glsl_type::bool_type
);
2005 cond_inst
= ir_to_mesa_emit_op1(ir
->condition
, OPCODE_MOV
,
2006 ir_to_mesa_dst_reg_from_src(temp
),
2009 cond_inst
->cond_update
= GL_TRUE
;
2011 if_inst
= ir_to_mesa_emit_op0(ir
->condition
, OPCODE_IF
);
2012 if_inst
->dst_reg
.cond_mask
= COND_NE
;
2014 if_inst
= ir_to_mesa_emit_op1(ir
->condition
,
2015 OPCODE_IF
, ir_to_mesa_undef_dst
,
2019 this->instructions
.push_tail(if_inst
);
2021 visit_exec_list(&ir
->then_instructions
, this);
2023 if (!ir
->else_instructions
.is_empty()) {
2024 else_inst
= ir_to_mesa_emit_op0(ir
->condition
, OPCODE_ELSE
);
2025 visit_exec_list(&ir
->else_instructions
, this);
2028 if_inst
= ir_to_mesa_emit_op1(ir
->condition
, OPCODE_ENDIF
,
2029 ir_to_mesa_undef_dst
, ir_to_mesa_undef
);
2032 ir_to_mesa_visitor::ir_to_mesa_visitor()
2034 result
.file
= PROGRAM_UNDEFINED
;
2036 next_signature_id
= 1;
2038 sampler_map_size
= 0;
2039 current_function
= NULL
;
2042 static struct prog_src_register
2043 mesa_src_reg_from_ir_src_reg(ir_to_mesa_src_reg reg
)
2045 struct prog_src_register mesa_reg
;
2047 mesa_reg
.File
= reg
.file
;
2048 assert(reg
.index
< (1 << INST_INDEX_BITS
) - 1);
2049 mesa_reg
.Index
= reg
.index
;
2050 mesa_reg
.Swizzle
= reg
.swizzle
;
2051 mesa_reg
.RelAddr
= reg
.reladdr
!= NULL
;
2052 mesa_reg
.Negate
= reg
.negate
;
2059 set_branchtargets(ir_to_mesa_visitor
*v
,
2060 struct prog_instruction
*mesa_instructions
,
2061 int num_instructions
)
2063 int if_count
= 0, loop_count
= 0;
2064 int *if_stack
, *loop_stack
;
2065 int if_stack_pos
= 0, loop_stack_pos
= 0;
2068 for (i
= 0; i
< num_instructions
; i
++) {
2069 switch (mesa_instructions
[i
].Opcode
) {
2073 case OPCODE_BGNLOOP
:
2078 mesa_instructions
[i
].BranchTarget
= -1;
2085 if_stack
= (int *)calloc(if_count
, sizeof(*if_stack
));
2086 loop_stack
= (int *)calloc(loop_count
, sizeof(*loop_stack
));
2088 for (i
= 0; i
< num_instructions
; i
++) {
2089 switch (mesa_instructions
[i
].Opcode
) {
2091 if_stack
[if_stack_pos
] = i
;
2095 mesa_instructions
[if_stack
[if_stack_pos
- 1]].BranchTarget
= i
;
2096 if_stack
[if_stack_pos
- 1] = i
;
2099 mesa_instructions
[if_stack
[if_stack_pos
- 1]].BranchTarget
= i
;
2102 case OPCODE_BGNLOOP
:
2103 loop_stack
[loop_stack_pos
] = i
;
2106 case OPCODE_ENDLOOP
:
2108 /* Rewrite any breaks/conts at this nesting level (haven't
2109 * already had a BranchTarget assigned) to point to the end
2112 for (j
= loop_stack
[loop_stack_pos
]; j
< i
; j
++) {
2113 if (mesa_instructions
[j
].Opcode
== OPCODE_BRK
||
2114 mesa_instructions
[j
].Opcode
== OPCODE_CONT
) {
2115 if (mesa_instructions
[j
].BranchTarget
== -1) {
2116 mesa_instructions
[j
].BranchTarget
= i
;
2120 /* The loop ends point at each other. */
2121 mesa_instructions
[i
].BranchTarget
= loop_stack
[loop_stack_pos
];
2122 mesa_instructions
[loop_stack
[loop_stack_pos
]].BranchTarget
= i
;
2125 foreach_iter(exec_list_iterator
, iter
, v
->function_signatures
) {
2126 function_entry
*entry
= (function_entry
*)iter
.get();
2128 if (entry
->sig_id
== mesa_instructions
[i
].BranchTarget
) {
2129 mesa_instructions
[i
].BranchTarget
= entry
->inst
;
2143 print_program(struct prog_instruction
*mesa_instructions
,
2144 ir_instruction
**mesa_instruction_annotation
,
2145 int num_instructions
)
2147 ir_instruction
*last_ir
= NULL
;
2151 for (i
= 0; i
< num_instructions
; i
++) {
2152 struct prog_instruction
*mesa_inst
= mesa_instructions
+ i
;
2153 ir_instruction
*ir
= mesa_instruction_annotation
[i
];
2155 fprintf(stdout
, "%3d: ", i
);
2157 if (last_ir
!= ir
&& ir
) {
2160 for (j
= 0; j
< indent
; j
++) {
2161 fprintf(stdout
, " ");
2167 fprintf(stdout
, " "); /* line number spacing. */
2170 indent
= _mesa_fprint_instruction_opt(stdout
, mesa_inst
, indent
,
2171 PROG_PRINT_DEBUG
, NULL
);
2176 mark_input(struct gl_program
*prog
,
2180 prog
->InputsRead
|= BITFIELD64_BIT(index
);
2184 if (index
>= FRAG_ATTRIB_TEX0
&& index
<= FRAG_ATTRIB_TEX7
) {
2185 for (i
= 0; i
< 8; i
++) {
2186 prog
->InputsRead
|= BITFIELD64_BIT(FRAG_ATTRIB_TEX0
+ i
);
2189 assert(!"FINISHME: Mark InputsRead for varying arrays");
2195 mark_output(struct gl_program
*prog
,
2199 prog
->OutputsWritten
|= BITFIELD64_BIT(index
);
2203 if (index
>= VERT_RESULT_TEX0
&& index
<= VERT_RESULT_TEX7
) {
2204 for (i
= 0; i
< 8; i
++) {
2205 prog
->OutputsWritten
|= BITFIELD64_BIT(FRAG_ATTRIB_TEX0
+ i
);
2208 assert(!"FINISHME: Mark OutputsWritten for varying arrays");
2214 count_resources(struct gl_program
*prog
)
2218 prog
->InputsRead
= 0;
2219 prog
->OutputsWritten
= 0;
2220 prog
->SamplersUsed
= 0;
2222 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
2223 struct prog_instruction
*inst
= &prog
->Instructions
[i
];
2226 switch (inst
->DstReg
.File
) {
2227 case PROGRAM_OUTPUT
:
2228 mark_output(prog
, inst
->DstReg
.Index
, inst
->DstReg
.RelAddr
);
2231 mark_input(prog
, inst
->DstReg
.Index
, inst
->DstReg
.RelAddr
);
2237 for (reg
= 0; reg
< _mesa_num_inst_src_regs(inst
->Opcode
); reg
++) {
2238 switch (inst
->SrcReg
[reg
].File
) {
2239 case PROGRAM_OUTPUT
:
2240 mark_output(prog
, inst
->SrcReg
[reg
].Index
,
2241 inst
->SrcReg
[reg
].RelAddr
);
2244 mark_input(prog
, inst
->SrcReg
[reg
].Index
, inst
->SrcReg
[reg
].RelAddr
);
2251 /* Instead of just using the uniform's value to map to a
2252 * sampler, Mesa first allocates a separate number for the
2253 * sampler (_mesa_add_sampler), then we reindex it down to a
2254 * small integer (sampler_map[], SamplersUsed), then that gets
2255 * mapped to the uniform's value, and we get an actual sampler.
2257 if (_mesa_is_tex_instruction(inst
->Opcode
)) {
2258 prog
->SamplerTargets
[inst
->TexSrcUnit
] =
2259 (gl_texture_index
)inst
->TexSrcTarget
;
2260 prog
->SamplersUsed
|= 1 << inst
->TexSrcUnit
;
2261 if (inst
->TexShadow
) {
2262 prog
->ShadowSamplers
|= 1 << inst
->TexSrcUnit
;
2267 _mesa_update_shader_textures_used(prog
);
2270 /* Each stage has some uniforms in its Parameters list. The Uniforms
2271 * list for the linked shader program has a pointer to these uniforms
2272 * in each of the stage's Parameters list, so that their values can be
2273 * updated when a uniform is set.
2276 link_uniforms_to_shared_uniform_list(struct gl_uniform_list
*uniforms
,
2277 struct gl_program
*prog
)
2281 for (i
= 0; i
< prog
->Parameters
->NumParameters
; i
++) {
2282 const struct gl_program_parameter
*p
= prog
->Parameters
->Parameters
+ i
;
2284 if (p
->Type
== PROGRAM_UNIFORM
|| p
->Type
== PROGRAM_SAMPLER
) {
2285 struct gl_uniform
*uniform
=
2286 _mesa_append_uniform(uniforms
, p
->Name
, prog
->Target
, i
);
2288 uniform
->Initialized
= p
->Initialized
;
2294 get_mesa_program(GLcontext
*ctx
, struct gl_shader_program
*shader_program
,
2295 struct gl_shader
*shader
)
2297 void *mem_ctx
= shader_program
;
2298 ir_to_mesa_visitor v
;
2299 struct prog_instruction
*mesa_instructions
, *mesa_inst
;
2300 ir_instruction
**mesa_instruction_annotation
;
2302 struct gl_program
*prog
;
2304 const char *target_string
;
2307 switch (shader
->Type
) {
2308 case GL_VERTEX_SHADER
:
2309 target
= GL_VERTEX_PROGRAM_ARB
;
2310 target_string
= "vertex";
2312 case GL_FRAGMENT_SHADER
:
2313 target
= GL_FRAGMENT_PROGRAM_ARB
;
2314 target_string
= "fragment";
2317 assert(!"should not be reached");
2321 validate_ir_tree(shader
->ir
);
2323 prog
= ctx
->Driver
.NewProgram(ctx
, target
, shader_program
->Name
);
2326 prog
->Parameters
= _mesa_new_parameter_list();
2327 prog
->Varying
= _mesa_new_parameter_list();
2328 prog
->Attributes
= _mesa_new_parameter_list();
2332 v
.mem_ctx
= talloc_new(NULL
);
2334 /* Emit Mesa IR for main(). */
2335 visit_exec_list(shader
->ir
, &v
);
2336 v
.ir_to_mesa_emit_op0(NULL
, OPCODE_END
);
2338 /* Now emit bodies for any functions that were used. */
2340 progress
= GL_FALSE
;
2342 foreach_iter(exec_list_iterator
, iter
, v
.function_signatures
) {
2343 function_entry
*entry
= (function_entry
*)iter
.get();
2345 if (!entry
->bgn_inst
) {
2346 v
.current_function
= entry
;
2348 entry
->bgn_inst
= v
.ir_to_mesa_emit_op0(NULL
, OPCODE_BGNSUB
);
2349 entry
->bgn_inst
->function
= entry
;
2351 visit_exec_list(&entry
->sig
->body
, &v
);
2353 ir_to_mesa_instruction
*last
;
2354 last
= (ir_to_mesa_instruction
*)v
.instructions
.get_tail();
2355 if (last
->op
!= OPCODE_RET
)
2356 v
.ir_to_mesa_emit_op0(NULL
, OPCODE_RET
);
2358 ir_to_mesa_instruction
*end
;
2359 end
= v
.ir_to_mesa_emit_op0(NULL
, OPCODE_ENDSUB
);
2360 end
->function
= entry
;
2367 prog
->NumTemporaries
= v
.next_temp
;
2369 int num_instructions
= 0;
2370 foreach_iter(exec_list_iterator
, iter
, v
.instructions
) {
2375 (struct prog_instruction
*)calloc(num_instructions
,
2376 sizeof(*mesa_instructions
));
2377 mesa_instruction_annotation
= talloc_array(mem_ctx
, ir_instruction
*,
2380 mesa_inst
= mesa_instructions
;
2382 foreach_iter(exec_list_iterator
, iter
, v
.instructions
) {
2383 ir_to_mesa_instruction
*inst
= (ir_to_mesa_instruction
*)iter
.get();
2385 mesa_inst
->Opcode
= inst
->op
;
2386 mesa_inst
->CondUpdate
= inst
->cond_update
;
2387 mesa_inst
->DstReg
.File
= inst
->dst_reg
.file
;
2388 mesa_inst
->DstReg
.Index
= inst
->dst_reg
.index
;
2389 mesa_inst
->DstReg
.CondMask
= inst
->dst_reg
.cond_mask
;
2390 mesa_inst
->DstReg
.WriteMask
= inst
->dst_reg
.writemask
;
2391 mesa_inst
->DstReg
.RelAddr
= inst
->dst_reg
.reladdr
!= NULL
;
2392 mesa_inst
->SrcReg
[0] = mesa_src_reg_from_ir_src_reg(inst
->src_reg
[0]);
2393 mesa_inst
->SrcReg
[1] = mesa_src_reg_from_ir_src_reg(inst
->src_reg
[1]);
2394 mesa_inst
->SrcReg
[2] = mesa_src_reg_from_ir_src_reg(inst
->src_reg
[2]);
2395 mesa_inst
->TexSrcUnit
= inst
->sampler
;
2396 mesa_inst
->TexSrcTarget
= inst
->tex_target
;
2397 mesa_inst
->TexShadow
= inst
->tex_shadow
;
2398 mesa_instruction_annotation
[i
] = inst
->ir
;
2400 if (ctx
->Shader
.EmitNoIfs
&& mesa_inst
->Opcode
== OPCODE_IF
) {
2401 shader_program
->InfoLog
=
2402 talloc_asprintf_append(shader_program
->InfoLog
,
2403 "Couldn't flatten if statement\n");
2404 shader_program
->LinkStatus
= false;
2407 switch (mesa_inst
->Opcode
) {
2409 inst
->function
->inst
= i
;
2410 mesa_inst
->Comment
= strdup(inst
->function
->sig
->function_name());
2413 mesa_inst
->Comment
= strdup(inst
->function
->sig
->function_name());
2416 mesa_inst
->BranchTarget
= inst
->function
->sig_id
; /* rewritten later */
2419 prog
->NumAddressRegs
= 1;
2429 set_branchtargets(&v
, mesa_instructions
, num_instructions
);
2430 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
2431 printf("Mesa %s program:\n", target_string
);
2432 print_program(mesa_instructions
, mesa_instruction_annotation
,
2436 prog
->Instructions
= mesa_instructions
;
2437 prog
->NumInstructions
= num_instructions
;
2439 _mesa_reference_program(ctx
, &shader
->Program
, prog
);
2441 if ((ctx
->Shader
.Flags
& GLSL_NO_OPT
) == 0) {
2442 _mesa_optimize_program(ctx
, prog
);
2451 _mesa_glsl_compile_shader(GLcontext
*ctx
, struct gl_shader
*shader
)
2453 struct _mesa_glsl_parse_state
*state
=
2454 new(shader
) _mesa_glsl_parse_state(ctx
, shader
->Type
, shader
);
2456 const char *source
= shader
->Source
;
2457 state
->error
= preprocess(state
, &source
, &state
->info_log
,
2460 if (!state
->error
) {
2461 _mesa_glsl_lexer_ctor(state
, source
);
2462 _mesa_glsl_parse(state
);
2463 _mesa_glsl_lexer_dtor(state
);
2466 shader
->ir
= new(shader
) exec_list
;
2467 if (!state
->error
&& !state
->translation_unit
.is_empty())
2468 _mesa_ast_to_hir(shader
->ir
, state
);
2470 if (!state
->error
&& !shader
->ir
->is_empty()) {
2471 validate_ir_tree(shader
->ir
);
2474 do_mat_op_to_vec(shader
->ir
);
2475 do_mod_to_fract(shader
->ir
);
2476 do_div_to_mul_rcp(shader
->ir
);
2478 /* Optimization passes */
2483 progress
= do_function_inlining(shader
->ir
) || progress
;
2484 progress
= do_if_simplification(shader
->ir
) || progress
;
2485 progress
= do_copy_propagation(shader
->ir
) || progress
;
2486 progress
= do_dead_code_local(shader
->ir
) || progress
;
2487 progress
= do_dead_code_unlinked(shader
->ir
) || progress
;
2488 progress
= do_tree_grafting(shader
->ir
) || progress
;
2489 progress
= do_constant_variable_unlinked(shader
->ir
) || progress
;
2490 progress
= do_constant_folding(shader
->ir
) || progress
;
2491 progress
= do_algebraic(shader
->ir
) || progress
;
2492 progress
= do_if_return(shader
->ir
) || progress
;
2493 if (ctx
->Shader
.EmitNoIfs
)
2494 progress
= do_if_to_cond_assign(shader
->ir
) || progress
;
2496 progress
= do_vec_index_to_swizzle(shader
->ir
) || progress
;
2497 /* Do this one after the previous to let the easier pass handle
2498 * constant vector indexing.
2500 progress
= do_vec_index_to_cond_assign(shader
->ir
) || progress
;
2502 progress
= do_swizzle_swizzle(shader
->ir
) || progress
;
2505 validate_ir_tree(shader
->ir
);
2508 shader
->symbols
= state
->symbols
;
2510 shader
->CompileStatus
= !state
->error
;
2511 shader
->InfoLog
= state
->info_log
;
2512 shader
->Version
= state
->language_version
;
2513 memcpy(shader
->builtins_to_link
, state
->builtins_to_link
,
2514 sizeof(shader
->builtins_to_link
[0]) * state
->num_builtins_to_link
);
2515 shader
->num_builtins_to_link
= state
->num_builtins_to_link
;
2517 /* Retain any live IR, but trash the rest. */
2518 reparent_ir(shader
->ir
, shader
);
2524 _mesa_glsl_link_shader(GLcontext
*ctx
, struct gl_shader_program
*prog
)
2528 _mesa_clear_shader_program_data(ctx
, prog
);
2530 prog
->LinkStatus
= GL_TRUE
;
2532 for (i
= 0; i
< prog
->NumShaders
; i
++) {
2533 if (!prog
->Shaders
[i
]->CompileStatus
) {
2535 talloc_asprintf_append(prog
->InfoLog
,
2536 "linking with uncompiled shader");
2537 prog
->LinkStatus
= GL_FALSE
;
2541 prog
->Varying
= _mesa_new_parameter_list();
2542 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
, NULL
);
2543 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
, NULL
);
2545 if (prog
->LinkStatus
) {
2548 /* We don't use the linker's uniforms list, and cook up our own at
2551 free(prog
->Uniforms
);
2552 prog
->Uniforms
= _mesa_new_uniform_list();
2555 if (prog
->LinkStatus
) {
2556 for (i
= 0; i
< prog
->_NumLinkedShaders
; i
++) {
2557 struct gl_program
*linked_prog
;
2560 linked_prog
= get_mesa_program(ctx
, prog
,
2561 prog
->_LinkedShaders
[i
]);
2562 count_resources(linked_prog
);
2564 link_uniforms_to_shared_uniform_list(prog
->Uniforms
, linked_prog
);
2566 switch (prog
->_LinkedShaders
[i
]->Type
) {
2567 case GL_VERTEX_SHADER
:
2568 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
,
2569 (struct gl_vertex_program
*)linked_prog
);
2570 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_VERTEX_PROGRAM_ARB
,
2573 case GL_FRAGMENT_SHADER
:
2574 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
,
2575 (struct gl_fragment_program
*)linked_prog
);
2576 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
2581 prog
->LinkStatus
= GL_FALSE
;