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"
56 static int swizzle_for_size(int size
);
59 * This struct is a corresponding struct to Mesa prog_src_register, with
62 typedef struct ir_to_mesa_src_reg
{
63 ir_to_mesa_src_reg(int file
, int index
, const glsl_type
*type
)
67 if (type
&& (type
->is_scalar() || type
->is_vector() || type
->is_matrix()))
68 this->swizzle
= swizzle_for_size(type
->vector_elements
);
70 this->swizzle
= SWIZZLE_XYZW
;
77 this->file
= PROGRAM_UNDEFINED
;
80 int file
; /**< PROGRAM_* from Mesa */
81 int index
; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
82 GLuint swizzle
; /**< SWIZZLE_XYZWONEZERO swizzles from Mesa. */
83 int negate
; /**< NEGATE_XYZW mask from mesa */
84 /** Register index should be offset by the integer in this reg. */
85 ir_to_mesa_src_reg
*reladdr
;
88 typedef struct ir_to_mesa_dst_reg
{
89 int file
; /**< PROGRAM_* from Mesa */
90 int index
; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
91 int writemask
; /**< Bitfield of WRITEMASK_[XYZW] */
93 /** Register index should be offset by the integer in this reg. */
94 ir_to_mesa_src_reg
*reladdr
;
97 extern ir_to_mesa_src_reg ir_to_mesa_undef
;
99 class ir_to_mesa_instruction
: public exec_node
{
102 ir_to_mesa_dst_reg dst_reg
;
103 ir_to_mesa_src_reg src_reg
[3];
104 /** Pointer to the ir source this tree came from for debugging */
106 GLboolean cond_update
;
107 int sampler
; /**< sampler index */
108 int tex_target
; /**< One of TEXTURE_*_INDEX */
109 GLboolean tex_shadow
;
111 class function_entry
*function
; /* Set on OPCODE_CAL or OPCODE_BGNSUB */
114 class variable_storage
: public exec_node
{
116 variable_storage(ir_variable
*var
, int file
, int index
)
117 : file(file
), index(index
), var(var
)
124 ir_variable
*var
; /* variable that maps to this, if any */
127 class function_entry
: public exec_node
{
129 ir_function_signature
*sig
;
132 * identifier of this function signature used by the program.
134 * At the point that Mesa instructions for function calls are
135 * generated, we don't know the address of the first instruction of
136 * the function body. So we make the BranchTarget that is called a
137 * small integer and rewrite them during set_branchtargets().
142 * Pointer to first instruction of the function body.
144 * Set during function body emits after main() is processed.
146 ir_to_mesa_instruction
*bgn_inst
;
149 * Index of the first instruction of the function body in actual
152 * Set after convertion from ir_to_mesa_instruction to prog_instruction.
156 /** Storage for the return value. */
157 ir_to_mesa_src_reg return_reg
;
160 class ir_to_mesa_visitor
: public ir_visitor
{
162 ir_to_mesa_visitor();
164 function_entry
*current_function
;
167 struct gl_program
*prog
;
171 variable_storage
*find_variable_storage(ir_variable
*var
);
173 function_entry
*get_function_signature(ir_function_signature
*sig
);
175 ir_to_mesa_src_reg
get_temp(const glsl_type
*type
);
176 void reladdr_to_temp(ir_instruction
*ir
,
177 ir_to_mesa_src_reg
*reg
, int *num_reladdr
);
179 struct ir_to_mesa_src_reg
src_reg_for_float(float val
);
182 * \name Visit methods
184 * As typical for the visitor pattern, there must be one \c visit method for
185 * each concrete subclass of \c ir_instruction. Virtual base classes within
186 * the hierarchy should not have \c visit methods.
189 virtual void visit(ir_variable
*);
190 virtual void visit(ir_loop
*);
191 virtual void visit(ir_loop_jump
*);
192 virtual void visit(ir_function_signature
*);
193 virtual void visit(ir_function
*);
194 virtual void visit(ir_expression
*);
195 virtual void visit(ir_swizzle
*);
196 virtual void visit(ir_dereference_variable
*);
197 virtual void visit(ir_dereference_array
*);
198 virtual void visit(ir_dereference_record
*);
199 virtual void visit(ir_assignment
*);
200 virtual void visit(ir_constant
*);
201 virtual void visit(ir_call
*);
202 virtual void visit(ir_return
*);
203 virtual void visit(ir_discard
*);
204 virtual void visit(ir_texture
*);
205 virtual void visit(ir_if
*);
208 struct ir_to_mesa_src_reg result
;
210 /** List of variable_storage */
213 /** List of function_entry */
214 exec_list function_signatures
;
215 int next_signature_id
;
217 /** List of ir_to_mesa_instruction */
218 exec_list instructions
;
220 ir_to_mesa_instruction
*ir_to_mesa_emit_op0(ir_instruction
*ir
,
221 enum prog_opcode op
);
223 ir_to_mesa_instruction
*ir_to_mesa_emit_op1(ir_instruction
*ir
,
225 ir_to_mesa_dst_reg dst
,
226 ir_to_mesa_src_reg src0
);
228 ir_to_mesa_instruction
*ir_to_mesa_emit_op2(ir_instruction
*ir
,
230 ir_to_mesa_dst_reg dst
,
231 ir_to_mesa_src_reg src0
,
232 ir_to_mesa_src_reg src1
);
234 ir_to_mesa_instruction
*ir_to_mesa_emit_op3(ir_instruction
*ir
,
236 ir_to_mesa_dst_reg dst
,
237 ir_to_mesa_src_reg src0
,
238 ir_to_mesa_src_reg src1
,
239 ir_to_mesa_src_reg src2
);
241 void ir_to_mesa_emit_scalar_op1(ir_instruction
*ir
,
243 ir_to_mesa_dst_reg dst
,
244 ir_to_mesa_src_reg src0
);
246 void ir_to_mesa_emit_scalar_op2(ir_instruction
*ir
,
248 ir_to_mesa_dst_reg dst
,
249 ir_to_mesa_src_reg src0
,
250 ir_to_mesa_src_reg src1
);
252 GLboolean
try_emit_mad(ir_expression
*ir
,
255 int add_uniform(const char *name
,
256 const glsl_type
*type
,
257 ir_constant
*constant
);
258 void add_aggregate_uniform(ir_instruction
*ir
,
260 const struct glsl_type
*type
,
261 ir_constant
*constant
,
262 struct ir_to_mesa_dst_reg temp
);
265 int sampler_map_size
;
267 void map_sampler(int location
, int sampler
);
268 int get_sampler_number(int location
);
273 ir_to_mesa_src_reg ir_to_mesa_undef
= ir_to_mesa_src_reg(PROGRAM_UNDEFINED
, 0, NULL
);
275 ir_to_mesa_dst_reg ir_to_mesa_undef_dst
= {
276 PROGRAM_UNDEFINED
, 0, SWIZZLE_NOOP
, COND_TR
, NULL
,
279 ir_to_mesa_dst_reg ir_to_mesa_address_reg
= {
280 PROGRAM_ADDRESS
, 0, WRITEMASK_X
, COND_TR
, NULL
283 static int swizzle_for_size(int size
)
285 int size_swizzles
[4] = {
286 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
),
287 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
),
288 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
),
289 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
292 return size_swizzles
[size
- 1];
295 ir_to_mesa_instruction
*
296 ir_to_mesa_visitor::ir_to_mesa_emit_op3(ir_instruction
*ir
,
298 ir_to_mesa_dst_reg dst
,
299 ir_to_mesa_src_reg src0
,
300 ir_to_mesa_src_reg src1
,
301 ir_to_mesa_src_reg src2
)
303 ir_to_mesa_instruction
*inst
= new(mem_ctx
) ir_to_mesa_instruction();
306 /* If we have to do relative addressing, we want to load the ARL
307 * reg directly for one of the regs, and preload the other reladdr
308 * sources into temps.
310 num_reladdr
+= dst
.reladdr
!= NULL
;
311 num_reladdr
+= src0
.reladdr
!= NULL
;
312 num_reladdr
+= src1
.reladdr
!= NULL
;
313 num_reladdr
+= src2
.reladdr
!= NULL
;
315 reladdr_to_temp(ir
, &src2
, &num_reladdr
);
316 reladdr_to_temp(ir
, &src1
, &num_reladdr
);
317 reladdr_to_temp(ir
, &src0
, &num_reladdr
);
320 ir_to_mesa_emit_op1(ir
, OPCODE_ARL
, ir_to_mesa_address_reg
,
325 assert(num_reladdr
== 0);
329 inst
->src_reg
[0] = src0
;
330 inst
->src_reg
[1] = src1
;
331 inst
->src_reg
[2] = src2
;
334 inst
->function
= NULL
;
336 this->instructions
.push_tail(inst
);
342 ir_to_mesa_instruction
*
343 ir_to_mesa_visitor::ir_to_mesa_emit_op2(ir_instruction
*ir
,
345 ir_to_mesa_dst_reg dst
,
346 ir_to_mesa_src_reg src0
,
347 ir_to_mesa_src_reg src1
)
349 return ir_to_mesa_emit_op3(ir
, op
, dst
, src0
, src1
, ir_to_mesa_undef
);
352 ir_to_mesa_instruction
*
353 ir_to_mesa_visitor::ir_to_mesa_emit_op1(ir_instruction
*ir
,
355 ir_to_mesa_dst_reg dst
,
356 ir_to_mesa_src_reg src0
)
358 return ir_to_mesa_emit_op3(ir
, op
, dst
,
359 src0
, ir_to_mesa_undef
, ir_to_mesa_undef
);
362 ir_to_mesa_instruction
*
363 ir_to_mesa_visitor::ir_to_mesa_emit_op0(ir_instruction
*ir
,
366 return ir_to_mesa_emit_op3(ir
, op
, ir_to_mesa_undef_dst
,
373 ir_to_mesa_visitor::map_sampler(int location
, int sampler
)
375 if (this->sampler_map_size
<= location
) {
376 this->sampler_map
= talloc_realloc(this->mem_ctx
, this->sampler_map
,
378 this->sampler_map_size
= location
+ 1;
381 this->sampler_map
[location
] = sampler
;
385 ir_to_mesa_visitor::get_sampler_number(int location
)
387 assert(location
< this->sampler_map_size
);
388 return this->sampler_map
[location
];
391 inline ir_to_mesa_dst_reg
392 ir_to_mesa_dst_reg_from_src(ir_to_mesa_src_reg reg
)
394 ir_to_mesa_dst_reg dst_reg
;
396 dst_reg
.file
= reg
.file
;
397 dst_reg
.index
= reg
.index
;
398 dst_reg
.writemask
= WRITEMASK_XYZW
;
399 dst_reg
.cond_mask
= COND_TR
;
400 dst_reg
.reladdr
= reg
.reladdr
;
405 inline ir_to_mesa_src_reg
406 ir_to_mesa_src_reg_from_dst(ir_to_mesa_dst_reg reg
)
408 return ir_to_mesa_src_reg(reg
.file
, reg
.index
, NULL
);
412 * Emits Mesa scalar opcodes to produce unique answers across channels.
414 * Some Mesa opcodes are scalar-only, like ARB_fp/vp. The src X
415 * channel determines the result across all channels. So to do a vec4
416 * of this operation, we want to emit a scalar per source channel used
417 * to produce dest channels.
420 ir_to_mesa_visitor::ir_to_mesa_emit_scalar_op2(ir_instruction
*ir
,
422 ir_to_mesa_dst_reg dst
,
423 ir_to_mesa_src_reg orig_src0
,
424 ir_to_mesa_src_reg orig_src1
)
427 int done_mask
= ~dst
.writemask
;
429 /* Mesa RCP is a scalar operation splatting results to all channels,
430 * like ARB_fp/vp. So emit as many RCPs as necessary to cover our
433 for (i
= 0; i
< 4; i
++) {
434 GLuint this_mask
= (1 << i
);
435 ir_to_mesa_instruction
*inst
;
436 ir_to_mesa_src_reg src0
= orig_src0
;
437 ir_to_mesa_src_reg src1
= orig_src1
;
439 if (done_mask
& this_mask
)
442 GLuint src0_swiz
= GET_SWZ(src0
.swizzle
, i
);
443 GLuint src1_swiz
= GET_SWZ(src1
.swizzle
, i
);
444 for (j
= i
+ 1; j
< 4; j
++) {
445 if (!(done_mask
& (1 << j
)) &&
446 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
&&
447 GET_SWZ(src1
.swizzle
, j
) == src1_swiz
) {
448 this_mask
|= (1 << j
);
451 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
452 src0_swiz
, src0_swiz
);
453 src1
.swizzle
= MAKE_SWIZZLE4(src1_swiz
, src1_swiz
,
454 src1_swiz
, src1_swiz
);
456 inst
= ir_to_mesa_emit_op2(ir
, op
,
460 inst
->dst_reg
.writemask
= this_mask
;
461 done_mask
|= this_mask
;
466 ir_to_mesa_visitor::ir_to_mesa_emit_scalar_op1(ir_instruction
*ir
,
468 ir_to_mesa_dst_reg dst
,
469 ir_to_mesa_src_reg src0
)
471 ir_to_mesa_src_reg undef
= ir_to_mesa_undef
;
473 undef
.swizzle
= SWIZZLE_XXXX
;
475 ir_to_mesa_emit_scalar_op2(ir
, op
, dst
, src0
, undef
);
478 struct ir_to_mesa_src_reg
479 ir_to_mesa_visitor::src_reg_for_float(float val
)
481 ir_to_mesa_src_reg
src_reg(PROGRAM_CONSTANT
, -1, NULL
);
483 src_reg
.index
= _mesa_add_unnamed_constant(this->prog
->Parameters
,
484 &val
, 1, &src_reg
.swizzle
);
490 type_size(const struct glsl_type
*type
)
495 switch (type
->base_type
) {
498 case GLSL_TYPE_FLOAT
:
500 if (type
->is_matrix()) {
501 return type
->matrix_columns
;
503 /* Regardless of size of vector, it gets a vec4. This is bad
504 * packing for things like floats, but otherwise arrays become a
505 * mess. Hopefully a later pass over the code can pack scalars
506 * down if appropriate.
510 case GLSL_TYPE_ARRAY
:
511 return type_size(type
->fields
.array
) * type
->length
;
512 case GLSL_TYPE_STRUCT
:
514 for (i
= 0; i
< type
->length
; i
++) {
515 size
+= type_size(type
->fields
.structure
[i
].type
);
524 * In the initial pass of codegen, we assign temporary numbers to
525 * intermediate results. (not SSA -- variable assignments will reuse
526 * storage). Actual register allocation for the Mesa VM occurs in a
527 * pass over the Mesa IR later.
530 ir_to_mesa_visitor::get_temp(const glsl_type
*type
)
532 ir_to_mesa_src_reg src_reg
;
536 src_reg
.file
= PROGRAM_TEMPORARY
;
537 src_reg
.index
= next_temp
;
538 src_reg
.reladdr
= NULL
;
539 next_temp
+= type_size(type
);
541 if (type
->is_array() || type
->is_record()) {
542 src_reg
.swizzle
= SWIZZLE_NOOP
;
544 for (i
= 0; i
< type
->vector_elements
; i
++)
547 swizzle
[i
] = type
->vector_elements
- 1;
548 src_reg
.swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1],
549 swizzle
[2], swizzle
[3]);
557 ir_to_mesa_visitor::find_variable_storage(ir_variable
*var
)
560 variable_storage
*entry
;
562 foreach_iter(exec_list_iterator
, iter
, this->variables
) {
563 entry
= (variable_storage
*)iter
.get();
565 if (entry
->var
== var
)
573 ir_to_mesa_visitor::visit(ir_variable
*ir
)
575 if (strcmp(ir
->name
, "gl_FragCoord") == 0) {
576 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
578 fp
->OriginUpperLeft
= ir
->origin_upper_left
;
579 fp
->PixelCenterInteger
= ir
->pixel_center_integer
;
584 ir_to_mesa_visitor::visit(ir_loop
*ir
)
588 assert(!ir
->increment
);
589 assert(!ir
->counter
);
591 ir_to_mesa_emit_op0(NULL
, OPCODE_BGNLOOP
);
592 visit_exec_list(&ir
->body_instructions
, this);
593 ir_to_mesa_emit_op0(NULL
, OPCODE_ENDLOOP
);
597 ir_to_mesa_visitor::visit(ir_loop_jump
*ir
)
600 case ir_loop_jump::jump_break
:
601 ir_to_mesa_emit_op0(NULL
, OPCODE_BRK
);
603 case ir_loop_jump::jump_continue
:
604 ir_to_mesa_emit_op0(NULL
, OPCODE_CONT
);
611 ir_to_mesa_visitor::visit(ir_function_signature
*ir
)
618 ir_to_mesa_visitor::visit(ir_function
*ir
)
620 /* Ignore function bodies other than main() -- we shouldn't see calls to
621 * them since they should all be inlined before we get to ir_to_mesa.
623 if (strcmp(ir
->name
, "main") == 0) {
624 const ir_function_signature
*sig
;
627 sig
= ir
->matching_signature(&empty
);
631 foreach_iter(exec_list_iterator
, iter
, sig
->body
) {
632 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
640 ir_to_mesa_visitor::try_emit_mad(ir_expression
*ir
, int mul_operand
)
642 int nonmul_operand
= 1 - mul_operand
;
643 ir_to_mesa_src_reg a
, b
, c
;
645 ir_expression
*expr
= ir
->operands
[mul_operand
]->as_expression();
646 if (!expr
|| expr
->operation
!= ir_binop_mul
)
649 expr
->operands
[0]->accept(this);
651 expr
->operands
[1]->accept(this);
653 ir
->operands
[nonmul_operand
]->accept(this);
656 this->result
= get_temp(ir
->type
);
657 ir_to_mesa_emit_op3(ir
, OPCODE_MAD
,
658 ir_to_mesa_dst_reg_from_src(this->result
), a
, b
, c
);
664 ir_to_mesa_visitor::reladdr_to_temp(ir_instruction
*ir
,
665 ir_to_mesa_src_reg
*reg
, int *num_reladdr
)
670 ir_to_mesa_emit_op1(ir
, OPCODE_ARL
, ir_to_mesa_address_reg
, *reg
->reladdr
);
672 if (*num_reladdr
!= 1) {
673 ir_to_mesa_src_reg temp
= get_temp(glsl_type::vec4_type
);
675 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
,
676 ir_to_mesa_dst_reg_from_src(temp
), *reg
);
684 ir_to_mesa_visitor::visit(ir_expression
*ir
)
686 unsigned int operand
;
687 struct ir_to_mesa_src_reg op
[2];
688 struct ir_to_mesa_src_reg result_src
;
689 struct ir_to_mesa_dst_reg result_dst
;
690 const glsl_type
*vec4_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
, 4, 1);
691 const glsl_type
*vec3_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
, 3, 1);
692 const glsl_type
*vec2_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
, 2, 1);
694 /* Quick peephole: Emit OPCODE_MAD(a, b, c) instead of ADD(MUL(a, b), c)
696 if (ir
->operation
== ir_binop_add
) {
697 if (try_emit_mad(ir
, 1))
699 if (try_emit_mad(ir
, 0))
703 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
704 this->result
.file
= PROGRAM_UNDEFINED
;
705 ir
->operands
[operand
]->accept(this);
706 if (this->result
.file
== PROGRAM_UNDEFINED
) {
708 printf("Failed to get tree for expression operand:\n");
709 ir
->operands
[operand
]->accept(&v
);
712 op
[operand
] = this->result
;
714 /* Matrix expression operands should have been broken down to vector
715 * operations already.
717 assert(!ir
->operands
[operand
]->type
->is_matrix());
720 this->result
.file
= PROGRAM_UNDEFINED
;
722 /* Storage for our result. Ideally for an assignment we'd be using
723 * the actual storage for the result here, instead.
725 result_src
= get_temp(ir
->type
);
726 /* convenience for the emit functions below. */
727 result_dst
= ir_to_mesa_dst_reg_from_src(result_src
);
728 /* Limit writes to the channels that will be used by result_src later.
729 * This does limit this temp's use as a temporary for multi-instruction
732 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
734 switch (ir
->operation
) {
735 case ir_unop_logic_not
:
736 ir_to_mesa_emit_op2(ir
, OPCODE_SEQ
, result_dst
,
737 op
[0], src_reg_for_float(0.0));
740 op
[0].negate
= ~op
[0].negate
;
744 ir_to_mesa_emit_op1(ir
, OPCODE_ABS
, result_dst
, op
[0]);
747 ir_to_mesa_emit_op1(ir
, OPCODE_SSG
, result_dst
, op
[0]);
750 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_RCP
, result_dst
, op
[0]);
754 ir_to_mesa_emit_scalar_op2(ir
, OPCODE_POW
, result_dst
,
755 src_reg_for_float(M_E
), op
[0]);
758 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_EX2
, result_dst
, op
[0]);
761 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_LOG
, result_dst
, op
[0]);
764 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_LG2
, result_dst
, op
[0]);
767 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_SIN
, result_dst
, op
[0]);
770 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_COS
, result_dst
, op
[0]);
774 ir_to_mesa_emit_op1(ir
, OPCODE_DDX
, result_dst
, op
[0]);
777 ir_to_mesa_emit_op1(ir
, OPCODE_DDY
, result_dst
, op
[0]);
781 ir_to_mesa_emit_op2(ir
, OPCODE_ADD
, result_dst
, op
[0], op
[1]);
784 ir_to_mesa_emit_op2(ir
, OPCODE_SUB
, result_dst
, op
[0], op
[1]);
788 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
, result_dst
, op
[0], op
[1]);
791 assert(!"not reached: should be handled by ir_div_to_mul_rcp");
793 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
797 ir_to_mesa_emit_op2(ir
, OPCODE_SLT
, result_dst
, op
[0], op
[1]);
799 case ir_binop_greater
:
800 ir_to_mesa_emit_op2(ir
, OPCODE_SGT
, result_dst
, op
[0], op
[1]);
802 case ir_binop_lequal
:
803 ir_to_mesa_emit_op2(ir
, OPCODE_SLE
, result_dst
, op
[0], op
[1]);
805 case ir_binop_gequal
:
806 ir_to_mesa_emit_op2(ir
, OPCODE_SGE
, result_dst
, op
[0], op
[1]);
809 ir_to_mesa_emit_op2(ir
, OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
811 case ir_binop_logic_xor
:
812 case ir_binop_nequal
:
813 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
, result_dst
, op
[0], op
[1]);
816 case ir_binop_logic_or
:
817 /* This could be a saturated add and skip the SNE. */
818 ir_to_mesa_emit_op2(ir
, OPCODE_ADD
,
822 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
,
824 result_src
, src_reg_for_float(0.0));
827 case ir_binop_logic_and
:
828 /* the bool args are stored as float 0.0 or 1.0, so "mul" gives us "and". */
829 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
,
835 if (ir
->operands
[0]->type
== vec4_type
) {
836 assert(ir
->operands
[1]->type
== vec4_type
);
837 ir_to_mesa_emit_op2(ir
, OPCODE_DP4
,
840 } else if (ir
->operands
[0]->type
== vec3_type
) {
841 assert(ir
->operands
[1]->type
== vec3_type
);
842 ir_to_mesa_emit_op2(ir
, OPCODE_DP3
,
845 } else if (ir
->operands
[0]->type
== vec2_type
) {
846 assert(ir
->operands
[1]->type
== vec2_type
);
847 ir_to_mesa_emit_op2(ir
, OPCODE_DP2
,
854 ir_to_mesa_emit_op2(ir
, OPCODE_XPD
, result_dst
, op
[0], op
[1]);
858 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_RSQ
, result_dst
, op
[0]);
859 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_RCP
, result_dst
, result_src
);
860 /* For incoming channels < 0, set the result to 0. */
861 ir_to_mesa_emit_op3(ir
, OPCODE_CMP
, result_dst
,
862 op
[0], src_reg_for_float(0.0), result_src
);
865 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_RSQ
, result_dst
, op
[0]);
870 /* Mesa IR lacks types, ints are stored as truncated floats. */
874 ir_to_mesa_emit_op1(ir
, OPCODE_TRUNC
, result_dst
, op
[0]);
878 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
, result_dst
,
879 result_src
, src_reg_for_float(0.0));
882 ir_to_mesa_emit_op1(ir
, OPCODE_TRUNC
, result_dst
, op
[0]);
885 op
[0].negate
= ~op
[0].negate
;
886 ir_to_mesa_emit_op1(ir
, OPCODE_FLR
, result_dst
, op
[0]);
887 result_src
.negate
= ~result_src
.negate
;
890 ir_to_mesa_emit_op1(ir
, OPCODE_FLR
, result_dst
, op
[0]);
893 ir_to_mesa_emit_op1(ir
, OPCODE_FRC
, result_dst
, op
[0]);
897 ir_to_mesa_emit_op2(ir
, OPCODE_MIN
, result_dst
, op
[0], op
[1]);
900 ir_to_mesa_emit_op2(ir
, OPCODE_MAX
, result_dst
, op
[0], op
[1]);
903 ir_to_mesa_emit_scalar_op2(ir
, OPCODE_POW
, result_dst
, op
[0], op
[1]);
906 case ir_unop_bit_not
:
908 case ir_binop_lshift
:
909 case ir_binop_rshift
:
910 case ir_binop_bit_and
:
911 case ir_binop_bit_xor
:
912 case ir_binop_bit_or
:
913 assert(!"GLSL 1.30 features unsupported");
917 this->result
= result_src
;
922 ir_to_mesa_visitor::visit(ir_swizzle
*ir
)
924 ir_to_mesa_src_reg src_reg
;
928 /* Note that this is only swizzles in expressions, not those on the left
929 * hand side of an assignment, which do write masking. See ir_assignment
933 ir
->val
->accept(this);
934 src_reg
= this->result
;
935 assert(src_reg
.file
!= PROGRAM_UNDEFINED
);
937 for (i
= 0; i
< 4; i
++) {
938 if (i
< ir
->type
->vector_elements
) {
941 swizzle
[i
] = GET_SWZ(src_reg
.swizzle
, ir
->mask
.x
);
944 swizzle
[i
] = GET_SWZ(src_reg
.swizzle
, ir
->mask
.y
);
947 swizzle
[i
] = GET_SWZ(src_reg
.swizzle
, ir
->mask
.z
);
950 swizzle
[i
] = GET_SWZ(src_reg
.swizzle
, ir
->mask
.w
);
954 /* If the type is smaller than a vec4, replicate the last
957 swizzle
[i
] = swizzle
[ir
->type
->vector_elements
- 1];
961 src_reg
.swizzle
= MAKE_SWIZZLE4(swizzle
[0],
966 this->result
= src_reg
;
969 static const struct {
972 int tokens
[STATE_LENGTH
];
976 {"gl_DepthRange", "near",
977 {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_XXXX
},
978 {"gl_DepthRange", "far",
979 {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_YYYY
},
980 {"gl_DepthRange", "diff",
981 {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_ZZZZ
},
983 {"gl_ClipPlane", NULL
,
984 {STATE_CLIPPLANE
, 0, 0}, SWIZZLE_XYZW
, true}
987 {STATE_POINT_SIZE
}, SWIZZLE_XXXX
},
988 {"gl_Point", "sizeMin",
989 {STATE_POINT_SIZE
}, SWIZZLE_YYYY
},
990 {"gl_Point", "sizeMax",
991 {STATE_POINT_SIZE
}, SWIZZLE_ZZZZ
},
992 {"gl_Point", "fadeThresholdSize",
993 {STATE_POINT_SIZE
}, SWIZZLE_WWWW
},
994 {"gl_Point", "distanceConstantAttenuation",
995 {STATE_POINT_ATTENUATION
}, SWIZZLE_XXXX
},
996 {"gl_Point", "distanceLinearAttenuation",
997 {STATE_POINT_ATTENUATION
}, SWIZZLE_YYYY
},
998 {"gl_Point", "distanceQuadraticAttenuation",
999 {STATE_POINT_ATTENUATION
}, SWIZZLE_ZZZZ
},
1001 {"gl_FrontMaterial", "emission",
1002 {STATE_MATERIAL
, 0, STATE_EMISSION
}, SWIZZLE_XYZW
},
1003 {"gl_FrontMaterial", "ambient",
1004 {STATE_MATERIAL
, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
1005 {"gl_FrontMaterial", "diffuse",
1006 {STATE_MATERIAL
, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
1007 {"gl_FrontMaterial", "specular",
1008 {STATE_MATERIAL
, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
1009 {"gl_FrontMaterial", "shininess",
1010 {STATE_MATERIAL
, 0, STATE_SHININESS
}, SWIZZLE_XXXX
},
1012 {"gl_BackMaterial", "emission",
1013 {STATE_MATERIAL
, 1, STATE_EMISSION
}, SWIZZLE_XYZW
},
1014 {"gl_BackMaterial", "ambient",
1015 {STATE_MATERIAL
, 1, STATE_AMBIENT
}, SWIZZLE_XYZW
},
1016 {"gl_BackMaterial", "diffuse",
1017 {STATE_MATERIAL
, 1, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
1018 {"gl_BackMaterial", "specular",
1019 {STATE_MATERIAL
, 1, STATE_SPECULAR
}, SWIZZLE_XYZW
},
1020 {"gl_BackMaterial", "shininess",
1021 {STATE_MATERIAL
, 1, STATE_SHININESS
}, SWIZZLE_XXXX
},
1023 {"gl_LightSource", "ambient",
1024 {STATE_LIGHT
, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
, true},
1025 {"gl_LightSource", "diffuse",
1026 {STATE_LIGHT
, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
, true},
1027 {"gl_LightSource", "specular",
1028 {STATE_LIGHT
, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
, true},
1029 {"gl_LightSource", "position",
1030 {STATE_LIGHT
, 0, STATE_POSITION
}, SWIZZLE_XYZW
, true},
1031 {"gl_LightSource", "halfVector",
1032 {STATE_LIGHT
, 0, STATE_HALF_VECTOR
}, SWIZZLE_XYZW
, true},
1033 {"gl_LightSource", "spotDirection",
1034 {STATE_LIGHT
, 0, STATE_SPOT_DIRECTION
}, SWIZZLE_XYZW
, true},
1035 {"gl_LightSource", "spotCosCutoff",
1036 {STATE_LIGHT
, 0, STATE_SPOT_DIRECTION
}, SWIZZLE_WWWW
, true},
1037 {"gl_LightSource", "spotCutoff",
1038 {STATE_LIGHT
, 0, STATE_SPOT_CUTOFF
}, SWIZZLE_XXXX
, true},
1039 {"gl_LightSource", "spotExponent",
1040 {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_WWWW
, true},
1041 {"gl_LightSource", "constantAttenuation",
1042 {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_XXXX
, true},
1043 {"gl_LightSource", "linearAttenuation",
1044 {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_YYYY
, true},
1045 {"gl_LightSource", "quadraticAttenuation",
1046 {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_ZZZZ
, true},
1048 {"gl_LightModel", NULL
,
1049 {STATE_LIGHTMODEL_AMBIENT
, 0}, SWIZZLE_XYZW
},
1051 {"gl_FrontLightModelProduct", NULL
,
1052 {STATE_LIGHTMODEL_SCENECOLOR
, 0}, SWIZZLE_XYZW
},
1053 {"gl_BackLightModelProduct", NULL
,
1054 {STATE_LIGHTMODEL_SCENECOLOR
, 1}, SWIZZLE_XYZW
},
1056 {"gl_FrontLightProduct", "ambient",
1057 {STATE_LIGHTPROD
, 0, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
, true},
1058 {"gl_FrontLightProduct", "diffuse",
1059 {STATE_LIGHTPROD
, 0, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
, true},
1060 {"gl_FrontLightProduct", "specular",
1061 {STATE_LIGHTPROD
, 0, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
, true},
1063 {"gl_BackLightProduct", "ambient",
1064 {STATE_LIGHTPROD
, 0, 1, STATE_AMBIENT
}, SWIZZLE_XYZW
, true},
1065 {"gl_BackLightProduct", "diffuse",
1066 {STATE_LIGHTPROD
, 0, 1, STATE_DIFFUSE
}, SWIZZLE_XYZW
, true},
1067 {"gl_BackLightProduct", "specular",
1068 {STATE_LIGHTPROD
, 0, 1, STATE_SPECULAR
}, SWIZZLE_XYZW
, true},
1070 {"gl_TextureEnvColor", "ambient",
1071 {STATE_TEXENV_COLOR
, 0}, SWIZZLE_XYZW
, true},
1073 {"gl_EyePlaneS", NULL
,
1074 {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_S
}, SWIZZLE_XYZW
, true},
1075 {"gl_EyePlaneT", NULL
,
1076 {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_T
}, SWIZZLE_XYZW
, true},
1077 {"gl_EyePlaneR", NULL
,
1078 {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_R
}, SWIZZLE_XYZW
, true},
1079 {"gl_EyePlaneQ", NULL
,
1080 {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_Q
}, SWIZZLE_XYZW
, true},
1082 {"gl_ObjectPlaneS", NULL
,
1083 {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_S
}, SWIZZLE_XYZW
, true},
1084 {"gl_ObjectPlaneT", NULL
,
1085 {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_T
}, SWIZZLE_XYZW
, true},
1086 {"gl_ObjectPlaneR", NULL
,
1087 {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_R
}, SWIZZLE_XYZW
, true},
1088 {"gl_ObjectPlaneQ", NULL
,
1089 {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_Q
}, SWIZZLE_XYZW
, true},
1092 {STATE_FOG_COLOR
}, SWIZZLE_XYZW
},
1093 {"gl_Fog", "density",
1094 {STATE_FOG_PARAMS
}, SWIZZLE_XXXX
},
1096 {STATE_FOG_PARAMS
}, SWIZZLE_YYYY
},
1098 {STATE_FOG_PARAMS
}, SWIZZLE_ZZZZ
},
1100 {STATE_FOG_PARAMS
}, SWIZZLE_WWWW
},
1103 static ir_to_mesa_src_reg
1104 get_builtin_uniform_reg(struct gl_program
*prog
,
1105 const char *name
, int array_index
, const char *field
)
1108 ir_to_mesa_src_reg src_reg
;
1109 int tokens
[STATE_LENGTH
];
1111 for (i
= 0; i
< Elements(statevars
); i
++) {
1112 if (strcmp(statevars
[i
].name
, name
) != 0)
1114 if (!field
&& statevars
[i
].field
) {
1115 assert(!"FINISHME: whole-structure state var dereference");
1117 if (field
&& strcmp(statevars
[i
].field
, field
) != 0)
1122 if (i
== Elements(statevars
)) {
1123 printf("builtin uniform %s%s%s not found\n",
1126 field
? field
: "");
1130 memcpy(&tokens
, statevars
[i
].tokens
, sizeof(tokens
));
1131 if (statevars
[i
].array_indexed
)
1132 tokens
[1] = array_index
;
1134 src_reg
.file
= PROGRAM_STATE_VAR
;
1135 src_reg
.index
= _mesa_add_state_reference(prog
->Parameters
,
1136 (gl_state_index
*)tokens
);
1137 src_reg
.swizzle
= statevars
[i
].swizzle
;
1139 src_reg
.reladdr
= false;
1145 add_matrix_ref(struct gl_program
*prog
, int *tokens
)
1150 /* Add a ref for each column. It looks like the reason we do
1151 * it this way is that _mesa_add_state_reference doesn't work
1152 * for things that aren't vec4s, so the tokens[2]/tokens[3]
1153 * range has to be equal.
1155 for (i
= 0; i
< 4; i
++) {
1158 int pos
= _mesa_add_state_reference(prog
->Parameters
,
1159 (gl_state_index
*)tokens
);
1163 assert(base_pos
+ i
== pos
);
1169 static variable_storage
*
1170 get_builtin_matrix_ref(void *mem_ctx
, struct gl_program
*prog
, ir_variable
*var
,
1171 ir_rvalue
*array_index
)
1174 * NOTE: The ARB_vertex_program extension specified that matrices get
1175 * loaded in registers in row-major order. With GLSL, we want column-
1176 * major order. So, we need to transpose all matrices here...
1178 static const struct {
1183 { "gl_ModelViewMatrix", STATE_MODELVIEW_MATRIX
, STATE_MATRIX_TRANSPOSE
},
1184 { "gl_ModelViewMatrixInverse", STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVTRANS
},
1185 { "gl_ModelViewMatrixTranspose", STATE_MODELVIEW_MATRIX
, 0 },
1186 { "gl_ModelViewMatrixInverseTranspose", STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVERSE
},
1188 { "gl_ProjectionMatrix", STATE_PROJECTION_MATRIX
, STATE_MATRIX_TRANSPOSE
},
1189 { "gl_ProjectionMatrixInverse", STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVTRANS
},
1190 { "gl_ProjectionMatrixTranspose", STATE_PROJECTION_MATRIX
, 0 },
1191 { "gl_ProjectionMatrixInverseTranspose", STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVERSE
},
1193 { "gl_ModelViewProjectionMatrix", STATE_MVP_MATRIX
, STATE_MATRIX_TRANSPOSE
},
1194 { "gl_ModelViewProjectionMatrixInverse", STATE_MVP_MATRIX
, STATE_MATRIX_INVTRANS
},
1195 { "gl_ModelViewProjectionMatrixTranspose", STATE_MVP_MATRIX
, 0 },
1196 { "gl_ModelViewProjectionMatrixInverseTranspose", STATE_MVP_MATRIX
, STATE_MATRIX_INVERSE
},
1198 { "gl_TextureMatrix", STATE_TEXTURE_MATRIX
, STATE_MATRIX_TRANSPOSE
},
1199 { "gl_TextureMatrixInverse", STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVTRANS
},
1200 { "gl_TextureMatrixTranspose", STATE_TEXTURE_MATRIX
, 0 },
1201 { "gl_TextureMatrixInverseTranspose", STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVERSE
},
1203 { "gl_NormalMatrix", STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVERSE
},
1207 variable_storage
*entry
;
1209 /* C++ gets angry when we try to use an int as a gl_state_index, so we use
1210 * ints for gl_state_index. Make sure they're compatible.
1212 assert(sizeof(gl_state_index
) == sizeof(int));
1214 for (i
= 0; i
< Elements(matrices
); i
++) {
1215 if (strcmp(var
->name
, matrices
[i
].name
) == 0) {
1216 int tokens
[STATE_LENGTH
];
1219 tokens
[0] = matrices
[i
].matrix
;
1220 tokens
[4] = matrices
[i
].modifier
;
1221 if (matrices
[i
].matrix
== STATE_TEXTURE_MATRIX
) {
1222 ir_constant
*index
= array_index
->constant_expression_value();
1224 tokens
[1] = index
->value
.i
[0];
1225 base_pos
= add_matrix_ref(prog
, tokens
);
1227 for (i
= 0; i
< var
->type
->length
; i
++) {
1229 int pos
= add_matrix_ref(prog
, tokens
);
1233 assert(base_pos
+ (int)i
* 4 == pos
);
1237 tokens
[1] = 0; /* unused array index */
1238 base_pos
= add_matrix_ref(prog
, tokens
);
1240 tokens
[4] = matrices
[i
].modifier
;
1242 entry
= new(mem_ctx
) variable_storage(var
,
1254 ir_to_mesa_visitor::add_uniform(const char *name
,
1255 const glsl_type
*type
,
1256 ir_constant
*constant
)
1260 if (type
->is_vector() ||
1261 type
->is_scalar()) {
1262 len
= type
->vector_elements
;
1264 len
= type_size(type
) * 4;
1267 float *values
= NULL
;
1268 if (constant
&& type
->is_array()) {
1269 values
= (float *)malloc(type
->length
* 4 * sizeof(float));
1271 assert(type
->fields
.array
->is_scalar() ||
1272 type
->fields
.array
->is_vector() ||
1273 !"FINISHME: uniform array initializers for non-vector");
1275 for (unsigned int i
= 0; i
< type
->length
; i
++) {
1276 ir_constant
*element
= constant
->array_elements
[i
];
1279 for (c
= 0; c
< type
->fields
.array
->vector_elements
; c
++) {
1280 switch (type
->fields
.array
->base_type
) {
1281 case GLSL_TYPE_FLOAT
:
1282 values
[4 * i
+ c
] = element
->value
.f
[c
];
1285 values
[4 * i
+ c
] = element
->value
.i
[c
];
1287 case GLSL_TYPE_UINT
:
1288 values
[4 * i
+ c
] = element
->value
.u
[c
];
1290 case GLSL_TYPE_BOOL
:
1291 values
[4 * i
+ c
] = element
->value
.b
[c
];
1294 assert(!"not reached");
1298 } else if (constant
) {
1299 values
= (float *)malloc(16 * sizeof(float));
1300 for (unsigned int i
= 0; i
< type
->components(); i
++) {
1301 switch (type
->base_type
) {
1302 case GLSL_TYPE_FLOAT
:
1303 values
[i
] = constant
->value
.f
[i
];
1306 values
[i
] = constant
->value
.i
[i
];
1308 case GLSL_TYPE_UINT
:
1309 values
[i
] = constant
->value
.u
[i
];
1311 case GLSL_TYPE_BOOL
:
1312 values
[i
] = constant
->value
.b
[i
];
1315 assert(!"not reached");
1320 int loc
= _mesa_add_uniform(this->prog
->Parameters
,
1330 /* Recursively add all the members of the aggregate uniform as uniform names
1331 * to Mesa, moving those uniforms to our structured temporary.
1334 ir_to_mesa_visitor::add_aggregate_uniform(ir_instruction
*ir
,
1336 const struct glsl_type
*type
,
1337 ir_constant
*constant
,
1338 struct ir_to_mesa_dst_reg temp
)
1342 if (type
->is_record()) {
1343 void *mem_ctx
= talloc_new(NULL
);
1344 ir_constant
*field_constant
= NULL
;
1347 field_constant
= (ir_constant
*)constant
->components
.get_head();
1349 for (unsigned int i
= 0; i
< type
->length
; i
++) {
1350 const glsl_type
*field_type
= type
->fields
.structure
[i
].type
;
1352 add_aggregate_uniform(ir
,
1353 talloc_asprintf(mem_ctx
, "%s.%s", name
,
1354 type
->fields
.structure
[i
].name
),
1355 field_type
, field_constant
, temp
);
1356 temp
.index
+= type_size(field_type
);
1359 field_constant
= (ir_constant
*)field_constant
->next
;
1362 talloc_free(mem_ctx
);
1367 assert(type
->is_vector() || type
->is_scalar() || !"FINISHME: other types");
1369 loc
= add_uniform(name
, type
, constant
);
1371 ir_to_mesa_src_reg
uniform(PROGRAM_UNIFORM
, loc
, type
);
1373 for (int i
= 0; i
< type_size(type
); i
++) {
1374 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, temp
, uniform
);
1382 ir_to_mesa_visitor::visit(ir_dereference_variable
*ir
)
1384 variable_storage
*entry
= find_variable_storage(ir
->var
);
1388 switch (ir
->var
->mode
) {
1389 case ir_var_uniform
:
1390 entry
= get_builtin_matrix_ref(this->mem_ctx
, this->prog
, ir
->var
,
1395 /* FINISHME: Fix up uniform name for arrays and things */
1396 if (ir
->var
->type
->base_type
== GLSL_TYPE_SAMPLER
) {
1397 /* FINISHME: we whack the location of the var here, which
1398 * is probably not expected. But we need to communicate
1399 * mesa's sampler number to the tex instruction.
1401 int sampler
= _mesa_add_sampler(this->prog
->Parameters
,
1403 ir
->var
->type
->gl_type
);
1404 map_sampler(ir
->var
->location
, sampler
);
1406 entry
= new(mem_ctx
) variable_storage(ir
->var
, PROGRAM_SAMPLER
,
1408 this->variables
.push_tail(entry
);
1412 assert(ir
->var
->type
->gl_type
!= 0 &&
1413 ir
->var
->type
->gl_type
!= GL_INVALID_ENUM
);
1415 /* Oh, the joy of aggregate types in Mesa. Like constants,
1416 * we can only really do vec4s. So, make a temp, chop the
1417 * aggregate up into vec4s, and move those vec4s to the temp.
1419 if (ir
->var
->type
->is_record()) {
1420 ir_to_mesa_src_reg temp
= get_temp(ir
->var
->type
);
1422 entry
= new(mem_ctx
) variable_storage(ir
->var
,
1425 this->variables
.push_tail(entry
);
1427 add_aggregate_uniform(ir
->var
, ir
->var
->name
, ir
->var
->type
,
1428 ir
->var
->constant_value
,
1429 ir_to_mesa_dst_reg_from_src(temp
));
1433 loc
= add_uniform(ir
->var
->name
,
1435 ir
->var
->constant_value
);
1437 /* Always mark the uniform used at this point. If it isn't
1438 * used, dead code elimination should have nuked the decl already.
1440 this->prog
->Parameters
->Parameters
[loc
].Used
= GL_TRUE
;
1442 entry
= new(mem_ctx
) variable_storage(ir
->var
, PROGRAM_UNIFORM
, loc
);
1443 this->variables
.push_tail(entry
);
1448 /* The linker assigns locations for varyings and attributes,
1449 * including deprecated builtins (like gl_Color), user-assign
1450 * generic attributes (glBindVertexLocation), and
1451 * user-defined varyings.
1453 * FINISHME: We would hit this path for function arguments. Fix!
1455 assert(ir
->var
->location
!= -1);
1456 if (ir
->var
->mode
== ir_var_in
||
1457 ir
->var
->mode
== ir_var_inout
) {
1458 entry
= new(mem_ctx
) variable_storage(ir
->var
,
1462 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
&&
1463 ir
->var
->location
>= VERT_ATTRIB_GENERIC0
) {
1464 _mesa_add_attribute(prog
->Attributes
,
1466 type_size(ir
->var
->type
) * 4,
1467 ir
->var
->type
->gl_type
,
1468 ir
->var
->location
- VERT_ATTRIB_GENERIC0
);
1471 entry
= new(mem_ctx
) variable_storage(ir
->var
,
1478 case ir_var_temporary
:
1479 entry
= new(mem_ctx
) variable_storage(ir
->var
, PROGRAM_TEMPORARY
,
1481 this->variables
.push_tail(entry
);
1483 next_temp
+= type_size(ir
->var
->type
);
1488 printf("Failed to make storage for %s\n", ir
->var
->name
);
1493 this->result
= ir_to_mesa_src_reg(entry
->file
, entry
->index
, ir
->var
->type
);
1497 ir_to_mesa_visitor::visit(ir_dereference_array
*ir
)
1499 ir_variable
*var
= ir
->variable_referenced();
1501 ir_to_mesa_src_reg src_reg
;
1502 ir_dereference_variable
*deref_var
= ir
->array
->as_dereference_variable();
1503 int element_size
= type_size(ir
->type
);
1505 index
= ir
->array_index
->constant_expression_value();
1507 if (deref_var
&& strncmp(deref_var
->var
->name
,
1509 strlen("gl_TextureMatrix")) == 0) {
1510 struct variable_storage
*entry
;
1512 entry
= get_builtin_matrix_ref(this->mem_ctx
, this->prog
, deref_var
->var
,
1516 ir_to_mesa_src_reg
src_reg(entry
->file
, entry
->index
, ir
->type
);
1519 src_reg
.reladdr
= NULL
;
1521 ir_to_mesa_src_reg index_reg
= get_temp(glsl_type::float_type
);
1523 ir
->array_index
->accept(this);
1524 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
,
1525 ir_to_mesa_dst_reg_from_src(index_reg
),
1526 this->result
, src_reg_for_float(element_size
));
1528 src_reg
.reladdr
= talloc(mem_ctx
, ir_to_mesa_src_reg
);
1529 memcpy(src_reg
.reladdr
, &index_reg
, sizeof(index_reg
));
1532 this->result
= src_reg
;
1536 if (strncmp(var
->name
, "gl_", 3) == 0 && var
->mode
== ir_var_uniform
&&
1537 !var
->type
->is_matrix()) {
1538 ir_dereference_record
*record
= NULL
;
1539 if (ir
->array
->ir_type
== ir_type_dereference_record
)
1540 record
= (ir_dereference_record
*)ir
->array
;
1542 assert(index
|| !"FINISHME: variable-indexed builtin uniform access");
1544 this->result
= get_builtin_uniform_reg(prog
,
1547 record
? record
->field
: NULL
);
1550 ir
->array
->accept(this);
1551 src_reg
= this->result
;
1554 src_reg
.index
+= index
->value
.i
[0] * element_size
;
1556 ir_to_mesa_src_reg array_base
= this->result
;
1557 /* Variable index array dereference. It eats the "vec4" of the
1558 * base of the array and an index that offsets the Mesa register
1561 ir
->array_index
->accept(this);
1563 ir_to_mesa_src_reg index_reg
;
1565 if (element_size
== 1) {
1566 index_reg
= this->result
;
1568 index_reg
= get_temp(glsl_type::float_type
);
1570 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
,
1571 ir_to_mesa_dst_reg_from_src(index_reg
),
1572 this->result
, src_reg_for_float(element_size
));
1575 src_reg
.reladdr
= talloc(mem_ctx
, ir_to_mesa_src_reg
);
1576 memcpy(src_reg
.reladdr
, &index_reg
, sizeof(index_reg
));
1579 /* If the type is smaller than a vec4, replicate the last channel out. */
1580 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
1581 src_reg
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1583 src_reg
.swizzle
= SWIZZLE_NOOP
;
1585 this->result
= src_reg
;
1589 ir_to_mesa_visitor::visit(ir_dereference_record
*ir
)
1592 const glsl_type
*struct_type
= ir
->record
->type
;
1594 ir_variable
*var
= ir
->record
->variable_referenced();
1596 if (strncmp(var
->name
, "gl_", 3) == 0 && var
->mode
== ir_var_uniform
) {
1599 this->result
= get_builtin_uniform_reg(prog
,
1606 ir
->record
->accept(this);
1608 for (i
= 0; i
< struct_type
->length
; i
++) {
1609 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
1611 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
1613 this->result
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1614 this->result
.index
+= offset
;
1618 * We want to be careful in assignment setup to hit the actual storage
1619 * instead of potentially using a temporary like we might with the
1620 * ir_dereference handler.
1622 * Thanks to ir_swizzle_swizzle, and ir_vec_index_to_swizzle, we
1623 * should only see potentially one variable array index of a vector,
1624 * and one swizzle, before getting to actual vec4 storage. So handle
1625 * those, then go use ir_dereference to handle the rest.
1627 static struct ir_to_mesa_dst_reg
1628 get_assignment_lhs(ir_instruction
*ir
, ir_to_mesa_visitor
*v
,
1629 ir_to_mesa_src_reg
*r
)
1631 struct ir_to_mesa_dst_reg dst_reg
;
1634 ir_dereference_array
*deref_array
= ir
->as_dereference_array();
1635 /* This should have been handled by ir_vec_index_to_cond_assign */
1637 assert(!deref_array
->array
->type
->is_vector());
1640 /* Use the rvalue deref handler for the most part. We'll ignore
1641 * swizzles in it and write swizzles using writemask, though.
1644 dst_reg
= ir_to_mesa_dst_reg_from_src(v
->result
);
1646 if ((swiz
= ir
->as_swizzle())) {
1653 int new_r_swizzle
[4];
1654 int orig_r_swizzle
= r
->swizzle
;
1657 for (i
= 0; i
< 4; i
++) {
1658 new_r_swizzle
[i
] = GET_SWZ(orig_r_swizzle
, 0);
1661 dst_reg
.writemask
= 0;
1662 for (i
= 0; i
< 4; i
++) {
1663 if (i
< swiz
->mask
.num_components
) {
1664 dst_reg
.writemask
|= 1 << swizzles
[i
];
1665 new_r_swizzle
[swizzles
[i
]] = GET_SWZ(orig_r_swizzle
, i
);
1669 r
->swizzle
= MAKE_SWIZZLE4(new_r_swizzle
[0],
1679 ir_to_mesa_visitor::visit(ir_assignment
*ir
)
1681 struct ir_to_mesa_dst_reg l
;
1682 struct ir_to_mesa_src_reg r
;
1685 ir
->rhs
->accept(this);
1688 l
= get_assignment_lhs(ir
->lhs
, this, &r
);
1690 assert(l
.file
!= PROGRAM_UNDEFINED
);
1691 assert(r
.file
!= PROGRAM_UNDEFINED
);
1693 if (ir
->condition
) {
1694 ir_to_mesa_src_reg condition
;
1696 ir
->condition
->accept(this);
1697 condition
= this->result
;
1699 /* We use the OPCODE_CMP (a < 0 ? b : c) for conditional moves,
1700 * and the condition we produced is 0.0 or 1.0. By flipping the
1701 * sign, we can choose which value OPCODE_CMP produces without
1702 * an extra computing the condition.
1704 condition
.negate
= ~condition
.negate
;
1705 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
1706 ir_to_mesa_emit_op3(ir
, OPCODE_CMP
, l
,
1707 condition
, r
, ir_to_mesa_src_reg_from_dst(l
));
1712 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
1713 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, l
, r
);
1722 ir_to_mesa_visitor::visit(ir_constant
*ir
)
1724 ir_to_mesa_src_reg src_reg
;
1725 GLfloat stack_vals
[4];
1726 GLfloat
*values
= stack_vals
;
1729 /* Unfortunately, 4 floats is all we can get into
1730 * _mesa_add_unnamed_constant. So, make a temp to store an
1731 * aggregate constant and move each constant value into it. If we
1732 * get lucky, copy propagation will eliminate the extra moves.
1735 if (ir
->type
->base_type
== GLSL_TYPE_STRUCT
) {
1736 ir_to_mesa_src_reg temp_base
= get_temp(ir
->type
);
1737 ir_to_mesa_dst_reg temp
= ir_to_mesa_dst_reg_from_src(temp_base
);
1739 foreach_iter(exec_list_iterator
, iter
, ir
->components
) {
1740 ir_constant
*field_value
= (ir_constant
*)iter
.get();
1741 int size
= type_size(field_value
->type
);
1745 field_value
->accept(this);
1746 src_reg
= this->result
;
1748 for (i
= 0; i
< (unsigned int)size
; i
++) {
1749 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, temp
, src_reg
);
1755 this->result
= temp_base
;
1759 if (ir
->type
->is_array()) {
1760 ir_to_mesa_src_reg temp_base
= get_temp(ir
->type
);
1761 ir_to_mesa_dst_reg temp
= ir_to_mesa_dst_reg_from_src(temp_base
);
1762 int size
= type_size(ir
->type
->fields
.array
);
1766 for (i
= 0; i
< ir
->type
->length
; i
++) {
1767 ir
->array_elements
[i
]->accept(this);
1768 src_reg
= this->result
;
1769 for (int j
= 0; j
< size
; j
++) {
1770 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, temp
, src_reg
);
1776 this->result
= temp_base
;
1780 if (ir
->type
->is_matrix()) {
1781 ir_to_mesa_src_reg mat
= get_temp(ir
->type
);
1782 ir_to_mesa_dst_reg mat_column
= ir_to_mesa_dst_reg_from_src(mat
);
1784 for (i
= 0; i
< ir
->type
->matrix_columns
; i
++) {
1785 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
1786 values
= &ir
->value
.f
[i
* ir
->type
->vector_elements
];
1788 src_reg
= ir_to_mesa_src_reg(PROGRAM_CONSTANT
, -1, NULL
);
1789 src_reg
.index
= _mesa_add_unnamed_constant(this->prog
->Parameters
,
1791 ir
->type
->vector_elements
,
1793 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, mat_column
, src_reg
);
1801 src_reg
.file
= PROGRAM_CONSTANT
;
1802 switch (ir
->type
->base_type
) {
1803 case GLSL_TYPE_FLOAT
:
1804 values
= &ir
->value
.f
[0];
1806 case GLSL_TYPE_UINT
:
1807 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1808 values
[i
] = ir
->value
.u
[i
];
1812 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1813 values
[i
] = ir
->value
.i
[i
];
1816 case GLSL_TYPE_BOOL
:
1817 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1818 values
[i
] = ir
->value
.b
[i
];
1822 assert(!"Non-float/uint/int/bool constant");
1825 this->result
= ir_to_mesa_src_reg(PROGRAM_CONSTANT
, -1, ir
->type
);
1826 this->result
.index
= _mesa_add_unnamed_constant(this->prog
->Parameters
,
1828 ir
->type
->vector_elements
,
1829 &this->result
.swizzle
);
1833 ir_to_mesa_visitor::get_function_signature(ir_function_signature
*sig
)
1835 function_entry
*entry
;
1837 foreach_iter(exec_list_iterator
, iter
, this->function_signatures
) {
1838 entry
= (function_entry
*)iter
.get();
1840 if (entry
->sig
== sig
)
1844 entry
= talloc(mem_ctx
, function_entry
);
1846 entry
->sig_id
= this->next_signature_id
++;
1847 entry
->bgn_inst
= NULL
;
1849 /* Allocate storage for all the parameters. */
1850 foreach_iter(exec_list_iterator
, iter
, sig
->parameters
) {
1851 ir_variable
*param
= (ir_variable
*)iter
.get();
1852 variable_storage
*storage
;
1854 storage
= find_variable_storage(param
);
1857 storage
= new(mem_ctx
) variable_storage(param
, PROGRAM_TEMPORARY
,
1859 this->variables
.push_tail(storage
);
1861 this->next_temp
+= type_size(param
->type
);
1864 if (!sig
->return_type
->is_void()) {
1865 entry
->return_reg
= get_temp(sig
->return_type
);
1867 entry
->return_reg
= ir_to_mesa_undef
;
1870 this->function_signatures
.push_tail(entry
);
1875 ir_to_mesa_visitor::visit(ir_call
*ir
)
1877 ir_to_mesa_instruction
*call_inst
;
1878 ir_function_signature
*sig
= ir
->get_callee();
1879 function_entry
*entry
= get_function_signature(sig
);
1882 /* Process in parameters. */
1883 exec_list_iterator sig_iter
= sig
->parameters
.iterator();
1884 foreach_iter(exec_list_iterator
, iter
, *ir
) {
1885 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
1886 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
1888 if (param
->mode
== ir_var_in
||
1889 param
->mode
== ir_var_inout
) {
1890 variable_storage
*storage
= find_variable_storage(param
);
1893 param_rval
->accept(this);
1894 ir_to_mesa_src_reg r
= this->result
;
1896 ir_to_mesa_dst_reg l
;
1897 l
.file
= storage
->file
;
1898 l
.index
= storage
->index
;
1900 l
.writemask
= WRITEMASK_XYZW
;
1901 l
.cond_mask
= COND_TR
;
1903 for (i
= 0; i
< type_size(param
->type
); i
++) {
1904 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, l
, r
);
1912 assert(!sig_iter
.has_next());
1914 /* Emit call instruction */
1915 call_inst
= ir_to_mesa_emit_op1(ir
, OPCODE_CAL
,
1916 ir_to_mesa_undef_dst
, ir_to_mesa_undef
);
1917 call_inst
->function
= entry
;
1919 /* Process out parameters. */
1920 sig_iter
= sig
->parameters
.iterator();
1921 foreach_iter(exec_list_iterator
, iter
, *ir
) {
1922 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
1923 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
1925 if (param
->mode
== ir_var_out
||
1926 param
->mode
== ir_var_inout
) {
1927 variable_storage
*storage
= find_variable_storage(param
);
1930 ir_to_mesa_src_reg r
;
1931 r
.file
= storage
->file
;
1932 r
.index
= storage
->index
;
1934 r
.swizzle
= SWIZZLE_NOOP
;
1937 param_rval
->accept(this);
1938 ir_to_mesa_dst_reg l
= ir_to_mesa_dst_reg_from_src(this->result
);
1940 for (i
= 0; i
< type_size(param
->type
); i
++) {
1941 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, l
, r
);
1949 assert(!sig_iter
.has_next());
1951 /* Process return value. */
1952 this->result
= entry
->return_reg
;
1957 ir_to_mesa_visitor::visit(ir_texture
*ir
)
1959 ir_to_mesa_src_reg result_src
, coord
, lod_info
, projector
;
1960 ir_to_mesa_dst_reg result_dst
, coord_dst
;
1961 ir_to_mesa_instruction
*inst
= NULL
;
1962 prog_opcode opcode
= OPCODE_NOP
;
1964 ir
->coordinate
->accept(this);
1966 /* Put our coords in a temp. We'll need to modify them for shadow,
1967 * projection, or LOD, so the only case we'd use it as is is if
1968 * we're doing plain old texturing. Mesa IR optimization should
1969 * handle cleaning up our mess in that case.
1971 coord
= get_temp(glsl_type::vec4_type
);
1972 coord_dst
= ir_to_mesa_dst_reg_from_src(coord
);
1973 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, coord_dst
,
1976 if (ir
->projector
) {
1977 ir
->projector
->accept(this);
1978 projector
= this->result
;
1981 /* Storage for our result. Ideally for an assignment we'd be using
1982 * the actual storage for the result here, instead.
1984 result_src
= get_temp(glsl_type::vec4_type
);
1985 result_dst
= ir_to_mesa_dst_reg_from_src(result_src
);
1989 opcode
= OPCODE_TEX
;
1992 opcode
= OPCODE_TXB
;
1993 ir
->lod_info
.bias
->accept(this);
1994 lod_info
= this->result
;
1997 opcode
= OPCODE_TXL
;
1998 ir
->lod_info
.lod
->accept(this);
1999 lod_info
= this->result
;
2003 assert(!"GLSL 1.30 features unsupported");
2007 if (ir
->projector
) {
2008 if (opcode
== OPCODE_TEX
) {
2009 /* Slot the projector in as the last component of the coord. */
2010 coord_dst
.writemask
= WRITEMASK_W
;
2011 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, coord_dst
, projector
);
2012 coord_dst
.writemask
= WRITEMASK_XYZW
;
2013 opcode
= OPCODE_TXP
;
2015 ir_to_mesa_src_reg coord_w
= coord
;
2016 coord_w
.swizzle
= SWIZZLE_WWWW
;
2018 /* For the other TEX opcodes there's no projective version
2019 * since the last slot is taken up by lod info. Do the
2020 * projective divide now.
2022 coord_dst
.writemask
= WRITEMASK_W
;
2023 ir_to_mesa_emit_op1(ir
, OPCODE_RCP
, coord_dst
, projector
);
2025 coord_dst
.writemask
= WRITEMASK_XYZ
;
2026 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
, coord_dst
, coord
, coord_w
);
2028 coord_dst
.writemask
= WRITEMASK_XYZW
;
2029 coord
.swizzle
= SWIZZLE_XYZW
;
2033 if (ir
->shadow_comparitor
) {
2034 /* Slot the shadow value in as the second to last component of the
2037 ir
->shadow_comparitor
->accept(this);
2038 coord_dst
.writemask
= WRITEMASK_Z
;
2039 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, coord_dst
, this->result
);
2040 coord_dst
.writemask
= WRITEMASK_XYZW
;
2043 if (opcode
== OPCODE_TXL
|| opcode
== OPCODE_TXB
) {
2044 /* Mesa IR stores lod or lod bias in the last channel of the coords. */
2045 coord_dst
.writemask
= WRITEMASK_W
;
2046 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, coord_dst
, lod_info
);
2047 coord_dst
.writemask
= WRITEMASK_XYZW
;
2050 inst
= ir_to_mesa_emit_op1(ir
, opcode
, result_dst
, coord
);
2052 if (ir
->shadow_comparitor
)
2053 inst
->tex_shadow
= GL_TRUE
;
2055 ir_dereference_variable
*sampler
= ir
->sampler
->as_dereference_variable();
2056 assert(sampler
); /* FINISHME: sampler arrays */
2057 /* generate the mapping, remove when we generate storage at
2060 sampler
->accept(this);
2062 inst
->sampler
= get_sampler_number(sampler
->var
->location
);
2064 switch (sampler
->type
->sampler_dimensionality
) {
2065 case GLSL_SAMPLER_DIM_1D
:
2066 inst
->tex_target
= TEXTURE_1D_INDEX
;
2068 case GLSL_SAMPLER_DIM_2D
:
2069 inst
->tex_target
= TEXTURE_2D_INDEX
;
2071 case GLSL_SAMPLER_DIM_3D
:
2072 inst
->tex_target
= TEXTURE_3D_INDEX
;
2074 case GLSL_SAMPLER_DIM_CUBE
:
2075 inst
->tex_target
= TEXTURE_CUBE_INDEX
;
2078 assert(!"FINISHME: other texture targets");
2081 this->result
= result_src
;
2085 ir_to_mesa_visitor::visit(ir_return
*ir
)
2087 assert(current_function
);
2089 if (ir
->get_value()) {
2090 ir_to_mesa_dst_reg l
;
2093 ir
->get_value()->accept(this);
2094 ir_to_mesa_src_reg r
= this->result
;
2096 l
= ir_to_mesa_dst_reg_from_src(current_function
->return_reg
);
2098 for (i
= 0; i
< type_size(current_function
->sig
->return_type
); i
++) {
2099 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, l
, r
);
2105 ir_to_mesa_emit_op0(ir
, OPCODE_RET
);
2109 ir_to_mesa_visitor::visit(ir_discard
*ir
)
2111 assert(ir
->condition
== NULL
); /* FINISHME */
2113 ir_to_mesa_emit_op0(ir
, OPCODE_KIL_NV
);
2117 ir_to_mesa_visitor::visit(ir_if
*ir
)
2119 ir_to_mesa_instruction
*cond_inst
, *if_inst
, *else_inst
= NULL
;
2120 ir_to_mesa_instruction
*prev_inst
;
2122 prev_inst
= (ir_to_mesa_instruction
*)this->instructions
.get_tail();
2124 ir
->condition
->accept(this);
2125 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
2127 if (ctx
->Shader
.EmitCondCodes
) {
2128 cond_inst
= (ir_to_mesa_instruction
*)this->instructions
.get_tail();
2130 /* See if we actually generated any instruction for generating
2131 * the condition. If not, then cook up a move to a temp so we
2132 * have something to set cond_update on.
2134 if (cond_inst
== prev_inst
) {
2135 ir_to_mesa_src_reg temp
= get_temp(glsl_type::bool_type
);
2136 cond_inst
= ir_to_mesa_emit_op1(ir
->condition
, OPCODE_MOV
,
2137 ir_to_mesa_dst_reg_from_src(temp
),
2140 cond_inst
->cond_update
= GL_TRUE
;
2142 if_inst
= ir_to_mesa_emit_op0(ir
->condition
, OPCODE_IF
);
2143 if_inst
->dst_reg
.cond_mask
= COND_NE
;
2145 if_inst
= ir_to_mesa_emit_op1(ir
->condition
,
2146 OPCODE_IF
, ir_to_mesa_undef_dst
,
2150 this->instructions
.push_tail(if_inst
);
2152 visit_exec_list(&ir
->then_instructions
, this);
2154 if (!ir
->else_instructions
.is_empty()) {
2155 else_inst
= ir_to_mesa_emit_op0(ir
->condition
, OPCODE_ELSE
);
2156 visit_exec_list(&ir
->else_instructions
, this);
2159 if_inst
= ir_to_mesa_emit_op1(ir
->condition
, OPCODE_ENDIF
,
2160 ir_to_mesa_undef_dst
, ir_to_mesa_undef
);
2163 ir_to_mesa_visitor::ir_to_mesa_visitor()
2165 result
.file
= PROGRAM_UNDEFINED
;
2167 next_signature_id
= 1;
2169 sampler_map_size
= 0;
2170 current_function
= NULL
;
2173 static struct prog_src_register
2174 mesa_src_reg_from_ir_src_reg(ir_to_mesa_src_reg reg
)
2176 struct prog_src_register mesa_reg
;
2178 mesa_reg
.File
= reg
.file
;
2179 assert(reg
.index
< (1 << INST_INDEX_BITS
) - 1);
2180 mesa_reg
.Index
= reg
.index
;
2181 mesa_reg
.Swizzle
= reg
.swizzle
;
2182 mesa_reg
.RelAddr
= reg
.reladdr
!= NULL
;
2183 mesa_reg
.Negate
= reg
.negate
;
2185 mesa_reg
.HasIndex2
= GL_FALSE
;
2191 set_branchtargets(ir_to_mesa_visitor
*v
,
2192 struct prog_instruction
*mesa_instructions
,
2193 int num_instructions
)
2195 int if_count
= 0, loop_count
= 0;
2196 int *if_stack
, *loop_stack
;
2197 int if_stack_pos
= 0, loop_stack_pos
= 0;
2200 for (i
= 0; i
< num_instructions
; i
++) {
2201 switch (mesa_instructions
[i
].Opcode
) {
2205 case OPCODE_BGNLOOP
:
2210 mesa_instructions
[i
].BranchTarget
= -1;
2217 if_stack
= (int *)calloc(if_count
, sizeof(*if_stack
));
2218 loop_stack
= (int *)calloc(loop_count
, sizeof(*loop_stack
));
2220 for (i
= 0; i
< num_instructions
; i
++) {
2221 switch (mesa_instructions
[i
].Opcode
) {
2223 if_stack
[if_stack_pos
] = i
;
2227 mesa_instructions
[if_stack
[if_stack_pos
- 1]].BranchTarget
= i
;
2228 if_stack
[if_stack_pos
- 1] = i
;
2231 mesa_instructions
[if_stack
[if_stack_pos
- 1]].BranchTarget
= i
;
2234 case OPCODE_BGNLOOP
:
2235 loop_stack
[loop_stack_pos
] = i
;
2238 case OPCODE_ENDLOOP
:
2240 /* Rewrite any breaks/conts at this nesting level (haven't
2241 * already had a BranchTarget assigned) to point to the end
2244 for (j
= loop_stack
[loop_stack_pos
]; j
< i
; j
++) {
2245 if (mesa_instructions
[j
].Opcode
== OPCODE_BRK
||
2246 mesa_instructions
[j
].Opcode
== OPCODE_CONT
) {
2247 if (mesa_instructions
[j
].BranchTarget
== -1) {
2248 mesa_instructions
[j
].BranchTarget
= i
;
2252 /* The loop ends point at each other. */
2253 mesa_instructions
[i
].BranchTarget
= loop_stack
[loop_stack_pos
];
2254 mesa_instructions
[loop_stack
[loop_stack_pos
]].BranchTarget
= i
;
2257 foreach_iter(exec_list_iterator
, iter
, v
->function_signatures
) {
2258 function_entry
*entry
= (function_entry
*)iter
.get();
2260 if (entry
->sig_id
== mesa_instructions
[i
].BranchTarget
) {
2261 mesa_instructions
[i
].BranchTarget
= entry
->inst
;
2275 print_program(struct prog_instruction
*mesa_instructions
,
2276 ir_instruction
**mesa_instruction_annotation
,
2277 int num_instructions
)
2279 ir_instruction
*last_ir
= NULL
;
2283 for (i
= 0; i
< num_instructions
; i
++) {
2284 struct prog_instruction
*mesa_inst
= mesa_instructions
+ i
;
2285 ir_instruction
*ir
= mesa_instruction_annotation
[i
];
2287 fprintf(stdout
, "%3d: ", i
);
2289 if (last_ir
!= ir
&& ir
) {
2292 for (j
= 0; j
< indent
; j
++) {
2293 fprintf(stdout
, " ");
2299 fprintf(stdout
, " "); /* line number spacing. */
2302 indent
= _mesa_fprint_instruction_opt(stdout
, mesa_inst
, indent
,
2303 PROG_PRINT_DEBUG
, NULL
);
2308 mark_input(struct gl_program
*prog
,
2312 prog
->InputsRead
|= BITFIELD64_BIT(index
);
2316 if (index
>= FRAG_ATTRIB_TEX0
&& index
<= FRAG_ATTRIB_TEX7
) {
2317 for (i
= 0; i
< 8; i
++) {
2318 prog
->InputsRead
|= BITFIELD64_BIT(FRAG_ATTRIB_TEX0
+ i
);
2321 assert(!"FINISHME: Mark InputsRead for varying arrays");
2327 mark_output(struct gl_program
*prog
,
2331 prog
->OutputsWritten
|= BITFIELD64_BIT(index
);
2335 if (index
>= VERT_RESULT_TEX0
&& index
<= VERT_RESULT_TEX7
) {
2336 for (i
= 0; i
< 8; i
++) {
2337 prog
->OutputsWritten
|= BITFIELD64_BIT(FRAG_ATTRIB_TEX0
+ i
);
2340 assert(!"FINISHME: Mark OutputsWritten for varying arrays");
2346 count_resources(struct gl_program
*prog
)
2350 prog
->InputsRead
= 0;
2351 prog
->OutputsWritten
= 0;
2352 prog
->SamplersUsed
= 0;
2354 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
2355 struct prog_instruction
*inst
= &prog
->Instructions
[i
];
2358 switch (inst
->DstReg
.File
) {
2359 case PROGRAM_OUTPUT
:
2360 mark_output(prog
, inst
->DstReg
.Index
, inst
->DstReg
.RelAddr
);
2363 mark_input(prog
, inst
->DstReg
.Index
, inst
->DstReg
.RelAddr
);
2369 for (reg
= 0; reg
< _mesa_num_inst_src_regs(inst
->Opcode
); reg
++) {
2370 switch (inst
->SrcReg
[reg
].File
) {
2371 case PROGRAM_OUTPUT
:
2372 mark_output(prog
, inst
->SrcReg
[reg
].Index
,
2373 inst
->SrcReg
[reg
].RelAddr
);
2376 mark_input(prog
, inst
->SrcReg
[reg
].Index
, inst
->SrcReg
[reg
].RelAddr
);
2383 /* Instead of just using the uniform's value to map to a
2384 * sampler, Mesa first allocates a separate number for the
2385 * sampler (_mesa_add_sampler), then we reindex it down to a
2386 * small integer (sampler_map[], SamplersUsed), then that gets
2387 * mapped to the uniform's value, and we get an actual sampler.
2389 if (_mesa_is_tex_instruction(inst
->Opcode
)) {
2390 prog
->SamplerTargets
[inst
->TexSrcUnit
] =
2391 (gl_texture_index
)inst
->TexSrcTarget
;
2392 prog
->SamplersUsed
|= 1 << inst
->TexSrcUnit
;
2393 if (inst
->TexShadow
) {
2394 prog
->ShadowSamplers
|= 1 << inst
->TexSrcUnit
;
2399 _mesa_update_shader_textures_used(prog
);
2402 /* Each stage has some uniforms in its Parameters list. The Uniforms
2403 * list for the linked shader program has a pointer to these uniforms
2404 * in each of the stage's Parameters list, so that their values can be
2405 * updated when a uniform is set.
2408 link_uniforms_to_shared_uniform_list(struct gl_uniform_list
*uniforms
,
2409 struct gl_program
*prog
)
2413 for (i
= 0; i
< prog
->Parameters
->NumParameters
; i
++) {
2414 const struct gl_program_parameter
*p
= prog
->Parameters
->Parameters
+ i
;
2416 if (p
->Type
== PROGRAM_UNIFORM
|| p
->Type
== PROGRAM_SAMPLER
) {
2417 struct gl_uniform
*uniform
=
2418 _mesa_append_uniform(uniforms
, p
->Name
, prog
->Target
, i
);
2420 uniform
->Initialized
= p
->Initialized
;
2426 get_mesa_program(GLcontext
*ctx
, struct gl_shader_program
*shader_program
,
2427 struct gl_shader
*shader
)
2429 void *mem_ctx
= shader_program
;
2430 ir_to_mesa_visitor v
;
2431 struct prog_instruction
*mesa_instructions
, *mesa_inst
;
2432 ir_instruction
**mesa_instruction_annotation
;
2434 struct gl_program
*prog
;
2436 const char *target_string
;
2439 switch (shader
->Type
) {
2440 case GL_VERTEX_SHADER
:
2441 target
= GL_VERTEX_PROGRAM_ARB
;
2442 target_string
= "vertex";
2444 case GL_FRAGMENT_SHADER
:
2445 target
= GL_FRAGMENT_PROGRAM_ARB
;
2446 target_string
= "fragment";
2449 assert(!"should not be reached");
2453 validate_ir_tree(shader
->ir
);
2455 prog
= ctx
->Driver
.NewProgram(ctx
, target
, shader_program
->Name
);
2458 prog
->Parameters
= _mesa_new_parameter_list();
2459 prog
->Varying
= _mesa_new_parameter_list();
2460 prog
->Attributes
= _mesa_new_parameter_list();
2464 v
.mem_ctx
= talloc_new(NULL
);
2466 /* Emit Mesa IR for main(). */
2467 visit_exec_list(shader
->ir
, &v
);
2468 v
.ir_to_mesa_emit_op0(NULL
, OPCODE_END
);
2470 /* Now emit bodies for any functions that were used. */
2472 progress
= GL_FALSE
;
2474 foreach_iter(exec_list_iterator
, iter
, v
.function_signatures
) {
2475 function_entry
*entry
= (function_entry
*)iter
.get();
2477 if (!entry
->bgn_inst
) {
2478 v
.current_function
= entry
;
2480 entry
->bgn_inst
= v
.ir_to_mesa_emit_op0(NULL
, OPCODE_BGNSUB
);
2481 entry
->bgn_inst
->function
= entry
;
2483 visit_exec_list(&entry
->sig
->body
, &v
);
2485 ir_to_mesa_instruction
*last
;
2486 last
= (ir_to_mesa_instruction
*)v
.instructions
.get_tail();
2487 if (last
->op
!= OPCODE_RET
)
2488 v
.ir_to_mesa_emit_op0(NULL
, OPCODE_RET
);
2490 ir_to_mesa_instruction
*end
;
2491 end
= v
.ir_to_mesa_emit_op0(NULL
, OPCODE_ENDSUB
);
2492 end
->function
= entry
;
2499 prog
->NumTemporaries
= v
.next_temp
;
2501 int num_instructions
= 0;
2502 foreach_iter(exec_list_iterator
, iter
, v
.instructions
) {
2507 (struct prog_instruction
*)calloc(num_instructions
,
2508 sizeof(*mesa_instructions
));
2509 mesa_instruction_annotation
= talloc_array(mem_ctx
, ir_instruction
*,
2512 mesa_inst
= mesa_instructions
;
2514 foreach_iter(exec_list_iterator
, iter
, v
.instructions
) {
2515 ir_to_mesa_instruction
*inst
= (ir_to_mesa_instruction
*)iter
.get();
2517 mesa_inst
->Opcode
= inst
->op
;
2518 mesa_inst
->CondUpdate
= inst
->cond_update
;
2519 mesa_inst
->DstReg
.File
= inst
->dst_reg
.file
;
2520 mesa_inst
->DstReg
.Index
= inst
->dst_reg
.index
;
2521 mesa_inst
->DstReg
.CondMask
= inst
->dst_reg
.cond_mask
;
2522 mesa_inst
->DstReg
.WriteMask
= inst
->dst_reg
.writemask
;
2523 mesa_inst
->DstReg
.RelAddr
= inst
->dst_reg
.reladdr
!= NULL
;
2524 mesa_inst
->SrcReg
[0] = mesa_src_reg_from_ir_src_reg(inst
->src_reg
[0]);
2525 mesa_inst
->SrcReg
[1] = mesa_src_reg_from_ir_src_reg(inst
->src_reg
[1]);
2526 mesa_inst
->SrcReg
[2] = mesa_src_reg_from_ir_src_reg(inst
->src_reg
[2]);
2527 mesa_inst
->TexSrcUnit
= inst
->sampler
;
2528 mesa_inst
->TexSrcTarget
= inst
->tex_target
;
2529 mesa_inst
->TexShadow
= inst
->tex_shadow
;
2530 mesa_instruction_annotation
[i
] = inst
->ir
;
2532 if (ctx
->Shader
.EmitNoIfs
&& mesa_inst
->Opcode
== OPCODE_IF
) {
2533 shader_program
->InfoLog
=
2534 talloc_asprintf_append(shader_program
->InfoLog
,
2535 "Couldn't flatten if statement\n");
2536 shader_program
->LinkStatus
= false;
2539 switch (mesa_inst
->Opcode
) {
2541 inst
->function
->inst
= i
;
2542 mesa_inst
->Comment
= strdup(inst
->function
->sig
->function_name());
2545 mesa_inst
->Comment
= strdup(inst
->function
->sig
->function_name());
2548 mesa_inst
->BranchTarget
= inst
->function
->sig_id
; /* rewritten later */
2551 prog
->NumAddressRegs
= 1;
2561 set_branchtargets(&v
, mesa_instructions
, num_instructions
);
2562 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
2563 printf("Mesa %s program:\n", target_string
);
2564 print_program(mesa_instructions
, mesa_instruction_annotation
,
2568 prog
->Instructions
= mesa_instructions
;
2569 prog
->NumInstructions
= num_instructions
;
2571 _mesa_reference_program(ctx
, &shader
->Program
, prog
);
2573 if ((ctx
->Shader
.Flags
& GLSL_NO_OPT
) == 0) {
2574 _mesa_optimize_program(ctx
, prog
);
2583 _mesa_glsl_compile_shader(GLcontext
*ctx
, struct gl_shader
*shader
)
2585 struct _mesa_glsl_parse_state
*state
=
2586 new(shader
) _mesa_glsl_parse_state(ctx
, shader
->Type
, shader
);
2588 const char *source
= shader
->Source
;
2589 state
->error
= preprocess(state
, &source
, &state
->info_log
,
2592 if (!state
->error
) {
2593 _mesa_glsl_lexer_ctor(state
, source
);
2594 _mesa_glsl_parse(state
);
2595 _mesa_glsl_lexer_dtor(state
);
2598 shader
->ir
= new(shader
) exec_list
;
2599 if (!state
->error
&& !state
->translation_unit
.is_empty())
2600 _mesa_ast_to_hir(shader
->ir
, state
);
2602 if (!state
->error
&& !shader
->ir
->is_empty()) {
2603 validate_ir_tree(shader
->ir
);
2606 do_mat_op_to_vec(shader
->ir
);
2607 do_mod_to_fract(shader
->ir
);
2608 do_div_to_mul_rcp(shader
->ir
);
2610 /* Optimization passes */
2615 progress
= do_function_inlining(shader
->ir
) || progress
;
2616 progress
= do_if_simplification(shader
->ir
) || progress
;
2617 progress
= do_copy_propagation(shader
->ir
) || progress
;
2618 progress
= do_dead_code_local(shader
->ir
) || progress
;
2619 progress
= do_dead_code_unlinked(shader
->ir
) || progress
;
2620 progress
= do_tree_grafting(shader
->ir
) || progress
;
2621 progress
= do_constant_variable_unlinked(shader
->ir
) || progress
;
2622 progress
= do_constant_folding(shader
->ir
) || progress
;
2623 progress
= do_algebraic(shader
->ir
) || progress
;
2624 progress
= do_if_return(shader
->ir
) || progress
;
2625 if (ctx
->Shader
.EmitNoIfs
)
2626 progress
= do_if_to_cond_assign(shader
->ir
) || progress
;
2628 progress
= do_vec_index_to_swizzle(shader
->ir
) || progress
;
2629 /* Do this one after the previous to let the easier pass handle
2630 * constant vector indexing.
2632 progress
= do_vec_index_to_cond_assign(shader
->ir
) || progress
;
2634 progress
= do_swizzle_swizzle(shader
->ir
) || progress
;
2637 validate_ir_tree(shader
->ir
);
2640 shader
->symbols
= state
->symbols
;
2642 shader
->CompileStatus
= !state
->error
;
2643 shader
->InfoLog
= state
->info_log
;
2644 shader
->Version
= state
->language_version
;
2645 memcpy(shader
->builtins_to_link
, state
->builtins_to_link
,
2646 sizeof(shader
->builtins_to_link
[0]) * state
->num_builtins_to_link
);
2647 shader
->num_builtins_to_link
= state
->num_builtins_to_link
;
2649 if (ctx
->Shader
.Flags
& GLSL_LOG
) {
2650 _mesa_write_shader_to_file(shader
);
2653 /* Retain any live IR, but trash the rest. */
2654 reparent_ir(shader
->ir
, shader
);
2660 _mesa_glsl_link_shader(GLcontext
*ctx
, struct gl_shader_program
*prog
)
2664 _mesa_clear_shader_program_data(ctx
, prog
);
2666 prog
->LinkStatus
= GL_TRUE
;
2668 for (i
= 0; i
< prog
->NumShaders
; i
++) {
2669 if (!prog
->Shaders
[i
]->CompileStatus
) {
2671 talloc_asprintf_append(prog
->InfoLog
,
2672 "linking with uncompiled shader");
2673 prog
->LinkStatus
= GL_FALSE
;
2677 prog
->Varying
= _mesa_new_parameter_list();
2678 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
, NULL
);
2679 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
, NULL
);
2681 if (prog
->LinkStatus
) {
2684 /* We don't use the linker's uniforms list, and cook up our own at
2687 free(prog
->Uniforms
);
2688 prog
->Uniforms
= _mesa_new_uniform_list();
2691 if (prog
->LinkStatus
) {
2692 for (i
= 0; i
< prog
->_NumLinkedShaders
; i
++) {
2693 struct gl_program
*linked_prog
;
2696 linked_prog
= get_mesa_program(ctx
, prog
,
2697 prog
->_LinkedShaders
[i
]);
2698 count_resources(linked_prog
);
2700 link_uniforms_to_shared_uniform_list(prog
->Uniforms
, linked_prog
);
2702 switch (prog
->_LinkedShaders
[i
]->Type
) {
2703 case GL_VERTEX_SHADER
:
2704 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
,
2705 (struct gl_vertex_program
*)linked_prog
);
2706 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_VERTEX_PROGRAM_ARB
,
2709 case GL_FRAGMENT_SHADER
:
2710 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
,
2711 (struct gl_fragment_program
*)linked_prog
);
2712 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
2717 prog
->LinkStatus
= GL_FALSE
;