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.
33 #include "st_glsl_to_tgsi.h"
35 #include "compiler/glsl/glsl_parser_extras.h"
36 #include "compiler/glsl/ir_optimization.h"
37 #include "compiler/glsl/program.h"
39 #include "main/errors.h"
40 #include "main/shaderobj.h"
41 #include "main/uniforms.h"
42 #include "main/shaderapi.h"
43 #include "main/shaderimage.h"
44 #include "program/prog_instruction.h"
46 #include "pipe/p_context.h"
47 #include "pipe/p_screen.h"
48 #include "tgsi/tgsi_ureg.h"
49 #include "tgsi/tgsi_info.h"
50 #include "util/u_math.h"
51 #include "util/u_memory.h"
52 #include "st_program.h"
53 #include "st_mesa_to_tgsi.h"
54 #include "st_format.h"
55 #include "st_glsl_types.h"
57 #include "st_shader_cache.h"
61 #define PROGRAM_ANY_CONST ((1 << PROGRAM_STATE_VAR) | \
62 (1 << PROGRAM_CONSTANT) | \
63 (1 << PROGRAM_UNIFORM))
65 #define MAX_GLSL_TEXTURE_OFFSET 4
70 static int swizzle_for_size(int size
);
72 static int swizzle_for_type(const glsl_type
*type
, int component
= 0)
74 unsigned num_elements
= 4;
77 type
= type
->without_array();
78 if (type
->is_scalar() || type
->is_vector() || type
->is_matrix())
79 num_elements
= type
->vector_elements
;
82 int swizzle
= swizzle_for_size(num_elements
);
83 assert(num_elements
+ component
<= 4);
85 swizzle
+= component
* MAKE_SWIZZLE4(1, 1, 1, 1);
90 * This struct is a corresponding struct to TGSI ureg_src.
94 st_src_reg(gl_register_file file
, int index
, const glsl_type
*type
,
95 int component
= 0, unsigned array_id
= 0)
97 assert(file
!= PROGRAM_ARRAY
|| array_id
!= 0);
100 this->swizzle
= swizzle_for_type(type
, component
);
104 this->type
= type
? type
->base_type
: GLSL_TYPE_ERROR
;
105 this->reladdr
= NULL
;
106 this->reladdr2
= NULL
;
107 this->has_index2
= false;
108 this->double_reg2
= false;
109 this->array_id
= array_id
;
110 this->is_double_vertex_input
= false;
113 st_src_reg(gl_register_file file
, int index
, enum glsl_base_type type
)
115 assert(file
!= PROGRAM_ARRAY
); /* need array_id > 0 */
120 this->swizzle
= SWIZZLE_XYZW
;
123 this->reladdr
= NULL
;
124 this->reladdr2
= NULL
;
125 this->has_index2
= false;
126 this->double_reg2
= false;
128 this->is_double_vertex_input
= false;
131 st_src_reg(gl_register_file file
, int index
, enum glsl_base_type type
, int index2D
)
133 assert(file
!= PROGRAM_ARRAY
); /* need array_id > 0 */
137 this->index2D
= index2D
;
138 this->swizzle
= SWIZZLE_XYZW
;
141 this->reladdr
= NULL
;
142 this->reladdr2
= NULL
;
143 this->has_index2
= false;
144 this->double_reg2
= false;
146 this->is_double_vertex_input
= false;
151 this->type
= GLSL_TYPE_ERROR
;
152 this->file
= PROGRAM_UNDEFINED
;
158 this->reladdr
= NULL
;
159 this->reladdr2
= NULL
;
160 this->has_index2
= false;
161 this->double_reg2
= false;
163 this->is_double_vertex_input
= false;
166 explicit st_src_reg(st_dst_reg reg
);
168 int16_t index
; /**< temporary index, VERT_ATTRIB_*, VARYING_SLOT_*, etc. */
170 uint16_t swizzle
; /**< SWIZZLE_XYZWONEZERO swizzles from Mesa. */
171 int negate
:4; /**< NEGATE_XYZW mask from mesa */
173 enum glsl_base_type type
:5; /** GLSL_TYPE_* from GLSL IR (enum glsl_base_type) */
174 unsigned has_index2
:1;
175 gl_register_file file
:5; /**< PROGRAM_* from Mesa */
177 * Is this the second half of a double register pair?
178 * currently used for input mapping only.
180 unsigned double_reg2
:1;
181 unsigned is_double_vertex_input
:1;
182 unsigned array_id
:10;
184 /** Register index should be offset by the integer in this reg. */
186 st_src_reg
*reladdr2
;
190 st_src_reg reg
= *this;
199 st_dst_reg(gl_register_file file
, int writemask
, enum glsl_base_type type
, int index
)
201 assert(file
!= PROGRAM_ARRAY
); /* need array_id > 0 */
205 this->writemask
= writemask
;
206 this->reladdr
= NULL
;
207 this->reladdr2
= NULL
;
208 this->has_index2
= false;
213 st_dst_reg(gl_register_file file
, int writemask
, enum glsl_base_type type
)
215 assert(file
!= PROGRAM_ARRAY
); /* need array_id > 0 */
219 this->writemask
= writemask
;
220 this->reladdr
= NULL
;
221 this->reladdr2
= NULL
;
222 this->has_index2
= false;
229 this->type
= GLSL_TYPE_ERROR
;
230 this->file
= PROGRAM_UNDEFINED
;
234 this->reladdr
= NULL
;
235 this->reladdr2
= NULL
;
236 this->has_index2
= false;
240 explicit st_dst_reg(st_src_reg reg
);
242 int16_t index
; /**< temporary index, VERT_ATTRIB_*, VARYING_SLOT_*, etc. */
244 gl_register_file file
:5; /**< PROGRAM_* from Mesa */
245 unsigned writemask
:4; /**< Bitfield of WRITEMASK_[XYZW] */
246 enum glsl_base_type type
:5; /** GLSL_TYPE_* from GLSL IR (enum glsl_base_type) */
247 unsigned has_index2
:1;
248 unsigned array_id
:10;
250 /** Register index should be offset by the integer in this reg. */
252 st_src_reg
*reladdr2
;
255 st_src_reg::st_src_reg(st_dst_reg reg
)
257 this->type
= reg
.type
;
258 this->file
= reg
.file
;
259 this->index
= reg
.index
;
260 this->swizzle
= SWIZZLE_XYZW
;
263 this->reladdr
= reg
.reladdr
;
264 this->index2D
= reg
.index2D
;
265 this->reladdr2
= reg
.reladdr2
;
266 this->has_index2
= reg
.has_index2
;
267 this->double_reg2
= false;
268 this->array_id
= reg
.array_id
;
269 this->is_double_vertex_input
= false;
272 st_dst_reg::st_dst_reg(st_src_reg reg
)
274 this->type
= reg
.type
;
275 this->file
= reg
.file
;
276 this->index
= reg
.index
;
277 this->writemask
= WRITEMASK_XYZW
;
278 this->reladdr
= reg
.reladdr
;
279 this->index2D
= reg
.index2D
;
280 this->reladdr2
= reg
.reladdr2
;
281 this->has_index2
= reg
.has_index2
;
282 this->array_id
= reg
.array_id
;
285 class glsl_to_tgsi_instruction
: public exec_node
{
287 DECLARE_RALLOC_CXX_OPERATORS(glsl_to_tgsi_instruction
)
291 st_src_reg resource
; /**< sampler or buffer register */
292 st_src_reg
*tex_offsets
;
294 /** Pointer to the ir source this tree came from for debugging */
297 unsigned op
:8; /**< TGSI opcode */
299 unsigned is_64bit_expanded
:1;
300 unsigned sampler_base
:5;
301 unsigned sampler_array_size
:6; /**< 1-based size of sampler array, 1 if not array */
302 unsigned tex_target
:4; /**< One of TEXTURE_*_INDEX */
303 glsl_base_type tex_type
:5;
304 unsigned tex_shadow
:1;
305 unsigned image_format
:9;
306 unsigned tex_offset_num_offset
:3;
307 unsigned dead_mask
:4; /**< Used in dead code elimination */
308 unsigned buffer_access
:3; /**< buffer access type */
310 const struct tgsi_opcode_info
*info
;
313 class variable_storage
: public exec_node
{
315 variable_storage(ir_variable
*var
, gl_register_file file
, int index
,
316 unsigned array_id
= 0)
317 : file(file
), index(index
), component(0), var(var
), array_id(array_id
)
319 assert(file
!= PROGRAM_ARRAY
|| array_id
!= 0);
322 gl_register_file file
;
325 /* Explicit component location. This is given in terms of the GLSL-style
326 * swizzles where each double is a single component, i.e. for 64-bit types
327 * it can only be 0 or 1.
330 ir_variable
*var
; /* variable that maps to this, if any */
334 class immediate_storage
: public exec_node
{
336 immediate_storage(gl_constant_value
*values
, int size32
, int type
)
338 memcpy(this->values
, values
, size32
* sizeof(gl_constant_value
));
339 this->size32
= size32
;
343 /* doubles are stored across 2 gl_constant_values */
344 gl_constant_value values
[4];
345 int size32
; /**< Number of 32-bit components (1-4) */
346 int type
; /**< GL_DOUBLE, GL_FLOAT, GL_INT, GL_BOOL, or GL_UNSIGNED_INT */
349 static st_src_reg undef_src
= st_src_reg(PROGRAM_UNDEFINED
, 0, GLSL_TYPE_ERROR
);
350 static st_dst_reg undef_dst
= st_dst_reg(PROGRAM_UNDEFINED
, SWIZZLE_NOOP
, GLSL_TYPE_ERROR
);
354 unsigned array_id
; /* TGSI ArrayID; 1-based: 0 means not an array */
357 unsigned gs_out_streams
;
358 enum glsl_interp_mode interp
;
359 enum glsl_base_type base_type
;
360 ubyte usage_mask
; /* GLSL-style usage-mask, i.e. single bit per double */
363 static struct inout_decl
*
364 find_inout_array(struct inout_decl
*decls
, unsigned count
, unsigned array_id
)
366 assert(array_id
!= 0);
368 for (unsigned i
= 0; i
< count
; i
++) {
369 struct inout_decl
*decl
= &decls
[i
];
371 if (array_id
== decl
->array_id
) {
379 static enum glsl_base_type
380 find_array_type(struct inout_decl
*decls
, unsigned count
, unsigned array_id
)
383 return GLSL_TYPE_ERROR
;
384 struct inout_decl
*decl
= find_inout_array(decls
, count
, array_id
);
386 return decl
->base_type
;
387 return GLSL_TYPE_ERROR
;
390 struct rename_reg_pair
{
395 struct glsl_to_tgsi_visitor
: public ir_visitor
{
397 glsl_to_tgsi_visitor();
398 ~glsl_to_tgsi_visitor();
400 struct gl_context
*ctx
;
401 struct gl_program
*prog
;
402 struct gl_shader_program
*shader_program
;
403 struct gl_linked_shader
*shader
;
404 struct gl_shader_compiler_options
*options
;
408 unsigned *array_sizes
;
409 unsigned max_num_arrays
;
412 struct inout_decl inputs
[4 * PIPE_MAX_SHADER_INPUTS
];
414 unsigned num_input_arrays
;
415 struct inout_decl outputs
[4 * PIPE_MAX_SHADER_OUTPUTS
];
416 unsigned num_outputs
;
417 unsigned num_output_arrays
;
419 int num_address_regs
;
420 uint32_t samplers_used
;
421 glsl_base_type sampler_types
[PIPE_MAX_SAMPLERS
];
422 int sampler_targets
[PIPE_MAX_SAMPLERS
]; /**< One of TGSI_TEXTURE_* */
425 int image_targets
[PIPE_MAX_SHADER_IMAGES
];
426 unsigned image_formats
[PIPE_MAX_SHADER_IMAGES
];
427 bool indirect_addr_consts
;
428 int wpos_transform_const
;
431 bool native_integers
;
434 bool use_shared_memory
;
437 variable_storage
*find_variable_storage(ir_variable
*var
);
439 int add_constant(gl_register_file file
, gl_constant_value values
[8],
440 int size
, int datatype
, uint16_t *swizzle_out
);
442 st_src_reg
get_temp(const glsl_type
*type
);
443 void reladdr_to_temp(ir_instruction
*ir
, st_src_reg
*reg
, int *num_reladdr
);
445 st_src_reg
st_src_reg_for_double(double val
);
446 st_src_reg
st_src_reg_for_float(float val
);
447 st_src_reg
st_src_reg_for_int(int val
);
448 st_src_reg
st_src_reg_for_type(enum glsl_base_type type
, int val
);
451 * \name Visit methods
453 * As typical for the visitor pattern, there must be one \c visit method for
454 * each concrete subclass of \c ir_instruction. Virtual base classes within
455 * the hierarchy should not have \c visit methods.
458 virtual void visit(ir_variable
*);
459 virtual void visit(ir_loop
*);
460 virtual void visit(ir_loop_jump
*);
461 virtual void visit(ir_function_signature
*);
462 virtual void visit(ir_function
*);
463 virtual void visit(ir_expression
*);
464 virtual void visit(ir_swizzle
*);
465 virtual void visit(ir_dereference_variable
*);
466 virtual void visit(ir_dereference_array
*);
467 virtual void visit(ir_dereference_record
*);
468 virtual void visit(ir_assignment
*);
469 virtual void visit(ir_constant
*);
470 virtual void visit(ir_call
*);
471 virtual void visit(ir_return
*);
472 virtual void visit(ir_discard
*);
473 virtual void visit(ir_texture
*);
474 virtual void visit(ir_if
*);
475 virtual void visit(ir_emit_vertex
*);
476 virtual void visit(ir_end_primitive
*);
477 virtual void visit(ir_barrier
*);
480 void visit_expression(ir_expression
*, st_src_reg
*) ATTRIBUTE_NOINLINE
;
482 void visit_atomic_counter_intrinsic(ir_call
*);
483 void visit_ssbo_intrinsic(ir_call
*);
484 void visit_membar_intrinsic(ir_call
*);
485 void visit_shared_intrinsic(ir_call
*);
486 void visit_image_intrinsic(ir_call
*);
487 void visit_generic_intrinsic(ir_call
*, unsigned op
);
491 /** List of variable_storage */
494 /** List of immediate_storage */
495 exec_list immediates
;
496 unsigned num_immediates
;
498 /** List of glsl_to_tgsi_instruction */
499 exec_list instructions
;
501 glsl_to_tgsi_instruction
*emit_asm(ir_instruction
*ir
, unsigned op
,
502 st_dst_reg dst
= undef_dst
,
503 st_src_reg src0
= undef_src
,
504 st_src_reg src1
= undef_src
,
505 st_src_reg src2
= undef_src
,
506 st_src_reg src3
= undef_src
);
508 glsl_to_tgsi_instruction
*emit_asm(ir_instruction
*ir
, unsigned op
,
509 st_dst_reg dst
, st_dst_reg dst1
,
510 st_src_reg src0
= undef_src
,
511 st_src_reg src1
= undef_src
,
512 st_src_reg src2
= undef_src
,
513 st_src_reg src3
= undef_src
);
515 unsigned get_opcode(unsigned op
,
517 st_src_reg src0
, st_src_reg src1
);
520 * Emit the correct dot-product instruction for the type of arguments
522 glsl_to_tgsi_instruction
*emit_dp(ir_instruction
*ir
,
528 void emit_scalar(ir_instruction
*ir
, unsigned op
,
529 st_dst_reg dst
, st_src_reg src0
);
531 void emit_scalar(ir_instruction
*ir
, unsigned op
,
532 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
);
534 void emit_arl(ir_instruction
*ir
, st_dst_reg dst
, st_src_reg src0
);
536 void get_deref_offsets(ir_dereference
*ir
,
537 unsigned *array_size
,
542 void calc_deref_offsets(ir_dereference
*tail
,
543 unsigned *array_elements
,
545 st_src_reg
*indirect
,
547 st_src_reg
canonicalize_gather_offset(st_src_reg offset
);
549 bool try_emit_mad(ir_expression
*ir
,
551 bool try_emit_mad_for_and_not(ir_expression
*ir
,
554 void emit_swz(ir_expression
*ir
);
556 bool process_move_condition(ir_rvalue
*ir
);
558 void simplify_cmp(void);
560 void rename_temp_registers(int num_renames
, struct rename_reg_pair
*renames
);
561 void get_first_temp_read(int *first_reads
);
562 void get_last_temp_read_first_temp_write(int *last_reads
, int *first_writes
);
563 void get_last_temp_write(int *last_writes
);
565 void copy_propagate(void);
566 int eliminate_dead_code(void);
568 void merge_two_dsts(void);
569 void merge_registers(void);
570 void renumber_registers(void);
572 void emit_block_mov(ir_assignment
*ir
, const struct glsl_type
*type
,
573 st_dst_reg
*l
, st_src_reg
*r
,
574 st_src_reg
*cond
, bool cond_swap
);
579 static st_dst_reg address_reg
= st_dst_reg(PROGRAM_ADDRESS
, WRITEMASK_X
, GLSL_TYPE_FLOAT
, 0);
580 static st_dst_reg address_reg2
= st_dst_reg(PROGRAM_ADDRESS
, WRITEMASK_X
, GLSL_TYPE_FLOAT
, 1);
581 static st_dst_reg sampler_reladdr
= st_dst_reg(PROGRAM_ADDRESS
, WRITEMASK_X
, GLSL_TYPE_FLOAT
, 2);
584 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...) PRINTFLIKE(2, 3);
587 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...)
591 ralloc_vasprintf_append(&prog
->data
->InfoLog
, fmt
, args
);
594 prog
->data
->LinkStatus
= linking_failure
;
598 swizzle_for_size(int size
)
600 static const int size_swizzles
[4] = {
601 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
),
602 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
),
603 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
),
604 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
607 assert((size
>= 1) && (size
<= 4));
608 return size_swizzles
[size
- 1];
612 is_resource_instruction(unsigned opcode
)
615 case TGSI_OPCODE_RESQ
:
616 case TGSI_OPCODE_LOAD
:
617 case TGSI_OPCODE_ATOMUADD
:
618 case TGSI_OPCODE_ATOMXCHG
:
619 case TGSI_OPCODE_ATOMCAS
:
620 case TGSI_OPCODE_ATOMAND
:
621 case TGSI_OPCODE_ATOMOR
:
622 case TGSI_OPCODE_ATOMXOR
:
623 case TGSI_OPCODE_ATOMUMIN
:
624 case TGSI_OPCODE_ATOMUMAX
:
625 case TGSI_OPCODE_ATOMIMIN
:
626 case TGSI_OPCODE_ATOMIMAX
:
634 num_inst_dst_regs(const glsl_to_tgsi_instruction
*op
)
636 return op
->info
->num_dst
;
640 num_inst_src_regs(const glsl_to_tgsi_instruction
*op
)
642 return op
->info
->is_tex
|| is_resource_instruction(op
->op
) ?
643 op
->info
->num_src
- 1 : op
->info
->num_src
;
646 glsl_to_tgsi_instruction
*
647 glsl_to_tgsi_visitor::emit_asm(ir_instruction
*ir
, unsigned op
,
648 st_dst_reg dst
, st_dst_reg dst1
,
649 st_src_reg src0
, st_src_reg src1
,
650 st_src_reg src2
, st_src_reg src3
)
652 glsl_to_tgsi_instruction
*inst
= new(mem_ctx
) glsl_to_tgsi_instruction();
653 int num_reladdr
= 0, i
, j
;
654 bool dst_is_64bit
[2];
656 op
= get_opcode(op
, dst
, src0
, src1
);
658 /* If we have to do relative addressing, we want to load the ARL
659 * reg directly for one of the regs, and preload the other reladdr
660 * sources into temps.
662 num_reladdr
+= dst
.reladdr
!= NULL
|| dst
.reladdr2
;
663 num_reladdr
+= dst1
.reladdr
!= NULL
|| dst1
.reladdr2
;
664 num_reladdr
+= src0
.reladdr
!= NULL
|| src0
.reladdr2
!= NULL
;
665 num_reladdr
+= src1
.reladdr
!= NULL
|| src1
.reladdr2
!= NULL
;
666 num_reladdr
+= src2
.reladdr
!= NULL
|| src2
.reladdr2
!= NULL
;
667 num_reladdr
+= src3
.reladdr
!= NULL
|| src3
.reladdr2
!= NULL
;
669 reladdr_to_temp(ir
, &src3
, &num_reladdr
);
670 reladdr_to_temp(ir
, &src2
, &num_reladdr
);
671 reladdr_to_temp(ir
, &src1
, &num_reladdr
);
672 reladdr_to_temp(ir
, &src0
, &num_reladdr
);
674 if (dst
.reladdr
|| dst
.reladdr2
) {
676 emit_arl(ir
, address_reg
, *dst
.reladdr
);
678 emit_arl(ir
, address_reg2
, *dst
.reladdr2
);
682 emit_arl(ir
, address_reg
, *dst1
.reladdr
);
685 assert(num_reladdr
== 0);
687 /* inst->op has only 8 bits. */
688 STATIC_ASSERT(TGSI_OPCODE_LAST
<= 255);
691 inst
->info
= tgsi_get_opcode_info(op
);
698 inst
->is_64bit_expanded
= false;
701 inst
->tex_offsets
= NULL
;
702 inst
->tex_offset_num_offset
= 0;
704 inst
->tex_shadow
= 0;
705 /* default to float, for paths where this is not initialized
706 * (since 0==UINT which is likely wrong):
708 inst
->tex_type
= GLSL_TYPE_FLOAT
;
710 /* Update indirect addressing status used by TGSI */
711 if (dst
.reladdr
|| dst
.reladdr2
) {
713 case PROGRAM_STATE_VAR
:
714 case PROGRAM_CONSTANT
:
715 case PROGRAM_UNIFORM
:
716 this->indirect_addr_consts
= true;
718 case PROGRAM_IMMEDIATE
:
719 assert(!"immediates should not have indirect addressing");
726 for (i
= 0; i
< 4; i
++) {
727 if(inst
->src
[i
].reladdr
) {
728 switch(inst
->src
[i
].file
) {
729 case PROGRAM_STATE_VAR
:
730 case PROGRAM_CONSTANT
:
731 case PROGRAM_UNIFORM
:
732 this->indirect_addr_consts
= true;
734 case PROGRAM_IMMEDIATE
:
735 assert(!"immediates should not have indirect addressing");
745 * This section contains the double processing.
746 * GLSL just represents doubles as single channel values,
747 * however most HW and TGSI represent doubles as pairs of register channels.
749 * so we have to fixup destination writemask/index and src swizzle/indexes.
750 * dest writemasks need to translate from single channel write mask
751 * to a dual-channel writemask, but also need to modify the index,
752 * if we are touching the Z,W fields in the pre-translated writemask.
754 * src channels have similiar index modifications along with swizzle
755 * changes to we pick the XY, ZW pairs from the correct index.
757 * GLSL [0].x -> TGSI [0].xy
758 * GLSL [0].y -> TGSI [0].zw
759 * GLSL [0].z -> TGSI [1].xy
760 * GLSL [0].w -> TGSI [1].zw
762 for (j
= 0; j
< 2; j
++) {
763 dst_is_64bit
[j
] = glsl_base_type_is_64bit(inst
->dst
[j
].type
);
764 if (!dst_is_64bit
[j
] && inst
->dst
[j
].file
== PROGRAM_OUTPUT
&& inst
->dst
[j
].type
== GLSL_TYPE_ARRAY
) {
765 enum glsl_base_type type
= find_array_type(this->outputs
, this->num_outputs
, inst
->dst
[j
].array_id
);
766 if (glsl_base_type_is_64bit(type
))
767 dst_is_64bit
[j
] = true;
771 if (dst_is_64bit
[0] || dst_is_64bit
[1] ||
772 glsl_base_type_is_64bit(inst
->src
[0].type
)) {
773 glsl_to_tgsi_instruction
*dinst
= NULL
;
774 int initial_src_swz
[4], initial_src_idx
[4];
775 int initial_dst_idx
[2], initial_dst_writemask
[2];
776 /* select the writemask for dst0 or dst1 */
777 unsigned writemask
= inst
->dst
[1].file
== PROGRAM_UNDEFINED
? inst
->dst
[0].writemask
: inst
->dst
[1].writemask
;
779 /* copy out the writemask, index and swizzles for all src/dsts. */
780 for (j
= 0; j
< 2; j
++) {
781 initial_dst_writemask
[j
] = inst
->dst
[j
].writemask
;
782 initial_dst_idx
[j
] = inst
->dst
[j
].index
;
785 for (j
= 0; j
< 4; j
++) {
786 initial_src_swz
[j
] = inst
->src
[j
].swizzle
;
787 initial_src_idx
[j
] = inst
->src
[j
].index
;
791 * scan all the components in the dst writemask
792 * generate an instruction for each of them if required.
797 int i
= u_bit_scan(&writemask
);
799 /* before emitting the instruction, see if we have to adjust load / store
801 if (i
> 1 && (inst
->op
== TGSI_OPCODE_LOAD
|| inst
->op
== TGSI_OPCODE_STORE
) &&
802 addr
.file
== PROGRAM_UNDEFINED
) {
803 /* We have to advance the buffer address by 16 */
804 addr
= get_temp(glsl_type::uint_type
);
805 emit_asm(ir
, TGSI_OPCODE_UADD
, st_dst_reg(addr
),
806 inst
->src
[0], st_src_reg_for_int(16));
809 /* first time use previous instruction */
813 /* create a new instructions for subsequent attempts */
814 dinst
= new(mem_ctx
) glsl_to_tgsi_instruction();
819 this->instructions
.push_tail(dinst
);
820 dinst
->is_64bit_expanded
= true;
822 /* modify the destination if we are splitting */
823 for (j
= 0; j
< 2; j
++) {
824 if (dst_is_64bit
[j
]) {
825 dinst
->dst
[j
].writemask
= (i
& 1) ? WRITEMASK_ZW
: WRITEMASK_XY
;
826 dinst
->dst
[j
].index
= initial_dst_idx
[j
];
828 if (dinst
->op
== TGSI_OPCODE_LOAD
|| dinst
->op
== TGSI_OPCODE_STORE
)
829 dinst
->src
[0] = addr
;
830 if (dinst
->op
!= TGSI_OPCODE_STORE
)
831 dinst
->dst
[j
].index
++;
834 /* if we aren't writing to a double, just get the bit of the initial writemask
836 dinst
->dst
[j
].writemask
= initial_dst_writemask
[j
] & (1 << i
);
840 /* modify the src registers */
841 for (j
= 0; j
< 4; j
++) {
842 int swz
= GET_SWZ(initial_src_swz
[j
], i
);
844 if (glsl_base_type_is_64bit(dinst
->src
[j
].type
)) {
845 dinst
->src
[j
].index
= initial_src_idx
[j
];
847 dinst
->src
[j
].double_reg2
= true;
848 dinst
->src
[j
].index
++;
852 dinst
->src
[j
].swizzle
= MAKE_SWIZZLE4(SWIZZLE_Z
, SWIZZLE_W
, SWIZZLE_Z
, SWIZZLE_W
);
854 dinst
->src
[j
].swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_X
, SWIZZLE_Y
);
857 /* some opcodes are special case in what they use as sources
858 - [FUI]2D/[UI]2I64 is a float/[u]int src0, DLDEXP is integer src1 */
859 if (op
== TGSI_OPCODE_F2D
|| op
== TGSI_OPCODE_U2D
|| op
== TGSI_OPCODE_I2D
||
860 op
== TGSI_OPCODE_I2I64
|| op
== TGSI_OPCODE_U2I64
||
861 op
== TGSI_OPCODE_DLDEXP
||
862 (op
== TGSI_OPCODE_UCMP
&& dst_is_64bit
[0])) {
863 dinst
->src
[j
].swizzle
= MAKE_SWIZZLE4(swz
, swz
, swz
, swz
);
870 this->instructions
.push_tail(inst
);
877 glsl_to_tgsi_instruction
*
878 glsl_to_tgsi_visitor::emit_asm(ir_instruction
*ir
, unsigned op
,
880 st_src_reg src0
, st_src_reg src1
,
881 st_src_reg src2
, st_src_reg src3
)
883 return emit_asm(ir
, op
, dst
, undef_dst
, src0
, src1
, src2
, src3
);
887 * Determines whether to use an integer, unsigned integer, or float opcode
888 * based on the operands and input opcode, then emits the result.
891 glsl_to_tgsi_visitor::get_opcode(unsigned op
,
893 st_src_reg src0
, st_src_reg src1
)
895 enum glsl_base_type type
= GLSL_TYPE_FLOAT
;
897 if (op
== TGSI_OPCODE_MOV
)
900 assert(src0
.type
!= GLSL_TYPE_ARRAY
);
901 assert(src0
.type
!= GLSL_TYPE_STRUCT
);
902 assert(src1
.type
!= GLSL_TYPE_ARRAY
);
903 assert(src1
.type
!= GLSL_TYPE_STRUCT
);
905 if (is_resource_instruction(op
))
907 else if (src0
.type
== GLSL_TYPE_INT64
|| src1
.type
== GLSL_TYPE_INT64
)
908 type
= GLSL_TYPE_INT64
;
909 else if (src0
.type
== GLSL_TYPE_UINT64
|| src1
.type
== GLSL_TYPE_UINT64
)
910 type
= GLSL_TYPE_UINT64
;
911 else if (src0
.type
== GLSL_TYPE_DOUBLE
|| src1
.type
== GLSL_TYPE_DOUBLE
)
912 type
= GLSL_TYPE_DOUBLE
;
913 else if (src0
.type
== GLSL_TYPE_FLOAT
|| src1
.type
== GLSL_TYPE_FLOAT
)
914 type
= GLSL_TYPE_FLOAT
;
915 else if (native_integers
)
916 type
= src0
.type
== GLSL_TYPE_BOOL
? GLSL_TYPE_INT
: src0
.type
;
918 #define case7(c, f, i, u, d, i64, ui64) \
919 case TGSI_OPCODE_##c: \
920 if (type == GLSL_TYPE_UINT64) \
921 op = TGSI_OPCODE_##ui64; \
922 else if (type == GLSL_TYPE_INT64) \
923 op = TGSI_OPCODE_##i64; \
924 else if (type == GLSL_TYPE_DOUBLE) \
925 op = TGSI_OPCODE_##d; \
926 else if (type == GLSL_TYPE_INT) \
927 op = TGSI_OPCODE_##i; \
928 else if (type == GLSL_TYPE_UINT) \
929 op = TGSI_OPCODE_##u; \
931 op = TGSI_OPCODE_##f; \
933 #define case5(c, f, i, u, d) \
934 case TGSI_OPCODE_##c: \
935 if (type == GLSL_TYPE_DOUBLE) \
936 op = TGSI_OPCODE_##d; \
937 else if (type == GLSL_TYPE_INT) \
938 op = TGSI_OPCODE_##i; \
939 else if (type == GLSL_TYPE_UINT) \
940 op = TGSI_OPCODE_##u; \
942 op = TGSI_OPCODE_##f; \
945 #define case4(c, f, i, u) \
946 case TGSI_OPCODE_##c: \
947 if (type == GLSL_TYPE_INT) \
948 op = TGSI_OPCODE_##i; \
949 else if (type == GLSL_TYPE_UINT) \
950 op = TGSI_OPCODE_##u; \
952 op = TGSI_OPCODE_##f; \
955 #define case3(f, i, u) case4(f, f, i, u)
956 #define case6d(f, i, u, d, i64, u64) case7(f, f, i, u, d, i64, u64)
957 #define case3fid(f, i, d) case5(f, f, i, i, d)
958 #define case3fid64(f, i, d, i64) case7(f, f, i, i, d, i64, i64)
959 #define case2fi(f, i) case4(f, f, i, i)
960 #define case2iu(i, u) case4(i, LAST, i, u)
962 #define case2iu64(i, i64) case7(i, LAST, i, i, LAST, i64, i64)
963 #define case4iu64(i, u, i64, u64) case7(i, LAST, i, u, LAST, i64, u64)
965 #define casecomp(c, f, i, u, d, i64, ui64) \
966 case TGSI_OPCODE_##c: \
967 if (type == GLSL_TYPE_INT64) \
968 op = TGSI_OPCODE_##i64; \
969 else if (type == GLSL_TYPE_UINT64) \
970 op = TGSI_OPCODE_##ui64; \
971 else if (type == GLSL_TYPE_DOUBLE) \
972 op = TGSI_OPCODE_##d; \
973 else if (type == GLSL_TYPE_INT || type == GLSL_TYPE_SUBROUTINE) \
974 op = TGSI_OPCODE_##i; \
975 else if (type == GLSL_TYPE_UINT) \
976 op = TGSI_OPCODE_##u; \
977 else if (native_integers) \
978 op = TGSI_OPCODE_##f; \
980 op = TGSI_OPCODE_##c; \
984 case3fid64(ADD
, UADD
, DADD
, U64ADD
);
985 case3fid64(MUL
, UMUL
, DMUL
, U64MUL
);
986 case3fid(MAD
, UMAD
, DMAD
);
987 case3fid(FMA
, UMAD
, DFMA
);
988 case6d(DIV
, IDIV
, UDIV
, DDIV
, I64DIV
, U64DIV
);
989 case6d(MAX
, IMAX
, UMAX
, DMAX
, I64MAX
, U64MAX
);
990 case6d(MIN
, IMIN
, UMIN
, DMIN
, I64MIN
, U64MIN
);
991 case4iu64(MOD
, UMOD
, I64MOD
, U64MOD
);
993 casecomp(SEQ
, FSEQ
, USEQ
, USEQ
, DSEQ
, U64SEQ
, U64SEQ
);
994 casecomp(SNE
, FSNE
, USNE
, USNE
, DSNE
, U64SNE
, U64SNE
);
995 casecomp(SGE
, FSGE
, ISGE
, USGE
, DSGE
, I64SGE
, U64SGE
);
996 casecomp(SLT
, FSLT
, ISLT
, USLT
, DSLT
, I64SLT
, U64SLT
);
998 case2iu64(SHL
, U64SHL
);
999 case4iu64(ISHR
, USHR
, I64SHR
, U64SHR
);
1001 case3fid64(SSG
, ISSG
, DSSG
, I64SSG
);
1003 case2iu(IBFE
, UBFE
);
1004 case2iu(IMSB
, UMSB
);
1005 case2iu(IMUL_HI
, UMUL_HI
);
1007 case3fid(SQRT
, SQRT
, DSQRT
);
1009 case3fid(RCP
, RCP
, DRCP
);
1010 case3fid(RSQ
, RSQ
, DRSQ
);
1012 case3fid(FRC
, FRC
, DFRAC
);
1013 case3fid(TRUNC
, TRUNC
, DTRUNC
);
1014 case3fid(CEIL
, CEIL
, DCEIL
);
1015 case3fid(FLR
, FLR
, DFLR
);
1016 case3fid(ROUND
, ROUND
, DROUND
);
1018 case2iu(ATOMIMAX
, ATOMUMAX
);
1019 case2iu(ATOMIMIN
, ATOMUMIN
);
1024 assert(op
!= TGSI_OPCODE_LAST
);
1028 glsl_to_tgsi_instruction
*
1029 glsl_to_tgsi_visitor::emit_dp(ir_instruction
*ir
,
1030 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
,
1033 static const unsigned dot_opcodes
[] = {
1034 TGSI_OPCODE_DP2
, TGSI_OPCODE_DP3
, TGSI_OPCODE_DP4
1037 return emit_asm(ir
, dot_opcodes
[elements
- 2], dst
, src0
, src1
);
1041 * Emits TGSI scalar opcodes to produce unique answers across channels.
1043 * Some TGSI opcodes are scalar-only, like ARB_fp/vp. The src X
1044 * channel determines the result across all channels. So to do a vec4
1045 * of this operation, we want to emit a scalar per source channel used
1046 * to produce dest channels.
1049 glsl_to_tgsi_visitor::emit_scalar(ir_instruction
*ir
, unsigned op
,
1051 st_src_reg orig_src0
, st_src_reg orig_src1
)
1054 int done_mask
= ~dst
.writemask
;
1056 /* TGSI RCP is a scalar operation splatting results to all channels,
1057 * like ARB_fp/vp. So emit as many RCPs as necessary to cover our
1060 for (i
= 0; i
< 4; i
++) {
1061 GLuint this_mask
= (1 << i
);
1062 st_src_reg src0
= orig_src0
;
1063 st_src_reg src1
= orig_src1
;
1065 if (done_mask
& this_mask
)
1068 GLuint src0_swiz
= GET_SWZ(src0
.swizzle
, i
);
1069 GLuint src1_swiz
= GET_SWZ(src1
.swizzle
, i
);
1070 for (j
= i
+ 1; j
< 4; j
++) {
1071 /* If there is another enabled component in the destination that is
1072 * derived from the same inputs, generate its value on this pass as
1075 if (!(done_mask
& (1 << j
)) &&
1076 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
&&
1077 GET_SWZ(src1
.swizzle
, j
) == src1_swiz
) {
1078 this_mask
|= (1 << j
);
1081 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
1082 src0_swiz
, src0_swiz
);
1083 src1
.swizzle
= MAKE_SWIZZLE4(src1_swiz
, src1_swiz
,
1084 src1_swiz
, src1_swiz
);
1086 dst
.writemask
= this_mask
;
1087 emit_asm(ir
, op
, dst
, src0
, src1
);
1088 done_mask
|= this_mask
;
1093 glsl_to_tgsi_visitor::emit_scalar(ir_instruction
*ir
, unsigned op
,
1094 st_dst_reg dst
, st_src_reg src0
)
1096 st_src_reg undef
= undef_src
;
1098 undef
.swizzle
= SWIZZLE_XXXX
;
1100 emit_scalar(ir
, op
, dst
, src0
, undef
);
1104 glsl_to_tgsi_visitor::emit_arl(ir_instruction
*ir
,
1105 st_dst_reg dst
, st_src_reg src0
)
1107 int op
= TGSI_OPCODE_ARL
;
1109 if (src0
.type
== GLSL_TYPE_INT
|| src0
.type
== GLSL_TYPE_UINT
)
1110 op
= TGSI_OPCODE_UARL
;
1112 assert(dst
.file
== PROGRAM_ADDRESS
);
1113 if (dst
.index
>= this->num_address_regs
)
1114 this->num_address_regs
= dst
.index
+ 1;
1116 emit_asm(NULL
, op
, dst
, src0
);
1120 glsl_to_tgsi_visitor::add_constant(gl_register_file file
,
1121 gl_constant_value values
[8], int size
, int datatype
,
1122 uint16_t *swizzle_out
)
1124 if (file
== PROGRAM_CONSTANT
) {
1125 GLuint swizzle
= swizzle_out
? *swizzle_out
: 0;
1126 int result
= _mesa_add_typed_unnamed_constant(this->prog
->Parameters
, values
,
1127 size
, datatype
, &swizzle
);
1129 *swizzle_out
= swizzle
;
1133 assert(file
== PROGRAM_IMMEDIATE
);
1136 immediate_storage
*entry
;
1137 int size32
= size
* ((datatype
== GL_DOUBLE
||
1138 datatype
== GL_INT64_ARB
||
1139 datatype
== GL_UNSIGNED_INT64_ARB
)? 2 : 1);
1142 /* Search immediate storage to see if we already have an identical
1143 * immediate that we can use instead of adding a duplicate entry.
1145 foreach_in_list(immediate_storage
, entry
, &this->immediates
) {
1146 immediate_storage
*tmp
= entry
;
1148 for (i
= 0; i
* 4 < size32
; i
++) {
1149 int slot_size
= MIN2(size32
- (i
* 4), 4);
1150 if (tmp
->type
!= datatype
|| tmp
->size32
!= slot_size
)
1152 if (memcmp(tmp
->values
, &values
[i
* 4],
1153 slot_size
* sizeof(gl_constant_value
)))
1156 /* Everything matches, keep going until the full size is matched */
1157 tmp
= (immediate_storage
*)tmp
->next
;
1160 /* The full value matched */
1161 if (i
* 4 >= size32
)
1167 for (i
= 0; i
* 4 < size32
; i
++) {
1168 int slot_size
= MIN2(size32
- (i
* 4), 4);
1169 /* Add this immediate to the list. */
1170 entry
= new(mem_ctx
) immediate_storage(&values
[i
* 4], slot_size
, datatype
);
1171 this->immediates
.push_tail(entry
);
1172 this->num_immediates
++;
1178 glsl_to_tgsi_visitor::st_src_reg_for_float(float val
)
1180 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_FLOAT
);
1181 union gl_constant_value uval
;
1184 src
.index
= add_constant(src
.file
, &uval
, 1, GL_FLOAT
, &src
.swizzle
);
1190 glsl_to_tgsi_visitor::st_src_reg_for_double(double val
)
1192 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_DOUBLE
);
1193 union gl_constant_value uval
[2];
1195 memcpy(uval
, &val
, sizeof(uval
));
1196 src
.index
= add_constant(src
.file
, uval
, 1, GL_DOUBLE
, &src
.swizzle
);
1197 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_X
, SWIZZLE_Y
);
1202 glsl_to_tgsi_visitor::st_src_reg_for_int(int val
)
1204 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_INT
);
1205 union gl_constant_value uval
;
1207 assert(native_integers
);
1210 src
.index
= add_constant(src
.file
, &uval
, 1, GL_INT
, &src
.swizzle
);
1216 glsl_to_tgsi_visitor::st_src_reg_for_type(enum glsl_base_type type
, int val
)
1218 if (native_integers
)
1219 return type
== GLSL_TYPE_FLOAT
? st_src_reg_for_float(val
) :
1220 st_src_reg_for_int(val
);
1222 return st_src_reg_for_float(val
);
1226 attrib_type_size(const struct glsl_type
*type
, bool is_vs_input
)
1228 return st_glsl_attrib_type_size(type
, is_vs_input
);
1232 type_size(const struct glsl_type
*type
)
1234 return st_glsl_type_size(type
);
1238 * If the given GLSL type is an array or matrix or a structure containing
1239 * an array/matrix member, return true. Else return false.
1241 * This is used to determine which kind of temp storage (PROGRAM_TEMPORARY
1242 * or PROGRAM_ARRAY) should be used for variables of this type. Anytime
1243 * we have an array that might be indexed with a variable, we need to use
1244 * the later storage type.
1247 type_has_array_or_matrix(const glsl_type
*type
)
1249 if (type
->is_array() || type
->is_matrix())
1252 if (type
->is_record()) {
1253 for (unsigned i
= 0; i
< type
->length
; i
++) {
1254 if (type_has_array_or_matrix(type
->fields
.structure
[i
].type
)) {
1265 * In the initial pass of codegen, we assign temporary numbers to
1266 * intermediate results. (not SSA -- variable assignments will reuse
1270 glsl_to_tgsi_visitor::get_temp(const glsl_type
*type
)
1274 src
.type
= native_integers
? type
->base_type
: GLSL_TYPE_FLOAT
;
1279 if (!options
->EmitNoIndirectTemp
&& type_has_array_or_matrix(type
)) {
1280 if (next_array
>= max_num_arrays
) {
1281 max_num_arrays
+= 32;
1282 array_sizes
= (unsigned*)
1283 realloc(array_sizes
, sizeof(array_sizes
[0]) * max_num_arrays
);
1286 src
.file
= PROGRAM_ARRAY
;
1288 src
.array_id
= next_array
+ 1;
1289 array_sizes
[next_array
] = type_size(type
);
1293 src
.file
= PROGRAM_TEMPORARY
;
1294 src
.index
= next_temp
;
1295 next_temp
+= type_size(type
);
1298 if (type
->is_array() || type
->is_record()) {
1299 src
.swizzle
= SWIZZLE_NOOP
;
1301 src
.swizzle
= swizzle_for_size(type
->vector_elements
);
1308 glsl_to_tgsi_visitor::find_variable_storage(ir_variable
*var
)
1311 foreach_in_list(variable_storage
, entry
, &this->variables
) {
1312 if (entry
->var
== var
)
1320 glsl_to_tgsi_visitor::visit(ir_variable
*ir
)
1322 if (strcmp(ir
->name
, "gl_FragCoord") == 0) {
1323 this->prog
->OriginUpperLeft
= ir
->data
.origin_upper_left
;
1324 this->prog
->PixelCenterInteger
= ir
->data
.pixel_center_integer
;
1327 if (ir
->data
.mode
== ir_var_uniform
&& strncmp(ir
->name
, "gl_", 3) == 0) {
1329 const ir_state_slot
*const slots
= ir
->get_state_slots();
1330 assert(slots
!= NULL
);
1332 /* Check if this statevar's setup in the STATE file exactly
1333 * matches how we'll want to reference it as a
1334 * struct/array/whatever. If not, then we need to move it into
1335 * temporary storage and hope that it'll get copy-propagated
1338 for (i
= 0; i
< ir
->get_num_state_slots(); i
++) {
1339 if (slots
[i
].swizzle
!= SWIZZLE_XYZW
) {
1344 variable_storage
*storage
;
1346 if (i
== ir
->get_num_state_slots()) {
1347 /* We'll set the index later. */
1348 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_STATE_VAR
, -1);
1349 this->variables
.push_tail(storage
);
1353 /* The variable_storage constructor allocates slots based on the size
1354 * of the type. However, this had better match the number of state
1355 * elements that we're going to copy into the new temporary.
1357 assert((int) ir
->get_num_state_slots() == type_size(ir
->type
));
1359 dst
= st_dst_reg(get_temp(ir
->type
));
1361 storage
= new(mem_ctx
) variable_storage(ir
, dst
.file
, dst
.index
,
1364 this->variables
.push_tail(storage
);
1368 for (unsigned int i
= 0; i
< ir
->get_num_state_slots(); i
++) {
1369 int index
= _mesa_add_state_reference(this->prog
->Parameters
,
1370 (gl_state_index
*)slots
[i
].tokens
);
1372 if (storage
->file
== PROGRAM_STATE_VAR
) {
1373 if (storage
->index
== -1) {
1374 storage
->index
= index
;
1376 assert(index
== storage
->index
+ (int)i
);
1379 /* We use GLSL_TYPE_FLOAT here regardless of the actual type of
1380 * the data being moved since MOV does not care about the type of
1381 * data it is moving, and we don't want to declare registers with
1382 * array or struct types.
1384 st_src_reg
src(PROGRAM_STATE_VAR
, index
, GLSL_TYPE_FLOAT
);
1385 src
.swizzle
= slots
[i
].swizzle
;
1386 emit_asm(ir
, TGSI_OPCODE_MOV
, dst
, src
);
1387 /* even a float takes up a whole vec4 reg in a struct/array. */
1392 if (storage
->file
== PROGRAM_TEMPORARY
&&
1393 dst
.index
!= storage
->index
+ (int) ir
->get_num_state_slots()) {
1394 fail_link(this->shader_program
,
1395 "failed to load builtin uniform `%s' (%d/%d regs loaded)\n",
1396 ir
->name
, dst
.index
- storage
->index
,
1397 type_size(ir
->type
));
1403 glsl_to_tgsi_visitor::visit(ir_loop
*ir
)
1405 emit_asm(NULL
, TGSI_OPCODE_BGNLOOP
);
1407 visit_exec_list(&ir
->body_instructions
, this);
1409 emit_asm(NULL
, TGSI_OPCODE_ENDLOOP
);
1413 glsl_to_tgsi_visitor::visit(ir_loop_jump
*ir
)
1416 case ir_loop_jump::jump_break
:
1417 emit_asm(NULL
, TGSI_OPCODE_BRK
);
1419 case ir_loop_jump::jump_continue
:
1420 emit_asm(NULL
, TGSI_OPCODE_CONT
);
1427 glsl_to_tgsi_visitor::visit(ir_function_signature
*ir
)
1434 glsl_to_tgsi_visitor::visit(ir_function
*ir
)
1436 /* Ignore function bodies other than main() -- we shouldn't see calls to
1437 * them since they should all be inlined before we get to glsl_to_tgsi.
1439 if (strcmp(ir
->name
, "main") == 0) {
1440 const ir_function_signature
*sig
;
1443 sig
= ir
->matching_signature(NULL
, &empty
, false);
1447 foreach_in_list(ir_instruction
, ir
, &sig
->body
) {
1454 glsl_to_tgsi_visitor::try_emit_mad(ir_expression
*ir
, int mul_operand
)
1456 int nonmul_operand
= 1 - mul_operand
;
1458 st_dst_reg result_dst
;
1460 ir_expression
*expr
= ir
->operands
[mul_operand
]->as_expression();
1461 if (!expr
|| expr
->operation
!= ir_binop_mul
)
1464 expr
->operands
[0]->accept(this);
1466 expr
->operands
[1]->accept(this);
1468 ir
->operands
[nonmul_operand
]->accept(this);
1471 this->result
= get_temp(ir
->type
);
1472 result_dst
= st_dst_reg(this->result
);
1473 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1474 emit_asm(ir
, TGSI_OPCODE_MAD
, result_dst
, a
, b
, c
);
1480 * Emit MAD(a, -b, a) instead of AND(a, NOT(b))
1482 * The logic values are 1.0 for true and 0.0 for false. Logical-and is
1483 * implemented using multiplication, and logical-or is implemented using
1484 * addition. Logical-not can be implemented as (true - x), or (1.0 - x).
1485 * As result, the logical expression (a & !b) can be rewritten as:
1489 * - (a * 1) - (a * b)
1493 * This final expression can be implemented as a single MAD(a, -b, a)
1497 glsl_to_tgsi_visitor::try_emit_mad_for_and_not(ir_expression
*ir
, int try_operand
)
1499 const int other_operand
= 1 - try_operand
;
1502 ir_expression
*expr
= ir
->operands
[try_operand
]->as_expression();
1503 if (!expr
|| expr
->operation
!= ir_unop_logic_not
)
1506 ir
->operands
[other_operand
]->accept(this);
1508 expr
->operands
[0]->accept(this);
1511 b
.negate
= ~b
.negate
;
1513 this->result
= get_temp(ir
->type
);
1514 emit_asm(ir
, TGSI_OPCODE_MAD
, st_dst_reg(this->result
), a
, b
, a
);
1520 glsl_to_tgsi_visitor::reladdr_to_temp(ir_instruction
*ir
,
1521 st_src_reg
*reg
, int *num_reladdr
)
1523 if (!reg
->reladdr
&& !reg
->reladdr2
)
1526 if (reg
->reladdr
) emit_arl(ir
, address_reg
, *reg
->reladdr
);
1527 if (reg
->reladdr2
) emit_arl(ir
, address_reg2
, *reg
->reladdr2
);
1529 if (*num_reladdr
!= 1) {
1530 st_src_reg temp
= get_temp(reg
->type
== GLSL_TYPE_DOUBLE
? glsl_type::dvec4_type
: glsl_type::vec4_type
);
1532 emit_asm(ir
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), *reg
);
1540 glsl_to_tgsi_visitor::visit(ir_expression
*ir
)
1542 st_src_reg op
[ARRAY_SIZE(ir
->operands
)];
1544 /* Quick peephole: Emit MAD(a, b, c) instead of ADD(MUL(a, b), c)
1546 if (ir
->operation
== ir_binop_add
) {
1547 if (try_emit_mad(ir
, 1))
1549 if (try_emit_mad(ir
, 0))
1553 /* Quick peephole: Emit OPCODE_MAD(-a, -b, a) instead of AND(a, NOT(b))
1555 if (!native_integers
&& ir
->operation
== ir_binop_logic_and
) {
1556 if (try_emit_mad_for_and_not(ir
, 1))
1558 if (try_emit_mad_for_and_not(ir
, 0))
1562 if (ir
->operation
== ir_quadop_vector
)
1563 assert(!"ir_quadop_vector should have been lowered");
1565 for (unsigned int operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
1566 this->result
.file
= PROGRAM_UNDEFINED
;
1567 ir
->operands
[operand
]->accept(this);
1568 if (this->result
.file
== PROGRAM_UNDEFINED
) {
1569 printf("Failed to get tree for expression operand:\n");
1570 ir
->operands
[operand
]->print();
1574 op
[operand
] = this->result
;
1576 /* Matrix expression operands should have been broken down to vector
1577 * operations already.
1579 assert(!ir
->operands
[operand
]->type
->is_matrix());
1582 visit_expression(ir
, op
);
1585 /* The non-recursive part of the expression visitor lives in a separate
1586 * function and should be prevented from being inlined, to avoid a stack
1587 * explosion when deeply nested expressions are visited.
1590 glsl_to_tgsi_visitor::visit_expression(ir_expression
* ir
, st_src_reg
*op
)
1592 st_src_reg result_src
;
1593 st_dst_reg result_dst
;
1595 int vector_elements
= ir
->operands
[0]->type
->vector_elements
;
1596 if (ir
->operands
[1]) {
1597 vector_elements
= MAX2(vector_elements
,
1598 ir
->operands
[1]->type
->vector_elements
);
1601 this->result
.file
= PROGRAM_UNDEFINED
;
1603 /* Storage for our result. Ideally for an assignment we'd be using
1604 * the actual storage for the result here, instead.
1606 result_src
= get_temp(ir
->type
);
1607 /* convenience for the emit functions below. */
1608 result_dst
= st_dst_reg(result_src
);
1609 /* Limit writes to the channels that will be used by result_src later.
1610 * This does limit this temp's use as a temporary for multi-instruction
1613 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1615 switch (ir
->operation
) {
1616 case ir_unop_logic_not
:
1617 if (result_dst
.type
!= GLSL_TYPE_FLOAT
)
1618 emit_asm(ir
, TGSI_OPCODE_NOT
, result_dst
, op
[0]);
1620 /* Previously 'SEQ dst, src, 0.0' was used for this. However, many
1621 * older GPUs implement SEQ using multiple instructions (i915 uses two
1622 * SGE instructions and a MUL instruction). Since our logic values are
1623 * 0.0 and 1.0, 1-x also implements !x.
1625 op
[0].negate
= ~op
[0].negate
;
1626 emit_asm(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], st_src_reg_for_float(1.0));
1630 if (result_dst
.type
== GLSL_TYPE_INT64
|| result_dst
.type
== GLSL_TYPE_UINT64
)
1631 emit_asm(ir
, TGSI_OPCODE_I64NEG
, result_dst
, op
[0]);
1632 else if (result_dst
.type
== GLSL_TYPE_INT
|| result_dst
.type
== GLSL_TYPE_UINT
)
1633 emit_asm(ir
, TGSI_OPCODE_INEG
, result_dst
, op
[0]);
1634 else if (result_dst
.type
== GLSL_TYPE_DOUBLE
)
1635 emit_asm(ir
, TGSI_OPCODE_DNEG
, result_dst
, op
[0]);
1637 op
[0].negate
= ~op
[0].negate
;
1641 case ir_unop_subroutine_to_int
:
1642 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0]);
1645 if (result_dst
.type
== GLSL_TYPE_FLOAT
)
1646 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0].get_abs());
1647 else if (result_dst
.type
== GLSL_TYPE_DOUBLE
)
1648 emit_asm(ir
, TGSI_OPCODE_DABS
, result_dst
, op
[0]);
1649 else if (result_dst
.type
== GLSL_TYPE_INT64
|| result_dst
.type
== GLSL_TYPE_UINT64
)
1650 emit_asm(ir
, TGSI_OPCODE_I64ABS
, result_dst
, op
[0]);
1652 emit_asm(ir
, TGSI_OPCODE_IABS
, result_dst
, op
[0]);
1655 emit_asm(ir
, TGSI_OPCODE_SSG
, result_dst
, op
[0]);
1658 emit_scalar(ir
, TGSI_OPCODE_RCP
, result_dst
, op
[0]);
1662 emit_scalar(ir
, TGSI_OPCODE_EX2
, result_dst
, op
[0]);
1665 assert(!"not reached: should be handled by exp_to_exp2");
1668 assert(!"not reached: should be handled by log_to_log2");
1671 emit_scalar(ir
, TGSI_OPCODE_LG2
, result_dst
, op
[0]);
1674 emit_scalar(ir
, TGSI_OPCODE_SIN
, result_dst
, op
[0]);
1677 emit_scalar(ir
, TGSI_OPCODE_COS
, result_dst
, op
[0]);
1679 case ir_unop_saturate
: {
1680 glsl_to_tgsi_instruction
*inst
;
1681 inst
= emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0]);
1682 inst
->saturate
= true;
1687 case ir_unop_dFdx_coarse
:
1688 emit_asm(ir
, TGSI_OPCODE_DDX
, result_dst
, op
[0]);
1690 case ir_unop_dFdx_fine
:
1691 emit_asm(ir
, TGSI_OPCODE_DDX_FINE
, result_dst
, op
[0]);
1694 case ir_unop_dFdy_coarse
:
1695 case ir_unop_dFdy_fine
:
1697 /* The X component contains 1 or -1 depending on whether the framebuffer
1698 * is a FBO or the window system buffer, respectively.
1699 * It is then multiplied with the source operand of DDY.
1701 static const gl_state_index transform_y_state
[STATE_LENGTH
]
1702 = { STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
};
1704 unsigned transform_y_index
=
1705 _mesa_add_state_reference(this->prog
->Parameters
,
1708 st_src_reg transform_y
= st_src_reg(PROGRAM_STATE_VAR
,
1710 glsl_type::vec4_type
);
1711 transform_y
.swizzle
= SWIZZLE_XXXX
;
1713 st_src_reg temp
= get_temp(glsl_type::vec4_type
);
1715 emit_asm(ir
, TGSI_OPCODE_MUL
, st_dst_reg(temp
), transform_y
, op
[0]);
1716 emit_asm(ir
, ir
->operation
== ir_unop_dFdy_fine
?
1717 TGSI_OPCODE_DDY_FINE
: TGSI_OPCODE_DDY
, result_dst
, temp
);
1721 case ir_unop_frexp_sig
:
1722 emit_asm(ir
, TGSI_OPCODE_DFRACEXP
, result_dst
, undef_dst
, op
[0]);
1725 case ir_unop_frexp_exp
:
1726 emit_asm(ir
, TGSI_OPCODE_DFRACEXP
, undef_dst
, result_dst
, op
[0]);
1729 case ir_unop_noise
: {
1730 /* At some point, a motivated person could add a better
1731 * implementation of noise. Currently not even the nvidia
1732 * binary drivers do anything more than this. In any case, the
1733 * place to do this is in the GL state tracker, not the poor
1736 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, st_src_reg_for_float(0.5));
1741 emit_asm(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1744 op
[1].negate
= ~op
[1].negate
;
1745 emit_asm(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1749 emit_asm(ir
, TGSI_OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1752 emit_asm(ir
, TGSI_OPCODE_DIV
, result_dst
, op
[0], op
[1]);
1755 if (result_dst
.type
== GLSL_TYPE_FLOAT
)
1756 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
1758 emit_asm(ir
, TGSI_OPCODE_MOD
, result_dst
, op
[0], op
[1]);
1762 emit_asm(ir
, TGSI_OPCODE_SLT
, result_dst
, op
[0], op
[1]);
1764 case ir_binop_greater
:
1765 emit_asm(ir
, TGSI_OPCODE_SLT
, result_dst
, op
[1], op
[0]);
1767 case ir_binop_lequal
:
1768 emit_asm(ir
, TGSI_OPCODE_SGE
, result_dst
, op
[1], op
[0]);
1770 case ir_binop_gequal
:
1771 emit_asm(ir
, TGSI_OPCODE_SGE
, result_dst
, op
[0], op
[1]);
1773 case ir_binop_equal
:
1774 emit_asm(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1776 case ir_binop_nequal
:
1777 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1779 case ir_binop_all_equal
:
1780 /* "==" operator producing a scalar boolean. */
1781 if (ir
->operands
[0]->type
->is_vector() ||
1782 ir
->operands
[1]->type
->is_vector()) {
1783 st_src_reg temp
= get_temp(native_integers
?
1784 glsl_type::uvec4_type
:
1785 glsl_type::vec4_type
);
1787 if (native_integers
) {
1788 st_dst_reg temp_dst
= st_dst_reg(temp
);
1789 st_src_reg temp1
= st_src_reg(temp
), temp2
= st_src_reg(temp
);
1791 if (ir
->operands
[0]->type
->is_boolean() &&
1792 ir
->operands
[1]->as_constant() &&
1793 ir
->operands
[1]->as_constant()->is_one()) {
1794 emit_asm(ir
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), op
[0]);
1796 emit_asm(ir
, TGSI_OPCODE_SEQ
, st_dst_reg(temp
), op
[0], op
[1]);
1799 /* Emit 1-3 AND operations to combine the SEQ results. */
1800 switch (ir
->operands
[0]->type
->vector_elements
) {
1804 temp_dst
.writemask
= WRITEMASK_Y
;
1805 temp1
.swizzle
= SWIZZLE_YYYY
;
1806 temp2
.swizzle
= SWIZZLE_ZZZZ
;
1807 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, temp1
, temp2
);
1810 temp_dst
.writemask
= WRITEMASK_X
;
1811 temp1
.swizzle
= SWIZZLE_XXXX
;
1812 temp2
.swizzle
= SWIZZLE_YYYY
;
1813 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, temp1
, temp2
);
1814 temp_dst
.writemask
= WRITEMASK_Y
;
1815 temp1
.swizzle
= SWIZZLE_ZZZZ
;
1816 temp2
.swizzle
= SWIZZLE_WWWW
;
1817 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, temp1
, temp2
);
1820 temp1
.swizzle
= SWIZZLE_XXXX
;
1821 temp2
.swizzle
= SWIZZLE_YYYY
;
1822 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, temp1
, temp2
);
1824 emit_asm(ir
, TGSI_OPCODE_SNE
, st_dst_reg(temp
), op
[0], op
[1]);
1826 /* After the dot-product, the value will be an integer on the
1827 * range [0,4]. Zero becomes 1.0, and positive values become zero.
1829 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1831 /* Negating the result of the dot-product gives values on the range
1832 * [-4, 0]. Zero becomes 1.0, and negative values become zero.
1833 * This is achieved using SGE.
1835 st_src_reg sge_src
= result_src
;
1836 sge_src
.negate
= ~sge_src
.negate
;
1837 emit_asm(ir
, TGSI_OPCODE_SGE
, result_dst
, sge_src
, st_src_reg_for_float(0.0));
1840 emit_asm(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1843 case ir_binop_any_nequal
:
1844 /* "!=" operator producing a scalar boolean. */
1845 if (ir
->operands
[0]->type
->is_vector() ||
1846 ir
->operands
[1]->type
->is_vector()) {
1847 st_src_reg temp
= get_temp(native_integers
?
1848 glsl_type::uvec4_type
:
1849 glsl_type::vec4_type
);
1850 if (ir
->operands
[0]->type
->is_boolean() &&
1851 ir
->operands
[1]->as_constant() &&
1852 ir
->operands
[1]->as_constant()->is_zero()) {
1853 emit_asm(ir
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), op
[0]);
1855 emit_asm(ir
, TGSI_OPCODE_SNE
, st_dst_reg(temp
), op
[0], op
[1]);
1858 if (native_integers
) {
1859 st_dst_reg temp_dst
= st_dst_reg(temp
);
1860 st_src_reg temp1
= st_src_reg(temp
), temp2
= st_src_reg(temp
);
1862 /* Emit 1-3 OR operations to combine the SNE results. */
1863 switch (ir
->operands
[0]->type
->vector_elements
) {
1867 temp_dst
.writemask
= WRITEMASK_Y
;
1868 temp1
.swizzle
= SWIZZLE_YYYY
;
1869 temp2
.swizzle
= SWIZZLE_ZZZZ
;
1870 emit_asm(ir
, TGSI_OPCODE_OR
, temp_dst
, temp1
, temp2
);
1873 temp_dst
.writemask
= WRITEMASK_X
;
1874 temp1
.swizzle
= SWIZZLE_XXXX
;
1875 temp2
.swizzle
= SWIZZLE_YYYY
;
1876 emit_asm(ir
, TGSI_OPCODE_OR
, temp_dst
, temp1
, temp2
);
1877 temp_dst
.writemask
= WRITEMASK_Y
;
1878 temp1
.swizzle
= SWIZZLE_ZZZZ
;
1879 temp2
.swizzle
= SWIZZLE_WWWW
;
1880 emit_asm(ir
, TGSI_OPCODE_OR
, temp_dst
, temp1
, temp2
);
1883 temp1
.swizzle
= SWIZZLE_XXXX
;
1884 temp2
.swizzle
= SWIZZLE_YYYY
;
1885 emit_asm(ir
, TGSI_OPCODE_OR
, result_dst
, temp1
, temp2
);
1887 /* After the dot-product, the value will be an integer on the
1888 * range [0,4]. Zero stays zero, and positive values become 1.0.
1890 glsl_to_tgsi_instruction
*const dp
=
1891 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1892 if (this->prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1893 /* The clamping to [0,1] can be done for free in the fragment
1894 * shader with a saturate.
1896 dp
->saturate
= true;
1898 /* Negating the result of the dot-product gives values on the range
1899 * [-4, 0]. Zero stays zero, and negative values become 1.0. This
1900 * achieved using SLT.
1902 st_src_reg slt_src
= result_src
;
1903 slt_src
.negate
= ~slt_src
.negate
;
1904 emit_asm(ir
, TGSI_OPCODE_SLT
, result_dst
, slt_src
, st_src_reg_for_float(0.0));
1908 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1912 case ir_binop_logic_xor
:
1913 if (native_integers
)
1914 emit_asm(ir
, TGSI_OPCODE_XOR
, result_dst
, op
[0], op
[1]);
1916 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1919 case ir_binop_logic_or
: {
1920 if (native_integers
) {
1921 /* If integers are used as booleans, we can use an actual "or"
1924 assert(native_integers
);
1925 emit_asm(ir
, TGSI_OPCODE_OR
, result_dst
, op
[0], op
[1]);
1927 /* After the addition, the value will be an integer on the
1928 * range [0,2]. Zero stays zero, and positive values become 1.0.
1930 glsl_to_tgsi_instruction
*add
=
1931 emit_asm(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1932 if (this->prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1933 /* The clamping to [0,1] can be done for free in the fragment
1934 * shader with a saturate if floats are being used as boolean values.
1936 add
->saturate
= true;
1938 /* Negating the result of the addition gives values on the range
1939 * [-2, 0]. Zero stays zero, and negative values become 1.0. This
1940 * is achieved using SLT.
1942 st_src_reg slt_src
= result_src
;
1943 slt_src
.negate
= ~slt_src
.negate
;
1944 emit_asm(ir
, TGSI_OPCODE_SLT
, result_dst
, slt_src
, st_src_reg_for_float(0.0));
1950 case ir_binop_logic_and
:
1951 /* If native integers are disabled, the bool args are stored as float 0.0
1952 * or 1.0, so "mul" gives us "and". If they're enabled, just use the
1953 * actual AND opcode.
1955 if (native_integers
)
1956 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], op
[1]);
1958 emit_asm(ir
, TGSI_OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1962 assert(ir
->operands
[0]->type
->is_vector());
1963 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
1964 emit_dp(ir
, result_dst
, op
[0], op
[1],
1965 ir
->operands
[0]->type
->vector_elements
);
1970 emit_scalar(ir
, TGSI_OPCODE_SQRT
, result_dst
, op
[0]);
1972 /* This is the only instruction sequence that makes the game "Risen"
1973 * render correctly. ABS is not required for the game, but since GLSL
1974 * declares negative values as "undefined", allowing us to do whatever
1975 * we want, I choose to use ABS to match DX9 and pre-GLSL RSQ
1978 emit_scalar(ir
, TGSI_OPCODE_RSQ
, result_dst
, op
[0].get_abs());
1979 emit_scalar(ir
, TGSI_OPCODE_RCP
, result_dst
, result_src
);
1983 emit_scalar(ir
, TGSI_OPCODE_RSQ
, result_dst
, op
[0]);
1986 if (native_integers
) {
1987 emit_asm(ir
, TGSI_OPCODE_I2F
, result_dst
, op
[0]);
1990 /* fallthrough to next case otherwise */
1992 if (native_integers
) {
1993 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], st_src_reg_for_float(1.0));
1996 /* fallthrough to next case otherwise */
1999 case ir_unop_i642u64
:
2000 case ir_unop_u642i64
:
2001 /* Converting between signed and unsigned integers is a no-op. */
2003 result_src
.type
= result_dst
.type
;
2006 if (native_integers
) {
2007 /* Booleans are stored as integers using ~0 for true and 0 for false.
2008 * GLSL requires that int(bool) return 1 for true and 0 for false.
2009 * This conversion is done with AND, but it could be done with NEG.
2011 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], st_src_reg_for_int(1));
2013 /* Booleans and integers are both stored as floats when native
2014 * integers are disabled.
2020 if (native_integers
)
2021 emit_asm(ir
, TGSI_OPCODE_F2I
, result_dst
, op
[0]);
2023 emit_asm(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
2026 if (native_integers
)
2027 emit_asm(ir
, TGSI_OPCODE_F2U
, result_dst
, op
[0]);
2029 emit_asm(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
2031 case ir_unop_bitcast_f2i
:
2032 case ir_unop_bitcast_f2u
:
2033 /* Make sure we don't propagate the negate modifier to integer opcodes. */
2034 if (op
[0].negate
|| op
[0].abs
)
2035 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0]);
2038 result_src
.type
= ir
->operation
== ir_unop_bitcast_f2i
? GLSL_TYPE_INT
:
2041 case ir_unop_bitcast_i2f
:
2042 case ir_unop_bitcast_u2f
:
2044 result_src
.type
= GLSL_TYPE_FLOAT
;
2047 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], st_src_reg_for_float(0.0));
2050 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], st_src_reg_for_double(0.0));
2053 if (native_integers
)
2054 emit_asm(ir
, TGSI_OPCODE_USNE
, result_dst
, op
[0], st_src_reg_for_int(0));
2056 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], st_src_reg_for_float(0.0));
2058 case ir_unop_bitcast_u642d
:
2059 case ir_unop_bitcast_i642d
:
2061 result_src
.type
= GLSL_TYPE_DOUBLE
;
2063 case ir_unop_bitcast_d2i64
:
2065 result_src
.type
= GLSL_TYPE_INT64
;
2067 case ir_unop_bitcast_d2u64
:
2069 result_src
.type
= GLSL_TYPE_UINT64
;
2072 emit_asm(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
2075 emit_asm(ir
, TGSI_OPCODE_CEIL
, result_dst
, op
[0]);
2078 emit_asm(ir
, TGSI_OPCODE_FLR
, result_dst
, op
[0]);
2080 case ir_unop_round_even
:
2081 emit_asm(ir
, TGSI_OPCODE_ROUND
, result_dst
, op
[0]);
2084 emit_asm(ir
, TGSI_OPCODE_FRC
, result_dst
, op
[0]);
2088 emit_asm(ir
, TGSI_OPCODE_MIN
, result_dst
, op
[0], op
[1]);
2091 emit_asm(ir
, TGSI_OPCODE_MAX
, result_dst
, op
[0], op
[1]);
2094 emit_scalar(ir
, TGSI_OPCODE_POW
, result_dst
, op
[0], op
[1]);
2097 case ir_unop_bit_not
:
2098 if (native_integers
) {
2099 emit_asm(ir
, TGSI_OPCODE_NOT
, result_dst
, op
[0]);
2103 if (native_integers
) {
2104 emit_asm(ir
, TGSI_OPCODE_U2F
, result_dst
, op
[0]);
2107 case ir_binop_lshift
:
2108 case ir_binop_rshift
:
2109 if (native_integers
) {
2110 unsigned opcode
= ir
->operation
== ir_binop_lshift
? TGSI_OPCODE_SHL
2114 if (glsl_base_type_is_64bit(op
[0].type
)) {
2115 /* GLSL shift operations have 32-bit shift counts, but TGSI uses
2118 count
= get_temp(glsl_type::u64vec(ir
->operands
[1]->type
->components()));
2119 emit_asm(ir
, TGSI_OPCODE_U2I64
, st_dst_reg(count
), op
[1]);
2124 emit_asm(ir
, opcode
, result_dst
, op
[0], count
);
2127 case ir_binop_bit_and
:
2128 if (native_integers
) {
2129 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], op
[1]);
2132 case ir_binop_bit_xor
:
2133 if (native_integers
) {
2134 emit_asm(ir
, TGSI_OPCODE_XOR
, result_dst
, op
[0], op
[1]);
2137 case ir_binop_bit_or
:
2138 if (native_integers
) {
2139 emit_asm(ir
, TGSI_OPCODE_OR
, result_dst
, op
[0], op
[1]);
2143 assert(!"GLSL 1.30 features unsupported");
2146 case ir_binop_ubo_load
: {
2147 ir_constant
*const_uniform_block
= ir
->operands
[0]->as_constant();
2148 ir_constant
*const_offset_ir
= ir
->operands
[1]->as_constant();
2149 unsigned const_offset
= const_offset_ir
? const_offset_ir
->value
.u
[0] : 0;
2150 unsigned const_block
= const_uniform_block
? const_uniform_block
->value
.u
[0] + 1 : 0;
2151 st_src_reg index_reg
= get_temp(glsl_type::uint_type
);
2154 cbuf
.type
= ir
->type
->base_type
;
2155 cbuf
.file
= PROGRAM_CONSTANT
;
2157 cbuf
.reladdr
= NULL
;
2161 assert(ir
->type
->is_vector() || ir
->type
->is_scalar());
2163 if (const_offset_ir
) {
2164 /* Constant index into constant buffer */
2165 cbuf
.reladdr
= NULL
;
2166 cbuf
.index
= const_offset
/ 16;
2169 ir_expression
*offset_expr
= ir
->operands
[1]->as_expression();
2170 st_src_reg offset
= op
[1];
2172 /* The OpenGL spec is written in such a way that accesses with
2173 * non-constant offset are almost always vec4-aligned. The only
2174 * exception to this are members of structs in arrays of structs:
2175 * each struct in an array of structs is at least vec4-aligned,
2176 * but single-element and [ui]vec2 members of the struct may be at
2177 * an offset that is not a multiple of 16 bytes.
2179 * Here, we extract that offset, relying on previous passes to always
2180 * generate offset expressions of the form (+ expr constant_offset).
2182 * Note that the std430 layout, which allows more cases of alignment
2183 * less than vec4 in arrays, is not supported for uniform blocks, so
2184 * we do not have to deal with it here.
2186 if (offset_expr
&& offset_expr
->operation
== ir_binop_add
) {
2187 const_offset_ir
= offset_expr
->operands
[1]->as_constant();
2188 if (const_offset_ir
) {
2189 const_offset
= const_offset_ir
->value
.u
[0];
2190 cbuf
.index
= const_offset
/ 16;
2191 offset_expr
->operands
[0]->accept(this);
2192 offset
= this->result
;
2196 /* Relative/variable index into constant buffer */
2197 emit_asm(ir
, TGSI_OPCODE_USHR
, st_dst_reg(index_reg
), offset
,
2198 st_src_reg_for_int(4));
2199 cbuf
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
2200 memcpy(cbuf
.reladdr
, &index_reg
, sizeof(index_reg
));
2203 if (const_uniform_block
) {
2204 /* Constant constant buffer */
2205 cbuf
.reladdr2
= NULL
;
2206 cbuf
.index2D
= const_block
;
2207 cbuf
.has_index2
= true;
2210 /* Relative/variable constant buffer */
2211 cbuf
.reladdr2
= ralloc(mem_ctx
, st_src_reg
);
2213 memcpy(cbuf
.reladdr2
, &op
[0], sizeof(st_src_reg
));
2214 cbuf
.has_index2
= true;
2217 cbuf
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
2218 if (glsl_base_type_is_64bit(cbuf
.type
))
2219 cbuf
.swizzle
+= MAKE_SWIZZLE4(const_offset
% 16 / 8,
2220 const_offset
% 16 / 8,
2221 const_offset
% 16 / 8,
2222 const_offset
% 16 / 8);
2224 cbuf
.swizzle
+= MAKE_SWIZZLE4(const_offset
% 16 / 4,
2225 const_offset
% 16 / 4,
2226 const_offset
% 16 / 4,
2227 const_offset
% 16 / 4);
2229 if (ir
->type
->is_boolean()) {
2230 emit_asm(ir
, TGSI_OPCODE_USNE
, result_dst
, cbuf
, st_src_reg_for_int(0));
2232 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, cbuf
);
2237 /* note: we have to reorder the three args here */
2238 emit_asm(ir
, TGSI_OPCODE_LRP
, result_dst
, op
[2], op
[1], op
[0]);
2241 if (this->ctx
->Const
.NativeIntegers
)
2242 emit_asm(ir
, TGSI_OPCODE_UCMP
, result_dst
, op
[0], op
[1], op
[2]);
2244 op
[0].negate
= ~op
[0].negate
;
2245 emit_asm(ir
, TGSI_OPCODE_CMP
, result_dst
, op
[0], op
[1], op
[2]);
2248 case ir_triop_bitfield_extract
:
2249 emit_asm(ir
, TGSI_OPCODE_IBFE
, result_dst
, op
[0], op
[1], op
[2]);
2251 case ir_quadop_bitfield_insert
:
2252 emit_asm(ir
, TGSI_OPCODE_BFI
, result_dst
, op
[0], op
[1], op
[2], op
[3]);
2254 case ir_unop_bitfield_reverse
:
2255 emit_asm(ir
, TGSI_OPCODE_BREV
, result_dst
, op
[0]);
2257 case ir_unop_bit_count
:
2258 emit_asm(ir
, TGSI_OPCODE_POPC
, result_dst
, op
[0]);
2260 case ir_unop_find_msb
:
2261 emit_asm(ir
, TGSI_OPCODE_IMSB
, result_dst
, op
[0]);
2263 case ir_unop_find_lsb
:
2264 emit_asm(ir
, TGSI_OPCODE_LSB
, result_dst
, op
[0]);
2266 case ir_binop_imul_high
:
2267 emit_asm(ir
, TGSI_OPCODE_IMUL_HI
, result_dst
, op
[0], op
[1]);
2270 /* In theory, MAD is incorrect here. */
2272 emit_asm(ir
, TGSI_OPCODE_FMA
, result_dst
, op
[0], op
[1], op
[2]);
2274 emit_asm(ir
, TGSI_OPCODE_MAD
, result_dst
, op
[0], op
[1], op
[2]);
2276 case ir_unop_interpolate_at_centroid
:
2277 emit_asm(ir
, TGSI_OPCODE_INTERP_CENTROID
, result_dst
, op
[0]);
2279 case ir_binop_interpolate_at_offset
: {
2280 /* The y coordinate needs to be flipped for the default fb */
2281 static const gl_state_index transform_y_state
[STATE_LENGTH
]
2282 = { STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
};
2284 unsigned transform_y_index
=
2285 _mesa_add_state_reference(this->prog
->Parameters
,
2288 st_src_reg transform_y
= st_src_reg(PROGRAM_STATE_VAR
,
2290 glsl_type::vec4_type
);
2291 transform_y
.swizzle
= SWIZZLE_XXXX
;
2293 st_src_reg temp
= get_temp(glsl_type::vec2_type
);
2294 st_dst_reg temp_dst
= st_dst_reg(temp
);
2296 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, op
[1]);
2297 temp_dst
.writemask
= WRITEMASK_Y
;
2298 emit_asm(ir
, TGSI_OPCODE_MUL
, temp_dst
, transform_y
, op
[1]);
2299 emit_asm(ir
, TGSI_OPCODE_INTERP_OFFSET
, result_dst
, op
[0], temp
);
2302 case ir_binop_interpolate_at_sample
:
2303 emit_asm(ir
, TGSI_OPCODE_INTERP_SAMPLE
, result_dst
, op
[0], op
[1]);
2307 emit_asm(ir
, TGSI_OPCODE_D2F
, result_dst
, op
[0]);
2310 emit_asm(ir
, TGSI_OPCODE_F2D
, result_dst
, op
[0]);
2313 emit_asm(ir
, TGSI_OPCODE_D2I
, result_dst
, op
[0]);
2316 emit_asm(ir
, TGSI_OPCODE_I2D
, result_dst
, op
[0]);
2319 emit_asm(ir
, TGSI_OPCODE_D2U
, result_dst
, op
[0]);
2322 emit_asm(ir
, TGSI_OPCODE_U2D
, result_dst
, op
[0]);
2324 case ir_unop_unpack_double_2x32
:
2325 case ir_unop_pack_double_2x32
:
2326 case ir_unop_unpack_int_2x32
:
2327 case ir_unop_pack_int_2x32
:
2328 case ir_unop_unpack_uint_2x32
:
2329 case ir_unop_pack_uint_2x32
:
2330 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0]);
2333 case ir_binop_ldexp
:
2334 if (ir
->operands
[0]->type
->is_double()) {
2335 emit_asm(ir
, TGSI_OPCODE_DLDEXP
, result_dst
, op
[0], op
[1]);
2337 assert(!"Invalid ldexp for non-double opcode in glsl_to_tgsi_visitor::visit()");
2341 case ir_unop_pack_half_2x16
:
2342 emit_asm(ir
, TGSI_OPCODE_PK2H
, result_dst
, op
[0]);
2344 case ir_unop_unpack_half_2x16
:
2345 emit_asm(ir
, TGSI_OPCODE_UP2H
, result_dst
, op
[0]);
2348 case ir_unop_get_buffer_size
: {
2349 ir_constant
*const_offset
= ir
->operands
[0]->as_constant();
2352 ctx
->Const
.Program
[shader
->Stage
].MaxAtomicBuffers
+
2353 (const_offset
? const_offset
->value
.u
[0] : 0),
2355 if (!const_offset
) {
2356 buffer
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
2357 *buffer
.reladdr
= op
[0];
2358 emit_arl(ir
, sampler_reladdr
, op
[0]);
2360 emit_asm(ir
, TGSI_OPCODE_RESQ
, result_dst
)->resource
= buffer
;
2366 case ir_unop_b2i64
: {
2367 st_src_reg temp
= get_temp(glsl_type::uvec4_type
);
2368 st_dst_reg temp_dst
= st_dst_reg(temp
);
2369 unsigned orig_swz
= op
[0].swizzle
;
2371 * To convert unsigned to 64-bit:
2372 * zero Y channel, copy X channel.
2374 temp_dst
.writemask
= WRITEMASK_Y
;
2375 if (vector_elements
> 1)
2376 temp_dst
.writemask
|= WRITEMASK_W
;
2377 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, st_src_reg_for_int(0));
2378 temp_dst
.writemask
= WRITEMASK_X
;
2379 if (vector_elements
> 1)
2380 temp_dst
.writemask
|= WRITEMASK_Z
;
2381 op
[0].swizzle
= MAKE_SWIZZLE4(GET_SWZ(orig_swz
, 0), GET_SWZ(orig_swz
, 0),
2382 GET_SWZ(orig_swz
, 1), GET_SWZ(orig_swz
, 1));
2383 if (ir
->operation
== ir_unop_u2i64
|| ir
->operation
== ir_unop_u2u64
)
2384 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, op
[0]);
2386 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, op
[0], st_src_reg_for_int(1));
2388 result_src
.type
= GLSL_TYPE_UINT64
;
2389 if (vector_elements
> 2) {
2390 /* Subtle: We rely on the fact that get_temp here returns the next
2391 * TGSI temporary register directly after the temp register used for
2392 * the first two components, so that the result gets picked up
2395 st_src_reg temp
= get_temp(glsl_type::uvec4_type
);
2396 st_dst_reg temp_dst
= st_dst_reg(temp
);
2397 temp_dst
.writemask
= WRITEMASK_Y
;
2398 if (vector_elements
> 3)
2399 temp_dst
.writemask
|= WRITEMASK_W
;
2400 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, st_src_reg_for_int(0));
2402 temp_dst
.writemask
= WRITEMASK_X
;
2403 if (vector_elements
> 3)
2404 temp_dst
.writemask
|= WRITEMASK_Z
;
2405 op
[0].swizzle
= MAKE_SWIZZLE4(GET_SWZ(orig_swz
, 2), GET_SWZ(orig_swz
, 2),
2406 GET_SWZ(orig_swz
, 3), GET_SWZ(orig_swz
, 3));
2407 if (ir
->operation
== ir_unop_u2i64
|| ir
->operation
== ir_unop_u2u64
)
2408 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, op
[0]);
2410 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, op
[0], st_src_reg_for_int(1));
2417 case ir_unop_i642u
: {
2418 st_src_reg temp
= get_temp(glsl_type::uvec4_type
);
2419 st_dst_reg temp_dst
= st_dst_reg(temp
);
2420 unsigned orig_swz
= op
[0].swizzle
;
2421 unsigned orig_idx
= op
[0].index
;
2423 temp_dst
.writemask
= WRITEMASK_X
;
2425 for (el
= 0; el
< vector_elements
; el
++) {
2426 unsigned swz
= GET_SWZ(orig_swz
, el
);
2428 op
[0].swizzle
= MAKE_SWIZZLE4(SWIZZLE_Z
, SWIZZLE_Z
, SWIZZLE_Z
, SWIZZLE_Z
);
2430 op
[0].swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
);
2432 op
[0].index
= orig_idx
+ 1;
2433 op
[0].type
= GLSL_TYPE_UINT
;
2434 temp_dst
.writemask
= WRITEMASK_X
<< el
;
2435 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, op
[0]);
2438 if (ir
->operation
== ir_unop_u642u
|| ir
->operation
== ir_unop_i642u
)
2439 result_src
.type
= GLSL_TYPE_UINT
;
2441 result_src
.type
= GLSL_TYPE_INT
;
2445 emit_asm(ir
, TGSI_OPCODE_U64SNE
, result_dst
, op
[0], st_src_reg_for_int(0));
2448 emit_asm(ir
, TGSI_OPCODE_I642F
, result_dst
, op
[0]);
2451 emit_asm(ir
, TGSI_OPCODE_U642F
, result_dst
, op
[0]);
2454 emit_asm(ir
, TGSI_OPCODE_I642D
, result_dst
, op
[0]);
2457 emit_asm(ir
, TGSI_OPCODE_U642D
, result_dst
, op
[0]);
2460 emit_asm(ir
, TGSI_OPCODE_I2I64
, result_dst
, op
[0]);
2463 emit_asm(ir
, TGSI_OPCODE_F2I64
, result_dst
, op
[0]);
2466 emit_asm(ir
, TGSI_OPCODE_D2I64
, result_dst
, op
[0]);
2469 emit_asm(ir
, TGSI_OPCODE_I2I64
, result_dst
, op
[0]);
2472 emit_asm(ir
, TGSI_OPCODE_F2U64
, result_dst
, op
[0]);
2475 emit_asm(ir
, TGSI_OPCODE_D2U64
, result_dst
, op
[0]);
2477 /* these might be needed */
2478 case ir_unop_pack_snorm_2x16
:
2479 case ir_unop_pack_unorm_2x16
:
2480 case ir_unop_pack_snorm_4x8
:
2481 case ir_unop_pack_unorm_4x8
:
2483 case ir_unop_unpack_snorm_2x16
:
2484 case ir_unop_unpack_unorm_2x16
:
2485 case ir_unop_unpack_snorm_4x8
:
2486 case ir_unop_unpack_unorm_4x8
:
2488 case ir_quadop_vector
:
2489 case ir_binop_vector_extract
:
2490 case ir_triop_vector_insert
:
2491 case ir_binop_carry
:
2492 case ir_binop_borrow
:
2493 case ir_unop_ssbo_unsized_array_length
:
2494 /* This operation is not supported, or should have already been handled.
2496 assert(!"Invalid ir opcode in glsl_to_tgsi_visitor::visit()");
2500 this->result
= result_src
;
2505 glsl_to_tgsi_visitor::visit(ir_swizzle
*ir
)
2511 /* Note that this is only swizzles in expressions, not those on the left
2512 * hand side of an assignment, which do write masking. See ir_assignment
2516 ir
->val
->accept(this);
2518 assert(src
.file
!= PROGRAM_UNDEFINED
);
2519 assert(ir
->type
->vector_elements
> 0);
2521 for (i
= 0; i
< 4; i
++) {
2522 if (i
< ir
->type
->vector_elements
) {
2525 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.x
);
2528 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.y
);
2531 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.z
);
2534 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.w
);
2538 /* If the type is smaller than a vec4, replicate the last
2541 swizzle
[i
] = swizzle
[ir
->type
->vector_elements
- 1];
2545 src
.swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
2550 /* Test if the variable is an array. Note that geometry and
2551 * tessellation shader inputs are outputs are always arrays (except
2552 * for patch inputs), so only the array element type is considered.
2555 is_inout_array(unsigned stage
, ir_variable
*var
, bool *remove_array
)
2557 const glsl_type
*type
= var
->type
;
2559 *remove_array
= false;
2561 if ((stage
== MESA_SHADER_VERTEX
&& var
->data
.mode
== ir_var_shader_in
) ||
2562 (stage
== MESA_SHADER_FRAGMENT
&& var
->data
.mode
== ir_var_shader_out
))
2565 if (((stage
== MESA_SHADER_GEOMETRY
&& var
->data
.mode
== ir_var_shader_in
) ||
2566 (stage
== MESA_SHADER_TESS_EVAL
&& var
->data
.mode
== ir_var_shader_in
) ||
2567 stage
== MESA_SHADER_TESS_CTRL
) &&
2569 if (!var
->type
->is_array())
2570 return false; /* a system value probably */
2572 type
= var
->type
->fields
.array
;
2573 *remove_array
= true;
2576 return type
->is_array() || type
->is_matrix();
2580 st_translate_interp_loc(ir_variable
*var
)
2582 if (var
->data
.centroid
)
2583 return TGSI_INTERPOLATE_LOC_CENTROID
;
2584 else if (var
->data
.sample
)
2585 return TGSI_INTERPOLATE_LOC_SAMPLE
;
2587 return TGSI_INTERPOLATE_LOC_CENTER
;
2591 glsl_to_tgsi_visitor::visit(ir_dereference_variable
*ir
)
2593 variable_storage
*entry
= find_variable_storage(ir
->var
);
2594 ir_variable
*var
= ir
->var
;
2598 switch (var
->data
.mode
) {
2599 case ir_var_uniform
:
2600 entry
= new(mem_ctx
) variable_storage(var
, PROGRAM_UNIFORM
,
2601 var
->data
.param_index
);
2602 this->variables
.push_tail(entry
);
2604 case ir_var_shader_in
: {
2605 /* The linker assigns locations for varyings and attributes,
2606 * including deprecated builtins (like gl_Color), user-assign
2607 * generic attributes (glBindVertexLocation), and
2608 * user-defined varyings.
2610 assert(var
->data
.location
!= -1);
2612 const glsl_type
*type_without_array
= var
->type
->without_array();
2613 struct inout_decl
*decl
= &inputs
[num_inputs
];
2614 unsigned component
= var
->data
.location_frac
;
2615 unsigned num_components
;
2618 if (type_without_array
->is_64bit())
2619 component
= component
/ 2;
2620 if (type_without_array
->vector_elements
)
2621 num_components
= type_without_array
->vector_elements
;
2625 decl
->mesa_index
= var
->data
.location
;
2626 decl
->interp
= (glsl_interp_mode
) var
->data
.interpolation
;
2627 decl
->interp_loc
= st_translate_interp_loc(var
);
2628 decl
->base_type
= type_without_array
->base_type
;
2629 decl
->usage_mask
= u_bit_consecutive(component
, num_components
);
2631 if (is_inout_array(shader
->Stage
, var
, &remove_array
)) {
2632 decl
->array_id
= num_input_arrays
+ 1;
2639 decl
->size
= type_size(var
->type
->fields
.array
);
2641 decl
->size
= type_size(var
->type
);
2643 entry
= new(mem_ctx
) variable_storage(var
,
2647 entry
->component
= component
;
2649 this->variables
.push_tail(entry
);
2652 case ir_var_shader_out
: {
2653 assert(var
->data
.location
!= -1);
2655 const glsl_type
*type_without_array
= var
->type
->without_array();
2656 struct inout_decl
*decl
= &outputs
[num_outputs
];
2657 unsigned component
= var
->data
.location_frac
;
2658 unsigned num_components
;
2661 if (type_without_array
->is_64bit())
2662 component
= component
/ 2;
2663 if (type_without_array
->vector_elements
)
2664 num_components
= type_without_array
->vector_elements
;
2668 decl
->mesa_index
= var
->data
.location
+ FRAG_RESULT_MAX
* var
->data
.index
;
2669 decl
->base_type
= type_without_array
->base_type
;
2670 decl
->usage_mask
= u_bit_consecutive(component
, num_components
);
2671 if (var
->data
.stream
& (1u << 31)) {
2672 decl
->gs_out_streams
= var
->data
.stream
& ~(1u << 31);
2674 assert(var
->data
.stream
< 4);
2675 decl
->gs_out_streams
= 0;
2676 for (unsigned i
= 0; i
< num_components
; ++i
)
2677 decl
->gs_out_streams
|= var
->data
.stream
<< (2 * (component
+ i
));
2680 if (is_inout_array(shader
->Stage
, var
, &remove_array
)) {
2681 decl
->array_id
= num_output_arrays
+ 1;
2682 num_output_arrays
++;
2688 decl
->size
= type_size(var
->type
->fields
.array
);
2690 decl
->size
= type_size(var
->type
);
2692 if (var
->data
.fb_fetch_output
) {
2693 st_dst_reg dst
= st_dst_reg(get_temp(var
->type
));
2694 st_src_reg src
= st_src_reg(PROGRAM_OUTPUT
, decl
->mesa_index
,
2695 var
->type
, component
, decl
->array_id
);
2696 emit_asm(NULL
, TGSI_OPCODE_FBFETCH
, dst
, src
);
2697 entry
= new(mem_ctx
) variable_storage(var
, dst
.file
, dst
.index
,
2700 entry
= new(mem_ctx
) variable_storage(var
,
2705 entry
->component
= component
;
2707 this->variables
.push_tail(entry
);
2710 case ir_var_system_value
:
2711 entry
= new(mem_ctx
) variable_storage(var
,
2712 PROGRAM_SYSTEM_VALUE
,
2713 var
->data
.location
);
2716 case ir_var_temporary
:
2717 st_src_reg src
= get_temp(var
->type
);
2719 entry
= new(mem_ctx
) variable_storage(var
, src
.file
, src
.index
,
2721 this->variables
.push_tail(entry
);
2727 printf("Failed to make storage for %s\n", var
->name
);
2732 this->result
= st_src_reg(entry
->file
, entry
->index
, var
->type
,
2733 entry
->component
, entry
->array_id
);
2734 if (this->shader
->Stage
== MESA_SHADER_VERTEX
&& var
->data
.mode
== ir_var_shader_in
&& var
->type
->is_double())
2735 this->result
.is_double_vertex_input
= true;
2736 if (!native_integers
)
2737 this->result
.type
= GLSL_TYPE_FLOAT
;
2741 shrink_array_declarations(struct inout_decl
*decls
, unsigned count
,
2742 GLbitfield64
* usage_mask
,
2743 GLbitfield64 double_usage_mask
,
2744 GLbitfield
* patch_usage_mask
)
2749 /* Fix array declarations by removing unused array elements at both ends
2750 * of the arrays. For example, mat4[3] where only mat[1] is used.
2752 for (i
= 0; i
< count
; i
++) {
2753 struct inout_decl
*decl
= &decls
[i
];
2754 if (!decl
->array_id
)
2757 /* Shrink the beginning. */
2758 for (j
= 0; j
< (int)decl
->size
; j
++) {
2759 if (decl
->mesa_index
>= VARYING_SLOT_PATCH0
) {
2760 if (*patch_usage_mask
&
2761 BITFIELD64_BIT(decl
->mesa_index
- VARYING_SLOT_PATCH0
+ j
))
2765 if (*usage_mask
& BITFIELD64_BIT(decl
->mesa_index
+j
))
2767 if (double_usage_mask
& BITFIELD64_BIT(decl
->mesa_index
+j
-1))
2776 /* Shrink the end. */
2777 for (j
= decl
->size
-1; j
>= 0; j
--) {
2778 if (decl
->mesa_index
>= VARYING_SLOT_PATCH0
) {
2779 if (*patch_usage_mask
&
2780 BITFIELD64_BIT(decl
->mesa_index
- VARYING_SLOT_PATCH0
+ j
))
2784 if (*usage_mask
& BITFIELD64_BIT(decl
->mesa_index
+j
))
2786 if (double_usage_mask
& BITFIELD64_BIT(decl
->mesa_index
+j
-1))
2793 /* When not all entries of an array are accessed, we mark them as used
2794 * here anyway, to ensure that the input/output mapping logic doesn't get
2797 * TODO This happens when an array isn't used via indirect access, which
2798 * some game ports do (at least eON-based). There is an optimization
2799 * opportunity here by replacing the array declaration with non-array
2800 * declarations of those slots that are actually used.
2802 for (j
= 1; j
< (int)decl
->size
; ++j
) {
2803 if (decl
->mesa_index
>= VARYING_SLOT_PATCH0
)
2804 *patch_usage_mask
|= BITFIELD64_BIT(decl
->mesa_index
- VARYING_SLOT_PATCH0
+ j
);
2806 *usage_mask
|= BITFIELD64_BIT(decl
->mesa_index
+ j
);
2812 glsl_to_tgsi_visitor::visit(ir_dereference_array
*ir
)
2816 int element_size
= type_size(ir
->type
);
2819 index
= ir
->array_index
->constant_expression_value();
2821 ir
->array
->accept(this);
2824 if (ir
->array
->ir_type
!= ir_type_dereference_array
) {
2825 switch (this->prog
->Target
) {
2826 case GL_TESS_CONTROL_PROGRAM_NV
:
2827 is_2D
= (src
.file
== PROGRAM_INPUT
|| src
.file
== PROGRAM_OUTPUT
) &&
2828 !ir
->variable_referenced()->data
.patch
;
2830 case GL_TESS_EVALUATION_PROGRAM_NV
:
2831 is_2D
= src
.file
== PROGRAM_INPUT
&&
2832 !ir
->variable_referenced()->data
.patch
;
2834 case GL_GEOMETRY_PROGRAM_NV
:
2835 is_2D
= src
.file
== PROGRAM_INPUT
;
2845 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
&&
2846 src
.file
== PROGRAM_INPUT
)
2847 element_size
= attrib_type_size(ir
->type
, true);
2849 src
.index2D
= index
->value
.i
[0];
2850 src
.has_index2
= true;
2852 src
.index
+= index
->value
.i
[0] * element_size
;
2854 /* Variable index array dereference. It eats the "vec4" of the
2855 * base of the array and an index that offsets the TGSI register
2858 ir
->array_index
->accept(this);
2860 st_src_reg index_reg
;
2862 if (element_size
== 1) {
2863 index_reg
= this->result
;
2865 index_reg
= get_temp(native_integers
?
2866 glsl_type::int_type
: glsl_type::float_type
);
2868 emit_asm(ir
, TGSI_OPCODE_MUL
, st_dst_reg(index_reg
),
2869 this->result
, st_src_reg_for_type(index_reg
.type
, element_size
));
2872 /* If there was already a relative address register involved, add the
2873 * new and the old together to get the new offset.
2875 if (!is_2D
&& src
.reladdr
!= NULL
) {
2876 st_src_reg accum_reg
= get_temp(native_integers
?
2877 glsl_type::int_type
: glsl_type::float_type
);
2879 emit_asm(ir
, TGSI_OPCODE_ADD
, st_dst_reg(accum_reg
),
2880 index_reg
, *src
.reladdr
);
2882 index_reg
= accum_reg
;
2886 src
.reladdr2
= ralloc(mem_ctx
, st_src_reg
);
2887 memcpy(src
.reladdr2
, &index_reg
, sizeof(index_reg
));
2889 src
.has_index2
= true;
2891 src
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
2892 memcpy(src
.reladdr
, &index_reg
, sizeof(index_reg
));
2896 /* Change the register type to the element type of the array. */
2897 src
.type
= ir
->type
->base_type
;
2903 glsl_to_tgsi_visitor::visit(ir_dereference_record
*ir
)
2906 const glsl_type
*struct_type
= ir
->record
->type
;
2909 ir
->record
->accept(this);
2911 for (i
= 0; i
< struct_type
->length
; i
++) {
2912 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
2914 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
2917 /* If the type is smaller than a vec4, replicate the last channel out. */
2918 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
2919 this->result
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
2921 this->result
.swizzle
= SWIZZLE_NOOP
;
2923 this->result
.index
+= offset
;
2924 this->result
.type
= ir
->type
->base_type
;
2928 * We want to be careful in assignment setup to hit the actual storage
2929 * instead of potentially using a temporary like we might with the
2930 * ir_dereference handler.
2933 get_assignment_lhs(ir_dereference
*ir
, glsl_to_tgsi_visitor
*v
, int *component
)
2935 /* The LHS must be a dereference. If the LHS is a variable indexed array
2936 * access of a vector, it must be separated into a series conditional moves
2937 * before reaching this point (see ir_vec_index_to_cond_assign).
2939 assert(ir
->as_dereference());
2940 ir_dereference_array
*deref_array
= ir
->as_dereference_array();
2942 assert(!deref_array
->array
->type
->is_vector());
2945 /* Use the rvalue deref handler for the most part. We write swizzles using
2946 * the writemask, but we do extract the base component for enhanced layouts
2947 * from the source swizzle.
2950 *component
= GET_SWZ(v
->result
.swizzle
, 0);
2951 return st_dst_reg(v
->result
);
2955 * Process the condition of a conditional assignment
2957 * Examines the condition of a conditional assignment to generate the optimal
2958 * first operand of a \c CMP instruction. If the condition is a relational
2959 * operator with 0 (e.g., \c ir_binop_less), the value being compared will be
2960 * used as the source for the \c CMP instruction. Otherwise the comparison
2961 * is processed to a boolean result, and the boolean result is used as the
2962 * operand to the CMP instruction.
2965 glsl_to_tgsi_visitor::process_move_condition(ir_rvalue
*ir
)
2967 ir_rvalue
*src_ir
= ir
;
2969 bool switch_order
= false;
2971 ir_expression
*const expr
= ir
->as_expression();
2973 if (native_integers
) {
2974 if ((expr
!= NULL
) && (expr
->get_num_operands() == 2)) {
2975 enum glsl_base_type type
= expr
->operands
[0]->type
->base_type
;
2976 if (type
== GLSL_TYPE_INT
|| type
== GLSL_TYPE_UINT
||
2977 type
== GLSL_TYPE_BOOL
) {
2978 if (expr
->operation
== ir_binop_equal
) {
2979 if (expr
->operands
[0]->is_zero()) {
2980 src_ir
= expr
->operands
[1];
2981 switch_order
= true;
2983 else if (expr
->operands
[1]->is_zero()) {
2984 src_ir
= expr
->operands
[0];
2985 switch_order
= true;
2988 else if (expr
->operation
== ir_binop_nequal
) {
2989 if (expr
->operands
[0]->is_zero()) {
2990 src_ir
= expr
->operands
[1];
2992 else if (expr
->operands
[1]->is_zero()) {
2993 src_ir
= expr
->operands
[0];
2999 src_ir
->accept(this);
3000 return switch_order
;
3003 if ((expr
!= NULL
) && (expr
->get_num_operands() == 2)) {
3004 bool zero_on_left
= false;
3006 if (expr
->operands
[0]->is_zero()) {
3007 src_ir
= expr
->operands
[1];
3008 zero_on_left
= true;
3009 } else if (expr
->operands
[1]->is_zero()) {
3010 src_ir
= expr
->operands
[0];
3011 zero_on_left
= false;
3015 * (a < 0) T F F ( a < 0) T F F
3016 * (0 < a) F F T (-a < 0) F F T
3017 * (a <= 0) T T F (-a < 0) F F T (swap order of other operands)
3018 * (0 <= a) F T T ( a < 0) T F F (swap order of other operands)
3019 * (a > 0) F F T (-a < 0) F F T
3020 * (0 > a) T F F ( a < 0) T F F
3021 * (a >= 0) F T T ( a < 0) T F F (swap order of other operands)
3022 * (0 >= a) T T F (-a < 0) F F T (swap order of other operands)
3024 * Note that exchanging the order of 0 and 'a' in the comparison simply
3025 * means that the value of 'a' should be negated.
3028 switch (expr
->operation
) {
3030 switch_order
= false;
3031 negate
= zero_on_left
;
3034 case ir_binop_greater
:
3035 switch_order
= false;
3036 negate
= !zero_on_left
;
3039 case ir_binop_lequal
:
3040 switch_order
= true;
3041 negate
= !zero_on_left
;
3044 case ir_binop_gequal
:
3045 switch_order
= true;
3046 negate
= zero_on_left
;
3050 /* This isn't the right kind of comparison afterall, so make sure
3051 * the whole condition is visited.
3059 src_ir
->accept(this);
3061 /* We use the TGSI_OPCODE_CMP (a < 0 ? b : c) for conditional moves, and the
3062 * condition we produced is 0.0 or 1.0. By flipping the sign, we can
3063 * choose which value TGSI_OPCODE_CMP produces without an extra instruction
3064 * computing the condition.
3067 this->result
.negate
= ~this->result
.negate
;
3069 return switch_order
;
3073 glsl_to_tgsi_visitor::emit_block_mov(ir_assignment
*ir
, const struct glsl_type
*type
,
3074 st_dst_reg
*l
, st_src_reg
*r
,
3075 st_src_reg
*cond
, bool cond_swap
)
3077 if (type
->is_record()) {
3078 for (unsigned int i
= 0; i
< type
->length
; i
++) {
3079 emit_block_mov(ir
, type
->fields
.structure
[i
].type
, l
, r
,
3085 if (type
->is_array()) {
3086 for (unsigned int i
= 0; i
< type
->length
; i
++) {
3087 emit_block_mov(ir
, type
->fields
.array
, l
, r
, cond
, cond_swap
);
3092 if (type
->is_matrix()) {
3093 const struct glsl_type
*vec_type
;
3095 vec_type
= glsl_type::get_instance(type
->is_double() ? GLSL_TYPE_DOUBLE
: GLSL_TYPE_FLOAT
,
3096 type
->vector_elements
, 1);
3098 for (int i
= 0; i
< type
->matrix_columns
; i
++) {
3099 emit_block_mov(ir
, vec_type
, l
, r
, cond
, cond_swap
);
3104 assert(type
->is_scalar() || type
->is_vector());
3106 l
->type
= type
->base_type
;
3107 r
->type
= type
->base_type
;
3109 st_src_reg l_src
= st_src_reg(*l
);
3110 l_src
.swizzle
= swizzle_for_size(type
->vector_elements
);
3112 if (native_integers
) {
3113 emit_asm(ir
, TGSI_OPCODE_UCMP
, *l
, *cond
,
3114 cond_swap
? l_src
: *r
,
3115 cond_swap
? *r
: l_src
);
3117 emit_asm(ir
, TGSI_OPCODE_CMP
, *l
, *cond
,
3118 cond_swap
? l_src
: *r
,
3119 cond_swap
? *r
: l_src
);
3122 emit_asm(ir
, TGSI_OPCODE_MOV
, *l
, *r
);
3126 if (type
->is_dual_slot()) {
3128 if (r
->is_double_vertex_input
== false)
3134 glsl_to_tgsi_visitor::visit(ir_assignment
*ir
)
3140 ir
->rhs
->accept(this);
3143 l
= get_assignment_lhs(ir
->lhs
, this, &dst_component
);
3147 int first_enabled_chan
= 0;
3149 ir_variable
*variable
= ir
->lhs
->variable_referenced();
3151 if (shader
->Stage
== MESA_SHADER_FRAGMENT
&&
3152 variable
->data
.mode
== ir_var_shader_out
&&
3153 (variable
->data
.location
== FRAG_RESULT_DEPTH
||
3154 variable
->data
.location
== FRAG_RESULT_STENCIL
)) {
3155 assert(ir
->lhs
->type
->is_scalar());
3156 assert(ir
->write_mask
== WRITEMASK_X
);
3158 if (variable
->data
.location
== FRAG_RESULT_DEPTH
)
3159 l
.writemask
= WRITEMASK_Z
;
3161 assert(variable
->data
.location
== FRAG_RESULT_STENCIL
);
3162 l
.writemask
= WRITEMASK_Y
;
3164 } else if (ir
->write_mask
== 0) {
3165 assert(!ir
->lhs
->type
->is_scalar() && !ir
->lhs
->type
->is_vector());
3167 unsigned num_elements
= ir
->lhs
->type
->without_array()->vector_elements
;
3170 l
.writemask
= u_bit_consecutive(0, num_elements
);
3172 /* The type is a struct or an array of (array of) structs. */
3173 l
.writemask
= WRITEMASK_XYZW
;
3176 l
.writemask
= ir
->write_mask
;
3179 for (int i
= 0; i
< 4; i
++) {
3180 if (l
.writemask
& (1 << i
)) {
3181 first_enabled_chan
= GET_SWZ(r
.swizzle
, i
);
3186 l
.writemask
= l
.writemask
<< dst_component
;
3188 /* Swizzle a small RHS vector into the channels being written.
3190 * glsl ir treats write_mask as dictating how many channels are
3191 * present on the RHS while TGSI treats write_mask as just
3192 * showing which channels of the vec4 RHS get written.
3194 for (int i
= 0; i
< 4; i
++) {
3195 if (l
.writemask
& (1 << i
))
3196 swizzles
[i
] = GET_SWZ(r
.swizzle
, rhs_chan
++);
3198 swizzles
[i
] = first_enabled_chan
;
3200 r
.swizzle
= MAKE_SWIZZLE4(swizzles
[0], swizzles
[1],
3201 swizzles
[2], swizzles
[3]);
3204 assert(l
.file
!= PROGRAM_UNDEFINED
);
3205 assert(r
.file
!= PROGRAM_UNDEFINED
);
3207 if (ir
->condition
) {
3208 const bool switch_order
= this->process_move_condition(ir
->condition
);
3209 st_src_reg condition
= this->result
;
3211 emit_block_mov(ir
, ir
->lhs
->type
, &l
, &r
, &condition
, switch_order
);
3212 } else if (ir
->rhs
->as_expression() &&
3213 this->instructions
.get_tail() &&
3214 ir
->rhs
== ((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->ir
&&
3215 !((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->is_64bit_expanded
&&
3216 type_size(ir
->lhs
->type
) == 1 &&
3217 l
.writemask
== ((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->dst
[0].writemask
) {
3218 /* To avoid emitting an extra MOV when assigning an expression to a
3219 * variable, emit the last instruction of the expression again, but
3220 * replace the destination register with the target of the assignment.
3221 * Dead code elimination will remove the original instruction.
3223 glsl_to_tgsi_instruction
*inst
, *new_inst
;
3224 inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
3225 new_inst
= emit_asm(ir
, inst
->op
, l
, inst
->src
[0], inst
->src
[1], inst
->src
[2], inst
->src
[3]);
3226 new_inst
->saturate
= inst
->saturate
;
3227 inst
->dead_mask
= inst
->dst
[0].writemask
;
3229 emit_block_mov(ir
, ir
->rhs
->type
, &l
, &r
, NULL
, false);
3235 glsl_to_tgsi_visitor::visit(ir_constant
*ir
)
3238 GLdouble stack_vals
[4] = { 0 };
3239 gl_constant_value
*values
= (gl_constant_value
*) stack_vals
;
3240 GLenum gl_type
= GL_NONE
;
3242 static int in_array
= 0;
3243 gl_register_file file
= in_array
? PROGRAM_CONSTANT
: PROGRAM_IMMEDIATE
;
3245 /* Unfortunately, 4 floats is all we can get into
3246 * _mesa_add_typed_unnamed_constant. So, make a temp to store an
3247 * aggregate constant and move each constant value into it. If we
3248 * get lucky, copy propagation will eliminate the extra moves.
3250 if (ir
->type
->is_record()) {
3251 st_src_reg temp_base
= get_temp(ir
->type
);
3252 st_dst_reg temp
= st_dst_reg(temp_base
);
3254 foreach_in_list(ir_constant
, field_value
, &ir
->components
) {
3255 int size
= type_size(field_value
->type
);
3259 field_value
->accept(this);
3262 for (i
= 0; i
< (unsigned int)size
; i
++) {
3263 emit_asm(ir
, TGSI_OPCODE_MOV
, temp
, src
);
3269 this->result
= temp_base
;
3273 if (ir
->type
->is_array()) {
3274 st_src_reg temp_base
= get_temp(ir
->type
);
3275 st_dst_reg temp
= st_dst_reg(temp_base
);
3276 int size
= type_size(ir
->type
->fields
.array
);
3281 for (i
= 0; i
< ir
->type
->length
; i
++) {
3282 ir
->array_elements
[i
]->accept(this);
3284 for (int j
= 0; j
< size
; j
++) {
3285 emit_asm(ir
, TGSI_OPCODE_MOV
, temp
, src
);
3291 this->result
= temp_base
;
3296 if (ir
->type
->is_matrix()) {
3297 st_src_reg mat
= get_temp(ir
->type
);
3298 st_dst_reg mat_column
= st_dst_reg(mat
);
3300 for (i
= 0; i
< ir
->type
->matrix_columns
; i
++) {
3301 switch (ir
->type
->base_type
) {
3302 case GLSL_TYPE_FLOAT
:
3303 values
= (gl_constant_value
*) &ir
->value
.f
[i
* ir
->type
->vector_elements
];
3305 src
= st_src_reg(file
, -1, ir
->type
->base_type
);
3306 src
.index
= add_constant(file
,
3308 ir
->type
->vector_elements
,
3311 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3313 case GLSL_TYPE_DOUBLE
:
3314 values
= (gl_constant_value
*) &ir
->value
.d
[i
* ir
->type
->vector_elements
];
3315 src
= st_src_reg(file
, -1, ir
->type
->base_type
);
3316 src
.index
= add_constant(file
,
3318 ir
->type
->vector_elements
,
3321 if (ir
->type
->vector_elements
>= 2) {
3322 mat_column
.writemask
= WRITEMASK_XY
;
3323 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_X
, SWIZZLE_Y
);
3324 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3326 mat_column
.writemask
= WRITEMASK_X
;
3327 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
);
3328 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3331 if (ir
->type
->vector_elements
> 2) {
3332 if (ir
->type
->vector_elements
== 4) {
3333 mat_column
.writemask
= WRITEMASK_ZW
;
3334 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_X
, SWIZZLE_Y
);
3335 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3337 mat_column
.writemask
= WRITEMASK_Z
;
3338 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
);
3339 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3340 mat_column
.writemask
= WRITEMASK_XYZW
;
3341 src
.swizzle
= SWIZZLE_XYZW
;
3347 unreachable("Illegal matrix constant type.\n");
3356 switch (ir
->type
->base_type
) {
3357 case GLSL_TYPE_FLOAT
:
3359 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3360 values
[i
].f
= ir
->value
.f
[i
];
3363 case GLSL_TYPE_DOUBLE
:
3364 gl_type
= GL_DOUBLE
;
3365 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3366 memcpy(&values
[i
* 2], &ir
->value
.d
[i
], sizeof(double));
3369 case GLSL_TYPE_INT64
:
3370 gl_type
= GL_INT64_ARB
;
3371 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3372 memcpy(&values
[i
* 2], &ir
->value
.d
[i
], sizeof(int64_t));
3375 case GLSL_TYPE_UINT64
:
3376 gl_type
= GL_UNSIGNED_INT64_ARB
;
3377 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3378 memcpy(&values
[i
* 2], &ir
->value
.d
[i
], sizeof(uint64_t));
3381 case GLSL_TYPE_UINT
:
3382 gl_type
= native_integers
? GL_UNSIGNED_INT
: GL_FLOAT
;
3383 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3384 if (native_integers
)
3385 values
[i
].u
= ir
->value
.u
[i
];
3387 values
[i
].f
= ir
->value
.u
[i
];
3391 gl_type
= native_integers
? GL_INT
: GL_FLOAT
;
3392 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3393 if (native_integers
)
3394 values
[i
].i
= ir
->value
.i
[i
];
3396 values
[i
].f
= ir
->value
.i
[i
];
3399 case GLSL_TYPE_BOOL
:
3400 gl_type
= native_integers
? GL_BOOL
: GL_FLOAT
;
3401 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3402 values
[i
].u
= ir
->value
.b
[i
] ? ctx
->Const
.UniformBooleanTrue
: 0;
3406 assert(!"Non-float/uint/int/bool constant");
3409 this->result
= st_src_reg(file
, -1, ir
->type
);
3410 this->result
.index
= add_constant(file
,
3412 ir
->type
->vector_elements
,
3414 &this->result
.swizzle
);
3418 glsl_to_tgsi_visitor::visit_atomic_counter_intrinsic(ir_call
*ir
)
3420 exec_node
*param
= ir
->actual_parameters
.get_head();
3421 ir_dereference
*deref
= static_cast<ir_dereference
*>(param
);
3422 ir_variable
*location
= deref
->variable_referenced();
3425 PROGRAM_BUFFER
, location
->data
.binding
, GLSL_TYPE_ATOMIC_UINT
);
3427 /* Calculate the surface offset */
3429 unsigned array_size
= 0, base
= 0;
3432 get_deref_offsets(deref
, &array_size
, &base
, &index
, &offset
, false);
3434 if (offset
.file
!= PROGRAM_UNDEFINED
) {
3435 emit_asm(ir
, TGSI_OPCODE_MUL
, st_dst_reg(offset
),
3436 offset
, st_src_reg_for_int(ATOMIC_COUNTER_SIZE
));
3437 emit_asm(ir
, TGSI_OPCODE_ADD
, st_dst_reg(offset
),
3438 offset
, st_src_reg_for_int(location
->data
.offset
+ index
* ATOMIC_COUNTER_SIZE
));
3440 offset
= st_src_reg_for_int(location
->data
.offset
+ index
* ATOMIC_COUNTER_SIZE
);
3443 ir
->return_deref
->accept(this);
3444 st_dst_reg
dst(this->result
);
3445 dst
.writemask
= WRITEMASK_X
;
3447 glsl_to_tgsi_instruction
*inst
;
3449 if (ir
->callee
->intrinsic_id
== ir_intrinsic_atomic_counter_read
) {
3450 inst
= emit_asm(ir
, TGSI_OPCODE_LOAD
, dst
, offset
);
3451 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_atomic_counter_increment
) {
3452 inst
= emit_asm(ir
, TGSI_OPCODE_ATOMUADD
, dst
, offset
,
3453 st_src_reg_for_int(1));
3454 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_atomic_counter_predecrement
) {
3455 inst
= emit_asm(ir
, TGSI_OPCODE_ATOMUADD
, dst
, offset
,
3456 st_src_reg_for_int(-1));
3457 emit_asm(ir
, TGSI_OPCODE_ADD
, dst
, this->result
, st_src_reg_for_int(-1));
3459 param
= param
->get_next();
3460 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3463 st_src_reg data
= this->result
, data2
= undef_src
;
3465 switch (ir
->callee
->intrinsic_id
) {
3466 case ir_intrinsic_atomic_counter_add
:
3467 opcode
= TGSI_OPCODE_ATOMUADD
;
3469 case ir_intrinsic_atomic_counter_min
:
3470 opcode
= TGSI_OPCODE_ATOMIMIN
;
3472 case ir_intrinsic_atomic_counter_max
:
3473 opcode
= TGSI_OPCODE_ATOMIMAX
;
3475 case ir_intrinsic_atomic_counter_and
:
3476 opcode
= TGSI_OPCODE_ATOMAND
;
3478 case ir_intrinsic_atomic_counter_or
:
3479 opcode
= TGSI_OPCODE_ATOMOR
;
3481 case ir_intrinsic_atomic_counter_xor
:
3482 opcode
= TGSI_OPCODE_ATOMXOR
;
3484 case ir_intrinsic_atomic_counter_exchange
:
3485 opcode
= TGSI_OPCODE_ATOMXCHG
;
3487 case ir_intrinsic_atomic_counter_comp_swap
: {
3488 opcode
= TGSI_OPCODE_ATOMCAS
;
3489 param
= param
->get_next();
3490 val
= ((ir_instruction
*)param
)->as_rvalue();
3492 data2
= this->result
;
3496 assert(!"Unexpected intrinsic");
3500 inst
= emit_asm(ir
, opcode
, dst
, offset
, data
, data2
);
3503 inst
->resource
= buffer
;
3507 glsl_to_tgsi_visitor::visit_ssbo_intrinsic(ir_call
*ir
)
3509 exec_node
*param
= ir
->actual_parameters
.get_head();
3511 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
3513 param
= param
->get_next();
3514 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
3516 ir_constant
*const_block
= block
->as_constant();
3520 ctx
->Const
.Program
[shader
->Stage
].MaxAtomicBuffers
+
3521 (const_block
? const_block
->value
.u
[0] : 0),
3525 block
->accept(this);
3526 buffer
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
3527 *buffer
.reladdr
= this->result
;
3528 emit_arl(ir
, sampler_reladdr
, this->result
);
3531 /* Calculate the surface offset */
3532 offset
->accept(this);
3533 st_src_reg off
= this->result
;
3535 st_dst_reg dst
= undef_dst
;
3536 if (ir
->return_deref
) {
3537 ir
->return_deref
->accept(this);
3538 dst
= st_dst_reg(this->result
);
3539 dst
.writemask
= (1 << ir
->return_deref
->type
->vector_elements
) - 1;
3542 glsl_to_tgsi_instruction
*inst
;
3544 if (ir
->callee
->intrinsic_id
== ir_intrinsic_ssbo_load
) {
3545 inst
= emit_asm(ir
, TGSI_OPCODE_LOAD
, dst
, off
);
3546 if (dst
.type
== GLSL_TYPE_BOOL
)
3547 emit_asm(ir
, TGSI_OPCODE_USNE
, dst
, st_src_reg(dst
), st_src_reg_for_int(0));
3548 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_ssbo_store
) {
3549 param
= param
->get_next();
3550 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3553 param
= param
->get_next();
3554 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
3556 dst
.writemask
= write_mask
->value
.u
[0];
3558 dst
.type
= this->result
.type
;
3559 inst
= emit_asm(ir
, TGSI_OPCODE_STORE
, dst
, off
, this->result
);
3561 param
= param
->get_next();
3562 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3565 st_src_reg data
= this->result
, data2
= undef_src
;
3567 switch (ir
->callee
->intrinsic_id
) {
3568 case ir_intrinsic_ssbo_atomic_add
:
3569 opcode
= TGSI_OPCODE_ATOMUADD
;
3571 case ir_intrinsic_ssbo_atomic_min
:
3572 opcode
= TGSI_OPCODE_ATOMIMIN
;
3574 case ir_intrinsic_ssbo_atomic_max
:
3575 opcode
= TGSI_OPCODE_ATOMIMAX
;
3577 case ir_intrinsic_ssbo_atomic_and
:
3578 opcode
= TGSI_OPCODE_ATOMAND
;
3580 case ir_intrinsic_ssbo_atomic_or
:
3581 opcode
= TGSI_OPCODE_ATOMOR
;
3583 case ir_intrinsic_ssbo_atomic_xor
:
3584 opcode
= TGSI_OPCODE_ATOMXOR
;
3586 case ir_intrinsic_ssbo_atomic_exchange
:
3587 opcode
= TGSI_OPCODE_ATOMXCHG
;
3589 case ir_intrinsic_ssbo_atomic_comp_swap
:
3590 opcode
= TGSI_OPCODE_ATOMCAS
;
3591 param
= param
->get_next();
3592 val
= ((ir_instruction
*)param
)->as_rvalue();
3594 data2
= this->result
;
3597 assert(!"Unexpected intrinsic");
3601 inst
= emit_asm(ir
, opcode
, dst
, off
, data
, data2
);
3604 param
= param
->get_next();
3605 ir_constant
*access
= NULL
;
3606 if (!param
->is_tail_sentinel()) {
3607 access
= ((ir_instruction
*)param
)->as_constant();
3611 /* The emit_asm() might have actually split the op into pieces, e.g. for
3612 * double stores. We have to go back and fix up all the generated ops.
3614 unsigned op
= inst
->op
;
3616 inst
->resource
= buffer
;
3618 inst
->buffer_access
= access
->value
.u
[0];
3620 if (inst
== this->instructions
.get_head_raw())
3622 inst
= (glsl_to_tgsi_instruction
*)inst
->get_prev();
3624 if (inst
->op
== TGSI_OPCODE_UADD
) {
3625 if (inst
== this->instructions
.get_head_raw())
3627 inst
= (glsl_to_tgsi_instruction
*)inst
->get_prev();
3629 } while (inst
->op
== op
&& inst
->resource
.file
== PROGRAM_UNDEFINED
);
3633 glsl_to_tgsi_visitor::visit_membar_intrinsic(ir_call
*ir
)
3635 switch (ir
->callee
->intrinsic_id
) {
3636 case ir_intrinsic_memory_barrier
:
3637 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3638 st_src_reg_for_int(TGSI_MEMBAR_SHADER_BUFFER
|
3639 TGSI_MEMBAR_ATOMIC_BUFFER
|
3640 TGSI_MEMBAR_SHADER_IMAGE
|
3641 TGSI_MEMBAR_SHARED
));
3643 case ir_intrinsic_memory_barrier_atomic_counter
:
3644 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3645 st_src_reg_for_int(TGSI_MEMBAR_ATOMIC_BUFFER
));
3647 case ir_intrinsic_memory_barrier_buffer
:
3648 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3649 st_src_reg_for_int(TGSI_MEMBAR_SHADER_BUFFER
));
3651 case ir_intrinsic_memory_barrier_image
:
3652 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3653 st_src_reg_for_int(TGSI_MEMBAR_SHADER_IMAGE
));
3655 case ir_intrinsic_memory_barrier_shared
:
3656 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3657 st_src_reg_for_int(TGSI_MEMBAR_SHARED
));
3659 case ir_intrinsic_group_memory_barrier
:
3660 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3661 st_src_reg_for_int(TGSI_MEMBAR_SHADER_BUFFER
|
3662 TGSI_MEMBAR_ATOMIC_BUFFER
|
3663 TGSI_MEMBAR_SHADER_IMAGE
|
3664 TGSI_MEMBAR_SHARED
|
3665 TGSI_MEMBAR_THREAD_GROUP
));
3668 assert(!"Unexpected memory barrier intrinsic");
3673 glsl_to_tgsi_visitor::visit_shared_intrinsic(ir_call
*ir
)
3675 exec_node
*param
= ir
->actual_parameters
.get_head();
3677 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
3679 st_src_reg
buffer(PROGRAM_MEMORY
, 0, GLSL_TYPE_UINT
);
3681 /* Calculate the surface offset */
3682 offset
->accept(this);
3683 st_src_reg off
= this->result
;
3685 st_dst_reg dst
= undef_dst
;
3686 if (ir
->return_deref
) {
3687 ir
->return_deref
->accept(this);
3688 dst
= st_dst_reg(this->result
);
3689 dst
.writemask
= (1 << ir
->return_deref
->type
->vector_elements
) - 1;
3692 glsl_to_tgsi_instruction
*inst
;
3694 if (ir
->callee
->intrinsic_id
== ir_intrinsic_shared_load
) {
3695 inst
= emit_asm(ir
, TGSI_OPCODE_LOAD
, dst
, off
);
3696 inst
->resource
= buffer
;
3697 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_shared_store
) {
3698 param
= param
->get_next();
3699 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3702 param
= param
->get_next();
3703 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
3705 dst
.writemask
= write_mask
->value
.u
[0];
3707 dst
.type
= this->result
.type
;
3708 inst
= emit_asm(ir
, TGSI_OPCODE_STORE
, dst
, off
, this->result
);
3709 inst
->resource
= buffer
;
3711 param
= param
->get_next();
3712 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3715 st_src_reg data
= this->result
, data2
= undef_src
;
3717 switch (ir
->callee
->intrinsic_id
) {
3718 case ir_intrinsic_shared_atomic_add
:
3719 opcode
= TGSI_OPCODE_ATOMUADD
;
3721 case ir_intrinsic_shared_atomic_min
:
3722 opcode
= TGSI_OPCODE_ATOMIMIN
;
3724 case ir_intrinsic_shared_atomic_max
:
3725 opcode
= TGSI_OPCODE_ATOMIMAX
;
3727 case ir_intrinsic_shared_atomic_and
:
3728 opcode
= TGSI_OPCODE_ATOMAND
;
3730 case ir_intrinsic_shared_atomic_or
:
3731 opcode
= TGSI_OPCODE_ATOMOR
;
3733 case ir_intrinsic_shared_atomic_xor
:
3734 opcode
= TGSI_OPCODE_ATOMXOR
;
3736 case ir_intrinsic_shared_atomic_exchange
:
3737 opcode
= TGSI_OPCODE_ATOMXCHG
;
3739 case ir_intrinsic_shared_atomic_comp_swap
:
3740 opcode
= TGSI_OPCODE_ATOMCAS
;
3741 param
= param
->get_next();
3742 val
= ((ir_instruction
*)param
)->as_rvalue();
3744 data2
= this->result
;
3747 assert(!"Unexpected intrinsic");
3751 inst
= emit_asm(ir
, opcode
, dst
, off
, data
, data2
);
3752 inst
->resource
= buffer
;
3757 glsl_to_tgsi_visitor::visit_image_intrinsic(ir_call
*ir
)
3759 exec_node
*param
= ir
->actual_parameters
.get_head();
3761 ir_dereference
*img
= (ir_dereference
*)param
;
3762 const ir_variable
*imgvar
= img
->variable_referenced();
3763 const glsl_type
*type
= imgvar
->type
->without_array();
3764 unsigned sampler_array_size
= 1, sampler_base
= 0;
3767 st_src_reg
image(PROGRAM_IMAGE
, 0, GLSL_TYPE_UINT
);
3769 get_deref_offsets(img
, &sampler_array_size
, &sampler_base
,
3770 (uint16_t*)&image
.index
, &reladdr
, true);
3772 if (reladdr
.file
!= PROGRAM_UNDEFINED
) {
3773 image
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
3774 *image
.reladdr
= reladdr
;
3775 emit_arl(ir
, sampler_reladdr
, reladdr
);
3778 st_dst_reg dst
= undef_dst
;
3779 if (ir
->return_deref
) {
3780 ir
->return_deref
->accept(this);
3781 dst
= st_dst_reg(this->result
);
3782 dst
.writemask
= (1 << ir
->return_deref
->type
->vector_elements
) - 1;
3785 glsl_to_tgsi_instruction
*inst
;
3787 if (ir
->callee
->intrinsic_id
== ir_intrinsic_image_size
) {
3788 dst
.writemask
= WRITEMASK_XYZ
;
3789 inst
= emit_asm(ir
, TGSI_OPCODE_RESQ
, dst
);
3790 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_image_samples
) {
3791 st_src_reg res
= get_temp(glsl_type::ivec4_type
);
3792 st_dst_reg dstres
= st_dst_reg(res
);
3793 dstres
.writemask
= WRITEMASK_W
;
3794 inst
= emit_asm(ir
, TGSI_OPCODE_RESQ
, dstres
);
3795 res
.swizzle
= SWIZZLE_WWWW
;
3796 emit_asm(ir
, TGSI_OPCODE_MOV
, dst
, res
);
3798 st_src_reg arg1
= undef_src
, arg2
= undef_src
;
3800 st_dst_reg coord_dst
;
3801 coord
= get_temp(glsl_type::ivec4_type
);
3802 coord_dst
= st_dst_reg(coord
);
3803 coord_dst
.writemask
= (1 << type
->coordinate_components()) - 1;
3804 param
= param
->get_next();
3805 ((ir_dereference
*)param
)->accept(this);
3806 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
3807 coord
.swizzle
= SWIZZLE_XXXX
;
3808 switch (type
->coordinate_components()) {
3809 case 4: assert(!"unexpected coord count");
3811 case 3: coord
.swizzle
|= SWIZZLE_Z
<< 6;
3813 case 2: coord
.swizzle
|= SWIZZLE_Y
<< 3;
3816 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_MS
) {
3817 param
= param
->get_next();
3818 ((ir_dereference
*)param
)->accept(this);
3819 st_src_reg sample
= this->result
;
3820 sample
.swizzle
= SWIZZLE_XXXX
;
3821 coord_dst
.writemask
= WRITEMASK_W
;
3822 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, sample
);
3823 coord
.swizzle
|= SWIZZLE_W
<< 9;
3826 param
= param
->get_next();
3827 if (!param
->is_tail_sentinel()) {
3828 ((ir_dereference
*)param
)->accept(this);
3829 arg1
= this->result
;
3830 param
= param
->get_next();
3833 if (!param
->is_tail_sentinel()) {
3834 ((ir_dereference
*)param
)->accept(this);
3835 arg2
= this->result
;
3836 param
= param
->get_next();
3839 assert(param
->is_tail_sentinel());
3842 switch (ir
->callee
->intrinsic_id
) {
3843 case ir_intrinsic_image_load
:
3844 opcode
= TGSI_OPCODE_LOAD
;
3846 case ir_intrinsic_image_store
:
3847 opcode
= TGSI_OPCODE_STORE
;
3849 case ir_intrinsic_image_atomic_add
:
3850 opcode
= TGSI_OPCODE_ATOMUADD
;
3852 case ir_intrinsic_image_atomic_min
:
3853 opcode
= TGSI_OPCODE_ATOMIMIN
;
3855 case ir_intrinsic_image_atomic_max
:
3856 opcode
= TGSI_OPCODE_ATOMIMAX
;
3858 case ir_intrinsic_image_atomic_and
:
3859 opcode
= TGSI_OPCODE_ATOMAND
;
3861 case ir_intrinsic_image_atomic_or
:
3862 opcode
= TGSI_OPCODE_ATOMOR
;
3864 case ir_intrinsic_image_atomic_xor
:
3865 opcode
= TGSI_OPCODE_ATOMXOR
;
3867 case ir_intrinsic_image_atomic_exchange
:
3868 opcode
= TGSI_OPCODE_ATOMXCHG
;
3870 case ir_intrinsic_image_atomic_comp_swap
:
3871 opcode
= TGSI_OPCODE_ATOMCAS
;
3874 assert(!"Unexpected intrinsic");
3878 inst
= emit_asm(ir
, opcode
, dst
, coord
, arg1
, arg2
);
3879 if (opcode
== TGSI_OPCODE_STORE
)
3880 inst
->dst
[0].writemask
= WRITEMASK_XYZW
;
3883 inst
->resource
= image
;
3884 inst
->sampler_array_size
= sampler_array_size
;
3885 inst
->sampler_base
= sampler_base
;
3887 inst
->tex_target
= type
->sampler_index();
3888 inst
->image_format
= st_mesa_format_to_pipe_format(st_context(ctx
),
3889 _mesa_get_shader_image_format(imgvar
->data
.image_format
));
3891 if (imgvar
->data
.image_coherent
)
3892 inst
->buffer_access
|= TGSI_MEMORY_COHERENT
;
3893 if (imgvar
->data
.image_restrict
)
3894 inst
->buffer_access
|= TGSI_MEMORY_RESTRICT
;
3895 if (imgvar
->data
.image_volatile
)
3896 inst
->buffer_access
|= TGSI_MEMORY_VOLATILE
;
3900 glsl_to_tgsi_visitor::visit_generic_intrinsic(ir_call
*ir
, unsigned op
)
3902 ir
->return_deref
->accept(this);
3903 st_dst_reg dst
= st_dst_reg(this->result
);
3905 st_src_reg src
[4] = { undef_src
, undef_src
, undef_src
, undef_src
};
3906 unsigned num_src
= 0;
3907 foreach_in_list(ir_rvalue
, param
, &ir
->actual_parameters
) {
3908 assert(num_src
< ARRAY_SIZE(src
));
3910 this->result
.file
= PROGRAM_UNDEFINED
;
3911 param
->accept(this);
3912 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
3914 src
[num_src
] = this->result
;
3918 emit_asm(ir
, op
, dst
, src
[0], src
[1], src
[2], src
[3]);
3922 glsl_to_tgsi_visitor::visit(ir_call
*ir
)
3924 ir_function_signature
*sig
= ir
->callee
;
3926 /* Filter out intrinsics */
3927 switch (sig
->intrinsic_id
) {
3928 case ir_intrinsic_atomic_counter_read
:
3929 case ir_intrinsic_atomic_counter_increment
:
3930 case ir_intrinsic_atomic_counter_predecrement
:
3931 case ir_intrinsic_atomic_counter_add
:
3932 case ir_intrinsic_atomic_counter_min
:
3933 case ir_intrinsic_atomic_counter_max
:
3934 case ir_intrinsic_atomic_counter_and
:
3935 case ir_intrinsic_atomic_counter_or
:
3936 case ir_intrinsic_atomic_counter_xor
:
3937 case ir_intrinsic_atomic_counter_exchange
:
3938 case ir_intrinsic_atomic_counter_comp_swap
:
3939 visit_atomic_counter_intrinsic(ir
);
3942 case ir_intrinsic_ssbo_load
:
3943 case ir_intrinsic_ssbo_store
:
3944 case ir_intrinsic_ssbo_atomic_add
:
3945 case ir_intrinsic_ssbo_atomic_min
:
3946 case ir_intrinsic_ssbo_atomic_max
:
3947 case ir_intrinsic_ssbo_atomic_and
:
3948 case ir_intrinsic_ssbo_atomic_or
:
3949 case ir_intrinsic_ssbo_atomic_xor
:
3950 case ir_intrinsic_ssbo_atomic_exchange
:
3951 case ir_intrinsic_ssbo_atomic_comp_swap
:
3952 visit_ssbo_intrinsic(ir
);
3955 case ir_intrinsic_memory_barrier
:
3956 case ir_intrinsic_memory_barrier_atomic_counter
:
3957 case ir_intrinsic_memory_barrier_buffer
:
3958 case ir_intrinsic_memory_barrier_image
:
3959 case ir_intrinsic_memory_barrier_shared
:
3960 case ir_intrinsic_group_memory_barrier
:
3961 visit_membar_intrinsic(ir
);
3964 case ir_intrinsic_shared_load
:
3965 case ir_intrinsic_shared_store
:
3966 case ir_intrinsic_shared_atomic_add
:
3967 case ir_intrinsic_shared_atomic_min
:
3968 case ir_intrinsic_shared_atomic_max
:
3969 case ir_intrinsic_shared_atomic_and
:
3970 case ir_intrinsic_shared_atomic_or
:
3971 case ir_intrinsic_shared_atomic_xor
:
3972 case ir_intrinsic_shared_atomic_exchange
:
3973 case ir_intrinsic_shared_atomic_comp_swap
:
3974 visit_shared_intrinsic(ir
);
3977 case ir_intrinsic_image_load
:
3978 case ir_intrinsic_image_store
:
3979 case ir_intrinsic_image_atomic_add
:
3980 case ir_intrinsic_image_atomic_min
:
3981 case ir_intrinsic_image_atomic_max
:
3982 case ir_intrinsic_image_atomic_and
:
3983 case ir_intrinsic_image_atomic_or
:
3984 case ir_intrinsic_image_atomic_xor
:
3985 case ir_intrinsic_image_atomic_exchange
:
3986 case ir_intrinsic_image_atomic_comp_swap
:
3987 case ir_intrinsic_image_size
:
3988 case ir_intrinsic_image_samples
:
3989 visit_image_intrinsic(ir
);
3992 case ir_intrinsic_shader_clock
: {
3993 ir
->return_deref
->accept(this);
3995 st_dst_reg dst
= st_dst_reg(this->result
);
3996 dst
.writemask
= TGSI_WRITEMASK_XY
;
3998 emit_asm(ir
, TGSI_OPCODE_CLOCK
, dst
);
4002 case ir_intrinsic_vote_all
:
4003 visit_generic_intrinsic(ir
, TGSI_OPCODE_VOTE_ALL
);
4005 case ir_intrinsic_vote_any
:
4006 visit_generic_intrinsic(ir
, TGSI_OPCODE_VOTE_ANY
);
4008 case ir_intrinsic_vote_eq
:
4009 visit_generic_intrinsic(ir
, TGSI_OPCODE_VOTE_EQ
);
4011 case ir_intrinsic_ballot
:
4012 visit_generic_intrinsic(ir
, TGSI_OPCODE_BALLOT
);
4014 case ir_intrinsic_read_first_invocation
:
4015 visit_generic_intrinsic(ir
, TGSI_OPCODE_READ_FIRST
);
4017 case ir_intrinsic_read_invocation
:
4018 visit_generic_intrinsic(ir
, TGSI_OPCODE_READ_INVOC
);
4021 case ir_intrinsic_invalid
:
4022 case ir_intrinsic_generic_load
:
4023 case ir_intrinsic_generic_store
:
4024 case ir_intrinsic_generic_atomic_add
:
4025 case ir_intrinsic_generic_atomic_and
:
4026 case ir_intrinsic_generic_atomic_or
:
4027 case ir_intrinsic_generic_atomic_xor
:
4028 case ir_intrinsic_generic_atomic_min
:
4029 case ir_intrinsic_generic_atomic_max
:
4030 case ir_intrinsic_generic_atomic_exchange
:
4031 case ir_intrinsic_generic_atomic_comp_swap
:
4032 unreachable("Invalid intrinsic");
4037 glsl_to_tgsi_visitor::calc_deref_offsets(ir_dereference
*tail
,
4038 unsigned *array_elements
,
4040 st_src_reg
*indirect
,
4043 switch (tail
->ir_type
) {
4044 case ir_type_dereference_record
: {
4045 ir_dereference_record
*deref_record
= tail
->as_dereference_record();
4046 const glsl_type
*struct_type
= deref_record
->record
->type
;
4047 int field_index
= deref_record
->record
->type
->field_index(deref_record
->field
);
4049 calc_deref_offsets(deref_record
->record
->as_dereference(), array_elements
, index
, indirect
, location
);
4051 assert(field_index
>= 0);
4052 *location
+= struct_type
->record_location_offset(field_index
);
4056 case ir_type_dereference_array
: {
4057 ir_dereference_array
*deref_arr
= tail
->as_dereference_array();
4058 ir_constant
*array_index
= deref_arr
->array_index
->constant_expression_value();
4061 st_src_reg temp_reg
;
4062 st_dst_reg temp_dst
;
4064 temp_reg
= get_temp(glsl_type::uint_type
);
4065 temp_dst
= st_dst_reg(temp_reg
);
4066 temp_dst
.writemask
= 1;
4068 deref_arr
->array_index
->accept(this);
4069 if (*array_elements
!= 1)
4070 emit_asm(NULL
, TGSI_OPCODE_MUL
, temp_dst
, this->result
, st_src_reg_for_int(*array_elements
));
4072 emit_asm(NULL
, TGSI_OPCODE_MOV
, temp_dst
, this->result
);
4074 if (indirect
->file
== PROGRAM_UNDEFINED
)
4075 *indirect
= temp_reg
;
4077 temp_dst
= st_dst_reg(*indirect
);
4078 temp_dst
.writemask
= 1;
4079 emit_asm(NULL
, TGSI_OPCODE_ADD
, temp_dst
, *indirect
, temp_reg
);
4082 *index
+= array_index
->value
.u
[0] * *array_elements
;
4084 *array_elements
*= deref_arr
->array
->type
->length
;
4086 calc_deref_offsets(deref_arr
->array
->as_dereference(), array_elements
, index
, indirect
, location
);
4095 glsl_to_tgsi_visitor::get_deref_offsets(ir_dereference
*ir
,
4096 unsigned *array_size
,
4099 st_src_reg
*reladdr
,
4102 GLuint shader
= _mesa_program_enum_to_shader_stage(this->prog
->Target
);
4103 unsigned location
= 0;
4104 ir_variable
*var
= ir
->variable_referenced();
4106 memset(reladdr
, 0, sizeof(*reladdr
));
4107 reladdr
->file
= PROGRAM_UNDEFINED
;
4113 location
= var
->data
.location
;
4114 calc_deref_offsets(ir
, array_size
, index
, reladdr
, &location
);
4117 * If we end up with no indirect then adjust the base to the index,
4118 * and set the array size to 1.
4120 if (reladdr
->file
== PROGRAM_UNDEFINED
) {
4126 assert(location
!= 0xffffffff);
4127 *base
+= this->shader_program
->data
->UniformStorage
[location
].opaque
[shader
].index
;
4128 *index
+= this->shader_program
->data
->UniformStorage
[location
].opaque
[shader
].index
;
4133 glsl_to_tgsi_visitor::canonicalize_gather_offset(st_src_reg offset
)
4135 if (offset
.reladdr
|| offset
.reladdr2
) {
4136 st_src_reg tmp
= get_temp(glsl_type::ivec2_type
);
4137 st_dst_reg tmp_dst
= st_dst_reg(tmp
);
4138 tmp_dst
.writemask
= WRITEMASK_XY
;
4139 emit_asm(NULL
, TGSI_OPCODE_MOV
, tmp_dst
, offset
);
4147 glsl_to_tgsi_visitor::visit(ir_texture
*ir
)
4149 st_src_reg result_src
, coord
, cube_sc
, lod_info
, projector
, dx
, dy
;
4150 st_src_reg offset
[MAX_GLSL_TEXTURE_OFFSET
], sample_index
, component
;
4151 st_src_reg levels_src
, reladdr
;
4152 st_dst_reg result_dst
, coord_dst
, cube_sc_dst
;
4153 glsl_to_tgsi_instruction
*inst
= NULL
;
4154 unsigned opcode
= TGSI_OPCODE_NOP
;
4155 const glsl_type
*sampler_type
= ir
->sampler
->type
;
4156 unsigned sampler_array_size
= 1, sampler_base
= 0;
4157 uint16_t sampler_index
= 0;
4158 bool is_cube_array
= false, is_cube_shadow
= false;
4161 /* if we are a cube array sampler or a cube shadow */
4162 if (sampler_type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
) {
4163 is_cube_array
= sampler_type
->sampler_array
;
4164 is_cube_shadow
= sampler_type
->sampler_shadow
;
4167 if (ir
->coordinate
) {
4168 ir
->coordinate
->accept(this);
4170 /* Put our coords in a temp. We'll need to modify them for shadow,
4171 * projection, or LOD, so the only case we'd use it as-is is if
4172 * we're doing plain old texturing. The optimization passes on
4173 * glsl_to_tgsi_visitor should handle cleaning up our mess in that case.
4175 coord
= get_temp(glsl_type::vec4_type
);
4176 coord_dst
= st_dst_reg(coord
);
4177 coord_dst
.writemask
= (1 << ir
->coordinate
->type
->vector_elements
) - 1;
4178 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
4181 if (ir
->projector
) {
4182 ir
->projector
->accept(this);
4183 projector
= this->result
;
4186 /* Storage for our result. Ideally for an assignment we'd be using
4187 * the actual storage for the result here, instead.
4189 result_src
= get_temp(ir
->type
);
4190 result_dst
= st_dst_reg(result_src
);
4191 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
4195 opcode
= (is_cube_array
&& ir
->shadow_comparator
) ? TGSI_OPCODE_TEX2
: TGSI_OPCODE_TEX
;
4197 ir
->offset
->accept(this);
4198 offset
[0] = this->result
;
4202 if (is_cube_array
|| is_cube_shadow
) {
4203 opcode
= TGSI_OPCODE_TXB2
;
4206 opcode
= TGSI_OPCODE_TXB
;
4208 ir
->lod_info
.bias
->accept(this);
4209 lod_info
= this->result
;
4211 ir
->offset
->accept(this);
4212 offset
[0] = this->result
;
4216 if (this->has_tex_txf_lz
&& ir
->lod_info
.lod
->is_zero()) {
4217 opcode
= TGSI_OPCODE_TEX_LZ
;
4219 opcode
= is_cube_array
? TGSI_OPCODE_TXL2
: TGSI_OPCODE_TXL
;
4220 ir
->lod_info
.lod
->accept(this);
4221 lod_info
= this->result
;
4224 ir
->offset
->accept(this);
4225 offset
[0] = this->result
;
4229 opcode
= TGSI_OPCODE_TXD
;
4230 ir
->lod_info
.grad
.dPdx
->accept(this);
4232 ir
->lod_info
.grad
.dPdy
->accept(this);
4235 ir
->offset
->accept(this);
4236 offset
[0] = this->result
;
4240 opcode
= TGSI_OPCODE_TXQ
;
4241 ir
->lod_info
.lod
->accept(this);
4242 lod_info
= this->result
;
4244 case ir_query_levels
:
4245 opcode
= TGSI_OPCODE_TXQ
;
4246 lod_info
= undef_src
;
4247 levels_src
= get_temp(ir
->type
);
4250 if (this->has_tex_txf_lz
&& ir
->lod_info
.lod
->is_zero()) {
4251 opcode
= TGSI_OPCODE_TXF_LZ
;
4253 opcode
= TGSI_OPCODE_TXF
;
4254 ir
->lod_info
.lod
->accept(this);
4255 lod_info
= this->result
;
4258 ir
->offset
->accept(this);
4259 offset
[0] = this->result
;
4263 opcode
= TGSI_OPCODE_TXF
;
4264 ir
->lod_info
.sample_index
->accept(this);
4265 sample_index
= this->result
;
4268 opcode
= TGSI_OPCODE_TG4
;
4269 ir
->lod_info
.component
->accept(this);
4270 component
= this->result
;
4272 ir
->offset
->accept(this);
4273 if (ir
->offset
->type
->is_array()) {
4274 const glsl_type
*elt_type
= ir
->offset
->type
->fields
.array
;
4275 for (i
= 0; i
< ir
->offset
->type
->length
; i
++) {
4276 offset
[i
] = this->result
;
4277 offset
[i
].index
+= i
* type_size(elt_type
);
4278 offset
[i
].type
= elt_type
->base_type
;
4279 offset
[i
].swizzle
= swizzle_for_size(elt_type
->vector_elements
);
4280 offset
[i
] = canonicalize_gather_offset(offset
[i
]);
4283 offset
[0] = canonicalize_gather_offset(this->result
);
4288 opcode
= TGSI_OPCODE_LODQ
;
4290 case ir_texture_samples
:
4291 opcode
= TGSI_OPCODE_TXQS
;
4293 case ir_samples_identical
:
4294 unreachable("Unexpected ir_samples_identical opcode");
4297 if (ir
->projector
) {
4298 if (opcode
== TGSI_OPCODE_TEX
) {
4299 /* Slot the projector in as the last component of the coord. */
4300 coord_dst
.writemask
= WRITEMASK_W
;
4301 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, projector
);
4302 coord_dst
.writemask
= WRITEMASK_XYZW
;
4303 opcode
= TGSI_OPCODE_TXP
;
4305 st_src_reg coord_w
= coord
;
4306 coord_w
.swizzle
= SWIZZLE_WWWW
;
4308 /* For the other TEX opcodes there's no projective version
4309 * since the last slot is taken up by LOD info. Do the
4310 * projective divide now.
4312 coord_dst
.writemask
= WRITEMASK_W
;
4313 emit_asm(ir
, TGSI_OPCODE_RCP
, coord_dst
, projector
);
4315 /* In the case where we have to project the coordinates "by hand,"
4316 * the shadow comparator value must also be projected.
4318 st_src_reg tmp_src
= coord
;
4319 if (ir
->shadow_comparator
) {
4320 /* Slot the shadow value in as the second to last component of the
4323 ir
->shadow_comparator
->accept(this);
4325 tmp_src
= get_temp(glsl_type::vec4_type
);
4326 st_dst_reg tmp_dst
= st_dst_reg(tmp_src
);
4328 /* Projective division not allowed for array samplers. */
4329 assert(!sampler_type
->sampler_array
);
4331 tmp_dst
.writemask
= WRITEMASK_Z
;
4332 emit_asm(ir
, TGSI_OPCODE_MOV
, tmp_dst
, this->result
);
4334 tmp_dst
.writemask
= WRITEMASK_XY
;
4335 emit_asm(ir
, TGSI_OPCODE_MOV
, tmp_dst
, coord
);
4338 coord_dst
.writemask
= WRITEMASK_XYZ
;
4339 emit_asm(ir
, TGSI_OPCODE_MUL
, coord_dst
, tmp_src
, coord_w
);
4341 coord_dst
.writemask
= WRITEMASK_XYZW
;
4342 coord
.swizzle
= SWIZZLE_XYZW
;
4346 /* If projection is done and the opcode is not TGSI_OPCODE_TXP, then the shadow
4347 * comparator was put in the correct place (and projected) by the code,
4348 * above, that handles by-hand projection.
4350 if (ir
->shadow_comparator
&& (!ir
->projector
|| opcode
== TGSI_OPCODE_TXP
)) {
4351 /* Slot the shadow value in as the second to last component of the
4354 ir
->shadow_comparator
->accept(this);
4356 if (is_cube_array
) {
4357 cube_sc
= get_temp(glsl_type::float_type
);
4358 cube_sc_dst
= st_dst_reg(cube_sc
);
4359 cube_sc_dst
.writemask
= WRITEMASK_X
;
4360 emit_asm(ir
, TGSI_OPCODE_MOV
, cube_sc_dst
, this->result
);
4361 cube_sc_dst
.writemask
= WRITEMASK_X
;
4364 if ((sampler_type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_2D
&&
4365 sampler_type
->sampler_array
) ||
4366 sampler_type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
) {
4367 coord_dst
.writemask
= WRITEMASK_W
;
4369 coord_dst
.writemask
= WRITEMASK_Z
;
4371 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
4372 coord_dst
.writemask
= WRITEMASK_XYZW
;
4376 if (ir
->op
== ir_txf_ms
) {
4377 coord_dst
.writemask
= WRITEMASK_W
;
4378 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, sample_index
);
4379 coord_dst
.writemask
= WRITEMASK_XYZW
;
4380 } else if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXB
||
4381 opcode
== TGSI_OPCODE_TXF
) {
4382 /* TGSI stores LOD or LOD bias in the last channel of the coords. */
4383 coord_dst
.writemask
= WRITEMASK_W
;
4384 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, lod_info
);
4385 coord_dst
.writemask
= WRITEMASK_XYZW
;
4388 get_deref_offsets(ir
->sampler
, &sampler_array_size
, &sampler_base
,
4389 &sampler_index
, &reladdr
, true);
4390 if (reladdr
.file
!= PROGRAM_UNDEFINED
)
4391 emit_arl(ir
, sampler_reladdr
, reladdr
);
4393 if (opcode
== TGSI_OPCODE_TXD
)
4394 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, dx
, dy
);
4395 else if (opcode
== TGSI_OPCODE_TXQ
) {
4396 if (ir
->op
== ir_query_levels
) {
4397 /* the level is stored in W */
4398 inst
= emit_asm(ir
, opcode
, st_dst_reg(levels_src
), lod_info
);
4399 result_dst
.writemask
= WRITEMASK_X
;
4400 levels_src
.swizzle
= SWIZZLE_WWWW
;
4401 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, levels_src
);
4403 inst
= emit_asm(ir
, opcode
, result_dst
, lod_info
);
4404 } else if (opcode
== TGSI_OPCODE_TXQS
) {
4405 inst
= emit_asm(ir
, opcode
, result_dst
);
4406 } else if (opcode
== TGSI_OPCODE_TXL2
|| opcode
== TGSI_OPCODE_TXB2
) {
4407 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, lod_info
);
4408 } else if (opcode
== TGSI_OPCODE_TEX2
) {
4409 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, cube_sc
);
4410 } else if (opcode
== TGSI_OPCODE_TG4
) {
4411 if (is_cube_array
&& ir
->shadow_comparator
) {
4412 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, cube_sc
);
4414 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, component
);
4417 inst
= emit_asm(ir
, opcode
, result_dst
, coord
);
4419 if (ir
->shadow_comparator
)
4420 inst
->tex_shadow
= GL_TRUE
;
4422 inst
->resource
.index
= sampler_index
;
4423 inst
->sampler_array_size
= sampler_array_size
;
4424 inst
->sampler_base
= sampler_base
;
4426 if (reladdr
.file
!= PROGRAM_UNDEFINED
) {
4427 inst
->resource
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
4428 memcpy(inst
->resource
.reladdr
, &reladdr
, sizeof(reladdr
));
4432 if (!inst
->tex_offsets
)
4433 inst
->tex_offsets
= rzalloc_array(inst
, st_src_reg
, MAX_GLSL_TEXTURE_OFFSET
);
4435 for (i
= 0; i
< MAX_GLSL_TEXTURE_OFFSET
&& offset
[i
].file
!= PROGRAM_UNDEFINED
; i
++)
4436 inst
->tex_offsets
[i
] = offset
[i
];
4437 inst
->tex_offset_num_offset
= i
;
4440 inst
->tex_target
= sampler_type
->sampler_index();
4441 inst
->tex_type
= ir
->type
->base_type
;
4443 this->result
= result_src
;
4447 glsl_to_tgsi_visitor::visit(ir_return
*ir
)
4449 assert(!ir
->get_value());
4451 emit_asm(ir
, TGSI_OPCODE_RET
);
4455 glsl_to_tgsi_visitor::visit(ir_discard
*ir
)
4457 if (ir
->condition
) {
4458 ir
->condition
->accept(this);
4459 st_src_reg condition
= this->result
;
4461 /* Convert the bool condition to a float so we can negate. */
4462 if (native_integers
) {
4463 st_src_reg temp
= get_temp(ir
->condition
->type
);
4464 emit_asm(ir
, TGSI_OPCODE_AND
, st_dst_reg(temp
),
4465 condition
, st_src_reg_for_float(1.0));
4469 condition
.negate
= ~condition
.negate
;
4470 emit_asm(ir
, TGSI_OPCODE_KILL_IF
, undef_dst
, condition
);
4472 /* unconditional kil */
4473 emit_asm(ir
, TGSI_OPCODE_KILL
);
4478 glsl_to_tgsi_visitor::visit(ir_if
*ir
)
4481 glsl_to_tgsi_instruction
*if_inst
;
4483 ir
->condition
->accept(this);
4484 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
4486 if_opcode
= native_integers
? TGSI_OPCODE_UIF
: TGSI_OPCODE_IF
;
4488 if_inst
= emit_asm(ir
->condition
, if_opcode
, undef_dst
, this->result
);
4490 this->instructions
.push_tail(if_inst
);
4492 visit_exec_list(&ir
->then_instructions
, this);
4494 if (!ir
->else_instructions
.is_empty()) {
4495 emit_asm(ir
->condition
, TGSI_OPCODE_ELSE
);
4496 visit_exec_list(&ir
->else_instructions
, this);
4499 if_inst
= emit_asm(ir
->condition
, TGSI_OPCODE_ENDIF
);
4504 glsl_to_tgsi_visitor::visit(ir_emit_vertex
*ir
)
4506 assert(this->prog
->Target
== GL_GEOMETRY_PROGRAM_NV
);
4508 ir
->stream
->accept(this);
4509 emit_asm(ir
, TGSI_OPCODE_EMIT
, undef_dst
, this->result
);
4513 glsl_to_tgsi_visitor::visit(ir_end_primitive
*ir
)
4515 assert(this->prog
->Target
== GL_GEOMETRY_PROGRAM_NV
);
4517 ir
->stream
->accept(this);
4518 emit_asm(ir
, TGSI_OPCODE_ENDPRIM
, undef_dst
, this->result
);
4522 glsl_to_tgsi_visitor::visit(ir_barrier
*ir
)
4524 assert(this->prog
->Target
== GL_TESS_CONTROL_PROGRAM_NV
||
4525 this->prog
->Target
== GL_COMPUTE_PROGRAM_NV
);
4527 emit_asm(ir
, TGSI_OPCODE_BARRIER
);
4530 glsl_to_tgsi_visitor::glsl_to_tgsi_visitor()
4532 STATIC_ASSERT(sizeof(samplers_used
) * 8 >= PIPE_MAX_SAMPLERS
);
4534 result
.file
= PROGRAM_UNDEFINED
;
4541 num_input_arrays
= 0;
4542 num_output_arrays
= 0;
4544 num_address_regs
= 0;
4548 indirect_addr_consts
= false;
4549 wpos_transform_const
= -1;
4551 native_integers
= false;
4552 mem_ctx
= ralloc_context(NULL
);
4555 shader_program
= NULL
;
4560 use_shared_memory
= false;
4561 has_tex_txf_lz
= false;
4564 glsl_to_tgsi_visitor::~glsl_to_tgsi_visitor()
4567 ralloc_free(mem_ctx
);
4570 extern "C" void free_glsl_to_tgsi_visitor(glsl_to_tgsi_visitor
*v
)
4577 * Count resources used by the given gpu program (number of texture
4581 count_resources(glsl_to_tgsi_visitor
*v
, gl_program
*prog
)
4583 v
->samplers_used
= 0;
4584 v
->buffers_used
= 0;
4587 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &v
->instructions
) {
4588 if (inst
->info
->is_tex
) {
4589 for (int i
= 0; i
< inst
->sampler_array_size
; i
++) {
4590 unsigned idx
= inst
->sampler_base
+ i
;
4591 v
->samplers_used
|= 1u << idx
;
4593 debug_assert(idx
< (int)ARRAY_SIZE(v
->sampler_types
));
4594 v
->sampler_types
[idx
] = inst
->tex_type
;
4595 v
->sampler_targets
[idx
] =
4596 st_translate_texture_target(inst
->tex_target
, inst
->tex_shadow
);
4598 if (inst
->tex_shadow
) {
4599 prog
->ShadowSamplers
|= 1 << (inst
->resource
.index
+ i
);
4604 if (inst
->tex_target
== TEXTURE_EXTERNAL_INDEX
)
4605 prog
->ExternalSamplersUsed
|= 1 << inst
->resource
.index
;
4607 if (inst
->resource
.file
!= PROGRAM_UNDEFINED
&& (
4608 is_resource_instruction(inst
->op
) ||
4609 inst
->op
== TGSI_OPCODE_STORE
)) {
4610 if (inst
->resource
.file
== PROGRAM_BUFFER
) {
4611 v
->buffers_used
|= 1 << inst
->resource
.index
;
4612 } else if (inst
->resource
.file
== PROGRAM_MEMORY
) {
4613 v
->use_shared_memory
= true;
4615 assert(inst
->resource
.file
== PROGRAM_IMAGE
);
4616 for (int i
= 0; i
< inst
->sampler_array_size
; i
++) {
4617 unsigned idx
= inst
->sampler_base
+ i
;
4618 v
->images_used
|= 1 << idx
;
4619 v
->image_targets
[idx
] =
4620 st_translate_texture_target(inst
->tex_target
, false);
4621 v
->image_formats
[idx
] = inst
->image_format
;
4626 prog
->SamplersUsed
= v
->samplers_used
;
4628 if (v
->shader_program
!= NULL
)
4629 _mesa_update_shader_textures_used(v
->shader_program
, prog
);
4633 * Returns the mask of channels (bitmask of WRITEMASK_X,Y,Z,W) which
4634 * are read from the given src in this instruction
4637 get_src_arg_mask(st_dst_reg dst
, st_src_reg src
)
4639 int read_mask
= 0, comp
;
4641 /* Now, given the src swizzle and the written channels, find which
4642 * components are actually read
4644 for (comp
= 0; comp
< 4; ++comp
) {
4645 const unsigned coord
= GET_SWZ(src
.swizzle
, comp
);
4647 if (dst
.writemask
& (1 << comp
) && coord
<= SWIZZLE_W
)
4648 read_mask
|= 1 << coord
;
4655 * This pass replaces CMP T0, T1 T2 T0 with MOV T0, T2 when the CMP
4656 * instruction is the first instruction to write to register T0. There are
4657 * several lowering passes done in GLSL IR (e.g. branches and
4658 * relative addressing) that create a large number of conditional assignments
4659 * that ir_to_mesa converts to CMP instructions like the one mentioned above.
4661 * Here is why this conversion is safe:
4662 * CMP T0, T1 T2 T0 can be expanded to:
4668 * If (T1 < 0.0) evaluates to true then our replacement MOV T0, T2 is the same
4669 * as the original program. If (T1 < 0.0) evaluates to false, executing
4670 * MOV T0, T0 will store a garbage value in T0 since T0 is uninitialized.
4671 * Therefore, it doesn't matter that we are replacing MOV T0, T0 with MOV T0, T2
4672 * because any instruction that was going to read from T0 after this was going
4673 * to read a garbage value anyway.
4676 glsl_to_tgsi_visitor::simplify_cmp(void)
4678 int tempWritesSize
= 0;
4679 unsigned *tempWrites
= NULL
;
4680 unsigned outputWrites
[VARYING_SLOT_TESS_MAX
];
4682 memset(outputWrites
, 0, sizeof(outputWrites
));
4684 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4685 unsigned prevWriteMask
= 0;
4687 /* Give up if we encounter relative addressing or flow control. */
4688 if (inst
->dst
[0].reladdr
|| inst
->dst
[0].reladdr2
||
4689 inst
->dst
[1].reladdr
|| inst
->dst
[1].reladdr2
||
4690 tgsi_get_opcode_info(inst
->op
)->is_branch
||
4691 inst
->op
== TGSI_OPCODE_CONT
||
4692 inst
->op
== TGSI_OPCODE_END
||
4693 inst
->op
== TGSI_OPCODE_RET
) {
4697 if (inst
->dst
[0].file
== PROGRAM_OUTPUT
) {
4698 assert(inst
->dst
[0].index
< (signed)ARRAY_SIZE(outputWrites
));
4699 prevWriteMask
= outputWrites
[inst
->dst
[0].index
];
4700 outputWrites
[inst
->dst
[0].index
] |= inst
->dst
[0].writemask
;
4701 } else if (inst
->dst
[0].file
== PROGRAM_TEMPORARY
) {
4702 if (inst
->dst
[0].index
>= tempWritesSize
) {
4703 const int inc
= 4096;
4705 tempWrites
= (unsigned*)
4707 (tempWritesSize
+ inc
) * sizeof(unsigned));
4711 memset(tempWrites
+ tempWritesSize
, 0, inc
* sizeof(unsigned));
4712 tempWritesSize
+= inc
;
4715 prevWriteMask
= tempWrites
[inst
->dst
[0].index
];
4716 tempWrites
[inst
->dst
[0].index
] |= inst
->dst
[0].writemask
;
4720 /* For a CMP to be considered a conditional write, the destination
4721 * register and source register two must be the same. */
4722 if (inst
->op
== TGSI_OPCODE_CMP
4723 && !(inst
->dst
[0].writemask
& prevWriteMask
)
4724 && inst
->src
[2].file
== inst
->dst
[0].file
4725 && inst
->src
[2].index
== inst
->dst
[0].index
4726 && inst
->dst
[0].writemask
== get_src_arg_mask(inst
->dst
[0], inst
->src
[2])) {
4728 inst
->op
= TGSI_OPCODE_MOV
;
4729 inst
->info
= tgsi_get_opcode_info(inst
->op
);
4730 inst
->src
[0] = inst
->src
[1];
4737 /* Replaces all references to a temporary register index with another index. */
4739 glsl_to_tgsi_visitor::rename_temp_registers(int num_renames
, struct rename_reg_pair
*renames
)
4741 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4744 for (j
= 0; j
< num_inst_src_regs(inst
); j
++) {
4745 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
)
4746 for (k
= 0; k
< num_renames
; k
++)
4747 if (inst
->src
[j
].index
== renames
[k
].old_reg
)
4748 inst
->src
[j
].index
= renames
[k
].new_reg
;
4751 for (j
= 0; j
< inst
->tex_offset_num_offset
; j
++) {
4752 if (inst
->tex_offsets
[j
].file
== PROGRAM_TEMPORARY
)
4753 for (k
= 0; k
< num_renames
; k
++)
4754 if (inst
->tex_offsets
[j
].index
== renames
[k
].old_reg
)
4755 inst
->tex_offsets
[j
].index
= renames
[k
].new_reg
;
4758 for (j
= 0; j
< num_inst_dst_regs(inst
); j
++) {
4759 if (inst
->dst
[j
].file
== PROGRAM_TEMPORARY
)
4760 for (k
= 0; k
< num_renames
; k
++)
4761 if (inst
->dst
[j
].index
== renames
[k
].old_reg
)
4762 inst
->dst
[j
].index
= renames
[k
].new_reg
;
4768 glsl_to_tgsi_visitor::get_first_temp_read(int *first_reads
)
4770 int depth
= 0; /* loop depth */
4771 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
4774 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4775 for (j
= 0; j
< num_inst_src_regs(inst
); j
++) {
4776 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
) {
4777 if (first_reads
[inst
->src
[j
].index
] == -1)
4778 first_reads
[inst
->src
[j
].index
] = (depth
== 0) ? i
: loop_start
;
4781 for (j
= 0; j
< inst
->tex_offset_num_offset
; j
++) {
4782 if (inst
->tex_offsets
[j
].file
== PROGRAM_TEMPORARY
) {
4783 if (first_reads
[inst
->tex_offsets
[j
].index
] == -1)
4784 first_reads
[inst
->tex_offsets
[j
].index
] = (depth
== 0) ? i
: loop_start
;
4787 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
4790 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
4800 glsl_to_tgsi_visitor::get_last_temp_read_first_temp_write(int *last_reads
, int *first_writes
)
4802 int depth
= 0; /* loop depth */
4803 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
4806 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4807 for (j
= 0; j
< num_inst_src_regs(inst
); j
++) {
4808 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
)
4809 last_reads
[inst
->src
[j
].index
] = (depth
== 0) ? i
: -2;
4811 for (j
= 0; j
< num_inst_dst_regs(inst
); j
++) {
4812 if (inst
->dst
[j
].file
== PROGRAM_TEMPORARY
) {
4813 if (first_writes
[inst
->dst
[j
].index
] == -1)
4814 first_writes
[inst
->dst
[j
].index
] = (depth
== 0) ? i
: loop_start
;
4815 last_reads
[inst
->dst
[j
].index
] = (depth
== 0) ? i
: -2;
4818 for (j
= 0; j
< inst
->tex_offset_num_offset
; j
++) {
4819 if (inst
->tex_offsets
[j
].file
== PROGRAM_TEMPORARY
)
4820 last_reads
[inst
->tex_offsets
[j
].index
] = (depth
== 0) ? i
: -2;
4822 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
4825 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
4828 for (k
= 0; k
< this->next_temp
; k
++) {
4829 if (last_reads
[k
] == -2) {
4841 glsl_to_tgsi_visitor::get_last_temp_write(int *last_writes
)
4843 int depth
= 0; /* loop depth */
4847 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4848 for (j
= 0; j
< num_inst_dst_regs(inst
); j
++) {
4849 if (inst
->dst
[j
].file
== PROGRAM_TEMPORARY
)
4850 last_writes
[inst
->dst
[j
].index
] = (depth
== 0) ? i
: -2;
4853 if (inst
->op
== TGSI_OPCODE_BGNLOOP
)
4855 else if (inst
->op
== TGSI_OPCODE_ENDLOOP
)
4857 for (k
= 0; k
< this->next_temp
; k
++) {
4858 if (last_writes
[k
] == -2) {
4869 * On a basic block basis, tracks available PROGRAM_TEMPORARY register
4870 * channels for copy propagation and updates following instructions to
4871 * use the original versions.
4873 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
4874 * will occur. As an example, a TXP production before this pass:
4876 * 0: MOV TEMP[1], INPUT[4].xyyy;
4877 * 1: MOV TEMP[1].w, INPUT[4].wwww;
4878 * 2: TXP TEMP[2], TEMP[1], texture[0], 2D;
4882 * 0: MOV TEMP[1], INPUT[4].xyyy;
4883 * 1: MOV TEMP[1].w, INPUT[4].wwww;
4884 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
4886 * which allows for dead code elimination on TEMP[1]'s writes.
4889 glsl_to_tgsi_visitor::copy_propagate(void)
4891 glsl_to_tgsi_instruction
**acp
= rzalloc_array(mem_ctx
,
4892 glsl_to_tgsi_instruction
*,
4893 this->next_temp
* 4);
4894 int *acp_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
4897 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4898 assert(inst
->dst
[0].file
!= PROGRAM_TEMPORARY
4899 || inst
->dst
[0].index
< this->next_temp
);
4901 /* First, do any copy propagation possible into the src regs. */
4902 for (int r
= 0; r
< 3; r
++) {
4903 glsl_to_tgsi_instruction
*first
= NULL
;
4905 int acp_base
= inst
->src
[r
].index
* 4;
4907 if (inst
->src
[r
].file
!= PROGRAM_TEMPORARY
||
4908 inst
->src
[r
].reladdr
||
4909 inst
->src
[r
].reladdr2
)
4912 /* See if we can find entries in the ACP consisting of MOVs
4913 * from the same src register for all the swizzled channels
4914 * of this src register reference.
4916 for (int i
= 0; i
< 4; i
++) {
4917 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
4918 glsl_to_tgsi_instruction
*copy_chan
= acp
[acp_base
+ src_chan
];
4925 assert(acp_level
[acp_base
+ src_chan
] <= level
);
4930 if (first
->src
[0].file
!= copy_chan
->src
[0].file
||
4931 first
->src
[0].index
!= copy_chan
->src
[0].index
||
4932 first
->src
[0].double_reg2
!= copy_chan
->src
[0].double_reg2
||
4933 first
->src
[0].index2D
!= copy_chan
->src
[0].index2D
) {
4941 /* We've now validated that we can copy-propagate to
4942 * replace this src register reference. Do it.
4944 inst
->src
[r
].file
= first
->src
[0].file
;
4945 inst
->src
[r
].index
= first
->src
[0].index
;
4946 inst
->src
[r
].index2D
= first
->src
[0].index2D
;
4947 inst
->src
[r
].has_index2
= first
->src
[0].has_index2
;
4948 inst
->src
[r
].double_reg2
= first
->src
[0].double_reg2
;
4949 inst
->src
[r
].array_id
= first
->src
[0].array_id
;
4952 for (int i
= 0; i
< 4; i
++) {
4953 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
4954 glsl_to_tgsi_instruction
*copy_inst
= acp
[acp_base
+ src_chan
];
4955 swizzle
|= (GET_SWZ(copy_inst
->src
[0].swizzle
, src_chan
) << (3 * i
));
4957 inst
->src
[r
].swizzle
= swizzle
;
4962 case TGSI_OPCODE_BGNLOOP
:
4963 case TGSI_OPCODE_ENDLOOP
:
4964 /* End of a basic block, clear the ACP entirely. */
4965 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
4968 case TGSI_OPCODE_IF
:
4969 case TGSI_OPCODE_UIF
:
4973 case TGSI_OPCODE_ENDIF
:
4974 case TGSI_OPCODE_ELSE
:
4975 /* Clear all channels written inside the block from the ACP, but
4976 * leaving those that were not touched.
4978 for (int r
= 0; r
< this->next_temp
; r
++) {
4979 for (int c
= 0; c
< 4; c
++) {
4980 if (!acp
[4 * r
+ c
])
4983 if (acp_level
[4 * r
+ c
] >= level
)
4984 acp
[4 * r
+ c
] = NULL
;
4987 if (inst
->op
== TGSI_OPCODE_ENDIF
)
4992 /* Continuing the block, clear any written channels from
4995 for (int d
= 0; d
< 2; d
++) {
4996 if (inst
->dst
[d
].file
== PROGRAM_TEMPORARY
&& inst
->dst
[d
].reladdr
) {
4997 /* Any temporary might be written, so no copy propagation
4998 * across this instruction.
5000 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
5001 } else if (inst
->dst
[d
].file
== PROGRAM_OUTPUT
&&
5002 inst
->dst
[d
].reladdr
) {
5003 /* Any output might be written, so no copy propagation
5004 * from outputs across this instruction.
5006 for (int r
= 0; r
< this->next_temp
; r
++) {
5007 for (int c
= 0; c
< 4; c
++) {
5008 if (!acp
[4 * r
+ c
])
5011 if (acp
[4 * r
+ c
]->src
[0].file
== PROGRAM_OUTPUT
)
5012 acp
[4 * r
+ c
] = NULL
;
5015 } else if (inst
->dst
[d
].file
== PROGRAM_TEMPORARY
||
5016 inst
->dst
[d
].file
== PROGRAM_OUTPUT
) {
5017 /* Clear where it's used as dst. */
5018 if (inst
->dst
[d
].file
== PROGRAM_TEMPORARY
) {
5019 for (int c
= 0; c
< 4; c
++) {
5020 if (inst
->dst
[d
].writemask
& (1 << c
))
5021 acp
[4 * inst
->dst
[d
].index
+ c
] = NULL
;
5025 /* Clear where it's used as src. */
5026 for (int r
= 0; r
< this->next_temp
; r
++) {
5027 for (int c
= 0; c
< 4; c
++) {
5028 if (!acp
[4 * r
+ c
])
5031 int src_chan
= GET_SWZ(acp
[4 * r
+ c
]->src
[0].swizzle
, c
);
5033 if (acp
[4 * r
+ c
]->src
[0].file
== inst
->dst
[d
].file
&&
5034 acp
[4 * r
+ c
]->src
[0].index
== inst
->dst
[d
].index
&&
5035 inst
->dst
[d
].writemask
& (1 << src_chan
)) {
5036 acp
[4 * r
+ c
] = NULL
;
5045 /* If this is a copy, add it to the ACP. */
5046 if (inst
->op
== TGSI_OPCODE_MOV
&&
5047 inst
->dst
[0].file
== PROGRAM_TEMPORARY
&&
5048 !(inst
->dst
[0].file
== inst
->src
[0].file
&&
5049 inst
->dst
[0].index
== inst
->src
[0].index
) &&
5050 !inst
->dst
[0].reladdr
&&
5051 !inst
->dst
[0].reladdr2
&&
5053 inst
->src
[0].file
!= PROGRAM_ARRAY
&&
5054 !inst
->src
[0].reladdr
&&
5055 !inst
->src
[0].reladdr2
&&
5056 !inst
->src
[0].negate
&&
5057 !inst
->src
[0].abs
) {
5058 for (int i
= 0; i
< 4; i
++) {
5059 if (inst
->dst
[0].writemask
& (1 << i
)) {
5060 acp
[4 * inst
->dst
[0].index
+ i
] = inst
;
5061 acp_level
[4 * inst
->dst
[0].index
+ i
] = level
;
5067 ralloc_free(acp_level
);
5072 * On a basic block basis, tracks available PROGRAM_TEMPORARY registers for dead
5075 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
5076 * will occur. As an example, a TXP production after copy propagation but
5079 * 0: MOV TEMP[1], INPUT[4].xyyy;
5080 * 1: MOV TEMP[1].w, INPUT[4].wwww;
5081 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
5083 * and after this pass:
5085 * 0: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
5088 glsl_to_tgsi_visitor::eliminate_dead_code(void)
5090 glsl_to_tgsi_instruction
**writes
= rzalloc_array(mem_ctx
,
5091 glsl_to_tgsi_instruction
*,
5092 this->next_temp
* 4);
5093 int *write_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
5097 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5098 assert(inst
->dst
[0].file
!= PROGRAM_TEMPORARY
5099 || inst
->dst
[0].index
< this->next_temp
);
5102 case TGSI_OPCODE_BGNLOOP
:
5103 case TGSI_OPCODE_ENDLOOP
:
5104 case TGSI_OPCODE_CONT
:
5105 case TGSI_OPCODE_BRK
:
5106 /* End of a basic block, clear the write array entirely.
5108 * This keeps us from killing dead code when the writes are
5109 * on either side of a loop, even when the register isn't touched
5110 * inside the loop. However, glsl_to_tgsi_visitor doesn't seem to emit
5111 * dead code of this type, so it shouldn't make a difference as long as
5112 * the dead code elimination pass in the GLSL compiler does its job.
5114 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
5117 case TGSI_OPCODE_ENDIF
:
5118 case TGSI_OPCODE_ELSE
:
5119 /* Promote the recorded level of all channels written inside the
5120 * preceding if or else block to the level above the if/else block.
5122 for (int r
= 0; r
< this->next_temp
; r
++) {
5123 for (int c
= 0; c
< 4; c
++) {
5124 if (!writes
[4 * r
+ c
])
5127 if (write_level
[4 * r
+ c
] == level
)
5128 write_level
[4 * r
+ c
] = level
-1;
5131 if(inst
->op
== TGSI_OPCODE_ENDIF
)
5135 case TGSI_OPCODE_IF
:
5136 case TGSI_OPCODE_UIF
:
5138 /* fallthrough to default case to mark the condition as read */
5140 /* Continuing the block, clear any channels from the write array that
5141 * are read by this instruction.
5143 for (unsigned i
= 0; i
< ARRAY_SIZE(inst
->src
); i
++) {
5144 if (inst
->src
[i
].file
== PROGRAM_TEMPORARY
&& inst
->src
[i
].reladdr
){
5145 /* Any temporary might be read, so no dead code elimination
5146 * across this instruction.
5148 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
5149 } else if (inst
->src
[i
].file
== PROGRAM_TEMPORARY
) {
5150 /* Clear where it's used as src. */
5151 int src_chans
= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 0);
5152 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 1);
5153 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 2);
5154 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 3);
5156 for (int c
= 0; c
< 4; c
++) {
5157 if (src_chans
& (1 << c
))
5158 writes
[4 * inst
->src
[i
].index
+ c
] = NULL
;
5162 for (unsigned i
= 0; i
< inst
->tex_offset_num_offset
; i
++) {
5163 if (inst
->tex_offsets
[i
].file
== PROGRAM_TEMPORARY
&& inst
->tex_offsets
[i
].reladdr
){
5164 /* Any temporary might be read, so no dead code elimination
5165 * across this instruction.
5167 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
5168 } else if (inst
->tex_offsets
[i
].file
== PROGRAM_TEMPORARY
) {
5169 /* Clear where it's used as src. */
5170 int src_chans
= 1 << GET_SWZ(inst
->tex_offsets
[i
].swizzle
, 0);
5171 src_chans
|= 1 << GET_SWZ(inst
->tex_offsets
[i
].swizzle
, 1);
5172 src_chans
|= 1 << GET_SWZ(inst
->tex_offsets
[i
].swizzle
, 2);
5173 src_chans
|= 1 << GET_SWZ(inst
->tex_offsets
[i
].swizzle
, 3);
5175 for (int c
= 0; c
< 4; c
++) {
5176 if (src_chans
& (1 << c
))
5177 writes
[4 * inst
->tex_offsets
[i
].index
+ c
] = NULL
;
5184 /* If this instruction writes to a temporary, add it to the write array.
5185 * If there is already an instruction in the write array for one or more
5186 * of the channels, flag that channel write as dead.
5188 for (unsigned i
= 0; i
< ARRAY_SIZE(inst
->dst
); i
++) {
5189 if (inst
->dst
[i
].file
== PROGRAM_TEMPORARY
&&
5190 !inst
->dst
[i
].reladdr
) {
5191 for (int c
= 0; c
< 4; c
++) {
5192 if (inst
->dst
[i
].writemask
& (1 << c
)) {
5193 if (writes
[4 * inst
->dst
[i
].index
+ c
]) {
5194 if (write_level
[4 * inst
->dst
[i
].index
+ c
] < level
)
5197 writes
[4 * inst
->dst
[i
].index
+ c
]->dead_mask
|= (1 << c
);
5199 writes
[4 * inst
->dst
[i
].index
+ c
] = inst
;
5200 write_level
[4 * inst
->dst
[i
].index
+ c
] = level
;
5207 /* Anything still in the write array at this point is dead code. */
5208 for (int r
= 0; r
< this->next_temp
; r
++) {
5209 for (int c
= 0; c
< 4; c
++) {
5210 glsl_to_tgsi_instruction
*inst
= writes
[4 * r
+ c
];
5212 inst
->dead_mask
|= (1 << c
);
5216 /* Now actually remove the instructions that are completely dead and update
5217 * the writemask of other instructions with dead channels.
5219 foreach_in_list_safe(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5220 if (!inst
->dead_mask
|| !inst
->dst
[0].writemask
)
5222 /* No amount of dead masks should remove memory stores */
5223 if (inst
->info
->is_store
)
5226 if ((inst
->dst
[0].writemask
& ~inst
->dead_mask
) == 0) {
5231 if (glsl_base_type_is_64bit(inst
->dst
[0].type
)) {
5232 if (inst
->dead_mask
== WRITEMASK_XY
||
5233 inst
->dead_mask
== WRITEMASK_ZW
)
5234 inst
->dst
[0].writemask
&= ~(inst
->dead_mask
);
5236 inst
->dst
[0].writemask
&= ~(inst
->dead_mask
);
5240 ralloc_free(write_level
);
5241 ralloc_free(writes
);
5246 /* merge DFRACEXP instructions into one. */
5248 glsl_to_tgsi_visitor::merge_two_dsts(void)
5250 foreach_in_list_safe(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5251 glsl_to_tgsi_instruction
*inst2
;
5253 if (num_inst_dst_regs(inst
) != 2)
5256 if (inst
->dst
[0].file
!= PROGRAM_UNDEFINED
&&
5257 inst
->dst
[1].file
!= PROGRAM_UNDEFINED
)
5260 inst2
= (glsl_to_tgsi_instruction
*) inst
->next
;
5263 if (inst
->src
[0].file
== inst2
->src
[0].file
&&
5264 inst
->src
[0].index
== inst2
->src
[0].index
&&
5265 inst
->src
[0].type
== inst2
->src
[0].type
&&
5266 inst
->src
[0].swizzle
== inst2
->src
[0].swizzle
)
5268 inst2
= (glsl_to_tgsi_instruction
*) inst2
->next
;
5274 if (inst
->dst
[0].file
== PROGRAM_UNDEFINED
) {
5276 inst
->dst
[0] = inst2
->dst
[0];
5277 } else if (inst
->dst
[1].file
== PROGRAM_UNDEFINED
) {
5278 inst
->dst
[1] = inst2
->dst
[1];
5289 /* Merges temporary registers together where possible to reduce the number of
5290 * registers needed to run a program.
5292 * Produces optimal code only after copy propagation and dead code elimination
5295 glsl_to_tgsi_visitor::merge_registers(void)
5297 int *last_reads
= rzalloc_array(mem_ctx
, int, this->next_temp
);
5298 int *first_writes
= rzalloc_array(mem_ctx
, int, this->next_temp
);
5299 struct rename_reg_pair
*renames
= rzalloc_array(mem_ctx
, struct rename_reg_pair
, this->next_temp
);
5301 int num_renames
= 0;
5303 /* Read the indices of the last read and first write to each temp register
5304 * into an array so that we don't have to traverse the instruction list as
5306 for (i
= 0; i
< this->next_temp
; i
++) {
5308 first_writes
[i
] = -1;
5310 get_last_temp_read_first_temp_write(last_reads
, first_writes
);
5312 /* Start looking for registers with non-overlapping usages that can be
5313 * merged together. */
5314 for (i
= 0; i
< this->next_temp
; i
++) {
5315 /* Don't touch unused registers. */
5316 if (last_reads
[i
] < 0 || first_writes
[i
] < 0) continue;
5318 for (j
= 0; j
< this->next_temp
; j
++) {
5319 /* Don't touch unused registers. */
5320 if (last_reads
[j
] < 0 || first_writes
[j
] < 0) continue;
5322 /* We can merge the two registers if the first write to j is after or
5323 * in the same instruction as the last read from i. Note that the
5324 * register at index i will always be used earlier or at the same time
5325 * as the register at index j. */
5326 if (first_writes
[i
] <= first_writes
[j
] &&
5327 last_reads
[i
] <= first_writes
[j
]) {
5328 renames
[num_renames
].old_reg
= j
;
5329 renames
[num_renames
].new_reg
= i
;
5332 /* Update the first_writes and last_reads arrays with the new
5333 * values for the merged register index, and mark the newly unused
5334 * register index as such. */
5335 assert(last_reads
[j
] >= last_reads
[i
]);
5336 last_reads
[i
] = last_reads
[j
];
5337 first_writes
[j
] = -1;
5343 rename_temp_registers(num_renames
, renames
);
5344 ralloc_free(renames
);
5345 ralloc_free(last_reads
);
5346 ralloc_free(first_writes
);
5349 /* Reassign indices to temporary registers by reusing unused indices created
5350 * by optimization passes. */
5352 glsl_to_tgsi_visitor::renumber_registers(void)
5356 int *first_reads
= rzalloc_array(mem_ctx
, int, this->next_temp
);
5357 struct rename_reg_pair
*renames
= rzalloc_array(mem_ctx
, struct rename_reg_pair
, this->next_temp
);
5358 int num_renames
= 0;
5359 for (i
= 0; i
< this->next_temp
; i
++) {
5360 first_reads
[i
] = -1;
5362 get_first_temp_read(first_reads
);
5364 for (i
= 0; i
< this->next_temp
; i
++) {
5365 if (first_reads
[i
] < 0) continue;
5366 if (i
!= new_index
) {
5367 renames
[num_renames
].old_reg
= i
;
5368 renames
[num_renames
].new_reg
= new_index
;
5374 rename_temp_registers(num_renames
, renames
);
5375 this->next_temp
= new_index
;
5376 ralloc_free(renames
);
5377 ralloc_free(first_reads
);
5380 /* ------------------------- TGSI conversion stuff -------------------------- */
5383 * Intermediate state used during shader translation.
5385 struct st_translate
{
5386 struct ureg_program
*ureg
;
5388 unsigned temps_size
;
5389 struct ureg_dst
*temps
;
5391 struct ureg_dst
*arrays
;
5392 unsigned num_temp_arrays
;
5393 struct ureg_src
*constants
;
5395 struct ureg_src
*immediates
;
5397 struct ureg_dst outputs
[PIPE_MAX_SHADER_OUTPUTS
];
5398 struct ureg_src inputs
[PIPE_MAX_SHADER_INPUTS
];
5399 struct ureg_dst address
[3];
5400 struct ureg_src samplers
[PIPE_MAX_SAMPLERS
];
5401 struct ureg_src buffers
[PIPE_MAX_SHADER_BUFFERS
];
5402 struct ureg_src images
[PIPE_MAX_SHADER_IMAGES
];
5403 struct ureg_src systemValues
[SYSTEM_VALUE_MAX
];
5404 struct ureg_src shared_memory
;
5405 unsigned *array_sizes
;
5406 struct inout_decl
*input_decls
;
5407 unsigned num_input_decls
;
5408 struct inout_decl
*output_decls
;
5409 unsigned num_output_decls
;
5411 const GLuint
*inputMapping
;
5412 const GLuint
*outputMapping
;
5414 unsigned procType
; /**< PIPE_SHADER_VERTEX/FRAGMENT */
5417 /** Map Mesa's SYSTEM_VALUE_x to TGSI_SEMANTIC_x */
5419 _mesa_sysval_to_semantic(unsigned sysval
)
5423 case SYSTEM_VALUE_VERTEX_ID
:
5424 return TGSI_SEMANTIC_VERTEXID
;
5425 case SYSTEM_VALUE_INSTANCE_ID
:
5426 return TGSI_SEMANTIC_INSTANCEID
;
5427 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
5428 return TGSI_SEMANTIC_VERTEXID_NOBASE
;
5429 case SYSTEM_VALUE_BASE_VERTEX
:
5430 return TGSI_SEMANTIC_BASEVERTEX
;
5431 case SYSTEM_VALUE_BASE_INSTANCE
:
5432 return TGSI_SEMANTIC_BASEINSTANCE
;
5433 case SYSTEM_VALUE_DRAW_ID
:
5434 return TGSI_SEMANTIC_DRAWID
;
5436 /* Geometry shader */
5437 case SYSTEM_VALUE_INVOCATION_ID
:
5438 return TGSI_SEMANTIC_INVOCATIONID
;
5440 /* Fragment shader */
5441 case SYSTEM_VALUE_FRAG_COORD
:
5442 return TGSI_SEMANTIC_POSITION
;
5443 case SYSTEM_VALUE_FRONT_FACE
:
5444 return TGSI_SEMANTIC_FACE
;
5445 case SYSTEM_VALUE_SAMPLE_ID
:
5446 return TGSI_SEMANTIC_SAMPLEID
;
5447 case SYSTEM_VALUE_SAMPLE_POS
:
5448 return TGSI_SEMANTIC_SAMPLEPOS
;
5449 case SYSTEM_VALUE_SAMPLE_MASK_IN
:
5450 return TGSI_SEMANTIC_SAMPLEMASK
;
5451 case SYSTEM_VALUE_HELPER_INVOCATION
:
5452 return TGSI_SEMANTIC_HELPER_INVOCATION
;
5454 /* Tessellation shader */
5455 case SYSTEM_VALUE_TESS_COORD
:
5456 return TGSI_SEMANTIC_TESSCOORD
;
5457 case SYSTEM_VALUE_VERTICES_IN
:
5458 return TGSI_SEMANTIC_VERTICESIN
;
5459 case SYSTEM_VALUE_PRIMITIVE_ID
:
5460 return TGSI_SEMANTIC_PRIMID
;
5461 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
5462 return TGSI_SEMANTIC_TESSOUTER
;
5463 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
5464 return TGSI_SEMANTIC_TESSINNER
;
5466 /* Compute shader */
5467 case SYSTEM_VALUE_LOCAL_INVOCATION_ID
:
5468 return TGSI_SEMANTIC_THREAD_ID
;
5469 case SYSTEM_VALUE_WORK_GROUP_ID
:
5470 return TGSI_SEMANTIC_BLOCK_ID
;
5471 case SYSTEM_VALUE_NUM_WORK_GROUPS
:
5472 return TGSI_SEMANTIC_GRID_SIZE
;
5473 case SYSTEM_VALUE_LOCAL_GROUP_SIZE
:
5474 return TGSI_SEMANTIC_BLOCK_SIZE
;
5476 /* ARB_shader_ballot */
5477 case SYSTEM_VALUE_SUBGROUP_SIZE
:
5478 return TGSI_SEMANTIC_SUBGROUP_SIZE
;
5479 case SYSTEM_VALUE_SUBGROUP_INVOCATION
:
5480 return TGSI_SEMANTIC_SUBGROUP_INVOCATION
;
5481 case SYSTEM_VALUE_SUBGROUP_EQ_MASK
:
5482 return TGSI_SEMANTIC_SUBGROUP_EQ_MASK
;
5483 case SYSTEM_VALUE_SUBGROUP_GE_MASK
:
5484 return TGSI_SEMANTIC_SUBGROUP_GE_MASK
;
5485 case SYSTEM_VALUE_SUBGROUP_GT_MASK
:
5486 return TGSI_SEMANTIC_SUBGROUP_GT_MASK
;
5487 case SYSTEM_VALUE_SUBGROUP_LE_MASK
:
5488 return TGSI_SEMANTIC_SUBGROUP_LE_MASK
;
5489 case SYSTEM_VALUE_SUBGROUP_LT_MASK
:
5490 return TGSI_SEMANTIC_SUBGROUP_LT_MASK
;
5493 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
:
5494 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID
:
5495 case SYSTEM_VALUE_VERTEX_CNT
:
5497 assert(!"Unexpected SYSTEM_VALUE_ enum");
5498 return TGSI_SEMANTIC_COUNT
;
5503 * Map a glsl_to_tgsi constant/immediate to a TGSI immediate.
5505 static struct ureg_src
5506 emit_immediate(struct st_translate
*t
,
5507 gl_constant_value values
[4],
5510 struct ureg_program
*ureg
= t
->ureg
;
5515 return ureg_DECL_immediate(ureg
, &values
[0].f
, size
);
5517 return ureg_DECL_immediate_f64(ureg
, (double *)&values
[0].f
, size
);
5519 return ureg_DECL_immediate_int64(ureg
, (int64_t *)&values
[0].f
, size
);
5520 case GL_UNSIGNED_INT64_ARB
:
5521 return ureg_DECL_immediate_uint64(ureg
, (uint64_t *)&values
[0].f
, size
);
5523 return ureg_DECL_immediate_int(ureg
, &values
[0].i
, size
);
5524 case GL_UNSIGNED_INT
:
5526 return ureg_DECL_immediate_uint(ureg
, &values
[0].u
, size
);
5528 assert(!"should not get here - type must be float, int, uint, or bool");
5529 return ureg_src_undef();
5534 * Map a glsl_to_tgsi dst register to a TGSI ureg_dst register.
5536 static struct ureg_dst
5537 dst_register(struct st_translate
*t
, gl_register_file file
, unsigned index
,
5543 case PROGRAM_UNDEFINED
:
5544 return ureg_dst_undef();
5546 case PROGRAM_TEMPORARY
:
5547 /* Allocate space for temporaries on demand. */
5548 if (index
>= t
->temps_size
) {
5549 const int inc
= align(index
- t
->temps_size
+ 1, 4096);
5551 t
->temps
= (struct ureg_dst
*)
5553 (t
->temps_size
+ inc
) * sizeof(struct ureg_dst
));
5555 return ureg_dst_undef();
5557 memset(t
->temps
+ t
->temps_size
, 0, inc
* sizeof(struct ureg_dst
));
5558 t
->temps_size
+= inc
;
5561 if (ureg_dst_is_undef(t
->temps
[index
]))
5562 t
->temps
[index
] = ureg_DECL_local_temporary(t
->ureg
);
5564 return t
->temps
[index
];
5567 assert(array_id
&& array_id
<= t
->num_temp_arrays
);
5568 array
= array_id
- 1;
5570 if (ureg_dst_is_undef(t
->arrays
[array
]))
5571 t
->arrays
[array
] = ureg_DECL_array_temporary(
5572 t
->ureg
, t
->array_sizes
[array
], TRUE
);
5574 return ureg_dst_array_offset(t
->arrays
[array
], index
);
5576 case PROGRAM_OUTPUT
:
5578 if (t
->procType
== PIPE_SHADER_FRAGMENT
)
5579 assert(index
< 2 * FRAG_RESULT_MAX
);
5580 else if (t
->procType
== PIPE_SHADER_TESS_CTRL
||
5581 t
->procType
== PIPE_SHADER_TESS_EVAL
)
5582 assert(index
< VARYING_SLOT_TESS_MAX
);
5584 assert(index
< VARYING_SLOT_MAX
);
5586 assert(t
->outputMapping
[index
] < ARRAY_SIZE(t
->outputs
));
5587 assert(t
->outputs
[t
->outputMapping
[index
]].File
!= TGSI_FILE_NULL
);
5588 return t
->outputs
[t
->outputMapping
[index
]];
5591 struct inout_decl
*decl
= find_inout_array(t
->output_decls
, t
->num_output_decls
, array_id
);
5592 unsigned mesa_index
= decl
->mesa_index
;
5593 int slot
= t
->outputMapping
[mesa_index
];
5595 assert(slot
!= -1 && t
->outputs
[slot
].File
== TGSI_FILE_OUTPUT
);
5597 struct ureg_dst dst
= t
->outputs
[slot
];
5598 dst
.ArrayID
= array_id
;
5599 return ureg_dst_array_offset(dst
, index
- mesa_index
);
5602 case PROGRAM_ADDRESS
:
5603 return t
->address
[index
];
5606 assert(!"unknown dst register file");
5607 return ureg_dst_undef();
5612 * Map a glsl_to_tgsi src register to a TGSI ureg_src register.
5614 static struct ureg_src
5615 src_register(struct st_translate
*t
, const st_src_reg
*reg
)
5617 int index
= reg
->index
;
5618 int double_reg2
= reg
->double_reg2
? 1 : 0;
5621 case PROGRAM_UNDEFINED
:
5622 return ureg_imm4f(t
->ureg
, 0, 0, 0, 0);
5624 case PROGRAM_TEMPORARY
:
5626 return ureg_src(dst_register(t
, reg
->file
, reg
->index
, reg
->array_id
));
5628 case PROGRAM_OUTPUT
: {
5629 struct ureg_dst dst
= dst_register(t
, reg
->file
, reg
->index
, reg
->array_id
);
5630 assert(dst
.WriteMask
!= 0);
5631 unsigned shift
= ffs(dst
.WriteMask
) - 1;
5632 return ureg_swizzle(ureg_src(dst
),
5636 MIN2(shift
+ 3, 3));
5639 case PROGRAM_UNIFORM
:
5640 assert(reg
->index
>= 0);
5641 return reg
->index
< t
->num_constants
?
5642 t
->constants
[reg
->index
] : ureg_imm4f(t
->ureg
, 0, 0, 0, 0);
5643 case PROGRAM_STATE_VAR
:
5644 case PROGRAM_CONSTANT
: /* ie, immediate */
5645 if (reg
->has_index2
)
5646 return ureg_src_register(TGSI_FILE_CONSTANT
, reg
->index
);
5648 return reg
->index
>= 0 && reg
->index
< t
->num_constants
?
5649 t
->constants
[reg
->index
] : ureg_imm4f(t
->ureg
, 0, 0, 0, 0);
5651 case PROGRAM_IMMEDIATE
:
5652 assert(reg
->index
>= 0 && reg
->index
< t
->num_immediates
);
5653 return t
->immediates
[reg
->index
];
5656 /* GLSL inputs are 64-bit containers, so we have to
5657 * map back to the original index and add the offset after
5659 index
-= double_reg2
;
5660 if (!reg
->array_id
) {
5661 assert(t
->inputMapping
[index
] < ARRAY_SIZE(t
->inputs
));
5662 assert(t
->inputs
[t
->inputMapping
[index
]].File
!= TGSI_FILE_NULL
);
5663 return t
->inputs
[t
->inputMapping
[index
] + double_reg2
];
5666 struct inout_decl
*decl
= find_inout_array(t
->input_decls
, t
->num_input_decls
, reg
->array_id
);
5667 unsigned mesa_index
= decl
->mesa_index
;
5668 int slot
= t
->inputMapping
[mesa_index
];
5670 assert(slot
!= -1 && t
->inputs
[slot
].File
== TGSI_FILE_INPUT
);
5672 struct ureg_src src
= t
->inputs
[slot
];
5673 src
.ArrayID
= reg
->array_id
;
5674 return ureg_src_array_offset(src
, index
+ double_reg2
- mesa_index
);
5677 case PROGRAM_ADDRESS
:
5678 return ureg_src(t
->address
[reg
->index
]);
5680 case PROGRAM_SYSTEM_VALUE
:
5681 assert(reg
->index
< (int) ARRAY_SIZE(t
->systemValues
));
5682 return t
->systemValues
[reg
->index
];
5685 assert(!"unknown src register file");
5686 return ureg_src_undef();
5691 * Create a TGSI ureg_dst register from an st_dst_reg.
5693 static struct ureg_dst
5694 translate_dst(struct st_translate
*t
,
5695 const st_dst_reg
*dst_reg
,
5698 struct ureg_dst dst
= dst_register(t
, dst_reg
->file
, dst_reg
->index
,
5701 if (dst
.File
== TGSI_FILE_NULL
)
5704 dst
= ureg_writemask(dst
, dst_reg
->writemask
);
5707 dst
= ureg_saturate(dst
);
5709 if (dst_reg
->reladdr
!= NULL
) {
5710 assert(dst_reg
->file
!= PROGRAM_TEMPORARY
);
5711 dst
= ureg_dst_indirect(dst
, ureg_src(t
->address
[0]));
5714 if (dst_reg
->has_index2
) {
5715 if (dst_reg
->reladdr2
)
5716 dst
= ureg_dst_dimension_indirect(dst
, ureg_src(t
->address
[1]),
5719 dst
= ureg_dst_dimension(dst
, dst_reg
->index2D
);
5726 * Create a TGSI ureg_src register from an st_src_reg.
5728 static struct ureg_src
5729 translate_src(struct st_translate
*t
, const st_src_reg
*src_reg
)
5731 struct ureg_src src
= src_register(t
, src_reg
);
5733 if (src_reg
->has_index2
) {
5734 /* 2D indexes occur with geometry shader inputs (attrib, vertex)
5735 * and UBO constant buffers (buffer, position).
5737 if (src_reg
->reladdr2
)
5738 src
= ureg_src_dimension_indirect(src
, ureg_src(t
->address
[1]),
5741 src
= ureg_src_dimension(src
, src_reg
->index2D
);
5744 src
= ureg_swizzle(src
,
5745 GET_SWZ(src_reg
->swizzle
, 0) & 0x3,
5746 GET_SWZ(src_reg
->swizzle
, 1) & 0x3,
5747 GET_SWZ(src_reg
->swizzle
, 2) & 0x3,
5748 GET_SWZ(src_reg
->swizzle
, 3) & 0x3);
5751 src
= ureg_abs(src
);
5753 if ((src_reg
->negate
& 0xf) == NEGATE_XYZW
)
5754 src
= ureg_negate(src
);
5756 if (src_reg
->reladdr
!= NULL
) {
5757 assert(src_reg
->file
!= PROGRAM_TEMPORARY
);
5758 src
= ureg_src_indirect(src
, ureg_src(t
->address
[0]));
5764 static struct tgsi_texture_offset
5765 translate_tex_offset(struct st_translate
*t
,
5766 const st_src_reg
*in_offset
)
5768 struct tgsi_texture_offset offset
;
5769 struct ureg_src src
= translate_src(t
, in_offset
);
5771 offset
.File
= src
.File
;
5772 offset
.Index
= src
.Index
;
5773 offset
.SwizzleX
= src
.SwizzleX
;
5774 offset
.SwizzleY
= src
.SwizzleY
;
5775 offset
.SwizzleZ
= src
.SwizzleZ
;
5778 assert(!src
.Indirect
);
5779 assert(!src
.DimIndirect
);
5780 assert(!src
.Dimension
);
5781 assert(!src
.Absolute
); /* those shouldn't be used with integers anyway */
5782 assert(!src
.Negate
);
5788 compile_tgsi_instruction(struct st_translate
*t
,
5789 const glsl_to_tgsi_instruction
*inst
)
5791 struct ureg_program
*ureg
= t
->ureg
;
5793 struct ureg_dst dst
[2];
5794 struct ureg_src src
[4];
5795 struct tgsi_texture_offset texoffsets
[MAX_GLSL_TEXTURE_OFFSET
];
5799 unsigned tex_target
= 0;
5801 num_dst
= num_inst_dst_regs(inst
);
5802 num_src
= num_inst_src_regs(inst
);
5804 for (i
= 0; i
< num_dst
; i
++)
5805 dst
[i
] = translate_dst(t
,
5809 for (i
= 0; i
< num_src
; i
++)
5810 src
[i
] = translate_src(t
, &inst
->src
[i
]);
5813 case TGSI_OPCODE_BGNLOOP
:
5814 case TGSI_OPCODE_ELSE
:
5815 case TGSI_OPCODE_ENDLOOP
:
5816 case TGSI_OPCODE_IF
:
5817 case TGSI_OPCODE_UIF
:
5818 assert(num_dst
== 0);
5819 ureg_insn(ureg
, inst
->op
, NULL
, 0, src
, num_src
);
5822 case TGSI_OPCODE_TEX
:
5823 case TGSI_OPCODE_TEX_LZ
:
5824 case TGSI_OPCODE_TXB
:
5825 case TGSI_OPCODE_TXD
:
5826 case TGSI_OPCODE_TXL
:
5827 case TGSI_OPCODE_TXP
:
5828 case TGSI_OPCODE_TXQ
:
5829 case TGSI_OPCODE_TXQS
:
5830 case TGSI_OPCODE_TXF
:
5831 case TGSI_OPCODE_TXF_LZ
:
5832 case TGSI_OPCODE_TEX2
:
5833 case TGSI_OPCODE_TXB2
:
5834 case TGSI_OPCODE_TXL2
:
5835 case TGSI_OPCODE_TG4
:
5836 case TGSI_OPCODE_LODQ
:
5837 src
[num_src
] = t
->samplers
[inst
->resource
.index
];
5838 assert(src
[num_src
].File
!= TGSI_FILE_NULL
);
5839 if (inst
->resource
.reladdr
)
5841 ureg_src_indirect(src
[num_src
], ureg_src(t
->address
[2]));
5843 for (i
= 0; i
< (int)inst
->tex_offset_num_offset
; i
++) {
5844 texoffsets
[i
] = translate_tex_offset(t
, &inst
->tex_offsets
[i
]);
5846 tex_target
= st_translate_texture_target(inst
->tex_target
, inst
->tex_shadow
);
5852 texoffsets
, inst
->tex_offset_num_offset
,
5856 case TGSI_OPCODE_RESQ
:
5857 case TGSI_OPCODE_LOAD
:
5858 case TGSI_OPCODE_ATOMUADD
:
5859 case TGSI_OPCODE_ATOMXCHG
:
5860 case TGSI_OPCODE_ATOMCAS
:
5861 case TGSI_OPCODE_ATOMAND
:
5862 case TGSI_OPCODE_ATOMOR
:
5863 case TGSI_OPCODE_ATOMXOR
:
5864 case TGSI_OPCODE_ATOMUMIN
:
5865 case TGSI_OPCODE_ATOMUMAX
:
5866 case TGSI_OPCODE_ATOMIMIN
:
5867 case TGSI_OPCODE_ATOMIMAX
:
5868 for (i
= num_src
- 1; i
>= 0; i
--)
5869 src
[i
+ 1] = src
[i
];
5871 if (inst
->resource
.file
== PROGRAM_MEMORY
) {
5872 src
[0] = t
->shared_memory
;
5873 } else if (inst
->resource
.file
== PROGRAM_BUFFER
) {
5874 src
[0] = t
->buffers
[inst
->resource
.index
];
5876 src
[0] = t
->images
[inst
->resource
.index
];
5877 tex_target
= st_translate_texture_target(inst
->tex_target
, inst
->tex_shadow
);
5879 if (inst
->resource
.reladdr
)
5880 src
[0] = ureg_src_indirect(src
[0], ureg_src(t
->address
[2]));
5881 assert(src
[0].File
!= TGSI_FILE_NULL
);
5882 ureg_memory_insn(ureg
, inst
->op
, dst
, num_dst
, src
, num_src
,
5883 inst
->buffer_access
,
5884 tex_target
, inst
->image_format
);
5887 case TGSI_OPCODE_STORE
:
5888 if (inst
->resource
.file
== PROGRAM_MEMORY
) {
5889 dst
[0] = ureg_dst(t
->shared_memory
);
5890 } else if (inst
->resource
.file
== PROGRAM_BUFFER
) {
5891 dst
[0] = ureg_dst(t
->buffers
[inst
->resource
.index
]);
5893 dst
[0] = ureg_dst(t
->images
[inst
->resource
.index
]);
5894 tex_target
= st_translate_texture_target(inst
->tex_target
, inst
->tex_shadow
);
5896 dst
[0] = ureg_writemask(dst
[0], inst
->dst
[0].writemask
);
5897 if (inst
->resource
.reladdr
)
5898 dst
[0] = ureg_dst_indirect(dst
[0], ureg_src(t
->address
[2]));
5899 assert(dst
[0].File
!= TGSI_FILE_NULL
);
5900 ureg_memory_insn(ureg
, inst
->op
, dst
, num_dst
, src
, num_src
,
5901 inst
->buffer_access
,
5902 tex_target
, inst
->image_format
);
5905 case TGSI_OPCODE_SCS
:
5906 dst
[0] = ureg_writemask(dst
[0], TGSI_WRITEMASK_XY
);
5907 ureg_insn(ureg
, inst
->op
, dst
, num_dst
, src
, num_src
);
5920 * Emit the TGSI instructions for inverting and adjusting WPOS.
5921 * This code is unavoidable because it also depends on whether
5922 * a FBO is bound (STATE_FB_WPOS_Y_TRANSFORM).
5925 emit_wpos_adjustment(struct gl_context
*ctx
,
5926 struct st_translate
*t
,
5927 int wpos_transform_const
,
5929 GLfloat adjX
, GLfloat adjY
[2])
5931 struct ureg_program
*ureg
= t
->ureg
;
5933 assert(wpos_transform_const
>= 0);
5935 /* Fragment program uses fragment position input.
5936 * Need to replace instances of INPUT[WPOS] with temp T
5937 * where T = INPUT[WPOS] is inverted by Y.
5939 struct ureg_src wpostrans
= ureg_DECL_constant(ureg
, wpos_transform_const
);
5940 struct ureg_dst wpos_temp
= ureg_DECL_temporary( ureg
);
5941 struct ureg_src
*wpos
=
5942 ctx
->Const
.GLSLFragCoordIsSysVal
?
5943 &t
->systemValues
[SYSTEM_VALUE_FRAG_COORD
] :
5944 &t
->inputs
[t
->inputMapping
[VARYING_SLOT_POS
]];
5945 struct ureg_src wpos_input
= *wpos
;
5947 /* First, apply the coordinate shift: */
5948 if (adjX
|| adjY
[0] || adjY
[1]) {
5949 if (adjY
[0] != adjY
[1]) {
5950 /* Adjust the y coordinate by adjY[1] or adjY[0] respectively
5951 * depending on whether inversion is actually going to be applied
5952 * or not, which is determined by testing against the inversion
5953 * state variable used below, which will be either +1 or -1.
5955 struct ureg_dst adj_temp
= ureg_DECL_local_temporary(ureg
);
5957 ureg_CMP(ureg
, adj_temp
,
5958 ureg_scalar(wpostrans
, invert
? 2 : 0),
5959 ureg_imm4f(ureg
, adjX
, adjY
[0], 0.0f
, 0.0f
),
5960 ureg_imm4f(ureg
, adjX
, adjY
[1], 0.0f
, 0.0f
));
5961 ureg_ADD(ureg
, wpos_temp
, wpos_input
, ureg_src(adj_temp
));
5963 ureg_ADD(ureg
, wpos_temp
, wpos_input
,
5964 ureg_imm4f(ureg
, adjX
, adjY
[0], 0.0f
, 0.0f
));
5966 wpos_input
= ureg_src(wpos_temp
);
5968 /* MOV wpos_temp, input[wpos]
5970 ureg_MOV( ureg
, wpos_temp
, wpos_input
);
5973 /* Now the conditional y flip: STATE_FB_WPOS_Y_TRANSFORM.xy/zw will be
5974 * inversion/identity, or the other way around if we're drawing to an FBO.
5977 /* MAD wpos_temp.y, wpos_input, wpostrans.xxxx, wpostrans.yyyy
5980 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
5982 ureg_scalar(wpostrans
, 0),
5983 ureg_scalar(wpostrans
, 1));
5985 /* MAD wpos_temp.y, wpos_input, wpostrans.zzzz, wpostrans.wwww
5988 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
5990 ureg_scalar(wpostrans
, 2),
5991 ureg_scalar(wpostrans
, 3));
5994 /* Use wpos_temp as position input from here on:
5996 *wpos
= ureg_src(wpos_temp
);
6001 * Emit fragment position/ooordinate code.
6004 emit_wpos(struct st_context
*st
,
6005 struct st_translate
*t
,
6006 const struct gl_program
*program
,
6007 struct ureg_program
*ureg
,
6008 int wpos_transform_const
)
6010 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
6011 GLfloat adjX
= 0.0f
;
6012 GLfloat adjY
[2] = { 0.0f
, 0.0f
};
6013 boolean invert
= FALSE
;
6015 /* Query the pixel center conventions supported by the pipe driver and set
6016 * adjX, adjY to help out if it cannot handle the requested one internally.
6018 * The bias of the y-coordinate depends on whether y-inversion takes place
6019 * (adjY[1]) or not (adjY[0]), which is in turn dependent on whether we are
6020 * drawing to an FBO (causes additional inversion), and whether the pipe
6021 * driver origin and the requested origin differ (the latter condition is
6022 * stored in the 'invert' variable).
6024 * For height = 100 (i = integer, h = half-integer, l = lower, u = upper):
6026 * center shift only:
6031 * l,i -> u,i: ( 0.0 + 1.0) * -1 + 100 = 99
6032 * l,h -> u,h: ( 0.5 + 0.0) * -1 + 100 = 99.5
6033 * u,i -> l,i: (99.0 + 1.0) * -1 + 100 = 0
6034 * u,h -> l,h: (99.5 + 0.0) * -1 + 100 = 0.5
6036 * inversion and center shift:
6037 * l,i -> u,h: ( 0.0 + 0.5) * -1 + 100 = 99.5
6038 * l,h -> u,i: ( 0.5 + 0.5) * -1 + 100 = 99
6039 * u,i -> l,h: (99.0 + 0.5) * -1 + 100 = 0.5
6040 * u,h -> l,i: (99.5 + 0.5) * -1 + 100 = 0
6042 if (program
->OriginUpperLeft
) {
6043 /* Fragment shader wants origin in upper-left */
6044 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
)) {
6045 /* the driver supports upper-left origin */
6047 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
)) {
6048 /* the driver supports lower-left origin, need to invert Y */
6049 ureg_property(ureg
, TGSI_PROPERTY_FS_COORD_ORIGIN
,
6050 TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
6057 /* Fragment shader wants origin in lower-left */
6058 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
))
6059 /* the driver supports lower-left origin */
6060 ureg_property(ureg
, TGSI_PROPERTY_FS_COORD_ORIGIN
,
6061 TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
6062 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
))
6063 /* the driver supports upper-left origin, need to invert Y */
6069 if (program
->PixelCenterInteger
) {
6070 /* Fragment shader wants pixel center integer */
6071 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)) {
6072 /* the driver supports pixel center integer */
6074 ureg_property(ureg
, TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
,
6075 TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
6077 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
)) {
6078 /* the driver supports pixel center half integer, need to bias X,Y */
6087 /* Fragment shader wants pixel center half integer */
6088 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
)) {
6089 /* the driver supports pixel center half integer */
6091 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)) {
6092 /* the driver supports pixel center integer, need to bias X,Y */
6093 adjX
= adjY
[0] = adjY
[1] = 0.5f
;
6094 ureg_property(ureg
, TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
,
6095 TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
6101 /* we invert after adjustment so that we avoid the MOV to temporary,
6102 * and reuse the adjustment ADD instead */
6103 emit_wpos_adjustment(st
->ctx
, t
, wpos_transform_const
, invert
, adjX
, adjY
);
6107 * OpenGL's fragment gl_FrontFace input is 1 for front-facing, 0 for back.
6108 * TGSI uses +1 for front, -1 for back.
6109 * This function converts the TGSI value to the GL value. Simply clamping/
6110 * saturating the value to [0,1] does the job.
6113 emit_face_var(struct gl_context
*ctx
, struct st_translate
*t
)
6115 struct ureg_program
*ureg
= t
->ureg
;
6116 struct ureg_dst face_temp
= ureg_DECL_temporary(ureg
);
6117 struct ureg_src face_input
= t
->inputs
[t
->inputMapping
[VARYING_SLOT_FACE
]];
6119 if (ctx
->Const
.NativeIntegers
) {
6120 ureg_FSGE(ureg
, face_temp
, face_input
, ureg_imm1f(ureg
, 0));
6123 /* MOV_SAT face_temp, input[face] */
6124 ureg_MOV(ureg
, ureg_saturate(face_temp
), face_input
);
6127 /* Use face_temp as face input from here on: */
6128 t
->inputs
[t
->inputMapping
[VARYING_SLOT_FACE
]] = ureg_src(face_temp
);
6132 emit_compute_block_size(const struct gl_program
*prog
,
6133 struct ureg_program
*ureg
) {
6134 ureg_property(ureg
, TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
,
6135 prog
->info
.cs
.local_size
[0]);
6136 ureg_property(ureg
, TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT
,
6137 prog
->info
.cs
.local_size
[1]);
6138 ureg_property(ureg
, TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH
,
6139 prog
->info
.cs
.local_size
[2]);
6142 struct sort_inout_decls
{
6143 bool operator()(const struct inout_decl
&a
, const struct inout_decl
&b
) const {
6144 return mapping
[a
.mesa_index
] < mapping
[b
.mesa_index
];
6147 const GLuint
*mapping
;
6150 /* Sort the given array of decls by the corresponding slot (TGSI file index).
6152 * This is for the benefit of older drivers which are broken when the
6153 * declarations aren't sorted in this way.
6156 sort_inout_decls_by_slot(struct inout_decl
*decls
,
6158 const GLuint mapping
[])
6160 sort_inout_decls sorter
;
6161 sorter
.mapping
= mapping
;
6162 std::sort(decls
, decls
+ count
, sorter
);
6166 st_translate_interp(enum glsl_interp_mode glsl_qual
, GLuint varying
)
6168 switch (glsl_qual
) {
6169 case INTERP_MODE_NONE
:
6170 if (varying
== VARYING_SLOT_COL0
|| varying
== VARYING_SLOT_COL1
)
6171 return TGSI_INTERPOLATE_COLOR
;
6172 return TGSI_INTERPOLATE_PERSPECTIVE
;
6173 case INTERP_MODE_SMOOTH
:
6174 return TGSI_INTERPOLATE_PERSPECTIVE
;
6175 case INTERP_MODE_FLAT
:
6176 return TGSI_INTERPOLATE_CONSTANT
;
6177 case INTERP_MODE_NOPERSPECTIVE
:
6178 return TGSI_INTERPOLATE_LINEAR
;
6180 assert(0 && "unexpected interp mode in st_translate_interp()");
6181 return TGSI_INTERPOLATE_PERSPECTIVE
;
6186 * Translate intermediate IR (glsl_to_tgsi_instruction) to TGSI format.
6187 * \param program the program to translate
6188 * \param numInputs number of input registers used
6189 * \param inputMapping maps Mesa fragment program inputs to TGSI generic
6191 * \param inputSemanticName the TGSI_SEMANTIC flag for each input
6192 * \param inputSemanticIndex the semantic index (ex: which texcoord) for
6194 * \param interpMode the TGSI_INTERPOLATE_LINEAR/PERSP mode for each input
6195 * \param numOutputs number of output registers used
6196 * \param outputMapping maps Mesa fragment program outputs to TGSI
6198 * \param outputSemanticName the TGSI_SEMANTIC flag for each output
6199 * \param outputSemanticIndex the semantic index (ex: which texcoord) for
6202 * \return PIPE_OK or PIPE_ERROR_OUT_OF_MEMORY
6204 extern "C" enum pipe_error
6205 st_translate_program(
6206 struct gl_context
*ctx
,
6208 struct ureg_program
*ureg
,
6209 glsl_to_tgsi_visitor
*program
,
6210 const struct gl_program
*proginfo
,
6212 const GLuint inputMapping
[],
6213 const GLuint inputSlotToAttr
[],
6214 const ubyte inputSemanticName
[],
6215 const ubyte inputSemanticIndex
[],
6216 const GLuint interpMode
[],
6218 const GLuint outputMapping
[],
6219 const GLuint outputSlotToAttr
[],
6220 const ubyte outputSemanticName
[],
6221 const ubyte outputSemanticIndex
[])
6223 struct st_translate
*t
;
6225 struct gl_program_constants
*frag_const
=
6226 &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
6227 enum pipe_error ret
= PIPE_OK
;
6229 assert(numInputs
<= ARRAY_SIZE(t
->inputs
));
6230 assert(numOutputs
<= ARRAY_SIZE(t
->outputs
));
6232 t
= CALLOC_STRUCT(st_translate
);
6234 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
6238 t
->procType
= procType
;
6239 t
->inputMapping
= inputMapping
;
6240 t
->outputMapping
= outputMapping
;
6242 t
->num_temp_arrays
= program
->next_array
;
6243 if (t
->num_temp_arrays
)
6244 t
->arrays
= (struct ureg_dst
*)
6245 calloc(t
->num_temp_arrays
, sizeof(t
->arrays
[0]));
6248 * Declare input attributes.
6251 case PIPE_SHADER_FRAGMENT
:
6252 case PIPE_SHADER_GEOMETRY
:
6253 case PIPE_SHADER_TESS_EVAL
:
6254 case PIPE_SHADER_TESS_CTRL
:
6255 sort_inout_decls_by_slot(program
->inputs
, program
->num_inputs
, inputMapping
);
6257 for (i
= 0; i
< program
->num_inputs
; ++i
) {
6258 struct inout_decl
*decl
= &program
->inputs
[i
];
6259 unsigned slot
= inputMapping
[decl
->mesa_index
];
6260 struct ureg_src src
;
6261 ubyte tgsi_usage_mask
= decl
->usage_mask
;
6263 if (glsl_base_type_is_64bit(decl
->base_type
)) {
6264 if (tgsi_usage_mask
== 1)
6265 tgsi_usage_mask
= TGSI_WRITEMASK_XY
;
6266 else if (tgsi_usage_mask
== 2)
6267 tgsi_usage_mask
= TGSI_WRITEMASK_ZW
;
6269 tgsi_usage_mask
= TGSI_WRITEMASK_XYZW
;
6272 unsigned interp_mode
= 0;
6273 unsigned interp_location
= 0;
6274 if (procType
== PIPE_SHADER_FRAGMENT
) {
6276 interp_mode
= interpMode
[slot
] != TGSI_INTERPOLATE_COUNT
?
6278 st_translate_interp(decl
->interp
, inputSlotToAttr
[slot
]);
6280 interp_location
= decl
->interp_loc
;
6283 src
= ureg_DECL_fs_input_cyl_centroid_layout(ureg
,
6284 inputSemanticName
[slot
], inputSemanticIndex
[slot
],
6285 interp_mode
, 0, interp_location
, slot
, tgsi_usage_mask
,
6286 decl
->array_id
, decl
->size
);
6288 for (unsigned j
= 0; j
< decl
->size
; ++j
) {
6289 if (t
->inputs
[slot
+ j
].File
!= TGSI_FILE_INPUT
) {
6290 /* The ArrayID is set up in dst_register */
6291 t
->inputs
[slot
+ j
] = src
;
6292 t
->inputs
[slot
+ j
].ArrayID
= 0;
6293 t
->inputs
[slot
+ j
].Index
+= j
;
6298 case PIPE_SHADER_VERTEX
:
6299 for (i
= 0; i
< numInputs
; i
++) {
6300 t
->inputs
[i
] = ureg_DECL_vs_input(ureg
, i
);
6303 case PIPE_SHADER_COMPUTE
:
6310 * Declare output attributes.
6313 case PIPE_SHADER_FRAGMENT
:
6314 case PIPE_SHADER_COMPUTE
:
6316 case PIPE_SHADER_GEOMETRY
:
6317 case PIPE_SHADER_TESS_EVAL
:
6318 case PIPE_SHADER_TESS_CTRL
:
6319 case PIPE_SHADER_VERTEX
:
6320 sort_inout_decls_by_slot(program
->outputs
, program
->num_outputs
, outputMapping
);
6322 for (i
= 0; i
< program
->num_outputs
; ++i
) {
6323 struct inout_decl
*decl
= &program
->outputs
[i
];
6324 unsigned slot
= outputMapping
[decl
->mesa_index
];
6325 struct ureg_dst dst
;
6326 ubyte tgsi_usage_mask
= decl
->usage_mask
;
6328 if (glsl_base_type_is_64bit(decl
->base_type
)) {
6329 if (tgsi_usage_mask
== 1)
6330 tgsi_usage_mask
= TGSI_WRITEMASK_XY
;
6331 else if (tgsi_usage_mask
== 2)
6332 tgsi_usage_mask
= TGSI_WRITEMASK_ZW
;
6334 tgsi_usage_mask
= TGSI_WRITEMASK_XYZW
;
6337 dst
= ureg_DECL_output_layout(ureg
,
6338 outputSemanticName
[slot
], outputSemanticIndex
[slot
],
6339 decl
->gs_out_streams
,
6340 slot
, tgsi_usage_mask
, decl
->array_id
, decl
->size
);
6342 for (unsigned j
= 0; j
< decl
->size
; ++j
) {
6343 if (t
->outputs
[slot
+ j
].File
!= TGSI_FILE_OUTPUT
) {
6344 /* The ArrayID is set up in dst_register */
6345 t
->outputs
[slot
+ j
] = dst
;
6346 t
->outputs
[slot
+ j
].ArrayID
= 0;
6347 t
->outputs
[slot
+ j
].Index
+= j
;
6356 if (procType
== PIPE_SHADER_FRAGMENT
) {
6357 if (program
->shader
->Program
->info
.fs
.early_fragment_tests
)
6358 ureg_property(ureg
, TGSI_PROPERTY_FS_EARLY_DEPTH_STENCIL
, 1);
6360 if (proginfo
->info
.inputs_read
& VARYING_BIT_POS
) {
6361 /* Must do this after setting up t->inputs. */
6362 emit_wpos(st_context(ctx
), t
, proginfo
, ureg
,
6363 program
->wpos_transform_const
);
6366 if (proginfo
->info
.inputs_read
& VARYING_BIT_FACE
)
6367 emit_face_var(ctx
, t
);
6369 for (i
= 0; i
< numOutputs
; i
++) {
6370 switch (outputSemanticName
[i
]) {
6371 case TGSI_SEMANTIC_POSITION
:
6372 t
->outputs
[i
] = ureg_DECL_output(ureg
,
6373 TGSI_SEMANTIC_POSITION
, /* Z/Depth */
6374 outputSemanticIndex
[i
]);
6375 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_Z
);
6377 case TGSI_SEMANTIC_STENCIL
:
6378 t
->outputs
[i
] = ureg_DECL_output(ureg
,
6379 TGSI_SEMANTIC_STENCIL
, /* Stencil */
6380 outputSemanticIndex
[i
]);
6381 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_Y
);
6383 case TGSI_SEMANTIC_COLOR
:
6384 t
->outputs
[i
] = ureg_DECL_output(ureg
,
6385 TGSI_SEMANTIC_COLOR
,
6386 outputSemanticIndex
[i
]);
6388 case TGSI_SEMANTIC_SAMPLEMASK
:
6389 t
->outputs
[i
] = ureg_DECL_output(ureg
,
6390 TGSI_SEMANTIC_SAMPLEMASK
,
6391 outputSemanticIndex
[i
]);
6392 /* TODO: If we ever support more than 32 samples, this will have
6393 * to become an array.
6395 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_X
);
6398 assert(!"fragment shader outputs must be POSITION/STENCIL/COLOR");
6399 ret
= PIPE_ERROR_BAD_INPUT
;
6404 else if (procType
== PIPE_SHADER_VERTEX
) {
6405 for (i
= 0; i
< numOutputs
; i
++) {
6406 if (outputSemanticName
[i
] == TGSI_SEMANTIC_FOG
) {
6407 /* force register to contain a fog coordinate in the form (F, 0, 0, 1). */
6409 ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_YZW
),
6410 ureg_imm4f(ureg
, 0.0f
, 0.0f
, 0.0f
, 1.0f
));
6411 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_X
);
6416 if (procType
== PIPE_SHADER_COMPUTE
) {
6417 emit_compute_block_size(proginfo
, ureg
);
6420 /* Declare address register.
6422 if (program
->num_address_regs
> 0) {
6423 assert(program
->num_address_regs
<= 3);
6424 for (int i
= 0; i
< program
->num_address_regs
; i
++)
6425 t
->address
[i
] = ureg_DECL_address(ureg
);
6428 /* Declare misc input registers
6431 GLbitfield sysInputs
= proginfo
->info
.system_values_read
;
6433 for (i
= 0; sysInputs
; i
++) {
6434 if (sysInputs
& (1 << i
)) {
6435 unsigned semName
= _mesa_sysval_to_semantic(i
);
6437 t
->systemValues
[i
] = ureg_DECL_system_value(ureg
, semName
, 0);
6439 if (semName
== TGSI_SEMANTIC_INSTANCEID
||
6440 semName
== TGSI_SEMANTIC_VERTEXID
) {
6441 /* From Gallium perspective, these system values are always
6442 * integer, and require native integer support. However, if
6443 * native integer is supported on the vertex stage but not the
6444 * pixel stage (e.g, i915g + draw), Mesa will generate IR that
6445 * assumes these system values are floats. To resolve the
6446 * inconsistency, we insert a U2F.
6448 struct st_context
*st
= st_context(ctx
);
6449 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
6450 assert(procType
== PIPE_SHADER_VERTEX
);
6451 assert(pscreen
->get_shader_param(pscreen
, PIPE_SHADER_VERTEX
, PIPE_SHADER_CAP_INTEGERS
));
6453 if (!ctx
->Const
.NativeIntegers
) {
6454 struct ureg_dst temp
= ureg_DECL_local_temporary(t
->ureg
);
6455 ureg_U2F( t
->ureg
, ureg_writemask(temp
, TGSI_WRITEMASK_X
), t
->systemValues
[i
]);
6456 t
->systemValues
[i
] = ureg_scalar(ureg_src(temp
), 0);
6460 if (procType
== PIPE_SHADER_FRAGMENT
&&
6461 semName
== TGSI_SEMANTIC_POSITION
)
6462 emit_wpos(st_context(ctx
), t
, proginfo
, ureg
,
6463 program
->wpos_transform_const
);
6465 sysInputs
&= ~(1 << i
);
6470 t
->array_sizes
= program
->array_sizes
;
6471 t
->input_decls
= program
->inputs
;
6472 t
->num_input_decls
= program
->num_inputs
;
6473 t
->output_decls
= program
->outputs
;
6474 t
->num_output_decls
= program
->num_outputs
;
6476 /* Emit constants and uniforms. TGSI uses a single index space for these,
6477 * so we put all the translated regs in t->constants.
6479 if (proginfo
->Parameters
) {
6480 t
->constants
= (struct ureg_src
*)
6481 calloc(proginfo
->Parameters
->NumParameters
, sizeof(t
->constants
[0]));
6482 if (t
->constants
== NULL
) {
6483 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
6486 t
->num_constants
= proginfo
->Parameters
->NumParameters
;
6488 for (i
= 0; i
< proginfo
->Parameters
->NumParameters
; i
++) {
6489 switch (proginfo
->Parameters
->Parameters
[i
].Type
) {
6490 case PROGRAM_STATE_VAR
:
6491 case PROGRAM_UNIFORM
:
6492 t
->constants
[i
] = ureg_DECL_constant(ureg
, i
);
6495 /* Emit immediates for PROGRAM_CONSTANT only when there's no indirect
6496 * addressing of the const buffer.
6497 * FIXME: Be smarter and recognize param arrays:
6498 * indirect addressing is only valid within the referenced
6501 case PROGRAM_CONSTANT
:
6502 if (program
->indirect_addr_consts
)
6503 t
->constants
[i
] = ureg_DECL_constant(ureg
, i
);
6505 t
->constants
[i
] = emit_immediate(t
,
6506 proginfo
->Parameters
->ParameterValues
[i
],
6507 proginfo
->Parameters
->Parameters
[i
].DataType
,
6516 for (i
= 0; i
< proginfo
->info
.num_ubos
; i
++) {
6517 unsigned size
= proginfo
->sh
.UniformBlocks
[i
]->UniformBufferSize
;
6518 unsigned num_const_vecs
= (size
+ 15) / 16;
6519 unsigned first
, last
;
6520 assert(num_const_vecs
> 0);
6522 last
= num_const_vecs
> 0 ? num_const_vecs
- 1 : 0;
6523 ureg_DECL_constant2D(t
->ureg
, first
, last
, i
+ 1);
6526 /* Emit immediate values.
6528 t
->immediates
= (struct ureg_src
*)
6529 calloc(program
->num_immediates
, sizeof(struct ureg_src
));
6530 if (t
->immediates
== NULL
) {
6531 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
6534 t
->num_immediates
= program
->num_immediates
;
6537 foreach_in_list(immediate_storage
, imm
, &program
->immediates
) {
6538 assert(i
< program
->num_immediates
);
6539 t
->immediates
[i
++] = emit_immediate(t
, imm
->values
, imm
->type
, imm
->size32
);
6541 assert(i
== program
->num_immediates
);
6543 /* texture samplers */
6544 for (i
= 0; i
< frag_const
->MaxTextureImageUnits
; i
++) {
6545 if (program
->samplers_used
& (1u << i
)) {
6548 t
->samplers
[i
] = ureg_DECL_sampler(ureg
, i
);
6550 switch (program
->sampler_types
[i
]) {
6552 type
= TGSI_RETURN_TYPE_SINT
;
6554 case GLSL_TYPE_UINT
:
6555 type
= TGSI_RETURN_TYPE_UINT
;
6557 case GLSL_TYPE_FLOAT
:
6558 type
= TGSI_RETURN_TYPE_FLOAT
;
6561 unreachable("not reached");
6564 ureg_DECL_sampler_view( ureg
, i
, program
->sampler_targets
[i
],
6565 type
, type
, type
, type
);
6569 for (i
= 0; i
< frag_const
->MaxAtomicBuffers
; i
++) {
6570 if (program
->buffers_used
& (1 << i
)) {
6571 t
->buffers
[i
] = ureg_DECL_buffer(ureg
, i
, true);
6575 for (; i
< frag_const
->MaxAtomicBuffers
+ frag_const
->MaxShaderStorageBlocks
;
6577 if (program
->buffers_used
& (1 << i
)) {
6578 t
->buffers
[i
] = ureg_DECL_buffer(ureg
, i
, false);
6582 if (program
->use_shared_memory
)
6583 t
->shared_memory
= ureg_DECL_memory(ureg
, TGSI_MEMORY_TYPE_SHARED
);
6585 for (i
= 0; i
< program
->shader
->Program
->info
.num_images
; i
++) {
6586 if (program
->images_used
& (1 << i
)) {
6587 t
->images
[i
] = ureg_DECL_image(ureg
, i
,
6588 program
->image_targets
[i
],
6589 program
->image_formats
[i
],
6594 /* Emit each instruction in turn:
6596 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &program
->instructions
)
6597 compile_tgsi_instruction(t
, inst
);
6599 /* Set the next shader stage hint for VS and TES. */
6601 case PIPE_SHADER_VERTEX
:
6602 case PIPE_SHADER_TESS_EVAL
:
6603 if (program
->shader_program
->SeparateShader
)
6606 for (i
= program
->shader
->Stage
+1; i
<= MESA_SHADER_FRAGMENT
; i
++) {
6607 if (program
->shader_program
->_LinkedShaders
[i
]) {
6611 case MESA_SHADER_TESS_CTRL
:
6612 next
= PIPE_SHADER_TESS_CTRL
;
6614 case MESA_SHADER_TESS_EVAL
:
6615 next
= PIPE_SHADER_TESS_EVAL
;
6617 case MESA_SHADER_GEOMETRY
:
6618 next
= PIPE_SHADER_GEOMETRY
;
6620 case MESA_SHADER_FRAGMENT
:
6621 next
= PIPE_SHADER_FRAGMENT
;
6628 ureg_set_next_shader_processor(ureg
, next
);
6640 t
->num_constants
= 0;
6641 free(t
->immediates
);
6642 t
->num_immediates
= 0;
6648 /* ----------------------------- End TGSI code ------------------------------ */
6652 * Convert a shader's GLSL IR into a Mesa gl_program, although without
6653 * generating Mesa IR.
6655 static struct gl_program
*
6656 get_mesa_program_tgsi(struct gl_context
*ctx
,
6657 struct gl_shader_program
*shader_program
,
6658 struct gl_linked_shader
*shader
)
6660 glsl_to_tgsi_visitor
* v
;
6661 struct gl_program
*prog
;
6662 struct gl_shader_compiler_options
*options
=
6663 &ctx
->Const
.ShaderCompilerOptions
[shader
->Stage
];
6664 struct pipe_screen
*pscreen
= ctx
->st
->pipe
->screen
;
6665 enum pipe_shader_type ptarget
= st_shader_stage_to_ptarget(shader
->Stage
);
6666 unsigned skip_merge_registers
;
6668 validate_ir_tree(shader
->ir
);
6670 prog
= shader
->Program
;
6672 prog
->Parameters
= _mesa_new_parameter_list();
6673 v
= new glsl_to_tgsi_visitor();
6676 v
->shader_program
= shader_program
;
6678 v
->options
= options
;
6679 v
->glsl_version
= ctx
->Const
.GLSLVersion
;
6680 v
->native_integers
= ctx
->Const
.NativeIntegers
;
6682 v
->have_sqrt
= pscreen
->get_shader_param(pscreen
, ptarget
,
6683 PIPE_SHADER_CAP_TGSI_SQRT_SUPPORTED
);
6684 v
->have_fma
= pscreen
->get_shader_param(pscreen
, ptarget
,
6685 PIPE_SHADER_CAP_TGSI_FMA_SUPPORTED
);
6686 v
->has_tex_txf_lz
= pscreen
->get_param(pscreen
,
6687 PIPE_CAP_TGSI_TEX_TXF_LZ
);
6688 skip_merge_registers
=
6689 pscreen
->get_shader_param(pscreen
, ptarget
,
6690 PIPE_SHADER_CAP_TGSI_SKIP_MERGE_REGISTERS
);
6692 _mesa_generate_parameters_list_for_uniforms(shader_program
, shader
,
6695 /* Remove reads from output registers. */
6696 if (!pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_CAN_READ_OUTPUTS
))
6697 lower_output_reads(shader
->Stage
, shader
->ir
);
6699 /* Emit intermediate IR for main(). */
6700 visit_exec_list(shader
->ir
, v
);
6703 /* Print out some information (for debugging purposes) used by the
6704 * optimization passes. */
6707 int *first_writes
= rzalloc_array(v
->mem_ctx
, int, v
->next_temp
);
6708 int *first_reads
= rzalloc_array(v
->mem_ctx
, int, v
->next_temp
);
6709 int *last_writes
= rzalloc_array(v
->mem_ctx
, int, v
->next_temp
);
6710 int *last_reads
= rzalloc_array(v
->mem_ctx
, int, v
->next_temp
);
6712 for (i
= 0; i
< v
->next_temp
; i
++) {
6713 first_writes
[i
] = -1;
6714 first_reads
[i
] = -1;
6715 last_writes
[i
] = -1;
6718 v
->get_first_temp_read(first_reads
);
6719 v
->get_last_temp_read_first_temp_write(last_reads
, first_writes
);
6720 v
->get_last_temp_write(last_writes
);
6721 for (i
= 0; i
< v
->next_temp
; i
++)
6722 printf("Temp %d: FR=%3d FW=%3d LR=%3d LW=%3d\n", i
, first_reads
[i
],
6726 ralloc_free(first_writes
);
6727 ralloc_free(first_reads
);
6728 ralloc_free(last_writes
);
6729 ralloc_free(last_reads
);
6733 /* Perform optimizations on the instructions in the glsl_to_tgsi_visitor. */
6736 if (shader
->Stage
!= MESA_SHADER_TESS_CTRL
&&
6737 shader
->Stage
!= MESA_SHADER_TESS_EVAL
)
6738 v
->copy_propagate();
6740 while (v
->eliminate_dead_code());
6742 v
->merge_two_dsts();
6743 if (!skip_merge_registers
)
6744 v
->merge_registers();
6745 v
->renumber_registers();
6747 /* Write the END instruction. */
6748 v
->emit_asm(NULL
, TGSI_OPCODE_END
);
6750 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
6752 _mesa_log("GLSL IR for linked %s program %d:\n",
6753 _mesa_shader_stage_to_string(shader
->Stage
),
6754 shader_program
->Name
);
6755 _mesa_print_ir(_mesa_get_log_file(), shader
->ir
, NULL
);
6759 do_set_program_inouts(shader
->ir
, prog
, shader
->Stage
);
6760 _mesa_copy_linked_program_data(shader_program
, shader
);
6761 shrink_array_declarations(v
->inputs
, v
->num_inputs
,
6762 &prog
->info
.inputs_read
,
6763 prog
->info
.double_inputs_read
,
6764 &prog
->info
.patch_inputs_read
);
6765 shrink_array_declarations(v
->outputs
, v
->num_outputs
,
6766 &prog
->info
.outputs_written
, 0ULL,
6767 &prog
->info
.patch_outputs_written
);
6768 count_resources(v
, prog
);
6770 /* The GLSL IR won't be needed anymore. */
6771 ralloc_free(shader
->ir
);
6774 /* This must be done before the uniform storage is associated. */
6775 if (shader
->Stage
== MESA_SHADER_FRAGMENT
&&
6776 (prog
->info
.inputs_read
& VARYING_BIT_POS
||
6777 prog
->info
.system_values_read
& (1 << SYSTEM_VALUE_FRAG_COORD
))) {
6778 static const gl_state_index wposTransformState
[STATE_LENGTH
] = {
6779 STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
6782 v
->wpos_transform_const
= _mesa_add_state_reference(prog
->Parameters
,
6783 wposTransformState
);
6786 /* Avoid reallocation of the program parameter list, because the uniform
6787 * storage is only associated with the original parameter list.
6788 * This should be enough for Bitmap and DrawPixels constants.
6790 _mesa_reserve_parameter_storage(prog
->Parameters
, 8);
6792 /* This has to be done last. Any operation the can cause
6793 * prog->ParameterValues to get reallocated (e.g., anything that adds a
6794 * program constant) has to happen before creating this linkage.
6796 _mesa_associate_uniform_storage(ctx
, shader_program
, prog
->Parameters
,
6798 if (!shader_program
->data
->LinkStatus
) {
6799 free_glsl_to_tgsi_visitor(v
);
6800 _mesa_reference_program(ctx
, &shader
->Program
, NULL
);
6804 struct st_vertex_program
*stvp
;
6805 struct st_fragment_program
*stfp
;
6806 struct st_geometry_program
*stgp
;
6807 struct st_tessctrl_program
*sttcp
;
6808 struct st_tesseval_program
*sttep
;
6809 struct st_compute_program
*stcp
;
6811 switch (shader
->Stage
) {
6812 case MESA_SHADER_VERTEX
:
6813 stvp
= (struct st_vertex_program
*)prog
;
6814 stvp
->glsl_to_tgsi
= v
;
6816 case MESA_SHADER_FRAGMENT
:
6817 stfp
= (struct st_fragment_program
*)prog
;
6818 stfp
->glsl_to_tgsi
= v
;
6820 case MESA_SHADER_GEOMETRY
:
6821 stgp
= (struct st_geometry_program
*)prog
;
6822 stgp
->glsl_to_tgsi
= v
;
6824 case MESA_SHADER_TESS_CTRL
:
6825 sttcp
= (struct st_tessctrl_program
*)prog
;
6826 sttcp
->glsl_to_tgsi
= v
;
6828 case MESA_SHADER_TESS_EVAL
:
6829 sttep
= (struct st_tesseval_program
*)prog
;
6830 sttep
->glsl_to_tgsi
= v
;
6832 case MESA_SHADER_COMPUTE
:
6833 stcp
= (struct st_compute_program
*)prog
;
6834 stcp
->glsl_to_tgsi
= v
;
6837 assert(!"should not be reached");
6844 /* See if there are unsupported control flow statements. */
6845 class ir_control_flow_info_visitor
: public ir_hierarchical_visitor
{
6847 const struct gl_shader_compiler_options
*options
;
6849 ir_control_flow_info_visitor(const struct gl_shader_compiler_options
*options
)
6855 virtual ir_visitor_status
visit_enter(ir_function
*ir
)
6857 /* Other functions are skipped (same as glsl_to_tgsi). */
6858 if (strcmp(ir
->name
, "main") == 0)
6859 return visit_continue
;
6861 return visit_continue_with_parent
;
6864 virtual ir_visitor_status
visit_enter(ir_call
*ir
)
6866 if (!ir
->callee
->is_intrinsic()) {
6867 unsupported
= true; /* it's a function call */
6870 return visit_continue
;
6873 virtual ir_visitor_status
visit_enter(ir_return
*ir
)
6875 if (options
->EmitNoMainReturn
) {
6879 return visit_continue
;
6886 has_unsupported_control_flow(exec_list
*ir
,
6887 const struct gl_shader_compiler_options
*options
)
6889 ir_control_flow_info_visitor
visitor(options
);
6890 visit_list_elements(&visitor
, ir
);
6891 return visitor
.unsupported
;
6898 * Called via ctx->Driver.LinkShader()
6899 * This actually involves converting GLSL IR into an intermediate TGSI-like IR
6900 * with code lowering and other optimizations.
6903 st_link_shader(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
6905 /* Return early if we are loading the shader from on-disk cache */
6906 if (st_load_tgsi_from_disk_cache(ctx
, prog
)) {
6910 struct pipe_screen
*pscreen
= ctx
->st
->pipe
->screen
;
6911 assert(prog
->data
->LinkStatus
);
6913 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
6914 if (prog
->_LinkedShaders
[i
] == NULL
)
6917 struct gl_linked_shader
*shader
= prog
->_LinkedShaders
[i
];
6918 exec_list
*ir
= shader
->ir
;
6919 gl_shader_stage stage
= shader
->Stage
;
6920 const struct gl_shader_compiler_options
*options
=
6921 &ctx
->Const
.ShaderCompilerOptions
[stage
];
6922 enum pipe_shader_type ptarget
= st_shader_stage_to_ptarget(stage
);
6923 bool have_dround
= pscreen
->get_shader_param(pscreen
, ptarget
,
6924 PIPE_SHADER_CAP_TGSI_DROUND_SUPPORTED
);
6925 bool have_dfrexp
= pscreen
->get_shader_param(pscreen
, ptarget
,
6926 PIPE_SHADER_CAP_TGSI_DFRACEXP_DLDEXP_SUPPORTED
);
6927 unsigned if_threshold
= pscreen
->get_shader_param(pscreen
, ptarget
,
6928 PIPE_SHADER_CAP_LOWER_IF_THRESHOLD
);
6930 /* If there are forms of indirect addressing that the driver
6931 * cannot handle, perform the lowering pass.
6933 if (options
->EmitNoIndirectInput
|| options
->EmitNoIndirectOutput
||
6934 options
->EmitNoIndirectTemp
|| options
->EmitNoIndirectUniform
) {
6935 lower_variable_index_to_cond_assign(stage
, ir
,
6936 options
->EmitNoIndirectInput
,
6937 options
->EmitNoIndirectOutput
,
6938 options
->EmitNoIndirectTemp
,
6939 options
->EmitNoIndirectUniform
);
6942 if (!pscreen
->get_param(pscreen
, PIPE_CAP_INT64_DIVMOD
))
6943 lower_64bit_integer_instructions(ir
, DIV64
| MOD64
);
6945 if (ctx
->Extensions
.ARB_shading_language_packing
) {
6946 unsigned lower_inst
= LOWER_PACK_SNORM_2x16
|
6947 LOWER_UNPACK_SNORM_2x16
|
6948 LOWER_PACK_UNORM_2x16
|
6949 LOWER_UNPACK_UNORM_2x16
|
6950 LOWER_PACK_SNORM_4x8
|
6951 LOWER_UNPACK_SNORM_4x8
|
6952 LOWER_UNPACK_UNORM_4x8
|
6953 LOWER_PACK_UNORM_4x8
;
6955 if (ctx
->Extensions
.ARB_gpu_shader5
)
6956 lower_inst
|= LOWER_PACK_USE_BFI
|
6958 if (!ctx
->st
->has_half_float_packing
)
6959 lower_inst
|= LOWER_PACK_HALF_2x16
|
6960 LOWER_UNPACK_HALF_2x16
;
6962 lower_packing_builtins(ir
, lower_inst
);
6965 if (!pscreen
->get_param(pscreen
, PIPE_CAP_TEXTURE_GATHER_OFFSETS
))
6966 lower_offset_arrays(ir
);
6967 do_mat_op_to_vec(ir
);
6969 if (stage
== MESA_SHADER_FRAGMENT
)
6970 lower_blend_equation_advanced(shader
);
6972 lower_instructions(ir
,
6978 (have_dfrexp
? 0 : DFREXP_DLDEXP_TO_ARITH
) |
6981 (have_dround
? 0 : DOPS_TO_DFRAC
) |
6982 (options
->EmitNoPow
? POW_TO_EXP2
: 0) |
6983 (!ctx
->Const
.NativeIntegers
? INT_DIV_TO_MUL_RCP
: 0) |
6984 (options
->EmitNoSat
? SAT_TO_CLAMP
: 0) |
6985 (ctx
->Const
.ForceGLSLAbsSqrt
? SQRT_TO_ABS_SQRT
: 0) |
6986 /* Assume that if ARB_gpu_shader5 is not supported
6987 * then all of the extended integer functions need
6988 * lowering. It may be necessary to add some caps
6989 * for individual instructions.
6991 (!ctx
->Extensions
.ARB_gpu_shader5
6992 ? BIT_COUNT_TO_MATH
|
6996 FIND_LSB_TO_FLOAT_CAST
|
6997 FIND_MSB_TO_FLOAT_CAST
|
7001 do_vec_index_to_cond_assign(ir
);
7002 lower_vector_insert(ir
, true);
7003 lower_quadop_vector(ir
, false);
7005 if (options
->MaxIfDepth
== 0) {
7009 if (ctx
->Const
.GLSLOptimizeConservatively
) {
7010 /* Do it once and repeat only if there's unsupported control flow. */
7012 do_common_optimization(ir
, true, true, options
,
7013 ctx
->Const
.NativeIntegers
);
7014 lower_if_to_cond_assign((gl_shader_stage
)i
, ir
,
7015 options
->MaxIfDepth
, if_threshold
);
7016 } while (has_unsupported_control_flow(ir
, options
));
7018 /* Repeat it until it stops making changes. */
7021 progress
= do_common_optimization(ir
, true, true, options
,
7022 ctx
->Const
.NativeIntegers
);
7023 progress
|= lower_if_to_cond_assign((gl_shader_stage
)i
, ir
,
7024 options
->MaxIfDepth
, if_threshold
);
7028 validate_ir_tree(ir
);
7031 build_program_resource_list(ctx
, prog
);
7033 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
7034 struct gl_linked_shader
*shader
= prog
->_LinkedShaders
[i
];
7038 enum pipe_shader_type ptarget
=
7039 st_shader_stage_to_ptarget(shader
->Stage
);
7040 enum pipe_shader_ir preferred_ir
= (enum pipe_shader_ir
)
7041 pscreen
->get_shader_param(pscreen
, ptarget
,
7042 PIPE_SHADER_CAP_PREFERRED_IR
);
7044 struct gl_program
*linked_prog
= NULL
;
7045 if (preferred_ir
== PIPE_SHADER_IR_NIR
) {
7046 /* TODO only for GLSL VS/FS for now: */
7047 switch (shader
->Stage
) {
7048 case MESA_SHADER_VERTEX
:
7049 case MESA_SHADER_FRAGMENT
:
7050 linked_prog
= st_nir_get_mesa_program(ctx
, prog
, shader
);
7055 linked_prog
= get_mesa_program_tgsi(ctx
, prog
, shader
);
7059 st_set_prog_affected_state_flags(linked_prog
);
7060 if (!ctx
->Driver
.ProgramStringNotify(ctx
,
7061 _mesa_shader_stage_to_program(i
),
7063 _mesa_reference_program(ctx
, &shader
->Program
, NULL
);
7073 st_translate_stream_output_info(glsl_to_tgsi_visitor
*glsl_to_tgsi
,
7074 const GLuint outputMapping
[],
7075 struct pipe_stream_output_info
*so
)
7077 if (!glsl_to_tgsi
->shader_program
->last_vert_prog
)
7080 struct gl_transform_feedback_info
*info
=
7081 glsl_to_tgsi
->shader_program
->last_vert_prog
->sh
.LinkedTransformFeedback
;
7082 st_translate_stream_output_info2(info
, outputMapping
, so
);
7086 st_translate_stream_output_info2(struct gl_transform_feedback_info
*info
,
7087 const GLuint outputMapping
[],
7088 struct pipe_stream_output_info
*so
)
7092 for (i
= 0; i
< info
->NumOutputs
; i
++) {
7093 so
->output
[i
].register_index
=
7094 outputMapping
[info
->Outputs
[i
].OutputRegister
];
7095 so
->output
[i
].start_component
= info
->Outputs
[i
].ComponentOffset
;
7096 so
->output
[i
].num_components
= info
->Outputs
[i
].NumComponents
;
7097 so
->output
[i
].output_buffer
= info
->Outputs
[i
].OutputBuffer
;
7098 so
->output
[i
].dst_offset
= info
->Outputs
[i
].DstOffset
;
7099 so
->output
[i
].stream
= info
->Outputs
[i
].StreamId
;
7102 for (i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
7103 so
->stride
[i
] = info
->Buffers
[i
].Stride
;
7105 so
->num_outputs
= info
->NumOutputs
;