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 * Translate GLSL IR to Mesa's gl_program representation.
33 #include "main/compiler.h"
35 #include "ir_visitor.h"
36 #include "ir_print_visitor.h"
37 #include "ir_expression_flattening.h"
38 #include "glsl_types.h"
39 #include "glsl_parser_extras.h"
40 #include "../glsl/program.h"
41 #include "ir_optimization.h"
45 #include "main/mtypes.h"
46 #include "main/shaderapi.h"
47 #include "main/shaderobj.h"
48 #include "main/uniforms.h"
49 #include "program/hash_table.h"
50 #include "program/prog_instruction.h"
51 #include "program/prog_optimize.h"
52 #include "program/prog_print.h"
53 #include "program/program.h"
54 #include "program/prog_uniform.h"
55 #include "program/prog_parameter.h"
56 #include "program/sampler.h"
62 static int swizzle_for_size(int size
);
65 * This struct is a corresponding struct to Mesa prog_src_register, with
70 src_reg(gl_register_file file
, int index
, const glsl_type
*type
)
74 if (type
&& (type
->is_scalar() || type
->is_vector() || type
->is_matrix()))
75 this->swizzle
= swizzle_for_size(type
->vector_elements
);
77 this->swizzle
= SWIZZLE_XYZW
;
84 this->file
= PROGRAM_UNDEFINED
;
91 explicit src_reg(dst_reg reg
);
93 gl_register_file file
; /**< PROGRAM_* from Mesa */
94 int index
; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
95 GLuint swizzle
; /**< SWIZZLE_XYZWONEZERO swizzles from Mesa. */
96 int negate
; /**< NEGATE_XYZW mask from mesa */
97 /** Register index should be offset by the integer in this reg. */
103 dst_reg(gl_register_file file
, int writemask
)
107 this->writemask
= writemask
;
108 this->cond_mask
= COND_TR
;
109 this->reladdr
= NULL
;
114 this->file
= PROGRAM_UNDEFINED
;
117 this->cond_mask
= COND_TR
;
118 this->reladdr
= NULL
;
121 explicit dst_reg(src_reg reg
);
123 gl_register_file file
; /**< PROGRAM_* from Mesa */
124 int index
; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
125 int writemask
; /**< Bitfield of WRITEMASK_[XYZW] */
127 /** Register index should be offset by the integer in this reg. */
131 src_reg::src_reg(dst_reg reg
)
133 this->file
= reg
.file
;
134 this->index
= reg
.index
;
135 this->swizzle
= SWIZZLE_XYZW
;
137 this->reladdr
= NULL
;
140 dst_reg::dst_reg(src_reg reg
)
142 this->file
= reg
.file
;
143 this->index
= reg
.index
;
144 this->writemask
= WRITEMASK_XYZW
;
145 this->cond_mask
= COND_TR
;
146 this->reladdr
= reg
.reladdr
;
149 class ir_to_mesa_instruction
: public exec_node
{
151 /* Callers of this ralloc-based new need not call delete. It's
152 * easier to just ralloc_free 'ctx' (or any of its ancestors). */
153 static void* operator new(size_t size
, void *ctx
)
157 node
= rzalloc_size(ctx
, size
);
158 assert(node
!= NULL
);
166 /** Pointer to the ir source this tree came from for debugging */
168 GLboolean cond_update
;
170 int sampler
; /**< sampler index */
171 int tex_target
; /**< One of TEXTURE_*_INDEX */
172 GLboolean tex_shadow
;
174 class function_entry
*function
; /* Set on OPCODE_CAL or OPCODE_BGNSUB */
177 class variable_storage
: public exec_node
{
179 variable_storage(ir_variable
*var
, gl_register_file file
, int index
)
180 : file(file
), index(index
), var(var
)
185 gl_register_file file
;
187 ir_variable
*var
; /* variable that maps to this, if any */
190 class function_entry
: public exec_node
{
192 ir_function_signature
*sig
;
195 * identifier of this function signature used by the program.
197 * At the point that Mesa instructions for function calls are
198 * generated, we don't know the address of the first instruction of
199 * the function body. So we make the BranchTarget that is called a
200 * small integer and rewrite them during set_branchtargets().
205 * Pointer to first instruction of the function body.
207 * Set during function body emits after main() is processed.
209 ir_to_mesa_instruction
*bgn_inst
;
212 * Index of the first instruction of the function body in actual
215 * Set after convertion from ir_to_mesa_instruction to prog_instruction.
219 /** Storage for the return value. */
223 class ir_to_mesa_visitor
: public ir_visitor
{
225 ir_to_mesa_visitor();
226 ~ir_to_mesa_visitor();
228 function_entry
*current_function
;
230 struct gl_context
*ctx
;
231 struct gl_program
*prog
;
232 struct gl_shader_program
*shader_program
;
233 struct gl_shader_compiler_options
*options
;
237 variable_storage
*find_variable_storage(ir_variable
*var
);
239 function_entry
*get_function_signature(ir_function_signature
*sig
);
241 src_reg
get_temp(const glsl_type
*type
);
242 void reladdr_to_temp(ir_instruction
*ir
, src_reg
*reg
, int *num_reladdr
);
244 src_reg
src_reg_for_float(float val
);
247 * \name Visit methods
249 * As typical for the visitor pattern, there must be one \c visit method for
250 * each concrete subclass of \c ir_instruction. Virtual base classes within
251 * the hierarchy should not have \c visit methods.
254 virtual void visit(ir_variable
*);
255 virtual void visit(ir_loop
*);
256 virtual void visit(ir_loop_jump
*);
257 virtual void visit(ir_function_signature
*);
258 virtual void visit(ir_function
*);
259 virtual void visit(ir_expression
*);
260 virtual void visit(ir_swizzle
*);
261 virtual void visit(ir_dereference_variable
*);
262 virtual void visit(ir_dereference_array
*);
263 virtual void visit(ir_dereference_record
*);
264 virtual void visit(ir_assignment
*);
265 virtual void visit(ir_constant
*);
266 virtual void visit(ir_call
*);
267 virtual void visit(ir_return
*);
268 virtual void visit(ir_discard
*);
269 virtual void visit(ir_texture
*);
270 virtual void visit(ir_if
*);
275 /** List of variable_storage */
278 /** List of function_entry */
279 exec_list function_signatures
;
280 int next_signature_id
;
282 /** List of ir_to_mesa_instruction */
283 exec_list instructions
;
285 ir_to_mesa_instruction
*emit(ir_instruction
*ir
, enum prog_opcode op
);
287 ir_to_mesa_instruction
*emit(ir_instruction
*ir
, enum prog_opcode op
,
288 dst_reg dst
, src_reg src0
);
290 ir_to_mesa_instruction
*emit(ir_instruction
*ir
, enum prog_opcode op
,
291 dst_reg dst
, src_reg src0
, src_reg src1
);
293 ir_to_mesa_instruction
*emit(ir_instruction
*ir
, enum prog_opcode op
,
295 src_reg src0
, src_reg src1
, src_reg src2
);
298 * Emit the correct dot-product instruction for the type of arguments
300 void emit_dp(ir_instruction
*ir
,
306 void emit_scalar(ir_instruction
*ir
, enum prog_opcode op
,
307 dst_reg dst
, src_reg src0
);
309 void emit_scalar(ir_instruction
*ir
, enum prog_opcode op
,
310 dst_reg dst
, src_reg src0
, src_reg src1
);
312 void emit_scs(ir_instruction
*ir
, enum prog_opcode op
,
313 dst_reg dst
, const src_reg
&src
);
315 GLboolean
try_emit_mad(ir_expression
*ir
,
317 GLboolean
try_emit_sat(ir_expression
*ir
);
319 void emit_swz(ir_expression
*ir
);
321 bool process_move_condition(ir_rvalue
*ir
);
323 void copy_propagate(void);
328 src_reg undef_src
= src_reg(PROGRAM_UNDEFINED
, 0, NULL
);
330 dst_reg undef_dst
= dst_reg(PROGRAM_UNDEFINED
, SWIZZLE_NOOP
);
332 dst_reg address_reg
= dst_reg(PROGRAM_ADDRESS
, WRITEMASK_X
);
335 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...) PRINTFLIKE(2, 3);
338 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...)
342 ralloc_vasprintf_append(&prog
->InfoLog
, fmt
, args
);
345 prog
->LinkStatus
= GL_FALSE
;
349 swizzle_for_size(int size
)
351 int size_swizzles
[4] = {
352 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
),
353 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
),
354 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
),
355 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
358 assert((size
>= 1) && (size
<= 4));
359 return size_swizzles
[size
- 1];
362 ir_to_mesa_instruction
*
363 ir_to_mesa_visitor::emit(ir_instruction
*ir
, enum prog_opcode op
,
365 src_reg src0
, src_reg src1
, src_reg src2
)
367 ir_to_mesa_instruction
*inst
= new(mem_ctx
) ir_to_mesa_instruction();
370 /* If we have to do relative addressing, we want to load the ARL
371 * reg directly for one of the regs, and preload the other reladdr
372 * sources into temps.
374 num_reladdr
+= dst
.reladdr
!= NULL
;
375 num_reladdr
+= src0
.reladdr
!= NULL
;
376 num_reladdr
+= src1
.reladdr
!= NULL
;
377 num_reladdr
+= src2
.reladdr
!= NULL
;
379 reladdr_to_temp(ir
, &src2
, &num_reladdr
);
380 reladdr_to_temp(ir
, &src1
, &num_reladdr
);
381 reladdr_to_temp(ir
, &src0
, &num_reladdr
);
384 emit(ir
, OPCODE_ARL
, address_reg
, *dst
.reladdr
);
387 assert(num_reladdr
== 0);
396 inst
->function
= NULL
;
398 this->instructions
.push_tail(inst
);
404 ir_to_mesa_instruction
*
405 ir_to_mesa_visitor::emit(ir_instruction
*ir
, enum prog_opcode op
,
406 dst_reg dst
, src_reg src0
, src_reg src1
)
408 return emit(ir
, op
, dst
, src0
, src1
, undef_src
);
411 ir_to_mesa_instruction
*
412 ir_to_mesa_visitor::emit(ir_instruction
*ir
, enum prog_opcode op
,
413 dst_reg dst
, src_reg src0
)
415 assert(dst
.writemask
!= 0);
416 return emit(ir
, op
, dst
, src0
, undef_src
, undef_src
);
419 ir_to_mesa_instruction
*
420 ir_to_mesa_visitor::emit(ir_instruction
*ir
, enum prog_opcode op
)
422 return emit(ir
, op
, undef_dst
, undef_src
, undef_src
, undef_src
);
426 ir_to_mesa_visitor::emit_dp(ir_instruction
*ir
,
427 dst_reg dst
, src_reg src0
, src_reg src1
,
430 static const gl_inst_opcode dot_opcodes
[] = {
431 OPCODE_DP2
, OPCODE_DP3
, OPCODE_DP4
434 emit(ir
, dot_opcodes
[elements
- 2], dst
, src0
, src1
);
438 * Emits Mesa scalar opcodes to produce unique answers across channels.
440 * Some Mesa opcodes are scalar-only, like ARB_fp/vp. The src X
441 * channel determines the result across all channels. So to do a vec4
442 * of this operation, we want to emit a scalar per source channel used
443 * to produce dest channels.
446 ir_to_mesa_visitor::emit_scalar(ir_instruction
*ir
, enum prog_opcode op
,
448 src_reg orig_src0
, src_reg orig_src1
)
451 int done_mask
= ~dst
.writemask
;
453 /* Mesa RCP is a scalar operation splatting results to all channels,
454 * like ARB_fp/vp. So emit as many RCPs as necessary to cover our
457 for (i
= 0; i
< 4; i
++) {
458 GLuint this_mask
= (1 << i
);
459 ir_to_mesa_instruction
*inst
;
460 src_reg src0
= orig_src0
;
461 src_reg src1
= orig_src1
;
463 if (done_mask
& this_mask
)
466 GLuint src0_swiz
= GET_SWZ(src0
.swizzle
, i
);
467 GLuint src1_swiz
= GET_SWZ(src1
.swizzle
, i
);
468 for (j
= i
+ 1; j
< 4; j
++) {
469 /* If there is another enabled component in the destination that is
470 * derived from the same inputs, generate its value on this pass as
473 if (!(done_mask
& (1 << j
)) &&
474 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
&&
475 GET_SWZ(src1
.swizzle
, j
) == src1_swiz
) {
476 this_mask
|= (1 << j
);
479 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
480 src0_swiz
, src0_swiz
);
481 src1
.swizzle
= MAKE_SWIZZLE4(src1_swiz
, src1_swiz
,
482 src1_swiz
, src1_swiz
);
484 inst
= emit(ir
, op
, dst
, src0
, src1
);
485 inst
->dst
.writemask
= this_mask
;
486 done_mask
|= this_mask
;
491 ir_to_mesa_visitor::emit_scalar(ir_instruction
*ir
, enum prog_opcode op
,
492 dst_reg dst
, src_reg src0
)
494 src_reg undef
= undef_src
;
496 undef
.swizzle
= SWIZZLE_XXXX
;
498 emit_scalar(ir
, op
, dst
, src0
, undef
);
502 * Emit an OPCODE_SCS instruction
504 * The \c SCS opcode functions a bit differently than the other Mesa (or
505 * ARB_fragment_program) opcodes. Instead of splatting its result across all
506 * four components of the destination, it writes one value to the \c x
507 * component and another value to the \c y component.
509 * \param ir IR instruction being processed
510 * \param op Either \c OPCODE_SIN or \c OPCODE_COS depending on which
512 * \param dst Destination register
513 * \param src Source register
516 ir_to_mesa_visitor::emit_scs(ir_instruction
*ir
, enum prog_opcode op
,
520 /* Vertex programs cannot use the SCS opcode.
522 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
523 emit_scalar(ir
, op
, dst
, src
);
527 const unsigned component
= (op
== OPCODE_SIN
) ? 0 : 1;
528 const unsigned scs_mask
= (1U << component
);
529 int done_mask
= ~dst
.writemask
;
532 assert(op
== OPCODE_SIN
|| op
== OPCODE_COS
);
534 /* If there are compnents in the destination that differ from the component
535 * that will be written by the SCS instrution, we'll need a temporary.
537 if (scs_mask
!= unsigned(dst
.writemask
)) {
538 tmp
= get_temp(glsl_type::vec4_type
);
541 for (unsigned i
= 0; i
< 4; i
++) {
542 unsigned this_mask
= (1U << i
);
545 if ((done_mask
& this_mask
) != 0)
548 /* The source swizzle specified which component of the source generates
549 * sine / cosine for the current component in the destination. The SCS
550 * instruction requires that this value be swizzle to the X component.
551 * Replace the current swizzle with a swizzle that puts the source in
554 unsigned src0_swiz
= GET_SWZ(src
.swizzle
, i
);
556 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
557 src0_swiz
, src0_swiz
);
558 for (unsigned j
= i
+ 1; j
< 4; j
++) {
559 /* If there is another enabled component in the destination that is
560 * derived from the same inputs, generate its value on this pass as
563 if (!(done_mask
& (1 << j
)) &&
564 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
) {
565 this_mask
|= (1 << j
);
569 if (this_mask
!= scs_mask
) {
570 ir_to_mesa_instruction
*inst
;
571 dst_reg tmp_dst
= dst_reg(tmp
);
573 /* Emit the SCS instruction.
575 inst
= emit(ir
, OPCODE_SCS
, tmp_dst
, src0
);
576 inst
->dst
.writemask
= scs_mask
;
578 /* Move the result of the SCS instruction to the desired location in
581 tmp
.swizzle
= MAKE_SWIZZLE4(component
, component
,
582 component
, component
);
583 inst
= emit(ir
, OPCODE_SCS
, dst
, tmp
);
584 inst
->dst
.writemask
= this_mask
;
586 /* Emit the SCS instruction to write directly to the destination.
588 ir_to_mesa_instruction
*inst
= emit(ir
, OPCODE_SCS
, dst
, src0
);
589 inst
->dst
.writemask
= scs_mask
;
592 done_mask
|= this_mask
;
597 ir_to_mesa_visitor::src_reg_for_float(float val
)
599 src_reg
src(PROGRAM_CONSTANT
, -1, NULL
);
601 src
.index
= _mesa_add_unnamed_constant(this->prog
->Parameters
,
602 &val
, 1, &src
.swizzle
);
608 type_size(const struct glsl_type
*type
)
613 switch (type
->base_type
) {
616 case GLSL_TYPE_FLOAT
:
618 if (type
->is_matrix()) {
619 return type
->matrix_columns
;
621 /* Regardless of size of vector, it gets a vec4. This is bad
622 * packing for things like floats, but otherwise arrays become a
623 * mess. Hopefully a later pass over the code can pack scalars
624 * down if appropriate.
628 case GLSL_TYPE_ARRAY
:
629 assert(type
->length
> 0);
630 return type_size(type
->fields
.array
) * type
->length
;
631 case GLSL_TYPE_STRUCT
:
633 for (i
= 0; i
< type
->length
; i
++) {
634 size
+= type_size(type
->fields
.structure
[i
].type
);
637 case GLSL_TYPE_SAMPLER
:
638 /* Samplers take up one slot in UNIFORMS[], but they're baked in
649 * In the initial pass of codegen, we assign temporary numbers to
650 * intermediate results. (not SSA -- variable assignments will reuse
651 * storage). Actual register allocation for the Mesa VM occurs in a
652 * pass over the Mesa IR later.
655 ir_to_mesa_visitor::get_temp(const glsl_type
*type
)
661 src
.file
= PROGRAM_TEMPORARY
;
662 src
.index
= next_temp
;
664 next_temp
+= type_size(type
);
666 if (type
->is_array() || type
->is_record()) {
667 src
.swizzle
= SWIZZLE_NOOP
;
669 for (i
= 0; i
< type
->vector_elements
; i
++)
672 swizzle
[i
] = type
->vector_elements
- 1;
673 src
.swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1],
674 swizzle
[2], swizzle
[3]);
682 ir_to_mesa_visitor::find_variable_storage(ir_variable
*var
)
685 variable_storage
*entry
;
687 foreach_iter(exec_list_iterator
, iter
, this->variables
) {
688 entry
= (variable_storage
*)iter
.get();
690 if (entry
->var
== var
)
698 ir_to_mesa_visitor::visit(ir_variable
*ir
)
700 if (strcmp(ir
->name
, "gl_FragCoord") == 0) {
701 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
703 fp
->OriginUpperLeft
= ir
->origin_upper_left
;
704 fp
->PixelCenterInteger
= ir
->pixel_center_integer
;
706 } else if (strcmp(ir
->name
, "gl_FragDepth") == 0) {
707 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
708 switch (ir
->depth_layout
) {
709 case ir_depth_layout_none
:
710 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_NONE
;
712 case ir_depth_layout_any
:
713 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_ANY
;
715 case ir_depth_layout_greater
:
716 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_GREATER
;
718 case ir_depth_layout_less
:
719 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_LESS
;
721 case ir_depth_layout_unchanged
:
722 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
730 if (ir
->mode
== ir_var_uniform
&& strncmp(ir
->name
, "gl_", 3) == 0) {
732 const ir_state_slot
*const slots
= ir
->state_slots
;
733 assert(ir
->state_slots
!= NULL
);
735 /* Check if this statevar's setup in the STATE file exactly
736 * matches how we'll want to reference it as a
737 * struct/array/whatever. If not, then we need to move it into
738 * temporary storage and hope that it'll get copy-propagated
741 for (i
= 0; i
< ir
->num_state_slots
; i
++) {
742 if (slots
[i
].swizzle
!= SWIZZLE_XYZW
) {
747 struct variable_storage
*storage
;
749 if (i
== ir
->num_state_slots
) {
750 /* We'll set the index later. */
751 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_STATE_VAR
, -1);
752 this->variables
.push_tail(storage
);
756 /* The variable_storage constructor allocates slots based on the size
757 * of the type. However, this had better match the number of state
758 * elements that we're going to copy into the new temporary.
760 assert((int) ir
->num_state_slots
== type_size(ir
->type
));
762 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_TEMPORARY
,
764 this->variables
.push_tail(storage
);
765 this->next_temp
+= type_size(ir
->type
);
767 dst
= dst_reg(src_reg(PROGRAM_TEMPORARY
, storage
->index
, NULL
));
771 for (unsigned int i
= 0; i
< ir
->num_state_slots
; i
++) {
772 int index
= _mesa_add_state_reference(this->prog
->Parameters
,
773 (gl_state_index
*)slots
[i
].tokens
);
775 if (storage
->file
== PROGRAM_STATE_VAR
) {
776 if (storage
->index
== -1) {
777 storage
->index
= index
;
779 assert(index
== storage
->index
+ (int)i
);
782 src_reg
src(PROGRAM_STATE_VAR
, index
, NULL
);
783 src
.swizzle
= slots
[i
].swizzle
;
784 emit(ir
, OPCODE_MOV
, dst
, src
);
785 /* even a float takes up a whole vec4 reg in a struct/array. */
790 if (storage
->file
== PROGRAM_TEMPORARY
&&
791 dst
.index
!= storage
->index
+ (int) ir
->num_state_slots
) {
792 fail_link(this->shader_program
,
793 "failed to load builtin uniform `%s' (%d/%d regs loaded)\n",
794 ir
->name
, dst
.index
- storage
->index
,
795 type_size(ir
->type
));
801 ir_to_mesa_visitor::visit(ir_loop
*ir
)
803 ir_dereference_variable
*counter
= NULL
;
805 if (ir
->counter
!= NULL
)
806 counter
= new(mem_ctx
) ir_dereference_variable(ir
->counter
);
808 if (ir
->from
!= NULL
) {
809 assert(ir
->counter
!= NULL
);
812 new(mem_ctx
) ir_assignment(counter
, ir
->from
, NULL
);
817 emit(NULL
, OPCODE_BGNLOOP
);
821 new(mem_ctx
) ir_expression(ir
->cmp
, glsl_type::bool_type
,
823 ir_if
*if_stmt
= new(mem_ctx
) ir_if(e
);
826 new(mem_ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
828 if_stmt
->then_instructions
.push_tail(brk
);
830 if_stmt
->accept(this);
833 visit_exec_list(&ir
->body_instructions
, this);
837 new(mem_ctx
) ir_expression(ir_binop_add
, counter
->type
,
838 counter
, ir
->increment
);
841 new(mem_ctx
) ir_assignment(counter
, e
, NULL
);
846 emit(NULL
, OPCODE_ENDLOOP
);
850 ir_to_mesa_visitor::visit(ir_loop_jump
*ir
)
853 case ir_loop_jump::jump_break
:
854 emit(NULL
, OPCODE_BRK
);
856 case ir_loop_jump::jump_continue
:
857 emit(NULL
, OPCODE_CONT
);
864 ir_to_mesa_visitor::visit(ir_function_signature
*ir
)
871 ir_to_mesa_visitor::visit(ir_function
*ir
)
873 /* Ignore function bodies other than main() -- we shouldn't see calls to
874 * them since they should all be inlined before we get to ir_to_mesa.
876 if (strcmp(ir
->name
, "main") == 0) {
877 const ir_function_signature
*sig
;
880 sig
= ir
->matching_signature(&empty
);
884 foreach_iter(exec_list_iterator
, iter
, sig
->body
) {
885 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
893 ir_to_mesa_visitor::try_emit_mad(ir_expression
*ir
, int mul_operand
)
895 int nonmul_operand
= 1 - mul_operand
;
898 ir_expression
*expr
= ir
->operands
[mul_operand
]->as_expression();
899 if (!expr
|| expr
->operation
!= ir_binop_mul
)
902 expr
->operands
[0]->accept(this);
904 expr
->operands
[1]->accept(this);
906 ir
->operands
[nonmul_operand
]->accept(this);
909 this->result
= get_temp(ir
->type
);
910 emit(ir
, OPCODE_MAD
, dst_reg(this->result
), a
, b
, c
);
916 ir_to_mesa_visitor::try_emit_sat(ir_expression
*ir
)
918 /* Saturates were only introduced to vertex programs in
919 * NV_vertex_program3, so don't give them to drivers in the VP.
921 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
)
924 ir_rvalue
*sat_src
= ir
->as_rvalue_to_saturate();
928 sat_src
->accept(this);
929 src_reg src
= this->result
;
931 this->result
= get_temp(ir
->type
);
932 ir_to_mesa_instruction
*inst
;
933 inst
= emit(ir
, OPCODE_MOV
, dst_reg(this->result
), src
);
934 inst
->saturate
= true;
940 ir_to_mesa_visitor::reladdr_to_temp(ir_instruction
*ir
,
941 src_reg
*reg
, int *num_reladdr
)
946 emit(ir
, OPCODE_ARL
, address_reg
, *reg
->reladdr
);
948 if (*num_reladdr
!= 1) {
949 src_reg temp
= get_temp(glsl_type::vec4_type
);
951 emit(ir
, OPCODE_MOV
, dst_reg(temp
), *reg
);
959 ir_to_mesa_visitor::emit_swz(ir_expression
*ir
)
961 /* Assume that the vector operator is in a form compatible with OPCODE_SWZ.
962 * This means that each of the operands is either an immediate value of -1,
963 * 0, or 1, or is a component from one source register (possibly with
966 uint8_t components
[4] = { 0 };
967 bool negate
[4] = { false };
968 ir_variable
*var
= NULL
;
970 for (unsigned i
= 0; i
< ir
->type
->vector_elements
; i
++) {
971 ir_rvalue
*op
= ir
->operands
[i
];
973 assert(op
->type
->is_scalar());
976 switch (op
->ir_type
) {
977 case ir_type_constant
: {
979 assert(op
->type
->is_scalar());
981 const ir_constant
*const c
= op
->as_constant();
983 components
[i
] = SWIZZLE_ONE
;
984 } else if (c
->is_zero()) {
985 components
[i
] = SWIZZLE_ZERO
;
986 } else if (c
->is_negative_one()) {
987 components
[i
] = SWIZZLE_ONE
;
990 assert(!"SWZ constant must be 0.0 or 1.0.");
997 case ir_type_dereference_variable
: {
998 ir_dereference_variable
*const deref
=
999 (ir_dereference_variable
*) op
;
1001 assert((var
== NULL
) || (deref
->var
== var
));
1002 components
[i
] = SWIZZLE_X
;
1008 case ir_type_expression
: {
1009 ir_expression
*const expr
= (ir_expression
*) op
;
1011 assert(expr
->operation
== ir_unop_neg
);
1014 op
= expr
->operands
[0];
1018 case ir_type_swizzle
: {
1019 ir_swizzle
*const swiz
= (ir_swizzle
*) op
;
1021 components
[i
] = swiz
->mask
.x
;
1027 assert(!"Should not get here.");
1033 assert(var
!= NULL
);
1035 ir_dereference_variable
*const deref
=
1036 new(mem_ctx
) ir_dereference_variable(var
);
1038 this->result
.file
= PROGRAM_UNDEFINED
;
1039 deref
->accept(this);
1040 if (this->result
.file
== PROGRAM_UNDEFINED
) {
1042 printf("Failed to get tree for expression operand:\n");
1050 src
.swizzle
= MAKE_SWIZZLE4(components
[0],
1054 src
.negate
= ((unsigned(negate
[0]) << 0)
1055 | (unsigned(negate
[1]) << 1)
1056 | (unsigned(negate
[2]) << 2)
1057 | (unsigned(negate
[3]) << 3));
1059 /* Storage for our result. Ideally for an assignment we'd be using the
1060 * actual storage for the result here, instead.
1062 const src_reg result_src
= get_temp(ir
->type
);
1063 dst_reg result_dst
= dst_reg(result_src
);
1065 /* Limit writes to the channels that will be used by result_src later.
1066 * This does limit this temp's use as a temporary for multi-instruction
1069 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1071 emit(ir
, OPCODE_SWZ
, result_dst
, src
);
1072 this->result
= result_src
;
1076 ir_to_mesa_visitor::visit(ir_expression
*ir
)
1078 unsigned int operand
;
1079 src_reg op
[Elements(ir
->operands
)];
1083 /* Quick peephole: Emit OPCODE_MAD(a, b, c) instead of ADD(MUL(a, b), c)
1085 if (ir
->operation
== ir_binop_add
) {
1086 if (try_emit_mad(ir
, 1))
1088 if (try_emit_mad(ir
, 0))
1091 if (try_emit_sat(ir
))
1094 if (ir
->operation
== ir_quadop_vector
) {
1099 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
1100 this->result
.file
= PROGRAM_UNDEFINED
;
1101 ir
->operands
[operand
]->accept(this);
1102 if (this->result
.file
== PROGRAM_UNDEFINED
) {
1104 printf("Failed to get tree for expression operand:\n");
1105 ir
->operands
[operand
]->accept(&v
);
1108 op
[operand
] = this->result
;
1110 /* Matrix expression operands should have been broken down to vector
1111 * operations already.
1113 assert(!ir
->operands
[operand
]->type
->is_matrix());
1116 int vector_elements
= ir
->operands
[0]->type
->vector_elements
;
1117 if (ir
->operands
[1]) {
1118 vector_elements
= MAX2(vector_elements
,
1119 ir
->operands
[1]->type
->vector_elements
);
1122 this->result
.file
= PROGRAM_UNDEFINED
;
1124 /* Storage for our result. Ideally for an assignment we'd be using
1125 * the actual storage for the result here, instead.
1127 result_src
= get_temp(ir
->type
);
1128 /* convenience for the emit functions below. */
1129 result_dst
= dst_reg(result_src
);
1130 /* Limit writes to the channels that will be used by result_src later.
1131 * This does limit this temp's use as a temporary for multi-instruction
1134 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1136 switch (ir
->operation
) {
1137 case ir_unop_logic_not
:
1138 emit(ir
, OPCODE_SEQ
, result_dst
, op
[0], src_reg_for_float(0.0));
1141 op
[0].negate
= ~op
[0].negate
;
1145 emit(ir
, OPCODE_ABS
, result_dst
, op
[0]);
1148 emit(ir
, OPCODE_SSG
, result_dst
, op
[0]);
1151 emit_scalar(ir
, OPCODE_RCP
, result_dst
, op
[0]);
1155 emit_scalar(ir
, OPCODE_EX2
, result_dst
, op
[0]);
1159 assert(!"not reached: should be handled by ir_explog_to_explog2");
1162 emit_scalar(ir
, OPCODE_LG2
, result_dst
, op
[0]);
1165 emit_scalar(ir
, OPCODE_SIN
, result_dst
, op
[0]);
1168 emit_scalar(ir
, OPCODE_COS
, result_dst
, op
[0]);
1170 case ir_unop_sin_reduced
:
1171 emit_scs(ir
, OPCODE_SIN
, result_dst
, op
[0]);
1173 case ir_unop_cos_reduced
:
1174 emit_scs(ir
, OPCODE_COS
, result_dst
, op
[0]);
1178 emit(ir
, OPCODE_DDX
, result_dst
, op
[0]);
1181 emit(ir
, OPCODE_DDY
, result_dst
, op
[0]);
1184 case ir_unop_noise
: {
1185 const enum prog_opcode opcode
=
1186 prog_opcode(OPCODE_NOISE1
1187 + (ir
->operands
[0]->type
->vector_elements
) - 1);
1188 assert((opcode
>= OPCODE_NOISE1
) && (opcode
<= OPCODE_NOISE4
));
1190 emit(ir
, opcode
, result_dst
, op
[0]);
1195 emit(ir
, OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1198 emit(ir
, OPCODE_SUB
, result_dst
, op
[0], op
[1]);
1202 emit(ir
, OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1205 assert(!"not reached: should be handled by ir_div_to_mul_rcp");
1207 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
1211 emit(ir
, OPCODE_SLT
, result_dst
, op
[0], op
[1]);
1213 case ir_binop_greater
:
1214 emit(ir
, OPCODE_SGT
, result_dst
, op
[0], op
[1]);
1216 case ir_binop_lequal
:
1217 emit(ir
, OPCODE_SLE
, result_dst
, op
[0], op
[1]);
1219 case ir_binop_gequal
:
1220 emit(ir
, OPCODE_SGE
, result_dst
, op
[0], op
[1]);
1222 case ir_binop_equal
:
1223 emit(ir
, OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1225 case ir_binop_nequal
:
1226 emit(ir
, OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1228 case ir_binop_all_equal
:
1229 /* "==" operator producing a scalar boolean. */
1230 if (ir
->operands
[0]->type
->is_vector() ||
1231 ir
->operands
[1]->type
->is_vector()) {
1232 src_reg temp
= get_temp(glsl_type::vec4_type
);
1233 emit(ir
, OPCODE_SNE
, dst_reg(temp
), op
[0], op
[1]);
1234 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1235 emit(ir
, OPCODE_SEQ
, result_dst
, result_src
, src_reg_for_float(0.0));
1237 emit(ir
, OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1240 case ir_binop_any_nequal
:
1241 /* "!=" operator producing a scalar boolean. */
1242 if (ir
->operands
[0]->type
->is_vector() ||
1243 ir
->operands
[1]->type
->is_vector()) {
1244 src_reg temp
= get_temp(glsl_type::vec4_type
);
1245 emit(ir
, OPCODE_SNE
, dst_reg(temp
), op
[0], op
[1]);
1246 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1247 emit(ir
, OPCODE_SNE
, result_dst
, result_src
, src_reg_for_float(0.0));
1249 emit(ir
, OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1254 assert(ir
->operands
[0]->type
->is_vector());
1255 emit_dp(ir
, result_dst
, op
[0], op
[0],
1256 ir
->operands
[0]->type
->vector_elements
);
1257 emit(ir
, OPCODE_SNE
, result_dst
, result_src
, src_reg_for_float(0.0));
1260 case ir_binop_logic_xor
:
1261 emit(ir
, OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1264 case ir_binop_logic_or
:
1265 /* This could be a saturated add and skip the SNE. */
1266 emit(ir
, OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1267 emit(ir
, OPCODE_SNE
, result_dst
, result_src
, src_reg_for_float(0.0));
1270 case ir_binop_logic_and
:
1271 /* the bool args are stored as float 0.0 or 1.0, so "mul" gives us "and". */
1272 emit(ir
, OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1276 assert(ir
->operands
[0]->type
->is_vector());
1277 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
1278 emit_dp(ir
, result_dst
, op
[0], op
[1],
1279 ir
->operands
[0]->type
->vector_elements
);
1283 /* sqrt(x) = x * rsq(x). */
1284 emit_scalar(ir
, OPCODE_RSQ
, result_dst
, op
[0]);
1285 emit(ir
, OPCODE_MUL
, result_dst
, result_src
, op
[0]);
1286 /* For incoming channels <= 0, set the result to 0. */
1287 op
[0].negate
= ~op
[0].negate
;
1288 emit(ir
, OPCODE_CMP
, result_dst
,
1289 op
[0], result_src
, src_reg_for_float(0.0));
1292 emit_scalar(ir
, OPCODE_RSQ
, result_dst
, op
[0]);
1300 /* Mesa IR lacks types, ints are stored as truncated floats. */
1304 emit(ir
, OPCODE_TRUNC
, result_dst
, op
[0]);
1308 emit(ir
, OPCODE_SNE
, result_dst
,
1309 op
[0], src_reg_for_float(0.0));
1312 emit(ir
, OPCODE_TRUNC
, result_dst
, op
[0]);
1315 op
[0].negate
= ~op
[0].negate
;
1316 emit(ir
, OPCODE_FLR
, result_dst
, op
[0]);
1317 result_src
.negate
= ~result_src
.negate
;
1320 emit(ir
, OPCODE_FLR
, result_dst
, op
[0]);
1323 emit(ir
, OPCODE_FRC
, result_dst
, op
[0]);
1327 emit(ir
, OPCODE_MIN
, result_dst
, op
[0], op
[1]);
1330 emit(ir
, OPCODE_MAX
, result_dst
, op
[0], op
[1]);
1333 emit_scalar(ir
, OPCODE_POW
, result_dst
, op
[0], op
[1]);
1336 case ir_unop_bit_not
:
1337 case ir_binop_lshift
:
1338 case ir_binop_rshift
:
1339 case ir_binop_bit_and
:
1340 case ir_binop_bit_xor
:
1341 case ir_binop_bit_or
:
1342 case ir_unop_round_even
:
1343 assert(!"GLSL 1.30 features unsupported");
1346 case ir_quadop_vector
:
1347 /* This operation should have already been handled.
1349 assert(!"Should not get here.");
1353 this->result
= result_src
;
1358 ir_to_mesa_visitor::visit(ir_swizzle
*ir
)
1364 /* Note that this is only swizzles in expressions, not those on the left
1365 * hand side of an assignment, which do write masking. See ir_assignment
1369 ir
->val
->accept(this);
1371 assert(src
.file
!= PROGRAM_UNDEFINED
);
1373 for (i
= 0; i
< 4; i
++) {
1374 if (i
< ir
->type
->vector_elements
) {
1377 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.x
);
1380 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.y
);
1383 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.z
);
1386 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.w
);
1390 /* If the type is smaller than a vec4, replicate the last
1393 swizzle
[i
] = swizzle
[ir
->type
->vector_elements
- 1];
1397 src
.swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
1403 ir_to_mesa_visitor::visit(ir_dereference_variable
*ir
)
1405 variable_storage
*entry
= find_variable_storage(ir
->var
);
1406 ir_variable
*var
= ir
->var
;
1409 switch (var
->mode
) {
1410 case ir_var_uniform
:
1411 entry
= new(mem_ctx
) variable_storage(var
, PROGRAM_UNIFORM
,
1413 this->variables
.push_tail(entry
);
1417 /* The linker assigns locations for varyings and attributes,
1418 * including deprecated builtins (like gl_Color),
1419 * user-assigned generic attributes (glBindVertexLocation),
1420 * and user-defined varyings.
1422 * FINISHME: We would hit this path for function arguments. Fix!
1424 assert(var
->location
!= -1);
1425 entry
= new(mem_ctx
) variable_storage(var
,
1428 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
&&
1429 var
->location
>= VERT_ATTRIB_GENERIC0
) {
1430 _mesa_add_attribute(this->prog
->Attributes
,
1432 _mesa_sizeof_glsl_type(var
->type
->gl_type
),
1434 var
->location
- VERT_ATTRIB_GENERIC0
);
1438 assert(var
->location
!= -1);
1439 entry
= new(mem_ctx
) variable_storage(var
,
1443 case ir_var_system_value
:
1444 entry
= new(mem_ctx
) variable_storage(var
,
1445 PROGRAM_SYSTEM_VALUE
,
1449 case ir_var_temporary
:
1450 entry
= new(mem_ctx
) variable_storage(var
, PROGRAM_TEMPORARY
,
1452 this->variables
.push_tail(entry
);
1454 next_temp
+= type_size(var
->type
);
1459 printf("Failed to make storage for %s\n", var
->name
);
1464 this->result
= src_reg(entry
->file
, entry
->index
, var
->type
);
1468 ir_to_mesa_visitor::visit(ir_dereference_array
*ir
)
1472 int element_size
= type_size(ir
->type
);
1474 index
= ir
->array_index
->constant_expression_value();
1476 ir
->array
->accept(this);
1480 src
.index
+= index
->value
.i
[0] * element_size
;
1482 /* Variable index array dereference. It eats the "vec4" of the
1483 * base of the array and an index that offsets the Mesa register
1486 ir
->array_index
->accept(this);
1490 if (element_size
== 1) {
1491 index_reg
= this->result
;
1493 index_reg
= get_temp(glsl_type::float_type
);
1495 emit(ir
, OPCODE_MUL
, dst_reg(index_reg
),
1496 this->result
, src_reg_for_float(element_size
));
1499 /* If there was already a relative address register involved, add the
1500 * new and the old together to get the new offset.
1502 if (src
.reladdr
!= NULL
) {
1503 src_reg accum_reg
= get_temp(glsl_type::float_type
);
1505 emit(ir
, OPCODE_ADD
, dst_reg(accum_reg
),
1506 index_reg
, *src
.reladdr
);
1508 index_reg
= accum_reg
;
1511 src
.reladdr
= ralloc(mem_ctx
, src_reg
);
1512 memcpy(src
.reladdr
, &index_reg
, sizeof(index_reg
));
1515 /* If the type is smaller than a vec4, replicate the last channel out. */
1516 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
1517 src
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1519 src
.swizzle
= SWIZZLE_NOOP
;
1525 ir_to_mesa_visitor::visit(ir_dereference_record
*ir
)
1528 const glsl_type
*struct_type
= ir
->record
->type
;
1531 ir
->record
->accept(this);
1533 for (i
= 0; i
< struct_type
->length
; i
++) {
1534 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
1536 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
1539 /* If the type is smaller than a vec4, replicate the last channel out. */
1540 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
1541 this->result
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1543 this->result
.swizzle
= SWIZZLE_NOOP
;
1545 this->result
.index
+= offset
;
1549 * We want to be careful in assignment setup to hit the actual storage
1550 * instead of potentially using a temporary like we might with the
1551 * ir_dereference handler.
1554 get_assignment_lhs(ir_dereference
*ir
, ir_to_mesa_visitor
*v
)
1556 /* The LHS must be a dereference. If the LHS is a variable indexed array
1557 * access of a vector, it must be separated into a series conditional moves
1558 * before reaching this point (see ir_vec_index_to_cond_assign).
1560 assert(ir
->as_dereference());
1561 ir_dereference_array
*deref_array
= ir
->as_dereference_array();
1563 assert(!deref_array
->array
->type
->is_vector());
1566 /* Use the rvalue deref handler for the most part. We'll ignore
1567 * swizzles in it and write swizzles using writemask, though.
1570 return dst_reg(v
->result
);
1574 * Process the condition of a conditional assignment
1576 * Examines the condition of a conditional assignment to generate the optimal
1577 * first operand of a \c CMP instruction. If the condition is a relational
1578 * operator with 0 (e.g., \c ir_binop_less), the value being compared will be
1579 * used as the source for the \c CMP instruction. Otherwise the comparison
1580 * is processed to a boolean result, and the boolean result is used as the
1581 * operand to the CMP instruction.
1584 ir_to_mesa_visitor::process_move_condition(ir_rvalue
*ir
)
1586 ir_rvalue
*src_ir
= ir
;
1588 bool switch_order
= false;
1590 ir_expression
*const expr
= ir
->as_expression();
1591 if ((expr
!= NULL
) && (expr
->get_num_operands() == 2)) {
1592 bool zero_on_left
= false;
1594 if (expr
->operands
[0]->is_zero()) {
1595 src_ir
= expr
->operands
[1];
1596 zero_on_left
= true;
1597 } else if (expr
->operands
[1]->is_zero()) {
1598 src_ir
= expr
->operands
[0];
1599 zero_on_left
= false;
1603 * (a < 0) T F F ( a < 0) T F F
1604 * (0 < a) F F T (-a < 0) F F T
1605 * (a <= 0) T T F (-a < 0) F F T (swap order of other operands)
1606 * (0 <= a) F T T ( a < 0) T F F (swap order of other operands)
1607 * (a > 0) F F T (-a < 0) F F T
1608 * (0 > a) T F F ( a < 0) T F F
1609 * (a >= 0) F T T ( a < 0) T F F (swap order of other operands)
1610 * (0 >= a) T T F (-a < 0) F F T (swap order of other operands)
1612 * Note that exchanging the order of 0 and 'a' in the comparison simply
1613 * means that the value of 'a' should be negated.
1616 switch (expr
->operation
) {
1618 switch_order
= false;
1619 negate
= zero_on_left
;
1622 case ir_binop_greater
:
1623 switch_order
= false;
1624 negate
= !zero_on_left
;
1627 case ir_binop_lequal
:
1628 switch_order
= true;
1629 negate
= !zero_on_left
;
1632 case ir_binop_gequal
:
1633 switch_order
= true;
1634 negate
= zero_on_left
;
1638 /* This isn't the right kind of comparison afterall, so make sure
1639 * the whole condition is visited.
1647 src_ir
->accept(this);
1649 /* We use the OPCODE_CMP (a < 0 ? b : c) for conditional moves, and the
1650 * condition we produced is 0.0 or 1.0. By flipping the sign, we can
1651 * choose which value OPCODE_CMP produces without an extra instruction
1652 * computing the condition.
1655 this->result
.negate
= ~this->result
.negate
;
1657 return switch_order
;
1661 ir_to_mesa_visitor::visit(ir_assignment
*ir
)
1667 ir
->rhs
->accept(this);
1670 l
= get_assignment_lhs(ir
->lhs
, this);
1672 /* FINISHME: This should really set to the correct maximal writemask for each
1673 * FINISHME: component written (in the loops below). This case can only
1674 * FINISHME: occur for matrices, arrays, and structures.
1676 if (ir
->write_mask
== 0) {
1677 assert(!ir
->lhs
->type
->is_scalar() && !ir
->lhs
->type
->is_vector());
1678 l
.writemask
= WRITEMASK_XYZW
;
1679 } else if (ir
->lhs
->type
->is_scalar()) {
1680 /* FINISHME: This hack makes writing to gl_FragDepth, which lives in the
1681 * FINISHME: W component of fragment shader output zero, work correctly.
1683 l
.writemask
= WRITEMASK_XYZW
;
1686 int first_enabled_chan
= 0;
1689 assert(ir
->lhs
->type
->is_vector());
1690 l
.writemask
= ir
->write_mask
;
1692 for (int i
= 0; i
< 4; i
++) {
1693 if (l
.writemask
& (1 << i
)) {
1694 first_enabled_chan
= GET_SWZ(r
.swizzle
, i
);
1699 /* Swizzle a small RHS vector into the channels being written.
1701 * glsl ir treats write_mask as dictating how many channels are
1702 * present on the RHS while Mesa IR treats write_mask as just
1703 * showing which channels of the vec4 RHS get written.
1705 for (int i
= 0; i
< 4; i
++) {
1706 if (l
.writemask
& (1 << i
))
1707 swizzles
[i
] = GET_SWZ(r
.swizzle
, rhs_chan
++);
1709 swizzles
[i
] = first_enabled_chan
;
1711 r
.swizzle
= MAKE_SWIZZLE4(swizzles
[0], swizzles
[1],
1712 swizzles
[2], swizzles
[3]);
1715 assert(l
.file
!= PROGRAM_UNDEFINED
);
1716 assert(r
.file
!= PROGRAM_UNDEFINED
);
1718 if (ir
->condition
) {
1719 const bool switch_order
= this->process_move_condition(ir
->condition
);
1720 src_reg condition
= this->result
;
1722 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
1724 emit(ir
, OPCODE_CMP
, l
, condition
, src_reg(l
), r
);
1726 emit(ir
, OPCODE_CMP
, l
, condition
, r
, src_reg(l
));
1733 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
1734 emit(ir
, OPCODE_MOV
, l
, r
);
1743 ir_to_mesa_visitor::visit(ir_constant
*ir
)
1746 GLfloat stack_vals
[4] = { 0 };
1747 GLfloat
*values
= stack_vals
;
1750 /* Unfortunately, 4 floats is all we can get into
1751 * _mesa_add_unnamed_constant. So, make a temp to store an
1752 * aggregate constant and move each constant value into it. If we
1753 * get lucky, copy propagation will eliminate the extra moves.
1756 if (ir
->type
->base_type
== GLSL_TYPE_STRUCT
) {
1757 src_reg temp_base
= get_temp(ir
->type
);
1758 dst_reg temp
= dst_reg(temp_base
);
1760 foreach_iter(exec_list_iterator
, iter
, ir
->components
) {
1761 ir_constant
*field_value
= (ir_constant
*)iter
.get();
1762 int size
= type_size(field_value
->type
);
1766 field_value
->accept(this);
1769 for (i
= 0; i
< (unsigned int)size
; i
++) {
1770 emit(ir
, OPCODE_MOV
, temp
, src
);
1776 this->result
= temp_base
;
1780 if (ir
->type
->is_array()) {
1781 src_reg temp_base
= get_temp(ir
->type
);
1782 dst_reg temp
= dst_reg(temp_base
);
1783 int size
= type_size(ir
->type
->fields
.array
);
1787 for (i
= 0; i
< ir
->type
->length
; i
++) {
1788 ir
->array_elements
[i
]->accept(this);
1790 for (int j
= 0; j
< size
; j
++) {
1791 emit(ir
, OPCODE_MOV
, temp
, src
);
1797 this->result
= temp_base
;
1801 if (ir
->type
->is_matrix()) {
1802 src_reg mat
= get_temp(ir
->type
);
1803 dst_reg mat_column
= dst_reg(mat
);
1805 for (i
= 0; i
< ir
->type
->matrix_columns
; i
++) {
1806 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
1807 values
= &ir
->value
.f
[i
* ir
->type
->vector_elements
];
1809 src
= src_reg(PROGRAM_CONSTANT
, -1, NULL
);
1810 src
.index
= _mesa_add_unnamed_constant(this->prog
->Parameters
,
1812 ir
->type
->vector_elements
,
1814 emit(ir
, OPCODE_MOV
, mat_column
, src
);
1823 src
.file
= PROGRAM_CONSTANT
;
1824 switch (ir
->type
->base_type
) {
1825 case GLSL_TYPE_FLOAT
:
1826 values
= &ir
->value
.f
[0];
1828 case GLSL_TYPE_UINT
:
1829 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1830 values
[i
] = ir
->value
.u
[i
];
1834 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1835 values
[i
] = ir
->value
.i
[i
];
1838 case GLSL_TYPE_BOOL
:
1839 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1840 values
[i
] = ir
->value
.b
[i
];
1844 assert(!"Non-float/uint/int/bool constant");
1847 this->result
= src_reg(PROGRAM_CONSTANT
, -1, ir
->type
);
1848 this->result
.index
= _mesa_add_unnamed_constant(this->prog
->Parameters
,
1850 ir
->type
->vector_elements
,
1851 &this->result
.swizzle
);
1855 ir_to_mesa_visitor::get_function_signature(ir_function_signature
*sig
)
1857 function_entry
*entry
;
1859 foreach_iter(exec_list_iterator
, iter
, this->function_signatures
) {
1860 entry
= (function_entry
*)iter
.get();
1862 if (entry
->sig
== sig
)
1866 entry
= ralloc(mem_ctx
, function_entry
);
1868 entry
->sig_id
= this->next_signature_id
++;
1869 entry
->bgn_inst
= NULL
;
1871 /* Allocate storage for all the parameters. */
1872 foreach_iter(exec_list_iterator
, iter
, sig
->parameters
) {
1873 ir_variable
*param
= (ir_variable
*)iter
.get();
1874 variable_storage
*storage
;
1876 storage
= find_variable_storage(param
);
1879 storage
= new(mem_ctx
) variable_storage(param
, PROGRAM_TEMPORARY
,
1881 this->variables
.push_tail(storage
);
1883 this->next_temp
+= type_size(param
->type
);
1886 if (!sig
->return_type
->is_void()) {
1887 entry
->return_reg
= get_temp(sig
->return_type
);
1889 entry
->return_reg
= undef_src
;
1892 this->function_signatures
.push_tail(entry
);
1897 ir_to_mesa_visitor::visit(ir_call
*ir
)
1899 ir_to_mesa_instruction
*call_inst
;
1900 ir_function_signature
*sig
= ir
->get_callee();
1901 function_entry
*entry
= get_function_signature(sig
);
1904 /* Process in parameters. */
1905 exec_list_iterator sig_iter
= sig
->parameters
.iterator();
1906 foreach_iter(exec_list_iterator
, iter
, *ir
) {
1907 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
1908 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
1910 if (param
->mode
== ir_var_in
||
1911 param
->mode
== ir_var_inout
) {
1912 variable_storage
*storage
= find_variable_storage(param
);
1915 param_rval
->accept(this);
1916 src_reg r
= this->result
;
1919 l
.file
= storage
->file
;
1920 l
.index
= storage
->index
;
1922 l
.writemask
= WRITEMASK_XYZW
;
1923 l
.cond_mask
= COND_TR
;
1925 for (i
= 0; i
< type_size(param
->type
); i
++) {
1926 emit(ir
, OPCODE_MOV
, l
, r
);
1934 assert(!sig_iter
.has_next());
1936 /* Emit call instruction */
1937 call_inst
= emit(ir
, OPCODE_CAL
);
1938 call_inst
->function
= entry
;
1940 /* Process out parameters. */
1941 sig_iter
= sig
->parameters
.iterator();
1942 foreach_iter(exec_list_iterator
, iter
, *ir
) {
1943 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
1944 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
1946 if (param
->mode
== ir_var_out
||
1947 param
->mode
== ir_var_inout
) {
1948 variable_storage
*storage
= find_variable_storage(param
);
1952 r
.file
= storage
->file
;
1953 r
.index
= storage
->index
;
1955 r
.swizzle
= SWIZZLE_NOOP
;
1958 param_rval
->accept(this);
1959 dst_reg l
= dst_reg(this->result
);
1961 for (i
= 0; i
< type_size(param
->type
); i
++) {
1962 emit(ir
, OPCODE_MOV
, l
, r
);
1970 assert(!sig_iter
.has_next());
1972 /* Process return value. */
1973 this->result
= entry
->return_reg
;
1977 ir_to_mesa_visitor::visit(ir_texture
*ir
)
1979 src_reg result_src
, coord
, lod_info
, projector
, dx
, dy
;
1980 dst_reg result_dst
, coord_dst
;
1981 ir_to_mesa_instruction
*inst
= NULL
;
1982 prog_opcode opcode
= OPCODE_NOP
;
1984 ir
->coordinate
->accept(this);
1986 /* Put our coords in a temp. We'll need to modify them for shadow,
1987 * projection, or LOD, so the only case we'd use it as is is if
1988 * we're doing plain old texturing. Mesa IR optimization should
1989 * handle cleaning up our mess in that case.
1991 coord
= get_temp(glsl_type::vec4_type
);
1992 coord_dst
= dst_reg(coord
);
1993 emit(ir
, OPCODE_MOV
, coord_dst
, this->result
);
1995 if (ir
->projector
) {
1996 ir
->projector
->accept(this);
1997 projector
= this->result
;
2000 /* Storage for our result. Ideally for an assignment we'd be using
2001 * the actual storage for the result here, instead.
2003 result_src
= get_temp(glsl_type::vec4_type
);
2004 result_dst
= dst_reg(result_src
);
2008 opcode
= OPCODE_TEX
;
2011 opcode
= OPCODE_TXB
;
2012 ir
->lod_info
.bias
->accept(this);
2013 lod_info
= this->result
;
2016 opcode
= OPCODE_TXL
;
2017 ir
->lod_info
.lod
->accept(this);
2018 lod_info
= this->result
;
2021 opcode
= OPCODE_TXD
;
2022 ir
->lod_info
.grad
.dPdx
->accept(this);
2024 ir
->lod_info
.grad
.dPdy
->accept(this);
2028 assert(!"GLSL 1.30 features unsupported");
2032 if (ir
->projector
) {
2033 if (opcode
== OPCODE_TEX
) {
2034 /* Slot the projector in as the last component of the coord. */
2035 coord_dst
.writemask
= WRITEMASK_W
;
2036 emit(ir
, OPCODE_MOV
, coord_dst
, projector
);
2037 coord_dst
.writemask
= WRITEMASK_XYZW
;
2038 opcode
= OPCODE_TXP
;
2040 src_reg coord_w
= coord
;
2041 coord_w
.swizzle
= SWIZZLE_WWWW
;
2043 /* For the other TEX opcodes there's no projective version
2044 * since the last slot is taken up by lod info. Do the
2045 * projective divide now.
2047 coord_dst
.writemask
= WRITEMASK_W
;
2048 emit(ir
, OPCODE_RCP
, coord_dst
, projector
);
2050 /* In the case where we have to project the coordinates "by hand,"
2051 * the shadow comparitor value must also be projected.
2053 src_reg tmp_src
= coord
;
2054 if (ir
->shadow_comparitor
) {
2055 /* Slot the shadow value in as the second to last component of the
2058 ir
->shadow_comparitor
->accept(this);
2060 tmp_src
= get_temp(glsl_type::vec4_type
);
2061 dst_reg tmp_dst
= dst_reg(tmp_src
);
2063 tmp_dst
.writemask
= WRITEMASK_Z
;
2064 emit(ir
, OPCODE_MOV
, tmp_dst
, this->result
);
2066 tmp_dst
.writemask
= WRITEMASK_XY
;
2067 emit(ir
, OPCODE_MOV
, tmp_dst
, coord
);
2070 coord_dst
.writemask
= WRITEMASK_XYZ
;
2071 emit(ir
, OPCODE_MUL
, coord_dst
, tmp_src
, coord_w
);
2073 coord_dst
.writemask
= WRITEMASK_XYZW
;
2074 coord
.swizzle
= SWIZZLE_XYZW
;
2078 /* If projection is done and the opcode is not OPCODE_TXP, then the shadow
2079 * comparitor was put in the correct place (and projected) by the code,
2080 * above, that handles by-hand projection.
2082 if (ir
->shadow_comparitor
&& (!ir
->projector
|| opcode
== OPCODE_TXP
)) {
2083 /* Slot the shadow value in as the second to last component of the
2086 ir
->shadow_comparitor
->accept(this);
2087 coord_dst
.writemask
= WRITEMASK_Z
;
2088 emit(ir
, OPCODE_MOV
, coord_dst
, this->result
);
2089 coord_dst
.writemask
= WRITEMASK_XYZW
;
2092 if (opcode
== OPCODE_TXL
|| opcode
== OPCODE_TXB
) {
2093 /* Mesa IR stores lod or lod bias in the last channel of the coords. */
2094 coord_dst
.writemask
= WRITEMASK_W
;
2095 emit(ir
, OPCODE_MOV
, coord_dst
, lod_info
);
2096 coord_dst
.writemask
= WRITEMASK_XYZW
;
2099 if (opcode
== OPCODE_TXD
)
2100 inst
= emit(ir
, opcode
, result_dst
, coord
, dx
, dy
);
2102 inst
= emit(ir
, opcode
, result_dst
, coord
);
2104 if (ir
->shadow_comparitor
)
2105 inst
->tex_shadow
= GL_TRUE
;
2107 inst
->sampler
= _mesa_get_sampler_uniform_value(ir
->sampler
,
2108 this->shader_program
,
2111 const glsl_type
*sampler_type
= ir
->sampler
->type
;
2113 switch (sampler_type
->sampler_dimensionality
) {
2114 case GLSL_SAMPLER_DIM_1D
:
2115 inst
->tex_target
= (sampler_type
->sampler_array
)
2116 ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
2118 case GLSL_SAMPLER_DIM_2D
:
2119 inst
->tex_target
= (sampler_type
->sampler_array
)
2120 ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
2122 case GLSL_SAMPLER_DIM_3D
:
2123 inst
->tex_target
= TEXTURE_3D_INDEX
;
2125 case GLSL_SAMPLER_DIM_CUBE
:
2126 inst
->tex_target
= TEXTURE_CUBE_INDEX
;
2128 case GLSL_SAMPLER_DIM_RECT
:
2129 inst
->tex_target
= TEXTURE_RECT_INDEX
;
2131 case GLSL_SAMPLER_DIM_BUF
:
2132 assert(!"FINISHME: Implement ARB_texture_buffer_object");
2135 assert(!"Should not get here.");
2138 this->result
= result_src
;
2142 ir_to_mesa_visitor::visit(ir_return
*ir
)
2144 if (ir
->get_value()) {
2148 assert(current_function
);
2150 ir
->get_value()->accept(this);
2151 src_reg r
= this->result
;
2153 l
= dst_reg(current_function
->return_reg
);
2155 for (i
= 0; i
< type_size(current_function
->sig
->return_type
); i
++) {
2156 emit(ir
, OPCODE_MOV
, l
, r
);
2162 emit(ir
, OPCODE_RET
);
2166 ir_to_mesa_visitor::visit(ir_discard
*ir
)
2168 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
2170 if (ir
->condition
) {
2171 ir
->condition
->accept(this);
2172 this->result
.negate
= ~this->result
.negate
;
2173 emit(ir
, OPCODE_KIL
, undef_dst
, this->result
);
2175 emit(ir
, OPCODE_KIL_NV
);
2178 fp
->UsesKill
= GL_TRUE
;
2182 ir_to_mesa_visitor::visit(ir_if
*ir
)
2184 ir_to_mesa_instruction
*cond_inst
, *if_inst
;
2185 ir_to_mesa_instruction
*prev_inst
;
2187 prev_inst
= (ir_to_mesa_instruction
*)this->instructions
.get_tail();
2189 ir
->condition
->accept(this);
2190 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
2192 if (this->options
->EmitCondCodes
) {
2193 cond_inst
= (ir_to_mesa_instruction
*)this->instructions
.get_tail();
2195 /* See if we actually generated any instruction for generating
2196 * the condition. If not, then cook up a move to a temp so we
2197 * have something to set cond_update on.
2199 if (cond_inst
== prev_inst
) {
2200 src_reg temp
= get_temp(glsl_type::bool_type
);
2201 cond_inst
= emit(ir
->condition
, OPCODE_MOV
, dst_reg(temp
), result
);
2203 cond_inst
->cond_update
= GL_TRUE
;
2205 if_inst
= emit(ir
->condition
, OPCODE_IF
);
2206 if_inst
->dst
.cond_mask
= COND_NE
;
2208 if_inst
= emit(ir
->condition
, OPCODE_IF
, undef_dst
, this->result
);
2211 this->instructions
.push_tail(if_inst
);
2213 visit_exec_list(&ir
->then_instructions
, this);
2215 if (!ir
->else_instructions
.is_empty()) {
2216 emit(ir
->condition
, OPCODE_ELSE
);
2217 visit_exec_list(&ir
->else_instructions
, this);
2220 if_inst
= emit(ir
->condition
, OPCODE_ENDIF
);
2223 ir_to_mesa_visitor::ir_to_mesa_visitor()
2225 result
.file
= PROGRAM_UNDEFINED
;
2227 next_signature_id
= 1;
2228 current_function
= NULL
;
2229 mem_ctx
= ralloc_context(NULL
);
2232 ir_to_mesa_visitor::~ir_to_mesa_visitor()
2234 ralloc_free(mem_ctx
);
2237 static struct prog_src_register
2238 mesa_src_reg_from_ir_src_reg(src_reg reg
)
2240 struct prog_src_register mesa_reg
;
2242 mesa_reg
.File
= reg
.file
;
2243 assert(reg
.index
< (1 << INST_INDEX_BITS
));
2244 mesa_reg
.Index
= reg
.index
;
2245 mesa_reg
.Swizzle
= reg
.swizzle
;
2246 mesa_reg
.RelAddr
= reg
.reladdr
!= NULL
;
2247 mesa_reg
.Negate
= reg
.negate
;
2249 mesa_reg
.HasIndex2
= GL_FALSE
;
2250 mesa_reg
.RelAddr2
= 0;
2251 mesa_reg
.Index2
= 0;
2257 set_branchtargets(ir_to_mesa_visitor
*v
,
2258 struct prog_instruction
*mesa_instructions
,
2259 int num_instructions
)
2261 int if_count
= 0, loop_count
= 0;
2262 int *if_stack
, *loop_stack
;
2263 int if_stack_pos
= 0, loop_stack_pos
= 0;
2266 for (i
= 0; i
< num_instructions
; i
++) {
2267 switch (mesa_instructions
[i
].Opcode
) {
2271 case OPCODE_BGNLOOP
:
2276 mesa_instructions
[i
].BranchTarget
= -1;
2283 if_stack
= rzalloc_array(v
->mem_ctx
, int, if_count
);
2284 loop_stack
= rzalloc_array(v
->mem_ctx
, int, loop_count
);
2286 for (i
= 0; i
< num_instructions
; i
++) {
2287 switch (mesa_instructions
[i
].Opcode
) {
2289 if_stack
[if_stack_pos
] = i
;
2293 mesa_instructions
[if_stack
[if_stack_pos
- 1]].BranchTarget
= i
;
2294 if_stack
[if_stack_pos
- 1] = i
;
2297 mesa_instructions
[if_stack
[if_stack_pos
- 1]].BranchTarget
= i
;
2300 case OPCODE_BGNLOOP
:
2301 loop_stack
[loop_stack_pos
] = i
;
2304 case OPCODE_ENDLOOP
:
2306 /* Rewrite any breaks/conts at this nesting level (haven't
2307 * already had a BranchTarget assigned) to point to the end
2310 for (j
= loop_stack
[loop_stack_pos
]; j
< i
; j
++) {
2311 if (mesa_instructions
[j
].Opcode
== OPCODE_BRK
||
2312 mesa_instructions
[j
].Opcode
== OPCODE_CONT
) {
2313 if (mesa_instructions
[j
].BranchTarget
== -1) {
2314 mesa_instructions
[j
].BranchTarget
= i
;
2318 /* The loop ends point at each other. */
2319 mesa_instructions
[i
].BranchTarget
= loop_stack
[loop_stack_pos
];
2320 mesa_instructions
[loop_stack
[loop_stack_pos
]].BranchTarget
= i
;
2323 foreach_iter(exec_list_iterator
, iter
, v
->function_signatures
) {
2324 function_entry
*entry
= (function_entry
*)iter
.get();
2326 if (entry
->sig_id
== mesa_instructions
[i
].BranchTarget
) {
2327 mesa_instructions
[i
].BranchTarget
= entry
->inst
;
2339 print_program(struct prog_instruction
*mesa_instructions
,
2340 ir_instruction
**mesa_instruction_annotation
,
2341 int num_instructions
)
2343 ir_instruction
*last_ir
= NULL
;
2347 for (i
= 0; i
< num_instructions
; i
++) {
2348 struct prog_instruction
*mesa_inst
= mesa_instructions
+ i
;
2349 ir_instruction
*ir
= mesa_instruction_annotation
[i
];
2351 fprintf(stdout
, "%3d: ", i
);
2353 if (last_ir
!= ir
&& ir
) {
2356 for (j
= 0; j
< indent
; j
++) {
2357 fprintf(stdout
, " ");
2363 fprintf(stdout
, " "); /* line number spacing. */
2366 indent
= _mesa_fprint_instruction_opt(stdout
, mesa_inst
, indent
,
2367 PROG_PRINT_DEBUG
, NULL
);
2373 * Count resources used by the given gpu program (number of texture
2377 count_resources(struct gl_program
*prog
)
2381 prog
->SamplersUsed
= 0;
2383 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
2384 struct prog_instruction
*inst
= &prog
->Instructions
[i
];
2386 if (_mesa_is_tex_instruction(inst
->Opcode
)) {
2387 prog
->SamplerTargets
[inst
->TexSrcUnit
] =
2388 (gl_texture_index
)inst
->TexSrcTarget
;
2389 prog
->SamplersUsed
|= 1 << inst
->TexSrcUnit
;
2390 if (inst
->TexShadow
) {
2391 prog
->ShadowSamplers
|= 1 << inst
->TexSrcUnit
;
2396 _mesa_update_shader_textures_used(prog
);
2401 * Check if the given vertex/fragment/shader program is within the
2402 * resource limits of the context (number of texture units, etc).
2403 * If any of those checks fail, record a linker error.
2405 * XXX more checks are needed...
2408 check_resources(const struct gl_context
*ctx
,
2409 struct gl_shader_program
*shader_program
,
2410 struct gl_program
*prog
)
2412 switch (prog
->Target
) {
2413 case GL_VERTEX_PROGRAM_ARB
:
2414 if (_mesa_bitcount(prog
->SamplersUsed
) >
2415 ctx
->Const
.MaxVertexTextureImageUnits
) {
2416 fail_link(shader_program
, "Too many vertex shader texture samplers");
2418 if (prog
->Parameters
->NumParameters
> MAX_UNIFORMS
) {
2419 fail_link(shader_program
, "Too many vertex shader constants");
2422 case MESA_GEOMETRY_PROGRAM
:
2423 if (_mesa_bitcount(prog
->SamplersUsed
) >
2424 ctx
->Const
.MaxGeometryTextureImageUnits
) {
2425 fail_link(shader_program
, "Too many geometry shader texture samplers");
2427 if (prog
->Parameters
->NumParameters
>
2428 MAX_GEOMETRY_UNIFORM_COMPONENTS
/ 4) {
2429 fail_link(shader_program
, "Too many geometry shader constants");
2432 case GL_FRAGMENT_PROGRAM_ARB
:
2433 if (_mesa_bitcount(prog
->SamplersUsed
) >
2434 ctx
->Const
.MaxTextureImageUnits
) {
2435 fail_link(shader_program
, "Too many fragment shader texture samplers");
2437 if (prog
->Parameters
->NumParameters
> MAX_UNIFORMS
) {
2438 fail_link(shader_program
, "Too many fragment shader constants");
2442 _mesa_problem(ctx
, "unexpected program type in check_resources()");
2448 struct uniform_sort
{
2449 struct gl_uniform
*u
;
2453 /* The shader_program->Uniforms list is almost sorted in increasing
2454 * uniform->{Frag,Vert}Pos locations, but not quite when there are
2455 * uniforms shared between targets. We need to add parameters in
2456 * increasing order for the targets.
2459 sort_uniforms(const void *a
, const void *b
)
2461 struct uniform_sort
*u1
= (struct uniform_sort
*)a
;
2462 struct uniform_sort
*u2
= (struct uniform_sort
*)b
;
2464 return u1
->pos
- u2
->pos
;
2467 /* Add the uniforms to the parameters. The linker chose locations
2468 * in our parameters lists (which weren't created yet), which the
2469 * uniforms code will use to poke values into our parameters list
2470 * when uniforms are updated.
2473 add_uniforms_to_parameters_list(struct gl_shader_program
*shader_program
,
2474 struct gl_shader
*shader
,
2475 struct gl_program
*prog
)
2478 unsigned int next_sampler
= 0, num_uniforms
= 0;
2479 struct uniform_sort
*sorted_uniforms
;
2481 sorted_uniforms
= ralloc_array(NULL
, struct uniform_sort
,
2482 shader_program
->Uniforms
->NumUniforms
);
2484 for (i
= 0; i
< shader_program
->Uniforms
->NumUniforms
; i
++) {
2485 struct gl_uniform
*uniform
= shader_program
->Uniforms
->Uniforms
+ i
;
2486 int parameter_index
= -1;
2488 switch (shader
->Type
) {
2489 case GL_VERTEX_SHADER
:
2490 parameter_index
= uniform
->VertPos
;
2492 case GL_FRAGMENT_SHADER
:
2493 parameter_index
= uniform
->FragPos
;
2495 case GL_GEOMETRY_SHADER
:
2496 parameter_index
= uniform
->GeomPos
;
2500 /* Only add uniforms used in our target. */
2501 if (parameter_index
!= -1) {
2502 sorted_uniforms
[num_uniforms
].pos
= parameter_index
;
2503 sorted_uniforms
[num_uniforms
].u
= uniform
;
2508 qsort(sorted_uniforms
, num_uniforms
, sizeof(struct uniform_sort
),
2511 for (i
= 0; i
< num_uniforms
; i
++) {
2512 struct gl_uniform
*uniform
= sorted_uniforms
[i
].u
;
2513 int parameter_index
= sorted_uniforms
[i
].pos
;
2514 const glsl_type
*type
= uniform
->Type
;
2517 if (type
->is_vector() ||
2518 type
->is_scalar()) {
2519 size
= type
->vector_elements
;
2521 size
= type_size(type
) * 4;
2524 gl_register_file file
;
2525 if (type
->is_sampler() ||
2526 (type
->is_array() && type
->fields
.array
->is_sampler())) {
2527 file
= PROGRAM_SAMPLER
;
2529 file
= PROGRAM_UNIFORM
;
2532 GLint index
= _mesa_lookup_parameter_index(prog
->Parameters
, -1,
2536 index
= _mesa_add_parameter(prog
->Parameters
, file
,
2537 uniform
->Name
, size
, type
->gl_type
,
2540 /* Sampler uniform values are stored in prog->SamplerUnits,
2541 * and the entry in that array is selected by this index we
2542 * store in ParameterValues[].
2544 if (file
== PROGRAM_SAMPLER
) {
2545 for (unsigned int j
= 0; j
< size
/ 4; j
++)
2546 prog
->Parameters
->ParameterValues
[index
+ j
][0] = next_sampler
++;
2549 /* The location chosen in the Parameters list here (returned
2550 * from _mesa_add_uniform) has to match what the linker chose.
2552 if (index
!= parameter_index
) {
2553 fail_link(shader_program
, "Allocation of uniform `%s' to target "
2554 "failed (%d vs %d)\n",
2555 uniform
->Name
, index
, parameter_index
);
2560 ralloc_free(sorted_uniforms
);
2564 set_uniform_initializer(struct gl_context
*ctx
, void *mem_ctx
,
2565 struct gl_shader_program
*shader_program
,
2566 const char *name
, const glsl_type
*type
,
2569 if (type
->is_record()) {
2570 ir_constant
*field_constant
;
2572 field_constant
= (ir_constant
*)val
->components
.get_head();
2574 for (unsigned int i
= 0; i
< type
->length
; i
++) {
2575 const glsl_type
*field_type
= type
->fields
.structure
[i
].type
;
2576 const char *field_name
= ralloc_asprintf(mem_ctx
, "%s.%s", name
,
2577 type
->fields
.structure
[i
].name
);
2578 set_uniform_initializer(ctx
, mem_ctx
, shader_program
, field_name
,
2579 field_type
, field_constant
);
2580 field_constant
= (ir_constant
*)field_constant
->next
;
2585 int loc
= _mesa_get_uniform_location(ctx
, shader_program
, name
);
2588 fail_link(shader_program
,
2589 "Couldn't find uniform for initializer %s\n", name
);
2593 for (unsigned int i
= 0; i
< (type
->is_array() ? type
->length
: 1); i
++) {
2594 ir_constant
*element
;
2595 const glsl_type
*element_type
;
2596 if (type
->is_array()) {
2597 element
= val
->array_elements
[i
];
2598 element_type
= type
->fields
.array
;
2601 element_type
= type
;
2606 if (element_type
->base_type
== GLSL_TYPE_BOOL
) {
2607 int *conv
= ralloc_array(mem_ctx
, int, element_type
->components());
2608 for (unsigned int j
= 0; j
< element_type
->components(); j
++) {
2609 conv
[j
] = element
->value
.b
[j
];
2611 values
= (void *)conv
;
2612 element_type
= glsl_type::get_instance(GLSL_TYPE_INT
,
2613 element_type
->vector_elements
,
2616 values
= &element
->value
;
2619 if (element_type
->is_matrix()) {
2620 _mesa_uniform_matrix(ctx
, shader_program
,
2621 element_type
->matrix_columns
,
2622 element_type
->vector_elements
,
2623 loc
, 1, GL_FALSE
, (GLfloat
*)values
);
2624 loc
+= element_type
->matrix_columns
;
2626 _mesa_uniform(ctx
, shader_program
, loc
, element_type
->matrix_columns
,
2627 values
, element_type
->gl_type
);
2628 loc
+= type_size(element_type
);
2634 set_uniform_initializers(struct gl_context
*ctx
,
2635 struct gl_shader_program
*shader_program
)
2637 void *mem_ctx
= NULL
;
2639 for (unsigned int i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
2640 struct gl_shader
*shader
= shader_program
->_LinkedShaders
[i
];
2645 foreach_iter(exec_list_iterator
, iter
, *shader
->ir
) {
2646 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
2647 ir_variable
*var
= ir
->as_variable();
2649 if (!var
|| var
->mode
!= ir_var_uniform
|| !var
->constant_value
)
2653 mem_ctx
= ralloc_context(NULL
);
2655 set_uniform_initializer(ctx
, mem_ctx
, shader_program
, var
->name
,
2656 var
->type
, var
->constant_value
);
2660 ralloc_free(mem_ctx
);
2664 * On a basic block basis, tracks available PROGRAM_TEMPORARY register
2665 * channels for copy propagation and updates following instructions to
2666 * use the original versions.
2668 * The ir_to_mesa_visitor lazily produces code assuming that this pass
2669 * will occur. As an example, a TXP production before this pass:
2671 * 0: MOV TEMP[1], INPUT[4].xyyy;
2672 * 1: MOV TEMP[1].w, INPUT[4].wwww;
2673 * 2: TXP TEMP[2], TEMP[1], texture[0], 2D;
2677 * 0: MOV TEMP[1], INPUT[4].xyyy;
2678 * 1: MOV TEMP[1].w, INPUT[4].wwww;
2679 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
2681 * which allows for dead code elimination on TEMP[1]'s writes.
2684 ir_to_mesa_visitor::copy_propagate(void)
2686 ir_to_mesa_instruction
**acp
= rzalloc_array(mem_ctx
,
2687 ir_to_mesa_instruction
*,
2688 this->next_temp
* 4);
2689 int *acp_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
2692 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2693 ir_to_mesa_instruction
*inst
= (ir_to_mesa_instruction
*)iter
.get();
2695 assert(inst
->dst
.file
!= PROGRAM_TEMPORARY
2696 || inst
->dst
.index
< this->next_temp
);
2698 /* First, do any copy propagation possible into the src regs. */
2699 for (int r
= 0; r
< 3; r
++) {
2700 ir_to_mesa_instruction
*first
= NULL
;
2702 int acp_base
= inst
->src
[r
].index
* 4;
2704 if (inst
->src
[r
].file
!= PROGRAM_TEMPORARY
||
2705 inst
->src
[r
].reladdr
)
2708 /* See if we can find entries in the ACP consisting of MOVs
2709 * from the same src register for all the swizzled channels
2710 * of this src register reference.
2712 for (int i
= 0; i
< 4; i
++) {
2713 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
2714 ir_to_mesa_instruction
*copy_chan
= acp
[acp_base
+ src_chan
];
2721 assert(acp_level
[acp_base
+ src_chan
] <= level
);
2726 if (first
->src
[0].file
!= copy_chan
->src
[0].file
||
2727 first
->src
[0].index
!= copy_chan
->src
[0].index
) {
2735 /* We've now validated that we can copy-propagate to
2736 * replace this src register reference. Do it.
2738 inst
->src
[r
].file
= first
->src
[0].file
;
2739 inst
->src
[r
].index
= first
->src
[0].index
;
2742 for (int i
= 0; i
< 4; i
++) {
2743 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
2744 ir_to_mesa_instruction
*copy_inst
= acp
[acp_base
+ src_chan
];
2745 swizzle
|= (GET_SWZ(copy_inst
->src
[0].swizzle
, src_chan
) <<
2748 inst
->src
[r
].swizzle
= swizzle
;
2753 case OPCODE_BGNLOOP
:
2754 case OPCODE_ENDLOOP
:
2755 /* End of a basic block, clear the ACP entirely. */
2756 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
2765 /* Clear all channels written inside the block from the ACP, but
2766 * leaving those that were not touched.
2768 for (int r
= 0; r
< this->next_temp
; r
++) {
2769 for (int c
= 0; c
< 4; c
++) {
2770 if (!acp
[4 * r
+ c
])
2773 if (acp_level
[4 * r
+ c
] >= level
)
2774 acp
[4 * r
+ c
] = NULL
;
2777 if (inst
->op
== OPCODE_ENDIF
)
2782 /* Continuing the block, clear any written channels from
2785 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.reladdr
) {
2786 /* Any temporary might be written, so no copy propagation
2787 * across this instruction.
2789 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
2790 } else if (inst
->dst
.file
== PROGRAM_OUTPUT
&&
2791 inst
->dst
.reladdr
) {
2792 /* Any output might be written, so no copy propagation
2793 * from outputs across this instruction.
2795 for (int r
= 0; r
< this->next_temp
; r
++) {
2796 for (int c
= 0; c
< 4; c
++) {
2797 if (!acp
[4 * r
+ c
])
2800 if (acp
[4 * r
+ c
]->src
[0].file
== PROGRAM_OUTPUT
)
2801 acp
[4 * r
+ c
] = NULL
;
2804 } else if (inst
->dst
.file
== PROGRAM_TEMPORARY
||
2805 inst
->dst
.file
== PROGRAM_OUTPUT
) {
2806 /* Clear where it's used as dst. */
2807 if (inst
->dst
.file
== PROGRAM_TEMPORARY
) {
2808 for (int c
= 0; c
< 4; c
++) {
2809 if (inst
->dst
.writemask
& (1 << c
)) {
2810 acp
[4 * inst
->dst
.index
+ c
] = NULL
;
2815 /* Clear where it's used as src. */
2816 for (int r
= 0; r
< this->next_temp
; r
++) {
2817 for (int c
= 0; c
< 4; c
++) {
2818 if (!acp
[4 * r
+ c
])
2821 int src_chan
= GET_SWZ(acp
[4 * r
+ c
]->src
[0].swizzle
, c
);
2823 if (acp
[4 * r
+ c
]->src
[0].file
== inst
->dst
.file
&&
2824 acp
[4 * r
+ c
]->src
[0].index
== inst
->dst
.index
&&
2825 inst
->dst
.writemask
& (1 << src_chan
))
2827 acp
[4 * r
+ c
] = NULL
;
2835 /* If this is a copy, add it to the ACP. */
2836 if (inst
->op
== OPCODE_MOV
&&
2837 inst
->dst
.file
== PROGRAM_TEMPORARY
&&
2838 !inst
->dst
.reladdr
&&
2840 !inst
->src
[0].reladdr
&&
2841 !inst
->src
[0].negate
) {
2842 for (int i
= 0; i
< 4; i
++) {
2843 if (inst
->dst
.writemask
& (1 << i
)) {
2844 acp
[4 * inst
->dst
.index
+ i
] = inst
;
2845 acp_level
[4 * inst
->dst
.index
+ i
] = level
;
2851 ralloc_free(acp_level
);
2857 * Convert a shader's GLSL IR into a Mesa gl_program.
2859 static struct gl_program
*
2860 get_mesa_program(struct gl_context
*ctx
,
2861 struct gl_shader_program
*shader_program
,
2862 struct gl_shader
*shader
)
2864 ir_to_mesa_visitor v
;
2865 struct prog_instruction
*mesa_instructions
, *mesa_inst
;
2866 ir_instruction
**mesa_instruction_annotation
;
2868 struct gl_program
*prog
;
2870 const char *target_string
;
2872 struct gl_shader_compiler_options
*options
=
2873 &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(shader
->Type
)];
2875 switch (shader
->Type
) {
2876 case GL_VERTEX_SHADER
:
2877 target
= GL_VERTEX_PROGRAM_ARB
;
2878 target_string
= "vertex";
2880 case GL_FRAGMENT_SHADER
:
2881 target
= GL_FRAGMENT_PROGRAM_ARB
;
2882 target_string
= "fragment";
2884 case GL_GEOMETRY_SHADER
:
2885 target
= GL_GEOMETRY_PROGRAM_NV
;
2886 target_string
= "geometry";
2889 assert(!"should not be reached");
2893 validate_ir_tree(shader
->ir
);
2895 prog
= ctx
->Driver
.NewProgram(ctx
, target
, shader_program
->Name
);
2898 prog
->Parameters
= _mesa_new_parameter_list();
2899 prog
->Varying
= _mesa_new_parameter_list();
2900 prog
->Attributes
= _mesa_new_parameter_list();
2903 v
.shader_program
= shader_program
;
2904 v
.options
= options
;
2906 add_uniforms_to_parameters_list(shader_program
, shader
, prog
);
2908 /* Emit Mesa IR for main(). */
2909 visit_exec_list(shader
->ir
, &v
);
2910 v
.emit(NULL
, OPCODE_END
);
2912 /* Now emit bodies for any functions that were used. */
2914 progress
= GL_FALSE
;
2916 foreach_iter(exec_list_iterator
, iter
, v
.function_signatures
) {
2917 function_entry
*entry
= (function_entry
*)iter
.get();
2919 if (!entry
->bgn_inst
) {
2920 v
.current_function
= entry
;
2922 entry
->bgn_inst
= v
.emit(NULL
, OPCODE_BGNSUB
);
2923 entry
->bgn_inst
->function
= entry
;
2925 visit_exec_list(&entry
->sig
->body
, &v
);
2927 ir_to_mesa_instruction
*last
;
2928 last
= (ir_to_mesa_instruction
*)v
.instructions
.get_tail();
2929 if (last
->op
!= OPCODE_RET
)
2930 v
.emit(NULL
, OPCODE_RET
);
2932 ir_to_mesa_instruction
*end
;
2933 end
= v
.emit(NULL
, OPCODE_ENDSUB
);
2934 end
->function
= entry
;
2941 prog
->NumTemporaries
= v
.next_temp
;
2943 int num_instructions
= 0;
2944 foreach_iter(exec_list_iterator
, iter
, v
.instructions
) {
2949 (struct prog_instruction
*)calloc(num_instructions
,
2950 sizeof(*mesa_instructions
));
2951 mesa_instruction_annotation
= ralloc_array(v
.mem_ctx
, ir_instruction
*,
2956 /* Convert ir_mesa_instructions into prog_instructions.
2958 mesa_inst
= mesa_instructions
;
2960 foreach_iter(exec_list_iterator
, iter
, v
.instructions
) {
2961 const ir_to_mesa_instruction
*inst
= (ir_to_mesa_instruction
*)iter
.get();
2963 mesa_inst
->Opcode
= inst
->op
;
2964 mesa_inst
->CondUpdate
= inst
->cond_update
;
2966 mesa_inst
->SaturateMode
= SATURATE_ZERO_ONE
;
2967 mesa_inst
->DstReg
.File
= inst
->dst
.file
;
2968 mesa_inst
->DstReg
.Index
= inst
->dst
.index
;
2969 mesa_inst
->DstReg
.CondMask
= inst
->dst
.cond_mask
;
2970 mesa_inst
->DstReg
.WriteMask
= inst
->dst
.writemask
;
2971 mesa_inst
->DstReg
.RelAddr
= inst
->dst
.reladdr
!= NULL
;
2972 mesa_inst
->SrcReg
[0] = mesa_src_reg_from_ir_src_reg(inst
->src
[0]);
2973 mesa_inst
->SrcReg
[1] = mesa_src_reg_from_ir_src_reg(inst
->src
[1]);
2974 mesa_inst
->SrcReg
[2] = mesa_src_reg_from_ir_src_reg(inst
->src
[2]);
2975 mesa_inst
->TexSrcUnit
= inst
->sampler
;
2976 mesa_inst
->TexSrcTarget
= inst
->tex_target
;
2977 mesa_inst
->TexShadow
= inst
->tex_shadow
;
2978 mesa_instruction_annotation
[i
] = inst
->ir
;
2980 /* Set IndirectRegisterFiles. */
2981 if (mesa_inst
->DstReg
.RelAddr
)
2982 prog
->IndirectRegisterFiles
|= 1 << mesa_inst
->DstReg
.File
;
2984 /* Update program's bitmask of indirectly accessed register files */
2985 for (unsigned src
= 0; src
< 3; src
++)
2986 if (mesa_inst
->SrcReg
[src
].RelAddr
)
2987 prog
->IndirectRegisterFiles
|= 1 << mesa_inst
->SrcReg
[src
].File
;
2989 if (options
->EmitNoIfs
&& mesa_inst
->Opcode
== OPCODE_IF
) {
2990 fail_link(shader_program
, "Couldn't flatten if statement\n");
2993 switch (mesa_inst
->Opcode
) {
2995 inst
->function
->inst
= i
;
2996 mesa_inst
->Comment
= strdup(inst
->function
->sig
->function_name());
2999 mesa_inst
->Comment
= strdup(inst
->function
->sig
->function_name());
3002 mesa_inst
->BranchTarget
= inst
->function
->sig_id
; /* rewritten later */
3005 prog
->NumAddressRegs
= 1;
3014 if (!shader_program
->LinkStatus
)
3018 if (!shader_program
->LinkStatus
) {
3019 free(mesa_instructions
);
3020 _mesa_reference_program(ctx
, &shader
->Program
, NULL
);
3024 set_branchtargets(&v
, mesa_instructions
, num_instructions
);
3026 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
3028 printf("GLSL IR for linked %s program %d:\n", target_string
,
3029 shader_program
->Name
);
3030 _mesa_print_ir(shader
->ir
, NULL
);
3033 printf("Mesa IR for linked %s program %d:\n", target_string
,
3034 shader_program
->Name
);
3035 print_program(mesa_instructions
, mesa_instruction_annotation
,
3039 prog
->Instructions
= mesa_instructions
;
3040 prog
->NumInstructions
= num_instructions
;
3042 do_set_program_inouts(shader
->ir
, prog
);
3043 count_resources(prog
);
3045 check_resources(ctx
, shader_program
, prog
);
3047 _mesa_reference_program(ctx
, &shader
->Program
, prog
);
3049 if ((ctx
->Shader
.Flags
& GLSL_NO_OPT
) == 0) {
3050 _mesa_optimize_program(ctx
, prog
);
3060 * Called via ctx->Driver.LinkShader()
3061 * This actually involves converting GLSL IR into Mesa gl_programs with
3062 * code lowering and other optimizations.
3065 _mesa_ir_link_shader(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
3067 assert(prog
->LinkStatus
);
3069 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
3070 if (prog
->_LinkedShaders
[i
] == NULL
)
3074 exec_list
*ir
= prog
->_LinkedShaders
[i
]->ir
;
3075 const struct gl_shader_compiler_options
*options
=
3076 &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(prog
->_LinkedShaders
[i
]->Type
)];
3082 do_mat_op_to_vec(ir
);
3083 lower_instructions(ir
, (MOD_TO_FRACT
| DIV_TO_MUL_RCP
| EXP_TO_EXP2
3085 | ((options
->EmitNoPow
) ? POW_TO_EXP2
: 0)));
3087 progress
= do_lower_jumps(ir
, true, true, options
->EmitNoMainReturn
, options
->EmitNoCont
, options
->EmitNoLoops
) || progress
;
3089 progress
= do_common_optimization(ir
, true, options
->MaxUnrollIterations
) || progress
;
3091 progress
= lower_quadop_vector(ir
, true) || progress
;
3093 if (options
->EmitNoIfs
) {
3094 progress
= lower_discard(ir
) || progress
;
3095 progress
= lower_if_to_cond_assign(ir
) || progress
;
3098 if (options
->EmitNoNoise
)
3099 progress
= lower_noise(ir
) || progress
;
3101 /* If there are forms of indirect addressing that the driver
3102 * cannot handle, perform the lowering pass.
3104 if (options
->EmitNoIndirectInput
|| options
->EmitNoIndirectOutput
3105 || options
->EmitNoIndirectTemp
|| options
->EmitNoIndirectUniform
)
3107 lower_variable_index_to_cond_assign(ir
,
3108 options
->EmitNoIndirectInput
,
3109 options
->EmitNoIndirectOutput
,
3110 options
->EmitNoIndirectTemp
,
3111 options
->EmitNoIndirectUniform
)
3114 progress
= do_vec_index_to_cond_assign(ir
) || progress
;
3117 validate_ir_tree(ir
);
3120 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
3121 struct gl_program
*linked_prog
;
3123 if (prog
->_LinkedShaders
[i
] == NULL
)
3126 linked_prog
= get_mesa_program(ctx
, prog
, prog
->_LinkedShaders
[i
]);
3131 switch (prog
->_LinkedShaders
[i
]->Type
) {
3132 case GL_VERTEX_SHADER
:
3133 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
,
3134 (struct gl_vertex_program
*)linked_prog
);
3135 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_VERTEX_PROGRAM_ARB
,
3138 case GL_FRAGMENT_SHADER
:
3139 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
,
3140 (struct gl_fragment_program
*)linked_prog
);
3141 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
3144 case GL_GEOMETRY_SHADER
:
3145 _mesa_reference_geomprog(ctx
, &prog
->GeometryProgram
,
3146 (struct gl_geometry_program
*)linked_prog
);
3147 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_GEOMETRY_PROGRAM_NV
,
3156 _mesa_reference_program(ctx
, &linked_prog
, NULL
);
3164 * Compile a GLSL shader. Called via glCompileShader().
3167 _mesa_glsl_compile_shader(struct gl_context
*ctx
, struct gl_shader
*shader
)
3169 struct _mesa_glsl_parse_state
*state
=
3170 new(shader
) _mesa_glsl_parse_state(ctx
, shader
->Type
, shader
);
3172 const char *source
= shader
->Source
;
3173 /* Check if the user called glCompileShader without first calling
3174 * glShaderSource. This should fail to compile, but not raise a GL_ERROR.
3176 if (source
== NULL
) {
3177 shader
->CompileStatus
= GL_FALSE
;
3181 state
->error
= preprocess(state
, &source
, &state
->info_log
,
3182 &ctx
->Extensions
, ctx
->API
);
3184 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
3185 printf("GLSL source for %s shader %d:\n",
3186 _mesa_glsl_shader_target_name(state
->target
), shader
->Name
);
3187 printf("%s\n", shader
->Source
);
3190 if (!state
->error
) {
3191 _mesa_glsl_lexer_ctor(state
, source
);
3192 _mesa_glsl_parse(state
);
3193 _mesa_glsl_lexer_dtor(state
);
3196 ralloc_free(shader
->ir
);
3197 shader
->ir
= new(shader
) exec_list
;
3198 if (!state
->error
&& !state
->translation_unit
.is_empty())
3199 _mesa_ast_to_hir(shader
->ir
, state
);
3201 if (!state
->error
&& !shader
->ir
->is_empty()) {
3202 validate_ir_tree(shader
->ir
);
3204 /* Do some optimization at compile time to reduce shader IR size
3205 * and reduce later work if the same shader is linked multiple times
3207 while (do_common_optimization(shader
->ir
, false, 32))
3210 validate_ir_tree(shader
->ir
);
3213 shader
->symbols
= state
->symbols
;
3215 shader
->CompileStatus
= !state
->error
;
3216 shader
->InfoLog
= state
->info_log
;
3217 shader
->Version
= state
->language_version
;
3218 memcpy(shader
->builtins_to_link
, state
->builtins_to_link
,
3219 sizeof(shader
->builtins_to_link
[0]) * state
->num_builtins_to_link
);
3220 shader
->num_builtins_to_link
= state
->num_builtins_to_link
;
3222 if (ctx
->Shader
.Flags
& GLSL_LOG
) {
3223 _mesa_write_shader_to_file(shader
);
3226 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
3227 if (shader
->CompileStatus
) {
3228 printf("GLSL IR for shader %d:\n", shader
->Name
);
3229 _mesa_print_ir(shader
->ir
, NULL
);
3232 printf("GLSL shader %d failed to compile.\n", shader
->Name
);
3234 if (shader
->InfoLog
&& shader
->InfoLog
[0] != 0) {
3235 printf("GLSL shader %d info log:\n", shader
->Name
);
3236 printf("%s\n", shader
->InfoLog
);
3240 /* Retain any live IR, but trash the rest. */
3241 reparent_ir(shader
->ir
, shader
->ir
);
3248 * Link a GLSL shader program. Called via glLinkProgram().
3251 _mesa_glsl_link_shader(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
3255 _mesa_clear_shader_program_data(ctx
, prog
);
3257 prog
->LinkStatus
= GL_TRUE
;
3259 for (i
= 0; i
< prog
->NumShaders
; i
++) {
3260 if (!prog
->Shaders
[i
]->CompileStatus
) {
3261 fail_link(prog
, "linking with uncompiled shader");
3262 prog
->LinkStatus
= GL_FALSE
;
3266 prog
->Varying
= _mesa_new_parameter_list();
3267 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
, NULL
);
3268 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
, NULL
);
3269 _mesa_reference_geomprog(ctx
, &prog
->GeometryProgram
, NULL
);
3271 if (prog
->LinkStatus
) {
3272 link_shaders(ctx
, prog
);
3275 if (prog
->LinkStatus
) {
3276 if (!ctx
->Driver
.LinkShader(ctx
, prog
)) {
3277 prog
->LinkStatus
= GL_FALSE
;
3281 set_uniform_initializers(ctx
, prog
);
3283 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
3284 if (!prog
->LinkStatus
) {
3285 printf("GLSL shader program %d failed to link\n", prog
->Name
);
3288 if (prog
->InfoLog
&& prog
->InfoLog
[0] != 0) {
3289 printf("GLSL shader program %d info log:\n", prog
->Name
);
3290 printf("%s\n", prog
->InfoLog
);