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_glsl_types.h"
53 #include "st_program.h"
54 #include "st_mesa_to_tgsi.h"
55 #include "st_format.h"
56 #include "st_glsl_to_tgsi_temprename.h"
58 #include "util/hash_table.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
68 #include "util/u_atomic.h"
69 #include "util/simple_mtx.h"
73 /* Prepare to make it possible to specify log file */
74 static std::ofstream stats_log
;
76 /* Helper function to check whether we want to write some statistics
77 * of the shader conversion.
80 static simple_mtx_t print_stats_mutex
= _SIMPLE_MTX_INITIALIZER_NP
;
82 static inline bool print_stats_enabled ()
84 static int stats_enabled
= 0;
87 simple_mtx_lock(&print_stats_mutex
);
89 const char *stats_filename
= getenv("GLSL_TO_TGSI_PRINT_STATS");
91 bool write_header
= std::ifstream(stats_filename
).fail();
92 stats_log
.open(stats_filename
, std::ios_base::out
| std::ios_base::app
);
93 stats_enabled
= stats_log
.good() ? 1 : -1;
95 stats_log
<< "arrays,temps,temps in arrays,total,instructions\n";
100 simple_mtx_unlock(&print_stats_mutex
);
102 return stats_enabled
> 0;
104 #define PRINT_STATS(X) if (print_stats_enabled()) do { X; } while (false);
106 #define PRINT_STATS(X)
110 static unsigned is_precise(const ir_variable
*ir
)
114 return ir
->data
.precise
|| ir
->data
.invariant
;
117 class variable_storage
{
118 DECLARE_RZALLOC_CXX_OPERATORS(variable_storage
)
121 variable_storage(ir_variable
*var
, gl_register_file file
, int index
,
122 unsigned array_id
= 0)
123 : file(file
), index(index
), component(0), var(var
), array_id(array_id
)
125 assert(file
!= PROGRAM_ARRAY
|| array_id
!= 0);
128 gl_register_file file
;
131 /* Explicit component location. This is given in terms of the GLSL-style
132 * swizzles where each double is a single component, i.e. for 64-bit types
133 * it can only be 0 or 1.
136 ir_variable
*var
; /* variable that maps to this, if any */
140 class immediate_storage
: public exec_node
{
142 immediate_storage(gl_constant_value
*values
, int size32
, GLenum type
)
144 memcpy(this->values
, values
, size32
* sizeof(gl_constant_value
));
145 this->size32
= size32
;
149 /* doubles are stored across 2 gl_constant_values */
150 gl_constant_value values
[4];
151 int size32
; /**< Number of 32-bit components (1-4) */
152 GLenum type
; /**< GL_DOUBLE, GL_FLOAT, GL_INT, GL_BOOL, or GL_UNSIGNED_INT */
155 static const st_src_reg undef_src
= st_src_reg(PROGRAM_UNDEFINED
, 0, GLSL_TYPE_ERROR
);
156 static const st_dst_reg undef_dst
= st_dst_reg(PROGRAM_UNDEFINED
, SWIZZLE_NOOP
, GLSL_TYPE_ERROR
);
160 unsigned array_id
; /* TGSI ArrayID; 1-based: 0 means not an array */
163 unsigned gs_out_streams
;
164 enum glsl_interp_mode interp
;
165 enum glsl_base_type base_type
;
166 ubyte usage_mask
; /* GLSL-style usage-mask, i.e. single bit per double */
170 static struct inout_decl
*
171 find_inout_array(struct inout_decl
*decls
, unsigned count
, unsigned array_id
)
173 assert(array_id
!= 0);
175 for (unsigned i
= 0; i
< count
; i
++) {
176 struct inout_decl
*decl
= &decls
[i
];
178 if (array_id
== decl
->array_id
) {
186 static enum glsl_base_type
187 find_array_type(struct inout_decl
*decls
, unsigned count
, unsigned array_id
)
190 return GLSL_TYPE_ERROR
;
191 struct inout_decl
*decl
= find_inout_array(decls
, count
, array_id
);
193 return decl
->base_type
;
194 return GLSL_TYPE_ERROR
;
197 struct hwatomic_decl
{
204 struct glsl_to_tgsi_visitor
: public ir_visitor
{
206 glsl_to_tgsi_visitor();
207 ~glsl_to_tgsi_visitor();
209 struct gl_context
*ctx
;
210 struct gl_program
*prog
;
211 struct gl_shader_program
*shader_program
;
212 struct gl_linked_shader
*shader
;
213 struct gl_shader_compiler_options
*options
;
217 unsigned *array_sizes
;
218 unsigned max_num_arrays
;
221 struct inout_decl inputs
[4 * PIPE_MAX_SHADER_INPUTS
];
223 unsigned num_input_arrays
;
224 struct inout_decl outputs
[4 * PIPE_MAX_SHADER_OUTPUTS
];
225 unsigned num_outputs
;
226 unsigned num_output_arrays
;
228 struct hwatomic_decl atomic_info
[PIPE_MAX_HW_ATOMIC_BUFFERS
];
229 unsigned num_atomics
;
230 unsigned num_atomic_arrays
;
231 int num_address_regs
;
232 uint32_t samplers_used
;
233 glsl_base_type sampler_types
[PIPE_MAX_SAMPLERS
];
234 enum tgsi_texture_type sampler_targets
[PIPE_MAX_SAMPLERS
];
236 enum tgsi_texture_type image_targets
[PIPE_MAX_SHADER_IMAGES
];
237 enum pipe_format image_formats
[PIPE_MAX_SHADER_IMAGES
];
238 bool image_wr
[PIPE_MAX_SHADER_IMAGES
];
239 bool indirect_addr_consts
;
240 int wpos_transform_const
;
242 bool native_integers
;
245 bool use_shared_memory
;
250 variable_storage
*find_variable_storage(ir_variable
*var
);
252 int add_constant(gl_register_file file
, gl_constant_value values
[8],
253 int size
, GLenum datatype
, uint16_t *swizzle_out
);
255 st_src_reg
get_temp(const glsl_type
*type
);
256 void reladdr_to_temp(ir_instruction
*ir
, st_src_reg
*reg
, int *num_reladdr
);
258 st_src_reg
st_src_reg_for_double(double val
);
259 st_src_reg
st_src_reg_for_float(float val
);
260 st_src_reg
st_src_reg_for_int(int val
);
261 st_src_reg
st_src_reg_for_int64(int64_t val
);
262 st_src_reg
st_src_reg_for_type(enum glsl_base_type type
, int val
);
265 * \name Visit methods
267 * As typical for the visitor pattern, there must be one \c visit method for
268 * each concrete subclass of \c ir_instruction. Virtual base classes within
269 * the hierarchy should not have \c visit methods.
272 virtual void visit(ir_variable
*);
273 virtual void visit(ir_loop
*);
274 virtual void visit(ir_loop_jump
*);
275 virtual void visit(ir_function_signature
*);
276 virtual void visit(ir_function
*);
277 virtual void visit(ir_expression
*);
278 virtual void visit(ir_swizzle
*);
279 virtual void visit(ir_dereference_variable
*);
280 virtual void visit(ir_dereference_array
*);
281 virtual void visit(ir_dereference_record
*);
282 virtual void visit(ir_assignment
*);
283 virtual void visit(ir_constant
*);
284 virtual void visit(ir_call
*);
285 virtual void visit(ir_return
*);
286 virtual void visit(ir_discard
*);
287 virtual void visit(ir_texture
*);
288 virtual void visit(ir_if
*);
289 virtual void visit(ir_emit_vertex
*);
290 virtual void visit(ir_end_primitive
*);
291 virtual void visit(ir_barrier
*);
294 void visit_expression(ir_expression
*, st_src_reg
*) ATTRIBUTE_NOINLINE
;
296 void visit_atomic_counter_intrinsic(ir_call
*);
297 void visit_ssbo_intrinsic(ir_call
*);
298 void visit_membar_intrinsic(ir_call
*);
299 void visit_shared_intrinsic(ir_call
*);
300 void visit_image_intrinsic(ir_call
*);
301 void visit_generic_intrinsic(ir_call
*, enum tgsi_opcode op
);
305 /** List of variable_storage */
306 struct hash_table
*variables
;
308 /** List of immediate_storage */
309 exec_list immediates
;
310 unsigned num_immediates
;
312 /** List of glsl_to_tgsi_instruction */
313 exec_list instructions
;
315 glsl_to_tgsi_instruction
*emit_asm(ir_instruction
*ir
, enum tgsi_opcode op
,
316 st_dst_reg dst
= undef_dst
,
317 st_src_reg src0
= undef_src
,
318 st_src_reg src1
= undef_src
,
319 st_src_reg src2
= undef_src
,
320 st_src_reg src3
= undef_src
);
322 glsl_to_tgsi_instruction
*emit_asm(ir_instruction
*ir
, enum tgsi_opcode op
,
323 st_dst_reg dst
, st_dst_reg dst1
,
324 st_src_reg src0
= undef_src
,
325 st_src_reg src1
= undef_src
,
326 st_src_reg src2
= undef_src
,
327 st_src_reg src3
= undef_src
);
329 enum tgsi_opcode
get_opcode(enum tgsi_opcode op
,
331 st_src_reg src0
, st_src_reg src1
);
334 * Emit the correct dot-product instruction for the type of arguments
336 glsl_to_tgsi_instruction
*emit_dp(ir_instruction
*ir
,
342 void emit_scalar(ir_instruction
*ir
, enum tgsi_opcode op
,
343 st_dst_reg dst
, st_src_reg src0
);
345 void emit_scalar(ir_instruction
*ir
, enum tgsi_opcode op
,
346 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
);
348 void emit_arl(ir_instruction
*ir
, st_dst_reg dst
, st_src_reg src0
);
350 void get_deref_offsets(ir_dereference
*ir
,
351 unsigned *array_size
,
356 void calc_deref_offsets(ir_dereference
*tail
,
357 unsigned *array_elements
,
359 st_src_reg
*indirect
,
361 st_src_reg
canonicalize_gather_offset(st_src_reg offset
);
362 bool handle_bound_deref(ir_dereference
*ir
);
364 bool try_emit_mad(ir_expression
*ir
,
366 bool try_emit_mad_for_and_not(ir_expression
*ir
,
369 void emit_swz(ir_expression
*ir
);
371 bool process_move_condition(ir_rvalue
*ir
);
373 void simplify_cmp(void);
375 void rename_temp_registers(struct rename_reg_pair
*renames
);
376 void get_first_temp_read(int *first_reads
);
377 void get_first_temp_write(int *first_writes
);
378 void get_last_temp_read_first_temp_write(int *last_reads
, int *first_writes
);
379 void get_last_temp_write(int *last_writes
);
381 void copy_propagate(void);
382 int eliminate_dead_code(void);
384 void split_arrays(void);
385 void merge_two_dsts(void);
386 void merge_registers(void);
387 void renumber_registers(void);
389 void emit_block_mov(ir_assignment
*ir
, const struct glsl_type
*type
,
390 st_dst_reg
*l
, st_src_reg
*r
,
391 st_src_reg
*cond
, bool cond_swap
);
398 static st_dst_reg address_reg
= st_dst_reg(PROGRAM_ADDRESS
, WRITEMASK_X
,
400 static st_dst_reg address_reg2
= st_dst_reg(PROGRAM_ADDRESS
, WRITEMASK_X
,
402 static st_dst_reg sampler_reladdr
= st_dst_reg(PROGRAM_ADDRESS
, WRITEMASK_X
,
406 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...)
410 fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...)
414 ralloc_vasprintf_append(&prog
->data
->InfoLog
, fmt
, args
);
417 prog
->data
->LinkStatus
= LINKING_FAILURE
;
421 swizzle_for_size(int size
)
423 static const int size_swizzles
[4] = {
424 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
),
425 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
),
426 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
),
427 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
430 assert((size
>= 1) && (size
<= 4));
431 return size_swizzles
[size
- 1];
435 glsl_to_tgsi_instruction
*
436 glsl_to_tgsi_visitor::emit_asm(ir_instruction
*ir
, enum tgsi_opcode op
,
437 st_dst_reg dst
, st_dst_reg dst1
,
438 st_src_reg src0
, st_src_reg src1
,
439 st_src_reg src2
, st_src_reg src3
)
441 glsl_to_tgsi_instruction
*inst
= new(mem_ctx
) glsl_to_tgsi_instruction();
442 int num_reladdr
= 0, i
, j
;
443 bool dst_is_64bit
[2];
445 op
= get_opcode(op
, dst
, src0
, src1
);
447 /* If we have to do relative addressing, we want to load the ARL
448 * reg directly for one of the regs, and preload the other reladdr
449 * sources into temps.
451 num_reladdr
+= dst
.reladdr
!= NULL
|| dst
.reladdr2
;
452 assert(!dst1
.reladdr
); /* should be lowered in earlier passes */
453 num_reladdr
+= src0
.reladdr
!= NULL
|| src0
.reladdr2
!= NULL
;
454 num_reladdr
+= src1
.reladdr
!= NULL
|| src1
.reladdr2
!= NULL
;
455 num_reladdr
+= src2
.reladdr
!= NULL
|| src2
.reladdr2
!= NULL
;
456 num_reladdr
+= src3
.reladdr
!= NULL
|| src3
.reladdr2
!= NULL
;
458 reladdr_to_temp(ir
, &src3
, &num_reladdr
);
459 reladdr_to_temp(ir
, &src2
, &num_reladdr
);
460 reladdr_to_temp(ir
, &src1
, &num_reladdr
);
461 reladdr_to_temp(ir
, &src0
, &num_reladdr
);
463 if (dst
.reladdr
|| dst
.reladdr2
) {
465 emit_arl(ir
, address_reg
, *dst
.reladdr
);
467 emit_arl(ir
, address_reg2
, *dst
.reladdr2
);
471 assert(num_reladdr
== 0);
473 /* inst->op has only 8 bits. */
474 STATIC_ASSERT(TGSI_OPCODE_LAST
<= 255);
477 inst
->precise
= this->precise
;
478 inst
->info
= tgsi_get_opcode_info(op
);
485 inst
->is_64bit_expanded
= false;
488 inst
->tex_offsets
= NULL
;
489 inst
->tex_offset_num_offset
= 0;
491 inst
->tex_shadow
= 0;
492 /* default to float, for paths where this is not initialized
493 * (since 0==UINT which is likely wrong):
495 inst
->tex_type
= GLSL_TYPE_FLOAT
;
497 /* Update indirect addressing status used by TGSI */
498 if (dst
.reladdr
|| dst
.reladdr2
) {
500 case PROGRAM_STATE_VAR
:
501 case PROGRAM_CONSTANT
:
502 case PROGRAM_UNIFORM
:
503 this->indirect_addr_consts
= true;
505 case PROGRAM_IMMEDIATE
:
506 assert(!"immediates should not have indirect addressing");
513 for (i
= 0; i
< 4; i
++) {
514 if (inst
->src
[i
].reladdr
) {
515 switch (inst
->src
[i
].file
) {
516 case PROGRAM_STATE_VAR
:
517 case PROGRAM_CONSTANT
:
518 case PROGRAM_UNIFORM
:
519 this->indirect_addr_consts
= true;
521 case PROGRAM_IMMEDIATE
:
522 assert(!"immediates should not have indirect addressing");
532 * This section contains the double processing.
533 * GLSL just represents doubles as single channel values,
534 * however most HW and TGSI represent doubles as pairs of register channels.
536 * so we have to fixup destination writemask/index and src swizzle/indexes.
537 * dest writemasks need to translate from single channel write mask
538 * to a dual-channel writemask, but also need to modify the index,
539 * if we are touching the Z,W fields in the pre-translated writemask.
541 * src channels have similiar index modifications along with swizzle
542 * changes to we pick the XY, ZW pairs from the correct index.
544 * GLSL [0].x -> TGSI [0].xy
545 * GLSL [0].y -> TGSI [0].zw
546 * GLSL [0].z -> TGSI [1].xy
547 * GLSL [0].w -> TGSI [1].zw
549 for (j
= 0; j
< 2; j
++) {
550 dst_is_64bit
[j
] = glsl_base_type_is_64bit(inst
->dst
[j
].type
);
551 if (!dst_is_64bit
[j
] && inst
->dst
[j
].file
== PROGRAM_OUTPUT
&&
552 inst
->dst
[j
].type
== GLSL_TYPE_ARRAY
) {
553 enum glsl_base_type type
= find_array_type(this->outputs
,
555 inst
->dst
[j
].array_id
);
556 if (glsl_base_type_is_64bit(type
))
557 dst_is_64bit
[j
] = true;
561 if (dst_is_64bit
[0] || dst_is_64bit
[1] ||
562 glsl_base_type_is_64bit(inst
->src
[0].type
)) {
563 glsl_to_tgsi_instruction
*dinst
= NULL
;
564 int initial_src_swz
[4], initial_src_idx
[4];
565 int initial_dst_idx
[2], initial_dst_writemask
[2];
566 /* select the writemask for dst0 or dst1 */
567 unsigned writemask
= inst
->dst
[1].file
== PROGRAM_UNDEFINED
568 ? inst
->dst
[0].writemask
: inst
->dst
[1].writemask
;
570 /* copy out the writemask, index and swizzles for all src/dsts. */
571 for (j
= 0; j
< 2; j
++) {
572 initial_dst_writemask
[j
] = inst
->dst
[j
].writemask
;
573 initial_dst_idx
[j
] = inst
->dst
[j
].index
;
576 for (j
= 0; j
< 4; j
++) {
577 initial_src_swz
[j
] = inst
->src
[j
].swizzle
;
578 initial_src_idx
[j
] = inst
->src
[j
].index
;
582 * scan all the components in the dst writemask
583 * generate an instruction for each of them if required.
588 int i
= u_bit_scan(&writemask
);
590 /* before emitting the instruction, see if we have to adjust
591 * load / store address */
592 if (i
> 1 && (inst
->op
== TGSI_OPCODE_LOAD
||
593 inst
->op
== TGSI_OPCODE_STORE
) &&
594 addr
.file
== PROGRAM_UNDEFINED
) {
595 /* We have to advance the buffer address by 16 */
596 addr
= get_temp(glsl_type::uint_type
);
597 emit_asm(ir
, TGSI_OPCODE_UADD
, st_dst_reg(addr
),
598 inst
->src
[0], st_src_reg_for_int(16));
601 /* first time use previous instruction */
605 /* create a new instructions for subsequent attempts */
606 dinst
= new(mem_ctx
) glsl_to_tgsi_instruction();
611 this->instructions
.push_tail(dinst
);
612 dinst
->is_64bit_expanded
= true;
614 /* modify the destination if we are splitting */
615 for (j
= 0; j
< 2; j
++) {
616 if (dst_is_64bit
[j
]) {
617 dinst
->dst
[j
].writemask
= (i
& 1) ? WRITEMASK_ZW
: WRITEMASK_XY
;
618 dinst
->dst
[j
].index
= initial_dst_idx
[j
];
620 if (dinst
->op
== TGSI_OPCODE_LOAD
||
621 dinst
->op
== TGSI_OPCODE_STORE
)
622 dinst
->src
[0] = addr
;
623 if (dinst
->op
!= TGSI_OPCODE_STORE
)
624 dinst
->dst
[j
].index
++;
627 /* if we aren't writing to a double, just get the bit of the
628 * initial writemask for this channel
630 dinst
->dst
[j
].writemask
= initial_dst_writemask
[j
] & (1 << i
);
634 /* modify the src registers */
635 for (j
= 0; j
< 4; j
++) {
636 int swz
= GET_SWZ(initial_src_swz
[j
], i
);
638 if (glsl_base_type_is_64bit(dinst
->src
[j
].type
)) {
639 dinst
->src
[j
].index
= initial_src_idx
[j
];
641 dinst
->src
[j
].double_reg2
= true;
642 dinst
->src
[j
].index
++;
646 dinst
->src
[j
].swizzle
= MAKE_SWIZZLE4(SWIZZLE_Z
, SWIZZLE_W
,
647 SWIZZLE_Z
, SWIZZLE_W
);
649 dinst
->src
[j
].swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
,
650 SWIZZLE_X
, SWIZZLE_Y
);
653 /* some opcodes are special case in what they use as sources
654 * - [FUI]2D/[UI]2I64 is a float/[u]int src0, (D)LDEXP is
657 if (op
== TGSI_OPCODE_F2D
|| op
== TGSI_OPCODE_U2D
||
658 op
== TGSI_OPCODE_I2D
||
659 op
== TGSI_OPCODE_I2I64
|| op
== TGSI_OPCODE_U2I64
||
660 op
== TGSI_OPCODE_DLDEXP
|| op
== TGSI_OPCODE_LDEXP
||
661 (op
== TGSI_OPCODE_UCMP
&& dst_is_64bit
[0])) {
662 dinst
->src
[j
].swizzle
= MAKE_SWIZZLE4(swz
, swz
, swz
, swz
);
669 this->instructions
.push_tail(inst
);
676 glsl_to_tgsi_instruction
*
677 glsl_to_tgsi_visitor::emit_asm(ir_instruction
*ir
, enum tgsi_opcode op
,
679 st_src_reg src0
, st_src_reg src1
,
680 st_src_reg src2
, st_src_reg src3
)
682 return emit_asm(ir
, op
, dst
, undef_dst
, src0
, src1
, src2
, src3
);
686 * Determines whether to use an integer, unsigned integer, or float opcode
687 * based on the operands and input opcode, then emits the result.
690 glsl_to_tgsi_visitor::get_opcode(enum tgsi_opcode op
,
692 st_src_reg src0
, st_src_reg src1
)
694 enum glsl_base_type type
= GLSL_TYPE_FLOAT
;
696 if (op
== TGSI_OPCODE_MOV
)
699 assert(src0
.type
!= GLSL_TYPE_ARRAY
);
700 assert(src0
.type
!= GLSL_TYPE_STRUCT
);
701 assert(src1
.type
!= GLSL_TYPE_ARRAY
);
702 assert(src1
.type
!= GLSL_TYPE_STRUCT
);
704 if (is_resource_instruction(op
))
706 else if (src0
.type
== GLSL_TYPE_INT64
|| src1
.type
== GLSL_TYPE_INT64
)
707 type
= GLSL_TYPE_INT64
;
708 else if (src0
.type
== GLSL_TYPE_UINT64
|| src1
.type
== GLSL_TYPE_UINT64
)
709 type
= GLSL_TYPE_UINT64
;
710 else if (src0
.type
== GLSL_TYPE_DOUBLE
|| src1
.type
== GLSL_TYPE_DOUBLE
)
711 type
= GLSL_TYPE_DOUBLE
;
712 else if (src0
.type
== GLSL_TYPE_FLOAT
|| src1
.type
== GLSL_TYPE_FLOAT
)
713 type
= GLSL_TYPE_FLOAT
;
714 else if (native_integers
)
715 type
= src0
.type
== GLSL_TYPE_BOOL
? GLSL_TYPE_INT
: src0
.type
;
717 #define case7(c, f, i, u, d, i64, ui64) \
718 case TGSI_OPCODE_##c: \
719 if (type == GLSL_TYPE_UINT64) \
720 op = TGSI_OPCODE_##ui64; \
721 else if (type == GLSL_TYPE_INT64) \
722 op = TGSI_OPCODE_##i64; \
723 else if (type == GLSL_TYPE_DOUBLE) \
724 op = TGSI_OPCODE_##d; \
725 else if (type == GLSL_TYPE_INT) \
726 op = TGSI_OPCODE_##i; \
727 else if (type == GLSL_TYPE_UINT) \
728 op = TGSI_OPCODE_##u; \
730 op = TGSI_OPCODE_##f; \
733 #define casecomp(c, f, i, u, d, i64, ui64) \
734 case TGSI_OPCODE_##c: \
735 if (type == GLSL_TYPE_INT64) \
736 op = TGSI_OPCODE_##i64; \
737 else if (type == GLSL_TYPE_UINT64) \
738 op = TGSI_OPCODE_##ui64; \
739 else if (type == GLSL_TYPE_DOUBLE) \
740 op = TGSI_OPCODE_##d; \
741 else if (type == GLSL_TYPE_INT || type == GLSL_TYPE_SUBROUTINE) \
742 op = TGSI_OPCODE_##i; \
743 else if (type == GLSL_TYPE_UINT) \
744 op = TGSI_OPCODE_##u; \
745 else if (native_integers) \
746 op = TGSI_OPCODE_##f; \
748 op = TGSI_OPCODE_##c; \
752 /* Some instructions are initially selected without considering the type.
753 * This fixes the type:
755 * INIT FLOAT SINT UINT DOUBLE SINT64 UINT64
757 case7(ADD
, ADD
, UADD
, UADD
, DADD
, U64ADD
, U64ADD
);
758 case7(CEIL
, CEIL
, LAST
, LAST
, DCEIL
, LAST
, LAST
);
759 case7(DIV
, DIV
, IDIV
, UDIV
, DDIV
, I64DIV
, U64DIV
);
760 case7(FMA
, FMA
, UMAD
, UMAD
, DFMA
, LAST
, LAST
);
761 case7(FLR
, FLR
, LAST
, LAST
, DFLR
, LAST
, LAST
);
762 case7(FRC
, FRC
, LAST
, LAST
, DFRAC
, LAST
, LAST
);
763 case7(MUL
, MUL
, UMUL
, UMUL
, DMUL
, U64MUL
, U64MUL
);
764 case7(MAD
, MAD
, UMAD
, UMAD
, DMAD
, LAST
, LAST
);
765 case7(MAX
, MAX
, IMAX
, UMAX
, DMAX
, I64MAX
, U64MAX
);
766 case7(MIN
, MIN
, IMIN
, UMIN
, DMIN
, I64MIN
, U64MIN
);
767 case7(RCP
, RCP
, LAST
, LAST
, DRCP
, LAST
, LAST
);
768 case7(ROUND
, ROUND
,LAST
, LAST
, DROUND
, LAST
, LAST
);
769 case7(RSQ
, RSQ
, LAST
, LAST
, DRSQ
, LAST
, LAST
);
770 case7(SQRT
, SQRT
, LAST
, LAST
, DSQRT
, LAST
, LAST
);
771 case7(SSG
, SSG
, ISSG
, ISSG
, DSSG
, I64SSG
, I64SSG
);
772 case7(TRUNC
, TRUNC
,LAST
, LAST
, DTRUNC
, LAST
, LAST
);
774 case7(MOD
, LAST
, MOD
, UMOD
, LAST
, I64MOD
, U64MOD
);
775 case7(SHL
, LAST
, SHL
, SHL
, LAST
, U64SHL
, U64SHL
);
776 case7(IBFE
, LAST
, IBFE
, UBFE
, LAST
, LAST
, LAST
);
777 case7(IMSB
, LAST
, IMSB
, UMSB
, LAST
, LAST
, LAST
);
778 case7(IMUL_HI
, LAST
, IMUL_HI
, UMUL_HI
, LAST
, LAST
, LAST
);
779 case7(ISHR
, LAST
, ISHR
, USHR
, LAST
, I64SHR
, U64SHR
);
780 case7(ATOMIMAX
,LAST
, ATOMIMAX
,ATOMUMAX
,LAST
, LAST
, LAST
);
781 case7(ATOMIMIN
,LAST
, ATOMIMIN
,ATOMUMIN
,LAST
, LAST
, LAST
);
782 case7(ATOMUADD
,ATOMFADD
,ATOMUADD
,ATOMUADD
,LAST
, LAST
, LAST
);
784 casecomp(SEQ
, FSEQ
, USEQ
, USEQ
, DSEQ
, U64SEQ
, U64SEQ
);
785 casecomp(SNE
, FSNE
, USNE
, USNE
, DSNE
, U64SNE
, U64SNE
);
786 casecomp(SGE
, FSGE
, ISGE
, USGE
, DSGE
, I64SGE
, U64SGE
);
787 casecomp(SLT
, FSLT
, ISLT
, USLT
, DSLT
, I64SLT
, U64SLT
);
793 assert(op
!= TGSI_OPCODE_LAST
);
797 glsl_to_tgsi_instruction
*
798 glsl_to_tgsi_visitor::emit_dp(ir_instruction
*ir
,
799 st_dst_reg dst
, st_src_reg src0
, st_src_reg src1
,
802 static const enum tgsi_opcode dot_opcodes
[] = {
803 TGSI_OPCODE_DP2
, TGSI_OPCODE_DP3
, TGSI_OPCODE_DP4
806 return emit_asm(ir
, dot_opcodes
[elements
- 2], dst
, src0
, src1
);
810 * Emits TGSI scalar opcodes to produce unique answers across channels.
812 * Some TGSI opcodes are scalar-only, like ARB_fp/vp. The src X
813 * channel determines the result across all channels. So to do a vec4
814 * of this operation, we want to emit a scalar per source channel used
815 * to produce dest channels.
818 glsl_to_tgsi_visitor::emit_scalar(ir_instruction
*ir
, enum tgsi_opcode op
,
820 st_src_reg orig_src0
, st_src_reg orig_src1
)
823 int done_mask
= ~dst
.writemask
;
825 /* TGSI RCP is a scalar operation splatting results to all channels,
826 * like ARB_fp/vp. So emit as many RCPs as necessary to cover our
829 for (i
= 0; i
< 4; i
++) {
830 GLuint this_mask
= (1 << i
);
831 st_src_reg src0
= orig_src0
;
832 st_src_reg src1
= orig_src1
;
834 if (done_mask
& this_mask
)
837 GLuint src0_swiz
= GET_SWZ(src0
.swizzle
, i
);
838 GLuint src1_swiz
= GET_SWZ(src1
.swizzle
, i
);
839 for (j
= i
+ 1; j
< 4; j
++) {
840 /* If there is another enabled component in the destination that is
841 * derived from the same inputs, generate its value on this pass as
844 if (!(done_mask
& (1 << j
)) &&
845 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
&&
846 GET_SWZ(src1
.swizzle
, j
) == src1_swiz
) {
847 this_mask
|= (1 << j
);
850 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
851 src0_swiz
, src0_swiz
);
852 src1
.swizzle
= MAKE_SWIZZLE4(src1_swiz
, src1_swiz
,
853 src1_swiz
, src1_swiz
);
855 dst
.writemask
= this_mask
;
856 emit_asm(ir
, op
, dst
, src0
, src1
);
857 done_mask
|= this_mask
;
862 glsl_to_tgsi_visitor::emit_scalar(ir_instruction
*ir
, enum tgsi_opcode op
,
863 st_dst_reg dst
, st_src_reg src0
)
865 st_src_reg undef
= undef_src
;
867 undef
.swizzle
= SWIZZLE_XXXX
;
869 emit_scalar(ir
, op
, dst
, src0
, undef
);
873 glsl_to_tgsi_visitor::emit_arl(ir_instruction
*ir
,
874 st_dst_reg dst
, st_src_reg src0
)
876 enum tgsi_opcode op
= TGSI_OPCODE_ARL
;
878 if (src0
.type
== GLSL_TYPE_INT
|| src0
.type
== GLSL_TYPE_UINT
) {
879 if (!this->need_uarl
&& src0
.is_legal_tgsi_address_operand())
882 op
= TGSI_OPCODE_UARL
;
885 assert(dst
.file
== PROGRAM_ADDRESS
);
886 if (dst
.index
>= this->num_address_regs
)
887 this->num_address_regs
= dst
.index
+ 1;
889 emit_asm(NULL
, op
, dst
, src0
);
893 glsl_to_tgsi_visitor::add_constant(gl_register_file file
,
894 gl_constant_value values
[8], int size
,
896 uint16_t *swizzle_out
)
898 if (file
== PROGRAM_CONSTANT
) {
899 GLuint swizzle
= swizzle_out
? *swizzle_out
: 0;
900 int result
= _mesa_add_typed_unnamed_constant(this->prog
->Parameters
,
901 values
, size
, datatype
,
904 *swizzle_out
= swizzle
;
908 assert(file
== PROGRAM_IMMEDIATE
);
911 immediate_storage
*entry
;
912 int size32
= size
* ((datatype
== GL_DOUBLE
||
913 datatype
== GL_INT64_ARB
||
914 datatype
== GL_UNSIGNED_INT64_ARB
) ? 2 : 1);
917 /* Search immediate storage to see if we already have an identical
918 * immediate that we can use instead of adding a duplicate entry.
920 foreach_in_list(immediate_storage
, entry
, &this->immediates
) {
921 immediate_storage
*tmp
= entry
;
923 for (i
= 0; i
* 4 < size32
; i
++) {
924 int slot_size
= MIN2(size32
- (i
* 4), 4);
925 if (tmp
->type
!= datatype
|| tmp
->size32
!= slot_size
)
927 if (memcmp(tmp
->values
, &values
[i
* 4],
928 slot_size
* sizeof(gl_constant_value
)))
931 /* Everything matches, keep going until the full size is matched */
932 tmp
= (immediate_storage
*)tmp
->next
;
935 /* The full value matched */
942 for (i
= 0; i
* 4 < size32
; i
++) {
943 int slot_size
= MIN2(size32
- (i
* 4), 4);
944 /* Add this immediate to the list. */
945 entry
= new(mem_ctx
) immediate_storage(&values
[i
* 4],
946 slot_size
, datatype
);
947 this->immediates
.push_tail(entry
);
948 this->num_immediates
++;
954 glsl_to_tgsi_visitor::st_src_reg_for_float(float val
)
956 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_FLOAT
);
957 union gl_constant_value uval
;
960 src
.index
= add_constant(src
.file
, &uval
, 1, GL_FLOAT
, &src
.swizzle
);
966 glsl_to_tgsi_visitor::st_src_reg_for_double(double val
)
968 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_DOUBLE
);
969 union gl_constant_value uval
[2];
971 memcpy(uval
, &val
, sizeof(uval
));
972 src
.index
= add_constant(src
.file
, uval
, 1, GL_DOUBLE
, &src
.swizzle
);
973 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_X
, SWIZZLE_Y
);
978 glsl_to_tgsi_visitor::st_src_reg_for_int(int val
)
980 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_INT
);
981 union gl_constant_value uval
;
983 assert(native_integers
);
986 src
.index
= add_constant(src
.file
, &uval
, 1, GL_INT
, &src
.swizzle
);
992 glsl_to_tgsi_visitor::st_src_reg_for_int64(int64_t val
)
994 st_src_reg
src(PROGRAM_IMMEDIATE
, -1, GLSL_TYPE_INT64
);
995 union gl_constant_value uval
[2];
997 memcpy(uval
, &val
, sizeof(uval
));
998 src
.index
= add_constant(src
.file
, uval
, 1, GL_DOUBLE
, &src
.swizzle
);
999 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_X
, SWIZZLE_Y
);
1005 glsl_to_tgsi_visitor::st_src_reg_for_type(enum glsl_base_type type
, int val
)
1007 if (native_integers
)
1008 return type
== GLSL_TYPE_FLOAT
? st_src_reg_for_float(val
) :
1009 st_src_reg_for_int(val
);
1011 return st_src_reg_for_float(val
);
1015 attrib_type_size(const struct glsl_type
*type
, bool is_vs_input
)
1017 return type
->count_attribute_slots(is_vs_input
);
1021 type_size(const struct glsl_type
*type
)
1023 return type
->count_attribute_slots(false);
1027 add_buffer_to_load_and_stores(glsl_to_tgsi_instruction
*inst
, st_src_reg
*buf
,
1028 exec_list
*instructions
, ir_constant
*access
)
1031 * emit_asm() might have actually split the op into pieces, e.g. for
1032 * double stores. We have to go back and fix up all the generated ops.
1034 enum tgsi_opcode op
= inst
->op
;
1036 inst
->resource
= *buf
;
1038 inst
->buffer_access
= access
->value
.u
[0];
1040 if (inst
== instructions
->get_head_raw())
1042 inst
= (glsl_to_tgsi_instruction
*)inst
->get_prev();
1044 if (inst
->op
== TGSI_OPCODE_UADD
) {
1045 if (inst
== instructions
->get_head_raw())
1047 inst
= (glsl_to_tgsi_instruction
*)inst
->get_prev();
1049 } while (inst
->op
== op
&& inst
->resource
.file
== PROGRAM_UNDEFINED
);
1053 * If the given GLSL type is an array or matrix or a structure containing
1054 * an array/matrix member, return true. Else return false.
1056 * This is used to determine which kind of temp storage (PROGRAM_TEMPORARY
1057 * or PROGRAM_ARRAY) should be used for variables of this type. Anytime
1058 * we have an array that might be indexed with a variable, we need to use
1059 * the later storage type.
1062 type_has_array_or_matrix(const glsl_type
*type
)
1064 if (type
->is_array() || type
->is_matrix())
1067 if (type
->is_struct()) {
1068 for (unsigned i
= 0; i
< type
->length
; i
++) {
1069 if (type_has_array_or_matrix(type
->fields
.structure
[i
].type
)) {
1080 * In the initial pass of codegen, we assign temporary numbers to
1081 * intermediate results. (not SSA -- variable assignments will reuse
1085 glsl_to_tgsi_visitor::get_temp(const glsl_type
*type
)
1089 src
.type
= native_integers
? type
->base_type
: GLSL_TYPE_FLOAT
;
1094 if (!options
->EmitNoIndirectTemp
&& type_has_array_or_matrix(type
)) {
1095 if (next_array
>= max_num_arrays
) {
1096 max_num_arrays
+= 32;
1097 array_sizes
= (unsigned*)
1098 realloc(array_sizes
, sizeof(array_sizes
[0]) * max_num_arrays
);
1101 src
.file
= PROGRAM_ARRAY
;
1103 src
.array_id
= next_array
+ 1;
1104 array_sizes
[next_array
] = type_size(type
);
1108 src
.file
= PROGRAM_TEMPORARY
;
1109 src
.index
= next_temp
;
1110 next_temp
+= type_size(type
);
1113 if (type
->is_array() || type
->is_struct()) {
1114 src
.swizzle
= SWIZZLE_NOOP
;
1116 src
.swizzle
= swizzle_for_size(type
->vector_elements
);
1123 glsl_to_tgsi_visitor::find_variable_storage(ir_variable
*var
)
1125 struct hash_entry
*entry
;
1127 entry
= _mesa_hash_table_search(this->variables
, var
);
1131 return (variable_storage
*)entry
->data
;
1135 glsl_to_tgsi_visitor::visit(ir_variable
*ir
)
1137 if (ir
->data
.mode
== ir_var_uniform
&& strncmp(ir
->name
, "gl_", 3) == 0) {
1139 const ir_state_slot
*const slots
= ir
->get_state_slots();
1140 assert(slots
!= NULL
);
1142 /* Check if this statevar's setup in the STATE file exactly
1143 * matches how we'll want to reference it as a
1144 * struct/array/whatever. If not, then we need to move it into
1145 * temporary storage and hope that it'll get copy-propagated
1148 for (i
= 0; i
< ir
->get_num_state_slots(); i
++) {
1149 if (slots
[i
].swizzle
!= SWIZZLE_XYZW
) {
1154 variable_storage
*storage
;
1156 if (i
== ir
->get_num_state_slots()) {
1157 /* We'll set the index later. */
1158 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_STATE_VAR
, -1);
1160 _mesa_hash_table_insert(this->variables
, ir
, storage
);
1164 /* The variable_storage constructor allocates slots based on the size
1165 * of the type. However, this had better match the number of state
1166 * elements that we're going to copy into the new temporary.
1168 assert((int) ir
->get_num_state_slots() == type_size(ir
->type
));
1170 dst
= st_dst_reg(get_temp(ir
->type
));
1172 storage
= new(mem_ctx
) variable_storage(ir
, dst
.file
, dst
.index
,
1175 _mesa_hash_table_insert(this->variables
, ir
, storage
);
1179 for (unsigned int i
= 0; i
< ir
->get_num_state_slots(); i
++) {
1180 int index
= _mesa_add_state_reference(this->prog
->Parameters
,
1183 if (storage
->file
== PROGRAM_STATE_VAR
) {
1184 if (storage
->index
== -1) {
1185 storage
->index
= index
;
1187 assert(index
== storage
->index
+ (int)i
);
1190 /* We use GLSL_TYPE_FLOAT here regardless of the actual type of
1191 * the data being moved since MOV does not care about the type of
1192 * data it is moving, and we don't want to declare registers with
1193 * array or struct types.
1195 st_src_reg
src(PROGRAM_STATE_VAR
, index
, GLSL_TYPE_FLOAT
);
1196 src
.swizzle
= slots
[i
].swizzle
;
1197 emit_asm(ir
, TGSI_OPCODE_MOV
, dst
, src
);
1198 /* even a float takes up a whole vec4 reg in a struct/array. */
1203 if (storage
->file
== PROGRAM_TEMPORARY
&&
1204 dst
.index
!= storage
->index
+ (int) ir
->get_num_state_slots()) {
1205 fail_link(this->shader_program
,
1206 "failed to load builtin uniform `%s' (%d/%d regs loaded)\n",
1207 ir
->name
, dst
.index
- storage
->index
,
1208 type_size(ir
->type
));
1214 glsl_to_tgsi_visitor::visit(ir_loop
*ir
)
1216 emit_asm(NULL
, TGSI_OPCODE_BGNLOOP
);
1218 visit_exec_list(&ir
->body_instructions
, this);
1220 emit_asm(NULL
, TGSI_OPCODE_ENDLOOP
);
1224 glsl_to_tgsi_visitor::visit(ir_loop_jump
*ir
)
1227 case ir_loop_jump::jump_break
:
1228 emit_asm(NULL
, TGSI_OPCODE_BRK
);
1230 case ir_loop_jump::jump_continue
:
1231 emit_asm(NULL
, TGSI_OPCODE_CONT
);
1238 glsl_to_tgsi_visitor::visit(ir_function_signature
*ir
)
1245 glsl_to_tgsi_visitor::visit(ir_function
*ir
)
1247 /* Ignore function bodies other than main() -- we shouldn't see calls to
1248 * them since they should all be inlined before we get to glsl_to_tgsi.
1250 if (strcmp(ir
->name
, "main") == 0) {
1251 const ir_function_signature
*sig
;
1254 sig
= ir
->matching_signature(NULL
, &empty
, false);
1258 foreach_in_list(ir_instruction
, ir
, &sig
->body
) {
1265 glsl_to_tgsi_visitor::try_emit_mad(ir_expression
*ir
, int mul_operand
)
1267 int nonmul_operand
= 1 - mul_operand
;
1269 st_dst_reg result_dst
;
1271 // there is no TGSI opcode for this
1272 if (ir
->type
->is_integer_64())
1275 ir_expression
*expr
= ir
->operands
[mul_operand
]->as_expression();
1276 if (!expr
|| expr
->operation
!= ir_binop_mul
)
1279 expr
->operands
[0]->accept(this);
1281 expr
->operands
[1]->accept(this);
1283 ir
->operands
[nonmul_operand
]->accept(this);
1286 this->result
= get_temp(ir
->type
);
1287 result_dst
= st_dst_reg(this->result
);
1288 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1289 emit_asm(ir
, TGSI_OPCODE_MAD
, result_dst
, a
, b
, c
);
1295 * Emit MAD(a, -b, a) instead of AND(a, NOT(b))
1297 * The logic values are 1.0 for true and 0.0 for false. Logical-and is
1298 * implemented using multiplication, and logical-or is implemented using
1299 * addition. Logical-not can be implemented as (true - x), or (1.0 - x).
1300 * As result, the logical expression (a & !b) can be rewritten as:
1304 * - (a * 1) - (a * b)
1308 * This final expression can be implemented as a single MAD(a, -b, a)
1312 glsl_to_tgsi_visitor::try_emit_mad_for_and_not(ir_expression
*ir
,
1315 const int other_operand
= 1 - try_operand
;
1318 ir_expression
*expr
= ir
->operands
[try_operand
]->as_expression();
1319 if (!expr
|| expr
->operation
!= ir_unop_logic_not
)
1322 ir
->operands
[other_operand
]->accept(this);
1324 expr
->operands
[0]->accept(this);
1327 b
.negate
= ~b
.negate
;
1329 this->result
= get_temp(ir
->type
);
1330 emit_asm(ir
, TGSI_OPCODE_MAD
, st_dst_reg(this->result
), a
, b
, a
);
1336 glsl_to_tgsi_visitor::reladdr_to_temp(ir_instruction
*ir
,
1337 st_src_reg
*reg
, int *num_reladdr
)
1339 if (!reg
->reladdr
&& !reg
->reladdr2
)
1343 emit_arl(ir
, address_reg
, *reg
->reladdr
);
1345 emit_arl(ir
, address_reg2
, *reg
->reladdr2
);
1347 if (*num_reladdr
!= 1) {
1348 st_src_reg temp
= get_temp(glsl_type::get_instance(reg
->type
, 4, 1));
1350 emit_asm(ir
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), *reg
);
1358 glsl_to_tgsi_visitor::visit(ir_expression
*ir
)
1360 st_src_reg op
[ARRAY_SIZE(ir
->operands
)];
1362 /* Quick peephole: Emit MAD(a, b, c) instead of ADD(MUL(a, b), c)
1364 if (!this->precise
&& ir
->operation
== ir_binop_add
) {
1365 if (try_emit_mad(ir
, 1))
1367 if (try_emit_mad(ir
, 0))
1371 /* Quick peephole: Emit OPCODE_MAD(-a, -b, a) instead of AND(a, NOT(b))
1373 if (!native_integers
&& ir
->operation
== ir_binop_logic_and
) {
1374 if (try_emit_mad_for_and_not(ir
, 1))
1376 if (try_emit_mad_for_and_not(ir
, 0))
1380 if (ir
->operation
== ir_quadop_vector
)
1381 assert(!"ir_quadop_vector should have been lowered");
1383 for (unsigned int operand
= 0; operand
< ir
->num_operands
; operand
++) {
1384 this->result
.file
= PROGRAM_UNDEFINED
;
1385 ir
->operands
[operand
]->accept(this);
1386 if (this->result
.file
== PROGRAM_UNDEFINED
) {
1387 printf("Failed to get tree for expression operand:\n");
1388 ir
->operands
[operand
]->print();
1392 op
[operand
] = this->result
;
1394 /* Matrix expression operands should have been broken down to vector
1395 * operations already.
1397 assert(!ir
->operands
[operand
]->type
->is_matrix());
1400 visit_expression(ir
, op
);
1403 /* The non-recursive part of the expression visitor lives in a separate
1404 * function and should be prevented from being inlined, to avoid a stack
1405 * explosion when deeply nested expressions are visited.
1408 glsl_to_tgsi_visitor::visit_expression(ir_expression
* ir
, st_src_reg
*op
)
1410 st_src_reg result_src
;
1411 st_dst_reg result_dst
;
1413 int vector_elements
= ir
->operands
[0]->type
->vector_elements
;
1414 if (ir
->operands
[1] &&
1415 ir
->operation
!= ir_binop_interpolate_at_offset
&&
1416 ir
->operation
!= ir_binop_interpolate_at_sample
) {
1417 st_src_reg
*swz_op
= NULL
;
1418 if (vector_elements
> ir
->operands
[1]->type
->vector_elements
) {
1419 assert(ir
->operands
[1]->type
->vector_elements
== 1);
1421 } else if (vector_elements
< ir
->operands
[1]->type
->vector_elements
) {
1422 assert(ir
->operands
[0]->type
->vector_elements
== 1);
1426 uint16_t swizzle_x
= GET_SWZ(swz_op
->swizzle
, 0);
1427 swz_op
->swizzle
= MAKE_SWIZZLE4(swizzle_x
, swizzle_x
,
1428 swizzle_x
, swizzle_x
);
1430 vector_elements
= MAX2(vector_elements
,
1431 ir
->operands
[1]->type
->vector_elements
);
1433 if (ir
->operands
[2] &&
1434 ir
->operands
[2]->type
->vector_elements
!= vector_elements
) {
1435 /* This can happen with ir_triop_lrp, i.e. glsl mix */
1436 assert(ir
->operands
[2]->type
->vector_elements
== 1);
1437 uint16_t swizzle_x
= GET_SWZ(op
[2].swizzle
, 0);
1438 op
[2].swizzle
= MAKE_SWIZZLE4(swizzle_x
, swizzle_x
,
1439 swizzle_x
, swizzle_x
);
1442 this->result
.file
= PROGRAM_UNDEFINED
;
1444 /* Storage for our result. Ideally for an assignment we'd be using
1445 * the actual storage for the result here, instead.
1447 result_src
= get_temp(ir
->type
);
1448 /* convenience for the emit functions below. */
1449 result_dst
= st_dst_reg(result_src
);
1450 /* Limit writes to the channels that will be used by result_src later.
1451 * This does limit this temp's use as a temporary for multi-instruction
1454 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1456 switch (ir
->operation
) {
1457 case ir_unop_logic_not
:
1458 if (result_dst
.type
!= GLSL_TYPE_FLOAT
)
1459 emit_asm(ir
, TGSI_OPCODE_NOT
, result_dst
, op
[0]);
1461 /* Previously 'SEQ dst, src, 0.0' was used for this. However, many
1462 * older GPUs implement SEQ using multiple instructions (i915 uses two
1463 * SGE instructions and a MUL instruction). Since our logic values are
1464 * 0.0 and 1.0, 1-x also implements !x.
1466 op
[0].negate
= ~op
[0].negate
;
1467 emit_asm(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0],
1468 st_src_reg_for_float(1.0));
1472 if (result_dst
.type
== GLSL_TYPE_INT64
||
1473 result_dst
.type
== GLSL_TYPE_UINT64
)
1474 emit_asm(ir
, TGSI_OPCODE_I64NEG
, result_dst
, op
[0]);
1475 else if (result_dst
.type
== GLSL_TYPE_INT
||
1476 result_dst
.type
== GLSL_TYPE_UINT
)
1477 emit_asm(ir
, TGSI_OPCODE_INEG
, result_dst
, op
[0]);
1478 else if (result_dst
.type
== GLSL_TYPE_DOUBLE
)
1479 emit_asm(ir
, TGSI_OPCODE_DNEG
, result_dst
, op
[0]);
1481 op
[0].negate
= ~op
[0].negate
;
1485 case ir_unop_subroutine_to_int
:
1486 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0]);
1489 if (result_dst
.type
== GLSL_TYPE_FLOAT
)
1490 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0].get_abs());
1491 else if (result_dst
.type
== GLSL_TYPE_DOUBLE
)
1492 emit_asm(ir
, TGSI_OPCODE_DABS
, result_dst
, op
[0]);
1493 else if (result_dst
.type
== GLSL_TYPE_INT64
||
1494 result_dst
.type
== GLSL_TYPE_UINT64
)
1495 emit_asm(ir
, TGSI_OPCODE_I64ABS
, result_dst
, op
[0]);
1497 emit_asm(ir
, TGSI_OPCODE_IABS
, result_dst
, op
[0]);
1500 emit_asm(ir
, TGSI_OPCODE_SSG
, result_dst
, op
[0]);
1503 emit_scalar(ir
, TGSI_OPCODE_RCP
, result_dst
, op
[0]);
1507 emit_scalar(ir
, TGSI_OPCODE_EX2
, result_dst
, op
[0]);
1510 assert(!"not reached: should be handled by exp_to_exp2");
1513 assert(!"not reached: should be handled by log_to_log2");
1516 emit_scalar(ir
, TGSI_OPCODE_LG2
, result_dst
, op
[0]);
1519 emit_scalar(ir
, TGSI_OPCODE_SIN
, result_dst
, op
[0]);
1522 emit_scalar(ir
, TGSI_OPCODE_COS
, result_dst
, op
[0]);
1524 case ir_unop_saturate
: {
1525 glsl_to_tgsi_instruction
*inst
;
1526 inst
= emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0]);
1527 inst
->saturate
= true;
1532 case ir_unop_dFdx_coarse
:
1533 emit_asm(ir
, TGSI_OPCODE_DDX
, result_dst
, op
[0]);
1535 case ir_unop_dFdx_fine
:
1536 emit_asm(ir
, TGSI_OPCODE_DDX_FINE
, result_dst
, op
[0]);
1539 case ir_unop_dFdy_coarse
:
1540 case ir_unop_dFdy_fine
:
1542 /* The X component contains 1 or -1 depending on whether the framebuffer
1543 * is a FBO or the window system buffer, respectively.
1544 * It is then multiplied with the source operand of DDY.
1546 static const gl_state_index16 transform_y_state
[STATE_LENGTH
]
1547 = { STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
};
1549 unsigned transform_y_index
=
1550 _mesa_add_state_reference(this->prog
->Parameters
,
1553 st_src_reg transform_y
= st_src_reg(PROGRAM_STATE_VAR
,
1555 glsl_type::vec4_type
);
1556 transform_y
.swizzle
= SWIZZLE_XXXX
;
1558 st_src_reg temp
= get_temp(glsl_type::vec4_type
);
1560 emit_asm(ir
, TGSI_OPCODE_MUL
, st_dst_reg(temp
), transform_y
, op
[0]);
1561 emit_asm(ir
, ir
->operation
== ir_unop_dFdy_fine
?
1562 TGSI_OPCODE_DDY_FINE
: TGSI_OPCODE_DDY
, result_dst
, temp
);
1566 case ir_unop_frexp_sig
:
1567 emit_asm(ir
, TGSI_OPCODE_DFRACEXP
, result_dst
, undef_dst
, op
[0]);
1570 case ir_unop_frexp_exp
:
1571 emit_asm(ir
, TGSI_OPCODE_DFRACEXP
, undef_dst
, result_dst
, op
[0]);
1574 case ir_unop_noise
: {
1575 /* At some point, a motivated person could add a better
1576 * implementation of noise. Currently not even the nvidia
1577 * binary drivers do anything more than this. In any case, the
1578 * place to do this is in the GL state tracker, not the poor
1581 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, st_src_reg_for_float(0.5));
1586 emit_asm(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1589 op
[1].negate
= ~op
[1].negate
;
1590 emit_asm(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1594 emit_asm(ir
, TGSI_OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1597 emit_asm(ir
, TGSI_OPCODE_DIV
, result_dst
, op
[0], op
[1]);
1600 if (result_dst
.type
== GLSL_TYPE_FLOAT
)
1601 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
1603 emit_asm(ir
, TGSI_OPCODE_MOD
, result_dst
, op
[0], op
[1]);
1607 emit_asm(ir
, TGSI_OPCODE_SLT
, result_dst
, op
[0], op
[1]);
1609 case ir_binop_gequal
:
1610 emit_asm(ir
, TGSI_OPCODE_SGE
, result_dst
, op
[0], op
[1]);
1612 case ir_binop_equal
:
1613 emit_asm(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1615 case ir_binop_nequal
:
1616 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1618 case ir_binop_all_equal
:
1619 /* "==" operator producing a scalar boolean. */
1620 if (ir
->operands
[0]->type
->is_vector() ||
1621 ir
->operands
[1]->type
->is_vector()) {
1622 st_src_reg temp
= get_temp(native_integers
?
1623 glsl_type::uvec4_type
:
1624 glsl_type::vec4_type
);
1626 if (native_integers
) {
1627 st_dst_reg temp_dst
= st_dst_reg(temp
);
1628 st_src_reg temp1
= st_src_reg(temp
), temp2
= st_src_reg(temp
);
1630 if (ir
->operands
[0]->type
->is_boolean() &&
1631 ir
->operands
[1]->as_constant() &&
1632 ir
->operands
[1]->as_constant()->is_one()) {
1633 emit_asm(ir
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), op
[0]);
1635 emit_asm(ir
, TGSI_OPCODE_SEQ
, st_dst_reg(temp
), op
[0], op
[1]);
1638 /* Emit 1-3 AND operations to combine the SEQ results. */
1639 switch (ir
->operands
[0]->type
->vector_elements
) {
1643 temp_dst
.writemask
= WRITEMASK_Y
;
1644 temp1
.swizzle
= SWIZZLE_YYYY
;
1645 temp2
.swizzle
= SWIZZLE_ZZZZ
;
1646 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, temp1
, temp2
);
1649 temp_dst
.writemask
= WRITEMASK_X
;
1650 temp1
.swizzle
= SWIZZLE_XXXX
;
1651 temp2
.swizzle
= SWIZZLE_YYYY
;
1652 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, temp1
, temp2
);
1653 temp_dst
.writemask
= WRITEMASK_Y
;
1654 temp1
.swizzle
= SWIZZLE_ZZZZ
;
1655 temp2
.swizzle
= SWIZZLE_WWWW
;
1656 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, temp1
, temp2
);
1659 temp1
.swizzle
= SWIZZLE_XXXX
;
1660 temp2
.swizzle
= SWIZZLE_YYYY
;
1661 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, temp1
, temp2
);
1663 emit_asm(ir
, TGSI_OPCODE_SNE
, st_dst_reg(temp
), op
[0], op
[1]);
1665 /* After the dot-product, the value will be an integer on the
1666 * range [0,4]. Zero becomes 1.0, and positive values become zero.
1668 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1670 /* Negating the result of the dot-product gives values on the range
1671 * [-4, 0]. Zero becomes 1.0, and negative values become zero.
1672 * This is achieved using SGE.
1674 st_src_reg sge_src
= result_src
;
1675 sge_src
.negate
= ~sge_src
.negate
;
1676 emit_asm(ir
, TGSI_OPCODE_SGE
, result_dst
, sge_src
,
1677 st_src_reg_for_float(0.0));
1680 emit_asm(ir
, TGSI_OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1683 case ir_binop_any_nequal
:
1684 /* "!=" operator producing a scalar boolean. */
1685 if (ir
->operands
[0]->type
->is_vector() ||
1686 ir
->operands
[1]->type
->is_vector()) {
1687 st_src_reg temp
= get_temp(native_integers
?
1688 glsl_type::uvec4_type
:
1689 glsl_type::vec4_type
);
1690 if (ir
->operands
[0]->type
->is_boolean() &&
1691 ir
->operands
[1]->as_constant() &&
1692 ir
->operands
[1]->as_constant()->is_zero()) {
1693 emit_asm(ir
, TGSI_OPCODE_MOV
, st_dst_reg(temp
), op
[0]);
1695 emit_asm(ir
, TGSI_OPCODE_SNE
, st_dst_reg(temp
), op
[0], op
[1]);
1698 if (native_integers
) {
1699 st_dst_reg temp_dst
= st_dst_reg(temp
);
1700 st_src_reg temp1
= st_src_reg(temp
), temp2
= st_src_reg(temp
);
1702 /* Emit 1-3 OR operations to combine the SNE results. */
1703 switch (ir
->operands
[0]->type
->vector_elements
) {
1707 temp_dst
.writemask
= WRITEMASK_Y
;
1708 temp1
.swizzle
= SWIZZLE_YYYY
;
1709 temp2
.swizzle
= SWIZZLE_ZZZZ
;
1710 emit_asm(ir
, TGSI_OPCODE_OR
, temp_dst
, temp1
, temp2
);
1713 temp_dst
.writemask
= WRITEMASK_X
;
1714 temp1
.swizzle
= SWIZZLE_XXXX
;
1715 temp2
.swizzle
= SWIZZLE_YYYY
;
1716 emit_asm(ir
, TGSI_OPCODE_OR
, temp_dst
, temp1
, temp2
);
1717 temp_dst
.writemask
= WRITEMASK_Y
;
1718 temp1
.swizzle
= SWIZZLE_ZZZZ
;
1719 temp2
.swizzle
= SWIZZLE_WWWW
;
1720 emit_asm(ir
, TGSI_OPCODE_OR
, temp_dst
, temp1
, temp2
);
1723 temp1
.swizzle
= SWIZZLE_XXXX
;
1724 temp2
.swizzle
= SWIZZLE_YYYY
;
1725 emit_asm(ir
, TGSI_OPCODE_OR
, result_dst
, temp1
, temp2
);
1727 /* After the dot-product, the value will be an integer on the
1728 * range [0,4]. Zero stays zero, and positive values become 1.0.
1730 glsl_to_tgsi_instruction
*const dp
=
1731 emit_dp(ir
, result_dst
, temp
, temp
, vector_elements
);
1732 if (this->prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1733 /* The clamping to [0,1] can be done for free in the fragment
1734 * shader with a saturate.
1736 dp
->saturate
= true;
1738 /* Negating the result of the dot-product gives values on the
1739 * range [-4, 0]. Zero stays zero, and negative values become
1740 * 1.0. This achieved using SLT.
1742 st_src_reg slt_src
= result_src
;
1743 slt_src
.negate
= ~slt_src
.negate
;
1744 emit_asm(ir
, TGSI_OPCODE_SLT
, result_dst
, slt_src
,
1745 st_src_reg_for_float(0.0));
1749 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1753 case ir_binop_logic_xor
:
1754 if (native_integers
)
1755 emit_asm(ir
, TGSI_OPCODE_XOR
, result_dst
, op
[0], op
[1]);
1757 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1760 case ir_binop_logic_or
: {
1761 if (native_integers
) {
1762 /* If integers are used as booleans, we can use an actual "or"
1765 assert(native_integers
);
1766 emit_asm(ir
, TGSI_OPCODE_OR
, result_dst
, op
[0], op
[1]);
1768 /* After the addition, the value will be an integer on the
1769 * range [0,2]. Zero stays zero, and positive values become 1.0.
1771 glsl_to_tgsi_instruction
*add
=
1772 emit_asm(ir
, TGSI_OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1773 if (this->prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1774 /* The clamping to [0,1] can be done for free in the fragment
1775 * shader with a saturate if floats are being used as boolean
1778 add
->saturate
= true;
1780 /* Negating the result of the addition gives values on the range
1781 * [-2, 0]. Zero stays zero, and negative values become 1.0
1782 * This is achieved using SLT.
1784 st_src_reg slt_src
= result_src
;
1785 slt_src
.negate
= ~slt_src
.negate
;
1786 emit_asm(ir
, TGSI_OPCODE_SLT
, result_dst
, slt_src
,
1787 st_src_reg_for_float(0.0));
1793 case ir_binop_logic_and
:
1794 /* If native integers are disabled, the bool args are stored as float 0.0
1795 * or 1.0, so "mul" gives us "and". If they're enabled, just use the
1796 * actual AND opcode.
1798 if (native_integers
)
1799 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], op
[1]);
1801 emit_asm(ir
, TGSI_OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1805 assert(ir
->operands
[0]->type
->is_vector());
1806 assert(ir
->operands
[0]->type
== ir
->operands
[1]->type
);
1807 emit_dp(ir
, result_dst
, op
[0], op
[1],
1808 ir
->operands
[0]->type
->vector_elements
);
1813 emit_scalar(ir
, TGSI_OPCODE_SQRT
, result_dst
, op
[0]);
1815 /* This is the only instruction sequence that makes the game "Risen"
1816 * render correctly. ABS is not required for the game, but since GLSL
1817 * declares negative values as "undefined", allowing us to do whatever
1818 * we want, I choose to use ABS to match DX9 and pre-GLSL RSQ
1821 emit_scalar(ir
, TGSI_OPCODE_RSQ
, result_dst
, op
[0].get_abs());
1822 emit_scalar(ir
, TGSI_OPCODE_RCP
, result_dst
, result_src
);
1826 emit_scalar(ir
, TGSI_OPCODE_RSQ
, result_dst
, op
[0]);
1829 if (native_integers
) {
1830 emit_asm(ir
, TGSI_OPCODE_I2F
, result_dst
, op
[0]);
1833 /* fallthrough to next case otherwise */
1835 if (native_integers
) {
1836 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0],
1837 st_src_reg_for_float(1.0));
1840 /* fallthrough to next case otherwise */
1843 case ir_unop_i642u64
:
1844 case ir_unop_u642i64
:
1845 /* Converting between signed and unsigned integers is a no-op. */
1847 result_src
.type
= result_dst
.type
;
1850 if (native_integers
) {
1851 /* Booleans are stored as integers using ~0 for true and 0 for false.
1852 * GLSL requires that int(bool) return 1 for true and 0 for false.
1853 * This conversion is done with AND, but it could be done with NEG.
1855 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0],
1856 st_src_reg_for_int(1));
1858 /* Booleans and integers are both stored as floats when native
1859 * integers are disabled.
1865 if (native_integers
)
1866 emit_asm(ir
, TGSI_OPCODE_F2I
, result_dst
, op
[0]);
1868 emit_asm(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
1871 if (native_integers
)
1872 emit_asm(ir
, TGSI_OPCODE_F2U
, result_dst
, op
[0]);
1874 emit_asm(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
1876 case ir_unop_bitcast_f2i
:
1877 case ir_unop_bitcast_f2u
:
1878 /* Make sure we don't propagate the negate modifier to integer opcodes. */
1879 if (op
[0].negate
|| op
[0].abs
)
1880 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0]);
1883 result_src
.type
= ir
->operation
== ir_unop_bitcast_f2i
? GLSL_TYPE_INT
:
1886 case ir_unop_bitcast_i2f
:
1887 case ir_unop_bitcast_u2f
:
1889 result_src
.type
= GLSL_TYPE_FLOAT
;
1892 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0],
1893 st_src_reg_for_float(0.0));
1896 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0],
1897 st_src_reg_for_double(0.0));
1900 if (native_integers
)
1901 emit_asm(ir
, TGSI_OPCODE_USNE
, result_dst
, op
[0],
1902 st_src_reg_for_int(0));
1904 emit_asm(ir
, TGSI_OPCODE_SNE
, result_dst
, op
[0],
1905 st_src_reg_for_float(0.0));
1907 case ir_unop_bitcast_u642d
:
1908 case ir_unop_bitcast_i642d
:
1910 result_src
.type
= GLSL_TYPE_DOUBLE
;
1912 case ir_unop_bitcast_d2i64
:
1914 result_src
.type
= GLSL_TYPE_INT64
;
1916 case ir_unop_bitcast_d2u64
:
1918 result_src
.type
= GLSL_TYPE_UINT64
;
1921 emit_asm(ir
, TGSI_OPCODE_TRUNC
, result_dst
, op
[0]);
1924 emit_asm(ir
, TGSI_OPCODE_CEIL
, result_dst
, op
[0]);
1927 emit_asm(ir
, TGSI_OPCODE_FLR
, result_dst
, op
[0]);
1929 case ir_unop_round_even
:
1930 emit_asm(ir
, TGSI_OPCODE_ROUND
, result_dst
, op
[0]);
1933 emit_asm(ir
, TGSI_OPCODE_FRC
, result_dst
, op
[0]);
1937 emit_asm(ir
, TGSI_OPCODE_MIN
, result_dst
, op
[0], op
[1]);
1940 emit_asm(ir
, TGSI_OPCODE_MAX
, result_dst
, op
[0], op
[1]);
1943 emit_scalar(ir
, TGSI_OPCODE_POW
, result_dst
, op
[0], op
[1]);
1946 case ir_unop_bit_not
:
1947 if (native_integers
) {
1948 emit_asm(ir
, TGSI_OPCODE_NOT
, result_dst
, op
[0]);
1952 if (native_integers
) {
1953 emit_asm(ir
, TGSI_OPCODE_U2F
, result_dst
, op
[0]);
1956 case ir_binop_lshift
:
1957 case ir_binop_rshift
:
1958 if (native_integers
) {
1959 enum tgsi_opcode opcode
= ir
->operation
== ir_binop_lshift
1960 ? TGSI_OPCODE_SHL
: TGSI_OPCODE_ISHR
;
1963 if (glsl_base_type_is_64bit(op
[0].type
)) {
1964 /* GLSL shift operations have 32-bit shift counts, but TGSI uses
1967 count
= get_temp(glsl_type::u64vec(ir
->operands
[1]
1968 ->type
->components()));
1969 emit_asm(ir
, TGSI_OPCODE_U2I64
, st_dst_reg(count
), op
[1]);
1974 emit_asm(ir
, opcode
, result_dst
, op
[0], count
);
1977 case ir_binop_bit_and
:
1978 if (native_integers
) {
1979 emit_asm(ir
, TGSI_OPCODE_AND
, result_dst
, op
[0], op
[1]);
1982 case ir_binop_bit_xor
:
1983 if (native_integers
) {
1984 emit_asm(ir
, TGSI_OPCODE_XOR
, result_dst
, op
[0], op
[1]);
1987 case ir_binop_bit_or
:
1988 if (native_integers
) {
1989 emit_asm(ir
, TGSI_OPCODE_OR
, result_dst
, op
[0], op
[1]);
1993 assert(!"GLSL 1.30 features unsupported");
1996 case ir_binop_ubo_load
: {
1997 if (ctx
->Const
.UseSTD430AsDefaultPacking
) {
1998 ir_rvalue
*block
= ir
->operands
[0];
1999 ir_rvalue
*offset
= ir
->operands
[1];
2000 ir_constant
*const_block
= block
->as_constant();
2002 st_src_reg
cbuf(PROGRAM_CONSTANT
,
2003 (const_block
? const_block
->value
.u
[0] + 1 : 1),
2004 ir
->type
->base_type
);
2006 cbuf
.has_index2
= true;
2009 block
->accept(this);
2010 cbuf
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
2011 *cbuf
.reladdr
= this->result
;
2012 emit_arl(ir
, sampler_reladdr
, this->result
);
2015 /* Calculate the surface offset */
2016 offset
->accept(this);
2017 st_src_reg off
= this->result
;
2019 glsl_to_tgsi_instruction
*inst
=
2020 emit_asm(ir
, TGSI_OPCODE_LOAD
, result_dst
, off
);
2022 if (result_dst
.type
== GLSL_TYPE_BOOL
)
2023 emit_asm(ir
, TGSI_OPCODE_USNE
, result_dst
, st_src_reg(result_dst
),
2024 st_src_reg_for_int(0));
2026 add_buffer_to_load_and_stores(inst
, &cbuf
, &this->instructions
,
2029 ir_constant
*const_uniform_block
= ir
->operands
[0]->as_constant();
2030 ir_constant
*const_offset_ir
= ir
->operands
[1]->as_constant();
2031 unsigned const_offset
= const_offset_ir
?
2032 const_offset_ir
->value
.u
[0] : 0;
2033 unsigned const_block
= const_uniform_block
?
2034 const_uniform_block
->value
.u
[0] + 1 : 1;
2035 st_src_reg index_reg
= get_temp(glsl_type::uint_type
);
2038 cbuf
.type
= ir
->type
->base_type
;
2039 cbuf
.file
= PROGRAM_CONSTANT
;
2041 cbuf
.reladdr
= NULL
;
2044 cbuf
.index2D
= const_block
;
2046 assert(ir
->type
->is_vector() || ir
->type
->is_scalar());
2048 if (const_offset_ir
) {
2049 /* Constant index into constant buffer */
2050 cbuf
.reladdr
= NULL
;
2051 cbuf
.index
= const_offset
/ 16;
2053 ir_expression
*offset_expr
= ir
->operands
[1]->as_expression();
2054 st_src_reg offset
= op
[1];
2056 /* The OpenGL spec is written in such a way that accesses with
2057 * non-constant offset are almost always vec4-aligned. The only
2058 * exception to this are members of structs in arrays of structs:
2059 * each struct in an array of structs is at least vec4-aligned,
2060 * but single-element and [ui]vec2 members of the struct may be at
2061 * an offset that is not a multiple of 16 bytes.
2063 * Here, we extract that offset, relying on previous passes to
2064 * always generate offset expressions of the form
2065 * (+ expr constant_offset).
2067 * Note that the std430 layout, which allows more cases of
2068 * alignment less than vec4 in arrays, is not supported for
2069 * uniform blocks, so we do not have to deal with it here.
2071 if (offset_expr
&& offset_expr
->operation
== ir_binop_add
) {
2072 const_offset_ir
= offset_expr
->operands
[1]->as_constant();
2073 if (const_offset_ir
) {
2074 const_offset
= const_offset_ir
->value
.u
[0];
2075 cbuf
.index
= const_offset
/ 16;
2076 offset_expr
->operands
[0]->accept(this);
2077 offset
= this->result
;
2081 /* Relative/variable index into constant buffer */
2082 emit_asm(ir
, TGSI_OPCODE_USHR
, st_dst_reg(index_reg
), offset
,
2083 st_src_reg_for_int(4));
2084 cbuf
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
2085 *cbuf
.reladdr
= index_reg
;
2088 if (const_uniform_block
) {
2089 /* Constant constant buffer */
2090 cbuf
.reladdr2
= NULL
;
2092 /* Relative/variable constant buffer */
2093 cbuf
.reladdr2
= ralloc(mem_ctx
, st_src_reg
);
2094 *cbuf
.reladdr2
= op
[0];
2096 cbuf
.has_index2
= true;
2098 cbuf
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
2099 if (glsl_base_type_is_64bit(cbuf
.type
))
2100 cbuf
.swizzle
+= MAKE_SWIZZLE4(const_offset
% 16 / 8,
2101 const_offset
% 16 / 8,
2102 const_offset
% 16 / 8,
2103 const_offset
% 16 / 8);
2105 cbuf
.swizzle
+= MAKE_SWIZZLE4(const_offset
% 16 / 4,
2106 const_offset
% 16 / 4,
2107 const_offset
% 16 / 4,
2108 const_offset
% 16 / 4);
2110 if (ir
->type
->is_boolean()) {
2111 emit_asm(ir
, TGSI_OPCODE_USNE
, result_dst
, cbuf
,
2112 st_src_reg_for_int(0));
2114 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, cbuf
);
2120 /* note: we have to reorder the three args here */
2121 emit_asm(ir
, TGSI_OPCODE_LRP
, result_dst
, op
[2], op
[1], op
[0]);
2124 if (this->ctx
->Const
.NativeIntegers
)
2125 emit_asm(ir
, TGSI_OPCODE_UCMP
, result_dst
, op
[0], op
[1], op
[2]);
2127 op
[0].negate
= ~op
[0].negate
;
2128 emit_asm(ir
, TGSI_OPCODE_CMP
, result_dst
, op
[0], op
[1], op
[2]);
2131 case ir_triop_bitfield_extract
:
2132 emit_asm(ir
, TGSI_OPCODE_IBFE
, result_dst
, op
[0], op
[1], op
[2]);
2134 case ir_quadop_bitfield_insert
:
2135 emit_asm(ir
, TGSI_OPCODE_BFI
, result_dst
, op
[0], op
[1], op
[2], op
[3]);
2137 case ir_unop_bitfield_reverse
:
2138 emit_asm(ir
, TGSI_OPCODE_BREV
, result_dst
, op
[0]);
2140 case ir_unop_bit_count
:
2141 emit_asm(ir
, TGSI_OPCODE_POPC
, result_dst
, op
[0]);
2143 case ir_unop_find_msb
:
2144 emit_asm(ir
, TGSI_OPCODE_IMSB
, result_dst
, op
[0]);
2146 case ir_unop_find_lsb
:
2147 emit_asm(ir
, TGSI_OPCODE_LSB
, result_dst
, op
[0]);
2149 case ir_binop_imul_high
:
2150 emit_asm(ir
, TGSI_OPCODE_IMUL_HI
, result_dst
, op
[0], op
[1]);
2153 /* In theory, MAD is incorrect here. */
2155 emit_asm(ir
, TGSI_OPCODE_FMA
, result_dst
, op
[0], op
[1], op
[2]);
2157 emit_asm(ir
, TGSI_OPCODE_MAD
, result_dst
, op
[0], op
[1], op
[2]);
2159 case ir_unop_interpolate_at_centroid
:
2160 emit_asm(ir
, TGSI_OPCODE_INTERP_CENTROID
, result_dst
, op
[0]);
2162 case ir_binop_interpolate_at_offset
: {
2163 /* The y coordinate needs to be flipped for the default fb */
2164 static const gl_state_index16 transform_y_state
[STATE_LENGTH
]
2165 = { STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
};
2167 unsigned transform_y_index
=
2168 _mesa_add_state_reference(this->prog
->Parameters
,
2171 st_src_reg transform_y
= st_src_reg(PROGRAM_STATE_VAR
,
2173 glsl_type::vec4_type
);
2174 transform_y
.swizzle
= SWIZZLE_XXXX
;
2176 st_src_reg temp
= get_temp(glsl_type::vec2_type
);
2177 st_dst_reg temp_dst
= st_dst_reg(temp
);
2179 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, op
[1]);
2180 temp_dst
.writemask
= WRITEMASK_Y
;
2181 emit_asm(ir
, TGSI_OPCODE_MUL
, temp_dst
, transform_y
, op
[1]);
2182 emit_asm(ir
, TGSI_OPCODE_INTERP_OFFSET
, result_dst
, op
[0], temp
);
2185 case ir_binop_interpolate_at_sample
:
2186 emit_asm(ir
, TGSI_OPCODE_INTERP_SAMPLE
, result_dst
, op
[0], op
[1]);
2190 emit_asm(ir
, TGSI_OPCODE_D2F
, result_dst
, op
[0]);
2193 emit_asm(ir
, TGSI_OPCODE_F2D
, result_dst
, op
[0]);
2196 emit_asm(ir
, TGSI_OPCODE_D2I
, result_dst
, op
[0]);
2199 emit_asm(ir
, TGSI_OPCODE_I2D
, result_dst
, op
[0]);
2202 emit_asm(ir
, TGSI_OPCODE_D2U
, result_dst
, op
[0]);
2205 emit_asm(ir
, TGSI_OPCODE_U2D
, result_dst
, op
[0]);
2207 case ir_unop_unpack_double_2x32
:
2208 case ir_unop_pack_double_2x32
:
2209 case ir_unop_unpack_int_2x32
:
2210 case ir_unop_pack_int_2x32
:
2211 case ir_unop_unpack_uint_2x32
:
2212 case ir_unop_pack_uint_2x32
:
2213 case ir_unop_unpack_sampler_2x32
:
2214 case ir_unop_pack_sampler_2x32
:
2215 case ir_unop_unpack_image_2x32
:
2216 case ir_unop_pack_image_2x32
:
2217 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, op
[0]);
2220 case ir_binop_ldexp
:
2221 if (ir
->operands
[0]->type
->is_double()) {
2222 emit_asm(ir
, TGSI_OPCODE_DLDEXP
, result_dst
, op
[0], op
[1]);
2223 } else if (ir
->operands
[0]->type
->is_float()) {
2224 emit_asm(ir
, TGSI_OPCODE_LDEXP
, result_dst
, op
[0], op
[1]);
2226 assert(!"Invalid ldexp for non-double opcode in glsl_to_tgsi_visitor::visit()");
2230 case ir_unop_pack_half_2x16
:
2231 emit_asm(ir
, TGSI_OPCODE_PK2H
, result_dst
, op
[0]);
2233 case ir_unop_unpack_half_2x16
:
2234 emit_asm(ir
, TGSI_OPCODE_UP2H
, result_dst
, op
[0]);
2237 case ir_unop_get_buffer_size
: {
2238 ir_constant
*const_offset
= ir
->operands
[0]->as_constant();
2239 int buf_base
= ctx
->st
->has_hw_atomics
2240 ? 0 : ctx
->Const
.Program
[shader
->Stage
].MaxAtomicBuffers
;
2243 buf_base
+ (const_offset
? const_offset
->value
.u
[0] : 0),
2245 if (!const_offset
) {
2246 buffer
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
2247 *buffer
.reladdr
= op
[0];
2248 emit_arl(ir
, sampler_reladdr
, op
[0]);
2250 emit_asm(ir
, TGSI_OPCODE_RESQ
, result_dst
)->resource
= buffer
;
2256 case ir_unop_b2i64
: {
2257 st_src_reg temp
= get_temp(glsl_type::uvec4_type
);
2258 st_dst_reg temp_dst
= st_dst_reg(temp
);
2259 unsigned orig_swz
= op
[0].swizzle
;
2261 * To convert unsigned to 64-bit:
2262 * zero Y channel, copy X channel.
2264 temp_dst
.writemask
= WRITEMASK_Y
;
2265 if (vector_elements
> 1)
2266 temp_dst
.writemask
|= WRITEMASK_W
;
2267 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, st_src_reg_for_int(0));
2268 temp_dst
.writemask
= WRITEMASK_X
;
2269 if (vector_elements
> 1)
2270 temp_dst
.writemask
|= WRITEMASK_Z
;
2271 op
[0].swizzle
= MAKE_SWIZZLE4(GET_SWZ(orig_swz
, 0), GET_SWZ(orig_swz
, 0),
2272 GET_SWZ(orig_swz
, 1), GET_SWZ(orig_swz
, 1));
2273 if (ir
->operation
== ir_unop_u2i64
|| ir
->operation
== ir_unop_u2u64
)
2274 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, op
[0]);
2276 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, op
[0], st_src_reg_for_int(1));
2278 result_src
.type
= GLSL_TYPE_UINT64
;
2279 if (vector_elements
> 2) {
2280 /* Subtle: We rely on the fact that get_temp here returns the next
2281 * TGSI temporary register directly after the temp register used for
2282 * the first two components, so that the result gets picked up
2285 st_src_reg temp
= get_temp(glsl_type::uvec4_type
);
2286 st_dst_reg temp_dst
= st_dst_reg(temp
);
2287 temp_dst
.writemask
= WRITEMASK_Y
;
2288 if (vector_elements
> 3)
2289 temp_dst
.writemask
|= WRITEMASK_W
;
2290 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, st_src_reg_for_int(0));
2292 temp_dst
.writemask
= WRITEMASK_X
;
2293 if (vector_elements
> 3)
2294 temp_dst
.writemask
|= WRITEMASK_Z
;
2295 op
[0].swizzle
= MAKE_SWIZZLE4(GET_SWZ(orig_swz
, 2),
2296 GET_SWZ(orig_swz
, 2),
2297 GET_SWZ(orig_swz
, 3),
2298 GET_SWZ(orig_swz
, 3));
2299 if (ir
->operation
== ir_unop_u2i64
|| ir
->operation
== ir_unop_u2u64
)
2300 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, op
[0]);
2302 emit_asm(ir
, TGSI_OPCODE_AND
, temp_dst
, op
[0],
2303 st_src_reg_for_int(1));
2310 case ir_unop_i642u
: {
2311 st_src_reg temp
= get_temp(glsl_type::uvec4_type
);
2312 st_dst_reg temp_dst
= st_dst_reg(temp
);
2313 unsigned orig_swz
= op
[0].swizzle
;
2314 unsigned orig_idx
= op
[0].index
;
2316 temp_dst
.writemask
= WRITEMASK_X
;
2318 for (el
= 0; el
< vector_elements
; el
++) {
2319 unsigned swz
= GET_SWZ(orig_swz
, el
);
2321 op
[0].swizzle
= MAKE_SWIZZLE4(SWIZZLE_Z
, SWIZZLE_Z
,
2322 SWIZZLE_Z
, SWIZZLE_Z
);
2324 op
[0].swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
,
2325 SWIZZLE_X
, SWIZZLE_X
);
2327 op
[0].index
= orig_idx
+ 1;
2328 op
[0].type
= GLSL_TYPE_UINT
;
2329 temp_dst
.writemask
= WRITEMASK_X
<< el
;
2330 emit_asm(ir
, TGSI_OPCODE_MOV
, temp_dst
, op
[0]);
2333 if (ir
->operation
== ir_unop_u642u
|| ir
->operation
== ir_unop_i642u
)
2334 result_src
.type
= GLSL_TYPE_UINT
;
2336 result_src
.type
= GLSL_TYPE_INT
;
2340 emit_asm(ir
, TGSI_OPCODE_U64SNE
, result_dst
, op
[0],
2341 st_src_reg_for_int64(0));
2344 emit_asm(ir
, TGSI_OPCODE_I642F
, result_dst
, op
[0]);
2347 emit_asm(ir
, TGSI_OPCODE_U642F
, result_dst
, op
[0]);
2350 emit_asm(ir
, TGSI_OPCODE_I642D
, result_dst
, op
[0]);
2353 emit_asm(ir
, TGSI_OPCODE_U642D
, result_dst
, op
[0]);
2356 emit_asm(ir
, TGSI_OPCODE_I2I64
, result_dst
, op
[0]);
2359 emit_asm(ir
, TGSI_OPCODE_F2I64
, result_dst
, op
[0]);
2362 emit_asm(ir
, TGSI_OPCODE_D2I64
, result_dst
, op
[0]);
2365 emit_asm(ir
, TGSI_OPCODE_I2I64
, result_dst
, op
[0]);
2368 emit_asm(ir
, TGSI_OPCODE_F2U64
, result_dst
, op
[0]);
2371 emit_asm(ir
, TGSI_OPCODE_D2U64
, result_dst
, op
[0]);
2373 /* these might be needed */
2374 case ir_unop_pack_snorm_2x16
:
2375 case ir_unop_pack_unorm_2x16
:
2376 case ir_unop_pack_snorm_4x8
:
2377 case ir_unop_pack_unorm_4x8
:
2379 case ir_unop_unpack_snorm_2x16
:
2380 case ir_unop_unpack_unorm_2x16
:
2381 case ir_unop_unpack_snorm_4x8
:
2382 case ir_unop_unpack_unorm_4x8
:
2384 case ir_quadop_vector
:
2385 case ir_binop_vector_extract
:
2386 case ir_triop_vector_insert
:
2387 case ir_binop_carry
:
2388 case ir_binop_borrow
:
2389 case ir_unop_ssbo_unsized_array_length
:
2390 /* This operation is not supported, or should have already been handled.
2392 assert(!"Invalid ir opcode in glsl_to_tgsi_visitor::visit()");
2396 this->result
= result_src
;
2401 glsl_to_tgsi_visitor::visit(ir_swizzle
*ir
)
2407 /* Note that this is only swizzles in expressions, not those on the left
2408 * hand side of an assignment, which do write masking. See ir_assignment
2412 ir
->val
->accept(this);
2414 assert(src
.file
!= PROGRAM_UNDEFINED
);
2415 assert(ir
->type
->vector_elements
> 0);
2417 for (i
= 0; i
< 4; i
++) {
2418 if (i
< ir
->type
->vector_elements
) {
2421 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.x
);
2424 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.y
);
2427 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.z
);
2430 swizzle
[i
] = GET_SWZ(src
.swizzle
, ir
->mask
.w
);
2434 /* If the type is smaller than a vec4, replicate the last
2437 swizzle
[i
] = swizzle
[ir
->type
->vector_elements
- 1];
2441 src
.swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
2446 /* Test if the variable is an array. Note that geometry and
2447 * tessellation shader inputs are outputs are always arrays (except
2448 * for patch inputs), so only the array element type is considered.
2451 is_inout_array(unsigned stage
, ir_variable
*var
, bool *remove_array
)
2453 const glsl_type
*type
= var
->type
;
2455 *remove_array
= false;
2457 if ((stage
== MESA_SHADER_VERTEX
&& var
->data
.mode
== ir_var_shader_in
) ||
2458 (stage
== MESA_SHADER_FRAGMENT
&& var
->data
.mode
== ir_var_shader_out
))
2461 if (((stage
== MESA_SHADER_GEOMETRY
&& var
->data
.mode
== ir_var_shader_in
) ||
2462 (stage
== MESA_SHADER_TESS_EVAL
&& var
->data
.mode
== ir_var_shader_in
) ||
2463 stage
== MESA_SHADER_TESS_CTRL
) &&
2465 if (!var
->type
->is_array())
2466 return false; /* a system value probably */
2468 type
= var
->type
->fields
.array
;
2469 *remove_array
= true;
2472 return type
->is_array() || type
->is_matrix();
2476 st_translate_interp_loc(ir_variable
*var
)
2478 if (var
->data
.centroid
)
2479 return TGSI_INTERPOLATE_LOC_CENTROID
;
2480 else if (var
->data
.sample
)
2481 return TGSI_INTERPOLATE_LOC_SAMPLE
;
2483 return TGSI_INTERPOLATE_LOC_CENTER
;
2487 glsl_to_tgsi_visitor::visit(ir_dereference_variable
*ir
)
2489 variable_storage
*entry
;
2490 ir_variable
*var
= ir
->var
;
2493 if (handle_bound_deref(ir
->as_dereference()))
2496 entry
= find_variable_storage(ir
->var
);
2499 switch (var
->data
.mode
) {
2500 case ir_var_uniform
:
2501 entry
= new(mem_ctx
) variable_storage(var
, PROGRAM_UNIFORM
,
2502 var
->data
.param_index
);
2503 _mesa_hash_table_insert(this->variables
, var
, entry
);
2505 case ir_var_shader_in
: {
2506 /* The linker assigns locations for varyings and attributes,
2507 * including deprecated builtins (like gl_Color), user-assign
2508 * generic attributes (glBindVertexLocation), and
2509 * user-defined varyings.
2511 assert(var
->data
.location
!= -1);
2513 const glsl_type
*type_without_array
= var
->type
->without_array();
2514 struct inout_decl
*decl
= &inputs
[num_inputs
];
2515 unsigned component
= var
->data
.location_frac
;
2516 unsigned num_components
;
2519 if (type_without_array
->is_64bit())
2520 component
= component
/ 2;
2521 if (type_without_array
->vector_elements
)
2522 num_components
= type_without_array
->vector_elements
;
2526 decl
->mesa_index
= var
->data
.location
;
2527 decl
->interp
= (glsl_interp_mode
) var
->data
.interpolation
;
2528 decl
->interp_loc
= st_translate_interp_loc(var
);
2529 decl
->base_type
= type_without_array
->base_type
;
2530 decl
->usage_mask
= u_bit_consecutive(component
, num_components
);
2532 if (is_inout_array(shader
->Stage
, var
, &remove_array
)) {
2533 decl
->array_id
= num_input_arrays
+ 1;
2540 decl
->size
= type_size(var
->type
->fields
.array
);
2542 decl
->size
= type_size(var
->type
);
2544 entry
= new(mem_ctx
) variable_storage(var
,
2548 entry
->component
= component
;
2550 _mesa_hash_table_insert(this->variables
, var
, entry
);
2554 case ir_var_shader_out
: {
2555 assert(var
->data
.location
!= -1);
2557 const glsl_type
*type_without_array
= var
->type
->without_array();
2558 struct inout_decl
*decl
= &outputs
[num_outputs
];
2559 unsigned component
= var
->data
.location_frac
;
2560 unsigned num_components
;
2563 decl
->invariant
= var
->data
.invariant
;
2565 if (type_without_array
->is_64bit())
2566 component
= component
/ 2;
2567 if (type_without_array
->vector_elements
)
2568 num_components
= type_without_array
->vector_elements
;
2572 decl
->mesa_index
= var
->data
.location
+ FRAG_RESULT_MAX
* var
->data
.index
;
2573 decl
->base_type
= type_without_array
->base_type
;
2574 decl
->usage_mask
= u_bit_consecutive(component
, num_components
);
2575 if (var
->data
.stream
& (1u << 31)) {
2576 decl
->gs_out_streams
= var
->data
.stream
& ~(1u << 31);
2578 assert(var
->data
.stream
< 4);
2579 decl
->gs_out_streams
= 0;
2580 for (unsigned i
= 0; i
< num_components
; ++i
)
2581 decl
->gs_out_streams
|= var
->data
.stream
<< (2 * (component
+ i
));
2584 if (is_inout_array(shader
->Stage
, var
, &remove_array
)) {
2585 decl
->array_id
= num_output_arrays
+ 1;
2586 num_output_arrays
++;
2592 decl
->size
= type_size(var
->type
->fields
.array
);
2594 decl
->size
= type_size(var
->type
);
2596 if (var
->data
.fb_fetch_output
) {
2597 st_dst_reg dst
= st_dst_reg(get_temp(var
->type
));
2598 st_src_reg src
= st_src_reg(PROGRAM_OUTPUT
, decl
->mesa_index
,
2599 var
->type
, component
, decl
->array_id
);
2600 emit_asm(NULL
, TGSI_OPCODE_FBFETCH
, dst
, src
);
2601 entry
= new(mem_ctx
) variable_storage(var
, dst
.file
, dst
.index
,
2604 entry
= new(mem_ctx
) variable_storage(var
,
2609 entry
->component
= component
;
2611 _mesa_hash_table_insert(this->variables
, var
, entry
);
2615 case ir_var_system_value
:
2616 entry
= new(mem_ctx
) variable_storage(var
,
2617 PROGRAM_SYSTEM_VALUE
,
2618 var
->data
.location
);
2621 case ir_var_temporary
:
2622 st_src_reg src
= get_temp(var
->type
);
2624 entry
= new(mem_ctx
) variable_storage(var
, src
.file
, src
.index
,
2626 _mesa_hash_table_insert(this->variables
, var
, entry
);
2632 printf("Failed to make storage for %s\n", var
->name
);
2637 this->result
= st_src_reg(entry
->file
, entry
->index
, var
->type
,
2638 entry
->component
, entry
->array_id
);
2639 if (this->shader
->Stage
== MESA_SHADER_VERTEX
&&
2640 var
->data
.mode
== ir_var_shader_in
&&
2641 var
->type
->without_array()->is_double())
2642 this->result
.is_double_vertex_input
= true;
2643 if (!native_integers
)
2644 this->result
.type
= GLSL_TYPE_FLOAT
;
2648 shrink_array_declarations(struct inout_decl
*decls
, unsigned count
,
2649 GLbitfield64
* usage_mask
,
2650 GLbitfield64 double_usage_mask
,
2651 GLbitfield
* patch_usage_mask
)
2656 /* Fix array declarations by removing unused array elements at both ends
2657 * of the arrays. For example, mat4[3] where only mat[1] is used.
2659 for (i
= 0; i
< count
; i
++) {
2660 struct inout_decl
*decl
= &decls
[i
];
2661 if (!decl
->array_id
)
2664 /* Shrink the beginning. */
2665 for (j
= 0; j
< (int)decl
->size
; j
++) {
2666 if (decl
->mesa_index
>= VARYING_SLOT_PATCH0
) {
2667 if (*patch_usage_mask
&
2668 BITFIELD64_BIT(decl
->mesa_index
- VARYING_SLOT_PATCH0
+ j
))
2672 if (*usage_mask
& BITFIELD64_BIT(decl
->mesa_index
+j
))
2674 if (double_usage_mask
& BITFIELD64_BIT(decl
->mesa_index
+j
-1))
2683 /* Shrink the end. */
2684 for (j
= decl
->size
-1; j
>= 0; j
--) {
2685 if (decl
->mesa_index
>= VARYING_SLOT_PATCH0
) {
2686 if (*patch_usage_mask
&
2687 BITFIELD64_BIT(decl
->mesa_index
- VARYING_SLOT_PATCH0
+ j
))
2691 if (*usage_mask
& BITFIELD64_BIT(decl
->mesa_index
+j
))
2693 if (double_usage_mask
& BITFIELD64_BIT(decl
->mesa_index
+j
-1))
2700 /* When not all entries of an array are accessed, we mark them as used
2701 * here anyway, to ensure that the input/output mapping logic doesn't get
2704 * TODO This happens when an array isn't used via indirect access, which
2705 * some game ports do (at least eON-based). There is an optimization
2706 * opportunity here by replacing the array declaration with non-array
2707 * declarations of those slots that are actually used.
2709 for (j
= 1; j
< (int)decl
->size
; ++j
) {
2710 if (decl
->mesa_index
>= VARYING_SLOT_PATCH0
)
2711 *patch_usage_mask
|= BITFIELD64_BIT(decl
->mesa_index
- VARYING_SLOT_PATCH0
+ j
);
2713 *usage_mask
|= BITFIELD64_BIT(decl
->mesa_index
+ j
);
2720 mark_array_io(struct inout_decl
*decls
, unsigned count
,
2721 GLbitfield64
* usage_mask
,
2722 GLbitfield64 double_usage_mask
,
2723 GLbitfield
* patch_usage_mask
)
2728 /* Fix array declarations by removing unused array elements at both ends
2729 * of the arrays. For example, mat4[3] where only mat[1] is used.
2731 for (i
= 0; i
< count
; i
++) {
2732 struct inout_decl
*decl
= &decls
[i
];
2733 if (!decl
->array_id
)
2736 /* When not all entries of an array are accessed, we mark them as used
2737 * here anyway, to ensure that the input/output mapping logic doesn't get
2740 * TODO This happens when an array isn't used via indirect access, which
2741 * some game ports do (at least eON-based). There is an optimization
2742 * opportunity here by replacing the array declaration with non-array
2743 * declarations of those slots that are actually used.
2745 for (j
= 0; j
< (int)decl
->size
; ++j
) {
2746 if (decl
->mesa_index
>= VARYING_SLOT_PATCH0
)
2747 *patch_usage_mask
|= BITFIELD64_BIT(decl
->mesa_index
- VARYING_SLOT_PATCH0
+ j
);
2749 *usage_mask
|= BITFIELD64_BIT(decl
->mesa_index
+ j
);
2755 glsl_to_tgsi_visitor::visit(ir_dereference_array
*ir
)
2760 ir_variable
*var
= ir
->variable_referenced();
2762 if (handle_bound_deref(ir
->as_dereference()))
2765 /* We only need the logic provided by st_glsl_storage_type_size()
2766 * for arrays of structs. Indirect sampler and image indexing is handled
2769 int element_size
= ir
->type
->without_array()->is_struct() ?
2770 st_glsl_storage_type_size(ir
->type
, var
->data
.bindless
) :
2771 type_size(ir
->type
);
2773 index
= ir
->array_index
->constant_expression_value(ralloc_parent(ir
));
2775 ir
->array
->accept(this);
2778 if (!src
.has_index2
) {
2779 switch (this->prog
->Target
) {
2780 case GL_TESS_CONTROL_PROGRAM_NV
:
2781 is_2D
= (src
.file
== PROGRAM_INPUT
|| src
.file
== PROGRAM_OUTPUT
) &&
2782 !ir
->variable_referenced()->data
.patch
;
2784 case GL_TESS_EVALUATION_PROGRAM_NV
:
2785 is_2D
= src
.file
== PROGRAM_INPUT
&&
2786 !ir
->variable_referenced()->data
.patch
;
2788 case GL_GEOMETRY_PROGRAM_NV
:
2789 is_2D
= src
.file
== PROGRAM_INPUT
;
2799 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
&&
2800 src
.file
== PROGRAM_INPUT
)
2801 element_size
= attrib_type_size(ir
->type
, true);
2803 src
.index2D
= index
->value
.i
[0];
2804 src
.has_index2
= true;
2806 src
.index
+= index
->value
.i
[0] * element_size
;
2808 /* Variable index array dereference. It eats the "vec4" of the
2809 * base of the array and an index that offsets the TGSI register
2812 ir
->array_index
->accept(this);
2814 st_src_reg index_reg
;
2816 if (element_size
== 1) {
2817 index_reg
= this->result
;
2819 index_reg
= get_temp(native_integers
?
2820 glsl_type::int_type
: glsl_type::float_type
);
2822 emit_asm(ir
, TGSI_OPCODE_MUL
, st_dst_reg(index_reg
),
2823 this->result
, st_src_reg_for_type(index_reg
.type
, element_size
));
2826 /* If there was already a relative address register involved, add the
2827 * new and the old together to get the new offset.
2829 if (!is_2D
&& src
.reladdr
!= NULL
) {
2830 st_src_reg accum_reg
= get_temp(native_integers
?
2831 glsl_type::int_type
: glsl_type::float_type
);
2833 emit_asm(ir
, TGSI_OPCODE_ADD
, st_dst_reg(accum_reg
),
2834 index_reg
, *src
.reladdr
);
2836 index_reg
= accum_reg
;
2840 src
.reladdr2
= ralloc(mem_ctx
, st_src_reg
);
2841 *src
.reladdr2
= index_reg
;
2843 src
.has_index2
= true;
2845 src
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
2846 *src
.reladdr
= index_reg
;
2850 /* Change the register type to the element type of the array. */
2851 src
.type
= ir
->type
->base_type
;
2857 glsl_to_tgsi_visitor::visit(ir_dereference_record
*ir
)
2860 const glsl_type
*struct_type
= ir
->record
->type
;
2861 ir_variable
*var
= ir
->record
->variable_referenced();
2864 if (handle_bound_deref(ir
->as_dereference()))
2867 ir
->record
->accept(this);
2869 assert(ir
->field_idx
>= 0);
2871 for (i
= 0; i
< struct_type
->length
; i
++) {
2872 if (i
== (unsigned) ir
->field_idx
)
2874 const glsl_type
*member_type
= struct_type
->fields
.structure
[i
].type
;
2875 offset
+= st_glsl_storage_type_size(member_type
, var
->data
.bindless
);
2878 /* If the type is smaller than a vec4, replicate the last channel out. */
2879 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
2880 this->result
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
2882 this->result
.swizzle
= SWIZZLE_NOOP
;
2884 this->result
.index
+= offset
;
2885 this->result
.type
= ir
->type
->base_type
;
2889 * We want to be careful in assignment setup to hit the actual storage
2890 * instead of potentially using a temporary like we might with the
2891 * ir_dereference handler.
2894 get_assignment_lhs(ir_dereference
*ir
, glsl_to_tgsi_visitor
*v
, int *component
)
2896 /* The LHS must be a dereference. If the LHS is a variable indexed array
2897 * access of a vector, it must be separated into a series conditional moves
2898 * before reaching this point (see ir_vec_index_to_cond_assign).
2900 assert(ir
->as_dereference());
2901 ir_dereference_array
*deref_array
= ir
->as_dereference_array();
2903 assert(!deref_array
->array
->type
->is_vector());
2906 /* Use the rvalue deref handler for the most part. We write swizzles using
2907 * the writemask, but we do extract the base component for enhanced layouts
2908 * from the source swizzle.
2911 *component
= GET_SWZ(v
->result
.swizzle
, 0);
2912 return st_dst_reg(v
->result
);
2916 * Process the condition of a conditional assignment
2918 * Examines the condition of a conditional assignment to generate the optimal
2919 * first operand of a \c CMP instruction. If the condition is a relational
2920 * operator with 0 (e.g., \c ir_binop_less), the value being compared will be
2921 * used as the source for the \c CMP instruction. Otherwise the comparison
2922 * is processed to a boolean result, and the boolean result is used as the
2923 * operand to the CMP instruction.
2926 glsl_to_tgsi_visitor::process_move_condition(ir_rvalue
*ir
)
2928 ir_rvalue
*src_ir
= ir
;
2930 bool switch_order
= false;
2932 ir_expression
*const expr
= ir
->as_expression();
2934 if (native_integers
) {
2935 if ((expr
!= NULL
) && (expr
->num_operands
== 2)) {
2936 enum glsl_base_type type
= expr
->operands
[0]->type
->base_type
;
2937 if (type
== GLSL_TYPE_INT
|| type
== GLSL_TYPE_UINT
||
2938 type
== GLSL_TYPE_BOOL
) {
2939 if (expr
->operation
== ir_binop_equal
) {
2940 if (expr
->operands
[0]->is_zero()) {
2941 src_ir
= expr
->operands
[1];
2942 switch_order
= true;
2944 else if (expr
->operands
[1]->is_zero()) {
2945 src_ir
= expr
->operands
[0];
2946 switch_order
= true;
2949 else if (expr
->operation
== ir_binop_nequal
) {
2950 if (expr
->operands
[0]->is_zero()) {
2951 src_ir
= expr
->operands
[1];
2953 else if (expr
->operands
[1]->is_zero()) {
2954 src_ir
= expr
->operands
[0];
2960 src_ir
->accept(this);
2961 return switch_order
;
2964 if ((expr
!= NULL
) && (expr
->num_operands
== 2)) {
2965 bool zero_on_left
= false;
2967 if (expr
->operands
[0]->is_zero()) {
2968 src_ir
= expr
->operands
[1];
2969 zero_on_left
= true;
2970 } else if (expr
->operands
[1]->is_zero()) {
2971 src_ir
= expr
->operands
[0];
2972 zero_on_left
= false;
2976 * (a < 0) T F F ( a < 0) T F F
2977 * (0 < a) F F T (-a < 0) F F T
2978 * (a >= 0) F T T ( a < 0) T F F (swap order of other operands)
2979 * (0 >= a) T T F (-a < 0) F F T (swap order of other operands)
2981 * Note that exchanging the order of 0 and 'a' in the comparison simply
2982 * means that the value of 'a' should be negated.
2985 switch (expr
->operation
) {
2987 switch_order
= false;
2988 negate
= zero_on_left
;
2991 case ir_binop_gequal
:
2992 switch_order
= true;
2993 negate
= zero_on_left
;
2997 /* This isn't the right kind of comparison afterall, so make sure
2998 * the whole condition is visited.
3006 src_ir
->accept(this);
3008 /* We use the TGSI_OPCODE_CMP (a < 0 ? b : c) for conditional moves, and the
3009 * condition we produced is 0.0 or 1.0. By flipping the sign, we can
3010 * choose which value TGSI_OPCODE_CMP produces without an extra instruction
3011 * computing the condition.
3014 this->result
.negate
= ~this->result
.negate
;
3016 return switch_order
;
3020 glsl_to_tgsi_visitor::emit_block_mov(ir_assignment
*ir
, const struct glsl_type
*type
,
3021 st_dst_reg
*l
, st_src_reg
*r
,
3022 st_src_reg
*cond
, bool cond_swap
)
3024 if (type
->is_struct()) {
3025 for (unsigned int i
= 0; i
< type
->length
; i
++) {
3026 emit_block_mov(ir
, type
->fields
.structure
[i
].type
, l
, r
,
3032 if (type
->is_array()) {
3033 for (unsigned int i
= 0; i
< type
->length
; i
++) {
3034 emit_block_mov(ir
, type
->fields
.array
, l
, r
, cond
, cond_swap
);
3039 if (type
->is_matrix()) {
3040 const struct glsl_type
*vec_type
;
3042 vec_type
= glsl_type::get_instance(type
->is_double()
3043 ? GLSL_TYPE_DOUBLE
: GLSL_TYPE_FLOAT
,
3044 type
->vector_elements
, 1);
3046 for (int i
= 0; i
< type
->matrix_columns
; i
++) {
3047 emit_block_mov(ir
, vec_type
, l
, r
, cond
, cond_swap
);
3052 assert(type
->is_scalar() || type
->is_vector());
3054 l
->type
= type
->base_type
;
3055 r
->type
= type
->base_type
;
3057 st_src_reg l_src
= st_src_reg(*l
);
3059 if (l_src
.file
== PROGRAM_OUTPUT
&&
3060 this->prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
&&
3061 (l_src
.index
== FRAG_RESULT_DEPTH
||
3062 l_src
.index
== FRAG_RESULT_STENCIL
)) {
3063 /* This is a special case because the source swizzles will be shifted
3064 * later to account for the difference between GLSL (where they're
3065 * plain floats) and TGSI (where they're Z and Y components). */
3066 l_src
.swizzle
= SWIZZLE_XXXX
;
3069 if (native_integers
) {
3070 emit_asm(ir
, TGSI_OPCODE_UCMP
, *l
, *cond
,
3071 cond_swap
? l_src
: *r
,
3072 cond_swap
? *r
: l_src
);
3074 emit_asm(ir
, TGSI_OPCODE_CMP
, *l
, *cond
,
3075 cond_swap
? l_src
: *r
,
3076 cond_swap
? *r
: l_src
);
3079 emit_asm(ir
, TGSI_OPCODE_MOV
, *l
, *r
);
3083 if (type
->is_dual_slot()) {
3085 if (r
->is_double_vertex_input
== false)
3091 glsl_to_tgsi_visitor::visit(ir_assignment
*ir
)
3097 /* all generated instructions need to be flaged as precise */
3098 this->precise
= is_precise(ir
->lhs
->variable_referenced());
3099 ir
->rhs
->accept(this);
3102 l
= get_assignment_lhs(ir
->lhs
, this, &dst_component
);
3106 int first_enabled_chan
= 0;
3108 ir_variable
*variable
= ir
->lhs
->variable_referenced();
3110 if (shader
->Stage
== MESA_SHADER_FRAGMENT
&&
3111 variable
->data
.mode
== ir_var_shader_out
&&
3112 (variable
->data
.location
== FRAG_RESULT_DEPTH
||
3113 variable
->data
.location
== FRAG_RESULT_STENCIL
)) {
3114 assert(ir
->lhs
->type
->is_scalar());
3115 assert(ir
->write_mask
== WRITEMASK_X
);
3117 if (variable
->data
.location
== FRAG_RESULT_DEPTH
)
3118 l
.writemask
= WRITEMASK_Z
;
3120 assert(variable
->data
.location
== FRAG_RESULT_STENCIL
);
3121 l
.writemask
= WRITEMASK_Y
;
3123 } else if (ir
->write_mask
== 0) {
3124 assert(!ir
->lhs
->type
->is_scalar() && !ir
->lhs
->type
->is_vector());
3126 unsigned num_elements
=
3127 ir
->lhs
->type
->without_array()->vector_elements
;
3130 l
.writemask
= u_bit_consecutive(0, num_elements
);
3132 /* The type is a struct or an array of (array of) structs. */
3133 l
.writemask
= WRITEMASK_XYZW
;
3136 l
.writemask
= ir
->write_mask
;
3139 for (int i
= 0; i
< 4; i
++) {
3140 if (l
.writemask
& (1 << i
)) {
3141 first_enabled_chan
= GET_SWZ(r
.swizzle
, i
);
3146 l
.writemask
= l
.writemask
<< dst_component
;
3148 /* Swizzle a small RHS vector into the channels being written.
3150 * glsl ir treats write_mask as dictating how many channels are
3151 * present on the RHS while TGSI treats write_mask as just
3152 * showing which channels of the vec4 RHS get written.
3154 for (int i
= 0; i
< 4; i
++) {
3155 if (l
.writemask
& (1 << i
))
3156 swizzles
[i
] = GET_SWZ(r
.swizzle
, rhs_chan
++);
3158 swizzles
[i
] = first_enabled_chan
;
3160 r
.swizzle
= MAKE_SWIZZLE4(swizzles
[0], swizzles
[1],
3161 swizzles
[2], swizzles
[3]);
3164 assert(l
.file
!= PROGRAM_UNDEFINED
);
3165 assert(r
.file
!= PROGRAM_UNDEFINED
);
3167 if (ir
->condition
) {
3168 const bool switch_order
= this->process_move_condition(ir
->condition
);
3169 st_src_reg condition
= this->result
;
3171 emit_block_mov(ir
, ir
->lhs
->type
, &l
, &r
, &condition
, switch_order
);
3172 } else if (ir
->rhs
->as_expression() &&
3173 this->instructions
.get_tail() &&
3174 ir
->rhs
== ((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->ir
&&
3175 !((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->is_64bit_expanded
&&
3176 type_size(ir
->lhs
->type
) == 1 &&
3177 l
.writemask
== ((glsl_to_tgsi_instruction
*)this->instructions
.get_tail())->dst
[0].writemask
) {
3178 /* To avoid emitting an extra MOV when assigning an expression to a
3179 * variable, emit the last instruction of the expression again, but
3180 * replace the destination register with the target of the assignment.
3181 * Dead code elimination will remove the original instruction.
3183 glsl_to_tgsi_instruction
*inst
, *new_inst
;
3184 inst
= (glsl_to_tgsi_instruction
*)this->instructions
.get_tail();
3185 new_inst
= emit_asm(ir
, inst
->op
, l
, inst
->src
[0], inst
->src
[1], inst
->src
[2], inst
->src
[3]);
3186 new_inst
->saturate
= inst
->saturate
;
3187 new_inst
->resource
= inst
->resource
;
3188 inst
->dead_mask
= inst
->dst
[0].writemask
;
3190 emit_block_mov(ir
, ir
->rhs
->type
, &l
, &r
, NULL
, false);
3197 glsl_to_tgsi_visitor::visit(ir_constant
*ir
)
3200 GLdouble stack_vals
[4] = { 0 };
3201 gl_constant_value
*values
= (gl_constant_value
*) stack_vals
;
3202 GLenum gl_type
= GL_NONE
;
3203 unsigned int i
, elements
;
3204 static int in_array
= 0;
3205 gl_register_file file
= in_array
? PROGRAM_CONSTANT
: PROGRAM_IMMEDIATE
;
3207 /* Unfortunately, 4 floats is all we can get into
3208 * _mesa_add_typed_unnamed_constant. So, make a temp to store an
3209 * aggregate constant and move each constant value into it. If we
3210 * get lucky, copy propagation will eliminate the extra moves.
3212 if (ir
->type
->is_struct()) {
3213 st_src_reg temp_base
= get_temp(ir
->type
);
3214 st_dst_reg temp
= st_dst_reg(temp_base
);
3216 for (i
= 0; i
< ir
->type
->length
; i
++) {
3217 ir_constant
*const field_value
= ir
->get_record_field(i
);
3218 int size
= type_size(field_value
->type
);
3222 field_value
->accept(this);
3225 for (unsigned j
= 0; j
< (unsigned int)size
; j
++) {
3226 emit_asm(ir
, TGSI_OPCODE_MOV
, temp
, src
);
3232 this->result
= temp_base
;
3236 if (ir
->type
->is_array()) {
3237 st_src_reg temp_base
= get_temp(ir
->type
);
3238 st_dst_reg temp
= st_dst_reg(temp_base
);
3239 int size
= type_size(ir
->type
->fields
.array
);
3244 for (i
= 0; i
< ir
->type
->length
; i
++) {
3245 ir
->const_elements
[i
]->accept(this);
3247 for (int j
= 0; j
< size
; j
++) {
3248 emit_asm(ir
, TGSI_OPCODE_MOV
, temp
, src
);
3254 this->result
= temp_base
;
3259 if (ir
->type
->is_matrix()) {
3260 st_src_reg mat
= get_temp(ir
->type
);
3261 st_dst_reg mat_column
= st_dst_reg(mat
);
3263 for (i
= 0; i
< ir
->type
->matrix_columns
; i
++) {
3264 switch (ir
->type
->base_type
) {
3265 case GLSL_TYPE_FLOAT
:
3266 values
= (gl_constant_value
*)
3267 &ir
->value
.f
[i
* ir
->type
->vector_elements
];
3269 src
= st_src_reg(file
, -1, ir
->type
->base_type
);
3270 src
.index
= add_constant(file
,
3272 ir
->type
->vector_elements
,
3275 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3277 case GLSL_TYPE_DOUBLE
:
3278 values
= (gl_constant_value
*)
3279 &ir
->value
.d
[i
* ir
->type
->vector_elements
];
3280 src
= st_src_reg(file
, -1, ir
->type
->base_type
);
3281 src
.index
= add_constant(file
,
3283 ir
->type
->vector_elements
,
3286 if (ir
->type
->vector_elements
>= 2) {
3287 mat_column
.writemask
= WRITEMASK_XY
;
3288 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
,
3289 SWIZZLE_X
, SWIZZLE_Y
);
3290 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3292 mat_column
.writemask
= WRITEMASK_X
;
3293 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
,
3294 SWIZZLE_X
, SWIZZLE_X
);
3295 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3298 if (ir
->type
->vector_elements
> 2) {
3299 if (ir
->type
->vector_elements
== 4) {
3300 mat_column
.writemask
= WRITEMASK_ZW
;
3301 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
,
3302 SWIZZLE_X
, SWIZZLE_Y
);
3303 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3305 mat_column
.writemask
= WRITEMASK_Z
;
3306 src
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_Y
, SWIZZLE_Y
,
3307 SWIZZLE_Y
, SWIZZLE_Y
);
3308 emit_asm(ir
, TGSI_OPCODE_MOV
, mat_column
, src
);
3309 mat_column
.writemask
= WRITEMASK_XYZW
;
3310 src
.swizzle
= SWIZZLE_XYZW
;
3316 unreachable("Illegal matrix constant type.\n");
3325 elements
= ir
->type
->vector_elements
;
3326 switch (ir
->type
->base_type
) {
3327 case GLSL_TYPE_FLOAT
:
3329 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3330 values
[i
].f
= ir
->value
.f
[i
];
3333 case GLSL_TYPE_DOUBLE
:
3334 gl_type
= GL_DOUBLE
;
3335 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3336 memcpy(&values
[i
* 2], &ir
->value
.d
[i
], sizeof(double));
3339 case GLSL_TYPE_INT64
:
3340 gl_type
= GL_INT64_ARB
;
3341 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3342 memcpy(&values
[i
* 2], &ir
->value
.d
[i
], sizeof(int64_t));
3345 case GLSL_TYPE_UINT64
:
3346 gl_type
= GL_UNSIGNED_INT64_ARB
;
3347 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3348 memcpy(&values
[i
* 2], &ir
->value
.d
[i
], sizeof(uint64_t));
3351 case GLSL_TYPE_UINT
:
3352 gl_type
= native_integers
? GL_UNSIGNED_INT
: GL_FLOAT
;
3353 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3354 if (native_integers
)
3355 values
[i
].u
= ir
->value
.u
[i
];
3357 values
[i
].f
= ir
->value
.u
[i
];
3361 gl_type
= native_integers
? GL_INT
: GL_FLOAT
;
3362 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3363 if (native_integers
)
3364 values
[i
].i
= ir
->value
.i
[i
];
3366 values
[i
].f
= ir
->value
.i
[i
];
3369 case GLSL_TYPE_BOOL
:
3370 gl_type
= native_integers
? GL_BOOL
: GL_FLOAT
;
3371 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
3372 values
[i
].u
= ir
->value
.b
[i
] ? ctx
->Const
.UniformBooleanTrue
: 0;
3375 case GLSL_TYPE_SAMPLER
:
3376 case GLSL_TYPE_IMAGE
:
3377 gl_type
= GL_UNSIGNED_INT
;
3379 values
[0].u
= ir
->value
.u64
[0] & 0xffffffff;
3380 values
[1].u
= ir
->value
.u64
[0] >> 32;
3383 assert(!"Non-float/uint/int/bool/sampler/image constant");
3386 this->result
= st_src_reg(file
, -1, ir
->type
);
3387 this->result
.index
= add_constant(file
,
3391 &this->result
.swizzle
);
3395 glsl_to_tgsi_visitor::visit_atomic_counter_intrinsic(ir_call
*ir
)
3397 exec_node
*param
= ir
->actual_parameters
.get_head();
3398 ir_dereference
*deref
= static_cast<ir_dereference
*>(param
);
3399 ir_variable
*location
= deref
->variable_referenced();
3400 bool has_hw_atomics
= st_context(ctx
)->has_hw_atomics
;
3401 /* Calculate the surface offset */
3403 unsigned array_size
= 0, base
= 0;
3405 st_src_reg resource
;
3407 get_deref_offsets(deref
, &array_size
, &base
, &index
, &offset
, false);
3409 if (has_hw_atomics
) {
3410 variable_storage
*entry
= find_variable_storage(location
);
3411 st_src_reg
buffer(PROGRAM_HW_ATOMIC
, 0, GLSL_TYPE_ATOMIC_UINT
,
3412 location
->data
.binding
);
3415 entry
= new(mem_ctx
) variable_storage(location
, PROGRAM_HW_ATOMIC
,
3417 _mesa_hash_table_insert(this->variables
, location
, entry
);
3419 atomic_info
[num_atomics
].location
= location
->data
.location
;
3420 atomic_info
[num_atomics
].binding
= location
->data
.binding
;
3421 atomic_info
[num_atomics
].size
= location
->type
->arrays_of_arrays_size();
3422 if (atomic_info
[num_atomics
].size
== 0)
3423 atomic_info
[num_atomics
].size
= 1;
3424 atomic_info
[num_atomics
].array_id
= 0;
3428 if (offset
.file
!= PROGRAM_UNDEFINED
) {
3429 if (atomic_info
[entry
->index
].array_id
== 0) {
3430 num_atomic_arrays
++;
3431 atomic_info
[entry
->index
].array_id
= num_atomic_arrays
;
3433 buffer
.array_id
= atomic_info
[entry
->index
].array_id
;
3436 buffer
.index
= index
;
3437 buffer
.index
+= location
->data
.offset
/ ATOMIC_COUNTER_SIZE
;
3438 buffer
.has_index2
= true;
3440 if (offset
.file
!= PROGRAM_UNDEFINED
) {
3441 buffer
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
3442 *buffer
.reladdr
= offset
;
3443 emit_arl(ir
, sampler_reladdr
, offset
);
3445 offset
= st_src_reg_for_int(0);
3449 st_src_reg
buffer(PROGRAM_BUFFER
, location
->data
.binding
,
3450 GLSL_TYPE_ATOMIC_UINT
);
3452 if (offset
.file
!= PROGRAM_UNDEFINED
) {
3453 emit_asm(ir
, TGSI_OPCODE_MUL
, st_dst_reg(offset
),
3454 offset
, st_src_reg_for_int(ATOMIC_COUNTER_SIZE
));
3455 emit_asm(ir
, TGSI_OPCODE_ADD
, st_dst_reg(offset
),
3456 offset
, st_src_reg_for_int(location
->data
.offset
+ index
* ATOMIC_COUNTER_SIZE
));
3458 offset
= st_src_reg_for_int(location
->data
.offset
+ index
* ATOMIC_COUNTER_SIZE
);
3463 ir
->return_deref
->accept(this);
3464 st_dst_reg
dst(this->result
);
3465 dst
.writemask
= WRITEMASK_X
;
3467 glsl_to_tgsi_instruction
*inst
;
3469 if (ir
->callee
->intrinsic_id
== ir_intrinsic_atomic_counter_read
) {
3470 inst
= emit_asm(ir
, TGSI_OPCODE_LOAD
, dst
, offset
);
3471 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_atomic_counter_increment
) {
3472 inst
= emit_asm(ir
, TGSI_OPCODE_ATOMUADD
, dst
, offset
,
3473 st_src_reg_for_int(1));
3474 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_atomic_counter_predecrement
) {
3475 inst
= emit_asm(ir
, TGSI_OPCODE_ATOMUADD
, dst
, offset
,
3476 st_src_reg_for_int(-1));
3477 emit_asm(ir
, TGSI_OPCODE_ADD
, dst
, this->result
, st_src_reg_for_int(-1));
3479 param
= param
->get_next();
3480 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3483 st_src_reg data
= this->result
, data2
= undef_src
;
3484 enum tgsi_opcode opcode
;
3485 switch (ir
->callee
->intrinsic_id
) {
3486 case ir_intrinsic_atomic_counter_add
:
3487 opcode
= TGSI_OPCODE_ATOMUADD
;
3489 case ir_intrinsic_atomic_counter_min
:
3490 opcode
= TGSI_OPCODE_ATOMIMIN
;
3492 case ir_intrinsic_atomic_counter_max
:
3493 opcode
= TGSI_OPCODE_ATOMIMAX
;
3495 case ir_intrinsic_atomic_counter_and
:
3496 opcode
= TGSI_OPCODE_ATOMAND
;
3498 case ir_intrinsic_atomic_counter_or
:
3499 opcode
= TGSI_OPCODE_ATOMOR
;
3501 case ir_intrinsic_atomic_counter_xor
:
3502 opcode
= TGSI_OPCODE_ATOMXOR
;
3504 case ir_intrinsic_atomic_counter_exchange
:
3505 opcode
= TGSI_OPCODE_ATOMXCHG
;
3507 case ir_intrinsic_atomic_counter_comp_swap
: {
3508 opcode
= TGSI_OPCODE_ATOMCAS
;
3509 param
= param
->get_next();
3510 val
= ((ir_instruction
*)param
)->as_rvalue();
3512 data2
= this->result
;
3516 assert(!"Unexpected intrinsic");
3520 inst
= emit_asm(ir
, opcode
, dst
, offset
, data
, data2
);
3523 inst
->resource
= resource
;
3527 glsl_to_tgsi_visitor::visit_ssbo_intrinsic(ir_call
*ir
)
3529 exec_node
*param
= ir
->actual_parameters
.get_head();
3531 ir_rvalue
*block
= ((ir_instruction
*)param
)->as_rvalue();
3533 param
= param
->get_next();
3534 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
3536 ir_constant
*const_block
= block
->as_constant();
3537 int buf_base
= st_context(ctx
)->has_hw_atomics
3538 ? 0 : ctx
->Const
.Program
[shader
->Stage
].MaxAtomicBuffers
;
3541 buf_base
+ (const_block
? const_block
->value
.u
[0] : 0),
3545 block
->accept(this);
3546 buffer
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
3547 *buffer
.reladdr
= this->result
;
3548 emit_arl(ir
, sampler_reladdr
, this->result
);
3551 /* Calculate the surface offset */
3552 offset
->accept(this);
3553 st_src_reg off
= this->result
;
3555 st_dst_reg dst
= undef_dst
;
3556 if (ir
->return_deref
) {
3557 ir
->return_deref
->accept(this);
3558 dst
= st_dst_reg(this->result
);
3559 dst
.writemask
= (1 << ir
->return_deref
->type
->vector_elements
) - 1;
3562 glsl_to_tgsi_instruction
*inst
;
3564 if (ir
->callee
->intrinsic_id
== ir_intrinsic_ssbo_load
) {
3565 inst
= emit_asm(ir
, TGSI_OPCODE_LOAD
, dst
, off
);
3566 if (dst
.type
== GLSL_TYPE_BOOL
)
3567 emit_asm(ir
, TGSI_OPCODE_USNE
, dst
, st_src_reg(dst
),
3568 st_src_reg_for_int(0));
3569 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_ssbo_store
) {
3570 param
= param
->get_next();
3571 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3574 param
= param
->get_next();
3575 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
3577 dst
.writemask
= write_mask
->value
.u
[0];
3579 dst
.type
= this->result
.type
;
3580 inst
= emit_asm(ir
, TGSI_OPCODE_STORE
, dst
, off
, this->result
);
3582 param
= param
->get_next();
3583 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3586 st_src_reg data
= this->result
, data2
= undef_src
;
3587 enum tgsi_opcode opcode
;
3588 switch (ir
->callee
->intrinsic_id
) {
3589 case ir_intrinsic_ssbo_atomic_add
:
3590 opcode
= TGSI_OPCODE_ATOMUADD
;
3592 case ir_intrinsic_ssbo_atomic_min
:
3593 opcode
= TGSI_OPCODE_ATOMIMIN
;
3595 case ir_intrinsic_ssbo_atomic_max
:
3596 opcode
= TGSI_OPCODE_ATOMIMAX
;
3598 case ir_intrinsic_ssbo_atomic_and
:
3599 opcode
= TGSI_OPCODE_ATOMAND
;
3601 case ir_intrinsic_ssbo_atomic_or
:
3602 opcode
= TGSI_OPCODE_ATOMOR
;
3604 case ir_intrinsic_ssbo_atomic_xor
:
3605 opcode
= TGSI_OPCODE_ATOMXOR
;
3607 case ir_intrinsic_ssbo_atomic_exchange
:
3608 opcode
= TGSI_OPCODE_ATOMXCHG
;
3610 case ir_intrinsic_ssbo_atomic_comp_swap
:
3611 opcode
= TGSI_OPCODE_ATOMCAS
;
3612 param
= param
->get_next();
3613 val
= ((ir_instruction
*)param
)->as_rvalue();
3615 data2
= this->result
;
3618 assert(!"Unexpected intrinsic");
3622 inst
= emit_asm(ir
, opcode
, dst
, off
, data
, data2
);
3625 param
= param
->get_next();
3626 ir_constant
*access
= NULL
;
3627 if (!param
->is_tail_sentinel()) {
3628 access
= ((ir_instruction
*)param
)->as_constant();
3632 add_buffer_to_load_and_stores(inst
, &buffer
, &this->instructions
, access
);
3636 glsl_to_tgsi_visitor::visit_membar_intrinsic(ir_call
*ir
)
3638 switch (ir
->callee
->intrinsic_id
) {
3639 case ir_intrinsic_memory_barrier
:
3640 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3641 st_src_reg_for_int(TGSI_MEMBAR_SHADER_BUFFER
|
3642 TGSI_MEMBAR_ATOMIC_BUFFER
|
3643 TGSI_MEMBAR_SHADER_IMAGE
|
3644 TGSI_MEMBAR_SHARED
));
3646 case ir_intrinsic_memory_barrier_atomic_counter
:
3647 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3648 st_src_reg_for_int(TGSI_MEMBAR_ATOMIC_BUFFER
));
3650 case ir_intrinsic_memory_barrier_buffer
:
3651 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3652 st_src_reg_for_int(TGSI_MEMBAR_SHADER_BUFFER
));
3654 case ir_intrinsic_memory_barrier_image
:
3655 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3656 st_src_reg_for_int(TGSI_MEMBAR_SHADER_IMAGE
));
3658 case ir_intrinsic_memory_barrier_shared
:
3659 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3660 st_src_reg_for_int(TGSI_MEMBAR_SHARED
));
3662 case ir_intrinsic_group_memory_barrier
:
3663 emit_asm(ir
, TGSI_OPCODE_MEMBAR
, undef_dst
,
3664 st_src_reg_for_int(TGSI_MEMBAR_SHADER_BUFFER
|
3665 TGSI_MEMBAR_ATOMIC_BUFFER
|
3666 TGSI_MEMBAR_SHADER_IMAGE
|
3667 TGSI_MEMBAR_SHARED
|
3668 TGSI_MEMBAR_THREAD_GROUP
));
3671 assert(!"Unexpected memory barrier intrinsic");
3676 glsl_to_tgsi_visitor::visit_shared_intrinsic(ir_call
*ir
)
3678 exec_node
*param
= ir
->actual_parameters
.get_head();
3680 ir_rvalue
*offset
= ((ir_instruction
*)param
)->as_rvalue();
3682 st_src_reg
buffer(PROGRAM_MEMORY
, 0, GLSL_TYPE_UINT
);
3684 /* Calculate the surface offset */
3685 offset
->accept(this);
3686 st_src_reg off
= this->result
;
3688 st_dst_reg dst
= undef_dst
;
3689 if (ir
->return_deref
) {
3690 ir
->return_deref
->accept(this);
3691 dst
= st_dst_reg(this->result
);
3692 dst
.writemask
= (1 << ir
->return_deref
->type
->vector_elements
) - 1;
3695 glsl_to_tgsi_instruction
*inst
;
3697 if (ir
->callee
->intrinsic_id
== ir_intrinsic_shared_load
) {
3698 inst
= emit_asm(ir
, TGSI_OPCODE_LOAD
, dst
, off
);
3699 inst
->resource
= buffer
;
3700 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_shared_store
) {
3701 param
= param
->get_next();
3702 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3705 param
= param
->get_next();
3706 ir_constant
*write_mask
= ((ir_instruction
*)param
)->as_constant();
3708 dst
.writemask
= write_mask
->value
.u
[0];
3710 dst
.type
= this->result
.type
;
3711 inst
= emit_asm(ir
, TGSI_OPCODE_STORE
, dst
, off
, this->result
);
3712 inst
->resource
= buffer
;
3714 param
= param
->get_next();
3715 ir_rvalue
*val
= ((ir_instruction
*)param
)->as_rvalue();
3718 st_src_reg data
= this->result
, data2
= undef_src
;
3719 enum tgsi_opcode opcode
;
3720 switch (ir
->callee
->intrinsic_id
) {
3721 case ir_intrinsic_shared_atomic_add
:
3722 opcode
= TGSI_OPCODE_ATOMUADD
;
3724 case ir_intrinsic_shared_atomic_min
:
3725 opcode
= TGSI_OPCODE_ATOMIMIN
;
3727 case ir_intrinsic_shared_atomic_max
:
3728 opcode
= TGSI_OPCODE_ATOMIMAX
;
3730 case ir_intrinsic_shared_atomic_and
:
3731 opcode
= TGSI_OPCODE_ATOMAND
;
3733 case ir_intrinsic_shared_atomic_or
:
3734 opcode
= TGSI_OPCODE_ATOMOR
;
3736 case ir_intrinsic_shared_atomic_xor
:
3737 opcode
= TGSI_OPCODE_ATOMXOR
;
3739 case ir_intrinsic_shared_atomic_exchange
:
3740 opcode
= TGSI_OPCODE_ATOMXCHG
;
3742 case ir_intrinsic_shared_atomic_comp_swap
:
3743 opcode
= TGSI_OPCODE_ATOMCAS
;
3744 param
= param
->get_next();
3745 val
= ((ir_instruction
*)param
)->as_rvalue();
3747 data2
= this->result
;
3750 assert(!"Unexpected intrinsic");
3754 inst
= emit_asm(ir
, opcode
, dst
, off
, data
, data2
);
3755 inst
->resource
= buffer
;
3760 get_image_qualifiers(ir_dereference
*ir
, const glsl_type
**type
,
3761 bool *memory_coherent
, bool *memory_volatile
,
3762 bool *memory_restrict
, bool *memory_read_only
,
3763 unsigned *image_format
)
3766 switch (ir
->ir_type
) {
3767 case ir_type_dereference_record
: {
3768 ir_dereference_record
*deref_record
= ir
->as_dereference_record();
3769 const glsl_type
*struct_type
= deref_record
->record
->type
;
3770 int fild_idx
= deref_record
->field_idx
;
3772 *type
= struct_type
->fields
.structure
[fild_idx
].type
->without_array();
3774 struct_type
->fields
.structure
[fild_idx
].memory_coherent
;
3776 struct_type
->fields
.structure
[fild_idx
].memory_volatile
;
3778 struct_type
->fields
.structure
[fild_idx
].memory_restrict
;
3780 struct_type
->fields
.structure
[fild_idx
].memory_read_only
;
3782 struct_type
->fields
.structure
[fild_idx
].image_format
;
3786 case ir_type_dereference_array
: {
3787 ir_dereference_array
*deref_arr
= ir
->as_dereference_array();
3788 get_image_qualifiers((ir_dereference
*)deref_arr
->array
, type
,
3789 memory_coherent
, memory_volatile
, memory_restrict
,
3790 memory_read_only
, image_format
);
3794 case ir_type_dereference_variable
: {
3795 ir_variable
*var
= ir
->variable_referenced();
3797 *type
= var
->type
->without_array();
3798 *memory_coherent
= var
->data
.memory_coherent
;
3799 *memory_volatile
= var
->data
.memory_volatile
;
3800 *memory_restrict
= var
->data
.memory_restrict
;
3801 *memory_read_only
= var
->data
.memory_read_only
;
3802 *image_format
= var
->data
.image_format
;
3812 glsl_to_tgsi_visitor::visit_image_intrinsic(ir_call
*ir
)
3814 exec_node
*param
= ir
->actual_parameters
.get_head();
3816 ir_dereference
*img
= (ir_dereference
*)param
;
3817 const ir_variable
*imgvar
= img
->variable_referenced();
3818 unsigned sampler_array_size
= 1, sampler_base
= 0;
3819 bool memory_coherent
= false, memory_volatile
= false,
3820 memory_restrict
= false, memory_read_only
= false;
3821 unsigned image_format
= 0;
3822 const glsl_type
*type
= NULL
;
3824 get_image_qualifiers(img
, &type
, &memory_coherent
, &memory_volatile
,
3825 &memory_restrict
, &memory_read_only
, &image_format
);
3828 st_src_reg
image(PROGRAM_IMAGE
, 0, GLSL_TYPE_UINT
);
3830 get_deref_offsets(img
, &sampler_array_size
, &sampler_base
,
3831 &index
, &reladdr
, !imgvar
->contains_bindless());
3833 image
.index
= index
;
3834 if (reladdr
.file
!= PROGRAM_UNDEFINED
) {
3835 image
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
3836 *image
.reladdr
= reladdr
;
3837 emit_arl(ir
, sampler_reladdr
, reladdr
);
3840 st_dst_reg dst
= undef_dst
;
3841 if (ir
->return_deref
) {
3842 ir
->return_deref
->accept(this);
3843 dst
= st_dst_reg(this->result
);
3844 dst
.writemask
= (1 << ir
->return_deref
->type
->vector_elements
) - 1;
3847 glsl_to_tgsi_instruction
*inst
;
3849 st_src_reg bindless
;
3850 if (imgvar
->contains_bindless()) {
3852 bindless
= this->result
;
3855 if (ir
->callee
->intrinsic_id
== ir_intrinsic_image_size
) {
3856 dst
.writemask
= WRITEMASK_XYZ
;
3857 inst
= emit_asm(ir
, TGSI_OPCODE_RESQ
, dst
);
3858 } else if (ir
->callee
->intrinsic_id
== ir_intrinsic_image_samples
) {
3859 st_src_reg res
= get_temp(glsl_type::ivec4_type
);
3860 st_dst_reg dstres
= st_dst_reg(res
);
3861 dstres
.writemask
= WRITEMASK_W
;
3862 inst
= emit_asm(ir
, TGSI_OPCODE_RESQ
, dstres
);
3863 res
.swizzle
= SWIZZLE_WWWW
;
3864 emit_asm(ir
, TGSI_OPCODE_MOV
, dst
, res
);
3866 st_src_reg arg1
= undef_src
, arg2
= undef_src
;
3868 st_dst_reg coord_dst
;
3869 coord
= get_temp(glsl_type::ivec4_type
);
3870 coord_dst
= st_dst_reg(coord
);
3871 coord_dst
.writemask
= (1 << type
->coordinate_components()) - 1;
3872 param
= param
->get_next();
3873 ((ir_dereference
*)param
)->accept(this);
3874 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
3875 coord
.swizzle
= SWIZZLE_XXXX
;
3876 switch (type
->coordinate_components()) {
3877 case 4: assert(!"unexpected coord count");
3879 case 3: coord
.swizzle
|= SWIZZLE_Z
<< 6;
3881 case 2: coord
.swizzle
|= SWIZZLE_Y
<< 3;
3884 if (type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_MS
) {
3885 param
= param
->get_next();
3886 ((ir_dereference
*)param
)->accept(this);
3887 st_src_reg sample
= this->result
;
3888 sample
.swizzle
= SWIZZLE_XXXX
;
3889 coord_dst
.writemask
= WRITEMASK_W
;
3890 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, sample
);
3891 coord
.swizzle
|= SWIZZLE_W
<< 9;
3894 param
= param
->get_next();
3895 if (!param
->is_tail_sentinel()) {
3896 ((ir_dereference
*)param
)->accept(this);
3897 arg1
= this->result
;
3898 param
= param
->get_next();
3901 if (!param
->is_tail_sentinel()) {
3902 ((ir_dereference
*)param
)->accept(this);
3903 arg2
= this->result
;
3904 param
= param
->get_next();
3907 assert(param
->is_tail_sentinel());
3909 enum tgsi_opcode opcode
;
3910 switch (ir
->callee
->intrinsic_id
) {
3911 case ir_intrinsic_image_load
:
3912 opcode
= TGSI_OPCODE_LOAD
;
3914 case ir_intrinsic_image_store
:
3915 opcode
= TGSI_OPCODE_STORE
;
3917 case ir_intrinsic_image_atomic_add
:
3918 opcode
= TGSI_OPCODE_ATOMUADD
;
3920 case ir_intrinsic_image_atomic_min
:
3921 opcode
= TGSI_OPCODE_ATOMIMIN
;
3923 case ir_intrinsic_image_atomic_max
:
3924 opcode
= TGSI_OPCODE_ATOMIMAX
;
3926 case ir_intrinsic_image_atomic_and
:
3927 opcode
= TGSI_OPCODE_ATOMAND
;
3929 case ir_intrinsic_image_atomic_or
:
3930 opcode
= TGSI_OPCODE_ATOMOR
;
3932 case ir_intrinsic_image_atomic_xor
:
3933 opcode
= TGSI_OPCODE_ATOMXOR
;
3935 case ir_intrinsic_image_atomic_exchange
:
3936 opcode
= TGSI_OPCODE_ATOMXCHG
;
3938 case ir_intrinsic_image_atomic_comp_swap
:
3939 opcode
= TGSI_OPCODE_ATOMCAS
;
3942 assert(!"Unexpected intrinsic");
3946 inst
= emit_asm(ir
, opcode
, dst
, coord
, arg1
, arg2
);
3947 if (opcode
== TGSI_OPCODE_STORE
)
3948 inst
->dst
[0].writemask
= WRITEMASK_XYZW
;
3951 if (imgvar
->contains_bindless()) {
3952 inst
->resource
= bindless
;
3953 inst
->resource
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
,
3954 SWIZZLE_X
, SWIZZLE_Y
);
3956 inst
->resource
= image
;
3957 inst
->sampler_array_size
= sampler_array_size
;
3958 inst
->sampler_base
= sampler_base
;
3961 inst
->tex_target
= type
->sampler_index();
3962 inst
->image_format
= st_mesa_format_to_pipe_format(st_context(ctx
),
3963 _mesa_get_shader_image_format(image_format
));
3964 inst
->read_only
= memory_read_only
;
3966 if (memory_coherent
)
3967 inst
->buffer_access
|= TGSI_MEMORY_COHERENT
;
3968 if (memory_restrict
)
3969 inst
->buffer_access
|= TGSI_MEMORY_RESTRICT
;
3970 if (memory_volatile
)
3971 inst
->buffer_access
|= TGSI_MEMORY_VOLATILE
;
3975 glsl_to_tgsi_visitor::visit_generic_intrinsic(ir_call
*ir
, enum tgsi_opcode op
)
3977 ir
->return_deref
->accept(this);
3978 st_dst_reg dst
= st_dst_reg(this->result
);
3980 dst
.writemask
= u_bit_consecutive(0, ir
->return_deref
->var
->type
->vector_elements
);
3982 st_src_reg src
[4] = { undef_src
, undef_src
, undef_src
, undef_src
};
3983 unsigned num_src
= 0;
3984 foreach_in_list(ir_rvalue
, param
, &ir
->actual_parameters
) {
3985 assert(num_src
< ARRAY_SIZE(src
));
3987 this->result
.file
= PROGRAM_UNDEFINED
;
3988 param
->accept(this);
3989 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
3991 src
[num_src
] = this->result
;
3995 emit_asm(ir
, op
, dst
, src
[0], src
[1], src
[2], src
[3]);
3999 glsl_to_tgsi_visitor::visit(ir_call
*ir
)
4001 ir_function_signature
*sig
= ir
->callee
;
4003 /* Filter out intrinsics */
4004 switch (sig
->intrinsic_id
) {
4005 case ir_intrinsic_atomic_counter_read
:
4006 case ir_intrinsic_atomic_counter_increment
:
4007 case ir_intrinsic_atomic_counter_predecrement
:
4008 case ir_intrinsic_atomic_counter_add
:
4009 case ir_intrinsic_atomic_counter_min
:
4010 case ir_intrinsic_atomic_counter_max
:
4011 case ir_intrinsic_atomic_counter_and
:
4012 case ir_intrinsic_atomic_counter_or
:
4013 case ir_intrinsic_atomic_counter_xor
:
4014 case ir_intrinsic_atomic_counter_exchange
:
4015 case ir_intrinsic_atomic_counter_comp_swap
:
4016 visit_atomic_counter_intrinsic(ir
);
4019 case ir_intrinsic_ssbo_load
:
4020 case ir_intrinsic_ssbo_store
:
4021 case ir_intrinsic_ssbo_atomic_add
:
4022 case ir_intrinsic_ssbo_atomic_min
:
4023 case ir_intrinsic_ssbo_atomic_max
:
4024 case ir_intrinsic_ssbo_atomic_and
:
4025 case ir_intrinsic_ssbo_atomic_or
:
4026 case ir_intrinsic_ssbo_atomic_xor
:
4027 case ir_intrinsic_ssbo_atomic_exchange
:
4028 case ir_intrinsic_ssbo_atomic_comp_swap
:
4029 visit_ssbo_intrinsic(ir
);
4032 case ir_intrinsic_memory_barrier
:
4033 case ir_intrinsic_memory_barrier_atomic_counter
:
4034 case ir_intrinsic_memory_barrier_buffer
:
4035 case ir_intrinsic_memory_barrier_image
:
4036 case ir_intrinsic_memory_barrier_shared
:
4037 case ir_intrinsic_group_memory_barrier
:
4038 visit_membar_intrinsic(ir
);
4041 case ir_intrinsic_shared_load
:
4042 case ir_intrinsic_shared_store
:
4043 case ir_intrinsic_shared_atomic_add
:
4044 case ir_intrinsic_shared_atomic_min
:
4045 case ir_intrinsic_shared_atomic_max
:
4046 case ir_intrinsic_shared_atomic_and
:
4047 case ir_intrinsic_shared_atomic_or
:
4048 case ir_intrinsic_shared_atomic_xor
:
4049 case ir_intrinsic_shared_atomic_exchange
:
4050 case ir_intrinsic_shared_atomic_comp_swap
:
4051 visit_shared_intrinsic(ir
);
4054 case ir_intrinsic_image_load
:
4055 case ir_intrinsic_image_store
:
4056 case ir_intrinsic_image_atomic_add
:
4057 case ir_intrinsic_image_atomic_min
:
4058 case ir_intrinsic_image_atomic_max
:
4059 case ir_intrinsic_image_atomic_and
:
4060 case ir_intrinsic_image_atomic_or
:
4061 case ir_intrinsic_image_atomic_xor
:
4062 case ir_intrinsic_image_atomic_exchange
:
4063 case ir_intrinsic_image_atomic_comp_swap
:
4064 case ir_intrinsic_image_size
:
4065 case ir_intrinsic_image_samples
:
4066 visit_image_intrinsic(ir
);
4069 case ir_intrinsic_shader_clock
:
4070 visit_generic_intrinsic(ir
, TGSI_OPCODE_CLOCK
);
4073 case ir_intrinsic_vote_all
:
4074 visit_generic_intrinsic(ir
, TGSI_OPCODE_VOTE_ALL
);
4076 case ir_intrinsic_vote_any
:
4077 visit_generic_intrinsic(ir
, TGSI_OPCODE_VOTE_ANY
);
4079 case ir_intrinsic_vote_eq
:
4080 visit_generic_intrinsic(ir
, TGSI_OPCODE_VOTE_EQ
);
4082 case ir_intrinsic_ballot
:
4083 visit_generic_intrinsic(ir
, TGSI_OPCODE_BALLOT
);
4085 case ir_intrinsic_read_first_invocation
:
4086 visit_generic_intrinsic(ir
, TGSI_OPCODE_READ_FIRST
);
4088 case ir_intrinsic_read_invocation
:
4089 visit_generic_intrinsic(ir
, TGSI_OPCODE_READ_INVOC
);
4092 case ir_intrinsic_invalid
:
4093 case ir_intrinsic_generic_load
:
4094 case ir_intrinsic_generic_store
:
4095 case ir_intrinsic_generic_atomic_add
:
4096 case ir_intrinsic_generic_atomic_and
:
4097 case ir_intrinsic_generic_atomic_or
:
4098 case ir_intrinsic_generic_atomic_xor
:
4099 case ir_intrinsic_generic_atomic_min
:
4100 case ir_intrinsic_generic_atomic_max
:
4101 case ir_intrinsic_generic_atomic_exchange
:
4102 case ir_intrinsic_generic_atomic_comp_swap
:
4103 case ir_intrinsic_begin_invocation_interlock
:
4104 case ir_intrinsic_end_invocation_interlock
:
4105 unreachable("Invalid intrinsic");
4110 glsl_to_tgsi_visitor::calc_deref_offsets(ir_dereference
*tail
,
4111 unsigned *array_elements
,
4113 st_src_reg
*indirect
,
4116 switch (tail
->ir_type
) {
4117 case ir_type_dereference_record
: {
4118 ir_dereference_record
*deref_record
= tail
->as_dereference_record();
4119 const glsl_type
*struct_type
= deref_record
->record
->type
;
4120 int field_index
= deref_record
->field_idx
;
4122 calc_deref_offsets(deref_record
->record
->as_dereference(), array_elements
, index
, indirect
, location
);
4124 assert(field_index
>= 0);
4125 *location
+= struct_type
->struct_location_offset(field_index
);
4129 case ir_type_dereference_array
: {
4130 ir_dereference_array
*deref_arr
= tail
->as_dereference_array();
4132 void *mem_ctx
= ralloc_parent(deref_arr
);
4133 ir_constant
*array_index
=
4134 deref_arr
->array_index
->constant_expression_value(mem_ctx
);
4137 st_src_reg temp_reg
;
4138 st_dst_reg temp_dst
;
4140 temp_reg
= get_temp(glsl_type::uint_type
);
4141 temp_dst
= st_dst_reg(temp_reg
);
4142 temp_dst
.writemask
= 1;
4144 deref_arr
->array_index
->accept(this);
4145 if (*array_elements
!= 1)
4146 emit_asm(NULL
, TGSI_OPCODE_MUL
, temp_dst
, this->result
, st_src_reg_for_int(*array_elements
));
4148 emit_asm(NULL
, TGSI_OPCODE_MOV
, temp_dst
, this->result
);
4150 if (indirect
->file
== PROGRAM_UNDEFINED
)
4151 *indirect
= temp_reg
;
4153 temp_dst
= st_dst_reg(*indirect
);
4154 temp_dst
.writemask
= 1;
4155 emit_asm(NULL
, TGSI_OPCODE_ADD
, temp_dst
, *indirect
, temp_reg
);
4158 *index
+= array_index
->value
.u
[0] * *array_elements
;
4160 *array_elements
*= deref_arr
->array
->type
->length
;
4162 calc_deref_offsets(deref_arr
->array
->as_dereference(), array_elements
, index
, indirect
, location
);
4171 glsl_to_tgsi_visitor::get_deref_offsets(ir_dereference
*ir
,
4172 unsigned *array_size
,
4175 st_src_reg
*reladdr
,
4178 GLuint shader
= _mesa_program_enum_to_shader_stage(this->prog
->Target
);
4179 unsigned location
= 0;
4180 ir_variable
*var
= ir
->variable_referenced();
4188 location
= var
->data
.location
;
4189 calc_deref_offsets(ir
, array_size
, index
, reladdr
, &location
);
4192 * If we end up with no indirect then adjust the base to the index,
4193 * and set the array size to 1.
4195 if (reladdr
->file
== PROGRAM_UNDEFINED
) {
4201 assert(location
!= 0xffffffff);
4202 *base
+= this->shader_program
->data
->UniformStorage
[location
].opaque
[shader
].index
;
4203 *index
+= this->shader_program
->data
->UniformStorage
[location
].opaque
[shader
].index
;
4208 glsl_to_tgsi_visitor::canonicalize_gather_offset(st_src_reg offset
)
4210 if (offset
.reladdr
|| offset
.reladdr2
||
4211 offset
.has_index2
||
4212 offset
.file
== PROGRAM_UNIFORM
||
4213 offset
.file
== PROGRAM_CONSTANT
||
4214 offset
.file
== PROGRAM_STATE_VAR
) {
4215 st_src_reg tmp
= get_temp(glsl_type::ivec2_type
);
4216 st_dst_reg tmp_dst
= st_dst_reg(tmp
);
4217 tmp_dst
.writemask
= WRITEMASK_XY
;
4218 emit_asm(NULL
, TGSI_OPCODE_MOV
, tmp_dst
, offset
);
4226 glsl_to_tgsi_visitor::handle_bound_deref(ir_dereference
*ir
)
4228 ir_variable
*var
= ir
->variable_referenced();
4230 if (!var
|| var
->data
.mode
!= ir_var_uniform
|| var
->data
.bindless
||
4231 !(ir
->type
->is_image() || ir
->type
->is_sampler()))
4234 /* Convert from bound sampler/image to bindless handle. */
4235 bool is_image
= ir
->type
->is_image();
4236 st_src_reg
resource(is_image
? PROGRAM_IMAGE
: PROGRAM_SAMPLER
, 0, GLSL_TYPE_UINT
);
4238 unsigned array_size
= 1, base
= 0;
4240 get_deref_offsets(ir
, &array_size
, &base
, &index
, &reladdr
, true);
4242 resource
.index
= index
;
4243 if (reladdr
.file
!= PROGRAM_UNDEFINED
) {
4244 resource
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
4245 *resource
.reladdr
= reladdr
;
4246 emit_arl(ir
, sampler_reladdr
, reladdr
);
4249 this->result
= get_temp(glsl_type::uvec2_type
);
4250 st_dst_reg
dst(this->result
);
4251 dst
.writemask
= WRITEMASK_XY
;
4253 glsl_to_tgsi_instruction
*inst
= emit_asm(
4254 ir
, is_image
? TGSI_OPCODE_IMG2HND
: TGSI_OPCODE_SAMP2HND
, dst
);
4256 inst
->tex_target
= ir
->type
->sampler_index();
4257 inst
->resource
= resource
;
4258 inst
->sampler_array_size
= array_size
;
4259 inst
->sampler_base
= base
;
4265 glsl_to_tgsi_visitor::visit(ir_texture
*ir
)
4267 st_src_reg result_src
, coord
, cube_sc
, lod_info
, projector
, dx
, dy
;
4268 st_src_reg offset
[MAX_GLSL_TEXTURE_OFFSET
], sample_index
, component
;
4269 st_src_reg levels_src
, reladdr
;
4270 st_dst_reg result_dst
, coord_dst
, cube_sc_dst
;
4271 glsl_to_tgsi_instruction
*inst
= NULL
;
4272 enum tgsi_opcode opcode
= TGSI_OPCODE_NOP
;
4273 const glsl_type
*sampler_type
= ir
->sampler
->type
;
4274 unsigned sampler_array_size
= 1, sampler_base
= 0;
4275 bool is_cube_array
= false, is_cube_shadow
= false;
4276 ir_variable
*var
= ir
->sampler
->variable_referenced();
4279 /* if we are a cube array sampler or a cube shadow */
4280 if (sampler_type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
) {
4281 is_cube_array
= sampler_type
->sampler_array
;
4282 is_cube_shadow
= sampler_type
->sampler_shadow
;
4285 if (ir
->coordinate
) {
4286 ir
->coordinate
->accept(this);
4288 /* Put our coords in a temp. We'll need to modify them for shadow,
4289 * projection, or LOD, so the only case we'd use it as-is is if
4290 * we're doing plain old texturing. The optimization passes on
4291 * glsl_to_tgsi_visitor should handle cleaning up our mess in that case.
4293 coord
= get_temp(glsl_type::vec4_type
);
4294 coord_dst
= st_dst_reg(coord
);
4295 coord_dst
.writemask
= (1 << ir
->coordinate
->type
->vector_elements
) - 1;
4296 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
4299 if (ir
->projector
) {
4300 ir
->projector
->accept(this);
4301 projector
= this->result
;
4304 /* Storage for our result. Ideally for an assignment we'd be using
4305 * the actual storage for the result here, instead.
4307 result_src
= get_temp(ir
->type
);
4308 result_dst
= st_dst_reg(result_src
);
4309 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
4313 opcode
= (is_cube_array
&& ir
->shadow_comparator
) ? TGSI_OPCODE_TEX2
: TGSI_OPCODE_TEX
;
4315 ir
->offset
->accept(this);
4316 offset
[0] = this->result
;
4320 if (is_cube_array
|| is_cube_shadow
) {
4321 opcode
= TGSI_OPCODE_TXB2
;
4324 opcode
= TGSI_OPCODE_TXB
;
4326 ir
->lod_info
.bias
->accept(this);
4327 lod_info
= this->result
;
4329 ir
->offset
->accept(this);
4330 offset
[0] = this->result
;
4334 if (this->has_tex_txf_lz
&& ir
->lod_info
.lod
->is_zero()) {
4335 opcode
= TGSI_OPCODE_TEX_LZ
;
4337 opcode
= is_cube_array
? TGSI_OPCODE_TXL2
: TGSI_OPCODE_TXL
;
4338 ir
->lod_info
.lod
->accept(this);
4339 lod_info
= this->result
;
4342 ir
->offset
->accept(this);
4343 offset
[0] = this->result
;
4347 opcode
= TGSI_OPCODE_TXD
;
4348 ir
->lod_info
.grad
.dPdx
->accept(this);
4350 ir
->lod_info
.grad
.dPdy
->accept(this);
4353 ir
->offset
->accept(this);
4354 offset
[0] = this->result
;
4358 opcode
= TGSI_OPCODE_TXQ
;
4359 ir
->lod_info
.lod
->accept(this);
4360 lod_info
= this->result
;
4362 case ir_query_levels
:
4363 opcode
= TGSI_OPCODE_TXQ
;
4364 lod_info
= undef_src
;
4365 levels_src
= get_temp(ir
->type
);
4368 if (this->has_tex_txf_lz
&& ir
->lod_info
.lod
->is_zero()) {
4369 opcode
= TGSI_OPCODE_TXF_LZ
;
4371 opcode
= TGSI_OPCODE_TXF
;
4372 ir
->lod_info
.lod
->accept(this);
4373 lod_info
= this->result
;
4376 ir
->offset
->accept(this);
4377 offset
[0] = this->result
;
4381 opcode
= TGSI_OPCODE_TXF
;
4382 ir
->lod_info
.sample_index
->accept(this);
4383 sample_index
= this->result
;
4386 opcode
= TGSI_OPCODE_TG4
;
4387 ir
->lod_info
.component
->accept(this);
4388 component
= this->result
;
4390 ir
->offset
->accept(this);
4391 if (ir
->offset
->type
->is_array()) {
4392 const glsl_type
*elt_type
= ir
->offset
->type
->fields
.array
;
4393 for (i
= 0; i
< ir
->offset
->type
->length
; i
++) {
4394 offset
[i
] = this->result
;
4395 offset
[i
].index
+= i
* type_size(elt_type
);
4396 offset
[i
].type
= elt_type
->base_type
;
4397 offset
[i
].swizzle
= swizzle_for_size(elt_type
->vector_elements
);
4398 offset
[i
] = canonicalize_gather_offset(offset
[i
]);
4401 offset
[0] = canonicalize_gather_offset(this->result
);
4406 opcode
= TGSI_OPCODE_LODQ
;
4408 case ir_texture_samples
:
4409 opcode
= TGSI_OPCODE_TXQS
;
4411 case ir_samples_identical
:
4412 unreachable("Unexpected ir_samples_identical opcode");
4415 if (ir
->projector
) {
4416 if (opcode
== TGSI_OPCODE_TEX
) {
4417 /* Slot the projector in as the last component of the coord. */
4418 coord_dst
.writemask
= WRITEMASK_W
;
4419 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, projector
);
4420 coord_dst
.writemask
= WRITEMASK_XYZW
;
4421 opcode
= TGSI_OPCODE_TXP
;
4423 st_src_reg coord_w
= coord
;
4424 coord_w
.swizzle
= SWIZZLE_WWWW
;
4426 /* For the other TEX opcodes there's no projective version
4427 * since the last slot is taken up by LOD info. Do the
4428 * projective divide now.
4430 coord_dst
.writemask
= WRITEMASK_W
;
4431 emit_asm(ir
, TGSI_OPCODE_RCP
, coord_dst
, projector
);
4433 /* In the case where we have to project the coordinates "by hand,"
4434 * the shadow comparator value must also be projected.
4436 st_src_reg tmp_src
= coord
;
4437 if (ir
->shadow_comparator
) {
4438 /* Slot the shadow value in as the second to last component of the
4441 ir
->shadow_comparator
->accept(this);
4443 tmp_src
= get_temp(glsl_type::vec4_type
);
4444 st_dst_reg tmp_dst
= st_dst_reg(tmp_src
);
4446 /* Projective division not allowed for array samplers. */
4447 assert(!sampler_type
->sampler_array
);
4449 tmp_dst
.writemask
= WRITEMASK_Z
;
4450 emit_asm(ir
, TGSI_OPCODE_MOV
, tmp_dst
, this->result
);
4452 tmp_dst
.writemask
= WRITEMASK_XY
;
4453 emit_asm(ir
, TGSI_OPCODE_MOV
, tmp_dst
, coord
);
4456 coord_dst
.writemask
= WRITEMASK_XYZ
;
4457 emit_asm(ir
, TGSI_OPCODE_MUL
, coord_dst
, tmp_src
, coord_w
);
4459 coord_dst
.writemask
= WRITEMASK_XYZW
;
4460 coord
.swizzle
= SWIZZLE_XYZW
;
4464 /* If projection is done and the opcode is not TGSI_OPCODE_TXP, then the
4465 * shadow comparator was put in the correct place (and projected) by the
4466 * code, above, that handles by-hand projection.
4468 if (ir
->shadow_comparator
&& (!ir
->projector
|| opcode
== TGSI_OPCODE_TXP
)) {
4469 /* Slot the shadow value in as the second to last component of the
4472 ir
->shadow_comparator
->accept(this);
4474 if (is_cube_array
) {
4475 cube_sc
= get_temp(glsl_type::float_type
);
4476 cube_sc_dst
= st_dst_reg(cube_sc
);
4477 cube_sc_dst
.writemask
= WRITEMASK_X
;
4478 emit_asm(ir
, TGSI_OPCODE_MOV
, cube_sc_dst
, this->result
);
4479 cube_sc_dst
.writemask
= WRITEMASK_X
;
4482 if ((sampler_type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_2D
&&
4483 sampler_type
->sampler_array
) ||
4484 sampler_type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
) {
4485 coord_dst
.writemask
= WRITEMASK_W
;
4487 coord_dst
.writemask
= WRITEMASK_Z
;
4489 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, this->result
);
4490 coord_dst
.writemask
= WRITEMASK_XYZW
;
4494 if (ir
->op
== ir_txf_ms
) {
4495 coord_dst
.writemask
= WRITEMASK_W
;
4496 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, sample_index
);
4497 coord_dst
.writemask
= WRITEMASK_XYZW
;
4498 } else if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXB
||
4499 opcode
== TGSI_OPCODE_TXF
) {
4500 /* TGSI stores LOD or LOD bias in the last channel of the coords. */
4501 coord_dst
.writemask
= WRITEMASK_W
;
4502 emit_asm(ir
, TGSI_OPCODE_MOV
, coord_dst
, lod_info
);
4503 coord_dst
.writemask
= WRITEMASK_XYZW
;
4506 st_src_reg
sampler(PROGRAM_SAMPLER
, 0, GLSL_TYPE_UINT
);
4509 get_deref_offsets(ir
->sampler
, &sampler_array_size
, &sampler_base
,
4510 &index
, &reladdr
, !var
->contains_bindless());
4512 sampler
.index
= index
;
4513 if (reladdr
.file
!= PROGRAM_UNDEFINED
) {
4514 sampler
.reladdr
= ralloc(mem_ctx
, st_src_reg
);
4515 *sampler
.reladdr
= reladdr
;
4516 emit_arl(ir
, sampler_reladdr
, reladdr
);
4519 st_src_reg bindless
;
4520 if (var
->contains_bindless()) {
4521 ir
->sampler
->accept(this);
4522 bindless
= this->result
;
4525 if (opcode
== TGSI_OPCODE_TXD
)
4526 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, dx
, dy
);
4527 else if (opcode
== TGSI_OPCODE_TXQ
) {
4528 if (ir
->op
== ir_query_levels
) {
4529 /* the level is stored in W */
4530 inst
= emit_asm(ir
, opcode
, st_dst_reg(levels_src
), lod_info
);
4531 result_dst
.writemask
= WRITEMASK_X
;
4532 levels_src
.swizzle
= SWIZZLE_WWWW
;
4533 emit_asm(ir
, TGSI_OPCODE_MOV
, result_dst
, levels_src
);
4535 inst
= emit_asm(ir
, opcode
, result_dst
, lod_info
);
4536 } else if (opcode
== TGSI_OPCODE_TXQS
) {
4537 inst
= emit_asm(ir
, opcode
, result_dst
);
4538 } else if (opcode
== TGSI_OPCODE_TXL2
|| opcode
== TGSI_OPCODE_TXB2
) {
4539 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, lod_info
);
4540 } else if (opcode
== TGSI_OPCODE_TEX2
) {
4541 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, cube_sc
);
4542 } else if (opcode
== TGSI_OPCODE_TG4
) {
4543 if (is_cube_array
&& ir
->shadow_comparator
) {
4544 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, cube_sc
);
4546 inst
= emit_asm(ir
, opcode
, result_dst
, coord
, component
);
4549 inst
= emit_asm(ir
, opcode
, result_dst
, coord
);
4551 if (ir
->shadow_comparator
)
4552 inst
->tex_shadow
= GL_TRUE
;
4554 if (var
->contains_bindless()) {
4555 inst
->resource
= bindless
;
4556 inst
->resource
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
,
4557 SWIZZLE_X
, SWIZZLE_Y
);
4559 inst
->resource
= sampler
;
4560 inst
->sampler_array_size
= sampler_array_size
;
4561 inst
->sampler_base
= sampler_base
;
4565 if (!inst
->tex_offsets
)
4566 inst
->tex_offsets
= rzalloc_array(inst
, st_src_reg
,
4567 MAX_GLSL_TEXTURE_OFFSET
);
4569 for (i
= 0; i
< MAX_GLSL_TEXTURE_OFFSET
&&
4570 offset
[i
].file
!= PROGRAM_UNDEFINED
; i
++)
4571 inst
->tex_offsets
[i
] = offset
[i
];
4572 inst
->tex_offset_num_offset
= i
;
4575 inst
->tex_target
= sampler_type
->sampler_index();
4576 inst
->tex_type
= ir
->type
->base_type
;
4578 this->result
= result_src
;
4582 glsl_to_tgsi_visitor::visit(ir_return
*ir
)
4584 assert(!ir
->get_value());
4586 emit_asm(ir
, TGSI_OPCODE_RET
);
4590 glsl_to_tgsi_visitor::visit(ir_discard
*ir
)
4592 if (ir
->condition
) {
4593 ir
->condition
->accept(this);
4594 st_src_reg condition
= this->result
;
4596 /* Convert the bool condition to a float so we can negate. */
4597 if (native_integers
) {
4598 st_src_reg temp
= get_temp(ir
->condition
->type
);
4599 emit_asm(ir
, TGSI_OPCODE_AND
, st_dst_reg(temp
),
4600 condition
, st_src_reg_for_float(1.0));
4604 condition
.negate
= ~condition
.negate
;
4605 emit_asm(ir
, TGSI_OPCODE_KILL_IF
, undef_dst
, condition
);
4607 /* unconditional kil */
4608 emit_asm(ir
, TGSI_OPCODE_KILL
);
4613 glsl_to_tgsi_visitor::visit(ir_if
*ir
)
4615 enum tgsi_opcode if_opcode
;
4616 glsl_to_tgsi_instruction
*if_inst
;
4618 ir
->condition
->accept(this);
4619 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
4621 if_opcode
= native_integers
? TGSI_OPCODE_UIF
: TGSI_OPCODE_IF
;
4623 if_inst
= emit_asm(ir
->condition
, if_opcode
, undef_dst
, this->result
);
4625 this->instructions
.push_tail(if_inst
);
4627 visit_exec_list(&ir
->then_instructions
, this);
4629 if (!ir
->else_instructions
.is_empty()) {
4630 emit_asm(ir
->condition
, TGSI_OPCODE_ELSE
);
4631 visit_exec_list(&ir
->else_instructions
, this);
4634 if_inst
= emit_asm(ir
->condition
, TGSI_OPCODE_ENDIF
);
4639 glsl_to_tgsi_visitor::visit(ir_emit_vertex
*ir
)
4641 assert(this->prog
->Target
== GL_GEOMETRY_PROGRAM_NV
);
4643 ir
->stream
->accept(this);
4644 emit_asm(ir
, TGSI_OPCODE_EMIT
, undef_dst
, this->result
);
4648 glsl_to_tgsi_visitor::visit(ir_end_primitive
*ir
)
4650 assert(this->prog
->Target
== GL_GEOMETRY_PROGRAM_NV
);
4652 ir
->stream
->accept(this);
4653 emit_asm(ir
, TGSI_OPCODE_ENDPRIM
, undef_dst
, this->result
);
4657 glsl_to_tgsi_visitor::visit(ir_barrier
*ir
)
4659 assert(this->prog
->Target
== GL_TESS_CONTROL_PROGRAM_NV
||
4660 this->prog
->Target
== GL_COMPUTE_PROGRAM_NV
);
4662 emit_asm(ir
, TGSI_OPCODE_BARRIER
);
4665 glsl_to_tgsi_visitor::glsl_to_tgsi_visitor()
4667 STATIC_ASSERT(sizeof(samplers_used
) * 8 >= PIPE_MAX_SAMPLERS
);
4669 result
.file
= PROGRAM_UNDEFINED
;
4676 num_input_arrays
= 0;
4677 num_output_arrays
= 0;
4679 num_atomic_arrays
= 0;
4681 num_address_regs
= 0;
4684 indirect_addr_consts
= false;
4685 wpos_transform_const
= -1;
4686 native_integers
= false;
4687 mem_ctx
= ralloc_context(NULL
);
4692 shader_program
= NULL
;
4697 use_shared_memory
= false;
4698 has_tex_txf_lz
= false;
4702 static void var_destroy(struct hash_entry
*entry
)
4704 variable_storage
*storage
= (variable_storage
*)entry
->data
;
4709 glsl_to_tgsi_visitor::~glsl_to_tgsi_visitor()
4711 _mesa_hash_table_destroy(variables
, var_destroy
);
4713 ralloc_free(mem_ctx
);
4716 extern "C" void free_glsl_to_tgsi_visitor(glsl_to_tgsi_visitor
*v
)
4723 * Count resources used by the given gpu program (number of texture
4727 count_resources(glsl_to_tgsi_visitor
*v
, gl_program
*prog
)
4729 v
->samplers_used
= 0;
4731 prog
->info
.textures_used_by_txf
= 0;
4733 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &v
->instructions
) {
4734 if (inst
->info
->is_tex
) {
4735 for (int i
= 0; i
< inst
->sampler_array_size
; i
++) {
4736 unsigned idx
= inst
->sampler_base
+ i
;
4737 v
->samplers_used
|= 1u << idx
;
4739 debug_assert(idx
< (int)ARRAY_SIZE(v
->sampler_types
));
4740 v
->sampler_types
[idx
] = inst
->tex_type
;
4741 v
->sampler_targets
[idx
] =
4742 st_translate_texture_target(inst
->tex_target
, inst
->tex_shadow
);
4744 if (inst
->op
== TGSI_OPCODE_TXF
|| inst
->op
== TGSI_OPCODE_TXF_LZ
) {
4745 prog
->info
.textures_used_by_txf
|= 1u << idx
;
4750 if (inst
->tex_target
== TEXTURE_EXTERNAL_INDEX
)
4751 prog
->ExternalSamplersUsed
|= 1 << inst
->resource
.index
;
4753 if (inst
->resource
.file
!= PROGRAM_UNDEFINED
&& (
4754 is_resource_instruction(inst
->op
) ||
4755 inst
->op
== TGSI_OPCODE_STORE
)) {
4756 if (inst
->resource
.file
== PROGRAM_MEMORY
) {
4757 v
->use_shared_memory
= true;
4758 } else if (inst
->resource
.file
== PROGRAM_IMAGE
) {
4759 for (int i
= 0; i
< inst
->sampler_array_size
; i
++) {
4760 unsigned idx
= inst
->sampler_base
+ i
;
4761 v
->images_used
|= 1 << idx
;
4762 v
->image_targets
[idx
] =
4763 st_translate_texture_target(inst
->tex_target
, false);
4764 v
->image_formats
[idx
] = inst
->image_format
;
4765 v
->image_wr
[idx
] = !inst
->read_only
;
4770 prog
->SamplersUsed
= v
->samplers_used
;
4772 if (v
->shader_program
!= NULL
)
4773 _mesa_update_shader_textures_used(v
->shader_program
, prog
);
4777 * Returns the mask of channels (bitmask of WRITEMASK_X,Y,Z,W) which
4778 * are read from the given src in this instruction
4781 get_src_arg_mask(st_dst_reg dst
, st_src_reg src
)
4783 int read_mask
= 0, comp
;
4785 /* Now, given the src swizzle and the written channels, find which
4786 * components are actually read
4788 for (comp
= 0; comp
< 4; ++comp
) {
4789 const unsigned coord
= GET_SWZ(src
.swizzle
, comp
);
4791 if (dst
.writemask
& (1 << comp
) && coord
<= SWIZZLE_W
)
4792 read_mask
|= 1 << coord
;
4799 * This pass replaces CMP T0, T1 T2 T0 with MOV T0, T2 when the CMP
4800 * instruction is the first instruction to write to register T0. There are
4801 * several lowering passes done in GLSL IR (e.g. branches and
4802 * relative addressing) that create a large number of conditional assignments
4803 * that ir_to_mesa converts to CMP instructions like the one mentioned above.
4805 * Here is why this conversion is safe:
4806 * CMP T0, T1 T2 T0 can be expanded to:
4812 * If (T1 < 0.0) evaluates to true then our replacement MOV T0, T2 is the same
4813 * as the original program. If (T1 < 0.0) evaluates to false, executing
4814 * MOV T0, T0 will store a garbage value in T0 since T0 is uninitialized.
4815 * Therefore, it doesn't matter that we are replacing MOV T0, T0 with MOV T0, T2
4816 * because any instruction that was going to read from T0 after this was going
4817 * to read a garbage value anyway.
4820 glsl_to_tgsi_visitor::simplify_cmp(void)
4822 int tempWritesSize
= 0;
4823 unsigned *tempWrites
= NULL
;
4824 unsigned outputWrites
[VARYING_SLOT_TESS_MAX
];
4826 memset(outputWrites
, 0, sizeof(outputWrites
));
4828 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4829 unsigned prevWriteMask
= 0;
4831 /* Give up if we encounter relative addressing or flow control. */
4832 if (inst
->dst
[0].reladdr
|| inst
->dst
[0].reladdr2
||
4833 inst
->dst
[1].reladdr
|| inst
->dst
[1].reladdr2
||
4834 inst
->info
->is_branch
||
4835 inst
->op
== TGSI_OPCODE_CONT
||
4836 inst
->op
== TGSI_OPCODE_END
||
4837 inst
->op
== TGSI_OPCODE_RET
) {
4841 if (inst
->dst
[0].file
== PROGRAM_OUTPUT
) {
4842 assert(inst
->dst
[0].index
< (signed)ARRAY_SIZE(outputWrites
));
4843 prevWriteMask
= outputWrites
[inst
->dst
[0].index
];
4844 outputWrites
[inst
->dst
[0].index
] |= inst
->dst
[0].writemask
;
4845 } else if (inst
->dst
[0].file
== PROGRAM_TEMPORARY
) {
4846 if (inst
->dst
[0].index
>= tempWritesSize
) {
4847 const int inc
= 4096;
4849 tempWrites
= (unsigned*)
4851 (tempWritesSize
+ inc
) * sizeof(unsigned));
4855 memset(tempWrites
+ tempWritesSize
, 0, inc
* sizeof(unsigned));
4856 tempWritesSize
+= inc
;
4859 prevWriteMask
= tempWrites
[inst
->dst
[0].index
];
4860 tempWrites
[inst
->dst
[0].index
] |= inst
->dst
[0].writemask
;
4864 /* For a CMP to be considered a conditional write, the destination
4865 * register and source register two must be the same. */
4866 if (inst
->op
== TGSI_OPCODE_CMP
4867 && !(inst
->dst
[0].writemask
& prevWriteMask
)
4868 && inst
->src
[2].file
== inst
->dst
[0].file
4869 && inst
->src
[2].index
== inst
->dst
[0].index
4870 && inst
->dst
[0].writemask
==
4871 get_src_arg_mask(inst
->dst
[0], inst
->src
[2])) {
4873 inst
->op
= TGSI_OPCODE_MOV
;
4874 inst
->info
= tgsi_get_opcode_info(inst
->op
);
4875 inst
->src
[0] = inst
->src
[1];
4883 rename_temp_handle_src(struct rename_reg_pair
*renames
, st_src_reg
*src
)
4885 if (src
&& src
->file
== PROGRAM_TEMPORARY
) {
4886 int old_idx
= src
->index
;
4887 if (renames
[old_idx
].valid
)
4888 src
->index
= renames
[old_idx
].new_reg
;
4892 /* Replaces all references to a temporary register index with another index. */
4894 glsl_to_tgsi_visitor::rename_temp_registers(struct rename_reg_pair
*renames
)
4896 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4898 for (j
= 0; j
< num_inst_src_regs(inst
); j
++) {
4899 rename_temp_handle_src(renames
, &inst
->src
[j
]);
4900 rename_temp_handle_src(renames
, inst
->src
[j
].reladdr
);
4901 rename_temp_handle_src(renames
, inst
->src
[j
].reladdr2
);
4904 for (j
= 0; j
< inst
->tex_offset_num_offset
; j
++) {
4905 rename_temp_handle_src(renames
, &inst
->tex_offsets
[j
]);
4906 rename_temp_handle_src(renames
, inst
->tex_offsets
[j
].reladdr
);
4907 rename_temp_handle_src(renames
, inst
->tex_offsets
[j
].reladdr2
);
4910 rename_temp_handle_src(renames
, &inst
->resource
);
4911 rename_temp_handle_src(renames
, inst
->resource
.reladdr
);
4912 rename_temp_handle_src(renames
, inst
->resource
.reladdr2
);
4914 for (j
= 0; j
< num_inst_dst_regs(inst
); j
++) {
4915 if (inst
->dst
[j
].file
== PROGRAM_TEMPORARY
) {
4916 int old_idx
= inst
->dst
[j
].index
;
4917 if (renames
[old_idx
].valid
)
4918 inst
->dst
[j
].index
= renames
[old_idx
].new_reg
;
4920 rename_temp_handle_src(renames
, inst
->dst
[j
].reladdr
);
4921 rename_temp_handle_src(renames
, inst
->dst
[j
].reladdr2
);
4927 glsl_to_tgsi_visitor::get_first_temp_write(int *first_writes
)
4929 int depth
= 0; /* loop depth */
4930 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
4933 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4934 for (j
= 0; j
< num_inst_dst_regs(inst
); j
++) {
4935 if (inst
->dst
[j
].file
== PROGRAM_TEMPORARY
) {
4936 if (first_writes
[inst
->dst
[j
].index
] == -1)
4937 first_writes
[inst
->dst
[j
].index
] = (depth
== 0) ? i
: loop_start
;
4941 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
4944 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
4954 glsl_to_tgsi_visitor::get_first_temp_read(int *first_reads
)
4956 int depth
= 0; /* loop depth */
4957 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
4960 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4961 for (j
= 0; j
< num_inst_src_regs(inst
); j
++) {
4962 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
) {
4963 if (first_reads
[inst
->src
[j
].index
] == -1)
4964 first_reads
[inst
->src
[j
].index
] = (depth
== 0) ? i
: loop_start
;
4967 for (j
= 0; j
< inst
->tex_offset_num_offset
; j
++) {
4968 if (inst
->tex_offsets
[j
].file
== PROGRAM_TEMPORARY
) {
4969 if (first_reads
[inst
->tex_offsets
[j
].index
] == -1)
4970 first_reads
[inst
->tex_offsets
[j
].index
] = (depth
== 0) ? i
: loop_start
;
4973 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
4976 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
4986 glsl_to_tgsi_visitor::get_last_temp_read_first_temp_write(int *last_reads
, int *first_writes
)
4988 int depth
= 0; /* loop depth */
4989 int loop_start
= -1; /* index of the first active BGNLOOP (if any) */
4992 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
4993 for (j
= 0; j
< num_inst_src_regs(inst
); j
++) {
4994 if (inst
->src
[j
].file
== PROGRAM_TEMPORARY
)
4995 last_reads
[inst
->src
[j
].index
] = (depth
== 0) ? i
: -2;
4997 for (j
= 0; j
< num_inst_dst_regs(inst
); j
++) {
4998 if (inst
->dst
[j
].file
== PROGRAM_TEMPORARY
) {
4999 if (first_writes
[inst
->dst
[j
].index
] == -1)
5000 first_writes
[inst
->dst
[j
].index
] = (depth
== 0) ? i
: loop_start
;
5001 last_reads
[inst
->dst
[j
].index
] = (depth
== 0) ? i
: -2;
5004 for (j
= 0; j
< inst
->tex_offset_num_offset
; j
++) {
5005 if (inst
->tex_offsets
[j
].file
== PROGRAM_TEMPORARY
)
5006 last_reads
[inst
->tex_offsets
[j
].index
] = (depth
== 0) ? i
: -2;
5008 if (inst
->op
== TGSI_OPCODE_BGNLOOP
) {
5011 } else if (inst
->op
== TGSI_OPCODE_ENDLOOP
) {
5014 for (k
= 0; k
< this->next_temp
; k
++) {
5015 if (last_reads
[k
] == -2) {
5027 glsl_to_tgsi_visitor::get_last_temp_write(int *last_writes
)
5029 int depth
= 0; /* loop depth */
5033 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5034 for (j
= 0; j
< num_inst_dst_regs(inst
); j
++) {
5035 if (inst
->dst
[j
].file
== PROGRAM_TEMPORARY
)
5036 last_writes
[inst
->dst
[j
].index
] = (depth
== 0) ? i
: -2;
5039 if (inst
->op
== TGSI_OPCODE_BGNLOOP
)
5041 else if (inst
->op
== TGSI_OPCODE_ENDLOOP
)
5043 for (k
= 0; k
< this->next_temp
; k
++) {
5044 if (last_writes
[k
] == -2) {
5055 * On a basic block basis, tracks available PROGRAM_TEMPORARY register
5056 * channels for copy propagation and updates following instructions to
5057 * use the original versions.
5059 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
5060 * will occur. As an example, a TXP production before this pass:
5062 * 0: MOV TEMP[1], INPUT[4].xyyy;
5063 * 1: MOV TEMP[1].w, INPUT[4].wwww;
5064 * 2: TXP TEMP[2], TEMP[1], texture[0], 2D;
5068 * 0: MOV TEMP[1], INPUT[4].xyyy;
5069 * 1: MOV TEMP[1].w, INPUT[4].wwww;
5070 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
5072 * which allows for dead code elimination on TEMP[1]'s writes.
5075 glsl_to_tgsi_visitor::copy_propagate(void)
5077 glsl_to_tgsi_instruction
**acp
= rzalloc_array(mem_ctx
,
5078 glsl_to_tgsi_instruction
*,
5079 this->next_temp
* 4);
5080 int *acp_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
5083 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5084 assert(inst
->dst
[0].file
!= PROGRAM_TEMPORARY
5085 || inst
->dst
[0].index
< this->next_temp
);
5087 /* First, do any copy propagation possible into the src regs. */
5088 for (int r
= 0; r
< 3; r
++) {
5089 glsl_to_tgsi_instruction
*first
= NULL
;
5091 int acp_base
= inst
->src
[r
].index
* 4;
5093 if (inst
->src
[r
].file
!= PROGRAM_TEMPORARY
||
5094 inst
->src
[r
].reladdr
||
5095 inst
->src
[r
].reladdr2
)
5098 /* See if we can find entries in the ACP consisting of MOVs
5099 * from the same src register for all the swizzled channels
5100 * of this src register reference.
5102 for (int i
= 0; i
< 4; i
++) {
5103 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
5104 glsl_to_tgsi_instruction
*copy_chan
= acp
[acp_base
+ src_chan
];
5111 assert(acp_level
[acp_base
+ src_chan
] <= level
);
5116 if (first
->src
[0].file
!= copy_chan
->src
[0].file
||
5117 first
->src
[0].index
!= copy_chan
->src
[0].index
||
5118 first
->src
[0].double_reg2
!= copy_chan
->src
[0].double_reg2
||
5119 first
->src
[0].index2D
!= copy_chan
->src
[0].index2D
) {
5127 /* We've now validated that we can copy-propagate to
5128 * replace this src register reference. Do it.
5130 inst
->src
[r
].file
= first
->src
[0].file
;
5131 inst
->src
[r
].index
= first
->src
[0].index
;
5132 inst
->src
[r
].index2D
= first
->src
[0].index2D
;
5133 inst
->src
[r
].has_index2
= first
->src
[0].has_index2
;
5134 inst
->src
[r
].double_reg2
= first
->src
[0].double_reg2
;
5135 inst
->src
[r
].array_id
= first
->src
[0].array_id
;
5138 for (int i
= 0; i
< 4; i
++) {
5139 int src_chan
= GET_SWZ(inst
->src
[r
].swizzle
, i
);
5140 glsl_to_tgsi_instruction
*copy_inst
= acp
[acp_base
+ src_chan
];
5141 swizzle
|= (GET_SWZ(copy_inst
->src
[0].swizzle
, src_chan
) << (3 * i
));
5143 inst
->src
[r
].swizzle
= swizzle
;
5148 case TGSI_OPCODE_BGNLOOP
:
5149 case TGSI_OPCODE_ENDLOOP
:
5150 /* End of a basic block, clear the ACP entirely. */
5151 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
5154 case TGSI_OPCODE_IF
:
5155 case TGSI_OPCODE_UIF
:
5159 case TGSI_OPCODE_ENDIF
:
5160 case TGSI_OPCODE_ELSE
:
5161 /* Clear all channels written inside the block from the ACP, but
5162 * leaving those that were not touched.
5164 for (int r
= 0; r
< this->next_temp
; r
++) {
5165 for (int c
= 0; c
< 4; c
++) {
5166 if (!acp
[4 * r
+ c
])
5169 if (acp_level
[4 * r
+ c
] >= level
)
5170 acp
[4 * r
+ c
] = NULL
;
5173 if (inst
->op
== TGSI_OPCODE_ENDIF
)
5178 /* Continuing the block, clear any written channels from
5181 for (int d
= 0; d
< 2; d
++) {
5182 if (inst
->dst
[d
].file
== PROGRAM_TEMPORARY
&& inst
->dst
[d
].reladdr
) {
5183 /* Any temporary might be written, so no copy propagation
5184 * across this instruction.
5186 memset(acp
, 0, sizeof(*acp
) * this->next_temp
* 4);
5187 } else if (inst
->dst
[d
].file
== PROGRAM_OUTPUT
&&
5188 inst
->dst
[d
].reladdr
) {
5189 /* Any output might be written, so no copy propagation
5190 * from outputs across this instruction.
5192 for (int r
= 0; r
< this->next_temp
; r
++) {
5193 for (int c
= 0; c
< 4; c
++) {
5194 if (!acp
[4 * r
+ c
])
5197 if (acp
[4 * r
+ c
]->src
[0].file
== PROGRAM_OUTPUT
)
5198 acp
[4 * r
+ c
] = NULL
;
5201 } else if (inst
->dst
[d
].file
== PROGRAM_TEMPORARY
||
5202 inst
->dst
[d
].file
== PROGRAM_OUTPUT
) {
5203 /* Clear where it's used as dst. */
5204 if (inst
->dst
[d
].file
== PROGRAM_TEMPORARY
) {
5205 for (int c
= 0; c
< 4; c
++) {
5206 if (inst
->dst
[d
].writemask
& (1 << c
))
5207 acp
[4 * inst
->dst
[d
].index
+ c
] = NULL
;
5211 /* Clear where it's used as src. */
5212 for (int r
= 0; r
< this->next_temp
; r
++) {
5213 for (int c
= 0; c
< 4; c
++) {
5214 if (!acp
[4 * r
+ c
])
5217 int src_chan
= GET_SWZ(acp
[4 * r
+ c
]->src
[0].swizzle
, c
);
5219 if (acp
[4 * r
+ c
]->src
[0].file
== inst
->dst
[d
].file
&&
5220 acp
[4 * r
+ c
]->src
[0].index
== inst
->dst
[d
].index
&&
5221 inst
->dst
[d
].writemask
& (1 << src_chan
)) {
5222 acp
[4 * r
+ c
] = NULL
;
5231 /* If this is a copy, add it to the ACP. */
5232 if (inst
->op
== TGSI_OPCODE_MOV
&&
5233 inst
->dst
[0].file
== PROGRAM_TEMPORARY
&&
5234 !(inst
->dst
[0].file
== inst
->src
[0].file
&&
5235 inst
->dst
[0].index
== inst
->src
[0].index
) &&
5236 !inst
->dst
[0].reladdr
&&
5237 !inst
->dst
[0].reladdr2
&&
5239 inst
->src
[0].file
!= PROGRAM_ARRAY
&&
5240 (inst
->src
[0].file
!= PROGRAM_OUTPUT
||
5241 this->shader
->Stage
!= MESA_SHADER_TESS_CTRL
) &&
5242 !inst
->src
[0].reladdr
&&
5243 !inst
->src
[0].reladdr2
&&
5244 !inst
->src
[0].negate
&&
5245 !inst
->src
[0].abs
) {
5246 for (int i
= 0; i
< 4; i
++) {
5247 if (inst
->dst
[0].writemask
& (1 << i
)) {
5248 acp
[4 * inst
->dst
[0].index
+ i
] = inst
;
5249 acp_level
[4 * inst
->dst
[0].index
+ i
] = level
;
5255 ralloc_free(acp_level
);
5260 dead_code_handle_reladdr(glsl_to_tgsi_instruction
**writes
, st_src_reg
*reladdr
)
5262 if (reladdr
&& reladdr
->file
== PROGRAM_TEMPORARY
) {
5263 /* Clear where it's used as src. */
5264 int swz
= GET_SWZ(reladdr
->swizzle
, 0);
5265 writes
[4 * reladdr
->index
+ swz
] = NULL
;
5270 * On a basic block basis, tracks available PROGRAM_TEMPORARY registers for dead
5273 * The glsl_to_tgsi_visitor lazily produces code assuming that this pass
5274 * will occur. As an example, a TXP production after copy propagation but
5277 * 0: MOV TEMP[1], INPUT[4].xyyy;
5278 * 1: MOV TEMP[1].w, INPUT[4].wwww;
5279 * 2: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
5281 * and after this pass:
5283 * 0: TXP TEMP[2], INPUT[4].xyyw, texture[0], 2D;
5286 glsl_to_tgsi_visitor::eliminate_dead_code(void)
5288 glsl_to_tgsi_instruction
**writes
= rzalloc_array(mem_ctx
,
5289 glsl_to_tgsi_instruction
*,
5290 this->next_temp
* 4);
5291 int *write_level
= rzalloc_array(mem_ctx
, int, this->next_temp
* 4);
5295 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5296 assert(inst
->dst
[0].file
!= PROGRAM_TEMPORARY
5297 || inst
->dst
[0].index
< this->next_temp
);
5300 case TGSI_OPCODE_BGNLOOP
:
5301 case TGSI_OPCODE_ENDLOOP
:
5302 case TGSI_OPCODE_CONT
:
5303 case TGSI_OPCODE_BRK
:
5304 /* End of a basic block, clear the write array entirely.
5306 * This keeps us from killing dead code when the writes are
5307 * on either side of a loop, even when the register isn't touched
5308 * inside the loop. However, glsl_to_tgsi_visitor doesn't seem to emit
5309 * dead code of this type, so it shouldn't make a difference as long as
5310 * the dead code elimination pass in the GLSL compiler does its job.
5312 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
5315 case TGSI_OPCODE_ENDIF
:
5316 case TGSI_OPCODE_ELSE
:
5317 /* Promote the recorded level of all channels written inside the
5318 * preceding if or else block to the level above the if/else block.
5320 for (int r
= 0; r
< this->next_temp
; r
++) {
5321 for (int c
= 0; c
< 4; c
++) {
5322 if (!writes
[4 * r
+ c
])
5325 if (write_level
[4 * r
+ c
] == level
)
5326 write_level
[4 * r
+ c
] = level
-1;
5329 if (inst
->op
== TGSI_OPCODE_ENDIF
)
5333 case TGSI_OPCODE_IF
:
5334 case TGSI_OPCODE_UIF
:
5336 /* fallthrough to default case to mark the condition as read */
5338 /* Continuing the block, clear any channels from the write array that
5339 * are read by this instruction.
5341 for (unsigned i
= 0; i
< ARRAY_SIZE(inst
->src
); i
++) {
5342 if (inst
->src
[i
].file
== PROGRAM_TEMPORARY
&& inst
->src
[i
].reladdr
){
5343 /* Any temporary might be read, so no dead code elimination
5344 * across this instruction.
5346 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
5347 } else if (inst
->src
[i
].file
== PROGRAM_TEMPORARY
) {
5348 /* Clear where it's used as src. */
5349 int src_chans
= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 0);
5350 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 1);
5351 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 2);
5352 src_chans
|= 1 << GET_SWZ(inst
->src
[i
].swizzle
, 3);
5354 for (int c
= 0; c
< 4; c
++) {
5355 if (src_chans
& (1 << c
))
5356 writes
[4 * inst
->src
[i
].index
+ c
] = NULL
;
5359 dead_code_handle_reladdr(writes
, inst
->src
[i
].reladdr
);
5360 dead_code_handle_reladdr(writes
, inst
->src
[i
].reladdr2
);
5362 for (unsigned i
= 0; i
< inst
->tex_offset_num_offset
; i
++) {
5363 if (inst
->tex_offsets
[i
].file
== PROGRAM_TEMPORARY
&& inst
->tex_offsets
[i
].reladdr
){
5364 /* Any temporary might be read, so no dead code elimination
5365 * across this instruction.
5367 memset(writes
, 0, sizeof(*writes
) * this->next_temp
* 4);
5368 } else if (inst
->tex_offsets
[i
].file
== PROGRAM_TEMPORARY
) {
5369 /* Clear where it's used as src. */
5370 int src_chans
= 1 << GET_SWZ(inst
->tex_offsets
[i
].swizzle
, 0);
5371 src_chans
|= 1 << GET_SWZ(inst
->tex_offsets
[i
].swizzle
, 1);
5372 src_chans
|= 1 << GET_SWZ(inst
->tex_offsets
[i
].swizzle
, 2);
5373 src_chans
|= 1 << GET_SWZ(inst
->tex_offsets
[i
].swizzle
, 3);
5375 for (int c
= 0; c
< 4; c
++) {
5376 if (src_chans
& (1 << c
))
5377 writes
[4 * inst
->tex_offsets
[i
].index
+ c
] = NULL
;
5380 dead_code_handle_reladdr(writes
, inst
->tex_offsets
[i
].reladdr
);
5381 dead_code_handle_reladdr(writes
, inst
->tex_offsets
[i
].reladdr2
);
5384 if (inst
->resource
.file
== PROGRAM_TEMPORARY
) {
5387 src_chans
= 1 << GET_SWZ(inst
->resource
.swizzle
, 0);
5388 src_chans
|= 1 << GET_SWZ(inst
->resource
.swizzle
, 1);
5389 src_chans
|= 1 << GET_SWZ(inst
->resource
.swizzle
, 2);
5390 src_chans
|= 1 << GET_SWZ(inst
->resource
.swizzle
, 3);
5392 for (int c
= 0; c
< 4; c
++) {
5393 if (src_chans
& (1 << c
))
5394 writes
[4 * inst
->resource
.index
+ c
] = NULL
;
5397 dead_code_handle_reladdr(writes
, inst
->resource
.reladdr
);
5398 dead_code_handle_reladdr(writes
, inst
->resource
.reladdr2
);
5400 for (unsigned i
= 0; i
< ARRAY_SIZE(inst
->dst
); i
++) {
5401 dead_code_handle_reladdr(writes
, inst
->dst
[i
].reladdr
);
5402 dead_code_handle_reladdr(writes
, inst
->dst
[i
].reladdr2
);
5407 /* If this instruction writes to a temporary, add it to the write array.
5408 * If there is already an instruction in the write array for one or more
5409 * of the channels, flag that channel write as dead.
5411 for (unsigned i
= 0; i
< ARRAY_SIZE(inst
->dst
); i
++) {
5412 if (inst
->dst
[i
].file
== PROGRAM_TEMPORARY
&&
5413 !inst
->dst
[i
].reladdr
) {
5414 for (int c
= 0; c
< 4; c
++) {
5415 if (inst
->dst
[i
].writemask
& (1 << c
)) {
5416 if (writes
[4 * inst
->dst
[i
].index
+ c
]) {
5417 if (write_level
[4 * inst
->dst
[i
].index
+ c
] < level
)
5420 writes
[4 * inst
->dst
[i
].index
+ c
]->dead_mask
|= (1 << c
);
5422 writes
[4 * inst
->dst
[i
].index
+ c
] = inst
;
5423 write_level
[4 * inst
->dst
[i
].index
+ c
] = level
;
5430 /* Anything still in the write array at this point is dead code. */
5431 for (int r
= 0; r
< this->next_temp
; r
++) {
5432 for (int c
= 0; c
< 4; c
++) {
5433 glsl_to_tgsi_instruction
*inst
= writes
[4 * r
+ c
];
5435 inst
->dead_mask
|= (1 << c
);
5439 /* Now actually remove the instructions that are completely dead and update
5440 * the writemask of other instructions with dead channels.
5442 foreach_in_list_safe(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5443 if (!inst
->dead_mask
|| !inst
->dst
[0].writemask
)
5445 /* No amount of dead masks should remove memory stores */
5446 if (inst
->info
->is_store
)
5449 if ((inst
->dst
[0].writemask
& ~inst
->dead_mask
) == 0) {
5454 if (glsl_base_type_is_64bit(inst
->dst
[0].type
)) {
5455 if (inst
->dead_mask
== WRITEMASK_XY
||
5456 inst
->dead_mask
== WRITEMASK_ZW
)
5457 inst
->dst
[0].writemask
&= ~(inst
->dead_mask
);
5459 inst
->dst
[0].writemask
&= ~(inst
->dead_mask
);
5463 ralloc_free(write_level
);
5464 ralloc_free(writes
);
5469 /* merge DFRACEXP instructions into one. */
5471 glsl_to_tgsi_visitor::merge_two_dsts(void)
5473 /* We never delete inst, but we may delete its successor. */
5474 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5475 glsl_to_tgsi_instruction
*inst2
;
5478 if (num_inst_dst_regs(inst
) != 2)
5481 if (inst
->dst
[0].file
!= PROGRAM_UNDEFINED
&&
5482 inst
->dst
[1].file
!= PROGRAM_UNDEFINED
)
5485 assert(inst
->dst
[0].file
!= PROGRAM_UNDEFINED
||
5486 inst
->dst
[1].file
!= PROGRAM_UNDEFINED
);
5488 if (inst
->dst
[0].file
== PROGRAM_UNDEFINED
)
5493 inst2
= (glsl_to_tgsi_instruction
*) inst
->next
;
5494 while (!inst2
->is_tail_sentinel()) {
5495 if (inst
->op
== inst2
->op
&&
5496 inst2
->dst
[defined
].file
== PROGRAM_UNDEFINED
&&
5497 inst
->src
[0].file
== inst2
->src
[0].file
&&
5498 inst
->src
[0].index
== inst2
->src
[0].index
&&
5499 inst
->src
[0].type
== inst2
->src
[0].type
&&
5500 inst
->src
[0].swizzle
== inst2
->src
[0].swizzle
)
5502 inst2
= (glsl_to_tgsi_instruction
*) inst2
->next
;
5505 if (inst2
->is_tail_sentinel()) {
5506 /* Undefined destinations are not allowed, substitute with an unused
5507 * temporary register.
5509 st_src_reg tmp
= get_temp(glsl_type::vec4_type
);
5510 inst
->dst
[defined
^ 1] = st_dst_reg(tmp
);
5511 inst
->dst
[defined
^ 1].writemask
= 0;
5515 inst
->dst
[defined
^ 1] = inst2
->dst
[defined
^ 1];
5521 template <typename st_reg
>
5522 void test_indirect_access(const st_reg
& reg
, bool *has_indirect_access
)
5524 if (reg
.file
== PROGRAM_ARRAY
) {
5525 if (reg
.reladdr
|| reg
.reladdr2
|| reg
.has_index2
) {
5526 has_indirect_access
[reg
.array_id
] = true;
5528 test_indirect_access(*reg
.reladdr
, has_indirect_access
);
5530 test_indirect_access(*reg
.reladdr2
, has_indirect_access
);
5535 template <typename st_reg
>
5536 void remap_array(st_reg
& reg
, const int *array_remap_info
,
5537 const bool *has_indirect_access
)
5539 if (reg
.file
== PROGRAM_ARRAY
) {
5540 if (!has_indirect_access
[reg
.array_id
]) {
5541 reg
.file
= PROGRAM_TEMPORARY
;
5542 reg
.index
= reg
.index
+ array_remap_info
[reg
.array_id
];
5545 reg
.array_id
= array_remap_info
[reg
.array_id
];
5549 remap_array(*reg
.reladdr
, array_remap_info
, has_indirect_access
);
5552 remap_array(*reg
.reladdr2
, array_remap_info
, has_indirect_access
);
5556 /* One-dimensional arrays whose elements are only accessed directly are
5557 * replaced by an according set of temporary registers that then can become
5558 * subject to further optimization steps like copy propagation and
5562 glsl_to_tgsi_visitor::split_arrays(void)
5567 bool *has_indirect_access
= rzalloc_array(mem_ctx
, bool, next_array
+ 1);
5569 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5570 for (unsigned j
= 0; j
< num_inst_src_regs(inst
); j
++)
5571 test_indirect_access(inst
->src
[j
], has_indirect_access
);
5573 for (unsigned j
= 0; j
< inst
->tex_offset_num_offset
; j
++)
5574 test_indirect_access(inst
->tex_offsets
[j
], has_indirect_access
);
5576 for (unsigned j
= 0; j
< num_inst_dst_regs(inst
); j
++)
5577 test_indirect_access(inst
->dst
[j
], has_indirect_access
);
5579 test_indirect_access(inst
->resource
, has_indirect_access
);
5582 unsigned array_offset
= 0;
5583 unsigned n_remaining_arrays
= 0;
5585 /* Double use: For arrays that get split this value will contain
5586 * the base index of the temporary registers this array is replaced
5587 * with. For arrays that remain it contains the new array ID.
5589 int *array_remap_info
= rzalloc_array(has_indirect_access
, int,
5592 for (unsigned i
= 1; i
<= next_array
; ++i
) {
5593 if (!has_indirect_access
[i
]) {
5594 array_remap_info
[i
] = this->next_temp
+ array_offset
;
5595 array_offset
+= array_sizes
[i
- 1];
5597 array_sizes
[n_remaining_arrays
] = array_sizes
[i
-1];
5598 array_remap_info
[i
] = ++n_remaining_arrays
;
5602 if (next_array
!= n_remaining_arrays
) {
5603 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &this->instructions
) {
5604 for (unsigned j
= 0; j
< num_inst_src_regs(inst
); j
++)
5605 remap_array(inst
->src
[j
], array_remap_info
, has_indirect_access
);
5607 for (unsigned j
= 0; j
< inst
->tex_offset_num_offset
; j
++)
5608 remap_array(inst
->tex_offsets
[j
], array_remap_info
, has_indirect_access
);
5610 for (unsigned j
= 0; j
< num_inst_dst_regs(inst
); j
++) {
5611 remap_array(inst
->dst
[j
], array_remap_info
, has_indirect_access
);
5613 remap_array(inst
->resource
, array_remap_info
, has_indirect_access
);
5617 ralloc_free(has_indirect_access
);
5618 this->next_temp
+= array_offset
;
5619 next_array
= n_remaining_arrays
;
5622 /* Merges temporary registers together where possible to reduce the number of
5623 * registers needed to run a program.
5625 * Produces optimal code only after copy propagation and dead code elimination
5628 glsl_to_tgsi_visitor::merge_registers(void)
5630 class array_live_range
*arr_live_ranges
= NULL
;
5632 struct register_live_range
*reg_live_ranges
=
5633 rzalloc_array(mem_ctx
, struct register_live_range
, this->next_temp
);
5635 if (this->next_array
> 0) {
5636 arr_live_ranges
= new array_live_range
[this->next_array
];
5637 for (unsigned i
= 0; i
< this->next_array
; ++i
)
5638 arr_live_ranges
[i
] = array_live_range(i
+1, this->array_sizes
[i
]);
5642 if (get_temp_registers_required_live_ranges(reg_live_ranges
, &this->instructions
,
5643 this->next_temp
, reg_live_ranges
,
5644 this->next_array
, arr_live_ranges
)) {
5645 struct rename_reg_pair
*renames
=
5646 rzalloc_array(reg_live_ranges
, struct rename_reg_pair
, this->next_temp
);
5647 get_temp_registers_remapping(reg_live_ranges
, this->next_temp
,
5648 reg_live_ranges
, renames
);
5649 rename_temp_registers(renames
);
5651 this->next_array
= merge_arrays(this->next_array
, this->array_sizes
,
5652 &this->instructions
, arr_live_ranges
);
5655 if (arr_live_ranges
)
5656 delete[] arr_live_ranges
;
5658 ralloc_free(reg_live_ranges
);
5661 /* Reassign indices to temporary registers by reusing unused indices created
5662 * by optimization passes. */
5664 glsl_to_tgsi_visitor::renumber_registers(void)
5668 int *first_writes
= ralloc_array(mem_ctx
, int, this->next_temp
);
5669 struct rename_reg_pair
*renames
= rzalloc_array(mem_ctx
, struct rename_reg_pair
, this->next_temp
);
5671 for (i
= 0; i
< this->next_temp
; i
++) {
5672 first_writes
[i
] = -1;
5674 get_first_temp_write(first_writes
);
5676 for (i
= 0; i
< this->next_temp
; i
++) {
5677 if (first_writes
[i
] < 0) continue;
5678 if (i
!= new_index
) {
5679 renames
[i
].new_reg
= new_index
;
5680 renames
[i
].valid
= true;
5685 rename_temp_registers(renames
);
5686 this->next_temp
= new_index
;
5687 ralloc_free(renames
);
5688 ralloc_free(first_writes
);
5692 void glsl_to_tgsi_visitor::print_stats()
5694 int narray_registers
= 0;
5695 for (unsigned i
= 0; i
< this->next_array
; ++i
)
5696 narray_registers
+= this->array_sizes
[i
];
5698 int ninstructions
= 0;
5699 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &instructions
) {
5703 simple_mtx_lock(&print_stats_mutex
);
5704 stats_log
<< next_array
<< ", "
5705 << next_temp
<< ", "
5706 << narray_registers
<< ", "
5707 << next_temp
+ narray_registers
<< ", "
5708 << ninstructions
<< "\n";
5709 simple_mtx_unlock(&print_stats_mutex
);
5712 /* ------------------------- TGSI conversion stuff -------------------------- */
5715 * Intermediate state used during shader translation.
5717 struct st_translate
{
5718 struct ureg_program
*ureg
;
5720 unsigned temps_size
;
5721 struct ureg_dst
*temps
;
5723 struct ureg_dst
*arrays
;
5724 unsigned num_temp_arrays
;
5725 struct ureg_src
*constants
;
5727 struct ureg_src
*immediates
;
5729 struct ureg_dst outputs
[PIPE_MAX_SHADER_OUTPUTS
];
5730 struct ureg_src inputs
[PIPE_MAX_SHADER_INPUTS
];
5731 struct ureg_dst address
[3];
5732 struct ureg_src samplers
[PIPE_MAX_SAMPLERS
];
5733 struct ureg_src buffers
[PIPE_MAX_SHADER_BUFFERS
];
5734 struct ureg_src images
[PIPE_MAX_SHADER_IMAGES
];
5735 struct ureg_src systemValues
[SYSTEM_VALUE_MAX
];
5736 struct ureg_src hw_atomics
[PIPE_MAX_HW_ATOMIC_BUFFERS
];
5737 struct ureg_src shared_memory
;
5738 unsigned *array_sizes
;
5739 struct inout_decl
*input_decls
;
5740 unsigned num_input_decls
;
5741 struct inout_decl
*output_decls
;
5742 unsigned num_output_decls
;
5744 const ubyte
*inputMapping
;
5745 const ubyte
*outputMapping
;
5747 enum pipe_shader_type procType
; /**< PIPE_SHADER_VERTEX/FRAGMENT */
5751 /** Map Mesa's SYSTEM_VALUE_x to TGSI_SEMANTIC_x */
5753 _mesa_sysval_to_semantic(unsigned sysval
)
5757 case SYSTEM_VALUE_VERTEX_ID
:
5758 return TGSI_SEMANTIC_VERTEXID
;
5759 case SYSTEM_VALUE_INSTANCE_ID
:
5760 return TGSI_SEMANTIC_INSTANCEID
;
5761 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
5762 return TGSI_SEMANTIC_VERTEXID_NOBASE
;
5763 case SYSTEM_VALUE_BASE_VERTEX
:
5764 return TGSI_SEMANTIC_BASEVERTEX
;
5765 case SYSTEM_VALUE_BASE_INSTANCE
:
5766 return TGSI_SEMANTIC_BASEINSTANCE
;
5767 case SYSTEM_VALUE_DRAW_ID
:
5768 return TGSI_SEMANTIC_DRAWID
;
5770 /* Geometry shader */
5771 case SYSTEM_VALUE_INVOCATION_ID
:
5772 return TGSI_SEMANTIC_INVOCATIONID
;
5774 /* Fragment shader */
5775 case SYSTEM_VALUE_FRAG_COORD
:
5776 return TGSI_SEMANTIC_POSITION
;
5777 case SYSTEM_VALUE_FRONT_FACE
:
5778 return TGSI_SEMANTIC_FACE
;
5779 case SYSTEM_VALUE_SAMPLE_ID
:
5780 return TGSI_SEMANTIC_SAMPLEID
;
5781 case SYSTEM_VALUE_SAMPLE_POS
:
5782 return TGSI_SEMANTIC_SAMPLEPOS
;
5783 case SYSTEM_VALUE_SAMPLE_MASK_IN
:
5784 return TGSI_SEMANTIC_SAMPLEMASK
;
5785 case SYSTEM_VALUE_HELPER_INVOCATION
:
5786 return TGSI_SEMANTIC_HELPER_INVOCATION
;
5788 /* Tessellation shader */
5789 case SYSTEM_VALUE_TESS_COORD
:
5790 return TGSI_SEMANTIC_TESSCOORD
;
5791 case SYSTEM_VALUE_VERTICES_IN
:
5792 return TGSI_SEMANTIC_VERTICESIN
;
5793 case SYSTEM_VALUE_PRIMITIVE_ID
:
5794 return TGSI_SEMANTIC_PRIMID
;
5795 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
5796 return TGSI_SEMANTIC_TESSOUTER
;
5797 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
5798 return TGSI_SEMANTIC_TESSINNER
;
5800 /* Compute shader */
5801 case SYSTEM_VALUE_LOCAL_INVOCATION_ID
:
5802 return TGSI_SEMANTIC_THREAD_ID
;
5803 case SYSTEM_VALUE_WORK_GROUP_ID
:
5804 return TGSI_SEMANTIC_BLOCK_ID
;
5805 case SYSTEM_VALUE_NUM_WORK_GROUPS
:
5806 return TGSI_SEMANTIC_GRID_SIZE
;
5807 case SYSTEM_VALUE_LOCAL_GROUP_SIZE
:
5808 return TGSI_SEMANTIC_BLOCK_SIZE
;
5810 /* ARB_shader_ballot */
5811 case SYSTEM_VALUE_SUBGROUP_SIZE
:
5812 return TGSI_SEMANTIC_SUBGROUP_SIZE
;
5813 case SYSTEM_VALUE_SUBGROUP_INVOCATION
:
5814 return TGSI_SEMANTIC_SUBGROUP_INVOCATION
;
5815 case SYSTEM_VALUE_SUBGROUP_EQ_MASK
:
5816 return TGSI_SEMANTIC_SUBGROUP_EQ_MASK
;
5817 case SYSTEM_VALUE_SUBGROUP_GE_MASK
:
5818 return TGSI_SEMANTIC_SUBGROUP_GE_MASK
;
5819 case SYSTEM_VALUE_SUBGROUP_GT_MASK
:
5820 return TGSI_SEMANTIC_SUBGROUP_GT_MASK
;
5821 case SYSTEM_VALUE_SUBGROUP_LE_MASK
:
5822 return TGSI_SEMANTIC_SUBGROUP_LE_MASK
;
5823 case SYSTEM_VALUE_SUBGROUP_LT_MASK
:
5824 return TGSI_SEMANTIC_SUBGROUP_LT_MASK
;
5827 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
:
5828 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID
:
5829 case SYSTEM_VALUE_VERTEX_CNT
:
5830 case SYSTEM_VALUE_VARYING_COORD
:
5832 assert(!"Unexpected SYSTEM_VALUE_ enum");
5833 return TGSI_SEMANTIC_COUNT
;
5838 * Map a glsl_to_tgsi constant/immediate to a TGSI immediate.
5840 static struct ureg_src
5841 emit_immediate(struct st_translate
*t
,
5842 gl_constant_value values
[4],
5843 GLenum type
, int size
)
5845 struct ureg_program
*ureg
= t
->ureg
;
5849 return ureg_DECL_immediate(ureg
, &values
[0].f
, size
);
5851 return ureg_DECL_immediate_f64(ureg
, (double *)&values
[0].f
, size
);
5853 return ureg_DECL_immediate_int64(ureg
, (int64_t *)&values
[0].f
, size
);
5854 case GL_UNSIGNED_INT64_ARB
:
5855 return ureg_DECL_immediate_uint64(ureg
, (uint64_t *)&values
[0].f
, size
);
5857 return ureg_DECL_immediate_int(ureg
, &values
[0].i
, size
);
5858 case GL_UNSIGNED_INT
:
5860 return ureg_DECL_immediate_uint(ureg
, &values
[0].u
, size
);
5862 assert(!"should not get here - type must be float, int, uint, or bool");
5863 return ureg_src_undef();
5868 * Map a glsl_to_tgsi dst register to a TGSI ureg_dst register.
5870 static struct ureg_dst
5871 dst_register(struct st_translate
*t
, gl_register_file file
, unsigned index
,
5877 case PROGRAM_UNDEFINED
:
5878 return ureg_dst_undef();
5880 case PROGRAM_TEMPORARY
:
5881 /* Allocate space for temporaries on demand. */
5882 if (index
>= t
->temps_size
) {
5883 const int inc
= align(index
- t
->temps_size
+ 1, 4096);
5885 t
->temps
= (struct ureg_dst
*)
5887 (t
->temps_size
+ inc
) * sizeof(struct ureg_dst
));
5889 return ureg_dst_undef();
5891 memset(t
->temps
+ t
->temps_size
, 0, inc
* sizeof(struct ureg_dst
));
5892 t
->temps_size
+= inc
;
5895 if (ureg_dst_is_undef(t
->temps
[index
]))
5896 t
->temps
[index
] = ureg_DECL_local_temporary(t
->ureg
);
5898 return t
->temps
[index
];
5901 assert(array_id
&& array_id
<= t
->num_temp_arrays
);
5902 array
= array_id
- 1;
5904 if (ureg_dst_is_undef(t
->arrays
[array
]))
5905 t
->arrays
[array
] = ureg_DECL_array_temporary(
5906 t
->ureg
, t
->array_sizes
[array
], TRUE
);
5908 return ureg_dst_array_offset(t
->arrays
[array
], index
);
5910 case PROGRAM_OUTPUT
:
5912 if (t
->procType
== PIPE_SHADER_FRAGMENT
)
5913 assert(index
< 2 * FRAG_RESULT_MAX
);
5914 else if (t
->procType
== PIPE_SHADER_TESS_CTRL
||
5915 t
->procType
== PIPE_SHADER_TESS_EVAL
)
5916 assert(index
< VARYING_SLOT_TESS_MAX
);
5918 assert(index
< VARYING_SLOT_MAX
);
5920 assert(t
->outputMapping
[index
] < ARRAY_SIZE(t
->outputs
));
5921 assert(t
->outputs
[t
->outputMapping
[index
]].File
!= TGSI_FILE_NULL
);
5922 return t
->outputs
[t
->outputMapping
[index
]];
5925 struct inout_decl
*decl
=
5926 find_inout_array(t
->output_decls
,
5927 t
->num_output_decls
, array_id
);
5928 unsigned mesa_index
= decl
->mesa_index
;
5929 int slot
= t
->outputMapping
[mesa_index
];
5931 assert(slot
!= -1 && t
->outputs
[slot
].File
== TGSI_FILE_OUTPUT
);
5933 struct ureg_dst dst
= t
->outputs
[slot
];
5934 dst
.ArrayID
= array_id
;
5935 return ureg_dst_array_offset(dst
, index
- mesa_index
);
5938 case PROGRAM_ADDRESS
:
5939 return t
->address
[index
];
5942 assert(!"unknown dst register file");
5943 return ureg_dst_undef();
5947 static struct ureg_src
5948 translate_src(struct st_translate
*t
, const st_src_reg
*src_reg
);
5950 static struct ureg_src
5951 translate_addr(struct st_translate
*t
, const st_src_reg
*reladdr
,
5952 unsigned addr_index
)
5954 if (t
->need_uarl
|| !reladdr
->is_legal_tgsi_address_operand())
5955 return ureg_src(t
->address
[addr_index
]);
5957 return translate_src(t
, reladdr
);
5961 * Create a TGSI ureg_dst register from an st_dst_reg.
5963 static struct ureg_dst
5964 translate_dst(struct st_translate
*t
,
5965 const st_dst_reg
*dst_reg
,
5968 struct ureg_dst dst
= dst_register(t
, dst_reg
->file
, dst_reg
->index
,
5971 if (dst
.File
== TGSI_FILE_NULL
)
5974 dst
= ureg_writemask(dst
, dst_reg
->writemask
);
5977 dst
= ureg_saturate(dst
);
5979 if (dst_reg
->reladdr
!= NULL
) {
5980 assert(dst_reg
->file
!= PROGRAM_TEMPORARY
);
5981 dst
= ureg_dst_indirect(dst
, translate_addr(t
, dst_reg
->reladdr
, 0));
5984 if (dst_reg
->has_index2
) {
5985 if (dst_reg
->reladdr2
)
5986 dst
= ureg_dst_dimension_indirect(dst
,
5987 translate_addr(t
, dst_reg
->reladdr2
, 1),
5990 dst
= ureg_dst_dimension(dst
, dst_reg
->index2D
);
5997 * Create a TGSI ureg_src register from an st_src_reg.
5999 static struct ureg_src
6000 translate_src(struct st_translate
*t
, const st_src_reg
*src_reg
)
6002 struct ureg_src src
;
6003 int index
= src_reg
->index
;
6004 int double_reg2
= src_reg
->double_reg2
? 1 : 0;
6006 switch (src_reg
->file
) {
6007 case PROGRAM_UNDEFINED
:
6008 src
= ureg_imm4f(t
->ureg
, 0, 0, 0, 0);
6011 case PROGRAM_TEMPORARY
:
6013 src
= ureg_src(dst_register(t
, src_reg
->file
, src_reg
->index
,
6014 src_reg
->array_id
));
6017 case PROGRAM_OUTPUT
: {
6018 struct ureg_dst dst
= dst_register(t
, src_reg
->file
, src_reg
->index
,
6020 assert(dst
.WriteMask
!= 0);
6021 unsigned shift
= ffs(dst
.WriteMask
) - 1;
6022 src
= ureg_swizzle(ureg_src(dst
),
6026 MIN2(shift
+ 3, 3));
6030 case PROGRAM_UNIFORM
:
6031 assert(src_reg
->index
>= 0);
6032 src
= src_reg
->index
< t
->num_constants
?
6033 t
->constants
[src_reg
->index
] : ureg_imm4f(t
->ureg
, 0, 0, 0, 0);
6035 case PROGRAM_STATE_VAR
:
6036 case PROGRAM_CONSTANT
: /* ie, immediate */
6037 if (src_reg
->has_index2
)
6038 src
= ureg_src_register(TGSI_FILE_CONSTANT
, src_reg
->index
);
6040 src
= src_reg
->index
>= 0 && src_reg
->index
< t
->num_constants
?
6041 t
->constants
[src_reg
->index
] : ureg_imm4f(t
->ureg
, 0, 0, 0, 0);
6044 case PROGRAM_IMMEDIATE
:
6045 assert(src_reg
->index
>= 0 && src_reg
->index
< t
->num_immediates
);
6046 src
= t
->immediates
[src_reg
->index
];
6050 /* GLSL inputs are 64-bit containers, so we have to
6051 * map back to the original index and add the offset after
6053 index
-= double_reg2
;
6054 if (!src_reg
->array_id
) {
6055 assert(t
->inputMapping
[index
] < ARRAY_SIZE(t
->inputs
));
6056 assert(t
->inputs
[t
->inputMapping
[index
]].File
!= TGSI_FILE_NULL
);
6057 src
= t
->inputs
[t
->inputMapping
[index
] + double_reg2
];
6060 struct inout_decl
*decl
= find_inout_array(t
->input_decls
,
6063 unsigned mesa_index
= decl
->mesa_index
;
6064 int slot
= t
->inputMapping
[mesa_index
];
6066 assert(slot
!= -1 && t
->inputs
[slot
].File
== TGSI_FILE_INPUT
);
6068 src
= t
->inputs
[slot
];
6069 src
.ArrayID
= src_reg
->array_id
;
6070 src
= ureg_src_array_offset(src
, index
+ double_reg2
- mesa_index
);
6074 case PROGRAM_ADDRESS
:
6075 src
= ureg_src(t
->address
[src_reg
->index
]);
6078 case PROGRAM_SYSTEM_VALUE
:
6079 assert(src_reg
->index
< (int) ARRAY_SIZE(t
->systemValues
));
6080 src
= t
->systemValues
[src_reg
->index
];
6083 case PROGRAM_HW_ATOMIC
:
6084 src
= ureg_src_array_register(TGSI_FILE_HW_ATOMIC
, src_reg
->index
,
6089 assert(!"unknown src register file");
6090 return ureg_src_undef();
6093 if (src_reg
->has_index2
) {
6094 /* 2D indexes occur with geometry shader inputs (attrib, vertex)
6095 * and UBO constant buffers (buffer, position).
6097 if (src_reg
->reladdr2
)
6098 src
= ureg_src_dimension_indirect(src
,
6099 translate_addr(t
, src_reg
->reladdr2
, 1),
6102 src
= ureg_src_dimension(src
, src_reg
->index2D
);
6105 src
= ureg_swizzle(src
,
6106 GET_SWZ(src_reg
->swizzle
, 0) & 0x3,
6107 GET_SWZ(src_reg
->swizzle
, 1) & 0x3,
6108 GET_SWZ(src_reg
->swizzle
, 2) & 0x3,
6109 GET_SWZ(src_reg
->swizzle
, 3) & 0x3);
6112 src
= ureg_abs(src
);
6114 if ((src_reg
->negate
& 0xf) == NEGATE_XYZW
)
6115 src
= ureg_negate(src
);
6117 if (src_reg
->reladdr
!= NULL
) {
6118 assert(src_reg
->file
!= PROGRAM_TEMPORARY
);
6119 src
= ureg_src_indirect(src
, translate_addr(t
, src_reg
->reladdr
, 0));
6125 static struct tgsi_texture_offset
6126 translate_tex_offset(struct st_translate
*t
,
6127 const st_src_reg
*in_offset
)
6129 struct tgsi_texture_offset offset
;
6130 struct ureg_src src
= translate_src(t
, in_offset
);
6132 offset
.File
= src
.File
;
6133 offset
.Index
= src
.Index
;
6134 offset
.SwizzleX
= src
.SwizzleX
;
6135 offset
.SwizzleY
= src
.SwizzleY
;
6136 offset
.SwizzleZ
= src
.SwizzleZ
;
6139 assert(!src
.Indirect
);
6140 assert(!src
.DimIndirect
);
6141 assert(!src
.Dimension
);
6142 assert(!src
.Absolute
); /* those shouldn't be used with integers anyway */
6143 assert(!src
.Negate
);
6149 compile_tgsi_instruction(struct st_translate
*t
,
6150 const glsl_to_tgsi_instruction
*inst
)
6152 struct ureg_program
*ureg
= t
->ureg
;
6154 struct ureg_dst dst
[2];
6155 struct ureg_src src
[4];
6156 struct tgsi_texture_offset texoffsets
[MAX_GLSL_TEXTURE_OFFSET
];
6160 enum tgsi_texture_type tex_target
= TGSI_TEXTURE_BUFFER
;
6162 num_dst
= num_inst_dst_regs(inst
);
6163 num_src
= num_inst_src_regs(inst
);
6165 for (i
= 0; i
< num_dst
; i
++)
6166 dst
[i
] = translate_dst(t
,
6170 for (i
= 0; i
< num_src
; i
++)
6171 src
[i
] = translate_src(t
, &inst
->src
[i
]);
6174 case TGSI_OPCODE_BGNLOOP
:
6175 case TGSI_OPCODE_ELSE
:
6176 case TGSI_OPCODE_ENDLOOP
:
6177 case TGSI_OPCODE_IF
:
6178 case TGSI_OPCODE_UIF
:
6179 assert(num_dst
== 0);
6180 ureg_insn(ureg
, inst
->op
, NULL
, 0, src
, num_src
, inst
->precise
);
6183 case TGSI_OPCODE_TEX
:
6184 case TGSI_OPCODE_TEX_LZ
:
6185 case TGSI_OPCODE_TXB
:
6186 case TGSI_OPCODE_TXD
:
6187 case TGSI_OPCODE_TXL
:
6188 case TGSI_OPCODE_TXP
:
6189 case TGSI_OPCODE_TXQ
:
6190 case TGSI_OPCODE_TXQS
:
6191 case TGSI_OPCODE_TXF
:
6192 case TGSI_OPCODE_TXF_LZ
:
6193 case TGSI_OPCODE_TEX2
:
6194 case TGSI_OPCODE_TXB2
:
6195 case TGSI_OPCODE_TXL2
:
6196 case TGSI_OPCODE_TG4
:
6197 case TGSI_OPCODE_LODQ
:
6198 case TGSI_OPCODE_SAMP2HND
:
6199 if (inst
->resource
.file
== PROGRAM_SAMPLER
) {
6200 src
[num_src
] = t
->samplers
[inst
->resource
.index
];
6202 /* Bindless samplers. */
6203 src
[num_src
] = translate_src(t
, &inst
->resource
);
6205 assert(src
[num_src
].File
!= TGSI_FILE_NULL
);
6206 if (inst
->resource
.reladdr
)
6208 ureg_src_indirect(src
[num_src
],
6209 translate_addr(t
, inst
->resource
.reladdr
, 2));
6211 for (i
= 0; i
< (int)inst
->tex_offset_num_offset
; i
++) {
6212 texoffsets
[i
] = translate_tex_offset(t
, &inst
->tex_offsets
[i
]);
6214 tex_target
= st_translate_texture_target(inst
->tex_target
, inst
->tex_shadow
);
6220 st_translate_texture_type(inst
->tex_type
),
6221 texoffsets
, inst
->tex_offset_num_offset
,
6225 case TGSI_OPCODE_RESQ
:
6226 case TGSI_OPCODE_LOAD
:
6227 case TGSI_OPCODE_ATOMUADD
:
6228 case TGSI_OPCODE_ATOMXCHG
:
6229 case TGSI_OPCODE_ATOMCAS
:
6230 case TGSI_OPCODE_ATOMAND
:
6231 case TGSI_OPCODE_ATOMOR
:
6232 case TGSI_OPCODE_ATOMXOR
:
6233 case TGSI_OPCODE_ATOMUMIN
:
6234 case TGSI_OPCODE_ATOMUMAX
:
6235 case TGSI_OPCODE_ATOMIMIN
:
6236 case TGSI_OPCODE_ATOMIMAX
:
6237 case TGSI_OPCODE_ATOMFADD
:
6238 case TGSI_OPCODE_IMG2HND
:
6239 for (i
= num_src
- 1; i
>= 0; i
--)
6240 src
[i
+ 1] = src
[i
];
6242 if (inst
->resource
.file
== PROGRAM_MEMORY
) {
6243 src
[0] = t
->shared_memory
;
6244 } else if (inst
->resource
.file
== PROGRAM_BUFFER
) {
6245 src
[0] = t
->buffers
[inst
->resource
.index
];
6246 } else if (inst
->resource
.file
== PROGRAM_HW_ATOMIC
) {
6247 src
[0] = translate_src(t
, &inst
->resource
);
6248 } else if (inst
->resource
.file
== PROGRAM_CONSTANT
) {
6249 assert(inst
->resource
.has_index2
);
6250 src
[0] = ureg_src_register(TGSI_FILE_CONSTBUF
, inst
->resource
.index
);
6252 assert(inst
->resource
.file
!= PROGRAM_UNDEFINED
);
6253 if (inst
->resource
.file
== PROGRAM_IMAGE
) {
6254 src
[0] = t
->images
[inst
->resource
.index
];
6256 /* Bindless images. */
6257 src
[0] = translate_src(t
, &inst
->resource
);
6259 tex_target
= st_translate_texture_target(inst
->tex_target
, inst
->tex_shadow
);
6261 if (inst
->resource
.reladdr
)
6262 src
[0] = ureg_src_indirect(src
[0],
6263 translate_addr(t
, inst
->resource
.reladdr
, 2));
6264 assert(src
[0].File
!= TGSI_FILE_NULL
);
6265 ureg_memory_insn(ureg
, inst
->op
, dst
, num_dst
, src
, num_src
,
6266 inst
->buffer_access
,
6267 tex_target
, inst
->image_format
);
6270 case TGSI_OPCODE_STORE
:
6271 if (inst
->resource
.file
== PROGRAM_MEMORY
) {
6272 dst
[0] = ureg_dst(t
->shared_memory
);
6273 } else if (inst
->resource
.file
== PROGRAM_BUFFER
) {
6274 dst
[0] = ureg_dst(t
->buffers
[inst
->resource
.index
]);
6276 if (inst
->resource
.file
== PROGRAM_IMAGE
) {
6277 dst
[0] = ureg_dst(t
->images
[inst
->resource
.index
]);
6279 /* Bindless images. */
6280 dst
[0] = ureg_dst(translate_src(t
, &inst
->resource
));
6282 tex_target
= st_translate_texture_target(inst
->tex_target
, inst
->tex_shadow
);
6284 dst
[0] = ureg_writemask(dst
[0], inst
->dst
[0].writemask
);
6285 if (inst
->resource
.reladdr
)
6286 dst
[0] = ureg_dst_indirect(dst
[0],
6287 translate_addr(t
, inst
->resource
.reladdr
, 2));
6288 assert(dst
[0].File
!= TGSI_FILE_NULL
);
6289 ureg_memory_insn(ureg
, inst
->op
, dst
, num_dst
, src
, num_src
,
6290 inst
->buffer_access
,
6291 tex_target
, inst
->image_format
);
6298 src
, num_src
, inst
->precise
);
6303 /* Invert SamplePos.y when rendering to the default framebuffer. */
6305 emit_samplepos_adjustment(struct st_translate
*t
, int wpos_y_transform
)
6307 struct ureg_program
*ureg
= t
->ureg
;
6309 assert(wpos_y_transform
>= 0);
6310 struct ureg_src trans_const
= ureg_DECL_constant(ureg
, wpos_y_transform
);
6311 struct ureg_src samplepos_sysval
= t
->systemValues
[SYSTEM_VALUE_SAMPLE_POS
];
6312 struct ureg_dst samplepos_flipped
= ureg_DECL_temporary(ureg
);
6313 struct ureg_dst is_fbo
= ureg_DECL_temporary(ureg
);
6315 ureg_ADD(ureg
, ureg_writemask(samplepos_flipped
, TGSI_WRITEMASK_Y
),
6316 ureg_imm1f(ureg
, 1), ureg_negate(samplepos_sysval
));
6318 /* If trans.x == 1, use samplepos.y, else use 1 - samplepos.y. */
6319 ureg_FSEQ(ureg
, ureg_writemask(is_fbo
, TGSI_WRITEMASK_Y
),
6320 ureg_scalar(trans_const
, TGSI_SWIZZLE_X
), ureg_imm1f(ureg
, 1));
6321 ureg_UCMP(ureg
, ureg_writemask(samplepos_flipped
, TGSI_WRITEMASK_Y
),
6322 ureg_src(is_fbo
), samplepos_sysval
, ureg_src(samplepos_flipped
));
6323 ureg_MOV(ureg
, ureg_writemask(samplepos_flipped
, TGSI_WRITEMASK_X
),
6326 /* Use the result in place of the system value. */
6327 t
->systemValues
[SYSTEM_VALUE_SAMPLE_POS
] = ureg_src(samplepos_flipped
);
6332 * Emit the TGSI instructions for inverting and adjusting WPOS.
6333 * This code is unavoidable because it also depends on whether
6334 * a FBO is bound (STATE_FB_WPOS_Y_TRANSFORM).
6337 emit_wpos_adjustment(struct gl_context
*ctx
,
6338 struct st_translate
*t
,
6339 int wpos_transform_const
,
6341 GLfloat adjX
, GLfloat adjY
[2])
6343 struct ureg_program
*ureg
= t
->ureg
;
6345 assert(wpos_transform_const
>= 0);
6347 /* Fragment program uses fragment position input.
6348 * Need to replace instances of INPUT[WPOS] with temp T
6349 * where T = INPUT[WPOS] is inverted by Y.
6351 struct ureg_src wpostrans
= ureg_DECL_constant(ureg
, wpos_transform_const
);
6352 struct ureg_dst wpos_temp
= ureg_DECL_temporary(ureg
);
6353 struct ureg_src
*wpos
=
6354 ctx
->Const
.GLSLFragCoordIsSysVal
?
6355 &t
->systemValues
[SYSTEM_VALUE_FRAG_COORD
] :
6356 &t
->inputs
[t
->inputMapping
[VARYING_SLOT_POS
]];
6357 struct ureg_src wpos_input
= *wpos
;
6359 /* First, apply the coordinate shift: */
6360 if (adjX
|| adjY
[0] || adjY
[1]) {
6361 if (adjY
[0] != adjY
[1]) {
6362 /* Adjust the y coordinate by adjY[1] or adjY[0] respectively
6363 * depending on whether inversion is actually going to be applied
6364 * or not, which is determined by testing against the inversion
6365 * state variable used below, which will be either +1 or -1.
6367 struct ureg_dst adj_temp
= ureg_DECL_local_temporary(ureg
);
6369 ureg_CMP(ureg
, adj_temp
,
6370 ureg_scalar(wpostrans
, invert
? 2 : 0),
6371 ureg_imm4f(ureg
, adjX
, adjY
[0], 0.0f
, 0.0f
),
6372 ureg_imm4f(ureg
, adjX
, adjY
[1], 0.0f
, 0.0f
));
6373 ureg_ADD(ureg
, wpos_temp
, wpos_input
, ureg_src(adj_temp
));
6375 ureg_ADD(ureg
, wpos_temp
, wpos_input
,
6376 ureg_imm4f(ureg
, adjX
, adjY
[0], 0.0f
, 0.0f
));
6378 wpos_input
= ureg_src(wpos_temp
);
6380 /* MOV wpos_temp, input[wpos]
6382 ureg_MOV(ureg
, wpos_temp
, wpos_input
);
6385 /* Now the conditional y flip: STATE_FB_WPOS_Y_TRANSFORM.xy/zw will be
6386 * inversion/identity, or the other way around if we're drawing to an FBO.
6389 /* MAD wpos_temp.y, wpos_input, wpostrans.xxxx, wpostrans.yyyy
6392 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
6394 ureg_scalar(wpostrans
, 0),
6395 ureg_scalar(wpostrans
, 1));
6397 /* MAD wpos_temp.y, wpos_input, wpostrans.zzzz, wpostrans.wwww
6400 ureg_writemask(wpos_temp
, TGSI_WRITEMASK_Y
),
6402 ureg_scalar(wpostrans
, 2),
6403 ureg_scalar(wpostrans
, 3));
6406 /* Use wpos_temp as position input from here on:
6408 *wpos
= ureg_src(wpos_temp
);
6413 * Emit fragment position/ooordinate code.
6416 emit_wpos(struct st_context
*st
,
6417 struct st_translate
*t
,
6418 const struct gl_program
*program
,
6419 struct ureg_program
*ureg
,
6420 int wpos_transform_const
)
6422 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
6423 GLfloat adjX
= 0.0f
;
6424 GLfloat adjY
[2] = { 0.0f
, 0.0f
};
6425 boolean invert
= FALSE
;
6427 /* Query the pixel center conventions supported by the pipe driver and set
6428 * adjX, adjY to help out if it cannot handle the requested one internally.
6430 * The bias of the y-coordinate depends on whether y-inversion takes place
6431 * (adjY[1]) or not (adjY[0]), which is in turn dependent on whether we are
6432 * drawing to an FBO (causes additional inversion), and whether the pipe
6433 * driver origin and the requested origin differ (the latter condition is
6434 * stored in the 'invert' variable).
6436 * For height = 100 (i = integer, h = half-integer, l = lower, u = upper):
6438 * center shift only:
6443 * l,i -> u,i: ( 0.0 + 1.0) * -1 + 100 = 99
6444 * l,h -> u,h: ( 0.5 + 0.0) * -1 + 100 = 99.5
6445 * u,i -> l,i: (99.0 + 1.0) * -1 + 100 = 0
6446 * u,h -> l,h: (99.5 + 0.0) * -1 + 100 = 0.5
6448 * inversion and center shift:
6449 * l,i -> u,h: ( 0.0 + 0.5) * -1 + 100 = 99.5
6450 * l,h -> u,i: ( 0.5 + 0.5) * -1 + 100 = 99
6451 * u,i -> l,h: (99.0 + 0.5) * -1 + 100 = 0.5
6452 * u,h -> l,i: (99.5 + 0.5) * -1 + 100 = 0
6454 if (program
->info
.fs
.origin_upper_left
) {
6455 /* Fragment shader wants origin in upper-left */
6456 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
)) {
6457 /* the driver supports upper-left origin */
6459 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
)) {
6460 /* the driver supports lower-left origin, need to invert Y */
6461 ureg_property(ureg
, TGSI_PROPERTY_FS_COORD_ORIGIN
,
6462 TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
6469 /* Fragment shader wants origin in lower-left */
6470 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
))
6471 /* the driver supports lower-left origin */
6472 ureg_property(ureg
, TGSI_PROPERTY_FS_COORD_ORIGIN
,
6473 TGSI_FS_COORD_ORIGIN_LOWER_LEFT
);
6474 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
))
6475 /* the driver supports upper-left origin, need to invert Y */
6481 if (program
->info
.fs
.pixel_center_integer
) {
6482 /* Fragment shader wants pixel center integer */
6483 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)) {
6484 /* the driver supports pixel center integer */
6486 ureg_property(ureg
, TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
,
6487 TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
6489 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
)) {
6490 /* the driver supports pixel center half integer, need to bias X,Y */
6499 /* Fragment shader wants pixel center half integer */
6500 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
)) {
6501 /* the driver supports pixel center half integer */
6503 else if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)) {
6504 /* the driver supports pixel center integer, need to bias X,Y */
6505 adjX
= adjY
[0] = adjY
[1] = 0.5f
;
6506 ureg_property(ureg
, TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
,
6507 TGSI_FS_COORD_PIXEL_CENTER_INTEGER
);
6513 /* we invert after adjustment so that we avoid the MOV to temporary,
6514 * and reuse the adjustment ADD instead */
6515 emit_wpos_adjustment(st
->ctx
, t
, wpos_transform_const
, invert
, adjX
, adjY
);
6519 * OpenGL's fragment gl_FrontFace input is 1 for front-facing, 0 for back.
6520 * TGSI uses +1 for front, -1 for back.
6521 * This function converts the TGSI value to the GL value. Simply clamping/
6522 * saturating the value to [0,1] does the job.
6525 emit_face_var(struct gl_context
*ctx
, struct st_translate
*t
)
6527 struct ureg_program
*ureg
= t
->ureg
;
6528 struct ureg_dst face_temp
= ureg_DECL_temporary(ureg
);
6529 struct ureg_src face_input
= t
->inputs
[t
->inputMapping
[VARYING_SLOT_FACE
]];
6531 if (ctx
->Const
.NativeIntegers
) {
6532 ureg_FSGE(ureg
, face_temp
, face_input
, ureg_imm1f(ureg
, 0));
6535 /* MOV_SAT face_temp, input[face] */
6536 ureg_MOV(ureg
, ureg_saturate(face_temp
), face_input
);
6539 /* Use face_temp as face input from here on: */
6540 t
->inputs
[t
->inputMapping
[VARYING_SLOT_FACE
]] = ureg_src(face_temp
);
6544 emit_compute_block_size(const struct gl_program
*prog
,
6545 struct ureg_program
*ureg
) {
6546 ureg_property(ureg
, TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
,
6547 prog
->info
.cs
.local_size
[0]);
6548 ureg_property(ureg
, TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT
,
6549 prog
->info
.cs
.local_size
[1]);
6550 ureg_property(ureg
, TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH
,
6551 prog
->info
.cs
.local_size
[2]);
6554 struct sort_inout_decls
{
6555 bool operator()(const struct inout_decl
&a
, const struct inout_decl
&b
) const {
6556 return mapping
[a
.mesa_index
] < mapping
[b
.mesa_index
];
6559 const ubyte
*mapping
;
6562 /* Sort the given array of decls by the corresponding slot (TGSI file index).
6564 * This is for the benefit of older drivers which are broken when the
6565 * declarations aren't sorted in this way.
6568 sort_inout_decls_by_slot(struct inout_decl
*decls
,
6570 const ubyte mapping
[])
6572 sort_inout_decls sorter
;
6573 sorter
.mapping
= mapping
;
6574 std::sort(decls
, decls
+ count
, sorter
);
6577 static enum tgsi_interpolate_mode
6578 st_translate_interp(enum glsl_interp_mode glsl_qual
, GLuint varying
)
6580 switch (glsl_qual
) {
6581 case INTERP_MODE_NONE
:
6582 if (varying
== VARYING_SLOT_COL0
|| varying
== VARYING_SLOT_COL1
)
6583 return TGSI_INTERPOLATE_COLOR
;
6584 return TGSI_INTERPOLATE_PERSPECTIVE
;
6585 case INTERP_MODE_SMOOTH
:
6586 return TGSI_INTERPOLATE_PERSPECTIVE
;
6587 case INTERP_MODE_FLAT
:
6588 return TGSI_INTERPOLATE_CONSTANT
;
6589 case INTERP_MODE_NOPERSPECTIVE
:
6590 return TGSI_INTERPOLATE_LINEAR
;
6592 assert(0 && "unexpected interp mode in st_translate_interp()");
6593 return TGSI_INTERPOLATE_PERSPECTIVE
;
6598 * Translate intermediate IR (glsl_to_tgsi_instruction) to TGSI format.
6599 * \param program the program to translate
6600 * \param numInputs number of input registers used
6601 * \param inputMapping maps Mesa fragment program inputs to TGSI generic
6603 * \param inputSemanticName the TGSI_SEMANTIC flag for each input
6604 * \param inputSemanticIndex the semantic index (ex: which texcoord) for
6606 * \param interpMode the TGSI_INTERPOLATE_LINEAR/PERSP mode for each input
6607 * \param numOutputs number of output registers used
6608 * \param outputMapping maps Mesa fragment program outputs to TGSI
6610 * \param outputSemanticName the TGSI_SEMANTIC flag for each output
6611 * \param outputSemanticIndex the semantic index (ex: which texcoord) for
6614 * \return PIPE_OK or PIPE_ERROR_OUT_OF_MEMORY
6616 extern "C" enum pipe_error
6617 st_translate_program(
6618 struct gl_context
*ctx
,
6619 enum pipe_shader_type procType
,
6620 struct ureg_program
*ureg
,
6621 glsl_to_tgsi_visitor
*program
,
6622 const struct gl_program
*proginfo
,
6624 const ubyte inputMapping
[],
6625 const ubyte inputSlotToAttr
[],
6626 const ubyte inputSemanticName
[],
6627 const ubyte inputSemanticIndex
[],
6628 const ubyte interpMode
[],
6630 const ubyte outputMapping
[],
6631 const ubyte outputSemanticName
[],
6632 const ubyte outputSemanticIndex
[])
6634 struct pipe_screen
*screen
= st_context(ctx
)->pipe
->screen
;
6635 struct st_translate
*t
;
6637 struct gl_program_constants
*frag_const
=
6638 &ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
];
6639 enum pipe_error ret
= PIPE_OK
;
6641 assert(numInputs
<= ARRAY_SIZE(t
->inputs
));
6642 assert(numOutputs
<= ARRAY_SIZE(t
->outputs
));
6644 ASSERT_BITFIELD_SIZE(st_src_reg
, type
, GLSL_TYPE_ERROR
);
6645 ASSERT_BITFIELD_SIZE(st_dst_reg
, type
, GLSL_TYPE_ERROR
);
6646 ASSERT_BITFIELD_SIZE(glsl_to_tgsi_instruction
, tex_type
, GLSL_TYPE_ERROR
);
6647 ASSERT_BITFIELD_SIZE(glsl_to_tgsi_instruction
, image_format
, PIPE_FORMAT_COUNT
);
6648 ASSERT_BITFIELD_SIZE(glsl_to_tgsi_instruction
, tex_target
,
6649 (gl_texture_index
) (NUM_TEXTURE_TARGETS
- 1));
6650 ASSERT_BITFIELD_SIZE(glsl_to_tgsi_instruction
, image_format
,
6651 (enum pipe_format
) (PIPE_FORMAT_COUNT
- 1));
6652 ASSERT_BITFIELD_SIZE(glsl_to_tgsi_instruction
, op
,
6653 (enum tgsi_opcode
) (TGSI_OPCODE_LAST
- 1));
6655 t
= CALLOC_STRUCT(st_translate
);
6657 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
6661 t
->procType
= procType
;
6662 t
->need_uarl
= !screen
->get_param(screen
, PIPE_CAP_TGSI_ANY_REG_AS_ADDRESS
);
6663 t
->inputMapping
= inputMapping
;
6664 t
->outputMapping
= outputMapping
;
6666 t
->num_temp_arrays
= program
->next_array
;
6667 if (t
->num_temp_arrays
)
6668 t
->arrays
= (struct ureg_dst
*)
6669 calloc(t
->num_temp_arrays
, sizeof(t
->arrays
[0]));
6672 * Declare input attributes.
6675 case PIPE_SHADER_FRAGMENT
:
6676 case PIPE_SHADER_GEOMETRY
:
6677 case PIPE_SHADER_TESS_EVAL
:
6678 case PIPE_SHADER_TESS_CTRL
:
6679 sort_inout_decls_by_slot(program
->inputs
, program
->num_inputs
, inputMapping
);
6681 for (i
= 0; i
< program
->num_inputs
; ++i
) {
6682 struct inout_decl
*decl
= &program
->inputs
[i
];
6683 unsigned slot
= inputMapping
[decl
->mesa_index
];
6684 struct ureg_src src
;
6685 ubyte tgsi_usage_mask
= decl
->usage_mask
;
6687 if (glsl_base_type_is_64bit(decl
->base_type
)) {
6688 if (tgsi_usage_mask
== 1)
6689 tgsi_usage_mask
= TGSI_WRITEMASK_XY
;
6690 else if (tgsi_usage_mask
== 2)
6691 tgsi_usage_mask
= TGSI_WRITEMASK_ZW
;
6693 tgsi_usage_mask
= TGSI_WRITEMASK_XYZW
;
6696 enum tgsi_interpolate_mode interp_mode
= TGSI_INTERPOLATE_CONSTANT
;
6697 enum tgsi_interpolate_loc interp_location
= TGSI_INTERPOLATE_LOC_CENTER
;
6698 if (procType
== PIPE_SHADER_FRAGMENT
) {
6700 interp_mode
= interpMode
[slot
] != TGSI_INTERPOLATE_COUNT
?
6701 (enum tgsi_interpolate_mode
) interpMode
[slot
] :
6702 st_translate_interp(decl
->interp
, inputSlotToAttr
[slot
]);
6704 interp_location
= (enum tgsi_interpolate_loc
) decl
->interp_loc
;
6707 src
= ureg_DECL_fs_input_cyl_centroid_layout(ureg
,
6708 (enum tgsi_semantic
) inputSemanticName
[slot
],
6709 inputSemanticIndex
[slot
],
6710 interp_mode
, 0, interp_location
, slot
, tgsi_usage_mask
,
6711 decl
->array_id
, decl
->size
);
6713 for (unsigned j
= 0; j
< decl
->size
; ++j
) {
6714 if (t
->inputs
[slot
+ j
].File
!= TGSI_FILE_INPUT
) {
6715 /* The ArrayID is set up in dst_register */
6716 t
->inputs
[slot
+ j
] = src
;
6717 t
->inputs
[slot
+ j
].ArrayID
= 0;
6718 t
->inputs
[slot
+ j
].Index
+= j
;
6723 case PIPE_SHADER_VERTEX
:
6724 for (i
= 0; i
< numInputs
; i
++) {
6725 t
->inputs
[i
] = ureg_DECL_vs_input(ureg
, i
);
6728 case PIPE_SHADER_COMPUTE
:
6735 * Declare output attributes.
6738 case PIPE_SHADER_FRAGMENT
:
6739 case PIPE_SHADER_COMPUTE
:
6741 case PIPE_SHADER_GEOMETRY
:
6742 case PIPE_SHADER_TESS_EVAL
:
6743 case PIPE_SHADER_TESS_CTRL
:
6744 case PIPE_SHADER_VERTEX
:
6745 sort_inout_decls_by_slot(program
->outputs
, program
->num_outputs
, outputMapping
);
6747 for (i
= 0; i
< program
->num_outputs
; ++i
) {
6748 struct inout_decl
*decl
= &program
->outputs
[i
];
6749 unsigned slot
= outputMapping
[decl
->mesa_index
];
6750 struct ureg_dst dst
;
6751 ubyte tgsi_usage_mask
= decl
->usage_mask
;
6753 if (glsl_base_type_is_64bit(decl
->base_type
)) {
6754 if (tgsi_usage_mask
== 1)
6755 tgsi_usage_mask
= TGSI_WRITEMASK_XY
;
6756 else if (tgsi_usage_mask
== 2)
6757 tgsi_usage_mask
= TGSI_WRITEMASK_ZW
;
6759 tgsi_usage_mask
= TGSI_WRITEMASK_XYZW
;
6762 dst
= ureg_DECL_output_layout(ureg
,
6763 (enum tgsi_semantic
) outputSemanticName
[slot
],
6764 outputSemanticIndex
[slot
],
6765 decl
->gs_out_streams
,
6766 slot
, tgsi_usage_mask
, decl
->array_id
, decl
->size
, decl
->invariant
);
6767 dst
.Invariant
= decl
->invariant
;
6768 for (unsigned j
= 0; j
< decl
->size
; ++j
) {
6769 if (t
->outputs
[slot
+ j
].File
!= TGSI_FILE_OUTPUT
) {
6770 /* The ArrayID is set up in dst_register */
6771 t
->outputs
[slot
+ j
] = dst
;
6772 t
->outputs
[slot
+ j
].ArrayID
= 0;
6773 t
->outputs
[slot
+ j
].Index
+= j
;
6774 t
->outputs
[slot
+ j
].Invariant
= decl
->invariant
;
6783 if (procType
== PIPE_SHADER_FRAGMENT
) {
6784 if (program
->shader
->Program
->info
.fs
.early_fragment_tests
||
6785 program
->shader
->Program
->info
.fs
.post_depth_coverage
) {
6786 ureg_property(ureg
, TGSI_PROPERTY_FS_EARLY_DEPTH_STENCIL
, 1);
6788 if (program
->shader
->Program
->info
.fs
.post_depth_coverage
)
6789 ureg_property(ureg
, TGSI_PROPERTY_FS_POST_DEPTH_COVERAGE
, 1);
6792 if (proginfo
->info
.inputs_read
& VARYING_BIT_POS
) {
6793 /* Must do this after setting up t->inputs. */
6794 emit_wpos(st_context(ctx
), t
, proginfo
, ureg
,
6795 program
->wpos_transform_const
);
6798 if (proginfo
->info
.inputs_read
& VARYING_BIT_FACE
)
6799 emit_face_var(ctx
, t
);
6801 for (i
= 0; i
< numOutputs
; i
++) {
6802 switch (outputSemanticName
[i
]) {
6803 case TGSI_SEMANTIC_POSITION
:
6804 t
->outputs
[i
] = ureg_DECL_output(ureg
,
6805 TGSI_SEMANTIC_POSITION
, /* Z/Depth */
6806 outputSemanticIndex
[i
]);
6807 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_Z
);
6809 case TGSI_SEMANTIC_STENCIL
:
6810 t
->outputs
[i
] = ureg_DECL_output(ureg
,
6811 TGSI_SEMANTIC_STENCIL
, /* Stencil */
6812 outputSemanticIndex
[i
]);
6813 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_Y
);
6815 case TGSI_SEMANTIC_COLOR
:
6816 t
->outputs
[i
] = ureg_DECL_output(ureg
,
6817 TGSI_SEMANTIC_COLOR
,
6818 outputSemanticIndex
[i
]);
6820 case TGSI_SEMANTIC_SAMPLEMASK
:
6821 t
->outputs
[i
] = ureg_DECL_output(ureg
,
6822 TGSI_SEMANTIC_SAMPLEMASK
,
6823 outputSemanticIndex
[i
]);
6824 /* TODO: If we ever support more than 32 samples, this will have
6825 * to become an array.
6827 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_X
);
6830 assert(!"fragment shader outputs must be POSITION/STENCIL/COLOR");
6831 ret
= PIPE_ERROR_BAD_INPUT
;
6836 else if (procType
== PIPE_SHADER_VERTEX
) {
6837 for (i
= 0; i
< numOutputs
; i
++) {
6838 if (outputSemanticName
[i
] == TGSI_SEMANTIC_FOG
) {
6839 /* force register to contain a fog coordinate in the form (F, 0, 0, 1). */
6841 ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_YZW
),
6842 ureg_imm4f(ureg
, 0.0f
, 0.0f
, 0.0f
, 1.0f
));
6843 t
->outputs
[i
] = ureg_writemask(t
->outputs
[i
], TGSI_WRITEMASK_X
);
6848 if (procType
== PIPE_SHADER_COMPUTE
) {
6849 emit_compute_block_size(proginfo
, ureg
);
6852 /* Declare address register.
6854 if (program
->num_address_regs
> 0) {
6855 assert(program
->num_address_regs
<= 3);
6856 for (int i
= 0; i
< program
->num_address_regs
; i
++)
6857 t
->address
[i
] = ureg_DECL_address(ureg
);
6860 /* Declare misc input registers
6863 GLbitfield64 sysInputs
= proginfo
->info
.system_values_read
;
6865 for (i
= 0; sysInputs
; i
++) {
6866 if (sysInputs
& (1ull << i
)) {
6867 enum tgsi_semantic semName
= _mesa_sysval_to_semantic(i
);
6869 t
->systemValues
[i
] = ureg_DECL_system_value(ureg
, semName
, 0);
6871 if (semName
== TGSI_SEMANTIC_INSTANCEID
||
6872 semName
== TGSI_SEMANTIC_VERTEXID
) {
6873 /* From Gallium perspective, these system values are always
6874 * integer, and require native integer support. However, if
6875 * native integer is supported on the vertex stage but not the
6876 * pixel stage (e.g, i915g + draw), Mesa will generate IR that
6877 * assumes these system values are floats. To resolve the
6878 * inconsistency, we insert a U2F.
6880 struct st_context
*st
= st_context(ctx
);
6881 struct pipe_screen
*pscreen
= st
->pipe
->screen
;
6882 assert(procType
== PIPE_SHADER_VERTEX
);
6883 assert(pscreen
->get_shader_param(pscreen
, PIPE_SHADER_VERTEX
, PIPE_SHADER_CAP_INTEGERS
));
6885 if (!ctx
->Const
.NativeIntegers
) {
6886 struct ureg_dst temp
= ureg_DECL_local_temporary(t
->ureg
);
6887 ureg_U2F(t
->ureg
, ureg_writemask(temp
, TGSI_WRITEMASK_X
),
6888 t
->systemValues
[i
]);
6889 t
->systemValues
[i
] = ureg_scalar(ureg_src(temp
), 0);
6893 if (procType
== PIPE_SHADER_FRAGMENT
&&
6894 semName
== TGSI_SEMANTIC_POSITION
)
6895 emit_wpos(st_context(ctx
), t
, proginfo
, ureg
,
6896 program
->wpos_transform_const
);
6898 if (procType
== PIPE_SHADER_FRAGMENT
&&
6899 semName
== TGSI_SEMANTIC_SAMPLEPOS
)
6900 emit_samplepos_adjustment(t
, program
->wpos_transform_const
);
6902 sysInputs
&= ~(1ull << i
);
6907 t
->array_sizes
= program
->array_sizes
;
6908 t
->input_decls
= program
->inputs
;
6909 t
->num_input_decls
= program
->num_inputs
;
6910 t
->output_decls
= program
->outputs
;
6911 t
->num_output_decls
= program
->num_outputs
;
6913 /* Emit constants and uniforms. TGSI uses a single index space for these,
6914 * so we put all the translated regs in t->constants.
6916 if (proginfo
->Parameters
) {
6917 t
->constants
= (struct ureg_src
*)
6918 calloc(proginfo
->Parameters
->NumParameters
, sizeof(t
->constants
[0]));
6919 if (t
->constants
== NULL
) {
6920 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
6923 t
->num_constants
= proginfo
->Parameters
->NumParameters
;
6925 for (i
= 0; i
< proginfo
->Parameters
->NumParameters
; i
++) {
6926 unsigned pvo
= proginfo
->Parameters
->ParameterValueOffset
[i
];
6928 switch (proginfo
->Parameters
->Parameters
[i
].Type
) {
6929 case PROGRAM_STATE_VAR
:
6930 case PROGRAM_UNIFORM
:
6931 t
->constants
[i
] = ureg_DECL_constant(ureg
, i
);
6934 /* Emit immediates for PROGRAM_CONSTANT only when there's no indirect
6935 * addressing of the const buffer.
6936 * FIXME: Be smarter and recognize param arrays:
6937 * indirect addressing is only valid within the referenced
6940 case PROGRAM_CONSTANT
:
6941 if (program
->indirect_addr_consts
)
6942 t
->constants
[i
] = ureg_DECL_constant(ureg
, i
);
6944 t
->constants
[i
] = emit_immediate(t
,
6945 proginfo
->Parameters
->ParameterValues
+ pvo
,
6946 proginfo
->Parameters
->Parameters
[i
].DataType
,
6955 for (i
= 0; i
< proginfo
->info
.num_ubos
; i
++) {
6956 unsigned size
= proginfo
->sh
.UniformBlocks
[i
]->UniformBufferSize
;
6957 unsigned num_const_vecs
= (size
+ 15) / 16;
6958 unsigned first
, last
;
6959 assert(num_const_vecs
> 0);
6961 last
= num_const_vecs
> 0 ? num_const_vecs
- 1 : 0;
6962 ureg_DECL_constant2D(t
->ureg
, first
, last
, i
+ 1);
6965 /* Emit immediate values.
6967 t
->immediates
= (struct ureg_src
*)
6968 calloc(program
->num_immediates
, sizeof(struct ureg_src
));
6969 if (t
->immediates
== NULL
) {
6970 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
6973 t
->num_immediates
= program
->num_immediates
;
6976 foreach_in_list(immediate_storage
, imm
, &program
->immediates
) {
6977 assert(i
< program
->num_immediates
);
6978 t
->immediates
[i
++] = emit_immediate(t
, imm
->values
, imm
->type
, imm
->size32
);
6980 assert(i
== program
->num_immediates
);
6982 /* texture samplers */
6983 for (i
= 0; i
< frag_const
->MaxTextureImageUnits
; i
++) {
6984 if (program
->samplers_used
& (1u << i
)) {
6985 enum tgsi_return_type type
=
6986 st_translate_texture_type(program
->sampler_types
[i
]);
6988 t
->samplers
[i
] = ureg_DECL_sampler(ureg
, i
);
6990 ureg_DECL_sampler_view(ureg
, i
, program
->sampler_targets
[i
],
6991 type
, type
, type
, type
);
6995 /* Declare atomic and shader storage buffers. */
6997 struct gl_program
*prog
= program
->prog
;
6999 if (!st_context(ctx
)->has_hw_atomics
) {
7000 for (i
= 0; i
< prog
->info
.num_abos
; i
++) {
7001 unsigned index
= prog
->sh
.AtomicBuffers
[i
]->Binding
;
7002 assert(index
< frag_const
->MaxAtomicBuffers
);
7003 t
->buffers
[index
] = ureg_DECL_buffer(ureg
, index
, true);
7006 for (i
= 0; i
< program
->num_atomics
; i
++) {
7007 struct hwatomic_decl
*ainfo
= &program
->atomic_info
[i
];
7008 gl_uniform_storage
*uni_storage
= &prog
->sh
.data
->UniformStorage
[ainfo
->location
];
7009 int base
= uni_storage
->offset
/ ATOMIC_COUNTER_SIZE
;
7010 ureg_DECL_hw_atomic(ureg
, base
, base
+ ainfo
->size
- 1, ainfo
->binding
,
7015 assert(prog
->info
.num_ssbos
<= frag_const
->MaxShaderStorageBlocks
);
7016 for (i
= 0; i
< prog
->info
.num_ssbos
; i
++) {
7018 if (!st_context(ctx
)->has_hw_atomics
)
7019 index
+= frag_const
->MaxAtomicBuffers
;
7021 t
->buffers
[index
] = ureg_DECL_buffer(ureg
, index
, false);
7025 if (program
->use_shared_memory
)
7026 t
->shared_memory
= ureg_DECL_memory(ureg
, TGSI_MEMORY_TYPE_SHARED
);
7028 for (i
= 0; i
< program
->shader
->Program
->info
.num_images
; i
++) {
7029 if (program
->images_used
& (1 << i
)) {
7030 t
->images
[i
] = ureg_DECL_image(ureg
, i
,
7031 program
->image_targets
[i
],
7032 program
->image_formats
[i
],
7033 program
->image_wr
[i
],
7038 /* Emit each instruction in turn:
7040 foreach_in_list(glsl_to_tgsi_instruction
, inst
, &program
->instructions
)
7041 compile_tgsi_instruction(t
, inst
);
7043 /* Set the next shader stage hint for VS and TES. */
7045 case PIPE_SHADER_VERTEX
:
7046 case PIPE_SHADER_TESS_EVAL
:
7047 if (program
->shader_program
->SeparateShader
)
7050 for (i
= program
->shader
->Stage
+1; i
<= MESA_SHADER_FRAGMENT
; i
++) {
7051 if (program
->shader_program
->_LinkedShaders
[i
]) {
7052 ureg_set_next_shader_processor(
7053 ureg
, pipe_shader_type_from_mesa((gl_shader_stage
)i
));
7059 ; /* nothing - silence compiler warning */
7067 t
->num_constants
= 0;
7068 free(t
->immediates
);
7069 t
->num_immediates
= 0;
7075 /* ----------------------------- End TGSI code ------------------------------ */
7079 * Convert a shader's GLSL IR into a Mesa gl_program, although without
7080 * generating Mesa IR.
7082 static struct gl_program
*
7083 get_mesa_program_tgsi(struct gl_context
*ctx
,
7084 struct gl_shader_program
*shader_program
,
7085 struct gl_linked_shader
*shader
)
7087 glsl_to_tgsi_visitor
* v
;
7088 struct gl_program
*prog
;
7089 struct gl_shader_compiler_options
*options
=
7090 &ctx
->Const
.ShaderCompilerOptions
[shader
->Stage
];
7091 struct pipe_screen
*pscreen
= ctx
->st
->pipe
->screen
;
7092 enum pipe_shader_type ptarget
= pipe_shader_type_from_mesa(shader
->Stage
);
7093 unsigned skip_merge_registers
;
7095 validate_ir_tree(shader
->ir
);
7097 prog
= shader
->Program
;
7099 prog
->Parameters
= _mesa_new_parameter_list();
7100 v
= new glsl_to_tgsi_visitor();
7103 v
->shader_program
= shader_program
;
7105 v
->options
= options
;
7106 v
->native_integers
= ctx
->Const
.NativeIntegers
;
7108 v
->have_sqrt
= pscreen
->get_shader_param(pscreen
, ptarget
,
7109 PIPE_SHADER_CAP_TGSI_SQRT_SUPPORTED
);
7110 v
->have_fma
= pscreen
->get_shader_param(pscreen
, ptarget
,
7111 PIPE_SHADER_CAP_TGSI_FMA_SUPPORTED
);
7112 v
->has_tex_txf_lz
= pscreen
->get_param(pscreen
,
7113 PIPE_CAP_TGSI_TEX_TXF_LZ
);
7114 v
->need_uarl
= !pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_ANY_REG_AS_ADDRESS
);
7116 v
->variables
= _mesa_hash_table_create(v
->mem_ctx
, _mesa_hash_pointer
,
7117 _mesa_key_pointer_equal
);
7118 skip_merge_registers
=
7119 pscreen
->get_shader_param(pscreen
, ptarget
,
7120 PIPE_SHADER_CAP_TGSI_SKIP_MERGE_REGISTERS
);
7122 _mesa_generate_parameters_list_for_uniforms(ctx
, shader_program
, shader
,
7125 /* Remove reads from output registers. */
7126 if (!pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_CAN_READ_OUTPUTS
))
7127 lower_output_reads(shader
->Stage
, shader
->ir
);
7129 /* Emit intermediate IR for main(). */
7130 visit_exec_list(shader
->ir
, v
);
7133 /* Print out some information (for debugging purposes) used by the
7134 * optimization passes. */
7137 int *first_writes
= ralloc_array(v
->mem_ctx
, int, v
->next_temp
);
7138 int *first_reads
= ralloc_array(v
->mem_ctx
, int, v
->next_temp
);
7139 int *last_writes
= ralloc_array(v
->mem_ctx
, int, v
->next_temp
);
7140 int *last_reads
= ralloc_array(v
->mem_ctx
, int, v
->next_temp
);
7142 for (i
= 0; i
< v
->next_temp
; i
++) {
7143 first_writes
[i
] = -1;
7144 first_reads
[i
] = -1;
7145 last_writes
[i
] = -1;
7148 v
->get_first_temp_read(first_reads
);
7149 v
->get_last_temp_read_first_temp_write(last_reads
, first_writes
);
7150 v
->get_last_temp_write(last_writes
);
7151 for (i
= 0; i
< v
->next_temp
; i
++)
7152 printf("Temp %d: FR=%3d FW=%3d LR=%3d LW=%3d\n", i
, first_reads
[i
],
7156 ralloc_free(first_writes
);
7157 ralloc_free(first_reads
);
7158 ralloc_free(last_writes
);
7159 ralloc_free(last_reads
);
7163 /* Perform optimizations on the instructions in the glsl_to_tgsi_visitor. */
7165 v
->copy_propagate();
7167 while (v
->eliminate_dead_code());
7169 v
->merge_two_dsts();
7171 if (!skip_merge_registers
) {
7173 v
->copy_propagate();
7174 while (v
->eliminate_dead_code());
7176 v
->merge_registers();
7177 v
->copy_propagate();
7178 while (v
->eliminate_dead_code());
7181 v
->renumber_registers();
7183 /* Write the END instruction. */
7184 v
->emit_asm(NULL
, TGSI_OPCODE_END
);
7186 if (ctx
->_Shader
->Flags
& GLSL_DUMP
) {
7188 _mesa_log("GLSL IR for linked %s program %d:\n",
7189 _mesa_shader_stage_to_string(shader
->Stage
),
7190 shader_program
->Name
);
7191 _mesa_print_ir(_mesa_get_log_file(), shader
->ir
, NULL
);
7195 do_set_program_inouts(shader
->ir
, prog
, shader
->Stage
);
7197 _mesa_copy_linked_program_data(shader_program
, shader
);
7199 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_SKIP_SHRINK_IO_ARRAYS
)) {
7200 mark_array_io(v
->inputs
, v
->num_inputs
,
7201 &prog
->info
.inputs_read
,
7202 prog
->DualSlotInputs
,
7203 &prog
->info
.patch_inputs_read
);
7205 mark_array_io(v
->outputs
, v
->num_outputs
,
7206 &prog
->info
.outputs_written
, 0ULL,
7207 &prog
->info
.patch_outputs_written
);
7209 shrink_array_declarations(v
->inputs
, v
->num_inputs
,
7210 &prog
->info
.inputs_read
,
7211 prog
->DualSlotInputs
,
7212 &prog
->info
.patch_inputs_read
);
7213 shrink_array_declarations(v
->outputs
, v
->num_outputs
,
7214 &prog
->info
.outputs_written
, 0ULL,
7215 &prog
->info
.patch_outputs_written
);
7218 count_resources(v
, prog
);
7220 /* The GLSL IR won't be needed anymore. */
7221 ralloc_free(shader
->ir
);
7224 /* This must be done before the uniform storage is associated. */
7225 if (shader
->Stage
== MESA_SHADER_FRAGMENT
&&
7226 (prog
->info
.inputs_read
& VARYING_BIT_POS
||
7227 prog
->info
.system_values_read
& (1ull << SYSTEM_VALUE_FRAG_COORD
) ||
7228 prog
->info
.system_values_read
& (1ull << SYSTEM_VALUE_SAMPLE_POS
))) {
7229 static const gl_state_index16 wposTransformState
[STATE_LENGTH
] = {
7230 STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
7233 v
->wpos_transform_const
= _mesa_add_state_reference(prog
->Parameters
,
7234 wposTransformState
);
7237 /* Avoid reallocation of the program parameter list, because the uniform
7238 * storage is only associated with the original parameter list.
7239 * This should be enough for Bitmap and DrawPixels constants.
7241 _mesa_reserve_parameter_storage(prog
->Parameters
, 8);
7243 /* This has to be done last. Any operation the can cause
7244 * prog->ParameterValues to get reallocated (e.g., anything that adds a
7245 * program constant) has to happen before creating this linkage.
7247 _mesa_associate_uniform_storage(ctx
, shader_program
, prog
, true);
7248 if (!shader_program
->data
->LinkStatus
) {
7249 free_glsl_to_tgsi_visitor(v
);
7250 _mesa_reference_program(ctx
, &shader
->Program
, NULL
);
7254 struct st_vertex_program
*stvp
;
7255 struct st_fragment_program
*stfp
;
7256 struct st_common_program
*stp
;
7257 struct st_compute_program
*stcp
;
7259 switch (shader
->Stage
) {
7260 case MESA_SHADER_VERTEX
:
7261 stvp
= (struct st_vertex_program
*)prog
;
7262 stvp
->glsl_to_tgsi
= v
;
7264 case MESA_SHADER_FRAGMENT
:
7265 stfp
= (struct st_fragment_program
*)prog
;
7266 stfp
->glsl_to_tgsi
= v
;
7268 case MESA_SHADER_TESS_CTRL
:
7269 case MESA_SHADER_TESS_EVAL
:
7270 case MESA_SHADER_GEOMETRY
:
7271 stp
= st_common_program(prog
);
7272 stp
->glsl_to_tgsi
= v
;
7274 case MESA_SHADER_COMPUTE
:
7275 stcp
= (struct st_compute_program
*)prog
;
7276 stcp
->glsl_to_tgsi
= v
;
7279 assert(!"should not be reached");
7283 PRINT_STATS(v
->print_stats());
7288 /* See if there are unsupported control flow statements. */
7289 class ir_control_flow_info_visitor
: public ir_hierarchical_visitor
{
7291 const struct gl_shader_compiler_options
*options
;
7293 ir_control_flow_info_visitor(const struct gl_shader_compiler_options
*options
)
7299 virtual ir_visitor_status
visit_enter(ir_function
*ir
)
7301 /* Other functions are skipped (same as glsl_to_tgsi). */
7302 if (strcmp(ir
->name
, "main") == 0)
7303 return visit_continue
;
7305 return visit_continue_with_parent
;
7308 virtual ir_visitor_status
visit_enter(ir_call
*ir
)
7310 if (!ir
->callee
->is_intrinsic()) {
7311 unsupported
= true; /* it's a function call */
7314 return visit_continue
;
7317 virtual ir_visitor_status
visit_enter(ir_return
*ir
)
7319 if (options
->EmitNoMainReturn
) {
7323 return visit_continue
;
7330 has_unsupported_control_flow(exec_list
*ir
,
7331 const struct gl_shader_compiler_options
*options
)
7333 ir_control_flow_info_visitor
visitor(options
);
7334 visit_list_elements(&visitor
, ir
);
7335 return visitor
.unsupported
;
7340 * This actually involves converting GLSL IR into an intermediate TGSI-like IR
7341 * with code lowering and other optimizations.
7344 st_link_tgsi(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
7346 struct pipe_screen
*pscreen
= ctx
->st
->pipe
->screen
;
7348 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
7349 struct gl_linked_shader
*shader
= prog
->_LinkedShaders
[i
];
7353 exec_list
*ir
= shader
->ir
;
7354 gl_shader_stage stage
= shader
->Stage
;
7355 enum pipe_shader_type ptarget
= pipe_shader_type_from_mesa(stage
);
7356 const struct gl_shader_compiler_options
*options
=
7357 &ctx
->Const
.ShaderCompilerOptions
[stage
];
7359 unsigned if_threshold
= pscreen
->get_shader_param(pscreen
, ptarget
,
7360 PIPE_SHADER_CAP_LOWER_IF_THRESHOLD
);
7361 if (ctx
->Const
.GLSLOptimizeConservatively
) {
7362 /* Do it once and repeat only if there's unsupported control flow. */
7364 do_common_optimization(ir
, true, true, options
,
7365 ctx
->Const
.NativeIntegers
);
7366 lower_if_to_cond_assign((gl_shader_stage
)i
, ir
,
7367 options
->MaxIfDepth
, if_threshold
);
7368 } while (has_unsupported_control_flow(ir
, options
));
7370 /* Repeat it until it stops making changes. */
7373 progress
= do_common_optimization(ir
, true, true, options
,
7374 ctx
->Const
.NativeIntegers
);
7375 progress
|= lower_if_to_cond_assign((gl_shader_stage
)i
, ir
,
7376 options
->MaxIfDepth
, if_threshold
);
7380 /* Do this again to lower ir_binop_vector_extract introduced
7381 * by optimization passes.
7383 do_vec_index_to_cond_assign(ir
);
7385 validate_ir_tree(ir
);
7387 struct gl_program
*linked_prog
=
7388 get_mesa_program_tgsi(ctx
, prog
, shader
);
7389 st_set_prog_affected_state_flags(linked_prog
);
7392 if (!ctx
->Driver
.ProgramStringNotify(ctx
,
7393 _mesa_shader_stage_to_program(i
),
7395 _mesa_reference_program(ctx
, &shader
->Program
, NULL
);
7407 st_translate_stream_output_info(struct gl_transform_feedback_info
*info
,
7408 const ubyte outputMapping
[],
7409 struct pipe_stream_output_info
*so
)
7414 so
->num_outputs
= 0;
7418 for (i
= 0; i
< info
->NumOutputs
; i
++) {
7419 so
->output
[i
].register_index
=
7420 outputMapping
[info
->Outputs
[i
].OutputRegister
];
7421 so
->output
[i
].start_component
= info
->Outputs
[i
].ComponentOffset
;
7422 so
->output
[i
].num_components
= info
->Outputs
[i
].NumComponents
;
7423 so
->output
[i
].output_buffer
= info
->Outputs
[i
].OutputBuffer
;
7424 so
->output
[i
].dst_offset
= info
->Outputs
[i
].DstOffset
;
7425 so
->output
[i
].stream
= info
->Outputs
[i
].StreamId
;
7428 for (i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
7429 so
->stride
[i
] = info
->Buffers
[i
].Stride
;
7431 so
->num_outputs
= info
->NumOutputs
;