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 TGSI 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 TGSI.
256 * Set after conversion from glsl_to_tgsi_instruction to TGSI.
260 /** Storage for the return value. */
261 st_src_reg return_reg
;
264 class glsl_to_tgsi_visitor
: public ir_visitor
{
266 glsl_to_tgsi_visitor();
267 ~glsl_to_tgsi_visitor();
269 function_entry
*current_function
;
271 struct gl_context
*ctx
;
272 struct gl_program
*prog
;
273 struct gl_shader_program
*shader_program
;
274 struct gl_shader_compiler_options
*options
;
275 struct gl_program_parameter_list
*immediates
;
279 int num_address_regs
;
281 bool indirect_addr_temps
;
282 bool indirect_addr_consts
;
286 variable_storage
*find_variable_storage(ir_variable
*var
);
288 function_entry
*get_function_signature(ir_function_signature
*sig
);
290 st_src_reg
get_temp(const glsl_type
*type
);
291 void reladdr_to_temp(ir_instruction
*ir
, st_src_reg
*reg
, int *num_reladdr
);
293 st_src_reg
st_src_reg_for_float(float val
);
294 st_src_reg
st_src_reg_for_int(int val
);
295 st_src_reg
st_src_reg_for_type(int type
, int val
);
298 * \name Visit methods
300 * As typical for the visitor pattern, there must be one \c visit method for
301 * each concrete subclass of \c ir_instruction. Virtual base classes within
302 * the hierarchy should not have \c visit methods.
305 virtual void visit(ir_variable
*);
306 virtual void visit(ir_loop
*);
307 virtual void visit(ir_loop_jump
*);
308 virtual void visit(ir_function_signature
*);
309 virtual void visit(ir_function
*);
310 virtual void visit(ir_expression
*);
311 virtual void visit(ir_swizzle
*);
312 virtual void visit(ir_dereference_variable
*);
313 virtual void visit(ir_dereference_array
*);
314 virtual void visit(ir_dereference_record
*);
315 virtual void visit(ir_assignment
*);
316 virtual void visit(ir_constant
*);
317 virtual void visit(ir_call
*);
318 virtual void visit(ir_return
*);
319 virtual void visit(ir_discard
*);
320 virtual void visit(ir_texture
*);
321 virtual void visit(ir_if
*);
326 /** List of variable_storage */
329 /** List of function_entry */
330 exec_list function_signatures
;
331 int next_signature_id
;
333 /** List of glsl_to_tgsi_instruction */
334 exec_list instructions
;
336 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
);
338 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
,
339 st_dst_reg dst
, st_src_reg src0
);
341 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
,
342 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
);
344 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
,
346 st_src_reg src0
, st_src_reg src1
, st_src_reg src2
);
348 unsigned get_opcode(ir_instruction
*ir
, unsigned op
,
350 st_src_reg src0
, st_src_reg src1
);
353 * Emit the correct dot-product instruction for the type of arguments
355 void emit_dp(ir_instruction
*ir
,
361 void emit_scalar(ir_instruction
*ir
, unsigned op
,
362 st_dst_reg dst
, st_src_reg src0
);
364 void emit_scalar(ir_instruction
*ir
, unsigned op
,
365 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
);
367 void emit_arl(ir_instruction
*ir
, st_dst_reg dst
, st_src_reg src0
);
369 void emit_scs(ir_instruction
*ir
, unsigned op
,
370 st_dst_reg dst
, const st_src_reg
&src
);
372 GLboolean
try_emit_mad(ir_expression
*ir
,
374 GLboolean
try_emit_sat(ir_expression
*ir
);
376 void emit_swz(ir_expression
*ir
);
378 bool process_move_condition(ir_rvalue
*ir
);
380 void remove_output_reads(gl_register_file type
);
381 void simplify_cmp(void);
383 void rename_temp_register(int index
, int new_index
);
384 int get_first_temp_read(int index
);
385 int get_first_temp_write(int index
);
386 int get_last_temp_read(int index
);
387 int get_last_temp_write(int index
);
389 void copy_propagate(void);
390 void eliminate_dead_code(void);
391 int eliminate_dead_code_advanced(void);
392 void merge_registers(void);
393 void renumber_registers(void);
398 static st_src_reg undef_src
= st_src_reg(PROGRAM_UNDEFINED
, 0, GLSL_TYPE_ERROR
);
400 static st_dst_reg undef_dst
= st_dst_reg(PROGRAM_UNDEFINED
, SWIZZLE_NOOP
, GLSL_TYPE_ERROR
);
402 static st_dst_reg address_reg
= st_dst_reg(PROGRAM_ADDRESS
, WRITEMASK_X
, GLSL_TYPE_FLOAT
);
405 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...) PRINTFLIKE(2, 3);
408 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...)
412 ralloc_vasprintf_append(&prog
->InfoLog
, fmt
, args
);
415 prog
->LinkStatus
= GL_FALSE
;
419 swizzle_for_size(int size
)
421 int size_swizzles
[4] = {
422 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
),
423 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
),
424 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
),
425 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
428 assert((size
>= 1) && (size
<= 4));
429 return size_swizzles
[size
- 1];
433 is_tex_instruction(unsigned opcode
)
435 const tgsi_opcode_info
* info
= tgsi_get_opcode_info(opcode
);
440 num_inst_dst_regs(unsigned opcode
)
442 const tgsi_opcode_info
* info
= tgsi_get_opcode_info(opcode
);
443 return info
->num_dst
;
447 num_inst_src_regs(unsigned opcode
)
449 const tgsi_opcode_info
* info
= tgsi_get_opcode_info(opcode
);
450 return info
->is_tex
? info
->num_src
- 1 : info
->num_src
;
453 glsl_to_tgsi_instruction
*
454 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
,
456 st_src_reg src0
, st_src_reg src1
, st_src_reg src2
)
458 glsl_to_tgsi_instruction
*inst
= new(mem_ctx
) glsl_to_tgsi_instruction();
459 int num_reladdr
= 0, i
;
461 op
= get_opcode(ir
, op
, dst
, src0
, src1
);
463 /* If we have to do relative addressing, we want to load the ARL
464 * reg directly for one of the regs, and preload the other reladdr
465 * sources into temps.
467 num_reladdr
+= dst
.reladdr
!= NULL
;
468 num_reladdr
+= src0
.reladdr
!= NULL
;
469 num_reladdr
+= src1
.reladdr
!= NULL
;
470 num_reladdr
+= src2
.reladdr
!= NULL
;
472 reladdr_to_temp(ir
, &src2
, &num_reladdr
);
473 reladdr_to_temp(ir
, &src1
, &num_reladdr
);
474 reladdr_to_temp(ir
, &src0
, &num_reladdr
);
477 emit_arl(ir
, address_reg
, *dst
.reladdr
);
480 assert(num_reladdr
== 0);
490 inst
->function
= NULL
;
492 if (op
== TGSI_OPCODE_ARL
)
493 this->num_address_regs
= 1;
495 /* Update indirect addressing status used by TGSI */
498 case PROGRAM_TEMPORARY
:
499 this->indirect_addr_temps
= true;
501 case PROGRAM_LOCAL_PARAM
:
502 case PROGRAM_ENV_PARAM
:
503 case PROGRAM_STATE_VAR
:
504 case PROGRAM_NAMED_PARAM
:
505 case PROGRAM_CONSTANT
:
506 case PROGRAM_UNIFORM
:
507 this->indirect_addr_consts
= true;
509 case PROGRAM_IMMEDIATE
:
510 assert(!"immediates should not have indirect addressing");
517 for (i
=0; i
<3; i
++) {
518 if(inst
->src
[i
].reladdr
) {
519 switch(inst
->src
[i
].file
) {
520 case PROGRAM_TEMPORARY
:
521 this->indirect_addr_temps
= true;
523 case PROGRAM_LOCAL_PARAM
:
524 case PROGRAM_ENV_PARAM
:
525 case PROGRAM_STATE_VAR
:
526 case PROGRAM_NAMED_PARAM
:
527 case PROGRAM_CONSTANT
:
528 case PROGRAM_UNIFORM
:
529 this->indirect_addr_consts
= true;
531 case PROGRAM_IMMEDIATE
:
532 assert(!"immediates should not have indirect addressing");
541 this->instructions
.push_tail(inst
);
547 glsl_to_tgsi_instruction
*
548 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
,
549 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
)
551 return emit(ir
, op
, dst
, src0
, src1
, undef_src
);
554 glsl_to_tgsi_instruction
*
555 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
,
556 st_dst_reg dst
, st_src_reg src0
)
558 assert(dst
.writemask
!= 0);
559 return emit(ir
, op
, dst
, src0
, undef_src
, undef_src
);
562 glsl_to_tgsi_instruction
*
563 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
)
565 return emit(ir
, op
, undef_dst
, undef_src
, undef_src
, undef_src
);
569 * Determines whether to use an integer, unsigned integer, or float opcode
570 * based on the operands and input opcode, then emits the result.
572 * TODO: type checking for remaining TGSI opcodes
575 glsl_to_tgsi_visitor::get_opcode(ir_instruction
*ir
, unsigned op
,
577 st_src_reg src0
, st_src_reg src1
)
579 int type
= GLSL_TYPE_FLOAT
;
581 if (src0
.type
== GLSL_TYPE_FLOAT
|| src1
.type
== GLSL_TYPE_FLOAT
)
582 type
= GLSL_TYPE_FLOAT
;
583 else if (glsl_version
>= 130)
586 #define case4(c, f, i, u) \
587 case TGSI_OPCODE_##c: \
588 if (type == GLSL_TYPE_INT) op = TGSI_OPCODE_##i; \
589 else if (type == GLSL_TYPE_UINT) op = TGSI_OPCODE_##u; \
590 else op = TGSI_OPCODE_##f; \
592 #define case3(f, i, u) case4(f, f, i, u)
593 #define case2fi(f, i) case4(f, f, i, i)
594 #define case2iu(i, u) case4(i, LAST, i, u)
600 case3(DIV
, IDIV
, UDIV
);
601 case3(MAX
, IMAX
, UMAX
);
602 case3(MIN
, IMIN
, UMIN
);
607 case3(SGE
, ISGE
, USGE
);
608 case3(SLT
, ISLT
, USLT
);
620 assert(op
!= TGSI_OPCODE_LAST
);
625 glsl_to_tgsi_visitor::emit_dp(ir_instruction
*ir
,
626 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
,
629 static const unsigned dot_opcodes
[] = {
630 TGSI_OPCODE_DP2
, TGSI_OPCODE_DP3
, TGSI_OPCODE_DP4
633 emit(ir
, dot_opcodes
[elements
- 2], dst
, src0
, src1
);
637 * Emits TGSI scalar opcodes to produce unique answers across channels.
639 * Some TGSI opcodes are scalar-only, like ARB_fp/vp. The src X
640 * channel determines the result across all channels. So to do a vec4
641 * of this operation, we want to emit a scalar per source channel used
642 * to produce dest channels.
645 glsl_to_tgsi_visitor::emit_scalar(ir_instruction
*ir
, unsigned op
,
647 st_src_reg orig_src0
, st_src_reg orig_src1
)
650 int done_mask
= ~dst
.writemask
;
652 /* TGSI RCP is a scalar operation splatting results to all channels,
653 * like ARB_fp/vp. So emit as many RCPs as necessary to cover our
656 for (i
= 0; i
< 4; i
++) {
657 GLuint this_mask
= (1 << i
);
658 glsl_to_tgsi_instruction
*inst
;
659 st_src_reg src0
= orig_src0
;
660 st_src_reg src1
= orig_src1
;
662 if (done_mask
& this_mask
)
665 GLuint src0_swiz
= GET_SWZ(src0
.swizzle
, i
);
666 GLuint src1_swiz
= GET_SWZ(src1
.swizzle
, i
);
667 for (j
= i
+ 1; j
< 4; j
++) {
668 /* If there is another enabled component in the destination that is
669 * derived from the same inputs, generate its value on this pass as
672 if (!(done_mask
& (1 << j
)) &&
673 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
&&
674 GET_SWZ(src1
.swizzle
, j
) == src1_swiz
) {
675 this_mask
|= (1 << j
);
678 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
679 src0_swiz
, src0_swiz
);
680 src1
.swizzle
= MAKE_SWIZZLE4(src1_swiz
, src1_swiz
,
681 src1_swiz
, src1_swiz
);
683 inst
= emit(ir
, op
, dst
, src0
, src1
);
684 inst
->dst
.writemask
= this_mask
;
685 done_mask
|= this_mask
;
690 glsl_to_tgsi_visitor::emit_scalar(ir_instruction
*ir
, unsigned op
,
691 st_dst_reg dst
, st_src_reg src0
)
693 st_src_reg undef
= undef_src
;
695 undef
.swizzle
= SWIZZLE_XXXX
;
697 emit_scalar(ir
, op
, dst
, src0
, undef
);
701 glsl_to_tgsi_visitor::emit_arl(ir_instruction
*ir
,
702 st_dst_reg dst
, st_src_reg src0
)
704 st_src_reg tmp
= get_temp(glsl_type::float_type
);
706 if (src0
.type
== GLSL_TYPE_INT
)
707 emit(NULL
, TGSI_OPCODE_I2F
, st_dst_reg(tmp
), src0
);
708 else if (src0
.type
== GLSL_TYPE_UINT
)
709 emit(NULL
, TGSI_OPCODE_U2F
, st_dst_reg(tmp
), src0
);
713 emit(NULL
, TGSI_OPCODE_ARL
, dst
, tmp
);
717 * Emit an TGSI_OPCODE_SCS instruction
719 * The \c SCS opcode functions a bit differently than the other TGSI opcodes.
720 * Instead of splatting its result across all four components of the
721 * destination, it writes one value to the \c x component and another value to
722 * the \c y component.
724 * \param ir IR instruction being processed
725 * \param op Either \c TGSI_OPCODE_SIN or \c TGSI_OPCODE_COS depending
726 * on which value is desired.
727 * \param dst Destination register
728 * \param src Source register
731 glsl_to_tgsi_visitor::emit_scs(ir_instruction
*ir
, unsigned op
,
733 const st_src_reg
&src
)
735 /* Vertex programs cannot use the SCS opcode.
737 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
738 emit_scalar(ir
, op
, dst
, src
);
742 const unsigned component
= (op
== TGSI_OPCODE_SIN
) ? 0 : 1;
743 const unsigned scs_mask
= (1U << component
);
744 int done_mask
= ~dst
.writemask
;
747 assert(op
== TGSI_OPCODE_SIN
|| op
== TGSI_OPCODE_COS
);
749 /* If there are compnents in the destination that differ from the component
750 * that will be written by the SCS instrution, we'll need a temporary.
752 if (scs_mask
!= unsigned(dst
.writemask
)) {
753 tmp
= get_temp(glsl_type::vec4_type
);
756 for (unsigned i
= 0; i
< 4; i
++) {
757 unsigned this_mask
= (1U << i
);
758 st_src_reg src0
= src
;
760 if ((done_mask
& this_mask
) != 0)
763 /* The source swizzle specified which component of the source generates
764 * sine / cosine for the current component in the destination. The SCS
765 * instruction requires that this value be swizzle to the X component.
766 * Replace the current swizzle with a swizzle that puts the source in
769 unsigned src0_swiz
= GET_SWZ(src
.swizzle
, i
);
771 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
772 src0_swiz
, src0_swiz
);
773 for (unsigned j
= i
+ 1; j
< 4; j
++) {
774 /* If there is another enabled component in the destination that is
775 * derived from the same inputs, generate its value on this pass as
778 if (!(done_mask
& (1 << j
)) &&
779 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
) {
780 this_mask
|= (1 << j
);
784 if (this_mask
!= scs_mask
) {
785 glsl_to_tgsi_instruction
*inst
;
786 st_dst_reg tmp_dst
= st_dst_reg(tmp
);
788 /* Emit the SCS instruction.
790 inst
= emit(ir
, TGSI_OPCODE_SCS
, tmp_dst
, src0
);
791 inst
->dst
.writemask
= scs_mask
;
793 /* Move the result of the SCS instruction to the desired location in
796 tmp
.swizzle
= MAKE_SWIZZLE4(component
, component
,
797 component
, component
);
798 inst
= emit(ir
, TGSI_OPCODE_SCS
, dst
, tmp
);
799 inst
->dst
.writemask
= this_mask
;
801 /* Emit the SCS instruction to write directly to the destination.
803 glsl_to_tgsi_instruction
*inst
= emit(ir
, TGSI_OPCODE_SCS
, dst
, src0
);
804 inst
->dst
.writemask
= scs_mask
;
807 done_mask
|= this_mask
;
812 glsl_to_tgsi_visitor::st_src_reg_for_float(float val
)
814 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_FLOAT
);
815 union gl_constant_value uval
;
818 src
.index
= _mesa_add_typed_unnamed_constant(this->immediates
, &uval
, 1,
819 GL_FLOAT
, &src
.swizzle
);
825 glsl_to_tgsi_visitor::st_src_reg_for_int(int val
)
827 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_INT
);
828 union gl_constant_value uval
;
830 assert(glsl_version
>= 130);
833 src
.index
= _mesa_add_typed_unnamed_constant(this->immediates
, &uval
, 1,
834 GL_INT
, &src
.swizzle
);
840 glsl_to_tgsi_visitor::st_src_reg_for_type(int type
, int val
)
842 if (glsl_version
>= 130)
843 return type
== GLSL_TYPE_FLOAT
? st_src_reg_for_float(val
) :
844 st_src_reg_for_int(val
);
846 return st_src_reg_for_float(val
);
850 type_size(const struct glsl_type
*type
)
855 switch (type
->base_type
) {
858 case GLSL_TYPE_FLOAT
:
860 if (type
->is_matrix()) {
861 return type
->matrix_columns
;
863 /* Regardless of size of vector, it gets a vec4. This is bad
864 * packing for things like floats, but otherwise arrays become a
865 * mess. Hopefully a later pass over the code can pack scalars
866 * down if appropriate.
870 case GLSL_TYPE_ARRAY
:
871 assert(type
->length
> 0);
872 return type_size(type
->fields
.array
) * type
->length
;
873 case GLSL_TYPE_STRUCT
:
875 for (i
= 0; i
< type
->length
; i
++) {
876 size
+= type_size(type
->fields
.structure
[i
].type
);
879 case GLSL_TYPE_SAMPLER
:
880 /* Samplers take up one slot in UNIFORMS[], but they're baked in
891 * In the initial pass of codegen, we assign temporary numbers to
892 * intermediate results. (not SSA -- variable assignments will reuse
896 glsl_to_tgsi_visitor::get_temp(const glsl_type
*type
)
902 src
.type
= glsl_version
>= 130 ? type
->base_type
: GLSL_TYPE_FLOAT
;
903 src
.file
= PROGRAM_TEMPORARY
;
904 src
.index
= next_temp
;
906 next_temp
+= type_size(type
);
908 if (type
->is_array() || type
->is_record()) {
909 src
.swizzle
= SWIZZLE_NOOP
;
911 for (i
= 0; i
< type
->vector_elements
; i
++)
914 swizzle
[i
] = type
->vector_elements
- 1;
915 src
.swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1],
916 swizzle
[2], swizzle
[3]);
924 glsl_to_tgsi_visitor::find_variable_storage(ir_variable
*var
)
927 variable_storage
*entry
;
929 foreach_iter(exec_list_iterator
, iter
, this->variables
) {
930 entry
= (variable_storage
*)iter
.get();
932 if (entry
->var
== var
)
940 glsl_to_tgsi_visitor::visit(ir_variable
*ir
)
942 if (strcmp(ir
->name
, "gl_FragCoord") == 0) {
943 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
945 fp
->OriginUpperLeft
= ir
->origin_upper_left
;
946 fp
->PixelCenterInteger
= ir
->pixel_center_integer
;
948 } else if (strcmp(ir
->name
, "gl_FragDepth") == 0) {
949 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
950 switch (ir
->depth_layout
) {
951 case ir_depth_layout_none
:
952 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_NONE
;
954 case ir_depth_layout_any
:
955 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_ANY
;
957 case ir_depth_layout_greater
:
958 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_GREATER
;
960 case ir_depth_layout_less
:
961 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_LESS
;
963 case ir_depth_layout_unchanged
:
964 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
972 if (ir
->mode
== ir_var_uniform
&& strncmp(ir
->name
, "gl_", 3) == 0) {
974 const ir_state_slot
*const slots
= ir
->state_slots
;
975 assert(ir
->state_slots
!= NULL
);
977 /* Check if this statevar's setup in the STATE file exactly
978 * matches how we'll want to reference it as a
979 * struct/array/whatever. If not, then we need to move it into
980 * temporary storage and hope that it'll get copy-propagated
983 for (i
= 0; i
< ir
->num_state_slots
; i
++) {
984 if (slots
[i
].swizzle
!= SWIZZLE_XYZW
) {
989 struct variable_storage
*storage
;
991 if (i
== ir
->num_state_slots
) {
992 /* We'll set the index later. */
993 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_STATE_VAR
, -1);
994 this->variables
.push_tail(storage
);
998 /* The variable_storage constructor allocates slots based on the size
999 * of the type. However, this had better match the number of state
1000 * elements that we're going to copy into the new temporary.
1002 assert((int) ir
->num_state_slots
== type_size(ir
->type
));
1004 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_TEMPORARY
,
1006 this->variables
.push_tail(storage
);
1007 this->next_temp
+= type_size(ir
->type
);
1009 dst
= st_dst_reg(st_src_reg(PROGRAM_TEMPORARY
, storage
->index
,
1010 glsl_version
>= 130 ? ir
->type
->base_type
: GLSL_TYPE_FLOAT
));
1014 for (unsigned int i
= 0; i
< ir
->num_state_slots
; i
++) {
1015 int index
= _mesa_add_state_reference(this->prog
->Parameters
,
1016 (gl_state_index
*)slots
[i
].tokens
);
1018 if (storage
->file
== PROGRAM_STATE_VAR
) {
1019 if (storage
->index
== -1) {
1020 storage
->index
= index
;
1022 assert(index
== storage
->index
+ (int)i
);
1025 st_src_reg
src(PROGRAM_STATE_VAR
, index
,
1026 glsl_version
>= 130 ? ir
->type
->base_type
: GLSL_TYPE_FLOAT
);
1027 src
.swizzle
= slots
[i
].swizzle
;
1028 emit(ir
, TGSI_OPCODE_MOV
, dst
, src
);
1029 /* even a float takes up a whole vec4 reg in a struct/array. */
1034 if (storage
->file
== PROGRAM_TEMPORARY
&&
1035 dst
.index
!= storage
->index
+ (int) ir
->num_state_slots
) {
1036 fail_link(this->shader_program
,
1037 "failed to load builtin uniform `%s' (%d/%d regs loaded)\n",
1038 ir
->name
, dst
.index
- storage
->index
,
1039 type_size(ir
->type
));
1045 glsl_to_tgsi_visitor::visit(ir_loop
*ir
)
1047 ir_dereference_variable
*counter
= NULL
;
1049 if (ir
->counter
!= NULL
)
1050 counter
= new(ir
) ir_dereference_variable(ir
->counter
);
1052 if (ir
->from
!= NULL
) {
1053 assert(ir
->counter
!= NULL
);
1055 ir_assignment
*a
= new(ir
) ir_assignment(counter
, ir
->from
, NULL
);
1061 emit(NULL
, TGSI_OPCODE_BGNLOOP
);
1065 new(ir
) ir_expression(ir
->cmp
, glsl_type::bool_type
,
1067 ir_if
*if_stmt
= new(ir
) ir_if(e
);
1069 ir_loop_jump
*brk
= new(ir
) ir_loop_jump(ir_loop_jump::jump_break
);
1071 if_stmt
->then_instructions
.push_tail(brk
);
1073 if_stmt
->accept(this);
1080 visit_exec_list(&ir
->body_instructions
, this);
1082 if (ir
->increment
) {
1084 new(ir
) ir_expression(ir_binop_add
, counter
->type
,
1085 counter
, ir
->increment
);
1087 ir_assignment
*a
= new(ir
) ir_assignment(counter
, e
, NULL
);
1094 emit(NULL
, TGSI_OPCODE_ENDLOOP
);
1098 glsl_to_tgsi_visitor::visit(ir_loop_jump
*ir
)
1101 case ir_loop_jump::jump_break
:
1102 emit(NULL
, TGSI_OPCODE_BRK
);
1104 case ir_loop_jump::jump_continue
:
1105 emit(NULL
, TGSI_OPCODE_CONT
);
1112 glsl_to_tgsi_visitor::visit(ir_function_signature
*ir
)
1119 glsl_to_tgsi_visitor::visit(ir_function
*ir
)
1121 /* Ignore function bodies other than main() -- we shouldn't see calls to
1122 * them since they should all be inlined before we get to glsl_to_tgsi.
1124 if (strcmp(ir
->name
, "main") == 0) {
1125 const ir_function_signature
*sig
;
1128 sig
= ir
->matching_signature(&empty
);
1132 foreach_iter(exec_list_iterator
, iter
, sig
->body
) {
1133 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
1141 glsl_to_tgsi_visitor::try_emit_mad(ir_expression
*ir
, int mul_operand
)
1143 int nonmul_operand
= 1 - mul_operand
;
1145 st_dst_reg result_dst
;
1147 ir_expression
*expr
= ir
->operands
[mul_operand
]->as_expression();
1148 if (!expr
|| expr
->operation
!= ir_binop_mul
)
1151 expr
->operands
[0]->accept(this);
1153 expr
->operands
[1]->accept(this);
1155 ir
->operands
[nonmul_operand
]->accept(this);
1158 this->result
= get_temp(ir
->type
);
1159 result_dst
= st_dst_reg(this->result
);
1160 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1161 emit(ir
, TGSI_OPCODE_MAD
, result_dst
, a
, b
, c
);
1167 glsl_to_tgsi_visitor::try_emit_sat(ir_expression
*ir
)
1169 /* Saturates were only introduced to vertex programs in
1170 * NV_vertex_program3, so don't give them to drivers in the VP.
1172 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
)
1175 ir_rvalue
*sat_src
= ir
->as_rvalue_to_saturate();
1179 sat_src
->accept(this);
1180 st_src_reg src
= this->result
;
1182 this->result
= get_temp(ir
->type
);
1183 st_dst_reg result_dst
= st_dst_reg(this->result
);
1184 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1185 glsl_to_tgsi_instruction
*inst
;
1186 inst
= emit(ir
, TGSI_OPCODE_MOV
, result_dst
, src
);
1187 inst
->saturate
= true;
1193 glsl_to_tgsi_visitor::reladdr_to_temp(ir_instruction
*ir
,
1194 st_src_reg
*reg
, int *num_reladdr
)
1199 emit_arl(ir
, address_reg
, *reg
->reladdr
);
1201 if (*num_reladdr
!= 1) {
1202 st_src_reg temp
= get_temp(glsl_type::vec4_type
);
1204 emit(ir
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), *reg
);
1212 glsl_to_tgsi_visitor::visit(ir_expression
*ir
)
1214 unsigned int operand
;
1215 st_src_reg op
[Elements(ir
->operands
)];
1216 st_src_reg result_src
;
1217 st_dst_reg result_dst
;
1219 /* Quick peephole: Emit MAD(a, b, c) instead of ADD(MUL(a, b), c)
1221 if (ir
->operation
== ir_binop_add
) {
1222 if (try_emit_mad(ir
, 1))
1224 if (try_emit_mad(ir
, 0))
1227 if (try_emit_sat(ir
))
1230 if (ir
->operation
== ir_quadop_vector
)
1231 assert(!"ir_quadop_vector should have been lowered");
1233 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
1234 this->result
.file
= PROGRAM_UNDEFINED
;
1235 ir
->operands
[operand
]->accept(this);
1236 if (this->result
.file
== PROGRAM_UNDEFINED
) {
1238 printf("Failed to get tree for expression operand:\n");
1239 ir
->operands
[operand
]->accept(&v
);
1242 op
[operand
] = this->result
;
1244 /* Matrix expression operands should have been broken down to vector
1245 * operations already.
1247 assert(!ir
->operands
[operand
]->type
->is_matrix());
1250 int vector_elements
= ir
->operands
[0]->type
->vector_elements
;
1251 if (ir
->operands
[1]) {
1252 vector_elements
= MAX2(vector_elements
,
1253 ir
->operands
[1]->type
->vector_elements
);
1256 this->result
.file
= PROGRAM_UNDEFINED
;
1258 /* Storage for our result. Ideally for an assignment we'd be using
1259 * the actual storage for the result here, instead.
1261 result_src
= get_temp(ir
->type
);
1262 /* convenience for the emit functions below. */
1263 result_dst
= st_dst_reg(result_src
);
1264 /* Limit writes to the channels that will be used by result_src later.
1265 * This does limit this temp's use as a temporary for multi-instruction
1268 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1270 switch (ir
->operation
) {
1271 case ir_unop_logic_not
:
1272 emit(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], st_src_reg_for_type(result_dst
.type
, 0));
1275 assert(result_dst
.type
== GLSL_TYPE_FLOAT
|| result_dst
.type
== GLSL_TYPE_INT
);
1276 if (result_dst
.type
== GLSL_TYPE_INT
)
1277 emit(ir
, TGSI_OPCODE_INEG
, result_dst
, op
[0]);
1279 op
[0].negate
= ~op
[0].negate
;
1284 assert(result_dst
.type
== GLSL_TYPE_FLOAT
);
1285 emit(ir
, TGSI_OPCODE_ABS
, result_dst
, op
[0]);
1288 emit(ir
, TGSI_OPCODE_SSG
, result_dst
, op
[0]);
1291 emit_scalar(ir
, TGSI_OPCODE_RCP
, result_dst
, op
[0]);
1295 emit_scalar(ir
, TGSI_OPCODE_EX2
, result_dst
, op
[0]);
1299 assert(!"not reached: should be handled by ir_explog_to_explog2");
1302 emit_scalar(ir
, TGSI_OPCODE_LG2
, result_dst
, op
[0]);
1305 emit_scalar(ir
, TGSI_OPCODE_SIN
, result_dst
, op
[0]);
1308 emit_scalar(ir
, TGSI_OPCODE_COS
, result_dst
, op
[0]);
1310 case ir_unop_sin_reduced
:
1311 emit_scs(ir
, TGSI_OPCODE_SIN
, result_dst
, op
[0]);
1313 case ir_unop_cos_reduced
:
1314 emit_scs(ir
, TGSI_OPCODE_COS
, result_dst
, op
[0]);
1318 emit(ir
, TGSI_OPCODE_DDX
, result_dst
, op
[0]);
1321 op
[0].negate
= ~op
[0].negate
;
1322 emit(ir
, TGSI_OPCODE_DDY
, result_dst
, op
[0]);
1325 case ir_unop_noise
: {
1326 /* At some point, a motivated person could add a better
1327 * implementation of noise. Currently not even the nvidia
1328 * binary drivers do anything more than this. In any case, the
1329 * place to do this is in the GL state tracker, not the poor
1332 emit(ir
, TGSI_OPCODE_MOV
, result_dst
, st_src_reg_for_float(0.5));
1337 emit(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1340 emit(ir
, TGSI_OPCODE_SUB
, result_dst
, op
[0], op
[1]);
1344 emit(ir
, TGSI_OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1347 if (result_dst
.type
== GLSL_TYPE_FLOAT
)
1348 assert(!"not reached: should be handled by ir_div_to_mul_rcp");
1350 emit(ir
, TGSI_OPCODE_DIV
, result_dst
, op
[0], op
[1]);
1353 if (result_dst
.type
== GLSL_TYPE_FLOAT
)
1354 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
1356 emit(ir
, TGSI_OPCODE_MOD
, result_dst
, op
[0], op
[1]);
1360 emit(ir
, TGSI_OPCODE_SLT
, result_dst
, op
[0], op
[1]);
1362 case ir_binop_greater
:
1363 emit(ir
, TGSI_OPCODE_SGT
, result_dst
, op
[0], op
[1]);
1365 case ir_binop_lequal
:
1366 emit(ir
, TGSI_OPCODE_SLE
, result_dst
, op
[0], op
[1]);
1368 case ir_binop_gequal
:
1369 emit(ir
, TGSI_OPCODE_SGE
, result_dst
, op
[0], op
[1]);
1371 case ir_binop_equal
:
1372 emit(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1374 case ir_binop_nequal
:
1375 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1377 case ir_binop_all_equal
:
1378 /* "==" operator producing a scalar boolean. */
1379 if (ir
->operands
[0]->type
->is_vector() ||
1380 ir
->operands
[1]->type
->is_vector()) {
1381 st_src_reg temp
= get_temp(glsl_version
>= 130 ?
1382 glsl_type::get_instance(ir
->operands
[0]->type
->base_type
, 4, 1) :
1383 glsl_type::vec4_type
);
1384 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
1385 emit(ir
, TGSI_OPCODE_SNE
, st_dst_reg(temp
), op
[0], op
[1]);
1386 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1387 emit(ir
, TGSI_OPCODE_SEQ
, result_dst
, result_src
, st_src_reg_for_float(0.0));
1389 emit(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1392 case ir_binop_any_nequal
:
1393 /* "!=" operator producing a scalar boolean. */
1394 if (ir
->operands
[0]->type
->is_vector() ||
1395 ir
->operands
[1]->type
->is_vector()) {
1396 st_src_reg temp
= get_temp(glsl_version
>= 130 ?
1397 glsl_type::get_instance(ir
->operands
[0]->type
->base_type
, 4, 1) :
1398 glsl_type::vec4_type
);
1399 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
1400 emit(ir
, TGSI_OPCODE_SNE
, st_dst_reg(temp
), op
[0], op
[1]);
1401 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1402 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, result_src
, st_src_reg_for_float(0.0));
1404 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1409 assert(ir
->operands
[0]->type
->is_vector());
1410 emit_dp(ir
, result_dst
, op
[0], op
[0],
1411 ir
->operands
[0]->type
->vector_elements
);
1412 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, result_src
, st_src_reg_for_float(0.0));
1415 case ir_binop_logic_xor
:
1416 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1419 case ir_binop_logic_or
:
1420 /* This could be a saturated add and skip the SNE. */
1421 emit(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1422 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, result_src
, st_src_reg_for_float(0.0));
1425 case ir_binop_logic_and
:
1426 /* the bool args are stored as float 0.0 or 1.0, so "mul" gives us "and". */
1427 emit(ir
, TGSI_OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1431 assert(ir
->operands
[0]->type
->is_vector());
1432 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
1433 emit_dp(ir
, result_dst
, op
[0], op
[1],
1434 ir
->operands
[0]->type
->vector_elements
);
1438 /* sqrt(x) = x * rsq(x). */
1439 emit_scalar(ir
, TGSI_OPCODE_RSQ
, result_dst
, op
[0]);
1440 emit(ir
, TGSI_OPCODE_MUL
, result_dst
, result_src
, op
[0]);
1441 /* For incoming channels <= 0, set the result to 0. */
1442 op
[0].negate
= ~op
[0].negate
;
1443 emit(ir
, TGSI_OPCODE_CMP
, result_dst
,
1444 op
[0], result_src
, st_src_reg_for_float(0.0));
1447 emit_scalar(ir
, TGSI_OPCODE_RSQ
, result_dst
, op
[0]);
1451 if (glsl_version
>= 130) {
1452 emit(ir
, TGSI_OPCODE_I2F
, result_dst
, op
[0]);
1456 /* Booleans are stored as integers (or floats in GLSL 1.20 and lower). */
1460 if (glsl_version
>= 130)
1461 emit(ir
, TGSI_OPCODE_F2I
, result_dst
, op
[0]);
1463 emit(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
1467 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0],
1468 st_src_reg_for_type(result_dst
.type
, 0));
1471 emit(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
1474 op
[0].negate
= ~op
[0].negate
;
1475 emit(ir
, TGSI_OPCODE_FLR
, result_dst
, op
[0]);
1476 result_src
.negate
= ~result_src
.negate
;
1479 emit(ir
, TGSI_OPCODE_FLR
, result_dst
, op
[0]);
1482 emit(ir
, TGSI_OPCODE_FRC
, result_dst
, op
[0]);
1486 emit(ir
, TGSI_OPCODE_MIN
, result_dst
, op
[0], op
[1]);
1489 emit(ir
, TGSI_OPCODE_MAX
, result_dst
, op
[0], op
[1]);
1492 emit_scalar(ir
, TGSI_OPCODE_POW
, result_dst
, op
[0], op
[1]);
1495 case ir_unop_bit_not
:
1496 if (glsl_version
>= 130) {
1497 emit(ir
, TGSI_OPCODE_NOT
, result_dst
, op
[0]);
1501 if (glsl_version
>= 130) {
1502 emit(ir
, TGSI_OPCODE_U2F
, result_dst
, op
[0]);
1505 case ir_binop_lshift
:
1506 if (glsl_version
>= 130) {
1507 emit(ir
, TGSI_OPCODE_SHL
, result_dst
, op
[0]);
1510 case ir_binop_rshift
:
1511 if (glsl_version
>= 130) {
1512 emit(ir
, TGSI_OPCODE_ISHR
, result_dst
, op
[0]);
1515 case ir_binop_bit_and
:
1516 if (glsl_version
>= 130) {
1517 emit(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0]);
1520 case ir_binop_bit_xor
:
1521 if (glsl_version
>= 130) {
1522 emit(ir
, TGSI_OPCODE_XOR
, result_dst
, op
[0]);
1525 case ir_binop_bit_or
:
1526 if (glsl_version
>= 130) {
1527 emit(ir
, TGSI_OPCODE_OR
, result_dst
, op
[0]);
1530 case ir_unop_round_even
:
1531 assert(!"GLSL 1.30 features unsupported");
1534 case ir_quadop_vector
:
1535 /* This operation should have already been handled.
1537 assert(!"Should not get here.");
1541 this->result
= result_src
;
1546 glsl_to_tgsi_visitor::visit(ir_swizzle
*ir
)
1552 /* Note that this is only swizzles in expressions, not those on the left
1553 * hand side of an assignment, which do write masking. See ir_assignment
1557 ir
->val
->accept(this);
1559 assert(src
.file
!= PROGRAM_UNDEFINED
);
1561 for (i
= 0; i
< 4; i
++) {
1562 if (i
< ir
->type
->vector_elements
) {
1565 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.x
);
1568 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.y
);
1571 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.z
);
1574 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.w
);
1578 /* If the type is smaller than a vec4, replicate the last
1581 swizzle
[i
] = swizzle
[ir
->type
->vector_elements
- 1];
1585 src
.swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
1591 glsl_to_tgsi_visitor::visit(ir_dereference_variable
*ir
)
1593 variable_storage
*entry
= find_variable_storage(ir
->var
);
1594 ir_variable
*var
= ir
->var
;
1597 switch (var
->mode
) {
1598 case ir_var_uniform
:
1599 entry
= new(mem_ctx
) variable_storage(var
, PROGRAM_UNIFORM
,
1601 this->variables
.push_tail(entry
);
1605 /* The linker assigns locations for varyings and attributes,
1606 * including deprecated builtins (like gl_Color), user-assign
1607 * generic attributes (glBindVertexLocation), and
1608 * user-defined varyings.
1610 * FINISHME: We would hit this path for function arguments. Fix!
1612 assert(var
->location
!= -1);
1613 entry
= new(mem_ctx
) variable_storage(var
,
1616 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
&&
1617 var
->location
>= VERT_ATTRIB_GENERIC0
) {
1618 _mesa_add_attribute(this->prog
->Attributes
,
1620 _mesa_sizeof_glsl_type(var
->type
->gl_type
),
1622 var
->location
- VERT_ATTRIB_GENERIC0
);
1626 assert(var
->location
!= -1);
1627 entry
= new(mem_ctx
) variable_storage(var
,
1631 case ir_var_system_value
:
1632 entry
= new(mem_ctx
) variable_storage(var
,
1633 PROGRAM_SYSTEM_VALUE
,
1637 case ir_var_temporary
:
1638 entry
= new(mem_ctx
) variable_storage(var
, PROGRAM_TEMPORARY
,
1640 this->variables
.push_tail(entry
);
1642 next_temp
+= type_size(var
->type
);
1647 printf("Failed to make storage for %s\n", var
->name
);
1652 this->result
= st_src_reg(entry
->file
, entry
->index
, var
->type
);
1653 if (glsl_version
<= 120)
1654 this->result
.type
= GLSL_TYPE_FLOAT
;
1658 glsl_to_tgsi_visitor::visit(ir_dereference_array
*ir
)
1662 int element_size
= type_size(ir
->type
);
1664 index
= ir
->array_index
->constant_expression_value();
1666 ir
->array
->accept(this);
1670 src
.index
+= index
->value
.i
[0] * element_size
;
1672 st_src_reg array_base
= this->result
;
1673 /* Variable index array dereference. It eats the "vec4" of the
1674 * base of the array and an index that offsets the TGSI register
1677 ir
->array_index
->accept(this);
1679 st_src_reg index_reg
;
1681 if (element_size
== 1) {
1682 index_reg
= this->result
;
1684 index_reg
= get_temp(glsl_type::float_type
);
1686 emit(ir
, TGSI_OPCODE_MUL
, st_dst_reg(index_reg
),
1687 this->result
, st_src_reg_for_float(element_size
));
1690 src
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
1691 memcpy(src
.reladdr
, &index_reg
, sizeof(index_reg
));
1694 /* If the type is smaller than a vec4, replicate the last channel out. */
1695 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
1696 src
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1698 src
.swizzle
= SWIZZLE_NOOP
;
1704 glsl_to_tgsi_visitor::visit(ir_dereference_record
*ir
)
1707 const glsl_type
*struct_type
= ir
->record
->type
;
1710 ir
->record
->accept(this);
1712 for (i
= 0; i
< struct_type
->length
; i
++) {
1713 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
1715 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
1718 /* If the type is smaller than a vec4, replicate the last channel out. */
1719 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
1720 this->result
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1722 this->result
.swizzle
= SWIZZLE_NOOP
;
1724 this->result
.index
+= offset
;
1728 * We want to be careful in assignment setup to hit the actual storage
1729 * instead of potentially using a temporary like we might with the
1730 * ir_dereference handler.
1733 get_assignment_lhs(ir_dereference
*ir
, glsl_to_tgsi_visitor
*v
)
1735 /* The LHS must be a dereference. If the LHS is a variable indexed array
1736 * access of a vector, it must be separated into a series conditional moves
1737 * before reaching this point (see ir_vec_index_to_cond_assign).
1739 assert(ir
->as_dereference());
1740 ir_dereference_array
*deref_array
= ir
->as_dereference_array();
1742 assert(!deref_array
->array
->type
->is_vector());
1745 /* Use the rvalue deref handler for the most part. We'll ignore
1746 * swizzles in it and write swizzles using writemask, though.
1749 return st_dst_reg(v
->result
);
1753 * Process the condition of a conditional assignment
1755 * Examines the condition of a conditional assignment to generate the optimal
1756 * first operand of a \c CMP instruction. If the condition is a relational
1757 * operator with 0 (e.g., \c ir_binop_less), the value being compared will be
1758 * used as the source for the \c CMP instruction. Otherwise the comparison
1759 * is processed to a boolean result, and the boolean result is used as the
1760 * operand to the CMP instruction.
1763 glsl_to_tgsi_visitor::process_move_condition(ir_rvalue
*ir
)
1765 ir_rvalue
*src_ir
= ir
;
1767 bool switch_order
= false;
1769 ir_expression
*const expr
= ir
->as_expression();
1770 if ((expr
!= NULL
) && (expr
->get_num_operands() == 2)) {
1771 bool zero_on_left
= false;
1773 if (expr
->operands
[0]->is_zero()) {
1774 src_ir
= expr
->operands
[1];
1775 zero_on_left
= true;
1776 } else if (expr
->operands
[1]->is_zero()) {
1777 src_ir
= expr
->operands
[0];
1778 zero_on_left
= false;
1782 * (a < 0) T F F ( a < 0) T F F
1783 * (0 < a) F F T (-a < 0) F F T
1784 * (a <= 0) T T F (-a < 0) F F T (swap order of other operands)
1785 * (0 <= a) F T T ( a < 0) T F F (swap order of other operands)
1786 * (a > 0) F F T (-a < 0) F F T
1787 * (0 > a) T F F ( a < 0) T F F
1788 * (a >= 0) F T T ( a < 0) T F F (swap order of other operands)
1789 * (0 >= a) T T F (-a < 0) F F T (swap order of other operands)
1791 * Note that exchanging the order of 0 and 'a' in the comparison simply
1792 * means that the value of 'a' should be negated.
1795 switch (expr
->operation
) {
1797 switch_order
= false;
1798 negate
= zero_on_left
;
1801 case ir_binop_greater
:
1802 switch_order
= false;
1803 negate
= !zero_on_left
;
1806 case ir_binop_lequal
:
1807 switch_order
= true;
1808 negate
= !zero_on_left
;
1811 case ir_binop_gequal
:
1812 switch_order
= true;
1813 negate
= zero_on_left
;
1817 /* This isn't the right kind of comparison afterall, so make sure
1818 * the whole condition is visited.
1826 src_ir
->accept(this);
1828 /* We use the TGSI_OPCODE_CMP (a < 0 ? b : c) for conditional moves, and the
1829 * condition we produced is 0.0 or 1.0. By flipping the sign, we can
1830 * choose which value TGSI_OPCODE_CMP produces without an extra instruction
1831 * computing the condition.
1834 this->result
.negate
= ~this->result
.negate
;
1836 return switch_order
;
1840 glsl_to_tgsi_visitor::visit(ir_assignment
*ir
)
1846 ir
->rhs
->accept(this);
1849 l
= get_assignment_lhs(ir
->lhs
, this);
1851 /* FINISHME: This should really set to the correct maximal writemask for each
1852 * FINISHME: component written (in the loops below). This case can only
1853 * FINISHME: occur for matrices, arrays, and structures.
1855 if (ir
->write_mask
== 0) {
1856 assert(!ir
->lhs
->type
->is_scalar() && !ir
->lhs
->type
->is_vector());
1857 l
.writemask
= WRITEMASK_XYZW
;
1858 } else if (ir
->lhs
->type
->is_scalar() &&
1859 ir
->lhs
->variable_referenced()->mode
== ir_var_out
) {
1860 /* FINISHME: This hack makes writing to gl_FragDepth, which lives in the
1861 * FINISHME: W component of fragment shader output zero, work correctly.
1863 l
.writemask
= WRITEMASK_XYZW
;
1866 int first_enabled_chan
= 0;
1869 l
.writemask
= ir
->write_mask
;
1871 for (int i
= 0; i
< 4; i
++) {
1872 if (l
.writemask
& (1 << i
)) {
1873 first_enabled_chan
= GET_SWZ(r
.swizzle
, i
);
1878 /* Swizzle a small RHS vector into the channels being written.
1880 * glsl ir treats write_mask as dictating how many channels are
1881 * present on the RHS while TGSI treats write_mask as just
1882 * showing which channels of the vec4 RHS get written.
1884 for (int i
= 0; i
< 4; i
++) {
1885 if (l
.writemask
& (1 << i
))
1886 swizzles
[i
] = GET_SWZ(r
.swizzle
, rhs_chan
++);
1888 swizzles
[i
] = first_enabled_chan
;
1890 r
.swizzle
= MAKE_SWIZZLE4(swizzles
[0], swizzles
[1],
1891 swizzles
[2], swizzles
[3]);
1894 assert(l
.file
!= PROGRAM_UNDEFINED
);
1895 assert(r
.file
!= PROGRAM_UNDEFINED
);
1897 if (ir
->condition
) {
1898 const bool switch_order
= this->process_move_condition(ir
->condition
);
1899 st_src_reg condition
= this->result
;
1901 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
1902 st_src_reg l_src
= st_src_reg(l
);
1903 l_src
.swizzle
= swizzle_for_size(ir
->lhs
->type
->vector_elements
);
1906 emit(ir
, TGSI_OPCODE_CMP
, l
, condition
, l_src
, r
);
1908 emit(ir
, TGSI_OPCODE_CMP
, l
, condition
, r
, l_src
);
1914 } else if (ir
->rhs
->as_expression() &&
1915 this->instructions
.get_tail() &&
1916 ir
->rhs
== ((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->ir
&&
1917 type_size(ir
->lhs
->type
) == 1) {
1918 /* To avoid emitting an extra MOV when assigning an expression to a
1919 * variable, emit the last instruction of the expression again, but
1920 * replace the destination register with the target of the assignment.
1921 * Dead code elimination will remove the original instruction.
1923 glsl_to_tgsi_instruction
*inst
;
1924 inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
1925 emit(ir
, inst
->op
, l
, inst
->src
[0], inst
->src
[1], inst
->src
[2]);
1927 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
1928 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
1937 glsl_to_tgsi_visitor::visit(ir_constant
*ir
)
1940 GLfloat stack_vals
[4] = { 0 };
1941 gl_constant_value
*values
= (gl_constant_value
*) stack_vals
;
1942 GLenum gl_type
= GL_NONE
;
1944 gl_register_file file
;
1945 gl_program_parameter_list
*param_list
;
1946 static int in_array
= 0;
1948 file
= in_array
? PROGRAM_CONSTANT
: PROGRAM_IMMEDIATE
;
1949 param_list
= in_array
? this->prog
->Parameters
: this->immediates
;
1951 /* Unfortunately, 4 floats is all we can get into
1952 * _mesa_add_typed_unnamed_constant. So, make a temp to store an
1953 * aggregate constant and move each constant value into it. If we
1954 * get lucky, copy propagation will eliminate the extra moves.
1956 if (ir
->type
->base_type
== GLSL_TYPE_STRUCT
) {
1957 st_src_reg temp_base
= get_temp(ir
->type
);
1958 st_dst_reg temp
= st_dst_reg(temp_base
);
1960 foreach_iter(exec_list_iterator
, iter
, ir
->components
) {
1961 ir_constant
*field_value
= (ir_constant
*)iter
.get();
1962 int size
= type_size(field_value
->type
);
1966 field_value
->accept(this);
1969 for (i
= 0; i
< (unsigned int)size
; i
++) {
1970 emit(ir
, TGSI_OPCODE_MOV
, temp
, src
);
1976 this->result
= temp_base
;
1980 if (ir
->type
->is_array()) {
1981 st_src_reg temp_base
= get_temp(ir
->type
);
1982 st_dst_reg temp
= st_dst_reg(temp_base
);
1983 int size
= type_size(ir
->type
->fields
.array
);
1988 for (i
= 0; i
< ir
->type
->length
; i
++) {
1989 ir
->array_elements
[i
]->accept(this);
1991 for (int j
= 0; j
< size
; j
++) {
1992 emit(ir
, TGSI_OPCODE_MOV
, temp
, src
);
1998 this->result
= temp_base
;
2003 if (ir
->type
->is_matrix()) {
2004 st_src_reg mat
= get_temp(ir
->type
);
2005 st_dst_reg mat_column
= st_dst_reg(mat
);
2007 for (i
= 0; i
< ir
->type
->matrix_columns
; i
++) {
2008 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
2009 values
= (gl_constant_value
*) &ir
->value
.f
[i
* ir
->type
->vector_elements
];
2011 src
= st_src_reg(file
, -1, ir
->type
->base_type
);
2012 src
.index
= _mesa_add_typed_unnamed_constant(param_list
,
2014 ir
->type
->vector_elements
,
2017 emit(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
2026 switch (ir
->type
->base_type
) {
2027 case GLSL_TYPE_FLOAT
:
2029 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2030 values
[i
].f
= ir
->value
.f
[i
];
2033 case GLSL_TYPE_UINT
:
2034 gl_type
= glsl_version
>= 130 ? GL_UNSIGNED_INT
: GL_FLOAT
;
2035 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2036 if (glsl_version
>= 130)
2037 values
[i
].u
= ir
->value
.u
[i
];
2039 values
[i
].f
= ir
->value
.u
[i
];
2043 gl_type
= glsl_version
>= 130 ? GL_INT
: GL_FLOAT
;
2044 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2045 if (glsl_version
>= 130)
2046 values
[i
].i
= ir
->value
.i
[i
];
2048 values
[i
].f
= ir
->value
.i
[i
];
2051 case GLSL_TYPE_BOOL
:
2052 gl_type
= glsl_version
>= 130 ? GL_BOOL
: GL_FLOAT
;
2053 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2054 if (glsl_version
>= 130)
2055 values
[i
].b
= ir
->value
.b
[i
];
2057 values
[i
].f
= ir
->value
.b
[i
];
2061 assert(!"Non-float/uint/int/bool constant");
2064 this->result
= st_src_reg(file
, -1, ir
->type
);
2065 this->result
.index
= _mesa_add_typed_unnamed_constant(param_list
,
2066 values
, ir
->type
->vector_elements
, gl_type
,
2067 &this->result
.swizzle
);
2071 glsl_to_tgsi_visitor::get_function_signature(ir_function_signature
*sig
)
2073 function_entry
*entry
;
2075 foreach_iter(exec_list_iterator
, iter
, this->function_signatures
) {
2076 entry
= (function_entry
*)iter
.get();
2078 if (entry
->sig
== sig
)
2082 entry
= ralloc(mem_ctx
, function_entry
);
2084 entry
->sig_id
= this->next_signature_id
++;
2085 entry
->bgn_inst
= NULL
;
2087 /* Allocate storage for all the parameters. */
2088 foreach_iter(exec_list_iterator
, iter
, sig
->parameters
) {
2089 ir_variable
*param
= (ir_variable
*)iter
.get();
2090 variable_storage
*storage
;
2092 storage
= find_variable_storage(param
);
2095 storage
= new(mem_ctx
) variable_storage(param
, PROGRAM_TEMPORARY
,
2097 this->variables
.push_tail(storage
);
2099 this->next_temp
+= type_size(param
->type
);
2102 if (!sig
->return_type
->is_void()) {
2103 entry
->return_reg
= get_temp(sig
->return_type
);
2105 entry
->return_reg
= undef_src
;
2108 this->function_signatures
.push_tail(entry
);
2113 glsl_to_tgsi_visitor::visit(ir_call
*ir
)
2115 glsl_to_tgsi_instruction
*call_inst
;
2116 ir_function_signature
*sig
= ir
->get_callee();
2117 function_entry
*entry
= get_function_signature(sig
);
2120 /* Process in parameters. */
2121 exec_list_iterator sig_iter
= sig
->parameters
.iterator();
2122 foreach_iter(exec_list_iterator
, iter
, *ir
) {
2123 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
2124 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
2126 if (param
->mode
== ir_var_in
||
2127 param
->mode
== ir_var_inout
) {
2128 variable_storage
*storage
= find_variable_storage(param
);
2131 param_rval
->accept(this);
2132 st_src_reg r
= this->result
;
2135 l
.file
= storage
->file
;
2136 l
.index
= storage
->index
;
2138 l
.writemask
= WRITEMASK_XYZW
;
2139 l
.cond_mask
= COND_TR
;
2141 for (i
= 0; i
< type_size(param
->type
); i
++) {
2142 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
2150 assert(!sig_iter
.has_next());
2152 /* Emit call instruction */
2153 call_inst
= emit(ir
, TGSI_OPCODE_CAL
);
2154 call_inst
->function
= entry
;
2156 /* Process out parameters. */
2157 sig_iter
= sig
->parameters
.iterator();
2158 foreach_iter(exec_list_iterator
, iter
, *ir
) {
2159 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
2160 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
2162 if (param
->mode
== ir_var_out
||
2163 param
->mode
== ir_var_inout
) {
2164 variable_storage
*storage
= find_variable_storage(param
);
2168 r
.file
= storage
->file
;
2169 r
.index
= storage
->index
;
2171 r
.swizzle
= SWIZZLE_NOOP
;
2174 param_rval
->accept(this);
2175 st_dst_reg l
= st_dst_reg(this->result
);
2177 for (i
= 0; i
< type_size(param
->type
); i
++) {
2178 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
2186 assert(!sig_iter
.has_next());
2188 /* Process return value. */
2189 this->result
= entry
->return_reg
;
2193 glsl_to_tgsi_visitor::visit(ir_texture
*ir
)
2195 st_src_reg result_src
, coord
, lod_info
, projector
, dx
, dy
;
2196 st_dst_reg result_dst
, coord_dst
;
2197 glsl_to_tgsi_instruction
*inst
= NULL
;
2198 unsigned opcode
= TGSI_OPCODE_NOP
;
2200 ir
->coordinate
->accept(this);
2202 /* Put our coords in a temp. We'll need to modify them for shadow,
2203 * projection, or LOD, so the only case we'd use it as is is if
2204 * we're doing plain old texturing. The optimization passes on
2205 * glsl_to_tgsi_visitor should handle cleaning up our mess in that case.
2207 coord
= get_temp(glsl_type::vec4_type
);
2208 coord_dst
= st_dst_reg(coord
);
2209 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
2211 if (ir
->projector
) {
2212 ir
->projector
->accept(this);
2213 projector
= this->result
;
2216 /* Storage for our result. Ideally for an assignment we'd be using
2217 * the actual storage for the result here, instead.
2219 result_src
= get_temp(glsl_type::vec4_type
);
2220 result_dst
= st_dst_reg(result_src
);
2224 opcode
= TGSI_OPCODE_TEX
;
2227 opcode
= TGSI_OPCODE_TXB
;
2228 ir
->lod_info
.bias
->accept(this);
2229 lod_info
= this->result
;
2232 opcode
= TGSI_OPCODE_TXL
;
2233 ir
->lod_info
.lod
->accept(this);
2234 lod_info
= this->result
;
2237 opcode
= TGSI_OPCODE_TXD
;
2238 ir
->lod_info
.grad
.dPdx
->accept(this);
2240 ir
->lod_info
.grad
.dPdy
->accept(this);
2243 case ir_txf
: /* TODO: use TGSI_OPCODE_TXF here */
2244 assert(!"GLSL 1.30 features unsupported");
2248 if (ir
->projector
) {
2249 if (opcode
== TGSI_OPCODE_TEX
) {
2250 /* Slot the projector in as the last component of the coord. */
2251 coord_dst
.writemask
= WRITEMASK_W
;
2252 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, projector
);
2253 coord_dst
.writemask
= WRITEMASK_XYZW
;
2254 opcode
= TGSI_OPCODE_TXP
;
2256 st_src_reg coord_w
= coord
;
2257 coord_w
.swizzle
= SWIZZLE_WWWW
;
2259 /* For the other TEX opcodes there's no projective version
2260 * since the last slot is taken up by LOD info. Do the
2261 * projective divide now.
2263 coord_dst
.writemask
= WRITEMASK_W
;
2264 emit(ir
, TGSI_OPCODE_RCP
, coord_dst
, projector
);
2266 /* In the case where we have to project the coordinates "by hand,"
2267 * the shadow comparator value must also be projected.
2269 st_src_reg tmp_src
= coord
;
2270 if (ir
->shadow_comparitor
) {
2271 /* Slot the shadow value in as the second to last component of the
2274 ir
->shadow_comparitor
->accept(this);
2276 tmp_src
= get_temp(glsl_type::vec4_type
);
2277 st_dst_reg tmp_dst
= st_dst_reg(tmp_src
);
2279 tmp_dst
.writemask
= WRITEMASK_Z
;
2280 emit(ir
, TGSI_OPCODE_MOV
, tmp_dst
, this->result
);
2282 tmp_dst
.writemask
= WRITEMASK_XY
;
2283 emit(ir
, TGSI_OPCODE_MOV
, tmp_dst
, coord
);
2286 coord_dst
.writemask
= WRITEMASK_XYZ
;
2287 emit(ir
, TGSI_OPCODE_MUL
, coord_dst
, tmp_src
, coord_w
);
2289 coord_dst
.writemask
= WRITEMASK_XYZW
;
2290 coord
.swizzle
= SWIZZLE_XYZW
;
2294 /* If projection is done and the opcode is not TGSI_OPCODE_TXP, then the shadow
2295 * comparator was put in the correct place (and projected) by the code,
2296 * above, that handles by-hand projection.
2298 if (ir
->shadow_comparitor
&& (!ir
->projector
|| opcode
== TGSI_OPCODE_TXP
)) {
2299 /* Slot the shadow value in as the second to last component of the
2302 ir
->shadow_comparitor
->accept(this);
2303 coord_dst
.writemask
= WRITEMASK_Z
;
2304 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
2305 coord_dst
.writemask
= WRITEMASK_XYZW
;
2308 if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXB
) {
2309 /* TGSI stores LOD or LOD bias in the last channel of the coords. */
2310 coord_dst
.writemask
= WRITEMASK_W
;
2311 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, lod_info
);
2312 coord_dst
.writemask
= WRITEMASK_XYZW
;
2315 if (opcode
== TGSI_OPCODE_TXD
)
2316 inst
= emit(ir
, opcode
, result_dst
, coord
, dx
, dy
);
2318 inst
= emit(ir
, opcode
, result_dst
, coord
);
2320 if (ir
->shadow_comparitor
)
2321 inst
->tex_shadow
= GL_TRUE
;
2323 inst
->sampler
= _mesa_get_sampler_uniform_value(ir
->sampler
,
2324 this->shader_program
,
2327 const glsl_type
*sampler_type
= ir
->sampler
->type
;
2329 switch (sampler_type
->sampler_dimensionality
) {
2330 case GLSL_SAMPLER_DIM_1D
:
2331 inst
->tex_target
= (sampler_type
->sampler_array
)
2332 ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
2334 case GLSL_SAMPLER_DIM_2D
:
2335 inst
->tex_target
= (sampler_type
->sampler_array
)
2336 ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
2338 case GLSL_SAMPLER_DIM_3D
:
2339 inst
->tex_target
= TEXTURE_3D_INDEX
;
2341 case GLSL_SAMPLER_DIM_CUBE
:
2342 inst
->tex_target
= TEXTURE_CUBE_INDEX
;
2344 case GLSL_SAMPLER_DIM_RECT
:
2345 inst
->tex_target
= TEXTURE_RECT_INDEX
;
2347 case GLSL_SAMPLER_DIM_BUF
:
2348 assert(!"FINISHME: Implement ARB_texture_buffer_object");
2351 assert(!"Should not get here.");
2354 this->result
= result_src
;
2358 glsl_to_tgsi_visitor::visit(ir_return
*ir
)
2360 if (ir
->get_value()) {
2364 assert(current_function
);
2366 ir
->get_value()->accept(this);
2367 st_src_reg r
= this->result
;
2369 l
= st_dst_reg(current_function
->return_reg
);
2371 for (i
= 0; i
< type_size(current_function
->sig
->return_type
); i
++) {
2372 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
2378 emit(ir
, TGSI_OPCODE_RET
);
2382 glsl_to_tgsi_visitor::visit(ir_discard
*ir
)
2384 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
2386 if (ir
->condition
) {
2387 ir
->condition
->accept(this);
2388 this->result
.negate
= ~this->result
.negate
;
2389 emit(ir
, TGSI_OPCODE_KIL
, undef_dst
, this->result
);
2391 emit(ir
, TGSI_OPCODE_KILP
);
2394 fp
->UsesKill
= GL_TRUE
;
2398 glsl_to_tgsi_visitor::visit(ir_if
*ir
)
2400 glsl_to_tgsi_instruction
*cond_inst
, *if_inst
, *else_inst
= NULL
;
2401 glsl_to_tgsi_instruction
*prev_inst
;
2403 prev_inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
2405 ir
->condition
->accept(this);
2406 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
2408 if (this->options
->EmitCondCodes
) {
2409 cond_inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
2411 /* See if we actually generated any instruction for generating
2412 * the condition. If not, then cook up a move to a temp so we
2413 * have something to set cond_update on.
2415 if (cond_inst
== prev_inst
) {
2416 st_src_reg temp
= get_temp(glsl_type::bool_type
);
2417 cond_inst
= emit(ir
->condition
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), result
);
2419 cond_inst
->cond_update
= GL_TRUE
;
2421 if_inst
= emit(ir
->condition
, TGSI_OPCODE_IF
);
2422 if_inst
->dst
.cond_mask
= COND_NE
;
2424 if_inst
= emit(ir
->condition
, TGSI_OPCODE_IF
, undef_dst
, this->result
);
2427 this->instructions
.push_tail(if_inst
);
2429 visit_exec_list(&ir
->then_instructions
, this);
2431 if (!ir
->else_instructions
.is_empty()) {
2432 else_inst
= emit(ir
->condition
, TGSI_OPCODE_ELSE
);
2433 visit_exec_list(&ir
->else_instructions
, this);
2436 if_inst
= emit(ir
->condition
, TGSI_OPCODE_ENDIF
);
2439 glsl_to_tgsi_visitor::glsl_to_tgsi_visitor()
2441 result
.file
= PROGRAM_UNDEFINED
;
2443 next_signature_id
= 1;
2444 current_function
= NULL
;
2445 num_address_regs
= 0;
2446 indirect_addr_temps
= false;
2447 indirect_addr_consts
= false;
2448 immediates
= _mesa_new_parameter_list();
2449 mem_ctx
= ralloc_context(NULL
);
2452 glsl_to_tgsi_visitor::~glsl_to_tgsi_visitor()
2454 _mesa_free_parameter_list(immediates
);
2455 ralloc_free(mem_ctx
);
2458 extern "C" void free_glsl_to_tgsi_visitor(glsl_to_tgsi_visitor
*v
)
2465 * Count resources used by the given gpu program (number of texture
2469 count_resources(glsl_to_tgsi_visitor
*v
, gl_program
*prog
)
2471 v
->samplers_used
= 0;
2473 foreach_iter(exec_list_iterator
, iter
, v
->instructions
) {
2474 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
2476 if (is_tex_instruction(inst
->op
)) {
2477 v
->samplers_used
|= 1 << inst
->sampler
;
2479 prog
->SamplerTargets
[inst
->sampler
] =
2480 (gl_texture_index
)inst
->tex_target
;
2481 if (inst
->tex_shadow
) {
2482 prog
->ShadowSamplers
|= 1 << inst
->sampler
;
2487 prog
->SamplersUsed
= v
->samplers_used
;
2488 _mesa_update_shader_textures_used(prog
);
2493 * Check if the given vertex/fragment/shader program is within the
2494 * resource limits of the context (number of texture units, etc).
2495 * If any of those checks fail, record a linker error.
2497 * XXX more checks are needed...
2500 check_resources(const struct gl_context
*ctx
,
2501 struct gl_shader_program
*shader_program
,
2502 glsl_to_tgsi_visitor
*prog
,
2503 struct gl_program
*proginfo
)
2505 switch (proginfo
->Target
) {
2506 case GL_VERTEX_PROGRAM_ARB
:
2507 if (_mesa_bitcount(prog
->samplers_used
) >
2508 ctx
->Const
.MaxVertexTextureImageUnits
) {
2509 fail_link(shader_program
, "Too many vertex shader texture samplers");
2511 if (proginfo
->Parameters
->NumParameters
> MAX_UNIFORMS
) {
2512 fail_link(shader_program
, "Too many vertex shader constants");
2515 case MESA_GEOMETRY_PROGRAM
:
2516 if (_mesa_bitcount(prog
->samplers_used
) >
2517 ctx
->Const
.MaxGeometryTextureImageUnits
) {
2518 fail_link(shader_program
, "Too many geometry shader texture samplers");
2520 if (proginfo
->Parameters
->NumParameters
>
2521 MAX_GEOMETRY_UNIFORM_COMPONENTS
/ 4) {
2522 fail_link(shader_program
, "Too many geometry shader constants");
2525 case GL_FRAGMENT_PROGRAM_ARB
:
2526 if (_mesa_bitcount(prog
->samplers_used
) >
2527 ctx
->Const
.MaxTextureImageUnits
) {
2528 fail_link(shader_program
, "Too many fragment shader texture samplers");
2530 if (proginfo
->Parameters
->NumParameters
> MAX_UNIFORMS
) {
2531 fail_link(shader_program
, "Too many fragment shader constants");
2535 _mesa_problem(ctx
, "unexpected program type in check_resources()");
2541 struct uniform_sort
{
2542 struct gl_uniform
*u
;
2546 /* The shader_program->Uniforms list is almost sorted in increasing
2547 * uniform->{Frag,Vert}Pos locations, but not quite when there are
2548 * uniforms shared between targets. We need to add parameters in
2549 * increasing order for the targets.
2552 sort_uniforms(const void *a
, const void *b
)
2554 struct uniform_sort
*u1
= (struct uniform_sort
*)a
;
2555 struct uniform_sort
*u2
= (struct uniform_sort
*)b
;
2557 return u1
->pos
- u2
->pos
;
2560 /* Add the uniforms to the parameters. The linker chose locations
2561 * in our parameters lists (which weren't created yet), which the
2562 * uniforms code will use to poke values into our parameters list
2563 * when uniforms are updated.
2566 add_uniforms_to_parameters_list(struct gl_shader_program
*shader_program
,
2567 struct gl_shader
*shader
,
2568 struct gl_program
*prog
)
2571 unsigned int next_sampler
= 0, num_uniforms
= 0;
2572 struct uniform_sort
*sorted_uniforms
;
2574 sorted_uniforms
= ralloc_array(NULL
, struct uniform_sort
,
2575 shader_program
->Uniforms
->NumUniforms
);
2577 for (i
= 0; i
< shader_program
->Uniforms
->NumUniforms
; i
++) {
2578 struct gl_uniform
*uniform
= shader_program
->Uniforms
->Uniforms
+ i
;
2579 int parameter_index
= -1;
2581 switch (shader
->Type
) {
2582 case GL_VERTEX_SHADER
:
2583 parameter_index
= uniform
->VertPos
;
2585 case GL_FRAGMENT_SHADER
:
2586 parameter_index
= uniform
->FragPos
;
2588 case GL_GEOMETRY_SHADER
:
2589 parameter_index
= uniform
->GeomPos
;
2593 /* Only add uniforms used in our target. */
2594 if (parameter_index
!= -1) {
2595 sorted_uniforms
[num_uniforms
].pos
= parameter_index
;
2596 sorted_uniforms
[num_uniforms
].u
= uniform
;
2601 qsort(sorted_uniforms
, num_uniforms
, sizeof(struct uniform_sort
),
2604 for (i
= 0; i
< num_uniforms
; i
++) {
2605 struct gl_uniform
*uniform
= sorted_uniforms
[i
].u
;
2606 int parameter_index
= sorted_uniforms
[i
].pos
;
2607 const glsl_type
*type
= uniform
->Type
;
2610 if (type
->is_vector() ||
2611 type
->is_scalar()) {
2612 size
= type
->vector_elements
;
2614 size
= type_size(type
) * 4;
2617 gl_register_file file
;
2618 if (type
->is_sampler() ||
2619 (type
->is_array() && type
->fields
.array
->is_sampler())) {
2620 file
= PROGRAM_SAMPLER
;
2622 file
= PROGRAM_UNIFORM
;
2625 GLint index
= _mesa_lookup_parameter_index(prog
->Parameters
, -1,
2629 index
= _mesa_add_parameter(prog
->Parameters
, file
,
2630 uniform
->Name
, size
, type
->gl_type
,
2633 /* Sampler uniform values are stored in prog->SamplerUnits,
2634 * and the entry in that array is selected by this index we
2635 * store in ParameterValues[].
2637 if (file
== PROGRAM_SAMPLER
) {
2638 for (unsigned int j
= 0; j
< size
/ 4; j
++)
2639 prog
->Parameters
->ParameterValues
[index
+ j
][0].f
= next_sampler
++;
2642 /* The location chosen in the Parameters list here (returned
2643 * from _mesa_add_uniform) has to match what the linker chose.
2645 if (index
!= parameter_index
) {
2646 fail_link(shader_program
, "Allocation of uniform `%s' to target "
2647 "failed (%d vs %d)\n",
2648 uniform
->Name
, index
, parameter_index
);
2653 ralloc_free(sorted_uniforms
);
2657 set_uniform_initializer(struct gl_context
*ctx
, void *mem_ctx
,
2658 struct gl_shader_program
*shader_program
,
2659 const char *name
, const glsl_type
*type
,
2662 if (type
->is_record()) {
2663 ir_constant
*field_constant
;
2665 field_constant
= (ir_constant
*)val
->components
.get_head();
2667 for (unsigned int i
= 0; i
< type
->length
; i
++) {
2668 const glsl_type
*field_type
= type
->fields
.structure
[i
].type
;
2669 const char *field_name
= ralloc_asprintf(mem_ctx
, "%s.%s", name
,
2670 type
->fields
.structure
[i
].name
);
2671 set_uniform_initializer(ctx
, mem_ctx
, shader_program
, field_name
,
2672 field_type
, field_constant
);
2673 field_constant
= (ir_constant
*)field_constant
->next
;
2678 int loc
= _mesa_get_uniform_location(ctx
, shader_program
, name
);
2681 fail_link(shader_program
,
2682 "Couldn't find uniform for initializer %s\n", name
);
2686 for (unsigned int i
= 0; i
< (type
->is_array() ? type
->length
: 1); i
++) {
2687 ir_constant
*element
;
2688 const glsl_type
*element_type
;
2689 if (type
->is_array()) {
2690 element
= val
->array_elements
[i
];
2691 element_type
= type
->fields
.array
;
2694 element_type
= type
;
2699 if (element_type
->base_type
== GLSL_TYPE_BOOL
) {
2700 int *conv
= ralloc_array(mem_ctx
, int, element_type
->components());
2701 for (unsigned int j
= 0; j
< element_type
->components(); j
++) {
2702 conv
[j
] = element
->value
.b
[j
];
2704 values
= (void *)conv
;
2705 element_type
= glsl_type::get_instance(GLSL_TYPE_INT
,
2706 element_type
->vector_elements
,
2709 values
= &element
->value
;
2712 if (element_type
->is_matrix()) {
2713 _mesa_uniform_matrix(ctx
, shader_program
,
2714 element_type
->matrix_columns
,
2715 element_type
->vector_elements
,
2716 loc
, 1, GL_FALSE
, (GLfloat
*)values
);
2717 loc
+= element_type
->matrix_columns
;
2719 _mesa_uniform(ctx
, shader_program
, loc
, element_type
->matrix_columns
,
2720 values
, element_type
->gl_type
);
2721 loc
+= type_size(element_type
);
2727 set_uniform_initializers(struct gl_context
*ctx
,
2728 struct gl_shader_program
*shader_program
)
2730 void *mem_ctx
= NULL
;
2732 for (unsigned int i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
2733 struct gl_shader
*shader
= shader_program
->_LinkedShaders
[i
];
2738 foreach_iter(exec_list_iterator
, iter
, *shader
->ir
) {
2739 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
2740 ir_variable
*var
= ir
->as_variable();
2742 if (!var
|| var
->mode
!= ir_var_uniform
|| !var
->constant_value
)
2746 mem_ctx
= ralloc_context(NULL
);
2748 set_uniform_initializer(ctx
, mem_ctx
, shader_program
, var
->name
,
2749 var
->type
, var
->constant_value
);
2753 ralloc_free(mem_ctx
);
2757 * Scan/rewrite program to remove reads of custom (output) registers.
2758 * The passed type has to be either PROGRAM_OUTPUT or PROGRAM_VARYING
2759 * (for vertex shaders).
2760 * In GLSL shaders, varying vars can be read and written.
2761 * On some hardware, trying to read an output register causes trouble.
2762 * So, rewrite the program to use a temporary register in this case.
2764 * Based on _mesa_remove_output_reads from programopt.c.
2767 glsl_to_tgsi_visitor::remove_output_reads(gl_register_file type
)
2770 GLint outputMap
[VERT_RESULT_MAX
];
2771 GLint outputTypes
[VERT_RESULT_MAX
];
2772 GLuint numVaryingReads
= 0;
2773 GLboolean usedTemps
[MAX_TEMPS
];
2774 GLuint firstTemp
= 0;
2776 _mesa_find_used_registers(prog
, PROGRAM_TEMPORARY
,
2777 usedTemps
, MAX_TEMPS
);
2779 assert(type
== PROGRAM_VARYING
|| type
== PROGRAM_OUTPUT
);
2780 assert(prog
->Target
== GL_VERTEX_PROGRAM_ARB
|| type
!= PROGRAM_VARYING
);
2782 for (i
= 0; i
< VERT_RESULT_MAX
; i
++)
2785 /* look for instructions which read from varying vars */
2786 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2787 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
2788 const GLuint numSrc
= num_inst_src_regs(inst
->op
);
2790 for (j
= 0; j
< numSrc
; j
++) {
2791 if (inst
->src
[j
].file
== type
) {
2792 /* replace the read with a temp reg */
2793 const GLuint var
= inst
->src
[j
].index
;
2794 if (outputMap
[var
] == -1) {
2796 outputMap
[var
] = _mesa_find_free_register(usedTemps
,
2799 outputTypes
[var
] = inst
->src
[j
].type
;
2800 firstTemp
= outputMap
[var
] + 1;
2802 inst
->src
[j
].file
= PROGRAM_TEMPORARY
;
2803 inst
->src
[j
].index
= outputMap
[var
];
2808 if (numVaryingReads
== 0)
2809 return; /* nothing to be done */
2811 /* look for instructions which write to the varying vars identified above */
2812 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2813 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
2814 if (inst
->dst
.file
== type
&& outputMap
[inst
->dst
.index
] >= 0) {
2815 /* change inst to write to the temp reg, instead of the varying */
2816 inst
->dst
.file
= PROGRAM_TEMPORARY
;
2817 inst
->dst
.index
= outputMap
[inst
->dst
.index
];
2821 /* insert new MOV instructions at the end */
2822 for (i
= 0; i
< VERT_RESULT_MAX
; i
++) {
2823 if (outputMap
[i
] >= 0) {
2824 /* MOV VAR[i], TEMP[tmp]; */
2825 st_src_reg src
= st_src_reg(PROGRAM_TEMPORARY
, outputMap
[i
], outputTypes
[i
]);
2826 st_dst_reg dst
= st_dst_reg(type
, WRITEMASK_XYZW
, outputTypes
[i
]);
2828 this->emit(NULL
, TGSI_OPCODE_MOV
, dst
, src
);
2834 * Returns the mask of channels (bitmask of WRITEMASK_X,Y,Z,W) which
2835 * are read from the given src in this instruction
2838 get_src_arg_mask(st_dst_reg dst
, st_src_reg src
)
2840 int read_mask
= 0, comp
;
2842 /* Now, given the src swizzle and the written channels, find which
2843 * components are actually read
2845 for (comp
= 0; comp
< 4; ++comp
) {
2846 const unsigned coord
= GET_SWZ(src
.swizzle
, comp
);
2848 if (dst
.writemask
& (1 << comp
) && coord
<= SWIZZLE_W
)
2849 read_mask
|= 1 << coord
;
2856 * This pass replaces CMP T0, T1 T2 T0 with MOV T0, T2 when the CMP
2857 * instruction is the first instruction to write to register T0. There are
2858 * several lowering passes done in GLSL IR (e.g. branches and
2859 * relative addressing) that create a large number of conditional assignments
2860 * that ir_to_mesa converts to CMP instructions like the one mentioned above.
2862 * Here is why this conversion is safe:
2863 * CMP T0, T1 T2 T0 can be expanded to:
2869 * If (T1 < 0.0) evaluates to true then our replacement MOV T0, T2 is the same
2870 * as the original program. If (T1 < 0.0) evaluates to false, executing
2871 * MOV T0, T0 will store a garbage value in T0 since T0 is uninitialized.
2872 * Therefore, it doesn't matter that we are replacing MOV T0, T0 with MOV T0, T2
2873 * because any instruction that was going to read from T0 after this was going
2874 * to read a garbage value anyway.
2877 glsl_to_tgsi_visitor::simplify_cmp(void)
2879 unsigned tempWrites
[MAX_TEMPS
];
2880 unsigned outputWrites
[MAX_PROGRAM_OUTPUTS
];
2882 memset(tempWrites
, 0, sizeof(tempWrites
));
2883 memset(outputWrites
, 0, sizeof(outputWrites
));
2885 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2886 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
2887 unsigned prevWriteMask
= 0;
2889 /* Give up if we encounter relative addressing or flow control. */
2890 if (inst
->dst
.reladdr
||
2891 tgsi_get_opcode_info(inst
->op
)->is_branch
||
2892 inst
->op
== TGSI_OPCODE_BGNSUB
||
2893 inst
->op
== TGSI_OPCODE_CONT
||
2894 inst
->op
== TGSI_OPCODE_END
||
2895 inst
->op
== TGSI_OPCODE_ENDSUB
||
2896 inst
->op
== TGSI_OPCODE_RET
) {
2900 if (inst
->dst
.file
== PROGRAM_OUTPUT
) {
2901 assert(inst
->dst
.index
< MAX_PROGRAM_OUTPUTS
);
2902 prevWriteMask
= outputWrites
[inst
->dst
.index
];
2903 outputWrites
[inst
->dst
.index
] |= inst
->dst
.writemask
;
2904 } else if (inst
->dst
.file
== PROGRAM_TEMPORARY
) {
2905 assert(inst
->dst
.index
< MAX_TEMPS
);
2906 prevWriteMask
= tempWrites
[inst
->dst
.index
];
2907 tempWrites
[inst
->dst
.index
] |= inst
->dst
.writemask
;
2910 /* For a CMP to be considered a conditional write, the destination
2911 * register and source register two must be the same. */
2912 if (inst
->op
== TGSI_OPCODE_CMP
2913 && !(inst
->dst
.writemask
& prevWriteMask
)
2914 && inst
->src
[2].file
== inst
->dst
.file
2915 && inst
->src
[2].index
== inst
->dst
.index
2916 && inst
->dst
.writemask
== get_src_arg_mask(inst
->dst
, inst
->src
[2])) {
2918 inst
->op
= TGSI_OPCODE_MOV
;
2919 inst
->src
[0] = inst
->src
[1];
2924 /* Replaces all references to a temporary register index with another index. */
2926 glsl_to_tgsi_visitor::rename_temp_register(int index
, int new_index
)
2928 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2929 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
2932 for (j
=0; j
< num_inst_src_regs(inst
->op
); j
++) {
2933 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
&&
2934 inst
->src
[j
].index
== index
) {
2935 inst
->src
[j
].index
= new_index
;
2939 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== index
) {
2940 inst
->dst
.index
= new_index
;
2946 glsl_to_tgsi_visitor::get_first_temp_read(int index
)
2948 int depth
= 0; /* loop depth */
2949 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
2952 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2953 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
2955 for (j
=0; j
< num_inst_src_regs(inst
->op
); j
++) {
2956 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
&&
2957 inst
->src
[j
].index
== index
) {
2958 return (depth
== 0) ? i
: loop_start
;
2962 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
2965 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
2978 glsl_to_tgsi_visitor::get_first_temp_write(int index
)
2980 int depth
= 0; /* loop depth */
2981 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
2984 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2985 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
2987 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== index
) {
2988 return (depth
== 0) ? i
: loop_start
;
2991 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
2994 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
3007 glsl_to_tgsi_visitor::get_last_temp_read(int index
)
3009 int depth
= 0; /* loop depth */
3010 int last
= -1; /* index of last instruction that reads the temporary */
3013 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3014 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3016 for (j
=0; j
< num_inst_src_regs(inst
->op
); j
++) {
3017 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
&&
3018 inst
->src
[j
].index
== index
) {
3019 last
= (depth
== 0) ? i
: -2;
3023 if (inst
->op
== TGSI_OPCODE_BGNLOOP
)
3025 else if (inst
->op
== TGSI_OPCODE_ENDLOOP
)
3026 if (--depth
== 0 && last
== -2)
3038 glsl_to_tgsi_visitor::get_last_temp_write(int index
)
3040 int depth
= 0; /* loop depth */
3041 int last
= -1; /* index of last instruction that writes to the temporary */
3044 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3045 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3047 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== index
)
3048 last
= (depth
== 0) ? i
: -2;
3050 if (inst
->op
== TGSI_OPCODE_BGNLOOP
)
3052 else if (inst
->op
== TGSI_OPCODE_ENDLOOP
)
3053 if (--depth
== 0 && last
== -2)
3065 * On a basic block basis, tracks available PROGRAM_TEMPORARY register
3066 * channels for copy propagation and updates following instructions to
3067 * use the original versions.
3069 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
3070 * will occur. As an example, a TXP production before this pass:
3072 * 0: MOV TEMP[1], INPUT[4].xyyy;
3073 * 1: MOV TEMP[1].w, INPUT[4].wwww;
3074 * 2: TXP TEMP[2], TEMP[1], texture[0], 2D;
3078 * 0: MOV TEMP[1], INPUT[4].xyyy;
3079 * 1: MOV TEMP[1].w, INPUT[4].wwww;
3080 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
3082 * which allows for dead code elimination on TEMP[1]'s writes.
3085 glsl_to_tgsi_visitor::copy_propagate(void)
3087 glsl_to_tgsi_instruction
**acp
= rzalloc_array(mem_ctx
,
3088 glsl_to_tgsi_instruction
*,
3089 this->next_temp
* 4);
3090 int *acp_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
3093 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3094 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3096 assert(inst
->dst
.file
!= PROGRAM_TEMPORARY
3097 || inst
->dst
.index
< this->next_temp
);
3099 /* First, do any copy propagation possible into the src regs. */
3100 for (int r
= 0; r
< 3; r
++) {
3101 glsl_to_tgsi_instruction
*first
= NULL
;
3103 int acp_base
= inst
->src
[r
].index
* 4;
3105 if (inst
->src
[r
].file
!= PROGRAM_TEMPORARY
||
3106 inst
->src
[r
].reladdr
)
3109 /* See if we can find entries in the ACP consisting of MOVs
3110 * from the same src register for all the swizzled channels
3111 * of this src register reference.
3113 for (int i
= 0; i
< 4; i
++) {
3114 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
3115 glsl_to_tgsi_instruction
*copy_chan
= acp
[acp_base
+ src_chan
];
3122 assert(acp_level
[acp_base
+ src_chan
] <= level
);
3127 if (first
->src
[0].file
!= copy_chan
->src
[0].file
||
3128 first
->src
[0].index
!= copy_chan
->src
[0].index
) {
3136 /* We've now validated that we can copy-propagate to
3137 * replace this src register reference. Do it.
3139 inst
->src
[r
].file
= first
->src
[0].file
;
3140 inst
->src
[r
].index
= first
->src
[0].index
;
3143 for (int i
= 0; i
< 4; i
++) {
3144 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
3145 glsl_to_tgsi_instruction
*copy_inst
= acp
[acp_base
+ src_chan
];
3146 swizzle
|= (GET_SWZ(copy_inst
->src
[0].swizzle
, src_chan
) <<
3149 inst
->src
[r
].swizzle
= swizzle
;
3154 case TGSI_OPCODE_BGNLOOP
:
3155 case TGSI_OPCODE_ENDLOOP
:
3156 /* End of a basic block, clear the ACP entirely. */
3157 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
3160 case TGSI_OPCODE_IF
:
3164 case TGSI_OPCODE_ENDIF
:
3165 case TGSI_OPCODE_ELSE
:
3166 /* Clear all channels written inside the block from the ACP, but
3167 * leaving those that were not touched.
3169 for (int r
= 0; r
< this->next_temp
; r
++) {
3170 for (int c
= 0; c
< 4; c
++) {
3171 if (!acp
[4 * r
+ c
])
3174 if (acp_level
[4 * r
+ c
] >= level
)
3175 acp
[4 * r
+ c
] = NULL
;
3178 if (inst
->op
== TGSI_OPCODE_ENDIF
)
3183 /* Continuing the block, clear any written channels from
3186 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.reladdr
) {
3187 /* Any temporary might be written, so no copy propagation
3188 * across this instruction.
3190 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
3191 } else if (inst
->dst
.file
== PROGRAM_OUTPUT
&&
3192 inst
->dst
.reladdr
) {
3193 /* Any output might be written, so no copy propagation
3194 * from outputs across this instruction.
3196 for (int r
= 0; r
< this->next_temp
; r
++) {
3197 for (int c
= 0; c
< 4; c
++) {
3198 if (!acp
[4 * r
+ c
])
3201 if (acp
[4 * r
+ c
]->src
[0].file
== PROGRAM_OUTPUT
)
3202 acp
[4 * r
+ c
] = NULL
;
3205 } else if (inst
->dst
.file
== PROGRAM_TEMPORARY
||
3206 inst
->dst
.file
== PROGRAM_OUTPUT
) {
3207 /* Clear where it's used as dst. */
3208 if (inst
->dst
.file
== PROGRAM_TEMPORARY
) {
3209 for (int c
= 0; c
< 4; c
++) {
3210 if (inst
->dst
.writemask
& (1 << c
)) {
3211 acp
[4 * inst
->dst
.index
+ c
] = NULL
;
3216 /* Clear where it's used as src. */
3217 for (int r
= 0; r
< this->next_temp
; r
++) {
3218 for (int c
= 0; c
< 4; c
++) {
3219 if (!acp
[4 * r
+ c
])
3222 int src_chan
= GET_SWZ(acp
[4 * r
+ c
]->src
[0].swizzle
, c
);
3224 if (acp
[4 * r
+ c
]->src
[0].file
== inst
->dst
.file
&&
3225 acp
[4 * r
+ c
]->src
[0].index
== inst
->dst
.index
&&
3226 inst
->dst
.writemask
& (1 << src_chan
))
3228 acp
[4 * r
+ c
] = NULL
;
3236 /* If this is a copy, add it to the ACP. */
3237 if (inst
->op
== TGSI_OPCODE_MOV
&&
3238 inst
->dst
.file
== PROGRAM_TEMPORARY
&&
3239 !inst
->dst
.reladdr
&&
3241 !inst
->src
[0].reladdr
&&
3242 !inst
->src
[0].negate
) {
3243 for (int i
= 0; i
< 4; i
++) {
3244 if (inst
->dst
.writemask
& (1 << i
)) {
3245 acp
[4 * inst
->dst
.index
+ i
] = inst
;
3246 acp_level
[4 * inst
->dst
.index
+ i
] = level
;
3252 ralloc_free(acp_level
);
3257 * Tracks available PROGRAM_TEMPORARY registers for dead code elimination.
3259 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
3260 * will occur. As an example, a TXP production after copy propagation but
3263 * 0: MOV TEMP[1], INPUT[4].xyyy;
3264 * 1: MOV TEMP[1].w, INPUT[4].wwww;
3265 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
3267 * and after this pass:
3269 * 0: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
3271 * FIXME: assumes that all functions are inlined (no support for BGNSUB/ENDSUB)
3272 * FIXME: doesn't eliminate all dead code inside of loops; it steps around them
3275 glsl_to_tgsi_visitor::eliminate_dead_code(void)
3279 for (i
=0; i
< this->next_temp
; i
++) {
3280 int last_read
= get_last_temp_read(i
);
3283 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3284 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3286 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== i
&&
3299 * On a basic block basis, tracks available PROGRAM_TEMPORARY registers for dead
3300 * code elimination. This is less primitive than eliminate_dead_code(), as it
3301 * is per-channel and can detect consecutive writes without a read between them
3302 * as dead code. However, there is some dead code that can be eliminated by
3303 * eliminate_dead_code() but not this function - for example, this function
3304 * cannot eliminate an instruction writing to a register that is never read and
3305 * is the only instruction writing to that register.
3307 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
3311 glsl_to_tgsi_visitor::eliminate_dead_code_advanced(void)
3313 glsl_to_tgsi_instruction
**writes
= rzalloc_array(mem_ctx
,
3314 glsl_to_tgsi_instruction
*,
3315 this->next_temp
* 4);
3316 int *write_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
3320 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3321 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3323 assert(inst
->dst
.file
!= PROGRAM_TEMPORARY
3324 || inst
->dst
.index
< this->next_temp
);
3327 case TGSI_OPCODE_BGNLOOP
:
3328 case TGSI_OPCODE_ENDLOOP
:
3329 /* End of a basic block, clear the write array entirely.
3330 * FIXME: This keeps us from killing dead code when the writes are
3331 * on either side of a loop, even when the register isn't touched
3334 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
3337 case TGSI_OPCODE_ENDIF
:
3341 case TGSI_OPCODE_ELSE
:
3342 /* Clear all channels written inside the preceding if block from the
3343 * write array, but leave those that were not touched.
3345 * FIXME: This destroys opportunities to remove dead code inside of
3346 * IF blocks that are followed by an ELSE block.
3348 for (int r
= 0; r
< this->next_temp
; r
++) {
3349 for (int c
= 0; c
< 4; c
++) {
3350 if (!writes
[4 * r
+ c
])
3353 if (write_level
[4 * r
+ c
] >= level
)
3354 writes
[4 * r
+ c
] = NULL
;
3359 case TGSI_OPCODE_IF
:
3361 /* fallthrough to default case to mark the condition as read */
3364 /* Continuing the block, clear any channels from the write array that
3365 * are read by this instruction.
3367 for (int i
= 0; i
< 4; i
++) {
3368 if (inst
->src
[i
].file
== PROGRAM_TEMPORARY
&& inst
->src
[i
].reladdr
){
3369 /* Any temporary might be read, so no dead code elimination
3370 * across this instruction.
3372 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
3373 } else if (inst
->src
[i
].file
== PROGRAM_TEMPORARY
) {
3374 /* Clear where it's used as src. */
3375 int src_chans
= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 0);
3376 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 1);
3377 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 2);
3378 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 3);
3380 for (int c
= 0; c
< 4; c
++) {
3381 if (src_chans
& (1 << c
)) {
3382 writes
[4 * inst
->src
[i
].index
+ c
] = NULL
;
3390 /* If this instruction writes to a temporary, add it to the write array.
3391 * If there is already an instruction in the write array for one or more
3392 * of the channels, flag that channel write as dead.
3394 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&&
3395 !inst
->dst
.reladdr
&&
3397 for (int c
= 0; c
< 4; c
++) {
3398 if (inst
->dst
.writemask
& (1 << c
)) {
3399 if (writes
[4 * inst
->dst
.index
+ c
]) {
3400 if (write_level
[4 * inst
->dst
.index
+ c
] < level
)
3403 writes
[4 * inst
->dst
.index
+ c
]->dead_mask
|= (1 << c
);
3405 writes
[4 * inst
->dst
.index
+ c
] = inst
;
3406 write_level
[4 * inst
->dst
.index
+ c
] = level
;
3412 /* Anything still in the write array at this point is dead code. */
3413 for (int r
= 0; r
< this->next_temp
; r
++) {
3414 for (int c
= 0; c
< 4; c
++) {
3415 glsl_to_tgsi_instruction
*inst
= writes
[4 * r
+ c
];
3417 inst
->dead_mask
|= (1 << c
);
3421 /* Now actually remove the instructions that are completely dead and update
3422 * the writemask of other instructions with dead channels.
3424 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3425 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3427 if (!inst
->dead_mask
|| !inst
->dst
.writemask
)
3429 else if (inst
->dead_mask
== inst
->dst
.writemask
) {
3434 inst
->dst
.writemask
&= ~(inst
->dead_mask
);
3437 ralloc_free(write_level
);
3438 ralloc_free(writes
);
3443 /* Merges temporary registers together where possible to reduce the number of
3444 * registers needed to run a program.
3446 * Produces optimal code only after copy propagation and dead code elimination
3449 glsl_to_tgsi_visitor::merge_registers(void)
3451 int *last_reads
= rzalloc_array(mem_ctx
, int, this->next_temp
);
3452 int *first_writes
= rzalloc_array(mem_ctx
, int, this->next_temp
);
3455 /* Read the indices of the last read and first write to each temp register
3456 * into an array so that we don't have to traverse the instruction list as
3458 for (i
=0; i
< this->next_temp
; i
++) {
3459 last_reads
[i
] = get_last_temp_read(i
);
3460 first_writes
[i
] = get_first_temp_write(i
);
3463 /* Start looking for registers with non-overlapping usages that can be
3464 * merged together. */
3465 for (i
=0; i
< this->next_temp
; i
++) {
3466 /* Don't touch unused registers. */
3467 if (last_reads
[i
] < 0 || first_writes
[i
] < 0) continue;
3469 for (j
=0; j
< this->next_temp
; j
++) {
3470 /* Don't touch unused registers. */
3471 if (last_reads
[j
] < 0 || first_writes
[j
] < 0) continue;
3473 /* We can merge the two registers if the first write to j is after or
3474 * in the same instruction as the last read from i. Note that the
3475 * register at index i will always be used earlier or at the same time
3476 * as the register at index j. */
3477 if (first_writes
[i
] <= first_writes
[j
] &&
3478 last_reads
[i
] <= first_writes
[j
])
3480 rename_temp_register(j
, i
); /* Replace all references to j with i.*/
3482 /* Update the first_writes and last_reads arrays with the new
3483 * values for the merged register index, and mark the newly unused
3484 * register index as such. */
3485 last_reads
[i
] = last_reads
[j
];
3486 first_writes
[j
] = -1;
3492 ralloc_free(last_reads
);
3493 ralloc_free(first_writes
);
3496 /* Reassign indices to temporary registers by reusing unused indices created
3497 * by optimization passes. */
3499 glsl_to_tgsi_visitor::renumber_registers(void)
3504 for (i
=0; i
< this->next_temp
; i
++) {
3505 if (get_first_temp_read(i
) < 0) continue;
3507 rename_temp_register(i
, new_index
);
3511 this->next_temp
= new_index
;
3515 * Returns a fragment program which implements the current pixel transfer ops.
3516 * Based on get_pixel_transfer_program in st_atom_pixeltransfer.c.
3519 get_pixel_transfer_visitor(struct st_fragment_program
*fp
,
3520 glsl_to_tgsi_visitor
*original
,
3521 int scale_and_bias
, int pixel_maps
)
3523 glsl_to_tgsi_visitor
*v
= new glsl_to_tgsi_visitor();
3524 struct st_context
*st
= st_context(original
->ctx
);
3525 struct gl_program
*prog
= &fp
->Base
.Base
;
3526 struct gl_program_parameter_list
*params
= _mesa_new_parameter_list();
3527 st_src_reg coord
, src0
;
3529 glsl_to_tgsi_instruction
*inst
;
3531 /* Copy attributes of the glsl_to_tgsi_visitor in the original shader. */
3532 v
->ctx
= original
->ctx
;
3534 v
->glsl_version
= original
->glsl_version
;
3535 v
->options
= original
->options
;
3536 v
->next_temp
= original
->next_temp
;
3537 v
->num_address_regs
= original
->num_address_regs
;
3538 v
->samplers_used
= prog
->SamplersUsed
= original
->samplers_used
;
3539 v
->indirect_addr_temps
= original
->indirect_addr_temps
;
3540 v
->indirect_addr_consts
= original
->indirect_addr_consts
;
3541 _mesa_free_parameter_list(v
->immediates
);
3542 v
->immediates
= _mesa_clone_parameter_list(original
->immediates
);
3545 * Get initial pixel color from the texture.
3546 * TEX colorTemp, fragment.texcoord[0], texture[0], 2D;
3548 coord
= st_src_reg(PROGRAM_INPUT
, FRAG_ATTRIB_TEX0
, glsl_type::vec2_type
);
3549 src0
= v
->get_temp(glsl_type::vec4_type
);
3550 dst0
= st_dst_reg(src0
);
3551 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, dst0
, coord
);
3553 inst
->tex_target
= TEXTURE_2D_INDEX
;
3555 prog
->InputsRead
|= (1 << FRAG_ATTRIB_TEX0
);
3556 prog
->SamplersUsed
|= (1 << 0); /* mark sampler 0 as used */
3557 v
->samplers_used
|= (1 << 0);
3559 if (scale_and_bias
) {
3560 static const gl_state_index scale_state
[STATE_LENGTH
] =
3561 { STATE_INTERNAL
, STATE_PT_SCALE
,
3562 (gl_state_index
) 0, (gl_state_index
) 0, (gl_state_index
) 0 };
3563 static const gl_state_index bias_state
[STATE_LENGTH
] =
3564 { STATE_INTERNAL
, STATE_PT_BIAS
,
3565 (gl_state_index
) 0, (gl_state_index
) 0, (gl_state_index
) 0 };
3566 GLint scale_p
, bias_p
;
3567 st_src_reg scale
, bias
;
3569 scale_p
= _mesa_add_state_reference(params
, scale_state
);
3570 bias_p
= _mesa_add_state_reference(params
, bias_state
);
3572 /* MAD colorTemp, colorTemp, scale, bias; */
3573 scale
= st_src_reg(PROGRAM_STATE_VAR
, scale_p
, GLSL_TYPE_FLOAT
);
3574 bias
= st_src_reg(PROGRAM_STATE_VAR
, bias_p
, GLSL_TYPE_FLOAT
);
3575 inst
= v
->emit(NULL
, TGSI_OPCODE_MAD
, dst0
, src0
, scale
, bias
);
3579 st_src_reg temp
= v
->get_temp(glsl_type::vec4_type
);
3580 st_dst_reg temp_dst
= st_dst_reg(temp
);
3582 assert(st
->pixel_xfer
.pixelmap_texture
);
3584 /* With a little effort, we can do four pixel map look-ups with
3585 * two TEX instructions:
3588 /* TEX temp.rg, colorTemp.rgba, texture[1], 2D; */
3589 temp_dst
.writemask
= WRITEMASK_XY
; /* write R,G */
3590 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, temp_dst
, src0
);
3592 inst
->tex_target
= TEXTURE_2D_INDEX
;
3594 /* TEX temp.ba, colorTemp.baba, texture[1], 2D; */
3595 src0
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_Z
, SWIZZLE_W
, SWIZZLE_Z
, SWIZZLE_W
);
3596 temp_dst
.writemask
= WRITEMASK_ZW
; /* write B,A */
3597 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, temp_dst
, src0
);
3599 inst
->tex_target
= TEXTURE_2D_INDEX
;
3601 prog
->SamplersUsed
|= (1 << 1); /* mark sampler 1 as used */
3602 v
->samplers_used
|= (1 << 1);
3604 /* MOV colorTemp, temp; */
3605 inst
= v
->emit(NULL
, TGSI_OPCODE_MOV
, dst0
, temp
);
3608 /* Now copy the instructions from the original glsl_to_tgsi_visitor into the
3610 foreach_iter(exec_list_iterator
, iter
, original
->instructions
) {
3611 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3612 st_src_reg src_regs
[3];
3614 if (inst
->dst
.file
== PROGRAM_OUTPUT
)
3615 prog
->OutputsWritten
|= BITFIELD64_BIT(inst
->dst
.index
);
3617 for (int i
=0; i
<3; i
++) {
3618 src_regs
[i
] = inst
->src
[i
];
3619 if (src_regs
[i
].file
== PROGRAM_INPUT
&&
3620 src_regs
[i
].index
== FRAG_ATTRIB_COL0
)
3622 src_regs
[i
].file
= PROGRAM_TEMPORARY
;
3623 src_regs
[i
].index
= src0
.index
;
3625 else if (src_regs
[i
].file
== PROGRAM_INPUT
)
3626 prog
->InputsRead
|= (1 << src_regs
[i
].index
);
3629 v
->emit(NULL
, inst
->op
, inst
->dst
, src_regs
[0], src_regs
[1], src_regs
[2]);
3632 /* Make modifications to fragment program info. */
3633 prog
->Parameters
= _mesa_combine_parameter_lists(params
,
3634 original
->prog
->Parameters
);
3635 prog
->Attributes
= _mesa_clone_parameter_list(original
->prog
->Attributes
);
3636 prog
->Varying
= _mesa_clone_parameter_list(original
->prog
->Varying
);
3637 _mesa_free_parameter_list(params
);
3638 count_resources(v
, prog
);
3639 fp
->glsl_to_tgsi
= v
;
3643 * Make fragment program for glBitmap:
3644 * Sample the texture and kill the fragment if the bit is 0.
3645 * This program will be combined with the user's fragment program.
3647 * Based on make_bitmap_fragment_program in st_cb_bitmap.c.
3650 get_bitmap_visitor(struct st_fragment_program
*fp
,
3651 glsl_to_tgsi_visitor
*original
, int samplerIndex
)
3653 glsl_to_tgsi_visitor
*v
= new glsl_to_tgsi_visitor();
3654 struct st_context
*st
= st_context(original
->ctx
);
3655 struct gl_program
*prog
= &fp
->Base
.Base
;
3656 st_src_reg coord
, src0
;
3658 glsl_to_tgsi_instruction
*inst
;
3660 /* Copy attributes of the glsl_to_tgsi_visitor in the original shader. */
3661 v
->ctx
= original
->ctx
;
3663 v
->glsl_version
= original
->glsl_version
;
3664 v
->options
= original
->options
;
3665 v
->next_temp
= original
->next_temp
;
3666 v
->num_address_regs
= original
->num_address_regs
;
3667 v
->samplers_used
= prog
->SamplersUsed
= original
->samplers_used
;
3668 v
->indirect_addr_temps
= original
->indirect_addr_temps
;
3669 v
->indirect_addr_consts
= original
->indirect_addr_consts
;
3670 _mesa_free_parameter_list(v
->immediates
);
3671 v
->immediates
= _mesa_clone_parameter_list(original
->immediates
);
3673 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
3674 coord
= st_src_reg(PROGRAM_INPUT
, FRAG_ATTRIB_TEX0
, glsl_type::vec2_type
);
3675 src0
= v
->get_temp(glsl_type::vec4_type
);
3676 dst0
= st_dst_reg(src0
);
3677 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, dst0
, coord
);
3678 inst
->sampler
= samplerIndex
;
3679 inst
->tex_target
= TEXTURE_2D_INDEX
;
3681 prog
->InputsRead
|= (1 << FRAG_ATTRIB_TEX0
);
3682 prog
->SamplersUsed
|= (1 << samplerIndex
); /* mark sampler as used */
3683 v
->samplers_used
|= (1 << samplerIndex
);
3685 /* KIL if -tmp0 < 0 # texel=0 -> keep / texel=0 -> discard */
3686 src0
.negate
= NEGATE_XYZW
;
3687 if (st
->bitmap
.tex_format
== PIPE_FORMAT_L8_UNORM
)
3688 src0
.swizzle
= SWIZZLE_XXXX
;
3689 inst
= v
->emit(NULL
, TGSI_OPCODE_KIL
, undef_dst
, src0
);
3691 /* Now copy the instructions from the original glsl_to_tgsi_visitor into the
3693 foreach_iter(exec_list_iterator
, iter
, original
->instructions
) {
3694 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3695 st_src_reg src_regs
[3];
3697 if (inst
->dst
.file
== PROGRAM_OUTPUT
)
3698 prog
->OutputsWritten
|= BITFIELD64_BIT(inst
->dst
.index
);
3700 for (int i
=0; i
<3; i
++) {
3701 src_regs
[i
] = inst
->src
[i
];
3702 if (src_regs
[i
].file
== PROGRAM_INPUT
)
3703 prog
->InputsRead
|= (1 << src_regs
[i
].index
);
3706 v
->emit(NULL
, inst
->op
, inst
->dst
, src_regs
[0], src_regs
[1], src_regs
[2]);
3709 /* Make modifications to fragment program info. */
3710 prog
->Parameters
= _mesa_clone_parameter_list(original
->prog
->Parameters
);
3711 prog
->Attributes
= _mesa_clone_parameter_list(original
->prog
->Attributes
);
3712 prog
->Varying
= _mesa_clone_parameter_list(original
->prog
->Varying
);
3713 count_resources(v
, prog
);
3714 fp
->glsl_to_tgsi
= v
;
3717 /* ------------------------- TGSI conversion stuff -------------------------- */
3719 unsigned branch_target
;
3724 * Intermediate state used during shader translation.
3726 struct st_translate
{
3727 struct ureg_program
*ureg
;
3729 struct ureg_dst temps
[MAX_TEMPS
];
3730 struct ureg_src
*constants
;
3731 struct ureg_src
*immediates
;
3732 struct ureg_dst outputs
[PIPE_MAX_SHADER_OUTPUTS
];
3733 struct ureg_src inputs
[PIPE_MAX_SHADER_INPUTS
];
3734 struct ureg_dst address
[1];
3735 struct ureg_src samplers
[PIPE_MAX_SAMPLERS
];
3736 struct ureg_src systemValues
[SYSTEM_VALUE_MAX
];
3738 /* Extra info for handling point size clamping in vertex shader */
3739 struct ureg_dst pointSizeResult
; /**< Actual point size output register */
3740 struct ureg_src pointSizeConst
; /**< Point size range constant register */
3741 GLint pointSizeOutIndex
; /**< Temp point size output register */
3742 GLboolean prevInstWrotePointSize
;
3744 const GLuint
*inputMapping
;
3745 const GLuint
*outputMapping
;
3747 /* For every instruction that contains a label (eg CALL), keep
3748 * details so that we can go back afterwards and emit the correct
3749 * tgsi instruction number for each label.
3751 struct label
*labels
;
3752 unsigned labels_size
;
3753 unsigned labels_count
;
3755 /* Keep a record of the tgsi instruction number that each mesa
3756 * instruction starts at, will be used to fix up labels after
3761 unsigned insn_count
;
3763 unsigned procType
; /**< TGSI_PROCESSOR_VERTEX/FRAGMENT */
3768 /** Map Mesa's SYSTEM_VALUE_x to TGSI_SEMANTIC_x */
3769 static unsigned mesa_sysval_to_semantic
[SYSTEM_VALUE_MAX
] = {
3771 TGSI_SEMANTIC_INSTANCEID
3775 * Make note of a branch to a label in the TGSI code.
3776 * After we've emitted all instructions, we'll go over the list
3777 * of labels built here and patch the TGSI code with the actual
3778 * location of each label.
3780 static unsigned *get_label(struct st_translate
*t
, unsigned branch_target
)
3784 if (t
->labels_count
+ 1 >= t
->labels_size
) {
3785 t
->labels_size
= 1 << (util_logbase2(t
->labels_size
) + 1);
3786 t
->labels
= (struct label
*)realloc(t
->labels
,
3787 t
->labels_size
* sizeof(struct label
));
3788 if (t
->labels
== NULL
) {
3789 static unsigned dummy
;
3795 i
= t
->labels_count
++;
3796 t
->labels
[i
].branch_target
= branch_target
;
3797 return &t
->labels
[i
].token
;
3801 * Called prior to emitting the TGSI code for each instruction.
3802 * Allocate additional space for instructions if needed.
3803 * Update the insn[] array so the next glsl_to_tgsi_instruction points to
3804 * the next TGSI instruction.
3806 static void set_insn_start(struct st_translate
*t
, unsigned start
)
3808 if (t
->insn_count
+ 1 >= t
->insn_size
) {
3809 t
->insn_size
= 1 << (util_logbase2(t
->insn_size
) + 1);
3810 t
->insn
= (unsigned *)realloc(t
->insn
, t
->insn_size
* sizeof(t
->insn
[0]));
3811 if (t
->insn
== NULL
) {
3817 t
->insn
[t
->insn_count
++] = start
;
3821 * Map a glsl_to_tgsi constant/immediate to a TGSI immediate.
3823 static struct ureg_src
3824 emit_immediate(struct st_translate
*t
,
3825 struct gl_program_parameter_list
*params
,
3828 struct ureg_program
*ureg
= t
->ureg
;
3830 switch(params
->Parameters
[index
].DataType
)
3836 return ureg_DECL_immediate(ureg
, (float *)params
->ParameterValues
[index
], 4);
3841 return ureg_DECL_immediate_int(ureg
, (int *)params
->ParameterValues
[index
], 4);
3842 case GL_UNSIGNED_INT
:
3843 case GL_UNSIGNED_INT_VEC2
:
3844 case GL_UNSIGNED_INT_VEC3
:
3845 case GL_UNSIGNED_INT_VEC4
:
3850 return ureg_DECL_immediate_uint(ureg
, (unsigned *)params
->ParameterValues
[index
], 4);
3852 assert(!"should not get here - type must be float, int, uint, or bool");
3853 return ureg_src_undef();
3858 * Map a glsl_to_tgsi dst register to a TGSI ureg_dst register.
3860 static struct ureg_dst
3861 dst_register(struct st_translate
*t
,
3862 gl_register_file file
,
3866 case PROGRAM_UNDEFINED
:
3867 return ureg_dst_undef();
3869 case PROGRAM_TEMPORARY
:
3870 if (ureg_dst_is_undef(t
->temps
[index
]))
3871 t
->temps
[index
] = ureg_DECL_temporary(t
->ureg
);
3873 return t
->temps
[index
];
3875 case PROGRAM_OUTPUT
:
3876 if (t
->procType
== TGSI_PROCESSOR_VERTEX
&& index
== VERT_RESULT_PSIZ
)
3877 t
->prevInstWrotePointSize
= GL_TRUE
;
3879 if (t
->procType
== TGSI_PROCESSOR_VERTEX
)
3880 assert(index
< VERT_RESULT_MAX
);
3881 else if (t
->procType
== TGSI_PROCESSOR_FRAGMENT
)
3882 assert(index
< FRAG_RESULT_MAX
);
3884 assert(index
< GEOM_RESULT_MAX
);
3886 assert(t
->outputMapping
[index
] < Elements(t
->outputs
));
3888 return t
->outputs
[t
->outputMapping
[index
]];
3890 case PROGRAM_ADDRESS
:
3891 return t
->address
[index
];
3894 assert(!"unknown dst register file");
3895 return ureg_dst_undef();
3900 * Map a glsl_to_tgsi src register to a TGSI ureg_src register.
3902 static struct ureg_src
3903 src_register(struct st_translate
*t
,
3904 gl_register_file file
,
3908 case PROGRAM_UNDEFINED
:
3909 return ureg_src_undef();
3911 case PROGRAM_TEMPORARY
:
3913 assert(index
< Elements(t
->temps
));
3914 if (ureg_dst_is_undef(t
->temps
[index
]))
3915 t
->temps
[index
] = ureg_DECL_temporary(t
->ureg
);
3916 return ureg_src(t
->temps
[index
]);
3918 case PROGRAM_NAMED_PARAM
:
3919 case PROGRAM_ENV_PARAM
:
3920 case PROGRAM_LOCAL_PARAM
:
3921 case PROGRAM_UNIFORM
:
3923 return t
->constants
[index
];
3924 case PROGRAM_STATE_VAR
:
3925 case PROGRAM_CONSTANT
: /* ie, immediate */
3927 return ureg_DECL_constant(t
->ureg
, 0);
3929 return t
->constants
[index
];
3931 case PROGRAM_IMMEDIATE
:
3932 return t
->immediates
[index
];
3935 assert(t
->inputMapping
[index
] < Elements(t
->inputs
));
3936 return t
->inputs
[t
->inputMapping
[index
]];
3938 case PROGRAM_OUTPUT
:
3939 assert(t
->outputMapping
[index
] < Elements(t
->outputs
));
3940 return ureg_src(t
->outputs
[t
->outputMapping
[index
]]); /* not needed? */
3942 case PROGRAM_ADDRESS
:
3943 return ureg_src(t
->address
[index
]);
3945 case PROGRAM_SYSTEM_VALUE
:
3946 assert(index
< Elements(t
->systemValues
));
3947 return t
->systemValues
[index
];
3950 assert(!"unknown src register file");
3951 return ureg_src_undef();
3956 * Create a TGSI ureg_dst register from an st_dst_reg.
3958 static struct ureg_dst
3959 translate_dst(struct st_translate
*t
,
3960 const st_dst_reg
*dst_reg
,
3963 struct ureg_dst dst
= dst_register(t
,
3967 dst
= ureg_writemask(dst
, dst_reg
->writemask
);
3970 dst
= ureg_saturate(dst
);
3972 if (dst_reg
->reladdr
!= NULL
)
3973 dst
= ureg_dst_indirect(dst
, ureg_src(t
->address
[0]));
3979 * Create a TGSI ureg_src register from an st_src_reg.
3981 static struct ureg_src
3982 translate_src(struct st_translate
*t
, const st_src_reg
*src_reg
)
3984 struct ureg_src src
= src_register(t
, src_reg
->file
, src_reg
->index
);
3986 src
= ureg_swizzle(src
,
3987 GET_SWZ(src_reg
->swizzle
, 0) & 0x3,
3988 GET_SWZ(src_reg
->swizzle
, 1) & 0x3,
3989 GET_SWZ(src_reg
->swizzle
, 2) & 0x3,
3990 GET_SWZ(src_reg
->swizzle
, 3) & 0x3);
3992 if ((src_reg
->negate
& 0xf) == NEGATE_XYZW
)
3993 src
= ureg_negate(src
);
3995 if (src_reg
->reladdr
!= NULL
) {
3996 /* Normally ureg_src_indirect() would be used here, but a stupid compiler
3997 * bug in g++ makes ureg_src_indirect (an inline C function) erroneously
3998 * set the bit for src.Negate. So we have to do the operation manually
3999 * here to work around the compiler's problems. */
4000 /*src = ureg_src_indirect(src, ureg_src(t->address[0]));*/
4001 struct ureg_src addr
= ureg_src(t
->address
[0]);
4003 src
.IndirectFile
= addr
.File
;
4004 src
.IndirectIndex
= addr
.Index
;
4005 src
.IndirectSwizzle
= addr
.SwizzleX
;
4007 if (src_reg
->file
!= PROGRAM_INPUT
&&
4008 src_reg
->file
!= PROGRAM_OUTPUT
) {
4009 /* If src_reg->index was negative, it was set to zero in
4010 * src_register(). Reassign it now. But don't do this
4011 * for input/output regs since they get remapped while
4012 * const buffers don't.
4014 src
.Index
= src_reg
->index
;
4022 compile_tgsi_instruction(struct st_translate
*t
,
4023 const struct glsl_to_tgsi_instruction
*inst
)
4025 struct ureg_program
*ureg
= t
->ureg
;
4027 struct ureg_dst dst
[1];
4028 struct ureg_src src
[4];
4032 num_dst
= num_inst_dst_regs(inst
->op
);
4033 num_src
= num_inst_src_regs(inst
->op
);
4036 dst
[0] = translate_dst(t
,
4040 for (i
= 0; i
< num_src
; i
++)
4041 src
[i
] = translate_src(t
, &inst
->src
[i
]);
4044 case TGSI_OPCODE_BGNLOOP
:
4045 case TGSI_OPCODE_CAL
:
4046 case TGSI_OPCODE_ELSE
:
4047 case TGSI_OPCODE_ENDLOOP
:
4048 case TGSI_OPCODE_IF
:
4049 assert(num_dst
== 0);
4050 ureg_label_insn(ureg
,
4054 inst
->op
== TGSI_OPCODE_CAL
? inst
->function
->sig_id
: 0));
4057 case TGSI_OPCODE_TEX
:
4058 case TGSI_OPCODE_TXB
:
4059 case TGSI_OPCODE_TXD
:
4060 case TGSI_OPCODE_TXL
:
4061 case TGSI_OPCODE_TXP
:
4062 src
[num_src
++] = t
->samplers
[inst
->sampler
];
4066 translate_texture_target(inst
->tex_target
, inst
->tex_shadow
),
4070 case TGSI_OPCODE_SCS
:
4071 dst
[0] = ureg_writemask(dst
[0], TGSI_WRITEMASK_XY
);
4072 ureg_insn(ureg
, inst
->op
, dst
, num_dst
, src
, num_src
);
4085 * Emit the TGSI instructions to adjust the WPOS pixel center convention
4086 * Basically, add (adjX, adjY) to the fragment position.
4089 emit_adjusted_wpos(struct st_translate
*t
,
4090 const struct gl_program
*program
,
4091 float adjX
, float adjY
)
4093 struct ureg_program
*ureg
= t
->ureg
;
4094 struct ureg_dst wpos_temp
= ureg_DECL_temporary(ureg
);
4095 struct ureg_src wpos_input
= t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_WPOS
]];
4097 /* Note that we bias X and Y and pass Z and W through unchanged.
4098 * The shader might also use gl_FragCoord.w and .z.
4100 ureg_ADD(ureg
, wpos_temp
, wpos_input
,
4101 ureg_imm4f(ureg
, adjX
, adjY
, 0.0f
, 0.0f
));
4103 t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_WPOS
]] = ureg_src(wpos_temp
);
4108 * Emit the TGSI instructions for inverting the WPOS y coordinate.
4109 * This code is unavoidable because it also depends on whether
4110 * a FBO is bound (STATE_FB_WPOS_Y_TRANSFORM).
4113 emit_wpos_inversion(struct st_translate
*t
,
4114 const struct gl_program
*program
,
4117 struct ureg_program
*ureg
= t
->ureg
;
4119 /* Fragment program uses fragment position input.
4120 * Need to replace instances of INPUT[WPOS] with temp T
4121 * where T = INPUT[WPOS] by y is inverted.
4123 static const gl_state_index wposTransformState
[STATE_LENGTH
]
4124 = { STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
,
4125 (gl_state_index
)0, (gl_state_index
)0, (gl_state_index
)0 };
4127 /* XXX: note we are modifying the incoming shader here! Need to
4128 * do this before emitting the constant decls below, or this
4131 unsigned wposTransConst
= _mesa_add_state_reference(program
->Parameters
,
4132 wposTransformState
);
4134 struct ureg_src wpostrans
= ureg_DECL_constant(ureg
, wposTransConst
);
4135 struct ureg_dst wpos_temp
;
4136 struct ureg_src wpos_input
= t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_WPOS
]];
4138 /* MOV wpos_temp, input[wpos]
4140 if (wpos_input
.File
== TGSI_FILE_TEMPORARY
)
4141 wpos_temp
= ureg_dst(wpos_input
);
4143 wpos_temp
= ureg_DECL_temporary(ureg
);
4144 ureg_MOV(ureg
, wpos_temp
, wpos_input
);
4148 /* MAD wpos_temp.y, wpos_input, wpostrans.xxxx, wpostrans.yyyy
4151 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
4153 ureg_scalar(wpostrans
, 0),
4154 ureg_scalar(wpostrans
, 1));
4156 /* MAD wpos_temp.y, wpos_input, wpostrans.zzzz, wpostrans.wwww
4159 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
4161 ureg_scalar(wpostrans
, 2),
4162 ureg_scalar(wpostrans
, 3));
4165 /* Use wpos_temp as position input from here on:
4167 t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_WPOS
]] = ureg_src(wpos_temp
);
4172 * Emit fragment position/ooordinate code.
4175 emit_wpos(struct st_context
*st
,
4176 struct st_translate
*t
,
4177 const struct gl_program
*program
,
4178 struct ureg_program
*ureg
)
4180 const struct gl_fragment_program
*fp
=
4181 (const struct gl_fragment_program
*) program
;
4182 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
4183 boolean invert
= FALSE
;
4185 if (fp
->OriginUpperLeft
) {
4186 /* Fragment shader wants origin in upper-left */
4187 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
)) {
4188 /* the driver supports upper-left origin */
4190 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
)) {
4191 /* the driver supports lower-left origin, need to invert Y */
4192 ureg_property_fs_coord_origin(ureg
, TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
4199 /* Fragment shader wants origin in lower-left */
4200 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
))
4201 /* the driver supports lower-left origin */
4202 ureg_property_fs_coord_origin(ureg
, TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
4203 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
))
4204 /* the driver supports upper-left origin, need to invert Y */
4210 if (fp
->PixelCenterInteger
) {
4211 /* Fragment shader wants pixel center integer */
4212 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
))
4213 /* the driver supports pixel center integer */
4214 ureg_property_fs_coord_pixel_center(ureg
, TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
4215 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
))
4216 /* the driver supports pixel center half integer, need to bias X,Y */
4217 emit_adjusted_wpos(t
, program
, 0.5f
, invert
? 0.5f
: -0.5f
);
4222 /* Fragment shader wants pixel center half integer */
4223 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
)) {
4224 /* the driver supports pixel center half integer */
4226 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)) {
4227 /* the driver supports pixel center integer, need to bias X,Y */
4228 ureg_property_fs_coord_pixel_center(ureg
, TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
4229 emit_adjusted_wpos(t
, program
, 0.5f
, invert
? -0.5f
: 0.5f
);
4235 /* we invert after adjustment so that we avoid the MOV to temporary,
4236 * and reuse the adjustment ADD instead */
4237 emit_wpos_inversion(t
, program
, invert
);
4241 * OpenGL's fragment gl_FrontFace input is 1 for front-facing, 0 for back.
4242 * TGSI uses +1 for front, -1 for back.
4243 * This function converts the TGSI value to the GL value. Simply clamping/
4244 * saturating the value to [0,1] does the job.
4247 emit_face_var(struct st_translate
*t
)
4249 struct ureg_program
*ureg
= t
->ureg
;
4250 struct ureg_dst face_temp
= ureg_DECL_temporary(ureg
);
4251 struct ureg_src face_input
= t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_FACE
]];
4253 /* MOV_SAT face_temp, input[face] */
4254 face_temp
= ureg_saturate(face_temp
);
4255 ureg_MOV(ureg
, face_temp
, face_input
);
4257 /* Use face_temp as face input from here on: */
4258 t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_FACE
]] = ureg_src(face_temp
);
4262 emit_edgeflags(struct st_translate
*t
)
4264 struct ureg_program
*ureg
= t
->ureg
;
4265 struct ureg_dst edge_dst
= t
->outputs
[t
->outputMapping
[VERT_RESULT_EDGE
]];
4266 struct ureg_src edge_src
= t
->inputs
[t
->inputMapping
[VERT_ATTRIB_EDGEFLAG
]];
4268 ureg_MOV(ureg
, edge_dst
, edge_src
);
4272 * Translate intermediate IR (glsl_to_tgsi_instruction) to TGSI format.
4273 * \param program the program to translate
4274 * \param numInputs number of input registers used
4275 * \param inputMapping maps Mesa fragment program inputs to TGSI generic
4277 * \param inputSemanticName the TGSI_SEMANTIC flag for each input
4278 * \param inputSemanticIndex the semantic index (ex: which texcoord) for
4280 * \param interpMode the TGSI_INTERPOLATE_LINEAR/PERSP mode for each input
4281 * \param numOutputs number of output registers used
4282 * \param outputMapping maps Mesa fragment program outputs to TGSI
4284 * \param outputSemanticName the TGSI_SEMANTIC flag for each output
4285 * \param outputSemanticIndex the semantic index (ex: which texcoord) for
4288 * \return PIPE_OK or PIPE_ERROR_OUT_OF_MEMORY
4290 extern "C" enum pipe_error
4291 st_translate_program(
4292 struct gl_context
*ctx
,
4294 struct ureg_program
*ureg
,
4295 glsl_to_tgsi_visitor
*program
,
4296 const struct gl_program
*proginfo
,
4298 const GLuint inputMapping
[],
4299 const ubyte inputSemanticName
[],
4300 const ubyte inputSemanticIndex
[],
4301 const GLuint interpMode
[],
4303 const GLuint outputMapping
[],
4304 const ubyte outputSemanticName
[],
4305 const ubyte outputSemanticIndex
[],
4306 boolean passthrough_edgeflags
)
4308 struct st_translate translate
, *t
;
4310 enum pipe_error ret
= PIPE_OK
;
4312 assert(numInputs
<= Elements(t
->inputs
));
4313 assert(numOutputs
<= Elements(t
->outputs
));
4316 memset(t
, 0, sizeof *t
);
4318 t
->procType
= procType
;
4319 t
->inputMapping
= inputMapping
;
4320 t
->outputMapping
= outputMapping
;
4322 t
->pointSizeOutIndex
= -1;
4323 t
->prevInstWrotePointSize
= GL_FALSE
;
4326 * Declare input attributes.
4328 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
4329 for (i
= 0; i
< numInputs
; i
++) {
4330 t
->inputs
[i
] = ureg_DECL_fs_input(ureg
,
4331 inputSemanticName
[i
],
4332 inputSemanticIndex
[i
],
4336 if (proginfo
->InputsRead
& FRAG_BIT_WPOS
) {
4337 /* Must do this after setting up t->inputs, and before
4338 * emitting constant references, below:
4340 emit_wpos(st_context(ctx
), t
, proginfo
, ureg
);
4343 if (proginfo
->InputsRead
& FRAG_BIT_FACE
)
4347 * Declare output attributes.
4349 for (i
= 0; i
< numOutputs
; i
++) {
4350 switch (outputSemanticName
[i
]) {
4351 case TGSI_SEMANTIC_POSITION
:
4352 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4353 TGSI_SEMANTIC_POSITION
, /* Z/Depth */
4354 outputSemanticIndex
[i
]);
4355 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_Z
);
4357 case TGSI_SEMANTIC_STENCIL
:
4358 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4359 TGSI_SEMANTIC_STENCIL
, /* Stencil */
4360 outputSemanticIndex
[i
]);
4361 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_Y
);
4363 case TGSI_SEMANTIC_COLOR
:
4364 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4365 TGSI_SEMANTIC_COLOR
,
4366 outputSemanticIndex
[i
]);
4369 assert(!"fragment shader outputs must be POSITION/STENCIL/COLOR");
4370 return PIPE_ERROR_BAD_INPUT
;
4374 else if (procType
== TGSI_PROCESSOR_GEOMETRY
) {
4375 for (i
= 0; i
< numInputs
; i
++) {
4376 t
->inputs
[i
] = ureg_DECL_gs_input(ureg
,
4378 inputSemanticName
[i
],
4379 inputSemanticIndex
[i
]);
4382 for (i
= 0; i
< numOutputs
; i
++) {
4383 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4384 outputSemanticName
[i
],
4385 outputSemanticIndex
[i
]);
4389 assert(procType
== TGSI_PROCESSOR_VERTEX
);
4391 for (i
= 0; i
< numInputs
; i
++) {
4392 t
->inputs
[i
] = ureg_DECL_vs_input(ureg
, i
);
4395 for (i
= 0; i
< numOutputs
; i
++) {
4396 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4397 outputSemanticName
[i
],
4398 outputSemanticIndex
[i
]);
4399 if ((outputSemanticName
[i
] == TGSI_SEMANTIC_PSIZE
) && proginfo
->Id
) {
4400 /* Writing to the point size result register requires special
4401 * handling to implement clamping.
4403 static const gl_state_index pointSizeClampState
[STATE_LENGTH
]
4404 = { STATE_INTERNAL
, STATE_POINT_SIZE_IMPL_CLAMP
, (gl_state_index
)0, (gl_state_index
)0, (gl_state_index
)0 };
4405 /* XXX: note we are modifying the incoming shader here! Need to
4406 * do this before emitting the constant decls below, or this
4409 unsigned pointSizeClampConst
=
4410 _mesa_add_state_reference(proginfo
->Parameters
,
4411 pointSizeClampState
);
4412 struct ureg_dst psizregtemp
= ureg_DECL_temporary(ureg
);
4413 t
->pointSizeConst
= ureg_DECL_constant(ureg
, pointSizeClampConst
);
4414 t
->pointSizeResult
= t
->outputs
[i
];
4415 t
->pointSizeOutIndex
= i
;
4416 t
->outputs
[i
] = psizregtemp
;
4419 if (passthrough_edgeflags
)
4423 /* Declare address register.
4425 if (program
->num_address_regs
> 0) {
4426 assert(program
->num_address_regs
== 1);
4427 t
->address
[0] = ureg_DECL_address(ureg
);
4430 /* Declare misc input registers
4433 GLbitfield sysInputs
= proginfo
->SystemValuesRead
;
4434 unsigned numSys
= 0;
4435 for (i
= 0; sysInputs
; i
++) {
4436 if (sysInputs
& (1 << i
)) {
4437 unsigned semName
= mesa_sysval_to_semantic
[i
];
4438 t
->systemValues
[i
] = ureg_DECL_system_value(ureg
, numSys
, semName
, 0);
4440 sysInputs
&= ~(1 << i
);
4445 if (program
->indirect_addr_temps
) {
4446 /* If temps are accessed with indirect addressing, declare temporaries
4447 * in sequential order. Else, we declare them on demand elsewhere.
4448 * (Note: the number of temporaries is equal to program->next_temp)
4450 for (i
= 0; i
< (unsigned)program
->next_temp
; i
++) {
4451 /* XXX use TGSI_FILE_TEMPORARY_ARRAY when it's supported by ureg */
4452 t
->temps
[i
] = ureg_DECL_temporary(t
->ureg
);
4456 /* Emit constants and uniforms. TGSI uses a single index space for these,
4457 * so we put all the translated regs in t->constants.
4459 if (proginfo
->Parameters
) {
4460 t
->constants
= (struct ureg_src
*)CALLOC(proginfo
->Parameters
->NumParameters
* sizeof(t
->constants
[0]));
4461 if (t
->constants
== NULL
) {
4462 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
4466 for (i
= 0; i
< proginfo
->Parameters
->NumParameters
; i
++) {
4467 switch (proginfo
->Parameters
->Parameters
[i
].Type
) {
4468 case PROGRAM_ENV_PARAM
:
4469 case PROGRAM_LOCAL_PARAM
:
4470 case PROGRAM_STATE_VAR
:
4471 case PROGRAM_NAMED_PARAM
:
4472 case PROGRAM_UNIFORM
:
4473 t
->constants
[i
] = ureg_DECL_constant(ureg
, i
);
4476 /* Emit immediates for PROGRAM_CONSTANT only when there's no indirect
4477 * addressing of the const buffer.
4478 * FIXME: Be smarter and recognize param arrays:
4479 * indirect addressing is only valid within the referenced
4482 case PROGRAM_CONSTANT
:
4483 if (program
->indirect_addr_consts
)
4484 t
->constants
[i
] = ureg_DECL_constant(ureg
, i
);
4486 t
->constants
[i
] = emit_immediate(t
, proginfo
->Parameters
, i
);
4494 /* Emit immediate values.
4496 t
->immediates
= (struct ureg_src
*)CALLOC(program
->immediates
->NumParameters
* sizeof(struct ureg_src
));
4497 if (t
->immediates
== NULL
) {
4498 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
4501 for (i
= 0; i
< program
->immediates
->NumParameters
; i
++) {
4502 assert(program
->immediates
->Parameters
[i
].Type
== PROGRAM_IMMEDIATE
);
4503 t
->immediates
[i
] = emit_immediate(t
, program
->immediates
, i
);
4506 /* texture samplers */
4507 for (i
= 0; i
< ctx
->Const
.MaxTextureImageUnits
; i
++) {
4508 if (program
->samplers_used
& (1 << i
)) {
4509 t
->samplers
[i
] = ureg_DECL_sampler(ureg
, i
);
4513 /* Emit each instruction in turn:
4515 foreach_iter(exec_list_iterator
, iter
, program
->instructions
) {
4516 set_insn_start(t
, ureg_get_instruction_number(ureg
));
4517 compile_tgsi_instruction(t
, (glsl_to_tgsi_instruction
*)iter
.get());
4519 if (t
->prevInstWrotePointSize
&& proginfo
->Id
) {
4520 /* The previous instruction wrote to the (fake) vertex point size
4521 * result register. Now we need to clamp that value to the min/max
4522 * point size range, putting the result into the real point size
4524 * Note that we can't do this easily at the end of program due to
4525 * possible early return.
4527 set_insn_start(t
, ureg_get_instruction_number(ureg
));
4529 ureg_writemask(t
->outputs
[t
->pointSizeOutIndex
], WRITEMASK_X
),
4530 ureg_src(t
->outputs
[t
->pointSizeOutIndex
]),
4531 ureg_swizzle(t
->pointSizeConst
, 1,1,1,1));
4532 ureg_MIN(t
->ureg
, ureg_writemask(t
->pointSizeResult
, WRITEMASK_X
),
4533 ureg_src(t
->outputs
[t
->pointSizeOutIndex
]),
4534 ureg_swizzle(t
->pointSizeConst
, 2,2,2,2));
4536 t
->prevInstWrotePointSize
= GL_FALSE
;
4539 /* Fix up all emitted labels:
4541 for (i
= 0; i
< t
->labels_count
; i
++) {
4542 ureg_fixup_label(ureg
, t
->labels
[i
].token
,
4543 t
->insn
[t
->labels
[i
].branch_target
]);
4550 FREE(t
->immediates
);
4553 debug_printf("%s: translate error flag set\n", __FUNCTION__
);
4558 /* ----------------------------- End TGSI code ------------------------------ */
4561 * Convert a shader's GLSL IR into a Mesa gl_program, although without
4562 * generating Mesa IR.
4564 static struct gl_program
*
4565 get_mesa_program(struct gl_context
*ctx
,
4566 struct gl_shader_program
*shader_program
,
4567 struct gl_shader
*shader
)
4569 glsl_to_tgsi_visitor
* v
= new glsl_to_tgsi_visitor();
4570 struct gl_program
*prog
;
4572 const char *target_string
;
4574 struct gl_shader_compiler_options
*options
=
4575 &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(shader
->Type
)];
4577 switch (shader
->Type
) {
4578 case GL_VERTEX_SHADER
:
4579 target
= GL_VERTEX_PROGRAM_ARB
;
4580 target_string
= "vertex";
4582 case GL_FRAGMENT_SHADER
:
4583 target
= GL_FRAGMENT_PROGRAM_ARB
;
4584 target_string
= "fragment";
4586 case GL_GEOMETRY_SHADER
:
4587 target
= GL_GEOMETRY_PROGRAM_NV
;
4588 target_string
= "geometry";
4591 assert(!"should not be reached");
4595 validate_ir_tree(shader
->ir
);
4597 prog
= ctx
->Driver
.NewProgram(ctx
, target
, shader_program
->Name
);
4600 prog
->Parameters
= _mesa_new_parameter_list();
4601 prog
->Varying
= _mesa_new_parameter_list();
4602 prog
->Attributes
= _mesa_new_parameter_list();
4605 v
->shader_program
= shader_program
;
4606 v
->options
= options
;
4607 v
->glsl_version
= ctx
->Const
.GLSLVersion
;
4609 add_uniforms_to_parameters_list(shader_program
, shader
, prog
);
4611 /* Emit intermediate IR for main(). */
4612 visit_exec_list(shader
->ir
, v
);
4614 /* Now emit bodies for any functions that were used. */
4616 progress
= GL_FALSE
;
4618 foreach_iter(exec_list_iterator
, iter
, v
->function_signatures
) {
4619 function_entry
*entry
= (function_entry
*)iter
.get();
4621 if (!entry
->bgn_inst
) {
4622 v
->current_function
= entry
;
4624 entry
->bgn_inst
= v
->emit(NULL
, TGSI_OPCODE_BGNSUB
);
4625 entry
->bgn_inst
->function
= entry
;
4627 visit_exec_list(&entry
->sig
->body
, v
);
4629 glsl_to_tgsi_instruction
*last
;
4630 last
= (glsl_to_tgsi_instruction
*)v
->instructions
.get_tail();
4631 if (last
->op
!= TGSI_OPCODE_RET
)
4632 v
->emit(NULL
, TGSI_OPCODE_RET
);
4634 glsl_to_tgsi_instruction
*end
;
4635 end
= v
->emit(NULL
, TGSI_OPCODE_ENDSUB
);
4636 end
->function
= entry
;
4644 /* Print out some information (for debugging purposes) used by the
4645 * optimization passes. */
4646 for (i
=0; i
< v
->next_temp
; i
++) {
4647 int fr
= v
->get_first_temp_read(i
);
4648 int fw
= v
->get_first_temp_write(i
);
4649 int lr
= v
->get_last_temp_read(i
);
4650 int lw
= v
->get_last_temp_write(i
);
4652 printf("Temp %d: FR=%3d FW=%3d LR=%3d LW=%3d\n", i
, fr
, fw
, lr
, lw
);
4657 /* Remove reads to output registers, and to varyings in vertex shaders. */
4658 v
->remove_output_reads(PROGRAM_OUTPUT
);
4659 if (target
== GL_VERTEX_PROGRAM_ARB
)
4660 v
->remove_output_reads(PROGRAM_VARYING
);
4662 /* Perform optimizations on the instructions in the glsl_to_tgsi_visitor. */
4664 v
->copy_propagate();
4665 while (v
->eliminate_dead_code_advanced());
4667 /* FIXME: These passes to optimize temporary registers don't work when there
4668 * is indirect addressing of the temporary register space. We need proper
4669 * array support so that we don't have to give up these passes in every
4670 * shader that uses arrays.
4672 if (!v
->indirect_addr_temps
) {
4673 v
->eliminate_dead_code();
4674 v
->merge_registers();
4675 v
->renumber_registers();
4678 /* Write the END instruction. */
4679 v
->emit(NULL
, TGSI_OPCODE_END
);
4681 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
4683 printf("GLSL IR for linked %s program %d:\n", target_string
,
4684 shader_program
->Name
);
4685 _mesa_print_ir(shader
->ir
, NULL
);
4690 prog
->Instructions
= NULL
;
4691 prog
->NumInstructions
= 0;
4693 do_set_program_inouts(shader
->ir
, prog
);
4694 count_resources(v
, prog
);
4696 check_resources(ctx
, shader_program
, v
, prog
);
4698 _mesa_reference_program(ctx
, &shader
->Program
, prog
);
4700 struct st_vertex_program
*stvp
;
4701 struct st_fragment_program
*stfp
;
4702 struct st_geometry_program
*stgp
;
4704 switch (shader
->Type
) {
4705 case GL_VERTEX_SHADER
:
4706 stvp
= (struct st_vertex_program
*)prog
;
4707 stvp
->glsl_to_tgsi
= v
;
4709 case GL_FRAGMENT_SHADER
:
4710 stfp
= (struct st_fragment_program
*)prog
;
4711 stfp
->glsl_to_tgsi
= v
;
4713 case GL_GEOMETRY_SHADER
:
4714 stgp
= (struct st_geometry_program
*)prog
;
4715 stgp
->glsl_to_tgsi
= v
;
4718 assert(!"should not be reached");
4728 st_new_shader(struct gl_context
*ctx
, GLuint name
, GLuint type
)
4730 struct gl_shader
*shader
;
4731 assert(type
== GL_FRAGMENT_SHADER
|| type
== GL_VERTEX_SHADER
||
4732 type
== GL_GEOMETRY_SHADER_ARB
);
4733 shader
= rzalloc(NULL
, struct gl_shader
);
4735 shader
->Type
= type
;
4736 shader
->Name
= name
;
4737 _mesa_init_shader(ctx
, shader
);
4742 struct gl_shader_program
*
4743 st_new_shader_program(struct gl_context
*ctx
, GLuint name
)
4745 struct gl_shader_program
*shProg
;
4746 shProg
= rzalloc(NULL
, struct gl_shader_program
);
4748 shProg
->Name
= name
;
4749 _mesa_init_shader_program(ctx
, shProg
);
4756 * Called via ctx->Driver.LinkShader()
4757 * This actually involves converting GLSL IR into an intermediate TGSI-like IR
4758 * with code lowering and other optimizations.
4761 st_link_shader(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
4763 assert(prog
->LinkStatus
);
4765 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
4766 if (prog
->_LinkedShaders
[i
] == NULL
)
4770 exec_list
*ir
= prog
->_LinkedShaders
[i
]->ir
;
4771 const struct gl_shader_compiler_options
*options
=
4772 &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(prog
->_LinkedShaders
[i
]->Type
)];
4778 do_mat_op_to_vec(ir
);
4779 lower_instructions(ir
, (MOD_TO_FRACT
| DIV_TO_MUL_RCP
| EXP_TO_EXP2
4781 | ((options
->EmitNoPow
) ? POW_TO_EXP2
: 0)));
4783 progress
= do_lower_jumps(ir
, true, true, options
->EmitNoMainReturn
, options
->EmitNoCont
, options
->EmitNoLoops
) || progress
;
4785 progress
= do_common_optimization(ir
, true, options
->MaxUnrollIterations
) || progress
;
4787 progress
= lower_quadop_vector(ir
, true) || progress
;
4789 if (options
->EmitNoIfs
) {
4790 progress
= lower_discard(ir
) || progress
;
4791 progress
= lower_if_to_cond_assign(ir
) || progress
;
4794 if (options
->EmitNoNoise
)
4795 progress
= lower_noise(ir
) || progress
;
4797 /* If there are forms of indirect addressing that the driver
4798 * cannot handle, perform the lowering pass.
4800 if (options
->EmitNoIndirectInput
|| options
->EmitNoIndirectOutput
4801 || options
->EmitNoIndirectTemp
|| options
->EmitNoIndirectUniform
)
4803 lower_variable_index_to_cond_assign(ir
,
4804 options
->EmitNoIndirectInput
,
4805 options
->EmitNoIndirectOutput
,
4806 options
->EmitNoIndirectTemp
,
4807 options
->EmitNoIndirectUniform
)
4810 progress
= do_vec_index_to_cond_assign(ir
) || progress
;
4813 validate_ir_tree(ir
);
4816 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
4817 struct gl_program
*linked_prog
;
4819 if (prog
->_LinkedShaders
[i
] == NULL
)
4822 linked_prog
= get_mesa_program(ctx
, prog
, prog
->_LinkedShaders
[i
]);
4827 switch (prog
->_LinkedShaders
[i
]->Type
) {
4828 case GL_VERTEX_SHADER
:
4829 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
,
4830 (struct gl_vertex_program
*)linked_prog
);
4831 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_VERTEX_PROGRAM_ARB
,
4834 case GL_FRAGMENT_SHADER
:
4835 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
,
4836 (struct gl_fragment_program
*)linked_prog
);
4837 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
4840 case GL_GEOMETRY_SHADER
:
4841 _mesa_reference_geomprog(ctx
, &prog
->GeometryProgram
,
4842 (struct gl_geometry_program
*)linked_prog
);
4843 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_GEOMETRY_PROGRAM_NV
,
4852 _mesa_reference_program(ctx
, &linked_prog
, NULL
);
4860 * Link a GLSL shader program. Called via glLinkProgram().
4863 st_glsl_link_shader(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
4867 _mesa_clear_shader_program_data(ctx
, prog
);
4869 prog
->LinkStatus
= GL_TRUE
;
4871 for (i
= 0; i
< prog
->NumShaders
; i
++) {
4872 if (!prog
->Shaders
[i
]->CompileStatus
) {
4873 fail_link(prog
, "linking with uncompiled shader");
4874 prog
->LinkStatus
= GL_FALSE
;
4878 prog
->Varying
= _mesa_new_parameter_list();
4879 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
, NULL
);
4880 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
, NULL
);
4881 _mesa_reference_geomprog(ctx
, &prog
->GeometryProgram
, NULL
);
4883 if (prog
->LinkStatus
) {
4884 link_shaders(ctx
, prog
);
4887 if (prog
->LinkStatus
) {
4888 if (!ctx
->Driver
.LinkShader(ctx
, prog
)) {
4889 prog
->LinkStatus
= GL_FALSE
;
4893 set_uniform_initializers(ctx
, prog
);
4895 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
4896 if (!prog
->LinkStatus
) {
4897 printf("GLSL shader program %d failed to link\n", prog
->Name
);
4900 if (prog
->InfoLog
&& prog
->InfoLog
[0] != 0) {
4901 printf("GLSL shader program %d info log:\n", prog
->Name
);
4902 printf("%s\n", prog
->InfoLog
);