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
= NULL
;
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 function_entry
: public exec_node
{
234 ir_function_signature
*sig
;
237 * identifier of this function signature used by the program.
239 * At the point that Mesa instructions for function calls are
240 * generated, we don't know the address of the first instruction of
241 * the function body. So we make the BranchTarget that is called a
242 * small integer and rewrite them during set_branchtargets().
247 * Pointer to first instruction of the function body.
249 * Set during function body emits after main() is processed.
251 glsl_to_tgsi_instruction
*bgn_inst
;
254 * Index of the first instruction of the function body in actual
257 * Set after convertion from glsl_to_tgsi_instruction to prog_instruction.
261 /** Storage for the return value. */
262 st_src_reg return_reg
;
265 class glsl_to_tgsi_visitor
: public ir_visitor
{
267 glsl_to_tgsi_visitor();
268 ~glsl_to_tgsi_visitor();
270 function_entry
*current_function
;
272 struct gl_context
*ctx
;
273 struct gl_program
*prog
;
274 struct gl_shader_program
*shader_program
;
275 struct gl_shader_compiler_options
*options
;
276 struct gl_program_parameter_list
*immediates
;
280 int num_address_regs
;
282 bool indirect_addr_temps
;
283 bool indirect_addr_consts
;
287 variable_storage
*find_variable_storage(ir_variable
*var
);
289 function_entry
*get_function_signature(ir_function_signature
*sig
);
291 st_src_reg
get_temp(const glsl_type
*type
);
292 void reladdr_to_temp(ir_instruction
*ir
, st_src_reg
*reg
, int *num_reladdr
);
294 st_src_reg
st_src_reg_for_float(float val
);
295 st_src_reg
st_src_reg_for_int(int val
);
296 st_src_reg
st_src_reg_for_type(int type
, int val
);
299 * \name Visit methods
301 * As typical for the visitor pattern, there must be one \c visit method for
302 * each concrete subclass of \c ir_instruction. Virtual base classes within
303 * the hierarchy should not have \c visit methods.
306 virtual void visit(ir_variable
*);
307 virtual void visit(ir_loop
*);
308 virtual void visit(ir_loop_jump
*);
309 virtual void visit(ir_function_signature
*);
310 virtual void visit(ir_function
*);
311 virtual void visit(ir_expression
*);
312 virtual void visit(ir_swizzle
*);
313 virtual void visit(ir_dereference_variable
*);
314 virtual void visit(ir_dereference_array
*);
315 virtual void visit(ir_dereference_record
*);
316 virtual void visit(ir_assignment
*);
317 virtual void visit(ir_constant
*);
318 virtual void visit(ir_call
*);
319 virtual void visit(ir_return
*);
320 virtual void visit(ir_discard
*);
321 virtual void visit(ir_texture
*);
322 virtual void visit(ir_if
*);
327 /** List of variable_storage */
330 /** List of function_entry */
331 exec_list function_signatures
;
332 int next_signature_id
;
334 /** List of glsl_to_tgsi_instruction */
335 exec_list instructions
;
337 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
);
339 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
,
340 st_dst_reg dst
, st_src_reg src0
);
342 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
,
343 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
);
345 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
,
347 st_src_reg src0
, st_src_reg src1
, st_src_reg src2
);
349 unsigned get_opcode(ir_instruction
*ir
, unsigned op
,
351 st_src_reg src0
, st_src_reg src1
);
354 * Emit the correct dot-product instruction for the type of arguments
356 void emit_dp(ir_instruction
*ir
,
362 void emit_scalar(ir_instruction
*ir
, unsigned op
,
363 st_dst_reg dst
, st_src_reg src0
);
365 void emit_scalar(ir_instruction
*ir
, unsigned op
,
366 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
);
368 void emit_arl(ir_instruction
*ir
, st_dst_reg dst
, st_src_reg src0
);
370 void emit_scs(ir_instruction
*ir
, unsigned op
,
371 st_dst_reg dst
, const st_src_reg
&src
);
373 GLboolean
try_emit_mad(ir_expression
*ir
,
375 GLboolean
try_emit_sat(ir_expression
*ir
);
377 void emit_swz(ir_expression
*ir
);
379 bool process_move_condition(ir_rvalue
*ir
);
381 void remove_output_reads(gl_register_file type
);
382 void simplify_cmp(void);
384 void rename_temp_register(int index
, int new_index
);
385 int get_first_temp_read(int index
);
386 int get_first_temp_write(int index
);
387 int get_last_temp_read(int index
);
388 int get_last_temp_write(int index
);
390 void copy_propagate(void);
391 void eliminate_dead_code(void);
392 int eliminate_dead_code_advanced(void);
393 void merge_registers(void);
394 void renumber_registers(void);
399 static st_src_reg undef_src
= st_src_reg(PROGRAM_UNDEFINED
, 0, GLSL_TYPE_ERROR
);
401 static st_dst_reg undef_dst
= st_dst_reg(PROGRAM_UNDEFINED
, SWIZZLE_NOOP
, GLSL_TYPE_ERROR
);
403 static st_dst_reg address_reg
= st_dst_reg(PROGRAM_ADDRESS
, WRITEMASK_X
, GLSL_TYPE_FLOAT
);
406 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...) PRINTFLIKE(2, 3);
409 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...)
413 ralloc_vasprintf_append(&prog
->InfoLog
, fmt
, args
);
416 prog
->LinkStatus
= GL_FALSE
;
420 swizzle_for_size(int size
)
422 int size_swizzles
[4] = {
423 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
),
424 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
),
425 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
),
426 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
429 assert((size
>= 1) && (size
<= 4));
430 return size_swizzles
[size
- 1];
434 is_tex_instruction(unsigned opcode
)
436 const tgsi_opcode_info
* info
= tgsi_get_opcode_info(opcode
);
441 num_inst_dst_regs(unsigned opcode
)
443 const tgsi_opcode_info
* info
= tgsi_get_opcode_info(opcode
);
444 return info
->num_dst
;
448 num_inst_src_regs(unsigned opcode
)
450 const tgsi_opcode_info
* info
= tgsi_get_opcode_info(opcode
);
451 return info
->is_tex
? info
->num_src
- 1 : info
->num_src
;
454 glsl_to_tgsi_instruction
*
455 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
,
457 st_src_reg src0
, st_src_reg src1
, st_src_reg src2
)
459 glsl_to_tgsi_instruction
*inst
= new(mem_ctx
) glsl_to_tgsi_instruction();
460 int num_reladdr
= 0, i
;
462 op
= get_opcode(ir
, op
, dst
, src0
, src1
);
464 /* If we have to do relative addressing, we want to load the ARL
465 * reg directly for one of the regs, and preload the other reladdr
466 * sources into temps.
468 num_reladdr
+= dst
.reladdr
!= NULL
;
469 num_reladdr
+= src0
.reladdr
!= NULL
;
470 num_reladdr
+= src1
.reladdr
!= NULL
;
471 num_reladdr
+= src2
.reladdr
!= NULL
;
473 reladdr_to_temp(ir
, &src2
, &num_reladdr
);
474 reladdr_to_temp(ir
, &src1
, &num_reladdr
);
475 reladdr_to_temp(ir
, &src0
, &num_reladdr
);
478 emit_arl(ir
, address_reg
, *dst
.reladdr
);
481 assert(num_reladdr
== 0);
491 inst
->function
= NULL
;
493 if (op
== TGSI_OPCODE_ARL
)
494 this->num_address_regs
= 1;
496 /* Update indirect addressing status used by TGSI */
499 case PROGRAM_TEMPORARY
:
500 this->indirect_addr_temps
= true;
502 case PROGRAM_LOCAL_PARAM
:
503 case PROGRAM_ENV_PARAM
:
504 case PROGRAM_STATE_VAR
:
505 case PROGRAM_NAMED_PARAM
:
506 case PROGRAM_CONSTANT
:
507 case PROGRAM_UNIFORM
:
508 this->indirect_addr_consts
= true;
510 case PROGRAM_IMMEDIATE
:
511 assert(!"immediates should not have indirect addressing");
518 for (i
=0; i
<3; i
++) {
519 if(inst
->src
[i
].reladdr
) {
520 switch(inst
->src
[i
].file
) {
521 case PROGRAM_TEMPORARY
:
522 this->indirect_addr_temps
= true;
524 case PROGRAM_LOCAL_PARAM
:
525 case PROGRAM_ENV_PARAM
:
526 case PROGRAM_STATE_VAR
:
527 case PROGRAM_NAMED_PARAM
:
528 case PROGRAM_CONSTANT
:
529 case PROGRAM_UNIFORM
:
530 this->indirect_addr_consts
= true;
532 case PROGRAM_IMMEDIATE
:
533 assert(!"immediates should not have indirect addressing");
542 this->instructions
.push_tail(inst
);
548 glsl_to_tgsi_instruction
*
549 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
,
550 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
)
552 return emit(ir
, op
, dst
, src0
, src1
, undef_src
);
555 glsl_to_tgsi_instruction
*
556 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
,
557 st_dst_reg dst
, st_src_reg src0
)
559 assert(dst
.writemask
!= 0);
560 return emit(ir
, op
, dst
, src0
, undef_src
, undef_src
);
563 glsl_to_tgsi_instruction
*
564 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
)
566 return emit(ir
, op
, undef_dst
, undef_src
, undef_src
, undef_src
);
570 * Determines whether to use an integer, unsigned integer, or float opcode
571 * based on the operands and input opcode, then emits the result.
573 * TODO: type checking for remaining TGSI opcodes
576 glsl_to_tgsi_visitor::get_opcode(ir_instruction
*ir
, unsigned op
,
578 st_src_reg src0
, st_src_reg src1
)
580 int type
= GLSL_TYPE_FLOAT
;
582 if (src0
.type
== GLSL_TYPE_FLOAT
|| src1
.type
== GLSL_TYPE_FLOAT
)
583 type
= GLSL_TYPE_FLOAT
;
584 else if (glsl_version
>= 130)
587 #define case4(c, f, i, u) \
588 case TGSI_OPCODE_##c: \
589 if (type == GLSL_TYPE_INT) op = TGSI_OPCODE_##i; \
590 else if (type == GLSL_TYPE_UINT) op = TGSI_OPCODE_##u; \
591 else op = TGSI_OPCODE_##f; \
593 #define case3(f, i, u) case4(f, f, i, u)
594 #define case2fi(f, i) case4(f, f, i, i)
595 #define case2iu(i, u) case4(i, LAST, i, u)
601 case3(DIV
, IDIV
, UDIV
);
602 case3(MAX
, IMAX
, UMAX
);
603 case3(MIN
, IMIN
, UMIN
);
608 case3(SGE
, ISGE
, USGE
);
609 case3(SLT
, ISLT
, USLT
);
621 assert(op
!= TGSI_OPCODE_LAST
);
626 glsl_to_tgsi_visitor::emit_dp(ir_instruction
*ir
,
627 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
,
630 static const unsigned dot_opcodes
[] = {
631 TGSI_OPCODE_DP2
, TGSI_OPCODE_DP3
, TGSI_OPCODE_DP4
634 emit(ir
, dot_opcodes
[elements
- 2], dst
, src0
, src1
);
638 * Emits TGSI scalar opcodes to produce unique answers across channels.
640 * Some TGSI opcodes are scalar-only, like ARB_fp/vp. The src X
641 * channel determines the result across all channels. So to do a vec4
642 * of this operation, we want to emit a scalar per source channel used
643 * to produce dest channels.
646 glsl_to_tgsi_visitor::emit_scalar(ir_instruction
*ir
, unsigned op
,
648 st_src_reg orig_src0
, st_src_reg orig_src1
)
651 int done_mask
= ~dst
.writemask
;
653 /* TGSI RCP is a scalar operation splatting results to all channels,
654 * like ARB_fp/vp. So emit as many RCPs as necessary to cover our
657 for (i
= 0; i
< 4; i
++) {
658 GLuint this_mask
= (1 << i
);
659 glsl_to_tgsi_instruction
*inst
;
660 st_src_reg src0
= orig_src0
;
661 st_src_reg src1
= orig_src1
;
663 if (done_mask
& this_mask
)
666 GLuint src0_swiz
= GET_SWZ(src0
.swizzle
, i
);
667 GLuint src1_swiz
= GET_SWZ(src1
.swizzle
, i
);
668 for (j
= i
+ 1; j
< 4; j
++) {
669 /* If there is another enabled component in the destination that is
670 * derived from the same inputs, generate its value on this pass as
673 if (!(done_mask
& (1 << j
)) &&
674 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
&&
675 GET_SWZ(src1
.swizzle
, j
) == src1_swiz
) {
676 this_mask
|= (1 << j
);
679 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
680 src0_swiz
, src0_swiz
);
681 src1
.swizzle
= MAKE_SWIZZLE4(src1_swiz
, src1_swiz
,
682 src1_swiz
, src1_swiz
);
684 inst
= emit(ir
, op
, dst
, src0
, src1
);
685 inst
->dst
.writemask
= this_mask
;
686 done_mask
|= this_mask
;
691 glsl_to_tgsi_visitor::emit_scalar(ir_instruction
*ir
, unsigned op
,
692 st_dst_reg dst
, st_src_reg src0
)
694 st_src_reg undef
= undef_src
;
696 undef
.swizzle
= SWIZZLE_XXXX
;
698 emit_scalar(ir
, op
, dst
, src0
, undef
);
702 glsl_to_tgsi_visitor::emit_arl(ir_instruction
*ir
,
703 st_dst_reg dst
, st_src_reg src0
)
705 st_src_reg tmp
= get_temp(glsl_type::float_type
);
707 if (src0
.type
== GLSL_TYPE_INT
)
708 emit(NULL
, TGSI_OPCODE_I2F
, st_dst_reg(tmp
), src0
);
709 else if (src0
.type
== GLSL_TYPE_UINT
)
710 emit(NULL
, TGSI_OPCODE_U2F
, st_dst_reg(tmp
), src0
);
714 emit(NULL
, TGSI_OPCODE_ARL
, dst
, tmp
);
718 * Emit an TGSI_OPCODE_SCS instruction
720 * The \c SCS opcode functions a bit differently than the other TGSI opcodes.
721 * Instead of splatting its result across all four components of the
722 * destination, it writes one value to the \c x component and another value to
723 * the \c y component.
725 * \param ir IR instruction being processed
726 * \param op Either \c TGSI_OPCODE_SIN or \c TGSI_OPCODE_COS depending
727 * on which value is desired.
728 * \param dst Destination register
729 * \param src Source register
732 glsl_to_tgsi_visitor::emit_scs(ir_instruction
*ir
, unsigned op
,
734 const st_src_reg
&src
)
736 /* Vertex programs cannot use the SCS opcode.
738 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
739 emit_scalar(ir
, op
, dst
, src
);
743 const unsigned component
= (op
== TGSI_OPCODE_SIN
) ? 0 : 1;
744 const unsigned scs_mask
= (1U << component
);
745 int done_mask
= ~dst
.writemask
;
748 assert(op
== TGSI_OPCODE_SIN
|| op
== TGSI_OPCODE_COS
);
750 /* If there are compnents in the destination that differ from the component
751 * that will be written by the SCS instrution, we'll need a temporary.
753 if (scs_mask
!= unsigned(dst
.writemask
)) {
754 tmp
= get_temp(glsl_type::vec4_type
);
757 for (unsigned i
= 0; i
< 4; i
++) {
758 unsigned this_mask
= (1U << i
);
759 st_src_reg src0
= src
;
761 if ((done_mask
& this_mask
) != 0)
764 /* The source swizzle specified which component of the source generates
765 * sine / cosine for the current component in the destination. The SCS
766 * instruction requires that this value be swizzle to the X component.
767 * Replace the current swizzle with a swizzle that puts the source in
770 unsigned src0_swiz
= GET_SWZ(src
.swizzle
, i
);
772 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
773 src0_swiz
, src0_swiz
);
774 for (unsigned j
= i
+ 1; j
< 4; j
++) {
775 /* If there is another enabled component in the destination that is
776 * derived from the same inputs, generate its value on this pass as
779 if (!(done_mask
& (1 << j
)) &&
780 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
) {
781 this_mask
|= (1 << j
);
785 if (this_mask
!= scs_mask
) {
786 glsl_to_tgsi_instruction
*inst
;
787 st_dst_reg tmp_dst
= st_dst_reg(tmp
);
789 /* Emit the SCS instruction.
791 inst
= emit(ir
, TGSI_OPCODE_SCS
, tmp_dst
, src0
);
792 inst
->dst
.writemask
= scs_mask
;
794 /* Move the result of the SCS instruction to the desired location in
797 tmp
.swizzle
= MAKE_SWIZZLE4(component
, component
,
798 component
, component
);
799 inst
= emit(ir
, TGSI_OPCODE_SCS
, dst
, tmp
);
800 inst
->dst
.writemask
= this_mask
;
802 /* Emit the SCS instruction to write directly to the destination.
804 glsl_to_tgsi_instruction
*inst
= emit(ir
, TGSI_OPCODE_SCS
, dst
, src0
);
805 inst
->dst
.writemask
= scs_mask
;
808 done_mask
|= this_mask
;
813 glsl_to_tgsi_visitor::st_src_reg_for_float(float val
)
815 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_FLOAT
);
816 union gl_constant_value uval
;
819 src
.index
= _mesa_add_typed_unnamed_constant(this->immediates
, &uval
, 1,
820 GL_FLOAT
, &src
.swizzle
);
826 glsl_to_tgsi_visitor::st_src_reg_for_int(int val
)
828 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_INT
);
829 union gl_constant_value uval
;
831 assert(glsl_version
>= 130);
834 src
.index
= _mesa_add_typed_unnamed_constant(this->immediates
, &uval
, 1,
835 GL_INT
, &src
.swizzle
);
841 glsl_to_tgsi_visitor::st_src_reg_for_type(int type
, int val
)
843 if (glsl_version
>= 130)
844 return type
== GLSL_TYPE_FLOAT
? st_src_reg_for_float(val
) :
845 st_src_reg_for_int(val
);
847 return st_src_reg_for_float(val
);
851 type_size(const struct glsl_type
*type
)
856 switch (type
->base_type
) {
859 case GLSL_TYPE_FLOAT
:
861 if (type
->is_matrix()) {
862 return type
->matrix_columns
;
864 /* Regardless of size of vector, it gets a vec4. This is bad
865 * packing for things like floats, but otherwise arrays become a
866 * mess. Hopefully a later pass over the code can pack scalars
867 * down if appropriate.
871 case GLSL_TYPE_ARRAY
:
872 assert(type
->length
> 0);
873 return type_size(type
->fields
.array
) * type
->length
;
874 case GLSL_TYPE_STRUCT
:
876 for (i
= 0; i
< type
->length
; i
++) {
877 size
+= type_size(type
->fields
.structure
[i
].type
);
880 case GLSL_TYPE_SAMPLER
:
881 /* Samplers take up one slot in UNIFORMS[], but they're baked in
892 * In the initial pass of codegen, we assign temporary numbers to
893 * intermediate results. (not SSA -- variable assignments will reuse
897 glsl_to_tgsi_visitor::get_temp(const glsl_type
*type
)
903 src
.type
= glsl_version
>= 130 ? type
->base_type
: GLSL_TYPE_FLOAT
;
904 src
.file
= PROGRAM_TEMPORARY
;
905 src
.index
= next_temp
;
907 next_temp
+= type_size(type
);
909 if (type
->is_array() || type
->is_record()) {
910 src
.swizzle
= SWIZZLE_NOOP
;
912 for (i
= 0; i
< type
->vector_elements
; i
++)
915 swizzle
[i
] = type
->vector_elements
- 1;
916 src
.swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1],
917 swizzle
[2], swizzle
[3]);
925 glsl_to_tgsi_visitor::find_variable_storage(ir_variable
*var
)
928 variable_storage
*entry
;
930 foreach_iter(exec_list_iterator
, iter
, this->variables
) {
931 entry
= (variable_storage
*)iter
.get();
933 if (entry
->var
== var
)
941 glsl_to_tgsi_visitor::visit(ir_variable
*ir
)
943 if (strcmp(ir
->name
, "gl_FragCoord") == 0) {
944 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
946 fp
->OriginUpperLeft
= ir
->origin_upper_left
;
947 fp
->PixelCenterInteger
= ir
->pixel_center_integer
;
949 } else if (strcmp(ir
->name
, "gl_FragDepth") == 0) {
950 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
951 switch (ir
->depth_layout
) {
952 case ir_depth_layout_none
:
953 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_NONE
;
955 case ir_depth_layout_any
:
956 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_ANY
;
958 case ir_depth_layout_greater
:
959 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_GREATER
;
961 case ir_depth_layout_less
:
962 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_LESS
;
964 case ir_depth_layout_unchanged
:
965 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
973 if (ir
->mode
== ir_var_uniform
&& strncmp(ir
->name
, "gl_", 3) == 0) {
975 const ir_state_slot
*const slots
= ir
->state_slots
;
976 assert(ir
->state_slots
!= NULL
);
978 /* Check if this statevar's setup in the STATE file exactly
979 * matches how we'll want to reference it as a
980 * struct/array/whatever. If not, then we need to move it into
981 * temporary storage and hope that it'll get copy-propagated
984 for (i
= 0; i
< ir
->num_state_slots
; i
++) {
985 if (slots
[i
].swizzle
!= SWIZZLE_XYZW
) {
990 struct variable_storage
*storage
;
992 if (i
== ir
->num_state_slots
) {
993 /* We'll set the index later. */
994 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_STATE_VAR
, -1);
995 this->variables
.push_tail(storage
);
999 /* The variable_storage constructor allocates slots based on the size
1000 * of the type. However, this had better match the number of state
1001 * elements that we're going to copy into the new temporary.
1003 assert((int) ir
->num_state_slots
== type_size(ir
->type
));
1005 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_TEMPORARY
,
1007 this->variables
.push_tail(storage
);
1008 this->next_temp
+= type_size(ir
->type
);
1010 dst
= st_dst_reg(st_src_reg(PROGRAM_TEMPORARY
, storage
->index
,
1011 glsl_version
>= 130 ? ir
->type
->base_type
: GLSL_TYPE_FLOAT
));
1015 for (unsigned int i
= 0; i
< ir
->num_state_slots
; i
++) {
1016 int index
= _mesa_add_state_reference(this->prog
->Parameters
,
1017 (gl_state_index
*)slots
[i
].tokens
);
1019 if (storage
->file
== PROGRAM_STATE_VAR
) {
1020 if (storage
->index
== -1) {
1021 storage
->index
= index
;
1023 assert(index
== storage
->index
+ (int)i
);
1026 st_src_reg
src(PROGRAM_STATE_VAR
, index
,
1027 glsl_version
>= 130 ? ir
->type
->base_type
: GLSL_TYPE_FLOAT
);
1028 src
.swizzle
= slots
[i
].swizzle
;
1029 emit(ir
, TGSI_OPCODE_MOV
, dst
, src
);
1030 /* even a float takes up a whole vec4 reg in a struct/array. */
1035 if (storage
->file
== PROGRAM_TEMPORARY
&&
1036 dst
.index
!= storage
->index
+ (int) ir
->num_state_slots
) {
1037 fail_link(this->shader_program
,
1038 "failed to load builtin uniform `%s' (%d/%d regs loaded)\n",
1039 ir
->name
, dst
.index
- storage
->index
,
1040 type_size(ir
->type
));
1046 glsl_to_tgsi_visitor::visit(ir_loop
*ir
)
1048 ir_dereference_variable
*counter
= NULL
;
1050 if (ir
->counter
!= NULL
)
1051 counter
= new(ir
) ir_dereference_variable(ir
->counter
);
1053 if (ir
->from
!= NULL
) {
1054 assert(ir
->counter
!= NULL
);
1056 ir_assignment
*a
= new(ir
) ir_assignment(counter
, ir
->from
, NULL
);
1062 emit(NULL
, TGSI_OPCODE_BGNLOOP
);
1066 new(ir
) ir_expression(ir
->cmp
, glsl_type::bool_type
,
1068 ir_if
*if_stmt
= new(ir
) ir_if(e
);
1070 ir_loop_jump
*brk
= new(ir
) ir_loop_jump(ir_loop_jump::jump_break
);
1072 if_stmt
->then_instructions
.push_tail(brk
);
1074 if_stmt
->accept(this);
1081 visit_exec_list(&ir
->body_instructions
, this);
1083 if (ir
->increment
) {
1085 new(ir
) ir_expression(ir_binop_add
, counter
->type
,
1086 counter
, ir
->increment
);
1088 ir_assignment
*a
= new(ir
) ir_assignment(counter
, e
, NULL
);
1095 emit(NULL
, TGSI_OPCODE_ENDLOOP
);
1099 glsl_to_tgsi_visitor::visit(ir_loop_jump
*ir
)
1102 case ir_loop_jump::jump_break
:
1103 emit(NULL
, TGSI_OPCODE_BRK
);
1105 case ir_loop_jump::jump_continue
:
1106 emit(NULL
, TGSI_OPCODE_CONT
);
1113 glsl_to_tgsi_visitor::visit(ir_function_signature
*ir
)
1120 glsl_to_tgsi_visitor::visit(ir_function
*ir
)
1122 /* Ignore function bodies other than main() -- we shouldn't see calls to
1123 * them since they should all be inlined before we get to glsl_to_tgsi.
1125 if (strcmp(ir
->name
, "main") == 0) {
1126 const ir_function_signature
*sig
;
1129 sig
= ir
->matching_signature(&empty
);
1133 foreach_iter(exec_list_iterator
, iter
, sig
->body
) {
1134 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
1142 glsl_to_tgsi_visitor::try_emit_mad(ir_expression
*ir
, int mul_operand
)
1144 int nonmul_operand
= 1 - mul_operand
;
1146 st_dst_reg result_dst
;
1148 ir_expression
*expr
= ir
->operands
[mul_operand
]->as_expression();
1149 if (!expr
|| expr
->operation
!= ir_binop_mul
)
1152 expr
->operands
[0]->accept(this);
1154 expr
->operands
[1]->accept(this);
1156 ir
->operands
[nonmul_operand
]->accept(this);
1159 this->result
= get_temp(ir
->type
);
1160 result_dst
= st_dst_reg(this->result
);
1161 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1162 emit(ir
, TGSI_OPCODE_MAD
, result_dst
, a
, b
, c
);
1168 glsl_to_tgsi_visitor::try_emit_sat(ir_expression
*ir
)
1170 /* Saturates were only introduced to vertex programs in
1171 * NV_vertex_program3, so don't give them to drivers in the VP.
1173 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
)
1176 ir_rvalue
*sat_src
= ir
->as_rvalue_to_saturate();
1180 sat_src
->accept(this);
1181 st_src_reg src
= this->result
;
1183 this->result
= get_temp(ir
->type
);
1184 st_dst_reg result_dst
= st_dst_reg(this->result
);
1185 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1186 glsl_to_tgsi_instruction
*inst
;
1187 inst
= emit(ir
, TGSI_OPCODE_MOV
, result_dst
, src
);
1188 inst
->saturate
= true;
1194 glsl_to_tgsi_visitor::reladdr_to_temp(ir_instruction
*ir
,
1195 st_src_reg
*reg
, int *num_reladdr
)
1200 emit_arl(ir
, address_reg
, *reg
->reladdr
);
1202 if (*num_reladdr
!= 1) {
1203 st_src_reg temp
= get_temp(glsl_type::vec4_type
);
1205 emit(ir
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), *reg
);
1213 glsl_to_tgsi_visitor::visit(ir_expression
*ir
)
1215 unsigned int operand
;
1216 st_src_reg op
[Elements(ir
->operands
)];
1217 st_src_reg result_src
;
1218 st_dst_reg result_dst
;
1220 /* Quick peephole: Emit MAD(a, b, c) instead of ADD(MUL(a, b), c)
1222 if (ir
->operation
== ir_binop_add
) {
1223 if (try_emit_mad(ir
, 1))
1225 if (try_emit_mad(ir
, 0))
1228 if (try_emit_sat(ir
))
1231 if (ir
->operation
== ir_quadop_vector
)
1232 assert(!"ir_quadop_vector should have been lowered");
1234 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
1235 this->result
.file
= PROGRAM_UNDEFINED
;
1236 ir
->operands
[operand
]->accept(this);
1237 if (this->result
.file
== PROGRAM_UNDEFINED
) {
1239 printf("Failed to get tree for expression operand:\n");
1240 ir
->operands
[operand
]->accept(&v
);
1243 op
[operand
] = this->result
;
1245 /* Matrix expression operands should have been broken down to vector
1246 * operations already.
1248 assert(!ir
->operands
[operand
]->type
->is_matrix());
1251 int vector_elements
= ir
->operands
[0]->type
->vector_elements
;
1252 if (ir
->operands
[1]) {
1253 vector_elements
= MAX2(vector_elements
,
1254 ir
->operands
[1]->type
->vector_elements
);
1257 this->result
.file
= PROGRAM_UNDEFINED
;
1259 /* Storage for our result. Ideally for an assignment we'd be using
1260 * the actual storage for the result here, instead.
1262 result_src
= get_temp(ir
->type
);
1263 /* convenience for the emit functions below. */
1264 result_dst
= st_dst_reg(result_src
);
1265 /* Limit writes to the channels that will be used by result_src later.
1266 * This does limit this temp's use as a temporary for multi-instruction
1269 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1271 switch (ir
->operation
) {
1272 case ir_unop_logic_not
:
1273 emit(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], st_src_reg_for_type(result_dst
.type
, 0));
1276 assert(result_dst
.type
== GLSL_TYPE_FLOAT
|| result_dst
.type
== GLSL_TYPE_INT
);
1277 if (result_dst
.type
== GLSL_TYPE_INT
)
1278 emit(ir
, TGSI_OPCODE_INEG
, result_dst
, op
[0]);
1280 op
[0].negate
= ~op
[0].negate
;
1285 assert(result_dst
.type
== GLSL_TYPE_FLOAT
);
1286 emit(ir
, TGSI_OPCODE_ABS
, result_dst
, op
[0]);
1289 emit(ir
, TGSI_OPCODE_SSG
, result_dst
, op
[0]);
1292 emit_scalar(ir
, TGSI_OPCODE_RCP
, result_dst
, op
[0]);
1296 emit_scalar(ir
, TGSI_OPCODE_EX2
, result_dst
, op
[0]);
1300 assert(!"not reached: should be handled by ir_explog_to_explog2");
1303 emit_scalar(ir
, TGSI_OPCODE_LG2
, result_dst
, op
[0]);
1306 emit_scalar(ir
, TGSI_OPCODE_SIN
, result_dst
, op
[0]);
1309 emit_scalar(ir
, TGSI_OPCODE_COS
, result_dst
, op
[0]);
1311 case ir_unop_sin_reduced
:
1312 emit_scs(ir
, TGSI_OPCODE_SIN
, result_dst
, op
[0]);
1314 case ir_unop_cos_reduced
:
1315 emit_scs(ir
, TGSI_OPCODE_COS
, result_dst
, op
[0]);
1319 emit(ir
, TGSI_OPCODE_DDX
, result_dst
, op
[0]);
1322 op
[0].negate
= ~op
[0].negate
;
1323 emit(ir
, TGSI_OPCODE_DDY
, result_dst
, op
[0]);
1326 case ir_unop_noise
: {
1327 /* At some point, a motivated person could add a better
1328 * implementation of noise. Currently not even the nvidia
1329 * binary drivers do anything more than this. In any case, the
1330 * place to do this is in the GL state tracker, not the poor
1333 emit(ir
, TGSI_OPCODE_MOV
, result_dst
, st_src_reg_for_float(0.5));
1338 emit(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1341 emit(ir
, TGSI_OPCODE_SUB
, result_dst
, op
[0], op
[1]);
1345 emit(ir
, TGSI_OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1348 if (result_dst
.type
== GLSL_TYPE_FLOAT
)
1349 assert(!"not reached: should be handled by ir_div_to_mul_rcp");
1351 emit(ir
, TGSI_OPCODE_DIV
, result_dst
, op
[0], op
[1]);
1354 if (result_dst
.type
== GLSL_TYPE_FLOAT
)
1355 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
1357 emit(ir
, TGSI_OPCODE_MOD
, result_dst
, op
[0], op
[1]);
1361 emit(ir
, TGSI_OPCODE_SLT
, result_dst
, op
[0], op
[1]);
1363 case ir_binop_greater
:
1364 emit(ir
, TGSI_OPCODE_SGT
, result_dst
, op
[0], op
[1]);
1366 case ir_binop_lequal
:
1367 emit(ir
, TGSI_OPCODE_SLE
, result_dst
, op
[0], op
[1]);
1369 case ir_binop_gequal
:
1370 emit(ir
, TGSI_OPCODE_SGE
, result_dst
, op
[0], op
[1]);
1372 case ir_binop_equal
:
1373 emit(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1375 case ir_binop_nequal
:
1376 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1378 case ir_binop_all_equal
:
1379 /* "==" operator producing a scalar boolean. */
1380 if (ir
->operands
[0]->type
->is_vector() ||
1381 ir
->operands
[1]->type
->is_vector()) {
1382 st_src_reg temp
= get_temp(glsl_version
>= 130 ?
1383 glsl_type::get_instance(ir
->operands
[0]->type
->base_type
, 4, 1) :
1384 glsl_type::vec4_type
);
1385 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
1386 emit(ir
, TGSI_OPCODE_SNE
, st_dst_reg(temp
), op
[0], op
[1]);
1387 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1388 emit(ir
, TGSI_OPCODE_SEQ
, result_dst
, result_src
, st_src_reg_for_float(0.0));
1390 emit(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1393 case ir_binop_any_nequal
:
1394 /* "!=" operator producing a scalar boolean. */
1395 if (ir
->operands
[0]->type
->is_vector() ||
1396 ir
->operands
[1]->type
->is_vector()) {
1397 st_src_reg temp
= get_temp(glsl_version
>= 130 ?
1398 glsl_type::get_instance(ir
->operands
[0]->type
->base_type
, 4, 1) :
1399 glsl_type::vec4_type
);
1400 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
1401 emit(ir
, TGSI_OPCODE_SNE
, st_dst_reg(temp
), op
[0], op
[1]);
1402 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1403 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, result_src
, st_src_reg_for_float(0.0));
1405 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1410 assert(ir
->operands
[0]->type
->is_vector());
1411 emit_dp(ir
, result_dst
, op
[0], op
[0],
1412 ir
->operands
[0]->type
->vector_elements
);
1413 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, result_src
, st_src_reg_for_float(0.0));
1416 case ir_binop_logic_xor
:
1417 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1420 case ir_binop_logic_or
:
1421 /* This could be a saturated add and skip the SNE. */
1422 emit(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1423 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, result_src
, st_src_reg_for_float(0.0));
1426 case ir_binop_logic_and
:
1427 /* the bool args are stored as float 0.0 or 1.0, so "mul" gives us "and". */
1428 emit(ir
, TGSI_OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1432 assert(ir
->operands
[0]->type
->is_vector());
1433 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
1434 emit_dp(ir
, result_dst
, op
[0], op
[1],
1435 ir
->operands
[0]->type
->vector_elements
);
1439 /* sqrt(x) = x * rsq(x). */
1440 emit_scalar(ir
, TGSI_OPCODE_RSQ
, result_dst
, op
[0]);
1441 emit(ir
, TGSI_OPCODE_MUL
, result_dst
, result_src
, op
[0]);
1442 /* For incoming channels <= 0, set the result to 0. */
1443 op
[0].negate
= ~op
[0].negate
;
1444 emit(ir
, TGSI_OPCODE_CMP
, result_dst
,
1445 op
[0], result_src
, st_src_reg_for_float(0.0));
1448 emit_scalar(ir
, TGSI_OPCODE_RSQ
, result_dst
, op
[0]);
1452 if (glsl_version
>= 130) {
1453 emit(ir
, TGSI_OPCODE_I2F
, result_dst
, op
[0]);
1457 /* Booleans are stored as integers (or floats in GLSL 1.20 and lower). */
1461 if (glsl_version
>= 130)
1462 emit(ir
, TGSI_OPCODE_F2I
, result_dst
, op
[0]);
1464 emit(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
1468 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0],
1469 st_src_reg_for_type(result_dst
.type
, 0));
1472 emit(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
1475 op
[0].negate
= ~op
[0].negate
;
1476 emit(ir
, TGSI_OPCODE_FLR
, result_dst
, op
[0]);
1477 result_src
.negate
= ~result_src
.negate
;
1480 emit(ir
, TGSI_OPCODE_FLR
, result_dst
, op
[0]);
1483 emit(ir
, TGSI_OPCODE_FRC
, result_dst
, op
[0]);
1487 emit(ir
, TGSI_OPCODE_MIN
, result_dst
, op
[0], op
[1]);
1490 emit(ir
, TGSI_OPCODE_MAX
, result_dst
, op
[0], op
[1]);
1493 emit_scalar(ir
, TGSI_OPCODE_POW
, result_dst
, op
[0], op
[1]);
1496 case ir_unop_bit_not
:
1497 if (glsl_version
>= 130) {
1498 emit(ir
, TGSI_OPCODE_NOT
, result_dst
, op
[0]);
1502 if (glsl_version
>= 130) {
1503 emit(ir
, TGSI_OPCODE_U2F
, result_dst
, op
[0]);
1506 case ir_binop_lshift
:
1507 if (glsl_version
>= 130) {
1508 emit(ir
, TGSI_OPCODE_SHL
, result_dst
, op
[0]);
1511 case ir_binop_rshift
:
1512 if (glsl_version
>= 130) {
1513 emit(ir
, TGSI_OPCODE_ISHR
, result_dst
, op
[0]);
1516 case ir_binop_bit_and
:
1517 if (glsl_version
>= 130) {
1518 emit(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0]);
1521 case ir_binop_bit_xor
:
1522 if (glsl_version
>= 130) {
1523 emit(ir
, TGSI_OPCODE_XOR
, result_dst
, op
[0]);
1526 case ir_binop_bit_or
:
1527 if (glsl_version
>= 130) {
1528 emit(ir
, TGSI_OPCODE_OR
, result_dst
, op
[0]);
1531 case ir_unop_round_even
:
1532 assert(!"GLSL 1.30 features unsupported");
1535 case ir_quadop_vector
:
1536 /* This operation should have already been handled.
1538 assert(!"Should not get here.");
1542 this->result
= result_src
;
1547 glsl_to_tgsi_visitor::visit(ir_swizzle
*ir
)
1553 /* Note that this is only swizzles in expressions, not those on the left
1554 * hand side of an assignment, which do write masking. See ir_assignment
1558 ir
->val
->accept(this);
1560 assert(src
.file
!= PROGRAM_UNDEFINED
);
1562 for (i
= 0; i
< 4; i
++) {
1563 if (i
< ir
->type
->vector_elements
) {
1566 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.x
);
1569 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.y
);
1572 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.z
);
1575 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.w
);
1579 /* If the type is smaller than a vec4, replicate the last
1582 swizzle
[i
] = swizzle
[ir
->type
->vector_elements
- 1];
1586 src
.swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
1592 glsl_to_tgsi_visitor::visit(ir_dereference_variable
*ir
)
1594 variable_storage
*entry
= find_variable_storage(ir
->var
);
1595 ir_variable
*var
= ir
->var
;
1598 switch (var
->mode
) {
1599 case ir_var_uniform
:
1600 entry
= new(mem_ctx
) variable_storage(var
, PROGRAM_UNIFORM
,
1602 this->variables
.push_tail(entry
);
1606 /* The linker assigns locations for varyings and attributes,
1607 * including deprecated builtins (like gl_Color), user-assign
1608 * generic attributes (glBindVertexLocation), and
1609 * user-defined varyings.
1611 * FINISHME: We would hit this path for function arguments. Fix!
1613 assert(var
->location
!= -1);
1614 entry
= new(mem_ctx
) variable_storage(var
,
1617 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
&&
1618 var
->location
>= VERT_ATTRIB_GENERIC0
) {
1619 _mesa_add_attribute(this->prog
->Attributes
,
1621 _mesa_sizeof_glsl_type(var
->type
->gl_type
),
1623 var
->location
- VERT_ATTRIB_GENERIC0
);
1627 assert(var
->location
!= -1);
1628 entry
= new(mem_ctx
) variable_storage(var
,
1632 case ir_var_system_value
:
1633 entry
= new(mem_ctx
) variable_storage(var
,
1634 PROGRAM_SYSTEM_VALUE
,
1638 case ir_var_temporary
:
1639 entry
= new(mem_ctx
) variable_storage(var
, PROGRAM_TEMPORARY
,
1641 this->variables
.push_tail(entry
);
1643 next_temp
+= type_size(var
->type
);
1648 printf("Failed to make storage for %s\n", var
->name
);
1653 this->result
= st_src_reg(entry
->file
, entry
->index
, var
->type
);
1654 if (glsl_version
<= 120)
1655 this->result
.type
= GLSL_TYPE_FLOAT
;
1659 glsl_to_tgsi_visitor::visit(ir_dereference_array
*ir
)
1663 int element_size
= type_size(ir
->type
);
1665 index
= ir
->array_index
->constant_expression_value();
1667 ir
->array
->accept(this);
1671 src
.index
+= index
->value
.i
[0] * element_size
;
1673 st_src_reg array_base
= this->result
;
1674 /* Variable index array dereference. It eats the "vec4" of the
1675 * base of the array and an index that offsets the Mesa register
1678 ir
->array_index
->accept(this);
1680 st_src_reg index_reg
;
1682 if (element_size
== 1) {
1683 index_reg
= this->result
;
1685 index_reg
= get_temp(glsl_type::float_type
);
1687 emit(ir
, TGSI_OPCODE_MUL
, st_dst_reg(index_reg
),
1688 this->result
, st_src_reg_for_float(element_size
));
1691 src
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
1692 memcpy(src
.reladdr
, &index_reg
, sizeof(index_reg
));
1695 /* If the type is smaller than a vec4, replicate the last channel out. */
1696 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
1697 src
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1699 src
.swizzle
= SWIZZLE_NOOP
;
1705 glsl_to_tgsi_visitor::visit(ir_dereference_record
*ir
)
1708 const glsl_type
*struct_type
= ir
->record
->type
;
1711 ir
->record
->accept(this);
1713 for (i
= 0; i
< struct_type
->length
; i
++) {
1714 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
1716 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
1719 /* If the type is smaller than a vec4, replicate the last channel out. */
1720 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
1721 this->result
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1723 this->result
.swizzle
= SWIZZLE_NOOP
;
1725 this->result
.index
+= offset
;
1729 * We want to be careful in assignment setup to hit the actual storage
1730 * instead of potentially using a temporary like we might with the
1731 * ir_dereference handler.
1734 get_assignment_lhs(ir_dereference
*ir
, glsl_to_tgsi_visitor
*v
)
1736 /* The LHS must be a dereference. If the LHS is a variable indexed array
1737 * access of a vector, it must be separated into a series conditional moves
1738 * before reaching this point (see ir_vec_index_to_cond_assign).
1740 assert(ir
->as_dereference());
1741 ir_dereference_array
*deref_array
= ir
->as_dereference_array();
1743 assert(!deref_array
->array
->type
->is_vector());
1746 /* Use the rvalue deref handler for the most part. We'll ignore
1747 * swizzles in it and write swizzles using writemask, though.
1750 return st_dst_reg(v
->result
);
1754 * Process the condition of a conditional assignment
1756 * Examines the condition of a conditional assignment to generate the optimal
1757 * first operand of a \c CMP instruction. If the condition is a relational
1758 * operator with 0 (e.g., \c ir_binop_less), the value being compared will be
1759 * used as the source for the \c CMP instruction. Otherwise the comparison
1760 * is processed to a boolean result, and the boolean result is used as the
1761 * operand to the CMP instruction.
1764 glsl_to_tgsi_visitor::process_move_condition(ir_rvalue
*ir
)
1766 ir_rvalue
*src_ir
= ir
;
1768 bool switch_order
= false;
1770 ir_expression
*const expr
= ir
->as_expression();
1771 if ((expr
!= NULL
) && (expr
->get_num_operands() == 2)) {
1772 bool zero_on_left
= false;
1774 if (expr
->operands
[0]->is_zero()) {
1775 src_ir
= expr
->operands
[1];
1776 zero_on_left
= true;
1777 } else if (expr
->operands
[1]->is_zero()) {
1778 src_ir
= expr
->operands
[0];
1779 zero_on_left
= false;
1783 * (a < 0) T F F ( a < 0) T F F
1784 * (0 < a) F F T (-a < 0) F F T
1785 * (a <= 0) T T F (-a < 0) F F T (swap order of other operands)
1786 * (0 <= a) F T T ( a < 0) T F F (swap order of other operands)
1787 * (a > 0) F F T (-a < 0) F F T
1788 * (0 > a) T F F ( a < 0) T F F
1789 * (a >= 0) F T T ( a < 0) T F F (swap order of other operands)
1790 * (0 >= a) T T F (-a < 0) F F T (swap order of other operands)
1792 * Note that exchanging the order of 0 and 'a' in the comparison simply
1793 * means that the value of 'a' should be negated.
1796 switch (expr
->operation
) {
1798 switch_order
= false;
1799 negate
= zero_on_left
;
1802 case ir_binop_greater
:
1803 switch_order
= false;
1804 negate
= !zero_on_left
;
1807 case ir_binop_lequal
:
1808 switch_order
= true;
1809 negate
= !zero_on_left
;
1812 case ir_binop_gequal
:
1813 switch_order
= true;
1814 negate
= zero_on_left
;
1818 /* This isn't the right kind of comparison afterall, so make sure
1819 * the whole condition is visited.
1827 src_ir
->accept(this);
1829 /* We use the TGSI_OPCODE_CMP (a < 0 ? b : c) for conditional moves, and the
1830 * condition we produced is 0.0 or 1.0. By flipping the sign, we can
1831 * choose which value TGSI_OPCODE_CMP produces without an extra instruction
1832 * computing the condition.
1835 this->result
.negate
= ~this->result
.negate
;
1837 return switch_order
;
1841 glsl_to_tgsi_visitor::visit(ir_assignment
*ir
)
1847 ir
->rhs
->accept(this);
1850 l
= get_assignment_lhs(ir
->lhs
, this);
1852 /* FINISHME: This should really set to the correct maximal writemask for each
1853 * FINISHME: component written (in the loops below). This case can only
1854 * FINISHME: occur for matrices, arrays, and structures.
1856 if (ir
->write_mask
== 0) {
1857 assert(!ir
->lhs
->type
->is_scalar() && !ir
->lhs
->type
->is_vector());
1858 l
.writemask
= WRITEMASK_XYZW
;
1859 } else if (ir
->lhs
->type
->is_scalar() &&
1860 ir
->lhs
->variable_referenced()->mode
== ir_var_out
) {
1861 /* FINISHME: This hack makes writing to gl_FragDepth, which lives in the
1862 * FINISHME: W component of fragment shader output zero, work correctly.
1864 l
.writemask
= WRITEMASK_XYZW
;
1867 int first_enabled_chan
= 0;
1870 l
.writemask
= ir
->write_mask
;
1872 for (int i
= 0; i
< 4; i
++) {
1873 if (l
.writemask
& (1 << i
)) {
1874 first_enabled_chan
= GET_SWZ(r
.swizzle
, i
);
1879 /* Swizzle a small RHS vector into the channels being written.
1881 * glsl ir treats write_mask as dictating how many channels are
1882 * present on the RHS while Mesa IR treats write_mask as just
1883 * showing which channels of the vec4 RHS get written.
1885 for (int i
= 0; i
< 4; i
++) {
1886 if (l
.writemask
& (1 << i
))
1887 swizzles
[i
] = GET_SWZ(r
.swizzle
, rhs_chan
++);
1889 swizzles
[i
] = first_enabled_chan
;
1891 r
.swizzle
= MAKE_SWIZZLE4(swizzles
[0], swizzles
[1],
1892 swizzles
[2], swizzles
[3]);
1895 assert(l
.file
!= PROGRAM_UNDEFINED
);
1896 assert(r
.file
!= PROGRAM_UNDEFINED
);
1898 if (ir
->condition
) {
1899 const bool switch_order
= this->process_move_condition(ir
->condition
);
1900 st_src_reg condition
= this->result
;
1902 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
1903 st_src_reg l_src
= st_src_reg(l
);
1904 l_src
.swizzle
= swizzle_for_size(ir
->lhs
->type
->vector_elements
);
1907 emit(ir
, TGSI_OPCODE_CMP
, l
, condition
, l_src
, r
);
1909 emit(ir
, TGSI_OPCODE_CMP
, l
, condition
, r
, l_src
);
1915 } else if (ir
->rhs
->as_expression() &&
1916 this->instructions
.get_tail() &&
1917 ir
->rhs
== ((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->ir
&&
1918 type_size(ir
->lhs
->type
) == 1) {
1919 /* To avoid emitting an extra MOV when assigning an expression to a
1920 * variable, emit the last instruction of the expression again, but
1921 * replace the destination register with the target of the assignment.
1922 * Dead code elimination will remove the original instruction.
1924 glsl_to_tgsi_instruction
*inst
;
1925 inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
1926 emit(ir
, inst
->op
, l
, inst
->src
[0], inst
->src
[1], inst
->src
[2]);
1928 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
1929 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
1938 glsl_to_tgsi_visitor::visit(ir_constant
*ir
)
1941 GLfloat stack_vals
[4] = { 0 };
1942 gl_constant_value
*values
= (gl_constant_value
*) stack_vals
;
1943 GLenum gl_type
= GL_NONE
;
1945 gl_register_file file
;
1946 gl_program_parameter_list
*param_list
;
1947 static int in_array
= 0;
1949 file
= in_array
? PROGRAM_CONSTANT
: PROGRAM_IMMEDIATE
;
1950 param_list
= in_array
? this->prog
->Parameters
: this->immediates
;
1952 /* Unfortunately, 4 floats is all we can get into
1953 * _mesa_add_typed_unnamed_constant. So, make a temp to store an
1954 * aggregate constant and move each constant value into it. If we
1955 * get lucky, copy propagation will eliminate the extra moves.
1957 if (ir
->type
->base_type
== GLSL_TYPE_STRUCT
) {
1958 st_src_reg temp_base
= get_temp(ir
->type
);
1959 st_dst_reg temp
= st_dst_reg(temp_base
);
1961 foreach_iter(exec_list_iterator
, iter
, ir
->components
) {
1962 ir_constant
*field_value
= (ir_constant
*)iter
.get();
1963 int size
= type_size(field_value
->type
);
1967 field_value
->accept(this);
1970 for (i
= 0; i
< (unsigned int)size
; i
++) {
1971 emit(ir
, TGSI_OPCODE_MOV
, temp
, src
);
1977 this->result
= temp_base
;
1981 if (ir
->type
->is_array()) {
1982 st_src_reg temp_base
= get_temp(ir
->type
);
1983 st_dst_reg temp
= st_dst_reg(temp_base
);
1984 int size
= type_size(ir
->type
->fields
.array
);
1989 for (i
= 0; i
< ir
->type
->length
; i
++) {
1990 ir
->array_elements
[i
]->accept(this);
1992 for (int j
= 0; j
< size
; j
++) {
1993 emit(ir
, TGSI_OPCODE_MOV
, temp
, src
);
1999 this->result
= temp_base
;
2004 if (ir
->type
->is_matrix()) {
2005 st_src_reg mat
= get_temp(ir
->type
);
2006 st_dst_reg mat_column
= st_dst_reg(mat
);
2008 for (i
= 0; i
< ir
->type
->matrix_columns
; i
++) {
2009 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
2010 values
= (gl_constant_value
*) &ir
->value
.f
[i
* ir
->type
->vector_elements
];
2012 src
= st_src_reg(file
, -1, ir
->type
->base_type
);
2013 src
.index
= _mesa_add_typed_unnamed_constant(param_list
,
2015 ir
->type
->vector_elements
,
2018 emit(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
2027 switch (ir
->type
->base_type
) {
2028 case GLSL_TYPE_FLOAT
:
2030 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2031 values
[i
].f
= ir
->value
.f
[i
];
2034 case GLSL_TYPE_UINT
:
2035 gl_type
= glsl_version
>= 130 ? GL_UNSIGNED_INT
: GL_FLOAT
;
2036 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2037 if (glsl_version
>= 130)
2038 values
[i
].u
= ir
->value
.u
[i
];
2040 values
[i
].f
= ir
->value
.u
[i
];
2044 gl_type
= glsl_version
>= 130 ? GL_INT
: GL_FLOAT
;
2045 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2046 if (glsl_version
>= 130)
2047 values
[i
].i
= ir
->value
.i
[i
];
2049 values
[i
].f
= ir
->value
.i
[i
];
2052 case GLSL_TYPE_BOOL
:
2053 gl_type
= glsl_version
>= 130 ? GL_BOOL
: GL_FLOAT
;
2054 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2055 if (glsl_version
>= 130)
2056 values
[i
].b
= ir
->value
.b
[i
];
2058 values
[i
].f
= ir
->value
.b
[i
];
2062 assert(!"Non-float/uint/int/bool constant");
2065 this->result
= st_src_reg(file
, -1, ir
->type
);
2066 this->result
.index
= _mesa_add_typed_unnamed_constant(param_list
,
2067 values
, ir
->type
->vector_elements
, gl_type
,
2068 &this->result
.swizzle
);
2072 glsl_to_tgsi_visitor::get_function_signature(ir_function_signature
*sig
)
2074 function_entry
*entry
;
2076 foreach_iter(exec_list_iterator
, iter
, this->function_signatures
) {
2077 entry
= (function_entry
*)iter
.get();
2079 if (entry
->sig
== sig
)
2083 entry
= ralloc(mem_ctx
, function_entry
);
2085 entry
->sig_id
= this->next_signature_id
++;
2086 entry
->bgn_inst
= NULL
;
2088 /* Allocate storage for all the parameters. */
2089 foreach_iter(exec_list_iterator
, iter
, sig
->parameters
) {
2090 ir_variable
*param
= (ir_variable
*)iter
.get();
2091 variable_storage
*storage
;
2093 storage
= find_variable_storage(param
);
2096 storage
= new(mem_ctx
) variable_storage(param
, PROGRAM_TEMPORARY
,
2098 this->variables
.push_tail(storage
);
2100 this->next_temp
+= type_size(param
->type
);
2103 if (!sig
->return_type
->is_void()) {
2104 entry
->return_reg
= get_temp(sig
->return_type
);
2106 entry
->return_reg
= undef_src
;
2109 this->function_signatures
.push_tail(entry
);
2114 glsl_to_tgsi_visitor::visit(ir_call
*ir
)
2116 glsl_to_tgsi_instruction
*call_inst
;
2117 ir_function_signature
*sig
= ir
->get_callee();
2118 function_entry
*entry
= get_function_signature(sig
);
2121 /* Process in parameters. */
2122 exec_list_iterator sig_iter
= sig
->parameters
.iterator();
2123 foreach_iter(exec_list_iterator
, iter
, *ir
) {
2124 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
2125 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
2127 if (param
->mode
== ir_var_in
||
2128 param
->mode
== ir_var_inout
) {
2129 variable_storage
*storage
= find_variable_storage(param
);
2132 param_rval
->accept(this);
2133 st_src_reg r
= this->result
;
2136 l
.file
= storage
->file
;
2137 l
.index
= storage
->index
;
2139 l
.writemask
= WRITEMASK_XYZW
;
2140 l
.cond_mask
= COND_TR
;
2142 for (i
= 0; i
< type_size(param
->type
); i
++) {
2143 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
2151 assert(!sig_iter
.has_next());
2153 /* Emit call instruction */
2154 call_inst
= emit(ir
, TGSI_OPCODE_CAL
);
2155 call_inst
->function
= entry
;
2157 /* Process out parameters. */
2158 sig_iter
= sig
->parameters
.iterator();
2159 foreach_iter(exec_list_iterator
, iter
, *ir
) {
2160 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
2161 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
2163 if (param
->mode
== ir_var_out
||
2164 param
->mode
== ir_var_inout
) {
2165 variable_storage
*storage
= find_variable_storage(param
);
2169 r
.file
= storage
->file
;
2170 r
.index
= storage
->index
;
2172 r
.swizzle
= SWIZZLE_NOOP
;
2175 param_rval
->accept(this);
2176 st_dst_reg l
= st_dst_reg(this->result
);
2178 for (i
= 0; i
< type_size(param
->type
); i
++) {
2179 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
2187 assert(!sig_iter
.has_next());
2189 /* Process return value. */
2190 this->result
= entry
->return_reg
;
2194 glsl_to_tgsi_visitor::visit(ir_texture
*ir
)
2196 st_src_reg result_src
, coord
, lod_info
, projector
, dx
, dy
;
2197 st_dst_reg result_dst
, coord_dst
;
2198 glsl_to_tgsi_instruction
*inst
= NULL
;
2199 unsigned opcode
= TGSI_OPCODE_NOP
;
2201 ir
->coordinate
->accept(this);
2203 /* Put our coords in a temp. We'll need to modify them for shadow,
2204 * projection, or LOD, so the only case we'd use it as is is if
2205 * we're doing plain old texturing. Mesa IR optimization should
2206 * handle cleaning up our mess in that case.
2208 coord
= get_temp(glsl_type::vec4_type
);
2209 coord_dst
= st_dst_reg(coord
);
2210 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
2212 if (ir
->projector
) {
2213 ir
->projector
->accept(this);
2214 projector
= this->result
;
2217 /* Storage for our result. Ideally for an assignment we'd be using
2218 * the actual storage for the result here, instead.
2220 result_src
= get_temp(glsl_type::vec4_type
);
2221 result_dst
= st_dst_reg(result_src
);
2225 opcode
= TGSI_OPCODE_TEX
;
2228 opcode
= TGSI_OPCODE_TXB
;
2229 ir
->lod_info
.bias
->accept(this);
2230 lod_info
= this->result
;
2233 opcode
= TGSI_OPCODE_TXL
;
2234 ir
->lod_info
.lod
->accept(this);
2235 lod_info
= this->result
;
2238 opcode
= TGSI_OPCODE_TXD
;
2239 ir
->lod_info
.grad
.dPdx
->accept(this);
2241 ir
->lod_info
.grad
.dPdy
->accept(this);
2244 case ir_txf
: /* TODO: use TGSI_OPCODE_TXF here */
2245 assert(!"GLSL 1.30 features unsupported");
2249 if (ir
->projector
) {
2250 if (opcode
== TGSI_OPCODE_TEX
) {
2251 /* Slot the projector in as the last component of the coord. */
2252 coord_dst
.writemask
= WRITEMASK_W
;
2253 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, projector
);
2254 coord_dst
.writemask
= WRITEMASK_XYZW
;
2255 opcode
= TGSI_OPCODE_TXP
;
2257 st_src_reg coord_w
= coord
;
2258 coord_w
.swizzle
= SWIZZLE_WWWW
;
2260 /* For the other TEX opcodes there's no projective version
2261 * since the last slot is taken up by LOD info. Do the
2262 * projective divide now.
2264 coord_dst
.writemask
= WRITEMASK_W
;
2265 emit(ir
, TGSI_OPCODE_RCP
, coord_dst
, projector
);
2267 /* In the case where we have to project the coordinates "by hand,"
2268 * the shadow comparator value must also be projected.
2270 st_src_reg tmp_src
= coord
;
2271 if (ir
->shadow_comparitor
) {
2272 /* Slot the shadow value in as the second to last component of the
2275 ir
->shadow_comparitor
->accept(this);
2277 tmp_src
= get_temp(glsl_type::vec4_type
);
2278 st_dst_reg tmp_dst
= st_dst_reg(tmp_src
);
2280 tmp_dst
.writemask
= WRITEMASK_Z
;
2281 emit(ir
, TGSI_OPCODE_MOV
, tmp_dst
, this->result
);
2283 tmp_dst
.writemask
= WRITEMASK_XY
;
2284 emit(ir
, TGSI_OPCODE_MOV
, tmp_dst
, coord
);
2287 coord_dst
.writemask
= WRITEMASK_XYZ
;
2288 emit(ir
, TGSI_OPCODE_MUL
, coord_dst
, tmp_src
, coord_w
);
2290 coord_dst
.writemask
= WRITEMASK_XYZW
;
2291 coord
.swizzle
= SWIZZLE_XYZW
;
2295 /* If projection is done and the opcode is not TGSI_OPCODE_TXP, then the shadow
2296 * comparator was put in the correct place (and projected) by the code,
2297 * above, that handles by-hand projection.
2299 if (ir
->shadow_comparitor
&& (!ir
->projector
|| opcode
== TGSI_OPCODE_TXP
)) {
2300 /* Slot the shadow value in as the second to last component of the
2303 ir
->shadow_comparitor
->accept(this);
2304 coord_dst
.writemask
= WRITEMASK_Z
;
2305 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
2306 coord_dst
.writemask
= WRITEMASK_XYZW
;
2309 if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXB
) {
2310 /* TGSI stores LOD or LOD bias in the last channel of the coords. */
2311 coord_dst
.writemask
= WRITEMASK_W
;
2312 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, lod_info
);
2313 coord_dst
.writemask
= WRITEMASK_XYZW
;
2316 if (opcode
== TGSI_OPCODE_TXD
)
2317 inst
= emit(ir
, opcode
, result_dst
, coord
, dx
, dy
);
2319 inst
= emit(ir
, opcode
, result_dst
, coord
);
2321 if (ir
->shadow_comparitor
)
2322 inst
->tex_shadow
= GL_TRUE
;
2324 inst
->sampler
= _mesa_get_sampler_uniform_value(ir
->sampler
,
2325 this->shader_program
,
2328 const glsl_type
*sampler_type
= ir
->sampler
->type
;
2330 switch (sampler_type
->sampler_dimensionality
) {
2331 case GLSL_SAMPLER_DIM_1D
:
2332 inst
->tex_target
= (sampler_type
->sampler_array
)
2333 ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
2335 case GLSL_SAMPLER_DIM_2D
:
2336 inst
->tex_target
= (sampler_type
->sampler_array
)
2337 ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
2339 case GLSL_SAMPLER_DIM_3D
:
2340 inst
->tex_target
= TEXTURE_3D_INDEX
;
2342 case GLSL_SAMPLER_DIM_CUBE
:
2343 inst
->tex_target
= TEXTURE_CUBE_INDEX
;
2345 case GLSL_SAMPLER_DIM_RECT
:
2346 inst
->tex_target
= TEXTURE_RECT_INDEX
;
2348 case GLSL_SAMPLER_DIM_BUF
:
2349 assert(!"FINISHME: Implement ARB_texture_buffer_object");
2352 assert(!"Should not get here.");
2355 this->result
= result_src
;
2359 glsl_to_tgsi_visitor::visit(ir_return
*ir
)
2361 if (ir
->get_value()) {
2365 assert(current_function
);
2367 ir
->get_value()->accept(this);
2368 st_src_reg r
= this->result
;
2370 l
= st_dst_reg(current_function
->return_reg
);
2372 for (i
= 0; i
< type_size(current_function
->sig
->return_type
); i
++) {
2373 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
2379 emit(ir
, TGSI_OPCODE_RET
);
2383 glsl_to_tgsi_visitor::visit(ir_discard
*ir
)
2385 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
2387 if (ir
->condition
) {
2388 ir
->condition
->accept(this);
2389 this->result
.negate
= ~this->result
.negate
;
2390 emit(ir
, TGSI_OPCODE_KIL
, undef_dst
, this->result
);
2392 emit(ir
, TGSI_OPCODE_KILP
);
2395 fp
->UsesKill
= GL_TRUE
;
2399 glsl_to_tgsi_visitor::visit(ir_if
*ir
)
2401 glsl_to_tgsi_instruction
*cond_inst
, *if_inst
, *else_inst
= NULL
;
2402 glsl_to_tgsi_instruction
*prev_inst
;
2404 prev_inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
2406 ir
->condition
->accept(this);
2407 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
2409 if (this->options
->EmitCondCodes
) {
2410 cond_inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
2412 /* See if we actually generated any instruction for generating
2413 * the condition. If not, then cook up a move to a temp so we
2414 * have something to set cond_update on.
2416 if (cond_inst
== prev_inst
) {
2417 st_src_reg temp
= get_temp(glsl_type::bool_type
);
2418 cond_inst
= emit(ir
->condition
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), result
);
2420 cond_inst
->cond_update
= GL_TRUE
;
2422 if_inst
= emit(ir
->condition
, TGSI_OPCODE_IF
);
2423 if_inst
->dst
.cond_mask
= COND_NE
;
2425 if_inst
= emit(ir
->condition
, TGSI_OPCODE_IF
, undef_dst
, this->result
);
2428 this->instructions
.push_tail(if_inst
);
2430 visit_exec_list(&ir
->then_instructions
, this);
2432 if (!ir
->else_instructions
.is_empty()) {
2433 else_inst
= emit(ir
->condition
, TGSI_OPCODE_ELSE
);
2434 visit_exec_list(&ir
->else_instructions
, this);
2437 if_inst
= emit(ir
->condition
, TGSI_OPCODE_ENDIF
);
2440 glsl_to_tgsi_visitor::glsl_to_tgsi_visitor()
2442 result
.file
= PROGRAM_UNDEFINED
;
2444 next_signature_id
= 1;
2445 current_function
= NULL
;
2446 num_address_regs
= 0;
2447 indirect_addr_temps
= false;
2448 indirect_addr_consts
= false;
2449 immediates
= _mesa_new_parameter_list();
2450 mem_ctx
= ralloc_context(NULL
);
2453 glsl_to_tgsi_visitor::~glsl_to_tgsi_visitor()
2455 _mesa_free_parameter_list(immediates
);
2456 ralloc_free(mem_ctx
);
2459 extern "C" void free_glsl_to_tgsi_visitor(glsl_to_tgsi_visitor
*v
)
2466 * Count resources used by the given gpu program (number of texture
2470 count_resources(glsl_to_tgsi_visitor
*v
, gl_program
*prog
)
2472 v
->samplers_used
= 0;
2474 foreach_iter(exec_list_iterator
, iter
, v
->instructions
) {
2475 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
2477 if (is_tex_instruction(inst
->op
)) {
2478 v
->samplers_used
|= 1 << inst
->sampler
;
2480 prog
->SamplerTargets
[inst
->sampler
] =
2481 (gl_texture_index
)inst
->tex_target
;
2482 if (inst
->tex_shadow
) {
2483 prog
->ShadowSamplers
|= 1 << inst
->sampler
;
2488 prog
->SamplersUsed
= v
->samplers_used
;
2489 _mesa_update_shader_textures_used(prog
);
2494 * Check if the given vertex/fragment/shader program is within the
2495 * resource limits of the context (number of texture units, etc).
2496 * If any of those checks fail, record a linker error.
2498 * XXX more checks are needed...
2501 check_resources(const struct gl_context
*ctx
,
2502 struct gl_shader_program
*shader_program
,
2503 glsl_to_tgsi_visitor
*prog
,
2504 struct gl_program
*proginfo
)
2506 switch (proginfo
->Target
) {
2507 case GL_VERTEX_PROGRAM_ARB
:
2508 if (_mesa_bitcount(prog
->samplers_used
) >
2509 ctx
->Const
.MaxVertexTextureImageUnits
) {
2510 fail_link(shader_program
, "Too many vertex shader texture samplers");
2512 if (proginfo
->Parameters
->NumParameters
> MAX_UNIFORMS
) {
2513 fail_link(shader_program
, "Too many vertex shader constants");
2516 case MESA_GEOMETRY_PROGRAM
:
2517 if (_mesa_bitcount(prog
->samplers_used
) >
2518 ctx
->Const
.MaxGeometryTextureImageUnits
) {
2519 fail_link(shader_program
, "Too many geometry shader texture samplers");
2521 if (proginfo
->Parameters
->NumParameters
>
2522 MAX_GEOMETRY_UNIFORM_COMPONENTS
/ 4) {
2523 fail_link(shader_program
, "Too many geometry shader constants");
2526 case GL_FRAGMENT_PROGRAM_ARB
:
2527 if (_mesa_bitcount(prog
->samplers_used
) >
2528 ctx
->Const
.MaxTextureImageUnits
) {
2529 fail_link(shader_program
, "Too many fragment shader texture samplers");
2531 if (proginfo
->Parameters
->NumParameters
> MAX_UNIFORMS
) {
2532 fail_link(shader_program
, "Too many fragment shader constants");
2536 _mesa_problem(ctx
, "unexpected program type in check_resources()");
2542 struct uniform_sort
{
2543 struct gl_uniform
*u
;
2547 /* The shader_program->Uniforms list is almost sorted in increasing
2548 * uniform->{Frag,Vert}Pos locations, but not quite when there are
2549 * uniforms shared between targets. We need to add parameters in
2550 * increasing order for the targets.
2553 sort_uniforms(const void *a
, const void *b
)
2555 struct uniform_sort
*u1
= (struct uniform_sort
*)a
;
2556 struct uniform_sort
*u2
= (struct uniform_sort
*)b
;
2558 return u1
->pos
- u2
->pos
;
2561 /* Add the uniforms to the parameters. The linker chose locations
2562 * in our parameters lists (which weren't created yet), which the
2563 * uniforms code will use to poke values into our parameters list
2564 * when uniforms are updated.
2567 add_uniforms_to_parameters_list(struct gl_shader_program
*shader_program
,
2568 struct gl_shader
*shader
,
2569 struct gl_program
*prog
)
2572 unsigned int next_sampler
= 0, num_uniforms
= 0;
2573 struct uniform_sort
*sorted_uniforms
;
2575 sorted_uniforms
= ralloc_array(NULL
, struct uniform_sort
,
2576 shader_program
->Uniforms
->NumUniforms
);
2578 for (i
= 0; i
< shader_program
->Uniforms
->NumUniforms
; i
++) {
2579 struct gl_uniform
*uniform
= shader_program
->Uniforms
->Uniforms
+ i
;
2580 int parameter_index
= -1;
2582 switch (shader
->Type
) {
2583 case GL_VERTEX_SHADER
:
2584 parameter_index
= uniform
->VertPos
;
2586 case GL_FRAGMENT_SHADER
:
2587 parameter_index
= uniform
->FragPos
;
2589 case GL_GEOMETRY_SHADER
:
2590 parameter_index
= uniform
->GeomPos
;
2594 /* Only add uniforms used in our target. */
2595 if (parameter_index
!= -1) {
2596 sorted_uniforms
[num_uniforms
].pos
= parameter_index
;
2597 sorted_uniforms
[num_uniforms
].u
= uniform
;
2602 qsort(sorted_uniforms
, num_uniforms
, sizeof(struct uniform_sort
),
2605 for (i
= 0; i
< num_uniforms
; i
++) {
2606 struct gl_uniform
*uniform
= sorted_uniforms
[i
].u
;
2607 int parameter_index
= sorted_uniforms
[i
].pos
;
2608 const glsl_type
*type
= uniform
->Type
;
2611 if (type
->is_vector() ||
2612 type
->is_scalar()) {
2613 size
= type
->vector_elements
;
2615 size
= type_size(type
) * 4;
2618 gl_register_file file
;
2619 if (type
->is_sampler() ||
2620 (type
->is_array() && type
->fields
.array
->is_sampler())) {
2621 file
= PROGRAM_SAMPLER
;
2623 file
= PROGRAM_UNIFORM
;
2626 GLint index
= _mesa_lookup_parameter_index(prog
->Parameters
, -1,
2630 index
= _mesa_add_parameter(prog
->Parameters
, file
,
2631 uniform
->Name
, size
, type
->gl_type
,
2634 /* Sampler uniform values are stored in prog->SamplerUnits,
2635 * and the entry in that array is selected by this index we
2636 * store in ParameterValues[].
2638 if (file
== PROGRAM_SAMPLER
) {
2639 for (unsigned int j
= 0; j
< size
/ 4; j
++)
2640 prog
->Parameters
->ParameterValues
[index
+ j
][0].f
= next_sampler
++;
2643 /* The location chosen in the Parameters list here (returned
2644 * from _mesa_add_uniform) has to match what the linker chose.
2646 if (index
!= parameter_index
) {
2647 fail_link(shader_program
, "Allocation of uniform `%s' to target "
2648 "failed (%d vs %d)\n",
2649 uniform
->Name
, index
, parameter_index
);
2654 ralloc_free(sorted_uniforms
);
2658 set_uniform_initializer(struct gl_context
*ctx
, void *mem_ctx
,
2659 struct gl_shader_program
*shader_program
,
2660 const char *name
, const glsl_type
*type
,
2663 if (type
->is_record()) {
2664 ir_constant
*field_constant
;
2666 field_constant
= (ir_constant
*)val
->components
.get_head();
2668 for (unsigned int i
= 0; i
< type
->length
; i
++) {
2669 const glsl_type
*field_type
= type
->fields
.structure
[i
].type
;
2670 const char *field_name
= ralloc_asprintf(mem_ctx
, "%s.%s", name
,
2671 type
->fields
.structure
[i
].name
);
2672 set_uniform_initializer(ctx
, mem_ctx
, shader_program
, field_name
,
2673 field_type
, field_constant
);
2674 field_constant
= (ir_constant
*)field_constant
->next
;
2679 int loc
= _mesa_get_uniform_location(ctx
, shader_program
, name
);
2682 fail_link(shader_program
,
2683 "Couldn't find uniform for initializer %s\n", name
);
2687 for (unsigned int i
= 0; i
< (type
->is_array() ? type
->length
: 1); i
++) {
2688 ir_constant
*element
;
2689 const glsl_type
*element_type
;
2690 if (type
->is_array()) {
2691 element
= val
->array_elements
[i
];
2692 element_type
= type
->fields
.array
;
2695 element_type
= type
;
2700 if (element_type
->base_type
== GLSL_TYPE_BOOL
) {
2701 int *conv
= ralloc_array(mem_ctx
, int, element_type
->components());
2702 for (unsigned int j
= 0; j
< element_type
->components(); j
++) {
2703 conv
[j
] = element
->value
.b
[j
];
2705 values
= (void *)conv
;
2706 element_type
= glsl_type::get_instance(GLSL_TYPE_INT
,
2707 element_type
->vector_elements
,
2710 values
= &element
->value
;
2713 if (element_type
->is_matrix()) {
2714 _mesa_uniform_matrix(ctx
, shader_program
,
2715 element_type
->matrix_columns
,
2716 element_type
->vector_elements
,
2717 loc
, 1, GL_FALSE
, (GLfloat
*)values
);
2718 loc
+= element_type
->matrix_columns
;
2720 _mesa_uniform(ctx
, shader_program
, loc
, element_type
->matrix_columns
,
2721 values
, element_type
->gl_type
);
2722 loc
+= type_size(element_type
);
2728 set_uniform_initializers(struct gl_context
*ctx
,
2729 struct gl_shader_program
*shader_program
)
2731 void *mem_ctx
= NULL
;
2733 for (unsigned int i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
2734 struct gl_shader
*shader
= shader_program
->_LinkedShaders
[i
];
2739 foreach_iter(exec_list_iterator
, iter
, *shader
->ir
) {
2740 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
2741 ir_variable
*var
= ir
->as_variable();
2743 if (!var
|| var
->mode
!= ir_var_uniform
|| !var
->constant_value
)
2747 mem_ctx
= ralloc_context(NULL
);
2749 set_uniform_initializer(ctx
, mem_ctx
, shader_program
, var
->name
,
2750 var
->type
, var
->constant_value
);
2754 ralloc_free(mem_ctx
);
2758 * Scan/rewrite program to remove reads of custom (output) registers.
2759 * The passed type has to be either PROGRAM_OUTPUT or PROGRAM_VARYING
2760 * (for vertex shaders).
2761 * In GLSL shaders, varying vars can be read and written.
2762 * On some hardware, trying to read an output register causes trouble.
2763 * So, rewrite the program to use a temporary register in this case.
2765 * Based on _mesa_remove_output_reads from programopt.c.
2768 glsl_to_tgsi_visitor::remove_output_reads(gl_register_file type
)
2771 GLint outputMap
[VERT_RESULT_MAX
];
2772 GLint outputTypes
[VERT_RESULT_MAX
];
2773 GLuint numVaryingReads
= 0;
2774 GLboolean usedTemps
[MAX_TEMPS
];
2775 GLuint firstTemp
= 0;
2777 _mesa_find_used_registers(prog
, PROGRAM_TEMPORARY
,
2778 usedTemps
, MAX_TEMPS
);
2780 assert(type
== PROGRAM_VARYING
|| type
== PROGRAM_OUTPUT
);
2781 assert(prog
->Target
== GL_VERTEX_PROGRAM_ARB
|| type
!= PROGRAM_VARYING
);
2783 for (i
= 0; i
< VERT_RESULT_MAX
; i
++)
2786 /* look for instructions which read from varying vars */
2787 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2788 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
2789 const GLuint numSrc
= num_inst_src_regs(inst
->op
);
2791 for (j
= 0; j
< numSrc
; j
++) {
2792 if (inst
->src
[j
].file
== type
) {
2793 /* replace the read with a temp reg */
2794 const GLuint var
= inst
->src
[j
].index
;
2795 if (outputMap
[var
] == -1) {
2797 outputMap
[var
] = _mesa_find_free_register(usedTemps
,
2800 outputTypes
[var
] = inst
->src
[j
].type
;
2801 firstTemp
= outputMap
[var
] + 1;
2803 inst
->src
[j
].file
= PROGRAM_TEMPORARY
;
2804 inst
->src
[j
].index
= outputMap
[var
];
2809 if (numVaryingReads
== 0)
2810 return; /* nothing to be done */
2812 /* look for instructions which write to the varying vars identified above */
2813 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2814 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
2815 if (inst
->dst
.file
== type
&& outputMap
[inst
->dst
.index
] >= 0) {
2816 /* change inst to write to the temp reg, instead of the varying */
2817 inst
->dst
.file
= PROGRAM_TEMPORARY
;
2818 inst
->dst
.index
= outputMap
[inst
->dst
.index
];
2822 /* insert new MOV instructions at the end */
2823 for (i
= 0; i
< VERT_RESULT_MAX
; i
++) {
2824 if (outputMap
[i
] >= 0) {
2825 /* MOV VAR[i], TEMP[tmp]; */
2826 st_src_reg src
= st_src_reg(PROGRAM_TEMPORARY
, outputMap
[i
], outputTypes
[i
]);
2827 st_dst_reg dst
= st_dst_reg(type
, WRITEMASK_XYZW
, outputTypes
[i
]);
2829 this->emit(NULL
, TGSI_OPCODE_MOV
, dst
, src
);
2835 * Returns the mask of channels (bitmask of WRITEMASK_X,Y,Z,W) which
2836 * are read from the given src in this instruction
2839 get_src_arg_mask(st_dst_reg dst
, st_src_reg src
)
2841 int read_mask
= 0, comp
;
2843 /* Now, given the src swizzle and the written channels, find which
2844 * components are actually read
2846 for (comp
= 0; comp
< 4; ++comp
) {
2847 const unsigned coord
= GET_SWZ(src
.swizzle
, comp
);
2849 if (dst
.writemask
& (1 << comp
) && coord
<= SWIZZLE_W
)
2850 read_mask
|= 1 << coord
;
2857 * This pass replaces CMP T0, T1 T2 T0 with MOV T0, T2 when the CMP
2858 * instruction is the first instruction to write to register T0. There are
2859 * several lowering passes done in GLSL IR (e.g. branches and
2860 * relative addressing) that create a large number of conditional assignments
2861 * that ir_to_mesa converts to CMP instructions like the one mentioned above.
2863 * Here is why this conversion is safe:
2864 * CMP T0, T1 T2 T0 can be expanded to:
2870 * If (T1 < 0.0) evaluates to true then our replacement MOV T0, T2 is the same
2871 * as the original program. If (T1 < 0.0) evaluates to false, executing
2872 * MOV T0, T0 will store a garbage value in T0 since T0 is uninitialized.
2873 * Therefore, it doesn't matter that we are replacing MOV T0, T0 with MOV T0, T2
2874 * because any instruction that was going to read from T0 after this was going
2875 * to read a garbage value anyway.
2878 glsl_to_tgsi_visitor::simplify_cmp(void)
2880 unsigned tempWrites
[MAX_TEMPS
];
2881 unsigned outputWrites
[MAX_PROGRAM_OUTPUTS
];
2883 memset(tempWrites
, 0, sizeof(tempWrites
));
2884 memset(outputWrites
, 0, sizeof(outputWrites
));
2886 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2887 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
2888 unsigned prevWriteMask
= 0;
2890 /* Give up if we encounter relative addressing or flow control. */
2891 if (inst
->dst
.reladdr
||
2892 tgsi_get_opcode_info(inst
->op
)->is_branch
||
2893 inst
->op
== TGSI_OPCODE_BGNSUB
||
2894 inst
->op
== TGSI_OPCODE_CONT
||
2895 inst
->op
== TGSI_OPCODE_END
||
2896 inst
->op
== TGSI_OPCODE_ENDSUB
||
2897 inst
->op
== TGSI_OPCODE_RET
) {
2901 if (inst
->dst
.file
== PROGRAM_OUTPUT
) {
2902 assert(inst
->dst
.index
< MAX_PROGRAM_OUTPUTS
);
2903 prevWriteMask
= outputWrites
[inst
->dst
.index
];
2904 outputWrites
[inst
->dst
.index
] |= inst
->dst
.writemask
;
2905 } else if (inst
->dst
.file
== PROGRAM_TEMPORARY
) {
2906 assert(inst
->dst
.index
< MAX_TEMPS
);
2907 prevWriteMask
= tempWrites
[inst
->dst
.index
];
2908 tempWrites
[inst
->dst
.index
] |= inst
->dst
.writemask
;
2911 /* For a CMP to be considered a conditional write, the destination
2912 * register and source register two must be the same. */
2913 if (inst
->op
== TGSI_OPCODE_CMP
2914 && !(inst
->dst
.writemask
& prevWriteMask
)
2915 && inst
->src
[2].file
== inst
->dst
.file
2916 && inst
->src
[2].index
== inst
->dst
.index
2917 && inst
->dst
.writemask
== get_src_arg_mask(inst
->dst
, inst
->src
[2])) {
2919 inst
->op
= TGSI_OPCODE_MOV
;
2920 inst
->src
[0] = inst
->src
[1];
2925 /* Replaces all references to a temporary register index with another index. */
2927 glsl_to_tgsi_visitor::rename_temp_register(int index
, int new_index
)
2929 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2930 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
2933 for (j
=0; j
< num_inst_src_regs(inst
->op
); j
++) {
2934 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
&&
2935 inst
->src
[j
].index
== index
) {
2936 inst
->src
[j
].index
= new_index
;
2940 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== index
) {
2941 inst
->dst
.index
= new_index
;
2947 glsl_to_tgsi_visitor::get_first_temp_read(int index
)
2949 int depth
= 0; /* loop depth */
2950 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
2953 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2954 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
2956 for (j
=0; j
< num_inst_src_regs(inst
->op
); j
++) {
2957 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
&&
2958 inst
->src
[j
].index
== index
) {
2959 return (depth
== 0) ? i
: loop_start
;
2963 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
2966 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
2979 glsl_to_tgsi_visitor::get_first_temp_write(int index
)
2981 int depth
= 0; /* loop depth */
2982 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
2985 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2986 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
2988 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== index
) {
2989 return (depth
== 0) ? i
: loop_start
;
2992 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
2995 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
3008 glsl_to_tgsi_visitor::get_last_temp_read(int index
)
3010 int depth
= 0; /* loop depth */
3011 int last
= -1; /* index of last instruction that reads the temporary */
3014 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3015 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3017 for (j
=0; j
< num_inst_src_regs(inst
->op
); j
++) {
3018 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
&&
3019 inst
->src
[j
].index
== index
) {
3020 last
= (depth
== 0) ? i
: -2;
3024 if (inst
->op
== TGSI_OPCODE_BGNLOOP
)
3026 else if (inst
->op
== TGSI_OPCODE_ENDLOOP
)
3027 if (--depth
== 0 && last
== -2)
3039 glsl_to_tgsi_visitor::get_last_temp_write(int index
)
3041 int depth
= 0; /* loop depth */
3042 int last
= -1; /* index of last instruction that writes to the temporary */
3045 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3046 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3048 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== index
)
3049 last
= (depth
== 0) ? i
: -2;
3051 if (inst
->op
== TGSI_OPCODE_BGNLOOP
)
3053 else if (inst
->op
== TGSI_OPCODE_ENDLOOP
)
3054 if (--depth
== 0 && last
== -2)
3066 * On a basic block basis, tracks available PROGRAM_TEMPORARY register
3067 * channels for copy propagation and updates following instructions to
3068 * use the original versions.
3070 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
3071 * will occur. As an example, a TXP production before this pass:
3073 * 0: MOV TEMP[1], INPUT[4].xyyy;
3074 * 1: MOV TEMP[1].w, INPUT[4].wwww;
3075 * 2: TXP TEMP[2], TEMP[1], texture[0], 2D;
3079 * 0: MOV TEMP[1], INPUT[4].xyyy;
3080 * 1: MOV TEMP[1].w, INPUT[4].wwww;
3081 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
3083 * which allows for dead code elimination on TEMP[1]'s writes.
3086 glsl_to_tgsi_visitor::copy_propagate(void)
3088 glsl_to_tgsi_instruction
**acp
= rzalloc_array(mem_ctx
,
3089 glsl_to_tgsi_instruction
*,
3090 this->next_temp
* 4);
3091 int *acp_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
3094 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3095 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3097 assert(inst
->dst
.file
!= PROGRAM_TEMPORARY
3098 || inst
->dst
.index
< this->next_temp
);
3100 /* First, do any copy propagation possible into the src regs. */
3101 for (int r
= 0; r
< 3; r
++) {
3102 glsl_to_tgsi_instruction
*first
= NULL
;
3104 int acp_base
= inst
->src
[r
].index
* 4;
3106 if (inst
->src
[r
].file
!= PROGRAM_TEMPORARY
||
3107 inst
->src
[r
].reladdr
)
3110 /* See if we can find entries in the ACP consisting of MOVs
3111 * from the same src register for all the swizzled channels
3112 * of this src register reference.
3114 for (int i
= 0; i
< 4; i
++) {
3115 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
3116 glsl_to_tgsi_instruction
*copy_chan
= acp
[acp_base
+ src_chan
];
3123 assert(acp_level
[acp_base
+ src_chan
] <= level
);
3128 if (first
->src
[0].file
!= copy_chan
->src
[0].file
||
3129 first
->src
[0].index
!= copy_chan
->src
[0].index
) {
3137 /* We've now validated that we can copy-propagate to
3138 * replace this src register reference. Do it.
3140 inst
->src
[r
].file
= first
->src
[0].file
;
3141 inst
->src
[r
].index
= first
->src
[0].index
;
3144 for (int i
= 0; i
< 4; i
++) {
3145 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
3146 glsl_to_tgsi_instruction
*copy_inst
= acp
[acp_base
+ src_chan
];
3147 swizzle
|= (GET_SWZ(copy_inst
->src
[0].swizzle
, src_chan
) <<
3150 inst
->src
[r
].swizzle
= swizzle
;
3155 case TGSI_OPCODE_BGNLOOP
:
3156 case TGSI_OPCODE_ENDLOOP
:
3157 /* End of a basic block, clear the ACP entirely. */
3158 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
3161 case TGSI_OPCODE_IF
:
3165 case TGSI_OPCODE_ENDIF
:
3166 case TGSI_OPCODE_ELSE
:
3167 /* Clear all channels written inside the block from the ACP, but
3168 * leaving those that were not touched.
3170 for (int r
= 0; r
< this->next_temp
; r
++) {
3171 for (int c
= 0; c
< 4; c
++) {
3172 if (!acp
[4 * r
+ c
])
3175 if (acp_level
[4 * r
+ c
] >= level
)
3176 acp
[4 * r
+ c
] = NULL
;
3179 if (inst
->op
== TGSI_OPCODE_ENDIF
)
3184 /* Continuing the block, clear any written channels from
3187 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.reladdr
) {
3188 /* Any temporary might be written, so no copy propagation
3189 * across this instruction.
3191 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
3192 } else if (inst
->dst
.file
== PROGRAM_OUTPUT
&&
3193 inst
->dst
.reladdr
) {
3194 /* Any output might be written, so no copy propagation
3195 * from outputs across this instruction.
3197 for (int r
= 0; r
< this->next_temp
; r
++) {
3198 for (int c
= 0; c
< 4; c
++) {
3199 if (!acp
[4 * r
+ c
])
3202 if (acp
[4 * r
+ c
]->src
[0].file
== PROGRAM_OUTPUT
)
3203 acp
[4 * r
+ c
] = NULL
;
3206 } else if (inst
->dst
.file
== PROGRAM_TEMPORARY
||
3207 inst
->dst
.file
== PROGRAM_OUTPUT
) {
3208 /* Clear where it's used as dst. */
3209 if (inst
->dst
.file
== PROGRAM_TEMPORARY
) {
3210 for (int c
= 0; c
< 4; c
++) {
3211 if (inst
->dst
.writemask
& (1 << c
)) {
3212 acp
[4 * inst
->dst
.index
+ c
] = NULL
;
3217 /* Clear where it's used as src. */
3218 for (int r
= 0; r
< this->next_temp
; r
++) {
3219 for (int c
= 0; c
< 4; c
++) {
3220 if (!acp
[4 * r
+ c
])
3223 int src_chan
= GET_SWZ(acp
[4 * r
+ c
]->src
[0].swizzle
, c
);
3225 if (acp
[4 * r
+ c
]->src
[0].file
== inst
->dst
.file
&&
3226 acp
[4 * r
+ c
]->src
[0].index
== inst
->dst
.index
&&
3227 inst
->dst
.writemask
& (1 << src_chan
))
3229 acp
[4 * r
+ c
] = NULL
;
3237 /* If this is a copy, add it to the ACP. */
3238 if (inst
->op
== TGSI_OPCODE_MOV
&&
3239 inst
->dst
.file
== PROGRAM_TEMPORARY
&&
3240 !inst
->dst
.reladdr
&&
3242 !inst
->src
[0].reladdr
&&
3243 !inst
->src
[0].negate
) {
3244 for (int i
= 0; i
< 4; i
++) {
3245 if (inst
->dst
.writemask
& (1 << i
)) {
3246 acp
[4 * inst
->dst
.index
+ i
] = inst
;
3247 acp_level
[4 * inst
->dst
.index
+ i
] = level
;
3253 ralloc_free(acp_level
);
3258 * Tracks available PROGRAM_TEMPORARY registers for dead code elimination.
3260 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
3261 * will occur. As an example, a TXP production after copy propagation but
3264 * 0: MOV TEMP[1], INPUT[4].xyyy;
3265 * 1: MOV TEMP[1].w, INPUT[4].wwww;
3266 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
3268 * and after this pass:
3270 * 0: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
3272 * FIXME: assumes that all functions are inlined (no support for BGNSUB/ENDSUB)
3273 * FIXME: doesn't eliminate all dead code inside of loops; it steps around them
3276 glsl_to_tgsi_visitor::eliminate_dead_code(void)
3280 for (i
=0; i
< this->next_temp
; i
++) {
3281 int last_read
= get_last_temp_read(i
);
3284 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3285 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3287 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== i
&&
3300 * On a basic block basis, tracks available PROGRAM_TEMPORARY registers for dead
3301 * code elimination. This is less primitive than eliminate_dead_code(), as it
3302 * is per-channel and can detect consecutive writes without a read between them
3303 * as dead code. However, there is some dead code that can be eliminated by
3304 * eliminate_dead_code() but not this function - for example, this function
3305 * cannot eliminate an instruction writing to a register that is never read and
3306 * is the only instruction writing to that register.
3308 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
3312 glsl_to_tgsi_visitor::eliminate_dead_code_advanced(void)
3314 glsl_to_tgsi_instruction
**writes
= rzalloc_array(mem_ctx
,
3315 glsl_to_tgsi_instruction
*,
3316 this->next_temp
* 4);
3317 int *write_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
3321 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3322 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3324 assert(inst
->dst
.file
!= PROGRAM_TEMPORARY
3325 || inst
->dst
.index
< this->next_temp
);
3328 case TGSI_OPCODE_BGNLOOP
:
3329 case TGSI_OPCODE_ENDLOOP
:
3330 /* End of a basic block, clear the write array entirely.
3331 * FIXME: This keeps us from killing dead code when the writes are
3332 * on either side of a loop, even when the register isn't touched
3335 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
3338 case TGSI_OPCODE_ENDIF
:
3342 case TGSI_OPCODE_ELSE
:
3343 /* Clear all channels written inside the preceding if block from the
3344 * write array, but leave those that were not touched.
3346 * FIXME: This destroys opportunities to remove dead code inside of
3347 * IF blocks that are followed by an ELSE block.
3349 for (int r
= 0; r
< this->next_temp
; r
++) {
3350 for (int c
= 0; c
< 4; c
++) {
3351 if (!writes
[4 * r
+ c
])
3354 if (write_level
[4 * r
+ c
] >= level
)
3355 writes
[4 * r
+ c
] = NULL
;
3360 case TGSI_OPCODE_IF
:
3362 /* fallthrough to default case to mark the condition as read */
3365 /* Continuing the block, clear any channels from the write array that
3366 * are read by this instruction.
3368 for (int i
= 0; i
< 4; i
++) {
3369 if (inst
->src
[i
].file
== PROGRAM_TEMPORARY
&& inst
->src
[i
].reladdr
){
3370 /* Any temporary might be read, so no dead code elimination
3371 * across this instruction.
3373 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
3374 } else if (inst
->src
[i
].file
== PROGRAM_TEMPORARY
) {
3375 /* Clear where it's used as src. */
3376 int src_chans
= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 0);
3377 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 1);
3378 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 2);
3379 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 3);
3381 for (int c
= 0; c
< 4; c
++) {
3382 if (src_chans
& (1 << c
)) {
3383 writes
[4 * inst
->src
[i
].index
+ c
] = NULL
;
3391 /* If this instruction writes to a temporary, add it to the write array.
3392 * If there is already an instruction in the write array for one or more
3393 * of the channels, flag that channel write as dead.
3395 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&&
3396 !inst
->dst
.reladdr
&&
3398 for (int c
= 0; c
< 4; c
++) {
3399 if (inst
->dst
.writemask
& (1 << c
)) {
3400 if (writes
[4 * inst
->dst
.index
+ c
]) {
3401 if (write_level
[4 * inst
->dst
.index
+ c
] < level
)
3404 writes
[4 * inst
->dst
.index
+ c
]->dead_mask
|= (1 << c
);
3406 writes
[4 * inst
->dst
.index
+ c
] = inst
;
3407 write_level
[4 * inst
->dst
.index
+ c
] = level
;
3413 /* Anything still in the write array at this point is dead code. */
3414 for (int r
= 0; r
< this->next_temp
; r
++) {
3415 for (int c
= 0; c
< 4; c
++) {
3416 glsl_to_tgsi_instruction
*inst
= writes
[4 * r
+ c
];
3418 inst
->dead_mask
|= (1 << c
);
3422 /* Now actually remove the instructions that are completely dead and update
3423 * the writemask of other instructions with dead channels.
3425 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3426 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3428 if (!inst
->dead_mask
|| !inst
->dst
.writemask
)
3430 else if (inst
->dead_mask
== inst
->dst
.writemask
) {
3435 inst
->dst
.writemask
&= ~(inst
->dead_mask
);
3438 ralloc_free(write_level
);
3439 ralloc_free(writes
);
3444 /* Merges temporary registers together where possible to reduce the number of
3445 * registers needed to run a program.
3447 * Produces optimal code only after copy propagation and dead code elimination
3450 glsl_to_tgsi_visitor::merge_registers(void)
3452 int *last_reads
= rzalloc_array(mem_ctx
, int, this->next_temp
);
3453 int *first_writes
= rzalloc_array(mem_ctx
, int, this->next_temp
);
3456 /* Read the indices of the last read and first write to each temp register
3457 * into an array so that we don't have to traverse the instruction list as
3459 for (i
=0; i
< this->next_temp
; i
++) {
3460 last_reads
[i
] = get_last_temp_read(i
);
3461 first_writes
[i
] = get_first_temp_write(i
);
3464 /* Start looking for registers with non-overlapping usages that can be
3465 * merged together. */
3466 for (i
=0; i
< this->next_temp
; i
++) {
3467 /* Don't touch unused registers. */
3468 if (last_reads
[i
] < 0 || first_writes
[i
] < 0) continue;
3470 for (j
=0; j
< this->next_temp
; j
++) {
3471 /* Don't touch unused registers. */
3472 if (last_reads
[j
] < 0 || first_writes
[j
] < 0) continue;
3474 /* We can merge the two registers if the first write to j is after or
3475 * in the same instruction as the last read from i. Note that the
3476 * register at index i will always be used earlier or at the same time
3477 * as the register at index j. */
3478 if (first_writes
[i
] <= first_writes
[j
] &&
3479 last_reads
[i
] <= first_writes
[j
])
3481 rename_temp_register(j
, i
); /* Replace all references to j with i.*/
3483 /* Update the first_writes and last_reads arrays with the new
3484 * values for the merged register index, and mark the newly unused
3485 * register index as such. */
3486 last_reads
[i
] = last_reads
[j
];
3487 first_writes
[j
] = -1;
3493 ralloc_free(last_reads
);
3494 ralloc_free(first_writes
);
3497 /* Reassign indices to temporary registers by reusing unused indices created
3498 * by optimization passes. */
3500 glsl_to_tgsi_visitor::renumber_registers(void)
3505 for (i
=0; i
< this->next_temp
; i
++) {
3506 if (get_first_temp_read(i
) < 0) continue;
3508 rename_temp_register(i
, new_index
);
3512 this->next_temp
= new_index
;
3516 * Returns a fragment program which implements the current pixel transfer ops.
3517 * Based on get_pixel_transfer_program in st_atom_pixeltransfer.c.
3520 get_pixel_transfer_visitor(struct st_fragment_program
*fp
,
3521 glsl_to_tgsi_visitor
*original
,
3522 int scale_and_bias
, int pixel_maps
)
3524 glsl_to_tgsi_visitor
*v
= new glsl_to_tgsi_visitor();
3525 struct st_context
*st
= st_context(original
->ctx
);
3526 struct gl_program
*prog
= &fp
->Base
.Base
;
3527 struct gl_program_parameter_list
*params
= _mesa_new_parameter_list();
3528 st_src_reg coord
, src0
;
3530 glsl_to_tgsi_instruction
*inst
;
3532 /* Copy attributes of the glsl_to_tgsi_visitor in the original shader. */
3533 v
->ctx
= original
->ctx
;
3535 v
->glsl_version
= original
->glsl_version
;
3536 v
->options
= original
->options
;
3537 v
->next_temp
= original
->next_temp
;
3538 v
->num_address_regs
= original
->num_address_regs
;
3539 v
->samplers_used
= prog
->SamplersUsed
= original
->samplers_used
;
3540 v
->indirect_addr_temps
= original
->indirect_addr_temps
;
3541 v
->indirect_addr_consts
= original
->indirect_addr_consts
;
3542 _mesa_free_parameter_list(v
->immediates
);
3543 v
->immediates
= _mesa_clone_parameter_list(original
->immediates
);
3546 * Get initial pixel color from the texture.
3547 * TEX colorTemp, fragment.texcoord[0], texture[0], 2D;
3549 coord
= st_src_reg(PROGRAM_INPUT
, FRAG_ATTRIB_TEX0
, glsl_type::vec2_type
);
3550 src0
= v
->get_temp(glsl_type::vec4_type
);
3551 dst0
= st_dst_reg(src0
);
3552 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, dst0
, coord
);
3554 inst
->tex_target
= TEXTURE_2D_INDEX
;
3556 prog
->InputsRead
|= (1 << FRAG_ATTRIB_TEX0
);
3557 prog
->SamplersUsed
|= (1 << 0); /* mark sampler 0 as used */
3558 v
->samplers_used
|= (1 << 0);
3560 if (scale_and_bias
) {
3561 static const gl_state_index scale_state
[STATE_LENGTH
] =
3562 { STATE_INTERNAL
, STATE_PT_SCALE
,
3563 (gl_state_index
) 0, (gl_state_index
) 0, (gl_state_index
) 0 };
3564 static const gl_state_index bias_state
[STATE_LENGTH
] =
3565 { STATE_INTERNAL
, STATE_PT_BIAS
,
3566 (gl_state_index
) 0, (gl_state_index
) 0, (gl_state_index
) 0 };
3567 GLint scale_p
, bias_p
;
3568 st_src_reg scale
, bias
;
3570 scale_p
= _mesa_add_state_reference(params
, scale_state
);
3571 bias_p
= _mesa_add_state_reference(params
, bias_state
);
3573 /* MAD colorTemp, colorTemp, scale, bias; */
3574 scale
= st_src_reg(PROGRAM_STATE_VAR
, scale_p
, GLSL_TYPE_FLOAT
);
3575 bias
= st_src_reg(PROGRAM_STATE_VAR
, bias_p
, GLSL_TYPE_FLOAT
);
3576 inst
= v
->emit(NULL
, TGSI_OPCODE_MAD
, dst0
, src0
, scale
, bias
);
3580 st_src_reg temp
= v
->get_temp(glsl_type::vec4_type
);
3581 st_dst_reg temp_dst
= st_dst_reg(temp
);
3583 assert(st
->pixel_xfer
.pixelmap_texture
);
3585 /* With a little effort, we can do four pixel map look-ups with
3586 * two TEX instructions:
3589 /* TEX temp.rg, colorTemp.rgba, texture[1], 2D; */
3590 temp_dst
.writemask
= WRITEMASK_XY
; /* write R,G */
3591 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, temp_dst
, src0
);
3593 inst
->tex_target
= TEXTURE_2D_INDEX
;
3595 /* TEX temp.ba, colorTemp.baba, texture[1], 2D; */
3596 src0
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_Z
, SWIZZLE_W
, SWIZZLE_Z
, SWIZZLE_W
);
3597 temp_dst
.writemask
= WRITEMASK_ZW
; /* write B,A */
3598 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, temp_dst
, src0
);
3600 inst
->tex_target
= TEXTURE_2D_INDEX
;
3602 prog
->SamplersUsed
|= (1 << 1); /* mark sampler 1 as used */
3603 v
->samplers_used
|= (1 << 1);
3605 /* MOV colorTemp, temp; */
3606 inst
= v
->emit(NULL
, TGSI_OPCODE_MOV
, dst0
, temp
);
3609 /* Now copy the instructions from the original glsl_to_tgsi_visitor into the
3611 foreach_iter(exec_list_iterator
, iter
, original
->instructions
) {
3612 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3613 st_src_reg src_regs
[3];
3615 if (inst
->dst
.file
== PROGRAM_OUTPUT
)
3616 prog
->OutputsWritten
|= BITFIELD64_BIT(inst
->dst
.index
);
3618 for (int i
=0; i
<3; i
++) {
3619 src_regs
[i
] = inst
->src
[i
];
3620 if (src_regs
[i
].file
== PROGRAM_INPUT
&&
3621 src_regs
[i
].index
== FRAG_ATTRIB_COL0
)
3623 src_regs
[i
].file
= PROGRAM_TEMPORARY
;
3624 src_regs
[i
].index
= src0
.index
;
3626 else if (src_regs
[i
].file
== PROGRAM_INPUT
)
3627 prog
->InputsRead
|= (1 << src_regs
[i
].index
);
3630 v
->emit(NULL
, inst
->op
, inst
->dst
, src_regs
[0], src_regs
[1], src_regs
[2]);
3633 /* Make modifications to fragment program info. */
3634 prog
->Parameters
= _mesa_combine_parameter_lists(params
,
3635 original
->prog
->Parameters
);
3636 prog
->Attributes
= _mesa_clone_parameter_list(original
->prog
->Attributes
);
3637 prog
->Varying
= _mesa_clone_parameter_list(original
->prog
->Varying
);
3638 _mesa_free_parameter_list(params
);
3639 count_resources(v
, prog
);
3640 fp
->glsl_to_tgsi
= v
;
3644 * Make fragment program for glBitmap:
3645 * Sample the texture and kill the fragment if the bit is 0.
3646 * This program will be combined with the user's fragment program.
3648 * Based on make_bitmap_fragment_program in st_cb_bitmap.c.
3651 get_bitmap_visitor(struct st_fragment_program
*fp
,
3652 glsl_to_tgsi_visitor
*original
, int samplerIndex
)
3654 glsl_to_tgsi_visitor
*v
= new glsl_to_tgsi_visitor();
3655 struct st_context
*st
= st_context(original
->ctx
);
3656 struct gl_program
*prog
= &fp
->Base
.Base
;
3657 st_src_reg coord
, src0
;
3659 glsl_to_tgsi_instruction
*inst
;
3661 /* Copy attributes of the glsl_to_tgsi_visitor in the original shader. */
3662 v
->ctx
= original
->ctx
;
3664 v
->glsl_version
= original
->glsl_version
;
3665 v
->options
= original
->options
;
3666 v
->next_temp
= original
->next_temp
;
3667 v
->num_address_regs
= original
->num_address_regs
;
3668 v
->samplers_used
= prog
->SamplersUsed
= original
->samplers_used
;
3669 v
->indirect_addr_temps
= original
->indirect_addr_temps
;
3670 v
->indirect_addr_consts
= original
->indirect_addr_consts
;
3671 _mesa_free_parameter_list(v
->immediates
);
3672 v
->immediates
= _mesa_clone_parameter_list(original
->immediates
);
3674 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
3675 coord
= st_src_reg(PROGRAM_INPUT
, FRAG_ATTRIB_TEX0
, glsl_type::vec2_type
);
3676 src0
= v
->get_temp(glsl_type::vec4_type
);
3677 dst0
= st_dst_reg(src0
);
3678 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, dst0
, coord
);
3679 inst
->sampler
= samplerIndex
;
3680 inst
->tex_target
= TEXTURE_2D_INDEX
;
3682 prog
->InputsRead
|= (1 << FRAG_ATTRIB_TEX0
);
3683 prog
->SamplersUsed
|= (1 << samplerIndex
); /* mark sampler as used */
3684 v
->samplers_used
|= (1 << samplerIndex
);
3686 /* KIL if -tmp0 < 0 # texel=0 -> keep / texel=0 -> discard */
3687 src0
.negate
= NEGATE_XYZW
;
3688 if (st
->bitmap
.tex_format
== PIPE_FORMAT_L8_UNORM
)
3689 src0
.swizzle
= SWIZZLE_XXXX
;
3690 inst
= v
->emit(NULL
, TGSI_OPCODE_KIL
, undef_dst
, src0
);
3692 /* Now copy the instructions from the original glsl_to_tgsi_visitor into the
3694 foreach_iter(exec_list_iterator
, iter
, original
->instructions
) {
3695 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3696 st_src_reg src_regs
[3];
3698 if (inst
->dst
.file
== PROGRAM_OUTPUT
)
3699 prog
->OutputsWritten
|= BITFIELD64_BIT(inst
->dst
.index
);
3701 for (int i
=0; i
<3; i
++) {
3702 src_regs
[i
] = inst
->src
[i
];
3703 if (src_regs
[i
].file
== PROGRAM_INPUT
)
3704 prog
->InputsRead
|= (1 << src_regs
[i
].index
);
3707 v
->emit(NULL
, inst
->op
, inst
->dst
, src_regs
[0], src_regs
[1], src_regs
[2]);
3710 /* Make modifications to fragment program info. */
3711 prog
->Parameters
= _mesa_clone_parameter_list(original
->prog
->Parameters
);
3712 prog
->Attributes
= _mesa_clone_parameter_list(original
->prog
->Attributes
);
3713 prog
->Varying
= _mesa_clone_parameter_list(original
->prog
->Varying
);
3714 count_resources(v
, prog
);
3715 fp
->glsl_to_tgsi
= v
;
3718 /* ------------------------- TGSI conversion stuff -------------------------- */
3720 unsigned branch_target
;
3725 * Intermediate state used during shader translation.
3727 struct st_translate
{
3728 struct ureg_program
*ureg
;
3730 struct ureg_dst temps
[MAX_TEMPS
];
3731 struct ureg_src
*constants
;
3732 struct ureg_src
*immediates
;
3733 struct ureg_dst outputs
[PIPE_MAX_SHADER_OUTPUTS
];
3734 struct ureg_src inputs
[PIPE_MAX_SHADER_INPUTS
];
3735 struct ureg_dst address
[1];
3736 struct ureg_src samplers
[PIPE_MAX_SAMPLERS
];
3737 struct ureg_src systemValues
[SYSTEM_VALUE_MAX
];
3739 /* Extra info for handling point size clamping in vertex shader */
3740 struct ureg_dst pointSizeResult
; /**< Actual point size output register */
3741 struct ureg_src pointSizeConst
; /**< Point size range constant register */
3742 GLint pointSizeOutIndex
; /**< Temp point size output register */
3743 GLboolean prevInstWrotePointSize
;
3745 const GLuint
*inputMapping
;
3746 const GLuint
*outputMapping
;
3748 /* For every instruction that contains a label (eg CALL), keep
3749 * details so that we can go back afterwards and emit the correct
3750 * tgsi instruction number for each label.
3752 struct label
*labels
;
3753 unsigned labels_size
;
3754 unsigned labels_count
;
3756 /* Keep a record of the tgsi instruction number that each mesa
3757 * instruction starts at, will be used to fix up labels after
3762 unsigned insn_count
;
3764 unsigned procType
; /**< TGSI_PROCESSOR_VERTEX/FRAGMENT */
3769 /** Map Mesa's SYSTEM_VALUE_x to TGSI_SEMANTIC_x */
3770 static unsigned mesa_sysval_to_semantic
[SYSTEM_VALUE_MAX
] = {
3772 TGSI_SEMANTIC_INSTANCEID
3776 * Make note of a branch to a label in the TGSI code.
3777 * After we've emitted all instructions, we'll go over the list
3778 * of labels built here and patch the TGSI code with the actual
3779 * location of each label.
3781 static unsigned *get_label(struct st_translate
*t
, unsigned branch_target
)
3785 if (t
->labels_count
+ 1 >= t
->labels_size
) {
3786 t
->labels_size
= 1 << (util_logbase2(t
->labels_size
) + 1);
3787 t
->labels
= (struct label
*)realloc(t
->labels
,
3788 t
->labels_size
* sizeof(struct label
));
3789 if (t
->labels
== NULL
) {
3790 static unsigned dummy
;
3796 i
= t
->labels_count
++;
3797 t
->labels
[i
].branch_target
= branch_target
;
3798 return &t
->labels
[i
].token
;
3802 * Called prior to emitting the TGSI code for each Mesa instruction.
3803 * Allocate additional space for instructions if needed.
3804 * Update the insn[] array so the next Mesa instruction points to
3805 * the next TGSI instruction.
3807 static void set_insn_start(struct st_translate
*t
, unsigned start
)
3809 if (t
->insn_count
+ 1 >= t
->insn_size
) {
3810 t
->insn_size
= 1 << (util_logbase2(t
->insn_size
) + 1);
3811 t
->insn
= (unsigned *)realloc(t
->insn
, t
->insn_size
* sizeof(t
->insn
[0]));
3812 if (t
->insn
== NULL
) {
3818 t
->insn
[t
->insn_count
++] = start
;
3822 * Map a glsl_to_tgsi constant/immediate to a TGSI immediate.
3824 static struct ureg_src
3825 emit_immediate(struct st_translate
*t
,
3826 struct gl_program_parameter_list
*params
,
3829 struct ureg_program
*ureg
= t
->ureg
;
3831 switch(params
->Parameters
[index
].DataType
)
3837 return ureg_DECL_immediate(ureg
, (float *)params
->ParameterValues
[index
], 4);
3842 return ureg_DECL_immediate_int(ureg
, (int *)params
->ParameterValues
[index
], 4);
3843 case GL_UNSIGNED_INT
:
3844 case GL_UNSIGNED_INT_VEC2
:
3845 case GL_UNSIGNED_INT_VEC3
:
3846 case GL_UNSIGNED_INT_VEC4
:
3851 return ureg_DECL_immediate_uint(ureg
, (unsigned *)params
->ParameterValues
[index
], 4);
3853 assert(!"should not get here - type must be float, int, uint, or bool");
3854 return ureg_src_undef();
3859 * Map a Mesa dst register to a TGSI ureg_dst register.
3861 static struct ureg_dst
3862 dst_register(struct st_translate
*t
,
3863 gl_register_file file
,
3867 case PROGRAM_UNDEFINED
:
3868 return ureg_dst_undef();
3870 case PROGRAM_TEMPORARY
:
3871 if (ureg_dst_is_undef(t
->temps
[index
]))
3872 t
->temps
[index
] = ureg_DECL_temporary(t
->ureg
);
3874 return t
->temps
[index
];
3876 case PROGRAM_OUTPUT
:
3877 if (t
->procType
== TGSI_PROCESSOR_VERTEX
&& index
== VERT_RESULT_PSIZ
)
3878 t
->prevInstWrotePointSize
= GL_TRUE
;
3880 if (t
->procType
== TGSI_PROCESSOR_VERTEX
)
3881 assert(index
< VERT_RESULT_MAX
);
3882 else if (t
->procType
== TGSI_PROCESSOR_FRAGMENT
)
3883 assert(index
< FRAG_RESULT_MAX
);
3885 assert(index
< GEOM_RESULT_MAX
);
3887 assert(t
->outputMapping
[index
] < Elements(t
->outputs
));
3889 return t
->outputs
[t
->outputMapping
[index
]];
3891 case PROGRAM_ADDRESS
:
3892 return t
->address
[index
];
3895 assert(!"unknown dst register file");
3896 return ureg_dst_undef();
3901 * Map a Mesa src register to a TGSI ureg_src register.
3903 static struct ureg_src
3904 src_register(struct st_translate
*t
,
3905 gl_register_file file
,
3909 case PROGRAM_UNDEFINED
:
3910 return ureg_src_undef();
3912 case PROGRAM_TEMPORARY
:
3914 assert(index
< Elements(t
->temps
));
3915 if (ureg_dst_is_undef(t
->temps
[index
]))
3916 t
->temps
[index
] = ureg_DECL_temporary(t
->ureg
);
3917 return ureg_src(t
->temps
[index
]);
3919 case PROGRAM_NAMED_PARAM
:
3920 case PROGRAM_ENV_PARAM
:
3921 case PROGRAM_LOCAL_PARAM
:
3922 case PROGRAM_UNIFORM
:
3924 return t
->constants
[index
];
3925 case PROGRAM_STATE_VAR
:
3926 case PROGRAM_CONSTANT
: /* ie, immediate */
3928 return ureg_DECL_constant(t
->ureg
, 0);
3930 return t
->constants
[index
];
3932 case PROGRAM_IMMEDIATE
:
3933 return t
->immediates
[index
];
3936 assert(t
->inputMapping
[index
] < Elements(t
->inputs
));
3937 return t
->inputs
[t
->inputMapping
[index
]];
3939 case PROGRAM_OUTPUT
:
3940 assert(t
->outputMapping
[index
] < Elements(t
->outputs
));
3941 return ureg_src(t
->outputs
[t
->outputMapping
[index
]]); /* not needed? */
3943 case PROGRAM_ADDRESS
:
3944 return ureg_src(t
->address
[index
]);
3946 case PROGRAM_SYSTEM_VALUE
:
3947 assert(index
< Elements(t
->systemValues
));
3948 return t
->systemValues
[index
];
3951 assert(!"unknown src register file");
3952 return ureg_src_undef();
3957 * Create a TGSI ureg_dst register from an st_dst_reg.
3959 static struct ureg_dst
3960 translate_dst(struct st_translate
*t
,
3961 const st_dst_reg
*dst_reg
,
3964 struct ureg_dst dst
= dst_register(t
,
3968 dst
= ureg_writemask(dst
, dst_reg
->writemask
);
3971 dst
= ureg_saturate(dst
);
3973 if (dst_reg
->reladdr
!= NULL
)
3974 dst
= ureg_dst_indirect(dst
, ureg_src(t
->address
[0]));
3980 * Create a TGSI ureg_src register from an st_src_reg.
3982 static struct ureg_src
3983 translate_src(struct st_translate
*t
, const st_src_reg
*src_reg
)
3985 struct ureg_src src
= src_register(t
, src_reg
->file
, src_reg
->index
);
3987 src
= ureg_swizzle(src
,
3988 GET_SWZ(src_reg
->swizzle
, 0) & 0x3,
3989 GET_SWZ(src_reg
->swizzle
, 1) & 0x3,
3990 GET_SWZ(src_reg
->swizzle
, 2) & 0x3,
3991 GET_SWZ(src_reg
->swizzle
, 3) & 0x3);
3993 if ((src_reg
->negate
& 0xf) == NEGATE_XYZW
)
3994 src
= ureg_negate(src
);
3996 if (src_reg
->reladdr
!= NULL
) {
3997 /* Normally ureg_src_indirect() would be used here, but a stupid compiler
3998 * bug in g++ makes ureg_src_indirect (an inline C function) erroneously
3999 * set the bit for src.Negate. So we have to do the operation manually
4000 * here to work around the compiler's problems. */
4001 /*src = ureg_src_indirect(src, ureg_src(t->address[0]));*/
4002 struct ureg_src addr
= ureg_src(t
->address
[0]);
4004 src
.IndirectFile
= addr
.File
;
4005 src
.IndirectIndex
= addr
.Index
;
4006 src
.IndirectSwizzle
= addr
.SwizzleX
;
4008 if (src_reg
->file
!= PROGRAM_INPUT
&&
4009 src_reg
->file
!= PROGRAM_OUTPUT
) {
4010 /* If src_reg->index was negative, it was set to zero in
4011 * src_register(). Reassign it now. But don't do this
4012 * for input/output regs since they get remapped while
4013 * const buffers don't.
4015 src
.Index
= src_reg
->index
;
4023 compile_tgsi_instruction(struct st_translate
*t
,
4024 const struct glsl_to_tgsi_instruction
*inst
)
4026 struct ureg_program
*ureg
= t
->ureg
;
4028 struct ureg_dst dst
[1];
4029 struct ureg_src src
[4];
4033 num_dst
= num_inst_dst_regs(inst
->op
);
4034 num_src
= num_inst_src_regs(inst
->op
);
4037 dst
[0] = translate_dst(t
,
4041 for (i
= 0; i
< num_src
; i
++)
4042 src
[i
] = translate_src(t
, &inst
->src
[i
]);
4045 case TGSI_OPCODE_BGNLOOP
:
4046 case TGSI_OPCODE_CAL
:
4047 case TGSI_OPCODE_ELSE
:
4048 case TGSI_OPCODE_ENDLOOP
:
4049 case TGSI_OPCODE_IF
:
4050 assert(num_dst
== 0);
4051 ureg_label_insn(ureg
,
4055 inst
->op
== TGSI_OPCODE_CAL
? inst
->function
->sig_id
: 0));
4058 case TGSI_OPCODE_TEX
:
4059 case TGSI_OPCODE_TXB
:
4060 case TGSI_OPCODE_TXD
:
4061 case TGSI_OPCODE_TXL
:
4062 case TGSI_OPCODE_TXP
:
4063 src
[num_src
++] = t
->samplers
[inst
->sampler
];
4067 translate_texture_target(inst
->tex_target
, inst
->tex_shadow
),
4071 case TGSI_OPCODE_SCS
:
4072 dst
[0] = ureg_writemask(dst
[0], TGSI_WRITEMASK_XY
);
4073 ureg_insn(ureg
, inst
->op
, dst
, num_dst
, src
, num_src
);
4086 * Emit the TGSI instructions to adjust the WPOS pixel center convention
4087 * Basically, add (adjX, adjY) to the fragment position.
4090 emit_adjusted_wpos(struct st_translate
*t
,
4091 const struct gl_program
*program
,
4092 float adjX
, float adjY
)
4094 struct ureg_program
*ureg
= t
->ureg
;
4095 struct ureg_dst wpos_temp
= ureg_DECL_temporary(ureg
);
4096 struct ureg_src wpos_input
= t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_WPOS
]];
4098 /* Note that we bias X and Y and pass Z and W through unchanged.
4099 * The shader might also use gl_FragCoord.w and .z.
4101 ureg_ADD(ureg
, wpos_temp
, wpos_input
,
4102 ureg_imm4f(ureg
, adjX
, adjY
, 0.0f
, 0.0f
));
4104 t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_WPOS
]] = ureg_src(wpos_temp
);
4109 * Emit the TGSI instructions for inverting the WPOS y coordinate.
4110 * This code is unavoidable because it also depends on whether
4111 * a FBO is bound (STATE_FB_WPOS_Y_TRANSFORM).
4114 emit_wpos_inversion(struct st_translate
*t
,
4115 const struct gl_program
*program
,
4118 struct ureg_program
*ureg
= t
->ureg
;
4120 /* Fragment program uses fragment position input.
4121 * Need to replace instances of INPUT[WPOS] with temp T
4122 * where T = INPUT[WPOS] by y is inverted.
4124 static const gl_state_index wposTransformState
[STATE_LENGTH
]
4125 = { STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
,
4126 (gl_state_index
)0, (gl_state_index
)0, (gl_state_index
)0 };
4128 /* XXX: note we are modifying the incoming shader here! Need to
4129 * do this before emitting the constant decls below, or this
4132 unsigned wposTransConst
= _mesa_add_state_reference(program
->Parameters
,
4133 wposTransformState
);
4135 struct ureg_src wpostrans
= ureg_DECL_constant(ureg
, wposTransConst
);
4136 struct ureg_dst wpos_temp
;
4137 struct ureg_src wpos_input
= t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_WPOS
]];
4139 /* MOV wpos_temp, input[wpos]
4141 if (wpos_input
.File
== TGSI_FILE_TEMPORARY
)
4142 wpos_temp
= ureg_dst(wpos_input
);
4144 wpos_temp
= ureg_DECL_temporary(ureg
);
4145 ureg_MOV(ureg
, wpos_temp
, wpos_input
);
4149 /* MAD wpos_temp.y, wpos_input, wpostrans.xxxx, wpostrans.yyyy
4152 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
4154 ureg_scalar(wpostrans
, 0),
4155 ureg_scalar(wpostrans
, 1));
4157 /* MAD wpos_temp.y, wpos_input, wpostrans.zzzz, wpostrans.wwww
4160 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
4162 ureg_scalar(wpostrans
, 2),
4163 ureg_scalar(wpostrans
, 3));
4166 /* Use wpos_temp as position input from here on:
4168 t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_WPOS
]] = ureg_src(wpos_temp
);
4173 * Emit fragment position/ooordinate code.
4176 emit_wpos(struct st_context
*st
,
4177 struct st_translate
*t
,
4178 const struct gl_program
*program
,
4179 struct ureg_program
*ureg
)
4181 const struct gl_fragment_program
*fp
=
4182 (const struct gl_fragment_program
*) program
;
4183 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
4184 boolean invert
= FALSE
;
4186 if (fp
->OriginUpperLeft
) {
4187 /* Fragment shader wants origin in upper-left */
4188 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
)) {
4189 /* the driver supports upper-left origin */
4191 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
)) {
4192 /* the driver supports lower-left origin, need to invert Y */
4193 ureg_property_fs_coord_origin(ureg
, TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
4200 /* Fragment shader wants origin in lower-left */
4201 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
))
4202 /* the driver supports lower-left origin */
4203 ureg_property_fs_coord_origin(ureg
, TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
4204 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
))
4205 /* the driver supports upper-left origin, need to invert Y */
4211 if (fp
->PixelCenterInteger
) {
4212 /* Fragment shader wants pixel center integer */
4213 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
))
4214 /* the driver supports pixel center integer */
4215 ureg_property_fs_coord_pixel_center(ureg
, TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
4216 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
))
4217 /* the driver supports pixel center half integer, need to bias X,Y */
4218 emit_adjusted_wpos(t
, program
, 0.5f
, invert
? 0.5f
: -0.5f
);
4223 /* Fragment shader wants pixel center half integer */
4224 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
)) {
4225 /* the driver supports pixel center half integer */
4227 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)) {
4228 /* the driver supports pixel center integer, need to bias X,Y */
4229 ureg_property_fs_coord_pixel_center(ureg
, TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
4230 emit_adjusted_wpos(t
, program
, 0.5f
, invert
? -0.5f
: 0.5f
);
4236 /* we invert after adjustment so that we avoid the MOV to temporary,
4237 * and reuse the adjustment ADD instead */
4238 emit_wpos_inversion(t
, program
, invert
);
4242 * OpenGL's fragment gl_FrontFace input is 1 for front-facing, 0 for back.
4243 * TGSI uses +1 for front, -1 for back.
4244 * This function converts the TGSI value to the GL value. Simply clamping/
4245 * saturating the value to [0,1] does the job.
4248 emit_face_var(struct st_translate
*t
)
4250 struct ureg_program
*ureg
= t
->ureg
;
4251 struct ureg_dst face_temp
= ureg_DECL_temporary(ureg
);
4252 struct ureg_src face_input
= t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_FACE
]];
4254 /* MOV_SAT face_temp, input[face] */
4255 face_temp
= ureg_saturate(face_temp
);
4256 ureg_MOV(ureg
, face_temp
, face_input
);
4258 /* Use face_temp as face input from here on: */
4259 t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_FACE
]] = ureg_src(face_temp
);
4263 emit_edgeflags(struct st_translate
*t
)
4265 struct ureg_program
*ureg
= t
->ureg
;
4266 struct ureg_dst edge_dst
= t
->outputs
[t
->outputMapping
[VERT_RESULT_EDGE
]];
4267 struct ureg_src edge_src
= t
->inputs
[t
->inputMapping
[VERT_ATTRIB_EDGEFLAG
]];
4269 ureg_MOV(ureg
, edge_dst
, edge_src
);
4273 * Translate intermediate IR (glsl_to_tgsi_instruction) to TGSI format.
4274 * \param program the program to translate
4275 * \param numInputs number of input registers used
4276 * \param inputMapping maps Mesa fragment program inputs to TGSI generic
4278 * \param inputSemanticName the TGSI_SEMANTIC flag for each input
4279 * \param inputSemanticIndex the semantic index (ex: which texcoord) for
4281 * \param interpMode the TGSI_INTERPOLATE_LINEAR/PERSP mode for each input
4282 * \param numOutputs number of output registers used
4283 * \param outputMapping maps Mesa fragment program outputs to TGSI
4285 * \param outputSemanticName the TGSI_SEMANTIC flag for each output
4286 * \param outputSemanticIndex the semantic index (ex: which texcoord) for
4289 * \return PIPE_OK or PIPE_ERROR_OUT_OF_MEMORY
4291 extern "C" enum pipe_error
4292 st_translate_program(
4293 struct gl_context
*ctx
,
4295 struct ureg_program
*ureg
,
4296 glsl_to_tgsi_visitor
*program
,
4297 const struct gl_program
*proginfo
,
4299 const GLuint inputMapping
[],
4300 const ubyte inputSemanticName
[],
4301 const ubyte inputSemanticIndex
[],
4302 const GLuint interpMode
[],
4304 const GLuint outputMapping
[],
4305 const ubyte outputSemanticName
[],
4306 const ubyte outputSemanticIndex
[],
4307 boolean passthrough_edgeflags
)
4309 struct st_translate translate
, *t
;
4311 enum pipe_error ret
= PIPE_OK
;
4313 assert(numInputs
<= Elements(t
->inputs
));
4314 assert(numOutputs
<= Elements(t
->outputs
));
4317 memset(t
, 0, sizeof *t
);
4319 t
->procType
= procType
;
4320 t
->inputMapping
= inputMapping
;
4321 t
->outputMapping
= outputMapping
;
4323 t
->pointSizeOutIndex
= -1;
4324 t
->prevInstWrotePointSize
= GL_FALSE
;
4327 * Declare input attributes.
4329 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
4330 for (i
= 0; i
< numInputs
; i
++) {
4331 t
->inputs
[i
] = ureg_DECL_fs_input(ureg
,
4332 inputSemanticName
[i
],
4333 inputSemanticIndex
[i
],
4337 if (proginfo
->InputsRead
& FRAG_BIT_WPOS
) {
4338 /* Must do this after setting up t->inputs, and before
4339 * emitting constant references, below:
4341 emit_wpos(st_context(ctx
), t
, proginfo
, ureg
);
4344 if (proginfo
->InputsRead
& FRAG_BIT_FACE
)
4348 * Declare output attributes.
4350 for (i
= 0; i
< numOutputs
; i
++) {
4351 switch (outputSemanticName
[i
]) {
4352 case TGSI_SEMANTIC_POSITION
:
4353 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4354 TGSI_SEMANTIC_POSITION
, /* Z/Depth */
4355 outputSemanticIndex
[i
]);
4356 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_Z
);
4358 case TGSI_SEMANTIC_STENCIL
:
4359 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4360 TGSI_SEMANTIC_STENCIL
, /* Stencil */
4361 outputSemanticIndex
[i
]);
4362 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_Y
);
4364 case TGSI_SEMANTIC_COLOR
:
4365 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4366 TGSI_SEMANTIC_COLOR
,
4367 outputSemanticIndex
[i
]);
4370 assert(!"fragment shader outputs must be POSITION/STENCIL/COLOR");
4371 return PIPE_ERROR_BAD_INPUT
;
4375 else if (procType
== TGSI_PROCESSOR_GEOMETRY
) {
4376 for (i
= 0; i
< numInputs
; i
++) {
4377 t
->inputs
[i
] = ureg_DECL_gs_input(ureg
,
4379 inputSemanticName
[i
],
4380 inputSemanticIndex
[i
]);
4383 for (i
= 0; i
< numOutputs
; i
++) {
4384 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4385 outputSemanticName
[i
],
4386 outputSemanticIndex
[i
]);
4390 assert(procType
== TGSI_PROCESSOR_VERTEX
);
4392 for (i
= 0; i
< numInputs
; i
++) {
4393 t
->inputs
[i
] = ureg_DECL_vs_input(ureg
, i
);
4396 for (i
= 0; i
< numOutputs
; i
++) {
4397 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4398 outputSemanticName
[i
],
4399 outputSemanticIndex
[i
]);
4400 if ((outputSemanticName
[i
] == TGSI_SEMANTIC_PSIZE
) && proginfo
->Id
) {
4401 /* Writing to the point size result register requires special
4402 * handling to implement clamping.
4404 static const gl_state_index pointSizeClampState
[STATE_LENGTH
]
4405 = { STATE_INTERNAL
, STATE_POINT_SIZE_IMPL_CLAMP
, (gl_state_index
)0, (gl_state_index
)0, (gl_state_index
)0 };
4406 /* XXX: note we are modifying the incoming shader here! Need to
4407 * do this before emitting the constant decls below, or this
4410 unsigned pointSizeClampConst
=
4411 _mesa_add_state_reference(proginfo
->Parameters
,
4412 pointSizeClampState
);
4413 struct ureg_dst psizregtemp
= ureg_DECL_temporary(ureg
);
4414 t
->pointSizeConst
= ureg_DECL_constant(ureg
, pointSizeClampConst
);
4415 t
->pointSizeResult
= t
->outputs
[i
];
4416 t
->pointSizeOutIndex
= i
;
4417 t
->outputs
[i
] = psizregtemp
;
4420 if (passthrough_edgeflags
)
4424 /* Declare address register.
4426 if (program
->num_address_regs
> 0) {
4427 assert(program
->num_address_regs
== 1);
4428 t
->address
[0] = ureg_DECL_address(ureg
);
4431 /* Declare misc input registers
4434 GLbitfield sysInputs
= proginfo
->SystemValuesRead
;
4435 unsigned numSys
= 0;
4436 for (i
= 0; sysInputs
; i
++) {
4437 if (sysInputs
& (1 << i
)) {
4438 unsigned semName
= mesa_sysval_to_semantic
[i
];
4439 t
->systemValues
[i
] = ureg_DECL_system_value(ureg
, numSys
, semName
, 0);
4441 sysInputs
&= ~(1 << i
);
4446 if (program
->indirect_addr_temps
) {
4447 /* If temps are accessed with indirect addressing, declare temporaries
4448 * in sequential order. Else, we declare them on demand elsewhere.
4449 * (Note: the number of temporaries is equal to program->next_temp)
4451 for (i
= 0; i
< (unsigned)program
->next_temp
; i
++) {
4452 /* XXX use TGSI_FILE_TEMPORARY_ARRAY when it's supported by ureg */
4453 t
->temps
[i
] = ureg_DECL_temporary(t
->ureg
);
4457 /* Emit constants and uniforms. TGSI uses a single index space for these,
4458 * so we put all the translated regs in t->constants.
4460 if (proginfo
->Parameters
) {
4461 t
->constants
= (struct ureg_src
*)CALLOC(proginfo
->Parameters
->NumParameters
* sizeof(t
->constants
[0]));
4462 if (t
->constants
== NULL
) {
4463 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
4467 for (i
= 0; i
< proginfo
->Parameters
->NumParameters
; i
++) {
4468 switch (proginfo
->Parameters
->Parameters
[i
].Type
) {
4469 case PROGRAM_ENV_PARAM
:
4470 case PROGRAM_LOCAL_PARAM
:
4471 case PROGRAM_STATE_VAR
:
4472 case PROGRAM_NAMED_PARAM
:
4473 case PROGRAM_UNIFORM
:
4474 t
->constants
[i
] = ureg_DECL_constant(ureg
, i
);
4477 /* Emit immediates for PROGRAM_CONSTANT only when there's no indirect
4478 * addressing of the const buffer.
4479 * FIXME: Be smarter and recognize param arrays:
4480 * indirect addressing is only valid within the referenced
4483 case PROGRAM_CONSTANT
:
4484 if (program
->indirect_addr_consts
)
4485 t
->constants
[i
] = ureg_DECL_constant(ureg
, i
);
4487 t
->constants
[i
] = emit_immediate(t
, proginfo
->Parameters
, i
);
4495 /* Emit immediate values.
4497 t
->immediates
= (struct ureg_src
*)CALLOC(program
->immediates
->NumParameters
* sizeof(struct ureg_src
));
4498 if (t
->immediates
== NULL
) {
4499 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
4502 for (i
= 0; i
< program
->immediates
->NumParameters
; i
++) {
4503 assert(program
->immediates
->Parameters
[i
].Type
== PROGRAM_IMMEDIATE
);
4504 t
->immediates
[i
] = emit_immediate(t
, program
->immediates
, i
);
4507 /* texture samplers */
4508 for (i
= 0; i
< ctx
->Const
.MaxTextureImageUnits
; i
++) {
4509 if (program
->samplers_used
& (1 << i
)) {
4510 t
->samplers
[i
] = ureg_DECL_sampler(ureg
, i
);
4514 /* Emit each instruction in turn:
4516 foreach_iter(exec_list_iterator
, iter
, program
->instructions
) {
4517 set_insn_start(t
, ureg_get_instruction_number(ureg
));
4518 compile_tgsi_instruction(t
, (glsl_to_tgsi_instruction
*)iter
.get());
4520 if (t
->prevInstWrotePointSize
&& proginfo
->Id
) {
4521 /* The previous instruction wrote to the (fake) vertex point size
4522 * result register. Now we need to clamp that value to the min/max
4523 * point size range, putting the result into the real point size
4525 * Note that we can't do this easily at the end of program due to
4526 * possible early return.
4528 set_insn_start(t
, ureg_get_instruction_number(ureg
));
4530 ureg_writemask(t
->outputs
[t
->pointSizeOutIndex
], WRITEMASK_X
),
4531 ureg_src(t
->outputs
[t
->pointSizeOutIndex
]),
4532 ureg_swizzle(t
->pointSizeConst
, 1,1,1,1));
4533 ureg_MIN(t
->ureg
, ureg_writemask(t
->pointSizeResult
, WRITEMASK_X
),
4534 ureg_src(t
->outputs
[t
->pointSizeOutIndex
]),
4535 ureg_swizzle(t
->pointSizeConst
, 2,2,2,2));
4537 t
->prevInstWrotePointSize
= GL_FALSE
;
4540 /* Fix up all emitted labels:
4542 for (i
= 0; i
< t
->labels_count
; i
++) {
4543 ureg_fixup_label(ureg
, t
->labels
[i
].token
,
4544 t
->insn
[t
->labels
[i
].branch_target
]);
4551 FREE(t
->immediates
);
4554 debug_printf("%s: translate error flag set\n", __FUNCTION__
);
4559 /* ----------------------------- End TGSI code ------------------------------ */
4562 * Convert a shader's GLSL IR into a Mesa gl_program, although without
4563 * generating Mesa IR.
4565 static struct gl_program
*
4566 get_mesa_program(struct gl_context
*ctx
,
4567 struct gl_shader_program
*shader_program
,
4568 struct gl_shader
*shader
)
4570 glsl_to_tgsi_visitor
* v
= new glsl_to_tgsi_visitor();
4571 struct gl_program
*prog
;
4573 const char *target_string
;
4575 struct gl_shader_compiler_options
*options
=
4576 &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(shader
->Type
)];
4578 switch (shader
->Type
) {
4579 case GL_VERTEX_SHADER
:
4580 target
= GL_VERTEX_PROGRAM_ARB
;
4581 target_string
= "vertex";
4583 case GL_FRAGMENT_SHADER
:
4584 target
= GL_FRAGMENT_PROGRAM_ARB
;
4585 target_string
= "fragment";
4587 case GL_GEOMETRY_SHADER
:
4588 target
= GL_GEOMETRY_PROGRAM_NV
;
4589 target_string
= "geometry";
4592 assert(!"should not be reached");
4596 validate_ir_tree(shader
->ir
);
4598 prog
= ctx
->Driver
.NewProgram(ctx
, target
, shader_program
->Name
);
4601 prog
->Parameters
= _mesa_new_parameter_list();
4602 prog
->Varying
= _mesa_new_parameter_list();
4603 prog
->Attributes
= _mesa_new_parameter_list();
4606 v
->shader_program
= shader_program
;
4607 v
->options
= options
;
4608 v
->glsl_version
= ctx
->Const
.GLSLVersion
;
4610 add_uniforms_to_parameters_list(shader_program
, shader
, prog
);
4612 /* Emit intermediate IR for main(). */
4613 visit_exec_list(shader
->ir
, v
);
4615 /* Now emit bodies for any functions that were used. */
4617 progress
= GL_FALSE
;
4619 foreach_iter(exec_list_iterator
, iter
, v
->function_signatures
) {
4620 function_entry
*entry
= (function_entry
*)iter
.get();
4622 if (!entry
->bgn_inst
) {
4623 v
->current_function
= entry
;
4625 entry
->bgn_inst
= v
->emit(NULL
, TGSI_OPCODE_BGNSUB
);
4626 entry
->bgn_inst
->function
= entry
;
4628 visit_exec_list(&entry
->sig
->body
, v
);
4630 glsl_to_tgsi_instruction
*last
;
4631 last
= (glsl_to_tgsi_instruction
*)v
->instructions
.get_tail();
4632 if (last
->op
!= TGSI_OPCODE_RET
)
4633 v
->emit(NULL
, TGSI_OPCODE_RET
);
4635 glsl_to_tgsi_instruction
*end
;
4636 end
= v
->emit(NULL
, TGSI_OPCODE_ENDSUB
);
4637 end
->function
= entry
;
4645 /* Print out some information (for debugging purposes) used by the
4646 * optimization passes. */
4647 for (i
=0; i
< v
->next_temp
; i
++) {
4648 int fr
= v
->get_first_temp_read(i
);
4649 int fw
= v
->get_first_temp_write(i
);
4650 int lr
= v
->get_last_temp_read(i
);
4651 int lw
= v
->get_last_temp_write(i
);
4653 printf("Temp %d: FR=%3d FW=%3d LR=%3d LW=%3d\n", i
, fr
, fw
, lr
, lw
);
4658 /* Remove reads to output registers, and to varyings in vertex shaders. */
4659 v
->remove_output_reads(PROGRAM_OUTPUT
);
4660 if (target
== GL_VERTEX_PROGRAM_ARB
)
4661 v
->remove_output_reads(PROGRAM_VARYING
);
4663 /* Perform optimizations on the instructions in the glsl_to_tgsi_visitor. */
4665 v
->copy_propagate();
4666 while (v
->eliminate_dead_code_advanced());
4668 /* FIXME: These passes to optimize temporary registers don't work when there
4669 * is indirect addressing of the temporary register space. We need proper
4670 * array support so that we don't have to give up these passes in every
4671 * shader that uses arrays.
4673 if (!v
->indirect_addr_temps
) {
4674 v
->eliminate_dead_code();
4675 v
->merge_registers();
4676 v
->renumber_registers();
4679 /* Write the END instruction. */
4680 v
->emit(NULL
, TGSI_OPCODE_END
);
4682 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
4684 printf("GLSL IR for linked %s program %d:\n", target_string
,
4685 shader_program
->Name
);
4686 _mesa_print_ir(shader
->ir
, NULL
);
4691 prog
->Instructions
= NULL
;
4692 prog
->NumInstructions
= 0;
4694 do_set_program_inouts(shader
->ir
, prog
);
4695 count_resources(v
, prog
);
4697 check_resources(ctx
, shader_program
, v
, prog
);
4699 _mesa_reference_program(ctx
, &shader
->Program
, prog
);
4701 struct st_vertex_program
*stvp
;
4702 struct st_fragment_program
*stfp
;
4703 struct st_geometry_program
*stgp
;
4705 switch (shader
->Type
) {
4706 case GL_VERTEX_SHADER
:
4707 stvp
= (struct st_vertex_program
*)prog
;
4708 stvp
->glsl_to_tgsi
= v
;
4710 case GL_FRAGMENT_SHADER
:
4711 stfp
= (struct st_fragment_program
*)prog
;
4712 stfp
->glsl_to_tgsi
= v
;
4714 case GL_GEOMETRY_SHADER
:
4715 stgp
= (struct st_geometry_program
*)prog
;
4716 stgp
->glsl_to_tgsi
= v
;
4719 assert(!"should not be reached");
4729 st_new_shader(struct gl_context
*ctx
, GLuint name
, GLuint type
)
4731 struct gl_shader
*shader
;
4732 assert(type
== GL_FRAGMENT_SHADER
|| type
== GL_VERTEX_SHADER
||
4733 type
== GL_GEOMETRY_SHADER_ARB
);
4734 shader
= rzalloc(NULL
, struct gl_shader
);
4736 shader
->Type
= type
;
4737 shader
->Name
= name
;
4738 _mesa_init_shader(ctx
, shader
);
4743 struct gl_shader_program
*
4744 st_new_shader_program(struct gl_context
*ctx
, GLuint name
)
4746 struct gl_shader_program
*shProg
;
4747 shProg
= rzalloc(NULL
, struct gl_shader_program
);
4749 shProg
->Name
= name
;
4750 _mesa_init_shader_program(ctx
, shProg
);
4757 * Called via ctx->Driver.LinkShader()
4758 * This actually involves converting GLSL IR into an intermediate TGSI-like IR
4759 * with code lowering and other optimizations.
4762 st_link_shader(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
4764 assert(prog
->LinkStatus
);
4766 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
4767 if (prog
->_LinkedShaders
[i
] == NULL
)
4771 exec_list
*ir
= prog
->_LinkedShaders
[i
]->ir
;
4772 const struct gl_shader_compiler_options
*options
=
4773 &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(prog
->_LinkedShaders
[i
]->Type
)];
4779 do_mat_op_to_vec(ir
);
4780 lower_instructions(ir
, (MOD_TO_FRACT
| DIV_TO_MUL_RCP
| EXP_TO_EXP2
4782 | ((options
->EmitNoPow
) ? POW_TO_EXP2
: 0)));
4784 progress
= do_lower_jumps(ir
, true, true, options
->EmitNoMainReturn
, options
->EmitNoCont
, options
->EmitNoLoops
) || progress
;
4786 progress
= do_common_optimization(ir
, true, options
->MaxUnrollIterations
) || progress
;
4788 progress
= lower_quadop_vector(ir
, true) || progress
;
4790 if (options
->EmitNoIfs
) {
4791 progress
= lower_discard(ir
) || progress
;
4792 progress
= lower_if_to_cond_assign(ir
) || progress
;
4795 if (options
->EmitNoNoise
)
4796 progress
= lower_noise(ir
) || progress
;
4798 /* If there are forms of indirect addressing that the driver
4799 * cannot handle, perform the lowering pass.
4801 if (options
->EmitNoIndirectInput
|| options
->EmitNoIndirectOutput
4802 || options
->EmitNoIndirectTemp
|| options
->EmitNoIndirectUniform
)
4804 lower_variable_index_to_cond_assign(ir
,
4805 options
->EmitNoIndirectInput
,
4806 options
->EmitNoIndirectOutput
,
4807 options
->EmitNoIndirectTemp
,
4808 options
->EmitNoIndirectUniform
)
4811 progress
= do_vec_index_to_cond_assign(ir
) || progress
;
4814 validate_ir_tree(ir
);
4817 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
4818 struct gl_program
*linked_prog
;
4820 if (prog
->_LinkedShaders
[i
] == NULL
)
4823 linked_prog
= get_mesa_program(ctx
, prog
, prog
->_LinkedShaders
[i
]);
4828 switch (prog
->_LinkedShaders
[i
]->Type
) {
4829 case GL_VERTEX_SHADER
:
4830 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
,
4831 (struct gl_vertex_program
*)linked_prog
);
4832 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_VERTEX_PROGRAM_ARB
,
4835 case GL_FRAGMENT_SHADER
:
4836 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
,
4837 (struct gl_fragment_program
*)linked_prog
);
4838 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
4841 case GL_GEOMETRY_SHADER
:
4842 _mesa_reference_geomprog(ctx
, &prog
->GeometryProgram
,
4843 (struct gl_geometry_program
*)linked_prog
);
4844 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_GEOMETRY_PROGRAM_NV
,
4853 _mesa_reference_program(ctx
, &linked_prog
, NULL
);
4861 * Link a GLSL shader program. Called via glLinkProgram().
4864 st_glsl_link_shader(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
4868 _mesa_clear_shader_program_data(ctx
, prog
);
4870 prog
->LinkStatus
= GL_TRUE
;
4872 for (i
= 0; i
< prog
->NumShaders
; i
++) {
4873 if (!prog
->Shaders
[i
]->CompileStatus
) {
4874 fail_link(prog
, "linking with uncompiled shader");
4875 prog
->LinkStatus
= GL_FALSE
;
4879 prog
->Varying
= _mesa_new_parameter_list();
4880 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
, NULL
);
4881 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
, NULL
);
4882 _mesa_reference_geomprog(ctx
, &prog
->GeometryProgram
, NULL
);
4884 if (prog
->LinkStatus
) {
4885 link_shaders(ctx
, prog
);
4888 if (prog
->LinkStatus
) {
4889 if (!ctx
->Driver
.LinkShader(ctx
, prog
)) {
4890 prog
->LinkStatus
= GL_FALSE
;
4894 set_uniform_initializers(ctx
, prog
);
4896 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
4897 if (!prog
->LinkStatus
) {
4898 printf("GLSL shader program %d failed to link\n", prog
->Name
);
4901 if (prog
->InfoLog
&& prog
->InfoLog
[0] != 0) {
4902 printf("GLSL shader program %d info log:\n", prog
->Name
);
4903 printf("%s\n", prog
->InfoLog
);