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
;
298 bool native_integers
;
300 variable_storage
*find_variable_storage(ir_variable
*var
);
302 int add_constant(gl_register_file file
, gl_constant_value values
[4],
303 int size
, int datatype
, GLuint
*swizzle_out
);
305 function_entry
*get_function_signature(ir_function_signature
*sig
);
307 st_src_reg
get_temp(const glsl_type
*type
);
308 void reladdr_to_temp(ir_instruction
*ir
, st_src_reg
*reg
, int *num_reladdr
);
310 st_src_reg
st_src_reg_for_float(float val
);
311 st_src_reg
st_src_reg_for_int(int val
);
312 st_src_reg
st_src_reg_for_type(int type
, int val
);
315 * \name Visit methods
317 * As typical for the visitor pattern, there must be one \c visit method for
318 * each concrete subclass of \c ir_instruction. Virtual base classes within
319 * the hierarchy should not have \c visit methods.
322 virtual void visit(ir_variable
*);
323 virtual void visit(ir_loop
*);
324 virtual void visit(ir_loop_jump
*);
325 virtual void visit(ir_function_signature
*);
326 virtual void visit(ir_function
*);
327 virtual void visit(ir_expression
*);
328 virtual void visit(ir_swizzle
*);
329 virtual void visit(ir_dereference_variable
*);
330 virtual void visit(ir_dereference_array
*);
331 virtual void visit(ir_dereference_record
*);
332 virtual void visit(ir_assignment
*);
333 virtual void visit(ir_constant
*);
334 virtual void visit(ir_call
*);
335 virtual void visit(ir_return
*);
336 virtual void visit(ir_discard
*);
337 virtual void visit(ir_texture
*);
338 virtual void visit(ir_if
*);
343 /** List of variable_storage */
346 /** List of immediate_storage */
347 exec_list immediates
;
350 /** List of function_entry */
351 exec_list function_signatures
;
352 int next_signature_id
;
354 /** List of glsl_to_tgsi_instruction */
355 exec_list instructions
;
357 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
);
359 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
,
360 st_dst_reg dst
, st_src_reg src0
);
362 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
,
363 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
);
365 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
,
367 st_src_reg src0
, st_src_reg src1
, st_src_reg src2
);
369 unsigned get_opcode(ir_instruction
*ir
, unsigned op
,
371 st_src_reg src0
, st_src_reg src1
);
374 * Emit the correct dot-product instruction for the type of arguments
376 glsl_to_tgsi_instruction
*emit_dp(ir_instruction
*ir
,
382 void emit_scalar(ir_instruction
*ir
, unsigned op
,
383 st_dst_reg dst
, st_src_reg src0
);
385 void emit_scalar(ir_instruction
*ir
, unsigned op
,
386 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
);
388 void emit_arl(ir_instruction
*ir
, st_dst_reg dst
, st_src_reg src0
);
390 void emit_scs(ir_instruction
*ir
, unsigned op
,
391 st_dst_reg dst
, const st_src_reg
&src
);
393 GLboolean
try_emit_mad(ir_expression
*ir
,
395 GLboolean
try_emit_sat(ir_expression
*ir
);
397 void emit_swz(ir_expression
*ir
);
399 bool process_move_condition(ir_rvalue
*ir
);
401 void remove_output_reads(gl_register_file type
);
402 void simplify_cmp(void);
404 void rename_temp_register(int index
, int new_index
);
405 int get_first_temp_read(int index
);
406 int get_first_temp_write(int index
);
407 int get_last_temp_read(int index
);
408 int get_last_temp_write(int index
);
410 void copy_propagate(void);
411 void eliminate_dead_code(void);
412 int eliminate_dead_code_advanced(void);
413 void merge_registers(void);
414 void renumber_registers(void);
419 static st_src_reg undef_src
= st_src_reg(PROGRAM_UNDEFINED
, 0, GLSL_TYPE_ERROR
);
421 static st_dst_reg undef_dst
= st_dst_reg(PROGRAM_UNDEFINED
, SWIZZLE_NOOP
, GLSL_TYPE_ERROR
);
423 static st_dst_reg address_reg
= st_dst_reg(PROGRAM_ADDRESS
, WRITEMASK_X
, GLSL_TYPE_FLOAT
);
426 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...) PRINTFLIKE(2, 3);
429 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...)
433 ralloc_vasprintf_append(&prog
->InfoLog
, fmt
, args
);
436 prog
->LinkStatus
= GL_FALSE
;
440 swizzle_for_size(int size
)
442 int size_swizzles
[4] = {
443 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
),
444 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
),
445 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
),
446 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
449 assert((size
>= 1) && (size
<= 4));
450 return size_swizzles
[size
- 1];
454 is_tex_instruction(unsigned opcode
)
456 const tgsi_opcode_info
* info
= tgsi_get_opcode_info(opcode
);
461 num_inst_dst_regs(unsigned opcode
)
463 const tgsi_opcode_info
* info
= tgsi_get_opcode_info(opcode
);
464 return info
->num_dst
;
468 num_inst_src_regs(unsigned opcode
)
470 const tgsi_opcode_info
* info
= tgsi_get_opcode_info(opcode
);
471 return info
->is_tex
? info
->num_src
- 1 : info
->num_src
;
474 glsl_to_tgsi_instruction
*
475 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
,
477 st_src_reg src0
, st_src_reg src1
, st_src_reg src2
)
479 glsl_to_tgsi_instruction
*inst
= new(mem_ctx
) glsl_to_tgsi_instruction();
480 int num_reladdr
= 0, i
;
482 op
= get_opcode(ir
, op
, dst
, src0
, src1
);
484 /* If we have to do relative addressing, we want to load the ARL
485 * reg directly for one of the regs, and preload the other reladdr
486 * sources into temps.
488 num_reladdr
+= dst
.reladdr
!= NULL
;
489 num_reladdr
+= src0
.reladdr
!= NULL
;
490 num_reladdr
+= src1
.reladdr
!= NULL
;
491 num_reladdr
+= src2
.reladdr
!= NULL
;
493 reladdr_to_temp(ir
, &src2
, &num_reladdr
);
494 reladdr_to_temp(ir
, &src1
, &num_reladdr
);
495 reladdr_to_temp(ir
, &src0
, &num_reladdr
);
498 emit_arl(ir
, address_reg
, *dst
.reladdr
);
501 assert(num_reladdr
== 0);
511 inst
->function
= NULL
;
513 if (op
== TGSI_OPCODE_ARL
)
514 this->num_address_regs
= 1;
516 /* Update indirect addressing status used by TGSI */
519 case PROGRAM_TEMPORARY
:
520 this->indirect_addr_temps
= true;
522 case PROGRAM_LOCAL_PARAM
:
523 case PROGRAM_ENV_PARAM
:
524 case PROGRAM_STATE_VAR
:
525 case PROGRAM_NAMED_PARAM
:
526 case PROGRAM_CONSTANT
:
527 case PROGRAM_UNIFORM
:
528 this->indirect_addr_consts
= true;
530 case PROGRAM_IMMEDIATE
:
531 assert(!"immediates should not have indirect addressing");
538 for (i
=0; i
<3; i
++) {
539 if(inst
->src
[i
].reladdr
) {
540 switch(inst
->src
[i
].file
) {
541 case PROGRAM_TEMPORARY
:
542 this->indirect_addr_temps
= true;
544 case PROGRAM_LOCAL_PARAM
:
545 case PROGRAM_ENV_PARAM
:
546 case PROGRAM_STATE_VAR
:
547 case PROGRAM_NAMED_PARAM
:
548 case PROGRAM_CONSTANT
:
549 case PROGRAM_UNIFORM
:
550 this->indirect_addr_consts
= true;
552 case PROGRAM_IMMEDIATE
:
553 assert(!"immediates should not have indirect addressing");
562 this->instructions
.push_tail(inst
);
568 glsl_to_tgsi_instruction
*
569 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
,
570 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
)
572 return emit(ir
, op
, dst
, src0
, src1
, undef_src
);
575 glsl_to_tgsi_instruction
*
576 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
,
577 st_dst_reg dst
, st_src_reg src0
)
579 assert(dst
.writemask
!= 0);
580 return emit(ir
, op
, dst
, src0
, undef_src
, undef_src
);
583 glsl_to_tgsi_instruction
*
584 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
)
586 return emit(ir
, op
, undef_dst
, undef_src
, undef_src
, undef_src
);
590 * Determines whether to use an integer, unsigned integer, or float opcode
591 * based on the operands and input opcode, then emits the result.
593 * TODO: type checking for remaining TGSI opcodes
596 glsl_to_tgsi_visitor::get_opcode(ir_instruction
*ir
, unsigned op
,
598 st_src_reg src0
, st_src_reg src1
)
600 int type
= GLSL_TYPE_FLOAT
;
602 if (src0
.type
== GLSL_TYPE_FLOAT
|| src1
.type
== GLSL_TYPE_FLOAT
)
603 type
= GLSL_TYPE_FLOAT
;
604 else if (native_integers
)
607 #define case4(c, f, i, u) \
608 case TGSI_OPCODE_##c: \
609 if (type == GLSL_TYPE_INT) op = TGSI_OPCODE_##i; \
610 else if (type == GLSL_TYPE_UINT) op = TGSI_OPCODE_##u; \
611 else op = TGSI_OPCODE_##f; \
613 #define case3(f, i, u) case4(f, f, i, u)
614 #define case2fi(f, i) case4(f, f, i, i)
615 #define case2iu(i, u) case4(i, LAST, i, u)
621 case3(DIV
, IDIV
, UDIV
);
622 case3(MAX
, IMAX
, UMAX
);
623 case3(MIN
, IMIN
, UMIN
);
628 case3(SGE
, ISGE
, USGE
);
629 case3(SLT
, ISLT
, USLT
);
641 assert(op
!= TGSI_OPCODE_LAST
);
645 glsl_to_tgsi_instruction
*
646 glsl_to_tgsi_visitor::emit_dp(ir_instruction
*ir
,
647 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
,
650 static const unsigned dot_opcodes
[] = {
651 TGSI_OPCODE_DP2
, TGSI_OPCODE_DP3
, TGSI_OPCODE_DP4
654 return emit(ir
, dot_opcodes
[elements
- 2], dst
, src0
, src1
);
658 * Emits TGSI scalar opcodes to produce unique answers across channels.
660 * Some TGSI opcodes are scalar-only, like ARB_fp/vp. The src X
661 * channel determines the result across all channels. So to do a vec4
662 * of this operation, we want to emit a scalar per source channel used
663 * to produce dest channels.
666 glsl_to_tgsi_visitor::emit_scalar(ir_instruction
*ir
, unsigned op
,
668 st_src_reg orig_src0
, st_src_reg orig_src1
)
671 int done_mask
= ~dst
.writemask
;
673 /* TGSI RCP is a scalar operation splatting results to all channels,
674 * like ARB_fp/vp. So emit as many RCPs as necessary to cover our
677 for (i
= 0; i
< 4; i
++) {
678 GLuint this_mask
= (1 << i
);
679 glsl_to_tgsi_instruction
*inst
;
680 st_src_reg src0
= orig_src0
;
681 st_src_reg src1
= orig_src1
;
683 if (done_mask
& this_mask
)
686 GLuint src0_swiz
= GET_SWZ(src0
.swizzle
, i
);
687 GLuint src1_swiz
= GET_SWZ(src1
.swizzle
, i
);
688 for (j
= i
+ 1; j
< 4; j
++) {
689 /* If there is another enabled component in the destination that is
690 * derived from the same inputs, generate its value on this pass as
693 if (!(done_mask
& (1 << j
)) &&
694 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
&&
695 GET_SWZ(src1
.swizzle
, j
) == src1_swiz
) {
696 this_mask
|= (1 << j
);
699 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
700 src0_swiz
, src0_swiz
);
701 src1
.swizzle
= MAKE_SWIZZLE4(src1_swiz
, src1_swiz
,
702 src1_swiz
, src1_swiz
);
704 inst
= emit(ir
, op
, dst
, src0
, src1
);
705 inst
->dst
.writemask
= this_mask
;
706 done_mask
|= this_mask
;
711 glsl_to_tgsi_visitor::emit_scalar(ir_instruction
*ir
, unsigned op
,
712 st_dst_reg dst
, st_src_reg src0
)
714 st_src_reg undef
= undef_src
;
716 undef
.swizzle
= SWIZZLE_XXXX
;
718 emit_scalar(ir
, op
, dst
, src0
, undef
);
722 glsl_to_tgsi_visitor::emit_arl(ir_instruction
*ir
,
723 st_dst_reg dst
, st_src_reg src0
)
725 st_src_reg tmp
= get_temp(glsl_type::float_type
);
727 if (src0
.type
== GLSL_TYPE_INT
)
728 emit(NULL
, TGSI_OPCODE_I2F
, st_dst_reg(tmp
), src0
);
729 else if (src0
.type
== GLSL_TYPE_UINT
)
730 emit(NULL
, TGSI_OPCODE_U2F
, st_dst_reg(tmp
), src0
);
734 emit(NULL
, TGSI_OPCODE_ARL
, dst
, tmp
);
738 * Emit an TGSI_OPCODE_SCS instruction
740 * The \c SCS opcode functions a bit differently than the other TGSI opcodes.
741 * Instead of splatting its result across all four components of the
742 * destination, it writes one value to the \c x component and another value to
743 * the \c y component.
745 * \param ir IR instruction being processed
746 * \param op Either \c TGSI_OPCODE_SIN or \c TGSI_OPCODE_COS depending
747 * on which value is desired.
748 * \param dst Destination register
749 * \param src Source register
752 glsl_to_tgsi_visitor::emit_scs(ir_instruction
*ir
, unsigned op
,
754 const st_src_reg
&src
)
756 /* Vertex programs cannot use the SCS opcode.
758 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
759 emit_scalar(ir
, op
, dst
, src
);
763 const unsigned component
= (op
== TGSI_OPCODE_SIN
) ? 0 : 1;
764 const unsigned scs_mask
= (1U << component
);
765 int done_mask
= ~dst
.writemask
;
768 assert(op
== TGSI_OPCODE_SIN
|| op
== TGSI_OPCODE_COS
);
770 /* If there are compnents in the destination that differ from the component
771 * that will be written by the SCS instrution, we'll need a temporary.
773 if (scs_mask
!= unsigned(dst
.writemask
)) {
774 tmp
= get_temp(glsl_type::vec4_type
);
777 for (unsigned i
= 0; i
< 4; i
++) {
778 unsigned this_mask
= (1U << i
);
779 st_src_reg src0
= src
;
781 if ((done_mask
& this_mask
) != 0)
784 /* The source swizzle specified which component of the source generates
785 * sine / cosine for the current component in the destination. The SCS
786 * instruction requires that this value be swizzle to the X component.
787 * Replace the current swizzle with a swizzle that puts the source in
790 unsigned src0_swiz
= GET_SWZ(src
.swizzle
, i
);
792 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
793 src0_swiz
, src0_swiz
);
794 for (unsigned j
= i
+ 1; j
< 4; j
++) {
795 /* If there is another enabled component in the destination that is
796 * derived from the same inputs, generate its value on this pass as
799 if (!(done_mask
& (1 << j
)) &&
800 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
) {
801 this_mask
|= (1 << j
);
805 if (this_mask
!= scs_mask
) {
806 glsl_to_tgsi_instruction
*inst
;
807 st_dst_reg tmp_dst
= st_dst_reg(tmp
);
809 /* Emit the SCS instruction.
811 inst
= emit(ir
, TGSI_OPCODE_SCS
, tmp_dst
, src0
);
812 inst
->dst
.writemask
= scs_mask
;
814 /* Move the result of the SCS instruction to the desired location in
817 tmp
.swizzle
= MAKE_SWIZZLE4(component
, component
,
818 component
, component
);
819 inst
= emit(ir
, TGSI_OPCODE_SCS
, dst
, tmp
);
820 inst
->dst
.writemask
= this_mask
;
822 /* Emit the SCS instruction to write directly to the destination.
824 glsl_to_tgsi_instruction
*inst
= emit(ir
, TGSI_OPCODE_SCS
, dst
, src0
);
825 inst
->dst
.writemask
= scs_mask
;
828 done_mask
|= this_mask
;
833 glsl_to_tgsi_visitor::add_constant(gl_register_file file
,
834 gl_constant_value values
[4], int size
, int datatype
,
837 if (file
== PROGRAM_CONSTANT
) {
838 return _mesa_add_typed_unnamed_constant(this->prog
->Parameters
, values
,
839 size
, datatype
, swizzle_out
);
842 immediate_storage
*entry
;
843 assert(file
== PROGRAM_IMMEDIATE
);
845 /* Search immediate storage to see if we already have an identical
846 * immediate that we can use instead of adding a duplicate entry.
848 foreach_iter(exec_list_iterator
, iter
, this->immediates
) {
849 entry
= (immediate_storage
*)iter
.get();
851 if (entry
->size
== size
&&
852 entry
->type
== datatype
&&
853 !memcmp(entry
->values
, values
, size
* sizeof(gl_constant_value
))) {
859 /* Add this immediate to the list. */
860 entry
= new(mem_ctx
) immediate_storage(values
, size
, datatype
);
861 this->immediates
.push_tail(entry
);
862 this->num_immediates
++;
868 glsl_to_tgsi_visitor::st_src_reg_for_float(float val
)
870 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_FLOAT
);
871 union gl_constant_value uval
;
874 src
.index
= add_constant(src
.file
, &uval
, 1, GL_FLOAT
, &src
.swizzle
);
880 glsl_to_tgsi_visitor::st_src_reg_for_int(int val
)
882 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_INT
);
883 union gl_constant_value uval
;
885 assert(native_integers
);
888 src
.index
= add_constant(src
.file
, &uval
, 1, GL_INT
, &src
.swizzle
);
894 glsl_to_tgsi_visitor::st_src_reg_for_type(int type
, int val
)
897 return type
== GLSL_TYPE_FLOAT
? st_src_reg_for_float(val
) :
898 st_src_reg_for_int(val
);
900 return st_src_reg_for_float(val
);
904 type_size(const struct glsl_type
*type
)
909 switch (type
->base_type
) {
912 case GLSL_TYPE_FLOAT
:
914 if (type
->is_matrix()) {
915 return type
->matrix_columns
;
917 /* Regardless of size of vector, it gets a vec4. This is bad
918 * packing for things like floats, but otherwise arrays become a
919 * mess. Hopefully a later pass over the code can pack scalars
920 * down if appropriate.
924 case GLSL_TYPE_ARRAY
:
925 assert(type
->length
> 0);
926 return type_size(type
->fields
.array
) * type
->length
;
927 case GLSL_TYPE_STRUCT
:
929 for (i
= 0; i
< type
->length
; i
++) {
930 size
+= type_size(type
->fields
.structure
[i
].type
);
933 case GLSL_TYPE_SAMPLER
:
934 /* Samplers take up one slot in UNIFORMS[], but they're baked in
945 * In the initial pass of codegen, we assign temporary numbers to
946 * intermediate results. (not SSA -- variable assignments will reuse
950 glsl_to_tgsi_visitor::get_temp(const glsl_type
*type
)
954 src
.type
= native_integers
? type
->base_type
: GLSL_TYPE_FLOAT
;
955 src
.file
= PROGRAM_TEMPORARY
;
956 src
.index
= next_temp
;
958 next_temp
+= type_size(type
);
960 if (type
->is_array() || type
->is_record()) {
961 src
.swizzle
= SWIZZLE_NOOP
;
963 src
.swizzle
= swizzle_for_size(type
->vector_elements
);
971 glsl_to_tgsi_visitor::find_variable_storage(ir_variable
*var
)
974 variable_storage
*entry
;
976 foreach_iter(exec_list_iterator
, iter
, this->variables
) {
977 entry
= (variable_storage
*)iter
.get();
979 if (entry
->var
== var
)
987 glsl_to_tgsi_visitor::visit(ir_variable
*ir
)
989 if (strcmp(ir
->name
, "gl_FragCoord") == 0) {
990 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
992 fp
->OriginUpperLeft
= ir
->origin_upper_left
;
993 fp
->PixelCenterInteger
= ir
->pixel_center_integer
;
995 } else if (strcmp(ir
->name
, "gl_FragDepth") == 0) {
996 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
997 switch (ir
->depth_layout
) {
998 case ir_depth_layout_none
:
999 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_NONE
;
1001 case ir_depth_layout_any
:
1002 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_ANY
;
1004 case ir_depth_layout_greater
:
1005 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_GREATER
;
1007 case ir_depth_layout_less
:
1008 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_LESS
;
1010 case ir_depth_layout_unchanged
:
1011 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
1019 if (ir
->mode
== ir_var_uniform
&& strncmp(ir
->name
, "gl_", 3) == 0) {
1021 const ir_state_slot
*const slots
= ir
->state_slots
;
1022 assert(ir
->state_slots
!= NULL
);
1024 /* Check if this statevar's setup in the STATE file exactly
1025 * matches how we'll want to reference it as a
1026 * struct/array/whatever. If not, then we need to move it into
1027 * temporary storage and hope that it'll get copy-propagated
1030 for (i
= 0; i
< ir
->num_state_slots
; i
++) {
1031 if (slots
[i
].swizzle
!= SWIZZLE_XYZW
) {
1036 struct variable_storage
*storage
;
1038 if (i
== ir
->num_state_slots
) {
1039 /* We'll set the index later. */
1040 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_STATE_VAR
, -1);
1041 this->variables
.push_tail(storage
);
1045 /* The variable_storage constructor allocates slots based on the size
1046 * of the type. However, this had better match the number of state
1047 * elements that we're going to copy into the new temporary.
1049 assert((int) ir
->num_state_slots
== type_size(ir
->type
));
1051 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_TEMPORARY
,
1053 this->variables
.push_tail(storage
);
1054 this->next_temp
+= type_size(ir
->type
);
1056 dst
= st_dst_reg(st_src_reg(PROGRAM_TEMPORARY
, storage
->index
,
1057 native_integers
? ir
->type
->base_type
: GLSL_TYPE_FLOAT
));
1061 for (unsigned int i
= 0; i
< ir
->num_state_slots
; i
++) {
1062 int index
= _mesa_add_state_reference(this->prog
->Parameters
,
1063 (gl_state_index
*)slots
[i
].tokens
);
1065 if (storage
->file
== PROGRAM_STATE_VAR
) {
1066 if (storage
->index
== -1) {
1067 storage
->index
= index
;
1069 assert(index
== storage
->index
+ (int)i
);
1072 st_src_reg
src(PROGRAM_STATE_VAR
, index
,
1073 native_integers
? ir
->type
->base_type
: GLSL_TYPE_FLOAT
);
1074 src
.swizzle
= slots
[i
].swizzle
;
1075 emit(ir
, TGSI_OPCODE_MOV
, dst
, src
);
1076 /* even a float takes up a whole vec4 reg in a struct/array. */
1081 if (storage
->file
== PROGRAM_TEMPORARY
&&
1082 dst
.index
!= storage
->index
+ (int) ir
->num_state_slots
) {
1083 fail_link(this->shader_program
,
1084 "failed to load builtin uniform `%s' (%d/%d regs loaded)\n",
1085 ir
->name
, dst
.index
- storage
->index
,
1086 type_size(ir
->type
));
1092 glsl_to_tgsi_visitor::visit(ir_loop
*ir
)
1094 ir_dereference_variable
*counter
= NULL
;
1096 if (ir
->counter
!= NULL
)
1097 counter
= new(ir
) ir_dereference_variable(ir
->counter
);
1099 if (ir
->from
!= NULL
) {
1100 assert(ir
->counter
!= NULL
);
1102 ir_assignment
*a
= new(ir
) ir_assignment(counter
, ir
->from
, NULL
);
1108 emit(NULL
, TGSI_OPCODE_BGNLOOP
);
1112 new(ir
) ir_expression(ir
->cmp
, glsl_type::bool_type
,
1114 ir_if
*if_stmt
= new(ir
) ir_if(e
);
1116 ir_loop_jump
*brk
= new(ir
) ir_loop_jump(ir_loop_jump::jump_break
);
1118 if_stmt
->then_instructions
.push_tail(brk
);
1120 if_stmt
->accept(this);
1127 visit_exec_list(&ir
->body_instructions
, this);
1129 if (ir
->increment
) {
1131 new(ir
) ir_expression(ir_binop_add
, counter
->type
,
1132 counter
, ir
->increment
);
1134 ir_assignment
*a
= new(ir
) ir_assignment(counter
, e
, NULL
);
1141 emit(NULL
, TGSI_OPCODE_ENDLOOP
);
1145 glsl_to_tgsi_visitor::visit(ir_loop_jump
*ir
)
1148 case ir_loop_jump::jump_break
:
1149 emit(NULL
, TGSI_OPCODE_BRK
);
1151 case ir_loop_jump::jump_continue
:
1152 emit(NULL
, TGSI_OPCODE_CONT
);
1159 glsl_to_tgsi_visitor::visit(ir_function_signature
*ir
)
1166 glsl_to_tgsi_visitor::visit(ir_function
*ir
)
1168 /* Ignore function bodies other than main() -- we shouldn't see calls to
1169 * them since they should all be inlined before we get to glsl_to_tgsi.
1171 if (strcmp(ir
->name
, "main") == 0) {
1172 const ir_function_signature
*sig
;
1175 sig
= ir
->matching_signature(&empty
);
1179 foreach_iter(exec_list_iterator
, iter
, sig
->body
) {
1180 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
1188 glsl_to_tgsi_visitor::try_emit_mad(ir_expression
*ir
, int mul_operand
)
1190 int nonmul_operand
= 1 - mul_operand
;
1192 st_dst_reg result_dst
;
1194 ir_expression
*expr
= ir
->operands
[mul_operand
]->as_expression();
1195 if (!expr
|| expr
->operation
!= ir_binop_mul
)
1198 expr
->operands
[0]->accept(this);
1200 expr
->operands
[1]->accept(this);
1202 ir
->operands
[nonmul_operand
]->accept(this);
1205 this->result
= get_temp(ir
->type
);
1206 result_dst
= st_dst_reg(this->result
);
1207 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1208 emit(ir
, TGSI_OPCODE_MAD
, result_dst
, a
, b
, c
);
1214 glsl_to_tgsi_visitor::try_emit_sat(ir_expression
*ir
)
1216 /* Saturates were only introduced to vertex programs in
1217 * NV_vertex_program3, so don't give them to drivers in the VP.
1219 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
)
1222 ir_rvalue
*sat_src
= ir
->as_rvalue_to_saturate();
1226 sat_src
->accept(this);
1227 st_src_reg src
= this->result
;
1229 /* If we generated an expression instruction into a temporary in
1230 * processing the saturate's operand, apply the saturate to that
1231 * instruction. Otherwise, generate a MOV to do the saturate.
1233 * Note that we have to be careful to only do this optimization if
1234 * the instruction in question was what generated src->result. For
1235 * example, ir_dereference_array might generate a MUL instruction
1236 * to create the reladdr, and return us a src reg using that
1237 * reladdr. That MUL result is not the value we're trying to
1240 ir_expression
*sat_src_expr
= sat_src
->as_expression();
1241 if (sat_src_expr
&& (sat_src_expr
->operation
== ir_binop_mul
||
1242 sat_src_expr
->operation
== ir_binop_add
||
1243 sat_src_expr
->operation
== ir_binop_dot
)) {
1244 glsl_to_tgsi_instruction
*new_inst
;
1245 new_inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
1246 new_inst
->saturate
= true;
1248 this->result
= get_temp(ir
->type
);
1249 st_dst_reg result_dst
= st_dst_reg(this->result
);
1250 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1251 glsl_to_tgsi_instruction
*inst
;
1252 inst
= emit(ir
, TGSI_OPCODE_MOV
, result_dst
, src
);
1253 inst
->saturate
= true;
1260 glsl_to_tgsi_visitor::reladdr_to_temp(ir_instruction
*ir
,
1261 st_src_reg
*reg
, int *num_reladdr
)
1266 emit_arl(ir
, address_reg
, *reg
->reladdr
);
1268 if (*num_reladdr
!= 1) {
1269 st_src_reg temp
= get_temp(glsl_type::vec4_type
);
1271 emit(ir
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), *reg
);
1279 glsl_to_tgsi_visitor::visit(ir_expression
*ir
)
1281 unsigned int operand
;
1282 st_src_reg op
[Elements(ir
->operands
)];
1283 st_src_reg result_src
;
1284 st_dst_reg result_dst
;
1286 /* Quick peephole: Emit MAD(a, b, c) instead of ADD(MUL(a, b), c)
1288 if (ir
->operation
== ir_binop_add
) {
1289 if (try_emit_mad(ir
, 1))
1291 if (try_emit_mad(ir
, 0))
1294 if (try_emit_sat(ir
))
1297 if (ir
->operation
== ir_quadop_vector
)
1298 assert(!"ir_quadop_vector should have been lowered");
1300 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
1301 this->result
.file
= PROGRAM_UNDEFINED
;
1302 ir
->operands
[operand
]->accept(this);
1303 if (this->result
.file
== PROGRAM_UNDEFINED
) {
1305 printf("Failed to get tree for expression operand:\n");
1306 ir
->operands
[operand
]->accept(&v
);
1309 op
[operand
] = this->result
;
1311 /* Matrix expression operands should have been broken down to vector
1312 * operations already.
1314 assert(!ir
->operands
[operand
]->type
->is_matrix());
1317 int vector_elements
= ir
->operands
[0]->type
->vector_elements
;
1318 if (ir
->operands
[1]) {
1319 vector_elements
= MAX2(vector_elements
,
1320 ir
->operands
[1]->type
->vector_elements
);
1323 this->result
.file
= PROGRAM_UNDEFINED
;
1325 /* Storage for our result. Ideally for an assignment we'd be using
1326 * the actual storage for the result here, instead.
1328 result_src
= get_temp(ir
->type
);
1329 /* convenience for the emit functions below. */
1330 result_dst
= st_dst_reg(result_src
);
1331 /* Limit writes to the channels that will be used by result_src later.
1332 * This does limit this temp's use as a temporary for multi-instruction
1335 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1337 switch (ir
->operation
) {
1338 case ir_unop_logic_not
:
1339 if (result_dst
.type
!= GLSL_TYPE_FLOAT
)
1340 emit(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], st_src_reg_for_type(result_dst
.type
, 0));
1342 /* Previously 'SEQ dst, src, 0.0' was used for this. However, many
1343 * older GPUs implement SEQ using multiple instructions (i915 uses two
1344 * SGE instructions and a MUL instruction). Since our logic values are
1345 * 0.0 and 1.0, 1-x also implements !x.
1347 op
[0].negate
= ~op
[0].negate
;
1348 emit(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], st_src_reg_for_float(1.0));
1352 assert(result_dst
.type
== GLSL_TYPE_FLOAT
|| result_dst
.type
== GLSL_TYPE_INT
);
1353 if (result_dst
.type
== GLSL_TYPE_INT
)
1354 emit(ir
, TGSI_OPCODE_INEG
, result_dst
, op
[0]);
1356 op
[0].negate
= ~op
[0].negate
;
1361 assert(result_dst
.type
== GLSL_TYPE_FLOAT
);
1362 emit(ir
, TGSI_OPCODE_ABS
, result_dst
, op
[0]);
1365 emit(ir
, TGSI_OPCODE_SSG
, result_dst
, op
[0]);
1368 emit_scalar(ir
, TGSI_OPCODE_RCP
, result_dst
, op
[0]);
1372 emit_scalar(ir
, TGSI_OPCODE_EX2
, result_dst
, op
[0]);
1376 assert(!"not reached: should be handled by ir_explog_to_explog2");
1379 emit_scalar(ir
, TGSI_OPCODE_LG2
, result_dst
, op
[0]);
1382 emit_scalar(ir
, TGSI_OPCODE_SIN
, result_dst
, op
[0]);
1385 emit_scalar(ir
, TGSI_OPCODE_COS
, result_dst
, op
[0]);
1387 case ir_unop_sin_reduced
:
1388 emit_scs(ir
, TGSI_OPCODE_SIN
, result_dst
, op
[0]);
1390 case ir_unop_cos_reduced
:
1391 emit_scs(ir
, TGSI_OPCODE_COS
, result_dst
, op
[0]);
1395 emit(ir
, TGSI_OPCODE_DDX
, result_dst
, op
[0]);
1398 op
[0].negate
= ~op
[0].negate
;
1399 emit(ir
, TGSI_OPCODE_DDY
, result_dst
, op
[0]);
1402 case ir_unop_noise
: {
1403 /* At some point, a motivated person could add a better
1404 * implementation of noise. Currently not even the nvidia
1405 * binary drivers do anything more than this. In any case, the
1406 * place to do this is in the GL state tracker, not the poor
1409 emit(ir
, TGSI_OPCODE_MOV
, result_dst
, st_src_reg_for_float(0.5));
1414 emit(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1417 emit(ir
, TGSI_OPCODE_SUB
, result_dst
, op
[0], op
[1]);
1421 emit(ir
, TGSI_OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1424 if (result_dst
.type
== GLSL_TYPE_FLOAT
)
1425 assert(!"not reached: should be handled by ir_div_to_mul_rcp");
1427 emit(ir
, TGSI_OPCODE_DIV
, result_dst
, op
[0], op
[1]);
1430 if (result_dst
.type
== GLSL_TYPE_FLOAT
)
1431 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
1433 emit(ir
, TGSI_OPCODE_MOD
, result_dst
, op
[0], op
[1]);
1437 emit(ir
, TGSI_OPCODE_SLT
, result_dst
, op
[0], op
[1]);
1439 case ir_binop_greater
:
1440 emit(ir
, TGSI_OPCODE_SGT
, result_dst
, op
[0], op
[1]);
1442 case ir_binop_lequal
:
1443 emit(ir
, TGSI_OPCODE_SLE
, result_dst
, op
[0], op
[1]);
1445 case ir_binop_gequal
:
1446 emit(ir
, TGSI_OPCODE_SGE
, result_dst
, op
[0], op
[1]);
1448 case ir_binop_equal
:
1449 emit(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1451 case ir_binop_nequal
:
1452 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1454 case ir_binop_all_equal
:
1455 /* "==" operator producing a scalar boolean. */
1456 if (ir
->operands
[0]->type
->is_vector() ||
1457 ir
->operands
[1]->type
->is_vector()) {
1458 st_src_reg temp
= get_temp(native_integers
?
1459 glsl_type::get_instance(ir
->operands
[0]->type
->base_type
, 4, 1) :
1460 glsl_type::vec4_type
);
1461 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
1462 emit(ir
, TGSI_OPCODE_SNE
, st_dst_reg(temp
), op
[0], op
[1]);
1463 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1464 emit(ir
, TGSI_OPCODE_SEQ
, result_dst
, result_src
, st_src_reg_for_float(0.0));
1466 emit(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1469 case ir_binop_any_nequal
:
1470 /* "!=" operator producing a scalar boolean. */
1471 if (ir
->operands
[0]->type
->is_vector() ||
1472 ir
->operands
[1]->type
->is_vector()) {
1473 st_src_reg temp
= get_temp(native_integers
?
1474 glsl_type::get_instance(ir
->operands
[0]->type
->base_type
, 4, 1) :
1475 glsl_type::vec4_type
);
1476 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
1477 emit(ir
, TGSI_OPCODE_SNE
, st_dst_reg(temp
), op
[0], op
[1]);
1478 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1479 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, result_src
, st_src_reg_for_float(0.0));
1481 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1486 assert(ir
->operands
[0]->type
->is_vector());
1488 /* After the dot-product, the value will be an integer on the
1489 * range [0,4]. Zero stays zero, and positive values become 1.0.
1491 glsl_to_tgsi_instruction
*const dp
=
1492 emit_dp(ir
, result_dst
, op
[0], op
[0],
1493 ir
->operands
[0]->type
->vector_elements
);
1494 if (this->prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
&&
1495 result_dst
.type
== GLSL_TYPE_FLOAT
) {
1496 /* The clamping to [0,1] can be done for free in the fragment
1497 * shader with a saturate.
1499 dp
->saturate
= true;
1500 } else if (result_dst
.type
== GLSL_TYPE_FLOAT
) {
1501 /* Negating the result of the dot-product gives values on the range
1502 * [-4, 0]. Zero stays zero, and negative values become 1.0. This
1503 * is achieved using SLT.
1505 st_src_reg slt_src
= result_src
;
1506 slt_src
.negate
= ~slt_src
.negate
;
1507 emit(ir
, TGSI_OPCODE_SLT
, result_dst
, slt_src
, st_src_reg_for_float(0.0));
1510 /* Use SNE 0 if integers are being used as boolean values. */
1511 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, result_src
, st_src_reg_for_int(0));
1516 case ir_binop_logic_xor
:
1517 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1520 case ir_binop_logic_or
: {
1521 /* After the addition, the value will be an integer on the
1522 * range [0,2]. Zero stays zero, and positive values become 1.0.
1524 glsl_to_tgsi_instruction
*add
=
1525 emit(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1526 if (this->prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
&&
1527 result_dst
.type
== GLSL_TYPE_FLOAT
) {
1528 /* The clamping to [0,1] can be done for free in the fragment
1529 * shader with a saturate if floats are being used as boolean values.
1531 add
->saturate
= true;
1532 } else if (result_dst
.type
== GLSL_TYPE_FLOAT
) {
1533 /* Negating the result of the addition gives values on the range
1534 * [-2, 0]. Zero stays zero, and negative values become 1.0. This
1535 * is achieved using SLT.
1537 st_src_reg slt_src
= result_src
;
1538 slt_src
.negate
= ~slt_src
.negate
;
1539 emit(ir
, TGSI_OPCODE_SLT
, result_dst
, slt_src
, st_src_reg_for_float(0.0));
1541 /* Use an SNE on the result of the addition. Zero stays zero,
1542 * 1 stays 1, and 2 becomes 1.
1544 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, result_src
, st_src_reg_for_int(0));
1549 case ir_binop_logic_and
:
1550 /* the bool args are stored as float 0.0 or 1.0, so "mul" gives us "and". */
1551 emit(ir
, TGSI_OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1555 assert(ir
->operands
[0]->type
->is_vector());
1556 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
1557 emit_dp(ir
, result_dst
, op
[0], op
[1],
1558 ir
->operands
[0]->type
->vector_elements
);
1562 /* sqrt(x) = x * rsq(x). */
1563 emit_scalar(ir
, TGSI_OPCODE_RSQ
, result_dst
, op
[0]);
1564 emit(ir
, TGSI_OPCODE_MUL
, result_dst
, result_src
, op
[0]);
1565 /* For incoming channels <= 0, set the result to 0. */
1566 op
[0].negate
= ~op
[0].negate
;
1567 emit(ir
, TGSI_OPCODE_CMP
, result_dst
,
1568 op
[0], result_src
, st_src_reg_for_float(0.0));
1571 emit_scalar(ir
, TGSI_OPCODE_RSQ
, result_dst
, op
[0]);
1575 if (native_integers
) {
1576 emit(ir
, TGSI_OPCODE_I2F
, result_dst
, op
[0]);
1581 /* Converting between signed and unsigned integers is a no-op. */
1583 /* Booleans are stored as integers (or floats in GLSL 1.20 and lower). */
1587 if (native_integers
)
1588 emit(ir
, TGSI_OPCODE_F2I
, result_dst
, op
[0]);
1590 emit(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
1594 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0],
1595 st_src_reg_for_type(result_dst
.type
, 0));
1598 emit(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
1601 op
[0].negate
= ~op
[0].negate
;
1602 emit(ir
, TGSI_OPCODE_FLR
, result_dst
, op
[0]);
1603 result_src
.negate
= ~result_src
.negate
;
1606 emit(ir
, TGSI_OPCODE_FLR
, result_dst
, op
[0]);
1609 emit(ir
, TGSI_OPCODE_FRC
, result_dst
, op
[0]);
1613 emit(ir
, TGSI_OPCODE_MIN
, result_dst
, op
[0], op
[1]);
1616 emit(ir
, TGSI_OPCODE_MAX
, result_dst
, op
[0], op
[1]);
1619 emit_scalar(ir
, TGSI_OPCODE_POW
, result_dst
, op
[0], op
[1]);
1622 case ir_unop_bit_not
:
1623 if (glsl_version
>= 130) {
1624 emit(ir
, TGSI_OPCODE_NOT
, result_dst
, op
[0]);
1628 if (native_integers
) {
1629 emit(ir
, TGSI_OPCODE_U2F
, result_dst
, op
[0]);
1632 case ir_binop_lshift
:
1633 if (glsl_version
>= 130) {
1634 emit(ir
, TGSI_OPCODE_SHL
, result_dst
, op
[0]);
1637 case ir_binop_rshift
:
1638 if (glsl_version
>= 130) {
1639 emit(ir
, TGSI_OPCODE_ISHR
, result_dst
, op
[0]);
1642 case ir_binop_bit_and
:
1643 if (glsl_version
>= 130) {
1644 emit(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0]);
1647 case ir_binop_bit_xor
:
1648 if (glsl_version
>= 130) {
1649 emit(ir
, TGSI_OPCODE_XOR
, result_dst
, op
[0]);
1652 case ir_binop_bit_or
:
1653 if (glsl_version
>= 130) {
1654 emit(ir
, TGSI_OPCODE_OR
, result_dst
, op
[0]);
1657 case ir_unop_round_even
:
1658 assert(!"GLSL 1.30 features unsupported");
1661 case ir_quadop_vector
:
1662 /* This operation should have already been handled.
1664 assert(!"Should not get here.");
1668 this->result
= result_src
;
1673 glsl_to_tgsi_visitor::visit(ir_swizzle
*ir
)
1679 /* Note that this is only swizzles in expressions, not those on the left
1680 * hand side of an assignment, which do write masking. See ir_assignment
1684 ir
->val
->accept(this);
1686 assert(src
.file
!= PROGRAM_UNDEFINED
);
1688 for (i
= 0; i
< 4; i
++) {
1689 if (i
< ir
->type
->vector_elements
) {
1692 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.x
);
1695 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.y
);
1698 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.z
);
1701 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.w
);
1705 /* If the type is smaller than a vec4, replicate the last
1708 swizzle
[i
] = swizzle
[ir
->type
->vector_elements
- 1];
1712 src
.swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
1718 glsl_to_tgsi_visitor::visit(ir_dereference_variable
*ir
)
1720 variable_storage
*entry
= find_variable_storage(ir
->var
);
1721 ir_variable
*var
= ir
->var
;
1724 switch (var
->mode
) {
1725 case ir_var_uniform
:
1726 entry
= new(mem_ctx
) variable_storage(var
, PROGRAM_UNIFORM
,
1728 this->variables
.push_tail(entry
);
1732 /* The linker assigns locations for varyings and attributes,
1733 * including deprecated builtins (like gl_Color), user-assign
1734 * generic attributes (glBindVertexLocation), and
1735 * user-defined varyings.
1737 * FINISHME: We would hit this path for function arguments. Fix!
1739 assert(var
->location
!= -1);
1740 entry
= new(mem_ctx
) variable_storage(var
,
1743 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
&&
1744 var
->location
>= VERT_ATTRIB_GENERIC0
) {
1745 _mesa_add_attribute(this->prog
->Attributes
,
1747 _mesa_sizeof_glsl_type(var
->type
->gl_type
),
1749 var
->location
- VERT_ATTRIB_GENERIC0
);
1753 assert(var
->location
!= -1);
1754 entry
= new(mem_ctx
) variable_storage(var
,
1758 case ir_var_system_value
:
1759 entry
= new(mem_ctx
) variable_storage(var
,
1760 PROGRAM_SYSTEM_VALUE
,
1764 case ir_var_temporary
:
1765 entry
= new(mem_ctx
) variable_storage(var
, PROGRAM_TEMPORARY
,
1767 this->variables
.push_tail(entry
);
1769 next_temp
+= type_size(var
->type
);
1774 printf("Failed to make storage for %s\n", var
->name
);
1779 this->result
= st_src_reg(entry
->file
, entry
->index
, var
->type
);
1780 if (!native_integers
)
1781 this->result
.type
= GLSL_TYPE_FLOAT
;
1785 glsl_to_tgsi_visitor::visit(ir_dereference_array
*ir
)
1789 int element_size
= type_size(ir
->type
);
1791 index
= ir
->array_index
->constant_expression_value();
1793 ir
->array
->accept(this);
1797 src
.index
+= index
->value
.i
[0] * element_size
;
1799 /* Variable index array dereference. It eats the "vec4" of the
1800 * base of the array and an index that offsets the TGSI register
1803 ir
->array_index
->accept(this);
1805 st_src_reg index_reg
;
1807 if (element_size
== 1) {
1808 index_reg
= this->result
;
1810 index_reg
= get_temp(glsl_type::float_type
);
1812 emit(ir
, TGSI_OPCODE_MUL
, st_dst_reg(index_reg
),
1813 this->result
, st_src_reg_for_float(element_size
));
1816 /* If there was already a relative address register involved, add the
1817 * new and the old together to get the new offset.
1819 if (src
.reladdr
!= NULL
) {
1820 st_src_reg accum_reg
= get_temp(glsl_type::float_type
);
1822 emit(ir
, TGSI_OPCODE_ADD
, st_dst_reg(accum_reg
),
1823 index_reg
, *src
.reladdr
);
1825 index_reg
= accum_reg
;
1828 src
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
1829 memcpy(src
.reladdr
, &index_reg
, sizeof(index_reg
));
1832 /* If the type is smaller than a vec4, replicate the last channel out. */
1833 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
1834 src
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1836 src
.swizzle
= SWIZZLE_NOOP
;
1842 glsl_to_tgsi_visitor::visit(ir_dereference_record
*ir
)
1845 const glsl_type
*struct_type
= ir
->record
->type
;
1848 ir
->record
->accept(this);
1850 for (i
= 0; i
< struct_type
->length
; i
++) {
1851 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
1853 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
1856 /* If the type is smaller than a vec4, replicate the last channel out. */
1857 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
1858 this->result
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1860 this->result
.swizzle
= SWIZZLE_NOOP
;
1862 this->result
.index
+= offset
;
1866 * We want to be careful in assignment setup to hit the actual storage
1867 * instead of potentially using a temporary like we might with the
1868 * ir_dereference handler.
1871 get_assignment_lhs(ir_dereference
*ir
, glsl_to_tgsi_visitor
*v
)
1873 /* The LHS must be a dereference. If the LHS is a variable indexed array
1874 * access of a vector, it must be separated into a series conditional moves
1875 * before reaching this point (see ir_vec_index_to_cond_assign).
1877 assert(ir
->as_dereference());
1878 ir_dereference_array
*deref_array
= ir
->as_dereference_array();
1880 assert(!deref_array
->array
->type
->is_vector());
1883 /* Use the rvalue deref handler for the most part. We'll ignore
1884 * swizzles in it and write swizzles using writemask, though.
1887 return st_dst_reg(v
->result
);
1891 * Process the condition of a conditional assignment
1893 * Examines the condition of a conditional assignment to generate the optimal
1894 * first operand of a \c CMP instruction. If the condition is a relational
1895 * operator with 0 (e.g., \c ir_binop_less), the value being compared will be
1896 * used as the source for the \c CMP instruction. Otherwise the comparison
1897 * is processed to a boolean result, and the boolean result is used as the
1898 * operand to the CMP instruction.
1901 glsl_to_tgsi_visitor::process_move_condition(ir_rvalue
*ir
)
1903 ir_rvalue
*src_ir
= ir
;
1905 bool switch_order
= false;
1907 ir_expression
*const expr
= ir
->as_expression();
1908 if ((expr
!= NULL
) && (expr
->get_num_operands() == 2)) {
1909 bool zero_on_left
= false;
1911 if (expr
->operands
[0]->is_zero()) {
1912 src_ir
= expr
->operands
[1];
1913 zero_on_left
= true;
1914 } else if (expr
->operands
[1]->is_zero()) {
1915 src_ir
= expr
->operands
[0];
1916 zero_on_left
= false;
1920 * (a < 0) T F F ( a < 0) T F F
1921 * (0 < a) F F T (-a < 0) F F T
1922 * (a <= 0) T T F (-a < 0) F F T (swap order of other operands)
1923 * (0 <= a) F T T ( a < 0) T F F (swap order of other operands)
1924 * (a > 0) F F T (-a < 0) F F T
1925 * (0 > a) T F F ( a < 0) T F F
1926 * (a >= 0) F T T ( a < 0) T F F (swap order of other operands)
1927 * (0 >= a) T T F (-a < 0) F F T (swap order of other operands)
1929 * Note that exchanging the order of 0 and 'a' in the comparison simply
1930 * means that the value of 'a' should be negated.
1933 switch (expr
->operation
) {
1935 switch_order
= false;
1936 negate
= zero_on_left
;
1939 case ir_binop_greater
:
1940 switch_order
= false;
1941 negate
= !zero_on_left
;
1944 case ir_binop_lequal
:
1945 switch_order
= true;
1946 negate
= !zero_on_left
;
1949 case ir_binop_gequal
:
1950 switch_order
= true;
1951 negate
= zero_on_left
;
1955 /* This isn't the right kind of comparison afterall, so make sure
1956 * the whole condition is visited.
1964 src_ir
->accept(this);
1966 /* We use the TGSI_OPCODE_CMP (a < 0 ? b : c) for conditional moves, and the
1967 * condition we produced is 0.0 or 1.0. By flipping the sign, we can
1968 * choose which value TGSI_OPCODE_CMP produces without an extra instruction
1969 * computing the condition.
1972 this->result
.negate
= ~this->result
.negate
;
1974 return switch_order
;
1978 glsl_to_tgsi_visitor::visit(ir_assignment
*ir
)
1984 ir
->rhs
->accept(this);
1987 l
= get_assignment_lhs(ir
->lhs
, this);
1989 /* FINISHME: This should really set to the correct maximal writemask for each
1990 * FINISHME: component written (in the loops below). This case can only
1991 * FINISHME: occur for matrices, arrays, and structures.
1993 if (ir
->write_mask
== 0) {
1994 assert(!ir
->lhs
->type
->is_scalar() && !ir
->lhs
->type
->is_vector());
1995 l
.writemask
= WRITEMASK_XYZW
;
1996 } else if (ir
->lhs
->type
->is_scalar() &&
1997 ir
->lhs
->variable_referenced()->mode
== ir_var_out
) {
1998 /* FINISHME: This hack makes writing to gl_FragDepth, which lives in the
1999 * FINISHME: W component of fragment shader output zero, work correctly.
2001 l
.writemask
= WRITEMASK_XYZW
;
2004 int first_enabled_chan
= 0;
2007 l
.writemask
= ir
->write_mask
;
2009 for (int i
= 0; i
< 4; i
++) {
2010 if (l
.writemask
& (1 << i
)) {
2011 first_enabled_chan
= GET_SWZ(r
.swizzle
, i
);
2016 /* Swizzle a small RHS vector into the channels being written.
2018 * glsl ir treats write_mask as dictating how many channels are
2019 * present on the RHS while TGSI treats write_mask as just
2020 * showing which channels of the vec4 RHS get written.
2022 for (int i
= 0; i
< 4; i
++) {
2023 if (l
.writemask
& (1 << i
))
2024 swizzles
[i
] = GET_SWZ(r
.swizzle
, rhs_chan
++);
2026 swizzles
[i
] = first_enabled_chan
;
2028 r
.swizzle
= MAKE_SWIZZLE4(swizzles
[0], swizzles
[1],
2029 swizzles
[2], swizzles
[3]);
2032 assert(l
.file
!= PROGRAM_UNDEFINED
);
2033 assert(r
.file
!= PROGRAM_UNDEFINED
);
2035 if (ir
->condition
) {
2036 const bool switch_order
= this->process_move_condition(ir
->condition
);
2037 st_src_reg condition
= this->result
;
2039 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
2040 st_src_reg l_src
= st_src_reg(l
);
2041 l_src
.swizzle
= swizzle_for_size(ir
->lhs
->type
->vector_elements
);
2044 emit(ir
, TGSI_OPCODE_CMP
, l
, condition
, l_src
, r
);
2046 emit(ir
, TGSI_OPCODE_CMP
, l
, condition
, r
, l_src
);
2052 } else if (ir
->rhs
->as_expression() &&
2053 this->instructions
.get_tail() &&
2054 ir
->rhs
== ((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->ir
&&
2055 type_size(ir
->lhs
->type
) == 1 &&
2056 l
.writemask
== ((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->dst
.writemask
) {
2057 /* To avoid emitting an extra MOV when assigning an expression to a
2058 * variable, emit the last instruction of the expression again, but
2059 * replace the destination register with the target of the assignment.
2060 * Dead code elimination will remove the original instruction.
2062 glsl_to_tgsi_instruction
*inst
, *new_inst
;
2063 inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
2064 new_inst
= emit(ir
, inst
->op
, l
, inst
->src
[0], inst
->src
[1], inst
->src
[2]);
2065 new_inst
->saturate
= inst
->saturate
;
2067 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
2068 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
2077 glsl_to_tgsi_visitor::visit(ir_constant
*ir
)
2080 GLfloat stack_vals
[4] = { 0 };
2081 gl_constant_value
*values
= (gl_constant_value
*) stack_vals
;
2082 GLenum gl_type
= GL_NONE
;
2084 static int in_array
= 0;
2085 gl_register_file file
= in_array
? PROGRAM_CONSTANT
: PROGRAM_IMMEDIATE
;
2087 /* Unfortunately, 4 floats is all we can get into
2088 * _mesa_add_typed_unnamed_constant. So, make a temp to store an
2089 * aggregate constant and move each constant value into it. If we
2090 * get lucky, copy propagation will eliminate the extra moves.
2092 if (ir
->type
->base_type
== GLSL_TYPE_STRUCT
) {
2093 st_src_reg temp_base
= get_temp(ir
->type
);
2094 st_dst_reg temp
= st_dst_reg(temp_base
);
2096 foreach_iter(exec_list_iterator
, iter
, ir
->components
) {
2097 ir_constant
*field_value
= (ir_constant
*)iter
.get();
2098 int size
= type_size(field_value
->type
);
2102 field_value
->accept(this);
2105 for (i
= 0; i
< (unsigned int)size
; i
++) {
2106 emit(ir
, TGSI_OPCODE_MOV
, temp
, src
);
2112 this->result
= temp_base
;
2116 if (ir
->type
->is_array()) {
2117 st_src_reg temp_base
= get_temp(ir
->type
);
2118 st_dst_reg temp
= st_dst_reg(temp_base
);
2119 int size
= type_size(ir
->type
->fields
.array
);
2124 for (i
= 0; i
< ir
->type
->length
; i
++) {
2125 ir
->array_elements
[i
]->accept(this);
2127 for (int j
= 0; j
< size
; j
++) {
2128 emit(ir
, TGSI_OPCODE_MOV
, temp
, src
);
2134 this->result
= temp_base
;
2139 if (ir
->type
->is_matrix()) {
2140 st_src_reg mat
= get_temp(ir
->type
);
2141 st_dst_reg mat_column
= st_dst_reg(mat
);
2143 for (i
= 0; i
< ir
->type
->matrix_columns
; i
++) {
2144 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
2145 values
= (gl_constant_value
*) &ir
->value
.f
[i
* ir
->type
->vector_elements
];
2147 src
= st_src_reg(file
, -1, ir
->type
->base_type
);
2148 src
.index
= add_constant(file
,
2150 ir
->type
->vector_elements
,
2153 emit(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
2162 switch (ir
->type
->base_type
) {
2163 case GLSL_TYPE_FLOAT
:
2165 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2166 values
[i
].f
= ir
->value
.f
[i
];
2169 case GLSL_TYPE_UINT
:
2170 gl_type
= native_integers
? GL_UNSIGNED_INT
: GL_FLOAT
;
2171 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2172 if (native_integers
)
2173 values
[i
].u
= ir
->value
.u
[i
];
2175 values
[i
].f
= ir
->value
.u
[i
];
2179 gl_type
= native_integers
? GL_INT
: GL_FLOAT
;
2180 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2181 if (native_integers
)
2182 values
[i
].i
= ir
->value
.i
[i
];
2184 values
[i
].f
= ir
->value
.i
[i
];
2187 case GLSL_TYPE_BOOL
:
2188 gl_type
= native_integers
? GL_BOOL
: GL_FLOAT
;
2189 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2190 if (native_integers
)
2191 values
[i
].b
= ir
->value
.b
[i
];
2193 values
[i
].f
= ir
->value
.b
[i
];
2197 assert(!"Non-float/uint/int/bool constant");
2200 this->result
= st_src_reg(file
, -1, ir
->type
);
2201 this->result
.index
= add_constant(file
,
2203 ir
->type
->vector_elements
,
2205 &this->result
.swizzle
);
2209 glsl_to_tgsi_visitor::get_function_signature(ir_function_signature
*sig
)
2211 function_entry
*entry
;
2213 foreach_iter(exec_list_iterator
, iter
, this->function_signatures
) {
2214 entry
= (function_entry
*)iter
.get();
2216 if (entry
->sig
== sig
)
2220 entry
= ralloc(mem_ctx
, function_entry
);
2222 entry
->sig_id
= this->next_signature_id
++;
2223 entry
->bgn_inst
= NULL
;
2225 /* Allocate storage for all the parameters. */
2226 foreach_iter(exec_list_iterator
, iter
, sig
->parameters
) {
2227 ir_variable
*param
= (ir_variable
*)iter
.get();
2228 variable_storage
*storage
;
2230 storage
= find_variable_storage(param
);
2233 storage
= new(mem_ctx
) variable_storage(param
, PROGRAM_TEMPORARY
,
2235 this->variables
.push_tail(storage
);
2237 this->next_temp
+= type_size(param
->type
);
2240 if (!sig
->return_type
->is_void()) {
2241 entry
->return_reg
= get_temp(sig
->return_type
);
2243 entry
->return_reg
= undef_src
;
2246 this->function_signatures
.push_tail(entry
);
2251 glsl_to_tgsi_visitor::visit(ir_call
*ir
)
2253 glsl_to_tgsi_instruction
*call_inst
;
2254 ir_function_signature
*sig
= ir
->get_callee();
2255 function_entry
*entry
= get_function_signature(sig
);
2258 /* Process in parameters. */
2259 exec_list_iterator sig_iter
= sig
->parameters
.iterator();
2260 foreach_iter(exec_list_iterator
, iter
, *ir
) {
2261 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
2262 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
2264 if (param
->mode
== ir_var_in
||
2265 param
->mode
== ir_var_inout
) {
2266 variable_storage
*storage
= find_variable_storage(param
);
2269 param_rval
->accept(this);
2270 st_src_reg r
= this->result
;
2273 l
.file
= storage
->file
;
2274 l
.index
= storage
->index
;
2276 l
.writemask
= WRITEMASK_XYZW
;
2277 l
.cond_mask
= COND_TR
;
2279 for (i
= 0; i
< type_size(param
->type
); i
++) {
2280 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
2288 assert(!sig_iter
.has_next());
2290 /* Emit call instruction */
2291 call_inst
= emit(ir
, TGSI_OPCODE_CAL
);
2292 call_inst
->function
= entry
;
2294 /* Process out parameters. */
2295 sig_iter
= sig
->parameters
.iterator();
2296 foreach_iter(exec_list_iterator
, iter
, *ir
) {
2297 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
2298 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
2300 if (param
->mode
== ir_var_out
||
2301 param
->mode
== ir_var_inout
) {
2302 variable_storage
*storage
= find_variable_storage(param
);
2306 r
.file
= storage
->file
;
2307 r
.index
= storage
->index
;
2309 r
.swizzle
= SWIZZLE_NOOP
;
2312 param_rval
->accept(this);
2313 st_dst_reg l
= st_dst_reg(this->result
);
2315 for (i
= 0; i
< type_size(param
->type
); i
++) {
2316 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
2324 assert(!sig_iter
.has_next());
2326 /* Process return value. */
2327 this->result
= entry
->return_reg
;
2331 glsl_to_tgsi_visitor::visit(ir_texture
*ir
)
2333 st_src_reg result_src
, coord
, lod_info
, projector
, dx
, dy
;
2334 st_dst_reg result_dst
, coord_dst
;
2335 glsl_to_tgsi_instruction
*inst
= NULL
;
2336 unsigned opcode
= TGSI_OPCODE_NOP
;
2338 ir
->coordinate
->accept(this);
2340 /* Put our coords in a temp. We'll need to modify them for shadow,
2341 * projection, or LOD, so the only case we'd use it as is is if
2342 * we're doing plain old texturing. The optimization passes on
2343 * glsl_to_tgsi_visitor should handle cleaning up our mess in that case.
2345 coord
= get_temp(glsl_type::vec4_type
);
2346 coord_dst
= st_dst_reg(coord
);
2347 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
2349 if (ir
->projector
) {
2350 ir
->projector
->accept(this);
2351 projector
= this->result
;
2354 /* Storage for our result. Ideally for an assignment we'd be using
2355 * the actual storage for the result here, instead.
2357 result_src
= get_temp(glsl_type::vec4_type
);
2358 result_dst
= st_dst_reg(result_src
);
2362 opcode
= TGSI_OPCODE_TEX
;
2365 opcode
= TGSI_OPCODE_TXB
;
2366 ir
->lod_info
.bias
->accept(this);
2367 lod_info
= this->result
;
2370 opcode
= TGSI_OPCODE_TXL
;
2371 ir
->lod_info
.lod
->accept(this);
2372 lod_info
= this->result
;
2375 opcode
= TGSI_OPCODE_TXD
;
2376 ir
->lod_info
.grad
.dPdx
->accept(this);
2378 ir
->lod_info
.grad
.dPdy
->accept(this);
2381 case ir_txf
: /* TODO: use TGSI_OPCODE_TXF here */
2382 assert(!"GLSL 1.30 features unsupported");
2386 if (ir
->projector
) {
2387 if (opcode
== TGSI_OPCODE_TEX
) {
2388 /* Slot the projector in as the last component of the coord. */
2389 coord_dst
.writemask
= WRITEMASK_W
;
2390 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, projector
);
2391 coord_dst
.writemask
= WRITEMASK_XYZW
;
2392 opcode
= TGSI_OPCODE_TXP
;
2394 st_src_reg coord_w
= coord
;
2395 coord_w
.swizzle
= SWIZZLE_WWWW
;
2397 /* For the other TEX opcodes there's no projective version
2398 * since the last slot is taken up by LOD info. Do the
2399 * projective divide now.
2401 coord_dst
.writemask
= WRITEMASK_W
;
2402 emit(ir
, TGSI_OPCODE_RCP
, coord_dst
, projector
);
2404 /* In the case where we have to project the coordinates "by hand,"
2405 * the shadow comparator value must also be projected.
2407 st_src_reg tmp_src
= coord
;
2408 if (ir
->shadow_comparitor
) {
2409 /* Slot the shadow value in as the second to last component of the
2412 ir
->shadow_comparitor
->accept(this);
2414 tmp_src
= get_temp(glsl_type::vec4_type
);
2415 st_dst_reg tmp_dst
= st_dst_reg(tmp_src
);
2417 tmp_dst
.writemask
= WRITEMASK_Z
;
2418 emit(ir
, TGSI_OPCODE_MOV
, tmp_dst
, this->result
);
2420 tmp_dst
.writemask
= WRITEMASK_XY
;
2421 emit(ir
, TGSI_OPCODE_MOV
, tmp_dst
, coord
);
2424 coord_dst
.writemask
= WRITEMASK_XYZ
;
2425 emit(ir
, TGSI_OPCODE_MUL
, coord_dst
, tmp_src
, coord_w
);
2427 coord_dst
.writemask
= WRITEMASK_XYZW
;
2428 coord
.swizzle
= SWIZZLE_XYZW
;
2432 /* If projection is done and the opcode is not TGSI_OPCODE_TXP, then the shadow
2433 * comparator was put in the correct place (and projected) by the code,
2434 * above, that handles by-hand projection.
2436 if (ir
->shadow_comparitor
&& (!ir
->projector
|| opcode
== TGSI_OPCODE_TXP
)) {
2437 /* Slot the shadow value in as the second to last component of the
2440 ir
->shadow_comparitor
->accept(this);
2441 coord_dst
.writemask
= WRITEMASK_Z
;
2442 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
2443 coord_dst
.writemask
= WRITEMASK_XYZW
;
2446 if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXB
) {
2447 /* TGSI stores LOD or LOD bias in the last channel of the coords. */
2448 coord_dst
.writemask
= WRITEMASK_W
;
2449 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, lod_info
);
2450 coord_dst
.writemask
= WRITEMASK_XYZW
;
2453 if (opcode
== TGSI_OPCODE_TXD
)
2454 inst
= emit(ir
, opcode
, result_dst
, coord
, dx
, dy
);
2456 inst
= emit(ir
, opcode
, result_dst
, coord
);
2458 if (ir
->shadow_comparitor
)
2459 inst
->tex_shadow
= GL_TRUE
;
2461 inst
->sampler
= _mesa_get_sampler_uniform_value(ir
->sampler
,
2462 this->shader_program
,
2465 const glsl_type
*sampler_type
= ir
->sampler
->type
;
2467 switch (sampler_type
->sampler_dimensionality
) {
2468 case GLSL_SAMPLER_DIM_1D
:
2469 inst
->tex_target
= (sampler_type
->sampler_array
)
2470 ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
2472 case GLSL_SAMPLER_DIM_2D
:
2473 inst
->tex_target
= (sampler_type
->sampler_array
)
2474 ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
2476 case GLSL_SAMPLER_DIM_3D
:
2477 inst
->tex_target
= TEXTURE_3D_INDEX
;
2479 case GLSL_SAMPLER_DIM_CUBE
:
2480 inst
->tex_target
= TEXTURE_CUBE_INDEX
;
2482 case GLSL_SAMPLER_DIM_RECT
:
2483 inst
->tex_target
= TEXTURE_RECT_INDEX
;
2485 case GLSL_SAMPLER_DIM_BUF
:
2486 assert(!"FINISHME: Implement ARB_texture_buffer_object");
2489 assert(!"Should not get here.");
2492 this->result
= result_src
;
2496 glsl_to_tgsi_visitor::visit(ir_return
*ir
)
2498 if (ir
->get_value()) {
2502 assert(current_function
);
2504 ir
->get_value()->accept(this);
2505 st_src_reg r
= this->result
;
2507 l
= st_dst_reg(current_function
->return_reg
);
2509 for (i
= 0; i
< type_size(current_function
->sig
->return_type
); i
++) {
2510 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
2516 emit(ir
, TGSI_OPCODE_RET
);
2520 glsl_to_tgsi_visitor::visit(ir_discard
*ir
)
2522 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
2524 if (ir
->condition
) {
2525 ir
->condition
->accept(this);
2526 this->result
.negate
= ~this->result
.negate
;
2527 emit(ir
, TGSI_OPCODE_KIL
, undef_dst
, this->result
);
2529 emit(ir
, TGSI_OPCODE_KILP
);
2532 fp
->UsesKill
= GL_TRUE
;
2536 glsl_to_tgsi_visitor::visit(ir_if
*ir
)
2538 glsl_to_tgsi_instruction
*cond_inst
, *if_inst
;
2539 glsl_to_tgsi_instruction
*prev_inst
;
2541 prev_inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
2543 ir
->condition
->accept(this);
2544 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
2546 if (this->options
->EmitCondCodes
) {
2547 cond_inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
2549 /* See if we actually generated any instruction for generating
2550 * the condition. If not, then cook up a move to a temp so we
2551 * have something to set cond_update on.
2553 if (cond_inst
== prev_inst
) {
2554 st_src_reg temp
= get_temp(glsl_type::bool_type
);
2555 cond_inst
= emit(ir
->condition
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), result
);
2557 cond_inst
->cond_update
= GL_TRUE
;
2559 if_inst
= emit(ir
->condition
, TGSI_OPCODE_IF
);
2560 if_inst
->dst
.cond_mask
= COND_NE
;
2562 if_inst
= emit(ir
->condition
, TGSI_OPCODE_IF
, undef_dst
, this->result
);
2565 this->instructions
.push_tail(if_inst
);
2567 visit_exec_list(&ir
->then_instructions
, this);
2569 if (!ir
->else_instructions
.is_empty()) {
2570 emit(ir
->condition
, TGSI_OPCODE_ELSE
);
2571 visit_exec_list(&ir
->else_instructions
, this);
2574 if_inst
= emit(ir
->condition
, TGSI_OPCODE_ENDIF
);
2577 glsl_to_tgsi_visitor::glsl_to_tgsi_visitor()
2579 result
.file
= PROGRAM_UNDEFINED
;
2581 next_signature_id
= 1;
2583 current_function
= NULL
;
2584 num_address_regs
= 0;
2585 indirect_addr_temps
= false;
2586 indirect_addr_consts
= false;
2587 mem_ctx
= ralloc_context(NULL
);
2590 glsl_to_tgsi_visitor::~glsl_to_tgsi_visitor()
2592 ralloc_free(mem_ctx
);
2595 extern "C" void free_glsl_to_tgsi_visitor(glsl_to_tgsi_visitor
*v
)
2602 * Count resources used by the given gpu program (number of texture
2606 count_resources(glsl_to_tgsi_visitor
*v
, gl_program
*prog
)
2608 v
->samplers_used
= 0;
2610 foreach_iter(exec_list_iterator
, iter
, v
->instructions
) {
2611 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
2613 if (is_tex_instruction(inst
->op
)) {
2614 v
->samplers_used
|= 1 << inst
->sampler
;
2616 prog
->SamplerTargets
[inst
->sampler
] =
2617 (gl_texture_index
)inst
->tex_target
;
2618 if (inst
->tex_shadow
) {
2619 prog
->ShadowSamplers
|= 1 << inst
->sampler
;
2624 prog
->SamplersUsed
= v
->samplers_used
;
2625 _mesa_update_shader_textures_used(prog
);
2630 * Check if the given vertex/fragment/shader program is within the
2631 * resource limits of the context (number of texture units, etc).
2632 * If any of those checks fail, record a linker error.
2634 * XXX more checks are needed...
2637 check_resources(const struct gl_context
*ctx
,
2638 struct gl_shader_program
*shader_program
,
2639 glsl_to_tgsi_visitor
*prog
,
2640 struct gl_program
*proginfo
)
2642 switch (proginfo
->Target
) {
2643 case GL_VERTEX_PROGRAM_ARB
:
2644 if (_mesa_bitcount(prog
->samplers_used
) >
2645 ctx
->Const
.MaxVertexTextureImageUnits
) {
2646 fail_link(shader_program
, "Too many vertex shader texture samplers");
2648 if (proginfo
->Parameters
->NumParameters
> MAX_UNIFORMS
) {
2649 fail_link(shader_program
, "Too many vertex shader constants");
2652 case MESA_GEOMETRY_PROGRAM
:
2653 if (_mesa_bitcount(prog
->samplers_used
) >
2654 ctx
->Const
.MaxGeometryTextureImageUnits
) {
2655 fail_link(shader_program
, "Too many geometry shader texture samplers");
2657 if (proginfo
->Parameters
->NumParameters
>
2658 MAX_GEOMETRY_UNIFORM_COMPONENTS
/ 4) {
2659 fail_link(shader_program
, "Too many geometry shader constants");
2662 case GL_FRAGMENT_PROGRAM_ARB
:
2663 if (_mesa_bitcount(prog
->samplers_used
) >
2664 ctx
->Const
.MaxTextureImageUnits
) {
2665 fail_link(shader_program
, "Too many fragment shader texture samplers");
2667 if (proginfo
->Parameters
->NumParameters
> MAX_UNIFORMS
) {
2668 fail_link(shader_program
, "Too many fragment shader constants");
2672 _mesa_problem(ctx
, "unexpected program type in check_resources()");
2678 struct uniform_sort
{
2679 struct gl_uniform
*u
;
2683 /* The shader_program->Uniforms list is almost sorted in increasing
2684 * uniform->{Frag,Vert}Pos locations, but not quite when there are
2685 * uniforms shared between targets. We need to add parameters in
2686 * increasing order for the targets.
2689 sort_uniforms(const void *a
, const void *b
)
2691 struct uniform_sort
*u1
= (struct uniform_sort
*)a
;
2692 struct uniform_sort
*u2
= (struct uniform_sort
*)b
;
2694 return u1
->pos
- u2
->pos
;
2697 /* Add the uniforms to the parameters. The linker chose locations
2698 * in our parameters lists (which weren't created yet), which the
2699 * uniforms code will use to poke values into our parameters list
2700 * when uniforms are updated.
2703 add_uniforms_to_parameters_list(struct gl_shader_program
*shader_program
,
2704 struct gl_shader
*shader
,
2705 struct gl_program
*prog
)
2708 unsigned int next_sampler
= 0, num_uniforms
= 0;
2709 struct uniform_sort
*sorted_uniforms
;
2711 sorted_uniforms
= ralloc_array(NULL
, struct uniform_sort
,
2712 shader_program
->Uniforms
->NumUniforms
);
2714 for (i
= 0; i
< shader_program
->Uniforms
->NumUniforms
; i
++) {
2715 struct gl_uniform
*uniform
= shader_program
->Uniforms
->Uniforms
+ i
;
2716 int parameter_index
= -1;
2718 switch (shader
->Type
) {
2719 case GL_VERTEX_SHADER
:
2720 parameter_index
= uniform
->VertPos
;
2722 case GL_FRAGMENT_SHADER
:
2723 parameter_index
= uniform
->FragPos
;
2725 case GL_GEOMETRY_SHADER
:
2726 parameter_index
= uniform
->GeomPos
;
2730 /* Only add uniforms used in our target. */
2731 if (parameter_index
!= -1) {
2732 sorted_uniforms
[num_uniforms
].pos
= parameter_index
;
2733 sorted_uniforms
[num_uniforms
].u
= uniform
;
2738 qsort(sorted_uniforms
, num_uniforms
, sizeof(struct uniform_sort
),
2741 for (i
= 0; i
< num_uniforms
; i
++) {
2742 struct gl_uniform
*uniform
= sorted_uniforms
[i
].u
;
2743 int parameter_index
= sorted_uniforms
[i
].pos
;
2744 const glsl_type
*type
= uniform
->Type
;
2747 if (type
->is_vector() ||
2748 type
->is_scalar()) {
2749 size
= type
->vector_elements
;
2751 size
= type_size(type
) * 4;
2754 gl_register_file file
;
2755 if (type
->is_sampler() ||
2756 (type
->is_array() && type
->fields
.array
->is_sampler())) {
2757 file
= PROGRAM_SAMPLER
;
2759 file
= PROGRAM_UNIFORM
;
2762 GLint index
= _mesa_lookup_parameter_index(prog
->Parameters
, -1,
2766 index
= _mesa_add_parameter(prog
->Parameters
, file
,
2767 uniform
->Name
, size
, type
->gl_type
,
2770 /* Sampler uniform values are stored in prog->SamplerUnits,
2771 * and the entry in that array is selected by this index we
2772 * store in ParameterValues[].
2774 if (file
== PROGRAM_SAMPLER
) {
2775 for (unsigned int j
= 0; j
< size
/ 4; j
++)
2776 prog
->Parameters
->ParameterValues
[index
+ j
][0].f
= next_sampler
++;
2779 /* The location chosen in the Parameters list here (returned
2780 * from _mesa_add_uniform) has to match what the linker chose.
2782 if (index
!= parameter_index
) {
2783 fail_link(shader_program
, "Allocation of uniform `%s' to target "
2784 "failed (%d vs %d)\n",
2785 uniform
->Name
, index
, parameter_index
);
2790 ralloc_free(sorted_uniforms
);
2794 set_uniform_initializer(struct gl_context
*ctx
, void *mem_ctx
,
2795 struct gl_shader_program
*shader_program
,
2796 const char *name
, const glsl_type
*type
,
2799 if (type
->is_record()) {
2800 ir_constant
*field_constant
;
2802 field_constant
= (ir_constant
*)val
->components
.get_head();
2804 for (unsigned int i
= 0; i
< type
->length
; i
++) {
2805 const glsl_type
*field_type
= type
->fields
.structure
[i
].type
;
2806 const char *field_name
= ralloc_asprintf(mem_ctx
, "%s.%s", name
,
2807 type
->fields
.structure
[i
].name
);
2808 set_uniform_initializer(ctx
, mem_ctx
, shader_program
, field_name
,
2809 field_type
, field_constant
);
2810 field_constant
= (ir_constant
*)field_constant
->next
;
2815 int loc
= _mesa_get_uniform_location(ctx
, shader_program
, name
);
2818 fail_link(shader_program
,
2819 "Couldn't find uniform for initializer %s\n", name
);
2823 for (unsigned int i
= 0; i
< (type
->is_array() ? type
->length
: 1); i
++) {
2824 ir_constant
*element
;
2825 const glsl_type
*element_type
;
2826 if (type
->is_array()) {
2827 element
= val
->array_elements
[i
];
2828 element_type
= type
->fields
.array
;
2831 element_type
= type
;
2836 if (element_type
->base_type
== GLSL_TYPE_BOOL
) {
2837 int *conv
= ralloc_array(mem_ctx
, int, element_type
->components());
2838 for (unsigned int j
= 0; j
< element_type
->components(); j
++) {
2839 conv
[j
] = element
->value
.b
[j
];
2841 values
= (void *)conv
;
2842 element_type
= glsl_type::get_instance(GLSL_TYPE_INT
,
2843 element_type
->vector_elements
,
2846 values
= &element
->value
;
2849 if (element_type
->is_matrix()) {
2850 _mesa_uniform_matrix(ctx
, shader_program
,
2851 element_type
->matrix_columns
,
2852 element_type
->vector_elements
,
2853 loc
, 1, GL_FALSE
, (GLfloat
*)values
);
2854 loc
+= element_type
->matrix_columns
;
2856 _mesa_uniform(ctx
, shader_program
, loc
, element_type
->matrix_columns
,
2857 values
, element_type
->gl_type
);
2858 loc
+= type_size(element_type
);
2864 set_uniform_initializers(struct gl_context
*ctx
,
2865 struct gl_shader_program
*shader_program
)
2867 void *mem_ctx
= NULL
;
2869 for (unsigned int i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
2870 struct gl_shader
*shader
= shader_program
->_LinkedShaders
[i
];
2875 foreach_iter(exec_list_iterator
, iter
, *shader
->ir
) {
2876 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
2877 ir_variable
*var
= ir
->as_variable();
2879 if (!var
|| var
->mode
!= ir_var_uniform
|| !var
->constant_value
)
2883 mem_ctx
= ralloc_context(NULL
);
2885 set_uniform_initializer(ctx
, mem_ctx
, shader_program
, var
->name
,
2886 var
->type
, var
->constant_value
);
2890 ralloc_free(mem_ctx
);
2894 * Scan/rewrite program to remove reads of custom (output) registers.
2895 * The passed type has to be either PROGRAM_OUTPUT or PROGRAM_VARYING
2896 * (for vertex shaders).
2897 * In GLSL shaders, varying vars can be read and written.
2898 * On some hardware, trying to read an output register causes trouble.
2899 * So, rewrite the program to use a temporary register in this case.
2901 * Based on _mesa_remove_output_reads from programopt.c.
2904 glsl_to_tgsi_visitor::remove_output_reads(gl_register_file type
)
2907 GLint outputMap
[VERT_RESULT_MAX
];
2908 GLint outputTypes
[VERT_RESULT_MAX
];
2909 GLuint numVaryingReads
= 0;
2910 GLboolean usedTemps
[MAX_TEMPS
];
2911 GLuint firstTemp
= 0;
2913 _mesa_find_used_registers(prog
, PROGRAM_TEMPORARY
,
2914 usedTemps
, MAX_TEMPS
);
2916 assert(type
== PROGRAM_VARYING
|| type
== PROGRAM_OUTPUT
);
2917 assert(prog
->Target
== GL_VERTEX_PROGRAM_ARB
|| type
!= PROGRAM_VARYING
);
2919 for (i
= 0; i
< VERT_RESULT_MAX
; i
++)
2922 /* look for instructions which read from varying vars */
2923 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2924 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
2925 const GLuint numSrc
= num_inst_src_regs(inst
->op
);
2927 for (j
= 0; j
< numSrc
; j
++) {
2928 if (inst
->src
[j
].file
== type
) {
2929 /* replace the read with a temp reg */
2930 const GLuint var
= inst
->src
[j
].index
;
2931 if (outputMap
[var
] == -1) {
2933 outputMap
[var
] = _mesa_find_free_register(usedTemps
,
2936 outputTypes
[var
] = inst
->src
[j
].type
;
2937 firstTemp
= outputMap
[var
] + 1;
2939 inst
->src
[j
].file
= PROGRAM_TEMPORARY
;
2940 inst
->src
[j
].index
= outputMap
[var
];
2945 if (numVaryingReads
== 0)
2946 return; /* nothing to be done */
2948 /* look for instructions which write to the varying vars identified above */
2949 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2950 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
2951 if (inst
->dst
.file
== type
&& outputMap
[inst
->dst
.index
] >= 0) {
2952 /* change inst to write to the temp reg, instead of the varying */
2953 inst
->dst
.file
= PROGRAM_TEMPORARY
;
2954 inst
->dst
.index
= outputMap
[inst
->dst
.index
];
2958 /* insert new MOV instructions at the end */
2959 for (i
= 0; i
< VERT_RESULT_MAX
; i
++) {
2960 if (outputMap
[i
] >= 0) {
2961 /* MOV VAR[i], TEMP[tmp]; */
2962 st_src_reg src
= st_src_reg(PROGRAM_TEMPORARY
, outputMap
[i
], outputTypes
[i
]);
2963 st_dst_reg dst
= st_dst_reg(type
, WRITEMASK_XYZW
, outputTypes
[i
]);
2965 this->emit(NULL
, TGSI_OPCODE_MOV
, dst
, src
);
2971 * Returns the mask of channels (bitmask of WRITEMASK_X,Y,Z,W) which
2972 * are read from the given src in this instruction
2975 get_src_arg_mask(st_dst_reg dst
, st_src_reg src
)
2977 int read_mask
= 0, comp
;
2979 /* Now, given the src swizzle and the written channels, find which
2980 * components are actually read
2982 for (comp
= 0; comp
< 4; ++comp
) {
2983 const unsigned coord
= GET_SWZ(src
.swizzle
, comp
);
2985 if (dst
.writemask
& (1 << comp
) && coord
<= SWIZZLE_W
)
2986 read_mask
|= 1 << coord
;
2993 * This pass replaces CMP T0, T1 T2 T0 with MOV T0, T2 when the CMP
2994 * instruction is the first instruction to write to register T0. There are
2995 * several lowering passes done in GLSL IR (e.g. branches and
2996 * relative addressing) that create a large number of conditional assignments
2997 * that ir_to_mesa converts to CMP instructions like the one mentioned above.
2999 * Here is why this conversion is safe:
3000 * CMP T0, T1 T2 T0 can be expanded to:
3006 * If (T1 < 0.0) evaluates to true then our replacement MOV T0, T2 is the same
3007 * as the original program. If (T1 < 0.0) evaluates to false, executing
3008 * MOV T0, T0 will store a garbage value in T0 since T0 is uninitialized.
3009 * Therefore, it doesn't matter that we are replacing MOV T0, T0 with MOV T0, T2
3010 * because any instruction that was going to read from T0 after this was going
3011 * to read a garbage value anyway.
3014 glsl_to_tgsi_visitor::simplify_cmp(void)
3016 unsigned tempWrites
[MAX_TEMPS
];
3017 unsigned outputWrites
[MAX_PROGRAM_OUTPUTS
];
3019 memset(tempWrites
, 0, sizeof(tempWrites
));
3020 memset(outputWrites
, 0, sizeof(outputWrites
));
3022 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3023 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3024 unsigned prevWriteMask
= 0;
3026 /* Give up if we encounter relative addressing or flow control. */
3027 if (inst
->dst
.reladdr
||
3028 tgsi_get_opcode_info(inst
->op
)->is_branch
||
3029 inst
->op
== TGSI_OPCODE_BGNSUB
||
3030 inst
->op
== TGSI_OPCODE_CONT
||
3031 inst
->op
== TGSI_OPCODE_END
||
3032 inst
->op
== TGSI_OPCODE_ENDSUB
||
3033 inst
->op
== TGSI_OPCODE_RET
) {
3037 if (inst
->dst
.file
== PROGRAM_OUTPUT
) {
3038 assert(inst
->dst
.index
< MAX_PROGRAM_OUTPUTS
);
3039 prevWriteMask
= outputWrites
[inst
->dst
.index
];
3040 outputWrites
[inst
->dst
.index
] |= inst
->dst
.writemask
;
3041 } else if (inst
->dst
.file
== PROGRAM_TEMPORARY
) {
3042 assert(inst
->dst
.index
< MAX_TEMPS
);
3043 prevWriteMask
= tempWrites
[inst
->dst
.index
];
3044 tempWrites
[inst
->dst
.index
] |= inst
->dst
.writemask
;
3047 /* For a CMP to be considered a conditional write, the destination
3048 * register and source register two must be the same. */
3049 if (inst
->op
== TGSI_OPCODE_CMP
3050 && !(inst
->dst
.writemask
& prevWriteMask
)
3051 && inst
->src
[2].file
== inst
->dst
.file
3052 && inst
->src
[2].index
== inst
->dst
.index
3053 && inst
->dst
.writemask
== get_src_arg_mask(inst
->dst
, inst
->src
[2])) {
3055 inst
->op
= TGSI_OPCODE_MOV
;
3056 inst
->src
[0] = inst
->src
[1];
3061 /* Replaces all references to a temporary register index with another index. */
3063 glsl_to_tgsi_visitor::rename_temp_register(int index
, int new_index
)
3065 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3066 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3069 for (j
=0; j
< num_inst_src_regs(inst
->op
); j
++) {
3070 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
&&
3071 inst
->src
[j
].index
== index
) {
3072 inst
->src
[j
].index
= new_index
;
3076 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== index
) {
3077 inst
->dst
.index
= new_index
;
3083 glsl_to_tgsi_visitor::get_first_temp_read(int index
)
3085 int depth
= 0; /* loop depth */
3086 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
3089 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3090 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3092 for (j
=0; j
< num_inst_src_regs(inst
->op
); j
++) {
3093 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
&&
3094 inst
->src
[j
].index
== index
) {
3095 return (depth
== 0) ? i
: loop_start
;
3099 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
3102 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
3115 glsl_to_tgsi_visitor::get_first_temp_write(int index
)
3117 int depth
= 0; /* loop depth */
3118 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
3121 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3122 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3124 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== index
) {
3125 return (depth
== 0) ? i
: loop_start
;
3128 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
3131 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
3144 glsl_to_tgsi_visitor::get_last_temp_read(int index
)
3146 int depth
= 0; /* loop depth */
3147 int last
= -1; /* index of last instruction that reads the temporary */
3150 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3151 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3153 for (j
=0; j
< num_inst_src_regs(inst
->op
); j
++) {
3154 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
&&
3155 inst
->src
[j
].index
== index
) {
3156 last
= (depth
== 0) ? i
: -2;
3160 if (inst
->op
== TGSI_OPCODE_BGNLOOP
)
3162 else if (inst
->op
== TGSI_OPCODE_ENDLOOP
)
3163 if (--depth
== 0 && last
== -2)
3175 glsl_to_tgsi_visitor::get_last_temp_write(int index
)
3177 int depth
= 0; /* loop depth */
3178 int last
= -1; /* index of last instruction that writes to the temporary */
3181 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3182 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3184 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== index
)
3185 last
= (depth
== 0) ? i
: -2;
3187 if (inst
->op
== TGSI_OPCODE_BGNLOOP
)
3189 else if (inst
->op
== TGSI_OPCODE_ENDLOOP
)
3190 if (--depth
== 0 && last
== -2)
3202 * On a basic block basis, tracks available PROGRAM_TEMPORARY register
3203 * channels for copy propagation and updates following instructions to
3204 * use the original versions.
3206 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
3207 * will occur. As an example, a TXP production before this pass:
3209 * 0: MOV TEMP[1], INPUT[4].xyyy;
3210 * 1: MOV TEMP[1].w, INPUT[4].wwww;
3211 * 2: TXP TEMP[2], TEMP[1], texture[0], 2D;
3215 * 0: MOV TEMP[1], INPUT[4].xyyy;
3216 * 1: MOV TEMP[1].w, INPUT[4].wwww;
3217 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
3219 * which allows for dead code elimination on TEMP[1]'s writes.
3222 glsl_to_tgsi_visitor::copy_propagate(void)
3224 glsl_to_tgsi_instruction
**acp
= rzalloc_array(mem_ctx
,
3225 glsl_to_tgsi_instruction
*,
3226 this->next_temp
* 4);
3227 int *acp_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
3230 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3231 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3233 assert(inst
->dst
.file
!= PROGRAM_TEMPORARY
3234 || inst
->dst
.index
< this->next_temp
);
3236 /* First, do any copy propagation possible into the src regs. */
3237 for (int r
= 0; r
< 3; r
++) {
3238 glsl_to_tgsi_instruction
*first
= NULL
;
3240 int acp_base
= inst
->src
[r
].index
* 4;
3242 if (inst
->src
[r
].file
!= PROGRAM_TEMPORARY
||
3243 inst
->src
[r
].reladdr
)
3246 /* See if we can find entries in the ACP consisting of MOVs
3247 * from the same src register for all the swizzled channels
3248 * of this src register reference.
3250 for (int i
= 0; i
< 4; i
++) {
3251 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
3252 glsl_to_tgsi_instruction
*copy_chan
= acp
[acp_base
+ src_chan
];
3259 assert(acp_level
[acp_base
+ src_chan
] <= level
);
3264 if (first
->src
[0].file
!= copy_chan
->src
[0].file
||
3265 first
->src
[0].index
!= copy_chan
->src
[0].index
) {
3273 /* We've now validated that we can copy-propagate to
3274 * replace this src register reference. Do it.
3276 inst
->src
[r
].file
= first
->src
[0].file
;
3277 inst
->src
[r
].index
= first
->src
[0].index
;
3280 for (int i
= 0; i
< 4; i
++) {
3281 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
3282 glsl_to_tgsi_instruction
*copy_inst
= acp
[acp_base
+ src_chan
];
3283 swizzle
|= (GET_SWZ(copy_inst
->src
[0].swizzle
, src_chan
) <<
3286 inst
->src
[r
].swizzle
= swizzle
;
3291 case TGSI_OPCODE_BGNLOOP
:
3292 case TGSI_OPCODE_ENDLOOP
:
3293 /* End of a basic block, clear the ACP entirely. */
3294 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
3297 case TGSI_OPCODE_IF
:
3301 case TGSI_OPCODE_ENDIF
:
3302 case TGSI_OPCODE_ELSE
:
3303 /* Clear all channels written inside the block from the ACP, but
3304 * leaving those that were not touched.
3306 for (int r
= 0; r
< this->next_temp
; r
++) {
3307 for (int c
= 0; c
< 4; c
++) {
3308 if (!acp
[4 * r
+ c
])
3311 if (acp_level
[4 * r
+ c
] >= level
)
3312 acp
[4 * r
+ c
] = NULL
;
3315 if (inst
->op
== TGSI_OPCODE_ENDIF
)
3320 /* Continuing the block, clear any written channels from
3323 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.reladdr
) {
3324 /* Any temporary might be written, so no copy propagation
3325 * across this instruction.
3327 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
3328 } else if (inst
->dst
.file
== PROGRAM_OUTPUT
&&
3329 inst
->dst
.reladdr
) {
3330 /* Any output might be written, so no copy propagation
3331 * from outputs across this instruction.
3333 for (int r
= 0; r
< this->next_temp
; r
++) {
3334 for (int c
= 0; c
< 4; c
++) {
3335 if (!acp
[4 * r
+ c
])
3338 if (acp
[4 * r
+ c
]->src
[0].file
== PROGRAM_OUTPUT
)
3339 acp
[4 * r
+ c
] = NULL
;
3342 } else if (inst
->dst
.file
== PROGRAM_TEMPORARY
||
3343 inst
->dst
.file
== PROGRAM_OUTPUT
) {
3344 /* Clear where it's used as dst. */
3345 if (inst
->dst
.file
== PROGRAM_TEMPORARY
) {
3346 for (int c
= 0; c
< 4; c
++) {
3347 if (inst
->dst
.writemask
& (1 << c
)) {
3348 acp
[4 * inst
->dst
.index
+ c
] = NULL
;
3353 /* Clear where it's used as src. */
3354 for (int r
= 0; r
< this->next_temp
; r
++) {
3355 for (int c
= 0; c
< 4; c
++) {
3356 if (!acp
[4 * r
+ c
])
3359 int src_chan
= GET_SWZ(acp
[4 * r
+ c
]->src
[0].swizzle
, c
);
3361 if (acp
[4 * r
+ c
]->src
[0].file
== inst
->dst
.file
&&
3362 acp
[4 * r
+ c
]->src
[0].index
== inst
->dst
.index
&&
3363 inst
->dst
.writemask
& (1 << src_chan
))
3365 acp
[4 * r
+ c
] = NULL
;
3373 /* If this is a copy, add it to the ACP. */
3374 if (inst
->op
== TGSI_OPCODE_MOV
&&
3375 inst
->dst
.file
== PROGRAM_TEMPORARY
&&
3376 !inst
->dst
.reladdr
&&
3378 !inst
->src
[0].reladdr
&&
3379 !inst
->src
[0].negate
) {
3380 for (int i
= 0; i
< 4; i
++) {
3381 if (inst
->dst
.writemask
& (1 << i
)) {
3382 acp
[4 * inst
->dst
.index
+ i
] = inst
;
3383 acp_level
[4 * inst
->dst
.index
+ i
] = level
;
3389 ralloc_free(acp_level
);
3394 * Tracks available PROGRAM_TEMPORARY registers for dead code elimination.
3396 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
3397 * will occur. As an example, a TXP production after copy propagation but
3400 * 0: MOV TEMP[1], INPUT[4].xyyy;
3401 * 1: MOV TEMP[1].w, INPUT[4].wwww;
3402 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
3404 * and after this pass:
3406 * 0: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
3408 * FIXME: assumes that all functions are inlined (no support for BGNSUB/ENDSUB)
3409 * FIXME: doesn't eliminate all dead code inside of loops; it steps around them
3412 glsl_to_tgsi_visitor::eliminate_dead_code(void)
3416 for (i
=0; i
< this->next_temp
; i
++) {
3417 int last_read
= get_last_temp_read(i
);
3420 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3421 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3423 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== i
&&
3436 * On a basic block basis, tracks available PROGRAM_TEMPORARY registers for dead
3437 * code elimination. This is less primitive than eliminate_dead_code(), as it
3438 * is per-channel and can detect consecutive writes without a read between them
3439 * as dead code. However, there is some dead code that can be eliminated by
3440 * eliminate_dead_code() but not this function - for example, this function
3441 * cannot eliminate an instruction writing to a register that is never read and
3442 * is the only instruction writing to that register.
3444 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
3448 glsl_to_tgsi_visitor::eliminate_dead_code_advanced(void)
3450 glsl_to_tgsi_instruction
**writes
= rzalloc_array(mem_ctx
,
3451 glsl_to_tgsi_instruction
*,
3452 this->next_temp
* 4);
3453 int *write_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
3457 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3458 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3460 assert(inst
->dst
.file
!= PROGRAM_TEMPORARY
3461 || inst
->dst
.index
< this->next_temp
);
3464 case TGSI_OPCODE_BGNLOOP
:
3465 case TGSI_OPCODE_ENDLOOP
:
3466 /* End of a basic block, clear the write array entirely.
3467 * FIXME: This keeps us from killing dead code when the writes are
3468 * on either side of a loop, even when the register isn't touched
3471 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
3474 case TGSI_OPCODE_ENDIF
:
3478 case TGSI_OPCODE_ELSE
:
3479 /* Clear all channels written inside the preceding if block from the
3480 * write array, but leave those that were not touched.
3482 * FIXME: This destroys opportunities to remove dead code inside of
3483 * IF blocks that are followed by an ELSE block.
3485 for (int r
= 0; r
< this->next_temp
; r
++) {
3486 for (int c
= 0; c
< 4; c
++) {
3487 if (!writes
[4 * r
+ c
])
3490 if (write_level
[4 * r
+ c
] >= level
)
3491 writes
[4 * r
+ c
] = NULL
;
3496 case TGSI_OPCODE_IF
:
3498 /* fallthrough to default case to mark the condition as read */
3501 /* Continuing the block, clear any channels from the write array that
3502 * are read by this instruction.
3504 for (unsigned i
= 0; i
< Elements(inst
->src
); i
++) {
3505 if (inst
->src
[i
].file
== PROGRAM_TEMPORARY
&& inst
->src
[i
].reladdr
){
3506 /* Any temporary might be read, so no dead code elimination
3507 * across this instruction.
3509 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
3510 } else if (inst
->src
[i
].file
== PROGRAM_TEMPORARY
) {
3511 /* Clear where it's used as src. */
3512 int src_chans
= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 0);
3513 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 1);
3514 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 2);
3515 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 3);
3517 for (int c
= 0; c
< 4; c
++) {
3518 if (src_chans
& (1 << c
)) {
3519 writes
[4 * inst
->src
[i
].index
+ c
] = NULL
;
3527 /* If this instruction writes to a temporary, add it to the write array.
3528 * If there is already an instruction in the write array for one or more
3529 * of the channels, flag that channel write as dead.
3531 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&&
3532 !inst
->dst
.reladdr
&&
3534 for (int c
= 0; c
< 4; c
++) {
3535 if (inst
->dst
.writemask
& (1 << c
)) {
3536 if (writes
[4 * inst
->dst
.index
+ c
]) {
3537 if (write_level
[4 * inst
->dst
.index
+ c
] < level
)
3540 writes
[4 * inst
->dst
.index
+ c
]->dead_mask
|= (1 << c
);
3542 writes
[4 * inst
->dst
.index
+ c
] = inst
;
3543 write_level
[4 * inst
->dst
.index
+ c
] = level
;
3549 /* Anything still in the write array at this point is dead code. */
3550 for (int r
= 0; r
< this->next_temp
; r
++) {
3551 for (int c
= 0; c
< 4; c
++) {
3552 glsl_to_tgsi_instruction
*inst
= writes
[4 * r
+ c
];
3554 inst
->dead_mask
|= (1 << c
);
3558 /* Now actually remove the instructions that are completely dead and update
3559 * the writemask of other instructions with dead channels.
3561 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3562 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3564 if (!inst
->dead_mask
|| !inst
->dst
.writemask
)
3566 else if (inst
->dead_mask
== inst
->dst
.writemask
) {
3571 inst
->dst
.writemask
&= ~(inst
->dead_mask
);
3574 ralloc_free(write_level
);
3575 ralloc_free(writes
);
3580 /* Merges temporary registers together where possible to reduce the number of
3581 * registers needed to run a program.
3583 * Produces optimal code only after copy propagation and dead code elimination
3586 glsl_to_tgsi_visitor::merge_registers(void)
3588 int *last_reads
= rzalloc_array(mem_ctx
, int, this->next_temp
);
3589 int *first_writes
= rzalloc_array(mem_ctx
, int, this->next_temp
);
3592 /* Read the indices of the last read and first write to each temp register
3593 * into an array so that we don't have to traverse the instruction list as
3595 for (i
=0; i
< this->next_temp
; i
++) {
3596 last_reads
[i
] = get_last_temp_read(i
);
3597 first_writes
[i
] = get_first_temp_write(i
);
3600 /* Start looking for registers with non-overlapping usages that can be
3601 * merged together. */
3602 for (i
=0; i
< this->next_temp
; i
++) {
3603 /* Don't touch unused registers. */
3604 if (last_reads
[i
] < 0 || first_writes
[i
] < 0) continue;
3606 for (j
=0; j
< this->next_temp
; j
++) {
3607 /* Don't touch unused registers. */
3608 if (last_reads
[j
] < 0 || first_writes
[j
] < 0) continue;
3610 /* We can merge the two registers if the first write to j is after or
3611 * in the same instruction as the last read from i. Note that the
3612 * register at index i will always be used earlier or at the same time
3613 * as the register at index j. */
3614 if (first_writes
[i
] <= first_writes
[j
] &&
3615 last_reads
[i
] <= first_writes
[j
])
3617 rename_temp_register(j
, i
); /* Replace all references to j with i.*/
3619 /* Update the first_writes and last_reads arrays with the new
3620 * values for the merged register index, and mark the newly unused
3621 * register index as such. */
3622 last_reads
[i
] = last_reads
[j
];
3623 first_writes
[j
] = -1;
3629 ralloc_free(last_reads
);
3630 ralloc_free(first_writes
);
3633 /* Reassign indices to temporary registers by reusing unused indices created
3634 * by optimization passes. */
3636 glsl_to_tgsi_visitor::renumber_registers(void)
3641 for (i
=0; i
< this->next_temp
; i
++) {
3642 if (get_first_temp_read(i
) < 0) continue;
3644 rename_temp_register(i
, new_index
);
3648 this->next_temp
= new_index
;
3652 * Returns a fragment program which implements the current pixel transfer ops.
3653 * Based on get_pixel_transfer_program in st_atom_pixeltransfer.c.
3656 get_pixel_transfer_visitor(struct st_fragment_program
*fp
,
3657 glsl_to_tgsi_visitor
*original
,
3658 int scale_and_bias
, int pixel_maps
)
3660 glsl_to_tgsi_visitor
*v
= new glsl_to_tgsi_visitor();
3661 struct st_context
*st
= st_context(original
->ctx
);
3662 struct gl_program
*prog
= &fp
->Base
.Base
;
3663 struct gl_program_parameter_list
*params
= _mesa_new_parameter_list();
3664 st_src_reg coord
, src0
;
3666 glsl_to_tgsi_instruction
*inst
;
3668 /* Copy attributes of the glsl_to_tgsi_visitor in the original shader. */
3669 v
->ctx
= original
->ctx
;
3671 v
->glsl_version
= original
->glsl_version
;
3672 v
->native_integers
= original
->native_integers
;
3673 v
->options
= original
->options
;
3674 v
->next_temp
= original
->next_temp
;
3675 v
->num_address_regs
= original
->num_address_regs
;
3676 v
->samplers_used
= prog
->SamplersUsed
= original
->samplers_used
;
3677 v
->indirect_addr_temps
= original
->indirect_addr_temps
;
3678 v
->indirect_addr_consts
= original
->indirect_addr_consts
;
3679 memcpy(&v
->immediates
, &original
->immediates
, sizeof(v
->immediates
));
3682 * Get initial pixel color from the texture.
3683 * TEX colorTemp, fragment.texcoord[0], texture[0], 2D;
3685 coord
= st_src_reg(PROGRAM_INPUT
, FRAG_ATTRIB_TEX0
, glsl_type::vec2_type
);
3686 src0
= v
->get_temp(glsl_type::vec4_type
);
3687 dst0
= st_dst_reg(src0
);
3688 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, dst0
, coord
);
3690 inst
->tex_target
= TEXTURE_2D_INDEX
;
3692 prog
->InputsRead
|= (1 << FRAG_ATTRIB_TEX0
);
3693 prog
->SamplersUsed
|= (1 << 0); /* mark sampler 0 as used */
3694 v
->samplers_used
|= (1 << 0);
3696 if (scale_and_bias
) {
3697 static const gl_state_index scale_state
[STATE_LENGTH
] =
3698 { STATE_INTERNAL
, STATE_PT_SCALE
,
3699 (gl_state_index
) 0, (gl_state_index
) 0, (gl_state_index
) 0 };
3700 static const gl_state_index bias_state
[STATE_LENGTH
] =
3701 { STATE_INTERNAL
, STATE_PT_BIAS
,
3702 (gl_state_index
) 0, (gl_state_index
) 0, (gl_state_index
) 0 };
3703 GLint scale_p
, bias_p
;
3704 st_src_reg scale
, bias
;
3706 scale_p
= _mesa_add_state_reference(params
, scale_state
);
3707 bias_p
= _mesa_add_state_reference(params
, bias_state
);
3709 /* MAD colorTemp, colorTemp, scale, bias; */
3710 scale
= st_src_reg(PROGRAM_STATE_VAR
, scale_p
, GLSL_TYPE_FLOAT
);
3711 bias
= st_src_reg(PROGRAM_STATE_VAR
, bias_p
, GLSL_TYPE_FLOAT
);
3712 inst
= v
->emit(NULL
, TGSI_OPCODE_MAD
, dst0
, src0
, scale
, bias
);
3716 st_src_reg temp
= v
->get_temp(glsl_type::vec4_type
);
3717 st_dst_reg temp_dst
= st_dst_reg(temp
);
3719 assert(st
->pixel_xfer
.pixelmap_texture
);
3721 /* With a little effort, we can do four pixel map look-ups with
3722 * two TEX instructions:
3725 /* TEX temp.rg, colorTemp.rgba, texture[1], 2D; */
3726 temp_dst
.writemask
= WRITEMASK_XY
; /* write R,G */
3727 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, temp_dst
, src0
);
3729 inst
->tex_target
= TEXTURE_2D_INDEX
;
3731 /* TEX temp.ba, colorTemp.baba, texture[1], 2D; */
3732 src0
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_Z
, SWIZZLE_W
, SWIZZLE_Z
, SWIZZLE_W
);
3733 temp_dst
.writemask
= WRITEMASK_ZW
; /* write B,A */
3734 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, temp_dst
, src0
);
3736 inst
->tex_target
= TEXTURE_2D_INDEX
;
3738 prog
->SamplersUsed
|= (1 << 1); /* mark sampler 1 as used */
3739 v
->samplers_used
|= (1 << 1);
3741 /* MOV colorTemp, temp; */
3742 inst
= v
->emit(NULL
, TGSI_OPCODE_MOV
, dst0
, temp
);
3745 /* Now copy the instructions from the original glsl_to_tgsi_visitor into the
3747 foreach_iter(exec_list_iterator
, iter
, original
->instructions
) {
3748 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3749 st_src_reg src_regs
[3];
3751 if (inst
->dst
.file
== PROGRAM_OUTPUT
)
3752 prog
->OutputsWritten
|= BITFIELD64_BIT(inst
->dst
.index
);
3754 for (int i
=0; i
<3; i
++) {
3755 src_regs
[i
] = inst
->src
[i
];
3756 if (src_regs
[i
].file
== PROGRAM_INPUT
&&
3757 src_regs
[i
].index
== FRAG_ATTRIB_COL0
)
3759 src_regs
[i
].file
= PROGRAM_TEMPORARY
;
3760 src_regs
[i
].index
= src0
.index
;
3762 else 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_combine_parameter_lists(params
,
3771 original
->prog
->Parameters
);
3772 prog
->Attributes
= _mesa_clone_parameter_list(original
->prog
->Attributes
);
3773 prog
->Varying
= _mesa_clone_parameter_list(original
->prog
->Varying
);
3774 _mesa_free_parameter_list(params
);
3775 count_resources(v
, prog
);
3776 fp
->glsl_to_tgsi
= v
;
3780 * Make fragment program for glBitmap:
3781 * Sample the texture and kill the fragment if the bit is 0.
3782 * This program will be combined with the user's fragment program.
3784 * Based on make_bitmap_fragment_program in st_cb_bitmap.c.
3787 get_bitmap_visitor(struct st_fragment_program
*fp
,
3788 glsl_to_tgsi_visitor
*original
, int samplerIndex
)
3790 glsl_to_tgsi_visitor
*v
= new glsl_to_tgsi_visitor();
3791 struct st_context
*st
= st_context(original
->ctx
);
3792 struct gl_program
*prog
= &fp
->Base
.Base
;
3793 st_src_reg coord
, src0
;
3795 glsl_to_tgsi_instruction
*inst
;
3797 /* Copy attributes of the glsl_to_tgsi_visitor in the original shader. */
3798 v
->ctx
= original
->ctx
;
3800 v
->glsl_version
= original
->glsl_version
;
3801 v
->native_integers
= original
->native_integers
;
3802 v
->options
= original
->options
;
3803 v
->next_temp
= original
->next_temp
;
3804 v
->num_address_regs
= original
->num_address_regs
;
3805 v
->samplers_used
= prog
->SamplersUsed
= original
->samplers_used
;
3806 v
->indirect_addr_temps
= original
->indirect_addr_temps
;
3807 v
->indirect_addr_consts
= original
->indirect_addr_consts
;
3808 memcpy(&v
->immediates
, &original
->immediates
, sizeof(v
->immediates
));
3810 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
3811 coord
= st_src_reg(PROGRAM_INPUT
, FRAG_ATTRIB_TEX0
, glsl_type::vec2_type
);
3812 src0
= v
->get_temp(glsl_type::vec4_type
);
3813 dst0
= st_dst_reg(src0
);
3814 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, dst0
, coord
);
3815 inst
->sampler
= samplerIndex
;
3816 inst
->tex_target
= TEXTURE_2D_INDEX
;
3818 prog
->InputsRead
|= (1 << FRAG_ATTRIB_TEX0
);
3819 prog
->SamplersUsed
|= (1 << samplerIndex
); /* mark sampler as used */
3820 v
->samplers_used
|= (1 << samplerIndex
);
3822 /* KIL if -tmp0 < 0 # texel=0 -> keep / texel=0 -> discard */
3823 src0
.negate
= NEGATE_XYZW
;
3824 if (st
->bitmap
.tex_format
== PIPE_FORMAT_L8_UNORM
)
3825 src0
.swizzle
= SWIZZLE_XXXX
;
3826 inst
= v
->emit(NULL
, TGSI_OPCODE_KIL
, undef_dst
, src0
);
3828 /* Now copy the instructions from the original glsl_to_tgsi_visitor into the
3830 foreach_iter(exec_list_iterator
, iter
, original
->instructions
) {
3831 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3832 st_src_reg src_regs
[3];
3834 if (inst
->dst
.file
== PROGRAM_OUTPUT
)
3835 prog
->OutputsWritten
|= BITFIELD64_BIT(inst
->dst
.index
);
3837 for (int i
=0; i
<3; i
++) {
3838 src_regs
[i
] = inst
->src
[i
];
3839 if (src_regs
[i
].file
== PROGRAM_INPUT
)
3840 prog
->InputsRead
|= (1 << src_regs
[i
].index
);
3843 v
->emit(NULL
, inst
->op
, inst
->dst
, src_regs
[0], src_regs
[1], src_regs
[2]);
3846 /* Make modifications to fragment program info. */
3847 prog
->Parameters
= _mesa_clone_parameter_list(original
->prog
->Parameters
);
3848 prog
->Attributes
= _mesa_clone_parameter_list(original
->prog
->Attributes
);
3849 prog
->Varying
= _mesa_clone_parameter_list(original
->prog
->Varying
);
3850 count_resources(v
, prog
);
3851 fp
->glsl_to_tgsi
= v
;
3854 /* ------------------------- TGSI conversion stuff -------------------------- */
3856 unsigned branch_target
;
3861 * Intermediate state used during shader translation.
3863 struct st_translate
{
3864 struct ureg_program
*ureg
;
3866 struct ureg_dst temps
[MAX_TEMPS
];
3867 struct ureg_src
*constants
;
3868 struct ureg_src
*immediates
;
3869 struct ureg_dst outputs
[PIPE_MAX_SHADER_OUTPUTS
];
3870 struct ureg_src inputs
[PIPE_MAX_SHADER_INPUTS
];
3871 struct ureg_dst address
[1];
3872 struct ureg_src samplers
[PIPE_MAX_SAMPLERS
];
3873 struct ureg_src systemValues
[SYSTEM_VALUE_MAX
];
3875 /* Extra info for handling point size clamping in vertex shader */
3876 struct ureg_dst pointSizeResult
; /**< Actual point size output register */
3877 struct ureg_src pointSizeConst
; /**< Point size range constant register */
3878 GLint pointSizeOutIndex
; /**< Temp point size output register */
3879 GLboolean prevInstWrotePointSize
;
3881 const GLuint
*inputMapping
;
3882 const GLuint
*outputMapping
;
3884 /* For every instruction that contains a label (eg CALL), keep
3885 * details so that we can go back afterwards and emit the correct
3886 * tgsi instruction number for each label.
3888 struct label
*labels
;
3889 unsigned labels_size
;
3890 unsigned labels_count
;
3892 /* Keep a record of the tgsi instruction number that each mesa
3893 * instruction starts at, will be used to fix up labels after
3898 unsigned insn_count
;
3900 unsigned procType
; /**< TGSI_PROCESSOR_VERTEX/FRAGMENT */
3905 /** Map Mesa's SYSTEM_VALUE_x to TGSI_SEMANTIC_x */
3906 static unsigned mesa_sysval_to_semantic
[SYSTEM_VALUE_MAX
] = {
3908 TGSI_SEMANTIC_INSTANCEID
3912 * Make note of a branch to a label in the TGSI code.
3913 * After we've emitted all instructions, we'll go over the list
3914 * of labels built here and patch the TGSI code with the actual
3915 * location of each label.
3917 static unsigned *get_label(struct st_translate
*t
, unsigned branch_target
)
3921 if (t
->labels_count
+ 1 >= t
->labels_size
) {
3922 t
->labels_size
= 1 << (util_logbase2(t
->labels_size
) + 1);
3923 t
->labels
= (struct label
*)realloc(t
->labels
,
3924 t
->labels_size
* sizeof(struct label
));
3925 if (t
->labels
== NULL
) {
3926 static unsigned dummy
;
3932 i
= t
->labels_count
++;
3933 t
->labels
[i
].branch_target
= branch_target
;
3934 return &t
->labels
[i
].token
;
3938 * Called prior to emitting the TGSI code for each instruction.
3939 * Allocate additional space for instructions if needed.
3940 * Update the insn[] array so the next glsl_to_tgsi_instruction points to
3941 * the next TGSI instruction.
3943 static void set_insn_start(struct st_translate
*t
, unsigned start
)
3945 if (t
->insn_count
+ 1 >= t
->insn_size
) {
3946 t
->insn_size
= 1 << (util_logbase2(t
->insn_size
) + 1);
3947 t
->insn
= (unsigned *)realloc(t
->insn
, t
->insn_size
* sizeof(t
->insn
[0]));
3948 if (t
->insn
== NULL
) {
3954 t
->insn
[t
->insn_count
++] = start
;
3958 * Map a glsl_to_tgsi constant/immediate to a TGSI immediate.
3960 static struct ureg_src
3961 emit_immediate(struct st_translate
*t
,
3962 gl_constant_value values
[4],
3965 struct ureg_program
*ureg
= t
->ureg
;
3970 return ureg_DECL_immediate(ureg
, &values
[0].f
, size
);
3972 return ureg_DECL_immediate_int(ureg
, &values
[0].i
, size
);
3973 case GL_UNSIGNED_INT
:
3975 return ureg_DECL_immediate_uint(ureg
, &values
[0].u
, size
);
3977 assert(!"should not get here - type must be float, int, uint, or bool");
3978 return ureg_src_undef();
3983 * Map a glsl_to_tgsi dst register to a TGSI ureg_dst register.
3985 static struct ureg_dst
3986 dst_register(struct st_translate
*t
,
3987 gl_register_file file
,
3991 case PROGRAM_UNDEFINED
:
3992 return ureg_dst_undef();
3994 case PROGRAM_TEMPORARY
:
3995 if (ureg_dst_is_undef(t
->temps
[index
]))
3996 t
->temps
[index
] = ureg_DECL_temporary(t
->ureg
);
3998 return t
->temps
[index
];
4000 case PROGRAM_OUTPUT
:
4001 if (t
->procType
== TGSI_PROCESSOR_VERTEX
&& index
== VERT_RESULT_PSIZ
)
4002 t
->prevInstWrotePointSize
= GL_TRUE
;
4004 if (t
->procType
== TGSI_PROCESSOR_VERTEX
)
4005 assert(index
< VERT_RESULT_MAX
);
4006 else if (t
->procType
== TGSI_PROCESSOR_FRAGMENT
)
4007 assert(index
< FRAG_RESULT_MAX
);
4009 assert(index
< GEOM_RESULT_MAX
);
4011 assert(t
->outputMapping
[index
] < Elements(t
->outputs
));
4013 return t
->outputs
[t
->outputMapping
[index
]];
4015 case PROGRAM_ADDRESS
:
4016 return t
->address
[index
];
4019 assert(!"unknown dst register file");
4020 return ureg_dst_undef();
4025 * Map a glsl_to_tgsi src register to a TGSI ureg_src register.
4027 static struct ureg_src
4028 src_register(struct st_translate
*t
,
4029 gl_register_file file
,
4033 case PROGRAM_UNDEFINED
:
4034 return ureg_src_undef();
4036 case PROGRAM_TEMPORARY
:
4038 assert(index
< Elements(t
->temps
));
4039 if (ureg_dst_is_undef(t
->temps
[index
]))
4040 t
->temps
[index
] = ureg_DECL_temporary(t
->ureg
);
4041 return ureg_src(t
->temps
[index
]);
4043 case PROGRAM_NAMED_PARAM
:
4044 case PROGRAM_ENV_PARAM
:
4045 case PROGRAM_LOCAL_PARAM
:
4046 case PROGRAM_UNIFORM
:
4048 return t
->constants
[index
];
4049 case PROGRAM_STATE_VAR
:
4050 case PROGRAM_CONSTANT
: /* ie, immediate */
4052 return ureg_DECL_constant(t
->ureg
, 0);
4054 return t
->constants
[index
];
4056 case PROGRAM_IMMEDIATE
:
4057 return t
->immediates
[index
];
4060 assert(t
->inputMapping
[index
] < Elements(t
->inputs
));
4061 return t
->inputs
[t
->inputMapping
[index
]];
4063 case PROGRAM_OUTPUT
:
4064 assert(t
->outputMapping
[index
] < Elements(t
->outputs
));
4065 return ureg_src(t
->outputs
[t
->outputMapping
[index
]]); /* not needed? */
4067 case PROGRAM_ADDRESS
:
4068 return ureg_src(t
->address
[index
]);
4070 case PROGRAM_SYSTEM_VALUE
:
4071 assert(index
< Elements(t
->systemValues
));
4072 return t
->systemValues
[index
];
4075 assert(!"unknown src register file");
4076 return ureg_src_undef();
4081 * Create a TGSI ureg_dst register from an st_dst_reg.
4083 static struct ureg_dst
4084 translate_dst(struct st_translate
*t
,
4085 const st_dst_reg
*dst_reg
,
4088 struct ureg_dst dst
= dst_register(t
,
4092 dst
= ureg_writemask(dst
, dst_reg
->writemask
);
4095 dst
= ureg_saturate(dst
);
4097 if (dst_reg
->reladdr
!= NULL
)
4098 dst
= ureg_dst_indirect(dst
, ureg_src(t
->address
[0]));
4104 * Create a TGSI ureg_src register from an st_src_reg.
4106 static struct ureg_src
4107 translate_src(struct st_translate
*t
, const st_src_reg
*src_reg
)
4109 struct ureg_src src
= src_register(t
, src_reg
->file
, src_reg
->index
);
4111 src
= ureg_swizzle(src
,
4112 GET_SWZ(src_reg
->swizzle
, 0) & 0x3,
4113 GET_SWZ(src_reg
->swizzle
, 1) & 0x3,
4114 GET_SWZ(src_reg
->swizzle
, 2) & 0x3,
4115 GET_SWZ(src_reg
->swizzle
, 3) & 0x3);
4117 if ((src_reg
->negate
& 0xf) == NEGATE_XYZW
)
4118 src
= ureg_negate(src
);
4120 if (src_reg
->reladdr
!= NULL
) {
4121 /* Normally ureg_src_indirect() would be used here, but a stupid compiler
4122 * bug in g++ makes ureg_src_indirect (an inline C function) erroneously
4123 * set the bit for src.Negate. So we have to do the operation manually
4124 * here to work around the compiler's problems. */
4125 /*src = ureg_src_indirect(src, ureg_src(t->address[0]));*/
4126 struct ureg_src addr
= ureg_src(t
->address
[0]);
4128 src
.IndirectFile
= addr
.File
;
4129 src
.IndirectIndex
= addr
.Index
;
4130 src
.IndirectSwizzle
= addr
.SwizzleX
;
4132 if (src_reg
->file
!= PROGRAM_INPUT
&&
4133 src_reg
->file
!= PROGRAM_OUTPUT
) {
4134 /* If src_reg->index was negative, it was set to zero in
4135 * src_register(). Reassign it now. But don't do this
4136 * for input/output regs since they get remapped while
4137 * const buffers don't.
4139 src
.Index
= src_reg
->index
;
4147 compile_tgsi_instruction(struct st_translate
*t
,
4148 const struct glsl_to_tgsi_instruction
*inst
)
4150 struct ureg_program
*ureg
= t
->ureg
;
4152 struct ureg_dst dst
[1];
4153 struct ureg_src src
[4];
4157 num_dst
= num_inst_dst_regs(inst
->op
);
4158 num_src
= num_inst_src_regs(inst
->op
);
4161 dst
[0] = translate_dst(t
,
4165 for (i
= 0; i
< num_src
; i
++)
4166 src
[i
] = translate_src(t
, &inst
->src
[i
]);
4169 case TGSI_OPCODE_BGNLOOP
:
4170 case TGSI_OPCODE_CAL
:
4171 case TGSI_OPCODE_ELSE
:
4172 case TGSI_OPCODE_ENDLOOP
:
4173 case TGSI_OPCODE_IF
:
4174 assert(num_dst
== 0);
4175 ureg_label_insn(ureg
,
4179 inst
->op
== TGSI_OPCODE_CAL
? inst
->function
->sig_id
: 0));
4182 case TGSI_OPCODE_TEX
:
4183 case TGSI_OPCODE_TXB
:
4184 case TGSI_OPCODE_TXD
:
4185 case TGSI_OPCODE_TXL
:
4186 case TGSI_OPCODE_TXP
:
4187 src
[num_src
++] = t
->samplers
[inst
->sampler
];
4191 translate_texture_target(inst
->tex_target
, inst
->tex_shadow
),
4195 case TGSI_OPCODE_SCS
:
4196 dst
[0] = ureg_writemask(dst
[0], TGSI_WRITEMASK_XY
);
4197 ureg_insn(ureg
, inst
->op
, dst
, num_dst
, src
, num_src
);
4210 * Emit the TGSI instructions to adjust the WPOS pixel center convention
4211 * Basically, add (adjX, adjY) to the fragment position.
4214 emit_adjusted_wpos(struct st_translate
*t
,
4215 const struct gl_program
*program
,
4216 float adjX
, float adjY
)
4218 struct ureg_program
*ureg
= t
->ureg
;
4219 struct ureg_dst wpos_temp
= ureg_DECL_temporary(ureg
);
4220 struct ureg_src wpos_input
= t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_WPOS
]];
4222 /* Note that we bias X and Y and pass Z and W through unchanged.
4223 * The shader might also use gl_FragCoord.w and .z.
4225 ureg_ADD(ureg
, wpos_temp
, wpos_input
,
4226 ureg_imm4f(ureg
, adjX
, adjY
, 0.0f
, 0.0f
));
4228 t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_WPOS
]] = ureg_src(wpos_temp
);
4233 * Emit the TGSI instructions for inverting the WPOS y coordinate.
4234 * This code is unavoidable because it also depends on whether
4235 * a FBO is bound (STATE_FB_WPOS_Y_TRANSFORM).
4238 emit_wpos_inversion(struct st_translate
*t
,
4239 const struct gl_program
*program
,
4242 struct ureg_program
*ureg
= t
->ureg
;
4244 /* Fragment program uses fragment position input.
4245 * Need to replace instances of INPUT[WPOS] with temp T
4246 * where T = INPUT[WPOS] by y is inverted.
4248 static const gl_state_index wposTransformState
[STATE_LENGTH
]
4249 = { STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
,
4250 (gl_state_index
)0, (gl_state_index
)0, (gl_state_index
)0 };
4252 /* XXX: note we are modifying the incoming shader here! Need to
4253 * do this before emitting the constant decls below, or this
4256 unsigned wposTransConst
= _mesa_add_state_reference(program
->Parameters
,
4257 wposTransformState
);
4259 struct ureg_src wpostrans
= ureg_DECL_constant(ureg
, wposTransConst
);
4260 struct ureg_dst wpos_temp
;
4261 struct ureg_src wpos_input
= t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_WPOS
]];
4263 /* MOV wpos_temp, input[wpos]
4265 if (wpos_input
.File
== TGSI_FILE_TEMPORARY
)
4266 wpos_temp
= ureg_dst(wpos_input
);
4268 wpos_temp
= ureg_DECL_temporary(ureg
);
4269 ureg_MOV(ureg
, wpos_temp
, wpos_input
);
4273 /* MAD wpos_temp.y, wpos_input, wpostrans.xxxx, wpostrans.yyyy
4276 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
4278 ureg_scalar(wpostrans
, 0),
4279 ureg_scalar(wpostrans
, 1));
4281 /* MAD wpos_temp.y, wpos_input, wpostrans.zzzz, wpostrans.wwww
4284 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
4286 ureg_scalar(wpostrans
, 2),
4287 ureg_scalar(wpostrans
, 3));
4290 /* Use wpos_temp as position input from here on:
4292 t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_WPOS
]] = ureg_src(wpos_temp
);
4297 * Emit fragment position/ooordinate code.
4300 emit_wpos(struct st_context
*st
,
4301 struct st_translate
*t
,
4302 const struct gl_program
*program
,
4303 struct ureg_program
*ureg
)
4305 const struct gl_fragment_program
*fp
=
4306 (const struct gl_fragment_program
*) program
;
4307 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
4308 boolean invert
= FALSE
;
4310 if (fp
->OriginUpperLeft
) {
4311 /* Fragment shader wants origin in upper-left */
4312 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
)) {
4313 /* the driver supports upper-left origin */
4315 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
)) {
4316 /* the driver supports lower-left origin, need to invert Y */
4317 ureg_property_fs_coord_origin(ureg
, TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
4324 /* Fragment shader wants origin in lower-left */
4325 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
))
4326 /* the driver supports lower-left origin */
4327 ureg_property_fs_coord_origin(ureg
, TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
4328 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
))
4329 /* the driver supports upper-left origin, need to invert Y */
4335 if (fp
->PixelCenterInteger
) {
4336 /* Fragment shader wants pixel center integer */
4337 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
))
4338 /* the driver supports pixel center integer */
4339 ureg_property_fs_coord_pixel_center(ureg
, TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
4340 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
))
4341 /* the driver supports pixel center half integer, need to bias X,Y */
4342 emit_adjusted_wpos(t
, program
, 0.5f
, invert
? 0.5f
: -0.5f
);
4347 /* Fragment shader wants pixel center half integer */
4348 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
)) {
4349 /* the driver supports pixel center half integer */
4351 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)) {
4352 /* the driver supports pixel center integer, need to bias X,Y */
4353 ureg_property_fs_coord_pixel_center(ureg
, TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
4354 emit_adjusted_wpos(t
, program
, 0.5f
, invert
? -0.5f
: 0.5f
);
4360 /* we invert after adjustment so that we avoid the MOV to temporary,
4361 * and reuse the adjustment ADD instead */
4362 emit_wpos_inversion(t
, program
, invert
);
4366 * OpenGL's fragment gl_FrontFace input is 1 for front-facing, 0 for back.
4367 * TGSI uses +1 for front, -1 for back.
4368 * This function converts the TGSI value to the GL value. Simply clamping/
4369 * saturating the value to [0,1] does the job.
4372 emit_face_var(struct st_translate
*t
)
4374 struct ureg_program
*ureg
= t
->ureg
;
4375 struct ureg_dst face_temp
= ureg_DECL_temporary(ureg
);
4376 struct ureg_src face_input
= t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_FACE
]];
4378 /* MOV_SAT face_temp, input[face] */
4379 face_temp
= ureg_saturate(face_temp
);
4380 ureg_MOV(ureg
, face_temp
, face_input
);
4382 /* Use face_temp as face input from here on: */
4383 t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_FACE
]] = ureg_src(face_temp
);
4387 emit_edgeflags(struct st_translate
*t
)
4389 struct ureg_program
*ureg
= t
->ureg
;
4390 struct ureg_dst edge_dst
= t
->outputs
[t
->outputMapping
[VERT_RESULT_EDGE
]];
4391 struct ureg_src edge_src
= t
->inputs
[t
->inputMapping
[VERT_ATTRIB_EDGEFLAG
]];
4393 ureg_MOV(ureg
, edge_dst
, edge_src
);
4397 * Translate intermediate IR (glsl_to_tgsi_instruction) to TGSI format.
4398 * \param program the program to translate
4399 * \param numInputs number of input registers used
4400 * \param inputMapping maps Mesa fragment program inputs to TGSI generic
4402 * \param inputSemanticName the TGSI_SEMANTIC flag for each input
4403 * \param inputSemanticIndex the semantic index (ex: which texcoord) for
4405 * \param interpMode the TGSI_INTERPOLATE_LINEAR/PERSP mode for each input
4406 * \param numOutputs number of output registers used
4407 * \param outputMapping maps Mesa fragment program outputs to TGSI
4409 * \param outputSemanticName the TGSI_SEMANTIC flag for each output
4410 * \param outputSemanticIndex the semantic index (ex: which texcoord) for
4413 * \return PIPE_OK or PIPE_ERROR_OUT_OF_MEMORY
4415 extern "C" enum pipe_error
4416 st_translate_program(
4417 struct gl_context
*ctx
,
4419 struct ureg_program
*ureg
,
4420 glsl_to_tgsi_visitor
*program
,
4421 const struct gl_program
*proginfo
,
4423 const GLuint inputMapping
[],
4424 const ubyte inputSemanticName
[],
4425 const ubyte inputSemanticIndex
[],
4426 const GLuint interpMode
[],
4428 const GLuint outputMapping
[],
4429 const ubyte outputSemanticName
[],
4430 const ubyte outputSemanticIndex
[],
4431 boolean passthrough_edgeflags
)
4433 struct st_translate translate
, *t
;
4435 enum pipe_error ret
= PIPE_OK
;
4437 assert(numInputs
<= Elements(t
->inputs
));
4438 assert(numOutputs
<= Elements(t
->outputs
));
4441 memset(t
, 0, sizeof *t
);
4443 t
->procType
= procType
;
4444 t
->inputMapping
= inputMapping
;
4445 t
->outputMapping
= outputMapping
;
4447 t
->pointSizeOutIndex
= -1;
4448 t
->prevInstWrotePointSize
= GL_FALSE
;
4451 * Declare input attributes.
4453 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
4454 for (i
= 0; i
< numInputs
; i
++) {
4455 t
->inputs
[i
] = ureg_DECL_fs_input(ureg
,
4456 inputSemanticName
[i
],
4457 inputSemanticIndex
[i
],
4461 if (proginfo
->InputsRead
& FRAG_BIT_WPOS
) {
4462 /* Must do this after setting up t->inputs, and before
4463 * emitting constant references, below:
4465 emit_wpos(st_context(ctx
), t
, proginfo
, ureg
);
4468 if (proginfo
->InputsRead
& FRAG_BIT_FACE
)
4472 * Declare output attributes.
4474 for (i
= 0; i
< numOutputs
; i
++) {
4475 switch (outputSemanticName
[i
]) {
4476 case TGSI_SEMANTIC_POSITION
:
4477 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4478 TGSI_SEMANTIC_POSITION
, /* Z/Depth */
4479 outputSemanticIndex
[i
]);
4480 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_Z
);
4482 case TGSI_SEMANTIC_STENCIL
:
4483 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4484 TGSI_SEMANTIC_STENCIL
, /* Stencil */
4485 outputSemanticIndex
[i
]);
4486 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_Y
);
4488 case TGSI_SEMANTIC_COLOR
:
4489 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4490 TGSI_SEMANTIC_COLOR
,
4491 outputSemanticIndex
[i
]);
4494 assert(!"fragment shader outputs must be POSITION/STENCIL/COLOR");
4495 return PIPE_ERROR_BAD_INPUT
;
4499 else if (procType
== TGSI_PROCESSOR_GEOMETRY
) {
4500 for (i
= 0; i
< numInputs
; i
++) {
4501 t
->inputs
[i
] = ureg_DECL_gs_input(ureg
,
4503 inputSemanticName
[i
],
4504 inputSemanticIndex
[i
]);
4507 for (i
= 0; i
< numOutputs
; i
++) {
4508 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4509 outputSemanticName
[i
],
4510 outputSemanticIndex
[i
]);
4514 assert(procType
== TGSI_PROCESSOR_VERTEX
);
4516 for (i
= 0; i
< numInputs
; i
++) {
4517 t
->inputs
[i
] = ureg_DECL_vs_input(ureg
, i
);
4520 for (i
= 0; i
< numOutputs
; i
++) {
4521 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4522 outputSemanticName
[i
],
4523 outputSemanticIndex
[i
]);
4524 if ((outputSemanticName
[i
] == TGSI_SEMANTIC_PSIZE
) && proginfo
->Id
) {
4525 /* Writing to the point size result register requires special
4526 * handling to implement clamping.
4528 static const gl_state_index pointSizeClampState
[STATE_LENGTH
]
4529 = { STATE_INTERNAL
, STATE_POINT_SIZE_IMPL_CLAMP
, (gl_state_index
)0, (gl_state_index
)0, (gl_state_index
)0 };
4530 /* XXX: note we are modifying the incoming shader here! Need to
4531 * do this before emitting the constant decls below, or this
4534 unsigned pointSizeClampConst
=
4535 _mesa_add_state_reference(proginfo
->Parameters
,
4536 pointSizeClampState
);
4537 struct ureg_dst psizregtemp
= ureg_DECL_temporary(ureg
);
4538 t
->pointSizeConst
= ureg_DECL_constant(ureg
, pointSizeClampConst
);
4539 t
->pointSizeResult
= t
->outputs
[i
];
4540 t
->pointSizeOutIndex
= i
;
4541 t
->outputs
[i
] = psizregtemp
;
4544 if (passthrough_edgeflags
)
4548 /* Declare address register.
4550 if (program
->num_address_regs
> 0) {
4551 assert(program
->num_address_regs
== 1);
4552 t
->address
[0] = ureg_DECL_address(ureg
);
4555 /* Declare misc input registers
4558 GLbitfield sysInputs
= proginfo
->SystemValuesRead
;
4559 unsigned numSys
= 0;
4560 for (i
= 0; sysInputs
; i
++) {
4561 if (sysInputs
& (1 << i
)) {
4562 unsigned semName
= mesa_sysval_to_semantic
[i
];
4563 t
->systemValues
[i
] = ureg_DECL_system_value(ureg
, numSys
, semName
, 0);
4565 sysInputs
&= ~(1 << i
);
4570 if (program
->indirect_addr_temps
) {
4571 /* If temps are accessed with indirect addressing, declare temporaries
4572 * in sequential order. Else, we declare them on demand elsewhere.
4573 * (Note: the number of temporaries is equal to program->next_temp)
4575 for (i
= 0; i
< (unsigned)program
->next_temp
; i
++) {
4576 /* XXX use TGSI_FILE_TEMPORARY_ARRAY when it's supported by ureg */
4577 t
->temps
[i
] = ureg_DECL_temporary(t
->ureg
);
4581 /* Emit constants and uniforms. TGSI uses a single index space for these,
4582 * so we put all the translated regs in t->constants.
4584 if (proginfo
->Parameters
) {
4585 t
->constants
= (struct ureg_src
*)CALLOC(proginfo
->Parameters
->NumParameters
* sizeof(t
->constants
[0]));
4586 if (t
->constants
== NULL
) {
4587 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
4591 for (i
= 0; i
< proginfo
->Parameters
->NumParameters
; i
++) {
4592 switch (proginfo
->Parameters
->Parameters
[i
].Type
) {
4593 case PROGRAM_ENV_PARAM
:
4594 case PROGRAM_LOCAL_PARAM
:
4595 case PROGRAM_STATE_VAR
:
4596 case PROGRAM_NAMED_PARAM
:
4597 case PROGRAM_UNIFORM
:
4598 t
->constants
[i
] = ureg_DECL_constant(ureg
, i
);
4601 /* Emit immediates for PROGRAM_CONSTANT only when there's no indirect
4602 * addressing of the const buffer.
4603 * FIXME: Be smarter and recognize param arrays:
4604 * indirect addressing is only valid within the referenced
4607 case PROGRAM_CONSTANT
:
4608 if (program
->indirect_addr_consts
)
4609 t
->constants
[i
] = ureg_DECL_constant(ureg
, i
);
4611 t
->constants
[i
] = emit_immediate(t
,
4612 proginfo
->Parameters
->ParameterValues
[i
],
4613 proginfo
->Parameters
->Parameters
[i
].DataType
,
4622 /* Emit immediate values.
4624 t
->immediates
= (struct ureg_src
*)CALLOC(program
->num_immediates
* sizeof(struct ureg_src
));
4625 if (t
->immediates
== NULL
) {
4626 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
4630 foreach_iter(exec_list_iterator
, iter
, program
->immediates
) {
4631 immediate_storage
*imm
= (immediate_storage
*)iter
.get();
4632 t
->immediates
[i
++] = emit_immediate(t
, imm
->values
, imm
->type
, imm
->size
);
4635 /* texture samplers */
4636 for (i
= 0; i
< ctx
->Const
.MaxTextureImageUnits
; i
++) {
4637 if (program
->samplers_used
& (1 << i
)) {
4638 t
->samplers
[i
] = ureg_DECL_sampler(ureg
, i
);
4642 /* Emit each instruction in turn:
4644 foreach_iter(exec_list_iterator
, iter
, program
->instructions
) {
4645 set_insn_start(t
, ureg_get_instruction_number(ureg
));
4646 compile_tgsi_instruction(t
, (glsl_to_tgsi_instruction
*)iter
.get());
4648 if (t
->prevInstWrotePointSize
&& proginfo
->Id
) {
4649 /* The previous instruction wrote to the (fake) vertex point size
4650 * result register. Now we need to clamp that value to the min/max
4651 * point size range, putting the result into the real point size
4653 * Note that we can't do this easily at the end of program due to
4654 * possible early return.
4656 set_insn_start(t
, ureg_get_instruction_number(ureg
));
4658 ureg_writemask(t
->outputs
[t
->pointSizeOutIndex
], WRITEMASK_X
),
4659 ureg_src(t
->outputs
[t
->pointSizeOutIndex
]),
4660 ureg_swizzle(t
->pointSizeConst
, 1,1,1,1));
4661 ureg_MIN(t
->ureg
, ureg_writemask(t
->pointSizeResult
, WRITEMASK_X
),
4662 ureg_src(t
->outputs
[t
->pointSizeOutIndex
]),
4663 ureg_swizzle(t
->pointSizeConst
, 2,2,2,2));
4665 t
->prevInstWrotePointSize
= GL_FALSE
;
4668 /* Fix up all emitted labels:
4670 for (i
= 0; i
< t
->labels_count
; i
++) {
4671 ureg_fixup_label(ureg
, t
->labels
[i
].token
,
4672 t
->insn
[t
->labels
[i
].branch_target
]);
4679 FREE(t
->immediates
);
4682 debug_printf("%s: translate error flag set\n", __FUNCTION__
);
4687 /* ----------------------------- End TGSI code ------------------------------ */
4690 * Convert a shader's GLSL IR into a Mesa gl_program, although without
4691 * generating Mesa IR.
4693 static struct gl_program
*
4694 get_mesa_program(struct gl_context
*ctx
,
4695 struct gl_shader_program
*shader_program
,
4696 struct gl_shader
*shader
)
4698 glsl_to_tgsi_visitor
* v
= new glsl_to_tgsi_visitor();
4699 struct gl_program
*prog
;
4701 const char *target_string
;
4703 struct gl_shader_compiler_options
*options
=
4704 &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(shader
->Type
)];
4706 switch (shader
->Type
) {
4707 case GL_VERTEX_SHADER
:
4708 target
= GL_VERTEX_PROGRAM_ARB
;
4709 target_string
= "vertex";
4711 case GL_FRAGMENT_SHADER
:
4712 target
= GL_FRAGMENT_PROGRAM_ARB
;
4713 target_string
= "fragment";
4715 case GL_GEOMETRY_SHADER
:
4716 target
= GL_GEOMETRY_PROGRAM_NV
;
4717 target_string
= "geometry";
4720 assert(!"should not be reached");
4724 validate_ir_tree(shader
->ir
);
4726 prog
= ctx
->Driver
.NewProgram(ctx
, target
, shader_program
->Name
);
4729 prog
->Parameters
= _mesa_new_parameter_list();
4730 prog
->Varying
= _mesa_new_parameter_list();
4731 prog
->Attributes
= _mesa_new_parameter_list();
4734 v
->shader_program
= shader_program
;
4735 v
->options
= options
;
4736 v
->glsl_version
= ctx
->Const
.GLSLVersion
;
4737 v
->native_integers
= ctx
->Const
.NativeIntegers
;
4739 add_uniforms_to_parameters_list(shader_program
, shader
, prog
);
4741 /* Emit intermediate IR for main(). */
4742 visit_exec_list(shader
->ir
, v
);
4744 /* Now emit bodies for any functions that were used. */
4746 progress
= GL_FALSE
;
4748 foreach_iter(exec_list_iterator
, iter
, v
->function_signatures
) {
4749 function_entry
*entry
= (function_entry
*)iter
.get();
4751 if (!entry
->bgn_inst
) {
4752 v
->current_function
= entry
;
4754 entry
->bgn_inst
= v
->emit(NULL
, TGSI_OPCODE_BGNSUB
);
4755 entry
->bgn_inst
->function
= entry
;
4757 visit_exec_list(&entry
->sig
->body
, v
);
4759 glsl_to_tgsi_instruction
*last
;
4760 last
= (glsl_to_tgsi_instruction
*)v
->instructions
.get_tail();
4761 if (last
->op
!= TGSI_OPCODE_RET
)
4762 v
->emit(NULL
, TGSI_OPCODE_RET
);
4764 glsl_to_tgsi_instruction
*end
;
4765 end
= v
->emit(NULL
, TGSI_OPCODE_ENDSUB
);
4766 end
->function
= entry
;
4774 /* Print out some information (for debugging purposes) used by the
4775 * optimization passes. */
4776 for (i
=0; i
< v
->next_temp
; i
++) {
4777 int fr
= v
->get_first_temp_read(i
);
4778 int fw
= v
->get_first_temp_write(i
);
4779 int lr
= v
->get_last_temp_read(i
);
4780 int lw
= v
->get_last_temp_write(i
);
4782 printf("Temp %d: FR=%3d FW=%3d LR=%3d LW=%3d\n", i
, fr
, fw
, lr
, lw
);
4787 /* Remove reads to output registers, and to varyings in vertex shaders. */
4788 v
->remove_output_reads(PROGRAM_OUTPUT
);
4789 if (target
== GL_VERTEX_PROGRAM_ARB
)
4790 v
->remove_output_reads(PROGRAM_VARYING
);
4792 /* Perform optimizations on the instructions in the glsl_to_tgsi_visitor. */
4794 v
->copy_propagate();
4795 while (v
->eliminate_dead_code_advanced());
4797 /* FIXME: These passes to optimize temporary registers don't work when there
4798 * is indirect addressing of the temporary register space. We need proper
4799 * array support so that we don't have to give up these passes in every
4800 * shader that uses arrays.
4802 if (!v
->indirect_addr_temps
) {
4803 v
->eliminate_dead_code();
4804 v
->merge_registers();
4805 v
->renumber_registers();
4808 /* Write the END instruction. */
4809 v
->emit(NULL
, TGSI_OPCODE_END
);
4811 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
4813 printf("GLSL IR for linked %s program %d:\n", target_string
,
4814 shader_program
->Name
);
4815 _mesa_print_ir(shader
->ir
, NULL
);
4820 prog
->Instructions
= NULL
;
4821 prog
->NumInstructions
= 0;
4823 do_set_program_inouts(shader
->ir
, prog
);
4824 count_resources(v
, prog
);
4826 check_resources(ctx
, shader_program
, v
, prog
);
4828 _mesa_reference_program(ctx
, &shader
->Program
, prog
);
4830 struct st_vertex_program
*stvp
;
4831 struct st_fragment_program
*stfp
;
4832 struct st_geometry_program
*stgp
;
4834 switch (shader
->Type
) {
4835 case GL_VERTEX_SHADER
:
4836 stvp
= (struct st_vertex_program
*)prog
;
4837 stvp
->glsl_to_tgsi
= v
;
4839 case GL_FRAGMENT_SHADER
:
4840 stfp
= (struct st_fragment_program
*)prog
;
4841 stfp
->glsl_to_tgsi
= v
;
4843 case GL_GEOMETRY_SHADER
:
4844 stgp
= (struct st_geometry_program
*)prog
;
4845 stgp
->glsl_to_tgsi
= v
;
4848 assert(!"should not be reached");
4858 st_new_shader(struct gl_context
*ctx
, GLuint name
, GLuint type
)
4860 struct gl_shader
*shader
;
4861 assert(type
== GL_FRAGMENT_SHADER
|| type
== GL_VERTEX_SHADER
||
4862 type
== GL_GEOMETRY_SHADER_ARB
);
4863 shader
= rzalloc(NULL
, struct gl_shader
);
4865 shader
->Type
= type
;
4866 shader
->Name
= name
;
4867 _mesa_init_shader(ctx
, shader
);
4872 struct gl_shader_program
*
4873 st_new_shader_program(struct gl_context
*ctx
, GLuint name
)
4875 struct gl_shader_program
*shProg
;
4876 shProg
= rzalloc(NULL
, struct gl_shader_program
);
4878 shProg
->Name
= name
;
4879 _mesa_init_shader_program(ctx
, shProg
);
4886 * Called via ctx->Driver.LinkShader()
4887 * This actually involves converting GLSL IR into an intermediate TGSI-like IR
4888 * with code lowering and other optimizations.
4891 st_link_shader(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
4893 assert(prog
->LinkStatus
);
4895 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
4896 if (prog
->_LinkedShaders
[i
] == NULL
)
4900 exec_list
*ir
= prog
->_LinkedShaders
[i
]->ir
;
4901 const struct gl_shader_compiler_options
*options
=
4902 &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(prog
->_LinkedShaders
[i
]->Type
)];
4908 do_mat_op_to_vec(ir
);
4909 lower_instructions(ir
, (MOD_TO_FRACT
| DIV_TO_MUL_RCP
| EXP_TO_EXP2
4911 | ((options
->EmitNoPow
) ? POW_TO_EXP2
: 0)));
4913 progress
= do_lower_jumps(ir
, true, true, options
->EmitNoMainReturn
, options
->EmitNoCont
, options
->EmitNoLoops
) || progress
;
4915 progress
= do_common_optimization(ir
, true, options
->MaxUnrollIterations
) || progress
;
4917 progress
= lower_quadop_vector(ir
, false) || progress
;
4919 if (options
->EmitNoIfs
) {
4920 progress
= lower_discard(ir
) || progress
;
4921 progress
= lower_if_to_cond_assign(ir
) || progress
;
4924 if (options
->EmitNoNoise
)
4925 progress
= lower_noise(ir
) || progress
;
4927 /* If there are forms of indirect addressing that the driver
4928 * cannot handle, perform the lowering pass.
4930 if (options
->EmitNoIndirectInput
|| options
->EmitNoIndirectOutput
4931 || options
->EmitNoIndirectTemp
|| options
->EmitNoIndirectUniform
)
4933 lower_variable_index_to_cond_assign(ir
,
4934 options
->EmitNoIndirectInput
,
4935 options
->EmitNoIndirectOutput
,
4936 options
->EmitNoIndirectTemp
,
4937 options
->EmitNoIndirectUniform
)
4940 progress
= do_vec_index_to_cond_assign(ir
) || progress
;
4943 validate_ir_tree(ir
);
4946 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
4947 struct gl_program
*linked_prog
;
4949 if (prog
->_LinkedShaders
[i
] == NULL
)
4952 linked_prog
= get_mesa_program(ctx
, prog
, prog
->_LinkedShaders
[i
]);
4957 switch (prog
->_LinkedShaders
[i
]->Type
) {
4958 case GL_VERTEX_SHADER
:
4959 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
,
4960 (struct gl_vertex_program
*)linked_prog
);
4961 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_VERTEX_PROGRAM_ARB
,
4964 case GL_FRAGMENT_SHADER
:
4965 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
,
4966 (struct gl_fragment_program
*)linked_prog
);
4967 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
4970 case GL_GEOMETRY_SHADER
:
4971 _mesa_reference_geomprog(ctx
, &prog
->GeometryProgram
,
4972 (struct gl_geometry_program
*)linked_prog
);
4973 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_GEOMETRY_PROGRAM_NV
,
4982 _mesa_reference_program(ctx
, &linked_prog
, NULL
);
4990 * Link a GLSL shader program. Called via glLinkProgram().
4993 st_glsl_link_shader(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
4997 _mesa_clear_shader_program_data(ctx
, prog
);
4999 prog
->LinkStatus
= GL_TRUE
;
5001 for (i
= 0; i
< prog
->NumShaders
; i
++) {
5002 if (!prog
->Shaders
[i
]->CompileStatus
) {
5003 fail_link(prog
, "linking with uncompiled shader");
5004 prog
->LinkStatus
= GL_FALSE
;
5008 prog
->Varying
= _mesa_new_parameter_list();
5009 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
, NULL
);
5010 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
, NULL
);
5011 _mesa_reference_geomprog(ctx
, &prog
->GeometryProgram
, NULL
);
5013 if (prog
->LinkStatus
) {
5014 link_shaders(ctx
, prog
);
5017 if (prog
->LinkStatus
) {
5018 if (!ctx
->Driver
.LinkShader(ctx
, prog
)) {
5019 prog
->LinkStatus
= GL_FALSE
;
5023 set_uniform_initializers(ctx
, prog
);
5025 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
5026 if (!prog
->LinkStatus
) {
5027 printf("GLSL shader program %d failed to link\n", prog
->Name
);
5030 if (prog
->InfoLog
&& prog
->InfoLog
[0] != 0) {
5031 printf("GLSL shader program %d info log:\n", prog
->Name
);
5032 printf("%s\n", prog
->InfoLog
);