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_NAMED_PARAM) | \
78 (1 << PROGRAM_CONSTANT) | \
79 (1 << PROGRAM_UNIFORM))
82 * Maximum number of temporary registers.
84 * It is too big for stack allocated arrays -- it will cause stack overflow on
85 * Windows and likely Mac OS X.
87 #define MAX_TEMPS 4096
89 /* will be 4 for GLSL 4.00 */
90 #define MAX_GLSL_TEXTURE_OFFSET 1
95 static int swizzle_for_size(int size
);
98 * This struct is a corresponding struct to TGSI ureg_src.
102 st_src_reg(gl_register_file file
, int index
, const glsl_type
*type
)
106 if (type
&& (type
->is_scalar() || type
->is_vector() || type
->is_matrix()))
107 this->swizzle
= swizzle_for_size(type
->vector_elements
);
109 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
)
120 this->swizzle
= SWIZZLE_XYZW
;
122 this->reladdr
= NULL
;
127 this->type
= GLSL_TYPE_ERROR
;
128 this->file
= PROGRAM_UNDEFINED
;
132 this->reladdr
= NULL
;
135 explicit st_src_reg(st_dst_reg reg
);
137 gl_register_file file
; /**< PROGRAM_* from Mesa */
138 int index
; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
139 GLuint swizzle
; /**< SWIZZLE_XYZWONEZERO swizzles from Mesa. */
140 int negate
; /**< NEGATE_XYZW mask from mesa */
141 int type
; /** GLSL_TYPE_* from GLSL IR (enum glsl_base_type) */
142 /** Register index should be offset by the integer in this reg. */
148 st_dst_reg(gl_register_file file
, int writemask
, int type
)
152 this->writemask
= writemask
;
153 this->cond_mask
= COND_TR
;
154 this->reladdr
= NULL
;
160 this->type
= GLSL_TYPE_ERROR
;
161 this->file
= PROGRAM_UNDEFINED
;
164 this->cond_mask
= COND_TR
;
165 this->reladdr
= NULL
;
168 explicit st_dst_reg(st_src_reg reg
);
170 gl_register_file file
; /**< PROGRAM_* from Mesa */
171 int index
; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
172 int writemask
; /**< Bitfield of WRITEMASK_[XYZW] */
174 int type
; /** GLSL_TYPE_* from GLSL IR (enum glsl_base_type) */
175 /** Register index should be offset by the integer in this reg. */
179 st_src_reg::st_src_reg(st_dst_reg reg
)
181 this->type
= reg
.type
;
182 this->file
= reg
.file
;
183 this->index
= reg
.index
;
184 this->swizzle
= SWIZZLE_XYZW
;
186 this->reladdr
= reg
.reladdr
;
189 st_dst_reg::st_dst_reg(st_src_reg reg
)
191 this->type
= reg
.type
;
192 this->file
= reg
.file
;
193 this->index
= reg
.index
;
194 this->writemask
= WRITEMASK_XYZW
;
195 this->cond_mask
= COND_TR
;
196 this->reladdr
= reg
.reladdr
;
199 class glsl_to_tgsi_instruction
: public exec_node
{
201 /* Callers of this ralloc-based new need not call delete. It's
202 * easier to just ralloc_free 'ctx' (or any of its ancestors). */
203 static void* operator new(size_t size
, void *ctx
)
207 node
= rzalloc_size(ctx
, size
);
208 assert(node
!= NULL
);
216 /** Pointer to the ir source this tree came from for debugging */
218 GLboolean cond_update
;
220 int sampler
; /**< sampler index */
221 int tex_target
; /**< One of TEXTURE_*_INDEX */
222 GLboolean tex_shadow
;
223 struct tgsi_texture_offset tex_offsets
[MAX_GLSL_TEXTURE_OFFSET
];
224 unsigned tex_offset_num_offset
;
225 int dead_mask
; /**< Used in dead code elimination */
227 class function_entry
*function
; /* Set on TGSI_OPCODE_CAL or TGSI_OPCODE_BGNSUB */
230 class variable_storage
: public exec_node
{
232 variable_storage(ir_variable
*var
, gl_register_file file
, int index
)
233 : file(file
), index(index
), var(var
)
238 gl_register_file file
;
240 ir_variable
*var
; /* variable that maps to this, if any */
243 class immediate_storage
: public exec_node
{
245 immediate_storage(gl_constant_value
*values
, int size
, int type
)
247 memcpy(this->values
, values
, size
* sizeof(gl_constant_value
));
252 gl_constant_value values
[4];
253 int size
; /**< Number of components (1-4) */
254 int type
; /**< GL_FLOAT, GL_INT, GL_BOOL, or GL_UNSIGNED_INT */
257 class function_entry
: public exec_node
{
259 ir_function_signature
*sig
;
262 * identifier of this function signature used by the program.
264 * At the point that TGSI instructions for function calls are
265 * generated, we don't know the address of the first instruction of
266 * the function body. So we make the BranchTarget that is called a
267 * small integer and rewrite them during set_branchtargets().
272 * Pointer to first instruction of the function body.
274 * Set during function body emits after main() is processed.
276 glsl_to_tgsi_instruction
*bgn_inst
;
279 * Index of the first instruction of the function body in actual TGSI.
281 * Set after conversion from glsl_to_tgsi_instruction to TGSI.
285 /** Storage for the return value. */
286 st_src_reg return_reg
;
289 class glsl_to_tgsi_visitor
: public ir_visitor
{
291 glsl_to_tgsi_visitor();
292 ~glsl_to_tgsi_visitor();
294 function_entry
*current_function
;
296 struct gl_context
*ctx
;
297 struct gl_program
*prog
;
298 struct gl_shader_program
*shader_program
;
299 struct gl_shader_compiler_options
*options
;
303 int num_address_regs
;
305 bool indirect_addr_temps
;
306 bool indirect_addr_consts
;
307 int num_clip_distances
;
310 bool native_integers
;
312 variable_storage
*find_variable_storage(ir_variable
*var
);
314 int add_constant(gl_register_file file
, gl_constant_value values
[4],
315 int size
, int datatype
, GLuint
*swizzle_out
);
317 function_entry
*get_function_signature(ir_function_signature
*sig
);
319 st_src_reg
get_temp(const glsl_type
*type
);
320 void reladdr_to_temp(ir_instruction
*ir
, st_src_reg
*reg
, int *num_reladdr
);
322 st_src_reg
st_src_reg_for_float(float val
);
323 st_src_reg
st_src_reg_for_int(int val
);
324 st_src_reg
st_src_reg_for_type(int type
, int val
);
327 * \name Visit methods
329 * As typical for the visitor pattern, there must be one \c visit method for
330 * each concrete subclass of \c ir_instruction. Virtual base classes within
331 * the hierarchy should not have \c visit methods.
334 virtual void visit(ir_variable
*);
335 virtual void visit(ir_loop
*);
336 virtual void visit(ir_loop_jump
*);
337 virtual void visit(ir_function_signature
*);
338 virtual void visit(ir_function
*);
339 virtual void visit(ir_expression
*);
340 virtual void visit(ir_swizzle
*);
341 virtual void visit(ir_dereference_variable
*);
342 virtual void visit(ir_dereference_array
*);
343 virtual void visit(ir_dereference_record
*);
344 virtual void visit(ir_assignment
*);
345 virtual void visit(ir_constant
*);
346 virtual void visit(ir_call
*);
347 virtual void visit(ir_return
*);
348 virtual void visit(ir_discard
*);
349 virtual void visit(ir_texture
*);
350 virtual void visit(ir_if
*);
355 /** List of variable_storage */
358 /** List of immediate_storage */
359 exec_list immediates
;
360 unsigned num_immediates
;
362 /** List of function_entry */
363 exec_list function_signatures
;
364 int next_signature_id
;
366 /** List of glsl_to_tgsi_instruction */
367 exec_list instructions
;
369 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
);
371 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
,
372 st_dst_reg dst
, st_src_reg src0
);
374 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
,
375 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
);
377 glsl_to_tgsi_instruction
*emit(ir_instruction
*ir
, unsigned op
,
379 st_src_reg src0
, st_src_reg src1
, st_src_reg src2
);
381 unsigned get_opcode(ir_instruction
*ir
, unsigned op
,
383 st_src_reg src0
, st_src_reg src1
);
386 * Emit the correct dot-product instruction for the type of arguments
388 glsl_to_tgsi_instruction
*emit_dp(ir_instruction
*ir
,
394 void emit_scalar(ir_instruction
*ir
, unsigned op
,
395 st_dst_reg dst
, st_src_reg src0
);
397 void emit_scalar(ir_instruction
*ir
, unsigned op
,
398 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
);
400 void try_emit_float_set(ir_instruction
*ir
, unsigned op
, st_dst_reg dst
);
402 void emit_arl(ir_instruction
*ir
, st_dst_reg dst
, st_src_reg src0
);
404 void emit_scs(ir_instruction
*ir
, unsigned op
,
405 st_dst_reg dst
, const st_src_reg
&src
);
407 bool try_emit_mad(ir_expression
*ir
,
409 bool try_emit_mad_for_and_not(ir_expression
*ir
,
411 bool try_emit_sat(ir_expression
*ir
);
413 void emit_swz(ir_expression
*ir
);
415 bool process_move_condition(ir_rvalue
*ir
);
417 void simplify_cmp(void);
419 void rename_temp_register(int index
, int new_index
);
420 int get_first_temp_read(int index
);
421 int get_first_temp_write(int index
);
422 int get_last_temp_read(int index
);
423 int get_last_temp_write(int index
);
425 void copy_propagate(void);
426 void eliminate_dead_code(void);
427 int eliminate_dead_code_advanced(void);
428 void merge_registers(void);
429 void renumber_registers(void);
434 static st_src_reg undef_src
= st_src_reg(PROGRAM_UNDEFINED
, 0, GLSL_TYPE_ERROR
);
436 static st_dst_reg undef_dst
= st_dst_reg(PROGRAM_UNDEFINED
, SWIZZLE_NOOP
, GLSL_TYPE_ERROR
);
438 static st_dst_reg address_reg
= st_dst_reg(PROGRAM_ADDRESS
, WRITEMASK_X
, GLSL_TYPE_FLOAT
);
441 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...) PRINTFLIKE(2, 3);
444 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...)
448 ralloc_vasprintf_append(&prog
->InfoLog
, fmt
, args
);
451 prog
->LinkStatus
= GL_FALSE
;
455 swizzle_for_size(int size
)
457 int size_swizzles
[4] = {
458 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
),
459 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
),
460 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
),
461 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
464 assert((size
>= 1) && (size
<= 4));
465 return size_swizzles
[size
- 1];
469 is_tex_instruction(unsigned opcode
)
471 const tgsi_opcode_info
* info
= tgsi_get_opcode_info(opcode
);
476 num_inst_dst_regs(unsigned opcode
)
478 const tgsi_opcode_info
* info
= tgsi_get_opcode_info(opcode
);
479 return info
->num_dst
;
483 num_inst_src_regs(unsigned opcode
)
485 const tgsi_opcode_info
* info
= tgsi_get_opcode_info(opcode
);
486 return info
->is_tex
? info
->num_src
- 1 : info
->num_src
;
489 glsl_to_tgsi_instruction
*
490 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
,
492 st_src_reg src0
, st_src_reg src1
, st_src_reg src2
)
494 glsl_to_tgsi_instruction
*inst
= new(mem_ctx
) glsl_to_tgsi_instruction();
495 int num_reladdr
= 0, i
;
497 op
= get_opcode(ir
, op
, dst
, src0
, src1
);
499 /* If we have to do relative addressing, we want to load the ARL
500 * reg directly for one of the regs, and preload the other reladdr
501 * sources into temps.
503 num_reladdr
+= dst
.reladdr
!= NULL
;
504 num_reladdr
+= src0
.reladdr
!= NULL
;
505 num_reladdr
+= src1
.reladdr
!= NULL
;
506 num_reladdr
+= src2
.reladdr
!= NULL
;
508 reladdr_to_temp(ir
, &src2
, &num_reladdr
);
509 reladdr_to_temp(ir
, &src1
, &num_reladdr
);
510 reladdr_to_temp(ir
, &src0
, &num_reladdr
);
513 emit_arl(ir
, address_reg
, *dst
.reladdr
);
516 assert(num_reladdr
== 0);
526 inst
->function
= NULL
;
528 if (op
== TGSI_OPCODE_ARL
|| op
== TGSI_OPCODE_UARL
)
529 this->num_address_regs
= 1;
531 /* Update indirect addressing status used by TGSI */
534 case PROGRAM_TEMPORARY
:
535 this->indirect_addr_temps
= true;
537 case PROGRAM_LOCAL_PARAM
:
538 case PROGRAM_ENV_PARAM
:
539 case PROGRAM_STATE_VAR
:
540 case PROGRAM_NAMED_PARAM
:
541 case PROGRAM_CONSTANT
:
542 case PROGRAM_UNIFORM
:
543 this->indirect_addr_consts
= true;
545 case PROGRAM_IMMEDIATE
:
546 assert(!"immediates should not have indirect addressing");
553 for (i
=0; i
<3; i
++) {
554 if(inst
->src
[i
].reladdr
) {
555 switch(inst
->src
[i
].file
) {
556 case PROGRAM_TEMPORARY
:
557 this->indirect_addr_temps
= true;
559 case PROGRAM_LOCAL_PARAM
:
560 case PROGRAM_ENV_PARAM
:
561 case PROGRAM_STATE_VAR
:
562 case PROGRAM_NAMED_PARAM
:
563 case PROGRAM_CONSTANT
:
564 case PROGRAM_UNIFORM
:
565 this->indirect_addr_consts
= true;
567 case PROGRAM_IMMEDIATE
:
568 assert(!"immediates should not have indirect addressing");
577 this->instructions
.push_tail(inst
);
580 try_emit_float_set(ir
, op
, dst
);
586 glsl_to_tgsi_instruction
*
587 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
,
588 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
)
590 return emit(ir
, op
, dst
, src0
, src1
, undef_src
);
593 glsl_to_tgsi_instruction
*
594 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
,
595 st_dst_reg dst
, st_src_reg src0
)
597 assert(dst
.writemask
!= 0);
598 return emit(ir
, op
, dst
, src0
, undef_src
, undef_src
);
601 glsl_to_tgsi_instruction
*
602 glsl_to_tgsi_visitor::emit(ir_instruction
*ir
, unsigned op
)
604 return emit(ir
, op
, undef_dst
, undef_src
, undef_src
, undef_src
);
608 * Emits the code to convert the result of float SET instructions to integers.
611 glsl_to_tgsi_visitor::try_emit_float_set(ir_instruction
*ir
, unsigned op
,
614 if ((op
== TGSI_OPCODE_SEQ
||
615 op
== TGSI_OPCODE_SNE
||
616 op
== TGSI_OPCODE_SGE
||
617 op
== TGSI_OPCODE_SLT
))
619 st_src_reg src
= st_src_reg(dst
);
620 src
.negate
= ~src
.negate
;
621 dst
.type
= GLSL_TYPE_FLOAT
;
622 emit(ir
, TGSI_OPCODE_F2I
, dst
, src
);
627 * Determines whether to use an integer, unsigned integer, or float opcode
628 * based on the operands and input opcode, then emits the result.
631 glsl_to_tgsi_visitor::get_opcode(ir_instruction
*ir
, unsigned op
,
633 st_src_reg src0
, st_src_reg src1
)
635 int type
= GLSL_TYPE_FLOAT
;
637 if (src0
.type
== GLSL_TYPE_FLOAT
|| src1
.type
== GLSL_TYPE_FLOAT
)
638 type
= GLSL_TYPE_FLOAT
;
639 else if (native_integers
)
640 type
= src0
.type
== GLSL_TYPE_BOOL
? GLSL_TYPE_INT
: src0
.type
;
642 #define case4(c, f, i, u) \
643 case TGSI_OPCODE_##c: \
644 if (type == GLSL_TYPE_INT) op = TGSI_OPCODE_##i; \
645 else if (type == GLSL_TYPE_UINT) op = TGSI_OPCODE_##u; \
646 else op = TGSI_OPCODE_##f; \
648 #define case3(f, i, u) case4(f, f, i, u)
649 #define case2fi(f, i) case4(f, f, i, i)
650 #define case2iu(i, u) case4(i, LAST, i, u)
656 case3(DIV
, IDIV
, UDIV
);
657 case3(MAX
, IMAX
, UMAX
);
658 case3(MIN
, IMIN
, UMIN
);
663 case3(SGE
, ISGE
, USGE
);
664 case3(SLT
, ISLT
, USLT
);
669 case3(ABS
, IABS
, IABS
);
674 assert(op
!= TGSI_OPCODE_LAST
);
678 glsl_to_tgsi_instruction
*
679 glsl_to_tgsi_visitor::emit_dp(ir_instruction
*ir
,
680 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
,
683 static const unsigned dot_opcodes
[] = {
684 TGSI_OPCODE_DP2
, TGSI_OPCODE_DP3
, TGSI_OPCODE_DP4
687 return emit(ir
, dot_opcodes
[elements
- 2], dst
, src0
, src1
);
691 * Emits TGSI scalar opcodes to produce unique answers across channels.
693 * Some TGSI opcodes are scalar-only, like ARB_fp/vp. The src X
694 * channel determines the result across all channels. So to do a vec4
695 * of this operation, we want to emit a scalar per source channel used
696 * to produce dest channels.
699 glsl_to_tgsi_visitor::emit_scalar(ir_instruction
*ir
, unsigned op
,
701 st_src_reg orig_src0
, st_src_reg orig_src1
)
704 int done_mask
= ~dst
.writemask
;
706 /* TGSI RCP is a scalar operation splatting results to all channels,
707 * like ARB_fp/vp. So emit as many RCPs as necessary to cover our
710 for (i
= 0; i
< 4; i
++) {
711 GLuint this_mask
= (1 << i
);
712 glsl_to_tgsi_instruction
*inst
;
713 st_src_reg src0
= orig_src0
;
714 st_src_reg src1
= orig_src1
;
716 if (done_mask
& this_mask
)
719 GLuint src0_swiz
= GET_SWZ(src0
.swizzle
, i
);
720 GLuint src1_swiz
= GET_SWZ(src1
.swizzle
, i
);
721 for (j
= i
+ 1; j
< 4; j
++) {
722 /* If there is another enabled component in the destination that is
723 * derived from the same inputs, generate its value on this pass as
726 if (!(done_mask
& (1 << j
)) &&
727 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
&&
728 GET_SWZ(src1
.swizzle
, j
) == src1_swiz
) {
729 this_mask
|= (1 << j
);
732 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
733 src0_swiz
, src0_swiz
);
734 src1
.swizzle
= MAKE_SWIZZLE4(src1_swiz
, src1_swiz
,
735 src1_swiz
, src1_swiz
);
737 inst
= emit(ir
, op
, dst
, src0
, src1
);
738 inst
->dst
.writemask
= this_mask
;
739 done_mask
|= this_mask
;
744 glsl_to_tgsi_visitor::emit_scalar(ir_instruction
*ir
, unsigned op
,
745 st_dst_reg dst
, st_src_reg src0
)
747 st_src_reg undef
= undef_src
;
749 undef
.swizzle
= SWIZZLE_XXXX
;
751 emit_scalar(ir
, op
, dst
, src0
, undef
);
755 glsl_to_tgsi_visitor::emit_arl(ir_instruction
*ir
,
756 st_dst_reg dst
, st_src_reg src0
)
758 int op
= TGSI_OPCODE_ARL
;
760 if (src0
.type
== GLSL_TYPE_INT
|| src0
.type
== GLSL_TYPE_UINT
)
761 op
= TGSI_OPCODE_UARL
;
763 emit(NULL
, op
, dst
, src0
);
767 * Emit an TGSI_OPCODE_SCS instruction
769 * The \c SCS opcode functions a bit differently than the other TGSI opcodes.
770 * Instead of splatting its result across all four components of the
771 * destination, it writes one value to the \c x component and another value to
772 * the \c y component.
774 * \param ir IR instruction being processed
775 * \param op Either \c TGSI_OPCODE_SIN or \c TGSI_OPCODE_COS depending
776 * on which value is desired.
777 * \param dst Destination register
778 * \param src Source register
781 glsl_to_tgsi_visitor::emit_scs(ir_instruction
*ir
, unsigned op
,
783 const st_src_reg
&src
)
785 /* Vertex programs cannot use the SCS opcode.
787 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
788 emit_scalar(ir
, op
, dst
, src
);
792 const unsigned component
= (op
== TGSI_OPCODE_SIN
) ? 0 : 1;
793 const unsigned scs_mask
= (1U << component
);
794 int done_mask
= ~dst
.writemask
;
797 assert(op
== TGSI_OPCODE_SIN
|| op
== TGSI_OPCODE_COS
);
799 /* If there are compnents in the destination that differ from the component
800 * that will be written by the SCS instrution, we'll need a temporary.
802 if (scs_mask
!= unsigned(dst
.writemask
)) {
803 tmp
= get_temp(glsl_type::vec4_type
);
806 for (unsigned i
= 0; i
< 4; i
++) {
807 unsigned this_mask
= (1U << i
);
808 st_src_reg src0
= src
;
810 if ((done_mask
& this_mask
) != 0)
813 /* The source swizzle specified which component of the source generates
814 * sine / cosine for the current component in the destination. The SCS
815 * instruction requires that this value be swizzle to the X component.
816 * Replace the current swizzle with a swizzle that puts the source in
819 unsigned src0_swiz
= GET_SWZ(src
.swizzle
, i
);
821 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
822 src0_swiz
, src0_swiz
);
823 for (unsigned j
= i
+ 1; j
< 4; j
++) {
824 /* If there is another enabled component in the destination that is
825 * derived from the same inputs, generate its value on this pass as
828 if (!(done_mask
& (1 << j
)) &&
829 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
) {
830 this_mask
|= (1 << j
);
834 if (this_mask
!= scs_mask
) {
835 glsl_to_tgsi_instruction
*inst
;
836 st_dst_reg tmp_dst
= st_dst_reg(tmp
);
838 /* Emit the SCS instruction.
840 inst
= emit(ir
, TGSI_OPCODE_SCS
, tmp_dst
, src0
);
841 inst
->dst
.writemask
= scs_mask
;
843 /* Move the result of the SCS instruction to the desired location in
846 tmp
.swizzle
= MAKE_SWIZZLE4(component
, component
,
847 component
, component
);
848 inst
= emit(ir
, TGSI_OPCODE_SCS
, dst
, tmp
);
849 inst
->dst
.writemask
= this_mask
;
851 /* Emit the SCS instruction to write directly to the destination.
853 glsl_to_tgsi_instruction
*inst
= emit(ir
, TGSI_OPCODE_SCS
, dst
, src0
);
854 inst
->dst
.writemask
= scs_mask
;
857 done_mask
|= this_mask
;
862 glsl_to_tgsi_visitor::add_constant(gl_register_file file
,
863 gl_constant_value values
[4], int size
, int datatype
,
866 if (file
== PROGRAM_CONSTANT
) {
867 return _mesa_add_typed_unnamed_constant(this->prog
->Parameters
, values
,
868 size
, datatype
, swizzle_out
);
871 immediate_storage
*entry
;
872 assert(file
== PROGRAM_IMMEDIATE
);
874 /* Search immediate storage to see if we already have an identical
875 * immediate that we can use instead of adding a duplicate entry.
877 foreach_iter(exec_list_iterator
, iter
, this->immediates
) {
878 entry
= (immediate_storage
*)iter
.get();
880 if (entry
->size
== size
&&
881 entry
->type
== datatype
&&
882 !memcmp(entry
->values
, values
, size
* sizeof(gl_constant_value
))) {
888 /* Add this immediate to the list. */
889 entry
= new(mem_ctx
) immediate_storage(values
, size
, datatype
);
890 this->immediates
.push_tail(entry
);
891 this->num_immediates
++;
897 glsl_to_tgsi_visitor::st_src_reg_for_float(float val
)
899 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_FLOAT
);
900 union gl_constant_value uval
;
903 src
.index
= add_constant(src
.file
, &uval
, 1, GL_FLOAT
, &src
.swizzle
);
909 glsl_to_tgsi_visitor::st_src_reg_for_int(int val
)
911 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_INT
);
912 union gl_constant_value uval
;
914 assert(native_integers
);
917 src
.index
= add_constant(src
.file
, &uval
, 1, GL_INT
, &src
.swizzle
);
923 glsl_to_tgsi_visitor::st_src_reg_for_type(int type
, int val
)
926 return type
== GLSL_TYPE_FLOAT
? st_src_reg_for_float(val
) :
927 st_src_reg_for_int(val
);
929 return st_src_reg_for_float(val
);
933 type_size(const struct glsl_type
*type
)
938 switch (type
->base_type
) {
941 case GLSL_TYPE_FLOAT
:
943 if (type
->is_matrix()) {
944 return type
->matrix_columns
;
946 /* Regardless of size of vector, it gets a vec4. This is bad
947 * packing for things like floats, but otherwise arrays become a
948 * mess. Hopefully a later pass over the code can pack scalars
949 * down if appropriate.
953 case GLSL_TYPE_ARRAY
:
954 assert(type
->length
> 0);
955 return type_size(type
->fields
.array
) * type
->length
;
956 case GLSL_TYPE_STRUCT
:
958 for (i
= 0; i
< type
->length
; i
++) {
959 size
+= type_size(type
->fields
.structure
[i
].type
);
962 case GLSL_TYPE_SAMPLER
:
963 /* Samplers take up one slot in UNIFORMS[], but they're baked in
974 * In the initial pass of codegen, we assign temporary numbers to
975 * intermediate results. (not SSA -- variable assignments will reuse
979 glsl_to_tgsi_visitor::get_temp(const glsl_type
*type
)
983 src
.type
= native_integers
? type
->base_type
: GLSL_TYPE_FLOAT
;
984 src
.file
= PROGRAM_TEMPORARY
;
985 src
.index
= next_temp
;
987 next_temp
+= type_size(type
);
989 if (type
->is_array() || type
->is_record()) {
990 src
.swizzle
= SWIZZLE_NOOP
;
992 src
.swizzle
= swizzle_for_size(type
->vector_elements
);
1000 glsl_to_tgsi_visitor::find_variable_storage(ir_variable
*var
)
1003 variable_storage
*entry
;
1005 foreach_iter(exec_list_iterator
, iter
, this->variables
) {
1006 entry
= (variable_storage
*)iter
.get();
1008 if (entry
->var
== var
)
1016 glsl_to_tgsi_visitor::visit(ir_variable
*ir
)
1018 if (strcmp(ir
->name
, "gl_FragCoord") == 0) {
1019 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
1021 fp
->OriginUpperLeft
= ir
->origin_upper_left
;
1022 fp
->PixelCenterInteger
= ir
->pixel_center_integer
;
1025 if (ir
->mode
== ir_var_uniform
&& strncmp(ir
->name
, "gl_", 3) == 0) {
1027 const ir_state_slot
*const slots
= ir
->state_slots
;
1028 assert(ir
->state_slots
!= NULL
);
1030 /* Check if this statevar's setup in the STATE file exactly
1031 * matches how we'll want to reference it as a
1032 * struct/array/whatever. If not, then we need to move it into
1033 * temporary storage and hope that it'll get copy-propagated
1036 for (i
= 0; i
< ir
->num_state_slots
; i
++) {
1037 if (slots
[i
].swizzle
!= SWIZZLE_XYZW
) {
1042 variable_storage
*storage
;
1044 if (i
== ir
->num_state_slots
) {
1045 /* We'll set the index later. */
1046 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_STATE_VAR
, -1);
1047 this->variables
.push_tail(storage
);
1051 /* The variable_storage constructor allocates slots based on the size
1052 * of the type. However, this had better match the number of state
1053 * elements that we're going to copy into the new temporary.
1055 assert((int) ir
->num_state_slots
== type_size(ir
->type
));
1057 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_TEMPORARY
,
1059 this->variables
.push_tail(storage
);
1060 this->next_temp
+= type_size(ir
->type
);
1062 dst
= st_dst_reg(st_src_reg(PROGRAM_TEMPORARY
, storage
->index
,
1063 native_integers
? ir
->type
->base_type
: GLSL_TYPE_FLOAT
));
1067 for (unsigned int i
= 0; i
< ir
->num_state_slots
; i
++) {
1068 int index
= _mesa_add_state_reference(this->prog
->Parameters
,
1069 (gl_state_index
*)slots
[i
].tokens
);
1071 if (storage
->file
== PROGRAM_STATE_VAR
) {
1072 if (storage
->index
== -1) {
1073 storage
->index
= index
;
1075 assert(index
== storage
->index
+ (int)i
);
1078 st_src_reg
src(PROGRAM_STATE_VAR
, index
,
1079 native_integers
? ir
->type
->base_type
: GLSL_TYPE_FLOAT
);
1080 src
.swizzle
= slots
[i
].swizzle
;
1081 emit(ir
, TGSI_OPCODE_MOV
, dst
, src
);
1082 /* even a float takes up a whole vec4 reg in a struct/array. */
1087 if (storage
->file
== PROGRAM_TEMPORARY
&&
1088 dst
.index
!= storage
->index
+ (int) ir
->num_state_slots
) {
1089 fail_link(this->shader_program
,
1090 "failed to load builtin uniform `%s' (%d/%d regs loaded)\n",
1091 ir
->name
, dst
.index
- storage
->index
,
1092 type_size(ir
->type
));
1098 glsl_to_tgsi_visitor::visit(ir_loop
*ir
)
1100 ir_dereference_variable
*counter
= NULL
;
1102 if (ir
->counter
!= NULL
)
1103 counter
= new(ir
) ir_dereference_variable(ir
->counter
);
1105 if (ir
->from
!= NULL
) {
1106 assert(ir
->counter
!= NULL
);
1108 ir_assignment
*a
= new(ir
) ir_assignment(counter
, ir
->from
, NULL
);
1114 emit(NULL
, TGSI_OPCODE_BGNLOOP
);
1118 new(ir
) ir_expression(ir
->cmp
, glsl_type::bool_type
,
1120 ir_if
*if_stmt
= new(ir
) ir_if(e
);
1122 ir_loop_jump
*brk
= new(ir
) ir_loop_jump(ir_loop_jump::jump_break
);
1124 if_stmt
->then_instructions
.push_tail(brk
);
1126 if_stmt
->accept(this);
1133 visit_exec_list(&ir
->body_instructions
, this);
1135 if (ir
->increment
) {
1137 new(ir
) ir_expression(ir_binop_add
, counter
->type
,
1138 counter
, ir
->increment
);
1140 ir_assignment
*a
= new(ir
) ir_assignment(counter
, e
, NULL
);
1147 emit(NULL
, TGSI_OPCODE_ENDLOOP
);
1151 glsl_to_tgsi_visitor::visit(ir_loop_jump
*ir
)
1154 case ir_loop_jump::jump_break
:
1155 emit(NULL
, TGSI_OPCODE_BRK
);
1157 case ir_loop_jump::jump_continue
:
1158 emit(NULL
, TGSI_OPCODE_CONT
);
1165 glsl_to_tgsi_visitor::visit(ir_function_signature
*ir
)
1172 glsl_to_tgsi_visitor::visit(ir_function
*ir
)
1174 /* Ignore function bodies other than main() -- we shouldn't see calls to
1175 * them since they should all be inlined before we get to glsl_to_tgsi.
1177 if (strcmp(ir
->name
, "main") == 0) {
1178 const ir_function_signature
*sig
;
1181 sig
= ir
->matching_signature(&empty
);
1185 foreach_iter(exec_list_iterator
, iter
, sig
->body
) {
1186 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
1194 glsl_to_tgsi_visitor::try_emit_mad(ir_expression
*ir
, int mul_operand
)
1196 int nonmul_operand
= 1 - mul_operand
;
1198 st_dst_reg result_dst
;
1200 ir_expression
*expr
= ir
->operands
[mul_operand
]->as_expression();
1201 if (!expr
|| expr
->operation
!= ir_binop_mul
)
1204 expr
->operands
[0]->accept(this);
1206 expr
->operands
[1]->accept(this);
1208 ir
->operands
[nonmul_operand
]->accept(this);
1211 this->result
= get_temp(ir
->type
);
1212 result_dst
= st_dst_reg(this->result
);
1213 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1214 emit(ir
, TGSI_OPCODE_MAD
, result_dst
, a
, b
, c
);
1220 * Emit MAD(a, -b, a) instead of AND(a, NOT(b))
1222 * The logic values are 1.0 for true and 0.0 for false. Logical-and is
1223 * implemented using multiplication, and logical-or is implemented using
1224 * addition. Logical-not can be implemented as (true - x), or (1.0 - x).
1225 * As result, the logical expression (a & !b) can be rewritten as:
1229 * - (a * 1) - (a * b)
1233 * This final expression can be implemented as a single MAD(a, -b, a)
1237 glsl_to_tgsi_visitor::try_emit_mad_for_and_not(ir_expression
*ir
, int try_operand
)
1239 const int other_operand
= 1 - try_operand
;
1242 ir_expression
*expr
= ir
->operands
[try_operand
]->as_expression();
1243 if (!expr
|| expr
->operation
!= ir_unop_logic_not
)
1246 ir
->operands
[other_operand
]->accept(this);
1248 expr
->operands
[0]->accept(this);
1251 b
.negate
= ~b
.negate
;
1253 this->result
= get_temp(ir
->type
);
1254 emit(ir
, TGSI_OPCODE_MAD
, st_dst_reg(this->result
), a
, b
, a
);
1260 glsl_to_tgsi_visitor::try_emit_sat(ir_expression
*ir
)
1262 /* Saturates were only introduced to vertex programs in
1263 * NV_vertex_program3, so don't give them to drivers in the VP.
1265 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
)
1268 ir_rvalue
*sat_src
= ir
->as_rvalue_to_saturate();
1272 sat_src
->accept(this);
1273 st_src_reg src
= this->result
;
1275 /* If we generated an expression instruction into a temporary in
1276 * processing the saturate's operand, apply the saturate to that
1277 * instruction. Otherwise, generate a MOV to do the saturate.
1279 * Note that we have to be careful to only do this optimization if
1280 * the instruction in question was what generated src->result. For
1281 * example, ir_dereference_array might generate a MUL instruction
1282 * to create the reladdr, and return us a src reg using that
1283 * reladdr. That MUL result is not the value we're trying to
1286 ir_expression
*sat_src_expr
= sat_src
->as_expression();
1287 if (sat_src_expr
&& (sat_src_expr
->operation
== ir_binop_mul
||
1288 sat_src_expr
->operation
== ir_binop_add
||
1289 sat_src_expr
->operation
== ir_binop_dot
)) {
1290 glsl_to_tgsi_instruction
*new_inst
;
1291 new_inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
1292 new_inst
->saturate
= true;
1294 this->result
= get_temp(ir
->type
);
1295 st_dst_reg result_dst
= st_dst_reg(this->result
);
1296 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1297 glsl_to_tgsi_instruction
*inst
;
1298 inst
= emit(ir
, TGSI_OPCODE_MOV
, result_dst
, src
);
1299 inst
->saturate
= true;
1306 glsl_to_tgsi_visitor::reladdr_to_temp(ir_instruction
*ir
,
1307 st_src_reg
*reg
, int *num_reladdr
)
1312 emit_arl(ir
, address_reg
, *reg
->reladdr
);
1314 if (*num_reladdr
!= 1) {
1315 st_src_reg temp
= get_temp(glsl_type::vec4_type
);
1317 emit(ir
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), *reg
);
1325 glsl_to_tgsi_visitor::visit(ir_expression
*ir
)
1327 unsigned int operand
;
1328 st_src_reg op
[Elements(ir
->operands
)];
1329 st_src_reg result_src
;
1330 st_dst_reg result_dst
;
1332 /* Quick peephole: Emit MAD(a, b, c) instead of ADD(MUL(a, b), c)
1334 if (ir
->operation
== ir_binop_add
) {
1335 if (try_emit_mad(ir
, 1))
1337 if (try_emit_mad(ir
, 0))
1341 /* Quick peephole: Emit OPCODE_MAD(-a, -b, a) instead of AND(a, NOT(b))
1343 if (ir
->operation
== ir_binop_logic_and
) {
1344 if (try_emit_mad_for_and_not(ir
, 1))
1346 if (try_emit_mad_for_and_not(ir
, 0))
1350 if (try_emit_sat(ir
))
1353 if (ir
->operation
== ir_quadop_vector
)
1354 assert(!"ir_quadop_vector should have been lowered");
1356 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
1357 this->result
.file
= PROGRAM_UNDEFINED
;
1358 ir
->operands
[operand
]->accept(this);
1359 if (this->result
.file
== PROGRAM_UNDEFINED
) {
1361 printf("Failed to get tree for expression operand:\n");
1362 ir
->operands
[operand
]->accept(&v
);
1365 op
[operand
] = this->result
;
1367 /* Matrix expression operands should have been broken down to vector
1368 * operations already.
1370 assert(!ir
->operands
[operand
]->type
->is_matrix());
1373 int vector_elements
= ir
->operands
[0]->type
->vector_elements
;
1374 if (ir
->operands
[1]) {
1375 vector_elements
= MAX2(vector_elements
,
1376 ir
->operands
[1]->type
->vector_elements
);
1379 this->result
.file
= PROGRAM_UNDEFINED
;
1381 /* Storage for our result. Ideally for an assignment we'd be using
1382 * the actual storage for the result here, instead.
1384 result_src
= get_temp(ir
->type
);
1385 /* convenience for the emit functions below. */
1386 result_dst
= st_dst_reg(result_src
);
1387 /* Limit writes to the channels that will be used by result_src later.
1388 * This does limit this temp's use as a temporary for multi-instruction
1391 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1393 switch (ir
->operation
) {
1394 case ir_unop_logic_not
:
1395 if (result_dst
.type
!= GLSL_TYPE_FLOAT
)
1396 emit(ir
, TGSI_OPCODE_NOT
, result_dst
, op
[0]);
1398 /* Previously 'SEQ dst, src, 0.0' was used for this. However, many
1399 * older GPUs implement SEQ using multiple instructions (i915 uses two
1400 * SGE instructions and a MUL instruction). Since our logic values are
1401 * 0.0 and 1.0, 1-x also implements !x.
1403 op
[0].negate
= ~op
[0].negate
;
1404 emit(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], st_src_reg_for_float(1.0));
1408 if (result_dst
.type
== GLSL_TYPE_INT
|| result_dst
.type
== GLSL_TYPE_UINT
)
1409 emit(ir
, TGSI_OPCODE_INEG
, result_dst
, op
[0]);
1411 op
[0].negate
= ~op
[0].negate
;
1416 emit(ir
, TGSI_OPCODE_ABS
, result_dst
, op
[0]);
1419 emit(ir
, TGSI_OPCODE_SSG
, result_dst
, op
[0]);
1422 emit_scalar(ir
, TGSI_OPCODE_RCP
, result_dst
, op
[0]);
1426 emit_scalar(ir
, TGSI_OPCODE_EX2
, result_dst
, op
[0]);
1430 assert(!"not reached: should be handled by ir_explog_to_explog2");
1433 emit_scalar(ir
, TGSI_OPCODE_LG2
, result_dst
, op
[0]);
1436 emit_scalar(ir
, TGSI_OPCODE_SIN
, result_dst
, op
[0]);
1439 emit_scalar(ir
, TGSI_OPCODE_COS
, result_dst
, op
[0]);
1441 case ir_unop_sin_reduced
:
1442 emit_scs(ir
, TGSI_OPCODE_SIN
, result_dst
, op
[0]);
1444 case ir_unop_cos_reduced
:
1445 emit_scs(ir
, TGSI_OPCODE_COS
, result_dst
, op
[0]);
1449 emit(ir
, TGSI_OPCODE_DDX
, result_dst
, op
[0]);
1452 op
[0].negate
= ~op
[0].negate
;
1453 emit(ir
, TGSI_OPCODE_DDY
, result_dst
, op
[0]);
1456 case ir_unop_noise
: {
1457 /* At some point, a motivated person could add a better
1458 * implementation of noise. Currently not even the nvidia
1459 * binary drivers do anything more than this. In any case, the
1460 * place to do this is in the GL state tracker, not the poor
1463 emit(ir
, TGSI_OPCODE_MOV
, result_dst
, st_src_reg_for_float(0.5));
1468 emit(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1471 emit(ir
, TGSI_OPCODE_SUB
, result_dst
, op
[0], op
[1]);
1475 emit(ir
, TGSI_OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1478 if (result_dst
.type
== GLSL_TYPE_FLOAT
)
1479 assert(!"not reached: should be handled by ir_div_to_mul_rcp");
1481 emit(ir
, TGSI_OPCODE_DIV
, result_dst
, op
[0], op
[1]);
1484 if (result_dst
.type
== GLSL_TYPE_FLOAT
)
1485 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
1487 emit(ir
, TGSI_OPCODE_MOD
, result_dst
, op
[0], op
[1]);
1491 emit(ir
, TGSI_OPCODE_SLT
, result_dst
, op
[0], op
[1]);
1493 case ir_binop_greater
:
1494 emit(ir
, TGSI_OPCODE_SLT
, result_dst
, op
[1], op
[0]);
1496 case ir_binop_lequal
:
1497 emit(ir
, TGSI_OPCODE_SGE
, result_dst
, op
[1], op
[0]);
1499 case ir_binop_gequal
:
1500 emit(ir
, TGSI_OPCODE_SGE
, result_dst
, op
[0], op
[1]);
1502 case ir_binop_equal
:
1503 emit(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1505 case ir_binop_nequal
:
1506 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1508 case ir_binop_all_equal
:
1509 /* "==" operator producing a scalar boolean. */
1510 if (ir
->operands
[0]->type
->is_vector() ||
1511 ir
->operands
[1]->type
->is_vector()) {
1512 st_src_reg temp
= get_temp(native_integers
?
1513 glsl_type::get_instance(ir
->operands
[0]->type
->base_type
, 4, 1) :
1514 glsl_type::vec4_type
);
1516 if (native_integers
) {
1517 st_dst_reg temp_dst
= st_dst_reg(temp
);
1518 st_src_reg temp1
= st_src_reg(temp
), temp2
= st_src_reg(temp
);
1520 emit(ir
, TGSI_OPCODE_SEQ
, st_dst_reg(temp
), op
[0], op
[1]);
1522 /* Emit 1-3 AND operations to combine the SEQ results. */
1523 switch (ir
->operands
[0]->type
->vector_elements
) {
1527 temp_dst
.writemask
= WRITEMASK_Y
;
1528 temp1
.swizzle
= SWIZZLE_YYYY
;
1529 temp2
.swizzle
= SWIZZLE_ZZZZ
;
1530 emit(ir
, TGSI_OPCODE_AND
, temp_dst
, temp1
, temp2
);
1533 temp_dst
.writemask
= WRITEMASK_X
;
1534 temp1
.swizzle
= SWIZZLE_XXXX
;
1535 temp2
.swizzle
= SWIZZLE_YYYY
;
1536 emit(ir
, TGSI_OPCODE_AND
, temp_dst
, temp1
, temp2
);
1537 temp_dst
.writemask
= WRITEMASK_Y
;
1538 temp1
.swizzle
= SWIZZLE_ZZZZ
;
1539 temp2
.swizzle
= SWIZZLE_WWWW
;
1540 emit(ir
, TGSI_OPCODE_AND
, temp_dst
, temp1
, temp2
);
1543 temp1
.swizzle
= SWIZZLE_XXXX
;
1544 temp2
.swizzle
= SWIZZLE_YYYY
;
1545 emit(ir
, TGSI_OPCODE_AND
, result_dst
, temp1
, temp2
);
1547 emit(ir
, TGSI_OPCODE_SNE
, st_dst_reg(temp
), op
[0], op
[1]);
1549 /* After the dot-product, the value will be an integer on the
1550 * range [0,4]. Zero becomes 1.0, and positive values become zero.
1552 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1554 /* Negating the result of the dot-product gives values on the range
1555 * [-4, 0]. Zero becomes 1.0, and negative values become zero.
1556 * This is achieved using SGE.
1558 st_src_reg sge_src
= result_src
;
1559 sge_src
.negate
= ~sge_src
.negate
;
1560 emit(ir
, TGSI_OPCODE_SGE
, result_dst
, sge_src
, st_src_reg_for_float(0.0));
1563 emit(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1566 case ir_binop_any_nequal
:
1567 /* "!=" operator producing a scalar boolean. */
1568 if (ir
->operands
[0]->type
->is_vector() ||
1569 ir
->operands
[1]->type
->is_vector()) {
1570 st_src_reg temp
= get_temp(native_integers
?
1571 glsl_type::get_instance(ir
->operands
[0]->type
->base_type
, 4, 1) :
1572 glsl_type::vec4_type
);
1573 emit(ir
, TGSI_OPCODE_SNE
, st_dst_reg(temp
), op
[0], op
[1]);
1575 if (native_integers
) {
1576 st_dst_reg temp_dst
= st_dst_reg(temp
);
1577 st_src_reg temp1
= st_src_reg(temp
), temp2
= st_src_reg(temp
);
1579 /* Emit 1-3 OR operations to combine the SNE results. */
1580 switch (ir
->operands
[0]->type
->vector_elements
) {
1584 temp_dst
.writemask
= WRITEMASK_Y
;
1585 temp1
.swizzle
= SWIZZLE_YYYY
;
1586 temp2
.swizzle
= SWIZZLE_ZZZZ
;
1587 emit(ir
, TGSI_OPCODE_OR
, temp_dst
, temp1
, temp2
);
1590 temp_dst
.writemask
= WRITEMASK_X
;
1591 temp1
.swizzle
= SWIZZLE_XXXX
;
1592 temp2
.swizzle
= SWIZZLE_YYYY
;
1593 emit(ir
, TGSI_OPCODE_OR
, temp_dst
, temp1
, temp2
);
1594 temp_dst
.writemask
= WRITEMASK_Y
;
1595 temp1
.swizzle
= SWIZZLE_ZZZZ
;
1596 temp2
.swizzle
= SWIZZLE_WWWW
;
1597 emit(ir
, TGSI_OPCODE_OR
, temp_dst
, temp1
, temp2
);
1600 temp1
.swizzle
= SWIZZLE_XXXX
;
1601 temp2
.swizzle
= SWIZZLE_YYYY
;
1602 emit(ir
, TGSI_OPCODE_OR
, result_dst
, temp1
, temp2
);
1604 /* After the dot-product, the value will be an integer on the
1605 * range [0,4]. Zero stays zero, and positive values become 1.0.
1607 glsl_to_tgsi_instruction
*const dp
=
1608 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1609 if (this->prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1610 /* The clamping to [0,1] can be done for free in the fragment
1611 * shader with a saturate.
1613 dp
->saturate
= true;
1615 /* Negating the result of the dot-product gives values on the range
1616 * [-4, 0]. Zero stays zero, and negative values become 1.0. This
1617 * achieved using SLT.
1619 st_src_reg slt_src
= result_src
;
1620 slt_src
.negate
= ~slt_src
.negate
;
1621 emit(ir
, TGSI_OPCODE_SLT
, result_dst
, slt_src
, st_src_reg_for_float(0.0));
1625 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1630 assert(ir
->operands
[0]->type
->is_vector());
1632 /* After the dot-product, the value will be an integer on the
1633 * range [0,4]. Zero stays zero, and positive values become 1.0.
1635 glsl_to_tgsi_instruction
*const dp
=
1636 emit_dp(ir
, result_dst
, op
[0], op
[0],
1637 ir
->operands
[0]->type
->vector_elements
);
1638 if (this->prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
&&
1639 result_dst
.type
== GLSL_TYPE_FLOAT
) {
1640 /* The clamping to [0,1] can be done for free in the fragment
1641 * shader with a saturate.
1643 dp
->saturate
= true;
1644 } else if (result_dst
.type
== GLSL_TYPE_FLOAT
) {
1645 /* Negating the result of the dot-product gives values on the range
1646 * [-4, 0]. Zero stays zero, and negative values become 1.0. This
1647 * is achieved using SLT.
1649 st_src_reg slt_src
= result_src
;
1650 slt_src
.negate
= ~slt_src
.negate
;
1651 emit(ir
, TGSI_OPCODE_SLT
, result_dst
, slt_src
, st_src_reg_for_float(0.0));
1654 /* Use SNE 0 if integers are being used as boolean values. */
1655 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, result_src
, st_src_reg_for_int(0));
1660 case ir_binop_logic_xor
:
1661 if (native_integers
)
1662 emit(ir
, TGSI_OPCODE_XOR
, result_dst
, op
[0], op
[1]);
1664 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1667 case ir_binop_logic_or
: {
1668 if (native_integers
) {
1669 /* If integers are used as booleans, we can use an actual "or"
1672 assert(native_integers
);
1673 emit(ir
, TGSI_OPCODE_OR
, result_dst
, op
[0], op
[1]);
1675 /* After the addition, the value will be an integer on the
1676 * range [0,2]. Zero stays zero, and positive values become 1.0.
1678 glsl_to_tgsi_instruction
*add
=
1679 emit(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1680 if (this->prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1681 /* The clamping to [0,1] can be done for free in the fragment
1682 * shader with a saturate if floats are being used as boolean values.
1684 add
->saturate
= true;
1686 /* Negating the result of the addition gives values on the range
1687 * [-2, 0]. Zero stays zero, and negative values become 1.0. This
1688 * is achieved using SLT.
1690 st_src_reg slt_src
= result_src
;
1691 slt_src
.negate
= ~slt_src
.negate
;
1692 emit(ir
, TGSI_OPCODE_SLT
, result_dst
, slt_src
, st_src_reg_for_float(0.0));
1698 case ir_binop_logic_and
:
1699 /* If native integers are disabled, the bool args are stored as float 0.0
1700 * or 1.0, so "mul" gives us "and". If they're enabled, just use the
1701 * actual AND opcode.
1703 if (native_integers
)
1704 emit(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], op
[1]);
1706 emit(ir
, TGSI_OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1710 assert(ir
->operands
[0]->type
->is_vector());
1711 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
1712 emit_dp(ir
, result_dst
, op
[0], op
[1],
1713 ir
->operands
[0]->type
->vector_elements
);
1717 /* sqrt(x) = x * rsq(x). */
1718 emit_scalar(ir
, TGSI_OPCODE_RSQ
, result_dst
, op
[0]);
1719 emit(ir
, TGSI_OPCODE_MUL
, result_dst
, result_src
, op
[0]);
1720 /* For incoming channels <= 0, set the result to 0. */
1721 op
[0].negate
= ~op
[0].negate
;
1722 emit(ir
, TGSI_OPCODE_CMP
, result_dst
,
1723 op
[0], result_src
, st_src_reg_for_float(0.0));
1726 emit_scalar(ir
, TGSI_OPCODE_RSQ
, result_dst
, op
[0]);
1729 if (native_integers
) {
1730 emit(ir
, TGSI_OPCODE_I2F
, result_dst
, op
[0]);
1733 /* fallthrough to next case otherwise */
1735 if (native_integers
) {
1736 emit(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], st_src_reg_for_float(1.0));
1739 /* fallthrough to next case otherwise */
1742 /* Converting between signed and unsigned integers is a no-op. */
1746 if (native_integers
) {
1747 /* Booleans are stored as integers using ~0 for true and 0 for false.
1748 * GLSL requires that int(bool) return 1 for true and 0 for false.
1749 * This conversion is done with AND, but it could be done with NEG.
1751 emit(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], st_src_reg_for_int(1));
1753 /* Booleans and integers are both stored as floats when native
1754 * integers are disabled.
1760 if (native_integers
)
1761 emit(ir
, TGSI_OPCODE_F2I
, result_dst
, op
[0]);
1763 emit(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
1766 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], st_src_reg_for_float(0.0));
1769 if (native_integers
)
1770 emit(ir
, TGSI_OPCODE_INEG
, result_dst
, op
[0]);
1772 emit(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], st_src_reg_for_float(0.0));
1775 emit(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
1778 op
[0].negate
= ~op
[0].negate
;
1779 emit(ir
, TGSI_OPCODE_FLR
, result_dst
, op
[0]);
1780 result_src
.negate
= ~result_src
.negate
;
1783 emit(ir
, TGSI_OPCODE_FLR
, result_dst
, op
[0]);
1785 case ir_unop_round_even
:
1786 emit(ir
, TGSI_OPCODE_ROUND
, result_dst
, op
[0]);
1789 emit(ir
, TGSI_OPCODE_FRC
, result_dst
, op
[0]);
1793 emit(ir
, TGSI_OPCODE_MIN
, result_dst
, op
[0], op
[1]);
1796 emit(ir
, TGSI_OPCODE_MAX
, result_dst
, op
[0], op
[1]);
1799 emit_scalar(ir
, TGSI_OPCODE_POW
, result_dst
, op
[0], op
[1]);
1802 case ir_unop_bit_not
:
1803 if (native_integers
) {
1804 emit(ir
, TGSI_OPCODE_NOT
, result_dst
, op
[0]);
1808 if (native_integers
) {
1809 emit(ir
, TGSI_OPCODE_U2F
, result_dst
, op
[0]);
1812 case ir_binop_lshift
:
1813 if (native_integers
) {
1814 emit(ir
, TGSI_OPCODE_SHL
, result_dst
, op
[0], op
[1]);
1817 case ir_binop_rshift
:
1818 if (native_integers
) {
1819 emit(ir
, TGSI_OPCODE_ISHR
, result_dst
, op
[0], op
[1]);
1822 case ir_binop_bit_and
:
1823 if (native_integers
) {
1824 emit(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], op
[1]);
1827 case ir_binop_bit_xor
:
1828 if (native_integers
) {
1829 emit(ir
, TGSI_OPCODE_XOR
, result_dst
, op
[0], op
[1]);
1832 case ir_binop_bit_or
:
1833 if (native_integers
) {
1834 emit(ir
, TGSI_OPCODE_OR
, result_dst
, op
[0], op
[1]);
1838 assert(!"GLSL 1.30 features unsupported");
1841 case ir_quadop_vector
:
1842 /* This operation should have already been handled.
1844 assert(!"Should not get here.");
1848 this->result
= result_src
;
1853 glsl_to_tgsi_visitor::visit(ir_swizzle
*ir
)
1859 /* Note that this is only swizzles in expressions, not those on the left
1860 * hand side of an assignment, which do write masking. See ir_assignment
1864 ir
->val
->accept(this);
1866 assert(src
.file
!= PROGRAM_UNDEFINED
);
1868 for (i
= 0; i
< 4; i
++) {
1869 if (i
< ir
->type
->vector_elements
) {
1872 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.x
);
1875 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.y
);
1878 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.z
);
1881 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.w
);
1885 /* If the type is smaller than a vec4, replicate the last
1888 swizzle
[i
] = swizzle
[ir
->type
->vector_elements
- 1];
1892 src
.swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
1898 glsl_to_tgsi_visitor::visit(ir_dereference_variable
*ir
)
1900 variable_storage
*entry
= find_variable_storage(ir
->var
);
1901 ir_variable
*var
= ir
->var
;
1904 switch (var
->mode
) {
1905 case ir_var_uniform
:
1906 entry
= new(mem_ctx
) variable_storage(var
, PROGRAM_UNIFORM
,
1908 this->variables
.push_tail(entry
);
1912 /* The linker assigns locations for varyings and attributes,
1913 * including deprecated builtins (like gl_Color), user-assign
1914 * generic attributes (glBindVertexLocation), and
1915 * user-defined varyings.
1917 * FINISHME: We would hit this path for function arguments. Fix!
1919 assert(var
->location
!= -1);
1920 entry
= new(mem_ctx
) variable_storage(var
,
1925 assert(var
->location
!= -1);
1926 entry
= new(mem_ctx
) variable_storage(var
,
1930 case ir_var_system_value
:
1931 entry
= new(mem_ctx
) variable_storage(var
,
1932 PROGRAM_SYSTEM_VALUE
,
1936 case ir_var_temporary
:
1937 entry
= new(mem_ctx
) variable_storage(var
, PROGRAM_TEMPORARY
,
1939 this->variables
.push_tail(entry
);
1941 next_temp
+= type_size(var
->type
);
1946 printf("Failed to make storage for %s\n", var
->name
);
1951 this->result
= st_src_reg(entry
->file
, entry
->index
, var
->type
);
1952 if (!native_integers
)
1953 this->result
.type
= GLSL_TYPE_FLOAT
;
1957 glsl_to_tgsi_visitor::visit(ir_dereference_array
*ir
)
1961 int element_size
= type_size(ir
->type
);
1963 index
= ir
->array_index
->constant_expression_value();
1965 ir
->array
->accept(this);
1969 src
.index
+= index
->value
.i
[0] * element_size
;
1971 /* Variable index array dereference. It eats the "vec4" of the
1972 * base of the array and an index that offsets the TGSI register
1975 ir
->array_index
->accept(this);
1977 st_src_reg index_reg
;
1979 if (element_size
== 1) {
1980 index_reg
= this->result
;
1982 index_reg
= get_temp(native_integers
?
1983 glsl_type::int_type
: glsl_type::float_type
);
1985 emit(ir
, TGSI_OPCODE_MUL
, st_dst_reg(index_reg
),
1986 this->result
, st_src_reg_for_type(index_reg
.type
, element_size
));
1989 /* If there was already a relative address register involved, add the
1990 * new and the old together to get the new offset.
1992 if (src
.reladdr
!= NULL
) {
1993 st_src_reg accum_reg
= get_temp(native_integers
?
1994 glsl_type::int_type
: glsl_type::float_type
);
1996 emit(ir
, TGSI_OPCODE_ADD
, st_dst_reg(accum_reg
),
1997 index_reg
, *src
.reladdr
);
1999 index_reg
= accum_reg
;
2002 src
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
2003 memcpy(src
.reladdr
, &index_reg
, sizeof(index_reg
));
2006 /* If the type is smaller than a vec4, replicate the last channel out. */
2007 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
2008 src
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
2010 src
.swizzle
= SWIZZLE_NOOP
;
2016 glsl_to_tgsi_visitor::visit(ir_dereference_record
*ir
)
2019 const glsl_type
*struct_type
= ir
->record
->type
;
2022 ir
->record
->accept(this);
2024 for (i
= 0; i
< struct_type
->length
; i
++) {
2025 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
2027 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
2030 /* If the type is smaller than a vec4, replicate the last channel out. */
2031 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
2032 this->result
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
2034 this->result
.swizzle
= SWIZZLE_NOOP
;
2036 this->result
.index
+= offset
;
2040 * We want to be careful in assignment setup to hit the actual storage
2041 * instead of potentially using a temporary like we might with the
2042 * ir_dereference handler.
2045 get_assignment_lhs(ir_dereference
*ir
, glsl_to_tgsi_visitor
*v
)
2047 /* The LHS must be a dereference. If the LHS is a variable indexed array
2048 * access of a vector, it must be separated into a series conditional moves
2049 * before reaching this point (see ir_vec_index_to_cond_assign).
2051 assert(ir
->as_dereference());
2052 ir_dereference_array
*deref_array
= ir
->as_dereference_array();
2054 assert(!deref_array
->array
->type
->is_vector());
2057 /* Use the rvalue deref handler for the most part. We'll ignore
2058 * swizzles in it and write swizzles using writemask, though.
2061 return st_dst_reg(v
->result
);
2065 * Process the condition of a conditional assignment
2067 * Examines the condition of a conditional assignment to generate the optimal
2068 * first operand of a \c CMP instruction. If the condition is a relational
2069 * operator with 0 (e.g., \c ir_binop_less), the value being compared will be
2070 * used as the source for the \c CMP instruction. Otherwise the comparison
2071 * is processed to a boolean result, and the boolean result is used as the
2072 * operand to the CMP instruction.
2075 glsl_to_tgsi_visitor::process_move_condition(ir_rvalue
*ir
)
2077 ir_rvalue
*src_ir
= ir
;
2079 bool switch_order
= false;
2081 ir_expression
*const expr
= ir
->as_expression();
2082 if ((expr
!= NULL
) && (expr
->get_num_operands() == 2)) {
2083 bool zero_on_left
= false;
2085 if (expr
->operands
[0]->is_zero()) {
2086 src_ir
= expr
->operands
[1];
2087 zero_on_left
= true;
2088 } else if (expr
->operands
[1]->is_zero()) {
2089 src_ir
= expr
->operands
[0];
2090 zero_on_left
= false;
2094 * (a < 0) T F F ( a < 0) T F F
2095 * (0 < a) F F T (-a < 0) F F T
2096 * (a <= 0) T T F (-a < 0) F F T (swap order of other operands)
2097 * (0 <= a) F T T ( a < 0) T F F (swap order of other operands)
2098 * (a > 0) F F T (-a < 0) F F T
2099 * (0 > a) T F F ( a < 0) T F F
2100 * (a >= 0) F T T ( a < 0) T F F (swap order of other operands)
2101 * (0 >= a) T T F (-a < 0) F F T (swap order of other operands)
2103 * Note that exchanging the order of 0 and 'a' in the comparison simply
2104 * means that the value of 'a' should be negated.
2107 switch (expr
->operation
) {
2109 switch_order
= false;
2110 negate
= zero_on_left
;
2113 case ir_binop_greater
:
2114 switch_order
= false;
2115 negate
= !zero_on_left
;
2118 case ir_binop_lequal
:
2119 switch_order
= true;
2120 negate
= !zero_on_left
;
2123 case ir_binop_gequal
:
2124 switch_order
= true;
2125 negate
= zero_on_left
;
2129 /* This isn't the right kind of comparison afterall, so make sure
2130 * the whole condition is visited.
2138 src_ir
->accept(this);
2140 /* We use the TGSI_OPCODE_CMP (a < 0 ? b : c) for conditional moves, and the
2141 * condition we produced is 0.0 or 1.0. By flipping the sign, we can
2142 * choose which value TGSI_OPCODE_CMP produces without an extra instruction
2143 * computing the condition.
2146 this->result
.negate
= ~this->result
.negate
;
2148 return switch_order
;
2152 glsl_to_tgsi_visitor::visit(ir_assignment
*ir
)
2158 ir
->rhs
->accept(this);
2161 l
= get_assignment_lhs(ir
->lhs
, this);
2163 /* FINISHME: This should really set to the correct maximal writemask for each
2164 * FINISHME: component written (in the loops below). This case can only
2165 * FINISHME: occur for matrices, arrays, and structures.
2167 if (ir
->write_mask
== 0) {
2168 assert(!ir
->lhs
->type
->is_scalar() && !ir
->lhs
->type
->is_vector());
2169 l
.writemask
= WRITEMASK_XYZW
;
2170 } else if (ir
->lhs
->type
->is_scalar() &&
2171 ir
->lhs
->variable_referenced()->mode
== ir_var_out
) {
2172 /* FINISHME: This hack makes writing to gl_FragDepth, which lives in the
2173 * FINISHME: W component of fragment shader output zero, work correctly.
2175 l
.writemask
= WRITEMASK_XYZW
;
2178 int first_enabled_chan
= 0;
2181 l
.writemask
= ir
->write_mask
;
2183 for (int i
= 0; i
< 4; i
++) {
2184 if (l
.writemask
& (1 << i
)) {
2185 first_enabled_chan
= GET_SWZ(r
.swizzle
, i
);
2190 /* Swizzle a small RHS vector into the channels being written.
2192 * glsl ir treats write_mask as dictating how many channels are
2193 * present on the RHS while TGSI treats write_mask as just
2194 * showing which channels of the vec4 RHS get written.
2196 for (int i
= 0; i
< 4; i
++) {
2197 if (l
.writemask
& (1 << i
))
2198 swizzles
[i
] = GET_SWZ(r
.swizzle
, rhs_chan
++);
2200 swizzles
[i
] = first_enabled_chan
;
2202 r
.swizzle
= MAKE_SWIZZLE4(swizzles
[0], swizzles
[1],
2203 swizzles
[2], swizzles
[3]);
2206 assert(l
.file
!= PROGRAM_UNDEFINED
);
2207 assert(r
.file
!= PROGRAM_UNDEFINED
);
2209 if (ir
->condition
) {
2210 const bool switch_order
= this->process_move_condition(ir
->condition
);
2211 st_src_reg condition
= this->result
;
2213 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
2214 st_src_reg l_src
= st_src_reg(l
);
2215 st_src_reg condition_temp
= condition
;
2216 l_src
.swizzle
= swizzle_for_size(ir
->lhs
->type
->vector_elements
);
2218 if (native_integers
) {
2219 /* This is necessary because TGSI's CMP instruction expects the
2220 * condition to be a float, and we store booleans as integers.
2221 * If TGSI had a UCMP instruction or similar, this extra
2222 * instruction would not be necessary.
2224 condition_temp
= get_temp(glsl_type::vec4_type
);
2225 condition
.negate
= 0;
2226 emit(ir
, TGSI_OPCODE_I2F
, st_dst_reg(condition_temp
), condition
);
2227 condition_temp
.swizzle
= condition
.swizzle
;
2231 emit(ir
, TGSI_OPCODE_CMP
, l
, condition_temp
, l_src
, r
);
2233 emit(ir
, TGSI_OPCODE_CMP
, l
, condition_temp
, r
, l_src
);
2239 } else if (ir
->rhs
->as_expression() &&
2240 this->instructions
.get_tail() &&
2241 ir
->rhs
== ((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->ir
&&
2242 type_size(ir
->lhs
->type
) == 1 &&
2243 l
.writemask
== ((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->dst
.writemask
) {
2244 /* To avoid emitting an extra MOV when assigning an expression to a
2245 * variable, emit the last instruction of the expression again, but
2246 * replace the destination register with the target of the assignment.
2247 * Dead code elimination will remove the original instruction.
2249 glsl_to_tgsi_instruction
*inst
, *new_inst
;
2250 inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
2251 new_inst
= emit(ir
, inst
->op
, l
, inst
->src
[0], inst
->src
[1], inst
->src
[2]);
2252 new_inst
->saturate
= inst
->saturate
;
2253 inst
->dead_mask
= inst
->dst
.writemask
;
2255 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
2256 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
2265 glsl_to_tgsi_visitor::visit(ir_constant
*ir
)
2268 GLfloat stack_vals
[4] = { 0 };
2269 gl_constant_value
*values
= (gl_constant_value
*) stack_vals
;
2270 GLenum gl_type
= GL_NONE
;
2272 static int in_array
= 0;
2273 gl_register_file file
= in_array
? PROGRAM_CONSTANT
: PROGRAM_IMMEDIATE
;
2275 /* Unfortunately, 4 floats is all we can get into
2276 * _mesa_add_typed_unnamed_constant. So, make a temp to store an
2277 * aggregate constant and move each constant value into it. If we
2278 * get lucky, copy propagation will eliminate the extra moves.
2280 if (ir
->type
->base_type
== GLSL_TYPE_STRUCT
) {
2281 st_src_reg temp_base
= get_temp(ir
->type
);
2282 st_dst_reg temp
= st_dst_reg(temp_base
);
2284 foreach_iter(exec_list_iterator
, iter
, ir
->components
) {
2285 ir_constant
*field_value
= (ir_constant
*)iter
.get();
2286 int size
= type_size(field_value
->type
);
2290 field_value
->accept(this);
2293 for (i
= 0; i
< (unsigned int)size
; i
++) {
2294 emit(ir
, TGSI_OPCODE_MOV
, temp
, src
);
2300 this->result
= temp_base
;
2304 if (ir
->type
->is_array()) {
2305 st_src_reg temp_base
= get_temp(ir
->type
);
2306 st_dst_reg temp
= st_dst_reg(temp_base
);
2307 int size
= type_size(ir
->type
->fields
.array
);
2312 for (i
= 0; i
< ir
->type
->length
; i
++) {
2313 ir
->array_elements
[i
]->accept(this);
2315 for (int j
= 0; j
< size
; j
++) {
2316 emit(ir
, TGSI_OPCODE_MOV
, temp
, src
);
2322 this->result
= temp_base
;
2327 if (ir
->type
->is_matrix()) {
2328 st_src_reg mat
= get_temp(ir
->type
);
2329 st_dst_reg mat_column
= st_dst_reg(mat
);
2331 for (i
= 0; i
< ir
->type
->matrix_columns
; i
++) {
2332 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
2333 values
= (gl_constant_value
*) &ir
->value
.f
[i
* ir
->type
->vector_elements
];
2335 src
= st_src_reg(file
, -1, ir
->type
->base_type
);
2336 src
.index
= add_constant(file
,
2338 ir
->type
->vector_elements
,
2341 emit(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
2350 switch (ir
->type
->base_type
) {
2351 case GLSL_TYPE_FLOAT
:
2353 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2354 values
[i
].f
= ir
->value
.f
[i
];
2357 case GLSL_TYPE_UINT
:
2358 gl_type
= native_integers
? GL_UNSIGNED_INT
: GL_FLOAT
;
2359 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2360 if (native_integers
)
2361 values
[i
].u
= ir
->value
.u
[i
];
2363 values
[i
].f
= ir
->value
.u
[i
];
2367 gl_type
= native_integers
? GL_INT
: GL_FLOAT
;
2368 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2369 if (native_integers
)
2370 values
[i
].i
= ir
->value
.i
[i
];
2372 values
[i
].f
= ir
->value
.i
[i
];
2375 case GLSL_TYPE_BOOL
:
2376 gl_type
= native_integers
? GL_BOOL
: GL_FLOAT
;
2377 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
2378 if (native_integers
)
2379 values
[i
].u
= ir
->value
.b
[i
] ? ~0 : 0;
2381 values
[i
].f
= ir
->value
.b
[i
];
2385 assert(!"Non-float/uint/int/bool constant");
2388 this->result
= st_src_reg(file
, -1, ir
->type
);
2389 this->result
.index
= add_constant(file
,
2391 ir
->type
->vector_elements
,
2393 &this->result
.swizzle
);
2397 glsl_to_tgsi_visitor::get_function_signature(ir_function_signature
*sig
)
2399 function_entry
*entry
;
2401 foreach_iter(exec_list_iterator
, iter
, this->function_signatures
) {
2402 entry
= (function_entry
*)iter
.get();
2404 if (entry
->sig
== sig
)
2408 entry
= ralloc(mem_ctx
, function_entry
);
2410 entry
->sig_id
= this->next_signature_id
++;
2411 entry
->bgn_inst
= NULL
;
2413 /* Allocate storage for all the parameters. */
2414 foreach_iter(exec_list_iterator
, iter
, sig
->parameters
) {
2415 ir_variable
*param
= (ir_variable
*)iter
.get();
2416 variable_storage
*storage
;
2418 storage
= find_variable_storage(param
);
2421 storage
= new(mem_ctx
) variable_storage(param
, PROGRAM_TEMPORARY
,
2423 this->variables
.push_tail(storage
);
2425 this->next_temp
+= type_size(param
->type
);
2428 if (!sig
->return_type
->is_void()) {
2429 entry
->return_reg
= get_temp(sig
->return_type
);
2431 entry
->return_reg
= undef_src
;
2434 this->function_signatures
.push_tail(entry
);
2439 glsl_to_tgsi_visitor::visit(ir_call
*ir
)
2441 glsl_to_tgsi_instruction
*call_inst
;
2442 ir_function_signature
*sig
= ir
->get_callee();
2443 function_entry
*entry
= get_function_signature(sig
);
2446 /* Process in parameters. */
2447 exec_list_iterator sig_iter
= sig
->parameters
.iterator();
2448 foreach_iter(exec_list_iterator
, iter
, *ir
) {
2449 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
2450 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
2452 if (param
->mode
== ir_var_in
||
2453 param
->mode
== ir_var_inout
) {
2454 variable_storage
*storage
= find_variable_storage(param
);
2457 param_rval
->accept(this);
2458 st_src_reg r
= this->result
;
2461 l
.file
= storage
->file
;
2462 l
.index
= storage
->index
;
2464 l
.writemask
= WRITEMASK_XYZW
;
2465 l
.cond_mask
= COND_TR
;
2467 for (i
= 0; i
< type_size(param
->type
); i
++) {
2468 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
2476 assert(!sig_iter
.has_next());
2478 /* Emit call instruction */
2479 call_inst
= emit(ir
, TGSI_OPCODE_CAL
);
2480 call_inst
->function
= entry
;
2482 /* Process out parameters. */
2483 sig_iter
= sig
->parameters
.iterator();
2484 foreach_iter(exec_list_iterator
, iter
, *ir
) {
2485 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
2486 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
2488 if (param
->mode
== ir_var_out
||
2489 param
->mode
== ir_var_inout
) {
2490 variable_storage
*storage
= find_variable_storage(param
);
2494 r
.file
= storage
->file
;
2495 r
.index
= storage
->index
;
2497 r
.swizzle
= SWIZZLE_NOOP
;
2500 param_rval
->accept(this);
2501 st_dst_reg l
= st_dst_reg(this->result
);
2503 for (i
= 0; i
< type_size(param
->type
); i
++) {
2504 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
2512 assert(!sig_iter
.has_next());
2514 /* Process return value. */
2515 this->result
= entry
->return_reg
;
2519 glsl_to_tgsi_visitor::visit(ir_texture
*ir
)
2521 st_src_reg result_src
, coord
, lod_info
, projector
, dx
, dy
, offset
;
2522 st_dst_reg result_dst
, coord_dst
;
2523 glsl_to_tgsi_instruction
*inst
= NULL
;
2524 unsigned opcode
= TGSI_OPCODE_NOP
;
2526 if (ir
->coordinate
) {
2527 ir
->coordinate
->accept(this);
2529 /* Put our coords in a temp. We'll need to modify them for shadow,
2530 * projection, or LOD, so the only case we'd use it as is is if
2531 * we're doing plain old texturing. The optimization passes on
2532 * glsl_to_tgsi_visitor should handle cleaning up our mess in that case.
2534 coord
= get_temp(glsl_type::vec4_type
);
2535 coord_dst
= st_dst_reg(coord
);
2536 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
2539 if (ir
->projector
) {
2540 ir
->projector
->accept(this);
2541 projector
= this->result
;
2544 /* Storage for our result. Ideally for an assignment we'd be using
2545 * the actual storage for the result here, instead.
2547 result_src
= get_temp(glsl_type::vec4_type
);
2548 result_dst
= st_dst_reg(result_src
);
2552 opcode
= TGSI_OPCODE_TEX
;
2555 opcode
= TGSI_OPCODE_TXB
;
2556 ir
->lod_info
.bias
->accept(this);
2557 lod_info
= this->result
;
2560 opcode
= TGSI_OPCODE_TXL
;
2561 ir
->lod_info
.lod
->accept(this);
2562 lod_info
= this->result
;
2565 opcode
= TGSI_OPCODE_TXD
;
2566 ir
->lod_info
.grad
.dPdx
->accept(this);
2568 ir
->lod_info
.grad
.dPdy
->accept(this);
2572 opcode
= TGSI_OPCODE_TXQ
;
2573 ir
->lod_info
.lod
->accept(this);
2574 lod_info
= this->result
;
2577 opcode
= TGSI_OPCODE_TXF
;
2578 ir
->lod_info
.lod
->accept(this);
2579 lod_info
= this->result
;
2581 ir
->offset
->accept(this);
2582 offset
= this->result
;
2587 const glsl_type
*sampler_type
= ir
->sampler
->type
;
2589 if (ir
->projector
) {
2590 if (opcode
== TGSI_OPCODE_TEX
) {
2591 /* Slot the projector in as the last component of the coord. */
2592 coord_dst
.writemask
= WRITEMASK_W
;
2593 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, projector
);
2594 coord_dst
.writemask
= WRITEMASK_XYZW
;
2595 opcode
= TGSI_OPCODE_TXP
;
2597 st_src_reg coord_w
= coord
;
2598 coord_w
.swizzle
= SWIZZLE_WWWW
;
2600 /* For the other TEX opcodes there's no projective version
2601 * since the last slot is taken up by LOD info. Do the
2602 * projective divide now.
2604 coord_dst
.writemask
= WRITEMASK_W
;
2605 emit(ir
, TGSI_OPCODE_RCP
, coord_dst
, projector
);
2607 /* In the case where we have to project the coordinates "by hand,"
2608 * the shadow comparator value must also be projected.
2610 st_src_reg tmp_src
= coord
;
2611 if (ir
->shadow_comparitor
) {
2612 /* Slot the shadow value in as the second to last component of the
2615 ir
->shadow_comparitor
->accept(this);
2617 tmp_src
= get_temp(glsl_type::vec4_type
);
2618 st_dst_reg tmp_dst
= st_dst_reg(tmp_src
);
2620 /* Projective division not allowed for array samplers. */
2621 assert(!sampler_type
->sampler_array
);
2623 tmp_dst
.writemask
= WRITEMASK_Z
;
2624 emit(ir
, TGSI_OPCODE_MOV
, tmp_dst
, this->result
);
2626 tmp_dst
.writemask
= WRITEMASK_XY
;
2627 emit(ir
, TGSI_OPCODE_MOV
, tmp_dst
, coord
);
2630 coord_dst
.writemask
= WRITEMASK_XYZ
;
2631 emit(ir
, TGSI_OPCODE_MUL
, coord_dst
, tmp_src
, coord_w
);
2633 coord_dst
.writemask
= WRITEMASK_XYZW
;
2634 coord
.swizzle
= SWIZZLE_XYZW
;
2638 /* If projection is done and the opcode is not TGSI_OPCODE_TXP, then the shadow
2639 * comparator was put in the correct place (and projected) by the code,
2640 * above, that handles by-hand projection.
2642 if (ir
->shadow_comparitor
&& (!ir
->projector
|| opcode
== TGSI_OPCODE_TXP
)) {
2643 /* Slot the shadow value in as the second to last component of the
2646 ir
->shadow_comparitor
->accept(this);
2648 /* XXX This will need to be updated for cubemap array samplers. */
2649 if ((sampler_type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_2D
&&
2650 sampler_type
->sampler_array
) ||
2651 sampler_type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
) {
2652 coord_dst
.writemask
= WRITEMASK_W
;
2654 coord_dst
.writemask
= WRITEMASK_Z
;
2657 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
2658 coord_dst
.writemask
= WRITEMASK_XYZW
;
2661 if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXB
||
2662 opcode
== TGSI_OPCODE_TXF
) {
2663 /* TGSI stores LOD or LOD bias in the last channel of the coords. */
2664 coord_dst
.writemask
= WRITEMASK_W
;
2665 emit(ir
, TGSI_OPCODE_MOV
, coord_dst
, lod_info
);
2666 coord_dst
.writemask
= WRITEMASK_XYZW
;
2669 if (opcode
== TGSI_OPCODE_TXD
)
2670 inst
= emit(ir
, opcode
, result_dst
, coord
, dx
, dy
);
2671 else if (opcode
== TGSI_OPCODE_TXQ
)
2672 inst
= emit(ir
, opcode
, result_dst
, lod_info
);
2673 else if (opcode
== TGSI_OPCODE_TXF
) {
2674 inst
= emit(ir
, opcode
, result_dst
, coord
);
2676 inst
= emit(ir
, opcode
, result_dst
, coord
);
2678 if (ir
->shadow_comparitor
)
2679 inst
->tex_shadow
= GL_TRUE
;
2681 inst
->sampler
= _mesa_get_sampler_uniform_value(ir
->sampler
,
2682 this->shader_program
,
2686 inst
->tex_offset_num_offset
= 1;
2687 inst
->tex_offsets
[0].Index
= offset
.index
;
2688 inst
->tex_offsets
[0].File
= offset
.file
;
2689 inst
->tex_offsets
[0].SwizzleX
= GET_SWZ(offset
.swizzle
, 0);
2690 inst
->tex_offsets
[0].SwizzleY
= GET_SWZ(offset
.swizzle
, 1);
2691 inst
->tex_offsets
[0].SwizzleZ
= GET_SWZ(offset
.swizzle
, 2);
2694 switch (sampler_type
->sampler_dimensionality
) {
2695 case GLSL_SAMPLER_DIM_1D
:
2696 inst
->tex_target
= (sampler_type
->sampler_array
)
2697 ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
2699 case GLSL_SAMPLER_DIM_2D
:
2700 inst
->tex_target
= (sampler_type
->sampler_array
)
2701 ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
2703 case GLSL_SAMPLER_DIM_3D
:
2704 inst
->tex_target
= TEXTURE_3D_INDEX
;
2706 case GLSL_SAMPLER_DIM_CUBE
:
2707 inst
->tex_target
= TEXTURE_CUBE_INDEX
;
2709 case GLSL_SAMPLER_DIM_RECT
:
2710 inst
->tex_target
= TEXTURE_RECT_INDEX
;
2712 case GLSL_SAMPLER_DIM_BUF
:
2713 assert(!"FINISHME: Implement ARB_texture_buffer_object");
2715 case GLSL_SAMPLER_DIM_EXTERNAL
:
2716 inst
->tex_target
= TEXTURE_EXTERNAL_INDEX
;
2719 assert(!"Should not get here.");
2722 this->result
= result_src
;
2726 glsl_to_tgsi_visitor::visit(ir_return
*ir
)
2728 if (ir
->get_value()) {
2732 assert(current_function
);
2734 ir
->get_value()->accept(this);
2735 st_src_reg r
= this->result
;
2737 l
= st_dst_reg(current_function
->return_reg
);
2739 for (i
= 0; i
< type_size(current_function
->sig
->return_type
); i
++) {
2740 emit(ir
, TGSI_OPCODE_MOV
, l
, r
);
2746 emit(ir
, TGSI_OPCODE_RET
);
2750 glsl_to_tgsi_visitor::visit(ir_discard
*ir
)
2752 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
2754 if (ir
->condition
) {
2755 ir
->condition
->accept(this);
2756 this->result
.negate
= ~this->result
.negate
;
2757 emit(ir
, TGSI_OPCODE_KIL
, undef_dst
, this->result
);
2759 emit(ir
, TGSI_OPCODE_KILP
);
2762 fp
->UsesKill
= GL_TRUE
;
2766 glsl_to_tgsi_visitor::visit(ir_if
*ir
)
2768 glsl_to_tgsi_instruction
*cond_inst
, *if_inst
;
2769 glsl_to_tgsi_instruction
*prev_inst
;
2771 prev_inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
2773 ir
->condition
->accept(this);
2774 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
2776 if (this->options
->EmitCondCodes
) {
2777 cond_inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
2779 /* See if we actually generated any instruction for generating
2780 * the condition. If not, then cook up a move to a temp so we
2781 * have something to set cond_update on.
2783 if (cond_inst
== prev_inst
) {
2784 st_src_reg temp
= get_temp(glsl_type::bool_type
);
2785 cond_inst
= emit(ir
->condition
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), result
);
2787 cond_inst
->cond_update
= GL_TRUE
;
2789 if_inst
= emit(ir
->condition
, TGSI_OPCODE_IF
);
2790 if_inst
->dst
.cond_mask
= COND_NE
;
2792 if_inst
= emit(ir
->condition
, TGSI_OPCODE_IF
, undef_dst
, this->result
);
2795 this->instructions
.push_tail(if_inst
);
2797 visit_exec_list(&ir
->then_instructions
, this);
2799 if (!ir
->else_instructions
.is_empty()) {
2800 emit(ir
->condition
, TGSI_OPCODE_ELSE
);
2801 visit_exec_list(&ir
->else_instructions
, this);
2804 if_inst
= emit(ir
->condition
, TGSI_OPCODE_ENDIF
);
2807 glsl_to_tgsi_visitor::glsl_to_tgsi_visitor()
2809 result
.file
= PROGRAM_UNDEFINED
;
2811 next_signature_id
= 1;
2813 current_function
= NULL
;
2814 num_address_regs
= 0;
2815 indirect_addr_temps
= false;
2816 indirect_addr_consts
= false;
2817 mem_ctx
= ralloc_context(NULL
);
2820 glsl_to_tgsi_visitor::~glsl_to_tgsi_visitor()
2822 ralloc_free(mem_ctx
);
2825 extern "C" void free_glsl_to_tgsi_visitor(glsl_to_tgsi_visitor
*v
)
2832 * Count resources used by the given gpu program (number of texture
2836 count_resources(glsl_to_tgsi_visitor
*v
, gl_program
*prog
)
2838 v
->samplers_used
= 0;
2840 foreach_iter(exec_list_iterator
, iter
, v
->instructions
) {
2841 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
2843 if (is_tex_instruction(inst
->op
)) {
2844 v
->samplers_used
|= 1 << inst
->sampler
;
2846 if (inst
->tex_shadow
) {
2847 prog
->ShadowSamplers
|= 1 << inst
->sampler
;
2852 prog
->SamplersUsed
= v
->samplers_used
;
2854 if (v
->shader_program
!= NULL
)
2855 _mesa_update_shader_textures_used(v
->shader_program
, prog
);
2859 set_uniform_initializer(struct gl_context
*ctx
, void *mem_ctx
,
2860 struct gl_shader_program
*shader_program
,
2861 const char *name
, const glsl_type
*type
,
2864 if (type
->is_record()) {
2865 ir_constant
*field_constant
;
2867 field_constant
= (ir_constant
*)val
->components
.get_head();
2869 for (unsigned int i
= 0; i
< type
->length
; i
++) {
2870 const glsl_type
*field_type
= type
->fields
.structure
[i
].type
;
2871 const char *field_name
= ralloc_asprintf(mem_ctx
, "%s.%s", name
,
2872 type
->fields
.structure
[i
].name
);
2873 set_uniform_initializer(ctx
, mem_ctx
, shader_program
, field_name
,
2874 field_type
, field_constant
);
2875 field_constant
= (ir_constant
*)field_constant
->next
;
2880 int loc
= _mesa_get_uniform_location(ctx
, shader_program
, name
);
2883 fail_link(shader_program
,
2884 "Couldn't find uniform for initializer %s\n", name
);
2888 for (unsigned int i
= 0; i
< (type
->is_array() ? type
->length
: 1); i
++) {
2889 ir_constant
*element
;
2890 const glsl_type
*element_type
;
2891 if (type
->is_array()) {
2892 element
= val
->array_elements
[i
];
2893 element_type
= type
->fields
.array
;
2896 element_type
= type
;
2901 if (element_type
->base_type
== GLSL_TYPE_BOOL
) {
2902 int *conv
= ralloc_array(mem_ctx
, int, element_type
->components());
2903 for (unsigned int j
= 0; j
< element_type
->components(); j
++) {
2904 conv
[j
] = element
->value
.b
[j
];
2906 values
= (void *)conv
;
2907 element_type
= glsl_type::get_instance(GLSL_TYPE_INT
,
2908 element_type
->vector_elements
,
2911 values
= &element
->value
;
2914 if (element_type
->is_matrix()) {
2915 _mesa_uniform_matrix(ctx
, shader_program
,
2916 element_type
->matrix_columns
,
2917 element_type
->vector_elements
,
2918 loc
, 1, GL_FALSE
, (GLfloat
*)values
);
2920 _mesa_uniform(ctx
, shader_program
, loc
, element_type
->matrix_columns
,
2921 values
, element_type
->gl_type
);
2929 * Returns the mask of channels (bitmask of WRITEMASK_X,Y,Z,W) which
2930 * are read from the given src in this instruction
2933 get_src_arg_mask(st_dst_reg dst
, st_src_reg src
)
2935 int read_mask
= 0, comp
;
2937 /* Now, given the src swizzle and the written channels, find which
2938 * components are actually read
2940 for (comp
= 0; comp
< 4; ++comp
) {
2941 const unsigned coord
= GET_SWZ(src
.swizzle
, comp
);
2943 if (dst
.writemask
& (1 << comp
) && coord
<= SWIZZLE_W
)
2944 read_mask
|= 1 << coord
;
2951 * This pass replaces CMP T0, T1 T2 T0 with MOV T0, T2 when the CMP
2952 * instruction is the first instruction to write to register T0. There are
2953 * several lowering passes done in GLSL IR (e.g. branches and
2954 * relative addressing) that create a large number of conditional assignments
2955 * that ir_to_mesa converts to CMP instructions like the one mentioned above.
2957 * Here is why this conversion is safe:
2958 * CMP T0, T1 T2 T0 can be expanded to:
2964 * If (T1 < 0.0) evaluates to true then our replacement MOV T0, T2 is the same
2965 * as the original program. If (T1 < 0.0) evaluates to false, executing
2966 * MOV T0, T0 will store a garbage value in T0 since T0 is uninitialized.
2967 * Therefore, it doesn't matter that we are replacing MOV T0, T0 with MOV T0, T2
2968 * because any instruction that was going to read from T0 after this was going
2969 * to read a garbage value anyway.
2972 glsl_to_tgsi_visitor::simplify_cmp(void)
2974 unsigned *tempWrites
;
2975 unsigned outputWrites
[MAX_PROGRAM_OUTPUTS
];
2977 tempWrites
= new unsigned[MAX_TEMPS
];
2981 memset(tempWrites
, 0, sizeof(unsigned) * MAX_TEMPS
);
2982 memset(outputWrites
, 0, sizeof(outputWrites
));
2984 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2985 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
2986 unsigned prevWriteMask
= 0;
2988 /* Give up if we encounter relative addressing or flow control. */
2989 if (inst
->dst
.reladdr
||
2990 tgsi_get_opcode_info(inst
->op
)->is_branch
||
2991 inst
->op
== TGSI_OPCODE_BGNSUB
||
2992 inst
->op
== TGSI_OPCODE_CONT
||
2993 inst
->op
== TGSI_OPCODE_END
||
2994 inst
->op
== TGSI_OPCODE_ENDSUB
||
2995 inst
->op
== TGSI_OPCODE_RET
) {
2999 if (inst
->dst
.file
== PROGRAM_OUTPUT
) {
3000 assert(inst
->dst
.index
< MAX_PROGRAM_OUTPUTS
);
3001 prevWriteMask
= outputWrites
[inst
->dst
.index
];
3002 outputWrites
[inst
->dst
.index
] |= inst
->dst
.writemask
;
3003 } else if (inst
->dst
.file
== PROGRAM_TEMPORARY
) {
3004 assert(inst
->dst
.index
< MAX_TEMPS
);
3005 prevWriteMask
= tempWrites
[inst
->dst
.index
];
3006 tempWrites
[inst
->dst
.index
] |= inst
->dst
.writemask
;
3009 /* For a CMP to be considered a conditional write, the destination
3010 * register and source register two must be the same. */
3011 if (inst
->op
== TGSI_OPCODE_CMP
3012 && !(inst
->dst
.writemask
& prevWriteMask
)
3013 && inst
->src
[2].file
== inst
->dst
.file
3014 && inst
->src
[2].index
== inst
->dst
.index
3015 && inst
->dst
.writemask
== get_src_arg_mask(inst
->dst
, inst
->src
[2])) {
3017 inst
->op
= TGSI_OPCODE_MOV
;
3018 inst
->src
[0] = inst
->src
[1];
3022 delete [] tempWrites
;
3025 /* Replaces all references to a temporary register index with another index. */
3027 glsl_to_tgsi_visitor::rename_temp_register(int index
, int new_index
)
3029 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3030 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3033 for (j
=0; j
< num_inst_src_regs(inst
->op
); j
++) {
3034 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
&&
3035 inst
->src
[j
].index
== index
) {
3036 inst
->src
[j
].index
= new_index
;
3040 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== index
) {
3041 inst
->dst
.index
= new_index
;
3047 glsl_to_tgsi_visitor::get_first_temp_read(int index
)
3049 int depth
= 0; /* loop depth */
3050 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
3053 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3054 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3056 for (j
=0; j
< num_inst_src_regs(inst
->op
); j
++) {
3057 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
&&
3058 inst
->src
[j
].index
== index
) {
3059 return (depth
== 0) ? i
: loop_start
;
3063 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
3066 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
3079 glsl_to_tgsi_visitor::get_first_temp_write(int index
)
3081 int depth
= 0; /* loop depth */
3082 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
3085 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3086 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3088 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== index
) {
3089 return (depth
== 0) ? i
: loop_start
;
3092 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
3095 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
3108 glsl_to_tgsi_visitor::get_last_temp_read(int index
)
3110 int depth
= 0; /* loop depth */
3111 int last
= -1; /* index of last instruction that reads the temporary */
3114 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3115 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3117 for (j
=0; j
< num_inst_src_regs(inst
->op
); j
++) {
3118 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
&&
3119 inst
->src
[j
].index
== index
) {
3120 last
= (depth
== 0) ? i
: -2;
3124 if (inst
->op
== TGSI_OPCODE_BGNLOOP
)
3126 else if (inst
->op
== TGSI_OPCODE_ENDLOOP
)
3127 if (--depth
== 0 && last
== -2)
3139 glsl_to_tgsi_visitor::get_last_temp_write(int index
)
3141 int depth
= 0; /* loop depth */
3142 int last
= -1; /* index of last instruction that writes to the temporary */
3145 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3146 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3148 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== index
)
3149 last
= (depth
== 0) ? i
: -2;
3151 if (inst
->op
== TGSI_OPCODE_BGNLOOP
)
3153 else if (inst
->op
== TGSI_OPCODE_ENDLOOP
)
3154 if (--depth
== 0 && last
== -2)
3166 * On a basic block basis, tracks available PROGRAM_TEMPORARY register
3167 * channels for copy propagation and updates following instructions to
3168 * use the original versions.
3170 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
3171 * will occur. As an example, a TXP production before this pass:
3173 * 0: MOV TEMP[1], INPUT[4].xyyy;
3174 * 1: MOV TEMP[1].w, INPUT[4].wwww;
3175 * 2: TXP TEMP[2], TEMP[1], texture[0], 2D;
3179 * 0: MOV TEMP[1], INPUT[4].xyyy;
3180 * 1: MOV TEMP[1].w, INPUT[4].wwww;
3181 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
3183 * which allows for dead code elimination on TEMP[1]'s writes.
3186 glsl_to_tgsi_visitor::copy_propagate(void)
3188 glsl_to_tgsi_instruction
**acp
= rzalloc_array(mem_ctx
,
3189 glsl_to_tgsi_instruction
*,
3190 this->next_temp
* 4);
3191 int *acp_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
3194 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3195 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3197 assert(inst
->dst
.file
!= PROGRAM_TEMPORARY
3198 || inst
->dst
.index
< this->next_temp
);
3200 /* First, do any copy propagation possible into the src regs. */
3201 for (int r
= 0; r
< 3; r
++) {
3202 glsl_to_tgsi_instruction
*first
= NULL
;
3204 int acp_base
= inst
->src
[r
].index
* 4;
3206 if (inst
->src
[r
].file
!= PROGRAM_TEMPORARY
||
3207 inst
->src
[r
].reladdr
)
3210 /* See if we can find entries in the ACP consisting of MOVs
3211 * from the same src register for all the swizzled channels
3212 * of this src register reference.
3214 for (int i
= 0; i
< 4; i
++) {
3215 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
3216 glsl_to_tgsi_instruction
*copy_chan
= acp
[acp_base
+ src_chan
];
3223 assert(acp_level
[acp_base
+ src_chan
] <= level
);
3228 if (first
->src
[0].file
!= copy_chan
->src
[0].file
||
3229 first
->src
[0].index
!= copy_chan
->src
[0].index
) {
3237 /* We've now validated that we can copy-propagate to
3238 * replace this src register reference. Do it.
3240 inst
->src
[r
].file
= first
->src
[0].file
;
3241 inst
->src
[r
].index
= first
->src
[0].index
;
3244 for (int i
= 0; i
< 4; i
++) {
3245 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
3246 glsl_to_tgsi_instruction
*copy_inst
= acp
[acp_base
+ src_chan
];
3247 swizzle
|= (GET_SWZ(copy_inst
->src
[0].swizzle
, src_chan
) <<
3250 inst
->src
[r
].swizzle
= swizzle
;
3255 case TGSI_OPCODE_BGNLOOP
:
3256 case TGSI_OPCODE_ENDLOOP
:
3257 /* End of a basic block, clear the ACP entirely. */
3258 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
3261 case TGSI_OPCODE_IF
:
3265 case TGSI_OPCODE_ENDIF
:
3266 case TGSI_OPCODE_ELSE
:
3267 /* Clear all channels written inside the block from the ACP, but
3268 * leaving those that were not touched.
3270 for (int r
= 0; r
< this->next_temp
; r
++) {
3271 for (int c
= 0; c
< 4; c
++) {
3272 if (!acp
[4 * r
+ c
])
3275 if (acp_level
[4 * r
+ c
] >= level
)
3276 acp
[4 * r
+ c
] = NULL
;
3279 if (inst
->op
== TGSI_OPCODE_ENDIF
)
3284 /* Continuing the block, clear any written channels from
3287 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.reladdr
) {
3288 /* Any temporary might be written, so no copy propagation
3289 * across this instruction.
3291 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
3292 } else if (inst
->dst
.file
== PROGRAM_OUTPUT
&&
3293 inst
->dst
.reladdr
) {
3294 /* Any output might be written, so no copy propagation
3295 * from outputs across this instruction.
3297 for (int r
= 0; r
< this->next_temp
; r
++) {
3298 for (int c
= 0; c
< 4; c
++) {
3299 if (!acp
[4 * r
+ c
])
3302 if (acp
[4 * r
+ c
]->src
[0].file
== PROGRAM_OUTPUT
)
3303 acp
[4 * r
+ c
] = NULL
;
3306 } else if (inst
->dst
.file
== PROGRAM_TEMPORARY
||
3307 inst
->dst
.file
== PROGRAM_OUTPUT
) {
3308 /* Clear where it's used as dst. */
3309 if (inst
->dst
.file
== PROGRAM_TEMPORARY
) {
3310 for (int c
= 0; c
< 4; c
++) {
3311 if (inst
->dst
.writemask
& (1 << c
)) {
3312 acp
[4 * inst
->dst
.index
+ c
] = NULL
;
3317 /* Clear where it's used as src. */
3318 for (int r
= 0; r
< this->next_temp
; r
++) {
3319 for (int c
= 0; c
< 4; c
++) {
3320 if (!acp
[4 * r
+ c
])
3323 int src_chan
= GET_SWZ(acp
[4 * r
+ c
]->src
[0].swizzle
, c
);
3325 if (acp
[4 * r
+ c
]->src
[0].file
== inst
->dst
.file
&&
3326 acp
[4 * r
+ c
]->src
[0].index
== inst
->dst
.index
&&
3327 inst
->dst
.writemask
& (1 << src_chan
))
3329 acp
[4 * r
+ c
] = NULL
;
3337 /* If this is a copy, add it to the ACP. */
3338 if (inst
->op
== TGSI_OPCODE_MOV
&&
3339 inst
->dst
.file
== PROGRAM_TEMPORARY
&&
3340 !inst
->dst
.reladdr
&&
3342 !inst
->src
[0].reladdr
&&
3343 !inst
->src
[0].negate
) {
3344 for (int i
= 0; i
< 4; i
++) {
3345 if (inst
->dst
.writemask
& (1 << i
)) {
3346 acp
[4 * inst
->dst
.index
+ i
] = inst
;
3347 acp_level
[4 * inst
->dst
.index
+ i
] = level
;
3353 ralloc_free(acp_level
);
3358 * Tracks available PROGRAM_TEMPORARY registers for dead code elimination.
3360 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
3361 * will occur. As an example, a TXP production after copy propagation but
3364 * 0: MOV TEMP[1], INPUT[4].xyyy;
3365 * 1: MOV TEMP[1].w, INPUT[4].wwww;
3366 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
3368 * and after this pass:
3370 * 0: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
3372 * FIXME: assumes that all functions are inlined (no support for BGNSUB/ENDSUB)
3373 * FIXME: doesn't eliminate all dead code inside of loops; it steps around them
3376 glsl_to_tgsi_visitor::eliminate_dead_code(void)
3380 for (i
=0; i
< this->next_temp
; i
++) {
3381 int last_read
= get_last_temp_read(i
);
3384 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3385 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3387 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&& inst
->dst
.index
== i
&&
3400 * On a basic block basis, tracks available PROGRAM_TEMPORARY registers for dead
3401 * code elimination. This is less primitive than eliminate_dead_code(), as it
3402 * is per-channel and can detect consecutive writes without a read between them
3403 * as dead code. However, there is some dead code that can be eliminated by
3404 * eliminate_dead_code() but not this function - for example, this function
3405 * cannot eliminate an instruction writing to a register that is never read and
3406 * is the only instruction writing to that register.
3408 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
3412 glsl_to_tgsi_visitor::eliminate_dead_code_advanced(void)
3414 glsl_to_tgsi_instruction
**writes
= rzalloc_array(mem_ctx
,
3415 glsl_to_tgsi_instruction
*,
3416 this->next_temp
* 4);
3417 int *write_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
3421 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3422 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3424 assert(inst
->dst
.file
!= PROGRAM_TEMPORARY
3425 || inst
->dst
.index
< this->next_temp
);
3428 case TGSI_OPCODE_BGNLOOP
:
3429 case TGSI_OPCODE_ENDLOOP
:
3430 case TGSI_OPCODE_CONT
:
3431 case TGSI_OPCODE_BRK
:
3432 /* End of a basic block, clear the write array entirely.
3434 * This keeps us from killing dead code when the writes are
3435 * on either side of a loop, even when the register isn't touched
3436 * inside the loop. However, glsl_to_tgsi_visitor doesn't seem to emit
3437 * dead code of this type, so it shouldn't make a difference as long as
3438 * the dead code elimination pass in the GLSL compiler does its job.
3440 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
3443 case TGSI_OPCODE_ENDIF
:
3444 case TGSI_OPCODE_ELSE
:
3445 /* Promote the recorded level of all channels written inside the
3446 * preceding if or else block to the level above the if/else block.
3448 for (int r
= 0; r
< this->next_temp
; r
++) {
3449 for (int c
= 0; c
< 4; c
++) {
3450 if (!writes
[4 * r
+ c
])
3453 if (write_level
[4 * r
+ c
] == level
)
3454 write_level
[4 * r
+ c
] = level
-1;
3458 if(inst
->op
== TGSI_OPCODE_ENDIF
)
3463 case TGSI_OPCODE_IF
:
3465 /* fallthrough to default case to mark the condition as read */
3468 /* Continuing the block, clear any channels from the write array that
3469 * are read by this instruction.
3471 for (unsigned i
= 0; i
< Elements(inst
->src
); i
++) {
3472 if (inst
->src
[i
].file
== PROGRAM_TEMPORARY
&& inst
->src
[i
].reladdr
){
3473 /* Any temporary might be read, so no dead code elimination
3474 * across this instruction.
3476 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
3477 } else if (inst
->src
[i
].file
== PROGRAM_TEMPORARY
) {
3478 /* Clear where it's used as src. */
3479 int src_chans
= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 0);
3480 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 1);
3481 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 2);
3482 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 3);
3484 for (int c
= 0; c
< 4; c
++) {
3485 if (src_chans
& (1 << c
)) {
3486 writes
[4 * inst
->src
[i
].index
+ c
] = NULL
;
3494 /* If this instruction writes to a temporary, add it to the write array.
3495 * If there is already an instruction in the write array for one or more
3496 * of the channels, flag that channel write as dead.
3498 if (inst
->dst
.file
== PROGRAM_TEMPORARY
&&
3499 !inst
->dst
.reladdr
&&
3501 for (int c
= 0; c
< 4; c
++) {
3502 if (inst
->dst
.writemask
& (1 << c
)) {
3503 if (writes
[4 * inst
->dst
.index
+ c
]) {
3504 if (write_level
[4 * inst
->dst
.index
+ c
] < level
)
3507 writes
[4 * inst
->dst
.index
+ c
]->dead_mask
|= (1 << c
);
3509 writes
[4 * inst
->dst
.index
+ c
] = inst
;
3510 write_level
[4 * inst
->dst
.index
+ c
] = level
;
3516 /* Anything still in the write array at this point is dead code. */
3517 for (int r
= 0; r
< this->next_temp
; r
++) {
3518 for (int c
= 0; c
< 4; c
++) {
3519 glsl_to_tgsi_instruction
*inst
= writes
[4 * r
+ c
];
3521 inst
->dead_mask
|= (1 << c
);
3525 /* Now actually remove the instructions that are completely dead and update
3526 * the writemask of other instructions with dead channels.
3528 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
3529 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3531 if (!inst
->dead_mask
|| !inst
->dst
.writemask
)
3533 else if ((inst
->dst
.writemask
& ~inst
->dead_mask
) == 0) {
3538 inst
->dst
.writemask
&= ~(inst
->dead_mask
);
3541 ralloc_free(write_level
);
3542 ralloc_free(writes
);
3547 /* Merges temporary registers together where possible to reduce the number of
3548 * registers needed to run a program.
3550 * Produces optimal code only after copy propagation and dead code elimination
3553 glsl_to_tgsi_visitor::merge_registers(void)
3555 int *last_reads
= rzalloc_array(mem_ctx
, int, this->next_temp
);
3556 int *first_writes
= rzalloc_array(mem_ctx
, int, this->next_temp
);
3559 /* Read the indices of the last read and first write to each temp register
3560 * into an array so that we don't have to traverse the instruction list as
3562 for (i
=0; i
< this->next_temp
; i
++) {
3563 last_reads
[i
] = get_last_temp_read(i
);
3564 first_writes
[i
] = get_first_temp_write(i
);
3567 /* Start looking for registers with non-overlapping usages that can be
3568 * merged together. */
3569 for (i
=0; i
< this->next_temp
; i
++) {
3570 /* Don't touch unused registers. */
3571 if (last_reads
[i
] < 0 || first_writes
[i
] < 0) continue;
3573 for (j
=0; j
< this->next_temp
; j
++) {
3574 /* Don't touch unused registers. */
3575 if (last_reads
[j
] < 0 || first_writes
[j
] < 0) continue;
3577 /* We can merge the two registers if the first write to j is after or
3578 * in the same instruction as the last read from i. Note that the
3579 * register at index i will always be used earlier or at the same time
3580 * as the register at index j. */
3581 if (first_writes
[i
] <= first_writes
[j
] &&
3582 last_reads
[i
] <= first_writes
[j
])
3584 rename_temp_register(j
, i
); /* Replace all references to j with i.*/
3586 /* Update the first_writes and last_reads arrays with the new
3587 * values for the merged register index, and mark the newly unused
3588 * register index as such. */
3589 last_reads
[i
] = last_reads
[j
];
3590 first_writes
[j
] = -1;
3596 ralloc_free(last_reads
);
3597 ralloc_free(first_writes
);
3600 /* Reassign indices to temporary registers by reusing unused indices created
3601 * by optimization passes. */
3603 glsl_to_tgsi_visitor::renumber_registers(void)
3608 for (i
=0; i
< this->next_temp
; i
++) {
3609 if (get_first_temp_read(i
) < 0) continue;
3611 rename_temp_register(i
, new_index
);
3615 this->next_temp
= new_index
;
3619 * Returns a fragment program which implements the current pixel transfer ops.
3620 * Based on get_pixel_transfer_program in st_atom_pixeltransfer.c.
3623 get_pixel_transfer_visitor(struct st_fragment_program
*fp
,
3624 glsl_to_tgsi_visitor
*original
,
3625 int scale_and_bias
, int pixel_maps
)
3627 glsl_to_tgsi_visitor
*v
= new glsl_to_tgsi_visitor();
3628 struct st_context
*st
= st_context(original
->ctx
);
3629 struct gl_program
*prog
= &fp
->Base
.Base
;
3630 struct gl_program_parameter_list
*params
= _mesa_new_parameter_list();
3631 st_src_reg coord
, src0
;
3633 glsl_to_tgsi_instruction
*inst
;
3635 /* Copy attributes of the glsl_to_tgsi_visitor in the original shader. */
3636 v
->ctx
= original
->ctx
;
3638 v
->shader_program
= NULL
;
3639 v
->glsl_version
= original
->glsl_version
;
3640 v
->native_integers
= original
->native_integers
;
3641 v
->options
= original
->options
;
3642 v
->next_temp
= original
->next_temp
;
3643 v
->num_address_regs
= original
->num_address_regs
;
3644 v
->samplers_used
= prog
->SamplersUsed
= original
->samplers_used
;
3645 v
->indirect_addr_temps
= original
->indirect_addr_temps
;
3646 v
->indirect_addr_consts
= original
->indirect_addr_consts
;
3647 memcpy(&v
->immediates
, &original
->immediates
, sizeof(v
->immediates
));
3648 v
->num_immediates
= original
->num_immediates
;
3651 * Get initial pixel color from the texture.
3652 * TEX colorTemp, fragment.texcoord[0], texture[0], 2D;
3654 coord
= st_src_reg(PROGRAM_INPUT
, FRAG_ATTRIB_TEX0
, glsl_type::vec2_type
);
3655 src0
= v
->get_temp(glsl_type::vec4_type
);
3656 dst0
= st_dst_reg(src0
);
3657 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, dst0
, coord
);
3659 inst
->tex_target
= TEXTURE_2D_INDEX
;
3661 prog
->InputsRead
|= FRAG_BIT_TEX0
;
3662 prog
->SamplersUsed
|= (1 << 0); /* mark sampler 0 as used */
3663 v
->samplers_used
|= (1 << 0);
3665 if (scale_and_bias
) {
3666 static const gl_state_index scale_state
[STATE_LENGTH
] =
3667 { STATE_INTERNAL
, STATE_PT_SCALE
,
3668 (gl_state_index
) 0, (gl_state_index
) 0, (gl_state_index
) 0 };
3669 static const gl_state_index bias_state
[STATE_LENGTH
] =
3670 { STATE_INTERNAL
, STATE_PT_BIAS
,
3671 (gl_state_index
) 0, (gl_state_index
) 0, (gl_state_index
) 0 };
3672 GLint scale_p
, bias_p
;
3673 st_src_reg scale
, bias
;
3675 scale_p
= _mesa_add_state_reference(params
, scale_state
);
3676 bias_p
= _mesa_add_state_reference(params
, bias_state
);
3678 /* MAD colorTemp, colorTemp, scale, bias; */
3679 scale
= st_src_reg(PROGRAM_STATE_VAR
, scale_p
, GLSL_TYPE_FLOAT
);
3680 bias
= st_src_reg(PROGRAM_STATE_VAR
, bias_p
, GLSL_TYPE_FLOAT
);
3681 inst
= v
->emit(NULL
, TGSI_OPCODE_MAD
, dst0
, src0
, scale
, bias
);
3685 st_src_reg temp
= v
->get_temp(glsl_type::vec4_type
);
3686 st_dst_reg temp_dst
= st_dst_reg(temp
);
3688 assert(st
->pixel_xfer
.pixelmap_texture
);
3690 /* With a little effort, we can do four pixel map look-ups with
3691 * two TEX instructions:
3694 /* TEX temp.rg, colorTemp.rgba, texture[1], 2D; */
3695 temp_dst
.writemask
= WRITEMASK_XY
; /* write R,G */
3696 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, temp_dst
, src0
);
3698 inst
->tex_target
= TEXTURE_2D_INDEX
;
3700 /* TEX temp.ba, colorTemp.baba, texture[1], 2D; */
3701 src0
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_Z
, SWIZZLE_W
, SWIZZLE_Z
, SWIZZLE_W
);
3702 temp_dst
.writemask
= WRITEMASK_ZW
; /* write B,A */
3703 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, temp_dst
, src0
);
3705 inst
->tex_target
= TEXTURE_2D_INDEX
;
3707 prog
->SamplersUsed
|= (1 << 1); /* mark sampler 1 as used */
3708 v
->samplers_used
|= (1 << 1);
3710 /* MOV colorTemp, temp; */
3711 inst
= v
->emit(NULL
, TGSI_OPCODE_MOV
, dst0
, temp
);
3714 /* Now copy the instructions from the original glsl_to_tgsi_visitor into the
3716 foreach_iter(exec_list_iterator
, iter
, original
->instructions
) {
3717 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3718 glsl_to_tgsi_instruction
*newinst
;
3719 st_src_reg src_regs
[3];
3721 if (inst
->dst
.file
== PROGRAM_OUTPUT
)
3722 prog
->OutputsWritten
|= BITFIELD64_BIT(inst
->dst
.index
);
3724 for (int i
=0; i
<3; i
++) {
3725 src_regs
[i
] = inst
->src
[i
];
3726 if (src_regs
[i
].file
== PROGRAM_INPUT
&&
3727 src_regs
[i
].index
== FRAG_ATTRIB_COL0
)
3729 src_regs
[i
].file
= PROGRAM_TEMPORARY
;
3730 src_regs
[i
].index
= src0
.index
;
3732 else if (src_regs
[i
].file
== PROGRAM_INPUT
)
3733 prog
->InputsRead
|= BITFIELD64_BIT(src_regs
[i
].index
);
3736 newinst
= v
->emit(NULL
, inst
->op
, inst
->dst
, src_regs
[0], src_regs
[1], src_regs
[2]);
3737 newinst
->tex_target
= inst
->tex_target
;
3740 /* Make modifications to fragment program info. */
3741 prog
->Parameters
= _mesa_combine_parameter_lists(params
,
3742 original
->prog
->Parameters
);
3743 _mesa_free_parameter_list(params
);
3744 count_resources(v
, prog
);
3745 fp
->glsl_to_tgsi
= v
;
3749 * Make fragment program for glBitmap:
3750 * Sample the texture and kill the fragment if the bit is 0.
3751 * This program will be combined with the user's fragment program.
3753 * Based on make_bitmap_fragment_program in st_cb_bitmap.c.
3756 get_bitmap_visitor(struct st_fragment_program
*fp
,
3757 glsl_to_tgsi_visitor
*original
, int samplerIndex
)
3759 glsl_to_tgsi_visitor
*v
= new glsl_to_tgsi_visitor();
3760 struct st_context
*st
= st_context(original
->ctx
);
3761 struct gl_program
*prog
= &fp
->Base
.Base
;
3762 st_src_reg coord
, src0
;
3764 glsl_to_tgsi_instruction
*inst
;
3766 /* Copy attributes of the glsl_to_tgsi_visitor in the original shader. */
3767 v
->ctx
= original
->ctx
;
3769 v
->shader_program
= NULL
;
3770 v
->glsl_version
= original
->glsl_version
;
3771 v
->native_integers
= original
->native_integers
;
3772 v
->options
= original
->options
;
3773 v
->next_temp
= original
->next_temp
;
3774 v
->num_address_regs
= original
->num_address_regs
;
3775 v
->samplers_used
= prog
->SamplersUsed
= original
->samplers_used
;
3776 v
->indirect_addr_temps
= original
->indirect_addr_temps
;
3777 v
->indirect_addr_consts
= original
->indirect_addr_consts
;
3778 memcpy(&v
->immediates
, &original
->immediates
, sizeof(v
->immediates
));
3779 v
->num_immediates
= original
->num_immediates
;
3781 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
3782 coord
= st_src_reg(PROGRAM_INPUT
, FRAG_ATTRIB_TEX0
, glsl_type::vec2_type
);
3783 src0
= v
->get_temp(glsl_type::vec4_type
);
3784 dst0
= st_dst_reg(src0
);
3785 inst
= v
->emit(NULL
, TGSI_OPCODE_TEX
, dst0
, coord
);
3786 inst
->sampler
= samplerIndex
;
3787 inst
->tex_target
= TEXTURE_2D_INDEX
;
3789 prog
->InputsRead
|= FRAG_BIT_TEX0
;
3790 prog
->SamplersUsed
|= (1 << samplerIndex
); /* mark sampler as used */
3791 v
->samplers_used
|= (1 << samplerIndex
);
3793 /* KIL if -tmp0 < 0 # texel=0 -> keep / texel=0 -> discard */
3794 src0
.negate
= NEGATE_XYZW
;
3795 if (st
->bitmap
.tex_format
== PIPE_FORMAT_L8_UNORM
)
3796 src0
.swizzle
= SWIZZLE_XXXX
;
3797 inst
= v
->emit(NULL
, TGSI_OPCODE_KIL
, undef_dst
, src0
);
3799 /* Now copy the instructions from the original glsl_to_tgsi_visitor into the
3801 foreach_iter(exec_list_iterator
, iter
, original
->instructions
) {
3802 glsl_to_tgsi_instruction
*inst
= (glsl_to_tgsi_instruction
*)iter
.get();
3803 glsl_to_tgsi_instruction
*newinst
;
3804 st_src_reg src_regs
[3];
3806 if (inst
->dst
.file
== PROGRAM_OUTPUT
)
3807 prog
->OutputsWritten
|= BITFIELD64_BIT(inst
->dst
.index
);
3809 for (int i
=0; i
<3; i
++) {
3810 src_regs
[i
] = inst
->src
[i
];
3811 if (src_regs
[i
].file
== PROGRAM_INPUT
)
3812 prog
->InputsRead
|= BITFIELD64_BIT(src_regs
[i
].index
);
3815 newinst
= v
->emit(NULL
, inst
->op
, inst
->dst
, src_regs
[0], src_regs
[1], src_regs
[2]);
3816 newinst
->tex_target
= inst
->tex_target
;
3819 /* Make modifications to fragment program info. */
3820 prog
->Parameters
= _mesa_clone_parameter_list(original
->prog
->Parameters
);
3821 count_resources(v
, prog
);
3822 fp
->glsl_to_tgsi
= v
;
3825 /* ------------------------- TGSI conversion stuff -------------------------- */
3827 unsigned branch_target
;
3832 * Intermediate state used during shader translation.
3834 struct st_translate
{
3835 struct ureg_program
*ureg
;
3837 struct ureg_dst temps
[MAX_TEMPS
];
3838 struct ureg_src
*constants
;
3839 struct ureg_src
*immediates
;
3840 struct ureg_dst outputs
[PIPE_MAX_SHADER_OUTPUTS
];
3841 struct ureg_src inputs
[PIPE_MAX_SHADER_INPUTS
];
3842 struct ureg_dst address
[1];
3843 struct ureg_src samplers
[PIPE_MAX_SAMPLERS
];
3844 struct ureg_src systemValues
[SYSTEM_VALUE_MAX
];
3846 const GLuint
*inputMapping
;
3847 const GLuint
*outputMapping
;
3849 /* For every instruction that contains a label (eg CALL), keep
3850 * details so that we can go back afterwards and emit the correct
3851 * tgsi instruction number for each label.
3853 struct label
*labels
;
3854 unsigned labels_size
;
3855 unsigned labels_count
;
3857 /* Keep a record of the tgsi instruction number that each mesa
3858 * instruction starts at, will be used to fix up labels after
3863 unsigned insn_count
;
3865 unsigned procType
; /**< TGSI_PROCESSOR_VERTEX/FRAGMENT */
3870 /** Map Mesa's SYSTEM_VALUE_x to TGSI_SEMANTIC_x */
3871 static unsigned mesa_sysval_to_semantic
[SYSTEM_VALUE_MAX
] = {
3873 TGSI_SEMANTIC_VERTEXID
,
3874 TGSI_SEMANTIC_INSTANCEID
3878 * Make note of a branch to a label in the TGSI code.
3879 * After we've emitted all instructions, we'll go over the list
3880 * of labels built here and patch the TGSI code with the actual
3881 * location of each label.
3883 static unsigned *get_label(struct st_translate
*t
, unsigned branch_target
)
3887 if (t
->labels_count
+ 1 >= t
->labels_size
) {
3888 t
->labels_size
= 1 << (util_logbase2(t
->labels_size
) + 1);
3889 t
->labels
= (struct label
*)realloc(t
->labels
,
3890 t
->labels_size
* sizeof(struct label
));
3891 if (t
->labels
== NULL
) {
3892 static unsigned dummy
;
3898 i
= t
->labels_count
++;
3899 t
->labels
[i
].branch_target
= branch_target
;
3900 return &t
->labels
[i
].token
;
3904 * Called prior to emitting the TGSI code for each instruction.
3905 * Allocate additional space for instructions if needed.
3906 * Update the insn[] array so the next glsl_to_tgsi_instruction points to
3907 * the next TGSI instruction.
3909 static void set_insn_start(struct st_translate
*t
, unsigned start
)
3911 if (t
->insn_count
+ 1 >= t
->insn_size
) {
3912 t
->insn_size
= 1 << (util_logbase2(t
->insn_size
) + 1);
3913 t
->insn
= (unsigned *)realloc(t
->insn
, t
->insn_size
* sizeof(t
->insn
[0]));
3914 if (t
->insn
== NULL
) {
3920 t
->insn
[t
->insn_count
++] = start
;
3924 * Map a glsl_to_tgsi constant/immediate to a TGSI immediate.
3926 static struct ureg_src
3927 emit_immediate(struct st_translate
*t
,
3928 gl_constant_value values
[4],
3931 struct ureg_program
*ureg
= t
->ureg
;
3936 return ureg_DECL_immediate(ureg
, &values
[0].f
, size
);
3938 return ureg_DECL_immediate_int(ureg
, &values
[0].i
, size
);
3939 case GL_UNSIGNED_INT
:
3941 return ureg_DECL_immediate_uint(ureg
, &values
[0].u
, size
);
3943 assert(!"should not get here - type must be float, int, uint, or bool");
3944 return ureg_src_undef();
3949 * Map a glsl_to_tgsi dst register to a TGSI ureg_dst register.
3951 static struct ureg_dst
3952 dst_register(struct st_translate
*t
,
3953 gl_register_file file
,
3957 case PROGRAM_UNDEFINED
:
3958 return ureg_dst_undef();
3960 case PROGRAM_TEMPORARY
:
3961 if (ureg_dst_is_undef(t
->temps
[index
]))
3962 t
->temps
[index
] = ureg_DECL_temporary(t
->ureg
);
3964 return t
->temps
[index
];
3966 case PROGRAM_OUTPUT
:
3967 if (t
->procType
== TGSI_PROCESSOR_VERTEX
)
3968 assert(index
< VERT_RESULT_MAX
);
3969 else if (t
->procType
== TGSI_PROCESSOR_FRAGMENT
)
3970 assert(index
< FRAG_RESULT_MAX
);
3972 assert(index
< GEOM_RESULT_MAX
);
3974 assert(t
->outputMapping
[index
] < Elements(t
->outputs
));
3976 return t
->outputs
[t
->outputMapping
[index
]];
3978 case PROGRAM_ADDRESS
:
3979 return t
->address
[index
];
3982 assert(!"unknown dst register file");
3983 return ureg_dst_undef();
3988 * Map a glsl_to_tgsi src register to a TGSI ureg_src register.
3990 static struct ureg_src
3991 src_register(struct st_translate
*t
,
3992 gl_register_file file
,
3996 case PROGRAM_UNDEFINED
:
3997 return ureg_src_undef();
3999 case PROGRAM_TEMPORARY
:
4001 assert(index
< Elements(t
->temps
));
4002 if (ureg_dst_is_undef(t
->temps
[index
]))
4003 t
->temps
[index
] = ureg_DECL_temporary(t
->ureg
);
4004 return ureg_src(t
->temps
[index
]);
4006 case PROGRAM_NAMED_PARAM
:
4007 case PROGRAM_ENV_PARAM
:
4008 case PROGRAM_LOCAL_PARAM
:
4009 case PROGRAM_UNIFORM
:
4011 return t
->constants
[index
];
4012 case PROGRAM_STATE_VAR
:
4013 case PROGRAM_CONSTANT
: /* ie, immediate */
4015 return ureg_DECL_constant(t
->ureg
, 0);
4017 return t
->constants
[index
];
4019 case PROGRAM_IMMEDIATE
:
4020 return t
->immediates
[index
];
4023 assert(t
->inputMapping
[index
] < Elements(t
->inputs
));
4024 return t
->inputs
[t
->inputMapping
[index
]];
4026 case PROGRAM_OUTPUT
:
4027 assert(t
->outputMapping
[index
] < Elements(t
->outputs
));
4028 return ureg_src(t
->outputs
[t
->outputMapping
[index
]]); /* not needed? */
4030 case PROGRAM_ADDRESS
:
4031 return ureg_src(t
->address
[index
]);
4033 case PROGRAM_SYSTEM_VALUE
:
4034 assert(index
< Elements(t
->systemValues
));
4035 return t
->systemValues
[index
];
4038 assert(!"unknown src register file");
4039 return ureg_src_undef();
4044 * Create a TGSI ureg_dst register from an st_dst_reg.
4046 static struct ureg_dst
4047 translate_dst(struct st_translate
*t
,
4048 const st_dst_reg
*dst_reg
,
4049 bool saturate
, bool clamp_color
)
4051 struct ureg_dst dst
= dst_register(t
,
4055 dst
= ureg_writemask(dst
, dst_reg
->writemask
);
4058 dst
= ureg_saturate(dst
);
4059 else if (clamp_color
&& dst_reg
->file
== PROGRAM_OUTPUT
) {
4060 /* Clamp colors for ARB_color_buffer_float. */
4061 switch (t
->procType
) {
4062 case TGSI_PROCESSOR_VERTEX
:
4063 /* XXX if the geometry shader is present, this must be done there
4064 * instead of here. */
4065 if (dst_reg
->index
== VERT_RESULT_COL0
||
4066 dst_reg
->index
== VERT_RESULT_COL1
||
4067 dst_reg
->index
== VERT_RESULT_BFC0
||
4068 dst_reg
->index
== VERT_RESULT_BFC1
) {
4069 dst
= ureg_saturate(dst
);
4073 case TGSI_PROCESSOR_FRAGMENT
:
4074 if (dst_reg
->index
>= FRAG_RESULT_COLOR
) {
4075 dst
= ureg_saturate(dst
);
4081 if (dst_reg
->reladdr
!= NULL
)
4082 dst
= ureg_dst_indirect(dst
, ureg_src(t
->address
[0]));
4088 * Create a TGSI ureg_src register from an st_src_reg.
4090 static struct ureg_src
4091 translate_src(struct st_translate
*t
, const st_src_reg
*src_reg
)
4093 struct ureg_src src
= src_register(t
, src_reg
->file
, src_reg
->index
);
4095 src
= ureg_swizzle(src
,
4096 GET_SWZ(src_reg
->swizzle
, 0) & 0x3,
4097 GET_SWZ(src_reg
->swizzle
, 1) & 0x3,
4098 GET_SWZ(src_reg
->swizzle
, 2) & 0x3,
4099 GET_SWZ(src_reg
->swizzle
, 3) & 0x3);
4101 if ((src_reg
->negate
& 0xf) == NEGATE_XYZW
)
4102 src
= ureg_negate(src
);
4104 if (src_reg
->reladdr
!= NULL
) {
4105 /* Normally ureg_src_indirect() would be used here, but a stupid compiler
4106 * bug in g++ makes ureg_src_indirect (an inline C function) erroneously
4107 * set the bit for src.Negate. So we have to do the operation manually
4108 * here to work around the compiler's problems. */
4109 /*src = ureg_src_indirect(src, ureg_src(t->address[0]));*/
4110 struct ureg_src addr
= ureg_src(t
->address
[0]);
4112 src
.IndirectFile
= addr
.File
;
4113 src
.IndirectIndex
= addr
.Index
;
4114 src
.IndirectSwizzle
= addr
.SwizzleX
;
4116 if (src_reg
->file
!= PROGRAM_INPUT
&&
4117 src_reg
->file
!= PROGRAM_OUTPUT
) {
4118 /* If src_reg->index was negative, it was set to zero in
4119 * src_register(). Reassign it now. But don't do this
4120 * for input/output regs since they get remapped while
4121 * const buffers don't.
4123 src
.Index
= src_reg
->index
;
4130 static struct tgsi_texture_offset
4131 translate_tex_offset(struct st_translate
*t
,
4132 const struct tgsi_texture_offset
*in_offset
)
4134 struct tgsi_texture_offset offset
;
4136 assert(in_offset
->File
== PROGRAM_IMMEDIATE
);
4138 offset
.File
= TGSI_FILE_IMMEDIATE
;
4139 offset
.Index
= in_offset
->Index
;
4140 offset
.SwizzleX
= in_offset
->SwizzleX
;
4141 offset
.SwizzleY
= in_offset
->SwizzleY
;
4142 offset
.SwizzleZ
= in_offset
->SwizzleZ
;
4148 compile_tgsi_instruction(struct st_translate
*t
,
4149 const glsl_to_tgsi_instruction
*inst
,
4150 bool clamp_dst_color_output
)
4152 struct ureg_program
*ureg
= t
->ureg
;
4154 struct ureg_dst dst
[1];
4155 struct ureg_src src
[4];
4156 struct tgsi_texture_offset texoffsets
[MAX_GLSL_TEXTURE_OFFSET
];
4161 num_dst
= num_inst_dst_regs(inst
->op
);
4162 num_src
= num_inst_src_regs(inst
->op
);
4165 dst
[0] = translate_dst(t
,
4168 clamp_dst_color_output
);
4170 for (i
= 0; i
< num_src
; i
++)
4171 src
[i
] = translate_src(t
, &inst
->src
[i
]);
4174 case TGSI_OPCODE_BGNLOOP
:
4175 case TGSI_OPCODE_CAL
:
4176 case TGSI_OPCODE_ELSE
:
4177 case TGSI_OPCODE_ENDLOOP
:
4178 case TGSI_OPCODE_IF
:
4179 assert(num_dst
== 0);
4180 ureg_label_insn(ureg
,
4184 inst
->op
== TGSI_OPCODE_CAL
? inst
->function
->sig_id
: 0));
4187 case TGSI_OPCODE_TEX
:
4188 case TGSI_OPCODE_TXB
:
4189 case TGSI_OPCODE_TXD
:
4190 case TGSI_OPCODE_TXL
:
4191 case TGSI_OPCODE_TXP
:
4192 case TGSI_OPCODE_TXQ
:
4193 case TGSI_OPCODE_TXF
:
4194 src
[num_src
++] = t
->samplers
[inst
->sampler
];
4195 for (i
= 0; i
< inst
->tex_offset_num_offset
; i
++) {
4196 texoffsets
[i
] = translate_tex_offset(t
, &inst
->tex_offsets
[i
]);
4201 st_translate_texture_target(inst
->tex_target
, inst
->tex_shadow
),
4202 texoffsets
, inst
->tex_offset_num_offset
,
4206 case TGSI_OPCODE_SCS
:
4207 dst
[0] = ureg_writemask(dst
[0], TGSI_WRITEMASK_XY
);
4208 ureg_insn(ureg
, inst
->op
, dst
, num_dst
, src
, num_src
);
4221 * Emit the TGSI instructions for inverting and adjusting WPOS.
4222 * This code is unavoidable because it also depends on whether
4223 * a FBO is bound (STATE_FB_WPOS_Y_TRANSFORM).
4226 emit_wpos_adjustment( struct st_translate
*t
,
4227 const struct gl_program
*program
,
4229 GLfloat adjX
, GLfloat adjY
[2])
4231 struct ureg_program
*ureg
= t
->ureg
;
4233 /* Fragment program uses fragment position input.
4234 * Need to replace instances of INPUT[WPOS] with temp T
4235 * where T = INPUT[WPOS] by y is inverted.
4237 static const gl_state_index wposTransformState
[STATE_LENGTH
]
4238 = { STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
,
4239 (gl_state_index
)0, (gl_state_index
)0, (gl_state_index
)0 };
4241 /* XXX: note we are modifying the incoming shader here! Need to
4242 * do this before emitting the constant decls below, or this
4245 unsigned wposTransConst
= _mesa_add_state_reference(program
->Parameters
,
4246 wposTransformState
);
4248 struct ureg_src wpostrans
= ureg_DECL_constant( ureg
, wposTransConst
);
4249 struct ureg_dst wpos_temp
= ureg_DECL_temporary( ureg
);
4250 struct ureg_src wpos_input
= t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_WPOS
]];
4252 /* First, apply the coordinate shift: */
4253 if (adjX
|| adjY
[0] || adjY
[1]) {
4254 if (adjY
[0] != adjY
[1]) {
4255 /* Adjust the y coordinate by adjY[1] or adjY[0] respectively
4256 * depending on whether inversion is actually going to be applied
4257 * or not, which is determined by testing against the inversion
4258 * state variable used below, which will be either +1 or -1.
4260 struct ureg_dst adj_temp
= ureg_DECL_temporary(ureg
);
4262 ureg_CMP(ureg
, adj_temp
,
4263 ureg_scalar(wpostrans
, invert
? 2 : 0),
4264 ureg_imm4f(ureg
, adjX
, adjY
[0], 0.0f
, 0.0f
),
4265 ureg_imm4f(ureg
, adjX
, adjY
[1], 0.0f
, 0.0f
));
4266 ureg_ADD(ureg
, wpos_temp
, wpos_input
, ureg_src(adj_temp
));
4268 ureg_ADD(ureg
, wpos_temp
, wpos_input
,
4269 ureg_imm4f(ureg
, adjX
, adjY
[0], 0.0f
, 0.0f
));
4271 wpos_input
= ureg_src(wpos_temp
);
4273 /* MOV wpos_temp, input[wpos]
4275 ureg_MOV( ureg
, wpos_temp
, wpos_input
);
4278 /* Now the conditional y flip: STATE_FB_WPOS_Y_TRANSFORM.xy/zw will be
4279 * inversion/identity, or the other way around if we're drawing to an FBO.
4282 /* MAD wpos_temp.y, wpos_input, wpostrans.xxxx, wpostrans.yyyy
4285 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
4287 ureg_scalar(wpostrans
, 0),
4288 ureg_scalar(wpostrans
, 1));
4290 /* MAD wpos_temp.y, wpos_input, wpostrans.zzzz, wpostrans.wwww
4293 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
4295 ureg_scalar(wpostrans
, 2),
4296 ureg_scalar(wpostrans
, 3));
4299 /* Use wpos_temp as position input from here on:
4301 t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_WPOS
]] = ureg_src(wpos_temp
);
4306 * Emit fragment position/ooordinate code.
4309 emit_wpos(struct st_context
*st
,
4310 struct st_translate
*t
,
4311 const struct gl_program
*program
,
4312 struct ureg_program
*ureg
)
4314 const struct gl_fragment_program
*fp
=
4315 (const struct gl_fragment_program
*) program
;
4316 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
4317 GLfloat adjX
= 0.0f
;
4318 GLfloat adjY
[2] = { 0.0f
, 0.0f
};
4319 boolean invert
= FALSE
;
4321 /* Query the pixel center conventions supported by the pipe driver and set
4322 * adjX, adjY to help out if it cannot handle the requested one internally.
4324 * The bias of the y-coordinate depends on whether y-inversion takes place
4325 * (adjY[1]) or not (adjY[0]), which is in turn dependent on whether we are
4326 * drawing to an FBO (causes additional inversion), and whether the the pipe
4327 * driver origin and the requested origin differ (the latter condition is
4328 * stored in the 'invert' variable).
4330 * For height = 100 (i = integer, h = half-integer, l = lower, u = upper):
4332 * center shift only:
4337 * l,i -> u,i: ( 0.0 + 1.0) * -1 + 100 = 99
4338 * l,h -> u,h: ( 0.5 + 0.0) * -1 + 100 = 99.5
4339 * u,i -> l,i: (99.0 + 1.0) * -1 + 100 = 0
4340 * u,h -> l,h: (99.5 + 0.0) * -1 + 100 = 0.5
4342 * inversion and center shift:
4343 * l,i -> u,h: ( 0.0 + 0.5) * -1 + 100 = 99.5
4344 * l,h -> u,i: ( 0.5 + 0.5) * -1 + 100 = 99
4345 * u,i -> l,h: (99.0 + 0.5) * -1 + 100 = 0.5
4346 * u,h -> l,i: (99.5 + 0.5) * -1 + 100 = 0
4348 if (fp
->OriginUpperLeft
) {
4349 /* Fragment shader wants origin in upper-left */
4350 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
)) {
4351 /* the driver supports upper-left origin */
4353 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
)) {
4354 /* the driver supports lower-left origin, need to invert Y */
4355 ureg_property_fs_coord_origin(ureg
, TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
4362 /* Fragment shader wants origin in lower-left */
4363 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
))
4364 /* the driver supports lower-left origin */
4365 ureg_property_fs_coord_origin(ureg
, TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
4366 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
))
4367 /* the driver supports upper-left origin, need to invert Y */
4373 if (fp
->PixelCenterInteger
) {
4374 /* Fragment shader wants pixel center integer */
4375 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)) {
4376 /* the driver supports pixel center integer */
4378 ureg_property_fs_coord_pixel_center(ureg
, TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
4380 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
)) {
4381 /* the driver supports pixel center half integer, need to bias X,Y */
4390 /* Fragment shader wants pixel center half integer */
4391 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
)) {
4392 /* the driver supports pixel center half integer */
4394 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)) {
4395 /* the driver supports pixel center integer, need to bias X,Y */
4396 adjX
= adjY
[0] = adjY
[1] = 0.5f
;
4397 ureg_property_fs_coord_pixel_center(ureg
, TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
4403 /* we invert after adjustment so that we avoid the MOV to temporary,
4404 * and reuse the adjustment ADD instead */
4405 emit_wpos_adjustment(t
, program
, invert
, adjX
, adjY
);
4409 * OpenGL's fragment gl_FrontFace input is 1 for front-facing, 0 for back.
4410 * TGSI uses +1 for front, -1 for back.
4411 * This function converts the TGSI value to the GL value. Simply clamping/
4412 * saturating the value to [0,1] does the job.
4415 emit_face_var(struct st_translate
*t
)
4417 struct ureg_program
*ureg
= t
->ureg
;
4418 struct ureg_dst face_temp
= ureg_DECL_temporary(ureg
);
4419 struct ureg_src face_input
= t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_FACE
]];
4421 /* MOV_SAT face_temp, input[face] */
4422 face_temp
= ureg_saturate(face_temp
);
4423 ureg_MOV(ureg
, face_temp
, face_input
);
4425 /* Use face_temp as face input from here on: */
4426 t
->inputs
[t
->inputMapping
[FRAG_ATTRIB_FACE
]] = ureg_src(face_temp
);
4430 emit_edgeflags(struct st_translate
*t
)
4432 struct ureg_program
*ureg
= t
->ureg
;
4433 struct ureg_dst edge_dst
= t
->outputs
[t
->outputMapping
[VERT_RESULT_EDGE
]];
4434 struct ureg_src edge_src
= t
->inputs
[t
->inputMapping
[VERT_ATTRIB_EDGEFLAG
]];
4436 ureg_MOV(ureg
, edge_dst
, edge_src
);
4440 * Translate intermediate IR (glsl_to_tgsi_instruction) to TGSI format.
4441 * \param program the program to translate
4442 * \param numInputs number of input registers used
4443 * \param inputMapping maps Mesa fragment program inputs to TGSI generic
4445 * \param inputSemanticName the TGSI_SEMANTIC flag for each input
4446 * \param inputSemanticIndex the semantic index (ex: which texcoord) for
4448 * \param interpMode the TGSI_INTERPOLATE_LINEAR/PERSP mode for each input
4449 * \param numOutputs number of output registers used
4450 * \param outputMapping maps Mesa fragment program outputs to TGSI
4452 * \param outputSemanticName the TGSI_SEMANTIC flag for each output
4453 * \param outputSemanticIndex the semantic index (ex: which texcoord) for
4456 * \return PIPE_OK or PIPE_ERROR_OUT_OF_MEMORY
4458 extern "C" enum pipe_error
4459 st_translate_program(
4460 struct gl_context
*ctx
,
4462 struct ureg_program
*ureg
,
4463 glsl_to_tgsi_visitor
*program
,
4464 const struct gl_program
*proginfo
,
4466 const GLuint inputMapping
[],
4467 const ubyte inputSemanticName
[],
4468 const ubyte inputSemanticIndex
[],
4469 const GLuint interpMode
[],
4471 const GLuint outputMapping
[],
4472 const ubyte outputSemanticName
[],
4473 const ubyte outputSemanticIndex
[],
4474 boolean passthrough_edgeflags
,
4475 boolean clamp_color
)
4477 struct st_translate
*t
;
4479 enum pipe_error ret
= PIPE_OK
;
4481 assert(numInputs
<= Elements(t
->inputs
));
4482 assert(numOutputs
<= Elements(t
->outputs
));
4484 t
= CALLOC_STRUCT(st_translate
);
4486 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
4490 memset(t
, 0, sizeof *t
);
4492 t
->procType
= procType
;
4493 t
->inputMapping
= inputMapping
;
4494 t
->outputMapping
= outputMapping
;
4497 if (program
->shader_program
) {
4498 for (i
= 0; i
< program
->shader_program
->NumUserUniformStorage
; i
++) {
4499 struct gl_uniform_storage
*const storage
=
4500 &program
->shader_program
->UniformStorage
[i
];
4502 _mesa_uniform_detach_all_driver_storage(storage
);
4507 * Declare input attributes.
4509 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
4510 for (i
= 0; i
< numInputs
; i
++) {
4511 t
->inputs
[i
] = ureg_DECL_fs_input(ureg
,
4512 inputSemanticName
[i
],
4513 inputSemanticIndex
[i
],
4517 if (proginfo
->InputsRead
& FRAG_BIT_WPOS
) {
4518 /* Must do this after setting up t->inputs, and before
4519 * emitting constant references, below:
4521 emit_wpos(st_context(ctx
), t
, proginfo
, ureg
);
4524 if (proginfo
->InputsRead
& FRAG_BIT_FACE
)
4528 * Declare output attributes.
4530 for (i
= 0; i
< numOutputs
; i
++) {
4531 switch (outputSemanticName
[i
]) {
4532 case TGSI_SEMANTIC_POSITION
:
4533 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4534 TGSI_SEMANTIC_POSITION
, /* Z/Depth */
4535 outputSemanticIndex
[i
]);
4536 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_Z
);
4538 case TGSI_SEMANTIC_STENCIL
:
4539 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4540 TGSI_SEMANTIC_STENCIL
, /* Stencil */
4541 outputSemanticIndex
[i
]);
4542 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_Y
);
4544 case TGSI_SEMANTIC_COLOR
:
4545 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4546 TGSI_SEMANTIC_COLOR
,
4547 outputSemanticIndex
[i
]);
4550 assert(!"fragment shader outputs must be POSITION/STENCIL/COLOR");
4551 ret
= PIPE_ERROR_BAD_INPUT
;
4556 else if (procType
== TGSI_PROCESSOR_GEOMETRY
) {
4557 for (i
= 0; i
< numInputs
; i
++) {
4558 t
->inputs
[i
] = ureg_DECL_gs_input(ureg
,
4560 inputSemanticName
[i
],
4561 inputSemanticIndex
[i
]);
4564 for (i
= 0; i
< numOutputs
; i
++) {
4565 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4566 outputSemanticName
[i
],
4567 outputSemanticIndex
[i
]);
4571 assert(procType
== TGSI_PROCESSOR_VERTEX
);
4573 for (i
= 0; i
< numInputs
; i
++) {
4574 t
->inputs
[i
] = ureg_DECL_vs_input(ureg
, i
);
4577 for (i
= 0; i
< numOutputs
; i
++) {
4578 if (outputSemanticName
[i
] == TGSI_SEMANTIC_CLIPDIST
) {
4579 int mask
= ((1 << (program
->num_clip_distances
- 4*outputSemanticIndex
[i
])) - 1) & TGSI_WRITEMASK_XYZW
;
4580 t
->outputs
[i
] = ureg_DECL_output_masked(ureg
,
4581 outputSemanticName
[i
],
4582 outputSemanticIndex
[i
],
4585 t
->outputs
[i
] = ureg_DECL_output(ureg
,
4586 outputSemanticName
[i
],
4587 outputSemanticIndex
[i
]);
4590 if (passthrough_edgeflags
)
4594 /* Declare address register.
4596 if (program
->num_address_regs
> 0) {
4597 assert(program
->num_address_regs
== 1);
4598 t
->address
[0] = ureg_DECL_address(ureg
);
4601 /* Declare misc input registers
4604 GLbitfield sysInputs
= proginfo
->SystemValuesRead
;
4605 unsigned numSys
= 0;
4606 for (i
= 0; sysInputs
; i
++) {
4607 if (sysInputs
& (1 << i
)) {
4608 unsigned semName
= mesa_sysval_to_semantic
[i
];
4609 t
->systemValues
[i
] = ureg_DECL_system_value(ureg
, numSys
, semName
, 0);
4611 sysInputs
&= ~(1 << i
);
4616 if (program
->indirect_addr_temps
) {
4617 /* If temps are accessed with indirect addressing, declare temporaries
4618 * in sequential order. Else, we declare them on demand elsewhere.
4619 * (Note: the number of temporaries is equal to program->next_temp)
4621 for (i
= 0; i
< (unsigned)program
->next_temp
; i
++) {
4622 /* XXX use TGSI_FILE_TEMPORARY_ARRAY when it's supported by ureg */
4623 t
->temps
[i
] = ureg_DECL_temporary(t
->ureg
);
4627 /* Emit constants and uniforms. TGSI uses a single index space for these,
4628 * so we put all the translated regs in t->constants.
4630 if (proginfo
->Parameters
) {
4631 t
->constants
= (struct ureg_src
*)CALLOC(proginfo
->Parameters
->NumParameters
* sizeof(t
->constants
[0]));
4632 if (t
->constants
== NULL
) {
4633 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
4637 for (i
= 0; i
< proginfo
->Parameters
->NumParameters
; i
++) {
4638 switch (proginfo
->Parameters
->Parameters
[i
].Type
) {
4639 case PROGRAM_ENV_PARAM
:
4640 case PROGRAM_LOCAL_PARAM
:
4641 case PROGRAM_STATE_VAR
:
4642 case PROGRAM_NAMED_PARAM
:
4643 case PROGRAM_UNIFORM
:
4644 t
->constants
[i
] = ureg_DECL_constant(ureg
, i
);
4647 /* Emit immediates for PROGRAM_CONSTANT only when there's no indirect
4648 * addressing of the const buffer.
4649 * FIXME: Be smarter and recognize param arrays:
4650 * indirect addressing is only valid within the referenced
4653 case PROGRAM_CONSTANT
:
4654 if (program
->indirect_addr_consts
)
4655 t
->constants
[i
] = ureg_DECL_constant(ureg
, i
);
4657 t
->constants
[i
] = emit_immediate(t
,
4658 proginfo
->Parameters
->ParameterValues
[i
],
4659 proginfo
->Parameters
->Parameters
[i
].DataType
,
4668 /* Emit immediate values.
4670 t
->immediates
= (struct ureg_src
*)CALLOC(program
->num_immediates
* sizeof(struct ureg_src
));
4671 if (t
->immediates
== NULL
) {
4672 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
4676 foreach_iter(exec_list_iterator
, iter
, program
->immediates
) {
4677 immediate_storage
*imm
= (immediate_storage
*)iter
.get();
4678 assert(i
< program
->num_immediates
);
4679 t
->immediates
[i
++] = emit_immediate(t
, imm
->values
, imm
->type
, imm
->size
);
4681 assert(i
== program
->num_immediates
);
4683 /* texture samplers */
4684 for (i
= 0; i
< ctx
->Const
.MaxTextureImageUnits
; i
++) {
4685 if (program
->samplers_used
& (1 << i
)) {
4686 t
->samplers
[i
] = ureg_DECL_sampler(ureg
, i
);
4690 /* Emit each instruction in turn:
4692 foreach_iter(exec_list_iterator
, iter
, program
->instructions
) {
4693 set_insn_start(t
, ureg_get_instruction_number(ureg
));
4694 compile_tgsi_instruction(t
, (glsl_to_tgsi_instruction
*)iter
.get(),
4698 /* Fix up all emitted labels:
4700 for (i
= 0; i
< t
->labels_count
; i
++) {
4701 ureg_fixup_label(ureg
, t
->labels
[i
].token
,
4702 t
->insn
[t
->labels
[i
].branch_target
]);
4705 if (program
->shader_program
) {
4706 /* This has to be done last. Any operation the can cause
4707 * prog->ParameterValues to get reallocated (e.g., anything that adds a
4708 * program constant) has to happen before creating this linkage.
4710 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
4711 if (program
->shader_program
->_LinkedShaders
[i
] == NULL
)
4714 _mesa_associate_uniform_storage(ctx
, program
->shader_program
,
4715 program
->shader_program
->_LinkedShaders
[i
]->Program
->Parameters
);
4724 FREE(t
->immediates
);
4727 debug_printf("%s: translate error flag set\n", __FUNCTION__
);
4735 /* ----------------------------- End TGSI code ------------------------------ */
4738 * Convert a shader's GLSL IR into a Mesa gl_program, although without
4739 * generating Mesa IR.
4741 static struct gl_program
*
4742 get_mesa_program(struct gl_context
*ctx
,
4743 struct gl_shader_program
*shader_program
,
4744 struct gl_shader
*shader
,
4745 int num_clip_distances
)
4747 glsl_to_tgsi_visitor
* v
= new glsl_to_tgsi_visitor();
4748 struct gl_program
*prog
;
4749 struct pipe_screen
* screen
= st_context(ctx
)->pipe
->screen
;
4750 unsigned pipe_shader_type
;
4752 const char *target_string
;
4754 struct gl_shader_compiler_options
*options
=
4755 &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(shader
->Type
)];
4757 switch (shader
->Type
) {
4758 case GL_VERTEX_SHADER
:
4759 target
= GL_VERTEX_PROGRAM_ARB
;
4760 target_string
= "vertex";
4761 pipe_shader_type
= PIPE_SHADER_VERTEX
;
4763 case GL_FRAGMENT_SHADER
:
4764 target
= GL_FRAGMENT_PROGRAM_ARB
;
4765 target_string
= "fragment";
4766 pipe_shader_type
= PIPE_SHADER_FRAGMENT
;
4768 case GL_GEOMETRY_SHADER
:
4769 target
= GL_GEOMETRY_PROGRAM_NV
;
4770 target_string
= "geometry";
4771 pipe_shader_type
= PIPE_SHADER_GEOMETRY
;
4774 assert(!"should not be reached");
4778 validate_ir_tree(shader
->ir
);
4780 prog
= ctx
->Driver
.NewProgram(ctx
, target
, shader_program
->Name
);
4783 prog
->Parameters
= _mesa_new_parameter_list();
4786 v
->shader_program
= shader_program
;
4787 v
->options
= options
;
4788 v
->glsl_version
= ctx
->Const
.GLSLVersion
;
4789 v
->native_integers
= ctx
->Const
.NativeIntegers
;
4790 v
->num_clip_distances
= num_clip_distances
;
4792 _mesa_generate_parameters_list_for_uniforms(shader_program
, shader
,
4795 if (!screen
->get_shader_param(screen
, pipe_shader_type
,
4796 PIPE_SHADER_CAP_OUTPUT_READ
)) {
4797 /* Remove reads to output registers, and to varyings in vertex shaders. */
4798 lower_output_reads(shader
->ir
);
4802 /* Emit intermediate IR for main(). */
4803 visit_exec_list(shader
->ir
, v
);
4805 /* Now emit bodies for any functions that were used. */
4807 progress
= GL_FALSE
;
4809 foreach_iter(exec_list_iterator
, iter
, v
->function_signatures
) {
4810 function_entry
*entry
= (function_entry
*)iter
.get();
4812 if (!entry
->bgn_inst
) {
4813 v
->current_function
= entry
;
4815 entry
->bgn_inst
= v
->emit(NULL
, TGSI_OPCODE_BGNSUB
);
4816 entry
->bgn_inst
->function
= entry
;
4818 visit_exec_list(&entry
->sig
->body
, v
);
4820 glsl_to_tgsi_instruction
*last
;
4821 last
= (glsl_to_tgsi_instruction
*)v
->instructions
.get_tail();
4822 if (last
->op
!= TGSI_OPCODE_RET
)
4823 v
->emit(NULL
, TGSI_OPCODE_RET
);
4825 glsl_to_tgsi_instruction
*end
;
4826 end
= v
->emit(NULL
, TGSI_OPCODE_ENDSUB
);
4827 end
->function
= entry
;
4835 /* Print out some information (for debugging purposes) used by the
4836 * optimization passes. */
4837 for (i
=0; i
< v
->next_temp
; i
++) {
4838 int fr
= v
->get_first_temp_read(i
);
4839 int fw
= v
->get_first_temp_write(i
);
4840 int lr
= v
->get_last_temp_read(i
);
4841 int lw
= v
->get_last_temp_write(i
);
4843 printf("Temp %d: FR=%3d FW=%3d LR=%3d LW=%3d\n", i
, fr
, fw
, lr
, lw
);
4848 /* Perform optimizations on the instructions in the glsl_to_tgsi_visitor. */
4850 v
->copy_propagate();
4851 while (v
->eliminate_dead_code_advanced());
4853 /* FIXME: These passes to optimize temporary registers don't work when there
4854 * is indirect addressing of the temporary register space. We need proper
4855 * array support so that we don't have to give up these passes in every
4856 * shader that uses arrays.
4858 if (!v
->indirect_addr_temps
) {
4859 v
->eliminate_dead_code();
4860 v
->merge_registers();
4861 v
->renumber_registers();
4864 /* Write the END instruction. */
4865 v
->emit(NULL
, TGSI_OPCODE_END
);
4867 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
4869 printf("GLSL IR for linked %s program %d:\n", target_string
,
4870 shader_program
->Name
);
4871 _mesa_print_ir(shader
->ir
, NULL
);
4877 prog
->Instructions
= NULL
;
4878 prog
->NumInstructions
= 0;
4880 do_set_program_inouts(shader
->ir
, prog
, shader
->Type
== GL_FRAGMENT_SHADER
);
4881 count_resources(v
, prog
);
4883 _mesa_reference_program(ctx
, &shader
->Program
, prog
);
4885 /* This has to be done last. Any operation the can cause
4886 * prog->ParameterValues to get reallocated (e.g., anything that adds a
4887 * program constant) has to happen before creating this linkage.
4889 _mesa_associate_uniform_storage(ctx
, shader_program
, prog
->Parameters
);
4890 if (!shader_program
->LinkStatus
) {
4894 struct st_vertex_program
*stvp
;
4895 struct st_fragment_program
*stfp
;
4896 struct st_geometry_program
*stgp
;
4898 switch (shader
->Type
) {
4899 case GL_VERTEX_SHADER
:
4900 stvp
= (struct st_vertex_program
*)prog
;
4901 stvp
->glsl_to_tgsi
= v
;
4903 case GL_FRAGMENT_SHADER
:
4904 stfp
= (struct st_fragment_program
*)prog
;
4905 stfp
->glsl_to_tgsi
= v
;
4907 case GL_GEOMETRY_SHADER
:
4908 stgp
= (struct st_geometry_program
*)prog
;
4909 stgp
->glsl_to_tgsi
= v
;
4912 assert(!"should not be reached");
4920 * Searches through the IR for a declaration of gl_ClipDistance and returns the
4921 * declared size of the gl_ClipDistance array. Returns 0 if gl_ClipDistance is
4922 * not declared in the IR.
4924 int get_clip_distance_size(exec_list
*ir
)
4926 foreach_iter (exec_list_iterator
, iter
, *ir
) {
4927 ir_instruction
*inst
= (ir_instruction
*)iter
.get();
4928 ir_variable
*var
= inst
->as_variable();
4929 if (var
== NULL
) continue;
4930 if (!strcmp(var
->name
, "gl_ClipDistance")) {
4931 return var
->type
->length
;
4941 st_new_shader(struct gl_context
*ctx
, GLuint name
, GLuint type
)
4943 struct gl_shader
*shader
;
4944 assert(type
== GL_FRAGMENT_SHADER
|| type
== GL_VERTEX_SHADER
||
4945 type
== GL_GEOMETRY_SHADER_ARB
);
4946 shader
= rzalloc(NULL
, struct gl_shader
);
4948 shader
->Type
= type
;
4949 shader
->Name
= name
;
4950 _mesa_init_shader(ctx
, shader
);
4955 struct gl_shader_program
*
4956 st_new_shader_program(struct gl_context
*ctx
, GLuint name
)
4958 struct gl_shader_program
*shProg
;
4959 shProg
= rzalloc(NULL
, struct gl_shader_program
);
4961 shProg
->Name
= name
;
4962 _mesa_init_shader_program(ctx
, shProg
);
4969 * Called via ctx->Driver.LinkShader()
4970 * This actually involves converting GLSL IR into an intermediate TGSI-like IR
4971 * with code lowering and other optimizations.
4974 st_link_shader(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
4976 int num_clip_distances
[MESA_SHADER_TYPES
];
4977 assert(prog
->LinkStatus
);
4979 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
4980 if (prog
->_LinkedShaders
[i
] == NULL
)
4984 exec_list
*ir
= prog
->_LinkedShaders
[i
]->ir
;
4985 const struct gl_shader_compiler_options
*options
=
4986 &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(prog
->_LinkedShaders
[i
]->Type
)];
4988 /* We have to determine the length of the gl_ClipDistance array before
4989 * the array is lowered to two vec4s by lower_clip_distance().
4991 num_clip_distances
[i
] = get_clip_distance_size(ir
);
4994 unsigned what_to_lower
= MOD_TO_FRACT
| DIV_TO_MUL_RCP
|
4995 EXP_TO_EXP2
| LOG_TO_LOG2
;
4996 if (options
->EmitNoPow
)
4997 what_to_lower
|= POW_TO_EXP2
;
4998 if (!ctx
->Const
.NativeIntegers
)
4999 what_to_lower
|= INT_DIV_TO_MUL_RCP
;
5004 do_mat_op_to_vec(ir
);
5005 lower_instructions(ir
, what_to_lower
);
5007 progress
= do_lower_jumps(ir
, true, true, options
->EmitNoMainReturn
, options
->EmitNoCont
, options
->EmitNoLoops
) || progress
;
5009 progress
= do_common_optimization(ir
, true, true,
5010 options
->MaxUnrollIterations
)
5013 progress
= lower_quadop_vector(ir
, false) || progress
;
5014 progress
= lower_clip_distance(ir
) || progress
;
5016 if (options
->MaxIfDepth
== 0)
5017 progress
= lower_discard(ir
) || progress
;
5019 progress
= lower_if_to_cond_assign(ir
, options
->MaxIfDepth
) || progress
;
5021 if (options
->EmitNoNoise
)
5022 progress
= lower_noise(ir
) || progress
;
5024 /* If there are forms of indirect addressing that the driver
5025 * cannot handle, perform the lowering pass.
5027 if (options
->EmitNoIndirectInput
|| options
->EmitNoIndirectOutput
5028 || options
->EmitNoIndirectTemp
|| options
->EmitNoIndirectUniform
)
5030 lower_variable_index_to_cond_assign(ir
,
5031 options
->EmitNoIndirectInput
,
5032 options
->EmitNoIndirectOutput
,
5033 options
->EmitNoIndirectTemp
,
5034 options
->EmitNoIndirectUniform
)
5037 progress
= do_vec_index_to_cond_assign(ir
) || progress
;
5040 validate_ir_tree(ir
);
5043 for (unsigned i
= 0; i
< MESA_SHADER_TYPES
; i
++) {
5044 struct gl_program
*linked_prog
;
5046 if (prog
->_LinkedShaders
[i
] == NULL
)
5049 linked_prog
= get_mesa_program(ctx
, prog
, prog
->_LinkedShaders
[i
],
5050 num_clip_distances
[i
]);
5053 static const GLenum targets
[] = {
5054 GL_VERTEX_PROGRAM_ARB
,
5055 GL_FRAGMENT_PROGRAM_ARB
,
5056 GL_GEOMETRY_PROGRAM_NV
5059 _mesa_reference_program(ctx
, &prog
->_LinkedShaders
[i
]->Program
,
5061 if (!ctx
->Driver
.ProgramStringNotify(ctx
, targets
[i
], linked_prog
)) {
5062 _mesa_reference_program(ctx
, &prog
->_LinkedShaders
[i
]->Program
,
5064 _mesa_reference_program(ctx
, &linked_prog
, NULL
);
5069 _mesa_reference_program(ctx
, &linked_prog
, NULL
);
5076 st_translate_stream_output_info(glsl_to_tgsi_visitor
*glsl_to_tgsi
,
5077 const GLuint outputMapping
[],
5078 struct pipe_stream_output_info
*so
)
5081 struct gl_transform_feedback_info
*info
=
5082 &glsl_to_tgsi
->shader_program
->LinkedTransformFeedback
;
5084 for (i
= 0; i
< info
->NumOutputs
; i
++) {
5085 so
->output
[i
].register_index
=
5086 outputMapping
[info
->Outputs
[i
].OutputRegister
];
5087 so
->output
[i
].start_component
= info
->Outputs
[i
].ComponentOffset
;
5088 so
->output
[i
].num_components
= info
->Outputs
[i
].NumComponents
;
5089 so
->output
[i
].output_buffer
= info
->Outputs
[i
].OutputBuffer
;
5090 so
->output
[i
].dst_offset
= info
->Outputs
[i
].DstOffset
;
5093 for (i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
5094 so
->stride
[i
] = info
->BufferStride
[i
];
5096 so
->num_outputs
= info
->NumOutputs
;