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
= reg
.reladdr
;
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]);
1297 /* Mesa IR lacks types, ints are stored as truncated floats. */
1301 emit(ir
, OPCODE_TRUNC
, result_dst
, op
[0]);
1305 emit(ir
, OPCODE_SNE
, result_dst
,
1306 op
[0], src_reg_for_float(0.0));
1309 emit(ir
, OPCODE_TRUNC
, result_dst
, op
[0]);
1312 op
[0].negate
= ~op
[0].negate
;
1313 emit(ir
, OPCODE_FLR
, result_dst
, op
[0]);
1314 result_src
.negate
= ~result_src
.negate
;
1317 emit(ir
, OPCODE_FLR
, result_dst
, op
[0]);
1320 emit(ir
, OPCODE_FRC
, result_dst
, op
[0]);
1324 emit(ir
, OPCODE_MIN
, result_dst
, op
[0], op
[1]);
1327 emit(ir
, OPCODE_MAX
, result_dst
, op
[0], op
[1]);
1330 emit_scalar(ir
, OPCODE_POW
, result_dst
, op
[0], op
[1]);
1333 case ir_unop_bit_not
:
1335 case ir_binop_lshift
:
1336 case ir_binop_rshift
:
1337 case ir_binop_bit_and
:
1338 case ir_binop_bit_xor
:
1339 case ir_binop_bit_or
:
1340 case ir_unop_round_even
:
1341 assert(!"GLSL 1.30 features unsupported");
1344 case ir_quadop_vector
:
1345 /* This operation should have already been handled.
1347 assert(!"Should not get here.");
1351 this->result
= result_src
;
1356 ir_to_mesa_visitor::visit(ir_swizzle
*ir
)
1362 /* Note that this is only swizzles in expressions, not those on the left
1363 * hand side of an assignment, which do write masking. See ir_assignment
1367 ir
->val
->accept(this);
1369 assert(src
.file
!= PROGRAM_UNDEFINED
);
1371 for (i
= 0; i
< 4; i
++) {
1372 if (i
< ir
->type
->vector_elements
) {
1375 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.x
);
1378 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.y
);
1381 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.z
);
1384 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.w
);
1388 /* If the type is smaller than a vec4, replicate the last
1391 swizzle
[i
] = swizzle
[ir
->type
->vector_elements
- 1];
1395 src
.swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
1401 ir_to_mesa_visitor::visit(ir_dereference_variable
*ir
)
1403 variable_storage
*entry
= find_variable_storage(ir
->var
);
1404 ir_variable
*var
= ir
->var
;
1407 switch (var
->mode
) {
1408 case ir_var_uniform
:
1409 entry
= new(mem_ctx
) variable_storage(var
, PROGRAM_UNIFORM
,
1411 this->variables
.push_tail(entry
);
1415 /* The linker assigns locations for varyings and attributes,
1416 * including deprecated builtins (like gl_Color), user-assign
1417 * generic attributes (glBindVertexLocation), and
1418 * user-defined varyings.
1420 * FINISHME: We would hit this path for function arguments. Fix!
1422 assert(var
->location
!= -1);
1423 entry
= new(mem_ctx
) variable_storage(var
,
1426 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
&&
1427 var
->location
>= VERT_ATTRIB_GENERIC0
) {
1428 _mesa_add_attribute(this->prog
->Attributes
,
1430 _mesa_sizeof_glsl_type(var
->type
->gl_type
),
1432 var
->location
- VERT_ATTRIB_GENERIC0
);
1436 assert(var
->location
!= -1);
1437 entry
= new(mem_ctx
) variable_storage(var
,
1441 case ir_var_system_value
:
1442 entry
= new(mem_ctx
) variable_storage(var
,
1443 PROGRAM_SYSTEM_VALUE
,
1447 case ir_var_temporary
:
1448 entry
= new(mem_ctx
) variable_storage(var
, PROGRAM_TEMPORARY
,
1450 this->variables
.push_tail(entry
);
1452 next_temp
+= type_size(var
->type
);
1457 printf("Failed to make storage for %s\n", var
->name
);
1462 this->result
= src_reg(entry
->file
, entry
->index
, var
->type
);
1466 ir_to_mesa_visitor::visit(ir_dereference_array
*ir
)
1470 int element_size
= type_size(ir
->type
);
1472 index
= ir
->array_index
->constant_expression_value();
1474 ir
->array
->accept(this);
1478 src
.index
+= index
->value
.i
[0] * element_size
;
1480 /* Variable index array dereference. It eats the "vec4" of the
1481 * base of the array and an index that offsets the Mesa register
1484 ir
->array_index
->accept(this);
1488 if (element_size
== 1) {
1489 index_reg
= this->result
;
1491 index_reg
= get_temp(glsl_type::float_type
);
1493 emit(ir
, OPCODE_MUL
, dst_reg(index_reg
),
1494 this->result
, src_reg_for_float(element_size
));
1497 /* If there was already a relative address register involved, add the
1498 * new and the old together to get the new offset.
1500 if (src
.reladdr
!= NULL
) {
1501 src_reg accum_reg
= get_temp(glsl_type::float_type
);
1503 emit(ir
, OPCODE_ADD
, dst_reg(accum_reg
),
1504 index_reg
, *src
.reladdr
);
1506 index_reg
= accum_reg
;
1509 src
.reladdr
= ralloc(mem_ctx
, src_reg
);
1510 memcpy(src
.reladdr
, &index_reg
, sizeof(index_reg
));
1513 /* If the type is smaller than a vec4, replicate the last channel out. */
1514 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
1515 src
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1517 src
.swizzle
= SWIZZLE_NOOP
;
1523 ir_to_mesa_visitor::visit(ir_dereference_record
*ir
)
1526 const glsl_type
*struct_type
= ir
->record
->type
;
1529 ir
->record
->accept(this);
1531 for (i
= 0; i
< struct_type
->length
; i
++) {
1532 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
1534 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
1537 /* If the type is smaller than a vec4, replicate the last channel out. */
1538 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
1539 this->result
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1541 this->result
.swizzle
= SWIZZLE_NOOP
;
1543 this->result
.index
+= offset
;
1547 * We want to be careful in assignment setup to hit the actual storage
1548 * instead of potentially using a temporary like we might with the
1549 * ir_dereference handler.
1552 get_assignment_lhs(ir_dereference
*ir
, ir_to_mesa_visitor
*v
)
1554 /* The LHS must be a dereference. If the LHS is a variable indexed array
1555 * access of a vector, it must be separated into a series conditional moves
1556 * before reaching this point (see ir_vec_index_to_cond_assign).
1558 assert(ir
->as_dereference());
1559 ir_dereference_array
*deref_array
= ir
->as_dereference_array();
1561 assert(!deref_array
->array
->type
->is_vector());
1564 /* Use the rvalue deref handler for the most part. We'll ignore
1565 * swizzles in it and write swizzles using writemask, though.
1568 return dst_reg(v
->result
);
1572 * Process the condition of a conditional assignment
1574 * Examines the condition of a conditional assignment to generate the optimal
1575 * first operand of a \c CMP instruction. If the condition is a relational
1576 * operator with 0 (e.g., \c ir_binop_less), the value being compared will be
1577 * used as the source for the \c CMP instruction. Otherwise the comparison
1578 * is processed to a boolean result, and the boolean result is used as the
1579 * operand to the CMP instruction.
1582 ir_to_mesa_visitor::process_move_condition(ir_rvalue
*ir
)
1584 ir_rvalue
*src_ir
= ir
;
1586 bool switch_order
= false;
1588 ir_expression
*const expr
= ir
->as_expression();
1589 if ((expr
!= NULL
) && (expr
->get_num_operands() == 2)) {
1590 bool zero_on_left
= false;
1592 if (expr
->operands
[0]->is_zero()) {
1593 src_ir
= expr
->operands
[1];
1594 zero_on_left
= true;
1595 } else if (expr
->operands
[1]->is_zero()) {
1596 src_ir
= expr
->operands
[0];
1597 zero_on_left
= false;
1601 * (a < 0) T F F ( a < 0) T F F
1602 * (0 < a) F F T (-a < 0) F F T
1603 * (a <= 0) T T F (-a < 0) F F T (swap order of other operands)
1604 * (0 <= a) F T T ( a < 0) T F F (swap order of other operands)
1605 * (a > 0) F F T (-a < 0) F F T
1606 * (0 > a) T F F ( a < 0) T F F
1607 * (a >= 0) F T T ( a < 0) T F F (swap order of other operands)
1608 * (0 >= a) T T F (-a < 0) F F T (swap order of other operands)
1610 * Note that exchanging the order of 0 and 'a' in the comparison simply
1611 * means that the value of 'a' should be negated.
1614 switch (expr
->operation
) {
1616 switch_order
= false;
1617 negate
= zero_on_left
;
1620 case ir_binop_greater
:
1621 switch_order
= false;
1622 negate
= !zero_on_left
;
1625 case ir_binop_lequal
:
1626 switch_order
= true;
1627 negate
= !zero_on_left
;
1630 case ir_binop_gequal
:
1631 switch_order
= true;
1632 negate
= zero_on_left
;
1636 /* This isn't the right kind of comparison afterall, so make sure
1637 * the whole condition is visited.
1645 src_ir
->accept(this);
1647 /* We use the OPCODE_CMP (a < 0 ? b : c) for conditional moves, and the
1648 * condition we produced is 0.0 or 1.0. By flipping the sign, we can
1649 * choose which value OPCODE_CMP produces without an extra instruction
1650 * computing the condition.
1653 this->result
.negate
= ~this->result
.negate
;
1655 return switch_order
;
1659 ir_to_mesa_visitor::visit(ir_assignment
*ir
)
1665 ir
->rhs
->accept(this);
1668 l
= get_assignment_lhs(ir
->lhs
, this);
1670 /* FINISHME: This should really set to the correct maximal writemask for each
1671 * FINISHME: component written (in the loops below). This case can only
1672 * FINISHME: occur for matrices, arrays, and structures.
1674 if (ir
->write_mask
== 0) {
1675 assert(!ir
->lhs
->type
->is_scalar() && !ir
->lhs
->type
->is_vector());
1676 l
.writemask
= WRITEMASK_XYZW
;
1677 } else if (ir
->lhs
->type
->is_scalar()) {
1678 /* FINISHME: This hack makes writing to gl_FragDepth, which lives in the
1679 * FINISHME: W component of fragment shader output zero, work correctly.
1681 l
.writemask
= WRITEMASK_XYZW
;
1684 int first_enabled_chan
= 0;
1687 assert(ir
->lhs
->type
->is_vector());
1688 l
.writemask
= ir
->write_mask
;
1690 for (int i
= 0; i
< 4; i
++) {
1691 if (l
.writemask
& (1 << i
)) {
1692 first_enabled_chan
= GET_SWZ(r
.swizzle
, i
);
1697 /* Swizzle a small RHS vector into the channels being written.
1699 * glsl ir treats write_mask as dictating how many channels are
1700 * present on the RHS while Mesa IR treats write_mask as just
1701 * showing which channels of the vec4 RHS get written.
1703 for (int i
= 0; i
< 4; i
++) {
1704 if (l
.writemask
& (1 << i
))
1705 swizzles
[i
] = GET_SWZ(r
.swizzle
, rhs_chan
++);
1707 swizzles
[i
] = first_enabled_chan
;
1709 r
.swizzle
= MAKE_SWIZZLE4(swizzles
[0], swizzles
[1],
1710 swizzles
[2], swizzles
[3]);
1713 assert(l
.file
!= PROGRAM_UNDEFINED
);
1714 assert(r
.file
!= PROGRAM_UNDEFINED
);
1716 if (ir
->condition
) {
1717 const bool switch_order
= this->process_move_condition(ir
->condition
);
1718 src_reg condition
= this->result
;
1720 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
1722 emit(ir
, OPCODE_CMP
, l
, condition
, src_reg(l
), r
);
1724 emit(ir
, OPCODE_CMP
, l
, condition
, r
, src_reg(l
));
1731 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
1732 emit(ir
, OPCODE_MOV
, l
, r
);
1741 ir_to_mesa_visitor::visit(ir_constant
*ir
)
1744 GLfloat stack_vals
[4] = { 0 };
1745 GLfloat
*values
= stack_vals
;
1748 /* Unfortunately, 4 floats is all we can get into
1749 * _mesa_add_unnamed_constant. So, make a temp to store an
1750 * aggregate constant and move each constant value into it. If we
1751 * get lucky, copy propagation will eliminate the extra moves.
1754 if (ir
->type
->base_type
== GLSL_TYPE_STRUCT
) {
1755 src_reg temp_base
= get_temp(ir
->type
);
1756 dst_reg temp
= dst_reg(temp_base
);
1758 foreach_iter(exec_list_iterator
, iter
, ir
->components
) {
1759 ir_constant
*field_value
= (ir_constant
*)iter
.get();
1760 int size
= type_size(field_value
->type
);
1764 field_value
->accept(this);
1767 for (i
= 0; i
< (unsigned int)size
; i
++) {
1768 emit(ir
, OPCODE_MOV
, temp
, src
);
1774 this->result
= temp_base
;
1778 if (ir
->type
->is_array()) {
1779 src_reg temp_base
= get_temp(ir
->type
);
1780 dst_reg temp
= dst_reg(temp_base
);
1781 int size
= type_size(ir
->type
->fields
.array
);
1785 for (i
= 0; i
< ir
->type
->length
; i
++) {
1786 ir
->array_elements
[i
]->accept(this);
1788 for (int j
= 0; j
< size
; j
++) {
1789 emit(ir
, OPCODE_MOV
, temp
, src
);
1795 this->result
= temp_base
;
1799 if (ir
->type
->is_matrix()) {
1800 src_reg mat
= get_temp(ir
->type
);
1801 dst_reg mat_column
= dst_reg(mat
);
1803 for (i
= 0; i
< ir
->type
->matrix_columns
; i
++) {
1804 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
1805 values
= &ir
->value
.f
[i
* ir
->type
->vector_elements
];
1807 src
= src_reg(PROGRAM_CONSTANT
, -1, NULL
);
1808 src
.index
= _mesa_add_unnamed_constant(this->prog
->Parameters
,
1810 ir
->type
->vector_elements
,
1812 emit(ir
, OPCODE_MOV
, mat_column
, src
);
1821 src
.file
= PROGRAM_CONSTANT
;
1822 switch (ir
->type
->base_type
) {
1823 case GLSL_TYPE_FLOAT
:
1824 values
= &ir
->value
.f
[0];
1826 case GLSL_TYPE_UINT
:
1827 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1828 values
[i
] = ir
->value
.u
[i
];
1832 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1833 values
[i
] = ir
->value
.i
[i
];
1836 case GLSL_TYPE_BOOL
:
1837 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1838 values
[i
] = ir
->value
.b
[i
];
1842 assert(!"Non-float/uint/int/bool constant");
1845 this->result
= src_reg(PROGRAM_CONSTANT
, -1, ir
->type
);
1846 this->result
.index
= _mesa_add_unnamed_constant(this->prog
->Parameters
,
1848 ir
->type
->vector_elements
,
1849 &this->result
.swizzle
);
1853 ir_to_mesa_visitor::get_function_signature(ir_function_signature
*sig
)
1855 function_entry
*entry
;
1857 foreach_iter(exec_list_iterator
, iter
, this->function_signatures
) {
1858 entry
= (function_entry
*)iter
.get();
1860 if (entry
->sig
== sig
)
1864 entry
= ralloc(mem_ctx
, function_entry
);
1866 entry
->sig_id
= this->next_signature_id
++;
1867 entry
->bgn_inst
= NULL
;
1869 /* Allocate storage for all the parameters. */
1870 foreach_iter(exec_list_iterator
, iter
, sig
->parameters
) {
1871 ir_variable
*param
= (ir_variable
*)iter
.get();
1872 variable_storage
*storage
;
1874 storage
= find_variable_storage(param
);
1877 storage
= new(mem_ctx
) variable_storage(param
, PROGRAM_TEMPORARY
,
1879 this->variables
.push_tail(storage
);
1881 this->next_temp
+= type_size(param
->type
);
1884 if (!sig
->return_type
->is_void()) {
1885 entry
->return_reg
= get_temp(sig
->return_type
);
1887 entry
->return_reg
= undef_src
;
1890 this->function_signatures
.push_tail(entry
);
1895 ir_to_mesa_visitor::visit(ir_call
*ir
)
1897 ir_to_mesa_instruction
*call_inst
;
1898 ir_function_signature
*sig
= ir
->get_callee();
1899 function_entry
*entry
= get_function_signature(sig
);
1902 /* Process in parameters. */
1903 exec_list_iterator sig_iter
= sig
->parameters
.iterator();
1904 foreach_iter(exec_list_iterator
, iter
, *ir
) {
1905 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
1906 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
1908 if (param
->mode
== ir_var_in
||
1909 param
->mode
== ir_var_inout
) {
1910 variable_storage
*storage
= find_variable_storage(param
);
1913 param_rval
->accept(this);
1914 src_reg r
= this->result
;
1917 l
.file
= storage
->file
;
1918 l
.index
= storage
->index
;
1920 l
.writemask
= WRITEMASK_XYZW
;
1921 l
.cond_mask
= COND_TR
;
1923 for (i
= 0; i
< type_size(param
->type
); i
++) {
1924 emit(ir
, OPCODE_MOV
, l
, r
);
1932 assert(!sig_iter
.has_next());
1934 /* Emit call instruction */
1935 call_inst
= emit(ir
, OPCODE_CAL
);
1936 call_inst
->function
= entry
;
1938 /* Process out parameters. */
1939 sig_iter
= sig
->parameters
.iterator();
1940 foreach_iter(exec_list_iterator
, iter
, *ir
) {
1941 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
1942 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
1944 if (param
->mode
== ir_var_out
||
1945 param
->mode
== ir_var_inout
) {
1946 variable_storage
*storage
= find_variable_storage(param
);
1950 r
.file
= storage
->file
;
1951 r
.index
= storage
->index
;
1953 r
.swizzle
= SWIZZLE_NOOP
;
1956 param_rval
->accept(this);
1957 dst_reg l
= dst_reg(this->result
);
1959 for (i
= 0; i
< type_size(param
->type
); i
++) {
1960 emit(ir
, OPCODE_MOV
, l
, r
);
1968 assert(!sig_iter
.has_next());
1970 /* Process return value. */
1971 this->result
= entry
->return_reg
;
1975 ir_to_mesa_visitor::visit(ir_texture
*ir
)
1977 src_reg result_src
, coord
, lod_info
, projector
, dx
, dy
;
1978 dst_reg result_dst
, coord_dst
;
1979 ir_to_mesa_instruction
*inst
= NULL
;
1980 prog_opcode opcode
= OPCODE_NOP
;
1982 ir
->coordinate
->accept(this);
1984 /* Put our coords in a temp. We'll need to modify them for shadow,
1985 * projection, or LOD, so the only case we'd use it as is is if
1986 * we're doing plain old texturing. Mesa IR optimization should
1987 * handle cleaning up our mess in that case.
1989 coord
= get_temp(glsl_type::vec4_type
);
1990 coord_dst
= dst_reg(coord
);
1991 emit(ir
, OPCODE_MOV
, coord_dst
, this->result
);
1993 if (ir
->projector
) {
1994 ir
->projector
->accept(this);
1995 projector
= this->result
;
1998 /* Storage for our result. Ideally for an assignment we'd be using
1999 * the actual storage for the result here, instead.
2001 result_src
= get_temp(glsl_type::vec4_type
);
2002 result_dst
= dst_reg(result_src
);
2006 opcode
= OPCODE_TEX
;
2009 opcode
= OPCODE_TXB
;
2010 ir
->lod_info
.bias
->accept(this);
2011 lod_info
= this->result
;
2014 opcode
= OPCODE_TXL
;
2015 ir
->lod_info
.lod
->accept(this);
2016 lod_info
= this->result
;
2019 opcode
= OPCODE_TXD
;
2020 ir
->lod_info
.grad
.dPdx
->accept(this);
2022 ir
->lod_info
.grad
.dPdy
->accept(this);
2026 assert(!"GLSL 1.30 features unsupported");
2030 if (ir
->projector
) {
2031 if (opcode
== OPCODE_TEX
) {
2032 /* Slot the projector in as the last component of the coord. */
2033 coord_dst
.writemask
= WRITEMASK_W
;
2034 emit(ir
, OPCODE_MOV
, coord_dst
, projector
);
2035 coord_dst
.writemask
= WRITEMASK_XYZW
;
2036 opcode
= OPCODE_TXP
;
2038 src_reg coord_w
= coord
;
2039 coord_w
.swizzle
= SWIZZLE_WWWW
;
2041 /* For the other TEX opcodes there's no projective version
2042 * since the last slot is taken up by lod info. Do the
2043 * projective divide now.
2045 coord_dst
.writemask
= WRITEMASK_W
;
2046 emit(ir
, OPCODE_RCP
, coord_dst
, projector
);
2048 /* In the case where we have to project the coordinates "by hand,"
2049 * the shadow comparitor value must also be projected.
2051 src_reg tmp_src
= coord
;
2052 if (ir
->shadow_comparitor
) {
2053 /* Slot the shadow value in as the second to last component of the
2056 ir
->shadow_comparitor
->accept(this);
2058 tmp_src
= get_temp(glsl_type::vec4_type
);
2059 dst_reg tmp_dst
= dst_reg(tmp_src
);
2061 tmp_dst
.writemask
= WRITEMASK_Z
;
2062 emit(ir
, OPCODE_MOV
, tmp_dst
, this->result
);
2064 tmp_dst
.writemask
= WRITEMASK_XY
;
2065 emit(ir
, OPCODE_MOV
, tmp_dst
, coord
);
2068 coord_dst
.writemask
= WRITEMASK_XYZ
;
2069 emit(ir
, OPCODE_MUL
, coord_dst
, tmp_src
, coord_w
);
2071 coord_dst
.writemask
= WRITEMASK_XYZW
;
2072 coord
.swizzle
= SWIZZLE_XYZW
;
2076 /* If projection is done and the opcode is not OPCODE_TXP, then the shadow
2077 * comparitor was put in the correct place (and projected) by the code,
2078 * above, that handles by-hand projection.
2080 if (ir
->shadow_comparitor
&& (!ir
->projector
|| opcode
== OPCODE_TXP
)) {
2081 /* Slot the shadow value in as the second to last component of the
2084 ir
->shadow_comparitor
->accept(this);
2085 coord_dst
.writemask
= WRITEMASK_Z
;
2086 emit(ir
, OPCODE_MOV
, coord_dst
, this->result
);
2087 coord_dst
.writemask
= WRITEMASK_XYZW
;
2090 if (opcode
== OPCODE_TXL
|| opcode
== OPCODE_TXB
) {
2091 /* Mesa IR stores lod or lod bias in the last channel of the coords. */
2092 coord_dst
.writemask
= WRITEMASK_W
;
2093 emit(ir
, OPCODE_MOV
, coord_dst
, lod_info
);
2094 coord_dst
.writemask
= WRITEMASK_XYZW
;
2097 if (opcode
== OPCODE_TXD
)
2098 inst
= emit(ir
, opcode
, result_dst
, coord
, dx
, dy
);
2100 inst
= emit(ir
, opcode
, result_dst
, coord
);
2102 if (ir
->shadow_comparitor
)
2103 inst
->tex_shadow
= GL_TRUE
;
2105 inst
->sampler
= _mesa_get_sampler_uniform_value(ir
->sampler
,
2106 this->shader_program
,
2109 const glsl_type
*sampler_type
= ir
->sampler
->type
;
2111 switch (sampler_type
->sampler_dimensionality
) {
2112 case GLSL_SAMPLER_DIM_1D
:
2113 inst
->tex_target
= (sampler_type
->sampler_array
)
2114 ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
2116 case GLSL_SAMPLER_DIM_2D
:
2117 inst
->tex_target
= (sampler_type
->sampler_array
)
2118 ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
2120 case GLSL_SAMPLER_DIM_3D
:
2121 inst
->tex_target
= TEXTURE_3D_INDEX
;
2123 case GLSL_SAMPLER_DIM_CUBE
:
2124 inst
->tex_target
= TEXTURE_CUBE_INDEX
;
2126 case GLSL_SAMPLER_DIM_RECT
:
2127 inst
->tex_target
= TEXTURE_RECT_INDEX
;
2129 case GLSL_SAMPLER_DIM_BUF
:
2130 assert(!"FINISHME: Implement ARB_texture_buffer_object");
2133 assert(!"Should not get here.");
2136 this->result
= result_src
;
2140 ir_to_mesa_visitor::visit(ir_return
*ir
)
2142 if (ir
->get_value()) {
2146 assert(current_function
);
2148 ir
->get_value()->accept(this);
2149 src_reg r
= this->result
;
2151 l
= dst_reg(current_function
->return_reg
);
2153 for (i
= 0; i
< type_size(current_function
->sig
->return_type
); i
++) {
2154 emit(ir
, OPCODE_MOV
, l
, r
);
2160 emit(ir
, OPCODE_RET
);
2164 ir_to_mesa_visitor::visit(ir_discard
*ir
)
2166 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
2168 if (ir
->condition
) {
2169 ir
->condition
->accept(this);
2170 this->result
.negate
= ~this->result
.negate
;
2171 emit(ir
, OPCODE_KIL
, undef_dst
, this->result
);
2173 emit(ir
, OPCODE_KIL_NV
);
2176 fp
->UsesKill
= GL_TRUE
;
2180 ir_to_mesa_visitor::visit(ir_if
*ir
)
2182 ir_to_mesa_instruction
*cond_inst
, *if_inst
;
2183 ir_to_mesa_instruction
*prev_inst
;
2185 prev_inst
= (ir_to_mesa_instruction
*)this->instructions
.get_tail();
2187 ir
->condition
->accept(this);
2188 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
2190 if (this->options
->EmitCondCodes
) {
2191 cond_inst
= (ir_to_mesa_instruction
*)this->instructions
.get_tail();
2193 /* See if we actually generated any instruction for generating
2194 * the condition. If not, then cook up a move to a temp so we
2195 * have something to set cond_update on.
2197 if (cond_inst
== prev_inst
) {
2198 src_reg temp
= get_temp(glsl_type::bool_type
);
2199 cond_inst
= emit(ir
->condition
, OPCODE_MOV
, dst_reg(temp
), result
);
2201 cond_inst
->cond_update
= GL_TRUE
;
2203 if_inst
= emit(ir
->condition
, OPCODE_IF
);
2204 if_inst
->dst
.cond_mask
= COND_NE
;
2206 if_inst
= emit(ir
->condition
, OPCODE_IF
, undef_dst
, this->result
);
2209 this->instructions
.push_tail(if_inst
);
2211 visit_exec_list(&ir
->then_instructions
, this);
2213 if (!ir
->else_instructions
.is_empty()) {
2214 emit(ir
->condition
, OPCODE_ELSE
);
2215 visit_exec_list(&ir
->else_instructions
, this);
2218 if_inst
= emit(ir
->condition
, OPCODE_ENDIF
);
2221 ir_to_mesa_visitor::ir_to_mesa_visitor()
2223 result
.file
= PROGRAM_UNDEFINED
;
2225 next_signature_id
= 1;
2226 current_function
= NULL
;
2227 mem_ctx
= ralloc_context(NULL
);
2230 ir_to_mesa_visitor::~ir_to_mesa_visitor()
2232 ralloc_free(mem_ctx
);
2235 static struct prog_src_register
2236 mesa_src_reg_from_ir_src_reg(src_reg reg
)
2238 struct prog_src_register mesa_reg
;
2240 mesa_reg
.File
= reg
.file
;
2241 assert(reg
.index
< (1 << INST_INDEX_BITS
));
2242 mesa_reg
.Index
= reg
.index
;
2243 mesa_reg
.Swizzle
= reg
.swizzle
;
2244 mesa_reg
.RelAddr
= reg
.reladdr
!= NULL
;
2245 mesa_reg
.Negate
= reg
.negate
;
2247 mesa_reg
.HasIndex2
= GL_FALSE
;
2248 mesa_reg
.RelAddr2
= 0;
2249 mesa_reg
.Index2
= 0;
2255 set_branchtargets(ir_to_mesa_visitor
*v
,
2256 struct prog_instruction
*mesa_instructions
,
2257 int num_instructions
)
2259 int if_count
= 0, loop_count
= 0;
2260 int *if_stack
, *loop_stack
;
2261 int if_stack_pos
= 0, loop_stack_pos
= 0;
2264 for (i
= 0; i
< num_instructions
; i
++) {
2265 switch (mesa_instructions
[i
].Opcode
) {
2269 case OPCODE_BGNLOOP
:
2274 mesa_instructions
[i
].BranchTarget
= -1;
2281 if_stack
= rzalloc_array(v
->mem_ctx
, int, if_count
);
2282 loop_stack
= rzalloc_array(v
->mem_ctx
, int, loop_count
);
2284 for (i
= 0; i
< num_instructions
; i
++) {
2285 switch (mesa_instructions
[i
].Opcode
) {
2287 if_stack
[if_stack_pos
] = i
;
2291 mesa_instructions
[if_stack
[if_stack_pos
- 1]].BranchTarget
= i
;
2292 if_stack
[if_stack_pos
- 1] = i
;
2295 mesa_instructions
[if_stack
[if_stack_pos
- 1]].BranchTarget
= i
;
2298 case OPCODE_BGNLOOP
:
2299 loop_stack
[loop_stack_pos
] = i
;
2302 case OPCODE_ENDLOOP
:
2304 /* Rewrite any breaks/conts at this nesting level (haven't
2305 * already had a BranchTarget assigned) to point to the end
2308 for (j
= loop_stack
[loop_stack_pos
]; j
< i
; j
++) {
2309 if (mesa_instructions
[j
].Opcode
== OPCODE_BRK
||
2310 mesa_instructions
[j
].Opcode
== OPCODE_CONT
) {
2311 if (mesa_instructions
[j
].BranchTarget
== -1) {
2312 mesa_instructions
[j
].BranchTarget
= i
;
2316 /* The loop ends point at each other. */
2317 mesa_instructions
[i
].BranchTarget
= loop_stack
[loop_stack_pos
];
2318 mesa_instructions
[loop_stack
[loop_stack_pos
]].BranchTarget
= i
;
2321 foreach_iter(exec_list_iterator
, iter
, v
->function_signatures
) {
2322 function_entry
*entry
= (function_entry
*)iter
.get();
2324 if (entry
->sig_id
== mesa_instructions
[i
].BranchTarget
) {
2325 mesa_instructions
[i
].BranchTarget
= entry
->inst
;
2337 print_program(struct prog_instruction
*mesa_instructions
,
2338 ir_instruction
**mesa_instruction_annotation
,
2339 int num_instructions
)
2341 ir_instruction
*last_ir
= NULL
;
2345 for (i
= 0; i
< num_instructions
; i
++) {
2346 struct prog_instruction
*mesa_inst
= mesa_instructions
+ i
;
2347 ir_instruction
*ir
= mesa_instruction_annotation
[i
];
2349 fprintf(stdout
, "%3d: ", i
);
2351 if (last_ir
!= ir
&& ir
) {
2354 for (j
= 0; j
< indent
; j
++) {
2355 fprintf(stdout
, " ");
2361 fprintf(stdout
, " "); /* line number spacing. */
2364 indent
= _mesa_fprint_instruction_opt(stdout
, mesa_inst
, indent
,
2365 PROG_PRINT_DEBUG
, NULL
);
2371 * Count resources used by the given gpu program (number of texture
2375 count_resources(struct gl_program
*prog
)
2379 prog
->SamplersUsed
= 0;
2381 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
2382 struct prog_instruction
*inst
= &prog
->Instructions
[i
];
2384 if (_mesa_is_tex_instruction(inst
->Opcode
)) {
2385 prog
->SamplerTargets
[inst
->TexSrcUnit
] =
2386 (gl_texture_index
)inst
->TexSrcTarget
;
2387 prog
->SamplersUsed
|= 1 << inst
->TexSrcUnit
;
2388 if (inst
->TexShadow
) {
2389 prog
->ShadowSamplers
|= 1 << inst
->TexSrcUnit
;
2394 _mesa_update_shader_textures_used(prog
);
2399 * Check if the given vertex/fragment/shader program is within the
2400 * resource limits of the context (number of texture units, etc).
2401 * If any of those checks fail, record a linker error.
2403 * XXX more checks are needed...
2406 check_resources(const struct gl_context
*ctx
,
2407 struct gl_shader_program
*shader_program
,
2408 struct gl_program
*prog
)
2410 switch (prog
->Target
) {
2411 case GL_VERTEX_PROGRAM_ARB
:
2412 if (_mesa_bitcount(prog
->SamplersUsed
) >
2413 ctx
->Const
.MaxVertexTextureImageUnits
) {
2414 fail_link(shader_program
, "Too many vertex shader texture samplers");
2416 if (prog
->Parameters
->NumParameters
> MAX_UNIFORMS
) {
2417 fail_link(shader_program
, "Too many vertex shader constants");
2420 case MESA_GEOMETRY_PROGRAM
:
2421 if (_mesa_bitcount(prog
->SamplersUsed
) >
2422 ctx
->Const
.MaxGeometryTextureImageUnits
) {
2423 fail_link(shader_program
, "Too many geometry shader texture samplers");
2425 if (prog
->Parameters
->NumParameters
>
2426 MAX_GEOMETRY_UNIFORM_COMPONENTS
/ 4) {
2427 fail_link(shader_program
, "Too many geometry shader constants");
2430 case GL_FRAGMENT_PROGRAM_ARB
:
2431 if (_mesa_bitcount(prog
->SamplersUsed
) >
2432 ctx
->Const
.MaxTextureImageUnits
) {
2433 fail_link(shader_program
, "Too many fragment shader texture samplers");
2435 if (prog
->Parameters
->NumParameters
> MAX_UNIFORMS
) {
2436 fail_link(shader_program
, "Too many fragment shader constants");
2440 _mesa_problem(ctx
, "unexpected program type in check_resources()");
2446 struct uniform_sort
{
2447 struct gl_uniform
*u
;
2451 /* The shader_program->Uniforms list is almost sorted in increasing
2452 * uniform->{Frag,Vert}Pos locations, but not quite when there are
2453 * uniforms shared between targets. We need to add parameters in
2454 * increasing order for the targets.
2457 sort_uniforms(const void *a
, const void *b
)
2459 struct uniform_sort
*u1
= (struct uniform_sort
*)a
;
2460 struct uniform_sort
*u2
= (struct uniform_sort
*)b
;
2462 return u1
->pos
- u2
->pos
;
2465 /* Add the uniforms to the parameters. The linker chose locations
2466 * in our parameters lists (which weren't created yet), which the
2467 * uniforms code will use to poke values into our parameters list
2468 * when uniforms are updated.
2471 add_uniforms_to_parameters_list(struct gl_shader_program
*shader_program
,
2472 struct gl_shader
*shader
,
2473 struct gl_program
*prog
)
2476 unsigned int next_sampler
= 0, num_uniforms
= 0;
2477 struct uniform_sort
*sorted_uniforms
;
2479 sorted_uniforms
= ralloc_array(NULL
, struct uniform_sort
,
2480 shader_program
->Uniforms
->NumUniforms
);
2482 for (i
= 0; i
< shader_program
->Uniforms
->NumUniforms
; i
++) {
2483 struct gl_uniform
*uniform
= shader_program
->Uniforms
->Uniforms
+ i
;
2484 int parameter_index
= -1;
2486 switch (shader
->Type
) {
2487 case GL_VERTEX_SHADER
:
2488 parameter_index
= uniform
->VertPos
;
2490 case GL_FRAGMENT_SHADER
:
2491 parameter_index
= uniform
->FragPos
;
2493 case GL_GEOMETRY_SHADER
:
2494 parameter_index
= uniform
->GeomPos
;
2498 /* Only add uniforms used in our target. */
2499 if (parameter_index
!= -1) {
2500 sorted_uniforms
[num_uniforms
].pos
= parameter_index
;
2501 sorted_uniforms
[num_uniforms
].u
= uniform
;
2506 qsort(sorted_uniforms
, num_uniforms
, sizeof(struct uniform_sort
),
2509 for (i
= 0; i
< num_uniforms
; i
++) {
2510 struct gl_uniform
*uniform
= sorted_uniforms
[i
].u
;
2511 int parameter_index
= sorted_uniforms
[i
].pos
;
2512 const glsl_type
*type
= uniform
->Type
;
2515 if (type
->is_vector() ||
2516 type
->is_scalar()) {
2517 size
= type
->vector_elements
;
2519 size
= type_size(type
) * 4;
2522 gl_register_file file
;
2523 if (type
->is_sampler() ||
2524 (type
->is_array() && type
->fields
.array
->is_sampler())) {
2525 file
= PROGRAM_SAMPLER
;
2527 file
= PROGRAM_UNIFORM
;
2530 GLint index
= _mesa_lookup_parameter_index(prog
->Parameters
, -1,
2534 index
= _mesa_add_parameter(prog
->Parameters
, file
,
2535 uniform
->Name
, size
, type
->gl_type
,
2538 /* Sampler uniform values are stored in prog->SamplerUnits,
2539 * and the entry in that array is selected by this index we
2540 * store in ParameterValues[].
2542 if (file
== PROGRAM_SAMPLER
) {
2543 for (unsigned int j
= 0; j
< size
/ 4; j
++)
2544 prog
->Parameters
->ParameterValues
[index
+ j
][0] = next_sampler
++;
2547 /* The location chosen in the Parameters list here (returned
2548 * from _mesa_add_uniform) has to match what the linker chose.
2550 if (index
!= parameter_index
) {
2551 fail_link(shader_program
, "Allocation of uniform `%s' to target "
2552 "failed (%d vs %d)\n",
2553 uniform
->Name
, index
, parameter_index
);
2558 ralloc_free(sorted_uniforms
);
2562 set_uniform_initializer(struct gl_context
*ctx
, void *mem_ctx
,
2563 struct gl_shader_program
*shader_program
,
2564 const char *name
, const glsl_type
*type
,
2567 if (type
->is_record()) {
2568 ir_constant
*field_constant
;
2570 field_constant
= (ir_constant
*)val
->components
.get_head();
2572 for (unsigned int i
= 0; i
< type
->length
; i
++) {
2573 const glsl_type
*field_type
= type
->fields
.structure
[i
].type
;
2574 const char *field_name
= ralloc_asprintf(mem_ctx
, "%s.%s", name
,
2575 type
->fields
.structure
[i
].name
);
2576 set_uniform_initializer(ctx
, mem_ctx
, shader_program
, field_name
,
2577 field_type
, field_constant
);
2578 field_constant
= (ir_constant
*)field_constant
->next
;
2583 int loc
= _mesa_get_uniform_location(ctx
, shader_program
, name
);
2586 fail_link(shader_program
,
2587 "Couldn't find uniform for initializer %s\n", name
);
2591 for (unsigned int i
= 0; i
< (type
->is_array() ? type
->length
: 1); i
++) {
2592 ir_constant
*element
;
2593 const glsl_type
*element_type
;
2594 if (type
->is_array()) {
2595 element
= val
->array_elements
[i
];
2596 element_type
= type
->fields
.array
;
2599 element_type
= type
;
2604 if (element_type
->base_type
== GLSL_TYPE_BOOL
) {
2605 int *conv
= ralloc_array(mem_ctx
, int, element_type
->components());
2606 for (unsigned int j
= 0; j
< element_type
->components(); j
++) {
2607 conv
[j
] = element
->value
.b
[j
];
2609 values
= (void *)conv
;
2610 element_type
= glsl_type::get_instance(GLSL_TYPE_INT
,
2611 element_type
->vector_elements
,
2614 values
= &element
->value
;
2617 if (element_type
->is_matrix()) {
2618 _mesa_uniform_matrix(ctx
, shader_program
,
2619 element_type
->matrix_columns
,
2620 element_type
->vector_elements
,
2621 loc
, 1, GL_FALSE
, (GLfloat
*)values
);
2622 loc
+= element_type
->matrix_columns
;
2624 _mesa_uniform(ctx
, shader_program
, loc
, element_type
->matrix_columns
,
2625 values
, element_type
->gl_type
);
2626 loc
+= type_size(element_type
);
2632 set_uniform_initializers(struct gl_context
*ctx
,
2633 struct gl_shader_program
*shader_program
)
2635 void *mem_ctx
= NULL
;
2637 for (unsigned int i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
2638 struct gl_shader
*shader
= shader_program
->_LinkedShaders
[i
];
2643 foreach_iter(exec_list_iterator
, iter
, *shader
->ir
) {
2644 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
2645 ir_variable
*var
= ir
->as_variable();
2647 if (!var
|| var
->mode
!= ir_var_uniform
|| !var
->constant_value
)
2651 mem_ctx
= ralloc_context(NULL
);
2653 set_uniform_initializer(ctx
, mem_ctx
, shader_program
, var
->name
,
2654 var
->type
, var
->constant_value
);
2658 ralloc_free(mem_ctx
);
2662 * On a basic block basis, tracks available PROGRAM_TEMPORARY register
2663 * channels for copy propagation and updates following instructions to
2664 * use the original versions.
2666 * The ir_to_mesa_visitor lazily produces code assuming that this pass
2667 * will occur. As an example, a TXP production before this pass:
2669 * 0: MOV TEMP[1], INPUT[4].xyyy;
2670 * 1: MOV TEMP[1].w, INPUT[4].wwww;
2671 * 2: TXP TEMP[2], TEMP[1], texture[0], 2D;
2675 * 0: MOV TEMP[1], INPUT[4].xyyy;
2676 * 1: MOV TEMP[1].w, INPUT[4].wwww;
2677 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
2679 * which allows for dead code elimination on TEMP[1]'s writes.
2682 ir_to_mesa_visitor::copy_propagate(void)
2684 ir_to_mesa_instruction
**acp
= rzalloc_array(mem_ctx
,
2685 ir_to_mesa_instruction
*,
2686 this->next_temp
* 4);
2687 int *acp_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
2690 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2691 ir_to_mesa_instruction
*inst
= (ir_to_mesa_instruction
*)iter
.get();
2693 assert(inst
->dst
.file
!= PROGRAM_TEMPORARY
2694 || inst
->dst
.index
< this->next_temp
);
2696 /* First, do any copy propagation possible into the src regs. */
2697 for (int r
= 0; r
< 3; r
++) {
2698 ir_to_mesa_instruction
*first
= NULL
;
2700 int acp_base
= inst
->src
[r
].index
* 4;
2702 if (inst
->src
[r
].file
!= PROGRAM_TEMPORARY
||
2703 inst
->src
[r
].reladdr
)
2706 /* See if we can find entries in the ACP consisting of MOVs
2707 * from the same src register for all the swizzled channels
2708 * of this src register reference.
2710 for (int i
= 0; i
< 4; i
++) {
2711 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
2712 ir_to_mesa_instruction
*copy_chan
= acp
[acp_base
+ src_chan
];
2719 assert(acp_level
[acp_base
+ src_chan
] <= level
);
2724 if (first
->src
[0].file
!= copy_chan
->src
[0].file
||
2725 first
->src
[0].index
!= copy_chan
->src
[0].index
) {
2733 /* We've now validated that we can copy-propagate to
2734 * replace this src register reference. Do it.
2736 inst
->src
[r
].file
= first
->src
[0].file
;
2737 inst
->src
[r
].index
= first
->src
[0].index
;
2740 for (int i
= 0; i
< 4; i
++) {
2741 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
2742 ir_to_mesa_instruction
*copy_inst
= acp
[acp_base
+ src_chan
];
2743 swizzle
|= (GET_SWZ(copy_inst
->src
[0].swizzle
, src_chan
) <<
2746 inst
->src
[r
].swizzle
= swizzle
;
2751 case OPCODE_BGNLOOP
:
2752 case OPCODE_ENDLOOP
:
2753 /* End of a basic block, clear the ACP entirely. */
2754 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
2763 /* Clear all channels written inside the block from the ACP, but
2764 * leaving those that were not touched.
2766 for (int r
= 0; r
< this->next_temp
; r
++) {
2767 for (int c
= 0; c
< 4; c
++) {
2768 if (!acp
[4 * r
+ c
])
2771 if (acp_level
[4 * r
+ c
] >= level
)
2772 acp
[4 * r
+ c
] = NULL
;
2775 if (inst
->op
== OPCODE_ENDIF
)
2780 /* Continuing the block, clear any written channels from
2783 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.reladdr
) {
2784 /* Any temporary might be written, so no copy propagation
2785 * across this instruction.
2787 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
2788 } else if (inst
->dst
.file
== PROGRAM_OUTPUT
&&
2789 inst
->dst
.reladdr
) {
2790 /* Any output might be written, so no copy propagation
2791 * from outputs across this instruction.
2793 for (int r
= 0; r
< this->next_temp
; r
++) {
2794 for (int c
= 0; c
< 4; c
++) {
2795 if (!acp
[4 * r
+ c
])
2798 if (acp
[4 * r
+ c
]->src
[0].file
== PROGRAM_OUTPUT
)
2799 acp
[4 * r
+ c
] = NULL
;
2802 } else if (inst
->dst
.file
== PROGRAM_TEMPORARY
||
2803 inst
->dst
.file
== PROGRAM_OUTPUT
) {
2804 /* Clear where it's used as dst. */
2805 if (inst
->dst
.file
== PROGRAM_TEMPORARY
) {
2806 for (int c
= 0; c
< 4; c
++) {
2807 if (inst
->dst
.writemask
& (1 << c
)) {
2808 acp
[4 * inst
->dst
.index
+ c
] = NULL
;
2813 /* Clear where it's used as src. */
2814 for (int r
= 0; r
< this->next_temp
; r
++) {
2815 for (int c
= 0; c
< 4; c
++) {
2816 if (!acp
[4 * r
+ c
])
2819 int src_chan
= GET_SWZ(acp
[4 * r
+ c
]->src
[0].swizzle
, c
);
2821 if (acp
[4 * r
+ c
]->src
[0].file
== inst
->dst
.file
&&
2822 acp
[4 * r
+ c
]->src
[0].index
== inst
->dst
.index
&&
2823 inst
->dst
.writemask
& (1 << src_chan
))
2825 acp
[4 * r
+ c
] = NULL
;
2833 /* If this is a copy, add it to the ACP. */
2834 if (inst
->op
== OPCODE_MOV
&&
2835 inst
->dst
.file
== PROGRAM_TEMPORARY
&&
2836 !inst
->dst
.reladdr
&&
2838 !inst
->src
[0].reladdr
&&
2839 !inst
->src
[0].negate
) {
2840 for (int i
= 0; i
< 4; i
++) {
2841 if (inst
->dst
.writemask
& (1 << i
)) {
2842 acp
[4 * inst
->dst
.index
+ i
] = inst
;
2843 acp_level
[4 * inst
->dst
.index
+ i
] = level
;
2849 ralloc_free(acp_level
);
2855 * Convert a shader's GLSL IR into a Mesa gl_program.
2857 static struct gl_program
*
2858 get_mesa_program(struct gl_context
*ctx
,
2859 struct gl_shader_program
*shader_program
,
2860 struct gl_shader
*shader
)
2862 ir_to_mesa_visitor v
;
2863 struct prog_instruction
*mesa_instructions
, *mesa_inst
;
2864 ir_instruction
**mesa_instruction_annotation
;
2866 struct gl_program
*prog
;
2868 const char *target_string
;
2870 struct gl_shader_compiler_options
*options
=
2871 &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(shader
->Type
)];
2873 switch (shader
->Type
) {
2874 case GL_VERTEX_SHADER
:
2875 target
= GL_VERTEX_PROGRAM_ARB
;
2876 target_string
= "vertex";
2878 case GL_FRAGMENT_SHADER
:
2879 target
= GL_FRAGMENT_PROGRAM_ARB
;
2880 target_string
= "fragment";
2882 case GL_GEOMETRY_SHADER
:
2883 target
= GL_GEOMETRY_PROGRAM_NV
;
2884 target_string
= "geometry";
2887 assert(!"should not be reached");
2891 validate_ir_tree(shader
->ir
);
2893 prog
= ctx
->Driver
.NewProgram(ctx
, target
, shader_program
->Name
);
2896 prog
->Parameters
= _mesa_new_parameter_list();
2897 prog
->Varying
= _mesa_new_parameter_list();
2898 prog
->Attributes
= _mesa_new_parameter_list();
2901 v
.shader_program
= shader_program
;
2902 v
.options
= options
;
2904 add_uniforms_to_parameters_list(shader_program
, shader
, prog
);
2906 /* Emit Mesa IR for main(). */
2907 visit_exec_list(shader
->ir
, &v
);
2908 v
.emit(NULL
, OPCODE_END
);
2910 /* Now emit bodies for any functions that were used. */
2912 progress
= GL_FALSE
;
2914 foreach_iter(exec_list_iterator
, iter
, v
.function_signatures
) {
2915 function_entry
*entry
= (function_entry
*)iter
.get();
2917 if (!entry
->bgn_inst
) {
2918 v
.current_function
= entry
;
2920 entry
->bgn_inst
= v
.emit(NULL
, OPCODE_BGNSUB
);
2921 entry
->bgn_inst
->function
= entry
;
2923 visit_exec_list(&entry
->sig
->body
, &v
);
2925 ir_to_mesa_instruction
*last
;
2926 last
= (ir_to_mesa_instruction
*)v
.instructions
.get_tail();
2927 if (last
->op
!= OPCODE_RET
)
2928 v
.emit(NULL
, OPCODE_RET
);
2930 ir_to_mesa_instruction
*end
;
2931 end
= v
.emit(NULL
, OPCODE_ENDSUB
);
2932 end
->function
= entry
;
2939 prog
->NumTemporaries
= v
.next_temp
;
2941 int num_instructions
= 0;
2942 foreach_iter(exec_list_iterator
, iter
, v
.instructions
) {
2947 (struct prog_instruction
*)calloc(num_instructions
,
2948 sizeof(*mesa_instructions
));
2949 mesa_instruction_annotation
= ralloc_array(v
.mem_ctx
, ir_instruction
*,
2954 /* Convert ir_mesa_instructions into prog_instructions.
2956 mesa_inst
= mesa_instructions
;
2958 foreach_iter(exec_list_iterator
, iter
, v
.instructions
) {
2959 const ir_to_mesa_instruction
*inst
= (ir_to_mesa_instruction
*)iter
.get();
2961 mesa_inst
->Opcode
= inst
->op
;
2962 mesa_inst
->CondUpdate
= inst
->cond_update
;
2964 mesa_inst
->SaturateMode
= SATURATE_ZERO_ONE
;
2965 mesa_inst
->DstReg
.File
= inst
->dst
.file
;
2966 mesa_inst
->DstReg
.Index
= inst
->dst
.index
;
2967 mesa_inst
->DstReg
.CondMask
= inst
->dst
.cond_mask
;
2968 mesa_inst
->DstReg
.WriteMask
= inst
->dst
.writemask
;
2969 mesa_inst
->DstReg
.RelAddr
= inst
->dst
.reladdr
!= NULL
;
2970 mesa_inst
->SrcReg
[0] = mesa_src_reg_from_ir_src_reg(inst
->src
[0]);
2971 mesa_inst
->SrcReg
[1] = mesa_src_reg_from_ir_src_reg(inst
->src
[1]);
2972 mesa_inst
->SrcReg
[2] = mesa_src_reg_from_ir_src_reg(inst
->src
[2]);
2973 mesa_inst
->TexSrcUnit
= inst
->sampler
;
2974 mesa_inst
->TexSrcTarget
= inst
->tex_target
;
2975 mesa_inst
->TexShadow
= inst
->tex_shadow
;
2976 mesa_instruction_annotation
[i
] = inst
->ir
;
2978 /* Set IndirectRegisterFiles. */
2979 if (mesa_inst
->DstReg
.RelAddr
)
2980 prog
->IndirectRegisterFiles
|= 1 << mesa_inst
->DstReg
.File
;
2982 /* Update program's bitmask of indirectly accessed register files */
2983 for (unsigned src
= 0; src
< 3; src
++)
2984 if (mesa_inst
->SrcReg
[src
].RelAddr
)
2985 prog
->IndirectRegisterFiles
|= 1 << mesa_inst
->SrcReg
[src
].File
;
2987 if (options
->EmitNoIfs
&& mesa_inst
->Opcode
== OPCODE_IF
) {
2988 fail_link(shader_program
, "Couldn't flatten if statement\n");
2991 switch (mesa_inst
->Opcode
) {
2993 inst
->function
->inst
= i
;
2994 mesa_inst
->Comment
= strdup(inst
->function
->sig
->function_name());
2997 mesa_inst
->Comment
= strdup(inst
->function
->sig
->function_name());
3000 mesa_inst
->BranchTarget
= inst
->function
->sig_id
; /* rewritten later */
3003 prog
->NumAddressRegs
= 1;
3012 if (!shader_program
->LinkStatus
)
3016 if (!shader_program
->LinkStatus
) {
3017 free(mesa_instructions
);
3018 _mesa_reference_program(ctx
, &shader
->Program
, NULL
);
3022 set_branchtargets(&v
, mesa_instructions
, num_instructions
);
3024 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
3026 printf("GLSL IR for linked %s program %d:\n", target_string
,
3027 shader_program
->Name
);
3028 _mesa_print_ir(shader
->ir
, NULL
);
3031 printf("Mesa IR for linked %s program %d:\n", target_string
,
3032 shader_program
->Name
);
3033 print_program(mesa_instructions
, mesa_instruction_annotation
,
3037 prog
->Instructions
= mesa_instructions
;
3038 prog
->NumInstructions
= num_instructions
;
3040 do_set_program_inouts(shader
->ir
, prog
);
3041 count_resources(prog
);
3043 check_resources(ctx
, shader_program
, prog
);
3045 _mesa_reference_program(ctx
, &shader
->Program
, prog
);
3047 if ((ctx
->Shader
.Flags
& GLSL_NO_OPT
) == 0) {
3048 _mesa_optimize_program(ctx
, prog
);
3058 * Called via ctx->Driver.LinkShader()
3059 * This actually involves converting GLSL IR into Mesa gl_programs with
3060 * code lowering and other optimizations.
3063 _mesa_ir_link_shader(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
3065 assert(prog
->LinkStatus
);
3067 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
3068 if (prog
->_LinkedShaders
[i
] == NULL
)
3072 exec_list
*ir
= prog
->_LinkedShaders
[i
]->ir
;
3073 const struct gl_shader_compiler_options
*options
=
3074 &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(prog
->_LinkedShaders
[i
]->Type
)];
3080 do_mat_op_to_vec(ir
);
3081 lower_instructions(ir
, (MOD_TO_FRACT
| DIV_TO_MUL_RCP
| EXP_TO_EXP2
3083 | ((options
->EmitNoPow
) ? POW_TO_EXP2
: 0)));
3085 progress
= do_lower_jumps(ir
, true, true, options
->EmitNoMainReturn
, options
->EmitNoCont
, options
->EmitNoLoops
) || progress
;
3087 progress
= do_common_optimization(ir
, true, options
->MaxUnrollIterations
) || progress
;
3089 progress
= lower_quadop_vector(ir
, true) || progress
;
3091 if (options
->EmitNoIfs
) {
3092 progress
= lower_discard(ir
) || progress
;
3093 progress
= lower_if_to_cond_assign(ir
) || progress
;
3096 if (options
->EmitNoNoise
)
3097 progress
= lower_noise(ir
) || progress
;
3099 /* If there are forms of indirect addressing that the driver
3100 * cannot handle, perform the lowering pass.
3102 if (options
->EmitNoIndirectInput
|| options
->EmitNoIndirectOutput
3103 || options
->EmitNoIndirectTemp
|| options
->EmitNoIndirectUniform
)
3105 lower_variable_index_to_cond_assign(ir
,
3106 options
->EmitNoIndirectInput
,
3107 options
->EmitNoIndirectOutput
,
3108 options
->EmitNoIndirectTemp
,
3109 options
->EmitNoIndirectUniform
)
3112 progress
= do_vec_index_to_cond_assign(ir
) || progress
;
3115 validate_ir_tree(ir
);
3118 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
3119 struct gl_program
*linked_prog
;
3121 if (prog
->_LinkedShaders
[i
] == NULL
)
3124 linked_prog
= get_mesa_program(ctx
, prog
, prog
->_LinkedShaders
[i
]);
3129 switch (prog
->_LinkedShaders
[i
]->Type
) {
3130 case GL_VERTEX_SHADER
:
3131 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
,
3132 (struct gl_vertex_program
*)linked_prog
);
3133 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_VERTEX_PROGRAM_ARB
,
3136 case GL_FRAGMENT_SHADER
:
3137 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
,
3138 (struct gl_fragment_program
*)linked_prog
);
3139 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
3142 case GL_GEOMETRY_SHADER
:
3143 _mesa_reference_geomprog(ctx
, &prog
->GeometryProgram
,
3144 (struct gl_geometry_program
*)linked_prog
);
3145 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_GEOMETRY_PROGRAM_NV
,
3154 _mesa_reference_program(ctx
, &linked_prog
, NULL
);
3162 * Compile a GLSL shader. Called via glCompileShader().
3165 _mesa_glsl_compile_shader(struct gl_context
*ctx
, struct gl_shader
*shader
)
3167 struct _mesa_glsl_parse_state
*state
=
3168 new(shader
) _mesa_glsl_parse_state(ctx
, shader
->Type
, shader
);
3170 const char *source
= shader
->Source
;
3171 /* Check if the user called glCompileShader without first calling
3172 * glShaderSource. This should fail to compile, but not raise a GL_ERROR.
3174 if (source
== NULL
) {
3175 shader
->CompileStatus
= GL_FALSE
;
3179 state
->error
= preprocess(state
, &source
, &state
->info_log
,
3180 &ctx
->Extensions
, ctx
->API
);
3182 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
3183 printf("GLSL source for %s shader %d:\n",
3184 _mesa_glsl_shader_target_name(state
->target
), shader
->Name
);
3185 printf("%s\n", shader
->Source
);
3188 if (!state
->error
) {
3189 _mesa_glsl_lexer_ctor(state
, source
);
3190 _mesa_glsl_parse(state
);
3191 _mesa_glsl_lexer_dtor(state
);
3194 ralloc_free(shader
->ir
);
3195 shader
->ir
= new(shader
) exec_list
;
3196 if (!state
->error
&& !state
->translation_unit
.is_empty())
3197 _mesa_ast_to_hir(shader
->ir
, state
);
3199 if (!state
->error
&& !shader
->ir
->is_empty()) {
3200 validate_ir_tree(shader
->ir
);
3202 /* Do some optimization at compile time to reduce shader IR size
3203 * and reduce later work if the same shader is linked multiple times
3205 while (do_common_optimization(shader
->ir
, false, 32))
3208 validate_ir_tree(shader
->ir
);
3211 shader
->symbols
= state
->symbols
;
3213 shader
->CompileStatus
= !state
->error
;
3214 shader
->InfoLog
= state
->info_log
;
3215 shader
->Version
= state
->language_version
;
3216 memcpy(shader
->builtins_to_link
, state
->builtins_to_link
,
3217 sizeof(shader
->builtins_to_link
[0]) * state
->num_builtins_to_link
);
3218 shader
->num_builtins_to_link
= state
->num_builtins_to_link
;
3220 if (ctx
->Shader
.Flags
& GLSL_LOG
) {
3221 _mesa_write_shader_to_file(shader
);
3224 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
3225 if (shader
->CompileStatus
) {
3226 printf("GLSL IR for shader %d:\n", shader
->Name
);
3227 _mesa_print_ir(shader
->ir
, NULL
);
3230 printf("GLSL shader %d failed to compile.\n", shader
->Name
);
3232 if (shader
->InfoLog
&& shader
->InfoLog
[0] != 0) {
3233 printf("GLSL shader %d info log:\n", shader
->Name
);
3234 printf("%s\n", shader
->InfoLog
);
3238 /* Retain any live IR, but trash the rest. */
3239 reparent_ir(shader
->ir
, shader
->ir
);
3246 * Link a GLSL shader program. Called via glLinkProgram().
3249 _mesa_glsl_link_shader(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
3253 _mesa_clear_shader_program_data(ctx
, prog
);
3255 prog
->LinkStatus
= GL_TRUE
;
3257 for (i
= 0; i
< prog
->NumShaders
; i
++) {
3258 if (!prog
->Shaders
[i
]->CompileStatus
) {
3259 fail_link(prog
, "linking with uncompiled shader");
3260 prog
->LinkStatus
= GL_FALSE
;
3264 prog
->Varying
= _mesa_new_parameter_list();
3265 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
, NULL
);
3266 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
, NULL
);
3267 _mesa_reference_geomprog(ctx
, &prog
->GeometryProgram
, NULL
);
3269 if (prog
->LinkStatus
) {
3270 link_shaders(ctx
, prog
);
3273 if (prog
->LinkStatus
) {
3274 if (!ctx
->Driver
.LinkShader(ctx
, prog
)) {
3275 prog
->LinkStatus
= GL_FALSE
;
3279 set_uniform_initializers(ctx
, prog
);
3281 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
3282 if (!prog
->LinkStatus
) {
3283 printf("GLSL shader program %d failed to link\n", prog
->Name
);
3286 if (prog
->InfoLog
&& prog
->InfoLog
[0] != 0) {
3287 printf("GLSL shader program %d info log:\n", prog
->Name
);
3288 printf("%s\n", prog
->InfoLog
);