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), user-assign
1419 * generic attributes (glBindVertexLocation), and
1420 * 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 src
.reladdr
= ralloc(mem_ctx
, src_reg
);
1500 memcpy(src
.reladdr
, &index_reg
, sizeof(index_reg
));
1503 /* If the type is smaller than a vec4, replicate the last channel out. */
1504 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
1505 src
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1507 src
.swizzle
= SWIZZLE_NOOP
;
1513 ir_to_mesa_visitor::visit(ir_dereference_record
*ir
)
1516 const glsl_type
*struct_type
= ir
->record
->type
;
1519 ir
->record
->accept(this);
1521 for (i
= 0; i
< struct_type
->length
; i
++) {
1522 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
1524 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
1527 /* If the type is smaller than a vec4, replicate the last channel out. */
1528 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
1529 this->result
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1531 this->result
.swizzle
= SWIZZLE_NOOP
;
1533 this->result
.index
+= offset
;
1537 * We want to be careful in assignment setup to hit the actual storage
1538 * instead of potentially using a temporary like we might with the
1539 * ir_dereference handler.
1542 get_assignment_lhs(ir_dereference
*ir
, ir_to_mesa_visitor
*v
)
1544 /* The LHS must be a dereference. If the LHS is a variable indexed array
1545 * access of a vector, it must be separated into a series conditional moves
1546 * before reaching this point (see ir_vec_index_to_cond_assign).
1548 assert(ir
->as_dereference());
1549 ir_dereference_array
*deref_array
= ir
->as_dereference_array();
1551 assert(!deref_array
->array
->type
->is_vector());
1554 /* Use the rvalue deref handler for the most part. We'll ignore
1555 * swizzles in it and write swizzles using writemask, though.
1558 return dst_reg(v
->result
);
1562 * Process the condition of a conditional assignment
1564 * Examines the condition of a conditional assignment to generate the optimal
1565 * first operand of a \c CMP instruction. If the condition is a relational
1566 * operator with 0 (e.g., \c ir_binop_less), the value being compared will be
1567 * used as the source for the \c CMP instruction. Otherwise the comparison
1568 * is processed to a boolean result, and the boolean result is used as the
1569 * operand to the CMP instruction.
1572 ir_to_mesa_visitor::process_move_condition(ir_rvalue
*ir
)
1574 ir_rvalue
*src_ir
= ir
;
1576 bool switch_order
= false;
1578 ir_expression
*const expr
= ir
->as_expression();
1579 if ((expr
!= NULL
) && (expr
->get_num_operands() == 2)) {
1580 bool zero_on_left
= false;
1582 if (expr
->operands
[0]->is_zero()) {
1583 src_ir
= expr
->operands
[1];
1584 zero_on_left
= true;
1585 } else if (expr
->operands
[1]->is_zero()) {
1586 src_ir
= expr
->operands
[0];
1587 zero_on_left
= false;
1591 * (a < 0) T F F ( a < 0) T F F
1592 * (0 < a) F F T (-a < 0) F F T
1593 * (a <= 0) T T F (-a < 0) F F T (swap order of other operands)
1594 * (0 <= a) F T T ( a < 0) T F F (swap order of other operands)
1595 * (a > 0) F F T (-a < 0) F F T
1596 * (0 > a) T F F ( a < 0) T F F
1597 * (a >= 0) F T T ( a < 0) T F F (swap order of other operands)
1598 * (0 >= a) T T F (-a < 0) F F T (swap order of other operands)
1600 * Note that exchanging the order of 0 and 'a' in the comparison simply
1601 * means that the value of 'a' should be negated.
1604 switch (expr
->operation
) {
1606 switch_order
= false;
1607 negate
= zero_on_left
;
1610 case ir_binop_greater
:
1611 switch_order
= false;
1612 negate
= !zero_on_left
;
1615 case ir_binop_lequal
:
1616 switch_order
= true;
1617 negate
= !zero_on_left
;
1620 case ir_binop_gequal
:
1621 switch_order
= true;
1622 negate
= zero_on_left
;
1626 /* This isn't the right kind of comparison afterall, so make sure
1627 * the whole condition is visited.
1635 src_ir
->accept(this);
1637 /* We use the OPCODE_CMP (a < 0 ? b : c) for conditional moves, and the
1638 * condition we produced is 0.0 or 1.0. By flipping the sign, we can
1639 * choose which value OPCODE_CMP produces without an extra instruction
1640 * computing the condition.
1643 this->result
.negate
= ~this->result
.negate
;
1645 return switch_order
;
1649 ir_to_mesa_visitor::visit(ir_assignment
*ir
)
1655 ir
->rhs
->accept(this);
1658 l
= get_assignment_lhs(ir
->lhs
, this);
1660 /* FINISHME: This should really set to the correct maximal writemask for each
1661 * FINISHME: component written (in the loops below). This case can only
1662 * FINISHME: occur for matrices, arrays, and structures.
1664 if (ir
->write_mask
== 0) {
1665 assert(!ir
->lhs
->type
->is_scalar() && !ir
->lhs
->type
->is_vector());
1666 l
.writemask
= WRITEMASK_XYZW
;
1667 } else if (ir
->lhs
->type
->is_scalar()) {
1668 /* FINISHME: This hack makes writing to gl_FragDepth, which lives in the
1669 * FINISHME: W component of fragment shader output zero, work correctly.
1671 l
.writemask
= WRITEMASK_XYZW
;
1674 int first_enabled_chan
= 0;
1677 assert(ir
->lhs
->type
->is_vector());
1678 l
.writemask
= ir
->write_mask
;
1680 for (int i
= 0; i
< 4; i
++) {
1681 if (l
.writemask
& (1 << i
)) {
1682 first_enabled_chan
= GET_SWZ(r
.swizzle
, i
);
1687 /* Swizzle a small RHS vector into the channels being written.
1689 * glsl ir treats write_mask as dictating how many channels are
1690 * present on the RHS while Mesa IR treats write_mask as just
1691 * showing which channels of the vec4 RHS get written.
1693 for (int i
= 0; i
< 4; i
++) {
1694 if (l
.writemask
& (1 << i
))
1695 swizzles
[i
] = GET_SWZ(r
.swizzle
, rhs_chan
++);
1697 swizzles
[i
] = first_enabled_chan
;
1699 r
.swizzle
= MAKE_SWIZZLE4(swizzles
[0], swizzles
[1],
1700 swizzles
[2], swizzles
[3]);
1703 assert(l
.file
!= PROGRAM_UNDEFINED
);
1704 assert(r
.file
!= PROGRAM_UNDEFINED
);
1706 if (ir
->condition
) {
1707 const bool switch_order
= this->process_move_condition(ir
->condition
);
1708 src_reg condition
= this->result
;
1710 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
1712 emit(ir
, OPCODE_CMP
, l
, condition
, src_reg(l
), r
);
1714 emit(ir
, OPCODE_CMP
, l
, condition
, r
, src_reg(l
));
1721 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
1722 emit(ir
, OPCODE_MOV
, l
, r
);
1731 ir_to_mesa_visitor::visit(ir_constant
*ir
)
1734 GLfloat stack_vals
[4] = { 0 };
1735 GLfloat
*values
= stack_vals
;
1738 /* Unfortunately, 4 floats is all we can get into
1739 * _mesa_add_unnamed_constant. So, make a temp to store an
1740 * aggregate constant and move each constant value into it. If we
1741 * get lucky, copy propagation will eliminate the extra moves.
1744 if (ir
->type
->base_type
== GLSL_TYPE_STRUCT
) {
1745 src_reg temp_base
= get_temp(ir
->type
);
1746 dst_reg temp
= dst_reg(temp_base
);
1748 foreach_iter(exec_list_iterator
, iter
, ir
->components
) {
1749 ir_constant
*field_value
= (ir_constant
*)iter
.get();
1750 int size
= type_size(field_value
->type
);
1754 field_value
->accept(this);
1757 for (i
= 0; i
< (unsigned int)size
; i
++) {
1758 emit(ir
, OPCODE_MOV
, temp
, src
);
1764 this->result
= temp_base
;
1768 if (ir
->type
->is_array()) {
1769 src_reg temp_base
= get_temp(ir
->type
);
1770 dst_reg temp
= dst_reg(temp_base
);
1771 int size
= type_size(ir
->type
->fields
.array
);
1775 for (i
= 0; i
< ir
->type
->length
; i
++) {
1776 ir
->array_elements
[i
]->accept(this);
1778 for (int j
= 0; j
< size
; j
++) {
1779 emit(ir
, OPCODE_MOV
, temp
, src
);
1785 this->result
= temp_base
;
1789 if (ir
->type
->is_matrix()) {
1790 src_reg mat
= get_temp(ir
->type
);
1791 dst_reg mat_column
= dst_reg(mat
);
1793 for (i
= 0; i
< ir
->type
->matrix_columns
; i
++) {
1794 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
1795 values
= &ir
->value
.f
[i
* ir
->type
->vector_elements
];
1797 src
= src_reg(PROGRAM_CONSTANT
, -1, NULL
);
1798 src
.index
= _mesa_add_unnamed_constant(this->prog
->Parameters
,
1800 ir
->type
->vector_elements
,
1802 emit(ir
, OPCODE_MOV
, mat_column
, src
);
1811 src
.file
= PROGRAM_CONSTANT
;
1812 switch (ir
->type
->base_type
) {
1813 case GLSL_TYPE_FLOAT
:
1814 values
= &ir
->value
.f
[0];
1816 case GLSL_TYPE_UINT
:
1817 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1818 values
[i
] = ir
->value
.u
[i
];
1822 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1823 values
[i
] = ir
->value
.i
[i
];
1826 case GLSL_TYPE_BOOL
:
1827 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1828 values
[i
] = ir
->value
.b
[i
];
1832 assert(!"Non-float/uint/int/bool constant");
1835 this->result
= src_reg(PROGRAM_CONSTANT
, -1, ir
->type
);
1836 this->result
.index
= _mesa_add_unnamed_constant(this->prog
->Parameters
,
1838 ir
->type
->vector_elements
,
1839 &this->result
.swizzle
);
1843 ir_to_mesa_visitor::get_function_signature(ir_function_signature
*sig
)
1845 function_entry
*entry
;
1847 foreach_iter(exec_list_iterator
, iter
, this->function_signatures
) {
1848 entry
= (function_entry
*)iter
.get();
1850 if (entry
->sig
== sig
)
1854 entry
= ralloc(mem_ctx
, function_entry
);
1856 entry
->sig_id
= this->next_signature_id
++;
1857 entry
->bgn_inst
= NULL
;
1859 /* Allocate storage for all the parameters. */
1860 foreach_iter(exec_list_iterator
, iter
, sig
->parameters
) {
1861 ir_variable
*param
= (ir_variable
*)iter
.get();
1862 variable_storage
*storage
;
1864 storage
= find_variable_storage(param
);
1867 storage
= new(mem_ctx
) variable_storage(param
, PROGRAM_TEMPORARY
,
1869 this->variables
.push_tail(storage
);
1871 this->next_temp
+= type_size(param
->type
);
1874 if (!sig
->return_type
->is_void()) {
1875 entry
->return_reg
= get_temp(sig
->return_type
);
1877 entry
->return_reg
= undef_src
;
1880 this->function_signatures
.push_tail(entry
);
1885 ir_to_mesa_visitor::visit(ir_call
*ir
)
1887 ir_to_mesa_instruction
*call_inst
;
1888 ir_function_signature
*sig
= ir
->get_callee();
1889 function_entry
*entry
= get_function_signature(sig
);
1892 /* Process in parameters. */
1893 exec_list_iterator sig_iter
= sig
->parameters
.iterator();
1894 foreach_iter(exec_list_iterator
, iter
, *ir
) {
1895 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
1896 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
1898 if (param
->mode
== ir_var_in
||
1899 param
->mode
== ir_var_inout
) {
1900 variable_storage
*storage
= find_variable_storage(param
);
1903 param_rval
->accept(this);
1904 src_reg r
= this->result
;
1907 l
.file
= storage
->file
;
1908 l
.index
= storage
->index
;
1910 l
.writemask
= WRITEMASK_XYZW
;
1911 l
.cond_mask
= COND_TR
;
1913 for (i
= 0; i
< type_size(param
->type
); i
++) {
1914 emit(ir
, OPCODE_MOV
, l
, r
);
1922 assert(!sig_iter
.has_next());
1924 /* Emit call instruction */
1925 call_inst
= emit(ir
, OPCODE_CAL
);
1926 call_inst
->function
= entry
;
1928 /* Process out parameters. */
1929 sig_iter
= sig
->parameters
.iterator();
1930 foreach_iter(exec_list_iterator
, iter
, *ir
) {
1931 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
1932 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
1934 if (param
->mode
== ir_var_out
||
1935 param
->mode
== ir_var_inout
) {
1936 variable_storage
*storage
= find_variable_storage(param
);
1940 r
.file
= storage
->file
;
1941 r
.index
= storage
->index
;
1943 r
.swizzle
= SWIZZLE_NOOP
;
1946 param_rval
->accept(this);
1947 dst_reg l
= dst_reg(this->result
);
1949 for (i
= 0; i
< type_size(param
->type
); i
++) {
1950 emit(ir
, OPCODE_MOV
, l
, r
);
1958 assert(!sig_iter
.has_next());
1960 /* Process return value. */
1961 this->result
= entry
->return_reg
;
1965 ir_to_mesa_visitor::visit(ir_texture
*ir
)
1967 src_reg result_src
, coord
, lod_info
, projector
, dx
, dy
;
1968 dst_reg result_dst
, coord_dst
;
1969 ir_to_mesa_instruction
*inst
= NULL
;
1970 prog_opcode opcode
= OPCODE_NOP
;
1972 ir
->coordinate
->accept(this);
1974 /* Put our coords in a temp. We'll need to modify them for shadow,
1975 * projection, or LOD, so the only case we'd use it as is is if
1976 * we're doing plain old texturing. Mesa IR optimization should
1977 * handle cleaning up our mess in that case.
1979 coord
= get_temp(glsl_type::vec4_type
);
1980 coord_dst
= dst_reg(coord
);
1981 emit(ir
, OPCODE_MOV
, coord_dst
, this->result
);
1983 if (ir
->projector
) {
1984 ir
->projector
->accept(this);
1985 projector
= this->result
;
1988 /* Storage for our result. Ideally for an assignment we'd be using
1989 * the actual storage for the result here, instead.
1991 result_src
= get_temp(glsl_type::vec4_type
);
1992 result_dst
= dst_reg(result_src
);
1996 opcode
= OPCODE_TEX
;
1999 opcode
= OPCODE_TXB
;
2000 ir
->lod_info
.bias
->accept(this);
2001 lod_info
= this->result
;
2004 opcode
= OPCODE_TXL
;
2005 ir
->lod_info
.lod
->accept(this);
2006 lod_info
= this->result
;
2009 opcode
= OPCODE_TXD
;
2010 ir
->lod_info
.grad
.dPdx
->accept(this);
2012 ir
->lod_info
.grad
.dPdy
->accept(this);
2016 assert(!"GLSL 1.30 features unsupported");
2020 if (ir
->projector
) {
2021 if (opcode
== OPCODE_TEX
) {
2022 /* Slot the projector in as the last component of the coord. */
2023 coord_dst
.writemask
= WRITEMASK_W
;
2024 emit(ir
, OPCODE_MOV
, coord_dst
, projector
);
2025 coord_dst
.writemask
= WRITEMASK_XYZW
;
2026 opcode
= OPCODE_TXP
;
2028 src_reg coord_w
= coord
;
2029 coord_w
.swizzle
= SWIZZLE_WWWW
;
2031 /* For the other TEX opcodes there's no projective version
2032 * since the last slot is taken up by lod info. Do the
2033 * projective divide now.
2035 coord_dst
.writemask
= WRITEMASK_W
;
2036 emit(ir
, OPCODE_RCP
, coord_dst
, projector
);
2038 /* In the case where we have to project the coordinates "by hand,"
2039 * the shadow comparitor value must also be projected.
2041 src_reg tmp_src
= coord
;
2042 if (ir
->shadow_comparitor
) {
2043 /* Slot the shadow value in as the second to last component of the
2046 ir
->shadow_comparitor
->accept(this);
2048 tmp_src
= get_temp(glsl_type::vec4_type
);
2049 dst_reg tmp_dst
= dst_reg(tmp_src
);
2051 tmp_dst
.writemask
= WRITEMASK_Z
;
2052 emit(ir
, OPCODE_MOV
, tmp_dst
, this->result
);
2054 tmp_dst
.writemask
= WRITEMASK_XY
;
2055 emit(ir
, OPCODE_MOV
, tmp_dst
, coord
);
2058 coord_dst
.writemask
= WRITEMASK_XYZ
;
2059 emit(ir
, OPCODE_MUL
, coord_dst
, tmp_src
, coord_w
);
2061 coord_dst
.writemask
= WRITEMASK_XYZW
;
2062 coord
.swizzle
= SWIZZLE_XYZW
;
2066 /* If projection is done and the opcode is not OPCODE_TXP, then the shadow
2067 * comparitor was put in the correct place (and projected) by the code,
2068 * above, that handles by-hand projection.
2070 if (ir
->shadow_comparitor
&& (!ir
->projector
|| opcode
== OPCODE_TXP
)) {
2071 /* Slot the shadow value in as the second to last component of the
2074 ir
->shadow_comparitor
->accept(this);
2075 coord_dst
.writemask
= WRITEMASK_Z
;
2076 emit(ir
, OPCODE_MOV
, coord_dst
, this->result
);
2077 coord_dst
.writemask
= WRITEMASK_XYZW
;
2080 if (opcode
== OPCODE_TXL
|| opcode
== OPCODE_TXB
) {
2081 /* Mesa IR stores lod or lod bias in the last channel of the coords. */
2082 coord_dst
.writemask
= WRITEMASK_W
;
2083 emit(ir
, OPCODE_MOV
, coord_dst
, lod_info
);
2084 coord_dst
.writemask
= WRITEMASK_XYZW
;
2087 if (opcode
== OPCODE_TXD
)
2088 inst
= emit(ir
, opcode
, result_dst
, coord
, dx
, dy
);
2090 inst
= emit(ir
, opcode
, result_dst
, coord
);
2092 if (ir
->shadow_comparitor
)
2093 inst
->tex_shadow
= GL_TRUE
;
2095 inst
->sampler
= _mesa_get_sampler_uniform_value(ir
->sampler
,
2096 this->shader_program
,
2099 const glsl_type
*sampler_type
= ir
->sampler
->type
;
2101 switch (sampler_type
->sampler_dimensionality
) {
2102 case GLSL_SAMPLER_DIM_1D
:
2103 inst
->tex_target
= (sampler_type
->sampler_array
)
2104 ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
2106 case GLSL_SAMPLER_DIM_2D
:
2107 inst
->tex_target
= (sampler_type
->sampler_array
)
2108 ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
2110 case GLSL_SAMPLER_DIM_3D
:
2111 inst
->tex_target
= TEXTURE_3D_INDEX
;
2113 case GLSL_SAMPLER_DIM_CUBE
:
2114 inst
->tex_target
= TEXTURE_CUBE_INDEX
;
2116 case GLSL_SAMPLER_DIM_RECT
:
2117 inst
->tex_target
= TEXTURE_RECT_INDEX
;
2119 case GLSL_SAMPLER_DIM_BUF
:
2120 assert(!"FINISHME: Implement ARB_texture_buffer_object");
2123 assert(!"Should not get here.");
2126 this->result
= result_src
;
2130 ir_to_mesa_visitor::visit(ir_return
*ir
)
2132 if (ir
->get_value()) {
2136 assert(current_function
);
2138 ir
->get_value()->accept(this);
2139 src_reg r
= this->result
;
2141 l
= dst_reg(current_function
->return_reg
);
2143 for (i
= 0; i
< type_size(current_function
->sig
->return_type
); i
++) {
2144 emit(ir
, OPCODE_MOV
, l
, r
);
2150 emit(ir
, OPCODE_RET
);
2154 ir_to_mesa_visitor::visit(ir_discard
*ir
)
2156 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
2158 if (ir
->condition
) {
2159 ir
->condition
->accept(this);
2160 this->result
.negate
= ~this->result
.negate
;
2161 emit(ir
, OPCODE_KIL
, undef_dst
, this->result
);
2163 emit(ir
, OPCODE_KIL_NV
);
2166 fp
->UsesKill
= GL_TRUE
;
2170 ir_to_mesa_visitor::visit(ir_if
*ir
)
2172 ir_to_mesa_instruction
*cond_inst
, *if_inst
;
2173 ir_to_mesa_instruction
*prev_inst
;
2175 prev_inst
= (ir_to_mesa_instruction
*)this->instructions
.get_tail();
2177 ir
->condition
->accept(this);
2178 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
2180 if (this->options
->EmitCondCodes
) {
2181 cond_inst
= (ir_to_mesa_instruction
*)this->instructions
.get_tail();
2183 /* See if we actually generated any instruction for generating
2184 * the condition. If not, then cook up a move to a temp so we
2185 * have something to set cond_update on.
2187 if (cond_inst
== prev_inst
) {
2188 src_reg temp
= get_temp(glsl_type::bool_type
);
2189 cond_inst
= emit(ir
->condition
, OPCODE_MOV
, dst_reg(temp
), result
);
2191 cond_inst
->cond_update
= GL_TRUE
;
2193 if_inst
= emit(ir
->condition
, OPCODE_IF
);
2194 if_inst
->dst
.cond_mask
= COND_NE
;
2196 if_inst
= emit(ir
->condition
, OPCODE_IF
, undef_dst
, this->result
);
2199 this->instructions
.push_tail(if_inst
);
2201 visit_exec_list(&ir
->then_instructions
, this);
2203 if (!ir
->else_instructions
.is_empty()) {
2204 emit(ir
->condition
, OPCODE_ELSE
);
2205 visit_exec_list(&ir
->else_instructions
, this);
2208 if_inst
= emit(ir
->condition
, OPCODE_ENDIF
);
2211 ir_to_mesa_visitor::ir_to_mesa_visitor()
2213 result
.file
= PROGRAM_UNDEFINED
;
2215 next_signature_id
= 1;
2216 current_function
= NULL
;
2217 mem_ctx
= ralloc_context(NULL
);
2220 ir_to_mesa_visitor::~ir_to_mesa_visitor()
2222 ralloc_free(mem_ctx
);
2225 static struct prog_src_register
2226 mesa_src_reg_from_ir_src_reg(src_reg reg
)
2228 struct prog_src_register mesa_reg
;
2230 mesa_reg
.File
= reg
.file
;
2231 assert(reg
.index
< (1 << INST_INDEX_BITS
));
2232 mesa_reg
.Index
= reg
.index
;
2233 mesa_reg
.Swizzle
= reg
.swizzle
;
2234 mesa_reg
.RelAddr
= reg
.reladdr
!= NULL
;
2235 mesa_reg
.Negate
= reg
.negate
;
2237 mesa_reg
.HasIndex2
= GL_FALSE
;
2238 mesa_reg
.RelAddr2
= 0;
2239 mesa_reg
.Index2
= 0;
2245 set_branchtargets(ir_to_mesa_visitor
*v
,
2246 struct prog_instruction
*mesa_instructions
,
2247 int num_instructions
)
2249 int if_count
= 0, loop_count
= 0;
2250 int *if_stack
, *loop_stack
;
2251 int if_stack_pos
= 0, loop_stack_pos
= 0;
2254 for (i
= 0; i
< num_instructions
; i
++) {
2255 switch (mesa_instructions
[i
].Opcode
) {
2259 case OPCODE_BGNLOOP
:
2264 mesa_instructions
[i
].BranchTarget
= -1;
2271 if_stack
= rzalloc_array(v
->mem_ctx
, int, if_count
);
2272 loop_stack
= rzalloc_array(v
->mem_ctx
, int, loop_count
);
2274 for (i
= 0; i
< num_instructions
; i
++) {
2275 switch (mesa_instructions
[i
].Opcode
) {
2277 if_stack
[if_stack_pos
] = i
;
2281 mesa_instructions
[if_stack
[if_stack_pos
- 1]].BranchTarget
= i
;
2282 if_stack
[if_stack_pos
- 1] = i
;
2285 mesa_instructions
[if_stack
[if_stack_pos
- 1]].BranchTarget
= i
;
2288 case OPCODE_BGNLOOP
:
2289 loop_stack
[loop_stack_pos
] = i
;
2292 case OPCODE_ENDLOOP
:
2294 /* Rewrite any breaks/conts at this nesting level (haven't
2295 * already had a BranchTarget assigned) to point to the end
2298 for (j
= loop_stack
[loop_stack_pos
]; j
< i
; j
++) {
2299 if (mesa_instructions
[j
].Opcode
== OPCODE_BRK
||
2300 mesa_instructions
[j
].Opcode
== OPCODE_CONT
) {
2301 if (mesa_instructions
[j
].BranchTarget
== -1) {
2302 mesa_instructions
[j
].BranchTarget
= i
;
2306 /* The loop ends point at each other. */
2307 mesa_instructions
[i
].BranchTarget
= loop_stack
[loop_stack_pos
];
2308 mesa_instructions
[loop_stack
[loop_stack_pos
]].BranchTarget
= i
;
2311 foreach_iter(exec_list_iterator
, iter
, v
->function_signatures
) {
2312 function_entry
*entry
= (function_entry
*)iter
.get();
2314 if (entry
->sig_id
== mesa_instructions
[i
].BranchTarget
) {
2315 mesa_instructions
[i
].BranchTarget
= entry
->inst
;
2327 print_program(struct prog_instruction
*mesa_instructions
,
2328 ir_instruction
**mesa_instruction_annotation
,
2329 int num_instructions
)
2331 ir_instruction
*last_ir
= NULL
;
2335 for (i
= 0; i
< num_instructions
; i
++) {
2336 struct prog_instruction
*mesa_inst
= mesa_instructions
+ i
;
2337 ir_instruction
*ir
= mesa_instruction_annotation
[i
];
2339 fprintf(stdout
, "%3d: ", i
);
2341 if (last_ir
!= ir
&& ir
) {
2344 for (j
= 0; j
< indent
; j
++) {
2345 fprintf(stdout
, " ");
2351 fprintf(stdout
, " "); /* line number spacing. */
2354 indent
= _mesa_fprint_instruction_opt(stdout
, mesa_inst
, indent
,
2355 PROG_PRINT_DEBUG
, NULL
);
2361 * Count resources used by the given gpu program (number of texture
2365 count_resources(struct gl_program
*prog
)
2369 prog
->SamplersUsed
= 0;
2371 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
2372 struct prog_instruction
*inst
= &prog
->Instructions
[i
];
2374 if (_mesa_is_tex_instruction(inst
->Opcode
)) {
2375 prog
->SamplerTargets
[inst
->TexSrcUnit
] =
2376 (gl_texture_index
)inst
->TexSrcTarget
;
2377 prog
->SamplersUsed
|= 1 << inst
->TexSrcUnit
;
2378 if (inst
->TexShadow
) {
2379 prog
->ShadowSamplers
|= 1 << inst
->TexSrcUnit
;
2384 _mesa_update_shader_textures_used(prog
);
2389 * Check if the given vertex/fragment/shader program is within the
2390 * resource limits of the context (number of texture units, etc).
2391 * If any of those checks fail, record a linker error.
2393 * XXX more checks are needed...
2396 check_resources(const struct gl_context
*ctx
,
2397 struct gl_shader_program
*shader_program
,
2398 struct gl_program
*prog
)
2400 switch (prog
->Target
) {
2401 case GL_VERTEX_PROGRAM_ARB
:
2402 if (_mesa_bitcount(prog
->SamplersUsed
) >
2403 ctx
->Const
.MaxVertexTextureImageUnits
) {
2404 fail_link(shader_program
, "Too many vertex shader texture samplers");
2406 if (prog
->Parameters
->NumParameters
> MAX_UNIFORMS
) {
2407 fail_link(shader_program
, "Too many vertex shader constants");
2410 case MESA_GEOMETRY_PROGRAM
:
2411 if (_mesa_bitcount(prog
->SamplersUsed
) >
2412 ctx
->Const
.MaxGeometryTextureImageUnits
) {
2413 fail_link(shader_program
, "Too many geometry shader texture samplers");
2415 if (prog
->Parameters
->NumParameters
>
2416 MAX_GEOMETRY_UNIFORM_COMPONENTS
/ 4) {
2417 fail_link(shader_program
, "Too many geometry shader constants");
2420 case GL_FRAGMENT_PROGRAM_ARB
:
2421 if (_mesa_bitcount(prog
->SamplersUsed
) >
2422 ctx
->Const
.MaxTextureImageUnits
) {
2423 fail_link(shader_program
, "Too many fragment shader texture samplers");
2425 if (prog
->Parameters
->NumParameters
> MAX_UNIFORMS
) {
2426 fail_link(shader_program
, "Too many fragment shader constants");
2430 _mesa_problem(ctx
, "unexpected program type in check_resources()");
2436 struct uniform_sort
{
2437 struct gl_uniform
*u
;
2441 /* The shader_program->Uniforms list is almost sorted in increasing
2442 * uniform->{Frag,Vert}Pos locations, but not quite when there are
2443 * uniforms shared between targets. We need to add parameters in
2444 * increasing order for the targets.
2447 sort_uniforms(const void *a
, const void *b
)
2449 struct uniform_sort
*u1
= (struct uniform_sort
*)a
;
2450 struct uniform_sort
*u2
= (struct uniform_sort
*)b
;
2452 return u1
->pos
- u2
->pos
;
2455 /* Add the uniforms to the parameters. The linker chose locations
2456 * in our parameters lists (which weren't created yet), which the
2457 * uniforms code will use to poke values into our parameters list
2458 * when uniforms are updated.
2461 add_uniforms_to_parameters_list(struct gl_shader_program
*shader_program
,
2462 struct gl_shader
*shader
,
2463 struct gl_program
*prog
)
2466 unsigned int next_sampler
= 0, num_uniforms
= 0;
2467 struct uniform_sort
*sorted_uniforms
;
2469 sorted_uniforms
= ralloc_array(NULL
, struct uniform_sort
,
2470 shader_program
->Uniforms
->NumUniforms
);
2472 for (i
= 0; i
< shader_program
->Uniforms
->NumUniforms
; i
++) {
2473 struct gl_uniform
*uniform
= shader_program
->Uniforms
->Uniforms
+ i
;
2474 int parameter_index
= -1;
2476 switch (shader
->Type
) {
2477 case GL_VERTEX_SHADER
:
2478 parameter_index
= uniform
->VertPos
;
2480 case GL_FRAGMENT_SHADER
:
2481 parameter_index
= uniform
->FragPos
;
2483 case GL_GEOMETRY_SHADER
:
2484 parameter_index
= uniform
->GeomPos
;
2488 /* Only add uniforms used in our target. */
2489 if (parameter_index
!= -1) {
2490 sorted_uniforms
[num_uniforms
].pos
= parameter_index
;
2491 sorted_uniforms
[num_uniforms
].u
= uniform
;
2496 qsort(sorted_uniforms
, num_uniforms
, sizeof(struct uniform_sort
),
2499 for (i
= 0; i
< num_uniforms
; i
++) {
2500 struct gl_uniform
*uniform
= sorted_uniforms
[i
].u
;
2501 int parameter_index
= sorted_uniforms
[i
].pos
;
2502 const glsl_type
*type
= uniform
->Type
;
2505 if (type
->is_vector() ||
2506 type
->is_scalar()) {
2507 size
= type
->vector_elements
;
2509 size
= type_size(type
) * 4;
2512 gl_register_file file
;
2513 if (type
->is_sampler() ||
2514 (type
->is_array() && type
->fields
.array
->is_sampler())) {
2515 file
= PROGRAM_SAMPLER
;
2517 file
= PROGRAM_UNIFORM
;
2520 GLint index
= _mesa_lookup_parameter_index(prog
->Parameters
, -1,
2524 index
= _mesa_add_parameter(prog
->Parameters
, file
,
2525 uniform
->Name
, size
, type
->gl_type
,
2528 /* Sampler uniform values are stored in prog->SamplerUnits,
2529 * and the entry in that array is selected by this index we
2530 * store in ParameterValues[].
2532 if (file
== PROGRAM_SAMPLER
) {
2533 for (unsigned int j
= 0; j
< size
/ 4; j
++)
2534 prog
->Parameters
->ParameterValues
[index
+ j
][0] = next_sampler
++;
2537 /* The location chosen in the Parameters list here (returned
2538 * from _mesa_add_uniform) has to match what the linker chose.
2540 if (index
!= parameter_index
) {
2541 fail_link(shader_program
, "Allocation of uniform `%s' to target "
2542 "failed (%d vs %d)\n",
2543 uniform
->Name
, index
, parameter_index
);
2548 ralloc_free(sorted_uniforms
);
2552 set_uniform_initializer(struct gl_context
*ctx
, void *mem_ctx
,
2553 struct gl_shader_program
*shader_program
,
2554 const char *name
, const glsl_type
*type
,
2557 if (type
->is_record()) {
2558 ir_constant
*field_constant
;
2560 field_constant
= (ir_constant
*)val
->components
.get_head();
2562 for (unsigned int i
= 0; i
< type
->length
; i
++) {
2563 const glsl_type
*field_type
= type
->fields
.structure
[i
].type
;
2564 const char *field_name
= ralloc_asprintf(mem_ctx
, "%s.%s", name
,
2565 type
->fields
.structure
[i
].name
);
2566 set_uniform_initializer(ctx
, mem_ctx
, shader_program
, field_name
,
2567 field_type
, field_constant
);
2568 field_constant
= (ir_constant
*)field_constant
->next
;
2573 int loc
= _mesa_get_uniform_location(ctx
, shader_program
, name
);
2576 fail_link(shader_program
,
2577 "Couldn't find uniform for initializer %s\n", name
);
2581 for (unsigned int i
= 0; i
< (type
->is_array() ? type
->length
: 1); i
++) {
2582 ir_constant
*element
;
2583 const glsl_type
*element_type
;
2584 if (type
->is_array()) {
2585 element
= val
->array_elements
[i
];
2586 element_type
= type
->fields
.array
;
2589 element_type
= type
;
2594 if (element_type
->base_type
== GLSL_TYPE_BOOL
) {
2595 int *conv
= ralloc_array(mem_ctx
, int, element_type
->components());
2596 for (unsigned int j
= 0; j
< element_type
->components(); j
++) {
2597 conv
[j
] = element
->value
.b
[j
];
2599 values
= (void *)conv
;
2600 element_type
= glsl_type::get_instance(GLSL_TYPE_INT
,
2601 element_type
->vector_elements
,
2604 values
= &element
->value
;
2607 if (element_type
->is_matrix()) {
2608 _mesa_uniform_matrix(ctx
, shader_program
,
2609 element_type
->matrix_columns
,
2610 element_type
->vector_elements
,
2611 loc
, 1, GL_FALSE
, (GLfloat
*)values
);
2612 loc
+= element_type
->matrix_columns
;
2614 _mesa_uniform(ctx
, shader_program
, loc
, element_type
->matrix_columns
,
2615 values
, element_type
->gl_type
);
2616 loc
+= type_size(element_type
);
2622 set_uniform_initializers(struct gl_context
*ctx
,
2623 struct gl_shader_program
*shader_program
)
2625 void *mem_ctx
= NULL
;
2627 for (unsigned int i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
2628 struct gl_shader
*shader
= shader_program
->_LinkedShaders
[i
];
2633 foreach_iter(exec_list_iterator
, iter
, *shader
->ir
) {
2634 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
2635 ir_variable
*var
= ir
->as_variable();
2637 if (!var
|| var
->mode
!= ir_var_uniform
|| !var
->constant_value
)
2641 mem_ctx
= ralloc_context(NULL
);
2643 set_uniform_initializer(ctx
, mem_ctx
, shader_program
, var
->name
,
2644 var
->type
, var
->constant_value
);
2648 ralloc_free(mem_ctx
);
2652 * On a basic block basis, tracks available PROGRAM_TEMPORARY register
2653 * channels for copy propagation and updates following instructions to
2654 * use the original versions.
2656 * The ir_to_mesa_visitor lazily produces code assuming that this pass
2657 * will occur. As an example, a TXP production before this pass:
2659 * 0: MOV TEMP[1], INPUT[4].xyyy;
2660 * 1: MOV TEMP[1].w, INPUT[4].wwww;
2661 * 2: TXP TEMP[2], TEMP[1], texture[0], 2D;
2665 * 0: MOV TEMP[1], INPUT[4].xyyy;
2666 * 1: MOV TEMP[1].w, INPUT[4].wwww;
2667 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
2669 * which allows for dead code elimination on TEMP[1]'s writes.
2672 ir_to_mesa_visitor::copy_propagate(void)
2674 ir_to_mesa_instruction
**acp
= rzalloc_array(mem_ctx
,
2675 ir_to_mesa_instruction
*,
2676 this->next_temp
* 4);
2677 int *acp_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
2680 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2681 ir_to_mesa_instruction
*inst
= (ir_to_mesa_instruction
*)iter
.get();
2683 assert(inst
->dst
.file
!= PROGRAM_TEMPORARY
2684 || inst
->dst
.index
< this->next_temp
);
2686 /* First, do any copy propagation possible into the src regs. */
2687 for (int r
= 0; r
< 3; r
++) {
2688 ir_to_mesa_instruction
*first
= NULL
;
2690 int acp_base
= inst
->src
[r
].index
* 4;
2692 if (inst
->src
[r
].file
!= PROGRAM_TEMPORARY
||
2693 inst
->src
[r
].reladdr
)
2696 /* See if we can find entries in the ACP consisting of MOVs
2697 * from the same src register for all the swizzled channels
2698 * of this src register reference.
2700 for (int i
= 0; i
< 4; i
++) {
2701 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
2702 ir_to_mesa_instruction
*copy_chan
= acp
[acp_base
+ src_chan
];
2709 assert(acp_level
[acp_base
+ src_chan
] <= level
);
2714 if (first
->src
[0].file
!= copy_chan
->src
[0].file
||
2715 first
->src
[0].index
!= copy_chan
->src
[0].index
) {
2723 /* We've now validated that we can copy-propagate to
2724 * replace this src register reference. Do it.
2726 inst
->src
[r
].file
= first
->src
[0].file
;
2727 inst
->src
[r
].index
= first
->src
[0].index
;
2730 for (int i
= 0; i
< 4; i
++) {
2731 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
2732 ir_to_mesa_instruction
*copy_inst
= acp
[acp_base
+ src_chan
];
2733 swizzle
|= (GET_SWZ(copy_inst
->src
[0].swizzle
, src_chan
) <<
2736 inst
->src
[r
].swizzle
= swizzle
;
2741 case OPCODE_BGNLOOP
:
2742 case OPCODE_ENDLOOP
:
2743 /* End of a basic block, clear the ACP entirely. */
2744 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
2753 /* Clear all channels written inside the block from the ACP, but
2754 * leaving those that were not touched.
2756 for (int r
= 0; r
< this->next_temp
; r
++) {
2757 for (int c
= 0; c
< 4; c
++) {
2758 if (!acp
[4 * r
+ c
])
2761 if (acp_level
[4 * r
+ c
] >= level
)
2762 acp
[4 * r
+ c
] = NULL
;
2765 if (inst
->op
== OPCODE_ENDIF
)
2770 /* Continuing the block, clear any written channels from
2773 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.reladdr
) {
2774 /* Any temporary might be written, so no copy propagation
2775 * across this instruction.
2777 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
2778 } else if (inst
->dst
.file
== PROGRAM_OUTPUT
&&
2779 inst
->dst
.reladdr
) {
2780 /* Any output might be written, so no copy propagation
2781 * from outputs across this instruction.
2783 for (int r
= 0; r
< this->next_temp
; r
++) {
2784 for (int c
= 0; c
< 4; c
++) {
2785 if (!acp
[4 * r
+ c
])
2788 if (acp
[4 * r
+ c
]->src
[0].file
== PROGRAM_OUTPUT
)
2789 acp
[4 * r
+ c
] = NULL
;
2792 } else if (inst
->dst
.file
== PROGRAM_TEMPORARY
||
2793 inst
->dst
.file
== PROGRAM_OUTPUT
) {
2794 /* Clear where it's used as dst. */
2795 if (inst
->dst
.file
== PROGRAM_TEMPORARY
) {
2796 for (int c
= 0; c
< 4; c
++) {
2797 if (inst
->dst
.writemask
& (1 << c
)) {
2798 acp
[4 * inst
->dst
.index
+ c
] = NULL
;
2803 /* Clear where it's used as src. */
2804 for (int r
= 0; r
< this->next_temp
; r
++) {
2805 for (int c
= 0; c
< 4; c
++) {
2806 if (!acp
[4 * r
+ c
])
2809 int src_chan
= GET_SWZ(acp
[4 * r
+ c
]->src
[0].swizzle
, c
);
2811 if (acp
[4 * r
+ c
]->src
[0].file
== inst
->dst
.file
&&
2812 acp
[4 * r
+ c
]->src
[0].index
== inst
->dst
.index
&&
2813 inst
->dst
.writemask
& (1 << src_chan
))
2815 acp
[4 * r
+ c
] = NULL
;
2823 /* If this is a copy, add it to the ACP. */
2824 if (inst
->op
== OPCODE_MOV
&&
2825 inst
->dst
.file
== PROGRAM_TEMPORARY
&&
2826 !inst
->dst
.reladdr
&&
2828 !inst
->src
[0].reladdr
&&
2829 !inst
->src
[0].negate
) {
2830 for (int i
= 0; i
< 4; i
++) {
2831 if (inst
->dst
.writemask
& (1 << i
)) {
2832 acp
[4 * inst
->dst
.index
+ i
] = inst
;
2833 acp_level
[4 * inst
->dst
.index
+ i
] = level
;
2839 ralloc_free(acp_level
);
2845 * Convert a shader's GLSL IR into a Mesa gl_program.
2847 static struct gl_program
*
2848 get_mesa_program(struct gl_context
*ctx
,
2849 struct gl_shader_program
*shader_program
,
2850 struct gl_shader
*shader
)
2852 ir_to_mesa_visitor v
;
2853 struct prog_instruction
*mesa_instructions
, *mesa_inst
;
2854 ir_instruction
**mesa_instruction_annotation
;
2856 struct gl_program
*prog
;
2858 const char *target_string
;
2860 struct gl_shader_compiler_options
*options
=
2861 &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(shader
->Type
)];
2863 switch (shader
->Type
) {
2864 case GL_VERTEX_SHADER
:
2865 target
= GL_VERTEX_PROGRAM_ARB
;
2866 target_string
= "vertex";
2868 case GL_FRAGMENT_SHADER
:
2869 target
= GL_FRAGMENT_PROGRAM_ARB
;
2870 target_string
= "fragment";
2872 case GL_GEOMETRY_SHADER
:
2873 target
= GL_GEOMETRY_PROGRAM_NV
;
2874 target_string
= "geometry";
2877 assert(!"should not be reached");
2881 validate_ir_tree(shader
->ir
);
2883 prog
= ctx
->Driver
.NewProgram(ctx
, target
, shader_program
->Name
);
2886 prog
->Parameters
= _mesa_new_parameter_list();
2887 prog
->Varying
= _mesa_new_parameter_list();
2888 prog
->Attributes
= _mesa_new_parameter_list();
2891 v
.shader_program
= shader_program
;
2892 v
.options
= options
;
2894 add_uniforms_to_parameters_list(shader_program
, shader
, prog
);
2896 /* Emit Mesa IR for main(). */
2897 visit_exec_list(shader
->ir
, &v
);
2898 v
.emit(NULL
, OPCODE_END
);
2900 /* Now emit bodies for any functions that were used. */
2902 progress
= GL_FALSE
;
2904 foreach_iter(exec_list_iterator
, iter
, v
.function_signatures
) {
2905 function_entry
*entry
= (function_entry
*)iter
.get();
2907 if (!entry
->bgn_inst
) {
2908 v
.current_function
= entry
;
2910 entry
->bgn_inst
= v
.emit(NULL
, OPCODE_BGNSUB
);
2911 entry
->bgn_inst
->function
= entry
;
2913 visit_exec_list(&entry
->sig
->body
, &v
);
2915 ir_to_mesa_instruction
*last
;
2916 last
= (ir_to_mesa_instruction
*)v
.instructions
.get_tail();
2917 if (last
->op
!= OPCODE_RET
)
2918 v
.emit(NULL
, OPCODE_RET
);
2920 ir_to_mesa_instruction
*end
;
2921 end
= v
.emit(NULL
, OPCODE_ENDSUB
);
2922 end
->function
= entry
;
2929 prog
->NumTemporaries
= v
.next_temp
;
2931 int num_instructions
= 0;
2932 foreach_iter(exec_list_iterator
, iter
, v
.instructions
) {
2937 (struct prog_instruction
*)calloc(num_instructions
,
2938 sizeof(*mesa_instructions
));
2939 mesa_instruction_annotation
= ralloc_array(v
.mem_ctx
, ir_instruction
*,
2944 /* Convert ir_mesa_instructions into prog_instructions.
2946 mesa_inst
= mesa_instructions
;
2948 foreach_iter(exec_list_iterator
, iter
, v
.instructions
) {
2949 const ir_to_mesa_instruction
*inst
= (ir_to_mesa_instruction
*)iter
.get();
2951 mesa_inst
->Opcode
= inst
->op
;
2952 mesa_inst
->CondUpdate
= inst
->cond_update
;
2954 mesa_inst
->SaturateMode
= SATURATE_ZERO_ONE
;
2955 mesa_inst
->DstReg
.File
= inst
->dst
.file
;
2956 mesa_inst
->DstReg
.Index
= inst
->dst
.index
;
2957 mesa_inst
->DstReg
.CondMask
= inst
->dst
.cond_mask
;
2958 mesa_inst
->DstReg
.WriteMask
= inst
->dst
.writemask
;
2959 mesa_inst
->DstReg
.RelAddr
= inst
->dst
.reladdr
!= NULL
;
2960 mesa_inst
->SrcReg
[0] = mesa_src_reg_from_ir_src_reg(inst
->src
[0]);
2961 mesa_inst
->SrcReg
[1] = mesa_src_reg_from_ir_src_reg(inst
->src
[1]);
2962 mesa_inst
->SrcReg
[2] = mesa_src_reg_from_ir_src_reg(inst
->src
[2]);
2963 mesa_inst
->TexSrcUnit
= inst
->sampler
;
2964 mesa_inst
->TexSrcTarget
= inst
->tex_target
;
2965 mesa_inst
->TexShadow
= inst
->tex_shadow
;
2966 mesa_instruction_annotation
[i
] = inst
->ir
;
2968 /* Set IndirectRegisterFiles. */
2969 if (mesa_inst
->DstReg
.RelAddr
)
2970 prog
->IndirectRegisterFiles
|= 1 << mesa_inst
->DstReg
.File
;
2972 /* Update program's bitmask of indirectly accessed register files */
2973 for (unsigned src
= 0; src
< 3; src
++)
2974 if (mesa_inst
->SrcReg
[src
].RelAddr
)
2975 prog
->IndirectRegisterFiles
|= 1 << mesa_inst
->SrcReg
[src
].File
;
2977 if (options
->EmitNoIfs
&& mesa_inst
->Opcode
== OPCODE_IF
) {
2978 fail_link(shader_program
, "Couldn't flatten if statement\n");
2981 switch (mesa_inst
->Opcode
) {
2983 inst
->function
->inst
= i
;
2984 mesa_inst
->Comment
= strdup(inst
->function
->sig
->function_name());
2987 mesa_inst
->Comment
= strdup(inst
->function
->sig
->function_name());
2990 mesa_inst
->BranchTarget
= inst
->function
->sig_id
; /* rewritten later */
2993 prog
->NumAddressRegs
= 1;
3002 if (!shader_program
->LinkStatus
)
3006 if (!shader_program
->LinkStatus
) {
3007 free(mesa_instructions
);
3008 _mesa_reference_program(ctx
, &shader
->Program
, NULL
);
3012 set_branchtargets(&v
, mesa_instructions
, num_instructions
);
3014 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
3016 printf("GLSL IR for linked %s program %d:\n", target_string
,
3017 shader_program
->Name
);
3018 _mesa_print_ir(shader
->ir
, NULL
);
3021 printf("Mesa IR for linked %s program %d:\n", target_string
,
3022 shader_program
->Name
);
3023 print_program(mesa_instructions
, mesa_instruction_annotation
,
3027 prog
->Instructions
= mesa_instructions
;
3028 prog
->NumInstructions
= num_instructions
;
3030 do_set_program_inouts(shader
->ir
, prog
);
3031 count_resources(prog
);
3033 check_resources(ctx
, shader_program
, prog
);
3035 _mesa_reference_program(ctx
, &shader
->Program
, prog
);
3037 if ((ctx
->Shader
.Flags
& GLSL_NO_OPT
) == 0) {
3038 _mesa_optimize_program(ctx
, prog
);
3048 * Called via ctx->Driver.LinkShader()
3049 * This actually involves converting GLSL IR into Mesa gl_programs with
3050 * code lowering and other optimizations.
3053 _mesa_ir_link_shader(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
3055 assert(prog
->LinkStatus
);
3057 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
3058 if (prog
->_LinkedShaders
[i
] == NULL
)
3062 exec_list
*ir
= prog
->_LinkedShaders
[i
]->ir
;
3063 const struct gl_shader_compiler_options
*options
=
3064 &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(prog
->_LinkedShaders
[i
]->Type
)];
3070 do_mat_op_to_vec(ir
);
3071 lower_instructions(ir
, (MOD_TO_FRACT
| DIV_TO_MUL_RCP
| EXP_TO_EXP2
3073 | ((options
->EmitNoPow
) ? POW_TO_EXP2
: 0)));
3075 progress
= do_lower_jumps(ir
, true, true, options
->EmitNoMainReturn
, options
->EmitNoCont
, options
->EmitNoLoops
) || progress
;
3077 progress
= do_common_optimization(ir
, true, options
->MaxUnrollIterations
) || progress
;
3079 progress
= lower_quadop_vector(ir
, true) || progress
;
3081 if (options
->EmitNoIfs
) {
3082 progress
= lower_discard(ir
) || progress
;
3083 progress
= lower_if_to_cond_assign(ir
) || progress
;
3086 if (options
->EmitNoNoise
)
3087 progress
= lower_noise(ir
) || progress
;
3089 /* If there are forms of indirect addressing that the driver
3090 * cannot handle, perform the lowering pass.
3092 if (options
->EmitNoIndirectInput
|| options
->EmitNoIndirectOutput
3093 || options
->EmitNoIndirectTemp
|| options
->EmitNoIndirectUniform
)
3095 lower_variable_index_to_cond_assign(ir
,
3096 options
->EmitNoIndirectInput
,
3097 options
->EmitNoIndirectOutput
,
3098 options
->EmitNoIndirectTemp
,
3099 options
->EmitNoIndirectUniform
)
3102 progress
= do_vec_index_to_cond_assign(ir
) || progress
;
3105 validate_ir_tree(ir
);
3108 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
3109 struct gl_program
*linked_prog
;
3111 if (prog
->_LinkedShaders
[i
] == NULL
)
3114 linked_prog
= get_mesa_program(ctx
, prog
, prog
->_LinkedShaders
[i
]);
3119 switch (prog
->_LinkedShaders
[i
]->Type
) {
3120 case GL_VERTEX_SHADER
:
3121 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
,
3122 (struct gl_vertex_program
*)linked_prog
);
3123 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_VERTEX_PROGRAM_ARB
,
3126 case GL_FRAGMENT_SHADER
:
3127 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
,
3128 (struct gl_fragment_program
*)linked_prog
);
3129 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
3132 case GL_GEOMETRY_SHADER
:
3133 _mesa_reference_geomprog(ctx
, &prog
->GeometryProgram
,
3134 (struct gl_geometry_program
*)linked_prog
);
3135 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_GEOMETRY_PROGRAM_NV
,
3144 _mesa_reference_program(ctx
, &linked_prog
, NULL
);
3152 * Compile a GLSL shader. Called via glCompileShader().
3155 _mesa_glsl_compile_shader(struct gl_context
*ctx
, struct gl_shader
*shader
)
3157 struct _mesa_glsl_parse_state
*state
=
3158 new(shader
) _mesa_glsl_parse_state(ctx
, shader
->Type
, shader
);
3160 const char *source
= shader
->Source
;
3161 /* Check if the user called glCompileShader without first calling
3162 * glShaderSource. This should fail to compile, but not raise a GL_ERROR.
3164 if (source
== NULL
) {
3165 shader
->CompileStatus
= GL_FALSE
;
3169 state
->error
= preprocess(state
, &source
, &state
->info_log
,
3170 &ctx
->Extensions
, ctx
->API
);
3172 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
3173 printf("GLSL source for %s shader %d:\n",
3174 _mesa_glsl_shader_target_name(state
->target
), shader
->Name
);
3175 printf("%s\n", shader
->Source
);
3178 if (!state
->error
) {
3179 _mesa_glsl_lexer_ctor(state
, source
);
3180 _mesa_glsl_parse(state
);
3181 _mesa_glsl_lexer_dtor(state
);
3184 ralloc_free(shader
->ir
);
3185 shader
->ir
= new(shader
) exec_list
;
3186 if (!state
->error
&& !state
->translation_unit
.is_empty())
3187 _mesa_ast_to_hir(shader
->ir
, state
);
3189 if (!state
->error
&& !shader
->ir
->is_empty()) {
3190 validate_ir_tree(shader
->ir
);
3192 /* Do some optimization at compile time to reduce shader IR size
3193 * and reduce later work if the same shader is linked multiple times
3195 while (do_common_optimization(shader
->ir
, false, 32))
3198 validate_ir_tree(shader
->ir
);
3201 shader
->symbols
= state
->symbols
;
3203 shader
->CompileStatus
= !state
->error
;
3204 shader
->InfoLog
= state
->info_log
;
3205 shader
->Version
= state
->language_version
;
3206 memcpy(shader
->builtins_to_link
, state
->builtins_to_link
,
3207 sizeof(shader
->builtins_to_link
[0]) * state
->num_builtins_to_link
);
3208 shader
->num_builtins_to_link
= state
->num_builtins_to_link
;
3210 if (ctx
->Shader
.Flags
& GLSL_LOG
) {
3211 _mesa_write_shader_to_file(shader
);
3214 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
3215 if (shader
->CompileStatus
) {
3216 printf("GLSL IR for shader %d:\n", shader
->Name
);
3217 _mesa_print_ir(shader
->ir
, NULL
);
3220 printf("GLSL shader %d failed to compile.\n", shader
->Name
);
3222 if (shader
->InfoLog
&& shader
->InfoLog
[0] != 0) {
3223 printf("GLSL shader %d info log:\n", shader
->Name
);
3224 printf("%s\n", shader
->InfoLog
);
3228 /* Retain any live IR, but trash the rest. */
3229 reparent_ir(shader
->ir
, shader
->ir
);
3236 * Link a GLSL shader program. Called via glLinkProgram().
3239 _mesa_glsl_link_shader(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
3243 _mesa_clear_shader_program_data(ctx
, prog
);
3245 prog
->LinkStatus
= GL_TRUE
;
3247 for (i
= 0; i
< prog
->NumShaders
; i
++) {
3248 if (!prog
->Shaders
[i
]->CompileStatus
) {
3249 fail_link(prog
, "linking with uncompiled shader");
3250 prog
->LinkStatus
= GL_FALSE
;
3254 prog
->Varying
= _mesa_new_parameter_list();
3255 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
, NULL
);
3256 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
, NULL
);
3257 _mesa_reference_geomprog(ctx
, &prog
->GeometryProgram
, NULL
);
3259 if (prog
->LinkStatus
) {
3260 link_shaders(ctx
, prog
);
3263 if (prog
->LinkStatus
) {
3264 if (!ctx
->Driver
.LinkShader(ctx
, prog
)) {
3265 prog
->LinkStatus
= GL_FALSE
;
3269 set_uniform_initializers(ctx
, prog
);
3271 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
3272 if (!prog
->LinkStatus
) {
3273 printf("GLSL shader program %d failed to link\n", prog
->Name
);
3276 if (prog
->InfoLog
&& prog
->InfoLog
[0] != 0) {
3277 printf("GLSL shader program %d info log:\n", prog
->Name
);
3278 printf("%s\n", prog
->InfoLog
);