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,
34 #include "main/compiler.h"
36 #include "ir_visitor.h"
37 #include "ir_print_visitor.h"
38 #include "ir_expression_flattening.h"
39 #include "glsl_types.h"
40 #include "glsl_parser_extras.h"
41 #include "../glsl/program.h"
42 #include "ir_optimization.h"
46 #include "main/mtypes.h"
47 #include "main/shaderapi.h"
48 #include "main/shaderobj.h"
49 #include "main/uniforms.h"
50 #include "program/hash_table.h"
51 #include "program/prog_instruction.h"
52 #include "program/prog_optimize.h"
53 #include "program/prog_print.h"
54 #include "program/program.h"
55 #include "program/prog_uniform.h"
56 #include "program/prog_parameter.h"
57 #include "program/sampler.h"
60 static int swizzle_for_size(int size
);
63 * This struct is a corresponding struct to Mesa prog_src_register, with
66 typedef struct ir_to_mesa_src_reg
{
67 ir_to_mesa_src_reg(int file
, int index
, const glsl_type
*type
)
71 if (type
&& (type
->is_scalar() || type
->is_vector() || type
->is_matrix()))
72 this->swizzle
= swizzle_for_size(type
->vector_elements
);
74 this->swizzle
= SWIZZLE_XYZW
;
81 this->file
= PROGRAM_UNDEFINED
;
88 int file
; /**< PROGRAM_* from Mesa */
89 int index
; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
90 GLuint swizzle
; /**< SWIZZLE_XYZWONEZERO swizzles from Mesa. */
91 int negate
; /**< NEGATE_XYZW mask from mesa */
92 /** Register index should be offset by the integer in this reg. */
93 ir_to_mesa_src_reg
*reladdr
;
96 typedef struct ir_to_mesa_dst_reg
{
97 int file
; /**< PROGRAM_* from Mesa */
98 int index
; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
99 int writemask
; /**< Bitfield of WRITEMASK_[XYZW] */
101 /** Register index should be offset by the integer in this reg. */
102 ir_to_mesa_src_reg
*reladdr
;
103 } ir_to_mesa_dst_reg
;
105 extern ir_to_mesa_src_reg ir_to_mesa_undef
;
107 class ir_to_mesa_instruction
: public exec_node
{
109 /* Callers of this talloc-based new need not call delete. It's
110 * easier to just talloc_free 'ctx' (or any of its ancestors). */
111 static void* operator new(size_t size
, void *ctx
)
115 node
= talloc_zero_size(ctx
, size
);
116 assert(node
!= NULL
);
122 ir_to_mesa_dst_reg dst_reg
;
123 ir_to_mesa_src_reg src_reg
[3];
124 /** Pointer to the ir source this tree came from for debugging */
126 GLboolean cond_update
;
127 int sampler
; /**< sampler index */
128 int tex_target
; /**< One of TEXTURE_*_INDEX */
129 GLboolean tex_shadow
;
131 class function_entry
*function
; /* Set on OPCODE_CAL or OPCODE_BGNSUB */
134 class variable_storage
: public exec_node
{
136 variable_storage(ir_variable
*var
, int file
, int index
)
137 : file(file
), index(index
), var(var
)
144 ir_variable
*var
; /* variable that maps to this, if any */
147 class function_entry
: public exec_node
{
149 ir_function_signature
*sig
;
152 * identifier of this function signature used by the program.
154 * At the point that Mesa instructions for function calls are
155 * generated, we don't know the address of the first instruction of
156 * the function body. So we make the BranchTarget that is called a
157 * small integer and rewrite them during set_branchtargets().
162 * Pointer to first instruction of the function body.
164 * Set during function body emits after main() is processed.
166 ir_to_mesa_instruction
*bgn_inst
;
169 * Index of the first instruction of the function body in actual
172 * Set after convertion from ir_to_mesa_instruction to prog_instruction.
176 /** Storage for the return value. */
177 ir_to_mesa_src_reg return_reg
;
180 class ir_to_mesa_visitor
: public ir_visitor
{
182 ir_to_mesa_visitor();
183 ~ir_to_mesa_visitor();
185 function_entry
*current_function
;
188 struct gl_program
*prog
;
189 struct gl_shader_program
*shader_program
;
190 struct gl_shader_compiler_options
*options
;
194 variable_storage
*find_variable_storage(ir_variable
*var
);
196 function_entry
*get_function_signature(ir_function_signature
*sig
);
198 ir_to_mesa_src_reg
get_temp(const glsl_type
*type
);
199 void reladdr_to_temp(ir_instruction
*ir
,
200 ir_to_mesa_src_reg
*reg
, int *num_reladdr
);
202 struct ir_to_mesa_src_reg
src_reg_for_float(float val
);
205 * \name Visit methods
207 * As typical for the visitor pattern, there must be one \c visit method for
208 * each concrete subclass of \c ir_instruction. Virtual base classes within
209 * the hierarchy should not have \c visit methods.
212 virtual void visit(ir_variable
*);
213 virtual void visit(ir_loop
*);
214 virtual void visit(ir_loop_jump
*);
215 virtual void visit(ir_function_signature
*);
216 virtual void visit(ir_function
*);
217 virtual void visit(ir_expression
*);
218 virtual void visit(ir_swizzle
*);
219 virtual void visit(ir_dereference_variable
*);
220 virtual void visit(ir_dereference_array
*);
221 virtual void visit(ir_dereference_record
*);
222 virtual void visit(ir_assignment
*);
223 virtual void visit(ir_constant
*);
224 virtual void visit(ir_call
*);
225 virtual void visit(ir_return
*);
226 virtual void visit(ir_discard
*);
227 virtual void visit(ir_texture
*);
228 virtual void visit(ir_if
*);
231 struct ir_to_mesa_src_reg result
;
233 /** List of variable_storage */
236 /** List of function_entry */
237 exec_list function_signatures
;
238 int next_signature_id
;
240 /** List of ir_to_mesa_instruction */
241 exec_list instructions
;
243 ir_to_mesa_instruction
*ir_to_mesa_emit_op0(ir_instruction
*ir
,
244 enum prog_opcode op
);
246 ir_to_mesa_instruction
*ir_to_mesa_emit_op1(ir_instruction
*ir
,
248 ir_to_mesa_dst_reg dst
,
249 ir_to_mesa_src_reg src0
);
251 ir_to_mesa_instruction
*ir_to_mesa_emit_op2(ir_instruction
*ir
,
253 ir_to_mesa_dst_reg dst
,
254 ir_to_mesa_src_reg src0
,
255 ir_to_mesa_src_reg src1
);
257 ir_to_mesa_instruction
*ir_to_mesa_emit_op3(ir_instruction
*ir
,
259 ir_to_mesa_dst_reg dst
,
260 ir_to_mesa_src_reg src0
,
261 ir_to_mesa_src_reg src1
,
262 ir_to_mesa_src_reg src2
);
264 void ir_to_mesa_emit_scalar_op1(ir_instruction
*ir
,
266 ir_to_mesa_dst_reg dst
,
267 ir_to_mesa_src_reg src0
);
269 void ir_to_mesa_emit_scalar_op2(ir_instruction
*ir
,
271 ir_to_mesa_dst_reg dst
,
272 ir_to_mesa_src_reg src0
,
273 ir_to_mesa_src_reg src1
);
275 GLboolean
try_emit_mad(ir_expression
*ir
,
281 ir_to_mesa_src_reg ir_to_mesa_undef
= ir_to_mesa_src_reg(PROGRAM_UNDEFINED
, 0, NULL
);
283 ir_to_mesa_dst_reg ir_to_mesa_undef_dst
= {
284 PROGRAM_UNDEFINED
, 0, SWIZZLE_NOOP
, COND_TR
, NULL
,
287 ir_to_mesa_dst_reg ir_to_mesa_address_reg
= {
288 PROGRAM_ADDRESS
, 0, WRITEMASK_X
, COND_TR
, NULL
291 static void fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...) PRINTFLIKE(2, 3);
293 static void fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...)
297 prog
->InfoLog
= talloc_vasprintf_append(prog
->InfoLog
, fmt
, args
);
300 prog
->LinkStatus
= GL_FALSE
;
303 static int swizzle_for_size(int size
)
305 int size_swizzles
[4] = {
306 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
),
307 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
),
308 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
),
309 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
312 return size_swizzles
[size
- 1];
315 ir_to_mesa_instruction
*
316 ir_to_mesa_visitor::ir_to_mesa_emit_op3(ir_instruction
*ir
,
318 ir_to_mesa_dst_reg dst
,
319 ir_to_mesa_src_reg src0
,
320 ir_to_mesa_src_reg src1
,
321 ir_to_mesa_src_reg src2
)
323 ir_to_mesa_instruction
*inst
= new(mem_ctx
) ir_to_mesa_instruction();
326 /* If we have to do relative addressing, we want to load the ARL
327 * reg directly for one of the regs, and preload the other reladdr
328 * sources into temps.
330 num_reladdr
+= dst
.reladdr
!= NULL
;
331 num_reladdr
+= src0
.reladdr
!= NULL
;
332 num_reladdr
+= src1
.reladdr
!= NULL
;
333 num_reladdr
+= src2
.reladdr
!= NULL
;
335 reladdr_to_temp(ir
, &src2
, &num_reladdr
);
336 reladdr_to_temp(ir
, &src1
, &num_reladdr
);
337 reladdr_to_temp(ir
, &src0
, &num_reladdr
);
340 ir_to_mesa_emit_op1(ir
, OPCODE_ARL
, ir_to_mesa_address_reg
,
345 assert(num_reladdr
== 0);
349 inst
->src_reg
[0] = src0
;
350 inst
->src_reg
[1] = src1
;
351 inst
->src_reg
[2] = src2
;
354 inst
->function
= NULL
;
356 this->instructions
.push_tail(inst
);
362 ir_to_mesa_instruction
*
363 ir_to_mesa_visitor::ir_to_mesa_emit_op2(ir_instruction
*ir
,
365 ir_to_mesa_dst_reg dst
,
366 ir_to_mesa_src_reg src0
,
367 ir_to_mesa_src_reg src1
)
369 return ir_to_mesa_emit_op3(ir
, op
, dst
, src0
, src1
, ir_to_mesa_undef
);
372 ir_to_mesa_instruction
*
373 ir_to_mesa_visitor::ir_to_mesa_emit_op1(ir_instruction
*ir
,
375 ir_to_mesa_dst_reg dst
,
376 ir_to_mesa_src_reg src0
)
378 assert(dst
.writemask
!= 0);
379 return ir_to_mesa_emit_op3(ir
, op
, dst
,
380 src0
, ir_to_mesa_undef
, ir_to_mesa_undef
);
383 ir_to_mesa_instruction
*
384 ir_to_mesa_visitor::ir_to_mesa_emit_op0(ir_instruction
*ir
,
387 return ir_to_mesa_emit_op3(ir
, op
, ir_to_mesa_undef_dst
,
393 inline ir_to_mesa_dst_reg
394 ir_to_mesa_dst_reg_from_src(ir_to_mesa_src_reg reg
)
396 ir_to_mesa_dst_reg dst_reg
;
398 dst_reg
.file
= reg
.file
;
399 dst_reg
.index
= reg
.index
;
400 dst_reg
.writemask
= WRITEMASK_XYZW
;
401 dst_reg
.cond_mask
= COND_TR
;
402 dst_reg
.reladdr
= reg
.reladdr
;
407 inline ir_to_mesa_src_reg
408 ir_to_mesa_src_reg_from_dst(ir_to_mesa_dst_reg reg
)
410 return ir_to_mesa_src_reg(reg
.file
, reg
.index
, NULL
);
414 * Emits Mesa scalar opcodes to produce unique answers across channels.
416 * Some Mesa opcodes are scalar-only, like ARB_fp/vp. The src X
417 * channel determines the result across all channels. So to do a vec4
418 * of this operation, we want to emit a scalar per source channel used
419 * to produce dest channels.
422 ir_to_mesa_visitor::ir_to_mesa_emit_scalar_op2(ir_instruction
*ir
,
424 ir_to_mesa_dst_reg dst
,
425 ir_to_mesa_src_reg orig_src0
,
426 ir_to_mesa_src_reg orig_src1
)
429 int done_mask
= ~dst
.writemask
;
431 /* Mesa RCP is a scalar operation splatting results to all channels,
432 * like ARB_fp/vp. So emit as many RCPs as necessary to cover our
435 for (i
= 0; i
< 4; i
++) {
436 GLuint this_mask
= (1 << i
);
437 ir_to_mesa_instruction
*inst
;
438 ir_to_mesa_src_reg src0
= orig_src0
;
439 ir_to_mesa_src_reg src1
= orig_src1
;
441 if (done_mask
& this_mask
)
444 GLuint src0_swiz
= GET_SWZ(src0
.swizzle
, i
);
445 GLuint src1_swiz
= GET_SWZ(src1
.swizzle
, i
);
446 for (j
= i
+ 1; j
< 4; j
++) {
447 if (!(done_mask
& (1 << j
)) &&
448 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
&&
449 GET_SWZ(src1
.swizzle
, j
) == src1_swiz
) {
450 this_mask
|= (1 << j
);
453 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
454 src0_swiz
, src0_swiz
);
455 src1
.swizzle
= MAKE_SWIZZLE4(src1_swiz
, src1_swiz
,
456 src1_swiz
, src1_swiz
);
458 inst
= ir_to_mesa_emit_op2(ir
, op
,
462 inst
->dst_reg
.writemask
= this_mask
;
463 done_mask
|= this_mask
;
468 ir_to_mesa_visitor::ir_to_mesa_emit_scalar_op1(ir_instruction
*ir
,
470 ir_to_mesa_dst_reg dst
,
471 ir_to_mesa_src_reg src0
)
473 ir_to_mesa_src_reg undef
= ir_to_mesa_undef
;
475 undef
.swizzle
= SWIZZLE_XXXX
;
477 ir_to_mesa_emit_scalar_op2(ir
, op
, dst
, src0
, undef
);
480 struct ir_to_mesa_src_reg
481 ir_to_mesa_visitor::src_reg_for_float(float val
)
483 ir_to_mesa_src_reg
src_reg(PROGRAM_CONSTANT
, -1, NULL
);
485 src_reg
.index
= _mesa_add_unnamed_constant(this->prog
->Parameters
,
486 &val
, 1, &src_reg
.swizzle
);
492 type_size(const struct glsl_type
*type
)
497 switch (type
->base_type
) {
500 case GLSL_TYPE_FLOAT
:
502 if (type
->is_matrix()) {
503 return type
->matrix_columns
;
505 /* Regardless of size of vector, it gets a vec4. This is bad
506 * packing for things like floats, but otherwise arrays become a
507 * mess. Hopefully a later pass over the code can pack scalars
508 * down if appropriate.
512 case GLSL_TYPE_ARRAY
:
513 return type_size(type
->fields
.array
) * type
->length
;
514 case GLSL_TYPE_STRUCT
:
516 for (i
= 0; i
< type
->length
; i
++) {
517 size
+= type_size(type
->fields
.structure
[i
].type
);
520 case GLSL_TYPE_SAMPLER
:
521 /* Samplers take up one slot in UNIFORMS[], but they're baked in
532 * In the initial pass of codegen, we assign temporary numbers to
533 * intermediate results. (not SSA -- variable assignments will reuse
534 * storage). Actual register allocation for the Mesa VM occurs in a
535 * pass over the Mesa IR later.
538 ir_to_mesa_visitor::get_temp(const glsl_type
*type
)
540 ir_to_mesa_src_reg src_reg
;
544 src_reg
.file
= PROGRAM_TEMPORARY
;
545 src_reg
.index
= next_temp
;
546 src_reg
.reladdr
= NULL
;
547 next_temp
+= type_size(type
);
549 if (type
->is_array() || type
->is_record()) {
550 src_reg
.swizzle
= SWIZZLE_NOOP
;
552 for (i
= 0; i
< type
->vector_elements
; i
++)
555 swizzle
[i
] = type
->vector_elements
- 1;
556 src_reg
.swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1],
557 swizzle
[2], swizzle
[3]);
565 ir_to_mesa_visitor::find_variable_storage(ir_variable
*var
)
568 variable_storage
*entry
;
570 foreach_iter(exec_list_iterator
, iter
, this->variables
) {
571 entry
= (variable_storage
*)iter
.get();
573 if (entry
->var
== var
)
581 ir_to_mesa_visitor::visit(ir_variable
*ir
)
583 if (strcmp(ir
->name
, "gl_FragCoord") == 0) {
584 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
586 fp
->OriginUpperLeft
= ir
->origin_upper_left
;
587 fp
->PixelCenterInteger
= ir
->pixel_center_integer
;
590 if (ir
->mode
== ir_var_uniform
&& strncmp(ir
->name
, "gl_", 3) == 0) {
592 const struct gl_builtin_uniform_desc
*statevar
;
594 for (i
= 0; _mesa_builtin_uniform_desc
[i
].name
; i
++) {
595 if (strcmp(ir
->name
, _mesa_builtin_uniform_desc
[i
].name
) == 0)
599 if (!_mesa_builtin_uniform_desc
[i
].name
) {
600 fail_link(this->shader_program
,
601 "Failed to find builtin uniform `%s'\n", ir
->name
);
605 statevar
= &_mesa_builtin_uniform_desc
[i
];
608 if (ir
->type
->is_array()) {
609 array_count
= ir
->type
->length
;
614 /* Check if this statevar's setup in the STATE file exactly
615 * matches how we'll want to reference it as a
616 * struct/array/whatever. If not, then we need to move it into
617 * temporary storage and hope that it'll get copy-propagated
620 for (i
= 0; i
< statevar
->num_elements
; i
++) {
621 if (statevar
->elements
[i
].swizzle
!= SWIZZLE_XYZW
) {
626 struct variable_storage
*storage
;
627 ir_to_mesa_dst_reg dst
;
628 if (i
== statevar
->num_elements
) {
629 /* We'll set the index later. */
630 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_STATE_VAR
, -1);
631 this->variables
.push_tail(storage
);
633 dst
= ir_to_mesa_undef_dst
;
635 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_TEMPORARY
,
637 this->variables
.push_tail(storage
);
638 this->next_temp
+= type_size(ir
->type
);
640 dst
= ir_to_mesa_dst_reg_from_src(ir_to_mesa_src_reg(PROGRAM_TEMPORARY
,
646 for (int a
= 0; a
< array_count
; a
++) {
647 for (unsigned int i
= 0; i
< statevar
->num_elements
; i
++) {
648 struct gl_builtin_uniform_element
*element
= &statevar
->elements
[i
];
649 int tokens
[STATE_LENGTH
];
651 memcpy(tokens
, element
->tokens
, sizeof(element
->tokens
));
652 if (ir
->type
->is_array()) {
656 int index
= _mesa_add_state_reference(this->prog
->Parameters
,
657 (gl_state_index
*)tokens
);
659 if (storage
->file
== PROGRAM_STATE_VAR
) {
660 if (storage
->index
== -1) {
661 storage
->index
= index
;
664 (int)(storage
->index
+ a
* statevar
->num_elements
+ i
));
667 ir_to_mesa_src_reg
src(PROGRAM_STATE_VAR
, index
, NULL
);
668 src
.swizzle
= element
->swizzle
;
669 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, dst
, src
);
670 /* even a float takes up a whole vec4 reg in a struct/array. */
675 if (storage
->file
== PROGRAM_TEMPORARY
&&
676 dst
.index
!= storage
->index
+ type_size(ir
->type
)) {
677 fail_link(this->shader_program
,
678 "failed to load builtin uniform `%s' (%d/%d regs loaded)\n",
679 ir
->name
, dst
.index
- storage
->index
,
680 type_size(ir
->type
));
686 ir_to_mesa_visitor::visit(ir_loop
*ir
)
688 ir_dereference_variable
*counter
= NULL
;
690 if (ir
->counter
!= NULL
)
691 counter
= new(ir
) ir_dereference_variable(ir
->counter
);
693 if (ir
->from
!= NULL
) {
694 assert(ir
->counter
!= NULL
);
696 ir_assignment
*a
= new(ir
) ir_assignment(counter
, ir
->from
, NULL
);
702 ir_to_mesa_emit_op0(NULL
, OPCODE_BGNLOOP
);
706 new(ir
) ir_expression(ir
->cmp
, glsl_type::bool_type
,
708 ir_if
*if_stmt
= new(ir
) ir_if(e
);
710 ir_loop_jump
*brk
= new(ir
) ir_loop_jump(ir_loop_jump::jump_break
);
712 if_stmt
->then_instructions
.push_tail(brk
);
714 if_stmt
->accept(this);
721 visit_exec_list(&ir
->body_instructions
, this);
725 new(ir
) ir_expression(ir_binop_add
, counter
->type
,
726 counter
, ir
->increment
);
728 ir_assignment
*a
= new(ir
) ir_assignment(counter
, e
, NULL
);
735 ir_to_mesa_emit_op0(NULL
, OPCODE_ENDLOOP
);
739 ir_to_mesa_visitor::visit(ir_loop_jump
*ir
)
742 case ir_loop_jump::jump_break
:
743 ir_to_mesa_emit_op0(NULL
, OPCODE_BRK
);
745 case ir_loop_jump::jump_continue
:
746 ir_to_mesa_emit_op0(NULL
, OPCODE_CONT
);
753 ir_to_mesa_visitor::visit(ir_function_signature
*ir
)
760 ir_to_mesa_visitor::visit(ir_function
*ir
)
762 /* Ignore function bodies other than main() -- we shouldn't see calls to
763 * them since they should all be inlined before we get to ir_to_mesa.
765 if (strcmp(ir
->name
, "main") == 0) {
766 const ir_function_signature
*sig
;
769 sig
= ir
->matching_signature(&empty
);
773 foreach_iter(exec_list_iterator
, iter
, sig
->body
) {
774 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
782 ir_to_mesa_visitor::try_emit_mad(ir_expression
*ir
, int mul_operand
)
784 int nonmul_operand
= 1 - mul_operand
;
785 ir_to_mesa_src_reg a
, b
, c
;
787 ir_expression
*expr
= ir
->operands
[mul_operand
]->as_expression();
788 if (!expr
|| expr
->operation
!= ir_binop_mul
)
791 expr
->operands
[0]->accept(this);
793 expr
->operands
[1]->accept(this);
795 ir
->operands
[nonmul_operand
]->accept(this);
798 this->result
= get_temp(ir
->type
);
799 ir_to_mesa_emit_op3(ir
, OPCODE_MAD
,
800 ir_to_mesa_dst_reg_from_src(this->result
), a
, b
, c
);
806 ir_to_mesa_visitor::reladdr_to_temp(ir_instruction
*ir
,
807 ir_to_mesa_src_reg
*reg
, int *num_reladdr
)
812 ir_to_mesa_emit_op1(ir
, OPCODE_ARL
, ir_to_mesa_address_reg
, *reg
->reladdr
);
814 if (*num_reladdr
!= 1) {
815 ir_to_mesa_src_reg temp
= get_temp(glsl_type::vec4_type
);
817 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
,
818 ir_to_mesa_dst_reg_from_src(temp
), *reg
);
826 ir_to_mesa_visitor::visit(ir_expression
*ir
)
828 unsigned int operand
;
829 struct ir_to_mesa_src_reg op
[2];
830 struct ir_to_mesa_src_reg result_src
;
831 struct ir_to_mesa_dst_reg result_dst
;
832 const glsl_type
*vec4_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
, 4, 1);
833 const glsl_type
*vec3_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
, 3, 1);
834 const glsl_type
*vec2_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
, 2, 1);
836 /* Quick peephole: Emit OPCODE_MAD(a, b, c) instead of ADD(MUL(a, b), c)
838 if (ir
->operation
== ir_binop_add
) {
839 if (try_emit_mad(ir
, 1))
841 if (try_emit_mad(ir
, 0))
845 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
846 this->result
.file
= PROGRAM_UNDEFINED
;
847 ir
->operands
[operand
]->accept(this);
848 if (this->result
.file
== PROGRAM_UNDEFINED
) {
850 printf("Failed to get tree for expression operand:\n");
851 ir
->operands
[operand
]->accept(&v
);
854 op
[operand
] = this->result
;
856 /* Matrix expression operands should have been broken down to vector
857 * operations already.
859 assert(!ir
->operands
[operand
]->type
->is_matrix());
862 int vector_elements
= ir
->operands
[0]->type
->vector_elements
;
863 if (ir
->operands
[1]) {
864 vector_elements
= MAX2(vector_elements
,
865 ir
->operands
[1]->type
->vector_elements
);
868 this->result
.file
= PROGRAM_UNDEFINED
;
870 /* Storage for our result. Ideally for an assignment we'd be using
871 * the actual storage for the result here, instead.
873 result_src
= get_temp(ir
->type
);
874 /* convenience for the emit functions below. */
875 result_dst
= ir_to_mesa_dst_reg_from_src(result_src
);
876 /* Limit writes to the channels that will be used by result_src later.
877 * This does limit this temp's use as a temporary for multi-instruction
880 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
882 switch (ir
->operation
) {
883 case ir_unop_logic_not
:
884 ir_to_mesa_emit_op2(ir
, OPCODE_SEQ
, result_dst
,
885 op
[0], src_reg_for_float(0.0));
888 op
[0].negate
= ~op
[0].negate
;
892 ir_to_mesa_emit_op1(ir
, OPCODE_ABS
, result_dst
, op
[0]);
895 ir_to_mesa_emit_op1(ir
, OPCODE_SSG
, result_dst
, op
[0]);
898 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_RCP
, result_dst
, op
[0]);
902 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_EX2
, result_dst
, op
[0]);
906 assert(!"not reached: should be handled by ir_explog_to_explog2");
909 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_LG2
, result_dst
, op
[0]);
912 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_SIN
, result_dst
, op
[0]);
915 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_COS
, result_dst
, op
[0]);
919 ir_to_mesa_emit_op1(ir
, OPCODE_DDX
, result_dst
, op
[0]);
922 ir_to_mesa_emit_op1(ir
, OPCODE_DDY
, result_dst
, op
[0]);
925 case ir_unop_noise
: {
926 const enum prog_opcode opcode
=
927 prog_opcode(OPCODE_NOISE1
928 + (ir
->operands
[0]->type
->vector_elements
) - 1);
929 assert((opcode
>= OPCODE_NOISE1
) && (opcode
<= OPCODE_NOISE4
));
931 ir_to_mesa_emit_op1(ir
, opcode
, result_dst
, op
[0]);
936 ir_to_mesa_emit_op2(ir
, OPCODE_ADD
, result_dst
, op
[0], op
[1]);
939 ir_to_mesa_emit_op2(ir
, OPCODE_SUB
, result_dst
, op
[0], op
[1]);
943 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
, result_dst
, op
[0], op
[1]);
946 assert(!"not reached: should be handled by ir_div_to_mul_rcp");
948 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
952 ir_to_mesa_emit_op2(ir
, OPCODE_SLT
, result_dst
, op
[0], op
[1]);
954 case ir_binop_greater
:
955 ir_to_mesa_emit_op2(ir
, OPCODE_SGT
, result_dst
, op
[0], op
[1]);
957 case ir_binop_lequal
:
958 ir_to_mesa_emit_op2(ir
, OPCODE_SLE
, result_dst
, op
[0], op
[1]);
960 case ir_binop_gequal
:
961 ir_to_mesa_emit_op2(ir
, OPCODE_SGE
, result_dst
, op
[0], op
[1]);
964 ir_to_mesa_emit_op2(ir
, OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
966 case ir_binop_nequal
:
967 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
, result_dst
, op
[0], op
[1]);
969 case ir_binop_all_equal
:
970 /* "==" operator producing a scalar boolean. */
971 if (ir
->operands
[0]->type
->is_vector() ||
972 ir
->operands
[1]->type
->is_vector()) {
973 ir_to_mesa_src_reg temp
= get_temp(glsl_type::vec4_type
);
974 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
,
975 ir_to_mesa_dst_reg_from_src(temp
), op
[0], op
[1]);
976 if (vector_elements
== 4)
977 ir_to_mesa_emit_op2(ir
, OPCODE_DP4
, result_dst
, temp
, temp
);
978 else if (vector_elements
== 3)
979 ir_to_mesa_emit_op2(ir
, OPCODE_DP3
, result_dst
, temp
, temp
);
981 ir_to_mesa_emit_op2(ir
, OPCODE_DP2
, result_dst
, temp
, temp
);
982 ir_to_mesa_emit_op2(ir
, OPCODE_SEQ
,
983 result_dst
, result_src
, src_reg_for_float(0.0));
985 ir_to_mesa_emit_op2(ir
, OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
988 case ir_binop_any_nequal
:
989 /* "!=" operator producing a scalar boolean. */
990 if (ir
->operands
[0]->type
->is_vector() ||
991 ir
->operands
[1]->type
->is_vector()) {
992 ir_to_mesa_src_reg temp
= get_temp(glsl_type::vec4_type
);
993 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
,
994 ir_to_mesa_dst_reg_from_src(temp
), op
[0], op
[1]);
995 if (vector_elements
== 4)
996 ir_to_mesa_emit_op2(ir
, OPCODE_DP4
, result_dst
, temp
, temp
);
997 else if (vector_elements
== 3)
998 ir_to_mesa_emit_op2(ir
, OPCODE_DP3
, result_dst
, temp
, temp
);
1000 ir_to_mesa_emit_op2(ir
, OPCODE_DP2
, result_dst
, temp
, temp
);
1001 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
,
1002 result_dst
, result_src
, src_reg_for_float(0.0));
1004 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1009 switch (ir
->operands
[0]->type
->vector_elements
) {
1011 ir_to_mesa_emit_op2(ir
, OPCODE_DP4
, result_dst
, op
[0], op
[0]);
1014 ir_to_mesa_emit_op2(ir
, OPCODE_DP3
, result_dst
, op
[0], op
[0]);
1017 ir_to_mesa_emit_op2(ir
, OPCODE_DP2
, result_dst
, op
[0], op
[0]);
1020 assert(!"unreached: ir_unop_any of non-bvec");
1023 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
,
1024 result_dst
, result_src
, src_reg_for_float(0.0));
1027 case ir_binop_logic_xor
:
1028 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1031 case ir_binop_logic_or
:
1032 /* This could be a saturated add and skip the SNE. */
1033 ir_to_mesa_emit_op2(ir
, OPCODE_ADD
,
1037 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
,
1039 result_src
, src_reg_for_float(0.0));
1042 case ir_binop_logic_and
:
1043 /* the bool args are stored as float 0.0 or 1.0, so "mul" gives us "and". */
1044 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
,
1050 if (ir
->operands
[0]->type
== vec4_type
) {
1051 assert(ir
->operands
[1]->type
== vec4_type
);
1052 ir_to_mesa_emit_op2(ir
, OPCODE_DP4
,
1055 } else if (ir
->operands
[0]->type
== vec3_type
) {
1056 assert(ir
->operands
[1]->type
== vec3_type
);
1057 ir_to_mesa_emit_op2(ir
, OPCODE_DP3
,
1060 } else if (ir
->operands
[0]->type
== vec2_type
) {
1061 assert(ir
->operands
[1]->type
== vec2_type
);
1062 ir_to_mesa_emit_op2(ir
, OPCODE_DP2
,
1068 case ir_binop_cross
:
1069 ir_to_mesa_emit_op2(ir
, OPCODE_XPD
, result_dst
, op
[0], op
[1]);
1073 /* sqrt(x) = x * rsq(x). */
1074 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_RSQ
, result_dst
, op
[0]);
1075 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
, result_dst
, result_src
, op
[0]);
1076 /* For incoming channels <= 0, set the result to 0. */
1077 op
[0].negate
= ~op
[0].negate
;
1078 ir_to_mesa_emit_op3(ir
, OPCODE_CMP
, result_dst
,
1079 op
[0], result_src
, src_reg_for_float(0.0));
1082 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_RSQ
, result_dst
, op
[0]);
1087 /* Mesa IR lacks types, ints are stored as truncated floats. */
1091 ir_to_mesa_emit_op1(ir
, OPCODE_TRUNC
, result_dst
, op
[0]);
1095 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
, result_dst
,
1096 op
[0], src_reg_for_float(0.0));
1099 ir_to_mesa_emit_op1(ir
, OPCODE_TRUNC
, result_dst
, op
[0]);
1102 op
[0].negate
= ~op
[0].negate
;
1103 ir_to_mesa_emit_op1(ir
, OPCODE_FLR
, result_dst
, op
[0]);
1104 result_src
.negate
= ~result_src
.negate
;
1107 ir_to_mesa_emit_op1(ir
, OPCODE_FLR
, result_dst
, op
[0]);
1110 ir_to_mesa_emit_op1(ir
, OPCODE_FRC
, result_dst
, op
[0]);
1114 ir_to_mesa_emit_op2(ir
, OPCODE_MIN
, result_dst
, op
[0], op
[1]);
1117 ir_to_mesa_emit_op2(ir
, OPCODE_MAX
, result_dst
, op
[0], op
[1]);
1120 ir_to_mesa_emit_scalar_op2(ir
, OPCODE_POW
, result_dst
, op
[0], op
[1]);
1123 case ir_unop_bit_not
:
1125 case ir_binop_lshift
:
1126 case ir_binop_rshift
:
1127 case ir_binop_bit_and
:
1128 case ir_binop_bit_xor
:
1129 case ir_binop_bit_or
:
1130 assert(!"GLSL 1.30 features unsupported");
1134 this->result
= result_src
;
1139 ir_to_mesa_visitor::visit(ir_swizzle
*ir
)
1141 ir_to_mesa_src_reg src_reg
;
1145 /* Note that this is only swizzles in expressions, not those on the left
1146 * hand side of an assignment, which do write masking. See ir_assignment
1150 ir
->val
->accept(this);
1151 src_reg
= this->result
;
1152 assert(src_reg
.file
!= PROGRAM_UNDEFINED
);
1154 for (i
= 0; i
< 4; i
++) {
1155 if (i
< ir
->type
->vector_elements
) {
1158 swizzle
[i
] = GET_SWZ(src_reg
.swizzle
, ir
->mask
.x
);
1161 swizzle
[i
] = GET_SWZ(src_reg
.swizzle
, ir
->mask
.y
);
1164 swizzle
[i
] = GET_SWZ(src_reg
.swizzle
, ir
->mask
.z
);
1167 swizzle
[i
] = GET_SWZ(src_reg
.swizzle
, ir
->mask
.w
);
1171 /* If the type is smaller than a vec4, replicate the last
1174 swizzle
[i
] = swizzle
[ir
->type
->vector_elements
- 1];
1178 src_reg
.swizzle
= MAKE_SWIZZLE4(swizzle
[0],
1183 this->result
= src_reg
;
1187 ir_to_mesa_visitor::visit(ir_dereference_variable
*ir
)
1189 variable_storage
*entry
= find_variable_storage(ir
->var
);
1192 switch (ir
->var
->mode
) {
1193 case ir_var_uniform
:
1194 entry
= new(mem_ctx
) variable_storage(ir
->var
, PROGRAM_UNIFORM
,
1196 this->variables
.push_tail(entry
);
1201 /* The linker assigns locations for varyings and attributes,
1202 * including deprecated builtins (like gl_Color), user-assign
1203 * generic attributes (glBindVertexLocation), and
1204 * user-defined varyings.
1206 * FINISHME: We would hit this path for function arguments. Fix!
1208 assert(ir
->var
->location
!= -1);
1209 if (ir
->var
->mode
== ir_var_in
||
1210 ir
->var
->mode
== ir_var_inout
) {
1211 entry
= new(mem_ctx
) variable_storage(ir
->var
,
1215 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
&&
1216 ir
->var
->location
>= VERT_ATTRIB_GENERIC0
) {
1217 _mesa_add_attribute(prog
->Attributes
,
1219 _mesa_sizeof_glsl_type(ir
->var
->type
->gl_type
),
1220 ir
->var
->type
->gl_type
,
1221 ir
->var
->location
- VERT_ATTRIB_GENERIC0
);
1224 entry
= new(mem_ctx
) variable_storage(ir
->var
,
1231 case ir_var_temporary
:
1232 entry
= new(mem_ctx
) variable_storage(ir
->var
, PROGRAM_TEMPORARY
,
1234 this->variables
.push_tail(entry
);
1236 next_temp
+= type_size(ir
->var
->type
);
1241 printf("Failed to make storage for %s\n", ir
->var
->name
);
1246 this->result
= ir_to_mesa_src_reg(entry
->file
, entry
->index
, ir
->var
->type
);
1250 ir_to_mesa_visitor::visit(ir_dereference_array
*ir
)
1253 ir_to_mesa_src_reg src_reg
;
1254 int element_size
= type_size(ir
->type
);
1256 index
= ir
->array_index
->constant_expression_value();
1258 ir
->array
->accept(this);
1259 src_reg
= this->result
;
1262 src_reg
.index
+= index
->value
.i
[0] * element_size
;
1264 ir_to_mesa_src_reg array_base
= this->result
;
1265 /* Variable index array dereference. It eats the "vec4" of the
1266 * base of the array and an index that offsets the Mesa register
1269 ir
->array_index
->accept(this);
1271 ir_to_mesa_src_reg index_reg
;
1273 if (element_size
== 1) {
1274 index_reg
= this->result
;
1276 index_reg
= get_temp(glsl_type::float_type
);
1278 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
,
1279 ir_to_mesa_dst_reg_from_src(index_reg
),
1280 this->result
, src_reg_for_float(element_size
));
1283 src_reg
.reladdr
= talloc(mem_ctx
, ir_to_mesa_src_reg
);
1284 memcpy(src_reg
.reladdr
, &index_reg
, sizeof(index_reg
));
1287 /* If the type is smaller than a vec4, replicate the last channel out. */
1288 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
1289 src_reg
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1291 src_reg
.swizzle
= SWIZZLE_NOOP
;
1293 this->result
= src_reg
;
1297 ir_to_mesa_visitor::visit(ir_dereference_record
*ir
)
1300 const glsl_type
*struct_type
= ir
->record
->type
;
1303 ir
->record
->accept(this);
1305 for (i
= 0; i
< struct_type
->length
; i
++) {
1306 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
1308 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
1310 this->result
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1311 this->result
.index
+= offset
;
1315 * We want to be careful in assignment setup to hit the actual storage
1316 * instead of potentially using a temporary like we might with the
1317 * ir_dereference handler.
1319 static struct ir_to_mesa_dst_reg
1320 get_assignment_lhs(ir_dereference
*ir
, ir_to_mesa_visitor
*v
)
1322 /* The LHS must be a dereference. If the LHS is a variable indexed array
1323 * access of a vector, it must be separated into a series conditional moves
1324 * before reaching this point (see ir_vec_index_to_cond_assign).
1326 assert(ir
->as_dereference());
1327 ir_dereference_array
*deref_array
= ir
->as_dereference_array();
1329 assert(!deref_array
->array
->type
->is_vector());
1332 /* Use the rvalue deref handler for the most part. We'll ignore
1333 * swizzles in it and write swizzles using writemask, though.
1336 return ir_to_mesa_dst_reg_from_src(v
->result
);
1340 ir_to_mesa_visitor::visit(ir_assignment
*ir
)
1342 struct ir_to_mesa_dst_reg l
;
1343 struct ir_to_mesa_src_reg r
;
1346 ir
->rhs
->accept(this);
1349 l
= get_assignment_lhs(ir
->lhs
, this);
1351 /* FINISHME: This should really set to the correct maximal writemask for each
1352 * FINISHME: component written (in the loops below). This case can only
1353 * FINISHME: occur for matrices, arrays, and structures.
1355 if (ir
->write_mask
== 0) {
1356 assert(!ir
->lhs
->type
->is_scalar() && !ir
->lhs
->type
->is_vector());
1357 l
.writemask
= WRITEMASK_XYZW
;
1358 } else if (ir
->lhs
->type
->is_scalar()) {
1359 /* FINISHME: This hack makes writing to gl_FragDepth, which lives in the
1360 * FINISHME: W component of fragment shader output zero, work correctly.
1362 l
.writemask
= WRITEMASK_XYZW
;
1365 int first_enabled_chan
= 0;
1368 assert(ir
->lhs
->type
->is_vector());
1369 l
.writemask
= ir
->write_mask
;
1371 for (int i
= 0; i
< 4; i
++) {
1372 if (l
.writemask
& (1 << i
)) {
1373 first_enabled_chan
= GET_SWZ(r
.swizzle
, i
);
1378 /* Swizzle a small RHS vector into the channels being written.
1380 * glsl ir treats write_mask as dictating how many channels are
1381 * present on the RHS while Mesa IR treats write_mask as just
1382 * showing which channels of the vec4 RHS get written.
1384 for (int i
= 0; i
< 4; i
++) {
1385 if (l
.writemask
& (1 << i
))
1386 swizzles
[i
] = GET_SWZ(r
.swizzle
, rhs_chan
++);
1388 swizzles
[i
] = first_enabled_chan
;
1390 r
.swizzle
= MAKE_SWIZZLE4(swizzles
[0], swizzles
[1],
1391 swizzles
[2], swizzles
[3]);
1394 assert(l
.file
!= PROGRAM_UNDEFINED
);
1395 assert(r
.file
!= PROGRAM_UNDEFINED
);
1397 if (ir
->condition
) {
1398 ir_to_mesa_src_reg condition
;
1400 ir
->condition
->accept(this);
1401 condition
= this->result
;
1403 /* We use the OPCODE_CMP (a < 0 ? b : c) for conditional moves,
1404 * and the condition we produced is 0.0 or 1.0. By flipping the
1405 * sign, we can choose which value OPCODE_CMP produces without
1406 * an extra computing the condition.
1408 condition
.negate
= ~condition
.negate
;
1409 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
1410 ir_to_mesa_emit_op3(ir
, OPCODE_CMP
, l
,
1411 condition
, r
, ir_to_mesa_src_reg_from_dst(l
));
1416 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
1417 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, l
, r
);
1426 ir_to_mesa_visitor::visit(ir_constant
*ir
)
1428 ir_to_mesa_src_reg src_reg
;
1429 GLfloat stack_vals
[4] = { 0 };
1430 GLfloat
*values
= stack_vals
;
1433 /* Unfortunately, 4 floats is all we can get into
1434 * _mesa_add_unnamed_constant. So, make a temp to store an
1435 * aggregate constant and move each constant value into it. If we
1436 * get lucky, copy propagation will eliminate the extra moves.
1439 if (ir
->type
->base_type
== GLSL_TYPE_STRUCT
) {
1440 ir_to_mesa_src_reg temp_base
= get_temp(ir
->type
);
1441 ir_to_mesa_dst_reg temp
= ir_to_mesa_dst_reg_from_src(temp_base
);
1443 foreach_iter(exec_list_iterator
, iter
, ir
->components
) {
1444 ir_constant
*field_value
= (ir_constant
*)iter
.get();
1445 int size
= type_size(field_value
->type
);
1449 field_value
->accept(this);
1450 src_reg
= this->result
;
1452 for (i
= 0; i
< (unsigned int)size
; i
++) {
1453 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, temp
, src_reg
);
1459 this->result
= temp_base
;
1463 if (ir
->type
->is_array()) {
1464 ir_to_mesa_src_reg temp_base
= get_temp(ir
->type
);
1465 ir_to_mesa_dst_reg temp
= ir_to_mesa_dst_reg_from_src(temp_base
);
1466 int size
= type_size(ir
->type
->fields
.array
);
1470 for (i
= 0; i
< ir
->type
->length
; i
++) {
1471 ir
->array_elements
[i
]->accept(this);
1472 src_reg
= this->result
;
1473 for (int j
= 0; j
< size
; j
++) {
1474 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, temp
, src_reg
);
1480 this->result
= temp_base
;
1484 if (ir
->type
->is_matrix()) {
1485 ir_to_mesa_src_reg mat
= get_temp(ir
->type
);
1486 ir_to_mesa_dst_reg mat_column
= ir_to_mesa_dst_reg_from_src(mat
);
1488 for (i
= 0; i
< ir
->type
->matrix_columns
; i
++) {
1489 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
1490 values
= &ir
->value
.f
[i
* ir
->type
->vector_elements
];
1492 src_reg
= ir_to_mesa_src_reg(PROGRAM_CONSTANT
, -1, NULL
);
1493 src_reg
.index
= _mesa_add_unnamed_constant(this->prog
->Parameters
,
1495 ir
->type
->vector_elements
,
1497 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, mat_column
, src_reg
);
1506 src_reg
.file
= PROGRAM_CONSTANT
;
1507 switch (ir
->type
->base_type
) {
1508 case GLSL_TYPE_FLOAT
:
1509 values
= &ir
->value
.f
[0];
1511 case GLSL_TYPE_UINT
:
1512 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1513 values
[i
] = ir
->value
.u
[i
];
1517 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1518 values
[i
] = ir
->value
.i
[i
];
1521 case GLSL_TYPE_BOOL
:
1522 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1523 values
[i
] = ir
->value
.b
[i
];
1527 assert(!"Non-float/uint/int/bool constant");
1530 this->result
= ir_to_mesa_src_reg(PROGRAM_CONSTANT
, -1, ir
->type
);
1531 this->result
.index
= _mesa_add_unnamed_constant(this->prog
->Parameters
,
1533 ir
->type
->vector_elements
,
1534 &this->result
.swizzle
);
1538 ir_to_mesa_visitor::get_function_signature(ir_function_signature
*sig
)
1540 function_entry
*entry
;
1542 foreach_iter(exec_list_iterator
, iter
, this->function_signatures
) {
1543 entry
= (function_entry
*)iter
.get();
1545 if (entry
->sig
== sig
)
1549 entry
= talloc(mem_ctx
, function_entry
);
1551 entry
->sig_id
= this->next_signature_id
++;
1552 entry
->bgn_inst
= NULL
;
1554 /* Allocate storage for all the parameters. */
1555 foreach_iter(exec_list_iterator
, iter
, sig
->parameters
) {
1556 ir_variable
*param
= (ir_variable
*)iter
.get();
1557 variable_storage
*storage
;
1559 storage
= find_variable_storage(param
);
1562 storage
= new(mem_ctx
) variable_storage(param
, PROGRAM_TEMPORARY
,
1564 this->variables
.push_tail(storage
);
1566 this->next_temp
+= type_size(param
->type
);
1569 if (!sig
->return_type
->is_void()) {
1570 entry
->return_reg
= get_temp(sig
->return_type
);
1572 entry
->return_reg
= ir_to_mesa_undef
;
1575 this->function_signatures
.push_tail(entry
);
1580 ir_to_mesa_visitor::visit(ir_call
*ir
)
1582 ir_to_mesa_instruction
*call_inst
;
1583 ir_function_signature
*sig
= ir
->get_callee();
1584 function_entry
*entry
= get_function_signature(sig
);
1587 /* Process in parameters. */
1588 exec_list_iterator sig_iter
= sig
->parameters
.iterator();
1589 foreach_iter(exec_list_iterator
, iter
, *ir
) {
1590 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
1591 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
1593 if (param
->mode
== ir_var_in
||
1594 param
->mode
== ir_var_inout
) {
1595 variable_storage
*storage
= find_variable_storage(param
);
1598 param_rval
->accept(this);
1599 ir_to_mesa_src_reg r
= this->result
;
1601 ir_to_mesa_dst_reg l
;
1602 l
.file
= storage
->file
;
1603 l
.index
= storage
->index
;
1605 l
.writemask
= WRITEMASK_XYZW
;
1606 l
.cond_mask
= COND_TR
;
1608 for (i
= 0; i
< type_size(param
->type
); i
++) {
1609 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, l
, r
);
1617 assert(!sig_iter
.has_next());
1619 /* Emit call instruction */
1620 call_inst
= ir_to_mesa_emit_op1(ir
, OPCODE_CAL
,
1621 ir_to_mesa_undef_dst
, ir_to_mesa_undef
);
1622 call_inst
->function
= entry
;
1624 /* Process out parameters. */
1625 sig_iter
= sig
->parameters
.iterator();
1626 foreach_iter(exec_list_iterator
, iter
, *ir
) {
1627 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
1628 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
1630 if (param
->mode
== ir_var_out
||
1631 param
->mode
== ir_var_inout
) {
1632 variable_storage
*storage
= find_variable_storage(param
);
1635 ir_to_mesa_src_reg r
;
1636 r
.file
= storage
->file
;
1637 r
.index
= storage
->index
;
1639 r
.swizzle
= SWIZZLE_NOOP
;
1642 param_rval
->accept(this);
1643 ir_to_mesa_dst_reg l
= ir_to_mesa_dst_reg_from_src(this->result
);
1645 for (i
= 0; i
< type_size(param
->type
); i
++) {
1646 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, l
, r
);
1654 assert(!sig_iter
.has_next());
1656 /* Process return value. */
1657 this->result
= entry
->return_reg
;
1661 ir_to_mesa_visitor::visit(ir_texture
*ir
)
1663 ir_to_mesa_src_reg result_src
, coord
, lod_info
, projector
;
1664 ir_to_mesa_dst_reg result_dst
, coord_dst
;
1665 ir_to_mesa_instruction
*inst
= NULL
;
1666 prog_opcode opcode
= OPCODE_NOP
;
1668 ir
->coordinate
->accept(this);
1670 /* Put our coords in a temp. We'll need to modify them for shadow,
1671 * projection, or LOD, so the only case we'd use it as is is if
1672 * we're doing plain old texturing. Mesa IR optimization should
1673 * handle cleaning up our mess in that case.
1675 coord
= get_temp(glsl_type::vec4_type
);
1676 coord_dst
= ir_to_mesa_dst_reg_from_src(coord
);
1677 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, coord_dst
,
1680 if (ir
->projector
) {
1681 ir
->projector
->accept(this);
1682 projector
= this->result
;
1685 /* Storage for our result. Ideally for an assignment we'd be using
1686 * the actual storage for the result here, instead.
1688 result_src
= get_temp(glsl_type::vec4_type
);
1689 result_dst
= ir_to_mesa_dst_reg_from_src(result_src
);
1693 opcode
= OPCODE_TEX
;
1696 opcode
= OPCODE_TXB
;
1697 ir
->lod_info
.bias
->accept(this);
1698 lod_info
= this->result
;
1701 opcode
= OPCODE_TXL
;
1702 ir
->lod_info
.lod
->accept(this);
1703 lod_info
= this->result
;
1707 assert(!"GLSL 1.30 features unsupported");
1711 if (ir
->projector
) {
1712 if (opcode
== OPCODE_TEX
) {
1713 /* Slot the projector in as the last component of the coord. */
1714 coord_dst
.writemask
= WRITEMASK_W
;
1715 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, coord_dst
, projector
);
1716 coord_dst
.writemask
= WRITEMASK_XYZW
;
1717 opcode
= OPCODE_TXP
;
1719 ir_to_mesa_src_reg coord_w
= coord
;
1720 coord_w
.swizzle
= SWIZZLE_WWWW
;
1722 /* For the other TEX opcodes there's no projective version
1723 * since the last slot is taken up by lod info. Do the
1724 * projective divide now.
1726 coord_dst
.writemask
= WRITEMASK_W
;
1727 ir_to_mesa_emit_op1(ir
, OPCODE_RCP
, coord_dst
, projector
);
1729 coord_dst
.writemask
= WRITEMASK_XYZ
;
1730 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
, coord_dst
, coord
, coord_w
);
1732 coord_dst
.writemask
= WRITEMASK_XYZW
;
1733 coord
.swizzle
= SWIZZLE_XYZW
;
1737 if (ir
->shadow_comparitor
) {
1738 /* Slot the shadow value in as the second to last component of the
1741 ir
->shadow_comparitor
->accept(this);
1742 coord_dst
.writemask
= WRITEMASK_Z
;
1743 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, coord_dst
, this->result
);
1744 coord_dst
.writemask
= WRITEMASK_XYZW
;
1747 if (opcode
== OPCODE_TXL
|| opcode
== OPCODE_TXB
) {
1748 /* Mesa IR stores lod or lod bias in the last channel of the coords. */
1749 coord_dst
.writemask
= WRITEMASK_W
;
1750 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, coord_dst
, lod_info
);
1751 coord_dst
.writemask
= WRITEMASK_XYZW
;
1754 inst
= ir_to_mesa_emit_op1(ir
, opcode
, result_dst
, coord
);
1756 if (ir
->shadow_comparitor
)
1757 inst
->tex_shadow
= GL_TRUE
;
1759 inst
->sampler
= _mesa_get_sampler_uniform_value(ir
->sampler
,
1760 this->shader_program
,
1763 const glsl_type
*sampler_type
= ir
->sampler
->type
;
1765 switch (sampler_type
->sampler_dimensionality
) {
1766 case GLSL_SAMPLER_DIM_1D
:
1767 inst
->tex_target
= (sampler_type
->sampler_array
)
1768 ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
1770 case GLSL_SAMPLER_DIM_2D
:
1771 inst
->tex_target
= (sampler_type
->sampler_array
)
1772 ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
1774 case GLSL_SAMPLER_DIM_3D
:
1775 inst
->tex_target
= TEXTURE_3D_INDEX
;
1777 case GLSL_SAMPLER_DIM_CUBE
:
1778 inst
->tex_target
= TEXTURE_CUBE_INDEX
;
1780 case GLSL_SAMPLER_DIM_RECT
:
1781 inst
->tex_target
= TEXTURE_RECT_INDEX
;
1783 case GLSL_SAMPLER_DIM_BUF
:
1784 assert(!"FINISHME: Implement ARB_texture_buffer_object");
1787 assert(!"Should not get here.");
1790 this->result
= result_src
;
1794 ir_to_mesa_visitor::visit(ir_return
*ir
)
1796 if (ir
->get_value()) {
1797 ir_to_mesa_dst_reg l
;
1800 assert(current_function
);
1802 ir
->get_value()->accept(this);
1803 ir_to_mesa_src_reg r
= this->result
;
1805 l
= ir_to_mesa_dst_reg_from_src(current_function
->return_reg
);
1807 for (i
= 0; i
< type_size(current_function
->sig
->return_type
); i
++) {
1808 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, l
, r
);
1814 ir_to_mesa_emit_op0(ir
, OPCODE_RET
);
1818 ir_to_mesa_visitor::visit(ir_discard
*ir
)
1820 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
1822 assert(ir
->condition
== NULL
); /* FINISHME */
1824 ir_to_mesa_emit_op0(ir
, OPCODE_KIL_NV
);
1825 fp
->UsesKill
= GL_TRUE
;
1829 ir_to_mesa_visitor::visit(ir_if
*ir
)
1831 ir_to_mesa_instruction
*cond_inst
, *if_inst
, *else_inst
= NULL
;
1832 ir_to_mesa_instruction
*prev_inst
;
1834 prev_inst
= (ir_to_mesa_instruction
*)this->instructions
.get_tail();
1836 ir
->condition
->accept(this);
1837 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
1839 if (this->options
->EmitCondCodes
) {
1840 cond_inst
= (ir_to_mesa_instruction
*)this->instructions
.get_tail();
1842 /* See if we actually generated any instruction for generating
1843 * the condition. If not, then cook up a move to a temp so we
1844 * have something to set cond_update on.
1846 if (cond_inst
== prev_inst
) {
1847 ir_to_mesa_src_reg temp
= get_temp(glsl_type::bool_type
);
1848 cond_inst
= ir_to_mesa_emit_op1(ir
->condition
, OPCODE_MOV
,
1849 ir_to_mesa_dst_reg_from_src(temp
),
1852 cond_inst
->cond_update
= GL_TRUE
;
1854 if_inst
= ir_to_mesa_emit_op0(ir
->condition
, OPCODE_IF
);
1855 if_inst
->dst_reg
.cond_mask
= COND_NE
;
1857 if_inst
= ir_to_mesa_emit_op1(ir
->condition
,
1858 OPCODE_IF
, ir_to_mesa_undef_dst
,
1862 this->instructions
.push_tail(if_inst
);
1864 visit_exec_list(&ir
->then_instructions
, this);
1866 if (!ir
->else_instructions
.is_empty()) {
1867 else_inst
= ir_to_mesa_emit_op0(ir
->condition
, OPCODE_ELSE
);
1868 visit_exec_list(&ir
->else_instructions
, this);
1871 if_inst
= ir_to_mesa_emit_op1(ir
->condition
, OPCODE_ENDIF
,
1872 ir_to_mesa_undef_dst
, ir_to_mesa_undef
);
1875 ir_to_mesa_visitor::ir_to_mesa_visitor()
1877 result
.file
= PROGRAM_UNDEFINED
;
1879 next_signature_id
= 1;
1880 current_function
= NULL
;
1881 mem_ctx
= talloc_new(NULL
);
1884 ir_to_mesa_visitor::~ir_to_mesa_visitor()
1886 talloc_free(mem_ctx
);
1889 static struct prog_src_register
1890 mesa_src_reg_from_ir_src_reg(ir_to_mesa_src_reg reg
)
1892 struct prog_src_register mesa_reg
;
1894 mesa_reg
.File
= reg
.file
;
1895 assert(reg
.index
< (1 << INST_INDEX_BITS
) - 1);
1896 mesa_reg
.Index
= reg
.index
;
1897 mesa_reg
.Swizzle
= reg
.swizzle
;
1898 mesa_reg
.RelAddr
= reg
.reladdr
!= NULL
;
1899 mesa_reg
.Negate
= reg
.negate
;
1901 mesa_reg
.HasIndex2
= GL_FALSE
;
1902 mesa_reg
.RelAddr2
= 0;
1903 mesa_reg
.Index2
= 0;
1909 set_branchtargets(ir_to_mesa_visitor
*v
,
1910 struct prog_instruction
*mesa_instructions
,
1911 int num_instructions
)
1913 int if_count
= 0, loop_count
= 0;
1914 int *if_stack
, *loop_stack
;
1915 int if_stack_pos
= 0, loop_stack_pos
= 0;
1918 for (i
= 0; i
< num_instructions
; i
++) {
1919 switch (mesa_instructions
[i
].Opcode
) {
1923 case OPCODE_BGNLOOP
:
1928 mesa_instructions
[i
].BranchTarget
= -1;
1935 if_stack
= talloc_zero_array(v
->mem_ctx
, int, if_count
);
1936 loop_stack
= talloc_zero_array(v
->mem_ctx
, int, loop_count
);
1938 for (i
= 0; i
< num_instructions
; i
++) {
1939 switch (mesa_instructions
[i
].Opcode
) {
1941 if_stack
[if_stack_pos
] = i
;
1945 mesa_instructions
[if_stack
[if_stack_pos
- 1]].BranchTarget
= i
;
1946 if_stack
[if_stack_pos
- 1] = i
;
1949 mesa_instructions
[if_stack
[if_stack_pos
- 1]].BranchTarget
= i
;
1952 case OPCODE_BGNLOOP
:
1953 loop_stack
[loop_stack_pos
] = i
;
1956 case OPCODE_ENDLOOP
:
1958 /* Rewrite any breaks/conts at this nesting level (haven't
1959 * already had a BranchTarget assigned) to point to the end
1962 for (j
= loop_stack
[loop_stack_pos
]; j
< i
; j
++) {
1963 if (mesa_instructions
[j
].Opcode
== OPCODE_BRK
||
1964 mesa_instructions
[j
].Opcode
== OPCODE_CONT
) {
1965 if (mesa_instructions
[j
].BranchTarget
== -1) {
1966 mesa_instructions
[j
].BranchTarget
= i
;
1970 /* The loop ends point at each other. */
1971 mesa_instructions
[i
].BranchTarget
= loop_stack
[loop_stack_pos
];
1972 mesa_instructions
[loop_stack
[loop_stack_pos
]].BranchTarget
= i
;
1975 foreach_iter(exec_list_iterator
, iter
, v
->function_signatures
) {
1976 function_entry
*entry
= (function_entry
*)iter
.get();
1978 if (entry
->sig_id
== mesa_instructions
[i
].BranchTarget
) {
1979 mesa_instructions
[i
].BranchTarget
= entry
->inst
;
1991 print_program(struct prog_instruction
*mesa_instructions
,
1992 ir_instruction
**mesa_instruction_annotation
,
1993 int num_instructions
)
1995 ir_instruction
*last_ir
= NULL
;
1999 for (i
= 0; i
< num_instructions
; i
++) {
2000 struct prog_instruction
*mesa_inst
= mesa_instructions
+ i
;
2001 ir_instruction
*ir
= mesa_instruction_annotation
[i
];
2003 fprintf(stdout
, "%3d: ", i
);
2005 if (last_ir
!= ir
&& ir
) {
2008 for (j
= 0; j
< indent
; j
++) {
2009 fprintf(stdout
, " ");
2015 fprintf(stdout
, " "); /* line number spacing. */
2018 indent
= _mesa_fprint_instruction_opt(stdout
, mesa_inst
, indent
,
2019 PROG_PRINT_DEBUG
, NULL
);
2024 count_resources(struct gl_program
*prog
)
2028 prog
->SamplersUsed
= 0;
2030 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
2031 struct prog_instruction
*inst
= &prog
->Instructions
[i
];
2033 if (_mesa_is_tex_instruction(inst
->Opcode
)) {
2034 prog
->SamplerTargets
[inst
->TexSrcUnit
] =
2035 (gl_texture_index
)inst
->TexSrcTarget
;
2036 prog
->SamplersUsed
|= 1 << inst
->TexSrcUnit
;
2037 if (inst
->TexShadow
) {
2038 prog
->ShadowSamplers
|= 1 << inst
->TexSrcUnit
;
2043 _mesa_update_shader_textures_used(prog
);
2046 struct uniform_sort
{
2047 struct gl_uniform
*u
;
2051 /* The shader_program->Uniforms list is almost sorted in increasing
2052 * uniform->{Frag,Vert}Pos locations, but not quite when there are
2053 * uniforms shared between targets. We need to add parameters in
2054 * increasing order for the targets.
2057 sort_uniforms(const void *a
, const void *b
)
2059 struct uniform_sort
*u1
= (struct uniform_sort
*)a
;
2060 struct uniform_sort
*u2
= (struct uniform_sort
*)b
;
2062 return u1
->pos
- u2
->pos
;
2065 /* Add the uniforms to the parameters. The linker chose locations
2066 * in our parameters lists (which weren't created yet), which the
2067 * uniforms code will use to poke values into our parameters list
2068 * when uniforms are updated.
2071 add_uniforms_to_parameters_list(struct gl_shader_program
*shader_program
,
2072 struct gl_shader
*shader
,
2073 struct gl_program
*prog
)
2076 unsigned int next_sampler
= 0, num_uniforms
= 0;
2077 struct uniform_sort
*sorted_uniforms
;
2079 sorted_uniforms
= talloc_array(NULL
, struct uniform_sort
,
2080 shader_program
->Uniforms
->NumUniforms
);
2082 for (i
= 0; i
< shader_program
->Uniforms
->NumUniforms
; i
++) {
2083 struct gl_uniform
*uniform
= shader_program
->Uniforms
->Uniforms
+ i
;
2084 int parameter_index
= -1;
2086 switch (shader
->Type
) {
2087 case GL_VERTEX_SHADER
:
2088 parameter_index
= uniform
->VertPos
;
2090 case GL_FRAGMENT_SHADER
:
2091 parameter_index
= uniform
->FragPos
;
2093 case GL_GEOMETRY_SHADER
:
2094 parameter_index
= uniform
->GeomPos
;
2098 /* Only add uniforms used in our target. */
2099 if (parameter_index
!= -1) {
2100 sorted_uniforms
[num_uniforms
].pos
= parameter_index
;
2101 sorted_uniforms
[num_uniforms
].u
= uniform
;
2106 qsort(sorted_uniforms
, num_uniforms
, sizeof(struct uniform_sort
),
2109 for (i
= 0; i
< num_uniforms
; i
++) {
2110 struct gl_uniform
*uniform
= sorted_uniforms
[i
].u
;
2111 int parameter_index
= sorted_uniforms
[i
].pos
;
2112 const glsl_type
*type
= uniform
->Type
;
2115 if (type
->is_vector() ||
2116 type
->is_scalar()) {
2117 size
= type
->vector_elements
;
2119 size
= type_size(type
) * 4;
2122 gl_register_file file
;
2123 if (type
->is_sampler() ||
2124 (type
->is_array() && type
->fields
.array
->is_sampler())) {
2125 file
= PROGRAM_SAMPLER
;
2127 file
= PROGRAM_UNIFORM
;
2130 GLint index
= _mesa_lookup_parameter_index(prog
->Parameters
, -1,
2134 index
= _mesa_add_parameter(prog
->Parameters
, file
,
2135 uniform
->Name
, size
, type
->gl_type
,
2138 /* Sampler uniform values are stored in prog->SamplerUnits,
2139 * and the entry in that array is selected by this index we
2140 * store in ParameterValues[].
2142 if (file
== PROGRAM_SAMPLER
) {
2143 for (unsigned int j
= 0; j
< size
/ 4; j
++)
2144 prog
->Parameters
->ParameterValues
[index
+ j
][0] = next_sampler
++;
2147 /* The location chosen in the Parameters list here (returned
2148 * from _mesa_add_uniform) has to match what the linker chose.
2150 if (index
!= parameter_index
) {
2151 fail_link(shader_program
, "Allocation of uniform `%s' to target "
2152 "failed (%d vs %d)\n",
2153 uniform
->Name
, index
, parameter_index
);
2158 talloc_free(sorted_uniforms
);
2162 set_uniform_initializer(GLcontext
*ctx
, void *mem_ctx
,
2163 struct gl_shader_program
*shader_program
,
2164 const char *name
, const glsl_type
*type
,
2167 if (type
->is_record()) {
2168 ir_constant
*field_constant
;
2170 field_constant
= (ir_constant
*)val
->components
.get_head();
2172 for (unsigned int i
= 0; i
< type
->length
; i
++) {
2173 const glsl_type
*field_type
= type
->fields
.structure
[i
].type
;
2174 const char *field_name
= talloc_asprintf(mem_ctx
, "%s.%s", name
,
2175 type
->fields
.structure
[i
].name
);
2176 set_uniform_initializer(ctx
, mem_ctx
, shader_program
, field_name
,
2177 field_type
, field_constant
);
2178 field_constant
= (ir_constant
*)field_constant
->next
;
2183 int loc
= _mesa_get_uniform_location(ctx
, shader_program
, name
);
2186 fail_link(shader_program
,
2187 "Couldn't find uniform for initializer %s\n", name
);
2191 for (unsigned int i
= 0; i
< (type
->is_array() ? type
->length
: 1); i
++) {
2192 ir_constant
*element
;
2193 const glsl_type
*element_type
;
2194 if (type
->is_array()) {
2195 element
= val
->array_elements
[i
];
2196 element_type
= type
->fields
.array
;
2199 element_type
= type
;
2204 if (element_type
->base_type
== GLSL_TYPE_BOOL
) {
2205 int *conv
= talloc_array(mem_ctx
, int, element_type
->components());
2206 for (unsigned int j
= 0; j
< element_type
->components(); j
++) {
2207 conv
[j
] = element
->value
.b
[j
];
2209 values
= (void *)conv
;
2210 element_type
= glsl_type::get_instance(GLSL_TYPE_INT
,
2211 element_type
->vector_elements
,
2214 values
= &element
->value
;
2217 if (element_type
->is_matrix()) {
2218 _mesa_uniform_matrix(ctx
, shader_program
,
2219 element_type
->matrix_columns
,
2220 element_type
->vector_elements
,
2221 loc
, 1, GL_FALSE
, (GLfloat
*)values
);
2222 loc
+= element_type
->matrix_columns
;
2224 _mesa_uniform(ctx
, shader_program
, loc
, element_type
->matrix_columns
,
2225 values
, element_type
->gl_type
);
2226 loc
+= type_size(element_type
);
2232 set_uniform_initializers(GLcontext
*ctx
,
2233 struct gl_shader_program
*shader_program
)
2235 void *mem_ctx
= NULL
;
2237 for (unsigned int i
= 0; i
< shader_program
->_NumLinkedShaders
; i
++) {
2238 struct gl_shader
*shader
= shader_program
->_LinkedShaders
[i
];
2239 foreach_iter(exec_list_iterator
, iter
, *shader
->ir
) {
2240 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
2241 ir_variable
*var
= ir
->as_variable();
2243 if (!var
|| var
->mode
!= ir_var_uniform
|| !var
->constant_value
)
2247 mem_ctx
= talloc_new(NULL
);
2249 set_uniform_initializer(ctx
, mem_ctx
, shader_program
, var
->name
,
2250 var
->type
, var
->constant_value
);
2254 talloc_free(mem_ctx
);
2258 get_mesa_program(GLcontext
*ctx
, struct gl_shader_program
*shader_program
,
2259 struct gl_shader
*shader
)
2261 ir_to_mesa_visitor v
;
2262 struct prog_instruction
*mesa_instructions
, *mesa_inst
;
2263 ir_instruction
**mesa_instruction_annotation
;
2265 struct gl_program
*prog
;
2267 const char *target_string
;
2269 struct gl_shader_compiler_options
*options
=
2270 &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(shader
->Type
)];
2272 switch (shader
->Type
) {
2273 case GL_VERTEX_SHADER
:
2274 target
= GL_VERTEX_PROGRAM_ARB
;
2275 target_string
= "vertex";
2277 case GL_FRAGMENT_SHADER
:
2278 target
= GL_FRAGMENT_PROGRAM_ARB
;
2279 target_string
= "fragment";
2282 assert(!"should not be reached");
2286 validate_ir_tree(shader
->ir
);
2288 prog
= ctx
->Driver
.NewProgram(ctx
, target
, shader_program
->Name
);
2291 prog
->Parameters
= _mesa_new_parameter_list();
2292 prog
->Varying
= _mesa_new_parameter_list();
2293 prog
->Attributes
= _mesa_new_parameter_list();
2296 v
.shader_program
= shader_program
;
2297 v
.options
= options
;
2299 add_uniforms_to_parameters_list(shader_program
, shader
, prog
);
2301 /* Emit Mesa IR for main(). */
2302 visit_exec_list(shader
->ir
, &v
);
2303 v
.ir_to_mesa_emit_op0(NULL
, OPCODE_END
);
2305 /* Now emit bodies for any functions that were used. */
2307 progress
= GL_FALSE
;
2309 foreach_iter(exec_list_iterator
, iter
, v
.function_signatures
) {
2310 function_entry
*entry
= (function_entry
*)iter
.get();
2312 if (!entry
->bgn_inst
) {
2313 v
.current_function
= entry
;
2315 entry
->bgn_inst
= v
.ir_to_mesa_emit_op0(NULL
, OPCODE_BGNSUB
);
2316 entry
->bgn_inst
->function
= entry
;
2318 visit_exec_list(&entry
->sig
->body
, &v
);
2320 ir_to_mesa_instruction
*last
;
2321 last
= (ir_to_mesa_instruction
*)v
.instructions
.get_tail();
2322 if (last
->op
!= OPCODE_RET
)
2323 v
.ir_to_mesa_emit_op0(NULL
, OPCODE_RET
);
2325 ir_to_mesa_instruction
*end
;
2326 end
= v
.ir_to_mesa_emit_op0(NULL
, OPCODE_ENDSUB
);
2327 end
->function
= entry
;
2334 prog
->NumTemporaries
= v
.next_temp
;
2336 int num_instructions
= 0;
2337 foreach_iter(exec_list_iterator
, iter
, v
.instructions
) {
2342 (struct prog_instruction
*)calloc(num_instructions
,
2343 sizeof(*mesa_instructions
));
2344 mesa_instruction_annotation
= talloc_array(v
.mem_ctx
, ir_instruction
*,
2347 mesa_inst
= mesa_instructions
;
2349 foreach_iter(exec_list_iterator
, iter
, v
.instructions
) {
2350 ir_to_mesa_instruction
*inst
= (ir_to_mesa_instruction
*)iter
.get();
2352 mesa_inst
->Opcode
= inst
->op
;
2353 mesa_inst
->CondUpdate
= inst
->cond_update
;
2354 mesa_inst
->DstReg
.File
= inst
->dst_reg
.file
;
2355 mesa_inst
->DstReg
.Index
= inst
->dst_reg
.index
;
2356 mesa_inst
->DstReg
.CondMask
= inst
->dst_reg
.cond_mask
;
2357 mesa_inst
->DstReg
.WriteMask
= inst
->dst_reg
.writemask
;
2358 mesa_inst
->DstReg
.RelAddr
= inst
->dst_reg
.reladdr
!= NULL
;
2359 mesa_inst
->SrcReg
[0] = mesa_src_reg_from_ir_src_reg(inst
->src_reg
[0]);
2360 mesa_inst
->SrcReg
[1] = mesa_src_reg_from_ir_src_reg(inst
->src_reg
[1]);
2361 mesa_inst
->SrcReg
[2] = mesa_src_reg_from_ir_src_reg(inst
->src_reg
[2]);
2362 mesa_inst
->TexSrcUnit
= inst
->sampler
;
2363 mesa_inst
->TexSrcTarget
= inst
->tex_target
;
2364 mesa_inst
->TexShadow
= inst
->tex_shadow
;
2365 mesa_instruction_annotation
[i
] = inst
->ir
;
2367 /* Set IndirectRegisterFiles. */
2368 if (mesa_inst
->DstReg
.RelAddr
)
2369 prog
->IndirectRegisterFiles
|= 1 << mesa_inst
->DstReg
.File
;
2371 for (unsigned src
= 0; src
< 3; src
++)
2372 if (mesa_inst
->SrcReg
[src
].RelAddr
)
2373 prog
->IndirectRegisterFiles
|= 1 << mesa_inst
->SrcReg
[src
].File
;
2375 if (options
->EmitNoIfs
&& mesa_inst
->Opcode
== OPCODE_IF
) {
2376 fail_link(shader_program
, "Couldn't flatten if statement\n");
2379 switch (mesa_inst
->Opcode
) {
2381 inst
->function
->inst
= i
;
2382 mesa_inst
->Comment
= strdup(inst
->function
->sig
->function_name());
2385 mesa_inst
->Comment
= strdup(inst
->function
->sig
->function_name());
2388 mesa_inst
->BranchTarget
= inst
->function
->sig_id
; /* rewritten later */
2391 prog
->NumAddressRegs
= 1;
2401 set_branchtargets(&v
, mesa_instructions
, num_instructions
);
2403 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
2405 printf("GLSL IR for linked %s program %d:\n", target_string
,
2406 shader_program
->Name
);
2407 _mesa_print_ir(shader
->ir
, NULL
);
2410 printf("Mesa IR for linked %s program %d:\n", target_string
,
2411 shader_program
->Name
);
2412 print_program(mesa_instructions
, mesa_instruction_annotation
,
2416 prog
->Instructions
= mesa_instructions
;
2417 prog
->NumInstructions
= num_instructions
;
2419 do_set_program_inouts(shader
->ir
, prog
);
2420 count_resources(prog
);
2422 _mesa_reference_program(ctx
, &shader
->Program
, prog
);
2424 if ((ctx
->Shader
.Flags
& GLSL_NO_OPT
) == 0) {
2425 _mesa_optimize_program(ctx
, prog
);
2433 _mesa_ir_compile_shader(GLcontext
*ctx
, struct gl_shader
*shader
)
2435 assert(shader
->CompileStatus
);
2442 _mesa_ir_link_shader(GLcontext
*ctx
, struct gl_shader_program
*prog
)
2444 assert(prog
->LinkStatus
);
2446 for (unsigned i
= 0; i
< prog
->_NumLinkedShaders
; i
++) {
2448 exec_list
*ir
= prog
->_LinkedShaders
[i
]->ir
;
2449 struct gl_shader_compiler_options
*options
=
2450 &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(prog
->_LinkedShaders
[i
]->Type
)];
2456 do_mat_op_to_vec(ir
);
2457 do_mod_to_fract(ir
);
2458 do_div_to_mul_rcp(ir
);
2459 do_explog_to_explog2(ir
);
2461 progress
= do_lower_jumps(ir
, true, true, options
->EmitNoMainReturn
, options
->EmitNoCont
, options
->EmitNoLoops
) || progress
;
2463 progress
= do_common_optimization(ir
, true, options
->MaxUnrollIterations
) || progress
;
2465 if (options
->EmitNoIfs
)
2466 progress
= do_if_to_cond_assign(ir
) || progress
;
2468 if (options
->EmitNoNoise
)
2469 progress
= lower_noise(ir
) || progress
;
2471 /* If there are forms of indirect addressing that the driver
2472 * cannot handle, perform the lowering pass.
2474 if (options
->EmitNoIndirectInput
|| options
->EmitNoIndirectOutput
2475 || options
->EmitNoIndirectTemp
|| options
->EmitNoIndirectUniform
)
2477 lower_variable_index_to_cond_assign(ir
,
2478 options
->EmitNoIndirectInput
,
2479 options
->EmitNoIndirectOutput
,
2480 options
->EmitNoIndirectTemp
,
2481 options
->EmitNoIndirectUniform
)
2484 progress
= do_vec_index_to_cond_assign(ir
) || progress
;
2487 validate_ir_tree(ir
);
2490 for (unsigned i
= 0; i
< prog
->_NumLinkedShaders
; i
++) {
2491 struct gl_program
*linked_prog
;
2494 linked_prog
= get_mesa_program(ctx
, prog
, prog
->_LinkedShaders
[i
]);
2496 switch (prog
->_LinkedShaders
[i
]->Type
) {
2497 case GL_VERTEX_SHADER
:
2498 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
,
2499 (struct gl_vertex_program
*)linked_prog
);
2500 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_VERTEX_PROGRAM_ARB
,
2503 case GL_FRAGMENT_SHADER
:
2504 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
,
2505 (struct gl_fragment_program
*)linked_prog
);
2506 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
2513 _mesa_reference_program(ctx
, &linked_prog
, NULL
);
2520 _mesa_glsl_compile_shader(GLcontext
*ctx
, struct gl_shader
*shader
)
2522 struct _mesa_glsl_parse_state
*state
=
2523 new(shader
) _mesa_glsl_parse_state(ctx
, shader
->Type
, shader
);
2525 const char *source
= shader
->Source
;
2526 /* Check if the user called glCompileShader without first calling
2527 * glShaderSource. This should fail to compile, but not raise a GL_ERROR.
2529 if (source
== NULL
) {
2530 shader
->CompileStatus
= GL_FALSE
;
2534 state
->error
= preprocess(state
, &source
, &state
->info_log
,
2535 &ctx
->Extensions
, ctx
->API
);
2537 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
2538 printf("GLSL source for shader %d:\n", shader
->Name
);
2539 printf("%s\n", shader
->Source
);
2542 if (!state
->error
) {
2543 _mesa_glsl_lexer_ctor(state
, source
);
2544 _mesa_glsl_parse(state
);
2545 _mesa_glsl_lexer_dtor(state
);
2548 talloc_free(shader
->ir
);
2549 shader
->ir
= new(shader
) exec_list
;
2550 if (!state
->error
&& !state
->translation_unit
.is_empty())
2551 _mesa_ast_to_hir(shader
->ir
, state
);
2553 if (!state
->error
&& !shader
->ir
->is_empty()) {
2554 validate_ir_tree(shader
->ir
);
2556 /* Do some optimization at compile time to reduce shader IR size
2557 * and reduce later work if the same shader is linked multiple times
2559 while (do_common_optimization(shader
->ir
, false, 32))
2562 validate_ir_tree(shader
->ir
);
2565 shader
->symbols
= state
->symbols
;
2567 shader
->CompileStatus
= !state
->error
;
2568 shader
->InfoLog
= state
->info_log
;
2569 shader
->Version
= state
->language_version
;
2570 memcpy(shader
->builtins_to_link
, state
->builtins_to_link
,
2571 sizeof(shader
->builtins_to_link
[0]) * state
->num_builtins_to_link
);
2572 shader
->num_builtins_to_link
= state
->num_builtins_to_link
;
2574 if (ctx
->Shader
.Flags
& GLSL_LOG
) {
2575 _mesa_write_shader_to_file(shader
);
2578 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
2579 if (shader
->CompileStatus
) {
2580 printf("GLSL IR for shader %d:\n", shader
->Name
);
2581 _mesa_print_ir(shader
->ir
, NULL
);
2584 printf("GLSL shader %d failed to compile.\n", shader
->Name
);
2586 if (shader
->InfoLog
&& shader
->InfoLog
[0] != 0) {
2587 printf("GLSL shader %d info log:\n", shader
->Name
);
2588 printf("%s\n", shader
->InfoLog
);
2592 /* Retain any live IR, but trash the rest. */
2593 reparent_ir(shader
->ir
, shader
->ir
);
2597 if (shader
->CompileStatus
) {
2598 if (!ctx
->Driver
.CompileShader(ctx
, shader
))
2599 shader
->CompileStatus
= GL_FALSE
;
2604 _mesa_glsl_link_shader(GLcontext
*ctx
, struct gl_shader_program
*prog
)
2608 _mesa_clear_shader_program_data(ctx
, prog
);
2610 prog
->LinkStatus
= GL_TRUE
;
2612 for (i
= 0; i
< prog
->NumShaders
; i
++) {
2613 if (!prog
->Shaders
[i
]->CompileStatus
) {
2614 fail_link(prog
, "linking with uncompiled shader");
2615 prog
->LinkStatus
= GL_FALSE
;
2619 prog
->Varying
= _mesa_new_parameter_list();
2620 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
, NULL
);
2621 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
, NULL
);
2623 if (prog
->LinkStatus
) {
2624 link_shaders(ctx
, prog
);
2627 if (prog
->LinkStatus
) {
2628 if (!ctx
->Driver
.LinkShader(ctx
, prog
)) {
2629 prog
->LinkStatus
= GL_FALSE
;
2633 set_uniform_initializers(ctx
, prog
);
2635 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
2636 if (!prog
->LinkStatus
) {
2637 printf("GLSL shader program %d failed to link\n", prog
->Name
);
2640 if (prog
->InfoLog
&& prog
->InfoLog
[0] != 0) {
2641 printf("GLSL shader program %d info log:\n", prog
->Name
);
2642 printf("%s\n", prog
->InfoLog
);