2 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
3 * Copyright (C) 2008 VMware, Inc. All Rights Reserved.
4 * Copyright © 2010 Intel Corporation
5 * Copyright © 2011 Bryan Cain
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
28 * \file glsl_to_tgsi.cpp
30 * Translate GLSL IR to TGSI.
34 #include "main/compiler.h"
36 #include "ir_visitor.h"
37 #include "ir_print_visitor.h"
38 #include "ir_expression_flattening.h"
39 #include "glsl_types.h"
40 #include "glsl_parser_extras.h"
41 #include "../glsl/program.h"
42 #include "ir_optimization.h"
46 #include "main/mtypes.h"
47 #include "main/shaderapi.h"
48 #include "main/shaderobj.h"
49 #include "main/uniforms.h"
50 #include "program/hash_table.h"
51 #include "program/prog_instruction.h"
52 #include "program/prog_optimize.h"
53 #include "program/prog_print.h"
54 #include "program/program.h"
55 #include "program/prog_uniform.h"
56 #include "program/prog_parameter.h"
57 #include "program/sampler.h"
59 #include "pipe/p_compiler.h"
60 #include "pipe/p_context.h"
61 #include "pipe/p_screen.h"
62 #include "pipe/p_shader_tokens.h"
63 #include "pipe/p_state.h"
64 #include "util/u_math.h"
65 #include "tgsi/tgsi_ureg.h"
66 #include "tgsi/tgsi_info.h"
67 #include "st_context.h"
68 #include "st_program.h"
69 #include "st_glsl_to_tgsi.h"
70 #include "st_mesa_to_tgsi.h"
73 #define PROGRAM_IMMEDIATE PROGRAM_FILE_MAX
74 #define PROGRAM_ANY_CONST ((1 << PROGRAM_LOCAL_PARAM) | \
75 (1 << PROGRAM_ENV_PARAM) | \
76 (1 << PROGRAM_STATE_VAR) | \
77 (1 << PROGRAM_NAMED_PARAM) | \
78 (1 << PROGRAM_CONSTANT) | \
79 (1 << PROGRAM_UNIFORM))
81 #define MAX_TEMPS 4096
86 static int swizzle_for_size(int size
);
89 * This struct is a corresponding struct to TGSI ureg_src.
93 st_src_reg(gl_register_file file
, int index
, const glsl_type
*type
)
97 if (type
&& (type
->is_scalar() || type
->is_vector() || type
->is_matrix()))
98 this->swizzle
= swizzle_for_size(type
->vector_elements
);
100 this->swizzle
= SWIZZLE_XYZW
;
102 this->type
= type
? type
->base_type
: GLSL_TYPE_ERROR
;
103 this->reladdr
= NULL
;
106 st_src_reg(gl_register_file file
, int index
, int type
)
111 this->swizzle
= SWIZZLE_XYZW
;
113 this->reladdr
= NULL
;
118 this->type
= GLSL_TYPE_ERROR
;
119 this->file
= PROGRAM_UNDEFINED
;
123 this->reladdr
= NULL
;
126 explicit st_src_reg(st_dst_reg reg
);
128 gl_register_file file
; /**< PROGRAM_* from Mesa */
129 int index
; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
130 GLuint swizzle
; /**< SWIZZLE_XYZWONEZERO swizzles from Mesa. */
131 int negate
; /**< NEGATE_XYZW mask from mesa */
132 int type
; /** GLSL_TYPE_* from GLSL IR (enum glsl_base_type) */
133 /** Register index should be offset by the integer in this reg. */
139 st_dst_reg(gl_register_file file
, int writemask
, int type
)
143 this->writemask
= writemask
;
144 this->cond_mask
= COND_TR
;
145 this->reladdr
= NULL
;
151 this->type
= GLSL_TYPE_ERROR
;
152 this->file
= PROGRAM_UNDEFINED
;
155 this->cond_mask
= COND_TR
;
156 this->reladdr
= NULL
;
159 explicit st_dst_reg(st_src_reg reg
);
161 gl_register_file file
; /**< PROGRAM_* from Mesa */
162 int index
; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
163 int writemask
; /**< Bitfield of WRITEMASK_[XYZW] */
165 int type
; /** GLSL_TYPE_* from GLSL IR (enum glsl_base_type) */
166 /** Register index should be offset by the integer in this reg. */
170 st_src_reg::st_src_reg(st_dst_reg reg
)
172 this->type
= reg
.type
;
173 this->file
= reg
.file
;
174 this->index
= reg
.index
;
175 this->swizzle
= SWIZZLE_XYZW
;
177 this->reladdr
= reg
.reladdr
;
180 st_dst_reg::st_dst_reg(st_src_reg reg
)
182 this->type
= reg
.type
;
183 this->file
= reg
.file
;
184 this->index
= reg
.index
;
185 this->writemask
= WRITEMASK_XYZW
;
186 this->cond_mask
= COND_TR
;
187 this->reladdr
= reg
.reladdr
;
190 class glsl_to_tgsi_instruction
: public exec_node
{
192 /* Callers of this ralloc-based new need not call delete. It's
193 * easier to just ralloc_free 'ctx' (or any of its ancestors). */
194 static void* operator new(size_t size
, void *ctx
)
198 node
= rzalloc_size(ctx
, size
);
199 assert(node
!= NULL
);
207 /** Pointer to the ir source this tree came from for debugging */
209 GLboolean cond_update
;
211 int sampler
; /**< sampler index */
212 int tex_target
; /**< One of TEXTURE_*_INDEX */
213 GLboolean tex_shadow
;
214 int dead_mask
; /**< Used in dead code elimination */
216 class function_entry
*function
; /* Set on TGSI_OPCODE_CAL or TGSI_OPCODE_BGNSUB */
219 class variable_storage
: public exec_node
{
221 variable_storage(ir_variable
*var
, gl_register_file file
, int index
)
222 : file(file
), index(index
), var(var
)
227 gl_register_file file
;
229 ir_variable
*var
; /* variable that maps to this, if any */
232 class immediate_storage
: public exec_node
{
234 immediate_storage(gl_constant_value
*values
, int size
, int type
)
236 memcpy(this->values
, values
, size
* sizeof(gl_constant_value
));
241 gl_constant_value values
[4];
242 int size
; /**< Number of components (1-4) */
243 int type
; /**< GL_FLOAT, GL_INT, GL_BOOL, or GL_UNSIGNED_INT */
246 class function_entry
: public exec_node
{
248 ir_function_signature
*sig
;
251 * identifier of this function signature used by the program.
253 * At the point that TGSI instructions for function calls are
254 * generated, we don't know the address of the first instruction of
255 * the function body. So we make the BranchTarget that is called a
256 * small integer and rewrite them during set_branchtargets().
261 * Pointer to first instruction of the function body.
263 * Set during function body emits after main() is processed.
265 glsl_to_tgsi_instruction
*bgn_inst
;
268 * Index of the first instruction of the function body in actual TGSI.
270 * Set after conversion from glsl_to_tgsi_instruction to TGSI.
274 /** Storage for the return value. */
275 st_src_reg return_reg
;
278 class glsl_to_tgsi_visitor
: public ir_visitor
{
280 glsl_to_tgsi_visitor();
281 ~glsl_to_tgsi_visitor();
283 function_entry
*current_function
;
285 struct gl_context
*ctx
;
286 struct gl_program
*prog
;
287 struct gl_shader_program
*shader_program
;
288 struct gl_shader_compiler_options
*options
;
292 int num_address_regs
;
294 bool indirect_addr_temps
;
295 bool indirect_addr_consts
;
299 variable_storage
*find_variable_storage(ir_variable
*var
);
301 int add_constant(gl_register_file file
, gl_constant_value values
[4],
302 int size
, int datatype
, GLuint
*swizzle_out
);
304 function_entry
*get_function_signature(ir_function_signature
*sig
);
306 st_src_reg
get_temp(const glsl_type
*type
);
307 void reladdr_to_temp(ir_instruction
*ir
, st_src_reg
*reg
, int *num_reladdr
);
309 st_src_reg
st_src_reg_for_float(float val
);
310 st_src_reg
st_src_reg_for_int(int val
);
311 st_src_reg
st_src_reg_for_type(int type
, int val
);
314 * \name Visit methods
316 * As typical for the visitor pattern, there must be one \c visit method for
317 * each concrete subclass of \c ir_instruction. Virtual base classes within
318 * the hierarchy should not have \c visit methods.
321 virtual void visit(ir_variable
*);
322 virtual void visit(ir_loop
*);
323 virtual void visit(ir_loop_jump
*);
324 virtual void visit(ir_function_signature
*);
325 virtual void visit(ir_function
*);
326 virtual void visit(ir_expression
*);
327 virtual void visit(ir_swizzle
*);
328 virtual void visit(ir_dereference_variable
*);
329 virtual void visit(ir_dereference_array
*);
330 virtual void visit(ir_dereference_record
*);
331 virtual void visit(ir_assignment
*);
332 virtual void visit(ir_constant
*);
333 virtual void visit(ir_call
*);
334 virtual void visit(ir_return
*);
335 virtual void visit(ir_discard
*);
336 virtual void visit(ir_texture
*);
337 virtual void visit(ir_if
*);
342 /** List of variable_storage */
345 /** List of immediate_storage */
346 exec_list immediates
;
349 /** List of function_entry */
350 exec_list function_signatures
;
351 int next_signature_id
;
353 /** List of glsl_to_tgsi_instruction */
354 exec_list instructions
;
356 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
);
358 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
,
359 st_dst_reg dst
, st_src_reg src0
);
361 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
,
362 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
);
364 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
,
366 st_src_reg src0
, st_src_reg src1
, st_src_reg src2
);
368 unsigned get_opcode(ir_instruction
*ir
, unsigned op
,
370 st_src_reg src0
, st_src_reg src1
);
373 * Emit the correct dot-product instruction for the type of arguments
375 void emit_dp(ir_instruction
*ir
,
381 void emit_scalar(ir_instruction
*ir
, unsigned op
,
382 st_dst_reg dst
, st_src_reg src0
);
384 void emit_scalar(ir_instruction
*ir
, unsigned op
,
385 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
);
387 void emit_arl(ir_instruction
*ir
, st_dst_reg dst
, st_src_reg src0
);
389 void emit_scs(ir_instruction
*ir
, unsigned op
,
390 st_dst_reg dst
, const st_src_reg
&src
);
392 GLboolean
try_emit_mad(ir_expression
*ir
,
394 GLboolean
try_emit_sat(ir_expression
*ir
);
396 void emit_swz(ir_expression
*ir
);
398 bool process_move_condition(ir_rvalue
*ir
);
400 void remove_output_reads(gl_register_file type
);
401 void simplify_cmp(void);
403 void rename_temp_register(int index
, int new_index
);
404 int get_first_temp_read(int index
);
405 int get_first_temp_write(int index
);
406 int get_last_temp_read(int index
);
407 int get_last_temp_write(int index
);
409 void copy_propagate(void);
410 void eliminate_dead_code(void);
411 int eliminate_dead_code_advanced(void);
412 void merge_registers(void);
413 void renumber_registers(void);
418 static st_src_reg undef_src
= st_src_reg(PROGRAM_UNDEFINED
, 0, GLSL_TYPE_ERROR
);
420 static st_dst_reg undef_dst
= st_dst_reg(PROGRAM_UNDEFINED
, SWIZZLE_NOOP
, GLSL_TYPE_ERROR
);
422 static st_dst_reg address_reg
= st_dst_reg(PROGRAM_ADDRESS
, WRITEMASK_X
, GLSL_TYPE_FLOAT
);
425 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...) PRINTFLIKE(2, 3);
428 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...)
432 ralloc_vasprintf_append(&prog
->InfoLog
, fmt
, args
);
435 prog
->LinkStatus
= GL_FALSE
;
439 swizzle_for_size(int size
)
441 int size_swizzles
[4] = {
442 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
),
443 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
),
444 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
),
445 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
448 assert((size
>= 1) && (size
<= 4));
449 return size_swizzles
[size
- 1];
453 is_tex_instruction(unsigned opcode
)
455 const tgsi_opcode_info
* info
= tgsi_get_opcode_info(opcode
);
460 num_inst_dst_regs(unsigned opcode
)
462 const tgsi_opcode_info
* info
= tgsi_get_opcode_info(opcode
);
463 return info
->num_dst
;
467 num_inst_src_regs(unsigned opcode
)
469 const tgsi_opcode_info
* info
= tgsi_get_opcode_info(opcode
);
470 return info
->is_tex
? info
->num_src
- 1 : info
->num_src
;
473 glsl_to_tgsi_instruction
*
474 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
,
476 st_src_reg src0
, st_src_reg src1
, st_src_reg src2
)
478 glsl_to_tgsi_instruction
*inst
= new(mem_ctx
) glsl_to_tgsi_instruction();
479 int num_reladdr
= 0, i
;
481 op
= get_opcode(ir
, op
, dst
, src0
, src1
);
483 /* If we have to do relative addressing, we want to load the ARL
484 * reg directly for one of the regs, and preload the other reladdr
485 * sources into temps.
487 num_reladdr
+= dst
.reladdr
!= NULL
;
488 num_reladdr
+= src0
.reladdr
!= NULL
;
489 num_reladdr
+= src1
.reladdr
!= NULL
;
490 num_reladdr
+= src2
.reladdr
!= NULL
;
492 reladdr_to_temp(ir
, &src2
, &num_reladdr
);
493 reladdr_to_temp(ir
, &src1
, &num_reladdr
);
494 reladdr_to_temp(ir
, &src0
, &num_reladdr
);
497 emit_arl(ir
, address_reg
, *dst
.reladdr
);
500 assert(num_reladdr
== 0);
510 inst
->function
= NULL
;
512 if (op
== TGSI_OPCODE_ARL
)
513 this->num_address_regs
= 1;
515 /* Update indirect addressing status used by TGSI */
518 case PROGRAM_TEMPORARY
:
519 this->indirect_addr_temps
= true;
521 case PROGRAM_LOCAL_PARAM
:
522 case PROGRAM_ENV_PARAM
:
523 case PROGRAM_STATE_VAR
:
524 case PROGRAM_NAMED_PARAM
:
525 case PROGRAM_CONSTANT
:
526 case PROGRAM_UNIFORM
:
527 this->indirect_addr_consts
= true;
529 case PROGRAM_IMMEDIATE
:
530 assert(!"immediates should not have indirect addressing");
537 for (i
=0; i
<3; i
++) {
538 if(inst
->src
[i
].reladdr
) {
539 switch(inst
->src
[i
].file
) {
540 case PROGRAM_TEMPORARY
:
541 this->indirect_addr_temps
= true;
543 case PROGRAM_LOCAL_PARAM
:
544 case PROGRAM_ENV_PARAM
:
545 case PROGRAM_STATE_VAR
:
546 case PROGRAM_NAMED_PARAM
:
547 case PROGRAM_CONSTANT
:
548 case PROGRAM_UNIFORM
:
549 this->indirect_addr_consts
= true;
551 case PROGRAM_IMMEDIATE
:
552 assert(!"immediates should not have indirect addressing");
561 this->instructions
.push_tail(inst
);
567 glsl_to_tgsi_instruction
*
568 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
,
569 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
)
571 return emit(ir
, op
, dst
, src0
, src1
, undef_src
);
574 glsl_to_tgsi_instruction
*
575 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
,
576 st_dst_reg dst
, st_src_reg src0
)
578 assert(dst
.writemask
!= 0);
579 return emit(ir
, op
, dst
, src0
, undef_src
, undef_src
);
582 glsl_to_tgsi_instruction
*
583 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
)
585 return emit(ir
, op
, undef_dst
, undef_src
, undef_src
, undef_src
);
589 * Determines whether to use an integer, unsigned integer, or float opcode
590 * based on the operands and input opcode, then emits the result.
592 * TODO: type checking for remaining TGSI opcodes
595 glsl_to_tgsi_visitor::get_opcode(ir_instruction
*ir
, unsigned op
,
597 st_src_reg src0
, st_src_reg src1
)
599 int type
= GLSL_TYPE_FLOAT
;
601 if (src0
.type
== GLSL_TYPE_FLOAT
|| src1
.type
== GLSL_TYPE_FLOAT
)
602 type
= GLSL_TYPE_FLOAT
;
603 else if (glsl_version
>= 130)
606 #define case4(c, f, i, u) \
607 case TGSI_OPCODE_##c: \
608 if (type == GLSL_TYPE_INT) op = TGSI_OPCODE_##i; \
609 else if (type == GLSL_TYPE_UINT) op = TGSI_OPCODE_##u; \
610 else op = TGSI_OPCODE_##f; \
612 #define case3(f, i, u) case4(f, f, i, u)
613 #define case2fi(f, i) case4(f, f, i, i)
614 #define case2iu(i, u) case4(i, LAST, i, u)
620 case3(DIV
, IDIV
, UDIV
);
621 case3(MAX
, IMAX
, UMAX
);
622 case3(MIN
, IMIN
, UMIN
);
627 case3(SGE
, ISGE
, USGE
);
628 case3(SLT
, ISLT
, USLT
);
640 assert(op
!= TGSI_OPCODE_LAST
);
645 glsl_to_tgsi_visitor::emit_dp(ir_instruction
*ir
,
646 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
,
649 static const unsigned dot_opcodes
[] = {
650 TGSI_OPCODE_DP2
, TGSI_OPCODE_DP3
, TGSI_OPCODE_DP4
653 emit(ir
, dot_opcodes
[elements
- 2], dst
, src0
, src1
);
657 * Emits TGSI scalar opcodes to produce unique answers across channels.
659 * Some TGSI opcodes are scalar-only, like ARB_fp/vp. The src X
660 * channel determines the result across all channels. So to do a vec4
661 * of this operation, we want to emit a scalar per source channel used
662 * to produce dest channels.
665 glsl_to_tgsi_visitor::emit_scalar(ir_instruction
*ir
, unsigned op
,
667 st_src_reg orig_src0
, st_src_reg orig_src1
)
670 int done_mask
= ~dst
.writemask
;
672 /* TGSI RCP is a scalar operation splatting results to all channels,
673 * like ARB_fp/vp. So emit as many RCPs as necessary to cover our
676 for (i
= 0; i
< 4; i
++) {
677 GLuint this_mask
= (1 << i
);
678 glsl_to_tgsi_instruction
*inst
;
679 st_src_reg src0
= orig_src0
;
680 st_src_reg src1
= orig_src1
;
682 if (done_mask
& this_mask
)
685 GLuint src0_swiz
= GET_SWZ(src0
.swizzle
, i
);
686 GLuint src1_swiz
= GET_SWZ(src1
.swizzle
, i
);
687 for (j
= i
+ 1; j
< 4; j
++) {
688 /* If there is another enabled component in the destination that is
689 * derived from the same inputs, generate its value on this pass as
692 if (!(done_mask
& (1 << j
)) &&
693 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
&&
694 GET_SWZ(src1
.swizzle
, j
) == src1_swiz
) {
695 this_mask
|= (1 << j
);
698 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
699 src0_swiz
, src0_swiz
);
700 src1
.swizzle
= MAKE_SWIZZLE4(src1_swiz
, src1_swiz
,
701 src1_swiz
, src1_swiz
);
703 inst
= emit(ir
, op
, dst
, src0
, src1
);
704 inst
->dst
.writemask
= this_mask
;
705 done_mask
|= this_mask
;
710 glsl_to_tgsi_visitor::emit_scalar(ir_instruction
*ir
, unsigned op
,
711 st_dst_reg dst
, st_src_reg src0
)
713 st_src_reg undef
= undef_src
;
715 undef
.swizzle
= SWIZZLE_XXXX
;
717 emit_scalar(ir
, op
, dst
, src0
, undef
);
721 glsl_to_tgsi_visitor::emit_arl(ir_instruction
*ir
,
722 st_dst_reg dst
, st_src_reg src0
)
724 st_src_reg tmp
= get_temp(glsl_type::float_type
);
726 if (src0
.type
== GLSL_TYPE_INT
)
727 emit(NULL
, TGSI_OPCODE_I2F
, st_dst_reg(tmp
), src0
);
728 else if (src0
.type
== GLSL_TYPE_UINT
)
729 emit(NULL
, TGSI_OPCODE_U2F
, st_dst_reg(tmp
), src0
);
733 emit(NULL
, TGSI_OPCODE_ARL
, dst
, tmp
);
737 * Emit an TGSI_OPCODE_SCS instruction
739 * The \c SCS opcode functions a bit differently than the other TGSI opcodes.
740 * Instead of splatting its result across all four components of the
741 * destination, it writes one value to the \c x component and another value to
742 * the \c y component.
744 * \param ir IR instruction being processed
745 * \param op Either \c TGSI_OPCODE_SIN or \c TGSI_OPCODE_COS depending
746 * on which value is desired.
747 * \param dst Destination register
748 * \param src Source register
751 glsl_to_tgsi_visitor::emit_scs(ir_instruction
*ir
, unsigned op
,
753 const st_src_reg
&src
)
755 /* Vertex programs cannot use the SCS opcode.
757 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
758 emit_scalar(ir
, op
, dst
, src
);
762 const unsigned component
= (op
== TGSI_OPCODE_SIN
) ? 0 : 1;
763 const unsigned scs_mask
= (1U << component
);
764 int done_mask
= ~dst
.writemask
;
767 assert(op
== TGSI_OPCODE_SIN
|| op
== TGSI_OPCODE_COS
);
769 /* If there are compnents in the destination that differ from the component
770 * that will be written by the SCS instrution, we'll need a temporary.
772 if (scs_mask
!= unsigned(dst
.writemask
)) {
773 tmp
= get_temp(glsl_type::vec4_type
);
776 for (unsigned i
= 0; i
< 4; i
++) {
777 unsigned this_mask
= (1U << i
);
778 st_src_reg src0
= src
;
780 if ((done_mask
& this_mask
) != 0)
783 /* The source swizzle specified which component of the source generates
784 * sine / cosine for the current component in the destination. The SCS
785 * instruction requires that this value be swizzle to the X component.
786 * Replace the current swizzle with a swizzle that puts the source in
789 unsigned src0_swiz
= GET_SWZ(src
.swizzle
, i
);
791 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
792 src0_swiz
, src0_swiz
);
793 for (unsigned j
= i
+ 1; j
< 4; j
++) {
794 /* If there is another enabled component in the destination that is
795 * derived from the same inputs, generate its value on this pass as
798 if (!(done_mask
& (1 << j
)) &&
799 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
) {
800 this_mask
|= (1 << j
);
804 if (this_mask
!= scs_mask
) {
805 glsl_to_tgsi_instruction
*inst
;
806 st_dst_reg tmp_dst
= st_dst_reg(tmp
);
808 /* Emit the SCS instruction.
810 inst
= emit(ir
, TGSI_OPCODE_SCS
, tmp_dst
, src0
);
811 inst
->dst
.writemask
= scs_mask
;
813 /* Move the result of the SCS instruction to the desired location in
816 tmp
.swizzle
= MAKE_SWIZZLE4(component
, component
,
817 component
, component
);
818 inst
= emit(ir
, TGSI_OPCODE_SCS
, dst
, tmp
);
819 inst
->dst
.writemask
= this_mask
;
821 /* Emit the SCS instruction to write directly to the destination.
823 glsl_to_tgsi_instruction
*inst
= emit(ir
, TGSI_OPCODE_SCS
, dst
, src0
);
824 inst
->dst
.writemask
= scs_mask
;
827 done_mask
|= this_mask
;
832 glsl_to_tgsi_visitor::add_constant(gl_register_file file
,
833 gl_constant_value values
[4], int size
, int datatype
,
836 if (file
== PROGRAM_CONSTANT
) {
837 return _mesa_add_typed_unnamed_constant(this->prog
->Parameters
, values
,
838 size
, datatype
, swizzle_out
);
841 immediate_storage
*entry
;
842 assert(file
== PROGRAM_IMMEDIATE
);
844 /* Search immediate storage to see if we already have an identical
845 * immediate that we can use instead of adding a duplicate entry.
847 foreach_iter(exec_list_iterator
, iter
, this->immediates
) {
848 entry
= (immediate_storage
*)iter
.get();
850 if (entry
->size
== size
&&
851 entry
->type
== datatype
&&
852 !memcmp(entry
->values
, values
, size
* sizeof(gl_constant_value
))) {
858 /* Add this immediate to the list. */
859 entry
= new(mem_ctx
) immediate_storage(values
, size
, datatype
);
860 this->immediates
.push_tail(entry
);
861 this->num_immediates
++;
867 glsl_to_tgsi_visitor::st_src_reg_for_float(float val
)
869 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_FLOAT
);
870 union gl_constant_value uval
;
873 src
.index
= add_constant(src
.file
, &uval
, 1, GL_FLOAT
, &src
.swizzle
);
879 glsl_to_tgsi_visitor::st_src_reg_for_int(int val
)
881 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_INT
);
882 union gl_constant_value uval
;
884 assert(glsl_version
>= 130);
887 src
.index
= add_constant(src
.file
, &uval
, 1, GL_INT
, &src
.swizzle
);
893 glsl_to_tgsi_visitor::st_src_reg_for_type(int type
, int val
)
895 if (glsl_version
>= 130)
896 return type
== GLSL_TYPE_FLOAT
? st_src_reg_for_float(val
) :
897 st_src_reg_for_int(val
);
899 return st_src_reg_for_float(val
);
903 type_size(const struct glsl_type
*type
)
908 switch (type
->base_type
) {
911 case GLSL_TYPE_FLOAT
:
913 if (type
->is_matrix()) {
914 return type
->matrix_columns
;
916 /* Regardless of size of vector, it gets a vec4. This is bad
917 * packing for things like floats, but otherwise arrays become a
918 * mess. Hopefully a later pass over the code can pack scalars
919 * down if appropriate.
923 case GLSL_TYPE_ARRAY
:
924 assert(type
->length
> 0);
925 return type_size(type
->fields
.array
) * type
->length
;
926 case GLSL_TYPE_STRUCT
:
928 for (i
= 0; i
< type
->length
; i
++) {
929 size
+= type_size(type
->fields
.structure
[i
].type
);
932 case GLSL_TYPE_SAMPLER
:
933 /* Samplers take up one slot in UNIFORMS[], but they're baked in
944 * In the initial pass of codegen, we assign temporary numbers to
945 * intermediate results. (not SSA -- variable assignments will reuse
949 glsl_to_tgsi_visitor::get_temp(const glsl_type
*type
)
955 src
.type
= glsl_version
>= 130 ? type
->base_type
: GLSL_TYPE_FLOAT
;
956 src
.file
= PROGRAM_TEMPORARY
;
957 src
.index
= next_temp
;
959 next_temp
+= type_size(type
);
961 if (type
->is_array() || type
->is_record()) {
962 src
.swizzle
= SWIZZLE_NOOP
;
964 for (i
= 0; i
< type
->vector_elements
; i
++)
967 swizzle
[i
] = type
->vector_elements
- 1;
968 src
.swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1],
969 swizzle
[2], swizzle
[3]);
977 glsl_to_tgsi_visitor::find_variable_storage(ir_variable
*var
)
980 variable_storage
*entry
;
982 foreach_iter(exec_list_iterator
, iter
, this->variables
) {
983 entry
= (variable_storage
*)iter
.get();
985 if (entry
->var
== var
)
993 glsl_to_tgsi_visitor::visit(ir_variable
*ir
)
995 if (strcmp(ir
->name
, "gl_FragCoord") == 0) {
996 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
998 fp
->OriginUpperLeft
= ir
->origin_upper_left
;
999 fp
->PixelCenterInteger
= ir
->pixel_center_integer
;
1001 } else if (strcmp(ir
->name
, "gl_FragDepth") == 0) {
1002 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
1003 switch (ir
->depth_layout
) {
1004 case ir_depth_layout_none
:
1005 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_NONE
;
1007 case ir_depth_layout_any
:
1008 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_ANY
;
1010 case ir_depth_layout_greater
:
1011 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_GREATER
;
1013 case ir_depth_layout_less
:
1014 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_LESS
;
1016 case ir_depth_layout_unchanged
:
1017 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
1025 if (ir
->mode
== ir_var_uniform
&& strncmp(ir
->name
, "gl_", 3) == 0) {
1027 const ir_state_slot
*const slots
= ir
->state_slots
;
1028 assert(ir
->state_slots
!= NULL
);
1030 /* Check if this statevar's setup in the STATE file exactly
1031 * matches how we'll want to reference it as a
1032 * struct/array/whatever. If not, then we need to move it into
1033 * temporary storage and hope that it'll get copy-propagated
1036 for (i
= 0; i
< ir
->num_state_slots
; i
++) {
1037 if (slots
[i
].swizzle
!= SWIZZLE_XYZW
) {
1042 struct variable_storage
*storage
;
1044 if (i
== ir
->num_state_slots
) {
1045 /* We'll set the index later. */
1046 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_STATE_VAR
, -1);
1047 this->variables
.push_tail(storage
);
1051 /* The variable_storage constructor allocates slots based on the size
1052 * of the type. However, this had better match the number of state
1053 * elements that we're going to copy into the new temporary.
1055 assert((int) ir
->num_state_slots
== type_size(ir
->type
));
1057 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_TEMPORARY
,
1059 this->variables
.push_tail(storage
);
1060 this->next_temp
+= type_size(ir
->type
);
1062 dst
= st_dst_reg(st_src_reg(PROGRAM_TEMPORARY
, storage
->index
,
1063 glsl_version
>= 130 ? ir
->type
->base_type
: GLSL_TYPE_FLOAT
));
1067 for (unsigned int i
= 0; i
< ir
->num_state_slots
; i
++) {
1068 int index
= _mesa_add_state_reference(this->prog
->Parameters
,
1069 (gl_state_index
*)slots
[i
].tokens
);
1071 if (storage
->file
== PROGRAM_STATE_VAR
) {
1072 if (storage
->index
== -1) {
1073 storage
->index
= index
;
1075 assert(index
== storage
->index
+ (int)i
);
1078 st_src_reg
src(PROGRAM_STATE_VAR
, index
,
1079 glsl_version
>= 130 ? ir
->type
->base_type
: GLSL_TYPE_FLOAT
);
1080 src
.swizzle
= slots
[i
].swizzle
;
1081 emit(ir
, TGSI_OPCODE_MOV
, dst
, src
);
1082 /* even a float takes up a whole vec4 reg in a struct/array. */
1087 if (storage
->file
== PROGRAM_TEMPORARY
&&
1088 dst
.index
!= storage
->index
+ (int) ir
->num_state_slots
) {
1089 fail_link(this->shader_program
,
1090 "failed to load builtin uniform `%s' (%d/%d regs loaded)\n",
1091 ir
->name
, dst
.index
- storage
->index
,
1092 type_size(ir
->type
));
1098 glsl_to_tgsi_visitor::visit(ir_loop
*ir
)
1100 ir_dereference_variable
*counter
= NULL
;
1102 if (ir
->counter
!= NULL
)
1103 counter
= new(ir
) ir_dereference_variable(ir
->counter
);
1105 if (ir
->from
!= NULL
) {
1106 assert(ir
->counter
!= NULL
);
1108 ir_assignment
*a
= new(ir
) ir_assignment(counter
, ir
->from
, NULL
);
1114 emit(NULL
, TGSI_OPCODE_BGNLOOP
);
1118 new(ir
) ir_expression(ir
->cmp
, glsl_type::bool_type
,
1120 ir_if
*if_stmt
= new(ir
) ir_if(e
);
1122 ir_loop_jump
*brk
= new(ir
) ir_loop_jump(ir_loop_jump::jump_break
);
1124 if_stmt
->then_instructions
.push_tail(brk
);
1126 if_stmt
->accept(this);
1133 visit_exec_list(&ir
->body_instructions
, this);
1135 if (ir
->increment
) {
1137 new(ir
) ir_expression(ir_binop_add
, counter
->type
,
1138 counter
, ir
->increment
);
1140 ir_assignment
*a
= new(ir
) ir_assignment(counter
, e
, NULL
);
1147 emit(NULL
, TGSI_OPCODE_ENDLOOP
);
1151 glsl_to_tgsi_visitor::visit(ir_loop_jump
*ir
)
1154 case ir_loop_jump::jump_break
:
1155 emit(NULL
, TGSI_OPCODE_BRK
);
1157 case ir_loop_jump::jump_continue
:
1158 emit(NULL
, TGSI_OPCODE_CONT
);
1165 glsl_to_tgsi_visitor::visit(ir_function_signature
*ir
)
1172 glsl_to_tgsi_visitor::visit(ir_function
*ir
)
1174 /* Ignore function bodies other than main() -- we shouldn't see calls to
1175 * them since they should all be inlined before we get to glsl_to_tgsi.
1177 if (strcmp(ir
->name
, "main") == 0) {
1178 const ir_function_signature
*sig
;
1181 sig
= ir
->matching_signature(&empty
);
1185 foreach_iter(exec_list_iterator
, iter
, sig
->body
) {
1186 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
1194 glsl_to_tgsi_visitor::try_emit_mad(ir_expression
*ir
, int mul_operand
)
1196 int nonmul_operand
= 1 - mul_operand
;
1198 st_dst_reg result_dst
;
1200 ir_expression
*expr
= ir
->operands
[mul_operand
]->as_expression();
1201 if (!expr
|| expr
->operation
!= ir_binop_mul
)
1204 expr
->operands
[0]->accept(this);
1206 expr
->operands
[1]->accept(this);
1208 ir
->operands
[nonmul_operand
]->accept(this);
1211 this->result
= get_temp(ir
->type
);
1212 result_dst
= st_dst_reg(this->result
);
1213 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1214 emit(ir
, TGSI_OPCODE_MAD
, result_dst
, a
, b
, c
);
1220 glsl_to_tgsi_visitor::try_emit_sat(ir_expression
*ir
)
1222 /* Saturates were only introduced to vertex programs in
1223 * NV_vertex_program3, so don't give them to drivers in the VP.
1225 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
)
1228 ir_rvalue
*sat_src
= ir
->as_rvalue_to_saturate();
1232 sat_src
->accept(this);
1233 st_src_reg src
= this->result
;
1235 this->result
= get_temp(ir
->type
);
1236 st_dst_reg result_dst
= st_dst_reg(this->result
);
1237 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1238 glsl_to_tgsi_instruction
*inst
;
1239 inst
= emit(ir
, TGSI_OPCODE_MOV
, result_dst
, src
);
1240 inst
->saturate
= true;
1246 glsl_to_tgsi_visitor::reladdr_to_temp(ir_instruction
*ir
,
1247 st_src_reg
*reg
, int *num_reladdr
)
1252 emit_arl(ir
, address_reg
, *reg
->reladdr
);
1254 if (*num_reladdr
!= 1) {
1255 st_src_reg temp
= get_temp(glsl_type::vec4_type
);
1257 emit(ir
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), *reg
);
1265 glsl_to_tgsi_visitor::visit(ir_expression
*ir
)
1267 unsigned int operand
;
1268 st_src_reg op
[Elements(ir
->operands
)];
1269 st_src_reg result_src
;
1270 st_dst_reg result_dst
;
1272 /* Quick peephole: Emit MAD(a, b, c) instead of ADD(MUL(a, b), c)
1274 if (ir
->operation
== ir_binop_add
) {
1275 if (try_emit_mad(ir
, 1))
1277 if (try_emit_mad(ir
, 0))
1280 if (try_emit_sat(ir
))
1283 if (ir
->operation
== ir_quadop_vector
)
1284 assert(!"ir_quadop_vector should have been lowered");
1286 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
1287 this->result
.file
= PROGRAM_UNDEFINED
;
1288 ir
->operands
[operand
]->accept(this);
1289 if (this->result
.file
== PROGRAM_UNDEFINED
) {
1291 printf("Failed to get tree for expression operand:\n");
1292 ir
->operands
[operand
]->accept(&v
);
1295 op
[operand
] = this->result
;
1297 /* Matrix expression operands should have been broken down to vector
1298 * operations already.
1300 assert(!ir
->operands
[operand
]->type
->is_matrix());
1303 int vector_elements
= ir
->operands
[0]->type
->vector_elements
;
1304 if (ir
->operands
[1]) {
1305 vector_elements
= MAX2(vector_elements
,
1306 ir
->operands
[1]->type
->vector_elements
);
1309 this->result
.file
= PROGRAM_UNDEFINED
;
1311 /* Storage for our result. Ideally for an assignment we'd be using
1312 * the actual storage for the result here, instead.
1314 result_src
= get_temp(ir
->type
);
1315 /* convenience for the emit functions below. */
1316 result_dst
= st_dst_reg(result_src
);
1317 /* Limit writes to the channels that will be used by result_src later.
1318 * This does limit this temp's use as a temporary for multi-instruction
1321 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1323 switch (ir
->operation
) {
1324 case ir_unop_logic_not
:
1325 emit(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], st_src_reg_for_type(result_dst
.type
, 0));
1328 assert(result_dst
.type
== GLSL_TYPE_FLOAT
|| result_dst
.type
== GLSL_TYPE_INT
);
1329 if (result_dst
.type
== GLSL_TYPE_INT
)
1330 emit(ir
, TGSI_OPCODE_INEG
, result_dst
, op
[0]);
1332 op
[0].negate
= ~op
[0].negate
;
1337 assert(result_dst
.type
== GLSL_TYPE_FLOAT
);
1338 emit(ir
, TGSI_OPCODE_ABS
, result_dst
, op
[0]);
1341 emit(ir
, TGSI_OPCODE_SSG
, result_dst
, op
[0]);
1344 emit_scalar(ir
, TGSI_OPCODE_RCP
, result_dst
, op
[0]);
1348 emit_scalar(ir
, TGSI_OPCODE_EX2
, result_dst
, op
[0]);
1352 assert(!"not reached: should be handled by ir_explog_to_explog2");
1355 emit_scalar(ir
, TGSI_OPCODE_LG2
, result_dst
, op
[0]);
1358 emit_scalar(ir
, TGSI_OPCODE_SIN
, result_dst
, op
[0]);
1361 emit_scalar(ir
, TGSI_OPCODE_COS
, result_dst
, op
[0]);
1363 case ir_unop_sin_reduced
:
1364 emit_scs(ir
, TGSI_OPCODE_SIN
, result_dst
, op
[0]);
1366 case ir_unop_cos_reduced
:
1367 emit_scs(ir
, TGSI_OPCODE_COS
, result_dst
, op
[0]);
1371 emit(ir
, TGSI_OPCODE_DDX
, result_dst
, op
[0]);
1374 op
[0].negate
= ~op
[0].negate
;
1375 emit(ir
, TGSI_OPCODE_DDY
, result_dst
, op
[0]);
1378 case ir_unop_noise
: {
1379 /* At some point, a motivated person could add a better
1380 * implementation of noise. Currently not even the nvidia
1381 * binary drivers do anything more than this. In any case, the
1382 * place to do this is in the GL state tracker, not the poor
1385 emit(ir
, TGSI_OPCODE_MOV
, result_dst
, st_src_reg_for_float(0.5));
1390 emit(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1393 emit(ir
, TGSI_OPCODE_SUB
, result_dst
, op
[0], op
[1]);
1397 emit(ir
, TGSI_OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1400 if (result_dst
.type
== GLSL_TYPE_FLOAT
)
1401 assert(!"not reached: should be handled by ir_div_to_mul_rcp");
1403 emit(ir
, TGSI_OPCODE_DIV
, result_dst
, op
[0], op
[1]);
1406 if (result_dst
.type
== GLSL_TYPE_FLOAT
)
1407 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
1409 emit(ir
, TGSI_OPCODE_MOD
, result_dst
, op
[0], op
[1]);
1413 emit(ir
, TGSI_OPCODE_SLT
, result_dst
, op
[0], op
[1]);
1415 case ir_binop_greater
:
1416 emit(ir
, TGSI_OPCODE_SGT
, result_dst
, op
[0], op
[1]);
1418 case ir_binop_lequal
:
1419 emit(ir
, TGSI_OPCODE_SLE
, result_dst
, op
[0], op
[1]);
1421 case ir_binop_gequal
:
1422 emit(ir
, TGSI_OPCODE_SGE
, result_dst
, op
[0], op
[1]);
1424 case ir_binop_equal
:
1425 emit(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1427 case ir_binop_nequal
:
1428 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1430 case ir_binop_all_equal
:
1431 /* "==" operator producing a scalar boolean. */
1432 if (ir
->operands
[0]->type
->is_vector() ||
1433 ir
->operands
[1]->type
->is_vector()) {
1434 st_src_reg temp
= get_temp(glsl_version
>= 130 ?
1435 glsl_type::get_instance(ir
->operands
[0]->type
->base_type
, 4, 1) :
1436 glsl_type::vec4_type
);
1437 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
1438 emit(ir
, TGSI_OPCODE_SNE
, st_dst_reg(temp
), op
[0], op
[1]);
1439 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1440 emit(ir
, TGSI_OPCODE_SEQ
, result_dst
, result_src
, st_src_reg_for_float(0.0));
1442 emit(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1445 case ir_binop_any_nequal
:
1446 /* "!=" operator producing a scalar boolean. */
1447 if (ir
->operands
[0]->type
->is_vector() ||
1448 ir
->operands
[1]->type
->is_vector()) {
1449 st_src_reg temp
= get_temp(glsl_version
>= 130 ?
1450 glsl_type::get_instance(ir
->operands
[0]->type
->base_type
, 4, 1) :
1451 glsl_type::vec4_type
);
1452 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
1453 emit(ir
, TGSI_OPCODE_SNE
, st_dst_reg(temp
), op
[0], op
[1]);
1454 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1455 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, result_src
, st_src_reg_for_float(0.0));
1457 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1462 assert(ir
->operands
[0]->type
->is_vector());
1463 emit_dp(ir
, result_dst
, op
[0], op
[0],
1464 ir
->operands
[0]->type
->vector_elements
);
1465 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, result_src
, st_src_reg_for_float(0.0));
1468 case ir_binop_logic_xor
:
1469 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1472 case ir_binop_logic_or
:
1473 /* This could be a saturated add and skip the SNE. */
1474 emit(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1475 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, result_src
, st_src_reg_for_float(0.0));
1478 case ir_binop_logic_and
:
1479 /* the bool args are stored as float 0.0 or 1.0, so "mul" gives us "and". */
1480 emit(ir
, TGSI_OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1484 assert(ir
->operands
[0]->type
->is_vector());
1485 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
1486 emit_dp(ir
, result_dst
, op
[0], op
[1],
1487 ir
->operands
[0]->type
->vector_elements
);
1491 /* sqrt(x) = x * rsq(x). */
1492 emit_scalar(ir
, TGSI_OPCODE_RSQ
, result_dst
, op
[0]);
1493 emit(ir
, TGSI_OPCODE_MUL
, result_dst
, result_src
, op
[0]);
1494 /* For incoming channels <= 0, set the result to 0. */
1495 op
[0].negate
= ~op
[0].negate
;
1496 emit(ir
, TGSI_OPCODE_CMP
, result_dst
,
1497 op
[0], result_src
, st_src_reg_for_float(0.0));
1500 emit_scalar(ir
, TGSI_OPCODE_RSQ
, result_dst
, op
[0]);
1504 if (glsl_version
>= 130) {
1505 emit(ir
, TGSI_OPCODE_I2F
, result_dst
, op
[0]);
1509 /* Booleans are stored as integers (or floats in GLSL 1.20 and lower). */
1513 if (glsl_version
>= 130)
1514 emit(ir
, TGSI_OPCODE_F2I
, result_dst
, op
[0]);
1516 emit(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
1520 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0],
1521 st_src_reg_for_type(result_dst
.type
, 0));
1524 emit(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
1527 op
[0].negate
= ~op
[0].negate
;
1528 emit(ir
, TGSI_OPCODE_FLR
, result_dst
, op
[0]);
1529 result_src
.negate
= ~result_src
.negate
;
1532 emit(ir
, TGSI_OPCODE_FLR
, result_dst
, op
[0]);
1535 emit(ir
, TGSI_OPCODE_FRC
, result_dst
, op
[0]);
1539 emit(ir
, TGSI_OPCODE_MIN
, result_dst
, op
[0], op
[1]);
1542 emit(ir
, TGSI_OPCODE_MAX
, result_dst
, op
[0], op
[1]);
1545 emit_scalar(ir
, TGSI_OPCODE_POW
, result_dst
, op
[0], op
[1]);
1548 case ir_unop_bit_not
:
1549 if (glsl_version
>= 130) {
1550 emit(ir
, TGSI_OPCODE_NOT
, result_dst
, op
[0]);
1554 if (glsl_version
>= 130) {
1555 emit(ir
, TGSI_OPCODE_U2F
, result_dst
, op
[0]);
1558 case ir_binop_lshift
:
1559 if (glsl_version
>= 130) {
1560 emit(ir
, TGSI_OPCODE_SHL
, result_dst
, op
[0]);
1563 case ir_binop_rshift
:
1564 if (glsl_version
>= 130) {
1565 emit(ir
, TGSI_OPCODE_ISHR
, result_dst
, op
[0]);
1568 case ir_binop_bit_and
:
1569 if (glsl_version
>= 130) {
1570 emit(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0]);
1573 case ir_binop_bit_xor
:
1574 if (glsl_version
>= 130) {
1575 emit(ir
, TGSI_OPCODE_XOR
, result_dst
, op
[0]);
1578 case ir_binop_bit_or
:
1579 if (glsl_version
>= 130) {
1580 emit(ir
, TGSI_OPCODE_OR
, result_dst
, op
[0]);
1583 case ir_unop_round_even
:
1584 assert(!"GLSL 1.30 features unsupported");
1587 case ir_quadop_vector
:
1588 /* This operation should have already been handled.
1590 assert(!"Should not get here.");
1594 this->result
= result_src
;
1599 glsl_to_tgsi_visitor::visit(ir_swizzle
*ir
)
1605 /* Note that this is only swizzles in expressions, not those on the left
1606 * hand side of an assignment, which do write masking. See ir_assignment
1610 ir
->val
->accept(this);
1612 assert(src
.file
!= PROGRAM_UNDEFINED
);
1614 for (i
= 0; i
< 4; i
++) {
1615 if (i
< ir
->type
->vector_elements
) {
1618 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.x
);
1621 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.y
);
1624 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.z
);
1627 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.w
);
1631 /* If the type is smaller than a vec4, replicate the last
1634 swizzle
[i
] = swizzle
[ir
->type
->vector_elements
- 1];
1638 src
.swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
1644 glsl_to_tgsi_visitor::visit(ir_dereference_variable
*ir
)
1646 variable_storage
*entry
= find_variable_storage(ir
->var
);
1647 ir_variable
*var
= ir
->var
;
1650 switch (var
->mode
) {
1651 case ir_var_uniform
:
1652 entry
= new(mem_ctx
) variable_storage(var
, PROGRAM_UNIFORM
,
1654 this->variables
.push_tail(entry
);
1658 /* The linker assigns locations for varyings and attributes,
1659 * including deprecated builtins (like gl_Color), user-assign
1660 * generic attributes (glBindVertexLocation), and
1661 * user-defined varyings.
1663 * FINISHME: We would hit this path for function arguments. Fix!
1665 assert(var
->location
!= -1);
1666 entry
= new(mem_ctx
) variable_storage(var
,
1669 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
&&
1670 var
->location
>= VERT_ATTRIB_GENERIC0
) {
1671 _mesa_add_attribute(this->prog
->Attributes
,
1673 _mesa_sizeof_glsl_type(var
->type
->gl_type
),
1675 var
->location
- VERT_ATTRIB_GENERIC0
);
1679 assert(var
->location
!= -1);
1680 entry
= new(mem_ctx
) variable_storage(var
,
1684 case ir_var_system_value
:
1685 entry
= new(mem_ctx
) variable_storage(var
,
1686 PROGRAM_SYSTEM_VALUE
,
1690 case ir_var_temporary
:
1691 entry
= new(mem_ctx
) variable_storage(var
, PROGRAM_TEMPORARY
,
1693 this->variables
.push_tail(entry
);
1695 next_temp
+= type_size(var
->type
);
1700 printf("Failed to make storage for %s\n", var
->name
);
1705 this->result
= st_src_reg(entry
->file
, entry
->index
, var
->type
);
1706 if (glsl_version
<= 120)
1707 this->result
.type
= GLSL_TYPE_FLOAT
;
1711 glsl_to_tgsi_visitor::visit(ir_dereference_array
*ir
)
1715 int element_size
= type_size(ir
->type
);
1717 index
= ir
->array_index
->constant_expression_value();
1719 ir
->array
->accept(this);
1723 src
.index
+= index
->value
.i
[0] * element_size
;
1725 st_src_reg array_base
= this->result
;
1726 /* Variable index array dereference. It eats the "vec4" of the
1727 * base of the array and an index that offsets the TGSI register
1730 ir
->array_index
->accept(this);
1732 st_src_reg index_reg
;
1734 if (element_size
== 1) {
1735 index_reg
= this->result
;
1737 index_reg
= get_temp(glsl_type::float_type
);
1739 emit(ir
, TGSI_OPCODE_MUL
, st_dst_reg(index_reg
),
1740 this->result
, st_src_reg_for_float(element_size
));
1743 /* If there was already a relative address register involved, add the
1744 * new and the old together to get the new offset.
1746 if (src
.reladdr
!= NULL
) {
1747 st_src_reg accum_reg
= get_temp(glsl_type::float_type
);
1749 emit(ir
, TGSI_OPCODE_ADD
, st_dst_reg(accum_reg
),
1750 index_reg
, *src
.reladdr
);
1752 index_reg
= accum_reg
;
1755 src
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
1756 memcpy(src
.reladdr
, &index_reg
, sizeof(index_reg
));
1759 /* If the type is smaller than a vec4, replicate the last channel out. */
1760 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
1761 src
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1763 src
.swizzle
= SWIZZLE_NOOP
;
1769 glsl_to_tgsi_visitor::visit(ir_dereference_record
*ir
)
1772 const glsl_type
*struct_type
= ir
->record
->type
;
1775 ir
->record
->accept(this);
1777 for (i
= 0; i
< struct_type
->length
; i
++) {
1778 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
1780 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
1783 /* If the type is smaller than a vec4, replicate the last channel out. */
1784 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
1785 this->result
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1787 this->result
.swizzle
= SWIZZLE_NOOP
;
1789 this->result
.index
+= offset
;
1793 * We want to be careful in assignment setup to hit the actual storage
1794 * instead of potentially using a temporary like we might with the
1795 * ir_dereference handler.
1798 get_assignment_lhs(ir_dereference
*ir
, glsl_to_tgsi_visitor
*v
)
1800 /* The LHS must be a dereference. If the LHS is a variable indexed array
1801 * access of a vector, it must be separated into a series conditional moves
1802 * before reaching this point (see ir_vec_index_to_cond_assign).
1804 assert(ir
->as_dereference());
1805 ir_dereference_array
*deref_array
= ir
->as_dereference_array();
1807 assert(!deref_array
->array
->type
->is_vector());
1810 /* Use the rvalue deref handler for the most part. We'll ignore
1811 * swizzles in it and write swizzles using writemask, though.
1814 return st_dst_reg(v
->result
);
1818 * Process the condition of a conditional assignment
1820 * Examines the condition of a conditional assignment to generate the optimal
1821 * first operand of a \c CMP instruction. If the condition is a relational
1822 * operator with 0 (e.g., \c ir_binop_less), the value being compared will be
1823 * used as the source for the \c CMP instruction. Otherwise the comparison
1824 * is processed to a boolean result, and the boolean result is used as the
1825 * operand to the CMP instruction.
1828 glsl_to_tgsi_visitor::process_move_condition(ir_rvalue
*ir
)
1830 ir_rvalue
*src_ir
= ir
;
1832 bool switch_order
= false;
1834 ir_expression
*const expr
= ir
->as_expression();
1835 if ((expr
!= NULL
) && (expr
->get_num_operands() == 2)) {
1836 bool zero_on_left
= false;
1838 if (expr
->operands
[0]->is_zero()) {
1839 src_ir
= expr
->operands
[1];
1840 zero_on_left
= true;
1841 } else if (expr
->operands
[1]->is_zero()) {
1842 src_ir
= expr
->operands
[0];
1843 zero_on_left
= false;
1847 * (a < 0) T F F ( a < 0) T F F
1848 * (0 < a) F F T (-a < 0) F F T
1849 * (a <= 0) T T F (-a < 0) F F T (swap order of other operands)
1850 * (0 <= a) F T T ( a < 0) T F F (swap order of other operands)
1851 * (a > 0) F F T (-a < 0) F F T
1852 * (0 > a) T F F ( a < 0) T F F
1853 * (a >= 0) F T T ( a < 0) T F F (swap order of other operands)
1854 * (0 >= a) T T F (-a < 0) F F T (swap order of other operands)
1856 * Note that exchanging the order of 0 and 'a' in the comparison simply
1857 * means that the value of 'a' should be negated.
1860 switch (expr
->operation
) {
1862 switch_order
= false;
1863 negate
= zero_on_left
;
1866 case ir_binop_greater
:
1867 switch_order
= false;
1868 negate
= !zero_on_left
;
1871 case ir_binop_lequal
:
1872 switch_order
= true;
1873 negate
= !zero_on_left
;
1876 case ir_binop_gequal
:
1877 switch_order
= true;
1878 negate
= zero_on_left
;
1882 /* This isn't the right kind of comparison afterall, so make sure
1883 * the whole condition is visited.
1891 src_ir
->accept(this);
1893 /* We use the TGSI_OPCODE_CMP (a < 0 ? b : c) for conditional moves, and the
1894 * condition we produced is 0.0 or 1.0. By flipping the sign, we can
1895 * choose which value TGSI_OPCODE_CMP produces without an extra instruction
1896 * computing the condition.
1899 this->result
.negate
= ~this->result
.negate
;
1901 return switch_order
;
1905 glsl_to_tgsi_visitor::visit(ir_assignment
*ir
)
1911 ir
->rhs
->accept(this);
1914 l
= get_assignment_lhs(ir
->lhs
, this);
1916 /* FINISHME: This should really set to the correct maximal writemask for each
1917 * FINISHME: component written (in the loops below). This case can only
1918 * FINISHME: occur for matrices, arrays, and structures.
1920 if (ir
->write_mask
== 0) {
1921 assert(!ir
->lhs
->type
->is_scalar() && !ir
->lhs
->type
->is_vector());
1922 l
.writemask
= WRITEMASK_XYZW
;
1923 } else if (ir
->lhs
->type
->is_scalar() &&
1924 ir
->lhs
->variable_referenced()->mode
== ir_var_out
) {
1925 /* FINISHME: This hack makes writing to gl_FragDepth, which lives in the
1926 * FINISHME: W component of fragment shader output zero, work correctly.
1928 l
.writemask
= WRITEMASK_XYZW
;
1931 int first_enabled_chan
= 0;
1934 l
.writemask
= ir
->write_mask
;
1936 for (int i
= 0; i
< 4; i
++) {
1937 if (l
.writemask
& (1 << i
)) {
1938 first_enabled_chan
= GET_SWZ(r
.swizzle
, i
);
1943 /* Swizzle a small RHS vector into the channels being written.
1945 * glsl ir treats write_mask as dictating how many channels are
1946 * present on the RHS while TGSI treats write_mask as just
1947 * showing which channels of the vec4 RHS get written.
1949 for (int i
= 0; i
< 4; i
++) {
1950 if (l
.writemask
& (1 << i
))
1951 swizzles
[i
] = GET_SWZ(r
.swizzle
, rhs_chan
++);
1953 swizzles
[i
] = first_enabled_chan
;
1955 r
.swizzle
= MAKE_SWIZZLE4(swizzles
[0], swizzles
[1],
1956 swizzles
[2], swizzles
[3]);
1959 assert(l
.file
!= PROGRAM_UNDEFINED
);
1960 assert(r
.file
!= PROGRAM_UNDEFINED
);
1962 if (ir
->condition
) {
1963 const bool switch_order
= this->process_move_condition(ir
->condition
);
1964 st_src_reg condition
= this->result
;
1966 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
1967 st_src_reg l_src
= st_src_reg(l
);
1968 l_src
.swizzle
= swizzle_for_size(ir
->lhs
->type
->vector_elements
);
1971 emit(ir
, TGSI_OPCODE_CMP
, l
, condition
, l_src
, r
);
1973 emit(ir
, TGSI_OPCODE_CMP
, l
, condition
, r
, l_src
);
1979 } else if (ir
->rhs
->as_expression() &&
1980 this->instructions
.get_tail() &&
1981 ir
->rhs
== ((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->ir
&&
1982 type_size(ir
->lhs
->type
) == 1) {
1983 /* To avoid emitting an extra MOV when assigning an expression to a
1984 * variable, emit the last instruction of the expression again, but
1985 * replace the destination register with the target of the assignment.
1986 * Dead code elimination will remove the original instruction.
1988 glsl_to_tgsi_instruction
*inst
;
1989 inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
1990 emit(ir
, inst
->op
, l
, inst
->src
[0], inst
->src
[1], inst
->src
[2]);
1992 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
1993 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
2002 glsl_to_tgsi_visitor::visit(ir_constant
*ir
)
2005 GLfloat stack_vals
[4] = { 0 };
2006 gl_constant_value
*values
= (gl_constant_value
*) stack_vals
;
2007 GLenum gl_type
= GL_NONE
;
2009 static int in_array
= 0;
2010 gl_register_file file
= in_array
? PROGRAM_CONSTANT
: PROGRAM_IMMEDIATE
;
2012 /* Unfortunately, 4 floats is all we can get into
2013 * _mesa_add_typed_unnamed_constant. So, make a temp to store an
2014 * aggregate constant and move each constant value into it. If we
2015 * get lucky, copy propagation will eliminate the extra moves.
2017 if (ir
->type
->base_type
== GLSL_TYPE_STRUCT
) {
2018 st_src_reg temp_base
= get_temp(ir
->type
);
2019 st_dst_reg temp
= st_dst_reg(temp_base
);
2021 foreach_iter(exec_list_iterator
, iter
, ir
->components
) {
2022 ir_constant
*field_value
= (ir_constant
*)iter
.get();
2023 int size
= type_size(field_value
->type
);
2027 field_value
->accept(this);
2030 for (i
= 0; i
< (unsigned int)size
; i
++) {
2031 emit(ir
, TGSI_OPCODE_MOV
, temp
, src
);
2037 this->result
= temp_base
;
2041 if (ir
->type
->is_array()) {
2042 st_src_reg temp_base
= get_temp(ir
->type
);
2043 st_dst_reg temp
= st_dst_reg(temp_base
);
2044 int size
= type_size(ir
->type
->fields
.array
);
2049 for (i
= 0; i
< ir
->type
->length
; i
++) {
2050 ir
->array_elements
[i
]->accept(this);
2052 for (int j
= 0; j
< size
; j
++) {
2053 emit(ir
, TGSI_OPCODE_MOV
, temp
, src
);
2059 this->result
= temp_base
;
2064 if (ir
->type
->is_matrix()) {
2065 st_src_reg mat
= get_temp(ir
->type
);
2066 st_dst_reg mat_column
= st_dst_reg(mat
);
2068 for (i
= 0; i
< ir
->type
->matrix_columns
; i
++) {
2069 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
2070 values
= (gl_constant_value
*) &ir
->value
.f
[i
* ir
->type
->vector_elements
];
2072 src
= st_src_reg(file
, -1, ir
->type
->base_type
);
2073 src
.index
= add_constant(file
,
2075 ir
->type
->vector_elements
,
2078 emit(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
2087 switch (ir
->type
->base_type
) {
2088 case GLSL_TYPE_FLOAT
:
2090 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2091 values
[i
].f
= ir
->value
.f
[i
];
2094 case GLSL_TYPE_UINT
:
2095 gl_type
= glsl_version
>= 130 ? GL_UNSIGNED_INT
: GL_FLOAT
;
2096 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2097 if (glsl_version
>= 130)
2098 values
[i
].u
= ir
->value
.u
[i
];
2100 values
[i
].f
= ir
->value
.u
[i
];
2104 gl_type
= glsl_version
>= 130 ? GL_INT
: GL_FLOAT
;
2105 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2106 if (glsl_version
>= 130)
2107 values
[i
].i
= ir
->value
.i
[i
];
2109 values
[i
].f
= ir
->value
.i
[i
];
2112 case GLSL_TYPE_BOOL
:
2113 gl_type
= glsl_version
>= 130 ? GL_BOOL
: GL_FLOAT
;
2114 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2115 if (glsl_version
>= 130)
2116 values
[i
].b
= ir
->value
.b
[i
];
2118 values
[i
].f
= ir
->value
.b
[i
];
2122 assert(!"Non-float/uint/int/bool constant");
2125 this->result
= st_src_reg(file
, -1, ir
->type
);
2126 this->result
.index
= add_constant(file
,
2128 ir
->type
->vector_elements
,
2130 &this->result
.swizzle
);
2134 glsl_to_tgsi_visitor::get_function_signature(ir_function_signature
*sig
)
2136 function_entry
*entry
;
2138 foreach_iter(exec_list_iterator
, iter
, this->function_signatures
) {
2139 entry
= (function_entry
*)iter
.get();
2141 if (entry
->sig
== sig
)
2145 entry
= ralloc(mem_ctx
, function_entry
);
2147 entry
->sig_id
= this->next_signature_id
++;
2148 entry
->bgn_inst
= NULL
;
2150 /* Allocate storage for all the parameters. */
2151 foreach_iter(exec_list_iterator
, iter
, sig
->parameters
) {
2152 ir_variable
*param
= (ir_variable
*)iter
.get();
2153 variable_storage
*storage
;
2155 storage
= find_variable_storage(param
);
2158 storage
= new(mem_ctx
) variable_storage(param
, PROGRAM_TEMPORARY
,
2160 this->variables
.push_tail(storage
);
2162 this->next_temp
+= type_size(param
->type
);
2165 if (!sig
->return_type
->is_void()) {
2166 entry
->return_reg
= get_temp(sig
->return_type
);
2168 entry
->return_reg
= undef_src
;
2171 this->function_signatures
.push_tail(entry
);
2176 glsl_to_tgsi_visitor::visit(ir_call
*ir
)
2178 glsl_to_tgsi_instruction
*call_inst
;
2179 ir_function_signature
*sig
= ir
->get_callee();
2180 function_entry
*entry
= get_function_signature(sig
);
2183 /* Process in parameters. */
2184 exec_list_iterator sig_iter
= sig
->parameters
.iterator();
2185 foreach_iter(exec_list_iterator
, iter
, *ir
) {
2186 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
2187 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
2189 if (param
->mode
== ir_var_in
||
2190 param
->mode
== ir_var_inout
) {
2191 variable_storage
*storage
= find_variable_storage(param
);
2194 param_rval
->accept(this);
2195 st_src_reg r
= this->result
;
2198 l
.file
= storage
->file
;
2199 l
.index
= storage
->index
;
2201 l
.writemask
= WRITEMASK_XYZW
;
2202 l
.cond_mask
= COND_TR
;
2204 for (i
= 0; i
< type_size(param
->type
); i
++) {
2205 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
2213 assert(!sig_iter
.has_next());
2215 /* Emit call instruction */
2216 call_inst
= emit(ir
, TGSI_OPCODE_CAL
);
2217 call_inst
->function
= entry
;
2219 /* Process out parameters. */
2220 sig_iter
= sig
->parameters
.iterator();
2221 foreach_iter(exec_list_iterator
, iter
, *ir
) {
2222 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
2223 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
2225 if (param
->mode
== ir_var_out
||
2226 param
->mode
== ir_var_inout
) {
2227 variable_storage
*storage
= find_variable_storage(param
);
2231 r
.file
= storage
->file
;
2232 r
.index
= storage
->index
;
2234 r
.swizzle
= SWIZZLE_NOOP
;
2237 param_rval
->accept(this);
2238 st_dst_reg l
= st_dst_reg(this->result
);
2240 for (i
= 0; i
< type_size(param
->type
); i
++) {
2241 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
2249 assert(!sig_iter
.has_next());
2251 /* Process return value. */
2252 this->result
= entry
->return_reg
;
2256 glsl_to_tgsi_visitor::visit(ir_texture
*ir
)
2258 st_src_reg result_src
, coord
, lod_info
, projector
, dx
, dy
;
2259 st_dst_reg result_dst
, coord_dst
;
2260 glsl_to_tgsi_instruction
*inst
= NULL
;
2261 unsigned opcode
= TGSI_OPCODE_NOP
;
2263 ir
->coordinate
->accept(this);
2265 /* Put our coords in a temp. We'll need to modify them for shadow,
2266 * projection, or LOD, so the only case we'd use it as is is if
2267 * we're doing plain old texturing. The optimization passes on
2268 * glsl_to_tgsi_visitor should handle cleaning up our mess in that case.
2270 coord
= get_temp(glsl_type::vec4_type
);
2271 coord_dst
= st_dst_reg(coord
);
2272 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
2274 if (ir
->projector
) {
2275 ir
->projector
->accept(this);
2276 projector
= this->result
;
2279 /* Storage for our result. Ideally for an assignment we'd be using
2280 * the actual storage for the result here, instead.
2282 result_src
= get_temp(glsl_type::vec4_type
);
2283 result_dst
= st_dst_reg(result_src
);
2287 opcode
= TGSI_OPCODE_TEX
;
2290 opcode
= TGSI_OPCODE_TXB
;
2291 ir
->lod_info
.bias
->accept(this);
2292 lod_info
= this->result
;
2295 opcode
= TGSI_OPCODE_TXL
;
2296 ir
->lod_info
.lod
->accept(this);
2297 lod_info
= this->result
;
2300 opcode
= TGSI_OPCODE_TXD
;
2301 ir
->lod_info
.grad
.dPdx
->accept(this);
2303 ir
->lod_info
.grad
.dPdy
->accept(this);
2306 case ir_txf
: /* TODO: use TGSI_OPCODE_TXF here */
2307 assert(!"GLSL 1.30 features unsupported");
2311 if (ir
->projector
) {
2312 if (opcode
== TGSI_OPCODE_TEX
) {
2313 /* Slot the projector in as the last component of the coord. */
2314 coord_dst
.writemask
= WRITEMASK_W
;
2315 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, projector
);
2316 coord_dst
.writemask
= WRITEMASK_XYZW
;
2317 opcode
= TGSI_OPCODE_TXP
;
2319 st_src_reg coord_w
= coord
;
2320 coord_w
.swizzle
= SWIZZLE_WWWW
;
2322 /* For the other TEX opcodes there's no projective version
2323 * since the last slot is taken up by LOD info. Do the
2324 * projective divide now.
2326 coord_dst
.writemask
= WRITEMASK_W
;
2327 emit(ir
, TGSI_OPCODE_RCP
, coord_dst
, projector
);
2329 /* In the case where we have to project the coordinates "by hand,"
2330 * the shadow comparator value must also be projected.
2332 st_src_reg tmp_src
= coord
;
2333 if (ir
->shadow_comparitor
) {
2334 /* Slot the shadow value in as the second to last component of the
2337 ir
->shadow_comparitor
->accept(this);
2339 tmp_src
= get_temp(glsl_type::vec4_type
);
2340 st_dst_reg tmp_dst
= st_dst_reg(tmp_src
);
2342 tmp_dst
.writemask
= WRITEMASK_Z
;
2343 emit(ir
, TGSI_OPCODE_MOV
, tmp_dst
, this->result
);
2345 tmp_dst
.writemask
= WRITEMASK_XY
;
2346 emit(ir
, TGSI_OPCODE_MOV
, tmp_dst
, coord
);
2349 coord_dst
.writemask
= WRITEMASK_XYZ
;
2350 emit(ir
, TGSI_OPCODE_MUL
, coord_dst
, tmp_src
, coord_w
);
2352 coord_dst
.writemask
= WRITEMASK_XYZW
;
2353 coord
.swizzle
= SWIZZLE_XYZW
;
2357 /* If projection is done and the opcode is not TGSI_OPCODE_TXP, then the shadow
2358 * comparator was put in the correct place (and projected) by the code,
2359 * above, that handles by-hand projection.
2361 if (ir
->shadow_comparitor
&& (!ir
->projector
|| opcode
== TGSI_OPCODE_TXP
)) {
2362 /* Slot the shadow value in as the second to last component of the
2365 ir
->shadow_comparitor
->accept(this);
2366 coord_dst
.writemask
= WRITEMASK_Z
;
2367 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
2368 coord_dst
.writemask
= WRITEMASK_XYZW
;
2371 if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXB
) {
2372 /* TGSI stores LOD or LOD bias in the last channel of the coords. */
2373 coord_dst
.writemask
= WRITEMASK_W
;
2374 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, lod_info
);
2375 coord_dst
.writemask
= WRITEMASK_XYZW
;
2378 if (opcode
== TGSI_OPCODE_TXD
)
2379 inst
= emit(ir
, opcode
, result_dst
, coord
, dx
, dy
);
2381 inst
= emit(ir
, opcode
, result_dst
, coord
);
2383 if (ir
->shadow_comparitor
)
2384 inst
->tex_shadow
= GL_TRUE
;
2386 inst
->sampler
= _mesa_get_sampler_uniform_value(ir
->sampler
,
2387 this->shader_program
,
2390 const glsl_type
*sampler_type
= ir
->sampler
->type
;
2392 switch (sampler_type
->sampler_dimensionality
) {
2393 case GLSL_SAMPLER_DIM_1D
:
2394 inst
->tex_target
= (sampler_type
->sampler_array
)
2395 ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
2397 case GLSL_SAMPLER_DIM_2D
:
2398 inst
->tex_target
= (sampler_type
->sampler_array
)
2399 ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
2401 case GLSL_SAMPLER_DIM_3D
:
2402 inst
->tex_target
= TEXTURE_3D_INDEX
;
2404 case GLSL_SAMPLER_DIM_CUBE
:
2405 inst
->tex_target
= TEXTURE_CUBE_INDEX
;
2407 case GLSL_SAMPLER_DIM_RECT
:
2408 inst
->tex_target
= TEXTURE_RECT_INDEX
;
2410 case GLSL_SAMPLER_DIM_BUF
:
2411 assert(!"FINISHME: Implement ARB_texture_buffer_object");
2414 assert(!"Should not get here.");
2417 this->result
= result_src
;
2421 glsl_to_tgsi_visitor::visit(ir_return
*ir
)
2423 if (ir
->get_value()) {
2427 assert(current_function
);
2429 ir
->get_value()->accept(this);
2430 st_src_reg r
= this->result
;
2432 l
= st_dst_reg(current_function
->return_reg
);
2434 for (i
= 0; i
< type_size(current_function
->sig
->return_type
); i
++) {
2435 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
2441 emit(ir
, TGSI_OPCODE_RET
);
2445 glsl_to_tgsi_visitor::visit(ir_discard
*ir
)
2447 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
2449 if (ir
->condition
) {
2450 ir
->condition
->accept(this);
2451 this->result
.negate
= ~this->result
.negate
;
2452 emit(ir
, TGSI_OPCODE_KIL
, undef_dst
, this->result
);
2454 emit(ir
, TGSI_OPCODE_KILP
);
2457 fp
->UsesKill
= GL_TRUE
;
2461 glsl_to_tgsi_visitor::visit(ir_if
*ir
)
2463 glsl_to_tgsi_instruction
*cond_inst
, *if_inst
, *else_inst
= NULL
;
2464 glsl_to_tgsi_instruction
*prev_inst
;
2466 prev_inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
2468 ir
->condition
->accept(this);
2469 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
2471 if (this->options
->EmitCondCodes
) {
2472 cond_inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
2474 /* See if we actually generated any instruction for generating
2475 * the condition. If not, then cook up a move to a temp so we
2476 * have something to set cond_update on.
2478 if (cond_inst
== prev_inst
) {
2479 st_src_reg temp
= get_temp(glsl_type::bool_type
);
2480 cond_inst
= emit(ir
->condition
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), result
);
2482 cond_inst
->cond_update
= GL_TRUE
;
2484 if_inst
= emit(ir
->condition
, TGSI_OPCODE_IF
);
2485 if_inst
->dst
.cond_mask
= COND_NE
;
2487 if_inst
= emit(ir
->condition
, TGSI_OPCODE_IF
, undef_dst
, this->result
);
2490 this->instructions
.push_tail(if_inst
);
2492 visit_exec_list(&ir
->then_instructions
, this);
2494 if (!ir
->else_instructions
.is_empty()) {
2495 else_inst
= emit(ir
->condition
, TGSI_OPCODE_ELSE
);
2496 visit_exec_list(&ir
->else_instructions
, this);
2499 if_inst
= emit(ir
->condition
, TGSI_OPCODE_ENDIF
);
2502 glsl_to_tgsi_visitor::glsl_to_tgsi_visitor()
2504 result
.file
= PROGRAM_UNDEFINED
;
2506 next_signature_id
= 1;
2508 current_function
= NULL
;
2509 num_address_regs
= 0;
2510 indirect_addr_temps
= false;
2511 indirect_addr_consts
= false;
2512 mem_ctx
= ralloc_context(NULL
);
2515 glsl_to_tgsi_visitor::~glsl_to_tgsi_visitor()
2517 ralloc_free(mem_ctx
);
2520 extern "C" void free_glsl_to_tgsi_visitor(glsl_to_tgsi_visitor
*v
)
2527 * Count resources used by the given gpu program (number of texture
2531 count_resources(glsl_to_tgsi_visitor
*v
, gl_program
*prog
)
2533 v
->samplers_used
= 0;
2535 foreach_iter(exec_list_iterator
, iter
, v
->instructions
) {
2536 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
2538 if (is_tex_instruction(inst
->op
)) {
2539 v
->samplers_used
|= 1 << inst
->sampler
;
2541 prog
->SamplerTargets
[inst
->sampler
] =
2542 (gl_texture_index
)inst
->tex_target
;
2543 if (inst
->tex_shadow
) {
2544 prog
->ShadowSamplers
|= 1 << inst
->sampler
;
2549 prog
->SamplersUsed
= v
->samplers_used
;
2550 _mesa_update_shader_textures_used(prog
);
2555 * Check if the given vertex/fragment/shader program is within the
2556 * resource limits of the context (number of texture units, etc).
2557 * If any of those checks fail, record a linker error.
2559 * XXX more checks are needed...
2562 check_resources(const struct gl_context
*ctx
,
2563 struct gl_shader_program
*shader_program
,
2564 glsl_to_tgsi_visitor
*prog
,
2565 struct gl_program
*proginfo
)
2567 switch (proginfo
->Target
) {
2568 case GL_VERTEX_PROGRAM_ARB
:
2569 if (_mesa_bitcount(prog
->samplers_used
) >
2570 ctx
->Const
.MaxVertexTextureImageUnits
) {
2571 fail_link(shader_program
, "Too many vertex shader texture samplers");
2573 if (proginfo
->Parameters
->NumParameters
> MAX_UNIFORMS
) {
2574 fail_link(shader_program
, "Too many vertex shader constants");
2577 case MESA_GEOMETRY_PROGRAM
:
2578 if (_mesa_bitcount(prog
->samplers_used
) >
2579 ctx
->Const
.MaxGeometryTextureImageUnits
) {
2580 fail_link(shader_program
, "Too many geometry shader texture samplers");
2582 if (proginfo
->Parameters
->NumParameters
>
2583 MAX_GEOMETRY_UNIFORM_COMPONENTS
/ 4) {
2584 fail_link(shader_program
, "Too many geometry shader constants");
2587 case GL_FRAGMENT_PROGRAM_ARB
:
2588 if (_mesa_bitcount(prog
->samplers_used
) >
2589 ctx
->Const
.MaxTextureImageUnits
) {
2590 fail_link(shader_program
, "Too many fragment shader texture samplers");
2592 if (proginfo
->Parameters
->NumParameters
> MAX_UNIFORMS
) {
2593 fail_link(shader_program
, "Too many fragment shader constants");
2597 _mesa_problem(ctx
, "unexpected program type in check_resources()");
2603 struct uniform_sort
{
2604 struct gl_uniform
*u
;
2608 /* The shader_program->Uniforms list is almost sorted in increasing
2609 * uniform->{Frag,Vert}Pos locations, but not quite when there are
2610 * uniforms shared between targets. We need to add parameters in
2611 * increasing order for the targets.
2614 sort_uniforms(const void *a
, const void *b
)
2616 struct uniform_sort
*u1
= (struct uniform_sort
*)a
;
2617 struct uniform_sort
*u2
= (struct uniform_sort
*)b
;
2619 return u1
->pos
- u2
->pos
;
2622 /* Add the uniforms to the parameters. The linker chose locations
2623 * in our parameters lists (which weren't created yet), which the
2624 * uniforms code will use to poke values into our parameters list
2625 * when uniforms are updated.
2628 add_uniforms_to_parameters_list(struct gl_shader_program
*shader_program
,
2629 struct gl_shader
*shader
,
2630 struct gl_program
*prog
)
2633 unsigned int next_sampler
= 0, num_uniforms
= 0;
2634 struct uniform_sort
*sorted_uniforms
;
2636 sorted_uniforms
= ralloc_array(NULL
, struct uniform_sort
,
2637 shader_program
->Uniforms
->NumUniforms
);
2639 for (i
= 0; i
< shader_program
->Uniforms
->NumUniforms
; i
++) {
2640 struct gl_uniform
*uniform
= shader_program
->Uniforms
->Uniforms
+ i
;
2641 int parameter_index
= -1;
2643 switch (shader
->Type
) {
2644 case GL_VERTEX_SHADER
:
2645 parameter_index
= uniform
->VertPos
;
2647 case GL_FRAGMENT_SHADER
:
2648 parameter_index
= uniform
->FragPos
;
2650 case GL_GEOMETRY_SHADER
:
2651 parameter_index
= uniform
->GeomPos
;
2655 /* Only add uniforms used in our target. */
2656 if (parameter_index
!= -1) {
2657 sorted_uniforms
[num_uniforms
].pos
= parameter_index
;
2658 sorted_uniforms
[num_uniforms
].u
= uniform
;
2663 qsort(sorted_uniforms
, num_uniforms
, sizeof(struct uniform_sort
),
2666 for (i
= 0; i
< num_uniforms
; i
++) {
2667 struct gl_uniform
*uniform
= sorted_uniforms
[i
].u
;
2668 int parameter_index
= sorted_uniforms
[i
].pos
;
2669 const glsl_type
*type
= uniform
->Type
;
2672 if (type
->is_vector() ||
2673 type
->is_scalar()) {
2674 size
= type
->vector_elements
;
2676 size
= type_size(type
) * 4;
2679 gl_register_file file
;
2680 if (type
->is_sampler() ||
2681 (type
->is_array() && type
->fields
.array
->is_sampler())) {
2682 file
= PROGRAM_SAMPLER
;
2684 file
= PROGRAM_UNIFORM
;
2687 GLint index
= _mesa_lookup_parameter_index(prog
->Parameters
, -1,
2691 index
= _mesa_add_parameter(prog
->Parameters
, file
,
2692 uniform
->Name
, size
, type
->gl_type
,
2695 /* Sampler uniform values are stored in prog->SamplerUnits,
2696 * and the entry in that array is selected by this index we
2697 * store in ParameterValues[].
2699 if (file
== PROGRAM_SAMPLER
) {
2700 for (unsigned int j
= 0; j
< size
/ 4; j
++)
2701 prog
->Parameters
->ParameterValues
[index
+ j
][0].f
= next_sampler
++;
2704 /* The location chosen in the Parameters list here (returned
2705 * from _mesa_add_uniform) has to match what the linker chose.
2707 if (index
!= parameter_index
) {
2708 fail_link(shader_program
, "Allocation of uniform `%s' to target "
2709 "failed (%d vs %d)\n",
2710 uniform
->Name
, index
, parameter_index
);
2715 ralloc_free(sorted_uniforms
);
2719 set_uniform_initializer(struct gl_context
*ctx
, void *mem_ctx
,
2720 struct gl_shader_program
*shader_program
,
2721 const char *name
, const glsl_type
*type
,
2724 if (type
->is_record()) {
2725 ir_constant
*field_constant
;
2727 field_constant
= (ir_constant
*)val
->components
.get_head();
2729 for (unsigned int i
= 0; i
< type
->length
; i
++) {
2730 const glsl_type
*field_type
= type
->fields
.structure
[i
].type
;
2731 const char *field_name
= ralloc_asprintf(mem_ctx
, "%s.%s", name
,
2732 type
->fields
.structure
[i
].name
);
2733 set_uniform_initializer(ctx
, mem_ctx
, shader_program
, field_name
,
2734 field_type
, field_constant
);
2735 field_constant
= (ir_constant
*)field_constant
->next
;
2740 int loc
= _mesa_get_uniform_location(ctx
, shader_program
, name
);
2743 fail_link(shader_program
,
2744 "Couldn't find uniform for initializer %s\n", name
);
2748 for (unsigned int i
= 0; i
< (type
->is_array() ? type
->length
: 1); i
++) {
2749 ir_constant
*element
;
2750 const glsl_type
*element_type
;
2751 if (type
->is_array()) {
2752 element
= val
->array_elements
[i
];
2753 element_type
= type
->fields
.array
;
2756 element_type
= type
;
2761 if (element_type
->base_type
== GLSL_TYPE_BOOL
) {
2762 int *conv
= ralloc_array(mem_ctx
, int, element_type
->components());
2763 for (unsigned int j
= 0; j
< element_type
->components(); j
++) {
2764 conv
[j
] = element
->value
.b
[j
];
2766 values
= (void *)conv
;
2767 element_type
= glsl_type::get_instance(GLSL_TYPE_INT
,
2768 element_type
->vector_elements
,
2771 values
= &element
->value
;
2774 if (element_type
->is_matrix()) {
2775 _mesa_uniform_matrix(ctx
, shader_program
,
2776 element_type
->matrix_columns
,
2777 element_type
->vector_elements
,
2778 loc
, 1, GL_FALSE
, (GLfloat
*)values
);
2779 loc
+= element_type
->matrix_columns
;
2781 _mesa_uniform(ctx
, shader_program
, loc
, element_type
->matrix_columns
,
2782 values
, element_type
->gl_type
);
2783 loc
+= type_size(element_type
);
2789 set_uniform_initializers(struct gl_context
*ctx
,
2790 struct gl_shader_program
*shader_program
)
2792 void *mem_ctx
= NULL
;
2794 for (unsigned int i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
2795 struct gl_shader
*shader
= shader_program
->_LinkedShaders
[i
];
2800 foreach_iter(exec_list_iterator
, iter
, *shader
->ir
) {
2801 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
2802 ir_variable
*var
= ir
->as_variable();
2804 if (!var
|| var
->mode
!= ir_var_uniform
|| !var
->constant_value
)
2808 mem_ctx
= ralloc_context(NULL
);
2810 set_uniform_initializer(ctx
, mem_ctx
, shader_program
, var
->name
,
2811 var
->type
, var
->constant_value
);
2815 ralloc_free(mem_ctx
);
2819 * Scan/rewrite program to remove reads of custom (output) registers.
2820 * The passed type has to be either PROGRAM_OUTPUT or PROGRAM_VARYING
2821 * (for vertex shaders).
2822 * In GLSL shaders, varying vars can be read and written.
2823 * On some hardware, trying to read an output register causes trouble.
2824 * So, rewrite the program to use a temporary register in this case.
2826 * Based on _mesa_remove_output_reads from programopt.c.
2829 glsl_to_tgsi_visitor::remove_output_reads(gl_register_file type
)
2832 GLint outputMap
[VERT_RESULT_MAX
];
2833 GLint outputTypes
[VERT_RESULT_MAX
];
2834 GLuint numVaryingReads
= 0;
2835 GLboolean usedTemps
[MAX_TEMPS
];
2836 GLuint firstTemp
= 0;
2838 _mesa_find_used_registers(prog
, PROGRAM_TEMPORARY
,
2839 usedTemps
, MAX_TEMPS
);
2841 assert(type
== PROGRAM_VARYING
|| type
== PROGRAM_OUTPUT
);
2842 assert(prog
->Target
== GL_VERTEX_PROGRAM_ARB
|| type
!= PROGRAM_VARYING
);
2844 for (i
= 0; i
< VERT_RESULT_MAX
; i
++)
2847 /* look for instructions which read from varying vars */
2848 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2849 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
2850 const GLuint numSrc
= num_inst_src_regs(inst
->op
);
2852 for (j
= 0; j
< numSrc
; j
++) {
2853 if (inst
->src
[j
].file
== type
) {
2854 /* replace the read with a temp reg */
2855 const GLuint var
= inst
->src
[j
].index
;
2856 if (outputMap
[var
] == -1) {
2858 outputMap
[var
] = _mesa_find_free_register(usedTemps
,
2861 outputTypes
[var
] = inst
->src
[j
].type
;
2862 firstTemp
= outputMap
[var
] + 1;
2864 inst
->src
[j
].file
= PROGRAM_TEMPORARY
;
2865 inst
->src
[j
].index
= outputMap
[var
];
2870 if (numVaryingReads
== 0)
2871 return; /* nothing to be done */
2873 /* look for instructions which write to the varying vars identified above */
2874 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2875 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
2876 if (inst
->dst
.file
== type
&& outputMap
[inst
->dst
.index
] >= 0) {
2877 /* change inst to write to the temp reg, instead of the varying */
2878 inst
->dst
.file
= PROGRAM_TEMPORARY
;
2879 inst
->dst
.index
= outputMap
[inst
->dst
.index
];
2883 /* insert new MOV instructions at the end */
2884 for (i
= 0; i
< VERT_RESULT_MAX
; i
++) {
2885 if (outputMap
[i
] >= 0) {
2886 /* MOV VAR[i], TEMP[tmp]; */
2887 st_src_reg src
= st_src_reg(PROGRAM_TEMPORARY
, outputMap
[i
], outputTypes
[i
]);
2888 st_dst_reg dst
= st_dst_reg(type
, WRITEMASK_XYZW
, outputTypes
[i
]);
2890 this->emit(NULL
, TGSI_OPCODE_MOV
, dst
, src
);
2896 * Returns the mask of channels (bitmask of WRITEMASK_X,Y,Z,W) which
2897 * are read from the given src in this instruction
2900 get_src_arg_mask(st_dst_reg dst
, st_src_reg src
)
2902 int read_mask
= 0, comp
;
2904 /* Now, given the src swizzle and the written channels, find which
2905 * components are actually read
2907 for (comp
= 0; comp
< 4; ++comp
) {
2908 const unsigned coord
= GET_SWZ(src
.swizzle
, comp
);
2910 if (dst
.writemask
& (1 << comp
) && coord
<= SWIZZLE_W
)
2911 read_mask
|= 1 << coord
;
2918 * This pass replaces CMP T0, T1 T2 T0 with MOV T0, T2 when the CMP
2919 * instruction is the first instruction to write to register T0. There are
2920 * several lowering passes done in GLSL IR (e.g. branches and
2921 * relative addressing) that create a large number of conditional assignments
2922 * that ir_to_mesa converts to CMP instructions like the one mentioned above.
2924 * Here is why this conversion is safe:
2925 * CMP T0, T1 T2 T0 can be expanded to:
2931 * If (T1 < 0.0) evaluates to true then our replacement MOV T0, T2 is the same
2932 * as the original program. If (T1 < 0.0) evaluates to false, executing
2933 * MOV T0, T0 will store a garbage value in T0 since T0 is uninitialized.
2934 * Therefore, it doesn't matter that we are replacing MOV T0, T0 with MOV T0, T2
2935 * because any instruction that was going to read from T0 after this was going
2936 * to read a garbage value anyway.
2939 glsl_to_tgsi_visitor::simplify_cmp(void)
2941 unsigned tempWrites
[MAX_TEMPS
];
2942 unsigned outputWrites
[MAX_PROGRAM_OUTPUTS
];
2944 memset(tempWrites
, 0, sizeof(tempWrites
));
2945 memset(outputWrites
, 0, sizeof(outputWrites
));
2947 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2948 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
2949 unsigned prevWriteMask
= 0;
2951 /* Give up if we encounter relative addressing or flow control. */
2952 if (inst
->dst
.reladdr
||
2953 tgsi_get_opcode_info(inst
->op
)->is_branch
||
2954 inst
->op
== TGSI_OPCODE_BGNSUB
||
2955 inst
->op
== TGSI_OPCODE_CONT
||
2956 inst
->op
== TGSI_OPCODE_END
||
2957 inst
->op
== TGSI_OPCODE_ENDSUB
||
2958 inst
->op
== TGSI_OPCODE_RET
) {
2962 if (inst
->dst
.file
== PROGRAM_OUTPUT
) {
2963 assert(inst
->dst
.index
< MAX_PROGRAM_OUTPUTS
);
2964 prevWriteMask
= outputWrites
[inst
->dst
.index
];
2965 outputWrites
[inst
->dst
.index
] |= inst
->dst
.writemask
;
2966 } else if (inst
->dst
.file
== PROGRAM_TEMPORARY
) {
2967 assert(inst
->dst
.index
< MAX_TEMPS
);
2968 prevWriteMask
= tempWrites
[inst
->dst
.index
];
2969 tempWrites
[inst
->dst
.index
] |= inst
->dst
.writemask
;
2972 /* For a CMP to be considered a conditional write, the destination
2973 * register and source register two must be the same. */
2974 if (inst
->op
== TGSI_OPCODE_CMP
2975 && !(inst
->dst
.writemask
& prevWriteMask
)
2976 && inst
->src
[2].file
== inst
->dst
.file
2977 && inst
->src
[2].index
== inst
->dst
.index
2978 && inst
->dst
.writemask
== get_src_arg_mask(inst
->dst
, inst
->src
[2])) {
2980 inst
->op
= TGSI_OPCODE_MOV
;
2981 inst
->src
[0] = inst
->src
[1];
2986 /* Replaces all references to a temporary register index with another index. */
2988 glsl_to_tgsi_visitor::rename_temp_register(int index
, int new_index
)
2990 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2991 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
2994 for (j
=0; j
< num_inst_src_regs(inst
->op
); j
++) {
2995 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
&&
2996 inst
->src
[j
].index
== index
) {
2997 inst
->src
[j
].index
= new_index
;
3001 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== index
) {
3002 inst
->dst
.index
= new_index
;
3008 glsl_to_tgsi_visitor::get_first_temp_read(int index
)
3010 int depth
= 0; /* loop depth */
3011 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
3014 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3015 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3017 for (j
=0; j
< num_inst_src_regs(inst
->op
); j
++) {
3018 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
&&
3019 inst
->src
[j
].index
== index
) {
3020 return (depth
== 0) ? i
: loop_start
;
3024 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
3027 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
3040 glsl_to_tgsi_visitor::get_first_temp_write(int index
)
3042 int depth
= 0; /* loop depth */
3043 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
3046 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3047 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3049 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== index
) {
3050 return (depth
== 0) ? i
: loop_start
;
3053 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
3056 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
3069 glsl_to_tgsi_visitor::get_last_temp_read(int index
)
3071 int depth
= 0; /* loop depth */
3072 int last
= -1; /* index of last instruction that reads the temporary */
3075 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3076 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3078 for (j
=0; j
< num_inst_src_regs(inst
->op
); j
++) {
3079 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
&&
3080 inst
->src
[j
].index
== index
) {
3081 last
= (depth
== 0) ? i
: -2;
3085 if (inst
->op
== TGSI_OPCODE_BGNLOOP
)
3087 else if (inst
->op
== TGSI_OPCODE_ENDLOOP
)
3088 if (--depth
== 0 && last
== -2)
3100 glsl_to_tgsi_visitor::get_last_temp_write(int index
)
3102 int depth
= 0; /* loop depth */
3103 int last
= -1; /* index of last instruction that writes to the temporary */
3106 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3107 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3109 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== index
)
3110 last
= (depth
== 0) ? i
: -2;
3112 if (inst
->op
== TGSI_OPCODE_BGNLOOP
)
3114 else if (inst
->op
== TGSI_OPCODE_ENDLOOP
)
3115 if (--depth
== 0 && last
== -2)
3127 * On a basic block basis, tracks available PROGRAM_TEMPORARY register
3128 * channels for copy propagation and updates following instructions to
3129 * use the original versions.
3131 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
3132 * will occur. As an example, a TXP production before this pass:
3134 * 0: MOV TEMP[1], INPUT[4].xyyy;
3135 * 1: MOV TEMP[1].w, INPUT[4].wwww;
3136 * 2: TXP TEMP[2], TEMP[1], texture[0], 2D;
3140 * 0: MOV TEMP[1], INPUT[4].xyyy;
3141 * 1: MOV TEMP[1].w, INPUT[4].wwww;
3142 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
3144 * which allows for dead code elimination on TEMP[1]'s writes.
3147 glsl_to_tgsi_visitor::copy_propagate(void)
3149 glsl_to_tgsi_instruction
**acp
= rzalloc_array(mem_ctx
,
3150 glsl_to_tgsi_instruction
*,
3151 this->next_temp
* 4);
3152 int *acp_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
3155 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3156 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3158 assert(inst
->dst
.file
!= PROGRAM_TEMPORARY
3159 || inst
->dst
.index
< this->next_temp
);
3161 /* First, do any copy propagation possible into the src regs. */
3162 for (int r
= 0; r
< 3; r
++) {
3163 glsl_to_tgsi_instruction
*first
= NULL
;
3165 int acp_base
= inst
->src
[r
].index
* 4;
3167 if (inst
->src
[r
].file
!= PROGRAM_TEMPORARY
||
3168 inst
->src
[r
].reladdr
)
3171 /* See if we can find entries in the ACP consisting of MOVs
3172 * from the same src register for all the swizzled channels
3173 * of this src register reference.
3175 for (int i
= 0; i
< 4; i
++) {
3176 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
3177 glsl_to_tgsi_instruction
*copy_chan
= acp
[acp_base
+ src_chan
];
3184 assert(acp_level
[acp_base
+ src_chan
] <= level
);
3189 if (first
->src
[0].file
!= copy_chan
->src
[0].file
||
3190 first
->src
[0].index
!= copy_chan
->src
[0].index
) {
3198 /* We've now validated that we can copy-propagate to
3199 * replace this src register reference. Do it.
3201 inst
->src
[r
].file
= first
->src
[0].file
;
3202 inst
->src
[r
].index
= first
->src
[0].index
;
3205 for (int i
= 0; i
< 4; i
++) {
3206 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
3207 glsl_to_tgsi_instruction
*copy_inst
= acp
[acp_base
+ src_chan
];
3208 swizzle
|= (GET_SWZ(copy_inst
->src
[0].swizzle
, src_chan
) <<
3211 inst
->src
[r
].swizzle
= swizzle
;
3216 case TGSI_OPCODE_BGNLOOP
:
3217 case TGSI_OPCODE_ENDLOOP
:
3218 /* End of a basic block, clear the ACP entirely. */
3219 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
3222 case TGSI_OPCODE_IF
:
3226 case TGSI_OPCODE_ENDIF
:
3227 case TGSI_OPCODE_ELSE
:
3228 /* Clear all channels written inside the block from the ACP, but
3229 * leaving those that were not touched.
3231 for (int r
= 0; r
< this->next_temp
; r
++) {
3232 for (int c
= 0; c
< 4; c
++) {
3233 if (!acp
[4 * r
+ c
])
3236 if (acp_level
[4 * r
+ c
] >= level
)
3237 acp
[4 * r
+ c
] = NULL
;
3240 if (inst
->op
== TGSI_OPCODE_ENDIF
)
3245 /* Continuing the block, clear any written channels from
3248 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.reladdr
) {
3249 /* Any temporary might be written, so no copy propagation
3250 * across this instruction.
3252 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
3253 } else if (inst
->dst
.file
== PROGRAM_OUTPUT
&&
3254 inst
->dst
.reladdr
) {
3255 /* Any output might be written, so no copy propagation
3256 * from outputs across this instruction.
3258 for (int r
= 0; r
< this->next_temp
; r
++) {
3259 for (int c
= 0; c
< 4; c
++) {
3260 if (!acp
[4 * r
+ c
])
3263 if (acp
[4 * r
+ c
]->src
[0].file
== PROGRAM_OUTPUT
)
3264 acp
[4 * r
+ c
] = NULL
;
3267 } else if (inst
->dst
.file
== PROGRAM_TEMPORARY
||
3268 inst
->dst
.file
== PROGRAM_OUTPUT
) {
3269 /* Clear where it's used as dst. */
3270 if (inst
->dst
.file
== PROGRAM_TEMPORARY
) {
3271 for (int c
= 0; c
< 4; c
++) {
3272 if (inst
->dst
.writemask
& (1 << c
)) {
3273 acp
[4 * inst
->dst
.index
+ c
] = NULL
;
3278 /* Clear where it's used as src. */
3279 for (int r
= 0; r
< this->next_temp
; r
++) {
3280 for (int c
= 0; c
< 4; c
++) {
3281 if (!acp
[4 * r
+ c
])
3284 int src_chan
= GET_SWZ(acp
[4 * r
+ c
]->src
[0].swizzle
, c
);
3286 if (acp
[4 * r
+ c
]->src
[0].file
== inst
->dst
.file
&&
3287 acp
[4 * r
+ c
]->src
[0].index
== inst
->dst
.index
&&
3288 inst
->dst
.writemask
& (1 << src_chan
))
3290 acp
[4 * r
+ c
] = NULL
;
3298 /* If this is a copy, add it to the ACP. */
3299 if (inst
->op
== TGSI_OPCODE_MOV
&&
3300 inst
->dst
.file
== PROGRAM_TEMPORARY
&&
3301 !inst
->dst
.reladdr
&&
3303 !inst
->src
[0].reladdr
&&
3304 !inst
->src
[0].negate
) {
3305 for (int i
= 0; i
< 4; i
++) {
3306 if (inst
->dst
.writemask
& (1 << i
)) {
3307 acp
[4 * inst
->dst
.index
+ i
] = inst
;
3308 acp_level
[4 * inst
->dst
.index
+ i
] = level
;
3314 ralloc_free(acp_level
);
3319 * Tracks available PROGRAM_TEMPORARY registers for dead code elimination.
3321 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
3322 * will occur. As an example, a TXP production after copy propagation but
3325 * 0: MOV TEMP[1], INPUT[4].xyyy;
3326 * 1: MOV TEMP[1].w, INPUT[4].wwww;
3327 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
3329 * and after this pass:
3331 * 0: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
3333 * FIXME: assumes that all functions are inlined (no support for BGNSUB/ENDSUB)
3334 * FIXME: doesn't eliminate all dead code inside of loops; it steps around them
3337 glsl_to_tgsi_visitor::eliminate_dead_code(void)
3341 for (i
=0; i
< this->next_temp
; i
++) {
3342 int last_read
= get_last_temp_read(i
);
3345 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3346 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3348 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== i
&&
3361 * On a basic block basis, tracks available PROGRAM_TEMPORARY registers for dead
3362 * code elimination. This is less primitive than eliminate_dead_code(), as it
3363 * is per-channel and can detect consecutive writes without a read between them
3364 * as dead code. However, there is some dead code that can be eliminated by
3365 * eliminate_dead_code() but not this function - for example, this function
3366 * cannot eliminate an instruction writing to a register that is never read and
3367 * is the only instruction writing to that register.
3369 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
3373 glsl_to_tgsi_visitor::eliminate_dead_code_advanced(void)
3375 glsl_to_tgsi_instruction
**writes
= rzalloc_array(mem_ctx
,
3376 glsl_to_tgsi_instruction
*,
3377 this->next_temp
* 4);
3378 int *write_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
3382 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3383 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3385 assert(inst
->dst
.file
!= PROGRAM_TEMPORARY
3386 || inst
->dst
.index
< this->next_temp
);
3389 case TGSI_OPCODE_BGNLOOP
:
3390 case TGSI_OPCODE_ENDLOOP
:
3391 /* End of a basic block, clear the write array entirely.
3392 * FIXME: This keeps us from killing dead code when the writes are
3393 * on either side of a loop, even when the register isn't touched
3396 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
3399 case TGSI_OPCODE_ENDIF
:
3403 case TGSI_OPCODE_ELSE
:
3404 /* Clear all channels written inside the preceding if block from the
3405 * write array, but leave those that were not touched.
3407 * FIXME: This destroys opportunities to remove dead code inside of
3408 * IF blocks that are followed by an ELSE block.
3410 for (int r
= 0; r
< this->next_temp
; r
++) {
3411 for (int c
= 0; c
< 4; c
++) {
3412 if (!writes
[4 * r
+ c
])
3415 if (write_level
[4 * r
+ c
] >= level
)
3416 writes
[4 * r
+ c
] = NULL
;
3421 case TGSI_OPCODE_IF
:
3423 /* fallthrough to default case to mark the condition as read */
3426 /* Continuing the block, clear any channels from the write array that
3427 * are read by this instruction.
3429 for (int i
= 0; i
< 4; i
++) {
3430 if (inst
->src
[i
].file
== PROGRAM_TEMPORARY
&& inst
->src
[i
].reladdr
){
3431 /* Any temporary might be read, so no dead code elimination
3432 * across this instruction.
3434 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
3435 } else if (inst
->src
[i
].file
== PROGRAM_TEMPORARY
) {
3436 /* Clear where it's used as src. */
3437 int src_chans
= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 0);
3438 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 1);
3439 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 2);
3440 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 3);
3442 for (int c
= 0; c
< 4; c
++) {
3443 if (src_chans
& (1 << c
)) {
3444 writes
[4 * inst
->src
[i
].index
+ c
] = NULL
;
3452 /* If this instruction writes to a temporary, add it to the write array.
3453 * If there is already an instruction in the write array for one or more
3454 * of the channels, flag that channel write as dead.
3456 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&&
3457 !inst
->dst
.reladdr
&&
3459 for (int c
= 0; c
< 4; c
++) {
3460 if (inst
->dst
.writemask
& (1 << c
)) {
3461 if (writes
[4 * inst
->dst
.index
+ c
]) {
3462 if (write_level
[4 * inst
->dst
.index
+ c
] < level
)
3465 writes
[4 * inst
->dst
.index
+ c
]->dead_mask
|= (1 << c
);
3467 writes
[4 * inst
->dst
.index
+ c
] = inst
;
3468 write_level
[4 * inst
->dst
.index
+ c
] = level
;
3474 /* Anything still in the write array at this point is dead code. */
3475 for (int r
= 0; r
< this->next_temp
; r
++) {
3476 for (int c
= 0; c
< 4; c
++) {
3477 glsl_to_tgsi_instruction
*inst
= writes
[4 * r
+ c
];
3479 inst
->dead_mask
|= (1 << c
);
3483 /* Now actually remove the instructions that are completely dead and update
3484 * the writemask of other instructions with dead channels.
3486 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3487 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3489 if (!inst
->dead_mask
|| !inst
->dst
.writemask
)
3491 else if (inst
->dead_mask
== inst
->dst
.writemask
) {
3496 inst
->dst
.writemask
&= ~(inst
->dead_mask
);
3499 ralloc_free(write_level
);
3500 ralloc_free(writes
);
3505 /* Merges temporary registers together where possible to reduce the number of
3506 * registers needed to run a program.
3508 * Produces optimal code only after copy propagation and dead code elimination
3511 glsl_to_tgsi_visitor::merge_registers(void)
3513 int *last_reads
= rzalloc_array(mem_ctx
, int, this->next_temp
);
3514 int *first_writes
= rzalloc_array(mem_ctx
, int, this->next_temp
);
3517 /* Read the indices of the last read and first write to each temp register
3518 * into an array so that we don't have to traverse the instruction list as
3520 for (i
=0; i
< this->next_temp
; i
++) {
3521 last_reads
[i
] = get_last_temp_read(i
);
3522 first_writes
[i
] = get_first_temp_write(i
);
3525 /* Start looking for registers with non-overlapping usages that can be
3526 * merged together. */
3527 for (i
=0; i
< this->next_temp
; i
++) {
3528 /* Don't touch unused registers. */
3529 if (last_reads
[i
] < 0 || first_writes
[i
] < 0) continue;
3531 for (j
=0; j
< this->next_temp
; j
++) {
3532 /* Don't touch unused registers. */
3533 if (last_reads
[j
] < 0 || first_writes
[j
] < 0) continue;
3535 /* We can merge the two registers if the first write to j is after or
3536 * in the same instruction as the last read from i. Note that the
3537 * register at index i will always be used earlier or at the same time
3538 * as the register at index j. */
3539 if (first_writes
[i
] <= first_writes
[j
] &&
3540 last_reads
[i
] <= first_writes
[j
])
3542 rename_temp_register(j
, i
); /* Replace all references to j with i.*/
3544 /* Update the first_writes and last_reads arrays with the new
3545 * values for the merged register index, and mark the newly unused
3546 * register index as such. */
3547 last_reads
[i
] = last_reads
[j
];
3548 first_writes
[j
] = -1;
3554 ralloc_free(last_reads
);
3555 ralloc_free(first_writes
);
3558 /* Reassign indices to temporary registers by reusing unused indices created
3559 * by optimization passes. */
3561 glsl_to_tgsi_visitor::renumber_registers(void)
3566 for (i
=0; i
< this->next_temp
; i
++) {
3567 if (get_first_temp_read(i
) < 0) continue;
3569 rename_temp_register(i
, new_index
);
3573 this->next_temp
= new_index
;
3577 * Returns a fragment program which implements the current pixel transfer ops.
3578 * Based on get_pixel_transfer_program in st_atom_pixeltransfer.c.
3581 get_pixel_transfer_visitor(struct st_fragment_program
*fp
,
3582 glsl_to_tgsi_visitor
*original
,
3583 int scale_and_bias
, int pixel_maps
)
3585 glsl_to_tgsi_visitor
*v
= new glsl_to_tgsi_visitor();
3586 struct st_context
*st
= st_context(original
->ctx
);
3587 struct gl_program
*prog
= &fp
->Base
.Base
;
3588 struct gl_program_parameter_list
*params
= _mesa_new_parameter_list();
3589 st_src_reg coord
, src0
;
3591 glsl_to_tgsi_instruction
*inst
;
3593 /* Copy attributes of the glsl_to_tgsi_visitor in the original shader. */
3594 v
->ctx
= original
->ctx
;
3596 v
->glsl_version
= original
->glsl_version
;
3597 v
->options
= original
->options
;
3598 v
->next_temp
= original
->next_temp
;
3599 v
->num_address_regs
= original
->num_address_regs
;
3600 v
->samplers_used
= prog
->SamplersUsed
= original
->samplers_used
;
3601 v
->indirect_addr_temps
= original
->indirect_addr_temps
;
3602 v
->indirect_addr_consts
= original
->indirect_addr_consts
;
3603 memcpy(&v
->immediates
, &original
->immediates
, sizeof(v
->immediates
));
3606 * Get initial pixel color from the texture.
3607 * TEX colorTemp, fragment.texcoord[0], texture[0], 2D;
3609 coord
= st_src_reg(PROGRAM_INPUT
, FRAG_ATTRIB_TEX0
, glsl_type::vec2_type
);
3610 src0
= v
->get_temp(glsl_type::vec4_type
);
3611 dst0
= st_dst_reg(src0
);
3612 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, dst0
, coord
);
3614 inst
->tex_target
= TEXTURE_2D_INDEX
;
3616 prog
->InputsRead
|= (1 << FRAG_ATTRIB_TEX0
);
3617 prog
->SamplersUsed
|= (1 << 0); /* mark sampler 0 as used */
3618 v
->samplers_used
|= (1 << 0);
3620 if (scale_and_bias
) {
3621 static const gl_state_index scale_state
[STATE_LENGTH
] =
3622 { STATE_INTERNAL
, STATE_PT_SCALE
,
3623 (gl_state_index
) 0, (gl_state_index
) 0, (gl_state_index
) 0 };
3624 static const gl_state_index bias_state
[STATE_LENGTH
] =
3625 { STATE_INTERNAL
, STATE_PT_BIAS
,
3626 (gl_state_index
) 0, (gl_state_index
) 0, (gl_state_index
) 0 };
3627 GLint scale_p
, bias_p
;
3628 st_src_reg scale
, bias
;
3630 scale_p
= _mesa_add_state_reference(params
, scale_state
);
3631 bias_p
= _mesa_add_state_reference(params
, bias_state
);
3633 /* MAD colorTemp, colorTemp, scale, bias; */
3634 scale
= st_src_reg(PROGRAM_STATE_VAR
, scale_p
, GLSL_TYPE_FLOAT
);
3635 bias
= st_src_reg(PROGRAM_STATE_VAR
, bias_p
, GLSL_TYPE_FLOAT
);
3636 inst
= v
->emit(NULL
, TGSI_OPCODE_MAD
, dst0
, src0
, scale
, bias
);
3640 st_src_reg temp
= v
->get_temp(glsl_type::vec4_type
);
3641 st_dst_reg temp_dst
= st_dst_reg(temp
);
3643 assert(st
->pixel_xfer
.pixelmap_texture
);
3645 /* With a little effort, we can do four pixel map look-ups with
3646 * two TEX instructions:
3649 /* TEX temp.rg, colorTemp.rgba, texture[1], 2D; */
3650 temp_dst
.writemask
= WRITEMASK_XY
; /* write R,G */
3651 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, temp_dst
, src0
);
3653 inst
->tex_target
= TEXTURE_2D_INDEX
;
3655 /* TEX temp.ba, colorTemp.baba, texture[1], 2D; */
3656 src0
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_Z
, SWIZZLE_W
, SWIZZLE_Z
, SWIZZLE_W
);
3657 temp_dst
.writemask
= WRITEMASK_ZW
; /* write B,A */
3658 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, temp_dst
, src0
);
3660 inst
->tex_target
= TEXTURE_2D_INDEX
;
3662 prog
->SamplersUsed
|= (1 << 1); /* mark sampler 1 as used */
3663 v
->samplers_used
|= (1 << 1);
3665 /* MOV colorTemp, temp; */
3666 inst
= v
->emit(NULL
, TGSI_OPCODE_MOV
, dst0
, temp
);
3669 /* Now copy the instructions from the original glsl_to_tgsi_visitor into the
3671 foreach_iter(exec_list_iterator
, iter
, original
->instructions
) {
3672 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3673 st_src_reg src_regs
[3];
3675 if (inst
->dst
.file
== PROGRAM_OUTPUT
)
3676 prog
->OutputsWritten
|= BITFIELD64_BIT(inst
->dst
.index
);
3678 for (int i
=0; i
<3; i
++) {
3679 src_regs
[i
] = inst
->src
[i
];
3680 if (src_regs
[i
].file
== PROGRAM_INPUT
&&
3681 src_regs
[i
].index
== FRAG_ATTRIB_COL0
)
3683 src_regs
[i
].file
= PROGRAM_TEMPORARY
;
3684 src_regs
[i
].index
= src0
.index
;
3686 else if (src_regs
[i
].file
== PROGRAM_INPUT
)
3687 prog
->InputsRead
|= (1 << src_regs
[i
].index
);
3690 v
->emit(NULL
, inst
->op
, inst
->dst
, src_regs
[0], src_regs
[1], src_regs
[2]);
3693 /* Make modifications to fragment program info. */
3694 prog
->Parameters
= _mesa_combine_parameter_lists(params
,
3695 original
->prog
->Parameters
);
3696 prog
->Attributes
= _mesa_clone_parameter_list(original
->prog
->Attributes
);
3697 prog
->Varying
= _mesa_clone_parameter_list(original
->prog
->Varying
);
3698 _mesa_free_parameter_list(params
);
3699 count_resources(v
, prog
);
3700 fp
->glsl_to_tgsi
= v
;
3704 * Make fragment program for glBitmap:
3705 * Sample the texture and kill the fragment if the bit is 0.
3706 * This program will be combined with the user's fragment program.
3708 * Based on make_bitmap_fragment_program in st_cb_bitmap.c.
3711 get_bitmap_visitor(struct st_fragment_program
*fp
,
3712 glsl_to_tgsi_visitor
*original
, int samplerIndex
)
3714 glsl_to_tgsi_visitor
*v
= new glsl_to_tgsi_visitor();
3715 struct st_context
*st
= st_context(original
->ctx
);
3716 struct gl_program
*prog
= &fp
->Base
.Base
;
3717 st_src_reg coord
, src0
;
3719 glsl_to_tgsi_instruction
*inst
;
3721 /* Copy attributes of the glsl_to_tgsi_visitor in the original shader. */
3722 v
->ctx
= original
->ctx
;
3724 v
->glsl_version
= original
->glsl_version
;
3725 v
->options
= original
->options
;
3726 v
->next_temp
= original
->next_temp
;
3727 v
->num_address_regs
= original
->num_address_regs
;
3728 v
->samplers_used
= prog
->SamplersUsed
= original
->samplers_used
;
3729 v
->indirect_addr_temps
= original
->indirect_addr_temps
;
3730 v
->indirect_addr_consts
= original
->indirect_addr_consts
;
3731 memcpy(&v
->immediates
, &original
->immediates
, sizeof(v
->immediates
));
3733 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
3734 coord
= st_src_reg(PROGRAM_INPUT
, FRAG_ATTRIB_TEX0
, glsl_type::vec2_type
);
3735 src0
= v
->get_temp(glsl_type::vec4_type
);
3736 dst0
= st_dst_reg(src0
);
3737 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, dst0
, coord
);
3738 inst
->sampler
= samplerIndex
;
3739 inst
->tex_target
= TEXTURE_2D_INDEX
;
3741 prog
->InputsRead
|= (1 << FRAG_ATTRIB_TEX0
);
3742 prog
->SamplersUsed
|= (1 << samplerIndex
); /* mark sampler as used */
3743 v
->samplers_used
|= (1 << samplerIndex
);
3745 /* KIL if -tmp0 < 0 # texel=0 -> keep / texel=0 -> discard */
3746 src0
.negate
= NEGATE_XYZW
;
3747 if (st
->bitmap
.tex_format
== PIPE_FORMAT_L8_UNORM
)
3748 src0
.swizzle
= SWIZZLE_XXXX
;
3749 inst
= v
->emit(NULL
, TGSI_OPCODE_KIL
, undef_dst
, src0
);
3751 /* Now copy the instructions from the original glsl_to_tgsi_visitor into the
3753 foreach_iter(exec_list_iterator
, iter
, original
->instructions
) {
3754 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3755 st_src_reg src_regs
[3];
3757 if (inst
->dst
.file
== PROGRAM_OUTPUT
)
3758 prog
->OutputsWritten
|= BITFIELD64_BIT(inst
->dst
.index
);
3760 for (int i
=0; i
<3; i
++) {
3761 src_regs
[i
] = inst
->src
[i
];
3762 if (src_regs
[i
].file
== PROGRAM_INPUT
)
3763 prog
->InputsRead
|= (1 << src_regs
[i
].index
);
3766 v
->emit(NULL
, inst
->op
, inst
->dst
, src_regs
[0], src_regs
[1], src_regs
[2]);
3769 /* Make modifications to fragment program info. */
3770 prog
->Parameters
= _mesa_clone_parameter_list(original
->prog
->Parameters
);
3771 prog
->Attributes
= _mesa_clone_parameter_list(original
->prog
->Attributes
);
3772 prog
->Varying
= _mesa_clone_parameter_list(original
->prog
->Varying
);
3773 count_resources(v
, prog
);
3774 fp
->glsl_to_tgsi
= v
;
3777 /* ------------------------- TGSI conversion stuff -------------------------- */
3779 unsigned branch_target
;
3784 * Intermediate state used during shader translation.
3786 struct st_translate
{
3787 struct ureg_program
*ureg
;
3789 struct ureg_dst temps
[MAX_TEMPS
];
3790 struct ureg_src
*constants
;
3791 struct ureg_src
*immediates
;
3792 struct ureg_dst outputs
[PIPE_MAX_SHADER_OUTPUTS
];
3793 struct ureg_src inputs
[PIPE_MAX_SHADER_INPUTS
];
3794 struct ureg_dst address
[1];
3795 struct ureg_src samplers
[PIPE_MAX_SAMPLERS
];
3796 struct ureg_src systemValues
[SYSTEM_VALUE_MAX
];
3798 /* Extra info for handling point size clamping in vertex shader */
3799 struct ureg_dst pointSizeResult
; /**< Actual point size output register */
3800 struct ureg_src pointSizeConst
; /**< Point size range constant register */
3801 GLint pointSizeOutIndex
; /**< Temp point size output register */
3802 GLboolean prevInstWrotePointSize
;
3804 const GLuint
*inputMapping
;
3805 const GLuint
*outputMapping
;
3807 /* For every instruction that contains a label (eg CALL), keep
3808 * details so that we can go back afterwards and emit the correct
3809 * tgsi instruction number for each label.
3811 struct label
*labels
;
3812 unsigned labels_size
;
3813 unsigned labels_count
;
3815 /* Keep a record of the tgsi instruction number that each mesa
3816 * instruction starts at, will be used to fix up labels after
3821 unsigned insn_count
;
3823 unsigned procType
; /**< TGSI_PROCESSOR_VERTEX/FRAGMENT */
3828 /** Map Mesa's SYSTEM_VALUE_x to TGSI_SEMANTIC_x */
3829 static unsigned mesa_sysval_to_semantic
[SYSTEM_VALUE_MAX
] = {
3831 TGSI_SEMANTIC_INSTANCEID
3835 * Make note of a branch to a label in the TGSI code.
3836 * After we've emitted all instructions, we'll go over the list
3837 * of labels built here and patch the TGSI code with the actual
3838 * location of each label.
3840 static unsigned *get_label(struct st_translate
*t
, unsigned branch_target
)
3844 if (t
->labels_count
+ 1 >= t
->labels_size
) {
3845 t
->labels_size
= 1 << (util_logbase2(t
->labels_size
) + 1);
3846 t
->labels
= (struct label
*)realloc(t
->labels
,
3847 t
->labels_size
* sizeof(struct label
));
3848 if (t
->labels
== NULL
) {
3849 static unsigned dummy
;
3855 i
= t
->labels_count
++;
3856 t
->labels
[i
].branch_target
= branch_target
;
3857 return &t
->labels
[i
].token
;
3861 * Called prior to emitting the TGSI code for each instruction.
3862 * Allocate additional space for instructions if needed.
3863 * Update the insn[] array so the next glsl_to_tgsi_instruction points to
3864 * the next TGSI instruction.
3866 static void set_insn_start(struct st_translate
*t
, unsigned start
)
3868 if (t
->insn_count
+ 1 >= t
->insn_size
) {
3869 t
->insn_size
= 1 << (util_logbase2(t
->insn_size
) + 1);
3870 t
->insn
= (unsigned *)realloc(t
->insn
, t
->insn_size
* sizeof(t
->insn
[0]));
3871 if (t
->insn
== NULL
) {
3877 t
->insn
[t
->insn_count
++] = start
;
3881 * Map a glsl_to_tgsi constant/immediate to a TGSI immediate.
3883 static struct ureg_src
3884 emit_immediate(struct st_translate
*t
,
3885 gl_constant_value values
[4],
3888 struct ureg_program
*ureg
= t
->ureg
;
3893 return ureg_DECL_immediate(ureg
, &values
[0].f
, size
);
3895 return ureg_DECL_immediate_int(ureg
, &values
[0].i
, size
);
3896 case GL_UNSIGNED_INT
:
3898 return ureg_DECL_immediate_uint(ureg
, &values
[0].u
, size
);
3900 assert(!"should not get here - type must be float, int, uint, or bool");
3901 return ureg_src_undef();
3906 * Map a glsl_to_tgsi dst register to a TGSI ureg_dst register.
3908 static struct ureg_dst
3909 dst_register(struct st_translate
*t
,
3910 gl_register_file file
,
3914 case PROGRAM_UNDEFINED
:
3915 return ureg_dst_undef();
3917 case PROGRAM_TEMPORARY
:
3918 if (ureg_dst_is_undef(t
->temps
[index
]))
3919 t
->temps
[index
] = ureg_DECL_temporary(t
->ureg
);
3921 return t
->temps
[index
];
3923 case PROGRAM_OUTPUT
:
3924 if (t
->procType
== TGSI_PROCESSOR_VERTEX
&& index
== VERT_RESULT_PSIZ
)
3925 t
->prevInstWrotePointSize
= GL_TRUE
;
3927 if (t
->procType
== TGSI_PROCESSOR_VERTEX
)
3928 assert(index
< VERT_RESULT_MAX
);
3929 else if (t
->procType
== TGSI_PROCESSOR_FRAGMENT
)
3930 assert(index
< FRAG_RESULT_MAX
);
3932 assert(index
< GEOM_RESULT_MAX
);
3934 assert(t
->outputMapping
[index
] < Elements(t
->outputs
));
3936 return t
->outputs
[t
->outputMapping
[index
]];
3938 case PROGRAM_ADDRESS
:
3939 return t
->address
[index
];
3942 assert(!"unknown dst register file");
3943 return ureg_dst_undef();
3948 * Map a glsl_to_tgsi src register to a TGSI ureg_src register.
3950 static struct ureg_src
3951 src_register(struct st_translate
*t
,
3952 gl_register_file file
,
3956 case PROGRAM_UNDEFINED
:
3957 return ureg_src_undef();
3959 case PROGRAM_TEMPORARY
:
3961 assert(index
< Elements(t
->temps
));
3962 if (ureg_dst_is_undef(t
->temps
[index
]))
3963 t
->temps
[index
] = ureg_DECL_temporary(t
->ureg
);
3964 return ureg_src(t
->temps
[index
]);
3966 case PROGRAM_NAMED_PARAM
:
3967 case PROGRAM_ENV_PARAM
:
3968 case PROGRAM_LOCAL_PARAM
:
3969 case PROGRAM_UNIFORM
:
3971 return t
->constants
[index
];
3972 case PROGRAM_STATE_VAR
:
3973 case PROGRAM_CONSTANT
: /* ie, immediate */
3975 return ureg_DECL_constant(t
->ureg
, 0);
3977 return t
->constants
[index
];
3979 case PROGRAM_IMMEDIATE
:
3980 return t
->immediates
[index
];
3983 assert(t
->inputMapping
[index
] < Elements(t
->inputs
));
3984 return t
->inputs
[t
->inputMapping
[index
]];
3986 case PROGRAM_OUTPUT
:
3987 assert(t
->outputMapping
[index
] < Elements(t
->outputs
));
3988 return ureg_src(t
->outputs
[t
->outputMapping
[index
]]); /* not needed? */
3990 case PROGRAM_ADDRESS
:
3991 return ureg_src(t
->address
[index
]);
3993 case PROGRAM_SYSTEM_VALUE
:
3994 assert(index
< Elements(t
->systemValues
));
3995 return t
->systemValues
[index
];
3998 assert(!"unknown src register file");
3999 return ureg_src_undef();
4004 * Create a TGSI ureg_dst register from an st_dst_reg.
4006 static struct ureg_dst
4007 translate_dst(struct st_translate
*t
,
4008 const st_dst_reg
*dst_reg
,
4011 struct ureg_dst dst
= dst_register(t
,
4015 dst
= ureg_writemask(dst
, dst_reg
->writemask
);
4018 dst
= ureg_saturate(dst
);
4020 if (dst_reg
->reladdr
!= NULL
)
4021 dst
= ureg_dst_indirect(dst
, ureg_src(t
->address
[0]));
4027 * Create a TGSI ureg_src register from an st_src_reg.
4029 static struct ureg_src
4030 translate_src(struct st_translate
*t
, const st_src_reg
*src_reg
)
4032 struct ureg_src src
= src_register(t
, src_reg
->file
, src_reg
->index
);
4034 src
= ureg_swizzle(src
,
4035 GET_SWZ(src_reg
->swizzle
, 0) & 0x3,
4036 GET_SWZ(src_reg
->swizzle
, 1) & 0x3,
4037 GET_SWZ(src_reg
->swizzle
, 2) & 0x3,
4038 GET_SWZ(src_reg
->swizzle
, 3) & 0x3);
4040 if ((src_reg
->negate
& 0xf) == NEGATE_XYZW
)
4041 src
= ureg_negate(src
);
4043 if (src_reg
->reladdr
!= NULL
) {
4044 /* Normally ureg_src_indirect() would be used here, but a stupid compiler
4045 * bug in g++ makes ureg_src_indirect (an inline C function) erroneously
4046 * set the bit for src.Negate. So we have to do the operation manually
4047 * here to work around the compiler's problems. */
4048 /*src = ureg_src_indirect(src, ureg_src(t->address[0]));*/
4049 struct ureg_src addr
= ureg_src(t
->address
[0]);
4051 src
.IndirectFile
= addr
.File
;
4052 src
.IndirectIndex
= addr
.Index
;
4053 src
.IndirectSwizzle
= addr
.SwizzleX
;
4055 if (src_reg
->file
!= PROGRAM_INPUT
&&
4056 src_reg
->file
!= PROGRAM_OUTPUT
) {
4057 /* If src_reg->index was negative, it was set to zero in
4058 * src_register(). Reassign it now. But don't do this
4059 * for input/output regs since they get remapped while
4060 * const buffers don't.
4062 src
.Index
= src_reg
->index
;
4070 compile_tgsi_instruction(struct st_translate
*t
,
4071 const struct glsl_to_tgsi_instruction
*inst
)
4073 struct ureg_program
*ureg
= t
->ureg
;
4075 struct ureg_dst dst
[1];
4076 struct ureg_src src
[4];
4080 num_dst
= num_inst_dst_regs(inst
->op
);
4081 num_src
= num_inst_src_regs(inst
->op
);
4084 dst
[0] = translate_dst(t
,
4088 for (i
= 0; i
< num_src
; i
++)
4089 src
[i
] = translate_src(t
, &inst
->src
[i
]);
4092 case TGSI_OPCODE_BGNLOOP
:
4093 case TGSI_OPCODE_CAL
:
4094 case TGSI_OPCODE_ELSE
:
4095 case TGSI_OPCODE_ENDLOOP
:
4096 case TGSI_OPCODE_IF
:
4097 assert(num_dst
== 0);
4098 ureg_label_insn(ureg
,
4102 inst
->op
== TGSI_OPCODE_CAL
? inst
->function
->sig_id
: 0));
4105 case TGSI_OPCODE_TEX
:
4106 case TGSI_OPCODE_TXB
:
4107 case TGSI_OPCODE_TXD
:
4108 case TGSI_OPCODE_TXL
:
4109 case TGSI_OPCODE_TXP
:
4110 src
[num_src
++] = t
->samplers
[inst
->sampler
];
4114 translate_texture_target(inst
->tex_target
, inst
->tex_shadow
),
4118 case TGSI_OPCODE_SCS
:
4119 dst
[0] = ureg_writemask(dst
[0], TGSI_WRITEMASK_XY
);
4120 ureg_insn(ureg
, inst
->op
, dst
, num_dst
, src
, num_src
);
4133 * Emit the TGSI instructions to adjust the WPOS pixel center convention
4134 * Basically, add (adjX, adjY) to the fragment position.
4137 emit_adjusted_wpos(struct st_translate
*t
,
4138 const struct gl_program
*program
,
4139 float adjX
, float adjY
)
4141 struct ureg_program
*ureg
= t
->ureg
;
4142 struct ureg_dst wpos_temp
= ureg_DECL_temporary(ureg
);
4143 struct ureg_src wpos_input
= t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_WPOS
]];
4145 /* Note that we bias X and Y and pass Z and W through unchanged.
4146 * The shader might also use gl_FragCoord.w and .z.
4148 ureg_ADD(ureg
, wpos_temp
, wpos_input
,
4149 ureg_imm4f(ureg
, adjX
, adjY
, 0.0f
, 0.0f
));
4151 t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_WPOS
]] = ureg_src(wpos_temp
);
4156 * Emit the TGSI instructions for inverting the WPOS y coordinate.
4157 * This code is unavoidable because it also depends on whether
4158 * a FBO is bound (STATE_FB_WPOS_Y_TRANSFORM).
4161 emit_wpos_inversion(struct st_translate
*t
,
4162 const struct gl_program
*program
,
4165 struct ureg_program
*ureg
= t
->ureg
;
4167 /* Fragment program uses fragment position input.
4168 * Need to replace instances of INPUT[WPOS] with temp T
4169 * where T = INPUT[WPOS] by y is inverted.
4171 static const gl_state_index wposTransformState
[STATE_LENGTH
]
4172 = { STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
,
4173 (gl_state_index
)0, (gl_state_index
)0, (gl_state_index
)0 };
4175 /* XXX: note we are modifying the incoming shader here! Need to
4176 * do this before emitting the constant decls below, or this
4179 unsigned wposTransConst
= _mesa_add_state_reference(program
->Parameters
,
4180 wposTransformState
);
4182 struct ureg_src wpostrans
= ureg_DECL_constant(ureg
, wposTransConst
);
4183 struct ureg_dst wpos_temp
;
4184 struct ureg_src wpos_input
= t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_WPOS
]];
4186 /* MOV wpos_temp, input[wpos]
4188 if (wpos_input
.File
== TGSI_FILE_TEMPORARY
)
4189 wpos_temp
= ureg_dst(wpos_input
);
4191 wpos_temp
= ureg_DECL_temporary(ureg
);
4192 ureg_MOV(ureg
, wpos_temp
, wpos_input
);
4196 /* MAD wpos_temp.y, wpos_input, wpostrans.xxxx, wpostrans.yyyy
4199 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
4201 ureg_scalar(wpostrans
, 0),
4202 ureg_scalar(wpostrans
, 1));
4204 /* MAD wpos_temp.y, wpos_input, wpostrans.zzzz, wpostrans.wwww
4207 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
4209 ureg_scalar(wpostrans
, 2),
4210 ureg_scalar(wpostrans
, 3));
4213 /* Use wpos_temp as position input from here on:
4215 t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_WPOS
]] = ureg_src(wpos_temp
);
4220 * Emit fragment position/ooordinate code.
4223 emit_wpos(struct st_context
*st
,
4224 struct st_translate
*t
,
4225 const struct gl_program
*program
,
4226 struct ureg_program
*ureg
)
4228 const struct gl_fragment_program
*fp
=
4229 (const struct gl_fragment_program
*) program
;
4230 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
4231 boolean invert
= FALSE
;
4233 if (fp
->OriginUpperLeft
) {
4234 /* Fragment shader wants origin in upper-left */
4235 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
)) {
4236 /* the driver supports upper-left origin */
4238 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
)) {
4239 /* the driver supports lower-left origin, need to invert Y */
4240 ureg_property_fs_coord_origin(ureg
, TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
4247 /* Fragment shader wants origin in lower-left */
4248 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
))
4249 /* the driver supports lower-left origin */
4250 ureg_property_fs_coord_origin(ureg
, TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
4251 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
))
4252 /* the driver supports upper-left origin, need to invert Y */
4258 if (fp
->PixelCenterInteger
) {
4259 /* Fragment shader wants pixel center integer */
4260 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
))
4261 /* the driver supports pixel center integer */
4262 ureg_property_fs_coord_pixel_center(ureg
, TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
4263 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
))
4264 /* the driver supports pixel center half integer, need to bias X,Y */
4265 emit_adjusted_wpos(t
, program
, 0.5f
, invert
? 0.5f
: -0.5f
);
4270 /* Fragment shader wants pixel center half integer */
4271 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
)) {
4272 /* the driver supports pixel center half integer */
4274 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)) {
4275 /* the driver supports pixel center integer, need to bias X,Y */
4276 ureg_property_fs_coord_pixel_center(ureg
, TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
4277 emit_adjusted_wpos(t
, program
, 0.5f
, invert
? -0.5f
: 0.5f
);
4283 /* we invert after adjustment so that we avoid the MOV to temporary,
4284 * and reuse the adjustment ADD instead */
4285 emit_wpos_inversion(t
, program
, invert
);
4289 * OpenGL's fragment gl_FrontFace input is 1 for front-facing, 0 for back.
4290 * TGSI uses +1 for front, -1 for back.
4291 * This function converts the TGSI value to the GL value. Simply clamping/
4292 * saturating the value to [0,1] does the job.
4295 emit_face_var(struct st_translate
*t
)
4297 struct ureg_program
*ureg
= t
->ureg
;
4298 struct ureg_dst face_temp
= ureg_DECL_temporary(ureg
);
4299 struct ureg_src face_input
= t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_FACE
]];
4301 /* MOV_SAT face_temp, input[face] */
4302 face_temp
= ureg_saturate(face_temp
);
4303 ureg_MOV(ureg
, face_temp
, face_input
);
4305 /* Use face_temp as face input from here on: */
4306 t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_FACE
]] = ureg_src(face_temp
);
4310 emit_edgeflags(struct st_translate
*t
)
4312 struct ureg_program
*ureg
= t
->ureg
;
4313 struct ureg_dst edge_dst
= t
->outputs
[t
->outputMapping
[VERT_RESULT_EDGE
]];
4314 struct ureg_src edge_src
= t
->inputs
[t
->inputMapping
[VERT_ATTRIB_EDGEFLAG
]];
4316 ureg_MOV(ureg
, edge_dst
, edge_src
);
4320 * Translate intermediate IR (glsl_to_tgsi_instruction) to TGSI format.
4321 * \param program the program to translate
4322 * \param numInputs number of input registers used
4323 * \param inputMapping maps Mesa fragment program inputs to TGSI generic
4325 * \param inputSemanticName the TGSI_SEMANTIC flag for each input
4326 * \param inputSemanticIndex the semantic index (ex: which texcoord) for
4328 * \param interpMode the TGSI_INTERPOLATE_LINEAR/PERSP mode for each input
4329 * \param numOutputs number of output registers used
4330 * \param outputMapping maps Mesa fragment program outputs to TGSI
4332 * \param outputSemanticName the TGSI_SEMANTIC flag for each output
4333 * \param outputSemanticIndex the semantic index (ex: which texcoord) for
4336 * \return PIPE_OK or PIPE_ERROR_OUT_OF_MEMORY
4338 extern "C" enum pipe_error
4339 st_translate_program(
4340 struct gl_context
*ctx
,
4342 struct ureg_program
*ureg
,
4343 glsl_to_tgsi_visitor
*program
,
4344 const struct gl_program
*proginfo
,
4346 const GLuint inputMapping
[],
4347 const ubyte inputSemanticName
[],
4348 const ubyte inputSemanticIndex
[],
4349 const GLuint interpMode
[],
4351 const GLuint outputMapping
[],
4352 const ubyte outputSemanticName
[],
4353 const ubyte outputSemanticIndex
[],
4354 boolean passthrough_edgeflags
)
4356 struct st_translate translate
, *t
;
4358 enum pipe_error ret
= PIPE_OK
;
4360 assert(numInputs
<= Elements(t
->inputs
));
4361 assert(numOutputs
<= Elements(t
->outputs
));
4364 memset(t
, 0, sizeof *t
);
4366 t
->procType
= procType
;
4367 t
->inputMapping
= inputMapping
;
4368 t
->outputMapping
= outputMapping
;
4370 t
->pointSizeOutIndex
= -1;
4371 t
->prevInstWrotePointSize
= GL_FALSE
;
4374 * Declare input attributes.
4376 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
4377 for (i
= 0; i
< numInputs
; i
++) {
4378 t
->inputs
[i
] = ureg_DECL_fs_input(ureg
,
4379 inputSemanticName
[i
],
4380 inputSemanticIndex
[i
],
4384 if (proginfo
->InputsRead
& FRAG_BIT_WPOS
) {
4385 /* Must do this after setting up t->inputs, and before
4386 * emitting constant references, below:
4388 emit_wpos(st_context(ctx
), t
, proginfo
, ureg
);
4391 if (proginfo
->InputsRead
& FRAG_BIT_FACE
)
4395 * Declare output attributes.
4397 for (i
= 0; i
< numOutputs
; i
++) {
4398 switch (outputSemanticName
[i
]) {
4399 case TGSI_SEMANTIC_POSITION
:
4400 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4401 TGSI_SEMANTIC_POSITION
, /* Z/Depth */
4402 outputSemanticIndex
[i
]);
4403 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_Z
);
4405 case TGSI_SEMANTIC_STENCIL
:
4406 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4407 TGSI_SEMANTIC_STENCIL
, /* Stencil */
4408 outputSemanticIndex
[i
]);
4409 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_Y
);
4411 case TGSI_SEMANTIC_COLOR
:
4412 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4413 TGSI_SEMANTIC_COLOR
,
4414 outputSemanticIndex
[i
]);
4417 assert(!"fragment shader outputs must be POSITION/STENCIL/COLOR");
4418 return PIPE_ERROR_BAD_INPUT
;
4422 else if (procType
== TGSI_PROCESSOR_GEOMETRY
) {
4423 for (i
= 0; i
< numInputs
; i
++) {
4424 t
->inputs
[i
] = ureg_DECL_gs_input(ureg
,
4426 inputSemanticName
[i
],
4427 inputSemanticIndex
[i
]);
4430 for (i
= 0; i
< numOutputs
; i
++) {
4431 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4432 outputSemanticName
[i
],
4433 outputSemanticIndex
[i
]);
4437 assert(procType
== TGSI_PROCESSOR_VERTEX
);
4439 for (i
= 0; i
< numInputs
; i
++) {
4440 t
->inputs
[i
] = ureg_DECL_vs_input(ureg
, i
);
4443 for (i
= 0; i
< numOutputs
; i
++) {
4444 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4445 outputSemanticName
[i
],
4446 outputSemanticIndex
[i
]);
4447 if ((outputSemanticName
[i
] == TGSI_SEMANTIC_PSIZE
) && proginfo
->Id
) {
4448 /* Writing to the point size result register requires special
4449 * handling to implement clamping.
4451 static const gl_state_index pointSizeClampState
[STATE_LENGTH
]
4452 = { STATE_INTERNAL
, STATE_POINT_SIZE_IMPL_CLAMP
, (gl_state_index
)0, (gl_state_index
)0, (gl_state_index
)0 };
4453 /* XXX: note we are modifying the incoming shader here! Need to
4454 * do this before emitting the constant decls below, or this
4457 unsigned pointSizeClampConst
=
4458 _mesa_add_state_reference(proginfo
->Parameters
,
4459 pointSizeClampState
);
4460 struct ureg_dst psizregtemp
= ureg_DECL_temporary(ureg
);
4461 t
->pointSizeConst
= ureg_DECL_constant(ureg
, pointSizeClampConst
);
4462 t
->pointSizeResult
= t
->outputs
[i
];
4463 t
->pointSizeOutIndex
= i
;
4464 t
->outputs
[i
] = psizregtemp
;
4467 if (passthrough_edgeflags
)
4471 /* Declare address register.
4473 if (program
->num_address_regs
> 0) {
4474 assert(program
->num_address_regs
== 1);
4475 t
->address
[0] = ureg_DECL_address(ureg
);
4478 /* Declare misc input registers
4481 GLbitfield sysInputs
= proginfo
->SystemValuesRead
;
4482 unsigned numSys
= 0;
4483 for (i
= 0; sysInputs
; i
++) {
4484 if (sysInputs
& (1 << i
)) {
4485 unsigned semName
= mesa_sysval_to_semantic
[i
];
4486 t
->systemValues
[i
] = ureg_DECL_system_value(ureg
, numSys
, semName
, 0);
4488 sysInputs
&= ~(1 << i
);
4493 if (program
->indirect_addr_temps
) {
4494 /* If temps are accessed with indirect addressing, declare temporaries
4495 * in sequential order. Else, we declare them on demand elsewhere.
4496 * (Note: the number of temporaries is equal to program->next_temp)
4498 for (i
= 0; i
< (unsigned)program
->next_temp
; i
++) {
4499 /* XXX use TGSI_FILE_TEMPORARY_ARRAY when it's supported by ureg */
4500 t
->temps
[i
] = ureg_DECL_temporary(t
->ureg
);
4504 /* Emit constants and uniforms. TGSI uses a single index space for these,
4505 * so we put all the translated regs in t->constants.
4507 if (proginfo
->Parameters
) {
4508 t
->constants
= (struct ureg_src
*)CALLOC(proginfo
->Parameters
->NumParameters
* sizeof(t
->constants
[0]));
4509 if (t
->constants
== NULL
) {
4510 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
4514 for (i
= 0; i
< proginfo
->Parameters
->NumParameters
; i
++) {
4515 switch (proginfo
->Parameters
->Parameters
[i
].Type
) {
4516 case PROGRAM_ENV_PARAM
:
4517 case PROGRAM_LOCAL_PARAM
:
4518 case PROGRAM_STATE_VAR
:
4519 case PROGRAM_NAMED_PARAM
:
4520 case PROGRAM_UNIFORM
:
4521 t
->constants
[i
] = ureg_DECL_constant(ureg
, i
);
4524 /* Emit immediates for PROGRAM_CONSTANT only when there's no indirect
4525 * addressing of the const buffer.
4526 * FIXME: Be smarter and recognize param arrays:
4527 * indirect addressing is only valid within the referenced
4530 case PROGRAM_CONSTANT
:
4531 if (program
->indirect_addr_consts
)
4532 t
->constants
[i
] = ureg_DECL_constant(ureg
, i
);
4534 t
->constants
[i
] = emit_immediate(t
,
4535 proginfo
->Parameters
->ParameterValues
[i
],
4536 proginfo
->Parameters
->Parameters
[i
].DataType
,
4545 /* Emit immediate values.
4547 t
->immediates
= (struct ureg_src
*)CALLOC(program
->num_immediates
* sizeof(struct ureg_src
));
4548 if (t
->immediates
== NULL
) {
4549 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
4553 foreach_iter(exec_list_iterator
, iter
, program
->immediates
) {
4554 immediate_storage
*imm
= (immediate_storage
*)iter
.get();
4555 t
->immediates
[i
++] = emit_immediate(t
, imm
->values
, imm
->type
, imm
->size
);
4558 /* texture samplers */
4559 for (i
= 0; i
< ctx
->Const
.MaxTextureImageUnits
; i
++) {
4560 if (program
->samplers_used
& (1 << i
)) {
4561 t
->samplers
[i
] = ureg_DECL_sampler(ureg
, i
);
4565 /* Emit each instruction in turn:
4567 foreach_iter(exec_list_iterator
, iter
, program
->instructions
) {
4568 set_insn_start(t
, ureg_get_instruction_number(ureg
));
4569 compile_tgsi_instruction(t
, (glsl_to_tgsi_instruction
*)iter
.get());
4571 if (t
->prevInstWrotePointSize
&& proginfo
->Id
) {
4572 /* The previous instruction wrote to the (fake) vertex point size
4573 * result register. Now we need to clamp that value to the min/max
4574 * point size range, putting the result into the real point size
4576 * Note that we can't do this easily at the end of program due to
4577 * possible early return.
4579 set_insn_start(t
, ureg_get_instruction_number(ureg
));
4581 ureg_writemask(t
->outputs
[t
->pointSizeOutIndex
], WRITEMASK_X
),
4582 ureg_src(t
->outputs
[t
->pointSizeOutIndex
]),
4583 ureg_swizzle(t
->pointSizeConst
, 1,1,1,1));
4584 ureg_MIN(t
->ureg
, ureg_writemask(t
->pointSizeResult
, WRITEMASK_X
),
4585 ureg_src(t
->outputs
[t
->pointSizeOutIndex
]),
4586 ureg_swizzle(t
->pointSizeConst
, 2,2,2,2));
4588 t
->prevInstWrotePointSize
= GL_FALSE
;
4591 /* Fix up all emitted labels:
4593 for (i
= 0; i
< t
->labels_count
; i
++) {
4594 ureg_fixup_label(ureg
, t
->labels
[i
].token
,
4595 t
->insn
[t
->labels
[i
].branch_target
]);
4602 FREE(t
->immediates
);
4605 debug_printf("%s: translate error flag set\n", __FUNCTION__
);
4610 /* ----------------------------- End TGSI code ------------------------------ */
4613 * Convert a shader's GLSL IR into a Mesa gl_program, although without
4614 * generating Mesa IR.
4616 static struct gl_program
*
4617 get_mesa_program(struct gl_context
*ctx
,
4618 struct gl_shader_program
*shader_program
,
4619 struct gl_shader
*shader
)
4621 glsl_to_tgsi_visitor
* v
= new glsl_to_tgsi_visitor();
4622 struct gl_program
*prog
;
4624 const char *target_string
;
4626 struct gl_shader_compiler_options
*options
=
4627 &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(shader
->Type
)];
4629 switch (shader
->Type
) {
4630 case GL_VERTEX_SHADER
:
4631 target
= GL_VERTEX_PROGRAM_ARB
;
4632 target_string
= "vertex";
4634 case GL_FRAGMENT_SHADER
:
4635 target
= GL_FRAGMENT_PROGRAM_ARB
;
4636 target_string
= "fragment";
4638 case GL_GEOMETRY_SHADER
:
4639 target
= GL_GEOMETRY_PROGRAM_NV
;
4640 target_string
= "geometry";
4643 assert(!"should not be reached");
4647 validate_ir_tree(shader
->ir
);
4649 prog
= ctx
->Driver
.NewProgram(ctx
, target
, shader_program
->Name
);
4652 prog
->Parameters
= _mesa_new_parameter_list();
4653 prog
->Varying
= _mesa_new_parameter_list();
4654 prog
->Attributes
= _mesa_new_parameter_list();
4657 v
->shader_program
= shader_program
;
4658 v
->options
= options
;
4659 v
->glsl_version
= ctx
->Const
.GLSLVersion
;
4661 add_uniforms_to_parameters_list(shader_program
, shader
, prog
);
4663 /* Emit intermediate IR for main(). */
4664 visit_exec_list(shader
->ir
, v
);
4666 /* Now emit bodies for any functions that were used. */
4668 progress
= GL_FALSE
;
4670 foreach_iter(exec_list_iterator
, iter
, v
->function_signatures
) {
4671 function_entry
*entry
= (function_entry
*)iter
.get();
4673 if (!entry
->bgn_inst
) {
4674 v
->current_function
= entry
;
4676 entry
->bgn_inst
= v
->emit(NULL
, TGSI_OPCODE_BGNSUB
);
4677 entry
->bgn_inst
->function
= entry
;
4679 visit_exec_list(&entry
->sig
->body
, v
);
4681 glsl_to_tgsi_instruction
*last
;
4682 last
= (glsl_to_tgsi_instruction
*)v
->instructions
.get_tail();
4683 if (last
->op
!= TGSI_OPCODE_RET
)
4684 v
->emit(NULL
, TGSI_OPCODE_RET
);
4686 glsl_to_tgsi_instruction
*end
;
4687 end
= v
->emit(NULL
, TGSI_OPCODE_ENDSUB
);
4688 end
->function
= entry
;
4696 /* Print out some information (for debugging purposes) used by the
4697 * optimization passes. */
4698 for (i
=0; i
< v
->next_temp
; i
++) {
4699 int fr
= v
->get_first_temp_read(i
);
4700 int fw
= v
->get_first_temp_write(i
);
4701 int lr
= v
->get_last_temp_read(i
);
4702 int lw
= v
->get_last_temp_write(i
);
4704 printf("Temp %d: FR=%3d FW=%3d LR=%3d LW=%3d\n", i
, fr
, fw
, lr
, lw
);
4709 /* Remove reads to output registers, and to varyings in vertex shaders. */
4710 v
->remove_output_reads(PROGRAM_OUTPUT
);
4711 if (target
== GL_VERTEX_PROGRAM_ARB
)
4712 v
->remove_output_reads(PROGRAM_VARYING
);
4714 /* Perform optimizations on the instructions in the glsl_to_tgsi_visitor. */
4716 v
->copy_propagate();
4717 while (v
->eliminate_dead_code_advanced());
4719 /* FIXME: These passes to optimize temporary registers don't work when there
4720 * is indirect addressing of the temporary register space. We need proper
4721 * array support so that we don't have to give up these passes in every
4722 * shader that uses arrays.
4724 if (!v
->indirect_addr_temps
) {
4725 v
->eliminate_dead_code();
4726 v
->merge_registers();
4727 v
->renumber_registers();
4730 /* Write the END instruction. */
4731 v
->emit(NULL
, TGSI_OPCODE_END
);
4733 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
4735 printf("GLSL IR for linked %s program %d:\n", target_string
,
4736 shader_program
->Name
);
4737 _mesa_print_ir(shader
->ir
, NULL
);
4742 prog
->Instructions
= NULL
;
4743 prog
->NumInstructions
= 0;
4745 do_set_program_inouts(shader
->ir
, prog
);
4746 count_resources(v
, prog
);
4748 check_resources(ctx
, shader_program
, v
, prog
);
4750 _mesa_reference_program(ctx
, &shader
->Program
, prog
);
4752 struct st_vertex_program
*stvp
;
4753 struct st_fragment_program
*stfp
;
4754 struct st_geometry_program
*stgp
;
4756 switch (shader
->Type
) {
4757 case GL_VERTEX_SHADER
:
4758 stvp
= (struct st_vertex_program
*)prog
;
4759 stvp
->glsl_to_tgsi
= v
;
4761 case GL_FRAGMENT_SHADER
:
4762 stfp
= (struct st_fragment_program
*)prog
;
4763 stfp
->glsl_to_tgsi
= v
;
4765 case GL_GEOMETRY_SHADER
:
4766 stgp
= (struct st_geometry_program
*)prog
;
4767 stgp
->glsl_to_tgsi
= v
;
4770 assert(!"should not be reached");
4780 st_new_shader(struct gl_context
*ctx
, GLuint name
, GLuint type
)
4782 struct gl_shader
*shader
;
4783 assert(type
== GL_FRAGMENT_SHADER
|| type
== GL_VERTEX_SHADER
||
4784 type
== GL_GEOMETRY_SHADER_ARB
);
4785 shader
= rzalloc(NULL
, struct gl_shader
);
4787 shader
->Type
= type
;
4788 shader
->Name
= name
;
4789 _mesa_init_shader(ctx
, shader
);
4794 struct gl_shader_program
*
4795 st_new_shader_program(struct gl_context
*ctx
, GLuint name
)
4797 struct gl_shader_program
*shProg
;
4798 shProg
= rzalloc(NULL
, struct gl_shader_program
);
4800 shProg
->Name
= name
;
4801 _mesa_init_shader_program(ctx
, shProg
);
4808 * Called via ctx->Driver.LinkShader()
4809 * This actually involves converting GLSL IR into an intermediate TGSI-like IR
4810 * with code lowering and other optimizations.
4813 st_link_shader(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
4815 assert(prog
->LinkStatus
);
4817 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
4818 if (prog
->_LinkedShaders
[i
] == NULL
)
4822 exec_list
*ir
= prog
->_LinkedShaders
[i
]->ir
;
4823 const struct gl_shader_compiler_options
*options
=
4824 &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(prog
->_LinkedShaders
[i
]->Type
)];
4830 do_mat_op_to_vec(ir
);
4831 lower_instructions(ir
, (MOD_TO_FRACT
| DIV_TO_MUL_RCP
| EXP_TO_EXP2
4833 | ((options
->EmitNoPow
) ? POW_TO_EXP2
: 0)));
4835 progress
= do_lower_jumps(ir
, true, true, options
->EmitNoMainReturn
, options
->EmitNoCont
, options
->EmitNoLoops
) || progress
;
4837 progress
= do_common_optimization(ir
, true, options
->MaxUnrollIterations
) || progress
;
4839 progress
= lower_quadop_vector(ir
, false) || progress
;
4841 if (options
->EmitNoIfs
) {
4842 progress
= lower_discard(ir
) || progress
;
4843 progress
= lower_if_to_cond_assign(ir
) || progress
;
4846 if (options
->EmitNoNoise
)
4847 progress
= lower_noise(ir
) || progress
;
4849 /* If there are forms of indirect addressing that the driver
4850 * cannot handle, perform the lowering pass.
4852 if (options
->EmitNoIndirectInput
|| options
->EmitNoIndirectOutput
4853 || options
->EmitNoIndirectTemp
|| options
->EmitNoIndirectUniform
)
4855 lower_variable_index_to_cond_assign(ir
,
4856 options
->EmitNoIndirectInput
,
4857 options
->EmitNoIndirectOutput
,
4858 options
->EmitNoIndirectTemp
,
4859 options
->EmitNoIndirectUniform
)
4862 progress
= do_vec_index_to_cond_assign(ir
) || progress
;
4865 validate_ir_tree(ir
);
4868 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
4869 struct gl_program
*linked_prog
;
4871 if (prog
->_LinkedShaders
[i
] == NULL
)
4874 linked_prog
= get_mesa_program(ctx
, prog
, prog
->_LinkedShaders
[i
]);
4879 switch (prog
->_LinkedShaders
[i
]->Type
) {
4880 case GL_VERTEX_SHADER
:
4881 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
,
4882 (struct gl_vertex_program
*)linked_prog
);
4883 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_VERTEX_PROGRAM_ARB
,
4886 case GL_FRAGMENT_SHADER
:
4887 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
,
4888 (struct gl_fragment_program
*)linked_prog
);
4889 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
4892 case GL_GEOMETRY_SHADER
:
4893 _mesa_reference_geomprog(ctx
, &prog
->GeometryProgram
,
4894 (struct gl_geometry_program
*)linked_prog
);
4895 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_GEOMETRY_PROGRAM_NV
,
4904 _mesa_reference_program(ctx
, &linked_prog
, NULL
);
4912 * Link a GLSL shader program. Called via glLinkProgram().
4915 st_glsl_link_shader(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
4919 _mesa_clear_shader_program_data(ctx
, prog
);
4921 prog
->LinkStatus
= GL_TRUE
;
4923 for (i
= 0; i
< prog
->NumShaders
; i
++) {
4924 if (!prog
->Shaders
[i
]->CompileStatus
) {
4925 fail_link(prog
, "linking with uncompiled shader");
4926 prog
->LinkStatus
= GL_FALSE
;
4930 prog
->Varying
= _mesa_new_parameter_list();
4931 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
, NULL
);
4932 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
, NULL
);
4933 _mesa_reference_geomprog(ctx
, &prog
->GeometryProgram
, NULL
);
4935 if (prog
->LinkStatus
) {
4936 link_shaders(ctx
, prog
);
4939 if (prog
->LinkStatus
) {
4940 if (!ctx
->Driver
.LinkShader(ctx
, prog
)) {
4941 prog
->LinkStatus
= GL_FALSE
;
4945 set_uniform_initializers(ctx
, prog
);
4947 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
4948 if (!prog
->LinkStatus
) {
4949 printf("GLSL shader program %d failed to link\n", prog
->Name
);
4952 if (prog
->InfoLog
&& prog
->InfoLog
[0] != 0) {
4953 printf("GLSL shader program %d info log:\n", prog
->Name
);
4954 printf("%s\n", prog
->InfoLog
);