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"
45 #include "main/mtypes.h"
46 #include "main/shaderobj.h"
47 #include "program/hash_table.h"
50 #include "main/shaderapi.h"
51 #include "main/uniforms.h"
52 #include "program/prog_instruction.h"
53 #include "program/prog_optimize.h"
54 #include "program/prog_print.h"
55 #include "program/program.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_CONSTANT) | \
78 (1 << PROGRAM_UNIFORM))
81 * Maximum number of temporary registers.
83 * It is too big for stack allocated arrays -- it will cause stack overflow on
84 * Windows and likely Mac OS X.
86 #define MAX_TEMPS 4096
88 /* will be 4 for GLSL 4.00 */
89 #define MAX_GLSL_TEXTURE_OFFSET 1
94 static int swizzle_for_size(int size
);
97 * This struct is a corresponding struct to TGSI ureg_src.
101 st_src_reg(gl_register_file file
, int index
, const glsl_type
*type
)
105 if (type
&& (type
->is_scalar() || type
->is_vector() || type
->is_matrix()))
106 this->swizzle
= swizzle_for_size(type
->vector_elements
);
108 this->swizzle
= SWIZZLE_XYZW
;
111 this->type
= type
? type
->base_type
: GLSL_TYPE_ERROR
;
112 this->reladdr
= NULL
;
115 st_src_reg(gl_register_file file
, int index
, int type
)
121 this->swizzle
= SWIZZLE_XYZW
;
123 this->reladdr
= NULL
;
126 st_src_reg(gl_register_file file
, int index
, int type
, int index2D
)
131 this->index2D
= index2D
;
132 this->swizzle
= SWIZZLE_XYZW
;
134 this->reladdr
= NULL
;
139 this->type
= GLSL_TYPE_ERROR
;
140 this->file
= PROGRAM_UNDEFINED
;
145 this->reladdr
= NULL
;
148 explicit st_src_reg(st_dst_reg reg
);
150 gl_register_file file
; /**< PROGRAM_* from Mesa */
151 int index
; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
153 GLuint swizzle
; /**< SWIZZLE_XYZWONEZERO swizzles from Mesa. */
154 int negate
; /**< NEGATE_XYZW mask from mesa */
155 int type
; /** GLSL_TYPE_* from GLSL IR (enum glsl_base_type) */
156 /** Register index should be offset by the integer in this reg. */
162 st_dst_reg(gl_register_file file
, int writemask
, int type
)
166 this->writemask
= writemask
;
167 this->cond_mask
= COND_TR
;
168 this->reladdr
= NULL
;
174 this->type
= GLSL_TYPE_ERROR
;
175 this->file
= PROGRAM_UNDEFINED
;
178 this->cond_mask
= COND_TR
;
179 this->reladdr
= NULL
;
182 explicit st_dst_reg(st_src_reg reg
);
184 gl_register_file file
; /**< PROGRAM_* from Mesa */
185 int index
; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
186 int writemask
; /**< Bitfield of WRITEMASK_[XYZW] */
188 int type
; /** GLSL_TYPE_* from GLSL IR (enum glsl_base_type) */
189 /** Register index should be offset by the integer in this reg. */
193 st_src_reg::st_src_reg(st_dst_reg reg
)
195 this->type
= reg
.type
;
196 this->file
= reg
.file
;
197 this->index
= reg
.index
;
198 this->swizzle
= SWIZZLE_XYZW
;
200 this->reladdr
= reg
.reladdr
;
204 st_dst_reg::st_dst_reg(st_src_reg reg
)
206 this->type
= reg
.type
;
207 this->file
= reg
.file
;
208 this->index
= reg
.index
;
209 this->writemask
= WRITEMASK_XYZW
;
210 this->cond_mask
= COND_TR
;
211 this->reladdr
= reg
.reladdr
;
214 class glsl_to_tgsi_instruction
: public exec_node
{
216 /* Callers of this ralloc-based new need not call delete. It's
217 * easier to just ralloc_free 'ctx' (or any of its ancestors). */
218 static void* operator new(size_t size
, void *ctx
)
222 node
= rzalloc_size(ctx
, size
);
223 assert(node
!= NULL
);
231 /** Pointer to the ir source this tree came from for debugging */
233 GLboolean cond_update
;
235 int sampler
; /**< sampler index */
236 int tex_target
; /**< One of TEXTURE_*_INDEX */
237 GLboolean tex_shadow
;
238 struct tgsi_texture_offset tex_offsets
[MAX_GLSL_TEXTURE_OFFSET
];
239 unsigned tex_offset_num_offset
;
240 int dead_mask
; /**< Used in dead code elimination */
242 class function_entry
*function
; /* Set on TGSI_OPCODE_CAL or TGSI_OPCODE_BGNSUB */
245 class variable_storage
: public exec_node
{
247 variable_storage(ir_variable
*var
, gl_register_file file
, int index
)
248 : file(file
), index(index
), var(var
)
253 gl_register_file file
;
255 ir_variable
*var
; /* variable that maps to this, if any */
258 class immediate_storage
: public exec_node
{
260 immediate_storage(gl_constant_value
*values
, int size
, int type
)
262 memcpy(this->values
, values
, size
* sizeof(gl_constant_value
));
267 gl_constant_value values
[4];
268 int size
; /**< Number of components (1-4) */
269 int type
; /**< GL_FLOAT, GL_INT, GL_BOOL, or GL_UNSIGNED_INT */
272 class function_entry
: public exec_node
{
274 ir_function_signature
*sig
;
277 * identifier of this function signature used by the program.
279 * At the point that TGSI instructions for function calls are
280 * generated, we don't know the address of the first instruction of
281 * the function body. So we make the BranchTarget that is called a
282 * small integer and rewrite them during set_branchtargets().
287 * Pointer to first instruction of the function body.
289 * Set during function body emits after main() is processed.
291 glsl_to_tgsi_instruction
*bgn_inst
;
294 * Index of the first instruction of the function body in actual TGSI.
296 * Set after conversion from glsl_to_tgsi_instruction to TGSI.
300 /** Storage for the return value. */
301 st_src_reg return_reg
;
304 struct glsl_to_tgsi_visitor
: public ir_visitor
{
306 glsl_to_tgsi_visitor();
307 ~glsl_to_tgsi_visitor();
309 function_entry
*current_function
;
311 struct gl_context
*ctx
;
312 struct gl_program
*prog
;
313 struct gl_shader_program
*shader_program
;
314 struct gl_shader_compiler_options
*options
;
318 int num_address_regs
;
320 bool indirect_addr_temps
;
321 bool indirect_addr_consts
;
324 bool native_integers
;
327 variable_storage
*find_variable_storage(ir_variable
*var
);
329 int add_constant(gl_register_file file
, gl_constant_value values
[4],
330 int size
, int datatype
, GLuint
*swizzle_out
);
332 function_entry
*get_function_signature(ir_function_signature
*sig
);
334 st_src_reg
get_temp(const glsl_type
*type
);
335 void reladdr_to_temp(ir_instruction
*ir
, st_src_reg
*reg
, int *num_reladdr
);
337 st_src_reg
st_src_reg_for_float(float val
);
338 st_src_reg
st_src_reg_for_int(int val
);
339 st_src_reg
st_src_reg_for_type(int type
, int val
);
342 * \name Visit methods
344 * As typical for the visitor pattern, there must be one \c visit method for
345 * each concrete subclass of \c ir_instruction. Virtual base classes within
346 * the hierarchy should not have \c visit methods.
349 virtual void visit(ir_variable
*);
350 virtual void visit(ir_loop
*);
351 virtual void visit(ir_loop_jump
*);
352 virtual void visit(ir_function_signature
*);
353 virtual void visit(ir_function
*);
354 virtual void visit(ir_expression
*);
355 virtual void visit(ir_swizzle
*);
356 virtual void visit(ir_dereference_variable
*);
357 virtual void visit(ir_dereference_array
*);
358 virtual void visit(ir_dereference_record
*);
359 virtual void visit(ir_assignment
*);
360 virtual void visit(ir_constant
*);
361 virtual void visit(ir_call
*);
362 virtual void visit(ir_return
*);
363 virtual void visit(ir_discard
*);
364 virtual void visit(ir_texture
*);
365 virtual void visit(ir_if
*);
370 /** List of variable_storage */
373 /** List of immediate_storage */
374 exec_list immediates
;
375 unsigned num_immediates
;
377 /** List of function_entry */
378 exec_list function_signatures
;
379 int next_signature_id
;
381 /** List of glsl_to_tgsi_instruction */
382 exec_list instructions
;
384 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
);
386 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
,
387 st_dst_reg dst
, st_src_reg src0
);
389 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
,
390 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
);
392 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
,
394 st_src_reg src0
, st_src_reg src1
, st_src_reg src2
);
396 unsigned get_opcode(ir_instruction
*ir
, unsigned op
,
398 st_src_reg src0
, st_src_reg src1
);
401 * Emit the correct dot-product instruction for the type of arguments
403 glsl_to_tgsi_instruction
*emit_dp(ir_instruction
*ir
,
409 void emit_scalar(ir_instruction
*ir
, unsigned op
,
410 st_dst_reg dst
, st_src_reg src0
);
412 void emit_scalar(ir_instruction
*ir
, unsigned op
,
413 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
);
415 void try_emit_float_set(ir_instruction
*ir
, unsigned op
, st_dst_reg dst
);
417 void emit_arl(ir_instruction
*ir
, st_dst_reg dst
, st_src_reg src0
);
419 void emit_scs(ir_instruction
*ir
, unsigned op
,
420 st_dst_reg dst
, const st_src_reg
&src
);
422 bool try_emit_mad(ir_expression
*ir
,
424 bool try_emit_mad_for_and_not(ir_expression
*ir
,
426 bool try_emit_sat(ir_expression
*ir
);
428 void emit_swz(ir_expression
*ir
);
430 bool process_move_condition(ir_rvalue
*ir
);
432 void simplify_cmp(void);
434 void rename_temp_register(int index
, int new_index
);
435 int get_first_temp_read(int index
);
436 int get_first_temp_write(int index
);
437 int get_last_temp_read(int index
);
438 int get_last_temp_write(int index
);
440 void copy_propagate(void);
441 void eliminate_dead_code(void);
442 int eliminate_dead_code_advanced(void);
443 void merge_registers(void);
444 void renumber_registers(void);
446 void emit_block_mov(ir_assignment
*ir
, const struct glsl_type
*type
,
447 st_dst_reg
*l
, st_src_reg
*r
);
452 static st_src_reg undef_src
= st_src_reg(PROGRAM_UNDEFINED
, 0, GLSL_TYPE_ERROR
);
454 static st_dst_reg undef_dst
= st_dst_reg(PROGRAM_UNDEFINED
, SWIZZLE_NOOP
, GLSL_TYPE_ERROR
);
456 static st_dst_reg address_reg
= st_dst_reg(PROGRAM_ADDRESS
, WRITEMASK_X
, GLSL_TYPE_FLOAT
);
459 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...) PRINTFLIKE(2, 3);
462 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...)
466 ralloc_vasprintf_append(&prog
->InfoLog
, fmt
, args
);
469 prog
->LinkStatus
= GL_FALSE
;
473 swizzle_for_size(int size
)
475 int size_swizzles
[4] = {
476 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
),
477 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
),
478 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
),
479 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
482 assert((size
>= 1) && (size
<= 4));
483 return size_swizzles
[size
- 1];
487 is_tex_instruction(unsigned opcode
)
489 const tgsi_opcode_info
* info
= tgsi_get_opcode_info(opcode
);
494 num_inst_dst_regs(unsigned opcode
)
496 const tgsi_opcode_info
* info
= tgsi_get_opcode_info(opcode
);
497 return info
->num_dst
;
501 num_inst_src_regs(unsigned opcode
)
503 const tgsi_opcode_info
* info
= tgsi_get_opcode_info(opcode
);
504 return info
->is_tex
? info
->num_src
- 1 : info
->num_src
;
507 glsl_to_tgsi_instruction
*
508 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
,
510 st_src_reg src0
, st_src_reg src1
, st_src_reg src2
)
512 glsl_to_tgsi_instruction
*inst
= new(mem_ctx
) glsl_to_tgsi_instruction();
513 int num_reladdr
= 0, i
;
515 op
= get_opcode(ir
, op
, dst
, src0
, src1
);
517 /* If we have to do relative addressing, we want to load the ARL
518 * reg directly for one of the regs, and preload the other reladdr
519 * sources into temps.
521 num_reladdr
+= dst
.reladdr
!= NULL
;
522 num_reladdr
+= src0
.reladdr
!= NULL
;
523 num_reladdr
+= src1
.reladdr
!= NULL
;
524 num_reladdr
+= src2
.reladdr
!= NULL
;
526 reladdr_to_temp(ir
, &src2
, &num_reladdr
);
527 reladdr_to_temp(ir
, &src1
, &num_reladdr
);
528 reladdr_to_temp(ir
, &src0
, &num_reladdr
);
531 emit_arl(ir
, address_reg
, *dst
.reladdr
);
534 assert(num_reladdr
== 0);
544 inst
->function
= NULL
;
546 if (op
== TGSI_OPCODE_ARL
|| op
== TGSI_OPCODE_UARL
)
547 this->num_address_regs
= 1;
549 /* Update indirect addressing status used by TGSI */
552 case PROGRAM_TEMPORARY
:
553 this->indirect_addr_temps
= true;
555 case PROGRAM_LOCAL_PARAM
:
556 case PROGRAM_ENV_PARAM
:
557 case PROGRAM_STATE_VAR
:
558 case PROGRAM_CONSTANT
:
559 case PROGRAM_UNIFORM
:
560 this->indirect_addr_consts
= true;
562 case PROGRAM_IMMEDIATE
:
563 assert(!"immediates should not have indirect addressing");
570 for (i
=0; i
<3; i
++) {
571 if(inst
->src
[i
].reladdr
) {
572 switch(inst
->src
[i
].file
) {
573 case PROGRAM_TEMPORARY
:
574 this->indirect_addr_temps
= true;
576 case PROGRAM_LOCAL_PARAM
:
577 case PROGRAM_ENV_PARAM
:
578 case PROGRAM_STATE_VAR
:
579 case PROGRAM_CONSTANT
:
580 case PROGRAM_UNIFORM
:
581 this->indirect_addr_consts
= true;
583 case PROGRAM_IMMEDIATE
:
584 assert(!"immediates should not have indirect addressing");
593 this->instructions
.push_tail(inst
);
596 try_emit_float_set(ir
, op
, dst
);
602 glsl_to_tgsi_instruction
*
603 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
,
604 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
)
606 return emit(ir
, op
, dst
, src0
, src1
, undef_src
);
609 glsl_to_tgsi_instruction
*
610 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
,
611 st_dst_reg dst
, st_src_reg src0
)
613 assert(dst
.writemask
!= 0);
614 return emit(ir
, op
, dst
, src0
, undef_src
, undef_src
);
617 glsl_to_tgsi_instruction
*
618 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
)
620 return emit(ir
, op
, undef_dst
, undef_src
, undef_src
, undef_src
);
624 * Emits the code to convert the result of float SET instructions to integers.
627 glsl_to_tgsi_visitor::try_emit_float_set(ir_instruction
*ir
, unsigned op
,
630 if ((op
== TGSI_OPCODE_SEQ
||
631 op
== TGSI_OPCODE_SNE
||
632 op
== TGSI_OPCODE_SGE
||
633 op
== TGSI_OPCODE_SLT
))
635 st_src_reg src
= st_src_reg(dst
);
636 src
.negate
= ~src
.negate
;
637 dst
.type
= GLSL_TYPE_FLOAT
;
638 emit(ir
, TGSI_OPCODE_F2I
, dst
, src
);
643 * Determines whether to use an integer, unsigned integer, or float opcode
644 * based on the operands and input opcode, then emits the result.
647 glsl_to_tgsi_visitor::get_opcode(ir_instruction
*ir
, unsigned op
,
649 st_src_reg src0
, st_src_reg src1
)
651 int type
= GLSL_TYPE_FLOAT
;
653 assert(src0
.type
!= GLSL_TYPE_ARRAY
);
654 assert(src0
.type
!= GLSL_TYPE_STRUCT
);
655 assert(src1
.type
!= GLSL_TYPE_ARRAY
);
656 assert(src1
.type
!= GLSL_TYPE_STRUCT
);
658 if (src0
.type
== GLSL_TYPE_FLOAT
|| src1
.type
== GLSL_TYPE_FLOAT
)
659 type
= GLSL_TYPE_FLOAT
;
660 else if (native_integers
)
661 type
= src0
.type
== GLSL_TYPE_BOOL
? GLSL_TYPE_INT
: src0
.type
;
663 #define case4(c, f, i, u) \
664 case TGSI_OPCODE_##c: \
665 if (type == GLSL_TYPE_INT) op = TGSI_OPCODE_##i; \
666 else if (type == GLSL_TYPE_UINT) op = TGSI_OPCODE_##u; \
667 else op = TGSI_OPCODE_##f; \
669 #define case3(f, i, u) case4(f, f, i, u)
670 #define case2fi(f, i) case4(f, f, i, i)
671 #define case2iu(i, u) case4(i, LAST, i, u)
677 case3(DIV
, IDIV
, UDIV
);
678 case3(MAX
, IMAX
, UMAX
);
679 case3(MIN
, IMIN
, UMIN
);
684 case3(SGE
, ISGE
, USGE
);
685 case3(SLT
, ISLT
, USLT
);
690 case3(ABS
, IABS
, IABS
);
695 assert(op
!= TGSI_OPCODE_LAST
);
699 glsl_to_tgsi_instruction
*
700 glsl_to_tgsi_visitor::emit_dp(ir_instruction
*ir
,
701 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
,
704 static const unsigned dot_opcodes
[] = {
705 TGSI_OPCODE_DP2
, TGSI_OPCODE_DP3
, TGSI_OPCODE_DP4
708 return emit(ir
, dot_opcodes
[elements
- 2], dst
, src0
, src1
);
712 * Emits TGSI scalar opcodes to produce unique answers across channels.
714 * Some TGSI opcodes are scalar-only, like ARB_fp/vp. The src X
715 * channel determines the result across all channels. So to do a vec4
716 * of this operation, we want to emit a scalar per source channel used
717 * to produce dest channels.
720 glsl_to_tgsi_visitor::emit_scalar(ir_instruction
*ir
, unsigned op
,
722 st_src_reg orig_src0
, st_src_reg orig_src1
)
725 int done_mask
= ~dst
.writemask
;
727 /* TGSI RCP is a scalar operation splatting results to all channels,
728 * like ARB_fp/vp. So emit as many RCPs as necessary to cover our
731 for (i
= 0; i
< 4; i
++) {
732 GLuint this_mask
= (1 << i
);
733 glsl_to_tgsi_instruction
*inst
;
734 st_src_reg src0
= orig_src0
;
735 st_src_reg src1
= orig_src1
;
737 if (done_mask
& this_mask
)
740 GLuint src0_swiz
= GET_SWZ(src0
.swizzle
, i
);
741 GLuint src1_swiz
= GET_SWZ(src1
.swizzle
, i
);
742 for (j
= i
+ 1; j
< 4; j
++) {
743 /* If there is another enabled component in the destination that is
744 * derived from the same inputs, generate its value on this pass as
747 if (!(done_mask
& (1 << j
)) &&
748 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
&&
749 GET_SWZ(src1
.swizzle
, j
) == src1_swiz
) {
750 this_mask
|= (1 << j
);
753 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
754 src0_swiz
, src0_swiz
);
755 src1
.swizzle
= MAKE_SWIZZLE4(src1_swiz
, src1_swiz
,
756 src1_swiz
, src1_swiz
);
758 inst
= emit(ir
, op
, dst
, src0
, src1
);
759 inst
->dst
.writemask
= this_mask
;
760 done_mask
|= this_mask
;
765 glsl_to_tgsi_visitor::emit_scalar(ir_instruction
*ir
, unsigned op
,
766 st_dst_reg dst
, st_src_reg src0
)
768 st_src_reg undef
= undef_src
;
770 undef
.swizzle
= SWIZZLE_XXXX
;
772 emit_scalar(ir
, op
, dst
, src0
, undef
);
776 glsl_to_tgsi_visitor::emit_arl(ir_instruction
*ir
,
777 st_dst_reg dst
, st_src_reg src0
)
779 int op
= TGSI_OPCODE_ARL
;
781 if (src0
.type
== GLSL_TYPE_INT
|| src0
.type
== GLSL_TYPE_UINT
)
782 op
= TGSI_OPCODE_UARL
;
784 emit(NULL
, op
, dst
, src0
);
788 * Emit an TGSI_OPCODE_SCS instruction
790 * The \c SCS opcode functions a bit differently than the other TGSI opcodes.
791 * Instead of splatting its result across all four components of the
792 * destination, it writes one value to the \c x component and another value to
793 * the \c y component.
795 * \param ir IR instruction being processed
796 * \param op Either \c TGSI_OPCODE_SIN or \c TGSI_OPCODE_COS depending
797 * on which value is desired.
798 * \param dst Destination register
799 * \param src Source register
802 glsl_to_tgsi_visitor::emit_scs(ir_instruction
*ir
, unsigned op
,
804 const st_src_reg
&src
)
806 /* Vertex programs cannot use the SCS opcode.
808 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
809 emit_scalar(ir
, op
, dst
, src
);
813 const unsigned component
= (op
== TGSI_OPCODE_SIN
) ? 0 : 1;
814 const unsigned scs_mask
= (1U << component
);
815 int done_mask
= ~dst
.writemask
;
818 assert(op
== TGSI_OPCODE_SIN
|| op
== TGSI_OPCODE_COS
);
820 /* If there are compnents in the destination that differ from the component
821 * that will be written by the SCS instrution, we'll need a temporary.
823 if (scs_mask
!= unsigned(dst
.writemask
)) {
824 tmp
= get_temp(glsl_type::vec4_type
);
827 for (unsigned i
= 0; i
< 4; i
++) {
828 unsigned this_mask
= (1U << i
);
829 st_src_reg src0
= src
;
831 if ((done_mask
& this_mask
) != 0)
834 /* The source swizzle specified which component of the source generates
835 * sine / cosine for the current component in the destination. The SCS
836 * instruction requires that this value be swizzle to the X component.
837 * Replace the current swizzle with a swizzle that puts the source in
840 unsigned src0_swiz
= GET_SWZ(src
.swizzle
, i
);
842 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
843 src0_swiz
, src0_swiz
);
844 for (unsigned j
= i
+ 1; j
< 4; j
++) {
845 /* If there is another enabled component in the destination that is
846 * derived from the same inputs, generate its value on this pass as
849 if (!(done_mask
& (1 << j
)) &&
850 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
) {
851 this_mask
|= (1 << j
);
855 if (this_mask
!= scs_mask
) {
856 glsl_to_tgsi_instruction
*inst
;
857 st_dst_reg tmp_dst
= st_dst_reg(tmp
);
859 /* Emit the SCS instruction.
861 inst
= emit(ir
, TGSI_OPCODE_SCS
, tmp_dst
, src0
);
862 inst
->dst
.writemask
= scs_mask
;
864 /* Move the result of the SCS instruction to the desired location in
867 tmp
.swizzle
= MAKE_SWIZZLE4(component
, component
,
868 component
, component
);
869 inst
= emit(ir
, TGSI_OPCODE_SCS
, dst
, tmp
);
870 inst
->dst
.writemask
= this_mask
;
872 /* Emit the SCS instruction to write directly to the destination.
874 glsl_to_tgsi_instruction
*inst
= emit(ir
, TGSI_OPCODE_SCS
, dst
, src0
);
875 inst
->dst
.writemask
= scs_mask
;
878 done_mask
|= this_mask
;
883 glsl_to_tgsi_visitor::add_constant(gl_register_file file
,
884 gl_constant_value values
[4], int size
, int datatype
,
887 if (file
== PROGRAM_CONSTANT
) {
888 return _mesa_add_typed_unnamed_constant(this->prog
->Parameters
, values
,
889 size
, datatype
, swizzle_out
);
892 immediate_storage
*entry
;
893 assert(file
== PROGRAM_IMMEDIATE
);
895 /* Search immediate storage to see if we already have an identical
896 * immediate that we can use instead of adding a duplicate entry.
898 foreach_iter(exec_list_iterator
, iter
, this->immediates
) {
899 entry
= (immediate_storage
*)iter
.get();
901 if (entry
->size
== size
&&
902 entry
->type
== datatype
&&
903 !memcmp(entry
->values
, values
, size
* sizeof(gl_constant_value
))) {
909 /* Add this immediate to the list. */
910 entry
= new(mem_ctx
) immediate_storage(values
, size
, datatype
);
911 this->immediates
.push_tail(entry
);
912 this->num_immediates
++;
918 glsl_to_tgsi_visitor::st_src_reg_for_float(float val
)
920 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_FLOAT
);
921 union gl_constant_value uval
;
924 src
.index
= add_constant(src
.file
, &uval
, 1, GL_FLOAT
, &src
.swizzle
);
930 glsl_to_tgsi_visitor::st_src_reg_for_int(int val
)
932 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_INT
);
933 union gl_constant_value uval
;
935 assert(native_integers
);
938 src
.index
= add_constant(src
.file
, &uval
, 1, GL_INT
, &src
.swizzle
);
944 glsl_to_tgsi_visitor::st_src_reg_for_type(int type
, int val
)
947 return type
== GLSL_TYPE_FLOAT
? st_src_reg_for_float(val
) :
948 st_src_reg_for_int(val
);
950 return st_src_reg_for_float(val
);
954 type_size(const struct glsl_type
*type
)
959 switch (type
->base_type
) {
962 case GLSL_TYPE_FLOAT
:
964 if (type
->is_matrix()) {
965 return type
->matrix_columns
;
967 /* Regardless of size of vector, it gets a vec4. This is bad
968 * packing for things like floats, but otherwise arrays become a
969 * mess. Hopefully a later pass over the code can pack scalars
970 * down if appropriate.
974 case GLSL_TYPE_ARRAY
:
975 assert(type
->length
> 0);
976 return type_size(type
->fields
.array
) * type
->length
;
977 case GLSL_TYPE_STRUCT
:
979 for (i
= 0; i
< type
->length
; i
++) {
980 size
+= type_size(type
->fields
.structure
[i
].type
);
983 case GLSL_TYPE_SAMPLER
:
984 /* Samplers take up one slot in UNIFORMS[], but they're baked in
988 case GLSL_TYPE_INTERFACE
:
990 case GLSL_TYPE_ERROR
:
991 assert(!"Invalid type in type_size");
998 * In the initial pass of codegen, we assign temporary numbers to
999 * intermediate results. (not SSA -- variable assignments will reuse
1003 glsl_to_tgsi_visitor::get_temp(const glsl_type
*type
)
1007 src
.type
= native_integers
? type
->base_type
: GLSL_TYPE_FLOAT
;
1008 src
.file
= PROGRAM_TEMPORARY
;
1009 src
.index
= next_temp
;
1011 next_temp
+= type_size(type
);
1013 if (type
->is_array() || type
->is_record()) {
1014 src
.swizzle
= SWIZZLE_NOOP
;
1016 src
.swizzle
= swizzle_for_size(type
->vector_elements
);
1024 glsl_to_tgsi_visitor::find_variable_storage(ir_variable
*var
)
1027 variable_storage
*entry
;
1029 foreach_iter(exec_list_iterator
, iter
, this->variables
) {
1030 entry
= (variable_storage
*)iter
.get();
1032 if (entry
->var
== var
)
1040 glsl_to_tgsi_visitor::visit(ir_variable
*ir
)
1042 if (strcmp(ir
->name
, "gl_FragCoord") == 0) {
1043 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
1045 fp
->OriginUpperLeft
= ir
->origin_upper_left
;
1046 fp
->PixelCenterInteger
= ir
->pixel_center_integer
;
1049 if (ir
->mode
== ir_var_uniform
&& strncmp(ir
->name
, "gl_", 3) == 0) {
1051 const ir_state_slot
*const slots
= ir
->state_slots
;
1052 assert(ir
->state_slots
!= NULL
);
1054 /* Check if this statevar's setup in the STATE file exactly
1055 * matches how we'll want to reference it as a
1056 * struct/array/whatever. If not, then we need to move it into
1057 * temporary storage and hope that it'll get copy-propagated
1060 for (i
= 0; i
< ir
->num_state_slots
; i
++) {
1061 if (slots
[i
].swizzle
!= SWIZZLE_XYZW
) {
1066 variable_storage
*storage
;
1068 if (i
== ir
->num_state_slots
) {
1069 /* We'll set the index later. */
1070 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_STATE_VAR
, -1);
1071 this->variables
.push_tail(storage
);
1075 /* The variable_storage constructor allocates slots based on the size
1076 * of the type. However, this had better match the number of state
1077 * elements that we're going to copy into the new temporary.
1079 assert((int) ir
->num_state_slots
== type_size(ir
->type
));
1081 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_TEMPORARY
,
1083 this->variables
.push_tail(storage
);
1084 this->next_temp
+= type_size(ir
->type
);
1086 dst
= st_dst_reg(st_src_reg(PROGRAM_TEMPORARY
, storage
->index
,
1087 native_integers
? ir
->type
->base_type
: GLSL_TYPE_FLOAT
));
1091 for (unsigned int i
= 0; i
< ir
->num_state_slots
; i
++) {
1092 int index
= _mesa_add_state_reference(this->prog
->Parameters
,
1093 (gl_state_index
*)slots
[i
].tokens
);
1095 if (storage
->file
== PROGRAM_STATE_VAR
) {
1096 if (storage
->index
== -1) {
1097 storage
->index
= index
;
1099 assert(index
== storage
->index
+ (int)i
);
1102 /* We use GLSL_TYPE_FLOAT here regardless of the actual type of
1103 * the data being moved since MOV does not care about the type of
1104 * data it is moving, and we don't want to declare registers with
1105 * array or struct types.
1107 st_src_reg
src(PROGRAM_STATE_VAR
, index
, GLSL_TYPE_FLOAT
);
1108 src
.swizzle
= slots
[i
].swizzle
;
1109 emit(ir
, TGSI_OPCODE_MOV
, dst
, src
);
1110 /* even a float takes up a whole vec4 reg in a struct/array. */
1115 if (storage
->file
== PROGRAM_TEMPORARY
&&
1116 dst
.index
!= storage
->index
+ (int) ir
->num_state_slots
) {
1117 fail_link(this->shader_program
,
1118 "failed to load builtin uniform `%s' (%d/%d regs loaded)\n",
1119 ir
->name
, dst
.index
- storage
->index
,
1120 type_size(ir
->type
));
1126 glsl_to_tgsi_visitor::visit(ir_loop
*ir
)
1128 ir_dereference_variable
*counter
= NULL
;
1130 if (ir
->counter
!= NULL
)
1131 counter
= new(ir
) ir_dereference_variable(ir
->counter
);
1133 if (ir
->from
!= NULL
) {
1134 assert(ir
->counter
!= NULL
);
1136 ir_assignment
*a
= new(ir
) ir_assignment(counter
, ir
->from
, NULL
);
1142 emit(NULL
, TGSI_OPCODE_BGNLOOP
);
1146 new(ir
) ir_expression(ir
->cmp
, glsl_type::bool_type
,
1148 ir_if
*if_stmt
= new(ir
) ir_if(e
);
1150 ir_loop_jump
*brk
= new(ir
) ir_loop_jump(ir_loop_jump::jump_break
);
1152 if_stmt
->then_instructions
.push_tail(brk
);
1154 if_stmt
->accept(this);
1161 visit_exec_list(&ir
->body_instructions
, this);
1163 if (ir
->increment
) {
1165 new(ir
) ir_expression(ir_binop_add
, counter
->type
,
1166 counter
, ir
->increment
);
1168 ir_assignment
*a
= new(ir
) ir_assignment(counter
, e
, NULL
);
1175 emit(NULL
, TGSI_OPCODE_ENDLOOP
);
1179 glsl_to_tgsi_visitor::visit(ir_loop_jump
*ir
)
1182 case ir_loop_jump::jump_break
:
1183 emit(NULL
, TGSI_OPCODE_BRK
);
1185 case ir_loop_jump::jump_continue
:
1186 emit(NULL
, TGSI_OPCODE_CONT
);
1193 glsl_to_tgsi_visitor::visit(ir_function_signature
*ir
)
1200 glsl_to_tgsi_visitor::visit(ir_function
*ir
)
1202 /* Ignore function bodies other than main() -- we shouldn't see calls to
1203 * them since they should all be inlined before we get to glsl_to_tgsi.
1205 if (strcmp(ir
->name
, "main") == 0) {
1206 const ir_function_signature
*sig
;
1209 sig
= ir
->matching_signature(&empty
);
1213 foreach_iter(exec_list_iterator
, iter
, sig
->body
) {
1214 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
1222 glsl_to_tgsi_visitor::try_emit_mad(ir_expression
*ir
, int mul_operand
)
1224 int nonmul_operand
= 1 - mul_operand
;
1226 st_dst_reg result_dst
;
1228 ir_expression
*expr
= ir
->operands
[mul_operand
]->as_expression();
1229 if (!expr
|| expr
->operation
!= ir_binop_mul
)
1232 expr
->operands
[0]->accept(this);
1234 expr
->operands
[1]->accept(this);
1236 ir
->operands
[nonmul_operand
]->accept(this);
1239 this->result
= get_temp(ir
->type
);
1240 result_dst
= st_dst_reg(this->result
);
1241 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1242 emit(ir
, TGSI_OPCODE_MAD
, result_dst
, a
, b
, c
);
1248 * Emit MAD(a, -b, a) instead of AND(a, NOT(b))
1250 * The logic values are 1.0 for true and 0.0 for false. Logical-and is
1251 * implemented using multiplication, and logical-or is implemented using
1252 * addition. Logical-not can be implemented as (true - x), or (1.0 - x).
1253 * As result, the logical expression (a & !b) can be rewritten as:
1257 * - (a * 1) - (a * b)
1261 * This final expression can be implemented as a single MAD(a, -b, a)
1265 glsl_to_tgsi_visitor::try_emit_mad_for_and_not(ir_expression
*ir
, int try_operand
)
1267 const int other_operand
= 1 - try_operand
;
1270 ir_expression
*expr
= ir
->operands
[try_operand
]->as_expression();
1271 if (!expr
|| expr
->operation
!= ir_unop_logic_not
)
1274 ir
->operands
[other_operand
]->accept(this);
1276 expr
->operands
[0]->accept(this);
1279 b
.negate
= ~b
.negate
;
1281 this->result
= get_temp(ir
->type
);
1282 emit(ir
, TGSI_OPCODE_MAD
, st_dst_reg(this->result
), a
, b
, a
);
1288 glsl_to_tgsi_visitor::try_emit_sat(ir_expression
*ir
)
1290 /* Emit saturates in the vertex shader only if SM 3.0 is supported.
1292 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
&&
1293 !st_context(this->ctx
)->has_shader_model3
) {
1297 ir_rvalue
*sat_src
= ir
->as_rvalue_to_saturate();
1301 sat_src
->accept(this);
1302 st_src_reg src
= this->result
;
1304 /* If we generated an expression instruction into a temporary in
1305 * processing the saturate's operand, apply the saturate to that
1306 * instruction. Otherwise, generate a MOV to do the saturate.
1308 * Note that we have to be careful to only do this optimization if
1309 * the instruction in question was what generated src->result. For
1310 * example, ir_dereference_array might generate a MUL instruction
1311 * to create the reladdr, and return us a src reg using that
1312 * reladdr. That MUL result is not the value we're trying to
1315 ir_expression
*sat_src_expr
= sat_src
->as_expression();
1316 if (sat_src_expr
&& (sat_src_expr
->operation
== ir_binop_mul
||
1317 sat_src_expr
->operation
== ir_binop_add
||
1318 sat_src_expr
->operation
== ir_binop_dot
)) {
1319 glsl_to_tgsi_instruction
*new_inst
;
1320 new_inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
1321 new_inst
->saturate
= true;
1323 this->result
= get_temp(ir
->type
);
1324 st_dst_reg result_dst
= st_dst_reg(this->result
);
1325 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1326 glsl_to_tgsi_instruction
*inst
;
1327 inst
= emit(ir
, TGSI_OPCODE_MOV
, result_dst
, src
);
1328 inst
->saturate
= true;
1335 glsl_to_tgsi_visitor::reladdr_to_temp(ir_instruction
*ir
,
1336 st_src_reg
*reg
, int *num_reladdr
)
1341 emit_arl(ir
, address_reg
, *reg
->reladdr
);
1343 if (*num_reladdr
!= 1) {
1344 st_src_reg temp
= get_temp(glsl_type::vec4_type
);
1346 emit(ir
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), *reg
);
1354 glsl_to_tgsi_visitor::visit(ir_expression
*ir
)
1356 unsigned int operand
;
1357 st_src_reg op
[Elements(ir
->operands
)];
1358 st_src_reg result_src
;
1359 st_dst_reg result_dst
;
1361 /* Quick peephole: Emit MAD(a, b, c) instead of ADD(MUL(a, b), c)
1363 if (ir
->operation
== ir_binop_add
) {
1364 if (try_emit_mad(ir
, 1))
1366 if (try_emit_mad(ir
, 0))
1370 /* Quick peephole: Emit OPCODE_MAD(-a, -b, a) instead of AND(a, NOT(b))
1372 if (ir
->operation
== ir_binop_logic_and
) {
1373 if (try_emit_mad_for_and_not(ir
, 1))
1375 if (try_emit_mad_for_and_not(ir
, 0))
1379 if (try_emit_sat(ir
))
1382 if (ir
->operation
== ir_quadop_vector
)
1383 assert(!"ir_quadop_vector should have been lowered");
1385 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
1386 this->result
.file
= PROGRAM_UNDEFINED
;
1387 ir
->operands
[operand
]->accept(this);
1388 if (this->result
.file
== PROGRAM_UNDEFINED
) {
1390 printf("Failed to get tree for expression operand:\n");
1391 ir
->operands
[operand
]->accept(&v
);
1394 op
[operand
] = this->result
;
1396 /* Matrix expression operands should have been broken down to vector
1397 * operations already.
1399 assert(!ir
->operands
[operand
]->type
->is_matrix());
1402 int vector_elements
= ir
->operands
[0]->type
->vector_elements
;
1403 if (ir
->operands
[1]) {
1404 vector_elements
= MAX2(vector_elements
,
1405 ir
->operands
[1]->type
->vector_elements
);
1408 this->result
.file
= PROGRAM_UNDEFINED
;
1410 /* Storage for our result. Ideally for an assignment we'd be using
1411 * the actual storage for the result here, instead.
1413 result_src
= get_temp(ir
->type
);
1414 /* convenience for the emit functions below. */
1415 result_dst
= st_dst_reg(result_src
);
1416 /* Limit writes to the channels that will be used by result_src later.
1417 * This does limit this temp's use as a temporary for multi-instruction
1420 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1422 switch (ir
->operation
) {
1423 case ir_unop_logic_not
:
1424 if (result_dst
.type
!= GLSL_TYPE_FLOAT
)
1425 emit(ir
, TGSI_OPCODE_NOT
, result_dst
, op
[0]);
1427 /* Previously 'SEQ dst, src, 0.0' was used for this. However, many
1428 * older GPUs implement SEQ using multiple instructions (i915 uses two
1429 * SGE instructions and a MUL instruction). Since our logic values are
1430 * 0.0 and 1.0, 1-x also implements !x.
1432 op
[0].negate
= ~op
[0].negate
;
1433 emit(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], st_src_reg_for_float(1.0));
1437 if (result_dst
.type
== GLSL_TYPE_INT
|| result_dst
.type
== GLSL_TYPE_UINT
)
1438 emit(ir
, TGSI_OPCODE_INEG
, result_dst
, op
[0]);
1440 op
[0].negate
= ~op
[0].negate
;
1445 emit(ir
, TGSI_OPCODE_ABS
, result_dst
, op
[0]);
1448 emit(ir
, TGSI_OPCODE_SSG
, result_dst
, op
[0]);
1451 emit_scalar(ir
, TGSI_OPCODE_RCP
, result_dst
, op
[0]);
1455 emit_scalar(ir
, TGSI_OPCODE_EX2
, result_dst
, op
[0]);
1459 assert(!"not reached: should be handled by ir_explog_to_explog2");
1462 emit_scalar(ir
, TGSI_OPCODE_LG2
, result_dst
, op
[0]);
1465 emit_scalar(ir
, TGSI_OPCODE_SIN
, result_dst
, op
[0]);
1468 emit_scalar(ir
, TGSI_OPCODE_COS
, result_dst
, op
[0]);
1470 case ir_unop_sin_reduced
:
1471 emit_scs(ir
, TGSI_OPCODE_SIN
, result_dst
, op
[0]);
1473 case ir_unop_cos_reduced
:
1474 emit_scs(ir
, TGSI_OPCODE_COS
, result_dst
, op
[0]);
1478 emit(ir
, TGSI_OPCODE_DDX
, result_dst
, op
[0]);
1482 /* The X component contains 1 or -1 depending on whether the framebuffer
1483 * is a FBO or the window system buffer, respectively.
1484 * It is then multiplied with the source operand of DDY.
1486 static const gl_state_index transform_y_state
[STATE_LENGTH
]
1487 = { STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
};
1489 unsigned transform_y_index
=
1490 _mesa_add_state_reference(this->prog
->Parameters
,
1493 st_src_reg transform_y
= st_src_reg(PROGRAM_STATE_VAR
,
1495 glsl_type::vec4_type
);
1496 transform_y
.swizzle
= SWIZZLE_XXXX
;
1498 st_src_reg temp
= get_temp(glsl_type::vec4_type
);
1500 emit(ir
, TGSI_OPCODE_MUL
, st_dst_reg(temp
), transform_y
, op
[0]);
1501 emit(ir
, TGSI_OPCODE_DDY
, result_dst
, temp
);
1505 case ir_unop_noise
: {
1506 /* At some point, a motivated person could add a better
1507 * implementation of noise. Currently not even the nvidia
1508 * binary drivers do anything more than this. In any case, the
1509 * place to do this is in the GL state tracker, not the poor
1512 emit(ir
, TGSI_OPCODE_MOV
, result_dst
, st_src_reg_for_float(0.5));
1517 emit(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1520 emit(ir
, TGSI_OPCODE_SUB
, result_dst
, op
[0], op
[1]);
1524 emit(ir
, TGSI_OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1527 if (result_dst
.type
== GLSL_TYPE_FLOAT
)
1528 assert(!"not reached: should be handled by ir_div_to_mul_rcp");
1530 emit(ir
, TGSI_OPCODE_DIV
, result_dst
, op
[0], op
[1]);
1533 if (result_dst
.type
== GLSL_TYPE_FLOAT
)
1534 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
1536 emit(ir
, TGSI_OPCODE_MOD
, result_dst
, op
[0], op
[1]);
1540 emit(ir
, TGSI_OPCODE_SLT
, result_dst
, op
[0], op
[1]);
1542 case ir_binop_greater
:
1543 emit(ir
, TGSI_OPCODE_SLT
, result_dst
, op
[1], op
[0]);
1545 case ir_binop_lequal
:
1546 emit(ir
, TGSI_OPCODE_SGE
, result_dst
, op
[1], op
[0]);
1548 case ir_binop_gequal
:
1549 emit(ir
, TGSI_OPCODE_SGE
, result_dst
, op
[0], op
[1]);
1551 case ir_binop_equal
:
1552 emit(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1554 case ir_binop_nequal
:
1555 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1557 case ir_binop_all_equal
:
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
);
1565 if (native_integers
) {
1566 st_dst_reg temp_dst
= st_dst_reg(temp
);
1567 st_src_reg temp1
= st_src_reg(temp
), temp2
= st_src_reg(temp
);
1569 emit(ir
, TGSI_OPCODE_SEQ
, st_dst_reg(temp
), op
[0], op
[1]);
1571 /* Emit 1-3 AND operations to combine the SEQ results. */
1572 switch (ir
->operands
[0]->type
->vector_elements
) {
1576 temp_dst
.writemask
= WRITEMASK_Y
;
1577 temp1
.swizzle
= SWIZZLE_YYYY
;
1578 temp2
.swizzle
= SWIZZLE_ZZZZ
;
1579 emit(ir
, TGSI_OPCODE_AND
, temp_dst
, temp1
, temp2
);
1582 temp_dst
.writemask
= WRITEMASK_X
;
1583 temp1
.swizzle
= SWIZZLE_XXXX
;
1584 temp2
.swizzle
= SWIZZLE_YYYY
;
1585 emit(ir
, TGSI_OPCODE_AND
, temp_dst
, temp1
, temp2
);
1586 temp_dst
.writemask
= WRITEMASK_Y
;
1587 temp1
.swizzle
= SWIZZLE_ZZZZ
;
1588 temp2
.swizzle
= SWIZZLE_WWWW
;
1589 emit(ir
, TGSI_OPCODE_AND
, temp_dst
, temp1
, temp2
);
1592 temp1
.swizzle
= SWIZZLE_XXXX
;
1593 temp2
.swizzle
= SWIZZLE_YYYY
;
1594 emit(ir
, TGSI_OPCODE_AND
, result_dst
, temp1
, temp2
);
1596 emit(ir
, TGSI_OPCODE_SNE
, st_dst_reg(temp
), op
[0], op
[1]);
1598 /* After the dot-product, the value will be an integer on the
1599 * range [0,4]. Zero becomes 1.0, and positive values become zero.
1601 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1603 /* Negating the result of the dot-product gives values on the range
1604 * [-4, 0]. Zero becomes 1.0, and negative values become zero.
1605 * This is achieved using SGE.
1607 st_src_reg sge_src
= result_src
;
1608 sge_src
.negate
= ~sge_src
.negate
;
1609 emit(ir
, TGSI_OPCODE_SGE
, result_dst
, sge_src
, st_src_reg_for_float(0.0));
1612 emit(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1615 case ir_binop_any_nequal
:
1616 /* "!=" operator producing a scalar boolean. */
1617 if (ir
->operands
[0]->type
->is_vector() ||
1618 ir
->operands
[1]->type
->is_vector()) {
1619 st_src_reg temp
= get_temp(native_integers
?
1620 glsl_type::get_instance(ir
->operands
[0]->type
->base_type
, 4, 1) :
1621 glsl_type::vec4_type
);
1622 emit(ir
, TGSI_OPCODE_SNE
, st_dst_reg(temp
), op
[0], op
[1]);
1624 if (native_integers
) {
1625 st_dst_reg temp_dst
= st_dst_reg(temp
);
1626 st_src_reg temp1
= st_src_reg(temp
), temp2
= st_src_reg(temp
);
1628 /* Emit 1-3 OR operations to combine the SNE results. */
1629 switch (ir
->operands
[0]->type
->vector_elements
) {
1633 temp_dst
.writemask
= WRITEMASK_Y
;
1634 temp1
.swizzle
= SWIZZLE_YYYY
;
1635 temp2
.swizzle
= SWIZZLE_ZZZZ
;
1636 emit(ir
, TGSI_OPCODE_OR
, temp_dst
, temp1
, temp2
);
1639 temp_dst
.writemask
= WRITEMASK_X
;
1640 temp1
.swizzle
= SWIZZLE_XXXX
;
1641 temp2
.swizzle
= SWIZZLE_YYYY
;
1642 emit(ir
, TGSI_OPCODE_OR
, temp_dst
, temp1
, temp2
);
1643 temp_dst
.writemask
= WRITEMASK_Y
;
1644 temp1
.swizzle
= SWIZZLE_ZZZZ
;
1645 temp2
.swizzle
= SWIZZLE_WWWW
;
1646 emit(ir
, TGSI_OPCODE_OR
, temp_dst
, temp1
, temp2
);
1649 temp1
.swizzle
= SWIZZLE_XXXX
;
1650 temp2
.swizzle
= SWIZZLE_YYYY
;
1651 emit(ir
, TGSI_OPCODE_OR
, result_dst
, temp1
, temp2
);
1653 /* After the dot-product, the value will be an integer on the
1654 * range [0,4]. Zero stays zero, and positive values become 1.0.
1656 glsl_to_tgsi_instruction
*const dp
=
1657 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1658 if (this->prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1659 /* The clamping to [0,1] can be done for free in the fragment
1660 * shader with a saturate.
1662 dp
->saturate
= true;
1664 /* Negating the result of the dot-product gives values on the range
1665 * [-4, 0]. Zero stays zero, and negative values become 1.0. This
1666 * achieved using SLT.
1668 st_src_reg slt_src
= result_src
;
1669 slt_src
.negate
= ~slt_src
.negate
;
1670 emit(ir
, TGSI_OPCODE_SLT
, result_dst
, slt_src
, st_src_reg_for_float(0.0));
1674 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1679 assert(ir
->operands
[0]->type
->is_vector());
1681 /* After the dot-product, the value will be an integer on the
1682 * range [0,4]. Zero stays zero, and positive values become 1.0.
1684 glsl_to_tgsi_instruction
*const dp
=
1685 emit_dp(ir
, result_dst
, op
[0], op
[0],
1686 ir
->operands
[0]->type
->vector_elements
);
1687 if (this->prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
&&
1688 result_dst
.type
== GLSL_TYPE_FLOAT
) {
1689 /* The clamping to [0,1] can be done for free in the fragment
1690 * shader with a saturate.
1692 dp
->saturate
= true;
1693 } else if (result_dst
.type
== GLSL_TYPE_FLOAT
) {
1694 /* Negating the result of the dot-product gives values on the range
1695 * [-4, 0]. Zero stays zero, and negative values become 1.0. This
1696 * is achieved using SLT.
1698 st_src_reg slt_src
= result_src
;
1699 slt_src
.negate
= ~slt_src
.negate
;
1700 emit(ir
, TGSI_OPCODE_SLT
, result_dst
, slt_src
, st_src_reg_for_float(0.0));
1703 /* Use SNE 0 if integers are being used as boolean values. */
1704 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, result_src
, st_src_reg_for_int(0));
1709 case ir_binop_logic_xor
:
1710 if (native_integers
)
1711 emit(ir
, TGSI_OPCODE_XOR
, result_dst
, op
[0], op
[1]);
1713 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1716 case ir_binop_logic_or
: {
1717 if (native_integers
) {
1718 /* If integers are used as booleans, we can use an actual "or"
1721 assert(native_integers
);
1722 emit(ir
, TGSI_OPCODE_OR
, result_dst
, op
[0], op
[1]);
1724 /* After the addition, the value will be an integer on the
1725 * range [0,2]. Zero stays zero, and positive values become 1.0.
1727 glsl_to_tgsi_instruction
*add
=
1728 emit(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1729 if (this->prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1730 /* The clamping to [0,1] can be done for free in the fragment
1731 * shader with a saturate if floats are being used as boolean values.
1733 add
->saturate
= true;
1735 /* Negating the result of the addition gives values on the range
1736 * [-2, 0]. Zero stays zero, and negative values become 1.0. This
1737 * is achieved using SLT.
1739 st_src_reg slt_src
= result_src
;
1740 slt_src
.negate
= ~slt_src
.negate
;
1741 emit(ir
, TGSI_OPCODE_SLT
, result_dst
, slt_src
, st_src_reg_for_float(0.0));
1747 case ir_binop_logic_and
:
1748 /* If native integers are disabled, the bool args are stored as float 0.0
1749 * or 1.0, so "mul" gives us "and". If they're enabled, just use the
1750 * actual AND opcode.
1752 if (native_integers
)
1753 emit(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], op
[1]);
1755 emit(ir
, TGSI_OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1759 assert(ir
->operands
[0]->type
->is_vector());
1760 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
1761 emit_dp(ir
, result_dst
, op
[0], op
[1],
1762 ir
->operands
[0]->type
->vector_elements
);
1767 emit_scalar(ir
, TGSI_OPCODE_SQRT
, result_dst
, op
[0]);
1770 /* sqrt(x) = x * rsq(x). */
1771 emit_scalar(ir
, TGSI_OPCODE_RSQ
, result_dst
, op
[0]);
1772 emit(ir
, TGSI_OPCODE_MUL
, result_dst
, result_src
, op
[0]);
1773 /* For incoming channels <= 0, set the result to 0. */
1774 op
[0].negate
= ~op
[0].negate
;
1775 emit(ir
, TGSI_OPCODE_CMP
, result_dst
,
1776 op
[0], result_src
, st_src_reg_for_float(0.0));
1780 emit_scalar(ir
, TGSI_OPCODE_RSQ
, result_dst
, op
[0]);
1783 if (native_integers
) {
1784 emit(ir
, TGSI_OPCODE_I2F
, result_dst
, op
[0]);
1787 /* fallthrough to next case otherwise */
1789 if (native_integers
) {
1790 emit(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], st_src_reg_for_float(1.0));
1793 /* fallthrough to next case otherwise */
1796 /* Converting between signed and unsigned integers is a no-op. */
1800 if (native_integers
) {
1801 /* Booleans are stored as integers using ~0 for true and 0 for false.
1802 * GLSL requires that int(bool) return 1 for true and 0 for false.
1803 * This conversion is done with AND, but it could be done with NEG.
1805 emit(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], st_src_reg_for_int(1));
1807 /* Booleans and integers are both stored as floats when native
1808 * integers are disabled.
1814 if (native_integers
)
1815 emit(ir
, TGSI_OPCODE_F2I
, result_dst
, op
[0]);
1817 emit(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
1820 if (native_integers
)
1821 emit(ir
, TGSI_OPCODE_F2U
, result_dst
, op
[0]);
1823 emit(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
1825 case ir_unop_bitcast_f2i
:
1826 case ir_unop_bitcast_f2u
:
1827 case ir_unop_bitcast_i2f
:
1828 case ir_unop_bitcast_u2f
:
1832 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], st_src_reg_for_float(0.0));
1835 if (native_integers
)
1836 emit(ir
, TGSI_OPCODE_INEG
, result_dst
, op
[0]);
1838 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], st_src_reg_for_float(0.0));
1841 emit(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
1844 emit(ir
, TGSI_OPCODE_CEIL
, result_dst
, op
[0]);
1847 emit(ir
, TGSI_OPCODE_FLR
, result_dst
, op
[0]);
1849 case ir_unop_round_even
:
1850 emit(ir
, TGSI_OPCODE_ROUND
, result_dst
, op
[0]);
1853 emit(ir
, TGSI_OPCODE_FRC
, result_dst
, op
[0]);
1857 emit(ir
, TGSI_OPCODE_MIN
, result_dst
, op
[0], op
[1]);
1860 emit(ir
, TGSI_OPCODE_MAX
, result_dst
, op
[0], op
[1]);
1863 emit_scalar(ir
, TGSI_OPCODE_POW
, result_dst
, op
[0], op
[1]);
1866 case ir_unop_bit_not
:
1867 if (native_integers
) {
1868 emit(ir
, TGSI_OPCODE_NOT
, result_dst
, op
[0]);
1872 if (native_integers
) {
1873 emit(ir
, TGSI_OPCODE_U2F
, result_dst
, op
[0]);
1876 case ir_binop_lshift
:
1877 if (native_integers
) {
1878 emit(ir
, TGSI_OPCODE_SHL
, result_dst
, op
[0], op
[1]);
1881 case ir_binop_rshift
:
1882 if (native_integers
) {
1883 emit(ir
, TGSI_OPCODE_ISHR
, result_dst
, op
[0], op
[1]);
1886 case ir_binop_bit_and
:
1887 if (native_integers
) {
1888 emit(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], op
[1]);
1891 case ir_binop_bit_xor
:
1892 if (native_integers
) {
1893 emit(ir
, TGSI_OPCODE_XOR
, result_dst
, op
[0], op
[1]);
1896 case ir_binop_bit_or
:
1897 if (native_integers
) {
1898 emit(ir
, TGSI_OPCODE_OR
, result_dst
, op
[0], op
[1]);
1902 assert(!"GLSL 1.30 features unsupported");
1905 case ir_binop_ubo_load
: {
1906 ir_constant
*uniform_block
= ir
->operands
[0]->as_constant();
1907 ir_constant
*const_offset_ir
= ir
->operands
[1]->as_constant();
1908 unsigned const_offset
= const_offset_ir
? const_offset_ir
->value
.u
[0] : 0;
1909 st_src_reg index_reg
= get_temp(glsl_type::uint_type
);
1912 cbuf
.type
= glsl_type::vec4_type
->base_type
;
1913 cbuf
.file
= PROGRAM_CONSTANT
;
1915 cbuf
.index2D
= uniform_block
->value
.u
[0] + 1;
1916 cbuf
.reladdr
= NULL
;
1919 assert(ir
->type
->is_vector() || ir
->type
->is_scalar());
1921 if (const_offset_ir
) {
1922 index_reg
= st_src_reg_for_int(const_offset
/ 16);
1924 emit(ir
, TGSI_OPCODE_USHR
, st_dst_reg(index_reg
), op
[1], st_src_reg_for_int(4));
1927 cbuf
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1928 cbuf
.swizzle
+= MAKE_SWIZZLE4(const_offset
% 16 / 4,
1929 const_offset
% 16 / 4,
1930 const_offset
% 16 / 4,
1931 const_offset
% 16 / 4);
1933 cbuf
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
1934 memcpy(cbuf
.reladdr
, &index_reg
, sizeof(index_reg
));
1936 if (ir
->type
->base_type
== GLSL_TYPE_BOOL
) {
1937 emit(ir
, TGSI_OPCODE_USNE
, result_dst
, cbuf
, st_src_reg_for_int(0));
1938 result_src
.negate
= 1;
1939 emit(ir
, TGSI_OPCODE_UCMP
, result_dst
, result_src
, st_src_reg_for_int(~0), st_src_reg_for_int(0));
1941 emit(ir
, TGSI_OPCODE_MOV
, result_dst
, cbuf
);
1946 /* note: we have to reorder the three args here */
1947 emit(ir
, TGSI_OPCODE_LRP
, result_dst
, op
[2], op
[1], op
[0]);
1949 case ir_unop_pack_snorm_2x16
:
1950 case ir_unop_pack_unorm_2x16
:
1951 case ir_unop_pack_half_2x16
:
1952 case ir_unop_pack_snorm_4x8
:
1953 case ir_unop_pack_unorm_4x8
:
1954 case ir_unop_unpack_snorm_2x16
:
1955 case ir_unop_unpack_unorm_2x16
:
1956 case ir_unop_unpack_half_2x16
:
1957 case ir_unop_unpack_half_2x16_split_x
:
1958 case ir_unop_unpack_half_2x16_split_y
:
1959 case ir_unop_unpack_snorm_4x8
:
1960 case ir_unop_unpack_unorm_4x8
:
1961 case ir_binop_pack_half_2x16_split
:
1962 case ir_quadop_vector
:
1963 /* This operation is not supported, or should have already been handled.
1965 assert(!"Invalid ir opcode in glsl_to_tgsi_visitor::visit()");
1969 this->result
= result_src
;
1974 glsl_to_tgsi_visitor::visit(ir_swizzle
*ir
)
1980 /* Note that this is only swizzles in expressions, not those on the left
1981 * hand side of an assignment, which do write masking. See ir_assignment
1985 ir
->val
->accept(this);
1987 assert(src
.file
!= PROGRAM_UNDEFINED
);
1989 for (i
= 0; i
< 4; i
++) {
1990 if (i
< ir
->type
->vector_elements
) {
1993 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.x
);
1996 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.y
);
1999 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.z
);
2002 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.w
);
2006 /* If the type is smaller than a vec4, replicate the last
2009 swizzle
[i
] = swizzle
[ir
->type
->vector_elements
- 1];
2013 src
.swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
2019 glsl_to_tgsi_visitor::visit(ir_dereference_variable
*ir
)
2021 variable_storage
*entry
= find_variable_storage(ir
->var
);
2022 ir_variable
*var
= ir
->var
;
2025 switch (var
->mode
) {
2026 case ir_var_uniform
:
2027 entry
= new(mem_ctx
) variable_storage(var
, PROGRAM_UNIFORM
,
2029 this->variables
.push_tail(entry
);
2031 case ir_var_shader_in
:
2032 /* The linker assigns locations for varyings and attributes,
2033 * including deprecated builtins (like gl_Color), user-assign
2034 * generic attributes (glBindVertexLocation), and
2035 * user-defined varyings.
2037 assert(var
->location
!= -1);
2038 entry
= new(mem_ctx
) variable_storage(var
,
2042 case ir_var_shader_out
:
2043 assert(var
->location
!= -1);
2044 entry
= new(mem_ctx
) variable_storage(var
,
2046 var
->location
+ var
->index
);
2048 case ir_var_system_value
:
2049 entry
= new(mem_ctx
) variable_storage(var
,
2050 PROGRAM_SYSTEM_VALUE
,
2054 case ir_var_temporary
:
2055 entry
= new(mem_ctx
) variable_storage(var
, PROGRAM_TEMPORARY
,
2057 this->variables
.push_tail(entry
);
2059 next_temp
+= type_size(var
->type
);
2064 printf("Failed to make storage for %s\n", var
->name
);
2069 this->result
= st_src_reg(entry
->file
, entry
->index
, var
->type
);
2070 if (!native_integers
)
2071 this->result
.type
= GLSL_TYPE_FLOAT
;
2075 glsl_to_tgsi_visitor::visit(ir_dereference_array
*ir
)
2079 int element_size
= type_size(ir
->type
);
2081 index
= ir
->array_index
->constant_expression_value();
2083 ir
->array
->accept(this);
2087 src
.index
+= index
->value
.i
[0] * element_size
;
2089 /* Variable index array dereference. It eats the "vec4" of the
2090 * base of the array and an index that offsets the TGSI register
2093 ir
->array_index
->accept(this);
2095 st_src_reg index_reg
;
2097 if (element_size
== 1) {
2098 index_reg
= this->result
;
2100 index_reg
= get_temp(native_integers
?
2101 glsl_type::int_type
: glsl_type::float_type
);
2103 emit(ir
, TGSI_OPCODE_MUL
, st_dst_reg(index_reg
),
2104 this->result
, st_src_reg_for_type(index_reg
.type
, element_size
));
2107 /* If there was already a relative address register involved, add the
2108 * new and the old together to get the new offset.
2110 if (src
.reladdr
!= NULL
) {
2111 st_src_reg accum_reg
= get_temp(native_integers
?
2112 glsl_type::int_type
: glsl_type::float_type
);
2114 emit(ir
, TGSI_OPCODE_ADD
, st_dst_reg(accum_reg
),
2115 index_reg
, *src
.reladdr
);
2117 index_reg
= accum_reg
;
2120 src
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
2121 memcpy(src
.reladdr
, &index_reg
, sizeof(index_reg
));
2124 /* If the type is smaller than a vec4, replicate the last channel out. */
2125 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
2126 src
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
2128 src
.swizzle
= SWIZZLE_NOOP
;
2130 /* Change the register type to the element type of the array. */
2131 src
.type
= ir
->type
->base_type
;
2137 glsl_to_tgsi_visitor::visit(ir_dereference_record
*ir
)
2140 const glsl_type
*struct_type
= ir
->record
->type
;
2143 ir
->record
->accept(this);
2145 for (i
= 0; i
< struct_type
->length
; i
++) {
2146 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
2148 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
2151 /* If the type is smaller than a vec4, replicate the last channel out. */
2152 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
2153 this->result
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
2155 this->result
.swizzle
= SWIZZLE_NOOP
;
2157 this->result
.index
+= offset
;
2158 this->result
.type
= ir
->type
->base_type
;
2162 * We want to be careful in assignment setup to hit the actual storage
2163 * instead of potentially using a temporary like we might with the
2164 * ir_dereference handler.
2167 get_assignment_lhs(ir_dereference
*ir
, glsl_to_tgsi_visitor
*v
)
2169 /* The LHS must be a dereference. If the LHS is a variable indexed array
2170 * access of a vector, it must be separated into a series conditional moves
2171 * before reaching this point (see ir_vec_index_to_cond_assign).
2173 assert(ir
->as_dereference());
2174 ir_dereference_array
*deref_array
= ir
->as_dereference_array();
2176 assert(!deref_array
->array
->type
->is_vector());
2179 /* Use the rvalue deref handler for the most part. We'll ignore
2180 * swizzles in it and write swizzles using writemask, though.
2183 return st_dst_reg(v
->result
);
2187 * Process the condition of a conditional assignment
2189 * Examines the condition of a conditional assignment to generate the optimal
2190 * first operand of a \c CMP instruction. If the condition is a relational
2191 * operator with 0 (e.g., \c ir_binop_less), the value being compared will be
2192 * used as the source for the \c CMP instruction. Otherwise the comparison
2193 * is processed to a boolean result, and the boolean result is used as the
2194 * operand to the CMP instruction.
2197 glsl_to_tgsi_visitor::process_move_condition(ir_rvalue
*ir
)
2199 ir_rvalue
*src_ir
= ir
;
2201 bool switch_order
= false;
2203 ir_expression
*const expr
= ir
->as_expression();
2204 if ((expr
!= NULL
) && (expr
->get_num_operands() == 2)) {
2205 bool zero_on_left
= false;
2207 if (expr
->operands
[0]->is_zero()) {
2208 src_ir
= expr
->operands
[1];
2209 zero_on_left
= true;
2210 } else if (expr
->operands
[1]->is_zero()) {
2211 src_ir
= expr
->operands
[0];
2212 zero_on_left
= false;
2216 * (a < 0) T F F ( a < 0) T F F
2217 * (0 < a) F F T (-a < 0) F F T
2218 * (a <= 0) T T F (-a < 0) F F T (swap order of other operands)
2219 * (0 <= a) F T T ( a < 0) T F F (swap order of other operands)
2220 * (a > 0) F F T (-a < 0) F F T
2221 * (0 > a) T F F ( a < 0) T F F
2222 * (a >= 0) F T T ( a < 0) T F F (swap order of other operands)
2223 * (0 >= a) T T F (-a < 0) F F T (swap order of other operands)
2225 * Note that exchanging the order of 0 and 'a' in the comparison simply
2226 * means that the value of 'a' should be negated.
2229 switch (expr
->operation
) {
2231 switch_order
= false;
2232 negate
= zero_on_left
;
2235 case ir_binop_greater
:
2236 switch_order
= false;
2237 negate
= !zero_on_left
;
2240 case ir_binop_lequal
:
2241 switch_order
= true;
2242 negate
= !zero_on_left
;
2245 case ir_binop_gequal
:
2246 switch_order
= true;
2247 negate
= zero_on_left
;
2251 /* This isn't the right kind of comparison afterall, so make sure
2252 * the whole condition is visited.
2260 src_ir
->accept(this);
2262 /* We use the TGSI_OPCODE_CMP (a < 0 ? b : c) for conditional moves, and the
2263 * condition we produced is 0.0 or 1.0. By flipping the sign, we can
2264 * choose which value TGSI_OPCODE_CMP produces without an extra instruction
2265 * computing the condition.
2268 this->result
.negate
= ~this->result
.negate
;
2270 return switch_order
;
2274 glsl_to_tgsi_visitor::emit_block_mov(ir_assignment
*ir
, const struct glsl_type
*type
,
2275 st_dst_reg
*l
, st_src_reg
*r
)
2277 if (type
->base_type
== GLSL_TYPE_STRUCT
) {
2278 for (unsigned int i
= 0; i
< type
->length
; i
++) {
2279 emit_block_mov(ir
, type
->fields
.structure
[i
].type
, l
, r
);
2284 if (type
->is_array()) {
2285 for (unsigned int i
= 0; i
< type
->length
; i
++) {
2286 emit_block_mov(ir
, type
->fields
.array
, l
, r
);
2291 if (type
->is_matrix()) {
2292 const struct glsl_type
*vec_type
;
2294 vec_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
,
2295 type
->vector_elements
, 1);
2297 for (int i
= 0; i
< type
->matrix_columns
; i
++) {
2298 emit_block_mov(ir
, vec_type
, l
, r
);
2303 assert(type
->is_scalar() || type
->is_vector());
2305 r
->type
= type
->base_type
;
2306 emit(ir
, TGSI_OPCODE_MOV
, *l
, *r
);
2312 glsl_to_tgsi_visitor::visit(ir_assignment
*ir
)
2318 ir
->rhs
->accept(this);
2321 l
= get_assignment_lhs(ir
->lhs
, this);
2323 /* FINISHME: This should really set to the correct maximal writemask for each
2324 * FINISHME: component written (in the loops below). This case can only
2325 * FINISHME: occur for matrices, arrays, and structures.
2327 if (ir
->write_mask
== 0) {
2328 assert(!ir
->lhs
->type
->is_scalar() && !ir
->lhs
->type
->is_vector());
2329 l
.writemask
= WRITEMASK_XYZW
;
2330 } else if (ir
->lhs
->type
->is_scalar() &&
2331 ir
->lhs
->variable_referenced()->mode
== ir_var_shader_out
) {
2332 /* FINISHME: This hack makes writing to gl_FragDepth, which lives in the
2333 * FINISHME: W component of fragment shader output zero, work correctly.
2335 l
.writemask
= WRITEMASK_XYZW
;
2338 int first_enabled_chan
= 0;
2341 l
.writemask
= ir
->write_mask
;
2343 for (int i
= 0; i
< 4; i
++) {
2344 if (l
.writemask
& (1 << i
)) {
2345 first_enabled_chan
= GET_SWZ(r
.swizzle
, i
);
2350 /* Swizzle a small RHS vector into the channels being written.
2352 * glsl ir treats write_mask as dictating how many channels are
2353 * present on the RHS while TGSI treats write_mask as just
2354 * showing which channels of the vec4 RHS get written.
2356 for (int i
= 0; i
< 4; i
++) {
2357 if (l
.writemask
& (1 << i
))
2358 swizzles
[i
] = GET_SWZ(r
.swizzle
, rhs_chan
++);
2360 swizzles
[i
] = first_enabled_chan
;
2362 r
.swizzle
= MAKE_SWIZZLE4(swizzles
[0], swizzles
[1],
2363 swizzles
[2], swizzles
[3]);
2366 assert(l
.file
!= PROGRAM_UNDEFINED
);
2367 assert(r
.file
!= PROGRAM_UNDEFINED
);
2369 if (ir
->condition
) {
2370 const bool switch_order
= this->process_move_condition(ir
->condition
);
2371 st_src_reg condition
= this->result
;
2373 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
2374 st_src_reg l_src
= st_src_reg(l
);
2375 st_src_reg condition_temp
= condition
;
2376 l_src
.swizzle
= swizzle_for_size(ir
->lhs
->type
->vector_elements
);
2378 if (native_integers
) {
2379 /* This is necessary because TGSI's CMP instruction expects the
2380 * condition to be a float, and we store booleans as integers.
2381 * If TGSI had a UCMP instruction or similar, this extra
2382 * instruction would not be necessary.
2384 condition_temp
= get_temp(glsl_type::vec4_type
);
2385 condition
.negate
= 0;
2386 emit(ir
, TGSI_OPCODE_I2F
, st_dst_reg(condition_temp
), condition
);
2387 condition_temp
.swizzle
= condition
.swizzle
;
2391 emit(ir
, TGSI_OPCODE_CMP
, l
, condition_temp
, l_src
, r
);
2393 emit(ir
, TGSI_OPCODE_CMP
, l
, condition_temp
, r
, l_src
);
2399 } else if (ir
->rhs
->as_expression() &&
2400 this->instructions
.get_tail() &&
2401 ir
->rhs
== ((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->ir
&&
2402 type_size(ir
->lhs
->type
) == 1 &&
2403 l
.writemask
== ((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->dst
.writemask
) {
2404 /* To avoid emitting an extra MOV when assigning an expression to a
2405 * variable, emit the last instruction of the expression again, but
2406 * replace the destination register with the target of the assignment.
2407 * Dead code elimination will remove the original instruction.
2409 glsl_to_tgsi_instruction
*inst
, *new_inst
;
2410 inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
2411 new_inst
= emit(ir
, inst
->op
, l
, inst
->src
[0], inst
->src
[1], inst
->src
[2]);
2412 new_inst
->saturate
= inst
->saturate
;
2413 inst
->dead_mask
= inst
->dst
.writemask
;
2415 emit_block_mov(ir
, ir
->rhs
->type
, &l
, &r
);
2421 glsl_to_tgsi_visitor::visit(ir_constant
*ir
)
2424 GLfloat stack_vals
[4] = { 0 };
2425 gl_constant_value
*values
= (gl_constant_value
*) stack_vals
;
2426 GLenum gl_type
= GL_NONE
;
2428 static int in_array
= 0;
2429 gl_register_file file
= in_array
? PROGRAM_CONSTANT
: PROGRAM_IMMEDIATE
;
2431 /* Unfortunately, 4 floats is all we can get into
2432 * _mesa_add_typed_unnamed_constant. So, make a temp to store an
2433 * aggregate constant and move each constant value into it. If we
2434 * get lucky, copy propagation will eliminate the extra moves.
2436 if (ir
->type
->base_type
== GLSL_TYPE_STRUCT
) {
2437 st_src_reg temp_base
= get_temp(ir
->type
);
2438 st_dst_reg temp
= st_dst_reg(temp_base
);
2440 foreach_iter(exec_list_iterator
, iter
, ir
->components
) {
2441 ir_constant
*field_value
= (ir_constant
*)iter
.get();
2442 int size
= type_size(field_value
->type
);
2446 field_value
->accept(this);
2449 for (i
= 0; i
< (unsigned int)size
; i
++) {
2450 emit(ir
, TGSI_OPCODE_MOV
, temp
, src
);
2456 this->result
= temp_base
;
2460 if (ir
->type
->is_array()) {
2461 st_src_reg temp_base
= get_temp(ir
->type
);
2462 st_dst_reg temp
= st_dst_reg(temp_base
);
2463 int size
= type_size(ir
->type
->fields
.array
);
2468 for (i
= 0; i
< ir
->type
->length
; i
++) {
2469 ir
->array_elements
[i
]->accept(this);
2471 for (int j
= 0; j
< size
; j
++) {
2472 emit(ir
, TGSI_OPCODE_MOV
, temp
, src
);
2478 this->result
= temp_base
;
2483 if (ir
->type
->is_matrix()) {
2484 st_src_reg mat
= get_temp(ir
->type
);
2485 st_dst_reg mat_column
= st_dst_reg(mat
);
2487 for (i
= 0; i
< ir
->type
->matrix_columns
; i
++) {
2488 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
2489 values
= (gl_constant_value
*) &ir
->value
.f
[i
* ir
->type
->vector_elements
];
2491 src
= st_src_reg(file
, -1, ir
->type
->base_type
);
2492 src
.index
= add_constant(file
,
2494 ir
->type
->vector_elements
,
2497 emit(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
2506 switch (ir
->type
->base_type
) {
2507 case GLSL_TYPE_FLOAT
:
2509 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2510 values
[i
].f
= ir
->value
.f
[i
];
2513 case GLSL_TYPE_UINT
:
2514 gl_type
= native_integers
? GL_UNSIGNED_INT
: GL_FLOAT
;
2515 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2516 if (native_integers
)
2517 values
[i
].u
= ir
->value
.u
[i
];
2519 values
[i
].f
= ir
->value
.u
[i
];
2523 gl_type
= native_integers
? GL_INT
: GL_FLOAT
;
2524 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2525 if (native_integers
)
2526 values
[i
].i
= ir
->value
.i
[i
];
2528 values
[i
].f
= ir
->value
.i
[i
];
2531 case GLSL_TYPE_BOOL
:
2532 gl_type
= native_integers
? GL_BOOL
: GL_FLOAT
;
2533 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2534 if (native_integers
)
2535 values
[i
].u
= ir
->value
.b
[i
] ? ~0 : 0;
2537 values
[i
].f
= ir
->value
.b
[i
];
2541 assert(!"Non-float/uint/int/bool constant");
2544 this->result
= st_src_reg(file
, -1, ir
->type
);
2545 this->result
.index
= add_constant(file
,
2547 ir
->type
->vector_elements
,
2549 &this->result
.swizzle
);
2553 glsl_to_tgsi_visitor::get_function_signature(ir_function_signature
*sig
)
2555 function_entry
*entry
;
2557 foreach_iter(exec_list_iterator
, iter
, this->function_signatures
) {
2558 entry
= (function_entry
*)iter
.get();
2560 if (entry
->sig
== sig
)
2564 entry
= ralloc(mem_ctx
, function_entry
);
2566 entry
->sig_id
= this->next_signature_id
++;
2567 entry
->bgn_inst
= NULL
;
2569 /* Allocate storage for all the parameters. */
2570 foreach_iter(exec_list_iterator
, iter
, sig
->parameters
) {
2571 ir_variable
*param
= (ir_variable
*)iter
.get();
2572 variable_storage
*storage
;
2574 storage
= find_variable_storage(param
);
2577 storage
= new(mem_ctx
) variable_storage(param
, PROGRAM_TEMPORARY
,
2579 this->variables
.push_tail(storage
);
2581 this->next_temp
+= type_size(param
->type
);
2584 if (!sig
->return_type
->is_void()) {
2585 entry
->return_reg
= get_temp(sig
->return_type
);
2587 entry
->return_reg
= undef_src
;
2590 this->function_signatures
.push_tail(entry
);
2595 glsl_to_tgsi_visitor::visit(ir_call
*ir
)
2597 glsl_to_tgsi_instruction
*call_inst
;
2598 ir_function_signature
*sig
= ir
->callee
;
2599 function_entry
*entry
= get_function_signature(sig
);
2602 /* Process in parameters. */
2603 exec_list_iterator sig_iter
= sig
->parameters
.iterator();
2604 foreach_iter(exec_list_iterator
, iter
, *ir
) {
2605 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
2606 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
2608 if (param
->mode
== ir_var_function_in
||
2609 param
->mode
== ir_var_function_inout
) {
2610 variable_storage
*storage
= find_variable_storage(param
);
2613 param_rval
->accept(this);
2614 st_src_reg r
= this->result
;
2617 l
.file
= storage
->file
;
2618 l
.index
= storage
->index
;
2620 l
.writemask
= WRITEMASK_XYZW
;
2621 l
.cond_mask
= COND_TR
;
2623 for (i
= 0; i
< type_size(param
->type
); i
++) {
2624 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
2632 assert(!sig_iter
.has_next());
2634 /* Emit call instruction */
2635 call_inst
= emit(ir
, TGSI_OPCODE_CAL
);
2636 call_inst
->function
= entry
;
2638 /* Process out parameters. */
2639 sig_iter
= sig
->parameters
.iterator();
2640 foreach_iter(exec_list_iterator
, iter
, *ir
) {
2641 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
2642 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
2644 if (param
->mode
== ir_var_function_out
||
2645 param
->mode
== ir_var_function_inout
) {
2646 variable_storage
*storage
= find_variable_storage(param
);
2650 r
.file
= storage
->file
;
2651 r
.index
= storage
->index
;
2653 r
.swizzle
= SWIZZLE_NOOP
;
2656 param_rval
->accept(this);
2657 st_dst_reg l
= st_dst_reg(this->result
);
2659 for (i
= 0; i
< type_size(param
->type
); i
++) {
2660 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
2668 assert(!sig_iter
.has_next());
2670 /* Process return value. */
2671 this->result
= entry
->return_reg
;
2675 glsl_to_tgsi_visitor::visit(ir_texture
*ir
)
2677 st_src_reg result_src
, coord
, cube_sc
, lod_info
, projector
, dx
, dy
, offset
;
2678 st_dst_reg result_dst
, coord_dst
, cube_sc_dst
;
2679 glsl_to_tgsi_instruction
*inst
= NULL
;
2680 unsigned opcode
= TGSI_OPCODE_NOP
;
2681 const glsl_type
*sampler_type
= ir
->sampler
->type
;
2682 bool is_cube_array
= false;
2684 /* if we are a cube array sampler */
2685 if ((sampler_type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
&&
2686 sampler_type
->sampler_array
)) {
2687 is_cube_array
= true;
2690 if (ir
->coordinate
) {
2691 ir
->coordinate
->accept(this);
2693 /* Put our coords in a temp. We'll need to modify them for shadow,
2694 * projection, or LOD, so the only case we'd use it as is is if
2695 * we're doing plain old texturing. The optimization passes on
2696 * glsl_to_tgsi_visitor should handle cleaning up our mess in that case.
2698 coord
= get_temp(glsl_type::vec4_type
);
2699 coord_dst
= st_dst_reg(coord
);
2700 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
2703 if (ir
->projector
) {
2704 ir
->projector
->accept(this);
2705 projector
= this->result
;
2708 /* Storage for our result. Ideally for an assignment we'd be using
2709 * the actual storage for the result here, instead.
2711 result_src
= get_temp(ir
->type
);
2712 result_dst
= st_dst_reg(result_src
);
2716 opcode
= (is_cube_array
&& ir
->shadow_comparitor
) ? TGSI_OPCODE_TEX2
: TGSI_OPCODE_TEX
;
2718 ir
->offset
->accept(this);
2719 offset
= this->result
;
2723 opcode
= is_cube_array
? TGSI_OPCODE_TXB2
: TGSI_OPCODE_TXB
;
2724 ir
->lod_info
.bias
->accept(this);
2725 lod_info
= this->result
;
2727 ir
->offset
->accept(this);
2728 offset
= this->result
;
2732 opcode
= is_cube_array
? TGSI_OPCODE_TXL2
: TGSI_OPCODE_TXL
;
2733 ir
->lod_info
.lod
->accept(this);
2734 lod_info
= this->result
;
2736 ir
->offset
->accept(this);
2737 offset
= this->result
;
2741 opcode
= TGSI_OPCODE_TXD
;
2742 ir
->lod_info
.grad
.dPdx
->accept(this);
2744 ir
->lod_info
.grad
.dPdy
->accept(this);
2747 ir
->offset
->accept(this);
2748 offset
= this->result
;
2752 opcode
= TGSI_OPCODE_TXQ
;
2753 ir
->lod_info
.lod
->accept(this);
2754 lod_info
= this->result
;
2757 opcode
= TGSI_OPCODE_TXF
;
2758 ir
->lod_info
.lod
->accept(this);
2759 lod_info
= this->result
;
2761 ir
->offset
->accept(this);
2762 offset
= this->result
;
2766 assert(!"Unexpected ir_txf_ms opcode");
2770 if (ir
->projector
) {
2771 if (opcode
== TGSI_OPCODE_TEX
) {
2772 /* Slot the projector in as the last component of the coord. */
2773 coord_dst
.writemask
= WRITEMASK_W
;
2774 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, projector
);
2775 coord_dst
.writemask
= WRITEMASK_XYZW
;
2776 opcode
= TGSI_OPCODE_TXP
;
2778 st_src_reg coord_w
= coord
;
2779 coord_w
.swizzle
= SWIZZLE_WWWW
;
2781 /* For the other TEX opcodes there's no projective version
2782 * since the last slot is taken up by LOD info. Do the
2783 * projective divide now.
2785 coord_dst
.writemask
= WRITEMASK_W
;
2786 emit(ir
, TGSI_OPCODE_RCP
, coord_dst
, projector
);
2788 /* In the case where we have to project the coordinates "by hand,"
2789 * the shadow comparator value must also be projected.
2791 st_src_reg tmp_src
= coord
;
2792 if (ir
->shadow_comparitor
) {
2793 /* Slot the shadow value in as the second to last component of the
2796 ir
->shadow_comparitor
->accept(this);
2798 tmp_src
= get_temp(glsl_type::vec4_type
);
2799 st_dst_reg tmp_dst
= st_dst_reg(tmp_src
);
2801 /* Projective division not allowed for array samplers. */
2802 assert(!sampler_type
->sampler_array
);
2804 tmp_dst
.writemask
= WRITEMASK_Z
;
2805 emit(ir
, TGSI_OPCODE_MOV
, tmp_dst
, this->result
);
2807 tmp_dst
.writemask
= WRITEMASK_XY
;
2808 emit(ir
, TGSI_OPCODE_MOV
, tmp_dst
, coord
);
2811 coord_dst
.writemask
= WRITEMASK_XYZ
;
2812 emit(ir
, TGSI_OPCODE_MUL
, coord_dst
, tmp_src
, coord_w
);
2814 coord_dst
.writemask
= WRITEMASK_XYZW
;
2815 coord
.swizzle
= SWIZZLE_XYZW
;
2819 /* If projection is done and the opcode is not TGSI_OPCODE_TXP, then the shadow
2820 * comparator was put in the correct place (and projected) by the code,
2821 * above, that handles by-hand projection.
2823 if (ir
->shadow_comparitor
&& (!ir
->projector
|| opcode
== TGSI_OPCODE_TXP
)) {
2824 /* Slot the shadow value in as the second to last component of the
2827 ir
->shadow_comparitor
->accept(this);
2829 if (is_cube_array
) {
2830 cube_sc
= get_temp(glsl_type::float_type
);
2831 cube_sc_dst
= st_dst_reg(cube_sc
);
2832 cube_sc_dst
.writemask
= WRITEMASK_X
;
2833 emit(ir
, TGSI_OPCODE_MOV
, cube_sc_dst
, this->result
);
2834 cube_sc_dst
.writemask
= WRITEMASK_X
;
2837 if ((sampler_type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_2D
&&
2838 sampler_type
->sampler_array
) ||
2839 sampler_type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
) {
2840 coord_dst
.writemask
= WRITEMASK_W
;
2842 coord_dst
.writemask
= WRITEMASK_Z
;
2845 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
2846 coord_dst
.writemask
= WRITEMASK_XYZW
;
2850 if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXB
||
2851 opcode
== TGSI_OPCODE_TXF
) {
2852 /* TGSI stores LOD or LOD bias in the last channel of the coords. */
2853 coord_dst
.writemask
= WRITEMASK_W
;
2854 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, lod_info
);
2855 coord_dst
.writemask
= WRITEMASK_XYZW
;
2858 if (opcode
== TGSI_OPCODE_TXD
)
2859 inst
= emit(ir
, opcode
, result_dst
, coord
, dx
, dy
);
2860 else if (opcode
== TGSI_OPCODE_TXQ
)
2861 inst
= emit(ir
, opcode
, result_dst
, lod_info
);
2862 else if (opcode
== TGSI_OPCODE_TXF
) {
2863 inst
= emit(ir
, opcode
, result_dst
, coord
);
2864 } else if (opcode
== TGSI_OPCODE_TXL2
|| opcode
== TGSI_OPCODE_TXB2
) {
2865 inst
= emit(ir
, opcode
, result_dst
, coord
, lod_info
);
2866 } else if (opcode
== TGSI_OPCODE_TEX2
) {
2867 inst
= emit(ir
, opcode
, result_dst
, coord
, cube_sc
);
2869 inst
= emit(ir
, opcode
, result_dst
, coord
);
2871 if (ir
->shadow_comparitor
)
2872 inst
->tex_shadow
= GL_TRUE
;
2874 inst
->sampler
= _mesa_get_sampler_uniform_value(ir
->sampler
,
2875 this->shader_program
,
2879 inst
->tex_offset_num_offset
= 1;
2880 inst
->tex_offsets
[0].Index
= offset
.index
;
2881 inst
->tex_offsets
[0].File
= offset
.file
;
2882 inst
->tex_offsets
[0].SwizzleX
= GET_SWZ(offset
.swizzle
, 0);
2883 inst
->tex_offsets
[0].SwizzleY
= GET_SWZ(offset
.swizzle
, 1);
2884 inst
->tex_offsets
[0].SwizzleZ
= GET_SWZ(offset
.swizzle
, 2);
2887 switch (sampler_type
->sampler_dimensionality
) {
2888 case GLSL_SAMPLER_DIM_1D
:
2889 inst
->tex_target
= (sampler_type
->sampler_array
)
2890 ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
2892 case GLSL_SAMPLER_DIM_2D
:
2893 inst
->tex_target
= (sampler_type
->sampler_array
)
2894 ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
2896 case GLSL_SAMPLER_DIM_3D
:
2897 inst
->tex_target
= TEXTURE_3D_INDEX
;
2899 case GLSL_SAMPLER_DIM_CUBE
:
2900 inst
->tex_target
= (sampler_type
->sampler_array
)
2901 ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
2903 case GLSL_SAMPLER_DIM_RECT
:
2904 inst
->tex_target
= TEXTURE_RECT_INDEX
;
2906 case GLSL_SAMPLER_DIM_BUF
:
2907 inst
->tex_target
= TEXTURE_BUFFER_INDEX
;
2909 case GLSL_SAMPLER_DIM_EXTERNAL
:
2910 inst
->tex_target
= TEXTURE_EXTERNAL_INDEX
;
2913 assert(!"Should not get here.");
2916 this->result
= result_src
;
2920 glsl_to_tgsi_visitor::visit(ir_return
*ir
)
2922 if (ir
->get_value()) {
2926 assert(current_function
);
2928 ir
->get_value()->accept(this);
2929 st_src_reg r
= this->result
;
2931 l
= st_dst_reg(current_function
->return_reg
);
2933 for (i
= 0; i
< type_size(current_function
->sig
->return_type
); i
++) {
2934 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
2940 emit(ir
, TGSI_OPCODE_RET
);
2944 glsl_to_tgsi_visitor::visit(ir_discard
*ir
)
2946 if (ir
->condition
) {
2947 ir
->condition
->accept(this);
2948 this->result
.negate
= ~this->result
.negate
;
2949 emit(ir
, TGSI_OPCODE_KIL
, undef_dst
, this->result
);
2951 emit(ir
, TGSI_OPCODE_KILP
);
2956 glsl_to_tgsi_visitor::visit(ir_if
*ir
)
2958 glsl_to_tgsi_instruction
*if_inst
;
2960 ir
->condition
->accept(this);
2961 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
2963 if_inst
= emit(ir
->condition
, TGSI_OPCODE_IF
, undef_dst
, this->result
);
2965 this->instructions
.push_tail(if_inst
);
2967 visit_exec_list(&ir
->then_instructions
, this);
2969 if (!ir
->else_instructions
.is_empty()) {
2970 emit(ir
->condition
, TGSI_OPCODE_ELSE
);
2971 visit_exec_list(&ir
->else_instructions
, this);
2974 if_inst
= emit(ir
->condition
, TGSI_OPCODE_ENDIF
);
2977 glsl_to_tgsi_visitor::glsl_to_tgsi_visitor()
2979 result
.file
= PROGRAM_UNDEFINED
;
2981 next_signature_id
= 1;
2983 current_function
= NULL
;
2984 num_address_regs
= 0;
2986 indirect_addr_temps
= false;
2987 indirect_addr_consts
= false;
2989 native_integers
= false;
2990 mem_ctx
= ralloc_context(NULL
);
2993 shader_program
= NULL
;
2997 glsl_to_tgsi_visitor::~glsl_to_tgsi_visitor()
2999 ralloc_free(mem_ctx
);
3002 extern "C" void free_glsl_to_tgsi_visitor(glsl_to_tgsi_visitor
*v
)
3009 * Count resources used by the given gpu program (number of texture
3013 count_resources(glsl_to_tgsi_visitor
*v
, gl_program
*prog
)
3015 v
->samplers_used
= 0;
3017 foreach_iter(exec_list_iterator
, iter
, v
->instructions
) {
3018 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3020 if (is_tex_instruction(inst
->op
)) {
3021 v
->samplers_used
|= 1 << inst
->sampler
;
3023 if (inst
->tex_shadow
) {
3024 prog
->ShadowSamplers
|= 1 << inst
->sampler
;
3029 prog
->SamplersUsed
= v
->samplers_used
;
3031 if (v
->shader_program
!= NULL
)
3032 _mesa_update_shader_textures_used(v
->shader_program
, prog
);
3036 set_uniform_initializer(struct gl_context
*ctx
, void *mem_ctx
,
3037 struct gl_shader_program
*shader_program
,
3038 const char *name
, const glsl_type
*type
,
3041 if (type
->is_record()) {
3042 ir_constant
*field_constant
;
3044 field_constant
= (ir_constant
*)val
->components
.get_head();
3046 for (unsigned int i
= 0; i
< type
->length
; i
++) {
3047 const glsl_type
*field_type
= type
->fields
.structure
[i
].type
;
3048 const char *field_name
= ralloc_asprintf(mem_ctx
, "%s.%s", name
,
3049 type
->fields
.structure
[i
].name
);
3050 set_uniform_initializer(ctx
, mem_ctx
, shader_program
, field_name
,
3051 field_type
, field_constant
);
3052 field_constant
= (ir_constant
*)field_constant
->next
;
3058 unsigned index
= _mesa_get_uniform_location(ctx
, shader_program
, name
,
3060 if (offset
== GL_INVALID_INDEX
) {
3061 fail_link(shader_program
,
3062 "Couldn't find uniform for initializer %s\n", name
);
3065 int loc
= _mesa_uniform_merge_location_offset(index
, offset
);
3067 for (unsigned int i
= 0; i
< (type
->is_array() ? type
->length
: 1); i
++) {
3068 ir_constant
*element
;
3069 const glsl_type
*element_type
;
3070 if (type
->is_array()) {
3071 element
= val
->array_elements
[i
];
3072 element_type
= type
->fields
.array
;
3075 element_type
= type
;
3080 if (element_type
->base_type
== GLSL_TYPE_BOOL
) {
3081 int *conv
= ralloc_array(mem_ctx
, int, element_type
->components());
3082 for (unsigned int j
= 0; j
< element_type
->components(); j
++) {
3083 conv
[j
] = element
->value
.b
[j
];
3085 values
= (void *)conv
;
3086 element_type
= glsl_type::get_instance(GLSL_TYPE_INT
,
3087 element_type
->vector_elements
,
3090 values
= &element
->value
;
3093 if (element_type
->is_matrix()) {
3094 _mesa_uniform_matrix(ctx
, shader_program
,
3095 element_type
->matrix_columns
,
3096 element_type
->vector_elements
,
3097 loc
, 1, GL_FALSE
, (GLfloat
*)values
);
3099 _mesa_uniform(ctx
, shader_program
, loc
, element_type
->matrix_columns
,
3100 values
, element_type
->gl_type
);
3108 * Returns the mask of channels (bitmask of WRITEMASK_X,Y,Z,W) which
3109 * are read from the given src in this instruction
3112 get_src_arg_mask(st_dst_reg dst
, st_src_reg src
)
3114 int read_mask
= 0, comp
;
3116 /* Now, given the src swizzle and the written channels, find which
3117 * components are actually read
3119 for (comp
= 0; comp
< 4; ++comp
) {
3120 const unsigned coord
= GET_SWZ(src
.swizzle
, comp
);
3122 if (dst
.writemask
& (1 << comp
) && coord
<= SWIZZLE_W
)
3123 read_mask
|= 1 << coord
;
3130 * This pass replaces CMP T0, T1 T2 T0 with MOV T0, T2 when the CMP
3131 * instruction is the first instruction to write to register T0. There are
3132 * several lowering passes done in GLSL IR (e.g. branches and
3133 * relative addressing) that create a large number of conditional assignments
3134 * that ir_to_mesa converts to CMP instructions like the one mentioned above.
3136 * Here is why this conversion is safe:
3137 * CMP T0, T1 T2 T0 can be expanded to:
3143 * If (T1 < 0.0) evaluates to true then our replacement MOV T0, T2 is the same
3144 * as the original program. If (T1 < 0.0) evaluates to false, executing
3145 * MOV T0, T0 will store a garbage value in T0 since T0 is uninitialized.
3146 * Therefore, it doesn't matter that we are replacing MOV T0, T0 with MOV T0, T2
3147 * because any instruction that was going to read from T0 after this was going
3148 * to read a garbage value anyway.
3151 glsl_to_tgsi_visitor::simplify_cmp(void)
3153 unsigned *tempWrites
;
3154 unsigned outputWrites
[MAX_PROGRAM_OUTPUTS
];
3156 tempWrites
= new unsigned[MAX_TEMPS
];
3160 memset(tempWrites
, 0, sizeof(unsigned) * MAX_TEMPS
);
3161 memset(outputWrites
, 0, sizeof(outputWrites
));
3163 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3164 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3165 unsigned prevWriteMask
= 0;
3167 /* Give up if we encounter relative addressing or flow control. */
3168 if (inst
->dst
.reladdr
||
3169 tgsi_get_opcode_info(inst
->op
)->is_branch
||
3170 inst
->op
== TGSI_OPCODE_BGNSUB
||
3171 inst
->op
== TGSI_OPCODE_CONT
||
3172 inst
->op
== TGSI_OPCODE_END
||
3173 inst
->op
== TGSI_OPCODE_ENDSUB
||
3174 inst
->op
== TGSI_OPCODE_RET
) {
3178 if (inst
->dst
.file
== PROGRAM_OUTPUT
) {
3179 assert(inst
->dst
.index
< MAX_PROGRAM_OUTPUTS
);
3180 prevWriteMask
= outputWrites
[inst
->dst
.index
];
3181 outputWrites
[inst
->dst
.index
] |= inst
->dst
.writemask
;
3182 } else if (inst
->dst
.file
== PROGRAM_TEMPORARY
) {
3183 assert(inst
->dst
.index
< MAX_TEMPS
);
3184 prevWriteMask
= tempWrites
[inst
->dst
.index
];
3185 tempWrites
[inst
->dst
.index
] |= inst
->dst
.writemask
;
3188 /* For a CMP to be considered a conditional write, the destination
3189 * register and source register two must be the same. */
3190 if (inst
->op
== TGSI_OPCODE_CMP
3191 && !(inst
->dst
.writemask
& prevWriteMask
)
3192 && inst
->src
[2].file
== inst
->dst
.file
3193 && inst
->src
[2].index
== inst
->dst
.index
3194 && inst
->dst
.writemask
== get_src_arg_mask(inst
->dst
, inst
->src
[2])) {
3196 inst
->op
= TGSI_OPCODE_MOV
;
3197 inst
->src
[0] = inst
->src
[1];
3201 delete [] tempWrites
;
3204 /* Replaces all references to a temporary register index with another index. */
3206 glsl_to_tgsi_visitor::rename_temp_register(int index
, int new_index
)
3208 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3209 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3212 for (j
=0; j
< num_inst_src_regs(inst
->op
); j
++) {
3213 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
&&
3214 inst
->src
[j
].index
== index
) {
3215 inst
->src
[j
].index
= new_index
;
3219 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== index
) {
3220 inst
->dst
.index
= new_index
;
3226 glsl_to_tgsi_visitor::get_first_temp_read(int index
)
3228 int depth
= 0; /* loop depth */
3229 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
3232 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3233 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 return (depth
== 0) ? i
: loop_start
;
3242 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
3245 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
3258 glsl_to_tgsi_visitor::get_first_temp_write(int index
)
3260 int depth
= 0; /* loop depth */
3261 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
3264 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3265 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3267 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== index
) {
3268 return (depth
== 0) ? i
: loop_start
;
3271 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
3274 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
3287 glsl_to_tgsi_visitor::get_last_temp_read(int index
)
3289 int depth
= 0; /* loop depth */
3290 int last
= -1; /* index of last instruction that reads the temporary */
3293 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3294 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3296 for (j
=0; j
< num_inst_src_regs(inst
->op
); j
++) {
3297 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
&&
3298 inst
->src
[j
].index
== index
) {
3299 last
= (depth
== 0) ? i
: -2;
3303 if (inst
->op
== TGSI_OPCODE_BGNLOOP
)
3305 else if (inst
->op
== TGSI_OPCODE_ENDLOOP
)
3306 if (--depth
== 0 && last
== -2)
3318 glsl_to_tgsi_visitor::get_last_temp_write(int index
)
3320 int depth
= 0; /* loop depth */
3321 int last
= -1; /* index of last instruction that writes to the temporary */
3324 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3325 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3327 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== index
)
3328 last
= (depth
== 0) ? i
: -2;
3330 if (inst
->op
== TGSI_OPCODE_BGNLOOP
)
3332 else if (inst
->op
== TGSI_OPCODE_ENDLOOP
)
3333 if (--depth
== 0 && last
== -2)
3345 * On a basic block basis, tracks available PROGRAM_TEMPORARY register
3346 * channels for copy propagation and updates following instructions to
3347 * use the original versions.
3349 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
3350 * will occur. As an example, a TXP production before this pass:
3352 * 0: MOV TEMP[1], INPUT[4].xyyy;
3353 * 1: MOV TEMP[1].w, INPUT[4].wwww;
3354 * 2: TXP TEMP[2], TEMP[1], texture[0], 2D;
3358 * 0: MOV TEMP[1], INPUT[4].xyyy;
3359 * 1: MOV TEMP[1].w, INPUT[4].wwww;
3360 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
3362 * which allows for dead code elimination on TEMP[1]'s writes.
3365 glsl_to_tgsi_visitor::copy_propagate(void)
3367 glsl_to_tgsi_instruction
**acp
= rzalloc_array(mem_ctx
,
3368 glsl_to_tgsi_instruction
*,
3369 this->next_temp
* 4);
3370 int *acp_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
3373 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3374 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3376 assert(inst
->dst
.file
!= PROGRAM_TEMPORARY
3377 || inst
->dst
.index
< this->next_temp
);
3379 /* First, do any copy propagation possible into the src regs. */
3380 for (int r
= 0; r
< 3; r
++) {
3381 glsl_to_tgsi_instruction
*first
= NULL
;
3383 int acp_base
= inst
->src
[r
].index
* 4;
3385 if (inst
->src
[r
].file
!= PROGRAM_TEMPORARY
||
3386 inst
->src
[r
].reladdr
)
3389 /* See if we can find entries in the ACP consisting of MOVs
3390 * from the same src register for all the swizzled channels
3391 * of this src register reference.
3393 for (int i
= 0; i
< 4; i
++) {
3394 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
3395 glsl_to_tgsi_instruction
*copy_chan
= acp
[acp_base
+ src_chan
];
3402 assert(acp_level
[acp_base
+ src_chan
] <= level
);
3407 if (first
->src
[0].file
!= copy_chan
->src
[0].file
||
3408 first
->src
[0].index
!= copy_chan
->src
[0].index
) {
3416 /* We've now validated that we can copy-propagate to
3417 * replace this src register reference. Do it.
3419 inst
->src
[r
].file
= first
->src
[0].file
;
3420 inst
->src
[r
].index
= first
->src
[0].index
;
3423 for (int i
= 0; i
< 4; i
++) {
3424 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
3425 glsl_to_tgsi_instruction
*copy_inst
= acp
[acp_base
+ src_chan
];
3426 swizzle
|= (GET_SWZ(copy_inst
->src
[0].swizzle
, src_chan
) <<
3429 inst
->src
[r
].swizzle
= swizzle
;
3434 case TGSI_OPCODE_BGNLOOP
:
3435 case TGSI_OPCODE_ENDLOOP
:
3436 /* End of a basic block, clear the ACP entirely. */
3437 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
3440 case TGSI_OPCODE_IF
:
3444 case TGSI_OPCODE_ENDIF
:
3445 case TGSI_OPCODE_ELSE
:
3446 /* Clear all channels written inside the block from the ACP, but
3447 * leaving those that were not touched.
3449 for (int r
= 0; r
< this->next_temp
; r
++) {
3450 for (int c
= 0; c
< 4; c
++) {
3451 if (!acp
[4 * r
+ c
])
3454 if (acp_level
[4 * r
+ c
] >= level
)
3455 acp
[4 * r
+ c
] = NULL
;
3458 if (inst
->op
== TGSI_OPCODE_ENDIF
)
3463 /* Continuing the block, clear any written channels from
3466 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.reladdr
) {
3467 /* Any temporary might be written, so no copy propagation
3468 * across this instruction.
3470 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
3471 } else if (inst
->dst
.file
== PROGRAM_OUTPUT
&&
3472 inst
->dst
.reladdr
) {
3473 /* Any output might be written, so no copy propagation
3474 * from outputs across this instruction.
3476 for (int r
= 0; r
< this->next_temp
; r
++) {
3477 for (int c
= 0; c
< 4; c
++) {
3478 if (!acp
[4 * r
+ c
])
3481 if (acp
[4 * r
+ c
]->src
[0].file
== PROGRAM_OUTPUT
)
3482 acp
[4 * r
+ c
] = NULL
;
3485 } else if (inst
->dst
.file
== PROGRAM_TEMPORARY
||
3486 inst
->dst
.file
== PROGRAM_OUTPUT
) {
3487 /* Clear where it's used as dst. */
3488 if (inst
->dst
.file
== PROGRAM_TEMPORARY
) {
3489 for (int c
= 0; c
< 4; c
++) {
3490 if (inst
->dst
.writemask
& (1 << c
)) {
3491 acp
[4 * inst
->dst
.index
+ c
] = NULL
;
3496 /* Clear where it's used as src. */
3497 for (int r
= 0; r
< this->next_temp
; r
++) {
3498 for (int c
= 0; c
< 4; c
++) {
3499 if (!acp
[4 * r
+ c
])
3502 int src_chan
= GET_SWZ(acp
[4 * r
+ c
]->src
[0].swizzle
, c
);
3504 if (acp
[4 * r
+ c
]->src
[0].file
== inst
->dst
.file
&&
3505 acp
[4 * r
+ c
]->src
[0].index
== inst
->dst
.index
&&
3506 inst
->dst
.writemask
& (1 << src_chan
))
3508 acp
[4 * r
+ c
] = NULL
;
3516 /* If this is a copy, add it to the ACP. */
3517 if (inst
->op
== TGSI_OPCODE_MOV
&&
3518 inst
->dst
.file
== PROGRAM_TEMPORARY
&&
3519 !inst
->dst
.reladdr
&&
3521 !inst
->src
[0].reladdr
&&
3522 !inst
->src
[0].negate
) {
3523 for (int i
= 0; i
< 4; i
++) {
3524 if (inst
->dst
.writemask
& (1 << i
)) {
3525 acp
[4 * inst
->dst
.index
+ i
] = inst
;
3526 acp_level
[4 * inst
->dst
.index
+ i
] = level
;
3532 ralloc_free(acp_level
);
3537 * Tracks available PROGRAM_TEMPORARY registers for dead code elimination.
3539 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
3540 * will occur. As an example, a TXP production after copy propagation but
3543 * 0: MOV TEMP[1], INPUT[4].xyyy;
3544 * 1: MOV TEMP[1].w, INPUT[4].wwww;
3545 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
3547 * and after this pass:
3549 * 0: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
3551 * FIXME: assumes that all functions are inlined (no support for BGNSUB/ENDSUB)
3552 * FIXME: doesn't eliminate all dead code inside of loops; it steps around them
3555 glsl_to_tgsi_visitor::eliminate_dead_code(void)
3559 for (i
=0; i
< this->next_temp
; i
++) {
3560 int last_read
= get_last_temp_read(i
);
3563 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3564 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3566 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== i
&&
3579 * On a basic block basis, tracks available PROGRAM_TEMPORARY registers for dead
3580 * code elimination. This is less primitive than eliminate_dead_code(), as it
3581 * is per-channel and can detect consecutive writes without a read between them
3582 * as dead code. However, there is some dead code that can be eliminated by
3583 * eliminate_dead_code() but not this function - for example, this function
3584 * cannot eliminate an instruction writing to a register that is never read and
3585 * is the only instruction writing to that register.
3587 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
3591 glsl_to_tgsi_visitor::eliminate_dead_code_advanced(void)
3593 glsl_to_tgsi_instruction
**writes
= rzalloc_array(mem_ctx
,
3594 glsl_to_tgsi_instruction
*,
3595 this->next_temp
* 4);
3596 int *write_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
3600 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3601 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3603 assert(inst
->dst
.file
!= PROGRAM_TEMPORARY
3604 || inst
->dst
.index
< this->next_temp
);
3607 case TGSI_OPCODE_BGNLOOP
:
3608 case TGSI_OPCODE_ENDLOOP
:
3609 case TGSI_OPCODE_CONT
:
3610 case TGSI_OPCODE_BRK
:
3611 /* End of a basic block, clear the write array entirely.
3613 * This keeps us from killing dead code when the writes are
3614 * on either side of a loop, even when the register isn't touched
3615 * inside the loop. However, glsl_to_tgsi_visitor doesn't seem to emit
3616 * dead code of this type, so it shouldn't make a difference as long as
3617 * the dead code elimination pass in the GLSL compiler does its job.
3619 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
3622 case TGSI_OPCODE_ENDIF
:
3623 case TGSI_OPCODE_ELSE
:
3624 /* Promote the recorded level of all channels written inside the
3625 * preceding if or else block to the level above the if/else block.
3627 for (int r
= 0; r
< this->next_temp
; r
++) {
3628 for (int c
= 0; c
< 4; c
++) {
3629 if (!writes
[4 * r
+ c
])
3632 if (write_level
[4 * r
+ c
] == level
)
3633 write_level
[4 * r
+ c
] = level
-1;
3637 if(inst
->op
== TGSI_OPCODE_ENDIF
)
3642 case TGSI_OPCODE_IF
:
3644 /* fallthrough to default case to mark the condition as read */
3647 /* Continuing the block, clear any channels from the write array that
3648 * are read by this instruction.
3650 for (unsigned i
= 0; i
< Elements(inst
->src
); i
++) {
3651 if (inst
->src
[i
].file
== PROGRAM_TEMPORARY
&& inst
->src
[i
].reladdr
){
3652 /* Any temporary might be read, so no dead code elimination
3653 * across this instruction.
3655 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
3656 } else if (inst
->src
[i
].file
== PROGRAM_TEMPORARY
) {
3657 /* Clear where it's used as src. */
3658 int src_chans
= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 0);
3659 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 1);
3660 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 2);
3661 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 3);
3663 for (int c
= 0; c
< 4; c
++) {
3664 if (src_chans
& (1 << c
)) {
3665 writes
[4 * inst
->src
[i
].index
+ c
] = NULL
;
3673 /* If this instruction writes to a temporary, add it to the write array.
3674 * If there is already an instruction in the write array for one or more
3675 * of the channels, flag that channel write as dead.
3677 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&&
3678 !inst
->dst
.reladdr
&&
3680 for (int c
= 0; c
< 4; c
++) {
3681 if (inst
->dst
.writemask
& (1 << c
)) {
3682 if (writes
[4 * inst
->dst
.index
+ c
]) {
3683 if (write_level
[4 * inst
->dst
.index
+ c
] < level
)
3686 writes
[4 * inst
->dst
.index
+ c
]->dead_mask
|= (1 << c
);
3688 writes
[4 * inst
->dst
.index
+ c
] = inst
;
3689 write_level
[4 * inst
->dst
.index
+ c
] = level
;
3695 /* Anything still in the write array at this point is dead code. */
3696 for (int r
= 0; r
< this->next_temp
; r
++) {
3697 for (int c
= 0; c
< 4; c
++) {
3698 glsl_to_tgsi_instruction
*inst
= writes
[4 * r
+ c
];
3700 inst
->dead_mask
|= (1 << c
);
3704 /* Now actually remove the instructions that are completely dead and update
3705 * the writemask of other instructions with dead channels.
3707 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3708 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3710 if (!inst
->dead_mask
|| !inst
->dst
.writemask
)
3712 else if ((inst
->dst
.writemask
& ~inst
->dead_mask
) == 0) {
3717 inst
->dst
.writemask
&= ~(inst
->dead_mask
);
3720 ralloc_free(write_level
);
3721 ralloc_free(writes
);
3726 /* Merges temporary registers together where possible to reduce the number of
3727 * registers needed to run a program.
3729 * Produces optimal code only after copy propagation and dead code elimination
3732 glsl_to_tgsi_visitor::merge_registers(void)
3734 int *last_reads
= rzalloc_array(mem_ctx
, int, this->next_temp
);
3735 int *first_writes
= rzalloc_array(mem_ctx
, int, this->next_temp
);
3738 /* Read the indices of the last read and first write to each temp register
3739 * into an array so that we don't have to traverse the instruction list as
3741 for (i
=0; i
< this->next_temp
; i
++) {
3742 last_reads
[i
] = get_last_temp_read(i
);
3743 first_writes
[i
] = get_first_temp_write(i
);
3746 /* Start looking for registers with non-overlapping usages that can be
3747 * merged together. */
3748 for (i
=0; i
< this->next_temp
; i
++) {
3749 /* Don't touch unused registers. */
3750 if (last_reads
[i
] < 0 || first_writes
[i
] < 0) continue;
3752 for (j
=0; j
< this->next_temp
; j
++) {
3753 /* Don't touch unused registers. */
3754 if (last_reads
[j
] < 0 || first_writes
[j
] < 0) continue;
3756 /* We can merge the two registers if the first write to j is after or
3757 * in the same instruction as the last read from i. Note that the
3758 * register at index i will always be used earlier or at the same time
3759 * as the register at index j. */
3760 if (first_writes
[i
] <= first_writes
[j
] &&
3761 last_reads
[i
] <= first_writes
[j
])
3763 rename_temp_register(j
, i
); /* Replace all references to j with i.*/
3765 /* Update the first_writes and last_reads arrays with the new
3766 * values for the merged register index, and mark the newly unused
3767 * register index as such. */
3768 last_reads
[i
] = last_reads
[j
];
3769 first_writes
[j
] = -1;
3775 ralloc_free(last_reads
);
3776 ralloc_free(first_writes
);
3779 /* Reassign indices to temporary registers by reusing unused indices created
3780 * by optimization passes. */
3782 glsl_to_tgsi_visitor::renumber_registers(void)
3787 for (i
=0; i
< this->next_temp
; i
++) {
3788 if (get_first_temp_read(i
) < 0) continue;
3790 rename_temp_register(i
, new_index
);
3794 this->next_temp
= new_index
;
3798 * Returns a fragment program which implements the current pixel transfer ops.
3799 * Based on get_pixel_transfer_program in st_atom_pixeltransfer.c.
3802 get_pixel_transfer_visitor(struct st_fragment_program
*fp
,
3803 glsl_to_tgsi_visitor
*original
,
3804 int scale_and_bias
, int pixel_maps
)
3806 glsl_to_tgsi_visitor
*v
= new glsl_to_tgsi_visitor();
3807 struct st_context
*st
= st_context(original
->ctx
);
3808 struct gl_program
*prog
= &fp
->Base
.Base
;
3809 struct gl_program_parameter_list
*params
= _mesa_new_parameter_list();
3810 st_src_reg coord
, src0
;
3812 glsl_to_tgsi_instruction
*inst
;
3814 /* Copy attributes of the glsl_to_tgsi_visitor in the original shader. */
3815 v
->ctx
= original
->ctx
;
3817 v
->shader_program
= NULL
;
3818 v
->glsl_version
= original
->glsl_version
;
3819 v
->native_integers
= original
->native_integers
;
3820 v
->options
= original
->options
;
3821 v
->next_temp
= original
->next_temp
;
3822 v
->num_address_regs
= original
->num_address_regs
;
3823 v
->samplers_used
= prog
->SamplersUsed
= original
->samplers_used
;
3824 v
->indirect_addr_temps
= original
->indirect_addr_temps
;
3825 v
->indirect_addr_consts
= original
->indirect_addr_consts
;
3826 memcpy(&v
->immediates
, &original
->immediates
, sizeof(v
->immediates
));
3827 v
->num_immediates
= original
->num_immediates
;
3830 * Get initial pixel color from the texture.
3831 * TEX colorTemp, fragment.texcoord[0], texture[0], 2D;
3833 coord
= st_src_reg(PROGRAM_INPUT
, FRAG_ATTRIB_TEX0
, glsl_type::vec2_type
);
3834 src0
= v
->get_temp(glsl_type::vec4_type
);
3835 dst0
= st_dst_reg(src0
);
3836 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, dst0
, coord
);
3838 inst
->tex_target
= TEXTURE_2D_INDEX
;
3840 prog
->InputsRead
|= FRAG_BIT_TEX0
;
3841 prog
->SamplersUsed
|= (1 << 0); /* mark sampler 0 as used */
3842 v
->samplers_used
|= (1 << 0);
3844 if (scale_and_bias
) {
3845 static const gl_state_index scale_state
[STATE_LENGTH
] =
3846 { STATE_INTERNAL
, STATE_PT_SCALE
,
3847 (gl_state_index
) 0, (gl_state_index
) 0, (gl_state_index
) 0 };
3848 static const gl_state_index bias_state
[STATE_LENGTH
] =
3849 { STATE_INTERNAL
, STATE_PT_BIAS
,
3850 (gl_state_index
) 0, (gl_state_index
) 0, (gl_state_index
) 0 };
3851 GLint scale_p
, bias_p
;
3852 st_src_reg scale
, bias
;
3854 scale_p
= _mesa_add_state_reference(params
, scale_state
);
3855 bias_p
= _mesa_add_state_reference(params
, bias_state
);
3857 /* MAD colorTemp, colorTemp, scale, bias; */
3858 scale
= st_src_reg(PROGRAM_STATE_VAR
, scale_p
, GLSL_TYPE_FLOAT
);
3859 bias
= st_src_reg(PROGRAM_STATE_VAR
, bias_p
, GLSL_TYPE_FLOAT
);
3860 inst
= v
->emit(NULL
, TGSI_OPCODE_MAD
, dst0
, src0
, scale
, bias
);
3864 st_src_reg temp
= v
->get_temp(glsl_type::vec4_type
);
3865 st_dst_reg temp_dst
= st_dst_reg(temp
);
3867 assert(st
->pixel_xfer
.pixelmap_texture
);
3869 /* With a little effort, we can do four pixel map look-ups with
3870 * two TEX instructions:
3873 /* TEX temp.rg, colorTemp.rgba, texture[1], 2D; */
3874 temp_dst
.writemask
= WRITEMASK_XY
; /* write R,G */
3875 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, temp_dst
, src0
);
3877 inst
->tex_target
= TEXTURE_2D_INDEX
;
3879 /* TEX temp.ba, colorTemp.baba, texture[1], 2D; */
3880 src0
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_Z
, SWIZZLE_W
, SWIZZLE_Z
, SWIZZLE_W
);
3881 temp_dst
.writemask
= WRITEMASK_ZW
; /* write B,A */
3882 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, temp_dst
, src0
);
3884 inst
->tex_target
= TEXTURE_2D_INDEX
;
3886 prog
->SamplersUsed
|= (1 << 1); /* mark sampler 1 as used */
3887 v
->samplers_used
|= (1 << 1);
3889 /* MOV colorTemp, temp; */
3890 inst
= v
->emit(NULL
, TGSI_OPCODE_MOV
, dst0
, temp
);
3893 /* Now copy the instructions from the original glsl_to_tgsi_visitor into the
3895 foreach_iter(exec_list_iterator
, iter
, original
->instructions
) {
3896 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3897 glsl_to_tgsi_instruction
*newinst
;
3898 st_src_reg src_regs
[3];
3900 if (inst
->dst
.file
== PROGRAM_OUTPUT
)
3901 prog
->OutputsWritten
|= BITFIELD64_BIT(inst
->dst
.index
);
3903 for (int i
=0; i
<3; i
++) {
3904 src_regs
[i
] = inst
->src
[i
];
3905 if (src_regs
[i
].file
== PROGRAM_INPUT
&&
3906 src_regs
[i
].index
== FRAG_ATTRIB_COL0
)
3908 src_regs
[i
].file
= PROGRAM_TEMPORARY
;
3909 src_regs
[i
].index
= src0
.index
;
3911 else if (src_regs
[i
].file
== PROGRAM_INPUT
)
3912 prog
->InputsRead
|= BITFIELD64_BIT(src_regs
[i
].index
);
3915 newinst
= v
->emit(NULL
, inst
->op
, inst
->dst
, src_regs
[0], src_regs
[1], src_regs
[2]);
3916 newinst
->tex_target
= inst
->tex_target
;
3919 /* Make modifications to fragment program info. */
3920 prog
->Parameters
= _mesa_combine_parameter_lists(params
,
3921 original
->prog
->Parameters
);
3922 _mesa_free_parameter_list(params
);
3923 count_resources(v
, prog
);
3924 fp
->glsl_to_tgsi
= v
;
3928 * Make fragment program for glBitmap:
3929 * Sample the texture and kill the fragment if the bit is 0.
3930 * This program will be combined with the user's fragment program.
3932 * Based on make_bitmap_fragment_program in st_cb_bitmap.c.
3935 get_bitmap_visitor(struct st_fragment_program
*fp
,
3936 glsl_to_tgsi_visitor
*original
, int samplerIndex
)
3938 glsl_to_tgsi_visitor
*v
= new glsl_to_tgsi_visitor();
3939 struct st_context
*st
= st_context(original
->ctx
);
3940 struct gl_program
*prog
= &fp
->Base
.Base
;
3941 st_src_reg coord
, src0
;
3943 glsl_to_tgsi_instruction
*inst
;
3945 /* Copy attributes of the glsl_to_tgsi_visitor in the original shader. */
3946 v
->ctx
= original
->ctx
;
3948 v
->shader_program
= NULL
;
3949 v
->glsl_version
= original
->glsl_version
;
3950 v
->native_integers
= original
->native_integers
;
3951 v
->options
= original
->options
;
3952 v
->next_temp
= original
->next_temp
;
3953 v
->num_address_regs
= original
->num_address_regs
;
3954 v
->samplers_used
= prog
->SamplersUsed
= original
->samplers_used
;
3955 v
->indirect_addr_temps
= original
->indirect_addr_temps
;
3956 v
->indirect_addr_consts
= original
->indirect_addr_consts
;
3957 memcpy(&v
->immediates
, &original
->immediates
, sizeof(v
->immediates
));
3958 v
->num_immediates
= original
->num_immediates
;
3960 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
3961 coord
= st_src_reg(PROGRAM_INPUT
, FRAG_ATTRIB_TEX0
, glsl_type::vec2_type
);
3962 src0
= v
->get_temp(glsl_type::vec4_type
);
3963 dst0
= st_dst_reg(src0
);
3964 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, dst0
, coord
);
3965 inst
->sampler
= samplerIndex
;
3966 inst
->tex_target
= TEXTURE_2D_INDEX
;
3968 prog
->InputsRead
|= FRAG_BIT_TEX0
;
3969 prog
->SamplersUsed
|= (1 << samplerIndex
); /* mark sampler as used */
3970 v
->samplers_used
|= (1 << samplerIndex
);
3972 /* KIL if -tmp0 < 0 # texel=0 -> keep / texel=0 -> discard */
3973 src0
.negate
= NEGATE_XYZW
;
3974 if (st
->bitmap
.tex_format
== PIPE_FORMAT_L8_UNORM
)
3975 src0
.swizzle
= SWIZZLE_XXXX
;
3976 inst
= v
->emit(NULL
, TGSI_OPCODE_KIL
, undef_dst
, src0
);
3978 /* Now copy the instructions from the original glsl_to_tgsi_visitor into the
3980 foreach_iter(exec_list_iterator
, iter
, original
->instructions
) {
3981 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3982 glsl_to_tgsi_instruction
*newinst
;
3983 st_src_reg src_regs
[3];
3985 if (inst
->dst
.file
== PROGRAM_OUTPUT
)
3986 prog
->OutputsWritten
|= BITFIELD64_BIT(inst
->dst
.index
);
3988 for (int i
=0; i
<3; i
++) {
3989 src_regs
[i
] = inst
->src
[i
];
3990 if (src_regs
[i
].file
== PROGRAM_INPUT
)
3991 prog
->InputsRead
|= BITFIELD64_BIT(src_regs
[i
].index
);
3994 newinst
= v
->emit(NULL
, inst
->op
, inst
->dst
, src_regs
[0], src_regs
[1], src_regs
[2]);
3995 newinst
->tex_target
= inst
->tex_target
;
3998 /* Make modifications to fragment program info. */
3999 prog
->Parameters
= _mesa_clone_parameter_list(original
->prog
->Parameters
);
4000 count_resources(v
, prog
);
4001 fp
->glsl_to_tgsi
= v
;
4004 /* ------------------------- TGSI conversion stuff -------------------------- */
4006 unsigned branch_target
;
4011 * Intermediate state used during shader translation.
4013 struct st_translate
{
4014 struct ureg_program
*ureg
;
4016 struct ureg_dst temps
[MAX_TEMPS
];
4017 struct ureg_src
*constants
;
4018 struct ureg_src
*immediates
;
4019 struct ureg_dst outputs
[PIPE_MAX_SHADER_OUTPUTS
];
4020 struct ureg_src inputs
[PIPE_MAX_SHADER_INPUTS
];
4021 struct ureg_dst address
[1];
4022 struct ureg_src samplers
[PIPE_MAX_SAMPLERS
];
4023 struct ureg_src systemValues
[SYSTEM_VALUE_MAX
];
4025 const GLuint
*inputMapping
;
4026 const GLuint
*outputMapping
;
4028 /* For every instruction that contains a label (eg CALL), keep
4029 * details so that we can go back afterwards and emit the correct
4030 * tgsi instruction number for each label.
4032 struct label
*labels
;
4033 unsigned labels_size
;
4034 unsigned labels_count
;
4036 /* Keep a record of the tgsi instruction number that each mesa
4037 * instruction starts at, will be used to fix up labels after
4042 unsigned insn_count
;
4044 unsigned procType
; /**< TGSI_PROCESSOR_VERTEX/FRAGMENT */
4049 /** Map Mesa's SYSTEM_VALUE_x to TGSI_SEMANTIC_x */
4050 static unsigned mesa_sysval_to_semantic
[SYSTEM_VALUE_MAX
] = {
4052 TGSI_SEMANTIC_VERTEXID
,
4053 TGSI_SEMANTIC_INSTANCEID
4057 * Make note of a branch to a label in the TGSI code.
4058 * After we've emitted all instructions, we'll go over the list
4059 * of labels built here and patch the TGSI code with the actual
4060 * location of each label.
4062 static unsigned *get_label(struct st_translate
*t
, unsigned branch_target
)
4066 if (t
->labels_count
+ 1 >= t
->labels_size
) {
4067 t
->labels_size
= 1 << (util_logbase2(t
->labels_size
) + 1);
4068 t
->labels
= (struct label
*)realloc(t
->labels
,
4069 t
->labels_size
* sizeof(struct label
));
4070 if (t
->labels
== NULL
) {
4071 static unsigned dummy
;
4077 i
= t
->labels_count
++;
4078 t
->labels
[i
].branch_target
= branch_target
;
4079 return &t
->labels
[i
].token
;
4083 * Called prior to emitting the TGSI code for each instruction.
4084 * Allocate additional space for instructions if needed.
4085 * Update the insn[] array so the next glsl_to_tgsi_instruction points to
4086 * the next TGSI instruction.
4088 static void set_insn_start(struct st_translate
*t
, unsigned start
)
4090 if (t
->insn_count
+ 1 >= t
->insn_size
) {
4091 t
->insn_size
= 1 << (util_logbase2(t
->insn_size
) + 1);
4092 t
->insn
= (unsigned *)realloc(t
->insn
, t
->insn_size
* sizeof(t
->insn
[0]));
4093 if (t
->insn
== NULL
) {
4099 t
->insn
[t
->insn_count
++] = start
;
4103 * Map a glsl_to_tgsi constant/immediate to a TGSI immediate.
4105 static struct ureg_src
4106 emit_immediate(struct st_translate
*t
,
4107 gl_constant_value values
[4],
4110 struct ureg_program
*ureg
= t
->ureg
;
4115 return ureg_DECL_immediate(ureg
, &values
[0].f
, size
);
4117 return ureg_DECL_immediate_int(ureg
, &values
[0].i
, size
);
4118 case GL_UNSIGNED_INT
:
4120 return ureg_DECL_immediate_uint(ureg
, &values
[0].u
, size
);
4122 assert(!"should not get here - type must be float, int, uint, or bool");
4123 return ureg_src_undef();
4128 * Map a glsl_to_tgsi dst register to a TGSI ureg_dst register.
4130 static struct ureg_dst
4131 dst_register(struct st_translate
*t
,
4132 gl_register_file file
,
4136 case PROGRAM_UNDEFINED
:
4137 return ureg_dst_undef();
4139 case PROGRAM_TEMPORARY
:
4140 if (ureg_dst_is_undef(t
->temps
[index
]))
4141 t
->temps
[index
] = ureg_DECL_local_temporary(t
->ureg
);
4143 return t
->temps
[index
];
4145 case PROGRAM_OUTPUT
:
4146 if (t
->procType
== TGSI_PROCESSOR_VERTEX
)
4147 assert(index
< VARYING_SLOT_MAX
);
4148 else if (t
->procType
== TGSI_PROCESSOR_FRAGMENT
)
4149 assert(index
< FRAG_RESULT_MAX
);
4151 assert(index
< GEOM_RESULT_MAX
);
4153 assert(t
->outputMapping
[index
] < Elements(t
->outputs
));
4155 return t
->outputs
[t
->outputMapping
[index
]];
4157 case PROGRAM_ADDRESS
:
4158 return t
->address
[index
];
4161 assert(!"unknown dst register file");
4162 return ureg_dst_undef();
4167 * Map a glsl_to_tgsi src register to a TGSI ureg_src register.
4169 static struct ureg_src
4170 src_register(struct st_translate
*t
,
4171 gl_register_file file
,
4172 GLint index
, GLint index2D
)
4175 case PROGRAM_UNDEFINED
:
4176 return ureg_src_undef();
4178 case PROGRAM_TEMPORARY
:
4180 assert(index
< (int) Elements(t
->temps
));
4181 if (ureg_dst_is_undef(t
->temps
[index
]))
4182 t
->temps
[index
] = ureg_DECL_local_temporary(t
->ureg
);
4183 return ureg_src(t
->temps
[index
]);
4185 case PROGRAM_ENV_PARAM
:
4186 case PROGRAM_LOCAL_PARAM
:
4187 case PROGRAM_UNIFORM
:
4189 return t
->constants
[index
];
4190 case PROGRAM_STATE_VAR
:
4191 case PROGRAM_CONSTANT
: /* ie, immediate */
4193 struct ureg_src src
;
4194 src
= ureg_src_register(TGSI_FILE_CONSTANT
, 0);
4196 src
.DimensionIndex
= index2D
;
4198 } else if (index
< 0)
4199 return ureg_DECL_constant(t
->ureg
, 0);
4201 return t
->constants
[index
];
4203 case PROGRAM_IMMEDIATE
:
4204 return t
->immediates
[index
];
4207 assert(t
->inputMapping
[index
] < Elements(t
->inputs
));
4208 return t
->inputs
[t
->inputMapping
[index
]];
4210 case PROGRAM_OUTPUT
:
4211 assert(t
->outputMapping
[index
] < Elements(t
->outputs
));
4212 return ureg_src(t
->outputs
[t
->outputMapping
[index
]]); /* not needed? */
4214 case PROGRAM_ADDRESS
:
4215 return ureg_src(t
->address
[index
]);
4217 case PROGRAM_SYSTEM_VALUE
:
4218 assert(index
< (int) Elements(t
->systemValues
));
4219 return t
->systemValues
[index
];
4222 assert(!"unknown src register file");
4223 return ureg_src_undef();
4228 * Create a TGSI ureg_dst register from an st_dst_reg.
4230 static struct ureg_dst
4231 translate_dst(struct st_translate
*t
,
4232 const st_dst_reg
*dst_reg
,
4233 bool saturate
, bool clamp_color
)
4235 struct ureg_dst dst
= dst_register(t
,
4239 dst
= ureg_writemask(dst
, dst_reg
->writemask
);
4242 dst
= ureg_saturate(dst
);
4243 else if (clamp_color
&& dst_reg
->file
== PROGRAM_OUTPUT
) {
4244 /* Clamp colors for ARB_color_buffer_float. */
4245 switch (t
->procType
) {
4246 case TGSI_PROCESSOR_VERTEX
:
4247 /* XXX if the geometry shader is present, this must be done there
4248 * instead of here. */
4249 if (dst_reg
->index
== VARYING_SLOT_COL0
||
4250 dst_reg
->index
== VARYING_SLOT_COL1
||
4251 dst_reg
->index
== VARYING_SLOT_BFC0
||
4252 dst_reg
->index
== VARYING_SLOT_BFC1
) {
4253 dst
= ureg_saturate(dst
);
4257 case TGSI_PROCESSOR_FRAGMENT
:
4258 if (dst_reg
->index
>= FRAG_RESULT_COLOR
) {
4259 dst
= ureg_saturate(dst
);
4265 if (dst_reg
->reladdr
!= NULL
)
4266 dst
= ureg_dst_indirect(dst
, ureg_src(t
->address
[0]));
4272 * Create a TGSI ureg_src register from an st_src_reg.
4274 static struct ureg_src
4275 translate_src(struct st_translate
*t
, const st_src_reg
*src_reg
)
4277 struct ureg_src src
= src_register(t
, src_reg
->file
, src_reg
->index
, src_reg
->index2D
);
4279 src
= ureg_swizzle(src
,
4280 GET_SWZ(src_reg
->swizzle
, 0) & 0x3,
4281 GET_SWZ(src_reg
->swizzle
, 1) & 0x3,
4282 GET_SWZ(src_reg
->swizzle
, 2) & 0x3,
4283 GET_SWZ(src_reg
->swizzle
, 3) & 0x3);
4285 if ((src_reg
->negate
& 0xf) == NEGATE_XYZW
)
4286 src
= ureg_negate(src
);
4288 if (src_reg
->reladdr
!= NULL
) {
4289 /* Normally ureg_src_indirect() would be used here, but a stupid compiler
4290 * bug in g++ makes ureg_src_indirect (an inline C function) erroneously
4291 * set the bit for src.Negate. So we have to do the operation manually
4292 * here to work around the compiler's problems. */
4293 /*src = ureg_src_indirect(src, ureg_src(t->address[0]));*/
4294 struct ureg_src addr
= ureg_src(t
->address
[0]);
4296 src
.IndirectFile
= addr
.File
;
4297 src
.IndirectIndex
= addr
.Index
;
4298 src
.IndirectSwizzle
= addr
.SwizzleX
;
4300 if (src_reg
->file
!= PROGRAM_INPUT
&&
4301 src_reg
->file
!= PROGRAM_OUTPUT
) {
4302 /* If src_reg->index was negative, it was set to zero in
4303 * src_register(). Reassign it now. But don't do this
4304 * for input/output regs since they get remapped while
4305 * const buffers don't.
4307 src
.Index
= src_reg
->index
;
4314 static struct tgsi_texture_offset
4315 translate_tex_offset(struct st_translate
*t
,
4316 const struct tgsi_texture_offset
*in_offset
)
4318 struct tgsi_texture_offset offset
;
4319 struct ureg_src imm_src
;
4321 assert(in_offset
->File
== PROGRAM_IMMEDIATE
);
4322 imm_src
= t
->immediates
[in_offset
->Index
];
4324 offset
.File
= imm_src
.File
;
4325 offset
.Index
= imm_src
.Index
;
4326 offset
.SwizzleX
= imm_src
.SwizzleX
;
4327 offset
.SwizzleY
= imm_src
.SwizzleY
;
4328 offset
.SwizzleZ
= imm_src
.SwizzleZ
;
4329 offset
.File
= TGSI_FILE_IMMEDIATE
;
4336 compile_tgsi_instruction(struct st_translate
*t
,
4337 const glsl_to_tgsi_instruction
*inst
,
4338 bool clamp_dst_color_output
)
4340 struct ureg_program
*ureg
= t
->ureg
;
4342 struct ureg_dst dst
[1];
4343 struct ureg_src src
[4];
4344 struct tgsi_texture_offset texoffsets
[MAX_GLSL_TEXTURE_OFFSET
];
4348 unsigned tex_target
;
4350 num_dst
= num_inst_dst_regs(inst
->op
);
4351 num_src
= num_inst_src_regs(inst
->op
);
4354 dst
[0] = translate_dst(t
,
4357 clamp_dst_color_output
);
4359 for (i
= 0; i
< num_src
; i
++)
4360 src
[i
] = translate_src(t
, &inst
->src
[i
]);
4363 case TGSI_OPCODE_BGNLOOP
:
4364 case TGSI_OPCODE_CAL
:
4365 case TGSI_OPCODE_ELSE
:
4366 case TGSI_OPCODE_ENDLOOP
:
4367 case TGSI_OPCODE_IF
:
4368 assert(num_dst
== 0);
4369 ureg_label_insn(ureg
,
4373 inst
->op
== TGSI_OPCODE_CAL
? inst
->function
->sig_id
: 0));
4376 case TGSI_OPCODE_TEX
:
4377 case TGSI_OPCODE_TXB
:
4378 case TGSI_OPCODE_TXD
:
4379 case TGSI_OPCODE_TXL
:
4380 case TGSI_OPCODE_TXP
:
4381 case TGSI_OPCODE_TXQ
:
4382 case TGSI_OPCODE_TXF
:
4383 case TGSI_OPCODE_TEX2
:
4384 case TGSI_OPCODE_TXB2
:
4385 case TGSI_OPCODE_TXL2
:
4386 src
[num_src
++] = t
->samplers
[inst
->sampler
];
4387 for (i
= 0; i
< inst
->tex_offset_num_offset
; i
++) {
4388 texoffsets
[i
] = translate_tex_offset(t
, &inst
->tex_offsets
[i
]);
4390 tex_target
= st_translate_texture_target(inst
->tex_target
, inst
->tex_shadow
);
4396 texoffsets
, inst
->tex_offset_num_offset
,
4400 case TGSI_OPCODE_SCS
:
4401 dst
[0] = ureg_writemask(dst
[0], TGSI_WRITEMASK_XY
);
4402 ureg_insn(ureg
, inst
->op
, dst
, num_dst
, src
, num_src
);
4415 * Emit the TGSI instructions for inverting and adjusting WPOS.
4416 * This code is unavoidable because it also depends on whether
4417 * a FBO is bound (STATE_FB_WPOS_Y_TRANSFORM).
4420 emit_wpos_adjustment( struct st_translate
*t
,
4421 const struct gl_program
*program
,
4423 GLfloat adjX
, GLfloat adjY
[2])
4425 struct ureg_program
*ureg
= t
->ureg
;
4427 /* Fragment program uses fragment position input.
4428 * Need to replace instances of INPUT[WPOS] with temp T
4429 * where T = INPUT[WPOS] by y is inverted.
4431 static const gl_state_index wposTransformState
[STATE_LENGTH
]
4432 = { STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
,
4433 (gl_state_index
)0, (gl_state_index
)0, (gl_state_index
)0 };
4435 /* XXX: note we are modifying the incoming shader here! Need to
4436 * do this before emitting the constant decls below, or this
4439 unsigned wposTransConst
= _mesa_add_state_reference(program
->Parameters
,
4440 wposTransformState
);
4442 struct ureg_src wpostrans
= ureg_DECL_constant( ureg
, wposTransConst
);
4443 struct ureg_dst wpos_temp
= ureg_DECL_temporary( ureg
);
4444 struct ureg_src wpos_input
= t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_WPOS
]];
4446 /* First, apply the coordinate shift: */
4447 if (adjX
|| adjY
[0] || adjY
[1]) {
4448 if (adjY
[0] != adjY
[1]) {
4449 /* Adjust the y coordinate by adjY[1] or adjY[0] respectively
4450 * depending on whether inversion is actually going to be applied
4451 * or not, which is determined by testing against the inversion
4452 * state variable used below, which will be either +1 or -1.
4454 struct ureg_dst adj_temp
= ureg_DECL_local_temporary(ureg
);
4456 ureg_CMP(ureg
, adj_temp
,
4457 ureg_scalar(wpostrans
, invert
? 2 : 0),
4458 ureg_imm4f(ureg
, adjX
, adjY
[0], 0.0f
, 0.0f
),
4459 ureg_imm4f(ureg
, adjX
, adjY
[1], 0.0f
, 0.0f
));
4460 ureg_ADD(ureg
, wpos_temp
, wpos_input
, ureg_src(adj_temp
));
4462 ureg_ADD(ureg
, wpos_temp
, wpos_input
,
4463 ureg_imm4f(ureg
, adjX
, adjY
[0], 0.0f
, 0.0f
));
4465 wpos_input
= ureg_src(wpos_temp
);
4467 /* MOV wpos_temp, input[wpos]
4469 ureg_MOV( ureg
, wpos_temp
, wpos_input
);
4472 /* Now the conditional y flip: STATE_FB_WPOS_Y_TRANSFORM.xy/zw will be
4473 * inversion/identity, or the other way around if we're drawing to an FBO.
4476 /* MAD wpos_temp.y, wpos_input, wpostrans.xxxx, wpostrans.yyyy
4479 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
4481 ureg_scalar(wpostrans
, 0),
4482 ureg_scalar(wpostrans
, 1));
4484 /* MAD wpos_temp.y, wpos_input, wpostrans.zzzz, wpostrans.wwww
4487 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
4489 ureg_scalar(wpostrans
, 2),
4490 ureg_scalar(wpostrans
, 3));
4493 /* Use wpos_temp as position input from here on:
4495 t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_WPOS
]] = ureg_src(wpos_temp
);
4500 * Emit fragment position/ooordinate code.
4503 emit_wpos(struct st_context
*st
,
4504 struct st_translate
*t
,
4505 const struct gl_program
*program
,
4506 struct ureg_program
*ureg
)
4508 const struct gl_fragment_program
*fp
=
4509 (const struct gl_fragment_program
*) program
;
4510 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
4511 GLfloat adjX
= 0.0f
;
4512 GLfloat adjY
[2] = { 0.0f
, 0.0f
};
4513 boolean invert
= FALSE
;
4515 /* Query the pixel center conventions supported by the pipe driver and set
4516 * adjX, adjY to help out if it cannot handle the requested one internally.
4518 * The bias of the y-coordinate depends on whether y-inversion takes place
4519 * (adjY[1]) or not (adjY[0]), which is in turn dependent on whether we are
4520 * drawing to an FBO (causes additional inversion), and whether the the pipe
4521 * driver origin and the requested origin differ (the latter condition is
4522 * stored in the 'invert' variable).
4524 * For height = 100 (i = integer, h = half-integer, l = lower, u = upper):
4526 * center shift only:
4531 * l,i -> u,i: ( 0.0 + 1.0) * -1 + 100 = 99
4532 * l,h -> u,h: ( 0.5 + 0.0) * -1 + 100 = 99.5
4533 * u,i -> l,i: (99.0 + 1.0) * -1 + 100 = 0
4534 * u,h -> l,h: (99.5 + 0.0) * -1 + 100 = 0.5
4536 * inversion and center shift:
4537 * l,i -> u,h: ( 0.0 + 0.5) * -1 + 100 = 99.5
4538 * l,h -> u,i: ( 0.5 + 0.5) * -1 + 100 = 99
4539 * u,i -> l,h: (99.0 + 0.5) * -1 + 100 = 0.5
4540 * u,h -> l,i: (99.5 + 0.5) * -1 + 100 = 0
4542 if (fp
->OriginUpperLeft
) {
4543 /* Fragment shader wants origin in upper-left */
4544 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
)) {
4545 /* the driver supports upper-left origin */
4547 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
)) {
4548 /* the driver supports lower-left origin, need to invert Y */
4549 ureg_property_fs_coord_origin(ureg
, TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
4556 /* Fragment shader wants origin in lower-left */
4557 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
))
4558 /* the driver supports lower-left origin */
4559 ureg_property_fs_coord_origin(ureg
, TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
4560 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
))
4561 /* the driver supports upper-left origin, need to invert Y */
4567 if (fp
->PixelCenterInteger
) {
4568 /* Fragment shader wants pixel center integer */
4569 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)) {
4570 /* the driver supports pixel center integer */
4572 ureg_property_fs_coord_pixel_center(ureg
, TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
4574 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
)) {
4575 /* the driver supports pixel center half integer, need to bias X,Y */
4584 /* Fragment shader wants pixel center half integer */
4585 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
)) {
4586 /* the driver supports pixel center half integer */
4588 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)) {
4589 /* the driver supports pixel center integer, need to bias X,Y */
4590 adjX
= adjY
[0] = adjY
[1] = 0.5f
;
4591 ureg_property_fs_coord_pixel_center(ureg
, TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
4597 /* we invert after adjustment so that we avoid the MOV to temporary,
4598 * and reuse the adjustment ADD instead */
4599 emit_wpos_adjustment(t
, program
, invert
, adjX
, adjY
);
4603 * OpenGL's fragment gl_FrontFace input is 1 for front-facing, 0 for back.
4604 * TGSI uses +1 for front, -1 for back.
4605 * This function converts the TGSI value to the GL value. Simply clamping/
4606 * saturating the value to [0,1] does the job.
4609 emit_face_var(struct st_translate
*t
)
4611 struct ureg_program
*ureg
= t
->ureg
;
4612 struct ureg_dst face_temp
= ureg_DECL_temporary(ureg
);
4613 struct ureg_src face_input
= t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_FACE
]];
4615 /* MOV_SAT face_temp, input[face] */
4616 face_temp
= ureg_saturate(face_temp
);
4617 ureg_MOV(ureg
, face_temp
, face_input
);
4619 /* Use face_temp as face input from here on: */
4620 t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_FACE
]] = ureg_src(face_temp
);
4624 emit_edgeflags(struct st_translate
*t
)
4626 struct ureg_program
*ureg
= t
->ureg
;
4627 struct ureg_dst edge_dst
= t
->outputs
[t
->outputMapping
[VARYING_SLOT_EDGE
]];
4628 struct ureg_src edge_src
= t
->inputs
[t
->inputMapping
[VERT_ATTRIB_EDGEFLAG
]];
4630 ureg_MOV(ureg
, edge_dst
, edge_src
);
4634 * Translate intermediate IR (glsl_to_tgsi_instruction) to TGSI format.
4635 * \param program the program to translate
4636 * \param numInputs number of input registers used
4637 * \param inputMapping maps Mesa fragment program inputs to TGSI generic
4639 * \param inputSemanticName the TGSI_SEMANTIC flag for each input
4640 * \param inputSemanticIndex the semantic index (ex: which texcoord) for
4642 * \param interpMode the TGSI_INTERPOLATE_LINEAR/PERSP mode for each input
4643 * \param numOutputs number of output registers used
4644 * \param outputMapping maps Mesa fragment program outputs to TGSI
4646 * \param outputSemanticName the TGSI_SEMANTIC flag for each output
4647 * \param outputSemanticIndex the semantic index (ex: which texcoord) for
4650 * \return PIPE_OK or PIPE_ERROR_OUT_OF_MEMORY
4652 extern "C" enum pipe_error
4653 st_translate_program(
4654 struct gl_context
*ctx
,
4656 struct ureg_program
*ureg
,
4657 glsl_to_tgsi_visitor
*program
,
4658 const struct gl_program
*proginfo
,
4660 const GLuint inputMapping
[],
4661 const ubyte inputSemanticName
[],
4662 const ubyte inputSemanticIndex
[],
4663 const GLuint interpMode
[],
4664 const GLboolean is_centroid
[],
4666 const GLuint outputMapping
[],
4667 const ubyte outputSemanticName
[],
4668 const ubyte outputSemanticIndex
[],
4669 boolean passthrough_edgeflags
,
4670 boolean clamp_color
)
4672 struct st_translate
*t
;
4674 enum pipe_error ret
= PIPE_OK
;
4676 assert(numInputs
<= Elements(t
->inputs
));
4677 assert(numOutputs
<= Elements(t
->outputs
));
4679 t
= CALLOC_STRUCT(st_translate
);
4681 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
4685 memset(t
, 0, sizeof *t
);
4687 t
->procType
= procType
;
4688 t
->inputMapping
= inputMapping
;
4689 t
->outputMapping
= outputMapping
;
4692 if (program
->shader_program
) {
4693 for (i
= 0; i
< program
->shader_program
->NumUserUniformStorage
; i
++) {
4694 struct gl_uniform_storage
*const storage
=
4695 &program
->shader_program
->UniformStorage
[i
];
4697 _mesa_uniform_detach_all_driver_storage(storage
);
4702 * Declare input attributes.
4704 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
4705 for (i
= 0; i
< numInputs
; i
++) {
4706 t
->inputs
[i
] = ureg_DECL_fs_input_cyl_centroid(ureg
,
4707 inputSemanticName
[i
],
4708 inputSemanticIndex
[i
],
4713 if (proginfo
->InputsRead
& FRAG_BIT_WPOS
) {
4714 /* Must do this after setting up t->inputs, and before
4715 * emitting constant references, below:
4717 emit_wpos(st_context(ctx
), t
, proginfo
, ureg
);
4720 if (proginfo
->InputsRead
& FRAG_BIT_FACE
)
4724 * Declare output attributes.
4726 for (i
= 0; i
< numOutputs
; i
++) {
4727 switch (outputSemanticName
[i
]) {
4728 case TGSI_SEMANTIC_POSITION
:
4729 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4730 TGSI_SEMANTIC_POSITION
, /* Z/Depth */
4731 outputSemanticIndex
[i
]);
4732 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_Z
);
4734 case TGSI_SEMANTIC_STENCIL
:
4735 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4736 TGSI_SEMANTIC_STENCIL
, /* Stencil */
4737 outputSemanticIndex
[i
]);
4738 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_Y
);
4740 case TGSI_SEMANTIC_COLOR
:
4741 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4742 TGSI_SEMANTIC_COLOR
,
4743 outputSemanticIndex
[i
]);
4746 assert(!"fragment shader outputs must be POSITION/STENCIL/COLOR");
4747 ret
= PIPE_ERROR_BAD_INPUT
;
4752 else if (procType
== TGSI_PROCESSOR_GEOMETRY
) {
4753 for (i
= 0; i
< numInputs
; i
++) {
4754 t
->inputs
[i
] = ureg_DECL_gs_input(ureg
,
4756 inputSemanticName
[i
],
4757 inputSemanticIndex
[i
]);
4760 for (i
= 0; i
< numOutputs
; i
++) {
4761 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4762 outputSemanticName
[i
],
4763 outputSemanticIndex
[i
]);
4767 assert(procType
== TGSI_PROCESSOR_VERTEX
);
4769 for (i
= 0; i
< numInputs
; i
++) {
4770 t
->inputs
[i
] = ureg_DECL_vs_input(ureg
, i
);
4773 for (i
= 0; i
< numOutputs
; i
++) {
4774 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4775 outputSemanticName
[i
],
4776 outputSemanticIndex
[i
]);
4778 if (passthrough_edgeflags
)
4782 /* Declare address register.
4784 if (program
->num_address_regs
> 0) {
4785 assert(program
->num_address_regs
== 1);
4786 t
->address
[0] = ureg_DECL_address(ureg
);
4789 /* Declare misc input registers
4792 GLbitfield sysInputs
= proginfo
->SystemValuesRead
;
4793 unsigned numSys
= 0;
4794 for (i
= 0; sysInputs
; i
++) {
4795 if (sysInputs
& (1 << i
)) {
4796 unsigned semName
= mesa_sysval_to_semantic
[i
];
4797 t
->systemValues
[i
] = ureg_DECL_system_value(ureg
, numSys
, semName
, 0);
4798 if (semName
== TGSI_SEMANTIC_INSTANCEID
||
4799 semName
== TGSI_SEMANTIC_VERTEXID
) {
4800 /* From Gallium perspective, these system values are always
4801 * integer, and require native integer support. However, if
4802 * native integer is supported on the vertex stage but not the
4803 * pixel stage (e.g, i915g + draw), Mesa will generate IR that
4804 * assumes these system values are floats. To resolve the
4805 * inconsistency, we insert a U2F.
4807 struct st_context
*st
= st_context(ctx
);
4808 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
4809 assert(procType
== TGSI_PROCESSOR_VERTEX
);
4810 assert(pscreen
->get_shader_param(pscreen
, PIPE_SHADER_VERTEX
, PIPE_SHADER_CAP_INTEGERS
));
4811 if (!ctx
->Const
.NativeIntegers
) {
4812 struct ureg_dst temp
= ureg_DECL_local_temporary(t
->ureg
);
4813 ureg_U2F( t
->ureg
, ureg_writemask(temp
, TGSI_WRITEMASK_X
), t
->systemValues
[i
]);
4814 t
->systemValues
[i
] = ureg_scalar(ureg_src(temp
), 0);
4818 sysInputs
&= ~(1 << i
);
4823 if (program
->indirect_addr_temps
) {
4824 /* If temps are accessed with indirect addressing, declare temporaries
4825 * in sequential order. Else, we declare them on demand elsewhere.
4826 * (Note: the number of temporaries is equal to program->next_temp)
4828 for (i
= 0; i
< (unsigned)program
->next_temp
; i
++) {
4829 /* XXX use TGSI_FILE_TEMPORARY_ARRAY when it's supported by ureg */
4830 t
->temps
[i
] = ureg_DECL_local_temporary(t
->ureg
);
4834 /* Emit constants and uniforms. TGSI uses a single index space for these,
4835 * so we put all the translated regs in t->constants.
4837 if (proginfo
->Parameters
) {
4838 t
->constants
= (struct ureg_src
*)
4839 calloc(proginfo
->Parameters
->NumParameters
, sizeof(t
->constants
[0]));
4840 if (t
->constants
== NULL
) {
4841 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
4845 for (i
= 0; i
< proginfo
->Parameters
->NumParameters
; i
++) {
4846 switch (proginfo
->Parameters
->Parameters
[i
].Type
) {
4847 case PROGRAM_ENV_PARAM
:
4848 case PROGRAM_LOCAL_PARAM
:
4849 case PROGRAM_STATE_VAR
:
4850 case PROGRAM_UNIFORM
:
4851 t
->constants
[i
] = ureg_DECL_constant(ureg
, i
);
4854 /* Emit immediates for PROGRAM_CONSTANT only when there's no indirect
4855 * addressing of the const buffer.
4856 * FIXME: Be smarter and recognize param arrays:
4857 * indirect addressing is only valid within the referenced
4860 case PROGRAM_CONSTANT
:
4861 if (program
->indirect_addr_consts
)
4862 t
->constants
[i
] = ureg_DECL_constant(ureg
, i
);
4864 t
->constants
[i
] = emit_immediate(t
,
4865 proginfo
->Parameters
->ParameterValues
[i
],
4866 proginfo
->Parameters
->Parameters
[i
].DataType
,
4875 if (program
->shader_program
) {
4876 unsigned num_ubos
= program
->shader_program
->NumUniformBlocks
;
4878 for (i
= 0; i
< num_ubos
; i
++) {
4879 ureg_DECL_constant2D(t
->ureg
, 0, program
->shader_program
->UniformBlocks
[i
].UniformBufferSize
/ 4, i
+ 1);
4883 /* Emit immediate values.
4885 t
->immediates
= (struct ureg_src
*)
4886 calloc(program
->num_immediates
, sizeof(struct ureg_src
));
4887 if (t
->immediates
== NULL
) {
4888 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
4892 foreach_iter(exec_list_iterator
, iter
, program
->immediates
) {
4893 immediate_storage
*imm
= (immediate_storage
*)iter
.get();
4894 assert(i
< program
->num_immediates
);
4895 t
->immediates
[i
++] = emit_immediate(t
, imm
->values
, imm
->type
, imm
->size
);
4897 assert(i
== program
->num_immediates
);
4899 /* texture samplers */
4900 for (i
= 0; i
< ctx
->Const
.MaxTextureImageUnits
; i
++) {
4901 if (program
->samplers_used
& (1 << i
)) {
4902 t
->samplers
[i
] = ureg_DECL_sampler(ureg
, i
);
4906 /* Emit each instruction in turn:
4908 foreach_iter(exec_list_iterator
, iter
, program
->instructions
) {
4909 set_insn_start(t
, ureg_get_instruction_number(ureg
));
4910 compile_tgsi_instruction(t
, (glsl_to_tgsi_instruction
*)iter
.get(),
4914 /* Fix up all emitted labels:
4916 for (i
= 0; i
< t
->labels_count
; i
++) {
4917 ureg_fixup_label(ureg
, t
->labels
[i
].token
,
4918 t
->insn
[t
->labels
[i
].branch_target
]);
4921 if (program
->shader_program
) {
4922 /* This has to be done last. Any operation the can cause
4923 * prog->ParameterValues to get reallocated (e.g., anything that adds a
4924 * program constant) has to happen before creating this linkage.
4926 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
4927 if (program
->shader_program
->_LinkedShaders
[i
] == NULL
)
4930 _mesa_associate_uniform_storage(ctx
, program
->shader_program
,
4931 program
->shader_program
->_LinkedShaders
[i
]->Program
->Parameters
);
4940 free(t
->immediates
);
4943 debug_printf("%s: translate error flag set\n", __FUNCTION__
);
4951 /* ----------------------------- End TGSI code ------------------------------ */
4954 * Convert a shader's GLSL IR into a Mesa gl_program, although without
4955 * generating Mesa IR.
4957 static struct gl_program
*
4958 get_mesa_program(struct gl_context
*ctx
,
4959 struct gl_shader_program
*shader_program
,
4960 struct gl_shader
*shader
)
4962 glsl_to_tgsi_visitor
* v
;
4963 struct gl_program
*prog
;
4965 const char *target_string
;
4967 struct gl_shader_compiler_options
*options
=
4968 &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(shader
->Type
)];
4969 struct pipe_screen
*pscreen
= ctx
->st
->pipe
->screen
;
4972 switch (shader
->Type
) {
4973 case GL_VERTEX_SHADER
:
4974 target
= GL_VERTEX_PROGRAM_ARB
;
4975 ptarget
= PIPE_SHADER_VERTEX
;
4976 target_string
= "vertex";
4978 case GL_FRAGMENT_SHADER
:
4979 target
= GL_FRAGMENT_PROGRAM_ARB
;
4980 ptarget
= PIPE_SHADER_FRAGMENT
;
4981 target_string
= "fragment";
4983 case GL_GEOMETRY_SHADER
:
4984 target
= GL_GEOMETRY_PROGRAM_NV
;
4985 ptarget
= PIPE_SHADER_GEOMETRY
;
4986 target_string
= "geometry";
4989 assert(!"should not be reached");
4993 validate_ir_tree(shader
->ir
);
4995 prog
= ctx
->Driver
.NewProgram(ctx
, target
, shader_program
->Name
);
4998 prog
->Parameters
= _mesa_new_parameter_list();
4999 v
= new glsl_to_tgsi_visitor();
5002 v
->shader_program
= shader_program
;
5003 v
->options
= options
;
5004 v
->glsl_version
= ctx
->Const
.GLSLVersion
;
5005 v
->native_integers
= ctx
->Const
.NativeIntegers
;
5007 v
->have_sqrt
= pscreen
->get_shader_param(pscreen
, ptarget
,
5008 PIPE_SHADER_CAP_TGSI_SQRT_SUPPORTED
);
5010 _mesa_generate_parameters_list_for_uniforms(shader_program
, shader
,
5013 /* Remove reads from output registers. */
5014 lower_output_reads(shader
->ir
);
5016 /* Emit intermediate IR for main(). */
5017 visit_exec_list(shader
->ir
, v
);
5019 /* Now emit bodies for any functions that were used. */
5021 progress
= GL_FALSE
;
5023 foreach_iter(exec_list_iterator
, iter
, v
->function_signatures
) {
5024 function_entry
*entry
= (function_entry
*)iter
.get();
5026 if (!entry
->bgn_inst
) {
5027 v
->current_function
= entry
;
5029 entry
->bgn_inst
= v
->emit(NULL
, TGSI_OPCODE_BGNSUB
);
5030 entry
->bgn_inst
->function
= entry
;
5032 visit_exec_list(&entry
->sig
->body
, v
);
5034 glsl_to_tgsi_instruction
*last
;
5035 last
= (glsl_to_tgsi_instruction
*)v
->instructions
.get_tail();
5036 if (last
->op
!= TGSI_OPCODE_RET
)
5037 v
->emit(NULL
, TGSI_OPCODE_RET
);
5039 glsl_to_tgsi_instruction
*end
;
5040 end
= v
->emit(NULL
, TGSI_OPCODE_ENDSUB
);
5041 end
->function
= entry
;
5049 /* Print out some information (for debugging purposes) used by the
5050 * optimization passes. */
5051 for (i
=0; i
< v
->next_temp
; i
++) {
5052 int fr
= v
->get_first_temp_read(i
);
5053 int fw
= v
->get_first_temp_write(i
);
5054 int lr
= v
->get_last_temp_read(i
);
5055 int lw
= v
->get_last_temp_write(i
);
5057 printf("Temp %d: FR=%3d FW=%3d LR=%3d LW=%3d\n", i
, fr
, fw
, lr
, lw
);
5062 /* Perform optimizations on the instructions in the glsl_to_tgsi_visitor. */
5064 v
->copy_propagate();
5065 while (v
->eliminate_dead_code_advanced());
5067 /* FIXME: These passes to optimize temporary registers don't work when there
5068 * is indirect addressing of the temporary register space. We need proper
5069 * array support so that we don't have to give up these passes in every
5070 * shader that uses arrays.
5072 if (!v
->indirect_addr_temps
) {
5073 v
->eliminate_dead_code();
5074 v
->merge_registers();
5075 v
->renumber_registers();
5078 /* Write the END instruction. */
5079 v
->emit(NULL
, TGSI_OPCODE_END
);
5081 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
5083 printf("GLSL IR for linked %s program %d:\n", target_string
,
5084 shader_program
->Name
);
5085 _mesa_print_ir(shader
->ir
, NULL
);
5091 prog
->Instructions
= NULL
;
5092 prog
->NumInstructions
= 0;
5094 do_set_program_inouts(shader
->ir
, prog
, shader
->Type
== GL_FRAGMENT_SHADER
);
5095 count_resources(v
, prog
);
5097 _mesa_reference_program(ctx
, &shader
->Program
, prog
);
5099 /* This has to be done last. Any operation the can cause
5100 * prog->ParameterValues to get reallocated (e.g., anything that adds a
5101 * program constant) has to happen before creating this linkage.
5103 _mesa_associate_uniform_storage(ctx
, shader_program
, prog
->Parameters
);
5104 if (!shader_program
->LinkStatus
) {
5108 struct st_vertex_program
*stvp
;
5109 struct st_fragment_program
*stfp
;
5110 struct st_geometry_program
*stgp
;
5112 switch (shader
->Type
) {
5113 case GL_VERTEX_SHADER
:
5114 stvp
= (struct st_vertex_program
*)prog
;
5115 stvp
->glsl_to_tgsi
= v
;
5117 case GL_FRAGMENT_SHADER
:
5118 stfp
= (struct st_fragment_program
*)prog
;
5119 stfp
->glsl_to_tgsi
= v
;
5121 case GL_GEOMETRY_SHADER
:
5122 stgp
= (struct st_geometry_program
*)prog
;
5123 stgp
->glsl_to_tgsi
= v
;
5126 assert(!"should not be reached");
5136 st_new_shader(struct gl_context
*ctx
, GLuint name
, GLuint type
)
5138 struct gl_shader
*shader
;
5139 assert(type
== GL_FRAGMENT_SHADER
|| type
== GL_VERTEX_SHADER
||
5140 type
== GL_GEOMETRY_SHADER_ARB
);
5141 shader
= rzalloc(NULL
, struct gl_shader
);
5143 shader
->Type
= type
;
5144 shader
->Name
= name
;
5145 _mesa_init_shader(ctx
, shader
);
5150 struct gl_shader_program
*
5151 st_new_shader_program(struct gl_context
*ctx
, GLuint name
)
5153 struct gl_shader_program
*shProg
;
5154 shProg
= rzalloc(NULL
, struct gl_shader_program
);
5156 shProg
->Name
= name
;
5157 _mesa_init_shader_program(ctx
, shProg
);
5164 * Called via ctx->Driver.LinkShader()
5165 * This actually involves converting GLSL IR into an intermediate TGSI-like IR
5166 * with code lowering and other optimizations.
5169 st_link_shader(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
5171 assert(prog
->LinkStatus
);
5173 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
5174 if (prog
->_LinkedShaders
[i
] == NULL
)
5178 exec_list
*ir
= prog
->_LinkedShaders
[i
]->ir
;
5179 const struct gl_shader_compiler_options
*options
=
5180 &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(prog
->_LinkedShaders
[i
]->Type
)];
5182 /* If there are forms of indirect addressing that the driver
5183 * cannot handle, perform the lowering pass.
5185 if (options
->EmitNoIndirectInput
|| options
->EmitNoIndirectOutput
||
5186 options
->EmitNoIndirectTemp
|| options
->EmitNoIndirectUniform
) {
5187 lower_variable_index_to_cond_assign(ir
,
5188 options
->EmitNoIndirectInput
,
5189 options
->EmitNoIndirectOutput
,
5190 options
->EmitNoIndirectTemp
,
5191 options
->EmitNoIndirectUniform
);
5194 if (ctx
->Extensions
.ARB_shading_language_packing
) {
5195 unsigned lower_inst
= LOWER_PACK_SNORM_2x16
|
5196 LOWER_UNPACK_SNORM_2x16
|
5197 LOWER_PACK_UNORM_2x16
|
5198 LOWER_UNPACK_UNORM_2x16
|
5199 LOWER_PACK_SNORM_4x8
|
5200 LOWER_UNPACK_SNORM_4x8
|
5201 LOWER_UNPACK_UNORM_4x8
|
5202 LOWER_PACK_UNORM_4x8
|
5203 LOWER_PACK_HALF_2x16
|
5204 LOWER_UNPACK_HALF_2x16
;
5206 lower_packing_builtins(ir
, lower_inst
);
5209 do_mat_op_to_vec(ir
);
5210 lower_instructions(ir
,
5215 (options
->EmitNoPow
? POW_TO_EXP2
: 0) |
5216 (!ctx
->Const
.NativeIntegers
? INT_DIV_TO_MUL_RCP
: 0));
5218 lower_ubo_reference(prog
->_LinkedShaders
[i
], ir
);
5219 do_vec_index_to_cond_assign(ir
);
5220 lower_quadop_vector(ir
, false);
5222 if (options
->MaxIfDepth
== 0) {
5229 progress
= do_lower_jumps(ir
, true, true, options
->EmitNoMainReturn
, options
->EmitNoCont
, options
->EmitNoLoops
) || progress
;
5231 progress
= do_common_optimization(ir
, true, true,
5232 options
->MaxUnrollIterations
)
5235 progress
= lower_if_to_cond_assign(ir
, options
->MaxIfDepth
) || progress
;
5239 validate_ir_tree(ir
);
5242 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
5243 struct gl_program
*linked_prog
;
5245 if (prog
->_LinkedShaders
[i
] == NULL
)
5248 linked_prog
= get_mesa_program(ctx
, prog
, prog
->_LinkedShaders
[i
]);
5251 static const GLenum targets
[] = {
5252 GL_VERTEX_PROGRAM_ARB
,
5253 GL_FRAGMENT_PROGRAM_ARB
,
5254 GL_GEOMETRY_PROGRAM_NV
5257 _mesa_reference_program(ctx
, &prog
->_LinkedShaders
[i
]->Program
,
5259 if (!ctx
->Driver
.ProgramStringNotify(ctx
, targets
[i
], linked_prog
)) {
5260 _mesa_reference_program(ctx
, &prog
->_LinkedShaders
[i
]->Program
,
5262 _mesa_reference_program(ctx
, &linked_prog
, NULL
);
5267 _mesa_reference_program(ctx
, &linked_prog
, NULL
);
5274 st_translate_stream_output_info(glsl_to_tgsi_visitor
*glsl_to_tgsi
,
5275 const GLuint outputMapping
[],
5276 struct pipe_stream_output_info
*so
)
5279 struct gl_transform_feedback_info
*info
=
5280 &glsl_to_tgsi
->shader_program
->LinkedTransformFeedback
;
5282 for (i
= 0; i
< info
->NumOutputs
; i
++) {
5283 so
->output
[i
].register_index
=
5284 outputMapping
[info
->Outputs
[i
].OutputRegister
];
5285 so
->output
[i
].start_component
= info
->Outputs
[i
].ComponentOffset
;
5286 so
->output
[i
].num_components
= info
->Outputs
[i
].NumComponents
;
5287 so
->output
[i
].output_buffer
= info
->Outputs
[i
].OutputBuffer
;
5288 so
->output
[i
].dst_offset
= info
->Outputs
[i
].DstOffset
;
5291 for (i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
5292 so
->stride
[i
] = info
->BufferStride
[i
];
5294 so
->num_outputs
= info
->NumOutputs
;