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
83 /* will be 4 for GLSL 4.00 */
84 #define MAX_GLSL_TEXTURE_OFFSET 1
89 static int swizzle_for_size(int size
);
92 * This struct is a corresponding struct to TGSI ureg_src.
96 st_src_reg(gl_register_file file
, int index
, const glsl_type
*type
)
100 if (type
&& (type
->is_scalar() || type
->is_vector() || type
->is_matrix()))
101 this->swizzle
= swizzle_for_size(type
->vector_elements
);
103 this->swizzle
= SWIZZLE_XYZW
;
105 this->type
= type
? type
->base_type
: GLSL_TYPE_ERROR
;
106 this->reladdr
= NULL
;
109 st_src_reg(gl_register_file file
, int index
, int type
)
114 this->swizzle
= SWIZZLE_XYZW
;
116 this->reladdr
= NULL
;
121 this->type
= GLSL_TYPE_ERROR
;
122 this->file
= PROGRAM_UNDEFINED
;
126 this->reladdr
= NULL
;
129 explicit st_src_reg(st_dst_reg reg
);
131 gl_register_file file
; /**< PROGRAM_* from Mesa */
132 int index
; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
133 GLuint swizzle
; /**< SWIZZLE_XYZWONEZERO swizzles from Mesa. */
134 int negate
; /**< NEGATE_XYZW mask from mesa */
135 int type
; /** GLSL_TYPE_* from GLSL IR (enum glsl_base_type) */
136 /** Register index should be offset by the integer in this reg. */
142 st_dst_reg(gl_register_file file
, int writemask
, int type
)
146 this->writemask
= writemask
;
147 this->cond_mask
= COND_TR
;
148 this->reladdr
= NULL
;
154 this->type
= GLSL_TYPE_ERROR
;
155 this->file
= PROGRAM_UNDEFINED
;
158 this->cond_mask
= COND_TR
;
159 this->reladdr
= NULL
;
162 explicit st_dst_reg(st_src_reg reg
);
164 gl_register_file file
; /**< PROGRAM_* from Mesa */
165 int index
; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
166 int writemask
; /**< Bitfield of WRITEMASK_[XYZW] */
168 int type
; /** GLSL_TYPE_* from GLSL IR (enum glsl_base_type) */
169 /** Register index should be offset by the integer in this reg. */
173 st_src_reg::st_src_reg(st_dst_reg reg
)
175 this->type
= reg
.type
;
176 this->file
= reg
.file
;
177 this->index
= reg
.index
;
178 this->swizzle
= SWIZZLE_XYZW
;
180 this->reladdr
= reg
.reladdr
;
183 st_dst_reg::st_dst_reg(st_src_reg reg
)
185 this->type
= reg
.type
;
186 this->file
= reg
.file
;
187 this->index
= reg
.index
;
188 this->writemask
= WRITEMASK_XYZW
;
189 this->cond_mask
= COND_TR
;
190 this->reladdr
= reg
.reladdr
;
193 class glsl_to_tgsi_instruction
: public exec_node
{
195 /* Callers of this ralloc-based new need not call delete. It's
196 * easier to just ralloc_free 'ctx' (or any of its ancestors). */
197 static void* operator new(size_t size
, void *ctx
)
201 node
= rzalloc_size(ctx
, size
);
202 assert(node
!= NULL
);
210 /** Pointer to the ir source this tree came from for debugging */
212 GLboolean cond_update
;
214 int sampler
; /**< sampler index */
215 int tex_target
; /**< One of TEXTURE_*_INDEX */
216 GLboolean tex_shadow
;
217 struct tgsi_texture_offset tex_offsets
[MAX_GLSL_TEXTURE_OFFSET
];
218 unsigned tex_offset_num_offset
;
219 int dead_mask
; /**< Used in dead code elimination */
221 class function_entry
*function
; /* Set on TGSI_OPCODE_CAL or TGSI_OPCODE_BGNSUB */
224 class variable_storage
: public exec_node
{
226 variable_storage(ir_variable
*var
, gl_register_file file
, int index
)
227 : file(file
), index(index
), var(var
)
232 gl_register_file file
;
234 ir_variable
*var
; /* variable that maps to this, if any */
237 class immediate_storage
: public exec_node
{
239 immediate_storage(gl_constant_value
*values
, int size
, int type
)
241 memcpy(this->values
, values
, size
* sizeof(gl_constant_value
));
246 gl_constant_value values
[4];
247 int size
; /**< Number of components (1-4) */
248 int type
; /**< GL_FLOAT, GL_INT, GL_BOOL, or GL_UNSIGNED_INT */
251 class function_entry
: public exec_node
{
253 ir_function_signature
*sig
;
256 * identifier of this function signature used by the program.
258 * At the point that TGSI instructions for function calls are
259 * generated, we don't know the address of the first instruction of
260 * the function body. So we make the BranchTarget that is called a
261 * small integer and rewrite them during set_branchtargets().
266 * Pointer to first instruction of the function body.
268 * Set during function body emits after main() is processed.
270 glsl_to_tgsi_instruction
*bgn_inst
;
273 * Index of the first instruction of the function body in actual TGSI.
275 * Set after conversion from glsl_to_tgsi_instruction to TGSI.
279 /** Storage for the return value. */
280 st_src_reg return_reg
;
283 class glsl_to_tgsi_visitor
: public ir_visitor
{
285 glsl_to_tgsi_visitor();
286 ~glsl_to_tgsi_visitor();
288 function_entry
*current_function
;
290 struct gl_context
*ctx
;
291 struct gl_program
*prog
;
292 struct gl_shader_program
*shader_program
;
293 struct gl_shader_compiler_options
*options
;
297 int num_address_regs
;
299 bool indirect_addr_temps
;
300 bool indirect_addr_consts
;
303 bool native_integers
;
305 variable_storage
*find_variable_storage(ir_variable
*var
);
307 int add_constant(gl_register_file file
, gl_constant_value values
[4],
308 int size
, int datatype
, GLuint
*swizzle_out
);
310 function_entry
*get_function_signature(ir_function_signature
*sig
);
312 st_src_reg
get_temp(const glsl_type
*type
);
313 void reladdr_to_temp(ir_instruction
*ir
, st_src_reg
*reg
, int *num_reladdr
);
315 st_src_reg
st_src_reg_for_float(float val
);
316 st_src_reg
st_src_reg_for_int(int val
);
317 st_src_reg
st_src_reg_for_type(int type
, int val
);
320 * \name Visit methods
322 * As typical for the visitor pattern, there must be one \c visit method for
323 * each concrete subclass of \c ir_instruction. Virtual base classes within
324 * the hierarchy should not have \c visit methods.
327 virtual void visit(ir_variable
*);
328 virtual void visit(ir_loop
*);
329 virtual void visit(ir_loop_jump
*);
330 virtual void visit(ir_function_signature
*);
331 virtual void visit(ir_function
*);
332 virtual void visit(ir_expression
*);
333 virtual void visit(ir_swizzle
*);
334 virtual void visit(ir_dereference_variable
*);
335 virtual void visit(ir_dereference_array
*);
336 virtual void visit(ir_dereference_record
*);
337 virtual void visit(ir_assignment
*);
338 virtual void visit(ir_constant
*);
339 virtual void visit(ir_call
*);
340 virtual void visit(ir_return
*);
341 virtual void visit(ir_discard
*);
342 virtual void visit(ir_texture
*);
343 virtual void visit(ir_if
*);
348 /** List of variable_storage */
351 /** List of immediate_storage */
352 exec_list immediates
;
355 /** List of function_entry */
356 exec_list function_signatures
;
357 int next_signature_id
;
359 /** List of glsl_to_tgsi_instruction */
360 exec_list instructions
;
362 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
);
364 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
,
365 st_dst_reg dst
, st_src_reg src0
);
367 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
,
368 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
);
370 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
,
372 st_src_reg src0
, st_src_reg src1
, st_src_reg src2
);
374 unsigned get_opcode(ir_instruction
*ir
, unsigned op
,
376 st_src_reg src0
, st_src_reg src1
);
379 * Emit the correct dot-product instruction for the type of arguments
381 glsl_to_tgsi_instruction
*emit_dp(ir_instruction
*ir
,
387 void emit_scalar(ir_instruction
*ir
, unsigned op
,
388 st_dst_reg dst
, st_src_reg src0
);
390 void emit_scalar(ir_instruction
*ir
, unsigned op
,
391 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
);
393 void try_emit_float_set(ir_instruction
*ir
, unsigned op
, st_dst_reg dst
);
395 void emit_arl(ir_instruction
*ir
, st_dst_reg dst
, st_src_reg src0
);
397 void emit_scs(ir_instruction
*ir
, unsigned op
,
398 st_dst_reg dst
, const st_src_reg
&src
);
400 bool try_emit_mad(ir_expression
*ir
,
402 bool try_emit_mad_for_and_not(ir_expression
*ir
,
404 bool try_emit_sat(ir_expression
*ir
);
406 void emit_swz(ir_expression
*ir
);
408 bool process_move_condition(ir_rvalue
*ir
);
410 void remove_output_reads(gl_register_file type
);
411 void simplify_cmp(void);
413 void rename_temp_register(int index
, int new_index
);
414 int get_first_temp_read(int index
);
415 int get_first_temp_write(int index
);
416 int get_last_temp_read(int index
);
417 int get_last_temp_write(int index
);
419 void copy_propagate(void);
420 void eliminate_dead_code(void);
421 int eliminate_dead_code_advanced(void);
422 void merge_registers(void);
423 void renumber_registers(void);
428 static st_src_reg undef_src
= st_src_reg(PROGRAM_UNDEFINED
, 0, GLSL_TYPE_ERROR
);
430 static st_dst_reg undef_dst
= st_dst_reg(PROGRAM_UNDEFINED
, SWIZZLE_NOOP
, GLSL_TYPE_ERROR
);
432 static st_dst_reg address_reg
= st_dst_reg(PROGRAM_ADDRESS
, WRITEMASK_X
, GLSL_TYPE_FLOAT
);
435 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...) PRINTFLIKE(2, 3);
438 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...)
442 ralloc_vasprintf_append(&prog
->InfoLog
, fmt
, args
);
445 prog
->LinkStatus
= GL_FALSE
;
449 swizzle_for_size(int size
)
451 int size_swizzles
[4] = {
452 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
),
453 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
),
454 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
),
455 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
458 assert((size
>= 1) && (size
<= 4));
459 return size_swizzles
[size
- 1];
463 is_tex_instruction(unsigned opcode
)
465 const tgsi_opcode_info
* info
= tgsi_get_opcode_info(opcode
);
470 num_inst_dst_regs(unsigned opcode
)
472 const tgsi_opcode_info
* info
= tgsi_get_opcode_info(opcode
);
473 return info
->num_dst
;
477 num_inst_src_regs(unsigned opcode
)
479 const tgsi_opcode_info
* info
= tgsi_get_opcode_info(opcode
);
480 return info
->is_tex
? info
->num_src
- 1 : info
->num_src
;
483 glsl_to_tgsi_instruction
*
484 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
,
486 st_src_reg src0
, st_src_reg src1
, st_src_reg src2
)
488 glsl_to_tgsi_instruction
*inst
= new(mem_ctx
) glsl_to_tgsi_instruction();
489 int num_reladdr
= 0, i
;
491 op
= get_opcode(ir
, op
, dst
, src0
, src1
);
493 /* If we have to do relative addressing, we want to load the ARL
494 * reg directly for one of the regs, and preload the other reladdr
495 * sources into temps.
497 num_reladdr
+= dst
.reladdr
!= NULL
;
498 num_reladdr
+= src0
.reladdr
!= NULL
;
499 num_reladdr
+= src1
.reladdr
!= NULL
;
500 num_reladdr
+= src2
.reladdr
!= NULL
;
502 reladdr_to_temp(ir
, &src2
, &num_reladdr
);
503 reladdr_to_temp(ir
, &src1
, &num_reladdr
);
504 reladdr_to_temp(ir
, &src0
, &num_reladdr
);
507 emit_arl(ir
, address_reg
, *dst
.reladdr
);
510 assert(num_reladdr
== 0);
520 inst
->function
= NULL
;
522 if (op
== TGSI_OPCODE_ARL
|| op
== TGSI_OPCODE_UARL
)
523 this->num_address_regs
= 1;
525 /* Update indirect addressing status used by TGSI */
528 case PROGRAM_TEMPORARY
:
529 this->indirect_addr_temps
= true;
531 case PROGRAM_LOCAL_PARAM
:
532 case PROGRAM_ENV_PARAM
:
533 case PROGRAM_STATE_VAR
:
534 case PROGRAM_NAMED_PARAM
:
535 case PROGRAM_CONSTANT
:
536 case PROGRAM_UNIFORM
:
537 this->indirect_addr_consts
= true;
539 case PROGRAM_IMMEDIATE
:
540 assert(!"immediates should not have indirect addressing");
547 for (i
=0; i
<3; i
++) {
548 if(inst
->src
[i
].reladdr
) {
549 switch(inst
->src
[i
].file
) {
550 case PROGRAM_TEMPORARY
:
551 this->indirect_addr_temps
= true;
553 case PROGRAM_LOCAL_PARAM
:
554 case PROGRAM_ENV_PARAM
:
555 case PROGRAM_STATE_VAR
:
556 case PROGRAM_NAMED_PARAM
:
557 case PROGRAM_CONSTANT
:
558 case PROGRAM_UNIFORM
:
559 this->indirect_addr_consts
= true;
561 case PROGRAM_IMMEDIATE
:
562 assert(!"immediates should not have indirect addressing");
571 this->instructions
.push_tail(inst
);
574 try_emit_float_set(ir
, op
, dst
);
580 glsl_to_tgsi_instruction
*
581 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
,
582 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
)
584 return emit(ir
, op
, dst
, src0
, src1
, undef_src
);
587 glsl_to_tgsi_instruction
*
588 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
,
589 st_dst_reg dst
, st_src_reg src0
)
591 assert(dst
.writemask
!= 0);
592 return emit(ir
, op
, dst
, src0
, undef_src
, undef_src
);
595 glsl_to_tgsi_instruction
*
596 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
)
598 return emit(ir
, op
, undef_dst
, undef_src
, undef_src
, undef_src
);
602 * Emits the code to convert the result of float SET instructions to integers.
605 glsl_to_tgsi_visitor::try_emit_float_set(ir_instruction
*ir
, unsigned op
,
608 if ((op
== TGSI_OPCODE_SEQ
||
609 op
== TGSI_OPCODE_SNE
||
610 op
== TGSI_OPCODE_SGE
||
611 op
== TGSI_OPCODE_SLT
))
613 st_src_reg src
= st_src_reg(dst
);
614 src
.negate
= ~src
.negate
;
615 dst
.type
= GLSL_TYPE_FLOAT
;
616 emit(ir
, TGSI_OPCODE_F2I
, dst
, src
);
621 * Determines whether to use an integer, unsigned integer, or float opcode
622 * based on the operands and input opcode, then emits the result.
625 glsl_to_tgsi_visitor::get_opcode(ir_instruction
*ir
, unsigned op
,
627 st_src_reg src0
, st_src_reg src1
)
629 int type
= GLSL_TYPE_FLOAT
;
631 if (src0
.type
== GLSL_TYPE_FLOAT
|| src1
.type
== GLSL_TYPE_FLOAT
)
632 type
= GLSL_TYPE_FLOAT
;
633 else if (native_integers
)
634 type
= src0
.type
== GLSL_TYPE_BOOL
? GLSL_TYPE_INT
: src0
.type
;
636 #define case4(c, f, i, u) \
637 case TGSI_OPCODE_##c: \
638 if (type == GLSL_TYPE_INT) op = TGSI_OPCODE_##i; \
639 else if (type == GLSL_TYPE_UINT) op = TGSI_OPCODE_##u; \
640 else op = TGSI_OPCODE_##f; \
642 #define case3(f, i, u) case4(f, f, i, u)
643 #define case2fi(f, i) case4(f, f, i, i)
644 #define case2iu(i, u) case4(i, LAST, i, u)
650 case3(DIV
, IDIV
, UDIV
);
651 case3(MAX
, IMAX
, UMAX
);
652 case3(MIN
, IMIN
, UMIN
);
657 case3(SGE
, ISGE
, USGE
);
658 case3(SLT
, ISLT
, USLT
);
665 assert(op
!= TGSI_OPCODE_LAST
);
669 glsl_to_tgsi_instruction
*
670 glsl_to_tgsi_visitor::emit_dp(ir_instruction
*ir
,
671 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
,
674 static const unsigned dot_opcodes
[] = {
675 TGSI_OPCODE_DP2
, TGSI_OPCODE_DP3
, TGSI_OPCODE_DP4
678 return emit(ir
, dot_opcodes
[elements
- 2], dst
, src0
, src1
);
682 * Emits TGSI scalar opcodes to produce unique answers across channels.
684 * Some TGSI opcodes are scalar-only, like ARB_fp/vp. The src X
685 * channel determines the result across all channels. So to do a vec4
686 * of this operation, we want to emit a scalar per source channel used
687 * to produce dest channels.
690 glsl_to_tgsi_visitor::emit_scalar(ir_instruction
*ir
, unsigned op
,
692 st_src_reg orig_src0
, st_src_reg orig_src1
)
695 int done_mask
= ~dst
.writemask
;
697 /* TGSI RCP is a scalar operation splatting results to all channels,
698 * like ARB_fp/vp. So emit as many RCPs as necessary to cover our
701 for (i
= 0; i
< 4; i
++) {
702 GLuint this_mask
= (1 << i
);
703 glsl_to_tgsi_instruction
*inst
;
704 st_src_reg src0
= orig_src0
;
705 st_src_reg src1
= orig_src1
;
707 if (done_mask
& this_mask
)
710 GLuint src0_swiz
= GET_SWZ(src0
.swizzle
, i
);
711 GLuint src1_swiz
= GET_SWZ(src1
.swizzle
, i
);
712 for (j
= i
+ 1; j
< 4; j
++) {
713 /* If there is another enabled component in the destination that is
714 * derived from the same inputs, generate its value on this pass as
717 if (!(done_mask
& (1 << j
)) &&
718 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
&&
719 GET_SWZ(src1
.swizzle
, j
) == src1_swiz
) {
720 this_mask
|= (1 << j
);
723 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
724 src0_swiz
, src0_swiz
);
725 src1
.swizzle
= MAKE_SWIZZLE4(src1_swiz
, src1_swiz
,
726 src1_swiz
, src1_swiz
);
728 inst
= emit(ir
, op
, dst
, src0
, src1
);
729 inst
->dst
.writemask
= this_mask
;
730 done_mask
|= this_mask
;
735 glsl_to_tgsi_visitor::emit_scalar(ir_instruction
*ir
, unsigned op
,
736 st_dst_reg dst
, st_src_reg src0
)
738 st_src_reg undef
= undef_src
;
740 undef
.swizzle
= SWIZZLE_XXXX
;
742 emit_scalar(ir
, op
, dst
, src0
, undef
);
746 glsl_to_tgsi_visitor::emit_arl(ir_instruction
*ir
,
747 st_dst_reg dst
, st_src_reg src0
)
749 int op
= TGSI_OPCODE_ARL
;
751 if (src0
.type
== GLSL_TYPE_INT
|| src0
.type
== GLSL_TYPE_UINT
)
752 op
= TGSI_OPCODE_UARL
;
754 emit(NULL
, op
, dst
, src0
);
758 * Emit an TGSI_OPCODE_SCS instruction
760 * The \c SCS opcode functions a bit differently than the other TGSI opcodes.
761 * Instead of splatting its result across all four components of the
762 * destination, it writes one value to the \c x component and another value to
763 * the \c y component.
765 * \param ir IR instruction being processed
766 * \param op Either \c TGSI_OPCODE_SIN or \c TGSI_OPCODE_COS depending
767 * on which value is desired.
768 * \param dst Destination register
769 * \param src Source register
772 glsl_to_tgsi_visitor::emit_scs(ir_instruction
*ir
, unsigned op
,
774 const st_src_reg
&src
)
776 /* Vertex programs cannot use the SCS opcode.
778 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
779 emit_scalar(ir
, op
, dst
, src
);
783 const unsigned component
= (op
== TGSI_OPCODE_SIN
) ? 0 : 1;
784 const unsigned scs_mask
= (1U << component
);
785 int done_mask
= ~dst
.writemask
;
788 assert(op
== TGSI_OPCODE_SIN
|| op
== TGSI_OPCODE_COS
);
790 /* If there are compnents in the destination that differ from the component
791 * that will be written by the SCS instrution, we'll need a temporary.
793 if (scs_mask
!= unsigned(dst
.writemask
)) {
794 tmp
= get_temp(glsl_type::vec4_type
);
797 for (unsigned i
= 0; i
< 4; i
++) {
798 unsigned this_mask
= (1U << i
);
799 st_src_reg src0
= src
;
801 if ((done_mask
& this_mask
) != 0)
804 /* The source swizzle specified which component of the source generates
805 * sine / cosine for the current component in the destination. The SCS
806 * instruction requires that this value be swizzle to the X component.
807 * Replace the current swizzle with a swizzle that puts the source in
810 unsigned src0_swiz
= GET_SWZ(src
.swizzle
, i
);
812 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
813 src0_swiz
, src0_swiz
);
814 for (unsigned j
= i
+ 1; j
< 4; j
++) {
815 /* If there is another enabled component in the destination that is
816 * derived from the same inputs, generate its value on this pass as
819 if (!(done_mask
& (1 << j
)) &&
820 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
) {
821 this_mask
|= (1 << j
);
825 if (this_mask
!= scs_mask
) {
826 glsl_to_tgsi_instruction
*inst
;
827 st_dst_reg tmp_dst
= st_dst_reg(tmp
);
829 /* Emit the SCS instruction.
831 inst
= emit(ir
, TGSI_OPCODE_SCS
, tmp_dst
, src0
);
832 inst
->dst
.writemask
= scs_mask
;
834 /* Move the result of the SCS instruction to the desired location in
837 tmp
.swizzle
= MAKE_SWIZZLE4(component
, component
,
838 component
, component
);
839 inst
= emit(ir
, TGSI_OPCODE_SCS
, dst
, tmp
);
840 inst
->dst
.writemask
= this_mask
;
842 /* Emit the SCS instruction to write directly to the destination.
844 glsl_to_tgsi_instruction
*inst
= emit(ir
, TGSI_OPCODE_SCS
, dst
, src0
);
845 inst
->dst
.writemask
= scs_mask
;
848 done_mask
|= this_mask
;
853 glsl_to_tgsi_visitor::add_constant(gl_register_file file
,
854 gl_constant_value values
[4], int size
, int datatype
,
857 if (file
== PROGRAM_CONSTANT
) {
858 return _mesa_add_typed_unnamed_constant(this->prog
->Parameters
, values
,
859 size
, datatype
, swizzle_out
);
862 immediate_storage
*entry
;
863 assert(file
== PROGRAM_IMMEDIATE
);
865 /* Search immediate storage to see if we already have an identical
866 * immediate that we can use instead of adding a duplicate entry.
868 foreach_iter(exec_list_iterator
, iter
, this->immediates
) {
869 entry
= (immediate_storage
*)iter
.get();
871 if (entry
->size
== size
&&
872 entry
->type
== datatype
&&
873 !memcmp(entry
->values
, values
, size
* sizeof(gl_constant_value
))) {
879 /* Add this immediate to the list. */
880 entry
= new(mem_ctx
) immediate_storage(values
, size
, datatype
);
881 this->immediates
.push_tail(entry
);
882 this->num_immediates
++;
888 glsl_to_tgsi_visitor::st_src_reg_for_float(float val
)
890 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_FLOAT
);
891 union gl_constant_value uval
;
894 src
.index
= add_constant(src
.file
, &uval
, 1, GL_FLOAT
, &src
.swizzle
);
900 glsl_to_tgsi_visitor::st_src_reg_for_int(int val
)
902 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_INT
);
903 union gl_constant_value uval
;
905 assert(native_integers
);
908 src
.index
= add_constant(src
.file
, &uval
, 1, GL_INT
, &src
.swizzle
);
914 glsl_to_tgsi_visitor::st_src_reg_for_type(int type
, int val
)
917 return type
== GLSL_TYPE_FLOAT
? st_src_reg_for_float(val
) :
918 st_src_reg_for_int(val
);
920 return st_src_reg_for_float(val
);
924 type_size(const struct glsl_type
*type
)
929 switch (type
->base_type
) {
932 case GLSL_TYPE_FLOAT
:
934 if (type
->is_matrix()) {
935 return type
->matrix_columns
;
937 /* Regardless of size of vector, it gets a vec4. This is bad
938 * packing for things like floats, but otherwise arrays become a
939 * mess. Hopefully a later pass over the code can pack scalars
940 * down if appropriate.
944 case GLSL_TYPE_ARRAY
:
945 assert(type
->length
> 0);
946 return type_size(type
->fields
.array
) * type
->length
;
947 case GLSL_TYPE_STRUCT
:
949 for (i
= 0; i
< type
->length
; i
++) {
950 size
+= type_size(type
->fields
.structure
[i
].type
);
953 case GLSL_TYPE_SAMPLER
:
954 /* Samplers take up one slot in UNIFORMS[], but they're baked in
965 * In the initial pass of codegen, we assign temporary numbers to
966 * intermediate results. (not SSA -- variable assignments will reuse
970 glsl_to_tgsi_visitor::get_temp(const glsl_type
*type
)
974 src
.type
= native_integers
? type
->base_type
: GLSL_TYPE_FLOAT
;
975 src
.file
= PROGRAM_TEMPORARY
;
976 src
.index
= next_temp
;
978 next_temp
+= type_size(type
);
980 if (type
->is_array() || type
->is_record()) {
981 src
.swizzle
= SWIZZLE_NOOP
;
983 src
.swizzle
= swizzle_for_size(type
->vector_elements
);
991 glsl_to_tgsi_visitor::find_variable_storage(ir_variable
*var
)
994 variable_storage
*entry
;
996 foreach_iter(exec_list_iterator
, iter
, this->variables
) {
997 entry
= (variable_storage
*)iter
.get();
999 if (entry
->var
== var
)
1007 glsl_to_tgsi_visitor::visit(ir_variable
*ir
)
1009 if (strcmp(ir
->name
, "gl_FragCoord") == 0) {
1010 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
1012 fp
->OriginUpperLeft
= ir
->origin_upper_left
;
1013 fp
->PixelCenterInteger
= ir
->pixel_center_integer
;
1015 } else if (strcmp(ir
->name
, "gl_FragDepth") == 0) {
1016 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
1017 switch (ir
->depth_layout
) {
1018 case ir_depth_layout_none
:
1019 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_NONE
;
1021 case ir_depth_layout_any
:
1022 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_ANY
;
1024 case ir_depth_layout_greater
:
1025 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_GREATER
;
1027 case ir_depth_layout_less
:
1028 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_LESS
;
1030 case ir_depth_layout_unchanged
:
1031 fp
->FragDepthLayout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
1039 if (ir
->mode
== ir_var_uniform
&& strncmp(ir
->name
, "gl_", 3) == 0) {
1041 const ir_state_slot
*const slots
= ir
->state_slots
;
1042 assert(ir
->state_slots
!= NULL
);
1044 /* Check if this statevar's setup in the STATE file exactly
1045 * matches how we'll want to reference it as a
1046 * struct/array/whatever. If not, then we need to move it into
1047 * temporary storage and hope that it'll get copy-propagated
1050 for (i
= 0; i
< ir
->num_state_slots
; i
++) {
1051 if (slots
[i
].swizzle
!= SWIZZLE_XYZW
) {
1056 variable_storage
*storage
;
1058 if (i
== ir
->num_state_slots
) {
1059 /* We'll set the index later. */
1060 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_STATE_VAR
, -1);
1061 this->variables
.push_tail(storage
);
1065 /* The variable_storage constructor allocates slots based on the size
1066 * of the type. However, this had better match the number of state
1067 * elements that we're going to copy into the new temporary.
1069 assert((int) ir
->num_state_slots
== type_size(ir
->type
));
1071 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_TEMPORARY
,
1073 this->variables
.push_tail(storage
);
1074 this->next_temp
+= type_size(ir
->type
);
1076 dst
= st_dst_reg(st_src_reg(PROGRAM_TEMPORARY
, storage
->index
,
1077 native_integers
? ir
->type
->base_type
: GLSL_TYPE_FLOAT
));
1081 for (unsigned int i
= 0; i
< ir
->num_state_slots
; i
++) {
1082 int index
= _mesa_add_state_reference(this->prog
->Parameters
,
1083 (gl_state_index
*)slots
[i
].tokens
);
1085 if (storage
->file
== PROGRAM_STATE_VAR
) {
1086 if (storage
->index
== -1) {
1087 storage
->index
= index
;
1089 assert(index
== storage
->index
+ (int)i
);
1092 st_src_reg
src(PROGRAM_STATE_VAR
, index
,
1093 native_integers
? ir
->type
->base_type
: GLSL_TYPE_FLOAT
);
1094 src
.swizzle
= slots
[i
].swizzle
;
1095 emit(ir
, TGSI_OPCODE_MOV
, dst
, src
);
1096 /* even a float takes up a whole vec4 reg in a struct/array. */
1101 if (storage
->file
== PROGRAM_TEMPORARY
&&
1102 dst
.index
!= storage
->index
+ (int) ir
->num_state_slots
) {
1103 fail_link(this->shader_program
,
1104 "failed to load builtin uniform `%s' (%d/%d regs loaded)\n",
1105 ir
->name
, dst
.index
- storage
->index
,
1106 type_size(ir
->type
));
1112 glsl_to_tgsi_visitor::visit(ir_loop
*ir
)
1114 ir_dereference_variable
*counter
= NULL
;
1116 if (ir
->counter
!= NULL
)
1117 counter
= new(ir
) ir_dereference_variable(ir
->counter
);
1119 if (ir
->from
!= NULL
) {
1120 assert(ir
->counter
!= NULL
);
1122 ir_assignment
*a
= new(ir
) ir_assignment(counter
, ir
->from
, NULL
);
1128 emit(NULL
, TGSI_OPCODE_BGNLOOP
);
1132 new(ir
) ir_expression(ir
->cmp
, glsl_type::bool_type
,
1134 ir_if
*if_stmt
= new(ir
) ir_if(e
);
1136 ir_loop_jump
*brk
= new(ir
) ir_loop_jump(ir_loop_jump::jump_break
);
1138 if_stmt
->then_instructions
.push_tail(brk
);
1140 if_stmt
->accept(this);
1147 visit_exec_list(&ir
->body_instructions
, this);
1149 if (ir
->increment
) {
1151 new(ir
) ir_expression(ir_binop_add
, counter
->type
,
1152 counter
, ir
->increment
);
1154 ir_assignment
*a
= new(ir
) ir_assignment(counter
, e
, NULL
);
1161 emit(NULL
, TGSI_OPCODE_ENDLOOP
);
1165 glsl_to_tgsi_visitor::visit(ir_loop_jump
*ir
)
1168 case ir_loop_jump::jump_break
:
1169 emit(NULL
, TGSI_OPCODE_BRK
);
1171 case ir_loop_jump::jump_continue
:
1172 emit(NULL
, TGSI_OPCODE_CONT
);
1179 glsl_to_tgsi_visitor::visit(ir_function_signature
*ir
)
1186 glsl_to_tgsi_visitor::visit(ir_function
*ir
)
1188 /* Ignore function bodies other than main() -- we shouldn't see calls to
1189 * them since they should all be inlined before we get to glsl_to_tgsi.
1191 if (strcmp(ir
->name
, "main") == 0) {
1192 const ir_function_signature
*sig
;
1195 sig
= ir
->matching_signature(&empty
);
1199 foreach_iter(exec_list_iterator
, iter
, sig
->body
) {
1200 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
1208 glsl_to_tgsi_visitor::try_emit_mad(ir_expression
*ir
, int mul_operand
)
1210 int nonmul_operand
= 1 - mul_operand
;
1212 st_dst_reg result_dst
;
1214 ir_expression
*expr
= ir
->operands
[mul_operand
]->as_expression();
1215 if (!expr
|| expr
->operation
!= ir_binop_mul
)
1218 expr
->operands
[0]->accept(this);
1220 expr
->operands
[1]->accept(this);
1222 ir
->operands
[nonmul_operand
]->accept(this);
1225 this->result
= get_temp(ir
->type
);
1226 result_dst
= st_dst_reg(this->result
);
1227 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1228 emit(ir
, TGSI_OPCODE_MAD
, result_dst
, a
, b
, c
);
1234 * Emit MAD(a, -b, a) instead of AND(a, NOT(b))
1236 * The logic values are 1.0 for true and 0.0 for false. Logical-and is
1237 * implemented using multiplication, and logical-or is implemented using
1238 * addition. Logical-not can be implemented as (true - x), or (1.0 - x).
1239 * As result, the logical expression (a & !b) can be rewritten as:
1243 * - (a * 1) - (a * b)
1247 * This final expression can be implemented as a single MAD(a, -b, a)
1251 glsl_to_tgsi_visitor::try_emit_mad_for_and_not(ir_expression
*ir
, int try_operand
)
1253 const int other_operand
= 1 - try_operand
;
1256 ir_expression
*expr
= ir
->operands
[try_operand
]->as_expression();
1257 if (!expr
|| expr
->operation
!= ir_unop_logic_not
)
1260 ir
->operands
[other_operand
]->accept(this);
1262 expr
->operands
[0]->accept(this);
1265 b
.negate
= ~b
.negate
;
1267 this->result
= get_temp(ir
->type
);
1268 emit(ir
, TGSI_OPCODE_MAD
, st_dst_reg(this->result
), a
, b
, a
);
1274 glsl_to_tgsi_visitor::try_emit_sat(ir_expression
*ir
)
1276 /* Saturates were only introduced to vertex programs in
1277 * NV_vertex_program3, so don't give them to drivers in the VP.
1279 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
)
1282 ir_rvalue
*sat_src
= ir
->as_rvalue_to_saturate();
1286 sat_src
->accept(this);
1287 st_src_reg src
= this->result
;
1289 /* If we generated an expression instruction into a temporary in
1290 * processing the saturate's operand, apply the saturate to that
1291 * instruction. Otherwise, generate a MOV to do the saturate.
1293 * Note that we have to be careful to only do this optimization if
1294 * the instruction in question was what generated src->result. For
1295 * example, ir_dereference_array might generate a MUL instruction
1296 * to create the reladdr, and return us a src reg using that
1297 * reladdr. That MUL result is not the value we're trying to
1300 ir_expression
*sat_src_expr
= sat_src
->as_expression();
1301 if (sat_src_expr
&& (sat_src_expr
->operation
== ir_binop_mul
||
1302 sat_src_expr
->operation
== ir_binop_add
||
1303 sat_src_expr
->operation
== ir_binop_dot
)) {
1304 glsl_to_tgsi_instruction
*new_inst
;
1305 new_inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
1306 new_inst
->saturate
= true;
1308 this->result
= get_temp(ir
->type
);
1309 st_dst_reg result_dst
= st_dst_reg(this->result
);
1310 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1311 glsl_to_tgsi_instruction
*inst
;
1312 inst
= emit(ir
, TGSI_OPCODE_MOV
, result_dst
, src
);
1313 inst
->saturate
= true;
1320 glsl_to_tgsi_visitor::reladdr_to_temp(ir_instruction
*ir
,
1321 st_src_reg
*reg
, int *num_reladdr
)
1326 emit_arl(ir
, address_reg
, *reg
->reladdr
);
1328 if (*num_reladdr
!= 1) {
1329 st_src_reg temp
= get_temp(glsl_type::vec4_type
);
1331 emit(ir
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), *reg
);
1339 glsl_to_tgsi_visitor::visit(ir_expression
*ir
)
1341 unsigned int operand
;
1342 st_src_reg op
[Elements(ir
->operands
)];
1343 st_src_reg result_src
;
1344 st_dst_reg result_dst
;
1346 /* Quick peephole: Emit MAD(a, b, c) instead of ADD(MUL(a, b), c)
1348 if (ir
->operation
== ir_binop_add
) {
1349 if (try_emit_mad(ir
, 1))
1351 if (try_emit_mad(ir
, 0))
1355 /* Quick peephole: Emit OPCODE_MAD(-a, -b, a) instead of AND(a, NOT(b))
1357 if (ir
->operation
== ir_binop_logic_and
) {
1358 if (try_emit_mad_for_and_not(ir
, 1))
1360 if (try_emit_mad_for_and_not(ir
, 0))
1364 if (try_emit_sat(ir
))
1367 if (ir
->operation
== ir_quadop_vector
)
1368 assert(!"ir_quadop_vector should have been lowered");
1370 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
1371 this->result
.file
= PROGRAM_UNDEFINED
;
1372 ir
->operands
[operand
]->accept(this);
1373 if (this->result
.file
== PROGRAM_UNDEFINED
) {
1375 printf("Failed to get tree for expression operand:\n");
1376 ir
->operands
[operand
]->accept(&v
);
1379 op
[operand
] = this->result
;
1381 /* Matrix expression operands should have been broken down to vector
1382 * operations already.
1384 assert(!ir
->operands
[operand
]->type
->is_matrix());
1387 int vector_elements
= ir
->operands
[0]->type
->vector_elements
;
1388 if (ir
->operands
[1]) {
1389 vector_elements
= MAX2(vector_elements
,
1390 ir
->operands
[1]->type
->vector_elements
);
1393 this->result
.file
= PROGRAM_UNDEFINED
;
1395 /* Storage for our result. Ideally for an assignment we'd be using
1396 * the actual storage for the result here, instead.
1398 result_src
= get_temp(ir
->type
);
1399 /* convenience for the emit functions below. */
1400 result_dst
= st_dst_reg(result_src
);
1401 /* Limit writes to the channels that will be used by result_src later.
1402 * This does limit this temp's use as a temporary for multi-instruction
1405 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1407 switch (ir
->operation
) {
1408 case ir_unop_logic_not
:
1409 if (result_dst
.type
!= GLSL_TYPE_FLOAT
)
1410 emit(ir
, TGSI_OPCODE_NOT
, result_dst
, op
[0]);
1412 /* Previously 'SEQ dst, src, 0.0' was used for this. However, many
1413 * older GPUs implement SEQ using multiple instructions (i915 uses two
1414 * SGE instructions and a MUL instruction). Since our logic values are
1415 * 0.0 and 1.0, 1-x also implements !x.
1417 op
[0].negate
= ~op
[0].negate
;
1418 emit(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], st_src_reg_for_float(1.0));
1422 assert(result_dst
.type
== GLSL_TYPE_FLOAT
|| result_dst
.type
== GLSL_TYPE_INT
);
1423 if (result_dst
.type
== GLSL_TYPE_INT
)
1424 emit(ir
, TGSI_OPCODE_INEG
, result_dst
, op
[0]);
1426 op
[0].negate
= ~op
[0].negate
;
1431 assert(result_dst
.type
== GLSL_TYPE_FLOAT
);
1432 emit(ir
, TGSI_OPCODE_ABS
, result_dst
, op
[0]);
1435 emit(ir
, TGSI_OPCODE_SSG
, result_dst
, op
[0]);
1438 emit_scalar(ir
, TGSI_OPCODE_RCP
, result_dst
, op
[0]);
1442 emit_scalar(ir
, TGSI_OPCODE_EX2
, result_dst
, op
[0]);
1446 assert(!"not reached: should be handled by ir_explog_to_explog2");
1449 emit_scalar(ir
, TGSI_OPCODE_LG2
, result_dst
, op
[0]);
1452 emit_scalar(ir
, TGSI_OPCODE_SIN
, result_dst
, op
[0]);
1455 emit_scalar(ir
, TGSI_OPCODE_COS
, result_dst
, op
[0]);
1457 case ir_unop_sin_reduced
:
1458 emit_scs(ir
, TGSI_OPCODE_SIN
, result_dst
, op
[0]);
1460 case ir_unop_cos_reduced
:
1461 emit_scs(ir
, TGSI_OPCODE_COS
, result_dst
, op
[0]);
1465 emit(ir
, TGSI_OPCODE_DDX
, result_dst
, op
[0]);
1468 op
[0].negate
= ~op
[0].negate
;
1469 emit(ir
, TGSI_OPCODE_DDY
, result_dst
, op
[0]);
1472 case ir_unop_noise
: {
1473 /* At some point, a motivated person could add a better
1474 * implementation of noise. Currently not even the nvidia
1475 * binary drivers do anything more than this. In any case, the
1476 * place to do this is in the GL state tracker, not the poor
1479 emit(ir
, TGSI_OPCODE_MOV
, result_dst
, st_src_reg_for_float(0.5));
1484 emit(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1487 emit(ir
, TGSI_OPCODE_SUB
, result_dst
, op
[0], op
[1]);
1491 emit(ir
, TGSI_OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1494 if (result_dst
.type
== GLSL_TYPE_FLOAT
)
1495 assert(!"not reached: should be handled by ir_div_to_mul_rcp");
1497 emit(ir
, TGSI_OPCODE_DIV
, result_dst
, op
[0], op
[1]);
1500 if (result_dst
.type
== GLSL_TYPE_FLOAT
)
1501 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
1503 emit(ir
, TGSI_OPCODE_MOD
, result_dst
, op
[0], op
[1]);
1507 emit(ir
, TGSI_OPCODE_SLT
, result_dst
, op
[0], op
[1]);
1509 case ir_binop_greater
:
1510 emit(ir
, TGSI_OPCODE_SLT
, result_dst
, op
[1], op
[0]);
1512 case ir_binop_lequal
:
1513 emit(ir
, TGSI_OPCODE_SGE
, result_dst
, op
[1], op
[0]);
1515 case ir_binop_gequal
:
1516 emit(ir
, TGSI_OPCODE_SGE
, result_dst
, op
[0], op
[1]);
1518 case ir_binop_equal
:
1519 emit(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1521 case ir_binop_nequal
:
1522 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1524 case ir_binop_all_equal
:
1525 /* "==" operator producing a scalar boolean. */
1526 if (ir
->operands
[0]->type
->is_vector() ||
1527 ir
->operands
[1]->type
->is_vector()) {
1528 st_src_reg temp
= get_temp(native_integers
?
1529 glsl_type::get_instance(ir
->operands
[0]->type
->base_type
, 4, 1) :
1530 glsl_type::vec4_type
);
1531 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
1532 emit(ir
, TGSI_OPCODE_SNE
, st_dst_reg(temp
), op
[0], op
[1]);
1534 /* After the dot-product, the value will be an integer on the
1535 * range [0,4]. Zero becomes 1.0, and positive values become zero.
1537 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1539 if (result_dst
.type
== GLSL_TYPE_FLOAT
) {
1540 /* Negating the result of the dot-product gives values on the range
1541 * [-4, 0]. Zero becomes 1.0, and negative values become zero.
1542 * This is achieved using SGE.
1544 st_src_reg sge_src
= result_src
;
1545 sge_src
.negate
= ~sge_src
.negate
;
1546 emit(ir
, TGSI_OPCODE_SGE
, result_dst
, sge_src
, st_src_reg_for_float(0.0));
1548 /* The TGSI negate flag doesn't work for integers, so use SEQ 0
1551 emit(ir
, TGSI_OPCODE_SEQ
, result_dst
, result_src
, st_src_reg_for_int(0));
1554 emit(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1557 case ir_binop_any_nequal
:
1558 /* "!=" operator producing a scalar boolean. */
1559 if (ir
->operands
[0]->type
->is_vector() ||
1560 ir
->operands
[1]->type
->is_vector()) {
1561 st_src_reg temp
= get_temp(native_integers
?
1562 glsl_type::get_instance(ir
->operands
[0]->type
->base_type
, 4, 1) :
1563 glsl_type::vec4_type
);
1564 assert(ir
->operands
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
1565 emit(ir
, TGSI_OPCODE_SNE
, st_dst_reg(temp
), op
[0], op
[1]);
1567 /* After the dot-product, the value will be an integer on the
1568 * range [0,4]. Zero stays zero, and positive values become 1.0.
1570 glsl_to_tgsi_instruction
*const dp
=
1571 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1572 if (this->prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
&&
1573 result_dst
.type
== GLSL_TYPE_FLOAT
) {
1574 /* The clamping to [0,1] can be done for free in the fragment
1575 * shader with a saturate.
1577 dp
->saturate
= true;
1578 } else if (result_dst
.type
== GLSL_TYPE_FLOAT
) {
1579 /* Negating the result of the dot-product gives values on the range
1580 * [-4, 0]. Zero stays zero, and negative values become 1.0. This
1581 * achieved using SLT.
1583 st_src_reg slt_src
= result_src
;
1584 slt_src
.negate
= ~slt_src
.negate
;
1585 emit(ir
, TGSI_OPCODE_SLT
, result_dst
, slt_src
, st_src_reg_for_float(0.0));
1587 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, result_src
, st_src_reg_for_int(0));
1590 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1595 assert(ir
->operands
[0]->type
->is_vector());
1597 /* After the dot-product, the value will be an integer on the
1598 * range [0,4]. Zero stays zero, and positive values become 1.0.
1600 glsl_to_tgsi_instruction
*const dp
=
1601 emit_dp(ir
, result_dst
, op
[0], op
[0],
1602 ir
->operands
[0]->type
->vector_elements
);
1603 if (this->prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
&&
1604 result_dst
.type
== GLSL_TYPE_FLOAT
) {
1605 /* The clamping to [0,1] can be done for free in the fragment
1606 * shader with a saturate.
1608 dp
->saturate
= true;
1609 } else if (result_dst
.type
== GLSL_TYPE_FLOAT
) {
1610 /* Negating the result of the dot-product gives values on the range
1611 * [-4, 0]. Zero stays zero, and negative values become 1.0. This
1612 * is achieved using SLT.
1614 st_src_reg slt_src
= result_src
;
1615 slt_src
.negate
= ~slt_src
.negate
;
1616 emit(ir
, TGSI_OPCODE_SLT
, result_dst
, slt_src
, st_src_reg_for_float(0.0));
1619 /* Use SNE 0 if integers are being used as boolean values. */
1620 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, result_src
, st_src_reg_for_int(0));
1625 case ir_binop_logic_xor
:
1626 if (native_integers
)
1627 emit(ir
, TGSI_OPCODE_XOR
, result_dst
, op
[0], op
[1]);
1629 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1632 case ir_binop_logic_or
: {
1633 if (native_integers
) {
1634 /* If integers are used as booleans, we can use an actual "or"
1637 assert(native_integers
);
1638 emit(ir
, TGSI_OPCODE_OR
, result_dst
, op
[0], op
[1]);
1640 /* After the addition, the value will be an integer on the
1641 * range [0,2]. Zero stays zero, and positive values become 1.0.
1643 glsl_to_tgsi_instruction
*add
=
1644 emit(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1645 if (this->prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1646 /* The clamping to [0,1] can be done for free in the fragment
1647 * shader with a saturate if floats are being used as boolean values.
1649 add
->saturate
= true;
1651 /* Negating the result of the addition gives values on the range
1652 * [-2, 0]. Zero stays zero, and negative values become 1.0. This
1653 * is achieved using SLT.
1655 st_src_reg slt_src
= result_src
;
1656 slt_src
.negate
= ~slt_src
.negate
;
1657 emit(ir
, TGSI_OPCODE_SLT
, result_dst
, slt_src
, st_src_reg_for_float(0.0));
1663 case ir_binop_logic_and
:
1664 /* If native integers are disabled, the bool args are stored as float 0.0
1665 * or 1.0, so "mul" gives us "and". If they're enabled, just use the
1666 * actual AND opcode.
1668 if (native_integers
)
1669 emit(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], op
[1]);
1671 emit(ir
, TGSI_OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1675 assert(ir
->operands
[0]->type
->is_vector());
1676 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
1677 emit_dp(ir
, result_dst
, op
[0], op
[1],
1678 ir
->operands
[0]->type
->vector_elements
);
1682 /* sqrt(x) = x * rsq(x). */
1683 emit_scalar(ir
, TGSI_OPCODE_RSQ
, result_dst
, op
[0]);
1684 emit(ir
, TGSI_OPCODE_MUL
, result_dst
, result_src
, op
[0]);
1685 /* For incoming channels <= 0, set the result to 0. */
1686 op
[0].negate
= ~op
[0].negate
;
1687 emit(ir
, TGSI_OPCODE_CMP
, result_dst
,
1688 op
[0], result_src
, st_src_reg_for_float(0.0));
1691 emit_scalar(ir
, TGSI_OPCODE_RSQ
, result_dst
, op
[0]);
1694 if (native_integers
) {
1695 emit(ir
, TGSI_OPCODE_I2F
, result_dst
, op
[0]);
1698 /* fallthrough to next case otherwise */
1700 if (native_integers
) {
1701 emit(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], st_src_reg_for_float(1.0));
1704 /* fallthrough to next case otherwise */
1707 /* Converting between signed and unsigned integers is a no-op. */
1711 if (native_integers
) {
1712 /* Booleans are stored as integers using ~0 for true and 0 for false.
1713 * GLSL requires that int(bool) return 1 for true and 0 for false.
1714 * This conversion is done with AND, but it could be done with NEG.
1716 emit(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], st_src_reg_for_int(1));
1718 /* Booleans and integers are both stored as floats when native
1719 * integers are disabled.
1725 if (native_integers
)
1726 emit(ir
, TGSI_OPCODE_F2I
, result_dst
, op
[0]);
1728 emit(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
1731 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], st_src_reg_for_float(0.0));
1734 if (native_integers
)
1735 emit(ir
, TGSI_OPCODE_INEG
, result_dst
, op
[0]);
1737 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], st_src_reg_for_float(0.0));
1740 emit(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
1743 op
[0].negate
= ~op
[0].negate
;
1744 emit(ir
, TGSI_OPCODE_FLR
, result_dst
, op
[0]);
1745 result_src
.negate
= ~result_src
.negate
;
1748 emit(ir
, TGSI_OPCODE_FLR
, result_dst
, op
[0]);
1751 emit(ir
, TGSI_OPCODE_FRC
, result_dst
, op
[0]);
1755 emit(ir
, TGSI_OPCODE_MIN
, result_dst
, op
[0], op
[1]);
1758 emit(ir
, TGSI_OPCODE_MAX
, result_dst
, op
[0], op
[1]);
1761 emit_scalar(ir
, TGSI_OPCODE_POW
, result_dst
, op
[0], op
[1]);
1764 case ir_unop_bit_not
:
1765 if (native_integers
) {
1766 emit(ir
, TGSI_OPCODE_NOT
, result_dst
, op
[0]);
1770 if (native_integers
) {
1771 emit(ir
, TGSI_OPCODE_U2F
, result_dst
, op
[0]);
1774 case ir_binop_lshift
:
1775 if (native_integers
) {
1776 emit(ir
, TGSI_OPCODE_SHL
, result_dst
, op
[0]);
1779 case ir_binop_rshift
:
1780 if (native_integers
) {
1781 emit(ir
, TGSI_OPCODE_ISHR
, result_dst
, op
[0]);
1784 case ir_binop_bit_and
:
1785 if (native_integers
) {
1786 emit(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0]);
1789 case ir_binop_bit_xor
:
1790 if (native_integers
) {
1791 emit(ir
, TGSI_OPCODE_XOR
, result_dst
, op
[0]);
1794 case ir_binop_bit_or
:
1795 if (native_integers
) {
1796 emit(ir
, TGSI_OPCODE_OR
, result_dst
, op
[0]);
1799 case ir_unop_round_even
:
1800 assert(!"GLSL 1.30 features unsupported");
1803 case ir_quadop_vector
:
1804 /* This operation should have already been handled.
1806 assert(!"Should not get here.");
1810 this->result
= result_src
;
1815 glsl_to_tgsi_visitor::visit(ir_swizzle
*ir
)
1821 /* Note that this is only swizzles in expressions, not those on the left
1822 * hand side of an assignment, which do write masking. See ir_assignment
1826 ir
->val
->accept(this);
1828 assert(src
.file
!= PROGRAM_UNDEFINED
);
1830 for (i
= 0; i
< 4; i
++) {
1831 if (i
< ir
->type
->vector_elements
) {
1834 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.x
);
1837 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.y
);
1840 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.z
);
1843 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.w
);
1847 /* If the type is smaller than a vec4, replicate the last
1850 swizzle
[i
] = swizzle
[ir
->type
->vector_elements
- 1];
1854 src
.swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
1860 glsl_to_tgsi_visitor::visit(ir_dereference_variable
*ir
)
1862 variable_storage
*entry
= find_variable_storage(ir
->var
);
1863 ir_variable
*var
= ir
->var
;
1866 switch (var
->mode
) {
1867 case ir_var_uniform
:
1868 entry
= new(mem_ctx
) variable_storage(var
, PROGRAM_UNIFORM
,
1870 this->variables
.push_tail(entry
);
1874 /* The linker assigns locations for varyings and attributes,
1875 * including deprecated builtins (like gl_Color), user-assign
1876 * generic attributes (glBindVertexLocation), and
1877 * user-defined varyings.
1879 * FINISHME: We would hit this path for function arguments. Fix!
1881 assert(var
->location
!= -1);
1882 entry
= new(mem_ctx
) variable_storage(var
,
1885 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
&&
1886 var
->location
>= VERT_ATTRIB_GENERIC0
) {
1887 _mesa_add_attribute(this->prog
->Attributes
,
1889 _mesa_sizeof_glsl_type(var
->type
->gl_type
),
1891 var
->location
- VERT_ATTRIB_GENERIC0
);
1895 assert(var
->location
!= -1);
1896 entry
= new(mem_ctx
) variable_storage(var
,
1900 case ir_var_system_value
:
1901 entry
= new(mem_ctx
) variable_storage(var
,
1902 PROGRAM_SYSTEM_VALUE
,
1906 case ir_var_temporary
:
1907 entry
= new(mem_ctx
) variable_storage(var
, PROGRAM_TEMPORARY
,
1909 this->variables
.push_tail(entry
);
1911 next_temp
+= type_size(var
->type
);
1916 printf("Failed to make storage for %s\n", var
->name
);
1921 this->result
= st_src_reg(entry
->file
, entry
->index
, var
->type
);
1922 if (!native_integers
)
1923 this->result
.type
= GLSL_TYPE_FLOAT
;
1927 glsl_to_tgsi_visitor::visit(ir_dereference_array
*ir
)
1931 int element_size
= type_size(ir
->type
);
1933 index
= ir
->array_index
->constant_expression_value();
1935 ir
->array
->accept(this);
1939 src
.index
+= index
->value
.i
[0] * element_size
;
1941 /* Variable index array dereference. It eats the "vec4" of the
1942 * base of the array and an index that offsets the TGSI register
1945 ir
->array_index
->accept(this);
1947 st_src_reg index_reg
;
1949 if (element_size
== 1) {
1950 index_reg
= this->result
;
1952 index_reg
= get_temp(native_integers
?
1953 glsl_type::int_type
: glsl_type::float_type
);
1955 emit(ir
, TGSI_OPCODE_MUL
, st_dst_reg(index_reg
),
1956 this->result
, st_src_reg_for_type(index_reg
.type
, element_size
));
1959 /* If there was already a relative address register involved, add the
1960 * new and the old together to get the new offset.
1962 if (src
.reladdr
!= NULL
) {
1963 st_src_reg accum_reg
= get_temp(native_integers
?
1964 glsl_type::int_type
: glsl_type::float_type
);
1966 emit(ir
, TGSI_OPCODE_ADD
, st_dst_reg(accum_reg
),
1967 index_reg
, *src
.reladdr
);
1969 index_reg
= accum_reg
;
1972 src
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
1973 memcpy(src
.reladdr
, &index_reg
, sizeof(index_reg
));
1976 /* If the type is smaller than a vec4, replicate the last channel out. */
1977 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
1978 src
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1980 src
.swizzle
= SWIZZLE_NOOP
;
1986 glsl_to_tgsi_visitor::visit(ir_dereference_record
*ir
)
1989 const glsl_type
*struct_type
= ir
->record
->type
;
1992 ir
->record
->accept(this);
1994 for (i
= 0; i
< struct_type
->length
; i
++) {
1995 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
1997 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
2000 /* If the type is smaller than a vec4, replicate the last channel out. */
2001 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
2002 this->result
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
2004 this->result
.swizzle
= SWIZZLE_NOOP
;
2006 this->result
.index
+= offset
;
2010 * We want to be careful in assignment setup to hit the actual storage
2011 * instead of potentially using a temporary like we might with the
2012 * ir_dereference handler.
2015 get_assignment_lhs(ir_dereference
*ir
, glsl_to_tgsi_visitor
*v
)
2017 /* The LHS must be a dereference. If the LHS is a variable indexed array
2018 * access of a vector, it must be separated into a series conditional moves
2019 * before reaching this point (see ir_vec_index_to_cond_assign).
2021 assert(ir
->as_dereference());
2022 ir_dereference_array
*deref_array
= ir
->as_dereference_array();
2024 assert(!deref_array
->array
->type
->is_vector());
2027 /* Use the rvalue deref handler for the most part. We'll ignore
2028 * swizzles in it and write swizzles using writemask, though.
2031 return st_dst_reg(v
->result
);
2035 * Process the condition of a conditional assignment
2037 * Examines the condition of a conditional assignment to generate the optimal
2038 * first operand of a \c CMP instruction. If the condition is a relational
2039 * operator with 0 (e.g., \c ir_binop_less), the value being compared will be
2040 * used as the source for the \c CMP instruction. Otherwise the comparison
2041 * is processed to a boolean result, and the boolean result is used as the
2042 * operand to the CMP instruction.
2045 glsl_to_tgsi_visitor::process_move_condition(ir_rvalue
*ir
)
2047 ir_rvalue
*src_ir
= ir
;
2049 bool switch_order
= false;
2051 ir_expression
*const expr
= ir
->as_expression();
2052 if ((expr
!= NULL
) && (expr
->get_num_operands() == 2)) {
2053 bool zero_on_left
= false;
2055 if (expr
->operands
[0]->is_zero()) {
2056 src_ir
= expr
->operands
[1];
2057 zero_on_left
= true;
2058 } else if (expr
->operands
[1]->is_zero()) {
2059 src_ir
= expr
->operands
[0];
2060 zero_on_left
= false;
2064 * (a < 0) T F F ( a < 0) T F F
2065 * (0 < a) F F T (-a < 0) F F T
2066 * (a <= 0) T T F (-a < 0) F F T (swap order of other operands)
2067 * (0 <= a) F T T ( a < 0) T F F (swap order of other operands)
2068 * (a > 0) F F T (-a < 0) F F T
2069 * (0 > a) T F F ( a < 0) T F F
2070 * (a >= 0) F T T ( a < 0) T F F (swap order of other operands)
2071 * (0 >= a) T T F (-a < 0) F F T (swap order of other operands)
2073 * Note that exchanging the order of 0 and 'a' in the comparison simply
2074 * means that the value of 'a' should be negated.
2077 switch (expr
->operation
) {
2079 switch_order
= false;
2080 negate
= zero_on_left
;
2083 case ir_binop_greater
:
2084 switch_order
= false;
2085 negate
= !zero_on_left
;
2088 case ir_binop_lequal
:
2089 switch_order
= true;
2090 negate
= !zero_on_left
;
2093 case ir_binop_gequal
:
2094 switch_order
= true;
2095 negate
= zero_on_left
;
2099 /* This isn't the right kind of comparison afterall, so make sure
2100 * the whole condition is visited.
2108 src_ir
->accept(this);
2110 /* We use the TGSI_OPCODE_CMP (a < 0 ? b : c) for conditional moves, and the
2111 * condition we produced is 0.0 or 1.0. By flipping the sign, we can
2112 * choose which value TGSI_OPCODE_CMP produces without an extra instruction
2113 * computing the condition.
2116 this->result
.negate
= ~this->result
.negate
;
2118 return switch_order
;
2122 glsl_to_tgsi_visitor::visit(ir_assignment
*ir
)
2128 ir
->rhs
->accept(this);
2131 l
= get_assignment_lhs(ir
->lhs
, this);
2133 /* FINISHME: This should really set to the correct maximal writemask for each
2134 * FINISHME: component written (in the loops below). This case can only
2135 * FINISHME: occur for matrices, arrays, and structures.
2137 if (ir
->write_mask
== 0) {
2138 assert(!ir
->lhs
->type
->is_scalar() && !ir
->lhs
->type
->is_vector());
2139 l
.writemask
= WRITEMASK_XYZW
;
2140 } else if (ir
->lhs
->type
->is_scalar() &&
2141 ir
->lhs
->variable_referenced()->mode
== ir_var_out
) {
2142 /* FINISHME: This hack makes writing to gl_FragDepth, which lives in the
2143 * FINISHME: W component of fragment shader output zero, work correctly.
2145 l
.writemask
= WRITEMASK_XYZW
;
2148 int first_enabled_chan
= 0;
2151 l
.writemask
= ir
->write_mask
;
2153 for (int i
= 0; i
< 4; i
++) {
2154 if (l
.writemask
& (1 << i
)) {
2155 first_enabled_chan
= GET_SWZ(r
.swizzle
, i
);
2160 /* Swizzle a small RHS vector into the channels being written.
2162 * glsl ir treats write_mask as dictating how many channels are
2163 * present on the RHS while TGSI treats write_mask as just
2164 * showing which channels of the vec4 RHS get written.
2166 for (int i
= 0; i
< 4; i
++) {
2167 if (l
.writemask
& (1 << i
))
2168 swizzles
[i
] = GET_SWZ(r
.swizzle
, rhs_chan
++);
2170 swizzles
[i
] = first_enabled_chan
;
2172 r
.swizzle
= MAKE_SWIZZLE4(swizzles
[0], swizzles
[1],
2173 swizzles
[2], swizzles
[3]);
2176 assert(l
.file
!= PROGRAM_UNDEFINED
);
2177 assert(r
.file
!= PROGRAM_UNDEFINED
);
2179 if (ir
->condition
) {
2180 const bool switch_order
= this->process_move_condition(ir
->condition
);
2181 st_src_reg condition
= this->result
;
2183 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
2184 st_src_reg l_src
= st_src_reg(l
);
2185 st_src_reg condition_temp
= condition
;
2186 l_src
.swizzle
= swizzle_for_size(ir
->lhs
->type
->vector_elements
);
2188 if (native_integers
) {
2189 /* This is necessary because TGSI's CMP instruction expects the
2190 * condition to be a float, and we store booleans as integers.
2191 * If TGSI had a UCMP instruction or similar, this extra
2192 * instruction would not be necessary.
2194 condition_temp
= get_temp(glsl_type::vec4_type
);
2195 condition
.negate
= 0;
2196 emit(ir
, TGSI_OPCODE_I2F
, st_dst_reg(condition_temp
), condition
);
2197 condition_temp
.swizzle
= condition
.swizzle
;
2201 emit(ir
, TGSI_OPCODE_CMP
, l
, condition_temp
, l_src
, r
);
2203 emit(ir
, TGSI_OPCODE_CMP
, l
, condition_temp
, r
, l_src
);
2209 } else if (ir
->rhs
->as_expression() &&
2210 this->instructions
.get_tail() &&
2211 ir
->rhs
== ((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->ir
&&
2212 type_size(ir
->lhs
->type
) == 1 &&
2213 l
.writemask
== ((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->dst
.writemask
) {
2214 /* To avoid emitting an extra MOV when assigning an expression to a
2215 * variable, emit the last instruction of the expression again, but
2216 * replace the destination register with the target of the assignment.
2217 * Dead code elimination will remove the original instruction.
2219 glsl_to_tgsi_instruction
*inst
, *new_inst
;
2220 inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
2221 new_inst
= emit(ir
, inst
->op
, l
, inst
->src
[0], inst
->src
[1], inst
->src
[2]);
2222 new_inst
->saturate
= inst
->saturate
;
2223 inst
->dead_mask
= inst
->dst
.writemask
;
2225 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
2226 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
2235 glsl_to_tgsi_visitor::visit(ir_constant
*ir
)
2238 GLfloat stack_vals
[4] = { 0 };
2239 gl_constant_value
*values
= (gl_constant_value
*) stack_vals
;
2240 GLenum gl_type
= GL_NONE
;
2242 static int in_array
= 0;
2243 gl_register_file file
= in_array
? PROGRAM_CONSTANT
: PROGRAM_IMMEDIATE
;
2245 /* Unfortunately, 4 floats is all we can get into
2246 * _mesa_add_typed_unnamed_constant. So, make a temp to store an
2247 * aggregate constant and move each constant value into it. If we
2248 * get lucky, copy propagation will eliminate the extra moves.
2250 if (ir
->type
->base_type
== GLSL_TYPE_STRUCT
) {
2251 st_src_reg temp_base
= get_temp(ir
->type
);
2252 st_dst_reg temp
= st_dst_reg(temp_base
);
2254 foreach_iter(exec_list_iterator
, iter
, ir
->components
) {
2255 ir_constant
*field_value
= (ir_constant
*)iter
.get();
2256 int size
= type_size(field_value
->type
);
2260 field_value
->accept(this);
2263 for (i
= 0; i
< (unsigned int)size
; i
++) {
2264 emit(ir
, TGSI_OPCODE_MOV
, temp
, src
);
2270 this->result
= temp_base
;
2274 if (ir
->type
->is_array()) {
2275 st_src_reg temp_base
= get_temp(ir
->type
);
2276 st_dst_reg temp
= st_dst_reg(temp_base
);
2277 int size
= type_size(ir
->type
->fields
.array
);
2282 for (i
= 0; i
< ir
->type
->length
; i
++) {
2283 ir
->array_elements
[i
]->accept(this);
2285 for (int j
= 0; j
< size
; j
++) {
2286 emit(ir
, TGSI_OPCODE_MOV
, temp
, src
);
2292 this->result
= temp_base
;
2297 if (ir
->type
->is_matrix()) {
2298 st_src_reg mat
= get_temp(ir
->type
);
2299 st_dst_reg mat_column
= st_dst_reg(mat
);
2301 for (i
= 0; i
< ir
->type
->matrix_columns
; i
++) {
2302 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
2303 values
= (gl_constant_value
*) &ir
->value
.f
[i
* ir
->type
->vector_elements
];
2305 src
= st_src_reg(file
, -1, ir
->type
->base_type
);
2306 src
.index
= add_constant(file
,
2308 ir
->type
->vector_elements
,
2311 emit(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
2320 switch (ir
->type
->base_type
) {
2321 case GLSL_TYPE_FLOAT
:
2323 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2324 values
[i
].f
= ir
->value
.f
[i
];
2327 case GLSL_TYPE_UINT
:
2328 gl_type
= native_integers
? GL_UNSIGNED_INT
: GL_FLOAT
;
2329 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2330 if (native_integers
)
2331 values
[i
].u
= ir
->value
.u
[i
];
2333 values
[i
].f
= ir
->value
.u
[i
];
2337 gl_type
= native_integers
? GL_INT
: GL_FLOAT
;
2338 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2339 if (native_integers
)
2340 values
[i
].i
= ir
->value
.i
[i
];
2342 values
[i
].f
= ir
->value
.i
[i
];
2345 case GLSL_TYPE_BOOL
:
2346 gl_type
= native_integers
? GL_BOOL
: GL_FLOAT
;
2347 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2348 if (native_integers
)
2349 values
[i
].b
= ir
->value
.b
[i
];
2351 values
[i
].f
= ir
->value
.b
[i
];
2355 assert(!"Non-float/uint/int/bool constant");
2358 this->result
= st_src_reg(file
, -1, ir
->type
);
2359 this->result
.index
= add_constant(file
,
2361 ir
->type
->vector_elements
,
2363 &this->result
.swizzle
);
2367 glsl_to_tgsi_visitor::get_function_signature(ir_function_signature
*sig
)
2369 function_entry
*entry
;
2371 foreach_iter(exec_list_iterator
, iter
, this->function_signatures
) {
2372 entry
= (function_entry
*)iter
.get();
2374 if (entry
->sig
== sig
)
2378 entry
= ralloc(mem_ctx
, function_entry
);
2380 entry
->sig_id
= this->next_signature_id
++;
2381 entry
->bgn_inst
= NULL
;
2383 /* Allocate storage for all the parameters. */
2384 foreach_iter(exec_list_iterator
, iter
, sig
->parameters
) {
2385 ir_variable
*param
= (ir_variable
*)iter
.get();
2386 variable_storage
*storage
;
2388 storage
= find_variable_storage(param
);
2391 storage
= new(mem_ctx
) variable_storage(param
, PROGRAM_TEMPORARY
,
2393 this->variables
.push_tail(storage
);
2395 this->next_temp
+= type_size(param
->type
);
2398 if (!sig
->return_type
->is_void()) {
2399 entry
->return_reg
= get_temp(sig
->return_type
);
2401 entry
->return_reg
= undef_src
;
2404 this->function_signatures
.push_tail(entry
);
2409 glsl_to_tgsi_visitor::visit(ir_call
*ir
)
2411 glsl_to_tgsi_instruction
*call_inst
;
2412 ir_function_signature
*sig
= ir
->get_callee();
2413 function_entry
*entry
= get_function_signature(sig
);
2416 /* Process in parameters. */
2417 exec_list_iterator sig_iter
= sig
->parameters
.iterator();
2418 foreach_iter(exec_list_iterator
, iter
, *ir
) {
2419 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
2420 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
2422 if (param
->mode
== ir_var_in
||
2423 param
->mode
== ir_var_inout
) {
2424 variable_storage
*storage
= find_variable_storage(param
);
2427 param_rval
->accept(this);
2428 st_src_reg r
= this->result
;
2431 l
.file
= storage
->file
;
2432 l
.index
= storage
->index
;
2434 l
.writemask
= WRITEMASK_XYZW
;
2435 l
.cond_mask
= COND_TR
;
2437 for (i
= 0; i
< type_size(param
->type
); i
++) {
2438 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
2446 assert(!sig_iter
.has_next());
2448 /* Emit call instruction */
2449 call_inst
= emit(ir
, TGSI_OPCODE_CAL
);
2450 call_inst
->function
= entry
;
2452 /* Process out parameters. */
2453 sig_iter
= sig
->parameters
.iterator();
2454 foreach_iter(exec_list_iterator
, iter
, *ir
) {
2455 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
2456 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
2458 if (param
->mode
== ir_var_out
||
2459 param
->mode
== ir_var_inout
) {
2460 variable_storage
*storage
= find_variable_storage(param
);
2464 r
.file
= storage
->file
;
2465 r
.index
= storage
->index
;
2467 r
.swizzle
= SWIZZLE_NOOP
;
2470 param_rval
->accept(this);
2471 st_dst_reg l
= st_dst_reg(this->result
);
2473 for (i
= 0; i
< type_size(param
->type
); i
++) {
2474 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
2482 assert(!sig_iter
.has_next());
2484 /* Process return value. */
2485 this->result
= entry
->return_reg
;
2489 glsl_to_tgsi_visitor::visit(ir_texture
*ir
)
2491 st_src_reg result_src
, coord
, lod_info
, projector
, dx
, dy
, offset
;
2492 st_dst_reg result_dst
, coord_dst
;
2493 glsl_to_tgsi_instruction
*inst
= NULL
;
2494 unsigned opcode
= TGSI_OPCODE_NOP
;
2496 if (ir
->coordinate
) {
2497 ir
->coordinate
->accept(this);
2499 /* Put our coords in a temp. We'll need to modify them for shadow,
2500 * projection, or LOD, so the only case we'd use it as is is if
2501 * we're doing plain old texturing. The optimization passes on
2502 * glsl_to_tgsi_visitor should handle cleaning up our mess in that case.
2504 coord
= get_temp(glsl_type::vec4_type
);
2505 coord_dst
= st_dst_reg(coord
);
2506 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
2509 if (ir
->projector
) {
2510 ir
->projector
->accept(this);
2511 projector
= this->result
;
2514 /* Storage for our result. Ideally for an assignment we'd be using
2515 * the actual storage for the result here, instead.
2517 result_src
= get_temp(glsl_type::vec4_type
);
2518 result_dst
= st_dst_reg(result_src
);
2522 opcode
= TGSI_OPCODE_TEX
;
2525 opcode
= TGSI_OPCODE_TXB
;
2526 ir
->lod_info
.bias
->accept(this);
2527 lod_info
= this->result
;
2530 opcode
= TGSI_OPCODE_TXL
;
2531 ir
->lod_info
.lod
->accept(this);
2532 lod_info
= this->result
;
2535 opcode
= TGSI_OPCODE_TXD
;
2536 ir
->lod_info
.grad
.dPdx
->accept(this);
2538 ir
->lod_info
.grad
.dPdy
->accept(this);
2542 opcode
= TGSI_OPCODE_TXQ
;
2543 ir
->lod_info
.lod
->accept(this);
2544 lod_info
= this->result
;
2547 opcode
= TGSI_OPCODE_TXF
;
2548 ir
->lod_info
.lod
->accept(this);
2549 lod_info
= this->result
;
2551 ir
->offset
->accept(this);
2552 offset
= this->result
;
2557 const glsl_type
*sampler_type
= ir
->sampler
->type
;
2559 if (ir
->projector
) {
2560 if (opcode
== TGSI_OPCODE_TEX
) {
2561 /* Slot the projector in as the last component of the coord. */
2562 coord_dst
.writemask
= WRITEMASK_W
;
2563 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, projector
);
2564 coord_dst
.writemask
= WRITEMASK_XYZW
;
2565 opcode
= TGSI_OPCODE_TXP
;
2567 st_src_reg coord_w
= coord
;
2568 coord_w
.swizzle
= SWIZZLE_WWWW
;
2570 /* For the other TEX opcodes there's no projective version
2571 * since the last slot is taken up by LOD info. Do the
2572 * projective divide now.
2574 coord_dst
.writemask
= WRITEMASK_W
;
2575 emit(ir
, TGSI_OPCODE_RCP
, coord_dst
, projector
);
2577 /* In the case where we have to project the coordinates "by hand,"
2578 * the shadow comparator value must also be projected.
2580 st_src_reg tmp_src
= coord
;
2581 if (ir
->shadow_comparitor
) {
2582 /* Slot the shadow value in as the second to last component of the
2585 ir
->shadow_comparitor
->accept(this);
2587 tmp_src
= get_temp(glsl_type::vec4_type
);
2588 st_dst_reg tmp_dst
= st_dst_reg(tmp_src
);
2590 /* Projective division not allowed for array samplers. */
2591 assert(!sampler_type
->sampler_array
);
2593 tmp_dst
.writemask
= WRITEMASK_Z
;
2594 emit(ir
, TGSI_OPCODE_MOV
, tmp_dst
, this->result
);
2596 tmp_dst
.writemask
= WRITEMASK_XY
;
2597 emit(ir
, TGSI_OPCODE_MOV
, tmp_dst
, coord
);
2600 coord_dst
.writemask
= WRITEMASK_XYZ
;
2601 emit(ir
, TGSI_OPCODE_MUL
, coord_dst
, tmp_src
, coord_w
);
2603 coord_dst
.writemask
= WRITEMASK_XYZW
;
2604 coord
.swizzle
= SWIZZLE_XYZW
;
2608 /* If projection is done and the opcode is not TGSI_OPCODE_TXP, then the shadow
2609 * comparator was put in the correct place (and projected) by the code,
2610 * above, that handles by-hand projection.
2612 if (ir
->shadow_comparitor
&& (!ir
->projector
|| opcode
== TGSI_OPCODE_TXP
)) {
2613 /* Slot the shadow value in as the second to last component of the
2616 ir
->shadow_comparitor
->accept(this);
2618 /* XXX This will need to be updated for cubemap array samplers. */
2619 if (sampler_type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_2D
&&
2620 sampler_type
->sampler_array
) {
2621 coord_dst
.writemask
= WRITEMASK_W
;
2623 coord_dst
.writemask
= WRITEMASK_Z
;
2626 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
2627 coord_dst
.writemask
= WRITEMASK_XYZW
;
2630 if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXB
||
2631 opcode
== TGSI_OPCODE_TXF
) {
2632 /* TGSI stores LOD or LOD bias in the last channel of the coords. */
2633 coord_dst
.writemask
= WRITEMASK_W
;
2634 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, lod_info
);
2635 coord_dst
.writemask
= WRITEMASK_XYZW
;
2638 if (opcode
== TGSI_OPCODE_TXD
)
2639 inst
= emit(ir
, opcode
, result_dst
, coord
, dx
, dy
);
2640 else if (opcode
== TGSI_OPCODE_TXQ
)
2641 inst
= emit(ir
, opcode
, result_dst
, lod_info
);
2642 else if (opcode
== TGSI_OPCODE_TXF
) {
2643 inst
= emit(ir
, opcode
, result_dst
, coord
);
2645 inst
= emit(ir
, opcode
, result_dst
, coord
);
2647 if (ir
->shadow_comparitor
)
2648 inst
->tex_shadow
= GL_TRUE
;
2650 inst
->sampler
= _mesa_get_sampler_uniform_value(ir
->sampler
,
2651 this->shader_program
,
2655 inst
->tex_offset_num_offset
= 1;
2656 inst
->tex_offsets
[0].Index
= offset
.index
;
2657 inst
->tex_offsets
[0].File
= offset
.file
;
2658 inst
->tex_offsets
[0].SwizzleX
= GET_SWZ(offset
.swizzle
, 0);
2659 inst
->tex_offsets
[0].SwizzleY
= GET_SWZ(offset
.swizzle
, 1);
2660 inst
->tex_offsets
[0].SwizzleZ
= GET_SWZ(offset
.swizzle
, 2);
2663 switch (sampler_type
->sampler_dimensionality
) {
2664 case GLSL_SAMPLER_DIM_1D
:
2665 inst
->tex_target
= (sampler_type
->sampler_array
)
2666 ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
2668 case GLSL_SAMPLER_DIM_2D
:
2669 inst
->tex_target
= (sampler_type
->sampler_array
)
2670 ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
2672 case GLSL_SAMPLER_DIM_3D
:
2673 inst
->tex_target
= TEXTURE_3D_INDEX
;
2675 case GLSL_SAMPLER_DIM_CUBE
:
2676 inst
->tex_target
= TEXTURE_CUBE_INDEX
;
2678 case GLSL_SAMPLER_DIM_RECT
:
2679 inst
->tex_target
= TEXTURE_RECT_INDEX
;
2681 case GLSL_SAMPLER_DIM_BUF
:
2682 assert(!"FINISHME: Implement ARB_texture_buffer_object");
2685 assert(!"Should not get here.");
2688 this->result
= result_src
;
2692 glsl_to_tgsi_visitor::visit(ir_return
*ir
)
2694 if (ir
->get_value()) {
2698 assert(current_function
);
2700 ir
->get_value()->accept(this);
2701 st_src_reg r
= this->result
;
2703 l
= st_dst_reg(current_function
->return_reg
);
2705 for (i
= 0; i
< type_size(current_function
->sig
->return_type
); i
++) {
2706 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
2712 emit(ir
, TGSI_OPCODE_RET
);
2716 glsl_to_tgsi_visitor::visit(ir_discard
*ir
)
2718 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
2720 if (ir
->condition
) {
2721 ir
->condition
->accept(this);
2722 this->result
.negate
= ~this->result
.negate
;
2723 emit(ir
, TGSI_OPCODE_KIL
, undef_dst
, this->result
);
2725 emit(ir
, TGSI_OPCODE_KILP
);
2728 fp
->UsesKill
= GL_TRUE
;
2732 glsl_to_tgsi_visitor::visit(ir_if
*ir
)
2734 glsl_to_tgsi_instruction
*cond_inst
, *if_inst
;
2735 glsl_to_tgsi_instruction
*prev_inst
;
2737 prev_inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
2739 ir
->condition
->accept(this);
2740 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
2742 if (this->options
->EmitCondCodes
) {
2743 cond_inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
2745 /* See if we actually generated any instruction for generating
2746 * the condition. If not, then cook up a move to a temp so we
2747 * have something to set cond_update on.
2749 if (cond_inst
== prev_inst
) {
2750 st_src_reg temp
= get_temp(glsl_type::bool_type
);
2751 cond_inst
= emit(ir
->condition
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), result
);
2753 cond_inst
->cond_update
= GL_TRUE
;
2755 if_inst
= emit(ir
->condition
, TGSI_OPCODE_IF
);
2756 if_inst
->dst
.cond_mask
= COND_NE
;
2758 if_inst
= emit(ir
->condition
, TGSI_OPCODE_IF
, undef_dst
, this->result
);
2761 this->instructions
.push_tail(if_inst
);
2763 visit_exec_list(&ir
->then_instructions
, this);
2765 if (!ir
->else_instructions
.is_empty()) {
2766 emit(ir
->condition
, TGSI_OPCODE_ELSE
);
2767 visit_exec_list(&ir
->else_instructions
, this);
2770 if_inst
= emit(ir
->condition
, TGSI_OPCODE_ENDIF
);
2773 glsl_to_tgsi_visitor::glsl_to_tgsi_visitor()
2775 result
.file
= PROGRAM_UNDEFINED
;
2777 next_signature_id
= 1;
2779 current_function
= NULL
;
2780 num_address_regs
= 0;
2781 indirect_addr_temps
= false;
2782 indirect_addr_consts
= false;
2783 mem_ctx
= ralloc_context(NULL
);
2786 glsl_to_tgsi_visitor::~glsl_to_tgsi_visitor()
2788 ralloc_free(mem_ctx
);
2791 extern "C" void free_glsl_to_tgsi_visitor(glsl_to_tgsi_visitor
*v
)
2798 * Count resources used by the given gpu program (number of texture
2802 count_resources(glsl_to_tgsi_visitor
*v
, gl_program
*prog
)
2804 v
->samplers_used
= 0;
2806 foreach_iter(exec_list_iterator
, iter
, v
->instructions
) {
2807 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
2809 if (is_tex_instruction(inst
->op
)) {
2810 v
->samplers_used
|= 1 << inst
->sampler
;
2812 prog
->SamplerTargets
[inst
->sampler
] =
2813 (gl_texture_index
)inst
->tex_target
;
2814 if (inst
->tex_shadow
) {
2815 prog
->ShadowSamplers
|= 1 << inst
->sampler
;
2820 prog
->SamplersUsed
= v
->samplers_used
;
2821 _mesa_update_shader_textures_used(prog
);
2826 * Check if the given vertex/fragment/shader program is within the
2827 * resource limits of the context (number of texture units, etc).
2828 * If any of those checks fail, record a linker error.
2830 * XXX more checks are needed...
2833 check_resources(const struct gl_context
*ctx
,
2834 struct gl_shader_program
*shader_program
,
2835 glsl_to_tgsi_visitor
*prog
,
2836 struct gl_program
*proginfo
)
2838 switch (proginfo
->Target
) {
2839 case GL_VERTEX_PROGRAM_ARB
:
2840 if (_mesa_bitcount(prog
->samplers_used
) >
2841 ctx
->Const
.MaxVertexTextureImageUnits
) {
2842 fail_link(shader_program
, "Too many vertex shader texture samplers");
2844 if (proginfo
->Parameters
->NumParameters
> MAX_UNIFORMS
) {
2845 fail_link(shader_program
, "Too many vertex shader constants");
2848 case MESA_GEOMETRY_PROGRAM
:
2849 if (_mesa_bitcount(prog
->samplers_used
) >
2850 ctx
->Const
.MaxGeometryTextureImageUnits
) {
2851 fail_link(shader_program
, "Too many geometry shader texture samplers");
2853 if (proginfo
->Parameters
->NumParameters
>
2854 MAX_GEOMETRY_UNIFORM_COMPONENTS
/ 4) {
2855 fail_link(shader_program
, "Too many geometry shader constants");
2858 case GL_FRAGMENT_PROGRAM_ARB
:
2859 if (_mesa_bitcount(prog
->samplers_used
) >
2860 ctx
->Const
.MaxTextureImageUnits
) {
2861 fail_link(shader_program
, "Too many fragment shader texture samplers");
2863 if (proginfo
->Parameters
->NumParameters
> MAX_UNIFORMS
) {
2864 fail_link(shader_program
, "Too many fragment shader constants");
2868 _mesa_problem(ctx
, "unexpected program type in check_resources()");
2874 struct uniform_sort
{
2875 struct gl_uniform
*u
;
2879 /* The shader_program->Uniforms list is almost sorted in increasing
2880 * uniform->{Frag,Vert}Pos locations, but not quite when there are
2881 * uniforms shared between targets. We need to add parameters in
2882 * increasing order for the targets.
2885 sort_uniforms(const void *a
, const void *b
)
2887 struct uniform_sort
*u1
= (struct uniform_sort
*)a
;
2888 struct uniform_sort
*u2
= (struct uniform_sort
*)b
;
2890 return u1
->pos
- u2
->pos
;
2893 /* Add the uniforms to the parameters. The linker chose locations
2894 * in our parameters lists (which weren't created yet), which the
2895 * uniforms code will use to poke values into our parameters list
2896 * when uniforms are updated.
2899 add_uniforms_to_parameters_list(struct gl_shader_program
*shader_program
,
2900 struct gl_shader
*shader
,
2901 struct gl_program
*prog
)
2904 unsigned int next_sampler
= 0, num_uniforms
= 0;
2905 struct uniform_sort
*sorted_uniforms
;
2907 sorted_uniforms
= ralloc_array(NULL
, struct uniform_sort
,
2908 shader_program
->Uniforms
->NumUniforms
);
2910 for (i
= 0; i
< shader_program
->Uniforms
->NumUniforms
; i
++) {
2911 struct gl_uniform
*uniform
= shader_program
->Uniforms
->Uniforms
+ i
;
2912 int parameter_index
= -1;
2914 switch (shader
->Type
) {
2915 case GL_VERTEX_SHADER
:
2916 parameter_index
= uniform
->VertPos
;
2918 case GL_FRAGMENT_SHADER
:
2919 parameter_index
= uniform
->FragPos
;
2921 case GL_GEOMETRY_SHADER
:
2922 parameter_index
= uniform
->GeomPos
;
2926 /* Only add uniforms used in our target. */
2927 if (parameter_index
!= -1) {
2928 sorted_uniforms
[num_uniforms
].pos
= parameter_index
;
2929 sorted_uniforms
[num_uniforms
].u
= uniform
;
2934 qsort(sorted_uniforms
, num_uniforms
, sizeof(struct uniform_sort
),
2937 for (i
= 0; i
< num_uniforms
; i
++) {
2938 struct gl_uniform
*uniform
= sorted_uniforms
[i
].u
;
2939 int parameter_index
= sorted_uniforms
[i
].pos
;
2940 const glsl_type
*type
= uniform
->Type
;
2943 if (type
->is_vector() ||
2944 type
->is_scalar()) {
2945 size
= type
->vector_elements
;
2947 size
= type_size(type
) * 4;
2950 gl_register_file file
;
2951 if (type
->is_sampler() ||
2952 (type
->is_array() && type
->fields
.array
->is_sampler())) {
2953 file
= PROGRAM_SAMPLER
;
2955 file
= PROGRAM_UNIFORM
;
2958 GLint index
= _mesa_lookup_parameter_index(prog
->Parameters
, -1,
2962 index
= _mesa_add_parameter(prog
->Parameters
, file
,
2963 uniform
->Name
, size
, type
->gl_type
,
2966 /* Sampler uniform values are stored in prog->SamplerUnits,
2967 * and the entry in that array is selected by this index we
2968 * store in ParameterValues[].
2970 if (file
== PROGRAM_SAMPLER
) {
2971 for (unsigned int j
= 0; j
< size
/ 4; j
++)
2972 prog
->Parameters
->ParameterValues
[index
+ j
][0].f
= next_sampler
++;
2975 /* The location chosen in the Parameters list here (returned
2976 * from _mesa_add_uniform) has to match what the linker chose.
2978 if (index
!= parameter_index
) {
2979 fail_link(shader_program
, "Allocation of uniform `%s' to target "
2980 "failed (%d vs %d)\n",
2981 uniform
->Name
, index
, parameter_index
);
2986 ralloc_free(sorted_uniforms
);
2990 set_uniform_initializer(struct gl_context
*ctx
, void *mem_ctx
,
2991 struct gl_shader_program
*shader_program
,
2992 const char *name
, const glsl_type
*type
,
2995 if (type
->is_record()) {
2996 ir_constant
*field_constant
;
2998 field_constant
= (ir_constant
*)val
->components
.get_head();
3000 for (unsigned int i
= 0; i
< type
->length
; i
++) {
3001 const glsl_type
*field_type
= type
->fields
.structure
[i
].type
;
3002 const char *field_name
= ralloc_asprintf(mem_ctx
, "%s.%s", name
,
3003 type
->fields
.structure
[i
].name
);
3004 set_uniform_initializer(ctx
, mem_ctx
, shader_program
, field_name
,
3005 field_type
, field_constant
);
3006 field_constant
= (ir_constant
*)field_constant
->next
;
3011 int loc
= _mesa_get_uniform_location(ctx
, shader_program
, name
);
3014 fail_link(shader_program
,
3015 "Couldn't find uniform for initializer %s\n", name
);
3019 for (unsigned int i
= 0; i
< (type
->is_array() ? type
->length
: 1); i
++) {
3020 ir_constant
*element
;
3021 const glsl_type
*element_type
;
3022 if (type
->is_array()) {
3023 element
= val
->array_elements
[i
];
3024 element_type
= type
->fields
.array
;
3027 element_type
= type
;
3032 if (element_type
->base_type
== GLSL_TYPE_BOOL
) {
3033 int *conv
= ralloc_array(mem_ctx
, int, element_type
->components());
3034 for (unsigned int j
= 0; j
< element_type
->components(); j
++) {
3035 conv
[j
] = element
->value
.b
[j
];
3037 values
= (void *)conv
;
3038 element_type
= glsl_type::get_instance(GLSL_TYPE_INT
,
3039 element_type
->vector_elements
,
3042 values
= &element
->value
;
3045 if (element_type
->is_matrix()) {
3046 _mesa_uniform_matrix(ctx
, shader_program
,
3047 element_type
->matrix_columns
,
3048 element_type
->vector_elements
,
3049 loc
, 1, GL_FALSE
, (GLfloat
*)values
);
3050 loc
+= element_type
->matrix_columns
;
3052 _mesa_uniform(ctx
, shader_program
, loc
, element_type
->matrix_columns
,
3053 values
, element_type
->gl_type
);
3054 loc
+= type_size(element_type
);
3060 * Scan/rewrite program to remove reads of custom (output) registers.
3061 * The passed type has to be either PROGRAM_OUTPUT or PROGRAM_VARYING
3062 * (for vertex shaders).
3063 * In GLSL shaders, varying vars can be read and written.
3064 * On some hardware, trying to read an output register causes trouble.
3065 * So, rewrite the program to use a temporary register in this case.
3067 * Based on _mesa_remove_output_reads from programopt.c.
3070 glsl_to_tgsi_visitor::remove_output_reads(gl_register_file type
)
3073 GLint outputMap
[VERT_RESULT_MAX
];
3074 GLint outputTypes
[VERT_RESULT_MAX
];
3075 GLuint numVaryingReads
= 0;
3076 GLboolean usedTemps
[MAX_TEMPS
];
3077 GLuint firstTemp
= 0;
3079 _mesa_find_used_registers(prog
, PROGRAM_TEMPORARY
,
3080 usedTemps
, MAX_TEMPS
);
3082 assert(type
== PROGRAM_VARYING
|| type
== PROGRAM_OUTPUT
);
3083 assert(prog
->Target
== GL_VERTEX_PROGRAM_ARB
|| type
!= PROGRAM_VARYING
);
3085 for (i
= 0; i
< VERT_RESULT_MAX
; i
++)
3088 /* look for instructions which read from varying vars */
3089 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3090 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3091 const GLuint numSrc
= num_inst_src_regs(inst
->op
);
3093 for (j
= 0; j
< numSrc
; j
++) {
3094 if (inst
->src
[j
].file
== type
) {
3095 /* replace the read with a temp reg */
3096 const GLuint var
= inst
->src
[j
].index
;
3097 if (outputMap
[var
] == -1) {
3099 outputMap
[var
] = _mesa_find_free_register(usedTemps
,
3102 outputTypes
[var
] = inst
->src
[j
].type
;
3103 firstTemp
= outputMap
[var
] + 1;
3105 inst
->src
[j
].file
= PROGRAM_TEMPORARY
;
3106 inst
->src
[j
].index
= outputMap
[var
];
3111 if (numVaryingReads
== 0)
3112 return; /* nothing to be done */
3114 /* look for instructions which write to the varying vars identified above */
3115 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3116 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3117 if (inst
->dst
.file
== type
&& outputMap
[inst
->dst
.index
] >= 0) {
3118 /* change inst to write to the temp reg, instead of the varying */
3119 inst
->dst
.file
= PROGRAM_TEMPORARY
;
3120 inst
->dst
.index
= outputMap
[inst
->dst
.index
];
3124 /* insert new MOV instructions at the end */
3125 for (i
= 0; i
< VERT_RESULT_MAX
; i
++) {
3126 if (outputMap
[i
] >= 0) {
3127 /* MOV VAR[i], TEMP[tmp]; */
3128 st_src_reg src
= st_src_reg(PROGRAM_TEMPORARY
, outputMap
[i
], outputTypes
[i
]);
3129 st_dst_reg dst
= st_dst_reg(type
, WRITEMASK_XYZW
, outputTypes
[i
]);
3131 this->emit(NULL
, TGSI_OPCODE_MOV
, dst
, src
);
3137 * Returns the mask of channels (bitmask of WRITEMASK_X,Y,Z,W) which
3138 * are read from the given src in this instruction
3141 get_src_arg_mask(st_dst_reg dst
, st_src_reg src
)
3143 int read_mask
= 0, comp
;
3145 /* Now, given the src swizzle and the written channels, find which
3146 * components are actually read
3148 for (comp
= 0; comp
< 4; ++comp
) {
3149 const unsigned coord
= GET_SWZ(src
.swizzle
, comp
);
3151 if (dst
.writemask
& (1 << comp
) && coord
<= SWIZZLE_W
)
3152 read_mask
|= 1 << coord
;
3159 * This pass replaces CMP T0, T1 T2 T0 with MOV T0, T2 when the CMP
3160 * instruction is the first instruction to write to register T0. There are
3161 * several lowering passes done in GLSL IR (e.g. branches and
3162 * relative addressing) that create a large number of conditional assignments
3163 * that ir_to_mesa converts to CMP instructions like the one mentioned above.
3165 * Here is why this conversion is safe:
3166 * CMP T0, T1 T2 T0 can be expanded to:
3172 * If (T1 < 0.0) evaluates to true then our replacement MOV T0, T2 is the same
3173 * as the original program. If (T1 < 0.0) evaluates to false, executing
3174 * MOV T0, T0 will store a garbage value in T0 since T0 is uninitialized.
3175 * Therefore, it doesn't matter that we are replacing MOV T0, T0 with MOV T0, T2
3176 * because any instruction that was going to read from T0 after this was going
3177 * to read a garbage value anyway.
3180 glsl_to_tgsi_visitor::simplify_cmp(void)
3182 unsigned tempWrites
[MAX_TEMPS
];
3183 unsigned outputWrites
[MAX_PROGRAM_OUTPUTS
];
3185 memset(tempWrites
, 0, sizeof(tempWrites
));
3186 memset(outputWrites
, 0, sizeof(outputWrites
));
3188 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3189 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3190 unsigned prevWriteMask
= 0;
3192 /* Give up if we encounter relative addressing or flow control. */
3193 if (inst
->dst
.reladdr
||
3194 tgsi_get_opcode_info(inst
->op
)->is_branch
||
3195 inst
->op
== TGSI_OPCODE_BGNSUB
||
3196 inst
->op
== TGSI_OPCODE_CONT
||
3197 inst
->op
== TGSI_OPCODE_END
||
3198 inst
->op
== TGSI_OPCODE_ENDSUB
||
3199 inst
->op
== TGSI_OPCODE_RET
) {
3203 if (inst
->dst
.file
== PROGRAM_OUTPUT
) {
3204 assert(inst
->dst
.index
< MAX_PROGRAM_OUTPUTS
);
3205 prevWriteMask
= outputWrites
[inst
->dst
.index
];
3206 outputWrites
[inst
->dst
.index
] |= inst
->dst
.writemask
;
3207 } else if (inst
->dst
.file
== PROGRAM_TEMPORARY
) {
3208 assert(inst
->dst
.index
< MAX_TEMPS
);
3209 prevWriteMask
= tempWrites
[inst
->dst
.index
];
3210 tempWrites
[inst
->dst
.index
] |= inst
->dst
.writemask
;
3213 /* For a CMP to be considered a conditional write, the destination
3214 * register and source register two must be the same. */
3215 if (inst
->op
== TGSI_OPCODE_CMP
3216 && !(inst
->dst
.writemask
& prevWriteMask
)
3217 && inst
->src
[2].file
== inst
->dst
.file
3218 && inst
->src
[2].index
== inst
->dst
.index
3219 && inst
->dst
.writemask
== get_src_arg_mask(inst
->dst
, inst
->src
[2])) {
3221 inst
->op
= TGSI_OPCODE_MOV
;
3222 inst
->src
[0] = inst
->src
[1];
3227 /* Replaces all references to a temporary register index with another index. */
3229 glsl_to_tgsi_visitor::rename_temp_register(int index
, int new_index
)
3231 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3232 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3235 for (j
=0; j
< num_inst_src_regs(inst
->op
); j
++) {
3236 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
&&
3237 inst
->src
[j
].index
== index
) {
3238 inst
->src
[j
].index
= new_index
;
3242 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== index
) {
3243 inst
->dst
.index
= new_index
;
3249 glsl_to_tgsi_visitor::get_first_temp_read(int index
)
3251 int depth
= 0; /* loop depth */
3252 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
3255 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3256 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3258 for (j
=0; j
< num_inst_src_regs(inst
->op
); j
++) {
3259 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
&&
3260 inst
->src
[j
].index
== index
) {
3261 return (depth
== 0) ? i
: loop_start
;
3265 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
3268 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
3281 glsl_to_tgsi_visitor::get_first_temp_write(int index
)
3283 int depth
= 0; /* loop depth */
3284 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
3287 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3288 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3290 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== index
) {
3291 return (depth
== 0) ? i
: loop_start
;
3294 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
3297 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
3310 glsl_to_tgsi_visitor::get_last_temp_read(int index
)
3312 int depth
= 0; /* loop depth */
3313 int last
= -1; /* index of last instruction that reads the temporary */
3316 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3317 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3319 for (j
=0; j
< num_inst_src_regs(inst
->op
); j
++) {
3320 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
&&
3321 inst
->src
[j
].index
== index
) {
3322 last
= (depth
== 0) ? i
: -2;
3326 if (inst
->op
== TGSI_OPCODE_BGNLOOP
)
3328 else if (inst
->op
== TGSI_OPCODE_ENDLOOP
)
3329 if (--depth
== 0 && last
== -2)
3341 glsl_to_tgsi_visitor::get_last_temp_write(int index
)
3343 int depth
= 0; /* loop depth */
3344 int last
= -1; /* index of last instruction that writes to the temporary */
3347 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3348 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3350 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== index
)
3351 last
= (depth
== 0) ? i
: -2;
3353 if (inst
->op
== TGSI_OPCODE_BGNLOOP
)
3355 else if (inst
->op
== TGSI_OPCODE_ENDLOOP
)
3356 if (--depth
== 0 && last
== -2)
3368 * On a basic block basis, tracks available PROGRAM_TEMPORARY register
3369 * channels for copy propagation and updates following instructions to
3370 * use the original versions.
3372 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
3373 * will occur. As an example, a TXP production before this pass:
3375 * 0: MOV TEMP[1], INPUT[4].xyyy;
3376 * 1: MOV TEMP[1].w, INPUT[4].wwww;
3377 * 2: TXP TEMP[2], TEMP[1], texture[0], 2D;
3381 * 0: MOV TEMP[1], INPUT[4].xyyy;
3382 * 1: MOV TEMP[1].w, INPUT[4].wwww;
3383 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
3385 * which allows for dead code elimination on TEMP[1]'s writes.
3388 glsl_to_tgsi_visitor::copy_propagate(void)
3390 glsl_to_tgsi_instruction
**acp
= rzalloc_array(mem_ctx
,
3391 glsl_to_tgsi_instruction
*,
3392 this->next_temp
* 4);
3393 int *acp_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
3396 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3397 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3399 assert(inst
->dst
.file
!= PROGRAM_TEMPORARY
3400 || inst
->dst
.index
< this->next_temp
);
3402 /* First, do any copy propagation possible into the src regs. */
3403 for (int r
= 0; r
< 3; r
++) {
3404 glsl_to_tgsi_instruction
*first
= NULL
;
3406 int acp_base
= inst
->src
[r
].index
* 4;
3408 if (inst
->src
[r
].file
!= PROGRAM_TEMPORARY
||
3409 inst
->src
[r
].reladdr
)
3412 /* See if we can find entries in the ACP consisting of MOVs
3413 * from the same src register for all the swizzled channels
3414 * of this src register reference.
3416 for (int i
= 0; i
< 4; i
++) {
3417 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
3418 glsl_to_tgsi_instruction
*copy_chan
= acp
[acp_base
+ src_chan
];
3425 assert(acp_level
[acp_base
+ src_chan
] <= level
);
3430 if (first
->src
[0].file
!= copy_chan
->src
[0].file
||
3431 first
->src
[0].index
!= copy_chan
->src
[0].index
) {
3439 /* We've now validated that we can copy-propagate to
3440 * replace this src register reference. Do it.
3442 inst
->src
[r
].file
= first
->src
[0].file
;
3443 inst
->src
[r
].index
= first
->src
[0].index
;
3446 for (int i
= 0; i
< 4; i
++) {
3447 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
3448 glsl_to_tgsi_instruction
*copy_inst
= acp
[acp_base
+ src_chan
];
3449 swizzle
|= (GET_SWZ(copy_inst
->src
[0].swizzle
, src_chan
) <<
3452 inst
->src
[r
].swizzle
= swizzle
;
3457 case TGSI_OPCODE_BGNLOOP
:
3458 case TGSI_OPCODE_ENDLOOP
:
3459 /* End of a basic block, clear the ACP entirely. */
3460 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
3463 case TGSI_OPCODE_IF
:
3467 case TGSI_OPCODE_ENDIF
:
3468 case TGSI_OPCODE_ELSE
:
3469 /* Clear all channels written inside the block from the ACP, but
3470 * leaving those that were not touched.
3472 for (int r
= 0; r
< this->next_temp
; r
++) {
3473 for (int c
= 0; c
< 4; c
++) {
3474 if (!acp
[4 * r
+ c
])
3477 if (acp_level
[4 * r
+ c
] >= level
)
3478 acp
[4 * r
+ c
] = NULL
;
3481 if (inst
->op
== TGSI_OPCODE_ENDIF
)
3486 /* Continuing the block, clear any written channels from
3489 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.reladdr
) {
3490 /* Any temporary might be written, so no copy propagation
3491 * across this instruction.
3493 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
3494 } else if (inst
->dst
.file
== PROGRAM_OUTPUT
&&
3495 inst
->dst
.reladdr
) {
3496 /* Any output might be written, so no copy propagation
3497 * from outputs across this instruction.
3499 for (int r
= 0; r
< this->next_temp
; r
++) {
3500 for (int c
= 0; c
< 4; c
++) {
3501 if (!acp
[4 * r
+ c
])
3504 if (acp
[4 * r
+ c
]->src
[0].file
== PROGRAM_OUTPUT
)
3505 acp
[4 * r
+ c
] = NULL
;
3508 } else if (inst
->dst
.file
== PROGRAM_TEMPORARY
||
3509 inst
->dst
.file
== PROGRAM_OUTPUT
) {
3510 /* Clear where it's used as dst. */
3511 if (inst
->dst
.file
== PROGRAM_TEMPORARY
) {
3512 for (int c
= 0; c
< 4; c
++) {
3513 if (inst
->dst
.writemask
& (1 << c
)) {
3514 acp
[4 * inst
->dst
.index
+ c
] = NULL
;
3519 /* Clear where it's used as src. */
3520 for (int r
= 0; r
< this->next_temp
; r
++) {
3521 for (int c
= 0; c
< 4; c
++) {
3522 if (!acp
[4 * r
+ c
])
3525 int src_chan
= GET_SWZ(acp
[4 * r
+ c
]->src
[0].swizzle
, c
);
3527 if (acp
[4 * r
+ c
]->src
[0].file
== inst
->dst
.file
&&
3528 acp
[4 * r
+ c
]->src
[0].index
== inst
->dst
.index
&&
3529 inst
->dst
.writemask
& (1 << src_chan
))
3531 acp
[4 * r
+ c
] = NULL
;
3539 /* If this is a copy, add it to the ACP. */
3540 if (inst
->op
== TGSI_OPCODE_MOV
&&
3541 inst
->dst
.file
== PROGRAM_TEMPORARY
&&
3542 !inst
->dst
.reladdr
&&
3544 !inst
->src
[0].reladdr
&&
3545 !inst
->src
[0].negate
) {
3546 for (int i
= 0; i
< 4; i
++) {
3547 if (inst
->dst
.writemask
& (1 << i
)) {
3548 acp
[4 * inst
->dst
.index
+ i
] = inst
;
3549 acp_level
[4 * inst
->dst
.index
+ i
] = level
;
3555 ralloc_free(acp_level
);
3560 * Tracks available PROGRAM_TEMPORARY registers for dead code elimination.
3562 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
3563 * will occur. As an example, a TXP production after copy propagation but
3566 * 0: MOV TEMP[1], INPUT[4].xyyy;
3567 * 1: MOV TEMP[1].w, INPUT[4].wwww;
3568 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
3570 * and after this pass:
3572 * 0: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
3574 * FIXME: assumes that all functions are inlined (no support for BGNSUB/ENDSUB)
3575 * FIXME: doesn't eliminate all dead code inside of loops; it steps around them
3578 glsl_to_tgsi_visitor::eliminate_dead_code(void)
3582 for (i
=0; i
< this->next_temp
; i
++) {
3583 int last_read
= get_last_temp_read(i
);
3586 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3587 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3589 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== i
&&
3602 * On a basic block basis, tracks available PROGRAM_TEMPORARY registers for dead
3603 * code elimination. This is less primitive than eliminate_dead_code(), as it
3604 * is per-channel and can detect consecutive writes without a read between them
3605 * as dead code. However, there is some dead code that can be eliminated by
3606 * eliminate_dead_code() but not this function - for example, this function
3607 * cannot eliminate an instruction writing to a register that is never read and
3608 * is the only instruction writing to that register.
3610 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
3614 glsl_to_tgsi_visitor::eliminate_dead_code_advanced(void)
3616 glsl_to_tgsi_instruction
**writes
= rzalloc_array(mem_ctx
,
3617 glsl_to_tgsi_instruction
*,
3618 this->next_temp
* 4);
3619 int *write_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
3623 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3624 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3626 assert(inst
->dst
.file
!= PROGRAM_TEMPORARY
3627 || inst
->dst
.index
< this->next_temp
);
3630 case TGSI_OPCODE_BGNLOOP
:
3631 case TGSI_OPCODE_ENDLOOP
:
3632 /* End of a basic block, clear the write array entirely.
3633 * FIXME: This keeps us from killing dead code when the writes are
3634 * on either side of a loop, even when the register isn't touched
3637 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
3640 case TGSI_OPCODE_ENDIF
:
3644 case TGSI_OPCODE_ELSE
:
3645 /* Clear all channels written inside the preceding if block from the
3646 * write array, but leave those that were not touched.
3648 * FIXME: This destroys opportunities to remove dead code inside of
3649 * IF blocks that are followed by an ELSE block.
3651 for (int r
= 0; r
< this->next_temp
; r
++) {
3652 for (int c
= 0; c
< 4; c
++) {
3653 if (!writes
[4 * r
+ c
])
3656 if (write_level
[4 * r
+ c
] >= level
)
3657 writes
[4 * r
+ c
] = NULL
;
3662 case TGSI_OPCODE_IF
:
3664 /* fallthrough to default case to mark the condition as read */
3667 /* Continuing the block, clear any channels from the write array that
3668 * are read by this instruction.
3670 for (unsigned i
= 0; i
< Elements(inst
->src
); i
++) {
3671 if (inst
->src
[i
].file
== PROGRAM_TEMPORARY
&& inst
->src
[i
].reladdr
){
3672 /* Any temporary might be read, so no dead code elimination
3673 * across this instruction.
3675 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
3676 } else if (inst
->src
[i
].file
== PROGRAM_TEMPORARY
) {
3677 /* Clear where it's used as src. */
3678 int src_chans
= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 0);
3679 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 1);
3680 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 2);
3681 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 3);
3683 for (int c
= 0; c
< 4; c
++) {
3684 if (src_chans
& (1 << c
)) {
3685 writes
[4 * inst
->src
[i
].index
+ c
] = NULL
;
3693 /* If this instruction writes to a temporary, add it to the write array.
3694 * If there is already an instruction in the write array for one or more
3695 * of the channels, flag that channel write as dead.
3697 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&&
3698 !inst
->dst
.reladdr
&&
3700 for (int c
= 0; c
< 4; c
++) {
3701 if (inst
->dst
.writemask
& (1 << c
)) {
3702 if (writes
[4 * inst
->dst
.index
+ c
]) {
3703 if (write_level
[4 * inst
->dst
.index
+ c
] < level
)
3706 writes
[4 * inst
->dst
.index
+ c
]->dead_mask
|= (1 << c
);
3708 writes
[4 * inst
->dst
.index
+ c
] = inst
;
3709 write_level
[4 * inst
->dst
.index
+ c
] = level
;
3715 /* Anything still in the write array at this point is dead code. */
3716 for (int r
= 0; r
< this->next_temp
; r
++) {
3717 for (int c
= 0; c
< 4; c
++) {
3718 glsl_to_tgsi_instruction
*inst
= writes
[4 * r
+ c
];
3720 inst
->dead_mask
|= (1 << c
);
3724 /* Now actually remove the instructions that are completely dead and update
3725 * the writemask of other instructions with dead channels.
3727 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3728 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3730 if (!inst
->dead_mask
|| !inst
->dst
.writemask
)
3732 else if (inst
->dead_mask
== inst
->dst
.writemask
) {
3737 inst
->dst
.writemask
&= ~(inst
->dead_mask
);
3740 ralloc_free(write_level
);
3741 ralloc_free(writes
);
3746 /* Merges temporary registers together where possible to reduce the number of
3747 * registers needed to run a program.
3749 * Produces optimal code only after copy propagation and dead code elimination
3752 glsl_to_tgsi_visitor::merge_registers(void)
3754 int *last_reads
= rzalloc_array(mem_ctx
, int, this->next_temp
);
3755 int *first_writes
= rzalloc_array(mem_ctx
, int, this->next_temp
);
3758 /* Read the indices of the last read and first write to each temp register
3759 * into an array so that we don't have to traverse the instruction list as
3761 for (i
=0; i
< this->next_temp
; i
++) {
3762 last_reads
[i
] = get_last_temp_read(i
);
3763 first_writes
[i
] = get_first_temp_write(i
);
3766 /* Start looking for registers with non-overlapping usages that can be
3767 * merged together. */
3768 for (i
=0; i
< this->next_temp
; i
++) {
3769 /* Don't touch unused registers. */
3770 if (last_reads
[i
] < 0 || first_writes
[i
] < 0) continue;
3772 for (j
=0; j
< this->next_temp
; j
++) {
3773 /* Don't touch unused registers. */
3774 if (last_reads
[j
] < 0 || first_writes
[j
] < 0) continue;
3776 /* We can merge the two registers if the first write to j is after or
3777 * in the same instruction as the last read from i. Note that the
3778 * register at index i will always be used earlier or at the same time
3779 * as the register at index j. */
3780 if (first_writes
[i
] <= first_writes
[j
] &&
3781 last_reads
[i
] <= first_writes
[j
])
3783 rename_temp_register(j
, i
); /* Replace all references to j with i.*/
3785 /* Update the first_writes and last_reads arrays with the new
3786 * values for the merged register index, and mark the newly unused
3787 * register index as such. */
3788 last_reads
[i
] = last_reads
[j
];
3789 first_writes
[j
] = -1;
3795 ralloc_free(last_reads
);
3796 ralloc_free(first_writes
);
3799 /* Reassign indices to temporary registers by reusing unused indices created
3800 * by optimization passes. */
3802 glsl_to_tgsi_visitor::renumber_registers(void)
3807 for (i
=0; i
< this->next_temp
; i
++) {
3808 if (get_first_temp_read(i
) < 0) continue;
3810 rename_temp_register(i
, new_index
);
3814 this->next_temp
= new_index
;
3818 * Returns a fragment program which implements the current pixel transfer ops.
3819 * Based on get_pixel_transfer_program in st_atom_pixeltransfer.c.
3822 get_pixel_transfer_visitor(struct st_fragment_program
*fp
,
3823 glsl_to_tgsi_visitor
*original
,
3824 int scale_and_bias
, int pixel_maps
)
3826 glsl_to_tgsi_visitor
*v
= new glsl_to_tgsi_visitor();
3827 struct st_context
*st
= st_context(original
->ctx
);
3828 struct gl_program
*prog
= &fp
->Base
.Base
;
3829 struct gl_program_parameter_list
*params
= _mesa_new_parameter_list();
3830 st_src_reg coord
, src0
;
3832 glsl_to_tgsi_instruction
*inst
;
3834 /* Copy attributes of the glsl_to_tgsi_visitor in the original shader. */
3835 v
->ctx
= original
->ctx
;
3837 v
->glsl_version
= original
->glsl_version
;
3838 v
->native_integers
= original
->native_integers
;
3839 v
->options
= original
->options
;
3840 v
->next_temp
= original
->next_temp
;
3841 v
->num_address_regs
= original
->num_address_regs
;
3842 v
->samplers_used
= prog
->SamplersUsed
= original
->samplers_used
;
3843 v
->indirect_addr_temps
= original
->indirect_addr_temps
;
3844 v
->indirect_addr_consts
= original
->indirect_addr_consts
;
3845 memcpy(&v
->immediates
, &original
->immediates
, sizeof(v
->immediates
));
3848 * Get initial pixel color from the texture.
3849 * TEX colorTemp, fragment.texcoord[0], texture[0], 2D;
3851 coord
= st_src_reg(PROGRAM_INPUT
, FRAG_ATTRIB_TEX0
, glsl_type::vec2_type
);
3852 src0
= v
->get_temp(glsl_type::vec4_type
);
3853 dst0
= st_dst_reg(src0
);
3854 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, dst0
, coord
);
3856 inst
->tex_target
= TEXTURE_2D_INDEX
;
3858 prog
->InputsRead
|= (1 << FRAG_ATTRIB_TEX0
);
3859 prog
->SamplersUsed
|= (1 << 0); /* mark sampler 0 as used */
3860 v
->samplers_used
|= (1 << 0);
3862 if (scale_and_bias
) {
3863 static const gl_state_index scale_state
[STATE_LENGTH
] =
3864 { STATE_INTERNAL
, STATE_PT_SCALE
,
3865 (gl_state_index
) 0, (gl_state_index
) 0, (gl_state_index
) 0 };
3866 static const gl_state_index bias_state
[STATE_LENGTH
] =
3867 { STATE_INTERNAL
, STATE_PT_BIAS
,
3868 (gl_state_index
) 0, (gl_state_index
) 0, (gl_state_index
) 0 };
3869 GLint scale_p
, bias_p
;
3870 st_src_reg scale
, bias
;
3872 scale_p
= _mesa_add_state_reference(params
, scale_state
);
3873 bias_p
= _mesa_add_state_reference(params
, bias_state
);
3875 /* MAD colorTemp, colorTemp, scale, bias; */
3876 scale
= st_src_reg(PROGRAM_STATE_VAR
, scale_p
, GLSL_TYPE_FLOAT
);
3877 bias
= st_src_reg(PROGRAM_STATE_VAR
, bias_p
, GLSL_TYPE_FLOAT
);
3878 inst
= v
->emit(NULL
, TGSI_OPCODE_MAD
, dst0
, src0
, scale
, bias
);
3882 st_src_reg temp
= v
->get_temp(glsl_type::vec4_type
);
3883 st_dst_reg temp_dst
= st_dst_reg(temp
);
3885 assert(st
->pixel_xfer
.pixelmap_texture
);
3887 /* With a little effort, we can do four pixel map look-ups with
3888 * two TEX instructions:
3891 /* TEX temp.rg, colorTemp.rgba, texture[1], 2D; */
3892 temp_dst
.writemask
= WRITEMASK_XY
; /* write R,G */
3893 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, temp_dst
, src0
);
3895 inst
->tex_target
= TEXTURE_2D_INDEX
;
3897 /* TEX temp.ba, colorTemp.baba, texture[1], 2D; */
3898 src0
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_Z
, SWIZZLE_W
, SWIZZLE_Z
, SWIZZLE_W
);
3899 temp_dst
.writemask
= WRITEMASK_ZW
; /* write B,A */
3900 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, temp_dst
, src0
);
3902 inst
->tex_target
= TEXTURE_2D_INDEX
;
3904 prog
->SamplersUsed
|= (1 << 1); /* mark sampler 1 as used */
3905 v
->samplers_used
|= (1 << 1);
3907 /* MOV colorTemp, temp; */
3908 inst
= v
->emit(NULL
, TGSI_OPCODE_MOV
, dst0
, temp
);
3911 /* Now copy the instructions from the original glsl_to_tgsi_visitor into the
3913 foreach_iter(exec_list_iterator
, iter
, original
->instructions
) {
3914 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3915 st_src_reg src_regs
[3];
3917 if (inst
->dst
.file
== PROGRAM_OUTPUT
)
3918 prog
->OutputsWritten
|= BITFIELD64_BIT(inst
->dst
.index
);
3920 for (int i
=0; i
<3; i
++) {
3921 src_regs
[i
] = inst
->src
[i
];
3922 if (src_regs
[i
].file
== PROGRAM_INPUT
&&
3923 src_regs
[i
].index
== FRAG_ATTRIB_COL0
)
3925 src_regs
[i
].file
= PROGRAM_TEMPORARY
;
3926 src_regs
[i
].index
= src0
.index
;
3928 else if (src_regs
[i
].file
== PROGRAM_INPUT
)
3929 prog
->InputsRead
|= (1 << src_regs
[i
].index
);
3932 v
->emit(NULL
, inst
->op
, inst
->dst
, src_regs
[0], src_regs
[1], src_regs
[2]);
3935 /* Make modifications to fragment program info. */
3936 prog
->Parameters
= _mesa_combine_parameter_lists(params
,
3937 original
->prog
->Parameters
);
3938 prog
->Attributes
= _mesa_clone_parameter_list(original
->prog
->Attributes
);
3939 prog
->Varying
= _mesa_clone_parameter_list(original
->prog
->Varying
);
3940 _mesa_free_parameter_list(params
);
3941 count_resources(v
, prog
);
3942 fp
->glsl_to_tgsi
= v
;
3946 * Make fragment program for glBitmap:
3947 * Sample the texture and kill the fragment if the bit is 0.
3948 * This program will be combined with the user's fragment program.
3950 * Based on make_bitmap_fragment_program in st_cb_bitmap.c.
3953 get_bitmap_visitor(struct st_fragment_program
*fp
,
3954 glsl_to_tgsi_visitor
*original
, int samplerIndex
)
3956 glsl_to_tgsi_visitor
*v
= new glsl_to_tgsi_visitor();
3957 struct st_context
*st
= st_context(original
->ctx
);
3958 struct gl_program
*prog
= &fp
->Base
.Base
;
3959 st_src_reg coord
, src0
;
3961 glsl_to_tgsi_instruction
*inst
;
3963 /* Copy attributes of the glsl_to_tgsi_visitor in the original shader. */
3964 v
->ctx
= original
->ctx
;
3966 v
->glsl_version
= original
->glsl_version
;
3967 v
->native_integers
= original
->native_integers
;
3968 v
->options
= original
->options
;
3969 v
->next_temp
= original
->next_temp
;
3970 v
->num_address_regs
= original
->num_address_regs
;
3971 v
->samplers_used
= prog
->SamplersUsed
= original
->samplers_used
;
3972 v
->indirect_addr_temps
= original
->indirect_addr_temps
;
3973 v
->indirect_addr_consts
= original
->indirect_addr_consts
;
3974 memcpy(&v
->immediates
, &original
->immediates
, sizeof(v
->immediates
));
3976 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
3977 coord
= st_src_reg(PROGRAM_INPUT
, FRAG_ATTRIB_TEX0
, glsl_type::vec2_type
);
3978 src0
= v
->get_temp(glsl_type::vec4_type
);
3979 dst0
= st_dst_reg(src0
);
3980 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, dst0
, coord
);
3981 inst
->sampler
= samplerIndex
;
3982 inst
->tex_target
= TEXTURE_2D_INDEX
;
3984 prog
->InputsRead
|= (1 << FRAG_ATTRIB_TEX0
);
3985 prog
->SamplersUsed
|= (1 << samplerIndex
); /* mark sampler as used */
3986 v
->samplers_used
|= (1 << samplerIndex
);
3988 /* KIL if -tmp0 < 0 # texel=0 -> keep / texel=0 -> discard */
3989 src0
.negate
= NEGATE_XYZW
;
3990 if (st
->bitmap
.tex_format
== PIPE_FORMAT_L8_UNORM
)
3991 src0
.swizzle
= SWIZZLE_XXXX
;
3992 inst
= v
->emit(NULL
, TGSI_OPCODE_KIL
, undef_dst
, src0
);
3994 /* Now copy the instructions from the original glsl_to_tgsi_visitor into the
3996 foreach_iter(exec_list_iterator
, iter
, original
->instructions
) {
3997 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3998 st_src_reg src_regs
[3];
4000 if (inst
->dst
.file
== PROGRAM_OUTPUT
)
4001 prog
->OutputsWritten
|= BITFIELD64_BIT(inst
->dst
.index
);
4003 for (int i
=0; i
<3; i
++) {
4004 src_regs
[i
] = inst
->src
[i
];
4005 if (src_regs
[i
].file
== PROGRAM_INPUT
)
4006 prog
->InputsRead
|= (1 << src_regs
[i
].index
);
4009 v
->emit(NULL
, inst
->op
, inst
->dst
, src_regs
[0], src_regs
[1], src_regs
[2]);
4012 /* Make modifications to fragment program info. */
4013 prog
->Parameters
= _mesa_clone_parameter_list(original
->prog
->Parameters
);
4014 prog
->Attributes
= _mesa_clone_parameter_list(original
->prog
->Attributes
);
4015 prog
->Varying
= _mesa_clone_parameter_list(original
->prog
->Varying
);
4016 count_resources(v
, prog
);
4017 fp
->glsl_to_tgsi
= v
;
4020 /* ------------------------- TGSI conversion stuff -------------------------- */
4022 unsigned branch_target
;
4027 * Intermediate state used during shader translation.
4029 struct st_translate
{
4030 struct ureg_program
*ureg
;
4032 struct ureg_dst temps
[MAX_TEMPS
];
4033 struct ureg_src
*constants
;
4034 struct ureg_src
*immediates
;
4035 struct ureg_dst outputs
[PIPE_MAX_SHADER_OUTPUTS
];
4036 struct ureg_src inputs
[PIPE_MAX_SHADER_INPUTS
];
4037 struct ureg_dst address
[1];
4038 struct ureg_src samplers
[PIPE_MAX_SAMPLERS
];
4039 struct ureg_src systemValues
[SYSTEM_VALUE_MAX
];
4041 /* Extra info for handling point size clamping in vertex shader */
4042 struct ureg_dst pointSizeResult
; /**< Actual point size output register */
4043 struct ureg_src pointSizeConst
; /**< Point size range constant register */
4044 GLint pointSizeOutIndex
; /**< Temp point size output register */
4045 GLboolean prevInstWrotePointSize
;
4047 const GLuint
*inputMapping
;
4048 const GLuint
*outputMapping
;
4050 /* For every instruction that contains a label (eg CALL), keep
4051 * details so that we can go back afterwards and emit the correct
4052 * tgsi instruction number for each label.
4054 struct label
*labels
;
4055 unsigned labels_size
;
4056 unsigned labels_count
;
4058 /* Keep a record of the tgsi instruction number that each mesa
4059 * instruction starts at, will be used to fix up labels after
4064 unsigned insn_count
;
4066 unsigned procType
; /**< TGSI_PROCESSOR_VERTEX/FRAGMENT */
4071 /** Map Mesa's SYSTEM_VALUE_x to TGSI_SEMANTIC_x */
4072 static unsigned mesa_sysval_to_semantic
[SYSTEM_VALUE_MAX
] = {
4074 TGSI_SEMANTIC_INSTANCEID
4078 * Make note of a branch to a label in the TGSI code.
4079 * After we've emitted all instructions, we'll go over the list
4080 * of labels built here and patch the TGSI code with the actual
4081 * location of each label.
4083 static unsigned *get_label(struct st_translate
*t
, unsigned branch_target
)
4087 if (t
->labels_count
+ 1 >= t
->labels_size
) {
4088 t
->labels_size
= 1 << (util_logbase2(t
->labels_size
) + 1);
4089 t
->labels
= (struct label
*)realloc(t
->labels
,
4090 t
->labels_size
* sizeof(struct label
));
4091 if (t
->labels
== NULL
) {
4092 static unsigned dummy
;
4098 i
= t
->labels_count
++;
4099 t
->labels
[i
].branch_target
= branch_target
;
4100 return &t
->labels
[i
].token
;
4104 * Called prior to emitting the TGSI code for each instruction.
4105 * Allocate additional space for instructions if needed.
4106 * Update the insn[] array so the next glsl_to_tgsi_instruction points to
4107 * the next TGSI instruction.
4109 static void set_insn_start(struct st_translate
*t
, unsigned start
)
4111 if (t
->insn_count
+ 1 >= t
->insn_size
) {
4112 t
->insn_size
= 1 << (util_logbase2(t
->insn_size
) + 1);
4113 t
->insn
= (unsigned *)realloc(t
->insn
, t
->insn_size
* sizeof(t
->insn
[0]));
4114 if (t
->insn
== NULL
) {
4120 t
->insn
[t
->insn_count
++] = start
;
4124 * Map a glsl_to_tgsi constant/immediate to a TGSI immediate.
4126 static struct ureg_src
4127 emit_immediate(struct st_translate
*t
,
4128 gl_constant_value values
[4],
4131 struct ureg_program
*ureg
= t
->ureg
;
4136 return ureg_DECL_immediate(ureg
, &values
[0].f
, size
);
4138 return ureg_DECL_immediate_int(ureg
, &values
[0].i
, size
);
4139 case GL_UNSIGNED_INT
:
4141 return ureg_DECL_immediate_uint(ureg
, &values
[0].u
, size
);
4143 assert(!"should not get here - type must be float, int, uint, or bool");
4144 return ureg_src_undef();
4149 * Map a glsl_to_tgsi dst register to a TGSI ureg_dst register.
4151 static struct ureg_dst
4152 dst_register(struct st_translate
*t
,
4153 gl_register_file file
,
4157 case PROGRAM_UNDEFINED
:
4158 return ureg_dst_undef();
4160 case PROGRAM_TEMPORARY
:
4161 if (ureg_dst_is_undef(t
->temps
[index
]))
4162 t
->temps
[index
] = ureg_DECL_temporary(t
->ureg
);
4164 return t
->temps
[index
];
4166 case PROGRAM_OUTPUT
:
4167 if (t
->procType
== TGSI_PROCESSOR_VERTEX
&& index
== VERT_RESULT_PSIZ
)
4168 t
->prevInstWrotePointSize
= GL_TRUE
;
4170 if (t
->procType
== TGSI_PROCESSOR_VERTEX
)
4171 assert(index
< VERT_RESULT_MAX
);
4172 else if (t
->procType
== TGSI_PROCESSOR_FRAGMENT
)
4173 assert(index
< FRAG_RESULT_MAX
);
4175 assert(index
< GEOM_RESULT_MAX
);
4177 assert(t
->outputMapping
[index
] < Elements(t
->outputs
));
4179 return t
->outputs
[t
->outputMapping
[index
]];
4181 case PROGRAM_ADDRESS
:
4182 return t
->address
[index
];
4185 assert(!"unknown dst register file");
4186 return ureg_dst_undef();
4191 * Map a glsl_to_tgsi src register to a TGSI ureg_src register.
4193 static struct ureg_src
4194 src_register(struct st_translate
*t
,
4195 gl_register_file file
,
4199 case PROGRAM_UNDEFINED
:
4200 return ureg_src_undef();
4202 case PROGRAM_TEMPORARY
:
4204 assert(index
< Elements(t
->temps
));
4205 if (ureg_dst_is_undef(t
->temps
[index
]))
4206 t
->temps
[index
] = ureg_DECL_temporary(t
->ureg
);
4207 return ureg_src(t
->temps
[index
]);
4209 case PROGRAM_NAMED_PARAM
:
4210 case PROGRAM_ENV_PARAM
:
4211 case PROGRAM_LOCAL_PARAM
:
4212 case PROGRAM_UNIFORM
:
4214 return t
->constants
[index
];
4215 case PROGRAM_STATE_VAR
:
4216 case PROGRAM_CONSTANT
: /* ie, immediate */
4218 return ureg_DECL_constant(t
->ureg
, 0);
4220 return t
->constants
[index
];
4222 case PROGRAM_IMMEDIATE
:
4223 return t
->immediates
[index
];
4226 assert(t
->inputMapping
[index
] < Elements(t
->inputs
));
4227 return t
->inputs
[t
->inputMapping
[index
]];
4229 case PROGRAM_OUTPUT
:
4230 assert(t
->outputMapping
[index
] < Elements(t
->outputs
));
4231 return ureg_src(t
->outputs
[t
->outputMapping
[index
]]); /* not needed? */
4233 case PROGRAM_ADDRESS
:
4234 return ureg_src(t
->address
[index
]);
4236 case PROGRAM_SYSTEM_VALUE
:
4237 assert(index
< Elements(t
->systemValues
));
4238 return t
->systemValues
[index
];
4241 assert(!"unknown src register file");
4242 return ureg_src_undef();
4247 * Create a TGSI ureg_dst register from an st_dst_reg.
4249 static struct ureg_dst
4250 translate_dst(struct st_translate
*t
,
4251 const st_dst_reg
*dst_reg
,
4254 struct ureg_dst dst
= dst_register(t
,
4258 dst
= ureg_writemask(dst
, dst_reg
->writemask
);
4261 dst
= ureg_saturate(dst
);
4263 if (dst_reg
->reladdr
!= NULL
)
4264 dst
= ureg_dst_indirect(dst
, ureg_src(t
->address
[0]));
4270 * Create a TGSI ureg_src register from an st_src_reg.
4272 static struct ureg_src
4273 translate_src(struct st_translate
*t
, const st_src_reg
*src_reg
)
4275 struct ureg_src src
= src_register(t
, src_reg
->file
, src_reg
->index
);
4277 src
= ureg_swizzle(src
,
4278 GET_SWZ(src_reg
->swizzle
, 0) & 0x3,
4279 GET_SWZ(src_reg
->swizzle
, 1) & 0x3,
4280 GET_SWZ(src_reg
->swizzle
, 2) & 0x3,
4281 GET_SWZ(src_reg
->swizzle
, 3) & 0x3);
4283 if ((src_reg
->negate
& 0xf) == NEGATE_XYZW
)
4284 src
= ureg_negate(src
);
4286 if (src_reg
->reladdr
!= NULL
) {
4287 /* Normally ureg_src_indirect() would be used here, but a stupid compiler
4288 * bug in g++ makes ureg_src_indirect (an inline C function) erroneously
4289 * set the bit for src.Negate. So we have to do the operation manually
4290 * here to work around the compiler's problems. */
4291 /*src = ureg_src_indirect(src, ureg_src(t->address[0]));*/
4292 struct ureg_src addr
= ureg_src(t
->address
[0]);
4294 src
.IndirectFile
= addr
.File
;
4295 src
.IndirectIndex
= addr
.Index
;
4296 src
.IndirectSwizzle
= addr
.SwizzleX
;
4298 if (src_reg
->file
!= PROGRAM_INPUT
&&
4299 src_reg
->file
!= PROGRAM_OUTPUT
) {
4300 /* If src_reg->index was negative, it was set to zero in
4301 * src_register(). Reassign it now. But don't do this
4302 * for input/output regs since they get remapped while
4303 * const buffers don't.
4305 src
.Index
= src_reg
->index
;
4312 static struct tgsi_texture_offset
4313 translate_tex_offset(struct st_translate
*t
,
4314 const struct tgsi_texture_offset
*in_offset
)
4316 struct tgsi_texture_offset offset
;
4318 assert(in_offset
->File
== PROGRAM_IMMEDIATE
);
4320 offset
.File
= TGSI_FILE_IMMEDIATE
;
4321 offset
.Index
= in_offset
->Index
;
4322 offset
.SwizzleX
= in_offset
->SwizzleX
;
4323 offset
.SwizzleY
= in_offset
->SwizzleY
;
4324 offset
.SwizzleZ
= in_offset
->SwizzleZ
;
4330 compile_tgsi_instruction(struct st_translate
*t
,
4331 const glsl_to_tgsi_instruction
*inst
)
4333 struct ureg_program
*ureg
= t
->ureg
;
4335 struct ureg_dst dst
[1];
4336 struct ureg_src src
[4];
4337 struct tgsi_texture_offset texoffsets
[MAX_GLSL_TEXTURE_OFFSET
];
4342 num_dst
= num_inst_dst_regs(inst
->op
);
4343 num_src
= num_inst_src_regs(inst
->op
);
4346 dst
[0] = translate_dst(t
,
4350 for (i
= 0; i
< num_src
; i
++)
4351 src
[i
] = translate_src(t
, &inst
->src
[i
]);
4354 case TGSI_OPCODE_BGNLOOP
:
4355 case TGSI_OPCODE_CAL
:
4356 case TGSI_OPCODE_ELSE
:
4357 case TGSI_OPCODE_ENDLOOP
:
4358 case TGSI_OPCODE_IF
:
4359 assert(num_dst
== 0);
4360 ureg_label_insn(ureg
,
4364 inst
->op
== TGSI_OPCODE_CAL
? inst
->function
->sig_id
: 0));
4367 case TGSI_OPCODE_TEX
:
4368 case TGSI_OPCODE_TXB
:
4369 case TGSI_OPCODE_TXD
:
4370 case TGSI_OPCODE_TXL
:
4371 case TGSI_OPCODE_TXP
:
4372 case TGSI_OPCODE_TXQ
:
4373 case TGSI_OPCODE_TXF
:
4374 src
[num_src
++] = t
->samplers
[inst
->sampler
];
4375 for (i
= 0; i
< inst
->tex_offset_num_offset
; i
++) {
4376 texoffsets
[i
] = translate_tex_offset(t
, &inst
->tex_offsets
[i
]);
4381 translate_texture_target(inst
->tex_target
, inst
->tex_shadow
),
4382 texoffsets
, inst
->tex_offset_num_offset
,
4386 case TGSI_OPCODE_SCS
:
4387 dst
[0] = ureg_writemask(dst
[0], TGSI_WRITEMASK_XY
);
4388 ureg_insn(ureg
, inst
->op
, dst
, num_dst
, src
, num_src
);
4401 * Emit the TGSI instructions to adjust the WPOS pixel center convention
4402 * Basically, add (adjX, adjY) to the fragment position.
4405 emit_adjusted_wpos(struct st_translate
*t
,
4406 const struct gl_program
*program
,
4407 float adjX
, float adjY
)
4409 struct ureg_program
*ureg
= t
->ureg
;
4410 struct ureg_dst wpos_temp
= ureg_DECL_temporary(ureg
);
4411 struct ureg_src wpos_input
= t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_WPOS
]];
4413 /* Note that we bias X and Y and pass Z and W through unchanged.
4414 * The shader might also use gl_FragCoord.w and .z.
4416 ureg_ADD(ureg
, wpos_temp
, wpos_input
,
4417 ureg_imm4f(ureg
, adjX
, adjY
, 0.0f
, 0.0f
));
4419 t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_WPOS
]] = ureg_src(wpos_temp
);
4424 * Emit the TGSI instructions for inverting the WPOS y coordinate.
4425 * This code is unavoidable because it also depends on whether
4426 * a FBO is bound (STATE_FB_WPOS_Y_TRANSFORM).
4429 emit_wpos_inversion(struct st_translate
*t
,
4430 const struct gl_program
*program
,
4433 struct ureg_program
*ureg
= t
->ureg
;
4435 /* Fragment program uses fragment position input.
4436 * Need to replace instances of INPUT[WPOS] with temp T
4437 * where T = INPUT[WPOS] by y is inverted.
4439 static const gl_state_index wposTransformState
[STATE_LENGTH
]
4440 = { STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
,
4441 (gl_state_index
)0, (gl_state_index
)0, (gl_state_index
)0 };
4443 /* XXX: note we are modifying the incoming shader here! Need to
4444 * do this before emitting the constant decls below, or this
4447 unsigned wposTransConst
= _mesa_add_state_reference(program
->Parameters
,
4448 wposTransformState
);
4450 struct ureg_src wpostrans
= ureg_DECL_constant(ureg
, wposTransConst
);
4451 struct ureg_dst wpos_temp
;
4452 struct ureg_src wpos_input
= t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_WPOS
]];
4454 /* MOV wpos_temp, input[wpos]
4456 if (wpos_input
.File
== TGSI_FILE_TEMPORARY
)
4457 wpos_temp
= ureg_dst(wpos_input
);
4459 wpos_temp
= ureg_DECL_temporary(ureg
);
4460 ureg_MOV(ureg
, wpos_temp
, wpos_input
);
4464 /* MAD wpos_temp.y, wpos_input, wpostrans.xxxx, wpostrans.yyyy
4467 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
4469 ureg_scalar(wpostrans
, 0),
4470 ureg_scalar(wpostrans
, 1));
4472 /* MAD wpos_temp.y, wpos_input, wpostrans.zzzz, wpostrans.wwww
4475 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
4477 ureg_scalar(wpostrans
, 2),
4478 ureg_scalar(wpostrans
, 3));
4481 /* Use wpos_temp as position input from here on:
4483 t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_WPOS
]] = ureg_src(wpos_temp
);
4488 * Emit fragment position/ooordinate code.
4491 emit_wpos(struct st_context
*st
,
4492 struct st_translate
*t
,
4493 const struct gl_program
*program
,
4494 struct ureg_program
*ureg
)
4496 const struct gl_fragment_program
*fp
=
4497 (const struct gl_fragment_program
*) program
;
4498 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
4499 boolean invert
= FALSE
;
4501 if (fp
->OriginUpperLeft
) {
4502 /* Fragment shader wants origin in upper-left */
4503 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
)) {
4504 /* the driver supports upper-left origin */
4506 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
)) {
4507 /* the driver supports lower-left origin, need to invert Y */
4508 ureg_property_fs_coord_origin(ureg
, TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
4515 /* Fragment shader wants origin in lower-left */
4516 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
))
4517 /* the driver supports lower-left origin */
4518 ureg_property_fs_coord_origin(ureg
, TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
4519 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
))
4520 /* the driver supports upper-left origin, need to invert Y */
4526 if (fp
->PixelCenterInteger
) {
4527 /* Fragment shader wants pixel center integer */
4528 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
))
4529 /* the driver supports pixel center integer */
4530 ureg_property_fs_coord_pixel_center(ureg
, TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
4531 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
))
4532 /* the driver supports pixel center half integer, need to bias X,Y */
4533 emit_adjusted_wpos(t
, program
, 0.5f
, invert
? 0.5f
: -0.5f
);
4538 /* Fragment shader wants pixel center half integer */
4539 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
)) {
4540 /* the driver supports pixel center half integer */
4542 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)) {
4543 /* the driver supports pixel center integer, need to bias X,Y */
4544 ureg_property_fs_coord_pixel_center(ureg
, TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
4545 emit_adjusted_wpos(t
, program
, 0.5f
, invert
? -0.5f
: 0.5f
);
4551 /* we invert after adjustment so that we avoid the MOV to temporary,
4552 * and reuse the adjustment ADD instead */
4553 emit_wpos_inversion(t
, program
, invert
);
4557 * OpenGL's fragment gl_FrontFace input is 1 for front-facing, 0 for back.
4558 * TGSI uses +1 for front, -1 for back.
4559 * This function converts the TGSI value to the GL value. Simply clamping/
4560 * saturating the value to [0,1] does the job.
4563 emit_face_var(struct st_translate
*t
)
4565 struct ureg_program
*ureg
= t
->ureg
;
4566 struct ureg_dst face_temp
= ureg_DECL_temporary(ureg
);
4567 struct ureg_src face_input
= t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_FACE
]];
4569 /* MOV_SAT face_temp, input[face] */
4570 face_temp
= ureg_saturate(face_temp
);
4571 ureg_MOV(ureg
, face_temp
, face_input
);
4573 /* Use face_temp as face input from here on: */
4574 t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_FACE
]] = ureg_src(face_temp
);
4578 emit_edgeflags(struct st_translate
*t
)
4580 struct ureg_program
*ureg
= t
->ureg
;
4581 struct ureg_dst edge_dst
= t
->outputs
[t
->outputMapping
[VERT_RESULT_EDGE
]];
4582 struct ureg_src edge_src
= t
->inputs
[t
->inputMapping
[VERT_ATTRIB_EDGEFLAG
]];
4584 ureg_MOV(ureg
, edge_dst
, edge_src
);
4588 * Translate intermediate IR (glsl_to_tgsi_instruction) to TGSI format.
4589 * \param program the program to translate
4590 * \param numInputs number of input registers used
4591 * \param inputMapping maps Mesa fragment program inputs to TGSI generic
4593 * \param inputSemanticName the TGSI_SEMANTIC flag for each input
4594 * \param inputSemanticIndex the semantic index (ex: which texcoord) for
4596 * \param interpMode the TGSI_INTERPOLATE_LINEAR/PERSP mode for each input
4597 * \param numOutputs number of output registers used
4598 * \param outputMapping maps Mesa fragment program outputs to TGSI
4600 * \param outputSemanticName the TGSI_SEMANTIC flag for each output
4601 * \param outputSemanticIndex the semantic index (ex: which texcoord) for
4604 * \return PIPE_OK or PIPE_ERROR_OUT_OF_MEMORY
4606 extern "C" enum pipe_error
4607 st_translate_program(
4608 struct gl_context
*ctx
,
4610 struct ureg_program
*ureg
,
4611 glsl_to_tgsi_visitor
*program
,
4612 const struct gl_program
*proginfo
,
4614 const GLuint inputMapping
[],
4615 const ubyte inputSemanticName
[],
4616 const ubyte inputSemanticIndex
[],
4617 const GLuint interpMode
[],
4619 const GLuint outputMapping
[],
4620 const ubyte outputSemanticName
[],
4621 const ubyte outputSemanticIndex
[],
4622 boolean passthrough_edgeflags
)
4624 struct st_translate translate
, *t
;
4626 enum pipe_error ret
= PIPE_OK
;
4628 assert(numInputs
<= Elements(t
->inputs
));
4629 assert(numOutputs
<= Elements(t
->outputs
));
4632 memset(t
, 0, sizeof *t
);
4634 t
->procType
= procType
;
4635 t
->inputMapping
= inputMapping
;
4636 t
->outputMapping
= outputMapping
;
4638 t
->pointSizeOutIndex
= -1;
4639 t
->prevInstWrotePointSize
= GL_FALSE
;
4642 * Declare input attributes.
4644 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
4645 for (i
= 0; i
< numInputs
; i
++) {
4646 t
->inputs
[i
] = ureg_DECL_fs_input(ureg
,
4647 inputSemanticName
[i
],
4648 inputSemanticIndex
[i
],
4652 if (proginfo
->InputsRead
& FRAG_BIT_WPOS
) {
4653 /* Must do this after setting up t->inputs, and before
4654 * emitting constant references, below:
4656 emit_wpos(st_context(ctx
), t
, proginfo
, ureg
);
4659 if (proginfo
->InputsRead
& FRAG_BIT_FACE
)
4663 * Declare output attributes.
4665 for (i
= 0; i
< numOutputs
; i
++) {
4666 switch (outputSemanticName
[i
]) {
4667 case TGSI_SEMANTIC_POSITION
:
4668 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4669 TGSI_SEMANTIC_POSITION
, /* Z/Depth */
4670 outputSemanticIndex
[i
]);
4671 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_Z
);
4673 case TGSI_SEMANTIC_STENCIL
:
4674 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4675 TGSI_SEMANTIC_STENCIL
, /* Stencil */
4676 outputSemanticIndex
[i
]);
4677 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_Y
);
4679 case TGSI_SEMANTIC_COLOR
:
4680 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4681 TGSI_SEMANTIC_COLOR
,
4682 outputSemanticIndex
[i
]);
4685 assert(!"fragment shader outputs must be POSITION/STENCIL/COLOR");
4686 return PIPE_ERROR_BAD_INPUT
;
4690 else if (procType
== TGSI_PROCESSOR_GEOMETRY
) {
4691 for (i
= 0; i
< numInputs
; i
++) {
4692 t
->inputs
[i
] = ureg_DECL_gs_input(ureg
,
4694 inputSemanticName
[i
],
4695 inputSemanticIndex
[i
]);
4698 for (i
= 0; i
< numOutputs
; i
++) {
4699 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4700 outputSemanticName
[i
],
4701 outputSemanticIndex
[i
]);
4705 assert(procType
== TGSI_PROCESSOR_VERTEX
);
4707 for (i
= 0; i
< numInputs
; i
++) {
4708 t
->inputs
[i
] = ureg_DECL_vs_input(ureg
, i
);
4711 for (i
= 0; i
< numOutputs
; i
++) {
4712 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4713 outputSemanticName
[i
],
4714 outputSemanticIndex
[i
]);
4715 if ((outputSemanticName
[i
] == TGSI_SEMANTIC_PSIZE
) && proginfo
->Id
) {
4716 /* Writing to the point size result register requires special
4717 * handling to implement clamping.
4719 static const gl_state_index pointSizeClampState
[STATE_LENGTH
]
4720 = { STATE_INTERNAL
, STATE_POINT_SIZE_IMPL_CLAMP
, (gl_state_index
)0, (gl_state_index
)0, (gl_state_index
)0 };
4721 /* XXX: note we are modifying the incoming shader here! Need to
4722 * do this before emitting the constant decls below, or this
4725 unsigned pointSizeClampConst
=
4726 _mesa_add_state_reference(proginfo
->Parameters
,
4727 pointSizeClampState
);
4728 struct ureg_dst psizregtemp
= ureg_DECL_temporary(ureg
);
4729 t
->pointSizeConst
= ureg_DECL_constant(ureg
, pointSizeClampConst
);
4730 t
->pointSizeResult
= t
->outputs
[i
];
4731 t
->pointSizeOutIndex
= i
;
4732 t
->outputs
[i
] = psizregtemp
;
4735 if (passthrough_edgeflags
)
4739 /* Declare address register.
4741 if (program
->num_address_regs
> 0) {
4742 assert(program
->num_address_regs
== 1);
4743 t
->address
[0] = ureg_DECL_address(ureg
);
4746 /* Declare misc input registers
4749 GLbitfield sysInputs
= proginfo
->SystemValuesRead
;
4750 unsigned numSys
= 0;
4751 for (i
= 0; sysInputs
; i
++) {
4752 if (sysInputs
& (1 << i
)) {
4753 unsigned semName
= mesa_sysval_to_semantic
[i
];
4754 t
->systemValues
[i
] = ureg_DECL_system_value(ureg
, numSys
, semName
, 0);
4756 sysInputs
&= ~(1 << i
);
4761 if (program
->indirect_addr_temps
) {
4762 /* If temps are accessed with indirect addressing, declare temporaries
4763 * in sequential order. Else, we declare them on demand elsewhere.
4764 * (Note: the number of temporaries is equal to program->next_temp)
4766 for (i
= 0; i
< (unsigned)program
->next_temp
; i
++) {
4767 /* XXX use TGSI_FILE_TEMPORARY_ARRAY when it's supported by ureg */
4768 t
->temps
[i
] = ureg_DECL_temporary(t
->ureg
);
4772 /* Emit constants and uniforms. TGSI uses a single index space for these,
4773 * so we put all the translated regs in t->constants.
4775 if (proginfo
->Parameters
) {
4776 t
->constants
= (struct ureg_src
*)CALLOC(proginfo
->Parameters
->NumParameters
* sizeof(t
->constants
[0]));
4777 if (t
->constants
== NULL
) {
4778 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
4782 for (i
= 0; i
< proginfo
->Parameters
->NumParameters
; i
++) {
4783 switch (proginfo
->Parameters
->Parameters
[i
].Type
) {
4784 case PROGRAM_ENV_PARAM
:
4785 case PROGRAM_LOCAL_PARAM
:
4786 case PROGRAM_STATE_VAR
:
4787 case PROGRAM_NAMED_PARAM
:
4788 case PROGRAM_UNIFORM
:
4789 t
->constants
[i
] = ureg_DECL_constant(ureg
, i
);
4792 /* Emit immediates for PROGRAM_CONSTANT only when there's no indirect
4793 * addressing of the const buffer.
4794 * FIXME: Be smarter and recognize param arrays:
4795 * indirect addressing is only valid within the referenced
4798 case PROGRAM_CONSTANT
:
4799 if (program
->indirect_addr_consts
)
4800 t
->constants
[i
] = ureg_DECL_constant(ureg
, i
);
4802 t
->constants
[i
] = emit_immediate(t
,
4803 proginfo
->Parameters
->ParameterValues
[i
],
4804 proginfo
->Parameters
->Parameters
[i
].DataType
,
4813 /* Emit immediate values.
4815 t
->immediates
= (struct ureg_src
*)CALLOC(program
->num_immediates
* sizeof(struct ureg_src
));
4816 if (t
->immediates
== NULL
) {
4817 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
4821 foreach_iter(exec_list_iterator
, iter
, program
->immediates
) {
4822 immediate_storage
*imm
= (immediate_storage
*)iter
.get();
4823 t
->immediates
[i
++] = emit_immediate(t
, imm
->values
, imm
->type
, imm
->size
);
4826 /* texture samplers */
4827 for (i
= 0; i
< ctx
->Const
.MaxTextureImageUnits
; i
++) {
4828 if (program
->samplers_used
& (1 << i
)) {
4829 t
->samplers
[i
] = ureg_DECL_sampler(ureg
, i
);
4833 /* Emit each instruction in turn:
4835 foreach_iter(exec_list_iterator
, iter
, program
->instructions
) {
4836 set_insn_start(t
, ureg_get_instruction_number(ureg
));
4837 compile_tgsi_instruction(t
, (glsl_to_tgsi_instruction
*)iter
.get());
4839 if (t
->prevInstWrotePointSize
&& proginfo
->Id
) {
4840 /* The previous instruction wrote to the (fake) vertex point size
4841 * result register. Now we need to clamp that value to the min/max
4842 * point size range, putting the result into the real point size
4844 * Note that we can't do this easily at the end of program due to
4845 * possible early return.
4847 set_insn_start(t
, ureg_get_instruction_number(ureg
));
4849 ureg_writemask(t
->outputs
[t
->pointSizeOutIndex
], WRITEMASK_X
),
4850 ureg_src(t
->outputs
[t
->pointSizeOutIndex
]),
4851 ureg_swizzle(t
->pointSizeConst
, 1,1,1,1));
4852 ureg_MIN(t
->ureg
, ureg_writemask(t
->pointSizeResult
, WRITEMASK_X
),
4853 ureg_src(t
->outputs
[t
->pointSizeOutIndex
]),
4854 ureg_swizzle(t
->pointSizeConst
, 2,2,2,2));
4856 t
->prevInstWrotePointSize
= GL_FALSE
;
4859 /* Fix up all emitted labels:
4861 for (i
= 0; i
< t
->labels_count
; i
++) {
4862 ureg_fixup_label(ureg
, t
->labels
[i
].token
,
4863 t
->insn
[t
->labels
[i
].branch_target
]);
4870 FREE(t
->immediates
);
4873 debug_printf("%s: translate error flag set\n", __FUNCTION__
);
4878 /* ----------------------------- End TGSI code ------------------------------ */
4881 * Convert a shader's GLSL IR into a Mesa gl_program, although without
4882 * generating Mesa IR.
4884 static struct gl_program
*
4885 get_mesa_program(struct gl_context
*ctx
,
4886 struct gl_shader_program
*shader_program
,
4887 struct gl_shader
*shader
)
4889 glsl_to_tgsi_visitor
* v
= new glsl_to_tgsi_visitor();
4890 struct gl_program
*prog
;
4892 const char *target_string
;
4894 struct gl_shader_compiler_options
*options
=
4895 &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(shader
->Type
)];
4897 switch (shader
->Type
) {
4898 case GL_VERTEX_SHADER
:
4899 target
= GL_VERTEX_PROGRAM_ARB
;
4900 target_string
= "vertex";
4902 case GL_FRAGMENT_SHADER
:
4903 target
= GL_FRAGMENT_PROGRAM_ARB
;
4904 target_string
= "fragment";
4906 case GL_GEOMETRY_SHADER
:
4907 target
= GL_GEOMETRY_PROGRAM_NV
;
4908 target_string
= "geometry";
4911 assert(!"should not be reached");
4915 validate_ir_tree(shader
->ir
);
4917 prog
= ctx
->Driver
.NewProgram(ctx
, target
, shader_program
->Name
);
4920 prog
->Parameters
= _mesa_new_parameter_list();
4921 prog
->Varying
= _mesa_new_parameter_list();
4922 prog
->Attributes
= _mesa_new_parameter_list();
4925 v
->shader_program
= shader_program
;
4926 v
->options
= options
;
4927 v
->glsl_version
= ctx
->Const
.GLSLVersion
;
4928 v
->native_integers
= ctx
->Const
.NativeIntegers
;
4930 add_uniforms_to_parameters_list(shader_program
, shader
, prog
);
4932 /* Emit intermediate IR for main(). */
4933 visit_exec_list(shader
->ir
, v
);
4935 /* Now emit bodies for any functions that were used. */
4937 progress
= GL_FALSE
;
4939 foreach_iter(exec_list_iterator
, iter
, v
->function_signatures
) {
4940 function_entry
*entry
= (function_entry
*)iter
.get();
4942 if (!entry
->bgn_inst
) {
4943 v
->current_function
= entry
;
4945 entry
->bgn_inst
= v
->emit(NULL
, TGSI_OPCODE_BGNSUB
);
4946 entry
->bgn_inst
->function
= entry
;
4948 visit_exec_list(&entry
->sig
->body
, v
);
4950 glsl_to_tgsi_instruction
*last
;
4951 last
= (glsl_to_tgsi_instruction
*)v
->instructions
.get_tail();
4952 if (last
->op
!= TGSI_OPCODE_RET
)
4953 v
->emit(NULL
, TGSI_OPCODE_RET
);
4955 glsl_to_tgsi_instruction
*end
;
4956 end
= v
->emit(NULL
, TGSI_OPCODE_ENDSUB
);
4957 end
->function
= entry
;
4965 /* Print out some information (for debugging purposes) used by the
4966 * optimization passes. */
4967 for (i
=0; i
< v
->next_temp
; i
++) {
4968 int fr
= v
->get_first_temp_read(i
);
4969 int fw
= v
->get_first_temp_write(i
);
4970 int lr
= v
->get_last_temp_read(i
);
4971 int lw
= v
->get_last_temp_write(i
);
4973 printf("Temp %d: FR=%3d FW=%3d LR=%3d LW=%3d\n", i
, fr
, fw
, lr
, lw
);
4978 /* Remove reads to output registers, and to varyings in vertex shaders. */
4979 v
->remove_output_reads(PROGRAM_OUTPUT
);
4980 if (target
== GL_VERTEX_PROGRAM_ARB
)
4981 v
->remove_output_reads(PROGRAM_VARYING
);
4983 /* Perform optimizations on the instructions in the glsl_to_tgsi_visitor. */
4985 v
->copy_propagate();
4986 while (v
->eliminate_dead_code_advanced());
4988 /* FIXME: These passes to optimize temporary registers don't work when there
4989 * is indirect addressing of the temporary register space. We need proper
4990 * array support so that we don't have to give up these passes in every
4991 * shader that uses arrays.
4993 if (!v
->indirect_addr_temps
) {
4994 v
->eliminate_dead_code();
4995 v
->merge_registers();
4996 v
->renumber_registers();
4999 /* Write the END instruction. */
5000 v
->emit(NULL
, TGSI_OPCODE_END
);
5002 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
5004 printf("GLSL IR for linked %s program %d:\n", target_string
,
5005 shader_program
->Name
);
5006 _mesa_print_ir(shader
->ir
, NULL
);
5011 prog
->Instructions
= NULL
;
5012 prog
->NumInstructions
= 0;
5014 do_set_program_inouts(shader
->ir
, prog
);
5015 count_resources(v
, prog
);
5017 check_resources(ctx
, shader_program
, v
, prog
);
5019 _mesa_reference_program(ctx
, &shader
->Program
, prog
);
5021 struct st_vertex_program
*stvp
;
5022 struct st_fragment_program
*stfp
;
5023 struct st_geometry_program
*stgp
;
5025 switch (shader
->Type
) {
5026 case GL_VERTEX_SHADER
:
5027 stvp
= (struct st_vertex_program
*)prog
;
5028 stvp
->glsl_to_tgsi
= v
;
5030 case GL_FRAGMENT_SHADER
:
5031 stfp
= (struct st_fragment_program
*)prog
;
5032 stfp
->glsl_to_tgsi
= v
;
5034 case GL_GEOMETRY_SHADER
:
5035 stgp
= (struct st_geometry_program
*)prog
;
5036 stgp
->glsl_to_tgsi
= v
;
5039 assert(!"should not be reached");
5049 st_new_shader(struct gl_context
*ctx
, GLuint name
, GLuint type
)
5051 struct gl_shader
*shader
;
5052 assert(type
== GL_FRAGMENT_SHADER
|| type
== GL_VERTEX_SHADER
||
5053 type
== GL_GEOMETRY_SHADER_ARB
);
5054 shader
= rzalloc(NULL
, struct gl_shader
);
5056 shader
->Type
= type
;
5057 shader
->Name
= name
;
5058 _mesa_init_shader(ctx
, shader
);
5063 struct gl_shader_program
*
5064 st_new_shader_program(struct gl_context
*ctx
, GLuint name
)
5066 struct gl_shader_program
*shProg
;
5067 shProg
= rzalloc(NULL
, struct gl_shader_program
);
5069 shProg
->Name
= name
;
5070 _mesa_init_shader_program(ctx
, shProg
);
5077 * Called via ctx->Driver.LinkShader()
5078 * This actually involves converting GLSL IR into an intermediate TGSI-like IR
5079 * with code lowering and other optimizations.
5082 st_link_shader(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
5084 assert(prog
->LinkStatus
);
5086 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
5087 if (prog
->_LinkedShaders
[i
] == NULL
)
5091 exec_list
*ir
= prog
->_LinkedShaders
[i
]->ir
;
5092 const struct gl_shader_compiler_options
*options
=
5093 &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(prog
->_LinkedShaders
[i
]->Type
)];
5099 do_mat_op_to_vec(ir
);
5100 lower_instructions(ir
, (MOD_TO_FRACT
| DIV_TO_MUL_RCP
| EXP_TO_EXP2
5101 | LOG_TO_LOG2
| INT_DIV_TO_MUL_RCP
5102 | ((options
->EmitNoPow
) ? POW_TO_EXP2
: 0)));
5104 progress
= do_lower_jumps(ir
, true, true, options
->EmitNoMainReturn
, options
->EmitNoCont
, options
->EmitNoLoops
) || progress
;
5106 progress
= do_common_optimization(ir
, true, options
->MaxUnrollIterations
) || progress
;
5108 progress
= lower_quadop_vector(ir
, false) || progress
;
5110 if (options
->MaxIfDepth
== 0)
5111 progress
= lower_discard(ir
) || progress
;
5113 progress
= lower_if_to_cond_assign(ir
, options
->MaxIfDepth
) || progress
;
5115 if (options
->EmitNoNoise
)
5116 progress
= lower_noise(ir
) || progress
;
5118 /* If there are forms of indirect addressing that the driver
5119 * cannot handle, perform the lowering pass.
5121 if (options
->EmitNoIndirectInput
|| options
->EmitNoIndirectOutput
5122 || options
->EmitNoIndirectTemp
|| options
->EmitNoIndirectUniform
)
5124 lower_variable_index_to_cond_assign(ir
,
5125 options
->EmitNoIndirectInput
,
5126 options
->EmitNoIndirectOutput
,
5127 options
->EmitNoIndirectTemp
,
5128 options
->EmitNoIndirectUniform
)
5131 progress
= do_vec_index_to_cond_assign(ir
) || progress
;
5134 validate_ir_tree(ir
);
5137 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
5138 struct gl_program
*linked_prog
;
5140 if (prog
->_LinkedShaders
[i
] == NULL
)
5143 linked_prog
= get_mesa_program(ctx
, prog
, prog
->_LinkedShaders
[i
]);
5148 switch (prog
->_LinkedShaders
[i
]->Type
) {
5149 case GL_VERTEX_SHADER
:
5150 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
,
5151 (struct gl_vertex_program
*)linked_prog
);
5152 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_VERTEX_PROGRAM_ARB
,
5155 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
, NULL
);
5158 case GL_FRAGMENT_SHADER
:
5159 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
,
5160 (struct gl_fragment_program
*)linked_prog
);
5161 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
5164 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
, NULL
);
5167 case GL_GEOMETRY_SHADER
:
5168 _mesa_reference_geomprog(ctx
, &prog
->GeometryProgram
,
5169 (struct gl_geometry_program
*)linked_prog
);
5170 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_GEOMETRY_PROGRAM_NV
,
5173 _mesa_reference_geomprog(ctx
, &prog
->GeometryProgram
, NULL
);
5178 _mesa_reference_program(ctx
, &prog
->_LinkedShaders
[i
]->Program
, NULL
);
5179 _mesa_reference_program(ctx
, &linked_prog
, NULL
);
5184 _mesa_reference_program(ctx
, &linked_prog
, NULL
);